Author Topic: Dynamic font support for NGUI  (Read 105623 times)

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #30 on: January 10, 2013, 04:07:59 AM »
yeah there's this weird thing with vertical offsets. Frankly I haven't bothered too much about it since I was testing with smaller fonts like 12 to 18px, but I agree that this is problematic.
I'll try to have another look at this issue too. It seems we don't fully understand the values that the CharacterInfo class gives us for positioning fonts. In fact it might make sens to try to figure that out even outside of the NGUI context

zh4ox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Dynamic font support for NGUI
« Reply #31 on: January 10, 2013, 04:18:00 AM »
yeah there's this weird thing with vertical offsets. Frankly I haven't bothered too much about it since I was testing with smaller fonts like 12 to 18px, but I agree that this is problematic.
I'll try to have another look at this issue too. It seems we don't fully understand the values that the CharacterInfo class gives us for positioning fonts. In fact it might make sens to try to figure that out even outside of the NGUI context
I've searched the Unity's forum, no answer to it, After many times experiment, I found that when set the font asset's size to ONE, a magic solution come out.
  1.                                         float m = 0.8203215f;
  2.                                         float n = 1.0f - m;
  3.                                         float offsetY = (int)(m * dynamicFontSize) + n;
  4.  
  5.                                         v0.x = scale.x * (charInfo.vert.x + x);
  6.                                         v0.y = scale.y * (y + charInfo.vert.y - offsetY);
  7.  
  8.                                         v1.x = v0.x + scale.x * charInfo.vert.width;
  9.                                         v1.y = v0.y + scale.y * charInfo.vert.height;
  10.  
  11.                                         u0.x = charInfo.uv.xMin;
  12.                                         u0.y = charInfo.uv.yMax;
  13.  
  14.                                         u1.x = charInfo.uv.xMax;
  15.                                         u1.y = charInfo.uv.yMin;
  16.  
  17.                                         x += mSpacingX + (int)charInfo.width;
  18.  
  19.                                         for (int b = 0; b < 4; ++b)
  20.                                                 cols.Add(color);
  21.  
  22.                                         verts.Add(new Vector3(v1.x, v0.y));
  23.                                         verts.Add(new Vector3(v1.x, v1.y));
  24.                                         verts.Add(new Vector3(v0.x, v1.y));
  25.                                         verts.Add(new Vector3(v0.x, v0.y));
  26.  
  27.                                         if (charInfo.flipped)
  28.                                         {
  29.                                                 uvs.Add(new Vector2(u1.x, u0.y));
  30.                                                 uvs.Add(new Vector2(u0.x, u0.y));
  31.                                                 uvs.Add(new Vector2(u0.x, u1.y));
  32.                                                 uvs.Add(new Vector2(u1.x, u1.y));
  33.                                         }
  34.                                         else
  35.                                         {
  36.                                                 uvs.Add(new Vector2(u1.x, u0.y));
  37.                                                 uvs.Add(new Vector2(u1.x, u1.y));
  38.                                                 uvs.Add(new Vector2(u0.x, u1.y));
  39.                                                 uvs.Add(new Vector2(u0.x, u0.y));
  40.                                         }
set the font asset's size to 1, and add the offset to vert.y calculation, then it fits all font sizes, here is my program's result
« Last Edit: January 10, 2013, 04:34:43 AM by zh4ox »

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #32 on: January 10, 2013, 04:45:43 AM »
Cool, I'll try this out too!

jarjin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Dynamic font support for NGUI
« Reply #33 on: January 10, 2013, 05:23:27 AM »
@Unisip Can I reset Font Texture to empty??????????????? how ?

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #34 on: January 10, 2013, 06:41:31 AM »
Why would you want to do that?
As far as I can tell, you can't. Unity manages the font and clears it up only when needed.

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #35 on: January 10, 2013, 09:17:10 AM »
@zh4ox, it sort of works, but I'm not really into magic numbers. I'd rather have an explanation of how that Y value is supposed to work. I guess I'll post on the Unity forums to see if someone figured it out

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #36 on: January 10, 2013, 10:07:09 AM »
Ok I *think* I might have something that makes more sense.
No more magic number, it relies on the font import size (which unfortunately can't be accessed from code, it seems, so I set the value to a constant 16, since when you import a font, its size is set to 16.
zh4ox, can you test this and tell me what you think?

zh4ox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Dynamic font support for NGUI
« Reply #37 on: January 10, 2013, 10:18:21 PM »
Ok I *think* I might have something that makes more sense.
No more magic number, it relies on the font import size (which unfortunately can't be accessed from code, it seems, so I set the value to a constant 16, since when you import a font, its size is set to 16.
zh4ox, can you test this and tell me what you think?
it still works not perfectily, I use YouYuan font, size 16 and test UILabel with char size 16, 64. Here is the result.


All the text is above the baseline, btw, 0.8203215f is not real magic number, it' seems the baseline of font size 1, and I have modified ur code, as u wish, now there r no limitation on font asset's size. Here is my result


U can test the modification on your machine
« Last Edit: January 10, 2013, 10:21:59 PM by zh4ox »

wsycarlos

  • Guest
Re: Dynamic font support for NGUI
« Reply #38 on: January 10, 2013, 11:04:15 PM »
@wsicarlos: what I found in my tests was the following:
- when you add new chars to the font texture, if the texture has to be rebuilt (to change its size), it will only be rebuilt with those new chars. All older chars are removed, and the texturerebuilt callbacks are invoked. On callback, you can have other gameobjects displaying text (UILabels, but also text meshes if needed) request their characters to be added to the font texture again. Any object that doesn't request its characters again takes the risk of not finding them in the texture anymore. Anyway, see my code for it, a diff with the original NGUI version will point you to the changes I made.

@Unisip
Thank you for your answer, your test is correct, but that's why i think is a problem.
The first time you mentioned about removing the unused chars, i am very confused about that, and considered that might be a problem.
After last night's testing, i finally solve this problem and now shows to everyone. You can check my test scene to see what happened when the unity rebuild the texture when it is full.
You didn't get this problem because you request the new chars at the same time when you get the callback, and unity collect all the request in this circle, and rebuilt the texture.

Anyone want to fully understand how the dynamic font works can use my test demo, and btw, unity's documentation is quite "easy" to understand with just few things on it!

And anyone want to use dynamic font in your own code should request the new character info just at the moment when you get the callback.

zhangjiq1983

  • Guest
Re: Dynamic font support for NGUI
« Reply #39 on: January 11, 2013, 01:48:50 AM »
Very useful. Thank you.

anangigi

  • Guest
Re: Dynamic font support for NGUI
« Reply #40 on: January 11, 2013, 03:05:25 AM »
very nice ,thanx

jarjin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Dynamic font support for NGUI
« Reply #41 on: January 11, 2013, 08:45:59 AM »
@wsycarlos can i remove unuse character now?

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #42 on: January 11, 2013, 10:49:10 AM »
The unity dynamic font API doesn't let you remove unused characters, as far as i can tell. But it's not clear to me why you would want to do that clean up work anyway, is it for video memory usage reasons?

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: Dynamic font support for NGUI
« Reply #43 on: January 11, 2013, 05:45:50 PM »
I just want to say nice work everyone. I spent a lot of time today just grinding through the UIFont class myself to see if I could figure out what the problem was in regards to the original vertical spacing and why there was such a discrepancy especially as the font got larger.

The best I could gauge was that it seemed to work if you approximated the .ttf asset import size to be in POINT and the UIFontInspector size to be in the asset's PIXEL value (24pt-30px, 30pt-40px) for example. This would place the text MUCH better in the vertical bounding area of the widget. However, I found out there is still a problem with letters that go below the baseline such as a q, y, or p interfering with the next line's characters.

I spent way too much time, but I ended up just adding a 10px Y spacing to the UIFont and it appears to work better. However, wow, what a pain in the butt. I really hope that we can maybe figure out an elegant solution to handle these types of below baseline characters.
« Last Edit: January 11, 2013, 05:48:41 PM by Ferazel »

jarjin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Dynamic font support for NGUI
« Reply #44 on: January 11, 2013, 09:10:59 PM »
@Unisip yes!! Thank you for your answer. I want to reduce video memory usage.