Hello Guest

Author Topic: Implementing word wrap  (Read 7510 times)

OnlineCop

  • Newbie
  • *
  • Posts: 11
    • View Profile
Implementing word wrap
« on: August 30, 2012, 07:58:00 pm »
If I edit tk2dTextMesh.cs and change "private Vector2 GetMeshDimensionsForString()" to "public Vector2 GetMeshDimensionsForString()", I can iterate over each of my strings and add newlines (\n) as needed to give it a "word wrap" ability on a per-pixel (as opposed to a per-character) basis. This is especially useful when I use a variable-width font, since character-counting makes lines of lowercase "LLLL" and uppercase "WWWW" break very differently.

That said, if I try to determine the height of the text, I notice that the GetMeshDimensionsForString() calculates its height off of the font's lineHeight variable. That height is often different from the actual height of the glyphs being used (the tallest of my glyphs reaches about 42 pixels, while the .fnt file defines lineHeight to be 64).

The attached screenshot shows a popup I've created with a TextMesh, as well as the properties for that TextMesh. I have added a child GameObject and attached a BoxCollider to it to indicate to my word wrap code how large the "word wrap box" needs to be. That makes it easier for my developers to eyeball things without changing (or forgetting to change) the dimensions somewhere.

It does not correctly chop off the last line, however. What I want to do is calculate the height of the font, calculate the height of this GameObject+BoxCollider combo, and chop off anything that spills over. I should have lines 1-5, but line 6 (which has the text, "excrement at you. Stand back!") needs to actually be removed (I might add an ellipsis '...' to the end of "they will fling", but that's more of an aside).

Is there a way to calculate the height of the used glyphs, instead of relying on the lineHeight property? I assume it would take the character's 'height' and 'yoffset' into account to ensure that the tall characters (like a pipe '|') and hanging letters (like lowercase 'g' or 'y') are all handled. It might also take the lineSpacing multiplied by some scaling factor into account.

...Unless I'm just reinventing the wheel here and you already have some sort of "concatenate long lines" option that I can just call.

Ideas?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Implementing word wrap
« Reply #1 on: August 30, 2012, 08:10:58 pm »
You may be reinventing the wheel here.
Have you tried this? http://unikronsoftware.com/2dtoolkit/forum/index.php?action=dlattach;topic=131.0;attach=302

This does word wrap (not character wrap), and correctly measures each character to see if it goes past the end of the border. The border is visible (and adjustable) when the textmesh is drawn. This isn't the final version though, but the final one will be in the next release.

p.s. you need to register to get access to this. Its in the private support area.

OnlineCop

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Implementing word wrap
« Reply #2 on: August 31, 2012, 06:55:35 pm »
This is very nice. Thanks!

Could I request an additional feature (for the final release)? Concatenation.

If we could have a "Max Height" option that chops off anything that tries to Word Wrap past that maximum, we could have 1-line text that simply concatenates if someone tries to type too long (again, not on a per-chararacter but on a per-pixel boundary). Or, if it spans 2+ lines, it will wrap normally until it hits the Max Height, and any text past that is removed.

mangoocean

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Implementing word wrap
« Reply #3 on: January 27, 2017, 02:59:14 am »

Have you tried this? http://unikronsoftware.com/2dtoolkit/forum/index.php?action=dlattach;topic=131.0;attach=30

p.s. you need to register to get access to this. Its in the private support area.

This file is no longer available - does it live somewhere else now?

Thanks