FileDocCategorySizeDatePackage
DefaultFontFace.javaAPI DocphoneME MR2 API (J2ME)8900Wed May 02 18:00:36 BST 2007com.sun.perseus.builder

DefaultFontFace

public final class DefaultFontFace extends Object
This helper class encapsulates the creation of a default FontFace which a renderer can use.
This class tries to load an SVG file containing a font. The resource identifier for the default font is:
com/sun/perseus/renderer/resources/defaultFont.svg
Note: the defaultFont.svg is loaded and its first <font> element is loaded. The first <font> element must be the first child of the root <svg> element. Any other structure for the defaultFont.svg file will result in an Error. This helper class also encapsulates the creation and loading of a set of FontFaces for the initial font-family value. The fonts are expected to all be in a single initial font file:
com/sun/perseus/renderer/resources/initialFont.svg Note: the initialFont.svg is loaded and all the children of the root <svg> element are expected to be <font> elements. Any other structure for the initialFont.svg file will result in an Error.
see
com.sun.perseus.render.RenderGraphics
version
$Id: DefaultFontFace.java,v 1.6 2006/07/17 00:35:43 st125089 Exp $

Fields Summary
protected static com.sun.perseus.model.FontFace
defaultFontFace
The default font face is cached so that it is loaded only once
protected static com.sun.perseus.model.FontFace[]
initialFontFaces
The set of initial font faces is cached so that it is loaded only once.
Constructors Summary
private DefaultFontFace()
Private default constructor to prevent instantiation of this utility class

    
Methods Summary
public static com.sun.perseus.model.FontFacegetDefaultFontFace()

return
the default font face. This font face is loaded from the {@link #ResourceHandler.DEFAULT_FONT_FACE_FILE ResourceHandler.DEFAULT_FONT_FACE} resource.

        if (defaultFontFace == null) {
            loadDefaultFontFace();
        }
        return defaultFontFace;
    
public static com.sun.perseus.model.FontFace[]getInitialFontFaces()

return
the initial font faces. The initial font faces are loaded from the {@link #ResourceHandler.INITIAL_FONT_FACE_FILE ResourceHandler.INITIAL_FONT_FACE_FILE} resource.

        if (initialFontFaces == null) {
            loadInitialFontFaces();
        }
        return initialFontFaces;
    
static voidloadDefaultFontFace()
Loads the default font face. This assumes that the font face file has not been loaded already. If the load of the font face fails, the application throws an error.

throws
Error if there is an error while loading the default font face file.
see
#getDefaultFontFace

        try {
            InputStream is = 
                ResourceHandler.getDefaultFontResource();
            if (is == null) {
                throw new Exception
                    (Messages.formatMessage
                     (Messages.ERROR_CANNOT_LOAD_RESOURCE,
                      new Object[] {"Default Font"}));
            }

            ModelNode svg = loadSVGFontResource(is);

            // Now, get the FontFace from the tree. 
            // svg -> font -> font-face
            defaultFontFace = (FontFace)
                svg.getFirstChildNode().getFirstChildNode();
            
        } catch (Exception e) {
            // Do not tolerate any error as this is critical for the operation
            // of the toolkit.
            e.printStackTrace();
            throw new Error();
        }
    
static voidloadInitialFontFaces()
Loads the initial font faces. This assumes that the font face file has not been loaded already. If the load of the font face fails, the application throws an error.

throws
Error if there is an error while loading the file. An error is generated.
see
#getInitialFontFaces

        try {
            InputStream is = 
                ResourceHandler.getInitialFontResource();
            if (is == null) {
                throw new Exception
                    (Messages.formatMessage
                     (Messages.ERROR_CANNOT_LOAD_RESOURCE,
                      new Object[] {"Initial Font"}));
            }

            ModelNode svg = loadSVGFontResource(is);

            // Now, get the FontFace from the tree. 
            // vp -> svg -> font[i] -> font-face
            
            // Count the font children
            int n = 0;
            ModelNode c = svg.getFirstChildNode();
            while (c != null) {
                n++;
                c = c.getNextSiblingNode();
            }
            initialFontFaces = new FontFace[n];
            ModelNode font = svg.getFirstChildNode();
            for (int i = 0; i < initialFontFaces.length; i++) {
                initialFontFaces[i] = (FontFace) font.getFirstChildNode();
                font = font.getNextSiblingNode();
            }
        } catch (Exception e) {
            // Do not tolerate any error as this is critical for the operation
            // of the toolkit.
            e.printStackTrace();
            throw new Error();
        }
    
protected static com.sun.perseus.model.ModelNodeloadSVGFontResource(java.io.InputStream is)
Helper method: loads an SVG Font resource file

param
svgResource the SVG Font resource file
return
The root <svg> element representing the resource file.
throws
Exception if an error happens while loading the resource

        // We use JAXP to get a SAX 2 Parser
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        SAXParser parser = factory.newSAXParser();
        
        // Configure a ModelBuilder to turn an SVG document 
        // into a ModelNode tree
        DocumentNode root = new DocumentNode();
        UpdateAdapter ul = new UpdateAdapter();
        root.setUpdateListener(ul);
        ModelBuilder modelBuilder = new ModelBuilder(null, root);
        
        ul.loadStarting(root, is);

        try {
            parser.parse(is, modelBuilder);
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
            }

            int n = modelBuilder.entityStreams.size();
            for (int i = 0; i < n; i++) {
                Reader r = (Reader) modelBuilder.entityStreams.elementAt(i);
                try {
                    r.close();
                } catch (IOException ioe) {
                    // Do nothing: this means the stream was 
                    // closed by the SAX parser.
                }
            }
        }

        if (!ul.loadSuccess()) {
            throw new Exception
                (Messages.formatMessage
                    (Messages.ERROR_CANNOT_LOAD_RESOURCE,
                     new Object[] {"SVG System Font Resource"}));
        }

        // trace(modelBuilder.getModelRoot(), "");
        return modelBuilder.getModelRoot().getFirstChildNode();