-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvg2ReGIS.py
125 lines (110 loc) · 4.13 KB
/
svg2ReGIS.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
from svgpathtools import svg2paths2
from svgpathtools.parser import parse_transform
from svgpathtools.path import transform as path_transform
import re
from math import ceil
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('svgfile')
parser.add_argument(
"-s", "--scale", help="Ammount to scale image. Provide 0 as argument to fit the window", type=float)
parser.add_argument(
"-x", "--xhome", help="Origin point for X", type=int, default=0)
parser.add_argument(
"-y", "--yhome", help="Origin point for Y", type=int, default=0)
parser.add_argument(
"-f", "--fill", help="Flag. Set to fill in polygons", action='store_true')
parser.add_argument(
"-c", "--dontclear", help="Flag. Set to disable clearing screen before starting to draw", action='store_true')
parser.add_argument(
"-e", "--escape", help="Flag. Set to replace ascii ESC character with escapped version (\\033)", action='store_true')
def head_to(x, y, draw=True):
x = x + args.xhome
y = y+ args.yhome
if draw:
string = "V[" + str(round(x)) + "," + str(round(y)) + "]"
else:
string = "\nP[" + str(round(x)) + "," + str(round(y)) + "]"
return string
def draw_polygon(poly, fill=False):
p = poly[0]
stringLocal = head_to(p[0], (p[1]), draw=False)
if fill:
stringLocal += "\nF("
for p in poly[1:]:
stringLocal += head_to(p[0], (p[1]))
if fill:
stringLocal += ")"
return stringLocal
def draw_multipolygon(mpoly, fill=False):
p = mpoly[0][0]
stringLocal = head_to(p[0], (p[1]), draw=False)
for i, poly in enumerate(mpoly):
stringLocal += draw_polygon(poly, fill)
if i != 0:
stringLocal += head_to(p[0], (p[1]), draw=False)
return stringLocal
args = parser.parse_args()
svg_file = args.svgfile
windowSize = {'width': 800, 'height': 480}
orig_paths, orig_attrs, svg_attr = svg2paths2(svg_file)
if args.scale is not None:
if 'viewBox' in svg_attr:
origWidth = svg_attr['viewBox'].split(' ')[2]
origHeight = svg_attr['viewBox'].split(' ')[3]
elif 'width' in svg_attr:
origWidth = re.sub('[A-Za-z]','',svg_attr['width'])
origHeight = re.sub('[A-Za-z]','',svg_attr['height'])
if args.scale == 0: #'width' in svg_attr and
origWidthFloat = float(origWidth)
origHeightFloat = float(origHeight)
if (windowSize['width'] / origWidthFloat) < (windowSize['height'] / origHeightFloat):
# limited by width
scaleRatio = (windowSize['width'] - 10) / origWidthFloat
else:
# limited by height
scaleRatio = (windowSize['height'] - 10) / origHeightFloat
else:
scaleRatio = args.scale
paths = []
attrs = []
for i, (path, attribute) in enumerate(zip(orig_paths, orig_attrs)):
new_path = path_transform(path, parse_transform(
'scale('+str(scaleRatio)+' '+str(scaleRatio)+')'))
orig_attrs[i]['d'] = new_path.d() # to make it consistent
paths.append(new_path)
attrs.append(orig_attrs[i])
else:
paths = orig_paths
attrs = orig_attrs
seg_res = 5
polys = []
for path in paths:
poly = []
for subpaths in path.continuous_subpaths():
points = []
for seg in subpaths:
interp_num = ceil(seg.length()/seg_res)
points.append(seg.point(np.arange(interp_num)/interp_num))
points = np.concatenate(points)
points = np.append(points, points[0])
poly.append(points)
polys.append([[(p.real, p.imag) for p in pl] for pl in poly])
if args.escape:
# Start regis command, clear screen, set cursor on.
RegisString = "\\033P1p"
else:
# Start regis command, clear screen, set cursor on.
RegisString = "\033P1p"
if args.dontclear:
RegisString += "S(I0,C1)"
else:
RegisString += "S(I0,C1,E)"
for poly, attr in zip(polys, attrs):
RegisString += draw_multipolygon(poly, fill=args.fill)
if args.escape:
RegisString += "\n\\033\\"
else:
RegisString += "\n\033\\"
print(RegisString)