Search code examples
cubuntugtkcairopango

Why is pango_cairo_show_layout drawing text at a slightly wrong location?


I have a Gtk app written in C running on Ubuntu Linux.

I'm confused about some behavior I'm seeing with the pango_cairo_show_layout function: I get the exact "ink" (not "logical") pixel size of a pango layout and draw the layout using pango_cairo_show_layout on a GtkDrawingArea widget. Right before drawing the layout, I draw a rectangle that should perfectly encompass the text that I'm about to draw, but the text always shows up a little below the bottom edge of the rectangle.

Here is my full code:

// The drawing area widget's "expose-event" callback handler
gboolean OnTestWindowExposeEvent(GtkWidget *pWidget, GdkEventExpose *pEvent, gpointer data)
{
    // Note that this window is 365 x 449 pixels
    double dEntireWindowWidth = pEvent->area.width; // This is 365.0
    double dEntireWindowHeight = pEvent->area.height; // This is 449.0

    // Create a cairo context with which to draw
    cairo_t *cr = gdk_cairo_create(pWidget->window);

    // Draw a red background
    cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
    cairo_rectangle(cr, 0.0, 0.0, dEntireWindowWidth, dEntireWindowHeight);
    cairo_fill(cr);

    // Calculate the padding inside the window which defines the text rectangle
    double dPadding = 0.05 * ((dEntireWindowWidth < dEntireWindowHeight) ? dEntireWindowWidth : dEntireWindowHeight);
    dPadding = round(dPadding); // This is 18.0

    // The size of the text box in which to draw text
    double dTextBoxSizeW = dEntireWindowWidth - (2.0 * dPadding);
    double dTextBoxSizeH = dEntireWindowHeight - (2.0 * dPadding);
    dTextBoxSizeW = round(dTextBoxSizeW); // This is 329.0
    dTextBoxSizeH = round(dTextBoxSizeH); // This is 413.0

    // Draw a black rectangle that defines the area in which text may be drawn
    cairo_set_line_width(cr, 1.0);
    cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    cairo_rectangle(cr, dPadding, dPadding, dTextBoxSizeW, dTextBoxSizeH);
    cairo_stroke(cr);

    // The text to draw
    std::string szText("Erik");

    // The font name to use
    std::string szFontName("FreeSans");

    // The font size to use
    double dFontSize = 153.0;

    // The font description string
    char szFontDescription[64];
    memset(&(szFontDescription[0]), 0, sizeof(szFontDescription));
    snprintf(szFontDescription, sizeof(szFontDescription) - 1, "%s %.02f", szFontName.c_str(), dFontSize);

    // Create a font description
    PangoFontDescription *pFontDescription = pango_font_description_from_string(szFontDescription);

    // Set up the font description
    pango_font_description_set_weight(pFontDescription, PANGO_WEIGHT_NORMAL);
    pango_font_description_set_style(pFontDescription, PANGO_STYLE_NORMAL);
    pango_font_description_set_variant(pFontDescription, PANGO_VARIANT_NORMAL);
    pango_font_description_set_stretch(pFontDescription, PANGO_STRETCH_NORMAL);

    // Create a pango layout
    PangoLayout *pLayout = gtk_widget_create_pango_layout(pWidget, szText.c_str());

    // Set up the pango layout
    pango_layout_set_alignment(pLayout, PANGO_ALIGN_LEFT);
    pango_layout_set_width(pLayout, -1);
    pango_layout_set_font_description(pLayout, pFontDescription);
    pango_layout_set_auto_dir(pLayout, TRUE);

    // Get the "ink" pixel size of the layout
    PangoRectangle tRectangle;
    pango_layout_get_pixel_extents(pLayout, &tRectangle, NULL);
    double dRealTextSizeW = static_cast<double>(tRectangle.width);
    double dRealTextSizeH = static_cast<double>(tRectangle.height);

    // Calculate the top left corner coordinate at which to draw the text
    double dTextLocX = dPadding + ((dTextBoxSizeW - dRealTextSizeW) / 2.0);
    double dTextLocY = dPadding + ((dTextBoxSizeH - dRealTextSizeH) / 2.0);

    // Draw a blue rectangle which should perfectly encompass the text we're about to draw
    cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
    cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
    cairo_rectangle(cr, dTextLocX, dTextLocY, dRealTextSizeW, dRealTextSizeH);
    cairo_stroke(cr);

    // Set up the cairo context for drawing the text
    cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
    cairo_set_antialias(cr, CAIRO_ANTIALIAS_BEST);

    // Move to the top left coordinate before drawing the text
    cairo_move_to(cr, dTextLocX, dTextLocY);

    // Draw the layout text
    pango_cairo_show_layout(cr, pLayout);

    // Clean up
    cairo_destroy(cr);
    g_object_unref(pLayout);
    pango_font_description_free(pFontDescription);

    return TRUE;
}

So, why is the text not being drawn exactly where I tell it to be drawn?

Thanks in advance for any help!


Solution

  • Look at the documentation for pango_layout_get_extents() (this is not mentioned in the docs for pango_layout_get_pixel_extents():

    Note that both extents may have non-zero x and y. You may want to use those to offset where you render the layout.

    https://developer.gnome.org/pango/stable/pango-Layout-Objects.html#pango-layout-get-extents

    This is because the position that you render the layout at is (as far as I remember) the position of the base line (so something logically related to the text) instead of the top-left corner of the layout (which would be some "arbitrary thing" not related to the actual text).

    In the case of your code, I would suggest to add tRectangle.x to dTextLocX (or subtract? I'm not completely sure about the sign). The same should be done with the y coordinate.

    TL;DR: Your PangoRectangle has a non-zero x/y position that you need to handle.

    Edit: I am not completely sure, but I think Pango handles this just like cairo. For cairo, there is a nice description at http://cairographics.org/tutorial/#L1understandingtext. The reference point is the point you give to cairo. You want to look at the description of bearing.