2
0
mirror of https://github.com/koreader/koreader synced 2024-11-04 12:00:25 +00:00

UI font fallbacks: use bold fonts if present for bold (#9621)

This commit is contained in:
weijiuqiao 2022-10-25 18:18:46 +08:00 committed by GitHub
parent 4d4b04359c
commit 58613d66e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -55,7 +55,7 @@ Fonts for many languages can be downloaded at:
https://fonts.google.com/noto
Only fonts named "Noto Sans xyz" or "Noto Sans xyz UI" (regular, not bold nor italic, not Serif) will be available in this menu.]])
Only fonts named "Noto Sans xyz" or "Noto Sans xyz UI" (regular, not bold nor italic, not Serif) will be available in this menu. However, bold fonts will be used if their corresponding regular fonts exist.]])
local getSubMenuItems = function()
genFallbackCandidates()

View File

@ -6,6 +6,7 @@ local FontList = require("fontlist")
local Freetype = require("ffi/freetype")
local Screen = require("device").screen
local logger = require("logger")
local util = require("util")
-- Known regular (and italic) fonts with an available bold font file
local _bold_font_variant = {}
@ -135,6 +136,38 @@ if G_reader_settings and G_reader_settings:has("font_ui_fallbacks") then
logger.dbg("updated Font.fallbacks:", Font.fallbacks)
end
-- We don't ship a bold variant for some of our fallback fonts.
-- Allow users themselves to drop a Noto Sans Bold variant of their most used fallbacks,
-- and we will use them if present.
-- Match bold font to fallback by name. We do not use FontInfo name match
-- to allow users more flexibility.
-- Because the hardcoded fallback fonts' paths are their filenames not actual paths,
-- we need to match with filenames rather than paths
local bold_candidates = {} -- key: bold font's name, value: corresponding regular font's path
for _, fallback_font_path in ipairs(Font.fallbacks) do
local _, font_name = util.splitFilePathName(fallback_font_path)
if font_name and not _bold_font_variant[fallback_font_path]
and not _bold_font_variant[font_name]
and font_name:find("-Regular") then
local bold_font_name = font_name:gsub("-Regular", "-Bold", 1, true)
bold_candidates[bold_font_name] = fallback_font_path
end
end
for _, font_path in ipairs(FontList:getFontList()) do
local _, bold_font_name = util.splitFilePathName(font_path)
local fallback_font_path = bold_candidates[bold_font_name]
if bold_font_name and fallback_font_path then
Font.bold_font_variant[fallback_font_path] = font_path
Font.regular_font_variant[font_path] = fallback_font_path
bold_candidates[bold_font_name] = nil
end
if #bold_candidates == 0 then
break
end
end
bold_candidates = nil -- luacheck: ignore
-- Helper functions with explicite names around
-- bold/regular_font_variant tables
function Font:hasBoldVariant(name)