gpt4 book ai didi

svg - Inkscape - 如何从 unix 命令行设置笔划样式

转载 作者:搜寻专家 更新时间:2023-10-31 08:11:00 27 4
gpt4 key购买 nike

我正在编写一个应用程序,它使用 svg 在图像上绘制点。该图像最初以 pdf 格式出现,我正在使用 unix 中的 Inkscape 将其转换为 .svg 文件,命令如下:

inkscape –l convertedImage.svg baseImage.pdf

然后在我的 html 中使用我的 svg 标签中转换后的图像。

<svg>
<image x=”100” y=”100” width=”500” height=”500”
xlink:href=”/my/location/convertedImage.svg”></image>

</sig>

我的问题是转换后图像线条太淡。如果我打开 Inkscape GUI,我可以选择图像并在“笔触样式”选项卡中将宽度增加 1 像素。这样做可以使图像看起来也像我想要的那样,但我需要能够以编程方式执行此操作,因为我每天都要对许多 pdf 文件运行此命令。

有什么办法可以:

  1. 在 inkscape unix 命令中包含 Stroke Style Width 设置?
  2. 在事后使用 css 在 svg img 标签中设置它?

最佳答案

SVG 是一种 XML 格式,因此您可以像这样使用 XML 转换来修复它:

外壳> xsltproc svglinewidth.xsl convertedImage.svg > fixedImage.svg

其中 svglinewidth.xsl 包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

<xsl:param name="stroke-width">1px</xsl:param>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*|text()|*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@style[contains(., 'stroke-width:')]">
<xsl:variable name="before" select="substring-before(.,'stroke-width:')"/>
<xsl:variable name="rest" select="substring-after(.,'stroke-width:')"/>
<xsl:variable name="after" select="substring-after($rest,';')"/>
<xsl:attribute name="style">
<xsl:value-of select="$before"/>
<xsl:text>stroke-width:</xsl:text>
<xsl:value-of select="$stroke-width"/>
<xsl:text>;</xsl:text>
<xsl:value-of select="$after"/>
</xsl:attribute>
</xsl:template>

<xsl:template match="@*|text()">
<xsl:copy/>
</xsl:template>

</xsl:stylesheet>

这会将样式属性中所有出现的 stroke-width 替换为值 stroke-width:1px,

要指定另一个宽度,您可以像这样将参数传递给 xsltproc:

外壳> xsltproc --stringparam stroke-width 5px svglinewidth.xsl convertedImage.svg > fixedImage.svg

xsltproc 几乎适用于任何平台。 Linux 将其作为软件包,对于 Unix,请参阅 http://xmlsoft.org/XSLT/xsltproc2.html

希望对您有所帮助。

更新:如果您不想设置固定的 stroke-width,而是想添加一些内容到 stroke-width,则以下更改将完成这项工作:

  1. 从 xsl:param 值中删除单位 (px),因此它显示为 <xsl:param name="stroke-width">1</xsl:param>
  2. 在现有变量之后添加一个新变量:<xsl:variable name="current" select="substring-before($rest,';')"/>
  3. 替换select xsl:value-of 的属性用 $current + $stroke-width 标记,所以它看起来像:<xsl:value-of select="$current + $stroke-width"/>

这假设stroke-width: 之后没有单位在源 SVG 中。为了添加,旧值和增量都必须是纯数字。

关于svg - Inkscape - 如何从 unix 命令行设置笔划样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com