]> Pileus Git - ~andy/gtk/blob - gdk/gdkpango.c
s/GdkRegion/cairo_region_t/ in all of gtk
[~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 "gdkcairo.h"
24 #include "gdkcolor.h"
25 #include "gdkgc.h"
26 #include "gdkinternals.h"
27 #include "gdkpango.h"
28 #include "gdkrgb.h"
29 #include "gdkprivate.h"
30 #include "gdkscreen.h"
31 #include "gdkintl.h"
32 #include "gdkalias.h"
33
34 #define GDK_INFO_KEY "gdk-info"
35
36 /* We have various arrays indexed by render part; if PangoRenderPart
37  * is extended, we want to make sure not to overwrite the end of
38  * those arrays.
39  */
40 #define MAX_RENDER_PART  PANGO_RENDER_PART_STRIKETHROUGH
41
42 struct _GdkPangoRendererPrivate
43 {
44   GdkScreen *screen;
45
46   /* GdkPangoRenderer specific state */
47   PangoColor override_color[MAX_RENDER_PART + 1];
48   gboolean override_color_set[MAX_RENDER_PART + 1];
49   
50   GdkBitmap *stipple[MAX_RENDER_PART + 1];
51   PangoColor emboss_color;
52   gboolean embossed;
53
54   cairo_t *cr;
55   PangoRenderPart last_part;
56
57   /* Current target */
58   GdkDrawable *drawable;
59   GdkGC *base_gc;
60
61   gboolean gc_changed;
62 };
63
64 static PangoAttrType gdk_pango_attr_stipple_type;
65 static PangoAttrType gdk_pango_attr_embossed_type;
66 static PangoAttrType gdk_pango_attr_emboss_color_type;
67
68 enum {
69   PROP_0,
70   PROP_SCREEN
71 };
72
73 G_DEFINE_TYPE (GdkPangoRenderer, gdk_pango_renderer, PANGO_TYPE_RENDERER)
74
75 static void
76 gdk_pango_renderer_finalize (GObject *object)
77 {
78   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (object);
79   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
80   int i;
81
82   if (priv->base_gc)
83     g_object_unref (priv->base_gc);
84   if (priv->drawable)
85     g_object_unref (priv->drawable);
86
87   for (i = 0; i <= MAX_RENDER_PART; i++)
88     if (priv->stipple[i])
89       g_object_unref (priv->stipple[i]);
90
91   G_OBJECT_CLASS (gdk_pango_renderer_parent_class)->finalize (object);
92 }
93
94 static GObject*
95 gdk_pango_renderer_constructor (GType                  type,
96                                 guint                  n_construct_properties,
97                                 GObjectConstructParam *construct_params)
98 {
99   GObject *object;
100   GdkPangoRenderer *gdk_renderer;
101
102   object = G_OBJECT_CLASS (gdk_pango_renderer_parent_class)->constructor (type,
103                                                                           n_construct_properties,
104                                                                           construct_params);
105
106   gdk_renderer = GDK_PANGO_RENDERER (object);
107   
108   if (!gdk_renderer->priv->screen)
109     {
110       g_warning ("Screen must be specified at construct time for GdkPangoRenderer");
111       gdk_renderer->priv->screen = gdk_screen_get_default ();
112     }
113
114   return object;
115 }
116
117 /* Adjusts matrix and color for the renderer to draw the secondary
118  * "shadow" copy for embossed text */
119 static void
120 emboss_context (GdkPangoRenderer *renderer, cairo_t *cr)
121 {
122   GdkPangoRendererPrivate *priv = renderer->priv;
123   cairo_matrix_t tmp_matrix;
124   double red, green, blue;
125
126   /* The gymnastics here to adjust the matrix are because we want
127    * to offset by +1,+1 in device-space, not in user-space,
128    * so we can't just draw the layout at x + 1, y + 1
129    */
130   cairo_get_matrix (cr, &tmp_matrix);
131   tmp_matrix.x0 += 1.0;
132   tmp_matrix.y0 += 1.0;
133   cairo_set_matrix (cr, &tmp_matrix);
134
135   red = (double) priv->emboss_color.red / 65535.;
136   green = (double) priv->emboss_color.green / 65535.;
137   blue = (double) priv->emboss_color.blue / 65535.;
138
139   cairo_set_source_rgb (cr, red, green, blue);
140 }
141
142 static inline gboolean
143 color_equal (const PangoColor *c1, const PangoColor *c2)
144 {
145   if (!c1 && !c2)
146     return TRUE;
147
148   if (c1 && c2 &&
149       c1->red == c2->red &&
150       c1->green == c2->green &&
151       c1->blue == c2->blue)
152     return TRUE;
153
154   return FALSE;
155 }
156
157 static cairo_t *
158 get_cairo_context (GdkPangoRenderer *gdk_renderer,
159                    PangoRenderPart   part)
160 {
161   PangoRenderer *renderer = PANGO_RENDERER (gdk_renderer);
162   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
163
164   if (!priv->cr)
165     {
166       const PangoMatrix *matrix;
167       
168       priv->cr = gdk_cairo_create (priv->drawable);
169
170       matrix = pango_renderer_get_matrix (renderer);
171       if (matrix)
172         {
173           cairo_matrix_t cairo_matrix;
174           
175           cairo_matrix_init (&cairo_matrix,
176                              matrix->xx, matrix->yx,
177                              matrix->xy, matrix->yy,
178                              matrix->x0, matrix->y0);
179           cairo_set_matrix (priv->cr, &cairo_matrix);
180         }
181     }
182
183   if (part != priv->last_part)
184     {
185       PangoColor *pango_color;
186       GdkColor *color;
187       GdkColor tmp_color;
188       gboolean changed;
189
190       pango_color = pango_renderer_get_color (renderer, part);
191       
192       if (priv->last_part != -1)
193         changed = priv->gc_changed ||
194           priv->stipple[priv->last_part] != priv->stipple[part] ||
195           !color_equal (pango_color,
196                         pango_renderer_get_color (renderer, priv->last_part));
197       else
198         changed = TRUE;
199       
200       if (changed)
201         {
202           if (pango_color)
203             {
204               tmp_color.red = pango_color->red;
205               tmp_color.green = pango_color->green;
206               tmp_color.blue = pango_color->blue;
207               
208               color = &tmp_color;
209             }
210           else
211             color = NULL;
212
213           _gdk_gc_update_context (priv->base_gc,
214                                   priv->cr,
215                                   color,
216                                   priv->stipple[part],
217                                   priv->gc_changed,
218                                   priv->drawable);
219         }
220
221       priv->last_part = part;
222       priv->gc_changed = FALSE;
223     }
224
225   return priv->cr;
226 }
227
228 static void
229 gdk_pango_renderer_draw_glyphs (PangoRenderer    *renderer,
230                                 PangoFont        *font,
231                                 PangoGlyphString *glyphs,
232                                 int               x,
233                                 int               y)
234 {
235   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
236   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
237   cairo_t *cr;
238
239   cr = get_cairo_context (gdk_renderer, 
240                           PANGO_RENDER_PART_FOREGROUND);
241
242   if (priv->embossed)
243     {
244       cairo_save (cr);
245       emboss_context (gdk_renderer, cr);
246       cairo_move_to (cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
247       pango_cairo_show_glyph_string (cr, font, glyphs);
248       cairo_restore (cr);
249     }
250
251   cairo_move_to (cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
252   pango_cairo_show_glyph_string (cr, font, glyphs);
253 }
254
255 static void
256 gdk_pango_renderer_draw_rectangle (PangoRenderer    *renderer,
257                                    PangoRenderPart   part,
258                                    int               x,
259                                    int               y,
260                                    int               width,
261                                    int               height)
262 {
263   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
264   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
265   cairo_t *cr;
266   
267   cr = get_cairo_context (gdk_renderer, part);
268
269   if (priv->embossed && part != PANGO_RENDER_PART_BACKGROUND)
270     {
271       cairo_save (cr);
272       emboss_context (gdk_renderer, cr);
273       cairo_rectangle (cr,
274                        (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
275                        (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
276
277       cairo_fill (cr);
278       cairo_restore (cr);
279     }
280
281   cairo_rectangle (cr,
282                    (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
283                    (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
284   cairo_fill (cr);
285 }
286
287 static void
288 gdk_pango_renderer_draw_error_underline (PangoRenderer    *renderer,
289                                          int               x,
290                                          int               y,
291                                          int               width,
292                                          int               height)
293 {
294   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
295   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
296   cairo_t *cr;
297   
298   cr = get_cairo_context (gdk_renderer, PANGO_RENDER_PART_UNDERLINE);
299   
300   if (priv->embossed)
301     {
302       cairo_save (cr);
303       emboss_context (gdk_renderer, cr);
304       pango_cairo_show_error_underline (cr,
305             (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
306             (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
307       cairo_restore (cr);
308     }
309
310   pango_cairo_show_error_underline (cr,
311         (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
312         (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
313 }
314
315 static void
316 gdk_pango_renderer_draw_shape (PangoRenderer  *renderer,
317                                PangoAttrShape *attr,
318                                int             x,
319                                int             y)
320 {
321   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
322   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
323   PangoLayout *layout;
324   PangoCairoShapeRendererFunc shape_renderer;
325   gpointer                    shape_renderer_data;
326   cairo_t *cr;
327   double dx = (double)x / PANGO_SCALE, dy = (double)y / PANGO_SCALE;
328
329   layout = pango_renderer_get_layout (renderer);
330
331   if (!layout)
332         return;
333
334   shape_renderer = pango_cairo_context_get_shape_renderer (pango_layout_get_context (layout),
335                                                            &shape_renderer_data);
336
337   if (!shape_renderer)
338     return;
339
340   cr = get_cairo_context (gdk_renderer, PANGO_RENDER_PART_FOREGROUND);
341   
342   cairo_save (cr);
343
344   if (priv->embossed)
345     {
346       cairo_save (cr);
347       emboss_context (gdk_renderer, cr);
348
349       cairo_move_to (cr, dx, dy);
350       shape_renderer (cr, attr, FALSE, shape_renderer_data);
351
352       cairo_restore (cr);
353     }
354
355   cairo_move_to (cr, dx, dy);
356   shape_renderer (cr, attr, FALSE, shape_renderer_data);
357
358   cairo_restore (cr);
359 }
360
361 static void
362 gdk_pango_renderer_part_changed (PangoRenderer   *renderer,
363                                  PangoRenderPart  part)
364 {
365   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
366
367   if (gdk_renderer->priv->last_part == part)
368     gdk_renderer->priv->last_part = (PangoRenderPart)-1;
369 }
370
371 static void
372 gdk_pango_renderer_begin (PangoRenderer *renderer)
373 {
374   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
375   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
376   
377   if (!priv->drawable || !priv->base_gc)
378     {
379       g_warning ("gdk_pango_renderer_set_drawable() and gdk_pango_renderer_set_drawable()"
380                  "must be used to set the target drawable and GC before using the renderer\n");
381     }
382 }
383
384 static void
385 gdk_pango_renderer_end (PangoRenderer *renderer)
386 {
387   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
388   GdkPangoRendererPrivate *priv = gdk_renderer->priv;
389
390   if (priv->cr)
391     {
392       cairo_destroy (priv->cr);
393       priv->cr = NULL;
394     }
395   priv->last_part = (PangoRenderPart)-1;
396 }
397
398 static void
399 gdk_pango_renderer_prepare_run (PangoRenderer  *renderer,
400                                 PangoLayoutRun *run)
401 {
402   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
403   gboolean embossed = FALSE;
404   GdkBitmap *stipple = NULL;
405   gboolean changed = FALSE;
406   PangoColor emboss_color;
407   GSList *l;
408   int i;
409
410   emboss_color.red = 0xffff;
411   emboss_color.green = 0xffff;
412   emboss_color.blue = 0xffff;
413
414   for (l = run->item->analysis.extra_attrs; l; l = l->next)
415     {
416       PangoAttribute *attr = l->data;
417
418       /* stipple_type and embossed_type aren't necessarily
419        * initialized, but they are 0, which is an
420        * invalid type so won't occur. 
421        */
422       if (attr->klass->type == gdk_pango_attr_stipple_type)
423         {
424           stipple = ((GdkPangoAttrStipple*)attr)->stipple;
425         }
426       else if (attr->klass->type == gdk_pango_attr_embossed_type)
427         {
428           embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
429         }
430       else if (attr->klass->type == gdk_pango_attr_emboss_color_type)
431         {
432           emboss_color = ((GdkPangoAttrEmbossColor*)attr)->color;
433         }
434     }
435
436   gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_FOREGROUND, stipple);
437   gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_BACKGROUND, stipple);
438   gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_UNDERLINE, stipple);
439   gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_STRIKETHROUGH, stipple);
440
441   if (embossed != gdk_renderer->priv->embossed)
442     {
443       gdk_renderer->priv->embossed = embossed;
444       changed = TRUE;
445     }
446
447   if (!color_equal (&gdk_renderer->priv->emboss_color, &emboss_color))
448     {
449       gdk_renderer->priv->emboss_color = emboss_color;
450       changed = TRUE;
451     }
452
453   if (changed)
454     pango_renderer_part_changed (renderer, PANGO_RENDER_PART_FOREGROUND);
455
456   PANGO_RENDERER_CLASS (gdk_pango_renderer_parent_class)->prepare_run (renderer, run);
457
458   for (i = 0; i <= MAX_RENDER_PART; i++)
459     {
460       if (gdk_renderer->priv->override_color_set[i])
461         pango_renderer_set_color (renderer, i, &gdk_renderer->priv->override_color[i]);
462     }
463 }
464
465 static void
466 gdk_pango_renderer_set_property (GObject         *object,
467                                  guint            prop_id,
468                                  const GValue    *value,
469                                  GParamSpec      *pspec)
470 {
471   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (object);
472
473   switch (prop_id)
474     {
475     case PROP_SCREEN:
476       gdk_renderer->priv->screen = g_value_get_object (value);
477       break;
478     default:
479       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480       break;
481     }
482 }
483
484 static void
485 gdk_pango_renderer_get_property (GObject    *object,
486                                  guint       prop_id,
487                                  GValue     *value,
488                                  GParamSpec *pspec)
489 {
490   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (object);
491
492   switch (prop_id)
493     {
494     case PROP_SCREEN:
495       g_value_set_object (value, gdk_renderer->priv->screen);
496       break;
497     default:
498       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
499       break;
500     }
501 }
502
503 static void
504 gdk_pango_renderer_init (GdkPangoRenderer *renderer)
505 {
506   renderer->priv = G_TYPE_INSTANCE_GET_PRIVATE (renderer,
507                                                 GDK_TYPE_PANGO_RENDERER,
508                                                 GdkPangoRendererPrivate);
509
510   renderer->priv->last_part = (PangoRenderPart)-1;
511   renderer->priv->gc_changed = TRUE;
512 }
513
514 static void
515 gdk_pango_renderer_class_init (GdkPangoRendererClass *klass)
516 {
517   GObjectClass *object_class = G_OBJECT_CLASS (klass);
518   
519   PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
520   
521   renderer_class->draw_glyphs = gdk_pango_renderer_draw_glyphs;
522   renderer_class->draw_rectangle = gdk_pango_renderer_draw_rectangle;
523   renderer_class->draw_error_underline = gdk_pango_renderer_draw_error_underline;
524   renderer_class->draw_shape = gdk_pango_renderer_draw_shape;
525   renderer_class->part_changed = gdk_pango_renderer_part_changed;
526   renderer_class->begin = gdk_pango_renderer_begin;
527   renderer_class->end = gdk_pango_renderer_end;
528   renderer_class->prepare_run = gdk_pango_renderer_prepare_run;
529
530   object_class->finalize = gdk_pango_renderer_finalize;
531   object_class->constructor = gdk_pango_renderer_constructor;
532   object_class->set_property = gdk_pango_renderer_set_property;
533   object_class->get_property = gdk_pango_renderer_get_property;
534   
535   g_object_class_install_property (object_class,
536                                    PROP_SCREEN,
537                                    g_param_spec_object ("screen",
538                                                         P_("Screen"),
539                                                         P_("the GdkScreen for the renderer"),
540                                                         GDK_TYPE_SCREEN,
541                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
542                                                         G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | 
543                                                         G_PARAM_STATIC_BLURB));
544
545   g_type_class_add_private (object_class, sizeof (GdkPangoRendererPrivate));  
546 }
547
548 /**
549  * gdk_pango_renderer_new:
550  * @screen: a #GdkScreen
551  * 
552  * Creates a new #PangoRenderer for @screen. Normally you can use the
553  * results of gdk_pango_renderer_get_default() rather than creating a new
554  * renderer.
555  * 
556  * Return value: a newly created #PangoRenderer. Free with g_object_unref().
557  *
558  * Since: 2.6
559  **/
560 PangoRenderer *
561 gdk_pango_renderer_new (GdkScreen *screen)
562 {
563   g_return_val_if_fail (screen != NULL, NULL);
564   
565   return g_object_new (GDK_TYPE_PANGO_RENDERER,
566                        "screen", screen,
567                        NULL);
568 }
569
570 static void
571 on_renderer_display_closed (GdkDisplay       *display,
572                             gboolean          is_error,
573                             GdkPangoRenderer *renderer)
574 {
575   g_signal_handlers_disconnect_by_func (display,
576                                         on_renderer_display_closed,
577                                         renderer);
578   g_object_set_data (G_OBJECT (renderer->priv->screen),
579                      g_intern_static_string ("gdk-pango-renderer"), NULL);
580 }
581
582 /**
583  * gdk_pango_renderer_get_default:
584  * @screen: a #GdkScreen
585  * 
586  * Gets the default #PangoRenderer for a screen. This default renderer
587  * is shared by all users of the display, so properties such as the color
588  * or transformation matrix set for the renderer may be overwritten
589  * by functions such as gdk_draw_layout().
590  *
591  * Before using the renderer, you need to call gdk_pango_renderer_set_drawable()
592  * and gdk_pango_renderer_set_gc() to set the drawable and graphics context
593  * to use for drawing.
594  * 
595  * Return value: the default #PangoRenderer for @screen. The
596  *  renderer is owned by GTK+ and will be kept around until the
597  *  screen is closed.
598  *
599  * Since: 2.6
600  **/
601 PangoRenderer *
602 gdk_pango_renderer_get_default (GdkScreen *screen)
603 {
604   PangoRenderer *renderer;
605
606   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
607   
608   renderer = g_object_get_data (G_OBJECT (screen), "gdk-pango-renderer");
609   if (!renderer)
610     {
611       renderer = gdk_pango_renderer_new (screen);
612       g_object_set_data_full (G_OBJECT (screen), 
613                               g_intern_static_string ("gdk-pango-renderer"), renderer,
614                               (GDestroyNotify)g_object_unref);
615
616       g_signal_connect (gdk_screen_get_display (screen), "closed",
617                         G_CALLBACK (on_renderer_display_closed), renderer);
618     }
619
620   return renderer;
621 }
622
623 /**
624  * gdk_pango_renderer_set_drawable:
625  * @gdk_renderer: a #GdkPangoRenderer
626  * @drawable: (allow-none): the new target drawable, or %NULL
627  * 
628  * Sets the drawable the renderer draws to.
629  *
630  * Since: 2.6
631  **/
632 void
633 gdk_pango_renderer_set_drawable (GdkPangoRenderer *gdk_renderer,
634                                  GdkDrawable      *drawable)
635 {
636   GdkPangoRendererPrivate *priv;
637   
638   g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
639   g_return_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable));
640
641   priv = gdk_renderer->priv;
642   
643   if (priv->drawable != drawable)
644     {
645       if (priv->drawable)
646         g_object_unref (priv->drawable);
647       priv->drawable = drawable;
648       if (priv->drawable)
649         g_object_ref (priv->drawable);
650     }
651 }
652
653 /**
654  * gdk_pango_renderer_set_gc:
655  * @gdk_renderer: a #GdkPangoRenderer
656  * @gc: (allow-none): the new GC to use for drawing, or %NULL
657  * 
658  * Sets the GC the renderer draws with. Note that the GC must not be
659  * modified until it is unset by calling the function again with
660  * %NULL for the @gc parameter, since GDK may make internal copies
661  * of the GC which won't be updated to follow changes to the
662  * original GC.
663  *
664  * Since: 2.6
665  **/
666 void
667 gdk_pango_renderer_set_gc (GdkPangoRenderer *gdk_renderer,
668                            GdkGC            *gc)
669 {
670   GdkPangoRendererPrivate *priv;
671   
672   g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
673   g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
674
675   priv = gdk_renderer->priv;
676   
677   if (priv->base_gc != gc)
678     {
679       if (priv->base_gc)
680         g_object_unref (priv->base_gc);
681       priv->base_gc = gc;
682       if (priv->base_gc)
683         g_object_ref (priv->base_gc);
684
685       priv->gc_changed = TRUE;
686     }
687 }
688
689
690 /**
691  * gdk_pango_renderer_set_stipple:
692  * @gdk_renderer: a #GdkPangoRenderer
693  * @part: the part to render with the stipple
694  * @stipple: the new stipple value.
695  * 
696  * Sets the stipple for one render part (foreground, background, underline,
697  * etc.) Note that this is overwritten when iterating through the individual
698  * styled runs of a #PangoLayout or #PangoLayoutLine. This function is thus
699  * only useful when you call low level functions like pango_renderer_draw_glyphs()
700  * directly, or in the 'prepare_run' virtual function of a subclass of
701  * #GdkPangoRenderer.
702  *
703  * Since: 2.6
704  **/
705 void
706 gdk_pango_renderer_set_stipple (GdkPangoRenderer *gdk_renderer,
707                                 PangoRenderPart   part,
708                                 GdkBitmap        *stipple)
709 {
710   g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
711
712   if (part > MAX_RENDER_PART)   /* Silently ignore unknown parts */
713     return;
714
715   if (stipple != gdk_renderer->priv->stipple[part])
716     {
717       if (gdk_renderer->priv->stipple[part])
718         g_object_unref (gdk_renderer->priv->stipple[part]);
719
720       gdk_renderer->priv->stipple[part] = stipple;
721       
722       if (gdk_renderer->priv->stipple[part])
723         g_object_ref (gdk_renderer->priv->stipple[part]);
724
725       pango_renderer_part_changed (PANGO_RENDERER (gdk_renderer), part);
726     }
727 }
728
729 /**
730  * gdk_pango_renderer_set_override_color:
731  * @gdk_renderer: a #GdkPangoRenderer
732  * @part: the part to render to set the color of
733  * @color: (allow-none): the color to use, or %NULL to unset a previously
734  *         set override color.
735  * 
736  * Sets the color for a particular render part (foreground,
737  * background, underline, etc.), overriding any attributes on the layouts
738  * renderered with this renderer.
739  * 
740  * Since: 2.6
741  **/
742 void
743 gdk_pango_renderer_set_override_color (GdkPangoRenderer *gdk_renderer,
744                                        PangoRenderPart   part,
745                                        const GdkColor   *color)
746 {
747   GdkPangoRendererPrivate *priv;
748   
749   g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
750
751   priv = gdk_renderer->priv;
752   
753   if (part > MAX_RENDER_PART)   /* Silently ignore unknown parts */
754     return;
755
756   if (color)
757     {
758       priv->override_color[part].red = color->red;
759       priv->override_color[part].green = color->green;
760       priv->override_color[part].blue = color->blue;
761       priv->override_color_set[part] = TRUE;
762     }
763   else
764     priv->override_color_set[part] = FALSE;
765 }
766
767 /* Gets a renderer to draw with, setting the properties of the
768  * renderer and activating it. Note that since we activate the
769  * renderer here, the implicit setting of the matrix that
770  * pango_renderer_draw_layout_[line] normally do when they
771  * activate the renderer is suppressed. */
772 static PangoRenderer *
773 get_renderer (GdkDrawable     *drawable,
774               GdkGC           *gc,
775               const GdkColor  *foreground,
776               const GdkColor  *background)
777 {
778   GdkScreen *screen = gdk_drawable_get_screen (drawable);
779   PangoRenderer *renderer = gdk_pango_renderer_get_default (screen);
780   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
781
782   gdk_pango_renderer_set_drawable (gdk_renderer, drawable);
783   gdk_pango_renderer_set_gc (gdk_renderer, gc);  
784
785   gdk_pango_renderer_set_override_color (gdk_renderer,
786                                          PANGO_RENDER_PART_FOREGROUND,
787                                          foreground);
788   gdk_pango_renderer_set_override_color (gdk_renderer,
789                                          PANGO_RENDER_PART_UNDERLINE,
790                                          foreground);
791   gdk_pango_renderer_set_override_color (gdk_renderer,
792                                          PANGO_RENDER_PART_STRIKETHROUGH,
793                                          foreground);
794
795   gdk_pango_renderer_set_override_color (gdk_renderer,
796                                          PANGO_RENDER_PART_BACKGROUND,
797                                          background);
798
799   pango_renderer_activate (renderer);
800
801   return renderer;
802 }
803
804 /* Cleans up the renderer obtained with get_renderer() */
805 static void
806 release_renderer (PangoRenderer *renderer)
807 {
808   GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
809   
810   pango_renderer_deactivate (renderer);
811   
812   gdk_pango_renderer_set_override_color (gdk_renderer,
813                                          PANGO_RENDER_PART_FOREGROUND,
814                                          NULL);
815   gdk_pango_renderer_set_override_color (gdk_renderer,
816                                          PANGO_RENDER_PART_UNDERLINE,
817                                          NULL);
818   gdk_pango_renderer_set_override_color (gdk_renderer,
819                                          PANGO_RENDER_PART_STRIKETHROUGH,
820                                          NULL);
821   gdk_pango_renderer_set_override_color (gdk_renderer,
822                                          PANGO_RENDER_PART_BACKGROUND,
823                                          NULL);
824   
825   gdk_pango_renderer_set_drawable (gdk_renderer, NULL);
826   gdk_pango_renderer_set_gc (gdk_renderer, NULL);
827 }
828
829 /**
830  * gdk_draw_layout_line_with_colors:
831  * @drawable:  the drawable on which to draw the line
832  * @gc:        base graphics to use
833  * @x:         the x position of start of string (in pixels)
834  * @y:         the y position of baseline (in pixels)
835  * @line:      a #PangoLayoutLine
836  * @foreground: (allow-none): foreground override color, or %NULL for none
837  * @background: (allow-none): background override color, or %NULL for none
838  *
839  * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
840  * layout's normal colors with @foreground and/or @background.
841  * @foreground and @background need not be allocated.
842  *
843  * If the layout's #PangoContext has a transformation matrix set, then
844  * @x and @y specify the position of the left edge of the baseline
845  * (left is in before-tranform user coordinates) in after-transform
846  * device coordinates.
847  */
848 void 
849 gdk_draw_layout_line_with_colors (GdkDrawable      *drawable,
850                                   GdkGC            *gc,
851                                   gint              x, 
852                                   gint              y,
853                                   PangoLayoutLine  *line,
854                                   const GdkColor   *foreground,
855                                   const GdkColor   *background)
856 {
857   PangoRenderer *renderer;
858   const PangoMatrix *matrix;
859   
860   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
861   g_return_if_fail (GDK_IS_GC (gc));
862   g_return_if_fail (line != NULL);
863
864   renderer = get_renderer (drawable, gc, foreground, background);
865
866   /* When we have a matrix, we do positioning by adjusting the matrix, and
867    * clamp just pass x=0, y=0 to the lower levels. We don't want to introduce
868    * a matrix when the caller didn't provide one, however, since that adds
869    * lots of floating point arithmetic for each glyph.
870    */
871   matrix = pango_context_get_matrix (pango_layout_get_context (line->layout));
872   if (matrix)
873     {
874       PangoMatrix tmp_matrix;
875       
876       tmp_matrix = *matrix;
877       tmp_matrix.x0 += x;
878       tmp_matrix.y0 += y;
879       pango_renderer_set_matrix (renderer, &tmp_matrix);
880
881       x = 0;
882       y = 0;
883     }
884   /* Fall back to introduce a matrix if the coords would scale out of range.
885    * The x and y here will be added to in-layout coordinates.  So we cannot
886    * support the entire range here safely.  So, we just accept the middle half
887    * and use fallback for the rest. */
888   else if (GDK_PANGO_UNITS_OVERFLOWS (x, y))
889     {
890       PangoMatrix tmp_matrix = PANGO_MATRIX_INIT;
891       tmp_matrix.x0 += x;
892       tmp_matrix.y0 += y;
893       pango_renderer_set_matrix (renderer, &tmp_matrix);
894
895       x = 0;
896       y = 0;
897     }
898   else
899     pango_renderer_set_matrix (renderer, NULL);
900
901   pango_renderer_draw_layout_line (renderer, line, x * PANGO_SCALE, y * PANGO_SCALE);
902
903   release_renderer (renderer);
904 }
905
906 /**
907  * gdk_draw_layout_with_colors:
908  * @drawable:  the drawable on which to draw string
909  * @gc:        base graphics context to use
910  * @x:         the X position of the left of the layout (in pixels)
911  * @y:         the Y position of the top of the layout (in pixels)
912  * @layout:    a #PangoLayout
913  * @foreground: (allow-none): foreground override color, or %NULL for none
914  * @background: (allow-none): background override color, or %NULL for none
915  *
916  * Render a #PangoLayout onto a #GdkDrawable, overriding the
917  * layout's normal colors with @foreground and/or @background.
918  * @foreground and @background need not be allocated.
919  *
920  * If the layout's #PangoContext has a transformation matrix set, then
921  * @x and @y specify the position of the top left corner of the
922  * bounding box (in device space) of the transformed layout.
923  *
924  * If you're using GTK+, the ususal way to obtain a #PangoLayout
925  * is gtk_widget_create_pango_layout().
926  */
927 void 
928 gdk_draw_layout_with_colors (GdkDrawable     *drawable,
929                              GdkGC           *gc,
930                              int              x, 
931                              int              y,
932                              PangoLayout     *layout,
933                              const GdkColor  *foreground,
934                              const GdkColor  *background)
935 {
936   PangoRenderer *renderer;
937   const PangoMatrix *matrix;
938   
939   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
940   g_return_if_fail (GDK_IS_GC (gc));
941   g_return_if_fail (PANGO_IS_LAYOUT (layout));
942
943   renderer = get_renderer (drawable, gc, foreground, background);
944
945   /* When we have a matrix, we do positioning by adjusting the matrix, and
946    * clamp just pass x=0, y=0 to the lower levels. We don't want to introduce
947    * a matrix when the caller didn't provide one, however, since that adds
948    * lots of floating point arithmetic for each glyph.
949    */
950   matrix = pango_context_get_matrix (pango_layout_get_context (layout));
951   if (matrix)
952     {
953       PangoMatrix tmp_matrix;
954       PangoRectangle rect;
955
956       pango_layout_get_extents (layout, NULL, &rect);
957       pango_matrix_transform_rectangle (matrix, &rect);
958       pango_extents_to_pixels (&rect, NULL);
959       
960       tmp_matrix = *matrix;
961       tmp_matrix.x0 += x - rect.x;
962       tmp_matrix.y0 += y - rect.y;
963       pango_renderer_set_matrix (renderer, &tmp_matrix);
964       
965       x = 0;
966       y = 0;
967     }
968   else if (GDK_PANGO_UNITS_OVERFLOWS (x, y))
969     {
970       PangoMatrix tmp_matrix = PANGO_MATRIX_INIT;
971       tmp_matrix.x0 = x;
972       tmp_matrix.y0 = y;
973       pango_renderer_set_matrix (renderer, &tmp_matrix);
974
975       x = 0;
976       y = 0;
977     }
978   else
979     pango_renderer_set_matrix (renderer, NULL);
980
981   pango_renderer_draw_layout (renderer, layout, x * PANGO_SCALE, y * PANGO_SCALE);
982   
983   release_renderer (renderer);
984 }
985
986 /**
987  * gdk_draw_layout_line:
988  * @drawable:  the drawable on which to draw the line
989  * @gc:        base graphics to use
990  * @x:         the x position of start of string (in pixels)
991  * @y:         the y position of baseline (in pixels)
992  * @line:      a #PangoLayoutLine
993  *
994  * Render a #PangoLayoutLine onto an GDK drawable
995  *
996  * If the layout's #PangoContext has a transformation matrix set, then
997  * @x and @y specify the position of the left edge of the baseline
998  * (left is in before-tranform user coordinates) in after-transform
999  * device coordinates.
1000  */
1001 void 
1002 gdk_draw_layout_line (GdkDrawable      *drawable,
1003                       GdkGC            *gc,
1004                       gint              x, 
1005                       gint              y,
1006                       PangoLayoutLine  *line)
1007 {
1008   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
1009   g_return_if_fail (GDK_IS_GC (gc));
1010   g_return_if_fail (line != NULL);
1011   
1012   gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
1013 }
1014
1015 /**
1016  * gdk_draw_layout:
1017  * @drawable:  the drawable on which to draw string
1018  * @gc:        base graphics context to use
1019  * @x:         the X position of the left of the layout (in pixels)
1020  * @y:         the Y position of the top of the layout (in pixels)
1021  * @layout:    a #PangoLayout
1022  *
1023  * Render a #PangoLayout onto a GDK drawable
1024  *
1025  * If the layout's #PangoContext has a transformation matrix set, then
1026  * @x and @y specify the position of the top left corner of the
1027  * bounding box (in device space) of the transformed layout.
1028  *
1029  * If you're using GTK+, the usual way to obtain a #PangoLayout
1030  * is gtk_widget_create_pango_layout().
1031  */
1032 void 
1033 gdk_draw_layout (GdkDrawable     *drawable,
1034                  GdkGC           *gc,
1035                  int              x, 
1036                  int              y,
1037                  PangoLayout     *layout)
1038 {
1039   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
1040   g_return_if_fail (GDK_IS_GC (gc));
1041   g_return_if_fail (PANGO_IS_LAYOUT (layout));
1042
1043   gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
1044 }
1045
1046 /* GdkPangoAttrStipple */
1047
1048 static PangoAttribute *
1049 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
1050 {
1051   const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
1052
1053   return gdk_pango_attr_stipple_new (src->stipple);
1054 }
1055
1056 static void
1057 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
1058 {
1059   GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
1060
1061   if (st->stipple)
1062     g_object_unref (st->stipple);
1063   
1064   g_free (attr);
1065 }
1066
1067 static gboolean
1068 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
1069                                     const PangoAttribute *attr2)
1070 {
1071   const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
1072   const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
1073
1074   return a->stipple == b->stipple;
1075 }
1076
1077 /**
1078  * gdk_pango_attr_stipple_new:
1079  * @stipple: a bitmap to be set as stipple
1080  *
1081  * Creates a new attribute containing a stipple bitmap to be used when
1082  * rendering the text.
1083  *
1084  * Return value: new #PangoAttribute
1085  **/
1086
1087 PangoAttribute *
1088 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
1089 {
1090   GdkPangoAttrStipple *result;
1091   
1092   static PangoAttrClass klass = {
1093     0,
1094     gdk_pango_attr_stipple_copy,
1095     gdk_pango_attr_stipple_destroy,
1096     gdk_pango_attr_stipple_compare
1097   };
1098
1099   if (!klass.type)
1100     klass.type = gdk_pango_attr_stipple_type =
1101       pango_attr_type_register ("GdkPangoAttrStipple");
1102
1103   result = g_new (GdkPangoAttrStipple, 1);
1104   result->attr.klass = &klass;
1105
1106   if (stipple)
1107     g_object_ref (stipple);
1108   
1109   result->stipple = stipple;
1110
1111   return (PangoAttribute *)result;
1112 }
1113
1114 /* GdkPangoAttrEmbossed */
1115
1116 static PangoAttribute *
1117 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
1118 {
1119   const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
1120
1121   return gdk_pango_attr_embossed_new (e->embossed);
1122 }
1123
1124 static void
1125 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
1126 {
1127   g_free (attr);
1128 }
1129
1130 static gboolean
1131 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
1132                                  const PangoAttribute *attr2)
1133 {
1134   const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
1135   const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
1136
1137   return e1->embossed == e2->embossed;
1138 }
1139
1140 /**
1141  * gdk_pango_attr_embossed_new:
1142  * @embossed: if the region should be embossed
1143  *
1144  * Creates a new attribute flagging a region as embossed or not.
1145  *
1146  * Return value: new #PangoAttribute
1147  **/
1148
1149 PangoAttribute *
1150 gdk_pango_attr_embossed_new (gboolean embossed)
1151 {
1152   GdkPangoAttrEmbossed *result;
1153   
1154   static PangoAttrClass klass = {
1155     0,
1156     gdk_pango_attr_embossed_copy,
1157     gdk_pango_attr_embossed_destroy,
1158     gdk_pango_attr_embossed_compare
1159   };
1160
1161   if (!klass.type)
1162     klass.type = gdk_pango_attr_embossed_type =
1163       pango_attr_type_register ("GdkPangoAttrEmbossed");
1164
1165   result = g_new (GdkPangoAttrEmbossed, 1);
1166   result->attr.klass = &klass;
1167   result->embossed = embossed;
1168   
1169   return (PangoAttribute *)result;
1170 }
1171
1172 /* GdkPangoAttrEmbossColor */
1173
1174 static PangoAttribute *
1175 gdk_pango_attr_emboss_color_copy (const PangoAttribute *attr)
1176 {
1177   const GdkPangoAttrEmbossColor *old = (const GdkPangoAttrEmbossColor*) attr;
1178   GdkPangoAttrEmbossColor *copy;
1179
1180   copy = g_new (GdkPangoAttrEmbossColor, 1);
1181   copy->attr.klass = old->attr.klass;
1182   copy->color = old->color;
1183
1184   return (PangoAttribute *) copy;
1185 }
1186
1187 static void
1188 gdk_pango_attr_emboss_color_destroy (PangoAttribute *attr)
1189 {
1190   g_free (attr);
1191 }
1192
1193 static gboolean
1194 gdk_pango_attr_emboss_color_compare (const PangoAttribute *attr1,
1195                                      const PangoAttribute *attr2)
1196 {
1197   const GdkPangoAttrEmbossColor *c1 = (const GdkPangoAttrEmbossColor*) attr1;
1198   const GdkPangoAttrEmbossColor *c2 = (const GdkPangoAttrEmbossColor*) attr2;
1199
1200   return color_equal (&c1->color, &c2->color);
1201 }
1202
1203 /**
1204  * gdk_pango_attr_emboss_color_new:
1205  * @color: a GdkColor representing the color to emboss with
1206  *
1207  * Creates a new attribute specifying the color to emboss text with.
1208  *
1209  * Return value: new #PangoAttribute
1210  *
1211  * Since: 2.12
1212  **/
1213 PangoAttribute *
1214 gdk_pango_attr_emboss_color_new (const GdkColor *color)
1215 {
1216   GdkPangoAttrEmbossColor *result;
1217   
1218   static PangoAttrClass klass = {
1219     0,
1220     gdk_pango_attr_emboss_color_copy,
1221     gdk_pango_attr_emboss_color_destroy,
1222     gdk_pango_attr_emboss_color_compare
1223   };
1224
1225   if (!klass.type)
1226     klass.type = gdk_pango_attr_emboss_color_type =
1227       pango_attr_type_register ("GdkPangoAttrEmbossColor");
1228
1229   result = g_new (GdkPangoAttrEmbossColor, 1);
1230   result->attr.klass = &klass;
1231   result->color.red = color->red;
1232   result->color.green = color->green;
1233   result->color.blue = color->blue;
1234
1235   return (PangoAttribute *) result;
1236 }
1237
1238 /* Get a clip region to draw only part of a layout. index_ranges
1239  * contains alternating range starts/stops. The region is the
1240  * region which contains the given ranges, i.e. if you draw with the
1241  * region as clip, only the given ranges are drawn.
1242  */
1243 static cairo_region_t*
1244 layout_iter_get_line_clip_region (PangoLayoutIter *iter,
1245                                   gint             x_origin,
1246                                   gint             y_origin,
1247                                   const gint      *index_ranges,
1248                                   gint             n_ranges)
1249 {
1250   PangoLayoutLine *line;
1251   cairo_region_t *clip_region;
1252   PangoRectangle logical_rect;
1253   gint baseline;
1254   gint i;
1255
1256   line = pango_layout_iter_get_line_readonly (iter);
1257
1258   clip_region = cairo_region_create ();
1259
1260   pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1261   baseline = pango_layout_iter_get_baseline (iter);
1262
1263   i = 0;
1264   while (i < n_ranges)
1265     {  
1266       gint *pixel_ranges = NULL;
1267       gint n_pixel_ranges = 0;
1268       gint j;
1269
1270       /* Note that get_x_ranges returns layout coordinates
1271        */
1272       if (index_ranges[i*2+1] >= line->start_index &&
1273           index_ranges[i*2] < line->start_index + line->length)
1274         pango_layout_line_get_x_ranges (line,
1275                                         index_ranges[i*2],
1276                                         index_ranges[i*2+1],
1277                                         &pixel_ranges, &n_pixel_ranges);
1278   
1279       for (j = 0; j < n_pixel_ranges; j++)
1280         {
1281           GdkRectangle rect;
1282           int x_off, y_off;
1283           
1284           x_off = PANGO_PIXELS (pixel_ranges[2*j] - logical_rect.x);
1285           y_off = PANGO_PIXELS (baseline - logical_rect.y);
1286
1287           rect.x = x_origin + x_off;
1288           rect.y = y_origin - y_off;
1289           rect.width = PANGO_PIXELS (pixel_ranges[2*j + 1] - logical_rect.x) - x_off;
1290           rect.height = PANGO_PIXELS (baseline - logical_rect.y + logical_rect.height) - y_off;
1291
1292           cairo_region_union_rectangle (clip_region, &rect);
1293         }
1294
1295       g_free (pixel_ranges);
1296       ++i;
1297     }
1298   return clip_region;
1299 }
1300
1301 /**
1302  * gdk_pango_layout_line_get_clip_region:
1303  * @line: a #PangoLayoutLine 
1304  * @x_origin: X pixel where you intend to draw the layout line with this clip
1305  * @y_origin: baseline pixel where you intend to draw the layout line with this clip
1306  * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
1307  * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
1308  * 
1309  * Obtains a clip region which contains the areas where the given
1310  * ranges of text would be drawn. @x_origin and @y_origin are the same
1311  * position you would pass to gdk_draw_layout_line(). @index_ranges
1312  * should contain ranges of bytes in the layout's text. The clip
1313  * region will include space to the left or right of the line (to the
1314  * layout bounding box) if you have indexes above or below the indexes
1315  * contained inside the line. This is to draw the selection all the way
1316  * to the side of the layout. However, the clip region is in line coordinates,
1317  * not layout coordinates.
1318  *
1319  * Note that the regions returned correspond to logical extents of the text
1320  * ranges, not ink extents. So the drawn line may in fact touch areas out of
1321  * the clip region.  The clip region is mainly useful for highlightling parts
1322  * of text, such as when text is selected.
1323  * 
1324  * Return value: a clip region containing the given ranges
1325  **/
1326 cairo_region_t*
1327 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
1328                                        gint             x_origin,
1329                                        gint             y_origin,
1330                                        const gint      *index_ranges,
1331                                        gint             n_ranges)
1332 {
1333   cairo_region_t *clip_region;
1334   PangoLayoutIter *iter;
1335   
1336   g_return_val_if_fail (line != NULL, NULL);
1337   g_return_val_if_fail (index_ranges != NULL, NULL);
1338   
1339   iter = pango_layout_get_iter (line->layout);
1340   while (pango_layout_iter_get_line_readonly (iter) != line)
1341     pango_layout_iter_next_line (iter);
1342   
1343   clip_region = layout_iter_get_line_clip_region(iter, x_origin, y_origin, index_ranges, n_ranges);
1344
1345   pango_layout_iter_free (iter);
1346
1347   return clip_region;
1348 }
1349
1350 /**
1351  * gdk_pango_layout_get_clip_region:
1352  * @layout: a #PangoLayout 
1353  * @x_origin: X pixel where you intend to draw the layout with this clip
1354  * @y_origin: Y pixel where you intend to draw the layout with this clip
1355  * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
1356  * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
1357  * 
1358  * Obtains a clip region which contains the areas where the given ranges
1359  * of text would be drawn. @x_origin and @y_origin are the same position
1360  * you would pass to gdk_draw_layout_line(). @index_ranges should contain
1361  * ranges of bytes in the layout's text.
1362  * 
1363  * Note that the regions returned correspond to logical extents of the text
1364  * ranges, not ink extents. So the drawn layout may in fact touch areas out of
1365  * the clip region.  The clip region is mainly useful for highlightling parts
1366  * of text, such as when text is selected.
1367  * 
1368  * Return value: a clip region containing the given ranges
1369  **/
1370 cairo_region_t*
1371 gdk_pango_layout_get_clip_region (PangoLayout *layout,
1372                                   gint         x_origin,
1373                                   gint         y_origin,
1374                                   const gint  *index_ranges,
1375                                   gint         n_ranges)
1376 {
1377   PangoLayoutIter *iter;  
1378   cairo_region_t *clip_region;
1379   
1380   g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
1381   g_return_val_if_fail (index_ranges != NULL, NULL);
1382   
1383   clip_region = cairo_region_create ();
1384   
1385   iter = pango_layout_get_iter (layout);
1386   
1387   do
1388     {
1389       PangoRectangle logical_rect;
1390       cairo_region_t *line_region;
1391       gint baseline;
1392       
1393       pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1394       baseline = pango_layout_iter_get_baseline (iter);      
1395
1396       line_region = layout_iter_get_line_clip_region(iter, 
1397                                                      x_origin + PANGO_PIXELS (logical_rect.x),
1398                                                      y_origin + PANGO_PIXELS (baseline),
1399                                                      index_ranges,
1400                                                      n_ranges);
1401
1402       cairo_region_union (clip_region, line_region);
1403       cairo_region_destroy (line_region);
1404     }
1405   while (pango_layout_iter_next_line (iter));
1406
1407   pango_layout_iter_free (iter);
1408
1409   return clip_region;
1410 }
1411
1412 /**
1413  * gdk_pango_context_get:
1414  * 
1415  * Creates a #PangoContext for the default GDK screen.
1416  *
1417  * The context must be freed when you're finished with it.
1418  * 
1419  * When using GTK+, normally you should use gtk_widget_get_pango_context()
1420  * instead of this function, to get the appropriate context for
1421  * the widget you intend to render text onto.
1422  * 
1423  * The newly created context will have the default font options (see
1424  * #cairo_font_options_t) for the default screen; if these options
1425  * change it will not be updated. Using gtk_widget_get_pango_context()
1426  * is more convenient if you want to keep a context around and track
1427  * changes to the screen's font rendering settings.
1428  *
1429  * Return value: a new #PangoContext for the default display
1430  **/
1431 PangoContext *
1432 gdk_pango_context_get (void)
1433 {
1434   return gdk_pango_context_get_for_screen (gdk_screen_get_default ());
1435 }
1436
1437 /**
1438  * gdk_pango_context_get_for_screen:
1439  * @screen: the #GdkScreen for which the context is to be created.
1440  * 
1441  * Creates a #PangoContext for @screen.
1442  *
1443  * The context must be freed when you're finished with it.
1444  * 
1445  * When using GTK+, normally you should use gtk_widget_get_pango_context()
1446  * instead of this function, to get the appropriate context for
1447  * the widget you intend to render text onto.
1448  * 
1449  * The newly created context will have the default font options
1450  * (see #cairo_font_options_t) for the screen; if these options
1451  * change it will not be updated. Using gtk_widget_get_pango_context()
1452  * is more convenient if you want to keep a context around and track
1453  * changes to the screen's font rendering settings.
1454  * 
1455  * Return value: a new #PangoContext for @screen
1456  *
1457  * Since: 2.2
1458  **/
1459 PangoContext *
1460 gdk_pango_context_get_for_screen (GdkScreen *screen)
1461 {
1462   PangoFontMap *fontmap;
1463   PangoContext *context;
1464   const cairo_font_options_t *options;
1465   double dpi;
1466   
1467   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1468
1469   fontmap = pango_cairo_font_map_get_default ();
1470   
1471   context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
1472
1473   options = gdk_screen_get_font_options (screen);
1474   pango_cairo_context_set_font_options (context, options);
1475
1476   dpi = gdk_screen_get_resolution (screen);
1477   pango_cairo_context_set_resolution (context, dpi);
1478
1479   return context;
1480 }
1481
1482 #define __GDK_PANGO_C__
1483 #include "gdkaliasdef.c"