Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow working with PDF annotations #1327

Merged
merged 2 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ffi-cdecl/mupdf_decl.c
Expand Up @@ -181,6 +181,11 @@ cdecl_struct(pdf_document_s)
cdecl_func(pdf_specifics)
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)

Expand Down
29 changes: 29 additions & 0 deletions ffi/mupdf.lua
Expand Up @@ -676,6 +676,35 @@ function page_mt.__index:addMarkupAnnotation(points, n, type)
if ok == nil then merror("could not set markup appearance") end
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
5 changes: 5 additions & 0 deletions ffi/mupdf_h.lua
Expand Up @@ -418,6 +418,11 @@ struct pdf_document_s {
pdf_document *pdf_specifics(fz_context *, fz_document *);
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;
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