Home | History | Annotate | Download | only in fonts
      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 SkTestSVGTypeface_DEFINED
      9 #define SkTestSVGTypeface_DEFINED
     10 
     11 #include "SkFontArguments.h"
     12 #include "SkFontMetrics.h"
     13 #include "SkMutex.h"
     14 #include "SkPaint.h"
     15 #include "SkPoint.h"
     16 #include "SkRect.h"
     17 #include "SkRefCnt.h"
     18 #include "SkScalar.h"
     19 #include "SkStream.h"
     20 #include "SkString.h"
     21 #include "SkTArray.h"
     22 #include "SkTHash.h"
     23 #include "SkTypeface.h"
     24 #include "SkTypes.h"
     25 
     26 #include <memory>
     27 
     28 class SkDescriptor;
     29 class SkFontDescriptor;
     30 class SkFontStyle;
     31 class SkGlyph;
     32 class SkPath;
     33 class SkScalerContext;
     34 class SkSVGDOM;
     35 class SkWStream;
     36 struct SkAdvancedTypefaceMetrics;
     37 struct SkScalerContextEffects;
     38 struct SkScalerContextRec;
     39 
     40 struct SkSVGTestTypefaceGlyphData {
     41     const char* fSvgResourcePath;
     42     SkPoint fOrigin;
     43     SkScalar fAdvance;
     44     SkUnichar fUnicode; //TODO: this limits to 1:1
     45 };
     46 
     47 class SkTestSVGTypeface : public SkTypeface {
     48 public:
     49     SkTestSVGTypeface(const char* name,
     50                       int upem,
     51                       const SkFontMetrics& metrics,
     52                       const SkSVGTestTypefaceGlyphData* data, int dataCount,
     53                       const SkFontStyle& style);
     54     ~SkTestSVGTypeface() override;
     55     void getAdvance(SkGlyph* glyph) const;
     56     void getFontMetrics(SkFontMetrics* metrics) const;
     57 
     58     static sk_sp<SkTestSVGTypeface> Default();
     59     void exportTtxCbdt(SkWStream*) const;
     60     void exportTtxSbix(SkWStream*) const;
     61     void exportTtxColr(SkWStream*) const;
     62 
     63     struct GlyfLayerInfo {
     64         GlyfLayerInfo(int layerColorIndex, SkIRect bounds)
     65             : fLayerColorIndex(layerColorIndex)
     66             , fBounds(bounds) {}
     67         int fLayerColorIndex;
     68         SkIRect fBounds;
     69     };
     70     struct GlyfInfo {
     71         GlyfInfo() : fBounds(SkIRect::MakeEmpty()) {}
     72         SkIRect fBounds;
     73         SkTArray<GlyfLayerInfo> fLayers;
     74     };
     75 protected:
     76     void exportTtxCommon(SkWStream*, const char* type, const SkTArray<GlyfInfo>* = nullptr) const;
     77 
     78     SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
     79                                            const SkDescriptor* desc) const override;
     80     void onFilterRec(SkScalerContextRec* rec) const override;
     81     void getGlyphToUnicodeMap(SkUnichar*) const override;
     82     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
     83 
     84     std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override {
     85         return nullptr;
     86     }
     87 
     88     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
     89         return sk_ref_sp(this);
     90     }
     91 
     92     void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
     93 
     94     int onCharsToGlyphs(const void* chars, Encoding encoding,
     95                         uint16_t glyphs[], int glyphCount) const override;
     96 
     97     int onCountGlyphs() const override {
     98         return fGlyphCount;
     99     }
    100 
    101     int onGetUPEM() const override {
    102         return fUpem;
    103     }
    104 
    105     void onGetFamilyName(SkString* familyName) const override;
    106     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
    107 
    108     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
    109                                      int coordinateCount) const override
    110     {
    111         return 0;
    112     }
    113 
    114     int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
    115                                        int parameterCount) const override
    116     {
    117         return 0;
    118     }
    119 
    120     int onGetTableTags(SkFontTableTag tags[]) const override {
    121         return 0;
    122     }
    123 
    124     size_t onGetTableData(SkFontTableTag tag, size_t offset,
    125                           size_t length, void* data) const override {
    126         return 0;
    127     }
    128 private:
    129     struct Glyph {
    130         Glyph();
    131         ~Glyph();
    132         sk_sp<SkSVGDOM> fSvg;
    133         SkMutex fSvgMutex;
    134         SkPoint fOrigin;
    135         SkScalar fAdvance;
    136     };
    137     SkString fName;
    138     int fUpem;
    139     const SkFontMetrics fFontMetrics;
    140     std::unique_ptr<Glyph[]> fGlyphs;
    141     int fGlyphCount;
    142     SkTHashMap<SkUnichar, SkGlyphID> fCMap;
    143     friend class SkTestSVGScalerContext;
    144 };
    145 
    146 #endif
    147