Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include "SkScalerContext.h" |
michael@0 | 11 | #include "SkColorPriv.h" |
michael@0 | 12 | #include "SkDescriptor.h" |
michael@0 | 13 | #include "SkDraw.h" |
michael@0 | 14 | #include "SkFontHost.h" |
michael@0 | 15 | #include "SkGlyph.h" |
michael@0 | 16 | #include "SkMaskFilter.h" |
michael@0 | 17 | #include "SkMaskGamma.h" |
michael@0 | 18 | #include "SkReadBuffer.h" |
michael@0 | 19 | #include "SkWriteBuffer.h" |
michael@0 | 20 | #include "SkPathEffect.h" |
michael@0 | 21 | #include "SkRasterizer.h" |
michael@0 | 22 | #include "SkRasterClip.h" |
michael@0 | 23 | #include "SkStroke.h" |
michael@0 | 24 | #include "SkThread.h" |
michael@0 | 25 | |
michael@0 | 26 | #ifdef SK_BUILD_FOR_ANDROID |
michael@0 | 27 | #include "SkTypeface_android.h" |
michael@0 | 28 | #endif |
michael@0 | 29 | |
michael@0 | 30 | #define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3) |
michael@0 | 31 | |
michael@0 | 32 | void SkGlyph::toMask(SkMask* mask) const { |
michael@0 | 33 | SkASSERT(mask); |
michael@0 | 34 | |
michael@0 | 35 | mask->fImage = (uint8_t*)fImage; |
michael@0 | 36 | mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight); |
michael@0 | 37 | mask->fRowBytes = this->rowBytes(); |
michael@0 | 38 | mask->fFormat = static_cast<SkMask::Format>(fMaskFormat); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | size_t SkGlyph::computeImageSize() const { |
michael@0 | 42 | const size_t size = this->rowBytes() * fHeight; |
michael@0 | 43 | |
michael@0 | 44 | switch (fMaskFormat) { |
michael@0 | 45 | case SkMask::k3D_Format: |
michael@0 | 46 | return 3 * size; |
michael@0 | 47 | default: |
michael@0 | 48 | return size; |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | void SkGlyph::zeroMetrics() { |
michael@0 | 53 | fAdvanceX = 0; |
michael@0 | 54 | fAdvanceY = 0; |
michael@0 | 55 | fWidth = 0; |
michael@0 | 56 | fHeight = 0; |
michael@0 | 57 | fTop = 0; |
michael@0 | 58 | fLeft = 0; |
michael@0 | 59 | fRsbDelta = 0; |
michael@0 | 60 | fLsbDelta = 0; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 64 | |
michael@0 | 65 | #ifdef SK_DEBUG |
michael@0 | 66 | #define DUMP_RECx |
michael@0 | 67 | #endif |
michael@0 | 68 | |
michael@0 | 69 | static SkFlattenable* load_flattenable(const SkDescriptor* desc, uint32_t tag, |
michael@0 | 70 | SkFlattenable::Type ft) { |
michael@0 | 71 | SkFlattenable* obj = NULL; |
michael@0 | 72 | uint32_t len; |
michael@0 | 73 | const void* data = desc->findEntry(tag, &len); |
michael@0 | 74 | |
michael@0 | 75 | if (data) { |
michael@0 | 76 | SkReadBuffer buffer(data, len); |
michael@0 | 77 | obj = buffer.readFlattenable(ft); |
michael@0 | 78 | SkASSERT(buffer.offset() == buffer.size()); |
michael@0 | 79 | } |
michael@0 | 80 | return obj; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | SkScalerContext::SkScalerContext(SkTypeface* typeface, const SkDescriptor* desc) |
michael@0 | 84 | : fRec(*static_cast<const Rec*>(desc->findEntry(kRec_SkDescriptorTag, NULL))) |
michael@0 | 85 | |
michael@0 | 86 | , fBaseGlyphCount(0) |
michael@0 | 87 | , fTypeface(SkRef(typeface)) |
michael@0 | 88 | , fPathEffect(static_cast<SkPathEffect*>(load_flattenable(desc, kPathEffect_SkDescriptorTag, |
michael@0 | 89 | SkFlattenable::kSkPathEffect_Type))) |
michael@0 | 90 | , fMaskFilter(static_cast<SkMaskFilter*>(load_flattenable(desc, kMaskFilter_SkDescriptorTag, |
michael@0 | 91 | SkFlattenable::kSkMaskFilter_Type))) |
michael@0 | 92 | , fRasterizer(static_cast<SkRasterizer*>(load_flattenable(desc, kRasterizer_SkDescriptorTag, |
michael@0 | 93 | SkFlattenable::kSkRasterizer_Type))) |
michael@0 | 94 | // Initialize based on our settings. Subclasses can also force this. |
michael@0 | 95 | , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != NULL || fRasterizer != NULL) |
michael@0 | 96 | |
michael@0 | 97 | , fNextContext(NULL) |
michael@0 | 98 | |
michael@0 | 99 | , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec)) |
michael@0 | 100 | , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec) |
michael@0 | 101 | : SkMaskGamma::PreBlend()) |
michael@0 | 102 | { |
michael@0 | 103 | #ifdef DUMP_REC |
michael@0 | 104 | desc->assertChecksum(); |
michael@0 | 105 | SkDebugf("SkScalerContext checksum %x count %d length %d\n", |
michael@0 | 106 | desc->getChecksum(), desc->getCount(), desc->getLength()); |
michael@0 | 107 | SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n", |
michael@0 | 108 | rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0], |
michael@0 | 109 | rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]); |
michael@0 | 110 | SkDebugf(" frame %g miter %g hints %d framefill %d format %d join %d\n", |
michael@0 | 111 | rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill, |
michael@0 | 112 | rec->fMaskFormat, rec->fStrokeJoin); |
michael@0 | 113 | SkDebugf(" pathEffect %x maskFilter %x\n", |
michael@0 | 114 | desc->findEntry(kPathEffect_SkDescriptorTag, NULL), |
michael@0 | 115 | desc->findEntry(kMaskFilter_SkDescriptorTag, NULL)); |
michael@0 | 116 | #endif |
michael@0 | 117 | #ifdef SK_BUILD_FOR_ANDROID |
michael@0 | 118 | uint32_t len; |
michael@0 | 119 | const void* data = desc->findEntry(kAndroidOpts_SkDescriptorTag, &len); |
michael@0 | 120 | if (data) { |
michael@0 | 121 | SkReadBuffer buffer(data, len); |
michael@0 | 122 | fPaintOptionsAndroid.unflatten(buffer); |
michael@0 | 123 | SkASSERT(buffer.offset() == buffer.size()); |
michael@0 | 124 | } |
michael@0 | 125 | #endif |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | SkScalerContext::~SkScalerContext() { |
michael@0 | 129 | SkDELETE(fNextContext); |
michael@0 | 130 | |
michael@0 | 131 | SkSafeUnref(fPathEffect); |
michael@0 | 132 | SkSafeUnref(fMaskFilter); |
michael@0 | 133 | SkSafeUnref(fRasterizer); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | // Return the context associated with the next logical typeface, or NULL if |
michael@0 | 137 | // there are no more entries in the fallback chain. |
michael@0 | 138 | SkScalerContext* SkScalerContext::allocNextContext() const { |
michael@0 | 139 | #ifdef SK_BUILD_FOR_ANDROID |
michael@0 | 140 | SkTypeface* newFace = SkAndroidNextLogicalTypeface(fRec.fFontID, |
michael@0 | 141 | fRec.fOrigFontID, |
michael@0 | 142 | fPaintOptionsAndroid); |
michael@0 | 143 | if (0 == newFace) { |
michael@0 | 144 | return NULL; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | SkAutoTUnref<SkTypeface> aur(newFace); |
michael@0 | 148 | uint32_t newFontID = newFace->uniqueID(); |
michael@0 | 149 | |
michael@0 | 150 | SkWriteBuffer androidBuffer; |
michael@0 | 151 | fPaintOptionsAndroid.flatten(androidBuffer); |
michael@0 | 152 | |
michael@0 | 153 | SkAutoDescriptor ad(sizeof(fRec) + androidBuffer.bytesWritten() |
michael@0 | 154 | + SkDescriptor::ComputeOverhead(2)); |
michael@0 | 155 | SkDescriptor* desc = ad.getDesc(); |
michael@0 | 156 | |
michael@0 | 157 | desc->init(); |
michael@0 | 158 | SkScalerContext::Rec* newRec = |
michael@0 | 159 | (SkScalerContext::Rec*)desc->addEntry(kRec_SkDescriptorTag, |
michael@0 | 160 | sizeof(fRec), &fRec); |
michael@0 | 161 | androidBuffer.writeToMemory(desc->addEntry(kAndroidOpts_SkDescriptorTag, |
michael@0 | 162 | androidBuffer.bytesWritten(), NULL)); |
michael@0 | 163 | |
michael@0 | 164 | newRec->fFontID = newFontID; |
michael@0 | 165 | desc->computeChecksum(); |
michael@0 | 166 | |
michael@0 | 167 | return newFace->createScalerContext(desc); |
michael@0 | 168 | #else |
michael@0 | 169 | return NULL; |
michael@0 | 170 | #endif |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | /* Return the next context, creating it if its not already created, but return |
michael@0 | 174 | NULL if the fonthost says there are no more fonts to fallback to. |
michael@0 | 175 | */ |
michael@0 | 176 | SkScalerContext* SkScalerContext::getNextContext() { |
michael@0 | 177 | SkScalerContext* next = fNextContext; |
michael@0 | 178 | // if next is null, then either it isn't cached yet, or we're at the |
michael@0 | 179 | // end of our possible chain |
michael@0 | 180 | if (NULL == next) { |
michael@0 | 181 | next = this->allocNextContext(); |
michael@0 | 182 | if (NULL == next) { |
michael@0 | 183 | return NULL; |
michael@0 | 184 | } |
michael@0 | 185 | // next's base is our base + our local count |
michael@0 | 186 | next->setBaseGlyphCount(fBaseGlyphCount + this->getGlyphCount()); |
michael@0 | 187 | // cache the answer |
michael@0 | 188 | fNextContext = next; |
michael@0 | 189 | } |
michael@0 | 190 | return next; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | SkScalerContext* SkScalerContext::getGlyphContext(const SkGlyph& glyph) { |
michael@0 | 194 | unsigned glyphID = glyph.getGlyphID(); |
michael@0 | 195 | SkScalerContext* ctx = this; |
michael@0 | 196 | for (;;) { |
michael@0 | 197 | unsigned count = ctx->getGlyphCount(); |
michael@0 | 198 | if (glyphID < count) { |
michael@0 | 199 | break; |
michael@0 | 200 | } |
michael@0 | 201 | glyphID -= count; |
michael@0 | 202 | ctx = ctx->getNextContext(); |
michael@0 | 203 | if (NULL == ctx) { |
michael@0 | 204 | // SkDebugf("--- no context for glyph %x\n", glyph.getGlyphID()); |
michael@0 | 205 | // just return the original context (this) |
michael@0 | 206 | return this; |
michael@0 | 207 | } |
michael@0 | 208 | } |
michael@0 | 209 | return ctx; |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | SkScalerContext* SkScalerContext::getContextFromChar(SkUnichar uni, |
michael@0 | 213 | uint16_t* glyphID) { |
michael@0 | 214 | SkScalerContext* ctx = this; |
michael@0 | 215 | for (;;) { |
michael@0 | 216 | const uint16_t glyph = ctx->generateCharToGlyph(uni); |
michael@0 | 217 | if (glyph) { |
michael@0 | 218 | if (NULL != glyphID) { |
michael@0 | 219 | *glyphID = glyph; |
michael@0 | 220 | } |
michael@0 | 221 | break; // found it |
michael@0 | 222 | } |
michael@0 | 223 | ctx = ctx->getNextContext(); |
michael@0 | 224 | if (NULL == ctx) { |
michael@0 | 225 | return NULL; |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | return ctx; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | #ifdef SK_BUILD_FOR_ANDROID |
michael@0 | 232 | SkFontID SkScalerContext::findTypefaceIdForChar(SkUnichar uni) { |
michael@0 | 233 | SkScalerContext* ctx = this->getContextFromChar(uni, NULL); |
michael@0 | 234 | if (NULL != ctx) { |
michael@0 | 235 | return ctx->fRec.fFontID; |
michael@0 | 236 | } else { |
michael@0 | 237 | return 0; |
michael@0 | 238 | } |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | /* This loops through all available fallback contexts (if needed) until it |
michael@0 | 242 | finds some context that can handle the unichar and return it. |
michael@0 | 243 | |
michael@0 | 244 | As this is somewhat expensive operation, it should only be done on the first |
michael@0 | 245 | char of a run. |
michael@0 | 246 | */ |
michael@0 | 247 | unsigned SkScalerContext::getBaseGlyphCount(SkUnichar uni) { |
michael@0 | 248 | SkScalerContext* ctx = this->getContextFromChar(uni, NULL); |
michael@0 | 249 | if (NULL != ctx) { |
michael@0 | 250 | return ctx->fBaseGlyphCount; |
michael@0 | 251 | } else { |
michael@0 | 252 | SkDEBUGF(("--- no context for char %x\n", uni)); |
michael@0 | 253 | return this->fBaseGlyphCount; |
michael@0 | 254 | } |
michael@0 | 255 | } |
michael@0 | 256 | #endif |
michael@0 | 257 | |
michael@0 | 258 | /* This loops through all available fallback contexts (if needed) until it |
michael@0 | 259 | finds some context that can handle the unichar. If all fail, returns 0 |
michael@0 | 260 | */ |
michael@0 | 261 | uint16_t SkScalerContext::charToGlyphID(SkUnichar uni) { |
michael@0 | 262 | |
michael@0 | 263 | uint16_t tempID; |
michael@0 | 264 | SkScalerContext* ctx = this->getContextFromChar(uni, &tempID); |
michael@0 | 265 | if (NULL == ctx) { |
michael@0 | 266 | return 0; // no more contexts, return missing glyph |
michael@0 | 267 | } |
michael@0 | 268 | // add the ctx's base, making glyphID unique for chain of contexts |
michael@0 | 269 | unsigned glyphID = tempID + ctx->fBaseGlyphCount; |
michael@0 | 270 | // check for overflow of 16bits, since our glyphID cannot exceed that |
michael@0 | 271 | if (glyphID > 0xFFFF) { |
michael@0 | 272 | glyphID = 0; |
michael@0 | 273 | } |
michael@0 | 274 | return SkToU16(glyphID); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | SkUnichar SkScalerContext::glyphIDToChar(uint16_t glyphID) { |
michael@0 | 278 | SkScalerContext* ctx = this; |
michael@0 | 279 | unsigned rangeEnd = 0; |
michael@0 | 280 | do { |
michael@0 | 281 | unsigned rangeStart = rangeEnd; |
michael@0 | 282 | |
michael@0 | 283 | rangeEnd += ctx->getGlyphCount(); |
michael@0 | 284 | if (rangeStart <= glyphID && glyphID < rangeEnd) { |
michael@0 | 285 | return ctx->generateGlyphToChar(glyphID - rangeStart); |
michael@0 | 286 | } |
michael@0 | 287 | ctx = ctx->getNextContext(); |
michael@0 | 288 | } while (NULL != ctx); |
michael@0 | 289 | return 0; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | void SkScalerContext::getAdvance(SkGlyph* glyph) { |
michael@0 | 293 | // mark us as just having a valid advance |
michael@0 | 294 | glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE; |
michael@0 | 295 | // we mark the format before making the call, in case the impl |
michael@0 | 296 | // internally ends up calling its generateMetrics, which is OK |
michael@0 | 297 | // albeit slower than strictly necessary |
michael@0 | 298 | this->getGlyphContext(*glyph)->generateAdvance(glyph); |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | void SkScalerContext::getMetrics(SkGlyph* glyph) { |
michael@0 | 302 | this->getGlyphContext(*glyph)->generateMetrics(glyph); |
michael@0 | 303 | |
michael@0 | 304 | // for now we have separate cache entries for devkerning on and off |
michael@0 | 305 | // in the future we might share caches, but make our measure/draw |
michael@0 | 306 | // code make the distinction. Thus we zap the values if the caller |
michael@0 | 307 | // has not asked for them. |
michael@0 | 308 | if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) { |
michael@0 | 309 | // no devkern, so zap the fields |
michael@0 | 310 | glyph->fLsbDelta = glyph->fRsbDelta = 0; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // if either dimension is empty, zap the image bounds of the glyph |
michael@0 | 314 | if (0 == glyph->fWidth || 0 == glyph->fHeight) { |
michael@0 | 315 | glyph->fWidth = 0; |
michael@0 | 316 | glyph->fHeight = 0; |
michael@0 | 317 | glyph->fTop = 0; |
michael@0 | 318 | glyph->fLeft = 0; |
michael@0 | 319 | glyph->fMaskFormat = 0; |
michael@0 | 320 | return; |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | if (fGenerateImageFromPath) { |
michael@0 | 324 | SkPath devPath, fillPath; |
michael@0 | 325 | SkMatrix fillToDevMatrix; |
michael@0 | 326 | |
michael@0 | 327 | this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); |
michael@0 | 328 | |
michael@0 | 329 | if (fRasterizer) { |
michael@0 | 330 | SkMask mask; |
michael@0 | 331 | |
michael@0 | 332 | if (fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, |
michael@0 | 333 | fMaskFilter, &mask, |
michael@0 | 334 | SkMask::kJustComputeBounds_CreateMode)) { |
michael@0 | 335 | glyph->fLeft = mask.fBounds.fLeft; |
michael@0 | 336 | glyph->fTop = mask.fBounds.fTop; |
michael@0 | 337 | glyph->fWidth = SkToU16(mask.fBounds.width()); |
michael@0 | 338 | glyph->fHeight = SkToU16(mask.fBounds.height()); |
michael@0 | 339 | } else { |
michael@0 | 340 | goto SK_ERROR; |
michael@0 | 341 | } |
michael@0 | 342 | } else { |
michael@0 | 343 | // just use devPath |
michael@0 | 344 | SkIRect ir; |
michael@0 | 345 | devPath.getBounds().roundOut(&ir); |
michael@0 | 346 | |
michael@0 | 347 | if (ir.isEmpty() || !ir.is16Bit()) { |
michael@0 | 348 | goto SK_ERROR; |
michael@0 | 349 | } |
michael@0 | 350 | glyph->fLeft = ir.fLeft; |
michael@0 | 351 | glyph->fTop = ir.fTop; |
michael@0 | 352 | glyph->fWidth = SkToU16(ir.width()); |
michael@0 | 353 | glyph->fHeight = SkToU16(ir.height()); |
michael@0 | 354 | |
michael@0 | 355 | if (glyph->fWidth > 0) { |
michael@0 | 356 | switch (fRec.fMaskFormat) { |
michael@0 | 357 | case SkMask::kLCD16_Format: |
michael@0 | 358 | case SkMask::kLCD32_Format: |
michael@0 | 359 | glyph->fWidth += 2; |
michael@0 | 360 | glyph->fLeft -= 1; |
michael@0 | 361 | break; |
michael@0 | 362 | default: |
michael@0 | 363 | break; |
michael@0 | 364 | } |
michael@0 | 365 | } |
michael@0 | 366 | } |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | if (SkMask::kARGB32_Format != glyph->fMaskFormat) { |
michael@0 | 370 | glyph->fMaskFormat = fRec.fMaskFormat; |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | // If we are going to create the mask, then we cannot keep the color |
michael@0 | 374 | if ((fGenerateImageFromPath || fMaskFilter) && |
michael@0 | 375 | SkMask::kARGB32_Format == glyph->fMaskFormat) { |
michael@0 | 376 | glyph->fMaskFormat = SkMask::kA8_Format; |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | if (fMaskFilter) { |
michael@0 | 380 | SkMask src, dst; |
michael@0 | 381 | SkMatrix matrix; |
michael@0 | 382 | |
michael@0 | 383 | glyph->toMask(&src); |
michael@0 | 384 | fRec.getMatrixFrom2x2(&matrix); |
michael@0 | 385 | |
michael@0 | 386 | src.fImage = NULL; // only want the bounds from the filter |
michael@0 | 387 | if (fMaskFilter->filterMask(&dst, src, matrix, NULL)) { |
michael@0 | 388 | if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) { |
michael@0 | 389 | goto SK_ERROR; |
michael@0 | 390 | } |
michael@0 | 391 | SkASSERT(dst.fImage == NULL); |
michael@0 | 392 | glyph->fLeft = dst.fBounds.fLeft; |
michael@0 | 393 | glyph->fTop = dst.fBounds.fTop; |
michael@0 | 394 | glyph->fWidth = SkToU16(dst.fBounds.width()); |
michael@0 | 395 | glyph->fHeight = SkToU16(dst.fBounds.height()); |
michael@0 | 396 | glyph->fMaskFormat = dst.fFormat; |
michael@0 | 397 | } |
michael@0 | 398 | } |
michael@0 | 399 | return; |
michael@0 | 400 | |
michael@0 | 401 | SK_ERROR: |
michael@0 | 402 | // draw nothing 'cause we failed |
michael@0 | 403 | glyph->fLeft = 0; |
michael@0 | 404 | glyph->fTop = 0; |
michael@0 | 405 | glyph->fWidth = 0; |
michael@0 | 406 | glyph->fHeight = 0; |
michael@0 | 407 | // put a valid value here, in case it was earlier set to |
michael@0 | 408 | // MASK_FORMAT_JUST_ADVANCE |
michael@0 | 409 | glyph->fMaskFormat = fRec.fMaskFormat; |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | #define SK_SHOW_TEXT_BLIT_COVERAGE 0 |
michael@0 | 413 | |
michael@0 | 414 | static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) { |
michael@0 | 415 | uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage; |
michael@0 | 416 | unsigned rowBytes = mask.fRowBytes; |
michael@0 | 417 | |
michael@0 | 418 | for (int y = mask.fBounds.height() - 1; y >= 0; --y) { |
michael@0 | 419 | for (int x = mask.fBounds.width() - 1; x >= 0; --x) { |
michael@0 | 420 | dst[x] = lut[dst[x]]; |
michael@0 | 421 | } |
michael@0 | 422 | dst += rowBytes; |
michael@0 | 423 | } |
michael@0 | 424 | } |
michael@0 | 425 | |
michael@0 | 426 | template<bool APPLY_PREBLEND> |
michael@0 | 427 | static void pack4xHToLCD16(const SkBitmap& src, const SkMask& dst, |
michael@0 | 428 | const SkMaskGamma::PreBlend& maskPreBlend) { |
michael@0 | 429 | #define SAMPLES_PER_PIXEL 4 |
michael@0 | 430 | #define LCD_PER_PIXEL 3 |
michael@0 | 431 | SkASSERT(kAlpha_8_SkColorType == src.colorType()); |
michael@0 | 432 | SkASSERT(SkMask::kLCD16_Format == dst.fFormat); |
michael@0 | 433 | |
michael@0 | 434 | const int sample_width = src.width(); |
michael@0 | 435 | const int height = src.height(); |
michael@0 | 436 | |
michael@0 | 437 | uint16_t* dstP = (uint16_t*)dst.fImage; |
michael@0 | 438 | size_t dstRB = dst.fRowBytes; |
michael@0 | 439 | // An N tap FIR is defined by |
michael@0 | 440 | // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N] |
michael@0 | 441 | // or |
michael@0 | 442 | // out[n] = sum(i, 0, N, coeff[i]*x[n-i]) |
michael@0 | 443 | |
michael@0 | 444 | // The strategy is to use one FIR (different coefficients) for each of r, g, and b. |
michael@0 | 445 | // This means using every 4th FIR output value of each FIR and discarding the rest. |
michael@0 | 446 | // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'. |
michael@0 | 447 | // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.) |
michael@0 | 448 | |
michael@0 | 449 | // These are in some fixed point repesentation. |
michael@0 | 450 | // Adding up to more than one simulates ink spread. |
michael@0 | 451 | // For implementation reasons, these should never add up to more than two. |
michael@0 | 452 | |
michael@0 | 453 | // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast'). |
michael@0 | 454 | // Calculated using tools/generate_fir_coeff.py |
michael@0 | 455 | // With this one almost no fringing is ever seen, but it is imperceptibly blurry. |
michael@0 | 456 | // The lcd smoothed text is almost imperceptibly different from gray, |
michael@0 | 457 | // but is still sharper on small stems and small rounded corners than gray. |
michael@0 | 458 | // This also seems to be about as wide as one can get and only have a three pixel kernel. |
michael@0 | 459 | // TODO: caculate these at runtime so parameters can be adjusted (esp contrast). |
michael@0 | 460 | static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = { |
michael@0 | 461 | //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted. |
michael@0 | 462 | { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, }, |
michael@0 | 463 | //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric |
michael@0 | 464 | { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, }, |
michael@0 | 465 | //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted. |
michael@0 | 466 | { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, }, |
michael@0 | 467 | }; |
michael@0 | 468 | |
michael@0 | 469 | for (int y = 0; y < height; ++y) { |
michael@0 | 470 | const uint8_t* srcP = src.getAddr8(0, y); |
michael@0 | 471 | |
michael@0 | 472 | // TODO: this fir filter implementation is straight forward, but slow. |
michael@0 | 473 | // It should be possible to make it much faster. |
michael@0 | 474 | for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) { |
michael@0 | 475 | int fir[LCD_PER_PIXEL] = { 0 }; |
michael@0 | 476 | for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4) |
michael@0 | 477 | ; sample_index < SkMin32(sample_x + 8, sample_width) |
michael@0 | 478 | ; ++sample_index, ++coeff_index) |
michael@0 | 479 | { |
michael@0 | 480 | int sample_value = srcP[sample_index]; |
michael@0 | 481 | for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { |
michael@0 | 482 | fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value; |
michael@0 | 483 | } |
michael@0 | 484 | } |
michael@0 | 485 | for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { |
michael@0 | 486 | fir[subpxl_index] /= 0x100; |
michael@0 | 487 | fir[subpxl_index] = SkMin32(fir[subpxl_index], 255); |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(fir[0], maskPreBlend.fR); |
michael@0 | 491 | U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(fir[1], maskPreBlend.fG); |
michael@0 | 492 | U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(fir[2], maskPreBlend.fB); |
michael@0 | 493 | #if SK_SHOW_TEXT_BLIT_COVERAGE |
michael@0 | 494 | r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10); |
michael@0 | 495 | #endif |
michael@0 | 496 | dstP[pixel_x] = SkPack888ToRGB16(r, g, b); |
michael@0 | 497 | } |
michael@0 | 498 | dstP = (uint16_t*)((char*)dstP + dstRB); |
michael@0 | 499 | } |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | template<bool APPLY_PREBLEND> |
michael@0 | 503 | static void pack4xHToLCD32(const SkBitmap& src, const SkMask& dst, |
michael@0 | 504 | const SkMaskGamma::PreBlend& maskPreBlend) { |
michael@0 | 505 | SkASSERT(kAlpha_8_SkColorType == src.colorType()); |
michael@0 | 506 | SkASSERT(SkMask::kLCD32_Format == dst.fFormat); |
michael@0 | 507 | |
michael@0 | 508 | const int width = dst.fBounds.width(); |
michael@0 | 509 | const int height = dst.fBounds.height(); |
michael@0 | 510 | SkPMColor* dstP = (SkPMColor*)dst.fImage; |
michael@0 | 511 | size_t dstRB = dst.fRowBytes; |
michael@0 | 512 | |
michael@0 | 513 | for (int y = 0; y < height; ++y) { |
michael@0 | 514 | const uint8_t* srcP = src.getAddr8(0, y); |
michael@0 | 515 | |
michael@0 | 516 | // TODO: need to use fir filter here as well. |
michael@0 | 517 | for (int x = 0; x < width; ++x) { |
michael@0 | 518 | U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>(*srcP++, maskPreBlend.fR); |
michael@0 | 519 | U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>(*srcP++, maskPreBlend.fG); |
michael@0 | 520 | U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>(*srcP++, maskPreBlend.fB); |
michael@0 | 521 | dstP[x] = SkPackARGB32(0xFF, r, g, b); |
michael@0 | 522 | } |
michael@0 | 523 | dstP = (SkPMColor*)((char*)dstP + dstRB); |
michael@0 | 524 | } |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | static inline int convert_8_to_1(unsigned byte) { |
michael@0 | 528 | SkASSERT(byte <= 0xFF); |
michael@0 | 529 | return byte >> 7; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | static uint8_t pack_8_to_1(const uint8_t alpha[8]) { |
michael@0 | 533 | unsigned bits = 0; |
michael@0 | 534 | for (int i = 0; i < 8; ++i) { |
michael@0 | 535 | bits <<= 1; |
michael@0 | 536 | bits |= convert_8_to_1(alpha[i]); |
michael@0 | 537 | } |
michael@0 | 538 | return SkToU8(bits); |
michael@0 | 539 | } |
michael@0 | 540 | |
michael@0 | 541 | static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) { |
michael@0 | 542 | const int height = mask.fBounds.height(); |
michael@0 | 543 | const int width = mask.fBounds.width(); |
michael@0 | 544 | const int octs = width >> 3; |
michael@0 | 545 | const int leftOverBits = width & 7; |
michael@0 | 546 | |
michael@0 | 547 | uint8_t* dst = mask.fImage; |
michael@0 | 548 | const int dstPad = mask.fRowBytes - SkAlign8(width)/8; |
michael@0 | 549 | SkASSERT(dstPad >= 0); |
michael@0 | 550 | |
michael@0 | 551 | const int srcPad = srcRB - width; |
michael@0 | 552 | SkASSERT(srcPad >= 0); |
michael@0 | 553 | |
michael@0 | 554 | for (int y = 0; y < height; ++y) { |
michael@0 | 555 | for (int i = 0; i < octs; ++i) { |
michael@0 | 556 | *dst++ = pack_8_to_1(src); |
michael@0 | 557 | src += 8; |
michael@0 | 558 | } |
michael@0 | 559 | if (leftOverBits > 0) { |
michael@0 | 560 | unsigned bits = 0; |
michael@0 | 561 | int shift = 7; |
michael@0 | 562 | for (int i = 0; i < leftOverBits; ++i, --shift) { |
michael@0 | 563 | bits |= convert_8_to_1(*src++) << shift; |
michael@0 | 564 | } |
michael@0 | 565 | *dst++ = bits; |
michael@0 | 566 | } |
michael@0 | 567 | src += srcPad; |
michael@0 | 568 | dst += dstPad; |
michael@0 | 569 | } |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | static void generateMask(const SkMask& mask, const SkPath& path, |
michael@0 | 573 | const SkMaskGamma::PreBlend& maskPreBlend) { |
michael@0 | 574 | SkPaint paint; |
michael@0 | 575 | |
michael@0 | 576 | int srcW = mask.fBounds.width(); |
michael@0 | 577 | int srcH = mask.fBounds.height(); |
michael@0 | 578 | int dstW = srcW; |
michael@0 | 579 | int dstH = srcH; |
michael@0 | 580 | int dstRB = mask.fRowBytes; |
michael@0 | 581 | |
michael@0 | 582 | SkMatrix matrix; |
michael@0 | 583 | matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), |
michael@0 | 584 | -SkIntToScalar(mask.fBounds.fTop)); |
michael@0 | 585 | |
michael@0 | 586 | SkBitmap::Config config = SkBitmap::kA8_Config; |
michael@0 | 587 | paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat); |
michael@0 | 588 | switch (mask.fFormat) { |
michael@0 | 589 | case SkMask::kBW_Format: |
michael@0 | 590 | dstRB = 0; // signals we need a copy |
michael@0 | 591 | break; |
michael@0 | 592 | case SkMask::kA8_Format: |
michael@0 | 593 | break; |
michael@0 | 594 | case SkMask::kLCD16_Format: |
michael@0 | 595 | case SkMask::kLCD32_Format: |
michael@0 | 596 | // TODO: trigger off LCD orientation |
michael@0 | 597 | dstW = 4*dstW - 8; |
michael@0 | 598 | matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1), |
michael@0 | 599 | -SkIntToScalar(mask.fBounds.fTop)); |
michael@0 | 600 | matrix.postScale(SkIntToScalar(4), SK_Scalar1); |
michael@0 | 601 | dstRB = 0; // signals we need a copy |
michael@0 | 602 | break; |
michael@0 | 603 | default: |
michael@0 | 604 | SkDEBUGFAIL("unexpected mask format"); |
michael@0 | 605 | } |
michael@0 | 606 | |
michael@0 | 607 | SkRasterClip clip; |
michael@0 | 608 | clip.setRect(SkIRect::MakeWH(dstW, dstH)); |
michael@0 | 609 | |
michael@0 | 610 | SkBitmap bm; |
michael@0 | 611 | bm.setConfig(config, dstW, dstH, dstRB); |
michael@0 | 612 | |
michael@0 | 613 | if (0 == dstRB) { |
michael@0 | 614 | if (!bm.allocPixels()) { |
michael@0 | 615 | // can't allocate offscreen, so empty the mask and return |
michael@0 | 616 | sk_bzero(mask.fImage, mask.computeImageSize()); |
michael@0 | 617 | return; |
michael@0 | 618 | } |
michael@0 | 619 | bm.lockPixels(); |
michael@0 | 620 | } else { |
michael@0 | 621 | bm.setPixels(mask.fImage); |
michael@0 | 622 | } |
michael@0 | 623 | sk_bzero(bm.getPixels(), bm.getSafeSize()); |
michael@0 | 624 | |
michael@0 | 625 | SkDraw draw; |
michael@0 | 626 | draw.fRC = &clip; |
michael@0 | 627 | draw.fClip = &clip.bwRgn(); |
michael@0 | 628 | draw.fMatrix = &matrix; |
michael@0 | 629 | draw.fBitmap = &bm; |
michael@0 | 630 | draw.drawPath(path, paint); |
michael@0 | 631 | |
michael@0 | 632 | switch (mask.fFormat) { |
michael@0 | 633 | case SkMask::kBW_Format: |
michael@0 | 634 | packA8ToA1(mask, bm.getAddr8(0, 0), bm.rowBytes()); |
michael@0 | 635 | break; |
michael@0 | 636 | case SkMask::kA8_Format: |
michael@0 | 637 | if (maskPreBlend.isApplicable()) { |
michael@0 | 638 | applyLUTToA8Mask(mask, maskPreBlend.fG); |
michael@0 | 639 | } |
michael@0 | 640 | break; |
michael@0 | 641 | case SkMask::kLCD16_Format: |
michael@0 | 642 | if (maskPreBlend.isApplicable()) { |
michael@0 | 643 | pack4xHToLCD16<true>(bm, mask, maskPreBlend); |
michael@0 | 644 | } else { |
michael@0 | 645 | pack4xHToLCD16<false>(bm, mask, maskPreBlend); |
michael@0 | 646 | } |
michael@0 | 647 | break; |
michael@0 | 648 | case SkMask::kLCD32_Format: |
michael@0 | 649 | if (maskPreBlend.isApplicable()) { |
michael@0 | 650 | pack4xHToLCD32<true>(bm, mask, maskPreBlend); |
michael@0 | 651 | } else { |
michael@0 | 652 | pack4xHToLCD32<false>(bm, mask, maskPreBlend); |
michael@0 | 653 | } |
michael@0 | 654 | break; |
michael@0 | 655 | default: |
michael@0 | 656 | break; |
michael@0 | 657 | } |
michael@0 | 658 | } |
michael@0 | 659 | |
michael@0 | 660 | static void extract_alpha(const SkMask& dst, |
michael@0 | 661 | const SkPMColor* srcRow, size_t srcRB) { |
michael@0 | 662 | int width = dst.fBounds.width(); |
michael@0 | 663 | int height = dst.fBounds.height(); |
michael@0 | 664 | int dstRB = dst.fRowBytes; |
michael@0 | 665 | uint8_t* dstRow = dst.fImage; |
michael@0 | 666 | |
michael@0 | 667 | for (int y = 0; y < height; ++y) { |
michael@0 | 668 | for (int x = 0; x < width; ++x) { |
michael@0 | 669 | dstRow[x] = SkGetPackedA32(srcRow[x]); |
michael@0 | 670 | } |
michael@0 | 671 | // zero any padding on each row |
michael@0 | 672 | for (int x = width; x < dstRB; ++x) { |
michael@0 | 673 | dstRow[x] = 0; |
michael@0 | 674 | } |
michael@0 | 675 | dstRow += dstRB; |
michael@0 | 676 | srcRow = (const SkPMColor*)((const char*)srcRow + srcRB); |
michael@0 | 677 | } |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | void SkScalerContext::getImage(const SkGlyph& origGlyph) { |
michael@0 | 681 | const SkGlyph* glyph = &origGlyph; |
michael@0 | 682 | SkGlyph tmpGlyph; |
michael@0 | 683 | |
michael@0 | 684 | // in case we need to call generateImage on a mask-format that is different |
michael@0 | 685 | // (i.e. larger) than what our caller allocated by looking at origGlyph. |
michael@0 | 686 | SkAutoMalloc tmpGlyphImageStorage; |
michael@0 | 687 | |
michael@0 | 688 | // If we are going to draw-from-path, then we cannot generate color, since |
michael@0 | 689 | // the path only makes a mask. This case should have been caught up in |
michael@0 | 690 | // generateMetrics(). |
michael@0 | 691 | SkASSERT(!fGenerateImageFromPath || |
michael@0 | 692 | SkMask::kARGB32_Format != origGlyph.fMaskFormat); |
michael@0 | 693 | |
michael@0 | 694 | if (fMaskFilter) { // restore the prefilter bounds |
michael@0 | 695 | tmpGlyph.init(origGlyph.fID); |
michael@0 | 696 | |
michael@0 | 697 | // need the original bounds, sans our maskfilter |
michael@0 | 698 | SkMaskFilter* mf = fMaskFilter; |
michael@0 | 699 | fMaskFilter = NULL; // temp disable |
michael@0 | 700 | this->getMetrics(&tmpGlyph); |
michael@0 | 701 | fMaskFilter = mf; // restore |
michael@0 | 702 | |
michael@0 | 703 | // we need the prefilter bounds to be <= filter bounds |
michael@0 | 704 | SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth); |
michael@0 | 705 | SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight); |
michael@0 | 706 | |
michael@0 | 707 | if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) { |
michael@0 | 708 | tmpGlyph.fImage = origGlyph.fImage; |
michael@0 | 709 | } else { |
michael@0 | 710 | tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize()); |
michael@0 | 711 | tmpGlyph.fImage = tmpGlyphImageStorage.get(); |
michael@0 | 712 | } |
michael@0 | 713 | glyph = &tmpGlyph; |
michael@0 | 714 | } |
michael@0 | 715 | |
michael@0 | 716 | if (fGenerateImageFromPath) { |
michael@0 | 717 | SkPath devPath, fillPath; |
michael@0 | 718 | SkMatrix fillToDevMatrix; |
michael@0 | 719 | SkMask mask; |
michael@0 | 720 | |
michael@0 | 721 | this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); |
michael@0 | 722 | glyph->toMask(&mask); |
michael@0 | 723 | |
michael@0 | 724 | if (fRasterizer) { |
michael@0 | 725 | mask.fFormat = SkMask::kA8_Format; |
michael@0 | 726 | sk_bzero(glyph->fImage, mask.computeImageSize()); |
michael@0 | 727 | |
michael@0 | 728 | if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, |
michael@0 | 729 | fMaskFilter, &mask, |
michael@0 | 730 | SkMask::kJustRenderImage_CreateMode)) { |
michael@0 | 731 | return; |
michael@0 | 732 | } |
michael@0 | 733 | if (fPreBlend.isApplicable()) { |
michael@0 | 734 | applyLUTToA8Mask(mask, fPreBlend.fG); |
michael@0 | 735 | } |
michael@0 | 736 | } else { |
michael@0 | 737 | SkASSERT(SkMask::kARGB32_Format != mask.fFormat); |
michael@0 | 738 | generateMask(mask, devPath, fPreBlend); |
michael@0 | 739 | } |
michael@0 | 740 | } else { |
michael@0 | 741 | this->getGlyphContext(*glyph)->generateImage(*glyph); |
michael@0 | 742 | } |
michael@0 | 743 | |
michael@0 | 744 | if (fMaskFilter) { |
michael@0 | 745 | SkMask srcM, dstM; |
michael@0 | 746 | SkMatrix matrix; |
michael@0 | 747 | |
michael@0 | 748 | // the src glyph image shouldn't be 3D |
michael@0 | 749 | SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat); |
michael@0 | 750 | |
michael@0 | 751 | SkAutoSMalloc<32*32> a8storage; |
michael@0 | 752 | glyph->toMask(&srcM); |
michael@0 | 753 | if (SkMask::kARGB32_Format == srcM.fFormat) { |
michael@0 | 754 | // now we need to extract the alpha-channel from the glyph's image |
michael@0 | 755 | // and copy it into a temp buffer, and then point srcM at that temp. |
michael@0 | 756 | srcM.fFormat = SkMask::kA8_Format; |
michael@0 | 757 | srcM.fRowBytes = SkAlign4(srcM.fBounds.width()); |
michael@0 | 758 | size_t size = srcM.computeImageSize(); |
michael@0 | 759 | a8storage.reset(size); |
michael@0 | 760 | srcM.fImage = (uint8_t*)a8storage.get(); |
michael@0 | 761 | extract_alpha(srcM, |
michael@0 | 762 | (const SkPMColor*)glyph->fImage, glyph->rowBytes()); |
michael@0 | 763 | } |
michael@0 | 764 | |
michael@0 | 765 | fRec.getMatrixFrom2x2(&matrix); |
michael@0 | 766 | |
michael@0 | 767 | if (fMaskFilter->filterMask(&dstM, srcM, matrix, NULL)) { |
michael@0 | 768 | int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width()); |
michael@0 | 769 | int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height()); |
michael@0 | 770 | int dstRB = origGlyph.rowBytes(); |
michael@0 | 771 | int srcRB = dstM.fRowBytes; |
michael@0 | 772 | |
michael@0 | 773 | const uint8_t* src = (const uint8_t*)dstM.fImage; |
michael@0 | 774 | uint8_t* dst = (uint8_t*)origGlyph.fImage; |
michael@0 | 775 | |
michael@0 | 776 | if (SkMask::k3D_Format == dstM.fFormat) { |
michael@0 | 777 | // we have to copy 3 times as much |
michael@0 | 778 | height *= 3; |
michael@0 | 779 | } |
michael@0 | 780 | |
michael@0 | 781 | // clean out our glyph, since it may be larger than dstM |
michael@0 | 782 | //sk_bzero(dst, height * dstRB); |
michael@0 | 783 | |
michael@0 | 784 | while (--height >= 0) { |
michael@0 | 785 | memcpy(dst, src, width); |
michael@0 | 786 | src += srcRB; |
michael@0 | 787 | dst += dstRB; |
michael@0 | 788 | } |
michael@0 | 789 | SkMask::FreeImage(dstM.fImage); |
michael@0 | 790 | |
michael@0 | 791 | if (fPreBlendForFilter.isApplicable()) { |
michael@0 | 792 | applyLUTToA8Mask(srcM, fPreBlendForFilter.fG); |
michael@0 | 793 | } |
michael@0 | 794 | } |
michael@0 | 795 | } |
michael@0 | 796 | } |
michael@0 | 797 | |
michael@0 | 798 | void SkScalerContext::getPath(const SkGlyph& glyph, SkPath* path) { |
michael@0 | 799 | this->internalGetPath(glyph, NULL, path, NULL); |
michael@0 | 800 | } |
michael@0 | 801 | |
michael@0 | 802 | void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) { |
michael@0 | 803 | // All of this complexity should go away when we change generateFontMetrics |
michael@0 | 804 | // to just take one parameter (since it knows if it is vertical or not) |
michael@0 | 805 | SkPaint::FontMetrics* mx = NULL; |
michael@0 | 806 | SkPaint::FontMetrics* my = NULL; |
michael@0 | 807 | if (fRec.fFlags & kVertical_Flag) { |
michael@0 | 808 | mx = fm; |
michael@0 | 809 | } else { |
michael@0 | 810 | my = fm; |
michael@0 | 811 | } |
michael@0 | 812 | this->generateFontMetrics(mx, my); |
michael@0 | 813 | } |
michael@0 | 814 | |
michael@0 | 815 | SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) { |
michael@0 | 816 | return 0; |
michael@0 | 817 | } |
michael@0 | 818 | |
michael@0 | 819 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 820 | |
michael@0 | 821 | void SkScalerContext::internalGetPath(const SkGlyph& glyph, SkPath* fillPath, |
michael@0 | 822 | SkPath* devPath, SkMatrix* fillToDevMatrix) { |
michael@0 | 823 | SkPath path; |
michael@0 | 824 | |
michael@0 | 825 | this->getGlyphContext(glyph)->generatePath(glyph, &path); |
michael@0 | 826 | |
michael@0 | 827 | if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { |
michael@0 | 828 | SkFixed dx = glyph.getSubXFixed(); |
michael@0 | 829 | SkFixed dy = glyph.getSubYFixed(); |
michael@0 | 830 | if (dx | dy) { |
michael@0 | 831 | path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy)); |
michael@0 | 832 | } |
michael@0 | 833 | } |
michael@0 | 834 | |
michael@0 | 835 | if (fRec.fFrameWidth > 0 || fPathEffect != NULL) { |
michael@0 | 836 | // need the path in user-space, with only the point-size applied |
michael@0 | 837 | // so that our stroking and effects will operate the same way they |
michael@0 | 838 | // would if the user had extracted the path themself, and then |
michael@0 | 839 | // called drawPath |
michael@0 | 840 | SkPath localPath; |
michael@0 | 841 | SkMatrix matrix, inverse; |
michael@0 | 842 | |
michael@0 | 843 | fRec.getMatrixFrom2x2(&matrix); |
michael@0 | 844 | if (!matrix.invert(&inverse)) { |
michael@0 | 845 | // assume fillPath and devPath are already empty. |
michael@0 | 846 | return; |
michael@0 | 847 | } |
michael@0 | 848 | path.transform(inverse, &localPath); |
michael@0 | 849 | // now localPath is only affected by the paint settings, and not the canvas matrix |
michael@0 | 850 | |
michael@0 | 851 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
michael@0 | 852 | |
michael@0 | 853 | if (fRec.fFrameWidth > 0) { |
michael@0 | 854 | rec.setStrokeStyle(fRec.fFrameWidth, |
michael@0 | 855 | SkToBool(fRec.fFlags & kFrameAndFill_Flag)); |
michael@0 | 856 | // glyphs are always closed contours, so cap type is ignored, |
michael@0 | 857 | // so we just pass something. |
michael@0 | 858 | rec.setStrokeParams(SkPaint::kButt_Cap, |
michael@0 | 859 | (SkPaint::Join)fRec.fStrokeJoin, |
michael@0 | 860 | fRec.fMiterLimit); |
michael@0 | 861 | } |
michael@0 | 862 | |
michael@0 | 863 | if (fPathEffect) { |
michael@0 | 864 | SkPath effectPath; |
michael@0 | 865 | if (fPathEffect->filterPath(&effectPath, localPath, &rec, NULL)) { |
michael@0 | 866 | localPath.swap(effectPath); |
michael@0 | 867 | } |
michael@0 | 868 | } |
michael@0 | 869 | |
michael@0 | 870 | if (rec.needToApply()) { |
michael@0 | 871 | SkPath strokePath; |
michael@0 | 872 | if (rec.applyToPath(&strokePath, localPath)) { |
michael@0 | 873 | localPath.swap(strokePath); |
michael@0 | 874 | } |
michael@0 | 875 | } |
michael@0 | 876 | |
michael@0 | 877 | // now return stuff to the caller |
michael@0 | 878 | if (fillToDevMatrix) { |
michael@0 | 879 | *fillToDevMatrix = matrix; |
michael@0 | 880 | } |
michael@0 | 881 | if (devPath) { |
michael@0 | 882 | localPath.transform(matrix, devPath); |
michael@0 | 883 | } |
michael@0 | 884 | if (fillPath) { |
michael@0 | 885 | fillPath->swap(localPath); |
michael@0 | 886 | } |
michael@0 | 887 | } else { // nothing tricky to do |
michael@0 | 888 | if (fillToDevMatrix) { |
michael@0 | 889 | fillToDevMatrix->reset(); |
michael@0 | 890 | } |
michael@0 | 891 | if (devPath) { |
michael@0 | 892 | if (fillPath == NULL) { |
michael@0 | 893 | devPath->swap(path); |
michael@0 | 894 | } else { |
michael@0 | 895 | *devPath = path; |
michael@0 | 896 | } |
michael@0 | 897 | } |
michael@0 | 898 | |
michael@0 | 899 | if (fillPath) { |
michael@0 | 900 | fillPath->swap(path); |
michael@0 | 901 | } |
michael@0 | 902 | } |
michael@0 | 903 | |
michael@0 | 904 | if (devPath) { |
michael@0 | 905 | devPath->updateBoundsCache(); |
michael@0 | 906 | } |
michael@0 | 907 | if (fillPath) { |
michael@0 | 908 | fillPath->updateBoundsCache(); |
michael@0 | 909 | } |
michael@0 | 910 | } |
michael@0 | 911 | |
michael@0 | 912 | |
michael@0 | 913 | void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const { |
michael@0 | 914 | dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0, |
michael@0 | 915 | fPost2x2[1][0], fPost2x2[1][1], 0, |
michael@0 | 916 | 0, 0, SkScalarToPersp(SK_Scalar1)); |
michael@0 | 917 | } |
michael@0 | 918 | |
michael@0 | 919 | void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const { |
michael@0 | 920 | SkPaint::SetTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX); |
michael@0 | 921 | } |
michael@0 | 922 | |
michael@0 | 923 | void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const { |
michael@0 | 924 | this->getLocalMatrix(m); |
michael@0 | 925 | |
michael@0 | 926 | // now concat the device matrix |
michael@0 | 927 | SkMatrix deviceMatrix; |
michael@0 | 928 | this->getMatrixFrom2x2(&deviceMatrix); |
michael@0 | 929 | m->postConcat(deviceMatrix); |
michael@0 | 930 | } |
michael@0 | 931 | |
michael@0 | 932 | SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix) { |
michael@0 | 933 | SkASSERT(!matrix.hasPerspective()); |
michael@0 | 934 | |
michael@0 | 935 | if (0 == matrix[SkMatrix::kMSkewY]) { |
michael@0 | 936 | return kX_SkAxisAlignment; |
michael@0 | 937 | } |
michael@0 | 938 | if (0 == matrix[SkMatrix::kMScaleX]) { |
michael@0 | 939 | return kY_SkAxisAlignment; |
michael@0 | 940 | } |
michael@0 | 941 | return kNone_SkAxisAlignment; |
michael@0 | 942 | } |
michael@0 | 943 | |
michael@0 | 944 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 945 | |
michael@0 | 946 | #include "SkFontHost.h" |
michael@0 | 947 | |
michael@0 | 948 | class SkScalerContext_Empty : public SkScalerContext { |
michael@0 | 949 | public: |
michael@0 | 950 | SkScalerContext_Empty(SkTypeface* face, const SkDescriptor* desc) |
michael@0 | 951 | : SkScalerContext(face, desc) {} |
michael@0 | 952 | |
michael@0 | 953 | protected: |
michael@0 | 954 | virtual unsigned generateGlyphCount() SK_OVERRIDE { |
michael@0 | 955 | return 0; |
michael@0 | 956 | } |
michael@0 | 957 | virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE { |
michael@0 | 958 | return 0; |
michael@0 | 959 | } |
michael@0 | 960 | virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE { |
michael@0 | 961 | glyph->zeroMetrics(); |
michael@0 | 962 | } |
michael@0 | 963 | virtual void generateMetrics(SkGlyph* glyph) SK_OVERRIDE { |
michael@0 | 964 | glyph->zeroMetrics(); |
michael@0 | 965 | } |
michael@0 | 966 | virtual void generateImage(const SkGlyph& glyph) SK_OVERRIDE {} |
michael@0 | 967 | virtual void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE {} |
michael@0 | 968 | virtual void generateFontMetrics(SkPaint::FontMetrics* mx, |
michael@0 | 969 | SkPaint::FontMetrics* my) SK_OVERRIDE { |
michael@0 | 970 | if (mx) { |
michael@0 | 971 | sk_bzero(mx, sizeof(*mx)); |
michael@0 | 972 | } |
michael@0 | 973 | if (my) { |
michael@0 | 974 | sk_bzero(my, sizeof(*my)); |
michael@0 | 975 | } |
michael@0 | 976 | } |
michael@0 | 977 | }; |
michael@0 | 978 | |
michael@0 | 979 | extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc); |
michael@0 | 980 | |
michael@0 | 981 | SkScalerContext* SkTypeface::createScalerContext(const SkDescriptor* desc, |
michael@0 | 982 | bool allowFailure) const { |
michael@0 | 983 | SkScalerContext* c = this->onCreateScalerContext(desc); |
michael@0 | 984 | |
michael@0 | 985 | if (!c && !allowFailure) { |
michael@0 | 986 | c = SkNEW_ARGS(SkScalerContext_Empty, |
michael@0 | 987 | (const_cast<SkTypeface*>(this), desc)); |
michael@0 | 988 | } |
michael@0 | 989 | return c; |
michael@0 | 990 | } |