Git init
[external/pango1.0.git] / pango / pangoft2-fontmap.c
1 /* Pango
2  * pangoft2-fontmap.c:
3  *
4  * Copyright (C) 2000 Red Hat Software
5  * Copyright (C) 2000 Tor Lillqvist
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <fontconfig/fontconfig.h>
32
33 #include "pango-impl-utils.h"
34 #include "pangoft2-private.h"
35 #include "pangofc-fontmap.h"
36
37 typedef struct _PangoFT2Family       PangoFT2Family;
38 typedef struct _PangoFT2FontMapClass PangoFT2FontMapClass;
39
40 struct _PangoFT2FontMap
41 {
42   PangoFcFontMap parent_instance;
43
44   FT_Library library;
45
46   double dpi_x;
47   double dpi_y;
48
49   /* Function to call on prepared patterns to do final
50    * config tweaking.
51    */
52   PangoFT2SubstituteFunc substitute_func;
53   gpointer substitute_data;
54   GDestroyNotify substitute_destroy;
55
56   PangoRenderer *renderer;
57 };
58
59 struct _PangoFT2FontMapClass
60 {
61   PangoFcFontMapClass parent_class;
62 };
63
64 static void          pango_ft2_font_map_finalize            (GObject              *object);
65 static PangoFcFont * pango_ft2_font_map_new_font            (PangoFcFontMap       *fcfontmap,
66                                                              FcPattern            *pattern);
67 static double        pango_ft2_font_map_get_resolution      (PangoFcFontMap       *fcfontmap,
68                                                              PangoContext         *context);
69
70 static PangoFT2FontMap *pango_ft2_global_fontmap = NULL;
71
72 G_DEFINE_TYPE (PangoFT2FontMap, pango_ft2_font_map, PANGO_TYPE_FC_FONT_MAP)
73
74 static void
75 pango_ft2_font_map_class_init (PangoFT2FontMapClass *class)
76 {
77   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
78   PangoFcFontMapClass *fcfontmap_class = PANGO_FC_FONT_MAP_CLASS (class);
79
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;
84 }
85
86 static void
87 pango_ft2_font_map_init (PangoFT2FontMap *fontmap)
88 {
89   fontmap->library = NULL;
90   fontmap->dpi_x   = 72.0;
91   fontmap->dpi_y   = 72.0;
92 }
93
94 static void
95 pango_ft2_font_map_finalize (GObject *object)
96 {
97   PangoFT2FontMap *ft2fontmap = PANGO_FT2_FONT_MAP (object);
98
99   if (ft2fontmap->renderer)
100     g_object_unref (ft2fontmap->renderer);
101
102   if (ft2fontmap->substitute_destroy)
103     ft2fontmap->substitute_destroy (ft2fontmap->substitute_data);
104
105   G_OBJECT_CLASS (pango_ft2_font_map_parent_class)->finalize (object);
106
107   FT_Done_FreeType (ft2fontmap->library);
108 }
109
110 /**
111  * pango_ft2_font_map_new:
112  *
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()).
118  *
119  * Return value: the newly created fontmap object. Unref
120  * with g_object_unref() when you are finished with it.
121  *
122  * Since: 1.2
123  **/
124 PangoFontMap *
125 pango_ft2_font_map_new (void)
126 {
127   PangoFT2FontMap *ft2fontmap;
128   FT_Error error;
129
130   /* Make sure that the type system is initialized */
131   g_type_init ();
132
133   ft2fontmap = g_object_new (PANGO_TYPE_FT2_FONT_MAP, NULL);
134
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");
138
139   return (PangoFontMap *)ft2fontmap;
140 }
141
142 /**
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.
149  *
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.
154  *
155  * Since: 1.2
156  **/
157 void
158 pango_ft2_font_map_set_default_substitute (PangoFT2FontMap        *fontmap,
159                                            PangoFT2SubstituteFunc  func,
160                                            gpointer                data,
161                                            GDestroyNotify          notify)
162 {
163   if (fontmap->substitute_destroy)
164     fontmap->substitute_destroy (fontmap->substitute_data);
165
166   fontmap->substitute_func = func;
167   fontmap->substitute_data = data;
168   fontmap->substitute_destroy = notify;
169
170   pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (fontmap));
171 }
172
173 /**
174  * pango_ft2_font_map_substitute_changed:
175  * @fontmap: a #PangoFT2Fontmap
176  *
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.
182  *
183  * Since: 1.2
184  **/
185 void
186 pango_ft2_font_map_substitute_changed (PangoFT2FontMap *fontmap)
187 {
188   pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (fontmap));
189 }
190
191 /**
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
196  *
197  * Sets the horizontal and vertical resolutions for the fontmap.
198  *
199  * Since: 1.2
200  **/
201 void
202 pango_ft2_font_map_set_resolution (PangoFT2FontMap *fontmap,
203                                    double           dpi_x,
204                                    double           dpi_y)
205 {
206   g_return_if_fail (PANGO_FT2_IS_FONT_MAP (fontmap));
207
208   fontmap->dpi_x = dpi_x;
209   fontmap->dpi_y = dpi_y;
210
211   pango_ft2_font_map_substitute_changed (fontmap);
212 }
213
214 /**
215  * pango_ft2_font_map_create_context:
216  * @fontmap: a #PangoFT2Fontmap
217  *
218  * Create a #PangoContext for the given fontmap.
219  *
220  * Return value: the newly created context; free with g_object_unref().
221  *
222  * Since: 1.2
223  *
224  * Deprecated: 1.22: Use pango_font_map_create_context() instead.
225  **/
226 PangoContext *
227 pango_ft2_font_map_create_context (PangoFT2FontMap *fontmap)
228 {
229   g_return_val_if_fail (PANGO_FT2_IS_FONT_MAP (fontmap), NULL);
230
231   return pango_font_map_create_context (PANGO_FONT_MAP (fontmap));
232 }
233
234 /**
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
238  *
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.
242  *
243  * Return value: the new #PangoContext
244  *
245  * Deprecated: 1.22: Use pango_font_map_create_context() instead.
246  **/
247 PangoContext *
248 pango_ft2_get_context (double dpi_x, double dpi_y)
249 {
250   PangoFontMap *fontmap;
251
252   fontmap = pango_ft2_font_map_for_display ();
253   pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (fontmap), dpi_x, dpi_y);
254
255   return pango_font_map_create_context (fontmap);
256 }
257
258 /**
259  * pango_ft2_font_map_for_display:
260  *
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()
265  * instead.
266  *
267  * Return value: a #PangoFT2FontMap.
268  **/
269 PangoFontMap *
270 pango_ft2_font_map_for_display (void)
271 {
272   if (pango_ft2_global_fontmap != NULL)
273     return PANGO_FONT_MAP (pango_ft2_global_fontmap);
274
275   pango_ft2_global_fontmap = (PangoFT2FontMap *)pango_ft2_font_map_new ();
276
277   return PANGO_FONT_MAP (pango_ft2_global_fontmap);
278 }
279
280 /**
281  * pango_ft2_shutdown_display:
282  *
283  * Free the global fontmap. (See pango_ft2_font_map_for_display())
284  * Use of the global PangoFT2 fontmap is deprecated.
285  **/
286 void
287 pango_ft2_shutdown_display (void)
288 {
289   if (pango_ft2_global_fontmap)
290     {
291       pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (pango_ft2_global_fontmap));
292
293       g_object_unref (pango_ft2_global_fontmap);
294
295       pango_ft2_global_fontmap = NULL;
296     }
297 }
298
299 FT_Library
300 _pango_ft2_font_map_get_library (PangoFontMap *fontmap)
301 {
302   PangoFT2FontMap *ft2fontmap = (PangoFT2FontMap *)fontmap;
303
304   return ft2fontmap->library;
305 }
306
307
308 /**
309  * _pango_ft2_font_map_get_renderer:
310  * @fontmap: a #PangoFT2Fontmap
311  *
312  * Gets the singleton PangoFT2Renderer for this fontmap.
313  *
314  * Return value: the renderer.
315  **/
316 PangoRenderer *
317 _pango_ft2_font_map_get_renderer (PangoFT2FontMap *ft2fontmap)
318 {
319   if (!ft2fontmap->renderer)
320     ft2fontmap->renderer = g_object_new (PANGO_TYPE_FT2_RENDERER, NULL);
321
322   return ft2fontmap->renderer;
323 }
324
325 void
326 _pango_ft2_font_map_default_substitute (PangoFcFontMap *fcfontmap,
327                                        FcPattern      *pattern)
328 {
329   PangoFT2FontMap *ft2fontmap = PANGO_FT2_FONT_MAP (fcfontmap);
330   FcValue v;
331
332   FcConfigSubstitute (NULL, pattern, FcMatchPattern);
333
334   if (ft2fontmap->substitute_func)
335     ft2fontmap->substitute_func (pattern, ft2fontmap->substitute_data);
336
337   if (FcPatternGet (pattern, FC_DPI, 0, &v) == FcResultNoMatch)
338     FcPatternAddDouble (pattern, FC_DPI, ft2fontmap->dpi_y);
339   FcDefaultSubstitute (pattern);
340 }
341
342 static double
343 pango_ft2_font_map_get_resolution (PangoFcFontMap       *fcfontmap,
344                                    PangoContext         *context G_GNUC_UNUSED)
345 {
346   return ((PangoFT2FontMap *)fcfontmap)->dpi_y;
347 }
348
349 static PangoFcFont *
350 pango_ft2_font_map_new_font (PangoFcFontMap  *fcfontmap,
351                              FcPattern       *pattern)
352 {
353   return (PangoFcFont *)_pango_ft2_font_new (PANGO_FT2_FONT_MAP (fcfontmap), pattern);
354 }