From dfeab303cb3111a13e161b74fc98b0a1de21a447 Mon Sep 17 00:00:00 2001 From: David Greenberg Date: Fri, 21 Jul 2023 12:18:07 -0400 Subject: [PATCH] Changed viewer to respect alpha, overprint, and spot by default. Also added save as PNG. --- platform/wasm/lib/mupdf.c | 33 +++++++++++++++++++++-- platform/wasm/lib/mupdf.js | 14 ++++++---- platform/wasm/viewer/mupdf-view-page.js | 15 +++++++++++ platform/wasm/viewer/mupdf-view-worker.js | 29 +++++++++++++++++--- platform/wasm/viewer/mupdf-view.html | 1 + 5 files changed, 81 insertions(+), 11 deletions(-) diff --git a/platform/wasm/lib/mupdf.c b/platform/wasm/lib/mupdf.c index 9aac5caf27..ee489c4b75 100644 --- a/platform/wasm/lib/mupdf.c +++ b/platform/wasm/lib/mupdf.c @@ -282,13 +282,42 @@ fz_pixmap * wasm_get_pixmap_from_image(fz_image *image) EXPORT fz_pixmap * wasm_new_pixmap_from_page(fz_page *page, fz_matrix *ctm, fz_colorspace *colorspace, int alpha) { - POINTER(fz_new_pixmap_from_page, page, *ctm, colorspace, alpha) + fz_separations *seps = NULL; + fz_var(seps); + seps = fz_page_separations(ctx, page); + if (seps) + { + int i, n = fz_count_separations(ctx, seps); + for (i = 0; i < n; i++) + fz_set_separation_behavior(ctx, seps, i, FZ_SEPARATION_SPOT); + } + POINTER(fz_new_pixmap_from_page_with_separations, page, *ctm, colorspace, seps, alpha) } EXPORT fz_pixmap * wasm_new_pixmap_with_bbox(fz_colorspace *colorspace, fz_rect *bbox, int alpha) { - POINTER(fz_new_pixmap_with_bbox, colorspace, fz_irect_from_rect(*bbox), NULL, alpha) + fz_separations *seps = NULL; + fz_var(seps); + seps = fz_new_separations(ctx, 0); + POINTER(fz_new_pixmap_with_bbox, colorspace, fz_irect_from_rect(*bbox), seps, alpha) +} + +EXPORT +fz_pixmap * wasm_new_pixmap_with_bbox_and_page(fz_colorspace *colorspace, fz_rect *bbox, int alpha, fz_page *page) +{ + fz_separations *seps = NULL; + fz_var(seps); + seps = fz_page_separations(ctx, page); + if (seps) + { + int i, n = fz_count_separations(ctx, seps); + for (i = 0; i < n; i++) + fz_set_separation_behavior(ctx, seps, i, FZ_SEPARATION_COMPOSITE); + } else { + seps = fz_new_separations(ctx, 0); + } + POINTER(fz_new_pixmap_with_bbox, colorspace, fz_irect_from_rect(*bbox), seps, alpha) } EXPORT diff --git a/platform/wasm/lib/mupdf.js b/platform/wasm/lib/mupdf.js index dd4baccded..5aee74cd32 100644 --- a/platform/wasm/lib/mupdf.js +++ b/platform/wasm/lib/mupdf.js @@ -675,7 +675,7 @@ class DisplayList extends Userdata { return fromRect(libmupdf._wasm_bound_display_list(this)) } - toPixmap(matrix, colorspace, alpha = false) { + toPixmap(matrix, colorspace, alpha = true) { checkMatrix(matrix) checkType(colorspace, ColorSpace) return new Pixmap( @@ -704,11 +704,15 @@ class DisplayList extends Userdata { class Pixmap extends Userdata { static _drop = "_wasm_drop_pixmap" - constructor(arg1, bbox = null, alpha = false) { + constructor(arg1, bbox = null, alpha = true, page = null) { let pointer = arg1 if (arg1 instanceof ColorSpace) { checkRect(bbox) - pointer = libmupdf._wasm_new_pixmap_with_bbox(arg1, RECT(bbox), alpha) + if (page) { + pointer = libmupdf._wasm_new_pixmap_with_bbox_and_page(arg1, RECT(bbox), alpha, page) + } else { + pointer = libmupdf._wasm_new_pixmap_with_bbox(arg1, RECT(bbox), alpha) + } } super(pointer) } @@ -1345,7 +1349,7 @@ class Page extends Userdata { libmupdf._wasm_run_page_widgets(this, device, MATRIX(matrix)) } - toPixmap(matrix, colorspace, alpha = false, showExtras = true) { + toPixmap(matrix, colorspace, alpha = true, showExtras = true) { checkType(colorspace, ColorSpace) checkMatrix(matrix) let result @@ -1973,7 +1977,7 @@ class PDFAnnotation extends Userdata { libmupdf._wasm_pdf_run_annot(this, device, MATRIX(matrix)) } - toPixmap(matrix, colorspace, alpha = false) { + toPixmap(matrix, colorspace, alpha = true) { checkMatrix(matrix) checkType(colorspace, ColorSpace) return new Pixmap( diff --git a/platform/wasm/viewer/mupdf-view-page.js b/platform/wasm/viewer/mupdf-view-page.js index 955d664833..166b77e94f 100644 --- a/platform/wasm/viewer/mupdf-view-page.js +++ b/platform/wasm/viewer/mupdf-view-page.js @@ -849,6 +849,21 @@ class MupdfDocumentViewer { this.documentHandler?.goToPage(pageNumber) } + async saveAsPNG() { + let pngs = await this.mupdfWorker.getPngs(); + for (let i = 0; i < pngs.length; i++) { + let png = pngs[i]; + let blob = new Blob([png], { type: 'image/png' }); + const link = document.createElement('a'); + link.href = URL.createObjectURL(blob); + link.download = this.documentHandler.title + '_' + (i + 1) + '.png'; + document.body.append(link); + link.click(); + link.remove(); + URL.revokeObjectURL(link.href); + } + } + toggleFullscreen() { if (!document.fullscreenElement) { this.enterFullscreen() diff --git a/platform/wasm/viewer/mupdf-view-worker.js b/platform/wasm/viewer/mupdf-view-worker.js index 503da15562..599c24ba1c 100644 --- a/platform/wasm/viewer/mupdf-view-worker.js +++ b/platform/wasm/viewer/mupdf-view-worker.js @@ -196,22 +196,43 @@ workerMethods.drawPageAsPNG = function (pageNumber, dpi) { // TODO - use canvas? let page = openDocument.loadPage(pageNumber - 1) - let pixmap = page.toPixmap(doc_to_screen, mupdf.DeviceRGB, false) + //let pixmap = page.toPixmap(doc_to_screen, mupdf.DeviceRGB, false) + let bbox = Rect.transform(page.getBounds(), doc_to_screen) + let pixmap = new mupdf.Pixmap(mupdf.ColorSpace.DeviceRGB, bbox, true, page) - let png = pixmap?.saveAsPNG() + let png = pixmap?.asPNG() pixmap?.destroy() return png } +workerMethods.getPngs = function (dpi = 300) { + const doc_to_screen = mupdf.Matrix.scale(dpi / 72, dpi / 72); + const pages = openDocument.countPages(); + let pngs = []; + for (let i = 0; i < pages; i++) { + let page = openDocument.loadPage(i); + let bbox = Rect.transform(page.getBounds(), doc_to_screen); + let pixmap = new mupdf.Pixmap(mupdf.ColorSpace.DeviceRGB, bbox, true, page); + pixmap.clear() + let device = new mupdf.DrawDevice(doc_to_screen, pixmap) + page.run(device, Matrix.identity) + device.close() + let png = pixmap?.asPNG(); + pixmap?.destroy(); + pngs.push(png); + } + return pngs; +}; + workerMethods.drawPageAsPixmap = function (pageNumber, dpi) { const doc_to_screen = mupdf.Matrix.scale(dpi / 72, dpi / 72) let page = openDocument.loadPage(pageNumber - 1) let bbox = Rect.transform(page.getBounds(), doc_to_screen) - let pixmap = new mupdf.Pixmap(mupdf.ColorSpace.DeviceRGB, bbox, true) - pixmap.clear(255) + let pixmap = new mupdf.Pixmap(mupdf.ColorSpace.DeviceRGB, bbox, true, page) + pixmap.clear() let device = new mupdf.DrawDevice(doc_to_screen, pixmap) page.run(device, Matrix.identity) diff --git a/platform/wasm/viewer/mupdf-view.html b/platform/wasm/viewer/mupdf-view.html index bda23b9970..5df0e5a7bb 100644 --- a/platform/wasm/viewer/mupdf-view.html +++ b/platform/wasm/viewer/mupdf-view.html @@ -210,6 +210,7 @@