-
Notifications
You must be signed in to change notification settings - Fork 93
/
tsq.py
293 lines (268 loc) · 8.04 KB
/
tsq.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#########
## TSQ ##
#########
# See https://github.com/cjquines/tsqx for a better version
# Also see https://github.com/vEnhance/dotfiles/blob/main/vim/after/syntax/tsq.vim for a syntax highlighter
import argparse
import string
import sys
fn_names = {
"circumcenter": 3,
"orthocenter": 3,
"incircle": 3,
"circumcircle": 3,
"centroid": 3,
"incenter": 3,
"midpoint": 1,
"extension": 4,
"foot": 3,
"CP": 2,
"CR": 2,
"dir": 1,
"conj": 1,
"intersect": 2,
"IP": 2,
"OP": 2,
"Line": 2,
"bisectorpoint": 2,
"arc": 4,
"abs": 1,
}
short_names = {
"circle": "circumcircle",
"rightangle": "rightanglemark",
}
# The following is really bad
for letter in string.ascii_uppercase:
fn_names["-%s+2*foot" % letter] = 3
fn_names["-%sp+2*foot" % letter] = 3
fn_names["-%ss+2*foot" % letter] = 3
for digit in "0123456789":
fn_names["-%s_%s+2*foot" % (letter, digit)] = 3
def autoParen(tokens):
if len(tokens) == 0:
return ""
else:
t = tokens.pop(0)
if t in short_names:
t = short_names[t]
if t in fn_names:
nargs = fn_names[t]
args = [autoParen(tokens) for i in range(nargs)]
return t + "(" + ", ".join(args) + ")"
else:
return t
# argument parsing
parser = argparse.ArgumentParser(description="Generate a diagram.")
parser.add_argument(
"-p",
"--pre",
help="Adds an Asymptote preamble.",
action="store_true",
dest="preamble",
default=False,
)
parser.add_argument(
"-t",
"--terse",
help="Omits the source code at the end",
action="store_true",
dest="terse",
default=False,
)
parser.add_argument(
"-n",
"--no-trans",
help="Temporarily disables the transparencies.",
action="store_true",
dest="notrans",
default=False,
)
parser.add_argument(
"fname",
help="If provided, reads from the designated file rather than stdin",
metavar="filename",
nargs="?",
default="",
)
parser.add_argument(
"-s",
"--size",
help="If provided, sets the image size in the preamble. (Use with -p.)",
action="store",
dest="size",
default="8cm",
)
parser.add_argument(
"-f",
"--fontsize",
help="If provided, sets the image size in the preamble. (Use with -p.)",
action="store",
dest="fontsize",
default="9pt",
)
opts = parser.parse_args()
# Initialize some stuff
raw_code = ""
dot_code = ""
GENERIC_PREAMBLE = r"""
usepackage("amsmath");
usepackage("amssymb");
settings.tex="pdflatex";
settings.outformat="pdf";
// Replacement for olympiad+cse5 which is not standard
import geometry;
// recalibrate fill and filldraw for conics
void filldraw(picture pic = currentpicture, conic g, pen fillpen=defaultpen, pen drawpen=defaultpen)
{ filldraw(pic, (path) g, fillpen, drawpen); }
void fill(picture pic = currentpicture, conic g, pen p=defaultpen)
{ filldraw(pic, (path) g, p); }
// some geometry
pair foot(pair P, pair A, pair B) { return foot(triangle(A,B,P).VC); }
pair orthocenter(pair A, pair B, pair C) { return orthocentercenter(A,B,C); }
pair centroid(pair A, pair B, pair C) { return (A+B+C)/3; }
// cse5 abbreviations
path CP(pair P, pair A) { return circle(P, abs(A-P)); }
path CR(pair P, real r) { return circle(P, r); }
pair IP(path p, path q) { return intersectionpoints(p,q)[0]; }
pair OP(path p, path q) { return intersectionpoints(p,q)[1]; }
path Line(pair A, pair B, real a=0.6, real b=a) { return (a*(A-B)+A)--(b*(B-A)+B); }
""".strip()
if opts.preamble:
print("defaultpen(fontsize(%s));" % opts.fontsize)
print("size(%s);" % opts.size)
print(GENERIC_PREAMBLE)
if opts.fname != "":
stream = open(opts.fname, "r")
else:
stream = sys.stdin # type: ignore
in_comment_mode = False
# Print output
for line in stream:
line = line.strip()
# Empty line = newspace
if line == "":
print("")
raw_code += line + "\n"
continue
# Handling of comments
if line[:2] == "//":
print(line)
raw_code += line + "\n"
continue
if line[:2] == "/*" and line.endswith("*/"):
print(line)
continue
elif line[:2] == "/*":
in_comment_mode = True
print(line)
continue
elif in_comment_mode and line.endswith("*/"):
in_comment_mode = False
print("*/")
continue
if in_comment_mode:
print(line)
continue
raw_code += line + "\n"
# Verbatim
if line[0] == "!":
print(line[1:].strip())
continue
# Decide whether to auto-paren
if line[0] == ".":
# Force auto paren
do_auto_paren = True
line = line[1:].strip()
elif line[0] == ">":
do_auto_paren = False
line = line[1:].strip()
else:
do_auto_paren = ", " not in line # just default to auto-ing unless , appears
if "=" in line:
raw_name, raw_expr = line.split("=", 2)
if len(raw_name) > 0 and raw_name[-1] == ":":
draw_point = False
label_point = False
raw_name = raw_name[:-1].strip()
elif len(raw_name) > 0 and raw_name[-1] == ".":
draw_point = True
label_point = False
raw_name = raw_name[:-1].strip()
else:
draw_point = True
label_point = True
raw_name = raw_name.strip()
point_name = (
raw_name.replace("'", "p").replace("*", "s").replace("^", "")
) # name used in source code
label_name = raw_name.replace(
"*", r"^\ast"
) # name passed to LaTeX label function
if do_auto_paren:
tokens = raw_expr.strip().split(" ")
expr = autoParen(tokens)
if len(tokens) == 0:
direction = "dir(" + point_name + ")"
elif len(tokens) == 1:
magnitude, angle = tokens[0].split("R", 2)
direction = "dir(" + angle + ")"
if magnitude != "":
direction = magnitude + "*" + direction
else:
raise ValueError(f"Too many tokens in {tokens}")
else:
expr = raw_expr.strip()
direction = "dir(" + point_name + ")"
if point_name != "":
print("pair %s = %s;" % (point_name, expr))
if draw_point:
if label_point:
dot_code += 'dot("$%s$", %s, %s);\n' % (
label_name,
point_name,
direction,
)
else:
dot_code += "dot(%s);\n" % (point_name if point_name else expr)
else:
line = line.strip()
pen = None
if do_auto_paren:
tokens = line.split(" ")
expr = autoParen(tokens)
# 0.2 mediumcyan / blue -> opacity(0.2)+mediumcyan, blue
if "/" in tokens:
tindex = tokens.index("/") # index of transparency divider
if tokens[0][0] == "0": # first token is leading 0
fillpen = "opacity(" + tokens[0] + ")"
if tindex != 1:
fillpen += "+" + "+".join(tokens[1:tindex]) # add on others
else:
fillpen = "+".join(tokens[0:tindex])
drawpen = "+".join(tokens[tindex + 1 :])
if not drawpen:
drawpen = "defaultpen"
if opts.notrans:
print("draw(" + expr + ", " + drawpen + ");")
else:
print("filldraw(" + expr + ", " + fillpen + ", " + drawpen + ");")
else:
pen = "+".join(tokens) # any remaining tokens
else:
expr = line # you'll have to put commas here for pens manually
pen = ""
if pen:
print("draw(" + expr + ", " + pen + ");")
elif pen is not None:
print("draw(" + expr + ");")
print("\n" + dot_code)
if opts.terse:
print("/* Source generated by TSQ */")
else:
print("/* TSQ Source:")
print("")
print(raw_code.strip())
print("")
print("*/")
stream.close()