2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkDrawPaint.h"
9 #include "SkAnimateMaker.h"
10 #include "SkDrawColor.h"
11 #include "SkDrawShader.h"
12 #include "SkMaskFilter.h"
13 #include "SkPaintPart.h"
14 #include "SkPathEffect.h"
16 enum SkPaint_Functions {
17 SK_FUNCTION(measureText)
20 enum SkPaint_Properties {
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)
33 #if SK_USE_CONDENSED_INFO == 0
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)
65 DEFINE_GET_MEMBER(SkDrawPaint);
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),
70 strokeCap((SkPaint::Cap) -1), strokeJoin((SkPaint::Join) -1), strokeMiter(SK_ScalarNaN),
71 strokeWidth(SK_ScalarNaN), style((SkPaint::Style) -1),
72 textAlign((SkPaint::Align) -1), textScaleX(SK_ScalarNaN), textSize(SK_ScalarNaN),
73 textSkewX(SK_ScalarNaN), typeface((SkDrawTypeface*) -1),
74 underline(-1), xfermode((SkXfermode::Mode) -1), fOwnsColor(false), fOwnsMaskFilter(false),
75 fOwnsPathEffect(false), fOwnsShader(false), fOwnsTypeface(false) {
78 SkDrawPaint::~SkDrawPaint() {
91 bool SkDrawPaint::add(SkAnimateMaker* maker, SkDisplayable* child) {
92 SkASSERT(child && child->isPaintPart());
93 SkPaintPart* part = (SkPaintPart*) child;
94 if (part->add() && maker)
95 maker->setErrorCode(SkDisplayXMLParserError::kErrorAddingToPaint);
99 SkDisplayable* SkDrawPaint::deepCopy(SkAnimateMaker* maker) {
100 SkDrawColor* tempColor = color;
102 SkDrawPaint* copy = (SkDrawPaint*) INHERITED::deepCopy(maker);
104 tempColor = (SkDrawColor*) color->deepCopy(maker);
105 tempColor->setParent(copy);
107 copy->fOwnsColor = true;
111 bool SkDrawPaint::draw(SkAnimateMaker& maker) {
112 SkPaint* paint = maker.fPaint;
117 #ifdef SK_DUMP_ENABLED
118 void SkDrawPaint::dump(SkAnimateMaker* maker) {
121 bool closedYet = false;
122 SkDisplayList::fIndent +=4;
123 //should i say if (maskFilter && ...?
124 if (maskFilter != (SkDrawMaskFilter*)-1) {
126 maskFilter->dump(maker);
129 if (pathEffect != (SkDrawPathEffect*) -1) {
130 if (closedYet == false) {
134 pathEffect->dump(maker);
137 if (closedYet == false) {
141 typeface->dump(maker);
143 SkDisplayList::fIndent -= 4;
144 dumpChildren(maker, closedYet);
148 void SkDrawPaint::executeFunction(SkDisplayable* target, int index,
149 SkTDArray<SkScriptValue>& parameters, SkDisplayTypes type,
150 SkScriptValue* scriptValue) {
151 if (scriptValue == NULL)
153 SkASSERT(target == this);
155 case SK_FUNCTION(measureText): {
156 SkASSERT(parameters.count() == 1);
157 SkASSERT(type == SkType_Float);
160 scriptValue->fType = SkType_Float;
161 SkASSERT(parameters[0].fType == SkType_String);
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(),
165 // scriptValue->fOperand.fScalar / 65536.0f);
172 const SkFunctionParamType* SkDrawPaint::getFunctionsParameters() {
173 return fFunctionParameters;
176 bool SkDrawPaint::getProperty(int index, SkScriptValue* value) const {
177 SkPaint::FontMetrics metrics;
180 paint.getFontMetrics(&metrics);
182 case SK_PROPERTY(ascent):
183 value->fOperand.fScalar = metrics.fAscent;
185 case SK_PROPERTY(descent):
186 value->fOperand.fScalar = metrics.fDescent;
188 // should consider returning fLeading as well (or roll it into ascent/descent somehow
193 value->fType = SkType_Float;
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)
202 if (fOwnsMaskFilter && maker.resolveID(maskFilter, original->maskFilter) == false)
204 if (fOwnsPathEffect && maker.resolveID(pathEffect, original->pathEffect) == false)
206 if (fOwnsShader && maker.resolveID(shader, original->shader) == false)
208 if (fOwnsTypeface && maker.resolveID(typeface, original->typeface) == false)
210 return false; // succeeded
213 void SkDrawPaint::setupPaint(SkPaint* paint) const {
215 paint->setAntiAlias(SkToBool(antiAlias));
217 paint->setColor(color->getColor());
219 paint->setFakeBoldText(SkToBool(fakeBold));
220 if (filterBitmap != -1)
221 paint->setFilterLevel(filterBitmap ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
222 // stroke is legacy; style setting if present overrides stroke
224 paint->setStyle(SkToBool(stroke) ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
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)
232 SkSafeUnref(paint->setMaskFilter(maskFilter->getMaskFilter()));
233 if (pathEffect == NULL)
234 paint->setPathEffect(NULL);
235 else if (pathEffect != (SkDrawPathEffect*) -1)
236 SkSafeUnref(paint->setPathEffect(pathEffect->getPathEffect()));
238 paint->setShader(NULL);
239 else if (shader != (SkDrawShader*) -1)
240 SkSafeUnref(paint->setShader(shader->getShader()));
241 if (strikeThru != -1)
242 paint->setStrikeThruText(SkToBool(strikeThru));
244 paint->setStrokeCap((SkPaint::Cap) strokeCap);
245 if (strokeJoin != -1)
246 paint->setStrokeJoin((SkPaint::Join) strokeJoin);
247 if (SkScalarIsNaN(strokeMiter) == false)
248 paint->setStrokeMiter(strokeMiter);
249 if (SkScalarIsNaN(strokeWidth) == false)
250 paint->setStrokeWidth(strokeWidth);
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)
262 SkSafeUnref(paint->setTypeface(typeface->getTypeface()));
264 paint->setUnderlineText(SkToBool(underline));
266 paint->setXfermodeMode((SkXfermode::Mode) xfermode);