From 1f14a9e80dd4cef40a1e1976196b38450a1e096f Mon Sep 17 00:00:00 2001 From: frankyifei Date: Thu, 22 Oct 2015 19:32:21 +1030 Subject: [PATCH 1/3] try to exclude those broken system fonts these fonts can not be used by freetype and will cause hang up --- frontend/ui/font.lua | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/frontend/ui/font.lua b/frontend/ui/font.lua index b1b9d96f4..d74f24b84 100644 --- a/frontend/ui/font.lua +++ b/frontend/ui/font.lua @@ -82,6 +82,26 @@ function Font:getFace(font, size) return face_obj end +function checkfont(f) + local exclusive_system_font = { + --these kindle system fonts can not be used by freetype and will give error + "HYGothicBold.ttf", + "HYGothicMedium.ttf", + "HYMyeongJoBold.ttf", + "HYMyeongJoMedium.ttf", + "MYingHeiTBold.ttf", + "MYingHeiTMedium.ttf", + "SongTBold.ttf", + "SongTMedium.ttf" + } + for _,value in ipairs(exclusive_system_font) do + if value == f then + return true + end + end + return false +end + function Font:_readList(target, dir) -- lfs.dir non-exsitent directory will give error, weird! local ok, iter, dir_obj = pcall(lfs.dir, dir) @@ -93,7 +113,9 @@ function Font:_readList(target, dir) local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "") if file_type == "ttf" or file_type == "ttc" or file_type == "cff" or file_type == "otf" then - table.insert(target, dir.."/"..f) + if checkfont(f) ~=true then + table.insert(target, dir.."/"..f) + end end end end From 86d6d46770550185d10251e99fb2c4f69bdbfb70 Mon Sep 17 00:00:00 2001 From: frankyifei Date: Thu, 22 Oct 2015 23:49:34 +1030 Subject: [PATCH 2/3] add isKindle check --- frontend/ui/font.lua | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/frontend/ui/font.lua b/frontend/ui/font.lua index d74f24b84..4553e6d3b 100644 --- a/frontend/ui/font.lua +++ b/frontend/ui/font.lua @@ -83,23 +83,25 @@ function Font:getFace(font, size) end function checkfont(f) - local exclusive_system_font = { - --these kindle system fonts can not be used by freetype and will give error - "HYGothicBold.ttf", - "HYGothicMedium.ttf", - "HYMyeongJoBold.ttf", - "HYMyeongJoMedium.ttf", - "MYingHeiTBold.ttf", - "MYingHeiTMedium.ttf", - "SongTBold.ttf", - "SongTMedium.ttf" - } - for _,value in ipairs(exclusive_system_font) do - if value == f then - return true + if Device:isKindle() then + local exclusive_system_font = { + --these kindle system fonts can not be used by freetype and will give error + "HYGothicBold.ttf", + "HYGothicMedium.ttf", + "HYMyeongJoBold.ttf", + "HYMyeongJoMedium.ttf", + "MYingHeiTBold.ttf", + "MYingHeiTMedium.ttf", + "SongTBold.ttf", + "SongTMedium.ttf" + } + for _,value in ipairs(exclusive_system_font) do + if value == f then + return true + end end + else return false end - return false end function Font:_readList(target, dir) From 55ba1450d4351124b720780c3a527e3caa2a6c65 Mon Sep 17 00:00:00 2001 From: chrox Date: Thu, 22 Oct 2015 23:15:41 +0800 Subject: [PATCH 3/3] code refactoring: use hash table index instead of loop --- frontend/ui/font.lua | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/frontend/ui/font.lua b/frontend/ui/font.lua index 4553e6d3b..d42222108 100644 --- a/frontend/ui/font.lua +++ b/frontend/ui/font.lua @@ -47,7 +47,6 @@ local Font = { faces = {}, } - function Font:getFace(font, size) -- default to content font if not font then font = self.cfont end @@ -82,25 +81,23 @@ function Font:getFace(font, size) return face_obj end -function checkfont(f) +--[[ + These fonts from Kindle system cannot be loaded by Freetype. +--]] +local kindle_fonts_blacklist = { + ["HYGothicBold.ttf"] = true, + ["HYGothicMedium.ttf"] = true, + ["HYMyeongJoBold.ttf"] = true, + ["HYMyeongJoMedium.ttf"] = true, + ["MYingHeiTBold.ttf"] = true, + ["MYingHeiTMedium.ttf"] = true, + ["SongTBold.ttf"] = true, + ["SongTMedium.ttf"] = true, +} + +local function isInFontsBlacklist(f) if Device:isKindle() then - local exclusive_system_font = { - --these kindle system fonts can not be used by freetype and will give error - "HYGothicBold.ttf", - "HYGothicMedium.ttf", - "HYMyeongJoBold.ttf", - "HYMyeongJoMedium.ttf", - "MYingHeiTBold.ttf", - "MYingHeiTMedium.ttf", - "SongTBold.ttf", - "SongTMedium.ttf" - } - for _,value in ipairs(exclusive_system_font) do - if value == f then - return true - end - end - else return false + return kindle_fonts_blacklist[f] end end @@ -115,7 +112,7 @@ function Font:_readList(target, dir) local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "") if file_type == "ttf" or file_type == "ttc" or file_type == "cff" or file_type == "otf" then - if checkfont(f) ~=true then + if not isInFontsBlacklist(f) then table.insert(target, dir.."/"..f) end end