CGE does not cache fonts with similar URL, size and charset?

I haven’t verified it yet and am basing my assumption on the log, but it seems that if I load UIs designed from separate .castle-user-interface files with their own TCastleFont, CGE will create a new font texture even if they are completely identical. If this is the case then it is very concerning, especially since I plan to include support for Japanese charsets in the future (huge texture font with a long loading time!) or if I design a UI intended as items in a list and load it multiple times.

We indeed don’t cache the underlying font images / glyphs for now. So if you use TCastleFont with the same settings (URL etc.) in multiple designs, each design will load it independently.

I agree it’s a problem, I have TODO about this.

For now, the workarounds (for cases when it is problematic) are to not customize font by creating TCastleFont instances in the editor. Instead:

  1. To change the default font, change it in CastleSettings.xml, Customize look by CastleSettings.xml | Manual | Castle Game Engine . Or assign different value to Container.DefaultFont.
  2. To control it more, change font from code. You can then create one/few (as little as you need) TCastleFont instances, and share them as needed across multiple designs.

I know, the above is complicated – and it means you cannot customize font at design-time (except CastleSettings.xml, other advises above mean that you only see customized font at run-time). So we should make it better some day :slight_smile:

I chose option 2 but took a step further: I manually parse the JSON to identify UIs with custom fonts, remove the custom font from the JSON, and assign the appropriate TCastleFont instance to the deserialized UIs. This way I can keep custom fonts in the design file.

1 Like