2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/ft.c

201 lines
5.1 KiB
C
Raw Normal View History

/*
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 <pdf/mupdf-internal.h>
#include "ft.h"
FT_Library freetypelib;
2012-04-16 20:51:13 +00:00
typedef struct KPVFace {
FT_Face face;
int allocated_face;
} KPVFace;
static int newFace(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
int pxsize = luaL_optint(L, 2, 16*64);
2012-04-16 20:51:13 +00:00
KPVFace *face = (KPVFace*) lua_newuserdata(L, sizeof(KPVFace));
luaL_getmetatable(L, "ft_face");
lua_setmetatable(L, -2);
2012-04-16 20:51:13 +00:00
face->allocated_face = 0;
FT_Error error = FT_New_Face(freetypelib, filename, 0, &face->face);
if(error) {
return luaL_error(L, "freetype error");
}
2012-04-16 20:51:13 +00:00
face->allocated_face = 1;
error = FT_Set_Pixel_Sizes(face->face, 0, pxsize);
if(error) {
2012-04-16 20:51:13 +00:00
error = FT_Done_Face(face->face);
return luaL_error(L, "freetype error");
}
2012-04-16 20:51:13 +00:00
if(face->face->charmap == NULL) {
//TODO
//fprintf(stderr, "no unicode charmap found, to be implemented.\n");
}
return 1;
}
static int renderGlyph(lua_State *L) {
2012-04-16 20:51:13 +00:00
KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
int ch = luaL_checkint(L, 2);
2012-04-16 20:51:13 +00:00
FT_Error error = FT_Load_Char(face->face, ch, FT_LOAD_RENDER);
if(error) {
return luaL_error(L, "freetype error");
}
2012-04-16 20:51:13 +00:00
int w = face->face->glyph->bitmap.width;
int h = face->face->glyph->bitmap.rows;
lua_newtable(L);
BlitBuffer *bb;
int result = newBlitBufferNative(L, w, h, &bb);
if(result != 1) {
return result;
}
lua_setfield(L, -2, "bb");
uint8_t *dst = bb->data;
int y;
int x;
for(y = 0; y < h; y++) {
2012-04-16 20:51:13 +00:00
uint8_t *src = face->face->glyph->bitmap.buffer + y * face->face->glyph->bitmap.pitch;
for(x = 0; x < (w/2); x++) {
*dst = (src[0] & 0xF0) | (src[1] >> 4);
src+=2;
dst++;
}
if(w & 1) {
*dst = *src & 0xF0;
dst++;
}
}
2012-04-16 20:51:13 +00:00
lua_pushinteger(L, face->face->glyph->bitmap_left);
lua_setfield(L, -2, "l");
2012-04-16 20:51:13 +00:00
lua_pushinteger(L, face->face->glyph->bitmap_top);
lua_setfield(L, -2, "t");
2012-04-16 20:51:13 +00:00
lua_pushinteger(L, face->face->glyph->metrics.horiAdvance >> 6);
2012-04-15 14:07:39 +00:00
lua_setfield(L, -2, "r");
2012-04-16 20:51:13 +00:00
lua_pushinteger(L, face->face->glyph->advance.x >> 6);
lua_setfield(L, -2, "ax");
2012-04-16 20:51:13 +00:00
lua_pushinteger(L, face->face->glyph->advance.y >> 6);
2012-03-19 18:41:09 +00:00
lua_setfield(L, -2, "ay");
return 1;
}
2011-12-01 15:11:43 +00:00
static int hasKerning(lua_State *L) {
2012-04-16 20:51:13 +00:00
KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
if(FT_HAS_KERNING((face->face))) {
2011-12-01 15:11:43 +00:00
lua_pushinteger(L, 1);
} else {
lua_pushinteger(L, 0);
}
return 1;
}
static int getKerning(lua_State *L) {
2012-04-16 20:51:13 +00:00
KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
int left = FT_Get_Char_Index(face->face, luaL_checkint(L, 2));
int right = FT_Get_Char_Index(face->face, luaL_checkint(L, 3));
2011-12-01 15:11:43 +00:00
FT_Vector kerning;
2012-04-16 20:51:13 +00:00
FT_Error error = FT_Get_Kerning(face->face, left, right, FT_KERNING_DEFAULT, &kerning);
2011-12-01 15:11:43 +00:00
if(error) {
return luaL_error(L, "freetype error when getting kerning (l=%d, r=%d)", left, right);
}
lua_pushinteger(L, kerning.x >> 6);
return 1;
}
static int getHeightAndAscender(lua_State *L) {
2012-04-16 20:51:13 +00:00
KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
2012-03-19 18:41:09 +00:00
double pixels_height,pixels_ascender;
double em_size, y_scale;
/* compute floating point scale factors */
2012-04-16 20:51:13 +00:00
em_size = 1.0 * face->face->units_per_EM;
y_scale = face->face->size->metrics.y_ppem / em_size;
2012-03-19 18:41:09 +00:00
/* convert design distances to floating point pixels */
2012-04-16 20:51:13 +00:00
pixels_height = face->face->height * y_scale;
pixels_ascender = face->face->ascender * y_scale;
2012-03-19 18:41:09 +00:00
lua_pushnumber(L, pixels_height);
lua_pushnumber(L, pixels_ascender);
return 2;
}
static int doneFace(lua_State *L) {
2012-04-16 20:51:13 +00:00
KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
if(face->allocated_face) {
FT_Error error = FT_Done_Face(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},
2011-12-01 15:11:43 +00:00
{"hasKerning", hasKerning},
{"getKerning", getKerning},
{"getHeightAndAscender", getHeightAndAscender},
{"done", doneFace},
{"__gc", doneFace},
{NULL, NULL}
};
static const struct luaL_Reg ft_func[] = {
{"newFace", newFace},
{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;
}