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)
29void QSvgFont::addGlyph(
const QString &unicode,
const QPainterPath &path, qreal horizAdvX)
31 m_glyphs.emplaceBack(unicode, path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX);
34bool QSvgFont::addMissingGlyph(
const QPainterPath &path, qreal horizAdvX)
37 qWarning(
"The font already has a 'missing-glyph' element.");
40 m_missingGlyph.reset(
new QSvgGlyph(QChar(), path, (horizAdvX == -1) ? m_horizAdvX : horizAdvX));
45void QSvgFont::draw(QPainter *p,
const QPointF &point,
const QList<
const QSvgGlyph *> &glyphs,
46 qreal pixelSize, Qt::Alignment alignment)
const
48 draw_helper(p, point, glyphs, pixelSize, alignment,
nullptr);
51QRectF QSvgFont::boundingRect(QPainter *p,
const QPointF &point,
52 const QList<
const QSvgGlyph *> &glyphs,
53 qreal pixelSize, Qt::Alignment alignment)
const
56 draw_helper(p, point, glyphs, pixelSize, alignment, &bounds);
63 if (text.size() > 1 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate())
64 return QChar::surrogateToUcs4(text.at(0), text.at(1));
66 return text.first().unicode();
71const QSvgGlyph *QSvgFont::findFirstGlyphFor(QStringView text)
const
73 const auto possibleIndices = m_possibleGlyphIndicesForChar.value(firstUcs4(text));
74 for (
const qsizetype i : possibleIndices) {
75 const QSvgGlyph ¤tGlyph = m_glyphs.at(i);
76 if (text.startsWith(currentGlyph.m_unicode)) {
81 for (; m_firstUnscannedGlyphIdx < m_glyphs.size(); ++m_firstUnscannedGlyphIdx) {
82 const QSvgGlyph ¤tGlyph = 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;
92QList<
const QSvgGlyph *> QSvgFont::toGlyphs(QStringView text)
const
94 QList<
const QSvgGlyph *> glyphs;
95 glyphs.reserve(text.length());
96 while (text.length()) {
97 const QSvgGlyph *foundGlyph = findFirstGlyphFor(text);
99 glyphs.append(foundGlyph);
100 text.slice(foundGlyph->m_unicode.length());
103 glyphs.append(m_missingGlyph.get());
105 && text.at(0).isHighSurrogate() && text.at(1).isLowSurrogate()) {
115void QSvgFont::draw_helper(QPainter *p,
const QPointF &point,
116 const QList<
const QSvgGlyph *> &glyphs, qreal pixelSize,
117 Qt::Alignment alignment, QRectF *boundingRect)
const
119 const bool isPainting = (boundingRect ==
nullptr);
123 p->scale(pixelSize / m_unitsPerEm, -pixelSize / m_unitsPerEm);
127 for (
const auto *glyph : glyphs)
128 textWidth +=
static_cast<
int>(glyph->m_horizAdvX);
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);
137 p->translate(alignmentOffset);
141 qreal penWidth = p->pen().widthF();
142 penWidth /= (pixelSize/m_unitsPerEm);
144 pen.setWidthF(penWidth);
147 for (
const auto *glyph : glyphs) {
149 p->drawPath(glyph->m_path);
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();
160 p->translate(glyph->m_horizAdvX, 0);
166void QSvgFont::setUnitsPerEm(qreal upem)
Combined button and popup list for selecting options.
char32_t firstUcs4(QStringView text)