OSDN Git Service

Make BitmapFont's calculation of line width match
authorPSpeed42@gmail.com <PSpeed42@gmail.com@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Fri, 21 Oct 2011 09:07:27 +0000 (09:07 +0000)
committerPSpeed42@gmail.com <PSpeed42@gmail.com@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Fri, 21 Oct 2011 09:07:27 +0000 (09:07 +0000)
BitmapText's version.

git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@8475 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

engine/src/core/com/jme3/font/BitmapFont.java

index ed402e6..f97c102 100644 (file)
@@ -185,6 +185,28 @@ public class BitmapFont implements Savable {
         // just to create a Letters object for the sole purpose of\r
         // getting a text size.  It's less efficient but at least it\r
         // would be accurate.  \r
+        \r
+        // And here I am mucking around in here again...\r
+        //\r
+        // A font character has a few values that are pertinent to the\r
+        // line width:\r
+        //  xOffset\r
+        //  xAdvance\r
+        //  kerningAmount(nextChar)\r
+        //\r
+        // The way BitmapText ultimately works is that the first character\r
+        // starts with xOffset included (ie: it is rendered at -xOffset).\r
+        // Its xAdvance is wider to accomodate that initial offset.\r
+        // The cursor position is advanced by xAdvance each time.\r
+        //\r
+        // So, a width should be calculated in a similar way.  Start with\r
+        // -xOffset + xAdvance for the first character and then each subsequent\r
+        // character is just xAdvance more 'width'.\r
+        // \r
+        // The kerning amount from one character to the next affects the\r
+        // cursor position of that next character and thus the ultimate width \r
+        // and so must be factored in also.\r
+        \r
         float lineWidth = 0f;\r
         float maxLineWidth = 0f;\r
         char lastChar = 0;\r
@@ -211,17 +233,27 @@ public class BitmapFont implements Savable {
                     }\r
                 }\r
                 if (!firstCharOfLine){\r
-                    lineWidth += c.getXOffset() * sizeScale;\r
-                    lineWidth += findKerningAmount(lastChar, theChar) * sizeScale;\r
+                    lineWidth += findKerningAmount(lastChar, theChar) * sizeScale;                    \r
                 } else {\r
+                    // The first character needs to add in its xOffset but it\r
+                    // is the only one... and negative offsets = postive width\r
+                    // because we're trying to account for the part that hangs\r
+                    // over the left.  So we subtract. \r
+                    lineWidth -= c.getXOffset() * sizeScale;                    \r
                     firstCharOfLine = false;\r
                 }\r
                 float xAdvance = c.getXAdvance() * sizeScale;\r
                 \r
                 // If this is the last character, then we really should have\r
-                // only add its width\r
+                // only add its width.  The advance may include extra spacing\r
+                // that we don't care about.\r
                 if (i == text.length() - 1) {\r
                     lineWidth += c.getWidth() * sizeScale;\r
+                    \r
+                    // Since theh width includes the xOffset then we need\r
+                    // to take it out again by adding it, ie: offset the width\r
+                    // we just added by the appropriate amount.\r
+                    lineWidth += c.getXOffset() * sizeScale;                      \r
                 } else {                 \r
                     lineWidth += xAdvance;\r
                 }\r