Author Topic: Font atlas / pixel size broken? (possible fix)  (Read 4185 times)

iandunlop_oefun

  • Guest
Font atlas / pixel size broken? (possible fix)
« on: December 30, 2012, 10:42:13 PM »
Hi,

I ran into this issue a month ago and since then I've been patching every release of NGUI. I thought it might be better to share this and see what others think.

You won't see this issue if your font is part of an existing sprite atlas. The problem exists when you try to create different font atlases to support retina using the font reference feature.

The main issue appears in UILabel.cs / line 478 & 513:

  1. float pixelSize = (font.atlas != null) ? font.atlas.pixelSize : 1f;

The result is that font atlases always end up with a pixel size of 1.0 (which is bad).

My current fix is as follows (tested on v2.2.6 c):

in UIFont.cs / line 39 insert:

  1. // Size in pixels for the sake of MakePixelPerfect functions.
  2. [HideInInspector][SerializeField] float mPixelSize = 1f;
  3.  

in UIFont.cs / line 72 insert:

  1. /// <summary>
  2. /// Pixel size is a multiplier applied to widgets dimensions when performing MakePixelPerfect() pixel correction.
  3. /// Most obvious use would be on retina screen displays. The resolution doubles, but with UIRoot staying the same
  4. /// for layout purposes, you can still get extra sharpness by switching to an HD atlas that has pixel size set to 0.5.
  5. /// </summary>
  6.  
  7.  
  8. public float pixelSize
  9. {
  10.      get
  11.      {
  12.           return (mReplacement != null) ? mReplacement.pixelSize : mPixelSize;
  13.      }
  14.      set
  15.      {
  16.           if (mReplacement != null)
  17.           {
  18.                mReplacement.pixelSize = value;
  19.           }
  20.           else
  21.           {
  22.                float val = Mathf.Clamp(value, 0.25f, 4f);
  23.  
  24.                if (mPixelSize != val)
  25.                {
  26.                     mPixelSize = val;
  27.                     MarkAsDirty();
  28.                }
  29.           }
  30.      }
  31. }
  32.  

in UILabel.cs / line 478 replace with:

  1. float pixelSize = (font.atlas != null) ? font.atlas.pixelSize : font.pixelSize;
  2.  

in UILabel.cs / line 513 replace with:

  1. float pixelSize = (font.atlas != null) ? font.atlas.pixelSize : font.pixelSize;
  2.  

in UIFontInspector / line 243 insert:

  1. float pixelSize = EditorGUILayout.FloatField("Pixel Size", mFont.pixelSize, GUILayout.Width(120f));
  2.  
  3. if (pixelSize != mFont.pixelSize)
  4. {
  5.      NGUIEditorTools.RegisterUndo("Font Change", mFont);
  6.      mFont.pixelSize = pixelSize;
  7. }
  8.  

Is there a better solution to this problem?

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Font atlas / pixel size broken? (possible fix)
« Reply #1 on: December 31, 2012, 06:44:28 AM »
Ideally the pixel size should be hidden on the UIFontInspector if the font is located within an atlas. Other than that, looks good. I'll put it on my TODO list to add it to the next version.

iandunlop_oefun

  • Guest
Re: Font atlas / pixel size broken? (possible fix)
« Reply #2 on: December 31, 2012, 04:31:49 PM »
Awesome, thanks!

iandunlop_oefun

  • Guest
Re: Font atlas / pixel size broken? (possible fix)
« Reply #3 on: January 28, 2013, 09:39:06 PM »
Ideally the pixel size should be hidden on the UIFontInspector if the font is located within an atlas. Other than that, looks good. I'll put it on my TODO list to add it to the next version.

I updated to v2.3.1 and sadly this is still broken. Do you have an idea on when this will be fixed?

Cheers,
   Ian

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Font atlas / pixel size broken? (possible fix)
« Reply #4 on: January 28, 2013, 10:56:08 PM »
Reminders like these help. ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Font atlas / pixel size broken? (possible fix)
« Reply #5 on: January 28, 2013, 11:12:29 PM »
I've just added it. You will see it in 2.3.2.

iandunlop_oefun

  • Guest
Re: Font atlas / pixel size broken? (possible fix)
« Reply #6 on: January 29, 2013, 03:36:33 AM »
I've just added it. You will see it in 2.3.2.

Thanks!