1
/* libs/graphics/animator/SkDrawPaint.cpp
3
** Copyright 2006, The Android Open Source Project
5
** 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
9
** http://www.apache.org/licenses/LICENSE-2.0
11
** 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
15
** limitations under the License.
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"
26
enum SkPaint_Functions {
27
SK_FUNCTION(measureText)
30
enum SkPaint_Properties {
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
37
const SkFunctionParamType SkDrawPaint::fFunctionParameters[] = {
38
(SkFunctionParamType) SkType_String,
39
(SkFunctionParamType) 0 // terminator for parameter list (there may be multiple parameter lists)
43
#if SK_USE_CONDENSED_INFO == 0
45
const 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)
75
DEFINE_GET_MEMBER(SkDrawPaint);
77
SkDrawPaint::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),
80
strokeCap((SkPaint::Cap) -1), strokeJoin((SkPaint::Join) -1), strokeMiter(SK_ScalarNaN),
81
strokeWidth(SK_ScalarNaN), style((SkPaint::Style) -1),
82
textAlign((SkPaint::Align) -1), textScaleX(SK_ScalarNaN), textSize(SK_ScalarNaN),
83
textSkewX(SK_ScalarNaN), typeface((SkDrawTypeface*) -1),
84
underline(-1), xfermode((SkPorterDuff::Mode) -1), fOwnsColor(false), fOwnsMaskFilter(false),
85
fOwnsPathEffect(false), fOwnsShader(false), fOwnsTypeface(false) {
88
SkDrawPaint::~SkDrawPaint() {
101
bool SkDrawPaint::add(SkAnimateMaker& maker, SkDisplayable* child) {
102
SkASSERT(child && child->isPaintPart());
103
SkPaintPart* part = (SkPaintPart*) child;
105
maker.setErrorCode(SkDisplayXMLParserError::kErrorAddingToPaint);
109
SkDisplayable* SkDrawPaint::deepCopy(SkAnimateMaker* maker) {
110
SkDrawColor* tempColor = color;
112
SkDrawPaint* copy = (SkDrawPaint*) INHERITED::deepCopy(maker);
114
tempColor = (SkDrawColor*) color->deepCopy(maker);
115
tempColor->setParent(copy);
117
copy->fOwnsColor = true;
121
bool SkDrawPaint::draw(SkAnimateMaker& maker) {
122
SkPaint* paint = maker.fPaint;
127
#ifdef SK_DUMP_ENABLED
128
void SkDrawPaint::dump(SkAnimateMaker* maker) {
131
bool closedYet = false;
132
SkDisplayList::fIndent +=4;
133
//should i say if (maskFilter && ...?
134
if (maskFilter != (SkDrawMaskFilter*)-1) {
136
maskFilter->dump(maker);
139
if (pathEffect != (SkDrawPathEffect*) -1) {
140
if (closedYet == false) {
144
pathEffect->dump(maker);
147
if (closedYet == false) {
151
typeface->dump(maker);
153
SkDisplayList::fIndent -= 4;
154
dumpChildren(maker, closedYet);
158
void SkDrawPaint::executeFunction(SkDisplayable* target, int index,
159
SkTDArray<SkScriptValue>& parameters, SkDisplayTypes type,
160
SkScriptValue* scriptValue) {
161
if (scriptValue == NULL)
163
SkASSERT(target == this);
165
case SK_FUNCTION(measureText): {
166
SkASSERT(parameters.count() == 1);
167
SkASSERT(type == SkType_Float);
170
scriptValue->fType = SkType_Float;
171
SkASSERT(parameters[0].fType == SkType_String);
172
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(),
175
// scriptValue->fOperand.fScalar / 65536.0f);
182
const SkFunctionParamType* SkDrawPaint::getFunctionsParameters() {
183
return fFunctionParameters;
186
bool SkDrawPaint::getProperty(int index, SkScriptValue* value) const {
187
SkPaint::FontMetrics metrics;
190
paint.getFontMetrics(&metrics);
192
case SK_PROPERTY(ascent):
193
value->fOperand.fScalar = metrics.fAscent;
195
case SK_PROPERTY(descent):
196
value->fOperand.fScalar = metrics.fDescent;
198
// should consider returning fLeading as well (or roll it into ascent/descent somehow
203
value->fType = SkType_Float;
207
bool 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)
212
if (fOwnsMaskFilter && maker.resolveID(maskFilter, original->maskFilter) == false)
214
if (fOwnsPathEffect && maker.resolveID(pathEffect, original->pathEffect) == false)
216
if (fOwnsShader && maker.resolveID(shader, original->shader) == false)
218
if (fOwnsTypeface && maker.resolveID(typeface, original->typeface) == false)
220
return false; // succeeded
223
void SkDrawPaint::setupPaint(SkPaint* paint) const {
225
paint->setAntiAlias(SkToBool(antiAlias));
227
paint->setColor(color->getColor());
229
paint->setFakeBoldText(SkToBool(fakeBold));
230
if (filterBitmap != -1)
231
paint->setFilterBitmap(SkToBool(filterBitmap));
232
// stroke is legacy; style setting if present overrides stroke
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)
242
paint->setMaskFilter(maskFilter->getMaskFilter())->safeUnref();
243
if (pathEffect == NULL)
244
paint->setPathEffect(NULL);
245
else if (pathEffect != (SkDrawPathEffect*) -1)
246
paint->setPathEffect(pathEffect->getPathEffect())->safeUnref();
248
paint->setShader(NULL);
249
else if (shader != (SkDrawShader*) -1)
250
paint->setShader(shader->getShader())->safeUnref();
251
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)
272
paint->setTypeface(typeface->getTypeface())->safeUnref();
274
paint->setUnderlineText(SkToBool(underline));
275
if (xfermode != (SkPorterDuff::Mode) -1)
276
paint->setPorterDuffXfermode((SkPorterDuff::Mode) xfermode);