qt5base-lts/tests/manual/qglyphruns/view.cpp
Eskil Abrahamsen Blomfeldt 1fe7144a07 Introduce QGlyphRun::stringIndexes()
This introduces a way to trace each entry in the glyph index
array to a specific index in the original text passed to
QTextLayout, as well as a convenience function to access
the original string from the QGlyphRun.

The index information is stored in the logClusters array internally
in Qt, but it contains the inverse information: For each
character in the output string, it contains an index into the
glyph array. In order to get the string indexes for each glyph,
which makes a lot more sense in the context of the QGlyphRun
API, we need to do a little search to construct the data.

To avoid adding unnecessary allocations, we make the new APIs
opt-in. If you do not specify anything, you will only get the
glyph indexes and glyph positions as before. However, you
can now specify exactly which parts of the layout to extract
using an optional flags parameter.

This also adds a manual test which can be very handy to
visualize QTextLayouts and how they are split into QGlyphRuns.

Fixes: QTBUG-103932
Change-Id: Ie4288fff338b9482aba0aba29fc7e1e59fa60900
Reviewed-by: Lars Knoll <lars@knoll.priv.no>
2022-09-29 18:06:40 +02:00

71 lines
1.6 KiB
C++

// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "view.h"
#include <QTextLayout>
#include <QPainter>
View::View(QWidget *parent)
: QWidget{parent}
{
setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum));
}
View::~View()
{
delete m_layout;
}
void View::updateLayout(const QString &sourceString,
float width,
QTextOption::WrapMode mode,
const QFont &font)
{
if (m_layout == nullptr)
m_layout = new QTextLayout;
m_layout->setText(sourceString);
QTextOption option;
option.setWrapMode(mode);
m_layout->setTextOption(option);
m_layout->setFont(font);
m_layout->beginLayout();
float y = 0.0f;
forever {
QTextLine line = m_layout->createLine();
if (!line.isValid())
break;
line.setLineWidth(width);
line.setPosition(QPointF(0, y));
y += line.height();
}
m_layout->endLayout();
update();
updateGeometry();
}
QSize View::sizeHint() const
{
if (m_layout != nullptr)
return m_layout->boundingRect().size().toSize();
else
return QSize(100, 100);
}
void View::paintEvent(QPaintEvent *)
{
QPainter p(this);
if (m_layout != nullptr)
m_layout->draw(&p, QPointF(0, 0));
if (!m_bounds.isEmpty()) {
p.setPen(Qt::NoPen);
p.setBrush(Qt::yellow);
p.setOpacity(0.25);
for (const QRect &r : m_bounds) {
p.drawRect(r);
}
}
}