【问题标题】:New Lines Ignored in PangoLayoutPangoLayout 中忽略的新行
【发布时间】:2020-10-18 23:39:53
【问题描述】:

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

Pango 似乎在任何换行符之后截断文本。 pango_layout_get_extents() 的结果似乎只包含第一行(我已经通过在第一行之后包含很长的行进行了测试)。 pango_cairo_show_layout() 也只呈现第一行。我尝试使用\n\r\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());
}

这里是the full source on Github

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

【问题讨论】:

    标签: c++ pango pangocairo


    【解决方案1】:

    经过一番调查,原来我用的是PANGO_ELLIPSIZE_NONEthe behavior of pango_layout_set_height() is undefinied if height is not -1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多