Get started with patching up the Qt GUI docs
[profile/ivi/qtbase.git] / src / gui / text / qfontengine_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QFONTENGINE_P_H
43 #define QFONTENGINE_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
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"
62
63
64
65 #include <private/qfontengineglyphcache_p.h>
66
67 struct glyph_metrics_t;
68 typedef unsigned int glyph_t;
69
70 QT_BEGIN_NAMESPACE
71
72 class QChar;
73 class QPainterPath;
74
75 class QTextEngine;
76 struct QGlyphLayout;
77
78 #define MAKE_TAG(ch1, ch2, ch3, ch4) (\
79     (((quint32)(ch1)) << 24) | \
80     (((quint32)(ch2)) << 16) | \
81     (((quint32)(ch3)) << 8) | \
82     ((quint32)(ch4)) \
83    )
84
85
86 class Q_GUI_EXPORT QFontEngine : public QObject
87 {
88     Q_OBJECT
89 public:
90     enum Type {
91         Box,
92         Multi,
93
94         // MS Windows types
95         Win,
96
97         // Apple Mac OS types
98         Mac,
99
100         // QWS types
101         Freetype,
102         QPF1,
103         QPF2,
104         Proxy,
105
106         DirectWrite,
107
108         TestFontEngine = 0x1000
109     };
110
111     enum GlyphFormat {
112         Format_None,
113         Format_Render = Format_None,
114         Format_Mono,
115         Format_A8,
116         Format_A32
117     };
118
119     QFontEngine();
120     virtual ~QFontEngine();
121
122     // all of these are in unscaled metrics if the engine supports uncsaled metrics,
123     // otherwise in design metrics
124     struct Properties {
125         QByteArray postscriptName;
126         QByteArray copyright;
127         QRectF boundingBox;
128         QFixed emSquare;
129         QFixed ascent;
130         QFixed descent;
131         QFixed leading;
132         QFixed italicAngle;
133         QFixed capHeight;
134         QFixed lineWidth;
135     };
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; }
140
141     struct FaceId {
142         FaceId() : index(0), encoding(0) {}
143         QByteArray filename;
144         QByteArray uuid;
145         int index;
146         int encoding;
147     };
148     virtual FaceId faceId() const { return FaceId(); }
149     enum SynthesizedFlags {
150         SynthesizedItalic = 0x1,
151         SynthesizedBold = 0x2,
152         SynthesizedStretch = 0x4
153     };
154     virtual int synthesized() const { return 0; }
155     virtual bool supportsSubPixelPositions() const { return false; }
156     virtual QFixed subPixelPositionForX(QFixed x) const;
157
158     virtual QFixed emSquareSize() const { return ascent(); }
159
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;
162
163     /**
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.
166      */
167     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
168     virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
169
170     virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
171                                  QPainterPath *path, QTextItem::RenderFlags flags);
172
173     void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
174                            QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
175
176     virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
177     void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
178     /**
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
181      */
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(),
191                                            QPoint *offset = 0);
192     virtual void unlockAlphaMapForGlyph();
193     virtual bool hasInternalCaching() const { return false; }
194
195     virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
196     {
197         return boundingBox(glyph, matrix);
198     }
199
200     virtual void removeGlyphFromCache(glyph_t);
201
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);
206
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;
212
213     virtual QFixed lineThickness() const;
214     virtual QFixed underlinePosition() const;
215
216     virtual qreal maxCharWidth() const = 0;
217     virtual qreal minLeftBearing() const { return qreal(); }
218     virtual qreal minRightBearing() const { return qreal(); }
219
220     virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
221
222     virtual const char *name() const = 0;
223
224     virtual bool canRender(const QChar *string, int len) = 0;
225     inline bool canRender(uint ucs4) {
226         QChar utf16[2];
227         int utf16len = 1;
228         if (QChar::requiresSurrogates(ucs4)) {
229             utf16[0] = QChar::highSurrogate(ucs4);
230             utf16[1] = QChar::lowSurrogate(ucs4);
231             ++utf16len;
232         } else {
233             utf16[0] = QChar(ucs4);
234         }
235         return canRender(utf16, utf16len);
236     }
237
238     virtual Type type() const = 0;
239
240     virtual int glyphCount() const;
241     virtual int glyphMargin(QFontEngineGlyphCache::Type type) { return type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0; }
242
243     virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
244
245     HB_Font harfbuzzFont() const;
246     HB_Face harfbuzzFace() const;
247     HB_Face initializedHarfbuzzFace() const;
248
249     virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
250
251     void setGlyphCache(const void *key, QFontEngineGlyphCache *data);
252     QFontEngineGlyphCache *glyphCache(const void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
253
254     static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
255     static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
256
257     static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
258
259     enum HintStyle {
260         HintNone,
261         HintLight,
262         HintMedium,
263         HintFull
264     };
265     virtual void setDefaultHintStyle(HintStyle) { }
266
267     QAtomicInt ref;
268     QFontDef fontDef;
269     uint cache_cost; // amount of mem used in kb by the font
270     int cache_count;
271     uint fsType : 16;
272     bool symbol;
273     mutable HB_FontRec hbFont;
274     mutable HB_Face hbFace;
275     struct KernPair {
276         uint left_right;
277         QFixed adjust;
278
279         inline bool operator<(const KernPair &other) const
280         {
281             return left_right < other.left_right;
282         }
283     };
284     QVector<KernPair> kerning_pairs;
285     void loadKerningPairs(QFixed scalingFactor);
286
287     int glyphFormat;
288     QImage currentlyLockedAlphaMap;
289     int m_subPixelPositionCount; // Number of positions within a single pixel for this cache
290
291 protected:
292     QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
293
294 private:
295     struct GlyphCacheEntry {
296         const void *context;
297         QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
298         bool operator==(const GlyphCacheEntry &other) const { return context == other.context && cache == other.cache; }
299     };
300
301     mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
302 };
303
304 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
305 {
306     return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
307 }
308
309 inline uint qHash(const QFontEngine::FaceId &f)
310 {
311     return qHash((f.index << 16) + f.encoding) + qHash(f.filename + f.uuid);
312 }
313
314
315 class QGlyph;
316
317
318
319 class QFontEngineBox : public QFontEngine
320 {
321 public:
322     QFontEngineBox(int size);
323     ~QFontEngineBox();
324
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;
327
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);
330
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;
334
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);
342
343     virtual const char *name() const;
344
345     virtual bool canRender(const QChar *string, int len);
346
347     virtual Type type() const;
348     inline int size() const { return _size; }
349
350 private:
351     friend class QFontPrivate;
352     int _size;
353 };
354
355 class Q_GUI_EXPORT QFontEngineMulti : public QFontEngine
356 {
357     Q_OBJECT
358 public:
359     explicit QFontEngineMulti(int engineCount);
360     ~QFontEngineMulti();
361
362     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
363                       QTextEngine::ShaperFlags flags) const;
364
365     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
366     virtual glyph_metrics_t boundingBox(glyph_t glyph);
367
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);
372
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);
383
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;
389
390     virtual inline Type type() const
391     { return QFontEngine::Multi; }
392
393     virtual bool canRender(const QChar *string, int len);
394     inline virtual const char *name() const
395     { return "Multi"; }
396
397     QFontEngine *engine(int at) const
398     {Q_ASSERT(at < engines.size()); return engines.at(at); }
399
400     inline void ensureEngineAt(int at)
401     {
402         if (at >= engines.size() || engines.at(at) == 0)
403             loadEngine(at);
404     }
405
406     virtual bool shouldLoadFontEngineForCharacter(int at, uint ucs4) const;
407     virtual void setFallbackFamiliesList(const QStringList &) {}
408
409 protected:
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;
416 };
417
418 class QTestFontEngine : public QFontEngineBox
419 {
420 public:
421     QTestFontEngine(int size) : QFontEngineBox(size) {}
422     virtual Type type() const { return TestFontEngine; }
423 };
424
425 QT_END_NAMESPACE
426
427
428
429 #endif // QFONTENGINE_P_H