blob: 1f6fbe041ef0a9990d298afc8befeabb77791587 [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
halcanary385fe4d2015-08-26 13:07:48 -070046SkRandomScalerContext::~SkRandomScalerContext() { delete fProxy; }
joshualittaa2f6582015-07-29 10:14:58 -070047
48unsigned SkRandomScalerContext::generateGlyphCount() {
49 return fProxy->getGlyphCount();
50}
51
52uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) {
53 return fProxy->charToGlyphID(uni);
54}
55
56void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) {
57 fProxy->getAdvance(glyph);
joshualittaa2f6582015-07-29 10:14:58 -070058}
59
60void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) {
joshualittaa2f6582015-07-29 10:14:58 -070061 // Here we will change the mask format of the glyph
62 // NOTE this is being overridden by the base class
63 SkMask::Format format;
joshualittd45fb5a2015-08-01 10:33:40 -070064 switch (glyph->getGlyphID() % 4) {
joshualittaa2f6582015-07-29 10:14:58 -070065 case 0:
66 format = SkMask::kLCD16_Format;
67 break;
68 case 1:
69 format = SkMask::kA8_Format;
70 break;
71 case 2:
72 format = SkMask::kARGB32_Format;
73 break;
joshualittd45fb5a2015-08-01 10:33:40 -070074 case 3:
75 format = SkMask::kBW_Format;
76 break;
joshualittaa2f6582015-07-29 10:14:58 -070077 }
78
joshualitt65e96b42015-07-31 11:45:22 -070079 fProxy->getMetrics(glyph);
80
joshualittaa2f6582015-07-29 10:14:58 -070081 glyph->fMaskFormat = format;
joshualitt65e96b42015-07-31 11:45:22 -070082 if (fFakeIt) {
83 return;
84 }
85 if (SkMask::kARGB32_Format == format) {
joshualitt65e96b42015-07-31 11:45:22 -070086 SkPath path;
87 fProxy->getPath(*glyph, &path);
joshualitt65e96b42015-07-31 11:45:22 -070088
89 SkRect storage;
90 const SkPaint& paint = fFace->paint();
91 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
92 &storage,
93 SkPaint::kFill_Style);
94 SkIRect ibounds;
95 newBounds.roundOut(&ibounds);
96 glyph->fLeft = ibounds.fLeft;
97 glyph->fTop = ibounds.fTop;
98 glyph->fWidth = ibounds.width();
99 glyph->fHeight = ibounds.height();
100 } else {
101 SkPath devPath, fillPath;
102 SkMatrix fillToDevMatrix;
103
104 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
105
106 // just use devPath
107 const SkIRect ir = devPath.getBounds().roundOut();
108
109 if (ir.isEmpty() || !ir.is16Bit()) {
110 glyph->fLeft = 0;
111 glyph->fTop = 0;
112 glyph->fWidth = 0;
113 glyph->fHeight = 0;
114 return;
115 }
116 glyph->fLeft = ir.fLeft;
117 glyph->fTop = ir.fTop;
118 glyph->fWidth = SkToU16(ir.width());
119 glyph->fHeight = SkToU16(ir.height());
120
121 if (glyph->fWidth > 0) {
122 switch (glyph->fMaskFormat) {
123 case SkMask::kLCD16_Format:
124 glyph->fWidth += 2;
125 glyph->fLeft -= 1;
126 break;
127 default:
128 break;
129 }
130 }
131 }
joshualittaa2f6582015-07-29 10:14:58 -0700132}
133
134void SkRandomScalerContext::generateImage(const SkGlyph& glyph) {
135 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
joshualittd45fb5a2015-08-01 10:33:40 -0700136 switch (glyph.getGlyphID() % 4) {
joshualittaa2f6582015-07-29 10:14:58 -0700137 case 0:
joshualittaa2f6582015-07-29 10:14:58 -0700138 format = SkMask::kLCD16_Format;
139 break;
joshualitt65e96b42015-07-31 11:45:22 -0700140 case 1:
joshualittaa2f6582015-07-29 10:14:58 -0700141 format = SkMask::kA8_Format;
142 break;
joshualitt65e96b42015-07-31 11:45:22 -0700143 case 2:
joshualittaa2f6582015-07-29 10:14:58 -0700144 format = SkMask::kARGB32_Format;
145 break;
joshualittd45fb5a2015-08-01 10:33:40 -0700146 case 3:
147 format = SkMask::kBW_Format;
148 break;
joshualittaa2f6582015-07-29 10:14:58 -0700149 }
150 const_cast<SkGlyph&>(glyph).fMaskFormat = format;
151
152 // if the format is ARGB, we just draw the glyph from path ourselves. Otherwise, we force
153 // our proxy context to generate the image from paths.
joshualitt65e96b42015-07-31 11:45:22 -0700154 if (!fFakeIt) {
155 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
156 SkPath path;
157 fProxy->getPath(glyph, &path);
joshualittaa2f6582015-07-29 10:14:58 -0700158
joshualitt65e96b42015-07-31 11:45:22 -0700159 SkBitmap bm;
160 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
161 glyph.fImage, glyph.rowBytes());
162 bm.eraseColor(0);
joshualittaa2f6582015-07-29 10:14:58 -0700163
joshualitt65e96b42015-07-31 11:45:22 -0700164 SkCanvas canvas(bm);
165 canvas.translate(-SkIntToScalar(glyph.fLeft),
166 -SkIntToScalar(glyph.fTop));
joshualitt65e96b42015-07-31 11:45:22 -0700167 canvas.drawPath(path, fFace->paint());
168 } else {
169 fProxy->forceGenerateImageFromPath();
170 fProxy->getImage(glyph);
171 fProxy->forceOffGenerateImageFromPath();
172 }
joshualittaa2f6582015-07-29 10:14:58 -0700173 } else {
joshualitt65e96b42015-07-31 11:45:22 -0700174 sk_bzero(glyph.fImage, glyph.computeImageSize());
joshualittaa2f6582015-07-29 10:14:58 -0700175 }
176}
177
178void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
179 fProxy->getPath(glyph, path);
joshualittaa2f6582015-07-29 10:14:58 -0700180}
181
182void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
183 fProxy->getFontMetrics(metrics);
joshualittaa2f6582015-07-29 10:14:58 -0700184}
185
186///////////////////////////////////////////////////////////////////////////////
187
188#include "SkTypefaceCache.h"
189
joshualitt65e96b42015-07-31 11:45:22 -0700190SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool fakeIt)
joshualittaa2f6582015-07-29 10:14:58 -0700191 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false)
192 , fProxy(SkRef(proxy))
joshualitt65e96b42015-07-31 11:45:22 -0700193 , fPaint(paint)
194 , fFakeIt(fakeIt) {}
joshualittaa2f6582015-07-29 10:14:58 -0700195
196SkRandomTypeface::~SkRandomTypeface() {
197 fProxy->unref();
198}
199
200SkScalerContext* SkRandomTypeface::onCreateScalerContext(
201 const SkDescriptor* desc) const {
halcanary385fe4d2015-08-26 13:07:48 -0700202 return new SkRandomScalerContext(const_cast<SkRandomTypeface*>(this), desc, fFakeIt);
joshualittaa2f6582015-07-29 10:14:58 -0700203}
204
205void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
206 fProxy->filterRec(rec);
207 rec->setHinting(SkPaint::kNo_Hinting);
208 rec->fMaskFormat = SkMask::kARGB32_Format;
209}
210
211SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics(
212 PerGlyphInfo info,
213 const uint32_t* glyphIDs,
214 uint32_t glyphIDsCount) const {
215 return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
216}
217
218SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const {
219 return fProxy->openStream(ttcIndex);
220}
221
222void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
223 bool* isLocal) const {
224 fProxy->getFontDescriptor(desc, isLocal);
225}
226
227int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
228 uint16_t glyphs[], int glyphCount) const {
229 return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
230}
231
232int SkRandomTypeface::onCountGlyphs() const {
233 return fProxy->countGlyphs();
234}
235
236int SkRandomTypeface::onGetUPEM() const {
237 return fProxy->getUnitsPerEm();
238}
239
240void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
241 fProxy->getFamilyName(familyName);
242}
243
244SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
245 return fProxy->createFamilyNameIterator();
246}
247
248int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
249 return fProxy->getTableTags(tags);
250}
251
252size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
253 size_t length, void* data) const {
254 return fProxy->getTableData(tag, offset, length, data);
255}
256