From 58613d66e7409a7252629aa39cc50f926cb611c6 Mon Sep 17 00:00:00 2001 From: weijiuqiao <59040746+weijiuqiao@users.noreply.github.com> Date: Tue, 25 Oct 2022 18:18:46 +0800 Subject: [PATCH] UI font fallbacks: use bold fonts if present for bold (#9621) --- frontend/ui/elements/font_ui_fallbacks.lua | 2 +- frontend/ui/font.lua | 33 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/frontend/ui/elements/font_ui_fallbacks.lua b/frontend/ui/elements/font_ui_fallbacks.lua index 0367cadf3..305258fe2 100644 --- a/frontend/ui/elements/font_ui_fallbacks.lua +++ b/frontend/ui/elements/font_ui_fallbacks.lua @@ -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() diff --git a/frontend/ui/font.lua b/frontend/ui/font.lua index 67514cf25..4e8c97a67 100644 --- a/frontend/ui/font.lua +++ b/frontend/ui/font.lua @@ -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)