【问题标题】:How can I set a font-family instead of font when using Batik's SVGGraphics2D?使用 Batik 的 SVGGraphics2D 时如何设置字体系列而不是字体?
【发布时间】:2020-09-02 14:09:05
【问题描述】:

我目前使用 Batik 的 SVGGraphics2D 有以下代码:

...
final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(svgGeneratorContext, false);
svgGraphics2D.setSVGCanvasSize(new Dimension(width, height));
svgGraphics2D.setFont(font);
...

因此,如果在执行代码的系统上font 是可用的Font,则会将正确的属性添加到生成的 SVG 文件中。

但是,如果缺少字体(例如 linux 机器上的“Verdana”),则使用默认字体(添加font-family:'Dialog')。

因此,我不想指定字体,而是传递一个字体系列,以便在生成的 SVG 中包含 font-family="DejaVu Sans,Verdana,Geneva,sans-serif"。如果我没记错,API 只接受 Font 参数,我怎么能做到这一点?

我希望有比使用 xslt 转换 xml 输出更简单的方法。

提前谢谢你。

【问题讨论】:

  • 关于赏金:我正在寻找添加 CSS 属性的通用解决方案,而不仅仅是字体。 SVGGraphics2D 的使用是绝对必要的。
  • stackoverflow.com/questions/38890844/…stackoverflow.com/questions/14314035/… 提出基本相同的问题,但没有提供答案。有可能 Batic 已经完全死了,只是在 Apache 墓地里不寒而栗,你应该搜索另一个 Java+SVG 库,或者可能会完全改变你的方法。
  • @OlegEstekhin 这就是它的样子。虽然一个真正的答案会很好,但我更有可能接受某种可以完成工作的可疑黑客,只要它是模糊合理的。

标签: java batik


【解决方案1】:

如果你有一个(许可的)字体文件,比如资源,你可以这样做:

    InputStream is = SomeClass.getResourceAsStream("/fonts/DejaVu Sans.ttf");
    Font font = Font.createFont(Font.TRUETYPE_FONT, is);
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(font);

这是使用自己的字体交付应用程序的一般方式。

然后在 SVG 中,您可以为独立的 svg 文件嵌入准确的字体。

我想知道这是不可能的:

font-family="'DejaVu Sans',Verdana,Geneva,sans-serif"

因为那将是 CSS 方式。

【讨论】:

  • 你所描述的是 OP 已经拥有的,一种单一的字体。嵌入字体是解决 OP 问题的一种解决方法,但如果出现,我将奖励一个解决方案,该解决方案可以更好地控制 CSS。谢谢!
  • @GKFX 确实我有足够的积分。但我想提一下在没有安装操作系统的情况下为应用程序提供字体的能力,以及为最终的 svg 嵌入它的能力。 CSS 会很好。
【解决方案2】:

给定一个widthheight 和 Swing 组件 (label),以下代码将导出一个在根 svg 元素上设置了 viewBoxfont-family 属性的 SVG 文档:

final var dom = getDOMImplementation();
final var ns = "http://www.w3.org/2000/svg";
final var doc = dom.createDocument( ns, "svg", null );
final var context = createDefault( doc );
context.setPrecision( 5 );

final var generator = new SVGGraphics2D( context, false );
generator.setSVGCanvasSize( new Dimension( width, height ) );
label.paintComponent( generator );

Element root = generator.getRoot();
final String viewBox = format( "0 0 %d %d", width, height );
root.setAttribute( SVG_VIEW_BOX_ATTRIBUTE, viewBox );
root.setAttribute( "font-family", "Arial" );

try( final var out = new OutputStreamWriter(
    new FileOutputStream( "/tmp/saved.svg" ), UTF_8 ) ) {
  generator.stream( root, out );
}

生产:

<svg
  stroke-dasharray="none"
  shape-rendering="auto"
  xmlns="http://www.w3.org/2000/svg"
  font-family="Arial"
  width="305"
  text-rendering="auto"
  fill-opacity="1"
  contentScriptType="text/ecmascript"
  color-interpolation="auto"
  color-rendering="auto"
  preserveAspectRatio="xMidYMid meet"
  font-size="12px"
  viewBox="0 0 3 3"
  fill="black"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  stroke="black"
  image-rendering="auto"
  stroke-miterlimit="10"
  zoomAndPan="magnify"
  version="1.0"
  stroke-linecap="square"
  stroke-linejoin="miter"
  contentStyleType="text/css"
  font-style="normal"
  height="311"
  stroke-width="1"
  stroke-dashoffset="0"
  font-weight="normal"
  stroke-opacity="1">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多