How many quadratics does a cubic take? The error of cu2qu’s spline in closed form
cu2qu converts cubic Bézier curves to quadratic splines for TrueType. It cuts a cubic into \(n\) pieces of equal parameter length, gives each piece one off-curve point, and raises \(n\) until the spline tests within tolerance. This note shows that the spline needs no test. At equal parameters it differs from the cubic by a multiple of a single vector, the cubic’s third difference \(\mathbf{d}_3\), and the largest distance between them is \(\lvert\mathbf{d}_3\rvert\,\Phi(n)/n^3\), where \(\Phi(n)\) depends on \(n\) alone and has a closed form: 4/27 for two quadratics, 27/128 for three, rising towards \(\tfrac14 + \tfrac{\sqrt3}{36}\). So the number of quadratics is computed, not searched for. The same analysis shows what it costs a spline to start and end with the cubic’s derivative, as cu2qu’s does, and that cu2qu’s weights are then the best there are for two and three quadratics; it tells when a single quadratic cannot fit, and explains why the recursive fit test could fail to return on finite input. For renderers, which have no masters to keep compatible, a second closed form bounds the true distance between a cubic and the quadratic of its end tangents. The results are in HarfBuzz and are proposed for fontTools and kurbo.
- 1. Introduction
- 2. The construction
- 3. The error in closed form
- 4. How good are the weights?
- 5. One quadratic
- 6. Masters: why equal parameters
- 7. Without masters: one quadratic by distance
- 8. In practice
- 9. Related work
- 10. Loose ends
- About this note
- Appendix A. The computation
- Appendix B. Checking the claims
- References
∞ 1. Introduction
Type is mostly drawn with cubic Bézier curves, and TrueType outlines are quadratic. So cubics get converted: by font compilers that build TrueType fonts from cubic sources, and by renderers whose curve handling is quadratic only. cu2qu [1], which James Godfrey-Kittle and Behdad Esfahbod wrote in 2015 and which has been part of fontTools since 2020 [2], is the converter behind fontmake (through ufo2ft). kurbo has a Rust port of it [3], which fontc uses, and HarfBuzz’s GPU library has one in C++.
The idea fits in three sentences. Cut the cubic into \(n\) pieces of equal parameter length. Give each piece one off-curve point, between the point that would keep the piece’s tangent at its start and the point that would keep it at its end, going from the first to the second as the pieces go from the first to the last. Leave the on-curve points between them out: TrueType implies an on-curve point halfway between two consecutive off-curve points. Behdad’s sketch of this, in July 2015, left one thing open [4]:
The only part I have not figured out is the error function. After error function is figured out, just increase n until feasible solution is found.
The error was at first measured at sampled parameters, an exact and recursive test replaced that the following year, and \(n\) has been increased until feasible ever since. This note figures the error function out. What it finds:
- Section 3. At equal parameters, the spline of \(n \ge 2\) quadratics differs from the cubic by a multiple of one vector, the cubic’s third difference \(\mathbf{d}_3\), and the multiple depends only on \(n\) and on where along the spline one looks. The largest distance is \(\lvert\mathbf{d}_3\rvert\,\Phi(n)/n^3\) with \(\Phi(n)\) in closed form. So \(n\) is computed, and the spline is built once and not tested.
- Section 4. At equal parameters, no spline of \(n\) quadratics with implied on-curve points comes closer than \(\tfrac{\sqrt3}{36}\,\lvert\mathbf{d}_3\rvert/n^3\), and none that starts or ends with the cubic’s derivative, as cu2qu’s does, comes closer than \(\tfrac{4}{27}\,\lvert\mathbf{d}_3\rvert/n^3\). Of those that do both, cu2qu’s is the best there is for two and for three quadratics, and within a factor of about two of the best beyond that.
- Section 5. At equal parameters, a single quadratic cannot fit a cubic whose third difference is longer than \(36/\sqrt3 \approx 20.8\) times the tolerance. The recursive test that remains for the single quadratic could run without end on finite input, in fontTools and in kurbo.
- Section 6. Why the distance has to be taken at equal parameters when masters are interpolated, what that has to do with kinks, and what it costs.
- Section 7. For a renderer, a closed-form bound on the true distance between a cubic and the quadratic of its end tangents.
- Section 8. What changed in HarfBuzz, fontTools and kurbo, and by how much.
∞ 2. The construction
A cubic has control points \(\mathbf{c}_0, \mathbf{c}_1, \mathbf{c}_2, \mathbf{c}_3\) and is
Points and vectors are in bold, and distances are Euclidean. The tolerance is \(\varepsilon\), in the units of the coordinates. Font compilers use a thousandth of the em by default, which is one unit for a font of 1,000 units per em.
∞ The spline
For \(n \ge 2\), cut the cubic at \(t = k/n\) into \(n\) pieces, numbered \(k = 0, \dots, n-1\). Each piece is a cubic with control points \(\mathbf{p}_0, \mathbf{p}_1, \mathbf{p}_2, \mathbf{p}_3\) of its own and a parameter \(u\) from 0 to 1 of its own. A quadratic from \(\mathbf{p}_0\) to \(\mathbf{p}_3\) with off-curve point \(\mathbf{q}\) starts with derivative \(2(\mathbf{q} - \mathbf{p}_0)\), and the piece starts with \(3(\mathbf{p}_1 - \mathbf{p}_0)\). So there is one quadratic that leaves \(\mathbf{p}_0\) exactly as the piece does, and one that arrives at \(\mathbf{p}_3\) exactly as the piece does. Their off-curve points are the two candidates of the piece:
The off-curve point of piece \(k\) is taken between its two candidates, at the first for the first piece and at the second for the last:
(The 2015 sketch divided by \(n\) here. cu2qu divides by \(n - 1\), so that the last piece takes its second candidate.) The spline has the points \(\mathbf{c}_0, \mathbf{o}_0, \dots, \mathbf{o}_{n-1}, \mathbf{c}_3\), of which only the first and the last are on the curve. Its \(n\) quadratics meet halfway between consecutive off-curve points (Figure 1).
third difference 1146, tolerance 10.0: 3 quadratics. Largest distance at equal parameters: 8.96 by the formula, 8.96 measured.
Three things follow at once.
- End tangents. \(\mathbf{o}_0 = \mathbf{q}_0^{+}\) and \(\mathbf{o}_{n-1} = \mathbf{q}_{n-1}^{-}\), so the spline leaves \(\mathbf{c}_0\) along \(\mathbf{c}_1 - \mathbf{c}_0\) and arrives at \(\mathbf{c}_3\) along \(\mathbf{c}_3 - \mathbf{c}_2\). Where the cubic met its neighbour smoothly, so does the spline, and a point drawn at an extremum keeps its level or upright tangent.
- Size. A cubic becomes \(n + 1\) points: its \(n\) off-curve points and one end. Five quadratics with implied on-curve points take the room of three with the on-curve points written out [4].
- Linearity. For a given \(n\), every point of the spline is a fixed combination of the cubic’s four control points. Section 6 rests on this.
∞ One quadratic
For \(n = 1\) the rule above has no meaning, and keeping both end tangents leaves one choice: the point \(\mathbf{q}\) where the two end tangent lines meet. cu2qu tries this quadratic first.
∞ The measure, and the search
A spline is accepted when it is within \(\varepsilon\) of the cubic at equal parameters: quadratic \(k\) at its parameter \(u\) is compared with the cubic at \(t = (k+u)/n\), for every \(k\) and \(u\). This asks for more than the two curves being close. Section 6 says why fonts need it.
Until now the answer was found by trying. The single quadratic first; then the spline for
\(n = 2, 3, \dots\), up to 100 in fontTools, each built and each of its quadratics tested
against its piece. The test, cubic_farthest_fit_inside, takes the difference of the two
curves, which is itself a cubic Bézier, and halves it until its control points are within
\(\varepsilon\) of the origin, which settles that it is inside, or its middle is
farther than \(\varepsilon\), which settles that it is not. It is exact up to
rounding, and it is a large part of the work.
∞ 3. The error in closed form
∞ One vector
Let
be the third difference of the control points. It is the coefficient of \(t^3\) in \(C(t)\), and it is zero exactly when the cubic is a quadratic written with four points. Each of the \(n\) pieces, with its own parameter from 0 to 1, has the third difference \(\mathbf{d} = \mathbf{d}_3/n^3\). Everything below comes from two facts, one about a piece and one about two neighbours.
Lemma 1. Let a piece have control points \(\mathbf{p}_0, \dots, \mathbf{p}_3\) and third difference \(\mathbf{d}\), and let \(\mathbf{m} = \tfrac12(\mathbf{q}^{+} + \mathbf{q}^{-})\) be the point halfway between its candidates. Then \(\mathbf{q}^{+} - \mathbf{q}^{-} = \tfrac12\,\mathbf{d}\), and the quadratic with control points \(\mathbf{p}_0 + \mathbf{x}_0\), \(\mathbf{m} + \mathbf{x}_1\), \(\mathbf{p}_3 + \mathbf{x}_2\) differs from the piece, at parameter \(u\), by
Proof. From the definitions, \(\mathbf{q}^{+} - \mathbf{q}^{-} = \tfrac12(\mathbf{p}_3 - 3\mathbf{p}_2 + 3\mathbf{p}_1 - \mathbf{p}_0)\) and \(\mathbf{m} = \tfrac14\,(3\mathbf{p}_1 + 3\mathbf{p}_2 - \mathbf{p}_0 - \mathbf{p}_3)\). Written as a cubic, the quadratic \((\mathbf{p}_0, \mathbf{m}, \mathbf{p}_3)\) has the inner control points \(\tfrac13(\mathbf{p}_0 + 2\mathbf{m}) = \mathbf{p}_1 - \tfrac16\mathbf{d}\) and \(\tfrac13(2\mathbf{m} + \mathbf{p}_3) = \mathbf{p}_2 + \tfrac16\mathbf{d}\), so it differs from the piece by
Moving the three control points of a quadratic by \(\mathbf{x}_0, \mathbf{x}_1, \mathbf{x}_2\) moves its point at \(u\) by \((1-u)^2\mathbf{x}_0 + 2u(1-u)\mathbf{x}_1 + u^2\mathbf{x}_2\). ∎
Lemma 2. Let two consecutive pieces of a cubic, of equal parameter length, meet at the point \(\mathbf{j}\) of the cubic. Then the halfway point \(\mathbf{m}\) of the first and that of the second have \(\mathbf{j}\) as their midpoint.
Proof. The pieces have equal parameter lengths, so the cubic’s derivative at \(\mathbf{j}\) gives them the same handle there: the first ends with \(\mathbf{p}_3 - \mathbf{p}_2 = \mathbf{h}\) and the second starts with \(\mathbf{p}_1 - \mathbf{p}_0 = \mathbf{h}\). Hence the second candidate of the first piece is \(\mathbf{j} - \tfrac32\mathbf{h}\) and the first candidate of the second piece is \(\mathbf{j} + \tfrac32\mathbf{h}\). By Lemma 1 the halfway points are those plus and minus \(\tfrac14\mathbf{d}\), with the same \(\mathbf{d}\) for both pieces. ∎
∞ The error of the spline
Write the off-curve point of piece \(k\) as \(\mathbf{o}_k = \mathbf{m}_k + \tfrac14\,s_k\,\mathbf{d}\). By Lemma 1 the weight \(s_k = +1\) is the first candidate and \(s_k = -1\) the second, and cu2qu’s rule is the ramp
By Lemma 2, the implied on-curve point between pieces \(k-1\) and \(k\) is \(\tfrac12(\mathbf{o}_{k-1} + \mathbf{o}_k) = C(k/n) + g_k\,\mathbf{d}\), with
the last two because the spline starts and ends on the cubic. Lemma 1, with \(\mathbf{x}_0 = g_k\mathbf{d}\), \(\mathbf{x}_1 = \tfrac14 s_k\mathbf{d}\) and \(\mathbf{x}_2 = g_{k+1}\mathbf{d}\), now gives the whole error.
Theorem 1. Quadratic \(k\) of the spline at parameter \(u\), minus the cubic at \(t = (k+u)/n\), is \(\varphi_k(u)\,\mathbf{d}\), where
The error always points along \(\mathbf{d}_3\). Its size, in units of \(\lvert\mathbf{d}_3\rvert/n^3\), has nothing to do with the cubic: it is the same function for every cubic there is (Figure 2).
∞ Its largest value
For the ramp the polynomials are simple. In the first piece,
which is largest at \(u = \tfrac23(1+g_1)\), where it is \(\tfrac{4}{27}(1+g_1)^3\). The last piece is its mirror image, \(\varphi_{n-1}(u) = -\varphi_0(1-u)\). In the pieces between, with \(v = u - \tfrac12\),
the same odd cubic in every piece, on a constant that ramps down from piece to piece. Its largest value over those pieces is in the second piece. From \(n = 9\) on that is \(\beta + 2(\beta/3)^{3/2}\), at \(v = \sqrt{\beta/3}\); for fewer pieces it is where the second piece meets the first, and below the first piece’s maximum.
Theorem 2. The largest distance, at equal parameters, between a cubic and its spline of \(n \ge 2\) quadratics is \(\lvert\mathbf{d}_3\rvert\,\Phi(n)/n^3\), with
with \(g_1 = \frac{n-2}{4(n-1)}\) and \(\beta = \frac{n-3}{4(n-1)}\) as above, where the second term counts from \(n = 4\). \(\Phi(2) = 4/27\), \(\Phi(3) = 27/128\), and \(\Phi\) grows with \(n\) towards \(\tfrac14 + \tfrac{\sqrt3}{36} \approx 0.2981\). The first term is the larger one up to \(n = 54\). (The second is the largest value of the pieces between only from \(n = 9\); below that it is one of their values, and less than the first term.)
∞ The number of quadratics
So the spline of \(n\) quadratics is within tolerance exactly when
\(L\) grows with \(n\), because \(\Phi\) grows more slowly than \(n^3\): from \(n\) to \(n+1\) its first term grows by the factor \(\bigl(1 + \tfrac{1}{n(5n-6)}\bigr)^3\) and its second by less than \(\bigl(1 + \tfrac{2}{n(n-3)}\bigr)^{3/2}\), and both are below \(\bigl(1 + \tfrac1n\bigr)^3\). So the number of quadratics a cubic takes is the least \(n\) whose limit holds its third difference (Table 1). It takes a third difference, one length, and a walk up a table that is the same for every cubic (Appendix A). The spline is then built once, with the same arithmetic as before, and not tested.
| \(n\) | \(\Phi(n)\) | \(L(n)\) | best weights |
|---|---|---|---|
| 2 | 0.14815 | 54 | 54 |
| 3 | 0.21094 | 128 | 128 |
| 4 | 0.23525 | 272.05 | 432 |
| 5 | 0.24808 | 503.86 | 823.02 |
| 6 | 0.25600 | 843.75 | 1,458 |
| 7 | 0.26137 | 1,312.31 | 2,296.10 |
| 8 | 0.26525 | 1,930.24 | 3,456 |
| 9 | 0.26819 | 2,718.24 | 4,917.60 |
| 10 | 0.27049 | 3,697.03 | 6,750 |
| 16 | 0.27793 | 14,737.45 | 27,648 |
| 100 | 0.29161 | 3,429,220.62 | 6,750,000 |
Three remarks.
- More quadratics fit too. Since \(L\) grows, a cubic that fits \(n\) quadratics fits any larger number. When several masters are converted together they must all take the same \(n\); they take the largest that any of them needs.
- Bounds. \(\Phi\) stays between 4/27 and its limit, so
\[\sqrt[3]{\frac{4\,\lvert\mathbf{d}_3\rvert}{27\,\varepsilon}} \;\le\; n \;\le\; \max\left(2,\ \left\lceil\, \sqrt[3]{\left(\frac14 + \frac{\sqrt3}{36}\right)\frac{\lvert\mathbf{d}_3\rvert}{\varepsilon}}\ \right\rceil\right).\]The two sides are a factor of 1.26 apart.
- Ties. The computed number differs from the searched one only for a cubic that is at a limit, to within rounding, such as \(\lvert\mathbf{d}_3\rvert = 128\,\varepsilon\). Its spline of three quadratics touches the tolerance without crossing it. The recursive test, working in floating point on halves of halves, did one of three things with such a spline: accept it, reject it, after which one more quadratic was used, or not return (section 5). Of random cubics with integer coordinates exactly at a limit, it accepted all 3,000 at 54 times the tolerance, rejected 2,145 of 3,000 at 128 times, and at 843.75 times rejected 2,290 of 4,200 and did not return for 61. In the fonts of section 8 it rejects 3 cubics of 77,533 at a tolerance of one unit and 7 at half a unit, and all of them go from four quadratics to three.
∞ 4. How good are the weights?
The ramp is one choice of weights. Theorem 1 did not use it: for any weights \(s_0, \dots, s_{n-1}\) the error is \(\varphi_k(u)\,\mathbf{d}\) with \(g_k = (s_{k-1} + s_k)/8\). Nor is there anything to gain from moving an off-curve point off the line through its two candidates. If \(\mathbf{o}_k = \mathbf{m}_k + \tfrac14 s_k\mathbf{d} + \mathbf{x}_k\) with \(\mathbf{x}_k\) across \(\mathbf{d}\), the part of the error along \(\mathbf{d}\) is still \(\varphi_k(u)\,\mathbf{d}\), and the \(\mathbf{x}_k\) only add a part across it, which makes it longer. So the weights cover every spline of \(n\) quadratics with implied on-curve points, as long as it is compared with the cubic piece by piece at equal parameters.
Two of the weights have a meaning of their own. With \(s_0 = 1\) the first off-curve point is the first candidate, and the spline starts with the cubic’s derivative: its first handle is \(\tfrac{3}{2n}\) of the cubic’s. With \(s_{n-1} = -1\) it ends with the cubic’s derivative. cu2qu’s spline does both. That is more than keeping the end tangents, for which an off-curve point may be anywhere on the tangent line.
Theorem 3. For every \(n \ge 1\) and every choice of weights, the largest \(\lvert\varphi_k(u)\rvert\) is at least \(\sqrt3/36 \approx 0.0481\), and it is exactly that when all weights are zero. If \(s_0 = 1\) or \(s_{n-1} = -1\), it is at least \(4/27 \approx 0.1481\).
Proof. Take the second claim first, with \(s_0 = 1\); the other end follows by reversing the cubic, which turns the weights \(s_k\) into \(-s_{n-1-k}\). From Theorem 1, for every \(k\),
Suppose every \(\lvert\varphi_k(u)\rvert\) were below 4/27. For an even \(k\) with \(g_k \ge 0\) and \(s_k \ge 1\), which holds for \(k = 0\), the first identity gives \(\varphi_k(\tfrac23) \ge \tfrac{4}{27} + \tfrac49 g_{k+1}\), so \(g_{k+1} \lt 0\), and then \(s_{k+1} = 8g_{k+1} - s_k \lt -1\). For an odd \(k\) with \(g_k \lt 0\) and \(s_k \lt -1\), the second identity gives \(\varphi_k(\tfrac13) \lt -\tfrac{4}{27} + \tfrac19 g_{k+1}\), so \(g_{k+1} \gt 0\) and \(s_{k+1} \gt 1\). So the signs alternate all the way along the spline, and the last step asks for \(g_n \ne 0\). But \(g_n = 0\): the spline ends on the cubic.
The first claim goes the same way with the two parameters \(u_\pm = \tfrac12 \pm \tfrac{\sqrt3}{6}\), where \(u(1-u) = \tfrac16\) and \((1 - u_\pm)^2 = u_\mp^2\):
Suppose every \(\lvert\varphi_k(u)\rvert\) were below \(\sqrt3/36\). The first piece, where \(g_0 = 0\), gives \(u_+^2\,g_1 + \tfrac{1}{12}s_0 \lt 0 \lt u_-^2\,g_1 + \tfrac{1}{12}s_0\), hence \(g_1 \lt 0 \lt s_0\), as \(u_+ \gt u_-\). For an odd \(k\) with \(g_k \lt 0\) and \(s_k \lt 0\), which \(k = 1\) is since \(s_1 = 8g_1 - s_0\), the second identity gives \(u_-^2\,g_{k+1} \gt -u_+^2\,g_k - \tfrac{1}{12}s_k \gt 0\), so \(g_{k+1} \gt 0\) and \(s_{k+1} = 8g_{k+1} - s_k \gt 0\). For an even \(k \ge 2\) with \(g_k \gt 0\) and \(s_k \gt 0\), the first gives \(u_+^2\,g_{k+1} \lt -u_-^2\,g_k - \tfrac{1}{12}s_k \lt 0\), so \(g_{k+1} \lt 0\) and \(s_{k+1} \lt 0\). Again the signs alternate up to \(g_n \ne 0\). With all weights zero every \(g_k\) is zero and \(\varphi_k(u) = \tfrac12 u(1-u)(2u-1)\), whose largest size is \(\sqrt3/36\), at \(u_\pm\). ∎
What the theorem says:
- Nothing beats the mid-point spline. All weights zero is the
mid-point spline: each
piece takes the point halfway between its candidates, every on-curve point is on the cubic, and
the error is \(\tfrac{\sqrt3}{36}\lvert\mathbf{d}_3\rvert/n^3\). This is the
scheme of kurbo’s
to_quadsand of lyon (section 9). Under this measure no spline with implied on-curve points does better. It gives up the end tangents: its first quadratic leaves \(\mathbf{c}_0\) a quarter of \(\mathbf{d}\) off the cubic’s tangent. In the fonts of section 8, at a tolerance of one unit, that turns the tangent at an end by more than a degree for 57% of the cubics. - Starting or ending with the cubic’s derivative costs a factor of three. \(\tfrac{4}{27} \div \tfrac{\sqrt3}{36} \approx 3.08\) in distance, or 1.45 times the pieces for a long curve, whatever the other weights are.
- Keeping only the tangent can cost less for one cubic, not for all. A spline keeps the tangent at an end with its off-curve point anywhere on the tangent line, and sliding the point along that line changes \(s_0\) by the part of the slide that lies along \(\mathbf{d}\). The single quadratic of section 5 is such a spline, and it can be closer than 4/27 allows. But when the cubic’s handle is at right angles to its third difference, as in a symmetric arch with parallel handles, sliding leaves \(s_0\) at one and the bound stands. So a rule that keeps the tangent cannot promise less than 4/27 in terms of the third difference; and the candidate is the one point of the tangent line that is right for a cubic that is a quadratic.
- cu2qu. Its spline starts and ends with the cubic’s derivative. For two quadratics that leaves no choice, \((1, -1)\), and the error is 4/27, the least there can be. For three, one weight is free; the largest error is a convex function of it and is the same for \(s\) and \(-s\), so the best value is zero, which is the ramp: 27/128. From four on the ramp is not the best. Alternating \(+1, -1, +1, \dots\) makes every \(g_k\) zero and gives 4/27 for every even \(n\): each pair of quadratics is the two-quadratic spline of one piece of twice the length, the construction of Truong, Yuksel and Seiler [9]. For an odd \(n\) the best weights come from a linear program: \((1, s, 0, -s, -1)\) with \(s \approx -0.9334\) for five quadratics, giving 0.1519; then 0.1494 for seven and 0.1482 for nine, nearing 4/27.
The last column of Table 1 has the limits of those best weights, and Figure 3 the number of quadratics each choice takes. For a long curve the ramp takes \(\sqrt[3]{0.2981 / 0.1481} = 1.26\) times the quadratics of the best weights. In fonts the difference is much less, because a cubic that takes two or three quadratics has nothing to gain, and most do. In the fonts of section 8 the best weights would save 0.6% of the quadratics at a tolerance of one unit, 2.6% at half a unit, and 10.6% at a tenth. They would also move every curve of four quadratics or more in every font that is rebuilt. cu2qu keeps the ramp.
∞ 5. One quadratic
The single quadratic has its off-curve point at \(\mathbf{q}\), where the lines through \(\mathbf{c}_0, \mathbf{c}_1\) and through \(\mathbf{c}_2, \mathbf{c}_3\) meet. By Lemma 1 it differs from the cubic by
and \(\mathbf{q} - \mathbf{m}\) does not lie along \(\mathbf{d}_3\) in general. So this error depends on the shape of the cubic, and cu2qu keeps testing it, with the recursive test.
∞ When it cannot fit
The part of that error along \(\mathbf{d}_3\) is the case \(n = 1\) of Theorem 3: wherever the off-curve point is, a quadratic with the cubic’s end points is at least \(\tfrac{\sqrt3}{36}\lvert\mathbf{d}_3\rvert\) from the cubic somewhere, at equal parameters. So no single quadratic fits when
and there is no need to intersect tangents or to run the test. In the fonts of section 8 this rules out 61% of the cubics at a tolerance of one unit, 80% at half a unit and 97% at a tenth. Of the rest, few pass: 6.3%, 2.4% and 0.3% of all cubics. The implementations compare with 21, so that the bound can change no result.
∞ When the test does not return
The recursive test stops at a piece of the difference curve whose inner control points are within \(\varepsilon\), and at one whose middle is beyond \(\varepsilon\). A difference that touches \(\varepsilon\) at its farthest point, without crossing, has a piece that never becomes either. In floating point one control point of that piece can stay one unit in the last place outside while its middle rounds to \(\varepsilon\) itself, and the half that contains the touching point is the same piece again. It takes a tolerance and a curve whose bits line up. These are two:
curve_to_quadratic([(0, 0), (0, 0), (0, 0), (25, 0)], 100 / 27)
curve_to_quadratic([(-45, -3), (-30, -2), (0, 5), (0, 9)], 20 / 3)
In the first, the difference is \(25\,u^2(1-u)\) along the
\(x\) axis, which peaks at exactly 100/27. With fontTools 4.60.1 each of the two
calls ends in a RecursionError in pure Python and takes the interpreter down with the
compiled module. Both are single quadratics. The same happens in the search over \(n\), to a
spline exactly at a limit of Table 1, as in these two, whose third differences are 128 × 5 and
843.75 × 4:
curve_to_quadratic([(-258, -394), (-148, -194), (-155, -13), (105, 661)], 5)
curve_to_quadratic([(-22, 289), (31, 324), (-137, 476), (1499, 3445)], 4)
kurbo’s port, whose arithmetic differs from fontTools’ in the last bit, returns from these four and overflows its stack on others: in the search over \(n\) on the cubic (386, −158), (527, −28), (480, −99), (2270, 2329) at an accuracy of 4, again 843.75 × 4, and on single quadratics such as (−20, 7), (−20, −27.5), (−12, −32.5), (4, −32) at 32/9. A NaN coordinate or tolerance does the same in both, because every comparison with it is false.
The repair proposed for both is a limit of 64 halvings, after which the control points of a piece differ by rounding alone and the piece is taken to be where its middle is, and comparisons written so that a NaN ends the recursion. With \(n\) computed, the spline no longer goes through the test at all.
∞ 6. Masters: why equal parameters
A variable font stores masters and interpolates between them: every control point of an instance is the same weighted average of that point in the masters. The masters have to be converted together, to the same number of points, and what the conversion did to the masters has to hold in every instance. That decides the measure.
∞ Instances inherit the bound
Let master \(i\) have the cubic \(C_i\) and the spline \(S_i\), and let an instance have the coefficients \(w_i\), which add up to one. Its cubic is \(\sum_i w_i C_i\) and its spline is \(\sum_i w_i S_i\), both at every parameter, because Bézier curves are linear in their control points. So at equal parameters
which is \(\varepsilon\) when no coefficient is negative, as for an instance between two masters. So if each master is within tolerance at equal parameters, every such instance is.
Nothing like this holds for the distance between the curves as sets of points. Take a master whose cubic is the straight segment from (0, 0) to (1, 0), with its control points a third and two thirds of the way, and whose quadratic is the same segment with its control point at (0.1, 0); and a second master with the segment from (0, 0) to (0, 1), the quadratic’s control point at (0, 0.5). In each master the two curves are the same set of points. Halfway between the masters the cubic is the straight segment to (0.5, 0.5), and the quadratic, with its control point at (0.05, 0.25), bulges 0.07 away from it. Each master’s quadratic runs along its cubic at a pace of its own, and averaging the masters puts together points that were never near each other.
∞ Smooth joints stay smooth
Take an on-curve point where two curves join smoothly, with the handle \(\mathbf{h}^{-}\) arriving and the handle \(\mathbf{h}^{+}\) leaving. Smooth means \(\mathbf{h}^{+} = r\,\mathbf{h}^{-}\) with \(r \gt 0\), the ratio of the two handle lengths. For two masters and the instance at \(\mu\) between them, \(\mathbf{h}^{-} = (1-\mu)\,\mathbf{h}_1^{-} + \mu\,\mathbf{h}_2^{-}\) and \(\mathbf{h}^{+} = (1-\mu)\,r_1\mathbf{h}_1^{-} + \mu\,r_2\mathbf{h}_2^{-}\), and their cross product is
The instance can be smooth only when this is zero: when the two masters have the same ratio of handles at the joint, or handles along the same line (Figure 4). That is the rule for drawing masters that interpolate without kinks, and a converter must not break it.
The spline of \(n \ge 2\) does not break it, for three reasons that are one:
- Every on-curve point it adds is halfway between its two off-curve neighbours. The ratio there is exactly one, in every master, so these joints are smooth in every instance whatever the masters do.
- At the cubic’s own ends, the first handle of the spline is \(\mathbf{o}_0 - \mathbf{c}_0 = \tfrac{3}{2n}\,(\mathbf{c}_1 - \mathbf{c}_0)\), and likewise the last: the cubic’s handle times a number that is the same for all masters, because \(n\) is. Directions do not change, and the ratio across a joint changes by the same factor in every master. Equal ratios stay equal and equal directions stay equal.
- Altogether, for a given \(n\) the spline is one linear map of the control points, the same for every master, so converting and interpolating can be done in either order: the instance of the converted masters is the conversion of the instance.
This is what cutting every master at the same parameters, with the same weights, is for. A conversion that picks its cuts or its control points by the shape of each master, as the intersection of tangents does, gives each master ratios of its own. The 2015 notes say it in one line [4]: curves remain interpolation-compatible if, at every new on-curve point, the ratio of the distances to the control points on its two sides is the same in all masters.
∞ The exception
The single quadratic is the one place where cu2qu is not linear. Its handles are the cubic’s, stretched to reach \(\mathbf{q}\): \(\mathbf{q} - \mathbf{c}_0 = (\mathbf{c}_1 - \mathbf{c}_0)/\lambda_1\) and \(\mathbf{c}_3 - \mathbf{q} = (\mathbf{c}_3 - \mathbf{c}_2)/\lambda_2\), where \(\lambda_1\) and \(\lambda_2\) say how far along the way to \(\mathbf{q}\) the cubic’s control points are. The directions are the cubic’s, so each master stays smooth. But the \(\lambda\) differ from master to master, so equal ratios across a joint do not stay equal, and by the formula above an instance can get a corner where the cubic masters had none, if the masters’ tangents there point in different directions.
How much it does was measured on the masters of 24 variable fonts: Cantarell, Noto Sans CJK and Noto Serif CJK as they are shipped, and the sources of 21 design spaces of other Noto families, of 2 to 16 masters each, converted at a thousandth of the em. They have 3.59 million cubics, of which 12.5% become single quadratics, most of them in Noto Serif CJK. A joint was counted when its two tangents are exactly parallel in every master. Of 1,056,932 such joints, 194,219 have a single quadratic on at least one side. All but 4,939 of those are horizontal or vertical in every master, and there the ratio cannot matter; nearly all of the 4,939 are in the two CJK fonts, which have two masters each. At these, over the instances a quarter, a half and three quarters of the way between every two masters, the conversion changes the angle of the joint by more than a tenth of a degree at 616, by more than half a degree at 70 and by more than a degree at 26; the largest change is 4.4 degrees, at a joint where it reaches 4.6 elsewhere between the same two masters. Rounding the off-curve points to integers, which a glyf table needs anyway, changes the angle by more than those amounts at 2,175, 1,932 and 1,850 of the same joints.
Tangents that are exactly parallel are rare off the axes. With joints counted that are smooth to within a degree in every master, 34,785 that are not horizontal or vertical have a single quadratic beside them; the conversion changes 4,951, 547 and 33 of them by more than those amounts, and rounding 28,885, 22,525 and 18,960.
At the 862,713 exactly smooth joints with no single quadratic beside them, the conversion changes nothing in those instances, to within \(10^{-11}\) of a degree, except at 69 where an instance has a handle of no length: at 50 because the handles of two masters cancel there, at 19 because no master has one. The tangent then comes from the control point further away, which the spline does not keep (section 10), and at 29 of the 69 the angle changes by more than a tenth of a degree. At two more joints a master itself has such a handle, and the angle changes there, by 27 and 11 degrees. So the exception is real, it is small beside rounding, and cu2qu leaves it.
∞ What the measure costs
Being within tolerance at equal parameters is a stronger demand than being within tolerance. For the fonts of section 8, the distance between each cubic and its splines as sets of points was measured. cu2qu’s output is on average 0.27, 0.34 and 0.45 of the tolerance away, at tolerances of one, a half and a tenth of a unit. Had the same constructions been accepted by that distance, they would have taken 1.70, 2.11 and 3.69 quadratics per cubic where cu2qu takes 2.34, 2.84 and 4.80: about a quarter fewer. At a tolerance of one unit, 69% of that saving is in cubics for which one quadratic would do: 6.3% of the cubics pass as one quadratic at equal parameters, and 45.8% are within a unit of that quadratic as curves. At half a unit it is 52% of the saving, and at a tenth 22%.
∞ 7. Without masters: one quadratic by distance
A renderer converts an outline once, for one instance, and nothing is interpolated afterwards. The distance between the curves is then the right measure, and the single quadratic is where it pays most. HarfBuzz’s GPU library, whose shaders take quadratics only, converts the cubics of CFF and CFF2 outlines at a tolerance of half a unit.
Let \(\mathbf{q}\) be where the end tangents meet, \(\mathbf{a} = \mathbf{q} - \mathbf{c}_0\) and \(\mathbf{z} = \mathbf{c}_3 - \mathbf{q}\) the two legs of the quadratic \(Q\), and let the cubic’s control points be \(\mathbf{c}_1 = \mathbf{c}_0 + \lambda_1\mathbf{a}\) and \(\mathbf{c}_2 = \mathbf{c}_3 - \lambda_2\mathbf{z}\). A cubic with \(\lambda_1 = \lambda_2 = \tfrac23\) is the quadratic itself.
Theorem 4. The cubic is the quadratic at another parameter, plus an offset along one fixed direction:
where
Proof. Measured from \(\mathbf{c}_0\), the quadratic is \(Q(\tau) = (2\tau - \tau^2)\,\mathbf{a} + \tau^2\,\mathbf{z}\) and the cubic is \(C(t) = A(t)\,\mathbf{a} + Z(t)\,\mathbf{z}\) with \(A = 3t(1-t)^2\lambda_1 + 3t^2(1-t) + t^3\) and \(Z = 3t^2(1-t)(1-\lambda_2) + t^3\). Choose \(\tau = \tfrac12(A + Z)\). Then \(C(t) - Q(\tau) = (\tau^2 - Z)(\mathbf{a} - \mathbf{z})\), and working out \(\tau\) and \(\tau^2 - Z\) gives the two expressions. ∎
\(\tau\) is a Bézier function with the control values \(0,\ \lambda_1/2,\ 1 - \lambda_2/2,\ 1\). For \(\lambda_1\) and \(\lambda_2\) from 0 to 2 these are between 0 and 1, so \(\tau\) takes the interval from 0 to 1 onto itself: every point of the cubic is \(\lvert\rho\rvert\,\lvert\mathbf{a} - \mathbf{z}\rvert\) from a point of the quadratic, and every point of the quadratic is that far from one of the cubic. As \(t^2(1-t)^2 \le \tfrac1{16}\), the two curves are within
of each other as sets of points. Here \(e\) runs in a straight line from \(\lambda_1/2 - \tfrac13\) to \(\tfrac13 - \lambda_2/2\), so \(e^2\) lies between the smaller of their squares, or zero if they differ in sign, and the larger; the maximum is at one of those two values, and the bound is a few multiplications.
The direction \(\mathbf{a} - \mathbf{z}\) is the axis of the parabola, and its length is twice the distance from \(\mathbf{q}\) to the middle of the chord. So the cubic leaves its quadratic only along the parabola’s axis. The offset vanishes at the middle of the curve when \(\lambda_1 + \lambda_2 = \tfrac43 - \tfrac{3}{16}(\lambda_1 - \lambda_2)^2\). So the cubic stays close when its two handles together reach 4/3 of the way to \(\mathbf{q}\), less a correction that grows with the square of their difference. At equal parameters each has to reach 2/3. The cubic of Figure 5 has 0.27 of the way at one end and 0.97 at the other.
handles 0.27 and 0.97 of the way, 1.24 together. At equal parameters 70.7 apart; as curves 0.74; the bound 2.41.
HarfBuzz now accepts the single quadratic by this bound; where a handle has no length it takes the tangent toward the other control point. Of the cubics of section 8, at its tolerance of half a unit, 24.4% become one quadratic, where 2.4% did at equal parameters; 33.3% are in fact that close to this quadratic. Quadratics per cubic go from 2.84 to 2.59. The bound is never below the measured distance; its median is 1.10 times it. The rule of section 5 has to go when this comes in, since a single quadratic now fits well beyond it. And the bound is not for masters: it says nothing about instances.
∞ 8. In practice
The numbers in this note are for the cubics of three fonts as Fedora 43 ships them: all of Source Code Pro Regular 2.042, and the first 3,000 glyphs of STIX Two Math 2.13 and of Noto Sans SC Regular 2.004. That is 77,533 cubics with integer coordinates, at 1,000 units per em. Half of them have a third difference shorter than 27.7 units, and the longest is 1,574.
| tolerance, units | 1 | 0.5 | 0.1 |
|---|---|---|---|
| cu2qu | 2.336 | 2.838 | 4.802 |
| the same with the best weights (section 4) | 2.322 | 2.763 | 4.291 |
| HarfBuzz’s converter: one quadratic by distance (section 7) | 1.998 | 2.585 | 4.673 |
| mid-point spline, end tangents not kept | 1.688 | 1.984 | 3.037 |
| cu2qu’s construction, accepted by measured distance | 1.703 | 2.107 | 3.694 |
At a tolerance of one unit, 6.3% of the cubics become one quadratic, 65.9% two, 17.6% three, 8.7% four, and 1.6% more, up to eight. HarfBuzz itself draws a cubic as a line when both of its control points are within the tolerance of the line through its ends, before its converter is reached. That is 2.1% of these cubics at half a unit, and takes its 2.585 down to 2.526 segments per cubic.
∞ Where it is
- HarfBuzz. The GPU library took cu2qu’s spline in place of halving single quadratics, then the computed \(n\), then the bound of section 7 [7]. All merged. By its pull request’s measurements, computing \(n\) took 14 to 19% off the instructions spent encoding all glyphs of four fonts with cubic outlines.
- fontTools. Proposed [5]: the computed \(n\), the rule of section 5, the limit on halvings, and NaN.
- kurbo. Proposed [6]: the same, and halving exactly
as cu2qu does, so that the port gives fontTools’ coordinates bit for bit on the fonts above.
Within rounding of a limit the two can still differ by a quadratic, because they take the length
of the third difference differently. Instructions per
approx_splinecall on those fonts go from 2,228 to 714 at a tolerance of one unit, and from 3,180 to 756 at half a unit.
| compiled, before | after | pure Python, before | after | |
|---|---|---|---|---|
curve_to_quadratic, tolerance 1 | 21,118 (1.46) | 15,885 (1.10) | 84,471 (5.63) | 47,427 (3.17) |
curve_to_quadratic, tolerance 0.5 | 28,871 (2.01) | 19,112 (1.34) | 118,338 (8.12) | 56,543 (3.97) |
curves_to_quadratic, tolerance 1 | 67,083 (4.51) | 57,198 (3.83) | 252,984 (17.92) | 156,125 (10.75) |
∞ What changes in the output
Nothing but the ties of section 3. On the 77,533 cubics, fontTools before and after gives the same coordinates for all but 3 cubics at a tolerance of one unit, 7 at half a unit and none at a tenth; each of the ten has a third difference of exactly 128 times the tolerance and goes from four quadratics to three. Converted together, the 3,589,652 groups of compatible masters of section 6 give the same coordinates before and after for all but 142, all in the two CJK fonts and each from four quadratics to three.
∞ 9. Related work
Replacing a Bézier curve by one of lower degree, with the least error at equal parameters, is the old subject of degree reduction: Watkins and Worsey [13], Lachance [14], and Eck [15], who keeps conditions at the ends by way of constrained Chebyshev polynomials. (These three are cited from their abstracts and from what later papers say of them; they were not read for this note.) For a cubic and one quadratic the answers are short. With nothing kept at the ends the least error is \(\lvert\mathbf{d}_3\rvert/32\), by Chebyshev’s cubic. With the end points kept it is \(\tfrac{\sqrt3}{36}\lvert\mathbf{d}_3\rvert\), the case \(n = 1\) of Theorem 3, and the mid-point quadratic reaches it.
Much of what sections 2 to 4 use is in Colomitchi’s note of 2006 [8]:
the distance at equal parameters as the measure; the two candidates, and that they are half the
third difference apart, which is the first statement of Lemma 1; the error 4/27 of the quadratic that
takes one of them; the mid-point quadratic and its error
\(\tfrac{\sqrt3}{36}\lvert\mathbf{d}_3\rvert\); and the cube law for a piece of the cubic, from
which he computes where to cut, a piece at a time. Biły [16] has the same
quadratic and error, the cube law for equal pieces, and the number of pieces as a cube root. kurbo’s
to_quads computes it as
\(\bigl\lceil \sqrt[6]{\lvert\mathbf{d}_3\rvert^2 / (432\,\varepsilon^2)}\, \bigr\rceil\),
and so does lyon, which credits Raph Levien with the derivation
[3] [10]. So computing the count from the third
difference is known for the spline that does not keep the end tangents, where the factor is one
number.
Truong, Yuksel and Seiler [9] replace a cubic by a pair of quadratics that starts and ends with its derivative. They measure the error at equal parameters and show that it is least when the two have equal parameter lengths, where it is 1/54 of the length of the third difference. That pair is cu2qu’s spline of two quadratics, and 1/54 is \(\Phi(2)/2^3\), the first row of Table 1. For more accuracy they cut the cubic into more cubics first, by that error; the alternating weights of section 4 are their pairs on equal pieces.
FontForge’s documentation describes dividing a cubic evenly, taking each control point where the tangents at the two ends of a division meet, and adding points until the result is close [11]. It notes, as something found by trial, that the on-curve points are then midway between the control points, so that TrueType can leave them out. For the points halfway between the candidates that is Lemma 2, and exact; where tangents meet it holds nearly. cubic2quad follows FontForge: it cuts at inflection points, then tries one to eight even divisions, with the error measured at sampled points [12]. Both search.
What this note adds is the error of the spline that starts and ends with the cubic’s derivative, for every \(n\) and every choice of weights (Theorem 1); its largest value for cu2qu’s ramp from three quadratics on (Theorem 2); the bounds of Theorem 3 for every \(n\); and Theorem 4. None of these was found in the sources above.
∞ 10. Loose ends
- Rounding. The bound is for the spline as computed. A glyf table stores integers, and rounding an off-curve point moves it by up to \(\sqrt2/2\) of a unit, and the curve by up to as much.
- Handles of no length. A cubic with a control point on its end point, as 1.1% of the cubics above have, has no handle there for the spline to keep. Its tangent at that end is toward the other control point, and the spline, whose handle there has no length either, leaves toward its own next point instead. The single quadratic can also turn a tangent around, when \(\mathbf{q}\) falls behind the cubic’s end point. That takes a handle shorter than \(2\sqrt3 \approx 3.5\) times the tolerance, or the quadratic would not pass. One joint of the 194,219 of section 6 has it, with a handle one unit long.
- Odd numbers of quadratics. For an even \(n\) the best weights of section 4 are a pattern. For an odd \(n\) they come out of a linear program; no formula for them is given here.
- The single quadratic at equal parameters. Its largest error is at a root of a cubic equation, so it has a closed form too. On the fonts above it decides every cubic as the recursive test does. fontTools and kurbo keep the recursion, which now ends.
- Distance for splines. Section 7 bounds the distance for one quadratic. A bound as simple for the spline of \(n\) would save a renderer more: accepting cu2qu’s splines by their measured distance would take 11%, 15% and 20% off the quadratics that HarfBuzz’s converter makes now, at tolerances of one, a half and a tenth of a unit.
∞ About this note
This note was written by Claude, Anthropic’s AI model, working in Claude Code at Behdad Esfahbod’s direction: the derivations, the code, the measurements, the figures and the text. Appendix B lists the scripts that check its formulas and produce its numbers. Two further instances of the model were then asked to find what is wrong with it. What they found, and what was done about it, is in the notes.
∞ Appendix A. The computation
In Python, as proposed for fontTools. Its g and c are
\(g_1\) and \(\beta\) above, and the table is built once.
import math
MAX_N = 100
def spline_error_factor(n):
"""How far the spline of n > 1 quadratics gets from its cubic at equal
parameters, over the length of the cubic's third difference divided by n**3."""
g = (n - 2) / (4 * (n - 1))
error = (4 / 27) * (1 + g) * (1 + g) * (1 + g) # first and last piece
if n > 3:
c = (n - 3) / (4 * (n - 1))
w = c / 3
error = max(error, c + 2 * w * math.sqrt(w)) # second and next to last
return error
SPLINE_MAX_D3 = [0.0, 0.0] + [
n * n * n / spline_error_factor(n) for n in range(2, MAX_N + 1)
]
def cubic_spline_n(cubic, tolerance):
"""The least number of quadratics, from 2, whose spline is within tolerance
of the cubic (four complex numbers); 0 if more than MAX_N."""
d3 = abs(cubic[3] - 3 * cubic[2] + 3 * cubic[1] - cubic[0])
for n in range(2, MAX_N + 1):
if d3 <= tolerance * SPLINE_MAX_D3[n]:
return n
return 0
∞ Appendix B. Checking the claims
The scripts that produced the numbers and the figures are in github.com/behdad/cu2qu-paper, with their output and the notes kept along the way:
check_math.pychecks the statements of sections 3 to 7: the lemmas and Theorem 1 on random cubics, for the ramp and for random weights; the closed form of \(\Phi\) against the largest value found on a fine grid, for every \(n\) up to 129 and some beyond; the bounds of Theorem 3 against random weights; that converting and interpolating commute; the kink formula; and Theorem 4 and the two identities in the proof of Theorem 3 in exact rational arithmetic.best_weights.pyfinds the best weights of section 4 by linear programming.measure.pymakes Table 2 and the other numbers about the three fonts, from the formulas alone; the distance between curves is measured by sampling one and refining the nearest point on the other with Newton’s method.corpus.pyextracts the cubics.compare_revs.pyruns fontTools before and after on the same cubics and compares both with the formulas, andcompare_revs_masters.pydoes it for the masters of section 6;ties.pycounts what the old test does at a limit;perf_fonttools.pymakes Table 3.check_endless.pyandcheck_endless_kurbo.pyrun the inputs of section 5;compare_kurbo.pycompares kurbo with fontTools bit by bit, andperf_kurbo.pycounts its instructions.kinks.pymeasures the joints of section 6 on the masters.cu2qu-figures.jsdraws the figures, andcheck_js.pycompares its numbers with the Python formulas.
∞ References
- J. Godfrey-Kittle, B. Esfahbod, C. Lupo and others. cu2qu: cubic-to-quadratic Bézier curve conversion. 2015–2020; since part of fontTools.
- fontTools.
fontTools.cu2qu, since version 4.7.0, 2020. - R. Levien and others. kurbo:
CubicBez::to_quads,CubicBez::approx_splineandcubics_to_quadratic_splines. - B. Esfahbod. Add documentation of the algorithm. cu2qu issue 10, 2015.
- fontTools pull request 4452: [cu2qu] Compute the number of quadratics instead of trying. 2026.
- kurbo pull request 625: cu2qu port: fix a stack overflow at the limit, and compute the number of quadratics. 2026.
- HarfBuzz pull requests 6879, 6880, 6881 and 6882. 2026.
- A. Colomitchi. Approximating cubic Bezier curves by quadratic ones. caffeineowl.com, 2006; as archived in 2021.
- N. Truong, C. Yuksel and L. Seiler. Quadratic approximation of cubic curves. Proceedings of the ACM on Computer Graphics and Interactive Techniques 3(2), article 16 (High-Performance Graphics 2020).
- N. Silva and others. lyon_geom:
CubicBezierSegment::num_quadratics. - FontForge documentation. Bézier splines: converting PostScript to TrueType.
- A. Rodin and V. Puzrin. cubic2quad.
- M. A. Watkins and A. J. Worsey. Degree reduction of Bézier curves. Computer-Aided Design 20(7), 398–405, 1988.
- M. A. Lachance. Chebyshev economization for parametric surfaces. Computer Aided Geometric Design 5(3), 195–208, 1988.
- M. Eck. Degree reduction of Bézier curves. Computer Aided Geometric Design 10(3–4), 237–251, 1993.
- B. Biły. On a method of the rapid approximation of a cubic Bézier curve by quadratic Bézier curves. Zeszyty Naukowe Politechniki Śląskiej, Matematyka Stosowana 4, 57–71, 2014.