Permalink
Browse files

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.
  • Loading branch information...
1 parent ff38118 commit f307264fb67d3c1108b2442e0771e2e2a8c96356 @hwhw hwhw committed Dec 1, 2011
Showing with 369 additions and 10 deletions.
  1. +6 −1 Makefile
  2. +14 −0 blitbuffer.c
  3. +53 −9 einkfb.c
  4. +176 −0 ft.c
  5. +28 −0 ft.h
  6. +2 −0 kpdfview.c
  7. +54 −0 rendertext.lua
  8. +17 −0 rendertext_example.lua
  9. +19 −0 util.c
View
@@ -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,14 +55,15 @@ 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 \
pdf.o \
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 $@
View
@@ -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}
};
View
@@ -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
View
176 ft.c
@@ -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;
+}
View
28 ft.h
@@ -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
+
View
@@ -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);
Oops, something went wrong.

0 comments on commit f307264

Please sign in to comment.