📄 font.java
字号:
/*
* MWT - Micro Window Toolkit
* Copyright (C) 2007 Lucas Domanico - lucazd@gmail.com
*
* Licensed under the terms of the GNU Lesser General Public License:
* http://www.opensource.org/licenses/lgpl-license.php
*
* For further information visit:
* http://j2me-mwt.sourceforge.net/
*/
package mwt;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* <p>The font class represents fonts, which are used to render text in a visible way.</p>
*
* <p>MWT allows to use custom bitmap fonts or system fonts, depending on how the font is created.</p>
*
* <br>
* <br>
*
* <h3>System Native Fonts</h3>
* <p>System native fonts are created using {@link #Font(int, int, int, int)}.</br>
* You can change the size, color, face and style using the appropriate values.</p>
* <p>The main drawback of system fonts is that they may not fit with the look and feel of your
* application as they are device dependent.</p>
* <p>A system native font uses internally {@link javax.microedition.lcdui.Font}.</p>
*
* <br>
* <br>
*
* <h3>Custom Bitmap Fonts</h3>
* <p>Custom bitmap fonts are created using {@link #Font(Image, char[], int[], int)}
* or {@link #Font(Image[], char[], int)}.<br>
* </p>
* A custom bitmap font is an immutable object, that means, you can't change the size or color.<br>
*
* Please check the <a href="http://j2me-mwt.sourceforge.net/microfontmaker.html">
* MicroFont-Maker</code>command line tool.<br>
*
<table width="800" border="0" cellspacing="4" cellpadding="0" style="border: 1px black solid">
<tr>
<td width="45"> </td>
<td colspan="3" bgcolor="#DBDBDB"><div align="center">System Native Fonts </div></td>
<td colspan="3" bgcolor="#DBDBDB"><div align="center">Custom Bitmap Fonts </div></td>
</tr>
<tr>
<td rowspan="3" bgcolor="#DBDBDB"><div align="center">Size</div></td>
<td width="73" rowspan="3" valign="top" bgcolor="#CEDBFF"><p>Accepts constants values</p> </td>
<td width="232" bgcolor="#CEDBFF">{@link #SIZE_MEDIUM}</td>
<td width="90" bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_size_med.png" width="90" height="30" /></td>
<td width="60" rowspan="3" valign="top" bgcolor="#CFFFCA">Any value</td>
<td width="225" bgcolor="#CFFFCA">17 (pixels)</td>
<td width="43" bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_size_17.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link #SIZE_SMALL}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_size_small.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">15 (pixels)</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_size_15.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link #SIZE_LARGE}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_size_large.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">19 (pixels)</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_size_19.png" width="90" height="30" /></td>
</tr>
<tr>
<td rowspan="4" bgcolor="#EEEEEE"><div align="center">Styles</div></td>
<td rowspan="4" valign="top" bgcolor="#EAEFFF">Accepts constants values</td>
<td bgcolor="#EAEFFF">{@link #STYLE_PLAIN}</td>
<td bgcolor="#EAEFFF"><img src="{@docRoot}/resources/font_sys_style_plain.png" width="90" height="18" /></td>
<td colspan="3" rowspan="4" valign="top" bgcolor="#ECFFEA">Not available (you can use different bitmap fonts for each style)</td>
</tr>
<tr>
<td bgcolor="#EAEFFF">{@link #STYLE_BOLD}</td>
<td bgcolor="#EAEFFF"><img src="{@docRoot}/resources/font_sys_style_bold.png" width="90" height="18" /></td>
</tr>
<tr>
<td bgcolor="#EAEFFF">{@link #STYLE_ITALIC}</td>
<td bgcolor="#EAEFFF"><img src="{@docRoot}/resources/font_sys_style_italic.png" width="90" height="18" /></td>
</tr>
<tr>
<td bgcolor="#EAEFFF">{@link #STYLE_UNDERLINED}</td>
<td bgcolor="#EAEFFF"><img src="{@docRoot}/resources/font_sys_style_underlined.png" width="90" height="18" /></td>
</tr>
<tr>
<td rowspan="3" bgcolor="#DBDBDB"><div align="center">Face</div></td>
<td rowspan="3" valign="top" bgcolor="#CEDBFF"><p>Accepts constants values</p> </td>
<td bgcolor="#CEDBFF">{@link #FACE_SYSTEM}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_style_plain.png" width="90" height="18" /></td>
<td colspan="3" rowspan="3" valign="top" bgcolor="#CFFFCA">Not available, however, you don't need this when using custom fonts </td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link #FACE_MONOSPACE}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_face_monospace.png" width="90" height="18" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link #FACE_PROPORTIONAL}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_style_plain.png" width="90" height="18" /></td>
</tr>
<tr>
<td bgcolor="#EEEEEE"><div align="center">Color</div></td>
<td bgcolor="#EAEFFF">Any </td>
<td bgcolor="#EAEFFF">0xA1C632</td>
<td bgcolor="#EAEFFF"><img src="{@docRoot}/resources/font_sys_color_0xA1C632.png" width="90" height="18" /></td>
<td bgcolor="#ECFFEA">Any </td>
<td bgcolor="#ECFFEA">0xA1C632</td>
<td bgcolor="#ECFFEA"><img src="{@docRoot}/resources/font_bmp_color_0xA1C632.png" width="90" height="18" /></td>
</tr>
<tr>
<td rowspan="9" bgcolor="#DBDBDB"><div align="center">Anchor</div></td>
<td rowspan="9" valign="top" bgcolor="#CEDBFF">Accepts constants values</td>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_TOP_LEFT}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_top_left.png" width="90" height="30" /></td>
<td rowspan="9" valign="top" bgcolor="#CFFFCA">Accepts constants values</td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_TOP_LEFT}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_top_left.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_TOP_CENTER}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_top_center.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_TOP_CENTER}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_top_center.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_TOP_RIGHT}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_top_right.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_TOP_RIGHT}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_top_right.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_MIDDLE_LEFT}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_middle_left.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_MIDDLE_LEFT}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_middle_left.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_MIDDLE_CENTER}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_middle_center.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_MIDDLE_CENTER}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_middle_center.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_MIDDLE_RIGHT}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_middle_right.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_MIDDLE_RIGHT}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_middle_right.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_BOTTOM_LEFT}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_bottom_left.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_BOTTOM_LEFT}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_bottom_left.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_BOTTOM_CENTER}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_bottom_center.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_BOTTOM_CENTER}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_bottom_center.png" width="90" height="30" /></td>
</tr>
<tr>
<td bgcolor="#CEDBFF">{@link Component#ALIGN_BOTTOM_RIGHT}</td>
<td bgcolor="#CEDBFF"><img src="{@docRoot}/resources/font_sys_anchor_bottom_right.png" width="90" height="30" /></td>
<td bgcolor="#CFFFCA">{@link Component#ALIGN_BOTTOM_RIGHT}</td>
<td bgcolor="#CFFFCA"><img src="{@docRoot}/resources/font_bmp_anchor_bottom_right.png" width="90" height="30" /></td>
</tr>
</table>
*
*
*
* <h3><a name="extending">Extending</a></h3>
* <p>Classes that extend a Font must override the following methods:
* <ul>
* <li>{@link #clone()}</li>
* <li>{@link #copy(Font)}</li>
* <li>{@link #getHeight()}</li>
* <li>{@link #getWidth(String)}</li>
* <li>{@link #write(Graphics, String, int, int, int)}</li>
* </ul>
* The contructor must call the system native font {@link #Font(InputStream, char[], int, int, int)}.
* </p>
* Example:
* <pre>
* class MyFont extends Font {
* int newAttribute = ...;
*
* public MyFont(int newAttribute) {
* super(0,0,0,0);
* this.newAttribute = newAttribute;
* }
*
* protected void copy(MyFont f) {
* super.copy(f);
* f.newAttribute = this.newAttribute;
* }
*
* private MyFont() {}
* public Font clone() {
* MyFont f = new MyFont();
* copy(f);
* return f;
* }
*
* public int getHeight() {
* // new implementation
* }
*
* public int getWidth(String s) {
* // new implementation
* }
*
* protected void write(Graphics g, String text, int x, int y, int anchor) {
* // new implementation
* }
* }
* </pre>
*/
public class Font {
/** The plain style constant. */
static final public int STYLE_PLAIN = 0;
/** The bold style constant. */
static final public int STYLE_BOLD = 1;
/** The italicized style constant. */
static final public int STYLE_ITALIC = 2;
/** The underlined style constant. */
static final public int STYLE_UNDERLINED = 4;
/** The "small" system-dependent font size. */
static final public int SIZE_SMALL = 8;
/** The "medium" system-dependent font size. */
static final public int SIZE_MEDIUM = 0;
/** The "large" system-dependent font size. */
static final public int SIZE_LARGE = 16;
/** The "system" font face. */
static final public int FACE_SYSTEM = 0;
/** The "monospace" font face. */
static final public int FACE_MONOSPACE = 32;
/** The "proportional" font face. */
static final public int FACE_PROPORTIONAL = 64;
static final public int TYPE_SYSTEM = 0;
static final public int TYPE_MAPPED = 1;
static final public int TYPE_STRIP = 2;
private int type;
private int color;
private int face;
private int size;
private int style;
private int charSpacing; // the space between chars
private int spaceWidth; // the space char ' ' width
private char[] charset;
private Image[] images;
private Image strip; // since 1.2
private int[] widths; // since 1.2
private int[] offsets; // since 1.2
/** Creates a system native font. */
public Font(int color, int face, int size, int style) {
this.color = color;
this.face = face;
this.size = size;
this.style = style;
this.type = TYPE_SYSTEM;
}
/**
* Creates a bitmap font using an {@link InputStream} formatted using
* <code>MicroFont-Maker</code> tool.<br>
* Optionally, you can specify which chars must be loaded.<br>
* <b>NOTE:</b> this method uses {@link Image#createImage(javax.microedition.lcdui.Image)}
* implementation which in some devices causes problems.
* Un such case, use {@link #Font(Image[], char[], int)} instead.<br>
* @param is the {@link InputStream} formatted using MicroFont-Maker tool
* @param charset The charset specifying which chars must be loaded or null to load the entire charset
* @param charSpacing The space within chars (negative values are valid)
* @param size The size value or 0 to use default size
* @param color The color value
* @throws IOException might be thrown by the {@link InputStream}
* @deprecated
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -