2

这是我第一次使用 pangomm,我试图将一些文本渲染到 Cairo::Context,但是当我尝试从 Pango::Layout 对象访问任何内容时,程序会抛出以下错误:

(process:7175): glibmm-CRITICAL **: 15:36:58.578: Glib::ObjectBase* Glib::wrap_create_new_wrapper(GObject*): assertion 'wrap_func_table != nullptr' failed

(process:7175): glibmm-WARNING **: 15:36:58.578: Failed to wrap object of type 'PangoLayout'. Hint: this error is commonlycaused by failing to call a library init() function.
[1]    7175 segmentation fault  ./a.out

我无法使用 gdb 回溯错误。

代码

#include <cairomm/cairomm.h>
#include <pangomm.h>

int main() {
  auto surf = Cairo::ImageSurface::create(Cairo::Format::FORMAT_ARGB32, 1920, 20);
  auto cr = Cairo::Context::create(surf);

  cr->set_source_rgb(0.0, 0.0, 0.0);
  cr->paint();

  cr->move_to(0.0, 0.0);

  cr->set_source_rgb(1.0, 1.0, 1.0);

  auto layout = Pango::Layout::create(cr);
  auto font = Glib::ustring("Sans Bold 27");

  Pango::FontDescription desc(font);
  layout->set_font_description(desc);

  auto text = Glib::ustring("Oi");
  layout->set_text(text);

  layout->show_in_cairo_context(cr);

  surf->write_to_png("test.png");

  return 0;
}

编译命令

g++ -g -Wall `pkg-config --cflags cairomm-1.0 pangomm-1.4` main.cpp `pkg-config --libs cairomm-1.0 pangomm-1.4`
4

1 回答 1

1

由于您在pangomm没有 Gtk 的情况下使用,因此您需要pangomm在程序开始时进行初始化。这也初始化了 Glib。调用Pango::init();并包含<pangomm/init.h>.

因此你的代码变成

...
#include <pangomm/init.h>

int main()
{
    Pango::init();

    ...
}
于 2019-02-01T19:14:33.030 回答