从 URI 加载 SVG 文件得到不同的结果

Getting different results loading SVG files from URIs

背景

JavaFX 程序 MyApp 将导入 SVG 图像,解析文件中的变换和形状信息,并在扩展 JavaFX Canvas 的 class 中显示形状。给出奇怪结果的部分是在初始阶段,获取 SVG 图像的文件系统 URI 并解析它。

奇怪的是,我有两种方法可以做同样的事情,但其中一种有效(使用 FileChooser)而另一种无效(使用 class 加载器 MyApp.class.getResource(pathString)).两者都为同一个文件生成一个有效的 URI,并且两个 URI 都被提供给蜡染 SAXSVGDocumentFactory.

我已在两个 URI 上使用 FileReader 检查以确保从两个 URI 读取的文本相同。

问题代码(简体)

FileChooser importer = new FileChooser();

URI uriBad = MyApp.class.getResource(“my_file.svg”).toURI();
URI uriGood = importer.showOpenDialog(myJavafxStage).toURI();

SAXSVGDocumentFactory documentFactory = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());

SVGDocument vectorDocumentBad = documentFactory.createSVGDocument(uriBad);
SVGDocument vectorDocumentGood = documentFactory.createSVGDocument(uriGood);

本以为vectorDocumentBadvectorDocumentGood能一样解析,但是用同样的解析函数得到的转换信息对于之前创建的文档是错误的,对应uriBad.

错误 URI 的输出

PARSING SVG
NODE_RootGraphicsNode without style or transform info
GROUP_RootGraphicsNode: 0.0,0.0
    GROUP_CanvasGraphicsNode: NaN,NaN
        SHAPE_ShapeNode: NaN,NaN
        SHAPE_ShapeNode: NaN,NaN
        SHAPE_ShapeNode: NaN,NaN
        SHAPE_ShapeNode: NaN,NaN
        SHAPE_ShapeNode: NaN,NaN
        SHAPE_ShapeNode: NaN,NaN
        ...

良好 URI 的输出

PARSING SVG
NODE_RootGraphicsNode without style or transform info
GROUP_RootGraphicsNode: 0.0,0.0
    GROUP_CanvasGraphicsNode: 164.0199324823223,-6.723440123593414
        SHAPE_ShapeNode: 164.0199324823223,-6.723440123593414
        SHAPE_ShapeNode: 164.0199324823223,-6.723440123593414
        SHAPE_ShapeNode: 164.0199324823223,-6.723440123593414
        SHAPE_ShapeNode: 164.0199324823223,-6.723440123593414
        SHAPE_ShapeNode: 164.0199324823223,-6.723440123593414
        ...

SVG 文档

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg  version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="181.957" height="238.119"
     viewBox="0 0 181.957 238.119" overflow="visible" enable-background="new 0 0 181.957 238.119;" xml:space="preserve">

<path fill="none" stroke="#46B035" stroke-width="7" d="M58.726,11.278c0,0-2.5,4.75,2.667,16.333s12.7,13.745,12.7,13.745"/>
<linearGradient id="XMLID_18_" gradientUnits="userSpaceOnUse" x1="51.5977" y1="130.6812" x2="84.5962" y2="162.1797">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_18_)" stroke="#56278A" stroke-width="2" cx="70.75" cy="148.962" r="24.751"/>
<linearGradient id="XMLID_19_" gradientUnits="userSpaceOnUse" x1="107.0942" y1="194.0854" x2="140.0934" y2="225.5846">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_19_)" stroke="#56278A" stroke-width="2" cx="126.247" cy="212.368" r="24.751"/>
<linearGradient id="XMLID_20_" gradientUnits="userSpaceOnUse" x1="76.2715" y1="167.9692" x2="109.2707" y2="199.4684">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_20_)" stroke="#56278A" stroke-width="2" cx="95.424" cy="186.251" r="24.751"/>
<linearGradient id="XMLID_21_" gradientUnits="userSpaceOnUse" x1="120.1133" y1="167.0015" x2="153.1125" y2="198.5006">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_21_)" stroke="#56278A" stroke-width="2" cx="139.265" cy="185.284" r="24.751"/>
<linearGradient id="XMLID_22_" gradientUnits="userSpaceOnUse" x1="124.1118" y1="119.4976" x2="157.111" y2="150.9967">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_22_)" stroke="#56278A" stroke-width="2" cx="143.264" cy="137.779" r="24.751"/>
<linearGradient id="XMLID_23_" gradientUnits="userSpaceOnUse" x1="76.6772" y1="104.4282" x2="109.6764" y2="135.9274">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_23_)" stroke="#56278A" stroke-width="2" cx="95.83" cy="122.71" r="24.751"/>
<linearGradient id="XMLID_24_" gradientUnits="userSpaceOnUse" x1="129.5947" y1="32.1802" x2="162.5939" y2="63.6793">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_24_)" stroke="#56278A" stroke-width="2" cx="148.748" cy="50.462" r="24.751"/>
<linearGradient id="XMLID_25_" gradientUnits="userSpaceOnUse" x1="137.0527" y1="71.9644" x2="170.0519" y2="103.4635">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_25_)" stroke="#56278A" stroke-width="2" cx="156.206" cy="90.247" r="24.751"/>
<linearGradient id="XMLID_26_" gradientUnits="userSpaceOnUse" x1="107.0933" y1="81.6763" x2="140.0924" y2="113.1754">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_26_)" stroke="#56278A" stroke-width="2" cx="126.246" cy="99.958" r="24.752"/>
<linearGradient id="XMLID_27_" gradientUnits="userSpaceOnUse" x1="87.5947" y1="22.1807" x2="120.5939" y2="53.6798">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_27_)" stroke="#56278A" stroke-width="2" cx="106.748" cy="40.462" r="24.751"/>
<linearGradient id="XMLID_28_" gradientUnits="userSpaceOnUse" x1="48.5972" y1="43.1802" x2="81.5963" y2="74.6793">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_28_)" stroke="#56278A" stroke-width="2" cx="67.75" cy="61.461" r="24.751"/>
<linearGradient id="XMLID_29_" gradientUnits="userSpaceOnUse" x1="6.5986" y1="50.106" x2="39.5985" y2="81.6057">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_29_)" stroke="#56278A" stroke-width="2" cx="25.751" cy="68.388" r="24.751"/>
<linearGradient id="XMLID_30_" gradientUnits="userSpaceOnUse" x1="26.7363" y1="97.314" x2="59.7355" y2="128.8131">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_30_)" stroke="#56278A" stroke-width="2" cx="45.889" cy="115.596" r="24.751"/>
<linearGradient id="XMLID_31_" gradientUnits="userSpaceOnUse" x1="102.9971" y1="46.4272" x2="135.9962" y2="77.9264">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_31_)" stroke="#56278A" stroke-width="2" cx="122.15" cy="64.709" r="24.752"/>
<linearGradient id="XMLID_32_" gradientUnits="userSpaceOnUse" x1="73.0938" y1="62.1743" x2="106.0929" y2="93.6734">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_32_)" stroke="#56278A" stroke-width="2" cx="92.247" cy="80.457" r="24.752"/>
<linearGradient id="XMLID_33_" gradientUnits="userSpaceOnUse" x1="37.436" y1="74.3335" x2="70.4352" y2="105.8326">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_33_)" stroke="#56278A" stroke-width="2" cx="56.589" cy="92.616" r="24.752"/>
<linearGradient id="XMLID_34_" gradientUnits="userSpaceOnUse" x1="90.9429" y1="143.8394" x2="123.9414" y2="175.3379">
    <stop  offset="0" style="stop-color:#FFFFFF"/>
    <stop  offset="0.736" style="stop-color:#573993"/>
</linearGradient>
<circle fill="url(#XMLID_34_)" stroke="#56278A" stroke-width="2" cx="110.095" cy="162.121" r="24.751"/>
<polyline fill="none" stroke="#46B035" stroke-width="7" points="35.654,16.038 58.726,11.278 80.652,3.289 "/>

</svg>

虚惊一场!当我试图修改从 SVG 文件中读取的那些转换值时,问题实际上并不在我认为的地方,在方法调用链中还有很多步骤。我基本上是在尝试获取还没有任何尺寸的 JavaFX 窗格的尺寸,然后将其未定义的值乘以 SVG 转换以进行显示。

所以我基本上是在问错问题。