blob: 85e8994f4f19daca34885fdaba3152834b18d857 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#include "SkScalerContext.h"
robertphillipsc5035e72016-03-17 06:58:39 -070010#include "SkAutoPixmapStorage.h"
agl@chromium.org309485b2009-07-21 17:41:32 +000011#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkDescriptor.h"
13#include "SkDraw.h"
bungeman@google.combbe50132012-07-24 20:33:21 +000014#include "SkGlyph.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMaskFilter.h"
bungeman@google.com97efada2012-07-30 20:40:50 +000016#include "SkMaskGamma.h"
bungeman5f14c5e2014-12-05 12:26:44 -080017#include "SkMatrix22.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000018#include "SkReadBuffer.h"
19#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkPathEffect.h"
21#include "SkRasterizer.h"
reed@google.com045e62d2011-10-24 12:19:46 +000022#include "SkRasterClip.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkStroke.h"
halcanary435657f2015-09-15 12:53:07 -070024#include "SkStrokeRec.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3)
27
reed@android.com8a1c16f2008-12-17 15:59:43 +000028void SkGlyph::toMask(SkMask* mask) const {
29 SkASSERT(mask);
30
31 mask->fImage = (uint8_t*)fImage;
32 mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight);
33 mask->fRowBytes = this->rowBytes();
reed@android.com6c14b432009-03-23 20:11:11 +000034 mask->fFormat = static_cast<SkMask::Format>(fMaskFormat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035}
36
37size_t SkGlyph::computeImageSize() const {
agl@chromium.org309485b2009-07-21 17:41:32 +000038 const size_t size = this->rowBytes() * fHeight;
39
40 switch (fMaskFormat) {
reed@google.com7db9fe62011-04-11 11:57:54 +000041 case SkMask::k3D_Format:
42 return 3 * size;
43 default:
44 return size;
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000046}
47
reed@android.com62900b42009-02-11 15:07:19 +000048void SkGlyph::zeroMetrics() {
49 fAdvanceX = 0;
50 fAdvanceY = 0;
51 fWidth = 0;
52 fHeight = 0;
53 fTop = 0;
54 fLeft = 0;
55 fRsbDelta = 0;
56 fLsbDelta = 0;
57}
58
59///////////////////////////////////////////////////////////////////////////////
60
reed@android.com8a1c16f2008-12-17 15:59:43 +000061#ifdef SK_DEBUG
62 #define DUMP_RECx
63#endif
64
reeda9322c22016-04-12 06:47:05 -070065SkScalerContext::SkScalerContext(SkTypeface* typeface, const SkScalerContextEffects& effects,
66 const SkDescriptor* desc)
halcanary96fcdcc2015-08-27 07:41:13 -070067 : fRec(*static_cast<const Rec*>(desc->findEntry(kRec_SkDescriptorTag, nullptr)))
bungeman@google.coma76de722012-10-26 19:35:54 +000068
reeda9322c22016-04-12 06:47:05 -070069 , fTypeface(sk_ref_sp(typeface))
70 , fPathEffect(sk_ref_sp(effects.fPathEffect))
71 , fMaskFilter(sk_ref_sp(effects.fMaskFilter))
72 , fRasterizer(sk_ref_sp(effects.fRasterizer))
bungeman@google.coma76de722012-10-26 19:35:54 +000073 // Initialize based on our settings. Subclasses can also force this.
halcanary96fcdcc2015-08-27 07:41:13 -070074 , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != nullptr || fRasterizer != nullptr)
bungeman@google.coma76de722012-10-26 19:35:54 +000075
bungeman@google.coma76de722012-10-26 19:35:54 +000076 , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec))
77 , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec)
78 : SkMaskGamma::PreBlend())
reed@android.com8a1c16f2008-12-17 15:59:43 +000079{
reed@android.com8a1c16f2008-12-17 15:59:43 +000080#ifdef DUMP_REC
81 desc->assertChecksum();
commit-bot@chromium.org34abef12014-02-17 15:28:55 +000082 SkDebugf("SkScalerContext checksum %x count %d length %d\n",
reed@google.com7db9fe62011-04-11 11:57:54 +000083 desc->getChecksum(), desc->getCount(), desc->getLength());
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n",
85 rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0],
86 rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]);
caryclarkd7ea92f2016-03-16 07:34:02 -070087 SkDebugf(" frame %g miter %g hints %d framefill %d format %d join %d cap %d\n",
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill,
caryclarkd7ea92f2016-03-16 07:34:02 -070089 rec->fMaskFormat, rec->fStrokeJoin, rec->fStrokeCap);
reed@google.com7db9fe62011-04-11 11:57:54 +000090 SkDebugf(" pathEffect %x maskFilter %x\n",
halcanary96fcdcc2015-08-27 07:41:13 -070091 desc->findEntry(kPathEffect_SkDescriptorTag, nullptr),
92 desc->findEntry(kMaskFilter_SkDescriptorTag, nullptr));
reed@android.com8a1c16f2008-12-17 15:59:43 +000093#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000094}
95
reeda9322c22016-04-12 06:47:05 -070096SkScalerContext::~SkScalerContext() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098void SkScalerContext::getAdvance(SkGlyph* glyph) {
99 // mark us as just having a valid advance
100 glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE;
101 // we mark the format before making the call, in case the impl
102 // internally ends up calling its generateMetrics, which is OK
103 // albeit slower than strictly necessary
djsollen1b277042014-08-06 06:58:06 -0700104 generateAdvance(glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105}
106
107void SkScalerContext::getMetrics(SkGlyph* glyph) {
djsollen1b277042014-08-06 06:58:06 -0700108 generateMetrics(glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109
110 // for now we have separate cache entries for devkerning on and off
111 // in the future we might share caches, but make our measure/draw
112 // code make the distinction. Thus we zap the values if the caller
113 // has not asked for them.
114 if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) {
115 // no devkern, so zap the fields
116 glyph->fLsbDelta = glyph->fRsbDelta = 0;
117 }
118
119 // if either dimension is empty, zap the image bounds of the glyph
120 if (0 == glyph->fWidth || 0 == glyph->fHeight) {
121 glyph->fWidth = 0;
122 glyph->fHeight = 0;
123 glyph->fTop = 0;
124 glyph->fLeft = 0;
125 glyph->fMaskFormat = 0;
126 return;
127 }
reed@google.com82065d62011-02-07 15:30:46 +0000128
reed@google.coma767fa02011-08-05 21:40:26 +0000129 if (fGenerateImageFromPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 SkPath devPath, fillPath;
131 SkMatrix fillToDevMatrix;
132
133 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
134
135 if (fRasterizer) {
136 SkMask mask;
137
halcanary96fcdcc2015-08-27 07:41:13 -0700138 if (fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr,
reeda9322c22016-04-12 06:47:05 -0700139 fMaskFilter.get(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 SkMask::kJustComputeBounds_CreateMode)) {
141 glyph->fLeft = mask.fBounds.fLeft;
142 glyph->fTop = mask.fBounds.fTop;
143 glyph->fWidth = SkToU16(mask.fBounds.width());
144 glyph->fHeight = SkToU16(mask.fBounds.height());
145 } else {
senorblanco@chromium.org7767cd82009-11-25 17:14:32 +0000146 goto SK_ERROR;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 }
148 } else {
149 // just use devPath
reedb07a94f2014-11-19 05:03:18 -0800150 const SkIRect ir = devPath.getBounds().roundOut();
reed@google.com82065d62011-02-07 15:30:46 +0000151
reed@android.comb757f8d2009-12-08 22:02:26 +0000152 if (ir.isEmpty() || !ir.is16Bit()) {
senorblanco@chromium.org7767cd82009-11-25 17:14:32 +0000153 goto SK_ERROR;
reed@android.comd4577752009-11-21 02:48:11 +0000154 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 glyph->fLeft = ir.fLeft;
156 glyph->fTop = ir.fTop;
157 glyph->fWidth = SkToU16(ir.width());
158 glyph->fHeight = SkToU16(ir.height());
bungeman@google.com0abbff92013-07-27 20:37:56 +0000159
160 if (glyph->fWidth > 0) {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000161 switch (fRec.fMaskFormat) {
162 case SkMask::kLCD16_Format:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000163 glyph->fWidth += 2;
164 glyph->fLeft -= 1;
165 break;
166 default:
167 break;
168 }
bungeman@google.com0abbff92013-07-27 20:37:56 +0000169 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 }
171 }
172
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000173 if (SkMask::kARGB32_Format != glyph->fMaskFormat) {
174 glyph->fMaskFormat = fRec.fMaskFormat;
175 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176
reed@google.com5bdfb332013-05-02 18:55:44 +0000177 // If we are going to create the mask, then we cannot keep the color
178 if ((fGenerateImageFromPath || fMaskFilter) &&
179 SkMask::kARGB32_Format == glyph->fMaskFormat) {
180 glyph->fMaskFormat = SkMask::kA8_Format;
181 }
182
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 if (fMaskFilter) {
184 SkMask src, dst;
185 SkMatrix matrix;
186
187 glyph->toMask(&src);
188 fRec.getMatrixFrom2x2(&matrix);
189
halcanary96fcdcc2015-08-27 07:41:13 -0700190 src.fImage = nullptr; // only want the bounds from the filter
191 if (fMaskFilter->filterMask(&dst, src, matrix, nullptr)) {
reed@google.com8136d582012-09-21 17:38:06 +0000192 if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) {
193 goto SK_ERROR;
194 }
halcanary96fcdcc2015-08-27 07:41:13 -0700195 SkASSERT(dst.fImage == nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 glyph->fLeft = dst.fBounds.fLeft;
197 glyph->fTop = dst.fBounds.fTop;
198 glyph->fWidth = SkToU16(dst.fBounds.width());
199 glyph->fHeight = SkToU16(dst.fBounds.height());
200 glyph->fMaskFormat = dst.fFormat;
201 }
202 }
reed@android.comd4577752009-11-21 02:48:11 +0000203 return;
reed@google.com82065d62011-02-07 15:30:46 +0000204
senorblanco@chromium.org7767cd82009-11-25 17:14:32 +0000205SK_ERROR:
reed@android.comd4577752009-11-21 02:48:11 +0000206 // draw nothing 'cause we failed
207 glyph->fLeft = 0;
208 glyph->fTop = 0;
209 glyph->fWidth = 0;
210 glyph->fHeight = 0;
reed@android.comb757f8d2009-12-08 22:02:26 +0000211 // put a valid value here, in case it was earlier set to
212 // MASK_FORMAT_JUST_ADVANCE
213 glyph->fMaskFormat = fRec.fMaskFormat;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214}
215
bungeman@google.com0abbff92013-07-27 20:37:56 +0000216#define SK_SHOW_TEXT_BLIT_COVERAGE 0
bungeman@google.coma76de722012-10-26 19:35:54 +0000217
218static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) {
219 uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage;
220 unsigned rowBytes = mask.fRowBytes;
221
222 for (int y = mask.fBounds.height() - 1; y >= 0; --y) {
223 for (int x = mask.fBounds.width() - 1; x >= 0; --x) {
224 dst[x] = lut[dst[x]];
225 }
226 dst += rowBytes;
227 }
228}
229
bungeman@google.com97efada2012-07-30 20:40:50 +0000230template<bool APPLY_PREBLEND>
reed41e010c2015-06-09 12:16:53 -0700231static void pack4xHToLCD16(const SkPixmap& src, const SkMask& dst,
bungeman@google.coma76de722012-10-26 19:35:54 +0000232 const SkMaskGamma::PreBlend& maskPreBlend) {
bungeman@google.com0abbff92013-07-27 20:37:56 +0000233#define SAMPLES_PER_PIXEL 4
234#define LCD_PER_PIXEL 3
reed@google.com900ecf22014-02-20 20:55:37 +0000235 SkASSERT(kAlpha_8_SkColorType == src.colorType());
reed@google.comcf598b12011-08-22 21:13:23 +0000236 SkASSERT(SkMask::kLCD16_Format == dst.fFormat);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000237
bungeman@google.com0abbff92013-07-27 20:37:56 +0000238 const int sample_width = src.width();
239 const int height = src.height();
240
reed@google.comcf598b12011-08-22 21:13:23 +0000241 uint16_t* dstP = (uint16_t*)dst.fImage;
242 size_t dstRB = dst.fRowBytes;
bungeman@google.com0abbff92013-07-27 20:37:56 +0000243 // An N tap FIR is defined by
244 // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N]
245 // or
246 // out[n] = sum(i, 0, N, coeff[i]*x[n-i])
247
248 // The strategy is to use one FIR (different coefficients) for each of r, g, and b.
249 // This means using every 4th FIR output value of each FIR and discarding the rest.
250 // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'.
251 // (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 +0000252
bungeman@google.com0abbff92013-07-27 20:37:56 +0000253 // These are in some fixed point repesentation.
254 // Adding up to more than one simulates ink spread.
255 // For implementation reasons, these should never add up to more than two.
256
257 // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast').
258 // Calculated using tools/generate_fir_coeff.py
259 // With this one almost no fringing is ever seen, but it is imperceptibly blurry.
260 // The lcd smoothed text is almost imperceptibly different from gray,
261 // but is still sharper on small stems and small rounded corners than gray.
262 // This also seems to be about as wide as one can get and only have a three pixel kernel.
263 // TODO: caculate these at runtime so parameters can be adjusted (esp contrast).
264 static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = {
265 //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted.
266 { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, },
267 //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric
268 { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, },
269 //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted.
270 { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, },
271 };
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272
reed@google.comcf598b12011-08-22 21:13:23 +0000273 for (int y = 0; y < height; ++y) {
reed41e010c2015-06-09 12:16:53 -0700274 const uint8_t* srcP = src.addr8(0, y);
bungeman@google.com0abbff92013-07-27 20:37:56 +0000275
276 // TODO: this fir filter implementation is straight forward, but slow.
277 // It should be possible to make it much faster.
278 for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) {
279 int fir[LCD_PER_PIXEL] = { 0 };
280 for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4)
281 ; sample_index < SkMin32(sample_x + 8, sample_width)
282 ; ++sample_index, ++coeff_index)
283 {
284 int sample_value = srcP[sample_index];
285 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
286 fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value;
287 }
288 }
289 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
290 fir[subpxl_index] /= 0x100;
291 fir[subpxl_index] = SkMin32(fir[subpxl_index], 255);
292 }
293
294 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(fir[0], maskPreBlend.fR);
295 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(fir[1], maskPreBlend.fG);
296 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(fir[2], maskPreBlend.fB);
297#if SK_SHOW_TEXT_BLIT_COVERAGE
298 r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10);
299#endif
300 dstP[pixel_x] = SkPack888ToRGB16(r, g, b);
reed@google.coma767fa02011-08-05 21:40:26 +0000301 }
reed@google.comcf598b12011-08-22 21:13:23 +0000302 dstP = (uint16_t*)((char*)dstP + dstRB);
303 }
304}
305
rmistry@google.comd6bab022013-12-02 13:50:38 +0000306static inline int convert_8_to_1(unsigned byte) {
307 SkASSERT(byte <= 0xFF);
308 return byte >> 7;
309}
310
311static uint8_t pack_8_to_1(const uint8_t alpha[8]) {
312 unsigned bits = 0;
313 for (int i = 0; i < 8; ++i) {
314 bits <<= 1;
315 bits |= convert_8_to_1(alpha[i]);
316 }
317 return SkToU8(bits);
318}
319
320static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) {
321 const int height = mask.fBounds.height();
322 const int width = mask.fBounds.width();
323 const int octs = width >> 3;
324 const int leftOverBits = width & 7;
325
326 uint8_t* dst = mask.fImage;
327 const int dstPad = mask.fRowBytes - SkAlign8(width)/8;
328 SkASSERT(dstPad >= 0);
329
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +0000330 SkASSERT(width >= 0);
331 SkASSERT(srcRB >= (size_t)width);
332 const size_t srcPad = srcRB - width;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000333
334 for (int y = 0; y < height; ++y) {
335 for (int i = 0; i < octs; ++i) {
336 *dst++ = pack_8_to_1(src);
337 src += 8;
338 }
339 if (leftOverBits > 0) {
340 unsigned bits = 0;
341 int shift = 7;
342 for (int i = 0; i < leftOverBits; ++i, --shift) {
robertphillips@google.comc932c9f2013-12-02 15:43:39 +0000343 bits |= convert_8_to_1(*src++) << shift;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000344 }
345 *dst++ = bits;
346 }
347 src += srcPad;
348 dst += dstPad;
349 }
350}
351
bungeman@google.coma76de722012-10-26 19:35:54 +0000352static void generateMask(const SkMask& mask, const SkPath& path,
353 const SkMaskGamma::PreBlend& maskPreBlend) {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000354 SkPaint paint;
reed@google.comcf598b12011-08-22 21:13:23 +0000355
356 int srcW = mask.fBounds.width();
357 int srcH = mask.fBounds.height();
358 int dstW = srcW;
359 int dstH = srcH;
360 int dstRB = mask.fRowBytes;
361
362 SkMatrix matrix;
363 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
364 -SkIntToScalar(mask.fBounds.fTop));
365
rmistry@google.comd6bab022013-12-02 13:50:38 +0000366 paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat);
367 switch (mask.fFormat) {
368 case SkMask::kBW_Format:
369 dstRB = 0; // signals we need a copy
370 break;
371 case SkMask::kA8_Format:
372 break;
373 case SkMask::kLCD16_Format:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000374 // TODO: trigger off LCD orientation
375 dstW = 4*dstW - 8;
376 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1),
377 -SkIntToScalar(mask.fBounds.fTop));
378 matrix.postScale(SkIntToScalar(4), SK_Scalar1);
379 dstRB = 0; // signals we need a copy
380 break;
381 default:
382 SkDEBUGFAIL("unexpected mask format");
reed@google.comcf598b12011-08-22 21:13:23 +0000383 }
384
reed@google.com045e62d2011-10-24 12:19:46 +0000385 SkRasterClip clip;
386 clip.setRect(SkIRect::MakeWH(dstW, dstH));
reed@google.comcf598b12011-08-22 21:13:23 +0000387
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000388 const SkImageInfo info = SkImageInfo::MakeA8(dstW, dstH);
reed41e010c2015-06-09 12:16:53 -0700389 SkAutoPixmapStorage dst;
reed@google.comcf598b12011-08-22 21:13:23 +0000390
391 if (0 == dstRB) {
reed41e010c2015-06-09 12:16:53 -0700392 if (!dst.tryAlloc(info)) {
reed@google.com9be57272012-12-17 14:21:38 +0000393 // can't allocate offscreen, so empty the mask and return
394 sk_bzero(mask.fImage, mask.computeImageSize());
395 return;
396 }
reed@google.comcf598b12011-08-22 21:13:23 +0000397 } else {
reed41e010c2015-06-09 12:16:53 -0700398 dst.reset(info, mask.fImage, dstRB);
reed@google.comcf598b12011-08-22 21:13:23 +0000399 }
reed41e010c2015-06-09 12:16:53 -0700400 sk_bzero(dst.writable_addr(), dst.getSafeSize());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000401
reed@google.comcf598b12011-08-22 21:13:23 +0000402 SkDraw draw;
reed41e010c2015-06-09 12:16:53 -0700403 draw.fDst = dst;
reed@google.com045e62d2011-10-24 12:19:46 +0000404 draw.fRC = &clip;
reed@google.comcf598b12011-08-22 21:13:23 +0000405 draw.fMatrix = &matrix;
reed@google.comcf598b12011-08-22 21:13:23 +0000406 draw.drawPath(path, paint);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000407
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000408 switch (mask.fFormat) {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000409 case SkMask::kBW_Format:
reed41e010c2015-06-09 12:16:53 -0700410 packA8ToA1(mask, dst.addr8(0, 0), dst.rowBytes());
rmistry@google.comd6bab022013-12-02 13:50:38 +0000411 break;
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000412 case SkMask::kA8_Format:
413 if (maskPreBlend.isApplicable()) {
414 applyLUTToA8Mask(mask, maskPreBlend.fG);
415 }
416 break;
417 case SkMask::kLCD16_Format:
418 if (maskPreBlend.isApplicable()) {
reed41e010c2015-06-09 12:16:53 -0700419 pack4xHToLCD16<true>(dst, mask, maskPreBlend);
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000420 } else {
reed41e010c2015-06-09 12:16:53 -0700421 pack4xHToLCD16<false>(dst, mask, maskPreBlend);
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000422 }
423 break;
bungeman@google.com0c5f3762012-10-26 20:48:54 +0000424 default:
425 break;
reed@google.comcf598b12011-08-22 21:13:23 +0000426 }
427}
428
reed@google.com5bdfb332013-05-02 18:55:44 +0000429static void extract_alpha(const SkMask& dst,
430 const SkPMColor* srcRow, size_t srcRB) {
431 int width = dst.fBounds.width();
432 int height = dst.fBounds.height();
433 int dstRB = dst.fRowBytes;
434 uint8_t* dstRow = dst.fImage;
435
436 for (int y = 0; y < height; ++y) {
437 for (int x = 0; x < width; ++x) {
438 dstRow[x] = SkGetPackedA32(srcRow[x]);
439 }
440 // zero any padding on each row
441 for (int x = width; x < dstRB; ++x) {
442 dstRow[x] = 0;
443 }
444 dstRow += dstRB;
445 srcRow = (const SkPMColor*)((const char*)srcRow + srcRB);
446 }
447}
448
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449void SkScalerContext::getImage(const SkGlyph& origGlyph) {
450 const SkGlyph* glyph = &origGlyph;
451 SkGlyph tmpGlyph;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000452
reed@google.com5bdfb332013-05-02 18:55:44 +0000453 // in case we need to call generateImage on a mask-format that is different
454 // (i.e. larger) than what our caller allocated by looking at origGlyph.
455 SkAutoMalloc tmpGlyphImageStorage;
456
457 // If we are going to draw-from-path, then we cannot generate color, since
458 // the path only makes a mask. This case should have been caught up in
459 // generateMetrics().
460 SkASSERT(!fGenerateImageFromPath ||
461 SkMask::kARGB32_Format != origGlyph.fMaskFormat);
462
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463 if (fMaskFilter) { // restore the prefilter bounds
herbb69d0e02015-02-25 06:47:06 -0800464 tmpGlyph.initGlyphIdFrom(origGlyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465
466 // need the original bounds, sans our maskfilter
reeda9322c22016-04-12 06:47:05 -0700467 SkMaskFilter* mf = fMaskFilter.release(); // temp disable
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468 this->getMetrics(&tmpGlyph);
reeda9322c22016-04-12 06:47:05 -0700469 fMaskFilter = sk_sp<SkMaskFilter>(mf); // restore
reed@android.com8a1c16f2008-12-17 15:59:43 +0000470
reed@android.com8a1c16f2008-12-17 15:59:43 +0000471 // we need the prefilter bounds to be <= filter bounds
472 SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth);
473 SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight);
reed@google.com5bdfb332013-05-02 18:55:44 +0000474
475 if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) {
476 tmpGlyph.fImage = origGlyph.fImage;
477 } else {
478 tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize());
479 tmpGlyph.fImage = tmpGlyphImageStorage.get();
480 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000481 glyph = &tmpGlyph;
482 }
483
reed@google.coma767fa02011-08-05 21:40:26 +0000484 if (fGenerateImageFromPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485 SkPath devPath, fillPath;
486 SkMatrix fillToDevMatrix;
reed@google.comcf598b12011-08-22 21:13:23 +0000487 SkMask mask;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488
489 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
reed@google.comcf598b12011-08-22 21:13:23 +0000490 glyph->toMask(&mask);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491
492 if (fRasterizer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493 mask.fFormat = SkMask::kA8_Format;
reed@android.com4516f472009-06-29 16:25:36 +0000494 sk_bzero(glyph->fImage, mask.computeImageSize());
reed@google.com82065d62011-02-07 15:30:46 +0000495
halcanary96fcdcc2015-08-27 07:41:13 -0700496 if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr,
reeda9322c22016-04-12 06:47:05 -0700497 fMaskFilter.get(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 SkMask::kJustRenderImage_CreateMode)) {
499 return;
500 }
bungeman@google.coma76de722012-10-26 19:35:54 +0000501 if (fPreBlend.isApplicable()) {
502 applyLUTToA8Mask(mask, fPreBlend.fG);
bungeman@google.com97efada2012-07-30 20:40:50 +0000503 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504 } else {
reed@google.com5bdfb332013-05-02 18:55:44 +0000505 SkASSERT(SkMask::kARGB32_Format != mask.fFormat);
bungeman@google.coma76de722012-10-26 19:35:54 +0000506 generateMask(mask, devPath, fPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507 }
508 } else {
djsollen1b277042014-08-06 06:58:06 -0700509 generateImage(*glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 }
511
512 if (fMaskFilter) {
513 SkMask srcM, dstM;
514 SkMatrix matrix;
515
516 // the src glyph image shouldn't be 3D
517 SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat);
reed@google.com5bdfb332013-05-02 18:55:44 +0000518
519 SkAutoSMalloc<32*32> a8storage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 glyph->toMask(&srcM);
reed@google.com5bdfb332013-05-02 18:55:44 +0000521 if (SkMask::kARGB32_Format == srcM.fFormat) {
522 // now we need to extract the alpha-channel from the glyph's image
523 // and copy it into a temp buffer, and then point srcM at that temp.
524 srcM.fFormat = SkMask::kA8_Format;
525 srcM.fRowBytes = SkAlign4(srcM.fBounds.width());
526 size_t size = srcM.computeImageSize();
527 a8storage.reset(size);
528 srcM.fImage = (uint8_t*)a8storage.get();
529 extract_alpha(srcM,
530 (const SkPMColor*)glyph->fImage, glyph->rowBytes());
531 }
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000532
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 fRec.getMatrixFrom2x2(&matrix);
534
halcanary96fcdcc2015-08-27 07:41:13 -0700535 if (fMaskFilter->filterMask(&dstM, srcM, matrix, nullptr)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width());
537 int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height());
538 int dstRB = origGlyph.rowBytes();
539 int srcRB = dstM.fRowBytes;
reed@google.com82065d62011-02-07 15:30:46 +0000540
reed@android.com8a1c16f2008-12-17 15:59:43 +0000541 const uint8_t* src = (const uint8_t*)dstM.fImage;
542 uint8_t* dst = (uint8_t*)origGlyph.fImage;
543
544 if (SkMask::k3D_Format == dstM.fFormat) {
545 // we have to copy 3 times as much
546 height *= 3;
547 }
548
549 // clean out our glyph, since it may be larger than dstM
reed@android.com4516f472009-06-29 16:25:36 +0000550 //sk_bzero(dst, height * dstRB);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551
552 while (--height >= 0) {
553 memcpy(dst, src, width);
554 src += srcRB;
555 dst += dstRB;
556 }
557 SkMask::FreeImage(dstM.fImage);
bungeman@google.com97efada2012-07-30 20:40:50 +0000558
bungeman@google.coma76de722012-10-26 19:35:54 +0000559 if (fPreBlendForFilter.isApplicable()) {
560 applyLUTToA8Mask(srcM, fPreBlendForFilter.fG);
561 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562 }
563 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564}
565
reed@google.com7db9fe62011-04-11 11:57:54 +0000566void SkScalerContext::getPath(const SkGlyph& glyph, SkPath* path) {
halcanary96fcdcc2015-08-27 07:41:13 -0700567 this->internalGetPath(glyph, nullptr, path, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568}
569
reed@google.com0a01f5a2013-05-08 14:19:08 +0000570void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) {
bungeman41078062014-07-07 08:16:37 -0700571 this->generateFontMetrics(fm);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572}
573
reed@android.com9d3a9852010-01-08 14:07:42 +0000574SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) {
575 return 0;
576}
577
reed@google.com7db9fe62011-04-11 11:57:54 +0000578///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579
reed@google.com7db9fe62011-04-11 11:57:54 +0000580void SkScalerContext::internalGetPath(const SkGlyph& glyph, SkPath* fillPath,
581 SkPath* devPath, SkMatrix* fillToDevMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000582 SkPath path;
djsollen1b277042014-08-06 06:58:06 -0700583 generatePath(glyph, &path);
reed@google.com82065d62011-02-07 15:30:46 +0000584
reed@google.comcf598b12011-08-22 21:13:23 +0000585 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
586 SkFixed dx = glyph.getSubXFixed();
587 SkFixed dy = glyph.getSubYFixed();
588 if (dx | dy) {
589 path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy));
590 }
591 }
592
halcanary96fcdcc2015-08-27 07:41:13 -0700593 if (fRec.fFrameWidth > 0 || fPathEffect != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000594 // need the path in user-space, with only the point-size applied
595 // so that our stroking and effects will operate the same way they
596 // would if the user had extracted the path themself, and then
597 // called drawPath
598 SkPath localPath;
599 SkMatrix matrix, inverse;
600
601 fRec.getMatrixFrom2x2(&matrix);
bungeman@google.com9575fb82012-04-09 20:49:03 +0000602 if (!matrix.invert(&inverse)) {
603 // assume fillPath and devPath are already empty.
604 return;
605 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000606 path.transform(inverse, &localPath);
607 // now localPath is only affected by the paint settings, and not the canvas matrix
608
reed@google.comfd4be262012-05-25 01:04:12 +0000609 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000610
reed@google.comfd4be262012-05-25 01:04:12 +0000611 if (fRec.fFrameWidth > 0) {
612 rec.setStrokeStyle(fRec.fFrameWidth,
613 SkToBool(fRec.fFlags & kFrameAndFill_Flag));
614 // glyphs are always closed contours, so cap type is ignored,
615 // so we just pass something.
caryclarkd7ea92f2016-03-16 07:34:02 -0700616 rec.setStrokeParams((SkPaint::Cap)fRec.fStrokeCap,
reed@google.comfd4be262012-05-25 01:04:12 +0000617 (SkPaint::Join)fRec.fStrokeJoin,
618 fRec.fMiterLimit);
619 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000620
reed@google.com7db9fe62011-04-11 11:57:54 +0000621 if (fPathEffect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000622 SkPath effectPath;
halcanary96fcdcc2015-08-27 07:41:13 -0700623 if (fPathEffect->filterPath(&effectPath, localPath, &rec, nullptr)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624 localPath.swap(effectPath);
reed@google.com7db9fe62011-04-11 11:57:54 +0000625 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 }
627
reed@google.comfd4be262012-05-25 01:04:12 +0000628 if (rec.needToApply()) {
629 SkPath strokePath;
630 if (rec.applyToPath(&strokePath, localPath)) {
631 localPath.swap(strokePath);
632 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000633 }
reed@google.com82065d62011-02-07 15:30:46 +0000634
reed@android.com8a1c16f2008-12-17 15:59:43 +0000635 // now return stuff to the caller
reed@google.com7db9fe62011-04-11 11:57:54 +0000636 if (fillToDevMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 *fillToDevMatrix = matrix;
reed@google.com7db9fe62011-04-11 11:57:54 +0000638 }
639 if (devPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640 localPath.transform(matrix, devPath);
reed@google.com7db9fe62011-04-11 11:57:54 +0000641 }
642 if (fillPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000643 fillPath->swap(localPath);
reed@google.com7db9fe62011-04-11 11:57:54 +0000644 }
645 } else { // nothing tricky to do
646 if (fillToDevMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 fillToDevMatrix->reset();
reed@google.com7db9fe62011-04-11 11:57:54 +0000648 }
649 if (devPath) {
halcanary96fcdcc2015-08-27 07:41:13 -0700650 if (fillPath == nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000651 devPath->swap(path);
reed@google.com7db9fe62011-04-11 11:57:54 +0000652 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000653 *devPath = path;
reed@google.com7db9fe62011-04-11 11:57:54 +0000654 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655 }
reed@google.com82065d62011-02-07 15:30:46 +0000656
reed@google.com7db9fe62011-04-11 11:57:54 +0000657 if (fillPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658 fillPath->swap(path);
reed@google.com7db9fe62011-04-11 11:57:54 +0000659 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000660 }
reed@google.com82065d62011-02-07 15:30:46 +0000661
reed@google.com7db9fe62011-04-11 11:57:54 +0000662 if (devPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 devPath->updateBoundsCache();
reed@google.com7db9fe62011-04-11 11:57:54 +0000664 }
665 if (fillPath) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666 fillPath->updateBoundsCache();
reed@google.com7db9fe62011-04-11 11:57:54 +0000667 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000668}
669
670
reed@google.coma9d4e842012-08-14 19:13:55 +0000671void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const {
bungeman@google.com50dd4102013-01-14 18:25:55 +0000672 dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0,
673 fPost2x2[1][0], fPost2x2[1][1], 0,
reed3f43f8a2015-01-20 19:58:36 -0800674 0, 0, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675}
676
reed@google.coma9d4e842012-08-14 19:13:55 +0000677void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const {
reed@google.comed43dff2013-06-04 16:56:27 +0000678 SkPaint::SetTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000679}
680
reed@google.coma9d4e842012-08-14 19:13:55 +0000681void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682 this->getLocalMatrix(m);
683
684 // now concat the device matrix
reed@google.com7db9fe62011-04-11 11:57:54 +0000685 SkMatrix deviceMatrix;
686 this->getMatrixFrom2x2(&deviceMatrix);
687 m->postConcat(deviceMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688}
689
bungeman5f14c5e2014-12-05 12:26:44 -0800690void SkScalerContextRec::computeMatrices(PreMatrixScale preMatrixScale, SkVector* s, SkMatrix* sA,
691 SkMatrix* GsA, SkMatrix* G_inv, SkMatrix* A_out)
692{
693 // A is the 'total' matrix.
694 SkMatrix A;
695 this->getSingleMatrix(&A);
696
697 // The caller may find the 'total' matrix useful when dealing directly with EM sizes.
698 if (A_out) {
699 *A_out = A;
700 }
701
bungeman34902632014-12-10 21:43:27 -0800702 // If the 'total' matrix is singular, set the 'scale' to something finite and zero the matrices.
703 // All underlying ports have issues with zero text size, so use the matricies to zero.
704
bungemane37514d2016-02-25 11:34:10 -0800705 // Map the vectors [0,1], [1,0], [1,1] and [1,-1] (the EM) through the 'total' matrix.
bungeman34902632014-12-10 21:43:27 -0800706 // If the length of one of these vectors is less than 1/256 then an EM filling square will
707 // never affect any pixels.
bungemane37514d2016-02-25 11:34:10 -0800708 SkVector diag[4] = { { A.getScaleX() , A.getSkewY() },
709 { A.getSkewX(), A.getScaleY() },
710 { A.getScaleX() + A.getSkewX(), A.getScaleY() + A.getSkewY() },
bungeman34902632014-12-10 21:43:27 -0800711 { A.getScaleX() - A.getSkewX(), A.getScaleY() - A.getSkewY() }, };
712 if (diag[0].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero ||
bungemane37514d2016-02-25 11:34:10 -0800713 diag[1].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero ||
714 diag[2].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero ||
715 diag[3].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero)
bungeman34902632014-12-10 21:43:27 -0800716 {
717 s->fX = SK_Scalar1;
718 s->fY = SK_Scalar1;
719 sA->setScale(0, 0);
720 if (GsA) {
721 GsA->setScale(0, 0);
722 }
723 if (G_inv) {
724 G_inv->reset();
725 }
726 return;
727 }
728
bungeman5f14c5e2014-12-05 12:26:44 -0800729 // GA is the matrix A with rotation removed.
730 SkMatrix GA;
731 bool skewedOrFlipped = A.getSkewX() || A.getSkewY() || A.getScaleX() < 0 || A.getScaleY() < 0;
732 if (skewedOrFlipped) {
733 // h is where A maps the horizontal baseline.
734 SkPoint h = SkPoint::Make(SK_Scalar1, 0);
735 A.mapPoints(&h, 1);
736
737 // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0).
738 SkMatrix G;
739 SkComputeGivensRotation(h, &G);
740
741 GA = G;
742 GA.preConcat(A);
743
744 // The 'remainingRotation' is G inverse, which is fairly simple since G is 2x2 rotational.
745 if (G_inv) {
746 G_inv->setAll(
747 G.get(SkMatrix::kMScaleX), -G.get(SkMatrix::kMSkewX), G.get(SkMatrix::kMTransX),
748 -G.get(SkMatrix::kMSkewY), G.get(SkMatrix::kMScaleY), G.get(SkMatrix::kMTransY),
749 G.get(SkMatrix::kMPersp0), G.get(SkMatrix::kMPersp1), G.get(SkMatrix::kMPersp2));
750 }
751 } else {
752 GA = A;
753 if (G_inv) {
754 G_inv->reset();
755 }
756 }
757
758 // At this point, given GA, create s.
759 switch (preMatrixScale) {
760 case kFull_PreMatrixScale:
761 s->fX = SkScalarAbs(GA.get(SkMatrix::kMScaleX));
762 s->fY = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
763 break;
764 case kVertical_PreMatrixScale: {
765 SkScalar yScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
766 s->fX = yScale;
767 s->fY = yScale;
768 break;
769 }
770 case kVerticalInteger_PreMatrixScale: {
771 SkScalar realYScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
772 SkScalar intYScale = SkScalarRoundToScalar(realYScale);
773 if (intYScale == 0) {
774 intYScale = SK_Scalar1;
775 }
776 s->fX = intYScale;
777 s->fY = intYScale;
778 break;
779 }
780 }
781
782 // The 'remaining' matrix sA is the total matrix A without the scale.
bungeman34902632014-12-10 21:43:27 -0800783 if (!skewedOrFlipped && (
784 (kFull_PreMatrixScale == preMatrixScale) ||
785 (kVertical_PreMatrixScale == preMatrixScale && A.getScaleX() == A.getScaleY())))
786 {
bungeman5f14c5e2014-12-05 12:26:44 -0800787 // If GA == A and kFull_PreMatrixScale, sA is identity.
bungeman34902632014-12-10 21:43:27 -0800788 // If GA == A and kVertical_PreMatrixScale and A.scaleX == A.scaleY, sA is identity.
bungeman5f14c5e2014-12-05 12:26:44 -0800789 sA->reset();
bungeman34902632014-12-10 21:43:27 -0800790 } else if (!skewedOrFlipped && kVertical_PreMatrixScale == preMatrixScale) {
791 // If GA == A and kVertical_PreMatrixScale, sA.scaleY is SK_Scalar1.
792 sA->reset();
793 sA->setScaleX(A.getScaleX() / s->fY);
bungeman5f14c5e2014-12-05 12:26:44 -0800794 } else {
bungeman5f14c5e2014-12-05 12:26:44 -0800795 // TODO: like kVertical_PreMatrixScale, kVerticalInteger_PreMatrixScale with int scales.
796 *sA = A;
797 sA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
798 }
799
800 // The 'remainingWithoutRotation' matrix GsA is the non-rotational part of A without the scale.
801 if (GsA) {
802 *GsA = GA;
803 // G is rotational so reorders with the scale.
804 GsA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
805 }
806}
807
bungeman27876bc2016-02-29 11:22:55 -0800808SkAxisAlignment SkScalerContext::computeAxisAlignmentForHText() {
809 // Why fPost2x2 can be used here.
810 // getSingleMatrix multiplies in getLocalMatrix, which consists of
811 // * fTextSize (a scale, which has no effect)
812 // * fPreScaleX (a scale in x, which has no effect)
813 // * fPreSkewX (has no effect, but would on vertical text alignment).
814 // In other words, making the text bigger, stretching it along the
815 // horizontal axis, or fake italicizing it does not move the baseline.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000816
bungeman27876bc2016-02-29 11:22:55 -0800817 if (0 == fRec.fPost2x2[1][0]) {
818 // The x axis is mapped onto the x axis.
reed@google.com2e684782011-08-24 15:38:46 +0000819 return kX_SkAxisAlignment;
820 }
bungeman27876bc2016-02-29 11:22:55 -0800821 if (0 == fRec.fPost2x2[0][0]) {
822 // The x axis is mapped onto the y axis.
reed@google.com2e684782011-08-24 15:38:46 +0000823 return kY_SkAxisAlignment;
824 }
825 return kNone_SkAxisAlignment;
826}
827
reed@android.com62900b42009-02-11 15:07:19 +0000828///////////////////////////////////////////////////////////////////////////////
829
reed@android.com62900b42009-02-11 15:07:19 +0000830class SkScalerContext_Empty : public SkScalerContext {
831public:
reeda9322c22016-04-12 06:47:05 -0700832 SkScalerContext_Empty(SkTypeface* typeface, const SkScalerContextEffects& effects,
833 const SkDescriptor* desc)
834 : SkScalerContext(typeface, effects, desc) {}
reed@android.com62900b42009-02-11 15:07:19 +0000835
836protected:
mtklein36352bf2015-03-25 18:17:31 -0700837 unsigned generateGlyphCount() override {
reed@android.com62900b42009-02-11 15:07:19 +0000838 return 0;
839 }
mtklein36352bf2015-03-25 18:17:31 -0700840 uint16_t generateCharToGlyph(SkUnichar uni) override {
reed@android.com62900b42009-02-11 15:07:19 +0000841 return 0;
842 }
mtklein36352bf2015-03-25 18:17:31 -0700843 void generateAdvance(SkGlyph* glyph) override {
reed@android.com62900b42009-02-11 15:07:19 +0000844 glyph->zeroMetrics();
845 }
mtklein36352bf2015-03-25 18:17:31 -0700846 void generateMetrics(SkGlyph* glyph) override {
reed@android.com62900b42009-02-11 15:07:19 +0000847 glyph->zeroMetrics();
848 }
mtklein36352bf2015-03-25 18:17:31 -0700849 void generateImage(const SkGlyph& glyph) override {}
850 void generatePath(const SkGlyph& glyph, SkPath* path) override {}
851 void generateFontMetrics(SkPaint::FontMetrics* metrics) override {
bungeman41078062014-07-07 08:16:37 -0700852 if (metrics) {
853 sk_bzero(metrics, sizeof(*metrics));
reed@android.com62900b42009-02-11 15:07:19 +0000854 }
855 }
856};
857
reed@android.comf2b98d62010-12-20 18:26:13 +0000858extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc);
859
reeda9322c22016-04-12 06:47:05 -0700860SkScalerContext* SkTypeface::createScalerContext(const SkScalerContextEffects& effects,
861 const SkDescriptor* desc,
reed@google.com84e22d82013-07-10 15:38:20 +0000862 bool allowFailure) const {
reeda9322c22016-04-12 06:47:05 -0700863 SkScalerContext* c = this->onCreateScalerContext(effects, desc);
reed@google.com84e22d82013-07-10 15:38:20 +0000864
865 if (!c && !allowFailure) {
reeda9322c22016-04-12 06:47:05 -0700866 c = new SkScalerContext_Empty(const_cast<SkTypeface*>(this), effects, desc);
reed@android.com62900b42009-02-11 15:07:19 +0000867 }
868 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000869}