/* * Fontlist.java * Some utilities to handle a list of fonts. * * Ken Shirriff ken.shirriff@eng.sun.com * * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * Please refer to the file "license.txt" * for further important copyright and licensing information. */ package pcffont; import java.awt.*; import java.awt.image.*; import java.applet.Applet; import java.net.*; import java.util.*; import java.io.*; /** * Some utilities to handle a list of fonts in a fontlist file. * The first line of the file contains the number of fonts. * The subsequent lines hold pairs of font paths and font names. * For example: *
 *	2
 *	helv12.pcf Helvetica/12pt
 *	cyr.pcf Cyrillic/8pt
 *	
* * @author Ken Shirriff, shirriff@eng.sun.com */ public class Fontlist { PCFFont fonts[]; URL context; boolean antialias; /** * Read a fontlist file. * The fontlist file contains a number, followed by that many lines * of "fontfile fontname" pairs. * (This file is the same format as an X11 fonts.dir file.) * @param context Context of the open * @param fontlist Name of the fontlist file */ public Fontlist(URL context, String fontlist) throws IOException { this(context, fontlist, false); } /** * Read a fontlist file. * The fontlist file contains a number, followed by that many lines * of "fontfile fontname" pairs. * (This file is the same format as an X11 fonts.dir file.) * @param context Context of the open * @param fontlist Name of the fontlist file * @param antialias Indicates if antialiased rendering is wanted. */ public Fontlist(URL context, String fontlist, boolean antialias) throws IOException { this.context = context; this.antialias = antialias; URL url = new URL(context, fontlist); // Open the input stream to the font file InputStream is = url.openStream(); DataInputStream dis = new DataInputStream(is); String line = dis.readLine(); numlines = Integer.valueOf(line).intValue(); names = new String[numlines]; files = new String[numlines]; fonts = new PCFFont[numlines]; for (int i=0; i < numlines ;i++) { line = dis.readLine(); StringTokenizer st = new StringTokenizer(line); files[i] = st.nextToken(" "); try { // An X Windows format name st.nextToken("-"); String foundry = st.nextToken("-"); // foundry String family = st.nextToken("-"); // family st.nextToken("-"); // weight st.nextToken("-"); // slant st.nextToken("-"); // sWidth String size = st.nextToken("-"); // adstyl if (foundry.equals("freetype")) { names[i] = family+"/"+size; } else if (foundry.equals("etl")) { throw(new NoSuchElementException("")); } else { names[i] = foundry+"/"+family+"/"+size; } } catch (NoSuchElementException e) { // Not an X windows format name st = new StringTokenizer(line); st.nextToken(" "); if (files[i].indexOf('/')>=0) { names[i] = files[i].substring(files[i].indexOf('/')+1); } else { names[i] = files[i]; } } } } /** * Open the specified font file, using caching. * @param n The font number. * @return The font. */ public PCFFont openfont(int num) { return openfont(num, null); } /** * Open the specified font file, using caching and report on progress. * @param n The font number. * @param callback Callback to report on progress. * @return The font. */ public PCFFont openfont(int num, CompletionCallback callback) { if (fonts[num] != null) { return fonts[num]; } try { // Open the font URL URL url = new URL(context, files[num]); // Open the input stream to the font InputStream is = url.openStream(); // Create the PCFFont from the stream. if (antialias) { fonts[num] = new PCFFontAA(is, callback); } else { fonts[num] = new PCFFont(is, callback); } } catch (IOException e) { System.out.println("URL Exception "+files[num]+" "+e); return null; } return fonts[num]; } /** * Return the index of a font in the fontlist file. * @param name Name of the desired font * @return The index of the specified font, or -1 if not found. */ public int lookupFont(String name) { for (int i=0; i