【问题标题】:How to access Cairo drawn path?如何访问开罗绘制的路径?
【发布时间】:2022-07-06 20:42:14
【问题描述】:

我正在使用 cairomm 来绘制对象。

#include "cairo/cairo.h"


int main()
{
    cairo_surface_t *surface;
    cairo_t *cr1;

    double width = 3840;
    double height = 2160;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    cr1 = cairo_create (surface);

    cairo_move_to(cr1, 0, 0);
    cairo_set_source_rgb(cr1, 1, 1, 1);
    cairo_set_line_width(cr1, 50.0);
    cairo_move_to(cr1, 0, 0)
    cairo_line_to(cr1, width, height)
    cairo_stroke();


    return 0;
}

上面的代码组成一行。 我想在创建线后操纵(移动、扩展等)线。有什么解决办法吗?

【问题讨论】:

  • 您的代码会立即画一条线,而不是创建cairo_path_t 对象。您需要创建一个cario_path_t 以便在定义路径后对其进行操作:cairographics.org/manual/cairo-Paths.html
  • @Dai 非常感谢!!如果你不介意,你能用我的代码展示 cairo_path_t 的例子吗?
  • 不,我不是开罗用户,我只是使用谷歌并快速浏览他们的文档来发表我的评论。

标签: c++ cairo


【解决方案1】:

您的代码正在创建一个包含一行的路径。在调用 cairo_stroke(或任何其他清除当前路径的方法)之前,您必须 copy the current path 从绘图上下文到 cairo_path_t 指针的末尾才能操作该路径。

在我的示例中,我稍微扩展了复制的路径,然后用不同的颜色再次对其进行描边。我还使用了非透明图像格式,并将创建的图像写入 png 文件以查看结果。

#include <cairo/cairo.h>

int main() {
    double width = 3840;
    double height = 2160;

    cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
    cairo_t* cr = cairo_create(surface);

    cairo_move_to(cr, 0, 0);
    cairo_line_to(cr, width, height);

    cairo_path_t* path = cairo_copy_path(cr);

    // Draw original path
    cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_set_line_width(cr, 50.0);
    cairo_stroke(cr);

    cairo_move_to(cr, 0, 0);
    cairo_append_path(cr, path);

    // Extend that path with some additional line
    cairo_rel_line_to(cr, -1 * width / 2, -1 * height / 3);

    // Draw extended path with different colour
    cairo_set_source_rgb(cr, 1, 0, 0);
    cairo_set_line_width(cr, 25.0);
    cairo_stroke(cr);

    cairo_surface_write_to_png(surface, "output.png");

    cairo_path_destroy(path);
    cairo_destroy(cr);
    cairo_surface_destroy(surface);
}

旁注:您使用的不是 cairomm 包装器,而是原始的 cairo API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 2017-11-21
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多