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

Changed viewer to respect alpha, overprint, and spot by default. Als… #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions platform/wasm/lib/mupdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions platform/wasm/lib/mupdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions platform/wasm/viewer/mupdf-view-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 25 additions & 4 deletions platform/wasm/viewer/mupdf-view-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions platform/wasm/viewer/mupdf-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<div class="menu-button">File</div>
<div class="menu-popup">
<div onclick="document.getElementById('open-file-input').click()">Open File...</div>
<div onclick="documentViewer?.saveAsPNG()">Save as PNG</div>
</div>
</div>
<div class="menu">
Expand Down