2017-10-28 02:10:06 +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
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_PATHHELPERS_H_
|
|
|
|
#define MOZILLA_GFX_PATHHELPERS_H_
|
|
|
|
|
|
|
|
#include "2D.h"
|
2014-10-19 13:22:47 +04:00
|
|
|
#include "UserData.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2015-11-28 07:49:55 +03:00
|
|
|
#include <cmath>
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2015-07-27 19:12:22 +03:00
|
|
|
// Kappa constant for 90-degree angle
|
|
|
|
const Float kKappaFactor = 0.55191497064665766025f;
|
|
|
|
|
|
|
|
// Calculate kappa constant for partial curve. The sign of angle in the
|
|
|
|
// tangent will actually ensure this is negative for a counter clockwise
|
|
|
|
// sweep, so changing signs later isn't needed.
|
|
|
|
inline Float ComputeKappaFactor(Float aAngle)
|
|
|
|
{
|
|
|
|
return (4.0f / 3.0f) * tanf(aAngle / 4.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a partial arc <= 90 degrees given exact start and end points.
|
|
|
|
* Assumes that it is continuing from an already specified start point.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline void PartialArcToBezier(T* aSink,
|
|
|
|
const Point& aStartOffset, const Point& aEndOffset,
|
2016-03-19 07:29:13 +03:00
|
|
|
const Matrix& aTransform,
|
|
|
|
Float aKappaFactor = kKappaFactor)
|
2015-07-27 19:12:22 +03:00
|
|
|
{
|
|
|
|
Point cp1 =
|
2016-03-19 07:29:13 +03:00
|
|
|
aStartOffset + Point(-aStartOffset.y, aStartOffset.x) * aKappaFactor;
|
2015-07-27 19:12:22 +03:00
|
|
|
|
|
|
|
Point cp2 =
|
2016-03-19 07:29:13 +03:00
|
|
|
aEndOffset + Point(aEndOffset.y, -aEndOffset.x) * aKappaFactor;
|
2015-07-27 19:12:22 +03:00
|
|
|
|
2016-09-08 19:26:03 +03:00
|
|
|
aSink->BezierTo(aTransform.TransformPoint(cp1),
|
|
|
|
aTransform.TransformPoint(cp2),
|
|
|
|
aTransform.TransformPoint(aEndOffset));
|
2015-07-27 19:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an acute arc (<= 90 degrees) given exact start and end points.
|
|
|
|
* Specialized version avoiding kappa calculation.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline void AcuteArcToBezier(T* aSink,
|
|
|
|
const Point& aOrigin, const Size& aRadius,
|
|
|
|
const Point& aStartPoint, const Point& aEndPoint,
|
|
|
|
Float aKappaFactor = kKappaFactor)
|
|
|
|
{
|
|
|
|
aSink->LineTo(aStartPoint);
|
|
|
|
if (!aRadius.IsEmpty()) {
|
2016-03-19 07:29:13 +03:00
|
|
|
Float kappaX = aKappaFactor * aRadius.width / aRadius.height;
|
|
|
|
Float kappaY = aKappaFactor * aRadius.height / aRadius.width;
|
2015-07-27 19:12:22 +03:00
|
|
|
Point startOffset = aStartPoint - aOrigin;
|
|
|
|
Point endOffset = aEndPoint - aOrigin;
|
2016-03-19 07:29:13 +03:00
|
|
|
aSink->BezierTo(aStartPoint + Point(-startOffset.y * kappaX, startOffset.x * kappaY),
|
|
|
|
aEndPoint + Point(endOffset.y * kappaX, -endOffset.x * kappaY),
|
|
|
|
aEndPoint);
|
2015-07-27 19:12:22 +03:00
|
|
|
} else if (aEndPoint != aStartPoint) {
|
|
|
|
aSink->LineTo(aEndPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an acute arc (<= 90 degrees) given exact start and end points.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline void AcuteArcToBezier(T* aSink,
|
|
|
|
const Point& aOrigin, const Size& aRadius,
|
|
|
|
const Point& aStartPoint, const Point& aEndPoint,
|
|
|
|
Float aStartAngle, Float aEndAngle)
|
|
|
|
{
|
|
|
|
AcuteArcToBezier(aSink, aOrigin, aRadius, aStartPoint, aEndPoint,
|
|
|
|
ComputeKappaFactor(aEndAngle - aStartAngle));
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
template <typename T>
|
2013-12-13 16:14:36 +04:00
|
|
|
void ArcToBezier(T* aSink, const Point &aOrigin, const Size &aRadius,
|
2016-03-15 02:53:00 +03:00
|
|
|
float aStartAngle, float aEndAngle, bool aAntiClockwise,
|
|
|
|
float aRotation = 0.0f)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
2015-07-25 09:45:02 +03:00
|
|
|
Float sweepDirection = aAntiClockwise ? -1.0f : 1.0f;
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2015-07-25 09:45:02 +03:00
|
|
|
// Calculate the total arc we're going to sweep.
|
|
|
|
Float arcSweepLeft = (aEndAngle - aStartAngle) * sweepDirection;
|
2012-09-28 21:21:40 +04:00
|
|
|
|
|
|
|
// Clockwise we always sweep from the smaller to the larger angle, ccw
|
2011-11-02 23:55:03 +04:00
|
|
|
// it's vice versa.
|
2015-07-25 09:45:02 +03:00
|
|
|
if (arcSweepLeft < 0) {
|
|
|
|
// Rerverse sweep is modulo'd into range rather than clamped.
|
|
|
|
arcSweepLeft = Float(2.0f * M_PI) + fmodf(arcSweepLeft, Float(2.0f * M_PI));
|
|
|
|
// Recalculate the start angle to land closer to end angle.
|
|
|
|
aStartAngle = aEndAngle - arcSweepLeft * sweepDirection;
|
|
|
|
} else if (arcSweepLeft > Float(2.0f * M_PI)) {
|
|
|
|
// Sweeping more than 2 * pi is a full circle.
|
|
|
|
arcSweepLeft = Float(2.0f * M_PI);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
2012-09-28 21:21:40 +04:00
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
Float currentStartAngle = aStartAngle;
|
2015-07-27 19:12:22 +03:00
|
|
|
Point currentStartOffset(cosf(aStartAngle), sinf(aStartAngle));
|
2016-03-19 07:29:13 +03:00
|
|
|
Matrix transform = Matrix::Scaling(aRadius.width, aRadius.height);
|
|
|
|
if (aRotation != 0.0f) {
|
|
|
|
transform *= Matrix::Rotation(aRotation);
|
|
|
|
}
|
|
|
|
transform.PostTranslate(aOrigin);
|
2016-09-08 19:26:03 +03:00
|
|
|
aSink->LineTo(transform.TransformPoint(currentStartOffset));
|
2012-09-28 21:21:40 +04:00
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
while (arcSweepLeft > 0) {
|
2015-07-25 09:45:02 +03:00
|
|
|
Float currentEndAngle =
|
|
|
|
currentStartAngle + std::min(arcSweepLeft, Float(M_PI / 2.0f)) * sweepDirection;
|
2015-07-27 19:12:22 +03:00
|
|
|
Point currentEndOffset(cosf(currentEndAngle), sinf(currentEndAngle));
|
2012-09-28 21:21:40 +04:00
|
|
|
|
2016-03-19 07:29:13 +03:00
|
|
|
PartialArcToBezier(aSink, currentStartOffset, currentEndOffset, transform,
|
|
|
|
ComputeKappaFactor(currentEndAngle - currentStartAngle));
|
2012-09-28 21:21:40 +04:00
|
|
|
|
2015-07-27 19:12:22 +03:00
|
|
|
// We guarantee here the current point is the start point of the next
|
|
|
|
// curve segment.
|
2012-09-28 21:21:40 +04:00
|
|
|
arcSweepLeft -= Float(M_PI / 2.0f);
|
2011-11-02 23:55:03 +04:00
|
|
|
currentStartAngle = currentEndAngle;
|
2015-07-27 19:12:22 +03:00
|
|
|
currentStartOffset = currentEndOffset;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 00:42:12 +04:00
|
|
|
/* This is basically the ArcToBezier with the parameters for drawing a circle
|
|
|
|
* inlined which vastly simplifies it and avoids a bunch of transcedental function
|
|
|
|
* calls which should make it faster. */
|
|
|
|
template <typename T>
|
|
|
|
void EllipseToBezier(T* aSink, const Point &aOrigin, const Size &aRadius)
|
|
|
|
{
|
2016-03-19 07:29:13 +03:00
|
|
|
Matrix transform(aRadius.width, 0, 0, aRadius.height, aOrigin.x, aOrigin.y);
|
2015-07-27 19:12:22 +03:00
|
|
|
Point currentStartOffset(1, 0);
|
2014-06-27 00:42:12 +04:00
|
|
|
|
2016-09-08 19:26:03 +03:00
|
|
|
aSink->LineTo(transform.TransformPoint(currentStartOffset));
|
2014-06-27 00:42:12 +04:00
|
|
|
|
2015-07-27 19:12:22 +03:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-06-27 00:42:12 +04:00
|
|
|
// cos(x+pi/2) == -sin(x)
|
|
|
|
// sin(x+pi/2) == cos(x)
|
2015-07-27 19:12:22 +03:00
|
|
|
Point currentEndOffset(-currentStartOffset.y, currentStartOffset.x);
|
|
|
|
|
2016-03-19 07:29:13 +03:00
|
|
|
PartialArcToBezier(aSink, currentStartOffset, currentEndOffset, transform);
|
2015-07-27 19:12:22 +03:00
|
|
|
|
|
|
|
// We guarantee here the current point is the start point of the next
|
|
|
|
// curve segment.
|
|
|
|
currentStartOffset = currentEndOffset;
|
2014-06-27 00:42:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 12:34:10 +03:00
|
|
|
/**
|
|
|
|
* Appends a path represending a rectangle to the path being built by
|
|
|
|
* aPathBuilder.
|
|
|
|
*
|
|
|
|
* aRect The rectangle to append.
|
|
|
|
* aDrawClockwise If set to true, the path will start at the left of the top
|
|
|
|
* left edge and draw clockwise. If set to false the path will
|
|
|
|
* start at the right of the top left edge and draw counter-
|
|
|
|
* clockwise.
|
|
|
|
*/
|
|
|
|
GFX2D_API void AppendRectToPath(PathBuilder* aPathBuilder,
|
|
|
|
const Rect& aRect,
|
|
|
|
bool aDrawClockwise = true);
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
inline already_AddRefed<Path> MakePathForRect(const DrawTarget& aDrawTarget,
|
2014-10-30 12:34:10 +03:00
|
|
|
const Rect& aRect,
|
|
|
|
bool aDrawClockwise = true)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> builder = aDrawTarget.CreatePathBuilder();
|
2014-10-30 12:34:10 +03:00
|
|
|
AppendRectToPath(builder, aRect, aDrawClockwise);
|
|
|
|
return builder->Finish();
|
|
|
|
}
|
|
|
|
|
2014-10-30 12:34:09 +03:00
|
|
|
struct RectCornerRadii {
|
2017-01-05 12:07:07 +03:00
|
|
|
Size radii[eCornerCount];
|
2014-10-30 12:34:09 +03:00
|
|
|
|
|
|
|
RectCornerRadii() {}
|
|
|
|
|
|
|
|
explicit RectCornerRadii(Float radius) {
|
2017-01-05 12:07:07 +03:00
|
|
|
NS_FOR_CSS_FULL_CORNERS(i) {
|
2014-10-30 12:34:09 +03:00
|
|
|
radii[i].SizeTo(radius, radius);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit RectCornerRadii(Float radiusX, Float radiusY) {
|
2017-01-05 12:07:07 +03:00
|
|
|
NS_FOR_CSS_FULL_CORNERS(i) {
|
2014-10-30 12:34:09 +03:00
|
|
|
radii[i].SizeTo(radiusX, radiusY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RectCornerRadii(Float tl, Float tr, Float br, Float bl) {
|
2017-01-05 12:07:07 +03:00
|
|
|
radii[eCornerTopLeft].SizeTo(tl, tl);
|
|
|
|
radii[eCornerTopRight].SizeTo(tr, tr);
|
|
|
|
radii[eCornerBottomRight].SizeTo(br, br);
|
|
|
|
radii[eCornerBottomLeft].SizeTo(bl, bl);
|
2014-10-30 12:34:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RectCornerRadii(const Size& tl, const Size& tr,
|
|
|
|
const Size& br, const Size& bl) {
|
2017-01-05 12:07:07 +03:00
|
|
|
radii[eCornerTopLeft] = tl;
|
|
|
|
radii[eCornerTopRight] = tr;
|
|
|
|
radii[eCornerBottomRight] = br;
|
|
|
|
radii[eCornerBottomLeft] = bl;
|
2014-10-30 12:34:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const Size& operator[](size_t aCorner) const {
|
|
|
|
return radii[aCorner];
|
|
|
|
}
|
|
|
|
|
|
|
|
Size& operator[](size_t aCorner) {
|
|
|
|
return radii[aCorner];
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:04:21 +03:00
|
|
|
bool operator==(const RectCornerRadii& aOther) const {
|
2017-01-05 12:07:07 +03:00
|
|
|
return TopLeft() == aOther.TopLeft() &&
|
|
|
|
TopRight() == aOther.TopRight() &&
|
|
|
|
BottomRight() == aOther.BottomRight() &&
|
|
|
|
BottomLeft() == aOther.BottomLeft();
|
2015-06-25 22:04:21 +03:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
bool AreRadiiSame() const {
|
2017-01-05 12:07:07 +03:00
|
|
|
return TopLeft() == TopRight() &&
|
|
|
|
TopLeft() == BottomRight() &&
|
|
|
|
TopLeft() == BottomLeft();
|
2016-11-21 21:16:59 +03:00
|
|
|
}
|
|
|
|
|
2014-10-30 12:34:09 +03:00
|
|
|
void Scale(Float aXScale, Float aYScale) {
|
2017-01-05 12:07:07 +03:00
|
|
|
NS_FOR_CSS_FULL_CORNERS(i) {
|
2014-10-30 12:34:09 +03:00
|
|
|
radii[i].Scale(aXScale, aYScale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 12:07:07 +03:00
|
|
|
const Size TopLeft() const { return radii[eCornerTopLeft]; }
|
|
|
|
Size& TopLeft() { return radii[eCornerTopLeft]; }
|
2014-10-30 12:34:09 +03:00
|
|
|
|
2017-01-05 12:07:07 +03:00
|
|
|
const Size TopRight() const { return radii[eCornerTopRight]; }
|
|
|
|
Size& TopRight() { return radii[eCornerTopRight]; }
|
2014-10-30 12:34:09 +03:00
|
|
|
|
2017-01-05 12:07:07 +03:00
|
|
|
const Size BottomRight() const { return radii[eCornerBottomRight]; }
|
|
|
|
Size& BottomRight() { return radii[eCornerBottomRight]; }
|
2014-10-30 12:34:09 +03:00
|
|
|
|
2017-01-05 12:07:07 +03:00
|
|
|
const Size BottomLeft() const { return radii[eCornerBottomLeft]; }
|
|
|
|
Size& BottomLeft() { return radii[eCornerBottomLeft]; }
|
2017-02-24 09:30:19 +03:00
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
return TopLeft().IsEmpty() && TopRight().IsEmpty() &&
|
|
|
|
BottomRight().IsEmpty() && BottomLeft().IsEmpty();
|
|
|
|
}
|
2014-10-30 12:34:09 +03:00
|
|
|
};
|
|
|
|
|
2013-11-01 17:29:44 +04:00
|
|
|
/**
|
|
|
|
* Appends a path represending a rounded rectangle to the path being built by
|
|
|
|
* aPathBuilder.
|
|
|
|
*
|
|
|
|
* aRect The rectangle to append.
|
|
|
|
* aCornerRadii Contains the radii of the top-left, top-right, bottom-right
|
|
|
|
* and bottom-left corners, in that order.
|
|
|
|
* aDrawClockwise If set to true, the path will start at the left of the top
|
|
|
|
* left edge and draw clockwise. If set to false the path will
|
|
|
|
* start at the right of the top left edge and draw counter-
|
|
|
|
* clockwise.
|
|
|
|
*/
|
|
|
|
GFX2D_API void AppendRoundedRectToPath(PathBuilder* aPathBuilder,
|
|
|
|
const Rect& aRect,
|
2014-10-30 12:34:09 +03:00
|
|
|
const RectCornerRadii& aRadii,
|
2013-11-01 17:29:44 +04:00
|
|
|
bool aDrawClockwise = true);
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
inline already_AddRefed<Path> MakePathForRoundedRect(const DrawTarget& aDrawTarget,
|
2014-10-24 11:26:28 +04:00
|
|
|
const Rect& aRect,
|
2014-10-30 12:34:09 +03:00
|
|
|
const RectCornerRadii& aRadii,
|
2014-10-24 11:26:28 +04:00
|
|
|
bool aDrawClockwise = true)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> builder = aDrawTarget.CreatePathBuilder();
|
2014-10-30 12:34:09 +03:00
|
|
|
AppendRoundedRectToPath(builder, aRect, aRadii, aDrawClockwise);
|
2014-10-24 11:26:28 +04:00
|
|
|
return builder->Finish();
|
|
|
|
}
|
|
|
|
|
2013-11-01 17:30:00 +04:00
|
|
|
/**
|
|
|
|
* Appends a path represending an ellipse to the path being built by
|
|
|
|
* aPathBuilder.
|
|
|
|
*
|
|
|
|
* The ellipse extends aDimensions.width / 2.0 in the horizontal direction
|
|
|
|
* from aCenter, and aDimensions.height / 2.0 in the vertical direction.
|
|
|
|
*/
|
|
|
|
GFX2D_API void AppendEllipseToPath(PathBuilder* aPathBuilder,
|
|
|
|
const Point& aCenter,
|
|
|
|
const Size& aDimensions);
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
inline already_AddRefed<Path> MakePathForEllipse(const DrawTarget& aDrawTarget,
|
2014-10-24 11:26:28 +04:00
|
|
|
const Point& aCenter,
|
|
|
|
const Size& aDimensions)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> builder = aDrawTarget.CreatePathBuilder();
|
2014-10-24 11:26:28 +04:00
|
|
|
AppendEllipseToPath(builder, aCenter, aDimensions);
|
|
|
|
return builder->Finish();
|
|
|
|
}
|
|
|
|
|
2014-10-16 13:51:14 +04:00
|
|
|
/**
|
|
|
|
* If aDrawTarget's transform only contains a translation, and if this line is
|
|
|
|
* a horizontal or vertical line, this function will snap the line's vertices
|
|
|
|
* to align with the device pixel grid so that stroking the line with a one
|
|
|
|
* pixel wide stroke will result in a crisp line that is not antialiased over
|
|
|
|
* two pixels across its width.
|
|
|
|
*
|
|
|
|
* @return Returns true if this function snaps aRect's vertices, else returns
|
|
|
|
* false.
|
|
|
|
*/
|
|
|
|
GFX2D_API bool SnapLineToDevicePixelsForStroking(Point& aP1, Point& aP2,
|
2015-07-07 21:56:23 +03:00
|
|
|
const DrawTarget& aDrawTarget,
|
|
|
|
Float aLineWidth);
|
2014-10-16 13:51:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function paints each edge of aRect separately, snapping the edges using
|
|
|
|
* SnapLineToDevicePixelsForStroking. Stroking the edges as separate paths
|
|
|
|
* helps ensure not only that the stroke spans a single row of device pixels if
|
|
|
|
* possible, but also that the ends of stroke dashes start and end on device
|
|
|
|
* pixels too.
|
|
|
|
*/
|
|
|
|
GFX2D_API void StrokeSnappedEdgesOfRect(const Rect& aRect,
|
|
|
|
DrawTarget& aDrawTarget,
|
|
|
|
const ColorPattern& aColor,
|
|
|
|
const StrokeOptions& aStrokeOptions);
|
|
|
|
|
2015-02-24 00:20:35 +03:00
|
|
|
/**
|
|
|
|
* Return the margin, in device space, by which a stroke can extend beyond the
|
|
|
|
* rendered shape.
|
|
|
|
* @param aStrokeOptions The stroke options that the stroke is drawn with.
|
|
|
|
* @param aTransform The user space to device space transform.
|
|
|
|
* @return The stroke margin.
|
|
|
|
*/
|
|
|
|
GFX2D_API Margin MaxStrokeExtents(const StrokeOptions& aStrokeOptions,
|
|
|
|
const Matrix& aTransform);
|
|
|
|
|
2014-10-19 13:22:47 +04:00
|
|
|
extern UserDataKey sDisablePixelSnapping;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If aDrawTarget's transform only contains a translation or, if
|
|
|
|
* aAllowScaleOr90DegreeRotate is true, and/or a scale/90 degree rotation, this
|
|
|
|
* function will convert aRect to device space and snap it to device pixels.
|
|
|
|
* This function returns true if aRect is modified, otherwise it returns false.
|
|
|
|
*
|
|
|
|
* Note that the snapping is such that filling the rect using a DrawTarget
|
|
|
|
* which has the identity matrix as its transform will result in crisp edges.
|
|
|
|
* (That is, aRect will have integer values, aligning its edges between pixel
|
|
|
|
* boundaries.) If on the other hand you stroking the rect with an odd valued
|
|
|
|
* stroke width then the edges of the stroke will be antialiased (assuming an
|
|
|
|
* AntialiasMode that does antialiasing).
|
2015-10-17 20:18:00 +03:00
|
|
|
*
|
|
|
|
* Empty snaps are those which result in a rectangle of 0 area. If they are
|
|
|
|
* disallowed, an axis is left unsnapped if the rounding process results in a
|
|
|
|
* length of 0.
|
2014-10-19 13:22:47 +04:00
|
|
|
*/
|
2014-10-19 13:22:47 +04:00
|
|
|
inline bool UserToDevicePixelSnapped(Rect& aRect, const DrawTarget& aDrawTarget,
|
2015-10-17 20:18:00 +03:00
|
|
|
bool aAllowScaleOr90DegreeRotate = false,
|
|
|
|
bool aAllowEmptySnaps = true)
|
2014-04-01 08:02:10 +04:00
|
|
|
{
|
2014-10-19 13:22:47 +04:00
|
|
|
if (aDrawTarget.GetUserData(&sDisablePixelSnapping)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix mat = aDrawTarget.GetTransform();
|
2014-10-19 13:22:47 +04:00
|
|
|
|
|
|
|
const Float epsilon = 0.0000001f;
|
|
|
|
#define WITHIN_E(a,b) (fabs((a)-(b)) < epsilon)
|
|
|
|
if (!aAllowScaleOr90DegreeRotate &&
|
|
|
|
(!WITHIN_E(mat._11, 1.f) || !WITHIN_E(mat._22, 1.f) ||
|
|
|
|
!WITHIN_E(mat._12, 0.f) || !WITHIN_E(mat._21, 0.f))) {
|
|
|
|
// We have non-translation, but only translation is allowed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#undef WITHIN_E
|
|
|
|
|
2016-09-08 19:26:03 +03:00
|
|
|
Point p1 = mat.TransformPoint(aRect.TopLeft());
|
|
|
|
Point p2 = mat.TransformPoint(aRect.TopRight());
|
|
|
|
Point p3 = mat.TransformPoint(aRect.BottomRight());
|
2014-04-01 08:02:10 +04:00
|
|
|
|
|
|
|
// Check that the rectangle is axis-aligned. For an axis-aligned rectangle,
|
|
|
|
// two opposite corners define the entire rectangle. So check if
|
|
|
|
// the axis-aligned rectangle with opposite corners p1 and p3
|
|
|
|
// define an axis-aligned rectangle whose other corners are p2 and p4.
|
|
|
|
// We actually only need to check one of p2 and p4, since an affine
|
|
|
|
// transform maps parallelograms to parallelograms.
|
|
|
|
if (p2 == Point(p1.x, p3.y) || p2 == Point(p3.x, p1.y)) {
|
2015-10-17 20:18:00 +03:00
|
|
|
Point p1r = p1;
|
|
|
|
Point p3r = p3;
|
|
|
|
p1r.Round();
|
|
|
|
p3r.Round();
|
|
|
|
if (aAllowEmptySnaps || p1r.x != p3r.x) {
|
|
|
|
p1.x = p1r.x;
|
|
|
|
p3.x = p3r.x;
|
|
|
|
}
|
|
|
|
if (aAllowEmptySnaps || p1r.y != p3r.y) {
|
|
|
|
p1.y = p1r.y;
|
|
|
|
p3.y = p3r.y;
|
|
|
|
}
|
2014-04-01 08:02:10 +04:00
|
|
|
|
|
|
|
aRect.MoveTo(Point(std::min(p1.x, p3.x), std::min(p1.y, p3.y)));
|
|
|
|
aRect.SizeTo(Size(std::max(p1.x, p3.x) - aRect.X(),
|
|
|
|
std::max(p1.y, p3.y) - aRect.Y()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-19 13:22:47 +04:00
|
|
|
/**
|
|
|
|
* This function has the same behavior as UserToDevicePixelSnapped except that
|
|
|
|
* aRect is not transformed to device space.
|
|
|
|
*/
|
2015-05-06 16:43:56 +03:00
|
|
|
inline bool MaybeSnapToDevicePixels(Rect& aRect, const DrawTarget& aDrawTarget,
|
2015-10-17 20:18:00 +03:00
|
|
|
bool aAllowScaleOr90DegreeRotate = false,
|
|
|
|
bool aAllowEmptySnaps = true)
|
2014-10-19 13:22:47 +04:00
|
|
|
{
|
2015-05-06 16:40:26 +03:00
|
|
|
if (UserToDevicePixelSnapped(aRect, aDrawTarget,
|
2015-10-17 20:18:00 +03:00
|
|
|
aAllowScaleOr90DegreeRotate, aAllowEmptySnaps)) {
|
2014-10-19 13:22:47 +04:00
|
|
|
// Since UserToDevicePixelSnapped returned true we know there is no
|
|
|
|
// rotation/skew in 'mat', so we can just use TransformBounds() here.
|
|
|
|
Matrix mat = aDrawTarget.GetTransform();
|
|
|
|
mat.Invert();
|
|
|
|
aRect = mat.TransformBounds(aRect);
|
2015-05-06 16:43:56 +03:00
|
|
|
return true;
|
2014-10-19 13:22:47 +04:00
|
|
|
}
|
2015-05-06 16:43:56 +03:00
|
|
|
return false;
|
2014-10-19 13:22:47 +04:00
|
|
|
}
|
|
|
|
|
2013-11-01 17:29:44 +04:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_PATHHELPERS_H_ */
|