blob: cd2c915e236efc115b992e1e037b40ff71dcf049 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/animator/SkDrawPaint.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
reed@google.com82065d62011-02-07 15:30:46 +00005** 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.com8a1c16f2008-12-17 15:59:43 +00008**
reed@google.com82065d62011-02-07 15:30:46 +00009** http://www.apache.org/licenses/LICENSE-2.0
reed@android.com8a1c16f2008-12-17 15:59:43 +000010**
reed@google.com82065d62011-02-07 15:30:46 +000011** 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.com8a1c16f2008-12-17 15:59:43 +000015** limitations under the License.
16*/
17
18#include "SkDrawPaint.h"
19#include "SkAnimateMaker.h"
20#include "SkDrawColor.h"
21#include "SkDrawShader.h"
22#include "SkMaskFilter.h"
23#include "SkPaintParts.h"
24#include "SkPathEffect.h"
25
26enum SkPaint_Functions {
27 SK_FUNCTION(measureText)
28};
29
30enum SkPaint_Properties {
31 SK_PROPERTY(ascent),
32 SK_PROPERTY(descent)
33};
34
35// !!! in the future, this could be compiled by build-condensed-info into an array of parameters
36// with a lookup table to find the first parameter -- for now, it is iteratively searched through
37const SkFunctionParamType SkDrawPaint::fFunctionParameters[] = {
38 (SkFunctionParamType) SkType_String,
39 (SkFunctionParamType) 0 // terminator for parameter list (there may be multiple parameter lists)
40};
41
42
43#if SK_USE_CONDENSED_INFO == 0
44
45const SkMemberInfo SkDrawPaint::fInfo[] = {
46 SK_MEMBER(antiAlias, Boolean),
47 SK_MEMBER_PROPERTY(ascent, Float),
48 SK_MEMBER(color, Color),
49 SK_MEMBER_PROPERTY(descent, Float),
50 SK_MEMBER(fakeBold, Boolean),
51 SK_MEMBER(filterBitmap, Boolean),
52 SK_MEMBER(linearText, Boolean),
53 SK_MEMBER(maskFilter, MaskFilter),
54 SK_MEMBER_FUNCTION(measureText, Float),
55 SK_MEMBER(pathEffect, PathEffect),
56 SK_MEMBER(shader, Shader),
57 SK_MEMBER(strikeThru, Boolean),
58 SK_MEMBER(stroke, Boolean),
59 SK_MEMBER(strokeCap, Cap),
60 SK_MEMBER(strokeJoin, Join),
61 SK_MEMBER(strokeMiter, Float),
62 SK_MEMBER(strokeWidth, Float),
63 SK_MEMBER(style, Style),
64 SK_MEMBER(textAlign, Align),
65 SK_MEMBER(textScaleX, Float),
66 SK_MEMBER(textSize, Float),
67 SK_MEMBER(textSkewX, Float),
68 SK_MEMBER(typeface, Typeface),
69 SK_MEMBER(underline, Boolean),
70 SK_MEMBER(xfermode, Xfermode)
71};
72
73#endif
74
75DEFINE_GET_MEMBER(SkDrawPaint);
76
77SkDrawPaint::SkDrawPaint() : antiAlias(-1), color(NULL), fakeBold(-1), filterBitmap(-1),
78 linearText(-1), maskFilter((SkDrawMaskFilter*) -1), pathEffect((SkDrawPathEffect*) -1),
79 shader((SkDrawShader*) -1), strikeThru(-1), stroke(-1),
reed@google.com82065d62011-02-07 15:30:46 +000080 strokeCap((SkPaint::Cap) -1), strokeJoin((SkPaint::Join) -1), strokeMiter(SK_ScalarNaN),
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 strokeWidth(SK_ScalarNaN), style((SkPaint::Style) -1),
reed@google.com82065d62011-02-07 15:30:46 +000082 textAlign((SkPaint::Align) -1), textScaleX(SK_ScalarNaN), textSize(SK_ScalarNaN),
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 textSkewX(SK_ScalarNaN), typeface((SkDrawTypeface*) -1),
reed@google.com82065d62011-02-07 15:30:46 +000084 underline(-1), xfermode((SkXfermode::Mode) -1), fOwnsColor(false), fOwnsMaskFilter(false),
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 fOwnsPathEffect(false), fOwnsShader(false), fOwnsTypeface(false) {
86}
87
88SkDrawPaint::~SkDrawPaint() {
89 if (fOwnsColor)
90 delete color;
91 if (fOwnsMaskFilter)
92 delete maskFilter;
93 if (fOwnsPathEffect)
94 delete pathEffect;
95 if (fOwnsShader)
96 delete shader;
97 if (fOwnsTypeface)
98 delete typeface;
99}
100
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +0000101bool SkDrawPaint::add(SkAnimateMaker* maker, SkDisplayable* child) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 SkASSERT(child && child->isPaintPart());
103 SkPaintPart* part = (SkPaintPart*) child;
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +0000104 if (part->add() && maker)
105 maker->setErrorCode(SkDisplayXMLParserError::kErrorAddingToPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 return true;
107}
108
109SkDisplayable* SkDrawPaint::deepCopy(SkAnimateMaker* maker) {
110 SkDrawColor* tempColor = color;
111 color = NULL;
112 SkDrawPaint* copy = (SkDrawPaint*) INHERITED::deepCopy(maker);
113 color = tempColor;
114 tempColor = (SkDrawColor*) color->deepCopy(maker);
115 tempColor->setParent(copy);
116 tempColor->add();
117 copy->fOwnsColor = true;
118 return copy;
119}
120
121bool SkDrawPaint::draw(SkAnimateMaker& maker) {
122 SkPaint* paint = maker.fPaint;
123 setupPaint(paint);
124 return false;
125}
126
127#ifdef SK_DUMP_ENABLED
128void SkDrawPaint::dump(SkAnimateMaker* maker) {
129 dumpBase(maker);
130 dumpAttrs(maker);
131 bool closedYet = false;
132 SkDisplayList::fIndent +=4;
133 //should i say if (maskFilter && ...?
134 if (maskFilter != (SkDrawMaskFilter*)-1) {
135 SkDebugf(">\n");
136 maskFilter->dump(maker);
137 closedYet = true;
138 }
139 if (pathEffect != (SkDrawPathEffect*) -1) {
140 if (closedYet == false) {
141 SkDebugf(">\n");
142 closedYet = true;
143 }
144 pathEffect->dump(maker);
145 }
146 if (fOwnsTypeface) {
147 if (closedYet == false) {
148 SkDebugf(">\n");
149 closedYet = true;
150 }
151 typeface->dump(maker);
152 }
153 SkDisplayList::fIndent -= 4;
154 dumpChildren(maker, closedYet);
155}
156#endif
reed@google.com82065d62011-02-07 15:30:46 +0000157
158void SkDrawPaint::executeFunction(SkDisplayable* target, int index,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 SkTDArray<SkScriptValue>& parameters, SkDisplayTypes type,
160 SkScriptValue* scriptValue) {
161 if (scriptValue == NULL)
162 return;
163 SkASSERT(target == this);
164 switch (index) {
165 case SK_FUNCTION(measureText): {
166 SkASSERT(parameters.count() == 1);
167 SkASSERT(type == SkType_Float);
168 SkPaint paint;
169 setupPaint(&paint);
170 scriptValue->fType = SkType_Float;
171 SkASSERT(parameters[0].fType == SkType_String);
reed@google.com82065d62011-02-07 15:30:46 +0000172 scriptValue->fOperand.fScalar = paint.measureText(parameters[0].fOperand.fString->c_str(),
173 parameters[0].fOperand.fString->size());
174// SkDebugf("measureText: %s = %g\n", parameters[0].fOperand.fString->c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175// scriptValue->fOperand.fScalar / 65536.0f);
176 } break;
177 default:
178 SkASSERT(0);
179 }
180}
181
182const SkFunctionParamType* SkDrawPaint::getFunctionsParameters() {
183 return fFunctionParameters;
184}
185
186bool SkDrawPaint::getProperty(int index, SkScriptValue* value) const {
187 SkPaint::FontMetrics metrics;
188 SkPaint paint;
189 setupPaint(&paint);
190 paint.getFontMetrics(&metrics);
191 switch (index) {
192 case SK_PROPERTY(ascent):
193 value->fOperand.fScalar = metrics.fAscent;
194 break;
195 case SK_PROPERTY(descent):
196 value->fOperand.fScalar = metrics.fDescent;
197 break;
reed@google.com82065d62011-02-07 15:30:46 +0000198 // should consider returning fLeading as well (or roll it into ascent/descent somehow
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 default:
200 SkASSERT(0);
201 return false;
202 }
203 value->fType = SkType_Float;
204 return true;
205}
206
207bool SkDrawPaint::resolveIDs(SkAnimateMaker& maker, SkDisplayable* origDisp, SkApply* ) {
208 SkASSERT(origDisp->isPaint());
209 SkDrawPaint* original = (SkDrawPaint*) origDisp;
210 if (fOwnsColor && maker.resolveID(color, original->color) == false)
211 return true;
212 if (fOwnsMaskFilter && maker.resolveID(maskFilter, original->maskFilter) == false)
213 return true;
214 if (fOwnsPathEffect && maker.resolveID(pathEffect, original->pathEffect) == false)
215 return true;
216 if (fOwnsShader && maker.resolveID(shader, original->shader) == false)
217 return true;
218 if (fOwnsTypeface && maker.resolveID(typeface, original->typeface) == false)
219 return true;
reed@google.com82065d62011-02-07 15:30:46 +0000220 return false; // succeeded
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221}
222
223void SkDrawPaint::setupPaint(SkPaint* paint) const {
224 if (antiAlias != -1)
225 paint->setAntiAlias(SkToBool(antiAlias));
226 if (color != NULL)
227 paint->setColor(color->getColor());
228 if (fakeBold != -1)
229 paint->setFakeBoldText(SkToBool(fakeBold));
230 if (filterBitmap != -1)
231 paint->setFilterBitmap(SkToBool(filterBitmap));
232 // stroke is legacy; style setting if present overrides stroke
233 if (stroke != -1)
234 paint->setStyle(SkToBool(stroke) ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
235 if (style != (SkPaint::Style) -1)
236 paint->setStyle((SkPaint::Style) style);
237 if (linearText != -1)
238 paint->setLinearText(SkToBool(linearText));
239 if (maskFilter == NULL)
240 paint->setMaskFilter(NULL);
241 else if (maskFilter != (SkDrawMaskFilter*) -1)
reed@google.com82065d62011-02-07 15:30:46 +0000242 SkSafeUnref(paint->setMaskFilter(maskFilter->getMaskFilter()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 if (pathEffect == NULL)
244 paint->setPathEffect(NULL);
245 else if (pathEffect != (SkDrawPathEffect*) -1)
reed@google.com82065d62011-02-07 15:30:46 +0000246 SkSafeUnref(paint->setPathEffect(pathEffect->getPathEffect()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 if (shader == NULL)
248 paint->setShader(NULL);
249 else if (shader != (SkDrawShader*) -1)
reed@google.com82065d62011-02-07 15:30:46 +0000250 SkSafeUnref(paint->setShader(shader->getShader()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 if (strikeThru != -1)
252 paint->setStrikeThruText(SkToBool(strikeThru));
253 if (strokeCap != (SkPaint::Cap) -1)
254 paint->setStrokeCap((SkPaint::Cap) strokeCap);
255 if (strokeJoin != (SkPaint::Join) -1)
256 paint->setStrokeJoin((SkPaint::Join) strokeJoin);
257 if (SkScalarIsNaN(strokeMiter) == false)
258 paint->setStrokeMiter(strokeMiter);
259 if (SkScalarIsNaN(strokeWidth) == false)
260 paint->setStrokeWidth(strokeWidth);
261 if (textAlign != (SkPaint::Align) -1)
262 paint->setTextAlign((SkPaint::Align) textAlign);
263 if (SkScalarIsNaN(textScaleX) == false)
264 paint->setTextScaleX(textScaleX);
265 if (SkScalarIsNaN(textSize) == false)
266 paint->setTextSize(textSize);
267 if (SkScalarIsNaN(textSkewX) == false)
268 paint->setTextSkewX(textSkewX);
269 if (typeface == NULL)
270 paint->setTypeface(NULL);
271 else if (typeface != (SkDrawTypeface*) -1)
reed@google.com82065d62011-02-07 15:30:46 +0000272 SkSafeUnref(paint->setTypeface(typeface->getTypeface()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 if (underline != -1)
274 paint->setUnderlineText(SkToBool(underline));
reed@android.com845fdac2009-06-23 03:01:32 +0000275 if (xfermode != (SkXfermode::Mode) -1)
reed@android.com0f0cfae2009-10-13 13:33:16 +0000276 paint->setXfermodeMode((SkXfermode::Mode) xfermode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277}