blob: 460907f1649a6df540066312aa20542919d1edb5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
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
Kevin Lubickc456b732017-01-11 17:21:57 +00008#include "SkScalerContext.h"
Hal Canary95e3c052017-01-11 12:44:43 -05009
10#include "SkAutoMalloc.h"
robertphillipsc5035e72016-03-17 06:58:39 -070011#include "SkAutoPixmapStorage.h"
Cary Clarka4083c92017-09-15 11:59:23 -040012#include "SkColorData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkDescriptor.h"
14#include "SkDraw.h"
bungeman@google.combbe50132012-07-24 20:33:21 +000015#include "SkGlyph.h"
bungeman7cfd46a2016-10-20 16:06:52 -040016#include "SkMakeUnique.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkMaskFilter.h"
bungeman@google.com97efada2012-07-30 20:40:50 +000018#include "SkMaskGamma.h"
bungeman5f14c5e2014-12-05 12:26:44 -080019#include "SkMatrix22.h"
Mike Reed7120b2d2017-05-09 12:16:04 -040020#include "SkPaintPriv.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000021#include "SkPathEffect.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000022#include "SkRasterClip.h"
Hal Canary95e3c052017-01-11 12:44:43 -050023#include "SkRasterizer.h"
24#include "SkReadBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkStroke.h"
halcanary435657f2015-09-15 12:53:07 -070026#include "SkStrokeRec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050027#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000028
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3)
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031void SkGlyph::toMask(SkMask* mask) const {
32 SkASSERT(mask);
33
34 mask->fImage = (uint8_t*)fImage;
35 mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight);
36 mask->fRowBytes = this->rowBytes();
reed@android.com6c14b432009-03-23 20:11:11 +000037 mask->fFormat = static_cast<SkMask::Format>(fMaskFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038}
39
40size_t SkGlyph::computeImageSize() const {
agl@chromium.org309485b2009-07-21 17:41:32 +000041 const size_t size = this->rowBytes() * fHeight;
42
43 switch (fMaskFormat) {
reed@google.com7db9fe62011-04-11 11:57:54 +000044 case SkMask::k3D_Format:
45 return 3 * size;
46 default:
47 return size;
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000049}
50
reed@android.com62900b42009-02-11 15:07:19 +000051void SkGlyph::zeroMetrics() {
52 fAdvanceX = 0;
53 fAdvanceY = 0;
54 fWidth = 0;
55 fHeight = 0;
56 fTop = 0;
57 fLeft = 0;
58 fRsbDelta = 0;
59 fLsbDelta = 0;
60}
61
62///////////////////////////////////////////////////////////////////////////////
63
reed@android.com8a1c16f2008-12-17 15:59:43 +000064#ifdef SK_DEBUG
65 #define DUMP_RECx
66#endif
67
bungeman7cfd46a2016-10-20 16:06:52 -040068SkScalerContext::SkScalerContext(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
reeda9322c22016-04-12 06:47:05 -070069 const SkDescriptor* desc)
halcanary96fcdcc2015-08-27 07:41:13 -070070 : fRec(*static_cast<const Rec*>(desc->findEntry(kRec_SkDescriptorTag, nullptr)))
bungeman@google.coma76de722012-10-26 19:35:54 +000071
bungeman7cfd46a2016-10-20 16:06:52 -040072 , fTypeface(std::move(typeface))
reeda9322c22016-04-12 06:47:05 -070073 , fPathEffect(sk_ref_sp(effects.fPathEffect))
74 , fMaskFilter(sk_ref_sp(effects.fMaskFilter))
75 , fRasterizer(sk_ref_sp(effects.fRasterizer))
bungeman@google.coma76de722012-10-26 19:35:54 +000076 // Initialize based on our settings. Subclasses can also force this.
halcanary96fcdcc2015-08-27 07:41:13 -070077 , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != nullptr || fRasterizer != nullptr)
bungeman@google.coma76de722012-10-26 19:35:54 +000078
bungeman@google.coma76de722012-10-26 19:35:54 +000079 , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec))
80 , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec)
81 : SkMaskGamma::PreBlend())
reed@android.com8a1c16f2008-12-17 15:59:43 +000082{
reed@android.com8a1c16f2008-12-17 15:59:43 +000083#ifdef DUMP_REC
84 desc->assertChecksum();
commit-bot@chromium.org34abef12014-02-17 15:28:55 +000085 SkDebugf("SkScalerContext checksum %x count %d length %d\n",
reed@google.com7db9fe62011-04-11 11:57:54 +000086 desc->getChecksum(), desc->getCount(), desc->getLength());
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n",
88 rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0],
89 rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]);
caryclarkd7ea92f2016-03-16 07:34:02 -070090 SkDebugf(" frame %g miter %g hints %d framefill %d format %d join %d cap %d\n",
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill,
caryclarkd7ea92f2016-03-16 07:34:02 -070092 rec->fMaskFormat, rec->fStrokeJoin, rec->fStrokeCap);
reed@google.com7db9fe62011-04-11 11:57:54 +000093 SkDebugf(" pathEffect %x maskFilter %x\n",
halcanary96fcdcc2015-08-27 07:41:13 -070094 desc->findEntry(kPathEffect_SkDescriptorTag, nullptr),
95 desc->findEntry(kMaskFilter_SkDescriptorTag, nullptr));
reed@android.com8a1c16f2008-12-17 15:59:43 +000096#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000097}
98
reeda9322c22016-04-12 06:47:05 -070099SkScalerContext::~SkScalerContext() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101void SkScalerContext::getAdvance(SkGlyph* glyph) {
102 // mark us as just having a valid advance
103 glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE;
104 // we mark the format before making the call, in case the impl
105 // internally ends up calling its generateMetrics, which is OK
106 // albeit slower than strictly necessary
djsollen1b277042014-08-06 06:58:06 -0700107 generateAdvance(glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108}
109
110void SkScalerContext::getMetrics(SkGlyph* glyph) {
djsollen1b277042014-08-06 06:58:06 -0700111 generateMetrics(glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112
113 // for now we have separate cache entries for devkerning on and off
114 // in the future we might share caches, but make our measure/draw
115 // code make the distinction. Thus we zap the values if the caller
116 // has not asked for them.
117 if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) {
118 // no devkern, so zap the fields
119 glyph->fLsbDelta = glyph->fRsbDelta = 0;
120 }
121
122 // if either dimension is empty, zap the image bounds of the glyph
123 if (0 == glyph->fWidth || 0 == glyph->fHeight) {
124 glyph->fWidth = 0;
125 glyph->fHeight = 0;
126 glyph->fTop = 0;
127 glyph->fLeft = 0;
128 glyph->fMaskFormat = 0;
129 return;
130 }
reed@google.com82065d62011-02-07 15:30:46 +0000131
reed@google.coma767fa02011-08-05 21:40:26 +0000132 if (fGenerateImageFromPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 SkPath devPath, fillPath;
134 SkMatrix fillToDevMatrix;
135
Ben Wagner6e9ac122016-11-11 14:31:06 -0500136 this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137
138 if (fRasterizer) {
139 SkMask mask;
140
halcanary96fcdcc2015-08-27 07:41:13 -0700141 if (fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr,
reeda9322c22016-04-12 06:47:05 -0700142 fMaskFilter.get(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 SkMask::kJustComputeBounds_CreateMode)) {
144 glyph->fLeft = mask.fBounds.fLeft;
145 glyph->fTop = mask.fBounds.fTop;
146 glyph->fWidth = SkToU16(mask.fBounds.width());
147 glyph->fHeight = SkToU16(mask.fBounds.height());
148 } else {
senorblanco@chromium.org7767cd82009-11-25 17:14:32 +0000149 goto SK_ERROR;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 }
151 } else {
152 // just use devPath
reedb07a94f2014-11-19 05:03:18 -0800153 const SkIRect ir = devPath.getBounds().roundOut();
reed@google.com82065d62011-02-07 15:30:46 +0000154
reed@android.comb757f8d2009-12-08 22:02:26 +0000155 if (ir.isEmpty() || !ir.is16Bit()) {
senorblanco@chromium.org7767cd82009-11-25 17:14:32 +0000156 goto SK_ERROR;
reed@android.comd4577752009-11-21 02:48:11 +0000157 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 glyph->fLeft = ir.fLeft;
159 glyph->fTop = ir.fTop;
160 glyph->fWidth = SkToU16(ir.width());
161 glyph->fHeight = SkToU16(ir.height());
bungeman@google.com0abbff92013-07-27 20:37:56 +0000162
163 if (glyph->fWidth > 0) {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000164 switch (fRec.fMaskFormat) {
165 case SkMask::kLCD16_Format:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000166 glyph->fWidth += 2;
167 glyph->fLeft -= 1;
168 break;
169 default:
170 break;
171 }
bungeman@google.com0abbff92013-07-27 20:37:56 +0000172 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 }
174 }
175
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000176 if (SkMask::kARGB32_Format != glyph->fMaskFormat) {
177 glyph->fMaskFormat = fRec.fMaskFormat;
178 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179
reed@google.com5bdfb332013-05-02 18:55:44 +0000180 // If we are going to create the mask, then we cannot keep the color
181 if ((fGenerateImageFromPath || fMaskFilter) &&
182 SkMask::kARGB32_Format == glyph->fMaskFormat) {
183 glyph->fMaskFormat = SkMask::kA8_Format;
184 }
185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 if (fMaskFilter) {
187 SkMask src, dst;
188 SkMatrix matrix;
189
190 glyph->toMask(&src);
191 fRec.getMatrixFrom2x2(&matrix);
192
halcanary96fcdcc2015-08-27 07:41:13 -0700193 src.fImage = nullptr; // only want the bounds from the filter
194 if (fMaskFilter->filterMask(&dst, src, matrix, nullptr)) {
reed@google.com8136d582012-09-21 17:38:06 +0000195 if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) {
196 goto SK_ERROR;
197 }
halcanary96fcdcc2015-08-27 07:41:13 -0700198 SkASSERT(dst.fImage == nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 glyph->fLeft = dst.fBounds.fLeft;
200 glyph->fTop = dst.fBounds.fTop;
201 glyph->fWidth = SkToU16(dst.fBounds.width());
202 glyph->fHeight = SkToU16(dst.fBounds.height());
203 glyph->fMaskFormat = dst.fFormat;
204 }
205 }
reed@android.comd4577752009-11-21 02:48:11 +0000206 return;
reed@google.com82065d62011-02-07 15:30:46 +0000207
senorblanco@chromium.org7767cd82009-11-25 17:14:32 +0000208SK_ERROR:
reed@android.comd4577752009-11-21 02:48:11 +0000209 // draw nothing 'cause we failed
210 glyph->fLeft = 0;
211 glyph->fTop = 0;
212 glyph->fWidth = 0;
213 glyph->fHeight = 0;
reed@android.comb757f8d2009-12-08 22:02:26 +0000214 // put a valid value here, in case it was earlier set to
215 // MASK_FORMAT_JUST_ADVANCE
216 glyph->fMaskFormat = fRec.fMaskFormat;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217}
218
bungeman@google.com0abbff92013-07-27 20:37:56 +0000219#define SK_SHOW_TEXT_BLIT_COVERAGE 0
bungeman@google.coma76de722012-10-26 19:35:54 +0000220
221static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) {
222 uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage;
223 unsigned rowBytes = mask.fRowBytes;
224
225 for (int y = mask.fBounds.height() - 1; y >= 0; --y) {
226 for (int x = mask.fBounds.width() - 1; x >= 0; --x) {
227 dst[x] = lut[dst[x]];
228 }
229 dst += rowBytes;
230 }
231}
232
bungeman@google.com97efada2012-07-30 20:40:50 +0000233template<bool APPLY_PREBLEND>
reed41e010c2015-06-09 12:16:53 -0700234static void pack4xHToLCD16(const SkPixmap& src, const SkMask& dst,
bungeman@google.coma76de722012-10-26 19:35:54 +0000235 const SkMaskGamma::PreBlend& maskPreBlend) {
bungeman@google.com0abbff92013-07-27 20:37:56 +0000236#define SAMPLES_PER_PIXEL 4
237#define LCD_PER_PIXEL 3
reed@google.com900ecf22014-02-20 20:55:37 +0000238 SkASSERT(kAlpha_8_SkColorType == src.colorType());
reed@google.comcf598b12011-08-22 21:13:23 +0000239 SkASSERT(SkMask::kLCD16_Format == dst.fFormat);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000240
bungeman@google.com0abbff92013-07-27 20:37:56 +0000241 const int sample_width = src.width();
242 const int height = src.height();
243
reed@google.comcf598b12011-08-22 21:13:23 +0000244 uint16_t* dstP = (uint16_t*)dst.fImage;
245 size_t dstRB = dst.fRowBytes;
bungeman@google.com0abbff92013-07-27 20:37:56 +0000246 // An N tap FIR is defined by
247 // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N]
248 // or
249 // out[n] = sum(i, 0, N, coeff[i]*x[n-i])
250
251 // The strategy is to use one FIR (different coefficients) for each of r, g, and b.
252 // This means using every 4th FIR output value of each FIR and discarding the rest.
253 // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'.
254 // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.)
skia.committer@gmail.com27e21fe2013-07-28 07:01:03 +0000255
bungeman@google.com0abbff92013-07-27 20:37:56 +0000256 // These are in some fixed point repesentation.
257 // Adding up to more than one simulates ink spread.
258 // For implementation reasons, these should never add up to more than two.
259
260 // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast').
261 // Calculated using tools/generate_fir_coeff.py
262 // With this one almost no fringing is ever seen, but it is imperceptibly blurry.
263 // The lcd smoothed text is almost imperceptibly different from gray,
264 // but is still sharper on small stems and small rounded corners than gray.
265 // This also seems to be about as wide as one can get and only have a three pixel kernel.
266 // TODO: caculate these at runtime so parameters can be adjusted (esp contrast).
267 static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = {
268 //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted.
269 { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, },
270 //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric
271 { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, },
272 //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted.
273 { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, },
274 };
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000275
reed@google.comcf598b12011-08-22 21:13:23 +0000276 for (int y = 0; y < height; ++y) {
reed41e010c2015-06-09 12:16:53 -0700277 const uint8_t* srcP = src.addr8(0, y);
bungeman@google.com0abbff92013-07-27 20:37:56 +0000278
279 // TODO: this fir filter implementation is straight forward, but slow.
280 // It should be possible to make it much faster.
281 for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) {
282 int fir[LCD_PER_PIXEL] = { 0 };
283 for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4)
284 ; sample_index < SkMin32(sample_x + 8, sample_width)
285 ; ++sample_index, ++coeff_index)
286 {
287 int sample_value = srcP[sample_index];
288 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
289 fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value;
290 }
291 }
292 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
293 fir[subpxl_index] /= 0x100;
294 fir[subpxl_index] = SkMin32(fir[subpxl_index], 255);
295 }
296
297 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(fir[0], maskPreBlend.fR);
298 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(fir[1], maskPreBlend.fG);
299 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(fir[2], maskPreBlend.fB);
300#if SK_SHOW_TEXT_BLIT_COVERAGE
301 r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10);
302#endif
303 dstP[pixel_x] = SkPack888ToRGB16(r, g, b);
reed@google.coma767fa02011-08-05 21:40:26 +0000304 }
reed@google.comcf598b12011-08-22 21:13:23 +0000305 dstP = (uint16_t*)((char*)dstP + dstRB);
306 }
307}
308
rmistry@google.comd6bab022013-12-02 13:50:38 +0000309static inline int convert_8_to_1(unsigned byte) {
310 SkASSERT(byte <= 0xFF);
311 return byte >> 7;
312}
313
314static uint8_t pack_8_to_1(const uint8_t alpha[8]) {
315 unsigned bits = 0;
316 for (int i = 0; i < 8; ++i) {
317 bits <<= 1;
318 bits |= convert_8_to_1(alpha[i]);
319 }
320 return SkToU8(bits);
321}
322
323static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) {
324 const int height = mask.fBounds.height();
325 const int width = mask.fBounds.width();
326 const int octs = width >> 3;
327 const int leftOverBits = width & 7;
328
329 uint8_t* dst = mask.fImage;
330 const int dstPad = mask.fRowBytes - SkAlign8(width)/8;
331 SkASSERT(dstPad >= 0);
332
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +0000333 SkASSERT(width >= 0);
334 SkASSERT(srcRB >= (size_t)width);
335 const size_t srcPad = srcRB - width;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000336
337 for (int y = 0; y < height; ++y) {
338 for (int i = 0; i < octs; ++i) {
339 *dst++ = pack_8_to_1(src);
340 src += 8;
341 }
342 if (leftOverBits > 0) {
343 unsigned bits = 0;
344 int shift = 7;
345 for (int i = 0; i < leftOverBits; ++i, --shift) {
robertphillips@google.comc932c9f2013-12-02 15:43:39 +0000346 bits |= convert_8_to_1(*src++) << shift;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000347 }
348 *dst++ = bits;
349 }
350 src += srcPad;
351 dst += dstPad;
352 }
353}
354
bungeman@google.coma76de722012-10-26 19:35:54 +0000355static void generateMask(const SkMask& mask, const SkPath& path,
356 const SkMaskGamma::PreBlend& maskPreBlend) {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000357 SkPaint paint;
reed@google.comcf598b12011-08-22 21:13:23 +0000358
359 int srcW = mask.fBounds.width();
360 int srcH = mask.fBounds.height();
361 int dstW = srcW;
362 int dstH = srcH;
363 int dstRB = mask.fRowBytes;
364
365 SkMatrix matrix;
366 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
367 -SkIntToScalar(mask.fBounds.fTop));
368
rmistry@google.comd6bab022013-12-02 13:50:38 +0000369 paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat);
370 switch (mask.fFormat) {
371 case SkMask::kBW_Format:
372 dstRB = 0; // signals we need a copy
373 break;
374 case SkMask::kA8_Format:
375 break;
376 case SkMask::kLCD16_Format:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000377 // TODO: trigger off LCD orientation
378 dstW = 4*dstW - 8;
379 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1),
380 -SkIntToScalar(mask.fBounds.fTop));
381 matrix.postScale(SkIntToScalar(4), SK_Scalar1);
382 dstRB = 0; // signals we need a copy
383 break;
384 default:
385 SkDEBUGFAIL("unexpected mask format");
reed@google.comcf598b12011-08-22 21:13:23 +0000386 }
387
reed@google.com045e62d2011-10-24 12:19:46 +0000388 SkRasterClip clip;
389 clip.setRect(SkIRect::MakeWH(dstW, dstH));
reed@google.comcf598b12011-08-22 21:13:23 +0000390
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000391 const SkImageInfo info = SkImageInfo::MakeA8(dstW, dstH);
reed41e010c2015-06-09 12:16:53 -0700392 SkAutoPixmapStorage dst;
reed@google.comcf598b12011-08-22 21:13:23 +0000393
394 if (0 == dstRB) {
reed41e010c2015-06-09 12:16:53 -0700395 if (!dst.tryAlloc(info)) {
reed@google.com9be57272012-12-17 14:21:38 +0000396 // can't allocate offscreen, so empty the mask and return
397 sk_bzero(mask.fImage, mask.computeImageSize());
398 return;
399 }
reed@google.comcf598b12011-08-22 21:13:23 +0000400 } else {
reed41e010c2015-06-09 12:16:53 -0700401 dst.reset(info, mask.fImage, dstRB);
reed@google.comcf598b12011-08-22 21:13:23 +0000402 }
Mike Reedcd284c52017-09-29 15:22:56 -0400403 sk_bzero(dst.writable_addr(), dst.computeByteSize());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000404
reed@google.comcf598b12011-08-22 21:13:23 +0000405 SkDraw draw;
reed41e010c2015-06-09 12:16:53 -0700406 draw.fDst = dst;
reed@google.com045e62d2011-10-24 12:19:46 +0000407 draw.fRC = &clip;
reed@google.comcf598b12011-08-22 21:13:23 +0000408 draw.fMatrix = &matrix;
reed@google.comcf598b12011-08-22 21:13:23 +0000409 draw.drawPath(path, paint);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000410
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000411 switch (mask.fFormat) {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000412 case SkMask::kBW_Format:
reed41e010c2015-06-09 12:16:53 -0700413 packA8ToA1(mask, dst.addr8(0, 0), dst.rowBytes());
rmistry@google.comd6bab022013-12-02 13:50:38 +0000414 break;
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000415 case SkMask::kA8_Format:
416 if (maskPreBlend.isApplicable()) {
417 applyLUTToA8Mask(mask, maskPreBlend.fG);
418 }
419 break;
420 case SkMask::kLCD16_Format:
421 if (maskPreBlend.isApplicable()) {
reed41e010c2015-06-09 12:16:53 -0700422 pack4xHToLCD16<true>(dst, mask, maskPreBlend);
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000423 } else {
reed41e010c2015-06-09 12:16:53 -0700424 pack4xHToLCD16<false>(dst, mask, maskPreBlend);
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000425 }
426 break;
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000427 default:
428 break;
reed@google.comcf598b12011-08-22 21:13:23 +0000429 }
430}
431
reed@google.com5bdfb332013-05-02 18:55:44 +0000432static void extract_alpha(const SkMask& dst,
433 const SkPMColor* srcRow, size_t srcRB) {
434 int width = dst.fBounds.width();
435 int height = dst.fBounds.height();
436 int dstRB = dst.fRowBytes;
437 uint8_t* dstRow = dst.fImage;
438
439 for (int y = 0; y < height; ++y) {
440 for (int x = 0; x < width; ++x) {
441 dstRow[x] = SkGetPackedA32(srcRow[x]);
442 }
443 // zero any padding on each row
444 for (int x = width; x < dstRB; ++x) {
445 dstRow[x] = 0;
446 }
447 dstRow += dstRB;
448 srcRow = (const SkPMColor*)((const char*)srcRow + srcRB);
449 }
450}
451
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452void SkScalerContext::getImage(const SkGlyph& origGlyph) {
453 const SkGlyph* glyph = &origGlyph;
454 SkGlyph tmpGlyph;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000455
reed@google.com5bdfb332013-05-02 18:55:44 +0000456 // in case we need to call generateImage on a mask-format that is different
457 // (i.e. larger) than what our caller allocated by looking at origGlyph.
458 SkAutoMalloc tmpGlyphImageStorage;
459
460 // If we are going to draw-from-path, then we cannot generate color, since
461 // the path only makes a mask. This case should have been caught up in
462 // generateMetrics().
463 SkASSERT(!fGenerateImageFromPath ||
464 SkMask::kARGB32_Format != origGlyph.fMaskFormat);
465
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 if (fMaskFilter) { // restore the prefilter bounds
Ben Wagner6e9ac122016-11-11 14:31:06 -0500467 tmpGlyph.initWithGlyphID(origGlyph.getPackedID());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468
469 // need the original bounds, sans our maskfilter
reeda9322c22016-04-12 06:47:05 -0700470 SkMaskFilter* mf = fMaskFilter.release(); // temp disable
reed@android.com8a1c16f2008-12-17 15:59:43 +0000471 this->getMetrics(&tmpGlyph);
reeda9322c22016-04-12 06:47:05 -0700472 fMaskFilter = sk_sp<SkMaskFilter>(mf); // restore
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474 // we need the prefilter bounds to be <= filter bounds
475 SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth);
476 SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight);
reed@google.com5bdfb332013-05-02 18:55:44 +0000477
478 if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) {
479 tmpGlyph.fImage = origGlyph.fImage;
480 } else {
481 tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize());
482 tmpGlyph.fImage = tmpGlyphImageStorage.get();
483 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000484 glyph = &tmpGlyph;
485 }
486
reed@google.coma767fa02011-08-05 21:40:26 +0000487 if (fGenerateImageFromPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488 SkPath devPath, fillPath;
489 SkMatrix fillToDevMatrix;
reed@google.comcf598b12011-08-22 21:13:23 +0000490 SkMask mask;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491
Ben Wagner6e9ac122016-11-11 14:31:06 -0500492 this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix);
reed@google.comcf598b12011-08-22 21:13:23 +0000493 glyph->toMask(&mask);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494
495 if (fRasterizer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496 mask.fFormat = SkMask::kA8_Format;
reed@android.com4516f472009-06-29 16:25:36 +0000497 sk_bzero(glyph->fImage, mask.computeImageSize());
reed@google.com82065d62011-02-07 15:30:46 +0000498
halcanary96fcdcc2015-08-27 07:41:13 -0700499 if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr,
reeda9322c22016-04-12 06:47:05 -0700500 fMaskFilter.get(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501 SkMask::kJustRenderImage_CreateMode)) {
502 return;
503 }
bungeman@google.coma76de722012-10-26 19:35:54 +0000504 if (fPreBlend.isApplicable()) {
505 applyLUTToA8Mask(mask, fPreBlend.fG);
bungeman@google.com97efada2012-07-30 20:40:50 +0000506 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507 } else {
reed@google.com5bdfb332013-05-02 18:55:44 +0000508 SkASSERT(SkMask::kARGB32_Format != mask.fFormat);
bungeman@google.coma76de722012-10-26 19:35:54 +0000509 generateMask(mask, devPath, fPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 }
511 } else {
djsollen1b277042014-08-06 06:58:06 -0700512 generateImage(*glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 }
514
515 if (fMaskFilter) {
516 SkMask srcM, dstM;
517 SkMatrix matrix;
518
519 // the src glyph image shouldn't be 3D
520 SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat);
reed@google.com5bdfb332013-05-02 18:55:44 +0000521
522 SkAutoSMalloc<32*32> a8storage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 glyph->toMask(&srcM);
reed@google.com5bdfb332013-05-02 18:55:44 +0000524 if (SkMask::kARGB32_Format == srcM.fFormat) {
525 // now we need to extract the alpha-channel from the glyph's image
526 // and copy it into a temp buffer, and then point srcM at that temp.
527 srcM.fFormat = SkMask::kA8_Format;
528 srcM.fRowBytes = SkAlign4(srcM.fBounds.width());
529 size_t size = srcM.computeImageSize();
530 a8storage.reset(size);
531 srcM.fImage = (uint8_t*)a8storage.get();
532 extract_alpha(srcM,
533 (const SkPMColor*)glyph->fImage, glyph->rowBytes());
534 }
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000535
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 fRec.getMatrixFrom2x2(&matrix);
537
halcanary96fcdcc2015-08-27 07:41:13 -0700538 if (fMaskFilter->filterMask(&dstM, srcM, matrix, nullptr)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width());
540 int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height());
541 int dstRB = origGlyph.rowBytes();
542 int srcRB = dstM.fRowBytes;
reed@google.com82065d62011-02-07 15:30:46 +0000543
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 const uint8_t* src = (const uint8_t*)dstM.fImage;
545 uint8_t* dst = (uint8_t*)origGlyph.fImage;
546
547 if (SkMask::k3D_Format == dstM.fFormat) {
548 // we have to copy 3 times as much
549 height *= 3;
550 }
551
552 // clean out our glyph, since it may be larger than dstM
reed@android.com4516f472009-06-29 16:25:36 +0000553 //sk_bzero(dst, height * dstRB);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000554
555 while (--height >= 0) {
556 memcpy(dst, src, width);
557 src += srcRB;
558 dst += dstRB;
559 }
560 SkMask::FreeImage(dstM.fImage);
bungeman@google.com97efada2012-07-30 20:40:50 +0000561
bungeman@google.coma76de722012-10-26 19:35:54 +0000562 if (fPreBlendForFilter.isApplicable()) {
563 applyLUTToA8Mask(srcM, fPreBlendForFilter.fG);
564 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565 }
566 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000567}
568
Ben Wagner6e9ac122016-11-11 14:31:06 -0500569void SkScalerContext::getPath(SkPackedGlyphID glyphID, SkPath* path) {
570 this->internalGetPath(glyphID, nullptr, path, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571}
572
reed@google.com0a01f5a2013-05-08 14:19:08 +0000573void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) {
Mike Reed8be952a2017-02-13 20:44:33 -0500574 SkASSERT(fm);
bungeman41078062014-07-07 08:16:37 -0700575 this->generateFontMetrics(fm);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576}
577
reed@android.com9d3a9852010-01-08 14:07:42 +0000578SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) {
579 return 0;
580}
581
reed@google.com7db9fe62011-04-11 11:57:54 +0000582///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583
Ben Wagner6e9ac122016-11-11 14:31:06 -0500584void SkScalerContext::internalGetPath(SkPackedGlyphID glyphID, SkPath* fillPath,
585 SkPath* devPath, SkMatrix* fillToDevMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000586 SkPath path;
Ben Wagner6e9ac122016-11-11 14:31:06 -0500587 generatePath(glyphID.code(), &path);
reed@google.com82065d62011-02-07 15:30:46 +0000588
reed@google.comcf598b12011-08-22 21:13:23 +0000589 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
Ben Wagner6e9ac122016-11-11 14:31:06 -0500590 SkFixed dx = glyphID.getSubXFixed();
591 SkFixed dy = glyphID.getSubYFixed();
reed@google.comcf598b12011-08-22 21:13:23 +0000592 if (dx | dy) {
593 path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy));
594 }
595 }
596
halcanary96fcdcc2015-08-27 07:41:13 -0700597 if (fRec.fFrameWidth > 0 || fPathEffect != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598 // need the path in user-space, with only the point-size applied
599 // so that our stroking and effects will operate the same way they
600 // would if the user had extracted the path themself, and then
601 // called drawPath
602 SkPath localPath;
603 SkMatrix matrix, inverse;
604
605 fRec.getMatrixFrom2x2(&matrix);
bungeman@google.com9575fb82012-04-09 20:49:03 +0000606 if (!matrix.invert(&inverse)) {
607 // assume fillPath and devPath are already empty.
608 return;
609 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000610 path.transform(inverse, &localPath);
611 // now localPath is only affected by the paint settings, and not the canvas matrix
612
reed@google.comfd4be262012-05-25 01:04:12 +0000613 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000614
reed@google.comfd4be262012-05-25 01:04:12 +0000615 if (fRec.fFrameWidth > 0) {
616 rec.setStrokeStyle(fRec.fFrameWidth,
617 SkToBool(fRec.fFlags & kFrameAndFill_Flag));
618 // glyphs are always closed contours, so cap type is ignored,
619 // so we just pass something.
caryclarkd7ea92f2016-03-16 07:34:02 -0700620 rec.setStrokeParams((SkPaint::Cap)fRec.fStrokeCap,
reed@google.comfd4be262012-05-25 01:04:12 +0000621 (SkPaint::Join)fRec.fStrokeJoin,
622 fRec.fMiterLimit);
623 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000624
reed@google.com7db9fe62011-04-11 11:57:54 +0000625 if (fPathEffect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 SkPath effectPath;
halcanary96fcdcc2015-08-27 07:41:13 -0700627 if (fPathEffect->filterPath(&effectPath, localPath, &rec, nullptr)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000628 localPath.swap(effectPath);
reed@google.com7db9fe62011-04-11 11:57:54 +0000629 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630 }
631
reed@google.comfd4be262012-05-25 01:04:12 +0000632 if (rec.needToApply()) {
633 SkPath strokePath;
634 if (rec.applyToPath(&strokePath, localPath)) {
635 localPath.swap(strokePath);
636 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 }
reed@google.com82065d62011-02-07 15:30:46 +0000638
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639 // now return stuff to the caller
reed@google.com7db9fe62011-04-11 11:57:54 +0000640 if (fillToDevMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000641 *fillToDevMatrix = matrix;
reed@google.com7db9fe62011-04-11 11:57:54 +0000642 }
643 if (devPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000644 localPath.transform(matrix, devPath);
reed@google.com7db9fe62011-04-11 11:57:54 +0000645 }
646 if (fillPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 fillPath->swap(localPath);
reed@google.com7db9fe62011-04-11 11:57:54 +0000648 }
649 } else { // nothing tricky to do
650 if (fillToDevMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000651 fillToDevMatrix->reset();
reed@google.com7db9fe62011-04-11 11:57:54 +0000652 }
653 if (devPath) {
halcanary96fcdcc2015-08-27 07:41:13 -0700654 if (fillPath == nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655 devPath->swap(path);
reed@google.com7db9fe62011-04-11 11:57:54 +0000656 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657 *devPath = path;
reed@google.com7db9fe62011-04-11 11:57:54 +0000658 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000659 }
reed@google.com82065d62011-02-07 15:30:46 +0000660
reed@google.com7db9fe62011-04-11 11:57:54 +0000661 if (fillPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000662 fillPath->swap(path);
reed@google.com7db9fe62011-04-11 11:57:54 +0000663 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664 }
reed@google.com82065d62011-02-07 15:30:46 +0000665
reed@google.com7db9fe62011-04-11 11:57:54 +0000666 if (devPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000667 devPath->updateBoundsCache();
reed@google.com7db9fe62011-04-11 11:57:54 +0000668 }
669 if (fillPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670 fillPath->updateBoundsCache();
reed@google.com7db9fe62011-04-11 11:57:54 +0000671 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000672}
673
674
reed@google.coma9d4e842012-08-14 19:13:55 +0000675void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const {
bungeman@google.com50dd4102013-01-14 18:25:55 +0000676 dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0,
677 fPost2x2[1][0], fPost2x2[1][1], 0,
reed3f43f8a2015-01-20 19:58:36 -0800678 0, 0, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000679}
680
reed@google.coma9d4e842012-08-14 19:13:55 +0000681void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const {
Mike Reed7120b2d2017-05-09 12:16:04 -0400682 SkPaintPriv::MakeTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000683}
684
reed@google.coma9d4e842012-08-14 19:13:55 +0000685void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000686 this->getLocalMatrix(m);
687
688 // now concat the device matrix
reed@google.com7db9fe62011-04-11 11:57:54 +0000689 SkMatrix deviceMatrix;
690 this->getMatrixFrom2x2(&deviceMatrix);
691 m->postConcat(deviceMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000692}
693
bungeman1f0e78d2016-08-23 13:19:01 -0700694bool SkScalerContextRec::computeMatrices(PreMatrixScale preMatrixScale, SkVector* s, SkMatrix* sA,
bungeman5f14c5e2014-12-05 12:26:44 -0800695 SkMatrix* GsA, SkMatrix* G_inv, SkMatrix* A_out)
696{
697 // A is the 'total' matrix.
698 SkMatrix A;
699 this->getSingleMatrix(&A);
700
701 // The caller may find the 'total' matrix useful when dealing directly with EM sizes.
702 if (A_out) {
703 *A_out = A;
704 }
705
706 // GA is the matrix A with rotation removed.
707 SkMatrix GA;
708 bool skewedOrFlipped = A.getSkewX() || A.getSkewY() || A.getScaleX() < 0 || A.getScaleY() < 0;
709 if (skewedOrFlipped) {
Ben Wagner453888f2017-06-15 15:41:42 -0400710 // QR by Givens rotations. G is Q^T and GA is R. G is rotational (no reflections).
bungeman5f14c5e2014-12-05 12:26:44 -0800711 // h is where A maps the horizontal baseline.
712 SkPoint h = SkPoint::Make(SK_Scalar1, 0);
713 A.mapPoints(&h, 1);
714
715 // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0).
716 SkMatrix G;
717 SkComputeGivensRotation(h, &G);
718
719 GA = G;
720 GA.preConcat(A);
721
722 // The 'remainingRotation' is G inverse, which is fairly simple since G is 2x2 rotational.
723 if (G_inv) {
724 G_inv->setAll(
725 G.get(SkMatrix::kMScaleX), -G.get(SkMatrix::kMSkewX), G.get(SkMatrix::kMTransX),
726 -G.get(SkMatrix::kMSkewY), G.get(SkMatrix::kMScaleY), G.get(SkMatrix::kMTransY),
727 G.get(SkMatrix::kMPersp0), G.get(SkMatrix::kMPersp1), G.get(SkMatrix::kMPersp2));
728 }
729 } else {
730 GA = A;
731 if (G_inv) {
732 G_inv->reset();
733 }
734 }
735
Ben Wagner453888f2017-06-15 15:41:42 -0400736 // If the 'total' matrix is singular, set the 'scale' to something finite and zero the matrices.
737 // All underlying ports have issues with zero text size, so use the matricies to zero.
738 // If one of the scale factors is less than 1/256 then an EM filling square will
739 // never affect any pixels.
740 if (SkScalarAbs(GA.get(SkMatrix::kMScaleX)) <= SK_ScalarNearlyZero ||
741 SkScalarAbs(GA.get(SkMatrix::kMScaleY)) <= SK_ScalarNearlyZero)
742 {
743 s->fX = SK_Scalar1;
744 s->fY = SK_Scalar1;
745 sA->setScale(0, 0);
746 if (GsA) {
747 GsA->setScale(0, 0);
748 }
749 if (G_inv) {
750 G_inv->reset();
751 }
752 return false;
753 }
754
bungeman5f14c5e2014-12-05 12:26:44 -0800755 // At this point, given GA, create s.
756 switch (preMatrixScale) {
757 case kFull_PreMatrixScale:
758 s->fX = SkScalarAbs(GA.get(SkMatrix::kMScaleX));
759 s->fY = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
760 break;
761 case kVertical_PreMatrixScale: {
762 SkScalar yScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
763 s->fX = yScale;
764 s->fY = yScale;
765 break;
766 }
767 case kVerticalInteger_PreMatrixScale: {
768 SkScalar realYScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
769 SkScalar intYScale = SkScalarRoundToScalar(realYScale);
770 if (intYScale == 0) {
771 intYScale = SK_Scalar1;
772 }
773 s->fX = intYScale;
774 s->fY = intYScale;
775 break;
776 }
777 }
778
779 // The 'remaining' matrix sA is the total matrix A without the scale.
bungeman34902632014-12-10 21:43:27 -0800780 if (!skewedOrFlipped && (
781 (kFull_PreMatrixScale == preMatrixScale) ||
782 (kVertical_PreMatrixScale == preMatrixScale && A.getScaleX() == A.getScaleY())))
783 {
bungeman5f14c5e2014-12-05 12:26:44 -0800784 // If GA == A and kFull_PreMatrixScale, sA is identity.
bungeman34902632014-12-10 21:43:27 -0800785 // If GA == A and kVertical_PreMatrixScale and A.scaleX == A.scaleY, sA is identity.
bungeman5f14c5e2014-12-05 12:26:44 -0800786 sA->reset();
bungeman34902632014-12-10 21:43:27 -0800787 } else if (!skewedOrFlipped && kVertical_PreMatrixScale == preMatrixScale) {
788 // If GA == A and kVertical_PreMatrixScale, sA.scaleY is SK_Scalar1.
789 sA->reset();
790 sA->setScaleX(A.getScaleX() / s->fY);
bungeman5f14c5e2014-12-05 12:26:44 -0800791 } else {
bungeman5f14c5e2014-12-05 12:26:44 -0800792 // TODO: like kVertical_PreMatrixScale, kVerticalInteger_PreMatrixScale with int scales.
793 *sA = A;
794 sA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
795 }
796
797 // The 'remainingWithoutRotation' matrix GsA is the non-rotational part of A without the scale.
798 if (GsA) {
799 *GsA = GA;
800 // G is rotational so reorders with the scale.
801 GsA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
802 }
bungeman1f0e78d2016-08-23 13:19:01 -0700803
804 return true;
bungeman5f14c5e2014-12-05 12:26:44 -0800805}
806
bungeman27876bc2016-02-29 11:22:55 -0800807SkAxisAlignment SkScalerContext::computeAxisAlignmentForHText() {
808 // Why fPost2x2 can be used here.
809 // getSingleMatrix multiplies in getLocalMatrix, which consists of
810 // * fTextSize (a scale, which has no effect)
811 // * fPreScaleX (a scale in x, which has no effect)
812 // * fPreSkewX (has no effect, but would on vertical text alignment).
813 // In other words, making the text bigger, stretching it along the
814 // horizontal axis, or fake italicizing it does not move the baseline.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000815
bungeman27876bc2016-02-29 11:22:55 -0800816 if (0 == fRec.fPost2x2[1][0]) {
817 // The x axis is mapped onto the x axis.
reed@google.com2e684782011-08-24 15:38:46 +0000818 return kX_SkAxisAlignment;
819 }
bungeman27876bc2016-02-29 11:22:55 -0800820 if (0 == fRec.fPost2x2[0][0]) {
821 // The x axis is mapped onto the y axis.
reed@google.com2e684782011-08-24 15:38:46 +0000822 return kY_SkAxisAlignment;
823 }
824 return kNone_SkAxisAlignment;
825}
826
reed@android.com62900b42009-02-11 15:07:19 +0000827///////////////////////////////////////////////////////////////////////////////
828
reed@android.com62900b42009-02-11 15:07:19 +0000829class SkScalerContext_Empty : public SkScalerContext {
830public:
bungeman7cfd46a2016-10-20 16:06:52 -0400831 SkScalerContext_Empty(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
reeda9322c22016-04-12 06:47:05 -0700832 const SkDescriptor* desc)
bungeman7cfd46a2016-10-20 16:06:52 -0400833 : SkScalerContext(std::move(typeface), effects, desc) {}
reed@android.com62900b42009-02-11 15:07:19 +0000834
835protected:
mtklein36352bf2015-03-25 18:17:31 -0700836 unsigned generateGlyphCount() override {
reed@android.com62900b42009-02-11 15:07:19 +0000837 return 0;
838 }
mtklein36352bf2015-03-25 18:17:31 -0700839 uint16_t generateCharToGlyph(SkUnichar uni) override {
reed@android.com62900b42009-02-11 15:07:19 +0000840 return 0;
841 }
mtklein36352bf2015-03-25 18:17:31 -0700842 void generateAdvance(SkGlyph* glyph) override {
reed@android.com62900b42009-02-11 15:07:19 +0000843 glyph->zeroMetrics();
844 }
mtklein36352bf2015-03-25 18:17:31 -0700845 void generateMetrics(SkGlyph* glyph) override {
reed@android.com62900b42009-02-11 15:07:19 +0000846 glyph->zeroMetrics();
847 }
mtklein36352bf2015-03-25 18:17:31 -0700848 void generateImage(const SkGlyph& glyph) override {}
Ben Wagner6e9ac122016-11-11 14:31:06 -0500849 void generatePath(SkGlyphID glyph, SkPath* path) override {}
mtklein36352bf2015-03-25 18:17:31 -0700850 void generateFontMetrics(SkPaint::FontMetrics* metrics) override {
bungeman41078062014-07-07 08:16:37 -0700851 if (metrics) {
852 sk_bzero(metrics, sizeof(*metrics));
reed@android.com62900b42009-02-11 15:07:19 +0000853 }
854 }
855};
856
reed@android.comf2b98d62010-12-20 18:26:13 +0000857extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc);
858
bungeman7cfd46a2016-10-20 16:06:52 -0400859std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext(
860 const SkScalerContextEffects& effects, const SkDescriptor* desc, bool allowFailure) const
861{
862 std::unique_ptr<SkScalerContext> c(this->onCreateScalerContext(effects, desc));
reed@google.com84e22d82013-07-10 15:38:20 +0000863 if (!c && !allowFailure) {
bungeman7cfd46a2016-10-20 16:06:52 -0400864 c = skstd::make_unique<SkScalerContext_Empty>(sk_ref_sp(const_cast<SkTypeface*>(this)),
865 effects, desc);
reed@android.com62900b42009-02-11 15:07:19 +0000866 }
867 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868}