How to display utf-8 Japanese characters in Panda3D?

I will use the Boxing Robots tutorial program as an example:

self.title = OnscreenText(text=“Panda3D: Tutorial - Actors”,
style=1, fg=(0,0,0,1),
pos=(0.8,-0.95), scale = .07)
self.escapeEventText = self.genLabelText(“ESC: “+u’\u3042’, 0)
self.akeyEventText = self.genLabelText(”[A]: Robot 1 Left Punch”, 1)
self.skeyEventText = self.genLabelText("[S]: Robot 1 Right Punch", 2)
self.kkeyEventText = self.genLabelText("[K]: Robot 2 Left Punch", 3)
self.lkeyEventText = self.genLabelText("[L]: Robot 2 Right Punch", 4)

With this, the u’\u3042’ should correspond to the hiragana ‘あ’ (the ‘a’ character) but it does not show in the label in the game.

I have tried using

-- coding: utf-8 --

at the top.

I have tried using

from pandac.PandaModules import loadPrcFileData
loadPrcFileData("", “”“text-encoding utf8"”")

that I saw from an obscure Spanish 2009 manual made by someone (on Google Docs).

I have tried

unicodeText = u’u\3042’
self.escapeEventText = self.genLabelText("ESC: "+unicodeText.encode(‘utf-8’), 0)

I have tried placing

text-encoding utf8

in the Config.prc file.

I am using the most recent build of Panda3d 1.7.0 and Python build 2.5.2. I have been able to show ‘latin-1’ characters but not Japanese characters. Can anyone enlighten me on how it should be done?

hm. quite some time ago when i played with utf stuff.
as far as i can see you covered pretty mucheverything important on the code side.

that’d be

  1. -- coding: utf-8 --

2)text-encoding utf8 in the config file.
3) actually using unicode strings…

what i’m missing somehow is your font. are you sure it actually contains the letters you want to display?

if unsure try to load a font where you know for sure. config parameter would be something like
text-default-font Cyberbit.ttf
or whatever font name you want to use.

that’s pretty much the only difference i see between my code, and yours. thought i never tried with japanese letters. but kyrillic worked great.

Thank you for the reply ThomaEgi. It was helpful.

It seems that I was doing it wrong. Here is a sample of the code that I used:

self.uniText = u'\u3051'
  
#This code puts the standard title and instruction text on screen
self.title = OnscreenText(text= self.uniText.encode('utf-8')+"Panda3D: Tutorial - Actors",
                          style=1, fg=(0,0,0,1), font = loader.loadFont('sazanami-mincho.ttf'),
                          pos=(0.8,-0.95), scale = .07)
self.escapeEventText = self.genLabelText("ESC: End program", 0)
self.akeyEventText = self.genLabelText("[A]: Robot 1 Left Punch", 1)
self.skeyEventText = self.genLabelText("[S]: Robot 1 Right Punch", 2)
self.kkeyEventText = self.genLabelText("[K]: Robot 2 Left Punch", 3)
self.lkeyEventText = self.genLabelText("[L]: Robot 2 Right Punch", 4)

Quite possibly, the font that I was using couldn’t handle utf-8 characters or were non-existent in it. I found a font that could use them via sazanami-mincho.ttf and it shows up. What I was doing wrong was trying to put the unicode character in the function called genLabelText() which clearly did not have the font set:

def genLabelText(self, text, i):
return OnscreenText(text = text, pos = (-1.3, .95-.05*i), fg=(1,1,1,1),
align = TextNode.ALeft, scale = .05)

So, I tried the character on the title and the hiragana showed up.

Many thanks :smiley: