LCOV - code coverage report
Current view: top level - gfx/2d - PathHelpers.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 43 104 41.3 %
Date: 2017-07-14 16:53:18 Functions: 2 6 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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/. */
       5             : 
       6             : #include "PathHelpers.h"
       7             : 
       8             : namespace mozilla {
       9             : namespace gfx {
      10             : 
      11             : UserDataKey sDisablePixelSnapping;
      12             : 
      13             : void
      14          29 : AppendRectToPath(PathBuilder* aPathBuilder,
      15             :                  const Rect& aRect,
      16             :                  bool aDrawClockwise)
      17             : {
      18          29 :   if (aDrawClockwise) {
      19          19 :     aPathBuilder->MoveTo(aRect.TopLeft());
      20          19 :     aPathBuilder->LineTo(aRect.TopRight());
      21          19 :     aPathBuilder->LineTo(aRect.BottomRight());
      22          19 :     aPathBuilder->LineTo(aRect.BottomLeft());
      23             :   } else {
      24          10 :     aPathBuilder->MoveTo(aRect.TopRight());
      25          10 :     aPathBuilder->LineTo(aRect.TopLeft());
      26          10 :     aPathBuilder->LineTo(aRect.BottomLeft());
      27          10 :     aPathBuilder->LineTo(aRect.BottomRight());
      28             :   }
      29          29 :   aPathBuilder->Close();
      30          29 : }
      31             : 
      32             : void
      33          69 : AppendRoundedRectToPath(PathBuilder* aPathBuilder,
      34             :                         const Rect& aRect,
      35             :                         const RectCornerRadii& aRadii,
      36             :                         bool aDrawClockwise)
      37             : {
      38             :   // For CW drawing, this looks like:
      39             :   //
      40             :   //  ...******0**      1    C
      41             :   //              ****
      42             :   //                  ***    2
      43             :   //                     **
      44             :   //                       *
      45             :   //                        *
      46             :   //                         3
      47             :   //                         *
      48             :   //                         *
      49             :   //
      50             :   // Where 0, 1, 2, 3 are the control points of the Bezier curve for
      51             :   // the corner, and C is the actual corner point.
      52             :   //
      53             :   // At the start of the loop, the current point is assumed to be
      54             :   // the point adjacent to the top left corner on the top
      55             :   // horizontal.  Note that corner indices start at the top left and
      56             :   // continue clockwise, whereas in our loop i = 0 refers to the top
      57             :   // right corner.
      58             :   //
      59             :   // When going CCW, the control points are swapped, and the first
      60             :   // corner that's drawn is the top left (along with the top segment).
      61             :   //
      62             :   // There is considerable latitude in how one chooses the four
      63             :   // control points for a Bezier curve approximation to an ellipse.
      64             :   // For the overall path to be continuous and show no corner at the
      65             :   // endpoints of the arc, points 0 and 3 must be at the ends of the
      66             :   // straight segments of the rectangle; points 0, 1, and C must be
      67             :   // collinear; and points 3, 2, and C must also be collinear.  This
      68             :   // leaves only two free parameters: the ratio of the line segments
      69             :   // 01 and 0C, and the ratio of the line segments 32 and 3C.  See
      70             :   // the following papers for extensive discussion of how to choose
      71             :   // these ratios:
      72             :   //
      73             :   //   Dokken, Tor, et al. "Good approximation of circles by
      74             :   //      curvature-continuous Bezier curves."  Computer-Aided
      75             :   //      Geometric Design 7(1990) 33--41.
      76             :   //   Goldapp, Michael. "Approximation of circular arcs by cubic
      77             :   //      polynomials." Computer-Aided Geometric Design 8(1991) 227--238.
      78             :   //   Maisonobe, Luc. "Drawing an elliptical arc using polylines,
      79             :   //      quadratic, or cubic Bezier curves."
      80             :   //      http://www.spaceroots.org/documents/ellipse/elliptical-arc.pdf
      81             :   //
      82             :   // We follow the approach in section 2 of Goldapp (least-error,
      83             :   // Hermite-type approximation) and make both ratios equal to
      84             :   //
      85             :   //          2   2 + n - sqrt(2n + 28)
      86             :   //  alpha = - * ---------------------
      87             :   //          3           n - 4
      88             :   //
      89             :   // where n = 3( cbrt(sqrt(2)+1) - cbrt(sqrt(2)-1) ).
      90             :   //
      91             :   // This is the result of Goldapp's equation (10b) when the angle
      92             :   // swept out by the arc is pi/2, and the parameter "a-bar" is the
      93             :   // expression given immediately below equation (21).
      94             :   //
      95             :   // Using this value, the maximum radial error for a circle, as a
      96             :   // fraction of the radius, is on the order of 0.2 x 10^-3.
      97             :   // Neither Dokken nor Goldapp discusses error for a general
      98             :   // ellipse; Maisonobe does, but his choice of control points
      99             :   // follows different constraints, and Goldapp's expression for
     100             :   // 'alpha' gives much smaller radial error, even for very flat
     101             :   // ellipses, than Maisonobe's equivalent.
     102             :   //
     103             :   // For the various corners and for each axis, the sign of this
     104             :   // constant changes, or it might be 0 -- it's multiplied by the
     105             :   // appropriate multiplier from the list before using.
     106             : 
     107          69 :   const Float alpha = Float(0.55191497064665766025);
     108             : 
     109             :   typedef struct { Float a, b; } twoFloats;
     110             : 
     111             :   twoFloats cwCornerMults[4] = { { -1,  0 },    // cc == clockwise
     112             :                                  {  0, -1 },
     113             :                                  { +1,  0 },
     114          69 :                                  {  0, +1 } };
     115             :   twoFloats ccwCornerMults[4] = { { +1,  0 },   // ccw == counter-clockwise
     116             :                                   {  0, -1 },
     117             :                                   { -1,  0 },
     118          69 :                                   {  0, +1 } };
     119             : 
     120          69 :   twoFloats *cornerMults = aDrawClockwise ? cwCornerMults : ccwCornerMults;
     121             : 
     122         138 :   Point cornerCoords[] = { aRect.TopLeft(), aRect.TopRight(),
     123         207 :                            aRect.BottomRight(), aRect.BottomLeft() };
     124             : 
     125          69 :   Point pc, p0, p1, p2, p3;
     126             : 
     127          69 :   if (aDrawClockwise) {
     128         104 :     aPathBuilder->MoveTo(Point(aRect.X() + aRadii[eCornerTopLeft].width,
     129         104 :                                aRect.Y()));
     130             :   } else {
     131          34 :     aPathBuilder->MoveTo(Point(aRect.X() + aRect.Width() - aRadii[eCornerTopRight].width,
     132          34 :                                aRect.Y()));
     133             :   }
     134             : 
     135         345 :   for (int i = 0; i < 4; ++i) {
     136             :     // the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw)
     137         276 :     int c = aDrawClockwise ? ((i+1) % 4) : ((4-i) % 4);
     138             : 
     139             :     // i+2 and i+3 respectively.  These are used to index into the corner
     140             :     // multiplier table, and were deduced by calculating out the long form
     141             :     // of each corner and finding a pattern in the signs and values.
     142         276 :     int i2 = (i+2) % 4;
     143         276 :     int i3 = (i+3) % 4;
     144             : 
     145         276 :     pc = cornerCoords[c];
     146             : 
     147         276 :     if (aRadii[c].width > 0.0 && aRadii[c].height > 0.0) {
     148         276 :       p0.x = pc.x + cornerMults[i].a * aRadii[c].width;
     149         276 :       p0.y = pc.y + cornerMults[i].b * aRadii[c].height;
     150             : 
     151         276 :       p3.x = pc.x + cornerMults[i3].a * aRadii[c].width;
     152         276 :       p3.y = pc.y + cornerMults[i3].b * aRadii[c].height;
     153             : 
     154         276 :       p1.x = p0.x + alpha * cornerMults[i2].a * aRadii[c].width;
     155         276 :       p1.y = p0.y + alpha * cornerMults[i2].b * aRadii[c].height;
     156             : 
     157         276 :       p2.x = p3.x - alpha * cornerMults[i3].a * aRadii[c].width;
     158         276 :       p2.y = p3.y - alpha * cornerMults[i3].b * aRadii[c].height;
     159             : 
     160         276 :       aPathBuilder->LineTo(p0);
     161         276 :       aPathBuilder->BezierTo(p1, p2, p3);
     162             :     } else {
     163           0 :       aPathBuilder->LineTo(pc);
     164             :     }
     165             :   }
     166             : 
     167          69 :   aPathBuilder->Close();
     168          69 : }
     169             : 
     170             : void
     171           0 : AppendEllipseToPath(PathBuilder* aPathBuilder,
     172             :                     const Point& aCenter,
     173             :                     const Size& aDimensions)
     174             : {
     175           0 :   Size halfDim = aDimensions / 2.f;
     176           0 :   Rect rect(aCenter - Point(halfDim.width, halfDim.height), aDimensions);
     177           0 :   RectCornerRadii radii(halfDim.width, halfDim.height);
     178             : 
     179           0 :   AppendRoundedRectToPath(aPathBuilder, rect, radii);
     180           0 : }
     181             : 
     182             : bool
     183           0 : SnapLineToDevicePixelsForStroking(Point& aP1, Point& aP2,
     184             :                                   const DrawTarget& aDrawTarget,
     185             :                                   Float aLineWidth)
     186             : {
     187           0 :   Matrix mat = aDrawTarget.GetTransform();
     188           0 :   if (mat.HasNonTranslation()) {
     189           0 :     return false;
     190             :   }
     191           0 :   if (aP1.x != aP2.x && aP1.y != aP2.y) {
     192           0 :     return false; // not a horizontal or vertical line
     193             :   }
     194           0 :   Point p1 = aP1 + mat.GetTranslation(); // into device space
     195           0 :   Point p2 = aP2 + mat.GetTranslation();
     196           0 :   p1.Round();
     197           0 :   p2.Round();
     198           0 :   p1 -= mat.GetTranslation(); // back into user space
     199           0 :   p2 -= mat.GetTranslation();
     200             : 
     201           0 :   aP1 = p1;
     202           0 :   aP2 = p2;
     203             : 
     204           0 :   bool lineWidthIsOdd = (int(aLineWidth) % 2) == 1;
     205           0 :   if (lineWidthIsOdd) {
     206           0 :     if (aP1.x == aP2.x) {
     207             :       // snap vertical line, adding 0.5 to align it to be mid-pixel:
     208           0 :       aP1 += Point(0.5, 0);
     209           0 :       aP2 += Point(0.5, 0);
     210             :     } else {
     211             :       // snap horizontal line, adding 0.5 to align it to be mid-pixel:
     212           0 :       aP1 += Point(0, 0.5);
     213           0 :       aP2 += Point(0, 0.5);
     214             :     }
     215             :   }
     216           0 :   return true;
     217             : }
     218             : 
     219             : void
     220           0 : StrokeSnappedEdgesOfRect(const Rect& aRect, DrawTarget& aDrawTarget,
     221             :                         const ColorPattern& aColor,
     222             :                         const StrokeOptions& aStrokeOptions)
     223             : {
     224           0 :   if (aRect.IsEmpty()) {
     225           0 :     return;
     226             :   }
     227             : 
     228           0 :   Point p1 = aRect.TopLeft();
     229           0 :   Point p2 = aRect.BottomLeft();
     230             :   SnapLineToDevicePixelsForStroking(p1, p2, aDrawTarget,
     231           0 :                                     aStrokeOptions.mLineWidth);
     232           0 :   aDrawTarget.StrokeLine(p1, p2, aColor, aStrokeOptions);
     233             : 
     234           0 :   p1 = aRect.BottomLeft();
     235           0 :   p2 = aRect.BottomRight();
     236             :   SnapLineToDevicePixelsForStroking(p1, p2, aDrawTarget,
     237           0 :                                     aStrokeOptions.mLineWidth);
     238           0 :   aDrawTarget.StrokeLine(p1, p2, aColor, aStrokeOptions);
     239             : 
     240           0 :   p1 = aRect.TopLeft();
     241           0 :   p2 = aRect.TopRight();
     242             :   SnapLineToDevicePixelsForStroking(p1, p2, aDrawTarget,
     243           0 :                                     aStrokeOptions.mLineWidth);
     244           0 :   aDrawTarget.StrokeLine(p1, p2, aColor, aStrokeOptions);
     245             : 
     246           0 :   p1 = aRect.TopRight();
     247           0 :   p2 = aRect.BottomRight();
     248             :   SnapLineToDevicePixelsForStroking(p1, p2, aDrawTarget,
     249           0 :                                     aStrokeOptions.mLineWidth);
     250           0 :   aDrawTarget.StrokeLine(p1, p2, aColor, aStrokeOptions);
     251             : }
     252             : 
     253             : // The logic for this comes from _cairo_stroke_style_max_distance_from_path
     254             : Margin
     255           0 : MaxStrokeExtents(const StrokeOptions& aStrokeOptions,
     256             :                  const Matrix& aTransform)
     257             : {
     258           0 :   double styleExpansionFactor = 0.5f;
     259             : 
     260           0 :   if (aStrokeOptions.mLineCap == CapStyle::SQUARE) {
     261           0 :     styleExpansionFactor = M_SQRT1_2;
     262             :   }
     263             : 
     264           0 :   if (aStrokeOptions.mLineJoin == JoinStyle::MITER &&
     265           0 :       styleExpansionFactor < M_SQRT2 * aStrokeOptions.mMiterLimit) {
     266           0 :     styleExpansionFactor = M_SQRT2 * aStrokeOptions.mMiterLimit;
     267             :   }
     268             : 
     269           0 :   styleExpansionFactor *= aStrokeOptions.mLineWidth;
     270             : 
     271           0 :   double dx = styleExpansionFactor * hypot(aTransform._11, aTransform._21);
     272           0 :   double dy = styleExpansionFactor * hypot(aTransform._22, aTransform._12);
     273             : 
     274             :   // Even if the stroke only partially covers a pixel, it must still render to
     275             :   // full pixels. Round up to compensate for this.
     276           0 :   dx = ceil(dx);
     277           0 :   dy = ceil(dy);
     278             : 
     279           0 :   return Margin(dy, dx, dy, dx);
     280             : }
     281             : 
     282             : } // namespace gfx
     283             : } // namespace mozilla

Generated by: LCOV version 1.13