jsvg icon indicating copy to clipboard operation
jsvg copied to clipboard

Text/Font specific questions/requests

Open mlaggner opened this issue 4 months ago • 6 comments

I am just about to migrate from SVG Salamander to jsvg, but I have a kinda hard time to solve "at runtime" specific tasks.

I do have some template SVGs where I render dynamic text at runtime. There are two tasks where I do have problems at the moment:

  1. Set the text in a <text> node using your Processor. While the Processor is able to put/change attributes, I did not find a way to change the tag contents (characters) at runtime. For now I read the SVG data as text, modify the content and load it via an Inputstream - not elegant, but works

  2. It looks like you kinda hard wired the default font family "Serif" for all texts, where no family is specified in the SVG elements. I did not find a way to alter that (I'd rather like to use "Dialog" which fits more to my app)

Thanks for any input from your side

mlaggner avatar Jul 25 '25 09:07 mlaggner

I believe that both of these things are currently not possible (If you want to stay within the boundaries of the stable public API). The Serif font default is to match defaults of most other rendering engines. It shouldn't be too difficult to pass a default font through the PlatformSupport.

The text node is more tricky, as text is not stored contiguously in the internal representation (this is needed to accommodate textpath tspan etc.).

weisJ avatar Jul 25 '25 12:07 weisJ

I do have some template SVGs where I render dynamic text at runtime.

For a similar use case, I use a DOM source that I manipulate as necessary. I then feed it to JSVG using SVGLoader.load(XMLInput, URI, LoaderContext):

https://github.com/weisJ/jsvg/blob/0d8beef8fb854dfbad0ead447ca640a4bef71e5a/jsvg/src/main/java/com/github/weisj/jsvg/parser/SVGLoader.java#L87-L90

where XMLInput.createReader() is implemented like:

Document doc;
...
return XMLInputFactory.newInstance().createXMLEventReader(DOMSource(doc));

Note, XMLInputFactory.createXMLEventReader(Source) is an optional operation, and is not implemented in the JDK built-in StAX implementation (for some reason). Woodstox, on the other hand, supports it. I have also implemented a fallback adapter using TrAX transformation.

This way, I avoid serializing DOM changes to XML (text) just to be parsed by JSVG.

stanio avatar Aug 15 '25 09:08 stanio

I have added an experimental API which allows to modify the text content of elements using the pre-processor. On DomElement you can call textContent(), which gives you an objects that exposes the fragments of text before the first and after each child element. It is available with in the latest snapshot.

weisJ avatar Sep 02 '25 09:09 weisJ

Thanks for the update - I hope I find some time next week to have a look at those changes.

A quick question meanwhile: does this also allows us to set the "default" text font? I was kinda unable to set it in my tests (this is what hurts most here).

mlaggner avatar Sep 06 '25 12:09 mlaggner

Yes you can change the default font size by implementing a custom PlatformSupport. If you are rendering to an AWT component you can simply set the font on the component itself.

weisJ avatar Sep 07 '25 13:09 weisJ

Thanks for this suggestion - after looking at the latest code I finally know what you are talking about 😆 I think I can solve almost everything using the latest snapshot. I will report back when I finished adopting my code to use the recently added features

mlaggner avatar Oct 08 '25 07:10 mlaggner