Compile with clang when C++11 support is enabled
[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         // X11 types
95         XLFD,
96
97         // MS Windows types
98         Win,
99
100         // Apple Mac OS types
101         Mac,
102
103         // QWS types
104         Freetype,
105         QPF1,
106         QPF2,
107         Proxy,
108
109         DirectWrite,
110
111         TestFontEngine = 0x1000
112     };
113
114     enum GlyphFormat {
115         Format_None,
116         Format_Render = Format_None,
117         Format_Mono,
118         Format_A8,
119         Format_A32
120     };
121
122     QFontEngine();
123     virtual ~QFontEngine();
124
125     // all of these are in unscaled metrics if the engine supports uncsaled metrics,
126     // otherwise in design metrics
127     struct Properties {
128         QByteArray postscriptName;
129         QByteArray copyright;
130         QRectF boundingBox;
131         QFixed emSquare;
132         QFixed ascent;
133         QFixed descent;
134         QFixed leading;
135         QFixed italicAngle;
136         QFixed capHeight;
137         QFixed lineWidth;
138     };
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; }
143
144     struct FaceId {
145         FaceId() : index(0), encoding(0) {}
146         QByteArray filename;
147         QByteArray uuid;
148         int index;
149         int encoding;
150     };
151     virtual FaceId faceId() const { return FaceId(); }
152     enum SynthesizedFlags {
153         SynthesizedItalic = 0x1,
154         SynthesizedBold = 0x2,
155         SynthesizedStretch = 0x4
156     };
157     virtual int synthesized() const { return 0; }
158     virtual bool supportsSubPixelPositions() const { return false; }
159     virtual QFixed subPixelPositionForX(QFixed x) const;
160
161     virtual QFixed emSquareSize() const { return ascent(); }
162
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;
165
166     /**
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.
169      */
170     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
171     virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
172
173     virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
174                                  QPainterPath *path, QTextItem::RenderFlags flags);
175
176     void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
177                            QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
178
179     virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
180     void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
181     /**
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
184      */
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(),
194                                            QPoint *offset = 0);
195     virtual void unlockAlphaMapForGlyph();
196     virtual bool hasInternalCaching() const { return false; }
197
198     virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
199     {
200         return boundingBox(glyph, matrix);
201     }
202
203     virtual void removeGlyphFromCache(glyph_t);
204
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);
209
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;
215
216     virtual QFixed lineThickness() const;
217     virtual QFixed underlinePosition() const;
218
219     virtual qreal maxCharWidth() const = 0;
220     virtual qreal minLeftBearing() const { return qreal(); }
221     virtual qreal minRightBearing() const { return qreal(); }
222
223     virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
224
225     virtual const char *name() const = 0;
226
227     virtual bool canRender(const QChar *string, int len) = 0;
228
229     virtual Type type() const = 0;
230
231     virtual int glyphCount() const;
232     virtual int glyphMargin(QFontEngineGlyphCache::Type type) { return type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0; }
233
234     virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
235
236     HB_Font harfbuzzFont() const;
237     HB_Face harfbuzzFace() const;
238     HB_Face initializedHarfbuzzFace() const;
239
240     virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
241
242     void setGlyphCache(const void *key, QFontEngineGlyphCache *data);
243     QFontEngineGlyphCache *glyphCache(const void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
244
245     static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
246     static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
247
248     static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
249
250     enum HintStyle {
251         HintNone,
252         HintLight,
253         HintMedium,
254         HintFull
255     };
256     virtual void setDefaultHintStyle(HintStyle) { }
257
258     QAtomicInt ref;
259     QFontDef fontDef;
260     uint cache_cost; // amount of mem used in kb by the font
261     int cache_count;
262     uint fsType : 16;
263     bool symbol;
264     mutable HB_FontRec hbFont;
265     mutable HB_Face hbFace;
266     struct KernPair {
267         uint left_right;
268         QFixed adjust;
269
270         inline bool operator<(const KernPair &other) const
271         {
272             return left_right < other.left_right;
273         }
274     };
275     QVector<KernPair> kerning_pairs;
276     void loadKerningPairs(QFixed scalingFactor);
277
278     int glyphFormat;
279     QImage currentlyLockedAlphaMap;
280     int m_subPixelPositionCount; // Number of positions within a single pixel for this cache
281
282 protected:
283     QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
284
285 private:
286     struct GlyphCacheEntry {
287         const void *context;
288         QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
289         bool operator==(const GlyphCacheEntry &other) const { return context == other.context && cache == other.cache; }
290     };
291
292     mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
293 };
294
295 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
296 {
297     return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
298 }
299
300 inline uint qHash(const QFontEngine::FaceId &f)
301 {
302     return qHash((f.index << 16) + f.encoding) + qHash(f.filename + f.uuid);
303 }
304
305
306 class QGlyph;
307
308
309
310 class QFontEngineBox : public QFontEngine
311 {
312 public:
313     QFontEngineBox(int size);
314     ~QFontEngineBox();
315
316     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
317     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
318
319     void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
320     virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
321
322     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
323     virtual glyph_metrics_t boundingBox(glyph_t glyph);
324     virtual QFontEngine *cloneWithSize(qreal pixelSize) const;
325
326     virtual QFixed ascent() const;
327     virtual QFixed descent() const;
328     virtual QFixed leading() const;
329     virtual qreal maxCharWidth() const;
330     virtual qreal minLeftBearing() const { return 0; }
331     virtual qreal minRightBearing() const { return 0; }
332     virtual QImage alphaMapForGlyph(glyph_t);
333
334     virtual const char *name() const;
335
336     virtual bool canRender(const QChar *string, int len);
337
338     virtual Type type() const;
339     inline int size() const { return _size; }
340
341 private:
342     friend class QFontPrivate;
343     int _size;
344 };
345
346 class Q_GUI_EXPORT QFontEngineMulti : public QFontEngine
347 {
348     Q_OBJECT
349 public:
350     explicit QFontEngineMulti(int engineCount);
351     ~QFontEngineMulti();
352
353     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
354                       QTextEngine::ShaperFlags flags) const;
355
356     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
357     virtual glyph_metrics_t boundingBox(glyph_t glyph);
358
359     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
360     virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
361     virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
362     virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
363
364     virtual QFixed ascent() const;
365     virtual QFixed descent() const;
366     virtual QFixed leading() const;
367     virtual QFixed xHeight() const;
368     virtual QFixed averageCharWidth() const;
369     virtual QImage alphaMapForGlyph(glyph_t);
370     virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
371     virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
372     virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
373     virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
374
375     virtual QFixed lineThickness() const;
376     virtual QFixed underlinePosition() const;
377     virtual qreal maxCharWidth() const;
378     virtual qreal minLeftBearing() const;
379     virtual qreal minRightBearing() const;
380
381     virtual inline Type type() const
382     { return QFontEngine::Multi; }
383
384     virtual bool canRender(const QChar *string, int len);
385     inline virtual const char *name() const
386     { return "Multi"; }
387
388     QFontEngine *engine(int at) const
389     {Q_ASSERT(at < engines.size()); return engines.at(at); }
390
391     inline void ensureEngineAt(int at)
392     {
393         if (at >= engines.size() || engines.at(at) == 0)
394             loadEngine(at);
395     }
396
397     virtual bool shouldLoadFontEngineForCharacter(int at, uint ucs4) const;
398     virtual void setFallbackFamiliesList(const QStringList &) {}
399
400 protected:
401     friend class QPSPrintEnginePrivate;
402     friend class QPSPrintEngineFontMulti;
403     friend class QRawFont;
404     virtual void loadEngine(int at) = 0;
405     virtual void ensureFallbackFamiliesQueried() {}
406     QVector<QFontEngine *> engines;
407 };
408
409 class QTestFontEngine : public QFontEngineBox
410 {
411 public:
412     QTestFontEngine(int size) : QFontEngineBox(size) {}
413     virtual Type type() const { return TestFontEngine; }
414 };
415
416 QT_END_NAMESPACE
417
418
419
420 #endif // QFONTENGINE_P_H