bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 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> |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 20 | #if SK_HAS_DWRITE_1_H |
| 21 | # include <dwrite_1.h> |
| 22 | #endif |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 23 | |
| 24 | class SkFontDescriptor; |
| 25 | struct SkScalerContextRec; |
| 26 | |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 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); |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | class DWriteFontTypeface : public SkTypeface { |
| 38 | private: |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 39 | DWriteFontTypeface(const SkFontStyle& style, SkFontID fontID, |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 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)) |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 53 | { |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 54 | #if SK_HAS_DWRITE_1_H |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 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 | } |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 60 | #endif |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 61 | } |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 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; |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 70 | #if SK_HAS_DWRITE_1_H |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 71 | SkTScopedComPtr<IDWriteFontFace1> fDWriteFontFace1; |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 72 | #endif |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 73 | |
| 74 | static DWriteFontTypeface* Create(IDWriteFactory* factory, |
| 75 | IDWriteFontFace* fontFace, |
| 76 | IDWriteFont* font, |
| 77 | IDWriteFontFamily* fontFamily, |
| 78 | IDWriteFontFileLoader* fontFileLoader = NULL, |
| 79 | IDWriteFontCollectionLoader* fontCollectionLoader = NULL) { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 80 | SkFontID fontID = SkTypefaceCache::NewFontID(); |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 81 | return SkNEW_ARGS(DWriteFontTypeface, (get_style(font), fontID, |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 82 | factory, fontFace, font, fontFamily, |
| 83 | fontFileLoader, fontCollectionLoader)); |
| 84 | } |
| 85 | |
| 86 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 87 | void weak_dispose() const override { |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | SkStreamAsset* onOpenStream(int* ttcIndex) const override; |
| 100 | SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override; |
| 101 | void onFilterRec(SkScalerContextRec*) const override; |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 102 | SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( |
| 103 | PerGlyphInfo, const uint32_t*, uint32_t) const override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 104 | void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 105 | virtual int onCharsToGlyphs(const void* chars, Encoding encoding, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 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; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 112 | virtual size_t onGetTableData(SkFontTableTag, size_t offset, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | size_t length, void* data) const override; |
bungeman | 51daa25 | 2014-06-05 13:38:45 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | typedef SkTypeface INHERITED; |
| 117 | }; |
| 118 | |
| 119 | #endif |