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"
25 #include "gdkscreen.h"
27 #define GDK_INFO_KEY "gdk-info"
29 typedef struct _GdkPangoContextInfo GdkPangoContextInfo;
31 struct _GdkPangoContextInfo
33 GdkColormap *colormap;
36 static PangoAttrType gdk_pango_attr_stipple_type;
37 static PangoAttrType gdk_pango_attr_embossed_type;
39 static void gdk_pango_get_item_properties (PangoItem *item,
40 PangoUnderline *uline,
41 gboolean *strikethrough,
50 PangoRectangle *ink_rect,
51 PangoRectangle *logical_rect);
54 gdk_pango_context_destroy (GdkPangoContextInfo *info)
57 g_object_unref (info->colormap);
61 static GdkPangoContextInfo *
62 gdk_pango_context_get_info (PangoContext *context, gboolean create)
64 GdkPangoContextInfo *info =
65 g_object_get_qdata (G_OBJECT (context),
66 g_quark_try_string (GDK_INFO_KEY));
69 info = g_new (GdkPangoContextInfo, 1);
70 info->colormap = NULL;
72 g_object_set_qdata_full (G_OBJECT (context),
73 g_quark_from_static_string (GDK_INFO_KEY),
74 info, (GDestroyNotify)gdk_pango_context_destroy);
81 gdk_pango_get_gc (GdkDrawable *drawable,
82 PangoContext *context,
88 GdkPangoContextInfo *info;
90 g_return_val_if_fail (context != NULL, NULL);
92 info = gdk_pango_context_get_info (context, FALSE);
94 if (info == NULL || info->colormap == NULL)
96 g_warning ("you must set the colormap on a PangoContext before using it to draw a layout");
100 result = gdk_gc_new (drawable);
101 gdk_gc_copy (result, base_gc);
107 color.red = fg_color->red;
108 color.green = fg_color->green;
109 color.blue = fg_color->blue;
111 gdk_rgb_find_color (info->colormap, &color);
112 gdk_gc_set_foreground (result, &color);
117 gdk_gc_set_fill (result, GDK_STIPPLED);
118 gdk_gc_set_stipple (result, stipple);
125 gdk_pango_free_gc (PangoContext *context,
132 * gdk_pango_context_set_colormap:
133 * @context: a #PangoContext
134 * @colormap: a #GdkColormap
136 * Sets the colormap to be used for drawing with @context.
138 * If you obtained your context from gtk_widget_get_pango_context() or
139 * gtk_widget_create_pango_context(), the colormap will already be set
140 * to the colormap for the widget, so you shouldn't need this
144 gdk_pango_context_set_colormap (PangoContext *context,
145 GdkColormap *colormap)
147 GdkPangoContextInfo *info;
149 g_return_if_fail (context != NULL);
151 info = gdk_pango_context_get_info (context, TRUE);
152 g_return_if_fail (info != NULL);
154 if (info->colormap != colormap)
157 g_object_unref (info->colormap);
159 info->colormap = colormap;
162 g_object_ref (info->colormap);
167 draw_underline (GdkDrawable *drawable,
169 PangoUnderline uline,
177 case PANGO_UNDERLINE_NONE:
179 case PANGO_UNDERLINE_DOUBLE:
180 gdk_draw_line (drawable, gc,
181 start_x, baseline_y + 3,
182 end_x, baseline_y + 3);
184 case PANGO_UNDERLINE_SINGLE:
185 gdk_draw_line (drawable, gc,
186 start_x, baseline_y + 1,
187 end_x, baseline_y + 1);
189 case PANGO_UNDERLINE_ERROR:
191 int point_x, point_y;
194 for (point_x = start_x;
198 point_y = counter ? baseline_y + 1 : baseline_y + 2;
200 gdk_draw_line (drawable, gc,
202 MIN (point_x + 1, end_x), point_y);
204 counter = (counter + 1) % 2;
208 case PANGO_UNDERLINE_LOW:
209 gdk_draw_line (drawable, gc,
217 * gdk_draw_layout_line_with_colors:
218 * @drawable: the drawable on which to draw the line
219 * @gc: base graphics to use
220 * @x: the x position of start of string (in pixels)
221 * @y: the y position of baseline (in pixels)
222 * @line: a #PangoLayoutLine
223 * @foreground: foreground override color, or %NULL for none
224 * @background: background override color, or %NULL for none
226 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
227 * layout's normal colors with @foreground and/or @background.
228 * @foreground and @background need not be allocated.
231 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
235 PangoLayoutLine *line,
236 const GdkColor *foreground,
237 const GdkColor *background)
239 GSList *tmp_list = line->runs;
240 PangoRectangle overall_rect;
241 PangoRectangle logical_rect;
242 PangoRectangle ink_rect;
243 PangoContext *context;
248 PangoUnderline last_uline = PANGO_UNDERLINE_NONE;
249 gint uline_start_x = 0;
250 gint uline_end_x = 0;
251 gint uline_end_x_extended = 0;
252 gint last_risen_y = 0;
253 gint low_y = G_MININT;
254 GdkGC *last_fg_gc = NULL;
255 gboolean last_fg_set = FALSE;
256 PangoColor last_fg_color;
258 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
259 g_return_if_fail (GDK_IS_GC (gc));
260 g_return_if_fail (line != NULL);
262 context = pango_layout_get_context (line->layout);
264 pango_layout_line_get_extents (line,NULL, &overall_rect);
268 PangoUnderline this_uline = PANGO_UNDERLINE_NONE;
269 PangoLayoutRun *run = tmp_list->data;
270 PangoColor fg_color, bg_color;
271 gboolean strike, fg_set, bg_set, shape_set;
275 tmp_list = tmp_list->next;
277 gdk_pango_get_item_properties (run->item, &this_uline,
288 /* we subtract the rise because X coordinates are upside down */
289 risen_y = y - rise / PANGO_SCALE;
293 if (this_uline == PANGO_UNDERLINE_NONE)
294 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
295 NULL, &logical_rect);
297 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
298 &ink_rect, &logical_rect);
301 if (bg_set || background)
304 PangoColor tmp = bg_color;
308 tmp.red = background->red;
309 tmp.blue = background->blue;
310 tmp.green = background->green;
313 bg_gc = gdk_pango_get_gc (drawable, context, &tmp, stipple, gc);
315 gdk_draw_rectangle (drawable, bg_gc, TRUE,
316 x + (x_off + logical_rect.x) / PANGO_SCALE,
317 risen_y + overall_rect.y / PANGO_SCALE,
318 logical_rect.width / PANGO_SCALE,
319 overall_rect.height / PANGO_SCALE);
322 gdk_gc_set_fill (bg_gc, GDK_SOLID);
324 gdk_pango_free_gc (context, bg_gc);
327 if (fg_set || stipple || foreground)
329 PangoColor tmp = fg_color;
333 tmp.red = foreground->red;
334 tmp.blue = foreground->blue;
335 tmp.green = foreground->green;
338 fg_gc = gdk_pango_get_gc (drawable, context, (fg_set || foreground) ? &tmp : NULL,
348 gx = x + x_off / PANGO_SCALE;
353 PangoColor color = { 65535, 65535, 65535 };
354 GdkGC *white_gc = gdk_pango_get_gc (drawable, context, &color, stipple, fg_gc);
355 gdk_draw_glyphs (drawable, white_gc, run->item->analysis.font,
359 gdk_pango_free_gc (context, white_gc);
362 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
367 if (this_uline != last_uline ||
368 risen_y != last_risen_y ||
369 fg_set != last_fg_set ||
370 (fg_set && (last_fg_color.red != fg_color.red ||
371 last_fg_color.green != fg_color.green ||
372 last_fg_color.blue != fg_color.blue)))
374 /* If only color changes, the underlines extend to the edge
375 * of the logical rectangle so they join up; otherwise they
376 * go 1 pixel beyond the ink rectangle. This doesn't work
377 * for low underlines (they will be at a different y anyways),
378 * so they follow the normal path.
380 gboolean extend_uline = (this_uline == last_uline &&
381 this_uline != PANGO_UNDERLINE_LOW &&
382 risen_y == last_risen_y);
384 /* Starting a new underline run
386 if (last_uline != PANGO_UNDERLINE_NONE)
388 draw_underline (drawable, last_fg_gc, last_uline,
391 extend_uline ? uline_end_x_extended : uline_end_x);
394 if (this_uline != PANGO_UNDERLINE_NONE)
397 uline_start_x = x + x_off / PANGO_SCALE;
399 uline_start_x = x + (x_off + ink_rect.x) / PANGO_SCALE - 1;
405 /* Update current underline segment information
407 if (this_uline != PANGO_UNDERLINE_NONE)
409 uline_end_x = x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE;
410 uline_end_x_extended = x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE - 1;
413 if (this_uline == PANGO_UNDERLINE_LOW)
414 low_y = MAX (low_y, risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE);
418 int centerline = logical_rect.y + logical_rect.height / 2;
420 gdk_draw_line (drawable, fg_gc,
421 x + (x_off + logical_rect.x) / PANGO_SCALE - 1,
422 risen_y + centerline / PANGO_SCALE,
423 x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE + 1,
424 risen_y + centerline / PANGO_SCALE);
427 if (last_fg_gc != gc && last_fg_gc)
428 gdk_pango_free_gc (context, last_fg_gc);
430 last_risen_y = risen_y;
431 last_uline = this_uline;
433 last_fg_set = fg_set;
435 last_fg_color = fg_color;
437 x_off += logical_rect.width;
440 /* Finish off any remaining underlines
442 if (last_uline != PANGO_UNDERLINE_NONE)
443 draw_underline (drawable, last_fg_gc, last_uline, last_risen_y, low_y,
444 uline_start_x, uline_end_x);
446 if (last_fg_gc != gc && last_fg_gc)
447 gdk_pango_free_gc (context, last_fg_gc);
451 * gdk_draw_layout_with_colors:
452 * @drawable: the drawable on which to draw string
453 * @gc: base graphics context to use
454 * @x: the X position of the left of the layout (in pixels)
455 * @y: the Y position of the top of the layout (in pixels)
456 * @layout: a #PangoLayout
457 * @foreground: foreground override color, or %NULL for none
458 * @background: background override color, or %NULL for none
460 * Render a #PangoLayout onto a #GdkDrawable, overriding the
461 * layout's normal colors with @foreground and/or @background.
462 * @foreground and @background need not be allocated.
464 * If you're using GTK+, the ususal way to obtain a #PangoLayout
465 * is gtk_widget_create_pango_layout().
468 gdk_draw_layout_with_colors (GdkDrawable *drawable,
473 const GdkColor *foreground,
474 const GdkColor *background)
476 PangoLayoutIter *iter;
478 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
479 g_return_if_fail (GDK_IS_GC (gc));
480 g_return_if_fail (PANGO_IS_LAYOUT (layout));
482 iter = pango_layout_get_iter (layout);
486 PangoRectangle logical_rect;
487 PangoLayoutLine *line;
490 line = pango_layout_iter_get_line (iter);
492 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
493 baseline = pango_layout_iter_get_baseline (iter);
495 gdk_draw_layout_line_with_colors (drawable, gc,
496 x + logical_rect.x / PANGO_SCALE,
497 y + baseline / PANGO_SCALE,
502 while (pango_layout_iter_next_line (iter));
504 pango_layout_iter_free (iter);
508 * gdk_draw_layout_line:
509 * @drawable: the drawable on which to draw the line
510 * @gc: base graphics to use
511 * @x: the x position of start of string (in pixels)
512 * @y: the y position of baseline (in pixels)
513 * @line: a #PangoLayoutLine
515 * Render a #PangoLayoutLine onto an GDK drawable
518 gdk_draw_layout_line (GdkDrawable *drawable,
522 PangoLayoutLine *line)
524 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
525 g_return_if_fail (GDK_IS_GC (gc));
526 g_return_if_fail (line != NULL);
528 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
533 * @drawable: the drawable on which to draw string
534 * @gc: base graphics context to use
535 * @x: the X position of the left of the layout (in pixels)
536 * @y: the Y position of the top of the layout (in pixels)
537 * @layout: a #PangoLayout
539 * Render a #PangoLayout onto a GDK drawable
541 * If you're using GTK+, the ususal way to obtain a #PangoLayout
542 * is gtk_widget_create_pango_layout().
545 gdk_draw_layout (GdkDrawable *drawable,
551 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
552 g_return_if_fail (GDK_IS_GC (gc));
553 g_return_if_fail (PANGO_IS_LAYOUT (layout));
555 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
559 gdk_pango_get_item_properties (PangoItem *item,
560 PangoUnderline *uline,
561 gboolean *strikethrough,
563 PangoColor *fg_color,
565 PangoColor *bg_color,
570 PangoRectangle *ink_rect,
571 PangoRectangle *logical_rect)
573 GSList *tmp_list = item->analysis.extra_attrs;
576 *strikethrough = FALSE;
598 PangoAttribute *attr = tmp_list->data;
600 switch (attr->klass->type)
602 case PANGO_ATTR_UNDERLINE:
604 *uline = ((PangoAttrInt *)attr)->value;
607 case PANGO_ATTR_STRIKETHROUGH:
609 *strikethrough = ((PangoAttrInt *)attr)->value;
612 case PANGO_ATTR_FOREGROUND:
614 *fg_color = ((PangoAttrColor *)attr)->color;
620 case PANGO_ATTR_BACKGROUND:
622 *bg_color = ((PangoAttrColor *)attr)->color;
628 case PANGO_ATTR_SHAPE:
632 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
634 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
637 case PANGO_ATTR_RISE:
639 *rise = ((PangoAttrInt *)attr)->value;
643 /* stipple_type and embossed_type aren't necessarily
644 * initialized, but they are 0, which is an
645 * invalid type so won't occur.
647 if (stipple && attr->klass->type == gdk_pango_attr_stipple_type)
649 *stipple = ((GdkPangoAttrStipple*)attr)->stipple;
651 else if (embossed && attr->klass->type == gdk_pango_attr_embossed_type)
653 *embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
657 tmp_list = tmp_list->next;
662 static PangoAttribute *
663 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
665 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
667 return gdk_pango_attr_stipple_new (src->stipple);
671 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
673 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
676 g_object_unref (st->stipple);
682 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
683 const PangoAttribute *attr2)
685 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
686 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
688 return a->stipple == b->stipple;
692 * gdk_pango_attr_stipple_new:
693 * @stipple: a bitmap to be set as stipple
695 * Creates a new attribute containing a stipple bitmap to be used when
696 * rendering the text.
698 * Return value: new #PangoAttribute
702 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
704 GdkPangoAttrStipple *result;
706 static PangoAttrClass klass = {
708 gdk_pango_attr_stipple_copy,
709 gdk_pango_attr_stipple_destroy,
710 gdk_pango_attr_stipple_compare
714 klass.type = gdk_pango_attr_stipple_type =
715 pango_attr_type_register ("GdkPangoAttrStipple");
717 result = g_new (GdkPangoAttrStipple, 1);
718 result->attr.klass = &klass;
721 g_object_ref (stipple);
723 result->stipple = stipple;
725 return (PangoAttribute *)result;
728 static PangoAttribute *
729 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
731 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
733 return gdk_pango_attr_embossed_new (e->embossed);
737 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
743 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
744 const PangoAttribute *attr2)
746 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
747 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
749 return e1->embossed == e2->embossed;
753 * gdk_pango_attr_embossed_new:
754 * @embossed: a bitmap to be set as embossed
756 * Creates a new attribute containing a embossed bitmap to be used when
757 * rendering the text.
759 * Return value: new #PangoAttribute
763 gdk_pango_attr_embossed_new (gboolean embossed)
765 GdkPangoAttrEmbossed *result;
767 static PangoAttrClass klass = {
769 gdk_pango_attr_embossed_copy,
770 gdk_pango_attr_embossed_destroy,
771 gdk_pango_attr_embossed_compare
775 klass.type = gdk_pango_attr_embossed_type =
776 pango_attr_type_register ("GdkPangoAttrEmbossed");
778 result = g_new (GdkPangoAttrEmbossed, 1);
779 result->attr.klass = &klass;
780 result->embossed = embossed;
782 return (PangoAttribute *)result;
785 /* Get a clip region to draw only part of a layout. index_ranges
786 * contains alternating range starts/stops. The region is the
787 * region which contains the given ranges, i.e. if you draw with the
788 * region as clip, only the given ranges are drawn.
792 * gdk_pango_layout_line_get_clip_region:
793 * @line: a #PangoLayoutLine
794 * @x_origin: X pixel where you intend to draw the layout line with this clip
795 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
796 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
797 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
799 * Obtains a clip region which contains the areas where the given
800 * ranges of text would be drawn. @x_origin and @y_origin are the same
801 * position you would pass to gdk_draw_layout_line(). @index_ranges
802 * should contain ranges of bytes in the layout's text. The clip
803 * region will include space to the left or right of the line (to the
804 * layout bounding box) if you have indexes above or below the indexes
805 * contained inside the line. This is to draw the selection all the way
806 * to the side of the layout. However, the clip region is in line coordinates,
807 * not layout coordinates.
809 * Return value: a clip region containing the given ranges
812 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
818 GdkRegion *clip_region;
820 PangoRectangle logical_rect;
821 PangoLayoutIter *iter;
824 g_return_val_if_fail (line != NULL, NULL);
825 g_return_val_if_fail (index_ranges != NULL, NULL);
827 clip_region = gdk_region_new ();
829 iter = pango_layout_get_iter (line->layout);
830 while (pango_layout_iter_get_line (iter) != line)
831 pango_layout_iter_next_line (iter);
833 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
834 baseline = pango_layout_iter_get_baseline (iter);
836 pango_layout_iter_free (iter);
841 gint *pixel_ranges = NULL;
842 gint n_pixel_ranges = 0;
845 /* Note that get_x_ranges returns layout coordinates
847 pango_layout_line_get_x_ranges (line,
850 &pixel_ranges, &n_pixel_ranges);
852 for (j=0; j < n_pixel_ranges; j++)
856 rect.x = x_origin + pixel_ranges[2*j] / PANGO_SCALE - logical_rect.x / PANGO_SCALE;
857 rect.y = y_origin - (baseline / PANGO_SCALE - logical_rect.y / PANGO_SCALE);
858 rect.width = (pixel_ranges[2*j + 1] - pixel_ranges[2*j]) / PANGO_SCALE;
859 rect.height = logical_rect.height / PANGO_SCALE;
861 gdk_region_union_with_rect (clip_region, &rect);
864 g_free (pixel_ranges);
872 * gdk_pango_layout_get_clip_region:
873 * @layout: a #PangoLayout
874 * @x_origin: X pixel where you intend to draw the layout with this clip
875 * @y_origin: Y pixel where you intend to draw the layout with this clip
876 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
877 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
879 * Obtains a clip region which contains the areas where the given ranges
880 * of text would be drawn. @x_origin and @y_origin are the same position
881 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
882 * ranges of bytes in the layout's text.
884 * Return value: a clip region containing the given ranges
887 gdk_pango_layout_get_clip_region (PangoLayout *layout,
893 PangoLayoutIter *iter;
894 GdkRegion *clip_region;
896 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
897 g_return_val_if_fail (index_ranges != NULL, NULL);
899 clip_region = gdk_region_new ();
901 iter = pango_layout_get_iter (layout);
905 PangoRectangle logical_rect;
906 PangoLayoutLine *line;
907 GdkRegion *line_region;
910 line = pango_layout_iter_get_line (iter);
912 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
913 baseline = pango_layout_iter_get_baseline (iter);
915 line_region = gdk_pango_layout_line_get_clip_region (line,
916 x_origin + logical_rect.x / PANGO_SCALE,
917 y_origin + baseline / PANGO_SCALE,
921 gdk_region_union (clip_region, line_region);
922 gdk_region_destroy (line_region);
924 while (pango_layout_iter_next_line (iter));
926 pango_layout_iter_free (iter);
932 * gdk_pango_context_get:
934 * Creates a #PangoContext for the default GDK screen.
936 * The context must be freed when you're finished with it.
938 * When using GTK+, normally you should use gtk_widget_get_pango_context()
939 * instead of this function, to get the appropriate context for
940 * the widget you intend to render text onto.
942 * Return value: a new #PangoContext for the default display
945 gdk_pango_context_get (void)
947 return gdk_pango_context_get_for_screen (gdk_screen_get_default ());