1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QFONTENGINE_P_H
43 #define QFONTENGINE_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "QtCore/qglobal.h"
57 #include "QtCore/qatomic.h"
58 #include <QtCore/qvarlengtharray.h>
59 #include <QtCore/QLinkedList>
60 #include "private/qtextengine_p.h"
61 #include "private/qfont_p.h"
64 # include "QtCore/qt_windows.h"
68 # include "private/qt_mac_p.h"
69 # include "QtCore/qmap.h"
70 # include "QtCore/qcache.h"
71 # include "private/qcore_mac_p.h"
74 #include <private/qfontengineglyphcache_p.h>
76 struct glyph_metrics_t;
77 typedef unsigned int glyph_t;
87 #define MAKE_TAG(ch1, ch2, ch3, ch4) (\
88 (((quint32)(ch1)) << 24) | \
89 (((quint32)(ch2)) << 16) | \
90 (((quint32)(ch3)) << 8) | \
95 class Q_GUI_EXPORT QFontEngine : public QObject
108 // Apple Mac OS types
118 S60FontEngine, // Cannot be simply called "S60". Reason is qt_s60Data.h
122 TestFontEngine = 0x1000
127 Format_Render = Format_None,
134 virtual ~QFontEngine();
136 // all of these are in unscaled metrics if the engine supports uncsaled metrics,
137 // otherwise in design metrics
139 QByteArray postscriptName;
140 QByteArray copyright;
150 virtual Properties properties() const;
151 virtual void getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics);
152 QByteArray getSfntTable(uint /*tag*/) const;
153 virtual bool getSfntTableData(uint /*tag*/, uchar * /*buffer*/, uint * /*length*/) const { return false; }
156 FaceId() : index(0), encoding(0) {}
161 virtual FaceId faceId() const { return FaceId(); }
162 enum SynthesizedFlags {
163 SynthesizedItalic = 0x1,
164 SynthesizedBold = 0x2,
165 SynthesizedStretch = 0x4
167 virtual int synthesized() const { return 0; }
168 virtual bool supportsSubPixelPositions() const { return false; }
170 virtual QFixed emSquareSize() const { return ascent(); }
172 /* returns 0 as glyph index for non existent glyphs */
173 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const = 0;
176 * This is a callback from harfbuzz. The font engine uses the font-system in use to find out the
177 * advances of each glyph and set it on the layout.
179 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
180 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
182 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN) && !defined(Q_WS_QPA)
183 virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si) = 0;
185 virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
186 QPainterPath *path, QTextItem::RenderFlags flags);
188 void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
189 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
191 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
192 void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
194 * Create a qimage with the alpha values for the glyph.
195 * Returns an image indexed_8 with index values ranging from 0=fully transparent to 255=opaque
197 virtual QImage alphaMapForGlyph(glyph_t);
198 virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
199 virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
200 virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
201 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
203 virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
205 return boundingBox(glyph, matrix);
208 virtual void removeGlyphFromCache(glyph_t);
210 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) = 0;
211 virtual glyph_metrics_t boundingBox(glyph_t glyph) = 0;
212 virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
213 glyph_metrics_t tightBoundingBox(const QGlyphLayout &glyphs);
215 virtual QFixed ascent() const = 0;
216 virtual QFixed descent() const = 0;
217 virtual QFixed leading() const = 0;
218 virtual QFixed xHeight() const;
219 virtual QFixed averageCharWidth() const;
221 virtual QFixed lineThickness() const;
222 virtual QFixed underlinePosition() const;
224 virtual qreal maxCharWidth() const = 0;
225 virtual qreal minLeftBearing() const { return qreal(); }
226 virtual qreal minRightBearing() const { return qreal(); }
228 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
230 virtual const char *name() const = 0;
232 virtual bool canRender(const QChar *string, int len) = 0;
234 virtual Type type() const = 0;
236 virtual int glyphCount() const;
238 virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
240 HB_Font harfbuzzFont() const;
241 HB_Face harfbuzzFace() const;
243 virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
245 void setGlyphCache(void *key, QFontEngineGlyphCache *data);
246 QFontEngineGlyphCache *glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
248 static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
249 static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
251 static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
259 virtual void setDefaultHintStyle(HintStyle) { }
263 uint cache_cost; // amount of mem used in kb by the font
267 mutable HB_FontRec hbFont;
268 mutable HB_Face hbFace;
269 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_WS_QPA) || defined(Q_OS_SYMBIAN)
274 inline bool operator<(const KernPair &other) const
276 return left_right < other.left_right;
279 QVector<KernPair> kerning_pairs;
280 void loadKerningPairs(QFixed scalingFactor);
286 static const QVector<QRgb> &grayPalette();
287 QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
290 struct GlyphCacheEntry {
292 QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
293 bool operator==(const GlyphCacheEntry &other) { return context == other.context && cache == other.cache; }
296 mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
299 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
301 return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
304 inline uint qHash(const QFontEngine::FaceId &f)
306 return qHash((f.index << 16) + f.encoding) + qHash(f.filename);
312 #if defined(Q_WS_QWS)
314 #ifndef QT_NO_QWS_QPF
316 class QFontEngineQPF1Data;
318 class QFontEngineQPF1 : public QFontEngine
321 QFontEngineQPF1(const QFontDef&, const QString &fn);
324 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
325 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
327 virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
328 virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
330 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
331 virtual glyph_metrics_t boundingBox(glyph_t glyph);
333 virtual QFixed ascent() const;
334 virtual QFixed descent() const;
335 virtual QFixed leading() const;
336 virtual qreal maxCharWidth() const;
337 virtual qreal minLeftBearing() const;
338 virtual qreal minRightBearing() const;
339 virtual QFixed underlinePosition() const;
340 virtual QFixed lineThickness() const;
342 virtual Type type() const;
344 virtual bool canRender(const QChar *string, int len);
345 inline const char *name() const { return 0; }
346 virtual QImage alphaMapForGlyph(glyph_t);
349 QFontEngineQPF1Data *d;
351 #endif // QT_NO_QWS_QPF
356 class QFontEngineBox : public QFontEngine
359 QFontEngineBox(int size);
362 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
363 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
365 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN)
366 void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
368 virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
370 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
371 virtual glyph_metrics_t boundingBox(glyph_t glyph);
373 virtual QFixed ascent() const;
374 virtual QFixed descent() const;
375 virtual QFixed leading() const;
376 virtual qreal maxCharWidth() const;
377 virtual qreal minLeftBearing() const { return 0; }
378 virtual qreal minRightBearing() const { return 0; }
379 virtual QImage alphaMapForGlyph(glyph_t);
384 virtual const char *name() const;
386 virtual bool canRender(const QChar *string, int len);
388 virtual Type type() const;
389 inline int size() const { return _size; }
392 friend class QFontPrivate;
396 class QFontEngineMulti : public QFontEngine
399 explicit QFontEngineMulti(int engineCount);
402 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
403 QTextEngine::ShaperFlags flags) const;
405 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
406 virtual glyph_metrics_t boundingBox(glyph_t glyph);
408 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
409 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
410 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
411 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
413 virtual QFixed ascent() const;
414 virtual QFixed descent() const;
415 virtual QFixed leading() const;
416 virtual QFixed xHeight() const;
417 virtual QFixed averageCharWidth() const;
418 virtual QImage alphaMapForGlyph(glyph_t);
420 virtual QFixed lineThickness() const;
421 virtual QFixed underlinePosition() const;
422 virtual qreal maxCharWidth() const;
423 virtual qreal minLeftBearing() const;
424 virtual qreal minRightBearing() const;
426 virtual inline Type type() const
427 { return QFontEngine::Multi; }
429 virtual bool canRender(const QChar *string, int len);
430 inline virtual const char *name() const
433 QFontEngine *engine(int at) const
434 {Q_ASSERT(at < engines.size()); return engines.at(at); }
438 friend class QPSPrintEnginePrivate;
439 friend class QPSPrintEngineFontMulti;
440 friend class QRawFont;
441 virtual void loadEngine(int at) = 0;
442 QVector<QFontEngine *> engines;
445 class QTestFontEngine : public QFontEngineBox
448 QTestFontEngine(int size) : QFontEngineBox(size) {}
449 virtual Type type() const { return TestFontEngine; }
455 # include "private/qfontengine_win_p.h"
458 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_FREETYPE)
459 # include "private/qfontengine_ft_p.h"
462 #endif // QFONTENGINE_P_H