OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #ifndef SkAdvancedTypefaceMetrics_DEFINED | 9 #ifndef SkAdvancedTypefaceMetrics_DEFINED |
10 #define SkAdvancedTypefaceMetrics_DEFINED | 10 #define SkAdvancedTypefaceMetrics_DEFINED |
11 | 11 |
12 #include "SkRect.h" | 12 #include "SkRect.h" |
13 #include "SkRefCnt.h" | 13 #include "SkRefCnt.h" |
14 #include "SkString.h" | 14 #include "SkString.h" |
15 #include "SkTDArray.h" | 15 #include "SkTDArray.h" |
16 #include "SkTemplates.h" | 16 #include "SkTemplates.h" |
17 | 17 #include "SkSinglyLinkedList.h" |
18 // Whatever std::unique_ptr Clank's using doesn't seem to work with AdvanceMetri
c's | |
19 // style of forward-declaration. Probably just a bug in an old libc++ / libstdc
++. | |
20 // For now, hack around it with our own smart pointer. It'd be nice to clean up
. | |
21 template <typename T> | |
22 class SkHackyAutoTDelete : SkNoncopyable { | |
23 public: | |
24 explicit SkHackyAutoTDelete(T* ptr = nullptr) : fPtr(ptr) {} | |
25 ~SkHackyAutoTDelete() { delete fPtr; } | |
26 | |
27 T* get() const { return fPtr; } | |
28 T* operator->() const { return fPtr; } | |
29 | |
30 void reset(T* ptr = nullptr) { | |
31 if (ptr != fPtr) { | |
32 delete fPtr; | |
33 fPtr = ptr; | |
34 } | |
35 } | |
36 T* release() { | |
37 T* ptr = fPtr; | |
38 fPtr = nullptr; | |
39 return ptr; | |
40 } | |
41 | |
42 private: | |
43 T* fPtr; | |
44 }; | |
45 | 18 |
46 /** \class SkAdvancedTypefaceMetrics | 19 /** \class SkAdvancedTypefaceMetrics |
47 | 20 |
48 The SkAdvancedTypefaceMetrics class is used by the PDF backend to correctly | 21 The SkAdvancedTypefaceMetrics class is used by the PDF backend to correctly |
49 embed typefaces. This class is created and filled in with information by | 22 embed typefaces. This class is created and filled in with information by |
50 SkTypeface::getAdvancedTypefaceMetrics. | 23 SkTypeface::getAdvancedTypefaceMetrics. |
51 */ | 24 */ |
52 | 25 |
53 class SkAdvancedTypefaceMetrics : public SkRefCnt { | 26 class SkAdvancedTypefaceMetrics : public SkRefCnt { |
54 public: | 27 public: |
55 | 28 |
56 SkAdvancedTypefaceMetrics() | 29 SkAdvancedTypefaceMetrics() |
57 : fType(SkAdvancedTypefaceMetrics::kOther_Font) | 30 : fType(SkAdvancedTypefaceMetrics::kOther_Font) |
58 , fFlags(SkAdvancedTypefaceMetrics::kEmpty_FontFlag) | 31 , fFlags(SkAdvancedTypefaceMetrics::kEmpty_FontFlag) |
59 , fLastGlyphID(0) | 32 , fLastGlyphID(0) |
60 , fEmSize(0) | 33 , fEmSize(0) |
61 , fStyle(0) | 34 , fStyle(0) |
62 , fItalicAngle(0) | 35 , fItalicAngle(0) |
63 , fAscent(0) | 36 , fAscent(0) |
64 , fDescent(0) | 37 , fDescent(0) |
65 , fStemV(0) | 38 , fStemV(0) |
66 , fCapHeight(0) | 39 , fCapHeight(0) |
67 , fBBox(SkIRect::MakeEmpty()) {} | 40 , fBBox(SkIRect::MakeEmpty()) {} |
68 | 41 |
69 ~SkAdvancedTypefaceMetrics(); | 42 ~SkAdvancedTypefaceMetrics(); |
70 | 43 |
| 44 /** Retrieve advance data for glyphs. Used by the PDF backend. It |
| 45 calls underlying platform dependent API getAdvance to acquire |
| 46 the data. |
| 47 @param num_glyphs Total number of glyphs in the given font. |
| 48 @param glyphIDs For per-glyph info, specify subset of the |
| 49 font by giving glyph ids. Each integer |
| 50 represents a glyph id. Passing nullptr |
| 51 means all glyphs in the font. |
| 52 @param glyphIDsCount Number of elements in subsetGlyphIds. |
| 53 Ignored if glyphIDs is nullptr. |
| 54 @param getAdvance A function that takes a glyph id and |
| 55 passes back advance data from the |
| 56 typeface. Returns false on failure. |
| 57 */ |
| 58 template <typename FontHandle> |
| 59 void setGlyphWidths(FontHandle fontHandle, |
| 60 int num_glyphs, |
| 61 const uint32_t* subsetGlyphIDs, |
| 62 uint32_t subsetGlyphIDsLength, |
| 63 bool (*getAdvance)(FontHandle fontHandle, |
| 64 int gId, |
| 65 int16_t* data)); |
| 66 |
71 SkString fFontName; | 67 SkString fFontName; |
72 | 68 |
73 enum FontType { | 69 enum FontType { |
74 kType1_Font, | 70 kType1_Font, |
75 kType1CID_Font, | 71 kType1CID_Font, |
76 kCFF_Font, | 72 kCFF_Font, |
77 kTrueType_Font, | 73 kTrueType_Font, |
78 kOther_Font, | 74 kOther_Font, |
79 }; | 75 }; |
80 // The type of the underlying font program. This field determines which | 76 // The type of the underlying font program. This field determines which |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 struct AdvanceMetric { | 115 struct AdvanceMetric { |
120 enum MetricType { | 116 enum MetricType { |
121 kDefault, // Default advance: fAdvance.count = 1 | 117 kDefault, // Default advance: fAdvance.count = 1 |
122 kRange, // Advances for a range: fAdvance.count = fEndID-fStartID | 118 kRange, // Advances for a range: fAdvance.count = fEndID-fStartID |
123 kRun // fStartID-fEndID have same advance: fAdvance.count = 1 | 119 kRun // fStartID-fEndID have same advance: fAdvance.count = 1 |
124 }; | 120 }; |
125 MetricType fType; | 121 MetricType fType; |
126 uint16_t fStartId; | 122 uint16_t fStartId; |
127 uint16_t fEndId; | 123 uint16_t fEndId; |
128 SkTDArray<Data> fAdvance; | 124 SkTDArray<Data> fAdvance; |
129 SkHackyAutoTDelete<AdvanceMetric<Data> > fNext; | 125 AdvanceMetric(uint16_t startId) : fStartId(startId) {} |
| 126 AdvanceMetric(AdvanceMetric&& other) |
| 127 : fType(other.fType) |
| 128 , fStartId(other.fStartId) |
| 129 , fEndId(other.fEndId) { |
| 130 fAdvance.swap(other.fAdvance); |
| 131 } |
| 132 AdvanceMetric& operator=(AdvanceMetric&& other) { |
| 133 fType = other.fType; |
| 134 fStartId = other.fStartId; |
| 135 fEndId = other.fEndId; |
| 136 fAdvance.swap(other.fAdvance); |
| 137 return *this; |
| 138 } |
| 139 AdvanceMetric(const AdvanceMetric&) = delete; |
| 140 AdvanceMetric& operator=(const AdvanceMetric&) = delete; |
130 }; | 141 }; |
131 | 142 |
132 struct VerticalMetric { | 143 struct VerticalMetric { |
133 int16_t fVerticalAdvance; | 144 int16_t fVerticalAdvance; |
134 int16_t fOriginXDisp; // Horiz. displacement of the secondary origin. | 145 int16_t fOriginXDisp; // Horiz. displacement of the secondary origin. |
135 int16_t fOriginYDisp; // Vert. displacement of the secondary origin. | 146 int16_t fOriginYDisp; // Vert. displacement of the secondary origin. |
136 }; | 147 }; |
137 typedef AdvanceMetric<int16_t> WidthRange; | 148 typedef AdvanceMetric<int16_t> WidthRange; |
138 typedef AdvanceMetric<VerticalMetric> VerticalAdvanceRange; | 149 typedef AdvanceMetric<VerticalMetric> VerticalAdvanceRange; |
139 | 150 |
140 // This is indexed by glyph id. | 151 // This is indexed by glyph id. |
141 SkAutoTDelete<WidthRange> fGlyphWidths; | 152 SkSinglyLinkedList<WidthRange> fGlyphWidths; |
142 // Only used for Vertical CID fonts. | 153 // Only used for Vertical CID fonts. |
143 SkAutoTDelete<VerticalAdvanceRange> fVerticalMetrics; | 154 SkSinglyLinkedList<VerticalAdvanceRange> fVerticalMetrics; |
144 | 155 |
145 // The names of each glyph, only populated for postscript fonts. | 156 // The names of each glyph, only populated for postscript fonts. |
146 SkAutoTDelete<SkAutoTArray<SkString> > fGlyphNames; | 157 SkAutoTDelete<SkAutoTArray<SkString> > fGlyphNames; |
147 | 158 |
148 // The mapping from glyph to Unicode, only populated if | 159 // The mapping from glyph to Unicode, only populated if |
149 // kToUnicode_PerGlyphInfo is passed to GetAdvancedTypefaceMetrics. | 160 // kToUnicode_PerGlyphInfo is passed to GetAdvancedTypefaceMetrics. |
150 SkTDArray<SkUnichar> fGlyphToUnicode; | 161 SkTDArray<SkUnichar> fGlyphToUnicode; |
151 | 162 |
| 163 static void FinishRange(WidthRange* range, |
| 164 int endId, |
| 165 WidthRange::MetricType type); |
| 166 |
152 private: | 167 private: |
153 typedef SkRefCnt INHERITED; | 168 typedef SkRefCnt INHERITED; |
154 }; | 169 }; |
155 | 170 |
156 namespace skia_advanced_typeface_metrics_utils { | |
157 | |
158 template <typename Data> | |
159 void resetRange(SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range, | |
160 int startId); | |
161 | |
162 template <typename Data, template<typename> class AutoTDelete> | |
163 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* appendRange( | |
164 AutoTDelete<SkAdvancedTypefaceMetrics::AdvanceMetric<Data> >* nextSlot, | |
165 int startId); | |
166 | |
167 template <typename Data> | |
168 void finishRange( | |
169 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range, | |
170 int endId, | |
171 typename SkAdvancedTypefaceMetrics::AdvanceMetric<Data>::MetricType | |
172 type); | |
173 | |
174 /** Retrieve advance data for glyphs. Used by the PDF backend. It calls | |
175 underlying platform dependent API getAdvance to acquire the data. | |
176 @param num_glyphs Total number of glyphs in the given font. | |
177 @param glyphIDs For per-glyph info, specify subset of the font by | |
178 giving glyph ids. Each integer represents a glyph | |
179 id. Passing nullptr means all glyphs in the font. | |
180 @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if | |
181 glyphIDs is nullptr. | |
182 */ | |
183 template <typename Data, typename FontHandle> | |
184 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* getAdvanceData( | |
185 FontHandle fontHandle, | |
186 int num_glyphs, | |
187 const uint32_t* glyphIDs, | |
188 uint32_t glyphIDsCount, | |
189 bool (*getAdvance)(FontHandle fontHandle, int gId, Data* data)); | |
190 | |
191 } // namespace skia_advanced_typeface_metrics_utils | |
192 | 171 |
193 #endif | 172 #endif |
OLD | NEW |