📄 qfont.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qfont.h"#include "qdebug.h"#include "qpaintdevice.h"#include "qfontdatabase.h"#include "qfontmetrics.h"#include "qfontinfo.h"#include "qpainter.h"#include "qhash.h"#include "qdatastream.h"#include "qapplication.h"#include "qstringlist.h"#include "qthread.h"#include <private/qunicodetables_p.h>#include "qfont_p.h"#include <private/qfontengine_p.h>#include <private/qpainter_p.h>#include <private/qtextengine_p.h>#ifdef Q_WS_X11#include "qx11info_x11.h"extern const QX11Info *qt_x11Info(const QPaintDevice *pd);#endif#ifdef Q_WS_QWS#include "qscreen_qws.h"#if !defined(QT_NO_QWS_QPF2)#include <qfile.h>#include "qfontengine_qpf_p.h"#endif#endif// #define QFONTCACHE_DEBUG#ifdef QFONTCACHE_DEBUG# define FC_DEBUG qDebug#else# define FC_DEBUG if (false) qDebug#endifbool QFontDef::exactMatch(const QFontDef &other) const{ /* QFontDef comparison is more complicated than just simple per-member comparisons. When comparing point/pixel sizes, either point or pixelsize could be -1. in This case we have to compare the non negative size value. This test will fail if the point-sizes differ by 1/2 point or more or they do not round to the same value. We have to do this since our API still uses 'int' point-sizes in the API, but store deci-point-sizes internally. To compare the family members, we need to parse the font names and compare the family/foundry strings separately. This allows us to compare e.g. "Helvetica" and "Helvetica [Adobe]" with positive results. */ if (pixelSize != -1 && other.pixelSize != -1) { if (pixelSize != other.pixelSize) return false; } else if (pointSize != -1 && other.pointSize != -1) { if (pointSize != other.pointSize) return false; } else { return false; } if (!ignorePitch && !other.ignorePitch && fixedPitch != other.fixedPitch) return false; if (stretch != 0 && other.stretch != 0 && stretch != other.stretch) return false; QString this_family, this_foundry, other_family, other_foundry; QFontDatabase::parseFontName(family, this_foundry, this_family); QFontDatabase::parseFontName(other.family, other_foundry, other_family); return (styleHint == other.styleHint && styleStrategy == other.styleStrategy && weight == other.weight && style == other.style && this_family == other_family && (this_foundry.isEmpty() || other_foundry.isEmpty() || this_foundry == other_foundry)#ifdef Q_WS_X11 && addStyle == other.addStyle#endif // Q_WS_X11 );}#ifdef Q_WS_WINextern HDC shared_dc;#endifextern bool qt_is_gui_used;Q_GUI_EXPORT int qt_defaultDpiX(){ if (!qt_is_gui_used) return 75; int dpi;#ifdef Q_WS_X11 dpi = QX11Info::appDpiX();#elif defined(Q_WS_WIN) dpi = GetDeviceCaps(shared_dc,LOGPIXELSX);#elif defined(Q_WS_MAC) extern float qt_mac_defaultDpi_x(); //qpaintdevice_mac.cpp dpi = qt_mac_defaultDpi_x();#elif defined(Q_WS_QWS) if (!qt_screen) return 72; QScreen *screen = qt_screen; const QList<QScreen*> subScreens = qt_screen->subScreens(); if (!subScreens.isEmpty()) screen = subScreens.at(0); dpi = qRound(screen->width() / double(screen->physicalWidth() / 25.4));#endif // Q_WS_X11 return dpi;}Q_GUI_EXPORT int qt_defaultDpiY(){ if (!qt_is_gui_used) return 75; int dpi;#ifdef Q_WS_X11 dpi = QX11Info::appDpiY();#elif defined(Q_WS_WIN) dpi = GetDeviceCaps(shared_dc,LOGPIXELSY);#elif defined(Q_WS_MAC) extern float qt_mac_defaultDpi_y(); //qpaintdevice_mac.cpp dpi = qt_mac_defaultDpi_y();#elif defined(Q_WS_QWS) if (!qt_screen) return 72; QScreen *screen = qt_screen; const QList<QScreen*> subScreens = qt_screen->subScreens(); if (!subScreens.isEmpty()) screen = subScreens.at(0); dpi = qRound(screen->height() / double(screen->physicalHeight() / 25.4));#endif // Q_WS_X11 return dpi;}Q_GUI_EXPORT int qt_defaultDpi(){ return qt_defaultDpiY();}QFontPrivate::QFontPrivate() : engineData(0), dpi(qt_defaultDpi()), screen(0), rawMode(false), underline(false), overline(false), strikeOut(false), kerning(true){ ref = 1;#ifdef Q_WS_X11 if (QX11Info::display()) screen = QX11Info::appScreen(); else screen = 0;#endif#ifdef Q_WS_WIN hdc = 0;#endif}QFontPrivate::QFontPrivate(const QFontPrivate &other) : request(other.request), engineData(0), dpi(other.dpi), screen(other.screen), rawMode(other.rawMode), underline(other.underline), overline(other.overline), strikeOut(other.strikeOut), kerning(other.kerning){ ref = 1;#ifdef Q_WS_WIN hdc = other.hdc;#endif}QFontPrivate::~QFontPrivate(){ if (engineData) engineData->ref.deref(); engineData = 0;}void QFontPrivate::resolve(uint mask, const QFontPrivate *other){ Q_ASSERT(other != 0); dpi = other->dpi; if ((mask & Complete) == Complete) return; // assign the unset-bits with the set-bits of the other font def if (! (mask & Family)) request.family = other->request.family; if (! (mask & Size)) { request.pointSize = other->request.pointSize; request.pixelSize = other->request.pixelSize; } if (! (mask & StyleHint)) request.styleHint = other->request.styleHint; if (! (mask & StyleStrategy)) request.styleStrategy = other->request.styleStrategy; if (! (mask & Weight)) request.weight = other->request.weight; if (! (mask & Style)) request.style = other->request.style; if (! (mask & FixedPitch)) request.fixedPitch = other->request.fixedPitch; if (! (mask & Stretch)) request.stretch = other->request.stretch; if (! (mask & Underline)) underline = other->underline; if (! (mask & Overline)) overline = other->overline; if (! (mask & StrikeOut)) strikeOut = other->strikeOut; if (! (mask & Kerning)) kerning = other->kerning;}QFontEngineData::QFontEngineData(){ ref = 1;#if !defined(Q_WS_MAC) memset(engines, 0, QUnicodeTables::ScriptCount * sizeof(QFontEngine *));#else engine = 0;#endif}QFontEngineData::~QFontEngineData(){#if !defined(Q_WS_MAC) for (int i = 0; i < QUnicodeTables::ScriptCount; ++i) { if (engines[i]) engines[i]->ref.deref(); engines[i] = 0; }#else if (engine) engine->ref.deref(); engine = 0;#endif // Q_WS_X11 || Q_WS_WIN || Q_WS_MAC}/*! \class QFont \brief The QFont class specifies a font used for drawing text. \ingroup multimedia \ingroup appearance \ingroup shared \ingroup text \mainclass When you create a QFont object you specify various attributes that you want the font to have. Qt will use the font with the specified attributes, or if no matching font exists, Qt will use the closest matching installed font. The attributes of the font that is actually used are retrievable from a QFontInfo object. If the window system provides an exact match exactMatch() returns true. Use QFontMetrics to get measurements, e.g. the pixel length of a string using QFontMetrics::width(). Use QApplication::setFont() to set the application's default font. If a chosen font does not include all the characters that need to be displayed, QFont will try to find the characters in the nearest equivalent fonts. When a QPainter draws a character from a font the QFont will report whether or not it has the character; if it does not, QPainter will draw an unfilled square. Create QFonts like this: \code QFont serifFont("Times", 10, QFont::Bold); QFont sansFont("Helvetica [Cronyx]", 12); \endcode The attributes set in the constructor can also be set later, e.g. setFamily(), setPointSize(), setPointSizeFloat(), setWeight() and setItalic(). The remaining attributes must be set after contstruction, e.g. setBold(), setUnderline(), setOverline(), setStrikeOut() and setFixedPitch(). QFontInfo objects should be created \e after the font's attributes have been set. A QFontInfo object will not change, even if you change the font's attributes. The corresponding "get" functions, e.g. family(), pointSize(), etc., return the values that were set, even though the values used may differ. The actual values are available from a QFontInfo object. If the requested font family is unavailable you can influence the \link #fontmatching font matching algorithm\endlink by choosing a particular \l{QFont::StyleHint} and \l{QFont::StyleStrategy} with setStyleHint(). The default family (corresponding to the current style hint) is returned by defaultFamily(). The font-matching algorithm has a lastResortFamily() and lastResortFont() in cases where a suitable match cannot be found. You can provide substitutions for font family names using insertSubstitution() and insertSubstitutions(). Substitutions can be removed with removeSubstitution(). Use substitute() to retrieve a family's first substitute, or the family name itself if it has no substitutes. Use substitutes() to retrieve a list of a family's substitutes (which may be empty). Every QFont has a key() which you can use, for example, as the key in a cache or dictionary. If you want to store a user's font preferences you could use QSettings, writing the font information with toString() and reading it back with fromString(). The operator<<() and operator>>() functions are also available, but they work on a data stream. It is possible to set the height of characters shown on the screen to a specified number of pixels with setPixelSize(); however using setPointSize() has a similar effect and provides device independence. Under X11 you can set a font using its system specific name with setRawName(). Loading fonts can be expensive, especially on X11. QFont contains extensive optimizations to make the copying of QFont objects fast, and to cache the results of the slow window system functions it depends upon. \target fontmatching The font matching algorithm works as follows: \list 1 \o The specified font family is searched for. \o If not found, the styleHint() is used to select a replacement family. \o Each replacement font family is searched for. \o If none of these are found or there was no styleHint(), "helvetica" will be searched for. \o If "helvetica" isn't found Qt will try the lastResortFamily(). \o If the lastResortFamily() isn't found Qt will try the lastResortFont() which will always return a name of some kind. \endlist Note that the actual font matching algorithm varies from platform to platform. Once a font is found, the remaining attributes are matched in order of priority: \list 1 \o fixedPitch() \o pointSize() (see below) \o weight() \o style() \endlist If you have a font which matches on family, even if none of the other attributes match, this font will be chosen in preference to a font which doesn't match on family but which does match on the other attributes. This is because font family is the dominant search criteria. The point size is defined to match if it is within 20% of the requested point size. When several fonts match and are only distinguished by point size, the font with the closest point size to the one requested will be chosen. The actual family, font size, weight and other font attributes used for drawing text will depend on what's available for the chosen family under the window system. A QFontInfo object can be used to determine the actual values used for drawing the text. Examples: \code QFont f("Helvetica"); \endcode If you had both an Adobe and a Cronyx Helvetica, you might get either. \code QFont f("Helvetica [Cronyx]"); \endcode You can specify the foundry you want in the family name. The font f in the above example will be set to "Helvetica [Cronyx]". To determine the attributes of the font actually used in the window system, use a QFontInfo object, e.g. \code QFontInfo info(f1); QString family = info.family(); \endcode To find out font metrics use a QFontMetrics object, e.g. \code QFontMetrics fm(f1); int textWidthInPixels = fm.width("How many pixels wide is this text?"); int textHeightInPixels = fm.height(); \endcode For more general information on fonts, see the \link http://nwalsh.com/comp.fonts/FAQ/ comp.fonts FAQ.\endlink Information on encodings can be found from \link http://czyborra.com/ Roman Czyborra's\endlink page. \sa QFontComboBox, QFontMetrics, QFontInfo, QFontDatabase, {Character Map Example}*//*! \enum QFont::Style This enum describes the different styles of glyphs that are used to display text. \value StyleNormal Normal glyphs used in unstyled text. \value StyleItalic Italic glyphs that are specifically designed for the purpose of representing italicized text. \value StyleOblique Glyphs with an italic appearance that are typically based on the unstyled glyphs, but are not fine-tuned for the purpose of representing italicized text. \sa Weight
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -