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