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