Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qsvgfont_p.h"
5
6#include "qpainter.h"
7#include "qpen.h"
8#include "qdebug.h"
9#include "qpicture.h"
10
12
14 : m_unicode(unicode), m_path(path), m_horizAdvX(horizAdvX)
15{
16
17}
18
19
21 : m_horizAdvX(horizAdvX)
22{
23}
24
25
27{
28 return m_familyName;
29}
30
31
32void QSvgFont::addGlyph(QChar unicode, const QPainterPath &path, qreal horizAdvX )
33{
34 m_glyphs.insert(unicode, QSvgGlyph(unicode, path,
35 (horizAdvX==-1)?m_horizAdvX:horizAdvX));
36}
37
38
39void QSvgFont::draw(QPainter *p, const QPointF &point, const QString &str, qreal pixelSize, Qt::Alignment alignment) const
40{
41 p->save();
42 p->translate(point);
43 p->scale(pixelSize / m_unitsPerEm, -pixelSize / m_unitsPerEm);
44
45 // Calculate the text width to be used for alignment
46 int textWidth = 0;
48 for ( ; itr != str.constEnd(); ++itr) {
49 QChar unicode = *itr;
50 if (!m_glyphs.contains(*itr)) {
51 unicode = u'\0';
52 if (!m_glyphs.contains(unicode))
53 continue;
54 }
55 textWidth += static_cast<int>(m_glyphs[unicode].m_horizAdvX);
56 }
57
58 QPoint alignmentOffset(0, 0);
60 alignmentOffset.setX(-textWidth / 2);
61 } else if (alignment == Qt::AlignRight) {
62 alignmentOffset.setX(-textWidth);
63 }
64
65 p->translate(alignmentOffset);
66
67 // since in SVG the embedded font ain't really a path
68 // the outline has got to stay untransformed...
69 qreal penWidth = p->pen().widthF();
70 penWidth /= (pixelSize/m_unitsPerEm);
71 QPen pen = p->pen();
72 pen.setWidthF(penWidth);
73 p->setPen(pen);
74
75 itr = str.constBegin();
76 for ( ; itr != str.constEnd(); ++itr) {
77 QChar unicode = *itr;
78 if (!m_glyphs.contains(*itr)) {
79 unicode = u'\0';
80 if (!m_glyphs.contains(unicode))
81 continue;
82 }
83 p->drawPath(m_glyphs[unicode].m_path);
84 p->translate(m_glyphs[unicode].m_horizAdvX, 0);
85 }
86
87 p->restore();
88}
89
94
96{
97 m_unitsPerEm = upem;
98}
99
\inmodule QtCore
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
\inmodule QtGui
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtGui
Definition qpen.h:28
void setWidthF(qreal width)
Sets the pen width to the given width in pixels with floating point precision.
Definition qpen.cpp:618
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
const_iterator constEnd() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing just after the last character in...
Definition qstring.h:1363
const_iterator constBegin() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in the st...
Definition qstring.h:1355
QString familyName() const
Definition qsvgfont.cpp:26
qreal m_horizAdvX
Definition qsvgfont_p.h:55
QSvgFont(qreal horizAdvX)
Definition qsvgfont.cpp:20
QString m_familyName
Definition qsvgfont_p.h:53
qreal m_unitsPerEm
Definition qsvgfont_p.h:54
QHash< QChar, QSvgGlyph > m_glyphs
Definition qsvgfont_p.h:56
void setUnitsPerEm(qreal upem)
Definition qsvgfont.cpp:95
void draw(QPainter *p, const QPointF &point, const QString &str, qreal pixelSize, Qt::Alignment alignment) const
Definition qsvgfont.cpp:39
void addGlyph(QChar unicode, const QPainterPath &path, qreal horizAdvX=-1)
Definition qsvgfont.cpp:32
void setFamilyName(const QString &name)
Definition qsvgfont.cpp:90
QString str
[2]
uint alignment
Combined button and popup list for selecting options.
@ AlignRight
Definition qnamespace.h:146
@ AlignHCenter
Definition qnamespace.h:148
GLuint name
GLsizei const GLchar *const * path
GLfloat GLfloat p
[1]
double qreal
Definition qtypes.h:187