Skip to content

Commit 7b5013e

Browse files
authored
mupdf: add annotation type and contents getters (#2287)
1 parent af4e241 commit 7b5013e

4 files changed

Lines changed: 33 additions & 14 deletions

File tree

ffi-cdecl/wrap-mupdf_cdecl.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ cdecl_type(pdf_page)
147147
cdecl_type(pdf_document)
148148

149149
/* annotations */
150+
cdecl_func(mupdf_pdf_annot_type)
151+
cdecl_func(mupdf_pdf_annot_contents)
150152
cdecl_func(mupdf_pdf_create_annot)
151153
cdecl_func(mupdf_pdf_delete_annot)
152154
cdecl_func(mupdf_pdf_set_annot_quad_points)

ffi/mupdf.lua

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -830,26 +830,35 @@ function page_mt.__index:updateMarkupAnnotation(annot, contents)
830830
if ok == nil then merror(self.ctx, "could not update markup annot contents") end
831831
end
832832

833-
function page_mt.__index:getMarkupAnnotationBoxesFromPage()
834-
local boxes = {}
833+
function page_mt.__index:getEmbeddedAnnotations()
834+
local annotations = {}
835835
local annot = W.mupdf_pdf_first_annot(self.ctx, ffi.cast("pdf_page*", self.page))
836836
while annot ~= nil do
837-
local annot_boxes = {}
838-
local quadpoint = ffi.new("fz_quad[1]")
839-
local point_count = W.mupdf_pdf_annot_quad_point_count(self.ctx, annot)
840-
for i = 0, point_count - 1 do
841-
W.mupdf_pdf_annot_quad_point(self.ctx, annot, i, quadpoint)
842-
table.insert(annot_boxes, {
843-
h = quadpoint[0].ll.y - quadpoint[0].ul.y + 1,
844-
w = quadpoint[0].ur.x - quadpoint[0].ul.x + 1,
845-
x = quadpoint[0].ul.x,
846-
y = quadpoint[0].ul.y,
837+
local annot_type = W.mupdf_pdf_annot_type(self.ctx, annot)
838+
-- markup annotations: highlight=8, underline=9, squiggly=10, strikeout=11
839+
if annot_type >= 8 and annot_type <= 11 then
840+
local annot_boxes = {}
841+
local quadpoint = ffi.new("fz_quad[1]")
842+
local point_count = W.mupdf_pdf_annot_quad_point_count(self.ctx, annot)
843+
for i = 0, point_count - 1 do
844+
W.mupdf_pdf_annot_quad_point(self.ctx, annot, i, quadpoint)
845+
table.insert(annot_boxes, {
846+
h = quadpoint[0].ll.y - quadpoint[0].ul.y + 1,
847+
w = quadpoint[0].ur.x - quadpoint[0].ul.x + 1,
848+
x = quadpoint[0].ul.x,
849+
y = quadpoint[0].ul.y,
850+
})
851+
end
852+
local contents = W.mupdf_pdf_annot_contents(self.ctx, annot)
853+
table.insert(annotations, {
854+
boxes = annot_boxes,
855+
type = annot_type,
856+
contents = contents ~= nil and ffi.string(contents) or nil,
847857
})
848858
end
849-
table.insert(boxes, annot_boxes)
850859
annot = W.mupdf_pdf_next_annot(self.ctx, annot)
851860
end
852-
return next(boxes) and boxes
861+
return next(annotations) and annotations
853862
end
854863

855864
-- image loading via MuPDF:

ffi/mupdf_h.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ enum pdf_annot_type {
277277
typedef struct pdf_annot pdf_annot;
278278
typedef struct pdf_page pdf_page;
279279
typedef struct pdf_document pdf_document;
280+
int mupdf_pdf_annot_type(fz_context *, pdf_annot *);
281+
const char *mupdf_pdf_annot_contents(fz_context *, pdf_annot *);
280282
pdf_annot *mupdf_pdf_create_annot(fz_context *, pdf_page *, enum pdf_annot_type);
281283
void *mupdf_pdf_delete_annot(fz_context *, pdf_page *, pdf_annot *);
282284
void *mupdf_pdf_set_annot_quad_points(fz_context *, pdf_annot *, int, const fz_quad *);

wrap-mupdf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ MUPDF_WRAP(mupdf_fz_page_number_from_location, int, -1,
144144
MUPDF_WRAP(mupdf_fz_location_from_page_number, void *, NULL,
145145
{ *location = fz_location_from_page_number(ctx, doc, number); ret = (void*) -1; },
146146
fz_document *doc, fz_location *location, int number)
147+
MUPDF_WRAP(mupdf_pdf_annot_type, int, -1,
148+
ret = pdf_annot_type(ctx, annot),
149+
pdf_annot *annot)
150+
MUPDF_WRAP(mupdf_pdf_annot_contents, const char*, NULL,
151+
ret = pdf_annot_contents(ctx, annot),
152+
pdf_annot *annot)
147153
MUPDF_WRAP(mupdf_pdf_create_annot, pdf_annot*, NULL,
148154
ret = pdf_create_annot(ctx, page, type),
149155
pdf_page *page, enum pdf_annot_type type)

0 commit comments

Comments
 (0)