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
qsvgfont.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
5
6#include "qsvgfont_p.h"
7
8#include "qpainter.h"
9#include "qpen.h"
10#include "qdebug.h"
11
13
14QSvgGlyph::QSvgGlyph(const QString &unicode, const QPainterPath &path, qreal horizAdvX)
15 : m_unicode(unicode), m_path(path), m_horizAdvX(horizAdvX)
16{
17
18}
19
20
21QSvgFont::QSvgFont(qreal horizAdvX)
22 : m_horizAdvX(horizAdvX)
23{
24}
25
26QSvgFont::~QSvgFont()
27 = default;
28
29void QSvgFont::addGlyph(const QString &unicode, const QPainterPath &path, qreal horizAdvX)
30{
31 m_glyphs.emplaceBack(unicode, path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX);
32}
33
34bool QSvgFont::addMissingGlyph(const QPainterPath &path, qreal horizAdvX)
35{
36 if (m_missingGlyph) {
37 qWarning("The font already has a 'missing-glyph' element.");
38 return false;
39 }
40 m_missingGlyph.reset(new QSvgGlyph(QChar(), path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX));
41 return true;
42}
43
44
45void QSvgFont::draw(QPainter *p, const QPointF &point, const QList<const QSvgGlyph *> &glyphs,
46 qreal pixelSize, Qt::Alignment alignment) const
47{
48 draw_helper(p, point, glyphs, pixelSize, alignment, nullptr);
49}
50
51QRectF QSvgFont::boundingRect(QPainter *p, const QPointF &point,
52 const QList<const QSvgGlyph *> &glyphs,
53 qreal pixelSize, Qt::Alignment alignment) const
54{
55 QRectF bounds;
56 draw_helper(p, point, glyphs, pixelSize, alignment, &bounds);
57 return bounds;
58}
59
60// text must not be empty
61char32_t firstUcs4(QStringView text)
62{
63 if (text.size() > 1 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate())
64 return QChar::surrogateToUcs4(text.at(0), text.at(1));
65
66 return text.first().unicode();
67}
68
69// returns a pointer to the first QSvgGlyph to be used in the text
70// or nullptr if none can be found
71const QSvgGlyph *QSvgFont::findFirstGlyphFor(QStringView text) const
72{
73 const auto possibleIndices = m_possibleGlyphIndicesForChar.value(firstUcs4(text));
74 for (const qsizetype i : possibleIndices) {
75 const QSvgGlyph &currentGlyph = m_glyphs.at(i);
76 if (text.startsWith(currentGlyph.m_unicode)) {
77 return &currentGlyph;
78 }
79 }
80
81 for (; m_firstUnscannedGlyphIdx < m_glyphs.size(); ++m_firstUnscannedGlyphIdx) {
82 const QSvgGlyph &currentGlyph = m_glyphs.at(m_firstUnscannedGlyphIdx);
83 m_possibleGlyphIndicesForChar[firstUcs4(currentGlyph.m_unicode)].push_back(m_firstUnscannedGlyphIdx);
84 if (text.startsWith(currentGlyph.m_unicode)) {
85 ++m_firstUnscannedGlyphIdx;
86 return &currentGlyph;
87 }
88 }
89 return nullptr;
90}
91
92QList<const QSvgGlyph *> QSvgFont::toGlyphs(QStringView text) const
93{
94 QList<const QSvgGlyph *> glyphs;
95 glyphs.reserve(text.length());
96 while (text.length()) {
97 const QSvgGlyph *foundGlyph = findFirstGlyphFor(text);
98 if (foundGlyph) {
99 glyphs.append(foundGlyph);
100 text.slice(foundGlyph->m_unicode.length());
101 } else {
102 if (m_missingGlyph)
103 glyphs.append(m_missingGlyph.get());
104 if (text.size() > 1
105 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate()) {
106 text.slice(2);
107 } else {
108 text.slice(1);
109 }
110 }
111 }
112 return glyphs;
113}
114
115void QSvgFont::draw_helper(QPainter *p, const QPointF &point,
116 const QList<const QSvgGlyph *> &glyphs, qreal pixelSize,
117 Qt::Alignment alignment, QRectF *boundingRect) const
118{
119 const bool isPainting = (boundingRect == nullptr);
120
121 p->save();
122 p->translate(point);
123 p->scale(pixelSize / m_unitsPerEm, -pixelSize / m_unitsPerEm);
124
125 // Calculate the text width to be used for alignment
126 int textWidth = 0;
127 for (const auto *glyph : glyphs)
128 textWidth += static_cast<int>(glyph->m_horizAdvX);
129
130 QPoint alignmentOffset(0, 0);
131 if (alignment == Qt::AlignHCenter) {
132 alignmentOffset.setX(-textWidth / 2);
133 } else if (alignment == Qt::AlignRight) {
134 alignmentOffset.setX(-textWidth);
135 }
136
137 p->translate(alignmentOffset);
138
139 // since in SVG the embedded font ain't really a path
140 // the outline has got to stay untransformed...
141 qreal penWidth = p->pen().widthF();
142 penWidth /= (pixelSize/m_unitsPerEm);
143 QPen pen = p->pen();
144 pen.setWidthF(penWidth);
145 p->setPen(pen);
146
147 for (const auto *glyph : glyphs) {
148 if (isPainting)
149 p->drawPath(glyph->m_path);
150
151 if (boundingRect) {
152 QPainterPathStroker stroker;
153 stroker.setWidth(penWidth);
154 stroker.setJoinStyle(p->pen().joinStyle());
155 stroker.setMiterLimit(p->pen().miterLimit());
156 QPainterPath stroke = stroker.createStroke(glyph->m_path);
157 *boundingRect |= p->transform().map(stroke).boundingRect();
158 }
159
160 p->translate(glyph->m_horizAdvX, 0);
161 }
162
163 p->restore();
164}
165
166void QSvgFont::setUnitsPerEm(qreal upem)
167{
168 m_unitsPerEm = upem;
169}
170
171QT_END_NAMESPACE
Combined button and popup list for selecting options.
char32_t firstUcs4(QStringView text)
Definition qsvgfont.cpp:61