blob: 71d928bcb9bcce39292a06493584b320bb10b0c7 [file] [log] [blame]
bungeman51daa252014-06-05 13:38:45 -07001/*
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>
bungemanf5484442014-09-10 07:49:05 -070020#if SK_HAS_DWRITE_1_H
21# include <dwrite_1.h>
22#endif
bungeman51daa252014-06-05 13:38:45 -070023
24class SkFontDescriptor;
25struct SkScalerContextRec;
26
bungemana4c4a2d2014-10-20 13:33:19 -070027static 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);
bungeman51daa252014-06-05 13:38:45 -070035}
36
37class DWriteFontTypeface : public SkTypeface {
38private:
bungemana4c4a2d2014-10-20 13:33:19 -070039 DWriteFontTypeface(const SkFontStyle& style, SkFontID fontID,
bungeman51daa252014-06-05 13:38:45 -070040 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))
bungemanf73c2372014-07-23 13:31:06 -070053 {
bungemanf5484442014-09-10 07:49:05 -070054#if SK_HAS_DWRITE_1_H
bungemanf73c2372014-07-23 13:31:06 -070055 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 }
bungemanf5484442014-09-10 07:49:05 -070060#endif
bungemanf73c2372014-07-23 13:31:06 -070061 }
bungeman51daa252014-06-05 13:38:45 -070062
63public:
64 SkTScopedComPtr<IDWriteFactory> fFactory;
65 SkTScopedComPtr<IDWriteFontCollectionLoader> fDWriteFontCollectionLoader;
66 SkTScopedComPtr<IDWriteFontFileLoader> fDWriteFontFileLoader;
67 SkTScopedComPtr<IDWriteFontFamily> fDWriteFontFamily;
68 SkTScopedComPtr<IDWriteFont> fDWriteFont;
69 SkTScopedComPtr<IDWriteFontFace> fDWriteFontFace;
bungemanf5484442014-09-10 07:49:05 -070070#if SK_HAS_DWRITE_1_H
bungemanf73c2372014-07-23 13:31:06 -070071 SkTScopedComPtr<IDWriteFontFace1> fDWriteFontFace1;
bungemanf5484442014-09-10 07:49:05 -070072#endif
bungeman51daa252014-06-05 13:38:45 -070073
74 static DWriteFontTypeface* Create(IDWriteFactory* factory,
75 IDWriteFontFace* fontFace,
76 IDWriteFont* font,
77 IDWriteFontFamily* fontFamily,
78 IDWriteFontFileLoader* fontFileLoader = NULL,
79 IDWriteFontCollectionLoader* fontCollectionLoader = NULL) {
bungeman51daa252014-06-05 13:38:45 -070080 SkFontID fontID = SkTypefaceCache::NewFontID();
bungemana4c4a2d2014-10-20 13:33:19 -070081 return SkNEW_ARGS(DWriteFontTypeface, (get_style(font), fontID,
bungeman51daa252014-06-05 13:38:45 -070082 factory, fontFace, font, fontFamily,
83 fontFileLoader, fontCollectionLoader));
84 }
85
86protected:
mtklein36352bf2015-03-25 18:17:31 -070087 void weak_dispose() const override {
bungeman51daa252014-06-05 13:38:45 -070088 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
mtklein36352bf2015-03-25 18:17:31 -070099 SkStreamAsset* onOpenStream(int* ttcIndex) const override;
100 SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override;
101 void onFilterRec(SkScalerContextRec*) const override;
reed39a9a502015-05-12 09:50:04 -0700102 SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
103 PerGlyphInfo, const uint32_t*, uint32_t) const override;
mtklein36352bf2015-03-25 18:17:31 -0700104 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
bungeman51daa252014-06-05 13:38:45 -0700105 virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
mtklein36352bf2015-03-25 18:17:31 -0700106 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;
bungeman51daa252014-06-05 13:38:45 -0700112 virtual size_t onGetTableData(SkFontTableTag, size_t offset,
mtklein36352bf2015-03-25 18:17:31 -0700113 size_t length, void* data) const override;
bungeman51daa252014-06-05 13:38:45 -0700114
115private:
116 typedef SkTypeface INHERITED;
117};
118
119#endif