1

I'm working on a program where I'm using PangoLayout for text layout and Cairo for rendering, and I'm having trouble with text with multiple lines (or rather, text that contains new line characters).

It appears that Pango is truncating the text after any new line character. The result of pango_layout_get_extents() only seems to include the first line (I've tested by including very long lines after the first line). pango_cairo_show_layout() also renders only the first line. I've tried using \n, \r, and \r\n as the new line character, and none works.

Strangely, pango_layout_get_line_count() reports the correct number of lines.

Here is the code I use to create a 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());
}

And here is the full source on Github.

I'm using the vcpkg version of Pango on Windows. I believe that there's something I'm missing that's causing the problem. Thank you for your help.

Luke Zhou
  • 125
  • 2
  • 10

1 Answers1

1

After some investigation it turned out that since I was using PANGO_ELLIPSIZE_NONE, the behavior of pango_layout_set_height() is undefinied if height is not -1.

Luke Zhou
  • 125
  • 2
  • 10