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)
55 gdk_colormap_unref (info->colormap);
59 static GdkPangoContextInfo *
60 gdk_pango_context_get_info (PangoContext *context, gboolean create)
62 GdkPangoContextInfo *info =
63 g_object_get_qdata (G_OBJECT (context),
64 g_quark_try_string (GDK_INFO_KEY));
67 info = g_new (GdkPangoContextInfo, 1);
68 info->colormap = NULL;
70 g_object_set_qdata_full (G_OBJECT (context),
71 g_quark_from_static_string (GDK_INFO_KEY),
72 info, (GDestroyNotify)gdk_pango_context_destroy);
79 gdk_pango_get_gc (PangoContext *context,
85 GdkPangoContextInfo *info;
87 g_return_val_if_fail (context != NULL, NULL);
89 info = gdk_pango_context_get_info (context, FALSE);
91 if (info == NULL || info->colormap == NULL)
93 g_warning ("you must set the colormap on a PangoContext before using it to draw a layout");
97 result = gdk_gc_new (gdk_parent_root);
98 gdk_gc_copy (result, base_gc);
104 color.red = fg_color->red;
105 color.green = fg_color->green;
106 color.blue = fg_color->blue;
108 gdk_rgb_find_color (info->colormap, &color);
109 gdk_gc_set_foreground (result, &color);
114 gdk_gc_set_fill (result, GDK_STIPPLED);
115 gdk_gc_set_stipple (result, stipple);
122 gdk_pango_free_gc (PangoContext *context,
129 gdk_pango_context_set_colormap (PangoContext *context,
130 GdkColormap *colormap)
132 GdkPangoContextInfo *info;
134 g_return_if_fail (context != NULL);
136 info = gdk_pango_context_get_info (context, TRUE);
137 g_return_if_fail (info != NULL);
139 if (info->colormap != colormap)
142 gdk_colormap_unref (info->colormap);
144 info->colormap = colormap;
147 gdk_colormap_ref (info->colormap);
152 * gdk_draw_layout_line_with_colors:
153 * @drawable: the drawable on which to draw the line
154 * @gc: base graphics to use
155 * @x: the x position of start of string (in pixels)
156 * @y: the y position of baseline (in pixels)
157 * @line: a #PangoLayoutLine
158 * @foreground: foreground override color, or %NULL for none
159 * @background: background override color, or %NULL for none
161 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
162 * layout's normal colors with @foreground and/or @background.
163 * @foreground and @background need not be allocated.
166 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
170 PangoLayoutLine *line,
171 GdkColor *foreground,
172 GdkColor *background)
174 GSList *tmp_list = line->runs;
175 PangoRectangle overall_rect;
176 PangoRectangle logical_rect;
177 PangoRectangle ink_rect;
178 PangoContext *context;
184 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
185 g_return_if_fail (GDK_IS_GC (gc));
186 g_return_if_fail (line != NULL);
188 context = pango_layout_get_context (line->layout);
190 pango_layout_line_get_extents (line,NULL, &overall_rect);
194 PangoUnderline uline = PANGO_UNDERLINE_NONE;
195 PangoLayoutRun *run = tmp_list->data;
196 PangoColor fg_color, bg_color;
197 gboolean strike, fg_set, bg_set, shape_set;
201 tmp_list = tmp_list->next;
203 gdk_pango_get_item_properties (run->item, &uline,
214 /* we subtract the rise because X coordinates are upside down */
215 risen_y = y - rise / PANGO_SCALE;
219 if (uline == PANGO_UNDERLINE_NONE)
220 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
221 NULL, &logical_rect);
223 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
224 &ink_rect, &logical_rect);
227 if (bg_set || background)
230 PangoColor tmp = bg_color;
234 tmp.red = background->red;
235 tmp.blue = background->blue;
236 tmp.green = background->green;
239 bg_gc = gdk_pango_get_gc (context, &tmp, stipple, gc);
241 gdk_draw_rectangle (drawable, bg_gc, TRUE,
242 x + (x_off + logical_rect.x) / PANGO_SCALE,
243 risen_y + overall_rect.y / PANGO_SCALE,
244 logical_rect.width / PANGO_SCALE,
245 overall_rect.height / PANGO_SCALE);
248 gdk_gc_set_fill (bg_gc, GDK_SOLID);
250 gdk_pango_free_gc (context, bg_gc);
253 if (fg_set || stipple || foreground)
255 PangoColor tmp = fg_color;
259 tmp.red = foreground->red;
260 tmp.blue = foreground->blue;
261 tmp.green = foreground->green;
264 fg_gc = gdk_pango_get_gc (context, fg_set ? &tmp : NULL,
274 gx = x + x_off / PANGO_SCALE;
279 PangoColor color = { 65535, 65535, 65535 };
280 GdkGC *white_gc = gdk_pango_get_gc (context, &color, stipple, fg_gc);
281 gdk_draw_glyphs (drawable, white_gc, run->item->analysis.font,
285 gdk_pango_free_gc (context, white_gc);
288 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
295 case PANGO_UNDERLINE_NONE:
297 case PANGO_UNDERLINE_DOUBLE:
298 gdk_draw_line (drawable, fg_gc,
299 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
301 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
304 case PANGO_UNDERLINE_SINGLE:
305 gdk_draw_line (drawable, fg_gc,
306 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
308 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
311 case PANGO_UNDERLINE_LOW:
312 gdk_draw_line (drawable, fg_gc,
313 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
314 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
315 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
316 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
322 int centerline = logical_rect.y + logical_rect.height / 2;
324 gdk_draw_line (drawable, fg_gc,
325 x + (x_off + logical_rect.x) / PANGO_SCALE - 1,
326 risen_y + centerline / PANGO_SCALE,
327 x + (x_off + logical_rect.x + logical_rect.width) / PANGO_SCALE + 1,
328 risen_y + centerline / PANGO_SCALE);
332 gdk_pango_free_gc (context, fg_gc);
334 x_off += logical_rect.width;
340 * @drawable: the drawable on which to draw string
341 * @gc: base graphics context to use
342 * @x: the X position of the left of the layout (in pixels)
343 * @y: the Y position of the top of the layout (in pixels)
344 * @layout: a #PangoLayout
345 * @foreground: foreground override color, or %NULL for none
346 * @background: background override color, or %NULL for none
348 * Render a #PangoLayout onto a #GdkDrawable, overriding the
349 * layout's normal colors with @foreground and/or @background.
350 * @foreground and @background need not be allocated.
353 gdk_draw_layout_with_colors (GdkDrawable *drawable,
358 GdkColor *foreground,
359 GdkColor *background)
361 PangoLayoutIter *iter;
363 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
364 g_return_if_fail (GDK_IS_GC (gc));
365 g_return_if_fail (PANGO_IS_LAYOUT (layout));
367 iter = pango_layout_get_iter (layout);
371 PangoRectangle logical_rect;
372 PangoLayoutLine *line;
375 line = pango_layout_iter_get_line (iter);
377 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
378 baseline = pango_layout_iter_get_baseline (iter);
380 gdk_draw_layout_line_with_colors (drawable, gc,
381 x + logical_rect.x / PANGO_SCALE,
382 y + baseline / PANGO_SCALE,
387 while (pango_layout_iter_next_line (iter));
389 pango_layout_iter_free (iter);
393 * gdk_draw_layout_line:
394 * @drawable: the drawable on which to draw the line
395 * @gc: base graphics to use
396 * @x: the x position of start of string (in pixels)
397 * @y: the y position of baseline (in pixels)
398 * @line: a #PangoLayoutLine
400 * Render a #PangoLayoutLine onto an GDK drawable
403 gdk_draw_layout_line (GdkDrawable *drawable,
407 PangoLayoutLine *line)
409 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
410 g_return_if_fail (GDK_IS_GC (gc));
411 g_return_if_fail (line != NULL);
413 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
418 * @drawable: the drawable on which to draw string
419 * @gc: base graphics context to use
420 * @x: the X position of the left of the layout (in pixels)
421 * @y: the Y position of the top of the layout (in pixels)
422 * @layout: a #PangoLayout
424 * Render a #PangoLayout onto a GDK drawable
427 gdk_draw_layout (GdkDrawable *drawable,
433 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
434 g_return_if_fail (GDK_IS_GC (gc));
435 g_return_if_fail (PANGO_IS_LAYOUT (layout));
437 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
441 gdk_pango_get_item_properties (PangoItem *item,
442 PangoUnderline *uline,
443 gboolean *strikethrough,
445 PangoColor *fg_color,
447 PangoColor *bg_color,
452 PangoRectangle *ink_rect,
453 PangoRectangle *logical_rect)
455 GSList *tmp_list = item->analysis.extra_attrs;
458 *strikethrough = FALSE;
480 PangoAttribute *attr = tmp_list->data;
482 switch (attr->klass->type)
484 case PANGO_ATTR_UNDERLINE:
486 *uline = ((PangoAttrInt *)attr)->value;
489 case PANGO_ATTR_STRIKETHROUGH:
491 *strikethrough = ((PangoAttrInt *)attr)->value;
494 case PANGO_ATTR_FOREGROUND:
496 *fg_color = ((PangoAttrColor *)attr)->color;
502 case PANGO_ATTR_BACKGROUND:
504 *bg_color = ((PangoAttrColor *)attr)->color;
510 case PANGO_ATTR_SHAPE:
514 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
516 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
519 case PANGO_ATTR_RISE:
521 *rise = ((PangoAttrInt *)attr)->value;
525 /* stipple_type and embossed_type aren't necessarily
526 * initialized, but they are 0, which is an
527 * invalid type so won't occur.
529 if (stipple && attr->klass->type == gdk_pango_attr_stipple_type)
531 *stipple = ((GdkPangoAttrStipple*)attr)->stipple;
533 else if (embossed && attr->klass->type == gdk_pango_attr_embossed_type)
535 *embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
539 tmp_list = tmp_list->next;
544 static PangoAttribute *
545 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
547 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
549 return gdk_pango_attr_stipple_new (src->stipple);
553 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
555 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
558 g_object_unref (G_OBJECT (st->stipple));
564 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
565 const PangoAttribute *attr2)
567 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
568 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
570 return a->stipple == b->stipple;
574 * gdk_pango_attr_stipple_new:
575 * @stipple: a bitmap to be set as stipple
577 * Creates a new attribute containing a stipple bitmap to be used when
578 * rendering the text.
580 * Return value: new #PangoAttribute
584 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
586 GdkPangoAttrStipple *result;
588 static PangoAttrClass klass = {
590 gdk_pango_attr_stipple_copy,
591 gdk_pango_attr_stipple_destroy,
592 gdk_pango_attr_stipple_compare
596 klass.type = gdk_pango_attr_stipple_type =
597 pango_attr_type_register ("GdkPangoAttrStipple");
599 result = g_new (GdkPangoAttrStipple, 1);
600 result->attr.klass = &klass;
603 g_object_ref (stipple);
605 result->stipple = stipple;
607 return (PangoAttribute *)result;
610 static PangoAttribute *
611 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
613 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
615 return gdk_pango_attr_embossed_new (e->embossed);
619 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
625 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
626 const PangoAttribute *attr2)
628 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
629 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
631 return e1->embossed == e2->embossed;
635 * gdk_pango_attr_embossed_new:
636 * @embossed: a bitmap to be set as embossed
638 * Creates a new attribute containing a embossed bitmap to be used when
639 * rendering the text.
641 * Return value: new #PangoAttribute
645 gdk_pango_attr_embossed_new (gboolean embossed)
647 GdkPangoAttrEmbossed *result;
649 static PangoAttrClass klass = {
651 gdk_pango_attr_embossed_copy,
652 gdk_pango_attr_embossed_destroy,
653 gdk_pango_attr_embossed_compare
657 klass.type = gdk_pango_attr_embossed_type =
658 pango_attr_type_register ("GdkPangoAttrEmbossed");
660 result = g_new (GdkPangoAttrEmbossed, 1);
661 result->attr.klass = &klass;
662 result->embossed = embossed;
664 return (PangoAttribute *)result;
667 /* Get a clip region to draw only part of a layout. index_ranges
668 * contains alternating range starts/stops. The region is the
669 * region which contains the given ranges, i.e. if you draw with the
670 * region as clip, only the given ranges are drawn.
674 * gdk_pango_layout_line_get_clip_region:
675 * @line: a #PangoLayoutLine
676 * @x_origin: X pixel where you intend to draw the layout line with this clip
677 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
678 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
679 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
681 * Obtains a clip region which contains the areas where the given
682 * ranges of text would be drawn. @x_origin and @y_origin are the same
683 * position you would pass to gdk_draw_layout_line(). @index_ranges
684 * should contain ranges of bytes in the layout's text. The clip
685 * region will include space to the left or right of the line (to the
686 * layout bounding box) if you have indexes above or below the indexes
687 * contained inside the line. This is to draw the selection all the way
688 * to the side of the layout. However, the clip region is in line coordinates,
689 * not layout coordinates.
691 * Return value: a clip region containing the given ranges
694 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
700 GdkRegion *clip_region;
702 PangoRectangle logical_rect;
703 PangoLayoutIter *iter;
706 g_return_val_if_fail (line != NULL, NULL);
707 g_return_val_if_fail (index_ranges != NULL, NULL);
709 clip_region = gdk_region_new ();
711 iter = pango_layout_get_iter (line->layout);
712 while (pango_layout_iter_get_line (iter) != line)
713 pango_layout_iter_next_line (iter);
715 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
716 baseline = pango_layout_iter_get_baseline (iter);
721 gint *pixel_ranges = NULL;
722 gint n_pixel_ranges = 0;
725 /* Note that get_x_ranges returns layout coordinates
727 pango_layout_line_get_x_ranges (line,
730 &pixel_ranges, &n_pixel_ranges);
732 for (j=0; j < n_pixel_ranges; j++)
736 rect.x = x_origin + pixel_ranges[2*j] / PANGO_SCALE - logical_rect.x / PANGO_SCALE;
737 rect.y = y_origin - (baseline / PANGO_SCALE - logical_rect.y / PANGO_SCALE);
738 rect.width = (pixel_ranges[2*j + 1] - pixel_ranges[2*j]) / PANGO_SCALE;
739 rect.height = logical_rect.height / PANGO_SCALE;
741 gdk_region_union_with_rect (clip_region, &rect);
744 g_free (pixel_ranges);
752 * gdk_pango_layout_get_clip_region:
753 * @layout: a #PangoLayout
754 * @x_origin: X pixel where you intend to draw the layout with this clip
755 * @y_origin: Y pixel where you intend to draw the layout with this clip
756 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
757 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
759 * Obtains a clip region which contains the areas where the given ranges
760 * of text would be drawn. @x_origin and @y_origin are the same position
761 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
762 * ranges of bytes in the layout's text.
764 * Return value: a clip region containing the given ranges
767 gdk_pango_layout_get_clip_region (PangoLayout *layout,
773 PangoLayoutIter *iter;
774 GdkRegion *clip_region;
776 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
777 g_return_val_if_fail (index_ranges != NULL, NULL);
779 clip_region = gdk_region_new ();
781 iter = pango_layout_get_iter (layout);
785 PangoRectangle logical_rect;
786 PangoLayoutLine *line;
787 GdkRegion *line_region;
790 line = pango_layout_iter_get_line (iter);
792 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
793 baseline = pango_layout_iter_get_baseline (iter);
795 line_region = gdk_pango_layout_line_get_clip_region (line,
796 x_origin + logical_rect.x / PANGO_SCALE,
797 y_origin + baseline / PANGO_SCALE,
801 gdk_region_union (clip_region, line_region);
802 gdk_region_destroy (line_region);
804 while (pango_layout_iter_next_line (iter));
806 pango_layout_iter_free (iter);