blob: 49d9ab43ffdafb835bda9bf940ea959879a93ce2 [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
Hal Canary209e4b12017-05-04 14:23:55 -04008#include "SkAdvancedTypefaceMetrics.h"
Florin Malitaab244f02017-05-03 19:16:58 +00009#include "SkBitmap.h"
joshualittaa2f6582015-07-29 10:14:58 -070010#include "SkCanvas.h"
bungeman7cfd46a2016-10-20 16:06:52 -040011#include "SkGlyph.h"
12#include "SkMakeUnique.h"
13#include "SkPath.h"
14#include "SkRandomScalerContext.h"
joshualitt65e96b42015-07-31 11:45:22 -070015#include "SkRasterizer.h"
joshualittaa2f6582015-07-29 10:14:58 -070016
bungeman7cfd46a2016-10-20 16:06:52 -040017class SkDescriptor;
18
joshualittaa2f6582015-07-29 10:14:58 -070019class SkRandomScalerContext : public SkScalerContext {
20public:
bungeman7cfd46a2016-10-20 16:06:52 -040021 SkRandomScalerContext(sk_sp<SkRandomTypeface>, const SkScalerContextEffects&,
reeda9322c22016-04-12 06:47:05 -070022 const SkDescriptor*, bool fFakeIt);
joshualittaa2f6582015-07-29 10:14:58 -070023
24protected:
25 unsigned generateGlyphCount() override;
26 uint16_t generateCharToGlyph(SkUnichar) override;
27 void generateAdvance(SkGlyph*) override;
28 void generateMetrics(SkGlyph*) override;
29 void generateImage(const SkGlyph&) override;
Ben Wagner6e9ac122016-11-11 14:31:06 -050030 void generatePath(SkGlyphID, SkPath*) override;
joshualittaa2f6582015-07-29 10:14:58 -070031 void generateFontMetrics(SkPaint::FontMetrics*) override;
32
33private:
bungeman7cfd46a2016-10-20 16:06:52 -040034 SkRandomTypeface* getRandomTypeface() const {
35 return static_cast<SkRandomTypeface*>(this->getTypeface());
36 }
37 std::unique_ptr<SkScalerContext> fProxy;
joshualitt65e96b42015-07-31 11:45:22 -070038 bool fFakeIt;
joshualittaa2f6582015-07-29 10:14:58 -070039};
40
bungeman7cfd46a2016-10-20 16:06:52 -040041SkRandomScalerContext::SkRandomScalerContext(sk_sp<SkRandomTypeface> face,
reeda9322c22016-04-12 06:47:05 -070042 const SkScalerContextEffects& effects,
43 const SkDescriptor* desc,
joshualitt65e96b42015-07-31 11:45:22 -070044 bool fakeIt)
bungeman7cfd46a2016-10-20 16:06:52 -040045 : SkScalerContext(std::move(face), effects, desc)
joshualitt65e96b42015-07-31 11:45:22 -070046 , fFakeIt(fakeIt) {
bungeman7cfd46a2016-10-20 16:06:52 -040047 fProxy = this->getRandomTypeface()->proxy()->createScalerContext(effects, desc);
joshualittaa2f6582015-07-29 10:14:58 -070048}
49
joshualittaa2f6582015-07-29 10:14:58 -070050unsigned 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
robertphillips52b88cc2016-04-05 16:55:41 -070065 SkMask::Format format = SkMask::kARGB32_Format; // init to handle defective compilers
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;
Ben Wagner6e9ac122016-11-11 14:31:06 -050089 fProxy->getPath(glyph->getPackedID(), &path);
joshualitt65e96b42015-07-31 11:45:22 -070090
91 SkRect storage;
bungeman7cfd46a2016-10-20 16:06:52 -040092 const SkPaint& paint = this->getRandomTypeface()->paint();
joshualitt65e96b42015-07-31 11:45:22 -070093 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
Ben Wagner6e9ac122016-11-11 14:31:06 -0500106 this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix);
joshualitt65e96b42015-07-31 11:45:22 -0700107
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;
Ben Wagner6e9ac122016-11-11 14:31:06 -0500159 fProxy->getPath(glyph.getPackedID(), &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));
bungeman7cfd46a2016-10-20 16:06:52 -0400169 canvas.drawPath(path, this->getRandomTypeface()->paint());
joshualitt65e96b42015-07-31 11:45:22 -0700170 } 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
Ben Wagner6e9ac122016-11-11 14:31:06 -0500180void SkRandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) {
181 fProxy->generatePath(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
bungeman13b9c952016-05-12 10:09:30 -0700192SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
bungemane3aea102016-07-13 05:16:58 -0700193 : SkTypeface(proxy->fontStyle(), false)
bungeman13b9c952016-05-12 10:09:30 -0700194 , fProxy(std::move(proxy))
joshualitt65e96b42015-07-31 11:45:22 -0700195 , fPaint(paint)
196 , fFakeIt(fakeIt) {}
joshualittaa2f6582015-07-29 10:14:58 -0700197
reeda9322c22016-04-12 06:47:05 -0700198SkScalerContext* SkRandomTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
199 const SkDescriptor* desc) const {
bungeman7cfd46a2016-10-20 16:06:52 -0400200 return new SkRandomScalerContext(sk_ref_sp(const_cast<SkRandomTypeface*>(this)),
201 effects, desc, fFakeIt);
joshualittaa2f6582015-07-29 10:14:58 -0700202}
203
204void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
205 fProxy->filterRec(rec);
206 rec->setHinting(SkPaint::kNo_Hinting);
207 rec->fMaskFormat = SkMask::kARGB32_Format;
208}
209
Hal Canary209e4b12017-05-04 14:23:55 -0400210std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const {
211 return fProxy->getAdvancedMetrics();
joshualittaa2f6582015-07-29 10:14:58 -0700212}
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
Ben Wagnerfc497342017-02-24 11:15:26 -0500244int SkRandomTypeface::onGetVariationDesignPosition(
245 SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
246{
247 return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
248}
249
joshualittaa2f6582015-07-29 10:14:58 -0700250int 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