From 07df23cbe1439bc204ff0c3e54c5c9a3c2fcb103 Mon Sep 17 00:00:00 2001 From: Alberth Date: Sun, 7 Jan 2024 11:40:29 +0100 Subject: [PATCH] Add loading of maerial font in jukebox --- CorsixTH/Lua/dialogs/jukebox.lua | 10 ++++ CorsixTH/Lua/graphics.lua | 91 +++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 32 deletions(-) diff --git a/CorsixTH/Lua/dialogs/jukebox.lua b/CorsixTH/Lua/dialogs/jukebox.lua index 47a374eed8..3354c58bd4 100644 --- a/CorsixTH/Lua/dialogs/jukebox.lua +++ b/CorsixTH/Lua/dialogs/jukebox.lua @@ -40,6 +40,16 @@ function UIJukebox:UIJukebox(app) self.white_font = app.gfx:loadFont("QData", "Font01V") self.blue_font = app.gfx:loadFont("QData", "Font02V") + if TH.freetype_font then + local font_data = self.app.gfx:_loadFontData("/usr/share/fonts/truetype/material-design-icons-iconfont/MaterialIcons-Regular.ttf") + + -- Boldly copied from app.lua, lines 197..200, but it makes little sense to me. + local builtin_font = self.gfx:loadBuiltinFont() + self.materials = Graphics._constructTtfFont(font_data, builtin_font:getSheet()) + else + print("no font available") + end + -- Dialog head (current track title & exit button) self:addPanel(389, 0, 0) for x = 30, self.width - 61, 24 do diff --git a/CorsixTH/Lua/graphics.lua b/CorsixTH/Lua/graphics.lua index 440e01b00e..1f8db69e7f 100644 --- a/CorsixTH/Lua/graphics.lua +++ b/CorsixTH/Lua/graphics.lua @@ -116,41 +116,63 @@ function Graphics:Graphics(app) self.custom_graphics_folder = graphics_folder end +function Graphics._isFile(path) + local lfs = require("lfs") + return path and lfs.attributes(path, "mode") == "file" +end + +function Graphics:_loadFontData(font_path) + if not Graphics._isFile(font_path) then return nil end + + local font_handle = font_path and io.open(font_path, "rb") + if not font_handle then return nil end + + local font_data = font_handle:read("*a") + font_handle:close() + return font_data +end + --! Tries to load the font file given in the config file as unicode_font. --! If it is not found it tries to find one in the operating system. function Graphics:loadFontFile() - local lfs = require("lfs") - local function check(path) return path and lfs.attributes(path, "mode") == "file" end - -- Load the Unicode font, if there is one specified. + local loaded_font_path + + -- Try the Unicode font, if there is one specified. local config_path = self.app.config.unicode_font - -- Try a font that commonly comes with the operating system. - local os_path, font_file - local windir = os.getenv("WINDIR") - if windir and windir ~= "" then - os_path = windir .. pathsep .. "Fonts" .. pathsep .. "ARIALUNI.TTF" - elseif self.app.os == "macos" then - os_path = "/Library/Fonts/Arial Unicode.ttf" + self.ttf_font_data = self:_loadFontData(config_path) + if self.ttf_font_data then + loaded_font_path = config_path + else - os_path = "/usr/share/fonts/truetype/arphic/uming.ttc" - end - if check(config_path) then font_file = config_path - elseif check(os_path) then - font_file = os_path - print("Configured unicode font not found, using " .. font_file .. " instead.") - print("This will be written to the config file.") - elseif config_path ~= nil then - print("Configured unicode font not found, no fallback available.") - return - end - local font = font_file and io.open(font_file, "rb") - if font then - self.ttf_font_data = font:read("*a") - font:close() - if self.ttf_font_data and self.app.config.unicode_font ~= font_file then - self.app.config.unicode_font = font_file - self.app:saveConfig() + -- Try a font that commonly comes with the operating system. + local os_path + local windir = os.getenv("WINDIR") + if windir and windir ~= "" then + os_path = windir .. pathsep .. "Fonts" .. pathsep .. "ARIALUNI.TTF" + elseif self.app.os == "macos" then + os_path = "/Library/Fonts/Arial Unicode.ttf" + else + os_path = "/usr/share/fonts/truetype/arphic/uming.ttc" + end + self.ttf_font_data = self:_loadFontData(os_path) + + if self.ttf_font_data then + loaded_font_path = os_path + + else + -- No font file found. + if config_path ~= nil then + print("Configured unicode font not found, no fallback available.") + end + return end end + + -- Adjust unicode font in the configuration if needed. + if self.ttf_font_data and self.app.config.unicode_font ~= loaded_font_file then + self.app.config.unicode_font = loaded_font_file + self.app:saveConfig() + end end function Graphics:loadMainCursor(id) @@ -407,10 +429,7 @@ function Graphics:loadLanguageFont(name, sprite_table, ...) local cache = self.cache.language_fonts[name] font = cache and cache[sprite_table] if not font then - font = TH.freetype_font() - -- TODO: Choose face based on "name" rather than always using same face. - font:setFace(self.ttf_font_data) - font:setSheet(sprite_table) + font = Graphics.lf:_constructTtfFont(self.ttf_font_data, sprite_table) self.reload_functions_last[font] = font_reloader if not cache then @@ -424,6 +443,14 @@ function Graphics:loadLanguageFont(name, sprite_table, ...) return font end +function Graphics._constructTtfFont(font_data, sprite_table) + local font = TH.freetype_font() + -- TODO: Choose face based on "name" rather than always using same face. + font:setFace(self.ttf_font_data) + font:setSheet(sprite_table) + return font +end + function Graphics:loadFont(sprite_table, x_sep, y_sep, ...) -- Allow (multiple) arguments for loading a sprite table in place of the -- sprite_table argument.