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,
48 PangoRectangle *ink_rect,
49 PangoRectangle *logical_rect);
52 gdk_pango_context_destroy (GdkPangoContextInfo *info)
54 gdk_colormap_unref (info->colormap);
58 static GdkPangoContextInfo *
59 gdk_pango_context_get_info (PangoContext *context, gboolean create)
61 GdkPangoContextInfo *info =
62 g_object_get_qdata (G_OBJECT (context),
63 g_quark_try_string (GDK_INFO_KEY));
66 info = g_new (GdkPangoContextInfo, 1);
67 info->colormap = NULL;
69 g_object_set_qdata_full (G_OBJECT (context),
70 g_quark_from_static_string (GDK_INFO_KEY),
71 info, (GDestroyNotify)gdk_pango_context_destroy);
78 gdk_pango_get_gc (PangoContext *context,
84 GdkPangoContextInfo *info;
86 g_return_val_if_fail (context != NULL, NULL);
88 info = gdk_pango_context_get_info (context, FALSE);
90 if (info == NULL || info->colormap == NULL)
92 g_warning ("you must set the colormap on a PangoContext before using it to draw a layout");
96 result = gdk_gc_new (gdk_parent_root);
97 gdk_gc_copy (result, base_gc);
103 color.red = fg_color->red;
104 color.green = fg_color->green;
105 color.blue = fg_color->blue;
107 gdk_rgb_find_color (info->colormap, &color);
108 gdk_gc_set_foreground (result, &color);
113 gdk_gc_set_fill (result, GDK_STIPPLED);
114 gdk_gc_set_stipple (result, stipple);
121 gdk_pango_free_gc (PangoContext *context,
128 gdk_pango_context_set_colormap (PangoContext *context,
129 GdkColormap *colormap)
131 GdkPangoContextInfo *info;
133 g_return_if_fail (context != NULL);
135 info = gdk_pango_context_get_info (context, TRUE);
136 g_return_if_fail (info != NULL);
138 if (info->colormap != colormap)
141 gdk_colormap_unref (info->colormap);
143 info->colormap = colormap;
146 gdk_colormap_ref (info->colormap);
151 * gdk_draw_layout_line_with_colors:
152 * @drawable: the drawable on which to draw the line
153 * @gc: base graphics to use
154 * @x: the x position of start of string (in pixels)
155 * @y: the y position of baseline (in pixels)
156 * @line: a #PangoLayoutLine
157 * @foreground: foreground override color, or %NULL for none
158 * @background: background override color, or %NULL for none
160 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
161 * layout's normal colors with @foreground and/or @background.
162 * @foreground and @background need not be allocated.
165 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
169 PangoLayoutLine *line,
170 GdkColor *foreground,
171 GdkColor *background)
173 GSList *tmp_list = line->runs;
174 PangoRectangle overall_rect;
175 PangoRectangle logical_rect;
176 PangoRectangle ink_rect;
177 PangoContext *context;
183 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
184 g_return_if_fail (GDK_IS_GC (gc));
185 g_return_if_fail (line != NULL);
187 context = pango_layout_get_context (line->layout);
189 pango_layout_line_get_extents (line,NULL, &overall_rect);
193 PangoUnderline uline = PANGO_UNDERLINE_NONE;
194 PangoLayoutRun *run = tmp_list->data;
195 PangoColor fg_color, bg_color;
196 gboolean fg_set, bg_set, shape_set;
200 tmp_list = tmp_list->next;
202 gdk_pango_get_item_properties (run->item, &uline,
212 /* we subtract the rise because X coordinates are upside down */
213 risen_y = y - rise / PANGO_SCALE;
217 if (uline == PANGO_UNDERLINE_NONE)
218 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
219 NULL, &logical_rect);
221 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
222 &ink_rect, &logical_rect);
225 if (bg_set || background)
228 PangoColor tmp = bg_color;
232 tmp.red = background->red;
233 tmp.blue = background->blue;
234 tmp.green = background->green;
237 bg_gc = gdk_pango_get_gc (context, &tmp, stipple, gc);
239 gdk_draw_rectangle (drawable, bg_gc, TRUE,
240 x + (x_off + logical_rect.x) / PANGO_SCALE,
241 risen_y + overall_rect.y / PANGO_SCALE,
242 logical_rect.width / PANGO_SCALE,
243 overall_rect.height / PANGO_SCALE);
246 gdk_gc_set_fill (bg_gc, GDK_SOLID);
248 gdk_pango_free_gc (context, bg_gc);
251 if (fg_set || stipple || foreground)
253 PangoColor tmp = fg_color;
257 tmp.red = foreground->red;
258 tmp.blue = foreground->blue;
259 tmp.green = foreground->green;
262 fg_gc = gdk_pango_get_gc (context, fg_set ? &tmp : NULL,
272 gx = x + x_off / PANGO_SCALE;
277 PangoColor color = { 65535, 65535, 65535 };
278 GdkGC *white_gc = gdk_pango_get_gc (context, &color, stipple, fg_gc);
279 gdk_draw_glyphs (drawable, white_gc, run->item->analysis.font,
283 gdk_pango_free_gc (context, white_gc);
286 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
293 case PANGO_UNDERLINE_NONE:
295 case PANGO_UNDERLINE_DOUBLE:
296 gdk_draw_line (drawable, fg_gc,
297 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
299 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
302 case PANGO_UNDERLINE_SINGLE:
303 gdk_draw_line (drawable, fg_gc,
304 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
306 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
309 case PANGO_UNDERLINE_LOW:
310 gdk_draw_line (drawable, fg_gc,
311 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
312 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
313 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
314 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
319 gdk_pango_free_gc (context, fg_gc);
321 x_off += logical_rect.width;
327 * @drawable: the drawable on which to draw string
328 * @gc: base graphics context to use
329 * @x: the X position of the left of the layout (in pixels)
330 * @y: the Y position of the top of the layout (in pixels)
331 * @layout: a #PangoLayout
332 * @foreground: foreground override color, or %NULL for none
333 * @background: background override color, or %NULL for none
335 * Render a #PangoLayout onto a #GdkDrawable, overriding the
336 * layout's normal colors with @foreground and/or @background.
337 * @foreground and @background need not be allocated.
340 gdk_draw_layout_with_colors (GdkDrawable *drawable,
345 GdkColor *foreground,
346 GdkColor *background)
348 PangoLayoutIter *iter;
350 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
351 g_return_if_fail (GDK_IS_GC (gc));
352 g_return_if_fail (PANGO_IS_LAYOUT (layout));
354 iter = pango_layout_get_iter (layout);
358 PangoRectangle logical_rect;
359 PangoLayoutLine *line;
362 line = pango_layout_iter_get_line (iter);
364 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
365 baseline = pango_layout_iter_get_baseline (iter);
367 gdk_draw_layout_line_with_colors (drawable, gc,
368 x + logical_rect.x / PANGO_SCALE,
369 y + baseline / PANGO_SCALE,
374 while (pango_layout_iter_next_line (iter));
376 pango_layout_iter_free (iter);
380 * gdk_draw_layout_line:
381 * @drawable: the drawable on which to draw the line
382 * @gc: base graphics to use
383 * @x: the x position of start of string (in pixels)
384 * @y: the y position of baseline (in pixels)
385 * @line: a #PangoLayoutLine
387 * Render a #PangoLayoutLine onto an GDK drawable
390 gdk_draw_layout_line (GdkDrawable *drawable,
394 PangoLayoutLine *line)
396 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
397 g_return_if_fail (GDK_IS_GC (gc));
398 g_return_if_fail (line != NULL);
400 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
405 * @drawable: the drawable on which to draw string
406 * @gc: base graphics context to use
407 * @x: the X position of the left of the layout (in pixels)
408 * @y: the Y position of the top of the layout (in pixels)
409 * @layout: a #PangoLayout
411 * Render a #PangoLayout onto a GDK drawable
414 gdk_draw_layout (GdkDrawable *drawable,
420 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
421 g_return_if_fail (GDK_IS_GC (gc));
422 g_return_if_fail (PANGO_IS_LAYOUT (layout));
424 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
428 gdk_pango_get_item_properties (PangoItem *item,
429 PangoUnderline *uline,
431 PangoColor *fg_color,
433 PangoColor *bg_color,
438 PangoRectangle *ink_rect,
439 PangoRectangle *logical_rect)
441 GSList *tmp_list = item->extra_attrs;
463 PangoAttribute *attr = tmp_list->data;
465 switch (attr->klass->type)
467 case PANGO_ATTR_UNDERLINE:
469 *uline = ((PangoAttrInt *)attr)->value;
472 case PANGO_ATTR_FOREGROUND:
474 *fg_color = ((PangoAttrColor *)attr)->color;
480 case PANGO_ATTR_BACKGROUND:
482 *bg_color = ((PangoAttrColor *)attr)->color;
488 case PANGO_ATTR_SHAPE:
492 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
494 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
497 case PANGO_ATTR_RISE:
499 *rise = ((PangoAttrInt *)attr)->value;
503 /* stipple_type and embossed_type aren't necessarily
504 * initialized, but they are 0, which is an
505 * invalid type so won't occur.
507 if (stipple && attr->klass->type == gdk_pango_attr_stipple_type)
509 *stipple = ((GdkPangoAttrStipple*)attr)->stipple;
511 else if (embossed && attr->klass->type == gdk_pango_attr_embossed_type)
513 *embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
517 tmp_list = tmp_list->next;
522 static PangoAttribute *
523 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
525 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
527 return gdk_pango_attr_stipple_new (src->stipple);
531 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
533 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
536 g_object_unref (G_OBJECT (st->stipple));
542 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
543 const PangoAttribute *attr2)
545 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
546 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
548 return a->stipple == b->stipple;
552 * gdk_pango_attr_stipple_new:
553 * @stipple: a bitmap to be set as stipple
555 * Creates a new attribute containing a stipple bitmap to be used when
556 * rendering the text.
558 * Return value: new #PangoAttribute
562 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
564 GdkPangoAttrStipple *result;
566 static PangoAttrClass klass = {
568 gdk_pango_attr_stipple_copy,
569 gdk_pango_attr_stipple_destroy,
570 gdk_pango_attr_stipple_compare
574 klass.type = gdk_pango_attr_stipple_type =
575 pango_attr_type_register ("GdkPangoAttrStipple");
577 result = g_new (GdkPangoAttrStipple, 1);
578 result->attr.klass = &klass;
581 g_object_ref (stipple);
583 result->stipple = stipple;
585 return (PangoAttribute *)result;
588 static PangoAttribute *
589 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
591 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
593 return gdk_pango_attr_embossed_new (e->embossed);
597 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
603 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
604 const PangoAttribute *attr2)
606 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
607 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
609 return e1->embossed == e2->embossed;
613 * gdk_pango_attr_embossed_new:
614 * @embossed: a bitmap to be set as embossed
616 * Creates a new attribute containing a embossed bitmap to be used when
617 * rendering the text.
619 * Return value: new #PangoAttribute
623 gdk_pango_attr_embossed_new (gboolean embossed)
625 GdkPangoAttrEmbossed *result;
627 static PangoAttrClass klass = {
629 gdk_pango_attr_embossed_copy,
630 gdk_pango_attr_embossed_destroy,
631 gdk_pango_attr_embossed_compare
635 klass.type = gdk_pango_attr_embossed_type =
636 pango_attr_type_register ("GdkPangoAttrEmbossed");
638 result = g_new (GdkPangoAttrEmbossed, 1);
639 result->attr.klass = &klass;
640 result->embossed = embossed;
642 return (PangoAttribute *)result;
645 /* Get a clip region to draw only part of a layout. index_ranges
646 * contains alternating range starts/stops. The region is the
647 * region which contains the given ranges, i.e. if you draw with the
648 * region as clip, only the given ranges are drawn.
652 * gdk_pango_layout_line_get_clip_region:
653 * @line: a #PangoLayoutLine
654 * @x_origin: X pixel where you intend to draw the layout line with this clip
655 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
656 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
657 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
659 * Obtains a clip region which contains the areas where the given
660 * ranges of text would be drawn. @x_origin and @y_origin are the same
661 * position you would pass to gdk_draw_layout_line(). @index_ranges
662 * should contain ranges of bytes in the layout's text. The clip
663 * region will include space to the left or right of the line (to the
664 * layout bounding box) if you have indexes above or below the indexes
665 * contained inside the line. This is to draw the selection all the way
666 * to the side of the layout. However, the clip region is in line coordinates,
667 * not layout coordinates.
669 * Return value: a clip region containing the given ranges
672 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
678 GdkRegion *clip_region;
680 PangoRectangle logical_rect;
681 PangoLayoutIter *iter;
684 g_return_val_if_fail (line != NULL, NULL);
685 g_return_val_if_fail (index_ranges != NULL, NULL);
687 clip_region = gdk_region_new ();
689 iter = pango_layout_get_iter (line->layout);
690 while (pango_layout_iter_get_line (iter) != line)
691 pango_layout_iter_next_line (iter);
693 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
694 baseline = pango_layout_iter_get_baseline (iter);
699 gint *pixel_ranges = NULL;
700 gint n_pixel_ranges = 0;
703 /* Note that get_x_ranges returns layout coordinates
705 pango_layout_line_get_x_ranges (line,
708 &pixel_ranges, &n_pixel_ranges);
710 for (j=0; j < n_pixel_ranges; j++)
714 rect.x = x_origin + pixel_ranges[2*j] / PANGO_SCALE - logical_rect.x / PANGO_SCALE;
715 rect.y = y_origin - (baseline / PANGO_SCALE - logical_rect.y / PANGO_SCALE);
716 rect.width = (pixel_ranges[2*j + 1] - pixel_ranges[2*j]) / PANGO_SCALE;
717 rect.height = logical_rect.height / PANGO_SCALE;
719 gdk_region_union_with_rect (clip_region, &rect);
722 g_free (pixel_ranges);
730 * gdk_pango_layout_get_clip_region:
731 * @layout: a #PangoLayout
732 * @x_origin: X pixel where you intend to draw the layout with this clip
733 * @y_origin: Y pixel where you intend to draw the layout with this clip
734 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
735 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
737 * Obtains a clip region which contains the areas where the given ranges
738 * of text would be drawn. @x_origin and @y_origin are the same position
739 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
740 * ranges of bytes in the layout's text.
742 * Return value: a clip region containing the given ranges
745 gdk_pango_layout_get_clip_region (PangoLayout *layout,
751 PangoLayoutIter *iter;
752 GdkRegion *clip_region;
754 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
755 g_return_val_if_fail (index_ranges != NULL, NULL);
757 clip_region = gdk_region_new ();
759 iter = pango_layout_get_iter (layout);
763 PangoRectangle logical_rect;
764 PangoLayoutLine *line;
765 GdkRegion *line_region;
768 line = pango_layout_iter_get_line (iter);
770 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
771 baseline = pango_layout_iter_get_baseline (iter);
773 line_region = gdk_pango_layout_line_get_clip_region (line,
774 x_origin + logical_rect.x / PANGO_SCALE,
775 y_origin + baseline / PANGO_SCALE,
779 gdk_region_union (clip_region, line_region);
780 gdk_region_destroy (line_region);
782 while (pango_layout_iter_next_line (iter));
784 pango_layout_iter_free (iter);