blob: 31a85b783ea57d75b1ccfaec074c01bef616383a [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"
joshualittaa2f6582015-07-29 10:14:58 -070015
bungeman7cfd46a2016-10-20 16:06:52 -040016class SkDescriptor;
17
joshualittaa2f6582015-07-29 10:14:58 -070018class SkRandomScalerContext : public SkScalerContext {
19public:
bungeman7cfd46a2016-10-20 16:06:52 -040020 SkRandomScalerContext(sk_sp<SkRandomTypeface>, const SkScalerContextEffects&,
reeda9322c22016-04-12 06:47:05 -070021 const SkDescriptor*, bool fFakeIt);
joshualittaa2f6582015-07-29 10:14:58 -070022
23protected:
24 unsigned generateGlyphCount() override;
25 uint16_t generateCharToGlyph(SkUnichar) override;
26 void generateAdvance(SkGlyph*) override;
27 void generateMetrics(SkGlyph*) override;
28 void generateImage(const SkGlyph&) override;
Ben Wagner6e9ac122016-11-11 14:31:06 -050029 void generatePath(SkGlyphID, SkPath*) override;
joshualittaa2f6582015-07-29 10:14:58 -070030 void generateFontMetrics(SkPaint::FontMetrics*) override;
31
32private:
bungeman7cfd46a2016-10-20 16:06:52 -040033 SkRandomTypeface* getRandomTypeface() const {
34 return static_cast<SkRandomTypeface*>(this->getTypeface());
35 }
36 std::unique_ptr<SkScalerContext> fProxy;
joshualitt65e96b42015-07-31 11:45:22 -070037 bool fFakeIt;
joshualittaa2f6582015-07-29 10:14:58 -070038};
39
bungeman7cfd46a2016-10-20 16:06:52 -040040SkRandomScalerContext::SkRandomScalerContext(sk_sp<SkRandomTypeface> face,
reeda9322c22016-04-12 06:47:05 -070041 const SkScalerContextEffects& effects,
42 const SkDescriptor* desc,
joshualitt65e96b42015-07-31 11:45:22 -070043 bool fakeIt)
bungeman7cfd46a2016-10-20 16:06:52 -040044 : SkScalerContext(std::move(face), effects, desc)
joshualitt65e96b42015-07-31 11:45:22 -070045 , fFakeIt(fakeIt) {
bungeman7cfd46a2016-10-20 16:06:52 -040046 fProxy = this->getRandomTypeface()->proxy()->createScalerContext(effects, desc);
joshualittaa2f6582015-07-29 10:14:58 -070047}
48
joshualittaa2f6582015-07-29 10:14:58 -070049unsigned SkRandomScalerContext::generateGlyphCount() {
50 return fProxy->getGlyphCount();
51}
52
53uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) {
54 return fProxy->charToGlyphID(uni);
55}
56
57void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) {
58 fProxy->getAdvance(glyph);
joshualittaa2f6582015-07-29 10:14:58 -070059}
60
61void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) {
joshualittaa2f6582015-07-29 10:14:58 -070062 // Here we will change the mask format of the glyph
63 // NOTE this is being overridden by the base class
robertphillips52b88cc2016-04-05 16:55:41 -070064 SkMask::Format format = SkMask::kARGB32_Format; // init to handle defective compilers
joshualittd45fb5a2015-08-01 10:33:40 -070065 switch (glyph->getGlyphID() % 4) {
joshualittaa2f6582015-07-29 10:14:58 -070066 case 0:
67 format = SkMask::kLCD16_Format;
68 break;
69 case 1:
70 format = SkMask::kA8_Format;
71 break;
72 case 2:
73 format = SkMask::kARGB32_Format;
74 break;
joshualittd45fb5a2015-08-01 10:33:40 -070075 case 3:
76 format = SkMask::kBW_Format;
77 break;
joshualittaa2f6582015-07-29 10:14:58 -070078 }
79
joshualitt65e96b42015-07-31 11:45:22 -070080 fProxy->getMetrics(glyph);
81
joshualittaa2f6582015-07-29 10:14:58 -070082 glyph->fMaskFormat = format;
joshualitt65e96b42015-07-31 11:45:22 -070083 if (fFakeIt) {
84 return;
85 }
86 if (SkMask::kARGB32_Format == format) {
joshualitt65e96b42015-07-31 11:45:22 -070087 SkPath path;
Ben Wagner6e9ac122016-11-11 14:31:06 -050088 fProxy->getPath(glyph->getPackedID(), &path);
joshualitt65e96b42015-07-31 11:45:22 -070089
90 SkRect storage;
bungeman7cfd46a2016-10-20 16:06:52 -040091 const SkPaint& paint = this->getRandomTypeface()->paint();
joshualitt65e96b42015-07-31 11:45:22 -070092 const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
93 &storage,
94 SkPaint::kFill_Style);
95 SkIRect ibounds;
96 newBounds.roundOut(&ibounds);
97 glyph->fLeft = ibounds.fLeft;
98 glyph->fTop = ibounds.fTop;
99 glyph->fWidth = ibounds.width();
100 glyph->fHeight = ibounds.height();
101 } else {
102 SkPath devPath, fillPath;
103 SkMatrix fillToDevMatrix;
104
Ben Wagner6e9ac122016-11-11 14:31:06 -0500105 this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix);
joshualitt65e96b42015-07-31 11:45:22 -0700106
107 // just use devPath
108 const SkIRect ir = devPath.getBounds().roundOut();
109
110 if (ir.isEmpty() || !ir.is16Bit()) {
111 glyph->fLeft = 0;
112 glyph->fTop = 0;
113 glyph->fWidth = 0;
114 glyph->fHeight = 0;
115 return;
116 }
117 glyph->fLeft = ir.fLeft;
118 glyph->fTop = ir.fTop;
119 glyph->fWidth = SkToU16(ir.width());
120 glyph->fHeight = SkToU16(ir.height());
121
122 if (glyph->fWidth > 0) {
123 switch (glyph->fMaskFormat) {
124 case SkMask::kLCD16_Format:
125 glyph->fWidth += 2;
126 glyph->fLeft -= 1;
127 break;
128 default:
129 break;
130 }
131 }
132 }
joshualittaa2f6582015-07-29 10:14:58 -0700133}
134
135void SkRandomScalerContext::generateImage(const SkGlyph& glyph) {
136 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
joshualittd45fb5a2015-08-01 10:33:40 -0700137 switch (glyph.getGlyphID() % 4) {
joshualittaa2f6582015-07-29 10:14:58 -0700138 case 0:
joshualittaa2f6582015-07-29 10:14:58 -0700139 format = SkMask::kLCD16_Format;
140 break;
joshualitt65e96b42015-07-31 11:45:22 -0700141 case 1:
joshualittaa2f6582015-07-29 10:14:58 -0700142 format = SkMask::kA8_Format;
143 break;
joshualitt65e96b42015-07-31 11:45:22 -0700144 case 2:
joshualittaa2f6582015-07-29 10:14:58 -0700145 format = SkMask::kARGB32_Format;
146 break;
joshualittd45fb5a2015-08-01 10:33:40 -0700147 case 3:
148 format = SkMask::kBW_Format;
149 break;
joshualittaa2f6582015-07-29 10:14:58 -0700150 }
151 const_cast<SkGlyph&>(glyph).fMaskFormat = format;
152
153 // if the format is ARGB, we just draw the glyph from path ourselves. Otherwise, we force
154 // our proxy context to generate the image from paths.
joshualitt65e96b42015-07-31 11:45:22 -0700155 if (!fFakeIt) {
156 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
157 SkPath path;
Ben Wagner6e9ac122016-11-11 14:31:06 -0500158 fProxy->getPath(glyph.getPackedID(), &path);
joshualittaa2f6582015-07-29 10:14:58 -0700159
joshualitt65e96b42015-07-31 11:45:22 -0700160 SkBitmap bm;
161 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
162 glyph.fImage, glyph.rowBytes());
163 bm.eraseColor(0);
joshualittaa2f6582015-07-29 10:14:58 -0700164
joshualitt65e96b42015-07-31 11:45:22 -0700165 SkCanvas canvas(bm);
166 canvas.translate(-SkIntToScalar(glyph.fLeft),
167 -SkIntToScalar(glyph.fTop));
bungeman7cfd46a2016-10-20 16:06:52 -0400168 canvas.drawPath(path, this->getRandomTypeface()->paint());
joshualitt65e96b42015-07-31 11:45:22 -0700169 } else {
170 fProxy->forceGenerateImageFromPath();
171 fProxy->getImage(glyph);
172 fProxy->forceOffGenerateImageFromPath();
173 }
joshualittaa2f6582015-07-29 10:14:58 -0700174 } else {
joshualitt65e96b42015-07-31 11:45:22 -0700175 sk_bzero(glyph.fImage, glyph.computeImageSize());
joshualittaa2f6582015-07-29 10:14:58 -0700176 }
177}
178
Ben Wagner6e9ac122016-11-11 14:31:06 -0500179void SkRandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) {
180 fProxy->generatePath(glyph, path);
joshualittaa2f6582015-07-29 10:14:58 -0700181}
182
183void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
184 fProxy->getFontMetrics(metrics);
joshualittaa2f6582015-07-29 10:14:58 -0700185}
186
187///////////////////////////////////////////////////////////////////////////////
188
189#include "SkTypefaceCache.h"
190
bungeman13b9c952016-05-12 10:09:30 -0700191SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
bungemane3aea102016-07-13 05:16:58 -0700192 : SkTypeface(proxy->fontStyle(), false)
bungeman13b9c952016-05-12 10:09:30 -0700193 , fProxy(std::move(proxy))
joshualitt65e96b42015-07-31 11:45:22 -0700194 , fPaint(paint)
195 , fFakeIt(fakeIt) {}
joshualittaa2f6582015-07-29 10:14:58 -0700196
reeda9322c22016-04-12 06:47:05 -0700197SkScalerContext* SkRandomTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
198 const SkDescriptor* desc) const {
bungeman7cfd46a2016-10-20 16:06:52 -0400199 return new SkRandomScalerContext(sk_ref_sp(const_cast<SkRandomTypeface*>(this)),
200 effects, desc, fFakeIt);
joshualittaa2f6582015-07-29 10:14:58 -0700201}
202
203void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
204 fProxy->filterRec(rec);
205 rec->setHinting(SkPaint::kNo_Hinting);
206 rec->fMaskFormat = SkMask::kARGB32_Format;
207}
208
Hal Canary209e4b12017-05-04 14:23:55 -0400209std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const {
210 return fProxy->getAdvancedMetrics();
joshualittaa2f6582015-07-29 10:14:58 -0700211}
212
213SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const {
214 return fProxy->openStream(ttcIndex);
215}
216
217void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
218 bool* isLocal) const {
219 fProxy->getFontDescriptor(desc, isLocal);
220}
221
222int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
223 uint16_t glyphs[], int glyphCount) const {
224 return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
225}
226
227int SkRandomTypeface::onCountGlyphs() const {
228 return fProxy->countGlyphs();
229}
230
231int SkRandomTypeface::onGetUPEM() const {
232 return fProxy->getUnitsPerEm();
233}
234
235void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
236 fProxy->getFamilyName(familyName);
237}
238
239SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
240 return fProxy->createFamilyNameIterator();
241}
242
Ben Wagnerfc497342017-02-24 11:15:26 -0500243int SkRandomTypeface::onGetVariationDesignPosition(
244 SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
245{
246 return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
247}
248
joshualittaa2f6582015-07-29 10:14:58 -0700249int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
250 return fProxy->getTableTags(tags);
251}
252
253size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
254 size_t length, void* data) const {
255 return fProxy->getTableData(tag, offset, length, data);
256}
257