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.pdf;
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_PDF_SURFACE)
30 {
31     import cairo.c.pdf;
32 
33     ///
34     public alias cairo_pdf_version_t PDFVersion;
35 
36     /**
37      * Used to retrieve the list of supported versions.
38      * See $(D PDFSurface.restrictToVersion()).
39      */
40     PDFVersion[] getPDFVersions()
41     {
42         int num;
43         immutable(cairo_pdf_version_t*) vers;
44         cairo_pdf_get_versions(&vers, &num);
45         PDFVersion[] dvers;
46         for(int i = 0; i < num; i++)
47         {
48             dvers ~= vers[i];
49         }
50         return dvers;
51     }
52 
53     /**
54      * Get the string representation of the given version id.
55      * This function will return null if version isn't valid.
56      * See $(D getPDFVersions()) for a way to get the list of valid version ids.
57      */
58     string PDFVersionToString(PDFVersion vers)
59     {
60         return to!string(cairo_pdf_version_to_string(vers));
61     }
62 
63     /**
64      * The PDF surface is used to render cairo graphics to Adobe PDF
65      * files and is a multi-page vector surface backend.
66      */
67     public class PDFSurface : Surface
68     {
69         public:
70             /**
71              * Create a $(D PDFSurface) from a existing $(D cairo_surface_t*).
72              * PDFSurface 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 PDF surface of the specified size in points to
89              * be written to filename.
90              *
91              * Params:
92              * fileName = a filename for the PDF output (must be writable)
93              * width = width of the surface, in points (1 point == 1/72.0 inch)
94              * height = height of the surface, in points (1 point == 1/72.0 inch)
95              */
96             this(string fileName, double width, double height)
97             {
98                 super(cairo_pdf_surface_create(toStringz(fileName), width, height));
99             }
100 
101             /**
102              * Creates a PDF surface of the specified size in points.
103              * This will generate a PDF surface that may be queried and
104              * used as a source, without generating a temporary file.
105              * 
106              * Params:
107              * width = width of the surface, in points (1 point == 1/72.0 inch)
108              * height = height of the surface, in points (1 point == 1/72.0 inch)
109              */
110             this(double width, double height)
111             {
112                 this("", width, height);
113             }
114 
115             /**
116              * Restricts the generated PDF file to version. See
117              * $(D getPDFVersions()) for a list of available
118              * version values that can be used here.
119              *
120              * This function should only be called before any drawing
121              * operations have been performed on the given surface.
122              * The simplest way to do this is to call this function
123              * immediately after creating the surface
124              */
125             void restrictToVersion(PDFVersion vers)
126             {
127                 scope(exit)
128                     checkError();
129                 cairo_pdf_surface_restrict_to_version(this.nativePointer, vers);
130             }
131 
132             /**
133              * Changes the size of a PDF surface for the current
134              * (and subsequent) pages.
135              *
136              * This function should only be called before any drawing
137              * operations have been performed on the current page.
138              * The simplest way to do this is to call this function
139              * immediately after creating the surface or immediately
140              * after completing a page with either $(D Context.showPage())
141              * or $(D Context.copyPage()).
142              *
143              * Params:
144              * width = width of the surface, in points (1 point == 1/72.0 inch)
145              * height = height of the surface, in points (1 point == 1/72.0 inch)
146              */
147             void setSize(double width, double height)
148             {
149                 scope(exit)
150                     checkError();
151                 return cairo_pdf_surface_set_size(this.nativePointer, width, height);
152             }
153     }
154 }