PangoLayout 中忽略的新行
New Lines Ignored in PangoLayout
本文关键字:新行 PangoLayout 更新时间:2023-10-16
我正在开发一个程序,其中我使用PangoLayout
进行文本布局,使用Cairo
进行渲染,并且在处理多行文本(或者更确切地说,包含换行符的文本(时遇到问题。
Pango 似乎正在截断任何换行符之后的文本。pango_layout_get_extents()
的结果似乎只包括第一行(我已经通过在第一行之后包含很长的行来测试(。pango_cairo_show_layout()
也只呈现第一行。我尝试使用n
、r
和rn
作为换行符,但没有一个有效。
奇怪的是,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()
的行为是不确定的。
相关文章:
- 使用新行和不使用新行读取文件
- 遇到新行时,有没有办法停止istream_iterator
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 新行上的 clang 格式右括号
- C++流:保存到新行而不是覆盖旧行
- (C++) 使用诅咒打印从文件中读入的新行
- 按升序输出整数时,将最后一个整数放在新行上
- 用C++计算输入文本的新行数
- 为什么我的代码在终端中没有输出任何内容.开始新行没有错误
- WriteConsoleOutputCharacterW - 我的新行在哪里?
- 为什么当测试用例数为 1 时,我的程序不输入而是打印新行?
- 如何运行 while 循环直到到达新行?
- 从文本文件读取时,如何知道是否已到达新行?
- 句子转换器在存在新行的情况下不起作用
- 如何控制 clang 格式新行中 lessless 的对齐方式