hey there. with these changes its possible to easily retrieve the total height of a BitmapText (with multiple lines). Also this patch makes the code compatible with the indentation code conventions from the wiki.
Index: src/com/jmex/angelfont/BitmapFont.java
===================================================================
--- src/com/jmex/angelfont/BitmapFont.java (revision 4766)
+++ src/com/jmex/angelfont/BitmapFont.java (working copy)
@@ -113,7 +113,7 @@
if (c == null && (text.charAt(i) != 'n' && text.charAt(i) != 'r')) {
Logger.getLogger("").log(Level.FINE,
"Character '" + text.charAt(i) + "' is not in alphabet, skipping it.");
- } else if(text.charAt(i) == ' ' && firstCharOfLine) {
+ } else if (text.charAt(i) == ' ' && firstCharOfLine) {
Logger.getLogger("").log(Level.FINE,
"Character '" + text.charAt(i) + "' is blank, skipping it because first char in line.");
} else {
@@ -129,12 +129,12 @@
width = c.getWidth() * sizeScale;
height = c.getHeight() * sizeScale;
}
-
+
if (text.charAt(i) == 'n' || text.charAt(i) == 'r') {
x = 0;
y -= charSet.getLineHeight() * sizeScale;
// float offset = 0f;
-
+
// Justify the last (now complete) line
if (alignment == Align.Center) {
for (int k = 0; k < target.getQuantity(); k++) {
@@ -161,7 +161,7 @@
} // if
} // for
} // if
-
+
// New line without any "carry-down" word
firstCharOfLine = true;
lastLineWidth = lineWidth;
@@ -170,7 +170,6 @@
lineNumber++;
continue;
} // End new line check
-
// Adjust for kerning
float kernAmount = 0f;
@@ -258,6 +257,7 @@
} // for
} // if
+ block.setNumLines(lineNumber);
target.setNumActive(numActive);
return lineWidth;
} // updateText
@@ -299,22 +299,22 @@
if (c == null && (text.charAt(i) != 'n' && text.charAt(i) != 'r')) {
Logger.getLogger("").log(Level.FINE,
"Character '" + text.charAt(i) + "' is not in alphabet, skipping it.");
- } else if(text.charAt(i) == ' ' && firstCharOfLine) {
+ } else if (text.charAt(i) == ' ' && firstCharOfLine) {
Logger.getLogger("").log(Level.FINE,
"Character '" + text.charAt(i) + "' is blank, skipping it because first char in line.");
} else {
- float xOffset = 0;
+ float xOffset = 0;
float yOffset = 0;
float xAdvance = 0;
float width = 0;
float height = 0;
- if (c != null) {
- xOffset = c.getXOffset() * sizeScale;
- yOffset = (charSet.getyOffset() + c.getYOffset()) * sizeScale;
- xAdvance = c.getXAdvance() * sizeScale;
- width = c.getWidth() * sizeScale;
- height = c.getHeight() * sizeScale;
- }
+ if (c != null) {
+ xOffset = c.getXOffset() * sizeScale;
+ yOffset = (charSet.getyOffset() + c.getYOffset()) * sizeScale;
+ xAdvance = c.getXAdvance() * sizeScale;
+ width = c.getWidth() * sizeScale;
+ height = c.getHeight() * sizeScale;
+ }
if (text.charAt(i) == 'n' || text.charAt(i) == 'r' || (lineWidth + xAdvance >= maxWidth)) {
x = block.getTextBox().x;
y -= charSet.getLineHeight() * sizeScale;
@@ -364,25 +364,25 @@
lastLineWidth = lineWidth;
lineWidth = 0f;
} // else
-
- // Justify the previous (now complete) line
- if (alignment == Align.Center) {
- for (int k = 0; k < target.getQuantity(); k++) {
- FontQuad q = target.getQuad(k);
-
- if (q.getLineNumber() == lineNumber) {
- q.setX(q.getX() + block.getTextBox().width / 2f - lastLineWidth / 2f);
- } // if
- } // for
- } // if
- if (alignment == Align.Right) {
- for (int k = 0; k < target.getQuantity(); k++) {
- FontQuad q = target.getQuad(k);
- if (q.getLineNumber() == lineNumber) {
- q.setX(q.getX() + block.getTextBox().width - lastLineWidth);
- } // if
- } // for
- } // if
+
+ // Justify the previous (now complete) line
+ if (alignment == Align.Center) {
+ for (int k = 0; k < target.getQuantity(); k++) {
+ FontQuad q = target.getQuad(k);
+
+ if (q.getLineNumber() == lineNumber) {
+ q.setX(q.getX() + block.getTextBox().width / 2f - lastLineWidth / 2f);
+ } // if
+ } // for
+ } // if
+ if (alignment == Align.Right) {
+ for (int k = 0; k < target.getQuantity(); k++) {
+ FontQuad q = target.getQuad(k);
+ if (q.getLineNumber() == lineNumber) {
+ q.setX(q.getX() + block.getTextBox().width - lastLineWidth);
+ } // if
+ } // for
+ } // if
wordNumber = 1;
lineNumber++;
@@ -465,6 +465,7 @@
} // for
} // if
+ block.setNumLines(lineNumber);
target.setNumActive(numActive);
} // updateTextRect
Index: src/com/jmex/angelfont/BitmapText.java
===================================================================
--- src/com/jmex/angelfont/BitmapText.java (revision 4766)
+++ src/com/jmex/angelfont/BitmapText.java (working copy)
@@ -225,6 +225,10 @@
block.setAlignment(alignment);
}
+ public float getHeight() {
+ return font.getLineHeight(block) * block.getNumLines();
+ }
+
public float getLineHeight() {
return font.getLineHeight(block);
}
Index: src/com/jmex/angelfont/StringBlock.java
===================================================================
--- src/com/jmex/angelfont/StringBlock.java (revision 4766)
+++ src/com/jmex/angelfont/StringBlock.java (working copy)
@@ -48,6 +48,7 @@
private BitmapFont.Align alignment;
private float size;
private boolean kerning;
+ private int numLines;
/**
*
@@ -118,4 +119,12 @@
public void setKerning(boolean kerning) {
this.kerning = kerning;
}
+
+ public void setNumLines(int numLines) {
+ this.numLines = numLines;
+ }
+
+ public int getNumLines() {
+ return numLines;
+ }
}
No newline at end of file
Index: src/jmetest/text/TestBitmapFont.java
===================================================================
--- src/jmetest/text/TestBitmapFont.java (revision 4766)
+++ src/jmetest/text/TestBitmapFont.java (working copy)
@@ -138,6 +138,11 @@
orthoNode.attachChild(txt);
orthoNode.attachChild(txt2);
orthoNode.attachChild(txt3);
+
+ System.out.println(txt.getHeight());
+ System.out.println(txt2.getHeight());
+ System.out.println(txt3.getHeight());
+ System.out.println(txt4.getHeight());
debug.getRootNode().attachChild(orthoNode);
return null;