michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_PATHHELPERS_H_ michael@0: #define MOZILLA_GFX_PATHHELPERS_H_ michael@0: michael@0: #include "2D.h" michael@0: #include "mozilla/Constants.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: template michael@0: void ArcToBezier(T* aSink, const Point &aOrigin, const Size &aRadius, michael@0: float aStartAngle, float aEndAngle, bool aAntiClockwise) michael@0: { michael@0: Point startPoint(aOrigin.x + cos(aStartAngle) * aRadius.width, michael@0: aOrigin.y + sin(aStartAngle) * aRadius.height); michael@0: michael@0: aSink->LineTo(startPoint); michael@0: michael@0: // Clockwise we always sweep from the smaller to the larger angle, ccw michael@0: // it's vice versa. michael@0: if (!aAntiClockwise && (aEndAngle < aStartAngle)) { michael@0: Float correction = Float(ceil((aStartAngle - aEndAngle) / (2.0f * M_PI))); michael@0: aEndAngle += float(correction * 2.0f * M_PI); michael@0: } else if (aAntiClockwise && (aStartAngle < aEndAngle)) { michael@0: Float correction = (Float)ceil((aEndAngle - aStartAngle) / (2.0f * M_PI)); michael@0: aStartAngle += float(correction * 2.0f * M_PI); michael@0: } michael@0: michael@0: // Sweeping more than 2 * pi is a full circle. michael@0: if (!aAntiClockwise && (aEndAngle - aStartAngle > 2 * M_PI)) { michael@0: aEndAngle = float(aStartAngle + 2.0f * M_PI); michael@0: } else if (aAntiClockwise && (aStartAngle - aEndAngle > 2.0f * M_PI)) { michael@0: aEndAngle = float(aStartAngle - 2.0f * M_PI); michael@0: } michael@0: michael@0: // Calculate the total arc we're going to sweep. michael@0: Float arcSweepLeft = fabs(aEndAngle - aStartAngle); michael@0: michael@0: Float sweepDirection = aAntiClockwise ? -1.0f : 1.0f; michael@0: michael@0: Float currentStartAngle = aStartAngle; michael@0: michael@0: while (arcSweepLeft > 0) { michael@0: // We guarantee here the current point is the start point of the next michael@0: // curve segment. michael@0: Float currentEndAngle; michael@0: michael@0: if (arcSweepLeft > M_PI / 2.0f) { michael@0: currentEndAngle = Float(currentStartAngle + M_PI / 2.0f * sweepDirection); michael@0: } else { michael@0: currentEndAngle = currentStartAngle + arcSweepLeft * sweepDirection; michael@0: } michael@0: michael@0: Point currentStartPoint(aOrigin.x + cos(currentStartAngle) * aRadius.width, michael@0: aOrigin.y + sin(currentStartAngle) * aRadius.height); michael@0: Point currentEndPoint(aOrigin.x + cos(currentEndAngle) * aRadius.width, michael@0: aOrigin.y + sin(currentEndAngle) * aRadius.height); michael@0: michael@0: // Calculate kappa constant for partial curve. The sign of angle in the michael@0: // tangent will actually ensure this is negative for a counter clockwise michael@0: // sweep, so changing signs later isn't needed. michael@0: Float kappaFactor = (4.0f / 3.0f) * tan((currentEndAngle - currentStartAngle) / 4.0f); michael@0: Float kappaX = kappaFactor * aRadius.width; michael@0: Float kappaY = kappaFactor * aRadius.height; michael@0: michael@0: Point tangentStart(-sin(currentStartAngle), cos(currentStartAngle)); michael@0: Point cp1 = currentStartPoint; michael@0: cp1 += Point(tangentStart.x * kappaX, tangentStart.y * kappaY); michael@0: michael@0: Point revTangentEnd(sin(currentEndAngle), -cos(currentEndAngle)); michael@0: Point cp2 = currentEndPoint; michael@0: cp2 += Point(revTangentEnd.x * kappaX, revTangentEnd.y * kappaY); michael@0: michael@0: aSink->BezierTo(cp1, cp2, currentEndPoint); michael@0: michael@0: arcSweepLeft -= Float(M_PI / 2.0f); michael@0: currentStartAngle = currentEndAngle; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Appends a path represending a rounded rectangle to the path being built by michael@0: * aPathBuilder. michael@0: * michael@0: * aRect The rectangle to append. michael@0: * aCornerRadii Contains the radii of the top-left, top-right, bottom-right michael@0: * and bottom-left corners, in that order. michael@0: * aDrawClockwise If set to true, the path will start at the left of the top michael@0: * left edge and draw clockwise. If set to false the path will michael@0: * start at the right of the top left edge and draw counter- michael@0: * clockwise. michael@0: */ michael@0: GFX2D_API void AppendRoundedRectToPath(PathBuilder* aPathBuilder, michael@0: const Rect& aRect, michael@0: const Size(& aCornerRadii)[4], michael@0: bool aDrawClockwise = true); michael@0: michael@0: /** michael@0: * Appends a path represending an ellipse to the path being built by michael@0: * aPathBuilder. michael@0: * michael@0: * The ellipse extends aDimensions.width / 2.0 in the horizontal direction michael@0: * from aCenter, and aDimensions.height / 2.0 in the vertical direction. michael@0: */ michael@0: GFX2D_API void AppendEllipseToPath(PathBuilder* aPathBuilder, michael@0: const Point& aCenter, michael@0: const Size& aDimensions); michael@0: michael@0: static inline bool michael@0: UserToDevicePixelSnapped(Rect& aRect, const Matrix& aTransform) michael@0: { michael@0: Point p1 = aTransform * aRect.TopLeft(); michael@0: Point p2 = aTransform * aRect.TopRight(); michael@0: Point p3 = aTransform * aRect.BottomRight(); michael@0: michael@0: // Check that the rectangle is axis-aligned. For an axis-aligned rectangle, michael@0: // two opposite corners define the entire rectangle. So check if michael@0: // the axis-aligned rectangle with opposite corners p1 and p3 michael@0: // define an axis-aligned rectangle whose other corners are p2 and p4. michael@0: // We actually only need to check one of p2 and p4, since an affine michael@0: // transform maps parallelograms to parallelograms. michael@0: if (p2 == Point(p1.x, p3.y) || p2 == Point(p3.x, p1.y)) { michael@0: p1.Round(); michael@0: p3.Round(); michael@0: michael@0: aRect.MoveTo(Point(std::min(p1.x, p3.x), std::min(p1.y, p3.y))); michael@0: aRect.SizeTo(Size(std::max(p1.x, p3.x) - aRect.X(), michael@0: std::max(p1.y, p3.y) - aRect.Y())); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: } // namespace gfx michael@0: } // namespace mozilla michael@0: michael@0: #endif /* MOZILLA_GFX_PATHHELPERS_H_ */