reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* libs/graphics/sgl/SkPaint.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | ** |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | ** |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkPaint.h" |
| 19 | #include "SkColorFilter.h" |
| 20 | #include "SkDrawLooper.h" |
| 21 | #include "SkFontHost.h" |
| 22 | #include "SkMaskFilter.h" |
| 23 | #include "SkPathEffect.h" |
| 24 | #include "SkRasterizer.h" |
| 25 | #include "SkShader.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 26 | #include "SkScalar.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | #include "SkScalerContext.h" |
| 28 | #include "SkStroke.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 29 | #include "SkTextFormatParams.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | #include "SkTypeface.h" |
| 31 | #include "SkXfermode.h" |
| 32 | #include "SkAutoKern.h" |
| 33 | |
| 34 | #define SK_DefaultTextSize SkIntToScalar(12) |
| 35 | |
| 36 | #define SK_DefaultFlags 0 //(kNativeHintsText_Flag) |
| 37 | |
reed@android.com | a3122b9 | 2009-08-13 20:38:25 +0000 | [diff] [blame] | 38 | SkPaint::SkPaint() { |
| 39 | // since we may have padding, we zero everything so that our memcmp() call |
| 40 | // in operator== will work correctly. |
| 41 | // with this, we can skip 0 and null individual initializations |
| 42 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | |
reed@android.com | a3122b9 | 2009-08-13 20:38:25 +0000 | [diff] [blame] | 44 | #if 0 // not needed with the bzero call above |
| 45 | fTypeface = NULL; |
| 46 | fTextSkewX = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | fPathEffect = NULL; |
| 48 | fShader = NULL; |
| 49 | fXfermode = NULL; |
| 50 | fMaskFilter = NULL; |
| 51 | fColorFilter = NULL; |
| 52 | fRasterizer = NULL; |
| 53 | fLooper = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | fWidth = 0; |
reed@android.com | a3122b9 | 2009-08-13 20:38:25 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | fTextSize = SK_DefaultTextSize; |
| 58 | fTextScaleX = SK_Scalar1; |
| 59 | fColor = SK_ColorBLACK; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | fMiterLimit = SK_DefaultMiterLimit; |
| 61 | fFlags = SK_DefaultFlags; |
| 62 | fCapType = kDefault_Cap; |
| 63 | fJoinType = kDefault_Join; |
| 64 | fTextAlign = kLeft_Align; |
| 65 | fStyle = kFill_Style; |
| 66 | fTextEncoding = kUTF8_TextEncoding; |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 67 | fHinting = kNormal_Hinting; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 70 | SkPaint::SkPaint(const SkPaint& src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | memcpy(this, &src, sizeof(src)); |
| 72 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 73 | SkSafeRef(fTypeface); |
| 74 | SkSafeRef(fPathEffect); |
| 75 | SkSafeRef(fShader); |
| 76 | SkSafeRef(fXfermode); |
| 77 | SkSafeRef(fMaskFilter); |
| 78 | SkSafeRef(fColorFilter); |
| 79 | SkSafeRef(fRasterizer); |
| 80 | SkSafeRef(fLooper); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 83 | SkPaint::~SkPaint() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 84 | SkSafeUnref(fTypeface); |
| 85 | SkSafeUnref(fPathEffect); |
| 86 | SkSafeUnref(fShader); |
| 87 | SkSafeUnref(fXfermode); |
| 88 | SkSafeUnref(fMaskFilter); |
| 89 | SkSafeUnref(fColorFilter); |
| 90 | SkSafeUnref(fRasterizer); |
| 91 | SkSafeUnref(fLooper); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 94 | SkPaint& SkPaint::operator=(const SkPaint& src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | SkASSERT(&src); |
| 96 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | SkSafeRef(src.fTypeface); |
| 98 | SkSafeRef(src.fPathEffect); |
| 99 | SkSafeRef(src.fShader); |
| 100 | SkSafeRef(src.fXfermode); |
| 101 | SkSafeRef(src.fMaskFilter); |
| 102 | SkSafeRef(src.fColorFilter); |
| 103 | SkSafeRef(src.fRasterizer); |
| 104 | SkSafeRef(src.fLooper); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 106 | SkSafeUnref(fTypeface); |
| 107 | SkSafeUnref(fPathEffect); |
| 108 | SkSafeUnref(fShader); |
| 109 | SkSafeUnref(fXfermode); |
| 110 | SkSafeUnref(fMaskFilter); |
| 111 | SkSafeUnref(fColorFilter); |
| 112 | SkSafeUnref(fRasterizer); |
| 113 | SkSafeUnref(fLooper); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | |
| 115 | memcpy(this, &src, sizeof(src)); |
| 116 | |
| 117 | return *this; |
| 118 | } |
| 119 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 120 | int operator==(const SkPaint& a, const SkPaint& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | return memcmp(&a, &b, sizeof(a)) == 0; |
| 122 | } |
| 123 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 124 | void SkPaint::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | SkPaint init; |
| 126 | |
| 127 | *this = init; |
| 128 | } |
| 129 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 130 | void SkPaint::setFlags(uint32_t flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | fFlags = flags; |
| 132 | } |
| 133 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 134 | void SkPaint::setAntiAlias(bool doAA) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | this->setFlags(SkSetClearMask(fFlags, doAA, kAntiAlias_Flag)); |
| 136 | } |
| 137 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 138 | void SkPaint::setDither(bool doDither) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | this->setFlags(SkSetClearMask(fFlags, doDither, kDither_Flag)); |
| 140 | } |
| 141 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 142 | void SkPaint::setSubpixelText(bool doSubpixel) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | this->setFlags(SkSetClearMask(fFlags, doSubpixel, kSubpixelText_Flag)); |
| 144 | } |
| 145 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 146 | void SkPaint::setLCDRenderText(bool doLCDRender) { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 147 | this->setFlags(SkSetClearMask(fFlags, doLCDRender, kLCDRenderText_Flag)); |
| 148 | } |
| 149 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 150 | void SkPaint::setEmbeddedBitmapText(bool doEmbeddedBitmapText) { |
agl@chromium.org | e95c91e | 2010-01-04 18:27:55 +0000 | [diff] [blame] | 151 | this->setFlags(SkSetClearMask(fFlags, doEmbeddedBitmapText, kEmbeddedBitmapText_Flag)); |
| 152 | } |
| 153 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 154 | void SkPaint::setAutohinted(bool useAutohinter) { |
agl@chromium.org | a2c71cb | 2010-06-17 20:49:17 +0000 | [diff] [blame] | 155 | this->setFlags(SkSetClearMask(fFlags, useAutohinter, kAutoHinting_Flag)); |
| 156 | } |
| 157 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 158 | void SkPaint::setLinearText(bool doLinearText) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | this->setFlags(SkSetClearMask(fFlags, doLinearText, kLinearText_Flag)); |
| 160 | } |
| 161 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 162 | void SkPaint::setUnderlineText(bool doUnderline) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | this->setFlags(SkSetClearMask(fFlags, doUnderline, kUnderlineText_Flag)); |
| 164 | } |
| 165 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 166 | void SkPaint::setStrikeThruText(bool doStrikeThru) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | this->setFlags(SkSetClearMask(fFlags, doStrikeThru, kStrikeThruText_Flag)); |
| 168 | } |
| 169 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 170 | void SkPaint::setFakeBoldText(bool doFakeBold) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | this->setFlags(SkSetClearMask(fFlags, doFakeBold, kFakeBoldText_Flag)); |
| 172 | } |
| 173 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 174 | void SkPaint::setDevKernText(bool doDevKern) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | this->setFlags(SkSetClearMask(fFlags, doDevKern, kDevKernText_Flag)); |
| 176 | } |
| 177 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 178 | void SkPaint::setFilterBitmap(bool doFilter) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | this->setFlags(SkSetClearMask(fFlags, doFilter, kFilterBitmap_Flag)); |
| 180 | } |
| 181 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 182 | void SkPaint::setStyle(Style style) { |
| 183 | if ((unsigned)style < kStyleCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | fStyle = style; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 185 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | #ifdef SK_DEBUG |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 187 | else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | SkDebugf("SkPaint::setStyle(%d) out of range\n", style); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 189 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | #endif |
| 191 | } |
| 192 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 193 | void SkPaint::setColor(SkColor color) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | fColor = color; |
| 195 | } |
| 196 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 197 | void SkPaint::setAlpha(U8CPU a) { |
| 198 | fColor = SkColorSetARGB(a, SkColorGetR(fColor), |
| 199 | SkColorGetG(fColor), SkColorGetB(fColor)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | } |
| 201 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 202 | void SkPaint::setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | fColor = SkColorSetARGB(a, r, g, b); |
| 204 | } |
| 205 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 206 | void SkPaint::setStrokeWidth(SkScalar width) { |
| 207 | if (width >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | fWidth = width; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 209 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | #ifdef SK_DEBUG |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 211 | else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | SkDebugf("SkPaint::setStrokeWidth() called with negative value\n"); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 213 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | #endif |
| 215 | } |
| 216 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 217 | void SkPaint::setStrokeMiter(SkScalar limit) { |
| 218 | if (limit >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | fMiterLimit = limit; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 220 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | #ifdef SK_DEBUG |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 222 | else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 223 | SkDebugf("SkPaint::setStrokeMiter() called with negative value\n"); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 224 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | #endif |
| 226 | } |
| 227 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 228 | void SkPaint::setStrokeCap(Cap ct) { |
| 229 | if ((unsigned)ct < kCapCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | fCapType = SkToU8(ct); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 231 | } else { |
| 232 | SkDEBUGCODE(SkDebugf("SkPaint::setStrokeCap(%d) out of range\n", ct);) |
| 233 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | } |
| 235 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 236 | void SkPaint::setStrokeJoin(Join jt) { |
| 237 | if ((unsigned)jt < kJoinCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | fJoinType = SkToU8(jt); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 239 | } else { |
| 240 | SkDEBUGCODE(SkDebugf("SkPaint::setStrokeJoin(%d) out of range\n", jt);) |
| 241 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | } |
| 243 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 244 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 246 | void SkPaint::setTextAlign(Align align) { |
| 247 | if ((unsigned)align < kAlignCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | fTextAlign = SkToU8(align); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 249 | } else { |
| 250 | SkDEBUGCODE(SkDebugf("SkPaint::setTextAlign(%d) out of range\n", align);) |
| 251 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | } |
| 253 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 254 | void SkPaint::setTextSize(SkScalar ts) { |
| 255 | if (ts >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | fTextSize = ts; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 257 | } else { |
| 258 | SkDEBUGCODE(SkDebugf("SkPaint::setTextSize() called with negative value\n");) |
| 259 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | } |
| 261 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 262 | void SkPaint::setTextScaleX(SkScalar scaleX) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | fTextScaleX = scaleX; |
| 264 | } |
| 265 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 266 | void SkPaint::setTextSkewX(SkScalar skewX) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | fTextSkewX = skewX; |
| 268 | } |
| 269 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 270 | void SkPaint::setTextEncoding(TextEncoding encoding) { |
| 271 | if ((unsigned)encoding <= kGlyphID_TextEncoding) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | fTextEncoding = encoding; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 273 | } else { |
| 274 | SkDEBUGCODE(SkDebugf("SkPaint::setTextEncoding(%d) out of range\n", encoding);) |
| 275 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 276 | } |
| 277 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 278 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 280 | SkTypeface* SkPaint::setTypeface(SkTypeface* font) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | SkRefCnt_SafeAssign(fTypeface, font); |
| 282 | return font; |
| 283 | } |
| 284 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 285 | SkRasterizer* SkPaint::setRasterizer(SkRasterizer* r) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | SkRefCnt_SafeAssign(fRasterizer, r); |
| 287 | return r; |
| 288 | } |
| 289 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 290 | SkDrawLooper* SkPaint::setLooper(SkDrawLooper* looper) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | SkRefCnt_SafeAssign(fLooper, looper); |
| 292 | return looper; |
| 293 | } |
| 294 | |
| 295 | /////////////////////////////////////////////////////////////////////////////// |
| 296 | |
| 297 | #include "SkGlyphCache.h" |
| 298 | #include "SkUtils.h" |
| 299 | |
| 300 | int SkPaint::textToGlyphs(const void* textData, size_t byteLength, |
| 301 | uint16_t glyphs[]) const { |
| 302 | if (byteLength == 0) { |
| 303 | return 0; |
| 304 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 305 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 306 | SkASSERT(textData != NULL); |
| 307 | |
| 308 | if (NULL == glyphs) { |
| 309 | switch (this->getTextEncoding()) { |
| 310 | case kUTF8_TextEncoding: |
| 311 | return SkUTF8_CountUnichars((const char*)textData, byteLength); |
| 312 | case kUTF16_TextEncoding: |
| 313 | return SkUTF16_CountUnichars((const uint16_t*)textData, |
| 314 | byteLength >> 1); |
| 315 | case kGlyphID_TextEncoding: |
| 316 | return byteLength >> 1; |
| 317 | default: |
| 318 | SkASSERT(!"unknown text encoding"); |
| 319 | } |
| 320 | return 0; |
| 321 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 322 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | // 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] | 324 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | // handle this encoding before the setup for the glyphcache |
| 326 | if (this->getTextEncoding() == kGlyphID_TextEncoding) { |
| 327 | // we want to ignore the low bit of byteLength |
| 328 | memcpy(glyphs, textData, byteLength >> 1 << 1); |
| 329 | return byteLength >> 1; |
| 330 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 331 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | SkAutoGlyphCache autoCache(*this, NULL); |
| 333 | SkGlyphCache* cache = autoCache.getCache(); |
| 334 | |
| 335 | const char* text = (const char*)textData; |
| 336 | const char* stop = text + byteLength; |
| 337 | uint16_t* gptr = glyphs; |
| 338 | |
| 339 | switch (this->getTextEncoding()) { |
| 340 | case SkPaint::kUTF8_TextEncoding: |
| 341 | while (text < stop) { |
| 342 | *gptr++ = cache->unicharToGlyph(SkUTF8_NextUnichar(&text)); |
| 343 | } |
| 344 | break; |
| 345 | case SkPaint::kUTF16_TextEncoding: { |
| 346 | const uint16_t* text16 = (const uint16_t*)text; |
| 347 | const uint16_t* stop16 = (const uint16_t*)stop; |
| 348 | while (text16 < stop16) { |
| 349 | *gptr++ = cache->unicharToGlyph(SkUTF16_NextUnichar(&text16)); |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | default: |
| 354 | SkASSERT(!"unknown text encoding"); |
| 355 | } |
| 356 | return gptr - glyphs; |
| 357 | } |
| 358 | |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 359 | bool SkPaint::containsText(const void* textData, size_t byteLength) const { |
| 360 | if (0 == byteLength) { |
| 361 | return true; |
| 362 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 363 | |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 364 | SkASSERT(textData != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 365 | |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 366 | // handle this encoding before the setup for the glyphcache |
| 367 | if (this->getTextEncoding() == kGlyphID_TextEncoding) { |
| 368 | const uint16_t* glyphID = static_cast<const uint16_t*>(textData); |
| 369 | size_t count = byteLength >> 1; |
| 370 | for (size_t i = 0; i < count; i++) { |
| 371 | if (0 == glyphID[i]) { |
| 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | return true; |
| 376 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 377 | |
reed@android.com | a5dcaf6 | 2010-02-05 17:12:32 +0000 | [diff] [blame] | 378 | SkAutoGlyphCache autoCache(*this, NULL); |
| 379 | SkGlyphCache* cache = autoCache.getCache(); |
| 380 | |
| 381 | switch (this->getTextEncoding()) { |
| 382 | case SkPaint::kUTF8_TextEncoding: { |
| 383 | const char* text = static_cast<const char*>(textData); |
| 384 | const char* stop = text + byteLength; |
| 385 | while (text < stop) { |
| 386 | if (0 == cache->unicharToGlyph(SkUTF8_NextUnichar(&text))) { |
| 387 | return false; |
| 388 | } |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | case SkPaint::kUTF16_TextEncoding: { |
| 393 | const uint16_t* text = static_cast<const uint16_t*>(textData); |
| 394 | const uint16_t* stop = text + (byteLength >> 1); |
| 395 | while (text < stop) { |
| 396 | if (0 == cache->unicharToGlyph(SkUTF16_NextUnichar(&text))) { |
| 397 | return false; |
| 398 | } |
| 399 | } |
| 400 | break; |
| 401 | } |
| 402 | default: |
| 403 | SkASSERT(!"unknown text encoding"); |
| 404 | return false; |
| 405 | } |
| 406 | return true; |
| 407 | } |
| 408 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 409 | void SkPaint::glyphsToUnichars(const uint16_t glyphs[], int count, |
| 410 | SkUnichar textData[]) const { |
| 411 | if (count <= 0) { |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | SkASSERT(glyphs != NULL); |
| 416 | SkASSERT(textData != NULL); |
| 417 | |
| 418 | SkAutoGlyphCache autoCache(*this, NULL); |
| 419 | SkGlyphCache* cache = autoCache.getCache(); |
| 420 | |
| 421 | for (int index = 0; index < count; index++) { |
| 422 | textData[index] = cache->glyphToUnichar(glyphs[index]); |
| 423 | } |
| 424 | } |
| 425 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | /////////////////////////////////////////////////////////////////////////////// |
| 427 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 428 | static const SkGlyph& sk_getMetrics_utf8_next(SkGlyphCache* cache, |
| 429 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | SkASSERT(cache != NULL); |
| 431 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 432 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | return cache->getUnicharMetrics(SkUTF8_NextUnichar(text)); |
| 434 | } |
| 435 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 436 | static const SkGlyph& sk_getMetrics_utf8_prev(SkGlyphCache* cache, |
| 437 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | SkASSERT(cache != NULL); |
| 439 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 440 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 441 | return cache->getUnicharMetrics(SkUTF8_PrevUnichar(text)); |
| 442 | } |
| 443 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 444 | static const SkGlyph& sk_getMetrics_utf16_next(SkGlyphCache* cache, |
| 445 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | SkASSERT(cache != NULL); |
| 447 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 448 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 449 | return cache->getUnicharMetrics(SkUTF16_NextUnichar((const uint16_t**)text)); |
| 450 | } |
| 451 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 452 | static const SkGlyph& sk_getMetrics_utf16_prev(SkGlyphCache* cache, |
| 453 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 454 | SkASSERT(cache != NULL); |
| 455 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 456 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 457 | return cache->getUnicharMetrics(SkUTF16_PrevUnichar((const uint16_t**)text)); |
| 458 | } |
| 459 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 460 | static const SkGlyph& sk_getMetrics_glyph_next(SkGlyphCache* cache, |
| 461 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | SkASSERT(cache != NULL); |
| 463 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 464 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 465 | const uint16_t* ptr = *(const uint16_t**)text; |
| 466 | unsigned glyphID = *ptr; |
| 467 | ptr += 1; |
| 468 | *text = (const char*)ptr; |
| 469 | return cache->getGlyphIDMetrics(glyphID); |
| 470 | } |
| 471 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 472 | static const SkGlyph& sk_getMetrics_glyph_prev(SkGlyphCache* cache, |
| 473 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 474 | SkASSERT(cache != NULL); |
| 475 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 476 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | const uint16_t* ptr = *(const uint16_t**)text; |
| 478 | ptr -= 1; |
| 479 | unsigned glyphID = *ptr; |
| 480 | *text = (const char*)ptr; |
| 481 | return cache->getGlyphIDMetrics(glyphID); |
| 482 | } |
| 483 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 484 | static const SkGlyph& sk_getAdvance_utf8_next(SkGlyphCache* cache, |
| 485 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 486 | SkASSERT(cache != NULL); |
| 487 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 488 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 489 | return cache->getUnicharAdvance(SkUTF8_NextUnichar(text)); |
| 490 | } |
| 491 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 492 | static const SkGlyph& sk_getAdvance_utf8_prev(SkGlyphCache* cache, |
| 493 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 494 | SkASSERT(cache != NULL); |
| 495 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 496 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 497 | return cache->getUnicharAdvance(SkUTF8_PrevUnichar(text)); |
| 498 | } |
| 499 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 500 | static const SkGlyph& sk_getAdvance_utf16_next(SkGlyphCache* cache, |
| 501 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 502 | SkASSERT(cache != NULL); |
| 503 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 504 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 505 | return cache->getUnicharAdvance(SkUTF16_NextUnichar((const uint16_t**)text)); |
| 506 | } |
| 507 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 508 | static const SkGlyph& sk_getAdvance_utf16_prev(SkGlyphCache* cache, |
| 509 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 510 | SkASSERT(cache != NULL); |
| 511 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 512 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 513 | return cache->getUnicharAdvance(SkUTF16_PrevUnichar((const uint16_t**)text)); |
| 514 | } |
| 515 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 516 | static const SkGlyph& sk_getAdvance_glyph_next(SkGlyphCache* cache, |
| 517 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 518 | SkASSERT(cache != NULL); |
| 519 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 520 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 521 | const uint16_t* ptr = *(const uint16_t**)text; |
| 522 | unsigned glyphID = *ptr; |
| 523 | ptr += 1; |
| 524 | *text = (const char*)ptr; |
| 525 | return cache->getGlyphIDAdvance(glyphID); |
| 526 | } |
| 527 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 528 | static const SkGlyph& sk_getAdvance_glyph_prev(SkGlyphCache* cache, |
| 529 | const char** text) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | SkASSERT(cache != NULL); |
| 531 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 532 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | const uint16_t* ptr = *(const uint16_t**)text; |
| 534 | ptr -= 1; |
| 535 | unsigned glyphID = *ptr; |
| 536 | *text = (const char*)ptr; |
| 537 | return cache->getGlyphIDAdvance(glyphID); |
| 538 | } |
| 539 | |
| 540 | SkMeasureCacheProc SkPaint::getMeasureCacheProc(TextBufferDirection tbd, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 541 | bool needFullMetrics) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | static const SkMeasureCacheProc gMeasureCacheProcs[] = { |
| 543 | sk_getMetrics_utf8_next, |
| 544 | sk_getMetrics_utf16_next, |
| 545 | sk_getMetrics_glyph_next, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 546 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 547 | sk_getMetrics_utf8_prev, |
| 548 | sk_getMetrics_utf16_prev, |
| 549 | sk_getMetrics_glyph_prev, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 550 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 551 | sk_getAdvance_utf8_next, |
| 552 | sk_getAdvance_utf16_next, |
| 553 | sk_getAdvance_glyph_next, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 554 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 555 | sk_getAdvance_utf8_prev, |
| 556 | sk_getAdvance_utf16_prev, |
| 557 | sk_getAdvance_glyph_prev |
| 558 | }; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 559 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 560 | unsigned index = this->getTextEncoding(); |
| 561 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 562 | if (kBackward_TextBufferDirection == tbd) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 563 | index += 3; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 564 | } |
| 565 | if (!needFullMetrics && !this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 566 | index += 6; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 567 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 568 | |
| 569 | SkASSERT(index < SK_ARRAY_COUNT(gMeasureCacheProcs)); |
| 570 | return gMeasureCacheProcs[index]; |
| 571 | } |
| 572 | |
| 573 | /////////////////////////////////////////////////////////////////////////////// |
| 574 | |
| 575 | static const SkGlyph& sk_getMetrics_utf8_00(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 576 | const char** text, SkFixed, SkFixed) { |
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 | return cache->getUnicharMetrics(SkUTF8_NextUnichar(text)); |
| 581 | } |
| 582 | |
| 583 | static const SkGlyph& sk_getMetrics_utf8_xy(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 584 | const char** text, SkFixed x, SkFixed y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 585 | SkASSERT(cache != NULL); |
| 586 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 587 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 588 | return cache->getUnicharMetrics(SkUTF8_NextUnichar(text), x, y); |
| 589 | } |
| 590 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 591 | static const SkGlyph& sk_getMetrics_utf16_00(SkGlyphCache* cache, |
| 592 | const char** text, SkFixed, SkFixed) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 593 | SkASSERT(cache != NULL); |
| 594 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 595 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 596 | return cache->getUnicharMetrics(SkUTF16_NextUnichar((const uint16_t**)text)); |
| 597 | } |
| 598 | |
| 599 | static const SkGlyph& sk_getMetrics_utf16_xy(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 600 | const char** text, SkFixed x, SkFixed y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 601 | SkASSERT(cache != NULL); |
| 602 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 603 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 604 | return cache->getUnicharMetrics(SkUTF16_NextUnichar((const uint16_t**)text), |
| 605 | x, y); |
| 606 | } |
| 607 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 608 | static const SkGlyph& sk_getMetrics_glyph_00(SkGlyphCache* cache, |
| 609 | const char** text, SkFixed, SkFixed) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 610 | SkASSERT(cache != NULL); |
| 611 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 612 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 613 | const uint16_t* ptr = *(const uint16_t**)text; |
| 614 | unsigned glyphID = *ptr; |
| 615 | ptr += 1; |
| 616 | *text = (const char*)ptr; |
| 617 | return cache->getGlyphIDMetrics(glyphID); |
| 618 | } |
| 619 | |
| 620 | static const SkGlyph& sk_getMetrics_glyph_xy(SkGlyphCache* cache, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 621 | const char** text, SkFixed x, SkFixed y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 622 | SkASSERT(cache != NULL); |
| 623 | SkASSERT(text != NULL); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 624 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 625 | const uint16_t* ptr = *(const uint16_t**)text; |
| 626 | unsigned glyphID = *ptr; |
| 627 | ptr += 1; |
| 628 | *text = (const char*)ptr; |
| 629 | return cache->getGlyphIDMetrics(glyphID, x, y); |
| 630 | } |
| 631 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 632 | SkDrawCacheProc SkPaint::getDrawCacheProc() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 633 | static const SkDrawCacheProc gDrawCacheProcs[] = { |
| 634 | sk_getMetrics_utf8_00, |
| 635 | sk_getMetrics_utf16_00, |
| 636 | sk_getMetrics_glyph_00, |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 637 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 638 | sk_getMetrics_utf8_xy, |
| 639 | sk_getMetrics_utf16_xy, |
| 640 | sk_getMetrics_glyph_xy |
| 641 | }; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 642 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 643 | unsigned index = this->getTextEncoding(); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 644 | if (fFlags & kSubpixelText_Flag) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 645 | index += 3; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 646 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 647 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 648 | SkASSERT(index < SK_ARRAY_COUNT(gDrawCacheProcs)); |
| 649 | return gDrawCacheProcs[index]; |
| 650 | } |
| 651 | |
| 652 | /////////////////////////////////////////////////////////////////////////////// |
| 653 | |
| 654 | class SkAutoRestorePaintTextSizeAndFrame { |
| 655 | public: |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 656 | SkAutoRestorePaintTextSizeAndFrame(const SkPaint* paint) |
| 657 | : fPaint((SkPaint*)paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 658 | fTextSize = paint->getTextSize(); |
| 659 | fStyle = paint->getStyle(); |
| 660 | fPaint->setStyle(SkPaint::kFill_Style); |
| 661 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 662 | |
| 663 | ~SkAutoRestorePaintTextSizeAndFrame() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 664 | fPaint->setStyle(fStyle); |
| 665 | fPaint->setTextSize(fTextSize); |
| 666 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 667 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 668 | private: |
| 669 | SkPaint* fPaint; |
| 670 | SkScalar fTextSize; |
| 671 | SkPaint::Style fStyle; |
| 672 | }; |
| 673 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 674 | static void set_bounds(const SkGlyph& g, SkRect* bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 675 | bounds->set(SkIntToScalar(g.fLeft), |
| 676 | SkIntToScalar(g.fTop), |
| 677 | SkIntToScalar(g.fLeft + g.fWidth), |
| 678 | SkIntToScalar(g.fTop + g.fHeight)); |
| 679 | } |
| 680 | |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 681 | // 64bits wide, with a 16bit bias. Useful when accumulating lots of 16.16 so |
| 682 | // we don't overflow along the way |
| 683 | typedef int64_t Sk48Dot16; |
| 684 | |
| 685 | #ifdef SK_SCALAR_IS_FLOAT |
| 686 | static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { |
reed@android.com | e89d3ec | 2010-05-18 21:23:30 +0000 | [diff] [blame] | 687 | return (float) (x * 1.5258789e-5); // x * (1 / 65536.0f) |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 688 | } |
| 689 | #else |
| 690 | static inline SkFixed Sk48Dot16ToScalar(Sk48Dot16 x) { |
| 691 | // just return the low 32bits |
| 692 | return static_cast<SkFixed>(x); |
| 693 | } |
| 694 | #endif |
| 695 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 696 | static void join_bounds(const SkGlyph& g, SkRect* bounds, Sk48Dot16 dx) { |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 697 | SkScalar sx = Sk48Dot16ToScalar(dx); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 698 | bounds->join(SkIntToScalar(g.fLeft) + sx, |
| 699 | SkIntToScalar(g.fTop), |
| 700 | SkIntToScalar(g.fLeft + g.fWidth) + sx, |
| 701 | SkIntToScalar(g.fTop + g.fHeight)); |
| 702 | } |
| 703 | |
| 704 | SkScalar SkPaint::measure_text(SkGlyphCache* cache, |
| 705 | const char* text, size_t byteLength, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 706 | int* count, SkRect* bounds) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 707 | SkASSERT(count); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 708 | if (byteLength == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 709 | *count = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 710 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 711 | bounds->setEmpty(); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 712 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | SkMeasureCacheProc glyphCacheProc; |
| 717 | glyphCacheProc = this->getMeasureCacheProc(kForward_TextBufferDirection, |
| 718 | NULL != bounds); |
| 719 | |
| 720 | int n = 1; |
| 721 | const char* stop = (const char*)text + byteLength; |
| 722 | const SkGlyph* g = &glyphCacheProc(cache, &text); |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 723 | // our accumulated fixed-point advances might overflow 16.16, so we use |
| 724 | // a 48.16 (64bit) accumulator, and then convert that to scalar at the |
| 725 | // very end. |
| 726 | Sk48Dot16 x = g->fAdvanceX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 727 | |
| 728 | SkAutoKern autokern; |
| 729 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 730 | if (NULL == bounds) { |
| 731 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | int rsb; |
| 733 | for (; text < stop; n++) { |
| 734 | rsb = g->fRsbDelta; |
| 735 | g = &glyphCacheProc(cache, &text); |
| 736 | x += SkAutoKern_AdjustF(rsb, g->fLsbDelta) + g->fAdvanceX; |
| 737 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 738 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 739 | for (; text < stop; n++) { |
| 740 | x += glyphCacheProc(cache, &text).fAdvanceX; |
| 741 | } |
| 742 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 743 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 744 | set_bounds(*g, bounds); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 745 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 746 | int rsb; |
| 747 | for (; text < stop; n++) { |
| 748 | rsb = g->fRsbDelta; |
| 749 | g = &glyphCacheProc(cache, &text); |
| 750 | x += SkAutoKern_AdjustF(rsb, g->fLsbDelta); |
| 751 | join_bounds(*g, bounds, x); |
| 752 | x += g->fAdvanceX; |
| 753 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 754 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 755 | for (; text < stop; n++) { |
| 756 | g = &glyphCacheProc(cache, &text); |
| 757 | join_bounds(*g, bounds, x); |
| 758 | x += g->fAdvanceX; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | SkASSERT(text == stop); |
| 763 | |
| 764 | *count = n; |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 765 | return Sk48Dot16ToScalar(x); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | SkScalar SkPaint::measureText(const void* textData, size_t length, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 769 | SkRect* bounds, SkScalar zoom) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 770 | const char* text = (const char*)textData; |
| 771 | SkASSERT(text != NULL || length == 0); |
| 772 | |
| 773 | SkScalar scale = 0; |
| 774 | SkAutoRestorePaintTextSizeAndFrame restore(this); |
| 775 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 776 | if (this->isLinearText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 777 | scale = fTextSize / kCanonicalTextSizeForPaths; |
| 778 | // this gets restored by restore |
| 779 | ((SkPaint*)this)->setTextSize(SkIntToScalar(kCanonicalTextSizeForPaths)); |
| 780 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 781 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 782 | SkMatrix zoomMatrix, *zoomPtr = NULL; |
| 783 | if (zoom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 784 | zoomMatrix.setScale(zoom, zoom); |
| 785 | zoomPtr = &zoomMatrix; |
| 786 | } |
| 787 | |
| 788 | SkAutoGlyphCache autoCache(*this, zoomPtr); |
| 789 | SkGlyphCache* cache = autoCache.getCache(); |
| 790 | |
| 791 | SkScalar width = 0; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 792 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 793 | if (length > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 794 | int tempCount; |
| 795 | |
| 796 | width = this->measure_text(cache, text, length, &tempCount, bounds); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 797 | if (scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 798 | width = SkScalarMul(width, scale); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 799 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 800 | bounds->fLeft = SkScalarMul(bounds->fLeft, scale); |
| 801 | bounds->fTop = SkScalarMul(bounds->fTop, scale); |
| 802 | bounds->fRight = SkScalarMul(bounds->fRight, scale); |
| 803 | bounds->fBottom = SkScalarMul(bounds->fBottom, scale); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | return width; |
| 808 | } |
| 809 | |
| 810 | typedef bool (*SkTextBufferPred)(const char* text, const char* stop); |
| 811 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 812 | static bool forward_textBufferPred(const char* text, const char* stop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 813 | return text < stop; |
| 814 | } |
| 815 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 816 | static bool backward_textBufferPred(const char* text, const char* stop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 817 | return text > stop; |
| 818 | } |
| 819 | |
| 820 | static SkTextBufferPred chooseTextBufferPred(SkPaint::TextBufferDirection tbd, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 821 | const char** text, size_t length, |
| 822 | const char** stop) { |
| 823 | if (SkPaint::kForward_TextBufferDirection == tbd) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 824 | *stop = *text + length; |
| 825 | return forward_textBufferPred; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 826 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 827 | // text should point to the end of the buffer, and stop to the beginning |
| 828 | *stop = *text; |
| 829 | *text += length; |
| 830 | return backward_textBufferPred; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | size_t SkPaint::breakText(const void* textD, size_t length, SkScalar maxWidth, |
| 835 | SkScalar* measuredWidth, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 836 | TextBufferDirection tbd) const { |
| 837 | if (0 == length || 0 >= maxWidth) { |
| 838 | if (measuredWidth) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 839 | *measuredWidth = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 840 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | SkASSERT(textD != NULL); |
| 845 | const char* text = (const char*)textD; |
| 846 | |
| 847 | SkScalar scale = 0; |
| 848 | SkAutoRestorePaintTextSizeAndFrame restore(this); |
| 849 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 850 | if (this->isLinearText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 851 | scale = fTextSize / kCanonicalTextSizeForPaths; |
reed@android.com | 647d3da | 2010-05-17 14:15:14 +0000 | [diff] [blame] | 852 | maxWidth = SkScalarMulDiv(maxWidth, kCanonicalTextSizeForPaths, fTextSize); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 853 | // this gets restored by restore |
| 854 | ((SkPaint*)this)->setTextSize(SkIntToScalar(kCanonicalTextSizeForPaths)); |
| 855 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 856 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 857 | SkAutoGlyphCache autoCache(*this, NULL); |
| 858 | SkGlyphCache* cache = autoCache.getCache(); |
| 859 | |
| 860 | SkMeasureCacheProc glyphCacheProc = this->getMeasureCacheProc(tbd, false); |
| 861 | const char* stop; |
| 862 | SkTextBufferPred pred = chooseTextBufferPred(tbd, &text, length, &stop); |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 863 | // use 64bits for our accumulator, to avoid overflowing 16.16 |
| 864 | Sk48Dot16 max = SkScalarToFixed(maxWidth); |
| 865 | Sk48Dot16 width = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 866 | |
| 867 | SkAutoKern autokern; |
| 868 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 869 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 870 | int rsb = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 871 | while (pred(text, stop)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 872 | const char* curr = text; |
| 873 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 874 | SkFixed x = SkAutoKern_AdjustF(rsb, g.fLsbDelta) + g.fAdvanceX; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 875 | if ((width += x) > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 876 | width -= x; |
| 877 | text = curr; |
| 878 | break; |
| 879 | } |
| 880 | rsb = g.fRsbDelta; |
| 881 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 882 | } else { |
| 883 | while (pred(text, stop)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 884 | const char* curr = text; |
| 885 | SkFixed x = glyphCacheProc(cache, &text).fAdvanceX; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 886 | if ((width += x) > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 887 | width -= x; |
| 888 | text = curr; |
| 889 | break; |
| 890 | } |
| 891 | } |
| 892 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 893 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 894 | if (measuredWidth) { |
reed@android.com | e88f551 | 2010-03-19 14:42:28 +0000 | [diff] [blame] | 895 | SkScalar scalarWidth = Sk48Dot16ToScalar(width); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 896 | if (scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 897 | scalarWidth = SkScalarMul(scalarWidth, scale); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 898 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 899 | *measuredWidth = scalarWidth; |
| 900 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 901 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 902 | // return the number of bytes measured |
| 903 | return (kForward_TextBufferDirection == tbd) ? |
| 904 | text - stop + length : stop - text + length; |
| 905 | } |
| 906 | |
| 907 | /////////////////////////////////////////////////////////////////////////////// |
| 908 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 909 | static bool FontMetricsCacheProc(const SkGlyphCache* cache, void* context) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 910 | *(SkPaint::FontMetrics*)context = cache->getFontMetricsY(); |
| 911 | return false; // don't detach the cache |
| 912 | } |
| 913 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 914 | static void FontMetricsDescProc(const SkDescriptor* desc, void* context) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 915 | SkGlyphCache::VisitCache(desc, FontMetricsCacheProc, context); |
| 916 | } |
| 917 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 918 | SkScalar SkPaint::getFontMetrics(FontMetrics* metrics, SkScalar zoom) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 919 | SkScalar scale = 0; |
| 920 | SkAutoRestorePaintTextSizeAndFrame restore(this); |
| 921 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 922 | if (this->isLinearText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 923 | scale = fTextSize / kCanonicalTextSizeForPaths; |
| 924 | // this gets restored by restore |
| 925 | ((SkPaint*)this)->setTextSize(SkIntToScalar(kCanonicalTextSizeForPaths)); |
| 926 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 927 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 928 | SkMatrix zoomMatrix, *zoomPtr = NULL; |
| 929 | if (zoom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 930 | zoomMatrix.setScale(zoom, zoom); |
| 931 | zoomPtr = &zoomMatrix; |
| 932 | } |
| 933 | |
| 934 | #if 0 |
| 935 | SkAutoGlyphCache autoCache(*this, zoomPtr); |
| 936 | SkGlyphCache* cache = autoCache.getCache(); |
| 937 | const FontMetrics& my = cache->getFontMetricsY(); |
| 938 | #endif |
| 939 | FontMetrics storage; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 940 | if (NULL == metrics) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 941 | metrics = &storage; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 942 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 943 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 944 | this->descriptorProc(zoomPtr, FontMetricsDescProc, metrics); |
| 945 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 946 | if (scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 947 | metrics->fTop = SkScalarMul(metrics->fTop, scale); |
| 948 | metrics->fAscent = SkScalarMul(metrics->fAscent, scale); |
| 949 | metrics->fDescent = SkScalarMul(metrics->fDescent, scale); |
| 950 | metrics->fBottom = SkScalarMul(metrics->fBottom, scale); |
| 951 | metrics->fLeading = SkScalarMul(metrics->fLeading, scale); |
| 952 | } |
| 953 | return metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 954 | } |
| 955 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 956 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 957 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 958 | static void set_bounds(const SkGlyph& g, SkRect* bounds, SkScalar scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 959 | bounds->set(g.fLeft * scale, |
| 960 | g.fTop * scale, |
| 961 | (g.fLeft + g.fWidth) * scale, |
| 962 | (g.fTop + g.fHeight) * scale); |
| 963 | } |
| 964 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 965 | int SkPaint::getTextWidths(const void* textData, size_t byteLength, |
| 966 | SkScalar widths[], SkRect bounds[]) const { |
| 967 | if (0 == byteLength) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 968 | return 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 969 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 970 | |
| 971 | SkASSERT(NULL != textData); |
| 972 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 973 | if (NULL == widths && NULL == bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 974 | return this->countText(textData, byteLength); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 975 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 976 | |
| 977 | SkAutoRestorePaintTextSizeAndFrame restore(this); |
| 978 | SkScalar scale = 0; |
| 979 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 980 | if (this->isLinearText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 981 | scale = fTextSize / kCanonicalTextSizeForPaths; |
| 982 | // this gets restored by restore |
| 983 | ((SkPaint*)this)->setTextSize(SkIntToScalar(kCanonicalTextSizeForPaths)); |
| 984 | } |
| 985 | |
| 986 | SkAutoGlyphCache autoCache(*this, NULL); |
| 987 | SkGlyphCache* cache = autoCache.getCache(); |
| 988 | SkMeasureCacheProc glyphCacheProc; |
| 989 | glyphCacheProc = this->getMeasureCacheProc(kForward_TextBufferDirection, |
| 990 | NULL != bounds); |
| 991 | |
| 992 | const char* text = (const char*)textData; |
| 993 | const char* stop = text + byteLength; |
| 994 | int count = 0; |
| 995 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 996 | if (this->isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 997 | // we adjust the widths returned here through auto-kerning |
| 998 | SkAutoKern autokern; |
| 999 | SkFixed prevWidth = 0; |
| 1000 | |
| 1001 | if (scale) { |
| 1002 | while (text < stop) { |
| 1003 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1004 | if (widths) { |
| 1005 | SkFixed adjust = autokern.adjust(g); |
| 1006 | |
| 1007 | if (count > 0) { |
| 1008 | SkScalar w = SkFixedToScalar(prevWidth + adjust); |
| 1009 | *widths++ = SkScalarMul(w, scale); |
| 1010 | } |
| 1011 | prevWidth = g.fAdvanceX; |
| 1012 | } |
| 1013 | if (bounds) { |
| 1014 | set_bounds(g, bounds++, scale); |
| 1015 | } |
| 1016 | ++count; |
| 1017 | } |
| 1018 | if (count > 0 && widths) { |
| 1019 | *widths = SkScalarMul(SkFixedToScalar(prevWidth), scale); |
| 1020 | } |
| 1021 | } else { |
| 1022 | while (text < stop) { |
| 1023 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1024 | if (widths) { |
| 1025 | SkFixed adjust = autokern.adjust(g); |
| 1026 | |
| 1027 | if (count > 0) { |
| 1028 | *widths++ = SkFixedToScalar(prevWidth + adjust); |
| 1029 | } |
| 1030 | prevWidth = g.fAdvanceX; |
| 1031 | } |
| 1032 | if (bounds) { |
| 1033 | set_bounds(g, bounds++); |
| 1034 | } |
| 1035 | ++count; |
| 1036 | } |
| 1037 | if (count > 0 && widths) { |
| 1038 | *widths = SkFixedToScalar(prevWidth); |
| 1039 | } |
| 1040 | } |
| 1041 | } else { // no devkern |
| 1042 | if (scale) { |
| 1043 | while (text < stop) { |
| 1044 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1045 | if (widths) { |
| 1046 | *widths++ = SkScalarMul(SkFixedToScalar(g.fAdvanceX), |
| 1047 | scale); |
| 1048 | } |
| 1049 | if (bounds) { |
| 1050 | set_bounds(g, bounds++, scale); |
| 1051 | } |
| 1052 | ++count; |
| 1053 | } |
| 1054 | } else { |
| 1055 | while (text < stop) { |
| 1056 | const SkGlyph& g = glyphCacheProc(cache, &text); |
| 1057 | if (widths) { |
| 1058 | *widths++ = SkFixedToScalar(g.fAdvanceX); |
| 1059 | } |
| 1060 | if (bounds) { |
| 1061 | set_bounds(g, bounds++); |
| 1062 | } |
| 1063 | ++count; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | SkASSERT(text == stop); |
| 1069 | return count; |
| 1070 | } |
| 1071 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1072 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1073 | |
| 1074 | #include "SkDraw.h" |
| 1075 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1076 | void SkPaint::getTextPath(const void* textData, size_t length, |
| 1077 | SkScalar x, SkScalar y, SkPath* path) const { |
| 1078 | SkASSERT(length == 0 || textData != NULL); |
| 1079 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1080 | const char* text = (const char*)textData; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1081 | if (text == NULL || length == 0 || path == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1082 | return; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1083 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1084 | |
| 1085 | SkTextToPathIter iter(text, length, *this, false, true); |
| 1086 | SkMatrix matrix; |
| 1087 | SkScalar prevXPos = 0; |
| 1088 | |
| 1089 | matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
| 1090 | matrix.postTranslate(x, y); |
| 1091 | path->reset(); |
| 1092 | |
| 1093 | SkScalar xpos; |
| 1094 | const SkPath* iterPath; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1095 | while ((iterPath = iter.next(&xpos)) != NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1096 | matrix.postTranslate(xpos - prevXPos, 0); |
| 1097 | path->addPath(*iterPath, matrix); |
| 1098 | prevXPos = xpos; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | static void add_flattenable(SkDescriptor* desc, uint32_t tag, |
| 1103 | SkFlattenableWriteBuffer* buffer) { |
| 1104 | buffer->flatten(desc->addEntry(tag, buffer->size(), NULL)); |
| 1105 | } |
| 1106 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1107 | static SkMask::Format computeMaskFormat(const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1108 | uint32_t flags = paint.getFlags(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1109 | |
agl@chromium.org | b4234a2 | 2010-01-21 11:43:44 +0000 | [diff] [blame] | 1110 | // Antialiasing being disabled trumps all other settings. |
| 1111 | if (!(flags & SkPaint::kAntiAlias_Flag)) |
| 1112 | return SkMask::kBW_Format; |
| 1113 | |
reed@android.com | f549369 | 2009-07-22 19:21:01 +0000 | [diff] [blame] | 1114 | #if defined(SK_SUPPORT_LCDTEXT) |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1115 | if (flags & SkPaint::kLCDRenderText_Flag) { |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 1116 | return SkFontHost::GetSubpixelOrientation() == SkFontHost::kHorizontal_LCDOrientation ? |
| 1117 | SkMask::kHorizontalLCD_Format : SkMask::kVerticalLCD_Format; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1118 | } |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 1119 | #endif |
agl@chromium.org | b4234a2 | 2010-01-21 11:43:44 +0000 | [diff] [blame] | 1120 | |
| 1121 | return SkMask::kA8_Format; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1124 | // if linear-text is on, then we force hinting to be off (since that's sort of |
| 1125 | // the point of linear-text. |
| 1126 | static SkPaint::Hinting computeHinting(const SkPaint& paint) { |
| 1127 | SkPaint::Hinting h = paint.getHinting(); |
| 1128 | if (paint.isLinearText()) { |
| 1129 | h = SkPaint::kNo_Hinting; |
| 1130 | } |
| 1131 | return h; |
| 1132 | } |
| 1133 | |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1134 | /* |
| 1135 | * Return the scalar with only limited fractional precision. Used to consolidate matrices |
| 1136 | * that vary only slightly when we create our key into the font cache, since the font scaler |
| 1137 | * typically returns the same looking resuts for tiny changes in the matrix. |
| 1138 | */ |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1139 | static SkScalar sk_relax(SkScalar x) { |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1140 | #ifdef SK_SCALAR_IS_FLOAT |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1141 | int n = sk_float_round2int(x * 1024); |
| 1142 | return n / 1024.0f; |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1143 | #else |
| 1144 | // round to the nearest 10 fractional bits |
| 1145 | return (x + (1 << 5)) & ~(1024 - 1); |
| 1146 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 1149 | void SkScalerContext::MakeRec(const SkPaint& paint, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1150 | const SkMatrix* deviceMatrix, Rec* rec) { |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 1151 | SkASSERT(deviceMatrix == NULL || |
| 1152 | (deviceMatrix->getType() & SkMatrix::kPerspective_Mask) == 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1153 | |
| 1154 | rec->fFontID = SkTypeface::UniqueID(paint.getTypeface()); |
| 1155 | rec->fTextSize = paint.getTextSize(); |
| 1156 | rec->fPreScaleX = paint.getTextScaleX(); |
| 1157 | rec->fPreSkewX = paint.getTextSkewX(); |
| 1158 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1159 | if (deviceMatrix) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1160 | rec->fPost2x2[0][0] = sk_relax(deviceMatrix->getScaleX()); |
| 1161 | rec->fPost2x2[0][1] = sk_relax(deviceMatrix->getSkewX()); |
| 1162 | rec->fPost2x2[1][0] = sk_relax(deviceMatrix->getSkewY()); |
| 1163 | rec->fPost2x2[1][1] = sk_relax(deviceMatrix->getScaleY()); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1164 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1165 | rec->fPost2x2[0][0] = rec->fPost2x2[1][1] = SK_Scalar1; |
| 1166 | rec->fPost2x2[0][1] = rec->fPost2x2[1][0] = 0; |
| 1167 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1168 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1169 | SkPaint::Style style = paint.getStyle(); |
| 1170 | SkScalar strokeWidth = paint.getStrokeWidth(); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1171 | |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1172 | unsigned flags = SkFontHost::ComputeGammaFlag(paint); |
| 1173 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1174 | if (paint.isFakeBoldText()) { |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1175 | #ifdef SK_USE_FREETYPE_EMBOLDEN |
| 1176 | flags |= SkScalerContext::kEmbolden_Flag; |
| 1177 | #else |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1178 | SkScalar fakeBoldScale = SkScalarInterpFunc(paint.getTextSize(), |
| 1179 | kStdFakeBoldInterpKeys, |
| 1180 | kStdFakeBoldInterpValues, |
| 1181 | kStdFakeBoldInterpLength); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1182 | SkScalar extra = SkScalarMul(paint.getTextSize(), fakeBoldScale); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1183 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1184 | if (style == SkPaint::kFill_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1185 | style = SkPaint::kStrokeAndFill_Style; |
| 1186 | strokeWidth = extra; // ignore paint's strokeWidth if it was "fill" |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1187 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1188 | strokeWidth += extra; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1189 | } |
senorblanco@chromium.org | 4526a84 | 2010-02-05 23:08:20 +0000 | [diff] [blame] | 1190 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1193 | if (paint.isDevKernText()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1194 | flags |= SkScalerContext::kDevKernText_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1195 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1196 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1197 | if (style != SkPaint::kFill_Style && strokeWidth > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1198 | rec->fFrameWidth = strokeWidth; |
| 1199 | rec->fMiterLimit = paint.getStrokeMiter(); |
| 1200 | rec->fStrokeJoin = SkToU8(paint.getStrokeJoin()); |
| 1201 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1202 | if (style == SkPaint::kStrokeAndFill_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1203 | flags |= SkScalerContext::kFrameAndFill_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1204 | } |
| 1205 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1206 | rec->fFrameWidth = 0; |
| 1207 | rec->fMiterLimit = 0; |
| 1208 | rec->fStrokeJoin = 0; |
| 1209 | } |
| 1210 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1211 | rec->fMaskFormat = SkToU8(computeMaskFormat(paint)); |
| 1212 | rec->fFlags = SkToU8(flags); |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1213 | rec->setHinting(computeHinting(paint)); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1214 | if (paint.isEmbeddedBitmapText()) { |
agl@chromium.org | 13c8558 | 2010-01-04 23:56:43 +0000 | [diff] [blame] | 1215 | rec->fFlags |= SkScalerContext::kEmbeddedBitmapText_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1216 | } |
| 1217 | if (paint.isSubpixelText()) { |
agl@chromium.org | a2c71cb | 2010-06-17 20:49:17 +0000 | [diff] [blame] | 1218 | rec->fFlags |= SkScalerContext::kSubpixelPositioning_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1219 | } |
| 1220 | if (paint.isAutohinted()) { |
agl@chromium.org | a2c71cb | 2010-06-17 20:49:17 +0000 | [diff] [blame] | 1221 | rec->fFlags |= SkScalerContext::kAutohinting_Flag; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1222 | } |
reed@android.com | 36a4c2a | 2009-07-22 19:52:11 +0000 | [diff] [blame] | 1223 | |
| 1224 | /* Allow the fonthost to modify our rec before we use it as a key into the |
| 1225 | cache. This way if we're asking for something that they will ignore, |
| 1226 | they can modify our rec up front, so we don't create duplicate cache |
| 1227 | entries. |
| 1228 | */ |
| 1229 | SkFontHost::FilterRec(rec); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | #define MIN_SIZE_FOR_EFFECT_BUFFER 1024 |
| 1233 | |
| 1234 | void SkPaint::descriptorProc(const SkMatrix* deviceMatrix, |
| 1235 | void (*proc)(const SkDescriptor*, void*), |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1236 | void* context) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1237 | SkScalerContext::Rec rec; |
| 1238 | |
| 1239 | SkScalerContext::MakeRec(*this, deviceMatrix, &rec); |
| 1240 | |
| 1241 | size_t descSize = sizeof(rec); |
| 1242 | int entryCount = 1; |
| 1243 | SkPathEffect* pe = this->getPathEffect(); |
| 1244 | SkMaskFilter* mf = this->getMaskFilter(); |
| 1245 | SkRasterizer* ra = this->getRasterizer(); |
| 1246 | |
| 1247 | SkFlattenableWriteBuffer peBuffer(MIN_SIZE_FOR_EFFECT_BUFFER); |
| 1248 | SkFlattenableWriteBuffer mfBuffer(MIN_SIZE_FOR_EFFECT_BUFFER); |
| 1249 | SkFlattenableWriteBuffer raBuffer(MIN_SIZE_FOR_EFFECT_BUFFER); |
| 1250 | |
| 1251 | if (pe) { |
| 1252 | peBuffer.writeFlattenable(pe); |
| 1253 | descSize += peBuffer.size(); |
| 1254 | entryCount += 1; |
| 1255 | rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion |
| 1256 | // seems like we could support kLCD as well at this point... |
| 1257 | } |
| 1258 | if (mf) { |
| 1259 | mfBuffer.writeFlattenable(mf); |
| 1260 | descSize += mfBuffer.size(); |
| 1261 | entryCount += 1; |
| 1262 | rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing with maskfilters |
| 1263 | } |
| 1264 | if (ra) { |
| 1265 | raBuffer.writeFlattenable(ra); |
| 1266 | descSize += raBuffer.size(); |
| 1267 | entryCount += 1; |
| 1268 | rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion |
| 1269 | } |
| 1270 | descSize += SkDescriptor::ComputeOverhead(entryCount); |
| 1271 | |
| 1272 | SkAutoDescriptor ad(descSize); |
| 1273 | SkDescriptor* desc = ad.getDesc(); |
| 1274 | |
| 1275 | desc->init(); |
| 1276 | desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 1277 | |
| 1278 | if (pe) { |
| 1279 | add_flattenable(desc, kPathEffect_SkDescriptorTag, &peBuffer); |
| 1280 | } |
| 1281 | if (mf) { |
| 1282 | add_flattenable(desc, kMaskFilter_SkDescriptorTag, &mfBuffer); |
| 1283 | } |
| 1284 | if (ra) { |
| 1285 | add_flattenable(desc, kRasterizer_SkDescriptorTag, &raBuffer); |
| 1286 | } |
| 1287 | |
| 1288 | SkASSERT(descSize == desc->getLength()); |
| 1289 | desc->computeChecksum(); |
| 1290 | |
| 1291 | proc(desc, context); |
| 1292 | } |
| 1293 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1294 | static void DetachDescProc(const SkDescriptor* desc, void* context) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1295 | *((SkGlyphCache**)context) = SkGlyphCache::DetachCache(desc); |
| 1296 | } |
| 1297 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1298 | SkGlyphCache* SkPaint::detachCache(const SkMatrix* deviceMatrix) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1299 | SkGlyphCache* cache; |
| 1300 | this->descriptorProc(deviceMatrix, DetachDescProc, &cache); |
| 1301 | return cache; |
| 1302 | } |
| 1303 | |
| 1304 | /////////////////////////////////////////////////////////////////////////////// |
| 1305 | |
| 1306 | #include "SkStream.h" |
| 1307 | |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1308 | static uintptr_t asint(const void* p) { |
| 1309 | return reinterpret_cast<uintptr_t>(p); |
| 1310 | } |
| 1311 | |
| 1312 | union Scalar32 { |
| 1313 | SkScalar fScalar; |
| 1314 | uint32_t f32; |
| 1315 | }; |
| 1316 | |
| 1317 | static uint32_t* write_scalar(uint32_t* ptr, SkScalar value) { |
| 1318 | SkASSERT(sizeof(SkScalar) == sizeof(uint32_t)); |
| 1319 | Scalar32 tmp; |
| 1320 | tmp.fScalar = value; |
| 1321 | *ptr = tmp.f32; |
| 1322 | return ptr + 1; |
| 1323 | } |
| 1324 | |
| 1325 | static SkScalar read_scalar(const uint32_t*& ptr) { |
| 1326 | SkASSERT(sizeof(SkScalar) == sizeof(uint32_t)); |
| 1327 | Scalar32 tmp; |
| 1328 | tmp.f32 = *ptr++; |
| 1329 | return tmp.fScalar; |
| 1330 | } |
| 1331 | |
| 1332 | static uint32_t pack_4(unsigned a, unsigned b, unsigned c, unsigned d) { |
| 1333 | SkASSERT(a == (uint8_t)a); |
| 1334 | SkASSERT(b == (uint8_t)b); |
| 1335 | SkASSERT(c == (uint8_t)c); |
| 1336 | SkASSERT(d == (uint8_t)d); |
| 1337 | return (a << 24) | (b << 16) | (c << 8) | d; |
| 1338 | } |
| 1339 | |
| 1340 | enum FlatFlags { |
| 1341 | kHasTypeface_FlatFlag = 0x01, |
| 1342 | kHasEffects_FlatFlag = 0x02 |
| 1343 | }; |
| 1344 | |
| 1345 | // The size of a flat paint's POD fields |
| 1346 | static const uint32_t kPODPaintSize = 5 * sizeof(SkScalar) + |
| 1347 | 1 * sizeof(SkColor) + |
| 1348 | 1 * sizeof(uint16_t) + |
| 1349 | 6 * sizeof(uint8_t); |
| 1350 | |
| 1351 | /* To save space/time, we analyze the paint, and write a truncated version of |
| 1352 | it if there are not tricky elements like shaders, etc. |
| 1353 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1354 | void SkPaint::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1355 | uint8_t flatFlags = 0; |
| 1356 | if (this->getTypeface()) { |
| 1357 | flatFlags |= kHasTypeface_FlatFlag; |
| 1358 | } |
| 1359 | if (asint(this->getPathEffect()) | |
| 1360 | asint(this->getShader()) | |
| 1361 | asint(this->getXfermode()) | |
| 1362 | asint(this->getMaskFilter()) | |
| 1363 | asint(this->getColorFilter()) | |
| 1364 | asint(this->getRasterizer()) | |
| 1365 | asint(this->getLooper())) { |
| 1366 | flatFlags |= kHasEffects_FlatFlag; |
| 1367 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1368 | |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1369 | SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); |
| 1370 | uint32_t* ptr = buffer.reserve(kPODPaintSize); |
| 1371 | |
| 1372 | ptr = write_scalar(ptr, this->getTextSize()); |
| 1373 | ptr = write_scalar(ptr, this->getTextScaleX()); |
| 1374 | ptr = write_scalar(ptr, this->getTextSkewX()); |
| 1375 | ptr = write_scalar(ptr, this->getStrokeWidth()); |
| 1376 | ptr = write_scalar(ptr, this->getStrokeMiter()); |
| 1377 | *ptr++ = this->getColor(); |
| 1378 | *ptr++ = (this->getFlags() << 16) | (this->getTextAlign() << 8) | flatFlags; |
| 1379 | *ptr++ = pack_4(this->getStrokeCap(), this->getStrokeJoin(), |
| 1380 | this->getStyle(), this->getTextEncoding()); |
| 1381 | |
| 1382 | // now we're done with ptr and the (pre)reserved space. If we need to write |
| 1383 | // additional fields, use the buffer directly |
| 1384 | if (flatFlags & kHasTypeface_FlatFlag) { |
| 1385 | buffer.writeTypeface(this->getTypeface()); |
| 1386 | } |
| 1387 | if (flatFlags & kHasEffects_FlatFlag) { |
| 1388 | buffer.writeFlattenable(this->getPathEffect()); |
| 1389 | buffer.writeFlattenable(this->getShader()); |
| 1390 | buffer.writeFlattenable(this->getXfermode()); |
| 1391 | buffer.writeFlattenable(this->getMaskFilter()); |
| 1392 | buffer.writeFlattenable(this->getColorFilter()); |
| 1393 | buffer.writeFlattenable(this->getRasterizer()); |
| 1394 | buffer.writeFlattenable(this->getLooper()); |
| 1395 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | void SkPaint::unflatten(SkFlattenableReadBuffer& buffer) { |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1399 | SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); |
| 1400 | const void* podData = buffer.skip(kPODPaintSize); |
| 1401 | const uint32_t* pod = reinterpret_cast<const uint32_t*>(podData); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1402 | |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1403 | // the order we read must match the order we wrote in flatten() |
| 1404 | this->setTextSize(read_scalar(pod)); |
| 1405 | this->setTextScaleX(read_scalar(pod)); |
| 1406 | this->setTextSkewX(read_scalar(pod)); |
| 1407 | this->setStrokeWidth(read_scalar(pod)); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1408 | this->setStrokeMiter(read_scalar(pod)); |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1409 | this->setColor(*pod++); |
| 1410 | |
| 1411 | uint32_t tmp = *pod++; |
| 1412 | this->setFlags(tmp >> 16); |
| 1413 | this->setTextAlign(static_cast<Align>((tmp >> 8) & 0xFF)); |
| 1414 | uint8_t flatFlags = tmp & 0xFF; |
| 1415 | |
| 1416 | tmp = *pod++; |
| 1417 | this->setStrokeCap(static_cast<Cap>((tmp >> 24) & 0xFF)); |
| 1418 | this->setStrokeJoin(static_cast<Join>((tmp >> 16) & 0xFF)); |
| 1419 | this->setStyle(static_cast<Style>((tmp >> 8) & 0xFF)); |
| 1420 | this->setTextEncoding(static_cast<TextEncoding>((tmp >> 0) & 0xFF)); |
| 1421 | |
| 1422 | if (flatFlags & kHasTypeface_FlatFlag) { |
| 1423 | this->setTypeface(buffer.readTypeface()); |
| 1424 | } else { |
| 1425 | this->setTypeface(NULL); |
| 1426 | } |
| 1427 | |
| 1428 | if (flatFlags & kHasEffects_FlatFlag) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1429 | SkSafeUnref(this->setPathEffect((SkPathEffect*) buffer.readFlattenable())); |
| 1430 | SkSafeUnref(this->setShader((SkShader*) buffer.readFlattenable())); |
| 1431 | SkSafeUnref(this->setXfermode((SkXfermode*) buffer.readFlattenable())); |
| 1432 | SkSafeUnref(this->setMaskFilter((SkMaskFilter*) buffer.readFlattenable())); |
| 1433 | SkSafeUnref(this->setColorFilter((SkColorFilter*) buffer.readFlattenable())); |
| 1434 | SkSafeUnref(this->setRasterizer((SkRasterizer*) buffer.readFlattenable())); |
| 1435 | SkSafeUnref(this->setLooper((SkDrawLooper*) buffer.readFlattenable())); |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1436 | } else { |
| 1437 | this->setPathEffect(NULL); |
| 1438 | this->setShader(NULL); |
| 1439 | this->setXfermode(NULL); |
| 1440 | this->setMaskFilter(NULL); |
| 1441 | this->setColorFilter(NULL); |
| 1442 | this->setRasterizer(NULL); |
| 1443 | this->setLooper(NULL); |
| 1444 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /////////////////////////////////////////////////////////////////////////////// |
| 1448 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1449 | SkShader* SkPaint::setShader(SkShader* shader) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1450 | SkRefCnt_SafeAssign(fShader, shader); |
| 1451 | return shader; |
| 1452 | } |
| 1453 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1454 | SkColorFilter* SkPaint::setColorFilter(SkColorFilter* filter) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1455 | SkRefCnt_SafeAssign(fColorFilter, filter); |
| 1456 | return filter; |
| 1457 | } |
| 1458 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1459 | SkXfermode* SkPaint::setXfermode(SkXfermode* mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1460 | SkRefCnt_SafeAssign(fXfermode, mode); |
| 1461 | return mode; |
| 1462 | } |
| 1463 | |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 1464 | SkXfermode* SkPaint::setXfermodeMode(SkXfermode::Mode mode) { |
reed@android.com | d66eef7 | 2009-06-24 12:29:16 +0000 | [diff] [blame] | 1465 | SkSafeUnref(fXfermode); |
| 1466 | fXfermode = SkXfermode::Create(mode); |
| 1467 | return fXfermode; |
| 1468 | } |
| 1469 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1470 | SkPathEffect* SkPaint::setPathEffect(SkPathEffect* effect) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1471 | SkRefCnt_SafeAssign(fPathEffect, effect); |
| 1472 | return effect; |
| 1473 | } |
| 1474 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1475 | SkMaskFilter* SkPaint::setMaskFilter(SkMaskFilter* filter) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1476 | SkRefCnt_SafeAssign(fMaskFilter, filter); |
| 1477 | return filter; |
| 1478 | } |
| 1479 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1480 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1481 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1482 | bool SkPaint::getFillPath(const SkPath& src, SkPath* dst) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1483 | SkPath effectPath, strokePath; |
| 1484 | const SkPath* path = &src; |
| 1485 | |
| 1486 | SkScalar width = this->getStrokeWidth(); |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1487 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1488 | switch (this->getStyle()) { |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1489 | case SkPaint::kFill_Style: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1490 | width = -1; // mark it as no-stroke |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1491 | break; |
| 1492 | case SkPaint::kStrokeAndFill_Style: |
| 1493 | if (width == 0) { |
| 1494 | width = -1; // mark it as no-stroke |
| 1495 | } |
| 1496 | break; |
| 1497 | case SkPaint::kStroke_Style: |
| 1498 | break; |
| 1499 | default: |
| 1500 | SkASSERT(!"unknown paint style"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1503 | if (this->getPathEffect()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1504 | // lie to the pathEffect if our style is strokeandfill, so that it treats us as just fill |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1505 | if (this->getStyle() == SkPaint::kStrokeAndFill_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1506 | width = -1; // mark it as no-stroke |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1507 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1508 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1509 | if (this->getPathEffect()->filterPath(&effectPath, src, &width)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1510 | path = &effectPath; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1511 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1512 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1513 | // restore the width if we earlier had to lie, and if we're still set to no-stroke |
| 1514 | // note: if we're now stroke (width >= 0), then the pathEffect asked for that change |
| 1515 | // and we want to respect that (i.e. don't overwrite their setting for width) |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1516 | if (this->getStyle() == SkPaint::kStrokeAndFill_Style && width < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1517 | width = this->getStrokeWidth(); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1518 | if (width == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1519 | width = -1; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1520 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1521 | } |
| 1522 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1523 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1524 | if (width > 0 && !path->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1525 | SkStroke stroker(*this, width); |
| 1526 | stroker.strokePath(*path, &strokePath); |
| 1527 | path = &strokePath; |
| 1528 | } |
| 1529 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1530 | if (path == &src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1531 | *dst = src; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1532 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1533 | SkASSERT(path == &effectPath || path == &strokePath); |
| 1534 | dst->swap(*(SkPath*)path); |
| 1535 | } |
| 1536 | |
| 1537 | return width != 0; // return true if we're filled, or false if we're hairline (width == 0) |
| 1538 | } |
| 1539 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1540 | const SkRect& SkPaint::computeStrokeFastBounds(const SkRect& src, |
| 1541 | SkRect* storage) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1542 | SkASSERT(storage); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1543 | SkASSERT(this->getStyle() != SkPaint::kFill_Style); |
| 1544 | |
| 1545 | // since we're stroked, outset the rect by the radius (and join type) |
| 1546 | SkScalar radius = SkScalarHalf(this->getStrokeWidth()); |
| 1547 | if (0 == radius) { // hairline |
| 1548 | radius = SK_Scalar1; |
| 1549 | } else if (this->getStrokeJoin() == SkPaint::kMiter_Join) { |
| 1550 | SkScalar scale = this->getStrokeMiter(); |
| 1551 | if (scale > SK_Scalar1) { |
| 1552 | radius = SkScalarMul(radius, scale); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1553 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1554 | } |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1555 | storage->set(src.fLeft - radius, src.fTop - radius, |
| 1556 | src.fRight + radius, src.fBottom + radius); |
| 1557 | return *storage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1560 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1561 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1562 | static bool has_thick_frame(const SkPaint& paint) { |
| 1563 | return paint.getStrokeWidth() > 0 && |
| 1564 | paint.getStyle() != SkPaint::kFill_Style; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | SkTextToPathIter::SkTextToPathIter( const char text[], size_t length, |
| 1568 | const SkPaint& paint, |
| 1569 | bool applyStrokeAndPathEffects, |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1570 | bool forceLinearTextOn) : fPaint(paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1571 | fGlyphCacheProc = paint.getMeasureCacheProc(SkPaint::kForward_TextBufferDirection, |
| 1572 | true); |
| 1573 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1574 | if (forceLinearTextOn) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1575 | fPaint.setLinearText(true); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1576 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1577 | fPaint.setMaskFilter(NULL); // don't want this affecting our path-cache lookup |
| 1578 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1579 | if (fPaint.getPathEffect() == NULL && !has_thick_frame(fPaint)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1580 | applyStrokeAndPathEffects = false; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1581 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1582 | |
| 1583 | // can't use our canonical size if we need to apply patheffects/strokes |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1584 | if (fPaint.isLinearText() && !applyStrokeAndPathEffects) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1585 | fPaint.setTextSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths)); |
| 1586 | fScale = paint.getTextSize() / SkPaint::kCanonicalTextSizeForPaths; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1587 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1588 | fScale = SK_Scalar1; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1589 | } |
reed@google.com | 72cf492 | 2011-01-04 19:58:20 +0000 | [diff] [blame] | 1590 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1591 | if (!applyStrokeAndPathEffects) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1592 | fPaint.setStyle(SkPaint::kFill_Style); |
| 1593 | fPaint.setPathEffect(NULL); |
| 1594 | } |
| 1595 | |
| 1596 | fCache = fPaint.detachCache(NULL); |
| 1597 | |
| 1598 | SkPaint::Style style = SkPaint::kFill_Style; |
| 1599 | SkPathEffect* pe = NULL; |
| 1600 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1601 | if (!applyStrokeAndPathEffects) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1602 | style = paint.getStyle(); // restore |
| 1603 | pe = paint.getPathEffect(); // restore |
| 1604 | } |
| 1605 | fPaint.setStyle(style); |
| 1606 | fPaint.setPathEffect(pe); |
| 1607 | fPaint.setMaskFilter(paint.getMaskFilter()); // restore |
| 1608 | |
| 1609 | // now compute fXOffset if needed |
| 1610 | |
| 1611 | SkScalar xOffset = 0; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1612 | if (paint.getTextAlign() != SkPaint::kLeft_Align) { // need to measure first |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1613 | int count; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1614 | SkScalar width = SkScalarMul(fPaint.measure_text(fCache, text, length, |
| 1615 | &count, NULL), fScale); |
| 1616 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1617 | width = SkScalarHalf(width); |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1618 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1619 | xOffset = -width; |
| 1620 | } |
| 1621 | fXPos = xOffset; |
| 1622 | fPrevAdvance = 0; |
| 1623 | |
| 1624 | fText = text; |
| 1625 | fStop = text + length; |
| 1626 | } |
| 1627 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1628 | SkTextToPathIter::~SkTextToPathIter() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1629 | SkGlyphCache::AttachCache(fCache); |
| 1630 | } |
| 1631 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1632 | const SkPath* SkTextToPathIter::next(SkScalar* xpos) { |
| 1633 | while (fText < fStop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1634 | const SkGlyph& glyph = fGlyphCacheProc(fCache, &fText); |
| 1635 | |
| 1636 | fXPos += SkScalarMul(SkFixedToScalar(fPrevAdvance + fAutoKern.adjust(glyph)), fScale); |
| 1637 | fPrevAdvance = glyph.fAdvanceX; // + fPaint.getTextTracking(); |
| 1638 | |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1639 | if (glyph.fWidth) { |
| 1640 | if (xpos) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1641 | *xpos = fXPos; |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1642 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1643 | return fCache->findPath(glyph); |
| 1644 | } |
| 1645 | } |
| 1646 | return NULL; |
| 1647 | } |
reed@google.com | 6fb7e2e | 2011-02-08 22:22:52 +0000 | [diff] [blame^] | 1648 | |