xref: /aosp_15_r20/external/skia/src/ports/SkTypeface_FreeType.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkFontScanner.h"
13 #include "include/core/SkSpan.h"
14 #include "include/core/SkTypeface.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/base/SkFixed.h"
17 #include "include/private/base/SkMutex.h"
18 #include "include/private/base/SkNoncopyable.h"
19 #include "include/private/base/SkTArray.h"
20 #include "src/base/SkSharedMutex.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     std::unique_ptr<SkScalerContext> onCreateScalerContextAsProxyTypeface(
53                                                            const SkScalerContextEffects&,
54                                                            const SkDescriptor*,
55                                                            sk_sp<SkTypeface>) const override;
56     void onFilterRec(SkScalerContextRec*) const override;
57     void getGlyphToUnicodeMap(SkUnichar*) const override;
58     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
59     void getPostScriptGlyphNames(SkString* dstArray) const override;
60     bool onGetPostScriptName(SkString*) const override;
61     int onGetUPEM() const override;
62     bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
63                                      int32_t adjustments[]) const override;
64     void onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const override;
65     int onCountGlyphs() const override;
66 
67     LocalizedStrings* onCreateFamilyNameIterator() const override;
68 
69     bool onGlyphMaskNeedsCurrentColor() const override;
70     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
71                                      int coordinateCount) const override;
72     int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
73                                        int parameterCount) const override;
74     int onGetTableTags(SkFontTableTag tags[]) const override;
75     size_t onGetTableData(SkFontTableTag, size_t offset,
76                           size_t length, void* data) const override;
77     sk_sp<SkData> onCopyTableData(SkFontTableTag) const override;
78 
79     virtual std::unique_ptr<SkFontData> onMakeFontData() const = 0;
80     /** Utility to fill out the SkFontDescriptor palette information from the SkFontData. */
81     static void FontDataPaletteToDescriptorPalette(const SkFontData&, SkFontDescriptor*);
82 
83 private:
84     mutable SkOnce fFTFaceOnce;
85     mutable std::unique_ptr<FaceRec> fFaceRec;
86 
87     mutable SkSharedMutex fC2GCacheMutex;
88     mutable SkCharToGlyphCache fC2GCache;
89 
90     mutable SkOnce fGlyphMasksMayNeedCurrentColorOnce;
91     mutable bool fGlyphMasksMayNeedCurrentColor;
92 
93     using INHERITED = SkTypeface;
94 };
95 
96 class SkTypeface_FreeTypeStream : public SkTypeface_FreeType {
97 public:
98     SkTypeface_FreeTypeStream(std::unique_ptr<SkFontData> fontData, const SkString familyName,
99                               const SkFontStyle& style, bool isFixedPitch);
100     ~SkTypeface_FreeTypeStream() override;
101 
102 protected:
103     void onGetFamilyName(SkString* familyName) const override;
104     void onGetFontDescriptor(SkFontDescriptor*, bool* serialize) const override;
105     std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
106     std::unique_ptr<SkFontData> onMakeFontData() const override;
107     sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const override;
108 
109 private:
110     const SkString fFamilyName;
111     const std::unique_ptr<const SkFontData> fData;
112 };
113 
114 #endif // SkTypeface_Freetype_DEFINED
115