Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (714) Games in Android Showcase (214) games submitted by our members Games in WIP (787) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Word Wrapping formula.  (Read 1717 times) 0 Members and 1 Guest are viewing this topic.
SkyAphid
 « Posted 2012-05-23 21:07:49 »

So, I'm writing a word wrapping function for my text renderer in openGL, which, already works. Just my problem now is to get word wrapping working :S

So, here's the formula:

 1  2  3  4  5  6  7  8  9 `for (int a = 0; a < text.length(); a ++){         float frame = (float)((textValues[a] - 32) * charWidth) / fontWidth;         float charWidthf = (float) charWidth / fontWidth;                  int yy = y + ((a / boundWidth) * charHeight) + 1;         int xx = (x + (charWidth * (a)));                  //...OpenGL part      }`

charWidth and charHeight in this case are 5 and 7. fontWidth is the size of the image, which is the charWidth multiplied by 95, as we use 95 ASCII characters.

Now, yy works. It pushes the text downwards the further you get along with the text according to the boundWidth, which is how many chars are allowed horizontally before it needs to be pushed down for wrapping. The problem here is xx, which keeps going right regardless of the push of the wrap.

So, what I need is a mathematical formula so that xx will go back to the starting x position everytime it's wrapped.

Sorry if this a hard question, I'm terrible at explaining this stuff.

it just werks
BoBear2681

JGO Coder

Medals: 19

 « Reply #1 - Posted 2012-05-23 21:22:51 »

If you're rendering one character at a time, why not just increment "xx" by the character width for each char, and whenever you're going to go past the right-hand side, simply reset xx to the starting value and increment yy by the font height?  This doesn't break on word boundaries of course, but you don't appear to be doing that anyway.
davedes
 « Reply #2 - Posted 2012-05-23 21:24:28 »

Word wrapping generally depends on things like whitespace, word breaks (i.e. punctuation), and so forth. So it's not so much a "mathematical formula" you need, as a way to check characters and respond accordingly.

TWL is pretty solid with word wrapping. LibGDX also has some word wrapping code that might help. Both are open source so you can see how they did it.

If you just want to wrap text to a given width, with no regard to whitespace or word breaks, something really basic might look like:
 1  2  3  4  5  6  7  8  9  10  11  12 `startX = 50, startY = 50; //start positionx = startX;y = startY;for each glyph {     if (x + glyph.width > maxWidth || glyph.chr == '\n') {          //bump down a line          x = startX;          y += lineHeight + lineSpacing;     }     glyph.draw(x, y);     x += glyph.width; //also increase by xadvance, kerning info, etc.}`

Also IMO you shouldn't hard-code things like having 95 ASCII characters unless you are under some very serious constraints (i.e. Java4K). You'll regret it later, when you realize how restricted your hard-coded system is. For the sake of flexibility and easier rendering, just use a Glyph object that has its own x/y position in the glyph page, its own width/height, kerning info, etc.

SkyAphid
 « Reply #3 - Posted 2012-05-23 21:47:21 »

Word wrapping generally depends on things like whitespace, word breaks (i.e. punctuation), and so forth. So it's not so much a "mathematical formula" you need, as a way to check characters and respond accordingly.

TWL is pretty solid with word wrapping. LibGDX also has some word wrapping code that might help. Both are open source so you can see how they did it.

If you just want to wrap text to a given width, with no regard to whitespace or word breaks, something really basic might look like:
 1  2  3  4  5  6  7  8  9  10  11  12 `startX = 50, startY = 50; //start positionx = startX;y = startY;for each glyph {     if (x + glyph.width > maxWidth || glyph.chr == '\n') {          //bump down a line          x = startX;          y += lineHeight + lineSpacing;     }     glyph.draw(x, y);     x += glyph.width; //also increase by xadvance, kerning info, etc.}`

Also IMO you shouldn't hard-code things like having 95 ASCII characters unless you are under some very serious constraints (i.e. Java4K). You'll regret it later, when you realize how restricted your hard-coded system is. For the sake of flexibility and easier rendering, just use a Glyph object that has its own x/y position in the glyph page, its own width/height, kerning info, etc.

I hard coded 95 to a static integer so it's easy to recognize and change later, but the reason I did so is because I will only use the ASCII values 32-127, ever. lol. The first 31 are irrelevant to what I need. But I understand what you mean. Thank you.

I'll be extending my text splicing method so that it sees full words and makes sure they are split too, instead of the \n thing. I'm not really a fan of having to keep track of that kind of stuff.

Thank you both for input

it just werks
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 CopyableCougar4 (102 views) 2017-03-24 15:39:42 theagentd (84 views) 2017-03-24 15:32:08 Rule (145 views) 2017-03-19 12:43:22 Rule (132 views) 2017-03-19 12:42:17 Rule (138 views) 2017-03-19 12:36:21 theagentd (150 views) 2017-03-16 05:07:07 theagentd (147 views) 2017-03-15 22:37:06 theagentd (126 views) 2017-03-15 22:32:18 theagentd (121 views) 2017-03-15 22:31:11 ral0r2 (154 views) 2017-03-03 11:52:41
 basil_ 20x philfrei 15x Apo 12x dime26 10x Archive 9x FrozenShade 8x bornander 7x SteveSmith 7x FabulousFellini 7x KaiHH 6x Spasi 6x Riven 5x orangepascal 5x Ecumene 5x theagentd 4x CJC 4x
 List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05SF/X Librariesby SkyAphid2017-03-02 06:38:56SF/X Librariesby SkyAphid2017-03-02 06:38:32SF/X Librariesby SkyAphid2017-03-02 06:38:05SF/X Librariesby SkyAphid2017-03-02 06:37:51
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org