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 | #include "SkPaint.h" |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 10 | #include "SkAnnotation.h" |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 11 | #include "SkAutoKern.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 13 | #include "SkData.h" |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 14 | #include "SkDeviceProperties.h" |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 15 | #include "SkFontDescriptor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkFontHost.h" |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 17 | #include "SkGlyphCache.h" |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 18 | #include "SkImageFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkMaskFilter.h" |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 20 | #include "SkMaskGamma.h" |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 21 | #include "SkOrderedReadBuffer.h" |
| 22 | #include "SkOrderedWriteBuffer.h" |
| 23 | #include "SkPaintDefaults.h" |
commit-bot@chromium.org | 97f8167 | 2013-09-26 15:16:12 +0000 | [diff] [blame] | 24 | #include "SkPaintOptionsAndroid.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkPathEffect.h" |
| 26 | #include "SkRasterizer.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 27 | #include "SkScalar.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | #include "SkScalerContext.h" |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 29 | #include "SkShader.h" |
| 30 | #include "SkStringUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | #include "SkStroke.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 32 | #include "SkTextFormatParams.h" |
reed@google.com | e691376 | 2012-08-07 15:19:47 +0000 | [diff] [blame] | 33 | #include "SkTextToPathIter.h" |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 34 | #include "SkTLazy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | #include "SkTypeface.h" |
| 36 | #include "SkXfermode.h" |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 37 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 39 | // define this to get a printf for out-of-range parameter in setters |
| 40 | // e.g. setTextSize(-1) |
| 41 | //#define SK_REPORT_API_RANGE_CHECK |
| 42 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 43 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 44 | #define GEN_ID_INC fGenerationID++ |
| 45 | #define GEN_ID_INC_EVAL(expression) if (expression) { fGenerationID++; } |
| 46 | #else |
| 47 | #define GEN_ID_INC |
| 48 | #define GEN_ID_INC_EVAL(expression) |
| 49 | #endif |
| 50 | |
reed@android.com | a3122b9 | 2009-08-13 20:38:25 +0000 | [diff] [blame] | 51 | SkPaint::SkPaint() { |
| 52 | // since we may have padding, we zero everything so that our memcmp() call |
| 53 | // in operator== will work correctly. |
| 54 | // with this, we can skip 0 and null individual initializations |
| 55 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
reed@android.com | a3122b9 | 2009-08-13 20:38:25 +0000 | [diff] [blame] | 57 | #if 0 // not needed with the bzero call above |
| 58 | fTypeface = NULL; |
| 59 | fTextSkewX = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | fPathEffect = NULL; |
| 61 | fShader = NULL; |
| 62 | fXfermode = NULL; |
| 63 | fMaskFilter = NULL; |
| 64 | fColorFilter = NULL; |
| 65 | fRasterizer = NULL; |
| 66 | fLooper = NULL; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 67 | fImageFilter = NULL; |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 68 | fAnnotation = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | fWidth = 0; |
reed@android.com | a3122b9 | 2009-08-13 20:38:25 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
reed@google.com | aefdd06 | 2012-02-29 13:03:00 +0000 | [diff] [blame] | 72 | fTextSize = SkPaintDefaults_TextSize; |
reed@android.com | a3122b9 | 2009-08-13 20:38:25 +0000 | [diff] [blame] | 73 | fTextScaleX = SK_Scalar1; |
| 74 | fColor = SK_ColorBLACK; |
reed@google.com | aefdd06 | 2012-02-29 13:03:00 +0000 | [diff] [blame] | 75 | fMiterLimit = SkPaintDefaults_MiterLimit; |
| 76 | fFlags = SkPaintDefaults_Flags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | fCapType = kDefault_Cap; |
| 78 | fJoinType = kDefault_Join; |
| 79 | fTextAlign = kLeft_Align; |
| 80 | fStyle = kFill_Style; |
| 81 | fTextEncoding = kUTF8_TextEncoding; |
reed@google.com | aefdd06 | 2012-02-29 13:03:00 +0000 | [diff] [blame] | 82 | fHinting = SkPaintDefaults_Hinting; |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 83 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | c7a20e4 | 2013-05-13 14:09:13 +0000 | [diff] [blame] | 84 | new (&fPaintOptionsAndroid) SkPaintOptionsAndroid; |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 85 | fGenerationID = 0; |
| 86 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | } |
| 88 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 89 | SkPaint::SkPaint(const SkPaint& src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | memcpy(this, &src, sizeof(src)); |
| 91 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 92 | SkSafeRef(fTypeface); |
| 93 | SkSafeRef(fPathEffect); |
| 94 | SkSafeRef(fShader); |
| 95 | SkSafeRef(fXfermode); |
| 96 | SkSafeRef(fMaskFilter); |
| 97 | SkSafeRef(fColorFilter); |
| 98 | SkSafeRef(fRasterizer); |
| 99 | SkSafeRef(fLooper); |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 100 | SkSafeRef(fImageFilter); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 101 | SkSafeRef(fAnnotation); |
djsollen@google.com | 40078cb | 2013-05-24 20:31:57 +0000 | [diff] [blame] | 102 | |
| 103 | #ifdef SK_BUILD_FOR_ANDROID |
| 104 | new (&fPaintOptionsAndroid) SkPaintOptionsAndroid(src.fPaintOptionsAndroid); |
| 105 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 108 | SkPaint::~SkPaint() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 109 | SkSafeUnref(fTypeface); |
| 110 | SkSafeUnref(fPathEffect); |
| 111 | SkSafeUnref(fShader); |
| 112 | SkSafeUnref(fXfermode); |
| 113 | SkSafeUnref(fMaskFilter); |
| 114 | SkSafeUnref(fColorFilter); |
| 115 | SkSafeUnref(fRasterizer); |
| 116 | SkSafeUnref(fLooper); |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 117 | SkSafeUnref(fImageFilter); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 118 | SkSafeUnref(fAnnotation); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 121 | SkPaint& SkPaint::operator=(const SkPaint& src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | SkASSERT(&src); |
| 123 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 124 | SkSafeRef(src.fTypeface); |
| 125 | SkSafeRef(src.fPathEffect); |
| 126 | SkSafeRef(src.fShader); |
| 127 | SkSafeRef(src.fXfermode); |
| 128 | SkSafeRef(src.fMaskFilter); |
| 129 | SkSafeRef(src.fColorFilter); |
| 130 | SkSafeRef(src.fRasterizer); |
| 131 | SkSafeRef(src.fLooper); |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 132 | SkSafeRef(src.fImageFilter); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 133 | SkSafeRef(src.fAnnotation); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 135 | SkSafeUnref(fTypeface); |
| 136 | SkSafeUnref(fPathEffect); |
| 137 | SkSafeUnref(fShader); |
| 138 | SkSafeUnref(fXfermode); |
| 139 | SkSafeUnref(fMaskFilter); |
| 140 | SkSafeUnref(fColorFilter); |
| 141 | SkSafeUnref(fRasterizer); |
| 142 | SkSafeUnref(fLooper); |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 143 | SkSafeUnref(fImageFilter); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 144 | SkSafeUnref(fAnnotation); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 146 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | c7a20e4 | 2013-05-13 14:09:13 +0000 | [diff] [blame] | 147 | fPaintOptionsAndroid.~SkPaintOptionsAndroid(); |
| 148 | |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 149 | uint32_t oldGenerationID = fGenerationID; |
| 150 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | memcpy(this, &src, sizeof(src)); |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 152 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 153 | fGenerationID = oldGenerationID + 1; |
commit-bot@chromium.org | c7a20e4 | 2013-05-13 14:09:13 +0000 | [diff] [blame] | 154 | |
| 155 | new (&fPaintOptionsAndroid) SkPaintOptionsAndroid(src.fPaintOptionsAndroid); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 156 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | |
| 158 | return *this; |
| 159 | } |
| 160 | |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 161 | bool operator==(const SkPaint& a, const SkPaint& b) { |
| 162 | #ifdef SK_BUILD_FOR_ANDROID |
| 163 | //assumes that fGenerationID is the last field in the struct |
| 164 | return !memcmp(&a, &b, SK_OFFSETOF(SkPaint, fGenerationID)); |
| 165 | #else |
| 166 | return !memcmp(&a, &b, sizeof(a)); |
| 167 | #endif |
| 168 | } |
| 169 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 170 | void SkPaint::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | SkPaint init; |
| 172 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 173 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 174 | uint32_t oldGenerationID = fGenerationID; |
| 175 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | *this = init; |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 177 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 178 | fGenerationID = oldGenerationID + 1; |
| 179 | #endif |
| 180 | } |
| 181 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 182 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 183 | uint32_t SkPaint::getGenerationID() const { |
| 184 | return fGenerationID; |
| 185 | } |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 186 | |
| 187 | void SkPaint::setGenerationID(uint32_t generationID) { |
| 188 | fGenerationID = generationID; |
| 189 | } |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 190 | |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 191 | unsigned SkPaint::getBaseGlyphCount(SkUnichar text) const { |
bungeman@google.com | ba3284e | 2013-01-22 19:49:33 +0000 | [diff] [blame] | 192 | SkAutoGlyphCache autoCache(*this, NULL, NULL); |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 193 | SkGlyphCache* cache = autoCache.getCache(); |
| 194 | return cache->getBaseGlyphCount(text); |
| 195 | } |
commit-bot@chromium.org | c7a20e4 | 2013-05-13 14:09:13 +0000 | [diff] [blame] | 196 | |
| 197 | void SkPaint::setPaintOptionsAndroid(const SkPaintOptionsAndroid& options) { |
| 198 | if (options != fPaintOptionsAndroid) { |
| 199 | fPaintOptionsAndroid = options; |
| 200 | GEN_ID_INC; |
| 201 | } |
| 202 | } |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 203 | #endif |
| 204 | |
reed@google.com | c968315 | 2013-07-18 13:47:01 +0000 | [diff] [blame] | 205 | SkPaint::FilterLevel SkPaint::getFilterLevel() const { |
| 206 | int level = 0; |
| 207 | if (fFlags & kFilterBitmap_Flag) { |
| 208 | level |= 1; |
| 209 | } |
| 210 | if (fFlags & kHighQualityFilterBitmap_Flag) { |
| 211 | level |= 2; |
| 212 | } |
| 213 | return (FilterLevel)level; |
| 214 | } |
| 215 | |
| 216 | void SkPaint::setFilterLevel(FilterLevel level) { |
| 217 | unsigned mask = kFilterBitmap_Flag | kHighQualityFilterBitmap_Flag; |
| 218 | unsigned flags = 0; |
| 219 | if (level & 1) { |
| 220 | flags |= kFilterBitmap_Flag; |
| 221 | } |
| 222 | if (level & 2) { |
| 223 | flags |= kHighQualityFilterBitmap_Flag; |
| 224 | } |
| 225 | this->setFlags((fFlags & ~mask) | flags); |
| 226 | } |
| 227 | |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 228 | void SkPaint::setHinting(Hinting hintingLevel) { |
| 229 | GEN_ID_INC_EVAL((unsigned) hintingLevel != fHinting); |
| 230 | fHinting = hintingLevel; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | } |
| 232 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 233 | void SkPaint::setFlags(uint32_t flags) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 234 | GEN_ID_INC_EVAL(fFlags != flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | fFlags = flags; |
| 236 | } |
| 237 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 238 | void SkPaint::setAntiAlias(bool doAA) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | this->setFlags(SkSetClearMask(fFlags, doAA, kAntiAlias_Flag)); |
| 240 | } |
| 241 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 242 | void SkPaint::setDither(bool doDither) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | this->setFlags(SkSetClearMask(fFlags, doDither, kDither_Flag)); |
| 244 | } |
| 245 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 246 | void SkPaint::setSubpixelText(bool doSubpixel) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | this->setFlags(SkSetClearMask(fFlags, doSubpixel, kSubpixelText_Flag)); |
| 248 | } |
| 249 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 250 | void SkPaint::setLCDRenderText(bool doLCDRender) { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 251 | this->setFlags(SkSetClearMask(fFlags, doLCDRender, kLCDRenderText_Flag)); |
| 252 | } |
| 253 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 254 | void SkPaint::setEmbeddedBitmapText(bool doEmbeddedBitmapText) { |
agl@chromium.org | e95c91e | 2010-01-04 18:27:55 +0000 | [diff] [blame] | 255 | this->setFlags(SkSetClearMask(fFlags, doEmbeddedBitmapText, kEmbeddedBitmapText_Flag)); |
| 256 | } |
| 257 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 258 | void SkPaint::setAutohinted(bool useAutohinter) { |
agl@chromium.org | a2c71cb | 2010-06-17 20:49:17 +0000 | [diff] [blame] | 259 | this->setFlags(SkSetClearMask(fFlags, useAutohinter, kAutoHinting_Flag)); |
| 260 | } |
| 261 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 262 | void SkPaint::setLinearText(bool doLinearText) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | this->setFlags(SkSetClearMask(fFlags, doLinearText, kLinearText_Flag)); |
| 264 | } |
| 265 | |
reed@google.com | 830a23e | 2011-11-10 15:20:49 +0000 | [diff] [blame] | 266 | void SkPaint::setVerticalText(bool doVertical) { |
reed@google.com | 830a23e | 2011-11-10 15:20:49 +0000 | [diff] [blame] | 267 | this->setFlags(SkSetClearMask(fFlags, doVertical, kVerticalText_Flag)); |
| 268 | } |
| 269 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 270 | void SkPaint::setUnderlineText(bool doUnderline) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | this->setFlags(SkSetClearMask(fFlags, doUnderline, kUnderlineText_Flag)); |
| 272 | } |
| 273 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 274 | void SkPaint::setStrikeThruText(bool doStrikeThru) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 275 | this->setFlags(SkSetClearMask(fFlags, doStrikeThru, kStrikeThruText_Flag)); |
| 276 | } |
| 277 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 278 | void SkPaint::setFakeBoldText(bool doFakeBold) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | this->setFlags(SkSetClearMask(fFlags, doFakeBold, kFakeBoldText_Flag)); |
| 280 | } |
| 281 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 282 | void SkPaint::setDevKernText(bool doDevKern) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | this->setFlags(SkSetClearMask(fFlags, doDevKern, kDevKernText_Flag)); |
| 284 | } |
| 285 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 286 | void SkPaint::setStyle(Style style) { |
| 287 | if ((unsigned)style < kStyleCount) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 288 | GEN_ID_INC_EVAL((unsigned)style != fStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | fStyle = style; |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 290 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 291 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 292 | SkDebugf("SkPaint::setStyle(%d) out of range\n", style); |
| 293 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 294 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | } |
| 296 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 297 | void SkPaint::setColor(SkColor color) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 298 | GEN_ID_INC_EVAL(color != fColor); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 299 | fColor = color; |
| 300 | } |
| 301 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 302 | void SkPaint::setAlpha(U8CPU a) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 303 | this->setColor(SkColorSetARGB(a, SkColorGetR(fColor), |
| 304 | SkColorGetG(fColor), SkColorGetB(fColor))); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | } |
| 306 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 307 | void SkPaint::setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 308 | this->setColor(SkColorSetARGB(a, r, g, b)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | } |
| 310 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 311 | void SkPaint::setStrokeWidth(SkScalar width) { |
| 312 | if (width >= 0) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 313 | GEN_ID_INC_EVAL(width != fWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | fWidth = width; |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 315 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 316 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 317 | SkDebugf("SkPaint::setStrokeWidth() called with negative value\n"); |
| 318 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 319 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | } |
| 321 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 322 | void SkPaint::setStrokeMiter(SkScalar limit) { |
| 323 | if (limit >= 0) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 324 | GEN_ID_INC_EVAL(limit != fMiterLimit); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | fMiterLimit = limit; |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 326 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 327 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 328 | SkDebugf("SkPaint::setStrokeMiter() called with negative value\n"); |
| 329 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 330 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | } |
| 332 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 333 | void SkPaint::setStrokeCap(Cap ct) { |
| 334 | if ((unsigned)ct < kCapCount) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 335 | GEN_ID_INC_EVAL((unsigned)ct != fCapType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | fCapType = SkToU8(ct); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 337 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 338 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 339 | SkDebugf("SkPaint::setStrokeCap(%d) out of range\n", ct); |
| 340 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 341 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | } |
| 343 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 344 | void SkPaint::setStrokeJoin(Join jt) { |
| 345 | if ((unsigned)jt < kJoinCount) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 346 | GEN_ID_INC_EVAL((unsigned)jt != fJoinType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | fJoinType = SkToU8(jt); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 348 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 349 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 350 | SkDebugf("SkPaint::setStrokeJoin(%d) out of range\n", jt); |
| 351 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 352 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | } |
| 354 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 355 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 357 | void SkPaint::setTextAlign(Align align) { |
| 358 | if ((unsigned)align < kAlignCount) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 359 | GEN_ID_INC_EVAL((unsigned)align != fTextAlign); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | fTextAlign = SkToU8(align); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 361 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 362 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 363 | SkDebugf("SkPaint::setTextAlign(%d) out of range\n", align); |
| 364 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 365 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | } |
| 367 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 368 | void SkPaint::setTextSize(SkScalar ts) { |
djsollen@google.com | c6f2e7d | 2012-01-04 18:43:43 +0000 | [diff] [blame] | 369 | if (ts >= 0) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 370 | GEN_ID_INC_EVAL(ts != fTextSize); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 371 | fTextSize = ts; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 372 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 373 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 374 | SkDebugf("SkPaint::setTextSize() called with negative value\n"); |
| 375 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 376 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | } |
| 378 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 379 | void SkPaint::setTextScaleX(SkScalar scaleX) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 380 | GEN_ID_INC_EVAL(scaleX != fTextScaleX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | fTextScaleX = scaleX; |
| 382 | } |
| 383 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 384 | void SkPaint::setTextSkewX(SkScalar skewX) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 385 | GEN_ID_INC_EVAL(skewX != fTextSkewX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 386 | fTextSkewX = skewX; |
| 387 | } |
| 388 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 389 | void SkPaint::setTextEncoding(TextEncoding encoding) { |
| 390 | if ((unsigned)encoding <= kGlyphID_TextEncoding) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 391 | GEN_ID_INC_EVAL((unsigned)encoding != fTextEncoding); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | fTextEncoding = encoding; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 393 | } else { |
reed@google.com | a323787 | 2011-07-05 19:20:48 +0000 | [diff] [blame] | 394 | #ifdef SK_REPORT_API_RANGE_CHECK |
| 395 | SkDebugf("SkPaint::setTextEncoding(%d) out of range\n", encoding); |
| 396 | #endif |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 397 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | } |
| 399 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 400 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 401 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 402 | SkTypeface* SkPaint::setTypeface(SkTypeface* font) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 403 | SkRefCnt_SafeAssign(fTypeface, font); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 404 | GEN_ID_INC; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | return font; |
| 406 | } |
| 407 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 408 | SkRasterizer* SkPaint::setRasterizer(SkRasterizer* r) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | SkRefCnt_SafeAssign(fRasterizer, r); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 410 | GEN_ID_INC; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 411 | return r; |
| 412 | } |
| 413 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 414 | SkDrawLooper* SkPaint::setLooper(SkDrawLooper* looper) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 415 | SkRefCnt_SafeAssign(fLooper, looper); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 416 | GEN_ID_INC; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 417 | return looper; |
| 418 | } |
| 419 | |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 420 | SkImageFilter* SkPaint::setImageFilter(SkImageFilter* imageFilter) { |
| 421 | SkRefCnt_SafeAssign(fImageFilter, imageFilter); |
| 422 | GEN_ID_INC; |
| 423 | return imageFilter; |
| 424 | } |
| 425 | |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 426 | SkAnnotation* SkPaint::setAnnotation(SkAnnotation* annotation) { |
| 427 | SkRefCnt_SafeAssign(fAnnotation, annotation); |
| 428 | GEN_ID_INC; |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 429 | return annotation; |
| 430 | } |
| 431 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 432 | /////////////////////////////////////////////////////////////////////////////// |
| 433 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 434 | static SkScalar mag2(SkScalar x, SkScalar y) { |
| 435 | return x * x + y * y; |
| 436 | } |
| 437 | |
| 438 | static bool tooBig(const SkMatrix& m, SkScalar ma2max) { |
| 439 | return mag2(m[SkMatrix::kMScaleX], m[SkMatrix::kMSkewY]) > ma2max |
| 440 | || |
| 441 | mag2(m[SkMatrix::kMSkewX], m[SkMatrix::kMScaleY]) > ma2max; |
| 442 | } |
| 443 | |
| 444 | bool SkPaint::TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM) { |
| 445 | SkASSERT(!ctm.hasPerspective()); |
| 446 | SkASSERT(!textM.hasPerspective()); |
| 447 | |
| 448 | SkMatrix matrix; |
| 449 | matrix.setConcat(ctm, textM); |
| 450 | return tooBig(matrix, MaxCacheSize2()); |
| 451 | } |
| 452 | |
| 453 | bool SkPaint::tooBigToUseCache(const SkMatrix& ctm) const { |
| 454 | SkMatrix textM; |
| 455 | return TooBigToUseCache(ctm, *this->setTextMatrix(&textM)); |
| 456 | } |
| 457 | |
| 458 | bool SkPaint::tooBigToUseCache() const { |
| 459 | SkMatrix textM; |
| 460 | return tooBig(*this->setTextMatrix(&textM), MaxCacheSize2()); |
| 461 | } |
| 462 | |
| 463 | /////////////////////////////////////////////////////////////////////////////// |
| 464 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 465 | #include "SkGlyphCache.h" |
| 466 | #include "SkUtils.h" |
| 467 | |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 468 | static void DetachDescProc(SkTypeface* typeface, const SkDescriptor* desc, |
| 469 | void* context) { |
| 470 | *((SkGlyphCache**)context) = SkGlyphCache::DetachCache(typeface, desc); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 471 | } |
| 472 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 473 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 474 | const SkGlyph& SkPaint::getUnicharMetrics(SkUnichar text, |
| 475 | const SkMatrix* deviceMatrix) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 476 | SkGlyphCache* cache; |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 477 | descriptorProc(NULL, deviceMatrix, DetachDescProc, &cache, true); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 478 | |
| 479 | const SkGlyph& glyph = cache->getUnicharMetrics(text); |
| 480 | |
| 481 | SkGlyphCache::AttachCache(cache); |
| 482 | return glyph; |
| 483 | } |
| 484 | |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 485 | const SkGlyph& SkPaint::getGlyphMetrics(uint16_t glyphId, |
| 486 | const SkMatrix* deviceMatrix) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 487 | SkGlyphCache* cache; |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 488 | descriptorProc(NULL, deviceMatrix, DetachDescProc, &cache, true); |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 489 | |
| 490 | const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphId); |
| 491 | |
| 492 | SkGlyphCache::AttachCache(cache); |
| 493 | return glyph; |
| 494 | } |
| 495 | |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 496 | const void* SkPaint::findImage(const SkGlyph& glyph, |
| 497 | const SkMatrix* deviceMatrix) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 498 | // See ::detachCache() |
| 499 | SkGlyphCache* cache; |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 500 | descriptorProc(NULL, deviceMatrix, DetachDescProc, &cache, true); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 501 | |
| 502 | const void* image = cache->findImage(glyph); |
| 503 | |
| 504 | SkGlyphCache::AttachCache(cache); |
| 505 | return image; |
| 506 | } |
| 507 | #endif |
| 508 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 509 | int SkPaint::textToGlyphs(const void* textData, size_t byteLength, |
| 510 | uint16_t glyphs[]) const { |
| 511 | if (byteLength == 0) { |
| 512 | return 0; |
| 513 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 514 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 515 | SkASSERT(textData != NULL); |
| 516 | |
| 517 | if (NULL == glyphs) { |
| 518 | switch (this->getTextEncoding()) { |
| 519 | case kUTF8_TextEncoding: |
| 520 | return SkUTF8_CountUnichars((const char*)textData, byteLength); |
| 521 | case kUTF16_TextEncoding: |
| 522 | return SkUTF16_CountUnichars((const uint16_t*)textData, |
| 523 | byteLength >> 1); |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 524 | case kUTF32_TextEncoding: |
| 525 | return byteLength >> 2; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | case kGlyphID_TextEncoding: |
| 527 | return byteLength >> 1; |
| 528 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 529 | SkDEBUGFAIL("unknown text encoding"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | } |
| 531 | return 0; |
| 532 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 533 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 534 | // if we get here, we have a valid glyphs[] array, so time to fill it in |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 535 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | // handle this encoding before the setup for the glyphcache |
| 537 | if (this->getTextEncoding() == kGlyphID_TextEncoding) { |
| 538 | // we want to ignore the low bit of byteLength |
| 539 | memcpy(glyphs, textData, byteLength >> 1 << 1); |
| 540 | return byteLength >> 1; |
| 541 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 542 | |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 543 | SkAutoGlyphCache autoCache(*this, NULL, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 544 | SkGlyphCache* cache = autoCache.getCache(); |
| 545 | |
| 546 | const char* text = (const char*)textData; |
| 547 | const char* stop = text + byteLength; |
| 548 | uint16_t* gptr = glyphs; |
| 549 | |
| 550 | switch (this->getTextEncoding()) { |
| 551 | case SkPaint::kUTF8_TextEncoding: |
| 552 | while (text < stop) { |
| 553 | *gptr++ = cache->unicharToGlyph(SkUTF8_NextUnichar(&text)); |
| 554 | } |
| 555 | break; |
| 556 | case SkPaint::kUTF16_TextEncoding: { |
| 557 | const uint16_t* text16 = (const uint16_t*)text; |
| 558 | const uint16_t* stop16 = (const uint16_t*)stop; |
| 559 | while (text16 < stop16) { |
| 560 | *gptr++ = cache->unicharToGlyph(SkUTF16_NextUnichar(&text16)); |
| 561 | } |
| 562 | break; |
| 563 | } |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 564 | case kUTF32_TextEncoding: { |
| 565 | const int32_t* text32 = (const int32_t*)text; |
| 566 | const int32_t* stop32 = (const int32_t*)stop; |
| 567 | while (text32 < stop32) { |
| 568 | *gptr++ = cache->unicharToGlyph(*text32++); |
| 569 | } |
| 570 | break; |
| 571 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 572 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 573 | SkDEBUGFAIL("unknown text encoding"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 574 | } |
| 575 | return gptr - glyphs; |
| 576 | } |
| 577 | |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 578 | bool SkPaint::containsText(const void* textData, size_t byteLength) const { |
| 579 | if (0 == byteLength) { |
| 580 | return true; |
| 581 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 582 | |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 583 | SkASSERT(textData != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 584 | |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 585 | // handle this encoding before the setup for the glyphcache |
| 586 | if (this->getTextEncoding() == kGlyphID_TextEncoding) { |
| 587 | const uint16_t* glyphID = static_cast<const uint16_t*>(textData); |
| 588 | size_t count = byteLength >> 1; |
| 589 | for (size_t i = 0; i < count; i++) { |
| 590 | if (0 == glyphID[i]) { |
| 591 | return false; |
| 592 | } |
| 593 | } |
| 594 | return true; |
| 595 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 596 | |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 597 | SkAutoGlyphCache autoCache(*this, NULL, NULL); |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 598 | SkGlyphCache* cache = autoCache.getCache(); |
| 599 | |
| 600 | switch (this->getTextEncoding()) { |
| 601 | case SkPaint::kUTF8_TextEncoding: { |
| 602 | const char* text = static_cast<const char*>(textData); |
| 603 | const char* stop = text + byteLength; |
| 604 | while (text < stop) { |
| 605 | if (0 == cache->unicharToGlyph(SkUTF8_NextUnichar(&text))) { |
| 606 | return false; |
| 607 | } |
| 608 | } |
| 609 | break; |
| 610 | } |
| 611 | case SkPaint::kUTF16_TextEncoding: { |
| 612 | const uint16_t* text = static_cast<const uint16_t*>(textData); |
| 613 | const uint16_t* stop = text + (byteLength >> 1); |
| 614 | while (text < stop) { |
| 615 | if (0 == cache->unicharToGlyph(SkUTF16_NextUnichar(&text))) { |
| 616 | return false; |
| 617 | } |
| 618 | } |
| 619 | break; |
| 620 | } |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 621 | case SkPaint::kUTF32_TextEncoding: { |
| 622 | const int32_t* text = static_cast<const int32_t*>(textData); |
| 623 | const int32_t* stop = text + (byteLength >> 2); |
| 624 | while (text < stop) { |
| 625 | if (0 == cache->unicharToGlyph(*text++)) { |
| 626 | return false; |
| 627 | } |
| 628 | } |
| 629 | break; |
| 630 | } |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 631 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 632 | SkDEBUGFAIL("unknown text encoding"); |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 633 | return false; |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 638 | void SkPaint::glyphsToUnichars(const uint16_t glyphs[], int count, |
| 639 | SkUnichar textData[]) const { |
| 640 | if (count <= 0) { |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | SkASSERT(glyphs != NULL); |
| 645 | SkASSERT(textData != NULL); |
| 646 | |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 647 | SkAutoGlyphCache autoCache(*this, NULL, NULL); |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 648 | SkGlyphCache* cache = autoCache.getCache(); |
| 649 | |
| 650 | for (int index = 0; index < count; index++) { |
| 651 | textData[index] = cache->glyphToUnichar(glyphs[index]); |
| 652 | } |
| 653 | } |
| 654 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 655 | /////////////////////////////////////////////////////////////////////////////// |
| 656 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 657 | static const SkGlyph& sk_getMetrics_utf8_next(SkGlyphCache* cache, |
| 658 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 659 | SkASSERT(cache != NULL); |
| 660 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 661 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 662 | return cache->getUnicharMetrics(SkUTF8_NextUnichar(text)); |
| 663 | } |
| 664 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 665 | static const SkGlyph& sk_getMetrics_utf8_prev(SkGlyphCache* cache, |
| 666 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 667 | SkASSERT(cache != NULL); |
| 668 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 669 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 670 | return cache->getUnicharMetrics(SkUTF8_PrevUnichar(text)); |
| 671 | } |
| 672 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 673 | static const SkGlyph& sk_getMetrics_utf16_next(SkGlyphCache* cache, |
| 674 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 675 | SkASSERT(cache != NULL); |
| 676 | SkASSERT(text != NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 677 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 678 | return cache->getUnicharMetrics(SkUTF16_NextUnichar((const uint16_t**)text)); |
| 679 | } |
| 680 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 681 | static const SkGlyph& sk_getMetrics_utf16_prev(SkGlyphCache* cache, |
| 682 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 683 | SkASSERT(cache != NULL); |
| 684 | SkASSERT(text != NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 685 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 686 | return cache->getUnicharMetrics(SkUTF16_PrevUnichar((const uint16_t**)text)); |
| 687 | } |
| 688 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 689 | static const SkGlyph& sk_getMetrics_utf32_next(SkGlyphCache* cache, |
| 690 | const char** text) { |
| 691 | SkASSERT(cache != NULL); |
| 692 | SkASSERT(text != NULL); |
| 693 | |
| 694 | const int32_t* ptr = *(const int32_t**)text; |
| 695 | SkUnichar uni = *ptr++; |
| 696 | *text = (const char*)ptr; |
| 697 | return cache->getUnicharMetrics(uni); |
| 698 | } |
| 699 | |
| 700 | static const SkGlyph& sk_getMetrics_utf32_prev(SkGlyphCache* cache, |
| 701 | const char** text) { |
| 702 | SkASSERT(cache != NULL); |
| 703 | SkASSERT(text != NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 704 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 705 | const int32_t* ptr = *(const int32_t**)text; |
| 706 | SkUnichar uni = *--ptr; |
| 707 | *text = (const char*)ptr; |
| 708 | return cache->getUnicharMetrics(uni); |
| 709 | } |
| 710 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 711 | static const SkGlyph& sk_getMetrics_glyph_next(SkGlyphCache* cache, |
| 712 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 713 | SkASSERT(cache != NULL); |
| 714 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 715 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 716 | const uint16_t* ptr = *(const uint16_t**)text; |
| 717 | unsigned glyphID = *ptr; |
| 718 | ptr += 1; |
| 719 | *text = (const char*)ptr; |
| 720 | return cache->getGlyphIDMetrics(glyphID); |
| 721 | } |
| 722 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 723 | static const SkGlyph& sk_getMetrics_glyph_prev(SkGlyphCache* cache, |
| 724 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 725 | SkASSERT(cache != NULL); |
| 726 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 727 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 728 | const uint16_t* ptr = *(const uint16_t**)text; |
| 729 | ptr -= 1; |
| 730 | unsigned glyphID = *ptr; |
| 731 | *text = (const char*)ptr; |
| 732 | return cache->getGlyphIDMetrics(glyphID); |
| 733 | } |
| 734 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 735 | static const SkGlyph& sk_getAdvance_utf8_next(SkGlyphCache* cache, |
| 736 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 737 | SkASSERT(cache != NULL); |
| 738 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 739 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | return cache->getUnicharAdvance(SkUTF8_NextUnichar(text)); |
| 741 | } |
| 742 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 743 | static const SkGlyph& sk_getAdvance_utf8_prev(SkGlyphCache* cache, |
| 744 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 745 | SkASSERT(cache != NULL); |
| 746 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 747 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | return cache->getUnicharAdvance(SkUTF8_PrevUnichar(text)); |
| 749 | } |
| 750 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 751 | static const SkGlyph& sk_getAdvance_utf16_next(SkGlyphCache* cache, |
| 752 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 753 | SkASSERT(cache != NULL); |
| 754 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 755 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 756 | return cache->getUnicharAdvance(SkUTF16_NextUnichar((const uint16_t**)text)); |
| 757 | } |
| 758 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 759 | static const SkGlyph& sk_getAdvance_utf16_prev(SkGlyphCache* cache, |
| 760 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 761 | SkASSERT(cache != NULL); |
| 762 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 763 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 764 | return cache->getUnicharAdvance(SkUTF16_PrevUnichar((const uint16_t**)text)); |
| 765 | } |
| 766 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 767 | static const SkGlyph& sk_getAdvance_utf32_next(SkGlyphCache* cache, |
| 768 | const char** text) { |
| 769 | SkASSERT(cache != NULL); |
| 770 | SkASSERT(text != NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 771 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 772 | const int32_t* ptr = *(const int32_t**)text; |
| 773 | SkUnichar uni = *ptr++; |
| 774 | *text = (const char*)ptr; |
| 775 | return cache->getUnicharAdvance(uni); |
| 776 | } |
| 777 | |
| 778 | static const SkGlyph& sk_getAdvance_utf32_prev(SkGlyphCache* cache, |
| 779 | const char** text) { |
| 780 | SkASSERT(cache != NULL); |
| 781 | SkASSERT(text != NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 782 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 783 | const int32_t* ptr = *(const int32_t**)text; |
| 784 | SkUnichar uni = *--ptr; |
| 785 | *text = (const char*)ptr; |
| 786 | return cache->getUnicharAdvance(uni); |
| 787 | } |
| 788 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 789 | static const SkGlyph& sk_getAdvance_glyph_next(SkGlyphCache* cache, |
| 790 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 791 | SkASSERT(cache != NULL); |
| 792 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 793 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 794 | const uint16_t* ptr = *(const uint16_t**)text; |
| 795 | unsigned glyphID = *ptr; |
| 796 | ptr += 1; |
| 797 | *text = (const char*)ptr; |
| 798 | return cache->getGlyphIDAdvance(glyphID); |
| 799 | } |
| 800 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 801 | static const SkGlyph& sk_getAdvance_glyph_prev(SkGlyphCache* cache, |
| 802 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 803 | SkASSERT(cache != NULL); |
| 804 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 805 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 806 | const uint16_t* ptr = *(const uint16_t**)text; |
| 807 | ptr -= 1; |
| 808 | unsigned glyphID = *ptr; |
| 809 | *text = (const char*)ptr; |
| 810 | return cache->getGlyphIDAdvance(glyphID); |
| 811 | } |
| 812 | |
| 813 | SkMeasureCacheProc SkPaint::getMeasureCacheProc(TextBufferDirection tbd, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 814 | bool needFullMetrics) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 815 | static const SkMeasureCacheProc gMeasureCacheProcs[] = { |
| 816 | sk_getMetrics_utf8_next, |
| 817 | sk_getMetrics_utf16_next, |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 818 | sk_getMetrics_utf32_next, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 819 | sk_getMetrics_glyph_next, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 820 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 821 | sk_getMetrics_utf8_prev, |
| 822 | sk_getMetrics_utf16_prev, |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 823 | sk_getMetrics_utf32_prev, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 824 | sk_getMetrics_glyph_prev, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 825 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 826 | sk_getAdvance_utf8_next, |
| 827 | sk_getAdvance_utf16_next, |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 828 | sk_getAdvance_utf32_next, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 829 | sk_getAdvance_glyph_next, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 830 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 831 | sk_getAdvance_utf8_prev, |
| 832 | sk_getAdvance_utf16_prev, |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 833 | sk_getAdvance_utf32_prev, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 834 | sk_getAdvance_glyph_prev |
| 835 | }; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 836 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 837 | unsigned index = this->getTextEncoding(); |
| 838 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 839 | if (kBackward_TextBufferDirection == tbd) { |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 840 | index += 4; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 841 | } |
| 842 | if (!needFullMetrics && !this->isDevKernText()) { |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 843 | index += 8; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 844 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 845 | |
| 846 | SkASSERT(index < SK_ARRAY_COUNT(gMeasureCacheProcs)); |
| 847 | return gMeasureCacheProcs[index]; |
| 848 | } |
| 849 | |
| 850 | /////////////////////////////////////////////////////////////////////////////// |
| 851 | |
| 852 | static const SkGlyph& sk_getMetrics_utf8_00(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 853 | const char** text, SkFixed, SkFixed) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 854 | SkASSERT(cache != NULL); |
| 855 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 856 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 857 | return cache->getUnicharMetrics(SkUTF8_NextUnichar(text)); |
| 858 | } |
| 859 | |
| 860 | static const SkGlyph& sk_getMetrics_utf8_xy(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 861 | const char** text, SkFixed x, SkFixed y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 862 | SkASSERT(cache != NULL); |
| 863 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 864 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 865 | return cache->getUnicharMetrics(SkUTF8_NextUnichar(text), x, y); |
| 866 | } |
| 867 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 868 | static const SkGlyph& sk_getMetrics_utf16_00(SkGlyphCache* cache, |
| 869 | const char** text, SkFixed, SkFixed) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 870 | SkASSERT(cache != NULL); |
| 871 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 872 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 873 | return cache->getUnicharMetrics(SkUTF16_NextUnichar((const uint16_t**)text)); |
| 874 | } |
| 875 | |
| 876 | static const SkGlyph& sk_getMetrics_utf16_xy(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 877 | const char** text, SkFixed x, SkFixed y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 878 | SkASSERT(cache != NULL); |
| 879 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 880 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 881 | return cache->getUnicharMetrics(SkUTF16_NextUnichar((const uint16_t**)text), |
| 882 | x, y); |
| 883 | } |
| 884 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 885 | static const SkGlyph& sk_getMetrics_utf32_00(SkGlyphCache* cache, |
| 886 | const char** text, SkFixed, SkFixed) { |
| 887 | SkASSERT(cache != NULL); |
| 888 | SkASSERT(text != NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 889 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 890 | const int32_t* ptr = *(const int32_t**)text; |
| 891 | SkUnichar uni = *ptr++; |
| 892 | *text = (const char*)ptr; |
| 893 | return cache->getUnicharMetrics(uni); |
| 894 | } |
| 895 | |
| 896 | static const SkGlyph& sk_getMetrics_utf32_xy(SkGlyphCache* cache, |
| 897 | const char** text, SkFixed x, SkFixed y) { |
| 898 | SkASSERT(cache != NULL); |
| 899 | SkASSERT(text != NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 900 | |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 901 | const int32_t* ptr = *(const int32_t**)text; |
| 902 | SkUnichar uni = *--ptr; |
| 903 | *text = (const char*)ptr; |
sugoi@google.com | 8d38d51 | 2013-02-26 21:56:19 +0000 | [diff] [blame] | 904 | return cache->getUnicharMetrics(uni, x, y); |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 905 | } |
| 906 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 907 | static const SkGlyph& sk_getMetrics_glyph_00(SkGlyphCache* cache, |
| 908 | const char** text, SkFixed, SkFixed) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 909 | SkASSERT(cache != NULL); |
| 910 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 911 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 912 | const uint16_t* ptr = *(const uint16_t**)text; |
| 913 | unsigned glyphID = *ptr; |
| 914 | ptr += 1; |
| 915 | *text = (const char*)ptr; |
| 916 | return cache->getGlyphIDMetrics(glyphID); |
| 917 | } |
| 918 | |
| 919 | static const SkGlyph& sk_getMetrics_glyph_xy(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 920 | const char** text, SkFixed x, SkFixed y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 921 | SkASSERT(cache != NULL); |
| 922 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 923 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 924 | const uint16_t* ptr = *(const uint16_t**)text; |
| 925 | unsigned glyphID = *ptr; |
| 926 | ptr += 1; |
| 927 | *text = (const char*)ptr; |
| 928 | return cache->getGlyphIDMetrics(glyphID, x, y); |
| 929 | } |
| 930 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 931 | SkDrawCacheProc SkPaint::getDrawCacheProc() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 932 | static const SkDrawCacheProc gDrawCacheProcs[] = { |
| 933 | sk_getMetrics_utf8_00, |
| 934 | sk_getMetrics_utf16_00, |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 935 | sk_getMetrics_utf32_00, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 936 | sk_getMetrics_glyph_00, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 937 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 938 | sk_getMetrics_utf8_xy, |
| 939 | sk_getMetrics_utf16_xy, |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 940 | sk_getMetrics_utf32_xy, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 941 | sk_getMetrics_glyph_xy |
| 942 | }; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 943 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 944 | unsigned index = this->getTextEncoding(); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 945 | if (fFlags & kSubpixelText_Flag) { |
reed@google.com | 68bc6f7 | 2012-03-14 19:41:55 +0000 | [diff] [blame] | 946 | index += 4; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 947 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 948 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 949 | SkASSERT(index < SK_ARRAY_COUNT(gDrawCacheProcs)); |
| 950 | return gDrawCacheProcs[index]; |
| 951 | } |
| 952 | |
| 953 | /////////////////////////////////////////////////////////////////////////////// |
| 954 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 955 | #define TEXT_AS_PATHS_PAINT_FLAGS_TO_IGNORE ( \ |
| 956 | SkPaint::kDevKernText_Flag | \ |
| 957 | SkPaint::kLinearText_Flag | \ |
| 958 | SkPaint::kLCDRenderText_Flag | \ |
| 959 | SkPaint::kEmbeddedBitmapText_Flag | \ |
| 960 | SkPaint::kAutoHinting_Flag | \ |
| 961 | SkPaint::kGenA8FromLCD_Flag ) |
| 962 | |
| 963 | SkScalar SkPaint::setupForAsPaths() { |
| 964 | uint32_t flags = this->getFlags(); |
| 965 | // clear the flags we don't care about |
| 966 | flags &= ~TEXT_AS_PATHS_PAINT_FLAGS_TO_IGNORE; |
| 967 | // set the flags we do care about |
| 968 | flags |= SkPaint::kSubpixelText_Flag; |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 969 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 970 | this->setFlags(flags); |
| 971 | this->setHinting(SkPaint::kNo_Hinting); |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 972 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 973 | SkScalar textSize = fTextSize; |
| 974 | this->setTextSize(kCanonicalTextSizeForPaths); |
| 975 | return textSize / kCanonicalTextSizeForPaths; |
| 976 | } |
| 977 | |
| 978 | class SkCanonicalizePaint { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 979 | public: |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 980 | SkCanonicalizePaint(const SkPaint& paint) : fPaint(&paint), fScale(0) { |
| 981 | if (paint.isLinearText() || paint.tooBigToUseCache()) { |
| 982 | SkPaint* p = fLazy.set(paint); |
| 983 | fScale = p->setupForAsPaths(); |
| 984 | fPaint = p; |
| 985 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 986 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 987 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 988 | const SkPaint& getPaint() const { return *fPaint; } |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 989 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 990 | /** |
| 991 | * Returns 0 if the paint was unmodified, or the scale factor need to |
| 992 | * the original textSize |
| 993 | */ |
| 994 | SkScalar getScale() const { return fScale; } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 995 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 996 | private: |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 997 | const SkPaint* fPaint; |
| 998 | SkScalar fScale; |
| 999 | SkTLazy<SkPaint> fLazy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1000 | }; |
| 1001 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1002 | static void set_bounds(const SkGlyph& g, SkRect* bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1003 | bounds->set(SkIntToScalar(g.fLeft), |
| 1004 | SkIntToScalar(g.fTop), |
| 1005 | SkIntToScalar(g.fLeft + g.fWidth), |
| 1006 | SkIntToScalar(g.fTop + g.fHeight)); |
| 1007 | } |
| 1008 | |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 1009 | // 64bits wide, with a 16bit bias. Useful when accumulating lots of 16.16 so |
| 1010 | // we don't overflow along the way |
| 1011 | typedef int64_t Sk48Dot16; |
| 1012 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame^] | 1013 | static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { |
| 1014 | return (float) (x * 1.5258789e-5); // x * (1 / 65536.0f) |
| 1015 | } |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 1016 | |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1017 | static void join_bounds_x(const SkGlyph& g, SkRect* bounds, Sk48Dot16 dx) { |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 1018 | SkScalar sx = Sk48Dot16ToScalar(dx); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1019 | bounds->join(SkIntToScalar(g.fLeft) + sx, |
| 1020 | SkIntToScalar(g.fTop), |
| 1021 | SkIntToScalar(g.fLeft + g.fWidth) + sx, |
| 1022 | SkIntToScalar(g.fTop + g.fHeight)); |
| 1023 | } |
| 1024 | |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1025 | static void join_bounds_y(const SkGlyph& g, SkRect* bounds, Sk48Dot16 dy) { |
| 1026 | SkScalar sy = Sk48Dot16ToScalar(dy); |
| 1027 | bounds->join(SkIntToScalar(g.fLeft), |
| 1028 | SkIntToScalar(g.fTop) + sy, |
| 1029 | SkIntToScalar(g.fLeft + g.fWidth), |
| 1030 | SkIntToScalar(g.fTop + g.fHeight) + sy); |
| 1031 | } |
| 1032 | |
| 1033 | typedef void (*JoinBoundsProc)(const SkGlyph&, SkRect*, Sk48Dot16); |
| 1034 | |
| 1035 | // xyIndex is 0 for fAdvanceX or 1 for fAdvanceY |
| 1036 | static SkFixed advance(const SkGlyph& glyph, int xyIndex) { |
| 1037 | SkASSERT(0 == xyIndex || 1 == xyIndex); |
| 1038 | return (&glyph.fAdvanceX)[xyIndex]; |
| 1039 | } |
| 1040 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1041 | SkScalar SkPaint::measure_text(SkGlyphCache* cache, |
| 1042 | const char* text, size_t byteLength, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1043 | int* count, SkRect* bounds) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1044 | SkASSERT(count); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1045 | if (byteLength == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1046 | *count = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1047 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1048 | bounds->setEmpty(); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1049 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | SkMeasureCacheProc glyphCacheProc; |
| 1054 | glyphCacheProc = this->getMeasureCacheProc(kForward_TextBufferDirection, |
| 1055 | NULL != bounds); |
| 1056 | |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1057 | int xyIndex; |
| 1058 | JoinBoundsProc joinBoundsProc; |
| 1059 | if (this->isVerticalText()) { |
| 1060 | xyIndex = 1; |
| 1061 | joinBoundsProc = join_bounds_y; |
| 1062 | } else { |
| 1063 | xyIndex = 0; |
| 1064 | joinBoundsProc = join_bounds_x; |
| 1065 | } |
| 1066 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1067 | int n = 1; |
| 1068 | const char* stop = (const char*)text + byteLength; |
| 1069 | const SkGlyph* g = &glyphCacheProc(cache, &text); |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 1070 | // our accumulated fixed-point advances might overflow 16.16, so we use |
| 1071 | // a 48.16 (64bit) accumulator, and then convert that to scalar at the |
| 1072 | // very end. |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1073 | Sk48Dot16 x = advance(*g, xyIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1074 | |
| 1075 | SkAutoKern autokern; |
| 1076 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1077 | if (NULL == bounds) { |
| 1078 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1079 | int rsb; |
| 1080 | for (; text < stop; n++) { |
| 1081 | rsb = g->fRsbDelta; |
| 1082 | g = &glyphCacheProc(cache, &text); |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1083 | x += SkAutoKern_AdjustF(rsb, g->fLsbDelta) + advance(*g, xyIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1084 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1085 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1086 | for (; text < stop; n++) { |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1087 | x += advance(glyphCacheProc(cache, &text), xyIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1090 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1091 | set_bounds(*g, bounds); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1092 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1093 | int rsb; |
| 1094 | for (; text < stop; n++) { |
| 1095 | rsb = g->fRsbDelta; |
| 1096 | g = &glyphCacheProc(cache, &text); |
| 1097 | x += SkAutoKern_AdjustF(rsb, g->fLsbDelta); |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1098 | joinBoundsProc(*g, bounds, x); |
| 1099 | x += advance(*g, xyIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1100 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1101 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1102 | for (; text < stop; n++) { |
| 1103 | g = &glyphCacheProc(cache, &text); |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1104 | joinBoundsProc(*g, bounds, x); |
| 1105 | x += advance(*g, xyIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | SkASSERT(text == stop); |
| 1110 | |
| 1111 | *count = n; |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 1112 | return Sk48Dot16ToScalar(x); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | SkScalar SkPaint::measureText(const void* textData, size_t length, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1116 | SkRect* bounds, SkScalar zoom) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1117 | const char* text = (const char*)textData; |
| 1118 | SkASSERT(text != NULL || length == 0); |
| 1119 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1120 | SkCanonicalizePaint canon(*this); |
| 1121 | const SkPaint& paint = canon.getPaint(); |
| 1122 | SkScalar scale = canon.getScale(); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1123 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1124 | SkMatrix zoomMatrix, *zoomPtr = NULL; |
| 1125 | if (zoom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1126 | zoomMatrix.setScale(zoom, zoom); |
| 1127 | zoomPtr = &zoomMatrix; |
| 1128 | } |
| 1129 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1130 | SkAutoGlyphCache autoCache(paint, NULL, zoomPtr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1131 | SkGlyphCache* cache = autoCache.getCache(); |
| 1132 | |
| 1133 | SkScalar width = 0; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1134 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1135 | if (length > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1136 | int tempCount; |
| 1137 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1138 | width = paint.measure_text(cache, text, length, &tempCount, bounds); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1139 | if (scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1140 | width = SkScalarMul(width, scale); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1141 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1142 | bounds->fLeft = SkScalarMul(bounds->fLeft, scale); |
| 1143 | bounds->fTop = SkScalarMul(bounds->fTop, scale); |
| 1144 | bounds->fRight = SkScalarMul(bounds->fRight, scale); |
| 1145 | bounds->fBottom = SkScalarMul(bounds->fBottom, scale); |
| 1146 | } |
| 1147 | } |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 1148 | } else if (bounds) { |
| 1149 | // ensure that even if we don't measure_text we still update the bounds |
| 1150 | bounds->setEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1151 | } |
| 1152 | return width; |
| 1153 | } |
| 1154 | |
| 1155 | typedef bool (*SkTextBufferPred)(const char* text, const char* stop); |
| 1156 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1157 | static bool forward_textBufferPred(const char* text, const char* stop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1158 | return text < stop; |
| 1159 | } |
| 1160 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1161 | static bool backward_textBufferPred(const char* text, const char* stop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1162 | return text > stop; |
| 1163 | } |
| 1164 | |
| 1165 | static SkTextBufferPred chooseTextBufferPred(SkPaint::TextBufferDirection tbd, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1166 | const char** text, size_t length, |
| 1167 | const char** stop) { |
| 1168 | if (SkPaint::kForward_TextBufferDirection == tbd) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1169 | *stop = *text + length; |
| 1170 | return forward_textBufferPred; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1171 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1172 | // text should point to the end of the buffer, and stop to the beginning |
| 1173 | *stop = *text; |
| 1174 | *text += length; |
| 1175 | return backward_textBufferPred; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | size_t SkPaint::breakText(const void* textD, size_t length, SkScalar maxWidth, |
| 1180 | SkScalar* measuredWidth, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1181 | TextBufferDirection tbd) const { |
| 1182 | if (0 == length || 0 >= maxWidth) { |
| 1183 | if (measuredWidth) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1184 | *measuredWidth = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1185 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | |
djsollen@google.com | c6f2e7d | 2012-01-04 18:43:43 +0000 | [diff] [blame] | 1189 | if (0 == fTextSize) { |
| 1190 | if (measuredWidth) { |
| 1191 | *measuredWidth = 0; |
| 1192 | } |
| 1193 | return length; |
| 1194 | } |
| 1195 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1196 | SkASSERT(textD != NULL); |
| 1197 | const char* text = (const char*)textD; |
| 1198 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1199 | SkCanonicalizePaint canon(*this); |
| 1200 | const SkPaint& paint = canon.getPaint(); |
| 1201 | SkScalar scale = canon.getScale(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1202 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1203 | // adjust max in case we changed the textSize in paint |
| 1204 | if (scale) { |
| 1205 | maxWidth /= scale; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1206 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1207 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1208 | SkAutoGlyphCache autoCache(paint, NULL, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1209 | SkGlyphCache* cache = autoCache.getCache(); |
| 1210 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1211 | SkMeasureCacheProc glyphCacheProc = paint.getMeasureCacheProc(tbd, false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1212 | const char* stop; |
| 1213 | SkTextBufferPred pred = chooseTextBufferPred(tbd, &text, length, &stop); |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1214 | const int xyIndex = paint.isVerticalText() ? 1 : 0; |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 1215 | // use 64bits for our accumulator, to avoid overflowing 16.16 |
| 1216 | Sk48Dot16 max = SkScalarToFixed(maxWidth); |
| 1217 | Sk48Dot16 width = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1218 | |
| 1219 | SkAutoKern autokern; |
| 1220 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1221 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1222 | int rsb = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1223 | while (pred(text, stop)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1224 | const char* curr = text; |
| 1225 | const SkGlyph& g = glyphCacheProc(cache, &text); |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1226 | SkFixed x = SkAutoKern_AdjustF(rsb, g.fLsbDelta) + advance(g, xyIndex); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1227 | if ((width += x) > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1228 | width -= x; |
| 1229 | text = curr; |
| 1230 | break; |
| 1231 | } |
| 1232 | rsb = g.fRsbDelta; |
| 1233 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1234 | } else { |
| 1235 | while (pred(text, stop)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1236 | const char* curr = text; |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1237 | SkFixed x = advance(glyphCacheProc(cache, &text), xyIndex); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1238 | if ((width += x) > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1239 | width -= x; |
| 1240 | text = curr; |
| 1241 | break; |
| 1242 | } |
| 1243 | } |
| 1244 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1245 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1246 | if (measuredWidth) { |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 1247 | SkScalar scalarWidth = Sk48Dot16ToScalar(width); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1248 | if (scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1249 | scalarWidth = SkScalarMul(scalarWidth, scale); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1250 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1251 | *measuredWidth = scalarWidth; |
| 1252 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1253 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1254 | // return the number of bytes measured |
| 1255 | return (kForward_TextBufferDirection == tbd) ? |
| 1256 | text - stop + length : stop - text + length; |
| 1257 | } |
| 1258 | |
| 1259 | /////////////////////////////////////////////////////////////////////////////// |
| 1260 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1261 | static bool FontMetricsCacheProc(const SkGlyphCache* cache, void* context) { |
reed@google.com | 0a01f5a | 2013-05-08 14:19:08 +0000 | [diff] [blame] | 1262 | *(SkPaint::FontMetrics*)context = cache->getFontMetrics(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1263 | return false; // don't detach the cache |
| 1264 | } |
| 1265 | |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 1266 | static void FontMetricsDescProc(SkTypeface* typeface, const SkDescriptor* desc, |
| 1267 | void* context) { |
| 1268 | SkGlyphCache::VisitCache(typeface, desc, FontMetricsCacheProc, context); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1271 | SkScalar SkPaint::getFontMetrics(FontMetrics* metrics, SkScalar zoom) const { |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1272 | SkCanonicalizePaint canon(*this); |
| 1273 | const SkPaint& paint = canon.getPaint(); |
| 1274 | SkScalar scale = canon.getScale(); |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 1275 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1276 | SkMatrix zoomMatrix, *zoomPtr = NULL; |
| 1277 | if (zoom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1278 | zoomMatrix.setScale(zoom, zoom); |
| 1279 | zoomPtr = &zoomMatrix; |
| 1280 | } |
| 1281 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1282 | FontMetrics storage; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1283 | if (NULL == metrics) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1284 | metrics = &storage; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1285 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1286 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1287 | paint.descriptorProc(NULL, zoomPtr, FontMetricsDescProc, metrics, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1288 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1289 | if (scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1290 | metrics->fTop = SkScalarMul(metrics->fTop, scale); |
| 1291 | metrics->fAscent = SkScalarMul(metrics->fAscent, scale); |
| 1292 | metrics->fDescent = SkScalarMul(metrics->fDescent, scale); |
| 1293 | metrics->fBottom = SkScalarMul(metrics->fBottom, scale); |
| 1294 | metrics->fLeading = SkScalarMul(metrics->fLeading, scale); |
reed@google.com | 0a01f5a | 2013-05-08 14:19:08 +0000 | [diff] [blame] | 1295 | metrics->fAvgCharWidth = SkScalarMul(metrics->fAvgCharWidth, scale); |
| 1296 | metrics->fXMin = SkScalarMul(metrics->fXMin, scale); |
| 1297 | metrics->fXMax = SkScalarMul(metrics->fXMax, scale); |
| 1298 | metrics->fXHeight = SkScalarMul(metrics->fXHeight, scale); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1299 | } |
| 1300 | return metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 1301 | } |
| 1302 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1303 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1304 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1305 | static void set_bounds(const SkGlyph& g, SkRect* bounds, SkScalar scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1306 | bounds->set(g.fLeft * scale, |
| 1307 | g.fTop * scale, |
| 1308 | (g.fLeft + g.fWidth) * scale, |
| 1309 | (g.fTop + g.fHeight) * scale); |
| 1310 | } |
| 1311 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1312 | int SkPaint::getTextWidths(const void* textData, size_t byteLength, |
| 1313 | SkScalar widths[], SkRect bounds[]) const { |
| 1314 | if (0 == byteLength) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1315 | return 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1316 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1317 | |
| 1318 | SkASSERT(NULL != textData); |
| 1319 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1320 | if (NULL == widths && NULL == bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1321 | return this->countText(textData, byteLength); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1322 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1323 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1324 | SkCanonicalizePaint canon(*this); |
| 1325 | const SkPaint& paint = canon.getPaint(); |
| 1326 | SkScalar scale = canon.getScale(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1327 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1328 | SkAutoGlyphCache autoCache(paint, NULL, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1329 | SkGlyphCache* cache = autoCache.getCache(); |
| 1330 | SkMeasureCacheProc glyphCacheProc; |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1331 | glyphCacheProc = paint.getMeasureCacheProc(kForward_TextBufferDirection, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1332 | NULL != bounds); |
| 1333 | |
| 1334 | const char* text = (const char*)textData; |
| 1335 | const char* stop = text + byteLength; |
| 1336 | int count = 0; |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1337 | const int xyIndex = paint.isVerticalText() ? 1 : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1338 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1339 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1340 | // we adjust the widths returned here through auto-kerning |
| 1341 | SkAutoKern autokern; |
| 1342 | SkFixed prevWidth = 0; |
| 1343 | |
| 1344 | if (scale) { |
| 1345 | while (text < stop) { |
| 1346 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1347 | if (widths) { |
| 1348 | SkFixed adjust = autokern.adjust(g); |
| 1349 | |
| 1350 | if (count > 0) { |
| 1351 | SkScalar w = SkFixedToScalar(prevWidth + adjust); |
| 1352 | *widths++ = SkScalarMul(w, scale); |
| 1353 | } |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1354 | prevWidth = advance(g, xyIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1355 | } |
| 1356 | if (bounds) { |
| 1357 | set_bounds(g, bounds++, scale); |
| 1358 | } |
| 1359 | ++count; |
| 1360 | } |
| 1361 | if (count > 0 && widths) { |
| 1362 | *widths = SkScalarMul(SkFixedToScalar(prevWidth), scale); |
| 1363 | } |
| 1364 | } else { |
| 1365 | while (text < stop) { |
| 1366 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1367 | if (widths) { |
| 1368 | SkFixed adjust = autokern.adjust(g); |
| 1369 | |
| 1370 | if (count > 0) { |
| 1371 | *widths++ = SkFixedToScalar(prevWidth + adjust); |
| 1372 | } |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1373 | prevWidth = advance(g, xyIndex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1374 | } |
| 1375 | if (bounds) { |
| 1376 | set_bounds(g, bounds++); |
| 1377 | } |
| 1378 | ++count; |
| 1379 | } |
| 1380 | if (count > 0 && widths) { |
| 1381 | *widths = SkFixedToScalar(prevWidth); |
| 1382 | } |
| 1383 | } |
| 1384 | } else { // no devkern |
| 1385 | if (scale) { |
| 1386 | while (text < stop) { |
| 1387 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1388 | if (widths) { |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1389 | *widths++ = SkScalarMul(SkFixedToScalar(advance(g, xyIndex)), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1390 | scale); |
| 1391 | } |
| 1392 | if (bounds) { |
| 1393 | set_bounds(g, bounds++, scale); |
| 1394 | } |
| 1395 | ++count; |
| 1396 | } |
| 1397 | } else { |
| 1398 | while (text < stop) { |
| 1399 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1400 | if (widths) { |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 1401 | *widths++ = SkFixedToScalar(advance(g, xyIndex)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1402 | } |
| 1403 | if (bounds) { |
| 1404 | set_bounds(g, bounds++); |
| 1405 | } |
| 1406 | ++count; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | SkASSERT(text == stop); |
| 1412 | return count; |
| 1413 | } |
| 1414 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1415 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1416 | |
| 1417 | #include "SkDraw.h" |
| 1418 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1419 | void SkPaint::getTextPath(const void* textData, size_t length, |
| 1420 | SkScalar x, SkScalar y, SkPath* path) const { |
| 1421 | SkASSERT(length == 0 || textData != NULL); |
| 1422 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1423 | const char* text = (const char*)textData; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1424 | if (text == NULL || length == 0 || path == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1425 | return; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1426 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1427 | |
djsollen@google.com | 166e653 | 2012-03-20 14:24:38 +0000 | [diff] [blame] | 1428 | SkTextToPathIter iter(text, length, *this, false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1429 | SkMatrix matrix; |
| 1430 | SkScalar prevXPos = 0; |
| 1431 | |
| 1432 | matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
| 1433 | matrix.postTranslate(x, y); |
| 1434 | path->reset(); |
| 1435 | |
| 1436 | SkScalar xpos; |
| 1437 | const SkPath* iterPath; |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 1438 | while (iter.next(&iterPath, &xpos)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1439 | matrix.postTranslate(xpos - prevXPos, 0); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 1440 | if (iterPath) { |
| 1441 | path->addPath(*iterPath, matrix); |
| 1442 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1443 | prevXPos = xpos; |
| 1444 | } |
| 1445 | } |
| 1446 | |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 1447 | void SkPaint::getPosTextPath(const void* textData, size_t length, |
| 1448 | const SkPoint pos[], SkPath* path) const { |
| 1449 | SkASSERT(length == 0 || textData != NULL); |
| 1450 | |
| 1451 | const char* text = (const char*)textData; |
| 1452 | if (text == NULL || length == 0 || path == NULL) { |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | SkTextToPathIter iter(text, length, *this, false); |
| 1457 | SkMatrix matrix; |
| 1458 | SkPoint prevPos; |
| 1459 | prevPos.set(0, 0); |
| 1460 | |
| 1461 | matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
| 1462 | path->reset(); |
| 1463 | |
| 1464 | unsigned int i = 0; |
| 1465 | const SkPath* iterPath; |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 1466 | while (iter.next(&iterPath, NULL)) { |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 1467 | matrix.postTranslate(pos[i].fX - prevPos.fX, pos[i].fY - prevPos.fY); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 1468 | if (iterPath) { |
| 1469 | path->addPath(*iterPath, matrix); |
| 1470 | } |
reed@google.com | ca0062e | 2012-07-20 11:20:32 +0000 | [diff] [blame] | 1471 | prevPos = pos[i]; |
| 1472 | i++; |
| 1473 | } |
| 1474 | } |
| 1475 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1476 | static void add_flattenable(SkDescriptor* desc, uint32_t tag, |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1477 | SkOrderedWriteBuffer* buffer) { |
| 1478 | buffer->writeToMemory(desc->addEntry(tag, buffer->size(), NULL)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
reed@google.com | 2739b27 | 2011-09-28 17:26:42 +0000 | [diff] [blame] | 1481 | // SkFontHost can override this choice in FilterRec() |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1482 | static SkMask::Format computeMaskFormat(const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1483 | uint32_t flags = paint.getFlags(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1484 | |
agl@chromium.org | b4234a2 | 2010-01-21 11:43:44 +0000 | [diff] [blame] | 1485 | // Antialiasing being disabled trumps all other settings. |
reed@google.com | 65dd8f8 | 2011-03-14 13:31:16 +0000 | [diff] [blame] | 1486 | if (!(flags & SkPaint::kAntiAlias_Flag)) { |
agl@chromium.org | b4234a2 | 2010-01-21 11:43:44 +0000 | [diff] [blame] | 1487 | return SkMask::kBW_Format; |
reed@google.com | f88d676 | 2011-03-10 15:06:27 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
reed@google.com | 65dd8f8 | 2011-03-14 13:31:16 +0000 | [diff] [blame] | 1490 | if (flags & SkPaint::kLCDRenderText_Flag) { |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1491 | return SkMask::kLCD16_Format; |
reed@google.com | 65dd8f8 | 2011-03-14 13:31:16 +0000 | [diff] [blame] | 1492 | } |
agl@chromium.org | b4234a2 | 2010-01-21 11:43:44 +0000 | [diff] [blame] | 1493 | |
| 1494 | return SkMask::kA8_Format; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1497 | // if linear-text is on, then we force hinting to be off (since that's sort of |
| 1498 | // the point of linear-text. |
| 1499 | static SkPaint::Hinting computeHinting(const SkPaint& paint) { |
| 1500 | SkPaint::Hinting h = paint.getHinting(); |
| 1501 | if (paint.isLinearText()) { |
| 1502 | h = SkPaint::kNo_Hinting; |
| 1503 | } |
| 1504 | return h; |
| 1505 | } |
| 1506 | |
reed@google.com | 1f6b4ae | 2011-11-22 14:20:55 +0000 | [diff] [blame] | 1507 | // return true if the paint is just a single color (i.e. not a shader). If its |
| 1508 | // a shader, then we can't compute a const luminance for it :( |
| 1509 | static bool justAColor(const SkPaint& paint, SkColor* color) { |
| 1510 | if (paint.getShader()) { |
| 1511 | return false; |
| 1512 | } |
| 1513 | SkColor c = paint.getColor(); |
| 1514 | if (paint.getColorFilter()) { |
| 1515 | c = paint.getColorFilter()->filterColor(c); |
| 1516 | } |
| 1517 | if (color) { |
| 1518 | *color = c; |
| 1519 | } |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
reed@google.com | ce6dbb6 | 2012-02-10 22:01:45 +0000 | [diff] [blame] | 1523 | static SkColor computeLuminanceColor(const SkPaint& paint) { |
| 1524 | SkColor c; |
| 1525 | if (!justAColor(paint, &c)) { |
| 1526 | c = SkColorSetRGB(0x7F, 0x80, 0x7F); |
| 1527 | } |
| 1528 | return c; |
| 1529 | } |
reed@google.com | 813d38b | 2012-02-13 21:37:57 +0000 | [diff] [blame] | 1530 | |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1531 | #define assert_byte(x) SkASSERT(0 == ((x) >> 8)) |
| 1532 | |
reed@google.com | 4f79b9b | 2011-09-13 13:23:26 +0000 | [diff] [blame] | 1533 | // Beyond this size, LCD doesn't appreciably improve quality, but it always |
| 1534 | // cost more RAM and draws slower, so we set a cap. |
reed@google.com | c27b741 | 2011-09-13 17:20:30 +0000 | [diff] [blame] | 1535 | #ifndef SK_MAX_SIZE_FOR_LCDTEXT |
| 1536 | #define SK_MAX_SIZE_FOR_LCDTEXT 48 |
| 1537 | #endif |
reed@google.com | 4f79b9b | 2011-09-13 13:23:26 +0000 | [diff] [blame] | 1538 | |
| 1539 | static bool tooBigForLCD(const SkScalerContext::Rec& rec) { |
| 1540 | SkScalar area = SkScalarMul(rec.fPost2x2[0][0], rec.fPost2x2[1][1]) - |
| 1541 | SkScalarMul(rec.fPost2x2[1][0], rec.fPost2x2[0][1]); |
| 1542 | SkScalar size = SkScalarMul(area, rec.fTextSize); |
reed@google.com | c27b741 | 2011-09-13 17:20:30 +0000 | [diff] [blame] | 1543 | return SkScalarAbs(size) > SkIntToScalar(SK_MAX_SIZE_FOR_LCDTEXT); |
reed@google.com | 4f79b9b | 2011-09-13 13:23:26 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1546 | /* |
| 1547 | * Return the scalar with only limited fractional precision. Used to consolidate matrices |
| 1548 | * that vary only slightly when we create our key into the font cache, since the font scaler |
| 1549 | * typically returns the same looking resuts for tiny changes in the matrix. |
| 1550 | */ |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1551 | static SkScalar sk_relax(SkScalar x) { |
| 1552 | int n = sk_float_round2int(x * 1024); |
| 1553 | return n / 1024.0f; |
| 1554 | } |
| 1555 | |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 1556 | void SkScalerContext::MakeRec(const SkPaint& paint, |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1557 | const SkDeviceProperties* deviceProperties, |
| 1558 | const SkMatrix* deviceMatrix, |
| 1559 | Rec* rec) { |
tomhudson@google.com | 8d43018 | 2011-06-06 19:11:19 +0000 | [diff] [blame] | 1560 | SkASSERT(deviceMatrix == NULL || !deviceMatrix->hasPerspective()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1561 | |
bungeman@google.com | cb1bbb3 | 2012-10-12 18:48:35 +0000 | [diff] [blame] | 1562 | SkTypeface* typeface = paint.getTypeface(); |
| 1563 | if (NULL == typeface) { |
| 1564 | typeface = SkTypeface::GetDefaultTypeface(); |
| 1565 | } |
| 1566 | rec->fOrigFontID = typeface->uniqueID(); |
reed@google.com | 7d26c59 | 2011-06-13 13:01:10 +0000 | [diff] [blame] | 1567 | rec->fFontID = rec->fOrigFontID; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1568 | rec->fTextSize = paint.getTextSize(); |
| 1569 | rec->fPreScaleX = paint.getTextScaleX(); |
| 1570 | rec->fPreSkewX = paint.getTextSkewX(); |
| 1571 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1572 | if (deviceMatrix) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1573 | rec->fPost2x2[0][0] = sk_relax(deviceMatrix->getScaleX()); |
| 1574 | rec->fPost2x2[0][1] = sk_relax(deviceMatrix->getSkewX()); |
| 1575 | rec->fPost2x2[1][0] = sk_relax(deviceMatrix->getSkewY()); |
| 1576 | rec->fPost2x2[1][1] = sk_relax(deviceMatrix->getScaleY()); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1577 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1578 | rec->fPost2x2[0][0] = rec->fPost2x2[1][1] = SK_Scalar1; |
| 1579 | rec->fPost2x2[0][1] = rec->fPost2x2[1][0] = 0; |
| 1580 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1581 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1582 | SkPaint::Style style = paint.getStyle(); |
| 1583 | SkScalar strokeWidth = paint.getStrokeWidth(); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1584 | |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 1585 | unsigned flags = 0; |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1586 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1587 | if (paint.isFakeBoldText()) { |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1588 | #ifdef SK_USE_FREETYPE_EMBOLDEN |
| 1589 | flags |= SkScalerContext::kEmbolden_Flag; |
| 1590 | #else |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1591 | SkScalar fakeBoldScale = SkScalarInterpFunc(paint.getTextSize(), |
| 1592 | kStdFakeBoldInterpKeys, |
| 1593 | kStdFakeBoldInterpValues, |
| 1594 | kStdFakeBoldInterpLength); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1595 | SkScalar extra = SkScalarMul(paint.getTextSize(), fakeBoldScale); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1596 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1597 | if (style == SkPaint::kFill_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1598 | style = SkPaint::kStrokeAndFill_Style; |
| 1599 | strokeWidth = extra; // ignore paint's strokeWidth if it was "fill" |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1600 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1601 | strokeWidth += extra; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1602 | } |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1603 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1606 | if (paint.isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1607 | flags |= SkScalerContext::kDevKernText_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1608 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1609 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1610 | if (style != SkPaint::kFill_Style && strokeWidth > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1611 | rec->fFrameWidth = strokeWidth; |
| 1612 | rec->fMiterLimit = paint.getStrokeMiter(); |
| 1613 | rec->fStrokeJoin = SkToU8(paint.getStrokeJoin()); |
| 1614 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1615 | if (style == SkPaint::kStrokeAndFill_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1616 | flags |= SkScalerContext::kFrameAndFill_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1617 | } |
| 1618 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1619 | rec->fFrameWidth = 0; |
| 1620 | rec->fMiterLimit = 0; |
| 1621 | rec->fStrokeJoin = 0; |
| 1622 | } |
| 1623 | |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1624 | rec->fMaskFormat = SkToU8(computeMaskFormat(paint)); |
| 1625 | |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1626 | SkDeviceProperties::Geometry geometry = deviceProperties |
| 1627 | ? deviceProperties->fGeometry |
| 1628 | : SkDeviceProperties::Geometry::MakeDefault(); |
| 1629 | if (SkMask::kLCD16_Format == rec->fMaskFormat || SkMask::kLCD32_Format == rec->fMaskFormat) { |
| 1630 | if (!geometry.isOrientationKnown() || !geometry.isLayoutKnown() || tooBigForLCD(*rec)) { |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1631 | // eeek, can't support LCD |
| 1632 | rec->fMaskFormat = SkMask::kA8_Format; |
| 1633 | } else { |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1634 | if (SkDeviceProperties::Geometry::kVertical_Orientation == geometry.getOrientation()) { |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1635 | flags |= SkScalerContext::kLCD_Vertical_Flag; |
| 1636 | } |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1637 | if (SkDeviceProperties::Geometry::kBGR_Layout == geometry.getLayout()) { |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1638 | flags |= SkScalerContext::kLCD_BGROrder_Flag; |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1643 | if (paint.isEmbeddedBitmapText()) { |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1644 | flags |= SkScalerContext::kEmbeddedBitmapText_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1645 | } |
| 1646 | if (paint.isSubpixelText()) { |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1647 | flags |= SkScalerContext::kSubpixelPositioning_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1648 | } |
| 1649 | if (paint.isAutohinted()) { |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1650 | flags |= SkScalerContext::kAutohinting_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 1651 | } |
reed@google.com | 830a23e | 2011-11-10 15:20:49 +0000 | [diff] [blame] | 1652 | if (paint.isVerticalText()) { |
| 1653 | flags |= SkScalerContext::kVertical_Flag; |
| 1654 | } |
reed@google.com | 8351aab | 2012-01-18 17:06:35 +0000 | [diff] [blame] | 1655 | if (paint.getFlags() & SkPaint::kGenA8FromLCD_Flag) { |
| 1656 | flags |= SkScalerContext::kGenA8FromLCD_Flag; |
| 1657 | } |
reed@google.com | 02b5331 | 2011-05-18 19:00:53 +0000 | [diff] [blame] | 1658 | rec->fFlags = SkToU16(flags); |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 1659 | |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 1660 | // these modify fFlags, so do them after assigning fFlags |
reed@google.com | 9d75767 | 2011-05-18 21:14:39 +0000 | [diff] [blame] | 1661 | rec->setHinting(computeHinting(paint)); |
| 1662 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1663 | rec->setLuminanceColor(computeLuminanceColor(paint)); |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (NULL == deviceProperties) { |
| 1666 | rec->setDeviceGamma(SK_GAMMA_EXPONENT); |
| 1667 | rec->setPaintGamma(SK_GAMMA_EXPONENT); |
| 1668 | } else { |
| 1669 | rec->setDeviceGamma(deviceProperties->fGamma); |
| 1670 | |
| 1671 | //For now always set the paint gamma equal to the device gamma. |
| 1672 | //The math in SkMaskGamma can handle them being different, |
| 1673 | //but it requires superluminous masks when |
| 1674 | //Ex : deviceGamma(x) < paintGamma(x) and x is sufficiently large. |
| 1675 | rec->setPaintGamma(deviceProperties->fGamma); |
| 1676 | } |
| 1677 | |
| 1678 | #ifdef SK_GAMMA_CONTRAST |
| 1679 | rec->setContrast(SK_GAMMA_CONTRAST); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1680 | #else |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1681 | /** |
| 1682 | * A value of 0.5 for SK_GAMMA_CONTRAST appears to be a good compromise. |
| 1683 | * With lower values small text appears washed out (though correctly so). |
| 1684 | * With higher values lcd fringing is worse and the smoothing effect of |
| 1685 | * partial coverage is diminished. |
| 1686 | */ |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 1687 | rec->setContrast(0.5f); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1688 | #endif |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1689 | |
bungeman@google.com | 2bf82d8 | 2012-08-02 10:06:19 +0000 | [diff] [blame] | 1690 | rec->fReservedAlign = 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1691 | |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 1692 | /* Allow the fonthost to modify our rec before we use it as a key into the |
| 1693 | cache. This way if we're asking for something that they will ignore, |
| 1694 | they can modify our rec up front, so we don't create duplicate cache |
| 1695 | entries. |
| 1696 | */ |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 1697 | typeface->onFilterRec(rec); |
reed@google.com | 3e8ae5b | 2011-09-28 15:32:20 +0000 | [diff] [blame] | 1698 | |
reed@google.com | 10d2d4d | 2012-03-01 22:32:51 +0000 | [diff] [blame] | 1699 | // be sure to call PostMakeRec(rec) before you actually use it! |
| 1700 | } |
| 1701 | |
| 1702 | /** |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1703 | * In order to call cachedDeviceLuminance, cachedPaintLuminance, or |
| 1704 | * cachedMaskGamma the caller must hold the gMaskGammaCacheMutex and continue |
| 1705 | * to hold it until the returned pointer is refed or forgotten. |
| 1706 | */ |
| 1707 | SK_DECLARE_STATIC_MUTEX(gMaskGammaCacheMutex); |
| 1708 | |
bungeman@google.com | ae30f56 | 2012-09-11 18:44:55 +0000 | [diff] [blame] | 1709 | static SkMaskGamma* gLinearMaskGamma = NULL; |
bungeman@google.com | b24b4fa | 2012-09-04 13:49:59 +0000 | [diff] [blame] | 1710 | static SkMaskGamma* gMaskGamma = NULL; |
| 1711 | static SkScalar gContrast = SK_ScalarMin; |
| 1712 | static SkScalar gPaintGamma = SK_ScalarMin; |
| 1713 | static SkScalar gDeviceGamma = SK_ScalarMin; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1714 | /** |
| 1715 | * The caller must hold the gMaskGammaCacheMutex and continue to hold it until |
| 1716 | * the returned SkMaskGamma pointer is refed or forgotten. |
| 1717 | */ |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1718 | static const SkMaskGamma& cachedMaskGamma(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma) { |
bungeman@google.com | ae30f56 | 2012-09-11 18:44:55 +0000 | [diff] [blame] | 1719 | if (0 == contrast && SK_Scalar1 == paintGamma && SK_Scalar1 == deviceGamma) { |
| 1720 | if (NULL == gLinearMaskGamma) { |
| 1721 | gLinearMaskGamma = SkNEW(SkMaskGamma); |
| 1722 | } |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1723 | return *gLinearMaskGamma; |
bungeman@google.com | ae30f56 | 2012-09-11 18:44:55 +0000 | [diff] [blame] | 1724 | } |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1725 | if (gContrast != contrast || gPaintGamma != paintGamma || gDeviceGamma != deviceGamma) { |
| 1726 | SkSafeUnref(gMaskGamma); |
bungeman@google.com | b1a72cb | 2012-10-04 22:03:41 +0000 | [diff] [blame] | 1727 | gMaskGamma = SkNEW_ARGS(SkMaskGamma, (contrast, paintGamma, deviceGamma)); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1728 | gContrast = contrast; |
| 1729 | gPaintGamma = paintGamma; |
| 1730 | gDeviceGamma = deviceGamma; |
| 1731 | } |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1732 | return *gMaskGamma; |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
bungeman@google.com | b24b4fa | 2012-09-04 13:49:59 +0000 | [diff] [blame] | 1735 | /*static*/ void SkPaint::Term() { |
| 1736 | SkAutoMutexAcquire ama(gMaskGammaCacheMutex); |
| 1737 | |
bungeman@google.com | ae30f56 | 2012-09-11 18:44:55 +0000 | [diff] [blame] | 1738 | SkSafeUnref(gLinearMaskGamma); |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 1739 | gLinearMaskGamma = NULL; |
bungeman@google.com | b24b4fa | 2012-09-04 13:49:59 +0000 | [diff] [blame] | 1740 | SkSafeUnref(gMaskGamma); |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 1741 | gMaskGamma = NULL; |
bungeman@google.com | b24b4fa | 2012-09-04 13:49:59 +0000 | [diff] [blame] | 1742 | SkDEBUGCODE(gContrast = SK_ScalarMin;) |
| 1743 | SkDEBUGCODE(gPaintGamma = SK_ScalarMin;) |
| 1744 | SkDEBUGCODE(gDeviceGamma = SK_ScalarMin;) |
bungeman@google.com | b24b4fa | 2012-09-04 13:49:59 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1747 | /** |
reed@google.com | 10d2d4d | 2012-03-01 22:32:51 +0000 | [diff] [blame] | 1748 | * We ensure that the rec is self-consistent and efficient (where possible) |
| 1749 | */ |
sugoi@google.com | 8d38d51 | 2013-02-26 21:56:19 +0000 | [diff] [blame] | 1750 | void SkScalerContext::PostMakeRec(const SkPaint&, SkScalerContext::Rec* rec) { |
reed@google.com | 10d2d4d | 2012-03-01 22:32:51 +0000 | [diff] [blame] | 1751 | /** |
| 1752 | * If we're asking for A8, we force the colorlum to be gray, since that |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1753 | * limits the number of unique entries, and the scaler will only look at |
| 1754 | * the lum of one of them. |
reed@google.com | 10d2d4d | 2012-03-01 22:32:51 +0000 | [diff] [blame] | 1755 | */ |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1756 | switch (rec->fMaskFormat) { |
| 1757 | case SkMask::kLCD16_Format: |
| 1758 | case SkMask::kLCD32_Format: { |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1759 | // filter down the luminance color to a finite number of bits |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1760 | SkColor color = rec->getLuminanceColor(); |
reed@google.com | b5715a1 | 2013-01-08 13:07:25 +0000 | [diff] [blame] | 1761 | rec->setLuminanceColor(SkMaskGamma::CanonicalColor(color)); |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1762 | break; |
| 1763 | } |
| 1764 | case SkMask::kA8_Format: { |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1765 | // filter down the luminance to a single component, since A8 can't |
| 1766 | // use per-component information |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1767 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1768 | SkColor color = rec->getLuminanceColor(); |
bungeman@google.com | b1a72cb | 2012-10-04 22:03:41 +0000 | [diff] [blame] | 1769 | U8CPU lum = SkColorSpaceLuminance::computeLuminance(rec->getPaintGamma(), color); |
bungeman@google.com | fd668cf | 2012-08-24 17:46:11 +0000 | [diff] [blame] | 1770 | //If we are asked to look like LCD, look like LCD. |
| 1771 | if (!(rec->fFlags & SkScalerContext::kGenA8FromLCD_Flag)) { |
| 1772 | // HACK: Prevents green from being pre-blended as white. |
| 1773 | lum -= ((255 - lum) * lum) / 255; |
| 1774 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1775 | |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1776 | // reduce to our finite number of bits |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1777 | color = SkColorSetRGB(lum, lum, lum); |
reed@google.com | b5715a1 | 2013-01-08 13:07:25 +0000 | [diff] [blame] | 1778 | rec->setLuminanceColor(SkMaskGamma::CanonicalColor(color)); |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1779 | break; |
| 1780 | } |
| 1781 | case SkMask::kBW_Format: |
| 1782 | // No need to differentiate gamma if we're BW |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1783 | rec->ignorePreBlend(); |
reed@google.com | dd43df9 | 2012-02-15 14:50:29 +0000 | [diff] [blame] | 1784 | break; |
reed@google.com | 3e8ae5b | 2011-09-28 15:32:20 +0000 | [diff] [blame] | 1785 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | #define MIN_SIZE_FOR_EFFECT_BUFFER 1024 |
| 1789 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 1790 | #ifdef SK_DEBUG |
| 1791 | #define TEST_DESC |
| 1792 | #endif |
| 1793 | |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 1794 | /* |
| 1795 | * ignoreGamma tells us that the caller just wants metrics that are unaffected |
| 1796 | * by gamma correction, so we jam the luminance field to 0 (most common value |
| 1797 | * for black text) in hopes that we get a cache hit easier. A better solution |
| 1798 | * would be for the fontcache lookup to know to ignore the luminance field |
| 1799 | * entirely, but not sure how to do that and keep it fast. |
| 1800 | */ |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1801 | void SkPaint::descriptorProc(const SkDeviceProperties* deviceProperties, |
| 1802 | const SkMatrix* deviceMatrix, |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 1803 | void (*proc)(SkTypeface*, const SkDescriptor*, void*), |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 1804 | void* context, bool ignoreGamma) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1805 | SkScalerContext::Rec rec; |
| 1806 | |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1807 | SkScalerContext::MakeRec(*this, deviceProperties, deviceMatrix, &rec); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 1808 | if (ignoreGamma) { |
reed@google.com | ce6dbb6 | 2012-02-10 22:01:45 +0000 | [diff] [blame] | 1809 | rec.setLuminanceColor(0); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 1810 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1811 | |
| 1812 | size_t descSize = sizeof(rec); |
| 1813 | int entryCount = 1; |
| 1814 | SkPathEffect* pe = this->getPathEffect(); |
| 1815 | SkMaskFilter* mf = this->getMaskFilter(); |
| 1816 | SkRasterizer* ra = this->getRasterizer(); |
| 1817 | |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 1818 | SkOrderedWriteBuffer peBuffer(MIN_SIZE_FOR_EFFECT_BUFFER); |
| 1819 | SkOrderedWriteBuffer mfBuffer(MIN_SIZE_FOR_EFFECT_BUFFER); |
| 1820 | SkOrderedWriteBuffer raBuffer(MIN_SIZE_FOR_EFFECT_BUFFER); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1821 | |
| 1822 | if (pe) { |
| 1823 | peBuffer.writeFlattenable(pe); |
| 1824 | descSize += peBuffer.size(); |
| 1825 | entryCount += 1; |
| 1826 | rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion |
| 1827 | // seems like we could support kLCD as well at this point... |
| 1828 | } |
| 1829 | if (mf) { |
| 1830 | mfBuffer.writeFlattenable(mf); |
| 1831 | descSize += mfBuffer.size(); |
| 1832 | entryCount += 1; |
| 1833 | rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing with maskfilters |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1834 | /* Pre-blend is not currently applied to filtered text. |
| 1835 | The primary filter is blur, for which contrast makes no sense, |
| 1836 | and for which the destination guess error is more visible. |
| 1837 | Also, all existing users of blur have calibrated for linear. */ |
| 1838 | rec.ignorePreBlend(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1839 | } |
| 1840 | if (ra) { |
| 1841 | raBuffer.writeFlattenable(ra); |
| 1842 | descSize += raBuffer.size(); |
| 1843 | entryCount += 1; |
| 1844 | rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion |
| 1845 | } |
reed@google.com | 10d2d4d | 2012-03-01 22:32:51 +0000 | [diff] [blame] | 1846 | |
commit-bot@chromium.org | c7a20e4 | 2013-05-13 14:09:13 +0000 | [diff] [blame] | 1847 | #ifdef SK_BUILD_FOR_ANDROID |
| 1848 | SkOrderedWriteBuffer androidBuffer(128); |
| 1849 | fPaintOptionsAndroid.flatten(androidBuffer); |
| 1850 | descSize += androidBuffer.size(); |
| 1851 | entryCount += 1; |
| 1852 | #endif |
| 1853 | |
reed@google.com | 10d2d4d | 2012-03-01 22:32:51 +0000 | [diff] [blame] | 1854 | /////////////////////////////////////////////////////////////////////////// |
| 1855 | // Now that we're done tweaking the rec, call the PostMakeRec cleanup |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1856 | SkScalerContext::PostMakeRec(*this, &rec); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1857 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1858 | descSize += SkDescriptor::ComputeOverhead(entryCount); |
| 1859 | |
| 1860 | SkAutoDescriptor ad(descSize); |
| 1861 | SkDescriptor* desc = ad.getDesc(); |
| 1862 | |
| 1863 | desc->init(); |
| 1864 | desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 1865 | |
commit-bot@chromium.org | c7a20e4 | 2013-05-13 14:09:13 +0000 | [diff] [blame] | 1866 | #ifdef SK_BUILD_FOR_ANDROID |
| 1867 | add_flattenable(desc, kAndroidOpts_SkDescriptorTag, &androidBuffer); |
| 1868 | #endif |
| 1869 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1870 | if (pe) { |
| 1871 | add_flattenable(desc, kPathEffect_SkDescriptorTag, &peBuffer); |
| 1872 | } |
| 1873 | if (mf) { |
| 1874 | add_flattenable(desc, kMaskFilter_SkDescriptorTag, &mfBuffer); |
| 1875 | } |
| 1876 | if (ra) { |
| 1877 | add_flattenable(desc, kRasterizer_SkDescriptorTag, &raBuffer); |
| 1878 | } |
| 1879 | |
| 1880 | SkASSERT(descSize == desc->getLength()); |
| 1881 | desc->computeChecksum(); |
| 1882 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 1883 | #ifdef TEST_DESC |
| 1884 | { |
| 1885 | // Check that we completely write the bytes in desc (our key), and that |
| 1886 | // there are no uninitialized bytes. If there were, then we would get |
| 1887 | // false-misses (or worse, false-hits) in our fontcache. |
| 1888 | // |
| 1889 | // We do this buy filling 2 others, one with 0s and the other with 1s |
| 1890 | // and create those, and then check that all 3 are identical. |
| 1891 | SkAutoDescriptor ad1(descSize); |
| 1892 | SkAutoDescriptor ad2(descSize); |
| 1893 | SkDescriptor* desc1 = ad1.getDesc(); |
| 1894 | SkDescriptor* desc2 = ad2.getDesc(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1895 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 1896 | memset(desc1, 0x00, descSize); |
| 1897 | memset(desc2, 0xFF, descSize); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1898 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 1899 | desc1->init(); |
| 1900 | desc2->init(); |
| 1901 | desc1->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 1902 | desc2->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1903 | |
commit-bot@chromium.org | c7a20e4 | 2013-05-13 14:09:13 +0000 | [diff] [blame] | 1904 | #ifdef SK_BUILD_FOR_ANDROID |
| 1905 | add_flattenable(desc1, kAndroidOpts_SkDescriptorTag, &androidBuffer); |
| 1906 | add_flattenable(desc2, kAndroidOpts_SkDescriptorTag, &androidBuffer); |
| 1907 | #endif |
| 1908 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 1909 | if (pe) { |
| 1910 | add_flattenable(desc1, kPathEffect_SkDescriptorTag, &peBuffer); |
| 1911 | add_flattenable(desc2, kPathEffect_SkDescriptorTag, &peBuffer); |
| 1912 | } |
| 1913 | if (mf) { |
| 1914 | add_flattenable(desc1, kMaskFilter_SkDescriptorTag, &mfBuffer); |
| 1915 | add_flattenable(desc2, kMaskFilter_SkDescriptorTag, &mfBuffer); |
| 1916 | } |
| 1917 | if (ra) { |
| 1918 | add_flattenable(desc1, kRasterizer_SkDescriptorTag, &raBuffer); |
| 1919 | add_flattenable(desc2, kRasterizer_SkDescriptorTag, &raBuffer); |
| 1920 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1921 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 1922 | SkASSERT(descSize == desc1->getLength()); |
| 1923 | SkASSERT(descSize == desc2->getLength()); |
| 1924 | desc1->computeChecksum(); |
| 1925 | desc2->computeChecksum(); |
| 1926 | SkASSERT(!memcmp(desc, desc1, descSize)); |
| 1927 | SkASSERT(!memcmp(desc, desc2, descSize)); |
| 1928 | } |
| 1929 | #endif |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1930 | |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 1931 | proc(fTypeface, desc, context); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1934 | SkGlyphCache* SkPaint::detachCache(const SkDeviceProperties* deviceProperties, |
| 1935 | const SkMatrix* deviceMatrix) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1936 | SkGlyphCache* cache; |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 1937 | this->descriptorProc(deviceProperties, deviceMatrix, DetachDescProc, &cache, false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1938 | return cache; |
| 1939 | } |
| 1940 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1941 | /** |
| 1942 | * Expands fDeviceGamma, fPaintGamma, fContrast, and fLumBits into a mask pre-blend. |
| 1943 | */ |
| 1944 | //static |
| 1945 | SkMaskGamma::PreBlend SkScalerContext::GetMaskPreBlend(const SkScalerContext::Rec& rec) { |
| 1946 | SkAutoMutexAcquire ama(gMaskGammaCacheMutex); |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1947 | const SkMaskGamma& maskGamma = cachedMaskGamma(rec.getContrast(), |
| 1948 | rec.getPaintGamma(), |
| 1949 | rec.getDeviceGamma()); |
| 1950 | return maskGamma.preBlend(rec.getLuminanceColor()); |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1953 | /////////////////////////////////////////////////////////////////////////////// |
| 1954 | |
| 1955 | #include "SkStream.h" |
| 1956 | |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1957 | static uintptr_t asint(const void* p) { |
| 1958 | return reinterpret_cast<uintptr_t>(p); |
| 1959 | } |
| 1960 | |
| 1961 | union Scalar32 { |
| 1962 | SkScalar fScalar; |
| 1963 | uint32_t f32; |
| 1964 | }; |
| 1965 | |
| 1966 | static uint32_t* write_scalar(uint32_t* ptr, SkScalar value) { |
| 1967 | SkASSERT(sizeof(SkScalar) == sizeof(uint32_t)); |
| 1968 | Scalar32 tmp; |
| 1969 | tmp.fScalar = value; |
| 1970 | *ptr = tmp.f32; |
| 1971 | return ptr + 1; |
| 1972 | } |
| 1973 | |
| 1974 | static SkScalar read_scalar(const uint32_t*& ptr) { |
| 1975 | SkASSERT(sizeof(SkScalar) == sizeof(uint32_t)); |
| 1976 | Scalar32 tmp; |
| 1977 | tmp.f32 = *ptr++; |
| 1978 | return tmp.fScalar; |
| 1979 | } |
| 1980 | |
| 1981 | static uint32_t pack_4(unsigned a, unsigned b, unsigned c, unsigned d) { |
| 1982 | SkASSERT(a == (uint8_t)a); |
| 1983 | SkASSERT(b == (uint8_t)b); |
| 1984 | SkASSERT(c == (uint8_t)c); |
| 1985 | SkASSERT(d == (uint8_t)d); |
| 1986 | return (a << 24) | (b << 16) | (c << 8) | d; |
| 1987 | } |
| 1988 | |
| 1989 | enum FlatFlags { |
commit-bot@chromium.org | 97f8167 | 2013-09-26 15:16:12 +0000 | [diff] [blame] | 1990 | kHasTypeface_FlatFlag = 0x01, |
| 1991 | kHasEffects_FlatFlag = 0x02, |
| 1992 | kHasNonDefaultPaintOptionsAndroid_FlatFlag = 0x04, |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1993 | }; |
| 1994 | |
| 1995 | // The size of a flat paint's POD fields |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 1996 | static const uint32_t kPODPaintSize = 5 * sizeof(SkScalar) + |
reed@google.com | 1f1543f | 2012-09-12 21:08:33 +0000 | [diff] [blame] | 1997 | 1 * sizeof(SkColor) + |
| 1998 | 1 * sizeof(uint16_t) + |
| 1999 | 6 * sizeof(uint8_t); |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2000 | |
| 2001 | /* To save space/time, we analyze the paint, and write a truncated version of |
| 2002 | it if there are not tricky elements like shaders, etc. |
| 2003 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2004 | void SkPaint::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2005 | uint8_t flatFlags = 0; |
| 2006 | if (this->getTypeface()) { |
| 2007 | flatFlags |= kHasTypeface_FlatFlag; |
| 2008 | } |
| 2009 | if (asint(this->getPathEffect()) | |
| 2010 | asint(this->getShader()) | |
| 2011 | asint(this->getXfermode()) | |
| 2012 | asint(this->getMaskFilter()) | |
| 2013 | asint(this->getColorFilter()) | |
| 2014 | asint(this->getRasterizer()) | |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 2015 | asint(this->getLooper()) | |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 2016 | asint(this->getAnnotation()) | |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 2017 | asint(this->getImageFilter())) { |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2018 | flatFlags |= kHasEffects_FlatFlag; |
| 2019 | } |
commit-bot@chromium.org | 80f3fcd | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 2020 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | 97f8167 | 2013-09-26 15:16:12 +0000 | [diff] [blame] | 2021 | if (this->getPaintOptionsAndroid() != SkPaintOptionsAndroid()) { |
| 2022 | flatFlags |= kHasNonDefaultPaintOptionsAndroid_FlatFlag; |
| 2023 | } |
| 2024 | #endif |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2025 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2026 | if (buffer.isOrderedBinaryBuffer()) { |
| 2027 | SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); |
| 2028 | uint32_t* ptr = buffer.getOrderedBinaryBuffer()->reserve(kPODPaintSize); |
| 2029 | |
| 2030 | ptr = write_scalar(ptr, this->getTextSize()); |
| 2031 | ptr = write_scalar(ptr, this->getTextScaleX()); |
| 2032 | ptr = write_scalar(ptr, this->getTextSkewX()); |
| 2033 | ptr = write_scalar(ptr, this->getStrokeWidth()); |
| 2034 | ptr = write_scalar(ptr, this->getStrokeMiter()); |
| 2035 | *ptr++ = this->getColor(); |
| 2036 | // previously flags:16, textAlign:8, flatFlags:8 |
| 2037 | // now flags:16, hinting:4, textAlign:4, flatFlags:8 |
| 2038 | *ptr++ = (this->getFlags() << 16) | |
| 2039 | // hinting added later. 0 in this nibble means use the default. |
| 2040 | ((this->getHinting()+1) << 12) | |
| 2041 | (this->getTextAlign() << 8) | |
| 2042 | flatFlags; |
| 2043 | *ptr++ = pack_4(this->getStrokeCap(), this->getStrokeJoin(), |
| 2044 | this->getStyle(), this->getTextEncoding()); |
| 2045 | } else { |
| 2046 | buffer.writeScalar(fTextSize); |
| 2047 | buffer.writeScalar(fTextScaleX); |
| 2048 | buffer.writeScalar(fTextSkewX); |
| 2049 | buffer.writeScalar(fWidth); |
| 2050 | buffer.writeScalar(fMiterLimit); |
| 2051 | buffer.writeColor(fColor); |
| 2052 | buffer.writeUInt(fFlags); |
| 2053 | buffer.writeUInt(fHinting); |
| 2054 | buffer.writeUInt(fTextAlign); |
| 2055 | buffer.writeUInt(flatFlags); |
| 2056 | |
| 2057 | buffer.writeUInt(fCapType); |
| 2058 | buffer.writeUInt(fJoinType); |
| 2059 | buffer.writeUInt(fStyle); |
| 2060 | buffer.writeUInt(fTextEncoding); |
| 2061 | } |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2062 | |
| 2063 | // now we're done with ptr and the (pre)reserved space. If we need to write |
| 2064 | // additional fields, use the buffer directly |
| 2065 | if (flatFlags & kHasTypeface_FlatFlag) { |
| 2066 | buffer.writeTypeface(this->getTypeface()); |
| 2067 | } |
| 2068 | if (flatFlags & kHasEffects_FlatFlag) { |
| 2069 | buffer.writeFlattenable(this->getPathEffect()); |
| 2070 | buffer.writeFlattenable(this->getShader()); |
| 2071 | buffer.writeFlattenable(this->getXfermode()); |
| 2072 | buffer.writeFlattenable(this->getMaskFilter()); |
| 2073 | buffer.writeFlattenable(this->getColorFilter()); |
| 2074 | buffer.writeFlattenable(this->getRasterizer()); |
| 2075 | buffer.writeFlattenable(this->getLooper()); |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 2076 | buffer.writeFlattenable(this->getImageFilter()); |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 2077 | |
| 2078 | if (fAnnotation) { |
| 2079 | buffer.writeBool(true); |
| 2080 | fAnnotation->writeToBuffer(buffer); |
| 2081 | } else { |
| 2082 | buffer.writeBool(false); |
| 2083 | } |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2084 | } |
commit-bot@chromium.org | 80f3fcd | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 2085 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | 97f8167 | 2013-09-26 15:16:12 +0000 | [diff] [blame] | 2086 | if (flatFlags & kHasNonDefaultPaintOptionsAndroid_FlatFlag) { |
| 2087 | this->getPaintOptionsAndroid().flatten(buffer); |
| 2088 | } |
| 2089 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | void SkPaint::unflatten(SkFlattenableReadBuffer& buffer) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2093 | uint8_t flatFlags = 0; |
| 2094 | if (buffer.isOrderedBinaryBuffer()) { |
| 2095 | SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); |
| 2096 | const void* podData = buffer.getOrderedBinaryBuffer()->skip(kPODPaintSize); |
| 2097 | const uint32_t* pod = reinterpret_cast<const uint32_t*>(podData); |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2098 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2099 | // the order we read must match the order we wrote in flatten() |
| 2100 | this->setTextSize(read_scalar(pod)); |
| 2101 | this->setTextScaleX(read_scalar(pod)); |
| 2102 | this->setTextSkewX(read_scalar(pod)); |
| 2103 | this->setStrokeWidth(read_scalar(pod)); |
| 2104 | this->setStrokeMiter(read_scalar(pod)); |
| 2105 | this->setColor(*pod++); |
bungeman@google.com | 24babf4 | 2011-11-07 16:33:40 +0000 | [diff] [blame] | 2106 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2107 | // previously flags:16, textAlign:8, flatFlags:8 |
| 2108 | // now flags:16, hinting:4, textAlign:4, flatFlags:8 |
| 2109 | uint32_t tmp = *pod++; |
| 2110 | this->setFlags(tmp >> 16); |
bungeman@google.com | 24babf4 | 2011-11-07 16:33:40 +0000 | [diff] [blame] | 2111 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2112 | // hinting added later. 0 in this nibble means use the default. |
| 2113 | uint32_t hinting = (tmp >> 12) & 0xF; |
| 2114 | this->setHinting(0 == hinting ? kNormal_Hinting : static_cast<Hinting>(hinting-1)); |
bungeman@google.com | 24babf4 | 2011-11-07 16:33:40 +0000 | [diff] [blame] | 2115 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2116 | this->setTextAlign(static_cast<Align>((tmp >> 8) & 0xF)); |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2117 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2118 | flatFlags = tmp & 0xFF; |
| 2119 | |
| 2120 | tmp = *pod++; |
| 2121 | this->setStrokeCap(static_cast<Cap>((tmp >> 24) & 0xFF)); |
| 2122 | this->setStrokeJoin(static_cast<Join>((tmp >> 16) & 0xFF)); |
| 2123 | this->setStyle(static_cast<Style>((tmp >> 8) & 0xFF)); |
| 2124 | this->setTextEncoding(static_cast<TextEncoding>((tmp >> 0) & 0xFF)); |
| 2125 | } else { |
| 2126 | this->setTextSize(buffer.readScalar()); |
| 2127 | this->setTextScaleX(buffer.readScalar()); |
| 2128 | this->setTextSkewX(buffer.readScalar()); |
scroggo@google.com | 5cd3656 | 2012-10-09 20:02:20 +0000 | [diff] [blame] | 2129 | // Skip the hinting scalar factor, which is not supported. |
| 2130 | buffer.readScalar(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2131 | this->setStrokeWidth(buffer.readScalar()); |
| 2132 | this->setStrokeMiter(buffer.readScalar()); |
| 2133 | this->setColor(buffer.readColor()); |
| 2134 | this->setFlags(buffer.readUInt()); |
| 2135 | this->setHinting(static_cast<SkPaint::Hinting>(buffer.readUInt())); |
| 2136 | this->setTextAlign(static_cast<SkPaint::Align>(buffer.readUInt())); |
| 2137 | flatFlags = buffer.readUInt(); |
| 2138 | |
| 2139 | this->setStrokeCap(static_cast<SkPaint::Cap>(buffer.readUInt())); |
| 2140 | this->setStrokeJoin(static_cast<SkPaint::Join>(buffer.readUInt())); |
| 2141 | this->setStyle(static_cast<SkPaint::Style>(buffer.readUInt())); |
| 2142 | this->setTextEncoding(static_cast<SkPaint::TextEncoding>(buffer.readUInt())); |
| 2143 | } |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2144 | |
| 2145 | if (flatFlags & kHasTypeface_FlatFlag) { |
| 2146 | this->setTypeface(buffer.readTypeface()); |
| 2147 | } else { |
| 2148 | this->setTypeface(NULL); |
| 2149 | } |
| 2150 | |
| 2151 | if (flatFlags & kHasEffects_FlatFlag) { |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 2152 | SkSafeUnref(this->setPathEffect(buffer.readPathEffect())); |
| 2153 | SkSafeUnref(this->setShader(buffer.readShader())); |
| 2154 | SkSafeUnref(this->setXfermode(buffer.readXfermode())); |
| 2155 | SkSafeUnref(this->setMaskFilter(buffer.readMaskFilter())); |
| 2156 | SkSafeUnref(this->setColorFilter(buffer.readColorFilter())); |
| 2157 | SkSafeUnref(this->setRasterizer(buffer.readRasterizer())); |
| 2158 | SkSafeUnref(this->setLooper(buffer.readDrawLooper())); |
| 2159 | SkSafeUnref(this->setImageFilter(buffer.readImageFilter())); |
skia.committer@gmail.com | fbc58a3 | 2013-10-15 07:02:27 +0000 | [diff] [blame] | 2160 | |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 2161 | if (buffer.readBool()) { |
| 2162 | this->setAnnotation(SkNEW_ARGS(SkAnnotation, (buffer)))->unref(); |
| 2163 | } |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2164 | } else { |
| 2165 | this->setPathEffect(NULL); |
| 2166 | this->setShader(NULL); |
| 2167 | this->setXfermode(NULL); |
| 2168 | this->setMaskFilter(NULL); |
| 2169 | this->setColorFilter(NULL); |
| 2170 | this->setRasterizer(NULL); |
| 2171 | this->setLooper(NULL); |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 2172 | this->setImageFilter(NULL); |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 2173 | } |
commit-bot@chromium.org | 97f8167 | 2013-09-26 15:16:12 +0000 | [diff] [blame] | 2174 | |
commit-bot@chromium.org | 80f3fcd | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 2175 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | 97f8167 | 2013-09-26 15:16:12 +0000 | [diff] [blame] | 2176 | this->setPaintOptionsAndroid(SkPaintOptionsAndroid()); |
| 2177 | #endif |
| 2178 | if (flatFlags & kHasNonDefaultPaintOptionsAndroid_FlatFlag) { |
| 2179 | SkPaintOptionsAndroid options; |
| 2180 | options.unflatten(buffer); |
commit-bot@chromium.org | 80f3fcd | 2013-10-08 16:24:07 +0000 | [diff] [blame] | 2181 | #ifdef SK_BUILD_FOR_ANDROID |
commit-bot@chromium.org | 97f8167 | 2013-09-26 15:16:12 +0000 | [diff] [blame] | 2182 | this->setPaintOptionsAndroid(options); |
| 2183 | #endif |
| 2184 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /////////////////////////////////////////////////////////////////////////////// |
| 2188 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 2189 | SkShader* SkPaint::setShader(SkShader* shader) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 2190 | GEN_ID_INC_EVAL(shader != fShader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2191 | SkRefCnt_SafeAssign(fShader, shader); |
| 2192 | return shader; |
| 2193 | } |
| 2194 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 2195 | SkColorFilter* SkPaint::setColorFilter(SkColorFilter* filter) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 2196 | GEN_ID_INC_EVAL(filter != fColorFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2197 | SkRefCnt_SafeAssign(fColorFilter, filter); |
| 2198 | return filter; |
| 2199 | } |
| 2200 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 2201 | SkXfermode* SkPaint::setXfermode(SkXfermode* mode) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 2202 | GEN_ID_INC_EVAL(mode != fXfermode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2203 | SkRefCnt_SafeAssign(fXfermode, mode); |
| 2204 | return mode; |
| 2205 | } |
| 2206 | |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 2207 | SkXfermode* SkPaint::setXfermodeMode(SkXfermode::Mode mode) { |
reed@android.com | d66eef7 | 2009-06-24 12:29:16 +0000 | [diff] [blame] | 2208 | SkSafeUnref(fXfermode); |
| 2209 | fXfermode = SkXfermode::Create(mode); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 2210 | GEN_ID_INC; |
reed@android.com | d66eef7 | 2009-06-24 12:29:16 +0000 | [diff] [blame] | 2211 | return fXfermode; |
| 2212 | } |
| 2213 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 2214 | SkPathEffect* SkPaint::setPathEffect(SkPathEffect* effect) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 2215 | GEN_ID_INC_EVAL(effect != fPathEffect); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2216 | SkRefCnt_SafeAssign(fPathEffect, effect); |
| 2217 | return effect; |
| 2218 | } |
| 2219 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 2220 | SkMaskFilter* SkPaint::setMaskFilter(SkMaskFilter* filter) { |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 2221 | GEN_ID_INC_EVAL(filter != fMaskFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2222 | SkRefCnt_SafeAssign(fMaskFilter, filter); |
| 2223 | return filter; |
| 2224 | } |
| 2225 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 2226 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2227 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 2228 | bool SkPaint::getFillPath(const SkPath& src, SkPath* dst, |
| 2229 | const SkRect* cullRect) const { |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 2230 | SkStrokeRec rec(*this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2231 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 2232 | const SkPath* srcPtr = &src; |
| 2233 | SkPath tmpPath; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 2234 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 2235 | if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec, cullRect)) { |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 2236 | srcPtr = &tmpPath; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 2239 | if (!rec.applyToPath(dst, *srcPtr)) { |
| 2240 | if (srcPtr == &tmpPath) { |
| 2241 | // If path's were copy-on-write, this trick would not be needed. |
| 2242 | // As it is, we want to save making a deep-copy from tmpPath -> dst |
| 2243 | // since we know we're just going to delete tmpPath when we return, |
| 2244 | // so the swap saves that copy. |
| 2245 | dst->swap(tmpPath); |
| 2246 | } else { |
| 2247 | *dst = *srcPtr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2248 | } |
| 2249 | } |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 2250 | return !rec.isHairlineStyle(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
reed@google.com | e4f10a7 | 2012-05-15 20:47:50 +0000 | [diff] [blame] | 2253 | const SkRect& SkPaint::doComputeFastBounds(const SkRect& origSrc, |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2254 | SkRect* storage, |
| 2255 | Style style) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2256 | SkASSERT(storage); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 2257 | |
reed@google.com | e4f10a7 | 2012-05-15 20:47:50 +0000 | [diff] [blame] | 2258 | const SkRect* src = &origSrc; |
| 2259 | |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 2260 | if (this->getLooper()) { |
| 2261 | SkASSERT(this->getLooper()->canComputeFastBounds(*this)); |
reed@google.com | e4f10a7 | 2012-05-15 20:47:50 +0000 | [diff] [blame] | 2262 | this->getLooper()->computeFastBounds(*this, *src, storage); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 2263 | return *storage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2264 | } |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 2265 | |
reed@google.com | e4f10a7 | 2012-05-15 20:47:50 +0000 | [diff] [blame] | 2266 | SkRect tmpSrc; |
| 2267 | if (this->getPathEffect()) { |
| 2268 | this->getPathEffect()->computeFastBounds(&tmpSrc, origSrc); |
| 2269 | src = &tmpSrc; |
| 2270 | } |
| 2271 | |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2272 | if (kFill_Style != style) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 2273 | // since we're stroked, outset the rect by the radius (and join type) |
| 2274 | SkScalar radius = SkScalarHalf(this->getStrokeWidth()); |
| 2275 | if (0 == radius) { // hairline |
| 2276 | radius = SK_Scalar1; |
| 2277 | } else if (this->getStrokeJoin() == SkPaint::kMiter_Join) { |
| 2278 | SkScalar scale = this->getStrokeMiter(); |
| 2279 | if (scale > SK_Scalar1) { |
| 2280 | radius = SkScalarMul(radius, scale); |
| 2281 | } |
| 2282 | } |
reed@google.com | e4f10a7 | 2012-05-15 20:47:50 +0000 | [diff] [blame] | 2283 | storage->set(src->fLeft - radius, src->fTop - radius, |
| 2284 | src->fRight + radius, src->fBottom + radius); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 2285 | } else { |
reed@google.com | e4f10a7 | 2012-05-15 20:47:50 +0000 | [diff] [blame] | 2286 | *storage = *src; |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 2289 | if (this->getMaskFilter()) { |
| 2290 | this->getMaskFilter()->computeFastBounds(*storage, storage); |
| 2291 | } |
| 2292 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 2293 | return *storage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2296 | #ifdef SK_DEVELOPER |
| 2297 | void SkPaint::toString(SkString* str) const { |
| 2298 | str->append("<dl><dt>SkPaint:</dt><dd><dl>"); |
| 2299 | |
| 2300 | SkTypeface* typeface = this->getTypeface(); |
| 2301 | if (NULL != typeface) { |
| 2302 | SkDynamicMemoryWStream ostream; |
| 2303 | typeface->serialize(&ostream); |
| 2304 | SkAutoTUnref<SkData> data(ostream.copyToData()); |
| 2305 | |
| 2306 | SkMemoryStream stream(data); |
| 2307 | SkFontDescriptor descriptor(&stream); |
| 2308 | |
| 2309 | str->append("<dt>Font Family Name:</dt><dd>"); |
| 2310 | str->append(descriptor.getFamilyName()); |
| 2311 | str->append("</dd><dt>Font Full Name:</dt><dd>"); |
| 2312 | str->append(descriptor.getFullName()); |
| 2313 | str->append("</dd><dt>Font PS Name:</dt><dd>"); |
| 2314 | str->append(descriptor.getPostscriptName()); |
| 2315 | str->append("</dd><dt>Font File Name:</dt><dd>"); |
| 2316 | str->append(descriptor.getFontFileName()); |
| 2317 | str->append("</dd>"); |
| 2318 | } |
| 2319 | |
| 2320 | str->append("<dt>TextSize:</dt><dd>"); |
| 2321 | str->appendScalar(this->getTextSize()); |
| 2322 | str->append("</dd>"); |
| 2323 | |
| 2324 | str->append("<dt>TextScaleX:</dt><dd>"); |
| 2325 | str->appendScalar(this->getTextScaleX()); |
| 2326 | str->append("</dd>"); |
| 2327 | |
| 2328 | str->append("<dt>TextSkewX:</dt><dd>"); |
| 2329 | str->appendScalar(this->getTextSkewX()); |
| 2330 | str->append("</dd>"); |
| 2331 | |
| 2332 | SkPathEffect* pathEffect = this->getPathEffect(); |
| 2333 | if (NULL != pathEffect) { |
| 2334 | str->append("<dt>PathEffect:</dt><dd>"); |
| 2335 | str->append("</dd>"); |
| 2336 | } |
| 2337 | |
| 2338 | SkShader* shader = this->getShader(); |
| 2339 | if (NULL != shader) { |
| 2340 | str->append("<dt>Shader:</dt><dd>"); |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 2341 | shader->toString(str); |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2342 | str->append("</dd>"); |
| 2343 | } |
| 2344 | |
| 2345 | SkXfermode* xfer = this->getXfermode(); |
| 2346 | if (NULL != xfer) { |
| 2347 | str->append("<dt>Xfermode:</dt><dd>"); |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 2348 | xfer->toString(str); |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2349 | str->append("</dd>"); |
| 2350 | } |
| 2351 | |
| 2352 | SkMaskFilter* maskFilter = this->getMaskFilter(); |
| 2353 | if (NULL != maskFilter) { |
| 2354 | str->append("<dt>MaskFilter:</dt><dd>"); |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 2355 | maskFilter->toString(str); |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2356 | str->append("</dd>"); |
| 2357 | } |
| 2358 | |
| 2359 | SkColorFilter* colorFilter = this->getColorFilter(); |
| 2360 | if (NULL != colorFilter) { |
| 2361 | str->append("<dt>ColorFilter:</dt><dd>"); |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 2362 | colorFilter->toString(str); |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2363 | str->append("</dd>"); |
| 2364 | } |
| 2365 | |
| 2366 | SkRasterizer* rasterizer = this->getRasterizer(); |
| 2367 | if (NULL != rasterizer) { |
| 2368 | str->append("<dt>Rasterizer:</dt><dd>"); |
| 2369 | str->append("</dd>"); |
| 2370 | } |
| 2371 | |
| 2372 | SkDrawLooper* looper = this->getLooper(); |
| 2373 | if (NULL != looper) { |
| 2374 | str->append("<dt>DrawLooper:</dt><dd>"); |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 2375 | looper->toString(str); |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2376 | str->append("</dd>"); |
| 2377 | } |
| 2378 | |
| 2379 | SkImageFilter* imageFilter = this->getImageFilter(); |
| 2380 | if (NULL != imageFilter) { |
| 2381 | str->append("<dt>ImageFilter:</dt><dd>"); |
| 2382 | str->append("</dd>"); |
| 2383 | } |
| 2384 | |
| 2385 | SkAnnotation* annotation = this->getAnnotation(); |
| 2386 | if (NULL != annotation) { |
| 2387 | str->append("<dt>Annotation:</dt><dd>"); |
| 2388 | str->append("</dd>"); |
| 2389 | } |
| 2390 | |
| 2391 | str->append("<dt>Color:</dt><dd>0x"); |
| 2392 | SkColor color = this->getColor(); |
| 2393 | str->appendHex(color); |
| 2394 | str->append("</dd>"); |
| 2395 | |
| 2396 | str->append("<dt>Stroke Width:</dt><dd>"); |
| 2397 | str->appendScalar(this->getStrokeWidth()); |
| 2398 | str->append("</dd>"); |
| 2399 | |
| 2400 | str->append("<dt>Stroke Miter:</dt><dd>"); |
| 2401 | str->appendScalar(this->getStrokeMiter()); |
| 2402 | str->append("</dd>"); |
| 2403 | |
| 2404 | str->append("<dt>Flags:</dt><dd>("); |
| 2405 | if (this->getFlags()) { |
| 2406 | bool needSeparator = false; |
| 2407 | SkAddFlagToString(str, this->isAntiAlias(), "AntiAlias", &needSeparator); |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2408 | SkAddFlagToString(str, this->isDither(), "Dither", &needSeparator); |
| 2409 | SkAddFlagToString(str, this->isUnderlineText(), "UnderlineText", &needSeparator); |
| 2410 | SkAddFlagToString(str, this->isStrikeThruText(), "StrikeThruText", &needSeparator); |
| 2411 | SkAddFlagToString(str, this->isFakeBoldText(), "FakeBoldText", &needSeparator); |
| 2412 | SkAddFlagToString(str, this->isLinearText(), "LinearText", &needSeparator); |
| 2413 | SkAddFlagToString(str, this->isSubpixelText(), "SubpixelText", &needSeparator); |
| 2414 | SkAddFlagToString(str, this->isDevKernText(), "DevKernText", &needSeparator); |
| 2415 | SkAddFlagToString(str, this->isLCDRenderText(), "LCDRenderText", &needSeparator); |
| 2416 | SkAddFlagToString(str, this->isEmbeddedBitmapText(), |
| 2417 | "EmbeddedBitmapText", &needSeparator); |
| 2418 | SkAddFlagToString(str, this->isAutohinted(), "Autohinted", &needSeparator); |
| 2419 | SkAddFlagToString(str, this->isVerticalText(), "VerticalText", &needSeparator); |
| 2420 | SkAddFlagToString(str, SkToBool(this->getFlags() & SkPaint::kGenA8FromLCD_Flag), |
| 2421 | "GenA8FromLCD", &needSeparator); |
| 2422 | } else { |
| 2423 | str->append("None"); |
| 2424 | } |
| 2425 | str->append(")</dd>"); |
| 2426 | |
robertphillips@google.com | 3e7e992 | 2013-10-16 17:53:18 +0000 | [diff] [blame] | 2427 | str->append("<dt>FilterLevel:</dt><dd>"); |
| 2428 | static const char* gFilterLevelStrings[] = { "None", "Low", "Medium", "High" }; |
| 2429 | str->append(gFilterLevelStrings[this->getFilterLevel()]); |
| 2430 | str->append("</dd>"); |
| 2431 | |
robertphillips@google.com | 791f12e | 2013-02-14 13:53:53 +0000 | [diff] [blame] | 2432 | str->append("<dt>TextAlign:</dt><dd>"); |
| 2433 | static const char* gTextAlignStrings[SkPaint::kAlignCount] = { "Left", "Center", "Right" }; |
| 2434 | str->append(gTextAlignStrings[this->getTextAlign()]); |
| 2435 | str->append("</dd>"); |
| 2436 | |
| 2437 | str->append("<dt>CapType:</dt><dd>"); |
| 2438 | static const char* gStrokeCapStrings[SkPaint::kCapCount] = { "Butt", "Round", "Square" }; |
| 2439 | str->append(gStrokeCapStrings[this->getStrokeCap()]); |
| 2440 | str->append("</dd>"); |
| 2441 | |
| 2442 | str->append("<dt>JoinType:</dt><dd>"); |
| 2443 | static const char* gJoinStrings[SkPaint::kJoinCount] = { "Miter", "Round", "Bevel" }; |
| 2444 | str->append(gJoinStrings[this->getStrokeJoin()]); |
| 2445 | str->append("</dd>"); |
| 2446 | |
| 2447 | str->append("<dt>Style:</dt><dd>"); |
| 2448 | static const char* gStyleStrings[SkPaint::kStyleCount] = { "Fill", "Stroke", "StrokeAndFill" }; |
| 2449 | str->append(gStyleStrings[this->getStyle()]); |
| 2450 | str->append("</dd>"); |
| 2451 | |
| 2452 | str->append("<dt>TextEncoding:</dt><dd>"); |
| 2453 | static const char* gTextEncodingStrings[] = { "UTF8", "UTF16", "UTF32", "GlyphID" }; |
| 2454 | str->append(gTextEncodingStrings[this->getTextEncoding()]); |
| 2455 | str->append("</dd>"); |
| 2456 | |
| 2457 | str->append("<dt>Hinting:</dt><dd>"); |
| 2458 | static const char* gHintingStrings[] = { "None", "Slight", "Normal", "Full" }; |
| 2459 | str->append(gHintingStrings[this->getHinting()]); |
| 2460 | str->append("</dd>"); |
| 2461 | |
| 2462 | str->append("</dd></dl></dl>"); |
| 2463 | } |
| 2464 | #endif |
| 2465 | |
| 2466 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2467 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2468 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2469 | static bool has_thick_frame(const SkPaint& paint) { |
| 2470 | return paint.getStrokeWidth() > 0 && |
| 2471 | paint.getStyle() != SkPaint::kFill_Style; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | SkTextToPathIter::SkTextToPathIter( const char text[], size_t length, |
| 2475 | const SkPaint& paint, |
djsollen@google.com | 166e653 | 2012-03-20 14:24:38 +0000 | [diff] [blame] | 2476 | bool applyStrokeAndPathEffects) |
| 2477 | : fPaint(paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2478 | fGlyphCacheProc = paint.getMeasureCacheProc(SkPaint::kForward_TextBufferDirection, |
| 2479 | true); |
| 2480 | |
djsollen@google.com | 166e653 | 2012-03-20 14:24:38 +0000 | [diff] [blame] | 2481 | fPaint.setLinearText(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2482 | fPaint.setMaskFilter(NULL); // don't want this affecting our path-cache lookup |
| 2483 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2484 | if (fPaint.getPathEffect() == NULL && !has_thick_frame(fPaint)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2485 | applyStrokeAndPathEffects = false; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2486 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2487 | |
djsollen@google.com | 166e653 | 2012-03-20 14:24:38 +0000 | [diff] [blame] | 2488 | // can't use our canonical size if we need to apply patheffects |
| 2489 | if (fPaint.getPathEffect() == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2490 | fPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 2491 | fScale = paint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
djsollen@google.com | 166e653 | 2012-03-20 14:24:38 +0000 | [diff] [blame] | 2492 | if (has_thick_frame(fPaint)) { |
| 2493 | fPaint.setStrokeWidth(SkScalarDiv(fPaint.getStrokeWidth(), fScale)); |
| 2494 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2495 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2496 | fScale = SK_Scalar1; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2497 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 2498 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2499 | if (!applyStrokeAndPathEffects) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2500 | fPaint.setStyle(SkPaint::kFill_Style); |
| 2501 | fPaint.setPathEffect(NULL); |
| 2502 | } |
| 2503 | |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 2504 | fCache = fPaint.detachCache(NULL, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2505 | |
| 2506 | SkPaint::Style style = SkPaint::kFill_Style; |
| 2507 | SkPathEffect* pe = NULL; |
| 2508 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2509 | if (!applyStrokeAndPathEffects) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2510 | style = paint.getStyle(); // restore |
| 2511 | pe = paint.getPathEffect(); // restore |
| 2512 | } |
| 2513 | fPaint.setStyle(style); |
| 2514 | fPaint.setPathEffect(pe); |
| 2515 | fPaint.setMaskFilter(paint.getMaskFilter()); // restore |
| 2516 | |
| 2517 | // now compute fXOffset if needed |
| 2518 | |
| 2519 | SkScalar xOffset = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2520 | if (paint.getTextAlign() != SkPaint::kLeft_Align) { // need to measure first |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2521 | int count; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2522 | SkScalar width = SkScalarMul(fPaint.measure_text(fCache, text, length, |
| 2523 | &count, NULL), fScale); |
| 2524 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2525 | width = SkScalarHalf(width); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2526 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2527 | xOffset = -width; |
| 2528 | } |
| 2529 | fXPos = xOffset; |
| 2530 | fPrevAdvance = 0; |
| 2531 | |
| 2532 | fText = text; |
| 2533 | fStop = text + length; |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 2534 | |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 2535 | fXYIndex = paint.isVerticalText() ? 1 : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2538 | SkTextToPathIter::~SkTextToPathIter() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2539 | SkGlyphCache::AttachCache(fCache); |
| 2540 | } |
| 2541 | |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 2542 | bool SkTextToPathIter::next(const SkPath** path, SkScalar* xpos) { |
| 2543 | if (fText < fStop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2544 | const SkGlyph& glyph = fGlyphCacheProc(fCache, &fText); |
| 2545 | |
| 2546 | fXPos += SkScalarMul(SkFixedToScalar(fPrevAdvance + fAutoKern.adjust(glyph)), fScale); |
reed@google.com | 44da42e | 2011-11-10 20:04:47 +0000 | [diff] [blame] | 2547 | fPrevAdvance = advance(glyph, fXYIndex); // + fPaint.getTextTracking(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2548 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2549 | if (glyph.fWidth) { |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 2550 | if (path) { |
| 2551 | *path = fCache->findPath(glyph); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame] | 2552 | } |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 2553 | } else { |
| 2554 | if (path) { |
| 2555 | *path = NULL; |
| 2556 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2557 | } |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 2558 | if (xpos) { |
| 2559 | *xpos = fXPos; |
| 2560 | } |
| 2561 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2562 | } |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 2563 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2564 | } |
reed@google.com | dcd0f3a | 2011-10-04 01:17:15 +0000 | [diff] [blame] | 2565 | |
| 2566 | /////////////////////////////////////////////////////////////////////////////// |
| 2567 | |
| 2568 | bool SkPaint::nothingToDraw() const { |
reed@google.com | 733e302 | 2011-10-06 15:11:03 +0000 | [diff] [blame] | 2569 | if (fLooper) { |
| 2570 | return false; |
| 2571 | } |
reed@google.com | dcd0f3a | 2011-10-04 01:17:15 +0000 | [diff] [blame] | 2572 | SkXfermode::Mode mode; |
| 2573 | if (SkXfermode::AsMode(fXfermode, &mode)) { |
| 2574 | switch (mode) { |
| 2575 | case SkXfermode::kSrcOver_Mode: |
| 2576 | case SkXfermode::kSrcATop_Mode: |
| 2577 | case SkXfermode::kDstOut_Mode: |
| 2578 | case SkXfermode::kDstOver_Mode: |
| 2579 | case SkXfermode::kPlus_Mode: |
| 2580 | return 0 == this->getAlpha(); |
| 2581 | case SkXfermode::kDst_Mode: |
| 2582 | return true; |
| 2583 | default: |
| 2584 | break; |
| 2585 | } |
| 2586 | } |
| 2587 | return false; |
| 2588 | } |