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