1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
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"
65 #include <private/qfontengineglyphcache_p.h>
67 struct glyph_metrics_t;
68 typedef unsigned int glyph_t;
78 #define MAKE_TAG(ch1, ch2, ch3, ch4) (\
79 (((quint32)(ch1)) << 24) | \
80 (((quint32)(ch2)) << 16) | \
81 (((quint32)(ch3)) << 8) | \
86 class Q_GUI_EXPORT QFontEngine : public QObject
108 TestFontEngine = 0x1000
113 Format_Render = Format_None,
120 virtual ~QFontEngine();
122 // all of these are in unscaled metrics if the engine supports uncsaled metrics,
123 // otherwise in design metrics
125 QByteArray postscriptName;
126 QByteArray copyright;
136 virtual Properties properties() const;
137 virtual void getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics);
138 QByteArray getSfntTable(uint /*tag*/) const;
139 virtual bool getSfntTableData(uint /*tag*/, uchar * /*buffer*/, uint * /*length*/) const { return false; }
142 FaceId() : index(0), encoding(0) {}
148 virtual FaceId faceId() const { return FaceId(); }
149 enum SynthesizedFlags {
150 SynthesizedItalic = 0x1,
151 SynthesizedBold = 0x2,
152 SynthesizedStretch = 0x4
154 virtual int synthesized() const { return 0; }
155 virtual bool supportsSubPixelPositions() const { return false; }
156 virtual QFixed subPixelPositionForX(QFixed x) const;
158 virtual QFixed emSquareSize() const { return ascent(); }
160 /* returns 0 as glyph index for non existent glyphs */
161 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const = 0;
164 * This is a callback from harfbuzz. The font engine uses the font-system in use to find out the
165 * advances of each glyph and set it on the layout.
167 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
168 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
170 virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
171 QPainterPath *path, QTextItem::RenderFlags flags);
173 void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
174 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
176 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
177 void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
179 * Create a qimage with the alpha values for the glyph.
180 * Returns an image indexed_8 with index values ranging from 0=fully transparent to 255=opaque
182 // ### Refactor this into a smaller and more flexible API.
183 virtual QImage alphaMapForGlyph(glyph_t);
184 virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
185 virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
186 virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
187 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
188 virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
189 GlyphFormat neededFormat,
190 const QTransform &t = QTransform(),
192 virtual void unlockAlphaMapForGlyph();
193 virtual bool hasInternalCaching() const { return false; }
195 virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
197 return boundingBox(glyph, matrix);
200 virtual void removeGlyphFromCache(glyph_t);
202 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) = 0;
203 virtual glyph_metrics_t boundingBox(glyph_t glyph) = 0;
204 virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
205 glyph_metrics_t tightBoundingBox(const QGlyphLayout &glyphs);
207 virtual QFixed ascent() const = 0;
208 virtual QFixed descent() const = 0;
209 virtual QFixed leading() const = 0;
210 virtual QFixed xHeight() const;
211 virtual QFixed averageCharWidth() const;
213 virtual QFixed lineThickness() const;
214 virtual QFixed underlinePosition() const;
216 virtual qreal maxCharWidth() const = 0;
217 virtual qreal minLeftBearing() const { return qreal(); }
218 virtual qreal minRightBearing() const { return qreal(); }
220 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
222 virtual const char *name() const = 0;
224 virtual bool canRender(const QChar *string, int len) = 0;
225 inline bool canRender(uint ucs4) {
228 if (QChar::requiresSurrogates(ucs4)) {
229 utf16[0] = QChar::highSurrogate(ucs4);
230 utf16[1] = QChar::lowSurrogate(ucs4);
233 utf16[0] = QChar(ucs4);
235 return canRender(utf16, utf16len);
238 virtual Type type() const = 0;
240 virtual int glyphCount() const;
241 virtual int glyphMargin(QFontEngineGlyphCache::Type type) { return type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0; }
243 virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
245 HB_Font harfbuzzFont() const;
246 HB_Face harfbuzzFace() const;
247 HB_Face initializedHarfbuzzFace() const;
249 virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
251 void setGlyphCache(const void *key, QFontEngineGlyphCache *data);
252 QFontEngineGlyphCache *glyphCache(const void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
254 static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
255 static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
257 static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
265 virtual void setDefaultHintStyle(HintStyle) { }
269 uint cache_cost; // amount of mem used in kb by the font
273 mutable HB_FontRec hbFont;
274 mutable HB_Face hbFace;
279 inline bool operator<(const KernPair &other) const
281 return left_right < other.left_right;
284 QVector<KernPair> kerning_pairs;
285 void loadKerningPairs(QFixed scalingFactor);
288 QImage currentlyLockedAlphaMap;
289 int m_subPixelPositionCount; // Number of positions within a single pixel for this cache
292 QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
295 struct GlyphCacheEntry {
297 QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
298 bool operator==(const GlyphCacheEntry &other) const { return context == other.context && cache == other.cache; }
301 mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
304 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
306 return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
309 inline uint qHash(const QFontEngine::FaceId &f)
311 return qHash((f.index << 16) + f.encoding) + qHash(f.filename + f.uuid);
319 class QFontEngineBox : public QFontEngine
322 QFontEngineBox(int size);
325 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
326 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
328 void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
329 virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
331 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
332 virtual glyph_metrics_t boundingBox(glyph_t glyph);
333 virtual QFontEngine *cloneWithSize(qreal pixelSize) const;
335 virtual QFixed ascent() const;
336 virtual QFixed descent() const;
337 virtual QFixed leading() const;
338 virtual qreal maxCharWidth() const;
339 virtual qreal minLeftBearing() const { return 0; }
340 virtual qreal minRightBearing() const { return 0; }
341 virtual QImage alphaMapForGlyph(glyph_t);
343 virtual const char *name() const;
345 virtual bool canRender(const QChar *string, int len);
347 virtual Type type() const;
348 inline int size() const { return _size; }
351 friend class QFontPrivate;
355 class Q_GUI_EXPORT QFontEngineMulti : public QFontEngine
359 explicit QFontEngineMulti(int engineCount);
362 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
363 QTextEngine::ShaperFlags flags) const;
365 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
366 virtual glyph_metrics_t boundingBox(glyph_t glyph);
368 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
369 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
370 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
371 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
373 virtual QFixed ascent() const;
374 virtual QFixed descent() const;
375 virtual QFixed leading() const;
376 virtual QFixed xHeight() const;
377 virtual QFixed averageCharWidth() const;
378 virtual QImage alphaMapForGlyph(glyph_t);
379 virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
380 virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
381 virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
382 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
384 virtual QFixed lineThickness() const;
385 virtual QFixed underlinePosition() const;
386 virtual qreal maxCharWidth() const;
387 virtual qreal minLeftBearing() const;
388 virtual qreal minRightBearing() const;
390 virtual inline Type type() const
391 { return QFontEngine::Multi; }
393 virtual bool canRender(const QChar *string, int len);
394 inline virtual const char *name() const
397 QFontEngine *engine(int at) const
398 {Q_ASSERT(at < engines.size()); return engines.at(at); }
400 inline void ensureEngineAt(int at)
402 if (at >= engines.size() || engines.at(at) == 0)
406 virtual bool shouldLoadFontEngineForCharacter(int at, uint ucs4) const;
407 virtual void setFallbackFamiliesList(const QStringList &) {}
410 friend class QPSPrintEnginePrivate;
411 friend class QPSPrintEngineFontMulti;
412 friend class QRawFont;
413 virtual void loadEngine(int at) = 0;
414 virtual void ensureFallbackFamiliesQueried() {}
415 QVector<QFontEngine *> engines;
418 class QTestFontEngine : public QFontEngineBox
421 QTestFontEngine(int size) : QFontEngineBox(size) {}
422 virtual Type type() const { return TestFontEngine; }
429 #endif // QFONTENGINE_P_H