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.
22 #include <pango/pangocairo.h>
26 #include "gdkinternals.h"
29 #include "gdkprivate.h"
30 #include "gdkscreen.h"
34 #define GDK_INFO_KEY "gdk-info"
36 /* We have various arrays indexed by render part; if PangoRenderPart
37 * is extended, we want to make sure not to overwrite the end of
40 #define MAX_RENDER_PART PANGO_RENDER_PART_STRIKETHROUGH
42 struct _GdkPangoRendererPrivate
46 /* GdkPangoRenderer specific state */
47 PangoColor override_color[MAX_RENDER_PART + 1];
48 gboolean override_color_set[MAX_RENDER_PART + 1];
50 GdkBitmap *stipple[MAX_RENDER_PART + 1];
51 PangoColor emboss_color;
55 PangoRenderPart last_part;
58 GdkDrawable *drawable;
64 static PangoAttrType gdk_pango_attr_stipple_type;
65 static PangoAttrType gdk_pango_attr_embossed_type;
66 static PangoAttrType gdk_pango_attr_emboss_color_type;
73 G_DEFINE_TYPE (GdkPangoRenderer, gdk_pango_renderer, PANGO_TYPE_RENDERER)
76 gdk_pango_renderer_finalize (GObject *object)
78 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (object);
79 GdkPangoRendererPrivate *priv = gdk_renderer->priv;
83 g_object_unref (priv->base_gc);
85 g_object_unref (priv->drawable);
87 for (i = 0; i <= MAX_RENDER_PART; i++)
89 g_object_unref (priv->stipple[i]);
91 G_OBJECT_CLASS (gdk_pango_renderer_parent_class)->finalize (object);
95 gdk_pango_renderer_constructor (GType type,
96 guint n_construct_properties,
97 GObjectConstructParam *construct_params)
100 GdkPangoRenderer *gdk_renderer;
102 object = (* G_OBJECT_CLASS (gdk_pango_renderer_parent_class)->constructor) (type,
103 n_construct_properties,
106 gdk_renderer = GDK_PANGO_RENDERER (object);
108 if (!gdk_renderer->priv->screen)
110 g_warning ("Screen must be specified at construct time for GdkPangoRenderer");
111 gdk_renderer->priv->screen = gdk_screen_get_default ();
117 /* Adjusts matrix and color for the renderer to draw the secondary
118 * "shadow" copy for embossed text */
120 emboss_context (GdkPangoRenderer *renderer, cairo_t *cr)
122 GdkPangoRendererPrivate *priv = renderer->priv;
123 cairo_matrix_t tmp_matrix;
124 double red, green, blue;
126 /* The gymnastics here to adjust the matrix are because we want
127 * to offset by +1,+1 in device-space, not in user-space,
128 * so we can't just draw the layout at x + 1, y + 1
130 cairo_get_matrix (cr, &tmp_matrix);
131 tmp_matrix.x0 += 1.0;
132 tmp_matrix.y0 += 1.0;
133 cairo_set_matrix (cr, &tmp_matrix);
135 red = (double) priv->emboss_color.red / 65535.;
136 green = (double) priv->emboss_color.green / 65535.;
137 blue = (double) priv->emboss_color.blue / 65535.;
139 cairo_set_source_rgb (cr, red, green, blue);
142 static inline gboolean
143 color_equal (const PangoColor *c1, const PangoColor *c2)
149 c1->red == c2->red &&
150 c1->green == c2->green &&
151 c1->blue == c2->blue)
158 get_cairo_context (GdkPangoRenderer *gdk_renderer,
159 PangoRenderPart part)
161 PangoRenderer *renderer = PANGO_RENDERER (gdk_renderer);
162 GdkPangoRendererPrivate *priv = gdk_renderer->priv;
166 const PangoMatrix *matrix;
168 priv->cr = gdk_cairo_create (priv->drawable);
170 matrix = pango_renderer_get_matrix (renderer);
173 cairo_matrix_t cairo_matrix;
175 cairo_matrix_init (&cairo_matrix,
176 matrix->xx, matrix->yx,
177 matrix->xy, matrix->yy,
178 matrix->x0, matrix->y0);
179 cairo_set_matrix (priv->cr, &cairo_matrix);
183 if (part != priv->last_part)
185 PangoColor *pango_color;
190 pango_color = pango_renderer_get_color (renderer, part);
192 if (priv->last_part != -1)
193 changed = priv->gc_changed ||
194 priv->stipple[priv->last_part] != priv->stipple[part] ||
195 !color_equal (pango_color,
196 pango_renderer_get_color (renderer, priv->last_part));
204 tmp_color.red = pango_color->red;
205 tmp_color.green = pango_color->green;
206 tmp_color.blue = pango_color->blue;
213 _gdk_gc_update_context (priv->base_gc,
220 priv->last_part = part;
221 priv->gc_changed = FALSE;
228 gdk_pango_renderer_draw_glyphs (PangoRenderer *renderer,
230 PangoGlyphString *glyphs,
234 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
235 GdkPangoRendererPrivate *priv = gdk_renderer->priv;
238 cr = get_cairo_context (gdk_renderer,
239 PANGO_RENDER_PART_FOREGROUND);
244 emboss_context (gdk_renderer, cr);
245 cairo_move_to (cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
246 pango_cairo_show_glyph_string (cr, font, glyphs);
250 cairo_move_to (cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
251 pango_cairo_show_glyph_string (cr, font, glyphs);
255 gdk_pango_renderer_draw_rectangle (PangoRenderer *renderer,
256 PangoRenderPart part,
262 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
263 GdkPangoRendererPrivate *priv = gdk_renderer->priv;
266 cr = get_cairo_context (gdk_renderer, part);
268 if (priv->embossed && part != PANGO_RENDER_PART_BACKGROUND)
271 emboss_context (gdk_renderer, cr);
273 (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
274 (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
281 (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
282 (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
287 gdk_pango_renderer_draw_error_underline (PangoRenderer *renderer,
293 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
294 GdkPangoRendererPrivate *priv = gdk_renderer->priv;
297 cr = get_cairo_context (gdk_renderer, PANGO_RENDER_PART_UNDERLINE);
302 emboss_context (gdk_renderer, cr);
303 pango_cairo_show_error_underline (cr,
304 (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
305 (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
309 pango_cairo_show_error_underline (cr,
310 (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
311 (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
315 gdk_pango_renderer_part_changed (PangoRenderer *renderer,
316 PangoRenderPart part)
318 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
320 if (gdk_renderer->priv->last_part == part)
321 gdk_renderer->priv->last_part = (PangoRenderPart)-1;
325 gdk_pango_renderer_begin (PangoRenderer *renderer)
327 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
328 GdkPangoRendererPrivate *priv = gdk_renderer->priv;
330 if (!priv->drawable || !priv->base_gc)
332 g_warning ("gdk_pango_renderer_set_drawable() and gdk_pango_renderer_set_drawable()"
333 "must be used to set the target drawable and GC before using the renderer\n");
338 gdk_pango_renderer_end (PangoRenderer *renderer)
340 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
341 GdkPangoRendererPrivate *priv = gdk_renderer->priv;
345 cairo_destroy (priv->cr);
348 priv->last_part = (PangoRenderPart)-1;
352 gdk_pango_renderer_prepare_run (PangoRenderer *renderer,
355 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
356 gboolean embossed = FALSE;
357 GdkBitmap *stipple = NULL;
358 gboolean changed = FALSE;
359 PangoColor emboss_color;
363 emboss_color.red = 0xffff;
364 emboss_color.green = 0xffff;
365 emboss_color.blue = 0xffff;
367 for (l = run->item->analysis.extra_attrs; l; l = l->next)
369 PangoAttribute *attr = l->data;
371 /* stipple_type and embossed_type aren't necessarily
372 * initialized, but they are 0, which is an
373 * invalid type so won't occur.
375 if (attr->klass->type == gdk_pango_attr_stipple_type)
377 stipple = ((GdkPangoAttrStipple*)attr)->stipple;
379 else if (attr->klass->type == gdk_pango_attr_embossed_type)
381 embossed = ((GdkPangoAttrEmbossed*)attr)->embossed;
383 else if (attr->klass->type == gdk_pango_attr_emboss_color_type)
385 emboss_color = ((GdkPangoAttrEmbossColor*)attr)->color;
389 gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_FOREGROUND, stipple);
390 gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_BACKGROUND, stipple);
391 gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_UNDERLINE, stipple);
392 gdk_pango_renderer_set_stipple (gdk_renderer, PANGO_RENDER_PART_STRIKETHROUGH, stipple);
394 if (embossed != gdk_renderer->priv->embossed)
396 gdk_renderer->priv->embossed = embossed;
400 if (!color_equal (&gdk_renderer->priv->emboss_color, &emboss_color))
402 gdk_renderer->priv->emboss_color = emboss_color;
407 pango_renderer_part_changed (renderer, PANGO_RENDER_PART_FOREGROUND);
409 PANGO_RENDERER_CLASS (gdk_pango_renderer_parent_class)->prepare_run (renderer, run);
411 for (i = 0; i <= MAX_RENDER_PART; i++)
413 if (gdk_renderer->priv->override_color_set[i])
414 pango_renderer_set_color (renderer, i, &gdk_renderer->priv->override_color[i]);
419 gdk_pango_renderer_set_property (GObject *object,
424 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (object);
429 gdk_renderer->priv->screen = g_value_get_object (value);
432 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
438 gdk_pango_renderer_get_property (GObject *object,
443 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (object);
448 g_value_set_object (value, gdk_renderer->priv->screen);
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457 gdk_pango_renderer_init (GdkPangoRenderer *renderer)
459 renderer->priv = G_TYPE_INSTANCE_GET_PRIVATE (renderer,
460 GDK_TYPE_PANGO_RENDERER,
461 GdkPangoRendererPrivate);
463 renderer->priv->last_part = (PangoRenderPart)-1;
464 renderer->priv->gc_changed = TRUE;
468 gdk_pango_renderer_class_init (GdkPangoRendererClass *klass)
470 GObjectClass *object_class = G_OBJECT_CLASS (klass);
472 PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
474 renderer_class->draw_glyphs = gdk_pango_renderer_draw_glyphs;
475 renderer_class->draw_rectangle = gdk_pango_renderer_draw_rectangle;
476 renderer_class->draw_error_underline = gdk_pango_renderer_draw_error_underline;
477 renderer_class->part_changed = gdk_pango_renderer_part_changed;
478 renderer_class->begin = gdk_pango_renderer_begin;
479 renderer_class->end = gdk_pango_renderer_end;
480 renderer_class->prepare_run = gdk_pango_renderer_prepare_run;
482 object_class->finalize = gdk_pango_renderer_finalize;
483 object_class->constructor = gdk_pango_renderer_constructor;
484 object_class->set_property = gdk_pango_renderer_set_property;
485 object_class->get_property = gdk_pango_renderer_get_property;
487 g_object_class_install_property (object_class,
489 g_param_spec_object ("screen",
491 P_("the GdkScreen for the renderer"),
493 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
494 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
495 G_PARAM_STATIC_BLURB));
497 g_type_class_add_private (object_class, sizeof (GdkPangoRendererPrivate));
501 * gdk_pango_renderer_new:
502 * @screen: a #GdkScreen
504 * Creates a new #PangoRenderer for @screen. Normally you can use the
505 * results of gdk_pango_renderer_get_default() rather than creating a new
508 * Return value: a newly created #PangoRenderer. Free with g_object_unref().
513 gdk_pango_renderer_new (GdkScreen *screen)
515 g_return_val_if_fail (screen != NULL, NULL);
517 return g_object_new (GDK_TYPE_PANGO_RENDERER,
523 on_renderer_display_closed (GdkDisplay *display,
525 GdkPangoRenderer *renderer)
527 g_signal_handlers_disconnect_by_func (display,
528 on_renderer_display_closed,
530 g_object_set_data (G_OBJECT (renderer->priv->screen),
531 g_intern_static_string ("gdk-pango-renderer"), NULL);
535 * gdk_pango_renderer_get_default:
536 * @screen: a #GdkScreen
538 * Gets the default #PangoRenderer for a screen. This default renderer
539 * is shared by all users of the display, so properties such as the color
540 * or transformation matrix set for the renderer may be overwritten
541 * by functions such as gdk_draw_layout().
543 * Before using the renderer, you need to call gdk_pango_renderer_set_drawable()
544 * and gdk_pango_renderer_set_gc() to set the drawable and graphics context
545 * to use for drawing.
547 * Return value: the default #PangoRenderer for @screen. The
548 * renderer is owned by GTK+ and will be kept around until the
554 gdk_pango_renderer_get_default (GdkScreen *screen)
556 PangoRenderer *renderer;
558 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
560 renderer = g_object_get_data (G_OBJECT (screen), "gdk-pango-renderer");
563 renderer = gdk_pango_renderer_new (screen);
564 g_object_set_data_full (G_OBJECT (screen),
565 g_intern_static_string ("gdk-pango-renderer"), renderer,
566 (GDestroyNotify)g_object_unref);
568 g_signal_connect (gdk_screen_get_display (screen), "closed",
569 G_CALLBACK (on_renderer_display_closed), renderer);
576 * gdk_pango_renderer_set_drawable:
577 * @gdk_renderer: a #GdkPangoRenderer
578 * @drawable: the new target drawable, or %NULL
580 * Sets the drawable the renderer draws to.
585 gdk_pango_renderer_set_drawable (GdkPangoRenderer *gdk_renderer,
586 GdkDrawable *drawable)
588 GdkPangoRendererPrivate *priv;
590 g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
591 g_return_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable));
593 priv = gdk_renderer->priv;
595 if (priv->drawable != drawable)
598 g_object_unref (priv->drawable);
599 priv->drawable = drawable;
601 g_object_ref (priv->drawable);
606 * gdk_pango_renderer_set_gc:
607 * @gdk_renderer: a #GdkPangoRenderer
608 * @gc: the new GC to use for drawing, or %NULL
610 * Sets the GC the renderer draws with. Note that the GC must not be
611 * modified until it is unset by calling the function again with
612 * %NULL for the @gc parameter, since GDK may make internal copies
613 * of the GC which won't be updated to follow changes to the
619 gdk_pango_renderer_set_gc (GdkPangoRenderer *gdk_renderer,
622 GdkPangoRendererPrivate *priv;
624 g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
625 g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
627 priv = gdk_renderer->priv;
629 if (priv->base_gc != gc)
632 g_object_unref (priv->base_gc);
635 g_object_ref (priv->base_gc);
637 priv->gc_changed = TRUE;
643 * gdk_pango_renderer_set_stipple:
644 * @gdk_renderer: a #GdkPangoRenderer
645 * @part: the part to render with the stipple
646 * @stipple: the new stipple value.
648 * Sets the stipple for one render part (foreground, background, underline,
649 * etc.) Note that this is overwritten when iterating through the individual
650 * styled runs of a #PangoLayout or #PangoLayoutLine. This function is thus
651 * only useful when you call low level functions like pango_renderer_draw_glyphs()
652 * directly, or in the 'prepare_run' virtual function of a subclass of
658 gdk_pango_renderer_set_stipple (GdkPangoRenderer *gdk_renderer,
659 PangoRenderPart part,
662 g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
664 if (part > MAX_RENDER_PART) /* Silently ignore unknown parts */
667 if (stipple != gdk_renderer->priv->stipple[part])
669 if (gdk_renderer->priv->stipple[part])
670 g_object_unref (gdk_renderer->priv->stipple[part]);
672 gdk_renderer->priv->stipple[part] = stipple;
674 if (gdk_renderer->priv->stipple[part])
675 g_object_ref (gdk_renderer->priv->stipple[part]);
677 pango_renderer_part_changed (PANGO_RENDERER (gdk_renderer), part);
682 * gdk_pango_renderer_set_override_color:
683 * @gdk_renderer: a #GdkPangoRenderer
684 * @part: the part to render to set the color of
685 * @color: the color to use, or %NULL to unset a previously
686 * set override color.
688 * Sets the color for a particular render part (foreground,
689 * background, underline, etc.), overriding any attributes on the layouts
690 * renderered with this renderer.
695 gdk_pango_renderer_set_override_color (GdkPangoRenderer *gdk_renderer,
696 PangoRenderPart part,
697 const GdkColor *color)
699 GdkPangoRendererPrivate *priv;
701 g_return_if_fail (GDK_IS_PANGO_RENDERER (gdk_renderer));
703 priv = gdk_renderer->priv;
705 if (part > MAX_RENDER_PART) /* Silently ignore unknown parts */
710 priv->override_color[part].red = color->red;
711 priv->override_color[part].green = color->green;
712 priv->override_color[part].blue = color->blue;
713 priv->override_color_set[part] = TRUE;
716 priv->override_color_set[part] = FALSE;
720 * gdk_pango_context_set_colormap:
721 * @context: a #PangoContext
722 * @colormap: a #GdkColormap
724 * This function used to set the colormap to be used for drawing with
725 * @context. The colormap is now always derived from the graphics
726 * context used for drawing, so calling this function is no longer
730 gdk_pango_context_set_colormap (PangoContext *context,
731 GdkColormap *colormap)
733 g_return_if_fail (PANGO_IS_CONTEXT (context));
734 g_return_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap));
737 /* Gets a renderer to draw with, setting the properties of the
738 * renderer and activating it. Note that since we activate the
739 * renderer here, the implicit setting of the matrix that
740 * pango_renderer_draw_layout_[line] normally do when they
741 * activate the renderer is suppressed. */
742 static PangoRenderer *
743 get_renderer (GdkDrawable *drawable,
745 const GdkColor *foreground,
746 const GdkColor *background)
748 GdkScreen *screen = gdk_drawable_get_screen (drawable);
749 PangoRenderer *renderer = gdk_pango_renderer_get_default (screen);
750 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
752 gdk_pango_renderer_set_drawable (gdk_renderer, drawable);
753 gdk_pango_renderer_set_gc (gdk_renderer, gc);
755 gdk_pango_renderer_set_override_color (gdk_renderer,
756 PANGO_RENDER_PART_FOREGROUND,
758 gdk_pango_renderer_set_override_color (gdk_renderer,
759 PANGO_RENDER_PART_UNDERLINE,
761 gdk_pango_renderer_set_override_color (gdk_renderer,
762 PANGO_RENDER_PART_STRIKETHROUGH,
765 gdk_pango_renderer_set_override_color (gdk_renderer,
766 PANGO_RENDER_PART_BACKGROUND,
769 pango_renderer_activate (renderer);
774 /* Cleans up the renderer obtained with get_renderer() */
776 release_renderer (PangoRenderer *renderer)
778 GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
780 pango_renderer_deactivate (renderer);
782 gdk_pango_renderer_set_override_color (gdk_renderer,
783 PANGO_RENDER_PART_FOREGROUND,
785 gdk_pango_renderer_set_override_color (gdk_renderer,
786 PANGO_RENDER_PART_UNDERLINE,
788 gdk_pango_renderer_set_override_color (gdk_renderer,
789 PANGO_RENDER_PART_STRIKETHROUGH,
791 gdk_pango_renderer_set_override_color (gdk_renderer,
792 PANGO_RENDER_PART_BACKGROUND,
795 gdk_pango_renderer_set_drawable (gdk_renderer, NULL);
796 gdk_pango_renderer_set_gc (gdk_renderer, NULL);
800 * gdk_draw_layout_line_with_colors:
801 * @drawable: the drawable on which to draw the line
802 * @gc: base graphics to use
803 * @x: the x position of start of string (in pixels)
804 * @y: the y position of baseline (in pixels)
805 * @line: a #PangoLayoutLine
806 * @foreground: foreground override color, or %NULL for none
807 * @background: background override color, or %NULL for none
809 * Render a #PangoLayoutLine onto a #GdkDrawable, overriding the
810 * layout's normal colors with @foreground and/or @background.
811 * @foreground and @background need not be allocated.
813 * If the layout's #PangoContext has a transformation matrix set, then
814 * @x and @y specify the position of the left edge of the baseline
815 * (left is in before-tranform user coordinates) in after-transform
816 * device coordinates.
819 gdk_draw_layout_line_with_colors (GdkDrawable *drawable,
823 PangoLayoutLine *line,
824 const GdkColor *foreground,
825 const GdkColor *background)
827 PangoRenderer *renderer;
828 const PangoMatrix *matrix;
830 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
831 g_return_if_fail (GDK_IS_GC (gc));
832 g_return_if_fail (line != NULL);
834 renderer = get_renderer (drawable, gc, foreground, background);
836 /* When we have a matrix, we do positioning by adjusting the matrix, and
837 * clamp just pass x=0, y=0 to the lower levels. We don't want to introduce
838 * a matrix when the caller didn't provide one, however, since that adds
839 * lots of floating point arithmetic for each glyph.
841 matrix = pango_context_get_matrix (pango_layout_get_context (line->layout));
844 PangoMatrix tmp_matrix;
846 tmp_matrix = *matrix;
849 pango_renderer_set_matrix (renderer, &tmp_matrix);
855 pango_renderer_set_matrix (renderer, NULL);
857 pango_renderer_draw_layout_line (renderer, line, x * PANGO_SCALE, y * PANGO_SCALE);
859 release_renderer (renderer);
862 /* Gets the bounds of a layout in device coordinates. Note cut-and-paste
863 * between here and gtklabel.c */
865 get_rotated_layout_bounds (PangoLayout *layout,
868 PangoContext *context = pango_layout_get_context (layout);
869 const PangoMatrix *matrix = pango_context_get_matrix (context);
870 gdouble x_min = 0, x_max = 0, y_min = 0, y_max = 0; /* quiet gcc */
871 PangoRectangle logical_rect;
874 pango_layout_get_extents (layout, NULL, &logical_rect);
876 for (i = 0; i < 2; i++)
878 gdouble x = (i == 0) ? logical_rect.x : logical_rect.x + logical_rect.width;
879 for (j = 0; j < 2; j++)
881 gdouble y = (j == 0) ? logical_rect.y : logical_rect.y + logical_rect.height;
883 gdouble xt = (x * matrix->xx + y * matrix->xy) / PANGO_SCALE + matrix->x0;
884 gdouble yt = (x * matrix->yx + y * matrix->yy) / PANGO_SCALE + matrix->y0;
886 if (i == 0 && j == 0)
905 rect->x = floor (x_min);
906 rect->width = ceil (x_max) - rect->x;
907 rect->y = floor (y_min);
908 rect->height = floor (y_max) - rect->y;
912 * gdk_draw_layout_with_colors:
913 * @drawable: the drawable on which to draw string
914 * @gc: base graphics context to use
915 * @x: the X position of the left of the layout (in pixels)
916 * @y: the Y position of the top of the layout (in pixels)
917 * @layout: a #PangoLayout
918 * @foreground: foreground override color, or %NULL for none
919 * @background: background override color, or %NULL for none
921 * Render a #PangoLayout onto a #GdkDrawable, overriding the
922 * layout's normal colors with @foreground and/or @background.
923 * @foreground and @background need not be allocated.
925 * If the layout's #PangoContext has a transformation matrix set, then
926 * @x and @y specify the position of the top left corner of the
927 * bounding box (in device space) of the transformed layout.
929 * If you're using GTK+, the ususal way to obtain a #PangoLayout
930 * is gtk_widget_create_pango_layout().
933 gdk_draw_layout_with_colors (GdkDrawable *drawable,
938 const GdkColor *foreground,
939 const GdkColor *background)
941 PangoRenderer *renderer;
942 const PangoMatrix *matrix;
944 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
945 g_return_if_fail (GDK_IS_GC (gc));
946 g_return_if_fail (PANGO_IS_LAYOUT (layout));
948 renderer = get_renderer (drawable, gc, foreground, background);
950 /* When we have a matrix, we do positioning by adjusting the matrix, and
951 * clamp just pass x=0, y=0 to the lower levels. We don't want to introduce
952 * a matrix when the caller didn't provide one, however, since that adds
953 * lots of floating point arithmetic for each glyph.
955 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
958 PangoMatrix tmp_matrix;
961 get_rotated_layout_bounds (layout, &rect);
963 tmp_matrix = *matrix;
964 tmp_matrix.x0 += x - rect.x;
965 tmp_matrix.y0 += y - rect.y;
966 pango_renderer_set_matrix (renderer, &tmp_matrix);
972 pango_renderer_set_matrix (renderer, NULL);
974 pango_renderer_draw_layout (renderer, layout, x * PANGO_SCALE, y * PANGO_SCALE);
976 release_renderer (renderer);
980 * gdk_draw_layout_line:
981 * @drawable: the drawable on which to draw the line
982 * @gc: base graphics to use
983 * @x: the x position of start of string (in pixels)
984 * @y: the y position of baseline (in pixels)
985 * @line: a #PangoLayoutLine
987 * Render a #PangoLayoutLine onto an GDK drawable
989 * If the layout's #PangoContext has a transformation matrix set, then
990 * @x and @y specify the position of the left edge of the baseline
991 * (left is in before-tranform user coordinates) in after-transform
992 * device coordinates.
995 gdk_draw_layout_line (GdkDrawable *drawable,
999 PangoLayoutLine *line)
1001 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
1002 g_return_if_fail (GDK_IS_GC (gc));
1003 g_return_if_fail (line != NULL);
1005 gdk_draw_layout_line_with_colors (drawable, gc, x, y, line, NULL, NULL);
1010 * @drawable: the drawable on which to draw string
1011 * @gc: base graphics context to use
1012 * @x: the X position of the left of the layout (in pixels)
1013 * @y: the Y position of the top of the layout (in pixels)
1014 * @layout: a #PangoLayout
1016 * Render a #PangoLayout onto a GDK drawable
1018 * If the layout's #PangoContext has a transformation matrix set, then
1019 * @x and @y specify the position of the top left corner of the
1020 * bounding box (in device space) of the transformed layout.
1022 * If you're using GTK+, the usual way to obtain a #PangoLayout
1023 * is gtk_widget_create_pango_layout().
1026 gdk_draw_layout (GdkDrawable *drawable,
1030 PangoLayout *layout)
1032 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
1033 g_return_if_fail (GDK_IS_GC (gc));
1034 g_return_if_fail (PANGO_IS_LAYOUT (layout));
1036 gdk_draw_layout_with_colors (drawable, gc, x, y, layout, NULL, NULL);
1039 /* GdkPangoAttrStipple */
1041 static PangoAttribute *
1042 gdk_pango_attr_stipple_copy (const PangoAttribute *attr)
1044 const GdkPangoAttrStipple *src = (const GdkPangoAttrStipple*) attr;
1046 return gdk_pango_attr_stipple_new (src->stipple);
1050 gdk_pango_attr_stipple_destroy (PangoAttribute *attr)
1052 GdkPangoAttrStipple *st = (GdkPangoAttrStipple*) attr;
1055 g_object_unref (st->stipple);
1061 gdk_pango_attr_stipple_compare (const PangoAttribute *attr1,
1062 const PangoAttribute *attr2)
1064 const GdkPangoAttrStipple *a = (const GdkPangoAttrStipple*) attr1;
1065 const GdkPangoAttrStipple *b = (const GdkPangoAttrStipple*) attr2;
1067 return a->stipple == b->stipple;
1071 * gdk_pango_attr_stipple_new:
1072 * @stipple: a bitmap to be set as stipple
1074 * Creates a new attribute containing a stipple bitmap to be used when
1075 * rendering the text.
1077 * Return value: new #PangoAttribute
1081 gdk_pango_attr_stipple_new (GdkBitmap *stipple)
1083 GdkPangoAttrStipple *result;
1085 static PangoAttrClass klass = {
1087 gdk_pango_attr_stipple_copy,
1088 gdk_pango_attr_stipple_destroy,
1089 gdk_pango_attr_stipple_compare
1093 klass.type = gdk_pango_attr_stipple_type =
1094 pango_attr_type_register ("GdkPangoAttrStipple");
1096 result = g_new (GdkPangoAttrStipple, 1);
1097 result->attr.klass = &klass;
1100 g_object_ref (stipple);
1102 result->stipple = stipple;
1104 return (PangoAttribute *)result;
1107 /* GdkPangoAttrEmbossed */
1109 static PangoAttribute *
1110 gdk_pango_attr_embossed_copy (const PangoAttribute *attr)
1112 const GdkPangoAttrEmbossed *e = (const GdkPangoAttrEmbossed*) attr;
1114 return gdk_pango_attr_embossed_new (e->embossed);
1118 gdk_pango_attr_embossed_destroy (PangoAttribute *attr)
1124 gdk_pango_attr_embossed_compare (const PangoAttribute *attr1,
1125 const PangoAttribute *attr2)
1127 const GdkPangoAttrEmbossed *e1 = (const GdkPangoAttrEmbossed*) attr1;
1128 const GdkPangoAttrEmbossed *e2 = (const GdkPangoAttrEmbossed*) attr2;
1130 return e1->embossed == e2->embossed;
1134 * gdk_pango_attr_embossed_new:
1135 * @embossed: if the region should be embossed
1137 * Creates a new attribute flagging a region as embossed or not.
1139 * Return value: new #PangoAttribute
1143 gdk_pango_attr_embossed_new (gboolean embossed)
1145 GdkPangoAttrEmbossed *result;
1147 static PangoAttrClass klass = {
1149 gdk_pango_attr_embossed_copy,
1150 gdk_pango_attr_embossed_destroy,
1151 gdk_pango_attr_embossed_compare
1155 klass.type = gdk_pango_attr_embossed_type =
1156 pango_attr_type_register ("GdkPangoAttrEmbossed");
1158 result = g_new (GdkPangoAttrEmbossed, 1);
1159 result->attr.klass = &klass;
1160 result->embossed = embossed;
1162 return (PangoAttribute *)result;
1165 /* GdkPangoAttrEmbossColor */
1167 static PangoAttribute *
1168 gdk_pango_attr_emboss_color_copy (const PangoAttribute *attr)
1170 const GdkPangoAttrEmbossColor *old = (const GdkPangoAttrEmbossColor*) attr;
1171 GdkPangoAttrEmbossColor *copy;
1173 copy = g_new (GdkPangoAttrEmbossColor, 1);
1174 copy->attr.klass = old->attr.klass;
1175 copy->color = old->color;
1177 return (PangoAttribute *) copy;
1181 gdk_pango_attr_emboss_color_destroy (PangoAttribute *attr)
1187 gdk_pango_attr_emboss_color_compare (const PangoAttribute *attr1,
1188 const PangoAttribute *attr2)
1190 const GdkPangoAttrEmbossColor *c1 = (const GdkPangoAttrEmbossColor*) attr1;
1191 const GdkPangoAttrEmbossColor *c2 = (const GdkPangoAttrEmbossColor*) attr2;
1193 return color_equal (&c1->color, &c2->color);
1197 * gdk_pango_attr_emboss_color_new:
1198 * @color: a GdkColor representing the color to emboss with
1200 * Creates a new attribute specifying the color to emboss text with.
1202 * Return value: new #PangoAttribute
1207 gdk_pango_attr_emboss_color_new (const GdkColor *color)
1209 GdkPangoAttrEmbossColor *result;
1211 static PangoAttrClass klass = {
1213 gdk_pango_attr_emboss_color_copy,
1214 gdk_pango_attr_emboss_color_destroy,
1215 gdk_pango_attr_emboss_color_compare
1219 klass.type = gdk_pango_attr_emboss_color_type =
1220 pango_attr_type_register ("GdkPangoAttrEmbossColor");
1222 result = g_new (GdkPangoAttrEmbossColor, 1);
1223 result->attr.klass = &klass;
1224 result->color.red = color->red;
1225 result->color.green = color->green;
1226 result->color.blue = color->blue;
1228 return (PangoAttribute *) result;
1231 /* Get a clip region to draw only part of a layout. index_ranges
1232 * contains alternating range starts/stops. The region is the
1233 * region which contains the given ranges, i.e. if you draw with the
1234 * region as clip, only the given ranges are drawn.
1237 layout_iter_get_line_clip_region (PangoLayoutIter *iter,
1243 PangoLayoutLine *line;
1244 GdkRegion *clip_region;
1245 PangoRectangle logical_rect;
1249 line = pango_layout_iter_get_line (iter);
1251 clip_region = gdk_region_new ();
1253 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1254 baseline = pango_layout_iter_get_baseline (iter);
1257 while (i < n_ranges)
1259 gint *pixel_ranges = NULL;
1260 gint n_pixel_ranges = 0;
1263 /* Note that get_x_ranges returns layout coordinates
1265 if (index_ranges[i*2+1] >= line->start_index &&
1266 index_ranges[i*2] < line->start_index + line->length)
1267 pango_layout_line_get_x_ranges (line,
1269 index_ranges[i*2+1],
1270 &pixel_ranges, &n_pixel_ranges);
1272 for (j = 0; j < n_pixel_ranges; j++)
1277 x_off = PANGO_PIXELS (pixel_ranges[2*j] - logical_rect.x);
1278 y_off = PANGO_PIXELS (baseline - logical_rect.y);
1280 rect.x = x_origin + x_off;
1281 rect.y = y_origin - y_off;
1282 rect.width = PANGO_PIXELS (pixel_ranges[2*j + 1] - logical_rect.x) - x_off;
1283 rect.height = PANGO_PIXELS (baseline - logical_rect.y + logical_rect.height) - y_off;
1285 gdk_region_union_with_rect (clip_region, &rect);
1288 g_free (pixel_ranges);
1295 * gdk_pango_layout_line_get_clip_region:
1296 * @line: a #PangoLayoutLine
1297 * @x_origin: X pixel where you intend to draw the layout line with this clip
1298 * @y_origin: baseline pixel where you intend to draw the layout line with this clip
1299 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
1300 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
1302 * Obtains a clip region which contains the areas where the given
1303 * ranges of text would be drawn. @x_origin and @y_origin are the same
1304 * position you would pass to gdk_draw_layout_line(). @index_ranges
1305 * should contain ranges of bytes in the layout's text. The clip
1306 * region will include space to the left or right of the line (to the
1307 * layout bounding box) if you have indexes above or below the indexes
1308 * contained inside the line. This is to draw the selection all the way
1309 * to the side of the layout. However, the clip region is in line coordinates,
1310 * not layout coordinates.
1312 * Note that the regions returned correspond to logical extents of the text
1313 * ranges, not ink extents. So the drawn line may in fact touch areas out of
1314 * the clip region. The clip region is mainly useful for highlightling parts
1315 * of text, such as when text is selected.
1317 * Return value: a clip region containing the given ranges
1320 gdk_pango_layout_line_get_clip_region (PangoLayoutLine *line,
1326 GdkRegion *clip_region;
1327 PangoLayoutIter *iter;
1329 g_return_val_if_fail (line != NULL, NULL);
1330 g_return_val_if_fail (index_ranges != NULL, NULL);
1332 iter = pango_layout_get_iter (line->layout);
1333 while (pango_layout_iter_get_line (iter) != line)
1334 pango_layout_iter_next_line (iter);
1336 clip_region = layout_iter_get_line_clip_region(iter, x_origin, y_origin, index_ranges, n_ranges);
1338 pango_layout_iter_free (iter);
1344 * gdk_pango_layout_get_clip_region:
1345 * @layout: a #PangoLayout
1346 * @x_origin: X pixel where you intend to draw the layout with this clip
1347 * @y_origin: Y pixel where you intend to draw the layout with this clip
1348 * @index_ranges: array of byte indexes into the layout, where even members of array are start indexes and odd elements are end indexes
1349 * @n_ranges: number of ranges in @index_ranges, i.e. half the size of @index_ranges
1351 * Obtains a clip region which contains the areas where the given ranges
1352 * of text would be drawn. @x_origin and @y_origin are the same position
1353 * you would pass to gdk_draw_layout_line(). @index_ranges should contain
1354 * ranges of bytes in the layout's text.
1356 * Note that the regions returned correspond to logical extents of the text
1357 * ranges, not ink extents. So the drawn layout may in fact touch areas out of
1358 * the clip region. The clip region is mainly useful for highlightling parts
1359 * of text, such as when text is selected.
1361 * Return value: a clip region containing the given ranges
1364 gdk_pango_layout_get_clip_region (PangoLayout *layout,
1370 PangoLayoutIter *iter;
1371 GdkRegion *clip_region;
1373 g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
1374 g_return_val_if_fail (index_ranges != NULL, NULL);
1376 clip_region = gdk_region_new ();
1378 iter = pango_layout_get_iter (layout);
1382 PangoRectangle logical_rect;
1383 GdkRegion *line_region;
1386 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1387 baseline = pango_layout_iter_get_baseline (iter);
1389 line_region = layout_iter_get_line_clip_region(iter,
1390 x_origin + logical_rect.x / PANGO_SCALE,
1391 y_origin + baseline / PANGO_SCALE,
1395 gdk_region_union (clip_region, line_region);
1396 gdk_region_destroy (line_region);
1398 while (pango_layout_iter_next_line (iter));
1400 pango_layout_iter_free (iter);
1406 * gdk_pango_context_get:
1408 * Creates a #PangoContext for the default GDK screen.
1410 * The context must be freed when you're finished with it.
1412 * When using GTK+, normally you should use gtk_widget_get_pango_context()
1413 * instead of this function, to get the appropriate context for
1414 * the widget you intend to render text onto.
1416 * The newly created context will have the default font options (see
1417 * #cairo_font_options_t) for the default screen; if these options
1418 * change it will not be updated. Using gtk_widget_get_pango_context()
1419 * is more convenient if you want to keep a context around and track
1420 * changes to the screen's font rendering settings.
1422 * Return value: a new #PangoContext for the default display
1425 gdk_pango_context_get (void)
1427 return gdk_pango_context_get_for_screen (gdk_screen_get_default ());
1431 * gdk_pango_context_get_for_screen:
1432 * @screen: the #GdkScreen for which the context is to be created.
1434 * Creates a #PangoContext for @screen.
1436 * The context must be freed when you're finished with it.
1438 * When using GTK+, normally you should use gtk_widget_get_pango_context()
1439 * instead of this function, to get the appropriate context for
1440 * the widget you intend to render text onto.
1442 * The newly created context will have the default font options
1443 * (see #cairo_font_options_t) for the screen; if these options
1444 * change it will not be updated. Using gtk_widget_get_pango_context()
1445 * is more convenient if you want to keep a context around and track
1446 * changes to the screen's font rendering settings.
1448 * Return value: a new #PangoContext for @screen
1453 gdk_pango_context_get_for_screen (GdkScreen *screen)
1455 PangoFontMap *fontmap;
1456 PangoContext *context;
1457 const cairo_font_options_t *options;
1460 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1462 fontmap = pango_cairo_font_map_get_default ();
1464 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
1466 options = gdk_screen_get_font_options (screen);
1467 pango_cairo_context_set_font_options (context, options);
1469 dpi = gdk_screen_get_resolution (screen);
1470 pango_cairo_context_set_resolution (context, dpi);
1475 #define __GDK_PANGO_C__
1476 #include "gdkaliasdef.c"