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)
29QString QSvgFont::familyName()
const
35void QSvgFont::addGlyph(
const QString &unicode,
const QPainterPath &path, qreal horizAdvX)
37 m_glyphs.emplaceBack(unicode, path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX);
40bool QSvgFont::addMissingGlyph(
const QPainterPath &path, qreal horizAdvX)
43 qWarning(
"The font already has a 'missing-glyph' element.");
46 m_missingGlyph.reset(
new QSvgGlyph(QChar(), path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX));
51void QSvgFont::draw(QPainter *p,
const QPointF &point,
const QList<
const QSvgGlyph *> &glyphs,
52 qreal pixelSize, Qt::Alignment alignment)
const
54 draw_helper(p, point, glyphs, pixelSize, alignment,
nullptr);
57QRectF QSvgFont::boundingRect(QPainter *p,
const QPointF &point,
58 const QList<
const QSvgGlyph *> &glyphs,
59 qreal pixelSize, Qt::Alignment alignment)
const
62 draw_helper(p, point, glyphs, pixelSize, alignment, &bounds);
69 if (text.size() > 1 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate())
70 return QChar::surrogateToUcs4(text.at(0), text.at(1));
72 return text.first().unicode();
77const QSvgGlyph *QSvgFont::findFirstGlyphFor(QStringView text)
const
79 const auto possibleIndices = m_possibleGlyphIndicesForChar.value(firstUcs4(text));
80 for (
const qsizetype i : possibleIndices) {
81 const QSvgGlyph ¤tGlyph = m_glyphs.at(i);
82 if (text.startsWith(currentGlyph.m_unicode)) {
87 for (; m_firstUnscannedGlyphIdx < m_glyphs.size(); ++m_firstUnscannedGlyphIdx) {
88 const QSvgGlyph ¤tGlyph = 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;
98QList<
const QSvgGlyph *> QSvgFont::toGlyphs(QStringView text)
const
100 QList<
const QSvgGlyph *> glyphs;
101 glyphs.reserve(text.length());
102 while (text.length()) {
103 const QSvgGlyph *foundGlyph = findFirstGlyphFor(text);
105 glyphs.append(foundGlyph);
106 text.slice(foundGlyph->m_unicode.length());
109 glyphs.append(m_missingGlyph.get());
111 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate()) {
121void QSvgFont::draw_helper(QPainter *p,
const QPointF &point,
122 const QList<
const QSvgGlyph *> &glyphs, qreal pixelSize,
123 Qt::Alignment alignment, QRectF *boundingRect)
const
125 const bool isPainting = (boundingRect ==
nullptr);
129 p->scale(pixelSize / m_unitsPerEm, -pixelSize / m_unitsPerEm);
133 for (
const auto *glyph : glyphs)
134 textWidth +=
static_cast<
int>(glyph->m_horizAdvX);
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);
143 p->translate(alignmentOffset);
147 qreal penWidth = p->pen().widthF();
148 penWidth /= (pixelSize/m_unitsPerEm);
150 pen.setWidthF(penWidth);
153 for (
const auto *glyph : glyphs) {
155 p->drawPath(glyph->m_path);
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();
166 p->translate(glyph->m_horizAdvX, 0);
172void QSvgFont::setFamilyName(
const QString &name)
177void QSvgFont::setUnitsPerEm(qreal upem)
Combined button and popup list for selecting options.
char32_t firstUcs4(QStringView text)