4 * Copyright (C) 2000 Red Hat Software
5 * Copyright (C) 2000 Tor Lillqvist
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
31 #include <fontconfig/fontconfig.h>
33 #include "pango-impl-utils.h"
34 #include "pangoft2-private.h"
35 #include "pangofc-fontmap.h"
37 typedef struct _PangoFT2Family PangoFT2Family;
38 typedef struct _PangoFT2FontMapClass PangoFT2FontMapClass;
40 struct _PangoFT2FontMap
42 PangoFcFontMap parent_instance;
49 /* Function to call on prepared patterns to do final
52 PangoFT2SubstituteFunc substitute_func;
53 gpointer substitute_data;
54 GDestroyNotify substitute_destroy;
56 PangoRenderer *renderer;
59 struct _PangoFT2FontMapClass
61 PangoFcFontMapClass parent_class;
64 static void pango_ft2_font_map_finalize (GObject *object);
65 static PangoFcFont * pango_ft2_font_map_new_font (PangoFcFontMap *fcfontmap,
67 static double pango_ft2_font_map_get_resolution (PangoFcFontMap *fcfontmap,
68 PangoContext *context);
70 static PangoFT2FontMap *pango_ft2_global_fontmap = NULL;
72 G_DEFINE_TYPE (PangoFT2FontMap, pango_ft2_font_map, PANGO_TYPE_FC_FONT_MAP)
75 pango_ft2_font_map_class_init (PangoFT2FontMapClass *class)
77 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
78 PangoFcFontMapClass *fcfontmap_class = PANGO_FC_FONT_MAP_CLASS (class);
80 gobject_class->finalize = pango_ft2_font_map_finalize;
81 fcfontmap_class->default_substitute = _pango_ft2_font_map_default_substitute;
82 fcfontmap_class->new_font = pango_ft2_font_map_new_font;
83 fcfontmap_class->get_resolution = pango_ft2_font_map_get_resolution;
87 pango_ft2_font_map_init (PangoFT2FontMap *fontmap)
89 fontmap->library = NULL;
90 fontmap->dpi_x = 72.0;
91 fontmap->dpi_y = 72.0;
95 pango_ft2_font_map_finalize (GObject *object)
97 PangoFT2FontMap *ft2fontmap = PANGO_FT2_FONT_MAP (object);
99 if (ft2fontmap->renderer)
100 g_object_unref (ft2fontmap->renderer);
102 if (ft2fontmap->substitute_destroy)
103 ft2fontmap->substitute_destroy (ft2fontmap->substitute_data);
105 G_OBJECT_CLASS (pango_ft2_font_map_parent_class)->finalize (object);
107 FT_Done_FreeType (ft2fontmap->library);
111 * pango_ft2_font_map_new:
113 * Create a new #PangoFT2FontMap object; a fontmap is used
114 * to cache information about available fonts, and holds
115 * certain global parameters such as the resolution and
116 * the default substitute function (see
117 * pango_ft2_font_map_set_default_substitute()).
119 * Return value: the newly created fontmap object. Unref
120 * with g_object_unref() when you are finished with it.
125 pango_ft2_font_map_new (void)
127 PangoFT2FontMap *ft2fontmap;
130 /* Make sure that the type system is initialized */
133 ft2fontmap = g_object_new (PANGO_TYPE_FT2_FONT_MAP, NULL);
135 error = FT_Init_FreeType (&ft2fontmap->library);
136 if (error != FT_Err_Ok)
137 g_critical ("pango_ft2_font_map_new: Could not initialize freetype");
139 return (PangoFontMap *)ft2fontmap;
143 * pango_ft2_font_map_set_default_substitute:
144 * @fontmap: a #PangoFT2FontMap
145 * @func: function to call to to do final config tweaking
146 * on #FcPattern objects.
147 * @data: data to pass to @func
148 * @notify: function to call when @data is no longer used.
150 * Sets a function that will be called to do final configuration
151 * substitution on a #FcPattern before it is used to load
152 * the font. This function can be used to do things like set
153 * hinting and antialiasing options.
158 pango_ft2_font_map_set_default_substitute (PangoFT2FontMap *fontmap,
159 PangoFT2SubstituteFunc func,
161 GDestroyNotify notify)
163 if (fontmap->substitute_destroy)
164 fontmap->substitute_destroy (fontmap->substitute_data);
166 fontmap->substitute_func = func;
167 fontmap->substitute_data = data;
168 fontmap->substitute_destroy = notify;
170 pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (fontmap));
174 * pango_ft2_font_map_substitute_changed:
175 * @fontmap: a #PangoFT2Fontmap
177 * Call this function any time the results of the
178 * default substitution function set with
179 * pango_ft2_font_map_set_default_substitute() change.
180 * That is, if your substitution function will return different
181 * results for the same input pattern, you must call this function.
186 pango_ft2_font_map_substitute_changed (PangoFT2FontMap *fontmap)
188 pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (fontmap));
192 * pango_ft2_font_map_set_resolution:
193 * @fontmap: a #PangoFT2Fontmap
194 * @dpi_x: dots per inch in the X direction
195 * @dpi_y: dots per inch in the Y direction
197 * Sets the horizontal and vertical resolutions for the fontmap.
202 pango_ft2_font_map_set_resolution (PangoFT2FontMap *fontmap,
206 g_return_if_fail (PANGO_FT2_IS_FONT_MAP (fontmap));
208 fontmap->dpi_x = dpi_x;
209 fontmap->dpi_y = dpi_y;
211 pango_ft2_font_map_substitute_changed (fontmap);
215 * pango_ft2_font_map_create_context:
216 * @fontmap: a #PangoFT2Fontmap
218 * Create a #PangoContext for the given fontmap.
220 * Return value: the newly created context; free with g_object_unref().
224 * Deprecated: 1.22: Use pango_font_map_create_context() instead.
227 pango_ft2_font_map_create_context (PangoFT2FontMap *fontmap)
229 g_return_val_if_fail (PANGO_FT2_IS_FONT_MAP (fontmap), NULL);
231 return pango_font_map_create_context (PANGO_FONT_MAP (fontmap));
235 * pango_ft2_get_context:
236 * @dpi_x: the horizontal DPI of the target device
237 * @dpi_y: the vertical DPI of the target device
239 * Retrieves a #PangoContext for the default PangoFT2 fontmap
240 * (see pango_ft2_fontmap_get_for_display()) and sets the resolution
241 * for the default fontmap to @dpi_x by @dpi_y.
243 * Return value: the new #PangoContext
245 * Deprecated: 1.22: Use pango_font_map_create_context() instead.
248 pango_ft2_get_context (double dpi_x, double dpi_y)
250 PangoFontMap *fontmap;
252 fontmap = pango_ft2_font_map_for_display ();
253 pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (fontmap), dpi_x, dpi_y);
255 return pango_font_map_create_context (fontmap);
259 * pango_ft2_font_map_for_display:
261 * Returns a #PangoFT2FontMap. This font map is cached and should
262 * not be freed. If the font map is no longer needed, it can
263 * be released with pango_ft2_shutdown_display(). Use of the
264 * global PangoFT2 fontmap is deprecated; use pango_ft2_font_map_new()
267 * Return value: a #PangoFT2FontMap.
270 pango_ft2_font_map_for_display (void)
272 if (pango_ft2_global_fontmap != NULL)
273 return PANGO_FONT_MAP (pango_ft2_global_fontmap);
275 pango_ft2_global_fontmap = (PangoFT2FontMap *)pango_ft2_font_map_new ();
277 return PANGO_FONT_MAP (pango_ft2_global_fontmap);
281 * pango_ft2_shutdown_display:
283 * Free the global fontmap. (See pango_ft2_font_map_for_display())
284 * Use of the global PangoFT2 fontmap is deprecated.
287 pango_ft2_shutdown_display (void)
289 if (pango_ft2_global_fontmap)
291 pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (pango_ft2_global_fontmap));
293 g_object_unref (pango_ft2_global_fontmap);
295 pango_ft2_global_fontmap = NULL;
300 _pango_ft2_font_map_get_library (PangoFontMap *fontmap)
302 PangoFT2FontMap *ft2fontmap = (PangoFT2FontMap *)fontmap;
304 return ft2fontmap->library;
309 * _pango_ft2_font_map_get_renderer:
310 * @fontmap: a #PangoFT2Fontmap
312 * Gets the singleton PangoFT2Renderer for this fontmap.
314 * Return value: the renderer.
317 _pango_ft2_font_map_get_renderer (PangoFT2FontMap *ft2fontmap)
319 if (!ft2fontmap->renderer)
320 ft2fontmap->renderer = g_object_new (PANGO_TYPE_FT2_RENDERER, NULL);
322 return ft2fontmap->renderer;
326 _pango_ft2_font_map_default_substitute (PangoFcFontMap *fcfontmap,
329 PangoFT2FontMap *ft2fontmap = PANGO_FT2_FONT_MAP (fcfontmap);
332 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
334 if (ft2fontmap->substitute_func)
335 ft2fontmap->substitute_func (pattern, ft2fontmap->substitute_data);
337 if (FcPatternGet (pattern, FC_DPI, 0, &v) == FcResultNoMatch)
338 FcPatternAddDouble (pattern, FC_DPI, ft2fontmap->dpi_y);
339 FcDefaultSubstitute (pattern);
343 pango_ft2_font_map_get_resolution (PangoFcFontMap *fcfontmap,
344 PangoContext *context G_GNUC_UNUSED)
346 return ((PangoFT2FontMap *)fcfontmap)->dpi_y;
350 pango_ft2_font_map_new_font (PangoFcFontMap *fcfontmap,
353 return (PangoFcFont *)_pango_ft2_font_new (PANGO_FT2_FONT_MAP (fcfontmap), pattern);