This thread is spun out of the thread in which I asked for a recommendation for a TOML parser, as it seemed sufficiently separate from that request.
In short, TextProperties-tags appear to work nicely!
… As long as they weren’t derived from a TOML-parsing. In that case, they don’t seem to work at all!
Specifically, it seems that TOML doesn’t like the escape sequences used by TextProperties: “\1” and “\2”–or at least, I haven’t yet found a way to successfully include them. And if the backslashes of those escape sequences are themselves escaped, they end up as just the text “\1” and “\2”, which TextProperties doesn’t recognise.
See the following test-program, which demonstrates the issue on my machine:
from direct.showbase.ShowBase import ShowBase
from panda3d.core import TextProperties, TextPropertiesManager
from direct.gui.OnscreenText import OnscreenText
import tomli
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
textPropertySlanted = TextProperties()
textPropertySlanted.setSlant(0.3)
textPropManager = TextPropertiesManager.getGlobalPtr()
textPropManager.setProperties("italic", textPropertySlanted)
self.label = OnscreenText(text = "Non-italic",
pos = (0, 0),
scale = 0.2)
self.accept("1", self.testBasicString)
self.accept("2", self.testTomlString)
def testBasicString(self):
self.label.setText("\1italic\1Italics!\2")
def testTomlString(self):
tomlString = "cat = '''\\1italic\\1TOML Italics!\\2'''"
#tomlString = "cat = '''\1italic\1TOML Italics!\2'''" # <-- Breaks
result = tomli.loads(tomlString)
self.label.setText(result["cat"])
game = Game()
game.run()
In short, it sets up an OnscreenText with some basic text, as well as a TextProperties that produces an italic effect.
Then:
- When the user presses “1”, it sets the text to be a simple string that contains the relevant TextProperties markup.
- When the user presses “2”, it sets the text to be a string derived from TOML, and that likewise contains the relevant markup–at least in its raw form.
On my end, at least, the former works, and the latter doesn’t.
Does anyone see a way around this?
And is it worth considering a different set of tags for TextProperties. After all, even if the conflict with TOML is resolved, the use of backslashes as escape-characters may result in another, similar clash with some other module in the future…