Qt 4.8
qsvgfont.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSvg module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsvgfont_p.h"
43 
44 #ifndef QT_NO_SVG
45 
46 #include "qpainter.h"
47 #include "qpen.h"
48 #include "qdebug.h"
49 #include "qpicture.h"
50 
52 
53 QSvgGlyph::QSvgGlyph(QChar unicode, const QPainterPath &path, qreal horizAdvX)
54  : m_unicode(unicode), m_path(path), m_horizAdvX(horizAdvX)
55 {
56 
57 }
58 
59 
61  : m_horizAdvX(horizAdvX)
62 {
63 }
64 
65 
67 {
68  return m_familyName;
69 }
70 
71 
72 void QSvgFont::addGlyph(QChar unicode, const QPainterPath &path, qreal horizAdvX )
73 {
74  m_glyphs.insert(unicode, QSvgGlyph(unicode, path,
75  (horizAdvX==-1)?m_horizAdvX:horizAdvX));
76 }
77 
78 
79 void QSvgFont::draw(QPainter *p, const QPointF &point, const QString &str, qreal pixelSize, Qt::Alignment alignment) const
80 {
81  p->save();
82  p->translate(point);
83  p->scale(pixelSize / m_unitsPerEm, -pixelSize / m_unitsPerEm);
84 
85  // Calculate the text width to be used for alignment
86  int textWidth = 0;
88  for ( ; itr != str.constEnd(); ++itr) {
89  QChar unicode = *itr;
90  if (!m_glyphs.contains(*itr)) {
91  unicode = 0;
92  if (!m_glyphs.contains(unicode))
93  continue;
94  }
95  textWidth += static_cast<int>(m_glyphs[unicode].m_horizAdvX);
96  }
97 
98  QPoint alignmentOffset(0, 0);
99  if (alignment == Qt::AlignHCenter) {
100  alignmentOffset.setX(-textWidth / 2);
101  } else if (alignment == Qt::AlignRight) {
102  alignmentOffset.setX(-textWidth);
103  }
104 
105  p->translate(alignmentOffset);
106 
107  // since in SVG the embedded font ain't really a path
108  // the outline has got to stay untransformed...
109  qreal penWidth = p->pen().widthF();
110  penWidth /= (pixelSize/m_unitsPerEm);
111  QPen pen = p->pen();
112  pen.setWidthF(penWidth);
113  p->setPen(pen);
114 
115  itr = str.constBegin();
116  for ( ; itr != str.constEnd(); ++itr) {
117  QChar unicode = *itr;
118  if (!m_glyphs.contains(*itr)) {
119  unicode = 0;
120  if (!m_glyphs.contains(unicode))
121  continue;
122  }
123  p->drawPath(m_glyphs[unicode].m_path);
124  p->translate(m_glyphs[unicode].m_horizAdvX, 0);
125  }
126 
127  p->restore();
128 }
129 
131 {
132  m_familyName = name;
133 }
134 
136 {
137  m_unitsPerEm = upem;
138 }
139 
141 
142 #endif // QT_NO_SVG
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
QSvgFont(qreal horizAdvX)
Definition: qsvgfont.cpp:60
void drawPath(const QPainterPath &path)
Draws the given painter path using the current pen for outline and the current brush for filling...
Definition: qpainter.cpp:3502
double qreal
Definition: qglobal.h:1193
QString m_familyName
Definition: qsvgfont_p.h:92
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first character in the string. ...
Definition: qstring.h:892
const QPen & pen() const
Returns the painter&#39;s current pen.
Definition: qpainter.cpp:4152
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static float pixelSize(const QFontDef &request, int dpi)
Definition: qfont_win.cpp:80
const char * name
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qstring.h:898
qreal m_unitsPerEm
Definition: qsvgfont_p.h:93
void setFamilyName(const QString &name)
Definition: qsvgfont.cpp:130
qreal m_horizAdvX
Definition: qsvgfont_p.h:74
qreal m_horizAdvX
Definition: qsvgfont_p.h:96
void setUnitsPerEm(qreal upem)
Definition: qsvgfont.cpp:135
void setWidthF(qreal width)
Sets the pen width to the given width in pixels with floating point precision.
Definition: qpen.cpp:690
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void draw(QPainter *p, const QPointF &point, const QString &str, qreal pixelSize, Qt::Alignment alignment) const
Definition: qsvgfont.cpp:79
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
void addGlyph(QChar unicode, const QPainterPath &path, qreal horizAdvX=-1)
Definition: qsvgfont.cpp:72
qreal widthF() const
Returns the pen width with floating point precision.
Definition: qpen.cpp:645
QHash< QChar, QSvgGlyph > m_glyphs
Definition: qsvgfont_p.h:97
void scale(qreal sx, qreal sy)
Scales the coordinate system by ({sx}, {sy}).
Definition: qpainter.cpp:3234
QString familyName() const
Definition: qsvgfont.cpp:66
void setX(int x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qpoint.h:134
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
Definition: qpainter.cpp:3311