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