"Initial commit to Gerrit"
[profile/ivi/cogl.git] / cogl-pango / cogl-pango-fontmap.c
1 /*
2  * Clutter.
3  *
4  * An OpenGL based 'interactive canvas' library.
5  *
6  * Authored By Matthew Allum  <mallum@openedhand.com>
7  *
8  * Copyright (C) 2008 OpenedHand
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 /**
25  * SECTION:cogl-pango
26  * @short_description: COGL-based text rendering using Pango
27  *
28  * FIXME
29  *
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 /* This is needed to get the Pango headers to export stuff needed to
37    subclass */
38 #ifndef PANGO_ENABLE_BACKEND
39 #define PANGO_ENABLE_BACKEND 1
40 #endif
41
42 #include <pango/pango-fontmap.h>
43 #include <pango/pangocairo.h>
44 #include <pango/pango-renderer.h>
45
46 #include "cogl-pango.h"
47 #include "cogl-pango-private.h"
48
49 static GQuark cogl_pango_font_map_get_renderer_key (void) G_GNUC_CONST;
50
51 /**
52  * cogl_pango_font_map_new:
53  *
54  * Creates a new font map.
55  *
56  * Return value: (transfer full): the newly created #PangoFontMap
57  *
58  * Since: 1.0
59  */
60 PangoFontMap *
61 cogl_pango_font_map_new (void)
62 {
63   return pango_cairo_font_map_new ();
64 }
65
66 /**
67  * cogl_pango_font_map_create_context:
68  * @fm: a #CoglPangoFontMap
69  *
70  * Creates a new #PangoContext from the passed font map.
71  *
72  * Return value: (transfer full): the newly created #PangoContext
73  *
74  * Since: 1.0
75  */
76 PangoContext *
77 cogl_pango_font_map_create_context (CoglPangoFontMap *fm)
78 {
79   g_return_val_if_fail (COGL_PANGO_IS_FONT_MAP (fm), NULL);
80
81   /* We can just directly use the pango context from the Cairo font
82      map */
83   return pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fm));
84 }
85
86 /**
87  * cogl_pango_font_map_get_renderer:
88  * @fm: a #CoglPangoFontMap
89  *
90  * Retrieves the #CoglPangoRenderer for the passed font map.
91  *
92  * Return value: (transfer none): a #PangoRenderer
93  *
94  * Since: 1.0
95  */
96 PangoRenderer *
97 cogl_pango_font_map_get_renderer (CoglPangoFontMap *fm)
98 {
99   PangoRenderer *renderer;
100
101   g_return_val_if_fail (COGL_PANGO_IS_FONT_MAP (fm), NULL);
102
103   /* We want to keep a cached pointer to the renderer from the font
104      map instance but as we don't have a proper subclass we have to
105      store it in the object data instead */
106
107   renderer = g_object_get_qdata (G_OBJECT (fm),
108                                  cogl_pango_font_map_get_renderer_key ());
109
110   if (G_UNLIKELY (renderer == NULL))
111     {
112       renderer = g_object_new (COGL_PANGO_TYPE_RENDERER, NULL);
113       g_object_set_qdata_full (G_OBJECT (fm),
114                                cogl_pango_font_map_get_renderer_key (),
115                                renderer,
116                                g_object_unref);
117     }
118
119   return renderer;
120 }
121
122 /**
123  * cogl_pango_font_map_set_resolution:
124  * @font_map: a #CoglPangoFontMap
125  * @dpi: DPI to set
126  *
127  * Sets the resolution to be used by @font_map at @dpi.
128  *
129  * Since: 1.0
130  */
131 void
132 cogl_pango_font_map_set_resolution (CoglPangoFontMap *font_map,
133                                     double            dpi)
134 {
135   g_return_if_fail (COGL_PANGO_IS_FONT_MAP (font_map));
136
137   pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (font_map), dpi);
138 }
139
140 /**
141  * cogl_pango_font_map_clear_glyph_cache:
142  * @fm: a #CoglPangoFontMap
143  *
144  * Clears the glyph cache for @fm.
145  *
146  * Since: 1.0
147  */
148 void
149 cogl_pango_font_map_clear_glyph_cache (CoglPangoFontMap *fm)
150 {
151   PangoRenderer *renderer;
152
153   renderer = cogl_pango_font_map_get_renderer (fm);
154
155   _cogl_pango_renderer_clear_glyph_cache (COGL_PANGO_RENDERER (renderer));
156 }
157
158 /**
159  * cogl_pango_font_map_set_use_mipmapping:
160  * @fm: a #CoglPangoFontMap
161  * @value: %TRUE to enable the use of mipmapping
162  *
163  * Sets whether the renderer for the passed font map should use
164  * mipmapping when rendering a #PangoLayout.
165  *
166  * Since: 1.0
167  */
168 void
169 cogl_pango_font_map_set_use_mipmapping (CoglPangoFontMap *fm,
170                                         gboolean          value)
171 {
172   CoglPangoRenderer *renderer;
173
174   renderer = COGL_PANGO_RENDERER (cogl_pango_font_map_get_renderer (fm));
175
176   _cogl_pango_renderer_set_use_mipmapping (renderer, value);
177 }
178
179 /**
180  * cogl_pango_font_map_get_use_mipmapping:
181  * @fm: a #CoglPangoFontMap
182  *
183  * Retrieves whether the #CoglPangoRenderer used by @fm will
184  * use mipmapping when rendering the glyphs.
185  *
186  * Return value: %TRUE if mipmapping is used, %FALSE otherwise.
187  *
188  * Since: 1.0
189  */
190 gboolean
191 cogl_pango_font_map_get_use_mipmapping (CoglPangoFontMap *fm)
192 {
193   CoglPangoRenderer *renderer;
194
195   renderer = COGL_PANGO_RENDERER (cogl_pango_font_map_get_renderer (fm));
196
197   return _cogl_pango_renderer_get_use_mipmapping (renderer);
198 }
199
200 static GQuark
201 cogl_pango_font_map_get_renderer_key (void)
202 {
203   static GQuark renderer_key = 0;
204
205   if (G_UNLIKELY (renderer_key == 0))
206       renderer_key = g_quark_from_static_string ("CoglPangoFontMap");
207
208   return renderer_key;
209 }