| 1 |
|
| 2 |
|
| 3 |
"""Create a PDF 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", "pdf")) |
| 23 |
except EnvironmentError: |
| 24 |
pass |
| 25 |
filename = os.path.join("_build", "pdf", f"{snippet.name}.pdf") |
| 26 |
|
| 27 |
surface = cairo.PDFSurface(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 |
|
| 39 |
if not cairo.HAS_PDF_SURFACE: |
| 40 |
raise SystemExit('cairo was not compiled with PDF 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 |
|
| 51 |
selected = [snippets[n] for n in sys.argv[1:]] |
| 52 |
else: |
| 53 |
|
| 54 |
selected = snippets.values() |
| 55 |
|
| 56 |
for s in selected: |
| 57 |
do_snippet(s) |