如何使用cairo将图像写入SVG文件?

时间:2021-01-01 22:09:56

I have some code that looks like this:

我有一些看起来像这样的代码:

cairo_surface_t * surface = cairo_svg_surface_create("0.svg", 512, 512);
cairo_t * context = cairo_create(surface);

int * data = new int[512*512];

// fill the data...

cairo_surface_t * image_surface = 
    cairo_image_surface_for_data(data, 512, 512, 512*4);
cairo_set_source_surface(context, image_surface, 0, 0);
cairo_paint(context);

// do some other drawing ...

cairo_surface_flush(surface);
cairo_surface_finish(surface);
cairo_surface_destroy(surface);
cairo_destroy(context);

However, the svg always appears corrupted. The image is not properly written, and all drawing commands following do not work. Changing the surface type to PS, ie:

但是,svg总是显得已损坏。图像未正确写入,并且后面的所有绘图命令都不起作用。将曲面类型更改为PS,即:

cairo_surface_t * surface = cairo_ps_surface_create("0.ps", 512, 512);

produces a perfectly correct PS document. Any help fixing the SVG would be appreciated.

生成一个完全正确的PS文档。任何帮助修复SVG将不胜感激。

EDIT: Forgot to provide version info. Cairo 1.10.2 as given by cairo_version_string(). g++ 4.52 Running on Ubuntu 11.04

编辑:忘了提供版本信息。由cairo_version_string()给出的开罗1.10.2。 g ++ 4.52在Ubuntu 11.04上运行

EDIT(2): Ok, I have traced this down to PNG problems with cairo and discovered that cairo_surface_write_to_png does not behave as expected either. Both this function and attempting to embed an image in an SVG cause "out of memory errors", and I still don't know why.

编辑(2):好的,我已经跟踪了cairo的PNG问题并发现cairo_surface_write_to_png也没有按预期运行。这个函数和试图在SVG中嵌入图像都会导致“内存不足错误”,我仍然不知道为什么。

3 个解决方案

#1


2  

Looks like you may have forgotten to specify the SVG version as:

看起来您可能忘记将SVG版本指定为:

cairo_svg_surface_restrict_to_version (surface, CAIRO_SVG_VERSION_1_2);

You can do this immediately after creating the surface.

您可以在创建曲面后立即执行此操作。

#2


0  

Perhaps publishing the resulting plain SVG can help.

也许发布生成的普通SVG可以提供帮助。

#3


0  

I cannot find cairo_image_surface_for_data in the Cairo documentation. Did you mean cairo_image_surface_create_for_data? If so, you need to use cairo_format_stride_for_width to calculate the array size, and the bitmap data needs to be in the format Cairo expects. Since both of your outputs are corrupted, this strongly suggests that the problem is with the input.

我在Cairo文档中找不到cairo_image_surface_for_data。你是说cairo_image_surface_create_for_data吗?如果是这样,您需要使用cairo_format_stride_for_width来计算数组大小,并且位图数据需要采用Cairo期望的格式。由于两个输出都已损坏,这强烈表明问题出在输入上。

#1


2  

Looks like you may have forgotten to specify the SVG version as:

看起来您可能忘记将SVG版本指定为:

cairo_svg_surface_restrict_to_version (surface, CAIRO_SVG_VERSION_1_2);

You can do this immediately after creating the surface.

您可以在创建曲面后立即执行此操作。

#2


0  

Perhaps publishing the resulting plain SVG can help.

也许发布生成的普通SVG可以提供帮助。

#3


0  

I cannot find cairo_image_surface_for_data in the Cairo documentation. Did you mean cairo_image_surface_create_for_data? If so, you need to use cairo_format_stride_for_width to calculate the array size, and the bitmap data needs to be in the format Cairo expects. Since both of your outputs are corrupted, this strongly suggests that the problem is with the input.

我在Cairo文档中找不到cairo_image_surface_for_data。你是说cairo_image_surface_create_for_data吗?如果是这样,您需要使用cairo_format_stride_for_width来计算数组大小,并且位图数据需要采用Cairo期望的格式。由于两个输出都已损坏,这强烈表明问题出在输入上。