1 2@c %start of fragment 3 4@node Pango Interaction 5@chapter Pango Interaction 6Using Pango in GDK 7 8@section Overview 9Pango is the text layout system used by GDK and GTK+. The functions and types in 10this section are used to render Pango objects to GDK. drawables, and also extend 11the set of Pango attributes to include stippling and embossing. 12 13Creating a @code{<pango-layout>} object is the first step in rendering text, and 14requires getting a handle to a @code{<pango-context>}. For GTK+ programs, you'll 15usually want to use @code{gtk-widget-get-pango-context}, or 16@code{gtk-widget-create-pango-layout}, rather than using the lowlevel 17@code{gdk-pango-context-get-for-screen}. Once you have a @code{<pango-layout>}, 18you can set the text and attributes of it with Pango functions like 19@code{pango-layout-set-text} and get its size with @code{pango-layout-get-size}. 20(Note that Pango uses a fixed point system internally, so converting between 21Pango units and pixels using PANGO_SCALE or the @code{pango-pixels} macro.) 22 23Rendering a Pango layout is done most simply with @code{gdk-draw-layout}; you 24can also draw pieces of the layout with @code{gdk-draw-layout} or 25@code{gdk-draw-glyphs}. @code{<gdk-pango-renderer>} is a subclass of 26@code{<pango-renderer>} that is used internally to implement these functions. 27Using it directly or subclassing it can be useful in some cases. See the 28@code{<gdk-pango-renderer>} documentation for details. 29 30@example 31 32#define RADIUS 100 33#define N_WORDS 10 34#define FONT "Sans Bold 18" 35 36GdkScreen *screen = gdk_drawable_get_screen (drawable); 37PangoRenderer *renderer; 38GdkGC *gc; 39 40PangoMatrix matrix = PANGO_MATRIX_INIT; 41PangoContext *context; 42PangoLayout *layout; 43PangoFontDescription *desc; 44 45double device_radius; 46int width, height; 47int i; 48 49/* Get the default renderer for the screen, and set it up for drawing */ 50renderer = gdk_pango_renderer_get_default (screen); 51gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), drawable); 52 53gc = gdk_gc_new (drawable); 54gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), gc); 55 56/* Set up a transformation matrix so that the user space coordinates for 57 * where we are drawing are [-RADIUS, RADIUS], [-RADIUS, RADIUS] 58 * We first center, then change the scale */ 59gdk_drawable_get_size (drawable, &width, &height); 60device_radius = MIN (width, height) / 2.; 61 62pango_matrix_translate (&matrix, 63 device_radius + (width - 2 * device_radius) / 2, 64 device_radius + (height - 2 * device_radius) / 2); 65pango_matrix_scale (&matrix, device_radius / RADIUS, device_radius / RADIUS); 66 67/* Create a PangoLayout, set the font and text */ 68context = gdk_pango_context_get_for_screen (screen); 69layout = pango_layout_new (context); 70pango_layout_set_text (layout, "Text", -1); 71desc = pango_font_description_from_string (FONT); 72pango_layout_set_font_description (layout, desc); 73pango_font_description_free (desc); 74 75/* Draw the layout N_WORDS times in a circle */ 76for (i = 0; i < N_WORDS; i++) 77 @{ 78 GdkColor color; 79 PangoMatrix rotated_matrix = matrix; 80 int width, height; 81 double angle = (360. * i) / N_WORDS; 82 83 /* Gradient from red at angle == 60 to blue at angle == 300 */ 84 color.red = 65535 * (1 + cos ((angle - 60) * M_PI / 180.)) / 2; 85 color.green = 0; 86 color.blue = 65535 - color.red; 87 88 gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer), 89 PANGO_RENDER_PART_FOREGROUND, &color); 90 91 pango_matrix_rotate (&rotated_matrix, angle); 92 93 pango_context_set_matrix (context, &rotated_matrix); 94 95 /* Inform Pango to re-layout the text with the new transformation matrix */ 96 pango_layout_context_changed (layout); 97 98 pango_layout_get_size (layout, &width, &height); 99 pango_renderer_draw_layout (renderer, layout, 100 - width / 2, - RADIUS * PANGO_SCALE); 101 @} 102 103/* Clean up default renderer, since it is shared */ 104gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer), 105 PANGO_RENDER_PART_FOREGROUND, NULL); 106gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), NULL); 107gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), NULL); 108 109/* free the objects we created */ 110g_object_unref (layout); 111g_object_unref (context); 112g_object_unref (gc); 113@end example 114 115@section Usage 116@include defuns-pango_interaction.xml.texi 117 118@c %end of fragment 119