1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Matt Woodrow <mwoodrow@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef MOZILLA_GFX_PATHHELPERS_H_
39 : #define MOZILLA_GFX_PATHHELPERS_H_
40 :
41 : #include "2D.h"
42 :
43 : #ifndef M_PI
44 : #define M_PI 3.14159265358979323846
45 : #endif
46 :
47 : namespace mozilla {
48 : namespace gfx {
49 :
50 : template <typename T>
51 0 : void ArcToBezier(T* aSink, const Point &aOrigin, float aRadius, float aStartAngle,
52 : float aEndAngle, bool aAntiClockwise)
53 : {
54 : Point startPoint(aOrigin.x + cos(aStartAngle) * aRadius,
55 0 : aOrigin.y + sin(aStartAngle) * aRadius);
56 :
57 0 : aSink->LineTo(startPoint);
58 :
59 : // Clockwise we always sweep from the smaller to the larger angle, ccw
60 : // it's vice versa.
61 0 : if (!aAntiClockwise && (aEndAngle < aStartAngle)) {
62 0 : Float correction = ceil((aStartAngle - aEndAngle) / (2.0f * M_PI));
63 0 : aEndAngle += correction * 2.0f * M_PI;
64 0 : } else if (aAntiClockwise && (aStartAngle < aEndAngle)) {
65 0 : Float correction = ceil((aEndAngle - aStartAngle) / (2.0f * M_PI));
66 0 : aStartAngle += correction * 2.0f * M_PI;
67 : }
68 :
69 : // Sweeping more than 2 * pi is a full circle.
70 0 : if (!aAntiClockwise && (aEndAngle - aStartAngle > 2 * M_PI)) {
71 0 : aEndAngle = aStartAngle + 2.0f * M_PI;
72 0 : } else if (aAntiClockwise && (aStartAngle - aEndAngle > 2.0f * M_PI)) {
73 0 : aEndAngle = aStartAngle - 2.0f * M_PI;
74 : }
75 :
76 : // Calculate the total arc we're going to sweep.
77 0 : Float arcSweepLeft = fabs(aEndAngle - aStartAngle);
78 :
79 0 : Float sweepDirection = aAntiClockwise ? -1.0f : 1.0f;
80 :
81 0 : Float currentStartAngle = aStartAngle;
82 :
83 0 : while (arcSweepLeft > 0) {
84 : // We guarantee here the current point is the start point of the next
85 : // curve segment.
86 : Float currentEndAngle;
87 :
88 0 : if (arcSweepLeft > M_PI / 2.0f) {
89 0 : currentEndAngle = currentStartAngle + M_PI / 2.0f * sweepDirection;
90 : } else {
91 0 : currentEndAngle = currentStartAngle + arcSweepLeft * sweepDirection;
92 : }
93 :
94 : Point currentStartPoint(aOrigin.x + cos(currentStartAngle) * aRadius,
95 0 : aOrigin.y + sin(currentStartAngle) * aRadius);
96 : Point currentEndPoint(aOrigin.x + cos(currentEndAngle) * aRadius,
97 0 : aOrigin.y + sin(currentEndAngle) * aRadius);
98 :
99 : // Calculate kappa constant for partial curve. The sign of angle in the
100 : // tangent will actually ensure this is negative for a counter clockwise
101 : // sweep, so changing signs later isn't needed.
102 0 : Float kappa = (4.0f / 3.0f) * tan((currentEndAngle - currentStartAngle) / 4.0f) * aRadius;
103 :
104 0 : Point tangentStart(-sin(currentStartAngle), cos(currentStartAngle));
105 0 : Point cp1 = currentStartPoint;
106 0 : cp1 += tangentStart * kappa;
107 :
108 0 : Point revTangentEnd(sin(currentEndAngle), -cos(currentEndAngle));
109 0 : Point cp2 = currentEndPoint;
110 0 : cp2 += revTangentEnd * kappa;
111 :
112 0 : aSink->BezierTo(cp1, cp2, currentEndPoint);
113 :
114 0 : arcSweepLeft -= M_PI / 2.0f;
115 0 : currentStartAngle = currentEndAngle;
116 : }
117 0 : }
118 :
119 : }
120 : }
121 :
122 : #endif /* MOZILLA_GFX_PATHHELPERS_H_ */
|