/** @see org.newdawn.slick.Font#getHeight(java.lang.String) */ public int getHeight(String text) { DisplayList displayList = null; if (displayListCaching) { displayList = (DisplayList) displayLists.get(text); if (displayList != null && displayList.height != null) return displayList.height.intValue(); } int lines = 0; int maxHeight = 0; for (int i = 0; i < text.length(); i++) { int id = text.charAt(i); if (id == '\n') { lines++; maxHeight = 0; continue; } // ignore space, it doesn't contribute to height if (id == ' ') { continue; } CharDef charDef = chars[id]; if (charDef == null) { continue; } maxHeight = Math.max(charDef.height + charDef.yoffset, maxHeight); } maxHeight += lines * getLineHeight(); if (displayList != null) displayList.height = new Short((short) maxHeight); return maxHeight; }
/** @see Font#drawString(float, float, String, Color, int, int) */ public void drawString(float x, float y, String text, Color col, int startIndex, int endIndex) { fontImage.bind(); col.bind(); GL.glTranslatef(x, y, 0); if (displayListCaching && startIndex == 0 && endIndex == text.length() - 1) { DisplayList displayList = (DisplayList) displayLists.get(text + "" + startIndex + "" + endIndex); if (displayList != null) { GL.glCallList(displayList.id); } else { // Compile a new display list. displayList = new DisplayList(); displayList.text = text; int displayListCount = displayLists.size(); if (displayListCount < DISPLAY_LIST_CACHE_SIZE) { displayList.id = baseDisplayListID + displayListCount; } else { displayList.id = eldestDisplayListID; displayLists.remove(eldestDisplayList.text); } displayLists.put(text + "" + startIndex + "" + endIndex, displayList); GL.glNewList(displayList.id, SGL.GL_COMPILE_AND_EXECUTE); render(text, startIndex, endIndex); GL.glEndList(); } } else { render(text, startIndex, endIndex); } GL.glTranslatef(-x, -y, 0); }
/** @see org.newdawn.slick.Font#getWidth(java.lang.String) */ public int getWidth(String text) { DisplayList displayList = null; if (displayListCaching) { displayList = (DisplayList) displayLists.get(text); if (displayList != null && displayList.width != null) return displayList.width.intValue(); } int maxWidth = 0; int width = 0; CharDef lastCharDef = null; for (int i = 0, n = text.length(); i < n; i++) { int id = text.charAt(i); if (id == '\n') { width = 0; continue; } if (id >= chars.length) { continue; } CharDef charDef = chars[id]; if (charDef == null) { continue; } if (lastCharDef != null) width += lastCharDef.getKerning(id); lastCharDef = charDef; if (i < n - 1) { width += charDef.xadvance; } else { width += charDef.width; } maxWidth = Math.max(maxWidth, width); } if (displayList != null) displayList.width = new Short((short) maxWidth); return maxWidth; }
/** * Returns the distance from the y drawing location to the top most pixel of the specified text. * * @param text The text that is to be tested * @return The yoffset from the y draw location at which text will start */ public int getYOffset(String text) { DisplayList displayList = null; if (displayListCaching) { displayList = (DisplayList) displayLists.get(text); if (displayList != null && displayList.yOffset != null) return displayList.yOffset.intValue(); } int stopIndex = text.indexOf('\n'); if (stopIndex == -1) stopIndex = text.length(); int minYOffset = 10000; for (int i = 0; i < stopIndex; i++) { int id = text.charAt(i); CharDef charDef = chars[id]; if (charDef == null) { continue; } minYOffset = Math.min(charDef.yoffset, minYOffset); } if (displayList != null) displayList.yOffset = new Short((short) minYOffset); return minYOffset; }