I currently have the following code using Batik's SVGGraphics2D:
...
final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(svgGeneratorContext, false);
svgGraphics2D.setSVGCanvasSize(new Dimension(width, height));
svgGraphics2D.setFont(font);
...
As a result, if font
is an available Font
on the system on which the code is executed, the correct attribute is added to the resulting SVG file.
However, if the font is missing (for instance "Verdana" on a linux box) a default font is used (font-family:'Dialog'
is added).
Thus, instead of specifying a font, I would like to pass a font-family in order to have font-family="DejaVu Sans,Verdana,Geneva,sans-serif"
in the resulting SVG. How can I achieve this knowing that, if I'm not wrong, the API only accepts Font
parameters ?
I hope that there's a easier way than using a xslt to transform the xml output.
Thank you in advance.
If you have a (licensed) font file, say as resource, you might do:
InputStream is = SomeClass.getResourceAsStream("/fonts/DejaVu Sans.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
This is a general way to deliver an application with its own font.
Then in the SVG you can embed the exact font for stand-alone svg files.
I wonder that it is not possible to do:
font-family="'DejaVu Sans',Verdana,Geneva,sans-serif"
as that would be the CSS way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With