Export QTextImageHandler and add accessor for image
[profile/ivi/qtbase.git] / src / gui / text / qfontengine_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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 #ifdef Q_WS_WIN
64 #   include "QtCore/qt_windows.h"
65 #endif
66
67 #ifdef Q_WS_MAC
68 #   include "private/qt_mac_p.h"
69 #   include "QtCore/qmap.h"
70 #   include "QtCore/qcache.h"
71 #   include "private/qcore_mac_p.h"
72 #endif
73
74 #include <private/qfontengineglyphcache_p.h>
75
76 struct glyph_metrics_t;
77 typedef unsigned int glyph_t;
78
79 QT_BEGIN_NAMESPACE
80
81 class QChar;
82 class QPainterPath;
83
84 class QTextEngine;
85 struct QGlyphLayout;
86
87 #define MAKE_TAG(ch1, ch2, ch3, ch4) (\
88     (((quint32)(ch1)) << 24) | \
89     (((quint32)(ch2)) << 16) | \
90     (((quint32)(ch3)) << 8) | \
91     ((quint32)(ch4)) \
92    )
93
94
95 class Q_GUI_EXPORT QFontEngine : public QObject
96 {
97 public:
98     enum Type {
99         Box,
100         Multi,
101
102         // X11 types
103         XLFD,
104
105         // MS Windows types
106         Win,
107
108         // Apple Mac OS types
109         Mac,
110
111         // QWS types
112         Freetype,
113         QPF1,
114         QPF2,
115         Proxy,
116
117         // S60 types
118         S60FontEngine, // Cannot be simply called "S60". Reason is qt_s60Data.h
119
120         DirectWrite,
121
122         TestFontEngine = 0x1000
123     };
124
125     enum GlyphFormat {
126         Format_None,
127         Format_Render = Format_None,
128         Format_Mono,
129         Format_A8,
130         Format_A32
131     };
132
133     QFontEngine();
134     virtual ~QFontEngine();
135
136     // all of these are in unscaled metrics if the engine supports uncsaled metrics,
137     // otherwise in design metrics
138     struct Properties {
139         QByteArray postscriptName;
140         QByteArray copyright;
141         QRectF boundingBox;
142         QFixed emSquare;
143         QFixed ascent;
144         QFixed descent;
145         QFixed leading;
146         QFixed italicAngle;
147         QFixed capHeight;
148         QFixed lineWidth;
149     };
150     virtual Properties properties() const;
151     virtual void getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics);
152     QByteArray getSfntTable(uint /*tag*/) const;
153     virtual bool getSfntTableData(uint /*tag*/, uchar * /*buffer*/, uint * /*length*/) const { return false; }
154
155     struct FaceId {
156         FaceId() : index(0), encoding(0) {}
157         QByteArray filename;
158         QByteArray uuid;
159         int index;
160         int encoding;
161     };
162     virtual FaceId faceId() const { return FaceId(); }
163     enum SynthesizedFlags {
164         SynthesizedItalic = 0x1,
165         SynthesizedBold = 0x2,
166         SynthesizedStretch = 0x4
167     };
168     virtual int synthesized() const { return 0; }
169     virtual bool supportsSubPixelPositions() const { return false; }
170     QFixed subPixelPositionForX(QFixed x);
171
172     virtual QFixed emSquareSize() const { return ascent(); }
173
174     /* returns 0 as glyph index for non existent glyphs */
175     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const = 0;
176
177     /**
178      * This is a callback from harfbuzz. The font engine uses the font-system in use to find out the
179      * advances of each glyph and set it on the layout.
180      */
181     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
182     virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
183
184 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN) && !defined(Q_WS_QPA)
185     virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si) = 0;
186 #endif
187     virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
188                                  QPainterPath *path, QTextItem::RenderFlags flags);
189
190     void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
191                            QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
192
193     virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
194     void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
195     /**
196      * Create a qimage with the alpha values for the glyph.
197      * Returns an image indexed_8 with index values ranging from 0=fully transparent to 255=opaque
198      */
199     // ### Refactor this into a smaller and more flexible API.
200     virtual QImage alphaMapForGlyph(glyph_t);
201     virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
202     virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
203     virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
204     virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
205     virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
206                                            GlyphFormat neededFormat,
207                                            const QTransform &t = QTransform(),
208                                            QPoint *offset = 0);
209     virtual void unlockAlphaMapForGlyph();
210     virtual bool hasInternalCaching() const { return false; }
211
212     virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
213     {
214         return boundingBox(glyph, matrix);
215     }
216
217     virtual void removeGlyphFromCache(glyph_t);
218
219     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) = 0;
220     virtual glyph_metrics_t boundingBox(glyph_t glyph) = 0;
221     virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
222     glyph_metrics_t tightBoundingBox(const QGlyphLayout &glyphs);
223
224     virtual QFixed ascent() const = 0;
225     virtual QFixed descent() const = 0;
226     virtual QFixed leading() const = 0;
227     virtual QFixed xHeight() const;
228     virtual QFixed averageCharWidth() const;
229
230     virtual QFixed lineThickness() const;
231     virtual QFixed underlinePosition() const;
232
233     virtual qreal maxCharWidth() const = 0;
234     virtual qreal minLeftBearing() const { return qreal(); }
235     virtual qreal minRightBearing() const { return qreal(); }
236
237     virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
238
239     virtual const char *name() const = 0;
240
241     virtual bool canRender(const QChar *string, int len) = 0;
242
243     virtual Type type() const = 0;
244
245     virtual int glyphCount() const;
246
247     virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
248
249     HB_Font harfbuzzFont() const;
250     HB_Face harfbuzzFace() const;
251
252     virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
253
254     void setGlyphCache(void *key, QFontEngineGlyphCache *data);
255     QFontEngineGlyphCache *glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
256
257     static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
258     static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
259
260     static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
261
262     enum HintStyle {
263         HintNone,
264         HintLight,
265         HintMedium,
266         HintFull
267     };
268     virtual void setDefaultHintStyle(HintStyle) { }
269
270     QAtomicInt ref;
271     QFontDef fontDef;
272     uint cache_cost; // amount of mem used in kb by the font
273     int cache_count;
274     uint fsType : 16;
275     bool symbol;
276     mutable HB_FontRec hbFont;
277     mutable HB_Face hbFace;
278 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_WS_QPA) || defined(Q_OS_SYMBIAN)
279     struct KernPair {
280         uint left_right;
281         QFixed adjust;
282
283         inline bool operator<(const KernPair &other) const
284         {
285             return left_right < other.left_right;
286         }
287     };
288     QVector<KernPair> kerning_pairs;
289     void loadKerningPairs(QFixed scalingFactor);
290 #endif
291
292     int glyphFormat;
293     QImage currentlyLockedAlphaMap;
294
295 protected:
296     static const QVector<QRgb> &grayPalette();
297     QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
298
299 private:
300     struct GlyphCacheEntry {
301         void *context;
302         QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
303         bool operator==(const GlyphCacheEntry &other) const { return context == other.context && cache == other.cache; }
304     };
305
306     mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
307 };
308
309 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
310 {
311     return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
312 }
313
314 inline uint qHash(const QFontEngine::FaceId &f)
315 {
316     return qHash((f.index << 16) + f.encoding) + qHash(f.filename + f.uuid);
317 }
318
319
320 class QGlyph;
321
322 #if defined(Q_WS_QWS)
323
324 #ifndef QT_NO_QWS_QPF
325
326 class QFontEngineQPF1Data;
327
328 class QFontEngineQPF1 : public QFontEngine
329 {
330 public:
331     QFontEngineQPF1(const QFontDef&, const QString &fn);
332    ~QFontEngineQPF1();
333
334     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
335     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
336
337     virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
338     virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
339
340     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
341     virtual glyph_metrics_t boundingBox(glyph_t glyph);
342
343     virtual QFixed ascent() const;
344     virtual QFixed descent() const;
345     virtual QFixed leading() const;
346     virtual qreal maxCharWidth() const;
347     virtual qreal minLeftBearing() const;
348     virtual qreal minRightBearing() const;
349     virtual QFixed underlinePosition() const;
350     virtual QFixed lineThickness() const;
351
352     virtual Type type() const;
353
354     virtual bool canRender(const QChar *string, int len);
355     inline const char *name() const { return 0; }
356     virtual QImage alphaMapForGlyph(glyph_t);
357
358
359     QFontEngineQPF1Data *d;
360 };
361 #endif // QT_NO_QWS_QPF
362
363 #endif // QWS
364
365
366 class QFontEngineBox : public QFontEngine
367 {
368 public:
369     QFontEngineBox(int size);
370     ~QFontEngineBox();
371
372     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
373     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
374
375 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN)
376     void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
377 #endif
378     virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
379
380     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
381     virtual glyph_metrics_t boundingBox(glyph_t glyph);
382
383     virtual QFixed ascent() const;
384     virtual QFixed descent() const;
385     virtual QFixed leading() const;
386     virtual qreal maxCharWidth() const;
387     virtual qreal minLeftBearing() const { return 0; }
388     virtual qreal minRightBearing() const { return 0; }
389     virtual QImage alphaMapForGlyph(glyph_t);
390
391 #ifdef Q_WS_X11
392     int cmap() const;
393 #endif
394     virtual const char *name() const;
395
396     virtual bool canRender(const QChar *string, int len);
397
398     virtual Type type() const;
399     inline int size() const { return _size; }
400
401 private:
402     friend class QFontPrivate;
403     int _size;
404 };
405
406 class Q_GUI_EXPORT QFontEngineMulti : public QFontEngine
407 {
408 public:
409     explicit QFontEngineMulti(int engineCount);
410     ~QFontEngineMulti();
411
412     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
413                       QTextEngine::ShaperFlags flags) const;
414
415     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
416     virtual glyph_metrics_t boundingBox(glyph_t glyph);
417
418     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
419     virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
420     virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
421     virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
422
423     virtual QFixed ascent() const;
424     virtual QFixed descent() const;
425     virtual QFixed leading() const;
426     virtual QFixed xHeight() const;
427     virtual QFixed averageCharWidth() const;
428     virtual QImage alphaMapForGlyph(glyph_t);
429     virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
430     virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
431     virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
432     virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
433
434     virtual QFixed lineThickness() const;
435     virtual QFixed underlinePosition() const;
436     virtual qreal maxCharWidth() const;
437     virtual qreal minLeftBearing() const;
438     virtual qreal minRightBearing() const;
439
440     virtual inline Type type() const
441     { return QFontEngine::Multi; }
442
443     virtual bool canRender(const QChar *string, int len);
444     inline virtual const char *name() const
445     { return "Multi"; }
446
447     QFontEngine *engine(int at) const
448     {Q_ASSERT(at < engines.size()); return engines.at(at); }
449
450     inline void ensureEngineAt(int at)
451     {
452         if (at >= engines.size() || engines.at(at) == 0)
453             loadEngine(at);
454     }
455
456 protected:
457     friend class QPSPrintEnginePrivate;
458     friend class QPSPrintEngineFontMulti;
459     friend class QRawFont;
460     virtual void loadEngine(int at) = 0;
461     QVector<QFontEngine *> engines;
462 };
463
464 class QTestFontEngine : public QFontEngineBox
465 {
466 public:
467     QTestFontEngine(int size) : QFontEngineBox(size) {}
468     virtual Type type() const { return TestFontEngine; }
469 };
470
471 QT_END_NAMESPACE
472
473 #ifdef Q_WS_WIN
474 #   include "private/qfontengine_win_p.h"
475 #endif
476
477 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_FREETYPE)
478 #   include "private/qfontengine_ft_p.h"
479 #endif
480
481 #endif // QFONTENGINE_P_H