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
100 // Apple Mac OS types
111 TestFontEngine = 0x1000
116 Format_Render = Format_None,
123 virtual ~QFontEngine();
125 // all of these are in unscaled metrics if the engine supports uncsaled metrics,
126 // otherwise in design metrics
128 QByteArray postscriptName;
129 QByteArray copyright;
139 virtual Properties properties() const;
140 virtual void getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics);
141 QByteArray getSfntTable(uint /*tag*/) const;
142 virtual bool getSfntTableData(uint /*tag*/, uchar * /*buffer*/, uint * /*length*/) const { return false; }
145 FaceId() : index(0), encoding(0) {}
151 virtual FaceId faceId() const { return FaceId(); }
152 enum SynthesizedFlags {
153 SynthesizedItalic = 0x1,
154 SynthesizedBold = 0x2,
155 SynthesizedStretch = 0x4
157 virtual int synthesized() const { return 0; }
158 virtual bool supportsSubPixelPositions() const { return false; }
159 virtual QFixed subPixelPositionForX(QFixed x) const;
161 virtual QFixed emSquareSize() const { return ascent(); }
163 /* returns 0 as glyph index for non existent glyphs */
164 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const = 0;
167 * This is a callback from harfbuzz. The font engine uses the font-system in use to find out the
168 * advances of each glyph and set it on the layout.
170 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
171 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
173 virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
174 QPainterPath *path, QTextItem::RenderFlags flags);
176 void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
177 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
179 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
180 void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
182 * Create a qimage with the alpha values for the glyph.
183 * Returns an image indexed_8 with index values ranging from 0=fully transparent to 255=opaque
185 // ### Refactor this into a smaller and more flexible API.
186 virtual QImage alphaMapForGlyph(glyph_t);
187 virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
188 virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
189 virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
190 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
191 virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
192 GlyphFormat neededFormat,
193 const QTransform &t = QTransform(),
195 virtual void unlockAlphaMapForGlyph();
196 virtual bool hasInternalCaching() const { return false; }
198 virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
200 return boundingBox(glyph, matrix);
203 virtual void removeGlyphFromCache(glyph_t);
205 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) = 0;
206 virtual glyph_metrics_t boundingBox(glyph_t glyph) = 0;
207 virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
208 glyph_metrics_t tightBoundingBox(const QGlyphLayout &glyphs);
210 virtual QFixed ascent() const = 0;
211 virtual QFixed descent() const = 0;
212 virtual QFixed leading() const = 0;
213 virtual QFixed xHeight() const;
214 virtual QFixed averageCharWidth() const;
216 virtual QFixed lineThickness() const;
217 virtual QFixed underlinePosition() const;
219 virtual qreal maxCharWidth() const = 0;
220 virtual qreal minLeftBearing() const { return qreal(); }
221 virtual qreal minRightBearing() const { return qreal(); }
223 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
225 virtual const char *name() const = 0;
227 virtual bool canRender(const QChar *string, int len) = 0;
229 virtual Type type() const = 0;
231 virtual int glyphCount() const;
232 virtual int glyphMargin(QFontEngineGlyphCache::Type type) { return type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0; }
234 virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
236 HB_Font harfbuzzFont() const;
237 HB_Face harfbuzzFace() const;
239 virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
241 void setGlyphCache(void *key, QFontEngineGlyphCache *data);
242 QFontEngineGlyphCache *glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
244 static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
245 static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
247 static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
255 virtual void setDefaultHintStyle(HintStyle) { }
259 uint cache_cost; // amount of mem used in kb by the font
263 mutable HB_FontRec hbFont;
264 mutable HB_Face hbFace;
269 inline bool operator<(const KernPair &other) const
271 return left_right < other.left_right;
274 QVector<KernPair> kerning_pairs;
275 void loadKerningPairs(QFixed scalingFactor);
278 QImage currentlyLockedAlphaMap;
279 int m_subPixelPositionCount; // Number of positions within a single pixel for this cache
282 QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
285 struct GlyphCacheEntry {
287 QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
288 bool operator==(const GlyphCacheEntry &other) const { return context == other.context && cache == other.cache; }
291 mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
294 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
296 return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
299 inline uint qHash(const QFontEngine::FaceId &f)
301 return qHash((f.index << 16) + f.encoding) + qHash(f.filename + f.uuid);
309 class QFontEngineBox : public QFontEngine
312 QFontEngineBox(int size);
315 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
316 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
318 void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
319 virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
321 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
322 virtual glyph_metrics_t boundingBox(glyph_t glyph);
323 virtual QFontEngine *cloneWithSize(qreal pixelSize) const;
325 virtual QFixed ascent() const;
326 virtual QFixed descent() const;
327 virtual QFixed leading() const;
328 virtual qreal maxCharWidth() const;
329 virtual qreal minLeftBearing() const { return 0; }
330 virtual qreal minRightBearing() const { return 0; }
331 virtual QImage alphaMapForGlyph(glyph_t);
333 virtual const char *name() const;
335 virtual bool canRender(const QChar *string, int len);
337 virtual Type type() const;
338 inline int size() const { return _size; }
341 friend class QFontPrivate;
345 class Q_GUI_EXPORT QFontEngineMulti : public QFontEngine
349 explicit QFontEngineMulti(int engineCount);
352 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
353 QTextEngine::ShaperFlags flags) const;
355 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
356 virtual glyph_metrics_t boundingBox(glyph_t glyph);
358 virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
359 virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
360 virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
361 virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
363 virtual QFixed ascent() const;
364 virtual QFixed descent() const;
365 virtual QFixed leading() const;
366 virtual QFixed xHeight() const;
367 virtual QFixed averageCharWidth() const;
368 virtual QImage alphaMapForGlyph(glyph_t);
369 virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
370 virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
371 virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
372 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
374 virtual QFixed lineThickness() const;
375 virtual QFixed underlinePosition() const;
376 virtual qreal maxCharWidth() const;
377 virtual qreal minLeftBearing() const;
378 virtual qreal minRightBearing() const;
380 virtual inline Type type() const
381 { return QFontEngine::Multi; }
383 virtual bool canRender(const QChar *string, int len);
384 inline virtual const char *name() const
387 QFontEngine *engine(int at) const
388 {Q_ASSERT(at < engines.size()); return engines.at(at); }
390 inline void ensureEngineAt(int at)
392 if (at >= engines.size() || engines.at(at) == 0)
396 virtual bool shouldLoadFontEngineForCharacter(int at, uint ucs4) const;
399 friend class QPSPrintEnginePrivate;
400 friend class QPSPrintEngineFontMulti;
401 friend class QRawFont;
402 virtual void loadEngine(int at) = 0;
403 virtual void unloadEngine(int at);
404 QVector<QFontEngine *> engines;
407 class QTestFontEngine : public QFontEngineBox
410 QTestFontEngine(int size) : QFontEngineBox(size) {}
411 virtual Type type() const { return TestFontEngine; }
418 #endif // QFONTENGINE_P_H