FileDocCategorySizeDatePackage
FontMetricsImpl.javaAPI DocAndroid 1.5 API7129Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.gl.font

FontMetricsImpl

public class FontMetricsImpl extends FontMetrics
FontMetrics implementation

Fields Summary
private static final long
serialVersionUID
private int
ascent
private int
descent
private int
leading
private int
maxAscent
private int
maxDescent
private int
maxAdvance
private int[]
widths
private transient FontPeerImpl
peer
private float
scaleX
public com.android.internal.awt.AndroidGraphics2D
mSg
private Font
mFn
private float
scaleY
Constructors Summary
public FontMetricsImpl(Font fnt)
Creates new FontMericsImpl object described by the specified Font.

param
fnt the specified Font object


	                           	 
	   
		super(fnt);
		this.mFn = fnt;
		
		mSg = AndroidGraphics2D.getInstance();
		Paint p = mSg.getAndroidPaint();
		
		this.ascent = (int)-p.ascent();
		this.descent = (int)p.descent();
		this.leading = p.getFontMetricsInt().leading;
		
		AffineTransform at = fnt.getTransform();
		if (!at.isIdentity()) {
			scaleX = (float) at.getScaleX();
			scaleY = (float) at.getScaleY();
		}
				
	    /*
	     * metrics[5] - strikethrough thickness<p>
	     * -metrics[6] - strikethrough offset<p>
	     * metrics[7] - maximum char width<p>
	     * metrics[8] - ascent in pixels<p>
	     * metrics[9] - descent in pixles<p>
	     * metrics[10] - external leading in pixels<p>
	     * metrics[11] - underline thickness in pixels<p>
	     * -metrics[12] - underline offset in pixels<p>
	     * metrics[13] - strikethrough thickness in pixels<p>
	     * -metrics[14] - strikethrough offset in pixels<p>
	     * metrics[15] - maximum char width in pixels<p>

	     * @param _baselineData an array of 3 elements with baseline offsets metrics<p>
	     * _baselineData[0] - roman baseline offset<p> 
	     * _baselineData[1] - center baseline offset<p>
	     * _baselineData[2] - hanging baseline offset<p>
	     */
	
Methods Summary
public intcharWidth(int ch)
Returns the advance width of the specified char of the Font describing this FontMetricsImpl object.

param
ch the char which width is to be returned
return
the advance width of the specified char of the Font describing this FontMetricsImpl object

		if (ch < 256) {
			return widths[ch];
		}

		return getFontPeer().charWidth((char) ch);
	
public intcharWidth(char ch)
Returns the advance width of the specified char of the Font describing this FontMetricsImpl object.

param
ch the char which width is to be returned
return
the advance width of the specified char of the Font describing this FontMetricsImpl object

		if (ch < 256) {
			return widths[ch];
		}
		return (int) (getFontPeer().charWidth(ch) * scaleX);
	
public intgetAscent()
Returns the ascent of the Font describing this FontMetricsImpl object.

		return this.ascent;
	
public intgetDescent()
Returns the descent of the Font describing this FontMetricsImpl object.

		return this.descent;
	
public FontPeerImplgetFontPeer()
Returns FontPeer implementation of the Font describing this FontMetricsImpl object.

return
a FontPeer object, that is the platform dependent FontPeer implementation for the Font describing this FontMetricsImpl object.

		if (peer == null) {
			peer = (FontPeerImpl) font.getPeer();
		}
		return peer;
	
public intgetLeading()
Returns the leading of the Font describing this FontMetricsImpl object.

		return this.leading;
	
public intgetMaxAdvance()
Returns the maximum advance of the Font describing this FontMetricsImpl object.

		return this.maxAdvance;
	
public intgetMaxAscent()
Returns the maximum ascent of the Font describing this FontMetricsImpl object.

		return this.maxAscent;
	
public intgetMaxDecent()
Returns the maximum descent of the Font describing this FontMetricsImpl object.

		return this.maxDescent;
	
public intgetMaxDescent()
Returns the maximum descent of the Font describing this FontMetricsImpl object.

		return this.maxDescent;
	
public int[]getWidths()
Returns the advance widths of the first 256 characters in the Font describing this FontMetricsImpl object.

		return this.widths;
	
private voidinitWidths()
Initialize the array of the first 256 chars' advance widths of the Font describing this FontMetricsImpl object.


		this.widths = new int[256];
		for (int chr = 0; chr < 256; chr++) {
			widths[chr] = (int) (getFontPeer().charWidth((char) chr) * scaleX);
		}

	
public intstringWidth(java.lang.String str)
Returns the total advance width of the specified string in the metrics of the Font describing this FontMetricsImpl object.

param
str the String which width is to be measured
return
the total advance width of the specified string in the metrics of the Font describing this FontMetricsImpl object


		int width = 0;
		char chr;

		for (int i = 0; i < str.length(); i++) {
			chr = str.charAt(i);
			width += charWidth(chr);
		}
		return width;

		/*
		 * float res = 0; int ln = str.length(); char[] c = new char[ln]; float[] f =
		 * new float[ln]; str.getChars(0, ln, c, 0); mSg.getPaint().getTextWidths(c, 0,
		 * ln, f);
		 * 
		 * for(int i = 0; i < f.length; i++) { res += f[i]; } return (int)res;
		 */