Debian Codegraph @

doc/sphinx/techref/pfaeditmath.rst

raw
1 FontForge's math
2 ================
3
4 Being a brief description of the mathematics underlying various of FontForge's
5 commands
6
7 It is presumed that you understand about parameterized splines, if not look at
8 the description of :doc:`Bézier curves <bezier>`.
9
10 * :ref:`Linear Transformations <pfaeditmath.Linear>`
11 * :ref:`Finding maxima and minima of a spline <pfaeditmath.maxima>`
12 * :ref:`Rasterizing a Glyph <pfaeditmath.Rasterizing>`
13
14 * finding intersections
15 * removing overlap
16 * :ref:`Approximating a spline <pfaeditmath.Approximating>`
17 * :ref:`Stroking a spline (historical) <pfaeditmath.Stroke>`
18 * :ref:`Approximating a cubic spline by a series of quadratic splines <bezier.ps2ttf>`
19
20 --------------------------------------------------------------------------------
21
22
23 .. _pfaeditmath.Linear:
24
25 Linear Transformations
26 ----------------------
27
28 A linear transformation is one where the spline described by transforming the
29 end and control points will match the transformed spline. This includes most of
30 the common transformations you might wish:
31
32 .. rubric:: translation
33
34 .. math::
35
36 x' &= x + \mathrm{d} x \\
37 y' &= y + \mathrm{d} y
38
39 .. rubric:: scaling
40
41 .. math::
42
43 x' &= s_x x \\
44 y' &= s_y y
45
46 .. rubric:: rotation
47
48 .. math::
49
50 x' &= \cos(A) x + \sin(A) y \\
51 y' &= -\sin(A) x + \cos(A) y
52
53 .. rubric:: skewing
54
55 .. math::
56
57 x' &= x + \sin(A) y \\
58 y' &= y
59
60
61 --------------------------------------------------------------------------------
62
63
64 .. _pfaeditmath.maxima:
65
66 Finding maxima and minima of a spline
67 -------------------------------------
68
69 The maximum or minimum of a spline (along either the x or y axes) may be found
70 by taking the first derivative of that spline with respect to t. So if we have a
71 spline
72
73 .. math::
74
75 x &= a_x t^3 + b_x t^2 + c_x t + d_x \\
76 y &= a_y t^3 + b_y t^2 + c_y t + d_y
77
78 and we wish to find the maximum point with respect to the x axis we set:
79
80 .. math::
81
82 \frac{\mathrm{d}x}{\mathrm{d}y} &= 0 \\
83 3 a_x t^2 + 2 b-x t + c_x &= 0
84
85 and then using the quadratic formula we can solve for t:
86
87 .. math::
88
89 t = \frac{-2 b_x \pm \sqrt{4b_x^2 - 4 \cdot 3a_x c_x}}{2 \cdot 3a_x}
90
91 --------------------------------------------------------------------------------
92
93
94 .. _pfaeditmath.POI:
95
96 Finding points of inflection of a spline
97 ----------------------------------------
98
99 A point of inflection occurs when :math:`\frac{\mathrm{d}^2y}{\mathrm{d}x^2} = 0`
100 (or infinity).
101
102 Unfortunately this does not mean that :math:`\frac{\mathrm{d}^2y}{\mathrm{d}t^2} = 0`
103 or :math:`\frac{\mathrm{d}^2x}{\mathrm{d}t^2} = 0`.
104
105 ..
106 TODO: Someone verify this? Seems a bit iffy
107
108 .. math::
109
110 \frac{\mathrm{d}^2y}{\mathrm{d}x^2} &=
111 \frac{
112 \frac{\mathrm{d}}{\mathrm{d}t}
113 \left(
114 \frac
115 {\frac{\mathrm{d}y}{\mathrm{d}t}}
116 {\frac{\mathrm{d}x}{\mathrm{d}t}}
117 \right)
118 }{\frac{\mathrm{d}x}{\mathrm{d}t}} \\
119 &
120 \frac{
121 \frac{\mathrm{d}x}{\mathrm{d}t} \frac{\mathrm{d}^2y}{\mathrm{d}t^2} -
122 \frac{\mathrm{d}y}{\mathrm{d}t} \frac{\mathrm{d}^2x}{\mathrm{d}t^2}
123 }{\left( \frac{\mathrm{d}x}{\mathrm{d}t} \right)^3}
124
125 After a lot of algebra this boils down to the quadratic in t:
126
127 .. math::
128
129 3(a_x b_y - a_y b_x)t^2 + 3(c_x a_y - c_y a_x)t + c_x b_y - c_y b_x = 0
130
131 If you examine this closely you will note that a quadratic spline
132 (:math:`a_y = a_x = 0`) can never have a point of inflection.
133
134 --------------------------------------------------------------------------------
135
136
137 .. _pfaeditmath.Rasterizing:
138
139 Rasterizing a glyph
140 -------------------
141
142
143 .. _pfaeditmath.Approximating:
144
145 Approximating a spline
146 ----------------------
147
148 Many of FontForge's commands need to fit a spline to a series of points. The
149 most obvious of these are the :menuselection:`Edit --> Merge`, and
150 :menuselection:`Element --> Simplify` commands, but many others rely on the same
151 technique. Let us consider the case of the Merge command, suppose we have the
152 following splines and we wish to remove the middle point and generate a new
153 spline that approximates the original two:
154
155 |mergepre| --> |mergepost|
156
157 .. |mergepre| image:: /images/mergepre.png
158 .. |mergepost| image:: /images/mergepost.png
159
160 FontForge uses a least squares approximation to determine the new spline. It
161 calculates the locations of several points along the old splines, and then it
162 guesses [#f1]_ at t values for those points.
163
164 Now a cubic :doc:`Bézier <bezier>` spline is determined by its two end points
165 (:math:`P_0` and :math:`P_1`) and two control points (:math:`CP_0` and :math:`CP_1`),
166 which specify the slope at those end points). Here we know the end points, so
167 all we need is to find the control points. The spline can also be expressed as a
168 cubic polynomial:
169
170 .. math:: S(t) = a t^3 + b t^2 + c t + d
171
172 with
173
174 .. math::
175
176 d &= P_0 \\
177 c &= 3 CP_0 - 3 P_0 \\
178 b &= 3 CP_1 - 6 CP_0 + 3 P_0 \\
179 a &= P_1 - 3 CP_1 + 3 CP_0 - P_0
180
181 substituting
182
183 .. math::
184
185 S(t) =& (P_1 - 3CP_1 + 3CP_0 - P_0) t^3 + \\
186 & (3CP_1 - 6CP_0 + 3P_0) t^2 + \\
187 & (3CP_0 - 3P_0) t + \\
188 & P_0
189
190 rearranging
191
192 .. math::
193
194 S(t) &= (-3t^3 + 3t^2) CP_1 + \\
195 & (3t^3 - 6t^2 + 3t) CP_0 + \\
196 & (P_1 - P_0)t^3 + 3P_0 t^2 - 3P_0 t + P_0
197
198 Now we want to minimize the sum of the squares of the difference between the
199 value we approximate with the new spline, :math:`S(t_i)`, and the actual value we
200 were given, :math:`P_i`.
201
202 .. math:: \sum [ S(t_i) - P_i ]^2
203
204 Now we have four unknown variables, the x and y coordinates of the two control
205 points. To find the minimum of the above error term we must take the derivative
206 with respect to each variable, and set that to zero. Then we have four equations
207 with four unknowns and we can solve for each variable.
208
209 .. math::
210
211 \sum 2 ( -3t^3 + 3t^2 ) [ S_x(t_i) - P_{i_x} ] = 0 \\
212 \sum 2 ( -3t^3 + 3t^2 ) [ S_y(t_i) - P_{i_y} ] = 0 \\
213 \sum 2 ( 3t^3 - 6t^2 + 3t ) [ S_x(t_i) - P_{i_x} ] = 0 \\
214 \sum 2 ( 3t^3 - 6t^2 + 3t ) [ S_y(t_i) - P_{i_y} ] = 0
215
216 Happily for us, the x and y terms do not interact and my be solved separately.
217 The procedure is the same for each coordinate, so I shall only show one:
218
219 .. math::
220
221 \sum 2 ( -3t^3 + 3t^2 ) [ S_x(t_i) - P_{i_x} ] = 0 \\
222 \sum 2 ( 3t^3 - 6t^2 + 3t ) [ S_x(t_i) - P_{i_x} ] = 0\\
223 \Rightarrow
224
225 .. math::
226
227 \sum \left[
228 (-3t^3 + 3t^2)CP_{1_x} + \\
229 (-3t^3 + 3t^2)(3t^3 - 6t^2 + 3t)CP_{0_x} + \\
230 (P_{1_x} - P_{0_x})t^3 + 3P_{0_x}t^2 - 3P_{0_x}t + P_{0_x} - P_{i_x}
231 \right] = 0
232
233 .. math::
234
235 CP_{1_x} \sum ( -3t^3 + 3t^2 ) ( -3t^3 + 3t^2 ) = \\
236 -\sum ( -3t^3 + 3t^2 )
237 [
238 ( 3t^3 - 6t^2 + 3t ) CP_{0_x} + \\
239 ( P_{1_x} - P_{0_x} ) t^3 + 3P_{0_x}t^2 - 3P_{0_x}t + P_{0_x} - P_{i_x}
240 ]
241
242 .. math::
243
244 CP_{1_x} = -\frac{
245 \sum ( -3t^3 + 3t^2 )
246 [
247 ( 3t^3 - 6t^2 + 3t ) CP_{0_x} + \\
248 ( P_{1_x} - P_{0_x} ) t^3 + 3P_{0_x}t^2 - 3P_{0_x}t + P_{0_x} - P_{i_x}
249 ]
250 }{
251 \sum ( -3t^3 + 3t^2 ) ( -3t^3 + 3t^2 )
252 }
253
254 Now this can be plugged into the other equation
255
256 .. math::
257
258 \sum ( 3t^3 - 6t^2 + 3t )
259 [
260 ( -3t^3 + 3t^2 ) CP_{1_x} + \\
261 ( 3t^3 - 6t^2 + 3t ) CP_{0_x} + \\
262 ( P_{1_x} - P_{0_x} ) t^3 + 3P_{0_x}t^2 - 3P_{0_x}t + P_{0_x} - P_{i_x}
263 ]
264 = 0
265
266 And we can solve for :math:`CP_{0_x}` and then :math:`CP_{1_x}`. The algebra
267 becomes very messy, with lots of terms, but the concepts are simple.
268
269 Thus we know where the control points are. We know where the end points are. We
270 have our spline.
271
272
273 Why that didn't (quite) work
274 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
275
276 The above matrix yields a curve which is a good approximation to the original
277 two. But it has one flaw: There is no constraint placed on the slopes, and
278 (surprisingly) the slopes at the end-points of the above method are not close
279 enough to those of the original, and the human eye can now detect the join
280 between this generated spline and the two that connect to it.
281
282 Generally we will know the slopes at the end points as well as the end points
283 themselves.
284
285 Let's try another approach, based on better geometry. Givens:
286
287 * the start point
288 * the slope at the start point
289 * the end point
290 * the slope at the end point
291
292 We want to find the two control points. Now it may seem that specifying the
293 slope specifies the control point but this is not so, it merely specifies the
294 direction in which the control point lies. The control point may be anywhere
295 along the line through the start point in that direction, and each position will
296 give a different curve.
297
298 So we can express the control point by saying it is :math:`CP_0 = P_0 + r_0 \Delta_0`
299 where :math:`\Delta_0` is a vector in the direction of the slope, and :math:`r_0`
300 is the distance in that direction. Similarly for the end point:
301 :math:`CP_1 = P_1 + r_1 \Delta_1`
302
303 We want to find :math:`r_0` and :math:`r_1`.
304
305 Converting from bezier control points into a polynomial gives us
306
307 .. math::
308
309 S(t) &= at^3 + bt^2 + ct + d \\
310 d &= P_0 \\
311 c &= 3(CP_0 - P_0) \\
312 b &= 3(CP_1 - CP_0) - c \\
313 a &= P_1 - P_0 - c -b
314
315 Substituting we get
316
317 .. math::
318 d &= P_0 \\
319 c &= 3r_0 \Delta_0 \\
320 b &= 3( P_1 - P_0 + r_1 \Delta_1 - 2r_0 \Delta_0 ) \\
321 a &= 2( P_0 - P_1 ) + 3 ( r_0 \Delta_0 - r_1 \Delta_1 )
322
323 For least squares we want to minimize :math:`\sum(S(t_i) - P_i)^2`. Taking
324 derivatives with both :math:`r_0` and :math:`r_1`, we get:
325
326 .. math::
327
328 \sum 2(3t^3 - 6t^2 + 3t)\Delta_0[S(t_i) - P_i] &= 0 \\
329 \sum 2(-3t^3 + 3t^2)\Delta_1[S(t_i) - P_i] &= 0
330
331 dividing by constants and substituting, we get
332
333 .. math::
334
335 \sum (3t^3 - 6t^2 + 3t) [
336 P_0 - P_i + 3(P_1 - P_0)t^2 + \\
337 2(P_0-P_1)t^3 + \\
338 \Delta_0(3t - 6t^2 + 3t^3)r_0 + \\
339 \Delta_1(3t^2 - 3t^3)r_1
340 ] = 0
341
342 .. math::
343
344 \sum (-3t^3 + 3t^2) [
345 P_0 - P_i + 3(P_1 - P_0)t^2 + \\
346 2(P_0 - P_1)t^3 + \\
347 \Delta_0(3t - 6t^2 + 3t^3)r_0 + \\
348 \Delta_1(3t^2 - 3t^3)r_1
349 ] = 0
350
351 Again we have two linear equations in two unknowns (:math:`r_0`, and
352 :math:`r_1`), and (after a lot of algebraic grubbing) we can solve for them.
353 One interesting thing here is that the x and y terms do not separate but must
354 be summed together.
355
356
357 Singular matrices
358 ^^^^^^^^^^^^^^^^^
359
360 Very occasionally, a singular matrix will pop out of these equations. Then what
361 I do is calculate the slope vectors at the endpoints and then try many
362 reasonable lengths for those vectors and see which yields the best approximation
363 to the original curve (this gives us our new control points).
364
365 This is very, very slow.
366
367 --------------------------------------------------------------------------------
368
369
370 .. [#f1] Guessing values for t
371
372 FontForge approximates the lengths of the two splines being merged. If
373 :math:`\mathrm{Point}_i = \mathrm{Spline1}(old-t_i)`, then we approximate
374 :math:`t_i` by
375
376 .. math::
377
378 t_i = old - t_i \frac{\mathrm{len}(\mathrm{spline1})}
379 {\mathrm{len}(\mathrm{spline1}) + \mathrm{len}(\mathrm{spline2})}
380
381 and if :math:`\mathrm{Point}_i = \mathrm{Spline2(old-t_i)}`
382
383 .. math::
384
385 t_i = \frac{\mathrm{len}(\mathrm{spline1})}
386 {\mathrm{len}(\mathrm{spline1}) + \mathrm{len}(\mathrm{spline2})} +
387 old-t_i
388 \frac{\mathrm{len}(\mathrm{spline2})}
389 {\mathrm{len}(\mathrm{spline1}) + \mathrm{len}(\mathrm{spline2})}
390
391 That is we do a linear interpolation of t based on the relative lengths of the
392 two splines.
393
394 --------------------------------------------------------------------------------
395
396
397 .. _pfaeditmath.Stroke:
398
399 Calculating the outline of a stroked path
400 -----------------------------------------
401
402 .. note::
403 While some of this section is still accurate, most of it describes an
404 earlier version of the Expand Stroke facility. See :doc:`here <stroke>`
405 for an up-to-date description of the current algorithm.
406
407
408 A circular pen
409 ^^^^^^^^^^^^^^
410
411 PostScript supports several variants on the theme of a circular pen, and
412 FontForge tries to emulate them all. Basically PostScript "stroke"s a path at a
413 certain width by:
414
415 | at every location on the curve
416 | find the normal vector at that location
417 | find the two points which are width/2 away from the curve
418 | filling in between those two points
419 | end
420
421 This is essentially what a circular pen does. The only aberrations appear at the
422 end-points of a contour, or at points where two splines join but their slopes
423 are not continuous. PostScript allows the user to specify the behavior at joints
424 and at end-points.
425
426 |expand-pre| --> |expand-post|
427
428 FontForge can't deal with an infinite number of locations, so it samples the
429 curve (approximately every em unit), and finds the two normal points. These are
430 on the edge of the area to be stroked, so FontForge can approximate new contours
431 from these edge points (using the
432 :ref:`above algorithm <pfaeditmath.Approximating>`).
433
434 PostScript pens can end in
435
436 * A butt edge -- this is easy, we just draw a line from the end of one spline to
437 the end of the other
438 * A rounded edge -- here we just draw a semi-circle (making sure it goes in the
439 right direction).
440 * A square edge -- just draw lines continuing the two splines, moving with the
441 same slope and width/2 units long, and then join those end-points with a
442 straight line.
443
444 Things are a bit more complex at a joint |expand-joint-pre| --> |expand-joint-post|
445 , the green lines in the right image show where the path would have gone had it
446 not been constrained by a joint, so on the inside of the joint FontForge must
447 figure out where this intersection occurs. While on the outside FontForge must
448 figure out either a round, miter or bevelled edge.
449
450 Unfortunately, the normal points are not always on the edge of the filled
451 region. If the curve makes a sharp bend, then one of the normal points may end
452 up inside the pen when it is centered somewhere else on the original contour
453 (similar to the case of a joint above).
454
455 So FontForge makes another pass through the edge points and figures out which
456 ones are actually internal points. After that it will approximate contours.
457
458 Now if we start with an open contour, (a straight line, for example) then we
459 will end up with one closed contour. While if we start with a closed contour we
460 will end up with two closed contours (one on the inside, and one on the
461 outside). Unless there are places where the curve doubles back on itself, then
462 when can get arbitrarily many closed contours.
463
464 .. |expand-pre| image:: /images/expand-pre.png
465 .. |expand-post| image:: /images/expand-post.png
466 .. |expand-joint-pre| image:: /images/expand-joint-pre.png
467 .. |expand-joint-post| image:: /images/expand-joint-post.png
468
469
470 An elliptical pen
471 ^^^^^^^^^^^^^^^^^
472
473 This is really just the same as a circular pen. Let us say we want an ellipse
474 which is twice as wide as it is high. Then before stroking the path, let's scale
475 it to 50% in the horizontal direction, then stroke it with a circular pen, and
476 then scale it back by 200% horizontally. The result will be as if we had used an
477 elliptical pen.
478
479 Obviously if the ellipse is at an angle to the glyph's axes, we must apply a
480 more complicated transformation which involves both rotation and scaling.
481
482
483 A rectangular pen (a calligraphic pen)
484 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
485
486 Things are subtly different between a rectangular pen and a circular pen. We can
487 no longer just find the points which are a given distance away and normal to the
488 curve. Except where the spline is parallel to one edge of the pen, a the outer
489 contour of a rectangular pen will be stroked by one of its end-points. So all we
490 need do is figure out where a spline is parallel to the pen's sides, and look at
491 the problem in little chunks between those difficult points.
492
493 If we are between difficult points then life is very simple indeed. The edge
494 will always be stroked by the same end-point, which is a fixed distance from the
495 center of the pen, so all we need to do is translate the original spline by this
496 distance (and then fix it up so that t goes from [0,1], but that's another easy
497 transformation).
498
499 When we reach a point where the spline's slope is parallel to one edge of the
500 pen, then on the outside path we draw a copy of that edge of of the pen, and on
501 the inside edge we calculate a join as above.
502
503
504 An arbitrary convex polygonal pen
505 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
506
507 The same method which works for a rectangle can be extended without too much
508 difficulty to any convex polygon. (MetaFont fonts can be drawn with such a pen.
509 I don't know if any are)
510
511
512 A pen of variable width
513 ^^^^^^^^^^^^^^^^^^^^^^^
514
515 FontForge does not currently support this (some of the assumptions behind this
516 algorithm are broken if the pen changes shape too rapidly).
517
518
519 A pen at a varying angle
520 ^^^^^^^^^^^^^^^^^^^^^^^^
521
522 FontForge does not support this.