1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QFONTENGINE_CORETEXT_P_H
41 #define QFONTENGINE_CORETEXT_P_H
42 
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 #include <private/qcore_mac_p.h>
56 #include <QtCore/qloggingcategory.h>
57 
58 #ifdef Q_OS_MACOS
59 #include <ApplicationServices/ApplicationServices.h>
60 #else
61 #include <CoreText/CoreText.h>
62 #include <CoreGraphics/CoreGraphics.h>
63 #endif
64 
65 QT_BEGIN_NAMESPACE
66 
Q_DECLARE_LOGGING_CATEGORY(lcQpaFonts)67 Q_DECLARE_LOGGING_CATEGORY(lcQpaFonts)
68 
69 class QCoreTextFontEngine : public QFontEngine
70 {
71     Q_GADGET
72 
73 public:
74     QCoreTextFontEngine(CTFontRef font, const QFontDef &def);
75     QCoreTextFontEngine(CGFontRef font, const QFontDef &def);
76     ~QCoreTextFontEngine();
77 
78     glyph_t glyphIndex(uint ucs4) const override;
79     bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, ShaperFlags flags) const override;
80     void recalcAdvances(QGlyphLayout *, ShaperFlags) const override;
81 
82     glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) override;
83     glyph_metrics_t boundingBox(glyph_t glyph) override;
84 
85     QFixed ascent() const override;
86     QFixed capHeight() const override;
87     QFixed descent() const override;
88     QFixed leading() const override;
89     QFixed xHeight() const override;
90     qreal maxCharWidth() const override;
91     QFixed averageCharWidth() const override;
92 
93     void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int numGlyphs,
94                          QPainterPath *path, QTextItem::RenderFlags) override;
95 
96     bool canRender(const QChar *string, int len) const override;
97 
98     int synthesized() const override { return synthesisFlags; }
99     bool supportsSubPixelPositions() const override { return true; }
100 
101     QFixed lineThickness() const override;
102     QFixed underlinePosition() const override;
103 
104     void draw(CGContextRef ctx, qreal x, qreal y, const QTextItemInt &ti, int paintDeviceHeight);
105 
106     FaceId faceId() const override;
107     bool getSfntTableData(uint /*tag*/, uchar * /*buffer*/, uint * /*length*/) const override;
108     void getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics) override;
109     QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition) override;
110     QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t) override;
111     QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t) override;
112     glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed, const QTransform &matrix, GlyphFormat) override;
113     QImage bitmapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t, const QColor &color) override;
114     QFixed emSquareSize() const override;
115     void doKerning(QGlyphLayout *g, ShaperFlags flags) const override;
116 
117     bool supportsTransformation(const QTransform &transform) const override;
118     bool expectsGammaCorrectedBlending() const override;
119 
120     QFontEngine *cloneWithSize(qreal pixelSize) const override;
121     Qt::HANDLE handle() const override;
122     int glyphMargin(QFontEngine::GlyphFormat format) override { Q_UNUSED(format); return 0; }
123 
124     QFontEngine::Properties properties() const override;
125 
126     enum FontSmoothing { Disabled, Subpixel, Grayscale };
127     Q_ENUM(FontSmoothing);
128 
129     static FontSmoothing fontSmoothing();
130     static qreal fontSmoothingGamma();
131 
132     static bool ct_getSfntTable(void *user_data, uint tag, uchar *buffer, uint *length);
133     static QFont::Weight qtWeightFromCFWeight(float value);
134 
135     static QCoreTextFontEngine *create(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference);
136 
137 protected:
138     QCoreTextFontEngine(const QFontDef &def);
139     void init();
140     QImage imageForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &m, const QColor &color = QColor());
141     void loadAdvancesForGlyphs(QVarLengthArray<CGGlyph> &cgGlyphs, QGlyphLayout *glyphs) const;
142     bool hasColorGlyphs() const;
143     bool shouldAntialias() const;
144     bool shouldSmoothFont() const;
145 
146     QCFType<CTFontRef> ctfont;
147     QCFType<CGFontRef> cgFont;
148     int synthesisFlags;
149     CGAffineTransform transform;
150     QFixed avgCharWidth;
151     QFixed underlineThickness;
152     QFixed underlinePos;
153     QFontEngine::FaceId face_id;
154     mutable bool kerningPairsLoaded;
155 };
156 
157 CGAffineTransform qt_transform_from_fontdef(const QFontDef &fontDef);
158 
159 QT_END_NAMESPACE
160 
161 #endif // QFONTENGINE_CORETEXT_P_H
162