001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.awt.Font;
005import java.awt.FontFormatException;
006import java.awt.GraphicsEnvironment;
007import java.io.IOException;
008import java.io.InputStream;
009import java.util.Arrays;
010import java.util.Collection;
011
012import org.openstreetmap.josm.io.CachedFile;
013
014/**
015 * Custom fonts manager that provides some embedded fonts to ensure
016 * a common rendering on different platforms.
017 * @since 7383
018 */
019public final class FontsManager {
020
021    /**
022     * List of fonts embedded into JOSM jar.
023     */
024    private static final Collection<String> INCLUDED_FONTS = Arrays.asList(
025            "DroidSans.ttf",
026            "DroidSans-Bold.ttf"
027    );
028
029    private FontsManager() {
030        // Hide constructor for utility classes
031    }
032
033    /**
034     * Initializes the fonts manager.
035     */
036    public static void initialize() {
037        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
038        for (String fontFile : INCLUDED_FONTS) {
039            String url = "resource://data/fonts/"+fontFile;
040            try (CachedFile cf = new CachedFile(url); InputStream i = cf.getInputStream()) {
041                ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, i));
042            } catch (IOException | FontFormatException ex) {
043                throw new JosmRuntimeException(ex);
044            }
045        }
046    }
047}