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