]> Pileus Git - ~andy/gtk/blob - gdk/gdkpango.c
Merge branch 'master' into treeview-refactor
[~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:
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 of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
198  * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
199  * 
200  * Obtains a clip region which contains the areas where the given
201  * ranges of text would be drawn. @x_origin and @y_origin are the top left
202  * position of the layout. @index_ranges
203  * should contain ranges of bytes in the layout's text. The clip
204  * region will include space to the left or right of the line (to the
205  * layout bounding box) if you have indexes above or below the indexes
206  * contained inside the line. This is to draw the selection all the way
207  * to the side of the layout. However, the clip region is in line coordinates,
208  * not layout coordinates.
209  *
210  * Note that the regions returned correspond to logical extents of the text
211  * ranges, not ink extents. So the drawn line may in fact touch areas out of
212  * the clip region.  The clip region is mainly useful for highlightling parts
213  * of text, such as when text is selected.
214  * 
215  * Return value: a clip region containing the given ranges
216  **/
217 cairo_region_t*
218 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
219                                        gint             x_origin,
220                                        gint             y_origin,
221                                        const gint      *index_ranges,
222                                        gint             n_ranges)
223 {
224   cairo_region_t *clip_region;
225   PangoLayoutIter *iter;
226   
227   g_return_val_if_fail (line != NULL, NULL);
228   g_return_val_if_fail (index_ranges != NULL, NULL);
229   
230   iter = pango_layout_get_iter (line->layout);
231   while (pango_layout_iter_get_line_readonly (iter) != line)
232     pango_layout_iter_next_line (iter);
233   
234   clip_region = layout_iter_get_line_clip_region(iter, x_origin, y_origin, index_ranges, n_ranges);
235
236   pango_layout_iter_free (iter);
237
238   return clip_region;
239 }
240
241 /**
242  * gdk_pango_layout_get_clip_region:
243  * @layout: a #PangoLayout 
244  * @x_origin: X pixel where you intend to draw the layout with this clip
245  * @y_origin: Y pixel where you intend to draw the layout with this clip
246  * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
247  * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
248  * 
249  * Obtains a clip region which contains the areas where the given ranges
250  * of text would be drawn. @x_origin and @y_origin are the top left point
251  * to center the layout. @index_ranges should contain
252  * ranges of bytes in the layout's text.
253  * 
254  * Note that the regions returned correspond to logical extents of the text
255  * ranges, not ink extents. So the drawn layout may in fact touch areas out of
256  * the clip region.  The clip region is mainly useful for highlightling parts
257  * of text, such as when text is selected.
258  * 
259  * Return value: a clip region containing the given ranges
260  **/
261 cairo_region_t*
262 gdk_pango_layout_get_clip_region (PangoLayout *layout,
263                                   gint         x_origin,
264                                   gint         y_origin,
265                                   const gint  *index_ranges,
266                                   gint         n_ranges)
267 {
268   PangoLayoutIter *iter;  
269   cairo_region_t *clip_region;
270   
271   g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
272   g_return_val_if_fail (index_ranges != NULL, NULL);
273   
274   clip_region = cairo_region_create ();
275   
276   iter = pango_layout_get_iter (layout);
277   
278   do
279     {
280       PangoRectangle logical_rect;
281       cairo_region_t *line_region;
282       gint baseline;
283       
284       pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
285       baseline = pango_layout_iter_get_baseline (iter);      
286
287       line_region = layout_iter_get_line_clip_region(iter, 
288                                                      x_origin + PANGO_PIXELS (logical_rect.x),
289                                                      y_origin + PANGO_PIXELS (baseline),
290                                                      index_ranges,
291                                                      n_ranges);
292
293       cairo_region_union (clip_region, line_region);
294       cairo_region_destroy (line_region);
295     }
296   while (pango_layout_iter_next_line (iter));
297
298   pango_layout_iter_free (iter);
299
300   return clip_region;
301 }
302
303 /**
304  * gdk_pango_context_get:
305  * 
306  * Creates a #PangoContext for the default GDK screen.
307  *
308  * The context must be freed when you're finished with it.
309  * 
310  * When using GTK+, normally you should use gtk_widget_get_pango_context()
311  * instead of this function, to get the appropriate context for
312  * the widget you intend to render text onto.
313  * 
314  * The newly created context will have the default font options (see
315  * #cairo_font_options_t) for the default screen; if these options
316  * change it will not be updated. Using gtk_widget_get_pango_context()
317  * is more convenient if you want to keep a context around and track
318  * changes to the screen's font rendering settings.
319  *
320  * Return value: (transfer full): a new #PangoContext for the default display
321  **/
322 PangoContext *
323 gdk_pango_context_get (void)
324 {
325   return gdk_pango_context_get_for_screen (gdk_screen_get_default ());
326 }
327
328 /**
329  * gdk_pango_context_get_for_screen:
330  * @screen: the #GdkScreen for which the context is to be created.
331  * 
332  * Creates a #PangoContext for @screen.
333  *
334  * The context must be freed when you're finished with it.
335  * 
336  * When using GTK+, normally you should use gtk_widget_get_pango_context()
337  * instead of this function, to get the appropriate context for
338  * the widget you intend to render text onto.
339  * 
340  * The newly created context will have the default font options
341  * (see #cairo_font_options_t) for the screen; if these options
342  * change it will not be updated. Using gtk_widget_get_pango_context()
343  * is more convenient if you want to keep a context around and track
344  * changes to the screen's font rendering settings.
345  * 
346  * Return value: (transfer full): a new #PangoContext for @screen
347  *
348  * Since: 2.2
349  **/
350 PangoContext *
351 gdk_pango_context_get_for_screen (GdkScreen *screen)
352 {
353   PangoFontMap *fontmap;
354   PangoContext *context;
355   const cairo_font_options_t *options;
356   double dpi;
357   
358   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
359
360   fontmap = pango_cairo_font_map_get_default ();
361   
362   context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
363
364   options = gdk_screen_get_font_options (screen);
365   pango_cairo_context_set_font_options (context, options);
366
367   dpi = gdk_screen_get_resolution (screen);
368   pango_cairo_context_set_resolution (context, dpi);
369
370   return context;
371 }