Package detail

cubic2quad

fontello1.2mMIT1.2.1

Approximate cubic Bezier curve with a number of quadratic ones

cubic, quad, bezier

readme

cubic2quad

CI NPM version Coverage Status

Aproximates cubic Bezier curves with quadratic ones.

This package was done to create TTF fonts (those support quadratic curves only). Generated curves have the same tangents angles at the ends. That's important to keep result visually smooth.

Algorithm

Logic is similar to one from FontForge.

Steps:

  1. Split quadratic curve into k segments (from 2 at start, to 8 max).
  2. Approximate each segment with tangents intersection approach (see picture in article).
  3. Measure approximation error and increase splits count if needed (and max not reached).
    • set 10 points on each interval & calculate minimal distance to created quadratic curve.

Usage

var cubic2quad = require('cubic2quad');
// Input: (px1, py1, cx1, cy1, cx2, cy2, px2, py2, precision)
var quads = cubic2quad(0, 0, 10, 9, 20, 11, 30, 0, 0.1);

It converts given quadratic curve to a number of quadratic ones. Result is:

[ P1x, P1y, C1x, C1y, P2x, P2y, C2x, C2y, ..., Cnx, Cny, P{n+1}x, P{n+1}y ]

where Pi are base points and Ci are control points.

Authors

License

MIT

changelog

1.2.1 / 2021-05-20

  • Restored code coverage to 100%.
  • Add floating point error tolerance in quad (1 root case) and cubic (2 root case) solvers.

1.2.0 / 2021-05-19

  • Fixed wrong approximation for very small cubic curves: when diff between start and end of a cubic curve falls within errorBound, in earlier versions any quadratic curve was considered to be fitting. Error estimation algorithm is changed: previously it calculated distance from points on cubic curve to quadratic, now it also calculates the other way around. Distance calculation is changed from exact solution with cubic solver to linear segment approximations of both curves.

1.1.1 / 2017-05-08

  • Fix sorting in solveInflections, #4.

1.1.0 / 2016-10-26

  • Support curves with inflection point.

1.0.0 / 2015-10-27

  • First release.