Unicode doesn't seem to work with TextField in native

It appears to be loading the font. This works on html5 and does appear to be using the font there but if I build for native, non-Roman text doesn’t appear even though the font I’m using supports it.

var textField = new TextField();`
var textFormat = new TextFormat();
textFormat.font = Assets.getFont('fonts/osakamono.ttf').fontName;
textField.setTextFormat(textFormat);
text.text = 'テスト';
addChild(textField);

If you are using OpenFL version 8.x.x, there’s a known issue that doesn’t process UTF strings correctly. It’s fixed in OpenFL 9.0.2.

If you want to continue using openfl ver. 8.x.x, do the following:

  • Open the src/openfl/_internal/text/TextLayout.hx. On Windows that’s C:\HaxeToolkit\haxe\lib\openfl\8,9,7\src\openfl\_internal\text\TextLayout.hx
  • Go to line ~143
  • Replace the code with this code:
#if (neko || mac || hl)
// other targets still uses dummy positions to make UTF8 work
// TODO: confirm
__hbBuffer.addUTF8(text, 0, -1);
#else
__hbBuffer.addUTF16(untyped __cpp__('(uintptr_t){0}', text.wc_str()), text.length, 0, -1);
#end

Please see https://github.com/openfl/openfl/commit/928dd46b3aa49df73ba79e4f807e244edf18b8e0 for how the correct file should look like.

This will enable OpenFL to use UTF16 strings when needed and will solve your problem.

Thanks to MSGhero for providing with the solution.

1 Like

I upgraded to 9.0.2 and still have the problem. If I remember right, I was avoiding 9.x because of a compilation issue so on the plus side, it seems that other problem went away. But yeah it seems to still not work on native (mac in this case, though I can try linux or windows to see if there’s a difference) but does on html5.

I had this exact problem where I couldn’t use different languages ( Asian languages, Cyrillic, some funky characters from European languages etc.) and the fix I’ve posted above fixed the issue for me. I know it’s annoying to edit the OpenFL source files, but it actually helps. And BTW, I develop my project in the 8.7.9 build of OpenFL, so it works.

Try it.