gfx/2d/PathHelpers.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/PathHelpers.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,143 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_GFX_PATHHELPERS_H_
    1.10 +#define MOZILLA_GFX_PATHHELPERS_H_
    1.11 +
    1.12 +#include "2D.h"
    1.13 +#include "mozilla/Constants.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +namespace gfx {
    1.17 +
    1.18 +template <typename T>
    1.19 +void ArcToBezier(T* aSink, const Point &aOrigin, const Size &aRadius,
    1.20 +                 float aStartAngle, float aEndAngle, bool aAntiClockwise)
    1.21 +{
    1.22 +  Point startPoint(aOrigin.x + cos(aStartAngle) * aRadius.width,
    1.23 +                   aOrigin.y + sin(aStartAngle) * aRadius.height);
    1.24 +
    1.25 +  aSink->LineTo(startPoint);
    1.26 +
    1.27 +  // Clockwise we always sweep from the smaller to the larger angle, ccw
    1.28 +  // it's vice versa.
    1.29 +  if (!aAntiClockwise && (aEndAngle < aStartAngle)) {
    1.30 +    Float correction = Float(ceil((aStartAngle - aEndAngle) / (2.0f * M_PI)));
    1.31 +    aEndAngle += float(correction * 2.0f * M_PI);
    1.32 +  } else if (aAntiClockwise && (aStartAngle < aEndAngle)) {
    1.33 +    Float correction = (Float)ceil((aEndAngle - aStartAngle) / (2.0f * M_PI));
    1.34 +    aStartAngle += float(correction * 2.0f * M_PI);
    1.35 +  }
    1.36 +
    1.37 +  // Sweeping more than 2 * pi is a full circle.
    1.38 +  if (!aAntiClockwise && (aEndAngle - aStartAngle > 2 * M_PI)) {
    1.39 +    aEndAngle = float(aStartAngle + 2.0f * M_PI);
    1.40 +  } else if (aAntiClockwise && (aStartAngle - aEndAngle > 2.0f * M_PI)) {
    1.41 +    aEndAngle = float(aStartAngle - 2.0f * M_PI);
    1.42 +  }
    1.43 +
    1.44 +  // Calculate the total arc we're going to sweep.
    1.45 +  Float arcSweepLeft = fabs(aEndAngle - aStartAngle);
    1.46 +
    1.47 +  Float sweepDirection = aAntiClockwise ? -1.0f : 1.0f;
    1.48 +
    1.49 +  Float currentStartAngle = aStartAngle;
    1.50 +
    1.51 +  while (arcSweepLeft > 0) {
    1.52 +    // We guarantee here the current point is the start point of the next
    1.53 +    // curve segment.
    1.54 +    Float currentEndAngle;
    1.55 +
    1.56 +    if (arcSweepLeft > M_PI / 2.0f) {
    1.57 +      currentEndAngle = Float(currentStartAngle + M_PI / 2.0f * sweepDirection);
    1.58 +    } else {
    1.59 +      currentEndAngle = currentStartAngle + arcSweepLeft * sweepDirection;
    1.60 +    }
    1.61 +
    1.62 +    Point currentStartPoint(aOrigin.x + cos(currentStartAngle) * aRadius.width,
    1.63 +                            aOrigin.y + sin(currentStartAngle) * aRadius.height);
    1.64 +    Point currentEndPoint(aOrigin.x + cos(currentEndAngle) * aRadius.width,
    1.65 +                          aOrigin.y + sin(currentEndAngle) * aRadius.height);
    1.66 +
    1.67 +    // Calculate kappa constant for partial curve. The sign of angle in the
    1.68 +    // tangent will actually ensure this is negative for a counter clockwise
    1.69 +    // sweep, so changing signs later isn't needed.
    1.70 +    Float kappaFactor = (4.0f / 3.0f) * tan((currentEndAngle - currentStartAngle) / 4.0f);
    1.71 +    Float kappaX = kappaFactor * aRadius.width;
    1.72 +    Float kappaY = kappaFactor * aRadius.height;
    1.73 +
    1.74 +    Point tangentStart(-sin(currentStartAngle), cos(currentStartAngle));
    1.75 +    Point cp1 = currentStartPoint;
    1.76 +    cp1 += Point(tangentStart.x * kappaX, tangentStart.y * kappaY);
    1.77 +
    1.78 +    Point revTangentEnd(sin(currentEndAngle), -cos(currentEndAngle));
    1.79 +    Point cp2 = currentEndPoint;
    1.80 +    cp2 += Point(revTangentEnd.x * kappaX, revTangentEnd.y * kappaY);
    1.81 +
    1.82 +    aSink->BezierTo(cp1, cp2, currentEndPoint);
    1.83 +
    1.84 +    arcSweepLeft -= Float(M_PI / 2.0f);
    1.85 +    currentStartAngle = currentEndAngle;
    1.86 +  }
    1.87 +}
    1.88 +
    1.89 +/**
    1.90 + * Appends a path represending a rounded rectangle to the path being built by
    1.91 + * aPathBuilder.
    1.92 + *
    1.93 + * aRect           The rectangle to append.
    1.94 + * aCornerRadii    Contains the radii of the top-left, top-right, bottom-right
    1.95 + *                 and bottom-left corners, in that order.
    1.96 + * aDrawClockwise  If set to true, the path will start at the left of the top
    1.97 + *                 left edge and draw clockwise. If set to false the path will
    1.98 + *                 start at the right of the top left edge and draw counter-
    1.99 + *                 clockwise.
   1.100 + */
   1.101 +GFX2D_API void AppendRoundedRectToPath(PathBuilder* aPathBuilder,
   1.102 +                                       const Rect& aRect,
   1.103 +                                       const Size(& aCornerRadii)[4],
   1.104 +                                       bool aDrawClockwise = true);
   1.105 +
   1.106 +/**
   1.107 + * Appends a path represending an ellipse to the path being built by
   1.108 + * aPathBuilder.
   1.109 + *
   1.110 + * The ellipse extends aDimensions.width / 2.0 in the horizontal direction
   1.111 + * from aCenter, and aDimensions.height / 2.0 in the vertical direction.
   1.112 + */
   1.113 +GFX2D_API void AppendEllipseToPath(PathBuilder* aPathBuilder,
   1.114 +                                   const Point& aCenter,
   1.115 +                                   const Size& aDimensions);
   1.116 +
   1.117 +static inline bool
   1.118 +UserToDevicePixelSnapped(Rect& aRect, const Matrix& aTransform)
   1.119 +{
   1.120 +  Point p1 = aTransform * aRect.TopLeft();
   1.121 +  Point p2 = aTransform * aRect.TopRight();
   1.122 +  Point p3 = aTransform * aRect.BottomRight();
   1.123 +
   1.124 +  // Check that the rectangle is axis-aligned. For an axis-aligned rectangle,
   1.125 +  // two opposite corners define the entire rectangle. So check if
   1.126 +  // the axis-aligned rectangle with opposite corners p1 and p3
   1.127 +  // define an axis-aligned rectangle whose other corners are p2 and p4.
   1.128 +  // We actually only need to check one of p2 and p4, since an affine
   1.129 +  // transform maps parallelograms to parallelograms.
   1.130 +  if (p2 == Point(p1.x, p3.y) || p2 == Point(p3.x, p1.y)) {
   1.131 +      p1.Round();
   1.132 +      p3.Round();
   1.133 +
   1.134 +      aRect.MoveTo(Point(std::min(p1.x, p3.x), std::min(p1.y, p3.y)));
   1.135 +      aRect.SizeTo(Size(std::max(p1.x, p3.x) - aRect.X(),
   1.136 +                        std::max(p1.y, p3.y) - aRect.Y()));
   1.137 +      return true;
   1.138 +  }
   1.139 +
   1.140 +  return false;
   1.141 +}
   1.142 +
   1.143 +} // namespace gfx
   1.144 +} // namespace mozilla
   1.145 +
   1.146 +#endif /* MOZILLA_GFX_PATHHELPERS_H_ */

mercurial