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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 uint16_t generateCharToGlyph(SkUnichar) override; | 21 uint16_t generateCharToGlyph(SkUnichar) override; |
22 void generateAdvance(SkGlyph*) override; | 22 void generateAdvance(SkGlyph*) override; |
23 void generateMetrics(SkGlyph*) override; | 23 void generateMetrics(SkGlyph*) override; |
24 void generateImage(const SkGlyph&) override; | 24 void generateImage(const SkGlyph&) override; |
25 void generatePath(const SkGlyph&, SkPath*) override; | 25 void generatePath(const SkGlyph&, SkPath*) override; |
26 void generateFontMetrics(SkPaint::FontMetrics*) override; | 26 void generateFontMetrics(SkPaint::FontMetrics*) override; |
27 | 27 |
28 private: | 28 private: |
29 SkRandomTypeface* fFace; | 29 SkRandomTypeface* fFace; |
30 SkScalerContext* fProxy; | 30 SkScalerContext* fProxy; |
31 SkMatrix fMatrix; | |
32 bool fFakeIt; | 31 bool fFakeIt; |
33 }; | 32 }; |
34 | 33 |
35 #define STD_SIZE 1 | 34 #define STD_SIZE 1 |
36 | 35 |
37 #include "SkDescriptor.h" | 36 #include "SkDescriptor.h" |
38 | 37 |
39 SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDes
criptor* desc, | 38 SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDes
criptor* desc, |
40 bool fakeIt) | 39 bool fakeIt) |
41 : SkScalerContext(face, desc) | 40 : SkScalerContext(face, desc) |
42 , fFace(face) | 41 , fFace(face) |
43 , fFakeIt(fakeIt) { | 42 , fFakeIt(fakeIt) { |
44 size_t descSize = SkDescriptor::ComputeOverhead(1) + sizeof(SkScalerContext
::Rec); | 43 fProxy = face->proxy()->createScalerContext(desc); |
45 SkAutoDescriptor ad(descSize); | |
46 SkDescriptor* newDesc = ad.getDesc(); | |
47 | |
48 newDesc->init(); | |
49 void* entry = newDesc->addEntry(kRec_SkDescriptorTag, | |
50 sizeof(SkScalerContext::Rec), &fRec); | |
51 { | |
52 SkScalerContext::Rec* rec = (SkScalerContext::Rec*)entry; | |
53 rec->fTextSize = STD_SIZE; | |
54 rec->fPreScaleX = SK_Scalar1; | |
55 rec->fPreSkewX = 0; | |
56 rec->fPost2x2[0][0] = rec->fPost2x2[1][1] = SK_Scalar1; | |
57 rec->fPost2x2[1][0] = rec->fPost2x2[0][1] = 0; | |
58 } | |
59 SkASSERT(descSize == newDesc->getLength()); | |
60 newDesc->computeChecksum(); | |
61 | |
62 fProxy = face->proxy()->createScalerContext(newDesc); | |
63 | |
64 fRec.getSingleMatrix(&fMatrix); | |
65 fMatrix.preScale(SK_Scalar1 / STD_SIZE, SK_Scalar1 / STD_SIZE); | |
66 } | 44 } |
67 | 45 |
68 SkRandomScalerContext::~SkRandomScalerContext() { | 46 SkRandomScalerContext::~SkRandomScalerContext() { |
69 SkDELETE(fProxy); | 47 SkDELETE(fProxy); |
70 } | 48 } |
71 | 49 |
72 unsigned SkRandomScalerContext::generateGlyphCount() { | 50 unsigned SkRandomScalerContext::generateGlyphCount() { |
73 return fProxy->getGlyphCount(); | 51 return fProxy->getGlyphCount(); |
74 } | 52 } |
75 | 53 |
76 uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) { | 54 uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) { |
77 return fProxy->charToGlyphID(uni); | 55 return fProxy->charToGlyphID(uni); |
78 } | 56 } |
79 | 57 |
80 void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) { | 58 void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) { |
81 fProxy->getAdvance(glyph); | 59 fProxy->getAdvance(glyph); |
82 | |
83 SkVector advance; | |
84 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX), | |
85 SkFixedToScalar(glyph->fAdvanceY), &advance); | |
86 glyph->fAdvanceX = SkScalarToFixed(advance.fX); | |
87 glyph->fAdvanceY = SkScalarToFixed(advance.fY); | |
88 } | 60 } |
89 | 61 |
90 void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) { | 62 void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) { |
91 // Here we will change the mask format of the glyph | 63 // Here we will change the mask format of the glyph |
92 // NOTE this is being overridden by the base class | 64 // NOTE this is being overridden by the base class |
93 SkMask::Format format; | 65 SkMask::Format format; |
94 switch (glyph->getGlyphID() % 3) { | 66 switch (glyph->getGlyphID() % 3) { |
95 case 0: | 67 case 0: |
96 format = SkMask::kLCD16_Format; | 68 format = SkMask::kLCD16_Format; |
97 break; | 69 break; |
98 case 1: | 70 case 1: |
99 format = SkMask::kA8_Format; | 71 format = SkMask::kA8_Format; |
100 break; | 72 break; |
101 case 2: | 73 case 2: |
102 format = SkMask::kARGB32_Format; | 74 format = SkMask::kARGB32_Format; |
103 break; | 75 break; |
104 } | 76 } |
105 | 77 |
106 fProxy->getMetrics(glyph); | 78 fProxy->getMetrics(glyph); |
107 | 79 |
108 glyph->fMaskFormat = format; | 80 glyph->fMaskFormat = format; |
109 if (fFakeIt) { | 81 if (fFakeIt) { |
110 return; | 82 return; |
111 } | 83 } |
112 if (SkMask::kARGB32_Format == format) { | 84 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; | 85 SkPath path; |
120 fProxy->getPath(*glyph, &path); | 86 fProxy->getPath(*glyph, &path); |
121 path.transform(fMatrix); | |
122 | 87 |
123 SkRect storage; | 88 SkRect storage; |
124 const SkPaint& paint = fFace->paint(); | 89 const SkPaint& paint = fFace->paint(); |
125 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(), | 90 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(), |
126 &storage, | 91 &storage, |
127 SkPaint::kFill_Style
); | 92 SkPaint::kFill_Style
); |
128 SkIRect ibounds; | 93 SkIRect ibounds; |
129 newBounds.roundOut(&ibounds); | 94 newBounds.roundOut(&ibounds); |
130 glyph->fLeft = ibounds.fLeft; | 95 glyph->fLeft = ibounds.fLeft; |
131 glyph->fTop = ibounds.fTop; | 96 glyph->fTop = ibounds.fTop; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 fProxy->getPath(glyph, &path); | 153 fProxy->getPath(glyph, &path); |
189 | 154 |
190 SkBitmap bm; | 155 SkBitmap bm; |
191 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHei
ght), | 156 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHei
ght), |
192 glyph.fImage, glyph.rowBytes()); | 157 glyph.fImage, glyph.rowBytes()); |
193 bm.eraseColor(0); | 158 bm.eraseColor(0); |
194 | 159 |
195 SkCanvas canvas(bm); | 160 SkCanvas canvas(bm); |
196 canvas.translate(-SkIntToScalar(glyph.fLeft), | 161 canvas.translate(-SkIntToScalar(glyph.fLeft), |
197 -SkIntToScalar(glyph.fTop)); | 162 -SkIntToScalar(glyph.fTop)); |
198 canvas.concat(fMatrix); | |
199 canvas.drawPath(path, fFace->paint()); | 163 canvas.drawPath(path, fFace->paint()); |
200 } else { | 164 } else { |
201 fProxy->forceGenerateImageFromPath(); | 165 fProxy->forceGenerateImageFromPath(); |
202 fProxy->getImage(glyph); | 166 fProxy->getImage(glyph); |
203 fProxy->forceOffGenerateImageFromPath(); | 167 fProxy->forceOffGenerateImageFromPath(); |
204 } | 168 } |
205 } else { | 169 } else { |
206 sk_bzero(glyph.fImage, glyph.computeImageSize()); | 170 sk_bzero(glyph.fImage, glyph.computeImageSize()); |
207 } | 171 } |
208 } | 172 } |
209 | 173 |
210 void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) { | 174 void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) { |
211 fProxy->getPath(glyph, path); | 175 fProxy->getPath(glyph, path); |
212 path->transform(fMatrix); | |
213 } | 176 } |
214 | 177 |
215 void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) { | 178 void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) { |
216 fProxy->getFontMetrics(metrics); | 179 fProxy->getFontMetrics(metrics); |
217 if (metrics) { | |
218 SkScalar scale = fMatrix.getScaleY(); | |
219 metrics->fTop = SkScalarMul(metrics->fTop, scale); | |
220 metrics->fAscent = SkScalarMul(metrics->fAscent, scale); | |
221 metrics->fDescent = SkScalarMul(metrics->fDescent, scale); | |
222 metrics->fBottom = SkScalarMul(metrics->fBottom, scale); | |
223 metrics->fLeading = SkScalarMul(metrics->fLeading, scale); | |
224 metrics->fAvgCharWidth = SkScalarMul(metrics->fAvgCharWidth, scale); | |
225 metrics->fXMin = SkScalarMul(metrics->fXMin, scale); | |
226 metrics->fXMax = SkScalarMul(metrics->fXMax, scale); | |
227 metrics->fXHeight = SkScalarMul(metrics->fXHeight, scale); | |
228 } | |
229 } | 180 } |
230 | 181 |
231 /////////////////////////////////////////////////////////////////////////////// | 182 /////////////////////////////////////////////////////////////////////////////// |
232 | 183 |
233 #include "SkTypefaceCache.h" | 184 #include "SkTypefaceCache.h" |
234 | 185 |
235 SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool
fakeIt) | 186 SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool
fakeIt) |
236 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false) | 187 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false) |
237 , fProxy(SkRef(proxy)) | 188 , fProxy(SkRef(proxy)) |
238 , fPaint(paint) | 189 , fPaint(paint) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 243 |
293 int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const { | 244 int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const { |
294 return fProxy->getTableTags(tags); | 245 return fProxy->getTableTags(tags); |
295 } | 246 } |
296 | 247 |
297 size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset, | 248 size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset, |
298 size_t length, void* data) const { | 249 size_t length, void* data) const { |
299 return fProxy->getTableData(tag, offset, length, data); | 250 return fProxy->getTableData(tag, offset, length, data); |
300 } | 251 } |
301 | 252 |
OLD | NEW |