blob: 799e51df820e5b9803e6a1aa55786420bd9c5caa [file] [log] [blame]
joshualittaa2f6582015-07-29 10:14:58 -07001/*
2 * Copyright 2015 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#include "SkRandomScalerContext.h"
9#include "SkGlyph.h"
10#include "SkPath.h"
11#include "SkCanvas.h"
joshualitt65e96b42015-07-31 11:45:22 -070012#include "SkRasterizer.h"
joshualittaa2f6582015-07-29 10:14:58 -070013
14class SkRandomScalerContext : public SkScalerContext {
15public:
joshualitt65e96b42015-07-31 11:45:22 -070016 SkRandomScalerContext(SkRandomTypeface*, const SkDescriptor*, bool fFakeIt);
joshualittaa2f6582015-07-29 10:14:58 -070017 virtual ~SkRandomScalerContext();
18
19protected:
20 unsigned generateGlyphCount() override;
21 uint16_t generateCharToGlyph(SkUnichar) override;
22 void generateAdvance(SkGlyph*) override;
23 void generateMetrics(SkGlyph*) override;
24 void generateImage(const SkGlyph&) override;
25 void generatePath(const SkGlyph&, SkPath*) override;
26 void generateFontMetrics(SkPaint::FontMetrics*) override;
27
28private:
29 SkRandomTypeface* fFace;
30 SkScalerContext* fProxy;
joshualitt65e96b42015-07-31 11:45:22 -070031 bool fFakeIt;
joshualittaa2f6582015-07-29 10:14:58 -070032};
33
34#define STD_SIZE 1
35
36#include "SkDescriptor.h"
37
joshualitt65e96b42015-07-31 11:45:22 -070038SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDescriptor* desc,
39 bool fakeIt)
joshualittaa2f6582015-07-29 10:14:58 -070040 : SkScalerContext(face, desc)
joshualitt65e96b42015-07-31 11:45:22 -070041 , fFace(face)
42 , fFakeIt(fakeIt) {
joshualitt44c48512015-08-01 07:33:41 -070043 fProxy = face->proxy()->createScalerContext(desc);
joshualittaa2f6582015-07-29 10:14:58 -070044}
45
46SkRandomScalerContext::~SkRandomScalerContext() {
47 SkDELETE(fProxy);
48}
49
50unsigned SkRandomScalerContext::generateGlyphCount() {
51 return fProxy->getGlyphCount();
52}
53
54uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) {
55 return fProxy->charToGlyphID(uni);
56}
57
58void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) {
59 fProxy->getAdvance(glyph);
joshualittaa2f6582015-07-29 10:14:58 -070060}
61
62void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) {
joshualittaa2f6582015-07-29 10:14:58 -070063 // Here we will change the mask format of the glyph
64 // NOTE this is being overridden by the base class
65 SkMask::Format format;
joshualittd45fb5a2015-08-01 10:33:40 -070066 switch (glyph->getGlyphID() % 4) {
joshualittaa2f6582015-07-29 10:14:58 -070067 case 0:
68 format = SkMask::kLCD16_Format;
69 break;
70 case 1:
71 format = SkMask::kA8_Format;
72 break;
73 case 2:
74 format = SkMask::kARGB32_Format;
75 break;
joshualittd45fb5a2015-08-01 10:33:40 -070076 case 3:
77 format = SkMask::kBW_Format;
78 break;
joshualittaa2f6582015-07-29 10:14:58 -070079 }
80
joshualitt65e96b42015-07-31 11:45:22 -070081 fProxy->getMetrics(glyph);
82
joshualittaa2f6582015-07-29 10:14:58 -070083 glyph->fMaskFormat = format;
joshualitt65e96b42015-07-31 11:45:22 -070084 if (fFakeIt) {
85 return;
86 }
87 if (SkMask::kARGB32_Format == format) {
joshualitt65e96b42015-07-31 11:45:22 -070088 SkPath path;
89 fProxy->getPath(*glyph, &path);
joshualitt65e96b42015-07-31 11:45:22 -070090
91 SkRect storage;
92 const SkPaint& paint = fFace->paint();
93 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
94 &storage,
95 SkPaint::kFill_Style);
96 SkIRect ibounds;
97 newBounds.roundOut(&ibounds);
98 glyph->fLeft = ibounds.fLeft;
99 glyph->fTop = ibounds.fTop;
100 glyph->fWidth = ibounds.width();
101 glyph->fHeight = ibounds.height();
102 } else {
103 SkPath devPath, fillPath;
104 SkMatrix fillToDevMatrix;
105
106 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
107
108 // just use devPath
109 const SkIRect ir = devPath.getBounds().roundOut();
110
111 if (ir.isEmpty() || !ir.is16Bit()) {
112 glyph->fLeft = 0;
113 glyph->fTop = 0;
114 glyph->fWidth = 0;
115 glyph->fHeight = 0;
116 return;
117 }
118 glyph->fLeft = ir.fLeft;
119 glyph->fTop = ir.fTop;
120 glyph->fWidth = SkToU16(ir.width());
121 glyph->fHeight = SkToU16(ir.height());
122
123 if (glyph->fWidth > 0) {
124 switch (glyph->fMaskFormat) {
125 case SkMask::kLCD16_Format:
126 glyph->fWidth += 2;
127 glyph->fLeft -= 1;
128 break;
129 default:
130 break;
131 }
132 }
133 }
joshualittaa2f6582015-07-29 10:14:58 -0700134}
135
136void SkRandomScalerContext::generateImage(const SkGlyph& glyph) {
137 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
joshualittd45fb5a2015-08-01 10:33:40 -0700138 switch (glyph.getGlyphID() % 4) {
joshualittaa2f6582015-07-29 10:14:58 -0700139 case 0:
joshualittaa2f6582015-07-29 10:14:58 -0700140 format = SkMask::kLCD16_Format;
141 break;
joshualitt65e96b42015-07-31 11:45:22 -0700142 case 1:
joshualittaa2f6582015-07-29 10:14:58 -0700143 format = SkMask::kA8_Format;
144 break;
joshualitt65e96b42015-07-31 11:45:22 -0700145 case 2:
joshualittaa2f6582015-07-29 10:14:58 -0700146 format = SkMask::kARGB32_Format;
147 break;
joshualittd45fb5a2015-08-01 10:33:40 -0700148 case 3:
149 format = SkMask::kBW_Format;
150 break;
joshualittaa2f6582015-07-29 10:14:58 -0700151 }
152 const_cast<SkGlyph&>(glyph).fMaskFormat = format;
153
154 // if the format is ARGB, we just draw the glyph from path ourselves. Otherwise, we force
155 // our proxy context to generate the image from paths.
joshualitt65e96b42015-07-31 11:45:22 -0700156 if (!fFakeIt) {
157 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
158 SkPath path;
159 fProxy->getPath(glyph, &path);
joshualittaa2f6582015-07-29 10:14:58 -0700160
joshualitt65e96b42015-07-31 11:45:22 -0700161 SkBitmap bm;
162 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
163 glyph.fImage, glyph.rowBytes());
164 bm.eraseColor(0);
joshualittaa2f6582015-07-29 10:14:58 -0700165
joshualitt65e96b42015-07-31 11:45:22 -0700166 SkCanvas canvas(bm);
167 canvas.translate(-SkIntToScalar(glyph.fLeft),
168 -SkIntToScalar(glyph.fTop));
joshualitt65e96b42015-07-31 11:45:22 -0700169 canvas.drawPath(path, fFace->paint());
170 } else {
171 fProxy->forceGenerateImageFromPath();
172 fProxy->getImage(glyph);
173 fProxy->forceOffGenerateImageFromPath();
174 }
joshualittaa2f6582015-07-29 10:14:58 -0700175 } else {
joshualitt65e96b42015-07-31 11:45:22 -0700176 sk_bzero(glyph.fImage, glyph.computeImageSize());
joshualittaa2f6582015-07-29 10:14:58 -0700177 }
178}
179
180void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
181 fProxy->getPath(glyph, path);
joshualittaa2f6582015-07-29 10:14:58 -0700182}
183
184void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
185 fProxy->getFontMetrics(metrics);
joshualittaa2f6582015-07-29 10:14:58 -0700186}
187
188///////////////////////////////////////////////////////////////////////////////
189
190#include "SkTypefaceCache.h"
191
joshualitt65e96b42015-07-31 11:45:22 -0700192SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool fakeIt)
joshualittaa2f6582015-07-29 10:14:58 -0700193 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false)
194 , fProxy(SkRef(proxy))
joshualitt65e96b42015-07-31 11:45:22 -0700195 , fPaint(paint)
196 , fFakeIt(fakeIt) {}
joshualittaa2f6582015-07-29 10:14:58 -0700197
198SkRandomTypeface::~SkRandomTypeface() {
199 fProxy->unref();
200}
201
202SkScalerContext* SkRandomTypeface::onCreateScalerContext(
203 const SkDescriptor* desc) const {
joshualitt65e96b42015-07-31 11:45:22 -0700204 return SkNEW_ARGS(SkRandomScalerContext, (const_cast<SkRandomTypeface*>(this), desc, fFakeIt));
joshualittaa2f6582015-07-29 10:14:58 -0700205}
206
207void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
208 fProxy->filterRec(rec);
209 rec->setHinting(SkPaint::kNo_Hinting);
210 rec->fMaskFormat = SkMask::kARGB32_Format;
211}
212
213SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics(
214 PerGlyphInfo info,
215 const uint32_t* glyphIDs,
216 uint32_t glyphIDsCount) const {
217 return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
218}
219
220SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const {
221 return fProxy->openStream(ttcIndex);
222}
223
224void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
225 bool* isLocal) const {
226 fProxy->getFontDescriptor(desc, isLocal);
227}
228
229int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
230 uint16_t glyphs[], int glyphCount) const {
231 return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
232}
233
234int SkRandomTypeface::onCountGlyphs() const {
235 return fProxy->countGlyphs();
236}
237
238int SkRandomTypeface::onGetUPEM() const {
239 return fProxy->getUnitsPerEm();
240}
241
242void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
243 fProxy->getFamilyName(familyName);
244}
245
246SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
247 return fProxy->createFamilyNameIterator();
248}
249
250int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
251 return fProxy->getTableTags(tags);
252}
253
254size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
255 size_t length, void* data) const {
256 return fProxy->getTableData(tag, offset, length, data);
257}
258