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 gdk_colormap_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.
137 * If you obtained your context from gtk_widget_get_pango_context() or
138 * gtk_widget_create_pango_context(), the colormap will already be set
139 * 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 gdk_colormap_unref (info->colormap);
159 info->colormap = colormap;
162 gdk_colormap_ref (info->colormap);
167 * gdk_draw_layout_line_with_colors:
168 * @drawable: the drawable on which to draw the line
169 * @gc: base graphics to use
170 * @x: the x position of start of string (in pixels)
171 * @y: the y position of baseline (in pixels)
172 * @line: a #PangoLayoutLine
173 * @foreground: foreground override color, or %NULL for none
174 * @background: background override color, or %NULL for none
176 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
177 * layout's normal colors with @foreground and/or @background.
178 * @foreground and @background need not be allocated.
181 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
185 PangoLayoutLine *line,
186 GdkColor *foreground,
187 GdkColor *background)
189 GSList *tmp_list = line->runs;
190 PangoRectangle overall_rect;
191 PangoRectangle logical_rect;
192 PangoRectangle ink_rect;
193 PangoContext *context;
199 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
200 g_return_if_fail (GDK_IS_GC (gc));
201 g_return_if_fail (line != NULL);
203 context = pango_layout_get_context (line->layout);
205 pango_layout_line_get_extents (line,NULL, &overall_rect);
209 PangoUnderline uline = PANGO_UNDERLINE_NONE;
210 PangoLayoutRun *run = tmp_list->data;
211 PangoColor fg_color, bg_color;
212 gboolean strike, fg_set, bg_set, shape_set;
216 tmp_list = tmp_list->next;
218 gdk_pango_get_item_properties (run->item, &uline,
229 /* we subtract the rise because X coordinates are upside down */
230 risen_y = y - rise / PANGO_SCALE;
234 if (uline == PANGO_UNDERLINE_NONE)
235 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
236 NULL, &logical_rect);
238 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
239 &ink_rect, &logical_rect);
242 if (bg_set || background)
245 PangoColor tmp = bg_color;
249 tmp.red = background->red;
250 tmp.blue = background->blue;
251 tmp.green = background->green;
254 bg_gc = gdk_pango_get_gc (drawable, context, &tmp, stipple, gc);
256 gdk_draw_rectangle (drawable, bg_gc, TRUE,
257 x + (x_off + logical_rect.x) / PANGO_SCALE,
258 risen_y + overall_rect.y / PANGO_SCALE,
259 logical_rect.width / PANGO_SCALE,
260 overall_rect.height / PANGO_SCALE);
263 gdk_gc_set_fill (bg_gc, GDK_SOLID);
265 gdk_pango_free_gc (context, bg_gc);
268 if (fg_set || stipple || foreground)
270 PangoColor tmp = fg_color;
274 tmp.red = foreground->red;
275 tmp.blue = foreground->blue;
276 tmp.green = foreground->green;
279 fg_gc = gdk_pango_get_gc (drawable, context, (fg_set || foreground) ? &tmp : NULL,
289 gx = x + x_off / PANGO_SCALE;
294 PangoColor color = { 65535, 65535, 65535 };
295 GdkGC *white_gc = gdk_pango_get_gc (drawable, context, &color, stipple, fg_gc);
296 gdk_draw_glyphs (drawable, white_gc, run->item->analysis.font,
300 gdk_pango_free_gc (context, white_gc);
303 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
310 case PANGO_UNDERLINE_NONE:
312 case PANGO_UNDERLINE_DOUBLE:
313 gdk_draw_line (drawable, fg_gc,
314 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
316 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
319 case PANGO_UNDERLINE_SINGLE:
320 gdk_draw_line (drawable, fg_gc,
321 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
323 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
326 case PANGO_UNDERLINE_LOW:
327 gdk_draw_line (drawable, fg_gc,
328 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
329 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
330 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
331 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
337 int centerline = logical_rect.y + logical_rect.height / 2;
339 gdk_draw_line (drawable, fg_gc,
340 x + (x_off + logical_rect.x) / PANGO_SCALE - 1,
341 risen_y + centerline / PANGO_SCALE,
342 x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE + 1,
343 risen_y + centerline / PANGO_SCALE);
347 gdk_pango_free_gc (context, fg_gc);
349 x_off += logical_rect.width;
354 * gdk_draw_layout_with_colors:
355 * @drawable: the drawable on which to draw string
356 * @gc: base graphics context to use
357 * @x: the X position of the left of the layout (in pixels)
358 * @y: the Y position of the top of the layout (in pixels)
359 * @layout: a #PangoLayout
360 * @foreground: foreground override color, or %NULL for none
361 * @background: background override color, or %NULL for none
363 * Render a #PangoLayout onto a #GdkDrawable, overriding the
364 * layout's normal colors with @foreground and/or @background.
365 * @foreground and @background need not be allocated.
368 gdk_draw_layout_with_colors (GdkDrawable *drawable,
373 GdkColor *foreground,
374 GdkColor *background)
376 PangoLayoutIter *iter;
378 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
379 g_return_if_fail (GDK_IS_GC (gc));
380 g_return_if_fail (PANGO_IS_LAYOUT (layout));
382 iter = pango_layout_get_iter (layout);
386 PangoRectangle logical_rect;
387 PangoLayoutLine *line;
390 line = pango_layout_iter_get_line (iter);
392 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
393 baseline = pango_layout_iter_get_baseline (iter);
395 gdk_draw_layout_line_with_colors (drawable, gc,
396 x + logical_rect.x / PANGO_SCALE,
397 y + baseline / PANGO_SCALE,
402 while (pango_layout_iter_next_line (iter));
404 pango_layout_iter_free (iter);
408 * gdk_draw_layout_line:
409 * @drawable: the drawable on which to draw the line
410 * @gc: base graphics to use
411 * @x: the x position of start of string (in pixels)
412 * @y: the y position of baseline (in pixels)
413 * @line: a #PangoLayoutLine
415 * Render a #PangoLayoutLine onto an GDK drawable
418 gdk_draw_layout_line (GdkDrawable *drawable,
422 PangoLayoutLine *line)
424 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
425 g_return_if_fail (GDK_IS_GC (gc));
426 g_return_if_fail (line != NULL);
428 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
433 * @drawable: the drawable on which to draw string
434 * @gc: base graphics context to use
435 * @x: the X position of the left of the layout (in pixels)
436 * @y: the Y position of the top of the layout (in pixels)
437 * @layout: a #PangoLayout
439 * Render a #PangoLayout onto a GDK drawable
442 gdk_draw_layout (GdkDrawable *drawable,
448 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
449 g_return_if_fail (GDK_IS_GC (gc));
450 g_return_if_fail (PANGO_IS_LAYOUT (layout));
452 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
456 gdk_pango_get_item_properties (PangoItem *item,
457 PangoUnderline *uline,
458 gboolean *strikethrough,
460 PangoColor *fg_color,
462 PangoColor *bg_color,
467 PangoRectangle *ink_rect,
468 PangoRectangle *logical_rect)
470 GSList *tmp_list = item->analysis.extra_attrs;
473 *strikethrough = FALSE;
495 PangoAttribute *attr = tmp_list->data;
497 switch (attr->klass->type)
499 case PANGO_ATTR_UNDERLINE:
501 *uline = ((PangoAttrInt *)attr)->value;
504 case PANGO_ATTR_STRIKETHROUGH:
506 *strikethrough = ((PangoAttrInt *)attr)->value;
509 case PANGO_ATTR_FOREGROUND:
511 *fg_color = ((PangoAttrColor *)attr)->color;
517 case PANGO_ATTR_BACKGROUND:
519 *bg_color = ((PangoAttrColor *)attr)->color;
525 case PANGO_ATTR_SHAPE:
529 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
531 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
534 case PANGO_ATTR_RISE:
536 *rise = ((PangoAttrInt *)attr)->value;
540 /* stipple_type and embossed_type aren't necessarily
541 * initialized, but they are 0, which is an
542 * invalid type so won't occur.
544 if (stipple && attr->klass->type == gdk_pango_attr_stipple_type)
546 *stipple = ((GdkPangoAttrStipple*)attr)->stipple;
548 else if (embossed && attr->klass->type == gdk_pango_attr_embossed_type)
550 *embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
554 tmp_list = tmp_list->next;
559 static PangoAttribute *
560 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
562 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
564 return gdk_pango_attr_stipple_new (src->stipple);
568 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
570 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
573 g_object_unref (G_OBJECT (st->stipple));
579 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
580 const PangoAttribute *attr2)
582 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
583 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
585 return a->stipple == b->stipple;
589 * gdk_pango_attr_stipple_new:
590 * @stipple: a bitmap to be set as stipple
592 * Creates a new attribute containing a stipple bitmap to be used when
593 * rendering the text.
595 * Return value: new #PangoAttribute
599 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
601 GdkPangoAttrStipple *result;
603 static PangoAttrClass klass = {
605 gdk_pango_attr_stipple_copy,
606 gdk_pango_attr_stipple_destroy,
607 gdk_pango_attr_stipple_compare
611 klass.type = gdk_pango_attr_stipple_type =
612 pango_attr_type_register ("GdkPangoAttrStipple");
614 result = g_new (GdkPangoAttrStipple, 1);
615 result->attr.klass = &klass;
618 g_object_ref (stipple);
620 result->stipple = stipple;
622 return (PangoAttribute *)result;
625 static PangoAttribute *
626 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
628 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
630 return gdk_pango_attr_embossed_new (e->embossed);
634 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
640 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
641 const PangoAttribute *attr2)
643 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
644 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
646 return e1->embossed == e2->embossed;
650 * gdk_pango_attr_embossed_new:
651 * @embossed: a bitmap to be set as embossed
653 * Creates a new attribute containing a embossed bitmap to be used when
654 * rendering the text.
656 * Return value: new #PangoAttribute
660 gdk_pango_attr_embossed_new (gboolean embossed)
662 GdkPangoAttrEmbossed *result;
664 static PangoAttrClass klass = {
666 gdk_pango_attr_embossed_copy,
667 gdk_pango_attr_embossed_destroy,
668 gdk_pango_attr_embossed_compare
672 klass.type = gdk_pango_attr_embossed_type =
673 pango_attr_type_register ("GdkPangoAttrEmbossed");
675 result = g_new (GdkPangoAttrEmbossed, 1);
676 result->attr.klass = &klass;
677 result->embossed = embossed;
679 return (PangoAttribute *)result;
682 /* Get a clip region to draw only part of a layout. index_ranges
683 * contains alternating range starts/stops. The region is the
684 * region which contains the given ranges, i.e. if you draw with the
685 * region as clip, only the given ranges are drawn.
689 * gdk_pango_layout_line_get_clip_region:
690 * @line: a #PangoLayoutLine
691 * @x_origin: X pixel where you intend to draw the layout line with this clip
692 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
693 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
694 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
696 * Obtains a clip region which contains the areas where the given
697 * ranges of text would be drawn. @x_origin and @y_origin are the same
698 * position you would pass to gdk_draw_layout_line(). @index_ranges
699 * should contain ranges of bytes in the layout's text. The clip
700 * region will include space to the left or right of the line (to the
701 * layout bounding box) if you have indexes above or below the indexes
702 * contained inside the line. This is to draw the selection all the way
703 * to the side of the layout. However, the clip region is in line coordinates,
704 * not layout coordinates.
706 * Return value: a clip region containing the given ranges
709 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
715 GdkRegion *clip_region;
717 PangoRectangle logical_rect;
718 PangoLayoutIter *iter;
721 g_return_val_if_fail (line != NULL, NULL);
722 g_return_val_if_fail (index_ranges != NULL, NULL);
724 clip_region = gdk_region_new ();
726 iter = pango_layout_get_iter (line->layout);
727 while (pango_layout_iter_get_line (iter) != line)
728 pango_layout_iter_next_line (iter);
730 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
731 baseline = pango_layout_iter_get_baseline (iter);
733 pango_layout_iter_free (iter);
738 gint *pixel_ranges = NULL;
739 gint n_pixel_ranges = 0;
742 /* Note that get_x_ranges returns layout coordinates
744 pango_layout_line_get_x_ranges (line,
747 &pixel_ranges, &n_pixel_ranges);
749 for (j=0; j < n_pixel_ranges; j++)
753 rect.x = x_origin + pixel_ranges[2*j] / PANGO_SCALE - logical_rect.x / PANGO_SCALE;
754 rect.y = y_origin - (baseline / PANGO_SCALE - logical_rect.y / PANGO_SCALE);
755 rect.width = (pixel_ranges[2*j + 1] - pixel_ranges[2*j]) / PANGO_SCALE;
756 rect.height = logical_rect.height / PANGO_SCALE;
758 gdk_region_union_with_rect (clip_region, &rect);
761 g_free (pixel_ranges);
769 * gdk_pango_layout_get_clip_region:
770 * @layout: a #PangoLayout
771 * @x_origin: X pixel where you intend to draw the layout with this clip
772 * @y_origin: Y pixel where you intend to draw the layout with this clip
773 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
774 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
776 * Obtains a clip region which contains the areas where the given ranges
777 * of text would be drawn. @x_origin and @y_origin are the same position
778 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
779 * ranges of bytes in the layout's text.
781 * Return value: a clip region containing the given ranges
784 gdk_pango_layout_get_clip_region (PangoLayout *layout,
790 PangoLayoutIter *iter;
791 GdkRegion *clip_region;
793 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
794 g_return_val_if_fail (index_ranges != NULL, NULL);
796 clip_region = gdk_region_new ();
798 iter = pango_layout_get_iter (layout);
802 PangoRectangle logical_rect;
803 PangoLayoutLine *line;
804 GdkRegion *line_region;
807 line = pango_layout_iter_get_line (iter);
809 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
810 baseline = pango_layout_iter_get_baseline (iter);
812 line_region = gdk_pango_layout_line_get_clip_region (line,
813 x_origin + logical_rect.x / PANGO_SCALE,
814 y_origin + baseline / PANGO_SCALE,
818 gdk_region_union (clip_region, line_region);
819 gdk_region_destroy (line_region);
821 while (pango_layout_iter_next_line (iter));
823 pango_layout_iter_free (iter);
829 * gdk_pango_context_get:
831 * Creates a #PangoContext for the default GDK screen.
833 * The context must be freed when you're finished with it.
835 * When using GTK+, normally you should use gtk_widget_get_pango_context()
836 * instead of this function, to get the appropriate context for
837 * the widget you intend to render text onto.
839 * Return value: a new #PangoContext for the default display
842 gdk_pango_context_get (void)
844 return gdk_pango_context_get_for_screen (gdk_screen_get_default ());