Krassnig / CodeDraw

CodeDraw is a beginner-friendly drawing library which can be used to create pictures, animations and even interactive applications.
https://krassnig.github.io/CodeDrawJavaDoc/
MIT License
18 stars 7 forks source link

[Suggestion] Font handling in CodeDraw: unavailable Fonts, and how to "work around" this issue #12

Closed rubyFeedback closed 1 year ago

rubyFeedback commented 1 year ago

Without any further ado, let me first copy/paste the error I ran into today:

Exception in thread "main" java.lang.IllegalArgumentException: The font Arial is not available on your device.
    at codedraw.TextFormat.setFontName(TextFormat.java:79)
    at VierGewinnt.returnTheDefaultFont(VierGewinnt.java:734)
    at VierGewinnt.enter_game_loop_for_the_GUI(VierGewinnt.java:860)
    at VierGewinnt.run(VierGewinnt.java:979)
    at VierGewinnt.<init>(VierGewinnt.java:174)
    at VierGewinnt.main(VierGewinnt.java:987)

So, the font Arial is not on my linux system, or perhaps it can not be found.

If I omit specifying the Arial font, the very same program works fine. IF I specify such a font that is not existing on my system, the program will instead terminate, with the above error message.

(If you are curious, I ran into the above for the last assignment of EPROG1 where we have to implement Vier Gewinnt.)

So it is not an issue for me; I can simply omit that line, or for EPROG1 presentation I can put it back in - no problem. But I think this could also be improved a little.

For instance, one obvious solution would be to use the default fallback / generic font. I assume CodeDraw already has a default font - after all if I do NOT specify the Arial font, the same code base works fine. So one approach could be to simply fall back towards that generic font. So the above would be displayed but not terminate the program, instead, the fallback font is used. (One can reason that it is my responsibility to handle such a use case via catch/throw, which I can understand - but this would require adding a few more lines of code, so my approach is to simply avoid Arial instead - less time investment.)

An alternative may be to add a way for users to specify a fallback font to use in this event. That way the old behaviour is retained, but we could override it via something like:

font.setDefaultFontName();

Or something like that. (Where font was: TextFormat font = new TextFormat(); )

There may be a few more ways to go about this, but I only wanted to a) report this small issue, and b) give a very few suggestions how this could be improved.

If you also have time, perhaps https://github.com/Krassnig/CodeDraw/blob/master/INTRODUCTION.md could mention a recommendation what to do when a font is not installed on a given operating system. Although perhaps most folks use windows, no clue.

Anyway, that was it, thanks for polishing CodeDraw!

rubyFeedback commented 1 year ago

Hmm. I am also trying other fonts:

"The font URW Palladio is not available on your device."

Or:

"The font VeraMono is not available on your device."

The font is here though:

/usr/share/fonts/ttf/VeraMono.ttf

So perhaps font handling may not be optimal if such a font can not be found? /usr/share/fonts/ttf/ is quite common on linux. I also ran "fc-cache -f -v" prior to trying to compile the .java file, but it seems it can not find such fonts. Omitting the name works, so it must work internally.

Krassnig commented 1 year ago

Hi,

you are correct, as of version 3.0.0 there is no way to check whether a font is available on a system except for catching an exception.

Starting from version 4.0.0 the setFontName() method will be changed to accept several fonts:

CodeDraw cd = new CodeDraw();
TextFormat tf = cd.getTextFormat();
tf.setFontName("Arial", "Verdana", "JetBrains Mono");

The first font in the list which is available on executing system will be used to draw text. If none of these are available, Javas default font Font.DIALOG will be used. CodeDraws default font will be Font.DIALOG, see Physical and Logical Fonts.

Additionally, the method TextFormat.getAllAvailableFontNames() and TextFormat.isFontNameAvailable(String fontName) will be added and a method to set the font name to its default value textFormat.setFontNameToDefault().

Previously, CodeDraw set the font name to 'Arial' internally, but there was no check to see if it was actually available (the check still happened when the setFontName(String) was called from the outside). I would assume that the Tests we did on Linux just defaulted to some other font and that was why we didn't notice.

Font not found issue:

I tried checking if there is a mismatch between the fonts listed with the linux command fc-list and on first glance they seem to match with the fonts in CodeDraw/Java. I also tried installing JetBrains Mono which also worked. CodeDraw uses the GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() method to get all available font names. This is also what is recommended in Physical and Logical Fonts.

What seems to be the issue is that the method GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() doesn't return the font you added to your system. Maybe you could ellaborate on how you added the font to your system, what linux distribution you used and what the list of fonts returned from GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() is. Maybe there is another method in Java which does return your font.

Thank you for your feedback 👌

Krassnig commented 1 year ago

Implemented in version 4.0.0