| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QFONT_P_H |
| 41 | #define QFONT_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists for the convenience |
| 48 | // of internal files. This header file may change from version to version |
| 49 | // without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtGui/private/qtguiglobal_p.h> |
| 55 | #include "QtGui/qfont.h" |
| 56 | #include "QtCore/qmap.h" |
| 57 | #include "QtCore/qhash.h" |
| 58 | #include "QtCore/qobject.h" |
| 59 | #include "QtCore/qstringlist.h" |
| 60 | #include <QtGui/qfontdatabase.h> |
| 61 | #include "private/qfixed_p.h" |
| 62 | |
| 63 | QT_BEGIN_NAMESPACE |
| 64 | |
| 65 | // forwards |
| 66 | class QFontCache; |
| 67 | class QFontEngine; |
| 68 | |
| 69 | #define QFONT_WEIGHT_MIN 1 |
| 70 | #define QFONT_WEIGHT_MAX 1000 |
| 71 | |
| 72 | struct QFontDef |
| 73 | { |
| 74 | inline QFontDef() |
| 75 | : pointSize(-1.0), |
| 76 | pixelSize(-1), |
| 77 | styleStrategy(QFont::PreferDefault), |
| 78 | stretch(QFont::AnyStretch), |
| 79 | style(QFont::StyleNormal), |
| 80 | hintingPreference(QFont::PreferDefaultHinting), |
| 81 | styleHint(QFont::AnyStyle), |
| 82 | weight(QFont::Normal), |
| 83 | fixedPitch(false), |
| 84 | ignorePitch(true), |
| 85 | fixedPitchComputed(0), |
| 86 | reserved(0) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | QString family; |
| 91 | QStringList families; |
| 92 | QString styleName; |
| 93 | |
| 94 | QStringList fallBackFamilies; |
| 95 | |
| 96 | qreal pointSize; |
| 97 | qreal pixelSize; |
| 98 | |
| 99 | // Note: Variable ordering matters to make sure no variable overlaps two 32-bit registers. |
| 100 | uint styleStrategy : 16; |
| 101 | uint stretch : 12; // 0-4000 |
| 102 | uint style : 2; |
| 103 | uint hintingPreference : 2; |
| 104 | |
| 105 | uint styleHint : 8; |
| 106 | uint weight : 10; // 1-1000 |
| 107 | uint fixedPitch : 1; |
| 108 | uint ignorePitch : 1; |
| 109 | uint fixedPitchComputed : 1; // for Mac OS X only |
| 110 | uint reserved : 11; // for future extensions |
| 111 | |
| 112 | bool exactMatch(const QFontDef &other) const; |
| 113 | bool operator==(const QFontDef &other) const |
| 114 | { |
| 115 | return pixelSize == other.pixelSize |
| 116 | && weight == other.weight |
| 117 | && style == other.style |
| 118 | && stretch == other.stretch |
| 119 | && styleHint == other.styleHint |
| 120 | && styleStrategy == other.styleStrategy |
| 121 | && ignorePitch == other.ignorePitch && fixedPitch == other.fixedPitch |
| 122 | && family == other.family |
| 123 | && families == other.families |
| 124 | && styleName == other.styleName |
| 125 | && hintingPreference == other.hintingPreference |
| 126 | ; |
| 127 | } |
| 128 | inline bool operator<(const QFontDef &other) const |
| 129 | { |
| 130 | if (pixelSize != other.pixelSize) return pixelSize < other.pixelSize; |
| 131 | if (weight != other.weight) return weight < other.weight; |
| 132 | if (style != other.style) return style < other.style; |
| 133 | if (stretch != other.stretch) return stretch < other.stretch; |
| 134 | if (styleHint != other.styleHint) return styleHint < other.styleHint; |
| 135 | if (styleStrategy != other.styleStrategy) return styleStrategy < other.styleStrategy; |
| 136 | if (family != other.family) return family < other.family; |
| 137 | if (families != other.families) return families < other.families; |
| 138 | if (styleName != other.styleName) |
| 139 | return styleName < other.styleName; |
| 140 | if (hintingPreference != other.hintingPreference) return hintingPreference < other.hintingPreference; |
| 141 | |
| 142 | |
| 143 | if (ignorePitch != other.ignorePitch) return ignorePitch < other.ignorePitch; |
| 144 | if (fixedPitch != other.fixedPitch) return fixedPitch < other.fixedPitch; |
| 145 | return false; |
| 146 | } |
| 147 | }; |
| 148 | |
| 149 | inline size_t qHash(const QFontDef &fd, size_t seed = 0) noexcept |
| 150 | { |
| 151 | return qHashMulti(seed, |
| 152 | qRound64(fd.pixelSize*10000), // use only 4 fractional digits |
| 153 | fd.weight, |
| 154 | fd.style, |
| 155 | fd.stretch, |
| 156 | fd.styleHint, |
| 157 | fd.styleStrategy, |
| 158 | fd.ignorePitch, |
| 159 | fd.fixedPitch, |
| 160 | fd.family, |
| 161 | fd.families, |
| 162 | fd.styleName, |
| 163 | fd.hintingPreference); |
| 164 | } |
| 165 | |
| 166 | class QFontEngineData |
| 167 | { |
| 168 | public: |
| 169 | QFontEngineData(); |
| 170 | ~QFontEngineData(); |
| 171 | |
| 172 | QAtomicInt ref; |
| 173 | const int fontCacheId; |
| 174 | |
| 175 | QFontEngine *engines[QChar::ScriptCount]; |
| 176 | |
| 177 | private: |
| 178 | Q_DISABLE_COPY_MOVE(QFontEngineData) |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | class Q_GUI_EXPORT QFontPrivate |
| 183 | { |
| 184 | public: |
| 185 | |
| 186 | QFontPrivate(); |
| 187 | QFontPrivate(const QFontPrivate &other); |
| 188 | ~QFontPrivate(); |
| 189 | |
| 190 | QFontEngine *engineForScript(int script) const; |
| 191 | void alterCharForCapitalization(QChar &c) const; |
| 192 | |
| 193 | QAtomicInt ref; |
| 194 | QFontDef request; |
| 195 | mutable QFontEngineData *engineData; |
| 196 | int dpi; |
| 197 | |
| 198 | uint underline : 1; |
| 199 | uint overline : 1; |
| 200 | uint strikeOut : 1; |
| 201 | uint kerning : 1; |
| 202 | uint capital : 3; |
| 203 | bool letterSpacingIsAbsolute : 1; |
| 204 | |
| 205 | QFixed letterSpacing; |
| 206 | QFixed wordSpacing; |
| 207 | |
| 208 | mutable QFontPrivate *scFont; |
| 209 | QFont smallCapsFont() const { return QFont(smallCapsFontPrivate()); } |
| 210 | QFontPrivate *smallCapsFontPrivate() const; |
| 211 | |
| 212 | static QFontPrivate *get(const QFont &font) |
| 213 | { |
| 214 | return font.d.data(); |
| 215 | } |
| 216 | |
| 217 | void resolve(uint mask, const QFontPrivate *other); |
| 218 | |
| 219 | static void detachButKeepEngineData(QFont *font); |
| 220 | |
| 221 | private: |
| 222 | QFontPrivate &operator=(const QFontPrivate &) { return *this; } |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | class Q_GUI_EXPORT QFontCache : public QObject |
| 227 | { |
| 228 | public: |
| 229 | // note: these static functions work on a per-thread basis |
| 230 | static QFontCache *instance(); |
| 231 | static void cleanup(); |
| 232 | |
| 233 | QFontCache(); |
| 234 | ~QFontCache(); |
| 235 | |
| 236 | int id() const { return m_id; } |
| 237 | |
| 238 | void clear(); |
| 239 | |
| 240 | struct Key { |
| 241 | Key() : script(0), multi(0) { } |
| 242 | Key(const QFontDef &d, uchar c, bool m = 0) |
| 243 | : def(d), script(c), multi(m) { } |
| 244 | |
| 245 | QFontDef def; |
| 246 | uchar script; |
| 247 | uchar multi: 1; |
| 248 | |
| 249 | inline bool operator<(const Key &other) const |
| 250 | { |
| 251 | if (script != other.script) return script < other.script; |
| 252 | if (multi != other.multi) return multi < other.multi; |
| 253 | if (multi && def.fallBackFamilies.size() != other.def.fallBackFamilies.size()) |
| 254 | return def.fallBackFamilies.size() < other.def.fallBackFamilies.size(); |
| 255 | return def < other.def; |
| 256 | } |
| 257 | inline bool operator==(const Key &other) const |
| 258 | { |
| 259 | return script == other.script |
| 260 | && multi == other.multi |
| 261 | && (!multi || def.fallBackFamilies == other.def.fallBackFamilies) |
| 262 | && def == other.def; |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | // QFontEngineData cache |
| 267 | typedef QMap<QFontDef, QFontEngineData*> EngineDataCache; |
| 268 | EngineDataCache engineDataCache; |
| 269 | |
| 270 | QFontEngineData *findEngineData(const QFontDef &def) const; |
| 271 | void insertEngineData(const QFontDef &def, QFontEngineData *engineData); |
| 272 | |
| 273 | // QFontEngine cache |
| 274 | struct Engine { |
| 275 | Engine() : data(nullptr), timestamp(0), hits(0) { } |
| 276 | Engine(QFontEngine *d) : data(d), timestamp(0), hits(0) { } |
| 277 | |
| 278 | QFontEngine *data; |
| 279 | uint timestamp; |
| 280 | uint hits; |
| 281 | }; |
| 282 | |
| 283 | typedef QMultiMap<Key,Engine> EngineCache; |
| 284 | EngineCache engineCache; |
| 285 | QHash<QFontEngine *, int> engineCacheCount; |
| 286 | |
| 287 | QFontEngine *findEngine(const Key &key); |
| 288 | |
| 289 | void updateHitCountAndTimeStamp(Engine &value); |
| 290 | void insertEngine(const Key &key, QFontEngine *engine, bool insertMulti = false); |
| 291 | |
| 292 | private: |
| 293 | void increaseCost(uint cost); |
| 294 | void decreaseCost(uint cost); |
| 295 | void timerEvent(QTimerEvent *event) override; |
| 296 | void decreaseCache(); |
| 297 | |
| 298 | static const uint min_cost; |
| 299 | uint total_cost, max_cost; |
| 300 | uint current_timestamp; |
| 301 | bool fast; |
| 302 | int timer_id; |
| 303 | const int m_id; |
| 304 | }; |
| 305 | |
| 306 | Q_GUI_EXPORT int qt_defaultDpiX(); |
| 307 | Q_GUI_EXPORT int qt_defaultDpiY(); |
| 308 | Q_GUI_EXPORT int qt_defaultDpi(); |
| 309 | |
| 310 | Q_GUI_EXPORT int qt_legacyToOpenTypeWeight(int weight); |
| 311 | Q_GUI_EXPORT int qt_openTypeToLegacyWeight(int weight); |
| 312 | |
| 313 | QT_END_NAMESPACE |
| 314 | |
| 315 | #endif // QFONT_P_H |
| 316 | |