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