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.
22 #include <pango/pangocairo.h>
24 #include "gdkscreen.h"
27 /* Get a clip region to draw only part of a layout. index_ranges
28 * contains alternating range starts/stops. The region is the
29 * region which contains the given ranges, i.e. if you draw with the
30 * region as clip, only the given ranges are drawn.
32 static cairo_region_t*
33 layout_iter_get_line_clip_region (PangoLayoutIter *iter,
36 const gint *index_ranges,
39 PangoLayoutLine *line;
40 cairo_region_t *clip_region;
41 PangoRectangle logical_rect;
45 line = pango_layout_iter_get_line_readonly (iter);
47 clip_region = cairo_region_create ();
49 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
50 baseline = pango_layout_iter_get_baseline (iter);
55 gint *pixel_ranges = NULL;
56 gint n_pixel_ranges = 0;
59 /* Note that get_x_ranges returns layout coordinates
61 if (index_ranges[i*2+1] >= line->start_index &&
62 index_ranges[i*2] < line->start_index + line->length)
63 pango_layout_line_get_x_ranges (line,
66 &pixel_ranges, &n_pixel_ranges);
68 for (j = 0; j < n_pixel_ranges; j++)
73 x_off = PANGO_PIXELS (pixel_ranges[2*j] - logical_rect.x);
74 y_off = PANGO_PIXELS (baseline - logical_rect.y);
76 rect.x = x_origin + x_off;
77 rect.y = y_origin - y_off;
78 rect.width = PANGO_PIXELS (pixel_ranges[2*j + 1] - logical_rect.x) - x_off;
79 rect.height = PANGO_PIXELS (baseline - logical_rect.y + logical_rect.height) - y_off;
81 cairo_region_union_rectangle (clip_region, &rect);
84 g_free (pixel_ranges);
91 * gdk_pango_layout_line_get_clip_region:
92 * @line: a #PangoLayoutLine
93 * @x_origin: X pixel where you intend to draw the layout line with this clip
94 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
95 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
96 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
98 * Obtains a clip region which contains the areas where the given
99 * ranges of text would be drawn. @x_origin and @y_origin are the top left
100 * position of the layout. @index_ranges
101 * should contain ranges of bytes in the layout's text. The clip
102 * region will include space to the left or right of the line (to the
103 * layout bounding box) if you have indexes above or below the indexes
104 * contained inside the line. This is to draw the selection all the way
105 * to the side of the layout. However, the clip region is in line coordinates,
106 * not layout coordinates.
108 * Note that the regions returned correspond to logical extents of the text
109 * ranges, not ink extents. So the drawn line may in fact touch areas out of
110 * the clip region. The clip region is mainly useful for highlightling parts
111 * of text, such as when text is selected.
113 * Return value: a clip region containing the given ranges
116 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
119 const gint *index_ranges,
122 cairo_region_t *clip_region;
123 PangoLayoutIter *iter;
125 g_return_val_if_fail (line != NULL, NULL);
126 g_return_val_if_fail (index_ranges != NULL, NULL);
128 iter = pango_layout_get_iter (line->layout);
129 while (pango_layout_iter_get_line_readonly (iter) != line)
130 pango_layout_iter_next_line (iter);
132 clip_region = layout_iter_get_line_clip_region(iter, x_origin, y_origin, index_ranges, n_ranges);
134 pango_layout_iter_free (iter);
140 * gdk_pango_layout_get_clip_region:
141 * @layout: a #PangoLayout
142 * @x_origin: X pixel where you intend to draw the layout with this clip
143 * @y_origin: Y pixel where you intend to draw the layout with this clip
144 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
145 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
147 * Obtains a clip region which contains the areas where the given ranges
148 * of text would be drawn. @x_origin and @y_origin are the top left point
149 * to center the layout. @index_ranges should contain
150 * ranges of bytes in the layout's text.
152 * Note that the regions returned correspond to logical extents of the text
153 * ranges, not ink extents. So the drawn layout may in fact touch areas out of
154 * the clip region. The clip region is mainly useful for highlightling parts
155 * of text, such as when text is selected.
157 * Return value: a clip region containing the given ranges
160 gdk_pango_layout_get_clip_region (PangoLayout *layout,
163 const gint *index_ranges,
166 PangoLayoutIter *iter;
167 cairo_region_t *clip_region;
169 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
170 g_return_val_if_fail (index_ranges != NULL, NULL);
172 clip_region = cairo_region_create ();
174 iter = pango_layout_get_iter (layout);
178 PangoRectangle logical_rect;
179 cairo_region_t *line_region;
182 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
183 baseline = pango_layout_iter_get_baseline (iter);
185 line_region = layout_iter_get_line_clip_region(iter,
186 x_origin + PANGO_PIXELS (logical_rect.x),
187 y_origin + PANGO_PIXELS (baseline),
191 cairo_region_union (clip_region, line_region);
192 cairo_region_destroy (line_region);
194 while (pango_layout_iter_next_line (iter));
196 pango_layout_iter_free (iter);
202 * gdk_pango_context_get:
204 * Creates a #PangoContext for the default GDK screen.
206 * The context must be freed when you're finished with it.
208 * When using GTK+, normally you should use gtk_widget_get_pango_context()
209 * instead of this function, to get the appropriate context for
210 * the widget you intend to render text onto.
212 * The newly created context will have the default font options (see
213 * #cairo_font_options_t) for the default screen; if these options
214 * change it will not be updated. Using gtk_widget_get_pango_context()
215 * is more convenient if you want to keep a context around and track
216 * changes to the screen's font rendering settings.
218 * Return value: a new #PangoContext for the default display
221 gdk_pango_context_get (void)
223 return gdk_pango_context_get_for_screen (gdk_screen_get_default ());
227 * gdk_pango_context_get_for_screen:
228 * @screen: the #GdkScreen for which the context is to be created.
230 * Creates a #PangoContext for @screen.
232 * The context must be freed when you're finished with it.
234 * When using GTK+, normally you should use gtk_widget_get_pango_context()
235 * instead of this function, to get the appropriate context for
236 * the widget you intend to render text onto.
238 * The newly created context will have the default font options
239 * (see #cairo_font_options_t) for the screen; if these options
240 * change it will not be updated. Using gtk_widget_get_pango_context()
241 * is more convenient if you want to keep a context around and track
242 * changes to the screen's font rendering settings.
244 * Return value: a new #PangoContext for @screen
249 gdk_pango_context_get_for_screen (GdkScreen *screen)
251 PangoFontMap *fontmap;
252 PangoContext *context;
253 const cairo_font_options_t *options;
256 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
258 fontmap = pango_cairo_font_map_get_default ();
260 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
262 options = gdk_screen_get_font_options (screen);
263 pango_cairo_context_set_font_options (context, options);
265 dpi = gdk_screen_get_resolution (screen);
266 pango_cairo_context_set_resolution (context, dpi);