Debian Codegraph @

examples/cairo_snippets/snippets_ps.py

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