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 "gdkprivate.h"
26 #define GDK_INFO_KEY "gdk-info"
28 typedef struct _GdkPangoContextInfo GdkPangoContextInfo;
30 struct _GdkPangoContextInfo
32 GdkColormap *colormap;
35 static PangoAttrType gdk_pango_attr_stipple_type;
36 static PangoAttrType gdk_pango_attr_embossed_type;
38 static void gdk_pango_get_item_properties (PangoItem *item,
39 PangoUnderline *uline,
40 gboolean *strikethrough,
49 PangoRectangle *ink_rect,
50 PangoRectangle *logical_rect);
53 gdk_pango_context_destroy (GdkPangoContextInfo *info)
56 gdk_colormap_unref (info->colormap);
60 static GdkPangoContextInfo *
61 gdk_pango_context_get_info (PangoContext *context, gboolean create)
63 GdkPangoContextInfo *info =
64 g_object_get_qdata (G_OBJECT (context),
65 g_quark_try_string (GDK_INFO_KEY));
68 info = g_new (GdkPangoContextInfo, 1);
69 info->colormap = NULL;
71 g_object_set_qdata_full (G_OBJECT (context),
72 g_quark_from_static_string (GDK_INFO_KEY),
73 info, (GDestroyNotify)gdk_pango_context_destroy);
80 gdk_pango_get_gc (GdkDrawable *drawable,
81 PangoContext *context,
87 GdkPangoContextInfo *info;
89 g_return_val_if_fail (context != NULL, NULL);
91 info = gdk_pango_context_get_info (context, FALSE);
93 if (info == NULL || info->colormap == NULL)
95 g_warning ("you must set the colormap on a PangoContext before using it to draw a layout");
99 result = gdk_gc_new (drawable);
100 gdk_gc_copy (result, base_gc);
106 color.red = fg_color->red;
107 color.green = fg_color->green;
108 color.blue = fg_color->blue;
110 gdk_rgb_find_color (info->colormap, &color);
111 gdk_gc_set_foreground (result, &color);
116 gdk_gc_set_fill (result, GDK_STIPPLED);
117 gdk_gc_set_stipple (result, stipple);
124 gdk_pango_free_gc (PangoContext *context,
131 * gdk_pango_context_set_colormap:
132 * @context: a #PangoContext
133 * @colormap: a #GdkColormap
135 * Sets the colormap to be used for drawing with @context.
136 * If you obtained your context from gtk_widget_get_pango_context() or
137 * gtk_widget_create_pango_context(), the colormap will already be set
138 * to the colormap for the widget, so you shouldn't need this
143 gdk_pango_context_set_colormap (PangoContext *context,
144 GdkColormap *colormap)
146 GdkPangoContextInfo *info;
148 g_return_if_fail (context != NULL);
150 info = gdk_pango_context_get_info (context, TRUE);
151 g_return_if_fail (info != NULL);
153 if (info->colormap != colormap)
156 gdk_colormap_unref (info->colormap);
158 info->colormap = colormap;
161 gdk_colormap_ref (info->colormap);
166 * gdk_draw_layout_line_with_colors:
167 * @drawable: the drawable on which to draw the line
168 * @gc: base graphics to use
169 * @x: the x position of start of string (in pixels)
170 * @y: the y position of baseline (in pixels)
171 * @line: a #PangoLayoutLine
172 * @foreground: foreground override color, or %NULL for none
173 * @background: background override color, or %NULL for none
175 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
176 * layout's normal colors with @foreground and/or @background.
177 * @foreground and @background need not be allocated.
180 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
184 PangoLayoutLine *line,
185 GdkColor *foreground,
186 GdkColor *background)
188 GSList *tmp_list = line->runs;
189 PangoRectangle overall_rect;
190 PangoRectangle logical_rect;
191 PangoRectangle ink_rect;
192 PangoContext *context;
198 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
199 g_return_if_fail (GDK_IS_GC (gc));
200 g_return_if_fail (line != NULL);
202 context = pango_layout_get_context (line->layout);
204 pango_layout_line_get_extents (line,NULL, &overall_rect);
208 PangoUnderline uline = PANGO_UNDERLINE_NONE;
209 PangoLayoutRun *run = tmp_list->data;
210 PangoColor fg_color, bg_color;
211 gboolean strike, fg_set, bg_set, shape_set;
215 tmp_list = tmp_list->next;
217 gdk_pango_get_item_properties (run->item, &uline,
228 /* we subtract the rise because X coordinates are upside down */
229 risen_y = y - rise / PANGO_SCALE;
233 if (uline == PANGO_UNDERLINE_NONE)
234 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
235 NULL, &logical_rect);
237 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
238 &ink_rect, &logical_rect);
241 if (bg_set || background)
244 PangoColor tmp = bg_color;
248 tmp.red = background->red;
249 tmp.blue = background->blue;
250 tmp.green = background->green;
253 bg_gc = gdk_pango_get_gc (drawable, context, &tmp, stipple, gc);
255 gdk_draw_rectangle (drawable, bg_gc, TRUE,
256 x + (x_off + logical_rect.x) / PANGO_SCALE,
257 risen_y + overall_rect.y / PANGO_SCALE,
258 logical_rect.width / PANGO_SCALE,
259 overall_rect.height / PANGO_SCALE);
262 gdk_gc_set_fill (bg_gc, GDK_SOLID);
264 gdk_pango_free_gc (context, bg_gc);
267 if (fg_set || stipple || foreground)
269 PangoColor tmp = fg_color;
273 tmp.red = foreground->red;
274 tmp.blue = foreground->blue;
275 tmp.green = foreground->green;
278 fg_gc = gdk_pango_get_gc (drawable, context, fg_set ? &tmp : NULL,
288 gx = x + x_off / PANGO_SCALE;
293 PangoColor color = { 65535, 65535, 65535 };
294 GdkGC *white_gc = gdk_pango_get_gc (drawable, context, &color, stipple, fg_gc);
295 gdk_draw_glyphs (drawable, white_gc, run->item->analysis.font,
299 gdk_pango_free_gc (context, white_gc);
302 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
309 case PANGO_UNDERLINE_NONE:
311 case PANGO_UNDERLINE_DOUBLE:
312 gdk_draw_line (drawable, fg_gc,
313 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
315 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
318 case PANGO_UNDERLINE_SINGLE:
319 gdk_draw_line (drawable, fg_gc,
320 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
322 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
325 case PANGO_UNDERLINE_LOW:
326 gdk_draw_line (drawable, fg_gc,
327 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
328 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
329 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
330 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
336 int centerline = logical_rect.y + logical_rect.height / 2;
338 gdk_draw_line (drawable, fg_gc,
339 x + (x_off + logical_rect.x) / PANGO_SCALE - 1,
340 risen_y + centerline / PANGO_SCALE,
341 x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE + 1,
342 risen_y + centerline / PANGO_SCALE);
346 gdk_pango_free_gc (context, fg_gc);
348 x_off += logical_rect.width;
353 * gdk_draw_layout_with_colors:
354 * @drawable: the drawable on which to draw string
355 * @gc: base graphics context to use
356 * @x: the X position of the left of the layout (in pixels)
357 * @y: the Y position of the top of the layout (in pixels)
358 * @layout: a #PangoLayout
359 * @foreground: foreground override color, or %NULL for none
360 * @background: background override color, or %NULL for none
362 * Render a #PangoLayout onto a #GdkDrawable, overriding the
363 * layout's normal colors with @foreground and/or @background.
364 * @foreground and @background need not be allocated.
367 gdk_draw_layout_with_colors (GdkDrawable *drawable,
372 GdkColor *foreground,
373 GdkColor *background)
375 PangoLayoutIter *iter;
377 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
378 g_return_if_fail (GDK_IS_GC (gc));
379 g_return_if_fail (PANGO_IS_LAYOUT (layout));
381 iter = pango_layout_get_iter (layout);
385 PangoRectangle logical_rect;
386 PangoLayoutLine *line;
389 line = pango_layout_iter_get_line (iter);
391 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
392 baseline = pango_layout_iter_get_baseline (iter);
394 gdk_draw_layout_line_with_colors (drawable, gc,
395 x + logical_rect.x / PANGO_SCALE,
396 y + baseline / PANGO_SCALE,
401 while (pango_layout_iter_next_line (iter));
403 pango_layout_iter_free (iter);
407 * gdk_draw_layout_line:
408 * @drawable: the drawable on which to draw the line
409 * @gc: base graphics to use
410 * @x: the x position of start of string (in pixels)
411 * @y: the y position of baseline (in pixels)
412 * @line: a #PangoLayoutLine
414 * Render a #PangoLayoutLine onto an GDK drawable
417 gdk_draw_layout_line (GdkDrawable *drawable,
421 PangoLayoutLine *line)
423 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
424 g_return_if_fail (GDK_IS_GC (gc));
425 g_return_if_fail (line != NULL);
427 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
432 * @drawable: the drawable on which to draw string
433 * @gc: base graphics context to use
434 * @x: the X position of the left of the layout (in pixels)
435 * @y: the Y position of the top of the layout (in pixels)
436 * @layout: a #PangoLayout
438 * Render a #PangoLayout onto a GDK drawable
441 gdk_draw_layout (GdkDrawable *drawable,
447 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
448 g_return_if_fail (GDK_IS_GC (gc));
449 g_return_if_fail (PANGO_IS_LAYOUT (layout));
451 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
455 gdk_pango_get_item_properties (PangoItem *item,
456 PangoUnderline *uline,
457 gboolean *strikethrough,
459 PangoColor *fg_color,
461 PangoColor *bg_color,
466 PangoRectangle *ink_rect,
467 PangoRectangle *logical_rect)
469 GSList *tmp_list = item->analysis.extra_attrs;
472 *strikethrough = FALSE;
494 PangoAttribute *attr = tmp_list->data;
496 switch (attr->klass->type)
498 case PANGO_ATTR_UNDERLINE:
500 *uline = ((PangoAttrInt *)attr)->value;
503 case PANGO_ATTR_STRIKETHROUGH:
505 *strikethrough = ((PangoAttrInt *)attr)->value;
508 case PANGO_ATTR_FOREGROUND:
510 *fg_color = ((PangoAttrColor *)attr)->color;
516 case PANGO_ATTR_BACKGROUND:
518 *bg_color = ((PangoAttrColor *)attr)->color;
524 case PANGO_ATTR_SHAPE:
528 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
530 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
533 case PANGO_ATTR_RISE:
535 *rise = ((PangoAttrInt *)attr)->value;
539 /* stipple_type and embossed_type aren't necessarily
540 * initialized, but they are 0, which is an
541 * invalid type so won't occur.
543 if (stipple && attr->klass->type == gdk_pango_attr_stipple_type)
545 *stipple = ((GdkPangoAttrStipple*)attr)->stipple;
547 else if (embossed && attr->klass->type == gdk_pango_attr_embossed_type)
549 *embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
553 tmp_list = tmp_list->next;
558 static PangoAttribute *
559 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
561 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
563 return gdk_pango_attr_stipple_new (src->stipple);
567 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
569 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
572 g_object_unref (G_OBJECT (st->stipple));
578 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
579 const PangoAttribute *attr2)
581 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
582 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
584 return a->stipple == b->stipple;
588 * gdk_pango_attr_stipple_new:
589 * @stipple: a bitmap to be set as stipple
591 * Creates a new attribute containing a stipple bitmap to be used when
592 * rendering the text.
594 * Return value: new #PangoAttribute
598 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
600 GdkPangoAttrStipple *result;
602 static PangoAttrClass klass = {
604 gdk_pango_attr_stipple_copy,
605 gdk_pango_attr_stipple_destroy,
606 gdk_pango_attr_stipple_compare
610 klass.type = gdk_pango_attr_stipple_type =
611 pango_attr_type_register ("GdkPangoAttrStipple");
613 result = g_new (GdkPangoAttrStipple, 1);
614 result->attr.klass = &klass;
617 g_object_ref (stipple);
619 result->stipple = stipple;
621 return (PangoAttribute *)result;
624 static PangoAttribute *
625 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
627 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
629 return gdk_pango_attr_embossed_new (e->embossed);
633 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
639 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
640 const PangoAttribute *attr2)
642 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
643 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
645 return e1->embossed == e2->embossed;
649 * gdk_pango_attr_embossed_new:
650 * @embossed: a bitmap to be set as embossed
652 * Creates a new attribute containing a embossed bitmap to be used when
653 * rendering the text.
655 * Return value: new #PangoAttribute
659 gdk_pango_attr_embossed_new (gboolean embossed)
661 GdkPangoAttrEmbossed *result;
663 static PangoAttrClass klass = {
665 gdk_pango_attr_embossed_copy,
666 gdk_pango_attr_embossed_destroy,
667 gdk_pango_attr_embossed_compare
671 klass.type = gdk_pango_attr_embossed_type =
672 pango_attr_type_register ("GdkPangoAttrEmbossed");
674 result = g_new (GdkPangoAttrEmbossed, 1);
675 result->attr.klass = &klass;
676 result->embossed = embossed;
678 return (PangoAttribute *)result;
681 /* Get a clip region to draw only part of a layout. index_ranges
682 * contains alternating range starts/stops. The region is the
683 * region which contains the given ranges, i.e. if you draw with the
684 * region as clip, only the given ranges are drawn.
688 * gdk_pango_layout_line_get_clip_region:
689 * @line: a #PangoLayoutLine
690 * @x_origin: X pixel where you intend to draw the layout line with this clip
691 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
692 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
693 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
695 * Obtains a clip region which contains the areas where the given
696 * ranges of text would be drawn. @x_origin and @y_origin are the same
697 * position you would pass to gdk_draw_layout_line(). @index_ranges
698 * should contain ranges of bytes in the layout's text. The clip
699 * region will include space to the left or right of the line (to the
700 * layout bounding box) if you have indexes above or below the indexes
701 * contained inside the line. This is to draw the selection all the way
702 * to the side of the layout. However, the clip region is in line coordinates,
703 * not layout coordinates.
705 * Return value: a clip region containing the given ranges
708 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
714 GdkRegion *clip_region;
716 PangoRectangle logical_rect;
717 PangoLayoutIter *iter;
720 g_return_val_if_fail (line != NULL, NULL);
721 g_return_val_if_fail (index_ranges != NULL, NULL);
723 clip_region = gdk_region_new ();
725 iter = pango_layout_get_iter (line->layout);
726 while (pango_layout_iter_get_line (iter) != line)
727 pango_layout_iter_next_line (iter);
729 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
730 baseline = pango_layout_iter_get_baseline (iter);
732 pango_layout_iter_free (iter);
737 gint *pixel_ranges = NULL;
738 gint n_pixel_ranges = 0;
741 /* Note that get_x_ranges returns layout coordinates
743 pango_layout_line_get_x_ranges (line,
746 &pixel_ranges, &n_pixel_ranges);
748 for (j=0; j < n_pixel_ranges; j++)
752 rect.x = x_origin + pixel_ranges[2*j] / PANGO_SCALE - logical_rect.x / PANGO_SCALE;
753 rect.y = y_origin - (baseline / PANGO_SCALE - logical_rect.y / PANGO_SCALE);
754 rect.width = (pixel_ranges[2*j + 1] - pixel_ranges[2*j]) / PANGO_SCALE;
755 rect.height = logical_rect.height / PANGO_SCALE;
757 gdk_region_union_with_rect (clip_region, &rect);
760 g_free (pixel_ranges);
768 * gdk_pango_layout_get_clip_region:
769 * @layout: a #PangoLayout
770 * @x_origin: X pixel where you intend to draw the layout with this clip
771 * @y_origin: Y pixel where you intend to draw the layout with this clip
772 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
773 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
775 * Obtains a clip region which contains the areas where the given ranges
776 * of text would be drawn. @x_origin and @y_origin are the same position
777 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
778 * ranges of bytes in the layout's text.
780 * Return value: a clip region containing the given ranges
783 gdk_pango_layout_get_clip_region (PangoLayout *layout,
789 PangoLayoutIter *iter;
790 GdkRegion *clip_region;
792 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
793 g_return_val_if_fail (index_ranges != NULL, NULL);
795 clip_region = gdk_region_new ();
797 iter = pango_layout_get_iter (layout);
801 PangoRectangle logical_rect;
802 PangoLayoutLine *line;
803 GdkRegion *line_region;
806 line = pango_layout_iter_get_line (iter);
808 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
809 baseline = pango_layout_iter_get_baseline (iter);
811 line_region = gdk_pango_layout_line_get_clip_region (line,
812 x_origin + logical_rect.x / PANGO_SCALE,
813 y_origin + baseline / PANGO_SCALE,
817 gdk_region_union (clip_region, line_region);
818 gdk_region_destroy (line_region);
820 while (pango_layout_iter_next_line (iter));
822 pango_layout_iter_free (iter);