diff mupdf-source/platform/wasm/examples/streams/custom-stream.js @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mupdf-source/platform/wasm/examples/streams/custom-stream.js	Mon Sep 15 11:43:07 2025 +0200
@@ -0,0 +1,45 @@
+"use strict"
+
+import * as fs from "fs"
+import * as mupdf from "mupdf"
+
+// Use synchronous file descriptor API with Node.
+class NodeFileStream {
+        constructor(path) {
+                this.fd = fs.openSync(path)
+        }
+        fileSize() {
+                return fs.fstatSync(this.fd).size
+        }
+        read(memory, offset, size, position) {
+                return fs.readSync(this.fd, memory, offset, size, position)
+        }
+        close() {
+                fs.closeSync(this.fd)
+        }
+}
+
+// Use FileReaderSync on Blob/File objects in Web Workers.
+class WorkerBlobStream {
+	constructor(blob) {
+		this.reader = new FileReaderSync()
+		this.blob = blob
+	}
+	fileSize() {
+		return this.blob.size
+	}
+	read(memory, offset, size, position) {
+		var data = this.reader.readAsArrayBuffer(this.blob.slice(position, position + size))
+		memory.set(new Uint8Array(data), offset)
+		return data.byteLength
+	}
+	close() {
+		this.reader = null
+		this.blob = null
+	}
+}
+
+/* to test:
+var stm = new mupdf.Stream(new NodeFileStream("pdfref17.pdf"))
+var doc = mupdf.Document.openDocument(stm, "application/pdf")
+*/