Laden von Schriftarten mit eigenem Namen möglich

This commit is contained in:
ngb 2022-07-25 19:05:04 +02:00
parent 617b915874
commit bd2364a8df
1 changed files with 36 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import schule.ngb.zm.util.Log;
import java.awt.Font; import java.awt.Font;
import java.awt.FontFormatException; import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Map; import java.util.Map;
@ -14,20 +15,50 @@ public class FontLoader {
private static final Map<String, Font> fontCache = new ConcurrentHashMap<>(); private static final Map<String, Font> fontCache = new ConcurrentHashMap<>();
/**
* Lädt eine Schrift aus einer Datei.
* <p>
* Die Schrift wird unter ihrem Dateinamen in den Schriftenspeicher geladen
* und kann danach in der Zeichenmaschine benutzt werden.
*
* Ein Datei mit dem Namen "fonts/Font-Name.ttf" würde mit dem Namen
* "Font-Name" geladen und kann danach zum Beispiel in einem
* {@link schule.ngb.zm.shapes.Text} mit {@code text.setFont("Font-Name");}
* verwendet werden.
*
* @param source
* @return
*/
public static Font loadFont( String source ) { public static Font loadFont( String source ) {
String name = source;
// Dateipfad entfernen
int lastIndex = source.lastIndexOf(File.separatorChar);
if( lastIndex > -1 ) {
source.substring(lastIndex + 1);
}
// Dateiendung entfernen
lastIndex = name.lastIndexOf('.');
if( lastIndex > -1 ) {
name = name.substring(0, lastIndex);
}
return loadFont(name, source);
}
public static Font loadFont( String name, String source ) {
Objects.requireNonNull(source, "Font source may not be null"); Objects.requireNonNull(source, "Font source may not be null");
if( source.length() == 0 ) { if( source.length() == 0 ) {
throw new IllegalArgumentException("Font source may not be empty."); throw new IllegalArgumentException("Font source may not be empty.");
} }
if( fontCache.containsKey(source) ) { if( fontCache.containsKey(name) ) {
LOG.trace("Retrieved font <%s> from font cache.", source); LOG.trace("Retrieved font <%s> from font cache.", name);
return fontCache.get(source); return fontCache.get(name);
} }
// Look for System fonts // Look for System fonts
Font font = Font.decode(source); Font font = Font.decode(source);
if( font != null && source.toLowerCase().contains(font.getFamily().toLowerCase()) ) { if( font != null && source.toLowerCase().contains(font.getFamily().toLowerCase()) ) {
fontCache.put(name, font);
fontCache.put(source, font); fontCache.put(source, font);
LOG.debug("Loaded system font for <%s>.", source); LOG.debug("Loaded system font for <%s>.", source);
return font; return font;
@ -40,10 +71,11 @@ public class FontLoader {
font = Font.createFont(Font.TRUETYPE_FONT, in).deriveFont(Font.PLAIN); font = Font.createFont(Font.TRUETYPE_FONT, in).deriveFont(Font.PLAIN);
if( font != null ) { if( font != null ) {
fontCache.put(name, font);
fontCache.put(source, font); fontCache.put(source, font);
//ge.registerFont(font); //ge.registerFont(font);
} }
LOG.debug("Loaded custom font from <%s>.", source); LOG.debug("Loaded custom font from source <%s>.", source);
} catch( IOException ioex ) { } catch( IOException ioex ) {
LOG.error(ioex, "Error loading custom font file from source <%s>.", source); LOG.error(ioex, "Error loading custom font file from source <%s>.", source);
} catch( FontFormatException ffex ) { } catch( FontFormatException ffex ) {