2 * Copyright (C) 2012 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 HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "BasicShapeFunctions.h"
34 #include "FloatSize.h"
35 #include "LengthFunctions.h"
36 #include "PolygonShape.h"
37 #include "RectangleShape.h"
39 #include <wtf/MathExtras.h>
40 #include <wtf/OwnPtr.h>
41 #include <wtf/PassOwnPtr.h>
45 static PassOwnPtr<Shape> createRectangleShape(const FloatRect& bounds, const FloatSize& radii)
47 ASSERT(bounds.width() >= 0 && bounds.height() >= 0 && radii.width() >= 0 && radii.height() >= 0);
48 return adoptPtr(new RectangleShape(bounds, radii));
51 static PassOwnPtr<Shape> createShapeCircle(const FloatPoint& center, float radius)
54 return adoptPtr(new RectangleShape(FloatRect(center.x() - radius, center.y() - radius, radius*2, radius*2), FloatSize(radius, radius)));
57 static PassOwnPtr<Shape> createShapeEllipse(const FloatPoint& center, const FloatSize& radii)
59 ASSERT(radii.width() >= 0 && radii.height() >= 0);
60 return adoptPtr(new RectangleShape(FloatRect(center.x() - radii.width(), center.y() - radii.height(), radii.width()*2, radii.height()*2), radii));
63 static PassOwnPtr<Shape> createPolygonShape(PassOwnPtr<Vector<FloatPoint> > vertices, WindRule fillRule)
65 return adoptPtr(new PolygonShape(vertices, fillRule));
68 static inline FloatRect physicalRectToLogical(const FloatRect& rect, float logicalBoxHeight, WritingMode writingMode)
70 if (isHorizontalWritingMode(writingMode))
72 if (isFlippedBlocksWritingMode(writingMode))
73 return FloatRect(rect.y(), logicalBoxHeight - rect.maxX(), rect.height(), rect.width());
74 return rect.transposedRect();
77 static inline FloatPoint physicalPointToLogical(const FloatPoint& point, float logicalBoxHeight, WritingMode writingMode)
79 if (isHorizontalWritingMode(writingMode))
81 if (isFlippedBlocksWritingMode(writingMode))
82 return FloatPoint(point.y(), logicalBoxHeight - point.x());
83 return point.transposedPoint();
86 static inline FloatSize physicalSizeToLogical(const FloatSize& size, WritingMode writingMode)
88 if (isHorizontalWritingMode(writingMode))
90 return size.transposedSize();
93 PassOwnPtr<Shape> Shape::createShape(const BasicShape* basicShape, const LayoutSize& logicalBoxSize, WritingMode writingMode, Length margin, Length padding)
97 bool horizontalWritingMode = isHorizontalWritingMode(writingMode);
98 float boxWidth = horizontalWritingMode ? logicalBoxSize.width() : logicalBoxSize.height();
99 float boxHeight = horizontalWritingMode ? logicalBoxSize.height() : logicalBoxSize.width();
102 switch (basicShape->type()) {
104 case BasicShape::BasicShapeRectangleType: {
105 const BasicShapeRectangle* rectangle = static_cast<const BasicShapeRectangle*>(basicShape);
107 floatValueForLength(rectangle->x(), boxWidth),
108 floatValueForLength(rectangle->y(), boxHeight),
109 floatValueForLength(rectangle->width(), boxWidth),
110 floatValueForLength(rectangle->height(), boxHeight));
111 Length radiusXLength = rectangle->cornerRadiusX();
112 Length radiusYLength = rectangle->cornerRadiusY();
113 FloatSize cornerRadii(
114 radiusXLength.isUndefined() ? 0 : floatValueForLength(radiusXLength, boxWidth),
115 radiusYLength.isUndefined() ? 0 : floatValueForLength(radiusYLength, boxHeight));
116 FloatRect logicalBounds = physicalRectToLogical(bounds, logicalBoxSize.height(), writingMode);
118 shape = createRectangleShape(logicalBounds, physicalSizeToLogical(cornerRadii, writingMode));
122 case BasicShape::BasicShapeCircleType: {
123 const BasicShapeCircle* circle = static_cast<const BasicShapeCircle*>(basicShape);
124 float centerX = floatValueForLength(circle->centerX(), boxWidth);
125 float centerY = floatValueForLength(circle->centerY(), boxHeight);
126 float radius = floatValueForLength(circle->radius(), std::min(boxHeight, boxWidth));
127 FloatPoint logicalCenter = physicalPointToLogical(FloatPoint(centerX, centerY), logicalBoxSize.height(), writingMode);
129 shape = createShapeCircle(logicalCenter, radius);
133 case BasicShape::BasicShapeEllipseType: {
134 const BasicShapeEllipse* ellipse = static_cast<const BasicShapeEllipse*>(basicShape);
135 float centerX = floatValueForLength(ellipse->centerX(), boxWidth);
136 float centerY = floatValueForLength(ellipse->centerY(), boxHeight);
137 float radiusX = floatValueForLength(ellipse->radiusX(), boxWidth);
138 float radiusY = floatValueForLength(ellipse->radiusY(), boxHeight);
139 FloatPoint logicalCenter = physicalPointToLogical(FloatPoint(centerX, centerY), logicalBoxSize.height(), writingMode);
140 FloatSize logicalRadii = physicalSizeToLogical(FloatSize(radiusX, radiusY), writingMode);
142 shape = createShapeEllipse(logicalCenter, logicalRadii);
146 case BasicShape::BasicShapePolygonType: {
147 const BasicShapePolygon* polygon = static_cast<const BasicShapePolygon*>(basicShape);
148 const Vector<Length>& values = polygon->values();
149 size_t valuesSize = values.size();
150 ASSERT(!(valuesSize % 2));
151 OwnPtr<Vector<FloatPoint> > vertices = adoptPtr(new Vector<FloatPoint>(valuesSize / 2));
152 for (unsigned i = 0; i < valuesSize; i += 2) {
154 floatValueForLength(values.at(i), boxWidth),
155 floatValueForLength(values.at(i + 1), boxHeight));
156 (*vertices)[i / 2] = physicalPointToLogical(vertex, logicalBoxSize.height(), writingMode);
159 shape = createPolygonShape(vertices.release(), polygon->windRule());
163 case BasicShape::BasicShapeInsetRectangleType: {
164 const BasicShapeInsetRectangle* rectangle = static_cast<const BasicShapeInsetRectangle*>(basicShape);
165 float left = floatValueForLength(rectangle->left(), boxWidth);
166 float top = floatValueForLength(rectangle->top(), boxHeight);
170 boxWidth - left - floatValueForLength(rectangle->right(), boxWidth),
171 boxHeight - top - floatValueForLength(rectangle->bottom(), boxHeight));
172 Length radiusXLength = rectangle->cornerRadiusX();
173 Length radiusYLength = rectangle->cornerRadiusY();
174 FloatSize cornerRadii(
175 radiusXLength.isUndefined() ? 0 : floatValueForLength(radiusXLength, boxWidth),
176 radiusYLength.isUndefined() ? 0 : floatValueForLength(radiusYLength, boxHeight));
177 FloatRect logicalBounds = physicalRectToLogical(bounds, logicalBoxSize.height(), writingMode);
179 shape = createRectangleShape(logicalBounds, physicalSizeToLogical(cornerRadii, writingMode));
184 ASSERT_NOT_REACHED();
187 shape->m_writingMode = writingMode;
188 shape->m_margin = floatValueForLength(margin, 0);
189 shape->m_padding = floatValueForLength(padding, 0);
191 return shape.release();
194 } // namespace WebCore