ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux-替换SVG文档中的变量(在YAML中外部定义)

2019-10-25 19:52:02  阅读:308  来源: 互联网

标签:string-interpolation bash svg yaml linux


背景

一些资源讨论了如何在SVG文档中使用变量,包括:

> Variables in SVG: Is it possible?
> SVG variable text
> How do I define or reference a variable in SVG?
> SVG: About using <defs> and <use> with variable text values
> http://www.schepers.cc/w3c/svg/params/ref.html
> https://www.w3.org/TR/2009/WD-SVGParamPrimer-20090430/#Introduction

尽管基于CSS,JavaScript和HTML的解决方案非常适合Web使用,但在其他情况下,SVG很有用,并且能够为变量定义外部源同样方便.

问题

SVG没有提供一种机制来定义与SVG相关的软件包(例如Inkscapersvg-convert)可以重用的可重用文本.例如,以下内容将是一流的:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg ...>
<input href="definitions.svg" />
...
<text ...>${variableName}</text>
</svg>

可以重载image element来导入外部文件,但是它有点黑,不允许将文本值分配给变量名以供重用.

您将如何从服务器上的外部文件(例如,YAML文件,但可能是数据库)中读取变量名称和值,并在呈现之前将这些变量替换为SVG文件?

解决方法:

另一个可能的解决方案:

在Inkscape中设置对象属性

enter image description here

保存它,我们将有类似

...
<text
   xml:space="preserve"
   style="font-style:normal;font-weight:normal;font-size:60px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;image-rendering:auto"
   x="262.91638"
   y="86.339157"
   id="mytest"
   sodipodi:linespacing="125%"
   inkscape:label="#myvar"><desc
     id="desc4150">The test object to replace with a var</desc><title
     id="title4148">myobj</title><tspan
     sodipodi:role="line"
     id="tspan4804"
     x="262.91638"
     y="86.339157"
     style="fill:#ffffff">sample</tspan></text>
...

然后使用键值对创建yaml文件

myvar: hello world

并解析SVG并替换值

#! /usr/bin/env python

import sys
from xml.dom import minidom
import yaml

yvars = yaml.load(file('drawing.yaml', 'r'))
xmldoc = minidom.parse('drawing.svg')
for s in xmldoc.getElementsByTagName('text'):
    for c in s.getElementsByTagName('tspan'):
        c.firstChild.replaceWholeText(yvars[s.attributes['inkscape:label'].value[1:]])
print xmldoc.toxml()

值将被替换

<text id="mytest" inkscape:label="#myvar" sodipodi:linespacing="125%" style="font-style:normal;font-weight:normal;font-size:60px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;image-rendering:auto" x="262.91638" xml:space="preserve" y="86.339157"><desc id="desc4150">The test object to replace with a var</desc><title id="title4148">myobj</title>
    <tspan id="tspan4804" sodipodi:role="line" style="fill:#ffffff" x="262.91638" y="86.339157">hello world</tspan></text>

标签:string-interpolation,bash,svg,yaml,linux
来源: https://codeday.me/bug/20191025/1930867.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有