Tue, 06 Jan 2015 21:39:09 +0100
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.
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "PathHelpers.h"
8 namespace mozilla {
9 namespace gfx {
11 void
12 AppendRoundedRectToPath(PathBuilder* aPathBuilder,
13 const Rect& aRect,
14 // paren's needed due to operator precedence:
15 const Size(& aCornerRadii)[4],
16 bool aDrawClockwise)
17 {
18 // For CW drawing, this looks like:
19 //
20 // ...******0** 1 C
21 // ****
22 // *** 2
23 // **
24 // *
25 // *
26 // 3
27 // *
28 // *
29 //
30 // Where 0, 1, 2, 3 are the control points of the Bezier curve for
31 // the corner, and C is the actual corner point.
32 //
33 // At the start of the loop, the current point is assumed to be
34 // the point adjacent to the top left corner on the top
35 // horizontal. Note that corner indices start at the top left and
36 // continue clockwise, whereas in our loop i = 0 refers to the top
37 // right corner.
38 //
39 // When going CCW, the control points are swapped, and the first
40 // corner that's drawn is the top left (along with the top segment).
41 //
42 // There is considerable latitude in how one chooses the four
43 // control points for a Bezier curve approximation to an ellipse.
44 // For the overall path to be continuous and show no corner at the
45 // endpoints of the arc, points 0 and 3 must be at the ends of the
46 // straight segments of the rectangle; points 0, 1, and C must be
47 // collinear; and points 3, 2, and C must also be collinear. This
48 // leaves only two free parameters: the ratio of the line segments
49 // 01 and 0C, and the ratio of the line segments 32 and 3C. See
50 // the following papers for extensive discussion of how to choose
51 // these ratios:
52 //
53 // Dokken, Tor, et al. "Good approximation of circles by
54 // curvature-continuous Bezier curves." Computer-Aided
55 // Geometric Design 7(1990) 33--41.
56 // Goldapp, Michael. "Approximation of circular arcs by cubic
57 // polynomials." Computer-Aided Geometric Design 8(1991) 227--238.
58 // Maisonobe, Luc. "Drawing an elliptical arc using polylines,
59 // quadratic, or cubic Bezier curves."
60 // http://www.spaceroots.org/documents/ellipse/elliptical-arc.pdf
61 //
62 // We follow the approach in section 2 of Goldapp (least-error,
63 // Hermite-type approximation) and make both ratios equal to
64 //
65 // 2 2 + n - sqrt(2n + 28)
66 // alpha = - * ---------------------
67 // 3 n - 4
68 //
69 // where n = 3( cbrt(sqrt(2)+1) - cbrt(sqrt(2)-1) ).
70 //
71 // This is the result of Goldapp's equation (10b) when the angle
72 // swept out by the arc is pi/2, and the parameter "a-bar" is the
73 // expression given immediately below equation (21).
74 //
75 // Using this value, the maximum radial error for a circle, as a
76 // fraction of the radius, is on the order of 0.2 x 10^-3.
77 // Neither Dokken nor Goldapp discusses error for a general
78 // ellipse; Maisonobe does, but his choice of control points
79 // follows different constraints, and Goldapp's expression for
80 // 'alpha' gives much smaller radial error, even for very flat
81 // ellipses, than Maisonobe's equivalent.
82 //
83 // For the various corners and for each axis, the sign of this
84 // constant changes, or it might be 0 -- it's multiplied by the
85 // appropriate multiplier from the list before using.
87 const Float alpha = Float(0.55191497064665766025);
89 typedef struct { Float a, b; } twoFloats;
91 twoFloats cwCornerMults[4] = { { -1, 0 }, // cc == clockwise
92 { 0, -1 },
93 { +1, 0 },
94 { 0, +1 } };
95 twoFloats ccwCornerMults[4] = { { +1, 0 }, // ccw == counter-clockwise
96 { 0, -1 },
97 { -1, 0 },
98 { 0, +1 } };
100 twoFloats *cornerMults = aDrawClockwise ? cwCornerMults : ccwCornerMults;
102 Point cornerCoords[] = { aRect.TopLeft(), aRect.TopRight(),
103 aRect.BottomRight(), aRect.BottomLeft() };
105 Point pc, p0, p1, p2, p3;
107 // The indexes of the corners:
108 const int kTopLeft = 0, kTopRight = 1;
110 if (aDrawClockwise) {
111 aPathBuilder->MoveTo(Point(aRect.X() + aCornerRadii[kTopLeft].width,
112 aRect.Y()));
113 } else {
114 aPathBuilder->MoveTo(Point(aRect.X() + aRect.Width() - aCornerRadii[kTopRight].width,
115 aRect.Y()));
116 }
118 for (int i = 0; i < 4; ++i) {
119 // the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw)
120 int c = aDrawClockwise ? ((i+1) % 4) : ((4-i) % 4);
122 // i+2 and i+3 respectively. These are used to index into the corner
123 // multiplier table, and were deduced by calculating out the long form
124 // of each corner and finding a pattern in the signs and values.
125 int i2 = (i+2) % 4;
126 int i3 = (i+3) % 4;
128 pc = cornerCoords[c];
130 if (aCornerRadii[c].width > 0.0 && aCornerRadii[c].height > 0.0) {
131 p0.x = pc.x + cornerMults[i].a * aCornerRadii[c].width;
132 p0.y = pc.y + cornerMults[i].b * aCornerRadii[c].height;
134 p3.x = pc.x + cornerMults[i3].a * aCornerRadii[c].width;
135 p3.y = pc.y + cornerMults[i3].b * aCornerRadii[c].height;
137 p1.x = p0.x + alpha * cornerMults[i2].a * aCornerRadii[c].width;
138 p1.y = p0.y + alpha * cornerMults[i2].b * aCornerRadii[c].height;
140 p2.x = p3.x - alpha * cornerMults[i3].a * aCornerRadii[c].width;
141 p2.y = p3.y - alpha * cornerMults[i3].b * aCornerRadii[c].height;
143 aPathBuilder->LineTo(p0);
144 aPathBuilder->BezierTo(p1, p2, p3);
145 } else {
146 aPathBuilder->LineTo(pc);
147 }
148 }
150 aPathBuilder->Close();
151 }
153 void
154 AppendEllipseToPath(PathBuilder* aPathBuilder,
155 const Point& aCenter,
156 const Size& aDimensions)
157 {
158 Size halfDim = aDimensions / 2.0;
159 Rect rect(aCenter - Point(halfDim.width, halfDim.height), aDimensions);
160 Size radii[] = { halfDim, halfDim, halfDim, halfDim };
162 AppendRoundedRectToPath(aPathBuilder, rect, radii);
163 }
165 } // namespace gfx
166 } // namespace mozilla