Git init
[external/pango1.0.git] / pango / pangocairo-fontmap.c
1 /* Pango
2  * pangocairo-fontmap.c: Cairo font handling
3  *
4  * Copyright (C) 2000-2005 Red Hat Software
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include "pangocairo.h"
25 #include "pangocairo-private.h"
26 #include "pango-impl-utils.h"
27
28 #if defined (HAVE_CAIRO_ATSUI)
29 #  include "pangocairo-atsui.h"
30 #endif
31 #if defined (HAVE_CAIRO_WIN32)
32 #  include "pangocairo-win32.h"
33 #endif
34 #if defined (HAVE_CAIRO_FREETYPE)
35 #  include "pangocairo-fc.h"
36 #endif
37
38 GType
39 pango_cairo_font_map_get_type (void)
40 {
41   static GType cairo_font_map_type = 0;
42
43   if (! cairo_font_map_type)
44     {
45       const GTypeInfo cairo_font_map_info =
46       {
47         sizeof (PangoCairoFontMapIface), /* class_size */
48         NULL,           /* base_init */
49         NULL,           /* base_finalize */
50         NULL,
51         NULL,           /* class_finalize */
52         NULL,           /* class_data */
53         0,
54         0,
55         NULL,
56         NULL
57       };
58
59       cairo_font_map_type =
60         g_type_register_static (G_TYPE_INTERFACE, I_("PangoCairoFontMap"),
61                                 &cairo_font_map_info, 0);
62
63       g_type_interface_add_prerequisite (cairo_font_map_type, PANGO_TYPE_FONT_MAP);
64     }
65
66   return cairo_font_map_type;
67 }
68
69 /**
70  * pango_cairo_font_map_new:
71  *
72  * Creates a new #PangoCairoFontMap object; a fontmap is used
73  * to cache information about available fonts, and holds
74  * certain global parameters such as the resolution.
75  * In most cases, you can use pango_cairo_font_map_get_default()
76  * instead.
77  *
78  * Note that the type of the returned object will depend
79  * on the particular font backend Cairo was compiled to use;
80  * You generally should only use the #PangoFontMap and
81  * #PangoCairoFontMap interfaces on the returned object.
82  *
83  * Return value: the newly allocated #PangoFontMap, which should
84  *               be freed with g_object_unref().
85  *
86  * Since: 1.10
87  **/
88 PangoFontMap *
89 pango_cairo_font_map_new (void)
90 {
91   /* Make sure that the type system is initialized */
92   g_type_init ();
93
94 #if defined(HAVE_CAIRO_ATSUI)
95   return g_object_new (PANGO_TYPE_CAIRO_ATSUI_FONT_MAP, NULL);
96 #elif defined(HAVE_CAIRO_WIN32)
97   return g_object_new (PANGO_TYPE_CAIRO_WIN32_FONT_MAP, NULL);
98 #elif defined(HAVE_CAIRO_FREETYPE)
99   return g_object_new (PANGO_TYPE_CAIRO_FC_FONT_MAP, NULL);
100 #else
101   g_assert_not_reached ();
102   return NULL;
103 #endif
104 }
105
106 /**
107  * pango_cairo_font_map_new_for_font_type:
108  * @fonttype: desired #cairo_font_type_t
109  *
110  * Creates a new #PangoCairoFontMap object of the type suitable
111  * to be used with cairo font backend of type @fonttype.
112  *
113  * In most cases one should simply use @pango_cairo_font_map_new(),
114  * or in fact in most of those cases, just use
115  * @pango_cairo_font_map_get_default().
116  *
117  * Return value: the newly allocated #PangoFontMap of suitable type
118  *               which should be freed with g_object_unref(),
119  *               or %NULL if the requested cairo font backend is
120  *               not supported / compiled in.
121  *
122  * Since: 1.18
123  **/
124 PangoFontMap *
125 pango_cairo_font_map_new_for_font_type (cairo_font_type_t fonttype)
126 {
127   /* Make sure that the type system is initialized */
128   g_type_init ();
129
130   switch ((int) fonttype)
131   {
132 #if defined(HAVE_CAIRO_ATSUI)
133     case CAIRO_FONT_TYPE_QUARTZ:
134       return g_object_new (PANGO_TYPE_CAIRO_ATSUI_FONT_MAP, NULL);
135 #endif
136 #if defined(HAVE_CAIRO_WIN32)
137     case CAIRO_FONT_TYPE_WIN32:
138       return g_object_new (PANGO_TYPE_CAIRO_WIN32_FONT_MAP, NULL);
139 #endif
140 #if defined(HAVE_CAIRO_FREETYPE)
141     case CAIRO_FONT_TYPE_FT:
142       return g_object_new (PANGO_TYPE_CAIRO_FC_FONT_MAP, NULL);
143 #endif
144     default:
145       return NULL;
146   }
147 }
148
149 static PangoFontMap *default_font_map = NULL;
150
151 /**
152  * pango_cairo_font_map_get_default:
153  *
154  * Gets a default #PangoCairoFontMap to use with Cairo.
155  *
156  * Note that the type of the returned object will depend
157  * on the particular font backend Cairo was compiled to use;
158  * You generally should only use the #PangoFontMap and
159  * #PangoCairoFontMap interfaces on the returned object.
160  *
161  * The default Cairo fontmap can be changed by using
162  * pango_cairo_font_map_set_default().  This can be used to
163  * change the Cairo font backend that the default fontmap
164  * uses for example.
165  *
166  * Return value: the default Cairo fontmap for Pango. This
167  *  object is owned by Pango and must not be freed.
168  *
169  * Since: 1.10
170  **/
171 PangoFontMap *
172 pango_cairo_font_map_get_default (void)
173 {
174   if (G_UNLIKELY (!default_font_map))
175     default_font_map = pango_cairo_font_map_new ();
176
177   return default_font_map;
178 }
179
180 /**
181  * pango_cairo_font_map_set_default:
182  * @fontmap: The new default font map, or %NULL
183  *
184  * Sets a default #PangoCairoFontMap to use with Cairo.
185  *
186  * This can be used to change the Cairo font backend that the
187  * default fontmap uses for example.  The old default font map
188  * is unreffed and the new font map referenced.
189  *
190  * A value of %NULL for @fontmap will cause the current default
191  * font map to be released and a new default font
192  * map to be created on demand, using pango_cairo_font_map_new().
193  *
194  * Since: 1.22
195  **/
196 void
197 pango_cairo_font_map_set_default (PangoCairoFontMap *fontmap)
198 {
199   g_return_if_fail (fontmap == NULL || PANGO_IS_CAIRO_FONT_MAP (fontmap));
200
201   if ((PangoFontMap *) fontmap == default_font_map)
202     return;
203
204   if (default_font_map)
205     g_object_unref (default_font_map);
206
207   if (fontmap)
208     g_object_ref (fontmap);
209   default_font_map = (PangoFontMap *) fontmap;
210 }
211
212 /**
213  * pango_cairo_font_map_set_resolution:
214  * @fontmap: a #PangoCairoFontMap
215  * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
216  *   involved; the terminology is conventional.)
217  *
218  * Sets the resolution for the fontmap. This is a scale factor between
219  * points specified in a #PangoFontDescription and Cairo units. The
220  * default value is 96, meaning that a 10 point font will be 13
221  * units high. (10 * 96. / 72. = 13.3).
222  *
223  * Since: 1.10
224  **/
225 void
226 pango_cairo_font_map_set_resolution (PangoCairoFontMap *fontmap,
227                                      double             dpi)
228 {
229   g_return_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap));
230
231   (* PANGO_CAIRO_FONT_MAP_GET_IFACE (fontmap)->set_resolution) (fontmap, dpi);
232 }
233
234 /**
235  * pango_cairo_font_map_get_resolution:
236  * @fontmap: a #PangoCairoFontMap
237  *
238  * Gets the resolution for the fontmap. See pango_cairo_font_map_set_resolution()
239  *
240  * Return value: the resolution in "dots per inch"
241  *
242  * Since: 1.10
243  **/
244 double
245 pango_cairo_font_map_get_resolution (PangoCairoFontMap *fontmap)
246 {
247   g_return_val_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap), 96.);
248
249   return (* PANGO_CAIRO_FONT_MAP_GET_IFACE (fontmap)->get_resolution) (fontmap);
250 }
251
252 /**
253  * pango_cairo_font_map_create_context:
254  * @fontmap: a #PangoCairoFontMap
255  *
256  * Create a #PangoContext for the given fontmap.
257  *
258  * Return value: the newly created context; free with g_object_unref().
259  *
260  * Since: 1.10
261  *
262  * Deprecated: 1.22: Use pango_font_map_create_context() instead.
263  **/
264 PangoContext *
265 pango_cairo_font_map_create_context (PangoCairoFontMap *fontmap)
266 {
267   g_return_val_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap), NULL);
268
269   return pango_font_map_create_context (PANGO_FONT_MAP (fontmap));
270 }
271
272 /**
273  * pango_cairo_font_map_get_font_type:
274  * @fontmap: a #PangoCairoFontMap
275  *
276  * Gets the type of Cairo font backend that @fontmap uses.  
277  *
278  * Return value: the #cairo_font_type_t cairo font backend type
279  *
280  * Since: 1.18
281  **/
282 cairo_font_type_t
283 pango_cairo_font_map_get_font_type (PangoCairoFontMap *fontmap)
284 {
285   g_return_val_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap), CAIRO_FONT_TYPE_TOY);
286
287   return (* PANGO_CAIRO_FONT_MAP_GET_IFACE (fontmap)->get_font_type) (fontmap);
288 }