diff --git a/Makefile b/Makefile index ffc97b782..352621700 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ MUPDFLIBDIR=$(MUPDFDIR)/$(MUPDFTARGET) SQLITE3DIR=sqlite-amalgamation-3070900 LSQLITE3DIR=lsqlite3_svn08 +FREETYPEDIR=$(MUPDFDIR)/thirdparty/freetype-2.4.4 # set this to your ARM cross compiler: @@ -54,7 +55,7 @@ SQLITE3LDFLAGS := -lpthread LUALIB := $(LUADIR)/src/liblua.a -kpdfview: kpdfview.o einkfb.o pdf.o blitbuffer.o input.o util.o $(SQLITE3OBJS) $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) +kpdfview: kpdfview.o einkfb.o pdf.o blitbuffer.o input.o util.o ft.o $(SQLITE3OBJS) $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) $(CC) -lm -ldl $(EMU_LDFLAGS) $(SQLITE3LDFLAGS) \ kpdfview.o \ einkfb.o \ @@ -62,6 +63,7 @@ kpdfview: kpdfview.o einkfb.o pdf.o blitbuffer.o input.o util.o $(SQLITE3OBJS) $ blitbuffer.o \ input.o \ util.o \ + ft.o \ $(SQLITE3OBJS) \ $(MUPDFLIBS) \ $(THIRDPARTYLIBS) \ @@ -71,6 +73,9 @@ kpdfview: kpdfview.o einkfb.o pdf.o blitbuffer.o input.o util.o $(SQLITE3OBJS) $ einkfb.o input.o: %.o: %.c $(CC) -c $(KPDFREADER_CFLAGS) $(EMU_CFLAGS) $< -o $@ +ft.o: %.o: %.c + $(CC) -c $(KPDFREADER_CFLAGS) -I$(FREETYPEDIR)/include $< -o $@ + kpdfview.o pdf.o blitbuffer.o util.o: %.o: %.c $(CC) -c $(KPDFREADER_CFLAGS) $< -o $@ diff --git a/blitbuffer.c b/blitbuffer.c index 03f0f675e..5e0458867 100644 --- a/blitbuffer.c +++ b/blitbuffer.c @@ -30,6 +30,18 @@ static int newBlitBuffer(lua_State *L) { return 1; } +static int getWidth(lua_State *L) { + BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer"); + lua_pushinteger(L, bb->w); + return 1; +} + +static int getHeight(lua_State *L) { + BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer"); + lua_pushinteger(L, bb->h); + return 1; +} + static int freeBlitBuffer(lua_State *L) { BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer"); @@ -43,6 +55,8 @@ static const struct luaL_reg blitbuffer_func[] = { }; static const struct luaL_reg blitbuffer_meth[] = { + {"getWidth", getWidth}, + {"getHeight", getHeight}, {"free", freeBlitBuffer}, {NULL, NULL} }; diff --git a/einkfb.c b/einkfb.c index 778a7bc9e..26d19f63a 100644 --- a/einkfb.c +++ b/einkfb.c @@ -96,6 +96,7 @@ static int openFrameBuffer(lua_State *L) { fb->finfo.type = FB_TYPE_PACKED_PIXELS; fb->data = malloc(fb->finfo.smem_len); #endif + memset(fb->data, 0, fb->finfo.smem_len); return 1; } @@ -134,9 +135,9 @@ static int blitFullToFrameBuffer(lua_State *L) { static int blitToFrameBuffer(lua_State *L) { FBInfo *fb = (FBInfo*) luaL_checkudata(L, 1, "einkfb"); BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 2, "blitbuffer"); - int xdest = luaL_checkint(L, 3) & 0x7FFFFFFE; + int xdest = luaL_checkint(L, 3); int ydest = luaL_checkint(L, 4); - int xoffs = luaL_checkint(L, 5) & 0x7FFFFFFE; + int xoffs = luaL_checkint(L, 5); int yoffs = luaL_checkint(L, 6); int w = luaL_checkint(L, 7); int h = luaL_checkint(L, 8); @@ -145,7 +146,7 @@ static int blitToFrameBuffer(lua_State *L) { // check bounds if(yoffs >= bb->h) { return 0; - } else if(yoffs + h > bb->w) { + } else if(yoffs + h > bb->h) { h = bb->h - yoffs; } if(ydest >= fb->vinfo.yres) { @@ -164,23 +165,66 @@ static int blitToFrameBuffer(lua_State *L) { w = fb->vinfo.xres - xdest; } + uint8_t *fbptr; + uint8_t *bbptr; + uint8_t smask; + + if(xdest & 1) { + /* this will render the leftmost column */ + fbptr = (uint8_t*)(fb->data + + ydest * fb->finfo.line_length + + xdest / 2); + bbptr = (uint8_t*)(bb->data + + yoffs * bb->w / 2 + + xoffs / 2 ); + if(xoffs & 1) { + for(y = 0; y < h; y++) { + *fbptr &= 0xF0; + *fbptr |= *bbptr & 0x0F; + fbptr += fb->finfo.line_length; + } + } else { + for(y = 0; y < h; y++) { + *fbptr &= 0xF0; + *fbptr |= (*bbptr & 0xF0) >> 4; + fbptr += fb->finfo.line_length; + } + } + xdest++; + xoffs++; + w--; + } w = (w+1) / 2; // we'll always do two pixels at once for now - uint8_t *fbptr = (uint8_t*)(fb->data + + fbptr = (uint8_t*)(fb->data + ydest * fb->finfo.line_length + xdest / 2); - uint8_t *bbptr = (uint8_t*)(bb->data + + bbptr = (uint8_t*)(bb->data + yoffs * bb->w / 2 + xoffs / 2 ); - for(y = 0; y < h; y++) { - memcpy(fbptr, bbptr, w); - fbptr += fb->finfo.line_length; - bbptr += (bb->w / 2); + if(xoffs & 1) { + for(y = 0; y < h; y++) { + for(x = 0; x < w; x++) { + fbptr[x] = (bbptr[x-1] << 4) | ((bbptr[x] & 0xF0) >> 4); + } + fbptr += fb->finfo.line_length; + bbptr += (bb->w / 2); + } + } else { + for(y = 0; y < h; y++) { + memcpy(fbptr, bbptr, w); + fbptr += fb->finfo.line_length; + bbptr += (bb->w / 2); + } } return 0; } +static int paintRect(lua_State *L) { + FBInfo *fb = (FBInfo*) luaL_checkudata(L, 1, "einkfb"); +} + static int einkUpdate(lua_State *L) { FBInfo *fb = (FBInfo*) luaL_checkudata(L, 1, "einkfb"); // for Kindle e-ink display diff --git a/ft.c b/ft.c new file mode 100644 index 000000000..6a472dbad --- /dev/null +++ b/ft.c @@ -0,0 +1,176 @@ +/* + KindlePDFViewer: FreeType font rastering for UI + Copyright (C) 2011 Hans-Werner Hilse + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include +#include +#include +#include +#include FT_FREETYPE_H +#include "blitbuffer.h" + +/* for font access: */ +#include +#include + +#include "ft.h" + +FT_Library freetypelib; + +static int newFace(lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + int pxsize = luaL_optint(L, 2, 16*64); + + FT_Face *face = (FT_Face*) lua_newuserdata(L, sizeof(FT_Face)); + luaL_getmetatable(L, "ft_face"); + lua_setmetatable(L, -2); + + FT_Error error = FT_New_Face(freetypelib, filename, 0, face); + if(error) { + return luaL_error(L, "freetype error"); + } + + error = FT_Set_Pixel_Sizes(*face, 0, pxsize); + if(error) { + error = FT_Done_Face(*face); + return luaL_error(L, "freetype error"); + } + + if((*face)->charmap == NULL) { + //TODO + //fprintf(stderr, "no unicode charmap found, to be implemented.\n"); + } + return 1; +} + +static int newBuiltinFace(lua_State *L) { + const char *fontname = luaL_checkstring(L, 1); + int pxsize = luaL_optint(L, 2, 16*64); + + unsigned int size; + const char *fontdata = pdf_find_builtin_font(fontname, &size); + if(fontdata == NULL) { + return luaL_error(L, "no such built-in font"); + } + + FT_Face *face = (FT_Face*) lua_newuserdata(L, sizeof(FT_Face)); + luaL_getmetatable(L, "ft_face"); + lua_setmetatable(L, -2); + + FT_Error error = FT_New_Memory_Face(freetypelib, (FT_Byte*)fontdata, size, 0, face); + if(error) { + return luaL_error(L, "freetype error"); + } + + error = FT_Set_Pixel_Sizes(*face, 0, pxsize); + if(error) { + error = FT_Done_Face(*face); + return luaL_error(L, "freetype error"); + } + + if((*face)->charmap == NULL) { + //TODO + //fprintf(stderr, "no unicode charmap found, to be implemented.\n"); + } + return 1; +} + + +static int renderGlyph(lua_State *L) { + FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); + int ch = luaL_checkint(L, 2); + FT_Error error = FT_Load_Char(*face, ch, FT_LOAD_RENDER); + if(error) { + return luaL_error(L, "freetype error"); + } + + int w = ((*face)->glyph->bitmap.width + 1) & -2; // 2px steps + int h = (*face)->glyph->bitmap.rows; + + lua_newtable(L); + + BlitBuffer *bb = (BlitBuffer*) lua_newuserdata(L, sizeof(BlitBuffer) + (w * h / 2) - 1); + luaL_getmetatable(L, "blitbuffer"); + lua_setmetatable(L, -2); + + lua_setfield(L, -2, "bb"); + + bb->w = w; + bb->h = h; + + uint8_t *dst = bb->data; + int y; + int x; + w = (*face)->glyph->bitmap.width; + for(y = 0; y < h; y++) { + uint8_t *src = (*face)->glyph->bitmap.buffer + y * (*face)->glyph->bitmap.pitch; + for(x = 0; x < w; x+=2) { + *dst = *src & 0xF0; + src++; + if(x+1 < w) { + *dst |= (*src & 0xF0) >> 4; + src++; + } + dst++; + } + } + + lua_pushinteger(L, (*face)->glyph->bitmap_left); + lua_setfield(L, -2, "l"); + lua_pushinteger(L, (*face)->glyph->bitmap_top); + lua_setfield(L, -2, "t"); + lua_pushinteger(L, (*face)->glyph->advance.x >> 6); + lua_setfield(L, -2, "ax"); + + return 1; +} + +static int doneFace(lua_State *L) { + FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); + FT_Error error = FT_Done_Face(*face); + if(error) { + return luaL_error(L, "freetype error when freeing face"); + } + return 0; +} + +static const struct luaL_reg ft_face_meth[] = { + {"renderGlyph", renderGlyph}, + {"done", doneFace}, + {NULL, NULL} +}; + +static const struct luaL_reg ft_func[] = { + {"newFace", newFace}, + {"newBuiltinFace", newBuiltinFace}, + {NULL, NULL} +}; + +int luaopen_ft(lua_State *L) { + int error = FT_Init_FreeType(&freetypelib); + if(error) { + return luaL_error(L, "freetype error on initialization"); + } + + luaL_newmetatable(L, "ft_face"); + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_settable(L, -3); + luaL_register(L, NULL, ft_face_meth); + + luaL_register(L, "freetype", ft_func); + return 1; +} diff --git a/ft.h b/ft.h new file mode 100644 index 000000000..1ecad5226 --- /dev/null +++ b/ft.h @@ -0,0 +1,28 @@ +/* + KindlePDFViewer: FreeType font rastering for UI + Copyright (C) 2011 Hans-Werner Hilse + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef _PDF_FT_H +#define _PDF_FT_H + +#include +#include +#include + +int luaopen_ft(lua_State *L); + +#endif + diff --git a/kpdfview.c b/kpdfview.c index fbd6579aa..55f56517d 100644 --- a/kpdfview.c +++ b/kpdfview.c @@ -27,6 +27,7 @@ #include "pdf.h" #include "einkfb.h" #include "input.h" +#include "ft.h" /* forward declaration for luasqlite3: */ LUALIB_API int luaopen_lsqlite3(lua_State *L); @@ -51,6 +52,7 @@ int main(int argc, char **argv) { luaopen_pdf(L); luaopen_input(L); luaopen_util(L); + luaopen_ft(L); luaopen_lsqlite3(L); diff --git a/rendertext.lua b/rendertext.lua new file mode 100644 index 000000000..ab19dd3ad --- /dev/null +++ b/rendertext.lua @@ -0,0 +1,54 @@ +glyphcache_max_memsize = 256*1024 -- 256kB glyphcache +glyphcache_current_memsize = 0 +glyphcache = {} +glyphcache_max_age = 4096 +function glyphcacheclaim(size) + if(size > glyphcache_max_memsize) then + error("too much memory claimed") + return false + end + while glyphcache_current_memsize + size > glyphcache_max_memsize do + for k, _ in pairs(glyphcache) do + if glyphcache[k].age > 0 then + glyphcache[k].age = glyphcache[k].age - 1 + else + glyphcache_current_memsize = glyphcache_current_memsize - glyphcache[k].size + glyphcache[k] = nil + end + end + end + glyphcache_current_memsize = glyphcache_current_memsize + size + return true +end +function getglyph(face, facehash, charcode) + local hash = glyphcachehash(facehash, charcode) + if glyphcache[hash] == nil then + print("render glyph") + local glyph = face:renderGlyph(charcode) + local size = glyph.bb:getWidth() * glyph.bb:getHeight() / 2 + 32 + print("cache claim") + glyphcacheclaim(size); + glyphcache[hash] = { + age = glyphcache_max_age, + size = size, + g = glyph + } + end + return glyphcache[hash].g +end +function glyphcachehash(face, charcode) + return face..'_'..charcode; +end +function clearglyphcache() + glyphcache = {} +end + +function renderUtf8Text(x, y, face, facehash, text) + local pen_x = 0 + for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do + local glyph = getglyph(face, facehash, util.utf8charcode(uchar)) + fb:blitFrom(glyph.bb, x + pen_x + glyph.l, y - glyph.t, 0, 0, glyph.bb:getWidth(), glyph.bb:getHeight()) + pen_x = pen_x + glyph.ax + end +end + diff --git a/rendertext_example.lua b/rendertext_example.lua new file mode 100644 index 000000000..2d7733844 --- /dev/null +++ b/rendertext_example.lua @@ -0,0 +1,17 @@ +require "rendertext" + +fb = einkfb.open("/dev/fb0") +width, height = fb:getSize() + +print("open") + +face = freetype.newBuiltinFace("Helvetica", 64) +print("got face") + +renderUtf8Text(100,100,face,"h","Hello World! äöü") + +fb:refresh() + +while true do + local ev = input.waitForEvent() +end diff --git a/util.c b/util.c index 4a63997b1..7da912fc0 100644 --- a/util.c +++ b/util.c @@ -28,8 +28,27 @@ static int gettime(lua_State *L) { return 2; } +static int utf8charcode(lua_State *L) { + size_t len; + const char* utf8char = luaL_checklstring(L, 1, &len); + int c; + if(len == 1) { + c = utf8char[0] & 0x7F; /* should not be needed */ + } else if(len == 2) { + c = ((utf8char[0] & 0x1F) << 6) | (utf8char[1] & 0x3F); + } else if(len == 3) { + c = ((utf8char[0] & 0x0F) << 12) | ((utf8char[1] & 0x3F) << 6) | (utf8char[2] & 0x3F); + } else { + // 4, 5, 6 byte cases still missing + return 0; + } + lua_pushinteger(L, c); + return 1; +} + static const struct luaL_reg util_func[] = { {"gettime", gettime}, + {"utf8charcode", utf8charcode}, {NULL, NULL} };