xref: /aosp_15_r20/external/skia/src/ports/SkTypeface_win_dw.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkTypeface_win_dw_DEFINED
9 #define SkTypeface_win_dw_DEFINED
10 
11 #include "include/core/SkFontArguments.h"
12 #include "include/core/SkTypeface.h"
13 #include "include/private/base/SkAPI.h"
14 #include "src/base/SkLeanWindows.h"
15 #include "src/core/SkAdvancedTypefaceMetrics.h"
16 #include "src/core/SkTypefaceCache.h"
17 #include "src/utils/win/SkDWrite.h"
18 #include "src/utils/win/SkHRESULT.h"
19 #include "src/utils/win/SkTScopedComPtr.h"
20 
21 #include <dwrite.h>
22 #include <dwrite_1.h>
23 #include <dwrite_2.h>
24 #include <dwrite_3.h>
25 
26 class SkFontDescriptor;
27 struct SkScalerContextRec;
28 
29 /* dwrite_3.h incorrectly uses NTDDI_VERSION to hide immutable interfaces (it should only be used to
30    gate changes to public ABI). The implementation files can (and must) get away with including
31    SkDWriteNTDDI_VERSION.h which simply unsets NTDDI_VERSION, but this doesn't work well for this
32    header which can be included in SkTypeface.cpp. Instead, ensure that any declarations hidden
33    behind the NTDDI_VERSION are forward (backward?) declared here in case dwrite_3.h did not declare
34    them. */
35 interface IDWriteFontFace4;
36 interface IDWriteFontFace7;
37 
38 class DWriteFontTypeface : public SkTypeface {
39 public:
40     struct Loaders : public SkNVRefCnt<Loaders> {
LoadersLoaders41         Loaders(IDWriteFactory* factory,
42                   IDWriteFontFileLoader* fontFileLoader,
43                   IDWriteFontCollectionLoader* fontCollectionLoader)
44             : fFactory(SkRefComPtr(factory))
45             , fDWriteFontFileLoader(SkRefComPtr(fontFileLoader))
46             , fDWriteFontCollectionLoader(SkRefComPtr(fontCollectionLoader))
47         {}
48         Loaders(const Loaders&) = delete;
49         Loaders& operator=(const Loaders&) = delete;
50         Loaders(Loaders&&) = delete;
51         Loaders& operator=(Loaders&&) = delete;
52         ~Loaders();
53 
54         SkTScopedComPtr<IDWriteFactory> fFactory;
55         SkTScopedComPtr<IDWriteFontFileLoader> fDWriteFontFileLoader;
56         SkTScopedComPtr<IDWriteFontCollectionLoader> fDWriteFontCollectionLoader;
57     };
58 
59     static constexpr SkTypeface::FactoryId FactoryId = SkSetFourByteTag('d','w','r','t');
60     static sk_sp<SkTypeface> SK_SPI MakeFromStream(std::unique_ptr<SkStreamAsset>,
61                                                    const SkFontArguments&);
62 
63     ~DWriteFontTypeface() override;
64 private:
65     DWriteFontTypeface(const SkFontStyle& style,
66                        IDWriteFactory* factory,
67                        IDWriteFontFace* fontFace,
68                        IDWriteFont* font,
69                        IDWriteFontFamily* fontFamily,
70                        sk_sp<Loaders> loaders,
71                        const SkFontArguments::Palette&);
72     HRESULT initializePalette();
73 
74 public:
75     SkTScopedComPtr<IDWriteFactory> fFactory;
76     SkTScopedComPtr<IDWriteFactory2> fFactory2;
77     SkTScopedComPtr<IDWriteFontFamily> fDWriteFontFamily;
78     SkTScopedComPtr<IDWriteFont> fDWriteFont;
79     SkTScopedComPtr<IDWriteFontFace> fDWriteFontFace;
80     SkTScopedComPtr<IDWriteFontFace1> fDWriteFontFace1;
81     SkTScopedComPtr<IDWriteFontFace2> fDWriteFontFace2;
82     SkTScopedComPtr<IDWriteFontFace4> fDWriteFontFace4;
83     // Once WDK 10.0.25357.0 or newer is required to build, fDWriteFontFace7 can be a smart pointer.
84     // If a smart pointer is used then ~DWriteFontTypeface must call the smart pointer's destructor,
85     // which must include code to Release the IDWriteFontFace7, but there may be no IDWriteFontFace7
86     // other than the forward declaration. Skia should never declare an IDWriteFontFace7 (other than
87     // copying the entire interface) for ODR reasons. This header cannot detect if there will be a
88     // full declaration of IDWriteFontFace7 at the ~DWriteFontTypeface implementation because of
89     // NTDDI_VERSION shenanigains, otherwise this defintition could just be ifdef'ed.
90     //SkTScopedComPtr<IDWriteFontFace7> fDWriteFontFace7;
91     IDWriteFontFace7* fDWriteFontFace7 = nullptr;
92     bool fIsColorFont;
93 
94     std::unique_ptr<SkFontArguments::Palette::Override> fRequestedPaletteEntryOverrides;
95     SkFontArguments::Palette fRequestedPalette;
96 
97     size_t fPaletteEntryCount;
98     std::unique_ptr<SkColor[]> fPalette;
99     std::unique_ptr<DWRITE_COLOR_F[]> fDWPalette;
100 
101     static SkFontStyle GetStyle(IDWriteFont* font, IDWriteFontFace* fontFace);
102     static sk_sp<DWriteFontTypeface> Make(
103         IDWriteFactory* factory,
104         IDWriteFontFace* fontFace,
105         IDWriteFont* font,
106         IDWriteFontFamily* fontFamily,
107         sk_sp<Loaders> loaders,
108         const SkFontArguments::Palette& palette);
109 
110 protected:
weak_dispose()111     void weak_dispose() const override {
112         fLoaders.reset();
113 
114         //SkTypefaceCache::Remove(this);
115         INHERITED::weak_dispose();
116     }
117 
118     sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const override;
119     std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
120     std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&,
121                                                            const SkDescriptor*) const override;
122     void onFilterRec(SkScalerContextRec*) const override;
123     void getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const override;
124     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
125     void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
126     void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override;
127     int onCountGlyphs() const override;
128     void getPostScriptGlyphNames(SkString*) const override;
129     int onGetUPEM() const override;
130     void onGetFamilyName(SkString* familyName) const override;
131     bool onGetPostScriptName(SkString*) const override;
132     int onGetResourceName(SkString*) const override;
133     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
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, size_t length, void* data) const override;
141     sk_sp<SkData> onCopyTableData(SkFontTableTag) const override;
142 
143 private:
144     mutable sk_sp<Loaders> fLoaders;
145     using INHERITED = SkTypeface;
146 };
147 
148 #endif
149