The OpenD Programming Language

1 /**
2  * License:
3  * $(TABLE
4  *   $(TR $(TD cairoD wrapper/bindings)
5  *     $(TD $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)))
6  *   $(TR $(TD $(LINK2 http://cgit.freedesktop.org/cairo/tree/COPYING, _cairo))
7  *     $(TD $(LINK2 http://cgit.freedesktop.org/cairo/tree/COPYING-LGPL-2.1, LGPL 2.1) /
8  *     $(LINK2 http://cgit.freedesktop.org/cairo/plain/COPYING-MPL-1.1, MPL 1.1)))
9  * )
10  * Authors:
11  * $(TABLE
12  *   $(TR $(TD Johannes Pfau) $(TD cairoD))
13  *   $(TR $(TD $(LINK2 http://cairographics.org, _cairo team)) $(TD _cairo))
14  * )
15  */
16 /*
17  * Distributed under the Boost Software License, Version 1.0.
18  *    (See accompanying file LICENSE_1_0.txt or copy at
19  *          http://www.boost.org/LICENSE_1_0.txt)
20  */
21 module cairo.svg;
22 
23 import std.string;
24 import std.conv;
25 
26 import cairo.cairo;
27 import cairo.c.cairo;
28 
29 static if(CAIRO_HAS_SVG_SURFACE)
30 {
31     import cairo.c.svg;
32     ///
33     public alias cairo_svg_version_t SVGVersion;
34 
35     /**
36      * Used to retrieve the list of supported versions.
37      * See $(D SVGSurface.restrictToVersion()).
38      */
39     SVGVersion[] getSVGVersions()
40     {
41         int num;
42         immutable(cairo_svg_version_t*) vers;
43         cairo_svg_get_versions(&vers, &num);
44         SVGVersion[] dvers;
45         for(int i = 0; i < num; i++)
46         {
47             dvers ~= vers[i];
48         }
49         return dvers;
50     }
51 
52     /**
53      * Get the string representation of the given version id. This
54      * function will return null if version isn't valid.
55      * See $(D getSVGVersions()) for a way to get the list of
56      * valid version ids.
57      */
58     string SVGVersionToString(SVGVersion vers)
59     {
60         return to!string(cairo_svg_version_to_string(vers));
61     }
62 
63     /**
64      * The SVG surface is used to render cairo graphics to SVG
65      * files and is a multi-page vector surface backend.
66      */
67     public class SVGSurface : Surface
68     {
69         public:
70             /**
71              * Create a $(D SVGSurface) from a existing $(D cairo_surface_t*).
72              * SVGSurface is a garbage collected class. It will call $(D cairo_surface_destroy)
73              * when it gets collected by the GC or when $(D dispose()) is called.
74              *
75              * Warning:
76              * $(D ptr)'s reference count is not increased by this function!
77              * Adjust reference count before calling it if necessary
78              *
79              * $(RED Only use this if you know what your doing!
80              * This function should not be needed for standard cairoD usage.)
81              */
82             this(cairo_surface_t* ptr)
83             {
84                 super(ptr);
85             }
86 
87             /**
88              * Creates a SVG surface of the specified size in points to
89              * be written to filename.
90              *
91              * The SVG surface backend recognizes the following MIME types
92              * for the data attached to a surface
93              * (see $(D Surface.setMimeData())) when it is used as a
94              * source pattern for drawing on this surface:
95              * CAIRO_MIME_TYPE_JPEG, CAIRO_MIME_TYPE_PNG, CAIRO_MIME_TYPE_URI.
96              * If any of them is specified, the SVG backend emits a href
97              * with the content of MIME data instead of a surface
98              * snapshot (PNG, Base64-encoded) in the corresponding image tag.
99              *
100              * The unofficial MIME type CAIRO_MIME_TYPE_URI is examined
101              * first. If present, the URI is emitted as is: assuring the
102              * correctness of URI is left to the client code.
103              *
104              * If CAIRO_MIME_TYPE_URI is not present, but CAIRO_MIME_TYPE_JPEG
105              * or CAIRO_MIME_TYPE_PNG is specified, the corresponding
106              * data is Base64-encoded and emitted.
107              *
108              * Params:
109              * width = width of the surface, in points (1 point == 1/72.0 inch)
110              * height = height of the surface, in points (1 point == 1/72.0 inch)
111              */
112             this(string fileName, double width, double height)
113             {
114                 super(cairo_svg_surface_create(toStringz(fileName), width, height));
115             }
116 
117             /**
118              * Creates a SVG surface of the specified size in points.
119              * This will generate a SVG surface that may be queried and
120              * used as a source, without generating a temporary file.
121              *
122              * The SVG surface backend recognizes the following MIME types
123              * for the data attached to a surface
124              * (see $(D Surface.setMimeData())) when it is used as a
125              * source pattern for drawing on this surface:
126              * CAIRO_MIME_TYPE_JPEG, CAIRO_MIME_TYPE_PNG, CAIRO_MIME_TYPE_URI.
127              * If any of them is specified, the SVG backend emits a href
128              * with the content of MIME data instead of a surface
129              * snapshot (PNG, Base64-encoded) in the corresponding image tag.
130              *
131              * The unofficial MIME type CAIRO_MIME_TYPE_URI is examined
132              * first. If present, the URI is emitted as is: assuring the
133              * correctness of URI is left to the client code.
134              *
135              * If CAIRO_MIME_TYPE_URI is not present, but CAIRO_MIME_TYPE_JPEG
136              * or CAIRO_MIME_TYPE_PNG is specified, the corresponding
137              * data is Base64-encoded and emitted.
138              *
139              * Params:
140              * width = width of the surface, in points (1 point == 1/72.0 inch)
141              * height = height of the surface, in points (1 point == 1/72.0 inch)
142              */
143             this(double width, double height)
144             {
145                 this("", width, height);
146             }
147 
148             /**
149              * Restricts the generated SVG file to version.
150              * See $(D getSVGVersions()) for a list of available
151              * version values that can be used here.
152              *
153              * This function should only be called before any
154              * drawing operations have been performed on the given surface.
155              * The simplest way to do this is to call this function
156              * immediately after creating the surface.
157              */
158             void restrictToVersion(SVGVersion vers)
159             {
160                 scope(exit)
161                     checkError();
162                 cairo_svg_surface_restrict_to_version(this.nativePointer, vers);
163             }
164     }
165 }