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 "SkAdvancedTypefaceMetrics.h" 12 #include "SkDWrite.h" 13 #include "SkHRESULT.h" 14 #include "SkTScopedComPtr.h" 15 #include "SkTypeface.h" 16 #include "SkTypefaceCache.h" 17 #include "SkTypes.h" 18 19 #include <dwrite.h> 20 #if SK_HAS_DWRITE_1_H 21 # include <dwrite_1.h> 22 #endif 23 24 class SkFontDescriptor; 25 struct SkScalerContextRec; 26 27 static SkFontStyle get_style(IDWriteFont* font) { 28 DWRITE_FONT_STYLE dwStyle = font->GetStyle(); 29 return SkFontStyle(font->GetWeight(), 30 font->GetStretch(), 31 (DWRITE_FONT_STYLE_OBLIQUE == dwStyle || 32 DWRITE_FONT_STYLE_ITALIC == dwStyle) 33 ? SkFontStyle::kItalic_Slant 34 : SkFontStyle::kUpright_Slant); 35 } 36 37 class DWriteFontTypeface : public SkTypeface { 38 private: 39 DWriteFontTypeface(const SkFontStyle& style, SkFontID fontID, 40 IDWriteFactory* factory, 41 IDWriteFontFace* fontFace, 42 IDWriteFont* font, 43 IDWriteFontFamily* fontFamily, 44 IDWriteFontFileLoader* fontFileLoader = NULL, 45 IDWriteFontCollectionLoader* fontCollectionLoader = NULL) 46 : SkTypeface(style, fontID, false) 47 , fFactory(SkRefComPtr(factory)) 48 , fDWriteFontCollectionLoader(SkSafeRefComPtr(fontCollectionLoader)) 49 , fDWriteFontFileLoader(SkSafeRefComPtr(fontFileLoader)) 50 , fDWriteFontFamily(SkRefComPtr(fontFamily)) 51 , fDWriteFont(SkRefComPtr(font)) 52 , fDWriteFontFace(SkRefComPtr(fontFace)) 53 { 54 #if SK_HAS_DWRITE_1_H 55 if (!SUCCEEDED(fDWriteFontFace->QueryInterface(&fDWriteFontFace1))) { 56 // IUnknown::QueryInterface states that if it fails, punk will be set to NULL. 57 // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/26/96777.aspx 58 SK_ALWAYSBREAK(NULL == fDWriteFontFace1.get()); 59 } 60 #endif 61 } 62 63 public: 64 SkTScopedComPtr<IDWriteFactory> fFactory; 65 SkTScopedComPtr<IDWriteFontCollectionLoader> fDWriteFontCollectionLoader; 66 SkTScopedComPtr<IDWriteFontFileLoader> fDWriteFontFileLoader; 67 SkTScopedComPtr<IDWriteFontFamily> fDWriteFontFamily; 68 SkTScopedComPtr<IDWriteFont> fDWriteFont; 69 SkTScopedComPtr<IDWriteFontFace> fDWriteFontFace; 70 #if SK_HAS_DWRITE_1_H 71 SkTScopedComPtr<IDWriteFontFace1> fDWriteFontFace1; 72 #endif 73 74 static DWriteFontTypeface* Create(IDWriteFactory* factory, 75 IDWriteFontFace* fontFace, 76 IDWriteFont* font, 77 IDWriteFontFamily* fontFamily, 78 IDWriteFontFileLoader* fontFileLoader = NULL, 79 IDWriteFontCollectionLoader* fontCollectionLoader = NULL) { 80 SkFontID fontID = SkTypefaceCache::NewFontID(); 81 return SkNEW_ARGS(DWriteFontTypeface, (get_style(font), fontID, 82 factory, fontFace, font, fontFamily, 83 fontFileLoader, fontCollectionLoader)); 84 } 85 86 protected: 87 void weak_dispose() const override { 88 if (fDWriteFontCollectionLoader.get()) { 89 HRV(fFactory->UnregisterFontCollectionLoader(fDWriteFontCollectionLoader.get())); 90 } 91 if (fDWriteFontFileLoader.get()) { 92 HRV(fFactory->UnregisterFontFileLoader(fDWriteFontFileLoader.get())); 93 } 94 95 //SkTypefaceCache::Remove(this); 96 INHERITED::weak_dispose(); 97 } 98 99 SkStreamAsset* onOpenStream(int* ttcIndex) const override; 100 SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override; 101 void onFilterRec(SkScalerContextRec*) const override; 102 SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( 103 PerGlyphInfo, const uint32_t*, uint32_t) const override; 104 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; 105 virtual int onCharsToGlyphs(const void* chars, Encoding encoding, 106 uint16_t glyphs[], int glyphCount) const override; 107 int onCountGlyphs() const override; 108 int onGetUPEM() const override; 109 void onGetFamilyName(SkString* familyName) const override; 110 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; 111 int onGetTableTags(SkFontTableTag tags[]) const override; 112 virtual size_t onGetTableData(SkFontTableTag, size_t offset, 113 size_t length, void* data) const override; 114 115 private: 116 typedef SkTypeface INHERITED; 117 }; 118 119 #endif 120