Inkscape - 如何从Unix命令行设置描边样式

4

我正在编写一款应用程序,该应用程序使用SVG在图像上绘制点。该图像最初是以PDF格式出现的,我使用Unix中的Inkscape将其转换为.SVG文件,并使用以下命令:

inkscape –l convertedImage.svg baseImage.pdf

然后在我的HTML中使用转换后的图像。

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

我的问题是,在转换后,图像线条过于轻。如果我打开Inkscape GUI,可以选择图像,在"描边样式"选项卡中将宽度增加1px。用这种方式可以让图像看起来符合我的要求,但我需要以编程方式进行操作,因为我每天都会运行这个命令来处理许多pdf文件。
有没有办法可以: 1. 在inkscape unix命令中包含Stroke Style Width设置? 2. 使用CSS在svg img标记之后设置它?

像这样的CSS svg * { stroke-width: 1px } 不是也可以吗?如果它在嵌入式图像中无效,将其嵌入SVG中一定有效。 - Dagg Nabbit
@DaggNabbit,那个CSS不会起作用。即使SVG被嵌入到其中,它也很可能不起作用,因为它会被Inkscape在每个元素上设置的样式属性所覆盖。然而,svg * {stroke-width: 1px !important}可能会起作用。但我认为还有更好的解决方案。 - Thomas W
@Marianna PDF中的线宽是否正常?如果是,那么请尝试使用另一个转换器,即pdf2svg。如果这没有帮助,我建议进行第二步SVG清理,这是一个好主意,因为Inkscape可以产生一些非常膨胀的SVG标记。根据这是否是一次性任务或您经常进行此转换,手动或自动清理都是可取的。为了更好地帮助您清理,我会要求至少提供转换后的SVG片段。 - Thomas W
线条在 PDF 中看起来还好,但我认为这只是因为 PDF 以不同的方式显示它们。我也尝试使用 imagemagick 将其转换为 PNG,结果相同。我知道 Inkscape 有能力做到我想要的,但我只需要从命令行访问它。 - user1514879
你能否提供一个示例PDF的链接? - nick_w
1个回答

5

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

shell> 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>

这将替换style属性中stroke-width的所有出现为stroke-width:1px的值,
要指定另一个宽度,您可以像这样向xsltproc传递参数:
shell> xsltproc --stringparam stroke-width 5px svglinewidth.xsl convertedImage.svg > fixedImage.svg
几乎任何平台都可以使用xsltproc。Linux有它作为软件包,对于Unix,请参见http://xmlsoft.org/XSLT/xsltproc2.html 希望这能帮到你。
更新: 如果您不想设置固定的描边宽度,而是想添加一些内容到描边宽度中,则以下更改将完成任务:
  1. 从xsl:param值中删除单位(px),使其变为<xsl:param name="stroke-width">1</xsl:param>
  2. 在现有变量后添加一个新变量:<xsl:variable name="current" select="substring-before($rest,';')"/>
  3. $current + $stroke-width替换xsl:value-of标记的select属性,使其看起来像这样:<xsl:value-of select="$current + $stroke-width"/>

这假设源SVG中stroke-width:后面没有单位。为了进行加法运算,旧值和增量都必须是纯数字。


太好了!那正是我需要的,非常感谢你! - user1514879

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接