Fix uses of qRound on non-floating-point types.
[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         int index;
159         int encoding;
160     };
161     virtual FaceId faceId() const { return FaceId(); }
162     enum SynthesizedFlags {
163         SynthesizedItalic = 0x1,
164         SynthesizedBold = 0x2,
165         SynthesizedStretch = 0x4
166     };
167     virtual int synthesized() const { return 0; }
168     virtual bool supportsSubPixelPositions() const { return false; }
169
170     virtual QFixed emSquareSize() const { return ascent(); }
171
172     /* returns 0 as glyph index for non existent glyphs */
173     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const = 0;
174
175     /**
176      * This is a callback from harfbuzz. The font engine uses the font-system in use to find out the
177      * advances of each glyph and set it on the layout.
178      */
179     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const {}
180     virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
181
182 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN) && !defined(Q_WS_QPA)
183     virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si) = 0;
184 #endif
185     virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
186                                  QPainterPath *path, QTextItem::RenderFlags flags);
187
188     void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
189                            QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions);
190
191     virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
192     void addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags);
193     /**
194      * Create a qimage with the alpha values for the glyph.
195      * Returns an image indexed_8 with index values ranging from 0=fully transparent to 255=opaque
196      */
197     virtual QImage alphaMapForGlyph(glyph_t);
198     virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
199     virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
200     virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
201     virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
202
203     virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
204     {
205         return boundingBox(glyph, matrix);
206     }
207
208     virtual void removeGlyphFromCache(glyph_t);
209
210     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) = 0;
211     virtual glyph_metrics_t boundingBox(glyph_t glyph) = 0;
212     virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
213     glyph_metrics_t tightBoundingBox(const QGlyphLayout &glyphs);
214
215     virtual QFixed ascent() const = 0;
216     virtual QFixed descent() const = 0;
217     virtual QFixed leading() const = 0;
218     virtual QFixed xHeight() const;
219     virtual QFixed averageCharWidth() const;
220
221     virtual QFixed lineThickness() const;
222     virtual QFixed underlinePosition() const;
223
224     virtual qreal maxCharWidth() const = 0;
225     virtual qreal minLeftBearing() const { return qreal(); }
226     virtual qreal minRightBearing() const { return qreal(); }
227
228     virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
229
230     virtual const char *name() const = 0;
231
232     virtual bool canRender(const QChar *string, int len) = 0;
233
234     virtual Type type() const = 0;
235
236     virtual int glyphCount() const;
237
238     virtual QFontEngine *cloneWithSize(qreal /*pixelSize*/) const { return 0; }
239
240     HB_Font harfbuzzFont() const;
241     HB_Face harfbuzzFace() const;
242
243     virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
244
245     void setGlyphCache(void *key, QFontEngineGlyphCache *data);
246     QFontEngineGlyphCache *glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const;
247
248     static const uchar *getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize);
249     static quint32 getTrueTypeGlyphIndex(const uchar *cmap, uint unicode);
250
251     static QByteArray convertToPostscriptFontFamilyName(const QByteArray &fontFamily);
252
253     enum HintStyle {
254         HintNone,
255         HintLight,
256         HintMedium,
257         HintFull
258     };
259     virtual void setDefaultHintStyle(HintStyle) { }
260
261     QAtomicInt ref;
262     QFontDef fontDef;
263     uint cache_cost; // amount of mem used in kb by the font
264     int cache_count;
265     uint fsType : 16;
266     bool symbol;
267     mutable HB_FontRec hbFont;
268     mutable HB_Face hbFace;
269 #if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_WS_QPA) || defined(Q_OS_SYMBIAN)
270     struct KernPair {
271         uint left_right;
272         QFixed adjust;
273
274         inline bool operator<(const KernPair &other) const
275         {
276             return left_right < other.left_right;
277         }
278     };
279     QVector<KernPair> kerning_pairs;
280     void loadKerningPairs(QFixed scalingFactor);
281 #endif
282
283     int glyphFormat;
284
285 protected:
286     static const QVector<QRgb> &grayPalette();
287     QFixed lastRightBearing(const QGlyphLayout &glyphs, bool round = false);
288
289 private:
290     struct GlyphCacheEntry {
291         void *context;
292         QExplicitlySharedDataPointer<QFontEngineGlyphCache> cache;
293         bool operator==(const GlyphCacheEntry &other) { return context == other.context && cache == other.cache; }
294     };
295
296     mutable QLinkedList<GlyphCacheEntry> m_glyphCaches;
297 };
298
299 inline bool operator ==(const QFontEngine::FaceId &f1, const QFontEngine::FaceId &f2)
300 {
301     return (f1.index == f2.index) && (f1.encoding == f2.encoding) && (f1.filename == f2.filename);
302 }
303
304 inline uint qHash(const QFontEngine::FaceId &f)
305 {
306     return qHash((f.index << 16) + f.encoding) + qHash(f.filename);
307 }
308
309
310 class QGlyph;
311
312 #if defined(Q_WS_QWS)
313
314 #ifndef QT_NO_QWS_QPF
315
316 class QFontEngineQPF1Data;
317
318 class QFontEngineQPF1 : public QFontEngine
319 {
320 public:
321     QFontEngineQPF1(const QFontDef&, const QString &fn);
322    ~QFontEngineQPF1();
323
324     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
325     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
326
327     virtual void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
328     virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
329
330     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
331     virtual glyph_metrics_t boundingBox(glyph_t glyph);
332
333     virtual QFixed ascent() const;
334     virtual QFixed descent() const;
335     virtual QFixed leading() const;
336     virtual qreal maxCharWidth() const;
337     virtual qreal minLeftBearing() const;
338     virtual qreal minRightBearing() const;
339     virtual QFixed underlinePosition() const;
340     virtual QFixed lineThickness() const;
341
342     virtual Type type() const;
343
344     virtual bool canRender(const QChar *string, int len);
345     inline const char *name() const { return 0; }
346     virtual QImage alphaMapForGlyph(glyph_t);
347
348
349     QFontEngineQPF1Data *d;
350 };
351 #endif // QT_NO_QWS_QPF
352
353 #endif // QWS
354
355
356 class QFontEngineBox : public QFontEngine
357 {
358 public:
359     QFontEngineBox(int size);
360     ~QFontEngineBox();
361
362     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const;
363     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
364
365 #if !defined(Q_WS_X11) && !defined(Q_WS_WIN) && !defined(Q_WS_MAC) && !defined(Q_OS_SYMBIAN)
366     void draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &si);
367 #endif
368     virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags);
369
370     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
371     virtual glyph_metrics_t boundingBox(glyph_t glyph);
372
373     virtual QFixed ascent() const;
374     virtual QFixed descent() const;
375     virtual QFixed leading() const;
376     virtual qreal maxCharWidth() const;
377     virtual qreal minLeftBearing() const { return 0; }
378     virtual qreal minRightBearing() const { return 0; }
379     virtual QImage alphaMapForGlyph(glyph_t);
380
381 #ifdef Q_WS_X11
382     int cmap() const;
383 #endif
384     virtual const char *name() const;
385
386     virtual bool canRender(const QChar *string, int len);
387
388     virtual Type type() const;
389     inline int size() const { return _size; }
390
391 private:
392     friend class QFontPrivate;
393     int _size;
394 };
395
396 class QFontEngineMulti : public QFontEngine
397 {
398 public:
399     explicit QFontEngineMulti(int engineCount);
400     ~QFontEngineMulti();
401
402     virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
403                       QTextEngine::ShaperFlags flags) const;
404
405     virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
406     virtual glyph_metrics_t boundingBox(glyph_t glyph);
407
408     virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const;
409     virtual void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
410     virtual void addOutlineToPath(qreal, qreal, const QGlyphLayout &, QPainterPath *, QTextItem::RenderFlags flags);
411     virtual void getGlyphBearings(glyph_t glyph, qreal *leftBearing = 0, qreal *rightBearing = 0);
412
413     virtual QFixed ascent() const;
414     virtual QFixed descent() const;
415     virtual QFixed leading() const;
416     virtual QFixed xHeight() const;
417     virtual QFixed averageCharWidth() const;
418     virtual QImage alphaMapForGlyph(glyph_t);
419
420     virtual QFixed lineThickness() const;
421     virtual QFixed underlinePosition() const;
422     virtual qreal maxCharWidth() const;
423     virtual qreal minLeftBearing() const;
424     virtual qreal minRightBearing() const;
425
426     virtual inline Type type() const
427     { return QFontEngine::Multi; }
428
429     virtual bool canRender(const QChar *string, int len);
430     inline virtual const char *name() const
431     { return "Multi"; }
432
433     QFontEngine *engine(int at) const
434     {Q_ASSERT(at < engines.size()); return engines.at(at); }
435
436
437 protected:
438     friend class QPSPrintEnginePrivate;
439     friend class QPSPrintEngineFontMulti;
440     friend class QRawFont;
441     virtual void loadEngine(int at) = 0;
442     QVector<QFontEngine *> engines;
443 };
444
445 class QTestFontEngine : public QFontEngineBox
446 {
447 public:
448     QTestFontEngine(int size) : QFontEngineBox(size) {}
449     virtual Type type() const { return TestFontEngine; }
450 };
451
452 QT_END_NAMESPACE
453
454 #ifdef Q_WS_WIN
455 #   include "private/qfontengine_win_p.h"
456 #endif
457
458 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_FREETYPE)
459 #   include "private/qfontengine_ft_p.h"
460 #endif
461
462 #endif // QFONTENGINE_P_H