| 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 | |
| 4 | #include "qsgcurveglyphatlas_p.h" |
| 5 | #include "qsgcurvefillnode_p.h" |
| 6 | #include "qsgcurvestrokenode_p.h" |
| 7 | #include "qsgcurveprocessor_p.h" |
| 8 | #include "util/qquadpath_p.h" |
| 9 | |
| 10 | #include <QtGui/qrawfont.h> |
| 11 | #include <QtGui/qpainterpath.h> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | QSGCurveGlyphAtlas::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(varName: "QSGCURVEGLYPHATLAS_FONT_SIZE" ); |
| 24 | m_font.setPixelSize(curveGlyphAtlasFontSize > 0 ? qreal(curveGlyphAtlasFontSize) : 64.0); |
| 25 | } |
| 26 | |
| 27 | QSGCurveGlyphAtlas::~QSGCurveGlyphAtlas() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | void QSGCurveGlyphAtlas::populate(const QList<glyph_t> &glyphs) |
| 32 | { |
| 33 | for (glyph_t glyphIndex : glyphs) { |
| 34 | if (!m_glyphs.contains(key: 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(strokePath: quadPath, miterLimit: 2, penWidth: 2, joinStyle: Qt::MiterJoin, capStyle: Qt::FlatCap, |
| 42 | addTriangle: [&glyph](const std::array<QVector2D, 3> &s, |
| 43 | const std::array<QVector2D, 3> &p, |
| 44 | const std::array<QVector2D, 3> &n, |
| 45 | bool isLine) { |
| 46 | glyph.strokeVertices.append(t: s.at(n: 0)); |
| 47 | glyph.strokeVertices.append(t: s.at(n: 1)); |
| 48 | glyph.strokeVertices.append(t: s.at(n: 2)); |
| 49 | |
| 50 | glyph.strokeUvs.append(t: p.at(n: 0)); |
| 51 | glyph.strokeUvs.append(t: p.at(n: 1)); |
| 52 | glyph.strokeUvs.append(t: p.at(n: 2)); |
| 53 | |
| 54 | glyph.strokeNormals.append(t: n.at(n: 0)); |
| 55 | glyph.strokeNormals.append(t: n.at(n: 1)); |
| 56 | glyph.strokeNormals.append(t: n.at(n: 2)); |
| 57 | |
| 58 | glyph.strokeElementIsLine.append(t: isLine); |
| 59 | }); |
| 60 | |
| 61 | quadPath = quadPath.subPathsClosed(); |
| 62 | quadPath.addCurvatureData(); // ### Since the inside of glyphs is defined by order of |
| 63 | // vertices, this step could be simplified |
| 64 | QSGCurveProcessor::solveOverlaps(path&: quadPath); |
| 65 | |
| 66 | QSGCurveProcessor::processFill(path: quadPath, |
| 67 | fillRule: Qt::WindingFill, |
| 68 | addTriangle: [&glyph](const std::array<QVector2D, 3> &v, |
| 69 | const std::array<QVector2D, 3> &n, |
| 70 | QSGCurveProcessor::uvForPointCallback uvForPoint) |
| 71 | { |
| 72 | glyph.vertices.append(t: v.at(n: 0)); |
| 73 | glyph.vertices.append(t: v.at(n: 1)); |
| 74 | glyph.vertices.append(t: v.at(n: 2)); |
| 75 | |
| 76 | QVector3D uv1 = uvForPoint(v.at(n: 0)); |
| 77 | glyph.uvs.append(t: uv1); |
| 78 | glyph.uvs.append(t: uvForPoint(v.at(n: 1))); |
| 79 | glyph.uvs.append(t: uvForPoint(v.at(n: 2))); |
| 80 | |
| 81 | glyph.normals.append(t: n.at(n: 0)); |
| 82 | glyph.normals.append(t: n.at(n: 1)); |
| 83 | glyph.normals.append(t: n.at(n: 2)); |
| 84 | |
| 85 | glyph.duvdx.append(t: QVector2D(uvForPoint(v.at(n: 0) + QVector2D(1, 0))) - QVector2D(uv1)); |
| 86 | glyph.duvdy.append(t: QVector2D(uvForPoint(v.at(n: 0) + QVector2D(0, 1))) - QVector2D(uv1)); |
| 87 | }); |
| 88 | |
| 89 | m_glyphs.insert(key: glyphIndex, value: glyph); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void QSGCurveGlyphAtlas::addStroke(QSGCurveStrokeNode *node, |
| 95 | glyph_t glyphIndex, |
| 96 | const QPointF &position) const |
| 97 | { |
| 98 | const Glyph &glyph = m_glyphs[glyphIndex]; |
| 99 | |
| 100 | const QVector2D v(position); |
| 101 | for (qsizetype i = glyph.strokeElementIsLine.size() - 1; i >= 0; --i) { |
| 102 | QVector2D v1 = glyph.strokeVertices.at(i: i * 3 + 0) + v; |
| 103 | QVector2D v2 = glyph.strokeVertices.at(i: i * 3 + 1) + v; |
| 104 | QVector2D v3 = glyph.strokeVertices.at(i: i * 3 + 2) + v; |
| 105 | if (glyph.strokeElementIsLine.at(i)) { |
| 106 | node->appendTriangle(v: { v1, v2, v3 }, |
| 107 | p: std::array<QVector2D, 2>({ glyph.strokeUvs.at(i: i * 3 + 0) + v, glyph.strokeUvs.at(i: i * 3 + 2) + v }), |
| 108 | n: { glyph.strokeNormals.at(i: i * 3 + 0), glyph.strokeNormals.at(i: i * 3 + 1), glyph.strokeNormals.at(i: i * 3 + 2) }); |
| 109 | } else { |
| 110 | node->appendTriangle(v: { v1, v2, v3 }, |
| 111 | p: { glyph.strokeUvs.at(i: i * 3 + 0) + v, glyph.strokeUvs.at(i: i * 3 + 1) + v, glyph.strokeUvs.at(i: i * 3 + 2) + v }, |
| 112 | n: { glyph.strokeNormals.at(i: i * 3 + 0), glyph.strokeNormals.at(i: i * 3 + 1), glyph.strokeNormals.at(i: i * 3 + 2) }); |
| 113 | |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void QSGCurveGlyphAtlas::addGlyph(QSGCurveFillNode *node, |
| 119 | glyph_t glyphIndex, |
| 120 | const QPointF &position, |
| 121 | qreal pixelSize) const |
| 122 | { |
| 123 | const Glyph &glyph = m_glyphs[glyphIndex]; |
| 124 | |
| 125 | const float scaleFactor = pixelSize / m_font.pixelSize(); |
| 126 | const QVector2D v(position); |
| 127 | for (qsizetype i = 0; i < glyph.vertices.size() / 3; ++i) { |
| 128 | node->appendTriangle(v1: scaleFactor * glyph.vertices.at(i: i * 3 + 0) + v, |
| 129 | v2: scaleFactor * glyph.vertices.at(i: i * 3 + 1) + v, |
| 130 | v3: scaleFactor * glyph.vertices.at(i: i * 3 + 2) + v, |
| 131 | uv1: glyph.uvs.at(i: i * 3 + 0), |
| 132 | uv2: glyph.uvs.at(i: i * 3 + 1), |
| 133 | uv3: glyph.uvs.at(i: i * 3 + 2), |
| 134 | n1: glyph.normals.at(i: i * 3 + 0), |
| 135 | n2: glyph.normals.at(i: i * 3 + 1), |
| 136 | n3: glyph.normals.at(i: i * 3 + 2), |
| 137 | duvdx: glyph.duvdx.at(i) / scaleFactor, |
| 138 | duvdy: glyph.duvdy.at(i) / scaleFactor); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | QT_END_NAMESPACE |
| 143 | |