mirror of https://github.com/koreader/koreader
added freetype text rendering (still buggy)
this allows to render glyphs and also brings a simple engine for rendering UTF-8 strings onto the framebuffer. blitting to uneven offset is implemented here, too, but needs more work and is still buggy. In the end, this will allow for a simple GUI.pull/2/merge
parent
ff38118a89
commit
f307264fb6
@ -0,0 +1,176 @@
|
||||
/*
|
||||
KindlePDFViewer: FreeType font rastering for UI
|
||||
Copyright (C) 2011 Hans-Werner Hilse <hilse@web.de>
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include "blitbuffer.h"
|
||||
|
||||
/* for font access: */
|
||||
#include <fitz/fitz.h>
|
||||
#include <pdf/mupdf.h>
|
||||
|
||||
#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;
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
KindlePDFViewer: FreeType font rastering for UI
|
||||
Copyright (C) 2011 Hans-Werner Hilse <hilse@web.de>
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef _PDF_FT_H
|
||||
#define _PDF_FT_H
|
||||
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
int luaopen_ft(lua_State *L);
|
||||
|
||||
#endif
|
||||
|
@ -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
|
||||
|
@ -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
|
Loading…
Reference in New Issue