首页 文章

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

提问于
浏览
4

我正在编写一个使用svg在图像上绘制点的应用程序 . 该图像最初是作为pdf进行的,我使用命令中的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,我可以选择图像,在“笔触样式”选项卡中将宽度增加1px . 这样做可以使图像看起来像我一样,但我需要能够以编程方式执行它,因为我每天都在运行这个命令,通过许多pdf文件 .

有没有办法可以:

  • 在inkscape unix命令中包含“笔触样式宽度”设置?

  • 以某种方式在使用css的svg img标签中设置它之后?

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>
    

    这将替换样式属性中所有出现的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

    希望有所帮助 .

    Update: 如果您不想设置固定的笔触宽度,但是在笔触宽度上添加了一些内容,则以下更改将执行以下操作:

    • 从xsl:param值中删除单位(px),因此它读取 <xsl:param name="stroke-width">1</xsl:param>

    • 在现有变量之后添加一个新变量: <xsl:variable name="current" select="substring-before($rest,';')"/>

    • $current + $stroke-width 替换 xsl:value-of 标签的 select 属性,如下所示: <xsl:value-of select="$current + $stroke-width"/>

    这假设在源SVG中 stroke-width: 之后没有单元 . 要添加,旧值和增量都必须是普通数字 .

相关问题