1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include "BasicShapeFunctions.h"
#include "BasicShapes.h"
#include "CSSBasicShapes.h"
#include "CSSPrimitiveValueMappings.h"
#include "CSSValuePool.h"
#include "Pair.h"
#include "RenderStyle.h"
namespace WebCore {
static PassRefPtr<CSSPrimitiveValue> valueForCenterCoordinate(CSSValuePool& pool, const RenderStyle* style, const BasicShapeCenterCoordinate& center, EBoxOrient orientation)
{
if (center.direction() == BasicShapeCenterCoordinate::TopLeft)
return pool.createValue(center.length(), style);
CSSValueID keyword = orientation == HORIZONTAL ? CSSValueRight : CSSValueBottom;
return pool.createValue(Pair::create(pool.createIdentifierValue(keyword), pool.createValue(center.length(), style)));
}
static PassRefPtr<CSSPrimitiveValue> basicShapeRadiusToCSSValue(const RenderStyle* style, CSSValuePool& pool, const BasicShapeRadius& radius)
{
switch (radius.type()) {
case BasicShapeRadius::Value:
return pool.createValue(radius.value(), style);
case BasicShapeRadius::ClosestSide:
return pool.createIdentifierValue(CSSValueClosestSide);
case BasicShapeRadius::FarthestSide:
return pool.createIdentifierValue(CSSValueFarthestSide);
}
ASSERT_NOT_REACHED();
return 0;
}
PassRefPtr<CSSValue> valueForBasicShape(const RenderStyle* style, const BasicShape* basicShape)
{
CSSValuePool& pool = cssValuePool();
RefPtr<CSSBasicShape> basicShapeValue;
switch (basicShape->type()) {
case BasicShape::BasicShapeRectangleType: {
const BasicShapeRectangle* rectangle = static_cast<const BasicShapeRectangle*>(basicShape);
RefPtr<CSSBasicShapeRectangle> rectangleValue = CSSBasicShapeRectangle::create();
rectangleValue->setX(pool.createValue(rectangle->x(), style));
rectangleValue->setY(pool.createValue(rectangle->y(), style));
rectangleValue->setWidth(pool.createValue(rectangle->width(), style));
rectangleValue->setHeight(pool.createValue(rectangle->height(), style));
rectangleValue->setRadiusX(pool.createValue(rectangle->cornerRadiusX(), style));
rectangleValue->setRadiusY(pool.createValue(rectangle->cornerRadiusY(), style));
basicShapeValue = rectangleValue.release();
break;
}
case BasicShape::DeprecatedBasicShapeCircleType: {
const DeprecatedBasicShapeCircle* circle = static_cast<const DeprecatedBasicShapeCircle*>(basicShape);
RefPtr<CSSDeprecatedBasicShapeCircle> circleValue = CSSDeprecatedBasicShapeCircle::create();
circleValue->setCenterX(pool.createValue(circle->centerX(), style));
circleValue->setCenterY(pool.createValue(circle->centerY(), style));
circleValue->setRadius(pool.createValue(circle->radius(), style));
basicShapeValue = circleValue.release();
break;
}
case BasicShape::BasicShapeCircleType: {
const BasicShapeCircle* circle = static_cast<const BasicShapeCircle*>(basicShape);
RefPtr<CSSBasicShapeCircle> circleValue = CSSBasicShapeCircle::create();
circleValue->setCenterX(valueForCenterCoordinate(pool, style, circle->centerX(), HORIZONTAL));
circleValue->setCenterY(valueForCenterCoordinate(pool, style, circle->centerY(), VERTICAL));
circleValue->setRadius(basicShapeRadiusToCSSValue(style, pool, circle->radius()));
basicShapeValue = circleValue.release();
break;
}
case BasicShape::DeprecatedBasicShapeEllipseType: {
const DeprecatedBasicShapeEllipse* ellipse = static_cast<const DeprecatedBasicShapeEllipse*>(basicShape);
RefPtr<CSSDeprecatedBasicShapeEllipse> ellipseValue = CSSDeprecatedBasicShapeEllipse::create();
ellipseValue->setCenterX(pool.createValue(ellipse->centerX(), style));
ellipseValue->setCenterY(pool.createValue(ellipse->centerY(), style));
ellipseValue->setRadiusX(pool.createValue(ellipse->radiusX(), style));
ellipseValue->setRadiusY(pool.createValue(ellipse->radiusY(), style));
basicShapeValue = ellipseValue.release();
break;
}
case BasicShape::BasicShapeEllipseType: {
const BasicShapeEllipse* ellipse = static_cast<const BasicShapeEllipse*>(basicShape);
RefPtr<CSSBasicShapeEllipse> ellipseValue = CSSBasicShapeEllipse::create();
ellipseValue->setCenterX(valueForCenterCoordinate(pool, style, ellipse->centerX(), HORIZONTAL));
ellipseValue->setCenterY(valueForCenterCoordinate(pool, style, ellipse->centerY(), VERTICAL));
ellipseValue->setRadiusX(basicShapeRadiusToCSSValue(style, pool, ellipse->radiusX()));
ellipseValue->setRadiusY(basicShapeRadiusToCSSValue(style, pool, ellipse->radiusY()));
basicShapeValue = ellipseValue.release();
break;
}
case BasicShape::BasicShapePolygonType: {
const BasicShapePolygon* polygon = static_cast<const BasicShapePolygon*>(basicShape);
RefPtr<CSSBasicShapePolygon> polygonValue = CSSBasicShapePolygon::create();
polygonValue->setWindRule(polygon->windRule());
const Vector<Length>& values = polygon->values();
for (unsigned i = 0; i < values.size(); i += 2)
polygonValue->appendPoint(pool.createValue(values.at(i), style), pool.createValue(values.at(i + 1), style));
basicShapeValue = polygonValue.release();
break;
}
case BasicShape::BasicShapeInsetRectangleType: {
const BasicShapeInsetRectangle* rectangle = static_cast<const BasicShapeInsetRectangle*>(basicShape);
RefPtr<CSSBasicShapeInsetRectangle> rectangleValue = CSSBasicShapeInsetRectangle::create();
rectangleValue->setTop(pool.createValue(rectangle->top(), style));
rectangleValue->setRight(pool.createValue(rectangle->right(), style));
rectangleValue->setBottom(pool.createValue(rectangle->bottom(), style));
rectangleValue->setLeft(pool.createValue(rectangle->left(), style));
rectangleValue->setRadiusX(pool.createValue(rectangle->cornerRadiusX(), style));
rectangleValue->setRadiusY(pool.createValue(rectangle->cornerRadiusY(), style));
basicShapeValue = rectangleValue.release();
break;
}
case BasicShape::BasicShapeInsetType: {
const BasicShapeInset* inset = static_cast<const BasicShapeInset*>(basicShape);
RefPtr<CSSBasicShapeInset> insetValue = CSSBasicShapeInset::create();
insetValue->setTop(pool.createValue(inset->top()));
insetValue->setRight(pool.createValue(inset->right()));
insetValue->setBottom(pool.createValue(inset->bottom()));
insetValue->setLeft(pool.createValue(inset->left()));
insetValue->setTopLeftRadius(pool.createValue(inset->topLeftRadius()));
insetValue->setTopRightRadius(pool.createValue(inset->topRightRadius()));
insetValue->setBottomRightRadius(pool.createValue(inset->bottomRightRadius()));
insetValue->setBottomLeftRadius(pool.createValue(inset->bottomLeftRadius()));
basicShapeValue = insetValue.release();
break;
}
default:
break;
}
if (basicShape->layoutBox() != BoxMissing)
basicShapeValue->setLayoutBox(pool.createValue(basicShape->layoutBox()));
return pool.createValue(basicShapeValue.release());
}
static Length convertToLength(const RenderStyle* style, const RenderStyle* rootStyle, CSSPrimitiveValue* value)
{
return value->convertToLength<FixedIntegerConversion | FixedFloatConversion | PercentConversion | CalculatedConversion | ViewportPercentageConversion>(style, rootStyle, style->effectiveZoom());
}
static BasicShapeCenterCoordinate convertToCenterCoordinate(const RenderStyle* style, const RenderStyle* rootStyle, CSSPrimitiveValue* value)
{
BasicShapeCenterCoordinate::Direction direction;
Length offset = Length(0, Fixed);
CSSValueID keyword = CSSValueTop;
if (!value)
keyword = CSSValueCenter;
else if (value->isValueID())
keyword = value->getValueID();
else if (Pair* pair = value->getPairValue()) {
keyword = pair->first()->getValueID();
offset = convertToLength(style, rootStyle, pair->second());
} else
offset = convertToLength(style, rootStyle, value);
switch (keyword) {
case CSSValueTop:
case CSSValueLeft:
direction = BasicShapeCenterCoordinate::TopLeft;
break;
case CSSValueRight:
case CSSValueBottom:
direction = BasicShapeCenterCoordinate::BottomRight;
break;
case CSSValueCenter:
direction = BasicShapeCenterCoordinate::TopLeft;
offset = Length(50, Percent);
break;
default:
ASSERT_NOT_REACHED();
direction = BasicShapeCenterCoordinate::TopLeft;
break;
}
return BasicShapeCenterCoordinate(direction, offset);
}
static BasicShapeRadius cssValueToBasicShapeRadius(const RenderStyle* style, const RenderStyle* rootStyle, PassRefPtr<CSSPrimitiveValue> radius)
{
if (!radius)
return BasicShapeRadius(BasicShapeRadius::ClosestSide);
if (radius->isValueID()) {
switch (radius->getValueID()) {
case CSSValueClosestSide:
return BasicShapeRadius(BasicShapeRadius::ClosestSide);
case CSSValueFarthestSide:
return BasicShapeRadius(BasicShapeRadius::FarthestSide);
default:
ASSERT_NOT_REACHED();
break;
}
}
return BasicShapeRadius(convertToLength(style, rootStyle, radius.get()));
}
PassRefPtr<BasicShape> basicShapeForValue(const RenderStyle* style, const RenderStyle* rootStyle, const CSSBasicShape* basicShapeValue)
{
RefPtr<BasicShape> basicShape;
switch (basicShapeValue->type()) {
case CSSBasicShape::CSSBasicShapeRectangleType: {
const CSSBasicShapeRectangle* rectValue = static_cast<const CSSBasicShapeRectangle *>(basicShapeValue);
RefPtr<BasicShapeRectangle> rect = BasicShapeRectangle::create();
rect->setX(convertToLength(style, rootStyle, rectValue->x()));
rect->setY(convertToLength(style, rootStyle, rectValue->y()));
rect->setWidth(convertToLength(style, rootStyle, rectValue->width()));
rect->setHeight(convertToLength(style, rootStyle, rectValue->height()));
if (rectValue->radiusX()) {
Length radiusX = convertToLength(style, rootStyle, rectValue->radiusX());
rect->setCornerRadiusX(radiusX);
if (rectValue->radiusY())
rect->setCornerRadiusY(convertToLength(style, rootStyle, rectValue->radiusY()));
else
rect->setCornerRadiusY(radiusX);
} else {
rect->setCornerRadiusX(Length(0, Fixed));
rect->setCornerRadiusY(Length(0, Fixed));
}
basicShape = rect.release();
break;
}
case CSSBasicShape::CSSDeprecatedBasicShapeCircleType: {
const CSSDeprecatedBasicShapeCircle* circleValue = static_cast<const CSSDeprecatedBasicShapeCircle *>(basicShapeValue);
RefPtr<DeprecatedBasicShapeCircle> circle = DeprecatedBasicShapeCircle::create();
circle->setCenterX(convertToLength(style, rootStyle, circleValue->centerX()));
circle->setCenterY(convertToLength(style, rootStyle, circleValue->centerY()));
circle->setRadius(convertToLength(style, rootStyle, circleValue->radius()));
basicShape = circle.release();
break;
}
case CSSBasicShape::CSSBasicShapeCircleType: {
const CSSBasicShapeCircle* circleValue = static_cast<const CSSBasicShapeCircle *>(basicShapeValue);
RefPtr<BasicShapeCircle> circle = BasicShapeCircle::create();
circle->setCenterX(convertToCenterCoordinate(style, rootStyle, circleValue->centerX()));
circle->setCenterY(convertToCenterCoordinate(style, rootStyle, circleValue->centerY()));
circle->setRadius(cssValueToBasicShapeRadius(style, rootStyle, circleValue->radius()));
basicShape = circle.release();
break;
}
case CSSBasicShape::CSSDeprecatedBasicShapeEllipseType: {
const CSSDeprecatedBasicShapeEllipse* ellipseValue = static_cast<const CSSDeprecatedBasicShapeEllipse *>(basicShapeValue);
RefPtr<DeprecatedBasicShapeEllipse> ellipse = DeprecatedBasicShapeEllipse::create();
ellipse->setCenterX(convertToLength(style, rootStyle, ellipseValue->centerX()));
ellipse->setCenterY(convertToLength(style, rootStyle, ellipseValue->centerY()));
ellipse->setRadiusX(convertToLength(style, rootStyle, ellipseValue->radiusX()));
ellipse->setRadiusY(convertToLength(style, rootStyle, ellipseValue->radiusY()));
basicShape = ellipse.release();
break;
}
case CSSBasicShape::CSSBasicShapeEllipseType: {
const CSSBasicShapeEllipse* ellipseValue = static_cast<const CSSBasicShapeEllipse *>(basicShapeValue);
RefPtr<BasicShapeEllipse> ellipse = BasicShapeEllipse::create();
ellipse->setCenterX(convertToCenterCoordinate(style, rootStyle, ellipseValue->centerX()));
ellipse->setCenterY(convertToCenterCoordinate(style, rootStyle, ellipseValue->centerY()));
ellipse->setRadiusX(cssValueToBasicShapeRadius(style, rootStyle, ellipseValue->radiusX()));
ellipse->setRadiusY(cssValueToBasicShapeRadius(style, rootStyle, ellipseValue->radiusY()));
basicShape = ellipse.release();
break;
}
case CSSBasicShape::CSSBasicShapePolygonType: {
const CSSBasicShapePolygon* polygonValue = static_cast<const CSSBasicShapePolygon *>(basicShapeValue);
RefPtr<BasicShapePolygon> polygon = BasicShapePolygon::create();
polygon->setWindRule(polygonValue->windRule());
const Vector<RefPtr<CSSPrimitiveValue>>& values = polygonValue->values();
for (unsigned i = 0; i < values.size(); i += 2)
polygon->appendPoint(convertToLength(style, rootStyle, values.at(i).get()), convertToLength(style, rootStyle, values.at(i + 1).get()));
basicShape = polygon.release();
break;
}
case CSSBasicShape::CSSBasicShapeInsetRectangleType: {
const CSSBasicShapeInsetRectangle* rectValue = static_cast<const CSSBasicShapeInsetRectangle *>(basicShapeValue);
RefPtr<BasicShapeInsetRectangle> rect = BasicShapeInsetRectangle::create();
rect->setTop(convertToLength(style, rootStyle, rectValue->top()));
rect->setRight(convertToLength(style, rootStyle, rectValue->right()));
rect->setBottom(convertToLength(style, rootStyle, rectValue->bottom()));
rect->setLeft(convertToLength(style, rootStyle, rectValue->left()));
if (rectValue->radiusX()) {
Length radiusX = convertToLength(style, rootStyle, rectValue->radiusX());
rect->setCornerRadiusX(radiusX);
if (rectValue->radiusY())
rect->setCornerRadiusY(convertToLength(style, rootStyle, rectValue->radiusY()));
else
rect->setCornerRadiusY(radiusX);
} else {
rect->setCornerRadiusX(Length(0, Fixed));
rect->setCornerRadiusY(Length(0, Fixed));
}
basicShape = rect.release();
break;
}
case CSSBasicShape::CSSBasicShapeInsetType: {
const CSSBasicShapeInset* rectValue = static_cast<const CSSBasicShapeInset* >(basicShapeValue);
RefPtr<BasicShapeInset> rect = BasicShapeInset::create();
if (rectValue->top())
rect->setTop(convertToLength(style, rootStyle, rectValue->top()));
else {
rect->setTop(Length(0, Fixed));
return rect;
}
if (rectValue->right())
rect->setRight(convertToLength(style, rootStyle, rectValue->right()));
else
rect->setRight(Length(0, Fixed));
if (rectValue->bottom())
rect->setBottom(convertToLength(style, rootStyle, rectValue->bottom()));
else
rect->setBottom(Length(0, Fixed));
if (rectValue->left())
rect->setLeft(convertToLength(style, rootStyle, rectValue->left()));
else
rect->setLeft(Length(0, Fixed));
if (rectValue->topLeftRadius()) {
Pair* topLeftRadius = rectValue->topLeftRadius()->getPairValue();
rect->setTopLeftRadius(LengthSize(convertToLength(style, rootStyle, topLeftRadius->first()), convertToLength(style, rootStyle, topLeftRadius->second())));
} else
rect->setTopLeftRadius(LengthSize(Length(0, Fixed), Length(0, Fixed)));
if (rectValue->topRightRadius()) {
Pair* topRightRadius = rectValue->topRightRadius()->getPairValue();
rect->setTopRightRadius(LengthSize(convertToLength(style, rootStyle, topRightRadius->first()), convertToLength(style, rootStyle, topRightRadius->second())));
} else
rect->setTopRightRadius(LengthSize(Length(0, Fixed), Length(0, Fixed)));
if (rectValue->bottomRightRadius()) {
Pair* bottomRightRadius = rectValue->bottomRightRadius()->getPairValue();
rect->setBottomRightRadius(LengthSize(convertToLength(style, rootStyle, bottomRightRadius->first()), convertToLength(style, rootStyle, bottomRightRadius->second())));
} else
rect->setBottomRightRadius(LengthSize(Length(0, Fixed), Length(0, Fixed)));
if (rectValue->topLeftRadius()) {
Pair* bottomLeftRadius = rectValue->bottomLeftRadius()->getPairValue();
rect->setBottomLeftRadius(LengthSize(convertToLength(style, rootStyle, bottomLeftRadius->first()), convertToLength(style, rootStyle, bottomLeftRadius->second())));
} else
rect->setBottomLeftRadius(LengthSize(Length(0, Fixed), Length(0, Fixed)));
basicShape = rect.release();
break;
}
default:
break;
}
if (basicShapeValue->layoutBox())
basicShape->setLayoutBox(LayoutBox(*basicShapeValue->layoutBox()));
return basicShape.release();
}
float floatValueForCenterCoordinate(const BasicShapeCenterCoordinate& center, float boxDimension)
{
float offset = floatValueForLength(center.length(), boxDimension);
if (center.direction() == BasicShapeCenterCoordinate::TopLeft)
return offset;
return boxDimension - offset;
}
}
|