rsvg 不渲染链接图像

发布于 2024-09-04 04:08:01 字数 322 浏览 8 评论 0原文

我使用 python rsvg 绑定将 svg 图像渲染到 cairo 中并保存到文件,这基本上可以工作。但是,如果 svg 文件包含链接图像,如下所示:

<image href="static/usrimgs/tmpDtIKpx.png" x="10" y="10" width="600px" height="400px"></image>

该图像不会显示在最终文件中(svg 的其余部分渲染得很好)。根据脚本运行的位置,相对路径是正确的,但我猜测它通常是相对 URL,而不是相对文件路径,这一事实存在一些问题。我该如何解决这个问题?

I use the python rsvg bindings to render an svg image into cairo and save to file, which mostly works. But if the svg file contains a linked image, like so:

<image href="static/usrimgs/tmpDtIKpx.png" x="10" y="10" width="600px" height="400px"></image>

the image doesn't show up in the final file (the rest of the svg renders just fine). The relative path is correct based on where the script is running, but I'm guessing there's some problem with the fact that it would normally be a relative URL, not a relative filepath. How do I get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天的雪花 2024-09-11 04:08:01

LibRsvg 必须满足许多因素才能允许加载图像资源。从 v2.40 开始,这些内容包括:

  • 图像必须具有 xlink:href 属性
  • 该属性必须包含完整、有效的 URI
  • 方案可以是 data:// 或 < code>file://
  • file:// 路径必须是绝对
  • file:// 路径必须位于 SVG 源文件的路径中或之下(任何符号链接被取消引用后)

请注意,如果输入通过 stdin 传递到 rsvg-convert ,则没有路径算作输入文件和所有 file:// 的子目录图像将被拒绝。

rsvg-convert < input.svg > output.png  # images denied

rsvg-convert -o output.png input.svg  # images allowed

控制图像 URL 解析的代码可以在 rsvg-base 中的 LibRsvg 源 中找到。 c,函数_rsvg_handle_allow_load

要在图像加载失败时向 rsvg-convert 添加调试通知,可以附加

#define G_ENABLE_DEBUG

到源代码中的 config.h 并重新编译。

There are many factors which must be satisfied for LibRsvg to allow an image resource to be loaded. As of v2.40 these include:

  • the image must have an xlink:href attribute
  • that attribute must contain a full, valid URI
  • the scheme can be data:// or file://
  • file:// paths must be absolute
  • file:// paths must be in or below the path of the SVG source file (after any symlinks have been dereferenced)

Note that if input is passed to rsvg-convert via stdin, then no paths count as subdirectories of the input file and all file:// images will be denied.

rsvg-convert < input.svg > output.png  # images denied

rsvg-convert -o output.png input.svg  # images allowed

The code governing image URL resolution can be found in the LibRsvg source in rsvg-base.c, function _rsvg_handle_allow_load.

To add a debugging notification to rsvg-convert when image loading fails, one can append

#define G_ENABLE_DEBUG

to config.h in the source and recompile.

把梦留给海 2024-09-11 04:08:01

看起来,如果您包含的文件与要转换的 SVG 文件不在同一路径或路径下,则它将找不到它。
我认为这是一个错误。

It seems that, if the file you are including is not on the same path or under the path of the SVG file you want to convert, it will not find it.
I regard this as a bug.

愁杀 2024-09-11 04:08:01

答案是处理以 file:/// 开头的链接,并使其成为完整的绝对路径。

The answer was to process the links to start with file:/// and be a full absolute path.

囍笑 2024-09-11 04:08:01

我想让 librsvg 图像渲染在我的 Mac 上运行,以加快 SVG 渲染速度。伊恩·麦金农 (Ian Mackinnon) 的出色答案包含了所有要素,但对库进行更改却很棘手。这些步骤使 librsvg 正确渲染具有相对路径的链接图像。我希望这可以节省一些人的时间。

首先,我使用以下方法安装了 librsvg:

brew install --build-from-source librsvg

在撰写本文时,这将安装版本 2.40.13 以及构建它所需的库。然后,我将源存档下载并解压到我的主目录中:

wget https://download.gnome.org/sources/librsvg/2.40/librsvg-2.40.13.tar.xz
tar jxvf librsvg-2.40.13.tar.xz
cd librsvg-2.40.13

我编辑了此目录中 rsvg-base.c 中的 _rsvg_handle_allow_load 函数,通过添加以下内容来绕过路径加载限制第 2275 行左右的代码:

2275
2276     goto allow; // Just try and load it!
2277     

我还需要编辑 rsvg-image.c 中的 rsvg_cairo_surface_new_from_href 函数并停止使用 mime 类型加载 - 只需替换如下函数:

55     if (mime_type) {
56         // loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error);     // This doesn't work on my mac
57         loader = gdk_pixbuf_loader_new (); // This works
58     } else {
59         loader = gdk_pixbuf_loader_new ();
60     }

我需要要使用这些稍微修改过的命令来编译和安装修改后的库:

make clean
make install gdk_pixbuf_binarydir=/usr/local/Cellar/librsvg/2.40.13/lib/gdk-pixbuf-2.0/2.10.0/loaders gdk_pixbuf_moduledir=/usr/local/Cellar/librsvg/2.40.13/lib/gdk-pixbuf-2.0/2.10.0/loaders

根据您的系统,您可能需要将 sudo 添加到上述命令中。

完成此操作后,我可以使用与 librsvg 一起安装的 rsvg-convert 命令行工具渲染相对 SVG 链接:

rsvg-convert test.svg -o test.png

如果满足以下条件,我还可以使用 ImageMagick 将具有相对图像链接的 SVG 转换为 PNG 文件我以这种方式安装 librsvg 后安装了它:

convert test.svg test.png

这将让您测试 rsvg 功能和性能 - 我发现它比我的应用程序的 Inkscape 快 2-3 倍。如果您在生产环境中使用它,我建议更智能地更改代码。

I wanted to get librsvg image rendering working on my Mac to speed up SVG rendering. Ian Mackinnon's great answer has all the ingredients but implementing a change to the library is tricky. These steps made librsvg correctly render linked images with relative paths. I hope this saves someone some time.

First, I installed librsvg using:

brew install --build-from-source librsvg

At the time of writing, this installs version 2.40.13 and the necessary libraries to build it. I then downloaded and extracted the source archive into my home directory:

wget https://download.gnome.org/sources/librsvg/2.40/librsvg-2.40.13.tar.xz
tar jxvf librsvg-2.40.13.tar.xz
cd librsvg-2.40.13

I edited the _rsvg_handle_allow_load function in rsvg-base.c in this directory to bypass the path loading restrictions by adding this code around line 2275:

2275
2276     goto allow; // Just try and load it!
2277     

I also needed to edit the rsvg_cairo_surface_new_from_href function in rsvg-image.c and stop it loading using mime types - just replace the function like this:

55     if (mime_type) {
56         // loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error);     // This doesn't work on my mac
57         loader = gdk_pixbuf_loader_new (); // This works
58     } else {
59         loader = gdk_pixbuf_loader_new ();
60     }

I needed to use these slightly modified commands to compile and install the modified library:

make clean
make install gdk_pixbuf_binarydir=/usr/local/Cellar/librsvg/2.40.13/lib/gdk-pixbuf-2.0/2.10.0/loaders gdk_pixbuf_moduledir=/usr/local/Cellar/librsvg/2.40.13/lib/gdk-pixbuf-2.0/2.10.0/loaders

Depending on your system, you might need to add sudo to the above commands.

Once this was done, I could render relative SVG links using the rsvg-convert command line tool that is installed with librsvg:

rsvg-convert test.svg -o test.png

I was also able to use ImageMagick to convert SVGs with relative image links to PNG files if I installed it after installing librsvg in this way:

convert test.svg test.png

This will let you test rsvg function and performance - I found it was 2-3x faster than Inkscape for my application. I recommend changing the code more intelligently if you use this in a production environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文