gfx/2d/PathHelpers.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef MOZILLA_GFX_PATHHELPERS_H_
michael@0 7 #define MOZILLA_GFX_PATHHELPERS_H_
michael@0 8
michael@0 9 #include "2D.h"
michael@0 10 #include "mozilla/Constants.h"
michael@0 11
michael@0 12 namespace mozilla {
michael@0 13 namespace gfx {
michael@0 14
michael@0 15 template <typename T>
michael@0 16 void ArcToBezier(T* aSink, const Point &aOrigin, const Size &aRadius,
michael@0 17 float aStartAngle, float aEndAngle, bool aAntiClockwise)
michael@0 18 {
michael@0 19 Point startPoint(aOrigin.x + cos(aStartAngle) * aRadius.width,
michael@0 20 aOrigin.y + sin(aStartAngle) * aRadius.height);
michael@0 21
michael@0 22 aSink->LineTo(startPoint);
michael@0 23
michael@0 24 // Clockwise we always sweep from the smaller to the larger angle, ccw
michael@0 25 // it's vice versa.
michael@0 26 if (!aAntiClockwise && (aEndAngle < aStartAngle)) {
michael@0 27 Float correction = Float(ceil((aStartAngle - aEndAngle) / (2.0f * M_PI)));
michael@0 28 aEndAngle += float(correction * 2.0f * M_PI);
michael@0 29 } else if (aAntiClockwise && (aStartAngle < aEndAngle)) {
michael@0 30 Float correction = (Float)ceil((aEndAngle - aStartAngle) / (2.0f * M_PI));
michael@0 31 aStartAngle += float(correction * 2.0f * M_PI);
michael@0 32 }
michael@0 33
michael@0 34 // Sweeping more than 2 * pi is a full circle.
michael@0 35 if (!aAntiClockwise && (aEndAngle - aStartAngle > 2 * M_PI)) {
michael@0 36 aEndAngle = float(aStartAngle + 2.0f * M_PI);
michael@0 37 } else if (aAntiClockwise && (aStartAngle - aEndAngle > 2.0f * M_PI)) {
michael@0 38 aEndAngle = float(aStartAngle - 2.0f * M_PI);
michael@0 39 }
michael@0 40
michael@0 41 // Calculate the total arc we're going to sweep.
michael@0 42 Float arcSweepLeft = fabs(aEndAngle - aStartAngle);
michael@0 43
michael@0 44 Float sweepDirection = aAntiClockwise ? -1.0f : 1.0f;
michael@0 45
michael@0 46 Float currentStartAngle = aStartAngle;
michael@0 47
michael@0 48 while (arcSweepLeft > 0) {
michael@0 49 // We guarantee here the current point is the start point of the next
michael@0 50 // curve segment.
michael@0 51 Float currentEndAngle;
michael@0 52
michael@0 53 if (arcSweepLeft > M_PI / 2.0f) {
michael@0 54 currentEndAngle = Float(currentStartAngle + M_PI / 2.0f * sweepDirection);
michael@0 55 } else {
michael@0 56 currentEndAngle = currentStartAngle + arcSweepLeft * sweepDirection;
michael@0 57 }
michael@0 58
michael@0 59 Point currentStartPoint(aOrigin.x + cos(currentStartAngle) * aRadius.width,
michael@0 60 aOrigin.y + sin(currentStartAngle) * aRadius.height);
michael@0 61 Point currentEndPoint(aOrigin.x + cos(currentEndAngle) * aRadius.width,
michael@0 62 aOrigin.y + sin(currentEndAngle) * aRadius.height);
michael@0 63
michael@0 64 // Calculate kappa constant for partial curve. The sign of angle in the
michael@0 65 // tangent will actually ensure this is negative for a counter clockwise
michael@0 66 // sweep, so changing signs later isn't needed.
michael@0 67 Float kappaFactor = (4.0f / 3.0f) * tan((currentEndAngle - currentStartAngle) / 4.0f);
michael@0 68 Float kappaX = kappaFactor * aRadius.width;
michael@0 69 Float kappaY = kappaFactor * aRadius.height;
michael@0 70
michael@0 71 Point tangentStart(-sin(currentStartAngle), cos(currentStartAngle));
michael@0 72 Point cp1 = currentStartPoint;
michael@0 73 cp1 += Point(tangentStart.x * kappaX, tangentStart.y * kappaY);
michael@0 74
michael@0 75 Point revTangentEnd(sin(currentEndAngle), -cos(currentEndAngle));
michael@0 76 Point cp2 = currentEndPoint;
michael@0 77 cp2 += Point(revTangentEnd.x * kappaX, revTangentEnd.y * kappaY);
michael@0 78
michael@0 79 aSink->BezierTo(cp1, cp2, currentEndPoint);
michael@0 80
michael@0 81 arcSweepLeft -= Float(M_PI / 2.0f);
michael@0 82 currentStartAngle = currentEndAngle;
michael@0 83 }
michael@0 84 }
michael@0 85
michael@0 86 /**
michael@0 87 * Appends a path represending a rounded rectangle to the path being built by
michael@0 88 * aPathBuilder.
michael@0 89 *
michael@0 90 * aRect The rectangle to append.
michael@0 91 * aCornerRadii Contains the radii of the top-left, top-right, bottom-right
michael@0 92 * and bottom-left corners, in that order.
michael@0 93 * aDrawClockwise If set to true, the path will start at the left of the top
michael@0 94 * left edge and draw clockwise. If set to false the path will
michael@0 95 * start at the right of the top left edge and draw counter-
michael@0 96 * clockwise.
michael@0 97 */
michael@0 98 GFX2D_API void AppendRoundedRectToPath(PathBuilder* aPathBuilder,
michael@0 99 const Rect& aRect,
michael@0 100 const Size(& aCornerRadii)[4],
michael@0 101 bool aDrawClockwise = true);
michael@0 102
michael@0 103 /**
michael@0 104 * Appends a path represending an ellipse to the path being built by
michael@0 105 * aPathBuilder.
michael@0 106 *
michael@0 107 * The ellipse extends aDimensions.width / 2.0 in the horizontal direction
michael@0 108 * from aCenter, and aDimensions.height / 2.0 in the vertical direction.
michael@0 109 */
michael@0 110 GFX2D_API void AppendEllipseToPath(PathBuilder* aPathBuilder,
michael@0 111 const Point& aCenter,
michael@0 112 const Size& aDimensions);
michael@0 113
michael@0 114 static inline bool
michael@0 115 UserToDevicePixelSnapped(Rect& aRect, const Matrix& aTransform)
michael@0 116 {
michael@0 117 Point p1 = aTransform * aRect.TopLeft();
michael@0 118 Point p2 = aTransform * aRect.TopRight();
michael@0 119 Point p3 = aTransform * aRect.BottomRight();
michael@0 120
michael@0 121 // Check that the rectangle is axis-aligned. For an axis-aligned rectangle,
michael@0 122 // two opposite corners define the entire rectangle. So check if
michael@0 123 // the axis-aligned rectangle with opposite corners p1 and p3
michael@0 124 // define an axis-aligned rectangle whose other corners are p2 and p4.
michael@0 125 // We actually only need to check one of p2 and p4, since an affine
michael@0 126 // transform maps parallelograms to parallelograms.
michael@0 127 if (p2 == Point(p1.x, p3.y) || p2 == Point(p3.x, p1.y)) {
michael@0 128 p1.Round();
michael@0 129 p3.Round();
michael@0 130
michael@0 131 aRect.MoveTo(Point(std::min(p1.x, p3.x), std::min(p1.y, p3.y)));
michael@0 132 aRect.SizeTo(Size(std::max(p1.x, p3.x) - aRect.X(),
michael@0 133 std::max(p1.y, p3.y) - aRect.Y()));
michael@0 134 return true;
michael@0 135 }
michael@0 136
michael@0 137 return false;
michael@0 138 }
michael@0 139
michael@0 140 } // namespace gfx
michael@0 141 } // namespace mozilla
michael@0 142
michael@0 143 #endif /* MOZILLA_GFX_PATHHELPERS_H_ */

mercurial