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:
public static void reloadSupportedFonts() {
FontFamiliesCache.INSTANCE.supportedFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
}
Then clients would be able to call registerFont() and then reloadSupportedFonts().
- add a field
List<String> customFontFamilies to which users can add font families and make isSupportedFontFamily check in both collections
- what about removing supported fonts?
- Allow disabling the isSupportedFontFamily check, making all fonts supported.
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):
For those who face the same issue, note that, to make custom fonts work, you also need to register them in the GraphicsEnvironment:
Some suggestions on how to make this a bit more flexible:
List<String> customFontFamiliesto which users can add font families and makeisSupportedFontFamilycheck in both collections