blob: 0693682faa707dcdcb9609d75a3b31bec79fd7bd [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;
joshualitt65e96b42015-07-31 11:45:22 -070066 switch (glyph->getGlyphID() % 3) {
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;
joshualittaa2f6582015-07-29 10:14:58 -070076 }
77
joshualitt65e96b42015-07-31 11:45:22 -070078 fProxy->getMetrics(glyph);
79
joshualittaa2f6582015-07-29 10:14:58 -070080 glyph->fMaskFormat = format;
joshualitt65e96b42015-07-31 11:45:22 -070081 if (fFakeIt) {
82 return;
83 }
84 if (SkMask::kARGB32_Format == format) {
joshualitt65e96b42015-07-31 11:45:22 -070085 SkPath path;
86 fProxy->getPath(*glyph, &path);
joshualitt65e96b42015-07-31 11:45:22 -070087
88 SkRect storage;
89 const SkPaint& paint = fFace->paint();
90 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
91 &storage,
92 SkPaint::kFill_Style);
93 SkIRect ibounds;
94 newBounds.roundOut(&ibounds);
95 glyph->fLeft = ibounds.fLeft;
96 glyph->fTop = ibounds.fTop;
97 glyph->fWidth = ibounds.width();
98 glyph->fHeight = ibounds.height();
99 } else {
100 SkPath devPath, fillPath;
101 SkMatrix fillToDevMatrix;
102
103 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
104
105 // just use devPath
106 const SkIRect ir = devPath.getBounds().roundOut();
107
108 if (ir.isEmpty() || !ir.is16Bit()) {
109 glyph->fLeft = 0;
110 glyph->fTop = 0;
111 glyph->fWidth = 0;
112 glyph->fHeight = 0;
113 return;
114 }
115 glyph->fLeft = ir.fLeft;
116 glyph->fTop = ir.fTop;
117 glyph->fWidth = SkToU16(ir.width());
118 glyph->fHeight = SkToU16(ir.height());
119
120 if (glyph->fWidth > 0) {
121 switch (glyph->fMaskFormat) {
122 case SkMask::kLCD16_Format:
123 glyph->fWidth += 2;
124 glyph->fLeft -= 1;
125 break;
126 default:
127 break;
128 }
129 }
130 }
joshualittaa2f6582015-07-29 10:14:58 -0700131}
132
133void SkRandomScalerContext::generateImage(const SkGlyph& glyph) {
134 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
joshualitt65e96b42015-07-31 11:45:22 -0700135 switch (glyph.getGlyphID() % 3) {
joshualittaa2f6582015-07-29 10:14:58 -0700136 case 0:
joshualittaa2f6582015-07-29 10:14:58 -0700137 format = SkMask::kLCD16_Format;
138 break;
joshualitt65e96b42015-07-31 11:45:22 -0700139 case 1:
joshualittaa2f6582015-07-29 10:14:58 -0700140 format = SkMask::kA8_Format;
141 break;
joshualitt65e96b42015-07-31 11:45:22 -0700142 case 2:
joshualittaa2f6582015-07-29 10:14:58 -0700143 format = SkMask::kARGB32_Format;
144 break;
145 }
146 const_cast<SkGlyph&>(glyph).fMaskFormat = format;
147
148 // if the format is ARGB, we just draw the glyph from path ourselves. Otherwise, we force
149 // our proxy context to generate the image from paths.
joshualitt65e96b42015-07-31 11:45:22 -0700150 if (!fFakeIt) {
151 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
152 SkPath path;
153 fProxy->getPath(glyph, &path);
joshualittaa2f6582015-07-29 10:14:58 -0700154
joshualitt65e96b42015-07-31 11:45:22 -0700155 SkBitmap bm;
156 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
157 glyph.fImage, glyph.rowBytes());
158 bm.eraseColor(0);
joshualittaa2f6582015-07-29 10:14:58 -0700159
joshualitt65e96b42015-07-31 11:45:22 -0700160 SkCanvas canvas(bm);
161 canvas.translate(-SkIntToScalar(glyph.fLeft),
162 -SkIntToScalar(glyph.fTop));
joshualitt65e96b42015-07-31 11:45:22 -0700163 canvas.drawPath(path, fFace->paint());
164 } else {
165 fProxy->forceGenerateImageFromPath();
166 fProxy->getImage(glyph);
167 fProxy->forceOffGenerateImageFromPath();
168 }
joshualittaa2f6582015-07-29 10:14:58 -0700169 } else {
joshualitt65e96b42015-07-31 11:45:22 -0700170 sk_bzero(glyph.fImage, glyph.computeImageSize());
joshualittaa2f6582015-07-29 10:14:58 -0700171 }
172}
173
174void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
175 fProxy->getPath(glyph, path);
joshualittaa2f6582015-07-29 10:14:58 -0700176}
177
178void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
179 fProxy->getFontMetrics(metrics);
joshualittaa2f6582015-07-29 10:14:58 -0700180}
181
182///////////////////////////////////////////////////////////////////////////////
183
184#include "SkTypefaceCache.h"
185
joshualitt65e96b42015-07-31 11:45:22 -0700186SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool fakeIt)
joshualittaa2f6582015-07-29 10:14:58 -0700187 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false)
188 , fProxy(SkRef(proxy))
joshualitt65e96b42015-07-31 11:45:22 -0700189 , fPaint(paint)
190 , fFakeIt(fakeIt) {}
joshualittaa2f6582015-07-29 10:14:58 -0700191
192SkRandomTypeface::~SkRandomTypeface() {
193 fProxy->unref();
194}
195
196SkScalerContext* SkRandomTypeface::onCreateScalerContext(
197 const SkDescriptor* desc) const {
joshualitt65e96b42015-07-31 11:45:22 -0700198 return SkNEW_ARGS(SkRandomScalerContext, (const_cast<SkRandomTypeface*>(this), desc, fFakeIt));
joshualittaa2f6582015-07-29 10:14:58 -0700199}
200
201void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
202 fProxy->filterRec(rec);
203 rec->setHinting(SkPaint::kNo_Hinting);
204 rec->fMaskFormat = SkMask::kARGB32_Format;
205}
206
207SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics(
208 PerGlyphInfo info,
209 const uint32_t* glyphIDs,
210 uint32_t glyphIDsCount) const {
211 return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
212}
213
214SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const {
215 return fProxy->openStream(ttcIndex);
216}
217
218void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
219 bool* isLocal) const {
220 fProxy->getFontDescriptor(desc, isLocal);
221}
222
223int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
224 uint16_t glyphs[], int glyphCount) const {
225 return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
226}
227
228int SkRandomTypeface::onCountGlyphs() const {
229 return fProxy->countGlyphs();
230}
231
232int SkRandomTypeface::onGetUPEM() const {
233 return fProxy->getUnitsPerEm();
234}
235
236void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
237 fProxy->getFamilyName(familyName);
238}
239
240SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
241 return fProxy->createFamilyNameIterator();
242}
243
244int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
245 return fProxy->getTableTags(tags);
246}
247
248size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
249 size_t length, void* data) const {
250 return fProxy->getTableData(tag, offset, length, data);
251}
252