2 * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "core/css/CSSBasicShapes.h"
33 #include "core/css/CSSValuePool.h"
34 #include "core/css/Pair.h"
35 #include "platform/Length.h"
36 #include "wtf/text/StringBuilder.h"
42 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CSSBasicShape)
44 static String buildCircleString(const String& radius, const String& centerX, const String& centerY, const String& box)
47 char separator[] = " ";
49 result.appendLiteral("circle(");
51 result.append(radius);
53 if (!centerX.isNull() || !centerY.isNull()) {
55 result.appendLiteral(separator);
57 result.appendLiteral(separator);
58 result.append(centerX);
59 result.appendLiteral(separator);
60 result.append(centerY);
64 result.appendLiteral(separator);
67 return result.toString();
70 static String serializePositionOffset(const Pair& offset, const Pair& other)
72 if ((offset.first()->getValueID() == CSSValueLeft && other.first()->getValueID() == CSSValueTop)
73 || (offset.first()->getValueID() == CSSValueTop && other.first()->getValueID() == CSSValueLeft))
74 return offset.second()->cssText();
75 return offset.cssText();
78 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> buildSerializablePositionOffset(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> offset, CSSValueID defaultSide)
80 CSSValueID side = defaultSide;
81 RefPtrWillBeRawPtr<CSSPrimitiveValue> amount = nullptr;
84 side = CSSValueCenter;
85 } else if (offset->isValueID()) {
86 side = offset->getValueID();
87 } else if (Pair* pair = offset->getPairValue()) {
88 side = pair->first()->getValueID();
89 amount = pair->second();
94 if (side == CSSValueCenter) {
96 amount = cssValuePool().createValue(Length(50, Percent));
97 } else if ((side == CSSValueRight || side == CSSValueBottom)
98 && amount->isPercentage()) {
100 amount = cssValuePool().createValue(Length(100 - amount->getFloatValue(), Percent));
101 } else if (amount->isLength() && !amount->getFloatValue()) {
102 if (side == CSSValueRight || side == CSSValueBottom)
103 amount = cssValuePool().createValue(Length(100, Percent));
105 amount = cssValuePool().createValue(Length(0, Percent));
109 return cssValuePool().createValue(Pair::create(cssValuePool().createValue(side), amount.release(), Pair::KeepIdenticalValues));
112 String CSSBasicShapeCircle::cssText() const
114 RefPtrWillBeRawPtr<CSSPrimitiveValue> normalizedCX = buildSerializablePositionOffset(m_centerX, CSSValueLeft);
115 RefPtrWillBeRawPtr<CSSPrimitiveValue> normalizedCY = buildSerializablePositionOffset(m_centerY, CSSValueTop);
117 return buildCircleString(m_radius ? m_radius->cssText() : String(),
118 serializePositionOffset(*normalizedCX->getPairValue(), *normalizedCY->getPairValue()),
119 serializePositionOffset(*normalizedCY->getPairValue(), *normalizedCX->getPairValue()),
120 m_referenceBox ? m_referenceBox->cssText() : String());
123 bool CSSBasicShapeCircle::equals(const CSSBasicShape& shape) const
125 if (shape.type() != CSSBasicShapeCircleType)
128 const CSSBasicShapeCircle& other = static_cast<const CSSBasicShapeCircle&>(shape);
129 return compareCSSValuePtr(m_centerX, other.m_centerX)
130 && compareCSSValuePtr(m_centerY, other.m_centerY)
131 && compareCSSValuePtr(m_radius, other.m_radius)
132 && compareCSSValuePtr(m_referenceBox, other.m_referenceBox);
135 void CSSBasicShapeCircle::trace(Visitor* visitor)
137 visitor->trace(m_centerX);
138 visitor->trace(m_centerY);
139 visitor->trace(m_radius);
140 CSSBasicShape::trace(visitor);
143 static String buildEllipseString(const String& radiusX, const String& radiusY, const String& centerX, const String& centerY, const String& box)
146 char separator[] = " ";
147 StringBuilder result;
148 result.appendLiteral("ellipse(");
149 bool needsSeparator = false;
150 if (!radiusX.isNull()) {
151 result.append(radiusX);
152 needsSeparator = true;
154 if (!radiusY.isNull()) {
156 result.appendLiteral(separator);
157 result.append(radiusY);
158 needsSeparator = true;
161 if (!centerX.isNull() || !centerY.isNull()) {
163 result.appendLiteral(separator);
164 result.appendLiteral(at);
165 result.appendLiteral(separator);
166 result.append(centerX);
167 result.appendLiteral(separator);
168 result.append(centerY);
172 result.appendLiteral(separator);
175 return result.toString();
178 String CSSBasicShapeEllipse::cssText() const
180 RefPtrWillBeRawPtr<CSSPrimitiveValue> normalizedCX = buildSerializablePositionOffset(m_centerX, CSSValueLeft);
181 RefPtrWillBeRawPtr<CSSPrimitiveValue> normalizedCY = buildSerializablePositionOffset(m_centerY, CSSValueTop);
183 return buildEllipseString(m_radiusX ? m_radiusX->cssText() : String(),
184 m_radiusY ? m_radiusY->cssText() : String(),
185 serializePositionOffset(*normalizedCX->getPairValue(), *normalizedCY->getPairValue()),
186 serializePositionOffset(*normalizedCY->getPairValue(), *normalizedCX->getPairValue()),
187 m_referenceBox ? m_referenceBox->cssText() : String());
190 bool CSSBasicShapeEllipse::equals(const CSSBasicShape& shape) const
192 if (shape.type() != CSSBasicShapeEllipseType)
195 const CSSBasicShapeEllipse& other = static_cast<const CSSBasicShapeEllipse&>(shape);
196 return compareCSSValuePtr(m_centerX, other.m_centerX)
197 && compareCSSValuePtr(m_centerY, other.m_centerY)
198 && compareCSSValuePtr(m_radiusX, other.m_radiusX)
199 && compareCSSValuePtr(m_radiusY, other.m_radiusY)
200 && compareCSSValuePtr(m_referenceBox, other.m_referenceBox);
203 void CSSBasicShapeEllipse::trace(Visitor* visitor)
205 visitor->trace(m_centerX);
206 visitor->trace(m_centerY);
207 visitor->trace(m_radiusX);
208 visitor->trace(m_radiusY);
209 CSSBasicShape::trace(visitor);
212 static String buildPolygonString(const WindRule& windRule, const Vector<String>& points, const String& box)
214 ASSERT(!(points.size() % 2));
216 StringBuilder result;
217 const char evenOddOpening[] = "polygon(evenodd, ";
218 const char nonZeroOpening[] = "polygon(";
219 const char commaSeparator[] = ", ";
220 COMPILE_ASSERT(sizeof(evenOddOpening) > sizeof(nonZeroOpening), polygon_string_openings_have_same_length);
222 // Compute the required capacity in advance to reduce allocations.
223 size_t length = sizeof(evenOddOpening) - 1;
224 for (size_t i = 0; i < points.size(); i += 2) {
226 length += (sizeof(commaSeparator) - 1);
227 // add length of two strings, plus one for the space separator.
228 length += points[i].length() + 1 + points[i + 1].length();
231 length += box.length() + 1;
232 result.reserveCapacity(length);
234 if (windRule == RULE_EVENODD)
235 result.appendLiteral(evenOddOpening);
237 result.appendLiteral(nonZeroOpening);
239 for (size_t i = 0; i < points.size(); i += 2) {
241 result.appendLiteral(commaSeparator);
242 result.append(points[i]);
244 result.append(points[i + 1]);
249 if (!box.isEmpty()) {
254 return result.toString();
257 String CSSBasicShapePolygon::cssText() const
259 Vector<String> points;
260 points.reserveInitialCapacity(m_values.size());
262 for (size_t i = 0; i < m_values.size(); ++i)
263 points.append(m_values.at(i)->cssText());
265 return buildPolygonString(m_windRule, points, m_referenceBox ? m_referenceBox->cssText() : String());
268 bool CSSBasicShapePolygon::equals(const CSSBasicShape& shape) const
270 if (shape.type() != CSSBasicShapePolygonType)
273 const CSSBasicShapePolygon& rhs = static_cast<const CSSBasicShapePolygon&>(shape);
275 if (!compareCSSValuePtr(m_referenceBox, rhs.m_referenceBox))
278 return compareCSSValueVector(m_values, rhs.m_values);
281 void CSSBasicShapePolygon::trace(Visitor* visitor)
283 visitor->trace(m_values);
284 CSSBasicShape::trace(visitor);
287 static bool buildInsetRadii(Vector<String> &radii, const String& topLeftRadius, const String& topRightRadius, const String& bottomRightRadius, const String& bottomLeftRadius)
289 bool showBottomLeft = topRightRadius != bottomLeftRadius;
290 bool showBottomRight = showBottomLeft || (bottomRightRadius != topLeftRadius);
291 bool showTopRight = showBottomRight || (topRightRadius != topLeftRadius);
293 radii.append(topLeftRadius);
295 radii.append(topRightRadius);
297 radii.append(bottomRightRadius);
299 radii.append(bottomLeftRadius);
301 return radii.size() == 1 && radii[0] == "0px";
304 static String buildInsetString(const String& top, const String& right, const String& bottom, const String& left,
305 const String& topLeftRadiusWidth, const String& topLeftRadiusHeight,
306 const String& topRightRadiusWidth, const String& topRightRadiusHeight,
307 const String& bottomRightRadiusWidth, const String& bottomRightRadiusHeight,
308 const String& bottomLeftRadiusWidth, const String& bottomLeftRadiusHeight)
310 char opening[] = "inset(";
311 char separator[] = " ";
312 char cornersSeparator[] = "round";
313 StringBuilder result;
314 result.appendLiteral(opening);
316 bool showLeftArg = !left.isNull() && left != right;
317 bool showBottomArg = !bottom.isNull() && (bottom != top || showLeftArg);
318 bool showRightArg = !right.isNull() && (right != top || showBottomArg);
320 result.appendLiteral(separator);
321 result.append(right);
324 result.appendLiteral(separator);
325 result.append(bottom);
328 result.appendLiteral(separator);
332 if (!topLeftRadiusWidth.isNull() && !topLeftRadiusHeight.isNull()) {
333 Vector<String> horizontalRadii;
334 bool areDefaultCornerRadii = buildInsetRadii(horizontalRadii, topLeftRadiusWidth, topRightRadiusWidth, bottomRightRadiusWidth, bottomLeftRadiusWidth);
336 Vector<String> verticalRadii;
337 areDefaultCornerRadii &= buildInsetRadii(verticalRadii, topLeftRadiusHeight, topRightRadiusHeight, bottomRightRadiusHeight, bottomLeftRadiusHeight);
339 if (!areDefaultCornerRadii) {
340 result.appendLiteral(separator);
341 result.appendLiteral(cornersSeparator);
343 for (size_t i = 0; i < horizontalRadii.size(); ++i) {
344 result.appendLiteral(separator);
345 result.append(horizontalRadii[i]);
347 if (horizontalRadii != verticalRadii) {
348 result.appendLiteral(separator);
349 result.appendLiteral("/");
351 for (size_t i = 0; i < verticalRadii.size(); ++i) {
352 result.appendLiteral(separator);
353 result.append(verticalRadii[i]);
360 return result.toString();
363 static inline void updateCornerRadiusWidthAndHeight(CSSPrimitiveValue* corner, String& width, String& height)
368 Pair* radius = corner->getPairValue();
369 width = radius->first() ? radius->first()->cssText() : String("0");
370 if (radius->second())
371 height = radius->second()->cssText();
374 String CSSBasicShapeInset::cssText() const
376 String topLeftRadiusWidth;
377 String topLeftRadiusHeight;
378 String topRightRadiusWidth;
379 String topRightRadiusHeight;
380 String bottomRightRadiusWidth;
381 String bottomRightRadiusHeight;
382 String bottomLeftRadiusWidth;
383 String bottomLeftRadiusHeight;
385 updateCornerRadiusWidthAndHeight(topLeftRadius(), topLeftRadiusWidth, topLeftRadiusHeight);
386 updateCornerRadiusWidthAndHeight(topRightRadius(), topRightRadiusWidth, topRightRadiusHeight);
387 updateCornerRadiusWidthAndHeight(bottomRightRadius(), bottomRightRadiusWidth, bottomRightRadiusHeight);
388 updateCornerRadiusWidthAndHeight(bottomLeftRadius(), bottomLeftRadiusWidth, bottomLeftRadiusHeight);
390 return buildInsetString(m_top ? m_top->cssText() : String(),
391 m_right ? m_right->cssText() : String(),
392 m_bottom ? m_bottom->cssText() : String(),
393 m_left ? m_left->cssText() : String(),
397 topRightRadiusHeight,
398 bottomRightRadiusWidth,
399 bottomRightRadiusHeight,
400 bottomLeftRadiusWidth,
401 bottomLeftRadiusHeight);
404 bool CSSBasicShapeInset::equals(const CSSBasicShape& shape) const
406 if (shape.type() != CSSBasicShapeInsetType)
409 const CSSBasicShapeInset& other = static_cast<const CSSBasicShapeInset&>(shape);
410 return compareCSSValuePtr(m_top, other.m_top)
411 && compareCSSValuePtr(m_right, other.m_right)
412 && compareCSSValuePtr(m_bottom, other.m_bottom)
413 && compareCSSValuePtr(m_left, other.m_left)
414 && compareCSSValuePtr(m_topLeftRadius, other.m_topLeftRadius)
415 && compareCSSValuePtr(m_topRightRadius, other.m_topRightRadius)
416 && compareCSSValuePtr(m_bottomRightRadius, other.m_bottomRightRadius)
417 && compareCSSValuePtr(m_bottomLeftRadius, other.m_bottomLeftRadius);
420 void CSSBasicShapeInset::trace(Visitor* visitor)
422 visitor->trace(m_top);
423 visitor->trace(m_right);
424 visitor->trace(m_bottom);
425 visitor->trace(m_left);
426 visitor->trace(m_topLeftRadius);
427 visitor->trace(m_topRightRadius);
428 visitor->trace(m_bottomRightRadius);
429 visitor->trace(m_bottomLeftRadius);
430 CSSBasicShape::trace(visitor);
433 } // namespace WebCore