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