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