]> Pileus Git - ~andy/gtk/blob - gdk/gdkpango.c
API: remove GdkPangoRenderer
[~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 #include <math.h>
22 #include <pango/pangocairo.h>
23 #include "gdkpango.h"
24 #include "gdkscreen.h"
25 #include "gdkintl.h"
26
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.
31  */
32 static cairo_region_t*
33 layout_iter_get_line_clip_region (PangoLayoutIter *iter,
34                                   gint             x_origin,
35                                   gint             y_origin,
36                                   const gint      *index_ranges,
37                                   gint             n_ranges)
38 {
39   PangoLayoutLine *line;
40   cairo_region_t *clip_region;
41   PangoRectangle logical_rect;
42   gint baseline;
43   gint i;
44
45   line = pango_layout_iter_get_line_readonly (iter);
46
47   clip_region = cairo_region_create ();
48
49   pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
50   baseline = pango_layout_iter_get_baseline (iter);
51
52   i = 0;
53   while (i < n_ranges)
54     {  
55       gint *pixel_ranges = NULL;
56       gint n_pixel_ranges = 0;
57       gint j;
58
59       /* Note that get_x_ranges returns layout coordinates
60        */
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,
64                                         index_ranges[i*2],
65                                         index_ranges[i*2+1],
66                                         &pixel_ranges, &n_pixel_ranges);
67   
68       for (j = 0; j < n_pixel_ranges; j++)
69         {
70           GdkRectangle rect;
71           int x_off, y_off;
72           
73           x_off = PANGO_PIXELS (pixel_ranges[2*j] - logical_rect.x);
74           y_off = PANGO_PIXELS (baseline - logical_rect.y);
75
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;
80
81           cairo_region_union_rectangle (clip_region, &rect);
82         }
83
84       g_free (pixel_ranges);
85       ++i;
86     }
87   return clip_region;
88 }
89
90 /**
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
97  * 
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.
107  *
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.
112  * 
113  * Return value: a clip region containing the given ranges
114  **/
115 cairo_region_t*
116 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
117                                        gint             x_origin,
118                                        gint             y_origin,
119                                        const gint      *index_ranges,
120                                        gint             n_ranges)
121 {
122   cairo_region_t *clip_region;
123   PangoLayoutIter *iter;
124   
125   g_return_val_if_fail (line != NULL, NULL);
126   g_return_val_if_fail (index_ranges != NULL, NULL);
127   
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);
131   
132   clip_region = layout_iter_get_line_clip_region(iter, x_origin, y_origin, index_ranges, n_ranges);
133
134   pango_layout_iter_free (iter);
135
136   return clip_region;
137 }
138
139 /**
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
146  * 
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.
151  * 
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.
156  * 
157  * Return value: a clip region containing the given ranges
158  **/
159 cairo_region_t*
160 gdk_pango_layout_get_clip_region (PangoLayout *layout,
161                                   gint         x_origin,
162                                   gint         y_origin,
163                                   const gint  *index_ranges,
164                                   gint         n_ranges)
165 {
166   PangoLayoutIter *iter;  
167   cairo_region_t *clip_region;
168   
169   g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
170   g_return_val_if_fail (index_ranges != NULL, NULL);
171   
172   clip_region = cairo_region_create ();
173   
174   iter = pango_layout_get_iter (layout);
175   
176   do
177     {
178       PangoRectangle logical_rect;
179       cairo_region_t *line_region;
180       gint baseline;
181       
182       pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
183       baseline = pango_layout_iter_get_baseline (iter);      
184
185       line_region = layout_iter_get_line_clip_region(iter, 
186                                                      x_origin + PANGO_PIXELS (logical_rect.x),
187                                                      y_origin + PANGO_PIXELS (baseline),
188                                                      index_ranges,
189                                                      n_ranges);
190
191       cairo_region_union (clip_region, line_region);
192       cairo_region_destroy (line_region);
193     }
194   while (pango_layout_iter_next_line (iter));
195
196   pango_layout_iter_free (iter);
197
198   return clip_region;
199 }
200
201 /**
202  * gdk_pango_context_get:
203  * 
204  * Creates a #PangoContext for the default GDK screen.
205  *
206  * The context must be freed when you're finished with it.
207  * 
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.
211  * 
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.
217  *
218  * Return value: a new #PangoContext for the default display
219  **/
220 PangoContext *
221 gdk_pango_context_get (void)
222 {
223   return gdk_pango_context_get_for_screen (gdk_screen_get_default ());
224 }
225
226 /**
227  * gdk_pango_context_get_for_screen:
228  * @screen: the #GdkScreen for which the context is to be created.
229  * 
230  * Creates a #PangoContext for @screen.
231  *
232  * The context must be freed when you're finished with it.
233  * 
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.
237  * 
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.
243  * 
244  * Return value: a new #PangoContext for @screen
245  *
246  * Since: 2.2
247  **/
248 PangoContext *
249 gdk_pango_context_get_for_screen (GdkScreen *screen)
250 {
251   PangoFontMap *fontmap;
252   PangoContext *context;
253   const cairo_font_options_t *options;
254   double dpi;
255   
256   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
257
258   fontmap = pango_cairo_font_map_get_default ();
259   
260   context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
261
262   options = gdk_screen_get_font_options (screen);
263   pango_cairo_context_set_font_options (context, options);
264
265   dpi = gdk_screen_get_resolution (screen);
266   pango_cairo_context_set_resolution (context, dpi);
267
268   return context;
269 }