epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkScalerContext.h" |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkDescriptor.h" |
| 13 | #include "SkDraw.h" |
| 14 | #include "SkFontHost.h" |
| 15 | #include "SkMaskFilter.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 16 | #include "SkOrderedReadBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkPathEffect.h" |
| 18 | #include "SkRasterizer.h" |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 19 | #include "SkRasterClip.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkStroke.h" |
| 21 | #include "SkThread.h" |
| 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3) |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | void SkGlyph::toMask(SkMask* mask) const { |
| 26 | SkASSERT(mask); |
| 27 | |
| 28 | mask->fImage = (uint8_t*)fImage; |
| 29 | mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight); |
| 30 | mask->fRowBytes = this->rowBytes(); |
reed@android.com | 6c14b43 | 2009-03-23 20:11:11 +0000 | [diff] [blame] | 31 | mask->fFormat = static_cast<SkMask::Format>(fMaskFormat); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | size_t SkGlyph::computeImageSize() const { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 35 | const size_t size = this->rowBytes() * fHeight; |
| 36 | |
| 37 | switch (fMaskFormat) { |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 38 | case SkMask::k3D_Format: |
| 39 | return 3 * size; |
| 40 | default: |
| 41 | return size; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | } |
| 44 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 45 | void SkGlyph::zeroMetrics() { |
| 46 | fAdvanceX = 0; |
| 47 | fAdvanceY = 0; |
| 48 | fWidth = 0; |
| 49 | fHeight = 0; |
| 50 | fTop = 0; |
| 51 | fLeft = 0; |
| 52 | fRsbDelta = 0; |
| 53 | fLsbDelta = 0; |
| 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | #ifdef SK_DEBUG |
| 59 | #define DUMP_RECx |
| 60 | #endif |
| 61 | |
| 62 | static SkFlattenable* load_flattenable(const SkDescriptor* desc, uint32_t tag) { |
| 63 | SkFlattenable* obj = NULL; |
| 64 | uint32_t len; |
| 65 | const void* data = desc->findEntry(tag, &len); |
| 66 | |
| 67 | if (data) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 68 | SkOrderedReadBuffer buffer(data, len); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | obj = buffer.readFlattenable(); |
| 70 | SkASSERT(buffer.offset() == buffer.size()); |
| 71 | } |
| 72 | return obj; |
| 73 | } |
| 74 | |
| 75 | SkScalerContext::SkScalerContext(const SkDescriptor* desc) |
| 76 | : fPathEffect(NULL), fMaskFilter(NULL) |
| 77 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | fBaseGlyphCount = 0; |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 79 | fNextContext = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | |
| 81 | const Rec* rec = (const Rec*)desc->findEntry(kRec_SkDescriptorTag, NULL); |
| 82 | SkASSERT(rec); |
| 83 | |
| 84 | fRec = *rec; |
| 85 | |
| 86 | #ifdef DUMP_REC |
| 87 | desc->assertChecksum(); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 88 | SkDebugf("SkScalarContext checksum %x count %d length %d\n", |
| 89 | desc->getChecksum(), desc->getCount(), desc->getLength()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n", |
| 91 | rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0], |
| 92 | rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]); |
| 93 | SkDebugf(" frame %g miter %g hints %d framefill %d format %d join %d\n", |
| 94 | rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill, |
| 95 | rec->fMaskFormat, rec->fStrokeJoin); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 96 | SkDebugf(" pathEffect %x maskFilter %x\n", |
| 97 | desc->findEntry(kPathEffect_SkDescriptorTag, NULL), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | desc->findEntry(kMaskFilter_SkDescriptorTag, NULL)); |
| 99 | #endif |
| 100 | |
| 101 | fPathEffect = (SkPathEffect*)load_flattenable(desc, kPathEffect_SkDescriptorTag); |
| 102 | fMaskFilter = (SkMaskFilter*)load_flattenable(desc, kMaskFilter_SkDescriptorTag); |
| 103 | fRasterizer = (SkRasterizer*)load_flattenable(desc, kRasterizer_SkDescriptorTag); |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 104 | |
| 105 | // initialize based on our settings. subclasses can also force this |
| 106 | fGenerateImageFromPath = fRec.fFrameWidth > 0 || fPathEffect != NULL || |
| 107 | fRasterizer != NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | SkScalerContext::~SkScalerContext() { |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 111 | SkDELETE(fNextContext); |
| 112 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 113 | SkSafeUnref(fPathEffect); |
| 114 | SkSafeUnref(fMaskFilter); |
| 115 | SkSafeUnref(fRasterizer); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | } |
| 117 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 118 | static SkScalerContext* allocNextContext(const SkScalerContext::Rec& rec) { |
| 119 | // fonthost will determine the next possible font to search, based |
| 120 | // on the current font in fRec. It will return NULL if ctx is our |
| 121 | // last font that can be searched (i.e. ultimate fallback font) |
reed@google.com | 7d26c59 | 2011-06-13 13:01:10 +0000 | [diff] [blame] | 122 | uint32_t newFontID = SkFontHost::NextLogicalFont(rec.fFontID, rec.fOrigFontID); |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 123 | if (0 == newFontID) { |
| 124 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | } |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 126 | |
| 127 | SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); |
| 128 | SkDescriptor* desc = ad.getDesc(); |
| 129 | |
| 130 | desc->init(); |
| 131 | SkScalerContext::Rec* newRec = |
| 132 | (SkScalerContext::Rec*)desc->addEntry(kRec_SkDescriptorTag, |
| 133 | sizeof(rec), &rec); |
| 134 | newRec->fFontID = newFontID; |
| 135 | desc->computeChecksum(); |
| 136 | |
| 137 | return SkFontHost::CreateScalerContext(desc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 140 | /* Return the next context, creating it if its not already created, but return |
| 141 | NULL if the fonthost says there are no more fonts to fallback to. |
| 142 | */ |
| 143 | SkScalerContext* SkScalerContext::getNextContext() { |
| 144 | SkScalerContext* next = fNextContext; |
| 145 | // if next is null, then either it isn't cached yet, or we're at the |
| 146 | // end of our possible chain |
| 147 | if (NULL == next) { |
| 148 | next = allocNextContext(fRec); |
| 149 | if (NULL == next) { |
| 150 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 152 | // next's base is our base + our local count |
| 153 | next->setBaseGlyphCount(fBaseGlyphCount + this->getGlyphCount()); |
| 154 | // cache the answer |
| 155 | fNextContext = next; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | } |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 157 | return next; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 160 | SkScalerContext* SkScalerContext::getGlyphContext(const SkGlyph& glyph) { |
| 161 | unsigned glyphID = glyph.getGlyphID(); |
| 162 | SkScalerContext* ctx = this; |
| 163 | for (;;) { |
| 164 | unsigned count = ctx->getGlyphCount(); |
| 165 | if (glyphID < count) { |
| 166 | break; |
| 167 | } |
| 168 | glyphID -= count; |
| 169 | ctx = ctx->getNextContext(); |
| 170 | if (NULL == ctx) { |
| 171 | SkDebugf("--- no context for glyph %x\n", glyph.getGlyphID()); |
| 172 | // just return the original context (this) |
| 173 | return this; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return ctx; |
| 177 | } |
| 178 | |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 179 | #ifdef SK_BUILD_FOR_ANDROID |
| 180 | /* This loops through all available fallback contexts (if needed) until it |
| 181 | finds some context that can handle the unichar and return it. |
| 182 | |
| 183 | As this is somewhat expensive operation, it should only be done on the first |
| 184 | char of a run. |
| 185 | */ |
| 186 | unsigned SkScalerContext::getBaseGlyphCount(SkUnichar uni) { |
| 187 | SkScalerContext* ctx = this; |
| 188 | unsigned glyphID; |
| 189 | for (;;) { |
| 190 | glyphID = ctx->generateCharToGlyph(uni); |
| 191 | if (glyphID) { |
| 192 | break; // found it |
| 193 | } |
| 194 | ctx = ctx->getNextContext(); |
| 195 | if (NULL == ctx) { |
| 196 | SkDebugf("--- no context for char %x\n", uni); |
| 197 | // just return the original context (this) |
| 198 | return this->fBaseGlyphCount; |
| 199 | } |
| 200 | } |
| 201 | return ctx->fBaseGlyphCount; |
| 202 | } |
| 203 | #endif |
| 204 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 205 | /* This loops through all available fallback contexts (if needed) until it |
| 206 | finds some context that can handle the unichar. If all fail, returns 0 |
| 207 | */ |
| 208 | uint16_t SkScalerContext::charToGlyphID(SkUnichar uni) { |
| 209 | SkScalerContext* ctx = this; |
| 210 | unsigned glyphID; |
| 211 | for (;;) { |
| 212 | glyphID = ctx->generateCharToGlyph(uni); |
| 213 | if (glyphID) { |
| 214 | break; // found it |
| 215 | } |
| 216 | ctx = ctx->getNextContext(); |
| 217 | if (NULL == ctx) { |
| 218 | return 0; // no more contexts, return missing glyph |
| 219 | } |
| 220 | } |
| 221 | // add the ctx's base, making glyphID unique for chain of contexts |
| 222 | glyphID += ctx->fBaseGlyphCount; |
| 223 | // check for overflow of 16bits, since our glyphID cannot exceed that |
| 224 | if (glyphID > 0xFFFF) { |
| 225 | glyphID = 0; |
| 226 | } |
| 227 | return SkToU16(glyphID); |
| 228 | } |
| 229 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 230 | SkUnichar SkScalerContext::glyphIDToChar(uint16_t glyphID) { |
| 231 | SkScalerContext* ctx = this; |
| 232 | unsigned rangeEnd = 0; |
| 233 | do { |
| 234 | unsigned rangeStart = rangeEnd; |
| 235 | |
| 236 | rangeEnd += ctx->getGlyphCount(); |
| 237 | if (rangeStart <= glyphID && glyphID < rangeEnd) { |
| 238 | return ctx->generateGlyphToChar(glyphID - rangeStart); |
| 239 | } |
| 240 | ctx = ctx->getNextContext(); |
| 241 | } while (NULL != ctx); |
| 242 | return 0; |
| 243 | } |
| 244 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | void SkScalerContext::getAdvance(SkGlyph* glyph) { |
| 246 | // mark us as just having a valid advance |
| 247 | glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE; |
| 248 | // we mark the format before making the call, in case the impl |
| 249 | // internally ends up calling its generateMetrics, which is OK |
| 250 | // albeit slower than strictly necessary |
| 251 | this->getGlyphContext(*glyph)->generateAdvance(glyph); |
| 252 | } |
| 253 | |
| 254 | void SkScalerContext::getMetrics(SkGlyph* glyph) { |
| 255 | this->getGlyphContext(*glyph)->generateMetrics(glyph); |
| 256 | |
| 257 | // for now we have separate cache entries for devkerning on and off |
| 258 | // in the future we might share caches, but make our measure/draw |
| 259 | // code make the distinction. Thus we zap the values if the caller |
| 260 | // has not asked for them. |
| 261 | if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) { |
| 262 | // no devkern, so zap the fields |
| 263 | glyph->fLsbDelta = glyph->fRsbDelta = 0; |
| 264 | } |
| 265 | |
| 266 | // if either dimension is empty, zap the image bounds of the glyph |
| 267 | if (0 == glyph->fWidth || 0 == glyph->fHeight) { |
| 268 | glyph->fWidth = 0; |
| 269 | glyph->fHeight = 0; |
| 270 | glyph->fTop = 0; |
| 271 | glyph->fLeft = 0; |
| 272 | glyph->fMaskFormat = 0; |
| 273 | return; |
| 274 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 275 | |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 276 | if (fGenerateImageFromPath) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | SkPath devPath, fillPath; |
| 278 | SkMatrix fillToDevMatrix; |
| 279 | |
| 280 | this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); |
| 281 | |
| 282 | if (fRasterizer) { |
| 283 | SkMask mask; |
| 284 | |
| 285 | if (fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, |
| 286 | fMaskFilter, &mask, |
| 287 | SkMask::kJustComputeBounds_CreateMode)) { |
| 288 | glyph->fLeft = mask.fBounds.fLeft; |
| 289 | glyph->fTop = mask.fBounds.fTop; |
| 290 | glyph->fWidth = SkToU16(mask.fBounds.width()); |
| 291 | glyph->fHeight = SkToU16(mask.fBounds.height()); |
| 292 | } else { |
senorblanco@chromium.org | 7767cd8 | 2009-11-25 17:14:32 +0000 | [diff] [blame] | 293 | goto SK_ERROR; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | } |
| 295 | } else { |
| 296 | // just use devPath |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | SkIRect ir; |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 298 | devPath.getBounds().roundOut(&ir); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 299 | |
reed@android.com | b757f8d | 2009-12-08 22:02:26 +0000 | [diff] [blame] | 300 | if (ir.isEmpty() || !ir.is16Bit()) { |
senorblanco@chromium.org | 7767cd8 | 2009-11-25 17:14:32 +0000 | [diff] [blame] | 301 | goto SK_ERROR; |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 302 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | glyph->fLeft = ir.fLeft; |
| 304 | glyph->fTop = ir.fTop; |
| 305 | glyph->fWidth = SkToU16(ir.width()); |
| 306 | glyph->fHeight = SkToU16(ir.height()); |
| 307 | } |
| 308 | } |
| 309 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 310 | if (SkMask::kARGB32_Format != glyph->fMaskFormat) { |
| 311 | glyph->fMaskFormat = fRec.fMaskFormat; |
| 312 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 313 | |
| 314 | if (fMaskFilter) { |
| 315 | SkMask src, dst; |
| 316 | SkMatrix matrix; |
| 317 | |
| 318 | glyph->toMask(&src); |
| 319 | fRec.getMatrixFrom2x2(&matrix); |
| 320 | |
| 321 | src.fImage = NULL; // only want the bounds from the filter |
| 322 | if (fMaskFilter->filterMask(&dst, src, matrix, NULL)) { |
| 323 | SkASSERT(dst.fImage == NULL); |
| 324 | glyph->fLeft = dst.fBounds.fLeft; |
| 325 | glyph->fTop = dst.fBounds.fTop; |
| 326 | glyph->fWidth = SkToU16(dst.fBounds.width()); |
| 327 | glyph->fHeight = SkToU16(dst.fBounds.height()); |
| 328 | glyph->fMaskFormat = dst.fFormat; |
| 329 | } |
| 330 | } |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 331 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 332 | |
senorblanco@chromium.org | 7767cd8 | 2009-11-25 17:14:32 +0000 | [diff] [blame] | 333 | SK_ERROR: |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 334 | // draw nothing 'cause we failed |
| 335 | glyph->fLeft = 0; |
| 336 | glyph->fTop = 0; |
| 337 | glyph->fWidth = 0; |
| 338 | glyph->fHeight = 0; |
reed@android.com | b757f8d | 2009-12-08 22:02:26 +0000 | [diff] [blame] | 339 | // put a valid value here, in case it was earlier set to |
| 340 | // MASK_FORMAT_JUST_ADVANCE |
| 341 | glyph->fMaskFormat = fRec.fMaskFormat; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | } |
| 343 | |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 344 | static bool isLCD(const SkScalerContext::Rec& rec) { |
| 345 | return SkMask::kLCD16_Format == rec.fMaskFormat || |
| 346 | SkMask::kLCD32_Format == rec.fMaskFormat; |
| 347 | } |
| 348 | |
| 349 | static uint16_t a8_to_rgb565(unsigned a8) { |
| 350 | return SkPackRGB16(a8 >> 3, a8 >> 2, a8 >> 3); |
| 351 | } |
| 352 | |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 353 | static void copyToLCD16(const SkBitmap& src, const SkMask& dst) { |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 354 | SkASSERT(SkBitmap::kA8_Config == src.config()); |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 355 | SkASSERT(SkMask::kLCD16_Format == dst.fFormat); |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 356 | |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 357 | const int width = dst.fBounds.width(); |
| 358 | const int height = dst.fBounds.height(); |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 359 | const uint8_t* srcP = src.getAddr8(0, 0); |
| 360 | size_t srcRB = src.rowBytes(); |
| 361 | uint16_t* dstP = (uint16_t*)dst.fImage; |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 362 | size_t dstRB = dst.fRowBytes; |
| 363 | for (int y = 0; y < height; ++y) { |
| 364 | for (int x = 0; x < width; ++x) { |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 365 | dstP[x] = a8_to_rgb565(srcP[x]); |
| 366 | } |
| 367 | srcP += srcRB; |
| 368 | dstP = (uint16_t*)((char*)dstP + dstRB); |
| 369 | } |
| 370 | } |
| 371 | |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 372 | #define SK_FREETYPE_LCD_LERP 160 |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 373 | |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 374 | static int lerp(int start, int end) { |
| 375 | SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256); |
| 376 | return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8); |
| 377 | } |
| 378 | |
| 379 | static uint16_t packLCD16(unsigned r, unsigned g, unsigned b) { |
| 380 | if (SK_FREETYPE_LCD_LERP) { |
| 381 | // want (a+b+c)/3, but we approx to avoid the divide |
| 382 | unsigned ave = (5 * (r + g + b) + g) >> 4; |
| 383 | r = lerp(r, ave); |
| 384 | g = lerp(g, ave); |
| 385 | b = lerp(b, ave); |
| 386 | } |
| 387 | return SkPackRGB16(r >> 3, g >> 2, b >> 3); |
| 388 | } |
| 389 | |
| 390 | static void pack3xHToLCD16(const SkBitmap& src, const SkMask& dst) { |
| 391 | SkASSERT(SkBitmap::kA8_Config == src.config()); |
| 392 | SkASSERT(SkMask::kLCD16_Format == dst.fFormat); |
| 393 | |
| 394 | const int width = dst.fBounds.width(); |
| 395 | const int height = dst.fBounds.height(); |
| 396 | uint16_t* dstP = (uint16_t*)dst.fImage; |
| 397 | size_t dstRB = dst.fRowBytes; |
| 398 | for (int y = 0; y < height; ++y) { |
| 399 | const uint8_t* srcP = src.getAddr8(0, y); |
| 400 | for (int x = 0; x < width; ++x) { |
| 401 | unsigned r = *srcP++; |
| 402 | unsigned g = *srcP++; |
| 403 | unsigned b = *srcP++; |
| 404 | dstP[x] = packLCD16(r, g, b); |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 405 | } |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 406 | dstP = (uint16_t*)((char*)dstP + dstRB); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | static void pack3xHToLCD32(const SkBitmap& src, const SkMask& dst) { |
| 411 | SkASSERT(SkBitmap::kA8_Config == src.config()); |
| 412 | SkASSERT(SkMask::kLCD32_Format == dst.fFormat); |
| 413 | |
| 414 | const int width = dst.fBounds.width(); |
| 415 | const int height = dst.fBounds.height(); |
| 416 | SkPMColor* dstP = (SkPMColor*)dst.fImage; |
| 417 | size_t dstRB = dst.fRowBytes; |
| 418 | for (int y = 0; y < height; ++y) { |
| 419 | const uint8_t* srcP = src.getAddr8(0, y); |
| 420 | for (int x = 0; x < width; ++x) { |
| 421 | unsigned r = *srcP++; |
| 422 | unsigned g = *srcP++; |
| 423 | unsigned b = *srcP++; |
| 424 | unsigned a = SkMax32(SkMax32(r, g), b); |
| 425 | dstP[x] = SkPackARGB32(a, r, g, b); |
| 426 | } |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 427 | dstP = (SkPMColor*)((char*)dstP + dstRB); |
| 428 | } |
| 429 | } |
| 430 | |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 431 | static void generateMask(const SkMask& mask, const SkPath& path) { |
| 432 | SkBitmap::Config config; |
| 433 | SkPaint paint; |
| 434 | |
| 435 | int srcW = mask.fBounds.width(); |
| 436 | int srcH = mask.fBounds.height(); |
| 437 | int dstW = srcW; |
| 438 | int dstH = srcH; |
| 439 | int dstRB = mask.fRowBytes; |
| 440 | |
| 441 | SkMatrix matrix; |
| 442 | matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), |
| 443 | -SkIntToScalar(mask.fBounds.fTop)); |
| 444 | |
| 445 | if (SkMask::kBW_Format == mask.fFormat) { |
| 446 | config = SkBitmap::kA1_Config; |
| 447 | paint.setAntiAlias(false); |
| 448 | } else { |
| 449 | config = SkBitmap::kA8_Config; |
| 450 | paint.setAntiAlias(true); |
| 451 | switch (mask.fFormat) { |
| 452 | case SkMask::kA8_Format: |
| 453 | break; |
| 454 | case SkMask::kLCD16_Format: |
| 455 | case SkMask::kLCD32_Format: |
| 456 | // TODO: trigger off LCD orientation |
| 457 | dstW *= 3; |
| 458 | matrix.postScale(SkIntToScalar(3), SK_Scalar1); |
| 459 | dstRB = 0; // signals we need a copy |
| 460 | break; |
| 461 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 462 | SkDEBUGFAIL("unexpected mask format"); |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 466 | SkRasterClip clip; |
| 467 | clip.setRect(SkIRect::MakeWH(dstW, dstH)); |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 468 | |
| 469 | SkBitmap bm; |
| 470 | bm.setConfig(config, dstW, dstH, dstRB); |
| 471 | |
| 472 | if (0 == dstRB) { |
| 473 | bm.allocPixels(); |
| 474 | bm.lockPixels(); |
| 475 | } else { |
| 476 | bm.setPixels(mask.fImage); |
| 477 | } |
| 478 | sk_bzero(bm.getPixels(), bm.getSafeSize()); |
| 479 | |
| 480 | SkDraw draw; |
| 481 | sk_bzero(&draw, sizeof(draw)); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 482 | draw.fRC = &clip; |
| 483 | draw.fClip = &clip.bwRgn(); |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 484 | draw.fMatrix = &matrix; |
| 485 | draw.fBitmap = &bm; |
| 486 | draw.drawPath(path, paint); |
| 487 | |
| 488 | if (0 == dstRB) { |
| 489 | switch (mask.fFormat) { |
| 490 | case SkMask::kLCD16_Format: |
| 491 | pack3xHToLCD16(bm, mask); |
| 492 | break; |
| 493 | case SkMask::kLCD32_Format: |
| 494 | pack3xHToLCD32(bm, mask); |
| 495 | break; |
| 496 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 497 | SkDEBUGFAIL("bad format for copyback"); |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 502 | void SkScalerContext::getImage(const SkGlyph& origGlyph) { |
| 503 | const SkGlyph* glyph = &origGlyph; |
| 504 | SkGlyph tmpGlyph; |
| 505 | |
| 506 | if (fMaskFilter) { // restore the prefilter bounds |
reed@google.com | ce2b1af | 2011-02-18 13:00:40 +0000 | [diff] [blame] | 507 | tmpGlyph.init(origGlyph.fID); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | |
| 509 | // need the original bounds, sans our maskfilter |
| 510 | SkMaskFilter* mf = fMaskFilter; |
| 511 | fMaskFilter = NULL; // temp disable |
| 512 | this->getMetrics(&tmpGlyph); |
| 513 | fMaskFilter = mf; // restore |
| 514 | |
| 515 | tmpGlyph.fImage = origGlyph.fImage; |
| 516 | |
| 517 | // we need the prefilter bounds to be <= filter bounds |
| 518 | SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth); |
| 519 | SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight); |
| 520 | glyph = &tmpGlyph; |
| 521 | } |
| 522 | |
reed@google.com | a767fa0 | 2011-08-05 21:40:26 +0000 | [diff] [blame] | 523 | if (fGenerateImageFromPath) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | SkPath devPath, fillPath; |
| 525 | SkMatrix fillToDevMatrix; |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 526 | SkMask mask; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 527 | |
| 528 | this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 529 | glyph->toMask(&mask); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | |
| 531 | if (fRasterizer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 532 | mask.fFormat = SkMask::kA8_Format; |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 533 | sk_bzero(glyph->fImage, mask.computeImageSize()); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 534 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, |
| 536 | fMaskFilter, &mask, |
| 537 | SkMask::kJustRenderImage_CreateMode)) { |
| 538 | return; |
| 539 | } |
| 540 | } else { |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 541 | generateMask(mask, devPath); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | } |
| 543 | } else { |
| 544 | this->getGlyphContext(*glyph)->generateImage(*glyph); |
| 545 | } |
| 546 | |
| 547 | if (fMaskFilter) { |
| 548 | SkMask srcM, dstM; |
| 549 | SkMatrix matrix; |
| 550 | |
| 551 | // the src glyph image shouldn't be 3D |
| 552 | SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat); |
| 553 | glyph->toMask(&srcM); |
| 554 | fRec.getMatrixFrom2x2(&matrix); |
| 555 | |
| 556 | if (fMaskFilter->filterMask(&dstM, srcM, matrix, NULL)) { |
| 557 | int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width()); |
| 558 | int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height()); |
| 559 | int dstRB = origGlyph.rowBytes(); |
| 560 | int srcRB = dstM.fRowBytes; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 561 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 562 | const uint8_t* src = (const uint8_t*)dstM.fImage; |
| 563 | uint8_t* dst = (uint8_t*)origGlyph.fImage; |
| 564 | |
| 565 | if (SkMask::k3D_Format == dstM.fFormat) { |
| 566 | // we have to copy 3 times as much |
| 567 | height *= 3; |
| 568 | } |
| 569 | |
| 570 | // clean out our glyph, since it may be larger than dstM |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 571 | //sk_bzero(dst, height * dstRB); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 572 | |
| 573 | while (--height >= 0) { |
| 574 | memcpy(dst, src, width); |
| 575 | src += srcRB; |
| 576 | dst += dstRB; |
| 577 | } |
| 578 | SkMask::FreeImage(dstM.fImage); |
| 579 | } |
| 580 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 581 | } |
| 582 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 583 | void SkScalerContext::getPath(const SkGlyph& glyph, SkPath* path) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 584 | this->internalGetPath(glyph, NULL, path, NULL); |
| 585 | } |
| 586 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 587 | void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* mx, |
| 588 | SkPaint::FontMetrics* my) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 589 | this->generateFontMetrics(mx, my); |
| 590 | } |
| 591 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 592 | SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) { |
| 593 | return 0; |
| 594 | } |
| 595 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 596 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 597 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 598 | void SkScalerContext::internalGetPath(const SkGlyph& glyph, SkPath* fillPath, |
| 599 | SkPath* devPath, SkMatrix* fillToDevMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 600 | SkPath path; |
| 601 | |
| 602 | this->getGlyphContext(glyph)->generatePath(glyph, &path); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 603 | |
reed@google.com | cf598b1 | 2011-08-22 21:13:23 +0000 | [diff] [blame] | 604 | if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { |
| 605 | SkFixed dx = glyph.getSubXFixed(); |
| 606 | SkFixed dy = glyph.getSubYFixed(); |
| 607 | if (dx | dy) { |
| 608 | path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy)); |
| 609 | } |
| 610 | } |
| 611 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 612 | if (fRec.fFrameWidth > 0 || fPathEffect != NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 613 | // need the path in user-space, with only the point-size applied |
| 614 | // so that our stroking and effects will operate the same way they |
| 615 | // would if the user had extracted the path themself, and then |
| 616 | // called drawPath |
| 617 | SkPath localPath; |
| 618 | SkMatrix matrix, inverse; |
| 619 | |
| 620 | fRec.getMatrixFrom2x2(&matrix); |
bungeman@google.com | 9575fb8 | 2012-04-09 20:49:03 +0000 | [diff] [blame] | 621 | if (!matrix.invert(&inverse)) { |
| 622 | // assume fillPath and devPath are already empty. |
| 623 | return; |
| 624 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 625 | path.transform(inverse, &localPath); |
| 626 | // now localPath is only affected by the paint settings, and not the canvas matrix |
| 627 | |
reed@google.com | 9797272 | 2012-05-24 20:13:57 +0000 | [diff] [blame^] | 628 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 629 | |
| 630 | if (fRec.fFrameWidth > 0) { |
| 631 | rec.setStrokeStyle(fRec.fFrameWidth, |
| 632 | SkToBool(fRec.fFlags & kFrameAndFill_Flag)); |
| 633 | // glyphs are always closed contours, so cap type is ignored, |
| 634 | // so we just pass something. |
| 635 | rec.setStrokeParams(SkPaint::kButt_Cap, |
| 636 | (SkPaint::Join)fRec.fStrokeJoin, |
| 637 | fRec.fMiterLimit); |
| 638 | } |
| 639 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 640 | if (fPathEffect) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 641 | SkPath effectPath; |
reed@google.com | 9797272 | 2012-05-24 20:13:57 +0000 | [diff] [blame^] | 642 | if (fPathEffect->filterPath(&effectPath, localPath, &rec)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 643 | localPath.swap(effectPath); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 644 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 645 | } |
| 646 | |
reed@google.com | 9797272 | 2012-05-24 20:13:57 +0000 | [diff] [blame^] | 647 | if (rec.needToApply()) { |
| 648 | SkPath strokePath; |
| 649 | if (rec.applyToPath(&strokePath, localPath)) { |
| 650 | localPath.swap(strokePath); |
| 651 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 652 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 653 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 654 | // now return stuff to the caller |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 655 | if (fillToDevMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 656 | *fillToDevMatrix = matrix; |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 657 | } |
| 658 | if (devPath) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 659 | localPath.transform(matrix, devPath); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 660 | } |
| 661 | if (fillPath) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 662 | fillPath->swap(localPath); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 663 | } |
| 664 | } else { // nothing tricky to do |
| 665 | if (fillToDevMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 666 | fillToDevMatrix->reset(); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 667 | } |
| 668 | if (devPath) { |
| 669 | if (fillPath == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 670 | devPath->swap(path); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 671 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 672 | *devPath = path; |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 673 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 674 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 675 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 676 | if (fillPath) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 677 | fillPath->swap(path); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 678 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 679 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 680 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 681 | if (devPath) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 682 | devPath->updateBoundsCache(); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 683 | } |
| 684 | if (fillPath) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 685 | fillPath->updateBoundsCache(); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 686 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 690 | void SkScalerContext::Rec::getMatrixFrom2x2(SkMatrix* dst) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 691 | dst->reset(); |
| 692 | dst->setScaleX(fPost2x2[0][0]); |
| 693 | dst->setSkewX( fPost2x2[0][1]); |
| 694 | dst->setSkewY( fPost2x2[1][0]); |
| 695 | dst->setScaleY(fPost2x2[1][1]); |
| 696 | } |
| 697 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 698 | void SkScalerContext::Rec::getLocalMatrix(SkMatrix* m) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 699 | m->setScale(SkScalarMul(fTextSize, fPreScaleX), fTextSize); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 700 | if (fPreSkewX) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 701 | m->postSkew(fPreSkewX, 0); |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 702 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 703 | } |
| 704 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 705 | void SkScalerContext::Rec::getSingleMatrix(SkMatrix* m) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 706 | this->getLocalMatrix(m); |
| 707 | |
| 708 | // now concat the device matrix |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 709 | SkMatrix deviceMatrix; |
| 710 | this->getMatrixFrom2x2(&deviceMatrix); |
| 711 | m->postConcat(deviceMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 712 | } |
| 713 | |
reed@google.com | 2e68478 | 2011-08-24 15:38:46 +0000 | [diff] [blame] | 714 | SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix) { |
| 715 | SkASSERT(!matrix.hasPerspective()); |
| 716 | |
| 717 | if (0 == matrix[SkMatrix::kMSkewY]) { |
| 718 | return kX_SkAxisAlignment; |
| 719 | } |
| 720 | if (0 == matrix[SkMatrix::kMScaleX]) { |
| 721 | return kY_SkAxisAlignment; |
| 722 | } |
| 723 | return kNone_SkAxisAlignment; |
| 724 | } |
| 725 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 726 | /////////////////////////////////////////////////////////////////////////////// |
| 727 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 728 | #include "SkFontHost.h" |
| 729 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 730 | class SkScalerContext_Empty : public SkScalerContext { |
| 731 | public: |
| 732 | SkScalerContext_Empty(const SkDescriptor* desc) : SkScalerContext(desc) {} |
| 733 | |
| 734 | protected: |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 735 | virtual unsigned generateGlyphCount() { |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | virtual uint16_t generateCharToGlyph(SkUnichar uni) { |
| 739 | return 0; |
| 740 | } |
| 741 | virtual void generateAdvance(SkGlyph* glyph) { |
| 742 | glyph->zeroMetrics(); |
| 743 | } |
| 744 | virtual void generateMetrics(SkGlyph* glyph) { |
| 745 | glyph->zeroMetrics(); |
| 746 | } |
| 747 | virtual void generateImage(const SkGlyph& glyph) {} |
| 748 | virtual void generatePath(const SkGlyph& glyph, SkPath* path) {} |
| 749 | virtual void generateFontMetrics(SkPaint::FontMetrics* mx, |
| 750 | SkPaint::FontMetrics* my) { |
| 751 | if (mx) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 752 | sk_bzero(mx, sizeof(*mx)); |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 753 | } |
| 754 | if (my) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 755 | sk_bzero(my, sizeof(*my)); |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | }; |
| 759 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 760 | extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc); |
| 761 | |
reed@google.com | 7db9fe6 | 2011-04-11 11:57:54 +0000 | [diff] [blame] | 762 | SkScalerContext* SkScalerContext::Create(const SkDescriptor* desc) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 763 | SkScalerContext* c = NULL; //SkCreateColorScalerContext(desc); |
| 764 | if (NULL == c) { |
| 765 | c = SkFontHost::CreateScalerContext(desc); |
| 766 | } |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 767 | if (NULL == c) { |
| 768 | c = SkNEW_ARGS(SkScalerContext_Empty, (desc)); |
| 769 | } |
| 770 | return c; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 771 | } |
| 772 | |