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) {}
162 virtual FaceId faceId() const { return FaceId(); }
163 enum SynthesizedFlags {
164 SynthesizedItalic = 0x1,
165 SynthesizedBold = 0x2,
166 SynthesizedStretch = 0x4
168 virtual int synthesized() const { return 0; }
169 virtual bool supportsSubPixelPositions() const { return false; }
170 QFixed subPixelPositionForX(QFixed x);
172 virtual QFixed emSquareSize() const { return ascent(); }
174 /* returns 0 as glyph index for non existent glyphs */
175 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const = 0;
178 * This is a callback from harfbuzz. The font engine uses the font-system in use to find out the
179 * advances of each glyph and set it on the layout.
181 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
182 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
184 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN) && !defined(Q_WS_QPA)
185 virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si) = 0;
187 virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
188 QPainterPath *path, QTextItem::RenderFlags flags);
190 void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
191 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
193 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
194 void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
196 * Create a qimage with the alpha values for the glyph.
197 * Returns an image indexed_8 with index values ranging from 0=fully transparent to 255=opaque
199 // ### Refactor this into a smaller and more flexible API.
200 virtual QImage alphaMapForGlyph(glyph_t);
201 virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
202 virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
203 virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
204 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
205 virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
206 GlyphFormat neededFormat,
207 const QTransform &t = QTransform(),
209 virtual void unlockAlphaMapForGlyph();
210 virtual bool hasInternalCaching() const { return false; }
212 virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
214 return boundingBox(glyph, matrix);
217 virtual void removeGlyphFromCache(glyph_t);
219 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) = 0;
220 virtual glyph_metrics_t boundingBox(glyph_t glyph) = 0;
221 virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
222 glyph_metrics_t tightBoundingBox(const QGlyphLayout &glyphs);
224 virtual QFixed ascent() const = 0;
225 virtual QFixed descent() const = 0;
226 virtual QFixed leading() const = 0;
227 virtual QFixed xHeight() const;
228 virtual QFixed averageCharWidth() const;
230 virtual QFixed lineThickness() const;
231 virtual QFixed underlinePosition() const;
233 virtual qreal maxCharWidth() const = 0;
234 virtual qreal minLeftBearing() const { return qreal(); }
235 virtual qreal minRightBearing() const { return qreal(); }
237 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
239 virtual const char *name() const = 0;
241 virtual bool canRender(const QChar *string, int len) = 0;
243 virtual Type type() const = 0;
245 virtual int glyphCount() const;
247 virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
249 HB_Font harfbuzzFont() const;
250 HB_Face harfbuzzFace() const;
252 virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
254 void setGlyphCache(void *key, QFontEngineGlyphCache *data);
255 QFontEngineGlyphCache *glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
257 static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
258 static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
260 static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
268 virtual void setDefaultHintStyle(HintStyle) { }
272 uint cache_cost; // amount of mem used in kb by the font
276 mutable HB_FontRec hbFont;
277 mutable HB_Face hbFace;
278 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_WS_QPA) || defined(Q_OS_SYMBIAN)
283 inline bool operator<(const KernPair &other) const
285 return left_right < other.left_right;
288 QVector<KernPair> kerning_pairs;
289 void loadKerningPairs(QFixed scalingFactor);
293 QImage currentlyLockedAlphaMap;
296 static const QVector<QRgb> &grayPalette();
297 QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
300 struct GlyphCacheEntry {
302 QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
303 bool operator==(const GlyphCacheEntry &other) const { return context == other.context && cache == other.cache; }
306 mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
309 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
311 return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
314 inline uint qHash(const QFontEngine::FaceId &f)
316 return qHash((f.index << 16) + f.encoding) + qHash(f.filename + f.uuid);
322 #if defined(Q_WS_QWS)
324 #ifndef QT_NO_QWS_QPF
326 class QFontEngineQPF1Data;
328 class QFontEngineQPF1 : public QFontEngine
331 QFontEngineQPF1(const QFontDef&, const QString &fn);
334 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
335 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
337 virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
338 virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
340 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
341 virtual glyph_metrics_t boundingBox(glyph_t glyph);
343 virtual QFixed ascent() const;
344 virtual QFixed descent() const;
345 virtual QFixed leading() const;
346 virtual qreal maxCharWidth() const;
347 virtual qreal minLeftBearing() const;
348 virtual qreal minRightBearing() const;
349 virtual QFixed underlinePosition() const;
350 virtual QFixed lineThickness() const;
352 virtual Type type() const;
354 virtual bool canRender(const QChar *string, int len);
355 inline const char *name() const { return 0; }
356 virtual QImage alphaMapForGlyph(glyph_t);
359 QFontEngineQPF1Data *d;
361 #endif // QT_NO_QWS_QPF
366 class QFontEngineBox : public QFontEngine
369 QFontEngineBox(int size);
372 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
373 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
375 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN)
376 void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
378 virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
380 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
381 virtual glyph_metrics_t boundingBox(glyph_t glyph);
383 virtual QFixed ascent() const;
384 virtual QFixed descent() const;
385 virtual QFixed leading() const;
386 virtual qreal maxCharWidth() const;
387 virtual qreal minLeftBearing() const { return 0; }
388 virtual qreal minRightBearing() const { return 0; }
389 virtual QImage alphaMapForGlyph(glyph_t);
394 virtual const char *name() const;
396 virtual bool canRender(const QChar *string, int len);
398 virtual Type type() const;
399 inline int size() const { return _size; }
402 friend class QFontPrivate;
406 class Q_GUI_EXPORT QFontEngineMulti : public QFontEngine
409 explicit QFontEngineMulti(int engineCount);
412 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
413 QTextEngine::ShaperFlags flags) const;
415 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
416 virtual glyph_metrics_t boundingBox(glyph_t glyph);
418 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
419 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
420 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
421 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
423 virtual QFixed ascent() const;
424 virtual QFixed descent() const;
425 virtual QFixed leading() const;
426 virtual QFixed xHeight() const;
427 virtual QFixed averageCharWidth() const;
428 virtual QImage alphaMapForGlyph(glyph_t);
429 virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
430 virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
431 virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
432 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
434 virtual QFixed lineThickness() const;
435 virtual QFixed underlinePosition() const;
436 virtual qreal maxCharWidth() const;
437 virtual qreal minLeftBearing() const;
438 virtual qreal minRightBearing() const;
440 virtual inline Type type() const
441 { return QFontEngine::Multi; }
443 virtual bool canRender(const QChar *string, int len);
444 inline virtual const char *name() const
447 QFontEngine *engine(int at) const
448 {Q_ASSERT(at < engines.size()); return engines.at(at); }
450 inline void ensureEngineAt(int at)
452 if (at >= engines.size() || engines.at(at) == 0)
457 friend class QPSPrintEnginePrivate;
458 friend class QPSPrintEngineFontMulti;
459 friend class QRawFont;
460 virtual void loadEngine(int at) = 0;
461 QVector<QFontEngine *> engines;
464 class QTestFontEngine : public QFontEngineBox
467 QTestFontEngine(int size) : QFontEngineBox(size) {}
468 virtual Type type() const { return TestFontEngine; }
474 # include "private/qfontengine_win_p.h"
477 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_FREETYPE)
478 # include "private/qfontengine_ft_p.h"
481 #endif // QFONTENGINE_P_H