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.win32;
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_WIN32_SURFACE)
30 {
31     import cairo.c.win32;
32 
33     version (CairoWindowsDruntime)
34     {
35         import core.sys.windows.windef;
36         import core.sys.windows.wingdi;
37     }
38     else
39     {
40         import windows.windef;
41         import windows.wingdi;
42     }
43 
44     /**
45      * Microsoft Windows surface support
46      *
47      * The Microsoft Windows surface is used to render cairo graphics to
48      * Microsoft Windows windows, bitmaps, and printing device contexts.
49      *
50      * If printing is set to true, the surface
51      * is of surface type CAIRO_SURFACE_TYPE_WIN32_PRINTING and is
52      * a multi-page vector surface type.
53      *
54      * The surface returned by the other win32 constructors is of
55      * surface type CAIRO_SURFACE_TYPE_WIN32 and is a raster surface type.
56      */
57     public class Win32Surface : Surface
58     {
59         public:
60             /**
61              * Create a $(D Win32Surface) from a existing $(D cairo_surface_t*).
62              * Win32Surface is a garbage collected class. It will call $(D cairo_surface_destroy)
63              * when it gets collected by the GC or when $(D dispose()) is called.
64              *
65              * Warning:
66              * $(D ptr)'s reference count is not increased by this function!
67              * Adjust reference count before calling it if necessary
68              *
69              * $(RED Only use this if you know what your doing!
70              * This function should not be needed for standard cairoD usage.)
71              */
72             this(cairo_surface_t* ptr)
73             {
74                 super(ptr);
75             }
76 
77             /**
78              * If printing is false:
79              *
80              * Creates a cairo surface that targets the given DC.
81              * The DC will be queried for its initial clip extents,
82              * and this will be used as the size of the cairo surface.
83              * The resulting surface will always be of format
84              * CAIRO_FORMAT_RGB24; should you need another surface
85              * format, you will need to us other constructors.
86              *
87              * If printing is true:
88              * Creates a cairo surface that targets the given DC.
89              * The DC will be queried for its initial clip extents,
90              * and this will be used as the size of the cairo surface.
91              * The DC should be a printing DC; antialiasing will be
92              * ignored, and GDI will be used as much as possible to
93              * draw to the surface.
94              *
95              * The returned surface will be wrapped using the paginated
96              * surface to provide correct complex rendering behaviour;
97              * $(D showPage()) and associated methods must be used fo
98              * correct output.
99              */
100             this(HDC hdc, bool printing = false)
101             {
102                 if(printing)
103                     super(cairo_win32_printing_surface_create(hdc));
104                 else
105                     super(cairo_win32_surface_create(hdc));
106             }
107 
108             /**
109              * Params:
110              * hdc = the DC to create a surface for
111              * format = format of pixels in the surface to create
112              * width = width of the surface, in pixels
113              * height = height of the surface, in pixels
114              */
115             this(HDC hdc, cairo_format_t format, int width, int height)
116             {
117                 super(cairo_win32_surface_create_with_ddb(hdc, format, width, height));
118             }
119 
120             /**
121              * Creates a device-independent-bitmap surface not associated
122              * with any particular existing surface or device context.
123              * The created bitmap will be uninitialized.
124              *
125              * Params:
126              * format = format of pixels in the surface to create
127              * width = width of the surface, in pixels
128              * height = height of the surface, in pixels
129              */
130             this(cairo_format_t format, int width, int height)
131             {
132                 super(cairo_win32_surface_create_with_dib(format, width, height));
133             }
134 
135             /**
136              * Returns the HDC associated with this surface, or
137              * null if none. Also returns null if the surface
138              * is not a win32 surface.
139              */
140             HDC getDC()
141             {
142                 scope(exit)
143                     checkError();
144                 return cairo_win32_surface_get_dc(this.nativePointer);
145             }
146 
147             /**
148              * Returns a $(D Surface) image surface that
149              * refers to the same bits as the DIB of the Win32
150              * surface. If the passed-in win32 surface is not a
151              * DIB surface, null is returned.
152              */
153             Surface getImage()
154             {
155                 scope(exit)
156                     checkError();
157                 return(Surface.createFromNative(cairo_win32_surface_get_image(this.nativePointer)));
158             }
159     }
160     static if(CAIRO_HAS_WIN32_FONT)
161     {
162         /**
163          * The Microsoft Windows font backend is primarily
164          * used to render text on Microsoft Windows systems.
165          */
166         public class Win32FontFace : FontFace
167         {
168             public:
169                 /**
170                  * Create a $(D Win32FontFace) from a existing $(D cairo_surface_t*).
171                  * Win32FontFace is a garbage collected class. It will call $(D cairo_font_face_destroy)
172                  * when it gets collected by the GC or when $(D dispose()) is called.
173                  *
174                  * Warning:
175                  * $(D ptr)'s reference count is not increased by this function!
176                  * Adjust reference count before calling it if necessary
177                  *
178                  * $(RED Only use this if you know what your doing!
179                  * This function should not be needed for standard cairoD usage.)
180                  */
181                 this(cairo_font_face_t* ptr)
182                 {
183                     super(ptr);
184                 }
185 
186                 /**
187                  * Creates a new font for the Win32 font backend based
188                  * on a LOGFONT.
189                  *
190                  * This font can then be used with
191                  * $(D Context.setFontFace()) or $(D new ScaledFont()).
192                  * The $(D ScaledFont) returned from
193                  * $(D new ScaledFont()) is also for
194                  * the Win32 backend and can be used with
195                  * functions such as $(D Win32ScaledFont.selectFont()).
196                  */
197                 this(LOGFONTW* logfont)
198                 {
199                     super(cairo_win32_font_face_create_for_logfontw(logfont));
200                 }
201 
202                 /**
203                  * Creates a new font for the Win32 font backend based on a HFONT.
204                  *
205                  * This font can then be used with
206                  * $(D Context.setFontFace()) or $(D new ScaledFont()).
207                  * The $(D ScaledFont) returned from
208                  * $(D new ScaledFont()) is also for
209                  * the Win32 backend and can be used with
210                  * functions such as $(D Win32ScaledFont.selectFont()).
211                  */
212                 this(HFONT font)
213                 {
214                     super(cairo_win32_font_face_create_for_hfont(font));
215                 }
216 
217                 /**
218                  * Creates a new font for the Win32 font backend based
219                  * on a LOGFONT.
220                  *
221                  * This font can then be used with
222                  * $(D Context.setFontFace()) or $(D new ScaledFont()).
223                  * The $(D ScaledFont) returned from
224                  * $(D new ScaledFont()) is also for
225                  * the Win32 backend and can be used with
226                  * functions such as $(D Win32ScaledFont.selectFont()).
227                  *
228                  * Params:
229                  * logfont = A LOGFONTW structure specifying the font
230                  *   to use. If font is NULL then the lfHeight, lfWidth,
231                  *   lfOrientation and lfEscapement fields of this
232                  *   structure are ignored. Otherwise lfWidth,
233                  *   lfOrientation and lfEscapement must be zero.
234                  * font = An HFONT that can be used when the font matrix
235                  *   is a scale by -lfHeight and the CTM is identity.
236                  */
237                 this(LOGFONTW* logfont, HFONT font)
238                 {
239                     super(cairo_win32_font_face_create_for_logfontw_hfont(logfont, font));
240                 }
241         }
242 
243         /**
244          * The Microsoft Windows font backend is primarily
245          * used to render text on Microsoft Windows systems.
246          */
247         public class Win32ScaledFont : ScaledFont
248         {
249             public:
250                 /**
251                  * Create a $(D Win32ScaledFont) from a existing $(D cairo_surface_t*).
252                  * Win32ScaledFont is a garbage collected class. It will call $(D cairo_scaled_font_destroy)
253                  * when it gets collected by the GC or when $(D dispose()) is called.
254                  *
255                  * Warning:
256                  * $(D ptr)'s reference count is not increased by this function!
257                  * Adjust reference count before calling it if necessary
258                  *
259                  * $(RED Only use this if you know what your doing!
260                  * This function should not be needed for standard cairoD usage.)
261                  */
262                 this(cairo_scaled_font_t* ptr)
263                 {
264                     super(ptr);
265                 }
266 
267                 /**
268                  *
269                  */
270                 this(Win32FontFace font_face, Matrix font_matrix, Matrix ctm,
271                     FontOptions options)
272                 {
273                     super(font_face, font_matrix, ctm, options);
274                 }
275 
276                 /**
277                  * Selects the font into the given device context and
278                  * changes the map mode and world transformation of
279                  * the device context to match that of the font. This
280                  * function is intended for use when using layout APIs
281                  * such as Uniscribe to do text layout with the cairo
282                  * font. After finishing using the device context,
283                  * you must call $(D Win32ScaledFont.doneFont()) to release
284                  * any resources allocated by this function.
285                  *
286                  * See $(D Win32ScaledFont.getMetricsFactor()) for converting
287                  * logical coordinates from the device context to font space.
288                  *
289                  * Normally, calls to SaveDC() and RestoreDC() would be
290                  * made around the use of this function to preserve
291                  * the original graphics state.
292                  */
293                 void selectFont(HDC hdc)
294                 {
295                     throwError(cairo_win32_scaled_font_select_font(this.nativePointer, hdc));
296                 }
297 
298                 /**
299                  * Releases any resources allocated by
300                  * $(D Win32ScaledFont.selectFont())
301                  */
302                 void doneFont()
303                 {
304                     cairo_win32_scaled_font_done_font(this.nativePointer);
305                     checkError();
306                 }
307 
308                 /**
309                  * Gets a scale factor between logical coordinates in
310                  * the coordinate space used
311                  * by $(D Win32ScaledFont.selectFont()) (that is,
312                  * the coordinate system used by the Windows functions
313                  * to return metrics) and font space coordinates.
314                  */
315                 double getMetricsFactor()
316                 {
317                     auto res = cairo_win32_scaled_font_get_metrics_factor(this.nativePointer);
318                     checkError();
319                     return res;
320                 }
321 
322                 /**
323                  * Gets the transformation mapping the logical space
324                  * used by ScaledFont to device space.
325                  */
326                 Matrix getLogicalToDevice()
327                 {
328                     Matrix mat;
329                     cairo_win32_scaled_font_get_logical_to_device(this.nativePointer,
330                         &mat.nativeMatrix);
331                     checkError();
332                     return mat;
333                 }
334 
335                 /**
336                  * Gets the transformation mapping device space to the
337                  * logical space used by ScaledFont.
338                  */
339                 Matrix getDeviceToLogical()
340                 {
341                     Matrix mat;
342                     cairo_win32_scaled_font_get_device_to_logical(this.nativePointer,
343                         &mat.nativeMatrix);
344                     checkError();
345                     return mat;
346                 }
347         }
348     }
349 }