14QSvgGlyph::QSvgGlyph(
const QString &unicode,
const QPainterPath &path, qreal horizAdvX)
15 : m_unicode(unicode), m_path(path), m_horizAdvX(horizAdvX)
21QSvgFont::QSvgFont(qreal horizAdvX)
22 : m_horizAdvX(horizAdvX)
27QString QSvgFont::familyName()
const
33void QSvgFont::addGlyph(
const QString &unicode,
const QPainterPath &path, qreal horizAdvX)
35 m_glyphs.emplaceBack(unicode, path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX);
38bool QSvgFont::addMissingGlyph(
const QPainterPath &path, qreal horizAdvX)
41 qWarning(
"The font already has a 'missing-glyph' element.");
44 m_missingGlyph.reset(
new QSvgGlyph(QChar(), path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX));
49void QSvgFont::draw(QPainter *p,
const QPointF &point,
const QList<
const QSvgGlyph *> &glyphs,
50 qreal pixelSize, Qt::Alignment alignment)
const
52 draw_helper(p, point, glyphs, pixelSize, alignment,
nullptr);
55QRectF QSvgFont::boundingRect(QPainter *p,
const QPointF &point,
56 const QList<
const QSvgGlyph *> &glyphs,
57 qreal pixelSize, Qt::Alignment alignment)
const
60 draw_helper(p, point, glyphs, pixelSize, alignment, &bounds);
67 if (text.size() > 1 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate())
68 return QChar::surrogateToUcs4(text.at(0), text.at(1));
70 return text.first().unicode();
75const QSvgGlyph *QSvgFont::findFirstGlyphFor(QStringView text)
const
77 const auto possibleIndices = m_possibleGlyphIndicesForChar.value(firstUcs4(text));
78 for (
const qsizetype i : possibleIndices) {
79 const QSvgGlyph ¤tGlyph = m_glyphs.at(i);
80 if (text.startsWith(currentGlyph.m_unicode)) {
85 for (; m_firstUnscannedGlyphIdx < m_glyphs.size(); ++m_firstUnscannedGlyphIdx) {
86 const QSvgGlyph ¤tGlyph = 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;
96QList<
const QSvgGlyph *> QSvgFont::toGlyphs(QStringView text)
const
98 QList<
const QSvgGlyph *> glyphs;
99 glyphs.reserve(text.length());
100 while (text.length()) {
101 const QSvgGlyph *foundGlyph = findFirstGlyphFor(text);
103 glyphs.append(foundGlyph);
104 text.slice(foundGlyph->m_unicode.length());
107 glyphs.append(m_missingGlyph.get());
109 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate()) {
119void QSvgFont::draw_helper(QPainter *p,
const QPointF &point,
120 const QList<
const QSvgGlyph *> &glyphs, qreal pixelSize,
121 Qt::Alignment alignment, QRectF *boundingRect)
const
123 const bool isPainting = (boundingRect ==
nullptr);
127 p->scale(pixelSize / m_unitsPerEm, -pixelSize / m_unitsPerEm);
131 for (
const auto *glyph : glyphs)
132 textWidth +=
static_cast<
int>(glyph->m_horizAdvX);
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);
141 p->translate(alignmentOffset);
145 qreal penWidth = p->pen().widthF();
146 penWidth /= (pixelSize/m_unitsPerEm);
148 pen.setWidthF(penWidth);
151 for (
const auto *glyph : glyphs) {
153 p->drawPath(glyph->m_path);
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();
164 p->translate(glyph->m_horizAdvX, 0);
170void QSvgFont::setFamilyName(
const QString &name)
175void QSvgFont::setUnitsPerEm(qreal upem)
Combined button and popup list for selecting options.
char32_t firstUcs4(QStringView text)