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.
25 #include "gdkprivate.h"
26 #include "gdkscreen.h"
28 #define GDK_INFO_KEY "gdk-info"
30 typedef struct _GdkPangoContextInfo GdkPangoContextInfo;
32 struct _GdkPangoContextInfo
34 GdkColormap *colormap;
37 static PangoAttrType gdk_pango_attr_stipple_type;
38 static PangoAttrType gdk_pango_attr_embossed_type;
40 static void gdk_pango_get_item_properties (PangoItem *item,
41 PangoUnderline *uline,
42 gboolean *strikethrough,
51 PangoRectangle *ink_rect,
52 PangoRectangle *logical_rect);
55 gdk_pango_context_destroy (GdkPangoContextInfo *info)
58 g_object_unref (info->colormap);
62 static GdkPangoContextInfo *
63 gdk_pango_context_get_info (PangoContext *context, gboolean create)
65 GdkPangoContextInfo *info =
66 g_object_get_qdata (G_OBJECT (context),
67 g_quark_try_string (GDK_INFO_KEY));
70 info = g_new (GdkPangoContextInfo, 1);
71 info->colormap = NULL;
73 g_object_set_qdata_full (G_OBJECT (context),
74 g_quark_from_static_string (GDK_INFO_KEY),
75 info, (GDestroyNotify)gdk_pango_context_destroy);
82 gdk_pango_get_gc (GdkDrawable *drawable,
83 PangoContext *context,
89 GdkPangoContextInfo *info;
91 g_return_val_if_fail (context != NULL, NULL);
93 info = gdk_pango_context_get_info (context, FALSE);
95 if (info == NULL || info->colormap == NULL)
97 g_warning ("you must set the colormap on a PangoContext before using it to draw a layout");
101 result = gdk_gc_new (drawable);
102 gdk_gc_copy (result, base_gc);
108 color.red = fg_color->red;
109 color.green = fg_color->green;
110 color.blue = fg_color->blue;
112 gdk_rgb_find_color (info->colormap, &color);
113 gdk_gc_set_foreground (result, &color);
118 gdk_gc_set_fill (result, GDK_STIPPLED);
119 gdk_gc_set_stipple (result, stipple);
126 gdk_pango_free_gc (PangoContext *context,
133 * gdk_pango_context_set_colormap:
134 * @context: a #PangoContext
135 * @colormap: a #GdkColormap
137 * Sets the colormap to be used for drawing with @context.
139 * If you obtained your context from gtk_widget_get_pango_context() or
140 * gtk_widget_create_pango_context(), the colormap will already be set
141 * to the colormap for the widget, so you shouldn't need this
145 gdk_pango_context_set_colormap (PangoContext *context,
146 GdkColormap *colormap)
148 GdkPangoContextInfo *info;
150 g_return_if_fail (context != NULL);
152 info = gdk_pango_context_get_info (context, TRUE);
153 g_return_if_fail (info != NULL);
155 if (info->colormap != colormap)
158 g_object_unref (info->colormap);
160 info->colormap = colormap;
163 g_object_ref (info->colormap);
168 draw_underline (GdkDrawable *drawable,
170 PangoUnderline uline,
178 case PANGO_UNDERLINE_NONE:
180 case PANGO_UNDERLINE_DOUBLE:
181 gdk_draw_line (drawable, gc,
182 start_x, baseline_y + 3,
183 end_x, baseline_y + 3);
185 case PANGO_UNDERLINE_SINGLE:
186 gdk_draw_line (drawable, gc,
187 start_x, baseline_y + 1,
188 end_x, baseline_y + 1);
190 case PANGO_UNDERLINE_ERROR:
192 int point_x, point_y;
195 for (point_x = start_x;
199 point_y = counter ? baseline_y + 1 : baseline_y + 2;
201 gdk_draw_line (drawable, gc,
203 MIN (point_x + 1, end_x), point_y);
205 counter = (counter + 1) % 2;
209 case PANGO_UNDERLINE_LOW:
210 gdk_draw_line (drawable, gc,
218 * gdk_draw_layout_line_with_colors:
219 * @drawable: the drawable on which to draw the line
220 * @gc: base graphics to use
221 * @x: the x position of start of string (in pixels)
222 * @y: the y position of baseline (in pixels)
223 * @line: a #PangoLayoutLine
224 * @foreground: foreground override color, or %NULL for none
225 * @background: background override color, or %NULL for none
227 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
228 * layout's normal colors with @foreground and/or @background.
229 * @foreground and @background need not be allocated.
232 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
236 PangoLayoutLine *line,
237 const GdkColor *foreground,
238 const GdkColor *background)
240 GSList *tmp_list = line->runs;
241 PangoRectangle overall_rect;
242 PangoRectangle logical_rect;
243 PangoRectangle ink_rect;
244 PangoContext *context;
249 PangoUnderline last_uline = PANGO_UNDERLINE_NONE;
250 gint uline_start_x = 0;
251 gint uline_end_x = 0;
252 gint uline_end_x_extended = 0;
253 gint last_risen_y = 0;
254 gint low_y = G_MININT;
255 GdkGC *last_fg_gc = NULL;
256 gboolean last_fg_set = FALSE;
257 PangoColor last_fg_color;
259 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
260 g_return_if_fail (GDK_IS_GC (gc));
261 g_return_if_fail (line != NULL);
263 context = pango_layout_get_context (line->layout);
265 pango_layout_line_get_extents (line,NULL, &overall_rect);
269 PangoUnderline this_uline = PANGO_UNDERLINE_NONE;
270 PangoLayoutRun *run = tmp_list->data;
271 PangoColor fg_color, bg_color;
272 gboolean strike, fg_set, bg_set, shape_set;
276 tmp_list = tmp_list->next;
278 gdk_pango_get_item_properties (run->item, &this_uline,
289 /* we subtract the rise because X coordinates are upside down */
290 risen_y = y - rise / PANGO_SCALE;
294 if (this_uline == PANGO_UNDERLINE_NONE)
295 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
296 NULL, &logical_rect);
298 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
299 &ink_rect, &logical_rect);
302 if (bg_set || background)
305 PangoColor tmp = bg_color;
309 tmp.red = background->red;
310 tmp.blue = background->blue;
311 tmp.green = background->green;
314 bg_gc = gdk_pango_get_gc (drawable, context, &tmp, stipple, gc);
316 gdk_draw_rectangle (drawable, bg_gc, TRUE,
317 x + (x_off + logical_rect.x) / PANGO_SCALE,
318 risen_y + overall_rect.y / PANGO_SCALE,
319 logical_rect.width / PANGO_SCALE,
320 overall_rect.height / PANGO_SCALE);
323 gdk_gc_set_fill (bg_gc, GDK_SOLID);
325 gdk_pango_free_gc (context, bg_gc);
328 if (fg_set || stipple || foreground)
330 PangoColor tmp = fg_color;
334 tmp.red = foreground->red;
335 tmp.blue = foreground->blue;
336 tmp.green = foreground->green;
339 fg_gc = gdk_pango_get_gc (drawable, context, (fg_set || foreground) ? &tmp : NULL,
349 gx = x + x_off / PANGO_SCALE;
354 PangoColor color = { 65535, 65535, 65535 };
355 GdkGC *white_gc = gdk_pango_get_gc (drawable, context, &color, stipple, fg_gc);
356 gdk_draw_glyphs (drawable, white_gc, run->item->analysis.font,
360 gdk_pango_free_gc (context, white_gc);
363 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
368 if (this_uline != last_uline ||
369 risen_y != last_risen_y ||
370 fg_set != last_fg_set ||
371 (fg_set && (last_fg_color.red != fg_color.red ||
372 last_fg_color.green != fg_color.green ||
373 last_fg_color.blue != fg_color.blue)))
375 /* If only color changes, the underlines extend to the edge
376 * of the logical rectangle so they join up; otherwise they
377 * go 1 pixel beyond the ink rectangle. This doesn't work
378 * for low underlines (they will be at a different y anyways),
379 * so they follow the normal path.
381 gboolean extend_uline = (this_uline == last_uline &&
382 this_uline != PANGO_UNDERLINE_LOW &&
383 risen_y == last_risen_y);
385 /* Starting a new underline run
387 if (last_uline != PANGO_UNDERLINE_NONE)
389 draw_underline (drawable, last_fg_gc, last_uline,
392 extend_uline ? uline_end_x_extended : uline_end_x);
395 if (this_uline != PANGO_UNDERLINE_NONE)
398 uline_start_x = x + x_off / PANGO_SCALE;
400 uline_start_x = x + (x_off + ink_rect.x) / PANGO_SCALE - 1;
406 /* Update current underline segment information
408 if (this_uline != PANGO_UNDERLINE_NONE)
410 uline_end_x = x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE;
411 uline_end_x_extended = x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE - 1;
414 if (this_uline == PANGO_UNDERLINE_LOW)
415 low_y = MAX (low_y, risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE);
419 int centerline = logical_rect.y + logical_rect.height / 2;
421 gdk_draw_line (drawable, fg_gc,
422 x + (x_off + logical_rect.x) / PANGO_SCALE - 1,
423 risen_y + centerline / PANGO_SCALE,
424 x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE + 1,
425 risen_y + centerline / PANGO_SCALE);
428 if (last_fg_gc != gc && last_fg_gc)
429 gdk_pango_free_gc (context, last_fg_gc);
431 last_risen_y = risen_y;
432 last_uline = this_uline;
434 last_fg_set = fg_set;
436 last_fg_color = fg_color;
438 x_off += logical_rect.width;
441 /* Finish off any remaining underlines
443 if (last_uline != PANGO_UNDERLINE_NONE)
444 draw_underline (drawable, last_fg_gc, last_uline, last_risen_y, low_y,
445 uline_start_x, uline_end_x);
447 if (last_fg_gc != gc && last_fg_gc)
448 gdk_pango_free_gc (context, last_fg_gc);
452 * gdk_draw_layout_with_colors:
453 * @drawable: the drawable on which to draw string
454 * @gc: base graphics context to use
455 * @x: the X position of the left of the layout (in pixels)
456 * @y: the Y position of the top of the layout (in pixels)
457 * @layout: a #PangoLayout
458 * @foreground: foreground override color, or %NULL for none
459 * @background: background override color, or %NULL for none
461 * Render a #PangoLayout onto a #GdkDrawable, overriding the
462 * layout's normal colors with @foreground and/or @background.
463 * @foreground and @background need not be allocated.
465 * If you're using GTK+, the ususal way to obtain a #PangoLayout
466 * is gtk_widget_create_pango_layout().
469 gdk_draw_layout_with_colors (GdkDrawable *drawable,
474 const GdkColor *foreground,
475 const GdkColor *background)
477 PangoLayoutIter *iter;
479 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
480 g_return_if_fail (GDK_IS_GC (gc));
481 g_return_if_fail (PANGO_IS_LAYOUT (layout));
483 iter = pango_layout_get_iter (layout);
487 PangoRectangle logical_rect;
488 PangoLayoutLine *line;
491 line = pango_layout_iter_get_line (iter);
493 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
494 baseline = pango_layout_iter_get_baseline (iter);
496 gdk_draw_layout_line_with_colors (drawable, gc,
497 x + logical_rect.x / PANGO_SCALE,
498 y + baseline / PANGO_SCALE,
503 while (pango_layout_iter_next_line (iter));
505 pango_layout_iter_free (iter);
509 * gdk_draw_layout_line:
510 * @drawable: the drawable on which to draw the line
511 * @gc: base graphics to use
512 * @x: the x position of start of string (in pixels)
513 * @y: the y position of baseline (in pixels)
514 * @line: a #PangoLayoutLine
516 * Render a #PangoLayoutLine onto an GDK drawable
519 gdk_draw_layout_line (GdkDrawable *drawable,
523 PangoLayoutLine *line)
525 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
526 g_return_if_fail (GDK_IS_GC (gc));
527 g_return_if_fail (line != NULL);
529 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
534 * @drawable: the drawable on which to draw string
535 * @gc: base graphics context to use
536 * @x: the X position of the left of the layout (in pixels)
537 * @y: the Y position of the top of the layout (in pixels)
538 * @layout: a #PangoLayout
540 * Render a #PangoLayout onto a GDK drawable
542 * If you're using GTK+, the ususal way to obtain a #PangoLayout
543 * is gtk_widget_create_pango_layout().
546 gdk_draw_layout (GdkDrawable *drawable,
552 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
553 g_return_if_fail (GDK_IS_GC (gc));
554 g_return_if_fail (PANGO_IS_LAYOUT (layout));
556 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
560 gdk_pango_get_item_properties (PangoItem *item,
561 PangoUnderline *uline,
562 gboolean *strikethrough,
564 PangoColor *fg_color,
566 PangoColor *bg_color,
571 PangoRectangle *ink_rect,
572 PangoRectangle *logical_rect)
574 GSList *tmp_list = item->analysis.extra_attrs;
577 *strikethrough = FALSE;
599 PangoAttribute *attr = tmp_list->data;
601 switch (attr->klass->type)
603 case PANGO_ATTR_UNDERLINE:
605 *uline = ((PangoAttrInt *)attr)->value;
608 case PANGO_ATTR_STRIKETHROUGH:
610 *strikethrough = ((PangoAttrInt *)attr)->value;
613 case PANGO_ATTR_FOREGROUND:
615 *fg_color = ((PangoAttrColor *)attr)->color;
621 case PANGO_ATTR_BACKGROUND:
623 *bg_color = ((PangoAttrColor *)attr)->color;
629 case PANGO_ATTR_SHAPE:
633 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
635 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
638 case PANGO_ATTR_RISE:
640 *rise = ((PangoAttrInt *)attr)->value;
644 /* stipple_type and embossed_type aren't necessarily
645 * initialized, but they are 0, which is an
646 * invalid type so won't occur.
648 if (stipple && attr->klass->type == gdk_pango_attr_stipple_type)
650 *stipple = ((GdkPangoAttrStipple*)attr)->stipple;
652 else if (embossed && attr->klass->type == gdk_pango_attr_embossed_type)
654 *embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
658 tmp_list = tmp_list->next;
663 static PangoAttribute *
664 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
666 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
668 return gdk_pango_attr_stipple_new (src->stipple);
672 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
674 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
677 g_object_unref (st->stipple);
683 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
684 const PangoAttribute *attr2)
686 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
687 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
689 return a->stipple == b->stipple;
693 * gdk_pango_attr_stipple_new:
694 * @stipple: a bitmap to be set as stipple
696 * Creates a new attribute containing a stipple bitmap to be used when
697 * rendering the text.
699 * Return value: new #PangoAttribute
703 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
705 GdkPangoAttrStipple *result;
707 static PangoAttrClass klass = {
709 gdk_pango_attr_stipple_copy,
710 gdk_pango_attr_stipple_destroy,
711 gdk_pango_attr_stipple_compare
715 klass.type = gdk_pango_attr_stipple_type =
716 pango_attr_type_register ("GdkPangoAttrStipple");
718 result = g_new (GdkPangoAttrStipple, 1);
719 result->attr.klass = &klass;
722 g_object_ref (stipple);
724 result->stipple = stipple;
726 return (PangoAttribute *)result;
729 static PangoAttribute *
730 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
732 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
734 return gdk_pango_attr_embossed_new (e->embossed);
738 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
744 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
745 const PangoAttribute *attr2)
747 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
748 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
750 return e1->embossed == e2->embossed;
754 * gdk_pango_attr_embossed_new:
755 * @embossed: a bitmap to be set as embossed
757 * Creates a new attribute containing a embossed bitmap to be used when
758 * rendering the text.
760 * Return value: new #PangoAttribute
764 gdk_pango_attr_embossed_new (gboolean embossed)
766 GdkPangoAttrEmbossed *result;
768 static PangoAttrClass klass = {
770 gdk_pango_attr_embossed_copy,
771 gdk_pango_attr_embossed_destroy,
772 gdk_pango_attr_embossed_compare
776 klass.type = gdk_pango_attr_embossed_type =
777 pango_attr_type_register ("GdkPangoAttrEmbossed");
779 result = g_new (GdkPangoAttrEmbossed, 1);
780 result->attr.klass = &klass;
781 result->embossed = embossed;
783 return (PangoAttribute *)result;
786 /* Get a clip region to draw only part of a layout. index_ranges
787 * contains alternating range starts/stops. The region is the
788 * region which contains the given ranges, i.e. if you draw with the
789 * region as clip, only the given ranges are drawn.
793 * gdk_pango_layout_line_get_clip_region:
794 * @line: a #PangoLayoutLine
795 * @x_origin: X pixel where you intend to draw the layout line with this clip
796 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
797 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
798 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
800 * Obtains a clip region which contains the areas where the given
801 * ranges of text would be drawn. @x_origin and @y_origin are the same
802 * position you would pass to gdk_draw_layout_line(). @index_ranges
803 * should contain ranges of bytes in the layout's text. The clip
804 * region will include space to the left or right of the line (to the
805 * layout bounding box) if you have indexes above or below the indexes
806 * contained inside the line. This is to draw the selection all the way
807 * to the side of the layout. However, the clip region is in line coordinates,
808 * not layout coordinates.
810 * Return value: a clip region containing the given ranges
813 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
819 GdkRegion *clip_region;
821 PangoRectangle logical_rect;
822 PangoLayoutIter *iter;
825 g_return_val_if_fail (line != NULL, NULL);
826 g_return_val_if_fail (index_ranges != NULL, NULL);
828 clip_region = gdk_region_new ();
830 iter = pango_layout_get_iter (line->layout);
831 while (pango_layout_iter_get_line (iter) != line)
832 pango_layout_iter_next_line (iter);
834 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
835 baseline = pango_layout_iter_get_baseline (iter);
837 pango_layout_iter_free (iter);
842 gint *pixel_ranges = NULL;
843 gint n_pixel_ranges = 0;
846 /* Note that get_x_ranges returns layout coordinates
848 pango_layout_line_get_x_ranges (line,
851 &pixel_ranges, &n_pixel_ranges);
853 for (j=0; j < n_pixel_ranges; j++)
857 rect.x = x_origin + pixel_ranges[2*j] / PANGO_SCALE - logical_rect.x / PANGO_SCALE;
858 rect.y = y_origin - (baseline / PANGO_SCALE - logical_rect.y / PANGO_SCALE);
859 rect.width = (pixel_ranges[2*j + 1] - pixel_ranges[2*j]) / PANGO_SCALE;
860 rect.height = logical_rect.height / PANGO_SCALE;
862 gdk_region_union_with_rect (clip_region, &rect);
865 g_free (pixel_ranges);
873 * gdk_pango_layout_get_clip_region:
874 * @layout: a #PangoLayout
875 * @x_origin: X pixel where you intend to draw the layout with this clip
876 * @y_origin: Y pixel where you intend to draw the layout with this clip
877 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
878 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
880 * Obtains a clip region which contains the areas where the given ranges
881 * of text would be drawn. @x_origin and @y_origin are the same position
882 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
883 * ranges of bytes in the layout's text.
885 * Return value: a clip region containing the given ranges
888 gdk_pango_layout_get_clip_region (PangoLayout *layout,
894 PangoLayoutIter *iter;
895 GdkRegion *clip_region;
897 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
898 g_return_val_if_fail (index_ranges != NULL, NULL);
900 clip_region = gdk_region_new ();
902 iter = pango_layout_get_iter (layout);
906 PangoRectangle logical_rect;
907 PangoLayoutLine *line;
908 GdkRegion *line_region;
911 line = pango_layout_iter_get_line (iter);
913 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
914 baseline = pango_layout_iter_get_baseline (iter);
916 line_region = gdk_pango_layout_line_get_clip_region (line,
917 x_origin + logical_rect.x / PANGO_SCALE,
918 y_origin + baseline / PANGO_SCALE,
922 gdk_region_union (clip_region, line_region);
923 gdk_region_destroy (line_region);
925 while (pango_layout_iter_next_line (iter));
927 pango_layout_iter_free (iter);
933 * gdk_pango_context_get:
935 * Creates a #PangoContext for the default GDK screen.
937 * The context must be freed when you're finished with it.
939 * When using GTK+, normally you should use gtk_widget_get_pango_context()
940 * instead of this function, to get the appropriate context for
941 * the widget you intend to render text onto.
943 * Return value: a new #PangoContext for the default display
946 gdk_pango_context_get (void)
948 return gdk_pango_context_get_for_screen (gdk_screen_get_default ());