epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkDrawPaint.h" |
| 9 | #include "SkAnimateMaker.h" |
| 10 | #include "SkDrawColor.h" |
| 11 | #include "SkDrawShader.h" |
| 12 | #include "SkMaskFilter.h" |
reed@google.com | 8d4dc71 | 2014-04-18 15:14:25 +0000 | [diff] [blame] | 13 | #include "SkPaintPart.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkPathEffect.h" |
| 15 | |
| 16 | enum SkPaint_Functions { |
| 17 | SK_FUNCTION(measureText) |
| 18 | }; |
| 19 | |
| 20 | enum SkPaint_Properties { |
| 21 | SK_PROPERTY(ascent), |
| 22 | SK_PROPERTY(descent) |
| 23 | }; |
| 24 | |
| 25 | // !!! in the future, this could be compiled by build-condensed-info into an array of parameters |
| 26 | // with a lookup table to find the first parameter -- for now, it is iteratively searched through |
| 27 | const SkFunctionParamType SkDrawPaint::fFunctionParameters[] = { |
| 28 | (SkFunctionParamType) SkType_String, |
| 29 | (SkFunctionParamType) 0 // terminator for parameter list (there may be multiple parameter lists) |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | #if SK_USE_CONDENSED_INFO == 0 |
| 34 | |
| 35 | const SkMemberInfo SkDrawPaint::fInfo[] = { |
| 36 | SK_MEMBER(antiAlias, Boolean), |
| 37 | SK_MEMBER_PROPERTY(ascent, Float), |
| 38 | SK_MEMBER(color, Color), |
| 39 | SK_MEMBER_PROPERTY(descent, Float), |
| 40 | SK_MEMBER(fakeBold, Boolean), |
| 41 | SK_MEMBER(filterBitmap, Boolean), |
| 42 | SK_MEMBER(linearText, Boolean), |
| 43 | SK_MEMBER(maskFilter, MaskFilter), |
| 44 | SK_MEMBER_FUNCTION(measureText, Float), |
| 45 | SK_MEMBER(pathEffect, PathEffect), |
| 46 | SK_MEMBER(shader, Shader), |
| 47 | SK_MEMBER(strikeThru, Boolean), |
| 48 | SK_MEMBER(stroke, Boolean), |
| 49 | SK_MEMBER(strokeCap, Cap), |
| 50 | SK_MEMBER(strokeJoin, Join), |
| 51 | SK_MEMBER(strokeMiter, Float), |
| 52 | SK_MEMBER(strokeWidth, Float), |
| 53 | SK_MEMBER(style, Style), |
| 54 | SK_MEMBER(textAlign, Align), |
| 55 | SK_MEMBER(textScaleX, Float), |
| 56 | SK_MEMBER(textSize, Float), |
| 57 | SK_MEMBER(textSkewX, Float), |
| 58 | SK_MEMBER(typeface, Typeface), |
| 59 | SK_MEMBER(underline, Boolean), |
| 60 | SK_MEMBER(xfermode, Xfermode) |
| 61 | }; |
| 62 | |
| 63 | #endif |
| 64 | |
| 65 | DEFINE_GET_MEMBER(SkDrawPaint); |
| 66 | |
| 67 | SkDrawPaint::SkDrawPaint() : antiAlias(-1), color(NULL), fakeBold(-1), filterBitmap(-1), |
| 68 | linearText(-1), maskFilter((SkDrawMaskFilter*) -1), pathEffect((SkDrawPathEffect*) -1), |
| 69 | shader((SkDrawShader*) -1), strikeThru(-1), stroke(-1), |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 70 | strokeCap((SkPaint::Cap) -1), strokeJoin((SkPaint::Join) -1), strokeMiter(SK_ScalarNaN), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | strokeWidth(SK_ScalarNaN), style((SkPaint::Style) -1), |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 72 | textAlign((SkPaint::Align) -1), textScaleX(SK_ScalarNaN), textSize(SK_ScalarNaN), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | textSkewX(SK_ScalarNaN), typeface((SkDrawTypeface*) -1), |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 74 | underline(-1), xfermode((SkXfermode::Mode) -1), fOwnsColor(false), fOwnsMaskFilter(false), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | fOwnsPathEffect(false), fOwnsShader(false), fOwnsTypeface(false) { |
| 76 | } |
| 77 | |
| 78 | SkDrawPaint::~SkDrawPaint() { |
| 79 | if (fOwnsColor) |
| 80 | delete color; |
| 81 | if (fOwnsMaskFilter) |
| 82 | delete maskFilter; |
| 83 | if (fOwnsPathEffect) |
| 84 | delete pathEffect; |
| 85 | if (fOwnsShader) |
| 86 | delete shader; |
| 87 | if (fOwnsTypeface) |
| 88 | delete typeface; |
| 89 | } |
| 90 | |
tomhudson@google.com | d6caf2e | 2011-07-08 14:41:12 +0000 | [diff] [blame] | 91 | bool SkDrawPaint::add(SkAnimateMaker* maker, SkDisplayable* child) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | SkASSERT(child && child->isPaintPart()); |
| 93 | SkPaintPart* part = (SkPaintPart*) child; |
tomhudson@google.com | d6caf2e | 2011-07-08 14:41:12 +0000 | [diff] [blame] | 94 | if (part->add() && maker) |
| 95 | maker->setErrorCode(SkDisplayXMLParserError::kErrorAddingToPaint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | SkDisplayable* SkDrawPaint::deepCopy(SkAnimateMaker* maker) { |
| 100 | SkDrawColor* tempColor = color; |
| 101 | color = NULL; |
| 102 | SkDrawPaint* copy = (SkDrawPaint*) INHERITED::deepCopy(maker); |
| 103 | color = tempColor; |
| 104 | tempColor = (SkDrawColor*) color->deepCopy(maker); |
| 105 | tempColor->setParent(copy); |
| 106 | tempColor->add(); |
| 107 | copy->fOwnsColor = true; |
| 108 | return copy; |
| 109 | } |
| 110 | |
| 111 | bool SkDrawPaint::draw(SkAnimateMaker& maker) { |
| 112 | SkPaint* paint = maker.fPaint; |
| 113 | setupPaint(paint); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | #ifdef SK_DUMP_ENABLED |
| 118 | void SkDrawPaint::dump(SkAnimateMaker* maker) { |
| 119 | dumpBase(maker); |
| 120 | dumpAttrs(maker); |
| 121 | bool closedYet = false; |
| 122 | SkDisplayList::fIndent +=4; |
| 123 | //should i say if (maskFilter && ...? |
| 124 | if (maskFilter != (SkDrawMaskFilter*)-1) { |
| 125 | SkDebugf(">\n"); |
| 126 | maskFilter->dump(maker); |
| 127 | closedYet = true; |
| 128 | } |
| 129 | if (pathEffect != (SkDrawPathEffect*) -1) { |
| 130 | if (closedYet == false) { |
| 131 | SkDebugf(">\n"); |
| 132 | closedYet = true; |
| 133 | } |
| 134 | pathEffect->dump(maker); |
| 135 | } |
| 136 | if (fOwnsTypeface) { |
| 137 | if (closedYet == false) { |
| 138 | SkDebugf(">\n"); |
| 139 | closedYet = true; |
| 140 | } |
| 141 | typeface->dump(maker); |
| 142 | } |
| 143 | SkDisplayList::fIndent -= 4; |
| 144 | dumpChildren(maker, closedYet); |
| 145 | } |
| 146 | #endif |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 147 | |
| 148 | void SkDrawPaint::executeFunction(SkDisplayable* target, int index, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | SkTDArray<SkScriptValue>& parameters, SkDisplayTypes type, |
| 150 | SkScriptValue* scriptValue) { |
| 151 | if (scriptValue == NULL) |
| 152 | return; |
| 153 | SkASSERT(target == this); |
| 154 | switch (index) { |
| 155 | case SK_FUNCTION(measureText): { |
| 156 | SkASSERT(parameters.count() == 1); |
| 157 | SkASSERT(type == SkType_Float); |
| 158 | SkPaint paint; |
| 159 | setupPaint(&paint); |
| 160 | scriptValue->fType = SkType_Float; |
| 161 | SkASSERT(parameters[0].fType == SkType_String); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 162 | scriptValue->fOperand.fScalar = paint.measureText(parameters[0].fOperand.fString->c_str(), |
| 163 | parameters[0].fOperand.fString->size()); |
| 164 | // SkDebugf("measureText: %s = %g\n", parameters[0].fOperand.fString->c_str(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | // scriptValue->fOperand.fScalar / 65536.0f); |
| 166 | } break; |
| 167 | default: |
| 168 | SkASSERT(0); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | const SkFunctionParamType* SkDrawPaint::getFunctionsParameters() { |
| 173 | return fFunctionParameters; |
| 174 | } |
| 175 | |
| 176 | bool SkDrawPaint::getProperty(int index, SkScriptValue* value) const { |
| 177 | SkPaint::FontMetrics metrics; |
| 178 | SkPaint paint; |
| 179 | setupPaint(&paint); |
| 180 | paint.getFontMetrics(&metrics); |
| 181 | switch (index) { |
| 182 | case SK_PROPERTY(ascent): |
| 183 | value->fOperand.fScalar = metrics.fAscent; |
| 184 | break; |
| 185 | case SK_PROPERTY(descent): |
| 186 | value->fOperand.fScalar = metrics.fDescent; |
| 187 | break; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 188 | // should consider returning fLeading as well (or roll it into ascent/descent somehow |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | default: |
| 190 | SkASSERT(0); |
| 191 | return false; |
| 192 | } |
| 193 | value->fType = SkType_Float; |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool SkDrawPaint::resolveIDs(SkAnimateMaker& maker, SkDisplayable* origDisp, SkApply* ) { |
| 198 | SkASSERT(origDisp->isPaint()); |
| 199 | SkDrawPaint* original = (SkDrawPaint*) origDisp; |
| 200 | if (fOwnsColor && maker.resolveID(color, original->color) == false) |
| 201 | return true; |
| 202 | if (fOwnsMaskFilter && maker.resolveID(maskFilter, original->maskFilter) == false) |
| 203 | return true; |
| 204 | if (fOwnsPathEffect && maker.resolveID(pathEffect, original->pathEffect) == false) |
| 205 | return true; |
| 206 | if (fOwnsShader && maker.resolveID(shader, original->shader) == false) |
| 207 | return true; |
| 208 | if (fOwnsTypeface && maker.resolveID(typeface, original->typeface) == false) |
| 209 | return true; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 210 | return false; // succeeded |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void SkDrawPaint::setupPaint(SkPaint* paint) const { |
| 214 | if (antiAlias != -1) |
| 215 | paint->setAntiAlias(SkToBool(antiAlias)); |
| 216 | if (color != NULL) |
| 217 | paint->setColor(color->getColor()); |
| 218 | if (fakeBold != -1) |
| 219 | paint->setFakeBoldText(SkToBool(fakeBold)); |
| 220 | if (filterBitmap != -1) |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 221 | paint->setFilterLevel(filterBitmap ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | // stroke is legacy; style setting if present overrides stroke |
| 223 | if (stroke != -1) |
| 224 | paint->setStyle(SkToBool(stroke) ? SkPaint::kStroke_Style : SkPaint::kFill_Style); |
tfarina@chromium.org | e80c2bc | 2013-03-29 16:53:30 +0000 | [diff] [blame] | 225 | if (style != -1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | paint->setStyle((SkPaint::Style) style); |
| 227 | if (linearText != -1) |
| 228 | paint->setLinearText(SkToBool(linearText)); |
| 229 | if (maskFilter == NULL) |
| 230 | paint->setMaskFilter(NULL); |
| 231 | else if (maskFilter != (SkDrawMaskFilter*) -1) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 232 | SkSafeUnref(paint->setMaskFilter(maskFilter->getMaskFilter())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | if (pathEffect == NULL) |
| 234 | paint->setPathEffect(NULL); |
| 235 | else if (pathEffect != (SkDrawPathEffect*) -1) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 236 | SkSafeUnref(paint->setPathEffect(pathEffect->getPathEffect())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | if (shader == NULL) |
| 238 | paint->setShader(NULL); |
| 239 | else if (shader != (SkDrawShader*) -1) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 240 | SkSafeUnref(paint->setShader(shader->getShader())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | if (strikeThru != -1) |
| 242 | paint->setStrikeThruText(SkToBool(strikeThru)); |
tfarina@chromium.org | e80c2bc | 2013-03-29 16:53:30 +0000 | [diff] [blame] | 243 | if (strokeCap != -1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | paint->setStrokeCap((SkPaint::Cap) strokeCap); |
tfarina@chromium.org | e80c2bc | 2013-03-29 16:53:30 +0000 | [diff] [blame] | 245 | if (strokeJoin != -1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 246 | paint->setStrokeJoin((SkPaint::Join) strokeJoin); |
| 247 | if (SkScalarIsNaN(strokeMiter) == false) |
| 248 | paint->setStrokeMiter(strokeMiter); |
| 249 | if (SkScalarIsNaN(strokeWidth) == false) |
| 250 | paint->setStrokeWidth(strokeWidth); |
tfarina@chromium.org | e80c2bc | 2013-03-29 16:53:30 +0000 | [diff] [blame] | 251 | if (textAlign != -1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | paint->setTextAlign((SkPaint::Align) textAlign); |
| 253 | if (SkScalarIsNaN(textScaleX) == false) |
| 254 | paint->setTextScaleX(textScaleX); |
| 255 | if (SkScalarIsNaN(textSize) == false) |
| 256 | paint->setTextSize(textSize); |
| 257 | if (SkScalarIsNaN(textSkewX) == false) |
| 258 | paint->setTextSkewX(textSkewX); |
| 259 | if (typeface == NULL) |
| 260 | paint->setTypeface(NULL); |
| 261 | else if (typeface != (SkDrawTypeface*) -1) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 262 | SkSafeUnref(paint->setTypeface(typeface->getTypeface())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | if (underline != -1) |
| 264 | paint->setUnderlineText(SkToBool(underline)); |
tfarina@chromium.org | e80c2bc | 2013-03-29 16:53:30 +0000 | [diff] [blame] | 265 | if (xfermode != -1) |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 266 | paint->setXfermodeMode((SkXfermode::Mode) xfermode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | } |