| 1 |
import std.stdio; |
|---|
| 2 |
import std.string; |
|---|
| 3 |
|
|---|
| 4 |
import cairo.cairo; |
|---|
| 5 |
import cairo.png.cairo_png; |
|---|
| 6 |
|
|---|
| 7 |
import snippets.all; |
|---|
| 8 |
import snippets.common; |
|---|
| 9 |
|
|---|
| 10 |
// Some constants |
|---|
| 11 |
const IMAGE_WIDTH = 256; |
|---|
| 12 |
const IMAGE_HEIGHT = 256; |
|---|
| 13 |
|
|---|
| 14 |
const LINE_WIDTH = 0.04; |
|---|
| 15 |
|
|---|
| 16 |
const OUTPUT_PATH = "output/%s.png"; |
|---|
| 17 |
|
|---|
| 18 |
// Static constructor; use this to load up the cairo |
|---|
| 19 |
// binding. |
|---|
| 20 |
static this() |
|---|
| 21 |
{ |
|---|
| 22 |
cairo_load(); |
|---|
| 23 |
cairo_png_load(); |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
void main(char[][] argv) |
|---|
| 27 |
{ |
|---|
| 28 |
writefln("cairo_snippets_png"); |
|---|
| 29 |
writefln("Using libcairo version %s", toString(cairo_version_string())); |
|---|
| 30 |
|
|---|
| 31 |
if( argv.length > 1 ) |
|---|
| 32 |
{ |
|---|
| 33 |
foreach( n, arg ; argv ) |
|---|
| 34 |
{ |
|---|
| 35 |
if( n == 0 ) continue; |
|---|
| 36 |
|
|---|
| 37 |
if( arg in snippets_hash ) |
|---|
| 38 |
do_snippet(arg, snippets_hash[arg]); |
|---|
| 39 |
else |
|---|
| 40 |
throw new Exception("Unknown snippet \"%s\".".format(arg)); |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
else |
|---|
| 44 |
{ |
|---|
| 45 |
auto keys = snippets_hash.keys.dup; |
|---|
| 46 |
keys.sort; |
|---|
| 47 |
|
|---|
| 48 |
foreach( k ; keys ) |
|---|
| 49 |
do_snippet(k, snippets_hash[k]); |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
void do_snippet(char[] name, snippet_fn snippet) |
|---|
| 54 |
{ |
|---|
| 55 |
writef("Running snippet \"%s\"... ", name); |
|---|
| 56 |
|
|---|
| 57 |
cairo_t* cr; |
|---|
| 58 |
cairo_surface_t* surface; |
|---|
| 59 |
|
|---|
| 60 |
// Create a new surface. |
|---|
| 61 |
surface = cairo_image_surface_create( |
|---|
| 62 |
cairo_format_t.CAIRO_FORMAT_ARGB32, IMAGE_WIDTH, IMAGE_HEIGHT); |
|---|
| 63 |
cr = cairo_create(surface); |
|---|
| 64 |
|
|---|
| 65 |
// Ensure surface and context are destroyed when we leave this |
|---|
| 66 |
// function, even if we throw an exception. |
|---|
| 67 |
scope(exit) |
|---|
| 68 |
{ |
|---|
| 69 |
cairo_destroy(cr); |
|---|
| 70 |
cairo_surface_destroy(surface); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
// Scale it to a unit square, and set the line width. |
|---|
| 74 |
cairo_scale(cr, IMAGE_WIDTH, IMAGE_HEIGHT); |
|---|
| 75 |
cairo_set_line_width(cr, LINE_WIDTH); |
|---|
| 76 |
|
|---|
| 77 |
// Clear the background |
|---|
| 78 |
cairo_rectangle(cr, 0, 0, 1, 1); |
|---|
| 79 |
cairo_set_source_rgba(cr, 1, 1, 1, 0); |
|---|
| 80 |
cairo_set_operator(cr, cairo_operator_t.CAIRO_OPERATOR_CLEAR); |
|---|
| 81 |
cairo_fill(cr); |
|---|
| 82 |
|
|---|
| 83 |
// Reset the context for the snippet |
|---|
| 84 |
cairo_set_source_rgb(cr, 0, 0, 0); |
|---|
| 85 |
cairo_set_operator(cr, cairo_operator_t.CAIRO_OPERATOR_OVER); |
|---|
| 86 |
|
|---|
| 87 |
// Call the snippet. |
|---|
| 88 |
cairo_save(cr); |
|---|
| 89 |
snippet(cr, IMAGE_WIDTH, IMAGE_HEIGHT); |
|---|
| 90 |
cairo_restore(cr); |
|---|
| 91 |
|
|---|
| 92 |
// Write the results to disk. |
|---|
| 93 |
cairo_surface_write_to_png(surface, toStringz(OUTPUT_PATH.format(name))); |
|---|
| 94 |
|
|---|
| 95 |
writefln("Done."); |
|---|
| 96 |
} |
|---|