Search Unity

TextMesh Pro "Invalid_File_Format" Error when attempting to create TMP Font

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by wickedpopular, Aug 21, 2019.

  1. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
    Hello! I'm having a problem creating a TextMeshPro Font. :(

    I am trying to create a bitmap based pixel-perfect sprite font. I have a .fontsettings file I have created from a sprite sheet. This font I have created works like any normal font in the old Unity UI system. However, whenever I try to use it to create a TextMeshPro Font from it using the Font Asset Creator, I get the following error:

    Font Asset Creator - Error Code [Invalid_File_Format] has occurred trying to load the [MyFont] font file. This typically results from the use of an incompatible or corrupted font file.


    I've tried creating TMP Fonts from downloaded ttf files and been successful, but I'm also unable to create one from the default Unity Arial font.

    What can I do to repair my font or make it compatible with TMP? I need to be able to use the font I created from a sprite sheet in my TMP UI objects.
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    The Font Asset creator works with source font files that are .ttf, .ttc and .otf.

    The .fontsettings format is not currently supported. This .fontsettings file essentially contains the character and glyph data which includes the UV coordinates of those glyphs in the atlas texture. The format of the data in the .fontsettings file is different and their UV layout might be flipped but it should be possible to convert this data to the Font Asset format manually or by creating some editor tool to do so.

    A Font Asset contains the font metric / Face Info. This data should be contained in the .fontsettings file. You should be able to create a font asset from that font using the Context Menu - Create - TextMeshPro - Font Asset. Some of the Face Info can only be edited in the Inspector in Debug mode.

    upload_2019-8-21_14-46-26.png

    This font asset should be marked as static.

    Then using the data contained in the fontsettings file, you will need to populate the Font Asset's character and glyph table.

    The first step is to create an entry for each glyph. The Glyph Rect defines the size and position of the glyph in the texture. The metrics are relevant to the layout. The Glyph Rect is the UV data which likely needs to be converted to the match TMP. The glyph Metrics should translate straight in.

    Once each of the glyphs have been defined, you will need to create a character for each assigning a Unicode value and pointing them to the correct glyph index.

    In the image below, you can see the Character 0x41 points to glyph ID 36.

    Make sure the scale is set to 1 for both character and glyphs.

    Each font asset includes a default material which points to a texture. You will need to double click on this material to have it reference your custom atlas texture.

    So create a new font asset (from the source font would be best). Edit the Face Info. Assign your texture to the default material. Then create a single glyph and edit the GlyphRect and Metrics using the data in the fontsettings. Then test it and you will know what needed to be done to convert from one format to the other so you can create a script to automate that instead of having to do it manually (which is still an option).

    You will need to assign one of the TMP bitmap shader for a bitmap atlas texture.
     
  3. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
    I'm sorry, but I've tried to get this working, and I still don't understand the process at all. :(


    Here is the process I went through: The spritesheet my characters are on is 112 x 24, imported at 1PPU. The .fontsettings file has the following settings, which includes the character rect data for the capital letter "A":
    Q1.PNG

    So, I right clicked on this file and created a TMP Font Asset. I then went inside the TMP Font Asset to the Atlas Material, changed the shader to TMP Bitmap, and added my Spritesheet as the texture.

    Next, I went into the Font Asset, which gives me the following Face Info:
    Q2.PNG

    Which I'm not sure how to fill out. I assume that Scale should be 1, but I do not know how to get the information based on my original .fontsettings. Is this information in units, so that if, for example, my characters are 8x8 and I wanted a tab to be 2 character widths, I would set Tab Width to 16?

    Leaving that part for a moment, I set the character and glyph settings for the capital A to the following:
    Q3.PNG

    This obviously doesn't work though. From your description I originally thought "Glyph Rect" was the UV settings from the the Character Rect, but they can't be anything but Integers so they must be the Vert values, right?

    This has been as far as I've been able to get. :(
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    The GlyphRect corresponds to the position in pixels of the glyph in the texture.

    The values for each character in the fontsettings are in texture space. You will need to convert those.

    The (0, 0) coordinate in the texture is the bottom left. So your first glyph is likely at (0, 4, 8, 8). The height of the glyph cannot be a negative value.

    Can you provide me with the texture and fontsettings file? If I have time later tonight, I can take a quick look and post a screenshot of a font asset with FaceInfo and one glyph and character added.
     
    wickedpopular likes this.
  5. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
  6. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
    Hello! I'm really sorry to double post but I just wanted to make sure I had provided the proper information to you. I'm still not really sure how to properly set this up. It's fine if you don't have time to do it, but if so would it be possible to point me in the direction I need to calculate the values myself?

    Thanks!:)
     
  7. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
    Hello! I'm really still stalled out on this. Can anyone help? :(
     
  8. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    I downloaded your sample .unitypackage and created a font asset to which I added two characters.

    I am creating a short video to document the process so you can finish adding the remaining glyphs and characters. I will likely upload this video tomorrow since it is already 12:00 am on my end.

    P.S. I attached the sample custom font asset with only two characters so you can play with it. Just create a <TextMeshPro> object, assign this font asset and add the letter A and B.

    The images for the glyphs and characters are not showing right now in the Glyph and Character table inspector but that is a minor thing which I will look at. The child Material and Texture reference LiberationSans which is the initial font file I used to create this font asset and then manually changed the values including shader assignment on the material and have it reference your texture.

    P.S.S. Updated the sample to include a scene and modified font asset so that is shows in the Glyph and Character table. Internally, it is using an unlit shader which is why the characters look weird in those tables but will render correctly.

    The .zip file contains a short video where I add the glyph and character for the letter C.
     

    Attached Files:

    Last edited: Sep 5, 2019
  9. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
    Hello! :D

    Wow this is more than I was expecting! Thank you so much! Everything is working perfectly and the characters are aligned perfectly on the grid. I think I understand the workflow now too (and it was a lot simpler than I previously understood it to be...:oops:) The only problem is that I seem to be doing something that is preventing my characters from updating when added in the scene?

    Scene2.PNG Scene.PNG

    Here are my settings. These should be correct and the letter C shows up just fine in the character table, but it doesn't seem to ever update in the scene, even if I close the scene and reload it or delete the characters and retype them.

    GlyphTable1.PNG CharacterTable.PNG

    There is also an object in the folder called "MySpriteFont," but it doesn't seem to work in the TMP text box. What is this new asset for?

    Thank you again for all your help! :) I really appreciate all you've done! I've been struggling with this feature for ages so it's a huge relief to finally be able to make progress on it!
     
  10. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Adding the new Glyph and Character should display correctly on the text object so I am not sure other than making sure you use the Capital C and not the lower case c as it is hard to tell which is used in the above image.

    Since your original texture was already sliced, I decided to make a sprite asset to assign Unicode values to those sprite characters. If you were to assign this sprite asset to the local text object in the Extra Settings and type uppercase characters, they would actually show up using the sprite asset. The sprite glyph would need to be adjusted in terms of position but I wanted to also show this other alternative way this could be done.

    In the image below, you can see the letter E and F which are coming from the Sprite Asset. They have a different scale because I didn't adjust it and it (yet) but it shows how this can be done.

    upload_2019-9-5_10-28-6.png

    In this image you can see the adjustments to the sprites Glyph Metrics in the Sprite Asset.

    upload_2019-9-5_10-29-11.png
     
  11. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
    That's strange then... Here is the MyCustomFont with the C and D added. Even when I reload the scene these characters will never display in the correct font and always use the default Liberation font. Am I doing something wrong? :(

    I also tried using the Sprite Font but it only displays characters if they aren't in the current font. So if the default Liberation font is added to the text object it won't work at all. However when I add My Custom Font, it will display the C and D characters, which means they're never being added to My Custom Font for some reason
     

    Attached Files:

    Last edited: Sep 6, 2019
  12. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    I'll try looking at the package you provided later tonight or tomorrow.

    In terms of the Sprite Asset, if the primary font asset already contains those characters then it will not use the sprite asset.
     
  13. wickedpopular

    wickedpopular

    Joined:
    Oct 29, 2016
    Posts:
    20
    Thank you!

    Edit: I don't know what was different but I was able to get it to work. Instead of adding characters in the Debug window I added them by copying the previous value in the main editor window. I don't know why that's different but it worked! :D:D:D
     

    Attached Files:

    Last edited: Sep 6, 2019
  14. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,192
    Just like in the video short video I provided, it is best to do this NOT in debug mode and using the functions I added for that purpose.