Qt 4.8
qfont_win.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 QtGui 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 "qfont.h"
43 #include "qfont_p.h"
44 #include "qfontengine_p.h"
45 #include "qtextengine_p.h"
46 #include "qfontmetrics.h"
47 #include "qfontinfo.h"
48 
49 #include "qwidget.h"
50 #include "qpainter.h"
51 #include <limits.h>
52 #include "qt_windows.h"
53 #include <private/qapplication_p.h>
54 #include "qapplication.h"
55 #include <private/qunicodetables_p.h>
56 #include <qfontdatabase.h>
57 
59 
60 extern HDC shared_dc(); // common dc for all fonts
61 extern QFont::Weight weightFromInteger(int weight); // qfontdatabase.cpp
62 
63 // ### maybe move to qapplication_win
64 QFont qt_LOGFONTtoQFont(LOGFONT& lf, bool /*scale*/)
65 {
66  QString family = QString::fromWCharArray(lf.lfFaceName);
67  QFont qf(family);
68  qf.setItalic(lf.lfItalic);
69  if (lf.lfWeight != FW_DONTCARE)
70  qf.setWeight(weightFromInteger(lf.lfWeight));
71  int lfh = qAbs(lf.lfHeight);
72  qf.setPointSizeF(lfh * 72.0 / GetDeviceCaps(shared_dc(),LOGPIXELSY));
73  qf.setUnderline(false);
74  qf.setOverline(false);
75  qf.setStrikeOut(false);
76  return qf;
77 }
78 
79 
80 static inline float pixelSize(const QFontDef &request, int dpi)
81 {
82  float pSize;
83  if (request.pointSize != -1)
84  pSize = request.pointSize * dpi/ 72.;
85  else
86  pSize = request.pixelSize;
87  return pSize;
88 }
89 
90 static inline float pointSize(const QFontDef &fd, int dpi)
91 {
92  float pSize;
93  if (fd.pointSize < 0)
94  pSize = fd.pixelSize * 72. / ((float)dpi);
95  else
96  pSize = fd.pointSize;
97  return pSize;
98 }
99 
100 /*****************************************************************************
101  QFont member functions
102  *****************************************************************************/
103 
104 void QFont::initialize()
105 {
106 }
107 
108 void QFont::cleanup()
109 {
111 }
112 
113 HFONT QFont::handle() const
114 {
116  Q_ASSERT(engine != 0);
117  if (engine->type() == QFontEngine::Multi)
118  engine = static_cast<QFontEngineMulti *>(engine)->engine(0);
119  if (engine->type() == QFontEngine::Win)
120  return static_cast<QFontEngineWin *>(engine)->hfont;
121  return 0;
122 }
123 
124 QString QFont::rawName() const
125 {
126  return family();
127 }
128 
129 void QFont::setRawName(const QString &name)
130 {
131  setFamily(name);
132 }
133 
135 {
136  switch(d->request.styleHint) {
137  case QFont::Times:
138  return QString::fromLatin1("Times New Roman");
139  case QFont::Courier:
140  case QFont::Monospace:
141  return QString::fromLatin1("Courier New");
142  case QFont::Decorative:
143  return QString::fromLatin1("Bookman Old Style");
144  case QFont::Cursive:
145  return QString::fromLatin1("Comic Sans MS");
146  case QFont::Fantasy:
147  return QString::fromLatin1("Impact");
148  case QFont::Helvetica:
149  return QString::fromLatin1("Arial");
150  case QFont::System:
151  default:
152  return QString::fromLatin1("MS Sans Serif");
153  }
154 }
155 
157 {
158  return QString::fromLatin1("helvetica");
159 }
160 
162 {
163  return QString::fromLatin1("arial");
164 }
165 
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
qreal pointSize
Definition: qfont_p.h:89
void setUnderline(bool)
If enable is true, sets underline on; otherwise sets underline off.
Definition: qfont.cpp:1331
QString lastResortFamily() const
Returns the "last resort" font family name.
Definition: qfont_mac.cpp:156
Weight
Qt uses a weighting scale from 0 to 99 similar to, but not the same as, the scales used in Windows or...
Definition: qfont.h:103
QFont::Weight weightFromInteger(int weight)
static void cleanup()
Internal function that cleans up the font system.
Definition: qfont_mac.cpp:93
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setItalic(bool b)
Sets the style() of the font to QFont::StyleItalic if enable is true; otherwise the style is set to Q...
Definition: qfont.h:360
HFONT handle() const
Returns the window system handle to the font, for low-level access.
Definition: qfont_mac.cpp:121
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual Type type() const =0
void setFamily(const QString &)
Sets the family name of the font.
Definition: qfont.cpp:924
QString rawName() const
Returns the name of the font within the underlying window system.
Definition: qfont_mac.cpp:83
#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
static void initialize()
Internal function that initializes the font system.
Definition: qfont_mac.cpp:131
void setStrikeOut(bool)
If enable is true, sets strikeout on; otherwise sets strikeout off.
Definition: qfont.cpp:1378
QFontDef request
Definition: qfont_p.h:178
const char * name
qreal pixelSize
Definition: qfont_p.h:90
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
QString lastResortFont() const
Returns a "last resort" font name for the font matching algorithm.
Definition: qfont_mac.cpp:161
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QString family() const
Returns the requested font family name, i.e.
Definition: qfont.cpp:906
void setOverline(bool)
If enable is true, sets overline on; otherwise sets overline off.
Definition: qfont.cpp:1354
static void cleanup()
Definition: qfont.cpp:2927
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfont.h:343
void setWeight(int)
Sets the weight the font to weight, which should be a value from the QFont::Weight enumeration...
Definition: qfont.cpp:1278
HDC shared_dc()
QString defaultFamily() const
Returns the family name that corresponds to the current style hint.
Definition: qfont_mac.cpp:134
void setPointSizeF(qreal)
Sets the point size to pointSize.
Definition: qfont.cpp:1121
void setRawName(const QString &)
Sets a font by its system specific name.
Definition: qfont_mac.cpp:88
uint styleHint
Definition: qfont_p.h:93
QFont qt_LOGFONTtoQFont(LOGFONT &lf, bool)
Definition: qfont_win.cpp:64
static float pointSize(const QFontDef &fd, int dpi)
Definition: qfont_win.cpp:90