• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006-2012 The Android Open Source Project
3  * Copyright 2012 Mozilla Foundation
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #ifndef SKFONTHOST_FREETYPE_COMMON_H_
10 #define SKFONTHOST_FREETYPE_COMMON_H_
11 
12 #include "include/core/SkTypeface.h"
13 #include "include/core/SkTypes.h"
14 #include "src/core/SkGlyph.h"
15 #include "src/core/SkScalerContext.h"
16 #include "src/core/SkSharedMutex.h"
17 #include "src/utils/SkCharToGlyphCache.h"
18 
19 #include "include/core/SkFontMgr.h"
20 
21 // These are forward declared to avoid pimpl but also hide the FreeType implementation.
22 typedef struct FT_LibraryRec_* FT_Library;
23 typedef struct FT_FaceRec_* FT_Face;
24 typedef struct FT_StreamRec_* FT_Stream;
25 typedef signed long FT_Pos;
26 typedef struct FT_BBox_ FT_BBox;
27 
28 
29 #ifdef SK_DEBUG
30 const char* SkTraceFtrGetError(int);
31 #define SK_TRACEFTR(ERR, MSG, ...) \
32     SkDebugf("%s:%d:1: error: 0x%x '%s' " MSG "\n", __FILE__, __LINE__, ERR, \
33             SkTraceFtrGetError((int)(ERR)), __VA_ARGS__)
34 #else
35 #define SK_TRACEFTR(ERR, ...) do { sk_ignore_unused_variable(ERR); } while (false)
36 #endif
37 
38 
39 class SkScalerContext_FreeType_Base : public SkScalerContext {
40 protected:
41     // See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
42     // This value was chosen by eyeballing the result in Firefox and trying to match it.
43     static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
44 
SkScalerContext_FreeType_Base(sk_sp<SkTypeface> typeface,const SkScalerContextEffects & effects,const SkDescriptor * desc)45     SkScalerContext_FreeType_Base(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
46                                   const SkDescriptor *desc)
47         : INHERITED(std::move(typeface), effects, desc)
48     {}
49 
50     void generateGlyphImage(FT_Face face, const SkGlyph& glyph, const SkMatrix& bitmapTransform);
51     bool generateGlyphPath(FT_Face face, SkPath* path);
52     bool generateFacePath(FT_Face face, SkGlyphID glyphID, SkPath* path);
53 
54     // Computes a bounding box for a COLRv1 glyph id in FT_BBox 26.6 format and FreeType's y-up
55     // coordinate space.
56     // Needed to call into COLRv1 from generateMetrics().
57     //
58     // Note : This method may change the configured size and transforms on FT_Face. Make sure to
59     // configure size, matrix and load glyphs as needed after using this function to restore the
60     // state of FT_Face.
61     bool computeColrV1GlyphBoundingBox(FT_Face face, SkGlyphID glyphID, FT_BBox* boundingBox);
62 
63 private:
64     using INHERITED = SkScalerContext;
65 };
66 
67 class SK_API SkTypeface_FreeType : public SkTypeface {
68 public:
69     /** For SkFontMgrs to make use of our ability to extract
70      *  name and style from a stream, using FreeType's API.
71      */
72     class Scanner : ::SkNoncopyable {
73     public:
74         Scanner();
75         ~Scanner();
76         struct AxisDefinition {
77             SkFourByteTag fTag;
78             SkFixed fMinimum;
79             SkFixed fDefault;
80             SkFixed fMaximum;
81         };
82         using AxisDefinitions = SkSTArray<4, AxisDefinition, true>;
83         bool recognizedFont(SkStreamAsset* stream, int* numFonts) const;
84         bool scanFont(SkStreamAsset* stream, int ttcIndex,
85                       SkString* name, SkFontStyle* style, bool* isFixedPitch,
86                       AxisDefinitions* axes) const;
87         static void computeAxisValues(
88             AxisDefinitions axisDefinitions,
89             const SkFontArguments::VariationPosition position,
90             SkFixed* axisValues,
91             const SkString& name,
92             const SkFontArguments::VariationPosition::Coordinate* currentPosition = nullptr);
93         static bool GetAxes(FT_Face face, AxisDefinitions* axes);
94 
95     private:
96         FT_Face openFace(SkStreamAsset* stream, int ttcIndex, FT_Stream ftStream) const;
97         FT_Library fLibrary;
98         mutable SkMutex fLibraryMutex;
99     };
100 
101     /** Fetch units/EM from "head" table if needed (ie for bitmap fonts) */
102     static int GetUnitsPerEm(FT_Face face);
103 
104     /**
105      *  Return the font data, or nullptr on failure.
106      */
107     std::unique_ptr<SkFontData> makeFontData() const;
108     class FaceRec;
109     FaceRec* getFaceRec() const;
110 
111 protected:
112     SkTypeface_FreeType(const SkFontStyle& style, bool isFixedPitch);
113     ~SkTypeface_FreeType() override;
114 
115     std::unique_ptr<SkFontData> cloneFontData(const SkFontArguments&) const;
116     std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&,
117                                                            const SkDescriptor*) const override;
118     void onFilterRec(SkScalerContextRec*) const override;
119     void getGlyphToUnicodeMap(SkUnichar*) const override;
120     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
121     void getPostScriptGlyphNames(SkString* dstArray) const override;
122     bool onGetPostScriptName(SkString*) const override;
123     int onGetUPEM() const override;
124     bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
125                                      int32_t adjustments[]) const override;
126     void onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const override;
127     int onCountGlyphs() const override;
128 
129     LocalizedStrings* onCreateFamilyNameIterator() const override;
130 
131     bool onGlyphMaskNeedsCurrentColor() const override;
132     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
133                                      int coordinateCount) const override;
134     int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
135                                        int parameterCount) const override;
136     int onGetTableTags(SkFontTableTag tags[]) const override;
137     size_t onGetTableData(SkFontTableTag, size_t offset,
138                           size_t length, void* data) const override;
139     sk_sp<SkData> onCopyTableData(SkFontTableTag) const override;
140 
141     virtual std::unique_ptr<SkFontData> onMakeFontData() const = 0;
142 
143 private:
144     mutable SkOnce fFTFaceOnce;
145     mutable std::unique_ptr<FaceRec> fFaceRec;
146 
147     mutable SkSharedMutex fC2GCacheMutex;
148     mutable SkCharToGlyphCache fC2GCache;
149 
150     using INHERITED = SkTypeface;
151 };
152 
153 #endif // SKFONTHOST_FREETYPE_COMMON_H_
154