gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:15:21 25 4
gpt4 key购买 nike

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

我对 pango_cairo_show_layout 函数看到的一些行为感到困惑:我得到了 pango 布局的精确“墨水”(不是“逻辑”)像素大小,并在 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

这是因为您渲染布局的位置(据我所知)是基线的位置(因此与文本逻辑相关),而不是布局的左上角(这将是一些与实际文本无关的“任意事物”)。

就您的代码而言,我建议将 tRectangle.x 添加到 dTextLocX (或减去?我不完全确定该符号)。 y 坐标也应执行相同的操作。

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

编辑:我不完全确定,但我认为 Pango 处理这个问题就像开罗一样。对于开罗,http://cairographics.org/tutorial/#L1understandingtext 有一个很好的描述。 。引用点是你给 cairo 的点。您要查看轴承的说明。

关于c - 为什么 pango_cairo_show_layout 在稍微错误的位置绘制文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33678228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com