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