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 {
|
|
|
|
|
|
|
|
nscoord
|
|
|
|
ShapeUtils::ComputeShapeRadius(const StyleShapeRadius aType,
|
|
|
|
const nscoord aCenter,
|
|
|
|
const nscoord aPosMin,
|
|
|
|
const nscoord aPosMax)
|
|
|
|
{
|
|
|
|
nscoord dist1 = std::abs(aPosMin - aCenter);
|
|
|
|
nscoord dist2 = std::abs(aPosMax - aCenter);
|
|
|
|
nscoord length = 0;
|
|
|
|
switch (aType) {
|
|
|
|
case StyleShapeRadius::FarthestSide:
|
|
|
|
length = dist1 > dist2 ? dist1 : dist2;
|
|
|
|
break;
|
|
|
|
case StyleShapeRadius::ClosestSide:
|
|
|
|
length = dist1 > dist2 ? dist2 : dist1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2017-01-06 11:35:53 +03:00
|
|
|
nsPoint
|
2017-09-25 13:50:24 +03:00
|
|
|
ShapeUtils::ComputeCircleOrEllipseCenter(const UniquePtr<StyleBasicShape>& aBasicShape,
|
2017-01-06 11:35:53 +03:00
|
|
|
const nsRect& aRefBox)
|
|
|
|
{
|
2017-02-16 05:51:47 +03:00
|
|
|
MOZ_ASSERT(aBasicShape->GetShapeType() == StyleBasicShapeType::Circle ||
|
|
|
|
aBasicShape->GetShapeType() == StyleBasicShapeType::Ellipse,
|
|
|
|
"The basic shape must be circle() or ellipse!");
|
|
|
|
|
2017-01-06 11:35:53 +03:00
|
|
|
nsPoint topLeft, anchor;
|
|
|
|
nsSize size(aRefBox.Size());
|
|
|
|
nsImageRenderer::ComputeObjectAnchorPoint(aBasicShape->GetPosition(),
|
|
|
|
size, size,
|
|
|
|
&topLeft, &anchor);
|
2017-03-13 07:03:39 +03:00
|
|
|
return anchor + aRefBox.TopLeft();
|
2017-01-06 11:35:53 +03:00
|
|
|
}
|
|
|
|
|
2017-01-06 11:36:05 +03:00
|
|
|
nscoord
|
2017-09-25 13:50:24 +03:00
|
|
|
ShapeUtils::ComputeCircleRadius(const UniquePtr<StyleBasicShape>& aBasicShape,
|
2017-01-06 11:36:05 +03:00
|
|
|
const nsPoint& aCenter,
|
|
|
|
const nsRect& aRefBox)
|
|
|
|
{
|
2017-02-16 05:51:47 +03:00
|
|
|
MOZ_ASSERT(aBasicShape->GetShapeType() == StyleBasicShapeType::Circle,
|
|
|
|
"The basic shape must be circle()!");
|
|
|
|
|
2017-01-06 11:36:05 +03:00
|
|
|
const nsTArray<nsStyleCoord>& coords = aBasicShape->Coordinates();
|
|
|
|
MOZ_ASSERT(coords.Length() == 1, "wrong number of arguments");
|
|
|
|
nscoord r = 0;
|
|
|
|
if (coords[0].GetUnit() == eStyleUnit_Enumerated) {
|
|
|
|
const auto styleShapeRadius = coords[0].GetEnumValue<StyleShapeRadius>();
|
|
|
|
nscoord horizontal =
|
|
|
|
ComputeShapeRadius(styleShapeRadius, aCenter.x, aRefBox.x, aRefBox.XMost());
|
|
|
|
nscoord vertical =
|
|
|
|
ComputeShapeRadius(styleShapeRadius, aCenter.y, aRefBox.y, aRefBox.YMost());
|
|
|
|
r = styleShapeRadius == StyleShapeRadius::FarthestSide
|
|
|
|
? std::max(horizontal, vertical)
|
|
|
|
: std::min(horizontal, vertical);
|
|
|
|
} else {
|
|
|
|
// 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);
|
2017-11-20 09:05:19 +03:00
|
|
|
r = coords[0].ComputeCoordPercentCalc(NSToCoordRound(referenceLength));
|
2017-01-06 11:36:05 +03:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-01-23 12:17:05 +03:00
|
|
|
nsSize
|
2017-09-25 13:50:24 +03:00
|
|
|
ShapeUtils::ComputeEllipseRadii(const UniquePtr<StyleBasicShape>& aBasicShape,
|
2017-01-23 12:17:05 +03:00
|
|
|
const nsPoint& aCenter,
|
|
|
|
const nsRect& aRefBox)
|
|
|
|
{
|
2017-02-16 05:51:47 +03:00
|
|
|
MOZ_ASSERT(aBasicShape->GetShapeType() == StyleBasicShapeType::Ellipse,
|
|
|
|
"The basic shape must be ellipse()!");
|
|
|
|
|
2017-01-23 12:17:05 +03:00
|
|
|
const nsTArray<nsStyleCoord>& coords = aBasicShape->Coordinates();
|
|
|
|
MOZ_ASSERT(coords.Length() == 2, "wrong number of arguments");
|
|
|
|
nsSize radii;
|
|
|
|
|
|
|
|
if (coords[0].GetUnit() == eStyleUnit_Enumerated) {
|
|
|
|
const StyleShapeRadius radiusX = coords[0].GetEnumValue<StyleShapeRadius>();
|
|
|
|
radii.width = ComputeShapeRadius(radiusX, aCenter.x, aRefBox.x,
|
|
|
|
aRefBox.XMost());
|
|
|
|
} else {
|
2017-11-20 09:05:19 +03:00
|
|
|
radii.width = coords[0].ComputeCoordPercentCalc(aRefBox.width);
|
2017-01-23 12:17:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (coords[1].GetUnit() == eStyleUnit_Enumerated) {
|
|
|
|
const StyleShapeRadius radiusY = coords[1].GetEnumValue<StyleShapeRadius>();
|
|
|
|
radii.height = ComputeShapeRadius(radiusY, aCenter.y, aRefBox.y,
|
|
|
|
aRefBox.YMost());
|
|
|
|
} else {
|
2017-11-20 09:05:19 +03:00
|
|
|
radii.height = coords[1].ComputeCoordPercentCalc(aRefBox.height);
|
2017-01-23 12:17:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return radii;
|
|
|
|
}
|
|
|
|
|
2017-02-16 05:51:47 +03:00
|
|
|
/* static */ nsRect
|
2017-09-25 13:50:24 +03:00
|
|
|
ShapeUtils::ComputeInsetRect(const UniquePtr<StyleBasicShape>& aBasicShape,
|
2017-02-16 05:51:47 +03:00
|
|
|
const nsRect& aRefBox)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBasicShape->GetShapeType() == StyleBasicShapeType::Inset,
|
|
|
|
"The basic shape must be inset()!");
|
|
|
|
|
|
|
|
const nsTArray<nsStyleCoord>& coords = aBasicShape->Coordinates();
|
|
|
|
MOZ_ASSERT(coords.Length() == 4, "wrong number of arguments");
|
|
|
|
|
2018-05-19 03:51:19 +03:00
|
|
|
nsMargin inset(coords[0].ComputeCoordPercentCalc(aRefBox.Height()),
|
|
|
|
coords[1].ComputeCoordPercentCalc(aRefBox.Width()),
|
|
|
|
coords[2].ComputeCoordPercentCalc(aRefBox.Height()),
|
|
|
|
coords[3].ComputeCoordPercentCalc(aRefBox.Width()));
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-16 05:51:48 +03:00
|
|
|
/* static */ bool
|
2017-09-25 13:50:24 +03:00
|
|
|
ShapeUtils::ComputeInsetRadii(const UniquePtr<StyleBasicShape>& aBasicShape,
|
2017-02-16 05:51:48 +03:00
|
|
|
const nsRect& aInsetRect,
|
|
|
|
const nsRect& aRefBox,
|
|
|
|
nscoord aRadii[8])
|
|
|
|
{
|
|
|
|
const nsStyleCorners& radius = aBasicShape->GetRadius();
|
|
|
|
return nsIFrame::ComputeBorderRadii(radius, aInsetRect.Size(), aRefBox.Size(),
|
|
|
|
Sides(), aRadii);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-13 07:03:40 +03:00
|
|
|
/* static */ nsTArray<nsPoint>
|
2017-09-25 13:50:24 +03:00
|
|
|
ShapeUtils::ComputePolygonVertices(const UniquePtr<StyleBasicShape>& aBasicShape,
|
2017-03-13 07:03:40 +03:00
|
|
|
const nsRect& aRefBox)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBasicShape->GetShapeType() == StyleBasicShapeType::Polygon,
|
|
|
|
"The basic shape must be polygon()!");
|
|
|
|
|
|
|
|
const nsTArray<nsStyleCoord>& coords = aBasicShape->Coordinates();
|
|
|
|
MOZ_ASSERT(coords.Length() % 2 == 0 &&
|
|
|
|
coords.Length() >= 2, "Wrong number of arguments!");
|
|
|
|
|
|
|
|
nsTArray<nsPoint> vertices(coords.Length() / 2);
|
|
|
|
for (size_t i = 0; i + 1 < coords.Length(); i += 2) {
|
|
|
|
vertices.AppendElement(
|
2017-11-20 09:05:19 +03:00
|
|
|
nsPoint(coords[i].ComputeCoordPercentCalc(aRefBox.width),
|
|
|
|
coords[i + 1].ComputeCoordPercentCalc(aRefBox.height))
|
2017-03-13 07:03:40 +03:00
|
|
|
+ aRefBox.TopLeft());
|
|
|
|
}
|
|
|
|
return vertices;
|
|
|
|
}
|
|
|
|
|
2017-01-06 11:35:29 +03:00
|
|
|
} // namespace mozilla
|