2017-01-06 11:35:29 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/ShapeUtils.h"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2017-01-06 11:35:53 +03:00
|
|
|
#include "nsCSSRendering.h"
|
2017-02-16 05:51:47 +03:00
|
|
|
#include "nsMargin.h"
|
2017-01-06 11:36:05 +03:00
|
|
|
#include "nsStyleCoord.h"
|
2017-01-06 11:35:53 +03:00
|
|
|
#include "nsStyleStruct.h"
|
2017-01-06 11:36:05 +03:00
|
|
|
#include "SVGContentUtils.h"
|
2017-01-06 11:35:53 +03:00
|
|
|
|
2017-01-06 11:35:29 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2019-05-09 14:24:57 +03:00
|
|
|
nscoord ShapeUtils::ComputeShapeRadius(const StyleShapeRadius& aType,
|
2017-01-06 11:35:29 +03:00
|
|
|
const nscoord aCenter,
|
|
|
|
const nscoord aPosMin,
|
|
|
|
const nscoord aPosMax) {
|
2019-05-09 14:24:57 +03:00
|
|
|
MOZ_ASSERT(aType.IsFarthestSide() || aType.IsClosestSide());
|
2017-01-06 11:35:29 +03:00
|
|
|
nscoord dist1 = std::abs(aPosMin - aCenter);
|
|
|
|
nscoord dist2 = std::abs(aPosMax - aCenter);
|
|
|
|
nscoord length = 0;
|
2019-05-09 14:24:57 +03:00
|
|
|
if (aType.IsFarthestSide()) {
|
|
|
|
length = dist1 > dist2 ? dist1 : dist2;
|
|
|
|
} else {
|
|
|
|
length = dist1 > dist2 ? dist2 : dist1;
|
2017-01-06 11:35:29 +03:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-11-02 21:19:06 +03:00
|
|
|
nsPoint ShapeUtils::ComputeCircleOrEllipseCenter(
|
|
|
|
const StyleBasicShape& aBasicShape, const nsRect& aRefBox) {
|
2019-05-09 14:24:57 +03:00
|
|
|
MOZ_ASSERT(aBasicShape.IsCircle() || aBasicShape.IsEllipse(),
|
2017-02-16 05:51:47 +03:00
|
|
|
"The basic shape must be circle() or ellipse!");
|
|
|
|
|
2019-05-09 14:24:57 +03:00
|
|
|
const auto& position = aBasicShape.IsCircle()
|
|
|
|
? aBasicShape.AsCircle().position
|
|
|
|
: aBasicShape.AsEllipse().position;
|
|
|
|
|
2017-01-06 11:35:53 +03:00
|
|
|
nsPoint topLeft, anchor;
|
|
|
|
nsSize size(aRefBox.Size());
|
2019-05-09 14:24:57 +03:00
|
|
|
nsImageRenderer::ComputeObjectAnchorPoint(position, size, size, &topLeft,
|
|
|
|
&anchor);
|
2017-03-13 07:03:39 +03:00
|
|
|
return anchor + aRefBox.TopLeft();
|
2017-01-06 11:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-11-02 21:19:06 +03:00
|
|
|
nscoord ShapeUtils::ComputeCircleRadius(const StyleBasicShape& aBasicShape,
|
2017-01-06 11:36:05 +03:00
|
|
|
const nsPoint& aCenter,
|
|
|
|
const nsRect& aRefBox) {
|
2019-05-09 14:24:57 +03:00
|
|
|
MOZ_ASSERT(aBasicShape.IsCircle(), "The basic shape must be circle()!");
|
|
|
|
const auto& radius = aBasicShape.AsCircle().radius;
|
|
|
|
if (radius.IsLength()) {
|
|
|
|
return radius.AsLength().Resolve([&] {
|
|
|
|
// We resolve percent <shape-radius> value for circle() as defined here:
|
|
|
|
// https://drafts.csswg.org/css-shapes/#funcdef-circle
|
|
|
|
double referenceLength = SVGContentUtils::ComputeNormalizedHypotenuse(
|
|
|
|
aRefBox.width, aRefBox.height);
|
|
|
|
return NSToCoordRound(referenceLength);
|
|
|
|
});
|
2017-01-06 11:36:05 +03:00
|
|
|
}
|
2019-05-09 14:24:57 +03:00
|
|
|
|
|
|
|
nscoord horizontal =
|
|
|
|
ComputeShapeRadius(radius, aCenter.x, aRefBox.x, aRefBox.XMost());
|
|
|
|
nscoord vertical =
|
|
|
|
ComputeShapeRadius(radius, aCenter.y, aRefBox.y, aRefBox.YMost());
|
|
|
|
return radius.IsFarthestSide() ? std::max(horizontal, vertical)
|
|
|
|
: std::min(horizontal, vertical);
|
2017-01-06 11:36:05 +03:00
|
|
|
}
|
|
|
|
|
2018-11-02 21:19:06 +03:00
|
|
|
nsSize ShapeUtils::ComputeEllipseRadii(const StyleBasicShape& aBasicShape,
|
2017-01-23 12:17:05 +03:00
|
|
|
const nsPoint& aCenter,
|
|
|
|
const nsRect& aRefBox) {
|
2019-05-09 14:24:57 +03:00
|
|
|
MOZ_ASSERT(aBasicShape.IsEllipse(), "The basic shape must be ellipse()!");
|
|
|
|
const auto& ellipse = aBasicShape.AsEllipse();
|
2017-01-23 12:17:05 +03:00
|
|
|
nsSize radii;
|
2019-05-09 14:24:57 +03:00
|
|
|
if (ellipse.semiaxis_x.IsLength()) {
|
|
|
|
radii.width = ellipse.semiaxis_x.AsLength().Resolve(aRefBox.width);
|
2017-01-23 12:17:05 +03:00
|
|
|
} else {
|
2019-05-09 14:24:57 +03:00
|
|
|
radii.width = ComputeShapeRadius(ellipse.semiaxis_x, aCenter.x, aRefBox.x,
|
|
|
|
aRefBox.XMost());
|
2017-01-23 12:17:05 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:24:57 +03:00
|
|
|
if (ellipse.semiaxis_y.IsLength()) {
|
|
|
|
radii.height = ellipse.semiaxis_y.AsLength().Resolve(aRefBox.height);
|
2017-01-23 12:17:05 +03:00
|
|
|
} else {
|
2019-05-09 14:24:57 +03:00
|
|
|
radii.height = ComputeShapeRadius(ellipse.semiaxis_y, aCenter.y, aRefBox.y,
|
|
|
|
aRefBox.YMost());
|
2017-01-23 12:17:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return radii;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
nsRect ShapeUtils::ComputeInsetRect(const StyleBasicShape& aBasicShape,
|
|
|
|
const nsRect& aRefBox) {
|
2019-05-09 14:24:57 +03:00
|
|
|
MOZ_ASSERT(aBasicShape.IsInset(), "The basic shape must be inset()!");
|
|
|
|
const auto& rect = aBasicShape.AsInset().rect;
|
|
|
|
nsMargin inset(
|
|
|
|
rect._0.Resolve(aRefBox.Height()), rect._1.Resolve(aRefBox.Width()),
|
|
|
|
rect._2.Resolve(aRefBox.Height()), rect._3.Resolve(aRefBox.Width()));
|
2018-05-19 03:51:19 +03:00
|
|
|
|
|
|
|
nscoord x = aRefBox.X() + inset.left;
|
|
|
|
nscoord width = aRefBox.Width() - inset.LeftRight();
|
|
|
|
nscoord y = aRefBox.Y() + inset.top;
|
|
|
|
nscoord height = aRefBox.Height() - inset.TopBottom();
|
|
|
|
|
|
|
|
// Invert left and right, if necessary.
|
|
|
|
if (width < 0) {
|
|
|
|
width *= -1;
|
|
|
|
x -= width;
|
|
|
|
}
|
2017-02-16 05:51:47 +03:00
|
|
|
|
2018-05-19 03:51:19 +03:00
|
|
|
// Invert top and bottom, if necessary.
|
|
|
|
if (height < 0) {
|
|
|
|
height *= -1;
|
|
|
|
y -= height;
|
|
|
|
}
|
2017-02-16 05:51:47 +03:00
|
|
|
|
2018-05-19 03:51:19 +03:00
|
|
|
return nsRect(x, y, width, height);
|
2017-02-16 05:51:47 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
bool ShapeUtils::ComputeInsetRadii(const StyleBasicShape& aBasicShape,
|
|
|
|
const nsRect& aInsetRect,
|
|
|
|
const nsRect& aRefBox, nscoord aRadii[8]) {
|
2019-05-09 14:24:57 +03:00
|
|
|
const auto& radius = aBasicShape.AsInset().round;
|
2017-02-16 05:51:48 +03:00
|
|
|
return nsIFrame::ComputeBorderRadii(radius, aInsetRect.Size(), aRefBox.Size(),
|
|
|
|
Sides(), aRadii);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
nsTArray<nsPoint> ShapeUtils::ComputePolygonVertices(
|
2018-11-02 21:19:06 +03:00
|
|
|
const StyleBasicShape& aBasicShape, const nsRect& aRefBox) {
|
2019-05-09 14:24:57 +03:00
|
|
|
MOZ_ASSERT(aBasicShape.IsPolygon(), "The basic shape must be polygon()!");
|
|
|
|
|
|
|
|
auto coords = aBasicShape.AsPolygon().coordinates.AsSpan();
|
|
|
|
nsTArray<nsPoint> vertices(coords.Length());
|
|
|
|
for (const StylePolygonCoord<LengthPercentage>& point : coords) {
|
|
|
|
vertices.AppendElement(nsPoint(point._0.Resolve(aRefBox.width),
|
|
|
|
point._1.Resolve(aRefBox.height)) +
|
|
|
|
aRefBox.TopLeft());
|
2017-03-13 07:03:40 +03:00
|
|
|
}
|
|
|
|
return vertices;
|
|
|
|
}
|
|
|
|
|
2017-01-06 11:35:29 +03:00
|
|
|
} // namespace mozilla
|