1

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

Pango 似乎在任何换行符之后截断文本。的结果pango_layout_get_extents()似乎只包括第一行(我已经通过在第一行之后包含很长的行进行了测试)。pango_cairo_show_layout()也只渲染第一行。我试过使用\n, \r, and\r\n作为换行符,但没有任何效果。

奇怪的是,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 上的完整源代码

vcpkg在 Windows 上使用 Pango 版本。我相信我缺少一些导致问题的东西。谢谢您的帮助。

4

1 回答 1

0

经过一番调查,事实证明,自从我使用以来,如果 height 不是 -1PANGO_ELLIPSIZE_NONE则 的行为pango_layout_set_height()是未定义的。

于 2020-06-28T20:40:47.203 回答