-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_matrix.py
164 lines (133 loc) · 4.44 KB
/
op_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Built-in matrix and coordinate operators for stilted."""
import cairo
from cairo_util import array_to_cmatrix, cmatrix_to_array
from dtypes import from_py, Array, Real, Integer, Number
from error import Tilted
from evaluate import operator, Engine
from util import deg_to_rad
def pop_matrix(engine: Engine) -> Array:
"""Pop a matrix from the stack."""
arr = engine.opop(Array)
if len(arr) != 6:
raise Tilted("rangecheck")
return arr
def transform_help(engine: Engine, *, invert: bool, distance: bool) -> None:
"""Code common to the four xxtransform operators."""
match engine.otop():
case Array():
mtx = array_to_cmatrix(pop_matrix(engine))
case Integer() | Real():
mtx = engine.gctx.get_matrix()
case _:
raise Tilted("typecheck")
if invert:
try:
mtx.invert()
except cairo.Error:
raise Tilted("undefinedresult")
fn = mtx.transform_distance if distance else mtx.transform_point
x, y = engine.opopn(2, Number)
x, y = fn(x.value, y.value)
engine.opush(from_py(x), from_py(y))
@operator
def concat(engine: Engine) -> None:
mtx = array_to_cmatrix(pop_matrix(engine))
mtx = mtx.multiply(engine.gctx.get_matrix())
engine.gctx.set_matrix(mtx)
@operator
def concatmatrix(engine: Engine) -> None:
arr3 = pop_matrix(engine)
mtx2 = array_to_cmatrix(pop_matrix(engine))
mtx1 = array_to_cmatrix(pop_matrix(engine))
mtx3 = mtx1.multiply(mtx2)
cmatrix_to_array(mtx3, arr3)
engine.opush(arr3)
@operator
def currentmatrix(engine: Engine) -> None:
arr = pop_matrix(engine)
cmatrix_to_array(engine.gctx.get_matrix(), arr)
engine.opush(arr)
@operator
def defaultmatrix(engine: Engine) -> None:
arr = pop_matrix(engine)
cmatrix_to_array(engine.device.default_matrix, arr)
engine.opush(arr)
@operator
def dtransform(engine: Engine) -> None:
transform_help(engine, invert=False, distance=True)
@operator
def identmatrix(engine: Engine) -> None:
arr = pop_matrix(engine)
cmatrix_to_array(cairo.Matrix(), arr)
engine.opush(arr)
@operator
def idtransform(engine: Engine) -> None:
transform_help(engine, invert=True, distance=True)
@operator
def initmatrix(engine: Engine) -> None:
engine.gctx.set_matrix(engine.device.default_matrix)
@operator
def invertmatrix(engine: Engine) -> None:
arr2 = pop_matrix(engine)
arr1 = pop_matrix(engine)
mtx1 = array_to_cmatrix(arr1)
try:
mtx1.invert()
except cairo.Error:
raise Tilted("undefinedresult")
cmatrix_to_array(mtx1, arr2)
engine.opush(arr2)
@operator
def itransform(engine: Engine) -> None:
transform_help(engine, invert=True, distance=False)
@operator
def matrix(engine: Engine) -> None:
arr = engine.new_array(n=6)
cmatrix_to_array(cairo.Matrix(), arr)
engine.opush(arr)
@operator
def rotate(engine: Engine) -> None:
match engine.otop():
case Array():
arr = pop_matrix(engine)
ang = engine.opop(Number)
cmatrix_to_array(cairo.Matrix.init_rotate(deg_to_rad(ang.value)), arr)
engine.opush(arr)
case Integer() | Real():
ang = engine.opop(Number)
engine.gctx.rotate(deg_to_rad(ang.value))
case _:
raise Tilted("typecheck")
@operator
def scale(engine: Engine) -> None:
match engine.otop():
case Array():
arr = pop_matrix(engine)
sx, sy = engine.opopn(2, Number)
cmatrix_to_array(cairo.Matrix(xx=sx.value, yy=sy.value), arr)
engine.opush(arr)
case Integer() | Real():
sx, sy = engine.opopn(2, Number)
engine.gctx.scale(sx.value, sy.value)
case _:
raise Tilted("typecheck")
@operator
def setmatrix(engine: Engine) -> None:
arr = pop_matrix(engine)
engine.gctx.set_matrix(array_to_cmatrix(arr))
@operator
def transform(engine: Engine) -> None:
transform_help(engine, invert=False, distance=False)
@operator
def translate(engine: Engine) -> None:
match engine.otop():
case Array():
arr = pop_matrix(engine)
tx, ty = engine.opopn(2, Number)
cmatrix_to_array(cairo.Matrix(x0=tx.value, y0=ty.value), arr)
engine.opush(arr)
case Integer() | Real():
tx, ty = engine.opopn(2, Number)
engine.gctx.translate(tx.value, ty.value)
case _:
raise Tilted("typecheck")