This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
forked from andytango/mupdf-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap.js
224 lines (187 loc) · 5.98 KB
/
wrap.js
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
Module.noExitRuntime = true;
Module.noInitialRun = true;
Module.locateFile = function (path, prefix) {
if (ENVIRONMENT_IS_NODE) {
return prefix + path;
}
return path;
};
var logger = {
log: console.log,
errorLog: console.warn
}
Module.printErr = function (...args) {
if (logger.errorLog) {
logger.errorLog(...args);
}
}
Module.print = function (...args) {
if (logger.log) {
logger.log(...args);
}
}
Module.onRuntimeInitialized = function () {
var isNode = typeof process === "object" && typeof process.versions === "object" && typeof process.versions.node === "string";
function cloneData(src) {
if (isNode) {
return Buffer.from(src);
} else {
var dst = new ArrayBuffer(src.byteLength);
new Uint8Array(dst).set(new Uint8Array(src));
return dst;
}
}
function parseDto(ptr) {
var dataLen = new Uint32Array(HEAPU8.slice(ptr, ptr + 4).buffer)[0];
return cloneData(HEAPU8.slice(ptr + 4, ptr + 4 + dataLen));
}
function uint8ArrayToString(data) {
if (isNode) {
return Buffer.from(data).toString('utf8');
} else {
return new TextDecoder().decode(data);
}
}
mupdf.setLogger = function(newLogger) {
logger.log = newLogger.log;
logger.errorLog = newLogger.errorLog;
}
var dropDto = Module.cwrap("dropDto", "", ["number"]);
mupdf.createContext = Module.cwrap("createContext", "number", []);
mupdf.freeContext = Module.cwrap("freeContext", "", ["number"]);
var openDocument = Module.cwrap("openDocument", "number", ["number", "string"]);
mupdf.openDocument = function (ctx, fileName) {
var doc = openDocument(ctx, fileName);
if (doc === 0) {
throw new Error('Can\'t open document');
}
return doc;
}
mupdf.freeDocument = Module.cwrap("freeDocument", "null", ["number", "number"]);
mupdf.documentTitle = Module.cwrap("documentTitle", "string", ["number", "number"]);
mupdf.countPages = Module.cwrap("countPages", "number", ["number", "number"]);
mupdf.pageWidth = Module.cwrap("pageWidth", "number", [
"number",
"number",
"number",
"number",
]);
mupdf.pageHeight = Module.cwrap("pageHeight", "number", [
"number",
"number",
"number",
"number",
]);
var pageLinks = Module.cwrap("pageLinks", "number", [
"number",
"number",
"number",
"number",
]);
mupdf.pageLinks = function (ctx, doc, pageNumber, dpi) {
var dto = pageLinks(ctx, doc, pageNumber, dpi);
var data = parseDto(dto);
var str = uint8ArrayToString(data.slice(0, -1));
dropDto(dto);
return str;
}
var drawPageAsPNGRaw = Module.cwrap("drawPageAsPNGRaw", "number", [
"number",
"number",
"number",
"number",
]);
mupdf.drawPageAsPNGRaw = function (ctx, doc, pageNumber, resolution) {
var dto = drawPageAsPNGRaw(ctx, doc, pageNumber, resolution);
var data = parseDto(dto);
dropDto(dto);
return data;
}
var drawPageAsPNG = Module.cwrap("drawPageAsPNG", "number", [
"number",
"number",
"number",
"number",
]);
mupdf.drawPageAsPNG = function (ctx, doc, pageNumber, resolution) {
var dto = drawPageAsPNG(ctx, doc, pageNumber, resolution);
var data = parseDto(dto);
var str = uint8ArrayToString(data.slice(0, -1));
dropDto(dto);
return str;
}
var drawPageAsHTML = Module.cwrap("drawPageAsHTML", "number", [
"number",
"number",
"number",
]);
mupdf.drawPageAsHTML = function (ctx, doc, pageNumber) {
var dto = drawPageAsHTML(ctx, doc, pageNumber);
var data = parseDto(dto);
var str = uint8ArrayToString(data.slice(0, -1));
dropDto(dto);
return str;
}
var drawPageAsSVG = Module.cwrap("drawPageAsSVG", "number", [
"number",
"number",
"number",
]);
mupdf.drawPageAsSVG = function (ctx, doc, pageNumber) {
var dto = drawPageAsSVG(ctx, doc, pageNumber);
var data = parseDto(dto);
var str = uint8ArrayToString( data.slice(0, -1));
dropDto(dto);
return str;
}
mupdf.loadOutline = Module.cwrap("loadOutline", "number", ["number", "number"]);
mupdf.freeOutline = Module.cwrap("freeOutline", null, ["number", "number"]);
mupdf.outlineTitle = Module.cwrap("outlineTitle", "string", ["number"]);
mupdf.outlinePage = Module.cwrap("outlinePage", "number", ["number", "number"]);
mupdf.outlineDown = Module.cwrap("outlineDown", "number", ["number"]);
mupdf.outlineNext = Module.cwrap("outlineNext", "number", ["number"]);
var getPageText = Module.cwrap("getPageText", "number", ["number", "number", "number"]);
mupdf.getPageText = function(ctx, doc, pageNumber) {
var dto = getPageText(ctx, doc, pageNumber);
var data = parseDto(dto);
var str = uint8ArrayToString(data.slice(0, -1));
dropDto(dto);
return str
};
var searchPageText = Module.cwrap("searchPageText", "number", ["number", "number", "number", "string", "number"]);
mupdf.searchPageText = function (ctx, doc, pageNumber, searchString, maxHits) {
var dto = searchPageText(ctx, doc, pageNumber, searchString, maxHits);
var data = parseDto(dto);
var resultStr = uint8ArrayToString(data);
return resultStr.slice(0, -1).split('\n').map(function (line) {
var [x, y, w, h, c] = line.split(';').map(function (item) { return parseFloat(item); });
return {x, y, w, h, isContinuation: c === 1};
})
}
};
mupdf.documentOutline = function (ctx, doc) {
function makeOutline(node) {
var ul = document.createElement("ul");
while (node) {
var li = document.createElement("li");
var a = document.createElement("a");
a.href = "#page" + mupdf.outlinePage(ctx, node);
a.textContent = mupdf.outlineTitle(node);
li.appendChild(a);
var down = mupdf.outlineDown(node);
if (down) {
li.appendChild(makeOutline(down));
}
ul.appendChild(li);
node = mupdf.outlineNext(node);
}
return ul;
}
var root = mupdf.loadOutline(ctx, doc);
if (root) {
var ul = makeOutline(root);
mupdf.freeOutline(ctx, root);
return ul;
}
return null;
};