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
5#include <QtGui/private/qrawfont_p.h>
6
8
9QSGSoftwareGlyphNode::QSGSoftwareGlyphNode()
10 : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 0)
11 , m_style(QQuickText::Normal)
12{
13 setMaterial((QSGMaterial*)1);
14 setGeometry(&m_geometry);
15}
16
17namespace {
18QRectF calculateBoundingRect(const QPointF &position, const QGlyphRun &glyphs)
19{
20 QFixed minX;
21 QFixed minY;
22 QFixed maxX;
23 QFixed maxY;
24
25 QRawFontPrivate *rawFontD = QRawFontPrivate::get(glyphs.rawFont());
26 QFontEngine *fontEngine = rawFontD->fontEngine;
27
28 QFontEngine::GlyphFormat glyphFormat = fontEngine->glyphFormat != QFontEngine::Format_None ? fontEngine->glyphFormat : QFontEngine::Format_A32;
29
30 int margin = fontEngine->glyphMargin(glyphFormat);
31
32 const QVector<uint> glyphIndexes = glyphs.glyphIndexes();
33 const QVector<QPointF> glyphPositions = glyphs.positions();
34 for (int i = 0, n = qMin(glyphIndexes.size(), glyphPositions.size()); i < n; ++i) {
35 glyph_metrics_t gm = fontEngine->alphaMapBoundingBox(glyphIndexes.at(i), QFixedPoint(), QTransform(), glyphFormat);
36
37 gm.x += QFixed::fromReal(glyphPositions.at(i).x()) - margin;
38 gm.y += QFixed::fromReal(glyphPositions.at(i).y()) - margin;
39
40 if (i == 0) {
41 minX = gm.x;
42 minY = gm.y;
43 maxX = gm.x + gm.width;
44 maxY = gm.y + gm.height;
45 } else {
46 minX = qMin(gm.x, minX);
47 minY = qMin(gm.y, minY);
48 maxX = qMax(gm.x + gm.width, maxX);
49 maxY = qMax(gm.y + gm.height, maxY);
50 }
51 }
52
53 QRectF boundingRect(QPointF(minX.toReal(), minY.toReal()), QPointF(maxX.toReal(), maxY.toReal()));
54 return boundingRect.translated(position - QPointF(0.0, glyphs.rawFont().ascent()));
55}
56}
57
58void QSGSoftwareGlyphNode::setGlyphs(const QPointF &position, const QGlyphRun &glyphs)
59{
60 m_position = position;
61 m_glyphRun = glyphs;
62 // Decorations handled by text node
63 m_glyphRun.setOverline(false);
64 m_glyphRun.setStrikeOut(false);
65 m_glyphRun.setUnderline(false);
66
67 recalculateBoundingRect();
68}
69
70void QSGSoftwareGlyphNode::recalculateBoundingRect()
71{
72 int x1Offset = m_style == QQuickText::Outline ? -1 : 0;
73 int x2Offset = m_style == QQuickText::Outline ? 1 : 0;
74 int y1Offset = m_style == QQuickText::Outline || m_style == QQuickText::Sunken ? -1 : 0;
75 int y2Offset = m_style == QQuickText::Outline || m_style == QQuickText::Raised ? 1 : 0;
76
77 m_bounding_rect = calculateBoundingRect(m_position, m_glyphRun).adjusted(x1Offset, y1Offset, x2Offset, y2Offset);
78}
79
80void QSGSoftwareGlyphNode::setColor(const QColor &color)
81{
82 m_color = color;
83}
84
85void QSGSoftwareGlyphNode::setStyle(QQuickText::TextStyle style)
86{
87 m_style = style;
88
89 recalculateBoundingRect();
90}
91
92void QSGSoftwareGlyphNode::setStyleColor(const QColor &color)
93{
94 m_styleColor = color;
95}
96
97QPointF QSGSoftwareGlyphNode::baseLine() const
98{
99 return QPointF();
100}
101
102void QSGSoftwareGlyphNode::setPreferredAntialiasingMode(QSGGlyphNode::AntialiasingMode)
103{
104}
105
106void QSGSoftwareGlyphNode::update()
107{
108}
109
110void QSGSoftwareGlyphNode::paint(QPainter *painter)
111{
112 painter->setBrush(QBrush());
113 QPointF pos = m_position - QPointF(0, m_glyphRun.rawFont().ascent());
114
115 qreal offset = 1.0;
116 if (painter->device()->devicePixelRatio() > 0.0)
117 offset = 1.0 / painter->device()->devicePixelRatio();
118
119 switch (m_style) {
120 case QQuickText::Normal: break;
121 case QQuickText::Outline:
122 painter->setPen(m_styleColor);
123 painter->drawGlyphRun(pos + QPointF(0, offset), m_glyphRun);
124 painter->drawGlyphRun(pos + QPointF(0, -offset), m_glyphRun);
125 painter->drawGlyphRun(pos + QPointF(offset, 0), m_glyphRun);
126 painter->drawGlyphRun(pos + QPointF(-offset, 0), m_glyphRun);
127 break;
128 case QQuickText::Raised:
129 painter->setPen(m_styleColor);
130 painter->drawGlyphRun(pos + QPointF(0, offset), m_glyphRun);
131 break;
132 case QQuickText::Sunken:
133 painter->setPen(m_styleColor);
134 painter->drawGlyphRun(pos + QPointF(0, -offset), m_glyphRun);
135 break;
136 }
137
138 painter->setPen(m_color);
139 painter->drawGlyphRun(pos, m_glyphRun);
140}
141
142QT_END_NAMESPACE
Combined button and popup list for selecting options.