44464ee7880d2a7265df6678d6ecee2cdd765cb7
[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
239     virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
240
241     void setGlyphCache(void *key, QFontEngineGlyphCache *data);
242     QFontEngineGlyphCache *glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
243
244     static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
245     static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
246
247     static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
248
249     enum HintStyle {
250         HintNone,
251         HintLight,
252         HintMedium,
253         HintFull
254     };
255     virtual void setDefaultHintStyle(HintStyle) { }
256
257     QAtomicInt ref;
258     QFontDef fontDef;
259     uint cache_cost; // amount of mem used in kb by the font
260     int cache_count;
261     uint fsType : 16;
262     bool symbol;
263     mutable HB_FontRec hbFont;
264     mutable HB_Face hbFace;
265     struct KernPair {
266         uint left_right;
267         QFixed adjust;
268
269         inline bool operator<(const KernPair &other) const
270         {
271             return left_right < other.left_right;
272         }
273     };
274     QVector<KernPair> kerning_pairs;
275     void loadKerningPairs(QFixed scalingFactor);
276
277     int glyphFormat;
278     QImage currentlyLockedAlphaMap;
279     int m_subPixelPositionCount; // Number of positions within a single pixel for this cache
280
281 protected:
282     QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
283
284 private:
285     struct GlyphCacheEntry {
286         void *context;
287         QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
288         bool operator==(const GlyphCacheEntry &other) const { return context == other.context && cache == other.cache; }
289     };
290
291     mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
292 };
293
294 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
295 {
296     return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
297 }
298
299 inline uint qHash(const QFontEngine::FaceId &f)
300 {
301     return qHash((f.index << 16) + f.encoding) + qHash(f.filename + f.uuid);
302 }
303
304
305 class QGlyph;
306
307
308
309 class QFontEngineBox : public QFontEngine
310 {
311 public:
312     QFontEngineBox(int size);
313     ~QFontEngineBox();
314
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;
317
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);
320
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;
324
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);
332
333     virtual const char *name() const;
334
335     virtual bool canRender(const QChar *string, int len);
336
337     virtual Type type() const;
338     inline int size() const { return _size; }
339
340 private:
341     friend class QFontPrivate;
342     int _size;
343 };
344
345 class Q_GUI_EXPORT QFontEngineMulti : public QFontEngine
346 {
347     Q_OBJECT
348 public:
349     explicit QFontEngineMulti(int engineCount);
350     ~QFontEngineMulti();
351
352     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
353                       QTextEngine::ShaperFlags flags) const;
354
355     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
356     virtual glyph_metrics_t boundingBox(glyph_t glyph);
357
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);
362
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);
373
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;
379
380     virtual inline Type type() const
381     { return QFontEngine::Multi; }
382
383     virtual bool canRender(const QChar *string, int len);
384     inline virtual const char *name() const
385     { return "Multi"; }
386
387     QFontEngine *engine(int at) const
388     {Q_ASSERT(at < engines.size()); return engines.at(at); }
389
390     inline void ensureEngineAt(int at)
391     {
392         if (at >= engines.size() || engines.at(at) == 0)
393             loadEngine(at);
394     }
395
396     virtual bool shouldLoadFontEngineForCharacter(int at, uint ucs4) const;
397
398 protected:
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;
405 };
406
407 class QTestFontEngine : public QFontEngineBox
408 {
409 public:
410     QTestFontEngine(int size) : QFontEngineBox(size) {}
411     virtual Type type() const { return TestFontEngine; }
412 };
413
414 QT_END_NAMESPACE
415
416
417
418 #endif // QFONTENGINE_P_H