Skip to content

Commit

Permalink
Merge pull request #238 from hwhw/master
Browse files Browse the repository at this point in the history
FFI-based MuPDF API
  • Loading branch information
chrox committed Oct 21, 2014
2 parents 4743191 + b47ddf0 commit 1c0cf89
Show file tree
Hide file tree
Showing 17 changed files with 1,845 additions and 1,384 deletions.
19 changes: 8 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ $(POPEN_NOSHELL_LIB):
CFLAGS="$(CFLAGS) $(if $(ANDROID),--sysroot=$(SYSROOT),)"

# k2pdfopt, fetched via GIT as a submodule
$(K2PDFOPT_LIB) $(LEPTONICA_LIB) $(TESSERACT_LIB): $(PNG_LIB) $(ZLIB)
$(K2PDFOPT_LIB) $(LEPTONICA_LIB) $(TESSERACT_LIB): $(PNG_LIB) $(ZLIB) $(MUPDF_LIB)
$(MAKE) -j$(PROCESSORS) -C $(K2PDFOPT_DIR) BUILDMODE=shared \
$(if $(EMULATE_READER),,HOST=$(if $(ANDROID),"arm-linux",$(CHOST))) \
CC="$(CC)" CFLAGS="$(CFLAGS) -O3 -I../$(MUPDF_DIR)/include" \
Expand All @@ -216,9 +216,9 @@ libs: \
$(if $(or $(EMULATE_READER),$(ANDROID),$(WIN32)),,$(OUTPUT_DIR)/libs/libkoreader-input.so) \
$(OUTPUT_DIR)/libs/libkoreader-lfs.so \
$(OUTPUT_DIR)/libs/libpic_jpeg.so \
$(OUTPUT_DIR)/libs/libkoreader-pdf.so \
$(if $(ANDROID),,$(OUTPUT_DIR)/libs/libkoreader-djvu.so) \
$(OUTPUT_DIR)/libs/libkoreader-cre.so
$(OUTPUT_DIR)/libs/libkoreader-cre.so \
$(OUTPUT_DIR)/libs/libwrap-mupdf.so

$(OUTPUT_DIR)/libs/libkoreader-input.so: input.c \
$(POPEN_NOSHELL_LIB)
Expand All @@ -235,14 +235,6 @@ $(OUTPUT_DIR)/libs/libpic_jpeg.so: pic_jpeg.c $(JPEG_LIB)

# put all the libs to the end of compile command to make ubuntu's tool chain
# happy
$(OUTPUT_DIR)/libs/libkoreader-pdf.so: pdf.c \
$(if $(or $(ANDROID),$(WIN32)),$(LUAJIT_LIB),) \
$(MUPDF_LIB) $(K2PDFOPT_LIB)
# Bionic's C library comes with its own pthread implementation
# So we need not to load pthread library for Android build
$(CC) -I$(MUPDF_DIR)/include $(K2PDFOPT_CFLAGS) $(DYNLIB_CFLAGS) \
-o $@ $^ $(if $(ANDROID),,-lpthread)

$(OUTPUT_DIR)/libs/libkoreader-djvu.so: djvu.c \
$(if $(or $(ANDROID),$(WIN32)),$(LUAJIT_LIB),) \
$(DJVULIBRE_LIB) $(K2PDFOPT_LIB)
Expand All @@ -257,6 +249,11 @@ $(OUTPUT_DIR)/libs/libkoreader-cre.so: cre.cpp \
$(if $(WIN32),-DQT_GL=1) \
-Wl,-rpath,'libs' -o $@ $^ $(STATICLIBSTDCPP)

$(OUTPUT_DIR)/libs/libwrap-mupdf.so: wrap-mupdf.c \
$(MUPDF_LIB)
$(CC) -I$(MUPDF_DIR)/include $(DYNLIB_CFLAGS) \
-o $@ $^

# ===========================================================================
# the attachment extraction tool:

Expand Down
173 changes: 167 additions & 6 deletions ffi-cdecl/mupdf_decl.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,185 @@
#include <mupdf/fitz.h>
#include "wrap-mupdf.h"

#include "ffi-cdecl.h"
#include "ffi-cdecl-luajit.h"

cdecl_const(FZ_PAGE_BLOCK_TEXT)

/* math */
cdecl_struct(fz_point_s)
cdecl_type(fz_point)

cdecl_struct(fz_rect_s)
cdecl_type(fz_rect)
cdecl_const(fz_unit_rect)
cdecl_const(fz_empty_rect)
cdecl_const(fz_infinite_rect)
cdecl_func(fz_intersect_rect)
cdecl_func(fz_union_rect)

cdecl_struct(fz_irect_s)
cdecl_type(fz_irect)
cdecl_const(fz_empty_irect)
cdecl_const(fz_infinite_irect)
cdecl_func(fz_intersect_irect)

cdecl_func(fz_irect_from_rect)
cdecl_func(fz_round_rect)

cdecl_struct(fz_matrix_s)
cdecl_type(fz_matrix)
cdecl_const(fz_identity)
cdecl_func(fz_concat)
cdecl_func(fz_scale)
cdecl_func(fz_pre_scale)
cdecl_func(fz_rotate)
cdecl_func(fz_pre_rotate)
cdecl_func(fz_translate)
cdecl_func(fz_pre_translate)

cdecl_func(fz_transform_rect)

/* context */
cdecl_type(fz_alloc_context)
cdecl_type(fz_colorspace)
cdecl_type(fz_context)
cdecl_type(fz_colorspace)
cdecl_type(fz_store_free_fn)
cdecl_func(fz_new_context_imp)
cdecl_func(fz_free_context)
cdecl_func(fz_register_document_handlers)

/* images */
cdecl_struct(fz_image_s)
cdecl_type(fz_image)
cdecl_func(fz_new_image_from_data) // compat, use wrapper instead:
cdecl_func(mupdf_new_image_from_data)
cdecl_func(fz_new_pixmap_from_image) // compat, use wrapper instead:
cdecl_func(mupdf_new_pixmap_from_image)
cdecl_func(fz_keep_image)
cdecl_func(fz_drop_image)

/* misc/assorted */
cdecl_struct(fz_storable_s)
cdecl_type(fz_storable)

cdecl_func(fz_load_png)
cdecl_func(fz_runetochar)

/* document */
cdecl_struct(fz_document_s)
cdecl_type(fz_document)
cdecl_struct(fz_page_s)
cdecl_type(fz_page)

cdecl_func(mupdf_open_document)
cdecl_func(fz_needs_password)
cdecl_func(fz_authenticate_password)
cdecl_func(fz_close_document)
cdecl_func(mupdf_count_pages)

/* page */
cdecl_func(mupdf_load_page)
cdecl_func(fz_bound_page)
cdecl_func(fz_free_page)

/* links */
cdecl_enum(fz_link_kind_e)
cdecl_struct(fz_link_dest_s)
cdecl_type(fz_link_dest)
cdecl_struct(fz_link_s)
cdecl_type(fz_link)
cdecl_func(mupdf_load_links)
cdecl_func(fz_drop_link)

/* outline */
cdecl_struct(fz_outline_s)
cdecl_type(fz_outline)
cdecl_func(mupdf_load_outline)
cdecl_func(fz_free_outline)

/* structured text */
cdecl_struct(fz_text_style_s)
cdecl_type(fz_text_style)

cdecl_struct(fz_text_char_s)
cdecl_type(fz_text_char)

cdecl_struct(fz_text_span_s)
cdecl_type(fz_text_span)

cdecl_struct(fz_text_line_s)
cdecl_type(fz_text_line)

cdecl_struct(fz_text_sheet_s)
cdecl_type(fz_text_sheet)
cdecl_func(mupdf_new_text_sheet)
cdecl_func(fz_free_text_sheet)

cdecl_struct(fz_text_page_s)
cdecl_type(fz_text_page)
cdecl_func(mupdf_new_text_page)
cdecl_func(fz_free_text_page)

cdecl_struct(fz_page_block_s)
cdecl_type(fz_page_block)

cdecl_struct(fz_text_block_s)
cdecl_type(fz_text_block)

cdecl_func(fz_text_char_bbox)

/* pixmaps */
cdecl_struct(fz_pixmap_s)
cdecl_type(fz_pixmap)

cdecl_func(fz_new_context_imp)
cdecl_func(fz_new_pixmap)
cdecl_func(mupdf_new_pixmap)
cdecl_func(fz_new_pixmap) // compat
cdecl_func(mupdf_new_pixmap_with_bbox)
cdecl_func(mupdf_new_pixmap_with_data)
cdecl_func(mupdf_new_pixmap_with_bbox_and_data)
cdecl_func(fz_convert_pixmap)
cdecl_func(fz_keep_pixmap)
cdecl_func(fz_drop_pixmap)
cdecl_func(fz_clear_pixmap_with_value)
cdecl_func(fz_gamma_pixmap)
cdecl_func(fz_pixmap_width)
cdecl_func(fz_pixmap_height)
cdecl_func(fz_pixmap_components)
cdecl_func(fz_pixmap_samples)

cdecl_func(fz_device_gray)
cdecl_func(fz_free_context)
cdecl_func(fz_device_rgb)

cdecl_func(fz_load_png)
/* device, rendering */
cdecl_func(mupdf_new_draw_device)
cdecl_func(mupdf_new_text_device)
cdecl_func(mupdf_new_bbox_device)
cdecl_func(mupdf_run_page)
cdecl_func(fz_free_device)

/* pdf specifics */
cdecl_struct(pdf_hotspot_s)
cdecl_type(pdf_hotspot)
cdecl_struct(pdf_lexbuf_s)
cdecl_type(pdf_lexbuf)
cdecl_struct(pdf_lexbuf_large_s)
cdecl_type(pdf_lexbuf_large)
cdecl_struct(pdf_annot_s)
cdecl_type(pdf_annot)
cdecl_struct(pdf_document_s)
cdecl_type(pdf_document)
cdecl_func(pdf_specifics)
cdecl_func(mupdf_pdf_create_annot)
cdecl_func(mupdf_pdf_set_markup_annot_quadpoints)
cdecl_func(mupdf_pdf_set_markup_appearance)

/* saving documents */
cdecl_struct(fz_write_options_s)
cdecl_type(fz_write_options)
cdecl_func(mupdf_write_document)

/* the following is for our own wrapper lib: */
cdecl_func(mupdf_get_my_alloc_context)
cdecl_func(mupdf_get_cache_size)
cdecl_func(mupdf_error_code)
cdecl_func(mupdf_error_message)
18 changes: 18 additions & 0 deletions ffi-cdecl/pthread_cdecl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <pthread.h>

#include "ffi-cdecl.h"
#include "ffi-cdecl-luajit.h"

cdecl_type(pthread_t)

cdecl_type(pthread_attr_t)
cdecl_union(pthread_attr_t)

cdecl_func(pthread_attr_init)
cdecl_func(pthread_attr_setdetachstate)
cdecl_func(pthread_attr_destroy)

cdecl_const(PTHREAD_CREATE_DETACHED)

cdecl_func(pthread_create)

17 changes: 8 additions & 9 deletions ffi/koptcontext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ else
k2pdfopt = ffi.load("libs/libk2pdfopt.so.2")
end

local KOPTContext = {}
local KOPTContext = {
k2pdfopt = k2pdfopt -- offer the libraries' functions to other users
}
local KOPTContext_mt = {__index={}}

local __VERSION__ = "1.0.0"
Expand Down Expand Up @@ -60,17 +62,14 @@ function KOPTContext_mt.__index:copyDestBMP(src)
end

function KOPTContext_mt.__index:dstToBlitBuffer()
local bb
if self.dst.bpp == 8 then
local bb = Blitbuffer.new(self.dst.width, self.dst.height)
for y = 0, self.dst.height - 1 do
for x = 0, self.dst.width - 1 do
local val = bit.rshift(self.dst.data[y*self.dst.width + x], 4)
bb:setPixel(x, y, Blitbuffer.Color4(val))
end
end
bb = Blitbuffer.new(self.dst.width, self.dst.height, Blitbuffer.TYPE_BB8, self.dst.data):copy()
bb:invert()
return bb
elseif self.dst.bpp == 24 then
bb = Blitbuffer.new(self.dst.width, self.dst.height, Blitbuffer.TYPE_BBRGB24, self.dst.data):copy()
end
return bb
end

function KOPTContext_mt.__index:getWordBoxes(bmp, x, y, w, h, box_type)
Expand Down

0 comments on commit 1c0cf89

Please sign in to comment.