tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SVGArcConverter.cpp (5294B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "SVGArcConverter.h"
      8 
      9 using namespace mozilla::gfx;
     10 
     11 namespace mozilla {
     12 
     13 //-----------------------------------------------------------------------
     14 
     15 static double CalcVectorAngle(double ux, double uy, double vx, double vy) {
     16  double ta = atan2(uy, ux);
     17  double tb = atan2(vy, vx);
     18  if (tb >= ta) return tb - ta;
     19  return 2 * M_PI - (ta - tb);
     20 }
     21 
     22 SVGArcConverter::SVGArcConverter(const Point& from, const Point& to,
     23                                 const Point& radii, double angle,
     24                                 bool largeArcFlag, bool sweepFlag) {
     25  MOZ_ASSERT(radii.x != 0.0f && radii.y != 0.0f, "Bad radii");
     26 
     27  const double radPerDeg = M_PI / 180.0;
     28  mTo = to;
     29 
     30  if (from == to) {
     31    mNumSegs = 0;
     32    return;
     33  }
     34 
     35  // Convert to center parameterization as shown in
     36  // http://www.w3.org/TR/SVG/implnote.html
     37  mRx = fabs(radii.x);
     38  mRy = fabs(radii.y);
     39 
     40  mSinPhi = sin(angle * radPerDeg);
     41  mCosPhi = cos(angle * radPerDeg);
     42 
     43  double x1dash =
     44      mCosPhi * (from.x - to.x) / 2.0 + mSinPhi * (from.y - to.y) / 2.0;
     45  double y1dash =
     46      -mSinPhi * (from.x - to.x) / 2.0 + mCosPhi * (from.y - to.y) / 2.0;
     47 
     48  double root;
     49  double numerator = mRx * mRx * mRy * mRy - mRx * mRx * y1dash * y1dash -
     50                     mRy * mRy * x1dash * x1dash;
     51 
     52  if (numerator < 0.0) {
     53    //  If mRx , mRy and are such that there is no solution (basically,
     54    //  the ellipse is not big enough to reach from 'from' to 'to'
     55    //  then the ellipse is scaled up uniformly until there is
     56    //  exactly one solution (until the ellipse is just big enough).
     57 
     58    // -> find factor s, such that numerator' with mRx'=s*mRx and
     59    //    mRy'=s*mRy becomes 0 :
     60    double s = sqrt(1.0 - numerator / (mRx * mRx * mRy * mRy));
     61 
     62    mRx *= s;
     63    mRy *= s;
     64    root = 0.0;
     65 
     66  } else {
     67    root = (largeArcFlag == sweepFlag ? -1.0 : 1.0) *
     68           sqrt(numerator /
     69                (mRx * mRx * y1dash * y1dash + mRy * mRy * x1dash * x1dash));
     70  }
     71 
     72  double cxdash = root * mRx * y1dash / mRy;
     73  double cydash = -root * mRy * x1dash / mRx;
     74 
     75  mC.x = mCosPhi * cxdash - mSinPhi * cydash + (from.x + to.x) / 2.0;
     76  mC.y = mSinPhi * cxdash + mCosPhi * cydash + (from.y + to.y) / 2.0;
     77  mTheta = CalcVectorAngle(1.0, 0.0, (x1dash - cxdash) / mRx,
     78                           (y1dash - cydash) / mRy);
     79  double dtheta =
     80      CalcVectorAngle((x1dash - cxdash) / mRx, (y1dash - cydash) / mRy,
     81                      (-x1dash - cxdash) / mRx, (-y1dash - cydash) / mRy);
     82  if (!sweepFlag && dtheta > 0)
     83    dtheta -= 2.0 * M_PI;
     84  else if (sweepFlag && dtheta < 0)
     85    dtheta += 2.0 * M_PI;
     86 
     87  // Convert into cubic bezier segments <= 90deg
     88  mNumSegs = static_cast<int>(ceil(fabs(dtheta / (M_PI / 2.0))));
     89  mDelta = dtheta / mNumSegs;
     90  mT = 8.0 / 3.0 * sin(mDelta / 4.0) * sin(mDelta / 4.0) / sin(mDelta / 2.0);
     91 
     92  mFrom = from;
     93 
     94  if (fabs(dtheta) < 1e-8) {
     95    // If the angle dtheta is extremely small, then the resulting portion of the
     96    // arc is indistinguishable from a line.
     97    // In this situation we are likely dealing with quantities that are large or
     98    // small enough (depending on what inputs caused the dtheta to end up this
     99    // way) to hit floating point precision issues, so it is safer to special
    100    // case this. The threshold may need some adjustments. For reference, skia
    101    // handles this case the same way:
    102    // https://searchfox.org/firefox-main/rev/d0ff31da7cb418d2d86b0d83fecd7114395e5d46/gfx/skia/skia/src/core/SkPath.cpp#1323
    103    mFallBackToSingleLine = true;
    104    mNumSegs = 1;
    105  }
    106 }
    107 
    108 bool SVGArcConverter::GetNextSegment(Point* cp1, Point* cp2, Point* to) {
    109  if (mSegIndex == mNumSegs) {
    110    return false;
    111  }
    112 
    113  if (mFallBackToSingleLine) {
    114    Point ctrl = (mFrom + mTo) * 0.5;
    115    *cp1 = ctrl;
    116    *cp2 = ctrl;
    117    *to = mTo;
    118    mSegIndex = 1;
    119    mFallBackToSingleLine = false;
    120    return true;
    121  }
    122 
    123  double cosTheta1 = cos(mTheta);
    124  double sinTheta1 = sin(mTheta);
    125  double theta2 = mTheta + mDelta;
    126  double cosTheta2 = cos(theta2);
    127  double sinTheta2 = sin(theta2);
    128 
    129  if (mSegIndex + 1 == mNumSegs) {
    130    // Always set the last segment's `to` endpoint to the exact end of the
    131    // arc. This prevents precision issues from "leaking" into the next path
    132    // element.
    133    *to = mTo;
    134  } else {
    135    // a) calculate endpoint of the segment:
    136    to->x = mCosPhi * mRx * cosTheta2 - mSinPhi * mRy * sinTheta2 + mC.x;
    137    to->y = mSinPhi * mRx * cosTheta2 + mCosPhi * mRy * sinTheta2 + mC.y;
    138  }
    139 
    140  // b) calculate gradients at start/end points of segment:
    141  cp1->x =
    142      mFrom.x + mT * (-mCosPhi * mRx * sinTheta1 - mSinPhi * mRy * cosTheta1);
    143  cp1->y =
    144      mFrom.y + mT * (-mSinPhi * mRx * sinTheta1 + mCosPhi * mRy * cosTheta1);
    145 
    146  cp2->x = to->x + mT * (mCosPhi * mRx * sinTheta2 + mSinPhi * mRy * cosTheta2);
    147  cp2->y = to->y + mT * (mSinPhi * mRx * sinTheta2 - mCosPhi * mRy * cosTheta2);
    148 
    149  // do next segment
    150  mTheta = theta2;
    151  mFrom = *to;
    152  ++mSegIndex;
    153 
    154  return true;
    155 }
    156 
    157 }  // namespace mozilla