| 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 "include/core/SkPaint.h" |
| 9 | #include "src/core/SkScalerContext.h" |
| 10 | |
| 11 | #include "include/core/SkFontMetrics.h" |
| 12 | #include "include/core/SkMaskFilter.h" |
| 13 | #include "include/core/SkPathEffect.h" |
| 14 | #include "include/core/SkStrokeRec.h" |
| 15 | #include "include/private/SkColorData.h" |
| 16 | #include "include/private/SkTo.h" |
| 17 | #include "src/core/SkAutoMalloc.h" |
| 18 | #include "src/core/SkAutoPixmapStorage.h" |
| 19 | #include "src/core/SkDescriptor.h" |
| 20 | #include "src/core/SkDraw.h" |
| 21 | #include "src/core/SkFontPriv.h" |
| 22 | #include "src/core/SkGlyph.h" |
| 23 | #include "src/core/SkMaskGamma.h" |
| 24 | #include "src/core/SkMatrixProvider.h" |
| 25 | #include "src/core/SkPaintPriv.h" |
| 26 | #include "src/core/SkPathPriv.h" |
| 27 | #include "src/core/SkRasterClip.h" |
| 28 | #include "src/core/SkReadBuffer.h" |
| 29 | #include "src/core/SkRectPriv.h" |
| 30 | #include "src/core/SkStroke.h" |
| 31 | #include "src/core/SkSurfacePriv.h" |
| 32 | #include "src/core/SkTextFormatParams.h" |
| 33 | #include "src/core/SkWriteBuffer.h" |
| 34 | #include "src/utils/SkMatrix22.h" |
| 35 | #include <new> |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
| 39 | #ifdef SK_DEBUG |
| 40 | #define DUMP_RECx |
| 41 | #endif |
| 42 | |
| 43 | SkScalerContextRec SkScalerContext::PreprocessRec(const SkTypeface& typeface, |
| 44 | const SkScalerContextEffects& effects, |
| 45 | const SkDescriptor& desc) { |
| 46 | SkScalerContextRec rec = |
| 47 | *static_cast<const SkScalerContextRec*>(desc.findEntry(kRec_SkDescriptorTag, nullptr)); |
| 48 | |
| 49 | // Allow the typeface to adjust the rec. |
| 50 | typeface.onFilterRec(&rec); |
| 51 | |
| 52 | if (effects.fMaskFilter) { |
| 53 | // Pre-blend is not currently applied to filtered text. |
| 54 | // The primary filter is blur, for which contrast makes no sense, |
| 55 | // and for which the destination guess error is more visible. |
| 56 | // Also, all existing users of blur have calibrated for linear. |
| 57 | rec.ignorePreBlend(); |
| 58 | } |
| 59 | |
| 60 | SkColor lumColor = rec.getLuminanceColor(); |
| 61 | |
| 62 | if (rec.fMaskFormat == SkMask::kA8_Format) { |
| 63 | U8CPU lum = SkComputeLuminance(SkColorGetR(lumColor), |
| 64 | SkColorGetG(lumColor), |
| 65 | SkColorGetB(lumColor)); |
| 66 | lumColor = SkColorSetRGB(lum, lum, lum); |
| 67 | } |
| 68 | |
| 69 | // TODO: remove CanonicalColor when we to fix up Chrome layout tests. |
| 70 | rec.setLuminanceColor(lumColor); |
| 71 | |
| 72 | return rec; |
| 73 | } |
| 74 | |
| 75 | SkScalerContext::SkScalerContext(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects, |
| 76 | const SkDescriptor* desc) |
| 77 | : fRec(PreprocessRec(*typeface, effects, *desc)) |
| 78 | , fTypeface(std::move(typeface)) |
| 79 | , fPathEffect(sk_ref_sp(effects.fPathEffect)) |
| 80 | , fMaskFilter(sk_ref_sp(effects.fMaskFilter)) |
| 81 | // Initialize based on our settings. Subclasses can also force this. |
| 82 | , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != nullptr) |
| 83 | |
| 84 | , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec)) |
| 85 | { |
| 86 | #ifdef DUMP_REC |
| 87 | SkDebugf("SkScalerContext checksum %x count %d length %d\n" , |
| 88 | desc->getChecksum(), desc->getCount(), desc->getLength()); |
| 89 | SkDebugf("%s" , fRec.dump().c_str()); |
| 90 | SkDebugf(" effects %x\n" , desc->findEntry(kEffects_SkDescriptorTag, nullptr)); |
| 91 | #endif |
| 92 | } |
| 93 | |
| 94 | SkScalerContext::~SkScalerContext() {} |
| 95 | |
| 96 | /** |
| 97 | * In order to call cachedDeviceLuminance, cachedPaintLuminance, or |
| 98 | * cachedMaskGamma the caller must hold the mask_gamma_cache_mutex and continue |
| 99 | * to hold it until the returned pointer is refed or forgotten. |
| 100 | */ |
| 101 | static SkMutex& mask_gamma_cache_mutex() { |
| 102 | static SkMutex& mutex = *(new SkMutex); |
| 103 | return mutex; |
| 104 | } |
| 105 | |
| 106 | static SkMaskGamma* gLinearMaskGamma = nullptr; |
| 107 | static SkMaskGamma* gMaskGamma = nullptr; |
| 108 | static SkScalar gContrast = SK_ScalarMin; |
| 109 | static SkScalar gPaintGamma = SK_ScalarMin; |
| 110 | static SkScalar gDeviceGamma = SK_ScalarMin; |
| 111 | |
| 112 | /** |
| 113 | * The caller must hold the mask_gamma_cache_mutex() and continue to hold it until |
| 114 | * the returned SkMaskGamma pointer is refed or forgotten. |
| 115 | */ |
| 116 | static const SkMaskGamma& cached_mask_gamma(SkScalar contrast, SkScalar paintGamma, |
| 117 | SkScalar deviceGamma) { |
| 118 | mask_gamma_cache_mutex().assertHeld(); |
| 119 | if (0 == contrast && SK_Scalar1 == paintGamma && SK_Scalar1 == deviceGamma) { |
| 120 | if (nullptr == gLinearMaskGamma) { |
| 121 | gLinearMaskGamma = new SkMaskGamma; |
| 122 | } |
| 123 | return *gLinearMaskGamma; |
| 124 | } |
| 125 | if (gContrast != contrast || gPaintGamma != paintGamma || gDeviceGamma != deviceGamma) { |
| 126 | SkSafeUnref(gMaskGamma); |
| 127 | gMaskGamma = new SkMaskGamma(contrast, paintGamma, deviceGamma); |
| 128 | gContrast = contrast; |
| 129 | gPaintGamma = paintGamma; |
| 130 | gDeviceGamma = deviceGamma; |
| 131 | } |
| 132 | return *gMaskGamma; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Expands fDeviceGamma, fPaintGamma, fContrast, and fLumBits into a mask pre-blend. |
| 137 | */ |
| 138 | SkMaskGamma::PreBlend SkScalerContext::GetMaskPreBlend(const SkScalerContextRec& rec) { |
| 139 | SkAutoMutexExclusive ama(mask_gamma_cache_mutex()); |
| 140 | |
| 141 | const SkMaskGamma& maskGamma = cached_mask_gamma(rec.getContrast(), |
| 142 | rec.getPaintGamma(), |
| 143 | rec.getDeviceGamma()); |
| 144 | |
| 145 | // TODO: remove CanonicalColor when we to fix up Chrome layout tests. |
| 146 | return maskGamma.preBlend(rec.getLuminanceColor()); |
| 147 | } |
| 148 | |
| 149 | size_t SkScalerContext::GetGammaLUTSize(SkScalar contrast, SkScalar paintGamma, |
| 150 | SkScalar deviceGamma, int* width, int* height) { |
| 151 | SkAutoMutexExclusive ama(mask_gamma_cache_mutex()); |
| 152 | const SkMaskGamma& maskGamma = cached_mask_gamma(contrast, |
| 153 | paintGamma, |
| 154 | deviceGamma); |
| 155 | |
| 156 | maskGamma.getGammaTableDimensions(width, height); |
| 157 | size_t size = (*width)*(*height)*sizeof(uint8_t); |
| 158 | |
| 159 | return size; |
| 160 | } |
| 161 | |
| 162 | bool SkScalerContext::GetGammaLUTData(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma, |
| 163 | uint8_t* data) { |
| 164 | SkAutoMutexExclusive ama(mask_gamma_cache_mutex()); |
| 165 | const SkMaskGamma& maskGamma = cached_mask_gamma(contrast, |
| 166 | paintGamma, |
| 167 | deviceGamma); |
| 168 | const uint8_t* gammaTables = maskGamma.getGammaTables(); |
| 169 | if (!gammaTables) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | int width, height; |
| 174 | maskGamma.getGammaTableDimensions(&width, &height); |
| 175 | size_t size = width*height * sizeof(uint8_t); |
| 176 | memcpy(data, gammaTables, size); |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | void SkScalerContext::getAdvance(SkGlyph* glyph) { |
| 181 | if (generateAdvance(glyph)) { |
| 182 | glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE; |
| 183 | } else { |
| 184 | this->getMetrics(glyph); |
| 185 | SkASSERT(glyph->fMaskFormat != MASK_FORMAT_UNKNOWN); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void SkScalerContext::getMetrics(SkGlyph* glyph) { |
| 190 | bool generatingImageFromPath = fGenerateImageFromPath; |
| 191 | if (!generatingImageFromPath) { |
| 192 | generateMetrics(glyph); |
| 193 | SkASSERT(glyph->fMaskFormat != MASK_FORMAT_UNKNOWN); |
| 194 | } else { |
| 195 | SkPath devPath; |
| 196 | generatingImageFromPath = this->internalGetPath(glyph->getPackedID(), &devPath); |
| 197 | if (!generatingImageFromPath) { |
| 198 | generateMetrics(glyph); |
| 199 | SkASSERT(glyph->fMaskFormat != MASK_FORMAT_UNKNOWN); |
| 200 | } else { |
| 201 | uint8_t originMaskFormat = glyph->fMaskFormat; |
| 202 | if (!generateAdvance(glyph)) { |
| 203 | generateMetrics(glyph); |
| 204 | } |
| 205 | |
| 206 | if (originMaskFormat != MASK_FORMAT_UNKNOWN) { |
| 207 | glyph->fMaskFormat = originMaskFormat; |
| 208 | } else { |
| 209 | glyph->fMaskFormat = fRec.fMaskFormat; |
| 210 | } |
| 211 | |
| 212 | // If we are going to create the mask, then we cannot keep the color |
| 213 | if (SkMask::kARGB32_Format == glyph->fMaskFormat) { |
| 214 | glyph->fMaskFormat = SkMask::kA8_Format; |
| 215 | } |
| 216 | |
| 217 | const SkIRect ir = devPath.getBounds().roundOut(); |
| 218 | if (ir.isEmpty() || !SkRectPriv::Is16Bit(ir)) { |
| 219 | goto SK_ERROR; |
| 220 | } |
| 221 | glyph->fLeft = ir.fLeft; |
| 222 | glyph->fTop = ir.fTop; |
| 223 | glyph->fWidth = SkToU16(ir.width()); |
| 224 | glyph->fHeight = SkToU16(ir.height()); |
| 225 | |
| 226 | if (glyph->fWidth > 0) { |
| 227 | switch (glyph->fMaskFormat) { |
| 228 | case SkMask::kLCD16_Format: |
| 229 | if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) { |
| 230 | glyph->fHeight += 2; |
| 231 | glyph->fTop -= 1; |
| 232 | } else { |
| 233 | glyph->fWidth += 2; |
| 234 | glyph->fLeft -= 1; |
| 235 | } |
| 236 | break; |
| 237 | default: |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // if either dimension is empty, zap the image bounds of the glyph |
| 245 | if (0 == glyph->fWidth || 0 == glyph->fHeight) { |
| 246 | glyph->fWidth = 0; |
| 247 | glyph->fHeight = 0; |
| 248 | glyph->fTop = 0; |
| 249 | glyph->fLeft = 0; |
| 250 | glyph->fMaskFormat = 0; |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | if (fMaskFilter) { |
| 255 | SkMask src = glyph->mask(), |
| 256 | dst; |
| 257 | SkMatrix matrix; |
| 258 | |
| 259 | fRec.getMatrixFrom2x2(&matrix); |
| 260 | |
| 261 | src.fImage = nullptr; // only want the bounds from the filter |
| 262 | if (as_MFB(fMaskFilter)->filterMask(&dst, src, matrix, nullptr)) { |
| 263 | if (dst.fBounds.isEmpty() || !SkRectPriv::Is16Bit(dst.fBounds)) { |
| 264 | goto SK_ERROR; |
| 265 | } |
| 266 | SkASSERT(dst.fImage == nullptr); |
| 267 | glyph->fLeft = dst.fBounds.fLeft; |
| 268 | glyph->fTop = dst.fBounds.fTop; |
| 269 | glyph->fWidth = SkToU16(dst.fBounds.width()); |
| 270 | glyph->fHeight = SkToU16(dst.fBounds.height()); |
| 271 | glyph->fMaskFormat = dst.fFormat; |
| 272 | } |
| 273 | } |
| 274 | return; |
| 275 | |
| 276 | SK_ERROR: |
| 277 | // draw nothing 'cause we failed |
| 278 | glyph->fLeft = 0; |
| 279 | glyph->fTop = 0; |
| 280 | glyph->fWidth = 0; |
| 281 | glyph->fHeight = 0; |
| 282 | // put a valid value here, in case it was earlier set to |
| 283 | // MASK_FORMAT_JUST_ADVANCE |
| 284 | glyph->fMaskFormat = fRec.fMaskFormat; |
| 285 | } |
| 286 | |
| 287 | #define SK_SHOW_TEXT_BLIT_COVERAGE 0 |
| 288 | |
| 289 | static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) { |
| 290 | uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage; |
| 291 | unsigned rowBytes = mask.fRowBytes; |
| 292 | |
| 293 | for (int y = mask.fBounds.height() - 1; y >= 0; --y) { |
| 294 | for (int x = mask.fBounds.width() - 1; x >= 0; --x) { |
| 295 | dst[x] = lut[dst[x]]; |
| 296 | } |
| 297 | dst += rowBytes; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static void pack4xHToLCD16(const SkPixmap& src, const SkMask& dst, |
| 302 | const SkMaskGamma::PreBlend& maskPreBlend, |
| 303 | const bool doBGR, const bool doVert) { |
| 304 | #define SAMPLES_PER_PIXEL 4 |
| 305 | #define LCD_PER_PIXEL 3 |
| 306 | SkASSERT(kAlpha_8_SkColorType == src.colorType()); |
| 307 | SkASSERT(SkMask::kLCD16_Format == dst.fFormat); |
| 308 | |
| 309 | // doVert in this function means swap x and y when writing to dst. |
| 310 | if (doVert) { |
| 311 | SkASSERT(src.width() == (dst.fBounds.height() - 2) * 4); |
| 312 | SkASSERT(src.height() == dst.fBounds.width()); |
| 313 | } else { |
| 314 | SkASSERT(src.width() == (dst.fBounds.width() - 2) * 4); |
| 315 | SkASSERT(src.height() == dst.fBounds.height()); |
| 316 | } |
| 317 | |
| 318 | const int sample_width = src.width(); |
| 319 | const int height = src.height(); |
| 320 | |
| 321 | uint16_t* dstImage = (uint16_t*)dst.fImage; |
| 322 | size_t dstRB = dst.fRowBytes; |
| 323 | // An N tap FIR is defined by |
| 324 | // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N] |
| 325 | // or |
| 326 | // out[n] = sum(i, 0, N, coeff[i]*x[n-i]) |
| 327 | |
| 328 | // The strategy is to use one FIR (different coefficients) for each of r, g, and b. |
| 329 | // This means using every 4th FIR output value of each FIR and discarding the rest. |
| 330 | // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'. |
| 331 | // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.) |
| 332 | |
| 333 | // These are in some fixed point repesentation. |
| 334 | // Adding up to more than one simulates ink spread. |
| 335 | // For implementation reasons, these should never add up to more than two. |
| 336 | |
| 337 | // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast'). |
| 338 | // Calculated using tools/generate_fir_coeff.py |
| 339 | // With this one almost no fringing is ever seen, but it is imperceptibly blurry. |
| 340 | // The lcd smoothed text is almost imperceptibly different from gray, |
| 341 | // but is still sharper on small stems and small rounded corners than gray. |
| 342 | // This also seems to be about as wide as one can get and only have a three pixel kernel. |
| 343 | // TODO: calculate these at runtime so parameters can be adjusted (esp contrast). |
| 344 | static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = { |
| 345 | //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted. |
| 346 | { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, }, |
| 347 | //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric |
| 348 | { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, }, |
| 349 | //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted. |
| 350 | { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, }, |
| 351 | }; |
| 352 | |
| 353 | for (int y = 0; y < height; ++y) { |
| 354 | uint16_t* dstP; |
| 355 | size_t dstPDelta; |
| 356 | if (doVert) { |
| 357 | dstP = dstImage + y; |
| 358 | dstPDelta = dstRB; |
| 359 | } else { |
| 360 | dstP = SkTAddOffset<uint16_t>(dstImage, dstRB * y); |
| 361 | dstPDelta = sizeof(uint16_t); |
| 362 | } |
| 363 | |
| 364 | const uint8_t* srcP = src.addr8(0, y); |
| 365 | |
| 366 | // TODO: this fir filter implementation is straight forward, but slow. |
| 367 | // It should be possible to make it much faster. |
| 368 | for (int sample_x = -4; sample_x < sample_width + 4; sample_x += 4) { |
| 369 | int fir[LCD_PER_PIXEL] = { 0 }; |
| 370 | for (int sample_index = std::max(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4) |
| 371 | ; sample_index < std::min(sample_x + 8, sample_width) |
| 372 | ; ++sample_index, ++coeff_index) |
| 373 | { |
| 374 | int sample_value = srcP[sample_index]; |
| 375 | for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { |
| 376 | fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value; |
| 377 | } |
| 378 | } |
| 379 | for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { |
| 380 | fir[subpxl_index] /= 0x100; |
| 381 | fir[subpxl_index] = std::min(fir[subpxl_index], 255); |
| 382 | } |
| 383 | |
| 384 | U8CPU r, g, b; |
| 385 | if (doBGR) { |
| 386 | r = fir[2]; |
| 387 | g = fir[1]; |
| 388 | b = fir[0]; |
| 389 | } else { |
| 390 | r = fir[0]; |
| 391 | g = fir[1]; |
| 392 | b = fir[2]; |
| 393 | } |
| 394 | if (maskPreBlend.isApplicable()) { |
| 395 | r = maskPreBlend.fR[r]; |
| 396 | g = maskPreBlend.fG[g]; |
| 397 | b = maskPreBlend.fB[b]; |
| 398 | } |
| 399 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
| 400 | r = std::max(r, 10); g = std::max(g, 10); b = std::max(b, 10); |
| 401 | #endif |
| 402 | *dstP = SkPack888ToRGB16(r, g, b); |
| 403 | dstP = SkTAddOffset<uint16_t>(dstP, dstPDelta); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | static inline int convert_8_to_1(unsigned byte) { |
| 409 | SkASSERT(byte <= 0xFF); |
| 410 | return byte >> 7; |
| 411 | } |
| 412 | |
| 413 | static uint8_t pack_8_to_1(const uint8_t alpha[8]) { |
| 414 | unsigned bits = 0; |
| 415 | for (int i = 0; i < 8; ++i) { |
| 416 | bits <<= 1; |
| 417 | bits |= convert_8_to_1(alpha[i]); |
| 418 | } |
| 419 | return SkToU8(bits); |
| 420 | } |
| 421 | |
| 422 | static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) { |
| 423 | const int height = mask.fBounds.height(); |
| 424 | const int width = mask.fBounds.width(); |
| 425 | const int octs = width >> 3; |
| 426 | const int leftOverBits = width & 7; |
| 427 | |
| 428 | uint8_t* dst = mask.fImage; |
| 429 | const int dstPad = mask.fRowBytes - SkAlign8(width)/8; |
| 430 | SkASSERT(dstPad >= 0); |
| 431 | |
| 432 | SkASSERT(width >= 0); |
| 433 | SkASSERT(srcRB >= (size_t)width); |
| 434 | const size_t srcPad = srcRB - width; |
| 435 | |
| 436 | for (int y = 0; y < height; ++y) { |
| 437 | for (int i = 0; i < octs; ++i) { |
| 438 | *dst++ = pack_8_to_1(src); |
| 439 | src += 8; |
| 440 | } |
| 441 | if (leftOverBits > 0) { |
| 442 | unsigned bits = 0; |
| 443 | int shift = 7; |
| 444 | for (int i = 0; i < leftOverBits; ++i, --shift) { |
| 445 | bits |= convert_8_to_1(*src++) << shift; |
| 446 | } |
| 447 | *dst++ = bits; |
| 448 | } |
| 449 | src += srcPad; |
| 450 | dst += dstPad; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static void generateMask(const SkMask& mask, const SkPath& path, |
| 455 | const SkMaskGamma::PreBlend& maskPreBlend, |
| 456 | bool doBGR, bool doVert) { |
| 457 | SkPaint paint; |
| 458 | |
| 459 | int srcW = mask.fBounds.width(); |
| 460 | int srcH = mask.fBounds.height(); |
| 461 | int dstW = srcW; |
| 462 | int dstH = srcH; |
| 463 | int dstRB = mask.fRowBytes; |
| 464 | |
| 465 | SkMatrix matrix; |
| 466 | matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), |
| 467 | -SkIntToScalar(mask.fBounds.fTop)); |
| 468 | |
| 469 | paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat); |
| 470 | switch (mask.fFormat) { |
| 471 | case SkMask::kBW_Format: |
| 472 | dstRB = 0; // signals we need a copy |
| 473 | break; |
| 474 | case SkMask::kA8_Format: |
| 475 | break; |
| 476 | case SkMask::kLCD16_Format: |
| 477 | if (doVert) { |
| 478 | dstW = 4*dstH - 8; |
| 479 | dstH = srcW; |
| 480 | matrix.setAll(0, 4, -SkIntToScalar(mask.fBounds.fTop + 1) * 4, |
| 481 | 1, 0, -SkIntToScalar(mask.fBounds.fLeft), |
| 482 | 0, 0, 1); |
| 483 | } else { |
| 484 | dstW = 4*dstW - 8; |
| 485 | matrix.setAll(4, 0, -SkIntToScalar(mask.fBounds.fLeft + 1) * 4, |
| 486 | 0, 1, -SkIntToScalar(mask.fBounds.fTop), |
| 487 | 0, 0, 1); |
| 488 | } |
| 489 | dstRB = 0; // signals we need a copy |
| 490 | break; |
| 491 | default: |
| 492 | SkDEBUGFAIL("unexpected mask format" ); |
| 493 | } |
| 494 | |
| 495 | SkRasterClip clip; |
| 496 | clip.setRect(SkIRect::MakeWH(dstW, dstH)); |
| 497 | |
| 498 | const SkImageInfo info = SkImageInfo::MakeA8(dstW, dstH); |
| 499 | SkAutoPixmapStorage dst; |
| 500 | |
| 501 | if (0 == dstRB) { |
| 502 | if (!dst.tryAlloc(info)) { |
| 503 | // can't allocate offscreen, so empty the mask and return |
| 504 | sk_bzero(mask.fImage, mask.computeImageSize()); |
| 505 | return; |
| 506 | } |
| 507 | } else { |
| 508 | dst.reset(info, mask.fImage, dstRB); |
| 509 | } |
| 510 | sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
| 511 | |
| 512 | SkDraw draw; |
| 513 | SkSimpleMatrixProvider matrixProvider(matrix); |
| 514 | draw.fDst = dst; |
| 515 | draw.fRC = &clip; |
| 516 | draw.fMatrixProvider = &matrixProvider; |
| 517 | draw.drawPath(path, paint); |
| 518 | |
| 519 | switch (mask.fFormat) { |
| 520 | case SkMask::kBW_Format: |
| 521 | packA8ToA1(mask, dst.addr8(0, 0), dst.rowBytes()); |
| 522 | break; |
| 523 | case SkMask::kA8_Format: |
| 524 | if (maskPreBlend.isApplicable()) { |
| 525 | applyLUTToA8Mask(mask, maskPreBlend.fG); |
| 526 | } |
| 527 | break; |
| 528 | case SkMask::kLCD16_Format: |
| 529 | pack4xHToLCD16(dst, mask, maskPreBlend, doBGR, doVert); |
| 530 | break; |
| 531 | default: |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void SkScalerContext::getImage(const SkGlyph& origGlyph) { |
| 537 | const SkGlyph* glyph = &origGlyph; |
| 538 | SkGlyph tmpGlyph{origGlyph.getPackedID()}; |
| 539 | |
| 540 | // in case we need to call generateImage on a mask-format that is different |
| 541 | // (i.e. larger) than what our caller allocated by looking at origGlyph. |
| 542 | SkAutoMalloc tmpGlyphImageStorage; |
| 543 | |
| 544 | if (fMaskFilter) { // restore the prefilter bounds |
| 545 | |
| 546 | // need the original bounds, sans our maskfilter |
| 547 | sk_sp<SkMaskFilter> mf = std::move(fMaskFilter); |
| 548 | this->getMetrics(&tmpGlyph); |
| 549 | fMaskFilter = std::move(mf); |
| 550 | |
| 551 | // we need the prefilter bounds to be <= filter bounds |
| 552 | SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth); |
| 553 | SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight); |
| 554 | |
| 555 | if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) { |
| 556 | tmpGlyph.fImage = origGlyph.fImage; |
| 557 | } else { |
| 558 | tmpGlyphImageStorage.reset(tmpGlyph.imageSize()); |
| 559 | tmpGlyph.fImage = tmpGlyphImageStorage.get(); |
| 560 | } |
| 561 | glyph = &tmpGlyph; |
| 562 | } |
| 563 | |
| 564 | if (!fGenerateImageFromPath) { |
| 565 | generateImage(*glyph); |
| 566 | } else { |
| 567 | SkPath devPath; |
| 568 | SkMask mask = glyph->mask(); |
| 569 | |
| 570 | if (!this->internalGetPath(glyph->getPackedID(), &devPath)) { |
| 571 | generateImage(*glyph); |
| 572 | } else { |
| 573 | SkASSERT(SkMask::kARGB32_Format != origGlyph.fMaskFormat); |
| 574 | SkASSERT(SkMask::kARGB32_Format != mask.fFormat); |
| 575 | const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag); |
| 576 | const bool doVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag); |
| 577 | generateMask(mask, devPath, fPreBlend, doBGR, doVert); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (fMaskFilter) { |
| 582 | // the src glyph image shouldn't be 3D |
| 583 | SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat); |
| 584 | |
| 585 | SkMask srcM = glyph->mask(), |
| 586 | dstM; |
| 587 | SkMatrix matrix; |
| 588 | |
| 589 | fRec.getMatrixFrom2x2(&matrix); |
| 590 | |
| 591 | if (as_MFB(fMaskFilter)->filterMask(&dstM, srcM, matrix, nullptr)) { |
| 592 | int width = std::min<int>(origGlyph.fWidth, dstM.fBounds.width()); |
| 593 | int height = std::min<int>(origGlyph.fHeight, dstM.fBounds.height()); |
| 594 | int dstRB = origGlyph.rowBytes(); |
| 595 | int srcRB = dstM.fRowBytes; |
| 596 | |
| 597 | const uint8_t* src = (const uint8_t*)dstM.fImage; |
| 598 | uint8_t* dst = (uint8_t*)origGlyph.fImage; |
| 599 | |
| 600 | if (SkMask::k3D_Format == dstM.fFormat) { |
| 601 | // we have to copy 3 times as much |
| 602 | height *= 3; |
| 603 | } |
| 604 | |
| 605 | // clean out our glyph, since it may be larger than dstM |
| 606 | //sk_bzero(dst, height * dstRB); |
| 607 | |
| 608 | while (--height >= 0) { |
| 609 | memcpy(dst, src, width); |
| 610 | src += srcRB; |
| 611 | dst += dstRB; |
| 612 | } |
| 613 | SkMask::FreeImage(dstM.fImage); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | bool SkScalerContext::getPath(SkPackedGlyphID glyphID, SkPath* path) { |
| 619 | return this->internalGetPath(glyphID, path); |
| 620 | } |
| 621 | |
| 622 | void SkScalerContext::getFontMetrics(SkFontMetrics* fm) { |
| 623 | SkASSERT(fm); |
| 624 | this->generateFontMetrics(fm); |
| 625 | } |
| 626 | |
| 627 | /////////////////////////////////////////////////////////////////////////////// |
| 628 | |
| 629 | bool SkScalerContext::internalGetPath(SkPackedGlyphID glyphID, SkPath* devPath) { |
| 630 | SkPath path; |
| 631 | if (!generatePath(glyphID.glyphID(), &path)) { |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { |
| 636 | SkFixed dx = glyphID.getSubXFixed(); |
| 637 | SkFixed dy = glyphID.getSubYFixed(); |
| 638 | if (dx | dy) { |
| 639 | path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy)); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (fRec.fFrameWidth > 0 || fPathEffect != nullptr) { |
| 644 | // need the path in user-space, with only the point-size applied |
| 645 | // so that our stroking and effects will operate the same way they |
| 646 | // would if the user had extracted the path themself, and then |
| 647 | // called drawPath |
| 648 | SkPath localPath; |
| 649 | SkMatrix matrix, inverse; |
| 650 | |
| 651 | fRec.getMatrixFrom2x2(&matrix); |
| 652 | if (!matrix.invert(&inverse)) { |
| 653 | // assume devPath is already empty. |
| 654 | return true; |
| 655 | } |
| 656 | path.transform(inverse, &localPath); |
| 657 | // now localPath is only affected by the paint settings, and not the canvas matrix |
| 658 | |
| 659 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 660 | |
| 661 | if (fRec.fFrameWidth > 0) { |
| 662 | rec.setStrokeStyle(fRec.fFrameWidth, |
| 663 | SkToBool(fRec.fFlags & kFrameAndFill_Flag)); |
| 664 | // glyphs are always closed contours, so cap type is ignored, |
| 665 | // so we just pass something. |
| 666 | rec.setStrokeParams((SkPaint::Cap)fRec.fStrokeCap, |
| 667 | (SkPaint::Join)fRec.fStrokeJoin, |
| 668 | fRec.fMiterLimit); |
| 669 | } |
| 670 | |
| 671 | if (fPathEffect) { |
| 672 | SkPath effectPath; |
| 673 | if (fPathEffect->filterPath(&effectPath, localPath, &rec, nullptr)) { |
| 674 | localPath.swap(effectPath); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (rec.needToApply()) { |
| 679 | SkPath strokePath; |
| 680 | if (rec.applyToPath(&strokePath, localPath)) { |
| 681 | localPath.swap(strokePath); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // now return stuff to the caller |
| 686 | if (devPath) { |
| 687 | localPath.transform(matrix, devPath); |
| 688 | } |
| 689 | } else { // nothing tricky to do |
| 690 | if (devPath) { |
| 691 | devPath->swap(path); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (devPath) { |
| 696 | devPath->updateBoundsCache(); |
| 697 | } |
| 698 | return true; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const { |
| 703 | dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0, |
| 704 | fPost2x2[1][0], fPost2x2[1][1], 0, |
| 705 | 0, 0, 1); |
| 706 | } |
| 707 | |
| 708 | void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const { |
| 709 | *m = SkFontPriv::MakeTextMatrix(fTextSize, fPreScaleX, fPreSkewX); |
| 710 | } |
| 711 | |
| 712 | void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const { |
| 713 | this->getLocalMatrix(m); |
| 714 | |
| 715 | // now concat the device matrix |
| 716 | SkMatrix deviceMatrix; |
| 717 | this->getMatrixFrom2x2(&deviceMatrix); |
| 718 | m->postConcat(deviceMatrix); |
| 719 | } |
| 720 | |
| 721 | bool SkScalerContextRec::computeMatrices(PreMatrixScale preMatrixScale, SkVector* s, SkMatrix* sA, |
| 722 | SkMatrix* GsA, SkMatrix* G_inv, SkMatrix* A_out) |
| 723 | { |
| 724 | // A is the 'total' matrix. |
| 725 | SkMatrix A; |
| 726 | this->getSingleMatrix(&A); |
| 727 | |
| 728 | // The caller may find the 'total' matrix useful when dealing directly with EM sizes. |
| 729 | if (A_out) { |
| 730 | *A_out = A; |
| 731 | } |
| 732 | |
| 733 | // GA is the matrix A with rotation removed. |
| 734 | SkMatrix GA; |
| 735 | bool skewedOrFlipped = A.getSkewX() || A.getSkewY() || A.getScaleX() < 0 || A.getScaleY() < 0; |
| 736 | if (skewedOrFlipped) { |
| 737 | // QR by Givens rotations. G is Q^T and GA is R. G is rotational (no reflections). |
| 738 | // h is where A maps the horizontal baseline. |
| 739 | SkPoint h = SkPoint::Make(SK_Scalar1, 0); |
| 740 | A.mapPoints(&h, 1); |
| 741 | |
| 742 | // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0). |
| 743 | SkMatrix G; |
| 744 | SkComputeGivensRotation(h, &G); |
| 745 | |
| 746 | GA = G; |
| 747 | GA.preConcat(A); |
| 748 | |
| 749 | // The 'remainingRotation' is G inverse, which is fairly simple since G is 2x2 rotational. |
| 750 | if (G_inv) { |
| 751 | G_inv->setAll( |
| 752 | G.get(SkMatrix::kMScaleX), -G.get(SkMatrix::kMSkewX), G.get(SkMatrix::kMTransX), |
| 753 | -G.get(SkMatrix::kMSkewY), G.get(SkMatrix::kMScaleY), G.get(SkMatrix::kMTransY), |
| 754 | G.get(SkMatrix::kMPersp0), G.get(SkMatrix::kMPersp1), G.get(SkMatrix::kMPersp2)); |
| 755 | } |
| 756 | } else { |
| 757 | GA = A; |
| 758 | if (G_inv) { |
| 759 | G_inv->reset(); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // If the 'total' matrix is singular, set the 'scale' to something finite and zero the matrices. |
| 764 | // All underlying ports have issues with zero text size, so use the matricies to zero. |
| 765 | // If one of the scale factors is less than 1/256 then an EM filling square will |
| 766 | // never affect any pixels. |
| 767 | // If there are any nonfinite numbers in the matrix, bail out and set the matrices to zero. |
| 768 | if (SkScalarAbs(GA.get(SkMatrix::kMScaleX)) <= SK_ScalarNearlyZero || |
| 769 | SkScalarAbs(GA.get(SkMatrix::kMScaleY)) <= SK_ScalarNearlyZero || |
| 770 | !GA.isFinite()) |
| 771 | { |
| 772 | s->fX = SK_Scalar1; |
| 773 | s->fY = SK_Scalar1; |
| 774 | sA->setScale(0, 0); |
| 775 | if (GsA) { |
| 776 | GsA->setScale(0, 0); |
| 777 | } |
| 778 | if (G_inv) { |
| 779 | G_inv->reset(); |
| 780 | } |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | // At this point, given GA, create s. |
| 785 | switch (preMatrixScale) { |
| 786 | case kFull_PreMatrixScale: |
| 787 | s->fX = SkScalarAbs(GA.get(SkMatrix::kMScaleX)); |
| 788 | s->fY = SkScalarAbs(GA.get(SkMatrix::kMScaleY)); |
| 789 | break; |
| 790 | case kVertical_PreMatrixScale: { |
| 791 | SkScalar yScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY)); |
| 792 | s->fX = yScale; |
| 793 | s->fY = yScale; |
| 794 | break; |
| 795 | } |
| 796 | case kVerticalInteger_PreMatrixScale: { |
| 797 | SkScalar realYScale = SkScalarAbs(GA.get(SkMatrix::kMScaleY)); |
| 798 | SkScalar intYScale = SkScalarRoundToScalar(realYScale); |
| 799 | if (intYScale == 0) { |
| 800 | intYScale = SK_Scalar1; |
| 801 | } |
| 802 | s->fX = intYScale; |
| 803 | s->fY = intYScale; |
| 804 | break; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // The 'remaining' matrix sA is the total matrix A without the scale. |
| 809 | if (!skewedOrFlipped && ( |
| 810 | (kFull_PreMatrixScale == preMatrixScale) || |
| 811 | (kVertical_PreMatrixScale == preMatrixScale && A.getScaleX() == A.getScaleY()))) |
| 812 | { |
| 813 | // If GA == A and kFull_PreMatrixScale, sA is identity. |
| 814 | // If GA == A and kVertical_PreMatrixScale and A.scaleX == A.scaleY, sA is identity. |
| 815 | sA->reset(); |
| 816 | } else if (!skewedOrFlipped && kVertical_PreMatrixScale == preMatrixScale) { |
| 817 | // If GA == A and kVertical_PreMatrixScale, sA.scaleY is SK_Scalar1. |
| 818 | sA->reset(); |
| 819 | sA->setScaleX(A.getScaleX() / s->fY); |
| 820 | } else { |
| 821 | // TODO: like kVertical_PreMatrixScale, kVerticalInteger_PreMatrixScale with int scales. |
| 822 | *sA = A; |
| 823 | sA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY)); |
| 824 | } |
| 825 | |
| 826 | // The 'remainingWithoutRotation' matrix GsA is the non-rotational part of A without the scale. |
| 827 | if (GsA) { |
| 828 | *GsA = GA; |
| 829 | // G is rotational so reorders with the scale. |
| 830 | GsA->preScale(SkScalarInvert(s->fX), SkScalarInvert(s->fY)); |
| 831 | } |
| 832 | |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | SkAxisAlignment SkScalerContext::computeAxisAlignmentForHText() const { |
| 837 | return fRec.computeAxisAlignmentForHText(); |
| 838 | } |
| 839 | |
| 840 | SkAxisAlignment SkScalerContextRec::computeAxisAlignmentForHText() const { |
| 841 | // Why fPost2x2 can be used here. |
| 842 | // getSingleMatrix multiplies in getLocalMatrix, which consists of |
| 843 | // * fTextSize (a scale, which has no effect) |
| 844 | // * fPreScaleX (a scale in x, which has no effect) |
| 845 | // * fPreSkewX (has no effect, but would on vertical text alignment). |
| 846 | // In other words, making the text bigger, stretching it along the |
| 847 | // horizontal axis, or fake italicizing it does not move the baseline. |
| 848 | if (!SkToBool(fFlags & SkScalerContext::kBaselineSnap_Flag)) { |
| 849 | return kNone_SkAxisAlignment; |
| 850 | } |
| 851 | |
| 852 | if (0 == fPost2x2[1][0]) { |
| 853 | // The x axis is mapped onto the x axis. |
| 854 | return kX_SkAxisAlignment; |
| 855 | } |
| 856 | if (0 == fPost2x2[0][0]) { |
| 857 | // The x axis is mapped onto the y axis. |
| 858 | return kY_SkAxisAlignment; |
| 859 | } |
| 860 | return kNone_SkAxisAlignment; |
| 861 | } |
| 862 | |
| 863 | void SkScalerContextRec::setLuminanceColor(SkColor c) { |
| 864 | fLumBits = SkMaskGamma::CanonicalColor( |
| 865 | SkColorSetRGB(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c))); |
| 866 | } |
| 867 | |
| 868 | extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc); |
| 869 | |
| 870 | std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext( |
| 871 | const SkScalerContextEffects& effects, const SkDescriptor* desc) const { |
| 872 | auto answer = std::unique_ptr<SkScalerContext>{this->onCreateScalerContext(effects, desc)}; |
| 873 | SkASSERT(answer != nullptr); |
| 874 | return answer; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * Return the scalar with only limited fractional precision. Used to consolidate matrices |
| 879 | * that vary only slightly when we create our key into the font cache, since the font scaler |
| 880 | * typically returns the same looking resuts for tiny changes in the matrix. |
| 881 | */ |
| 882 | static SkScalar sk_relax(SkScalar x) { |
| 883 | SkScalar n = SkScalarRoundToScalar(x * 1024); |
| 884 | return n / 1024.0f; |
| 885 | } |
| 886 | |
| 887 | static SkMask::Format compute_mask_format(const SkFont& font) { |
| 888 | switch (font.getEdging()) { |
| 889 | case SkFont::Edging::kAlias: |
| 890 | return SkMask::kBW_Format; |
| 891 | case SkFont::Edging::kAntiAlias: |
| 892 | return SkMask::kA8_Format; |
| 893 | case SkFont::Edging::kSubpixelAntiAlias: |
| 894 | return SkMask::kLCD16_Format; |
| 895 | } |
| 896 | SkASSERT(false); |
| 897 | return SkMask::kA8_Format; |
| 898 | } |
| 899 | |
| 900 | // Beyond this size, LCD doesn't appreciably improve quality, but it always |
| 901 | // cost more RAM and draws slower, so we set a cap. |
| 902 | #ifndef SK_MAX_SIZE_FOR_LCDTEXT |
| 903 | #define SK_MAX_SIZE_FOR_LCDTEXT 48 |
| 904 | #endif |
| 905 | |
| 906 | const SkScalar gMaxSize2ForLCDText = SK_MAX_SIZE_FOR_LCDTEXT * SK_MAX_SIZE_FOR_LCDTEXT; |
| 907 | |
| 908 | static bool too_big_for_lcd(const SkScalerContextRec& rec, bool checkPost2x2) { |
| 909 | if (checkPost2x2) { |
| 910 | SkScalar area = rec.fPost2x2[0][0] * rec.fPost2x2[1][1] - |
| 911 | rec.fPost2x2[1][0] * rec.fPost2x2[0][1]; |
| 912 | area *= rec.fTextSize * rec.fTextSize; |
| 913 | return area > gMaxSize2ForLCDText; |
| 914 | } else { |
| 915 | return rec.fTextSize > SK_MAX_SIZE_FOR_LCDTEXT; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | // The only reason this is not file static is because it needs the context of SkScalerContext to |
| 920 | // access SkPaint::computeLuminanceColor. |
| 921 | void SkScalerContext::MakeRecAndEffects(const SkFont& font, const SkPaint& paint, |
| 922 | const SkSurfaceProps& surfaceProps, |
| 923 | SkScalerContextFlags scalerContextFlags, |
| 924 | const SkMatrix& deviceMatrix, |
| 925 | SkScalerContextRec* rec, |
| 926 | SkScalerContextEffects* effects) { |
| 927 | SkASSERT(!deviceMatrix.hasPerspective()); |
| 928 | |
| 929 | sk_bzero(rec, sizeof(SkScalerContextRec)); |
| 930 | |
| 931 | SkTypeface* typeface = font.getTypefaceOrDefault(); |
| 932 | |
| 933 | rec->fFontID = typeface->uniqueID(); |
| 934 | rec->fTextSize = font.getSize(); |
| 935 | rec->fPreScaleX = font.getScaleX(); |
| 936 | rec->fPreSkewX = font.getSkewX(); |
| 937 | |
| 938 | bool checkPost2x2 = false; |
| 939 | |
| 940 | const SkMatrix::TypeMask mask = deviceMatrix.getType(); |
| 941 | if (mask & SkMatrix::kScale_Mask) { |
| 942 | rec->fPost2x2[0][0] = sk_relax(deviceMatrix.getScaleX()); |
| 943 | rec->fPost2x2[1][1] = sk_relax(deviceMatrix.getScaleY()); |
| 944 | checkPost2x2 = true; |
| 945 | } else { |
| 946 | rec->fPost2x2[0][0] = rec->fPost2x2[1][1] = SK_Scalar1; |
| 947 | } |
| 948 | if (mask & SkMatrix::kAffine_Mask) { |
| 949 | rec->fPost2x2[0][1] = sk_relax(deviceMatrix.getSkewX()); |
| 950 | rec->fPost2x2[1][0] = sk_relax(deviceMatrix.getSkewY()); |
| 951 | checkPost2x2 = true; |
| 952 | } else { |
| 953 | rec->fPost2x2[0][1] = rec->fPost2x2[1][0] = 0; |
| 954 | } |
| 955 | |
| 956 | SkPaint::Style style = paint.getStyle(); |
| 957 | SkScalar strokeWidth = paint.getStrokeWidth(); |
| 958 | |
| 959 | unsigned flags = 0; |
| 960 | |
| 961 | if (font.isEmbolden()) { |
| 962 | #ifdef SK_USE_FREETYPE_EMBOLDEN |
| 963 | flags |= SkScalerContext::kEmbolden_Flag; |
| 964 | #else |
| 965 | SkScalar fakeBoldScale = SkScalarInterpFunc(font.getSize(), |
| 966 | kStdFakeBoldInterpKeys, |
| 967 | kStdFakeBoldInterpValues, |
| 968 | kStdFakeBoldInterpLength); |
| 969 | SkScalar = font.getSize() * fakeBoldScale; |
| 970 | |
| 971 | if (style == SkPaint::kFill_Style) { |
| 972 | style = SkPaint::kStrokeAndFill_Style; |
| 973 | strokeWidth = extra; // ignore paint's strokeWidth if it was "fill" |
| 974 | } else { |
| 975 | strokeWidth += extra; |
| 976 | } |
| 977 | #endif |
| 978 | } |
| 979 | |
| 980 | if (style != SkPaint::kFill_Style && strokeWidth > 0) { |
| 981 | rec->fFrameWidth = strokeWidth; |
| 982 | rec->fMiterLimit = paint.getStrokeMiter(); |
| 983 | rec->fStrokeJoin = SkToU8(paint.getStrokeJoin()); |
| 984 | rec->fStrokeCap = SkToU8(paint.getStrokeCap()); |
| 985 | |
| 986 | if (style == SkPaint::kStrokeAndFill_Style) { |
| 987 | flags |= SkScalerContext::kFrameAndFill_Flag; |
| 988 | } |
| 989 | } else { |
| 990 | rec->fFrameWidth = 0; |
| 991 | rec->fMiterLimit = 0; |
| 992 | rec->fStrokeJoin = 0; |
| 993 | rec->fStrokeCap = 0; |
| 994 | } |
| 995 | |
| 996 | rec->fMaskFormat = SkToU8(compute_mask_format(font)); |
| 997 | |
| 998 | if (SkMask::kLCD16_Format == rec->fMaskFormat) { |
| 999 | if (too_big_for_lcd(*rec, checkPost2x2)) { |
| 1000 | rec->fMaskFormat = SkMask::kA8_Format; |
| 1001 | flags |= SkScalerContext::kGenA8FromLCD_Flag; |
| 1002 | } else { |
| 1003 | SkPixelGeometry geometry = surfaceProps.pixelGeometry(); |
| 1004 | |
| 1005 | switch (geometry) { |
| 1006 | case kUnknown_SkPixelGeometry: |
| 1007 | // eeek, can't support LCD |
| 1008 | rec->fMaskFormat = SkMask::kA8_Format; |
| 1009 | flags |= SkScalerContext::kGenA8FromLCD_Flag; |
| 1010 | break; |
| 1011 | case kRGB_H_SkPixelGeometry: |
| 1012 | // our default, do nothing. |
| 1013 | break; |
| 1014 | case kBGR_H_SkPixelGeometry: |
| 1015 | flags |= SkScalerContext::kLCD_BGROrder_Flag; |
| 1016 | break; |
| 1017 | case kRGB_V_SkPixelGeometry: |
| 1018 | flags |= SkScalerContext::kLCD_Vertical_Flag; |
| 1019 | break; |
| 1020 | case kBGR_V_SkPixelGeometry: |
| 1021 | flags |= SkScalerContext::kLCD_Vertical_Flag; |
| 1022 | flags |= SkScalerContext::kLCD_BGROrder_Flag; |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | if (font.isEmbeddedBitmaps()) { |
| 1029 | flags |= SkScalerContext::kEmbeddedBitmapText_Flag; |
| 1030 | } |
| 1031 | if (font.isSubpixel()) { |
| 1032 | flags |= SkScalerContext::kSubpixelPositioning_Flag; |
| 1033 | } |
| 1034 | if (font.isForceAutoHinting()) { |
| 1035 | flags |= SkScalerContext::kForceAutohinting_Flag; |
| 1036 | } |
| 1037 | if (font.isLinearMetrics()) { |
| 1038 | flags |= SkScalerContext::kLinearMetrics_Flag; |
| 1039 | } |
| 1040 | if (font.isBaselineSnap()) { |
| 1041 | flags |= SkScalerContext::kBaselineSnap_Flag; |
| 1042 | } |
| 1043 | rec->fFlags = SkToU16(flags); |
| 1044 | |
| 1045 | // these modify fFlags, so do them after assigning fFlags |
| 1046 | rec->setHinting(font.getHinting()); |
| 1047 | rec->setLuminanceColor(SkPaintPriv::ComputeLuminanceColor(paint)); |
| 1048 | |
| 1049 | // For now always set the paint gamma equal to the device gamma. |
| 1050 | // The math in SkMaskGamma can handle them being different, |
| 1051 | // but it requires superluminous masks when |
| 1052 | // Ex : deviceGamma(x) < paintGamma(x) and x is sufficiently large. |
| 1053 | rec->setDeviceGamma(SK_GAMMA_EXPONENT); |
| 1054 | rec->setPaintGamma(SK_GAMMA_EXPONENT); |
| 1055 | |
| 1056 | #ifdef SK_GAMMA_CONTRAST |
| 1057 | rec->setContrast(SK_GAMMA_CONTRAST); |
| 1058 | #else |
| 1059 | // A value of 0.5 for SK_GAMMA_CONTRAST appears to be a good compromise. |
| 1060 | // With lower values small text appears washed out (though correctly so). |
| 1061 | // With higher values lcd fringing is worse and the smoothing effect of |
| 1062 | // partial coverage is diminished. |
| 1063 | rec->setContrast(0.5f); |
| 1064 | #endif |
| 1065 | |
| 1066 | if (!SkToBool(scalerContextFlags & SkScalerContextFlags::kFakeGamma)) { |
| 1067 | rec->ignoreGamma(); |
| 1068 | } |
| 1069 | if (!SkToBool(scalerContextFlags & SkScalerContextFlags::kBoostContrast)) { |
| 1070 | rec->setContrast(0); |
| 1071 | } |
| 1072 | |
| 1073 | new (effects) SkScalerContextEffects{paint}; |
| 1074 | } |
| 1075 | |
| 1076 | SkDescriptor* SkScalerContext::MakeDescriptorForPaths(SkFontID typefaceID, |
| 1077 | SkAutoDescriptor* ad) { |
| 1078 | SkScalerContextRec rec; |
| 1079 | memset((void*)&rec, 0, sizeof(rec)); |
| 1080 | rec.fFontID = typefaceID; |
| 1081 | rec.fTextSize = SkFontPriv::kCanonicalTextSizeForPaths; |
| 1082 | rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1; |
| 1083 | return AutoDescriptorGivenRecAndEffects(rec, SkScalerContextEffects(), ad); |
| 1084 | } |
| 1085 | |
| 1086 | SkDescriptor* SkScalerContext::CreateDescriptorAndEffectsUsingPaint( |
| 1087 | const SkFont& font, const SkPaint& paint, const SkSurfaceProps& surfaceProps, |
| 1088 | SkScalerContextFlags scalerContextFlags, const SkMatrix& deviceMatrix, SkAutoDescriptor* ad, |
| 1089 | SkScalerContextEffects* effects) |
| 1090 | { |
| 1091 | SkScalerContextRec rec; |
| 1092 | MakeRecAndEffects(font, paint, surfaceProps, scalerContextFlags, deviceMatrix, &rec, effects); |
| 1093 | return AutoDescriptorGivenRecAndEffects(rec, *effects, ad); |
| 1094 | } |
| 1095 | |
| 1096 | static size_t calculate_size_and_flatten(const SkScalerContextRec& rec, |
| 1097 | const SkScalerContextEffects& effects, |
| 1098 | SkBinaryWriteBuffer* effectBuffer) { |
| 1099 | size_t descSize = sizeof(rec); |
| 1100 | int entryCount = 1; |
| 1101 | |
| 1102 | if (effects.fPathEffect || effects.fMaskFilter) { |
| 1103 | if (effects.fPathEffect) { effectBuffer->writeFlattenable(effects.fPathEffect); } |
| 1104 | if (effects.fMaskFilter) { effectBuffer->writeFlattenable(effects.fMaskFilter); } |
| 1105 | entryCount += 1; |
| 1106 | descSize += effectBuffer->bytesWritten(); |
| 1107 | } |
| 1108 | |
| 1109 | descSize += SkDescriptor::ComputeOverhead(entryCount); |
| 1110 | return descSize; |
| 1111 | } |
| 1112 | |
| 1113 | static void generate_descriptor(const SkScalerContextRec& rec, |
| 1114 | const SkBinaryWriteBuffer& effectBuffer, |
| 1115 | SkDescriptor* desc) { |
| 1116 | desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 1117 | |
| 1118 | if (effectBuffer.bytesWritten() > 0) { |
| 1119 | effectBuffer.writeToMemory(desc->addEntry(kEffects_SkDescriptorTag, |
| 1120 | effectBuffer.bytesWritten(), |
| 1121 | nullptr)); |
| 1122 | } |
| 1123 | |
| 1124 | desc->computeChecksum(); |
| 1125 | } |
| 1126 | |
| 1127 | SkDescriptor* SkScalerContext::AutoDescriptorGivenRecAndEffects( |
| 1128 | const SkScalerContextRec& rec, |
| 1129 | const SkScalerContextEffects& effects, |
| 1130 | SkAutoDescriptor* ad) |
| 1131 | { |
| 1132 | SkBinaryWriteBuffer buf; |
| 1133 | |
| 1134 | ad->reset(calculate_size_and_flatten(rec, effects, &buf)); |
| 1135 | generate_descriptor(rec, buf, ad->getDesc()); |
| 1136 | |
| 1137 | return ad->getDesc(); |
| 1138 | } |
| 1139 | |
| 1140 | std::unique_ptr<SkDescriptor> SkScalerContext::DescriptorGivenRecAndEffects( |
| 1141 | const SkScalerContextRec& rec, |
| 1142 | const SkScalerContextEffects& effects) |
| 1143 | { |
| 1144 | SkBinaryWriteBuffer buf; |
| 1145 | |
| 1146 | auto desc = SkDescriptor::Alloc(calculate_size_and_flatten(rec, effects, &buf)); |
| 1147 | generate_descriptor(rec, buf, desc.get()); |
| 1148 | |
| 1149 | return desc; |
| 1150 | } |
| 1151 | |
| 1152 | void SkScalerContext::DescriptorBufferGiveRec(const SkScalerContextRec& rec, void* buffer) { |
| 1153 | generate_descriptor(rec, SkBinaryWriteBuffer{}, (SkDescriptor*)buffer); |
| 1154 | } |
| 1155 | |
| 1156 | bool SkScalerContext::CheckBufferSizeForRec(const SkScalerContextRec& rec, |
| 1157 | const SkScalerContextEffects& effects, |
| 1158 | size_t size) { |
| 1159 | SkBinaryWriteBuffer buf; |
| 1160 | return size >= calculate_size_and_flatten(rec, effects, &buf); |
| 1161 | } |
| 1162 | |
| 1163 | SkScalerContext* SkScalerContext::MakeEmptyContext( |
| 1164 | sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects, |
| 1165 | const SkDescriptor* desc) { |
| 1166 | class SkScalerContext_Empty : public SkScalerContext { |
| 1167 | public: |
| 1168 | SkScalerContext_Empty(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects, |
| 1169 | const SkDescriptor* desc) |
| 1170 | : SkScalerContext(std::move(typeface), effects, desc) {} |
| 1171 | |
| 1172 | protected: |
| 1173 | unsigned generateGlyphCount() override { |
| 1174 | return 0; |
| 1175 | } |
| 1176 | bool generateAdvance(SkGlyph* glyph) override { |
| 1177 | glyph->zeroMetrics(); |
| 1178 | return true; |
| 1179 | } |
| 1180 | void generateMetrics(SkGlyph* glyph) override { |
| 1181 | glyph->fMaskFormat = fRec.fMaskFormat; |
| 1182 | glyph->zeroMetrics(); |
| 1183 | } |
| 1184 | void generateImage(const SkGlyph& glyph) override {} |
| 1185 | bool generatePath(SkGlyphID glyph, SkPath* path) override { |
| 1186 | path->reset(); |
| 1187 | return false; |
| 1188 | } |
| 1189 | void generateFontMetrics(SkFontMetrics* metrics) override { |
| 1190 | if (metrics) { |
| 1191 | sk_bzero(metrics, sizeof(*metrics)); |
| 1192 | } |
| 1193 | } |
| 1194 | }; |
| 1195 | |
| 1196 | return new SkScalerContext_Empty{std::move(typeface), effects, desc}; |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |