Skip to content

Commit

Permalink
Add loading of maerial font in jukebox
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberth289346 committed Jan 7, 2024
1 parent 835bb9a commit 07df23c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 32 deletions.
10 changes: 10 additions & 0 deletions CorsixTH/Lua/dialogs/jukebox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 59 additions & 32 deletions CorsixTH/Lua/graphics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 07df23c

Please sign in to comment.