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