fixed various bugs in pointer handling

pull/2/merge
HW 12 years ago
parent db76421567
commit ec905a5a13

@ -61,6 +61,7 @@ static int getHeight(lua_State *L) {
static int freeBlitBuffer(lua_State *L) { static int freeBlitBuffer(lua_State *L) {
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer"); BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
// should be save if called twice
if(bb->allocated && bb->data != NULL) { if(bb->allocated && bb->data != NULL) {
free(bb->data); free(bb->data);
bb->data = NULL; bb->data = NULL;

@ -78,7 +78,12 @@ static int setGammaIndex(lua_State *L) {
static int closeDocument(lua_State *L) { static int closeDocument(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
delete doc->text_view;
// should be save if called twice
if(doc->text_view != NULL) {
delete doc->text_view;
doc->text_view = NULL;
}
return 0; return 0;
} }

@ -97,6 +97,8 @@ static int openDocument(lua_State *L) {
static int closeDocument(lua_State *L) { static int closeDocument(lua_State *L) {
DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument");
// should be save if called twice
if(doc->doc_ref != NULL) { if(doc->doc_ref != NULL) {
ddjvu_document_release(doc->doc_ref); ddjvu_document_release(doc->doc_ref);
doc->doc_ref = NULL; doc->doc_ref = NULL;
@ -352,6 +354,8 @@ static int getPageText(lua_State *L) {
static int closePage(lua_State *L) { static int closePage(lua_State *L) {
DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage"); DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage");
// should be save if called twice
if(page->page_ref != NULL) { if(page->page_ref != NULL) {
ddjvu_page_release(page->page_ref); ddjvu_page_release(page->page_ref);
page->page_ref = NULL; page->page_ref = NULL;

@ -123,6 +123,7 @@ static int getSize(lua_State *L) {
static int closeFrameBuffer(lua_State *L) { static int closeFrameBuffer(lua_State *L) {
FBInfo *fb = (FBInfo*) luaL_checkudata(L, 1, "einkfb"); FBInfo *fb = (FBInfo*) luaL_checkudata(L, 1, "einkfb");
// should be save if called twice
if(fb->buf != NULL && fb->buf->data != NULL) { if(fb->buf != NULL && fb->buf->data != NULL) {
#ifndef EMULATE_READER #ifndef EMULATE_READER
munmap(fb->buf->data, fb->finfo.smem_len); munmap(fb->buf->data, fb->finfo.smem_len);
@ -131,6 +132,10 @@ static int closeFrameBuffer(lua_State *L) {
free(fb->buf->data); free(fb->buf->data);
#endif #endif
fb->buf->data = NULL; fb->buf->data = NULL;
// the blitbuffer in fb->buf should be freed
// by the Lua GC when our object is garbage
// collected sice it is visible as an entry
// in the fb table
} }
return 0; return 0;
} }

68
ft.c

@ -29,26 +29,35 @@
FT_Library freetypelib; FT_Library freetypelib;
typedef struct KPVFace {
FT_Face face;
int allocated_face;
} KPVFace;
static int newFace(lua_State *L) { static int newFace(lua_State *L) {
const char *filename = luaL_checkstring(L, 1); const char *filename = luaL_checkstring(L, 1);
int pxsize = luaL_optint(L, 2, 16*64); int pxsize = luaL_optint(L, 2, 16*64);
FT_Face *face = (FT_Face*) lua_newuserdata(L, sizeof(FT_Face)); KPVFace *face = (KPVFace*) lua_newuserdata(L, sizeof(KPVFace));
luaL_getmetatable(L, "ft_face"); luaL_getmetatable(L, "ft_face");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
FT_Error error = FT_New_Face(freetypelib, filename, 0, face); face->allocated_face = 0;
FT_Error error = FT_New_Face(freetypelib, filename, 0, &face->face);
if(error) { if(error) {
return luaL_error(L, "freetype error"); return luaL_error(L, "freetype error");
} }
error = FT_Set_Pixel_Sizes(*face, 0, pxsize); face->allocated_face = 1;
error = FT_Set_Pixel_Sizes(face->face, 0, pxsize);
if(error) { if(error) {
error = FT_Done_Face(*face); error = FT_Done_Face(face->face);
return luaL_error(L, "freetype error"); return luaL_error(L, "freetype error");
} }
if((*face)->charmap == NULL) { if(face->face->charmap == NULL) {
//TODO //TODO
//fprintf(stderr, "no unicode charmap found, to be implemented.\n"); //fprintf(stderr, "no unicode charmap found, to be implemented.\n");
} }
@ -56,15 +65,15 @@ static int newFace(lua_State *L) {
} }
static int renderGlyph(lua_State *L) { static int renderGlyph(lua_State *L) {
FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
int ch = luaL_checkint(L, 2); int ch = luaL_checkint(L, 2);
FT_Error error = FT_Load_Char(*face, ch, FT_LOAD_RENDER); FT_Error error = FT_Load_Char(face->face, ch, FT_LOAD_RENDER);
if(error) { if(error) {
return luaL_error(L, "freetype error"); return luaL_error(L, "freetype error");
} }
int w = (*face)->glyph->bitmap.width; int w = face->face->glyph->bitmap.width;
int h = (*face)->glyph->bitmap.rows; int h = face->face->glyph->bitmap.rows;
lua_newtable(L); lua_newtable(L);
@ -80,7 +89,7 @@ static int renderGlyph(lua_State *L) {
int y; int y;
int x; int x;
for(y = 0; y < h; y++) { for(y = 0; y < h; y++) {
uint8_t *src = (*face)->glyph->bitmap.buffer + y * (*face)->glyph->bitmap.pitch; uint8_t *src = face->face->glyph->bitmap.buffer + y * face->face->glyph->bitmap.pitch;
for(x = 0; x < (w/2); x++) { for(x = 0; x < (w/2); x++) {
*dst = (src[0] & 0xF0) | (src[1] >> 4); *dst = (src[0] & 0xF0) | (src[1] >> 4);
src+=2; src+=2;
@ -92,23 +101,23 @@ static int renderGlyph(lua_State *L) {
} }
} }
lua_pushinteger(L, (*face)->glyph->bitmap_left); lua_pushinteger(L, face->face->glyph->bitmap_left);
lua_setfield(L, -2, "l"); lua_setfield(L, -2, "l");
lua_pushinteger(L, (*face)->glyph->bitmap_top); lua_pushinteger(L, face->face->glyph->bitmap_top);
lua_setfield(L, -2, "t"); lua_setfield(L, -2, "t");
lua_pushinteger(L, (*face)->glyph->metrics.horiAdvance >> 6); lua_pushinteger(L, face->face->glyph->metrics.horiAdvance >> 6);
lua_setfield(L, -2, "r"); lua_setfield(L, -2, "r");
lua_pushinteger(L, (*face)->glyph->advance.x >> 6); lua_pushinteger(L, face->face->glyph->advance.x >> 6);
lua_setfield(L, -2, "ax"); lua_setfield(L, -2, "ax");
lua_pushinteger(L, (*face)->glyph->advance.y >> 6); lua_pushinteger(L, face->face->glyph->advance.y >> 6);
lua_setfield(L, -2, "ay"); lua_setfield(L, -2, "ay");
return 1; return 1;
} }
static int hasKerning(lua_State *L) { static int hasKerning(lua_State *L) {
FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
if(FT_HAS_KERNING((*face))) { if(FT_HAS_KERNING((face->face))) {
lua_pushinteger(L, 1); lua_pushinteger(L, 1);
} else { } else {
lua_pushinteger(L, 0); lua_pushinteger(L, 0);
@ -117,11 +126,11 @@ static int hasKerning(lua_State *L) {
} }
static int getKerning(lua_State *L) { static int getKerning(lua_State *L) {
FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
int left = FT_Get_Char_Index(*face, luaL_checkint(L, 2)); int left = FT_Get_Char_Index(face->face, luaL_checkint(L, 2));
int right = FT_Get_Char_Index(*face, luaL_checkint(L, 3)); int right = FT_Get_Char_Index(face->face, luaL_checkint(L, 3));
FT_Vector kerning; FT_Vector kerning;
FT_Error error = FT_Get_Kerning(*face, left, right, FT_KERNING_DEFAULT, &kerning); FT_Error error = FT_Get_Kerning(face->face, left, right, FT_KERNING_DEFAULT, &kerning);
if(error) { if(error) {
return luaL_error(L, "freetype error when getting kerning (l=%d, r=%d)", left, right); return luaL_error(L, "freetype error when getting kerning (l=%d, r=%d)", left, right);
} }
@ -130,18 +139,18 @@ static int getKerning(lua_State *L) {
} }
static int getHeightAndAscender(lua_State *L) { static int getHeightAndAscender(lua_State *L) {
FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
double pixels_height,pixels_ascender; double pixels_height,pixels_ascender;
double em_size, y_scale; double em_size, y_scale;
/* compute floating point scale factors */ /* compute floating point scale factors */
em_size = 1.0 * (*face)->units_per_EM; em_size = 1.0 * face->face->units_per_EM;
y_scale = (*face)->size->metrics.y_ppem / em_size; y_scale = face->face->size->metrics.y_ppem / em_size;
/* convert design distances to floating point pixels */ /* convert design distances to floating point pixels */
pixels_height = (*face)->height * y_scale; pixels_height = face->face->height * y_scale;
pixels_ascender = (*face)->ascender * y_scale; pixels_ascender = face->face->ascender * y_scale;
lua_pushnumber(L, pixels_height); lua_pushnumber(L, pixels_height);
lua_pushnumber(L, pixels_ascender); lua_pushnumber(L, pixels_ascender);
@ -149,13 +158,12 @@ static int getHeightAndAscender(lua_State *L) {
} }
static int doneFace(lua_State *L) { static int doneFace(lua_State *L) {
FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face");
if(*face != NULL) { if(face->allocated_face) {
FT_Error error = FT_Done_Face(*face); FT_Error error = FT_Done_Face(face->face);
if(error) { if(error) {
return luaL_error(L, "freetype error when freeing face"); return luaL_error(L, "freetype error when freeing face");
} }
*face = NULL;
} }
return 0; return 0;
} }

@ -84,11 +84,11 @@ static int toBlitBuffer(lua_State *L) {
} else { } else {
fz_try(img->context) { fz_try(img->context) {
pix = fz_new_pixmap(img->context, fz_device_gray, img->pixmap->w, img->pixmap->h); pix = fz_new_pixmap(img->context, fz_device_gray, img->pixmap->w, img->pixmap->h);
fz_convert_pixmap(img->context, pix, img->pixmap);
} }
fz_catch(img->context) { fz_catch(img->context) {
return luaL_error(L, "can't claim new grayscale fz_pixmap"); return luaL_error(L, "can't claim or convert new grayscale fz_pixmap");
} }
fz_convert_pixmap(img->context, img->pixmap, pix);
} }
ret = newBlitBufferNative(L, img->pixmap->w, img->pixmap->h, &bb); ret = newBlitBufferNative(L, img->pixmap->w, img->pixmap->h, &bb);
@ -121,10 +121,17 @@ static int toBlitBuffer(lua_State *L) {
static int freeImage(lua_State *L) { static int freeImage(lua_State *L) {
Image *img = (Image*) luaL_checkudata(L, 1, "image"); Image *img = (Image*) luaL_checkudata(L, 1, "image");
if(img->pixmap) {
// should be save if called twice
if(img->pixmap != NULL) {
fz_drop_pixmap(img->context, img->pixmap); fz_drop_pixmap(img->context, img->pixmap);
img->pixmap = NULL;
}
if(img->context != NULL) {
fz_free_context(img->context);
img->context = NULL;
} }
fz_free_context(img->context);
return 0; return 0;
} }

@ -208,6 +208,8 @@ static int authenticatePassword(lua_State *L) {
static int closeDocument(lua_State *L) { static int closeDocument(lua_State *L) {
PdfDocument *doc = (PdfDocument*) luaL_checkudata(L, 1, "pdfdocument"); PdfDocument *doc = (PdfDocument*) luaL_checkudata(L, 1, "pdfdocument");
// should be save if called twice
if(doc->xref != NULL) { if(doc->xref != NULL) {
fz_close_document(doc->xref); fz_close_document(doc->xref);
doc->xref = NULL; doc->xref = NULL;
@ -216,6 +218,7 @@ static int closeDocument(lua_State *L) {
fz_free_context(doc->context); fz_free_context(doc->context);
doc->context = NULL; doc->context = NULL;
} }
return 0; return 0;
} }

Loading…
Cancel
Save