-
Notifications
You must be signed in to change notification settings - Fork 23
/
mupdf.ts
3492 lines (2932 loc) · 91.7 KB
/
mupdf.ts
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2004-2023 Artifex Software, Inc.
//
// This file is part of MuPDF WASM Library.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
"use strict"
import { Pointer } from "./mupdf-wasm.js"
import libmupdf_wasm from "./mupdf-wasm.js"
const libmupdf = await libmupdf_wasm()
libmupdf._wasm_init_context()
function Malloc<T>(size: number) {
return libmupdf._wasm_malloc(size) as unknown as Pointer<T>
}
function Free(ptr: any) {
return libmupdf._wasm_free(ptr as Pointer<"void">)
}
/*
--------------------------------------------------------------------------------
How to call into WASM and convert values between JS and WASM (libmupdf) worlds:
Passing values into WASM needs to either copy primitive values into WASM memory
or passing around pointer values.
Wrap and/or copy non-Userdata values into WASM:
STRING(stringValue)
STRING2(stringValue) -- if you need to pass more than one string
MATRIX(matrixArray)
RECT(rectArray)
BUFFER(bufferValue)
etc.
Look up an enum value by string:
ENUM<EnumType>(string, listOfValidValues)
Pass the pointer when the value is a Userdata object:
userdataObject.pointer
Convert WASM pointer into a JS value (for simple types like strings and matrices).
fromType(pointer)
Wrap a WASM pointer in a new Userdata object (for complex types):
new Wrapper(pointer)
PDFObjects are always bound to a PDFDocument, so must be accessed via a document:
doc._fromPDFObjectNew(new_ptr)
doc._fromPDFObjectKeep(borrowed_ptr)
doc._PDFOBJ(value)
Type checking of input arguments at runtime.
checkType(value, "string")
checkType(value, Class)
checkRect(value)
checkMatrix(value)
This code needs to work type safely from plain Javascript too,
so do NOT rely on Typescript to do all the type checking.
--------------------------------------------------------------------------------
*/
type Matrix = [number, number, number, number, number, number]
type Rect = [number, number, number, number]
type Quad = [number, number, number, number, number, number, number, number]
type Point = [number, number]
type Color = [number] | [number, number, number] | [number, number, number, number]
type Rotate = 0 | 90 | 180 | 270
export const Matrix = {
identity: [ 1, 0, 0, 1, 0, 0 ] as Matrix,
scale(sx: number, sy: number): Matrix {
return [ sx, 0, 0, sy, 0, 0 ]
},
translate(tx: number, ty: number): Matrix {
return [ 1, 0, 0, 1, tx, ty ]
},
rotate(d: number): Matrix {
while (d < 0)
d += 360
while (d >= 360)
d -= 360
let s = Math.sin((d * Math.PI) / 180)
let c = Math.cos((d * Math.PI) / 180)
return [ c, s, -s, c, 0, 0 ]
},
invert(m: Matrix): Matrix {
checkMatrix(m)
let det = m[0] * m[3] - m[1] * m[2]
if (det > -1e-23 && det < 1e-23)
return m
let rdet = 1 / det
let inva = m[3] * rdet
let invb = -m[1] * rdet
let invc = -m[2] * rdet
let invd = m[0] * rdet
let inve = -m[4] * inva - m[5] * invc
let invf = -m[4] * invb - m[5] * invd
return [ inva, invb, invc, invd, inve, invf ]
},
concat(one: Matrix, two: Matrix): Matrix {
checkMatrix(one)
checkMatrix(two)
return [
one[0] * two[0] + one[1] * two[2],
one[0] * two[1] + one[1] * two[3],
one[2] * two[0] + one[3] * two[2],
one[2] * two[1] + one[3] * two[3],
one[4] * two[0] + one[5] * two[2] + two[4],
one[4] * two[1] + one[5] * two[3] + two[5],
]
},
}
export const Rect = {
MIN_INF_RECT: 0x80000000,
MAX_INF_RECT: 0x7fffff80,
isEmpty: function (rect: Rect) {
checkRect(rect)
return rect[0] >= rect[2] || rect[1] >= rect[3]
},
isValid: function (rect: Rect) {
checkRect(rect)
return rect[0] <= rect[2] && rect[1] <= rect[3]
},
isInfinite: function (rect: Rect) {
checkRect(rect)
return (
rect[0] === Rect.MIN_INF_RECT &&
rect[1] === Rect.MIN_INF_RECT &&
rect[2] === Rect.MAX_INF_RECT &&
rect[3] === Rect.MAX_INF_RECT
)
},
transform: function (rect: Rect, matrix: Matrix): Rect {
checkRect(rect)
checkMatrix(matrix)
var t
if (Rect.isInfinite(rect))
return rect
if (!Rect.isValid(rect))
return rect
var ax0 = rect[0] * matrix[0]
var ax1 = rect[2] * matrix[0]
if (ax0 > ax1)
t = ax0, ax0 = ax1, ax1 = t
var cy0 = rect[1] * matrix[2]
var cy1 = rect[3] * matrix[2]
if (cy0 > cy1)
t = cy0, cy0 = cy1, cy1 = t
ax0 += cy0 + matrix[4]
ax1 += cy1 + matrix[4]
var bx0 = rect[0] * matrix[1]
var bx1 = rect[2] * matrix[1]
if (bx0 > bx1)
t = bx0, bx0 = bx1, bx1 = t
var dy0 = rect[1] * matrix[3]
var dy1 = rect[3] * matrix[3]
if (dy0 > dy1)
t = dy0, dy0 = dy1, dy1 = t
bx0 += dy0 + matrix[5]
bx1 += dy1 + matrix[5]
return [ ax0, bx0, ax1, bx1 ]
},
}
export function enableICC() {
libmupdf._wasm_enable_icc()
}
export function disableICC() {
libmupdf._wasm_disable_icc()
}
export function setUserCSS(text: string) {
libmupdf._wasm_set_user_css(STRING(text))
}
/* -------------------------------------------------------------------------- */
// To pass Rect and Matrix as pointer arguments
const _wasm_int = Malloc<"int">(4)
const _wasm_point = Malloc<"fz_point">(4 * 4) >> 2
const _wasm_rect = Malloc<"fz_rect">(4 * 8) >> 2
const _wasm_matrix = Malloc<"fz_matrix">(4 * 6) >> 2
const _wasm_color = Malloc<"number">(4 * 4) >> 2
const _wasm_quad = Malloc<"fz_quad">(4 * 8) >> 2
const _wasm_string: [ Pointer<"char">, Pointer<"char"> ] = [ 0 as Pointer<"char">, 0 as Pointer<"char"> ]
function checkType(value: any, type: any) {
if (typeof type === "string" && typeof value !== type)
throw new TypeError("expected " + type)
if (typeof type === "function" && !(value instanceof type))
throw new TypeError("expected " + type.name)
}
function checkPoint(value: any): asserts value is Point {
if (!Array.isArray(value) || value.length !== 2)
throw new TypeError("expected point")
}
function checkRect(value: any): asserts value is Rect {
if (!Array.isArray(value) || value.length !== 4)
throw new TypeError("expected rectangle")
}
function checkMatrix(value: any): asserts value is Matrix {
if (!Array.isArray(value) || value.length !== 6)
throw new TypeError("expected matrix")
}
function checkQuad(value: any): asserts value is Quad {
if (!Array.isArray(value) || value.length !== 8)
throw new TypeError("expected quad")
}
function checkColor(value: any): asserts value is Color {
if (!Array.isArray(value) || (value.length !== 1 && value.length !== 3 && value.length !== 4))
throw new TypeError("expected color array")
}
/** The types that can be automatically converted into a Buffer object */
type AnyBuffer = Buffer | ArrayBuffer | Uint8Array | string
function BUFFER(input: AnyBuffer) {
if (input instanceof Buffer)
return input.pointer
if (input instanceof ArrayBuffer || input instanceof Uint8Array)
return new Buffer(input).pointer
if (typeof input === "string")
return new Buffer(input).pointer
throw new TypeError("expected buffer")
}
function ENUM<T>(value: T, list: readonly T[]): number {
if (typeof value === "number") {
if (value >= 0 && value < list.length)
return value
}
if (typeof value === "string") {
let idx = list.indexOf(value)
if (idx >= 0)
return idx
}
throw new TypeError(`invalid enum value ("${value}"; expected ${list.join(", ")})`)
}
function allocateUTF8(str: string) {
var size = libmupdf.lengthBytesUTF8(str) + 1
var pointer = Malloc<"XXX">(size) as unknown as Pointer<"char">
libmupdf.stringToUTF8(str, pointer, size)
return pointer
}
function STRING_N(s: string, i: number) {
if (_wasm_string[i]) {
Free(_wasm_string[i])
_wasm_string[i] = 0 as Pointer<"char">
}
return _wasm_string[i] = allocateUTF8(s)
}
function STRING(s: string) {
return STRING_N(s, 0)
}
function STRING2(s: string) {
return STRING_N(s, 1)
}
function STRING_OPT(s: string | null | undefined) {
return typeof s === "string" ? STRING_N(s, 0) : 0 as Pointer<"char">
}
function STRING2_OPT(s: string | null | undefined) {
return typeof s === "string" ? STRING_N(s, 1) : 0 as Pointer<"char">
}
function POINT(p: Point) {
libmupdf.HEAPF32[_wasm_point + 0] = p[0]
libmupdf.HEAPF32[_wasm_point + 1] = p[1]
return _wasm_point << 2 as Pointer<"fz_point">
}
function POINT2(p: Point) {
libmupdf.HEAPF32[_wasm_point + 2] = p[0]
libmupdf.HEAPF32[_wasm_point + 3] = p[1]
return (_wasm_point + 2) << 2 as Pointer<"fz_point">
}
function RECT(r: Rect) {
libmupdf.HEAPF32[_wasm_rect + 0] = r[0]
libmupdf.HEAPF32[_wasm_rect + 1] = r[1]
libmupdf.HEAPF32[_wasm_rect + 2] = r[2]
libmupdf.HEAPF32[_wasm_rect + 3] = r[3]
return _wasm_rect << 2 as Pointer<"fz_rect">
}
function RECT2(r: Rect) {
libmupdf.HEAPF32[_wasm_rect + 4] = r[0]
libmupdf.HEAPF32[_wasm_rect + 5] = r[1]
libmupdf.HEAPF32[_wasm_rect + 6] = r[2]
libmupdf.HEAPF32[_wasm_rect + 7] = r[3]
return (_wasm_rect + 4) << 2 as Pointer<"fz_rect">
}
function MATRIX(m: Matrix) {
libmupdf.HEAPF32[_wasm_matrix + 0] = m[0]
libmupdf.HEAPF32[_wasm_matrix + 1] = m[1]
libmupdf.HEAPF32[_wasm_matrix + 2] = m[2]
libmupdf.HEAPF32[_wasm_matrix + 3] = m[3]
libmupdf.HEAPF32[_wasm_matrix + 4] = m[4]
libmupdf.HEAPF32[_wasm_matrix + 5] = m[5]
return _wasm_matrix << 2 as Pointer<"fz_matrix">
}
function QUAD(q: Quad) {
libmupdf.HEAPF32[_wasm_quad + 0] = q[0]
libmupdf.HEAPF32[_wasm_quad + 1] = q[1]
libmupdf.HEAPF32[_wasm_quad + 2] = q[2]
libmupdf.HEAPF32[_wasm_quad + 3] = q[3]
libmupdf.HEAPF32[_wasm_quad + 4] = q[4]
libmupdf.HEAPF32[_wasm_quad + 5] = q[5]
libmupdf.HEAPF32[_wasm_quad + 6] = q[6]
libmupdf.HEAPF32[_wasm_quad + 7] = q[7]
return _wasm_quad << 2 as Pointer<"fz_quad">
}
function COLOR(c?: Color) {
if (typeof c !== "undefined") {
switch (c.length) {
case 1:
libmupdf.HEAPF32[_wasm_color + 0] = c[0]
break
case 3:
libmupdf.HEAPF32[_wasm_color + 0] = c[0]
libmupdf.HEAPF32[_wasm_color + 1] = c[1]
libmupdf.HEAPF32[_wasm_color + 2] = c[2]
break
case 4:
libmupdf.HEAPF32[_wasm_color + 0] = c[0]
libmupdf.HEAPF32[_wasm_color + 1] = c[1]
libmupdf.HEAPF32[_wasm_color + 2] = c[2]
libmupdf.HEAPF32[_wasm_color + 3] = c[3]
break
}
}
return _wasm_color << 2 as Pointer<"float">
}
/* -------------------------------------------------------------------------- */
function fromColor(n: number): Color {
if (n === 1)
return [
libmupdf.HEAPF32[_wasm_color] as number
]
if (n === 3)
return [
libmupdf.HEAPF32[_wasm_color + 0] as number,
libmupdf.HEAPF32[_wasm_color + 1] as number,
libmupdf.HEAPF32[_wasm_color + 2] as number,
]
if (n === 4)
return [
libmupdf.HEAPF32[_wasm_color + 0] as number,
libmupdf.HEAPF32[_wasm_color + 1] as number,
libmupdf.HEAPF32[_wasm_color + 2] as number,
libmupdf.HEAPF32[_wasm_color + 3] as number,
]
throw new TypeError("invalid number of components for Color: " + n)
}
function fromString(ptr: Pointer<"char">): string {
return libmupdf.UTF8ToString(ptr)
}
function fromStringFree(ptr: Pointer<"char">): string {
let str = libmupdf.UTF8ToString(ptr)
Free(ptr)
return str
}
function fromPoint(ptr: Pointer<"fz_point">): Point {
let addr = ptr >> 2
return [
libmupdf.HEAPF32[addr + 0] as number,
libmupdf.HEAPF32[addr + 1] as number,
]
}
function fromRect(ptr: Pointer<"fz_rect">): Rect {
let addr = ptr >> 2
return [
libmupdf.HEAPF32[addr + 0] as number,
libmupdf.HEAPF32[addr + 1] as number,
libmupdf.HEAPF32[addr + 2] as number,
libmupdf.HEAPF32[addr + 3] as number,
]
}
function fromMatrix(ptr: Pointer<"fz_matrix">): Matrix {
let addr = ptr >> 2
return [
libmupdf.HEAPF32[addr + 0] as number,
libmupdf.HEAPF32[addr + 1] as number,
libmupdf.HEAPF32[addr + 2] as number,
libmupdf.HEAPF32[addr + 3] as number,
libmupdf.HEAPF32[addr + 4] as number,
libmupdf.HEAPF32[addr + 5] as number,
]
}
function fromQuad(ptr: Pointer<"fz_quad">): Quad {
let addr = ptr >> 2
return [
libmupdf.HEAPF32[addr + 0] as number,
libmupdf.HEAPF32[addr + 1] as number,
libmupdf.HEAPF32[addr + 2] as number,
libmupdf.HEAPF32[addr + 3] as number,
libmupdf.HEAPF32[addr + 4] as number,
libmupdf.HEAPF32[addr + 5] as number,
libmupdf.HEAPF32[addr + 6] as number,
libmupdf.HEAPF32[addr + 7] as number,
]
}
function fromBuffer(ptr: Pointer<"fz_buffer">): Uint8Array {
let data = libmupdf._wasm_buffer_get_data(ptr)
let size = libmupdf._wasm_buffer_get_len(ptr)
return libmupdf.HEAPU8.slice(data, data + size)
}
/* -------------------------------------------------------------------------- */
type SearchFunction = (
display_list: any,
needle: Pointer<"char">,
marks: Pointer<"int">,
hits: Pointer<"fz_quad">,
hit_max: number
) => number
function runSearch(searchFun: SearchFunction, searchThis: number, needle: string, max_hits = 500) {
checkType(needle, "string")
let hits = 0 as Pointer<"fz_quad">
let marks = 0 as Pointer<"int">
try {
hits = Malloc<"fz_quad">(32 * max_hits)
marks = Malloc<"int">(4 * max_hits)
let n = searchFun(searchThis as any, STRING(needle), marks, hits, max_hits)
let outer: Quad[][] = []
if (n > 0) {
let inner: Quad[] = []
for (let i = 0; i < n; ++i) {
let mark = libmupdf.HEAP32[(marks>>2) + i]
let quad = fromQuad(hits + i * 32 as Pointer<"fz_quad">)
if (i > 0 && mark) {
outer.push(inner)
inner = []
}
inner.push(quad)
}
outer.push(inner)
}
return outer
} finally {
Free(marks)
Free(hits)
}
}
/* -------------------------------------------------------------------------- */
abstract class Userdata<B> {
private static _finalizer: FinalizationRegistry<number>
static readonly _drop: (pointer: any) => void
pointer: Pointer<B>
constructor(pointer: Pointer<B>) {
if (typeof pointer !== "number")
throw new Error("invalid pointer: " + typeof pointer)
if (pointer !== 0) {
let ctor = this.constructor as typeof Userdata
if (!ctor._finalizer)
ctor._finalizer = new FinalizationRegistry(ctor._drop)
ctor._finalizer.register(this, pointer, this)
}
this.pointer = pointer
}
destroy() {
if (this.pointer !== 0) {
let ctor = this.constructor as typeof Userdata
ctor._finalizer.unregister(this)
ctor._drop(this.pointer)
}
this.pointer = 0 as Pointer<B>
}
// Custom "console.log" formatting for Node
[Symbol.for("nodejs.util.inspect.custom")]() {
return this.toString()
}
toString() {
return `[${this.constructor.name} ${this.pointer}]`
}
valueOf() {
throw new Error("cannot convert Userdata to Javascript value")
}
}
export class Buffer extends Userdata<"fz_buffer"> {
static override readonly _drop = libmupdf._wasm_drop_buffer
/** New empty Buffer. */
constructor()
/** New Buffer initialized with string contents as UTF-8. */
constructor(data: string)
/** New Buffer initialized with typed array contents. */
constructor(data: ArrayBuffer | Uint8Array)
/** PRIVATE */
constructor(pointer: Pointer<"fz_buffer">)
constructor(arg?: Pointer<"fz_buffer"> | string | ArrayBuffer | Uint8Array) {
if (typeof arg === "undefined")
super(libmupdf._wasm_new_buffer(1024))
else if (typeof arg === "number")
super(arg)
else if (typeof arg === "string") {
let data_len = libmupdf.lengthBytesUTF8(arg)
let data_ptr = Malloc<"char">(data_len + 1)
libmupdf.stringToUTF8(arg, data_ptr, data_len + 1)
super(libmupdf._wasm_new_buffer_from_data(data_ptr, data_len))
}
else if (arg instanceof ArrayBuffer || arg instanceof Uint8Array) {
let data_len = arg.byteLength
let data_ptr = Malloc<"char">(data_len)
libmupdf.HEAPU8.set(new Uint8Array(arg), data_ptr)
super(libmupdf._wasm_new_buffer_from_data(data_ptr, data_len))
}
}
getLength() {
return libmupdf._wasm_buffer_get_len(this.pointer)
}
readByte(at: number) {
let data = libmupdf._wasm_buffer_get_data(this.pointer)
return libmupdf.HEAPU8[data + at] as number
}
write(s: string) {
libmupdf._wasm_append_string(this.pointer, STRING(s))
}
writeByte(b: number) {
libmupdf._wasm_append_byte(this.pointer, b)
}
writeLine(s: string) {
this.write(s)
this.writeByte(10)
}
writeBuffer(other: AnyBuffer) {
libmupdf._wasm_append_buffer(this.pointer, BUFFER(other))
}
asUint8Array() {
let data = libmupdf._wasm_buffer_get_data(this.pointer)
let size = libmupdf._wasm_buffer_get_len(this.pointer)
return libmupdf.HEAPU8.subarray(data, data + size)
}
slice(start: number, end: number) {
return new Buffer(libmupdf._wasm_slice_buffer(this.pointer, start, end))
}
asString() {
return fromString(libmupdf._wasm_string_from_buffer(this.pointer))
}
}
type ColorSpaceType =
"None" |
"Gray" |
"RGB" |
"BGR" |
"CMYK" |
"Lab" |
"Indexed" |
"Separation"
export class ColorSpace extends Userdata<"fz_colorspace"> {
static override readonly _drop = libmupdf._wasm_drop_colorspace
static readonly COLORSPACE_TYPES: ColorSpaceType[] = [
"None",
"Gray",
"RGB",
"BGR",
"CMYK",
"Lab",
"Indexed",
"Separation"
]
// Create ColorSpace from ICC profile.
constructor(profile: AnyBuffer, name: string)
// PRIVATE
constructor(pointer: Pointer<"fz_colorspace">)
constructor(from: Pointer<"fz_colorspace"> | AnyBuffer, name?: string) {
if (typeof from === "number")
super(from)
else
super(libmupdf._wasm_new_icc_colorspace(STRING_OPT(name), BUFFER(from)))
}
getName() {
return fromString(libmupdf._wasm_colorspace_get_name(this.pointer))
}
getType() {
return ColorSpace.COLORSPACE_TYPES[libmupdf._wasm_colorspace_get_type(this.pointer)] || "None"
}
getNumberOfComponents() {
return libmupdf._wasm_colorspace_get_n(this.pointer)
}
isGray(): boolean { return this.getType() === "Gray" }
isRGB(): boolean { return this.getType() === "RGB" }
isCMYK(): boolean { return this.getType() === "CMYK" }
isIndexed(): boolean { return this.getType() === "Indexed" }
isLab(): boolean { return this.getType() === "Lab" }
isDeviceN(): boolean { return this.getType() === "Separation" }
isSubtractive(): boolean { return this.getType() === "CMYK" || this.getType() === "Separation" }
override toString() {
return "[ColorSpace " + this.getName() + "]"
}
static readonly DeviceGray = new ColorSpace(libmupdf._wasm_device_gray())
static readonly DeviceRGB = new ColorSpace(libmupdf._wasm_device_rgb())
static readonly DeviceBGR = new ColorSpace(libmupdf._wasm_device_bgr())
static readonly DeviceCMYK = new ColorSpace(libmupdf._wasm_device_cmyk())
static readonly Lab = new ColorSpace(libmupdf._wasm_device_lab())
}
type FontSimpleEncoding = "Latin" | "Greek" | "Cyrillic"
type FontCJKOrdering = 0 | 1 | 2 | 3
type FontCJKLanguage =
"Adobe-CNS1" |
"Adobe-GB1" |
"Adobe-Japan1" |
"Adobe-Korea1" |
"zh-Hant" |
"zh-TW" |
"zh-HK" |
"zh-Hans" |
"zh-CN" |
"ja" |
"ko"
export class Font extends Userdata<"fz_font"> {
static override readonly _drop = libmupdf._wasm_drop_font
static readonly SIMPLE_ENCODING: FontSimpleEncoding[] = [
"Latin",
"Greek",
"Cyrillic"
]
static readonly ADOBE_CNS = 0
static readonly ADOBE_GB = 1
static readonly ADOBE_JAPAN = 2
static readonly ADOBE_KOREA = 3
static readonly CJK_ORDERING_BY_LANG: Record<FontCJKLanguage,FontCJKOrdering> = {
"Adobe-CNS1": 0,
"Adobe-GB1": 1,
"Adobe-Japan1": 2,
"Adobe-Korea1": 3,
"zh-Hant": 0,
"zh-TW": 0,
"zh-HK": 0,
"zh-Hans": 1,
"zh-CN": 1,
"ja": 2,
"ko": 3,
}
// Create new Font from a font file.
constructor(name: string, data: AnyBuffer, subfont: number)
// PRIVATE
constructor(pointer: Pointer<"fz_font">)
constructor(name_or_pointer: Pointer<"fz_font"> | string, data?: AnyBuffer, subfont=0) {
let pointer = 0 as Pointer<"fz_font">
if (typeof name_or_pointer === "number") {
pointer = libmupdf._wasm_keep_font(name_or_pointer)
} else {
if (data)
pointer = libmupdf._wasm_new_font_from_buffer(STRING(name_or_pointer), BUFFER(data), subfont)
else
pointer = libmupdf._wasm_new_base14_font(STRING(name_or_pointer))
}
super(pointer)
}
getName() {
return fromString(libmupdf._wasm_font_get_name(this.pointer))
}
encodeCharacter(uni: number | string) {
if (typeof uni === "string")
uni = uni.charCodeAt(0)
return libmupdf._wasm_encode_character(this.pointer, uni)
}
advanceGlyph(gid: number, wmode = 0) {
return libmupdf._wasm_advance_glyph(this.pointer, gid, wmode)
}
isMono() {
return !!libmupdf._wasm_font_is_monospaced(this.pointer)
}
isSerif() {
return !!libmupdf._wasm_font_is_serif(this.pointer)
}
isBold() {
return !!libmupdf._wasm_font_is_bold(this.pointer)
}
isItalic() {
return !!libmupdf._wasm_font_is_italic(this.pointer)
}
}
export class Image extends Userdata<"fz_image"> {
static override readonly _drop = libmupdf._wasm_drop_image
constructor(pointer: Pointer<"fz_image">)
constructor(data: AnyBuffer)
constructor(pixmap: Pixmap, mask?: Image)
constructor(pixmap_or_buffer: Pointer<"fz_image"> | Pixmap | AnyBuffer, mask?: Image) {
let pointer = 0 as Pointer<"fz_image">
if (typeof pixmap_or_buffer === "number")
pointer = libmupdf._wasm_keep_image(pixmap_or_buffer)
else if (pixmap_or_buffer instanceof Pixmap)
pointer = libmupdf._wasm_new_image_from_pixmap(
pixmap_or_buffer.pointer,
mask ? mask.pointer : 0 as Pointer<"fz_image">
)
else
pointer = libmupdf._wasm_new_image_from_buffer(BUFFER(pixmap_or_buffer))
super(pointer)
}
getWidth() {
return libmupdf._wasm_image_get_w(this.pointer)
}
getHeight() {
return libmupdf._wasm_image_get_h(this.pointer)
}
getNumberOfComponents() {
return libmupdf._wasm_image_get_n(this.pointer)
}
getBitsPerComponent() {
return libmupdf._wasm_image_get_bpc(this.pointer)
}
getXResolution() {
return libmupdf._wasm_image_get_xres(this.pointer)
}
getYResolution() {
return libmupdf._wasm_image_get_yres(this.pointer)
}
getImageMask() {
return !!libmupdf._wasm_image_get_imagemask(this.pointer)
}
getColorSpace() {
let cs = libmupdf._wasm_image_get_colorspace(this.pointer)
if (cs)
return new ColorSpace(libmupdf._wasm_keep_colorspace(cs))
return null
}
getMask() {
let mask = libmupdf._wasm_image_get_mask(this.pointer)
if (mask)
return new Image(libmupdf._wasm_keep_image(mask))
return null
}
toPixmap() {
return new Pixmap(libmupdf._wasm_get_pixmap_from_image(this.pointer))
}
}
type LineCap = "Butt" | "Round" | "Square" | "Triangle"
type LineJoin = "Miter" | "Round" | "Bevel" | "MiterXPS"
// TODO: convert StrokeState from plain JS object to match mutool run ffi_pushstroke/ffi_tostroke
export class StrokeState extends Userdata<"fz_stroke_state"> {
static override readonly _drop = libmupdf._wasm_drop_stroke_state
static readonly LINE_CAP: LineCap[] = [
"Butt",
"Round",
"Square",
"Triangle"
]
static readonly LINE_JOIN: LineJoin[] = [
"Miter",
"Round",
"Bevel",
"MiterXPS"
]
constructor(pointer?: Pointer<"fz_stroke_state">) {
if (typeof pointer === "number")
super(pointer)
else
super(libmupdf._wasm_new_stroke_state())
}
getLineCap() {
return libmupdf._wasm_stroke_state_get_start_cap(this.pointer)
}
setLineCap(j: LineCap) {
let jj = ENUM<LineCap>(j, StrokeState.LINE_CAP)
libmupdf._wasm_stroke_state_set_start_cap(this.pointer, jj)
libmupdf._wasm_stroke_state_set_dash_cap(this.pointer, jj)
libmupdf._wasm_stroke_state_set_end_cap(this.pointer, jj)
}
getLineJoin() {
return libmupdf._wasm_stroke_state_get_linejoin(this.pointer)
}
setLineJoin(j: LineJoin) {
let jj = ENUM<LineJoin>(j, StrokeState.LINE_JOIN)
libmupdf._wasm_stroke_state_set_linejoin(this.pointer, jj)
}
getLineWidth() {
return libmupdf._wasm_stroke_state_get_linewidth(this.pointer)
}
setLineWidth(w: number) {
libmupdf._wasm_stroke_state_set_linewidth(this.pointer, w)
}
getMiterLimit() {
return libmupdf._wasm_stroke_state_get_miterlimit(this.pointer)
}
setMiterLimit(m: number) {
libmupdf._wasm_stroke_state_set_miterlimit(this.pointer, m)
}
// TODO: dashes
}
export class Path extends Userdata<"fz_path"> {
static override readonly _drop = libmupdf._wasm_drop_path
constructor(pointer?: Pointer<"fz_path">) {
if (typeof pointer === "number")
super(pointer)
else
super(libmupdf._wasm_new_path())
}
getBounds(strokeState: StrokeState, transform: Matrix) {
if (strokeState !== null)
checkType(strokeState, StrokeState)
checkMatrix(transform)
return fromRect(libmupdf._wasm_bound_path(this.pointer, strokeState?.pointer, MATRIX(transform)))
}
moveTo(x: number, y: number) {
checkType(x, "number")
checkType(y, "number")
libmupdf._wasm_moveto(this.pointer, x, y)
}
lineTo(x: number, y: number) {
checkType(x, "number")
checkType(y, "number")
libmupdf._wasm_lineto(this.pointer, x, y)
}
curveTo(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) {
libmupdf._wasm_curveto(this.pointer, x1, y1, x2, y2, x3, y3)
}
curveToV(cx: number, cy: number, ex: number, ey: number) {
libmupdf._wasm_curvetov(this.pointer, cx, cy, ex, ey)
}
curveToY(cx: number, cy: number, ex: number, ey: number) {
libmupdf._wasm_curvetoy(this.pointer, cx, cy, ex, ey)
}
closePath() {
libmupdf._wasm_closepath(this.pointer)
}
rect(x1: number, y1: number, x2: number, y2: number) {
libmupdf._wasm_rectto(this.pointer, x1, y1, x2, y2)
}
transform(matrix: Matrix) {
checkMatrix(matrix)
libmupdf._wasm_transform_path(this.pointer, MATRIX(matrix))
}
walk(_walker: any) {
throw "TODO"
}
}
export class Text extends Userdata<"fz_text"> {
static override readonly _drop = libmupdf._wasm_drop_text
constructor(pointer?: Pointer<"fz_text">) {
if (typeof pointer === "number")
super(pointer)
else
super(libmupdf._wasm_new_text())
}