Java Examples for java.awt.Font.createFont

The following java examples will help you to understand the usage of java.awt.Font.createFont. These source code samples are taken from different open source projects.

Example 1
Project: mapfish-print-master  File: CustomFontLoader.java View source code
private void loadFont(final String path) throws FontFormatException, IOException {
    URL url = CustomFontLoader.class.getClassLoader().getResource(path);
    if (url != null) {
        File fontFile = new File(url.getFile());
        if (fontFile.canRead()) {
            java.awt.Font font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile);
            registerFont(font, fontFile);
        } else {
            throw new ConfigurationException("Can not read font file " + fontFile.getAbsolutePath());
        }
    } else {
        throw new ConfigurationException("Can not read font file " + path);
    }
}
Example 2
Project: neembuunow-master  File: Fonts.java View source code
static java.awt.Font initFont(String fntName) {
    try {
        //create the font to use. Specify the size!
        java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, Fonts.class.getResourceAsStream("fonts/" + fntName)).deriveFont(10f);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        //register the font
        ge.registerFont(customFont);
        return customFont;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (FontFormatException e) {
        e.printStackTrace();
    }
    return new JPanel().getFont();
}
Example 3
Project: lenient-pdf-compare-master  File: FontFactory.java View source code
public FontFile createFontFile(Stream fontStream, int fontType) {
    FontFile fontFile = null;
    if (foundFontEngine()) {
        try {
            Class fontClass = getNFontClass(fontType);
            if (fontClass != null) {
                // convert the stream to byte[]
                Class[] bytArrayArg = { byte[].class };
                Constructor fontClassConstructor = fontClass.getDeclaredConstructor(bytArrayArg);
                Object[] fontStreamBytes = { fontStream.getBytes() };
                if (fontStream.getBytes().length > 0) {
                    fontFile = (FontFile) fontClassConstructor.newInstance(fontStreamBytes);
                }
            }
        } catch (Throwable e) {
            logger.log(Level.FINE, "Could not create instance oof font file " + fontType, e);
        }
    } else if (awtFontLoading) {
        // see if the font file can be loaded with Java Fonts
        InputStream in = null;
        try {
            in = fontStream.getInputStreamForDecodedStreamBytes();
            // disabling create font as it brings the JVM down a little too often. 
            java.awt.Font javaFont = java.awt.Font.createFont(fontType, in);
            if (javaFont != null) {
                // create instance of OFont.
                fontFile = new OFont(javaFont);
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Successfully created embedded OFont: " + fontTypeToString(fontType));
                }
                try {
                    in.close();
                } catch (IOException e) {
                    logger.log(Level.FINE, "Error closing font stream.", e);
                }
            }
        } catch (Throwable e) {
            logger.log(Level.FINE, "Error reading font file with ", e);
            try {
                if (in != null)
                    in.close();
            } catch (Throwable e1) {
                logger.log(Level.FINE, "Error closing font stream.", e);
            }
        }
    }
    return fontFile;
}
Example 4
Project: CodenameOne-master  File: EditorTTFFont.java View source code
public void refresh() {
    if (fontFile != null && fontFile.exists() || nativeFontName != null) {
        try {
            java.awt.Font f;
            if (nativeFontName != null) {
                String res;
                switch(nativeFontName) {
                    case "native:MainThin":
                        res = "Thin";
                        break;
                    case "native:MainLight":
                        res = "Light";
                        break;
                    case "native:MainRegular":
                        res = "Medium";
                        break;
                    case "native:MainBold":
                        res = "Bold";
                        break;
                    case "native:MainBlack":
                        res = "Black";
                        break;
                    case "native:ItalicThin":
                        res = "ThinItalic";
                        break;
                    case "native:ItalicLight":
                        res = "LightItalic";
                        break;
                    case "native:ItalicRegular":
                        res = "Italic";
                        break;
                    case "native:ItalicBold":
                        res = "BoldItalic";
                        break;
                    case "native:ItalicBlack":
                        res = "BlackItalic";
                        break;
                    default:
                        throw new IllegalArgumentException("Unsupported native font type: " + nativeFontName);
                }
                InputStream is = getClass().getResourceAsStream("/com/codename1/impl/javase/Roboto-" + res + ".ttf");
                try {
                    f = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
                    is.close();
                } catch (Exception err) {
                    err.printStackTrace();
                    return;
                }
            } else {
                f = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile);
            }
            switch(sizeSetting) {
                case 0:
                    f = f.deriveFont((float) com.codename1.ui.Font.createSystemFont(com.codename1.ui.Font.FACE_SYSTEM, com.codename1.ui.Font.STYLE_PLAIN, com.codename1.ui.Font.SIZE_SMALL).getHeight());
                    break;
                case 1:
                    f = f.deriveFont((float) com.codename1.ui.Font.createSystemFont(com.codename1.ui.Font.FACE_SYSTEM, com.codename1.ui.Font.STYLE_PLAIN, com.codename1.ui.Font.SIZE_MEDIUM).getHeight());
                    break;
                case 2:
                    f = f.deriveFont((float) com.codename1.ui.Font.createSystemFont(com.codename1.ui.Font.FACE_SYSTEM, com.codename1.ui.Font.STYLE_PLAIN, com.codename1.ui.Font.SIZE_LARGE).getHeight());
                    break;
                case 3:
                    f = f.deriveFont(Display.getInstance().convertToPixels(Math.round(actualSize * 10), false) / 10.0f);
                    break;
                default:
                    f = f.deriveFont(actualSize);
                    break;
            }
            actualFont = new com.codename1.ui.Font(f);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    } else {
        actualFont = systemFont;
    }
}
Example 5
Project: fop-master  File: FOPFontFamilyResolverTestCase.java View source code
private LineMetrics getAWTLineMetrics() throws FontFormatException, IOException {
    File fontFile = new File("test/resources/fonts/ttf/DejaVuLGCSerif.ttf");
    java.awt.Font awtFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile).deriveFont(10f);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    BufferedImage dummyImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
    FontRenderContext frc = ge.createGraphics(dummyImage).getFontRenderContext();
    LineMetrics awtMetrics = awtFont.getLineMetrics("ABC", frc);
    return awtMetrics;
}
Example 6
Project: MinecraftGUI-Mod-master  File: FontRepository.java View source code
@Override
public void run() {
    try {
        InputStream is = getFile(fontUrl);
        ZipInputStream zis = new ZipInputStream(is);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            String fileName = entry.getName().toLowerCase();
            if (fileName.endsWith(".ttf") || fileName.endsWith(".otf")) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int i;
                while ((i = zis.read()) != -1) baos.write(i);
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                java.awt.Font font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new ByteArrayInputStream(baos.toByteArray()));
                ge.registerFont(font);
            }
        }
        is.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    urlLoaded.add(fontUrl);
    controller.fontDownloadFinished();
}
Example 7
Project: geogebra-master  File: FontLoaderD.java View source code
@Override
public Font loadFont(Object base, String name) throws ResourceParseException {
    InputStream fontIn = (InputStream) new Resource().loadResource(base, name);
    try {
        java.awt.Font f = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontIn).deriveFont((float) PIXELS_PER_POINT);
        GraphicsEnvironment graphicEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
        /**
			 * The following fails under java 1.5 graphicEnv.registerFont(f);
			 * dynamic load then
			 */
        if (shouldRegisterFonts) {
            graphicEnv.registerFont(f);
        // try {
        // Method registerFontMethod =
        // graphicEnv.getClass().getMethod("registerFont", new Class[] {
        // Font.class });
        // if ((Boolean) registerFontMethod.invoke(graphicEnv, new
        // Object[] { f }) == Boolean.FALSE) {
        // System.err.println("Cannot register the font " +
        // f.getFontName());
        // }
        // } catch (Exception ex) {
        // if (!registerFontExceptionDisplayed) {
        // System.err.println("Warning: Jlatexmath: Could not access to
        // registerFont. Please update to java 6");
        // registerFontExceptionDisplayed = true;
        // }
        // }
        }
        return new FontD(f);
    } catch (Exception e) {
        throw new XMLResourceParseException("FontLoader" + ": FontLoader '" + name + "'. Error message: " + e.getMessage());
    } finally {
        try {
            if (fontIn != null) {
                fontIn.close();
            }
        } catch (IOException ioex) {
            throw new RuntimeException("Close threw exception", ioex);
        }
    }
}
Example 8
Project: xith3d-master  File: HUDFont.java View source code
/**
     * Returns the underlying {@link java.awt.Font}.
     * 
     * @param hud
     * 
     * @return the underlying {@link java.awt.Font}
     */
public final java.awt.Font getAWTFont(HUD hud) {
    boolean useScaling = useFontScaling && hud.hasCustomResolution();
    if (useScaling) {
        if ((this.awtFont == null) || (this.lastHUDHeight != (int) hud.getResY())) {
            if (url == null) {
                this.awtFont = new java.awt.Font(name, style.getAWTStyle(), Math.round(size * hud.getHeight() / hud.getResY()));
            } else if (this.awtFont == null) {
                try {
                    this.awtFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, url.openStream()).deriveFont(Math.round(size * hud.getHeight() / hud.getResY())).deriveFont(style.getAWTStyle(), size);
                } catch (Throwable t) {
                    throw new Error(t);
                }
            } else {
                this.awtFont = this.awtFont.deriveFont(style.getAWTStyle(), size);
            }
            this.metrics = null;
            this.lastHUDHeight = (int) hud.getResY();
        }
    } else {
        if (awtFont == null) {
            if (url == null) {
                this.awtFont = new java.awt.Font(name, style.getAWTStyle(), size);
            } else {
                try {
                    this.awtFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, url.openStream()).deriveFont(style.getAWTStyle(), size);
                } catch (Throwable t) {
                    throw new Error(t);
                }
            }
            this.metrics = null;
        }
    }
    return (awtFont);
}
Example 9
Project: geotools_trunk-master  File: FontCache.java View source code
/**
     * Tries to load the specified font name as a URL
     * 
     * @param fontUrl
     * @return
     */
java.awt.Font loadFromUrl(String fontUrl) {
    // may be its a file or url
    InputStream is = null;
    if (fontUrl.startsWith("http") || fontUrl.startsWith("file:")) {
        try {
            URL url = new URL(fontUrl);
            is = url.openStream();
        } catch (MalformedURLException mue) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.info("Bad url in SLDStyleFactory " + fontUrl + "\n" + mue);
            }
        } catch (IOException ioe) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.info("IO error in SLDStyleFactory " + fontUrl + "\n" + ioe);
            }
        }
    } else {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("not a URL");
        }
        File file = new File(fontUrl);
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException fne) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.info("Bad file name in SLDStyleFactory" + fontUrl + "\n" + fne);
            }
        }
    }
    // make sure we have anything to load
    if (is == null) {
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("null input stream, could not load the font");
        }
        return null;
    }
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.finest("about to load");
    }
    try {
        return java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
    } catch (FontFormatException ffe) {
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("Font format error in SLDStyleFactory " + fontUrl + "\n" + ffe);
        }
        return null;
    } catch (IOException ioe) {
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("IO error in SLDStyleFactory " + fontUrl + "\n" + ioe);
        }
        return null;
    }
}
Example 10
Project: OpenGrave-master  File: Resources.java View source code
public static Font getFont(File f, int size) {
    Font font = null;
    String[] nameSplit = f.getName().split(".");
    int last = nameSplit.length - 1;
    if (// True type
    nameSplit[last].equalsIgnoreCase("ttf")) {
        String id = f.getPath() + ":" + size;
        if (loadedFonts.containsKey(id)) {
            return loadedFonts.get(id);
        }
        java.awt.Font ttf = null;
        try {
            ttf = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, f).deriveFont(size);
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ttf == null) {
            return null;
        }
        font = new Font(ttf, true);
        loadedFonts.put(id, font);
        return font;
    } else if (// Shit old font format
    nameSplit[last].equalsIgnoreCase("fnt")) {
        String id = f.getPath();
        if (loadedFonts.containsKey(id)) {
            return loadedFonts.get(id);
        }
        font = loadFont(f.getPath());
        loadedFonts.put(id, font);
        return font;
    }
    return font;
}
Example 11
Project: glimpse-master  File: FontUtils.java View source code
private static Font readFont(String filename) {
    try {
        InputStream stream = null;
        try {
            stream = StreamOpener.fileThenResource.openForRead(filename);
            return createFont(Font.TRUETYPE_FONT, stream);
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    } catch (FontFormatException e) {
        throw new RuntimeException("Could not load font.", e);
    } catch (IOException e) {
        throw new RuntimeException("Could not load font.", e);
    }
}