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.ft;
22 
23 import cairo.cairo;
24 import cairo.c.cairo;
25 
26 static if(CAIRO_HAS_FT_FONT)
27 {
28     import cairo.c.ft;
29     import derelict.freetype.ft;
30 
31     /**
32      * Font support for FreeType
33      */
34     public class FTFontFace : FontFace
35     {
36         public:
37             /**
38              * Create a $(D FTFontFace) from a existing $(D cairo_font_face_t*).
39              * FTFontFace is a garbage collected class. It will call $(D cairo_font_face_destroy)
40              * when it gets collected by the GC or when $(D dispose()) is called.
41              *
42              * Warning:
43              * $(D ptr)'s reference count is not increased by this function!
44              * Adjust reference count before calling it if necessary
45              *
46              * $(RED Only use this if you know what your doing!
47              * This function should not be needed for standard cairoD usage.)
48              */
49             this(cairo_font_face_t* ptr)
50             {
51                 super(ptr);
52             }
53 
54             /**
55              * Creates a new font face for the FreeType font backend from
56              * a pre-opened FreeType face. This font can then be
57              * used with $(D Context.setFontFace()) or $(D new ScaledFont()).
58              * The $(D ScaledFont) returned from
59              * $(D new ScaledFont()) is also for
60              * the FreeType backend and can be used with functions such
61              * as $(FTScaledFont.lockFace()). Note that Cairo may
62              * keep a reference to the FT_Face alive in a font-cache
63              * and the exact lifetime of the reference depends highly
64              * upon the exact usage pattern and is subject to external
65              * factors. You must not call FT_Done_Face() before the
66              * $(D FTFontFace) has been disposed / collected.
67              *
68              * TODO: translate example from cairo API docs;
69              *  What abou the cairo_font_face_set_user_data part?
70              */
71             this(FT_Face face, int loadFlags)
72             {
73                 super(cairo_ft_font_face_create_for_ft_face(face, loadFlags));
74             }
75     }
76 
77     /**
78      * Font support for FreeType
79      */
80     public class FTScaledFont : ScaledFont
81     {
82         public:
83             /**
84              * Create a $(D FTScaledFont) from a existing $(D cairo_scaled_font_t*).
85              * FTScaledFont is a garbage collected class. It will call $(D cairo_scaled_font_destroy)
86              * when it gets collected by the GC or when $(D dispose()) is called.
87              *
88              * Warning:
89              * $(D ptr)'s reference count is not increased by this function!
90              * Adjust reference count before calling it if necessary
91              *
92              * $(RED Only use this if you know what your doing!
93              * This function should not be needed for standard cairoD usage.)
94              */
95             this(cairo_scaled_font_t* ptr)
96             {
97                 super(ptr);
98             }
99 
100             /**
101              * $(D lockFace()) gets the FT_Face object from a FreeType
102              * backend font and scales it appropriately for the font.
103              * You must release the face with $(D unlockFace()) when you
104              * are done using it. Since the FT_Face object can be shared
105              * between multiple $(D ScaledFont) objects, you must not
106              * lock any other font objects until you unlock this one.
107              * A count is kept of the number of times $(D lockFace())
108              * is called. $(D unlockFace()) must be called the same number of times.
109              *
110              * You must be careful when using this function in a library
111              * or in a threaded application, because freetype's design
112              * makes it unsafe to call freetype functions simultaneously
113              * from multiple threads, (even if using distinct FT_Face objects).
114              * Because of this, application code that acquires an FT_Face
115              * object with this call must add its own locking to protect
116              * any use of that object, (and which also must protect any
117              * other calls into cairo as almost any cairo function
118              * might result in a call into the freetype library).
119              */
120             FT_Face lockFace()
121             {
122                 auto tmp = cairo_ft_scaled_font_lock_face(this.nativePointer);
123                 checkError();
124                 return tmp;
125             }
126 
127             /**
128              * Releases a face obtained with $(D lockFace()).
129              */
130             void unlockFace()
131             {
132                 cairo_ft_scaled_font_unlock_face(this.nativePointer);
133                 checkError();
134                 return;
135             }
136     }
137 }