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 SkTypeface_Freetype_DEFINED 10 #define SkTypeface_Freetype_DEFINED 11 12 #include "include/core/SkSpan.h" 13 #include "include/core/SkTypeface.h" 14 #include "include/core/SkTypes.h" 15 #include "include/private/base/SkFixed.h" 16 #include "include/private/base/SkMutex.h" 17 #include "include/private/base/SkNoncopyable.h" 18 #include "include/private/base/SkTArray.h" 19 #include "src/base/SkSharedMutex.h" 20 #include "src/core/SkFontScanner.h" 21 #include "src/utils/SkCharToGlyphCache.h" 22 23 class SkFontData; 24 25 // These are forward declared to avoid pimpl but also hide the FreeType implementation. 26 typedef struct FT_LibraryRec_* FT_Library; 27 typedef struct FT_FaceRec_* FT_Face; 28 typedef struct FT_StreamRec_* FT_Stream; 29 typedef signed long FT_Pos; 30 typedef struct FT_BBox_ FT_BBox; 31 32 class SkTypeface_FreeType : public SkTypeface { 33 public: 34 /** Fetch units/EM from "head" table if needed (ie for bitmap fonts) */ 35 static int GetUnitsPerEm(FT_Face face); 36 37 /** Return the font data, or nullptr on failure. */ 38 std::unique_ptr<SkFontData> makeFontData() const; 39 class FaceRec; 40 FaceRec* getFaceRec() const; 41 42 static constexpr SkTypeface::FactoryId FactoryId = SkSetFourByteTag('f','r','e','e'); 43 static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&); 44 45 protected: 46 SkTypeface_FreeType(const SkFontStyle& style, bool isFixedPitch); 47 ~SkTypeface_FreeType() override; 48 49 std::unique_ptr<SkFontData> cloneFontData(const SkFontArguments&, SkFontStyle* style) const; 50 std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&, 51 const SkDescriptor*) const override; 52 void onFilterRec(SkScalerContextRec*) const override; 53 void getGlyphToUnicodeMap(SkUnichar*) const override; 54 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; 55 void getPostScriptGlyphNames(SkString* dstArray) const override; 56 bool onGetPostScriptName(SkString*) const override; 57 int onGetUPEM() const override; 58 bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count, 59 int32_t adjustments[]) const override; 60 void onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const override; 61 int onCountGlyphs() const override; 62 63 LocalizedStrings* onCreateFamilyNameIterator() const override; 64 65 bool onGlyphMaskNeedsCurrentColor() const override; 66 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], 67 int coordinateCount) const override; 68 int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], 69 int parameterCount) const override; 70 int onGetTableTags(SkFontTableTag tags[]) const override; 71 size_t onGetTableData(SkFontTableTag, size_t offset, 72 size_t length, void* data) const override; 73 sk_sp<SkData> onCopyTableData(SkFontTableTag) const override; 74 75 virtual std::unique_ptr<SkFontData> onMakeFontData() const = 0; 76 /** Utility to fill out the SkFontDescriptor palette information from the SkFontData. */ 77 static void FontDataPaletteToDescriptorPalette(const SkFontData&, SkFontDescriptor*); 78 79 private: 80 mutable SkOnce fFTFaceOnce; 81 mutable std::unique_ptr<FaceRec> fFaceRec; 82 83 mutable SkSharedMutex fC2GCacheMutex; 84 mutable SkCharToGlyphCache fC2GCache; 85 86 mutable SkOnce fGlyphMasksMayNeedCurrentColorOnce; 87 mutable bool fGlyphMasksMayNeedCurrentColor; 88 89 using INHERITED = SkTypeface; 90 }; 91 92 class SkTypeface_FreeTypeStream : public SkTypeface_FreeType { 93 public: 94 SkTypeface_FreeTypeStream(std::unique_ptr<SkFontData> fontData, const SkString familyName, 95 const SkFontStyle& style, bool isFixedPitch); 96 ~SkTypeface_FreeTypeStream() override; 97 98 protected: 99 void onGetFamilyName(SkString* familyName) const override; 100 void onGetFontDescriptor(SkFontDescriptor*, bool* serialize) const override; 101 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; 102 std::unique_ptr<SkFontData> onMakeFontData() const override; 103 sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const override; 104 105 private: 106 const SkString fFamilyName; 107 const std::unique_ptr<const SkFontData> fData; 108 }; 109 110 class SkFontScanner_FreeType : public SkFontScanner { 111 public: 112 SkFontScanner_FreeType(); 113 ~SkFontScanner_FreeType() override; 114 115 bool scanFile(SkStreamAsset* stream, int* numFaces) const override; 116 bool scanFace(SkStreamAsset* stream, int faceIndex, int* numInstances) const override; 117 bool scanInstance(SkStreamAsset* stream, 118 int faceIndex, 119 int instanceIndex, 120 SkString* name, 121 SkFontStyle* style, 122 bool* isFixedPitch, 123 AxisDefinitions* axes) const override; 124 static void computeAxisValues( 125 AxisDefinitions axisDefinitions, 126 const SkFontArguments::VariationPosition position, 127 SkFixed* axisValues, 128 const SkString& name, 129 SkFontStyle* style, 130 const SkFontArguments::VariationPosition::Coordinate* currentPosition = nullptr); 131 static bool GetAxes(FT_Face face, AxisDefinitions* axes); 132 private: 133 FT_Face openFace(SkStreamAsset* stream, int ttcIndex, FT_Stream ftStream) const; 134 FT_Library fLibrary; 135 mutable SkMutex fLibraryMutex; 136 }; 137 138 #endif // SkTypeface_Freetype_DEFINED 139