FileDocCategorySizeDatePackage
Font.javaAPI DocphoneME MR2 API (J2ME)15798Wed May 02 18:00:36 BST 2007com.sun.perseus.model

Font

public class Font extends ElementNode
A Font node models an SVG <code> element.
version
$Id: Font.java,v 1.7 2006/06/29 10:47:31 ln156897 Exp $

Fields Summary
static final String[]
REQUIRED_TRAITS
Only the horiz-adv-x trait is required on
protected float
horizontalOriginX
The X-coordinate in the font coordinate system of the origin of a glyph to be used when drawing horizontally oriented text. (Note that the origin applies to all glyphs in the font.)
protected float
horizontalAdvanceX
The default horizontal advance after rendering a glyph in horizontal orientation. Glyph widths are required to be non-negative, even if the glyph is typically rendered right-to-left, as in Hebrew and Arabic scripts.
protected FontFace
fontFace
The current fontFace describing this font, if any
protected Glyph
missingGlyph
The Font's missing glyph, used to render unknown characters
protected HKern
firstHKern
The Font's first horizontal kerning pair. May be null.
protected HKern
lastHKern
The Font's last horizontal kerning pair. May be null.
Constructors Summary
public Font(DocumentNode ownerDocument)
Constructor.

param
ownerDocument this element's owner DocumentNode


                
        
        super(ownerDocument);
    
Methods Summary
public voidadd(ElementNode node)

param
node ElementNode to add to this ElementNode

        super.add(node);
        addSpecial(node);
    
protected voidaddFontFace(FontFace newFontFace)

param
newFontFace the FontFace child to add

        if (fontFace != null) {
            // If there is already a FontFace child, we ignore the second
            // one. Only the first child is accounted for.
            return;
        }

        // A Font can only have a single font-face child. If there is alre
        fontFace = newFontFace;
        updateGlyphEmSquare();
    
protected voidaddGlyph(Glyph gl)

param
gl Glyph to add to this font

        updateGlyphEmSquare(gl);
    
protected voidaddHKern(HKern hkern)
Simply chain the kerning pairs so that they can be looked up easily later.

param
hkern the new HKern entry to add to this node.

        if (firstHKern != null) {
            lastHKern.nextHKern = hkern;
            lastHKern = hkern;
        } else {
            firstHKern = hkern;
            lastHKern = hkern;
            hkern.nextHKern = null;
        }
    
protected voidaddMissingGlyph(Glyph gl)
If there is already a missing glyph, any new call to addMissingGlyph has no effect on the missing glyph.

param
gl Glyph to add as a missing glyph.

        // Only use gl as a missing glyph if it is the 
        // first missing glyph
        if (missingGlyph == null) {
            missingGlyph = gl;
        } 
        updateGlyphEmSquare(gl);
    
protected voidaddSpecial(ModelNode node)
Called in case special handling of children is required.

param
node the ModelNode added to this list
see
#addFontFace
see
#addGlyph

        if (!(node instanceof Glyph)) {
            if (!(node instanceof FontFace)) {
                if (!(node instanceof HKern)) {
                    return;
                } else {
                    addHKern((HKern) node);
                }
            } else {
                addFontFace((FontFace) node);
            }
        } else {
            Glyph gl = (Glyph) node;
            if (gl.getUnicode() == null) {
                addMissingGlyph(gl);
            } else {
                addGlyph(gl);
            }
        }
    
public GlyphcanDisplay(char[] s, int index)
Returns a Glyph if the FontFace can display the character at the requested index. Returns null if no matching glyph can be found.

param
s the character sequence to display
param
index the index of the first character in s to display.
return
the Glyph used to represent the input character at index in s

        int max = s.length - index;
        int glLength = 0;
        int j = 0;

        Glyph gl = null;
        ModelNode c = firstChild;
        while (c != null) {
            if (c instanceof Glyph) {
                gl = (Glyph) c;
                glLength = gl.getLength();
                if (gl.getUnicode() != null 
                    && glLength > 0 
                    && index + glLength <= s.length) {
                    for (j = 0; j < glLength; j++) {
                        if (s[index + j] != gl.getCharAt(j)) {
                            break;
                        }
                    }
                    if (j == glLength) {
                        return gl;
                    }
                }
            }
            c = c.nextSibling;
        }

        // No matching glyph
        return null;
    
floatgetFloatTraitImpl(java.lang.String name)
Font handles the horiz-adv-x and horiz-origin-x traits. Other traits are handled by the super class.

param
name the requested trait name (e.g., "horiz-adv-x")
return
the trait's value, as a float.
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code TYPE_MISMATCH_ERR if requested trait's computed value cannot be converted to a String (SVG Tiny only).

        if (SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == name) {
            return getHorizontalAdvanceX();
        } else if (SVGConstants.SVG_HORIZ_ORIGIN_X_ATTRIBUTE == name) {
            return getHorizontalOriginX();
        } else {
            return super.getFloatTraitImpl(name);
        }
    
floatgetHKern(Glyph g1, Glyph g2)
Used by the Text class: looks up kerning pairs to check if there is any horizontal kerning for the input pair.

param
g1 this first glyph is assumed to be part of this Font.
param
g2 this second glyph may come from a different font, in which case this method returns 0.
return
the kerning adjustment for the given pair of Glyphs.

        if (g2.parent != this) {
            return 0;
        }

        HKern k = firstHKern;
        while (k != null) {
            if (k.matchesFirst(g1)) {
                if (k.matchesSecond(g2)) {
                    break;
                }
            }
            k = k.nextHKern;
        }

        if (k != null) {
            if (fontFace != null) {
                return k.k * fontFace.getEmSquareScale();
            } else {
                return k.k;
            }
        } else {
            return 0;
        }
    
public floatgetHorizontalAdvanceX()

return
this Font's horizontal adavance along the x-axis

        return horizontalAdvanceX;
    
public floatgetHorizontalOriginX()

return
this Font's origin along the x-axis

        return horizontalOriginX;
    
public java.lang.StringgetLocalName()

return
the SVGConstants.SVG_FONT_TAG value

        return SVGConstants.SVG_FONT_TAG;
    
public GlyphgetMissingGlyph()

return
the Glyph that represents missing glyphs. This should *not* be null.

        return missingGlyph;
    
public java.lang.String[]getRequiredTraits()

return
an array of traits that are required by this element.

        return REQUIRED_TRAITS;
    
public java.lang.StringgetTraitImpl(java.lang.String name)
Font handles the horiz-adv-x and horiz-origin-x traits. Other traits are handled by the super class.

param
name the requested trait name (e.g., "horiz-adv-x")
return
the trait's value, as a string.
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code TYPE_MISMATCH_ERR if requested trait's computed value cannot be converted to a String (SVG Tiny only).

        if (SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == name) {
            return Float.toString(getHorizontalAdvanceX());
        } else if (SVGConstants.SVG_HORIZ_ORIGIN_X_ATTRIBUTE == name) {
            return Float.toString(getHorizontalOriginX());
        } else {
            return super.getTraitImpl(name);
        }
    
public ElementNodenewInstance(DocumentNode doc)
Used by DocumentNode to create a new instance from a prototype Font.

param
doc the DocumentNode for which a new node is should be created.
return
a new Font for the requested document.

        return new Font(doc);
    
public voidsetFloatTraitImpl(java.lang.String name, float value)
Font handles the horiz-adv-x and horiz-origin-x traits. Other traits are handled by the super class.

param
name the trait's name (e.g., "horiz-adv-x")
param
value the new trait float value (e.g., 10f)
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code TYPE_MISMATCH_ERR if the requested trait's value cannot be specified as a String
throws
DOMException with error code INVALID_ACCESS_ERR if the input value is an invalid value for the given trait or null.
throws
DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if attempt is made to change readonly trait.

        if (SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == name) {
            setHorizontalAdvanceX(value);
        } else if (SVGConstants.SVG_HORIZ_ORIGIN_X_ATTRIBUTE == name) {
            setHorizontalOriginX(value);
        } else {
            super.setFloatTraitImpl(name, value);
        }
    
public voidsetHorizontalAdvanceX(float newHorizontalAdvanceX)

param
newHorizontalAdvanceX the new horizontal advance along the x-axis for this Font

        if (newHorizontalAdvanceX == horizontalAdvanceX) {
            return;
        }
        modifyingNode();
        horizontalAdvanceX = newHorizontalAdvanceX;
        modifiedNode();
    
public voidsetHorizontalOriginX(float newHorizontalOriginX)

param
newHorizontalOriginX the new value for the Font's origin along the x-axis.

        if (newHorizontalOriginX == horizontalOriginX) {
            return;
        }
        modifyingNode();
        horizontalOriginX = newHorizontalOriginX;
        modifiedNode();
    
public voidsetTraitImpl(java.lang.String name, java.lang.String value)
Font handles the horiz-adv-x and horiz-origin-x traits. Other traits are handled by the super class.

param
name the trait's name (e.g., "horiz-adv-x")
param
value the new trait string value (e.g., "10")
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code TYPE_MISMATCH_ERR if the requested trait's value cannot be specified as a String
throws
DOMException with error code INVALID_ACCESS_ERR if the input value is an invalid value for the given trait or null.
throws
DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if attempt is made to change readonly trait.

        if (SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setHorizontalAdvanceX(parseFloatTrait(name, value));
        } else if (SVGConstants.SVG_HORIZ_ORIGIN_X_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setHorizontalOriginX(parseFloatTrait(name, value));
        } else {
            super.setTraitImpl(name, value);
        }
    
booleansupportsTrait(java.lang.String traitName)
Font handlers the horiz-adv-x and horiz-origin-x traits.

param
traitName the name of the trait which the element may support.
return
true if this element supports the given trait in one of the trait accessor methods.

        if (SVGConstants.SVG_HORIZ_ORIGIN_X_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == traitName) {
            return true;
        }

        return super.supportsTrait(traitName);
    
protected voidupdateGlyphEmSquare()
Update the em square on all the Font's glyphs

        float emSquareScale = 1;
        if (fontFace != null) {
            emSquareScale = fontFace.getEmSquareScale();
        }

        ElementNode c = firstChild;
        while (c != null) {
            if (c instanceof Glyph) {
                ((Glyph) c).setEmSquareScale(emSquareScale);
            }
            c = (ElementNode) c.nextSibling;
        }
    
protected voidupdateGlyphEmSquare(Glyph gl)

param
gl set the input Glyph's em square.

        if (fontFace != null) {
            gl.setEmSquareScale(fontFace.getEmSquareScale());
        } else {
            gl.setEmSquareScale(1);
        }