Merge pull request #142 from mocchapi/theme-font-additions

Allow themes to load custom font files
This commit is contained in:
Dpeta 2023-06-28 02:41:38 +02:00 committed by GitHub
commit 0ea2d450ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 6 deletions

View file

@ -1112,7 +1112,13 @@ def themeChecker(theme):
"main/icon",
"main/windowtitle",
"main/style",
"main/fonts",
"main/background-image",
"main/sounds/namealarm",
"main/sounds/alertsound",
"main/sounds/memosound",
"main/sounds/ceasesound",
"main/sounds/honk",
"main/menubar/style",
"main/menu/menuitem",
"main/menu/style",

View file

@ -1540,6 +1540,8 @@ class PesterWindow(MovingWindow):
# Load font
QtGui.QFontDatabase.addApplicationFont(
os.path.join("fonts", "alternian", "AllisDaedric-VYWz.otf")
# ~lisanne "alternian lol" TODO: make the parsetools 'alternianTagBegin' lex part use the theme's "main/alternian-font-family" value instead of just assuming "AllisDaedric"
# This way themes can change what the alternian font looks like
)
self.pcUpdate[str, str].connect(self.updateMsg)
@ -1991,6 +1993,26 @@ class PesterWindow(MovingWindow):
self.choosetheme.exec()
def initTheme(self, theme):
# First doing the fonts because any style may depend on it
QtGui.QFontDatabase.removeAllApplicationFonts() # GOODBYE previous fonts
QtGui.QFontDatabase.addApplicationFont(
os.path.join("fonts", "alternian", "AllisDaedric-VYWz.otf")
) # haha oops we still need that one!!! for now!! (check `~lisanne "alternian lol" TODO` up above)
for font_path in theme["main/fonts"]:
# ~lisanne : loads fonts from the `main/fonts` key in a theme
# Note that this wont load fonts from inherited themes
# that seems fine imo, esp since u could still load them through `$path/../inheritedtheme/somefont.ttf`
PchumLog.debug("Loading font " + font_path)
fontID = QtGui.QFontDatabase.addApplicationFont(font_path)
if fontID == -1:
PchumLog.error("Failed loading font: " + font_path)
# TODO? Maybe make this spawn an error popup
else:
PchumLog.debug(
f"Font families: {(QtGui.QFontDatabase.applicationFontFamilies(fontID))} (id: {fontID})"
)
self.resize(*theme["main/size"])
self.setWindowIcon(PesterIcon(theme["main/icon"]))
self.setWindowTitle(theme["main/windowtitle"])

View file

@ -2,6 +2,7 @@
{"style": "background-repeat: no-repeat;",
"background-image": "$path/pcbg.png",
"size": [232, 380],
"fonts": [],
"icon": "$path/trayicon.png",
"newmsgicon": "$path/trayicon2.png",
"windowtitle": "PESTERCHUM 6.0",

View file

@ -984,12 +984,23 @@ class pesterTheme(dict):
raise e
return v
def pathHook(self, d):
for k, v in d.items():
if isinstance(v, str):
s = Template(v)
d[k] = s.safe_substitute(path=self.path)
return d
def pathHook(self, dict):
# This converts strings containing $path into the proper paths
# Honestly ive never even seen this Template stuff before. very funky!
for key, value in dict.items():
if isinstance(value, str):
templ = Template(value)
dict[key] = templ.safe_substitute(path=self.path)
elif isinstance(value, list):
# ~lisanne : for dealing with 'main/fonts' which is an array which contains filepaths with $
# probably good to have for future additions
for idx, item in enumerate(value):
item = value[idx]
if isinstance(item, str):
# not very DRY of me >:3c
templ = Template(item)
value[idx] = templ.safe_substitute(path=self.path)
return dict
def get(self, key, default):
keys = key.split("/")