1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  *    (See accompanying file LICENSE_1_0.txt or copy at
4  *          http://www.boost.org/LICENSE_1_0.txt)
5  */
6 module pango.cairo;
7 
8 import pango.utils;
9 import pango.font;
10 import pango.fontmap;
11 import pango.fontset;
12 import pango.context : PgContext=Context;
13 import pango.layout;
14 import pango.glyph;
15 import pango.glyph_item;
16 import pango.language;
17 import pango.c.cairo;
18 import pango.c.font;
19 import pango.c.fontmap;
20 import pango.c.fontset;
21 import pango.c.context;
22 
23 import cairo.cairo : FontType, ScaledFont, FontOptions, CrContext=Context;
24 import cairo.c.cairo;
25 
26 import gobject;
27 
28 import std..string;
29 
30 
31 /**
32  * PangoCairoShapeRendererFunc:
33  * @cr: a Cairo context with current point set to where the shape should
34  * be rendered
35  * @attr: the %PANGO_ATTR_SHAPE to render
36  * @do_path: whether only the shape path should be appended to current
37  * path of @cr and no filling/stroking done.  This will be set
38  * to %TRUE when called from pango_cairo_layout_path() and
39  * pango_cairo_layout_line_path() rendering functions.
40  * @data: user data passed to pango_cairo_context_set_shape_renderer()
41  *
42  * Function type for rendering attributes of type %PANGO_ATTR_SHAPE
43  * with Pango's Cairo renderer.
44  */
45 //alias PangoCairoShapeRendererFunc = void function (cairo_t        *cr,
46 //                          PangoAttrShape *attr,
47 //                          gboolean        do_path,
48 //                          gpointer        data);
49 
50 /**
51 * PangoCairoFontMap:
52 *
53 * #PangoCairoFontMap is an interface exported by font maps for
54 * use with Cairo. The actual type of the font map will depend
55 * on the particular font technology Cairo was compiled to use.
56 *
57 * Since: 1.10
58 **/
59 
60 class CairoFontMap : FontMap
61 {
62     mixin GObjectHolder!PangoCairoFontMap;
63 
64     package this(PangoCairoFontMap *ptr, Transfer transfer) {
65         super(cast(PangoFontMap*)ptr, transfer);
66     }
67 
68     this() {
69         this(cast(PangoCairoFontMap*)pango_cairo_font_map_new(), Transfer.Full);
70     }
71 
72     this(FontType fonttype) {
73         this(cast(PangoCairoFontMap*)pango_cairo_font_map_new_for_font_type(fonttype), Transfer.Full);
74     }
75 
76     static CairoFontMap getDefault() {
77         return getDObject!CairoFontMap(cast(PangoCairoFontMap*)pango_cairo_font_map_get_default(), Transfer.None);
78     }
79 
80     static void setDefault(CairoFontMap fm) {
81         pango_cairo_font_map_set_default(fm.nativePtr);
82     }
83 
84     override PgContext createContext()
85     {
86         return getDObject!CairoContext(pango_font_map_create_context(cast(PangoFontMap*)nativePtr), Transfer.Full);
87     }
88 
89     override Font loadFont(PgContext context, const(FontDescription) desc) {
90         return CairoFontFactory.instance.getFont(pango_font_map_load_font(
91                                                             cast(PangoFontMap*)nativePtr,
92                                                             context.nativePtr,
93                                                             desc.nativePtr),
94                                    Transfer.Full);
95     }
96 
97     override FontSet loadFontSet(PgContext context, const(FontDescription) desc, Language language) {
98         return CairoFontFactory.instance.getFontSet(pango_font_map_load_fontset(cast(PangoFontMap*)nativePtr,
99                                             context.nativePtr, desc.nativePtr, language.nativePtr),
100                                       Transfer.Full);
101     }
102 
103     @property FontType fontType() {
104         return pango_cairo_font_map_get_font_type(nativePtr);
105     }
106 
107     @property double resolution() {
108         return pango_cairo_font_map_get_resolution(nativePtr);
109     }
110 
111     @property void resolution(double dpi) {
112         pango_cairo_font_map_set_resolution(nativePtr, dpi);
113     }
114 
115 }
116 
117 
118 
119 /**
120 * PangoCairoFont:
121 *
122 * #PangoCairoFont is an interface exported by fonts for
123 * use with Cairo. The actual type of the font will depend
124 * on the particular font technology Cairo was compiled to use.
125 *
126 * Since: 1.18
127 **/
128 class CairoFont : Font {
129 
130     mixin GObjectHolder!PangoCairoFont;
131 
132     package this(PangoCairoFont *ptr, Transfer transfer) {
133         super(cast(PangoFont*)ptr, transfer);
134     }
135 
136     @property ScaledFont scaledFont() {
137         auto sf = pango_cairo_font_get_scaled_font(nativePtr);
138         cairo_scaled_font_reference(sf);
139         return new ScaledFont(sf);
140     }
141 
142 }
143 
144 
145 /*
146  * Update a Pango context for the current state of a cairo context
147  */
148 class CairoContext : PgContext
149 {
150     package this(PangoContext *ptr, Transfer transfer) {
151         super(ptr, transfer);
152         fontFactory = CairoFontFactory.instance;
153     }
154 
155     @property void fontOptions(FontOptions options) {
156         pango_cairo_context_set_font_options(nativePtr, options.nativePointer);
157     }
158 
159     @property FontOptions options() {
160         auto fo = pango_cairo_context_get_font_options(nativePtr);
161         auto foc = cairo_font_options_copy(fo);
162         return FontOptions(foc);
163     }
164 
165     @property double resolution() {
166         return  pango_cairo_context_get_resolution(nativePtr);
167     }
168 
169     @property resolution(double dpi) {
170         pango_cairo_context_set_resolution(nativePtr, dpi);
171     }
172 
173     //void                        pango_cairo_context_set_shape_renderer (PangoContext                *context,
174     //                                    PangoCairoShapeRendererFunc  func,
175     //                                    gpointer                     data,
176     //                                    GDestroyNotify               dnotify);
177     //PangoCairoShapeRendererFunc pango_cairo_context_get_shape_renderer (PangoContext                *context,
178     //                                    gpointer                    *data);
179     //
180 }
181 
182 
183 
184 // following complete the definition of cairo.cairo.Context
185 
186 
187 /*
188  * Convenience
189  */
190 PgContext createContext(CrContext cr) {
191     return getDObject!CairoContext(pango_cairo_create_context(cr.nativePointer), Transfer.Full);
192 }
193 
194 void updatePangoContext(CrContext cr, PgContext context) {
195     pango_cairo_update_context(cr.nativePointer, context.nativePtr);
196 }
197 
198 Layout createPangoLayout(CrContext cr) {
199     return getDObject!Layout(pango_cairo_create_layout(cr.nativePointer), Transfer.Full);
200 }
201 
202 void updatePangoLayout(CrContext cr, Layout layout) {
203     pango_cairo_update_layout(cr.nativePointer, layout.nativePtr);
204 }
205 
206 
207 /*
208  * Rendering
209  */
210 void showPangoGlyphString(CrContext cr, Font font, GlyphString glyphs) {
211     pango_cairo_show_glyph_string(cr.nativePointer, font.nativePtr, glyphs.nativePtr);
212 }
213 
214 void showPangoGlyphItem(CrContext cr, string text, GlyphItem item) {
215     pango_cairo_show_glyph_item(cr.nativePointer, toStringz(text), item.nativePtr);
216 }
217 
218 void showPangoLayoutLine(CrContext cr, LayoutLine line) {
219     pango_cairo_show_layout_line(cr.nativePointer, line.nativePtr);
220 }
221 
222 void showPangoLayout(CrContext cr, Layout layout) {
223     pango_cairo_show_layout(cr.nativePointer, layout.nativePtr);
224 }
225 
226 void showPangoErrorUnderline(CrContext cr, double x, double y, double width, double height) {
227     pango_cairo_show_error_underline(cr.nativePointer, x, y, width, height);
228 }
229 
230 
231 /*
232  * Rendering to a path
233  */
234 void pangoGlyphStringPath(CrContext cr, Font font, GlyphString glyphs) {
235     pango_cairo_glyph_string_path(cr.nativePointer, font.nativePtr, glyphs.nativePtr);
236 }
237 
238 void pangoLayoutLinePath(CrContext cr, LayoutLine line) {
239     pango_cairo_layout_line_path(cr.nativePointer, line.nativePtr);
240 }
241 
242 void pangoLayoutPath(CrContext cr, Layout layout) {
243     pango_cairo_layout_path(cr.nativePointer, layout.nativePtr);
244 }
245 
246 void pangoErrorUnderlinePath(CrContext cr, double x, double y, double width, double height) {
247     pango_cairo_error_underline_path(cr.nativePointer, x, y, width, height);
248 }
249 
250 
251 
252 
253 class CairoFontFactory : FontFactory
254 {
255     static @property FontFactory instance() {
256         static FontFactory inst_;
257         if (!inst_) inst_ = new CairoFontFactory;
258         return inst_;
259     }
260 
261     FontMap getFontMap(PangoFontMap *ptr, Transfer transfer) {
262         return getDObject!CairoFontMap(cast(PangoCairoFontMap*)ptr, transfer);
263     }
264 
265     FontSet getFontSet(PangoFontset *ptr, Transfer transfer) {
266         auto fs = getDObject!FontSet(ptr, transfer);
267         if (fs) fs.fontFactory = this;
268         return fs;
269     }
270 
271     Font getFont(PangoFont *ptr, Transfer transfer) {
272         return getDObject!CairoFont(cast(PangoCairoFont*)ptr, transfer);
273     }
274 }
275 
276