Cairo: grabbing the whole drawing as a pattern

喜你入骨 提交于 2019-12-12 01:39:57

问题


I am probably missing something conceptual about cairo. I draw using the following helper class:

struct GroupLock {
    GroupLock(Graphics &g) : g_(g) {
        cairo_push_group(g_.cr);
    }

    ~GroupLock() {
        cairo_pop_group_to_source(g_.cr);
        cairo_paint(g_.cr);
        cairo_surface_flush(g_.surface);
        XFlush(g_.display);
    }
private:
    Graphics &g_;
};

All my drawing functions are of the form:

void drawSomething(Graphics &g) {
    GroupLock lock{g}; (void)lock;
    ... // some drawing
}

Each call to such a drawing function sets the source (by virtue of using GroupLock) and makes the previous source unreachable. How can I modify this code to "concatenate" the sources instead? I would like be able to grab the whole drawing as a pattern by doing:

cairo_pattern_t *p_ = cairo_get_source(g_.cr);
cairo_pattern_reference(p_);

回答1:


After struggling for hours, here it is! All that needed to be done is to modify the constructor to read like this:

p_ = cairo_get_source(g_.cr);
cairo_pattern_reference(p_);

cairo_push_group(g_.cr);
cairo_set_source (g_.cr, p_);
cairo_paint(g_.cr);
cairo_pattern_destroy(p_);



回答2:


For future people wondering, the function that I believe to be the "correct" way to do this is cairo_push_group and cairo_pop_group which will create a new group and allow one to draw to it, and then subsequently get the drawing result as a pattern, which can then be set as a source. Cairo provides a convenience function for the latter in the form of cairo_pop_group_to_source, which pops the group and sets it as a source, as well as performing memory management for you.



来源:https://stackoverflow.com/questions/35969970/cairo-grabbing-the-whole-drawing-as-a-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!