From 2cd9b50137b7bb40fabb209a496b1234c1a5c2b9 Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Wed, 4 Sep 2019 20:50:25 +0200 Subject: [PATCH] [Android] Don't ship Noto (#5310) Follow-up to . This greatly reduces the Android package size. See discussion in . --- Makefile | 1 + frontend/fontlist.lua | 7 +++-- frontend/ui/font.lua | 69 +++++++++++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 156c11d9b..f277ca53e 100644 --- a/Makefile +++ b/Makefile @@ -351,6 +351,7 @@ androidupdate: all cd $(INSTALL_DIR)/koreader && zip -r9 \ ../../$(ANDROID_LAUNCHER_DIR)/assets/module/koreader-$(VERSION).zip * \ --exclude=*fonts/droid* \ + --exclude=*fonts/noto* \ --exclude=*resources/fonts* \ --exclude=*resources/icons/src* \ --exclude=*share/man* \ diff --git a/frontend/fontlist.lua b/frontend/fontlist.lua index d1c6f0e08..b4abaa4df 100644 --- a/frontend/fontlist.lua +++ b/frontend/fontlist.lua @@ -74,13 +74,14 @@ local function getExternalFontDir() end local function _readList(target, dir) - -- lfs.dir non-exsitent directory will give error, weird! + -- lfs.dir non-existent directory will give an error, weird! local ok, iter, dir_obj = pcall(lfs.dir, dir) if not ok then return end for f in iter, dir_obj do - if lfs.attributes(dir.."/"..f, "mode") == "directory" and f ~= "." and f ~= ".." then + local mode = lfs.attributes(dir.."/"..f, "mode") + if mode == "directory" and f ~= "." and f ~= ".." then _readList(target, dir.."/"..f) - else + elseif mode == "file" or mode == "link" then if string.sub(f, 1, 1) ~= "." then local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "") if file_type == "ttf" or file_type == "ttc" diff --git a/frontend/ui/font.lua b/frontend/ui/font.lua index 3584b2bae..b95798e20 100644 --- a/frontend/ui/font.lua +++ b/frontend/ui/font.lua @@ -2,6 +2,8 @@ Font module. ]] +local is_android = pcall(require, "android") + local FontList = require("fontlist") local Freetype = require("ffi/freetype") local Screen = require("device").screen @@ -10,22 +12,22 @@ local logger = require("logger") local Font = { fontmap = { -- default font for menu contents - cfont = "noto/NotoSans-Regular.ttf", + cfont = "NotoSans-Regular.ttf", -- default font for title --tfont = "NimbusSanL-BoldItal.cff", - tfont = "noto/NotoSans-Bold.ttf", - smalltfont = "noto/NotoSans-Bold.ttf", - x_smalltfont = "noto/NotoSans-Bold.ttf", + tfont = "NotoSans-Bold.ttf", + smalltfont = "NotoSans-Bold.ttf", + x_smalltfont = "NotoSans-Bold.ttf", -- default font for footer - ffont = "noto/NotoSans-Regular.ttf", - smallffont = "noto/NotoSans-Regular.ttf", - largeffont = "noto/NotoSans-Regular.ttf", + ffont = "NotoSans-Regular.ttf", + smallffont = "NotoSans-Regular.ttf", + largeffont = "NotoSans-Regular.ttf", -- default font for reading position info - rifont = "noto/NotoSans-Regular.ttf", + rifont = "NotoSans-Regular.ttf", -- default font for pagination display - pgfont = "noto/NotoSans-Regular.ttf", + pgfont = "NotoSans-Regular.ttf", -- selectmenu: font for item shortcut scfont = "DroidSansMono.ttf", @@ -33,7 +35,7 @@ local Font = { -- help page: font for displaying keys hpkfont = "DroidSansMono.ttf", -- font for displaying help messages - hfont = "noto/NotoSans-Regular.ttf", + hfont = "NotoSans-Regular.ttf", -- font for displaying input content -- we have to use mono here for better distance controlling @@ -42,16 +44,16 @@ local Font = { smallinfont = "DroidSansMono.ttf", -- font for info messages - infofont = "noto/NotoSans-Regular.ttf", + infofont = "NotoSans-Regular.ttf", -- small font for info messages - smallinfofont = "noto/NotoSans-Regular.ttf", + smallinfofont = "NotoSans-Regular.ttf", -- small bold font for info messages - smallinfofontbold = "noto/NotoSans-Bold.ttf", + smallinfofontbold = "NotoSans-Bold.ttf", -- extra small font for info messages - x_smallinfofont = "noto/NotoSans-Regular.ttf", + x_smallinfofont = "NotoSans-Regular.ttf", -- extra extra small font for info messages - xx_smallinfofont = "noto/NotoSans-Regular.ttf", + xx_smallinfofont = "NotoSans-Regular.ttf", }, sizemap = { cfont = 24, @@ -75,8 +77,8 @@ local Font = { xx_smallinfofont = 18, }, fallbacks = { - [1] = "noto/NotoSans-Regular.ttf", - [2] = "noto/NotoSansCJKsc-Regular.otf", + [1] = "NotoSans-Regular.ttf", + [2] = "NotoSansCJKsc-Regular.otf", [3] = "freefont/FreeSans.ttf", [4] = "freefont/FreeSerif.ttf", }, @@ -85,6 +87,10 @@ local Font = { faces = {}, } +if is_android then + table.insert(Font.fallbacks, 3, "DroidSansFallback.ttf") -- for some ancient pre-4.4 Androids +end + --- Gets font face object. -- @string font -- @int size optional size @@ -108,13 +114,38 @@ function Font:getFace(font, size) end local builtin_font_location = FontList.fontdir.."/"..realname local ok, face = pcall(Freetype.newFace, builtin_font_location, size) + + -- We don't ship Droid and Noto on Android because they're system fonts. + -- This cuts package size in half, but 4.4 and older systems + -- might not ship Noto. + if not ok and is_android and realname:match("^Noto") then + local system_font_location = "/system/fonts" + logger.dbg("Font:", realname, "not found. Trying system location.") + + ok, face = pcall(Freetype.newFace, system_font_location.."/"..realname, size) + + -- Relevant Noto font not found on this device, fall back to Droid + if not ok then + if realname:match("Bold") then + realname = "DroidSans-Bold.ttf" + else + realname = "DroidSans.ttf" + end + end + end + -- Not all fonts are bundled on all platforms because they come with the system. -- In that case, search through all font folders for the requested font. - if not ok and font ~= realname then + if not ok then local fonts = FontList:getFontList() + local escaped_realname = realname:gsub("[-]", "%%-") + for _k, _v in ipairs(fonts) do - if _v:find(realname) then + if _v:find(escaped_realname) then + logger.dbg("Found font:", realname, "in", _v) ok, face = pcall(Freetype.newFace, _v, size) + + if ok then break end end end end