add: font menu and bold attribute toggle shortcut in crereader

This commit is contained in:
Qingping Hou
2012-04-06 18:05:54 +08:00
parent d3e8c57bc8
commit 085d79d033
2 changed files with 78 additions and 3 deletions

53
cre.cpp
View File

@@ -54,7 +54,6 @@ static int openDocument(lua_State *L) {
}
doc->text_view->setViewMode(DVM_SCROLL, -1);
doc->text_view->Resize(width, height);
doc->text_view->LoadDocument(file_name);
doc->text_view->Render();
@@ -153,7 +152,8 @@ static int walkTableOfContent(lua_State *L, LVTocItem *toc, int *count) {
* }
*
* Warnning: not like pdf or djvu support, page here refers to the
* position(height) within the document, not the real page number.
* percent of height within the document, not the real page number.
* We use page here just to keep consistent with other readers.
*
*/
static int getTableOfContent(lua_State *L) {
@@ -168,6 +168,41 @@ static int getTableOfContent(lua_State *L) {
return 1;
}
/*
* Return a table like this:
* {
* "FreeMono",
* "FreeSans",
* "FreeSerif",
* }
*
*/
static int getFontFaces(lua_State *L) {
int i = 0;
lString16Collection face_list;
fontMan->getFaceList(face_list);
lua_newtable(L);
for (i = 0; i < face_list.length(); i++)
{
lua_pushnumber(L, i+1);
lua_pushstring(L, UnicodeToLocal(face_list[i]).c_str());
lua_settable(L, -3);
}
return 1;
}
static int setFontFace(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *face = luaL_checkstring(L, 2);
doc->text_view->setDefaultFontFace(lString8(face));
return 0;
}
static int gotoPage(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
int pageno = luaL_checkint(L, 2);
@@ -206,6 +241,14 @@ static int zoomFont(lua_State *L) {
return 1;
}
static int toggleFontBolder(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
doc->text_view->doCommand(DCMD_TOGGLE_BOLD);
return 0;
}
static int drawCurrentPage(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
@@ -240,20 +283,26 @@ static int drawCurrentPage(lua_State *L) {
static const struct luaL_Reg cre_func[] = {
{"openDocument", openDocument},
{"getFontFaces", getFontFaces},
{NULL, NULL}
};
static const struct luaL_Reg credocument_meth[] = {
/* get methods */
{"getPages", getNumberOfPages},
{"getCurrentPage", getCurrentPage},
{"getPos", getPos},
{"getPosPercent", getPosPercent},
{"getFullHeight", getFullHeight},
{"getToc", getTableOfContent},
/* set methods */
{"setFontFace", setFontFace},
/* control methods */
{"gotoPage", gotoPage},
{"gotoPercent", gotoPercent},
{"gotoPos", gotoPos},
{"zoomFont", zoomFont},
{"toggleFontBolder", toggleFontBolder},
{"drawCurrentPage", drawCurrentPage},
{"close", closeDocument},
{"__gc", closeDocument},

View File

@@ -1,5 +1,6 @@
require "unireader"
require "inputbox"
require "selectmenu"
CREReader = UniReader:new{
pos = 0,
@@ -15,6 +16,7 @@ end
function CREReader:open(filename)
local ok
local file_type = string.lower(string.match(filename, ".+%.([^.]+)"))
-- these two format use the same css file
if file_type == "html" then
file_type = "htm"
end
@@ -125,7 +127,6 @@ function CREReader:adjustCreReaderCommands()
self.commands:del(KEY_D, nil, "D")
self.commands:del(KEY_D, MOD_SHIFT, "D")
self.commands:del(KEY_D, MOD_ALT, "D")
self.commands:del(KEY_F, nil, "F")
self.commands:del(KEY_F, MOD_SHIFT, "F")
self.commands:del(KEY_F, MOD_ALT, "F")
@@ -157,4 +158,29 @@ function CREReader:adjustCreReaderCommands()
cr:goto(math.floor(cr.doc:getFullHeight()*(keydef.keycode-KEY_1)/9))
end
)
self.commands:add(KEY_F, nil, "F",
"invoke font menu",
function(cr)
local face_list = cre.getFontFaces()
local fonts_menu = SelectMenu:new{
menu_title = "Fonts Menu",
item_array = face_list,
}
local item_no = fonts_menu:choose(0, height)
print(face_list[item_no])
if item_no then
cr.doc:setFontFace(face_list[item_no])
end
cr:redrawCurrentPage()
end
)
self.commands:add(KEY_F, MOD_ALT, "F",
"Toggle font bolder attribute",
function(cr)
cr.doc:toggleFontBolder()
cr:redrawCurrentPage()
end
)
end