OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 #include "SkRandomScalerContext.h" | 8 #include "SkRandomScalerContext.h" |
9 #include "SkGlyph.h" | 9 #include "SkGlyph.h" |
10 #include "SkPath.h" | 10 #include "SkPath.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "SkRasterizer.h" |
12 | 13 |
13 class SkRandomScalerContext : public SkScalerContext { | 14 class SkRandomScalerContext : public SkScalerContext { |
14 public: | 15 public: |
15 SkRandomScalerContext(SkRandomTypeface*, const SkDescriptor*); | 16 SkRandomScalerContext(SkRandomTypeface*, const SkDescriptor*, bool fFakeIt); |
16 virtual ~SkRandomScalerContext(); | 17 virtual ~SkRandomScalerContext(); |
17 | 18 |
18 protected: | 19 protected: |
19 unsigned generateGlyphCount() override; | 20 unsigned generateGlyphCount() override; |
20 uint16_t generateCharToGlyph(SkUnichar) override; | 21 uint16_t generateCharToGlyph(SkUnichar) override; |
21 void generateAdvance(SkGlyph*) override; | 22 void generateAdvance(SkGlyph*) override; |
22 void generateMetrics(SkGlyph*) override; | 23 void generateMetrics(SkGlyph*) override; |
23 void generateImage(const SkGlyph&) override; | 24 void generateImage(const SkGlyph&) override; |
24 void generatePath(const SkGlyph&, SkPath*) override; | 25 void generatePath(const SkGlyph&, SkPath*) override; |
25 void generateFontMetrics(SkPaint::FontMetrics*) override; | 26 void generateFontMetrics(SkPaint::FontMetrics*) override; |
26 | 27 |
27 private: | 28 private: |
28 SkRandomTypeface* fFace; | 29 SkRandomTypeface* fFace; |
29 SkScalerContext* fProxy; | 30 SkScalerContext* fProxy; |
30 SkMatrix fMatrix; | 31 SkMatrix fMatrix; |
| 32 bool fFakeIt; |
31 }; | 33 }; |
32 | 34 |
33 #define STD_SIZE 1 | 35 #define STD_SIZE 1 |
34 | 36 |
35 #include "SkDescriptor.h" | 37 #include "SkDescriptor.h" |
36 | 38 |
37 SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDes
criptor* desc) | 39 SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDes
criptor* desc, |
| 40 bool fakeIt) |
38 : SkScalerContext(face, desc) | 41 : SkScalerContext(face, desc) |
39 , fFace(face) { | 42 , fFace(face) |
| 43 , fFakeIt(fakeIt) { |
40 size_t descSize = SkDescriptor::ComputeOverhead(1) + sizeof(SkScalerContext
::Rec); | 44 size_t descSize = SkDescriptor::ComputeOverhead(1) + sizeof(SkScalerContext
::Rec); |
41 SkAutoDescriptor ad(descSize); | 45 SkAutoDescriptor ad(descSize); |
42 SkDescriptor* newDesc = ad.getDesc(); | 46 SkDescriptor* newDesc = ad.getDesc(); |
43 | 47 |
44 newDesc->init(); | 48 newDesc->init(); |
45 void* entry = newDesc->addEntry(kRec_SkDescriptorTag, | 49 void* entry = newDesc->addEntry(kRec_SkDescriptorTag, |
46 sizeof(SkScalerContext::Rec), &fRec); | 50 sizeof(SkScalerContext::Rec), &fRec); |
47 { | 51 { |
48 SkScalerContext::Rec* rec = (SkScalerContext::Rec*)entry; | 52 SkScalerContext::Rec* rec = (SkScalerContext::Rec*)entry; |
49 rec->fTextSize = STD_SIZE; | 53 rec->fTextSize = STD_SIZE; |
(...skipping 27 matching lines...) Expand all Loading... |
77 fProxy->getAdvance(glyph); | 81 fProxy->getAdvance(glyph); |
78 | 82 |
79 SkVector advance; | 83 SkVector advance; |
80 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX), | 84 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX), |
81 SkFixedToScalar(glyph->fAdvanceY), &advance); | 85 SkFixedToScalar(glyph->fAdvanceY), &advance); |
82 glyph->fAdvanceX = SkScalarToFixed(advance.fX); | 86 glyph->fAdvanceX = SkScalarToFixed(advance.fX); |
83 glyph->fAdvanceY = SkScalarToFixed(advance.fY); | 87 glyph->fAdvanceY = SkScalarToFixed(advance.fY); |
84 } | 88 } |
85 | 89 |
86 void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) { | 90 void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) { |
87 fProxy->getAdvance(glyph); | |
88 | |
89 SkVector advance; | |
90 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX), | |
91 SkFixedToScalar(glyph->fAdvanceY), &advance); | |
92 glyph->fAdvanceX = SkScalarToFixed(advance.fX); | |
93 glyph->fAdvanceY = SkScalarToFixed(advance.fY); | |
94 | |
95 SkPath path; | |
96 fProxy->getPath(*glyph, &path); | |
97 path.transform(fMatrix); | |
98 | |
99 SkRect storage; | |
100 const SkPaint& paint = fFace->paint(); | |
101 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(), | |
102 &storage, | |
103 SkPaint::kFill_Style); | |
104 SkIRect ibounds; | |
105 newBounds.roundOut(&ibounds); | |
106 glyph->fLeft = ibounds.fLeft; | |
107 glyph->fTop = ibounds.fTop; | |
108 glyph->fWidth = ibounds.width(); | |
109 glyph->fHeight = ibounds.height(); | |
110 | |
111 // Here we will change the mask format of the glyph | 91 // Here we will change the mask format of the glyph |
112 // NOTE this is being overridden by the base class | 92 // NOTE this is being overridden by the base class |
113 SkMask::Format format; | 93 SkMask::Format format; |
114 switch (glyph->getGlyphID() % 6) { | 94 switch (glyph->getGlyphID() % 3) { |
115 case 0: | 95 case 0: |
116 format = SkMask::kLCD16_Format; | 96 format = SkMask::kLCD16_Format; |
117 break; | 97 break; |
118 case 1: | 98 case 1: |
119 format = SkMask::kA8_Format; | 99 format = SkMask::kA8_Format; |
120 break; | 100 break; |
121 case 2: | 101 case 2: |
122 format = SkMask::kARGB32_Format; | 102 format = SkMask::kARGB32_Format; |
123 break; | 103 break; |
124 default: | |
125 // we will fiddle with these in generate image | |
126 format = (SkMask::Format)MASK_FORMAT_UNKNOWN; | |
127 } | 104 } |
128 | 105 |
| 106 fProxy->getMetrics(glyph); |
| 107 |
129 glyph->fMaskFormat = format; | 108 glyph->fMaskFormat = format; |
| 109 if (fFakeIt) { |
| 110 return; |
| 111 } |
| 112 if (SkMask::kARGB32_Format == format) { |
| 113 SkVector advance; |
| 114 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX), |
| 115 SkFixedToScalar(glyph->fAdvanceY), &advance); |
| 116 glyph->fAdvanceX = SkScalarToFixed(advance.fX); |
| 117 glyph->fAdvanceY = SkScalarToFixed(advance.fY); |
| 118 |
| 119 SkPath path; |
| 120 fProxy->getPath(*glyph, &path); |
| 121 path.transform(fMatrix); |
| 122 |
| 123 SkRect storage; |
| 124 const SkPaint& paint = fFace->paint(); |
| 125 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(), |
| 126 &storage, |
| 127 SkPaint::kFill_Style
); |
| 128 SkIRect ibounds; |
| 129 newBounds.roundOut(&ibounds); |
| 130 glyph->fLeft = ibounds.fLeft; |
| 131 glyph->fTop = ibounds.fTop; |
| 132 glyph->fWidth = ibounds.width(); |
| 133 glyph->fHeight = ibounds.height(); |
| 134 } else { |
| 135 SkPath devPath, fillPath; |
| 136 SkMatrix fillToDevMatrix; |
| 137 |
| 138 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); |
| 139 |
| 140 // just use devPath |
| 141 const SkIRect ir = devPath.getBounds().roundOut(); |
| 142 |
| 143 if (ir.isEmpty() || !ir.is16Bit()) { |
| 144 glyph->fLeft = 0; |
| 145 glyph->fTop = 0; |
| 146 glyph->fWidth = 0; |
| 147 glyph->fHeight = 0; |
| 148 return; |
| 149 } |
| 150 glyph->fLeft = ir.fLeft; |
| 151 glyph->fTop = ir.fTop; |
| 152 glyph->fWidth = SkToU16(ir.width()); |
| 153 glyph->fHeight = SkToU16(ir.height()); |
| 154 |
| 155 if (glyph->fWidth > 0) { |
| 156 switch (glyph->fMaskFormat) { |
| 157 case SkMask::kLCD16_Format: |
| 158 glyph->fWidth += 2; |
| 159 glyph->fLeft -= 1; |
| 160 break; |
| 161 default: |
| 162 break; |
| 163 } |
| 164 } |
| 165 } |
130 } | 166 } |
131 | 167 |
132 void SkRandomScalerContext::generateImage(const SkGlyph& glyph) { | 168 void SkRandomScalerContext::generateImage(const SkGlyph& glyph) { |
133 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat; | 169 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat; |
134 switch (glyph.getGlyphID() % 6) { | 170 switch (glyph.getGlyphID() % 3) { |
135 case 0: | 171 case 0: |
136 case 1: | |
137 case 2: | |
138 break; | |
139 case 3: | |
140 format = SkMask::kLCD16_Format; | 172 format = SkMask::kLCD16_Format; |
141 break; | 173 break; |
142 case 4: | 174 case 1: |
143 format = SkMask::kA8_Format; | 175 format = SkMask::kA8_Format; |
144 break; | 176 break; |
145 case 5: | 177 case 2: |
146 format = SkMask::kARGB32_Format; | 178 format = SkMask::kARGB32_Format; |
147 break; | 179 break; |
148 } | 180 } |
149 const_cast<SkGlyph&>(glyph).fMaskFormat = format; | 181 const_cast<SkGlyph&>(glyph).fMaskFormat = format; |
150 | 182 |
151 // if the format is ARGB, we just draw the glyph from path ourselves. Other
wise, we force | 183 // if the format is ARGB, we just draw the glyph from path ourselves. Other
wise, we force |
152 // our proxy context to generate the image from paths. | 184 // our proxy context to generate the image from paths. |
153 if (SkMask::kARGB32_Format == glyph.fMaskFormat) { | 185 if (!fFakeIt) { |
154 SkPath path; | 186 if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
155 fProxy->getPath(glyph, &path); | 187 SkPath path; |
| 188 fProxy->getPath(glyph, &path); |
156 | 189 |
157 SkBitmap bm; | 190 SkBitmap bm; |
158 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight)
, | 191 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHei
ght), |
159 glyph.fImage, glyph.rowBytes()); | 192 glyph.fImage, glyph.rowBytes()); |
160 bm.eraseColor(0); | 193 bm.eraseColor(0); |
161 | 194 |
162 SkCanvas canvas(bm); | 195 SkCanvas canvas(bm); |
163 canvas.translate(-SkIntToScalar(glyph.fLeft), | 196 canvas.translate(-SkIntToScalar(glyph.fLeft), |
164 -SkIntToScalar(glyph.fTop)); | 197 -SkIntToScalar(glyph.fTop)); |
165 canvas.concat(fMatrix); | 198 canvas.concat(fMatrix); |
166 canvas.drawPath(path, fFace->paint()); | 199 canvas.drawPath(path, fFace->paint()); |
| 200 } else { |
| 201 fProxy->forceGenerateImageFromPath(); |
| 202 fProxy->getImage(glyph); |
| 203 fProxy->forceOffGenerateImageFromPath(); |
| 204 } |
167 } else { | 205 } else { |
168 this->forceGenerateImageFromPath(); | 206 sk_bzero(glyph.fImage, glyph.computeImageSize()); |
169 fProxy->getImage(glyph); | |
170 this->forceOffGenerateImageFromPath(); | |
171 } | 207 } |
172 } | 208 } |
173 | 209 |
174 void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) { | 210 void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) { |
175 fProxy->getPath(glyph, path); | 211 fProxy->getPath(glyph, path); |
176 path->transform(fMatrix); | 212 path->transform(fMatrix); |
177 } | 213 } |
178 | 214 |
179 void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) { | 215 void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) { |
180 fProxy->getFontMetrics(metrics); | 216 fProxy->getFontMetrics(metrics); |
181 if (metrics) { | 217 if (metrics) { |
182 SkScalar scale = fMatrix.getScaleY(); | 218 SkScalar scale = fMatrix.getScaleY(); |
183 metrics->fTop = SkScalarMul(metrics->fTop, scale); | 219 metrics->fTop = SkScalarMul(metrics->fTop, scale); |
184 metrics->fAscent = SkScalarMul(metrics->fAscent, scale); | 220 metrics->fAscent = SkScalarMul(metrics->fAscent, scale); |
185 metrics->fDescent = SkScalarMul(metrics->fDescent, scale); | 221 metrics->fDescent = SkScalarMul(metrics->fDescent, scale); |
186 metrics->fBottom = SkScalarMul(metrics->fBottom, scale); | 222 metrics->fBottom = SkScalarMul(metrics->fBottom, scale); |
187 metrics->fLeading = SkScalarMul(metrics->fLeading, scale); | 223 metrics->fLeading = SkScalarMul(metrics->fLeading, scale); |
188 metrics->fAvgCharWidth = SkScalarMul(metrics->fAvgCharWidth, scale); | 224 metrics->fAvgCharWidth = SkScalarMul(metrics->fAvgCharWidth, scale); |
189 metrics->fXMin = SkScalarMul(metrics->fXMin, scale); | 225 metrics->fXMin = SkScalarMul(metrics->fXMin, scale); |
190 metrics->fXMax = SkScalarMul(metrics->fXMax, scale); | 226 metrics->fXMax = SkScalarMul(metrics->fXMax, scale); |
191 metrics->fXHeight = SkScalarMul(metrics->fXHeight, scale); | 227 metrics->fXHeight = SkScalarMul(metrics->fXHeight, scale); |
192 } | 228 } |
193 } | 229 } |
194 | 230 |
195 /////////////////////////////////////////////////////////////////////////////// | 231 /////////////////////////////////////////////////////////////////////////////// |
196 | 232 |
197 #include "SkTypefaceCache.h" | 233 #include "SkTypefaceCache.h" |
198 | 234 |
199 SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint) | 235 SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool
fakeIt) |
200 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false) | 236 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false) |
201 , fProxy(SkRef(proxy)) | 237 , fProxy(SkRef(proxy)) |
202 , fPaint(paint) {} | 238 , fPaint(paint) |
| 239 , fFakeIt(fakeIt) {} |
203 | 240 |
204 SkRandomTypeface::~SkRandomTypeface() { | 241 SkRandomTypeface::~SkRandomTypeface() { |
205 fProxy->unref(); | 242 fProxy->unref(); |
206 } | 243 } |
207 | 244 |
208 SkScalerContext* SkRandomTypeface::onCreateScalerContext( | 245 SkScalerContext* SkRandomTypeface::onCreateScalerContext( |
209 const SkDescriptor* desc) const { | 246 const SkDescriptor* desc) const { |
210 return SkNEW_ARGS(SkRandomScalerContext, (const_cast<SkRandomTypeface*>(this
), desc)); | 247 return SkNEW_ARGS(SkRandomScalerContext, (const_cast<SkRandomTypeface*>(this
), desc, fFakeIt)); |
211 } | 248 } |
212 | 249 |
213 void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const { | 250 void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const { |
214 fProxy->filterRec(rec); | 251 fProxy->filterRec(rec); |
215 rec->setHinting(SkPaint::kNo_Hinting); | 252 rec->setHinting(SkPaint::kNo_Hinting); |
216 rec->fMaskFormat = SkMask::kARGB32_Format; | 253 rec->fMaskFormat = SkMask::kARGB32_Format; |
217 } | 254 } |
218 | 255 |
219 SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics( | 256 SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics( |
220 PerGlyphInfo info, | 257 PerGlyphInfo info, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 292 |
256 int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const { | 293 int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const { |
257 return fProxy->getTableTags(tags); | 294 return fProxy->getTableTags(tags); |
258 } | 295 } |
259 | 296 |
260 size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset, | 297 size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset, |
261 size_t length, void* data) const { | 298 size_t length, void* data) const { |
262 return fProxy->getTableData(tag, offset, length, data); | 299 return fProxy->getTableData(tag, offset, length, data); |
263 } | 300 } |
264 | 301 |
OLD | NEW |