📄 bitmapfontsfamily.java
字号:
package BitmapFont;import java.util.*;import java.awt.*;import java.awt.image.*;import java.io.*;import gl4java.*;import gl4java.awt.*;import gl4java.GLFunc;import gl4java.utils.glut.*;import BitmapFont.*;/** * Class used to generate, read, and render bitmap fonts. * * To generate fonts, run * * 1. cd .. * 2. java BitmapFonts/BitmapFontsFamily fontfamilyname min max step * * this will generate fonts with size from min to max with increment step * and the bitmap data are written into a file with name fontfamilyname.font * * To read fonts, call * * BitmapFontsFamily bff = new BitmapFontsFamily(fontfamilyname); * bff.read(); * * Alarm: the file fontfamilyname.font must exist. * * To initialize GL, call setupGL(...), after which, call drawString(...) * to draw the string on the GL context. * * @author Li Zhang * **/public class BitmapFontsFamily { public String fname; public int nofonts; public int maxHeight; BitmapFonts[] fonts; int[] fontOffsets; public int[] indexByHeight; // index of the font in the vector by height public BitmapFontsFamily(String fn) { fname = fn; } public void read() throws IOException { DataInputStream fin; InputStream fs = getClass().getResourceAsStream(fname); if (fs != null) { fin = new DataInputStream(new BufferedInputStream(fs)); System.out.println("found font in jar: "+fs); } else { FileInputStream ffs = new FileInputStream(fname); System.out.println("looking for font on filesystem"); fin = new DataInputStream(ffs); } nofonts = fin.readInt(); maxHeight = fin.readInt(); indexByHeight = new int[maxHeight+1]; //System.out.println("nofonts "+nofonts+" maxHeight "+maxHeight); fonts = new BitmapFonts[nofonts]; for(int i=0; i<=maxHeight; i++) indexByHeight[i] = 0; for(int i=0; i<nofonts; i++) { BitmapFonts f = new BitmapFonts(); f.read(fin); fonts[i]= f; indexByHeight[f.height] = i; //System.out.println("i "+i+" f.height "+f.height); } int ind = 0; for(int i=0; i<=maxHeight; i++) { if(indexByHeight[i]==0) indexByHeight[i] = ind; else ind = indexByHeight[i]; } } public void write() throws IOException{ DataOutputStream fout = new DataOutputStream (new FileOutputStream(fname)); fout.writeInt(nofonts); fout.writeInt(maxHeight); for(int i=0; i<nofonts; i++) { BitmapFonts f = fonts[i]; f.write(fout); } } public void generate(int fmin, int fmax, int fstep) throws Exception { nofonts = ((fmax-fmin)/fstep+1); maxHeight = 0; fonts = new BitmapFonts[nofonts]; int i = 0; for(int fsize = fmin; fsize<=fmax; fsize+=fstep) { BitmapFonts f = new BitmapFonts(); System.out.println("fmin "+fmin+" fmax "+fmax+" fstep "+fstep+" nofonts "+nofonts+" fsize "+fsize); f.generate(fname, fsize); fonts[i++] = f; if(maxHeight<f.height) maxHeight = f.height; } } public void print(boolean verbose) { System.out.println(fname+" font number: "+nofonts+" maxHeight: "+maxHeight); for(int i=0; i<nofonts; i++) { BitmapFonts f = fonts[i]; f.print(verbose); } } public void setupGL(GLFunc gl) { // setup the call list for opengl int mch = BitmapFonts.maxChar+1; int totalBitmaps = nofonts*mch; int baseOffset = gl.glGenLists(totalBitmaps); gl.glPixelStorei(GLEnum.GL_UNPACK_ALIGNMENT, 1); int[] tmpfo = new int[nofonts]; for(int i=0; i<nofonts; i++) { BitmapFonts f = fonts[i]; int offset = i*mch; tmpfo[i] = offset; f.setupGL(gl, offset); } fontOffsets = new int[maxHeight+1]; for(int i=0; i<=maxHeight; i++) { fontOffsets[i] = tmpfo[indexByHeight[i]]; } } public void drawString(GLFunc gl, String s, int height) { // System.out.println(s.getBytes()+" height: "+height); int h = (height>maxHeight)?maxHeight:height; h = (height<0)?0:h; gl.glPushAttrib(GLEnum.GL_LIST_BIT); gl.glListBase(fontOffsets[h]); gl.glCallLists(s.length(), GLEnum.GL_UNSIGNED_BYTE, s.getBytes()); gl.glPopAttrib(); } public int stringWidth(String s, int height) { int h = (height>maxHeight)?maxHeight:height; h = (height<0)?0:h; BitmapFonts f = fonts[indexByHeight[h]]; int swidth = 0; for(int i=0; i<s.length(); i++) { swidth += f.charWidth(s.charAt(i)); } return swidth; } public int charWidth(String s, int height, int i) { int h = (height>maxHeight)?maxHeight:height; h = (height<0)?0:h; BitmapFonts f = fonts[indexByHeight[h]]; int cwidth = f.charWidth(s.charAt(i)); return cwidth; } public int getDescent(int height) { int h = (height>maxHeight)?maxHeight:height; h = (height<0)?0:h; BitmapFonts f = fonts[indexByHeight[h]]; return f.getDescent(); } public int fontPixelHeight(int height) { int h = (height>maxHeight)?maxHeight:height; h = (height<0)?0:h; BitmapFonts f = fonts[indexByHeight[h]]; return f.getDescent(); } public int getAscent(int height) { int h = (height>maxHeight)?maxHeight:height; h = (height<0)?0:h; BitmapFonts f = fonts[indexByHeight[h]]; return f.ascent; } public static void main(String[] args) throws Exception { String fontfamily = args[0]; int minfontsize = Integer.parseInt(args[1]); int maxfontsize = Integer.parseInt(args[2]); int stepfontsize = Integer.parseInt(args[3]); BitmapFontsFamily f = new BitmapFontsFamily(fontfamily); f.generate(minfontsize, maxfontsize, stepfontsize); f.write(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -