root/trunk/cairo/cairo/cairo.d

Revision 228, 4.4 kB (checked in by DRK, 5 years ago)

* Updated the bindings to support cairo 1.2 and cairo 1.4. Support for these can be enabled with the cairo_1_2 and cairo_1_4 version identifiers.
* Added pdf, ps and svg support.
* Updated the build script appropriately.
* Note that cairooo is not likely to receive any further updates in its current form. It is being forked and rewritten for Tango's graphics package, although a port back to Phobos isn't out of the question.

Line 
1 /**
2  * cairo bindings for D.
3  *
4  * Authors: Daniel Keep
5  * Copyright: 2006, Daniel Keep
6  * License: BSD v2 (http://www.opensource.org/licenses/bsd-license.php).
7  */
8 /*
9  * Copyright © 2006 Daniel Keep
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are
14  * met:
15  *
16  * * Redistributions of source code must retain the above copyright
17  *   notice, this list of conditions and the following disclaimer.
18  *
19  * * Redistributions in binary form must reproduce the above copyright
20  *   notice, this list of conditions and the following disclaimer in the
21  *   documentation and/or other materials provided with the distribution.
22  *
23  * * Neither the name of this software, nor the names of its contributors
24  *   may be used to endorse or promote products derived from this software
25  *   without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39 module cairo.cairo;
40
41 public import cairo.cairotypes;
42 public import cairo.cairofuncs;
43
44 private import cairo.loader;
45 package SharedLib libCairo;
46
47 /**
48  * This function will attempt to load the cairo library.
49  */
50 public void cairo_load( char[] libName )
51 {
52     if( libCairo !is null )
53         return;
54    
55     libCairo = loadSharedLib(libName);
56
57     /* Try to find out what version of cairo we've got.  If it's too old,
58      * throw an exception explaining this.
59      */
60     auto versionnum = (cast(pf_cairo_version)
61             getProc(libCairo,"cairo_version"))();
62
63     version( cairo_1_4 )
64         auto expected_version = CAIRO_VERSION_ENCODE(1,4,0);
65
66     else version( cairo_1_2 )
67         auto expected_version = CAIRO_VERSION_ENCODE(1,2,0);
68    
69     else
70         auto expected_version = CAIRO_VERSION_ENCODE(1,0,0);
71    
72     if( versionnum < expected_version )
73         throw new CairoVersionException(versionnum, expected_version);
74
75     // Now we can actually load the functions.
76     cairo_loadprocs(libCairo);
77 }
78
79 /// ditto
80 public void cairo_load()
81 {
82     version(Windows)
83         cairo_load("libcairo-2.dll");
84     else version(linux)
85         cairo_load("libcairo.so");
86     else
87     {
88         pragma(msg, "I'm sorry, but implicit loading is not supported on"
89                     " your platform.  Please call cairo_load with the"
90                     " name of the library to load.");
91         pragma(msg, "If you would like to help, please contact us with"
92                     " the name of your platform, your compiler, and the"
93                     " name of the cairo library for your platform."
94                     "  Thankyou, and sorry for the inconvenience.");
95         static assert(0);
96     }
97 }
98
99 /**
100  * This will unload the cairo library from memory.  This will be
101  * called automatically when your program terminates
102  */
103 public void cairo_unload()
104 {
105     if( libCairo is null )
106         return;
107    
108     unloadSharedLib(libCairo);
109     libCairo = null;
110 }
111
112 version( Tango )
113 {
114     import tango.text.convert.Integer : toUtf8;
115     alias toUtf8 intToString;
116 }
117 else
118 {
119     import std.string : toString;
120     alias toString intToString;
121 }
122
123 private char[] verToString(int ver)
124 {
125     return intToString(ver/100_00)
126         ~ "." ~ intToString((ver/100)%100)
127         ~ "." ~ intToString(ver%100);
128 }
129
130 private class CairoVersionException : Exception
131 {
132     this(int got, int expected)
133     {
134         this("expected cairo version "~verToString(expected)
135                 ~", got version "~verToString(got)~".");
136     }
137
138     this(char[] msg)
139     {
140         super(msg);
141     }
142 }
143
144 static ~this()
145 {
146     cairo_unload();
147 }
Note: See TracBrowser for help on using the browser.