Get started with patching up the Qt GUI docs
[profile/ivi/qtbase.git] / src / gui / text / qfont_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QFONT_P_H
43 #define QFONT_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists for the convenience
50 // of internal files.  This header file may change from version to version
51 // without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "QtGui/qfont.h"
57 #include "QtCore/qmap.h"
58 #include "QtCore/qobject.h"
59 #include "QtCore/qstringlist.h"
60 #include <private/qunicodetables_p.h>
61 #include <QtGui/qfontdatabase.h>
62 #include "private/qfixed_p.h"
63
64 QT_BEGIN_NAMESPACE
65
66 // forwards
67 class QFontCache;
68 class QFontEngine;
69
70 struct QFontDef
71 {
72     inline QFontDef()
73         : pointSize(-1.0), pixelSize(-1),
74           styleStrategy(QFont::PreferDefault), styleHint(QFont::AnyStyle),
75           weight(50), fixedPitch(false), style(QFont::StyleNormal), stretch(100),
76           ignorePitch(true), hintingPreference(QFont::PreferDefaultHinting)
77     {
78     }
79
80     QString family;
81     QString styleName;
82
83     QStringList fallBackFamilies;
84
85     qreal pointSize;
86     qreal pixelSize;
87
88     uint styleStrategy : 16;
89     uint styleHint     : 8;
90
91     uint weight     :  7; // 0-99
92     uint fixedPitch :  1;
93     uint style      :  2;
94     uint stretch    : 12; // 0-400
95
96     uint ignorePitch : 1;
97     uint hintingPreference : 2;
98     uint fixedPitchComputed : 1; // for Mac OS X only
99     int reserved   : 14; // for future extensions
100
101     bool exactMatch(const QFontDef &other) const;
102     bool operator==(const QFontDef &other) const
103     {
104         return pixelSize == other.pixelSize
105                     && weight == other.weight
106                     && style == other.style
107                     && stretch == other.stretch
108                     && styleHint == other.styleHint
109                     && styleStrategy == other.styleStrategy
110                     && ignorePitch == other.ignorePitch && fixedPitch == other.fixedPitch
111                     && family == other.family
112                     && (styleName.isEmpty() || other.styleName.isEmpty() || styleName == other.styleName)
113                     && hintingPreference == other.hintingPreference
114                           ;
115     }
116     inline bool operator<(const QFontDef &other) const
117     {
118         if (pixelSize != other.pixelSize) return pixelSize < other.pixelSize;
119         if (weight != other.weight) return weight < other.weight;
120         if (style != other.style) return style < other.style;
121         if (stretch != other.stretch) return stretch < other.stretch;
122         if (styleHint != other.styleHint) return styleHint < other.styleHint;
123         if (styleStrategy != other.styleStrategy) return styleStrategy < other.styleStrategy;
124         if (family != other.family) return family < other.family;
125         if (!styleName.isEmpty() && !other.styleName.isEmpty() && styleName != other.styleName)
126             return styleName < other.styleName;
127         if (hintingPreference != other.hintingPreference) return hintingPreference < other.hintingPreference;
128
129
130         if (ignorePitch != other.ignorePitch) return ignorePitch < other.ignorePitch;
131         if (fixedPitch != other.fixedPitch) return fixedPitch < other.fixedPitch;
132         return false;
133     }
134 };
135
136 class QFontEngineData
137 {
138 public:
139     QFontEngineData();
140     ~QFontEngineData();
141
142     QAtomicInt ref;
143     QFontCache *fontCache;
144
145     QFontEngine *engines[QUnicodeTables::ScriptCount];
146 };
147
148
149 class Q_GUI_EXPORT QFontPrivate
150 {
151 public:
152
153     QFontPrivate();
154     QFontPrivate(const QFontPrivate &other);
155     ~QFontPrivate();
156
157     QFontEngine *engineForScript(int script) const;
158     void alterCharForCapitalization(QChar &c) const;
159
160     QAtomicInt ref;
161     QFontDef request;
162     mutable QFontEngineData *engineData;
163     int dpi;
164     int screen;
165
166
167     uint rawMode    :  1;
168     uint underline  :  1;
169     uint overline   :  1;
170     uint strikeOut  :  1;
171     uint kerning    :  1;
172     uint capital    :  3;
173     bool letterSpacingIsAbsolute : 1;
174
175     QFixed letterSpacing;
176     QFixed wordSpacing;
177
178     mutable QFontPrivate *scFont;
179     QFont smallCapsFont() const { return QFont(smallCapsFontPrivate()); }
180     QFontPrivate *smallCapsFontPrivate() const;
181
182     static QFontPrivate *get(const QFont &font)
183     {
184         return font.d.data();
185     }
186
187     void resolve(uint mask, const QFontPrivate *other);
188 private:
189     QFontPrivate &operator=(const QFontPrivate &) { return *this; }
190 };
191
192
193 class QFontCache : public QObject
194 {
195     Q_OBJECT
196 public:
197     // note: these static functions work on a per-thread basis
198     static QFontCache *instance();
199     static void cleanup();
200
201     QFontCache();
202     ~QFontCache();
203
204     void clear();
205     // universal key structure.  QFontEngineDatas and QFontEngines are cached using
206     // the same keys
207     struct Key {
208         Key() : script(0), screen(0) { }
209         Key(const QFontDef &d, int c, int s = 0)
210             : def(d), script(c), screen(s) { }
211
212         QFontDef def;
213         int script;
214         int screen;
215
216         inline bool operator<(const Key &other) const
217         {
218             if (script != other.script) return script < other.script;
219             if (screen != other.screen) return screen < other.screen;
220             return def < other.def;
221         }
222         inline bool operator==(const Key &other) const
223         { return def == other.def && script == other.script && screen == other.screen; }
224     };
225
226     // QFontEngineData cache
227     typedef QMap<QFontDef, QFontEngineData*> EngineDataCache;
228     EngineDataCache engineDataCache;
229
230     QFontEngineData *findEngineData(const QFontDef &def) const;
231     void insertEngineData(const QFontDef &def, QFontEngineData *engineData);
232
233     // QFontEngine cache
234     struct Engine {
235         Engine() : data(0), timestamp(0), hits(0) { }
236         Engine(QFontEngine *d) : data(d), timestamp(0), hits(0) { }
237
238         QFontEngine *data;
239         uint timestamp;
240         uint hits;
241     };
242
243     typedef QMap<Key,Engine> EngineCache;
244     EngineCache engineCache;
245
246     QFontEngine *findEngine(const Key &key);
247
248     void updateHitCountAndTimeStamp(Engine &value);
249     void insertEngine(const Key &key, QFontEngine *engine, bool insertMulti = false);
250
251     private:
252     void increaseCost(uint cost);
253     void decreaseCost(uint cost);
254     void timerEvent(QTimerEvent *event);
255
256     static const uint min_cost;
257     uint total_cost, max_cost;
258     uint current_timestamp;
259     bool fast;
260     int timer_id;
261 };
262
263 Q_GUI_EXPORT int qt_defaultDpiX();
264 Q_GUI_EXPORT int qt_defaultDpiY();
265 Q_GUI_EXPORT int qt_defaultDpi();
266
267 QT_END_NAMESPACE
268
269 #endif // QFONT_P_H