Skip to content

Commit

Permalink
Allow working with PDF annotations (koreader#1327)
Browse files Browse the repository at this point in the history
This exposes mupdf functions for searching PDF annotations and changing their contents.
  • Loading branch information
Toromtomtom authored and roygbyte committed Mar 3, 2022
1 parent 3f2fcf0 commit 325fd41
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
20 changes: 9 additions & 11 deletions ffi-cdecl/mupdf_decl.c
Expand Up @@ -182,17 +182,15 @@ cdecl_type(pdf_page)
cdecl_type(pdf_document)
//cdecl_struct(pdf_document_s)
cdecl_func(pdf_specifics)
cdecl_func(pdf_create_annot)
cdecl_func(pdf_create_annot_raw)
cdecl_func(pdf_add_annot_quad_point)
cdecl_func(pdf_set_annot_color)
cdecl_func(pdf_set_annot_opacity)
cdecl_func(pdf_update_appearance)

//cdecl_func(pdf_set_text_annot_position)
//cdecl_func(pdf_set_annot_rect)
//cdecl_func(pdf_set_markup_appearance)
cdecl_func(pdf_update_annot)
cdecl_func(mupdf_pdf_create_annot)
cdecl_func(mupdf_pdf_set_annot_quad_points)
cdecl_func(mupdf_pdf_set_annot_contents)
cdecl_func(mupdf_pdf_first_annot)
cdecl_func(mupdf_pdf_next_annot)
cdecl_func(mupdf_pdf_annot_quad_point_count)
cdecl_func(mupdf_pdf_annot_quad_point)
cdecl_func(mupdf_pdf_set_text_annot_position)
cdecl_func(mupdf_pdf_set_markup_appearance)

/* saving documents */
cdecl_type(pdf_write_options)
Expand Down
29 changes: 29 additions & 0 deletions ffi/mupdf.lua
Expand Up @@ -662,6 +662,35 @@ function page_mt.__index:addMarkupAnnotation(points, n, type)
self.ctx:pdf_update_appearance(annot)
end
function page_mt.__index:getMarkupAnnotation(points, n)
local doc = M.pdf_specifics(context(), self.doc.doc)
if doc == nil then merror("could not get pdf_specifics") end
local annot = W.mupdf_pdf_first_annot(context(), ffi.cast("pdf_page*", self.page))
while annot ~= nil do
local n2 = W.mupdf_pdf_annot_quad_point_count(context(), annot)
if n == n2 then
local quadpoint = ffi.new("float[?]", 8)
local match = true
for i = 0, n-1 do
W.mupdf_pdf_annot_quad_point(context(), annot, i, quadpoint)
for k = 0, 7 do
if points[i*8 + k] ~= quadpoint[k] then match = false end
end
end
if match then return annot end
end
annot = W.mupdf_pdf_next_annot(context(), annot)
end
return nil
end
function page_mt.__index:updateMarkupAnnotation(annot, contents)
local doc = M.pdf_specifics(context(), self.doc.doc)
if doc == nil then merror("could not get pdf_specifics") end
local ok = W.mupdf_pdf_set_annot_contents(context(), annot, contents)
if ok == nil then merror("could not update markup annot contents") end
end
-- image loading via MuPDF:
Expand Down
19 changes: 11 additions & 8 deletions ffi/mupdf_h.lua
Expand Up @@ -306,14 +306,17 @@ typedef struct pdf_annot pdf_annot;
typedef struct pdf_page pdf_page;
typedef struct pdf_document pdf_document;
pdf_document *pdf_specifics(fz_context *, fz_document *);
pdf_annot *pdf_create_annot(fz_context *, pdf_page *, enum pdf_annot_type);
pdf_annot *pdf_create_annot_raw(fz_context *, pdf_page *, enum pdf_annot_type);
void pdf_add_annot_quad_point(fz_context *, pdf_annot *, fz_quad);
void pdf_set_annot_color(fz_context *, pdf_annot *, int, const float *);
void pdf_set_annot_opacity(fz_context *, pdf_annot *, float);
void pdf_update_appearance(fz_context *, pdf_annot *);
int pdf_update_annot(fz_context *, pdf_annot *);
typedef struct {
pdf_annot *mupdf_pdf_create_annot(fz_context *, pdf_page *, enum pdf_annot_type);
void *mupdf_pdf_set_annot_quad_points(fz_context *, pdf_annot *, int, const float *);
void *mupdf_pdf_set_annot_contents(fz_context *, pdf_annot *, const char *);
pdf_annot *mupdf_pdf_first_annot(fz_context *, pdf_page *);
pdf_annot *mupdf_pdf_next_annot(fz_context *, pdf_annot *);
int mupdf_pdf_annot_quad_point_count(fz_context *, pdf_annot *);
void *mupdf_pdf_annot_quad_point(fz_context *, pdf_annot *, int, float *);
void *mupdf_pdf_set_text_annot_position(fz_context *, pdf_annot *, fz_point);
void *mupdf_pdf_set_markup_appearance(fz_context *, pdf_document *, pdf_annot *, float *, float, float, float);
typedef struct pdf_write_options_s pdf_write_options;
struct pdf_write_options_s {
int do_incremental;
int do_pretty;
int do_ascii;
Expand Down
15 changes: 15 additions & 0 deletions wrap-mupdf.h
Expand Up @@ -124,6 +124,21 @@ MUPDF_WRAP(mupdf_pdf_create_annot, pdf_annot*, NULL,
MUPDF_WRAP(mupdf_pdf_set_annot_quad_points, void*, NULL,
{ pdf_set_annot_quad_points(ctx, annot, n, v); ret = (void*) -1; },
pdf_annot *annot, int n, const float *v)
MUPDF_WRAP(mupdf_pdf_set_annot_contents, void*, NULL,
{ pdf_set_annot_contents(ctx, annot, text); ret = (void*) -1; },
pdf_annot *annot, const char *text)
MUPDF_WRAP(mupdf_pdf_first_annot, pdf_annot*, NULL,
ret = pdf_first_annot(ctx, page),
pdf_page *page)
MUPDF_WRAP(mupdf_pdf_next_annot, pdf_annot*, NULL,
ret = pdf_next_annot(ctx, annot),
pdf_annot *annot)
MUPDF_WRAP(mupdf_pdf_annot_quad_point_count, int, -1,
ret = pdf_annot_quad_point_count(ctx, annot),
pdf_annot *annot)
MUPDF_WRAP(mupdf_pdf_annot_quad_point, void*, NULL,
{ pdf_annot_quad_point(ctx, annot, i, qp); ret = (void*) -1; },
pdf_annot *annot, int i, float qp[8])
MUPDF_WRAP(mupdf_pdf_set_text_annot_position, void*, NULL,
{ pdf_set_text_annot_position(ctx, annot, pt); ret = (void*) -1; },
pdf_annot *annot, fz_point pt)
Expand Down

0 comments on commit 325fd41

Please sign in to comment.