Bézier Splines [#f1]_ ===================== .. image:: /images/splines.gif :align: left Technically PostScript uses cubic Bézier splines. Each control point determines the slope of the spline at the corresponding end point. TrueType uses quadratic Bézier splines, in these there is only one control point between two end points and that point determines the slope of the spline at both end points. Both formats also use lines (which are linear Bézier splines). .. image:: /images/quadbezier.gif :align: right It is also possible to have other degree Bézier splines. For a quartic spline there would be 3 control points, for a quintic 4 control points, etc. Of these only the linear, quadratic and cubic splines are interesting to us -- those used by TrueType and PostScript. The examples at left are cubic splines with two control points, each determining the slope at the nearest end point, while the example at right shows a quadratic spline with one control point used to determine the slopes at both end points. In general if there are :math:`n+1` points labeled :math:`P_0, P_1, ... P_n`, with :math:`P_0` and :math:`P_n` the end points (and all the others control points) then the equation of the Bézier spline between them is: .. math:: \sum_{i=0}^{n}{\begin{pmatrix} i \\ n \end{pmatrix} t^i (1-t)^{n-i} P_i} If there are two points then this is just the line between the two end points, if three then the quadratic spline used by TrueType, if four then the cubic spline used by PostScript. A cubic Bézier curve may be viewed as: .. math:: x &= a_x t^3 + b_x t^2 + c_x t + d_x \\ y &= a_y t^3 + b_y t^2 + c_y t + d_y Where .. math:: \begin{align} d_x &= P_{0_x} & d_y &= P_{0_y} \\ c_x &= 3 P_{1_x} - 3 P_{0_x} & c_y &= 3 P_{1_y} - 3 P_{0_y} \\ 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} \\ 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} \end{align} And a quadratic Bézier curve: .. math:: x &= b_x t^2 + c_x t + d_x \\ y &= b_y t^2 + c_y t + d_y with .. math:: \begin{align} d_x &= P_{0_x} & d_y &= P_{0_y} \\ c_x &= 2 P_{1_x} - 2 P_{0_x} & c_y &= 2 P_{1_y} - 2 P_{0_y} \\ b_x &= P_{2_x} - 2 P_{1_x} + P_{0_x} & b_y &= P_{2_y} - 2 P_{1_y} + P_{0_y} \end{align} And a line: .. math:: \begin{align} d_x &= P_{0_x} & d_y &= P_{0_y} \\ c_x &= P_{1_x} - P_{0_x} & c_y &= P_{1_y} - P_{0_y} \end{align} .. _bezier.ttf2ps: Converting TrueType to PostScript --------------------------------- Lines convert with no math whatsoever. Any quadratic spline can be expressed as a cubic (where the cubic term is zero). The end points of the cubic will be the same as the quadratic's. .. math:: CP_0 &= QP_0 \\ CP_3 &= QP_2 The two control points for the cubic are: .. math:: CP_1 &= QP_0 + \frac{2}{3}(QP_1 - QP_0) \\ CP_2 &= QP_2 + \frac{2}{3}(QP_1 - QP_2) So converting from TrueType to PostScript is trivial. There is a slight error introduced due to rounding, but it is usually not noticeable. (Anish Mehta points out that in truetype it is possible to specify points which are in the middle of a pixel (ie. with an x or y value ending in 1/2). These will also introduce rounding errors when converted to postscript, but here the solution is simple: Scale the em-square of the font by a factor of 2 and the problem vanishes). .. _bezier.ps2ttf: Converting PostScript to TrueType --------------------------------- Most cubic splines cannot be represented exactly by a quadratic (or even by a series of quadratics). The best that can be done is to approximate the cubic to within some margin of error. Here is a way to do that: Given a cubic spline. Look at two points on the spline. Then there is only one possible quadratic spline between those two points which has the desired slope at those two points. (the one quadratic has its end points as the end points of the interval, and its control point is determined by the intersections of the lines tangent to the cubic at the start and end of the interval). .. image:: /images/cubic2quad.png :align: right I cannot prove this, but have determined empirically (after being told it worked) that if a cubic spline -- like the quarter ellipse at right -- is divided "evenly" into n quadratic splines (here :math:`n = 3`) then the control points will have the nice property that the on-curve points are exactly mid-way between control points -- this means that truetype can represent them as interpolated points. An even division means means that each sub-point is placed where :math:`t = i/n` (where :math:`0` .. rubric:: Footnotes .. [#f1] Bézier splines were developed by Pierre Bézier (1910-1999).