使用 fop-2.2 将 svg 转换为 pdf 时,stroke-opacity 对 svg:text 没有影响

stroke-opacity has no effect on svg:text while conveting svg to pdf using fop-2.2

我正在将 batik-1.7 升级到 1.11,将 fop-0.94 升级到 2.2。我在我的应用程序中使用 fop 的 PDFTranscoder 将 SVG 转换为 PDF。 stroke-opacity 应用于 svg 中的文本。在升级 pdf 之前看起来不错,结果与不透明度一样。但是升级后笔画不透明度没有应用到文本。

下面是用于将 svg 转换为 pdf 的代码:

Transcoder transcoder = new PDFTranscoder();
TranscoderInput input = new TranscoderInput(svgFile.toURI().toString());
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
transcoder.transcode(input, output);

使用的 svg 文件:

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="418" viewBox="0,0 65416,45424" width="816" xml:space="preserve">   
<defs>
<style type="text/css"><![CDATA[
.P{
font-family:"Arial";
font-weight:normal;
font-size:247px;
font-family:"Lucida Sans";
font-style:normal;
stroke:#000;
stroke-width:16px;
stroke-dasharray:none;
stroke-linejoin:miter;
stroke-miterlimit:10;
stroke-width:0.5px;
stroke-linecap:square;
stroke-opacity:1.0;
fill:none;
fill-opacity:0.0;
fill-rule:evenodd;
}

.M{
font-family:"Arial";
font-weight:normal;
font-size:282px;
font-style:normal;
stroke:#00F;
stroke-dasharray:none;
stroke-linejoin:miter;
stroke-miterlimit:10;
stroke-width:2px;
stroke-linecap:square;
stroke-opacity:1.0;
fill:#00F;
fill-rule:evenodd;
fill-opacity:1.0;
}

.dimmed{
stroke-opacity:0.5;
fill-opacity:0.0;
}
]]></style>
</defs>
<g>


<g>

<path class="dimmed P" d="M7964,25320h5669m-5669,0v1905m0,-1401h5669m-5669,467h5669m-5669,467h5669m-5669,467h5669m-4968,-1401v1401m934,-1401v1401m785,-1401v1401m743,-1401v1401m1295,-1401v1401m1210,-1905v1905"/> 

<text class="dimmed M" x="10258" y="25695">CONN3</text>
<text class="M" x="8054" y="26172">Cav</text>
</g>
</g>

</svg>

升级 batik 和 fop 后,"dimmed"class 中的描边不透明度对文本 "CONN3" 没有任何影响。

出现上述错误是因为文字的描边不透明度默认指定为“1”。因此,无论我们传递给 stroke-oppacity 属性的值是什么,它都没有任何效果。

我已经通过修改fop源代码中PDFTextPainter.java的"applyColorAndPaint"并重新构建jar解决了这个问题。

private void applyColorAndPaint(TextPaintInfo tpi) { 
 Paint fillPaint = tpi.fillPaint; 
 Paint strokePaint = tpi.strokePaint; 
 Stroke stroke = tpi.strokeStroke; 
 int fillAlpha = PDFGraphics2D.OPAQUE; 
 int strokeAlpha = PDFGraphics2D.OPAQUE; 
 if (fillPaint instanceof Color) { 
      Color col = (Color) fillPaint; 
      pdf.applyColor(col, true); 
      fillAlpha = col.getAlpha(); 
 } 
 if (strokePaint instanceof Color) { 
      Color col = (Color) strokePaint; 
      pdf.applyColor(col, false); 
      strokeAlpha = col.getAlpha(); 
 } 
 pdf.applyPaint(fillPaint, true); 
 pdf.applyStroke(stroke); 

 if (strokePaint != null){ 
      pdf.applyPaint(strokePaint, false); 
 } 
 pdf.applyAlpha(fillAlpha, strokeAlpha); 
}