Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgsoftwareglyphnode.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include <QtGui/private/qrawfont_p.h>
7
9
10QSGSoftwareGlyphNode::QSGSoftwareGlyphNode()
11 : QSGGlyphNode(QSGTextNode::NativeRendering)
12 , m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 0)
13 , m_style(QQuickText::Normal)
14{
15 setMaterial((QSGMaterial*)1);
16 setGeometry(&m_geometry);
17}
18
19namespace {
20QRectF calculateBoundingRect(const QPointF &position, const QGlyphRun &glyphs)
21{
22 QFixed minX;
23 QFixed minY;
24 QFixed maxX;
25 QFixed maxY;
26
27 QRawFontPrivate *rawFontD = QRawFontPrivate::get(glyphs.rawFont());
28 QFontEngine *fontEngine = rawFontD->fontEngine;
29
30 QFontEngine::GlyphFormat glyphFormat = fontEngine->glyphFormat != QFontEngine::Format_None ? fontEngine->glyphFormat : QFontEngine::Format_A32;
31
32 int margin = fontEngine->glyphMargin(glyphFormat);
33
34 const QList<uint> glyphIndexes = glyphs.glyphIndexes();
35 const QList<QPointF> glyphPositions = glyphs.positions();
36 for (int i = 0, n = qMin(glyphIndexes.size(), glyphPositions.size()); i < n; ++i) {
37 glyph_metrics_t gm = fontEngine->alphaMapBoundingBox(glyphIndexes.at(i), QFixedPoint(), QTransform(), glyphFormat);
38
39 gm.x += QFixed::fromReal(glyphPositions.at(i).x()) - margin;
40 gm.y += QFixed::fromReal(glyphPositions.at(i).y()) - margin;
41
42 if (i == 0) {
43 minX = gm.x;
44 minY = gm.y;
45 maxX = gm.x + gm.width;
46 maxY = gm.y + gm.height;
47 } else {
48 minX = qMin(gm.x, minX);
49 minY = qMin(gm.y, minY);
50 maxX = qMax(gm.x + gm.width, maxX);
51 maxY = qMax(gm.y + gm.height, maxY);
52 }
53 }
54
55 QRectF boundingRect(QPointF(minX.toReal(), minY.toReal()), QPointF(maxX.toReal(), maxY.toReal()));
56 return boundingRect.translated(position - QPointF(0.0, glyphs.rawFont().ascent()));
57}
58}
59
60void QSGSoftwareGlyphNode::setGlyphs(const QPointF &position, const QGlyphRun &glyphs)
61{
62 m_position = position;
63 m_glyphRun = glyphs;
64 // Decorations handled by text node
65 m_glyphRun.setOverline(false);
66 m_glyphRun.setStrikeOut(false);
67 m_glyphRun.setUnderline(false);
68
69 recalculateBoundingRect();
70}
71
72void QSGSoftwareGlyphNode::recalculateBoundingRect()
73{
74 int x1Offset = m_style == QQuickText::Outline ? -1 : 0;
75 int x2Offset = m_style == QQuickText::Outline ? 1 : 0;
76 int y1Offset = m_style == QQuickText::Outline || m_style == QQuickText::Sunken ? -1 : 0;
77 int y2Offset = m_style == QQuickText::Outline || m_style == QQuickText::Raised ? 1 : 0;
78
79 m_bounding_rect = calculateBoundingRect(m_position, m_glyphRun).adjusted(x1Offset, y1Offset, x2Offset, y2Offset);
80}
81
82void QSGSoftwareGlyphNode::setColor(const QColor &color)
83{
84 m_color = color;
85}
86
87void QSGSoftwareGlyphNode::setStyle(QQuickText::TextStyle style)
88{
89 m_style = style;
90
91 recalculateBoundingRect();
92}
93
94void QSGSoftwareGlyphNode::setStyleColor(const QColor &color)
95{
96 m_styleColor = color;
97}
98
99QPointF QSGSoftwareGlyphNode::baseLine() const
100{
101 return QPointF();
102}
103
104void QSGSoftwareGlyphNode::setPreferredAntialiasingMode(QSGGlyphNode::AntialiasingMode)
105{
106}
107
108void QSGSoftwareGlyphNode::update()
109{
110 if (m_recycled) {
111 markDirty(DirtyGeometry);
112 m_recycled = false;
113 }
114}
115
116void QSGSoftwareGlyphNode::paint(QPainter *painter)
117{
118 painter->setBrush(QBrush());
119 QPointF pos = m_position - QPointF(0, m_glyphRun.rawFont().ascent());
120
121 qreal offset = 1.0;
122 if (painter->device()->devicePixelRatio() > 0.0)
123 offset = 1.0 / painter->device()->devicePixelRatio();
124
125 switch (m_style) {
126 case QQuickText::Normal: break;
127 case QQuickText::Outline:
128 painter->setPen(m_styleColor);
129 painter->drawGlyphRun(pos + QPointF(0, offset), m_glyphRun);
130 painter->drawGlyphRun(pos + QPointF(0, -offset), m_glyphRun);
131 painter->drawGlyphRun(pos + QPointF(offset, 0), m_glyphRun);
132 painter->drawGlyphRun(pos + QPointF(-offset, 0), m_glyphRun);
133 break;
134 case QQuickText::Raised:
135 painter->setPen(m_styleColor);
136 painter->drawGlyphRun(pos + QPointF(0, offset), m_glyphRun);
137 break;
138 case QQuickText::Sunken:
139 painter->setPen(m_styleColor);
140 painter->drawGlyphRun(pos + QPointF(0, -offset), m_glyphRun);
141 break;
142 }
143
144 painter->setPen(m_color);
145 painter->drawGlyphRun(pos, m_glyphRun);
146}
147
148void QSGSoftwareGlyphNode::recycle()
149{
150 QSGGlyphNode::recycle();
151 m_position = QPointF{};
152 m_glyphRun = QGlyphRun{};
153 m_color = QColor{};
154 m_geometry.allocate(0, 0);
155 m_style = QQuickText::Normal;
156 m_styleColor = QColor{};
157 m_recycled = true;
158}
159
160QT_END_NAMESPACE
Combined button and popup list for selecting options.