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 #ifdef OHOS_SUPPORT 88 bool GetTypefaceFullname(SkStreamAsset* stream, int ttcIndex, SkByteArray& fullname) const; 89 #endif 90 static void computeAxisValues( 91 AxisDefinitions axisDefinitions, 92 const SkFontArguments::VariationPosition position, 93 SkFixed* axisValues, 94 const SkString& name, 95 const SkFontArguments::VariationPosition::Coordinate* currentPosition = nullptr); 96 static bool GetAxes(FT_Face face, AxisDefinitions* axes); 97 98 private: 99 FT_Face openFace(SkStreamAsset* stream, int ttcIndex, FT_Stream ftStream) const; 100 FT_Library fLibrary; 101 mutable SkMutex fLibraryMutex; 102 }; 103 104 /** Fetch units/EM from "head" table if needed (ie for bitmap fonts) */ 105 static int GetUnitsPerEm(FT_Face face); 106 107 /** 108 * Return the font data, or nullptr on failure. 109 */ 110 std::unique_ptr<SkFontData> makeFontData() const; 111 class FaceRec; 112 FaceRec* getFaceRec() const; 113 114 protected: 115 SkTypeface_FreeType(const SkFontStyle& style, bool isFixedPitch); 116 ~SkTypeface_FreeType() override; 117 118 std::unique_ptr<SkFontData> cloneFontData(const SkFontArguments&) const; 119 std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&, 120 const SkDescriptor*) const override; 121 void onFilterRec(SkScalerContextRec*) const override; 122 void getGlyphToUnicodeMap(SkUnichar*) const override; 123 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; 124 void getPostScriptGlyphNames(SkString* dstArray) const override; 125 bool onGetPostScriptName(SkString*) const override; 126 int onGetUPEM() const override; 127 bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count, 128 int32_t adjustments[]) const override; 129 void onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const override; 130 int onCountGlyphs() const override; 131 132 LocalizedStrings* onCreateFamilyNameIterator() const override; 133 134 bool onGlyphMaskNeedsCurrentColor() const override; 135 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], 136 int coordinateCount) const override; 137 int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], 138 int parameterCount) const override; 139 int onGetTableTags(SkFontTableTag tags[]) const override; 140 size_t onGetTableData(SkFontTableTag, size_t offset, 141 size_t length, void* data) const override; 142 sk_sp<SkData> onCopyTableData(SkFontTableTag) const override; 143 144 virtual std::unique_ptr<SkFontData> onMakeFontData() const = 0; 145 146 private: 147 mutable SkOnce fFTFaceOnce; 148 mutable std::unique_ptr<FaceRec> fFaceRec; 149 150 mutable SkSharedMutex fC2GCacheMutex; 151 mutable SkCharToGlyphCache fC2GCache; 152 153 using INHERITED = SkTypeface; 154 }; 155 156 #endif // SKFONTHOST_FREETYPE_COMMON_H_ 157