Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2018 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 SkRemoteTypeface_DEFINED
      9 #define SkRemoteTypeface_DEFINED
     10 
     11 #include "SkAdvancedTypefaceMetrics.h"
     12 #include "SkDescriptor.h"
     13 #include "SkFontDescriptor.h"
     14 #include "SkFontStyle.h"
     15 #include "SkScalerContext.h"
     16 #include "SkTypeface.h"
     17 
     18 #include <thread>
     19 
     20 class SkTypefaceProxy;
     21 
     22 class SkRemoteScalerContext {
     23 public:
     24     virtual ~SkRemoteScalerContext() {}
     25     // TODO: do metrics need effects?
     26     virtual void generateFontMetrics(
     27             const SkTypefaceProxy& tf,
     28             const SkScalerContextRec& rec,
     29             SkPaint::FontMetrics*) = 0;
     30     virtual void generateMetrics(
     31             const SkTypefaceProxy& tf,
     32             const SkScalerContextRec& rec,
     33             SkGlyph* glyph) = 0;
     34     virtual void generateImage(
     35             const SkTypefaceProxy& tf,
     36             const SkScalerContextRec& rec,
     37             const SkGlyph& glyph)  = 0;
     38     virtual void generateMetricsAndImage(
     39             const SkTypefaceProxy& tf,
     40             const SkScalerContextRec& rec,
     41             SkArenaAlloc* alloc,
     42             SkGlyph* glyph)  = 0;
     43     virtual void generatePath(
     44             const SkTypefaceProxy& tf,
     45             const SkScalerContextRec& rec,
     46             SkGlyphID glyph, SkPath* path) = 0;
     47 };
     48 
     49 class SkScalerContextProxy : public SkScalerContext {
     50 public:
     51     SkScalerContextProxy(
     52             sk_sp<SkTypeface> tf,
     53             const SkScalerContextEffects& effects,
     54             const SkDescriptor* desc,
     55             SkRemoteScalerContext* rsc);
     56 
     57 protected:
     58     unsigned generateGlyphCount(void) override { SK_ABORT("Should never be called."); return 0;}
     59     uint16_t generateCharToGlyph(SkUnichar uni) override {
     60         SK_ABORT("Should never be called.");
     61         return 0;
     62     }
     63     void generateAdvance(SkGlyph* glyph) override { this->generateMetrics(glyph); }
     64     void generateMetrics(SkGlyph* glyph) override;
     65     void generateImage(const SkGlyph& glyph) override;
     66     void generatePath(SkGlyphID glyphID, SkPath* path) override;
     67     void generateFontMetrics(SkPaint::FontMetrics* metrics) override;
     68 
     69 private:
     70     // Copied from SkGlyphCache
     71     // so we don't grow our arrays a lot
     72     static constexpr size_t kMinGlyphCount = 8;
     73     static constexpr size_t kMinGlyphImageSize = 16 /* height */ * 8 /* width */;
     74     static constexpr size_t kMinAllocAmount = kMinGlyphImageSize * kMinGlyphCount;
     75     SkArenaAlloc  fAlloc{kMinAllocAmount};
     76 
     77     SkTypefaceProxy* typefaceProxy();
     78     SkRemoteScalerContext* const fRemote;
     79     typedef SkScalerContext INHERITED;
     80 };
     81 
     82 class SkTypefaceProxy : public SkTypeface {
     83 public:
     84     SkTypefaceProxy(
     85             SkFontID fontId,
     86             const SkFontStyle& style,
     87             bool isFixed,
     88             SkRemoteScalerContext* rsc)
     89             : INHERITED{style, false}
     90             , fFontId{fontId}
     91             , fRsc{rsc} { }
     92     SkFontID fontID() const {return fFontId;}
     93 
     94 protected:
     95     int onGetUPEM() const override { SK_ABORT("Should never be called."); return 0; }
     96     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
     97         SK_ABORT("Should never be called.");
     98         return nullptr;
     99     }
    100     std::unique_ptr<SkFontData> onMakeFontData() const override {
    101         SK_ABORT("Should never be called.");
    102         return nullptr;
    103     }
    104     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
    105                                      int coordinateCount) const override {
    106         SK_ABORT("Should never be called.");
    107         return 0;
    108     }
    109     void onGetFamilyName(SkString* familyName) const override {
    110         SK_ABORT("Should never be called.");
    111     }
    112     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override {
    113         SK_ABORT("Should never be called.");
    114         return nullptr;
    115     }
    116     int onGetTableTags(SkFontTableTag tags[]) const override {
    117         SK_ABORT("Should never be called.");
    118         return 0;
    119     }
    120     size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override {
    121         SK_ABORT("Should never be called.");
    122         return 0;
    123     }
    124     SkScalerContext* onCreateScalerContext(const SkScalerContextEffects& effects,
    125                                            const SkDescriptor* desc) const override {
    126         //std::cout << fFontId << fThreadId;
    127 
    128         return new SkScalerContextProxy(sk_ref_sp(const_cast<SkTypefaceProxy*>(this)), effects,
    129                                          desc, fRsc);
    130 
    131     }
    132     void onFilterRec(SkScalerContextRec* rec) const override {
    133         // Add all the device information here.
    134         //rec->fPost2x2[0][0] = 0.5f;
    135 
    136         // This would be the best place to run the host SkTypeface_* onFilterRec.
    137         // Can we move onFilterRec to the FongMgr, that way we don't need to cross the boundary to
    138         // filter.
    139     }
    140     void onGetFontDescriptor(SkFontDescriptor*, bool*) const override {
    141         SK_ABORT("Should never be called.");
    142     }
    143     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override {
    144         SK_ABORT("Should never be called.");
    145         return nullptr;
    146     }
    147     int onCharsToGlyphs(const void* chars, Encoding,
    148                         uint16_t glyphs[], int glyphCount) const override {
    149         SK_ABORT("Should never be called.");
    150         return 0;
    151     }
    152     int onCountGlyphs() const override {
    153         SK_ABORT("Should never be called.");
    154         return 0;
    155     }
    156 
    157     void* onGetCTFontRef() const override {
    158         SK_ABORT("Should never be called.");
    159         return nullptr;
    160     }
    161 
    162 private:
    163     const SkFontID fFontId;
    164     // const std::thread::id fThreadId;  // TODO: figure out a good solutions for this.
    165     SkRemoteScalerContext* const fRsc;
    166 
    167     typedef SkTypeface INHERITED;
    168 };
    169 
    170 #endif  // SkRemoteTypeface_DEFINED
    171