private Font(File fontFile, int fontFormat, boolean isCopy, CreatedFontTracker tracker) throws FontFormatException { this.createdFont = true; /* Font2D instances created by this method track their font file * so that when the Font2D is GC'd it can also remove the file. */ FontManager fm = FontManagerFactory.getInstance(); Font2D[] fonts = fm.createFont2D(fontFile, fontFormat, false, isCopy, tracker); this.font2DHandle = fonts[0].handle; this.name = this.font2DHandle.font2D.getFontName(Locale.getDefault()); this.style = Font.PLAIN; this.size = 1; this.pointSize = 1f; }
private Font(File fontFile, int fontFormat, boolean isCopy, CreatedFontTracker tracker) throws FontFormatException { this.createdFont = true; /* Font2D instances created by this method track their font file * so that when the Font2D is GC'd it can also remove the file. */ FontManager fm = FontManagerFactory.getInstance(); this.font2DHandle = fm.createFont2D(fontFile, fontFormat, isCopy, tracker).handle; this.name = this.font2DHandle.font2D.getFontName(Locale.getDefault()); this.style = Font.PLAIN; this.size = 1; this.pointSize = 1f; }
/** * Returns a new <code>Font</code> using the specified font type * and input data. The new <code>Font</code> is * created with a point size of 1 and style {@link #PLAIN PLAIN}. * This base font can then be used with the <code>deriveFont</code> * methods in this class to derive new <code>Font</code> objects with * varying sizes, styles, transforms and font features. This * method does not close the {@link InputStream}. * <p> * To make the <code>Font</code> available to Font constructors the * returned <code>Font</code> must be registered in the * <code>GraphicsEnviroment</code> by calling * {@link GraphicsEnvironment#registerFont(Font) registerFont(Font)}. * @param fontFormat the type of the <code>Font</code>, which is * {@link #TRUETYPE_FONT TRUETYPE_FONT} if a TrueType resource is specified. * or {@link #TYPE1_FONT TYPE1_FONT} if a Type 1 resource is specified. * @param fontStream an <code>InputStream</code> object representing the * input data for the font. * @return a new <code>Font</code> created with the specified font type. * @throws IllegalArgumentException if <code>fontFormat</code> is not * <code>TRUETYPE_FONT</code>or<code>TYPE1_FONT</code>. * @throws FontFormatException if the <code>fontStream</code> data does * not contain the required font tables for the specified format. * @throws IOException if the <code>fontStream</code> * cannot be completely read. * @see GraphicsEnvironment#registerFont(Font) * @since 1.3 */ public static Font createFont(int fontFormat, InputStream fontStream) throws java.awt.FontFormatException, java.io.IOException { if (hasTempPermission()) { return createFont0(fontFormat, fontStream, null); } // Otherwise, be extra conscious of pending temp file creation and // resourcefully handle the temp file resources, among other things. CreatedFontTracker tracker = CreatedFontTracker.getTracker(); boolean acquired = false; try { acquired = tracker.acquirePermit(); if (!acquired) { throw new IOException("Timed out waiting for resources."); } return createFont0(fontFormat, fontStream, tracker); } catch (InterruptedException e) { throw new IOException("Problem reading font data."); } finally { if (acquired) { tracker.releasePermit(); } } }
/** * Returns a new {@code Font} using the specified font type * and input data. The new {@code Font} is * created with a point size of 1 and style {@link #PLAIN PLAIN}. * This base font can then be used with the {@code deriveFont} * methods in this class to derive new {@code Font} objects with * varying sizes, styles, transforms and font features. This * method does not close the {@link InputStream}. * <p> * To make the {@code Font} available to Font constructors the * returned {@code Font} must be registered in the * {@code GraphicsEnvironment} by calling * {@link GraphicsEnvironment#registerFont(Font) registerFont(Font)}. * @param fontFormat the type of the {@code Font}, which is * {@link #TRUETYPE_FONT TRUETYPE_FONT} if a TrueType resource is specified. * or {@link #TYPE1_FONT TYPE1_FONT} if a Type 1 resource is specified. * @param fontStream an {@code InputStream} object representing the * input data for the font. * @return a new {@code Font} created with the specified font type. * @throws IllegalArgumentException if {@code fontFormat} is not * {@code TRUETYPE_FONT} or {@code TYPE1_FONT}. * @throws FontFormatException if the {@code fontStream} data does * not contain the required font tables for the specified format. * @throws IOException if the {@code fontStream} * cannot be completely read. * @see GraphicsEnvironment#registerFont(Font) * @since 1.3 */ public static Font createFont(int fontFormat, InputStream fontStream) throws java.awt.FontFormatException, java.io.IOException { if (hasTempPermission()) { return createFont0(fontFormat, fontStream, false, null)[0]; } // Otherwise, be extra conscious of pending temp file creation and // resourcefully handle the temp file resources, among other things. CreatedFontTracker tracker = CreatedFontTracker.getTracker(); boolean acquired = false; try { acquired = tracker.acquirePermit(); if (!acquired) { throw new IOException("Timed out waiting for resources."); } return createFont0(fontFormat, fontStream, false, tracker)[0]; } catch (InterruptedException e) { throw new IOException("Problem reading font data."); } finally { if (acquired) { tracker.releasePermit(); } } }
/** * Returns a new array of {@code Font} decoded from the specified stream. * The returned {@code Font[]} will have at least one element. * <p> * The explicit purpose of this variation on the * {@code createFont(int, InputStream)} method is to support font * sources which represent a TrueType/OpenType font collection and * be able to return all individual fonts in that collection. * Consequently this method will throw {@code FontFormatException} * if the data source does not contain at least one TrueType/OpenType * font. The same exception will also be thrown if any of the fonts in * the collection does not contain the required font tables. * <p> * The condition "at least one", allows for the stream to represent * a single OpenType/TrueType font. That is, it does not have to be * a collection. * Each {@code Font} element of the returned array is * created with a point size of 1 and style {@link #PLAIN PLAIN}. * This base font can then be used with the {@code deriveFont} * methods in this class to derive new {@code Font} objects with * varying sizes, styles, transforms and font features. * <p>This method does not close the {@link InputStream}. * <p> * To make each {@code Font} available to Font constructors it * must be registered in the {@code GraphicsEnvironment} by calling * {@link GraphicsEnvironment#registerFont(Font) registerFont(Font)}. * @param fontStream an {@code InputStream} object representing the * input data for the font or font collection. * @return a new {@code Font[]}. * @throws FontFormatException if the {@code fontStream} data does * not contain the required font tables for any of the elements of * the collection, or if it contains no fonts at all. * @throws IOException if the {@code fontStream} cannot be completely read. * @see GraphicsEnvironment#registerFont(Font) * @since 9 */ public static Font[] createFonts(InputStream fontStream) throws FontFormatException, IOException { final int fontFormat = Font.TRUETYPE_FONT; if (hasTempPermission()) { return createFont0(fontFormat, fontStream, true, null); } // Otherwise, be extra conscious of pending temp file creation and // resourcefully handle the temp file resources, among other things. CreatedFontTracker tracker = CreatedFontTracker.getTracker(); boolean acquired = false; try { acquired = tracker.acquirePermit(); if (!acquired) { throw new IOException("Timed out waiting for resources."); } return createFont0(fontFormat, fontStream, true, tracker); } catch (InterruptedException e) { throw new IOException("Problem reading font data."); } finally { if (acquired) { tracker.releasePermit(); } } }