xref: /aosp_15_r20/external/fonttools/Lib/fontTools/cu2qu/cu2qu.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1# cython: language_level=3
2# distutils: define_macros=CYTHON_TRACE_NOGIL=1
3
4# Copyright 2015 Google Inc. All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18try:
19    import cython
20
21    COMPILED = cython.compiled
22except (AttributeError, ImportError):
23    # if cython not installed, use mock module with no-op decorators and types
24    from fontTools.misc import cython
25
26    COMPILED = False
27
28import math
29
30from .errors import Error as Cu2QuError, ApproxNotFoundError
31
32
33__all__ = ["curve_to_quadratic", "curves_to_quadratic"]
34
35MAX_N = 100
36
37NAN = float("NaN")
38
39
40@cython.cfunc
41@cython.inline
42@cython.returns(cython.double)
43@cython.locals(v1=cython.complex, v2=cython.complex)
44def dot(v1, v2):
45    """Return the dot product of two vectors.
46
47    Args:
48        v1 (complex): First vector.
49        v2 (complex): Second vector.
50
51    Returns:
52        double: Dot product.
53    """
54    return (v1 * v2.conjugate()).real
55
56
57@cython.cfunc
58@cython.inline
59@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
60@cython.locals(
61    _1=cython.complex, _2=cython.complex, _3=cython.complex, _4=cython.complex
62)
63def calc_cubic_points(a, b, c, d):
64    _1 = d
65    _2 = (c / 3.0) + d
66    _3 = (b + c) / 3.0 + _2
67    _4 = a + d + c + b
68    return _1, _2, _3, _4
69
70
71@cython.cfunc
72@cython.inline
73@cython.locals(
74    p0=cython.complex, p1=cython.complex, p2=cython.complex, p3=cython.complex
75)
76@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
77def calc_cubic_parameters(p0, p1, p2, p3):
78    c = (p1 - p0) * 3.0
79    b = (p2 - p1) * 3.0 - c
80    d = p0
81    a = p3 - d - c - b
82    return a, b, c, d
83
84
85@cython.cfunc
86@cython.inline
87@cython.locals(
88    p0=cython.complex, p1=cython.complex, p2=cython.complex, p3=cython.complex
89)
90def split_cubic_into_n_iter(p0, p1, p2, p3, n):
91    """Split a cubic Bezier into n equal parts.
92
93    Splits the curve into `n` equal parts by curve time.
94    (t=0..1/n, t=1/n..2/n, ...)
95
96    Args:
97        p0 (complex): Start point of curve.
98        p1 (complex): First handle of curve.
99        p2 (complex): Second handle of curve.
100        p3 (complex): End point of curve.
101
102    Returns:
103        An iterator yielding the control points (four complex values) of the
104        subcurves.
105    """
106    # Hand-coded special-cases
107    if n == 2:
108        return iter(split_cubic_into_two(p0, p1, p2, p3))
109    if n == 3:
110        return iter(split_cubic_into_three(p0, p1, p2, p3))
111    if n == 4:
112        a, b = split_cubic_into_two(p0, p1, p2, p3)
113        return iter(
114            split_cubic_into_two(a[0], a[1], a[2], a[3])
115            + split_cubic_into_two(b[0], b[1], b[2], b[3])
116        )
117    if n == 6:
118        a, b = split_cubic_into_two(p0, p1, p2, p3)
119        return iter(
120            split_cubic_into_three(a[0], a[1], a[2], a[3])
121            + split_cubic_into_three(b[0], b[1], b[2], b[3])
122        )
123
124    return _split_cubic_into_n_gen(p0, p1, p2, p3, n)
125
126
127@cython.locals(
128    p0=cython.complex,
129    p1=cython.complex,
130    p2=cython.complex,
131    p3=cython.complex,
132    n=cython.int,
133)
134@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
135@cython.locals(
136    dt=cython.double, delta_2=cython.double, delta_3=cython.double, i=cython.int
137)
138@cython.locals(
139    a1=cython.complex, b1=cython.complex, c1=cython.complex, d1=cython.complex
140)
141def _split_cubic_into_n_gen(p0, p1, p2, p3, n):
142    a, b, c, d = calc_cubic_parameters(p0, p1, p2, p3)
143    dt = 1 / n
144    delta_2 = dt * dt
145    delta_3 = dt * delta_2
146    for i in range(n):
147        t1 = i * dt
148        t1_2 = t1 * t1
149        # calc new a, b, c and d
150        a1 = a * delta_3
151        b1 = (3 * a * t1 + b) * delta_2
152        c1 = (2 * b * t1 + c + 3 * a * t1_2) * dt
153        d1 = a * t1 * t1_2 + b * t1_2 + c * t1 + d
154        yield calc_cubic_points(a1, b1, c1, d1)
155
156
157@cython.cfunc
158@cython.inline
159@cython.locals(
160    p0=cython.complex, p1=cython.complex, p2=cython.complex, p3=cython.complex
161)
162@cython.locals(mid=cython.complex, deriv3=cython.complex)
163def split_cubic_into_two(p0, p1, p2, p3):
164    """Split a cubic Bezier into two equal parts.
165
166    Splits the curve into two equal parts at t = 0.5
167
168    Args:
169        p0 (complex): Start point of curve.
170        p1 (complex): First handle of curve.
171        p2 (complex): Second handle of curve.
172        p3 (complex): End point of curve.
173
174    Returns:
175        tuple: Two cubic Beziers (each expressed as a tuple of four complex
176        values).
177    """
178    mid = (p0 + 3 * (p1 + p2) + p3) * 0.125
179    deriv3 = (p3 + p2 - p1 - p0) * 0.125
180    return (
181        (p0, (p0 + p1) * 0.5, mid - deriv3, mid),
182        (mid, mid + deriv3, (p2 + p3) * 0.5, p3),
183    )
184
185
186@cython.cfunc
187@cython.inline
188@cython.locals(
189    p0=cython.complex,
190    p1=cython.complex,
191    p2=cython.complex,
192    p3=cython.complex,
193)
194@cython.locals(
195    mid1=cython.complex,
196    deriv1=cython.complex,
197    mid2=cython.complex,
198    deriv2=cython.complex,
199)
200def split_cubic_into_three(p0, p1, p2, p3):
201    """Split a cubic Bezier into three equal parts.
202
203    Splits the curve into three equal parts at t = 1/3 and t = 2/3
204
205    Args:
206        p0 (complex): Start point of curve.
207        p1 (complex): First handle of curve.
208        p2 (complex): Second handle of curve.
209        p3 (complex): End point of curve.
210
211    Returns:
212        tuple: Three cubic Beziers (each expressed as a tuple of four complex
213        values).
214    """
215    mid1 = (8 * p0 + 12 * p1 + 6 * p2 + p3) * (1 / 27)
216    deriv1 = (p3 + 3 * p2 - 4 * p0) * (1 / 27)
217    mid2 = (p0 + 6 * p1 + 12 * p2 + 8 * p3) * (1 / 27)
218    deriv2 = (4 * p3 - 3 * p1 - p0) * (1 / 27)
219    return (
220        (p0, (2 * p0 + p1) / 3.0, mid1 - deriv1, mid1),
221        (mid1, mid1 + deriv1, mid2 - deriv2, mid2),
222        (mid2, mid2 + deriv2, (p2 + 2 * p3) / 3.0, p3),
223    )
224
225
226@cython.cfunc
227@cython.inline
228@cython.returns(cython.complex)
229@cython.locals(
230    t=cython.double,
231    p0=cython.complex,
232    p1=cython.complex,
233    p2=cython.complex,
234    p3=cython.complex,
235)
236@cython.locals(_p1=cython.complex, _p2=cython.complex)
237def cubic_approx_control(t, p0, p1, p2, p3):
238    """Approximate a cubic Bezier using a quadratic one.
239
240    Args:
241        t (double): Position of control point.
242        p0 (complex): Start point of curve.
243        p1 (complex): First handle of curve.
244        p2 (complex): Second handle of curve.
245        p3 (complex): End point of curve.
246
247    Returns:
248        complex: Location of candidate control point on quadratic curve.
249    """
250    _p1 = p0 + (p1 - p0) * 1.5
251    _p2 = p3 + (p2 - p3) * 1.5
252    return _p1 + (_p2 - _p1) * t
253
254
255@cython.cfunc
256@cython.inline
257@cython.returns(cython.complex)
258@cython.locals(a=cython.complex, b=cython.complex, c=cython.complex, d=cython.complex)
259@cython.locals(ab=cython.complex, cd=cython.complex, p=cython.complex, h=cython.double)
260def calc_intersect(a, b, c, d):
261    """Calculate the intersection of two lines.
262
263    Args:
264        a (complex): Start point of first line.
265        b (complex): End point of first line.
266        c (complex): Start point of second line.
267        d (complex): End point of second line.
268
269    Returns:
270        complex: Location of intersection if one present, ``complex(NaN,NaN)``
271        if no intersection was found.
272    """
273    ab = b - a
274    cd = d - c
275    p = ab * 1j
276    try:
277        h = dot(p, a - c) / dot(p, cd)
278    except ZeroDivisionError:
279        return complex(NAN, NAN)
280    return c + cd * h
281
282
283@cython.cfunc
284@cython.returns(cython.int)
285@cython.locals(
286    tolerance=cython.double,
287    p0=cython.complex,
288    p1=cython.complex,
289    p2=cython.complex,
290    p3=cython.complex,
291)
292@cython.locals(mid=cython.complex, deriv3=cython.complex)
293def cubic_farthest_fit_inside(p0, p1, p2, p3, tolerance):
294    """Check if a cubic Bezier lies within a given distance of the origin.
295
296    "Origin" means *the* origin (0,0), not the start of the curve. Note that no
297    checks are made on the start and end positions of the curve; this function
298    only checks the inside of the curve.
299
300    Args:
301        p0 (complex): Start point of curve.
302        p1 (complex): First handle of curve.
303        p2 (complex): Second handle of curve.
304        p3 (complex): End point of curve.
305        tolerance (double): Distance from origin.
306
307    Returns:
308        bool: True if the cubic Bezier ``p`` entirely lies within a distance
309        ``tolerance`` of the origin, False otherwise.
310    """
311    # First check p2 then p1, as p2 has higher error early on.
312    if abs(p2) <= tolerance and abs(p1) <= tolerance:
313        return True
314
315    # Split.
316    mid = (p0 + 3 * (p1 + p2) + p3) * 0.125
317    if abs(mid) > tolerance:
318        return False
319    deriv3 = (p3 + p2 - p1 - p0) * 0.125
320    return cubic_farthest_fit_inside(
321        p0, (p0 + p1) * 0.5, mid - deriv3, mid, tolerance
322    ) and cubic_farthest_fit_inside(mid, mid + deriv3, (p2 + p3) * 0.5, p3, tolerance)
323
324
325@cython.cfunc
326@cython.inline
327@cython.locals(tolerance=cython.double)
328@cython.locals(
329    q1=cython.complex,
330    c0=cython.complex,
331    c1=cython.complex,
332    c2=cython.complex,
333    c3=cython.complex,
334)
335def cubic_approx_quadratic(cubic, tolerance):
336    """Approximate a cubic Bezier with a single quadratic within a given tolerance.
337
338    Args:
339        cubic (sequence): Four complex numbers representing control points of
340            the cubic Bezier curve.
341        tolerance (double): Permitted deviation from the original curve.
342
343    Returns:
344        Three complex numbers representing control points of the quadratic
345        curve if it fits within the given tolerance, or ``None`` if no suitable
346        curve could be calculated.
347    """
348
349    q1 = calc_intersect(cubic[0], cubic[1], cubic[2], cubic[3])
350    if math.isnan(q1.imag):
351        return None
352    c0 = cubic[0]
353    c3 = cubic[3]
354    c1 = c0 + (q1 - c0) * (2 / 3)
355    c2 = c3 + (q1 - c3) * (2 / 3)
356    if not cubic_farthest_fit_inside(0, c1 - cubic[1], c2 - cubic[2], 0, tolerance):
357        return None
358    return c0, q1, c3
359
360
361@cython.cfunc
362@cython.locals(n=cython.int, tolerance=cython.double)
363@cython.locals(i=cython.int)
364@cython.locals(all_quadratic=cython.int)
365@cython.locals(
366    c0=cython.complex, c1=cython.complex, c2=cython.complex, c3=cython.complex
367)
368@cython.locals(
369    q0=cython.complex,
370    q1=cython.complex,
371    next_q1=cython.complex,
372    q2=cython.complex,
373    d1=cython.complex,
374)
375def cubic_approx_spline(cubic, n, tolerance, all_quadratic):
376    """Approximate a cubic Bezier curve with a spline of n quadratics.
377
378    Args:
379        cubic (sequence): Four complex numbers representing control points of
380            the cubic Bezier curve.
381        n (int): Number of quadratic Bezier curves in the spline.
382        tolerance (double): Permitted deviation from the original curve.
383
384    Returns:
385        A list of ``n+2`` complex numbers, representing control points of the
386        quadratic spline if it fits within the given tolerance, or ``None`` if
387        no suitable spline could be calculated.
388    """
389
390    if n == 1:
391        return cubic_approx_quadratic(cubic, tolerance)
392    if n == 2 and all_quadratic == False:
393        return cubic
394
395    cubics = split_cubic_into_n_iter(cubic[0], cubic[1], cubic[2], cubic[3], n)
396
397    # calculate the spline of quadratics and check errors at the same time.
398    next_cubic = next(cubics)
399    next_q1 = cubic_approx_control(
400        0, next_cubic[0], next_cubic[1], next_cubic[2], next_cubic[3]
401    )
402    q2 = cubic[0]
403    d1 = 0j
404    spline = [cubic[0], next_q1]
405    for i in range(1, n + 1):
406        # Current cubic to convert
407        c0, c1, c2, c3 = next_cubic
408
409        # Current quadratic approximation of current cubic
410        q0 = q2
411        q1 = next_q1
412        if i < n:
413            next_cubic = next(cubics)
414            next_q1 = cubic_approx_control(
415                i / (n - 1), next_cubic[0], next_cubic[1], next_cubic[2], next_cubic[3]
416            )
417            spline.append(next_q1)
418            q2 = (q1 + next_q1) * 0.5
419        else:
420            q2 = c3
421
422        # End-point deltas
423        d0 = d1
424        d1 = q2 - c3
425
426        if abs(d1) > tolerance or not cubic_farthest_fit_inside(
427            d0,
428            q0 + (q1 - q0) * (2 / 3) - c1,
429            q2 + (q1 - q2) * (2 / 3) - c2,
430            d1,
431            tolerance,
432        ):
433            return None
434    spline.append(cubic[3])
435
436    return spline
437
438
439@cython.locals(max_err=cython.double)
440@cython.locals(n=cython.int)
441@cython.locals(all_quadratic=cython.int)
442def curve_to_quadratic(curve, max_err, all_quadratic=True):
443    """Approximate a cubic Bezier curve with a spline of n quadratics.
444
445    Args:
446        cubic (sequence): Four 2D tuples representing control points of
447            the cubic Bezier curve.
448        max_err (double): Permitted deviation from the original curve.
449        all_quadratic (bool): If True (default) returned value is a
450            quadratic spline. If False, it's either a single quadratic
451            curve or a single cubic curve.
452
453    Returns:
454        If all_quadratic is True: A list of 2D tuples, representing
455        control points of the quadratic spline if it fits within the
456        given tolerance, or ``None`` if no suitable spline could be
457        calculated.
458
459        If all_quadratic is False: Either a quadratic curve (if length
460        of output is 3), or a cubic curve (if length of output is 4).
461    """
462
463    curve = [complex(*p) for p in curve]
464
465    for n in range(1, MAX_N + 1):
466        spline = cubic_approx_spline(curve, n, max_err, all_quadratic)
467        if spline is not None:
468            # done. go home
469            return [(s.real, s.imag) for s in spline]
470
471    raise ApproxNotFoundError(curve)
472
473
474@cython.locals(l=cython.int, last_i=cython.int, i=cython.int)
475@cython.locals(all_quadratic=cython.int)
476def curves_to_quadratic(curves, max_errors, all_quadratic=True):
477    """Return quadratic Bezier splines approximating the input cubic Beziers.
478
479    Args:
480        curves: A sequence of *n* curves, each curve being a sequence of four
481            2D tuples.
482        max_errors: A sequence of *n* floats representing the maximum permissible
483            deviation from each of the cubic Bezier curves.
484        all_quadratic (bool): If True (default) returned values are a
485            quadratic spline. If False, they are either a single quadratic
486            curve or a single cubic curve.
487
488    Example::
489
490        >>> curves_to_quadratic( [
491        ...   [ (50,50), (100,100), (150,100), (200,50) ],
492        ...   [ (75,50), (120,100), (150,75),  (200,60) ]
493        ... ], [1,1] )
494        [[(50.0, 50.0), (75.0, 75.0), (125.0, 91.66666666666666), (175.0, 75.0), (200.0, 50.0)], [(75.0, 50.0), (97.5, 75.0), (135.41666666666666, 82.08333333333333), (175.0, 67.5), (200.0, 60.0)]]
495
496    The returned splines have "implied oncurve points" suitable for use in
497    TrueType ``glif`` outlines - i.e. in the first spline returned above,
498    the first quadratic segment runs from (50,50) to
499    ( (75 + 125)/2 , (120 + 91.666..)/2 ) = (100, 83.333...).
500
501    Returns:
502        If all_quadratic is True, a list of splines, each spline being a list
503        of 2D tuples.
504
505        If all_quadratic is False, a list of curves, each curve being a quadratic
506        (length 3), or cubic (length 4).
507
508    Raises:
509        fontTools.cu2qu.Errors.ApproxNotFoundError: if no suitable approximation
510        can be found for all curves with the given parameters.
511    """
512
513    curves = [[complex(*p) for p in curve] for curve in curves]
514    assert len(max_errors) == len(curves)
515
516    l = len(curves)
517    splines = [None] * l
518    last_i = i = 0
519    n = 1
520    while True:
521        spline = cubic_approx_spline(curves[i], n, max_errors[i], all_quadratic)
522        if spline is None:
523            if n == MAX_N:
524                break
525            n += 1
526            last_i = i
527            continue
528        splines[i] = spline
529        i = (i + 1) % l
530        if i == last_i:
531            # done. go home
532            return [[(s.real, s.imag) for s in spline] for spline in splines]
533
534    raise ApproxNotFoundError(curves)
535