Debian Codegraph @

examples/cairo_snippets/snippets_svg.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 """Create a SVG file for each example"""
4
5
6 import os
7 import sys
8 import cairo
9
10 from snippets import get_snippets
11
12
13 def do_snippet(snippet):
14 if verbose_mode:
15 print(f'processing {snippet.name}')
16
17 width_in_inches, height_in_inches = 2, 2
18 width_in_points, height_in_points = \
19 width_in_inches * 72, height_in_inches * 72
20 width, height = width_in_points, height_in_points
21
22 try:
23 os.makedirs(os.path.join("_build", "svg"))
24 except EnvironmentError:
25 pass
26 filename = os.path.join("_build", "svg", f"{snippet.name}.svg")
27
28 surface = cairo.SVGSurface(filename, width_in_points, height_in_points)
29 cr = cairo.Context(surface)
30
31 cr.save()
32 snippet.draw_func(cr, width, height)
33 cr.restore()
34 cr.show_page()
35 surface.finish()
36
37
38 if __name__ == '__main__':
39 if not cairo.HAS_SVG_SURFACE:
40 raise SystemExit('cairo was not compiled with SVG support')
41
42 verbose_mode = True
43 if len(sys.argv) > 1 and sys.argv[1] == '-s':
44 verbose_mode = False
45 del sys.argv[1]
46
47 snippets = get_snippets()
48
49 if len(sys.argv) > 1:
50 # do specified snippets
51 selected = [snippets[n] for n in sys.argv[1:]]
52 else:
53 # do all snippets
54 selected = snippets.values()
55
56 for s in selected:
57 do_snippet(s)