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

Glyph

public class Glyph extends ElementNode
A Glyph node corresponds to an SVG <glyph> and <missing-glyph> elements. If the unicode value is null, then a Glyph represents a <missing-glyph> and its length is assumed to be 1.
version
$Id: Glyph.java,v 1.11 2006/06/29 10:47:31 ln156897 Exp $

Fields Summary
public static final float
DEFAULT_HORIZONTAL_ADVANCE_X
By default, use the parent Font's advance
protected com.sun.perseus.j2d.Path
d
The Path representing the outline of this glyph
protected float
horizontalAdvanceX
This glyph's horizontal advance. A negative value means that the parent Font value should be used
protected float
computedHorizontalAdvanceX
The computed glyph's horizontal advance. If this glyphs advance is set, then this is equal to the glyph's advance. Otherwise, this is the grand-parent font's advance if there is one.
protected float
origin
The computed glyph origin.
protected float
emSquareScale
The em square scale factor.
protected String
unicode
The unicode character, or sequence of unicode character, that this glyph represents. If null, then this Glyph can be used to represent missing glyphs for arbitrary unicode values.
protected String[]
glyphName
Set of glyph names. May be null.
protected String
localName
This glyph's local name.
Constructors Summary
public Glyph(DocumentNode ownerDocument)
Constructor.

param
ownerDocument this element's owner DocumentNode


                
        
        this(ownerDocument, SVGConstants.SVG_GLYPH_TAG);
    
public Glyph(DocumentNode ownerDocument, String localName)
Constructor.

param
ownerDocument this element's owner DocumentNode
param
localName this element's local name. One of 'glyph' or 'missing-glyph'.

        super(ownerDocument);

        if (SVGConstants.SVG_GLYPH_TAG == localName
            ||
            SVGConstants.SVG_MISSING_GLYPH_TAG == localName) {
            this.localName = localName;
        } else {
            throw new IllegalArgumentException("Illegal Glyph name : " 
                                               + localName);
        }
    
Methods Summary
com.sun.perseus.j2d.BoxaddNodeBBox(com.sun.perseus.j2d.Box bbox, com.sun.perseus.j2d.Transform t)

param
bbox the bounding box to which this node's bounding box should be appended. That bounding box is in the target coordinate space. It may be null, in which case this node should create a new one.
param
t the transform from the node coordinate system to the coordinate system into which the bounds should be computed.
return
the bounding box of this node, in the target coordinate space,

        return addShapeBBox(bbox, d, t);
    
protected com.sun.perseus.j2d.TileaddRenderingTile(com.sun.perseus.j2d.Tile tile, com.sun.perseus.j2d.TextRenderingProperties trp, com.sun.perseus.j2d.Transform t)

param
tile the Tile instance whose bounds should be appended.
param
trp the TextRenderingProperties describing the nodes rendering characteristics.
param
t the Transform to the requested tile space, from this node's user space.
return
the expanded tile.

        if (d != null) {
            if (trp.getStroke() == null) {
                // No stroking on the shape, we can use the geometrical bounding
                // box.
                Box renderingBox = addNodeBBox(null, t);
                if (renderingBox != null) {
                    if (tile == null) {
                        tile = new Tile();
                        tile.snapBox(renderingBox);
                    } else {
                        tile.addSnapBox(renderingBox);
                    }
                }
            } else {
                // Need to account for stroking, with a more costly operation 
                // to compute the stroked bounds.
                Object strokedPath = PathSupport.getStrokedPath(d, trp);
                if (tile == null) {
                    tile = new Tile();
                    PathSupport.computeStrokedPathTile(tile, strokedPath, t);
                } else {
                    Tile st = new Tile();
                    PathSupport.computeStrokedPathTile(st, strokedPath, t);
                    tile.addTile(st);
                }
            }
        }
        return tile;
    
protected voidapplyInverseTransform(com.sun.perseus.j2d.Transform tx)

param
tx the inverse Transform between this node and its parent space.

        tx.mTranslate(origin, 0);
        tx.mScale(1 / emSquareScale, -1 / emSquareScale);
    
protected voidapplyTransform(com.sun.perseus.j2d.Transform tx)

param
tx the Transform to add node transform to.

        tx.mScale(emSquareScale, -emSquareScale);
        tx.mTranslate(-origin, 0);
    
public intgetCharAt(int i)
It is the responsibility of the caller to check that the requested index is between the unicode's value index range (i.e., between 0 and unicode.length())

param
i the index of the unicode value for the given index.
return
the character at the requested index

        return unicode.charAt(i);
    
public com.sun.perseus.j2d.PathgetD()

return
this glyph's geometry, as a Path. The returned path is in the Font's coordinate system.

        return d;
    
public java.lang.String[]getGlyphName()
Returns this glyph's name(s)

return
thsi glyph's name set. Null if none.

        return glyphName;
    
public floatgetHorizontalAdvanceX()

return
the horizontal advance along the x axis for this glyph. This advance is in the em square coordinate space. To get the advance in text coordinate space, use the getTextHorizontalAdvanceX method. Note that if the horizontalAdvanceX of this glyph is -1, this method returns the horizontal advance of the parent FontFace element if one is set.
see
#getTextHorizontalAdvanceX

        return computedHorizontalAdvanceX;
    
public intgetLength()

return
the number of characters represented by this glyph

        if (unicode == null) {
            return 1;
        }
        
        return unicode.length();
    
public java.lang.StringgetLocalName()

return
the SVGConstants.SVG_GLYPH_TAG value

        return localName;
    
public floatgetTextHorizontalAdvanceX()

return
the advance along the x axis in the text coordinate space, i.e., after applying the emSquare transform.

        return computedHorizontalAdvanceX * emSquareScale;
    
public java.lang.StringgetTraitImpl(java.lang.String name)
Supported traits: d, horiz-adv-x

param
name the requested trait name.
return
the requested trait 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_D_ATTRIBUTE == name) {
            if (d == null) {
                return "";
            } else {
                return d.toString();
            }
        } else if (SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == name) {
            return Float.toString(getHorizontalAdvanceX());
        } else if (SVGConstants.SVG_UNICODE_ATTRIBUTE == name) {
            if (unicode == null) {
                return "";
            }
            return unicode;
        } else if (SVGConstants.SVG_GLYPH_NAME_ATTRIBUTE == name) {
            return toStringTrait(getGlyphName());
        }

        return super.getTraitImpl(name);
    
public java.lang.StringgetUnicode()

return
this Glyph's unicode value

        return unicode;
    
public booleanisHit(float[] pt, com.sun.perseus.j2d.TextRenderingProperties trp)
A Glyph is hit if the Path it paints is hit by the given point.

param
pt the x/y coordinate. Should never be null and be of size two. If not, the behavior is unspecified. The point is in the node's user space.
param
trp the TextRenderingProperties containing the properties applicable to the hit detection operation. This is used because the same node may be referenced multiple times by different proxies.
return
true if this node is hit at the given point. false otherwise.

        if (d == null) {
            return false;
        }

        // If the node is filled, see if the shape is hit
        if (trp.getFill() != null) {
            if (PathSupport.isHit(d, trp.getFillRule(), pt[0], pt[1])) {
                return true;
            }
        }

        // Test detection on the edge if the stroke color
        // is set.
        if (trp.getStroke() != null) {
            Object strokedPath = PathSupport.getStrokedPath(d, trp);
            if (PathSupport.isStrokedPathHit(strokedPath, trp.getFillRule(), 
                                             pt[0], pt[1])) {
                return true;
            }
        }

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

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

        return new Glyph(doc, localName);
    
public voidsetD(com.sun.perseus.j2d.Path newD)

param
newD the new Path for this Glyph

        if (equal(newD, d)) {
            return;
        }
        this.d = newD;
    
public voidsetEmSquareScale(float newEmSquareScale)
Sets the emSquareScale. See the Font's list method adding children: when the FontFace is set, the Font sets the em square transform on all its children. When a Glyph is set, and if there is a FontFace already, the em square transform is set as well.

param
newEmSquareScale the new value for this glyph's em square scale, i.e., the scale between the glyph's em square and the text's coordinate system.

        if (newEmSquareScale == emSquareScale) {
            return;
        }
        emSquareScale = newEmSquareScale;
    
public voidsetGlyphName(java.lang.String[] glyphName)
Sets this glyph's name(s). May be null.

param
glyphName the new name for the glyph

        if (equal(glyphName, this.glyphName)) {
            return;
        }
        this.glyphName = glyphName;
    
public voidsetHorizontalAdvanceX(float newHorizontalAdvanceX)
Sets the horizontal advance along the x axis. This is in the em square coordinate space.

param
newHorizontalAdvanceX the new advance along the x axis

        if (newHorizontalAdvanceX == horizontalAdvanceX) {
            return;
        }

        this.horizontalAdvanceX = newHorizontalAdvanceX;
        if (newHorizontalAdvanceX == -1
            &&
            parent != null
            && parent instanceof Font) {
            computedHorizontalAdvanceX 
                = ((Font) parent).getHorizontalAdvanceX();
            origin = ((Font) parent).getHorizontalOriginX();
        } else {
            computedHorizontalAdvanceX = newHorizontalAdvanceX;
        }
    
public voidsetParent(ModelNode parent)
Override set parent to capture the default advance

param
parent this node's new parent

        super.setParent(parent);

        if (parent != null && parent instanceof Font) {
            if (horizontalAdvanceX == -1) {
                computedHorizontalAdvanceX 
                    = ((Font) parent).getHorizontalAdvanceX();
            }
            origin = ((Font) parent).getHorizontalOriginX();
        } else {
            origin = 0;
        }
    
public voidsetTraitImpl(java.lang.String name, java.lang.String value)
Supported traits: d, horiz-adv-x

param
name the trait name.
param
value the trait's string value.
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_D_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setD(parsePathTrait(name, value));
        } else if (SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setHorizontalAdvanceX(parsePositiveFloatTrait(name, value));
        } else if (SVGConstants.SVG_UNICODE_ATTRIBUTE == name) {
            checkWriteLoading(name);
            if (value == null) {
                throw illegalTraitValue(name, value);
            }
            setUnicode(value);
        } else if (SVGConstants.SVG_GLYPH_NAME_ATTRIBUTE == name) {
            checkWriteLoading(name);
            setGlyphName(parseStringArrayTrait(name, value, 
                                               SVGConstants.COMMA_STR));
        } else {
            super.setTraitImpl(name, value);
        }
    
public voidsetUnicode(java.lang.String newUnicode)

param
newUnicode this Glyph's new unicode value

        if (equal(newUnicode, unicode)) {
            return;
        }
        this.unicode = newUnicode;
    
booleansupportsTrait(java.lang.String traitName)
Supported traits: d, horiz-adv-x

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_D_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_HORIZ_ADV_X_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_GLYPH_NAME_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_UNICODE_ATTRIBUTE == traitName) {
            return true;
        }
        return super.supportsTrait(traitName);
    
public java.lang.StringtoString()

return
a text description of the glyph including it's unicode value

        return "com.sun.perseus.model.Glyph[" + unicode + "]";