1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #include "SkScalerContext.h" 11 #include "SkColorPriv.h" 12 #include "SkDescriptor.h" 13 #include "SkDraw.h" 14 #include "SkFontHost.h" 15 #include "SkGlyph.h" 16 #include "SkMaskFilter.h" 17 #include "SkMaskGamma.h" 18 #include "SkReadBuffer.h" 19 #include "SkWriteBuffer.h" 20 #include "SkPathEffect.h" 21 #include "SkRasterizer.h" 22 #include "SkRasterClip.h" 23 #include "SkStroke.h" 24 #include "SkThread.h" 25 26 #define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3) 27 28 void 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(); 34 mask->fFormat = static_cast<SkMask::Format>(fMaskFormat); 35 } 36 37 size_t SkGlyph::computeImageSize() const { 38 const size_t size = this->rowBytes() * fHeight; 39 40 switch (fMaskFormat) { 41 case SkMask::k3D_Format: 42 return 3 * size; 43 default: 44 return size; 45 } 46 } 47 48 void 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 61 #ifdef SK_DEBUG 62 #define DUMP_RECx 63 #endif 64 65 static SkFlattenable* load_flattenable(const SkDescriptor* desc, uint32_t tag, 66 SkFlattenable::Type ft) { 67 SkFlattenable* obj = NULL; 68 uint32_t len; 69 const void* data = desc->findEntry(tag, &len); 70 71 if (data) { 72 SkReadBuffer buffer(data, len); 73 obj = buffer.readFlattenable(ft); 74 SkASSERT(buffer.offset() == buffer.size()); 75 } 76 return obj; 77 } 78 79 SkScalerContext::SkScalerContext(SkTypeface* typeface, const SkDescriptor* desc) 80 : fRec(*static_cast<const Rec*>(desc->findEntry(kRec_SkDescriptorTag, NULL))) 81 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) 91 92 , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec)) 93 , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec) 94 : SkMaskGamma::PreBlend()) 95 { 96 #ifdef DUMP_REC 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)); 109 #endif 110 } 111 112 SkScalerContext::~SkScalerContext() { 113 SkSafeUnref(fPathEffect); 114 SkSafeUnref(fMaskFilter); 115 SkSafeUnref(fRasterizer); 116 } 117 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); 125 } 126 127 void SkScalerContext::getMetrics(SkGlyph* glyph) { 128 generateMetrics(glyph); 129 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; 137 } 138 139 // if either dimension is empty, zap the image bounds of the glyph 140 if (0 == glyph->fWidth || 0 == glyph->fHeight) { 141 glyph->fWidth = 0; 142 glyph->fHeight = 0; 143 glyph->fTop = 0; 144 glyph->fLeft = 0; 145 glyph->fMaskFormat = 0; 146 return; 147 } 148 149 if (fGenerateImageFromPath) { 150 SkPath devPath, fillPath; 151 SkMatrix fillToDevMatrix; 152 153 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); 154 155 if (fRasterizer) { 156 SkMask mask; 157 158 if (fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, 159 fMaskFilter, &mask, 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()); 165 } else { 166 goto SK_ERROR; 167 } 168 } else { 169 // just use devPath 170 SkIRect ir; 171 devPath.getBounds().roundOut(&ir); 172 173 if (ir.isEmpty() || !ir.is16Bit()) { 174 goto SK_ERROR; 175 } 176 glyph->fLeft = ir.fLeft; 177 glyph->fTop = ir.fTop; 178 glyph->fWidth = SkToU16(ir.width()); 179 glyph->fHeight = SkToU16(ir.height()); 180 181 if (glyph->fWidth > 0) { 182 switch (fRec.fMaskFormat) { 183 case SkMask::kLCD16_Format: 184 case SkMask::kLCD32_Format: 185 glyph->fWidth += 2; 186 glyph->fLeft -= 1; 187 break; 188 default: 189 break; 190 } 191 } 192 } 193 } 194 195 if (SkMask::kARGB32_Format != glyph->fMaskFormat) { 196 glyph->fMaskFormat = fRec.fMaskFormat; 197 } 198 199 // If we are going to create the mask, then we cannot keep the color 200 if ((fGenerateImageFromPath || fMaskFilter) && 201 SkMask::kARGB32_Format == glyph->fMaskFormat) { 202 glyph->fMaskFormat = SkMask::kA8_Format; 203 } 204 205 if (fMaskFilter) { 206 SkMask src, dst; 207 SkMatrix matrix; 208 209 glyph->toMask(&src); 210 fRec.getMatrixFrom2x2(&matrix); 211 212 src.fImage = NULL; // only want the bounds from the filter 213 if (fMaskFilter->filterMask(&dst, src, matrix, NULL)) { 214 if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) { 215 goto SK_ERROR; 216 } 217 SkASSERT(dst.fImage == NULL); 218 glyph->fLeft = dst.fBounds.fLeft; 219 glyph->fTop = dst.fBounds.fTop; 220 glyph->fWidth = SkToU16(dst.fBounds.width()); 221 glyph->fHeight = SkToU16(dst.fBounds.height()); 222 glyph->fMaskFormat = dst.fFormat; 223 } 224 } 225 return; 226 227 SK_ERROR: 228 // draw nothing 'cause we failed 229 glyph->fLeft = 0; 230 glyph->fTop = 0; 231 glyph->fWidth = 0; 232 glyph->fHeight = 0; 233 // put a valid value here, in case it was earlier set to 234 // MASK_FORMAT_JUST_ADVANCE 235 glyph->fMaskFormat = fRec.fMaskFormat; 236 } 237 238 #define SK_SHOW_TEXT_BLIT_COVERAGE 0 239 240 static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) { 241 uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage; 242 unsigned rowBytes = mask.fRowBytes; 243 244 for (int y = mask.fBounds.height() - 1; y >= 0; --y) { 245 for (int x = mask.fBounds.width() - 1; x >= 0; --x) { 246 dst[x] = lut[dst[x]]; 247 } 248 dst += rowBytes; 249 } 250 } 251 252 template<bool APPLY_PREBLEND> 253 static void pack4xHToLCD16(const SkBitmap& src, const SkMask& dst, 254 const SkMaskGamma::PreBlend& maskPreBlend) { 255 #define SAMPLES_PER_PIXEL 4 256 #define LCD_PER_PIXEL 3 257 SkASSERT(kAlpha_8_SkColorType == src.colorType()); 258 SkASSERT(SkMask::kLCD16_Format == dst.fFormat); 259 260 const int sample_width = src.width(); 261 const int height = src.height(); 262 263 uint16_t* dstP = (uint16_t*)dst.fImage; 264 size_t dstRB = dst.fRowBytes; 265 // An N tap FIR is defined by 266 // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N] 267 // or 268 // out[n] = sum(i, 0, N, coeff[i]*x[n-i]) 269 270 // The strategy is to use one FIR (different coefficients) for each of r, g, and b. 271 // This means using every 4th FIR output value of each FIR and discarding the rest. 272 // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'. 273 // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.) 274 275 // These are in some fixed point repesentation. 276 // Adding up to more than one simulates ink spread. 277 // For implementation reasons, these should never add up to more than two. 278 279 // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast'). 280 // Calculated using tools/generate_fir_coeff.py 281 // With this one almost no fringing is ever seen, but it is imperceptibly blurry. 282 // The lcd smoothed text is almost imperceptibly different from gray, 283 // but is still sharper on small stems and small rounded corners than gray. 284 // This also seems to be about as wide as one can get and only have a three pixel kernel. 285 // TODO: caculate these at runtime so parameters can be adjusted (esp contrast). 286 static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = { 287 //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted. 288 { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, }, 289 //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric 290 { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, }, 291 //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted. 292 { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, }, 293 }; 294 295 for (int y = 0; y < height; ++y) { 296 const uint8_t* srcP = src.getAddr8(0, y); 297 298 // TODO: this fir filter implementation is straight forward, but slow. 299 // It should be possible to make it much faster. 300 for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) { 301 int fir[LCD_PER_PIXEL] = { 0 }; 302 for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4) 303 ; sample_index < SkMin32(sample_x + 8, sample_width) 304 ; ++sample_index, ++coeff_index) 305 { 306 int sample_value = srcP[sample_index]; 307 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { 308 fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value; 309 } 310 } 311 for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { 312 fir[subpxl_index] /= 0x100; 313 fir[subpxl_index] = SkMin32(fir[subpxl_index], 255); 314 } 315 316 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(fir[0], maskPreBlend.fR); 317 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(fir[1], maskPreBlend.fG); 318 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(fir[2], maskPreBlend.fB); 319 #if SK_SHOW_TEXT_BLIT_COVERAGE 320 r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10); 321 #endif 322 dstP[pixel_x] = SkPack888ToRGB16(r, g, b); 323 } 324 dstP = (uint16_t*)((char*)dstP + dstRB); 325 } 326 } 327 328 template<bool APPLY_PREBLEND> 329 static void pack4xHToLCD32(const SkBitmap& src, const SkMask& dst, 330 const SkMaskGamma::PreBlend& maskPreBlend) { 331 SkASSERT(kAlpha_8_SkColorType == src.colorType()); 332 SkASSERT(SkMask::kLCD32_Format == dst.fFormat); 333 334 const int width = dst.fBounds.width(); 335 const int height = dst.fBounds.height(); 336 SkPMColor* dstP = (SkPMColor*)dst.fImage; 337 size_t dstRB = dst.fRowBytes; 338 339 for (int y = 0; y < height; ++y) { 340 const uint8_t* srcP = src.getAddr8(0, y); 341 342 // TODO: need to use fir filter here as well. 343 for (int x = 0; x < width; ++x) { 344 U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(*srcP++, maskPreBlend.fR); 345 U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(*srcP++, maskPreBlend.fG); 346 U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(*srcP++, maskPreBlend.fB); 347 dstP[x] = SkPackARGB32(0xFF, r, g, b); 348 } 349 dstP = (SkPMColor*)((char*)dstP + dstRB); 350 } 351 } 352 353 static inline int convert_8_to_1(unsigned byte) { 354 SkASSERT(byte <= 0xFF); 355 return byte >> 7; 356 } 357 358 static uint8_t pack_8_to_1(const uint8_t alpha[8]) { 359 unsigned bits = 0; 360 for (int i = 0; i < 8; ++i) { 361 bits <<= 1; 362 bits |= convert_8_to_1(alpha[i]); 363 } 364 return SkToU8(bits); 365 } 366 367 static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) { 368 const int height = mask.fBounds.height(); 369 const int width = mask.fBounds.width(); 370 const int octs = width >> 3; 371 const int leftOverBits = width & 7; 372 373 uint8_t* dst = mask.fImage; 374 const int dstPad = mask.fRowBytes - SkAlign8(width)/8; 375 SkASSERT(dstPad >= 0); 376 377 SkASSERT(width >= 0); 378 SkASSERT(srcRB >= (size_t)width); 379 const size_t srcPad = srcRB - width; 380 381 for (int y = 0; y < height; ++y) { 382 for (int i = 0; i < octs; ++i) { 383 *dst++ = pack_8_to_1(src); 384 src += 8; 385 } 386 if (leftOverBits > 0) { 387 unsigned bits = 0; 388 int shift = 7; 389 for (int i = 0; i < leftOverBits; ++i, --shift) { 390 bits |= convert_8_to_1(*src++) << shift; 391 } 392 *dst++ = bits; 393 } 394 src += srcPad; 395 dst += dstPad; 396 } 397 } 398 399 static void generateMask(const SkMask& mask, const SkPath& path, 400 const SkMaskGamma::PreBlend& maskPreBlend) { 401 SkPaint paint; 402 403 int srcW = mask.fBounds.width(); 404 int srcH = mask.fBounds.height(); 405 int dstW = srcW; 406 int dstH = srcH; 407 int dstRB = mask.fRowBytes; 408 409 SkMatrix matrix; 410 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), 411 -SkIntToScalar(mask.fBounds.fTop)); 412 413 paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat); 414 switch (mask.fFormat) { 415 case SkMask::kBW_Format: 416 dstRB = 0; // signals we need a copy 417 break; 418 case SkMask::kA8_Format: 419 break; 420 case SkMask::kLCD16_Format: 421 case SkMask::kLCD32_Format: 422 // TODO: trigger off LCD orientation 423 dstW = 4*dstW - 8; 424 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1), 425 -SkIntToScalar(mask.fBounds.fTop)); 426 matrix.postScale(SkIntToScalar(4), SK_Scalar1); 427 dstRB = 0; // signals we need a copy 428 break; 429 default: 430 SkDEBUGFAIL("unexpected mask format"); 431 } 432 433 SkRasterClip clip; 434 clip.setRect(SkIRect::MakeWH(dstW, dstH)); 435 436 const SkImageInfo info = SkImageInfo::MakeA8(dstW, dstH); 437 SkBitmap bm; 438 439 if (0 == dstRB) { 440 if (!bm.tryAllocPixels(info)) { 441 // can't allocate offscreen, so empty the mask and return 442 sk_bzero(mask.fImage, mask.computeImageSize()); 443 return; 444 } 445 } else { 446 bm.installPixels(info, mask.fImage, dstRB); 447 } 448 sk_bzero(bm.getPixels(), bm.getSafeSize()); 449 450 SkDraw draw; 451 draw.fRC = &clip; 452 draw.fClip = &clip.bwRgn(); 453 draw.fMatrix = &matrix; 454 draw.fBitmap = &bm; 455 draw.drawPath(path, paint); 456 457 switch (mask.fFormat) { 458 case SkMask::kBW_Format: 459 packA8ToA1(mask, bm.getAddr8(0, 0), bm.rowBytes()); 460 break; 461 case SkMask::kA8_Format: 462 if (maskPreBlend.isApplicable()) { 463 applyLUTToA8Mask(mask, maskPreBlend.fG); 464 } 465 break; 466 case SkMask::kLCD16_Format: 467 if (maskPreBlend.isApplicable()) { 468 pack4xHToLCD16<true>(bm, mask, maskPreBlend); 469 } else { 470 pack4xHToLCD16<false>(bm, mask, maskPreBlend); 471 } 472 break; 473 case SkMask::kLCD32_Format: 474 if (maskPreBlend.isApplicable()) { 475 pack4xHToLCD32<true>(bm, mask, maskPreBlend); 476 } else { 477 pack4xHToLCD32<false>(bm, mask, maskPreBlend); 478 } 479 break; 480 default: 481 break; 482 } 483 } 484 485 static void extract_alpha(const SkMask& dst, 486 const SkPMColor* srcRow, size_t srcRB) { 487 int width = dst.fBounds.width(); 488 int height = dst.fBounds.height(); 489 int dstRB = dst.fRowBytes; 490 uint8_t* dstRow = dst.fImage; 491 492 for (int y = 0; y < height; ++y) { 493 for (int x = 0; x < width; ++x) { 494 dstRow[x] = SkGetPackedA32(srcRow[x]); 495 } 496 // zero any padding on each row 497 for (int x = width; x < dstRB; ++x) { 498 dstRow[x] = 0; 499 } 500 dstRow += dstRB; 501 srcRow = (const SkPMColor*)((const char*)srcRow + srcRB); 502 } 503 } 504 505 void SkScalerContext::getImage(const SkGlyph& origGlyph) { 506 const SkGlyph* glyph = &origGlyph; 507 SkGlyph tmpGlyph; 508 509 // in case we need to call generateImage on a mask-format that is different 510 // (i.e. larger) than what our caller allocated by looking at origGlyph. 511 SkAutoMalloc tmpGlyphImageStorage; 512 513 // If we are going to draw-from-path, then we cannot generate color, since 514 // the path only makes a mask. This case should have been caught up in 515 // generateMetrics(). 516 SkASSERT(!fGenerateImageFromPath || 517 SkMask::kARGB32_Format != origGlyph.fMaskFormat); 518 519 if (fMaskFilter) { // restore the prefilter bounds 520 tmpGlyph.init(origGlyph.fID); 521 522 // need the original bounds, sans our maskfilter 523 SkMaskFilter* mf = fMaskFilter; 524 fMaskFilter = NULL; // temp disable 525 this->getMetrics(&tmpGlyph); 526 fMaskFilter = mf; // restore 527 528 // we need the prefilter bounds to be <= filter bounds 529 SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth); 530 SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight); 531 532 if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) { 533 tmpGlyph.fImage = origGlyph.fImage; 534 } else { 535 tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize()); 536 tmpGlyph.fImage = tmpGlyphImageStorage.get(); 537 } 538 glyph = &tmpGlyph; 539 } 540 541 if (fGenerateImageFromPath) { 542 SkPath devPath, fillPath; 543 SkMatrix fillToDevMatrix; 544 SkMask mask; 545 546 this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); 547 glyph->toMask(&mask); 548 549 if (fRasterizer) { 550 mask.fFormat = SkMask::kA8_Format; 551 sk_bzero(glyph->fImage, mask.computeImageSize()); 552 553 if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, 554 fMaskFilter, &mask, 555 SkMask::kJustRenderImage_CreateMode)) { 556 return; 557 } 558 if (fPreBlend.isApplicable()) { 559 applyLUTToA8Mask(mask, fPreBlend.fG); 560 } 561 } else { 562 SkASSERT(SkMask::kARGB32_Format != mask.fFormat); 563 generateMask(mask, devPath, fPreBlend); 564 } 565 } else { 566 generateImage(*glyph); 567 } 568 569 if (fMaskFilter) { 570 SkMask srcM, dstM; 571 SkMatrix matrix; 572 573 // the src glyph image shouldn't be 3D 574 SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat); 575 576 SkAutoSMalloc<32*32> a8storage; 577 glyph->toMask(&srcM); 578 if (SkMask::kARGB32_Format == srcM.fFormat) { 579 // now we need to extract the alpha-channel from the glyph's image 580 // and copy it into a temp buffer, and then point srcM at that temp. 581 srcM.fFormat = SkMask::kA8_Format; 582 srcM.fRowBytes = SkAlign4(srcM.fBounds.width()); 583 size_t size = srcM.computeImageSize(); 584 a8storage.reset(size); 585 srcM.fImage = (uint8_t*)a8storage.get(); 586 extract_alpha(srcM, 587 (const SkPMColor*)glyph->fImage, glyph->rowBytes()); 588 } 589 590 fRec.getMatrixFrom2x2(&matrix); 591 592 if (fMaskFilter->filterMask(&dstM, srcM, matrix, NULL)) { 593 int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width()); 594 int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height()); 595 int dstRB = origGlyph.rowBytes(); 596 int srcRB = dstM.fRowBytes; 597 598 const uint8_t* src = (const uint8_t*)dstM.fImage; 599 uint8_t* dst = (uint8_t*)origGlyph.fImage; 600 601 if (SkMask::k3D_Format == dstM.fFormat) { 602 // we have to copy 3 times as much 603 height *= 3; 604 } 605 606 // clean out our glyph, since it may be larger than dstM 607 //sk_bzero(dst, height * dstRB); 608 609 while (--height >= 0) { 610 memcpy(dst, src, width); 611 src += srcRB; 612 dst += dstRB; 613 } 614 SkMask::FreeImage(dstM.fImage); 615 616 if (fPreBlendForFilter.isApplicable()) { 617 applyLUTToA8Mask(srcM, fPreBlendForFilter.fG); 618 } 619 } 620 } 621 } 622 623 void SkScalerContext::getPath(const SkGlyph& glyph, SkPath* path) { 624 this->internalGetPath(glyph, NULL, path, NULL); 625 } 626 627 void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) { 628 this->generateFontMetrics(fm); 629 } 630 631 SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) { 632 return 0; 633 } 634 635 /////////////////////////////////////////////////////////////////////////////// 636 637 void SkScalerContext::internalGetPath(const SkGlyph& glyph, SkPath* fillPath, 638 SkPath* devPath, SkMatrix* fillToDevMatrix) { 639 SkPath path; 640 generatePath(glyph, &path); 641 642 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { 643 SkFixed dx = glyph.getSubXFixed(); 644 SkFixed dy = glyph.getSubYFixed(); 645 if (dx | dy) { 646 path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy)); 647 } 648 } 649 650 if (fRec.fFrameWidth > 0 || fPathEffect != NULL) { 651 // need the path in user-space, with only the point-size applied 652 // so that our stroking and effects will operate the same way they 653 // would if the user had extracted the path themself, and then 654 // called drawPath 655 SkPath localPath; 656 SkMatrix matrix, inverse; 657 658 fRec.getMatrixFrom2x2(&matrix); 659 if (!matrix.invert(&inverse)) { 660 // assume fillPath and devPath are already empty. 661 return; 662 } 663 path.transform(inverse, &localPath); 664 // now localPath is only affected by the paint settings, and not the canvas matrix 665 666 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); 667 668 if (fRec.fFrameWidth > 0) { 669 rec.setStrokeStyle(fRec.fFrameWidth, 670 SkToBool(fRec.fFlags & kFrameAndFill_Flag)); 671 // glyphs are always closed contours, so cap type is ignored, 672 // so we just pass something. 673 rec.setStrokeParams(SkPaint::kButt_Cap, 674 (SkPaint::Join)fRec.fStrokeJoin, 675 fRec.fMiterLimit); 676 } 677 678 if (fPathEffect) { 679 SkPath effectPath; 680 if (fPathEffect->filterPath(&effectPath, localPath, &rec, NULL)) { 681 localPath.swap(effectPath); 682 } 683 } 684 685 if (rec.needToApply()) { 686 SkPath strokePath; 687 if (rec.applyToPath(&strokePath, localPath)) { 688 localPath.swap(strokePath); 689 } 690 } 691 692 // now return stuff to the caller 693 if (fillToDevMatrix) { 694 *fillToDevMatrix = matrix; 695 } 696 if (devPath) { 697 localPath.transform(matrix, devPath); 698 } 699 if (fillPath) { 700 fillPath->swap(localPath); 701 } 702 } else { // nothing tricky to do 703 if (fillToDevMatrix) { 704 fillToDevMatrix->reset(); 705 } 706 if (devPath) { 707 if (fillPath == NULL) { 708 devPath->swap(path); 709 } else { 710 *devPath = path; 711 } 712 } 713 714 if (fillPath) { 715 fillPath->swap(path); 716 } 717 } 718 719 if (devPath) { 720 devPath->updateBoundsCache(); 721 } 722 if (fillPath) { 723 fillPath->updateBoundsCache(); 724 } 725 } 726 727 728 void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const { 729 dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0, 730 fPost2x2[1][0], fPost2x2[1][1], 0, 731 0, 0, SkScalarToPersp(SK_Scalar1)); 732 } 733 734 void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const { 735 SkPaint::SetTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX); 736 } 737 738 void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const { 739 this->getLocalMatrix(m); 740 741 // now concat the device matrix 742 SkMatrix deviceMatrix; 743 this->getMatrixFrom2x2(&deviceMatrix); 744 m->postConcat(deviceMatrix); 745 } 746 747 SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix) { 748 SkASSERT(!matrix.hasPerspective()); 749 750 if (0 == matrix[SkMatrix::kMSkewY]) { 751 return kX_SkAxisAlignment; 752 } 753 if (0 == matrix[SkMatrix::kMScaleX]) { 754 return kY_SkAxisAlignment; 755 } 756 return kNone_SkAxisAlignment; 757 } 758 759 /////////////////////////////////////////////////////////////////////////////// 760 761 #include "SkFontHost.h" 762 763 class SkScalerContext_Empty : public SkScalerContext { 764 public: 765 SkScalerContext_Empty(SkTypeface* face, const SkDescriptor* desc) 766 : SkScalerContext(face, desc) {} 767 768 protected: 769 virtual unsigned generateGlyphCount() SK_OVERRIDE { 770 return 0; 771 } 772 virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE { 773 return 0; 774 } 775 virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE { 776 glyph->zeroMetrics(); 777 } 778 virtual void generateMetrics(SkGlyph* glyph) SK_OVERRIDE { 779 glyph->zeroMetrics(); 780 } 781 virtual void generateImage(const SkGlyph& glyph) SK_OVERRIDE {} 782 virtual void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE {} 783 virtual void generateFontMetrics(SkPaint::FontMetrics* metrics) SK_OVERRIDE { 784 if (metrics) { 785 sk_bzero(metrics, sizeof(*metrics)); 786 } 787 } 788 }; 789 790 extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc); 791 792 SkScalerContext* SkTypeface::createScalerContext(const SkDescriptor* desc, 793 bool allowFailure) const { 794 SkScalerContext* c = this->onCreateScalerContext(desc); 795 796 if (!c && !allowFailure) { 797 c = SkNEW_ARGS(SkScalerContext_Empty, 798 (const_cast<SkTypeface*>(this), desc)); 799 } 800 return c; 801 } 802