3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkScalerContext.h"
11 #include "SkColorPriv.h"
12 #include "SkDescriptor.h"
15 #include "SkMaskFilter.h"
16 #include "SkMaskGamma.h"
17 #include "SkMatrix22.h"
18 #include "SkReadBuffer.h"
19 #include "SkWriteBuffer.h"
20 #include "SkPathEffect.h"
21 #include "SkRasterizer.h"
22 #include "SkRasterClip.h"
26 #define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3)
28 void SkGlyph::toMask(SkMask* mask) const {
31 mask->fImage = (uint8_t*)fImage;
32 mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight);
33 mask->fRowBytes = this->rowBytes();
34 mask->fFormat = static_cast<SkMask::Format>(fMaskFormat);
37 size_t SkGlyph::computeImageSize() const {
38 const size_t size = this->rowBytes() * fHeight;
40 switch (fMaskFormat) {
41 case SkMask::k3D_Format:
48 void SkGlyph::zeroMetrics() {
59 ///////////////////////////////////////////////////////////////////////////////
65 static SkFlattenable* load_flattenable(const SkDescriptor* desc, uint32_t tag,
66 SkFlattenable::Type ft) {
67 SkFlattenable* obj = NULL;
69 const void* data = desc->findEntry(tag, &len);
72 SkReadBuffer buffer(data, len);
73 obj = buffer.readFlattenable(ft);
74 SkASSERT(buffer.offset() == buffer.size());
79 SkScalerContext::SkScalerContext(SkTypeface* typeface, const SkDescriptor* desc)
80 : fRec(*static_cast<const Rec*>(desc->findEntry(kRec_SkDescriptorTag, NULL)))
82 , fTypeface(SkRef(typeface))
83 , fPathEffect(static_cast<SkPathEffect*>(load_flattenable(desc, kPathEffect_SkDescriptorTag,
84 SkFlattenable::kSkPathEffect_Type)))
85 , fMaskFilter(static_cast<SkMaskFilter*>(load_flattenable(desc, kMaskFilter_SkDescriptorTag,
86 SkFlattenable::kSkMaskFilter_Type)))
87 , fRasterizer(static_cast<SkRasterizer*>(load_flattenable(desc, kRasterizer_SkDescriptorTag,
88 SkFlattenable::kSkRasterizer_Type)))
89 // Initialize based on our settings. Subclasses can also force this.
90 , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != NULL || fRasterizer != NULL)
92 , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec))
93 , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec)
94 : SkMaskGamma::PreBlend())
97 desc->assertChecksum();
98 SkDebugf("SkScalerContext checksum %x count %d length %d\n",
99 desc->getChecksum(), desc->getCount(), desc->getLength());
100 SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n",
101 rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0],
102 rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]);
103 SkDebugf(" frame %g miter %g hints %d framefill %d format %d join %d\n",
104 rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill,
105 rec->fMaskFormat, rec->fStrokeJoin);
106 SkDebugf(" pathEffect %x maskFilter %x\n",
107 desc->findEntry(kPathEffect_SkDescriptorTag, NULL),
108 desc->findEntry(kMaskFilter_SkDescriptorTag, NULL));
112 SkScalerContext::~SkScalerContext() {
113 SkSafeUnref(fPathEffect);
114 SkSafeUnref(fMaskFilter);
115 SkSafeUnref(fRasterizer);
118 void SkScalerContext::getAdvance(SkGlyph* glyph) {
119 // mark us as just having a valid advance
120 glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE;
121 // we mark the format before making the call, in case the impl
122 // internally ends up calling its generateMetrics, which is OK
123 // albeit slower than strictly necessary
124 generateAdvance(glyph);
127 void SkScalerContext::getMetrics(SkGlyph* glyph) {
128 generateMetrics(glyph);
130 // for now we have separate cache entries for devkerning on and off
131 // in the future we might share caches, but make our measure/draw
132 // code make the distinction. Thus we zap the values if the caller
133 // has not asked for them.
134 if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) {
135 // no devkern, so zap the fields
136 glyph->fLsbDelta = glyph->fRsbDelta = 0;
139 // if either dimension is empty, zap the image bounds of the glyph
140 if (0 == glyph->fWidth || 0 == glyph->fHeight) {
145 glyph->fMaskFormat = 0;
149 if (fGenerateImageFromPath) {
150 SkPath devPath, fillPath;
151 SkMatrix fillToDevMatrix;
153 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
158 if (fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL,
160 SkMask::kJustComputeBounds_CreateMode)) {
161 glyph->fLeft = mask.fBounds.fLeft;
162 glyph->fTop = mask.fBounds.fTop;
163 glyph->fWidth = SkToU16(mask.fBounds.width());
164 glyph->fHeight = SkToU16(mask.fBounds.height());
170 const SkIRect ir = devPath.getBounds().roundOut();
172 if (ir.isEmpty() || !ir.is16Bit()) {
175 glyph->fLeft = ir.fLeft;
176 glyph->fTop = ir.fTop;
177 glyph->fWidth = SkToU16(ir.width());
178 glyph->fHeight = SkToU16(ir.height());
180 if (glyph->fWidth > 0) {
181 switch (fRec.fMaskFormat) {
182 case SkMask::kLCD16_Format:
193 if (SkMask::kARGB32_Format != glyph->fMaskFormat) {
194 glyph->fMaskFormat = fRec.fMaskFormat;
197 // If we are going to create the mask, then we cannot keep the color
198 if ((fGenerateImageFromPath || fMaskFilter) &&
199 SkMask::kARGB32_Format == glyph->fMaskFormat) {
200 glyph->fMaskFormat = SkMask::kA8_Format;
208 fRec.getMatrixFrom2x2(&matrix);
210 src.fImage = NULL; // only want the bounds from the filter
211 if (fMaskFilter->filterMask(&dst, src, matrix, NULL)) {
212 if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) {
215 SkASSERT(dst.fImage == NULL);
216 glyph->fLeft = dst.fBounds.fLeft;
217 glyph->fTop = dst.fBounds.fTop;
218 glyph->fWidth = SkToU16(dst.fBounds.width());
219 glyph->fHeight = SkToU16(dst.fBounds.height());
220 glyph->fMaskFormat = dst.fFormat;
226 // draw nothing 'cause we failed
231 // put a valid value here, in case it was earlier set to
232 // MASK_FORMAT_JUST_ADVANCE
233 glyph->fMaskFormat = fRec.fMaskFormat;
236 #define SK_SHOW_TEXT_BLIT_COVERAGE 0
238 static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) {
239 uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage;
240 unsigned rowBytes = mask.fRowBytes;
242 for (int y = mask.fBounds.height() - 1; y >= 0; --y) {
243 for (int x = mask.fBounds.width() - 1; x >= 0; --x) {
244 dst[x] = lut[dst[x]];
250 template<bool APPLY_PREBLEND>
251 static void pack4xHToLCD16(const SkBitmap& src, const SkMask& dst,
252 const SkMaskGamma::PreBlend& maskPreBlend) {
253 #define SAMPLES_PER_PIXEL 4
254 #define LCD_PER_PIXEL 3
255 SkASSERT(kAlpha_8_SkColorType == src.colorType());
256 SkASSERT(SkMask::kLCD16_Format == dst.fFormat);
258 const int sample_width = src.width();
259 const int height = src.height();
261 uint16_t* dstP = (uint16_t*)dst.fImage;
262 size_t dstRB = dst.fRowBytes;
263 // An N tap FIR is defined by
264 // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N]
266 // out[n] = sum(i, 0, N, coeff[i]*x[n-i])
268 // The strategy is to use one FIR (different coefficients) for each of r, g, and b.
269 // This means using every 4th FIR output value of each FIR and discarding the rest.
270 // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'.
271 // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.)
273 // These are in some fixed point repesentation.
274 // Adding up to more than one simulates ink spread.
275 // For implementation reasons, these should never add up to more than two.
277 // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast').
278 // Calculated using tools/generate_fir_coeff.py
279 // With this one almost no fringing is ever seen, but it is imperceptibly blurry.
280 // The lcd smoothed text is almost imperceptibly different from gray,
281 // but is still sharper on small stems and small rounded corners than gray.
282 // This also seems to be about as wide as one can get and only have a three pixel kernel.
283 // TODO: caculate these at runtime so parameters can be adjusted (esp contrast).
284 static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = {
285 //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted.
286 { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, },
287 //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric
288 { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, },
289 //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted.
290 { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, },
293 for (int y = 0; y < height; ++y) {
294 const uint8_t* srcP = src.getAddr8(0, y);
296 // TODO: this fir filter implementation is straight forward, but slow.
297 // It should be possible to make it much faster.
298 for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) {
299 int fir[LCD_PER_PIXEL] = { 0 };
300 for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4)
301 ; sample_index < SkMin32(sample_x + 8, sample_width)
302 ; ++sample_index, ++coeff_index)
304 int sample_value = srcP[sample_index];
305 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
306 fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value;
309 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) {
310 fir[subpxl_index] /= 0x100;
311 fir[subpxl_index] = SkMin32(fir[subpxl_index], 255);
314 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(fir[0], maskPreBlend.fR);
315 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(fir[1], maskPreBlend.fG);
316 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(fir[2], maskPreBlend.fB);
317 #if SK_SHOW_TEXT_BLIT_COVERAGE
318 r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10);
320 dstP[pixel_x] = SkPack888ToRGB16(r, g, b);
322 dstP = (uint16_t*)((char*)dstP + dstRB);
326 static inline int convert_8_to_1(unsigned byte) {
327 SkASSERT(byte <= 0xFF);
331 static uint8_t pack_8_to_1(const uint8_t alpha[8]) {
333 for (int i = 0; i < 8; ++i) {
335 bits |= convert_8_to_1(alpha[i]);
340 static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) {
341 const int height = mask.fBounds.height();
342 const int width = mask.fBounds.width();
343 const int octs = width >> 3;
344 const int leftOverBits = width & 7;
346 uint8_t* dst = mask.fImage;
347 const int dstPad = mask.fRowBytes - SkAlign8(width)/8;
348 SkASSERT(dstPad >= 0);
350 SkASSERT(width >= 0);
351 SkASSERT(srcRB >= (size_t)width);
352 const size_t srcPad = srcRB - width;
354 for (int y = 0; y < height; ++y) {
355 for (int i = 0; i < octs; ++i) {
356 *dst++ = pack_8_to_1(src);
359 if (leftOverBits > 0) {
362 for (int i = 0; i < leftOverBits; ++i, --shift) {
363 bits |= convert_8_to_1(*src++) << shift;
372 static void generateMask(const SkMask& mask, const SkPath& path,
373 const SkMaskGamma::PreBlend& maskPreBlend) {
376 int srcW = mask.fBounds.width();
377 int srcH = mask.fBounds.height();
380 int dstRB = mask.fRowBytes;
383 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
384 -SkIntToScalar(mask.fBounds.fTop));
386 paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat);
387 switch (mask.fFormat) {
388 case SkMask::kBW_Format:
389 dstRB = 0; // signals we need a copy
391 case SkMask::kA8_Format:
393 case SkMask::kLCD16_Format:
394 // TODO: trigger off LCD orientation
396 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1),
397 -SkIntToScalar(mask.fBounds.fTop));
398 matrix.postScale(SkIntToScalar(4), SK_Scalar1);
399 dstRB = 0; // signals we need a copy
402 SkDEBUGFAIL("unexpected mask format");
406 clip.setRect(SkIRect::MakeWH(dstW, dstH));
408 const SkImageInfo info = SkImageInfo::MakeA8(dstW, dstH);
412 if (!bm.tryAllocPixels(info)) {
413 // can't allocate offscreen, so empty the mask and return
414 sk_bzero(mask.fImage, mask.computeImageSize());
418 bm.installPixels(info, mask.fImage, dstRB);
420 sk_bzero(bm.getPixels(), bm.getSafeSize());
424 draw.fClip = &clip.bwRgn();
425 draw.fMatrix = &matrix;
427 draw.drawPath(path, paint);
429 switch (mask.fFormat) {
430 case SkMask::kBW_Format:
431 packA8ToA1(mask, bm.getAddr8(0, 0), bm.rowBytes());
433 case SkMask::kA8_Format:
434 if (maskPreBlend.isApplicable()) {
435 applyLUTToA8Mask(mask, maskPreBlend.fG);
438 case SkMask::kLCD16_Format:
439 if (maskPreBlend.isApplicable()) {
440 pack4xHToLCD16<true>(bm, mask, maskPreBlend);
442 pack4xHToLCD16<false>(bm, mask, maskPreBlend);
450 static void extract_alpha(const SkMask& dst,
451 const SkPMColor* srcRow, size_t srcRB) {
452 int width = dst.fBounds.width();
453 int height = dst.fBounds.height();
454 int dstRB = dst.fRowBytes;
455 uint8_t* dstRow = dst.fImage;
457 for (int y = 0; y < height; ++y) {
458 for (int x = 0; x < width; ++x) {
459 dstRow[x] = SkGetPackedA32(srcRow[x]);
461 // zero any padding on each row
462 for (int x = width; x < dstRB; ++x) {
466 srcRow = (const SkPMColor*)((const char*)srcRow + srcRB);
470 void SkScalerContext::getImage(const SkGlyph& origGlyph) {
471 const SkGlyph* glyph = &origGlyph;
474 // in case we need to call generateImage on a mask-format that is different
475 // (i.e. larger) than what our caller allocated by looking at origGlyph.
476 SkAutoMalloc tmpGlyphImageStorage;
478 // If we are going to draw-from-path, then we cannot generate color, since
479 // the path only makes a mask. This case should have been caught up in
480 // generateMetrics().
481 SkASSERT(!fGenerateImageFromPath ||
482 SkMask::kARGB32_Format != origGlyph.fMaskFormat);
484 if (fMaskFilter) { // restore the prefilter bounds
485 tmpGlyph.initGlyphIdFrom(origGlyph);
487 // need the original bounds, sans our maskfilter
488 SkMaskFilter* mf = fMaskFilter;
489 fMaskFilter = NULL; // temp disable
490 this->getMetrics(&tmpGlyph);
491 fMaskFilter = mf; // restore
493 // we need the prefilter bounds to be <= filter bounds
494 SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth);
495 SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight);
497 if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) {
498 tmpGlyph.fImage = origGlyph.fImage;
500 tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize());
501 tmpGlyph.fImage = tmpGlyphImageStorage.get();
506 if (fGenerateImageFromPath) {
507 SkPath devPath, fillPath;
508 SkMatrix fillToDevMatrix;
511 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
512 glyph->toMask(&mask);
515 mask.fFormat = SkMask::kA8_Format;
516 sk_bzero(glyph->fImage, mask.computeImageSize());
518 if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL,
520 SkMask::kJustRenderImage_CreateMode)) {
523 if (fPreBlend.isApplicable()) {
524 applyLUTToA8Mask(mask, fPreBlend.fG);
527 SkASSERT(SkMask::kARGB32_Format != mask.fFormat);
528 generateMask(mask, devPath, fPreBlend);
531 generateImage(*glyph);
538 // the src glyph image shouldn't be 3D
539 SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat);
541 SkAutoSMalloc<32*32> a8storage;
542 glyph->toMask(&srcM);
543 if (SkMask::kARGB32_Format == srcM.fFormat) {
544 // now we need to extract the alpha-channel from the glyph's image
545 // and copy it into a temp buffer, and then point srcM at that temp.
546 srcM.fFormat = SkMask::kA8_Format;
547 srcM.fRowBytes = SkAlign4(srcM.fBounds.width());
548 size_t size = srcM.computeImageSize();
549 a8storage.reset(size);
550 srcM.fImage = (uint8_t*)a8storage.get();
552 (const SkPMColor*)glyph->fImage, glyph->rowBytes());
555 fRec.getMatrixFrom2x2(&matrix);
557 if (fMaskFilter->filterMask(&dstM, srcM, matrix, NULL)) {
558 int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width());
559 int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height());
560 int dstRB = origGlyph.rowBytes();
561 int srcRB = dstM.fRowBytes;
563 const uint8_t* src = (const uint8_t*)dstM.fImage;
564 uint8_t* dst = (uint8_t*)origGlyph.fImage;
566 if (SkMask::k3D_Format == dstM.fFormat) {
567 // we have to copy 3 times as much
571 // clean out our glyph, since it may be larger than dstM
572 //sk_bzero(dst, height * dstRB);
574 while (--height >= 0) {
575 memcpy(dst, src, width);
579 SkMask::FreeImage(dstM.fImage);
581 if (fPreBlendForFilter.isApplicable()) {
582 applyLUTToA8Mask(srcM, fPreBlendForFilter.fG);
588 void SkScalerContext::getPath(const SkGlyph& glyph, SkPath* path) {
589 this->internalGetPath(glyph, NULL, path, NULL);
592 void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) {
593 this->generateFontMetrics(fm);
596 SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) {
600 ///////////////////////////////////////////////////////////////////////////////
602 void SkScalerContext::internalGetPath(const SkGlyph& glyph, SkPath* fillPath,
603 SkPath* devPath, SkMatrix* fillToDevMatrix) {
605 generatePath(glyph, &path);
607 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
608 SkFixed dx = glyph.getSubXFixed();
609 SkFixed dy = glyph.getSubYFixed();
611 path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy));
615 if (fRec.fFrameWidth > 0 || fPathEffect != NULL) {
616 // need the path in user-space, with only the point-size applied
617 // so that our stroking and effects will operate the same way they
618 // would if the user had extracted the path themself, and then
621 SkMatrix matrix, inverse;
623 fRec.getMatrixFrom2x2(&matrix);
624 if (!matrix.invert(&inverse)) {
625 // assume fillPath and devPath are already empty.
628 path.transform(inverse, &localPath);
629 // now localPath is only affected by the paint settings, and not the canvas matrix
631 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
633 if (fRec.fFrameWidth > 0) {
634 rec.setStrokeStyle(fRec.fFrameWidth,
635 SkToBool(fRec.fFlags & kFrameAndFill_Flag));
636 // glyphs are always closed contours, so cap type is ignored,
637 // so we just pass something.
638 rec.setStrokeParams(SkPaint::kButt_Cap,
639 (SkPaint::Join)fRec.fStrokeJoin,
645 if (fPathEffect->filterPath(&effectPath, localPath, &rec, NULL)) {
646 localPath.swap(effectPath);
650 if (rec.needToApply()) {
652 if (rec.applyToPath(&strokePath, localPath)) {
653 localPath.swap(strokePath);
657 // now return stuff to the caller
658 if (fillToDevMatrix) {
659 *fillToDevMatrix = matrix;
662 localPath.transform(matrix, devPath);
665 fillPath->swap(localPath);
667 } else { // nothing tricky to do
668 if (fillToDevMatrix) {
669 fillToDevMatrix->reset();
672 if (fillPath == NULL) {
680 fillPath->swap(path);
685 devPath->updateBoundsCache();
688 fillPath->updateBoundsCache();
693 void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const {
694 dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0,
695 fPost2x2[1][0], fPost2x2[1][1], 0,
699 void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const {
700 SkPaint::SetTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX);
703 void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const {
704 this->getLocalMatrix(m);
706 // now concat the device matrix
707 SkMatrix deviceMatrix;
708 this->getMatrixFrom2x2(&deviceMatrix);
709 m->postConcat(deviceMatrix);
712 void SkScalerContextRec::computeMatrices(PreMatrixScale preMatrixScale, SkVector* s, SkMatrix* sA,
713 SkMatrix* GsA, SkMatrix* G_inv, SkMatrix* A_out)
715 // A is the 'total' matrix.
717 this->getSingleMatrix(&A);
719 // The caller may find the 'total' matrix useful when dealing directly with EM sizes.
724 // If the 'total' matrix is singular, set the 'scale' to something finite and zero the matrices.
725 // All underlying ports have issues with zero text size, so use the matricies to zero.
727 // Map the vectors [1,1] and [1,-1] (the EM) through the 'total' matrix.
728 // If the length of one of these vectors is less than 1/256 then an EM filling square will
729 // never affect any pixels.
730 SkVector diag[2] = { { A.getScaleX() + A.getSkewX(), A.getScaleY() + A.getSkewY() },
731 { A.getScaleX() - A.getSkewX(), A.getScaleY() - A.getSkewY() }, };
732 if (diag[0].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero ||
733 diag[1].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero)
747 // GA is the matrix A with rotation removed.
749 bool skewedOrFlipped = A.getSkewX() || A.getSkewY() || A.getScaleX() < 0 || A.getScaleY() < 0;
750 if (skewedOrFlipped) {
751 // h is where A maps the horizontal baseline.
752 SkPoint h = SkPoint::Make(SK_Scalar1, 0);
755 // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0).
757 SkComputeGivensRotation(h, &G);
762 // The 'remainingRotation' is G inverse, which is fairly simple since G is 2x2 rotational.
765 G.get(SkMatrix::kMScaleX), -G.get(SkMatrix::kMSkewX), G.get(SkMatrix::kMTransX),
766 -G.get(SkMatrix::kMSkewY), G.get(SkMatrix::kMScaleY), G.get(SkMatrix::kMTransY),
767 G.get(SkMatrix::kMPersp0), G.get(SkMatrix::kMPersp1), G.get(SkMatrix::kMPersp2));
776 // At this point, given GA, create s.
777 switch (preMatrixScale) {
778 case kFull_PreMatrixScale:
779 s->fX = SkScalarAbs(GA.get(SkMatrix::kMScaleX));
780 s->fY = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
782 case kVertical_PreMatrixScale: {
783 SkScalar yScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
788 case kVerticalInteger_PreMatrixScale: {
789 SkScalar realYScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
790 SkScalar intYScale = SkScalarRoundToScalar(realYScale);
791 if (intYScale == 0) {
792 intYScale = SK_Scalar1;
800 // The 'remaining' matrix sA is the total matrix A without the scale.
801 if (!skewedOrFlipped && (
802 (kFull_PreMatrixScale == preMatrixScale) ||
803 (kVertical_PreMatrixScale == preMatrixScale && A.getScaleX() == A.getScaleY())))
805 // If GA == A and kFull_PreMatrixScale, sA is identity.
806 // If GA == A and kVertical_PreMatrixScale and A.scaleX == A.scaleY, sA is identity.
808 } else if (!skewedOrFlipped && kVertical_PreMatrixScale == preMatrixScale) {
809 // If GA == A and kVertical_PreMatrixScale, sA.scaleY is SK_Scalar1.
811 sA->setScaleX(A.getScaleX() / s->fY);
813 // TODO: like kVertical_PreMatrixScale, kVerticalInteger_PreMatrixScale with int scales.
815 sA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
818 // The 'remainingWithoutRotation' matrix GsA is the non-rotational part of A without the scale.
821 // G is rotational so reorders with the scale.
822 GsA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY));
826 SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix) {
827 SkASSERT(!matrix.hasPerspective());
829 if (0 == matrix[SkMatrix::kMSkewY]) {
830 return kX_SkAxisAlignment;
832 if (0 == matrix[SkMatrix::kMScaleX]) {
833 return kY_SkAxisAlignment;
835 return kNone_SkAxisAlignment;
838 ///////////////////////////////////////////////////////////////////////////////
840 class SkScalerContext_Empty : public SkScalerContext {
842 SkScalerContext_Empty(SkTypeface* face, const SkDescriptor* desc)
843 : SkScalerContext(face, desc) {}
846 unsigned generateGlyphCount() override {
849 uint16_t generateCharToGlyph(SkUnichar uni) override {
852 void generateAdvance(SkGlyph* glyph) override {
853 glyph->zeroMetrics();
855 void generateMetrics(SkGlyph* glyph) override {
856 glyph->zeroMetrics();
858 void generateImage(const SkGlyph& glyph) override {}
859 void generatePath(const SkGlyph& glyph, SkPath* path) override {}
860 void generateFontMetrics(SkPaint::FontMetrics* metrics) override {
862 sk_bzero(metrics, sizeof(*metrics));
867 extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc);
869 SkScalerContext* SkTypeface::createScalerContext(const SkDescriptor* desc,
870 bool allowFailure) const {
871 SkScalerContext* c = this->onCreateScalerContext(desc);
873 if (!c && !allowFailure) {
874 c = SkNEW_ARGS(SkScalerContext_Empty,
875 (const_cast<SkTypeface*>(this), desc));