xref: /aosp_15_r20/external/fonttools/Tests/misc/arrayTools_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.misc.arrayTools import (
2*e1fe3e4aSElliott Hughes    calcBounds,
3*e1fe3e4aSElliott Hughes    calcIntBounds,
4*e1fe3e4aSElliott Hughes    updateBounds,
5*e1fe3e4aSElliott Hughes    pointInRect,
6*e1fe3e4aSElliott Hughes    pointsInRect,
7*e1fe3e4aSElliott Hughes    vectorLength,
8*e1fe3e4aSElliott Hughes    asInt16,
9*e1fe3e4aSElliott Hughes    normRect,
10*e1fe3e4aSElliott Hughes    scaleRect,
11*e1fe3e4aSElliott Hughes    offsetRect,
12*e1fe3e4aSElliott Hughes    insetRect,
13*e1fe3e4aSElliott Hughes    sectRect,
14*e1fe3e4aSElliott Hughes    unionRect,
15*e1fe3e4aSElliott Hughes    rectCenter,
16*e1fe3e4aSElliott Hughes    intRect,
17*e1fe3e4aSElliott Hughes)
18*e1fe3e4aSElliott Hughesimport math
19*e1fe3e4aSElliott Hughes
20*e1fe3e4aSElliott Hughes
21*e1fe3e4aSElliott Hughesdef test_calcBounds():
22*e1fe3e4aSElliott Hughes    assert calcBounds([]) == (0, 0, 0, 0)
23*e1fe3e4aSElliott Hughes    assert calcBounds([(0, 40), (0, 100), (50, 50), (80, 10)]) == (0, 10, 80, 100)
24*e1fe3e4aSElliott Hughes
25*e1fe3e4aSElliott Hughes
26*e1fe3e4aSElliott Hughesdef test_calcIntBounds():
27*e1fe3e4aSElliott Hughes    assert calcIntBounds([(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (78.5, 9.5)]) == (
28*e1fe3e4aSElliott Hughes        0,
29*e1fe3e4aSElliott Hughes        10,
30*e1fe3e4aSElliott Hughes        79,
31*e1fe3e4aSElliott Hughes        100,
32*e1fe3e4aSElliott Hughes    )
33*e1fe3e4aSElliott Hughes
34*e1fe3e4aSElliott Hughes    assert calcIntBounds(
35*e1fe3e4aSElliott Hughes        [(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (78.5, 9.5)], round=round
36*e1fe3e4aSElliott Hughes    ) == (0, 10, 78, 100)
37*e1fe3e4aSElliott Hughes
38*e1fe3e4aSElliott Hughes
39*e1fe3e4aSElliott Hughesdef test_updateBounds():
40*e1fe3e4aSElliott Hughes    assert updateBounds((0, 0, 0, 0), (100, 100)) == (0, 0, 100, 100)
41*e1fe3e4aSElliott Hughes
42*e1fe3e4aSElliott Hughes
43*e1fe3e4aSElliott Hughesdef test_pointInRect():
44*e1fe3e4aSElliott Hughes    assert pointInRect((50, 50), (0, 0, 100, 100))
45*e1fe3e4aSElliott Hughes    assert pointInRect((0, 0), (0, 0, 100, 100))
46*e1fe3e4aSElliott Hughes    assert pointInRect((100, 100), (0, 0, 100, 100))
47*e1fe3e4aSElliott Hughes    assert not pointInRect((101, 100), (0, 0, 100, 100))
48*e1fe3e4aSElliott Hughes
49*e1fe3e4aSElliott Hughes
50*e1fe3e4aSElliott Hughesdef test_pointsInRect():
51*e1fe3e4aSElliott Hughes    assert pointsInRect([], (0, 0, 100, 100)) == []
52*e1fe3e4aSElliott Hughes    assert pointsInRect(
53*e1fe3e4aSElliott Hughes        [(50, 50), (0, 0), (100, 100), (101, 100)], (0, 0, 100, 100)
54*e1fe3e4aSElliott Hughes    ) == [True, True, True, False]
55*e1fe3e4aSElliott Hughes
56*e1fe3e4aSElliott Hughes
57*e1fe3e4aSElliott Hughesdef test_vectorLength():
58*e1fe3e4aSElliott Hughes    assert vectorLength((1, 1)) == math.sqrt(2)
59*e1fe3e4aSElliott Hughes
60*e1fe3e4aSElliott Hughes
61*e1fe3e4aSElliott Hughesdef test_asInt16():
62*e1fe3e4aSElliott Hughes    assert asInt16([0, 0.1, 0.5, 0.9]) == [0, 0, 1, 1]
63*e1fe3e4aSElliott Hughes
64*e1fe3e4aSElliott Hughes
65*e1fe3e4aSElliott Hughesdef test_normRect():
66*e1fe3e4aSElliott Hughes    assert normRect((0, 10, 100, 200)) == (0, 10, 100, 200)
67*e1fe3e4aSElliott Hughes    assert normRect((100, 200, 0, 10)) == (0, 10, 100, 200)
68*e1fe3e4aSElliott Hughes
69*e1fe3e4aSElliott Hughes
70*e1fe3e4aSElliott Hughesdef test_scaleRect():
71*e1fe3e4aSElliott Hughes    assert scaleRect((10, 20, 50, 150), 1.5, 2) == (15.0, 40, 75.0, 300)
72*e1fe3e4aSElliott Hughes
73*e1fe3e4aSElliott Hughes
74*e1fe3e4aSElliott Hughesdef test_offsetRect():
75*e1fe3e4aSElliott Hughes    assert offsetRect((10, 20, 30, 40), 5, 6) == (15, 26, 35, 46)
76*e1fe3e4aSElliott Hughes
77*e1fe3e4aSElliott Hughes
78*e1fe3e4aSElliott Hughesdef test_insetRect():
79*e1fe3e4aSElliott Hughes    assert insetRect((10, 20, 50, 60), 5, 10) == (15, 30, 45, 50)
80*e1fe3e4aSElliott Hughes    assert insetRect((10, 20, 50, 60), -5, -10) == (5, 10, 55, 70)
81*e1fe3e4aSElliott Hughes
82*e1fe3e4aSElliott Hughes
83*e1fe3e4aSElliott Hughesdef test_sectRect():
84*e1fe3e4aSElliott Hughes    intersects, rect = sectRect((0, 10, 20, 30), (0, 40, 20, 50))
85*e1fe3e4aSElliott Hughes    assert not intersects
86*e1fe3e4aSElliott Hughes
87*e1fe3e4aSElliott Hughes    intersects, rect = sectRect((0, 10, 20, 30), (5, 20, 35, 50))
88*e1fe3e4aSElliott Hughes    assert intersects
89*e1fe3e4aSElliott Hughes    assert rect == (5, 20, 20, 30)
90*e1fe3e4aSElliott Hughes
91*e1fe3e4aSElliott Hughes
92*e1fe3e4aSElliott Hughesdef test_unionRect():
93*e1fe3e4aSElliott Hughes    assert unionRect((0, 10, 20, 30), (0, 40, 20, 50)) == (0, 10, 20, 50)
94*e1fe3e4aSElliott Hughes
95*e1fe3e4aSElliott Hughes
96*e1fe3e4aSElliott Hughesdef test_rectCenter():
97*e1fe3e4aSElliott Hughes    assert rectCenter((0, 0, 100, 200)) == (50.0, 100.0)
98*e1fe3e4aSElliott Hughes    assert rectCenter((0, 0, 100, 199.0)) == (50.0, 99.5)
99*e1fe3e4aSElliott Hughes
100*e1fe3e4aSElliott Hughes
101*e1fe3e4aSElliott Hughesdef test_intRect():
102*e1fe3e4aSElliott Hughes    assert intRect((0.9, 2.9, 3.1, 4.1)) == (0, 2, 4, 5)
103