Code example for Paint

Methods: getFontMetrics, getFontMetricsInt, getTextSize, setTextSize, setTextSkewX, setTypeface, setUnderlineText

0
	 
	private Font( int face, int style, Dimension size ) { 
		this( face, style );
		this.size = SIZE_MEDIUM; 
 
		this.paint = new Paint();
		double factor;
		//#if polish.android.font.MediumFactor:defined 
			//#= factor = ${polish.android.font.MediumFactor}; 
		//#else 
			factor = 1.85;
		//#endif 
 
		this.height = size.getValue( (int)(this.paint.getTextSize() * factor) );
		initPaint( this.paint );
	} 
 
	/** 
	 * Gets the <code>Font</code> used by the high level user interface 
	 * for the <code>fontSpecifier</code> passed in. It should be used 
	 * by subclasses of 
	 * <code>CustomItem</code> and <code>Canvas</code> to match user 
	 * interface on the device. 
	 *  
	 * @param fontSpecifier - one of FONT_INPUT_TEXT, or FONT_STATIC_TEXT 
	 * @return font that corresponds to the passed in font specifier 
	 * @throws IllegalArgumentException - if fontSpecifier is not a valid fontSpecifier 
	 * @since  MIDP 2.0 
	 */ 
	public static Font getFont(int fontSpecifier)
	{ 
		//TODO implement getFont( int fontSpecifier ) 
		return getDefaultFont(); 
	} 
 
	/** 
	 * Gets the default font of the system. 
	 *  
	 * @return the default font 
	 */ 
	public static Font getDefaultFont() 
	{ 
		if (defaultFont == null) {
			try { 
				defaultFont = new Font( FACE_SYSTEM, STYLE_PLAIN, SIZE_MEDIUM );
			} catch (Exception e) {
				//#debug error 
				System.out.println("Unable to create default font" + e);
			} 
		} 
		return defaultFont;
	} 
 
	/** 
	 * Obtains an object representing a font having the specified face, style, 
	 * and size. If a matching font does not exist, the system will 
	 * attempt to provide the closest match. This method <em>always</em> 
	 * returns 
	 * a valid font object, even if it is not a close match to the request. 
	 *  
	 * @param face - one of FACE_SYSTEM, FACE_MONOSPACE, or FACE_PROPORTIONAL 
	 * @param style - STYLE_PLAIN, or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED 
	 * @param size - one of SIZE_SMALL, SIZE_MEDIUM, or SIZE_LARGE 
	 * @return instance the nearest font found 
	 * @throws IllegalArgumentException - if face, style, or size are not legal values 
	 */ 
	public static Font getFont(int face, int style, int size)
	{ 
		return new Font( face, style, size );
	} 
	 
	/** 
	 * Obtains an object representing a font having the specified face, style, 
	 * and size. If a matching font does not exist, the system will 
	 * attempt to provide the closest match. This method <em>always</em> 
	 * returns 
	 * a valid font object, even if it is not a close match to the request. 
	 *  
	 * @param face one of FACE_SYSTEM, FACE_MONOSPACE, or FACE_PROPORTIONAL 
	 * @param style STYLE_PLAIN, or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED 
	 * @param size  the size either in percent or pixel 
	 * @return instance the nearest font found 
	 * @throws IllegalArgumentException - if face, style, or size are not legal values 
	 */ 
	public static Font getFont(int face, int style, Dimension size)
	{ 
		return new Font( face, style, size );
	} 
 
	/** 
	 * Gets the style of the font. The value is an <code>OR'ed</code> 
	 * combination of 
	 * <code>STYLE_BOLD</code>, <code>STYLE_ITALIC</code>, and 
	 * <code>STYLE_UNDERLINED</code>; or the value is 
	 * zero (<code>STYLE_PLAIN</code>). 
	 *  
	 * @return style of the current font 
	 * @see #isPlain() 
	 * @see #isBold() 
	 * @see #isItalic() 
	 */ 
	public int getStyle() 
	{ 
		return this.style;
	} 
 
	/** 
	 * Gets the size of the font. 
	 *  
	 * @return one of SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE 
	 */ 
	public int getSize() 
	{ 
		return this.size;
	} 
 
	/** 
	 * Gets the face of the font. 
	 *  
	 * @return one of FACE_SYSTEM, FACE_PROPORTIONAL, FACE_MONOSPACE 
	 */ 
	public int getFace() 
	{ 
		return this.face;
	} 
 
	/** 
	 * Returns <code>true</code> if the font is plain. 
	 *  
	 * @return true if font is plain 
	 * @see #getStyle() 
	 */ 
	public boolean isPlain() 
	{ 
		return (this.style == STYLE_PLAIN);
	} 
 
	/** 
	 * Returns <code>true</code> if the font is bold. 
	 *  
	 * @return true if font is bold 
	 * @see #getStyle() 
	 */ 
	public boolean isBold() 
	{ 
		return (this.style & STYLE_BOLD) == STYLE_BOLD ;
	} 
 
	/** 
	 * Returns <code>true</code> if the font is italic. 
	 *  
	 * @return true if font is italic 
	 * @see #getStyle() 
	 */ 
	public boolean isItalic() 
	{ 
		return (this.style & STYLE_ITALIC) == STYLE_ITALIC;
	} 
 
	/** 
	 * Returns <code>true</code> if the font is underlined. 
	 *  
	 * @return true if font is underlined 
	 * @see #getStyle() 
	 */ 
	public boolean isUnderlined() 
	{ 
		return (this.style & STYLE_UNDERLINED) == STYLE_UNDERLINED;
	} 
 
	/** 
	 * Gets the standard height of a line of text in this font. This value 
	 * includes sufficient spacing to ensure that lines of text painted this 
	 * distance from anchor point to anchor point are spaced as intended by the 
	 * font designer and the device. This extra space (leading) occurs below 
	 * the text. 
	 *  
	 * @return standard height of a line of text in this font (a  non-negative value) 
	 */ 
	public int getHeight() 
	{ 
		return this.height;
	} 
 
	/** 
	 * Gets the distance in pixels from the top of the text to the text's 
	 * baseline. 
	 *  
	 * @return the distance in pixels from the top of the text to the text's baseline 
	 */ 
	public int getBaselinePosition() 
	{ 
		return this.baselinePosition;
	} 
 
	/** 
	 * Gets the advance width of the specified character in this Font. 
	 * The advance width is the horizontal distance that would be occupied if 
	 * <code>ch</code> were to be drawn using this <code>Font</code>, 
	 * including inter-character spacing following 
	 * <code>ch</code> necessary for proper positioning of subsequent text. 
	 *  
	 * @param ch - the character to be measured 
	 * @return the total advance width (a non-negative value) 
	 */ 
	public int charWidth(char ch)
	{ 
		return (int)this.paint.measureText("" + ch);
	} 
 
	/** 
	 * Returns the advance width of the characters in <code>ch</code>, 
	 * starting at the specified offset and for the specified number of 
	 * characters (length). 
	 * The advance width is the horizontal distance that would be occupied if 
	 * the characters were to be drawn using this <code>Font</code>, 
	 * including inter-character spacing following 
	 * the characters necessary for proper positioning of subsequent text. 
	 *  
	 * <p>The <code>offset</code> and <code>length</code> parameters must 
	 * specify a valid range of characters 
	 * within the character array <code>ch</code>. The <code>offset</code> 
	 * parameter must be within the 
	 * range <code>[0..(ch.length)]</code>, inclusive. 
	 * The <code>length</code> parameter must be a non-negative 
	 * integer such that <code>(offset + length) &lt;= ch.length</code>.</p> 
	 *  
	 * @param ch - the array of characters 
	 * @param offset - the index of the first character to measure 
	 * @param length - the number of characters to measure 
	 * @return the width of the character range 
	 * @throws ArrayIndexOutOfBoundsException - if offset and length specify an invalid range 
	 * @throws NullPointerException - if ch is null 
	 */ 
	public int charsWidth(char[] ch, int offset, int length)
	{ 
		return (int)this.paint.measureText(new String(ch), offset, length + offset);
	} 
 
	/** 
	 * Gets the total advance width for showing the specified 
	 * <code>String</code> 
	 * in this <code>Font</code>. 
	 * The advance width is the horizontal distance that would be occupied if 
	 * <code>str</code> were to be drawn using this <code>Font</code>, 
	 * including inter-character spacing following 
	 * <code>str</code> necessary for proper positioning of subsequent text. 
	 *  
	 * @param str - the String to be measured 
	 * @return the total advance width 
	 * @throws NullPointerException - if str is null 
	 */ 
	public int stringWidth( String str)
	{ 
		float measureText = this.paint.measureText(str);
		return (int)measureText;
	} 
 
	 
	public void initPaint( Paint p ) {
		p.setTypeface( this.typeface );
		p.setUnderlineText(isUnderlined());
		if (isItalic()) { 
			p.setTextSkewX(-0.25f);
		} else { 
			p.setTextSkewX(0f);			
		} 
		p.setTextSize( this.height );
 
		FontMetricsInt fontMetricsInt = p.getFontMetricsInt();
		this.descent =  fontMetricsInt.descent;
		this.baselinePosition = (int) (p.getTextSize() - p.getFontMetrics().descent) + 1;
	} 
 
	/** 
	 * Gets the total advance width for showing the specified substring in this 
	 * <code>Font</code>.