jfree / jfreesvg

A fast, lightweight Java library for creating Scalable Vector Graphics (SVG) output.
http://www.jfree.org/jfreesvg
GNU General Public License v3.0
319 stars 58 forks source link

How to add "letter-spacing"? #43

Closed faystmax closed 2 years ago

faystmax commented 2 years ago

Hello! I use a font that have TRACKING = 0.45. But when i create svg and 'drawString' with this font, it seems that this property is being ignored. I looked through svg documentation and found that i should use "letter-spacing". So, how i can add letter-spacing to my text?

jfree commented 2 years ago

There's no support for this in JFreeSVG. Do you know if this character spacing is supported in Java2D? I ask because the goal of JFreeSVG is to replicate the output that you would get from Java2D.

If your focus is just SVG output, then you could achieve this (with some code changes to JFreeSVG) by adding a new rendering hint (see the SVGHints class) and modifying the drawString() method to read the value and add the attribute if the hint is present. Search the sources for SVGHints.KEY_TEXT_RENDERING for an example.

faystmax commented 2 years ago

- Do you know if this character spacing is supported in Java2D? Yes. Java2D actually supports this. I have a font defined like this: private static final Font POST_FONT = RUSSIA_FONT.deriveFont(ImmutableMap.of(TextAttribute.TRACKING, 0.45)); And Java2D succesfully render it correctly.

I also tried what you suggest by using custom SVGHints and modifying the drawString() method. But after some research i found SVGHints.KEY_DRAW_STRING_TYPE. Setting this hint to value SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR solves my problem.

jfree commented 2 years ago

Thanks for the info. I just added support for TextAttribute.TRACKING and released JFreeSVG 5.0.2.

Drawing strings as vectors (using the hint you mentioned) will also work, the only downside is that it increases the size of the SVG significantly (depending on how much text you have).

faystmax commented 2 years ago

Drawing strings as vectors (using the hint you mentioned) will also work, the only downside is that it increases the size of the SVG significantly (depending on how much text you have). Yeah. I know that, but i dont have much text to render so its not a problem for me)

Thanks for the info. I just added support for TextAttribute.TRACKING and released JFreeSVG 5.0.2. Oh. Thanks very much! Gonna try it later!