| 1 |
Bézier Splines [#f1]_ |
| 2 |
===================== |
| 3 |
|
| 4 |
.. image:: /images/splines.gif |
| 5 |
:align: left |
| 6 |
|
| 7 |
Technically PostScript uses cubic Bézier splines. Each control point determines |
| 8 |
the slope of the spline at the corresponding end point. TrueType uses quadratic |
| 9 |
Bézier splines, in these there is only one control point between two end points |
| 10 |
and that point determines the slope of the spline at both end points. Both |
| 11 |
formats also use lines (which are linear Bézier splines). |
| 12 |
|
| 13 |
.. image:: /images/quadbezier.gif |
| 14 |
:align: right |
| 15 |
|
| 16 |
It is also possible to have other degree Bézier splines. For a quartic spline |
| 17 |
there would be 3 control points, for a quintic 4 control points, etc. Of these |
| 18 |
only the linear, quadratic and cubic splines are interesting to us -- those used |
| 19 |
by TrueType and PostScript. |
| 20 |
|
| 21 |
The examples at left are cubic splines with two control points, each determining |
| 22 |
the slope at the nearest end point, while the example at right shows a quadratic |
| 23 |
spline with one control point used to determine the slopes at both end points. |
| 24 |
|
| 25 |
In general if there are :math:`n+1` points labeled :math:`P_0, P_1, ... P_n`, with |
| 26 |
:math:`P_0` and :math:`P_n` the end points (and all the others control points) |
| 27 |
then the equation of the Bézier spline between them is: |
| 28 |
|
| 29 |
.. math:: \sum_{i=0}^{n}{\begin{pmatrix} i \\ n \end{pmatrix} t^i (1-t)^{n-i} P_i} |
| 30 |
|
| 31 |
If there are two points then this is just the line between the two end points, |
| 32 |
if three then the quadratic spline used by TrueType, if four then the cubic |
| 33 |
spline used by PostScript. |
| 34 |
|
| 35 |
A cubic Bézier curve may be viewed as: |
| 36 |
|
| 37 |
.. math:: |
| 38 |
|
| 39 |
x &= a_x t^3 + b_x t^2 + c_x t + d_x \\ |
| 40 |
y &= a_y t^3 + b_y t^2 + c_y t + d_y |
| 41 |
|
| 42 |
Where |
| 43 |
|
| 44 |
.. math:: |
| 45 |
|
| 46 |
\begin{align} |
| 47 |
d_x &= P_{0_x} & d_y &= P_{0_y} \\ |
| 48 |
c_x &= 3 P_{1_x} - 3 P_{0_x} & c_y &= 3 P_{1_y} - 3 P_{0_y} \\ |
| 49 |
b_x &= 3 P_{2_x} - 6 P_{1_x} + 3 P_{0_x} & b_y &= 3 P_{2_y} - 6 P_{1_y} + 3 P_{0_y} \\ |
| 50 |
a_x &= P_{3_x} - 3 P_{2_x} + 3 P_{1_x} - P_{0_x} & a_y &= P_{3_y} - 3 P_{2_y} + 3 P_{1_y} - P_{0_y} |
| 51 |
\end{align} |
| 52 |
|
| 53 |
|
| 54 |
And a quadratic Bézier curve: |
| 55 |
|
| 56 |
.. math:: |
| 57 |
|
| 58 |
x &= b_x t^2 + c_x t + d_x \\ |
| 59 |
y &= b_y t^2 + c_y t + d_y |
| 60 |
|
| 61 |
with |
| 62 |
|
| 63 |
.. math:: |
| 64 |
|
| 65 |
\begin{align} |
| 66 |
d_x &= P_{0_x} & d_y &= P_{0_y} \\ |
| 67 |
c_x &= 2 P_{1_x} - 2 P_{0_x} & c_y &= 2 P_{1_y} - 2 P_{0_y} \\ |
| 68 |
b_x &= P_{2_x} - 2 P_{1_x} + P_{0_x} & b_y &= P_{2_y} - 2 P_{1_y} + P_{0_y} |
| 69 |
\end{align} |
| 70 |
|
| 71 |
And a line: |
| 72 |
|
| 73 |
.. math:: |
| 74 |
|
| 75 |
\begin{align} |
| 76 |
d_x &= P_{0_x} & d_y &= P_{0_y} \\ |
| 77 |
c_x &= P_{1_x} - P_{0_x} & c_y &= P_{1_y} - P_{0_y} |
| 78 |
\end{align} |
| 79 |
|
| 80 |
.. _bezier.ttf2ps: |
| 81 |
|
| 82 |
Converting TrueType to PostScript |
| 83 |
--------------------------------- |
| 84 |
|
| 85 |
Lines convert with no math whatsoever. |
| 86 |
|
| 87 |
Any quadratic spline can be expressed as a cubic (where the cubic term is zero). |
| 88 |
The end points of the cubic will be the same as the quadratic's. |
| 89 |
|
| 90 |
.. math:: |
| 91 |
|
| 92 |
CP_0 &= QP_0 \\ |
| 93 |
CP_3 &= QP_2 |
| 94 |
|
| 95 |
The two control points for the cubic are: |
| 96 |
|
| 97 |
.. math:: |
| 98 |
|
| 99 |
CP_1 &= QP_0 + \frac{2}{3}(QP_1 - QP_0) \\ |
| 100 |
CP_2 &= QP_2 + \frac{2}{3}(QP_1 - QP_2) |
| 101 |
|
| 102 |
So converting from TrueType to PostScript is trivial. There is a slight error |
| 103 |
introduced due to rounding, but it is usually not noticeable. |
| 104 |
|
| 105 |
(Anish Mehta points out that in truetype it is possible to specify points which |
| 106 |
are in the middle of a pixel (ie. with an x or y value ending in 1/2). These |
| 107 |
will also introduce rounding errors when converted to postscript, but here the |
| 108 |
solution is simple: Scale the em-square of the font by a factor of 2 and the |
| 109 |
problem vanishes). |
| 110 |
|
| 111 |
|
| 112 |
.. _bezier.ps2ttf: |
| 113 |
|
| 114 |
Converting PostScript to TrueType |
| 115 |
--------------------------------- |
| 116 |
|
| 117 |
Most cubic splines cannot be represented exactly by a quadratic (or even by a |
| 118 |
series of quadratics). The best that can be done is to approximate the cubic to |
| 119 |
within some margin of error. Here is a way to do that: |
| 120 |
|
| 121 |
Given a cubic spline. Look at two points on the spline. Then there is only one |
| 122 |
possible quadratic spline between those two points which has the desired slope |
| 123 |
at those two points. (the one quadratic has its end points as the end points of |
| 124 |
the interval, and its control point is determined by the intersections of the |
| 125 |
lines tangent to the cubic at the start and end of the interval). |
| 126 |
|
| 127 |
.. image:: /images/cubic2quad.png |
| 128 |
:align: right |
| 129 |
|
| 130 |
I cannot prove this, but have determined empirically (after being told it |
| 131 |
worked) that if a cubic spline -- like the quarter ellipse at right -- is |
| 132 |
divided "evenly" into n quadratic splines (here :math:`n = 3`) then the control |
| 133 |
points will have the nice property that the on-curve points are exactly mid-way |
| 134 |
between control points -- this means that truetype can represent them as |
| 135 |
interpolated points. An even division means means that each sub-point is placed |
| 136 |
where :math:`t = i/n` (where :math:`0<i<n`). |
| 137 |
|
| 138 |
FontForge first checks to see if the curve is already a quadratic -- this might |
| 139 |
happen if the cubic originally came from a truetype font. If it is it simply |
| 140 |
converts the control point. |
| 141 |
|
| 142 |
Otherwise FontForge start inserting points. It begins by inserting a single |
| 143 |
point mid-way on the spline. If this produces a good approximation it stops, |
| 144 |
otherwise it tries again with two, three ... additional points. |
| 145 |
|
| 146 |
On each test it will round the control points to integer values because truetype |
| 147 |
output requires this (note that it does not do this to the on-curve points, they |
| 148 |
are interpolated between control points and will be ok if the control points |
| 149 |
are). Then it checks to see if the resultant quadratics are close to the |
| 150 |
original curve. |
| 151 |
|
| 152 |
FontForge may also break the curve at its points of inflection (because no |
| 153 |
quadratic can have one there should be a point there to start a new quadratic) |
| 154 |
and apply the above algorithm to the components. |
| 155 |
|
| 156 |
If FontForge can find no good approximation using the above algorithm it will |
| 157 |
fall back to its old method, which is: |
| 158 |
|
| 159 |
Start from the end of the spline and every so often (ie. within the margin of |
| 160 |
error) check to see if the one permissible quadratic approximation actually |
| 161 |
matches the cubic closely enough . |
| 162 |
|
| 163 |
If this approximation works then keep it as part of the result, and continue the |
| 164 |
process by advancing our start point along to the cubic spline to the end of the |
| 165 |
quadratic we just created. |
| 166 |
|
| 167 |
(There are some slight complexities introduced because there may not be a |
| 168 |
quadratic approximation at a given point (if the tangents happen to be parallel) |
| 169 |
or because the approximation happens to be linear, but these are easily dealt |
| 170 |
with). |
| 171 |
|
| 172 |
It may, of course, happen that the "cubic" we are given is actually a quadratic |
| 173 |
(if its third degree term is 0), the most likely cause is that the font came |
| 174 |
from a truetype source. In that case the control point for the quadratic is at: |
| 175 |
|
| 176 |
.. math:: QP_1 = CP_0 + \frac{3}{2}(CP_1 - CP_0) |
| 177 |
|
| 178 |
Other sources I have read on the net suggest checking the cubic spline for |
| 179 |
points of inflection (which quadratic splines cannot have) and forcing breaks |
| 180 |
there. To my eye this actually makes the result worse, it uses more points and |
| 181 |
the approximation does not look as close as it does when ignoring the points of |
| 182 |
inflection. So I ignore them. |
| 183 |
|
| 184 |
|
| 185 |
Open Type, another solution |
| 186 |
--------------------------- |
| 187 |
|
| 188 |
Adobe and Microsoft decided to produce one font format which could hold either a |
| 189 |
true type font or a postscript font. This is called Open Type. It is essentially |
| 190 |
a superset of TrueType. Any TrueType font is a valid Open Type font, but Open |
| 191 |
Type fonts can also contain postscript. Anything that supports Open Type will |
| 192 |
not require converting between PostScript and True Type. |
| 193 |
|
| 194 |
|
| 195 |
See Also |
| 196 |
^^^^^^^^ |
| 197 |
|
| 198 |
* :doc:`FontForge's math </techref/pfaeditmath>` |
| 199 |
|
| 200 |
|
| 201 |
.. rubric:: Footnotes |
| 202 |
|
| 203 |
.. [#f1] Bézier splines were developed by Pierre Bézier (1910-1999). |