forked from koreader/koreader-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmupdf.lua
1049 lines (896 loc) · 35.1 KB
/
mupdf.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[--
MuPDF API
This is a FFI wrapper for what was a Lua-based API in the past
Some kind of C wrapper is needed for muPDF since muPDF uses
a setjmp/longjmp based approach to error/exception handling.
That's one of the very few things we can't deal with using
LuaJIT's FFI.
@module ffi.mupdf
--]]
local ffi = require("ffi")
require("ffi/mupdf_h")
require("ffi/posix_h") -- for malloc
local BlitBuffer = require("ffi/blitbuffer")
local C = ffi.C
local W = ffi.loadlib("wrap-mupdf")
local M = W
--- @fixme: Don't make cache_size too low, at least not until we bump MµPDF,
--- as there's a pernicious issue that corrupts its store cache on overcommit on old versions.
--- c.f., https://github.com/koreader/koreader/issues/7627
--- (FZ_STORE_DEFAULT is 256MB, we used to set it to 8MB).
--- And when we bump MµPDF, it'll likely have *more* stuff to store in there,
-- so, don't make that too low, period ;).
-- NOTE: Revisit when we bump MµPDF by doing a few tests with the tracing memory allocators,
-- as even 32MB is likely to be too conservative.
local mupdf = {
debug_memory = false,
cache_size = 32*1024*1024,
}
-- this cannot get adapted by the cdecl file because it is a
-- string constant. Must match the actual mupdf API:
local FZ_VERSION = "1.25.2"
local document_mt = { __index = {} }
local page_mt = { __index = {} }
mupdf.debug = function() --[[ no debugging by default ]] end
local function drop_context(ctx)
local refcount = ffi.cast("int *", M.fz_user_context(ctx))
refcount[0] = refcount[0] - 1
if refcount[0] == 0 then
M.fz_drop_context(ctx)
C.free(refcount)
end
end
local function keep_context(ctx)
local refcount = ffi.cast("int *", M.fz_user_context(ctx))
refcount[0] = refcount[0] + 1
return ctx
end
local save_ctx = setmetatable({}, {__mode="kv"})
-- provides an fz_context for mupdf
local function context()
local ctx = save_ctx[1]
if ctx then return ctx end
ctx = M.fz_new_context_imp(
mupdf.debug_memory and W.mupdf_get_my_alloc_context() or nil,
nil,
mupdf.cache_size, FZ_VERSION)
if ctx == nil then
error("cannot create fz_context for MuPDF")
end
local refcount = ffi.cast("int *", C.malloc(ffi.sizeof("int")))
M.fz_set_user_context(ctx, refcount)
refcount[0] = 1
-- ctx is a cdata<fz_context *>, attach a finalizer to it to release ressources on garbage collection
ctx = ffi.gc(ctx, drop_context)
M.fz_install_external_font_funcs(ctx)
M.fz_register_document_handlers(ctx)
save_ctx[1] = ctx
return ctx
end
-- a wrapper for mupdf exception error messages
local function merror(ctx, message)
error(string.format("%s: %s (%d)", message,
ffi.string(W.mupdf_error_message(ctx)),
W.mupdf_error_code(ctx)))
end
local function drop_document(ctx, doc)
-- Clear the cdata finalizer to avoid a double-free
ffi.gc(doc, nil)
M.fz_drop_document(ctx, doc)
drop_context(ctx)
end
local function drop_page(ctx, page)
-- Clear the cdata finalizer to avoid a double-free
ffi.gc(page, nil)
M.fz_drop_page(ctx, page)
drop_context(ctx)
end
--[[--
Opens a document.
--]]
function mupdf.openDocument(filename)
local ctx = context()
local mupdf_doc = {
doc = W.mupdf_open_document(ctx, filename),
filename = filename,
}
if mupdf_doc.doc == nil then
merror(ctx, "MuPDF cannot open file.")
end
-- doc is a cdata<fz_document *>, attach a finalizer to it to release ressources on garbage collection
mupdf_doc.doc = ffi.gc(mupdf_doc.doc, function(doc) drop_document(ctx, doc) end)
mupdf_doc.ctx = keep_context(ctx)
setmetatable(mupdf_doc, document_mt)
if mupdf_doc:getPages() <= 0 then
merror(ctx, "MuPDF found no pages in file.")
end
return mupdf_doc
end
function mupdf.openDocumentFromText(text, magic, html_resource_directory)
local ctx = context()
local stream = W.mupdf_open_memory(ctx, ffi.cast("const unsigned char*", text), #text)
local archive = nil
if html_resource_directory ~= nil then
archive = W.mupdf_open_directory(ctx, html_resource_directory)
end
local mupdf_doc = {
doc = W.mupdf_open_document_with_stream_and_dir(ctx, magic, stream, archive),
}
W.mupdf_drop_stream(ctx, stream)
if archive ~= nil then
W.mupdf_drop_archive(ctx, archive)
end
if mupdf_doc.doc == nil then
merror(ctx, "MuPDF cannot open document from text")
end
-- doc is a cdata<fz_document *>, attach a finalizer to it to release ressources on garbage collection
mupdf_doc.doc = ffi.gc(mupdf_doc.doc, function(doc) drop_document(ctx, doc) end)
mupdf_doc.ctx = keep_context(ctx)
setmetatable(mupdf_doc, document_mt)
return mupdf_doc
end
-- Document functions:
--[[
close the document
this is done automatically by the garbage collector but can be
triggered explicitly
--]]
function document_mt.__index:close()
if self.doc ~= nil then
drop_document(self.ctx, self.doc)
self.doc = nil
self.ctx = nil
end
end
--[[
check if the document needs a password for access
--]]
function document_mt.__index:needsPassword()
return M.fz_needs_password(self.ctx, self.doc) ~= 0
end
--[[
try to authenticate with a password
--]]
function document_mt.__index:authenticatePassword(password)
if M.fz_authenticate_password(self.ctx, self.doc, password) == 0 then
return false
end
return true
end
--[[
read number of pages in document
--]]
function document_mt.__index:getPages()
-- cache number of pages
if self.number_of_pages then return self.number_of_pages end
local pages = W.mupdf_count_pages(self.ctx, self.doc)
if pages == -1 then
merror(self.ctx, "cannot access page tree")
end
self.number_of_pages = pages
return pages
end
function document_mt.__index:isDocumentReflowable()
if self.is_reflowable then return self.is_reflowable end
self.is_reflowable = M.fz_is_document_reflowable(self.ctx, self.doc) == 1
return self.is_reflowable
end
function document_mt.__index:layoutDocument(width, height, em)
-- Reset the cache.
self.number_of_pages = nil
W.mupdf_layout_document(self.ctx, self.doc, width, height, em)
end
function document_mt.__index:setColorRendering(color)
self.color = color
end
local function toc_walker(toc, outline, depth)
while outline ~= nil do
table.insert(toc, {
page = outline.page.page + 1,
title = ffi.string(outline.title),
depth = depth,
})
if outline.down then
toc_walker(toc, outline.down, depth+1)
end
outline = outline.next
end
end
--[[
read table of contents (ToC)
Returns a table like this:
{
{page=12, depth=1, title="chapter1"},
{page=54, depth=1, title="chapter2"},
}
Returns an empty table when there is no ToC
--]]
function document_mt.__index:getToc()
local toc = {}
local outline = W.mupdf_load_outline(self.ctx, self.doc)
if outline ~= nil then
toc_walker(toc, outline, 1)
M.fz_drop_outline(self.ctx, outline)
end
return toc
end
--[[
open a page, return page object
--]]
function document_mt.__index:openPage(number)
local ctx = self.ctx
local mupdf_page = {
page = W.mupdf_load_page(ctx, self.doc, number-1),
number = number,
doc = self,
}
if mupdf_page.page == nil then
merror(ctx, "cannot open page #" .. number)
end
-- page is a cdata<fz_page *>, attach a finalizer to it to release ressources on garbage collection
mupdf_page.page = ffi.gc(mupdf_page.page, function(page) drop_page(ctx, page) end)
mupdf_page.ctx = keep_context(ctx)
setmetatable(mupdf_page, page_mt)
return mupdf_page
end
local function getMetadataInfo(ctx, doc, info)
local bufsize = 255
local buf = ffi.new("char[?]", bufsize)
-- `fz_lookup_metadata` return the number of bytes needed
-- to store the string, **including** the null terminator.
local res = M.fz_lookup_metadata(ctx, doc, info, buf, bufsize)
if res > bufsize then
-- Buffer was too small.
bufsize = res
buf = ffi.new("char[?]", bufsize)
res = M.fz_lookup_metadata(ctx, doc, info, buf, bufsize)
end
if res > 1 then
-- Note: strip the null terminator.
return ffi.string(buf, res - 1)
end
-- Empty string or error (-1).
return ""
end
--[[
Get metadata, return object
--]]
function document_mt.__index:getMetadata()
local metadata = {
title = getMetadataInfo(self.ctx, self.doc, "info:Title"),
author = getMetadataInfo(self.ctx, self.doc, "info:Author"),
subject = getMetadataInfo(self.ctx, self.doc, "info:Subject"),
keywords = getMetadataInfo(self.ctx, self.doc, "info:Keywords"),
creator = getMetadataInfo(self.ctx, self.doc, "info:Creator"),
producer = getMetadataInfo(self.ctx, self.doc, "info:Producer"),
creationDate = getMetadataInfo(self.ctx, self.doc, "info:CreationDate"),
modDate = getMetadataInfo(self.ctx, self.doc, "info:ModDate")
}
return metadata
end
--[[
return currently claimed memory by MuPDF
This will return sensible values only when the debug_memory flag is set
--]]
function document_mt.__index:getCacheSize()
if mupdf.debug_memory then
return W.mupdf_get_cache_size()
else
return 0
end
end
function document_mt.__index:cleanCache()
-- NOP, just for API compatibility
end
--[[
write the document to a new file
--]]
function document_mt.__index:writeDocument(filename)
local opts = ffi.new("pdf_write_options[1]")
opts[0].do_incremental = (filename == self.filename ) and 1 or 0
opts[0].do_ascii = 0
opts[0].do_garbage = 0
opts[0].do_linear = 0
local ok = W.mupdf_pdf_save_document(self.ctx, ffi.cast("pdf_document*", self.doc), filename, opts)
if ok == nil then merror(self.ctx, "could not write document") end
end
-- Page functions:
--[[
explicitly close the page object
this is done implicitly by garbage collection, too.
--]]
function page_mt.__index:close()
if self.page ~= nil then
drop_page(self.ctx, self.page)
self.page = nil
self.ctx = nil
end
end
--[[
calculate page size after applying DrawContext
--]]
function page_mt.__index:getSize(draw_context)
local bounds = ffi.new("fz_rect")
local ctm = ffi.new("fz_matrix")
W.mupdf_fz_scale(ctm, draw_context.zoom, draw_context.zoom)
W.mupdf_fz_pre_rotate(ctm, draw_context.rotate)
W.mupdf_fz_bound_page(self.ctx, self.page, bounds)
W.mupdf_fz_transform_rect(bounds, ctm)
-- NOTE: fz_bound_page returns an internal representation computed @ 72dpi...
-- It is often superbly mysterious, even for images,
-- so we do *NOT* want to round it right now,
-- as it would introduce rounding errors much too early in the pipeline...
-- NOTE: ReaderZooming uses it to compute the scale factor, where accuracy matters!
-- NOTE: This is also used in conjunction with getUsedBBox,
-- which also returns precise, floating point rectangles!
--[[
M.fz_round_rect(bbox, bounds)
return bbox[0].x1-bbox[0].x0, bbox[0].y1-bbox[0].y0
--]]
return bounds.x1 - bounds.x0, bounds.y1 - bounds.y0
end
--[[
check which part of the page actually contains content
--]]
function page_mt.__index:getUsedBBox()
local result = ffi.new("fz_rect")
local dev = W.mupdf_new_bbox_device(self.ctx, result)
if dev == nil then merror(self.ctx, "cannot allocate bbox_device") end
local ok = W.mupdf_run_page(self.ctx, self.page, dev, M.fz_identity, nil)
M.fz_close_device(self.ctx, dev)
M.fz_drop_device(self.ctx, dev)
if ok == nil then merror(self.ctx, "cannot calculate bbox for page") end
return result.x0, result.y0, result.x1, result.y1
end
local B = string.byte
local function is_unicode_wspace(c)
return c == 9 or -- TAB
c == 0x0a or -- HT
c == 0x0b or -- LF
c == 0x0c or -- VT
c == 0x0d or -- FF
c == 0x20 or -- CR
c == 0x85 or -- NEL
c == 0xA0 or -- No break space
c == 0x1680 or -- Ogham space mark
c == 0x180E or -- Mongolian Vowel Separator
c == 0x2000 or -- En quad
c == 0x2001 or -- Em quad
c == 0x2002 or -- En space
c == 0x2003 or -- Em space
c == 0x2004 or -- Three-per-Em space
c == 0x2005 or -- Four-per-Em space
c == 0x2006 or -- Five-per-Em space
c == 0x2007 or -- Figure space
c == 0x2008 or -- Punctuation space
c == 0x2009 or -- Thin space
c == 0x200A or -- Hair space
c == 0x2028 or -- Line separator
c == 0x2029 or -- Paragraph separator
c == 0x202F or -- Narrow no-break space
c == 0x205F or -- Medium mathematical space
c == 0x3000 -- Ideographic space
end
local function is_unicode_bullet(c)
-- Not all of them are strictly bullets, but will do for our usage here
return c == 0x2022 or -- Bullet
c == 0x2023 or -- Triangular bullet
c == 0x25a0 or -- Black square
c == 0x25cb or -- White circle
c == 0x25cf or -- Black circle
c == 0x25e6 or -- White bullet
c == 0x2043 or -- Hyphen bullet
c == 0x2219 or -- Bullet operator
c == 149 or -- Ascii bullet
c == B'*'
end
local function skip_starting_bullet(line)
local ch = line.first_char
local found_bullet = false
while ch ~= nil do
if is_unicode_bullet(ch.c) then
found_bullet = true
elseif not is_unicode_wspace(ch.c) then
break
end
ch = ch.next
end
if found_bullet then
return ch
else
return line.first_char
end
end
--[[
get the text of the given page
will return text in a Lua table that is modeled after
djvu.c creates this table.
note that the definition of "line" is somewhat arbitrary
here (for now)
MuPDFs API provides text as single char information
that is collected in "spans". we use a span as a "line"
in Lua output and segment spans into words by looking
for space characters.
will return an empty table if we have no text
--]]
function page_mt.__index:getPageText()
-- first, we run the page through a special device, the text_device
local text_page = W.mupdf_new_stext_page_from_page(self.ctx, self.page, nil)
if text_page == nil then merror(self.ctx, "cannot alloc text_page") end
-- now we analyze the data returned by the device and bring it
-- into the format we want to return
local lines = {}
local size = 0
local block = text_page.first_block
while block ~= nil do
if block.type == M.FZ_STEXT_BLOCK_TEXT then
-- a block contains lines, which is our primary return datum
local mupdf_line = block.u.t.first_line
while mupdf_line ~= nil do
local line = {}
local line_bbox = ffi.new("fz_rect", M.fz_empty_rect)
local first_char = skip_starting_bullet( mupdf_line )
local ch = first_char
local ch_len = 0
while ch ~= nil do
ch = ch.next
ch_len = ch_len + 1
end
if ch_len > 0 then
-- here we will collect UTF-8 chars before making them
-- a Lua string:
local textbuf = ffi.new("char[?]", ch_len * 4)
ch = first_char
while ch ~= nil do
local textlen = 0
local word_bbox = ffi.new("fz_rect", M.fz_empty_rect)
while ch ~= nil do
if is_unicode_wspace(ch.c) then
-- ignore and end word
break
end
textlen = textlen + M.fz_runetochar(textbuf + textlen, ch.c)
local char_bbox = ffi.new("fz_rect")
W.mupdf_fz_rect_from_quad(char_bbox, ch.quad)
W.mupdf_fz_union_rect(word_bbox, char_bbox)
W.mupdf_fz_union_rect(line_bbox, char_bbox)
if ch.c >= 0x4e00 and ch.c <= 0x9FFF or -- CJK Unified Ideographs
ch.c >= 0x2000 and ch.c <= 0x206F or -- General Punctuation
ch.c >= 0x3000 and ch.c <= 0x303F or -- CJK Symbols and Punctuation
ch.c >= 0x3400 and ch.c <= 0x4DBF or -- CJK Unified Ideographs Extension A
ch.c >= 0xF900 and ch.c <= 0xFAFF or -- CJK Compatibility Ideographs
ch.c >= 0xFF01 and ch.c <= 0xFFEE or -- Halfwidth and Fullwidth Forms
ch.c >= 0x20000 and ch.c <= 0x2A6DF -- CJK Unified Ideographs Extension B
then
-- end word
break
end
ch = ch.next
end
-- add word to line
if word_bbox.x0 < word_bbox.x1 and word_bbox.y0 < word_bbox.y1 then
table.insert(line, {
word = ffi.string(textbuf, textlen),
x0 = word_bbox.x0, y0 = word_bbox.y0,
x1 = word_bbox.x1, y1 = word_bbox.y1,
})
size = size + 5 * 8 + textlen
end
if ch == nil then
break
end
ch = ch.next
end
if line_bbox.x0 < line_bbox.x1 and line_bbox.y0 < line_bbox.y1 then
line.x0, line.y0 = line_bbox.x0, line_bbox.y0
line.x1, line.y1 = line_bbox.x1, line_bbox.y1
size = size + 5 * 8
table.insert(lines, line)
end
end
mupdf_line = mupdf_line.next
end
end
block = block.next
end
-- Rough approximation of size for caching
lines.size = size
M.fz_drop_stext_page(self.ctx, text_page)
return lines
end
--[[
Get a list of the Hyperlinks on a page
--]]
function page_mt.__index:getPageLinks()
local page_links = W.mupdf_load_links(self.ctx, self.page)
-- do not error out when page_links == NULL, since there might
-- simply be no links present.
local links = {}
local link = page_links
while link ~= nil do
local data = {
x0 = link.rect.x0, y0 = link.rect.y0,
x1 = link.rect.x1, y1 = link.rect.y1,
}
local pos = ffi.new("float[2]")
local location = ffi.new("fz_location")
W.mupdf_fz_resolve_link(self.ctx, self.doc.doc, link.uri, pos, pos+1, location)
-- `fz_resolve_link` return a location of (-1, -1) for external links.
if location.chapter == -1 and location.page == -1 then
data.uri = ffi.string(link.uri)
else
data.page = W.mupdf_fz_page_number_from_location(self.ctx, self.doc.doc, location)
end
data.pos = {
x = pos[0], y = pos[1],
}
table.insert(links, data)
link = link.next
end
M.fz_drop_link(self.ctx, page_links)
return links
end
local function run_page(page, pixmap, ctm)
M.fz_clear_pixmap_with_value(page.ctx, pixmap, 0xff)
local dev = W.mupdf_new_draw_device(page.ctx, nil, pixmap)
if dev == nil then merror(page.ctx, "cannot create draw device") end
local ok = W.mupdf_run_page(page.ctx, page.page, dev, ctm, nil)
M.fz_close_device(page.ctx, dev)
M.fz_drop_device(page.ctx, dev)
if ok == nil then merror(page.ctx, "could not run page") end
end
--[[
render page to blitbuffer
old interface: expects a blitbuffer to render to
--]]
function page_mt.__index:draw(draw_context, blitbuffer, offset_x, offset_y)
local buffer = self:draw_new(draw_context, blitbuffer:getWidth(), blitbuffer:getHeight(), offset_x, offset_y)
blitbuffer:blitFrom(buffer)
buffer:free()
end
--[[
render page to blitbuffer
new interface: creates the blitbuffer with the rendered data and returns that
TODO: make this the used interface
--]]
function page_mt.__index:draw_new(draw_context, width, height, offset_x, offset_y)
local ctm = ffi.new("fz_matrix")
W.mupdf_fz_scale(ctm, draw_context.zoom, draw_context.zoom)
W.mupdf_fz_pre_rotate(ctm, draw_context.rotate)
W.mupdf_fz_pre_translate(ctm, draw_context.offset_x, draw_context.offset_y)
local bbox = ffi.new("fz_irect", offset_x, offset_y, offset_x + width, offset_y + height)
local bb = BlitBuffer.new(width, height, self.doc.color and BlitBuffer.TYPE_BBRGB32 or BlitBuffer.TYPE_BB8)
local colorspace = self.doc.color and M.fz_device_rgb(self.ctx)
or M.fz_device_gray(self.ctx)
if mupdf.bgr and self.doc.color then
colorspace = M.fz_device_bgr(self.ctx)
end
local pix = W.mupdf_new_pixmap_with_bbox_and_data(
self.ctx, colorspace, bbox, nil, self.doc.color and 1 or 0, ffi.cast("unsigned char*", bb.data))
if pix == nil then merror(self.ctx, "cannot allocate pixmap") end
run_page(self, pix, ctm)
if draw_context.gamma >= 0.0 then
M.fz_gamma_pixmap(self.ctx, pix, draw_context.gamma)
end
M.fz_drop_pixmap(self.ctx, pix)
return bb
end
mupdf.STRIKE_HEIGHT = 0.375
mupdf.UNDERLINE_HEIGHT = 0
mupdf.LINE_THICKNESS = 0.05
mupdf.HIGHLIGHT_COLOR = {1.0, 1.0, 0.0}
mupdf.UNDERLINE_COLOR = {0.0, 0.0, 1.0}
mupdf.STRIKE_OUT_COLOR = {1.0, 0.0, 0.0}
function page_mt.__index:addMarkupAnnotation(points, n, type, bb_color)
local color = ffi.new("float[3]")
local alpha = 1.0
if type == M.PDF_ANNOT_HIGHLIGHT then
if bb_color then
color[0] = bb_color.r / 255
color[1] = bb_color.g / 255
color[2] = bb_color.b / 255
else
color[0] = mupdf.HIGHLIGHT_COLOR[1]
color[1] = mupdf.HIGHLIGHT_COLOR[2]
color[2] = mupdf.HIGHLIGHT_COLOR[3]
end
alpha = 0.5
elseif type == M.PDF_ANNOT_UNDERLINE then
if bb_color then
color[0] = bb_color.r / 255
color[1] = bb_color.g / 255
color[2] = bb_color.b / 255
else
color[0] = mupdf.UNDERLINE_COLOR[1]
color[1] = mupdf.UNDERLINE_COLOR[2]
color[2] = mupdf.UNDERLINE_COLOR[3]
end
elseif type == M.PDF_ANNOT_STRIKE_OUT then
if bb_color then
color[0] = bb_color.r / 255
color[1] = bb_color.g / 255
color[2] = bb_color.b / 255
else
color[0] = mupdf.STRIKE_OUT_COLOR[1]
color[1] = mupdf.STRIKE_OUT_COLOR[2]
color[2] = mupdf.STRIKE_OUT_COLOR[3]
end
else
return
end
local annot = W.mupdf_pdf_create_annot(self.ctx, ffi.cast("pdf_page*", self.page), type)
if annot == nil then merror(self.ctx, "could not create annotation") end
local ok = W.mupdf_pdf_set_annot_quad_points(self.ctx, annot, n, points)
if ok == nil then merror(self.ctx, "could not set annotation quadpoints") end
ok = W.mupdf_pdf_set_annot_color(self.ctx, annot, 3, color)
if ok == nil then merror(self.ctx, "could not set annotation color") end
ok = W.mupdf_pdf_set_annot_opacity(self.ctx, annot, alpha)
if ok == nil then merror(self.ctx, "could not set annotation opacity") end
-- Fetch back MuPDF's stored coordinates of all quadpoints, as they may have been modified/rounded
-- (we need the exact ones that were saved if we want to be able to find them for deletion/update)
for i = 0, n-1 do
W.mupdf_pdf_annot_quad_point(self.ctx, annot, i, points+i)
end
end
function page_mt.__index:deleteMarkupAnnotation(annot)
local ok = W.mupdf_pdf_delete_annot(self.ctx, ffi.cast("pdf_page*", self.page), annot)
if ok == nil then merror(self.ctx, "could not delete markup annotation") end
end
function page_mt.__index:getMarkupAnnotation(points, n)
local annot = W.mupdf_pdf_first_annot(self.ctx, ffi.cast("pdf_page*", self.page))
while annot ~= nil do
local n2 = W.mupdf_pdf_annot_quad_point_count(self.ctx, annot)
if n == n2 then
local quadpoint = ffi.new("fz_quad[1]")
local match = true
for i = 0, n-1 do
W.mupdf_pdf_annot_quad_point(self.ctx, annot, i, quadpoint)
if (points[i].ul.x ~= quadpoint[0].ul.x or
points[i].ul.y ~= quadpoint[0].ul.y or
points[i].ur.x ~= quadpoint[0].ur.x or
points[i].ur.y ~= quadpoint[0].ur.y or
points[i].ll.x ~= quadpoint[0].ll.x or
points[i].ll.y ~= quadpoint[0].ll.y or
points[i].lr.x ~= quadpoint[0].lr.x or
points[i].lr.y ~= quadpoint[0].lr.y) then
match = false
break
end
end
if match then return annot end
end
annot = W.mupdf_pdf_next_annot(self.ctx, annot)
end
return nil
end
function page_mt.__index:updateMarkupAnnotation(annot, contents)
local ok = W.mupdf_pdf_set_annot_contents(self.ctx, annot, contents)
if ok == nil then merror(self.ctx, "could not update markup annot contents") end
end
-- image loading via MuPDF:
--[[--
Renders image data.
--]]
function mupdf.renderImage(data, size, width, height)
local ctx = context()
local buffer = W.mupdf_new_buffer_from_shared_data(ctx,
ffi.cast("unsigned char*", data), size)
local image = W.mupdf_new_image_from_buffer(ctx, buffer)
W.mupdf_drop_buffer(ctx, buffer)
if image == nil then merror(ctx, "could not load image data") end
local pixmap = W.mupdf_get_pixmap_from_image(ctx,
image, nil, nil, nil, nil)
M.fz_drop_image(ctx, image)
if pixmap == nil then
merror(ctx, "could not create pixmap from image")
end
local p_width = M.fz_pixmap_width(ctx, pixmap)
local p_height = M.fz_pixmap_height(ctx, pixmap)
-- mupdf_get_pixmap_from_image() may not scale image to the
-- width and height provided, so check and scale it if needed
if width and height then
-- Ensure we pass integer values for width & height to fz_scale_pixmap(),
-- because it enforces an alpha channel otherwise...
width = math.floor(width)
height = math.floor(height)
if p_width ~= width or p_height ~= height then
local scaled_pixmap = M.fz_scale_pixmap(ctx, pixmap, 0, 0, width, height, nil)
M.fz_drop_pixmap(ctx, pixmap)
if scaled_pixmap == nil then
merror(ctx, "could not create scaled pixmap from pixmap")
end
pixmap = scaled_pixmap
p_width = M.fz_pixmap_width(ctx, pixmap)
p_height = M.fz_pixmap_height(ctx, pixmap)
end
end
local bbtype
local ncomp = M.fz_pixmap_components(ctx, pixmap)
if ncomp == 1 then bbtype = BlitBuffer.TYPE_BB8
elseif ncomp == 2 then bbtype = BlitBuffer.TYPE_BB8A
elseif ncomp == 3 then bbtype = BlitBuffer.TYPE_BBRGB24
elseif ncomp == 4 then bbtype = BlitBuffer.TYPE_BBRGB32
else error("unsupported number of color components")
end
-- Handle RGB->BGR conversion for Kobos when needed
local bb
if mupdf.bgr and ncomp >= 3 then
local bgr_pixmap = W.mupdf_convert_pixmap(ctx, pixmap, M.fz_device_bgr(ctx), nil, nil, M.fz_default_color_params, (ncomp == 4 and 1 or 0))
if pixmap == nil then
merror(ctx, "could not convert pixmap to BGR")
end
M.fz_drop_pixmap(ctx, pixmap)
local p = M.fz_pixmap_samples(ctx, bgr_pixmap)
bb = BlitBuffer.new(p_width, p_height, bbtype, p):copy()
M.fz_drop_pixmap(ctx, bgr_pixmap)
else
local p = M.fz_pixmap_samples(ctx, pixmap)
bb = BlitBuffer.new(p_width, p_height, bbtype, p):copy()
M.fz_drop_pixmap(ctx, pixmap)
end
return bb
end
--- Renders image file.
function mupdf.renderImageFile(filename, width, height)
local file = io.open(filename, "rb")
if not file then error("could not open image file") end
local data = file:read("*a")
file:close()
return mupdf.renderImage(data, #data, width, height)
end
--[[--
Scales a blitbuffer.
MµPDF's scaling is of much better quality than the very naive implementation in blitbuffer.lua.
(see fz_scale_pixmap_cached() in mupdf/source/fitz/draw-scale-simple.c).
Same arguments as BlitBuffer:scale() for easy replacement.
Unlike BlitBuffer:scale(), this *ignores* the blitbuffer's rotation
(i.e., where possible, we simply wrap the BlitBuffer's data in a fitz pixmap,
with no data copy, so the buffer's *native* memory layout is followed).
If you actually want to preserve the rotation, you'll have to fudge
with the width & height arguments and tweak the returned buffer's rotation flag,
or go through a temporary copy to ensure that the buffer's memory is laid out accordingly.
--]]
function mupdf.scaleBlitBuffer(bb, width, height)
-- We need first to convert our BlitBuffer to a pixmap
local bbtype = bb:getType()
local colorspace
local converted_bb
local alpha
local stride = bb.stride
local ctx = context()
-- MuPDF should know how to handle *most* of our BB types,
-- special snowflakes excluded (4bpp & RGB565),
-- in which case we feed it a temporary copy in the closest format it'll understand.
if bbtype == BlitBuffer.TYPE_BB8 then
colorspace = M.fz_device_gray(ctx)
alpha = 0
elseif bbtype == BlitBuffer.TYPE_BB8A then
colorspace = M.fz_device_gray(ctx)
alpha = 1
elseif bbtype == BlitBuffer.TYPE_BBRGB24 then
if mupdf.bgr then
colorspace = M.fz_device_bgr(ctx)
else
colorspace = M.fz_device_rgb(ctx)
end
alpha = 0
elseif bbtype == BlitBuffer.TYPE_BBRGB32 then
if mupdf.bgr then
colorspace = M.fz_device_bgr(ctx)
else
colorspace = M.fz_device_rgb(ctx)
end
alpha = 1
elseif bbtype == BlitBuffer.TYPE_BB4 then
converted_bb = BlitBuffer.new(bb.w, bb.h, BlitBuffer.TYPE_BB8)
converted_bb:blitFrom(bb, 0, 0, 0, 0, bb.w, bb.h)
bb = converted_bb -- we don't free() the provided bb, but we'll have to free our converted_bb
colorspace = M.fz_device_gray(ctx)
alpha = 0
stride = bb.w
else
converted_bb = BlitBuffer.new(bb.w, bb.h, BlitBuffer.TYPE_BBRGB32)
converted_bb:blitFrom(bb, 0, 0, 0, 0, bb.w, bb.h)
bb = converted_bb -- we don't free() the provided bb, but we'll have to free our converted_bb
if mupdf.bgr then
colorspace = M.fz_device_bgr(ctx)
else
colorspace = M.fz_device_rgb(ctx)
end
alpha = 1
end
-- We can now create a pixmap from this bb of correct type
local pixmap = W.mupdf_new_pixmap_with_data(ctx, colorspace,
bb.w, bb.h, nil, alpha, stride, ffi.cast("unsigned char*", bb.data))
if pixmap == nil then
if converted_bb then converted_bb:free() end -- free our home made bb
merror(ctx, "could not create pixmap from blitbuffer")
end
-- We can now scale the pixmap
-- Better to ensure we give integer width and height, to avoid a black 1-pixel line at right and bottom of image.
-- Also, fz_scale_pixmap enforces an alpha channel if w or h are floats...
local scaled_pixmap = M.fz_scale_pixmap(ctx, pixmap, 0, 0, math.floor(width), math.floor(height), nil)
M.fz_drop_pixmap(ctx, pixmap) -- free our original pixmap
if scaled_pixmap == nil then
if converted_bb then converted_bb:free() end -- free our home made bb
merror(ctx, "could not create scaled pixmap from pixmap")
end
local p_width = M.fz_pixmap_width(ctx, scaled_pixmap)
local p_height = M.fz_pixmap_height(ctx, scaled_pixmap)
-- And convert the pixmap back to a BlitBuffer
bbtype = nil
local ncomp = M.fz_pixmap_components(ctx, scaled_pixmap)
if ncomp == 1 then bbtype = BlitBuffer.TYPE_BB8
elseif ncomp == 2 then bbtype = BlitBuffer.TYPE_BB8A
elseif ncomp == 3 then bbtype = BlitBuffer.TYPE_BBRGB24
elseif ncomp == 4 then bbtype = BlitBuffer.TYPE_BBRGB32
else
if converted_bb then converted_bb:free() end -- free our home made bb
error("unsupported number of color components")
end
local p = M.fz_pixmap_samples(ctx, scaled_pixmap)
bb = BlitBuffer.new(p_width, p_height, bbtype, p):copy()
M.fz_drop_pixmap(ctx, scaled_pixmap) -- free our scaled pixmap
if converted_bb then converted_bb:free() end -- free our home made bb
return bb
end
-- k2pdfopt interfacing
-- will lazily load ffi/koptcontext.lua in order to interface k2pdfopt
local cached_k2pdfopt
local function get_k2pdfopt()
if cached_k2pdfopt then return cached_k2pdfopt end
local koptcontext = require("ffi/koptcontext")
cached_k2pdfopt = koptcontext.k2pdfopt
return cached_k2pdfopt
end
--[[
the following function is a reimplementation of what can be found
in libk2pdfopt/willuslib/bmpmupdf.c
k2pdfopt supports only 8bit and 24bit "bitmaps" - and mupdf will give
only 8bit+8bit alpha or 24bit+8bit alpha pixmaps. So we need to convert
what we get from mupdf.
--]]
local function bmpmupdf_pixmap_to_bmp(bmp, pixmap)
local k2pdfopt = get_k2pdfopt()
local ctx = context()
bmp.width = M.fz_pixmap_width(ctx, pixmap)
bmp.height = M.fz_pixmap_height(ctx, pixmap)
local ncomp = M.fz_pixmap_components(ctx, pixmap)
local p = M.fz_pixmap_samples(ctx, pixmap)