⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 font.java

📁 Micro Window Toolkit(MWT)是一个用于开发J2ME用户界面(UI)的工具包。它具有友好
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 */
	public Font(InputStream is, char[] charset, int charSpacing, int size, int color) throws IOException {
		this(is,charset,size,color); // micro font
		this.charSpacing = charSpacing;
		this.type = TYPE_MAPPED;
	}
	
	
	/**
	 * Creates a font giving the char images and specifying the charset.
	 * @param images
	 * @param charset The charset
	 * @param charSpacing The space within chars (negative values are valid)
	 * @since 1.1
	 */
	public Font(Image[] images, char[] charset, int charSpacing) {
		this.charSpacing = charSpacing;
		this.charset = new char[charset.length];
		System.arraycopy(charset,0,this.charset,0,charset.length);
		this.images = new Image[images.length];
		System.arraycopy(images,0,this.images,0,images.length);
		
		for(int i=0; i<images.length ;i++)
			if(images[i].getHeight()>this.size)
				this.size = images[i].getHeight();
		this.spaceWidth = this.size/2;
		
		// sort the charset (a binary search will be used later)
		for(int i=0; i<this.charset.length ;i++)
			for(int j=i+1; j<this.charset.length ;j++)
				if(this.charset[j] < this.charset[i]) {
					final char tc = this.charset[i];
					this.charset[i] = this.charset[j];
					this.charset[j] = tc;
					final Image ti = this.images[i];
					this.images[i] = this.images[j];
					this.images[j] = ti;
				}
		this.type = TYPE_MAPPED;
	}
	
	
	
	
	
//	public Font(InputStream is, int charSpacing) throws IOException {
//		DataInputStream dis = new DataInputStream(is);
//		int imgdataLength = dis.readInt();
//		byte[] imgdata = new byte[imgdataLength];
//		dis.read(imgdata);
//		Image image = Image.createImage(imgdata, 0, imgdata.length);
//		imgdata = null;
//		int charsetLength = dis.readInt();
//		char[] charset = new char[charsetLength];
//		int[] widths = new int[charsetLength];
//		for(int i=0; i<charsetLength ;i++) {
//			charset[i] = dis.readChar();
//			widths[i] = dis.readInt();
//		}
//		dis.close();
//		_Font(image, widths, charset, charSpacing);
//	}
	
	/**
	 * Creates a font-strip.
	 * @param img
	 * @param widths array of each char width
	 * @param charset
	 * @param charSpacing
	 * @since 1.2
	 */
	public Font(Image img, char[] charset, int[] widths, int charSpacing) {
		_Font(img,widths,charset,charSpacing);
	}
	final private void _Font(Image img, int[] widths, char[] charset, int charSpacing) {
		this.strip = img;
		this.size = img.getHeight();
		this.widths = new int[widths.length];
		for(int i=0; i<widths.length ;i++) this.widths[i] = widths[i];
		this.charset = new char[charset.length];
		for(int i=0; i<charset.length ;i++) this.charset[i] = charset[i];
		this.spaceWidth = this.size/2;
		this.charSpacing = charSpacing;
		this.offsets = new int[widths.length];
		for(int i=1; i<widths.length ;i++) {
			this.offsets[i] = this.offsets[i-1] + widths[i-1]; 
		}
		this.type = TYPE_STRIP;
		
		// sort the charset (a binary search will be used later)
		for(int i=0; i<this.charset.length ;i++)
			for(int j=i+1; j<this.charset.length ;j++)
				if(this.charset[j] < this.charset[i]) {
					final char tc = this.charset[i];
					this.charset[i] = this.charset[j];
					this.charset[j] = tc;
					
					final int ti = this.widths[i];
					this.widths[i] = this.widths[j];
					this.widths[j] = ti;
					
					final int to = this.offsets[i];
					this.offsets[i] = this.offsets[j];
					this.offsets[j] = to;					
				}
	}
	
	
	/**
	 * Gets a clone of this font.
	 * @see <a href="#extending">Extending a Font</a>
	 */
	public Font clone() {
		Font f = new Font(0,0,0,0);
		copy(f);
		return f;
	}
	
	/**
	 * Copies this font into the given font.
	 * @see <a href="#extending">Extending a Font</a>
	 */
	protected void copy(Font font) {
		font.color = this.color;
		font.face = this.face;
		font.size = this.size;
		font.style = this.style;
		
		font.images = this.images;
		font.charset = this.charset;
		font.charSpacing = this.charSpacing;
		font.spaceWidth = this.spaceWidth;
		font.strip = this.strip;
		font.widths = this.widths;
		font.offsets = this.offsets;
		
		font.type = this.type;
	}


	/** Checks if this font is a bitmap font. */
	final public boolean isBitmapFont() { return type != TYPE_SYSTEM; }

	/** Gets the font type: {@link #TYPE_MAPPED}, {@link #TYPE_STRIP} or {@link #TYPE_SYSTEM}  */
	final public int getType() { return type; }
	
	/** Gets the color of the font. */
	final public int getColor() { return color; }
	/** Sets the color of the font if it's not a bitmap font */
	final public void setColor(int color) { if(!isBitmapFont()) this.color = color; }
	
	/** Gets the face of the font. */
	final public int getFace() { return face; }
	/** Sets the face of the font. */
	final public void setFace(int face) { this.face = face; }
	
	/** Gets the size of the font. */
	final public int getSize() { return size; }
	/** Sets the size of the font if it's not a bitmap font */
	final public void setSize(int size) { if(!isBitmapFont()) this.size = size; }
	
	/** Gets the style of the font. */
	final public int getStyle() { return style; }
	/** Sets the style of the font. */
	final public void setStyle(int style) { this.style = style; }

	/**
	 * Gets the charset length or -1 if it is not available.<br>
	 * The default implementation, returns -1 if this is a system native font,
	 * otherwise this returns the charset length. 
	 */
	public int getCharsetLength() {
		return isBitmapFont()? charset.length : -1;
	}
	
	/**
	 * Copies the charset into the given buffer at the current offset.<br>
	 * If {@link #getCharsetLength()} returns -1, this does nothing.
	 */
	public void getCharset(char[] buffer, int offset, int length) {
		if(!isBitmapFont()) return;
		System.arraycopy(charset,0,buffer,offset,length);
	}
	
	/**
	 * Only for {@link #TYPE_MAPPED} fonts, otherwise throws a {@link RuntimeException}
	 * Copies the images into the given image array at the current offset.<br>
	 * The images length is the same as {@link #getCharsetLength()}
	 * @param images
	 * @param offset
	 * @param length
	 * @since 1.2
	 */
	public void getImages(Image[] images, int offset, int length) {
		if(!isBitmapFont()) return;
		System.arraycopy(this.images,0,images,offset,length);
	}
	
	
	/** Gets the font height. */
	public int getHeight() {
		return isBitmapFont()? size : javax.microedition.lcdui.Font.getFont(face,style,size).getHeight();
	}

	/** Gets the width for the given string. */
	public int getWidth(String s) {
		return isBitmapFont()? _getWidth(s) : javax.microedition.lcdui.Font.getFont(face,style,size).stringWidth(s);
	}
	
	/**
	 * Writes the string into the given graphics object.<br>
	 * It uses the width and height parameters to fit the string according to the anchor.<br>
	 * The anchor value must be a <a href="./Component.html#align">Component ALIGN constant</a>.
	 * This method uses {@link #write(Graphics, String, int, int, int)}.
	 */
	final public void write(Graphics g, String s, int x, int y, int width, int height, int anchor) {
		if((anchor & Graphics.HCENTER) != 0) x += width/2;
		else if((anchor & Graphics.RIGHT) != 0) x += width;
		
		if((anchor & Graphics.VCENTER) != 0) {
			this.write(g, s, x, y + (height-this.getHeight())/2, Graphics.TOP | (anchor & 13));
			return;
		}
		else if((anchor & Graphics.BOTTOM) != 0) {
			this.write(g, s, x, y + height-this.getHeight(), Graphics.TOP | (anchor & 13));
			return;
		}
		this.write(g,s,x,y,anchor);
	}
	
//	/**
//	 * Same as write(g,s,x,y,0,0,0).
//	 * @param g
//	 * @param s
//	 * @param x
//	 * @param y
//	 * @since 1.2
//	 */
//	final public void write(Graphics g, String s, int x, int y) { write(g,s,x,y,0,0,0); }
	
	/**
	 * Writes the string into the given graphics object.
	 * The anchor value must be a <a href="./Component.html#align">Component ALIGN constant</a>.
	 */
	protected void write(Graphics g, String s, int x, int y, int anchor) {
		if(isBitmapFont()) _write(g,s,x,y,anchor);
		else {
			g.setFont(javax.microedition.lcdui.Font.getFont(face,style,size));
			g.setColor(color);
			g.drawString(s,x,y,anchor);
		}
	}
	
	
	
	// ===============
	// class MicroFont
	// ===============
	
	// Returns the index in the charset/image array for the given char
	final private int _getIndex(char c) {
	    int high = charset.length, low = -1, probe;
	    while(high - low > 1) {
	        probe = (high + low) / 2;
	        if(charset[probe] < c) low = probe;
	        else high = probe;
	    }
	    if(!(high == charset.length || charset[high] != c)) return high;
		return -1;
	}
	
	// This private constructor is the MicroFont's format reader
	private Font(InputStream is, char[] charset, int size, int color) throws IOException {
		this.color = color;
		DataInputStream dis = new DataInputStream(is);
		
		// the MicroFont format (not implemented yet)
		dis.readByte();
		
		// read the entire charset
		final int charsetLength = dis.readShort();
		final char[] fullCharset = new char[charsetLength];
		for(int i=0; i<fullCharset.length ;i++)
			fullCharset[i] = dis.readChar();
		
		// the original max height
		final int maxh =  dis.readByte();
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -