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:

Uploaded with
ImageShack.usAll 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:
1 2 3 4 5 6 7 8
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| private RasterFontInfo deriveRasterFontInfo(char c) { int width = 0, height = 0; String stringValueOfC = String.valueOf(c);
BufferedImage temporaryBufferedImage = new BufferedImage(1024, 1024, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) temporaryBufferedImage.getGraphics(); g2d.setFont(getFont()); FontMetrics fontMetrics = g2d.getFontMetrics(); 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); 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'); 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'; } } bitmapArray = clearUnusedBits(bitmapArray); ArrayList<Byte> chunkList = new ArrayList<Byte>(); int numChunks = bitmapArray[0].length / 8; for(int lineNumber = bitmapArray.length - 1; lineNumber >= 0; --lineNumber) { 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); height = bitmapArray.length; int x1 = -1, x2 = -1; if( currentLine.contains("1") ) { 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); } } System.out.println("---"); for(char[] ch : bitmapArray) { System.out.println(new String(ch)); } System.out.println("---"); int x0 = 0; int y0 = 0; ByteBuffer buffer = BufferUtils.createByteBuffer(chunkList.size()); for(Byte b : chunkList) buffer.put(b); buffer.flip(); 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