Pango Interaction

## Pango Interaction Pango Interaction — Using Pango in GDK |

## Includes

    #include <gdk/gdk.h>

## Description

Pango is the text layout system used by GDK and GTK+. The functions and types in this section are used to render Pango objects to GDK. drawables, and also extend the set of Pango attributes to include stippling and embossing.

Creating a PangoLayout object is the first step in rendering text, and requires getting a handle to a PangoContext. For GTK+ programs, you'll usually want to use `gtk_widget_get_pango_context()`, or `gtk_widget_create_pango_layout()`, rather than using the lowlevel `gdk_pango_context_get_for_screen()`. Once you have a PangoLayout, you can set the text and attributes of it with Pango functions like `pango_layout_set_text()` and get its size with `pango_layout_get_size()`. (Note that Pango uses a fixed point system internally, so converting between Pango units and pixels using PANGO_SCALE or the `PANGO_PIXELS()` macro.)

Rendering a Pango layout is done most simply with `gdk_draw_layout()`; you can also draw pieces of the layout with `gdk_draw_layout()` or `gdk_draw_glyphs()`. GdkPangoRenderer is a subclass of PangoRenderer that is used internally to implement these functions. Using it directly or subclassing it can be useful in some cases. See the GdkPangoRenderer documentation for details.

Example 5. Using GdkPangoRenderer to draw transformed text

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    #define RADIUS 100
    #define N_WORDS 10
    #define FONT "Sans Bold 18"
    GdkScreen *screen = gdk_drawable_get_screen (drawable);
    PangoRenderer *renderer;
    GdkGC *gc;
    PangoMatrix matrix = PANGO_MATRIX_INIT;
    PangoContext *context;
    PangoLayout *layout;
    PangoFontDescription *desc;
    double device_radius;
    int width, height;
    int i;
    /* Get the default renderer for the screen, and set it up for drawing  */
    renderer = gdk_pango_renderer_get_default (screen);
    gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), drawable);
    gc = gdk_gc_new (drawable);
    gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), gc);
    /* Set up a transformation matrix so that the user space coordinates for
     * where we are drawing are [-RADIUS, RADIUS], [-RADIUS, RADIUS]
     * We first center, then change the scale */
    gdk_drawable_get_size (drawable, &width, &height);
    device_radius = MIN (width, height) / 2.;
    pango_matrix_translate (&matrix,
                            device_radius + (width - 2 * device_radius) / 2,
                            device_radius + (height - 2 * device_radius) / 2);
    pango_matrix_scale (&matrix, device_radius / RADIUS, device_radius / RADIUS);
    /* Create a PangoLayout, set the font and text */
    context = gdk_pango_context_get_for_screen (screen);
    layout = pango_layout_new (context);
    pango_layout_set_text (layout, "Text", -1);
    desc = pango_font_description_from_string (FONT);
    pango_layout_set_font_description (layout, desc);
    pango_font_description_free (desc);
    /* Draw the layout N_WORDS times in a circle */
    for (i = 0; i < N_WORDS; i++)
      {
        GdkColor color;
        PangoMatrix rotated_matrix = matrix;
        int width, height;
        double angle = (360. * i) / N_WORDS;
        /* Gradient from red at angle == 60 to blue at angle == 300 */
        color.red   = 65535 * (1 + cos ((angle - 60) * M_PI / 180.)) / 2;
        color.green = 0;
        color.blue  = 65535  - color.red;
        gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
                                               PANGO_RENDER_PART_FOREGROUND, &color);
        pango_matrix_rotate (&rotated_matrix, angle);
        pango_context_set_matrix (context, &rotated_matrix);
        /* Inform Pango to re-layout the text with the new transformation matrix */
        pango_layout_context_changed (layout);
        pango_layout_get_size (layout, &width, &height);
        pango_renderer_draw_layout (renderer, layout,
                                    - width / 2, - RADIUS * PANGO_SCALE);
      }
    /* Clean up default renderer, since it is shared */
    gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
                                           PANGO_RENDER_PART_FOREGROUND, NULL);
    gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), NULL);
    gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), NULL);
    /* free the objects we created */
    g_object_unref (layout);
    g_object_unref (context);
    g_object_unref (gc);

Figure 1. Output of Example 5, “Using GdkPangoRenderer to draw transformed text”

## Functions

## Types and Values