Java Code Examples for java.awt.Font#createFont()
The following examples show how to use
java.awt.Font#createFont() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PFontHandler.java From PolyGlot with MIT License | 6 votes |
/** * Attempts to load the given font from the OS's font folder (due to Java's * ligature problems) * * @param fontFamily * @return returns loaded font file on success, null otherwise */ public static Font loadFontFromOSFileFolder(String fontFamily) { Font ret = null; try { File fontFile = getFontFile(fontFamily); if (fontFile != null && fontFile.exists()) { ret = Font.createFont(Font.TRUETYPE_FONT, fontFile); Map attributes = ret.getAttributes(); attributes.put(TextAttribute.LIGATURES, TextAttribute.LIGATURES_ON); ret = ret.deriveFont(attributes); } } catch (Exception e) { IOHandler.writeErrorLog(e); // do nothing here. Failure means returning null } return ret; }
Example 2
Source File: IceLeaf.java From snowblossom with Apache License 2.0 | 6 votes |
public void run() { try { fixed_font = Font.createFont(Font.TRUETYPE_FONT, IceLeaf.class.getResourceAsStream( getResourceBasePath() + "/iceleaf-ui/resources/font/Hack-Regular.ttf")); bold_fixed_font = Font.createFont(Font.TRUETYPE_FONT, IceLeaf.class.getResourceAsStream( getResourceBasePath() +"/iceleaf-ui/resources/font/Hack-Bold.ttf")); var_font = new Font("Verdana", 0, 12); //IceLeaf.setUIFont(new Font("Verdana", 0, 12)); //UIManager.setLookAndFeel(new javax.swing.plaf.nimbus.NimbusLookAndFeel()); } catch(Exception e) { e.printStackTrace(); } }
Example 3
Source File: PFontHandler.java From PolyGlot with MIT License | 5 votes |
/** * Fetches and returns LCD style font NOTE 1: the font returned is very * small, use deriveFont() to make it a usable size NOTE 2: this is a * non-static method due to an input stream restriction * * @return LCD display font * @throws java.awt.FontFormatException if font corrupted * @throws java.io.IOException if unable to load font */ private Font getLcdFontInternal() throws FontFormatException, IOException { try (InputStream tmp = this.getClass().getResourceAsStream(PGTUtil.LCD_FONT_LOCATION)) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font ret = Font.createFont(Font.TRUETYPE_FONT, tmp); if (ret != null) { ge.registerFont(ret); } return ret; } }
Example 4
Source File: UIRes.java From RipplePower with Apache License 2.0 | 5 votes |
public static Font getFont(String fontName, int type, int style, int size) { try { Font font = Font.createFont(type, UIRes.getStream(fontName)); font = font.deriveFont(style, size); final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(font); return font; } catch (Exception e) { e.printStackTrace(); } return new Font(Font.DIALOG, style, size); }
Example 5
Source File: PFontHandler.java From PolyGlot with MIT License | 5 votes |
/** * Fetches and returns a font from a given location * * @param filePath * @return collected font * @throws java.awt.FontFormatException * @throws java.io.IOException */ public static Font getFontFromFile(String filePath) throws FontFormatException, IOException { File fontFile = new File(filePath); Font ret = Font.createFont(Font.TRUETYPE_FONT, fontFile); Map attributes = ret.getAttributes(); attributes.put(TextAttribute.LIGATURES, TextAttribute.LIGATURES_ON); ret = ret.deriveFont(attributes); return ret; }
Example 6
Source File: StatusImage.java From jenkins-status-badges-plugin with MIT License | 5 votes |
public int measureText( String text ) throws FontFormatException, IOException { URL fontURL = new URL( Jenkins.getInstance().pluginManager.getPlugin( "status-badges" ).baseResourceURL, "fonts/verdana.ttf" ); InputStream fontStream = fontURL.openStream(); Font defaultFont = Font.createFont( Font.TRUETYPE_FONT, fontStream ); defaultFont = defaultFont.deriveFont( 11f ); Canvas canvas = new Canvas(); FontMetrics fontMetrics = canvas.getFontMetrics( defaultFont ); return fontMetrics.stringWidth( text ); }
Example 7
Source File: AsposeFontManager.java From iaf with Apache License 2.0 | 5 votes |
/** * Get the font. When retrieving the font fails it is logged and * <code>null</code> is returned. * * @param fontFile File location of the font to be loaded * @return the font or <code>null</code>. */ private Font createFont(File fontFile) { String name = fontFile.getName(); if (!name.toLowerCase().endsWith(TRUETYPE_FONT_EXT)) { throw new IllegalArgumentException("Unexpected extension! (file: " + name + " expected extension: " + TRUETYPE_FONT_EXT + ")"); } Font result = null; try { result = Font.createFont(Font.TRUETYPE_FONT, fontFile); } catch (FontFormatException | IOException e) { log.error("Loading font failed! (file: " + name + ")", e); } return result; }
Example 8
Source File: FontDisposeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { // The bug only happens with Type 1 fonts. The Ghostscript font files // should be commonly available. From distro pacakge or // ftp://ftp.gnu.org/gnu/ghostscript/gnu-gs-fonts-other-6.0.tar.gz // Pass pfa/pfb font file as argument String path = args[0]; // Load InputStream stream = new FileInputStream(path); Font font = Font.createFont(Font.TYPE1_FONT,stream); // Ensure native bits have been generated BufferedImage img = new BufferedImage(100,100, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); FontRenderContext frc = g2d.getFontRenderContext(); font.getLineMetrics("derp",frc); // Force disposal - // System.gc() is not sufficient. Field font2DHandleField = Font.class.getDeclaredField("font2DHandle"); font2DHandleField.setAccessible(true); sun.font.Font2DHandle font2DHandle = (sun.font.Font2DHandle)font2DHandleField.get(font); sun.font.Font2D font2D = font2DHandle.font2D; sun.font.Type1Font type1Font = (sun.font.Type1Font)font2D; Method getScalerMethod = sun.font.Type1Font.class.getDeclaredMethod("getScaler"); getScalerMethod.setAccessible(true); sun.font.FontScaler scaler = (sun.font.FontScaler)getScalerMethod.invoke(type1Font); // dispose should not crash due to double free scaler.dispose(); }
Example 9
Source File: SinglePointFont.java From mil-sym-java with Apache License 2.0 | 5 votes |
/** * Font used to render single point tactical graphics * @param size * @return */ public Font getSPFont(float size) { //load font from resource InputStream fontStream = _instance.getClass().getClassLoader().getResourceAsStream("FONTS/SinglePoint.ttf"); //InputStream fontStream = _spFontStream; Font newFont = null; try { //create font newFont = Font.createFont(Font.TRUETYPE_FONT, fontStream); } catch(FontFormatException ffe) { ErrorLogger.LogException(this.getClass().getName() ,"getSPFont()", new RendererException("SPFont failed to load.", ffe)); } catch(IOException ioe) { ErrorLogger.LogException(this.getClass().getName() ,"getSPFont()", new RendererException("SPFont failed to load.", ioe)); } catch(Exception exc) { ErrorLogger.LogException(this.getClass().getName() ,"getSPFont()", new RendererException("SPFont failed to load.", exc)); } //resize font newFont = newFont.deriveFont(Font.TRUETYPE_FONT, size); //return font return newFont; }
Example 10
Source File: DSSFileFont.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
private Font deriveJavaFont() { try (InputStream is = fileFont.openStream()) { Font javaFont = Font.createFont(Font.TRUETYPE_FONT, is); return javaFont.deriveFont(size); } catch (IOException | FontFormatException e) { throw new DSSException(String.format("The Java font cannot be instantiated. Reason : %s", e.getMessage()), e); } }
Example 11
Source File: Fonts.java From WorldGrower with GNU General Public License v3.0 | 5 votes |
private static Font createFont() { try { Font font = Font.createFont(Font.TRUETYPE_FONT, ImageInfoReader.class.getResourceAsStream("/ufonts.com_tw-cen-mt.ttf")); Font derivedFont = font.deriveFont((float)FONT_SIZE); GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment(); genv.registerFont(derivedFont); return derivedFont; } catch (FontFormatException | IOException e) { throw new IllegalStateException(e); } }
Example 12
Source File: LFont.java From RipplePower with Apache License 2.0 | 5 votes |
public static LFont getAssetsFont(String file, int style, int size) { try { String name = "assets" + (file + style + size).toLowerCase(); LFont o = fonts.get(name); if (o == null) { o = new LFont(); o.name = file; o.style = style; o.size = size; o.antialiasing = true; if (o.antialiasing) { g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } else { g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); } Font trueFont = Font.createFont(0, UIRes.getStream(file)); Font baseFont = trueFont.deriveFont(style, size); o.fontMetrics = g2d.getFontMetrics(baseFont); o.initialized = true; fonts.put(name, o); } return o; } catch (Exception e) { e.printStackTrace(); } return null; }
Example 13
Source File: Fonts.java From opsu with GNU General Public License v3.0 | 5 votes |
/** * Initializes all fonts. * @throws SlickException if ASCII glyphs could not be loaded * @throws FontFormatException if any font stream data does not contain the required font tables * @throws IOException if a font stream cannot be completely read */ public static void init() throws SlickException, FontFormatException, IOException { float fontBase = 12f * GameImage.getUIscale(); Font javaFontMain = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Options.FONT_MAIN)); Font javaFontBold = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Options.FONT_BOLD)); Font javaFontCJK = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Options.FONT_CJK)); Font fontMain = javaFontMain.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3)); Font fontBold = javaFontBold.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3)); Font fontCJK = javaFontCJK.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3)); DEFAULT = new UnicodeFont(fontMain); BOLD = new UnicodeFont(fontBold.deriveFont(Font.BOLD)); XLARGE = new UnicodeFont(fontMain.deriveFont(fontBase * 3)); LARGE = new UnicodeFont(fontMain.deriveFont(fontBase * 2)); MEDIUM = new UnicodeFont(fontMain.deriveFont(fontBase * 3 / 2)); MEDIUMBOLD = new UnicodeFont(fontBold.deriveFont(Font.BOLD, fontBase * 3 / 2)); SMALL = new UnicodeFont(fontMain.deriveFont(fontBase)); SMALLBOLD = new UnicodeFont(fontBold.deriveFont(Font.BOLD, fontBase)); ColorEffect colorEffect = new ColorEffect(); loadFont(DEFAULT, colorEffect, new UnicodeFont(fontCJK)); loadFont(BOLD, colorEffect, new UnicodeFont(fontCJK.deriveFont(Font.BOLD))); loadFont(XLARGE, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase * 3))); loadFont(LARGE, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase * 2))); loadFont(MEDIUM, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase * 3 / 2))); loadFont(MEDIUMBOLD, colorEffect, new UnicodeFont(fontCJK.deriveFont(Font.BOLD, fontBase * 3 / 2))); loadFont(SMALL, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase))); loadFont(SMALLBOLD, colorEffect, new UnicodeFont(fontCJK.deriveFont(Font.BOLD, fontBase))); }
Example 14
Source File: TestBitmapFontLayout.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static Font loadTtf( String resource ) { try { return Font.createFont(Font.TRUETYPE_FONT, TestBitmapFontLayout.class.getResourceAsStream(resource)); } catch( FontFormatException | IOException e ) { throw new RuntimeException("Error loading resource:" + resource, e); } }
Example 15
Source File: FontCheck.java From jclic with GNU General Public License v2.0 | 5 votes |
public static Font buildNewFont(String fileName, StreamIO.InputStreamProvider isp, String resourceName) throws Exception { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font font = Font.createFont(Font.TRUETYPE_FONT, isp.getInputStream(resourceName)); if (ge != null && font != null) { ge.registerFont(font); font = getValidFont(font.getName(), Font.PLAIN, 1); } return font; }
Example 16
Source File: FontUtils.java From scrimage with Apache License 2.0 | 5 votes |
public static Font createTrueType(InputStream in, int size) throws IOException, FontFormatException { assert (in != null); Font font = Font.createFont(Font.TRUETYPE_FONT, in); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(font); return font.deriveFont(Font.PLAIN, size); }
Example 17
Source File: FontUtil.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
/** * Get weather symbol font * * @return Weather symbol font */ public static Font getWeatherFont() { Font font = null; InputStream is = Draw.class.getResourceAsStream("/fonts/WeatherSymbol.ttf"); try { font = Font.createFont(Font.TRUETYPE_FONT, is); } catch (FontFormatException | IOException ex) { Logger.getLogger(Draw.class.getName()).log(Level.SEVERE, null, ex); } return font; }
Example 18
Source File: FontDecoder.java From CSSBox with GNU Lesser General Public License v3.0 | 4 votes |
public static Font decodeFont(DocumentSource fontSource, String format) throws FontFormatException, IOException { //TODO decode other formats than TTF return Font.createFont(Font.TRUETYPE_FONT, fontSource.getInputStream()); }
Example 19
Source File: HyperiumFontRenderer.java From Hyperium with GNU Lesser General Public License v3.0 | 4 votes |
private Font getFontFromInput(String path) throws IOException, FontFormatException { return Font.createFont(Font.TRUETYPE_FONT, HyperiumFontRenderer.class.getResourceAsStream(path)); }
Example 20
Source File: ValueAxisTest.java From jrobin with GNU Lesser General Public License v2.1 | 4 votes |
public void prepareGraph() throws RrdException, IOException, FontFormatException { graphDef = new RrdGraphDef(); graphDef.datasource("testvalue", jrbFileName, "testvalue","AVERAGE"); graphDef.area("testvalue", Util.parseColor("#FF0000"), "TestValue"); graphDef.setStartTime(startTime); graphDef.setEndTime(startTime + (60*60*24)); //We really need to specify exactly our own fonts, otherwise we get different behaviour on // different systems (not conducive to reliably testing) // We expect the TTF file to be on the class path. Font monoSpacedFont = Font.createFont(Font.TRUETYPE_FONT, new File("target/classes/DejaVuSansMono.ttf")); graphDef.setFont(RrdGraphDef.FONTTAG_DEFAULT, monoSpacedFont.deriveFont(10.0f), true, true); graphDef.setLargeFont(monoSpacedFont.deriveFont(12.0f)); //There's only a couple of methods of ImageWorker that we actually care about in this test. // More to the point, we want the rest to work as normal (like getFontHeight, getFontAscent etc) imageWorker = createMockBuilder(ImageWorker.class) .addMockedMethod("drawLine") .addMockedMethod("drawString") .createStrictMock(); //Order is important! //For the test only; when creating the mock object above, if we include ".withConstructor(100,100)" // easymock calls the constructor but the call to resize doesn't happen properly. // But it's imperative to setup internal state in the ImageWorker. So, call it here. imageWorker.resize(100,100); //The imageParameters setup code is duplicated and simplified from that in similarly named functions // in RrdGraph. It would be nice if this could be re-used somehow, but that would require // being able to pass in an ImageWorker to RrdGraph. But currently the imageworker is created // and used immediately in the constructor of RrdGraph. // And we need some existing unit tests to be able to prove that it's doing the right thing first // before we can refactor it so that we can write better unit tests. Mr Chicken, meet Mr Egg. // So, this is about the best we can do at this stage. // The called functions are named the same as in RrdGraph, but are generally simplified for the purposes of this test // eliminating code that we don't use imageParameters = new ImageParameters(); DataProcessor dataProcessor = this.fetchData(imageParameters, graphDef); this.calculatePlotValues(graphDef, dataProcessor); this.findMinMaxValues(imageParameters, graphDef); this.identifySiUnit(imageParameters, graphDef); this.expandValueRange(imageParameters, graphDef); this.initializeLimits(imageParameters, graphDef); imageWorker.resize(imageParameters.xgif, imageParameters.ygif); Mapper graphMapper = new Mapper(graphDef, imageParameters); this.valueAxis = new ValueAxis(imageParameters, imageWorker, graphDef, graphMapper); }