File: SVGArcConverter.cpp

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (127 lines) | stat: -rw-r--r-- 4,172 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SVGArcConverter.h"

using namespace mozilla::gfx;

namespace mozilla {

//-----------------------------------------------------------------------

static double CalcVectorAngle(double ux, double uy, double vx, double vy) {
  double ta = atan2(uy, ux);
  double tb = atan2(vy, vx);
  if (tb >= ta) return tb - ta;
  return 2 * M_PI - (ta - tb);
}

SVGArcConverter::SVGArcConverter(const Point& from, const Point& to,
                                 const Point& radii, double angle,
                                 bool largeArcFlag, bool sweepFlag) {
  MOZ_ASSERT(radii.x != 0.0f && radii.y != 0.0f, "Bad radii");

  const double radPerDeg = M_PI / 180.0;
  mSegIndex = 0;

  if (from == to) {
    mNumSegs = 0;
    return;
  }

  // Convert to center parameterization as shown in
  // http://www.w3.org/TR/SVG/implnote.html
  mRx = fabs(radii.x);
  mRy = fabs(radii.y);

  mSinPhi = sin(angle * radPerDeg);
  mCosPhi = cos(angle * radPerDeg);

  double x1dash =
      mCosPhi * (from.x - to.x) / 2.0 + mSinPhi * (from.y - to.y) / 2.0;
  double y1dash =
      -mSinPhi * (from.x - to.x) / 2.0 + mCosPhi * (from.y - to.y) / 2.0;

  double root;
  double numerator = mRx * mRx * mRy * mRy - mRx * mRx * y1dash * y1dash -
                     mRy * mRy * x1dash * x1dash;

  if (numerator < 0.0) {
    //  If mRx , mRy and are such that there is no solution (basically,
    //  the ellipse is not big enough to reach from 'from' to 'to'
    //  then the ellipse is scaled up uniformly until there is
    //  exactly one solution (until the ellipse is just big enough).

    // -> find factor s, such that numerator' with mRx'=s*mRx and
    //    mRy'=s*mRy becomes 0 :
    double s = sqrt(1.0 - numerator / (mRx * mRx * mRy * mRy));

    mRx *= s;
    mRy *= s;
    root = 0.0;

  } else {
    root = (largeArcFlag == sweepFlag ? -1.0 : 1.0) *
           sqrt(numerator /
                (mRx * mRx * y1dash * y1dash + mRy * mRy * x1dash * x1dash));
  }

  double cxdash = root * mRx * y1dash / mRy;
  double cydash = -root * mRy * x1dash / mRx;

  mC.x = mCosPhi * cxdash - mSinPhi * cydash + (from.x + to.x) / 2.0;
  mC.y = mSinPhi * cxdash + mCosPhi * cydash + (from.y + to.y) / 2.0;
  mTheta = CalcVectorAngle(1.0, 0.0, (x1dash - cxdash) / mRx,
                           (y1dash - cydash) / mRy);
  double dtheta =
      CalcVectorAngle((x1dash - cxdash) / mRx, (y1dash - cydash) / mRy,
                      (-x1dash - cxdash) / mRx, (-y1dash - cydash) / mRy);
  if (!sweepFlag && dtheta > 0)
    dtheta -= 2.0 * M_PI;
  else if (sweepFlag && dtheta < 0)
    dtheta += 2.0 * M_PI;

  // Convert into cubic bezier segments <= 90deg
  mNumSegs = static_cast<int>(ceil(fabs(dtheta / (M_PI / 2.0))));
  mDelta = dtheta / mNumSegs;
  mT = 8.0 / 3.0 * sin(mDelta / 4.0) * sin(mDelta / 4.0) / sin(mDelta / 2.0);

  mFrom = from;
}

bool SVGArcConverter::GetNextSegment(Point* cp1, Point* cp2, Point* to) {
  if (mSegIndex == mNumSegs) {
    return false;
  }

  double cosTheta1 = cos(mTheta);
  double sinTheta1 = sin(mTheta);
  double theta2 = mTheta + mDelta;
  double cosTheta2 = cos(theta2);
  double sinTheta2 = sin(theta2);

  // a) calculate endpoint of the segment:
  to->x = mCosPhi * mRx * cosTheta2 - mSinPhi * mRy * sinTheta2 + mC.x;
  to->y = mSinPhi * mRx * cosTheta2 + mCosPhi * mRy * sinTheta2 + mC.y;

  // b) calculate gradients at start/end points of segment:
  cp1->x =
      mFrom.x + mT * (-mCosPhi * mRx * sinTheta1 - mSinPhi * mRy * cosTheta1);
  cp1->y =
      mFrom.y + mT * (-mSinPhi * mRx * sinTheta1 + mCosPhi * mRy * cosTheta1);

  cp2->x = to->x + mT * (mCosPhi * mRx * sinTheta2 + mSinPhi * mRy * cosTheta2);
  cp2->y = to->y + mT * (mSinPhi * mRx * sinTheta2 - mCosPhi * mRy * cosTheta2);

  // do next segment
  mTheta = theta2;
  mFrom = *to;
  ++mSegIndex;

  return true;
}

}  // namespace mozilla