Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a font-family instead of font when using Batik's SVGGraphics2D?

Tags:

java

batik

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.

like image 680
Kraal Avatar asked Oct 31 '22 08:10

Kraal


1 Answers

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.

like image 57
Joop Eggen Avatar answered Nov 14 '22 02:11

Joop Eggen