Qt 4.8
qfont_x11.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 #define QT_FATAL_ASSERT
43 
44 #include "qplatformdefs.h"
45 
46 #include "qfont.h"
47 #include "qapplication.h"
48 #include "qfontinfo.h"
49 #include "qfontdatabase.h"
50 #include "qfontmetrics.h"
51 #include "qpaintdevice.h"
52 #include "qtextcodec.h"
53 #include "qiodevice.h"
54 #include "qhash.h"
55 
56 #include <private/qunicodetables_p.h>
57 #include "qfont_p.h"
58 #include "qfontengine_p.h"
59 #include "qfontengine_x11_p.h"
60 #include "qtextengine_p.h"
61 
62 #include <private/qt_x11_p.h>
63 #include "qx11info_x11.h"
64 
65 #include <time.h>
66 #include <stdlib.h>
67 #include <ctype.h>
68 
69 #define QFONTLOADER_DEBUG
70 #define QFONTLOADER_DEBUG_VERBOSE
71 
73 
74 double qt_pixelSize(double pointSize, int dpi)
75 {
76  if (pointSize < 0)
77  return -1.;
78  if (dpi == 75) // the stupid 75 dpi setting on X11
79  dpi = 72;
80  return (pointSize * dpi) /72.;
81 }
82 
83 double qt_pointSize(double pixelSize, int dpi)
84 {
85  if (pixelSize < 0)
86  return -1.;
87  if (dpi == 75) // the stupid 75 dpi setting on X11
88  dpi = 72;
89  return pixelSize * 72. / ((double) dpi);
90 }
91 
92 /*
93  Removes wildcards from an XLFD.
94 
95  Returns \a xlfd with all wildcards removed if a match for \a xlfd is
96  found, otherwise it returns \a xlfd.
97 */
98 static QByteArray qt_fixXLFD(const QByteArray &xlfd)
99 {
100  QByteArray ret = xlfd;
101  int count = 0;
102  char **fontNames =
103  XListFonts(QX11Info::display(), xlfd, 32768, &count);
104  if (count > 0)
105  ret = fontNames[0];
106  XFreeFontNames(fontNames);
107  return ret ;
108 }
109 
111 Q_GLOBAL_STATIC(FallBackHash, fallBackHash)
112 
113 // Returns the user-configured fallback family for the specified script.
115 {
116  FallBackHash *hash = fallBackHash();
117  return hash->value(script);
118 }
119 
120 // Sets the fallback family for the specified script.
122 {
123  FallBackHash *hash = fallBackHash();
124  if (!family.isEmpty())
125  hash->insert(script, family);
126  else
127  hash->remove(script);
128 }
129 
131 
132 void QFont::initialize()
133 {
134  extern int qt_encoding_id_for_mib(int mib); // from qfontdatabase_x11.cpp
136  // determine the default encoding id using the locale, otherwise
137  // fallback to latin1 (mib == 4)
138  int mib = codec ? codec->mibEnum() : 4;
139 
140  // for asian locales, use the mib for the font codec instead of the locale codec
141  switch (mib) {
142  case 38: // eucKR
143  mib = 36;
144  break;
145 
146  case 2025: // GB2312
147  mib = 57;
148  break;
149 
150  case 113: // GBK
151  mib = -113;
152  break;
153 
154  case 114: // GB18030
155  mib = -114;
156  break;
157 
158  case 2026: // Big5
159  mib = -2026;
160  break;
161 
162  case 2101: // Big5-HKSCS
163  mib = -2101;
164  break;
165 
166  case 16: // JIS7
167  mib = 15;
168  break;
169 
170  case 17: // SJIS
171  case 18: // eucJP
172  mib = 63;
173  break;
174  }
175 
176  // get the default encoding id for the locale encoding...
178 }
179 
180 void QFont::cleanup()
181 {
183 }
184 
192 int QFont::x11Screen() const
193 {
194  return d->screen;
195 }
196 
203 void QFont::x11SetScreen(int screen)
204 {
205  if (screen < 0) // assume default
206  screen = QX11Info::appScreen();
207 
208  if (screen == d->screen)
209  return; // nothing to do
210 
211  detach();
212  d->screen = screen;
213 }
214 
216 {
218  Q_ASSERT(engine != 0);
219  if (engine->type() == QFontEngine::Multi)
220  engine = static_cast<QFontEngineMulti *>(engine)->engine(0);
221  if (engine->type() == QFontEngine::XLFD)
222  return static_cast<QFontEngineXLFD *>(engine)->fontStruct()->fid;
223  return 0;
224 }
225 
226 
228 {
229 #ifndef QT_NO_FREETYPE
231  if (engine->type() == QFontEngine::Multi)
232  engine = static_cast<QFontEngineMulti *>(engine)->engine(0);
233 #ifndef QT_NO_FONTCONFIG
234  if (engine->type() == QFontEngine::Freetype) {
235  const QFontEngineFT *ft = static_cast<const QFontEngineFT *>(engine);
236  return ft->non_locked_face();
237  } else
238 #endif
239  if (engine->type() == QFontEngine::XLFD) {
240  const QFontEngineXLFD *xlfd = static_cast<const QFontEngineXLFD *>(engine);
241  return xlfd->non_locked_face();
242  }
243 #endif
244  return 0;
245 }
246 
247 QString QFont::rawName() const
248 {
250  Q_ASSERT(engine != 0);
251  if (engine->type() == QFontEngine::Multi)
252  engine = static_cast<QFontEngineMulti *>(engine)->engine(0);
253  if (engine->type() == QFontEngine::XLFD)
254  return QString::fromLatin1(engine->name());
255  return QString();
256 }
257 struct QtFontDesc;
258 
259 void QFont::setRawName(const QString &name)
260 {
261  detach();
262 
263  // from qfontdatabase_x11.cpp
264  extern bool qt_fillFontDef(const QByteArray &xlfd, QFontDef *fd, int dpi, QtFontDesc *desc);
265 
266  if (!qt_fillFontDef(qt_fixXLFD(name.toLatin1()), &d->request, d->dpi, 0)) {
267  qWarning("QFont::setRawName: Invalid XLFD: \"%s\"", name.toLatin1().constData());
268 
269  setFamily(name);
270  setRawMode(true);
271  } else {
273  }
274 }
275 
277 {
278  return QString::fromLatin1("Helvetica");
279 }
280 
282 {
283  switch (d->request.styleHint) {
284  case QFont::Times:
285  return QString::fromLatin1("Times");
286 
287  case QFont::Courier:
288  return QString::fromLatin1("Courier");
289 
290  case QFont::Monospace:
291  return QString::fromLatin1("Courier New");
292 
293  case QFont::Cursive:
294  return QString::fromLatin1("Comic Sans MS");
295 
296  case QFont::Fantasy:
297  return QString::fromLatin1("Impact");
298 
299  case QFont::Decorative:
300  return QString::fromLatin1("Old English");
301 
302  case QFont::Helvetica:
303  case QFont::System:
304  default:
305  return QString::fromLatin1("Helvetica");
306  }
307 }
308 
309 /*
310  Returns a last resort raw font name for the font matching algorithm.
311  This is used if even the last resort family is not available. It
312  returns \e something, almost no matter what. The current
313  implementation tries a wide variety of common fonts, returning the
314  first one it finds. The implementation may change at any time.
315 */
316 static const char * const tryFonts[] = {
317  "-*-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*",
318  "-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*",
319  "-*-times-medium-r-*-*-*-120-*-*-*-*-*-*",
320  "-*-lucida-medium-r-*-*-*-120-*-*-*-*-*-*",
321  "-*-helvetica-*-*-*-*-*-120-*-*-*-*-*-*",
322  "-*-courier-*-*-*-*-*-120-*-*-*-*-*-*",
323  "-*-times-*-*-*-*-*-120-*-*-*-*-*-*",
324  "-*-lucida-*-*-*-*-*-120-*-*-*-*-*-*",
325  "-*-helvetica-*-*-*-*-*-*-*-*-*-*-*-*",
326  "-*-courier-*-*-*-*-*-*-*-*-*-*-*-*",
327  "-*-times-*-*-*-*-*-*-*-*-*-*-*-*",
328  "-*-lucida-*-*-*-*-*-*-*-*-*-*-*-*",
329  "-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*",
330  "6x13",
331  "7x13",
332  "8x13",
333  "9x15",
334  "fixed",
335  0
336 };
337 
338 // Returns true if the font exists, false otherwise
339 static bool fontExists(const QString &fontName)
340 {
341  int count;
342  char **fontNames = XListFonts(QX11Info::display(), (char*)fontName.toLatin1().constData(), 32768, &count);
343  if (fontNames) XFreeFontNames(fontNames);
344 
345  return count != 0;
346 }
347 
349 {
350  static QString last;
351 
352  // already found
353  if (! last.isNull())
354  return last;
355 
356  int i = 0;
357  const char* f;
358 
359  while ((f = tryFonts[i])) {
360  last = QString::fromLatin1(f);
361 
362  if (fontExists(last))
363  return last;
364 
365  i++;
366  }
367 
368 #if defined(CHECK_NULL)
369  qFatal("QFontPrivate::lastResortFont: Cannot find any reasonable font");
370 #endif
371  return last;
372 }
373 
static QTextCodec * codecForLocale()
Returns a pointer to the codec most suitable for this locale.
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
QHash< int, QString > FallBackHash
Definition: qfont_x11.cpp:110
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static int appScreen()
Returns the number of the screen where the application is being displayed.
int remove(const Key &key)
Removes all the items that have the key from the hash.
Definition: qhash.h:784
QString qt_fallback_font_family(int script)
Definition: qfont_x11.cpp:114
double qt_pixelSize(double pointSize, int dpi)
Definition: qfont_x11.cpp:74
static bool fontExists(const QString &fontName)
Definition: qfont_x11.cpp:339
static QByteArray qt_fixXLFD(const QByteArray &xlfd)
Definition: qfont_x11.cpp:98
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QString lastResortFamily() const
Returns the "last resort" font family name.
Definition: qfont_mac.cpp:156
static int defaultEncodingID
Definition: qfont_p.h:167
static void cleanup()
Internal function that cleans up the font system.
Definition: qfont_mac.cpp:93
void setRawMode(bool)
If enable is true, turns raw mode on; otherwise turns raw mode off.
Definition: qfont.cpp:1842
The QString class provides a Unicode character string.
Definition: qstring.h:83
HFONT handle() const
Returns the window system handle to the font, for low-level access.
Definition: qfont_mac.cpp:121
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual Type type() const =0
int qt_encoding_id_for_mib(int mib)
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
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
virtual const char * name() const =0
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
struct FT_FaceRec_ * FT_Face
Definition: qfont.h:50
static float pixelSize(const QFontDef &request, int dpi)
Definition: qfont_win.cpp:80
uint resolve_mask
Definition: qfont.h:344
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static void initialize()
Internal function that initializes the font system.
Definition: qfont_mac.cpp:131
QFontDef request
Definition: qfont_p.h:178
const char * name
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
bool qt_fillFontDef(const QByteArray &xlfd, QFontDef *fd, int dpi, QtFontDesc *desc)
Q_CORE_EXPORT void qWarning(const char *,...)
virtual int mibEnum() const =0
Subclasses of QTextCodec must reimplement this function.
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
int mib
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
void * HANDLE
Definition: qnamespace.h:1671
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
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
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
Q_CORE_EXPORT void qFatal(const char *,...)
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
FT_Face non_locked_face() const
static void cleanup()
Definition: qfont.cpp:2927
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfont.h:343
void detach()
Detaches the font object from common font data.
Definition: qfont.cpp:803
QString defaultFamily() const
Returns the family name that corresponds to the current style hint.
Definition: qfont_mac.cpp:134
int screen
Definition: qfont_p.h:181
void setRawName(const QString &)
Sets a font by its system specific name.
Definition: qfont_mac.cpp:88
FT_Face non_locked_face() const
static const char *const tryFonts[]
Definition: qfont_x11.cpp:316
Q_GUI_EXPORT void qt_x11_set_fallback_font_family(int script, const QString &family)
Definition: qfont_x11.cpp:121
FT_Face freetypeFace() const
Returns the handle to the primary FreeType face of the font.
Definition: qfont_qws.cpp:82
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
uint styleHint
Definition: qfont_p.h:93
static float pointSize(const QFontDef &fd, int dpi)
Definition: qfont_win.cpp:90
static Display * display()
Returns the default display for the application.
double qt_pointSize(double pixelSize, int dpi)
Definition: qfont_x11.cpp:83