DirectWrite font engine: don't leak the font table buffer
[profile/ivi/qtbase.git] / src / gui / text / qfontengine_ft_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 #ifndef QFONTENGINE_FT_P_H
42 #define QFONTENGINE_FT_P_H
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53
54 #include "private/qfontengine_p.h"
55
56 #ifndef QT_NO_FREETYPE
57
58 #include <ft2build.h>
59 #include FT_FREETYPE_H
60
61
62 #ifndef Q_OS_WIN
63 #include <unistd.h>
64 #endif
65
66 #ifndef QT_NO_FONTCONFIG
67 #include <fontconfig/fontconfig.h>
68 #endif
69
70 #include <qmutex.h>
71
72 #include "private/qharfbuzz_copy_p.h"
73
74 QT_BEGIN_NAMESPACE
75
76 class QFontEngineFTRawFont;
77 class QFontconfigDatabase;
78
79 /*
80  * This struct represents one font file on disk (like Arial.ttf) and is shared between all the font engines
81  * that show this font file (at different pixel sizes).
82  */
83 struct QFreetypeFace
84 {
85     void computeSize(const QFontDef &fontDef, int *xsize, int *ysize, bool *outline_drawing);
86     QFontEngine::Properties properties() const;
87     bool getSfntTable(uint tag, uchar *buffer, uint *length) const;
88
89     static QFreetypeFace *getFace(const QFontEngine::FaceId &face_id,
90                                   const QByteArray &fontData = QByteArray());
91     void release(const QFontEngine::FaceId &face_id);
92
93     // locks the struct for usage. Any read/write operations require locking.
94     void lock()
95     {
96         _lock.lock();
97     }
98     void unlock()
99     {
100         _lock.unlock();
101     }
102
103     FT_Face face;
104     HB_Face hbFace;
105     int xsize; // 26.6
106     int ysize; // 26.6
107     FT_Matrix matrix;
108     FT_CharMap unicode_map;
109     FT_CharMap symbol_map;
110
111     enum { cmapCacheSize = 0x200 };
112     glyph_t cmapCache[cmapCacheSize];
113
114     int fsType() const;
115
116     HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
117
118     static void addGlyphToPath(FT_Face face, FT_GlyphSlot g, const QFixedPoint &point, QPainterPath *path, FT_Fixed x_scale, FT_Fixed y_scale);
119     static void addBitmapToPath(FT_GlyphSlot slot, const QFixedPoint &point, QPainterPath *path, bool = false);
120
121 private:
122     friend class QFontEngineFT;
123     friend struct QScopedPointerDeleter<QFreetypeFace>;
124     QFreetypeFace() : _lock(QMutex::Recursive) {}
125     ~QFreetypeFace() {}
126     QAtomicInt ref;
127     QMutex _lock;
128     QByteArray fontData;
129 };
130
131 // If this is exported this breaks compilation of the windows
132 // plugin as qfontengine_ft_p.h/.cpp are also compiled there
133 #ifndef Q_OS_WIN
134 class Q_GUI_EXPORT QFontEngineFT : public QFontEngine
135 #else
136 class QFontEngineFT : public QFontEngine
137 #endif
138 {
139 public:
140
141     /* we don't cache glyphs that are too large anyway, so we can make this struct rather small */
142     struct Glyph {
143         ~Glyph();
144         short linearAdvance;
145         unsigned char width;
146         unsigned char height;
147         signed char x;
148         signed char y;
149         signed char advance;
150         signed char format;
151         uchar *data;
152     };
153
154     enum SubpixelAntialiasingType {
155         Subpixel_None,
156         Subpixel_RGB,
157         Subpixel_BGR,
158         Subpixel_VRGB,
159         Subpixel_VBGR
160     };
161
162     struct GlyphInfo {
163         unsigned short  width;
164         unsigned short  height;
165         short           x;
166         short           y;
167         short           xOff;
168         short           yOff;
169     };
170
171     struct GlyphAndSubPixelPosition
172     {
173         GlyphAndSubPixelPosition(glyph_t g, QFixed spp) : glyph(g), subPixelPosition(spp) {}
174
175         bool operator==(const GlyphAndSubPixelPosition &other) const
176         {
177             return glyph == other.glyph && subPixelPosition == other.subPixelPosition;
178         }
179
180         glyph_t glyph;
181         QFixed subPixelPosition;
182     };
183
184     struct QGlyphSet
185     {
186         QGlyphSet();
187         ~QGlyphSet();
188         FT_Matrix transformationMatrix;
189         bool outline_drawing;
190
191         void removeGlyphFromCache(glyph_t index, QFixed subPixelPosition);
192         void clear();
193         inline bool useFastGlyphData(glyph_t index, QFixed subPixelPosition) const {
194             return (index < 256 && subPixelPosition == 0);
195         }
196         inline Glyph *getGlyph(glyph_t index, QFixed subPixelPosition = 0) const;
197         void setGlyph(glyph_t index, QFixed spp, Glyph *glyph);
198
199 private:
200         mutable QHash<GlyphAndSubPixelPosition, Glyph *> glyph_data; // maps from glyph index to glyph data
201         mutable Glyph *fast_glyph_data[256]; // for fast lookup of glyphs < 256
202         mutable int fast_glyph_count;
203     };
204
205     virtual QFontEngine::FaceId faceId() const;
206     virtual QFontEngine::Properties properties() const;
207     virtual QFixed emSquareSize() const;
208     virtual bool supportsSubPixelPositions() const
209     {
210         return default_hint_style == HintLight ||
211                default_hint_style == HintNone;
212     }
213
214     virtual bool getSfntTableData(uint tag, uchar *buffer, uint *length) const;
215     virtual int synthesized() const;
216
217     virtual QFixed ascent() const;
218     virtual QFixed descent() const;
219     virtual QFixed leading() const;
220     virtual QFixed xHeight() const;
221     virtual QFixed averageCharWidth() const;
222
223     virtual qreal maxCharWidth() const;
224     virtual qreal minLeftBearing() const;
225     virtual qreal minRightBearing() const;
226     virtual QFixed lineThickness() const;
227     virtual QFixed underlinePosition() const;
228
229     void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
230
231     inline virtual Type type() const
232     { return QFontEngine::Freetype; }
233     inline virtual const char *name() const
234     { return "freetype"; }
235
236     virtual void getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics);
237
238     virtual bool canRender(const QChar *string, int len);
239
240     virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
241                          QPainterPath *path, QTextItem::RenderFlags flags);
242     virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
243                           QPainterPath *path, QTextItem::RenderFlags flags);
244
245     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
246                       QTextEngine::ShaperFlags flags) const;
247
248     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
249     virtual glyph_metrics_t boundingBox(glyph_t glyph);
250     virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
251
252     virtual void recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const;
253     virtual QImage alphaMapForGlyph(glyph_t g) { return alphaMapForGlyph(g, 0); }
254     virtual QImage alphaMapForGlyph(glyph_t, QFixed);
255     virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
256     virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph,
257                                                 QFixed subPixelPosition,
258                                                 const QTransform &matrix,
259                                                 QFontEngine::GlyphFormat format);
260     virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
261                                            GlyphFormat neededFormat, const QTransform &t,
262                                            QPoint *offset);
263     virtual bool hasInternalCaching() const { return cacheEnabled; }
264     virtual void unlockAlphaMapForGlyph();
265
266     virtual void removeGlyphFromCache(glyph_t glyph);
267     virtual int glyphMargin(QFontEngineGlyphCache::Type /* type */) { return 0; }
268
269     virtual int glyphCount() const;
270
271     enum Scaling {
272         Scaled,
273         Unscaled
274     };
275     FT_Face lockFace(Scaling scale = Scaled) const;
276     void unlockFace() const;
277
278     FT_Face non_locked_face() const;
279
280     inline bool drawAntialiased() const { return antialias; }
281     inline bool invalid() const { return xsize == 0 && ysize == 0; }
282     inline bool isBitmapFont() const { return defaultFormat == Format_Mono; }
283
284     inline Glyph *loadGlyph(uint glyph, QFixed subPixelPosition, GlyphFormat format = Format_None, bool fetchMetricsOnly = false) const
285     { return loadGlyph(cacheEnabled ? &defaultGlyphSet : 0, glyph, subPixelPosition, format, fetchMetricsOnly); }
286     Glyph *loadGlyph(QGlyphSet *set, uint glyph, QFixed subPixelPosition, GlyphFormat = Format_None, bool fetchMetricsOnly = false) const;
287     Glyph *loadGlyphFor(glyph_t g, QFixed subPixelPosition, GlyphFormat format);
288
289     QGlyphSet *loadTransformedGlyphSet(const QTransform &matrix);
290     bool loadGlyphs(QGlyphSet *gs, const glyph_t *glyphs, int num_glyphs,
291                     const QFixedPoint *positions,
292                     GlyphFormat format = Format_Render);
293
294     QFontEngineFT(const QFontDef &fd);
295     virtual ~QFontEngineFT();
296
297     bool init(FaceId faceId, bool antiaalias, GlyphFormat defaultFormat = Format_None,
298               const QByteArray &fontData = QByteArray());
299     bool init(FaceId faceId, bool antialias, GlyphFormat format,
300               QFreetypeFace *freetypeFace);
301
302     virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
303
304
305     virtual void setDefaultHintStyle(HintStyle style);
306
307     virtual QFontEngine *cloneWithSize(qreal pixelSize) const;
308     bool initFromFontEngine(const QFontEngineFT *fontEngine);
309
310     HintStyle defaultHintStyle() const { return default_hint_style; }
311 protected:
312
313     QFreetypeFace *freetype;
314     int default_load_flags;
315     HintStyle default_hint_style;
316     bool antialias;
317     bool transform;
318     bool embolden;
319     bool obliquen;
320     SubpixelAntialiasingType subpixelType;
321     int lcdFilterType;
322     bool embeddedbitmap;
323     bool cacheEnabled;
324
325 private:
326     friend class QFontEngineFTRawFont;
327     friend class QFontconfigDatabase;
328     friend class QFontEngineMultiFontConfig;
329
330     int loadFlags(QGlyphSet *set, GlyphFormat format, int flags, bool &hsubpixel, int &vfactor) const;
331
332     GlyphFormat defaultFormat;
333     FT_Matrix matrix;
334
335     QList<QGlyphSet> transformedGlyphSets;
336     mutable QGlyphSet defaultGlyphSet;
337
338     QFontEngine::FaceId face_id;
339
340     int xsize;
341     int ysize;
342
343     mutable QFixed lbearing;
344     mutable QFixed rbearing;
345     QFixed line_thickness;
346     QFixed underline_position;
347
348     FT_Size_Metrics metrics;
349     mutable bool kerning_pairs_loaded;
350 };
351
352 inline uint qHash(const QFontEngineFT::GlyphAndSubPixelPosition &g)
353 {
354     return (g.glyph << 8)  | (g.subPixelPosition * 10).round().toInt();
355 }
356
357 inline QFontEngineFT::Glyph *QFontEngineFT::QGlyphSet::getGlyph(glyph_t index, QFixed subPixelPosition) const
358 {
359     if (useFastGlyphData(index, subPixelPosition))
360         return fast_glyph_data[index];
361     return glyph_data.value(GlyphAndSubPixelPosition(index, subPixelPosition));
362 }
363
364 extern FT_Library qt_getFreetype();
365
366 QT_END_NAMESPACE
367
368 #endif // QT_NO_FREETYPE
369
370 #endif // QFONTENGINE_FT_P_H