• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 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 SkFontDescriptor_DEFINED
9 #define SkFontDescriptor_DEFINED
10 
11 #include "include/core/SkFontArguments.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkStream.h"
15 #include "include/core/SkString.h"
16 #include "include/core/SkTypeface.h"
17 #include "include/private/base/SkFixed.h"
18 #include "include/private/base/SkNoncopyable.h"
19 #include "include/private/base/SkTemplates.h"
20 
21 #include <memory>
22 #include <utility>
23 
24 class SkFontData {
25 public:
26     /** Makes a copy of the data in 'axis'. */
SkFontData(std::unique_ptr<SkStreamAsset> stream,int index,int paletteIndex,const SkFixed * axis,int axisCount,const SkFontArguments::Palette::Override * paletteOverrides,int paletteOverrideCount)27     SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, int paletteIndex,
28                const SkFixed* axis, int axisCount,
29                const SkFontArguments::Palette::Override* paletteOverrides, int paletteOverrideCount)
30         : fStream(std::move(stream))
31         , fIndex(index)
32         , fPaletteIndex(paletteIndex)
33         , fAxisCount(axisCount)
34         , fPaletteOverrideCount(paletteOverrideCount)
35         , fAxis(fAxisCount)
36         , fPaletteOverrides(fPaletteOverrideCount)
37     {
38         for (int i = 0; i < fAxisCount; ++i) {
39             fAxis[i] = axis[i];
40         }
41         for (int i = 0; i < fPaletteOverrideCount; ++i) {
42             fPaletteOverrides[i] = paletteOverrides[i];
43         }
44     }
45 
SkFontData(const SkFontData & that)46     SkFontData(const SkFontData& that)
47         : fStream(that.fStream->duplicate())
48         , fIndex(that.fIndex)
49         , fPaletteIndex(that.fPaletteIndex)
50         , fAxisCount(that.fAxisCount)
51         , fPaletteOverrideCount(that.fPaletteOverrideCount)
52         , fAxis(fAxisCount)
53         , fPaletteOverrides(fPaletteOverrideCount)
54     {
55         for (int i = 0; i < fAxisCount; ++i) {
56             fAxis[i] = that.fAxis[i];
57         }
58         for (int i = 0; i < fPaletteOverrideCount; ++i) {
59             fPaletteOverrides[i] = that.fPaletteOverrides[i];
60         }
61     }
hasStream()62     bool hasStream() const { return fStream != nullptr; }
detachStream()63     std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
getStream()64     SkStreamAsset* getStream() { return fStream.get(); }
getStream()65     SkStreamAsset const* getStream() const { return fStream.get(); }
getIndex()66     int getIndex() const { return fIndex; }
getAxisCount()67     int getAxisCount() const { return fAxisCount; }
getAxis()68     const SkFixed* getAxis() const { return fAxis.get(); }
getPaletteIndex()69     int getPaletteIndex() const { return fPaletteIndex; }
getPaletteOverrideCount()70     int getPaletteOverrideCount() const { return fPaletteOverrideCount; }
getPaletteOverrides()71     const SkFontArguments::Palette::Override* getPaletteOverrides() const {
72         return fPaletteOverrides.get();
73     }
74 
75 private:
76     std::unique_ptr<SkStreamAsset> fStream;
77     int fIndex;
78     int fPaletteIndex;
79     int fAxisCount;
80     int fPaletteOverrideCount;
81     skia_private::AutoSTMalloc<4, SkFixed> fAxis;
82     skia_private::AutoSTMalloc<4, SkFontArguments::Palette::Override> fPaletteOverrides;
83 };
84 
85 class SkFontDescriptor : SkNoncopyable {
86 public:
87     SkFontDescriptor();
88     // Does not affect ownership of SkStream.
89     static bool Deserialize(SkStream*, SkFontDescriptor* result);
90 
91     void serialize(SkWStream*) const;
92 
getStyle()93     SkFontStyle getStyle() const { return fStyle; }
setStyle(SkFontStyle style)94     void setStyle(SkFontStyle style) { fStyle = style; }
95 
getFamilyName()96     const char* getFamilyName() const { return fFamilyName.c_str(); }
getFullName()97     const char* getFullName() const { return fFullName.c_str(); }
getPostscriptName()98     const char* getPostscriptName() const { return fPostscriptName.c_str(); }
99 
setFamilyName(const char * name)100     void setFamilyName(const char* name) { fFamilyName.set(name); }
setFullName(const char * name)101     void setFullName(const char* name) { fFullName.set(name); }
setPostscriptName(const char * name)102     void setPostscriptName(const char* name) { fPostscriptName.set(name); }
103 
hasStream()104     bool hasStream() const { return bool(fStream); }
dupStream()105     std::unique_ptr<SkStreamAsset> dupStream() const { return fStream->duplicate(); }
getCollectionIndex()106     int getCollectionIndex() const { return fCollectionIndex; }
getPaletteIndex()107     int getPaletteIndex() const { return fPaletteIndex; }
getVariationCoordinateCount()108     int getVariationCoordinateCount() const { return fCoordinateCount; }
getVariation()109     const SkFontArguments::VariationPosition::Coordinate* getVariation() const {
110         return fVariation.get();
111     }
112 #ifdef ENABLE_TEXT_ENHANCE
getVariation()113     SkFontArguments::VariationPosition::Coordinate* getVariation() {
114         return fVariation.get();
115     }
116 #endif
getPaletteEntryOverrideCount()117     int getPaletteEntryOverrideCount() const { return fPaletteEntryOverrideCount; }
getPaletteEntryOverrides()118     const SkFontArguments::Palette::Override* getPaletteEntryOverrides() const {
119         return fPaletteEntryOverrides.get();
120     }
getFactoryId()121     SkTypeface::FactoryId getFactoryId() {
122         return fFactoryId;
123     }
124 
detachStream()125     std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
setStream(std::unique_ptr<SkStreamAsset> stream)126     void setStream(std::unique_ptr<SkStreamAsset> stream) { fStream = std::move(stream); }
setCollectionIndex(int collectionIndex)127     void setCollectionIndex(int collectionIndex) { fCollectionIndex = collectionIndex; }
setPaletteIndex(int paletteIndex)128     void setPaletteIndex(int paletteIndex) { fPaletteIndex = paletteIndex; }
setVariationCoordinates(int coordinateCount)129     SkFontArguments::VariationPosition::Coordinate* setVariationCoordinates(int coordinateCount) {
130         fCoordinateCount = coordinateCount;
131         return fVariation.reset(coordinateCount);
132     }
setPaletteEntryOverrides(int paletteEntryOverrideCount)133     SkFontArguments::Palette::Override* setPaletteEntryOverrides(int paletteEntryOverrideCount) {
134         fPaletteEntryOverrideCount = paletteEntryOverrideCount;
135         return fPaletteEntryOverrides.reset(paletteEntryOverrideCount);
136     }
setFactoryId(SkTypeface::FactoryId factoryId)137     void setFactoryId(SkTypeface::FactoryId factoryId) {
138         fFactoryId = factoryId;
139     }
140 
getFontArguments()141     SkFontArguments getFontArguments() const {
142         return SkFontArguments()
143             .setCollectionIndex(this->getCollectionIndex())
144             .setVariationDesignPosition({this->getVariation(),this->getVariationCoordinateCount()})
145             .setPalette({this->getPaletteIndex(),
146                          this->getPaletteEntryOverrides(),
147                          this->getPaletteEntryOverrideCount()});
148     }
149     static SkFontStyle::Width SkFontStyleWidthForWidthAxisValue(SkScalar width);
150 
151 private:
152     SkString fFamilyName;
153     SkString fFullName;
154     SkString fPostscriptName;
155     SkFontStyle fStyle;
156 
157     std::unique_ptr<SkStreamAsset> fStream;
158     int fCollectionIndex = 0;
159     using Coordinates =
160             skia_private::AutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate>;
161     int fCoordinateCount = 0;
162     Coordinates fVariation;
163     int fPaletteIndex = 0;
164     int fPaletteEntryOverrideCount = 0;
165     skia_private::AutoTMalloc<SkFontArguments::Palette::Override> fPaletteEntryOverrides;
166     SkTypeface::FactoryId fFactoryId = 0;
167 };
168 
169 #endif // SkFontDescriptor_DEFINED
170