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_CORE_TEXT) && defined (HAVE_CAIRO_QUARTZ)
29 #  include "pangocairo-coretext.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 
39 typedef PangoCairoFontMapIface PangoCairoFontMapInterface;
G_DEFINE_INTERFACE(PangoCairoFontMap,pango_cairo_font_map,PANGO_TYPE_FONT_MAP)40 G_DEFINE_INTERFACE (PangoCairoFontMap, pango_cairo_font_map, PANGO_TYPE_FONT_MAP)
41 
42 static void
43 pango_cairo_font_map_default_init (PangoCairoFontMapIface *iface)
44 {
45 }
46 
47 /**
48  * pango_cairo_font_map_new:
49  *
50  * Creates a new `PangoCairoFontMap` object.
51  *
52  * A fontmap is used to cache information about available fonts,
53  * and holds certain global parameters such as the resolution.
54  * In most cases, you can use `func@PangoCairo.font_map_get_default]
55  * instead.
56  *
57  * Note that the type of the returned object will depend
58  * on the particular font backend Cairo was compiled to use;
59  * You generally should only use the `PangoFontMap` and
60  * `PangoCairoFontMap` interfaces on the returned object.
61  *
62  * You can override the type of backend returned by using an
63  * environment variable %PANGOCAIRO_BACKEND. Supported types,
64  * based on your build, are fc (fontconfig), win32, and coretext.
65  * If requested type is not available, NULL is returned. Ie.
66  * this is only useful for testing, when at least two backends
67  * are compiled in.
68  *
69  * Return value: (transfer full): the newly allocated `PangoFontMap`,
70  *   which should be freed with g_object_unref().
71  *
72  * Since: 1.10
73  */
74 PangoFontMap *
pango_cairo_font_map_new(void)75 pango_cairo_font_map_new (void)
76 {
77   const char *backend = getenv ("PANGOCAIRO_BACKEND");
78   if (backend && !*backend)
79     backend = NULL;
80 #if defined(HAVE_CORE_TEXT) && defined (HAVE_CAIRO_QUARTZ)
81   if (!backend || 0 == strcmp (backend, "coretext"))
82     return g_object_new (PANGO_TYPE_CAIRO_CORE_TEXT_FONT_MAP, NULL);
83 #endif
84 #if defined(HAVE_CAIRO_WIN32)
85   if (!backend || 0 == strcmp (backend, "win32"))
86     return g_object_new (PANGO_TYPE_CAIRO_WIN32_FONT_MAP, NULL);
87 #endif
88 #if defined(HAVE_CAIRO_FREETYPE)
89   if (!backend || 0 == strcmp (backend, "fc")
90 	       || 0 == strcmp (backend, "fontconfig"))
91     return g_object_new (PANGO_TYPE_CAIRO_FC_FONT_MAP, NULL);
92 #endif
93   {
94     const char backends[] = ""
95 #if defined(HAVE_CORE_TEXT) && defined (HAVE_CAIRO_QUARTZ)
96       " coretext"
97 #endif
98 #if defined(HAVE_CAIRO_WIN32)
99       " win32"
100 #endif
101 #if defined(HAVE_CAIRO_FREETYPE)
102       " fontconfig"
103 #endif
104       ;
105     g_critical ("Unknown $PANGOCAIRO_BACKEND value.\n  Available backends are:%s", backends);
106   }
107   return NULL;
108 }
109 
110 /**
111  * pango_cairo_font_map_new_for_font_type:
112  * @fonttype: desired #cairo_font_type_t
113  *
114  * Creates a new `PangoCairoFontMap` object of the type suitable
115  * to be used with cairo font backend of type @fonttype.
116  *
117  * In most cases one should simply use [func@PangoCairo.FontMap.new], or
118  * in fact in most of those cases, just use [func@PangoCairo.FontMap.get_default].
119  *
120  * Return value: (transfer full) (nullable): the newly allocated
121  *   `PangoFontMap` of suitable type which should be freed with
122  *   g_object_unref(), or %NULL if the requested cairo font backend
123  *   is not supported / compiled in.
124  *
125  * Since: 1.18
126  */
127 PangoFontMap *
pango_cairo_font_map_new_for_font_type(cairo_font_type_t fonttype)128 pango_cairo_font_map_new_for_font_type (cairo_font_type_t fonttype)
129 {
130   switch ((int) fonttype)
131   {
132 #if defined(HAVE_CORE_TEXT) && defined (HAVE_CAIRO_QUARTZ)
133     case CAIRO_FONT_TYPE_QUARTZ:
134       return g_object_new (PANGO_TYPE_CAIRO_CORE_TEXT_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 GPrivate default_font_map = G_PRIVATE_INIT (g_object_unref); /* MT-safe */
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 on the
157  * particular font backend Cairo was compiled to use; you generally
158  * should only use the `PangoFontMap` and `PangoCairoFontMap`
159  * interfaces on the returned object.
160  *
161  * The default Cairo fontmap can be changed by using
162  * [method@PangoCairo.FontMap.set_default]. This can be used to
163  * change the Cairo font backend that the default fontmap uses
164  * for example.
165  *
166  * Note that since Pango 1.32.6, the default fontmap is per-thread.
167  * Each thread gets its own default fontmap. In this way, PangoCairo
168  * can be used safely from multiple threads.
169  *
170  * Return value: (transfer none): the default PangoCairo fontmap
171  *  for the current thread. This object is owned by Pango and must
172  *  not be freed.
173  *
174  * Since: 1.10
175  */
176 PangoFontMap *
pango_cairo_font_map_get_default(void)177 pango_cairo_font_map_get_default (void)
178 {
179   PangoFontMap *fontmap = g_private_get (&default_font_map);
180 
181   if (G_UNLIKELY (!fontmap))
182     {
183       fontmap = pango_cairo_font_map_new ();
184       g_private_replace (&default_font_map, fontmap);
185     }
186 
187   return fontmap;
188 }
189 
190 /**
191  * pango_cairo_font_map_set_default:
192  * @fontmap: (nullable): The new default font map
193  *
194  * Sets a default `PangoCairoFontMap` to use with Cairo.
195  *
196  * This can be used to change the Cairo font backend that the
197  * default fontmap uses for example. The old default font map
198  * is unreffed and the new font map referenced.
199  *
200  * Note that since Pango 1.32.6, the default fontmap is per-thread.
201  * This function only changes the default fontmap for
202  * the current thread. Default fontmaps of existing threads
203  * are not changed. Default fontmaps of any new threads will
204  * still be created using [func@PangoCairo.FontMap.new].
205  *
206  * A value of %NULL for @fontmap will cause the current default
207  * font map to be released and a new default font map to be created
208  * on demand, using [func@PangoCairo.FontMap.new].
209  *
210  * Since: 1.22
211  */
212 void
pango_cairo_font_map_set_default(PangoCairoFontMap * fontmap)213 pango_cairo_font_map_set_default (PangoCairoFontMap *fontmap)
214 {
215   g_return_if_fail (fontmap == NULL || PANGO_IS_CAIRO_FONT_MAP (fontmap));
216 
217   if (fontmap)
218     g_object_ref (fontmap);
219 
220   g_private_replace (&default_font_map, fontmap);
221 }
222 
223 /**
224  * pango_cairo_font_map_set_resolution:
225  * @fontmap: a `PangoCairoFontMap`
226  * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
227  *   involved; the terminology is conventional.)
228  *
229  * Sets the resolution for the fontmap.
230  *
231  * This is a scale factor between
232  * points specified in a `PangoFontDescription` and Cairo units. The
233  * default value is 96, meaning that a 10 point font will be 13
234  * units high. (10 * 96. / 72. = 13.3).
235  *
236  * Since: 1.10
237  */
238 void
pango_cairo_font_map_set_resolution(PangoCairoFontMap * fontmap,double dpi)239 pango_cairo_font_map_set_resolution (PangoCairoFontMap *fontmap,
240                                      double             dpi)
241 {
242   g_return_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap));
243 
244   (* PANGO_CAIRO_FONT_MAP_GET_IFACE (fontmap)->set_resolution) (fontmap, dpi);
245 }
246 
247 /**
248  * pango_cairo_font_map_get_resolution:
249  * @fontmap: a `PangoCairoFontMap`
250  *
251  * Gets the resolution for the fontmap.
252  *
253  * See [method@PangoCairo.FontMap.set_resolution].
254  *
255  * Return value: the resolution in "dots per inch"
256  *
257  * Since: 1.10
258  **/
259 double
pango_cairo_font_map_get_resolution(PangoCairoFontMap * fontmap)260 pango_cairo_font_map_get_resolution (PangoCairoFontMap *fontmap)
261 {
262   g_return_val_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap), 96.);
263 
264   return (* PANGO_CAIRO_FONT_MAP_GET_IFACE (fontmap)->get_resolution) (fontmap);
265 }
266 
267 /**
268  * pango_cairo_font_map_create_context: (skip)
269  * @fontmap: a `PangoCairoFontMap`
270  *
271  * Create a `PangoContext` for the given fontmap.
272  *
273  * Return value: the newly created context; free with g_object_unref().
274  *
275  * Since: 1.10
276  *
277  * Deprecated: 1.22: Use pango_font_map_create_context() instead.
278  */
279 PangoContext *
pango_cairo_font_map_create_context(PangoCairoFontMap * fontmap)280 pango_cairo_font_map_create_context (PangoCairoFontMap *fontmap)
281 {
282   g_return_val_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap), NULL);
283 
284   return pango_font_map_create_context (PANGO_FONT_MAP (fontmap));
285 }
286 
287 /**
288  * pango_cairo_font_map_get_font_type:
289  * @fontmap: a `PangoCairoFontMap`
290  *
291  * Gets the type of Cairo font backend that @fontmap uses.
292  *
293  * Return value: the `cairo_font_type_t` cairo font backend type
294  *
295  * Since: 1.18
296  */
297 cairo_font_type_t
pango_cairo_font_map_get_font_type(PangoCairoFontMap * fontmap)298 pango_cairo_font_map_get_font_type (PangoCairoFontMap *fontmap)
299 {
300   g_return_val_if_fail (PANGO_IS_CAIRO_FONT_MAP (fontmap), CAIRO_FONT_TYPE_TOY);
301 
302   return (* PANGO_CAIRO_FONT_MAP_GET_IFACE (fontmap)->get_font_type) (fontmap);
303 }
304