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