为什么 pango_cairo_show_layout 在稍微错误的位置绘制文本?

Why is pango_cairo_show_layout drawing text at a slightly wrong location?

我在 Ubuntu Linux 上有一个用 C 运行 编写的 Gtk 应用程序。

我对使用 pango_cairo_show_layout 函数看到的一些行为感到困惑:我得到了 pango 布局的确切 "ink"(不是 "logical")像素大小并绘制在 GtkDrawingArea 小部件上使用 pango_cairo_show_layout 的布局。在绘制布局之前,我绘制了一个矩形,它应该完美地包含我将要绘制的文本,但文本总是显示在矩形底部边缘下方一点。

这是我的完整代码:

// 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;
}

那么,为什么文本没有准确地绘制在我指定的位置?

在此先感谢您的帮助!

查看 pango_layout_get_extents() 的文档(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

这是因为您渲染布局的位置(据我所知)是基线的位置(因此在逻辑上与文本相关)而不是布局的左上角(这将是一些 "arbitrary thing" 与实际文本无关的内容)。

对于您的代码,我建议将 tRectangle.x 添加到 dTextLocX(或减去?我不太确定符号)。对 y 坐标也应该这样做。

TL;DR:您的 PangoRectangle 有一个您需要处理的非零 x/y 位置。

编辑:我不完全确定,但我认为 Pango 处理这个问题就像开罗一样。对于开罗,在 http://cairographics.org/tutorial/#L1understandingtext 有一个很好的描述。参考点是你给开罗的点。你要看bearing的描述