rsvg 不渲染链接图像
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
LibRsvg 必须满足许多因素才能允许加载图像资源。从 v2.40 开始,这些内容包括:
xlink:href
属性data://
或 < code>file://file://
路径必须是绝对file://
路径必须位于 SVG 源文件的路径中或之下(任何符号链接被取消引用后)请注意,如果输入通过 stdin 传递到 rsvg-convert ,则没有路径算作输入文件和所有
file://
的子目录图像将被拒绝。控制图像 URL 解析的代码可以在
rsvg-base 中的 LibRsvg 源 中找到。 c
,函数_rsvg_handle_allow_load
。要在图像加载失败时向 rsvg-convert 添加调试通知,可以附加
到源代码中的 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:
xlink:href
attributedata://
orfile://
file://
paths must be absolutefile://
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 allfile://
images will be denied.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
to
config.h
in the source and recompile.看起来,如果您包含的文件与要转换的 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.
答案是处理以
file:///
开头的链接,并使其成为完整的绝对路径。The answer was to process the links to start with
file:///
and be a full absolute path.我想让 librsvg 图像渲染在我的 Mac 上运行,以加快 SVG 渲染速度。伊恩·麦金农 (Ian Mackinnon) 的出色答案包含了所有要素,但对库进行更改却很棘手。这些步骤使 librsvg 正确渲染具有相对路径的链接图像。我希望这可以节省一些人的时间。
首先,我使用以下方法安装了 librsvg:
在撰写本文时,这将安装版本 2.40.13 以及构建它所需的库。然后,我将源存档下载并解压到我的主目录中:
我编辑了此目录中
rsvg-base.c
中的_rsvg_handle_allow_load
函数,通过添加以下内容来绕过路径加载限制第 2275 行左右的代码:我还需要编辑 rsvg-image.c 中的 rsvg_cairo_surface_new_from_href 函数并停止使用 mime 类型加载 - 只需替换如下函数:
我需要要使用这些稍微修改过的命令来编译和安装修改后的库:
根据您的系统,您可能需要将 sudo 添加到上述命令中。
完成此操作后,我可以使用与 librsvg 一起安装的 rsvg-convert 命令行工具渲染相对 SVG 链接:
如果满足以下条件,我还可以使用 ImageMagick 将具有相对图像链接的 SVG 转换为 PNG 文件我以这种方式安装 librsvg 后安装了它:
这将让您测试 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:
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:
I edited the
_rsvg_handle_allow_load
function inrsvg-base.c
in this directory to bypass the path loading restrictions by adding this code around line 2275:I also needed to edit the
rsvg_cairo_surface_new_from_href
function inrsvg-image.c
and stop it loading using mime types - just replace the function like this:I needed to use these slightly modified commands to compile and install the modified library:
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: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:
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.