PangoLayout 中忽略的新行

New Lines Ignored in PangoLayout

本文关键字:新行 PangoLayout      更新时间:2023-10-16

我正在开发一个程序,其中我使用PangoLayout进行文本布局,使用Cairo进行渲染,并且在处理多行文本(或者更确切地说,包含换行符的文本(时遇到问题。

Pango 似乎正在截断任何换行符之后的文本。pango_layout_get_extents()的结果似乎只包括第一行(我已经通过在第一行之后包含很长的行来测试(。pango_cairo_show_layout()也只呈现第一行。我尝试使用nrrn作为换行符,但没有一个有效。

奇怪的是,pango_layout_get_line_count()报告正确的行数。

这是我用来创建PangoLayout的代码:

PangoLayout *layout = pango_layout_new(_pango_context.get());
pango_layout_set_text(
layout, reinterpret_cast<const char*>(text.data()), static_cast<int>(text.size())
);
logger::get().log_debug(CP_HERE) << "line count: " << pango_layout_get_line_count(layout);
{ // set font
PangoFontDescription *desc = pango_font_description_new();
pango_font_description_set_family(desc, reinterpret_cast<const char*>(font.family.c_str()));
pango_font_description_set_style(desc, _details::cast_font_style(font.style));
pango_font_description_set_weight(desc, _details::cast_font_weight(font.weight));
pango_font_description_set_stretch(desc, _details::cast_font_stretch(font.stretch));
pango_font_description_set_size(desc, pango_units_from_double(font.size));
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
}
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
pango_layout_set_single_paragraph_mode(layout, false);
// horizontal wrapping
if (wrap == wrapping_mode::none) {
// FIXME alignment won't work for this case
pango_layout_set_width(layout, -1); // disable wrapping
} else {
pango_layout_set_width(layout, pango_units_from_double(size.x));
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
}
pango_layout_set_alignment(layout, _details::cast_horizontal_alignment(halign));
pango_layout_set_height(layout, pango_units_from_double(size.y));
// TODO vertical alignment
{ // set color
auto attr_list = _details::make_gtk_object_ref_give(pango_attr_list_new());
pango_attr_list_insert(attr_list.get(), pango_attr_foreground_new(
_details::cast_color_component(c.r),
_details::cast_color_component(c.g),
_details::cast_color_component(c.b)
));
pango_attr_list_insert(
attr_list.get(), pango_attr_foreground_alpha_new(_details::cast_color_component(c.a))
);
pango_layout_set_attributes(layout, attr_list.get());
}

这是Github上的完整源代码。

我在Windows上使用vcpkg版本的Pango。我相信我错过了一些导致问题的东西。谢谢你的帮助。

经过一番调查,事实证明,由于我使用的是PANGO_ELLIPSIZE_NONE,如果高度不是 -1,则pango_layout_set_height()的行为是不确定的。