2 * pangoxft-fontmap.c: Xft font handling
4 * Copyright (C) 2000-2003 Red Hat Software
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.
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.
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.
26 #include "pangofc-fontmap.h"
28 #include "pangoxft-private.h"
30 /* For XExtSetCloseDisplay */
31 #include <X11/Xlibint.h>
33 typedef struct _PangoXftFamily PangoXftFamily;
34 typedef struct _PangoXftFontMapClass PangoXftFontMapClass;
36 #define PANGO_TYPE_XFT_FONT_MAP (pango_xft_font_map_get_type ())
37 #define PANGO_XFT_FONT_MAP(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_XFT_FONT_MAP, PangoXftFontMap))
38 #define PANGO_XFT_IS_FONT_MAP(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_XFT_FONT_MAP))
40 struct _PangoXftFontMap
42 PangoFcFontMap parent_instance;
47 /* Function to call on prepared patterns to do final
50 PangoXftSubstituteFunc substitute_func;
51 gpointer substitute_data;
52 GDestroyNotify substitute_destroy;
54 PangoRenderer *renderer;
57 struct _PangoXftFontMapClass
59 PangoFcFontMapClass parent_class;
62 static void pango_xft_font_map_default_substitute (PangoFcFontMap *fcfontmap,
64 static PangoFcFont * pango_xft_font_map_new_font (PangoFcFontMap *fcfontmap,
66 static void pango_xft_font_map_finalize (GObject *object);
68 static GSList *fontmaps = NULL;
70 G_DEFINE_TYPE (PangoXftFontMap, pango_xft_font_map, PANGO_TYPE_FC_FONT_MAP)
73 pango_xft_font_map_class_init (PangoXftFontMapClass *class)
75 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
76 PangoFcFontMapClass *fcfontmap_class = PANGO_FC_FONT_MAP_CLASS (class);
78 gobject_class->finalize = pango_xft_font_map_finalize;
79 fcfontmap_class->default_substitute = pango_xft_font_map_default_substitute;
80 fcfontmap_class->new_font = pango_xft_font_map_new_font;
84 pango_xft_font_map_init (PangoXftFontMap *xftfontmap G_GNUC_UNUSED)
89 pango_xft_font_map_finalize (GObject *object)
91 PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (object);
93 if (xftfontmap->renderer)
94 g_object_unref (xftfontmap->renderer);
96 fontmaps = g_slist_remove (fontmaps, object);
98 if (xftfontmap->substitute_destroy)
99 xftfontmap->substitute_destroy (xftfontmap->substitute_data);
101 G_OBJECT_CLASS (pango_xft_font_map_parent_class)->finalize (object);
105 static PangoFontMap *
106 pango_xft_find_font_map (Display *display,
114 PangoXftFontMap *xftfontmap = tmp_list->data;
116 if (xftfontmap->display == display &&
117 xftfontmap->screen == screen)
118 return PANGO_FONT_MAP (xftfontmap);
120 tmp_list = tmp_list->next;
127 * Hackery to set up notification when a Display is closed
129 static GSList *registered_displays;
132 close_display_cb (Display *display,
133 XExtCodes *extcodes G_GNUC_UNUSED)
140 PangoXftFontMap *xftfontmap = tmp_list->data;
141 tmp_list = tmp_list->next;
143 if (xftfontmap->display == display)
144 pango_xft_shutdown_display (display, xftfontmap->screen);
147 registered_displays = g_slist_remove (registered_displays, display);
153 register_display (Display *display)
158 for (tmp_list = registered_displays; tmp_list; tmp_list = tmp_list->next)
160 if (tmp_list->data == display)
164 registered_displays = g_slist_prepend (registered_displays, display);
166 extcodes = XAddExtension (display);
167 XESetCloseDisplay (display, extcodes->extension, close_display_cb);
171 * pango_xft_get_font_map:
172 * @display: an X display
173 * @screen: the screen number of a screen within @display
175 * Returns the #PangoXftFontmap for the given display and screen.
176 * The fontmap is owned by Pango and will be valid until
177 * the display is closed.
179 * Return value: a #PangoFontMap object, owned by Pango.
184 pango_xft_get_font_map (Display *display,
187 PangoFontMap *fontmap;
188 PangoXftFontMap *xftfontmap;
190 g_return_val_if_fail (display != NULL, NULL);
192 fontmap = pango_xft_find_font_map (display, screen);
196 /* Make sure that the type system is initialized */
199 xftfontmap = (PangoXftFontMap *)g_object_new (PANGO_TYPE_XFT_FONT_MAP, NULL);
201 xftfontmap->display = display;
202 xftfontmap->screen = screen;
204 register_display (display);
206 fontmaps = g_slist_prepend (fontmaps, xftfontmap);
208 return PANGO_FONT_MAP (xftfontmap);
212 * pango_xft_shutdown_display:
213 * @display: an X display
214 * @screen: the screen number of a screen within @display
216 * Release any resources that have been cached for the
217 * combination of @display and @screen. Note that when the
218 * X display is closed, resources are released automatically,
219 * without needing to call this function.
224 pango_xft_shutdown_display (Display *display,
227 PangoFontMap *fontmap;
229 fontmap = pango_xft_find_font_map (display, screen);
232 PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (fontmap);
234 fontmaps = g_slist_remove (fontmaps, fontmap);
235 pango_fc_font_map_shutdown (PANGO_FC_FONT_MAP (fontmap));
237 xftfontmap->display = NULL;
238 g_object_unref (fontmap);
243 * pango_xft_set_default_substitute:
244 * @display: an X Display
245 * @screen: the screen number of a screen within @display
246 * @func: function to call to to do final config tweaking
247 * on #FcPattern objects.
248 * @data: data to pass to @func
249 * @notify: function to call when @data is no longer used.
251 * Sets a function that will be called to do final configuration
252 * substitution on a #FcPattern before it is used to load
253 * the font. This function can be used to do things like set
254 * hinting and antialiasing options.
259 pango_xft_set_default_substitute (Display *display,
261 PangoXftSubstituteFunc func,
263 GDestroyNotify notify)
265 PangoXftFontMap *xftfontmap = (PangoXftFontMap *)pango_xft_get_font_map (display, screen);
267 if (xftfontmap->substitute_destroy)
268 xftfontmap->substitute_destroy (xftfontmap->substitute_data);
270 xftfontmap->substitute_func = func;
271 xftfontmap->substitute_data = data;
272 xftfontmap->substitute_destroy = notify;
274 pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (xftfontmap));
278 * pango_xft_substitute_changed:
279 * @display: an X Display
280 * @screen: the screen number of a screen within @display
282 * Call this function any time the results of the
283 * default substitution function set with
284 * pango_xft_set_default_substitute() change.
285 * That is, if your substitution function will return different
286 * results for the same input pattern, you must call this function.
291 pango_xft_substitute_changed (Display *display,
294 PangoXftFontMap *xftfontmap = (PangoXftFontMap *)pango_xft_get_font_map (display, screen);
296 pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (xftfontmap));
300 _pango_xft_font_map_get_info (PangoFontMap *fontmap,
304 PangoXftFontMap *xftfontmap = (PangoXftFontMap *)fontmap;
307 *display = xftfontmap->display;
309 *screen = xftfontmap->screen;
313 * pango_xft_get_context:
314 * @display: an X display.
315 * @screen: an X screen.
317 * Retrieves a #PangoContext appropriate for rendering with
318 * Xft fonts on the given screen of the given display.
320 * Return value: the new #PangoContext.
322 * Deprecated: 1.22: Use pango_xft_get_font_map() followed by
323 * pango_font_map_create_context() instead.
326 pango_xft_get_context (Display *display,
329 g_return_val_if_fail (display != NULL, NULL);
331 return pango_font_map_create_context (pango_xft_get_font_map (display, screen));
335 * _pango_xft_font_map_get_renderer:
336 * @fontmap: a #PangoXftFontmap
338 * Gets the singleton #PangoXFTRenderer for this fontmap.
340 * Return value: the renderer.
343 _pango_xft_font_map_get_renderer (PangoXftFontMap *xftfontmap)
345 if (!xftfontmap->renderer)
346 xftfontmap->renderer = pango_xft_renderer_new (xftfontmap->display,
349 return xftfontmap->renderer;
353 pango_xft_font_map_default_substitute (PangoFcFontMap *fcfontmap,
356 PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (fcfontmap);
359 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
360 if (xftfontmap->substitute_func)
361 xftfontmap->substitute_func (pattern, xftfontmap->substitute_data);
362 XftDefaultSubstitute (xftfontmap->display, xftfontmap->screen, pattern);
363 if (FcPatternGetDouble (pattern, FC_PIXEL_SIZE, 0, &d) == FcResultMatch && d == 0.0)
366 v.type = FcTypeDouble;
368 FcPatternAdd (pattern, FC_PIXEL_SIZE, v, FcFalse);
373 pango_xft_font_map_new_font (PangoFcFontMap *fcfontmap,
376 return (PangoFcFont *)_pango_xft_font_new (PANGO_XFT_FONT_MAP (fcfontmap), pattern);