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 (PangoContext *context,
86 GdkPangoContextInfo *info;
88 g_return_val_if_fail (context != NULL, NULL);
90 info = gdk_pango_context_get_info (context, FALSE);
92 if (info == NULL || info->colormap == NULL)
94 g_warning ("you must set the colormap on a PangoContext before using it to draw a layout");
98 result = gdk_gc_new (gdk_get_default_root_window ());
99 gdk_gc_copy (result, base_gc);
105 color.red = fg_color->red;
106 color.green = fg_color->green;
107 color.blue = fg_color->blue;
109 gdk_rgb_find_color (info->colormap, &color);
110 gdk_gc_set_foreground (result, &color);
115 gdk_gc_set_fill (result, GDK_STIPPLED);
116 gdk_gc_set_stipple (result, stipple);
123 gdk_pango_free_gc (PangoContext *context,
130 gdk_pango_context_set_colormap (PangoContext *context,
131 GdkColormap *colormap)
133 GdkPangoContextInfo *info;
135 g_return_if_fail (context != NULL);
137 info = gdk_pango_context_get_info (context, TRUE);
138 g_return_if_fail (info != NULL);
140 if (info->colormap != colormap)
143 gdk_colormap_unref (info->colormap);
145 info->colormap = colormap;
148 gdk_colormap_ref (info->colormap);
153 * gdk_draw_layout_line_with_colors:
154 * @drawable: the drawable on which to draw the line
155 * @gc: base graphics to use
156 * @x: the x position of start of string (in pixels)
157 * @y: the y position of baseline (in pixels)
158 * @line: a #PangoLayoutLine
159 * @foreground: foreground override color, or %NULL for none
160 * @background: background override color, or %NULL for none
162 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
163 * layout's normal colors with @foreground and/or @background.
164 * @foreground and @background need not be allocated.
167 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
171 PangoLayoutLine *line,
172 GdkColor *foreground,
173 GdkColor *background)
175 GSList *tmp_list = line->runs;
176 PangoRectangle overall_rect;
177 PangoRectangle logical_rect;
178 PangoRectangle ink_rect;
179 PangoContext *context;
185 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
186 g_return_if_fail (GDK_IS_GC (gc));
187 g_return_if_fail (line != NULL);
189 context = pango_layout_get_context (line->layout);
191 pango_layout_line_get_extents (line,NULL, &overall_rect);
195 PangoUnderline uline = PANGO_UNDERLINE_NONE;
196 PangoLayoutRun *run = tmp_list->data;
197 PangoColor fg_color, bg_color;
198 gboolean strike, fg_set, bg_set, shape_set;
202 tmp_list = tmp_list->next;
204 gdk_pango_get_item_properties (run->item, &uline,
215 /* we subtract the rise because X coordinates are upside down */
216 risen_y = y - rise / PANGO_SCALE;
220 if (uline == PANGO_UNDERLINE_NONE)
221 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
222 NULL, &logical_rect);
224 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
225 &ink_rect, &logical_rect);
228 if (bg_set || background)
231 PangoColor tmp = bg_color;
235 tmp.red = background->red;
236 tmp.blue = background->blue;
237 tmp.green = background->green;
240 bg_gc = gdk_pango_get_gc (context, &tmp, stipple, gc);
242 gdk_draw_rectangle (drawable, bg_gc, TRUE,
243 x + (x_off + logical_rect.x) / PANGO_SCALE,
244 risen_y + overall_rect.y / PANGO_SCALE,
245 logical_rect.width / PANGO_SCALE,
246 overall_rect.height / PANGO_SCALE);
249 gdk_gc_set_fill (bg_gc, GDK_SOLID);
251 gdk_pango_free_gc (context, bg_gc);
254 if (fg_set || stipple || foreground)
256 PangoColor tmp = fg_color;
260 tmp.red = foreground->red;
261 tmp.blue = foreground->blue;
262 tmp.green = foreground->green;
265 fg_gc = gdk_pango_get_gc (context, fg_set ? &tmp : NULL,
275 gx = x + x_off / PANGO_SCALE;
280 PangoColor color = { 65535, 65535, 65535 };
281 GdkGC *white_gc = gdk_pango_get_gc (context, &color, stipple, fg_gc);
282 gdk_draw_glyphs (drawable, white_gc, run->item->analysis.font,
286 gdk_pango_free_gc (context, white_gc);
289 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
296 case PANGO_UNDERLINE_NONE:
298 case PANGO_UNDERLINE_DOUBLE:
299 gdk_draw_line (drawable, fg_gc,
300 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
302 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
305 case PANGO_UNDERLINE_SINGLE:
306 gdk_draw_line (drawable, fg_gc,
307 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
309 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
312 case PANGO_UNDERLINE_LOW:
313 gdk_draw_line (drawable, fg_gc,
314 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
315 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
316 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
317 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
323 int centerline = logical_rect.y + logical_rect.height / 2;
325 gdk_draw_line (drawable, fg_gc,
326 x + (x_off + logical_rect.x) / PANGO_SCALE - 1,
327 risen_y + centerline / PANGO_SCALE,
328 x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE + 1,
329 risen_y + centerline / PANGO_SCALE);
333 gdk_pango_free_gc (context, fg_gc);
335 x_off += logical_rect.width;
340 * gdk_draw_layout_with_colors:
341 * @drawable: the drawable on which to draw string
342 * @gc: base graphics context to use
343 * @x: the X position of the left of the layout (in pixels)
344 * @y: the Y position of the top of the layout (in pixels)
345 * @layout: a #PangoLayout
346 * @foreground: foreground override color, or %NULL for none
347 * @background: background override color, or %NULL for none
349 * Render a #PangoLayout onto a #GdkDrawable, overriding the
350 * layout's normal colors with @foreground and/or @background.
351 * @foreground and @background need not be allocated.
354 gdk_draw_layout_with_colors (GdkDrawable *drawable,
359 GdkColor *foreground,
360 GdkColor *background)
362 PangoLayoutIter *iter;
364 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
365 g_return_if_fail (GDK_IS_GC (gc));
366 g_return_if_fail (PANGO_IS_LAYOUT (layout));
368 iter = pango_layout_get_iter (layout);
372 PangoRectangle logical_rect;
373 PangoLayoutLine *line;
376 line = pango_layout_iter_get_line (iter);
378 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
379 baseline = pango_layout_iter_get_baseline (iter);
381 gdk_draw_layout_line_with_colors (drawable, gc,
382 x + logical_rect.x / PANGO_SCALE,
383 y + baseline / PANGO_SCALE,
388 while (pango_layout_iter_next_line (iter));
390 pango_layout_iter_free (iter);
394 * gdk_draw_layout_line:
395 * @drawable: the drawable on which to draw the line
396 * @gc: base graphics to use
397 * @x: the x position of start of string (in pixels)
398 * @y: the y position of baseline (in pixels)
399 * @line: a #PangoLayoutLine
401 * Render a #PangoLayoutLine onto an GDK drawable
404 gdk_draw_layout_line (GdkDrawable *drawable,
408 PangoLayoutLine *line)
410 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
411 g_return_if_fail (GDK_IS_GC (gc));
412 g_return_if_fail (line != NULL);
414 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
419 * @drawable: the drawable on which to draw string
420 * @gc: base graphics context to use
421 * @x: the X position of the left of the layout (in pixels)
422 * @y: the Y position of the top of the layout (in pixels)
423 * @layout: a #PangoLayout
425 * Render a #PangoLayout onto a GDK drawable
428 gdk_draw_layout (GdkDrawable *drawable,
434 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
435 g_return_if_fail (GDK_IS_GC (gc));
436 g_return_if_fail (PANGO_IS_LAYOUT (layout));
438 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
442 gdk_pango_get_item_properties (PangoItem *item,
443 PangoUnderline *uline,
444 gboolean *strikethrough,
446 PangoColor *fg_color,
448 PangoColor *bg_color,
453 PangoRectangle *ink_rect,
454 PangoRectangle *logical_rect)
456 GSList *tmp_list = item->analysis.extra_attrs;
459 *strikethrough = FALSE;
481 PangoAttribute *attr = tmp_list->data;
483 switch (attr->klass->type)
485 case PANGO_ATTR_UNDERLINE:
487 *uline = ((PangoAttrInt *)attr)->value;
490 case PANGO_ATTR_STRIKETHROUGH:
492 *strikethrough = ((PangoAttrInt *)attr)->value;
495 case PANGO_ATTR_FOREGROUND:
497 *fg_color = ((PangoAttrColor *)attr)->color;
503 case PANGO_ATTR_BACKGROUND:
505 *bg_color = ((PangoAttrColor *)attr)->color;
511 case PANGO_ATTR_SHAPE:
515 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
517 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
520 case PANGO_ATTR_RISE:
522 *rise = ((PangoAttrInt *)attr)->value;
526 /* stipple_type and embossed_type aren't necessarily
527 * initialized, but they are 0, which is an
528 * invalid type so won't occur.
530 if (stipple && attr->klass->type == gdk_pango_attr_stipple_type)
532 *stipple = ((GdkPangoAttrStipple*)attr)->stipple;
534 else if (embossed && attr->klass->type == gdk_pango_attr_embossed_type)
536 *embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
540 tmp_list = tmp_list->next;
545 static PangoAttribute *
546 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
548 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
550 return gdk_pango_attr_stipple_new (src->stipple);
554 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
556 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
559 g_object_unref (G_OBJECT (st->stipple));
565 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
566 const PangoAttribute *attr2)
568 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
569 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
571 return a->stipple == b->stipple;
575 * gdk_pango_attr_stipple_new:
576 * @stipple: a bitmap to be set as stipple
578 * Creates a new attribute containing a stipple bitmap to be used when
579 * rendering the text.
581 * Return value: new #PangoAttribute
585 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
587 GdkPangoAttrStipple *result;
589 static PangoAttrClass klass = {
591 gdk_pango_attr_stipple_copy,
592 gdk_pango_attr_stipple_destroy,
593 gdk_pango_attr_stipple_compare
597 klass.type = gdk_pango_attr_stipple_type =
598 pango_attr_type_register ("GdkPangoAttrStipple");
600 result = g_new (GdkPangoAttrStipple, 1);
601 result->attr.klass = &klass;
604 g_object_ref (stipple);
606 result->stipple = stipple;
608 return (PangoAttribute *)result;
611 static PangoAttribute *
612 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
614 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
616 return gdk_pango_attr_embossed_new (e->embossed);
620 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
626 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
627 const PangoAttribute *attr2)
629 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
630 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
632 return e1->embossed == e2->embossed;
636 * gdk_pango_attr_embossed_new:
637 * @embossed: a bitmap to be set as embossed
639 * Creates a new attribute containing a embossed bitmap to be used when
640 * rendering the text.
642 * Return value: new #PangoAttribute
646 gdk_pango_attr_embossed_new (gboolean embossed)
648 GdkPangoAttrEmbossed *result;
650 static PangoAttrClass klass = {
652 gdk_pango_attr_embossed_copy,
653 gdk_pango_attr_embossed_destroy,
654 gdk_pango_attr_embossed_compare
658 klass.type = gdk_pango_attr_embossed_type =
659 pango_attr_type_register ("GdkPangoAttrEmbossed");
661 result = g_new (GdkPangoAttrEmbossed, 1);
662 result->attr.klass = &klass;
663 result->embossed = embossed;
665 return (PangoAttribute *)result;
668 /* Get a clip region to draw only part of a layout. index_ranges
669 * contains alternating range starts/stops. The region is the
670 * region which contains the given ranges, i.e. if you draw with the
671 * region as clip, only the given ranges are drawn.
675 * gdk_pango_layout_line_get_clip_region:
676 * @line: a #PangoLayoutLine
677 * @x_origin: X pixel where you intend to draw the layout line with this clip
678 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
679 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
680 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
682 * Obtains a clip region which contains the areas where the given
683 * ranges of text would be drawn. @x_origin and @y_origin are the same
684 * position you would pass to gdk_draw_layout_line(). @index_ranges
685 * should contain ranges of bytes in the layout's text. The clip
686 * region will include space to the left or right of the line (to the
687 * layout bounding box) if you have indexes above or below the indexes
688 * contained inside the line. This is to draw the selection all the way
689 * to the side of the layout. However, the clip region is in line coordinates,
690 * not layout coordinates.
692 * Return value: a clip region containing the given ranges
695 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
701 GdkRegion *clip_region;
703 PangoRectangle logical_rect;
704 PangoLayoutIter *iter;
707 g_return_val_if_fail (line != NULL, NULL);
708 g_return_val_if_fail (index_ranges != NULL, NULL);
710 clip_region = gdk_region_new ();
712 iter = pango_layout_get_iter (line->layout);
713 while (pango_layout_iter_get_line (iter) != line)
714 pango_layout_iter_next_line (iter);
716 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
717 baseline = pango_layout_iter_get_baseline (iter);
719 pango_layout_iter_free (iter);
724 gint *pixel_ranges = NULL;
725 gint n_pixel_ranges = 0;
728 /* Note that get_x_ranges returns layout coordinates
730 pango_layout_line_get_x_ranges (line,
733 &pixel_ranges, &n_pixel_ranges);
735 for (j=0; j < n_pixel_ranges; j++)
739 rect.x = x_origin + pixel_ranges[2*j] / PANGO_SCALE - logical_rect.x / PANGO_SCALE;
740 rect.y = y_origin - (baseline / PANGO_SCALE - logical_rect.y / PANGO_SCALE);
741 rect.width = (pixel_ranges[2*j + 1] - pixel_ranges[2*j]) / PANGO_SCALE;
742 rect.height = logical_rect.height / PANGO_SCALE;
744 gdk_region_union_with_rect (clip_region, &rect);
747 g_free (pixel_ranges);
755 * gdk_pango_layout_get_clip_region:
756 * @layout: a #PangoLayout
757 * @x_origin: X pixel where you intend to draw the layout with this clip
758 * @y_origin: Y pixel where you intend to draw the layout with this clip
759 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
760 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
762 * Obtains a clip region which contains the areas where the given ranges
763 * of text would be drawn. @x_origin and @y_origin are the same position
764 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
765 * ranges of bytes in the layout's text.
767 * Return value: a clip region containing the given ranges
770 gdk_pango_layout_get_clip_region (PangoLayout *layout,
776 PangoLayoutIter *iter;
777 GdkRegion *clip_region;
779 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
780 g_return_val_if_fail (index_ranges != NULL, NULL);
782 clip_region = gdk_region_new ();
784 iter = pango_layout_get_iter (layout);
788 PangoRectangle logical_rect;
789 PangoLayoutLine *line;
790 GdkRegion *line_region;
793 line = pango_layout_iter_get_line (iter);
795 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
796 baseline = pango_layout_iter_get_baseline (iter);
798 line_region = gdk_pango_layout_line_get_clip_region (line,
799 x_origin + logical_rect.x / PANGO_SCALE,
800 y_origin + baseline / PANGO_SCALE,
804 gdk_region_union (clip_region, line_region);
805 gdk_region_destroy (line_region);
807 while (pango_layout_iter_next_line (iter));
809 pango_layout_iter_free (iter);