c++ - 获取模拟 Cairo::Context 以测试路径上的条件

标签 c++ gtk cairo gtkmm

这是对 this post 的跟进,其中我询问了检查在 Gtk::DrawingArea 中使用 Cairomm 绘制的形状边界的某些条件。派生小部件。就我而言,我有一个 void drawBorder(const Cairo::RefPtr<Cairo::Context>& p_context)方法是虚拟的,被重写以指定形状的边框。例如,如果我想要一个圆圈,我可以提供以下实现:

void drawBorder(const Cairo::RefPtr<Cairo::Context>& p_context)
{
    const Gtk::Allocation allocation{get_allocation()};

    const int width{allocation.get_width()};
    const int height{allocation.get_height()};
    const int smallestDimension{std::min(width, height)};

    const int xCenter{width / 2};
    const int yCenter{height / 2};

    p_context->arc(xCenter,
                   yCenter,
                   smallestDimension / 2.5,
                   0.0,
                   2.0 * M_PI);
}

我想用这个方法来检查我在边界曲线上的情况,如the answer中所建议的那样:

So, you would somehow get a cairo context (cairo_t in C), create your shape there (with line_to, curve_to, arc etc). Then you do not call fill or stroke, but instead cairo_copy_path_flat.

到目前为止,我无法获得可用的 Cairo::Context模拟执行检查。我不需要绘制任何东西来执行我的检查,我只需要获取底层路径并对其进行处理。

到目前为止,我已经尝试过:

  1. 路过nullptr作为Cairo::Surface (当然失败了);
  2. 获得与我的小部件等效的表面。

但是失败了。这个:gdk_window_create_similar_surface看起来很有希望,但我还没有找到小部件的等效项。

如何获得最小的模拟上下文来执行此类检查?这对我以后的单元测试也有很大帮助。


到目前为止我得到了这段代码:

bool isTheBorderASimpleAndClosedCurve()
{
    const Gtk::Allocation allocation{get_allocation()};

    Glib::RefPtr<Gdk::Window> widgetWindow{get_window()};

    Cairo::RefPtr<Cairo::Surface> widgetSurface{widgetWindow->create_similar_surface(Cairo::Content::CONTENT_COLOR_ALPHA,
                                                                                     allocation.get_width(),                                                                            allocation.get_height()) };

    Cairo::Context nakedContext{cairo_create(widgetSurface->cobj())};
    const Cairo::RefPtr<Cairo::Context> context{&nakedContext};

    drawBorder(context);

    // Would like to get the path and test my condition here...!
}

它编译和链接,但在运行时我得到一个段错误和一堆垃圾:

double free or corruption (out): 0x00007ffc0401c740

最佳答案

只需创建大小为 0x0 的 cairo 图像表面并为其创建上下文。

Cairo::RefPtr<Cairo::Surface> surface = Cairo::ImageSurface::create(
    Cairo::Format::FORMAT_ARGB32, 0, 0);
Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create(surface);

由于该表面不用于任何用途,因此它的大小无关紧要。

(旁注:根据 Google 给我的 API 文档,Context 的构造函数需要一个 cairo_t* 作为参数,而不是 Cairo::上下文*;这可能解释了您所看到的崩溃)

关于c++ - 获取模拟 Cairo::Context 以测试路径上的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48843411/

相关文章:

python - GtkTreeviewColumn 每行具有不同的组合模型

python - queue_draw_area 具有特定的处理程序?

Java 等同于 C++ 加密

c++ - 使用空类类型对象的内存

c++ - 如何使用 Gtkmm 获取光标位置?

c - 如何从组合框 GTK C 代码中删除所有项目?

python - 在 python 中使用 cairosvg 模块时可以指定缩放吗

gtk - X11:绘制叠加层/从叠加层中删除图纸

c++ - 为什么我不能在 Qt 中使用普通的 C++ 类

c++ - 获取重载静态函数的地址