1# Copyright 2023 Behdad Esfahbod. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import unittest 16import pytest 17 18from fontTools.qu2cu import quadratic_to_curves 19from fontTools.qu2cu.qu2cu import main as qu2cu_main 20from fontTools.qu2cu.benchmark import main as benchmark_main 21 22import os 23import json 24from fontTools.cu2qu import curve_to_quadratic 25 26 27class Qu2CuTest: 28 @pytest.mark.parametrize( 29 "quadratics, expected, tolerance, cubic_only", 30 [ 31 ( 32 [ 33 [(0, 0), (0, 1), (2, 1), (2, 0)], 34 ], 35 [ 36 ((0, 0), (0, 4 / 3), (2, 4 / 3), (2, 0)), 37 ], 38 0.1, 39 True, 40 ), 41 ( 42 [ 43 [(0, 0), (0, 1), (2, 1), (2, 2)], 44 ], 45 [ 46 ((0, 0), (0, 4 / 3), (2, 2 / 3), (2, 2)), 47 ], 48 0.2, 49 True, 50 ), 51 ( 52 [ 53 [(0, 0), (0, 1), (1, 1)], 54 [(1, 1), (3, 1), (3, 0)], 55 ], 56 [ 57 ((0, 0), (0, 1), (1, 1)), 58 ((1, 1), (3, 1), (3, 0)), 59 ], 60 0.2, 61 False, 62 ), 63 ( 64 [ 65 [(0, 0), (0, 1), (1, 1)], 66 [(1, 1), (3, 1), (3, 0)], 67 ], 68 [ 69 ((0, 0), (0, 2 / 3), (1 / 3, 1), (1, 1)), 70 ((1, 1), (7 / 3, 1), (3, 2 / 3), (3, 0)), 71 ], 72 0.2, 73 True, 74 ), 75 ], 76 ) 77 def test_simple(self, quadratics, expected, tolerance, cubic_only): 78 expected = [ 79 tuple((pytest.approx(p[0]), pytest.approx(p[1])) for p in curve) 80 for curve in expected 81 ] 82 83 c = quadratic_to_curves(quadratics, tolerance, cubic_only) 84 assert c == expected 85 86 def test_roundtrip(self): 87 DATADIR = os.path.join(os.path.dirname(__file__), "..", "cu2qu", "data") 88 with open(os.path.join(DATADIR, "curves.json"), "r") as fp: 89 curves = json.load(fp) 90 91 tolerance = 1 92 93 splines = [curve_to_quadratic(c, tolerance) for c in curves] 94 reconsts = [quadratic_to_curves([spline], tolerance) for spline in splines] 95 96 for curve, reconst in zip(curves, reconsts): 97 assert len(reconst) == 1 98 curve = tuple((pytest.approx(p[0]), pytest.approx(p[1])) for p in curve) 99 assert curve == reconst[0] 100 101 def test_main(self): 102 # Just for coverage 103 qu2cu_main() 104 benchmark_main() 105