C++ pango 文本方向

标签 c++ gtk cairo gtkmm pango

我正在尝试在使用 pango 构建的图中标记 y 轴。

我无法将文本定向为沿 y 轴垂直向上。

代码的相关部分是:

 #include <gtkmm.h>

 Pango::FontDescription font;
 cr->save();  // where cr is Glib::RefPtr<Cairo::Context> const &
 cr->set_source_rgba(1.0,1.0,1.0,0.5);
 font.set_family("Monospace");
 font.set_style(Pango::STYLE_ITALIC);
 font.set_weight(Pango::WEIGHT_BOLD);
 font.set_absolute_size(20);
 Glib::RefPtr<Pango::Layout> x_axis_label = this->create_pango_layout("x-axis label");
 x_axis_label->set_font_description(font);
 cr->move_to(0.38,0.465);
 x_axis_label->show_in_cairo_context(cr);

 // so far so good, renders as expected

 // now trying to render the y-axis label
 Glib::RefPtr<Pango::Context> t_y_axis_label_ctxt = x_axis_label->get_context();
 Pango::Matrix p_matrix;
 // apply some transformation
 p_matrix.xx = 0.0;
 p_matrix.xy = 1.0;
 p_matrix.yx = 1.0;
 p_matrix.yy = 0.0;
 p_matrix.x0 = 0.0;
 p_matrix.y0 = 0.0;
 t_y_axis_label_ctxt->set_matrix(p_matrix);
 Glib::RefPtr<Pango::Layout> y_axis_label = Pango::Layout::create(t_y_axis_label_ctxt);

 y_axis_label->set_text("y-axis label"); // if this line of code is omitted I would expect, at least the text "x-axis label" to be rendered. But this does not happen.
 y_axis_label->set_font_description(font);
 cr->move_to(0.0,0.0);
 y_axis_label->show_in_cairo_context(cr);  // renders no output
 cr->restore();

我怀疑问题与我从 x 轴标签检索的上下文有关,并且预期的复制行为没有显现出来。

最佳答案

您确定要旋转 Pango 上下文吗?我相信该功能是在开罗之前的日子里引入的;现在您只需旋转开罗上下文,例如:

cr->save();
cr->rotate_degrees(90);
Glib::RefPtr<Pango::Layout> y_axis_label = this->create_pango_layout("y-axis label");
y_axis_label->set_font_description(font);
cr->move_to(...); // wherever you wanna put it
y_axis_label->show_in_cairo_context(cr);
cr->restore();

如果您仍然看不到任何内容,请确保没有将标签移出可见区域。

关于C++ pango 文本方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19109299/

相关文章:

java - 将 char 从 fgets 转换为 Wchar*

c++ - 原子赋值运算符

C++ 通过指针传递/访问矩阵

c - 将 Gtk Widgets 结构传递给回调函数

haskell - 如何使用 Haskell 在 GTK 中显示原始像素数据的图像?

c - 如何在 GTK 中创建项目目录 TreeView 和树存储?

c# - 无法使用 Mono.Cairo

c++ - 在 Cairo Surface 之前创建 Pango 布局

python - python中gtk.DrawingArea的透明背景

c++ - 强制 WSARecv 重叠