-
-
Notifications
You must be signed in to change notification settings - Fork 19
Ability to provide custom fonts #149
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Hi, great project!
Currently, it is impossible to provide custom fonts (fonts generated at runtime or loaded from files at runtime) for the SVGs after the start of the application. This is because, once set, field FontResolver.FontFamiliesCache.supportedFonts (link) can no longer be updated.
For my applications, I had to use reflection to work around this (Java 8):
Class<?> cacheClass = Class.forName("com.github.weisj.jsvg.attributes.font.FontResolver$FontFamiliesCache");
Object instance = Enum.valueOf((Class) cacheClass, "INSTANCE");
Field field = cacheClass.getDeclaredField("supportedFonts");
field.setAccessible(true);
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(instance, fontFamilies);For those who face the same issue, note that, to make custom fonts work, you also need to register them in the GraphicsEnvironment:
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);Some suggestions on how to make this a bit more flexible:
- allow reloading the supported fonts in FontResolver:
Then clients would be able to call registerFont() and then reloadSupportedFonts().
public static void reloadSupportedFonts() { FontFamiliesCache.INSTANCE.supportedFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); }
- add a field
List<String> customFontFamiliesto which users can add font families and makeisSupportedFontFamilycheck in both collections - what about removing supported fonts?
- Allow disabling the isSupportedFontFamily check, making all fonts supported.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request