1 /* 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 8 #include "SkScalerContext.h" 9 10 #include "SkAutoMalloc.h" 11 #include "SkAutoPixmapStorage.h" 12 #include "SkColorPriv.h" 13 #include "SkDescriptor.h" 14 #include "SkDraw.h" 15 #include "SkGlyph.h" 16 #include "SkMakeUnique.h" 17 #include "SkMaskFilter.h" 18 #include "SkMaskGamma.h" 19 #include "SkMatrix22.h" 20 #include "SkPathEffect.h" 21 #include "SkRasterClip.h" 22 #include "SkRasterizer.h" 23 #include "SkReadBuffer.h" 24 #include "SkStroke.h" 25 #include "SkStrokeRec.h" 26 #include "SkWriteBuffer.h" 27 28 #define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3) 29 30 void SkGlyph::toMask(SkMask* mask) const { 31 SkASSERT(mask); 32 33 mask->fImage = (uint8_t*)fImage; 34 mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight); 35 mask->fRowBytes = this->rowBytes(); 36 mask->fFormat = static_cast<SkMask::Format>(fMaskFormat); 37 } 38 39 size_t SkGlyph::computeImageSize() const { 40 const size_t size = this->rowBytes() * fHeight; 41 42 switch (fMaskFormat) { 43 case SkMask::k3D_Format: 44 return 3 * size; 45 default: 46 return size; 47 } 48 } 49 50 void SkGlyph::zeroMetrics() { 51 fAdvanceX = 0; 52 fAdvanceY = 0; 53 fWidth = 0; 54 fHeight = 0; 55 fTop = 0; 56 fLeft = 0; 57 fRsbDelta = 0; 58 fLsbDelta = 0; 59 } 60 61 /////////////////////////////////////////////////////////////////////////////// 62 63 #ifdef SK_DEBUG 64 #define DUMP_RECx 65 #endif 66 67 SkScalerContext::SkScalerContext(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects, 68 const SkDescriptor* desc) 69 : fRec(*static_cast<const Rec*>(desc->findEntry(kRec_SkDescriptorTag, nullptr))) 70 71 , fTypeface(std::move(typeface)) 72 , fPathEffect(sk_ref_sp(effects.fPathEffect)) 73 , fMaskFilter(sk_ref_sp(effects.fMaskFilter)) 74 , fRasterizer(sk_ref_sp(effects.fRasterizer)) 75 // Initialize based on our settings. Subclasses can also force this. 76 , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != nullptr || fRasterizer != nullptr) 77 78 , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec)) 79 , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec) 80 : SkMaskGamma::PreBlend()) 81 { 82 #ifdef DUMP_REC 83 desc->assertChecksum(); 84 SkDebugf("SkScalerContext checksum %x count %d length %d\n", 85 desc->getChecksum(), desc->getCount(), desc->getLength()); 86 SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n", 87 rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0], 88 rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]); 89 SkDebugf(" frame %g miter %g hints %d framefill %d format %d join %d cap %d\n", 90 rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill, 91 rec->fMaskFormat, rec->fStrokeJoin, rec->fStrokeCap); 92 SkDebugf(" pathEffect %x maskFilter %x\n", 93 desc->findEntry(kPathEffect_SkDescriptorTag, nullptr), 94 desc->findEntry(kMaskFilter_SkDescriptorTag, nullptr)); 95 #endif 96 } 97 98 SkScalerContext::~SkScalerContext() {} 99 100 void SkScalerContext::getAdvance(SkGlyph* glyph) { 101 // mark us as just having a valid advance 102 glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE; 103 // we mark the format before making the call, in case the impl 104 // internally ends up calling its generateMetrics, which is OK 105 // albeit slower than strictly necessary 106 generateAdvance(glyph); 107 } 108 109 void SkScalerContext::getMetrics(SkGlyph* glyph) { 110 generateMetrics(glyph); 111 112 // for now we have separate cache entries for devkerning on and off 113 // in the future we might share caches, but make our measure/draw 114 // code make the distinction. Thus we zap the values if the caller 115 // has not asked for them. 116 if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) { 117 // no devkern, so zap the fields 118 glyph->fLsbDelta = glyph->fRsbDelta = 0; 119 } 120 121 // if either dimension is empty, zap the image bounds of the glyph 122 if (0 == glyph->fWidth || 0 == glyph->fHeight) { 123 glyph->fWidth = 0; 124 glyph->fHeight = 0; 125 glyph->fTop = 0; 126 glyph->fLeft = 0; 127 glyph->fMaskFormat = 0; 128 return; 129 } 130 131 if (fGenerateImageFromPath) { 132 SkPath devPath, fillPath; 133 SkMatrix fillToDevMatrix; 134 135 this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix); 136 137 if (fRasterizer) { 138 SkMask mask; 139 140 if (fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr, 141 fMaskFilter.get(), &mask, 142 SkMask::kJustComputeBounds_CreateMode)) { 143 glyph->fLeft = mask.fBounds.fLeft; 144 glyph->fTop = mask.fBounds.fTop; 145 glyph->fWidth = SkToU16(mask.fBounds.width()); 146 glyph->fHeight = SkToU16(mask.fBounds.height()); 147 } else { 148 goto SK_ERROR; 149 } 150 } else { 151 // just use devPath 152 const SkIRect ir = devPath.getBounds().roundOut(); 153 154 if (ir.isEmpty() || !ir.is16Bit()) { 155 goto SK_ERROR; 156 } 157 glyph->fLeft = ir.fLeft; 158 glyph->fTop = ir.fTop; 159 glyph->fWidth = SkToU16(ir.width()); 160 glyph->fHeight = SkToU16(ir.height()); 161 162 if (glyph->fWidth > 0) { 163 switch (fRec.fMaskFormat) { 164 case SkMask::kLCD16_Format: 165 glyph->fWidth += 2; 166 glyph->fLeft -= 1; 167 break; 168 default: 169 break; 170 } 171 } 172 } 173 } 174 175 if (SkMask::kARGB32_Format != glyph->fMaskFormat) { 176 glyph->fMaskFormat = fRec.fMaskFormat; 177 } 178 179 // If we are going to create the mask, then we cannot keep the color 180 if ((fGenerateImageFromPath || fMaskFilter) && 181 SkMask::kARGB32_Format == glyph->fMaskFormat) { 182 glyph->fMaskFormat = SkMask::kA8_Format; 183 } 184 185 if (fMaskFilter) { 186 SkMask src, dst; 187 SkMatrix matrix; 188 189 glyph->toMask(&src); 190 fRec.getMatrixFrom2x2(&matrix); 191 192 src.fImage = nullptr; // only want the bounds from the filter 193 if (fMaskFilter->filterMask(&dst, src, matrix, nullptr)) { 194 if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) { 195 goto SK_ERROR; 196 } 197 SkASSERT(dst.fImage == nullptr); 198 glyph->fLeft = dst.fBounds.fLeft; 199 glyph->fTop = dst.fBounds.fTop; 200 glyph->fWidth = SkToU16(dst.fBounds.width()); 201 glyph->fHeight = SkToU16(dst.fBounds.height()); 202 glyph->fMaskFormat = dst.fFormat; 203 } 204 } 205 return; 206 207 SK_ERROR: 208 // draw nothing 'cause we failed 209 glyph->fLeft = 0; 210 glyph->fTop = 0; 211 glyph->fWidth = 0; 212 glyph->fHeight = 0; 213 // put a valid value here, in case it was earlier set to 214 // MASK_FORMAT_JUST_ADVANCE 215 glyph->fMaskFormat = fRec.fMaskFormat; 216 } 217 218 #define SK_SHOW_TEXT_BLIT_COVERAGE 0 219 220 static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) { 221 uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage; 222 unsigned rowBytes = mask.fRowBytes; 223 224 for (int y = mask.fBounds.height() - 1; y >= 0; --y) { 225 for (int x = mask.fBounds.width() - 1; x >= 0; --x) { 226 dst[x] = lut[dst[x]]; 227 } 228 dst += rowBytes; 229 } 230 } 231 232 template<bool APPLY_PREBLEND> 233 static void pack4xHToLCD16(const SkPixmap& src, const SkMask& dst, 234 const SkMaskGamma::PreBlend& maskPreBlend) { 235 #define SAMPLES_PER_PIXEL 4 236 #define LCD_PER_PIXEL 3 237 SkASSERT(kAlpha_8_SkColorType == src.colorType()); 238 SkASSERT(SkMask::kLCD16_Format == dst.fFormat); 239 240 const int sample_width = src.width(); 241 const int height = src.height(); 242 243 uint16_t* dstP = (uint16_t*)dst.fImage; 244 size_t dstRB = dst.fRowBytes; 245 // An N tap FIR is defined by 246 // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N] 247 // or 248 // out[n] = sum(i, 0, N, coeff[i]*x[n-i]) 249 250 // The strategy is to use one FIR (different coefficients) for each of r, g, and b. 251 // This means using every 4th FIR output value of each FIR and discarding the rest. 252 // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'. 253 // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.) 254 255 // These are in some fixed point repesentation. 256 // Adding up to more than one simulates ink spread. 257 // For implementation reasons, these should never add up to more than two. 258 259 // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast'). 260 // Calculated using tools/generate_fir_coeff.py 261 // With this one almost no fringing is ever seen, but it is imperceptibly blurry. 262 // The lcd smoothed text is almost imperceptibly different from gray, 263 // but is still sharper on small stems and small rounded corners than gray. 264 // This also seems to be about as wide as one can get and only have a three pixel kernel. 265 // TODO: caculate these at runtime so parameters can be adjusted (esp contrast). 266 static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = { 267 //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted. 268 { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, }, 269 //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric 270 { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, }, 271 //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted. 272 { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, }, 273 }; 274 275 for (int y = 0; y < height; ++y) { 276 const uint8_t* srcP = src.addr8(0, y); 277 278 // TODO: this fir filter implementation is straight forward, but slow. 279 // It should be possible to make it much faster. 280 for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) { 281 int fir[LCD_PER_PIXEL] = { 0 }; 282 for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4) 283 ; sample_index < SkMin32(sample_x + 8, sample_width) 284 ; ++sample_index, ++coeff_index) 285 { 286 int sample_value = srcP[sample_index]; 287 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { 288 fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value; 289 } 290 } 291 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { 292 fir[subpxl_index] /= 0x100; 293 fir[subpxl_index] = SkMin32(fir[subpxl_index], 255); 294 } 295 296 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(fir[0], maskPreBlend.fR); 297 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(fir[1], maskPreBlend.fG); 298 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(fir[2], maskPreBlend.fB); 299 #if SK_SHOW_TEXT_BLIT_COVERAGE 300 r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10); 301 #endif 302 dstP[pixel_x] = SkPack888ToRGB16(r, g, b); 303 } 304 dstP = (uint16_t*)((char*)dstP + dstRB); 305 } 306 } 307 308 static inline int convert_8_to_1(unsigned byte) { 309 SkASSERT(byte <= 0xFF); 310 return byte >> 7; 311 } 312 313 static uint8_t pack_8_to_1(const uint8_t alpha[8]) { 314 unsigned bits = 0; 315 for (int i = 0; i < 8; ++i) { 316 bits <<= 1; 317 bits |= convert_8_to_1(alpha[i]); 318 } 319 return SkToU8(bits); 320 } 321 322 static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) { 323 const int height = mask.fBounds.height(); 324 const int width = mask.fBounds.width(); 325 const int octs = width >> 3; 326 const int leftOverBits = width & 7; 327 328 uint8_t* dst = mask.fImage; 329 const int dstPad = mask.fRowBytes - SkAlign8(width)/8; 330 SkASSERT(dstPad >= 0); 331 332 SkASSERT(width >= 0); 333 SkASSERT(srcRB >= (size_t)width); 334 const size_t srcPad = srcRB - width; 335 336 for (int y = 0; y < height; ++y) { 337 for (int i = 0; i < octs; ++i) { 338 *dst++ = pack_8_to_1(src); 339 src += 8; 340 } 341 if (leftOverBits > 0) { 342 unsigned bits = 0; 343 int shift = 7; 344 for (int i = 0; i < leftOverBits; ++i, --shift) { 345 bits |= convert_8_to_1(*src++) << shift; 346 } 347 *dst++ = bits; 348 } 349 src += srcPad; 350 dst += dstPad; 351 } 352 } 353 354 static void generateMask(const SkMask& mask, const SkPath& path, 355 const SkMaskGamma::PreBlend& maskPreBlend) { 356 SkPaint paint; 357 358 int srcW = mask.fBounds.width(); 359 int srcH = mask.fBounds.height(); 360 int dstW = srcW; 361 int dstH = srcH; 362 int dstRB = mask.fRowBytes; 363 364 SkMatrix matrix; 365 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), 366 -SkIntToScalar(mask.fBounds.fTop)); 367 368 paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat); 369 switch (mask.fFormat) { 370 case SkMask::kBW_Format: 371 dstRB = 0; // signals we need a copy 372 break; 373 case SkMask::kA8_Format: 374 break; 375 case SkMask::kLCD16_Format: 376 // TODO: trigger off LCD orientation 377 dstW = 4*dstW - 8; 378 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1), 379 -SkIntToScalar(mask.fBounds.fTop)); 380 matrix.postScale(SkIntToScalar(4), SK_Scalar1); 381 dstRB = 0; // signals we need a copy 382 break; 383 default: 384 SkDEBUGFAIL("unexpected mask format"); 385 } 386 387 SkRasterClip clip; 388 clip.setRect(SkIRect::MakeWH(dstW, dstH)); 389 390 const SkImageInfo info = SkImageInfo::MakeA8(dstW, dstH); 391 SkAutoPixmapStorage dst; 392 393 if (0 == dstRB) { 394 if (!dst.tryAlloc(info)) { 395 // can't allocate offscreen, so empty the mask and return 396 sk_bzero(mask.fImage, mask.computeImageSize()); 397 return; 398 } 399 } else { 400 dst.reset(info, mask.fImage, dstRB); 401 } 402 sk_bzero(dst.writable_addr(), dst.getSafeSize()); 403 404 SkDraw draw; 405 draw.fDst = dst; 406 draw.fRC = &clip; 407 draw.fMatrix = &matrix; 408 draw.drawPath(path, paint); 409 410 switch (mask.fFormat) { 411 case SkMask::kBW_Format: 412 packA8ToA1(mask, dst.addr8(0, 0), dst.rowBytes()); 413 break; 414 case SkMask::kA8_Format: 415 if (maskPreBlend.isApplicable()) { 416 applyLUTToA8Mask(mask, maskPreBlend.fG); 417 } 418 break; 419 case SkMask::kLCD16_Format: 420 if (maskPreBlend.isApplicable()) { 421 pack4xHToLCD16<true>(dst, mask, maskPreBlend); 422 } else { 423 pack4xHToLCD16<false>(dst, mask, maskPreBlend); 424 } 425 break; 426 default: 427 break; 428 } 429 } 430 431 static void extract_alpha(const SkMask& dst, 432 const SkPMColor* srcRow, size_t srcRB) { 433 int width = dst.fBounds.width(); 434 int height = dst.fBounds.height(); 435 int dstRB = dst.fRowBytes; 436 uint8_t* dstRow = dst.fImage; 437 438 for (int y = 0; y < height; ++y) { 439 for (int x = 0; x < width; ++x) { 440 dstRow[x] = SkGetPackedA32(srcRow[x]); 441 } 442 // zero any padding on each row 443 for (int x = width; x < dstRB; ++x) { 444 dstRow[x] = 0; 445 } 446 dstRow += dstRB; 447 srcRow = (const SkPMColor*)((const char*)srcRow + srcRB); 448 } 449 } 450 451 void SkScalerContext::getImage(const SkGlyph& origGlyph) { 452 const SkGlyph* glyph = &origGlyph; 453 SkGlyph tmpGlyph; 454 455 // in case we need to call generateImage on a mask-format that is different 456 // (i.e. larger) than what our caller allocated by looking at origGlyph. 457 SkAutoMalloc tmpGlyphImageStorage; 458 459 // If we are going to draw-from-path, then we cannot generate color, since 460 // the path only makes a mask. This case should have been caught up in 461 // generateMetrics(). 462 SkASSERT(!fGenerateImageFromPath || 463 SkMask::kARGB32_Format != origGlyph.fMaskFormat); 464 465 if (fMaskFilter) { // restore the prefilter bounds 466 tmpGlyph.initWithGlyphID(origGlyph.getPackedID()); 467 468 // need the original bounds, sans our maskfilter 469 SkMaskFilter* mf = fMaskFilter.release(); // temp disable 470 this->getMetrics(&tmpGlyph); 471 fMaskFilter = sk_sp<SkMaskFilter>(mf); // restore 472 473 // we need the prefilter bounds to be <= filter bounds 474 SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth); 475 SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight); 476 477 if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) { 478 tmpGlyph.fImage = origGlyph.fImage; 479 } else { 480 tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize()); 481 tmpGlyph.fImage = tmpGlyphImageStorage.get(); 482 } 483 glyph = &tmpGlyph; 484 } 485 486 if (fGenerateImageFromPath) { 487 SkPath devPath, fillPath; 488 SkMatrix fillToDevMatrix; 489 SkMask mask; 490 491 this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix); 492 glyph->toMask(&mask); 493 494 if (fRasterizer) { 495 mask.fFormat = SkMask::kA8_Format; 496 sk_bzero(glyph->fImage, mask.computeImageSize()); 497 498 if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, nullptr, 499 fMaskFilter.get(), &mask, 500 SkMask::kJustRenderImage_CreateMode)) { 501 return; 502 } 503 if (fPreBlend.isApplicable()) { 504 applyLUTToA8Mask(mask, fPreBlend.fG); 505 } 506 } else { 507 SkASSERT(SkMask::kARGB32_Format != mask.fFormat); 508 generateMask(mask, devPath, fPreBlend); 509 } 510 } else { 511 generateImage(*glyph); 512 } 513 514 if (fMaskFilter) { 515 SkMask srcM, dstM; 516 SkMatrix matrix; 517 518 // the src glyph image shouldn't be 3D 519 SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat); 520 521 SkAutoSMalloc<32*32> a8storage; 522 glyph->toMask(&srcM); 523 if (SkMask::kARGB32_Format == srcM.fFormat) { 524 // now we need to extract the alpha-channel from the glyph's image 525 // and copy it into a temp buffer, and then point srcM at that temp. 526 srcM.fFormat = SkMask::kA8_Format; 527 srcM.fRowBytes = SkAlign4(srcM.fBounds.width()); 528 size_t size = srcM.computeImageSize(); 529 a8storage.reset(size); 530 srcM.fImage = (uint8_t*)a8storage.get(); 531 extract_alpha(srcM, 532 (const SkPMColor*)glyph->fImage, glyph->rowBytes()); 533 } 534 535 fRec.getMatrixFrom2x2(&matrix); 536 537 if (fMaskFilter->filterMask(&dstM, srcM, matrix, nullptr)) { 538 int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width()); 539 int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height()); 540 int dstRB = origGlyph.rowBytes(); 541 int srcRB = dstM.fRowBytes; 542 543 const uint8_t* src = (const uint8_t*)dstM.fImage; 544 uint8_t* dst = (uint8_t*)origGlyph.fImage; 545 546 if (SkMask::k3D_Format == dstM.fFormat) { 547 // we have to copy 3 times as much 548 height *= 3; 549 } 550 551 // clean out our glyph, since it may be larger than dstM 552 //sk_bzero(dst, height * dstRB); 553 554 while (--height >= 0) { 555 memcpy(dst, src, width); 556 src += srcRB; 557 dst += dstRB; 558 } 559 SkMask::FreeImage(dstM.fImage); 560 561 if (fPreBlendForFilter.isApplicable()) { 562 applyLUTToA8Mask(srcM, fPreBlendForFilter.fG); 563 } 564 } 565 } 566 } 567 568 void SkScalerContext::getPath(SkPackedGlyphID glyphID, SkPath* path) { 569 this->internalGetPath(glyphID, nullptr, path, nullptr); 570 } 571 572 void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) { 573 SkASSERT(fm); 574 this->generateFontMetrics(fm); 575 } 576 577 SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) { 578 return 0; 579 } 580 581 /////////////////////////////////////////////////////////////////////////////// 582 583 void SkScalerContext::internalGetPath(SkPackedGlyphID glyphID, SkPath* fillPath, 584 SkPath* devPath, SkMatrix* fillToDevMatrix) { 585 SkPath path; 586 generatePath(glyphID.code(), &path); 587 588 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { 589 SkFixed dx = glyphID.getSubXFixed(); 590 SkFixed dy = glyphID.getSubYFixed(); 591 if (dx | dy) { 592 path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy)); 593 } 594 } 595 596 if (fRec.fFrameWidth > 0 || fPathEffect != nullptr) { 597 // need the path in user-space, with only the point-size applied 598 // so that our stroking and effects will operate the same way they 599 // would if the user had extracted the path themself, and then 600 // called drawPath 601 SkPath localPath; 602 SkMatrix matrix, inverse; 603 604 fRec.getMatrixFrom2x2(&matrix); 605 if (!matrix.invert(&inverse)) { 606 // assume fillPath and devPath are already empty. 607 return; 608 } 609 path.transform(inverse, &localPath); 610 // now localPath is only affected by the paint settings, and not the canvas matrix 611 612 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); 613 614 if (fRec.fFrameWidth > 0) { 615 rec.setStrokeStyle(fRec.fFrameWidth, 616 SkToBool(fRec.fFlags & kFrameAndFill_Flag)); 617 // glyphs are always closed contours, so cap type is ignored, 618 // so we just pass something. 619 rec.setStrokeParams((SkPaint::Cap)fRec.fStrokeCap, 620 (SkPaint::Join)fRec.fStrokeJoin, 621 fRec.fMiterLimit); 622 } 623 624 if (fPathEffect) { 625 SkPath effectPath; 626 if (fPathEffect->filterPath(&effectPath, localPath, &rec, nullptr)) { 627 localPath.swap(effectPath); 628 } 629 } 630 631 if (rec.needToApply()) { 632 SkPath strokePath; 633 if (rec.applyToPath(&strokePath, localPath)) { 634 localPath.swap(strokePath); 635 } 636 } 637 638 // now return stuff to the caller 639 if (fillToDevMatrix) { 640 *fillToDevMatrix = matrix; 641 } 642 if (devPath) { 643 localPath.transform(matrix, devPath); 644 } 645 if (fillPath) { 646 fillPath->swap(localPath); 647 } 648 } else { // nothing tricky to do 649 if (fillToDevMatrix) { 650 fillToDevMatrix->reset(); 651 } 652 if (devPath) { 653 if (fillPath == nullptr) { 654 devPath->swap(path); 655 } else { 656 *devPath = path; 657 } 658 } 659 660 if (fillPath) { 661 fillPath->swap(path); 662 } 663 } 664 665 if (devPath) { 666 devPath->updateBoundsCache(); 667 } 668 if (fillPath) { 669 fillPath->updateBoundsCache(); 670 } 671 } 672 673 674 void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const { 675 dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0, 676 fPost2x2[1][0], fPost2x2[1][1], 0, 677 0, 0, 1); 678 } 679 680 void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const { 681 SkPaint::SetTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX); 682 } 683 684 void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const { 685 this->getLocalMatrix(m); 686 687 // now concat the device matrix 688 SkMatrix deviceMatrix; 689 this->getMatrixFrom2x2(&deviceMatrix); 690 m->postConcat(deviceMatrix); 691 } 692 693 bool SkScalerContextRec::computeMatrices(PreMatrixScale preMatrixScale, SkVector* s, SkMatrix* sA, 694 SkMatrix* GsA, SkMatrix* G_inv, SkMatrix* A_out) 695 { 696 // A is the 'total' matrix. 697 SkMatrix A; 698 this->getSingleMatrix(&A); 699 700 // The caller may find the 'total' matrix useful when dealing directly with EM sizes. 701 if (A_out) { 702 *A_out = A; 703 } 704 705 // If the 'total' matrix is singular, set the 'scale' to something finite and zero the matrices. 706 // All underlying ports have issues with zero text size, so use the matricies to zero. 707 708 // Map the vectors [0,1], [1,0], [1,1] and [1,-1] (the EM) through the 'total' matrix. 709 // If the length of one of these vectors is less than 1/256 then an EM filling square will 710 // never affect any pixels. 711 SkVector diag[4] = { { A.getScaleX() , A.getSkewY() }, 712 { A.getSkewX(), A.getScaleY() }, 713 { A.getScaleX() + A.getSkewX(), A.getScaleY() + A.getSkewY() }, 714 { A.getScaleX() - A.getSkewX(), A.getScaleY() - A.getSkewY() }, }; 715 if (diag[0].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero || 716 diag[1].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero || 717 diag[2].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero || 718 diag[3].lengthSqd() <= SK_ScalarNearlyZero * SK_ScalarNearlyZero) 719 { 720 s->fX = SK_Scalar1; 721 s->fY = SK_Scalar1; 722 sA->setScale(0, 0); 723 if (GsA) { 724 GsA->setScale(0, 0); 725 } 726 if (G_inv) { 727 G_inv->reset(); 728 } 729 return false; 730 } 731 732 // GA is the matrix A with rotation removed. 733 SkMatrix GA; 734 bool skewedOrFlipped = A.getSkewX() || A.getSkewY() || A.getScaleX() < 0 || A.getScaleY() < 0; 735 if (skewedOrFlipped) { 736 // h is where A maps the horizontal baseline. 737 SkPoint h = SkPoint::Make(SK_Scalar1, 0); 738 A.mapPoints(&h, 1); 739 740 // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0). 741 SkMatrix G; 742 SkComputeGivensRotation(h, &G); 743 744 GA = G; 745 GA.preConcat(A); 746 747 // The 'remainingRotation' is G inverse, which is fairly simple since G is 2x2 rotational. 748 if (G_inv) { 749 G_inv->setAll( 750 G.get(SkMatrix::kMScaleX), -G.get(SkMatrix::kMSkewX), G.get(SkMatrix::kMTransX), 751 -G.get(SkMatrix::kMSkewY), G.get(SkMatrix::kMScaleY), G.get(SkMatrix::kMTransY), 752 G.get(SkMatrix::kMPersp0), G.get(SkMatrix::kMPersp1), G.get(SkMatrix::kMPersp2)); 753 } 754 } else { 755 GA = A; 756 if (G_inv) { 757 G_inv->reset(); 758 } 759 } 760 761 // At this point, given GA, create s. 762 switch (preMatrixScale) { 763 case kFull_PreMatrixScale: 764 s->fX = SkScalarAbs(GA.get(SkMatrix::kMScaleX)); 765 s->fY = SkScalarAbs(GA.get(SkMatrix::kMScaleY)); 766 break; 767 case kVertical_PreMatrixScale: { 768 SkScalar yScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY)); 769 s->fX = yScale; 770 s->fY = yScale; 771 break; 772 } 773 case kVerticalInteger_PreMatrixScale: { 774 SkScalar realYScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY)); 775 SkScalar intYScale = SkScalarRoundToScalar(realYScale); 776 if (intYScale == 0) { 777 intYScale = SK_Scalar1; 778 } 779 s->fX = intYScale; 780 s->fY = intYScale; 781 break; 782 } 783 } 784 785 // The 'remaining' matrix sA is the total matrix A without the scale. 786 if (!skewedOrFlipped && ( 787 (kFull_PreMatrixScale == preMatrixScale) || 788 (kVertical_PreMatrixScale == preMatrixScale && A.getScaleX() == A.getScaleY()))) 789 { 790 // If GA == A and kFull_PreMatrixScale, sA is identity. 791 // If GA == A and kVertical_PreMatrixScale and A.scaleX == A.scaleY, sA is identity. 792 sA->reset(); 793 } else if (!skewedOrFlipped && kVertical_PreMatrixScale == preMatrixScale) { 794 // If GA == A and kVertical_PreMatrixScale, sA.scaleY is SK_Scalar1. 795 sA->reset(); 796 sA->setScaleX(A.getScaleX() / s->fY); 797 } else { 798 // TODO: like kVertical_PreMatrixScale, kVerticalInteger_PreMatrixScale with int scales. 799 *sA = A; 800 sA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY)); 801 } 802 803 // The 'remainingWithoutRotation' matrix GsA is the non-rotational part of A without the scale. 804 if (GsA) { 805 *GsA = GA; 806 // G is rotational so reorders with the scale. 807 GsA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY)); 808 } 809 810 return true; 811 } 812 813 SkAxisAlignment SkScalerContext::computeAxisAlignmentForHText() { 814 // Why fPost2x2 can be used here. 815 // getSingleMatrix multiplies in getLocalMatrix, which consists of 816 // * fTextSize (a scale, which has no effect) 817 // * fPreScaleX (a scale in x, which has no effect) 818 // * fPreSkewX (has no effect, but would on vertical text alignment). 819 // In other words, making the text bigger, stretching it along the 820 // horizontal axis, or fake italicizing it does not move the baseline. 821 822 if (0 == fRec.fPost2x2[1][0]) { 823 // The x axis is mapped onto the x axis. 824 return kX_SkAxisAlignment; 825 } 826 if (0 == fRec.fPost2x2[0][0]) { 827 // The x axis is mapped onto the y axis. 828 return kY_SkAxisAlignment; 829 } 830 return kNone_SkAxisAlignment; 831 } 832 833 /////////////////////////////////////////////////////////////////////////////// 834 835 class SkScalerContext_Empty : public SkScalerContext { 836 public: 837 SkScalerContext_Empty(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects, 838 const SkDescriptor* desc) 839 : SkScalerContext(std::move(typeface), effects, desc) {} 840 841 protected: 842 unsigned generateGlyphCount() override { 843 return 0; 844 } 845 uint16_t generateCharToGlyph(SkUnichar uni) override { 846 return 0; 847 } 848 void generateAdvance(SkGlyph* glyph) override { 849 glyph->zeroMetrics(); 850 } 851 void generateMetrics(SkGlyph* glyph) override { 852 glyph->zeroMetrics(); 853 } 854 void generateImage(const SkGlyph& glyph) override {} 855 void generatePath(SkGlyphID glyph, SkPath* path) override {} 856 void generateFontMetrics(SkPaint::FontMetrics* metrics) override { 857 if (metrics) { 858 sk_bzero(metrics, sizeof(*metrics)); 859 } 860 } 861 }; 862 863 extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc); 864 865 std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext( 866 const SkScalerContextEffects& effects, const SkDescriptor* desc, bool allowFailure) const 867 { 868 std::unique_ptr<SkScalerContext> c(this->onCreateScalerContext(effects, desc)); 869 if (!c && !allowFailure) { 870 c = skstd::make_unique<SkScalerContext_Empty>(sk_ref_sp(const_cast<SkTypeface*>(this)), 871 effects, desc); 872 } 873 return c; 874 } 875