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