1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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.
24 #include "gdkscreen.h"
28 #include <pango/pangocairo.h>
32 * SECTION:pango_interaction
33 * @Short_description: Using Pango in GDK
34 * @Title: Pango Interaction
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
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.)
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 -->
61 * #define FONT "Sans Bold 18"
63 * PangoContext *context;
64 * PangoLayout *layout;
65 * PangoFontDescription *desc;
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 *<!---->/
75 * width = gdk_window_get_width (window);
76 * height = gdk_window_get_height (window);
77 * radius = MIN (width, height) / 2.;
79 * cairo_translate (cr,
80 * radius + (width - 2 * radius) / 2,
81 * radius + (height - 2 * radius) / 2);
82 * cairo_scale (cr, radius / RADIUS, radius / RADIUS);
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);
92 * /<!---->* Draw the layout N_WORDS times in a circle *<!---->/
93 * for (i = 0; i < N_WORDS; i++)
95 * double red, green, blue;
96 * double angle = 2 * G_PI * i / n_words;
100 * /<!---->* Gradient from red at angle == 60 to blue at angle == 300 *<!---->/
101 * red = (1 + cos (angle - 60)) / 2;
105 * cairo_set_source_rgb (cr, red, green, blue);
106 * cairo_rotate (cr, angle);
108 * /<!---->* Inform Pango to re-layout the text with the new transformation matrix *<!---->/
109 * pango_cairo_update_layout (cr, layout);
111 * pango_layout_get_size (layout, &width, &height);
113 * cairo_move_to (cr, - width / 2 / PANGO_SCALE, - DEFAULT_TEXT_RADIUS);
114 * pango_cairo_show_layout (cr, layout);
116 * cairo_restore (cr);
119 * g_object_unref (layout);
120 * g_object_unref (context);
124 * <title>Output of <xref linkend="rotated-example"/></title>
125 * <graphic fileref="rotated-text.png" format="PNG"/>
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.
134 static cairo_region_t*
135 layout_iter_get_line_clip_region (PangoLayoutIter *iter,
138 const gint *index_ranges,
141 PangoLayoutLine *line;
142 cairo_region_t *clip_region;
143 PangoRectangle logical_rect;
147 line = pango_layout_iter_get_line_readonly (iter);
149 clip_region = cairo_region_create ();
151 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
152 baseline = pango_layout_iter_get_baseline (iter);
157 gint *pixel_ranges = NULL;
158 gint n_pixel_ranges = 0;
161 /* Note that get_x_ranges returns layout coordinates
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,
168 &pixel_ranges, &n_pixel_ranges);
170 for (j = 0; j < n_pixel_ranges; j++)
175 x_off = PANGO_PIXELS (pixel_ranges[2*j] - logical_rect.x);
176 y_off = PANGO_PIXELS (baseline - logical_rect.y);
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;
183 cairo_region_union_rectangle (clip_region, &rect);
186 g_free (pixel_ranges);
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
200 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
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.
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.
217 * Return value: a clip region containing the given ranges
220 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
223 const gint *index_ranges,
226 cairo_region_t *clip_region;
227 PangoLayoutIter *iter;
229 g_return_val_if_fail (line != NULL, NULL);
230 g_return_val_if_fail (index_ranges != NULL, NULL);
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);
236 clip_region = layout_iter_get_line_clip_region(iter, x_origin, y_origin, index_ranges, n_ranges);
238 pango_layout_iter_free (iter);
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
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.
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.
261 * Return value: a clip region containing the given ranges
264 gdk_pango_layout_get_clip_region (PangoLayout *layout,
267 const gint *index_ranges,
270 PangoLayoutIter *iter;
271 cairo_region_t *clip_region;
273 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
274 g_return_val_if_fail (index_ranges != NULL, NULL);
276 clip_region = cairo_region_create ();
278 iter = pango_layout_get_iter (layout);
282 PangoRectangle logical_rect;
283 cairo_region_t *line_region;
286 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
287 baseline = pango_layout_iter_get_baseline (iter);
289 line_region = layout_iter_get_line_clip_region(iter,
290 x_origin + PANGO_PIXELS (logical_rect.x),
291 y_origin + PANGO_PIXELS (baseline),
295 cairo_region_union (clip_region, line_region);
296 cairo_region_destroy (line_region);
298 while (pango_layout_iter_next_line (iter));
300 pango_layout_iter_free (iter);
306 * gdk_pango_context_get:
308 * Creates a #PangoContext for the default GDK screen.
310 * The context must be freed when you're finished with it.
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.
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.
322 * Return value: (transfer full): a new #PangoContext for the default display
325 gdk_pango_context_get (void)
327 return gdk_pango_context_get_for_screen (gdk_screen_get_default ());
331 * gdk_pango_context_get_for_screen:
332 * @screen: the #GdkScreen for which the context is to be created.
334 * Creates a #PangoContext for @screen.
336 * The context must be freed when you're finished with it.
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.
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.
348 * Return value: (transfer full): a new #PangoContext for @screen
353 gdk_pango_context_get_for_screen (GdkScreen *screen)
355 PangoFontMap *fontmap;
356 PangoContext *context;
357 const cairo_font_options_t *options;
360 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
362 fontmap = pango_cairo_font_map_get_default ();
363 context = pango_font_map_create_context (fontmap);
365 options = gdk_screen_get_font_options (screen);
366 pango_cairo_context_set_font_options (context, options);
368 dpi = gdk_screen_get_resolution (screen);
369 pango_cairo_context_set_resolution (context, dpi);