From 6c9ec8c896acd8048dc1e07bbcfbb4cb99fdcec3 Mon Sep 17 00:00:00 2001 From: traycold Date: Sun, 15 Apr 2012 16:07:39 +0200 Subject: [PATCH] refactoring for display help page --- commands.lua | 4 +-- ft.c | 2 ++ helppage.lua | 31 ++++++++++++++++----- keys.lua | 11 +++++--- rendertext.lua | 20 ++++++++------ unireader.lua | 73 +++++++++++++++++++------------------------------- 6 files changed, 74 insertions(+), 67 deletions(-) diff --git a/commands.lua b/commands.lua index 43dfca87d..09611a77c 100644 --- a/commands.lua +++ b/commands.lua @@ -21,7 +21,7 @@ function Keydef:new(keycode,modifier,descr) return obj end function Keydef:display() - return ((self.modifier and self.modifier.."+") or "")..(self.descr or "") + return (self.modifier or "")..(self.descr or "") end function Keydef:tostring() return ((self.modifier and self.modifier.."+") or "").."["..(self.keycode or "").."]"..(self.descr or "") @@ -74,7 +74,7 @@ function Commands:_addImpl(keydef,help,func,keygroup) if keydef.modifier==MOD_ANY then self:addGroup(keygroup or keydef.descr,{Keydef:new(keydef.keycode,nil), Keydef:new(keydef.keycode,MOD_SHIFT), Keydef:new(keydef.keycode,MOD_ALT)},help,func) elseif keydef.modifier==MOD_SHIFT_OR_ALT then - self:addGroup(keygroup or (MOD_SHIFT.."|"..MOD_ALT.."+"..(keydef.descr or "")),{Keydef:new(keydef.keycode,MOD_SHIFT), Keydef:new(keydef.keycode,MOD_ALT)},help,func) + self:addGroup(keygroup or (MOD_SHIFT..MOD_ALT..(keydef.descr or "")),{Keydef:new(keydef.keycode,MOD_SHIFT), Keydef:new(keydef.keycode,MOD_ALT)},help,func) else local command = self.map[keydef] if command == nil then diff --git a/ft.c b/ft.c index 38b16542e..a073fd415 100644 --- a/ft.c +++ b/ft.c @@ -139,6 +139,8 @@ static int renderGlyph(lua_State *L) { lua_setfield(L, -2, "l"); lua_pushinteger(L, (*face)->glyph->bitmap_top); lua_setfield(L, -2, "t"); + lua_pushinteger(L, (*face)->glyph->metrics.horiAdvance >> 6); + lua_setfield(L, -2, "r"); lua_pushinteger(L, (*face)->glyph->advance.x >> 6); lua_setfield(L, -2, "ax"); lua_pushinteger(L, (*face)->glyph->advance.y >> 6); diff --git a/helppage.lua b/helppage.lua index 8e28034f4..12fba6ff5 100644 --- a/helppage.lua +++ b/helppage.lua @@ -15,10 +15,10 @@ HelpPage = { -- Other Class vars: --- font for displaying keys -HelpPage.mFace, HelpPage.mHash = Font:getFaceAndHash(20, "mono") -- font for displaying help messages HelpPage.sFace, HelpPage.sHash = Font:getFaceAndHash(20, "sans") +-- font for displaying keys +HelpPage.mFace, HelpPage.mHash = Font:getFaceAndHash(20, "sans") -- font for paging display HelpPage.fFace, HelpPage.fHash = Font:getFaceAndHash(15, "sans") @@ -39,8 +39,8 @@ function HelpPage:show(ypos,height,commands) local mFaceHeight, mFaceAscender = self.mFace:getHeightAndAscender(); local fFaceHeight, fFaceAscender = self.fFace:getHeightAndAscender(); - print(mFaceHeight.."-"..mFaceAscender) - print(fFaceHeight.."-"..fFaceAscender) + --print(mFaceHeight.."-"..mFaceAscender) + --print(fFaceHeight.."-"..fFaceAscender) mFaceHeight = math.ceil(mFaceHeight) mFaceAscender = math.ceil(mFaceAscender) fFaceHeight = math.ceil(fFaceHeight) @@ -52,14 +52,31 @@ function HelpPage:show(ypos,height,commands) while true do if pagedirty then - fb.bb:paintRect(0, 0, fb.bb:getWidth(), height, 0) + fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0) local c local max_x = 0 for c = 1, perpage do + local x = 5 local i = (self.page - 1) * perpage + c if i <= self.items then - local pen_x = renderUtf8Text(fb.bb, 5, ypos + spacing*c, self.mFace, self.mHash, self.commands[i].shortcut, true) - max_x = math.max(max_x, pen_x) + local key = self.commands[i].shortcut + for _k,aMod in pairs(MOD_TABLE) do + local modStart, modEnd = key:find(aMod.v) + print("key:"..key.." v:"..aMod.v.." d:"..aMod.d.." modstart:"..(modStart or "nil")) + if(modStart ~= nil) then + key = key:sub(1,modStart-1)..key:sub(modEnd+1) + local box = sizeUtf8Text( x, fb.bb:getWidth(), self.mFace, self.mHash, aMod.d, true) + fb.bb:paintRect(x, ypos + spacing*c - box.y_top, box.x, box.y_top + box.y_bottom, 4); + local pen_x = renderUtf8Text(fb.bb, x, ypos + spacing*c, self.mFace, self.mHash, aMod.d.." + ", true) + x = x + pen_x + max_x = math.max(max_x, pen_x) + end + end + local box = sizeUtf8Text( x, fb.bb:getWidth(), self.mFace, self.mHash, key , true) + fb.bb:paintRect(x, ypos + spacing*c - box.y_top, box.x, box.y_top + box.y_bottom, 4); + local pen_x = renderUtf8Text(fb.bb, x, ypos + spacing*c, self.mFace, self.mHash, key, true) + x = x + pen_x + max_x = math.max(max_x, x) end end for c = 1, perpage do diff --git a/keys.lua b/keys.lua index 43e683120..e07af2ada 100644 --- a/keys.lua +++ b/keys.lua @@ -96,10 +96,13 @@ EVENT_VALUE_KEY_REPEAT = 2 EVENT_VALUE_KEY_RELEASE = 0 -- modifiers -MOD_SHIFT = "Shift" -MOD_ALT = "Alt" -MOD_SHIFT_OR_ALT = "ShiftAlt" -MOD_ANY = "Any" +MOD_SHIFT = "_Shift_" +MOD_ALT = "_Alt_" +MOD_SHIFT_OR_ALT = "_ShiftAlt_" +MOD_ANY = "_Any_" +MOD_SHIFT_DISPLAY = "Shift" +MOD_ALT_DISPLAY = "Alt" +MOD_TABLE = {MOD_SHIFT={v=MOD_SHIFT,d=MOD_SHIFT_DISPLAY},MOD_ALT={v=MOD_ALT,d=MOD_ALT_DISPLAY}} function getKeyModifier() return Keys.altmode and MOD_ALT or Keys.shiftmode and MOD_SHIFT diff --git a/rendertext.lua b/rendertext.lua index 4d4f46a71..337d86285 100644 --- a/rendertext.lua +++ b/rendertext.lua @@ -43,7 +43,7 @@ function clearGlyphCache() glyphcache = {} end -function sizeUtf8Text(face, facehash, text, kerning) +function sizeUtf8Text(x, width, face, facehash, text, kerning) if text == nil then print("# sizeUtf8Text called without text"); return @@ -51,28 +51,32 @@ function sizeUtf8Text(face, facehash, text, kerning) -- may still need more adaptive pen placement when kerning, -- see: http://freetype.org/freetype2/docs/glyphs/glyphs-4.html local pen_x = 0 + local pen_y_top = 0 + local pen_y_bottom = 0 local prevcharcode = 0 + --print("----------------- text:"..text) for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do - if pen_x < buffer:getWidth() then + if pen_x < (width - x) then local charcode = util.utf8charcode(uchar) - local glyph = getglyph(face, facehash, charcode) + local glyph = getGlyph(face, facehash, charcode) if kerning and prevcharcode then local kern = face:getKerning(prevcharcode, charcode) pen_x = pen_x + kern - print("prev:"..string.char(prevcharcode+10).." curr:"..string.char(charcode).." kern:"..kern) - buffer:addblitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight()) + print("prev:"..string.char(prevcharcode).." curr:"..string.char(charcode).." kern:"..kern) else print("curr:"..string.char(charcode)) - buffer:blitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight()) end pen_x = pen_x + glyph.ax + pen_y_top = math.max(pen_y_top, glyph.t) + pen_y_bottom = math.max(pen_y_bottom, glyph.bb:getHeight() - glyph.t) + --print("ax:"..glyph.ax.." t:"..glyph.t.." r:"..glyph.r.." h:"..glyph.bb:getHeight().." w:"..glyph.bb:getWidth().." yt:"..pen_y_top.." yb:"..pen_y_bottom) prevcharcode = charcode end end - return pen_x + return { x = pen_x, y_top = pen_y_top, y_bottom = pen_y_bottom} end -function renderUtf8Text(buffer, x, y, face, facehash, text, kerning) +function renderUtf8Text(buffer, x, y, face, facehash, text, kerning, backgroundColor) if text == nil then print("# renderUtf8Text called without text"); return diff --git a/unireader.lua b/unireader.lua index bf956be15..bb067a723 100644 --- a/unireader.lua +++ b/unireader.lua @@ -815,63 +815,50 @@ end -- command definitions function UniReader:addAllCommands() self.commands = Commands:new() - self.commands:add(KEY_PGFWD,nil,">", - "next page", - function(unireader) - unireader:goto(unireader:nextView()) - end) - self.commands:add(KEY_PGBCK,nil,"<", - "previous page", - function(unireader) - unireader:goto(unireader:prevView()) - end) - self.commands:add(KEY_PGFWD,MOD_ALT,">", - "zoom in 10%", - function(unireader) - unireader:setGlobalZoom(unireader.globalzoom+unireader.globalzoom_orig*0.1) - end) - self.commands:add(KEY_PGBCK,MOD_ALT,"<", - "zoom out 10%", - function(unireader) - unireader:setGlobalZoom(unireader.globalzoom-unireader.globalzoom_orig*0.1) + + self.commands:addGroup("< >",{Keydef:new(KEY_PGBCK,nil),Keydef:new(KEY_PGFWD,nil)}, + "previous/next page", + function(unireader,keydef) + unireader:goto(keydef.keycode==KEY_PGBCK and unireader:prevView() or unireader:nextView()) end) - self.commands:add(KEY_PGFWD,MOD_SHIFT,">", - "zoom in 20%", - function(unireader) - unireader:setGlobalZoom(unireader.globalzoom+unireader.globalzoom_orig*0.2) + self.commands:addGroup(MOD_ALT.."< >",{Keydef:new(KEY_PGBCK,MOD_ALT),Keydef:new(KEY_PGFWD,MOD_ALT)}, + "zoom out/in 10%", + function(unireader,keydef) + unireader:setGlobalZoom(unireader.globalzoom + (keydef.keycode==KEY_PGBCK and -1 or 1)*unireader.globalzoom_orig*0.1) end) - self.commands:add(KEY_PGBCK,MOD_SHIFT,"<", - "zoom out 20%", - function(unireader) - unireader:setGlobalZoom(unireader.globalzoom-unireader.globalzoom_orig*0.2) + self.commands:addGroup(MOD_SHIFT.."< >",{Keydef:new(KEY_PGBCK,MOD_SHIFT),Keydef:new(KEY_PGFWD,MOD_ALTSHIFT)}, + "zoom out/in 20%", + function(unireader,keydef) + unireader:setGlobalZoom(unireader.globalzoom + (keydef.keycode==KEY_PGBCK and -1 or 1)*unireader.globalzoom_orig*0.2) end) - self.commands:add(KEY_BACK,nil,"back", + self.commands:add(KEY_BACK,nil,"Back", "back to last jump", function(unireader) if #unireader.jump_stack ~= 0 then unireader:goto(unireader.jump_stack[1].page) end end) - self.commands:add(KEY_BACK,MOD_ALT,"back", + self.commands:add(KEY_BACK,MOD_ALT,"Back", "close document", function(unireader) return "break" end) - self.commands:add(KEY_VPLUS,nil,"vol+", - "increase gamma 25%", + self.commands:add(KEY_HOME,MOD_ALT,"Home", + "exit application", function(unireader) - unireader:modifyGamma( 1.25 ) + keep_running = false + return "break" end) - self.commands:add(KEY_VMINUS,nil,"vol-", - "decrease gamma 25%", - function(unireader) - unireader:modifyGamma( 0.80 ) + self.commands:addGroup("vol-/+",{Keydef:new(KEY_VPLUS,nil),Keydef:new(KEY_VMINUS,nil)}, + "decrease/increase gamma 25%", + function(unireader,keydef) + unireader:modifyGamma(keydef.keycode==KEY_VPLUS and 1.25 or 0.8) end) --numeric key group local numeric_keydefs = {} for i=1,10 do numeric_keydefs[i]=Keydef:new(KEY_1+i-1,nil,tostring(i%10)) end - self.commands:addGroup("[1..0]",numeric_keydefs, - "jump to *10% of document", + self.commands:addGroup("[1, 2 .. 9, 0]",numeric_keydefs, + "jump to 10%, 20% .. 90%, 100% of document", function(unireader,keydef) print('jump to page: '..math.max(math.floor(unireader.doc:getPages()*(keydef.keycode-KEY_1)/9),1)..'/'..unireader.doc:getPages()) unireader:goto(math.max(math.floor(unireader.doc:getPages()*(keydef.keycode-KEY_1)/9),1)) @@ -918,7 +905,7 @@ function UniReader:addAllCommands() unireader:setGlobalZoomMode(unireader.ZOOM_FIT_TO_CONTENT_HALF_WIDTH) end) self.commands:add(KEY_G,nil,"G", - "goto page", + "open 'go to page' input box", function(unireader) local page = InputBox:input(height-100, 100, "Page:") -- convert string to number @@ -972,12 +959,6 @@ function UniReader:addAllCommands() function(unireader) unireader:screenRotate("anticlockwise") end) - self.commands:add(KEY_HOME,MOD_SHIFT_OR_ALT,"Home", - "exit application", - function(unireader) - keep_running = false - return "break" - end) self.commands:add(KEY_Z,nil,"Z", "set crop mode", function(unireader) @@ -1007,7 +988,7 @@ function UniReader:addAllCommands() print("# bbox override: ", unireader.bbox.enabled); end) self.commands:add(KEY_MENU,nil,"Menu", - "open menu", + "toggle info box", function(unireader) unireader:showMenu() unireader:goto(unireader.pageno)