Search Unity

TextMesh Pro Font size in WorldSpace?

Discussion in 'UGUI & TextMesh Pro' started by Yandalf, Mar 8, 2022.

  1. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Hey everyone!
    I'm working on a system where I want to dynamically place TextMeshPro-Text objects in the world within a set boundary. Whatever text is being shown needs to fit within these bounds, and each object needs to have the same visual text size regardless of font (different fonts need to show up equally big).
    I've been looking around quite a bit how I could calculate a desired font size based on how big (tall) in the game world I want the text to show up, but I'm not finding anything on this.
    I've been looking at the FontAsset.FaceInfo class to see if anything like the LineHeight could help, but there seems to be no direct relation between this and the world size.
    Anyone know how to actually do this?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The Face metrics / info are relative to the sampling point size. As such, if a text object's point size is the same as the sampling point size, then the actual line height will be the same. If the point size of the text object is half the sampling point size then the text metrics will be half.

    For example: Here is the Face Info of one of my font assets. As seen below, the sampling point size is 90 and the line height is 122.94.

    upload_2022-3-8_15-43-55.png

    I created a new text object with a point size of 90 thus a scale of 1:1.

    I then added the TMP_TextInfoDebugTool.cs script which is located in the TMP Examples & Extras to help visualize these metrics as seen below.

    upload_2022-3-8_15-53-14.png

    Looking at the above image, we can see the Ascender is 9 unit or 90 pixels above the baseline as it should be as per the Face Info Ascender value. The Descender is 32.94 pixels below the baseline and the line height as expected as well.

    So provided the text object is displayed at 1:1, you can compute the height of any line of text or character by looking at the Face and Glyph metrics depending on what you are after.

    The TMP_TextInfoDebugTool.cs uses the content of the textInfo to display all this information. The textInfo contains information about each character, word, line, links, etc. and the mesh of the text object.
     
    Yandalf likes this.
  3. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Thanks for the reply! So looking at this and checking the DebugInfo myself, looks like I'd have to make my calculations based on the Cap Line and Base Line.
    This dimension will be the height at default sampling point size, so if I then divide this by the world size I desire I should get the font size I have to use, if I understand things correctly.

    Just out of curiosity, I've been fiddling a bit with the various Face Info settings while inspecting the font with debug lines and while some settings do affect the placement of the glyph, other settings like Ascent Line, Mean Line, and Descent Line don't seem to do anything. What are these used for?
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You should be using the Ascender and Descender as those typically represent the height of the tallest character in the font.

    The Cap line typically represents the height of the upper case letters.

    Line height is Ascender - Descender + line gap.

    With regards to Ascent and Descent line this depends on the vertical text alignment. If top aligned, the text will be positioned where the ascent line is aligned with the top of the RectTransform. If bottom aligned, the text will be positioned where the descent line is aligned with the bottom of the RectTransform.
     
    lweist3317, DrViJ and Yandalf like this.
  5. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    I see, but in my case I'm using numbers only, which all seem to fall neatly between Cap and Base.
    I'm also having the somewhat curious case here that my font has a positive Descent Line value. I'm assuming that's not meant to happen?
     
    Last edited: Mar 13, 2022
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The Ascent line value is always positive and the Descent line negative since it is below the baseline which is typically at zero.