| 1 |
|
| 2 |
|
| 3 |
"""Create a PNG 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, height = 256, 256 |
| 17 |
|
| 18 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
| 19 |
cr = cairo.Context(surface) |
| 20 |
|
| 21 |
cr.save() |
| 22 |
snippet.draw_func(cr, width, height) |
| 23 |
|
| 24 |
cr.restore() |
| 25 |
|
| 26 |
try: |
| 27 |
os.makedirs(os.path.join("_build", "png")) |
| 28 |
except EnvironmentError: |
| 29 |
pass |
| 30 |
filename = os.path.join("_build", "png", f"{snippet.name}.png") |
| 31 |
|
| 32 |
surface.write_to_png(filename) |
| 33 |
|
| 34 |
|
| 35 |
if __name__ == '__main__': |
| 36 |
if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS): |
| 37 |
raise SystemExit( |
| 38 |
'cairo was not compiled with ImageSurface and PNG support') |
| 39 |
|
| 40 |
verbose_mode = True |
| 41 |
if len(sys.argv) > 1 and sys.argv[1] == '-s': |
| 42 |
verbose_mode = False |
| 43 |
del sys.argv[1] |
| 44 |
|
| 45 |
snippets = get_snippets() |
| 46 |
|
| 47 |
if len(sys.argv) > 1: |
| 48 |
|
| 49 |
selected = [snippets[n] for n in sys.argv[1:]] |
| 50 |
else: |
| 51 |
|
| 52 |
selected = snippets.values() |
| 53 |
|
| 54 |
for s in selected: |
| 55 |
do_snippet(s) |