| 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 | |
| 4 | #include "qsvgfont_p.h" |
| 5 | |
| 6 | #include "qpainter.h" |
| 7 | #include "qpen.h" |
| 8 | #include "qdebug.h" |
| 9 | #include "qpicture.h" |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | QSvgGlyph::QSvgGlyph(QChar unicode, const QPainterPath &path, qreal horizAdvX) |
| 14 | : m_unicode(unicode), m_path(path), m_horizAdvX(horizAdvX) |
| 15 | { |
| 16 | |
| 17 | } |
| 18 | |
| 19 | |
| 20 | QSvgFont::QSvgFont(qreal horizAdvX) |
| 21 | : m_horizAdvX(horizAdvX) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | |
| 26 | QString QSvgFont::familyName() const |
| 27 | { |
| 28 | return m_familyName; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | void QSvgFont::addGlyph(QChar unicode, const QPainterPath &path, qreal horizAdvX ) |
| 33 | { |
| 34 | m_glyphs.insert(key: unicode, value: QSvgGlyph(unicode, path, |
| 35 | (horizAdvX==-1)?m_horizAdvX:horizAdvX)); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void QSvgFont::draw(QPainter *p, const QPointF &point, const QString &str, |
| 40 | qreal pixelSize, Qt::Alignment alignment) const |
| 41 | { |
| 42 | draw_helper(p, point, str, pixelSize, alignment, boundingRect: nullptr); |
| 43 | } |
| 44 | |
| 45 | QRectF QSvgFont::boundingRect(QPainter *p, const QPointF &point, const QString &str, |
| 46 | qreal pixelSize, Qt::Alignment alignment) const |
| 47 | { |
| 48 | QRectF bounds; |
| 49 | draw_helper(p, point, str, pixelSize, alignment, boundingRect: &bounds); |
| 50 | return bounds; |
| 51 | } |
| 52 | |
| 53 | void QSvgFont::draw_helper(QPainter *p, const QPointF &point, const QString &str, qreal pixelSize, |
| 54 | Qt::Alignment alignment, QRectF *boundingRect) const |
| 55 | { |
| 56 | const bool isPainting = (boundingRect == nullptr); |
| 57 | |
| 58 | p->save(); |
| 59 | p->translate(offset: point); |
| 60 | p->scale(sx: pixelSize / m_unitsPerEm, sy: -pixelSize / m_unitsPerEm); |
| 61 | |
| 62 | // Calculate the text width to be used for alignment |
| 63 | int textWidth = 0; |
| 64 | QString::const_iterator itr = str.constBegin(); |
| 65 | for ( ; itr != str.constEnd(); ++itr) { |
| 66 | QChar unicode = *itr; |
| 67 | if (!m_glyphs.contains(key: *itr)) { |
| 68 | unicode = u'\0'; |
| 69 | if (!m_glyphs.contains(key: unicode)) |
| 70 | continue; |
| 71 | } |
| 72 | textWidth += static_cast<int>(m_glyphs[unicode].m_horizAdvX); |
| 73 | } |
| 74 | |
| 75 | QPoint alignmentOffset(0, 0); |
| 76 | if (alignment == Qt::AlignHCenter) { |
| 77 | alignmentOffset.setX(-textWidth / 2); |
| 78 | } else if (alignment == Qt::AlignRight) { |
| 79 | alignmentOffset.setX(-textWidth); |
| 80 | } |
| 81 | |
| 82 | p->translate(offset: alignmentOffset); |
| 83 | |
| 84 | // since in SVG the embedded font ain't really a path |
| 85 | // the outline has got to stay untransformed... |
| 86 | qreal penWidth = p->pen().widthF(); |
| 87 | penWidth /= (pixelSize/m_unitsPerEm); |
| 88 | QPen pen = p->pen(); |
| 89 | pen.setWidthF(penWidth); |
| 90 | p->setPen(pen); |
| 91 | |
| 92 | itr = str.constBegin(); |
| 93 | for ( ; itr != str.constEnd(); ++itr) { |
| 94 | QChar unicode = *itr; |
| 95 | if (!m_glyphs.contains(key: *itr)) { |
| 96 | unicode = u'\0'; |
| 97 | if (!m_glyphs.contains(key: unicode)) |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if (isPainting) |
| 102 | p->drawPath(path: m_glyphs[unicode].m_path); |
| 103 | |
| 104 | if (boundingRect) { |
| 105 | QPainterPathStroker stroker; |
| 106 | stroker.setWidth(penWidth); |
| 107 | stroker.setJoinStyle(p->pen().joinStyle()); |
| 108 | stroker.setMiterLimit(p->pen().miterLimit()); |
| 109 | QPainterPath stroke = stroker.createStroke(path: m_glyphs[unicode].m_path); |
| 110 | *boundingRect |= p->transform().map(p: stroke).boundingRect(); |
| 111 | } |
| 112 | |
| 113 | p->translate(dx: m_glyphs[unicode].m_horizAdvX, dy: 0); |
| 114 | } |
| 115 | |
| 116 | p->restore(); |
| 117 | } |
| 118 | |
| 119 | void QSvgFont::setFamilyName(const QString &name) |
| 120 | { |
| 121 | m_familyName = name; |
| 122 | } |
| 123 | |
| 124 | void QSvgFont::setUnitsPerEm(qreal upem) |
| 125 | { |
| 126 | m_unitsPerEm = upem; |
| 127 | } |
| 128 | |
| 129 | QT_END_NAMESPACE |
| 130 | |