Skip to content

Instantly share code, notes, and snippets.

@vincenzopalazzo
Last active April 7, 2020 21:07
Show Gist options
  • Select an option

  • Save vincenzopalazzo/4d22b48d3b1c8087f86b054ef62918ce to your computer and use it in GitHub Desktop.

Select an option

Save vincenzopalazzo/4d22b48d3b1c8087f86b054ef62918ce to your computer and use it in GitHub Desktop.
The Document import inside the change inside JMars to load the personal font with the library

Load Personal Font with material-ui-swing

In the new version of the library, so material-ui-swing-1.1.1 (0.3) is possible load a personal font with the library directly in the clien application such as JMars 5.

The building the font with material library is very fast because the instance of the font is the same, the class MaterialFontsFactory is use the facory pattern and store the font inside a map and the class doesn't build the same font two times.

New API MaterialFontsFacoty

The previus version of the library were able only to load the Noto-Sans font (font library) but it didn't able to load a font from InputStream or an relative path.

With the new API the class have tre different method, like:

  • public FontUIResource getFont(MaterialTypeFont typeFont)
  • public FontUIResource getFontWithPath(String path)
  • public FontUIResource getFontWithStream(InputStream stream)

P.S: Remember that the class MaterialFontesFactory is an Singleton, so you should get the instance of the class with this code: MaterialFontFactory.getInstance()

Traning with JMars

JMars is using the following code to load the personal font

public static Font getFont(String fileName) {
		try {
			Font customFont = Font
					.createFont(Font.PLAIN, Main.getResourceAsStream(PATH_TO_MATERIAL_FONTS + fileName))
					.deriveFont(FONTS.ROBOTO.fontSize());
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			ge.registerFont(customFont);
			return customFont;
		} catch (IOException e) {
			return null;
		} catch (FontFormatException e) {
			return null;
		}
	}

This code is an basic code and should be include different bug with different monitor.

Why JMars should use the MaterialFontsFactory?

material-ui-library include different bug fixeng to the personal fonts, like

You can look the font pixeled in the following image

Pixeled font result.

Selection_044

Pixeled font fixed.

Selection_045

How import this change inside JMars?

To import the change easy inside JMars you can change the getFont mothod with this code:

 public static Font getFont(String fileName) {
    Font customFont = MaterialFontFactory.getInstance().getFontWithStream(Main.getResourceAsStream(PATH_TO_MATERIAL_FONTS + fileName));
    return customFont;
 }

If the Input stream is wrong the library throws 2 different RuntimeException, like:

  • IllegalArgumentException: if the stream is null
  • RuntimeException: if there is error in the build the font, an example: If the input stream contains an image and not an font.

P.S: The Material library build Only one instance each type font and the dimension of font is calculate with the following method.

float size = defaultSize * Math.min(Toolkit.getDefaultToolkit().getScreenResolution(), 96) /72;

Where the defaultSize is by default = 11. (FIXED dimension, at the moment you should be able to change the value)

Know Bug

If you use the Roboto font, you can have other problem with JDK unitll 13, this is described the bug: https://bugs.openjdk.java.net/browse/JDK-8236996

P.S: The bug was fixed in the 2020-01-27 13:06 and it will be release in the JDK 15, unfortunatly.

Download material-1.1.1_0.3

Author

Vincent Palazzo: https://github.com/vincenzopalazzo

Document License

Sponsors this work

To support my work in this version you can use

Donation Custom badge

or

Github donations

This document is developer for Arizona State University.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment