1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
41 #ifndef QFONTENGINE_FT_P_H
42 #define QFONTENGINE_FT_P_H
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.
54 #include "private/qfontengine_p.h"
56 #ifndef QT_NO_FREETYPE
59 #include FT_FREETYPE_H
62 #include <private/qt_x11_p.h>
67 #ifndef QT_NO_FONTCONFIG
68 #include <fontconfig/fontconfig.h>
73 #include "private/qharfbuzz_copy_p.h"
77 class QFontEngineFTRawFont;
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).
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;
89 static QFreetypeFace *getFace(const QFontEngine::FaceId &face_id,
90 const QByteArray &fontData = QByteArray());
91 void release(const QFontEngine::FaceId &face_id);
93 // locks the struct for usage. Any read/write operations require locking.
105 #ifndef QT_NO_FONTCONFIG
111 FT_CharMap unicode_map;
112 FT_CharMap symbol_map;
114 enum { cmapCacheSize = 0x200 };
115 glyph_t cmapCache[cmapCacheSize];
119 HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
121 static void addGlyphToPath(FT_Face face, FT_GlyphSlot g, const QFixedPoint &point, QPainterPath *path, FT_Fixed x_scale, FT_Fixed y_scale);
122 static void addBitmapToPath(FT_GlyphSlot slot, const QFixedPoint &point, QPainterPath *path, bool = false);
125 friend class QFontEngineFT;
126 friend class QScopedPointerDeleter<QFreetypeFace>;
127 QFreetypeFace() : _lock(QMutex::Recursive) {}
134 class Q_GUI_EXPORT QFontEngineFT : public QFontEngine
138 /* we don't cache glyphs that are too large anyway, so we can make this struct rather small */
143 unsigned char height;
149 unsigned int uploadedToServer : 1;
152 enum SubpixelAntialiasingType {
160 #if defined(Q_WS_X11) && !defined(QT_NO_XRENDER)
161 typedef XGlyphInfo GlyphInfo;
164 unsigned short width;
165 unsigned short height;
173 struct GlyphAndSubPixelPosition
175 GlyphAndSubPixelPosition(glyph_t g, QFixed spp) : glyph(g), subPixelPosition(spp) {}
177 bool operator==(const GlyphAndSubPixelPosition &other) const
179 return glyph == other.glyph && subPixelPosition == other.subPixelPosition;
183 QFixed subPixelPosition;
190 FT_Matrix transformationMatrix;
191 unsigned long id; // server sided id, GlyphSet for X11
192 bool outline_drawing;
194 void removeGlyphFromCache(glyph_t index, QFixed subPixelPosition);
196 inline bool useFastGlyphData(glyph_t index, QFixed subPixelPosition) const {
197 return (index < 256 && subPixelPosition == 0);
199 inline Glyph *getGlyph(glyph_t index, QFixed subPixelPosition = 0) const
201 if (useFastGlyphData(index, subPixelPosition))
202 return fast_glyph_data[index];
203 return glyph_data.value(GlyphAndSubPixelPosition(index, subPixelPosition));
205 void setGlyph(glyph_t index, QFixed spp, Glyph *glyph);
208 mutable QHash<GlyphAndSubPixelPosition, Glyph *> glyph_data; // maps from glyph index to glyph data
209 mutable Glyph *fast_glyph_data[256]; // for fast lookup of glyphs < 256
210 mutable int fast_glyph_count;
213 virtual QFontEngine::FaceId faceId() const;
214 virtual QFontEngine::Properties properties() const;
215 virtual QFixed emSquareSize() const;
216 virtual bool supportsSubPixelPositions() const
218 return default_hint_style == HintLight ||
219 default_hint_style == HintNone;
222 virtual bool getSfntTableData(uint tag, uchar *buffer, uint *length) const;
223 virtual int synthesized() const;
225 virtual QFixed ascent() const;
226 virtual QFixed descent() const;
227 virtual QFixed leading() const;
228 virtual QFixed xHeight() const;
229 virtual QFixed averageCharWidth() const;
231 virtual qreal maxCharWidth() const;
232 virtual qreal minLeftBearing() const;
233 virtual qreal minRightBearing() const;
234 virtual QFixed lineThickness() const;
235 virtual QFixed underlinePosition() const;
237 void doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const;
239 inline virtual Type type() const
240 { return QFontEngine::Freetype; }
241 inline virtual const char *name() const
242 { return "freetype"; }
244 virtual void getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics);
246 virtual bool canRender(const QChar *string, int len);
248 virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
249 QPainterPath *path, QTextItem::RenderFlags flags);
250 virtual void addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
251 QPainterPath *path, QTextItem::RenderFlags flags);
253 virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs,
254 QTextEngine::ShaperFlags flags) const;
256 virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs);
257 virtual glyph_metrics_t boundingBox(glyph_t glyph);
258 virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix);
260 virtual void recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const;
261 virtual QImage alphaMapForGlyph(glyph_t g) { return alphaMapForGlyph(g, 0); }
262 virtual QImage alphaMapForGlyph(glyph_t, QFixed);
263 virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
264 virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph,
265 QFixed subPixelPosition,
266 const QTransform &matrix,
267 QFontEngine::GlyphFormat format);
268 virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
269 GlyphFormat neededFormat, const QTransform &t,
271 virtual bool hasInternalCaching() const { return true; }
272 virtual void unlockAlphaMapForGlyph();
274 virtual void removeGlyphFromCache(glyph_t glyph);
276 virtual int glyphCount() const;
282 FT_Face lockFace(Scaling scale = Scaled) const;
283 void unlockFace() const;
285 FT_Face non_locked_face() const;
287 inline bool drawAntialiased() const { return antialias; }
288 inline bool invalid() const { return xsize == 0 && ysize == 0; }
289 inline bool isBitmapFont() const { return defaultFormat == Format_Mono; }
291 inline Glyph *loadGlyph(uint glyph, QFixed subPixelPosition, GlyphFormat format = Format_None, bool fetchMetricsOnly = false) const
292 { return loadGlyph(&defaultGlyphSet, glyph, subPixelPosition, format, fetchMetricsOnly); }
293 Glyph *loadGlyph(QGlyphSet *set, uint glyph, QFixed subPixelPosition, GlyphFormat = Format_None, bool fetchMetricsOnly = false) const;
295 QGlyphSet *defaultGlyphs() { return &defaultGlyphSet; }
296 GlyphFormat defaultGlyphFormat() const { return defaultFormat; }
298 inline Glyph *cachedGlyph(glyph_t g) const { return defaultGlyphSet.getGlyph(g, 0); }
300 QGlyphSet *loadTransformedGlyphSet(const QTransform &matrix);
301 bool loadGlyphs(QGlyphSet *gs, const glyph_t *glyphs, int num_glyphs,
302 const QFixedPoint *positions,
303 GlyphFormat format = Format_Render);
305 #if defined(Q_WS_QWS) || defined(Q_OS_SYMBIAN)
306 virtual void draw(QPaintEngine * /*p*/, qreal /*x*/, qreal /*y*/, const QTextItemInt & /*si*/) {}
309 QFontEngineFT(const QFontDef &fd);
310 virtual ~QFontEngineFT();
312 bool init(FaceId faceId, bool antiaalias, GlyphFormat defaultFormat = Format_None,
313 const QByteArray &fontData = QByteArray());
314 bool init(FaceId faceId, bool antialias, GlyphFormat format,
315 QFreetypeFace *freetypeFace);
317 virtual HB_Error getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints);
320 virtual void setDefaultHintStyle(HintStyle style);
322 virtual QFontEngine *cloneWithSize(qreal pixelSize) const;
323 bool initFromFontEngine(const QFontEngineFT *fontEngine);
325 HintStyle defaultHintStyle() const { return default_hint_style; }
328 void freeGlyphSets();
330 virtual bool uploadGlyphToServer(QGlyphSet *set, uint glyphid, Glyph *g, GlyphInfo *info, int glyphDataSize) const;
331 virtual unsigned long allocateServerGlyphSet();
332 virtual void freeServerGlyphSet(unsigned long id);
334 QFreetypeFace *freetype;
335 int default_load_flags;
338 HintStyle default_hint_style;
344 SubpixelAntialiasingType subpixelType;
346 bool canUploadGlyphsToServer;
350 friend class QFontEngineFTRawFont;
352 int loadFlags(QGlyphSet *set, GlyphFormat format, int flags, bool &hsubpixel, int &vfactor) const;
354 GlyphFormat defaultFormat;
357 QList<QGlyphSet> transformedGlyphSets;
358 mutable QGlyphSet defaultGlyphSet;
360 QFontEngine::FaceId face_id;
365 mutable QFixed lbearing;
366 mutable QFixed rbearing;
367 QFixed line_thickness;
368 QFixed underline_position;
370 FT_Size_Metrics metrics;
371 mutable bool kerning_pairs_loaded;
374 inline uint qHash(const QFontEngineFT::GlyphAndSubPixelPosition &g)
376 return (g.glyph << 8) | (g.subPixelPosition * 10).round().toInt();
381 #endif // QT_NO_FREETYPE
383 #endif // QFONTENGINE_FT_P_H