Home | History | Annotate | Download | only in ports
      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 = nullptr,
     45                        IDWriteFontCollectionLoader* fontCollectionLoader = nullptr)
     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 nullptr.
     57             // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/26/96777.aspx
     58             SkASSERT_RELEASE(nullptr == 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 = nullptr,
     79                                       IDWriteFontCollectionLoader* fontCollectionLoader = nullptr) {
     80         SkFontID fontID = SkTypefaceCache::NewFontID();
     81         return new DWriteFontTypeface(get_style(font), fontID, factory, fontFace, font, fontFamily,
     82                                       fontFileLoader, fontCollectionLoader);
     83     }
     84 
     85 protected:
     86     void weak_dispose() const override {
     87         if (fDWriteFontCollectionLoader.get()) {
     88             HRV(fFactory->UnregisterFontCollectionLoader(fDWriteFontCollectionLoader.get()));
     89         }
     90         if (fDWriteFontFileLoader.get()) {
     91             HRV(fFactory->UnregisterFontFileLoader(fDWriteFontFileLoader.get()));
     92         }
     93 
     94         //SkTypefaceCache::Remove(this);
     95         INHERITED::weak_dispose();
     96     }
     97 
     98     SkStreamAsset* onOpenStream(int* ttcIndex) const override;
     99     SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override;
    100     void onFilterRec(SkScalerContextRec*) const override;
    101     SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
    102                                 PerGlyphInfo, const uint32_t*, uint32_t) const override;
    103     void onGetFontDescriptor(SkFontDescriptor*, bool*) const override;
    104     virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
    105                                 uint16_t glyphs[], int glyphCount) const override;
    106     int onCountGlyphs() const override;
    107     int onGetUPEM() const override;
    108     void onGetFamilyName(SkString* familyName) const override;
    109     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
    110     int onGetTableTags(SkFontTableTag tags[]) const override;
    111     virtual size_t onGetTableData(SkFontTableTag, size_t offset,
    112                                   size_t length, void* data) const override;
    113 
    114 private:
    115     typedef SkTypeface INHERITED;
    116 };
    117 
    118 #endif
    119