blob: feeca48073a2334058d408e89c2c422502393ce8 [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"
Mike Reed1f275852018-04-11 14:30:17 -040015#include "SkRectPriv.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 Wagner5ddb3082018-03-29 11:18:06 -040030 bool 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 Wagner5ddb3082018-03-29 11:18:06 -040089 sk_ignore_unused_variable(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 {
Ben Wagner4b5e4b32018-04-19 10:43:05 -0400103 SkPath devPath;
104 this->internalGetPath(glyph->getPackedID(), &devPath);
joshualitt65e96b42015-07-31 11:45:22 -0700105
106 // just use devPath
107 const SkIRect ir = devPath.getBounds().roundOut();
108
Mike Reed1f275852018-04-11 14:30:17 -0400109 if (ir.isEmpty() || !SkRectPriv::Is16Bit(ir)) {
joshualitt65e96b42015-07-31 11:45:22 -0700110 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;
Ben Wagner5ddb3082018-03-29 11:18:06 -0400157 sk_ignore_unused_variable(fProxy->getPath(glyph.getPackedID(), &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));
bungeman7cfd46a2016-10-20 16:06:52 -0400167 canvas.drawPath(path, this->getRandomTypeface()->paint());
joshualitt65e96b42015-07-31 11:45:22 -0700168 } 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
Ben Wagner5ddb3082018-03-29 11:18:06 -0400178bool SkRandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) {
179 return fProxy->generatePath(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
bungeman13b9c952016-05-12 10:09:30 -0700190SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
bungemane3aea102016-07-13 05:16:58 -0700191 : SkTypeface(proxy->fontStyle(), false)
bungeman13b9c952016-05-12 10:09:30 -0700192 , fProxy(std::move(proxy))
joshualitt65e96b42015-07-31 11:45:22 -0700193 , fPaint(paint)
194 , fFakeIt(fakeIt) {}
joshualittaa2f6582015-07-29 10:14:58 -0700195
reeda9322c22016-04-12 06:47:05 -0700196SkScalerContext* SkRandomTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
197 const SkDescriptor* desc) const {
bungeman7cfd46a2016-10-20 16:06:52 -0400198 return new SkRandomScalerContext(sk_ref_sp(const_cast<SkRandomTypeface*>(this)),
199 effects, desc, fFakeIt);
joshualittaa2f6582015-07-29 10:14:58 -0700200}
201
202void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
203 fProxy->filterRec(rec);
204 rec->setHinting(SkPaint::kNo_Hinting);
205 rec->fMaskFormat = SkMask::kARGB32_Format;
206}
207
Hal Canary209e4b12017-05-04 14:23:55 -0400208std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const {
209 return fProxy->getAdvancedMetrics();
joshualittaa2f6582015-07-29 10:14:58 -0700210}
211
212SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const {
213 return fProxy->openStream(ttcIndex);
214}
215
216void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
217 bool* isLocal) const {
218 fProxy->getFontDescriptor(desc, isLocal);
219}
220
221int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
222 uint16_t glyphs[], int glyphCount) const {
223 return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
224}
225
226int SkRandomTypeface::onCountGlyphs() const {
227 return fProxy->countGlyphs();
228}
229
230int SkRandomTypeface::onGetUPEM() const {
231 return fProxy->getUnitsPerEm();
232}
233
234void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
235 fProxy->getFamilyName(familyName);
236}
237
238SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
239 return fProxy->createFamilyNameIterator();
240}
241
Ben Wagnerfc497342017-02-24 11:15:26 -0500242int SkRandomTypeface::onGetVariationDesignPosition(
243 SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
244{
245 return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
246}
247
joshualittaa2f6582015-07-29 10:14:58 -0700248int 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