I’m reading the red book and I’m in the section about bitmaps and fonts.
What I’m trying to do is setup a bitmap for each letter (uppercase/lowercase) and number and store the data into an inner-class I call RasterFontInfo.
The class RasterFont contains RasterFontInfo and sets up the Map.
A bitmap for each char can be called from a Map that is setup like:
Map<Character, RasterFontInfo> map;
RasterFontInfo contains width, height, x0, y0, and a ByteBuffer which holds the bitmap data.
So far, I’ve been pretty successful but I’m now blocked with the following problem:
http://img209.imageshack.us/img209/7146/javalwjglfontproblem.jpg
Uploaded with ImageShack.us
All of the letters are showing up correctly except for m, G, J, M, O, Q, and 1. I’ve tried looking for patterns between these but I can’t see any.
I’m hoping someone can give insight as to why the letters show up like this.
If you need the code I have the following:
The code I’m using to draw each letter looks like:
glTranslatef(0.0f, 0.0f, zTranslation);
glRasterPos2f(-3.2f, 0f);
char[] phrase = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
for(char c : phrase) {
RasterFontInfo rfi = characterMap.get(c);
glBitmap(rfi.getWidth(), rfi.getHeight(), (float)rfi.getX0(), (float)rfi.getY0(), (float)rfi.getWidth()+2, 0, rfi.getBuffer());
}
Code for deriving a RasterFontInfo object:
private RasterFontInfo deriveRasterFontInfo(char c) {
/* keeps track of width and height of font raster */
int width = 0, height = 0;
String stringValueOfC = String.valueOf(c);
/* Temporary drawing area for c */
BufferedImage temporaryBufferedImage = new BufferedImage(1024, 1024,
BufferedImage.TYPE_INT_RGB);
/* Acquire graphics object to draw char to drawing area */
Graphics2D g2d = (Graphics2D) temporaryBufferedImage.getGraphics();
g2d.setFont(getFont());
/* Acquire FontMetrics object to get geometrical data from font */
FontMetrics fontMetrics = g2d.getFontMetrics();
/* Acquire geometrical font data */
int charHeight = fontMetrics.getHeight();
int charWidth = fontMetrics.stringWidth(stringValueOfC);
int charDescent = fontMetrics.getDescent();
Dimension charDimension = new Dimension(charWidth, charHeight+charDescent);
g2d.drawString(stringValueOfC, 0, charHeight);
/* Acquire a sub-image that only contains the drawing area of the char */
BufferedImage bi = temporaryBufferedImage.getSubimage(0, 0, (int)charDimension.getWidth(), (int)charDimension.getHeight());
char[][] bitmapArray = new char[bi.getHeight()][multipleOfEight(bi.getWidth())];
for(char[] ch : bitmapArray)
java.util.Arrays.fill(ch, '0');
/* setup bitmap array based on values read from the subimage's RGB coordinates */
/* the result is a 2D-array filled with 0's and 1's */
for(int i = bi.getHeight()-1; i >= 0; --i) {
for(int j = bi.getWidth()-1; j >= 0; --j) {
if(bi.getRGB(j,i) == -16777216)
bitmapArray[i][j] = '0';
else if (bi.getRGB(j,i) == -1)
bitmapArray[i][j] = '1';
}
}
/* Clear unused bits from bitmapArray */
bitmapArray = clearUnusedBits(bitmapArray);
/* Temporary list to hold chunks of translated data from bitmap */
ArrayList<Byte> chunkList = new ArrayList<Byte>();
int numChunks = bitmapArray[0].length / 8;
/* loop through each array of bitmapArray beginning from the end */
for(int lineNumber = bitmapArray.length - 1; lineNumber >= 0; --lineNumber) {
/* sets up data to insert into chunk list */
String currentLine = new String(bitmapArray[lineNumber]);
String[] chunks = new String[numChunks];
getChunks(chunks, currentLine);
int[] binaryChunks = new int[numChunks];
getBinaryChunks(binaryChunks, chunks);
for(int binaryChunk : binaryChunks)
chunkList.add((byte) binaryChunk);
/* computes height and width of font raster */
height = bitmapArray.length;
int x1 = -1, x2 = -1;
if( currentLine.contains("1") ) {
/* count the distance from the first 1 to the last 1 */
/* the greatest distance found instantiates the width of the raster of the font */
x1 = currentLine.indexOf('1');
x2 = currentLine.lastIndexOf('1');
int leadingZeros = currentLine.substring(0, x1).length();
int distance = x2 - x1 + 1 + leadingZeros;
width = Math.max(width, distance);
}
}
/* Prints each bitmap */
System.out.println("---");
for(char[] ch : bitmapArray) {
System.out.println(new String(ch));
}
System.out.println("---");
/* determine x0 and y0, used for glBitmap */
int x0 = 0;
int y0 = 0;
/* add chunkList elements to ByteBuffer */
ByteBuffer buffer = BufferUtils.createByteBuffer(chunkList.size());
for(Byte b : chunkList)
buffer.put(b);
buffer.flip();
/* Setup and return RasterFontInfo used for Map */
RasterFontInfo rfi = new RasterFontInfo(buffer, width, height, x0, y0);
return rfi;
}
And if you need all of the files, they’re located here: (2 .java files)
https://skydrive.live.com/?cid=f6516fcd92fde78e#cid=F6516FCD92FDE78E&id=F6516FCD92FDE78E!130