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
|
/*
* Copyright (C) 2011 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 "CSSBasicShapes.h"
#include "CSSMarkup.h"
#include "CSSPrimitiveValueMappings.h"
#include "CSSValuePair.h"
#include "CSSValuePool.h"
#include "SVGPathByteStream.h"
#include "SVGPathUtilities.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
CSSCircleValue::CSSCircleValue(RefPtr<CSSValue> radius, RefPtr<CSSValue> centerX, RefPtr<CSSValue> centerY)
: CSSValue(CircleClass)
, m_radius(WTFMove(radius))
, m_centerX(WTFMove(centerX))
, m_centerY(WTFMove(centerY))
{
}
Ref<CSSCircleValue> CSSCircleValue::create(RefPtr<CSSValue> radius, RefPtr<CSSValue> centerX, RefPtr<CSSValue> centerY)
{
return adoptRef(*new CSSCircleValue(WTFMove(radius), WTFMove(centerX), WTFMove(centerY)));
}
struct SerializablePositionOffset {
CSSValueID side;
Ref<const CSSValue> amount;
};
static String serializePositionOffset(const SerializablePositionOffset& offset, const SerializablePositionOffset& other)
{
if ((offset.side == CSSValueLeft && other.side == CSSValueTop) || (offset.side == CSSValueTop && other.side == CSSValueLeft))
return offset.amount->cssText();
return makeString(nameLiteral(offset.side), ' ', offset.amount->cssText());
}
static bool isZeroLength(const CSSValue& value)
{
auto* primitive = dynamicDowncast<CSSPrimitiveValue>(value);
return primitive && primitive->isLength() && primitive->isZero().value_or(false);
}
static SerializablePositionOffset buildSerializablePositionOffset(CSSValue* offset, CSSValueID defaultSide)
{
CSSValueID side = defaultSide;
RefPtr<const CSSValue> amount;
if (!offset)
side = CSSValueCenter;
else if (offset->isValueID())
side = offset->valueID();
else if (offset->isPair()) {
side = offset->first().valueID();
amount = &offset->second();
} else
amount = offset;
if (!amount)
amount = CSSPrimitiveValue::create(Length(side == CSSValueCenter ? 50 : 0, LengthType::Percent));
if (side == CSSValueCenter)
side = defaultSide;
else if ((side == CSSValueRight || side == CSSValueBottom) && is<CSSPrimitiveValue>(*amount) && downcast<CSSPrimitiveValue>(*amount).isPercentage()) {
side = defaultSide;
amount = CSSPrimitiveValue::create(Length(100 - downcast<CSSPrimitiveValue>(*amount).floatValue(), LengthType::Percent));
} else if (isZeroLength(*amount)) {
if (side == CSSValueRight || side == CSSValueBottom)
amount = CSSPrimitiveValue::create(Length(100, LengthType::Percent));
else
amount = CSSPrimitiveValue::create(Length(0, LengthType::Percent));
side = defaultSide;
}
return { side, amount.releaseNonNull() };
}
String CSSCircleValue::customCSSText() const
{
String radius;
if (m_radius && m_radius->valueID() != CSSValueClosestSide)
radius = m_radius->cssText();
// FIXME: We should serialize without "at 50% 50%".
auto x = buildSerializablePositionOffset(m_centerX.get(), CSSValueLeft);
auto y = buildSerializablePositionOffset(m_centerY.get(), CSSValueTop);
return makeString("circle("_s, radius, radius.isNull() ? ""_s : " "_s,
"at ", serializePositionOffset(x, y), ' ', serializePositionOffset(y, x), ')');
}
bool CSSCircleValue::equals(const CSSCircleValue& other) const
{
return compareCSSValuePtr(m_centerX, other.m_centerX)
&& compareCSSValuePtr(m_centerY, other.m_centerY)
&& compareCSSValuePtr(m_radius, other.m_radius);
}
CSSEllipseValue::CSSEllipseValue(RefPtr<CSSValue> radiusX, RefPtr<CSSValue> radiusY, RefPtr<CSSValue> centerX, RefPtr<CSSValue> centerY)
: CSSValue(EllipseClass)
, m_radiusX(WTFMove(radiusX))
, m_radiusY(WTFMove(radiusY))
, m_centerX(WTFMove(centerX))
, m_centerY(WTFMove(centerY))
{
}
Ref<CSSEllipseValue> CSSEllipseValue::create(RefPtr<CSSValue> radiusX, RefPtr<CSSValue> radiusY, RefPtr<CSSValue> centerX, RefPtr<CSSValue> centerY)
{
return adoptRef(*new CSSEllipseValue(WTFMove(radiusX), WTFMove(radiusY), WTFMove(centerX), WTFMove(centerY)));
}
static String buildEllipseString(const String& radiusX, const String& radiusY, const String& centerX, const String& centerY)
{
StringBuilder result;
result.append("ellipse(");
bool needsSeparator = false;
if (!radiusX.isNull()) {
result.append(radiusX);
needsSeparator = true;
}
if (!radiusY.isNull()) {
if (needsSeparator)
result.append(' ');
result.append(radiusY);
needsSeparator = true;
}
if (!centerX.isNull() || !centerY.isNull()) {
if (needsSeparator)
result.append(' ');
result.append("at ", centerX, ' ', centerY);
}
result.append(')');
return result.toString();
}
String CSSEllipseValue::customCSSText() const
{
String radiusX;
String radiusY;
if (m_radiusX) {
ASSERT(m_radiusY);
bool radiusXClosestSide = m_radiusX->valueID() == CSSValueClosestSide;
bool radiusYClosestSide = m_radiusY->valueID() == CSSValueClosestSide;
if (!radiusXClosestSide || !radiusYClosestSide) {
radiusX = m_radiusX->cssText();
radiusY = m_radiusY->cssText();
}
}
// FIXME: We should serialize without "at 50% 50%".
auto x = buildSerializablePositionOffset(m_centerX.get(), CSSValueLeft);
auto y = buildSerializablePositionOffset(m_centerY.get(), CSSValueTop);
return buildEllipseString(radiusX, radiusY, serializePositionOffset(x, y), serializePositionOffset(y, x));
}
bool CSSEllipseValue::equals(const CSSEllipseValue& other) const
{
return compareCSSValuePtr(m_centerX, other.m_centerX)
&& compareCSSValuePtr(m_centerY, other.m_centerY)
&& compareCSSValuePtr(m_radiusX, other.m_radiusX)
&& compareCSSValuePtr(m_radiusY, other.m_radiusY);
}
CSSPathValue::CSSPathValue(SVGPathByteStream data, WindRule rule)
: CSSValue(PathClass)
, m_pathData(WTFMove(data))
, m_windRule(rule)
{
}
Ref<CSSPathValue> CSSPathValue::create(SVGPathByteStream data, WindRule rule)
{
return adoptRef(*new CSSPathValue(WTFMove(data), rule));
}
String CSSPathValue::customCSSText() const
{
String pathString;
buildStringFromByteStream(m_pathData, pathString, UnalteredParsing);
StringBuilder result;
if (m_windRule == WindRule::EvenOdd)
result.append("path(evenodd, "_s);
else
result.append("path("_s);
serializeString(pathString, result);
result.append(')');
return result.toString();
}
bool CSSPathValue::equals(const CSSPathValue& other) const
{
return m_pathData == other.m_pathData && m_windRule == other.m_windRule;
}
CSSPolygonValue::CSSPolygonValue(CSSValueListBuilder values, WindRule rule)
: CSSValueContainingVector(PolygonClass, SpaceSeparator, WTFMove(values))
, m_windRule(rule)
{
}
Ref<CSSPolygonValue> CSSPolygonValue::create(CSSValueListBuilder values, WindRule rule)
{
return adoptRef(*new CSSPolygonValue(WTFMove(values), rule));
}
String CSSPolygonValue::customCSSText() const
{
ASSERT(!(length() % 2));
StringBuilder result;
if (m_windRule == WindRule::EvenOdd)
result.append("polygon(evenodd, "_s);
else
result.append("polygon("_s);
for (size_t i = 0; i < length(); i += 2)
result.append(i ? ", "_s : ""_s, item(i)->cssText(), ' ', item(i + 1)->cssText());
result.append(')');
return result.toString();
}
bool CSSPolygonValue::equals(const CSSPolygonValue& other) const
{
return m_windRule == other.m_windRule && itemsEqual(other);
}
CSSInsetShapeValue::CSSInsetShapeValue(Ref<CSSValue> top, Ref<CSSValue> right, Ref<CSSValue> bottom, Ref<CSSValue> left, RefPtr<CSSValue> topLeftRadius, RefPtr<CSSValue> topRightRadius, RefPtr<CSSValue> bottomRightRadius, RefPtr<CSSValue> bottomLeftRadius)
: CSSValue(InsetShapeClass)
, m_top(WTFMove(top))
, m_right(WTFMove(right))
, m_bottom(WTFMove(bottom))
, m_left(WTFMove(left))
, m_topLeftRadius(WTFMove(topLeftRadius))
, m_topRightRadius(WTFMove(topRightRadius))
, m_bottomRightRadius(WTFMove(bottomRightRadius))
, m_bottomLeftRadius(WTFMove(bottomLeftRadius))
{
}
Ref<CSSInsetShapeValue> CSSInsetShapeValue::create(Ref<CSSValue> top, Ref<CSSValue> right, Ref<CSSValue> bottom, Ref<CSSValue> left,
RefPtr<CSSValue> topLeftRadius, RefPtr<CSSValue> topRightRadius, RefPtr<CSSValue> bottomRightRadius, RefPtr<CSSValue> bottomLeftRadius)
{
return adoptRef(*new CSSInsetShapeValue(WTFMove(top), WTFMove(right), WTFMove(bottom), WTFMove(left),
WTFMove(topLeftRadius), WTFMove(topRightRadius), WTFMove(bottomRightRadius), WTFMove(bottomLeftRadius)));
}
static bool buildInsetRadii(Vector<String>& radii, const String& topLeftRadius, const String& topRightRadius, const String& bottomRightRadius, const String& bottomLeftRadius)
{
bool showBottomLeft = topRightRadius != bottomLeftRadius;
bool showBottomRight = showBottomLeft || (bottomRightRadius != topLeftRadius);
bool showTopRight = showBottomRight || (topRightRadius != topLeftRadius);
radii.append(topLeftRadius);
if (showTopRight)
radii.append(topRightRadius);
if (showBottomRight)
radii.append(bottomRightRadius);
if (showBottomLeft)
radii.append(bottomLeftRadius);
return radii.size() == 1 && radii[0] == "0px"_s;
}
static String buildInsetString(const String& top, const String& right, const String& bottom, const String& left,
const String& topLeftRadiusWidth, const String& topLeftRadiusHeight,
const String& topRightRadiusWidth, const String& topRightRadiusHeight,
const String& bottomRightRadiusWidth, const String& bottomRightRadiusHeight,
const String& bottomLeftRadiusWidth, const String& bottomLeftRadiusHeight)
{
StringBuilder result;
result.append("inset(", top);
bool showLeftArg = !left.isNull() && left != right;
bool showBottomArg = !bottom.isNull() && (bottom != top || showLeftArg);
bool showRightArg = !right.isNull() && (right != top || showBottomArg);
if (showRightArg)
result.append(' ', right);
if (showBottomArg)
result.append(' ', bottom);
if (showLeftArg)
result.append(' ', left);
if (!topLeftRadiusWidth.isNull() && !topLeftRadiusHeight.isNull()) {
Vector<String> horizontalRadii;
bool areDefaultCornerRadii = buildInsetRadii(horizontalRadii, topLeftRadiusWidth, topRightRadiusWidth, bottomRightRadiusWidth, bottomLeftRadiusWidth);
Vector<String> verticalRadii;
areDefaultCornerRadii &= buildInsetRadii(verticalRadii, topLeftRadiusHeight, topRightRadiusHeight, bottomRightRadiusHeight, bottomLeftRadiusHeight);
if (!areDefaultCornerRadii) {
result.append(" round"_s);
for (auto& radius : horizontalRadii)
result.append(' ', radius);
if (verticalRadii.size() != horizontalRadii.size()
|| !WTF::VectorComparer<false, String>::compare(verticalRadii.data(), horizontalRadii.data(), verticalRadii.size())) {
result.append(" /"_s);
for (auto& radius : verticalRadii)
result.append(' ', radius);
}
}
}
result.append(')');
return result.toString();
}
static inline void updateCornerRadiusWidthAndHeight(const CSSValue* corner, String& width, String& height)
{
if (!corner)
return;
width = corner->first().cssText();
height = corner->second().cssText();
}
String CSSInsetShapeValue::customCSSText() const
{
String topLeftRadiusWidth;
String topLeftRadiusHeight;
String topRightRadiusWidth;
String topRightRadiusHeight;
String bottomRightRadiusWidth;
String bottomRightRadiusHeight;
String bottomLeftRadiusWidth;
String bottomLeftRadiusHeight;
updateCornerRadiusWidthAndHeight(topLeftRadius(), topLeftRadiusWidth, topLeftRadiusHeight);
updateCornerRadiusWidthAndHeight(topRightRadius(), topRightRadiusWidth, topRightRadiusHeight);
updateCornerRadiusWidthAndHeight(bottomRightRadius(), bottomRightRadiusWidth, bottomRightRadiusHeight);
updateCornerRadiusWidthAndHeight(bottomLeftRadius(), bottomLeftRadiusWidth, bottomLeftRadiusHeight);
return buildInsetString(m_top->cssText(), m_right->cssText(), m_bottom->cssText(), m_left->cssText(),
topLeftRadiusWidth, topLeftRadiusHeight, topRightRadiusWidth, topRightRadiusHeight,
bottomRightRadiusWidth, bottomRightRadiusHeight, bottomLeftRadiusWidth, bottomLeftRadiusHeight);
}
bool CSSInsetShapeValue::equals(const CSSInsetShapeValue& other) const
{
return compareCSSValue(m_top, other.m_top)
&& compareCSSValue(m_right, other.m_right)
&& compareCSSValue(m_bottom, other.m_bottom)
&& compareCSSValue(m_left, other.m_left)
&& compareCSSValuePtr(m_topLeftRadius, other.m_topLeftRadius)
&& compareCSSValuePtr(m_topRightRadius, other.m_topRightRadius)
&& compareCSSValuePtr(m_bottomRightRadius, other.m_bottomRightRadius)
&& compareCSSValuePtr(m_bottomLeftRadius, other.m_bottomLeftRadius);
}
} // namespace WebCore
|