• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/base/SkLeanWindows.h"
14 #include "src/core/SkAdvancedTypefaceMetrics.h"
15 #include "src/core/SkTypefaceCache.h"
16 #include "src/utils/win/SkDWrite.h"
17 #include "src/utils/win/SkHRESULT.h"
18 #include "src/utils/win/SkTScopedComPtr.h"
19 
20 #include <dwrite.h>
21 #include <dwrite_1.h>
22 #include <dwrite_2.h>
23 #include <dwrite_3.h>
24 
25 class SkFontDescriptor;
26 struct SkScalerContextRec;
27 
28 /* dwrite_3.h incorrectly uses NTDDI_VERSION to hide immutible interfaces (it should only be used to
29    gate changes to public ABI). The implementation files can (and must) get away with including
30    SkDWriteNTDDI_VERSION.h which simply unsets NTDDI_VERSION, but this doesn't work well for this
31    header which can be included in SkTypeface.cpp. Instead, ensure that any declarations hidden
32    behind the NTDDI_VERSION are forward (backward?) declared here in case dwrite_3.h did not declare
33    them. */
34 interface IDWriteFontFace4;
35 
get_style(IDWriteFont * font)36 static SkFontStyle get_style(IDWriteFont* font) {
37     int weight = font->GetWeight();
38     int width = font->GetStretch();
39     SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
40     switch (font->GetStyle()) {
41         case DWRITE_FONT_STYLE_NORMAL: slant = SkFontStyle::kUpright_Slant; break;
42         case DWRITE_FONT_STYLE_OBLIQUE: slant = SkFontStyle::kOblique_Slant; break;
43         case DWRITE_FONT_STYLE_ITALIC: slant = SkFontStyle::kItalic_Slant; break;
44         default: SkASSERT(false); break;
45     }
46     return SkFontStyle(weight, width, slant);
47 }
48 
49 class DWriteFontTypeface : public SkTypeface {
50 public:
51     struct Loaders : public SkNVRefCnt<Loaders> {
LoadersLoaders52         Loaders(IDWriteFactory* factory,
53                   IDWriteFontFileLoader* fontFileLoader,
54                   IDWriteFontCollectionLoader* fontCollectionLoader)
55             : fFactory(SkRefComPtr(factory))
56             , fDWriteFontFileLoader(SkRefComPtr(fontFileLoader))
57             , fDWriteFontCollectionLoader(SkRefComPtr(fontCollectionLoader))
58         {}
59         Loaders(const Loaders&) = delete;
60         Loaders& operator=(const Loaders&) = delete;
61         Loaders(Loaders&&) = delete;
62         Loaders& operator=(Loaders&&) = delete;
63         ~Loaders();
64 
65         SkTScopedComPtr<IDWriteFactory> fFactory;
66         SkTScopedComPtr<IDWriteFontFileLoader> fDWriteFontFileLoader;
67         SkTScopedComPtr<IDWriteFontCollectionLoader> fDWriteFontCollectionLoader;
68     };
69 
70     static constexpr SkTypeface::FactoryId FactoryId = SkSetFourByteTag('d','w','r','t');
71     static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&);
72 
73     ~DWriteFontTypeface() override;
74 private:
75     DWriteFontTypeface(const SkFontStyle& style,
76                        IDWriteFactory* factory,
77                        IDWriteFontFace* fontFace,
78                        IDWriteFont* font,
79                        IDWriteFontFamily* fontFamily,
80                        sk_sp<Loaders> loaders,
81                        const SkFontArguments::Palette&);
82     HRESULT initializePalette();
83 
84 public:
85     SkTScopedComPtr<IDWriteFactory> fFactory;
86     SkTScopedComPtr<IDWriteFactory2> fFactory2;
87     SkTScopedComPtr<IDWriteFontFamily> fDWriteFontFamily;
88     SkTScopedComPtr<IDWriteFont> fDWriteFont;
89     SkTScopedComPtr<IDWriteFontFace> fDWriteFontFace;
90     SkTScopedComPtr<IDWriteFontFace1> fDWriteFontFace1;
91     SkTScopedComPtr<IDWriteFontFace2> fDWriteFontFace2;
92     SkTScopedComPtr<IDWriteFontFace4> fDWriteFontFace4;
93     bool fIsColorFont;
94 
95     std::unique_ptr<SkFontArguments::Palette::Override> fRequestedPaletteEntryOverrides;
96     SkFontArguments::Palette fRequestedPalette;
97 
98     size_t fPaletteEntryCount;
99     std::unique_ptr<SkColor[]> fPalette;
100 
Make(IDWriteFactory * factory,IDWriteFontFace * fontFace,IDWriteFont * font,IDWriteFontFamily * fontFamily,sk_sp<Loaders> loaders,const SkFontArguments::Palette & palette)101     static sk_sp<DWriteFontTypeface> Make(
102         IDWriteFactory* factory,
103         IDWriteFontFace* fontFace,
104         IDWriteFont* font,
105         IDWriteFontFamily* fontFamily,
106         sk_sp<Loaders> loaders,
107         const SkFontArguments::Palette& palette)
108     {
109         return sk_sp<DWriteFontTypeface>(new DWriteFontTypeface(
110             get_style(font), factory, fontFace, font, fontFamily, std::move(loaders), palette));
111     }
112 
113 protected:
weak_dispose()114     void weak_dispose() const override {
115         fLoaders.reset();
116 
117         //SkTypefaceCache::Remove(this);
118         INHERITED::weak_dispose();
119     }
120 
121     sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const override;
122     std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override;
123     std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&,
124                                                            const SkDescriptor*) const override;
125     void onFilterRec(SkScalerContextRec*) const override;
126     void getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const override;
127     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
128     void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
129     void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override;
130     int onCountGlyphs() const override;
131     void getPostScriptGlyphNames(SkString*) const override;
132     int onGetUPEM() const override;
133     void onGetFamilyName(SkString* familyName) const override;
134     bool onGetPostScriptName(SkString*) const override;
135     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
136     bool onGlyphMaskNeedsCurrentColor() const override;
137     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
138                                      int coordinateCount) const override;
139     int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
140                                        int parameterCount) const override;
141     int onGetTableTags(SkFontTableTag tags[]) const override;
142     size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override;
143     sk_sp<SkData> onCopyTableData(SkFontTableTag) const override;
144 
145 private:
146     mutable sk_sp<Loaders> fLoaders;
147     using INHERITED = SkTypeface;
148 };
149 
150 #endif
151