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
qsgcurveglyphatlas.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 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
9#include "util/qquadpath_p.h"
10
11#include <QtGui/qrawfont.h>
12#include <QtGui/qpainterpath.h>
13
15
16QSGCurveGlyphAtlas::QSGCurveGlyphAtlas(const QRawFont &font)
17 : m_font(font)
18{
19 // The font size used for the curve atlas currently affects the outlines, since we don't
20 // really support cosmetic outlines. Therefore we need to pick one which gives large enough
21 // triangles relative to glyph size that we can reuse the same triangles for any font size.
22 // 64 is used as a "base font size" by the distance field renderer and other places in Qt
23 // because this also has the benefit it's big enough that hinting will be disabled.
24 static int curveGlyphAtlasFontSize = qEnvironmentVariableIntValue("QSGCURVEGLYPHATLAS_FONT_SIZE");
25 m_font.setPixelSize(curveGlyphAtlasFontSize > 0 ? qreal(curveGlyphAtlasFontSize) : 64.0);
26}
27
28QSGCurveGlyphAtlas::~QSGCurveGlyphAtlas()
29{
30}
31
32void QSGCurveGlyphAtlas::populate(const QList<glyph_t> &glyphs)
33{
34 for (glyph_t glyphIndex : glyphs) {
35 if (!m_glyphs.contains(glyphIndex)) {
36 QPainterPath path = m_font.pathForGlyph(glyphIndex);
37 QQuadPath quadPath = QQuadPath::fromPainterPath(path);
38 quadPath.setFillRule(Qt::WindingFill);
39
40 Glyph glyph;
41
42 QSGCurveProcessor::processStroke(quadPath, 2, 2, false, Qt::MiterJoin, Qt::FlatCap,
43 [&glyph](const std::array<QVector2D, 3> &s,
44 const std::array<QVector2D, 3> &p,
45 const std::array<QVector2D, 3> &n,
46 const std::array<float, 3> & /* extrusions */,
47 QSGCurveStrokeNode::TriangleFlags f) {
48 glyph.strokeVertices.append(s.at(0));
49 glyph.strokeVertices.append(s.at(1));
50 glyph.strokeVertices.append(s.at(2));
51
52 glyph.strokeUvs.append(p.at(0));
53 glyph.strokeUvs.append(p.at(1));
54 glyph.strokeUvs.append(p.at(2));
55
56 glyph.strokeNormals.append(n.at(0));
57 glyph.strokeNormals.append(n.at(1));
58 glyph.strokeNormals.append(n.at(2));
59
60 glyph.strokeElementIsLine.append(f.testFlag(QSGCurveStrokeNode::TriangleFlag::Line));
61 });
62
63 quadPath = quadPath.subPathsClosed();
64 quadPath.addCurvatureData(); // ### Since the inside of glyphs is defined by order of
65 // vertices, this step could be simplified
66 QSGCurveProcessor::solveOverlaps(quadPath);
67
68 QSGCurveProcessor::processFill(quadPath,
69 Qt::WindingFill,
70 [&glyph](const std::array<QVector2D, 3> &v,
71 const std::array<QVector2D, 3> &n,
72 QSGCurveProcessor::uvForPointCallback uvForPoint)
73 {
74 glyph.vertices.append(v.at(0));
75 glyph.vertices.append(v.at(1));
76 glyph.vertices.append(v.at(2));
77
78 QVector3D uv1 = uvForPoint(v.at(0));
79 glyph.uvs.append(uv1);
80 glyph.uvs.append(uvForPoint(v.at(1)));
81 glyph.uvs.append(uvForPoint(v.at(2)));
82
83 glyph.normals.append(n.at(0));
84 glyph.normals.append(n.at(1));
85 glyph.normals.append(n.at(2));
86
87 glyph.duvdx.append(QVector2D(uvForPoint(v.at(0) + QVector2D(1, 0))) - QVector2D(uv1));
88 glyph.duvdy.append(QVector2D(uvForPoint(v.at(0) + QVector2D(0, 1))) - QVector2D(uv1));
89 });
90
91 m_glyphs.insert(glyphIndex, glyph);
92 }
93 }
94}
95
96void QSGCurveGlyphAtlas::addStroke(QSGCurveStrokeNode *node,
97 glyph_t glyphIndex,
98 const QPointF &position) const
99{
100 const Glyph &glyph = m_glyphs[glyphIndex];
101
102 const QVector2D v(position);
103 for (qsizetype i = glyph.strokeElementIsLine.size() - 1; i >= 0; --i) {
104 QVector2D v1 = glyph.strokeVertices.at(i * 3 + 0) + v;
105 QVector2D v2 = glyph.strokeVertices.at(i * 3 + 1) + v;
106 QVector2D v3 = glyph.strokeVertices.at(i * 3 + 2) + v;
107 if (glyph.strokeElementIsLine.at(i)) {
108 node->appendTriangle({ v1, v2, v3 },
109 std::array<QVector2D, 2>({ glyph.strokeUvs.at(i * 3 + 0) + v, glyph.strokeUvs.at(i * 3 + 2) + v }),
110 { glyph.strokeNormals.at(i * 3 + 0), glyph.strokeNormals.at(i * 3 + 1), glyph.strokeNormals.at(i * 3 + 2) });
111 } else {
112 node->appendTriangle({ v1, v2, v3 },
113 { glyph.strokeUvs.at(i * 3 + 0) + v, glyph.strokeUvs.at(i * 3 + 1) + v, glyph.strokeUvs.at(i * 3 + 2) + v },
114 { glyph.strokeNormals.at(i * 3 + 0), glyph.strokeNormals.at(i * 3 + 1), glyph.strokeNormals.at(i * 3 + 2) });
115
116 }
117 }
118}
119
120void QSGCurveGlyphAtlas::addGlyph(QSGCurveFillNode *node,
121 glyph_t glyphIndex,
122 const QPointF &position,
123 qreal pixelSize) const
124{
125 const Glyph &glyph = m_glyphs[glyphIndex];
126
127 const float scaleFactor = pixelSize / m_font.pixelSize();
128 const QVector2D v(position);
129 for (qsizetype i = 0; i < glyph.vertices.size() / 3; ++i) {
130 node->appendTriangle(scaleFactor * glyph.vertices.at(i * 3 + 0) + v,
131 scaleFactor * glyph.vertices.at(i * 3 + 1) + v,
132 scaleFactor * glyph.vertices.at(i * 3 + 2) + v,
133 glyph.uvs.at(i * 3 + 0),
134 glyph.uvs.at(i * 3 + 1),
135 glyph.uvs.at(i * 3 + 2),
136 glyph.normals.at(i * 3 + 0),
137 glyph.normals.at(i * 3 + 1),
138 glyph.normals.at(i * 3 + 2),
139 glyph.duvdx.at(i) / scaleFactor,
140 glyph.duvdy.at(i) / scaleFactor);
141 }
142}
143
144QT_END_NAMESPACE
Combined button and popup list for selecting options.