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
|
#!/usr/bin/env python3
###########################################################################
#
# xasyFile implements the loading, parsing, and saving of an asy file.
#
#
# Author: Orest Shardt
# Created: June 29, 2007
#
############################################################################
from string import *
import xasy2asy as xasy2asy
import io
import re
class xasyParseError(Exception):
"""A parsing error"""
pass
class xasyFileError(Exception):
"""An i/o error or other error not related to parsing"""
pass
def extractTransform(line):
"""Returns key and the new transform."""
# see https://regex101.com/r/6DqkRJ/4 for info
mapString = xasy2asy.xasyItem.mapString
testMatch = re.match(
r'^{0:s}\s*\(\s*\"([^\"]+)\"\s*,\s*\(([-\d, .]+)\)\s*\)'.format(mapString), line.strip())
if testMatch is None:
mapOnlyMatch = re.match(r'^{0:s}\s*\(\s *\"([^\"]+)\"\s*\)'.format(mapString), line.strip())
if mapOnlyMatch is None:
return None
else:
key = mapOnlyMatch.group(1)
return key, xasy2asy.identity()
else:
key = testMatch.group(1)
rawStr = testMatch.group(2)
rawStrArray = rawStr.split(',')
if len(rawStrArray) != 6:
return None
transf = [float(val.strip()) for val in rawStrArray]
return key, xasy2asy.asyTransform(transf)
def extractTransformsFromFile(fileStr):
transfDict = {}
maxItemCount = 0
with io.StringIO() as rawCode:
for line in fileStr.splitlines():
test_transf = extractTransform(line.rstrip())
if test_transf is None:
rawCode.write(line + '\n')
else:
key, transf = test_transf
if key not in transfDict.keys():
transfDict[key] = []
transfDict[key].append(transf)
final_str = rawCode.getvalue()
return final_str, transfDict
def xasy2asyCode(xasyItems, asy2psmap):
asyCode = ''
for item in xasyItems:
asyCode += item.getTransformCode(asy2psmap)
for item in xasyItems:
asyCode += item.getObjectCode(asy2psmap)
asyCode += 'size('+str(asy2psmap*xasy2asy.yflip())+'); '+ xasy2asy.xasyItem.resizeComment+'\n'
return asyCode
def saveFile(file, xasyItems, asy2psmap):
"""Write a list of xasyItems to a file"""
file.write(xasy2asyCode(xasyItems, asy2psmap))
def xasyToDict(file, xasyItems, asy2psmap):
fileItems = []
asyItems = []
for item in xasyItems:
if isinstance(item, xasy2asy.xasyScript):
# reusing xasyFile code for objects
# imported from asy script.
asyItems.append({'item':item, 'type': 'xasyScript'})
elif isinstance(item, xasy2asy.xasyText):
# At the moment xasyText cannot be edited
# so we treat it the same as xasyScript
penData = {'color': item.pen.color, 'width': item.pen.width, 'options': item.pen.options}
fileItems.append({'type': 'xasyText',
'align': item.label.align,
'location': item.label.location,
'fontSize': item.label.fontSize,
'text': item.label.text,
'transform': item.transfKeymap[item.transfKey][0].t,
'transfKey': item.transfKey,
'pen': penData
})
elif isinstance(item, xasy2asy.xasyShape):
penData = {'color': item.pen.color, 'width': item.pen.width, 'options': item.pen.options}
fileItems.append({'type': 'xasyShape',
'nodes': item.path.nodeSet,
'links': item.path.linkSet,
'transform': item.transfKeymap[item.transfKey][0].t,
'transfKey': item.transfKey,
'pen': penData
})
else:
# DEBUGGING PURPOSES ONLY
print(type(item))
return {'objects': fileItems, 'asy2psmap': asy2psmap.t}, asyItems
|