blob: c882427585f9bbca791d645535d6f1e8b8a17bc7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00008#include "SkDrawPaint.h"
9#include "SkAnimateMaker.h"
10#include "SkDrawColor.h"
11#include "SkDrawShader.h"
12#include "SkMaskFilter.h"
reed@google.com8d4dc712014-04-18 15:14:25 +000013#include "SkPaintPart.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPathEffect.h"
15
16enum SkPaint_Functions {
17 SK_FUNCTION(measureText)
18};
19
20enum 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
27const 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
35const 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
65DEFINE_GET_MEMBER(SkDrawPaint);
66
67SkDrawPaint::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.com82065d62011-02-07 15:30:46 +000070 strokeCap((SkPaint::Cap) -1), strokeJoin((SkPaint::Join) -1), strokeMiter(SK_ScalarNaN),
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 strokeWidth(SK_ScalarNaN), style((SkPaint::Style) -1),
reed@google.com82065d62011-02-07 15:30:46 +000072 textAlign((SkPaint::Align) -1), textScaleX(SK_ScalarNaN), textSize(SK_ScalarNaN),
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 textSkewX(SK_ScalarNaN), typeface((SkDrawTypeface*) -1),
reed@google.com82065d62011-02-07 15:30:46 +000074 underline(-1), xfermode((SkXfermode::Mode) -1), fOwnsColor(false), fOwnsMaskFilter(false),
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 fOwnsPathEffect(false), fOwnsShader(false), fOwnsTypeface(false) {
76}
77
78SkDrawPaint::~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.comd6caf2e2011-07-08 14:41:12 +000091bool SkDrawPaint::add(SkAnimateMaker* maker, SkDisplayable* child) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 SkASSERT(child && child->isPaintPart());
93 SkPaintPart* part = (SkPaintPart*) child;
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +000094 if (part->add() && maker)
95 maker->setErrorCode(SkDisplayXMLParserError::kErrorAddingToPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 return true;
97}
98
99SkDisplayable* 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
111bool SkDrawPaint::draw(SkAnimateMaker& maker) {
112 SkPaint* paint = maker.fPaint;
113 setupPaint(paint);
114 return false;
115}
116
117#ifdef SK_DUMP_ENABLED
118void 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.com82065d62011-02-07 15:30:46 +0000147
148void SkDrawPaint::executeFunction(SkDisplayable* target, int index,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 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.com82065d62011-02-07 15:30:46 +0000162 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.com8a1c16f2008-12-17 15:59:43 +0000165// scriptValue->fOperand.fScalar / 65536.0f);
166 } break;
167 default:
168 SkASSERT(0);
169 }
170}
171
172const SkFunctionParamType* SkDrawPaint::getFunctionsParameters() {
173 return fFunctionParameters;
174}
175
176bool 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.com82065d62011-02-07 15:30:46 +0000188 // should consider returning fLeading as well (or roll it into ascent/descent somehow
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 default:
190 SkASSERT(0);
191 return false;
192 }
193 value->fType = SkType_Float;
194 return true;
195}
196
197bool 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.com82065d62011-02-07 15:30:46 +0000210 return false; // succeeded
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211}
212
213void 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.com44699382013-10-31 17:28:30 +0000221 paint->setFilterLevel(filterBitmap ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 // 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.orge80c2bc2013-03-29 16:53:30 +0000225 if (style != -1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 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.com82065d62011-02-07 15:30:46 +0000232 SkSafeUnref(paint->setMaskFilter(maskFilter->getMaskFilter()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 if (pathEffect == NULL)
234 paint->setPathEffect(NULL);
235 else if (pathEffect != (SkDrawPathEffect*) -1)
reed@google.com82065d62011-02-07 15:30:46 +0000236 SkSafeUnref(paint->setPathEffect(pathEffect->getPathEffect()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 if (shader == NULL)
238 paint->setShader(NULL);
239 else if (shader != (SkDrawShader*) -1)
reed@google.com82065d62011-02-07 15:30:46 +0000240 SkSafeUnref(paint->setShader(shader->getShader()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 if (strikeThru != -1)
242 paint->setStrikeThruText(SkToBool(strikeThru));
tfarina@chromium.orge80c2bc2013-03-29 16:53:30 +0000243 if (strokeCap != -1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 paint->setStrokeCap((SkPaint::Cap) strokeCap);
tfarina@chromium.orge80c2bc2013-03-29 16:53:30 +0000245 if (strokeJoin != -1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 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.orge80c2bc2013-03-29 16:53:30 +0000251 if (textAlign != -1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 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.com82065d62011-02-07 15:30:46 +0000262 SkSafeUnref(paint->setTypeface(typeface->getTypeface()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 if (underline != -1)
264 paint->setUnderlineText(SkToBool(underline));
tfarina@chromium.orge80c2bc2013-03-29 16:53:30 +0000265 if (xfermode != -1)
reed@android.com0f0cfae2009-10-13 13:33:16 +0000266 paint->setXfermodeMode((SkXfermode::Mode) xfermode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267}