// Copyright 1996, Marimba Inc. All Rights Reserved.


// Confidential and Proprietary Information of Marimba, Inc.


// @(#)FontFamily.java, 1.4, 12/15/96





package marimba.text;





import java.awt.*;


import java.util.*;





/**


 * This implements a family of fonts.  This allows for quick access


 * to relatives of the font family based on the face, size of the


 * font.


 *


 * @author	Jonathan Payne


 * @version 1.4, 12/15/96


 */


class FontFamily {


    public static final int PLAIN = Font.PLAIN;


    public static final int BOLD = Font.BOLD;


    public static final int ITALIC = Font.ITALIC;


    public static final int EMBOSS = ITALIC << 1;





    static Hashtable families = new Hashtable();





    public static FontFamily lookup(String name) {


	FontFamily family = (FontFamily) families.get(name);


	if (family == null) {


	    family = new FontFamily(name);


	    families.put(name, family);


	}


	return family;


    }





    /** Name of family, e.g., "courier". */


    String name;





    /** Index for sizes.  sizes[12] = 2 => font 12 is stored in


      slot 2 of the faces array.  Slot 0 is not used in the faces


      array. */


    byte sizes[] = new byte[0];





    /** Faces, such as Font.PLAIN, Font.BOLD, Font.ITALIC


      and Font.BOLD | Font.ITALIC */


    DeviceFont faces[][];





    /** Next slot for an font size. */


    int nextSlot = 1;





    FontFamily(String name) {


	this.name = name;


	try {


	    faces = new DeviceFont[0][4];


	} catch (Throwable t) {


	    t.printStackTrace();


	}


    }





    final DeviceFont getFont(int size, int face) {


	face &= 3;


	if (size >= sizes.length) {


	    byte sz[] = new byte[size + 1];


	    System.arraycopy(sizes, 0, sz, 0, sizes.length);


	    sizes = sz;


	}


	int slot = sizes[size];


	if (slot == 0) {


	    slot = nextSlot++;


	    sizes[size] = (byte) slot;


	    if (slot >= faces.length) {


		DeviceFont fcs[][] = new DeviceFont[slot + 5][];


		System.arraycopy(faces, 0, fcs, 0, faces.length);


		faces = fcs;


	    }


	    faces[slot] = new DeviceFont[4];


	}


	DeviceFont font = faces[slot][face];


	if (font == null)


	    font = faces[slot][face] = new DeviceFont(name, face, size);


	return font;


    }


}


