root/trunk/cairo/cairooo_snippets/cairooo_snippets_png.d

Revision 119, 1.9 kB (checked in by DRK, 6 years ago)

* Added bindings for the Glitz and Xlib backends.
* Checked in cairooo: an OO layer on top of the cairo binding.
* Added snippets directory for cairooo
* Added basic tutorial on how to use cairooo
* Added a simple demo program.
* Added scripts for building import libraries.
* Fixed several bugs.
* Drank WAAY too much coffee.

Line 
1 import std.stdio;
2 import std.string;
3
4 import cairooo.all;
5 import cairooo.png.all;
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 this()
19 {
20     Cairo.load();
21     CairoPNG.load();
22 }
23
24 void main(char[][] argv)
25 {
26     writefln("cairooo_snippets_png");
27     writefln("Using libcairo version %s", Cairo.cairoVersionString);
28    
29     if( argv.length > 1 )
30     {
31         foreach( n, arg ; argv )
32         {
33             if( n == 0 ) continue;
34
35             if( arg in snippets_hash )
36                 do_snippet(arg, snippets_hash[arg]);
37             else
38                 throw new Exception("Unknown snippet \"%s\".".format(arg));
39         }
40     }
41     else
42     {
43         auto keys = snippets_hash.keys.dup;
44         keys.sort;
45        
46         foreach( k ; keys )
47             do_snippet(k, snippets_hash[k]);
48     }
49 }
50
51 void do_snippet(char[] name, snippet_fn snippet)
52 {
53     writef("Running snippet \"%s\"... ", name);
54    
55     // Create a new surface.
56     auto ImageSurface surface = new ImageSurface(Format.ARGB32, IMAGE_WIDTH,
57             IMAGE_HEIGHT);
58     auto Context cr = new Context(surface);
59
60     // Scale it to a unit square, and set the line width.
61     cr.scale(IMAGE_WIDTH, IMAGE_HEIGHT);
62     cr.lineWidth = LINE_WIDTH;
63
64     // Clear the background
65     cr.rectangle(0, 0, 1, 1);
66     cr.setSourceRGBA(1, 1, 1, 0);
67     cr.operator = Operator.Clear;
68     cr.fill();
69
70     // Reset the context for the snippet
71     cr.setSourceRGB(0, 0, 0);
72     cr.operator = Operator.Over;
73
74     // Call the snippet.
75     cr.save();
76     snippet(cr, IMAGE_WIDTH, IMAGE_HEIGHT);
77     cr.restore();
78
79     // Write the results to disk.
80     PNGSurface.writeToPNG(surface, OUTPUT_PATH.format(name));
81
82     writefln("Done.");
83 }
Note: See TracBrowser for help on using the browser.