Search Unity

TextMesh Pro Chinese punctuation placement problem

Discussion in 'UGUI & TextMesh Pro' started by abcjjy, Sep 25, 2020.

  1. abcjjy

    abcjjy

    Joined:
    Mar 6, 2015
    Posts:
    35
    Here is my code

    Code (CSharp):
    1.  
    2. public class TestTMP : MonoBehaviour
    3. {
    4.     public TextMeshProUGUI text;
    5.  
    6.     void Start()
    7.     {
    8.         var f2 = new Font("/System/Library/Fonts/PingFang.ttc");
    9.         var tmf = TMP_FontAsset.CreateFontAsset(f2);
    10.         text.font = tmf;
    11.         text.text = "\u6587\u5b57\uff0c\u6807\u70b9\u3002";
    12.     }
    13. }
    14.  
    Unity 2018.4.27
    Mac 10.15
    TextMeshPro 1.5.1

    Output: 截屏2020-09-25 15.25.26.png

    Period and comma are expected to be placed at bottom like this 截屏2020-09-25 15.27.38.png
     
    MrLucid72 likes this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Looking at the font in a font editing tool, both of those glyphs are placed in the middle of the line as seen below.

    upload_2020-9-25_1-25-33.png

    This appears to be a design choice on the part of the font designer.

    You can always edit individual glyphs in the font asset glyph table inspector. In this case, you would adjust the ybearing of this particular glyph as seen below.

    upload_2020-9-25_1-31-35.png

    Image showing "." and "," along with \u3002 and \uFF0C unmodified metrics.

    Default values for \u3002
    upload_2020-9-25_1-33-35.png

    Edited values to adjust the Y Bearing (BY) to place it on the baseline.
    upload_2020-9-25_1-34-33.png

    These values are relative to the sampling point size so this is just to show what values you would adjust.

    Note the Character Table would include \U3002 which then references a glyph index which is this 29264.

    To visualize these font metrics, you can add the TMP_TextInfoDebugTool.cs to any TMP text object. This script is located in the TMP Examples & Extras.
     
  3. abcjjy

    abcjjy

    Joined:
    Mar 6, 2015
    Posts:
    35
    Thanks for your quick response. I tried BY and BX. It works. But there still must be something wrong. Here is the sample text displayed with the same font in Mac Font Book app.
    截屏2020-09-25 17.29.05.png

    And this is rendered with legacy Text component: 截屏2020-09-25 17.38.58.png

    I'm not familiar with font metrics. But I am a Chinese native speaker. I do know which one is correct. Font Book sample is the standard form. Legacy Text component result is not very ideal but acceptable.

    Please help.
     
    Last edited: Sep 25, 2020
  4. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    995
    I feel like my font does this too. Self reminder (not at home) to check!
     
  5. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    995
    Seems like false positive: Indeed, many fonts prefer this. Eg, Discord prefers the v-centered Asian period.
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Here is a comparison with NotoSansCJK where in this case the full stop is baseline aligned.

    upload_2020-9-25_12-9-59.png

    Whereas for PingFang.ttc, the full stop is in the middle vertically. This is by design from whoever designed this font.
     
  7. abcjjy

    abcjjy

    Joined:
    Mar 6, 2015
    Posts:
    35
    How does legacy text component render it aligned the baseline? @Stephan_B
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    This positioning of the glyph is font specific.

    Are you observing the alignment being different with the same font between the legacy text system and TMP?
     
  9. abcjjy

    abcjjy

    Joined:
    Mar 6, 2015
    Posts:
    35
    Yes, they are different with same font. Legacy text is consistent with Mac system font book.
     
  10. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Just had a chance to look at this and here are my findings.

    First, let's compare how PingFang.ttc renders between UI.Text and TMP.

    upload_2021-7-23_1-5-59.png
    UI.Text - Top, TMP - Bottom

    As you can see, the ',' and '。' render above the baseline in both components.

    However, this is because PingFang.ttc is a TrueType Collection which contains multiple font faces, where the first face is PingFang HK - Regular as seen below.

    upload_2021-7-23_1-9-13.png

    Now, if I choose the PingFang SC - Regular font face, the text renders as follows with the characters at the baseline.

    upload_2021-7-23_1-11-55.png

    Note: The ability to select the font face from a TrueType Collection (TTC) is a new feature that will be in the next release of the TMP package.

    Until then, you have to specifically use the PingFang SC font file instead of the TTC.
     
    Last edited: Jul 23, 2021
  11. abcjjy

    abcjjy

    Joined:
    Mar 6, 2015
    Posts:
    35
    Thank you very much! You are the true expert!