]> Pileus Git - ~andy/gtk/blob - gdk/gdkpango.c
Don't use deprecated Pango api
[~andy/gtk] / gdk / gdkpango.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2000 Red Hat, Inc. 
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gdkpango.h"
23
24 #include "gdkscreen.h"
25 #include "gdkintl.h"
26
27 #include <math.h>
28 #include <pango/pangocairo.h>
29
30
31 /**
32  * SECTION:pango_interaction
33  * @Short_description: Using Pango in GDK
34  * @Title: Pango Interaction
35  *
36  * Pango is the text layout system used by GDK and GTK+. The functions
37  * and types in this section are used to obtain clip regions for
38  * #PangoLayouts, and to get #PangoContexts that can be used with
39  * GDK.
40  *
41  * Creating a #PangoLayout object is the first step in rendering text,
42  * and requires getting a handle to a #PangoContext. For GTK+ programs,
43  * you'll usually want to use gtk_widget_get_pango_context(), or
44  * gtk_widget_create_pango_layout(), rather than using the lowlevel
45  * gdk_pango_context_get_for_screen(). Once you have a #PangoLayout, you
46  * can set the text and attributes of it with Pango functions like
47  * pango_layout_set_text() and get its size with pango_layout_get_size().
48  * (Note that Pango uses a fixed point system internally, so converting
49  * between Pango units and pixels using <link
50  * linkend="PANGO-SCALE-CAPS">PANGO_SCALE</link> or the PANGO_PIXELS() macro.)
51  *
52  * Rendering a Pango layout is done most simply with pango_cairo_show_layout();
53  * you can also draw pieces of the layout with pango_cairo_show_layout_line().
54  * <example id="rotated-example">
55  * <title>Draw transformed text with Pango and cairo</title>
56  * <!-- Note that this example is basically the same as
57  *      demos/gtk-demo/rotated_text.c -->
58  * <programlisting>
59  * #define RADIUS 100
60  * #define N_WORDS 10
61  * #define FONT "Sans Bold 18"
62  *
63  * PangoContext *context;
64  * PangoLayout *layout;
65  * PangoFontDescription *desc;
66  *
67  * double radius;
68  * int width, height;
69  * int i;
70  *
71  * /<!---->* Set up a transformation matrix so that the user space coordinates for
72  *  * where we are drawing are [-RADIUS, RADIUS], [-RADIUS, RADIUS]
73  *  * We first center, then change the scale *<!---->/
74  *
75  * width = gdk_window_get_width (window);
76  * height = gdk_window_get_height (window);
77  * radius = MIN (width, height) / 2.;
78  *
79  * cairo_translate (cr,
80  *                  radius + (width - 2 * radius) / 2,
81  *                  radius + (height - 2 * radius) / 2);
82  *                  cairo_scale (cr, radius / RADIUS, radius / RADIUS);
83  *
84  * /<!---->* Create a PangoLayout, set the font and text *<!---->/
85  * context = gdk_pango_context_get_for_screen (screen);
86  * layout = pango_layout_new (context);
87  * pango_layout_set_text (layout, "Text", -1);
88  * desc = pango_font_description_from_string (FONT);
89  * pango_layout_set_font_description (layout, desc);
90  * pango_font_description_free (desc);
91  *
92  * /<!---->* Draw the layout N_WORDS times in a circle *<!---->/
93  * for (i = 0; i < N_WORDS; i++)
94  *   {
95  *     double red, green, blue;
96  *     double angle = 2 * G_PI * i / n_words;
97  *
98  *     cairo_save (cr);
99  *
100  *     /<!---->* Gradient from red at angle == 60 to blue at angle == 300 *<!---->/
101  *     red = (1 + cos (angle - 60)) / 2;
102  *     green = 0;
103  *     blue = 1 - red;
104  *
105  *     cairo_set_source_rgb (cr, red, green, blue);
106  *     cairo_rotate (cr, angle);
107  *
108  *     /<!---->* Inform Pango to re-layout the text with the new transformation matrix *<!---->/
109  *     pango_cairo_update_layout (cr, layout);
110  *
111  *     pango_layout_get_size (layout, &width, &height);
112  *
113  *     cairo_move_to (cr, - width / 2 / PANGO_SCALE, - DEFAULT_TEXT_RADIUS);
114  *     pango_cairo_show_layout (cr, layout);
115  *
116  *     cairo_restore (cr);
117  *   }
118  *
119  * g_object_unref (layout);
120  * g_object_unref (context);
121  * </programlisting>
122  * </example>
123  * <figure>
124  *   <title>Output of <xref linkend="rotated-example"/></title>
125  *   <graphic fileref="rotated-text.png" format="PNG"/>
126  * </figure>
127  */
128
129 /* Get a clip region to draw only part of a layout. index_ranges
130  * contains alternating range starts/stops. The region is the
131  * region which contains the given ranges, i.e. if you draw with the
132  * region as clip, only the given ranges are drawn.
133  */
134 static cairo_region_t*
135 layout_iter_get_line_clip_region (PangoLayoutIter *iter,
136                                   gint             x_origin,
137                                   gint             y_origin,
138                                   const gint      *index_ranges,
139                                   gint             n_ranges)
140 {
141   PangoLayoutLine *line;
142   cairo_region_t *clip_region;
143   PangoRectangle logical_rect;
144   gint baseline;
145   gint i;
146
147   line = pango_layout_iter_get_line_readonly (iter);
148
149   clip_region = cairo_region_create ();
150
151   pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
152   baseline = pango_layout_iter_get_baseline (iter);
153
154   i = 0;
155   while (i < n_ranges)
156     {  
157       gint *pixel_ranges = NULL;
158       gint n_pixel_ranges = 0;
159       gint j;
160
161       /* Note that get_x_ranges returns layout coordinates
162        */
163       if (index_ranges[i*2+1] >= line->start_index &&
164           index_ranges[i*2] < line->start_index + line->length)
165         pango_layout_line_get_x_ranges (line,
166                                         index_ranges[i*2],
167                                         index_ranges[i*2+1],
168                                         &pixel_ranges, &n_pixel_ranges);
169   
170       for (j = 0; j < n_pixel_ranges; j++)
171         {
172           GdkRectangle rect;
173           int x_off, y_off;
174           
175           x_off = PANGO_PIXELS (pixel_ranges[2*j] - logical_rect.x);
176           y_off = PANGO_PIXELS (baseline - logical_rect.y);
177
178           rect.x = x_origin + x_off;
179           rect.y = y_origin - y_off;
180           rect.width = PANGO_PIXELS (pixel_ranges[2*j + 1] - logical_rect.x) - x_off;
181           rect.height = PANGO_PIXELS (baseline - logical_rect.y + logical_rect.height) - y_off;
182
183           cairo_region_union_rectangle (clip_region, &rect);
184         }
185
186       g_free (pixel_ranges);
187       ++i;
188     }
189   return clip_region;
190 }
191
192 /**
193  * gdk_pango_layout_line_get_clip_region: (skip)
194  * @line: a #PangoLayoutLine 
195  * @x_origin: X pixel where you intend to draw the layout line with this clip
196  * @y_origin: baseline pixel where you intend to draw the layout line with this clip
197  * @index_ranges: (array): array of byte indexes into the layout,
198  *     where even members of array are start indexes and odd elements
199  *     are end indexes
200  * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
201  * 
202  * Obtains a clip region which contains the areas where the given
203  * ranges of text would be drawn. @x_origin and @y_origin are the top left
204  * position of the layout. @index_ranges
205  * should contain ranges of bytes in the layout's text. The clip
206  * region will include space to the left or right of the line (to the
207  * layout bounding box) if you have indexes above or below the indexes
208  * contained inside the line. This is to draw the selection all the way
209  * to the side of the layout. However, the clip region is in line coordinates,
210  * not layout coordinates.
211  *
212  * Note that the regions returned correspond to logical extents of the text
213  * ranges, not ink extents. So the drawn line may in fact touch areas out of
214  * the clip region.  The clip region is mainly useful for highlightling parts
215  * of text, such as when text is selected.
216  * 
217  * Return value: a clip region containing the given ranges
218  **/
219 cairo_region_t*
220 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
221                                        gint             x_origin,
222                                        gint             y_origin,
223                                        const gint      *index_ranges,
224                                        gint             n_ranges)
225 {
226   cairo_region_t *clip_region;
227   PangoLayoutIter *iter;
228   
229   g_return_val_if_fail (line != NULL, NULL);
230   g_return_val_if_fail (index_ranges != NULL, NULL);
231   
232   iter = pango_layout_get_iter (line->layout);
233   while (pango_layout_iter_get_line_readonly (iter) != line)
234     pango_layout_iter_next_line (iter);
235   
236   clip_region = layout_iter_get_line_clip_region(iter, x_origin, y_origin, index_ranges, n_ranges);
237
238   pango_layout_iter_free (iter);
239
240   return clip_region;
241 }
242
243 /**
244  * gdk_pango_layout_get_clip_region: (skip)
245  * @layout: a #PangoLayout 
246  * @x_origin: X pixel where you intend to draw the layout with this clip
247  * @y_origin: Y pixel where you intend to draw the layout with this clip
248  * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
249  * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
250  * 
251  * Obtains a clip region which contains the areas where the given ranges
252  * of text would be drawn. @x_origin and @y_origin are the top left point
253  * to center the layout. @index_ranges should contain
254  * ranges of bytes in the layout's text.
255  * 
256  * Note that the regions returned correspond to logical extents of the text
257  * ranges, not ink extents. So the drawn layout may in fact touch areas out of
258  * the clip region.  The clip region is mainly useful for highlightling parts
259  * of text, such as when text is selected.
260  * 
261  * Return value: a clip region containing the given ranges
262  **/
263 cairo_region_t*
264 gdk_pango_layout_get_clip_region (PangoLayout *layout,
265                                   gint         x_origin,
266                                   gint         y_origin,
267                                   const gint  *index_ranges,
268                                   gint         n_ranges)
269 {
270   PangoLayoutIter *iter;  
271   cairo_region_t *clip_region;
272   
273   g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
274   g_return_val_if_fail (index_ranges != NULL, NULL);
275   
276   clip_region = cairo_region_create ();
277   
278   iter = pango_layout_get_iter (layout);
279   
280   do
281     {
282       PangoRectangle logical_rect;
283       cairo_region_t *line_region;
284       gint baseline;
285       
286       pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
287       baseline = pango_layout_iter_get_baseline (iter);      
288
289       line_region = layout_iter_get_line_clip_region(iter, 
290                                                      x_origin + PANGO_PIXELS (logical_rect.x),
291                                                      y_origin + PANGO_PIXELS (baseline),
292                                                      index_ranges,
293                                                      n_ranges);
294
295       cairo_region_union (clip_region, line_region);
296       cairo_region_destroy (line_region);
297     }
298   while (pango_layout_iter_next_line (iter));
299
300   pango_layout_iter_free (iter);
301
302   return clip_region;
303 }
304
305 /**
306  * gdk_pango_context_get:
307  * 
308  * Creates a #PangoContext for the default GDK screen.
309  *
310  * The context must be freed when you're finished with it.
311  * 
312  * When using GTK+, normally you should use gtk_widget_get_pango_context()
313  * instead of this function, to get the appropriate context for
314  * the widget you intend to render text onto.
315  * 
316  * The newly created context will have the default font options (see
317  * #cairo_font_options_t) for the default screen; if these options
318  * change it will not be updated. Using gtk_widget_get_pango_context()
319  * is more convenient if you want to keep a context around and track
320  * changes to the screen's font rendering settings.
321  *
322  * Return value: (transfer full): a new #PangoContext for the default display
323  **/
324 PangoContext *
325 gdk_pango_context_get (void)
326 {
327   return gdk_pango_context_get_for_screen (gdk_screen_get_default ());
328 }
329
330 /**
331  * gdk_pango_context_get_for_screen:
332  * @screen: the #GdkScreen for which the context is to be created.
333  * 
334  * Creates a #PangoContext for @screen.
335  *
336  * The context must be freed when you're finished with it.
337  * 
338  * When using GTK+, normally you should use gtk_widget_get_pango_context()
339  * instead of this function, to get the appropriate context for
340  * the widget you intend to render text onto.
341  * 
342  * The newly created context will have the default font options
343  * (see #cairo_font_options_t) for the screen; if these options
344  * change it will not be updated. Using gtk_widget_get_pango_context()
345  * is more convenient if you want to keep a context around and track
346  * changes to the screen's font rendering settings.
347  * 
348  * Return value: (transfer full): a new #PangoContext for @screen
349  *
350  * Since: 2.2
351  **/
352 PangoContext *
353 gdk_pango_context_get_for_screen (GdkScreen *screen)
354 {
355   PangoFontMap *fontmap;
356   PangoContext *context;
357   const cairo_font_options_t *options;
358   double dpi;
359
360   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
361
362   fontmap = pango_cairo_font_map_get_default ();
363   context = pango_font_map_create_context (fontmap);
364
365   options = gdk_screen_get_font_options (screen);
366   pango_cairo_context_set_font_options (context, options);
367
368   dpi = gdk_screen_get_resolution (screen);
369   pango_cairo_context_set_resolution (context, dpi);
370
371   return context;
372 }