From 85cb570e8882b81bc919f4edf40b63938b66615b Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Fri, 24 Feb 2012 00:16:20 +0800 Subject: [PATCH] fix: unify font changes to all menus --- filechooser.lua | 46 +++++++++++++++++++++++++++++--------------- filesearcher.lua | 39 +++++++++++++++++++++++++++++-------- fontchooser.lua | 50 ++++++++++++++++++++++++++++++++++++++---------- tocmenu.lua | 43 ++++++++++++++++++++++++++++++----------- 4 files changed, 134 insertions(+), 44 deletions(-) diff --git a/filechooser.lua b/filechooser.lua index afac047be..dc0f0e833 100644 --- a/filechooser.lua +++ b/filechooser.lua @@ -7,13 +7,20 @@ require "inputbox" FileChooser = { -- Class vars: - - -- font for displaying file/dir names - face = freetype.newBuiltinFace("sans", 25), - fhash = "s25", + -- font for displaying toc item names + fsize = 25, + face = nil, + fhash = nil, + --face = freetype.newBuiltinFace("sans", 25), + --fhash = "s25", + -- font for paging display - sface = freetype.newBuiltinFace("sans", 16), - sfhash = "s16", + ffsize = 16, + fface = nil, + ffhash = nil, + --sface = freetype.newBuiltinFace("sans", 16), + --sfhash = "s16", + -- spacing between lines spacing = 40, @@ -54,11 +61,23 @@ function FileChooser:setPath(newPath) return true end +function FileChooser:updateFont() + if self.fhash ~= FontChooser.cfont..self.fsize then + self.face = freetype.newBuiltinFace(FontChooser.cfont, self.fsize) + self.fhash = FontChooser.cfont..self.fsize + end + + if self.ffhash ~= FontChooser.ffont..self.ffsize then + self.fface = freetype.newBuiltinFace(FontChooser.ffont, self.ffsize) + self.ffhash = FontChooser.ffont..self.ffsize + end +end function FileChooser:choose(ypos, height) local perpage = math.floor(height / self.spacing) - 1 local pagedirty = true local markerdirty = false + self:updateFont() local prevItem = function () if self.current == 1 then @@ -103,7 +122,7 @@ function FileChooser:choose(ypos, height) renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.files[i-#self.dirs], true) end end - renderUtf8Text(fb.bb, 39, ypos + self.spacing * perpage + 32, self.sface, self.sfhash, + renderUtf8Text(fb.bb, 39, ypos + self.spacing * perpage + 32, self.fface, self.ffhash, "Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true) markerdirty = true end @@ -134,12 +153,9 @@ function FileChooser:choose(ypos, height) elseif ev.code == KEY_FW_DOWN then nextItem() elseif ev.code == KEY_F then -- invoke fontchooser menu - FontChooser:init() - newfont = FontChooser:choose(0, height) - if newfont ~= nil then - self.face = freetype.newBuiltinFace(newfont, 25) - clearglyphcache() - end + --FontChooser:init() + FontChooser:choose(0, height) + self:updateFont() pagedirty = true elseif ev.code == KEY_S then -- invoke search input keywords = InputBox:input(height-100, 100, "Search:") @@ -150,8 +166,8 @@ function FileChooser:choose(ypos, height) || to test search feature in EMU mode ---------------------------------------------------------------- --]] - --FileSearcher:init("/home/dave/documents/kindle/backup/documents") - FileSearcher:init() + FileSearcher:init("/home/dave/documents/kindle/backup/documents") + --FileSearcher:init() file = FileSearcher:choose(ypos, height, keywords) if file then return file diff --git a/filesearcher.lua b/filesearcher.lua index 367486cd1..316c3e33f 100644 --- a/filesearcher.lua +++ b/filesearcher.lua @@ -1,17 +1,22 @@ require "rendertext" require "keys" require "graphics" +require "fontchooser" FileSearcher = { - -- font for displaying file/dir names - face = freetype.newBuiltinFace("sans", 25), - fhash = "s25", + -- font for displaying toc item names + fsize = 25, + face = nil, + fhash = nil, -- font for page title - tface = freetype.newBuiltinFace("Helvetica-BoldOblique", 32), - tfhash = "hbo32", + tfsize = 30, + tface = nil, + tfhash = nil, -- font for paging display - sface = freetype.newBuiltinFace("sans", 16), - sfhash = "s16", + ffsize = 16, + fface = nil, + ffhash = nil, + -- title height title_H = 45, -- spacing between lines @@ -65,6 +70,23 @@ function FileSearcher:setPath(newPath) return true end +function FileSearcher:updateFont() + if self.fhash ~= FontChooser.cfont..self.fsize then + self.face = freetype.newBuiltinFace(FontChooser.cfont, self.fsize) + self.fhash = FontChooser.cfont..self.fsize + end + + if self.tfhash ~= FontChooser.tfont..self.tfsize then + self.tface = freetype.newBuiltinFace(FontChooser.tfont, self.tfsize) + self.tfhash = FontChooser.tfont..self.tfsize + end + + if self.ffhash ~= FontChooser.ffont..self.ffsize then + self.fface = freetype.newBuiltinFace(FontChooser.ffont, self.ffsize) + self.ffhash = FontChooser.ffont..self.ffsize + end +end + function FileSearcher:setSearchResult(keywords) self.result = {} if keywords == " " then -- one space to show all files @@ -94,6 +116,7 @@ function FileSearcher:choose(ypos, height, keywords) local perpage = math.floor(height / self.spacing) - 2 local pagedirty = true local markerdirty = false + self:updateFont() local prevItem = function () if self.current == 1 then @@ -163,7 +186,7 @@ function FileSearcher:choose(ypos, height, keywords) y = ypos + self.title_H + (self.spacing * perpage) + self.foot_H x = (fb.bb:getWidth() / 2) - 50 all_page = (math.floor(self.items / perpage)+1) - renderUtf8Text(fb.bb, x, y, self.sface, self.sfhash, + renderUtf8Text(fb.bb, x, y, self.fface, self.ffhash, "Page "..self.page.." of "..all_page, true) end diff --git a/fontchooser.lua b/fontchooser.lua index a1acf08cc..0cf6b4292 100644 --- a/fontchooser.lua +++ b/fontchooser.lua @@ -3,15 +3,27 @@ require "keys" require "graphics" FontChooser = { + -- font name for content + cfont = "sans", -- font for displaying file/dir names - face = freetype.newBuiltinFace("sans", 25), - fhash = "s25", + fsize = 25, + face = nil, + fhash = nil, + + -- font name for title + tfont = "Helvetica-BoldOblique", -- font for page title - tface = freetype.newBuiltinFace("Helvetica-BoldOblique", 30), - tfhash = "hbo30", - -- font for paging display - sface = freetype.newBuiltinFace("sans", 16), - sfhash = "s16", + tfsize = 30, + tface = nil, + tfhash = nil, + + -- font name for footer + ffont = "sans", + -- font for page footer display + ffsize = 16, + fface = nil, + ffhash = nil, + -- title height title_H = 45, -- spacing between lines @@ -35,11 +47,28 @@ function FontChooser:init() self.items = #self.fonts end +function FontChooser:updateFont() + if self.fhash ~= FontChooser.cfont..self.fsize then + self.face = freetype.newBuiltinFace(FontChooser.cfont, self.fsize) + self.fhash = FontChooser.cfont..self.fsize + end + + if self.tfhash ~= FontChooser.tfont..self.tfsize then + self.tface = freetype.newBuiltinFace(FontChooser.tfont, self.tfsize) + self.tfhash = FontChooser.tfont..self.tfsize + end + + if self.ffhash ~= FontChooser.ffont..self.ffsize then + self.fface = freetype.newBuiltinFace(FontChooser.ffont, self.ffsize) + self.ffhash = FontChooser.ffont..self.ffsize + end +end function FontChooser:choose(ypos, height) local perpage = math.floor(height / self.spacing) - 2 local pagedirty = true local markerdirty = false + self:updateFont() local prevItem = function () if self.current == 1 then @@ -95,7 +124,7 @@ function FontChooser:choose(ypos, height) -- draw footer y = ypos + self.title_H + (self.spacing * perpage) + self.foot_H x = (fb.bb:getWidth() / 2) - 50 - renderUtf8Text(fb.bb, x, y, self.sface, self.sfhash, + renderUtf8Text(fb.bb, x, y, self.fface, self.ffhash, "Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true) markerdirty = true end @@ -150,8 +179,9 @@ function FontChooser:choose(ypos, height) markerdirty = true end elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then - local newface = self.fonts[perpage*(self.page-1)+self.current] - return newface + self.cfont = self.fonts[perpage*(self.page-1)+self.current] + clearglyphcache() + return nil elseif ev.code == KEY_BACK then return nil end diff --git a/tocmenu.lua b/tocmenu.lua index 73e62ed1a..ee2d70d51 100644 --- a/tocmenu.lua +++ b/tocmenu.lua @@ -1,17 +1,22 @@ require "rendertext" require "keys" require "graphics" +require "fontchooser" TOCMenu = { - -- font for displaying file/dir names - face = freetype.newBuiltinFace("cjk", 22), - fhash = "s22", + -- font for displaying toc item names + fsize = 22, + face = nil, + fhash = nil, -- font for page title - tface = freetype.newBuiltinFace("Helvetica-BoldOblique", 25), - tfhash = "hbo25", + tfsize = 25, + tface = nil, + tfhash = nil, -- font for paging display - sface = freetype.newBuiltinFace("sans", 16), - sfhash = "s16", + ffsize = 16, + fface = nil, + ffhash = nil, + -- title height title_H = 40, -- spacing between lines @@ -28,8 +33,6 @@ TOCMenu = { } function TOCMenu:new(toc) - --@TODO set font here in the future 21.02 2012 - --clearglyphcache() instance = self instance.toc = toc instance.items = #toc @@ -45,10 +48,28 @@ function TOCMenu:dump() end end +function TOCMenu:updateFont() + if self.fhash ~= FontChooser.cfont..self.fsize then + self.face = freetype.newBuiltinFace(FontChooser.cfont, self.fsize) + self.fhash = FontChooser.cfont..self.fsize + end + + if self.tfhash ~= FontChooser.tfont..self.tfsize then + self.tface = freetype.newBuiltinFace(FontChooser.tfont, self.tfsize) + self.tfhash = FontChooser.tfont..self.tfsize + end + + if self.ffhash ~= FontChooser.ffont..self.ffsize then + self.fface = freetype.newBuiltinFace(FontChooser.ffont, self.ffsize) + self.ffhash = FontChooser.ffont..self.ffsize + end +end + function TOCMenu:choose(ypos, height) local perpage = math.floor(height / self.spacing) - 2 local pagedirty = true local markerdirty = false + self:updateFont() local prevItem = function () if self.current == 1 then @@ -99,7 +120,7 @@ function TOCMenu:choose(ypos, height) "Oops... Bad news for you:", true) y = y + self.spacing renderUtf8Text(fb.bb, 30, y, self.face, self.fhash, - "This document does not have a Table of Conent.", true) + "This document doesn't have a TOC.", true) markerdirty = false else local c @@ -116,7 +137,7 @@ function TOCMenu:choose(ypos, height) -- draw footer y = ypos + self.title_H + (self.spacing * perpage) + self.foot_H + 5 x = (fb.bb:getWidth() / 2) - 50 - renderUtf8Text(fb.bb, x, y, self.sface, self.sfhash, + renderUtf8Text(fb.bb, x, y, self.fface, self.ffhash, "Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true) end