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

FontFace

public class FontFace extends ElementNode
A FontFace node corresponds to an SVG <font-face> element.
Like many other classes, FontFace extends ElementNode so that we can support mixed namespaces (i.e., unexpected content in our model).
version
$Id: FontFace.java,v 1.4 2006/04/21 06:37:12 st125089 Exp $

Fields Summary
public static final int
FONT_WEIGHT_ALL
Values when all the font weights are used
public static final int
FONT_STYLE_ALL
Value whena all the font styles are used
public static final float
UNITS_PER_EM_DEFAULT
Default value for units per em
public static final float[]
FONT_SIZE_ALL
Value when all the font sizes match
protected String[]
fontFamilies
List of font families that constitute a match
protected int
fontStyles
Describes the styles that constitute a match for this font. This can be a combination (or'd) of the various FONT_STYLE_XXX values.
protected int
fontWeights
Describes the weights that constitute a match for this font. This can be a combination (or'd) of the various FONT_WEIGHT_XXX values. The values FONT_WEIGHT_BOLDER and FONT_WEIGHT_LIGHTER are not allowed.
protected float[]
fontSizes
Font sizes that constitute a match for this font during font selection. A null value means that all font-sizes can be matched by this font
protected float
unitsPerEm
Number of coordinate units on the em square. Defaults to 1000.
protected Font
font
Parent Font
protected float
emSquareScale
The main scale factor needed to convert from the em square grid to the text's current position coordinate system. The transform built from the returned value is:
scale 0 0
0 -scale 0
0 0 1
Constructors Summary
public FontFace(DocumentNode ownerDocument)
The constructor computes the inital emSquareScale

param
ownerDocument this element's owner DocumentNode

        super(ownerDocument);
        computeEmSquareScale();
    
Methods Summary
GlyphcanDisplay(char[] s, int index)

param
s the character string
param
index the index of the character for which to find a Glyph
return
a Glyph if the FontFace can display the character at the requested index. Returns null if no matching glyph can be found.

        return font.canDisplay(s, index);
    
protected voidcomputeEmSquareScale()
Computes the scale that should be applied to glyphs to transform glyph coordinates into the text coordiate system.

        // The number of units per em defines the number of 
        // units in 1x1 em square grid. Hence, the 1/unitsPerEm
        // scale factor.
        // The Y axis points up for the em square grid, which
        // is why the Y axis scale is negative.
        emSquareScale = 1f / unitsPerEm; 
    
public intfontWeightDistance(int refFontWeight)
The distance between the input refFontWeight and this FontFace's fontWeights

param
refFontWeight this method computes the distance between this FontFace's fontWeight and the input fontWeight
return
the distance to the input fontWeight

        // This FontFace matches all, this is a zero distance
        if (fontWeights == FONT_WEIGHT_ALL) {
            return 0;
        }

        // This FontFace matches the requested fontWeight,
        // this is a zero distance.
        if ((fontWeights & refFontWeight) != 0) {
            return 0;
        }

        // The requested fontWeight is not part of the ones
        // matching exactly this FontFace. Compute how far is 
        // the closes match.
        int dA = fontWeights;
        int i = 0;
        while ((dA & 0x01) == 0) {
            dA >>= 1;
            i++;
        }
        
        dA = i;

        i = 0;
        int curD = 100; // Infinity in this algorithm
        int d = 100;

        for (i = 0; i < 9; i++) {
            if (((refFontWeight >> i) & 0x01) != 0) {
                d = (dA - i) * (dA - i);
                if (d < curD) {
                    curD = d;
                }
            }
        }

        return curD;
    
public floatgetEmSquareScale()

return
the current scale factor from the em grid to the text coordinate system. The scale is recomputed every time the unitsPerEm value changes.
scale 0 0
0 -scale 0
0 0 1

        return emSquareScale;
    
public java.lang.String[]getFontFamilies()

return
the array of font families matched by this node

        return fontFamilies;
    
public float[]getFontSizes()

return
the array of font sizes matched by this node

        return fontSizes;
    
public intgetFontStyles()

return
the set of font styles matched by this node. To test if a particular font style is supported, use the '&' operator.

        return fontStyles;
    
public intgetFontWeights()

return
the set of font weights matched by this node

        return fontWeights;
    
public java.lang.StringgetLocalName()

return
the SVGConstants.SVG_FONT_FACE_TAG value


             

       
        return SVGConstants.SVG_FONT_FACE_TAG;
    
GlyphgetMissingGlyph()

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

        return font.getMissingGlyph();
    
public java.lang.StringgetTraitImpl(java.lang.String name)
FontFace handles the font-family, font-style, font-weight, font-size, and unitsPerEm attributes. 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_FONT_FAMILY_ATTRIBUTE == name) {
            return toStringTraitQuote(getFontFamilies());
        } else if (SVGConstants.SVG_FONT_STYLE_ATTRIBUTE == name) {
            return fontStylesToStringTrait(getFontStyles());
        } else if (SVGConstants.SVG_FONT_WEIGHT_ATTRIBUTE == name) {
            return fontWeightsToStringTrait(getFontWeights());
        } else if (SVGConstants.SVG_FONT_SIZE_ATTRIBUTE == name) {
            if (getFontSizes() == FONT_SIZE_ALL) {
                return SVGConstants.CSS_ALL_VALUE;
            }
            return toStringTrait(getFontSizes());
        } else if (SVGConstants.SVG_UNITS_PER_EM_ATTRIBUTE == name) {
            return Float.toString(getUnitsPerEm());
        } else {
            return super.getTraitImpl(name);
        }
    
public floatgetUnitsPerEm()

return
this node's unitsPerEm property

        return unitsPerEm;
    
public ElementNodenewInstance(DocumentNode doc)
Used by DocumentNode to create a new instance from a prototype FontFace.

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

        return new FontFace(doc);
    
public voidsetFontFamilies(java.lang.String[] newFontFamilies)

param
newFontFamilies the new set of font families matched by this node. Note that the input array is used by reference after this call

        if (equal(fontFamilies, newFontFamilies)) {
            return;
        }
        modifyingNode();
        this.fontFamilies = newFontFamilies;
        modifiedNode();
    
public voidsetFontSizes(float[] newFontSizes)

param
newFontSizes the new set of font sizes matched by this node

        if (equal(newFontSizes, fontSizes)) {
            return;
        }
        modifyingNode();
        this.fontSizes = newFontSizes;
        modifiedNode();
    
public voidsetFontStyles(int newFontStyles)

param
newFontStyles the set of font styles mateched by this node.

        if (newFontStyles == fontStyles) {
            return;
        }
        modifyingNode();
        this.fontStyles = newFontStyles;
        modifiedNode();
    
public voidsetFontWeights(int newFontWeights)

param
newFontWeights the set of font weight value matched by this FontFace

        if (newFontWeights == fontWeights) {
            return;
        }
        modifyingNode();
        this.fontWeights = newFontWeights;
        modifiedNode();
    
public voidsetParent(ModelNode parent)
If the parent is a Font object, keep track of it in the font member.

param
parent this node's new parent

        super.setParent(parent);

        if (parent instanceof Font) {
            this.font = (Font) parent;
        } else {
            this.font = null;
        }
    
public voidsetTraitImpl(java.lang.String name, java.lang.String value)
FontFace handles the font-family, font-style, font-weight, font-size, and unitsPerEm attributes. Other traits are handled by the super class.

param
name the trait's name (e.g., "units-per-em")
param
value the new trait string value (e.g., "1000")
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_FONT_FAMILY_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setFontFamilies(parseFontFamilyTrait(name, value));
        } else if (SVGConstants.SVG_FONT_STYLE_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setFontStyles(parseFontStylesTrait(name, value));
        } else if (SVGConstants.SVG_FONT_WEIGHT_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setFontWeights(parseFontWeightsTrait(name, value));
        } else if (SVGConstants.SVG_FONT_SIZE_ATTRIBUTE == name) {
            checkWriteLoading(name);
            if (SVGConstants.CSS_ALL_VALUE.equals(value)) {
                setFontSizes(FONT_SIZE_ALL);
            } else {
                setFontSizes(parseFloatArrayTrait(name, value));
            }
        } else if (SVGConstants.SVG_UNITS_PER_EM_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setUnitsPerEm(parseFloatTrait(name, value));
        } else {
            super.setTraitImpl(name, value);
        }
    
public voidsetUnitsPerEm(float newUnitsPerEm)

param
newUnitsPerEm this node's new unitsPerEm property

        if (newUnitsPerEm == unitsPerEm) {
            return;
        }
        modifyingNode();
        this.unitsPerEm = newUnitsPerEm;
        computeEmSquareScale();
        modifiedNode();
    
booleansupportsTrait(java.lang.String traitName)
FontFace handles the font-family, font-style, font-weight, font-size, and unitsPerEm attributes.

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_FONT_FAMILY_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_FONT_STYLE_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_FONT_WEIGHT_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_FONT_SIZE_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_UNITS_PER_EM_ATTRIBUTE == traitName) {
            return true;
        }
        
        return super.supportsTrait(traitName);