Basic website with home page, contact/feedback forms (project links do nothing)
[ndcode_site.git] / _svg / rescale.py
1 #!/usr/bin/env python3
2
3 import re
4 import sys
5
6 EXIT_SUCCESS = 0
7 EXIT_FAILURE = 1
8
9 if len(sys.argv) < 2:
10   print(f'usage: {argv[0]:s} scale')
11   sys.exit(EXIT_FAILURE)
12 scale = float(sys.argv[1])
13
14 re_scalar = re.compile('( *(cx|cy|dx|dy|r|stroke-width|x1|x2|y1|y2)=")([^"]*)(".*)')
15 re_scalar2 = re.compile('( *stdDeviation=")([^ "]*)( ([^ "]*))?(".*)')
16 re_viewbox = re.compile('( *viewBox="0 0 )([^ "]*) ([^ "]*)(".*)')
17 re_path = re.compile('( *d=")([^"]*)(".*)')
18 re_path_command = re.compile('([A-Za-z,]) *(.*)')
19 re_path_number = re.compile('(-?([0-9]*\.[0-9]+|[0-9]+)) *(.*)')
20 for line in sys.stdin:
21   match = re_scalar.match(line)
22   if match is not None:
23     line = match.group(1) + str(float(match.group(3)) * scale) + match.group(4) + '\n'
24   else:
25     match = re_scalar2.match(line)
26     if match is not None:
27       out = [str(float(match.group(2)) * scale)]
28       if match.group(3) is not None:
29         out.append(str(float(match.group(4)) * scale))
30       line = match.group(1) + ' '.join(out) + match.group(5) + '\n'
31     else:
32       match = re_viewbox.match(line)
33       if match is not None:
34         line = match.group(1) + str(float(match.group(2)) * scale) + ' ' + str(float(match.group(3)) * scale) + match.group(4) + '\n'
35       else:
36         match = re_path.match(line)
37         if match is not None:
38           path = match.group(2).lstrip()
39           out = []
40           while True:
41             match2 = re_path_command.match(path)
42             if match2 is not None:
43               out.append(match2.group(1))
44               path = match2.group(2)
45             else:
46               match2 = re_path_number.match(path)
47               if match2 is not None:
48                 out.append(str(float(match2.group(1)) * scale))
49                 path = match2.group(3)
50               else:
51                 break
52           assert len(path) == 0
53           line = match.group(1) + ' '.join(out).replace(' ,', ',').replace(', ', ',') + match.group(3) + '\n'
54   sys.stdout.write(line)