app-text/sile: restrict to lua 5.3+
[sgilles-overlay.git] / app-text / mupdf / files / mupdf-1.10a-djvu-support.patch
blob425e72a675def7084faa709f4037bdb9329f30b2
1 From ee2dd70f708ea1316fbe40298fa22b3a2b469649 Mon Sep 17 00:00:00 2001
2 From: "S. Gilles" <sgilles@math.umd.edu>
3 Date: Mon, 27 Feb 2017 01:01:44 -0500
4 Subject: [PATCH] Support DjVu files
6 The text layer is not supported. It would require modifying djvu_run_page
7 to add in all the fz_fill_text commands, as well as retrieving and parsing
8 the minilisp expressions returned by ddjvuapi.
10 If you really want it, hire me or something. Flexible rates.
11 ---
12 Makefile | 9 +-
13 Makerules | 7 ++
14 Makethird | 5 +
15 include/mupdf/fitz/config.h | 5 +
16 platform/gl/gl-win32.c | 10 +-
17 platform/x11/win_main.c | 8 +-
18 source/djvu/djvu-doc.c | 271 ++++++++++++++++++++++++++++++++++++++++++++
19 source/fitz/document-all.c | 4 +
20 8 files changed, 314 insertions(+), 5 deletions(-)
21 create mode 100644 source/djvu/djvu-doc.c
23 diff --git a/Makefile b/Makefile
24 index 85103bc..aa552a7 100644
25 --- a/Makefile
26 +++ b/Makefile
27 @@ -28,6 +28,7 @@ LIBS += $(LURATECH_LIBS)
28 LIBS += $(MUJS_LIBS)
29 LIBS += $(OPENJPEG_LIBS)
30 LIBS += $(ZLIB_LIBS)
31 +LIBS += $(DDJVU_LIBS)
33 CFLAGS += $(FREETYPE_CFLAGS)
34 CFLAGS += $(HARFBUZZ_CFLAGS)
35 @@ -39,6 +40,7 @@ CFLAGS += $(LURATECH_CFLAGS)
36 CFLAGS += $(MUJS_CFLAGS)
37 CFLAGS += $(OPENJPEG_CFLAGS)
38 CFLAGS += $(ZLIB_CFLAGS)
39 +CFLAGS += $(DDJVU_CFLAGS)
41 # --- Commands ---
43 @@ -72,6 +74,7 @@ ALL_DIR += $(OUT)/svg
44 ALL_DIR += $(OUT)/cbz
45 ALL_DIR += $(OUT)/html
46 ALL_DIR += $(OUT)/gprf
47 +ALL_DIR += $(OUT)/djvu
48 ALL_DIR += $(OUT)/tools
49 ALL_DIR += $(OUT)/platform/x11
50 ALL_DIR += $(OUT)/platform/x11/curl
51 @@ -90,6 +93,7 @@ SVG_SRC := $(wildcard source/svg/*.c)
52 CBZ_SRC := $(wildcard source/cbz/*.c)
53 HTML_SRC := $(wildcard source/html/*.c)
54 GPRF_SRC := $(wildcard source/gprf/*.c)
55 +DJVU_SRC := $(wildcard source/djvu/*.c)
57 FITZ_SRC_HDR := $(wildcard source/fitz/*.h)
58 PDF_SRC_HDR := $(wildcard source/pdf/*.h) source/pdf/pdf-name-table.h
59 @@ -97,6 +101,7 @@ XPS_SRC_HDR := $(wildcard source/xps/*.h)
60 SVG_SRC_HDR := $(wildcard source/svg/*.h)
61 HTML_SRC_HDR := $(wildcard source/html/*.h)
62 GPRF_SRC_HDR := $(wildcard source/gprf/*.h)
63 +DJVU_SRC_HDR := $(wildcard source/djvu/*.h)
65 FITZ_OBJ := $(subst source/, $(OUT)/, $(addsuffix .o, $(basename $(FITZ_SRC))))
66 PDF_OBJ := $(subst source/, $(OUT)/, $(addsuffix .o, $(basename $(PDF_SRC))))
67 @@ -105,6 +110,7 @@ SVG_OBJ := $(subst source/, $(OUT)/, $(addsuffix .o, $(basename $(SVG_SRC))))
68 CBZ_OBJ := $(subst source/, $(OUT)/, $(addsuffix .o, $(basename $(CBZ_SRC))))
69 HTML_OBJ := $(subst source/, $(OUT)/, $(addsuffix .o, $(basename $(HTML_SRC))))
70 GPRF_OBJ := $(subst source/, $(OUT)/, $(addsuffix .o, $(basename $(GPRF_SRC))))
71 +DJVU_OBJ := $(subst source/, $(OUT)/, $(addsuffix .o, $(basename $(DJVU_SRC))))
73 $(FITZ_OBJ) : $(FITZ_HDR) $(FITZ_SRC_HDR)
74 $(PDF_OBJ) : $(FITZ_HDR) $(PDF_HDR) $(PDF_SRC_HDR)
75 @@ -113,6 +119,7 @@ $(SVG_OBJ) : $(FITZ_HDR) $(SVG_HDR) $(SVG_SRC_HDR)
76 $(CBZ_OBJ) : $(FITZ_HDR)
77 $(HTML_OBJ) : $(FITZ_HDR) $(HTML_HDR) $(HTML_SRC_HDR)
78 $(GPRF_OBJ) : $(FITZ_HDR) $(GPRF_HDR) $(GPRF_SRC_HDR)
79 +$(DJVU_OBJ) : $(FITZ_HDR) $(DJVU_HDR) $(DJVU_SRC_HDR)
81 # --- Generated embedded font files ---
83 @@ -155,7 +162,7 @@ $(FONT_GEN_SIL) : $(FONT_BIN_SIL)
84 MUPDF_LIB = $(OUT)/libmupdf.so.1.10a
85 THIRD_LIB = $(OUT)/libmupdfthird.a
87 -MUPDF_OBJ := $(FITZ_OBJ) $(FONT_OBJ) $(PDF_OBJ) $(XPS_OBJ) $(SVG_OBJ) $(CBZ_OBJ) $(HTML_OBJ) $(GPRF_OBJ)
88 +MUPDF_OBJ := $(FITZ_OBJ) $(FONT_OBJ) $(PDF_OBJ) $(XPS_OBJ) $(SVG_OBJ) $(CBZ_OBJ) $(HTML_OBJ) $(GPRF_OBJ) $(DJVU_OBJ)
89 THIRD_OBJ := $(FREETYPE_OBJ) $(HARFBUZZ_OBJ) $(JBIG2DEC_OBJ) $(JPEG_OBJ) $(JPEGXR_OBJ) $(LURATECH_OBJ) $(MUJS_OBJ) $(OPENJPEG_OBJ) $(ZLIB_OBJ)
91 $(MUPDF_LIB) : $(MUPDF_OBJ)
92 diff --git a/Makerules b/Makerules
93 index b469f63..bf2bbf5 100644
94 --- a/Makerules
95 +++ b/Makerules
96 @@ -169,6 +169,13 @@ SYS_OPENJPEG_CFLAGS = $(shell pkg-config --cflags libopenjp2)
97 SYS_OPENJPEG_LIBS = $(shell pkg-config --libs libopenjp2)
98 endif
100 +ifeq "$(WANT_DJVU)" "yes"
101 +ifeq "$(shell pkg-config --exists ddjvuapi && echo yes)" "yes"
102 +SYS_DDJVU_CFLAGS = $(shell pkg-config --cflags ddjvuapi)
103 +SYS_DDJVU_LIBS = $(shell pkg-config --libs ddjvuapi)
104 +endif
105 +endif
107 SYS_JBIG2DEC_LIBS = -ljbig2dec
108 SYS_JPEG_LIBS = -ljpeg
109 SYS_ZLIB_LIBS = -lz
110 diff --git a/Makethird b/Makethird
111 index ad48011..8308ab9 100644
112 --- a/Makethird
113 +++ b/Makethird
114 @@ -532,6 +532,11 @@ ZLIB_CFLAGS := $(SYS_ZLIB_CFLAGS)
115 ZLIB_LIBS := $(SYS_ZLIB_LIBS)
116 endif
118 +# --- DDJVU ---
120 +DDJVU_CFLAGS := $(SYS_DDJVU_CFLAGS)
121 +DDJVU_LIBS := $(SYS_DDJVU_LIBS)
123 # --- cURL ---
125 ifneq "$(wildcard $(CURL_DIR)/README)" ""
126 diff --git a/include/mupdf/fitz/config.h b/include/mupdf/fitz/config.h
127 index 280384d..0e46e8c 100644
128 --- a/include/mupdf/fitz/config.h
129 +++ b/include/mupdf/fitz/config.h
130 @@ -28,6 +28,7 @@
131 /* #define FZ_ENABLE_HTML 1 */
132 /* #define FZ_ENABLE_EPUB 1 */
133 /* #define FZ_ENABLE_GPRF 1 */
134 +/* #define FZ_ENABLE_DJVU 1 */
137 Choose whether to enable JavaScript.
138 @@ -129,6 +130,10 @@
139 #define FZ_ENABLE_GPRF 0
140 #endif /* FZ_ENABLE_GPRF */
142 +#ifndef FZ_ENABLE_DJVU
143 +#define FZ_ENABLE_DJVU 1
144 +#endif /* FZ_ENABLE_DJVU */
146 #ifndef FZ_ENABLE_JS
147 #define FZ_ENABLE_JS 1
148 #endif /* FZ_ENABLE_JS */
149 diff --git a/platform/gl/gl-win32.c b/platform/gl/gl-win32.c
150 index d583033..12e080e 100644
151 --- a/platform/gl/gl-win32.c
152 +++ b/platform/gl/gl-win32.c
153 @@ -15,7 +15,7 @@ void win_install(void)
154 char command_str[2048], argv0[2048];
155 HKEY software, classes, mupdf;
156 HKEY supported_types, shell, open, command;
157 - HKEY dotpdf, dotxps, dotcbz, dotepub;
158 + HKEY dotpdf, dotxps, dotcbz, dotepub, dotdjvu;
159 HKEY pdf_progids, xps_progids, cbz_progids, epub_progids;
161 GetModuleFileNameA(NULL, argv0, sizeof argv0);
162 @@ -32,6 +32,7 @@ void win_install(void)
163 SET_VALUE(supported_types, ".xps", "");
164 SET_VALUE(supported_types, ".cbz", "");
165 SET_VALUE(supported_types, ".epub", "");
166 + SET_VALUE(supported_types, ".djvu", "");
168 RegCloseKey(supported_types);
169 OPEN_KEY(mupdf, "shell", &shell);
170 @@ -51,26 +52,31 @@ void win_install(void)
171 OPEN_KEY(classes, ".xps", &dotxps);
172 OPEN_KEY(classes, ".cbz", &dotcbz);
173 OPEN_KEY(classes, ".epub", &dotepub);
174 + OPEN_KEY(classes, ".djvu", &dotdjvu);
176 OPEN_KEY(dotpdf, "OpenWithProgids", &pdf_progids);
177 OPEN_KEY(dotxps, "OpenWithProgids", &xps_progids);
178 OPEN_KEY(dotcbz, "OpenWithProgids", &cbz_progids);
179 OPEN_KEY(dotepub, "OpenWithProgids", &epub_progids);
180 + OPEN_KEY(dotdjvu, "OpenWithProgids", &djvu_progids);
182 SET_VALUE(pdf_progids, "MuPDF", "");
183 SET_VALUE(xps_progids, "MuPDF", "");
184 SET_VALUE(cbz_progids, "MuPDF", "");
185 SET_VALUE(epub_progids, "MuPDF", "");
186 + SET_VALUE(djvu_progids, "MuPDF", "");
188 RegCloseKey(pdf_progids);
189 RegCloseKey(xps_progids);
190 RegCloseKey(cbz_progids);
191 RegCloseKey(epub_progids);
192 + RegCloseKey(djvu_progids);
194 RegCloseKey(dotpdf);
195 RegCloseKey(dotxps);
196 RegCloseKey(dotcbz);
197 RegCloseKey(dotepub);
198 + RegCloseKey(dotdjvu);
200 RegCloseKey(classes);
201 RegCloseKey(software);
202 @@ -87,7 +93,7 @@ int win_open_file(char *buf, int len)
203 ofn.lpstrFile = wbuf;
204 ofn.nMaxFile = 2048;
205 ofn.lpstrTitle = L"MuPDF: Open PDF file";
206 - ofn.lpstrFilter = L"Documents (*.pdf;*.xps;*.cbz;*.epub;*.zip;*.png;*.jpeg;*.tiff)\0*.zip;*.cbz;*.xps;*.epub;*.pdf;*.jpe;*.jpg;*.jpeg;*.jfif;*.tif;*.tiff\0PDF Files (*.pdf)\0*.pdf\0XPS Files (*.xps)\0*.xps\0CBZ Files (*.cbz;*.zip)\0*.zip;*.cbz\0EPUB Files (*.epub)\0*.epub\0Image Files (*.png;*.jpeg;*.tiff)\0*.png;*.jpg;*.jpe;*.jpeg;*.jfif;*.tif;*.tiff\0All Files\0*\0\0";
207 + ofn.lpstrFilter = L"Documents (*.pdf;*.xps;*.cbz;*.epub;*.djvu;*.zip;*.png;*.jpeg;*.tiff)\0*.zip;*.cbz;*.xps;*.epub;*.djvu;*.pdf;*.jpe;*.jpg;*.jpeg;*.jfif;*.tif;*.tiff\0PDF Files (*.pdf)\0*.pdf\0XPS Files (*.xps)\0*.xps\0CBZ Files (*.cbz;*.zip)\0*.zip;*.cbz\0EPUB Files (*.epub)\0*.epub\0DjVu Files (*.djvu)\0*.djvu\0Image Files (*.png;*.jpeg;*.tiff)\0*.png;*.jpg;*.jpe;*.jpeg;*.jfif;*.tif;*.tiff\0All Files\0*\0\0";
208 ofn.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
209 code = GetOpenFileNameW(&ofn);
210 if (code)
211 diff --git a/platform/x11/win_main.c b/platform/x11/win_main.c
212 index b6a5ae6..4c7624b 100644
213 --- a/platform/x11/win_main.c
214 +++ b/platform/x11/win_main.c
215 @@ -57,7 +57,7 @@ static char filename[PATH_MAX];
216 void install_app(char *argv0)
218 char buf[512];
219 - HKEY software, classes, mupdf, dotpdf, dotxps, dotepub;
220 + HKEY software, classes, mupdf, dotpdf, dotxps, dotepub, dotdjvu;
221 HKEY shell, open, command, supported_types;
222 HKEY pdf_progids, xps_progids, epub_progids;
224 @@ -69,6 +69,8 @@ void install_app(char *argv0)
225 OPEN_KEY(dotxps, "OpenWithProgids", xps_progids);
226 OPEN_KEY(classes, ".epub", dotepub);
227 OPEN_KEY(dotepub, "OpenWithProgids", epub_progids);
228 + OPEN_KEY(classes, ".djvu", dotdjvu);
229 + OPEN_KEY(dotdjvu, "OpenWithProgids", djvu_progids);
230 OPEN_KEY(classes, "MuPDF", mupdf);
231 OPEN_KEY(mupdf, "SupportedTypes", supported_types);
232 OPEN_KEY(mupdf, "shell", shell);
233 @@ -82,10 +84,12 @@ void install_app(char *argv0)
234 SET_KEY(supported_types, ".pdf", "");
235 SET_KEY(supported_types, ".xps", "");
236 SET_KEY(supported_types, ".epub", "");
237 + SET_KEY(supported_types, ".djvu", "");
238 SET_KEY(pdf_progids, "MuPDF", "");
239 SET_KEY(xps_progids, "MuPDF", "");
240 SET_KEY(epub_progids, "MuPDF", "");
242 + RegCloseKey(dotdjvu);
243 RegCloseKey(dotepub);
244 RegCloseKey(dotxps);
245 RegCloseKey(dotpdf);
246 @@ -191,7 +195,7 @@ int winfilename(wchar_t *buf, int len)
247 ofn.nMaxFile = len;
248 ofn.lpstrInitialDir = NULL;
249 ofn.lpstrTitle = L"MuPDF: Open PDF file";
250 - ofn.lpstrFilter = L"Documents (*.pdf;*.xps;*.cbz;*.epub;*.zip;*.png;*.jpeg;*.tiff)\0*.zip;*.cbz;*.xps;*.epub;*.pdf;*.jpe;*.jpg;*.jpeg;*.jfif;*.tif;*.tiff\0PDF Files (*.pdf)\0*.pdf\0XPS Files (*.xps)\0*.xps\0CBZ Files (*.cbz;*.zip)\0*.zip;*.cbz\0EPUB Files (*.epub)\0*.epub\0Image Files (*.png;*.jpeg;*.tiff)\0*.png;*.jpg;*.jpe;*.jpeg;*.jfif;*.tif;*.tiff\0All Files\0*\0\0";
251 + ofn.lpstrFilter = L"Documents (*.pdf;*.xps;*.cbz;*.epub;*.djvu;*.zip;*.png;*.jpeg;*.tiff)\0*.zip;*.cbz;*.xps;*.epub;*.djvu;*.pdf;*.jpe;*.jpg;*.jpeg;*.jfif;*.tif;*.tiff\0PDF Files (*.pdf)\0*.pdf\0XPS Files (*.xps)\0*.xps\0CBZ Files (*.cbz;*.zip)\0*.zip;*.cbz\0EPUB Files (*.epub)\0*.epub\0DjVu Files (*.djvu)\0*.djvu\0Image Files (*.png;*.jpeg;*.tiff)\0*.png;*.jpg;*.jpe;*.jpeg;*.jfif;*.tif;*.tiff\0All Files\0*\0\0";
252 ofn.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
253 return GetOpenFileNameW(&ofn);
255 diff --git a/source/djvu/djvu-doc.c b/source/djvu/djvu-doc.c
256 new file mode 100644
257 index 0000000..43beb88
258 --- /dev/null
259 +++ b/source/djvu/djvu-doc.c
260 @@ -0,0 +1,271 @@
261 +#include "mupdf/fitz.h"
263 +#if FZ_ENABLE_DJVU
264 +#include <limits.h>
266 +#include <libdjvu/ddjvuapi.h>
267 +#include <libdjvu/miniexp.h>
269 +/* libdjvu defines this, but so does document.h */
270 +#ifdef inline
271 +#undef inline
272 +#endif
274 +/* XXX: Find a way to do this better */
275 +#define DPI 96.0f
277 +typedef struct djvu_document_s djvu_document;
278 +typedef struct djvu_page_s djvu_page;
280 +struct djvu_document_s
282 + fz_document super;
283 + char *filename;
284 + ddjvu_context_t *ddjvu_ctx;
285 + ddjvu_document_t *ddjvu_doc;
286 + int page_count;
287 + int header_complete;
288 + int want_stream0;
291 +struct djvu_page_s
293 + fz_page super;
294 + fz_image *image;
295 + int width;
296 + int height;
297 + int dpi;
300 +static void
301 +djvu_exhaust_messages(fz_context *ctx, djvu_document *doc)
303 + const ddjvu_message_t *msg;
305 + ddjvu_message_wait(doc->ddjvu_ctx);
307 + while ((msg = ddjvu_message_peek(doc->ddjvu_ctx)))
309 + switch(msg->m_any.tag)
311 + case DDJVU_ERROR:
312 + fz_throw(ctx, FZ_ERROR_GENERIC, "ddjvu: %s", msg->m_error.message);
313 + break;
314 + case DDJVU_NEWSTREAM:
315 + /* I don't particularly like this feature */
316 + printf("ddjvu: newstream\n\turl = %s\n\tname = %s\n\tstreamid = %d\n", msg->m_newstream.url, msg->m_newstream.name, msg->m_newstream.streamid);
317 + if (msg->m_newstream.streamid != 0)
318 + ddjvu_stream_close(doc->ddjvu_doc, msg->m_newstream.streamid, FALSE);
319 + else
320 + doc->want_stream0 = 1;
321 + break;
322 + case DDJVU_DOCINFO:
323 + doc->header_complete = 1;
324 + break;
325 + case DDJVU_PROGRESS:
326 + printf("ddjvu: reports %d%% progress\n", msg->m_progress.percent);
327 + break;
328 + case DDJVU_RELAYOUT:
329 + case DDJVU_PAGEINFO:
330 + case DDJVU_REDISPLAY:
331 + case DDJVU_CHUNK:
332 + case DDJVU_THUMBNAIL:
333 + case DDJVU_INFO:
334 + /* Ignore this chaff */
335 + break;
337 + ddjvu_message_pop(doc->ddjvu_ctx);
341 +static void
342 +djvu_drop_document(fz_context *ctx, fz_document *doc_)
344 + djvu_document *doc = (djvu_document *)doc_;
345 + ddjvu_job_stop(ddjvu_document_job(doc->ddjvu_doc));
346 + ddjvu_document_release(doc->ddjvu_doc);
347 + ddjvu_context_release(doc->ddjvu_ctx);
348 + doc->ddjvu_ctx = NULL;
351 +static int
352 +djvu_count_pages(fz_context *ctx, fz_document *doc_)
354 + djvu_document *doc = (djvu_document *)doc_;
355 + return doc->page_count;
358 +static fz_rect *
359 +djvu_bound_page(fz_context *ctx, fz_page *page_, fz_rect *bbox)
361 + djvu_page *page = (djvu_page *)page_;
362 + bbox->x0 = bbox->y0 = 0;
364 + bbox->x1 = page->width * DPI / page->dpi;
365 + bbox->y1 = page->height * DPI / page->dpi;
366 + return bbox;
369 +static void
370 +djvu_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie)
372 + fz_matrix local_ctm = *ctm;
373 + djvu_page *page = (djvu_page *)page_;
374 + fz_image *image = page->image;
375 + int xres, yres;
376 + float w, h;
378 + fz_image_resolution(image, &xres, &yres);
379 + w = page->width * DPI / xres;
380 + h = page->height * DPI / yres;
381 + fz_pre_scale(&local_ctm, w, h);
382 + fz_fill_image(ctx, dev, image, &local_ctm, 1);
385 +static void djvu_drop_page(fz_context *ctx, fz_page *page_)
387 + djvu_page *page = (djvu_page *)page_;
388 + fz_drop_image(ctx, page->image);
391 +static djvu_page *
392 +djvu_load_page(fz_context *ctx, fz_document *doc_, int number)
394 + djvu_document *doc = (djvu_document *)doc_;
395 + djvu_page *page = NULL;
396 + ddjvu_status_t ddjvu_ret;
397 + ddjvu_page_t *ddjvu_page;
398 + ddjvu_pageinfo_t info;
399 + fz_colorspace *colorspace;
400 + fz_pixmap *pixmap;
401 + ddjvu_format_t *fmt;
402 + ddjvu_rect_t rect;
403 + char *buf;
405 + if (number < 0 || number >= doc->page_count)
406 + return NULL;
408 + while ((ddjvu_ret = ddjvu_document_get_pageinfo(doc->ddjvu_doc, number, &info)) < DDJVU_JOB_OK)
409 + djvu_exhaust_messages(ctx, doc);
410 + if (ddjvu_ret >= DDJVU_JOB_FAILED)
411 + fz_throw(ctx, FZ_ERROR_GENERIC, "DjVu pageinfo error");
413 + fz_var(page);
414 + fz_var(pixmap);
415 + fz_var(ddjvu_page);
416 + fz_var(colorspace);
417 + fz_var(fmt);
419 + fz_try(ctx)
421 + page = fz_new_page(ctx, sizeof *page);
422 + page->super.bound_page = (fz_page_bound_page_fn *)djvu_bound_page;
423 + page->super.run_page_contents = (fz_page_run_page_contents_fn *)djvu_run_page;
424 + page->super.drop_page = (fz_page_drop_page_fn *)djvu_drop_page;
426 + do {
427 + ddjvu_page = ddjvu_page_create_by_pageno(doc->ddjvu_doc, number);
428 + if (ddjvu_page == NULL)
429 + djvu_exhaust_messages(ctx, doc);
430 + } while (ddjvu_page == NULL);
432 + while (!ddjvu_page_decoding_done(ddjvu_page))
433 + djvu_exhaust_messages(ctx, doc);
435 + page->width = info.width;
436 + page->height = info.height;
437 + page->dpi = info.dpi;
439 + rect = (ddjvu_rect_t) {.x = 0, .y = 0, .w = page->width, .h = page->height};
440 + fmt = ddjvu_format_create(DDJVU_FORMAT_RGB24, 0, NULL);
441 + colorspace = fz_device_rgb(ctx);
442 + ddjvu_format_set_row_order(fmt, 1);
443 + buf = fz_calloc(ctx, page->width * 3 * page->height, 1);
444 + ddjvu_page_render(ddjvu_page, DDJVU_RENDER_COLOR, &rect, &rect, fmt, page->width * 3, buf);
445 + pixmap = fz_new_pixmap_with_data(ctx, colorspace, page->width, page->height, 0, page->width * 3, (unsigned char *)buf);
446 + pixmap->free_samples = 1;
447 + fz_set_pixmap_resolution(ctx, pixmap, page->dpi, page->dpi);
448 + page->image = fz_new_image_from_pixmap(ctx, pixmap, NULL);
450 + fz_always(ctx)
452 + fz_drop_pixmap(ctx, pixmap);
453 + fz_drop_colorspace(ctx, colorspace);
454 + ddjvu_format_release(fmt);
455 + ddjvu_page_release(ddjvu_page);
457 + fz_catch(ctx)
459 + djvu_drop_page(ctx, (fz_page *)page);
460 + page = NULL;
461 + fz_rethrow(ctx);
463 + return page;
466 +static int
467 +djvu_lookup_metadata(fz_context *ctx, fz_document *doc_, const char *key, char *buf, int size)
469 + if (!strcmp(key, "format"))
470 + return (int)fz_strlcpy(buf, "DjVu", size);
471 + return -1;
474 +static int
475 +djvu_recognize(fz_context *doc, const char *magic)
477 + char *ext = strrchr(magic, '.');
478 + if (ext)
479 + if (!fz_strcasecmp(ext, ".djvu"))
480 + return 50;
481 + if (!strcmp(magic, "image/vnd.djvu") || !strcmp(magic, "image/x.djvu") || !strcmp(magic, "image/x-djvu"))
482 + return 100;
484 + return 1;
487 +static djvu_document *
488 +djvu_open_document(fz_context *ctx, const char *filename)
490 + djvu_document *doc;
491 + doc = fz_new_document(ctx, djvu_document);
492 + doc->super.drop_document = (fz_document_drop_fn *)djvu_drop_document;
493 + doc->super.count_pages = (fz_document_count_pages_fn *)djvu_count_pages;
494 + doc->super.load_page = (fz_document_load_page_fn *)djvu_load_page;
495 + doc->super.lookup_metadata = (fz_document_lookup_metadata_fn *)djvu_lookup_metadata;
497 + fz_try(ctx)
499 + doc->filename = fz_strdup(ctx, filename);
501 + fz_catch(ctx)
503 + doc->filename = NULL;
506 + doc->ddjvu_ctx = ddjvu_context_create("mupdf");
507 + doc->ddjvu_doc = ddjvu_document_create_by_filename(doc->ddjvu_ctx, filename, 1);
508 + if (!doc->ddjvu_ctx)
509 + fz_throw(ctx, FZ_ERROR_GENERIC, "DjVU context creation failed");
511 + fz_try(ctx)
513 + while (!doc->header_complete)
514 + djvu_exhaust_messages(ctx, doc);
515 + doc->page_count = ddjvu_document_get_pagenum(doc->ddjvu_doc);
517 + fz_catch(ctx)
519 + fz_rethrow(ctx);
522 + return doc;
525 +fz_document_handler djvu_document_handler =
527 + (fz_document_recognize_fn *)&djvu_recognize,
528 + (fz_document_open_fn *)&djvu_open_document,
529 + (fz_document_open_with_stream_fn *) NULL
531 +#endif /* FZ_ENABLE_DJVU */
532 diff --git a/source/fitz/document-all.c b/source/fitz/document-all.c
533 index 53969e2..10c444f 100644
534 --- a/source/fitz/document-all.c
535 +++ b/source/fitz/document-all.c
536 @@ -9,6 +9,7 @@ extern fz_document_handler tiff_document_handler;
537 extern fz_document_handler html_document_handler;
538 extern fz_document_handler epub_document_handler;
539 extern fz_document_handler gprf_document_handler;
540 +extern fz_document_handler djvu_document_handler;
542 void fz_register_document_handlers(fz_context *ctx)
544 @@ -39,4 +40,7 @@ void fz_register_document_handlers(fz_context *ctx)
545 #if FZ_ENABLE_GPRF
546 fz_register_document_handler(ctx, &gprf_document_handler);
547 #endif /* FZ_ENABLE_GPRF */
548 +#if FZ_ENABLE_DJVU
549 + fz_register_document_handler(ctx, &djvu_document_handler);
550 +#endif /* FZ_ENABLE_DJVU */
553 2.11.1