@u1f992/pdfdiff 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/.clang-format +3 -0
  2. package/.github/workflows/gh-pages.yml +6 -1
  3. package/LICENSE +68 -81
  4. package/dist/browser.js +1405 -3099
  5. package/dist/browser.js.map +1 -1
  6. package/dist/cli-png-worker.d.ts +13 -0
  7. package/dist/cli-png-worker.d.ts.map +1 -0
  8. package/dist/cli-png-worker.js +287 -0
  9. package/dist/cli-png-worker.js.map +1 -0
  10. package/dist/cli.js +401 -3110
  11. package/dist/cli.js.map +1 -1
  12. package/dist/core.wasm +0 -0
  13. package/dist/decode.d.ts +9 -0
  14. package/dist/decode.d.ts.map +1 -0
  15. package/dist/diff.d.ts +2 -1
  16. package/dist/diff.d.ts.map +1 -1
  17. package/dist/gs-wasm/gs.js +5821 -0
  18. package/dist/gs-wasm/gs.wasm +0 -0
  19. package/dist/gs-wasm/index.js +120 -0
  20. package/dist/gs-wasm/index.js.map +1 -0
  21. package/dist/gs-wasm/worker.js +764 -0
  22. package/dist/gs-wasm/worker.js.map +1 -0
  23. package/dist/image.d.ts.map +1 -1
  24. package/dist/index.d.ts +3 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.html +6 -1
  27. package/dist/index.js +310 -3094
  28. package/dist/index.js.map +1 -1
  29. package/dist/iterable.d.ts.map +1 -1
  30. package/dist/jimp.d.ts +23 -1
  31. package/dist/jimp.d.ts.map +1 -1
  32. package/dist/pdf.d.ts +15 -4
  33. package/dist/pdf.d.ts.map +1 -1
  34. package/dist/perf.d.ts +16 -0
  35. package/dist/perf.d.ts.map +1 -0
  36. package/dist/rgba-color.d.ts.map +1 -1
  37. package/dist/squoosh_png_bg.wasm +0 -0
  38. package/dist/style.css +12 -0
  39. package/dist/transferable.d.ts +11 -0
  40. package/dist/transferable.d.ts.map +1 -0
  41. package/dist/version.d.ts +1 -1
  42. package/dist/worker.d.ts +8 -8
  43. package/dist/worker.d.ts.map +1 -1
  44. package/dist/worker.js +144 -3210
  45. package/dist/worker.js.map +1 -1
  46. package/package.json +11 -4
  47. package/rollup.config.js +83 -5
  48. package/scripts/build-wasm.sh +32 -0
  49. package/src/browser.ts +122 -9
  50. package/src/cli-png-worker.ts +42 -0
  51. package/src/cli.ts +113 -34
  52. package/src/decode.ts +15 -0
  53. package/src/diff.ts +99 -51
  54. package/src/image.ts +4 -18
  55. package/src/index.html +6 -1
  56. package/src/index.test.ts +10 -18
  57. package/src/index.ts +176 -76
  58. package/src/iterable.test.ts +0 -17
  59. package/src/iterable.ts +0 -17
  60. package/src/jimp.ts +25 -7
  61. package/src/pdf.ts +99 -62
  62. package/src/perf.ts +77 -0
  63. package/src/rgba-color.test.ts +0 -17
  64. package/src/rgba-color.ts +0 -17
  65. package/src/style.css +12 -0
  66. package/src/transferable.ts +15 -0
  67. package/src/worker.ts +106 -100
  68. package/wasm/Makefile +34 -0
  69. package/wasm/bindings.cpp +76 -0
  70. package/wasm/core.c +176 -0
  71. package/wasm/core.h +69 -0
  72. package/dist/mupdf-wasm.wasm +0 -0
package/dist/cli.js CHANGED
@@ -2,6 +2,7 @@
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import util$2 from 'node:util';
5
+ import { Worker as Worker$1 } from 'node:worker_threads';
5
6
  import require$$0, { promises, existsSync } from 'fs';
6
7
  import require$$0$1 from 'util';
7
8
  import require$$1 from 'stream';
@@ -9,6 +10,7 @@ import require$$1$1 from 'zlib';
9
10
  import require$$0$2 from 'assert';
10
11
  import require$$3 from 'buffer';
11
12
  import Worker from 'web-worker';
13
+ import { gs } from '@u1f992/gs-wasm';
12
14
 
13
15
  var HeaderTypes;
14
16
  (function (HeaderTypes) {
@@ -36429,3000 +36431,66 @@ const Jimp = createJimp({
36429
36431
  plugins: defaultPlugins,
36430
36432
  });
36431
36433
 
36432
- var _=async function(_={}){var a,e,t=_,s=new Promise(((_,t)=>{a=_,e=t;})),n="object"==typeof window,i="undefined"!=typeof WorkerGlobalScope,m="object"==typeof process&&"object"==typeof process.versions&&"string"==typeof process.versions.node&&"renderer"!=process.type;if(m){const{createRequire:_}=await import('module');var r=_(import.meta.url);}var o,p,d="./this.program",w=(_,a)=>{throw a},f=import.meta.url,l="";if(m){var c=r("fs"),g=r("path");f.startsWith("file:")&&(l=g.dirname(r("url").fileURLToPath(f))+"/"),p=_=>(_=U(_)?new URL(_):_,c.readFileSync(_)),o=async(_,a=true)=>(_=U(_)?new URL(_):_,c.readFileSync(_,a?void 0:"utf8")),process.argv.length>1&&(d=process.argv[1].replace(/\\/g,"/")),process.argv.slice(2),w=(_,a)=>{throw process.exitCode=_,a};}else if(n||i){try{l=new URL(".",f).href;}catch{}i&&(p=_=>{var a=new XMLHttpRequest;return a.open("GET",_,false),a.responseType="arraybuffer",a.send(null),new Uint8Array(a.response)}),o=async _=>{if(U(_))return new Promise(((a,e)=>{var t=new XMLHttpRequest;t.open("GET",_,true),t.responseType="arraybuffer",t.onload=()=>{200==t.status||0==t.status&&t.response?a(t.response):e(t.status);},t.onerror=e,t.send(null);}));var a=await fetch(_,{credentials:"same-origin"});if(a.ok)return a.arrayBuffer();throw new Error(a.status+" : "+a.url)};}var u,h,b,v,y,x,k,T=console.log.bind(console),j=console.error.bind(console),$=false,A=false,U=_=>_.startsWith("file://");function R(){var _=h.buffer;t.HEAPU8=b=new Uint8Array(_),t.HEAP32=v=new Int32Array(_),t.HEAPU32=y=new Uint32Array(_),t.HEAPF32=new Float32Array(_),k=new Float64Array(_),x=new BigInt64Array(_),new BigUint64Array(_);}var E,C=0,S=null;function M(){return t.locateFile?(_="mupdf-wasm.wasm",t.locateFile?t.locateFile(_,l):l+_):new URL("mupdf-wasm.wasm",import.meta.url).href;var _;}async function D(_){if(!u)try{var a=await o(_);return new Uint8Array(a)}catch{}return function(_){if(_==E&&u)return new Uint8Array(u);if(p)return p(_);throw "both async and sync fetching of the wasm failed"}(_)}async function F(_,a){try{var s=await D(_);return await WebAssembly.instantiate(s,a)}catch(_){j(`failed to asynchronously prepare wasm: ${_}`),function(_){t.onAbort?.(_),j(_="Aborted("+_+")"),$=true,_+=". Build with -sASSERTIONS for more info.",A&&r_();var a=new WebAssembly.RuntimeError(_);throw e(a),a}(_);}}class P{name="ExitStatus";constructor(_){this.message=`Program terminated with exit(${_})`,this.status=_;}}var I=_=>{for(;_.length>0;)_.shift()(t);},B=[],q=_=>B.push(_),L=[],W=_=>L.push(_),z=true,H=new TextDecoder,N=(_,a)=>{if(!_)return "";for(var e=_+a,t=_;!(t>=e)&&b[t];)++t;return H.decode(b.subarray(_,t))},G=_=>_<-9007199254740992||_>9007199254740992?NaN:Number(_),Y=(_,a,e)=>((_,a,e,t)=>{if(!(t>0))return 0;for(var s=e,n=e+t-1,i=0;i<_.length;++i){var m=_.charCodeAt(i);if(m>=55296&&m<=57343&&(m=65536+((1023&m)<<10)|1023&_.charCodeAt(++i)),m<=127){if(e>=n)break;a[e++]=m;}else if(m<=2047){if(e+1>=n)break;a[e++]=192|m>>6,a[e++]=128|63&m;}else if(m<=65535){if(e+2>=n)break;a[e++]=224|m>>12,a[e++]=128|m>>6&63,a[e++]=128|63&m;}else {if(e+3>=n)break;a[e++]=240|m>>18,a[e++]=128|m>>12&63,a[e++]=128|m>>6&63,a[e++]=128|63&m;}}return a[e]=0,e-s})(_,b,a,e),X=()=>Date.now(),J=[],K=(_,a,e)=>{var t=((_,a)=>{var e;for(J.length=0;e=b[_++];){var t=105!=e;a+=(t&=112!=e)&&a%8?4:0,J.push(112==e?y[a>>2]:106==e?x[a>>3]:105==e?v[a>>2]:k[a>>3]),a+=t?8:4;}return J})(a,e);return n_[_](...t)},Q=_=>{var a=(_-h.buffer.byteLength+65535)/65536|0;try{return h.grow(a),R(),1}catch(_){}},V={},Z=()=>{if(!Z.strings){var _={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:("object"==typeof navigator&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:d||"./this.program"};for(var a in V) void 0===V[a]?delete _[a]:_[a]=V[a];var e=[];for(var a in _)e.push(`${a}=${_[a]}`);Z.strings=e;}return Z.strings},__=_=>{for(var a=0,e=0;e<_.length;++e){var t=_.charCodeAt(e);t<=127?a++:t<=2047?a+=2:t>=55296&&t<=57343?(a+=4,++e):a+=3;}return a},a_=_=>{z||(t.onExit?.(_),$=true),w(_,new P(_));},e_=(_,a)=>{a_(_);},t_=[null,[],[]],s_=(_,a)=>{var e=t_[_];0===a||10===a?((1===_?T:j)(((_,a=0,e=NaN)=>{for(var t=a+e,s=a;_[s]&&!(s>=t);)++s;return H.decode(_.buffer?_.subarray(a,s):new Uint8Array(_.slice(a,s)))})(e)),e.length=0):e.push(a);};t.noExitRuntime&&(z=t.noExitRuntime),t.print&&(T=t.print),t.printErr&&(j=t.printErr),t.wasmBinary&&(u=t.wasmBinary),t.arguments&&t.arguments,t.thisProgram&&(d=t.thisProgram),t.UTF8ToString=N,t.stringToUTF8=Y,t.lengthBytesUTF8=__;var n_={6015984:()=>{throw "TRYLATER"},6016006:()=>{throw "ABORT"},6016025:_=>{throw new Error(N(_))},6016064:()=>{throw new Error("Cannot create MuPDF context!")},6016117:(_,a,e,t,s,n,i)=>{globalThis.$libmupdf_text_walk.begin_span(_,a,e,t,s,n,i);},6016191:(_,a,e,t,s,n,i,m)=>{globalThis.$libmupdf_text_walk.show_glyph(_,a,e,t,s,n,i,m);},6016269:_=>{globalThis.$libmupdf_text_walk.end_span(_);},6016317:(_,a,e,t)=>globalThis.$libmupdf_load_font_file(_,a,e,t),6016380:(_,a,e,t)=>globalThis.$libmupdf_stm_read(_,a,e,t),6016438:_=>{globalThis.$libmupdf_stm_close(_);},6016478:(_,a,e,t)=>globalThis.$libmupdf_stm_seek(_,a,e,t),6016536:(_,a,e)=>{globalThis.$libmupdf_path_walk.moveto(_,a,e);},6016590:(_,a,e)=>{globalThis.$libmupdf_path_walk.lineto(_,a,e);},6016644:(_,a,e,t,s,n,i)=>{globalThis.$libmupdf_path_walk.curveto(_,a,e,t,s,n,i);},6016715:_=>{globalThis.$libmupdf_path_walk.closepath(_);},6016764:_=>{globalThis.$libmupdf_device.close_device(_);},6016813:_=>{globalThis.$libmupdf_device.drop_device(_);},6016861:(_,a,e,t,s,n,i,m)=>{globalThis.$libmupdf_device.fill_path(_,a,e,t,s,n,i,m);},6016935:(_,a,e,t,s,n,i,m)=>{globalThis.$libmupdf_device.stroke_path(_,a,e,t,s,n,i,m);},6017011:(_,a,e,t)=>{globalThis.$libmupdf_device.clip_path(_,a,e,t);},6017069:(_,a,e,t)=>{globalThis.$libmupdf_device.clip_stroke_path(_,a,e,t);},6017134:(_,a,e,t,s,n,i)=>{globalThis.$libmupdf_device.fill_text(_,a,e,t,s,n,i);},6017204:(_,a,e,t,s,n,i,m)=>{globalThis.$libmupdf_device.stroke_text(_,a,e,t,s,n,i,m);},6017280:(_,a,e)=>{globalThis.$libmupdf_device.clip_text(_,a,e);},6017334:(_,a,e,t)=>{globalThis.$libmupdf_device.clip_stroke_text(_,a,e,t);},6017399:(_,a,e)=>{globalThis.$libmupdf_device.ignore_text(_,a,e);},6017455:(_,a,e,t)=>{globalThis.$libmupdf_device.fill_shade(_,a,e,t);},6017514:(_,a,e,t)=>{globalThis.$libmupdf_device.fill_image(_,a,e,t);},6017573:(_,a,e,t,s,n,i)=>{globalThis.$libmupdf_device.fill_image_mask(_,a,e,t,s,n,i);},6017649:(_,a,e)=>{globalThis.$libmupdf_device.clip_image_mask(_,a,e);},6017709:_=>{globalThis.$libmupdf_device.pop_clip(_);},6017754:(_,a,e,t,s,n)=>{globalThis.$libmupdf_device.begin_mask(_,a,e,t,s,n);},6017821:(_,a)=>{globalThis.$libmupdf_device.end_mask(_,a);},6017870:(_,a,e,t,s,n,i)=>{globalThis.$libmupdf_device.begin_group(_,a,e,t,s,n,i);},6017942:_=>{globalThis.$libmupdf_device.end_group(_);},6017988:(_,a,e,t,s,n,i)=>globalThis.$libmupdf_device.begin_tile(_,a,e,t,s,n,i),6018066:_=>{globalThis.$libmupdf_device.end_tile(_);},6018111:(_,a)=>{globalThis.$libmupdf_device.begin_layer(_,a);},6018163:_=>{globalThis.$libmupdf_device.end_layer(_);}},i_={g:function(_,a,e){return 0},u:function(_,a){},t:function(_,a,e){return 0},f:function(_,a,e,t){},s:_=>{},e:(_,a,e)=>{},m:function(_,a){_=G(_);var e=new Date(1e3*_);v[a>>2]=e.getUTCSeconds(),v[a+4>>2]=e.getUTCMinutes(),v[a+8>>2]=e.getUTCHours(),v[a+12>>2]=e.getUTCDate(),v[a+16>>2]=e.getUTCMonth(),v[a+20>>2]=e.getUTCFullYear()-1900,v[a+24>>2]=e.getUTCDay();var t=Date.UTC(e.getUTCFullYear(),0,1,0,0,0,0),s=(e.getTime()-t)/864e5|0;v[a+28>>2]=s;},l:function(_){var a=(()=>{var a=Date.UTC(v[_+20>>2]+1900,v[_+16>>2],v[_+12>>2],v[_+8>>2],v[_+4>>2],v[_>>2],0),e=new Date(a);v[_+24>>2]=e.getUTCDay();var t=Date.UTC(e.getUTCFullYear(),0,1,0,0,0,0),s=(e.getTime()-t)/864e5|0;return v[_+28>>2]=s,e.getTime()/1e3})();return BigInt(a)},k:(_,a,e,t)=>{var s=(new Date).getFullYear(),n=new Date(s,0,1),i=new Date(s,6,1),m=n.getTimezoneOffset(),r=i.getTimezoneOffset(),o=Math.max(m,r);y[_>>2]=60*o,v[a>>2]=Number(m!=r);var p=_=>{var a=_>=0?"-":"+",e=Math.abs(_);return `UTC${a}${String(Math.floor(e/60)).padStart(2,"0")}${String(e%60).padStart(2,"0")}`},d=p(m),w=p(r);r<m?(Y(d,e,17),Y(w,t,17)):(Y(d,t,17),Y(w,e,17));},r:function(_,a,e){if(!((t=_)>=0&&t<=3))return 28;var t,s;s=0===_?X():performance.now();var n=Math.round(1e3*s*1e3);return x[e>>3]=BigInt(n),0},a:(_,a,e)=>K(_,a,e),j:(_,a,e)=>K(_,a,e),i:X,h:_=>{var a,e,t=b.length,s=2147483648;if((_>>>=0)>s)return false;for(var n=1;n<=4;n*=2){var i=t*(1+.2/n);i=Math.min(i,_+100663296);var m=Math.min(s,(a=Math.max(_,i),e=65536,Math.ceil(a/e)*e));if(Q(m))return true}return false},q:(_,a)=>{var e=0,t=0;for(var s of Z()){var n=a+e;y[_+t>>2]=n,e+=Y(s,n,1/0)+1,t+=4;}return 0},p:(_,a)=>{var e=Z();y[_>>2]=e.length;var t=0;for(var s of e)t+=__(s)+1;return y[a>>2]=t,0},b:e_,d:_=>52,o:(_,a,e,t)=>52,n:function(_,a,e,t){return 70},c:(_,a,e,t)=>{for(var s=0,n=0;n<e;n++){var i=y[a>>2],m=y[a+4>>2];a+=8;for(var r=0;r<m;r++)s_(_,b[i+r]);s+=m;}return y[t>>2]=s,0}},m_=await async function(){function _(_,a){return m_=_.exports,h=m_.v,R(),function(){if(C--,t.monitorRunDependencies?.(C),0==C&&S){var _=S;S=null,_();}}(),m_}C++,t.monitorRunDependencies?.(C);var a={a:i_};if(t.instantiateWasm)return new Promise(((e,s)=>{t.instantiateWasm(a,((a,t)=>{e(_(a));}));}));E??=M();try{var s=function(a){return _(a.instance)}(await async function(_,a,e){if(!_&&"function"==typeof WebAssembly.instantiateStreaming&&!U(a)&&!m)try{var t=fetch(a,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(t,e)}catch(_){j(`wasm streaming compile failed: ${_}`),j("falling back to ArrayBuffer instantiation");}return F(a,e)}(u,E,a));return s}catch(_){return e(_),Promise.reject(_)}}(),r_=(m_.w,t._wasm_init_context=m_.x,t._wasm_malloc=m_.y,t._wasm_free=m_.z,t._wasm_enable_icc=m_.A,t._wasm_disable_icc=m_.B,t._wasm_set_user_css=m_.C,t._wasm_Memento_checkAllMemory=m_.D,t._wasm_Memento_listBlocks=m_.E,t._wasm_keep_buffer=m_.F,t._wasm_drop_buffer=m_.G,t._wasm_keep_stream=m_.H,t._wasm_drop_stream=m_.I,t._wasm_keep_colorspace=m_.J,t._wasm_drop_colorspace=m_.K,t._wasm_keep_pixmap=m_.L,t._wasm_drop_pixmap=m_.M,t._wasm_keep_font=m_.N,t._wasm_drop_font=m_.O,t._wasm_keep_stroke_state=m_.P,t._wasm_drop_stroke_state=m_.Q,t._wasm_keep_image=m_.R,t._wasm_drop_image=m_.S,t._wasm_keep_shade=m_.T,t._wasm_drop_shade=m_.U,t._wasm_keep_path=m_.V,t._wasm_drop_path=m_.W,t._wasm_keep_text=m_.X,t._wasm_drop_text=m_.Y,t._wasm_keep_device=m_.Z,t._wasm_drop_device=m_._,t._wasm_keep_display_list=m_.$,t._wasm_drop_display_list=m_.aa,t._wasm_drop_stext_page=m_.ba,t._wasm_drop_document_writer=m_.ca,t._wasm_drop_outline_iterator=m_.da,t._wasm_keep_document=m_.ea,t._wasm_drop_document=m_.fa,t._wasm_keep_page=m_.ga,t._wasm_drop_page=m_.ha,t._wasm_keep_link=m_.ia,t._wasm_drop_link=m_.ja,t._wasm_keep_outline=m_.ka,t._wasm_drop_outline=m_.la,t._wasm_pdf_keep_annot=m_.ma,t._wasm_pdf_drop_annot=m_.na,t._wasm_pdf_keep_obj=m_.oa,t._wasm_pdf_drop_obj=m_.pa,t._wasm_pdf_keep_graft_map=m_.qa,t._wasm_pdf_drop_graft_map=m_.ra,t._wasm_buffer_get_data=m_.sa,t._wasm_buffer_get_len=m_.ta,t._wasm_colorspace_get_type=m_.ua,t._wasm_colorspace_get_n=m_.va,t._wasm_colorspace_get_name=m_.wa,t._wasm_pixmap_get_w=m_.xa,t._wasm_pixmap_get_h=m_.ya,t._wasm_pixmap_get_x=m_.za,t._wasm_pixmap_get_y=m_.Aa,t._wasm_pixmap_get_n=m_.Ba,t._wasm_pixmap_get_stride=m_.Ca,t._wasm_pixmap_get_alpha=m_.Da,t._wasm_pixmap_get_xres=m_.Ea,t._wasm_pixmap_get_yres=m_.Fa,t._wasm_pixmap_get_colorspace=m_.Ga,t._wasm_pixmap_get_samples=m_.Ha,t._wasm_pixmap_set_xres=m_.Ia,t._wasm_pixmap_set_yres=m_.Ja,t._wasm_font_get_name=m_.Ka,t._wasm_stroke_state_get_start_cap=m_.La,t._wasm_stroke_state_set_start_cap=m_.Ma,t._wasm_stroke_state_get_dash_cap=m_.Na,t._wasm_stroke_state_set_dash_cap=m_.Oa,t._wasm_stroke_state_get_end_cap=m_.Pa,t._wasm_stroke_state_set_end_cap=m_.Qa,t._wasm_stroke_state_get_linejoin=m_.Ra,t._wasm_stroke_state_set_linejoin=m_.Sa,t._wasm_stroke_state_get_linewidth=m_.Ta,t._wasm_stroke_state_set_linewidth=m_.Ua,t._wasm_stroke_state_get_miterlimit=m_.Va,t._wasm_stroke_state_set_miterlimit=m_.Wa,t._wasm_stroke_state_get_dash_phase=m_.Xa,t._wasm_stroke_state_set_dash_phase=m_.Ya,t._wasm_stroke_state_get_dash_len=m_.Za,t._wasm_image_get_w=m_._a,t._wasm_image_get_h=m_.$a,t._wasm_image_get_n=m_.ab,t._wasm_image_get_bpc=m_.bb,t._wasm_image_get_xres=m_.cb,t._wasm_image_get_yres=m_.db,t._wasm_image_get_imagemask=m_.eb,t._wasm_image_get_colorspace=m_.fb,t._wasm_image_get_mask=m_.gb,t._wasm_outline_get_title=m_.hb,t._wasm_outline_get_uri=m_.ib,t._wasm_outline_get_next=m_.jb,t._wasm_outline_get_down=m_.kb,t._wasm_outline_get_is_open=m_.lb,t._wasm_outline_item_get_title=m_.mb,t._wasm_outline_item_get_uri=m_.nb,t._wasm_outline_item_get_is_open=m_.ob,t._wasm_link_get_rect=m_.pb,t._wasm_link_get_uri=m_.qb,t._wasm_link_get_next=m_.rb,t._wasm_stext_page_get_mediabox=m_.sb,t._wasm_stext_page_get_first_block=m_.tb,t._wasm_stext_block_get_next=m_.ub,t._wasm_stext_block_get_type=m_.vb,t._wasm_stext_block_get_bbox=m_.wb,t._wasm_stext_block_get_first_line=m_.xb,t._wasm_stext_block_get_transform=m_.yb,t._wasm_stext_block_get_image=m_.zb,t._wasm_stext_line_get_next=m_.Ab,t._wasm_stext_line_get_wmode=m_.Bb,t._wasm_stext_line_get_dir=m_.Cb,t._wasm_stext_line_get_bbox=m_.Db,t._wasm_stext_line_get_first_char=m_.Eb,t._wasm_stext_char_get_next=m_.Fb,t._wasm_stext_char_get_c=m_.Gb,t._wasm_stext_char_get_origin=m_.Hb,t._wasm_stext_char_get_quad=m_.Ib,t._wasm_stext_char_get_size=m_.Jb,t._wasm_stext_char_get_font=m_.Kb,t._wasm_stext_char_get_argb=m_.Lb,t._wasm_link_dest_get_chapter=m_.Mb,t._wasm_link_dest_get_page=m_.Nb,t._wasm_link_dest_get_type=m_.Ob,t._wasm_link_dest_get_x=m_.Pb,t._wasm_link_dest_get_y=m_.Qb,t._wasm_link_dest_get_w=m_.Rb,t._wasm_link_dest_get_h=m_.Sb,t._wasm_link_dest_get_zoom=m_.Tb,t._wasm_pdf_filespec_params_get_filename=m_.Ub,t._wasm_pdf_filespec_params_get_mimetype=m_.Vb,t._wasm_pdf_filespec_params_get_size=m_.Wb,t._wasm_pdf_filespec_params_get_created=m_.Xb,t._wasm_pdf_filespec_params_get_modified=m_.Yb,t._wasm_pdf_page_get_obj=m_.Zb,t._wasm_new_buffer=m_._b,t._wasm_new_buffer_from_data=m_.$b,t._wasm_append_string=m_.ac,t._wasm_append_byte=m_.bc,t._wasm_append_buffer=m_.cc,t._wasm_slice_buffer=m_.dc,t._wasm_string_from_buffer=m_.ec,t._wasm_device_gray=m_.fc,t._wasm_device_rgb=m_.gc,t._wasm_device_bgr=m_.hc,t._wasm_device_cmyk=m_.ic,t._wasm_device_lab=m_.jc,t._wasm_new_icc_colorspace=m_.kc,t._wasm_new_stroke_state=m_.lc,t._wasm_stroke_state_get_dash_item=m_.mc,t._wasm_stroke_state_set_dash_item=m_.nc,t._wasm_new_base14_font=m_.oc,t._wasm_new_cjk_font=m_.pc,t._wasm_new_font_from_buffer=m_.qc,t._wasm_encode_character=m_.rc,t._wasm_advance_glyph=m_.sc,t._wasm_font_is_monospaced=m_.tc,t._wasm_font_is_serif=m_.uc,t._wasm_font_is_bold=m_.vc,t._wasm_font_is_italic=m_.wc,t._wasm_new_image_from_pixmap=m_.xc,t._wasm_new_image_from_buffer=m_.yc,t._wasm_get_pixmap_from_image=m_.zc,t._wasm_new_pixmap_from_page=m_.Ac,t._wasm_new_pixmap_from_page_contents=m_.Bc,t._wasm_pdf_new_pixmap_from_page_with_usage=m_.Cc,t._wasm_pdf_new_pixmap_from_page_contents_with_usage=m_.Dc,t._wasm_new_pixmap_with_bbox=m_.Ec,t._wasm_clear_pixmap=m_.Fc,t._wasm_clear_pixmap_with_value=m_.Gc,t._wasm_invert_pixmap=m_.Hc,t._wasm_invert_pixmap_luminance=m_.Ic,t._wasm_gamma_pixmap=m_.Jc,t._wasm_tint_pixmap=m_.Kc,t._wasm_new_buffer_from_pixmap_as_png=m_.Lc,t._wasm_new_buffer_from_pixmap_as_pam=m_.Mc,t._wasm_new_buffer_from_pixmap_as_psd=m_.Nc,t._wasm_new_buffer_from_pixmap_as_jpeg=m_.Oc,t._wasm_convert_pixmap=m_.Pc,t._wasm_warp_pixmap=m_.Qc,t._wasm_bound_shade=m_.Rc,t._wasm_new_display_list=m_.Sc,t._wasm_bound_display_list=m_.Tc,t._wasm_run_display_list=m_.Uc,t._wasm_new_pixmap_from_display_list=m_.Vc,t._wasm_new_stext_page_from_display_list=m_.Wc,t._wasm_search_display_list=m_.Xc,t._wasm_new_path=m_.Yc,t._wasm_moveto=m_.Zc,t._wasm_lineto=m_._c,t._wasm_curveto=m_.$c,t._wasm_curvetov=m_.ad,t._wasm_curvetoy=m_.bd,t._wasm_closepath=m_.cd,t._wasm_rectto=m_.dd,t._wasm_transform_path=m_.ed,t._wasm_bound_path=m_.fd,t._wasm_new_text=m_.gd,t._wasm_bound_text=m_.hd,t._wasm_show_glyph=m_.id,t._wasm_show_string=m_.jd,t._wasm_new_draw_device=m_.kd,t._wasm_new_display_list_device=m_.ld,t._wasm_close_device=m_.md,t._wasm_fill_path=m_.nd,t._wasm_stroke_path=m_.od,t._wasm_clip_path=m_.pd,t._wasm_clip_stroke_path=m_.qd,t._wasm_fill_text=m_.rd,t._wasm_stroke_text=m_.sd,t._wasm_clip_text=m_.td,t._wasm_clip_stroke_text=m_.ud,t._wasm_ignore_text=m_.vd,t._wasm_fill_shade=m_.wd,t._wasm_fill_image=m_.xd,t._wasm_fill_image_mask=m_.yd,t._wasm_clip_image_mask=m_.zd,t._wasm_pop_clip=m_.Ad,t._wasm_begin_mask=m_.Bd,t._wasm_end_mask=m_.Cd,t._wasm_begin_group=m_.Dd,t._wasm_end_group=m_.Ed,t._wasm_begin_tile=m_.Fd,t._wasm_end_tile=m_.Gd,t._wasm_begin_layer=m_.Hd,t._wasm_end_layer=m_.Id,t._wasm_new_document_writer_with_buffer=m_.Jd,t._wasm_begin_page=m_.Kd,t._wasm_end_page=m_.Ld,t._wasm_close_document_writer=m_.Md,t._wasm_print_stext_page_as_json=m_.Nd,t._wasm_search_stext_page=m_.Od,t._wasm_snap_selection=m_.Pd,t._wasm_copy_selection=m_.Qd,t._wasm_highlight_selection=m_.Rd,t._wasm_print_stext_page_as_html=m_.Sd,t._wasm_print_stext_page_as_text=m_.Td,t._wasm_open_document_with_buffer=m_.Ud,t._wasm_open_document_with_stream=m_.Vd,t._wasm_format_link_uri=m_.Wd,t._wasm_needs_password=m_.Xd,t._wasm_authenticate_password=m_.Yd,t._wasm_has_permission=m_.Zd,t._wasm_count_pages=m_._d,t._wasm_load_page=m_.$d,t._wasm_lookup_metadata=m_.ae,t._wasm_set_metadata=m_.be,t._wasm_resolve_link=m_.ce,t._wasm_resolve_link_dest=m_.de,t._wasm_load_outline=m_.ee,t._wasm_outline_get_page=m_.fe,t._wasm_layout_document=m_.ge,t._wasm_is_document_reflowable=m_.he,t._wasm_link_set_rect=m_.ie,t._wasm_link_set_uri=m_.je,t._wasm_bound_page=m_.ke,t._wasm_load_links=m_.le,t._wasm_create_link=m_.me,t._wasm_delete_link=m_.ne,t._wasm_run_page=m_.oe,t._wasm_run_page_contents=m_.pe,t._wasm_run_page_annots=m_.qe,t._wasm_run_page_widgets=m_.re,t._wasm_new_stext_page_from_page=m_.se,t._wasm_new_display_list_from_page=m_.te,t._wasm_new_display_list_from_page_contents=m_.ue,t._wasm_page_label=m_.ve,t._wasm_search_page=m_.we,t._wasm_new_outline_iterator=m_.xe,t._wasm_outline_iterator_next=m_.ye,t._wasm_outline_iterator_prev=m_.ze,t._wasm_outline_iterator_up=m_.Ae,t._wasm_outline_iterator_down=m_.Be,t._wasm_outline_iterator_delete=m_.Ce,t._wasm_outline_iterator_item=m_.De,t._wasm_outline_iterator_insert=m_.Ee,t._wasm_outline_iterator_update=m_.Fe,t._wasm_pdf_document_from_fz_document=m_.Ge,t._wasm_pdf_page_from_fz_page=m_.He,t._wasm_pdf_create_document=m_.Ie,t._wasm_pdf_version=m_.Je,t._wasm_pdf_was_repaired=m_.Ke,t._wasm_pdf_has_unsaved_changes=m_.Le,t._wasm_pdf_can_be_saved_incrementally=m_.Me,t._wasm_pdf_count_versions=m_.Ne,t._wasm_pdf_count_unsaved_versions=m_.Oe,t._wasm_pdf_validate_change_history=m_.Pe,t._wasm_pdf_enable_journal=m_.Qe,t._wasm_pdf_undoredo_state_position=m_.Re,t._wasm_pdf_undoredo_state_count=m_.Se,t._wasm_pdf_undoredo_step=m_.Te,t._wasm_pdf_begin_operation=m_.Ue,t._wasm_pdf_begin_implicit_operation=m_.Ve,t._wasm_pdf_end_operation=m_.We,t._wasm_pdf_abandon_operation=m_.Xe,t._wasm_pdf_undo=m_.Ye,t._wasm_pdf_redo=m_.Ze,t._wasm_pdf_can_undo=m_._e,t._wasm_pdf_can_redo=m_.$e,t._wasm_pdf_document_language=m_.af,t._wasm_pdf_set_document_language=m_.bf,t._wasm_pdf_trailer=m_.cf,t._wasm_pdf_xref_len=m_.df,t._wasm_pdf_lookup_page_obj=m_.ef,t._wasm_pdf_add_object=m_.ff,t._wasm_pdf_create_object=m_.gf,t._wasm_pdf_delete_object=m_.hf,t._wasm_pdf_add_stream=m_.jf,t._wasm_pdf_add_simple_font=m_.kf,t._wasm_pdf_add_cjk_font=m_.lf,t._wasm_pdf_add_cid_font=m_.mf,t._wasm_pdf_add_image=m_.nf,t._wasm_pdf_load_image=m_.of,t._wasm_pdf_add_page=m_.pf,t._wasm_pdf_insert_page=m_.qf,t._wasm_pdf_delete_page=m_.rf,t._wasm_pdf_set_page_labels=m_.sf,t._wasm_pdf_delete_page_labels=m_.tf,t._wasm_pdf_is_embedded_file=m_.uf,t._wasm_pdf_get_filespec_params=m_.vf,t._wasm_pdf_add_embedded_file=m_.wf,t._wasm_pdf_load_embedded_file_contents=m_.xf,t._wasm_pdf_write_document_buffer=m_.yf,t._wasm_pdf_js_supported=m_.zf,t._wasm_pdf_enable_js=m_.Af,t._wasm_pdf_disable_js=m_.Bf,t._wasm_pdf_rearrange_pages=m_.Cf,t._wasm_pdf_subset_fonts=m_.Df,t._wasm_pdf_bake_document=m_.Ef,t._wasm_pdf_count_layers=m_.Ff,t._wasm_pdf_layer_name=m_.Gf,t._wasm_pdf_layer_is_enabled=m_.Hf,t._wasm_pdf_enable_layer=m_.If,t._wasm_pdf_page_transform=m_.Jf,t._wasm_pdf_set_page_box=m_.Kf,t._wasm_pdf_first_annot=m_.Lf,t._wasm_pdf_next_annot=m_.Mf,t._wasm_pdf_first_widget=m_.Nf,t._wasm_pdf_next_widget=m_.Of,t._wasm_pdf_create_annot=m_.Pf,t._wasm_pdf_delete_annot=m_.Qf,t._wasm_pdf_update_page=m_.Rf,t._wasm_pdf_redact_page=m_.Sf,t._wasm_pdf_new_graft_map=m_.Tf,t._wasm_pdf_graft_mapped_object=m_.Uf,t._wasm_pdf_graft_object=m_.Vf,t._wasm_pdf_graft_mapped_page=m_.Wf,t._wasm_pdf_graft_page=m_.Xf,t._wasm_pdf_bound_annot=m_.Yf,t._wasm_pdf_run_annot=m_.Zf,t._wasm_pdf_new_pixmap_from_annot=m_._f,t._wasm_pdf_new_display_list_from_annot=m_.$f,t._wasm_pdf_update_annot=m_.ag,t._wasm_pdf_annot_obj=m_.bg,t._wasm_pdf_annot_type=m_.cg,t._wasm_pdf_annot_flags=m_.dg,t._wasm_pdf_set_annot_flags=m_.eg,t._wasm_pdf_annot_contents=m_.fg,t._wasm_pdf_set_annot_contents=m_.gg,t._wasm_pdf_annot_author=m_.hg,t._wasm_pdf_set_annot_author=m_.ig,t._wasm_pdf_annot_creation_date=m_.jg,t._wasm_pdf_set_annot_creation_date=m_.kg,t._wasm_pdf_annot_modification_date=m_.lg,t._wasm_pdf_set_annot_modification_date=m_.mg,t._wasm_pdf_annot_border_width=m_.ng,t._wasm_pdf_set_annot_border_width=m_.og,t._wasm_pdf_annot_border_style=m_.pg,t._wasm_pdf_set_annot_border_style=m_.qg,t._wasm_pdf_annot_border_effect=m_.rg,t._wasm_pdf_set_annot_border_effect=m_.sg,t._wasm_pdf_annot_border_effect_intensity=m_.tg,t._wasm_pdf_set_annot_border_effect_intensity=m_.ug,t._wasm_pdf_annot_opacity=m_.vg,t._wasm_pdf_set_annot_opacity=m_.wg,t._wasm_pdf_annot_filespec=m_.xg,t._wasm_pdf_set_annot_filespec=m_.yg,t._wasm_pdf_annot_quadding=m_.zg,t._wasm_pdf_set_annot_quadding=m_.Ag,t._wasm_pdf_annot_is_open=m_.Bg,t._wasm_pdf_set_annot_is_open=m_.Cg,t._wasm_pdf_annot_hidden_for_editing=m_.Dg,t._wasm_pdf_set_annot_hidden_for_editing=m_.Eg,t._wasm_pdf_annot_icon_name=m_.Fg,t._wasm_pdf_set_annot_icon_name=m_.Gg,t._wasm_pdf_annot_intent=m_.Hg,t._wasm_pdf_set_annot_intent=m_.Ig,t._wasm_pdf_annot_callout_style=m_.Jg,t._wasm_pdf_set_annot_callout_style=m_.Kg,t._wasm_pdf_annot_line_leader=m_.Lg,t._wasm_pdf_set_annot_line_leader=m_.Mg,t._wasm_pdf_annot_line_leader_extension=m_.Ng,t._wasm_pdf_set_annot_line_leader_extension=m_.Og,t._wasm_pdf_annot_line_leader_offset=m_.Pg,t._wasm_pdf_set_annot_line_leader_offset=m_.Qg,t._wasm_pdf_annot_line_caption=m_.Rg,t._wasm_pdf_set_annot_line_caption=m_.Sg,t._wasm_pdf_annot_rich_defaults=m_.Tg,t._wasm_pdf_set_annot_rich_defaults=m_.Ug,t._wasm_pdf_annot_callout_point=m_.Vg,t._wasm_pdf_annot_line_caption_offset=m_.Wg,t._wasm_pdf_annot_rect=m_.Xg,t._wasm_pdf_annot_popup=m_.Yg,t._wasm_pdf_annot_quad_point_count=m_.Zg,t._wasm_pdf_annot_quad_point=m_._g,t._wasm_pdf_annot_vertex_count=m_.$g,t._wasm_pdf_annot_vertex=m_.ah,t._wasm_pdf_annot_ink_list_count=m_.bh,t._wasm_pdf_annot_ink_list_stroke_count=m_.ch,t._wasm_pdf_annot_ink_list_stroke_vertex=m_.dh,t._wasm_pdf_annot_rich_contents=m_.eh,t._wasm_pdf_annot_border_dash_count=m_.fh,t._wasm_pdf_annot_border_dash_item=m_.gh,t._wasm_pdf_annot_has_rect=m_.hh,t._wasm_pdf_annot_has_ink_list=m_.ih,t._wasm_pdf_annot_has_quad_points=m_.jh,t._wasm_pdf_annot_has_vertices=m_.kh,t._wasm_pdf_annot_has_line=m_.lh,t._wasm_pdf_annot_has_interior_color=m_.mh,t._wasm_pdf_annot_has_line_ending_styles=m_.nh,t._wasm_pdf_annot_has_border=m_.oh,t._wasm_pdf_annot_has_border_effect=m_.ph,t._wasm_pdf_annot_has_icon_name=m_.qh,t._wasm_pdf_annot_has_open=m_.rh,t._wasm_pdf_annot_has_author=m_.sh,t._wasm_pdf_annot_has_filespec=m_.th,t._wasm_pdf_annot_has_callout=m_.uh,t._wasm_pdf_annot_has_rich_contents=m_.vh,t._wasm_pdf_annot_language=m_.wh,t._wasm_pdf_set_annot_language=m_.xh,t._wasm_pdf_set_annot_popup=m_.yh,t._wasm_pdf_set_annot_rect=m_.zh,t._wasm_pdf_clear_annot_quad_points=m_.Ah,t._wasm_pdf_clear_annot_vertices=m_.Bh,t._wasm_pdf_clear_annot_ink_list=m_.Ch,t._wasm_pdf_clear_annot_border_dash=m_.Dh,t._wasm_pdf_add_annot_quad_point=m_.Eh,t._wasm_pdf_add_annot_vertex=m_.Fh,t._wasm_pdf_add_annot_ink_list_stroke=m_.Gh,t._wasm_pdf_add_annot_ink_list_stroke_vertex=m_.Hh,t._wasm_pdf_add_annot_border_dash_item=m_.Ih,t._wasm_pdf_annot_line_ending_styles_start=m_.Jh,t._wasm_pdf_annot_line_1=m_.Kh,t._wasm_pdf_annot_line_2=m_.Lh,t._wasm_pdf_set_annot_line=m_.Mh,t._wasm_pdf_set_annot_callout_point=m_.Nh,t._wasm_pdf_annot_callout_line=m_.Oh,t._wasm_pdf_set_annot_callout_line=m_.Ph,t._wasm_pdf_set_annot_line_caption_offset=m_.Qh,t._wasm_pdf_annot_line_ending_styles_end=m_.Rh,t._wasm_pdf_set_annot_line_ending_styles=m_.Sh,t._wasm_pdf_annot_color=m_.Th,t._wasm_pdf_annot_interior_color=m_.Uh,t._wasm_pdf_set_annot_color=m_.Vh,t._wasm_pdf_set_annot_interior_color=m_.Wh,t._wasm_pdf_set_annot_default_appearance=m_.Xh,t._wasm_pdf_annot_default_appearance_font=m_.Yh,t._wasm_pdf_annot_default_appearance_size=m_.Zh,t._wasm_pdf_annot_default_appearance_color=m_._h,t._wasm_pdf_set_annot_rich_contents=m_.$h,t._wasm_pdf_set_annot_stamp_image=m_.ai,t._wasm_pdf_set_annot_appearance_from_display_list=m_.bi,t._wasm_pdf_set_annot_appearance=m_.ci,t._wasm_pdf_apply_redaction=m_.di,t._wasm_pdf_reset_form=m_.ei,t._wasm_pdf_annot_field_type=m_.fi,t._wasm_pdf_annot_field_flags=m_.gi,t._wasm_pdf_annot_field_label=m_.hi,t._wasm_pdf_annot_field_value=m_.ii,t._wasm_pdf_load_field_name=m_.ji,t._wasm_pdf_annot_text_widget_max_len=m_.ki,t._wasm_pdf_set_annot_text_field_value=m_.li,t._wasm_pdf_set_annot_choice_field_value=m_.mi,t._wasm_pdf_annot_choice_field_option_count=m_.ni,t._wasm_pdf_annot_choice_field_option=m_.oi,t._wasm_pdf_toggle_widget=m_.pi,t._wasm_pdf_is_indirect=m_.qi,t._wasm_pdf_is_bool=m_.ri,t._wasm_pdf_is_int=m_.si,t._wasm_pdf_is_real=m_.ti,t._wasm_pdf_is_number=m_.ui,t._wasm_pdf_is_name=m_.vi,t._wasm_pdf_is_string=m_.wi,t._wasm_pdf_is_array=m_.xi,t._wasm_pdf_is_dict=m_.yi,t._wasm_pdf_is_stream=m_.zi,t._wasm_pdf_to_num=m_.Ai,t._wasm_pdf_to_bool=m_.Bi,t._wasm_pdf_to_real=m_.Ci,t._wasm_pdf_to_name=m_.Di,t._wasm_pdf_to_text_string=m_.Ei,t._wasm_pdf_new_indirect=m_.Fi,t._wasm_pdf_new_array=m_.Gi,t._wasm_pdf_new_dict=m_.Hi,t._wasm_pdf_new_bool=m_.Ii,t._wasm_pdf_new_int=m_.Ji,t._wasm_pdf_new_real=m_.Ki,t._wasm_pdf_new_name=m_.Li,t._wasm_pdf_new_text_string=m_.Mi,t._wasm_pdf_new_string=m_.Ni,t._wasm_pdf_resolve_indirect=m_.Oi,t._wasm_pdf_array_len=m_.Pi,t._wasm_pdf_array_get=m_.Qi,t._wasm_pdf_dict_get=m_.Ri,t._wasm_pdf_dict_len=m_.Si,t._wasm_pdf_dict_get_key=m_.Ti,t._wasm_pdf_dict_get_val=m_.Ui,t._wasm_pdf_dict_get_inheritable=m_.Vi,t._wasm_pdf_dict_gets=m_.Wi,t._wasm_pdf_dict_gets_inheritable=m_.Xi,t._wasm_pdf_dict_put=m_.Yi,t._wasm_pdf_dict_puts=m_.Zi,t._wasm_pdf_dict_del=m_._i,t._wasm_pdf_dict_dels=m_.$i,t._wasm_pdf_array_put=m_.aj,t._wasm_pdf_array_push=m_.bj,t._wasm_pdf_array_delete=m_.cj,t._wasm_pdf_sprint_obj=m_.dj,t._wasm_pdf_load_stream=m_.ej,t._wasm_pdf_load_raw_stream=m_.fj,t._wasm_pdf_update_object=m_.gj,t._wasm_pdf_update_stream=m_.hj,t._wasm_pdf_to_string=m_.ij,t._wasm_new_stream=m_.jj,t._wasm_walk_path=m_.kj,t._wasm_walk_text=m_.lj,t._wasm_new_js_device=m_.mj,m_.nj);return function(){if(t.preInit)for("function"==typeof t.preInit&&(t.preInit=[t.preInit]);t.preInit.length>0;)t.preInit.shift()();}(),function _(){function e(){t.calledRun=true,$||(A=true,m_.w(),a(t),t.onRuntimeInitialized?.(),function(){if(t.postRun)for("function"==typeof t.postRun&&(t.postRun=[t.postRun]);t.postRun.length;)q(t.postRun.shift());I(B);}());}C>0?S=_:(function(){if(t.preRun)for("function"==typeof t.preRun&&(t.preRun=[t.preRun]);t.preRun.length;)W(t.preRun.shift());I(L);}(),C>0?S=_:t.setStatus?(t.setStatus("Running..."),setTimeout((()=>{setTimeout((()=>t.setStatus("")),1),e();}),1)):e());}(),s};
36433
-
36434
- // Copyright (C) 2004-2025 Artifex Software, Inc.
36435
- //
36436
- // This file is part of MuPDF WASM Library.
36437
- //
36438
- // MuPDF is free software: you can redistribute it and/or modify it under the
36439
- // terms of the GNU Affero General Public License as published by the Free
36440
- // Software Foundation, either version 3 of the License, or (at your option)
36441
- // any later version.
36442
- //
36443
- // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
36444
- // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
36445
- // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
36446
- // details.
36447
- //
36448
- // You should have received a copy of the GNU Affero General Public License
36449
- // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
36450
- //
36451
- // Alternative licensing terms are available from the licensor.
36452
- // For commercial licensing, see <https://www.artifex.com/> or contact
36453
- // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
36454
- // CA 94129, USA, for further information.
36455
- var node_fs = null;
36456
- if (typeof process !== "undefined" && process.versions && process.versions.node)
36457
- node_fs = await import('node:fs');
36458
- const libmupdf = await _(globalThis["$libmupdf_wasm_Module"]);
36459
- libmupdf._wasm_init_context();
36460
- function Malloc(size) {
36461
- return libmupdf._wasm_malloc(size);
36462
- }
36463
- function Free(ptr) {
36464
- libmupdf._wasm_free(ptr);
36465
- }
36466
- /* -------------------------------------------------------------------------- */
36467
- // To pass Rect and Matrix as pointer arguments
36468
- const _wasm_int = Malloc(4);
36469
- const _wasm_point = Malloc(4 * 6) >> 2;
36470
- const _wasm_rect = Malloc(4 * 8) >> 2;
36471
- const _wasm_matrix = Malloc(4 * 6) >> 2;
36472
- const _wasm_color = Malloc(4 * 4) >> 2;
36473
- const _wasm_quad = Malloc(4 * 8) >> 2;
36474
- const _wasm_string = [0, 0];
36475
- function checkType(value, type) {
36476
- if (typeof type === "string" && typeof value !== type)
36477
- throw new TypeError("expected " + type);
36478
- if (typeof type === "function" && !(value instanceof type))
36479
- throw new TypeError("expected " + type.name);
36480
- }
36481
- function checkPoint(value) {
36482
- if (!Array.isArray(value) || value.length !== 2)
36483
- throw new TypeError("expected point");
36484
- }
36485
- function checkRect(value) {
36486
- if (!Array.isArray(value) || value.length !== 4)
36487
- throw new TypeError("expected rectangle");
36488
- }
36489
- function checkMatrix(value) {
36490
- if (!Array.isArray(value) || value.length !== 6)
36491
- throw new TypeError("expected matrix");
36492
- }
36493
- function checkQuad(value) {
36494
- if (!Array.isArray(value) || value.length !== 8)
36495
- throw new TypeError("expected quad");
36496
- }
36497
- function checkColor(value) {
36498
- if (!Array.isArray(value) || (value.length !== 1 && value.length !== 3 && value.length !== 4))
36499
- throw new TypeError("expected color array");
36500
- }
36501
- function checkAnnotColor(value) {
36502
- if (!Array.isArray(value) || (value.length !== 0 && value.length !== 1 && value.length !== 3 && value.length !== 4))
36503
- throw new TypeError("expected color array");
36504
- }
36505
- function BUFFER(input) {
36506
- if (input instanceof Buffer$1)
36507
- return input.pointer;
36508
- if (input instanceof ArrayBuffer || input instanceof Uint8Array)
36509
- return new Buffer$1(input).pointer;
36510
- if (typeof input === "string")
36511
- return new Buffer$1(input).pointer;
36512
- throw new TypeError("expected buffer");
36513
- }
36514
- function ENUM(value, list) {
36515
- if (typeof value === "number") {
36516
- if (value >= 0 && value < list.length)
36517
- return value;
36518
- }
36519
- if (typeof value === "string") {
36520
- let idx = list.indexOf(value);
36521
- if (idx >= 0)
36522
- return idx;
36523
- }
36524
- throw new TypeError(`invalid enum value ("${value}"; expected ${list.join(", ")})`);
36525
- }
36526
- function allocateUTF8(str) {
36527
- var size = libmupdf.lengthBytesUTF8(str) + 1;
36528
- var pointer = Malloc(size);
36529
- libmupdf.stringToUTF8(str, pointer, size);
36530
- return pointer;
36531
- }
36532
- function STRING_N(s, i) {
36533
- if (_wasm_string[i]) {
36534
- Free(_wasm_string[i]);
36535
- _wasm_string[i] = 0;
36536
- }
36537
- return _wasm_string[i] = allocateUTF8(s);
36538
- }
36539
- function STRING(s) {
36540
- return STRING_N(s, 0);
36541
- }
36542
- function STRING2(s) {
36543
- return STRING_N(s, 1);
36544
- }
36545
- function STRING_OPT(s) {
36546
- return typeof s === "string" ? STRING_N(s, 0) : 0;
36547
- }
36548
- function STRING2_OPT(s) {
36549
- return typeof s === "string" ? STRING_N(s, 1) : 0;
36550
- }
36551
- function POINT(p) {
36552
- libmupdf.HEAPF32[_wasm_point + 0] = p[0];
36553
- libmupdf.HEAPF32[_wasm_point + 1] = p[1];
36554
- return _wasm_point << 2;
36555
- }
36556
- function POINT2(p) {
36557
- libmupdf.HEAPF32[_wasm_point + 2] = p[0];
36558
- libmupdf.HEAPF32[_wasm_point + 3] = p[1];
36559
- return (_wasm_point + 2) << 2;
36560
- }
36561
- function POINT3(p) {
36562
- libmupdf.HEAPF32[_wasm_point + 4] = p[0];
36563
- libmupdf.HEAPF32[_wasm_point + 5] = p[1];
36564
- return (_wasm_point + 4) << 2;
36565
- }
36566
- function RECT(r) {
36567
- libmupdf.HEAPF32[_wasm_rect + 0] = r[0];
36568
- libmupdf.HEAPF32[_wasm_rect + 1] = r[1];
36569
- libmupdf.HEAPF32[_wasm_rect + 2] = r[2];
36570
- libmupdf.HEAPF32[_wasm_rect + 3] = r[3];
36571
- return _wasm_rect << 2;
36572
- }
36573
- function RECT2(r) {
36574
- libmupdf.HEAPF32[_wasm_rect + 4] = r[0];
36575
- libmupdf.HEAPF32[_wasm_rect + 5] = r[1];
36576
- libmupdf.HEAPF32[_wasm_rect + 6] = r[2];
36577
- libmupdf.HEAPF32[_wasm_rect + 7] = r[3];
36578
- return (_wasm_rect + 4) << 2;
36579
- }
36580
- function MATRIX(m) {
36581
- libmupdf.HEAPF32[_wasm_matrix + 0] = m[0];
36582
- libmupdf.HEAPF32[_wasm_matrix + 1] = m[1];
36583
- libmupdf.HEAPF32[_wasm_matrix + 2] = m[2];
36584
- libmupdf.HEAPF32[_wasm_matrix + 3] = m[3];
36585
- libmupdf.HEAPF32[_wasm_matrix + 4] = m[4];
36586
- libmupdf.HEAPF32[_wasm_matrix + 5] = m[5];
36587
- return _wasm_matrix << 2;
36588
- }
36589
- function QUAD(q) {
36590
- libmupdf.HEAPF32[_wasm_quad + 0] = q[0];
36591
- libmupdf.HEAPF32[_wasm_quad + 1] = q[1];
36592
- libmupdf.HEAPF32[_wasm_quad + 2] = q[2];
36593
- libmupdf.HEAPF32[_wasm_quad + 3] = q[3];
36594
- libmupdf.HEAPF32[_wasm_quad + 4] = q[4];
36595
- libmupdf.HEAPF32[_wasm_quad + 5] = q[5];
36596
- libmupdf.HEAPF32[_wasm_quad + 6] = q[6];
36597
- libmupdf.HEAPF32[_wasm_quad + 7] = q[7];
36598
- return _wasm_quad << 2;
36599
- }
36600
- function COLOR(c) {
36601
- if (typeof c !== "undefined") {
36602
- switch (c.length) {
36603
- case 0:
36604
- break;
36605
- case 1:
36606
- libmupdf.HEAPF32[_wasm_color + 0] = c[0];
36607
- break;
36608
- case 3:
36609
- libmupdf.HEAPF32[_wasm_color + 0] = c[0];
36610
- libmupdf.HEAPF32[_wasm_color + 1] = c[1];
36611
- libmupdf.HEAPF32[_wasm_color + 2] = c[2];
36612
- break;
36613
- case 4:
36614
- libmupdf.HEAPF32[_wasm_color + 0] = c[0];
36615
- libmupdf.HEAPF32[_wasm_color + 1] = c[1];
36616
- libmupdf.HEAPF32[_wasm_color + 2] = c[2];
36617
- libmupdf.HEAPF32[_wasm_color + 3] = c[3];
36618
- break;
36619
- }
36620
- }
36621
- return _wasm_color << 2;
36622
- }
36623
- /* -------------------------------------------------------------------------- */
36624
- function fromColor(n) {
36625
- if (n === 1)
36626
- return [
36627
- libmupdf.HEAPF32[_wasm_color]
36628
- ];
36629
- if (n === 3)
36630
- return [
36631
- libmupdf.HEAPF32[_wasm_color + 0],
36632
- libmupdf.HEAPF32[_wasm_color + 1],
36633
- libmupdf.HEAPF32[_wasm_color + 2],
36634
- ];
36635
- if (n === 4)
36636
- return [
36637
- libmupdf.HEAPF32[_wasm_color + 0],
36638
- libmupdf.HEAPF32[_wasm_color + 1],
36639
- libmupdf.HEAPF32[_wasm_color + 2],
36640
- libmupdf.HEAPF32[_wasm_color + 3],
36641
- ];
36642
- throw new TypeError("invalid number of components for Color: " + n);
36643
- }
36644
- function fromAnnotColor(n) {
36645
- if (n === 0)
36646
- return [];
36647
- return fromColor(n);
36648
- }
36649
- function fromColorArray(n, ptr) {
36650
- let addr = ptr >> 2;
36651
- let color = [];
36652
- for (let i = 0; i < n; ++i)
36653
- color.push(libmupdf.HEAPF32[addr + i]);
36654
- return color;
36655
- }
36656
- function fromStringOrNull(ptr) {
36657
- if (ptr === 0)
36658
- return null;
36659
- return libmupdf.UTF8ToString(ptr);
36660
- }
36661
- function fromString(ptr) {
36662
- return libmupdf.UTF8ToString(ptr);
36663
- }
36664
- function fromStringFree(ptr) {
36665
- let str = libmupdf.UTF8ToString(ptr);
36666
- Free(ptr);
36667
- return str;
36668
- }
36669
- function fromPoint(ptr) {
36670
- let addr = ptr >> 2;
36671
- return [
36672
- libmupdf.HEAPF32[addr + 0],
36673
- libmupdf.HEAPF32[addr + 1],
36674
- ];
36675
- }
36676
- function fromRect(ptr) {
36677
- let addr = ptr >> 2;
36678
- return [
36679
- libmupdf.HEAPF32[addr + 0],
36680
- libmupdf.HEAPF32[addr + 1],
36681
- libmupdf.HEAPF32[addr + 2],
36682
- libmupdf.HEAPF32[addr + 3],
36683
- ];
36684
- }
36685
- function fromMatrix(ptr) {
36686
- let addr = ptr >> 2;
36687
- return [
36688
- libmupdf.HEAPF32[addr + 0],
36689
- libmupdf.HEAPF32[addr + 1],
36690
- libmupdf.HEAPF32[addr + 2],
36691
- libmupdf.HEAPF32[addr + 3],
36692
- libmupdf.HEAPF32[addr + 4],
36693
- libmupdf.HEAPF32[addr + 5],
36694
- ];
36695
- }
36696
- function fromQuad(ptr) {
36697
- let addr = ptr >> 2;
36698
- return [
36699
- libmupdf.HEAPF32[addr + 0],
36700
- libmupdf.HEAPF32[addr + 1],
36701
- libmupdf.HEAPF32[addr + 2],
36702
- libmupdf.HEAPF32[addr + 3],
36703
- libmupdf.HEAPF32[addr + 4],
36704
- libmupdf.HEAPF32[addr + 5],
36705
- libmupdf.HEAPF32[addr + 6],
36706
- libmupdf.HEAPF32[addr + 7],
36707
- ];
36708
- }
36709
- function fromBuffer(ptr) {
36710
- let data = libmupdf._wasm_buffer_get_data(ptr);
36711
- let size = libmupdf._wasm_buffer_get_len(ptr);
36712
- return libmupdf.HEAPU8.slice(data, data + size);
36713
- }
36714
- /* unused for now
36715
- function rgbFromColor(c?: Color): [number, number, number] {
36716
- var r = 0, g = 0, b = 0
36717
- if (typeof c !== "undefined") {
36718
- switch (c.length) {
36719
- case 1:
36720
- r = g = b = c[0]
36721
- break
36722
- case 3:
36723
- r = c[0]
36724
- g = c[1]
36725
- b = c[2]
36726
- break
36727
- case 4:
36728
- r = 1 - Math.min(1, c[0] + c[3])
36729
- g = 1 - Math.min(1, c[1] + c[3])
36730
- b = 1 - Math.min(1, c[2] + c[3])
36731
- break
36732
- }
36733
- }
36734
- return [ r, g, b ]
36735
- }
36736
-
36737
- function numberFromColor(c?: Color): number {
36738
- var [ r, g, b ] = rgbFromColor(c)
36739
- return (255 << 24) | (r << 16) | (g << 8) | b
36740
- }
36741
- */
36742
- function colorFromNumber(argb) {
36743
- var r = (argb >> 16) & 255;
36744
- var g = (argb >> 8) & 255;
36745
- var b = (argb) & 255;
36746
- return [r / 255, g / 255, b / 255];
36747
- }
36748
- function runSearch(searchFun, searchThis, needle, max_hits = 500) {
36749
- checkType(needle, "string");
36750
- let hits = 0;
36751
- let marks = 0;
36752
- try {
36753
- hits = Malloc(32 * max_hits);
36754
- marks = Malloc(4 * max_hits);
36755
- let n = searchFun(searchThis, STRING(needle), marks, hits, max_hits);
36756
- let outer = [];
36757
- if (n > 0) {
36758
- let inner = [];
36759
- for (let i = 0; i < n; ++i) {
36760
- let mark = libmupdf.HEAP32[(marks >> 2) + i];
36761
- let quad = fromQuad(hits + i * 32);
36762
- if (i > 0 && mark) {
36763
- outer.push(inner);
36764
- inner = [];
36765
- }
36766
- inner.push(quad);
36767
- }
36768
- outer.push(inner);
36769
- }
36770
- return outer;
36771
- }
36772
- finally {
36773
- Free(marks);
36774
- Free(hits);
36775
- }
36776
- }
36777
- /* -------------------------------------------------------------------------- */
36778
- class Userdata {
36779
- constructor(pointer) {
36780
- if (typeof pointer !== "number")
36781
- throw new Error("invalid pointer: " + typeof pointer);
36782
- if (pointer !== 0) {
36783
- let ctor = this.constructor;
36784
- if (!ctor._finalizer)
36785
- ctor._finalizer = new FinalizationRegistry(ctor._drop);
36786
- ctor._finalizer.register(this, pointer, this);
36787
- }
36788
- this.pointer = pointer;
36789
- }
36790
- destroy() {
36791
- if (this.pointer !== 0) {
36792
- let ctor = this.constructor;
36793
- ctor._finalizer.unregister(this);
36794
- ctor._drop(this.pointer);
36795
- }
36796
- this.pointer = 0;
36797
- }
36798
- // Custom "console.log" formatting for Node
36799
- [Symbol.for("nodejs.util.inspect.custom")]() {
36800
- return this.toString();
36801
- }
36802
- toString() {
36803
- return `[${this.constructor.name} ${this.pointer}]`;
36804
- }
36805
- valueOf() {
36806
- throw new Error("cannot convert Userdata to Javascript value");
36807
- }
36808
- }
36809
- let Buffer$1 = class Buffer extends Userdata {
36810
- constructor(arg) {
36811
- if (typeof arg === "undefined")
36812
- super(libmupdf._wasm_new_buffer(1024));
36813
- else if (typeof arg === "number")
36814
- super(arg);
36815
- else if (typeof arg === "string") {
36816
- let data_len = libmupdf.lengthBytesUTF8(arg);
36817
- let data_ptr = Malloc(data_len + 1);
36818
- libmupdf.stringToUTF8(arg, data_ptr, data_len + 1);
36819
- super(libmupdf._wasm_new_buffer_from_data(data_ptr, data_len));
36820
- }
36821
- else if (arg instanceof ArrayBuffer || arg instanceof Uint8Array) {
36822
- let data_len = arg.byteLength;
36823
- let data_ptr = Malloc(data_len);
36824
- libmupdf.HEAPU8.set(new Uint8Array(arg), data_ptr);
36825
- super(libmupdf._wasm_new_buffer_from_data(data_ptr, data_len));
36826
- }
36827
- }
36828
- get length() {
36829
- return this.getLength();
36830
- }
36831
- set length(_) {
36832
- throw new TypeError("buffer length is read-only");
36833
- }
36834
- getLength() {
36835
- return libmupdf._wasm_buffer_get_len(this.pointer);
36836
- }
36837
- readByte(at) {
36838
- let data = libmupdf._wasm_buffer_get_data(this.pointer);
36839
- return libmupdf.HEAPU8[data + at];
36840
- }
36841
- write(s) {
36842
- libmupdf._wasm_append_string(this.pointer, STRING(s));
36843
- }
36844
- writeByte(b) {
36845
- libmupdf._wasm_append_byte(this.pointer, b);
36846
- }
36847
- writeLine(s) {
36848
- this.write(s);
36849
- this.writeByte(10);
36850
- }
36851
- writeBuffer(other) {
36852
- libmupdf._wasm_append_buffer(this.pointer, BUFFER(other));
36853
- }
36854
- asUint8Array() {
36855
- let data = libmupdf._wasm_buffer_get_data(this.pointer);
36856
- let size = libmupdf._wasm_buffer_get_len(this.pointer);
36857
- return libmupdf.HEAPU8.subarray(data, data + size);
36858
- }
36859
- slice(start, end) {
36860
- if (typeof end === "undefined")
36861
- end = this.getLength();
36862
- return new Buffer(libmupdf._wasm_slice_buffer(this.pointer, start, end));
36863
- }
36864
- asString() {
36865
- return fromString(libmupdf._wasm_string_from_buffer(this.pointer));
36866
- }
36867
- save(filename) {
36868
- if (node_fs)
36869
- node_fs.writeFileSync(filename, this.asUint8Array());
36870
- else
36871
- throw new Error("missing 'fs' module");
36872
- }
36873
- };
36874
- Buffer$1._drop = libmupdf._wasm_drop_buffer;
36875
- class ColorSpace extends Userdata {
36876
- constructor(from, name) {
36877
- if (typeof from === "number") {
36878
- super(from);
36879
- }
36880
- else {
36881
- if (typeof from === "string") {
36882
- if (node_fs)
36883
- from = node_fs.readFileSync(from);
36884
- else
36885
- throw new Error("missing 'fs' module");
36886
- }
36887
- super(libmupdf._wasm_new_icc_colorspace(STRING_OPT(name), BUFFER(from)));
36888
- }
36889
- }
36890
- getName() {
36891
- return fromString(libmupdf._wasm_colorspace_get_name(this.pointer));
36892
- }
36893
- getType() {
36894
- return ColorSpace.COLORSPACE_TYPES[libmupdf._wasm_colorspace_get_type(this.pointer)] || "None";
36895
- }
36896
- getNumberOfComponents() {
36897
- return libmupdf._wasm_colorspace_get_n(this.pointer);
36898
- }
36899
- isGray() { return this.getType() === "Gray"; }
36900
- isRGB() { return this.getType() === "RGB"; }
36901
- isCMYK() { return this.getType() === "CMYK"; }
36902
- isIndexed() { return this.getType() === "Indexed"; }
36903
- isLab() { return this.getType() === "Lab"; }
36904
- isDeviceN() { return this.getType() === "Separation"; }
36905
- isSubtractive() { return this.getType() === "CMYK" || this.getType() === "Separation"; }
36906
- toString() {
36907
- return "[ColorSpace " + this.getName() + "]";
36908
- }
36909
- }
36910
- ColorSpace._drop = libmupdf._wasm_drop_colorspace;
36911
- ColorSpace.COLORSPACE_TYPES = [
36912
- "None",
36913
- "Gray",
36914
- "RGB",
36915
- "BGR",
36916
- "CMYK",
36917
- "Lab",
36918
- "Indexed",
36919
- "Separation"
36920
- ];
36921
- ColorSpace.DeviceGray = new ColorSpace(libmupdf._wasm_device_gray());
36922
- ColorSpace.DeviceRGB = new ColorSpace(libmupdf._wasm_device_rgb());
36923
- ColorSpace.DeviceBGR = new ColorSpace(libmupdf._wasm_device_bgr());
36924
- ColorSpace.DeviceCMYK = new ColorSpace(libmupdf._wasm_device_cmyk());
36925
- ColorSpace.Lab = new ColorSpace(libmupdf._wasm_device_lab());
36926
- class Font extends Userdata {
36927
- constructor(name_or_pointer, data, subfont = 0) {
36928
- let pointer = 0;
36929
- if (typeof name_or_pointer === "number") {
36930
- pointer = libmupdf._wasm_keep_font(name_or_pointer);
36931
- }
36932
- else {
36933
- if (typeof data === "string") {
36934
- if (node_fs)
36935
- data = node_fs.readFileSync(data);
36936
- else
36937
- throw new Error("missing 'fs' module");
36938
- }
36939
- if (data)
36940
- pointer = libmupdf._wasm_new_font_from_buffer(STRING(name_or_pointer), BUFFER(data), subfont);
36941
- else if (name_or_pointer === "zh-Hant")
36942
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_CNS);
36943
- else if (name_or_pointer === "zh-Hans")
36944
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_GB);
36945
- else if (name_or_pointer === "ja")
36946
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_JAPAN);
36947
- else if (name_or_pointer === "ko")
36948
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_KOREA);
36949
- else
36950
- pointer = libmupdf._wasm_new_base14_font(STRING(name_or_pointer));
36951
- }
36952
- super(pointer);
36953
- }
36954
- getName() {
36955
- return fromString(libmupdf._wasm_font_get_name(this.pointer));
36956
- }
36957
- encodeCharacter(uni) {
36958
- if (typeof uni === "string")
36959
- uni = uni.charCodeAt(0);
36960
- return libmupdf._wasm_encode_character(this.pointer, uni);
36961
- }
36962
- advanceGlyph(gid, wmode = 0) {
36963
- return libmupdf._wasm_advance_glyph(this.pointer, gid, wmode);
36964
- }
36965
- isMono() {
36966
- return !!libmupdf._wasm_font_is_monospaced(this.pointer);
36967
- }
36968
- isSerif() {
36969
- return !!libmupdf._wasm_font_is_serif(this.pointer);
36970
- }
36971
- isBold() {
36972
- return !!libmupdf._wasm_font_is_bold(this.pointer);
36973
- }
36974
- isItalic() {
36975
- return !!libmupdf._wasm_font_is_italic(this.pointer);
36976
- }
36977
- }
36978
- Font._drop = libmupdf._wasm_drop_font;
36979
- Font.SIMPLE_ENCODING = [
36980
- "Latin",
36981
- "Greek",
36982
- "Cyrillic"
36983
- ];
36984
- Font.ADOBE_CNS = 0;
36985
- Font.ADOBE_GB = 1;
36986
- Font.ADOBE_JAPAN = 2;
36987
- Font.ADOBE_KOREA = 3;
36988
- Font.CJK_ORDERING_BY_LANG = {
36989
- "Adobe-CNS1": 0,
36990
- "Adobe-GB1": 1,
36991
- "Adobe-Japan1": 2,
36992
- "Adobe-Korea1": 3,
36993
- "zh-Hant": 0,
36994
- "zh-TW": 0,
36995
- "zh-HK": 0,
36996
- "zh-Hans": 1,
36997
- "zh-CN": 1,
36998
- "ja": 2,
36999
- "ko": 3,
37000
- };
37001
- class Image extends Userdata {
37002
- constructor(data, mask) {
37003
- let pointer = 0;
37004
- if (typeof data === "number") {
37005
- pointer = libmupdf._wasm_keep_image(data);
37006
- }
37007
- else if (data instanceof Pixmap) {
37008
- pointer = libmupdf._wasm_new_image_from_pixmap(data.pointer, mask ? mask.pointer : 0);
37009
- }
37010
- else {
37011
- if (typeof data === "string") {
37012
- if (node_fs)
37013
- data = node_fs.readFileSync(data);
37014
- else
37015
- throw new Error("missing 'fs' module");
37016
- }
37017
- pointer = libmupdf._wasm_new_image_from_buffer(BUFFER(data));
37018
- }
37019
- super(pointer);
37020
- }
37021
- getWidth() {
37022
- return libmupdf._wasm_image_get_w(this.pointer);
37023
- }
37024
- getHeight() {
37025
- return libmupdf._wasm_image_get_h(this.pointer);
37026
- }
37027
- getNumberOfComponents() {
37028
- return libmupdf._wasm_image_get_n(this.pointer);
37029
- }
37030
- getBitsPerComponent() {
37031
- return libmupdf._wasm_image_get_bpc(this.pointer);
37032
- }
37033
- getXResolution() {
37034
- return libmupdf._wasm_image_get_xres(this.pointer);
37035
- }
37036
- getYResolution() {
37037
- return libmupdf._wasm_image_get_yres(this.pointer);
37038
- }
37039
- getImageMask() {
37040
- return !!libmupdf._wasm_image_get_imagemask(this.pointer);
37041
- }
37042
- getColorSpace() {
37043
- let cs = libmupdf._wasm_image_get_colorspace(this.pointer);
37044
- if (cs)
37045
- return new ColorSpace(libmupdf._wasm_keep_colorspace(cs));
37046
- return null;
37047
- }
37048
- getMask() {
37049
- let mask = libmupdf._wasm_image_get_mask(this.pointer);
37050
- if (mask)
37051
- return new Image(libmupdf._wasm_keep_image(mask));
37052
- return null;
37053
- }
37054
- toPixmap() {
37055
- return new Pixmap(libmupdf._wasm_get_pixmap_from_image(this.pointer));
37056
- }
37057
- }
37058
- Image._drop = libmupdf._wasm_drop_image;
37059
- class StrokeState extends Userdata {
37060
- constructor(data) {
37061
- if (typeof data === "number") {
37062
- super(data);
37063
- return this;
37064
- }
37065
- super(libmupdf._wasm_new_stroke_state(data?.dashes?.length ?? 0));
37066
- let lineCap = ENUM(data.lineCap, StrokeState.LINE_CAP);
37067
- let lineJoin = ENUM(data.lineJoin, StrokeState.LINE_JOIN);
37068
- libmupdf._wasm_stroke_state_set_start_cap(this.pointer, lineCap);
37069
- libmupdf._wasm_stroke_state_set_dash_cap(this.pointer, lineCap);
37070
- libmupdf._wasm_stroke_state_set_end_cap(this.pointer, lineCap);
37071
- libmupdf._wasm_stroke_state_set_linejoin(this.pointer, lineJoin);
37072
- libmupdf._wasm_stroke_state_set_linewidth(this.pointer, data.lineWidth);
37073
- libmupdf._wasm_stroke_state_set_miterlimit(this.pointer, data.miterLimit);
37074
- libmupdf._wasm_stroke_state_set_dash_phase(this.pointer, data.dashPhase ?? 0);
37075
- if (data.dashes) {
37076
- for (let i = 0; i < data.dashes.length; ++i)
37077
- libmupdf._wasm_stroke_state_set_dash_item(this.pointer, i, data.dashes[i] ?? 0);
37078
- }
37079
- }
37080
- getLineCap() {
37081
- return libmupdf._wasm_stroke_state_get_start_cap(this.pointer);
37082
- }
37083
- getLineJoin() {
37084
- return libmupdf._wasm_stroke_state_get_linejoin(this.pointer);
37085
- }
37086
- getLineWidth() {
37087
- return libmupdf._wasm_stroke_state_get_linewidth(this.pointer);
37088
- }
37089
- getMiterLimit() {
37090
- return libmupdf._wasm_stroke_state_get_miterlimit(this.pointer);
37091
- }
37092
- getDashPhase() {
37093
- return libmupdf._wasm_stroke_state_get_dash_phase(this.pointer);
37094
- }
37095
- getDashes() {
37096
- var n = libmupdf._wasm_stroke_state_get_dash_len(this.pointer);
37097
- if (n > 0) {
37098
- var out = [];
37099
- for (let i = 0; i < n; ++i)
37100
- out[i] = libmupdf._wasm_stroke_state_get_dash_item(this.pointer, i);
37101
- return out;
37102
- }
37103
- return null;
37104
- }
37105
- }
37106
- StrokeState._drop = libmupdf._wasm_drop_stroke_state;
37107
- StrokeState.LINE_CAP = [
37108
- "Butt",
37109
- "Round",
37110
- "Square",
37111
- "Triangle"
37112
- ];
37113
- StrokeState.LINE_JOIN = [
37114
- "Miter",
37115
- "Round",
37116
- "Bevel",
37117
- "MiterXPS"
37118
- ];
37119
- class Path extends Userdata {
37120
- constructor(pointer) {
37121
- if (typeof pointer === "number")
37122
- super(pointer);
37123
- else
37124
- super(libmupdf._wasm_new_path());
37125
- }
37126
- getBounds(strokeState, transform) {
37127
- if (strokeState !== null)
37128
- checkType(strokeState, StrokeState);
37129
- checkMatrix(transform);
37130
- return fromRect(libmupdf._wasm_bound_path(this.pointer, strokeState?.pointer, MATRIX(transform)));
37131
- }
37132
- moveTo(x, y) {
37133
- checkType(x, "number");
37134
- checkType(y, "number");
37135
- libmupdf._wasm_moveto(this.pointer, x, y);
37136
- }
37137
- lineTo(x, y) {
37138
- checkType(x, "number");
37139
- checkType(y, "number");
37140
- libmupdf._wasm_lineto(this.pointer, x, y);
37141
- }
37142
- curveTo(x1, y1, x2, y2, x3, y3) {
37143
- checkType(x1, "number");
37144
- checkType(y1, "number");
37145
- checkType(x2, "number");
37146
- checkType(y2, "number");
37147
- checkType(x3, "number");
37148
- checkType(y3, "number");
37149
- libmupdf._wasm_curveto(this.pointer, x1, y1, x2, y2, x3, y3);
37150
- }
37151
- curveToV(cx, cy, ex, ey) {
37152
- checkType(cx, "number");
37153
- checkType(cy, "number");
37154
- checkType(ex, "number");
37155
- checkType(ey, "number");
37156
- libmupdf._wasm_curvetov(this.pointer, cx, cy, ex, ey);
37157
- }
37158
- curveToY(cx, cy, ex, ey) {
37159
- checkType(cx, "number");
37160
- checkType(cy, "number");
37161
- checkType(ex, "number");
37162
- checkType(ey, "number");
37163
- libmupdf._wasm_curvetoy(this.pointer, cx, cy, ex, ey);
37164
- }
37165
- closePath() {
37166
- libmupdf._wasm_closepath(this.pointer);
37167
- }
37168
- rect(x1, y1, x2, y2) {
37169
- checkType(x1, "number");
37170
- checkType(y1, "number");
37171
- checkType(x2, "number");
37172
- checkType(y2, "number");
37173
- libmupdf._wasm_rectto(this.pointer, x1, y1, x2, y2);
37174
- }
37175
- transform(matrix) {
37176
- checkMatrix(matrix);
37177
- libmupdf._wasm_transform_path(this.pointer, MATRIX(matrix));
37178
- }
37179
- walk(walker) {
37180
- let id = $libmupdf_path_id++;
37181
- $libmupdf_path_table.set(id, walker);
37182
- libmupdf._wasm_walk_path(this.pointer, id);
37183
- $libmupdf_path_table.delete(id);
37184
- }
37185
- }
37186
- Path._drop = libmupdf._wasm_drop_path;
37187
- class Text extends Userdata {
37188
- constructor(pointer) {
37189
- if (typeof pointer === "number")
37190
- super(pointer);
37191
- else
37192
- super(libmupdf._wasm_new_text());
37193
- }
37194
- getBounds(strokeState, transform) {
37195
- if (strokeState !== null)
37196
- checkType(strokeState, StrokeState);
37197
- checkMatrix(transform);
37198
- return fromRect(libmupdf._wasm_bound_text(this.pointer, strokeState?.pointer, MATRIX(transform)));
37199
- }
37200
- showGlyph(font, trm, gid, uni, wmode = 0) {
37201
- checkType(font, Font);
37202
- checkMatrix(trm);
37203
- checkType(gid, "number");
37204
- checkType(uni, "number");
37205
- libmupdf._wasm_show_glyph(this.pointer, font.pointer, MATRIX(trm), gid, uni, wmode);
37206
- }
37207
- showString(font, trm, str, wmode = 0) {
37208
- checkType(font, Font);
37209
- checkMatrix(trm);
37210
- checkType(str, "string");
37211
- return fromMatrix(libmupdf._wasm_show_string(this.pointer, font.pointer, MATRIX(trm), STRING(str), wmode));
37212
- }
37213
- walk(walker) {
37214
- let id = $libmupdf_text_id++;
37215
- $libmupdf_text_table.set(id, walker);
37216
- libmupdf._wasm_walk_text(this.pointer, id);
37217
- $libmupdf_text_table.delete(id);
37218
- }
37219
- }
37220
- Text._drop = libmupdf._wasm_drop_text;
37221
- class DisplayList extends Userdata {
37222
- constructor(arg1) {
37223
- let pointer = 0;
37224
- if (typeof arg1 === "number") {
37225
- pointer = arg1;
37226
- }
37227
- else {
37228
- checkRect(arg1);
37229
- pointer = libmupdf._wasm_new_display_list(RECT(arg1));
37230
- }
37231
- super(pointer);
37232
- }
37233
- getBounds() {
37234
- return fromRect(libmupdf._wasm_bound_display_list(this.pointer));
37235
- }
37236
- toPixmap(matrix, colorspace, alpha = false) {
37237
- checkMatrix(matrix);
37238
- checkType(colorspace, ColorSpace);
37239
- return new Pixmap(libmupdf._wasm_new_pixmap_from_display_list(this.pointer, MATRIX(matrix), colorspace.pointer, alpha));
37240
- }
37241
- toStructuredText(options = "") {
37242
- checkType(options, "string");
37243
- return new StructuredText(libmupdf._wasm_new_stext_page_from_display_list(this.pointer, STRING(options)));
37244
- }
37245
- run(device, matrix) {
37246
- checkType(device, Device);
37247
- checkMatrix(matrix);
37248
- libmupdf._wasm_run_display_list(this.pointer, device.pointer, MATRIX(matrix));
37249
- }
37250
- search(needle, max_hits = 500) {
37251
- return runSearch(libmupdf._wasm_search_display_list, this.pointer, needle, max_hits);
37252
- }
37253
- }
37254
- DisplayList._drop = libmupdf._wasm_drop_display_list;
37255
- class Pixmap extends Userdata {
37256
- constructor(arg1, bbox, alpha = false) {
37257
- if (typeof arg1 === "number") {
37258
- super(arg1);
37259
- }
37260
- if (arg1 instanceof ColorSpace) {
37261
- checkRect(bbox);
37262
- super(libmupdf._wasm_new_pixmap_with_bbox(arg1.pointer, RECT(bbox), alpha));
37263
- }
37264
- if (arg1 === null) {
37265
- checkRect(bbox);
37266
- super(libmupdf._wasm_new_pixmap_with_bbox(0, RECT(bbox), alpha));
37267
- }
37268
- }
37269
- getBounds() {
37270
- let x = libmupdf._wasm_pixmap_get_x(this.pointer);
37271
- let y = libmupdf._wasm_pixmap_get_y(this.pointer);
37272
- let w = libmupdf._wasm_pixmap_get_w(this.pointer);
37273
- let h = libmupdf._wasm_pixmap_get_h(this.pointer);
37274
- return [x, y, x + w, y + h];
37275
- }
37276
- clear(value) {
37277
- if (typeof value === "undefined")
37278
- libmupdf._wasm_clear_pixmap(this.pointer);
37279
- else
37280
- libmupdf._wasm_clear_pixmap_with_value(this.pointer, value);
37281
- }
37282
- getWidth() {
37283
- return libmupdf._wasm_pixmap_get_w(this.pointer);
37284
- }
37285
- getHeight() {
37286
- return libmupdf._wasm_pixmap_get_h(this.pointer);
37287
- }
37288
- getX() {
37289
- return libmupdf._wasm_pixmap_get_x(this.pointer);
37290
- }
37291
- getY() {
37292
- return libmupdf._wasm_pixmap_get_y(this.pointer);
37293
- }
37294
- getStride() {
37295
- return libmupdf._wasm_pixmap_get_stride(this.pointer);
37296
- }
37297
- getNumberOfComponents() {
37298
- return libmupdf._wasm_pixmap_get_n(this.pointer);
37299
- }
37300
- getAlpha() {
37301
- return libmupdf._wasm_pixmap_get_alpha(this.pointer);
37302
- }
37303
- getXResolution() {
37304
- return libmupdf._wasm_pixmap_get_xres(this.pointer);
37305
- }
37306
- getYResolution() {
37307
- return libmupdf._wasm_pixmap_get_yres(this.pointer);
37308
- }
37309
- setResolution(x, y) {
37310
- libmupdf._wasm_pixmap_set_xres(this.pointer, x);
37311
- libmupdf._wasm_pixmap_set_yres(this.pointer, y);
37312
- }
37313
- getColorSpace() {
37314
- let cs = libmupdf._wasm_pixmap_get_colorspace(this.pointer);
37315
- if (cs)
37316
- return new ColorSpace(libmupdf._wasm_keep_colorspace(cs));
37317
- return null;
37318
- }
37319
- getPixels() {
37320
- let s = libmupdf._wasm_pixmap_get_stride(this.pointer);
37321
- let h = libmupdf._wasm_pixmap_get_h(this.pointer);
37322
- let p = libmupdf._wasm_pixmap_get_samples(this.pointer);
37323
- return new Uint8ClampedArray(libmupdf.HEAPU8.buffer, p, s * h);
37324
- }
37325
- asPNG() {
37326
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_png(this.pointer);
37327
- try {
37328
- return fromBuffer(buf);
37329
- }
37330
- finally {
37331
- libmupdf._wasm_drop_buffer(buf);
37332
- }
37333
- }
37334
- asPSD() {
37335
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_psd(this.pointer);
37336
- try {
37337
- return fromBuffer(buf);
37338
- }
37339
- finally {
37340
- libmupdf._wasm_drop_buffer(buf);
37341
- }
37342
- }
37343
- asPAM() {
37344
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_pam(this.pointer);
37345
- try {
37346
- return fromBuffer(buf);
37347
- }
37348
- finally {
37349
- libmupdf._wasm_drop_buffer(buf);
37350
- }
37351
- }
37352
- asJPEG(quality, invert_cmyk = false) {
37353
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_jpeg(this.pointer, quality, invert_cmyk);
37354
- try {
37355
- return fromBuffer(buf);
37356
- }
37357
- finally {
37358
- libmupdf._wasm_drop_buffer(buf);
37359
- }
37360
- }
37361
- invert() {
37362
- libmupdf._wasm_invert_pixmap(this.pointer);
37363
- }
37364
- invertLuminance() {
37365
- libmupdf._wasm_invert_pixmap_luminance(this.pointer);
37366
- }
37367
- gamma(p) {
37368
- libmupdf._wasm_gamma_pixmap(this.pointer, p);
37369
- }
37370
- tint(black, white) {
37371
- let black_hex = 0x000000;
37372
- let white_hex = 0xffffff;
37373
- if (typeof black === "number")
37374
- black_hex = black;
37375
- else if (black instanceof Array && black.length === 3)
37376
- black_hex = (((black[0] * 255) << 16) | ((black[1] * 255) << 8) | ((black[2] * 255)));
37377
- if (typeof white === "number")
37378
- white_hex = white;
37379
- else if (white instanceof Array && white.length === 3)
37380
- white = (((white[0] * 255) << 16) | ((white[1] * 255) << 8) | ((white[2] * 255)));
37381
- libmupdf._wasm_tint_pixmap(this.pointer, black_hex, white_hex);
37382
- }
37383
- convertToColorSpace(colorspace, keepAlpha = false) {
37384
- checkType(colorspace, ColorSpace);
37385
- checkType(keepAlpha, "boolean");
37386
- return new Pixmap(libmupdf._wasm_convert_pixmap(this.pointer, colorspace.pointer, keepAlpha));
37387
- }
37388
- warp(points, width, height) {
37389
- let quad = points.flat();
37390
- checkQuad(quad);
37391
- checkType(width, "number");
37392
- checkType(height, "number");
37393
- return new Pixmap(libmupdf._wasm_warp_pixmap(this.pointer, QUAD(quad), width, height));
37394
- }
37395
- }
37396
- Pixmap._drop = libmupdf._wasm_drop_pixmap;
37397
- class Shade extends Userdata {
37398
- getBounds() {
37399
- return fromRect(libmupdf._wasm_bound_shade(this.pointer));
37400
- }
37401
- }
37402
- Shade._drop = libmupdf._wasm_drop_shade;
37403
- class StructuredText extends Userdata {
37404
- walk(walker) {
37405
- let block = libmupdf._wasm_stext_page_get_first_block(this.pointer);
37406
- while (block) {
37407
- let block_type = libmupdf._wasm_stext_block_get_type(block);
37408
- let block_bbox = fromRect(libmupdf._wasm_stext_block_get_bbox(block));
37409
- if (block_type === 1) {
37410
- if (walker.onImageBlock) {
37411
- let matrix = fromMatrix(libmupdf._wasm_stext_block_get_transform(block));
37412
- let image = new Image(libmupdf._wasm_stext_block_get_image(block));
37413
- walker.onImageBlock(block_bbox, matrix, image);
37414
- }
37415
- }
37416
- else {
37417
- if (walker.beginTextBlock)
37418
- walker.beginTextBlock(block_bbox);
37419
- let line = libmupdf._wasm_stext_block_get_first_line(block);
37420
- while (line) {
37421
- let line_bbox = fromRect(libmupdf._wasm_stext_line_get_bbox(line));
37422
- let line_wmode = libmupdf._wasm_stext_line_get_wmode(line);
37423
- let line_dir = fromPoint(libmupdf._wasm_stext_line_get_dir(line));
37424
- if (walker.beginLine)
37425
- walker.beginLine(line_bbox, line_wmode, line_dir);
37426
- if (walker.onChar) {
37427
- let ch = libmupdf._wasm_stext_line_get_first_char(line);
37428
- while (ch) {
37429
- let ch_rune = String.fromCharCode(libmupdf._wasm_stext_char_get_c(ch));
37430
- let ch_origin = fromPoint(libmupdf._wasm_stext_char_get_origin(ch));
37431
- let ch_font = new Font(libmupdf._wasm_stext_char_get_font(ch));
37432
- let ch_size = libmupdf._wasm_stext_char_get_size(ch);
37433
- let ch_quad = fromQuad(libmupdf._wasm_stext_char_get_quad(ch));
37434
- let ch_color = colorFromNumber(libmupdf._wasm_stext_char_get_argb(ch));
37435
- walker.onChar(ch_rune, ch_origin, ch_font, ch_size, ch_quad, ch_color);
37436
- ch = libmupdf._wasm_stext_char_get_next(ch);
37437
- }
37438
- }
37439
- if (walker.endLine)
37440
- walker.endLine();
37441
- line = libmupdf._wasm_stext_line_get_next(line);
37442
- }
37443
- if (walker.endTextBlock)
37444
- walker.endTextBlock();
37445
- }
37446
- block = libmupdf._wasm_stext_block_get_next(block);
37447
- }
37448
- }
37449
- asJSON(scale = 1) {
37450
- return fromStringFree(libmupdf._wasm_print_stext_page_as_json(this.pointer, scale));
37451
- }
37452
- asHTML(id) {
37453
- return fromStringFree(libmupdf._wasm_print_stext_page_as_html(this.pointer, id));
37454
- }
37455
- asText() {
37456
- return fromStringFree(libmupdf._wasm_print_stext_page_as_text(this.pointer));
37457
- }
37458
- snap(p, q, mode) {
37459
- let mm = ENUM(mode, StructuredText.SELECT_MODE);
37460
- return fromQuad(libmupdf._wasm_snap_selection(this.pointer, POINT(p), POINT2(q), mm));
37461
- }
37462
- copy(p, q) {
37463
- return fromStringFree(libmupdf._wasm_copy_selection(this.pointer, POINT(p), POINT2(q)));
37464
- }
37465
- highlight(p, q, max_hits = 100) {
37466
- let hits = 0;
37467
- let result = [];
37468
- try {
37469
- hits = Malloc(32 * max_hits);
37470
- let n = libmupdf._wasm_highlight_selection(this.pointer, POINT(p), POINT2(q), hits, max_hits);
37471
- for (let i = 0; i < n; ++i)
37472
- result.push(fromQuad(hits + i * 32));
37473
- }
37474
- finally {
37475
- Free(hits);
37476
- }
37477
- return result;
37478
- }
37479
- search(needle, max_hits = 500) {
37480
- return runSearch(libmupdf._wasm_search_stext_page, this.pointer, needle, max_hits);
37481
- }
37482
- }
37483
- StructuredText._drop = libmupdf._wasm_drop_stext_page;
37484
- StructuredText.SELECT_MODE = [
37485
- "chars",
37486
- "words",
37487
- "lines"
37488
- ];
37489
- StructuredText.SELECT_CHARS = "chars";
37490
- StructuredText.SELECT_WORDS = "words";
37491
- StructuredText.SELECT_LINES = "lines";
37492
- class Device extends Userdata {
37493
- constructor(pointer_or_callbacks) {
37494
- if (typeof pointer_or_callbacks === "number")
37495
- super(pointer_or_callbacks);
37496
- else {
37497
- let id = $libmupdf_device_id++;
37498
- $libmupdf_device_table.set(id, pointer_or_callbacks);
37499
- super(libmupdf._wasm_new_js_device(id));
37500
- }
37501
- }
37502
- fillPath(path, evenOdd, ctm, colorspace, color, alpha) {
37503
- checkType(path, Path);
37504
- checkMatrix(ctm);
37505
- checkType(colorspace, ColorSpace);
37506
- checkColor(color);
37507
- libmupdf._wasm_fill_path(this.pointer, path.pointer, evenOdd, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37508
- }
37509
- strokePath(path, stroke, ctm, colorspace, color, alpha) {
37510
- checkType(path, Path);
37511
- checkType(stroke, StrokeState);
37512
- checkMatrix(ctm);
37513
- checkType(colorspace, ColorSpace);
37514
- checkColor(color);
37515
- libmupdf._wasm_stroke_path(this.pointer, path.pointer, stroke.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37516
- }
37517
- clipPath(path, evenOdd, ctm) {
37518
- checkType(path, Path);
37519
- checkMatrix(ctm);
37520
- libmupdf._wasm_clip_path(this.pointer, path.pointer, evenOdd, MATRIX(ctm));
37521
- }
37522
- clipStrokePath(path, stroke, ctm) {
37523
- checkType(path, Path);
37524
- checkType(stroke, StrokeState);
37525
- checkMatrix(ctm);
37526
- libmupdf._wasm_clip_stroke_path(this.pointer, path.pointer, stroke.pointer, MATRIX(ctm));
37527
- }
37528
- fillText(text, ctm, colorspace, color, alpha) {
37529
- checkType(text, Text);
37530
- checkMatrix(ctm);
37531
- checkType(colorspace, ColorSpace);
37532
- checkColor(color);
37533
- libmupdf._wasm_fill_text(this.pointer, text.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37534
- }
37535
- strokeText(text, stroke, ctm, colorspace, color, alpha) {
37536
- checkType(text, Text);
37537
- checkType(stroke, StrokeState);
37538
- checkMatrix(ctm);
37539
- checkType(colorspace, ColorSpace);
37540
- checkColor(color);
37541
- libmupdf._wasm_stroke_text(this.pointer, text.pointer, stroke.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37542
- }
37543
- clipText(text, ctm) {
37544
- checkType(text, Text);
37545
- checkMatrix(ctm);
37546
- libmupdf._wasm_clip_text(this.pointer, text.pointer, MATRIX(ctm));
37547
- }
37548
- clipStrokeText(text, stroke, ctm) {
37549
- checkType(text, Text);
37550
- checkType(stroke, StrokeState);
37551
- checkMatrix(ctm);
37552
- libmupdf._wasm_clip_stroke_text(this.pointer, text.pointer, stroke.pointer, MATRIX(ctm));
37553
- }
37554
- ignoreText(text, ctm) {
37555
- checkType(text, Text);
37556
- checkMatrix(ctm);
37557
- libmupdf._wasm_ignore_text(this.pointer, text.pointer, MATRIX(ctm));
37558
- }
37559
- fillShade(shade, ctm, alpha) {
37560
- checkType(shade, Shade);
37561
- checkMatrix(ctm);
37562
- libmupdf._wasm_fill_shade(this.pointer, shade.pointer, MATRIX(ctm), alpha);
37563
- }
37564
- fillImage(image, ctm, alpha) {
37565
- checkType(image, Image);
37566
- checkMatrix(ctm);
37567
- libmupdf._wasm_fill_image(this.pointer, image.pointer, MATRIX(ctm), alpha);
37568
- }
37569
- fillImageMask(image, ctm, colorspace, color, alpha) {
37570
- checkType(image, Image);
37571
- checkMatrix(ctm);
37572
- checkType(colorspace, ColorSpace);
37573
- checkColor(color);
37574
- libmupdf._wasm_fill_image_mask(this.pointer, image.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37575
- }
37576
- clipImageMask(image, ctm) {
37577
- checkType(image, Image);
37578
- checkMatrix(ctm);
37579
- libmupdf._wasm_clip_image_mask(this.pointer, image.pointer, MATRIX(ctm));
37580
- }
37581
- popClip() {
37582
- libmupdf._wasm_pop_clip(this.pointer);
37583
- }
37584
- beginMask(area, luminosity, colorspace, color) {
37585
- checkRect(area);
37586
- checkType(colorspace, ColorSpace);
37587
- checkColor(color);
37588
- libmupdf._wasm_begin_mask(this.pointer, RECT(area), luminosity, colorspace.pointer, COLOR(color));
37589
- }
37590
- endMask() {
37591
- libmupdf._wasm_end_mask(this.pointer);
37592
- }
37593
- beginGroup(area, colorspace, isolated, knockout, blendmode, alpha) {
37594
- checkRect(area);
37595
- checkType(colorspace, ColorSpace);
37596
- let blendmode_ix = ENUM(blendmode, Device.BLEND_MODES);
37597
- libmupdf._wasm_begin_group(this.pointer, RECT(area), colorspace.pointer, isolated, knockout, blendmode_ix, alpha);
37598
- }
37599
- endGroup() {
37600
- libmupdf._wasm_end_group(this.pointer);
37601
- }
37602
- beginTile(area, view, xstep, ystep, ctm, id) {
37603
- checkRect(area);
37604
- checkRect(view);
37605
- checkMatrix(ctm);
37606
- return libmupdf._wasm_begin_tile(this.pointer, RECT(area), RECT2(view), xstep, ystep, MATRIX(ctm), id);
37607
- }
37608
- endTile() {
37609
- libmupdf._wasm_end_tile(this.pointer);
37610
- }
37611
- beginLayer(name) {
37612
- libmupdf._wasm_begin_layer(this.pointer, STRING(name));
37613
- }
37614
- endLayer() {
37615
- libmupdf._wasm_end_layer(this.pointer);
37616
- }
37617
- close() {
37618
- libmupdf._wasm_close_device(this.pointer);
37619
- }
37620
- }
37621
- Device._drop = libmupdf._wasm_drop_device;
37622
- Device.BLEND_MODES = [
37623
- "Normal",
37624
- "Multiply",
37625
- "Screen",
37626
- "Overlay",
37627
- "Darken",
37628
- "Lighten",
37629
- "ColorDodge",
37630
- "ColorBurn",
37631
- "HardLight",
37632
- "SoftLight",
37633
- "Difference",
37634
- "Exclusion",
37635
- "Hue",
37636
- "Saturation",
37637
- "Color",
37638
- "Luminosity",
37639
- ];
37640
- class DocumentWriter extends Userdata {
37641
- constructor(buffer, format, options) {
37642
- super(libmupdf._wasm_new_document_writer_with_buffer(BUFFER(buffer), STRING(format), STRING2(options)));
37643
- }
37644
- beginPage(mediabox) {
37645
- checkRect(mediabox);
37646
- return new Device(libmupdf._wasm_begin_page(this.pointer, RECT(mediabox)));
37647
- }
37648
- endPage() {
37649
- libmupdf._wasm_end_page(this.pointer);
37650
- }
37651
- close() {
37652
- libmupdf._wasm_close_document_writer(this.pointer);
37653
- }
37654
- }
37655
- DocumentWriter._drop = libmupdf._wasm_drop_document_writer;
37656
- class Document extends Userdata {
37657
- static openDocument(from, magic) {
37658
- let pointer = 0;
37659
- if (typeof from === "string") {
37660
- magic = from;
37661
- if (node_fs)
37662
- from = node_fs.readFileSync(from);
37663
- else
37664
- throw new Error("missing 'fs' module");
37665
- }
37666
- else {
37667
- if (typeof magic === "undefined")
37668
- magic = "application/pdf";
37669
- }
37670
- checkType(magic, "string");
37671
- if (from instanceof ArrayBuffer || from instanceof Uint8Array)
37672
- from = new Buffer$1(from);
37673
- if (from instanceof Buffer$1)
37674
- pointer = libmupdf._wasm_open_document_with_buffer(STRING(magic), from.pointer);
37675
- else if (from instanceof Stream)
37676
- pointer = libmupdf._wasm_open_document_with_stream(STRING(magic), from.pointer);
37677
- else
37678
- throw new Error("not a Buffer or Stream");
37679
- let pdf = libmupdf._wasm_pdf_document_from_fz_document(pointer);
37680
- if (pdf)
37681
- return new PDFDocument(pdf);
37682
- return new Document(pointer);
37683
- }
37684
- formatLinkURI(dest) {
37685
- return fromStringFree(libmupdf._wasm_format_link_uri(this.pointer, dest.chapter | 0, dest.page | 0, ENUM(dest.type, Document.LINK_DEST), +dest.x, +dest.y, +dest.width, +dest.height, +dest.zoom));
37686
- }
37687
- asPDF() {
37688
- if (this instanceof PDFDocument)
37689
- return this;
37690
- return null;
37691
- }
37692
- isPDF() {
37693
- return this instanceof PDFDocument;
37694
- }
37695
- needsPassword() {
37696
- return !!libmupdf._wasm_needs_password(this.pointer);
37697
- }
37698
- authenticatePassword(password) {
37699
- return libmupdf._wasm_authenticate_password(this.pointer, STRING(password));
37700
- }
37701
- hasPermission(perm) {
37702
- let perm_ix = Document.PERMISSION[perm];
37703
- return !!libmupdf._wasm_has_permission(this.pointer, perm_ix);
37704
- }
37705
- getMetaData(key) {
37706
- let value = libmupdf._wasm_lookup_metadata(this.pointer, STRING(key));
37707
- if (value)
37708
- return fromString(value);
37709
- return undefined;
37710
- }
37711
- setMetaData(key, value) {
37712
- libmupdf._wasm_set_metadata(this.pointer, STRING(key), STRING2(value));
37713
- }
37714
- countPages() {
37715
- return libmupdf._wasm_count_pages(this.pointer);
37716
- }
37717
- isReflowable() {
37718
- libmupdf._wasm_is_document_reflowable(this.pointer);
37719
- }
37720
- layout(w, h, em) {
37721
- libmupdf._wasm_layout_document(this.pointer, w, h, em);
37722
- }
37723
- loadPage(index) {
37724
- let fz_ptr = libmupdf._wasm_load_page(this.pointer, index);
37725
- if (this instanceof PDFDocument) {
37726
- let pdf_ptr = libmupdf._wasm_pdf_page_from_fz_page(fz_ptr);
37727
- if (pdf_ptr)
37728
- return new PDFPage(this, pdf_ptr);
37729
- }
37730
- return new Page(fz_ptr);
37731
- }
37732
- loadOutline() {
37733
- let doc = this.pointer;
37734
- function to_outline(outline) {
37735
- let result = [];
37736
- while (outline) {
37737
- let title = libmupdf._wasm_outline_get_title(outline);
37738
- let uri = libmupdf._wasm_outline_get_uri(outline);
37739
- let open = libmupdf._wasm_outline_get_is_open(outline);
37740
- let item = {
37741
- title: title ? fromString(title) : undefined,
37742
- uri: uri ? fromString(uri) : undefined,
37743
- open: !!open,
37744
- };
37745
- let page = libmupdf._wasm_outline_get_page(doc, outline);
37746
- if (page >= 0)
37747
- item.page = page;
37748
- let down = libmupdf._wasm_outline_get_down(outline);
37749
- if (down)
37750
- item.down = to_outline(down);
37751
- result.push(item);
37752
- outline = libmupdf._wasm_outline_get_next(outline);
37753
- }
37754
- return result;
37755
- }
37756
- let root = libmupdf._wasm_load_outline(doc);
37757
- if (root)
37758
- return to_outline(root);
37759
- return null;
37760
- }
37761
- resolveLink(link) {
37762
- if (link instanceof Link)
37763
- return libmupdf._wasm_resolve_link(this.pointer, libmupdf._wasm_link_get_uri(link.pointer));
37764
- return libmupdf._wasm_resolve_link(this.pointer, STRING(link));
37765
- }
37766
- resolveLinkDestination(link) {
37767
- let dest;
37768
- if (link instanceof Link)
37769
- dest = libmupdf._wasm_resolve_link_dest(this.pointer, libmupdf._wasm_link_get_uri(link.pointer));
37770
- else
37771
- dest = libmupdf._wasm_resolve_link_dest(this.pointer, STRING(link));
37772
- return {
37773
- type: Document.LINK_DEST[libmupdf._wasm_link_dest_get_type(dest)],
37774
- chapter: libmupdf._wasm_link_dest_get_chapter(dest),
37775
- page: libmupdf._wasm_link_dest_get_page(dest),
37776
- x: libmupdf._wasm_link_dest_get_x(dest),
37777
- y: libmupdf._wasm_link_dest_get_y(dest),
37778
- width: libmupdf._wasm_link_dest_get_w(dest),
37779
- height: libmupdf._wasm_link_dest_get_h(dest),
37780
- zoom: libmupdf._wasm_link_dest_get_zoom(dest),
37781
- };
37782
- }
37783
- outlineIterator() {
37784
- return new OutlineIterator(libmupdf._wasm_new_outline_iterator(this.pointer));
37785
- }
37786
- }
37787
- Document._drop = libmupdf._wasm_drop_document;
37788
- Document.META_FORMAT = "format";
37789
- Document.META_ENCRYPTION = "encryption";
37790
- Document.META_INFO_AUTHOR = "info:Author";
37791
- Document.META_INFO_TITLE = "info:Title";
37792
- Document.META_INFO_SUBJECT = "info:Subject";
37793
- Document.META_INFO_KEYWORDS = "info:Keywords";
37794
- Document.META_INFO_CREATOR = "info:Creator";
37795
- Document.META_INFO_PRODUCER = "info:Producer";
37796
- Document.META_INFO_CREATIONDATE = "info:CreationDate";
37797
- Document.META_INFO_MODIFICATIONDATE = "info:ModDate";
37798
- Document.PERMISSION = {
37799
- "print": "p".charCodeAt(0),
37800
- "copy": "c".charCodeAt(0),
37801
- "edit": "e".charCodeAt(0),
37802
- "annotate": "n".charCodeAt(0),
37803
- "form": "f".charCodeAt(0),
37804
- "accessibility": "y".charCodeAt(0),
37805
- "assemble": "a".charCodeAt(0),
37806
- "print-hq": "h".charCodeAt(0),
37807
- };
37808
- Document.LINK_DEST = [
37809
- "Fit",
37810
- "FitB",
37811
- "FitH",
37812
- "FitBH",
37813
- "FitV",
37814
- "FitBV",
37815
- "FitR",
37816
- "XYZ",
37817
- ];
37818
- class OutlineIterator extends Userdata {
37819
- item() {
37820
- let item = libmupdf._wasm_outline_iterator_item(this.pointer);
37821
- if (item) {
37822
- let title_ptr = libmupdf._wasm_outline_item_get_title(item);
37823
- let uri_ptr = libmupdf._wasm_outline_item_get_uri(item);
37824
- let is_open = libmupdf._wasm_outline_item_get_is_open(item);
37825
- return {
37826
- title: title_ptr ? fromString(title_ptr) : undefined,
37827
- uri: uri_ptr ? fromString(uri_ptr) : undefined,
37828
- open: !!is_open,
37829
- };
37830
- }
37831
- return null;
37832
- }
37833
- next() {
37834
- return libmupdf._wasm_outline_iterator_next(this.pointer);
37835
- }
37836
- prev() {
37837
- return libmupdf._wasm_outline_iterator_prev(this.pointer);
37838
- }
37839
- up() {
37840
- return libmupdf._wasm_outline_iterator_up(this.pointer);
37841
- }
37842
- down() {
37843
- return libmupdf._wasm_outline_iterator_down(this.pointer);
37844
- }
37845
- delete() {
37846
- return libmupdf._wasm_outline_iterator_delete(this.pointer);
37847
- }
37848
- insert(item) {
37849
- return libmupdf._wasm_outline_iterator_insert(this.pointer, STRING_OPT(item.title), STRING2_OPT(item.uri), item.open);
37850
- }
37851
- update(item) {
37852
- libmupdf._wasm_outline_iterator_update(this.pointer, STRING_OPT(item.title), STRING2_OPT(item.uri), item.open);
37853
- }
37854
- }
37855
- OutlineIterator._drop = libmupdf._wasm_drop_outline_iterator;
37856
- OutlineIterator.RESULT_DID_NOT_MOVE = -1;
37857
- OutlineIterator.RESULT_AT_ITEM = 0;
37858
- OutlineIterator.RESULT_AT_EMPTY = 1;
37859
- class Link extends Userdata {
37860
- getBounds() {
37861
- return fromRect(libmupdf._wasm_link_get_rect(this.pointer));
37862
- }
37863
- setBounds(rect) {
37864
- checkRect(rect);
37865
- libmupdf._wasm_link_set_rect(this.pointer, RECT(rect));
37866
- }
37867
- getURI() {
37868
- return fromString(libmupdf._wasm_link_get_uri(this.pointer));
37869
- }
37870
- setURI(uri) {
37871
- checkType(uri, "string");
37872
- libmupdf._wasm_link_set_uri(this.pointer, STRING(uri));
37873
- }
37874
- isExternal() {
37875
- return /^\w[\w+-.]*:/.test(this.getURI());
37876
- }
37877
- }
37878
- Link._drop = libmupdf._wasm_drop_link;
37879
- class Page extends Userdata {
37880
- isPDF() {
37881
- return this instanceof PDFPage;
37882
- }
37883
- getBounds(box = "CropBox") {
37884
- let box_ix = ENUM(box, Page.BOXES);
37885
- return fromRect(libmupdf._wasm_bound_page(this.pointer, box_ix));
37886
- }
37887
- getLabel() {
37888
- return fromString(libmupdf._wasm_page_label(this.pointer));
37889
- }
37890
- run(device, matrix) {
37891
- checkType(device, Device);
37892
- checkMatrix(matrix);
37893
- libmupdf._wasm_run_page(this.pointer, device.pointer, MATRIX(matrix));
37894
- }
37895
- runPageContents(device, matrix) {
37896
- checkType(device, Device);
37897
- checkMatrix(matrix);
37898
- libmupdf._wasm_run_page_contents(this.pointer, device.pointer, MATRIX(matrix));
37899
- }
37900
- runPageAnnots(device, matrix) {
37901
- checkType(device, Device);
37902
- checkMatrix(matrix);
37903
- libmupdf._wasm_run_page_annots(this.pointer, device.pointer, MATRIX(matrix));
37904
- }
37905
- runPageWidgets(device, matrix) {
37906
- checkType(device, Device);
37907
- checkMatrix(matrix);
37908
- libmupdf._wasm_run_page_widgets(this.pointer, device.pointer, MATRIX(matrix));
37909
- }
37910
- toPixmap(matrix, colorspace, alpha = false, showExtras = true) {
37911
- checkType(colorspace, ColorSpace);
37912
- checkMatrix(matrix);
37913
- let result;
37914
- if (showExtras)
37915
- result = libmupdf._wasm_new_pixmap_from_page(this.pointer, MATRIX(matrix), colorspace.pointer, alpha);
37916
- else
37917
- result = libmupdf._wasm_new_pixmap_from_page_contents(this.pointer, MATRIX(matrix), colorspace.pointer, alpha);
37918
- return new Pixmap(result);
37919
- }
37920
- toDisplayList(showExtras = true) {
37921
- let result;
37922
- if (showExtras)
37923
- result = libmupdf._wasm_new_display_list_from_page(this.pointer);
37924
- else
37925
- result = libmupdf._wasm_new_display_list_from_page_contents(this.pointer);
37926
- return new DisplayList(result);
37927
- }
37928
- toStructuredText(options = "") {
37929
- checkType(options, "string");
37930
- return new StructuredText(libmupdf._wasm_new_stext_page_from_page(this.pointer, STRING(options)));
37931
- }
37932
- getLinks() {
37933
- let links = [];
37934
- let link = libmupdf._wasm_load_links(this.pointer);
37935
- while (link) {
37936
- links.push(new Link(libmupdf._wasm_keep_link(link)));
37937
- link = libmupdf._wasm_link_get_next(link);
37938
- }
37939
- return links;
37940
- }
37941
- createLink(bbox, uri) {
37942
- checkRect(bbox);
37943
- return new Link(libmupdf._wasm_create_link(this.pointer, RECT(bbox), STRING(uri)));
37944
- }
37945
- deleteLink(link) {
37946
- checkType(link, Link);
37947
- libmupdf._wasm_delete_link(this.pointer, link.pointer);
37948
- }
37949
- search(needle, max_hits = 500) {
37950
- return runSearch(libmupdf._wasm_search_page, this.pointer, needle, max_hits);
37951
- }
37952
- }
37953
- Page._drop = libmupdf._wasm_drop_page;
37954
- Page.BOXES = [
37955
- "MediaBox",
37956
- "CropBox",
37957
- "BleedBox",
37958
- "TrimBox",
37959
- "ArtBox"
37960
- ];
37961
- /* -------------------------------------------------------------------------- */
37962
- class PDFDocument extends Document {
37963
- constructor(arg1) {
37964
- if (typeof arg1 === "undefined")
37965
- super(libmupdf._wasm_pdf_create_document());
37966
- else if (typeof arg1 === "number")
37967
- super(arg1);
37968
- else if (arg1 instanceof PDFDocument) {
37969
- super(arg1.pointer);
37970
- libmupdf._wasm_keep_document(this.pointer);
37971
- }
37972
- else {
37973
- let doc = Document.openDocument(arg1, "application/pdf");
37974
- if (doc instanceof PDFDocument)
37975
- return doc;
37976
- throw new Error("not a PDF document");
37977
- }
37978
- }
37979
- loadPage(index) {
37980
- return super.loadPage(index);
37981
- }
37982
- // PDFObject instances are always bound to a document, so the WASM/JS value interface lives here.
37983
- // Wrap a pdf_obj in a Userdata object. The pointer must be newly created or we already own it.
37984
- _fromPDFObjectNew(ptr) {
37985
- if (ptr === 0)
37986
- return PDFObject.Null;
37987
- return new PDFObject(this, ptr);
37988
- }
37989
- // Wrap a pdf_obj in a Userdata object. The pointer must be a borrowed pointer, so we have to take ownership.
37990
- _fromPDFObjectKeep(ptr) {
37991
- if (ptr === 0)
37992
- return PDFObject.Null;
37993
- return new PDFObject(this, libmupdf._wasm_pdf_keep_obj(ptr));
37994
- }
37995
- _toPDFObject(obj) {
37996
- if (obj instanceof PDFObject)
37997
- return obj;
37998
- if (obj === null || obj === undefined)
37999
- return this.newNull();
38000
- if (typeof obj === "string") {
38001
- // if a JS string is surrounded by parens, convert it to a PDF string
38002
- if (obj.startsWith("(") && obj.endsWith(")"))
38003
- return this.newString(obj.slice(1, -1));
38004
- // otherwise treat it as a name
38005
- return this.newName(obj);
38006
- }
38007
- if (typeof obj === "number") {
38008
- if (obj === (obj | 0))
38009
- return this.newInteger(obj);
38010
- return this.newReal(obj);
38011
- }
38012
- if (typeof obj === "boolean")
38013
- return this.newBoolean(obj);
38014
- if (obj instanceof Array) {
38015
- let result = this.newArray();
38016
- for (let item of obj)
38017
- result.push(item);
38018
- return result;
38019
- }
38020
- if (obj instanceof Object) {
38021
- let result = this.newDictionary();
38022
- for (let key in obj)
38023
- result.put(key, obj[key]);
38024
- return result;
38025
- }
38026
- throw new TypeError("cannot convert value to PDFObject");
38027
- }
38028
- _PDFOBJ(obj) {
38029
- // Note: We have to create a PDFObject instance for garbage collection.
38030
- return this._toPDFObject(obj).pointer;
38031
- }
38032
- getVersion() {
38033
- return libmupdf._wasm_pdf_version(this.pointer);
38034
- }
38035
- getLanguage() {
38036
- return fromStringOrNull(libmupdf._wasm_pdf_document_language(this.pointer));
38037
- }
38038
- setLanguage(lang) {
38039
- libmupdf._wasm_pdf_set_document_language(this.pointer, STRING(lang));
38040
- }
38041
- countObjects() {
38042
- return libmupdf._wasm_pdf_xref_len(this.pointer);
38043
- }
38044
- getTrailer() {
38045
- return new PDFObject(this, libmupdf._wasm_pdf_trailer(this.pointer));
38046
- }
38047
- createObject() {
38048
- let num = libmupdf._wasm_pdf_create_object(this.pointer);
38049
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_indirect(this.pointer, num));
38050
- }
38051
- newNull() { return PDFObject.Null; }
38052
- newBoolean(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_bool(v)); }
38053
- newInteger(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_int(v)); }
38054
- newReal(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_real(v)); }
38055
- newName(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_name(STRING(v))); }
38056
- newString(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_text_string(STRING(v))); }
38057
- newByteString(v) {
38058
- if (v instanceof Array)
38059
- v = Uint8Array.from(v);
38060
- checkType(v, Uint8Array);
38061
- let len = v.byteLength;
38062
- let ptr = Malloc(len);
38063
- libmupdf.HEAPU8.set(v, ptr);
38064
- try {
38065
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_string(ptr, len));
38066
- }
38067
- finally {
38068
- Free(ptr);
38069
- }
38070
- }
38071
- newIndirect(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_indirect(this.pointer, v)); }
38072
- newArray() { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_array(this.pointer)); }
38073
- newDictionary() { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_dict(this.pointer)); }
38074
- deleteObject(num) {
38075
- if (num instanceof PDFObject)
38076
- num = num.asIndirect();
38077
- else
38078
- checkType(num, "number");
38079
- libmupdf._wasm_pdf_delete_object(this.pointer, num);
38080
- }
38081
- addObject(obj) {
38082
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_object(this.pointer, this._PDFOBJ(obj)));
38083
- }
38084
- addStream(buf, obj) {
38085
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_stream(this.pointer, BUFFER(buf), this._PDFOBJ(obj), 0));
38086
- }
38087
- addRawStream(buf, obj) {
38088
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_stream(this.pointer, BUFFER(buf), this._PDFOBJ(obj), 1));
38089
- }
38090
- newGraftMap() {
38091
- return new PDFGraftMap(this, libmupdf._wasm_pdf_new_graft_map(this.pointer));
38092
- }
38093
- graftObject(obj) {
38094
- checkType(obj, PDFObject);
38095
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_graft_object(this.pointer, obj.pointer));
38096
- }
38097
- graftPage(to, srcDoc, srcPage) {
38098
- checkType(to, "number");
38099
- checkType(srcDoc, PDFDocument);
38100
- checkType(srcPage, "number");
38101
- libmupdf._wasm_pdf_graft_page(this.pointer, to, srcDoc.pointer, srcPage);
38102
- }
38103
- addSimpleFont(font, encoding = "Latin") {
38104
- checkType(font, Font);
38105
- var encoding_ix = ENUM(encoding, Font.SIMPLE_ENCODING);
38106
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_simple_font(this.pointer, font.pointer, encoding_ix));
38107
- }
38108
- addCJKFont(font, lang, wmode = 0, serif = true) {
38109
- checkType(font, Font);
38110
- if (typeof lang === "string")
38111
- lang = Font.CJK_ORDERING_BY_LANG[lang];
38112
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_cjk_font(this.pointer, font.pointer, lang, wmode, serif));
38113
- }
38114
- addFont(font) {
38115
- checkType(font, Font);
38116
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_cid_font(this.pointer, font.pointer));
38117
- }
38118
- addImage(image) {
38119
- checkType(image, Image);
38120
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_image(this.pointer, image.pointer));
38121
- }
38122
- loadImage(ref) {
38123
- checkType(ref, PDFObject);
38124
- return new Image(libmupdf._wasm_pdf_load_image(this.pointer, ref.pointer));
38125
- }
38126
- findPage(index) {
38127
- checkType(index, "number");
38128
- return this._fromPDFObjectKeep(libmupdf._wasm_pdf_lookup_page_obj(this.pointer, index));
38129
- }
38130
- addPage(mediabox, rotate, resources, contents) {
38131
- checkRect(mediabox);
38132
- checkType(rotate, "number");
38133
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_page(this.pointer, RECT(mediabox), rotate, this._PDFOBJ(resources), BUFFER(contents)));
38134
- }
38135
- insertPage(at, obj) {
38136
- checkType(at, "number");
38137
- libmupdf._wasm_pdf_insert_page(this.pointer, at, this._PDFOBJ(obj));
38138
- }
38139
- deletePage(at) {
38140
- checkType(at, "number");
38141
- libmupdf._wasm_pdf_delete_page(this.pointer, at);
38142
- }
38143
- isEmbeddedFile(ref) {
38144
- checkType(ref, PDFObject);
38145
- return !!libmupdf._wasm_pdf_is_embedded_file(ref.pointer);
38146
- }
38147
- addEmbeddedFile(filename, mimetype, contents, created, modified, checksum = false) {
38148
- checkType(filename, "string");
38149
- checkType(mimetype, "string");
38150
- checkType(created, Date);
38151
- checkType(modified, Date);
38152
- checkType(checksum, "boolean");
38153
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_embedded_file(this.pointer, STRING(filename), STRING2(mimetype), BUFFER(contents), created.getTime() / 1000 | 0, modified.getTime() / 1000 | 0, checksum));
38154
- }
38155
- getFilespecParams(ref) {
38156
- checkType(ref, PDFObject);
38157
- let ptr = libmupdf._wasm_pdf_get_filespec_params(ref.pointer);
38158
- return {
38159
- filename: fromString(libmupdf._wasm_pdf_filespec_params_get_filename(ptr)),
38160
- mimetype: fromString(libmupdf._wasm_pdf_filespec_params_get_mimetype(ptr)),
38161
- size: libmupdf._wasm_pdf_filespec_params_get_filename(ptr),
38162
- creationDate: new Date(libmupdf._wasm_pdf_filespec_params_get_created(ptr) * 1000),
38163
- modificationDate: new Date(libmupdf._wasm_pdf_filespec_params_get_modified(ptr) * 1000),
38164
- };
38165
- }
38166
- getEmbeddedFileContents(ref) {
38167
- checkType(ref, PDFObject);
38168
- let contents = libmupdf._wasm_pdf_load_embedded_file_contents(ref.pointer);
38169
- if (contents)
38170
- return new Buffer$1(contents);
38171
- return null;
38172
- }
38173
- getEmbeddedFiles() {
38174
- function _getEmbeddedFilesRec(result, N) {
38175
- var i, n;
38176
- if (N.isDictionary()) {
38177
- var NN = N.get("Names");
38178
- if (NN)
38179
- for (i = 0, n = NN.length; i < n; i += 2)
38180
- result[NN.get(i + 0).asString()] = NN.get(i + 1);
38181
- var NK = N.get("Kids");
38182
- if (NK)
38183
- for (i = 0, n = NK.length; i < n; i += 1)
38184
- _getEmbeddedFilesRec(result, NK.get(i));
38185
- }
38186
- return result;
38187
- }
38188
- return _getEmbeddedFilesRec({}, this.getTrailer().get("Root", "Names", "EmbeddedFiles"));
38189
- }
38190
- loadNameTree(treeName) {
38191
- function _loadNameTreeRec(dict, node) {
38192
- var kids = node.get("Kids");
38193
- if (kids && kids.isArray())
38194
- for (var i = 0; i < kids.length; i += 1)
38195
- _loadNameTreeRec(dict, kids.get(i));
38196
- var names = node.get("Names");
38197
- if (names && names.isArray())
38198
- for (var i = 0; i < names.length; i += 2)
38199
- dict[names.get(i).asString()] = names.get(i + 1);
38200
- }
38201
- var node = this.getTrailer().get("Root").get("Names").get(treeName);
38202
- var dict = {};
38203
- if (node.isDictionary())
38204
- _loadNameTreeRec(dict, node);
38205
- return dict;
38206
- }
38207
- insertEmbeddedFile(filename, filespec) {
38208
- var efs = this.getEmbeddedFiles();
38209
- efs[filename] = filespec;
38210
- this._rewriteEmbeddedFiles(efs);
38211
- }
38212
- deleteEmbeddedFile(filename) {
38213
- var efs = this.getEmbeddedFiles();
38214
- delete efs[filename];
38215
- this._rewriteEmbeddedFiles(efs);
38216
- }
38217
- _rewriteEmbeddedFiles(efs) {
38218
- var efs_keys = Object.keys(efs);
38219
- efs_keys.sort();
38220
- var root = this.getTrailer().get("Root");
38221
- var root_names = root.get("Names");
38222
- if (!root_names.isDictionary())
38223
- root_names = root.put("Names", this.newDictionary());
38224
- var root_names_efs = root_names.put("EmbeddedFiles", this.newDictionary());
38225
- var root_names_efs_names = root_names_efs.put("Names", this.newArray());
38226
- for (var key of efs_keys) {
38227
- root_names_efs_names.push(this.newString(key));
38228
- root_names_efs_names.push(efs[key]);
38229
- }
38230
- }
38231
- saveToBuffer(options = "") {
38232
- var options_string;
38233
- if (typeof options === "object") {
38234
- options_string = Object.entries(options).map(kv => {
38235
- var k = kv[0];
38236
- var v = kv[1];
38237
- if (v === true)
38238
- return k + "=" + "yes";
38239
- else if (v === false)
38240
- return k + "=" + "no";
38241
- else
38242
- return k + "=" + String(v).replaceAll(",", ":");
38243
- }).join(",");
38244
- }
38245
- else {
38246
- options_string = options;
38247
- }
38248
- return new Buffer$1(libmupdf._wasm_pdf_write_document_buffer(this.pointer, STRING(options_string)));
38249
- }
38250
- save(filename, options = "") {
38251
- if (node_fs)
38252
- node_fs.writeFileSync(filename, this.saveToBuffer(options).asUint8Array());
38253
- else
38254
- throw new Error("missing 'fs' module");
38255
- }
38256
- setPageLabels(index, style = "D", prefix = "", start = 1) {
38257
- libmupdf._wasm_pdf_set_page_labels(this.pointer, index, style.charCodeAt(0), STRING(prefix), start);
38258
- }
38259
- deletePageLabels(index) {
38260
- libmupdf._wasm_pdf_delete_page_labels(this.pointer, index);
38261
- }
38262
- wasRepaired() {
38263
- return !!libmupdf._wasm_pdf_was_repaired(this.pointer);
38264
- }
38265
- hasUnsavedChanges() {
38266
- return !!libmupdf._wasm_pdf_has_unsaved_changes(this.pointer);
38267
- }
38268
- countVersions() {
38269
- return libmupdf._wasm_pdf_count_versions(this.pointer);
38270
- }
38271
- countUnsavedVersions() {
38272
- return libmupdf._wasm_pdf_count_unsaved_versions(this.pointer);
38273
- }
38274
- validateChangeHistory() {
38275
- return libmupdf._wasm_pdf_validate_change_history(this.pointer);
38276
- }
38277
- canBeSavedIncrementally() {
38278
- return !!libmupdf._wasm_pdf_can_be_saved_incrementally(this.pointer);
38279
- }
38280
- enableJournal() {
38281
- libmupdf._wasm_pdf_enable_journal(this.pointer);
38282
- }
38283
- getJournal() {
38284
- let position = libmupdf._wasm_pdf_undoredo_state_position(this.pointer);
38285
- let n = libmupdf._wasm_pdf_undoredo_state_count(this.pointer);
38286
- let steps = [];
38287
- for (let i = 0; i < n; ++i)
38288
- steps.push(fromString(libmupdf._wasm_pdf_undoredo_step(this.pointer, i)));
38289
- return { position, steps };
38290
- }
38291
- beginOperation(op) {
38292
- libmupdf._wasm_pdf_begin_operation(this.pointer, STRING(op));
38293
- }
38294
- beginImplicitOperation() {
38295
- libmupdf._wasm_pdf_begin_implicit_operation(this.pointer);
38296
- }
38297
- endOperation() {
38298
- libmupdf._wasm_pdf_end_operation(this.pointer);
38299
- }
38300
- abandonOperation() {
38301
- libmupdf._wasm_pdf_abandon_operation(this.pointer);
38302
- }
38303
- canUndo() {
38304
- return !!libmupdf._wasm_pdf_can_undo(this.pointer);
38305
- }
38306
- canRedo() {
38307
- return !!libmupdf._wasm_pdf_can_redo(this.pointer);
38308
- }
38309
- undo() {
38310
- libmupdf._wasm_pdf_undo(this.pointer);
38311
- }
38312
- redo() {
38313
- libmupdf._wasm_pdf_redo(this.pointer);
38314
- }
38315
- isJSSupported() {
38316
- return !!libmupdf._wasm_pdf_js_supported(this.pointer);
38317
- }
38318
- enableJS() {
38319
- libmupdf._wasm_pdf_enable_js(this.pointer);
38320
- }
38321
- disableJS() {
38322
- libmupdf._wasm_pdf_disable_js(this.pointer);
38323
- }
38324
- setJSEventListener(_listener) {
38325
- throw "TODO";
38326
- }
38327
- rearrangePages(pages) {
38328
- let n = pages.length;
38329
- let ptr = Malloc(n << 2);
38330
- for (let i = 0; i < n; ++i)
38331
- libmupdf.HEAPU32[(ptr >> 2) + i] = pages[i] || 0;
38332
- try {
38333
- libmupdf._wasm_pdf_rearrange_pages(this.pointer, n, ptr);
38334
- }
38335
- finally {
38336
- Free(ptr);
38337
- }
38338
- }
38339
- subsetFonts() {
38340
- libmupdf._wasm_pdf_subset_fonts(this.pointer);
38341
- }
38342
- bake(bakeAnnots = true, bakeWidgets = true) {
38343
- libmupdf._wasm_pdf_bake_document(this.pointer, bakeAnnots, bakeWidgets);
38344
- }
38345
- countLayers() {
38346
- return libmupdf._wasm_pdf_count_layers(this.pointer);
38347
- }
38348
- isLayerVisible(layer) {
38349
- return !!libmupdf._wasm_pdf_layer_is_enabled(this.pointer, layer);
38350
- }
38351
- setLayerVisible(layer, visible) {
38352
- libmupdf._wasm_pdf_enable_layer(this.pointer, layer, Number(visible));
38353
- }
38354
- getLayerName(layer) {
38355
- return fromString(libmupdf._wasm_pdf_layer_name(this.pointer, layer));
38356
- }
38357
- resetForm(fields, exclude) {
38358
- libmupdf._wasm_pdf_reset_form(this.pointer, this._PDFOBJ(fields), Number(exclude));
38359
- }
38360
- }
38361
- PDFDocument.PAGE_LABEL_NONE = "\0";
38362
- PDFDocument.PAGE_LABEL_DECIMAL = "D";
38363
- PDFDocument.PAGE_LABEL_ROMAN_UC = "R";
38364
- PDFDocument.PAGE_LABEL_ROMAN_LC = "r";
38365
- PDFDocument.PAGE_LABEL_ALPHA_UC = "A";
38366
- PDFDocument.PAGE_LABEL_ALPHA_LC = "a";
38367
- class PDFPage extends Page {
38368
- // PRIVATE
38369
- constructor(doc, pointer) {
38370
- super(pointer);
38371
- this._doc = doc;
38372
- this._annots = null;
38373
- this._widgets = null;
38374
- }
38375
- getObject() {
38376
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_page_get_obj(this.pointer));
38377
- }
38378
- getTransform() {
38379
- return fromMatrix(libmupdf._wasm_pdf_page_transform(this.pointer));
38380
- }
38381
- setPageBox(box, rect) {
38382
- let box_ix = ENUM(box, Page.BOXES);
38383
- checkRect(rect);
38384
- libmupdf._wasm_pdf_set_page_box(this.pointer, box_ix, RECT(rect));
38385
- }
38386
- toPixmap(matrix, colorspace, alpha = false, showExtras = true, usage = "View", box = "CropBox") {
38387
- checkMatrix(matrix);
38388
- checkType(colorspace, ColorSpace);
38389
- let box_ix = ENUM(box, Page.BOXES);
38390
- let result;
38391
- if (showExtras)
38392
- result = libmupdf._wasm_pdf_new_pixmap_from_page_with_usage(this.pointer, MATRIX(matrix), colorspace.pointer, alpha, STRING(usage), box_ix);
38393
- else
38394
- result = libmupdf._wasm_pdf_new_pixmap_from_page_contents_with_usage(this.pointer, MATRIX(matrix), colorspace.pointer, alpha, STRING(usage), box_ix);
38395
- return new Pixmap(result);
38396
- }
38397
- getWidgets() {
38398
- if (!this._widgets) {
38399
- this._widgets = [];
38400
- let widget = libmupdf._wasm_pdf_first_widget(this.pointer);
38401
- while (widget) {
38402
- this._widgets.push(new PDFWidget(this._doc, libmupdf._wasm_pdf_keep_annot(widget)));
38403
- widget = libmupdf._wasm_pdf_next_widget(widget);
38404
- }
38405
- }
38406
- return this._widgets;
38407
- }
38408
- getAnnotations() {
38409
- if (!this._annots) {
38410
- this._annots = [];
38411
- let annot = libmupdf._wasm_pdf_first_annot(this.pointer);
38412
- while (annot) {
38413
- this._annots.push(new PDFAnnotation(this._doc, libmupdf._wasm_pdf_keep_annot(annot)));
38414
- annot = libmupdf._wasm_pdf_next_annot(annot);
38415
- }
38416
- }
38417
- return this._annots;
38418
- }
38419
- createAnnotation(type) {
38420
- let type_ix = ENUM(type, PDFAnnotation.ANNOT_TYPES);
38421
- let annot = new PDFAnnotation(this._doc, libmupdf._wasm_pdf_create_annot(this.pointer, type_ix));
38422
- if (this._annots)
38423
- this._annots.push(annot);
38424
- return annot;
38425
- }
38426
- deleteAnnotation(annot) {
38427
- checkType(annot, PDFAnnotation);
38428
- libmupdf._wasm_pdf_delete_annot(this.pointer, annot.pointer);
38429
- if (this._annots) {
38430
- let ix = this._annots.indexOf(annot);
38431
- if (ix >= 0)
38432
- this._annots.splice(ix, 1);
38433
- }
38434
- }
38435
- applyRedactions(black_boxes = true, image_method = 2, line_art_method = 1, text_method = 0) {
38436
- libmupdf._wasm_pdf_redact_page(this.pointer, Number(black_boxes), image_method, line_art_method, text_method);
38437
- }
38438
- update() {
38439
- return !!libmupdf._wasm_pdf_update_page(this.pointer);
38440
- }
38441
- }
38442
- PDFPage.REDACT_IMAGE_NONE = 0;
38443
- PDFPage.REDACT_IMAGE_REMOVE = 1;
38444
- PDFPage.REDACT_IMAGE_PIXELS = 2;
38445
- PDFPage.REDACT_IMAGE_UNLESS_INVISIBLE = 3;
38446
- PDFPage.REDACT_LINE_ART_NONE = 0;
38447
- PDFPage.REDACT_LINE_ART_REMOVE_IF_COVERED = 1;
38448
- PDFPage.REDACT_LINE_ART_REMOVE_IF_TOUCHED = 2;
38449
- PDFPage.REDACT_TEXT_REMOVE = 0;
38450
- PDFPage.REDACT_TEXT_NONE = 1;
38451
- class PDFObject extends Userdata {
38452
- // PRIVATE
38453
- constructor(doc, pointer) {
38454
- super(libmupdf._wasm_pdf_keep_obj(pointer));
38455
- this._doc = doc;
38456
- }
38457
- isNull() { return this === PDFObject.Null; }
38458
- isIndirect() { return !!libmupdf._wasm_pdf_is_indirect(this.pointer); }
38459
- isBoolean() { return !!libmupdf._wasm_pdf_is_bool(this.pointer); }
38460
- isInteger() { return !!libmupdf._wasm_pdf_is_int(this.pointer); }
38461
- isReal() { return !!libmupdf._wasm_pdf_is_real(this.pointer); }
38462
- isNumber() { return !!libmupdf._wasm_pdf_is_number(this.pointer); }
38463
- isName() { return !!libmupdf._wasm_pdf_is_name(this.pointer); }
38464
- isString() { return !!libmupdf._wasm_pdf_is_string(this.pointer); }
38465
- isArray() { return !!libmupdf._wasm_pdf_is_array(this.pointer); }
38466
- isDictionary() { return !!libmupdf._wasm_pdf_is_dict(this.pointer); }
38467
- isStream() { return !!libmupdf._wasm_pdf_is_stream(this.pointer); }
38468
- asIndirect() { return libmupdf._wasm_pdf_to_num(this.pointer); }
38469
- asBoolean() { return !!libmupdf._wasm_pdf_to_bool(this.pointer); }
38470
- asNumber() { return libmupdf._wasm_pdf_to_real(this.pointer); }
38471
- asName() { return fromString(libmupdf._wasm_pdf_to_name(this.pointer)); }
38472
- asString() { return fromString(libmupdf._wasm_pdf_to_text_string(this.pointer)); }
38473
- asByteString() {
38474
- let ptr = libmupdf._wasm_pdf_to_string(this.pointer, _wasm_int);
38475
- let len = libmupdf.HEAPU32[_wasm_int >> 2];
38476
- return libmupdf.HEAPU8.slice(ptr, ptr + len);
38477
- }
38478
- readStream() { return new Buffer$1(libmupdf._wasm_pdf_load_stream(this.pointer)); }
38479
- readRawStream() { return new Buffer$1(libmupdf._wasm_pdf_load_raw_stream(this.pointer)); }
38480
- writeObject(obj) {
38481
- if (!this.isIndirect())
38482
- throw new TypeError("can only call PDFObject.writeObject on an indirect reference");
38483
- libmupdf._wasm_pdf_update_object(this._doc.pointer, this.asIndirect(), this._doc._PDFOBJ(obj));
38484
- }
38485
- writeStream(buf) {
38486
- if (!this.isIndirect())
38487
- throw new TypeError("can only call PDFObject.writeStream on an indirect reference");
38488
- libmupdf._wasm_pdf_update_stream(this._doc.pointer, this.pointer, BUFFER(buf), 0);
38489
- }
38490
- writeRawStream(buf) {
38491
- if (!this.isIndirect())
38492
- throw new TypeError("can only call PDFObject.writeRawStream on an indirect reference");
38493
- libmupdf._wasm_pdf_update_stream(this._doc.pointer, this.pointer, BUFFER(buf), 1);
38494
- }
38495
- resolve() {
38496
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_resolve_indirect(this.pointer));
38497
- }
38498
- get length() {
38499
- return libmupdf._wasm_pdf_array_len(this.pointer);
38500
- }
38501
- set length(_) {
38502
- throw new TypeError("object length is read-only");
38503
- }
38504
- _get(path) {
38505
- let obj = this.pointer;
38506
- for (let key of path) {
38507
- if (typeof key === "number")
38508
- obj = libmupdf._wasm_pdf_array_get(obj, key);
38509
- else if (key instanceof PDFObject)
38510
- obj = libmupdf._wasm_pdf_dict_get(obj, key.pointer);
38511
- else
38512
- obj = libmupdf._wasm_pdf_dict_gets(obj, STRING(key));
38513
- if (obj === 0)
38514
- break;
38515
- }
38516
- return obj;
38517
- }
38518
- get(...path) {
38519
- return this._doc._fromPDFObjectKeep(this._get(path));
38520
- }
38521
- getInheritable(key) {
38522
- if (key instanceof PDFObject)
38523
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_get_inheritable(this.pointer, key.pointer));
38524
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_gets_inheritable(this.pointer, STRING(key)));
38525
- }
38526
- put(key, value) {
38527
- value = this._doc._toPDFObject(value);
38528
- if (typeof key === "number")
38529
- libmupdf._wasm_pdf_array_put(this.pointer, key, value.pointer);
38530
- else if (key instanceof PDFObject)
38531
- libmupdf._wasm_pdf_dict_put(this.pointer, key.pointer, value.pointer);
38532
- else
38533
- libmupdf._wasm_pdf_dict_puts(this.pointer, STRING(key), value.pointer);
38534
- return value;
38535
- }
38536
- push(value) {
38537
- value = this._doc._toPDFObject(value);
38538
- libmupdf._wasm_pdf_array_push(this.pointer, value.pointer);
38539
- return value;
38540
- }
38541
- delete(key) {
38542
- if (typeof key === "number")
38543
- libmupdf._wasm_pdf_array_delete(this.pointer, key);
38544
- else if (key instanceof PDFObject)
38545
- libmupdf._wasm_pdf_dict_del(this.pointer, key.pointer);
38546
- else
38547
- libmupdf._wasm_pdf_dict_dels(this.pointer, STRING(key));
38548
- }
38549
- valueOf() {
38550
- if (this.isNull())
38551
- return null;
38552
- if (this.isBoolean())
38553
- return this.asBoolean();
38554
- if (this.isNumber())
38555
- return this.asNumber();
38556
- if (this.isName())
38557
- return this.asName();
38558
- if (this.isString())
38559
- return this.asString();
38560
- if (this.isIndirect())
38561
- return `${this.asIndirect()} 0 R`;
38562
- return this;
38563
- }
38564
- toString(tight = true, ascii = true) {
38565
- return fromStringFree(libmupdf._wasm_pdf_sprint_obj(this.pointer, tight, ascii));
38566
- }
38567
- forEach(fn) {
38568
- if (this.isArray()) {
38569
- let n = this.length;
38570
- for (let i = 0; i < n; ++i)
38571
- fn(this.get(i), i, this);
38572
- }
38573
- else if (this.isDictionary()) {
38574
- let n = libmupdf._wasm_pdf_dict_len(this.pointer);
38575
- for (let i = 0; i < n; ++i) {
38576
- let key = this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_get_key(this.pointer, i));
38577
- let val = this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_get_val(this.pointer, i));
38578
- fn(val, key.asName(), this);
38579
- }
38580
- }
38581
- }
38582
- // Convert to plain Javascript values, objects, and arrays.
38583
- // If you want to resolve indirect references, pass an empty object or array as the first argument.
38584
- // On exit, this object will contain all indirect objects encountered indexed by object number.
38585
- // Note: This function will omit cyclic references.
38586
- asJS(seen) {
38587
- if (this.isIndirect()) {
38588
- let ref = this.asIndirect();
38589
- if (!seen)
38590
- return `${ref} 0 R`;
38591
- if (ref in seen)
38592
- return seen[ref];
38593
- seen[ref] = PDFObject.Null; // stop recursion!
38594
- return seen[ref] = this.resolve().asJS(seen);
38595
- }
38596
- if (this.isArray()) {
38597
- let result = [];
38598
- this.forEach(val => {
38599
- result.push(val.asJS(seen));
38600
- });
38601
- return result;
38602
- }
38603
- if (this.isDictionary()) {
38604
- let result = {};
38605
- this.forEach((val, key) => {
38606
- result[key] = val.asJS(seen);
38607
- });
38608
- return result;
38609
- }
38610
- return this.valueOf();
38611
- }
38612
- }
38613
- PDFObject._drop = libmupdf._wasm_pdf_drop_obj;
38614
- PDFObject.Null = new PDFObject(null, 0);
38615
- class PDFGraftMap extends Userdata {
38616
- // PRIVATE
38617
- constructor(doc, pointer) {
38618
- super(pointer);
38619
- this._doc = doc;
38620
- }
38621
- graftObject(obj) {
38622
- checkType(obj, PDFObject);
38623
- return this._doc._fromPDFObjectNew(libmupdf._wasm_pdf_graft_mapped_object(this.pointer, obj.pointer));
38624
- }
38625
- graftPage(to, srcDoc, srcPage) {
38626
- checkType(to, "number");
38627
- checkType(srcDoc, PDFDocument);
38628
- checkType(srcPage, "number");
38629
- libmupdf._wasm_pdf_graft_mapped_page(this.pointer, to, srcDoc.pointer, srcPage);
38630
- }
38631
- }
38632
- PDFGraftMap._drop = libmupdf._wasm_pdf_drop_graft_map;
38633
- class PDFAnnotation extends Userdata {
38634
- // PRIVATE
38635
- constructor(doc, pointer) {
38636
- super(pointer);
38637
- this._doc = doc;
38638
- }
38639
- getObject() {
38640
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_annot_obj(this.pointer));
38641
- }
38642
- getBounds() {
38643
- return fromRect(libmupdf._wasm_pdf_bound_annot(this.pointer));
38644
- }
38645
- run(device, matrix) {
38646
- checkType(device, Device);
38647
- checkMatrix(matrix);
38648
- libmupdf._wasm_pdf_run_annot(this.pointer, device.pointer, MATRIX(matrix));
38649
- }
38650
- toPixmap(matrix, colorspace, alpha = false) {
38651
- checkMatrix(matrix);
38652
- checkType(colorspace, ColorSpace);
38653
- return new Pixmap(libmupdf._wasm_pdf_new_pixmap_from_annot(this.pointer, MATRIX(matrix), colorspace.pointer, alpha));
38654
- }
38655
- toDisplayList() {
38656
- return new DisplayList(libmupdf._wasm_pdf_new_display_list_from_annot(this.pointer));
38657
- }
38658
- update() {
38659
- return !!libmupdf._wasm_pdf_update_annot(this.pointer);
38660
- }
38661
- getType() {
38662
- let type = libmupdf._wasm_pdf_annot_type(this.pointer);
38663
- return PDFAnnotation.ANNOT_TYPES[type] || "Text";
38664
- }
38665
- getLanguage() {
38666
- return fromStringOrNull(libmupdf._wasm_pdf_annot_language(this.pointer));
38667
- }
38668
- setLanguage(lang) {
38669
- libmupdf._wasm_pdf_set_annot_language(this.pointer, STRING(lang));
38670
- }
38671
- getFlags() {
38672
- return libmupdf._wasm_pdf_annot_flags(this.pointer);
38673
- }
38674
- setFlags(flags) {
38675
- return libmupdf._wasm_pdf_set_annot_flags(this.pointer, flags);
38676
- }
38677
- getContents() {
38678
- return fromString(libmupdf._wasm_pdf_annot_contents(this.pointer));
38679
- }
38680
- setContents(text) {
38681
- libmupdf._wasm_pdf_set_annot_contents(this.pointer, STRING(text));
38682
- }
38683
- getAuthor() {
38684
- return fromString(libmupdf._wasm_pdf_annot_author(this.pointer));
38685
- }
38686
- setAuthor(text) {
38687
- libmupdf._wasm_pdf_set_annot_author(this.pointer, STRING(text));
38688
- }
38689
- getCreationDate() {
38690
- return new Date(libmupdf._wasm_pdf_annot_creation_date(this.pointer) * 1000);
38691
- }
38692
- setCreationDate(date) {
38693
- checkType(date, Date);
38694
- libmupdf._wasm_pdf_set_annot_creation_date(this.pointer, date.getTime() / 1000);
38695
- }
38696
- getModificationDate() {
38697
- return new Date(libmupdf._wasm_pdf_annot_modification_date(this.pointer) * 1000);
38698
- }
38699
- setModificationDate(date) {
38700
- checkType(date, Date);
38701
- libmupdf._wasm_pdf_set_annot_modification_date(this.pointer, date.getTime() / 1000);
38702
- }
38703
- hasRect() {
38704
- return !!libmupdf._wasm_pdf_annot_has_rect(this.pointer);
38705
- }
38706
- hasInkList() {
38707
- return !!libmupdf._wasm_pdf_annot_has_ink_list(this.pointer);
38708
- }
38709
- hasQuadPoints() {
38710
- return !!libmupdf._wasm_pdf_annot_has_quad_points(this.pointer);
38711
- }
38712
- hasVertices() {
38713
- return !!libmupdf._wasm_pdf_annot_has_vertices(this.pointer);
38714
- }
38715
- hasLine() {
38716
- return !!libmupdf._wasm_pdf_annot_has_line(this.pointer);
38717
- }
38718
- hasInteriorColor() {
38719
- return !!libmupdf._wasm_pdf_annot_has_interior_color(this.pointer);
38720
- }
38721
- hasLineEndingStyles() {
38722
- return !!libmupdf._wasm_pdf_annot_has_line_ending_styles(this.pointer);
38723
- }
38724
- hasBorder() {
38725
- return !!libmupdf._wasm_pdf_annot_has_border(this.pointer);
38726
- }
38727
- hasBorderEffect() {
38728
- return !!libmupdf._wasm_pdf_annot_has_border_effect(this.pointer);
38729
- }
38730
- hasIcon() {
38731
- return !!libmupdf._wasm_pdf_annot_has_icon_name(this.pointer);
38732
- }
38733
- hasOpen() {
38734
- return !!libmupdf._wasm_pdf_annot_has_open(this.pointer);
38735
- }
38736
- hasAuthor() {
38737
- return !!libmupdf._wasm_pdf_annot_has_author(this.pointer);
38738
- }
38739
- hasFilespec() {
38740
- return !!libmupdf._wasm_pdf_annot_has_filespec(this.pointer);
38741
- }
38742
- hasCallout() {
38743
- return !!libmupdf._wasm_pdf_annot_has_callout(this.pointer);
38744
- }
38745
- hasRichContents() {
38746
- return !!libmupdf._wasm_pdf_annot_has_rich_contents(this.pointer);
38747
- }
38748
- getRect() {
38749
- return fromRect(libmupdf._wasm_pdf_annot_rect(this.pointer));
38750
- }
38751
- setRect(rect) {
38752
- checkRect(rect);
38753
- libmupdf._wasm_pdf_set_annot_rect(this.pointer, RECT(rect));
38754
- }
38755
- getPopup() {
38756
- return fromRect(libmupdf._wasm_pdf_annot_popup(this.pointer));
38757
- }
38758
- setPopup(rect) {
38759
- checkRect(rect);
38760
- libmupdf._wasm_pdf_set_annot_popup(this.pointer, RECT(rect));
38761
- }
38762
- getIsOpen() {
38763
- return !!libmupdf._wasm_pdf_annot_is_open(this.pointer);
38764
- }
38765
- setIsOpen(isOpen) {
38766
- checkType(isOpen, "boolean");
38767
- libmupdf._wasm_pdf_set_annot_is_open(this.pointer, isOpen);
38768
- }
38769
- getHiddenForEditing() {
38770
- return !!libmupdf._wasm_pdf_annot_hidden_for_editing(this.pointer);
38771
- }
38772
- setHiddenForEditing(isHidden) {
38773
- checkType(isHidden, "boolean");
38774
- libmupdf._wasm_pdf_set_annot_hidden_for_editing(this.pointer, isHidden);
38775
- }
38776
- getIcon() {
38777
- return fromString(libmupdf._wasm_pdf_annot_icon_name(this.pointer));
38778
- }
38779
- setIcon(text) {
38780
- checkType(text, "string");
38781
- libmupdf._wasm_pdf_set_annot_icon_name(this.pointer, STRING(text));
38782
- }
38783
- getOpacity() {
38784
- return libmupdf._wasm_pdf_annot_opacity(this.pointer);
38785
- }
38786
- setOpacity(opacity) {
38787
- checkType(opacity, "number");
38788
- libmupdf._wasm_pdf_set_annot_opacity(this.pointer, opacity);
38789
- }
38790
- getQuadding() {
38791
- return libmupdf._wasm_pdf_annot_quadding(this.pointer);
38792
- }
38793
- setQuadding(quadding) {
38794
- checkType(quadding, "number");
38795
- libmupdf._wasm_pdf_set_annot_quadding(this.pointer, quadding);
38796
- }
38797
- getLine() {
38798
- let a = fromPoint(libmupdf._wasm_pdf_annot_line_1(this.pointer));
38799
- let b = fromPoint(libmupdf._wasm_pdf_annot_line_2(this.pointer));
38800
- return [a, b];
38801
- }
38802
- setLine(a, b) {
38803
- checkPoint(a);
38804
- checkPoint(b);
38805
- libmupdf._wasm_pdf_set_annot_line(this.pointer, POINT(a), POINT2(b));
38806
- }
38807
- getLineEndingStyles() {
38808
- let a = libmupdf._wasm_pdf_annot_line_ending_styles_start(this.pointer);
38809
- let b = libmupdf._wasm_pdf_annot_line_ending_styles_end(this.pointer);
38810
- return {
38811
- start: PDFAnnotation.LINE_ENDING[a] || "None",
38812
- end: PDFAnnotation.LINE_ENDING[b] || "None",
38813
- };
38814
- }
38815
- setLineEndingStyles(start, end) {
38816
- let start_ix = ENUM(start, PDFAnnotation.LINE_ENDING);
38817
- let end_ix = ENUM(end, PDFAnnotation.LINE_ENDING);
38818
- libmupdf._wasm_pdf_set_annot_line_ending_styles(this.pointer, start_ix, end_ix);
38819
- }
38820
- getLineCaption() {
38821
- return libmupdf._wasm_pdf_annot_line_caption(this.pointer);
38822
- }
38823
- setLineCaption(on) {
38824
- return libmupdf._wasm_pdf_set_annot_line_caption(this.pointer, on);
38825
- }
38826
- getLineCaptionOffset() {
38827
- return fromPoint(libmupdf._wasm_pdf_annot_line_caption_offset(this.pointer));
38828
- }
38829
- setLineCaptionOffset(p) {
38830
- return libmupdf._wasm_pdf_set_annot_line_caption_offset(this.pointer, POINT(p));
38831
- }
38832
- getLineLeader() {
38833
- return libmupdf._wasm_pdf_annot_line_leader(this.pointer);
38834
- }
38835
- getLineLeaderExtension() {
38836
- return libmupdf._wasm_pdf_annot_line_leader_extension(this.pointer);
38837
- }
38838
- getLineLeaderOffset() {
38839
- return libmupdf._wasm_pdf_annot_line_leader_offset(this.pointer);
38840
- }
38841
- setLineLeader(v) {
38842
- return libmupdf._wasm_pdf_set_annot_line_leader(this.pointer, v);
38843
- }
38844
- setLineLeaderExtension(v) {
38845
- return libmupdf._wasm_pdf_set_annot_line_leader_extension(this.pointer, v);
38846
- }
38847
- setLineLeaderOffset(v) {
38848
- return libmupdf._wasm_pdf_set_annot_line_leader_offset(this.pointer, v);
38849
- }
38850
- getCalloutStyle() {
38851
- let style = libmupdf._wasm_pdf_annot_callout_style(this.pointer);
38852
- return PDFAnnotation.LINE_ENDING[style] || "None";
38853
- }
38854
- setCalloutStyle(style) {
38855
- let style_ix = ENUM(style, PDFAnnotation.LINE_ENDING);
38856
- libmupdf._wasm_pdf_set_annot_callout_style(this.pointer, style_ix);
38857
- }
38858
- getCalloutLine() {
38859
- let n = libmupdf._wasm_pdf_annot_callout_line(this.pointer, (_wasm_point << 2));
38860
- if (n == 3)
38861
- return [
38862
- fromPoint((_wasm_point + 0) << 2),
38863
- fromPoint((_wasm_point + 1) << 2),
38864
- fromPoint((_wasm_point + 2) << 2)
38865
- ];
38866
- if (n == 2)
38867
- return [
38868
- fromPoint((_wasm_point + 0) << 2),
38869
- fromPoint((_wasm_point + 1) << 2)
38870
- ];
38871
- return null;
38872
- }
38873
- setCalloutLine(line) {
38874
- let a = line[0] || [0, 0];
38875
- let b = line[1] || [0, 0];
38876
- let c = line[2] || [0, 0];
38877
- libmupdf._wasm_pdf_set_annot_callout_line(this.pointer, line.length, POINT(a), POINT2(b), POINT3(c));
38878
- }
38879
- getCalloutPoint() {
38880
- let line = this.getCalloutLine();
38881
- if (line)
38882
- return line[0];
38883
- return undefined;
38884
- }
38885
- setCalloutPoint(p) {
38886
- libmupdf._wasm_pdf_set_annot_callout_point(this.pointer, POINT(p));
38887
- }
38888
- getColor() {
38889
- return fromAnnotColor(libmupdf._wasm_pdf_annot_color(this.pointer, COLOR()));
38890
- }
38891
- getInteriorColor() {
38892
- return fromAnnotColor(libmupdf._wasm_pdf_annot_interior_color(this.pointer, COLOR()));
38893
- }
38894
- setColor(color) {
38895
- checkAnnotColor(color);
38896
- libmupdf._wasm_pdf_set_annot_color(this.pointer, color.length, COLOR(color));
38897
- }
38898
- setInteriorColor(color) {
38899
- checkAnnotColor(color);
38900
- libmupdf._wasm_pdf_set_annot_interior_color(this.pointer, color.length, COLOR(color));
38901
- }
38902
- getBorderWidth() {
38903
- return libmupdf._wasm_pdf_annot_border_width(this.pointer);
38904
- }
38905
- setBorderWidth(value) {
38906
- checkType(value, "number");
38907
- return libmupdf._wasm_pdf_set_annot_border_width(this.pointer, value);
38908
- }
38909
- getBorderStyle() {
38910
- return PDFAnnotation.BORDER_STYLE[libmupdf._wasm_pdf_annot_border_style(this.pointer)] || "Solid";
38911
- }
38912
- setBorderStyle(value) {
38913
- let value_ix = ENUM(value, PDFAnnotation.BORDER_STYLE);
38914
- return libmupdf._wasm_pdf_set_annot_border_style(this.pointer, value_ix);
38915
- }
38916
- getBorderEffect() {
38917
- return PDFAnnotation.BORDER_EFFECT[libmupdf._wasm_pdf_annot_border_effect(this.pointer)] || "None";
38918
- }
38919
- setBorderEffect(value) {
38920
- let value_ix = ENUM(value, PDFAnnotation.BORDER_EFFECT);
38921
- return libmupdf._wasm_pdf_set_annot_border_effect(this.pointer, value_ix);
38922
- }
38923
- getBorderEffectIntensity() {
38924
- return libmupdf._wasm_pdf_annot_border_effect_intensity(this.pointer);
38925
- }
38926
- setBorderEffectIntensity(value) {
38927
- checkType(value, "number");
38928
- return libmupdf._wasm_pdf_set_annot_border_effect_intensity(this.pointer, value);
38929
- }
38930
- getBorderDashCount() {
38931
- return libmupdf._wasm_pdf_annot_border_dash_count(this.pointer);
38932
- }
38933
- getBorderDashItem(idx) {
38934
- return libmupdf._wasm_pdf_annot_border_dash_item(this.pointer, idx);
38935
- }
38936
- clearBorderDash() {
38937
- libmupdf._wasm_pdf_clear_annot_border_dash(this.pointer);
38938
- }
38939
- addBorderDashItem(v) {
38940
- checkType(v, "number");
38941
- return libmupdf._wasm_pdf_add_annot_border_dash_item(this.pointer, v);
38942
- }
38943
- getBorderDashPattern() {
38944
- let n = this.getBorderDashCount();
38945
- let result = new Array(n);
38946
- for (let i = 0; i < n; ++i)
38947
- result[i] = this.getBorderDashItem(i);
38948
- return result;
38949
- }
38950
- setBorderDashPattern(list) {
38951
- this.clearBorderDash();
38952
- for (let v of list)
38953
- this.addBorderDashItem(v);
38954
- }
38955
- getIntent() {
38956
- return PDFAnnotation.INTENT[libmupdf._wasm_pdf_annot_intent(this.pointer)] || null;
38957
- }
38958
- setIntent(value) {
38959
- let value_ix = ENUM(value, PDFAnnotation.INTENT);
38960
- return libmupdf._wasm_pdf_set_annot_intent(this.pointer, value_ix);
38961
- }
38962
- setDefaultAppearance(fontName, size, color) {
38963
- checkType(fontName, "string");
38964
- checkType(size, "number");
38965
- checkAnnotColor(color);
38966
- libmupdf._wasm_pdf_set_annot_default_appearance(this.pointer, STRING(fontName), size, color.length, COLOR(color));
38967
- }
38968
- getDefaultAppearance() {
38969
- let font = fromString(libmupdf._wasm_pdf_annot_default_appearance_font(this.pointer));
38970
- let size = libmupdf._wasm_pdf_annot_default_appearance_size(this.pointer);
38971
- let color = fromAnnotColor(libmupdf._wasm_pdf_annot_default_appearance_color(this.pointer, COLOR()));
38972
- return { font, size, color };
38973
- }
38974
- getFileSpec() {
38975
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_annot_filespec(this.pointer));
38976
- }
38977
- setFileSpec(fs) {
38978
- return libmupdf._wasm_pdf_set_annot_filespec(this.pointer, this._doc._PDFOBJ(fs));
38979
- }
38980
- getQuadPoints() {
38981
- let n = libmupdf._wasm_pdf_annot_quad_point_count(this.pointer);
38982
- let result = [];
38983
- for (let i = 0; i < n; ++i)
38984
- result.push(fromQuad(libmupdf._wasm_pdf_annot_quad_point(this.pointer, i)));
38985
- return result;
38986
- }
38987
- clearQuadPoints() {
38988
- libmupdf._wasm_pdf_clear_annot_quad_points(this.pointer);
38989
- }
38990
- addQuadPoint(quad) {
38991
- checkQuad(quad);
38992
- libmupdf._wasm_pdf_add_annot_quad_point(this.pointer, QUAD(quad));
38993
- }
38994
- setQuadPoints(quadlist) {
38995
- this.clearQuadPoints();
38996
- for (let quad of quadlist)
38997
- this.addQuadPoint(quad);
38998
- }
38999
- getVertices() {
39000
- let n = libmupdf._wasm_pdf_annot_vertex_count(this.pointer);
39001
- let result = new Array(n);
39002
- for (let i = 0; i < n; ++i)
39003
- result[i] = fromPoint(libmupdf._wasm_pdf_annot_vertex(this.pointer, i));
39004
- return result;
39005
- }
39006
- clearVertices() {
39007
- libmupdf._wasm_pdf_clear_annot_vertices(this.pointer);
39008
- }
39009
- addVertex(vertex) {
39010
- checkPoint(vertex);
39011
- libmupdf._wasm_pdf_add_annot_vertex(this.pointer, POINT(vertex));
39012
- }
39013
- setVertices(vertexlist) {
39014
- this.clearVertices();
39015
- for (let vertex of vertexlist)
39016
- this.addVertex(vertex);
39017
- }
39018
- getInkList() {
39019
- let n = libmupdf._wasm_pdf_annot_ink_list_count(this.pointer);
39020
- let outer = [];
39021
- for (let i = 0; i < n; ++i) {
39022
- let m = libmupdf._wasm_pdf_annot_ink_list_stroke_count(this.pointer, i);
39023
- let inner = new Array(m);
39024
- for (let k = 0; k < m; ++k)
39025
- inner[k] = fromPoint(libmupdf._wasm_pdf_annot_ink_list_stroke_vertex(this.pointer, i, k));
39026
- outer.push(inner);
39027
- }
39028
- return outer;
39029
- }
39030
- clearInkList() {
39031
- libmupdf._wasm_pdf_clear_annot_ink_list(this.pointer);
39032
- }
39033
- addInkListStroke() {
39034
- libmupdf._wasm_pdf_add_annot_ink_list_stroke(this.pointer);
39035
- }
39036
- addInkListStrokeVertex(v) {
39037
- checkPoint(v);
39038
- libmupdf._wasm_pdf_add_annot_ink_list_stroke_vertex(this.pointer, POINT(v));
39039
- }
39040
- setInkList(inklist) {
39041
- this.clearInkList();
39042
- for (let stroke of inklist) {
39043
- this.addInkListStroke();
39044
- for (let vertex of stroke)
39045
- this.addInkListStrokeVertex(vertex);
39046
- }
39047
- }
39048
- getRichContents() {
39049
- return fromString(libmupdf._wasm_pdf_annot_rich_contents(this.pointer));
39050
- }
39051
- setRichContents(plain, html) {
39052
- checkType(plain, "string");
39053
- checkType(html, "string");
39054
- libmupdf._wasm_pdf_set_annot_rich_contents(this.pointer, STRING(plain), STRING2(html));
39055
- }
39056
- getRichDefaults() {
39057
- return fromString(libmupdf._wasm_pdf_annot_rich_defaults(this.pointer));
39058
- }
39059
- setRichDefaults(style) {
39060
- checkType(style, "string");
39061
- libmupdf._wasm_pdf_set_annot_rich_defaults(this.pointer, STRING(style));
39062
- }
39063
- setStampImage(image) {
39064
- libmupdf._wasm_pdf_set_annot_stamp_image(this.pointer, image.pointer);
39065
- }
39066
- setAppearanceFromDisplayList(appearance, state, transform, list) {
39067
- checkMatrix(transform);
39068
- checkType(list, DisplayList);
39069
- libmupdf._wasm_pdf_set_annot_appearance_from_display_list(this.pointer, STRING_OPT(appearance), STRING2_OPT(state), MATRIX(transform), list.pointer);
39070
- }
39071
- setAppearance(appearance, state, transform, bbox, resources, contents) {
39072
- checkMatrix(transform);
39073
- checkRect(bbox);
39074
- libmupdf._wasm_pdf_set_annot_appearance(this.pointer, STRING_OPT(appearance), STRING2_OPT(state), MATRIX(transform), RECT(bbox), this._doc._PDFOBJ(resources), BUFFER(contents));
39075
- }
39076
- applyRedaction(black_boxes = 1, image_method = 2, line_art_method = 1, text_method = 0) {
39077
- libmupdf._wasm_pdf_apply_redaction(this.pointer, black_boxes, image_method, line_art_method, text_method);
39078
- }
39079
- }
39080
- PDFAnnotation._drop = libmupdf._wasm_pdf_drop_annot;
39081
- /* IMPORTANT: Keep in sync with mupdf/pdf/annot.h and PDFAnnotation.java */
39082
- PDFAnnotation.ANNOT_TYPES = [
39083
- "Text",
39084
- "Link",
39085
- "FreeText",
39086
- "Line",
39087
- "Square",
39088
- "Circle",
39089
- "Polygon",
39090
- "PolyLine",
39091
- "Highlight",
39092
- "Underline",
39093
- "Squiggly",
39094
- "StrikeOut",
39095
- "Redact",
39096
- "Stamp",
39097
- "Caret",
39098
- "Ink",
39099
- "Popup",
39100
- "FileAttachment",
39101
- "Sound",
39102
- "Movie",
39103
- "RichMedia",
39104
- "Widget",
39105
- "Screen",
39106
- "PrinterMark",
39107
- "TrapNet",
39108
- "Watermark",
39109
- "3D",
39110
- "Projection",
39111
- ];
39112
- PDFAnnotation.LINE_ENDING = [
39113
- "None",
39114
- "Square",
39115
- "Circle",
39116
- "Diamond",
39117
- "OpenArrow",
39118
- "ClosedArrow",
39119
- "Butt",
39120
- "ROpenArrow",
39121
- "RClosedArrow",
39122
- "Slash",
39123
- ];
39124
- PDFAnnotation.BORDER_STYLE = ["Solid", "Dashed", "Beveled", "Inset", "Underline"];
39125
- PDFAnnotation.BORDER_EFFECT = ["None", "Cloudy"];
39126
- PDFAnnotation.INTENT = [
39127
- null,
39128
- "FreeTextCallout",
39129
- "FreeTextTypeWriter",
39130
- "LineArrow",
39131
- "LineDimension",
39132
- "PloyLine",
39133
- "PolygonCloud",
39134
- "PolygonDimension",
39135
- "StampImage",
39136
- "StampSnapshot"
39137
- ];
39138
- // Bit masks for getFlags and setFlags
39139
- PDFAnnotation.IS_INVISIBLE = 1 << (1 - 1);
39140
- PDFAnnotation.IS_HIDDEN = 1 << (2 - 1);
39141
- PDFAnnotation.IS_PRINT = 1 << (3 - 1);
39142
- PDFAnnotation.IS_NO_ZOOM = 1 << (4 - 1);
39143
- PDFAnnotation.IS_NO_ROTATE = 1 << (5 - 1);
39144
- PDFAnnotation.IS_NO_VIEW = 1 << (6 - 1);
39145
- PDFAnnotation.IS_READ_ONLY = 1 << (7 - 1);
39146
- PDFAnnotation.IS_LOCKED = 1 << (8 - 1);
39147
- PDFAnnotation.IS_TOGGLE_NO_VIEW = 1 << (9 - 1);
39148
- PDFAnnotation.IS_LOCKED_CONTENTS = 1 << (10 - 1);
39149
- class PDFWidget extends PDFAnnotation {
39150
- getFieldType() {
39151
- return PDFWidget.WIDGET_TYPES[libmupdf._wasm_pdf_annot_field_type(this.pointer)] || "button";
39152
- }
39153
- isButton() {
39154
- let type = this.getFieldType();
39155
- return type === "button" || type === "checkbox" || type === "radiobutton";
39156
- }
39157
- isPushButton() {
39158
- return this.getFieldType() === "button";
39159
- }
39160
- isCheckbox() {
39161
- return this.getFieldType() === "checkbox";
39162
- }
39163
- isRadioButton() {
39164
- return this.getFieldType() === "radiobutton";
39165
- }
39166
- isText() {
39167
- return this.getFieldType() === "text";
39168
- }
39169
- isChoice() {
39170
- let type = this.getFieldType();
39171
- return type === "combobox" || type === "listbox";
39172
- }
39173
- isListBox() {
39174
- return this.getFieldType() === "listbox";
39175
- }
39176
- isComboBox() {
39177
- return this.getFieldType() === "combobox";
39178
- }
39179
- getFieldFlags() {
39180
- return libmupdf._wasm_pdf_annot_field_flags(this.pointer);
39181
- }
39182
- isMultiline() {
39183
- return (this.getFieldFlags() & PDFWidget.TX_FIELD_IS_MULTILINE) !== 0;
39184
- }
39185
- isPassword() {
39186
- return (this.getFieldFlags() & PDFWidget.TX_FIELD_IS_PASSWORD) !== 0;
39187
- }
39188
- isComb() {
39189
- return (this.getFieldFlags() & PDFWidget.TX_FIELD_IS_COMB) !== 0;
39190
- }
39191
- isReadOnly() {
39192
- return (this.getFieldFlags() & PDFWidget.FIELD_IS_READ_ONLY) !== 0;
39193
- }
39194
- getLabel() {
39195
- return fromString(libmupdf._wasm_pdf_annot_field_label(this.pointer));
39196
- }
39197
- getName() {
39198
- return fromStringFree(libmupdf._wasm_pdf_load_field_name(this.pointer));
39199
- }
39200
- getValue() {
39201
- return fromString(libmupdf._wasm_pdf_annot_field_value(this.pointer));
39202
- }
39203
- setTextValue(value) {
39204
- return libmupdf._wasm_pdf_set_annot_text_field_value(this.pointer, STRING(value));
39205
- }
39206
- getMaxLen() {
39207
- return libmupdf._wasm_pdf_annot_text_widget_max_len(this.pointer);
39208
- }
39209
- setChoiceValue(value) {
39210
- return libmupdf._wasm_pdf_set_annot_choice_field_value(this.pointer, STRING(value));
39211
- }
39212
- getOptions(isExport = false) {
39213
- let result = [];
39214
- let n = libmupdf._wasm_pdf_annot_choice_field_option_count(this.pointer);
39215
- for (let i = 0; i < n; ++i) {
39216
- result.push(fromString(libmupdf._wasm_pdf_annot_choice_field_option(this.pointer, isExport, i)));
39217
- }
39218
- return result;
39219
- }
39220
- toggle() {
39221
- return libmupdf._wasm_pdf_toggle_widget(this.pointer);
39222
- }
39223
- }
39224
- /* IMPORTANT: Keep in sync with mupdf/pdf/widget.h and PDFWidget.java */
39225
- PDFWidget.WIDGET_TYPES = [
39226
- "widget", // unknown
39227
- "button",
39228
- "checkbox",
39229
- "combobox",
39230
- "listbox",
39231
- "radiobutton",
39232
- "signature",
39233
- "text",
39234
- ];
39235
- /* Field flags */
39236
- PDFWidget.FIELD_IS_READ_ONLY = 1;
39237
- PDFWidget.FIELD_IS_REQUIRED = 1 << 1;
39238
- PDFWidget.FIELD_IS_NO_EXPORT = 1 << 2;
39239
- /* Text fields */
39240
- PDFWidget.TX_FIELD_IS_MULTILINE = 1 << 12;
39241
- PDFWidget.TX_FIELD_IS_PASSWORD = 1 << 13;
39242
- PDFWidget.TX_FIELD_IS_COMB = 1 << 24;
39243
- /* Button fields */
39244
- PDFWidget.BTN_FIELD_IS_NO_TOGGLE_TO_OFF = 1 << 14;
39245
- PDFWidget.BTN_FIELD_IS_RADIO = 1 << 15;
39246
- PDFWidget.BTN_FIELD_IS_PUSHBUTTON = 1 << 16;
39247
- /* Choice fields */
39248
- PDFWidget.CH_FIELD_IS_COMBO = 1 << 17;
39249
- PDFWidget.CH_FIELD_IS_EDIT = 1 << 18;
39250
- PDFWidget.CH_FIELD_IS_SORT = 1 << 19;
39251
- PDFWidget.CH_FIELD_IS_MULTI_SELECT = 1 << 21;
39252
- var $libmupdf_stm_id = 0;
39253
- var $libmupdf_stm_table = new Map();
39254
- globalThis.$libmupdf_stm_close = function (id) {
39255
- let handle = $libmupdf_stm_table.get(id);
39256
- if (handle) {
39257
- handle.close();
39258
- $libmupdf_stm_table.delete(id);
39259
- return;
39260
- }
39261
- throw new Error("invalid file handle");
39262
- };
39263
- globalThis.$libmupdf_stm_seek = function (id, pos, offset, whence) {
39264
- let handle = $libmupdf_stm_table.get(id);
39265
- if (handle) {
39266
- if (whence === 0)
39267
- return offset;
39268
- if (whence === 1)
39269
- return pos + offset;
39270
- if (whence === 2) {
39271
- let size = handle.fileSize();
39272
- if (size < 0)
39273
- return -1;
39274
- return size + offset;
39275
- }
39276
- throw new Error("invalid whence argument");
39277
- }
39278
- throw new Error("invalid file handle");
39279
- };
39280
- globalThis.$libmupdf_stm_read = function (id, pos, addr, size) {
39281
- let handle = $libmupdf_stm_table.get(id);
39282
- if (handle) {
39283
- return handle.read(libmupdf.HEAPU8, addr, size, pos);
39284
- }
39285
- throw new Error("invalid file handle");
39286
- };
39287
- class Stream extends Userdata {
39288
- constructor(handle) {
39289
- let id = $libmupdf_stm_id++;
39290
- $libmupdf_stm_table.set(id, handle);
39291
- super(libmupdf._wasm_new_stream(id));
39292
- }
39293
- }
39294
- Stream._drop = libmupdf._wasm_drop_stream;
39295
- globalThis.$libmupdf_load_font_file = function (name, script, bold, italic) {
39296
- return 0;
39297
- };
39298
- var $libmupdf_device_id = 0;
39299
- var $libmupdf_device_table = new Map();
39300
- var $libmupdf_path_id = 0;
39301
- var $libmupdf_path_table = new Map();
39302
- var $libmupdf_text_id = 0;
39303
- var $libmupdf_text_table = new Map();
39304
- globalThis.$libmupdf_path_walk = {
39305
- moveto(id, x, y) {
39306
- $libmupdf_path_table.get(id)?.moveTo?.(x, y);
39307
- },
39308
- lineto(id, x, y) {
39309
- $libmupdf_path_table.get(id)?.lineTo?.(x, y);
39310
- },
39311
- curveto(id, x1, y1, x2, y2, x3, y3) {
39312
- $libmupdf_path_table.get(id)?.curveTo?.(x1, y1, x2, y2, x3, y3);
39313
- },
39314
- closepath(id) {
39315
- $libmupdf_path_table.get(id)?.closePath?.();
39316
- },
39317
- };
39318
- var $libmupdf_text_font = null;
39319
- globalThis.$libmupdf_text_walk = {
39320
- begin_span(id, font, trm, wmode, bidi, dir, lang) {
39321
- if (font !== $libmupdf_text_font?.pointer)
39322
- $libmupdf_text_font = new Font(font);
39323
- $libmupdf_text_table.get(id)?.beginSpan?.($libmupdf_text_font, fromMatrix(trm), wmode, bidi, dir, fromString(lang));
39324
- },
39325
- end_span(id) {
39326
- $libmupdf_text_table.get(id)?.endSpan?.();
39327
- },
39328
- show_glyph(id, font, trm, glyph, unicode, wmode, bidi) {
39329
- if (font !== $libmupdf_text_font?.pointer)
39330
- $libmupdf_text_font = new Font(font);
39331
- $libmupdf_text_table.get(id)?.showGlyph?.($libmupdf_text_font, fromMatrix(trm), glyph, unicode, wmode, bidi);
39332
- },
36434
+ const _enabled = (() => {
36435
+ try {
36436
+ if (typeof process !== "undefined" &&
36437
+ process.env &&
36438
+ process.env.PDFDIFF_PROFILE === "1") {
36439
+ return true;
36440
+ }
36441
+ }
36442
+ catch {
36443
+ // process not available (e.g. in some browser worker environments)
36444
+ }
36445
+ const g = globalThis;
36446
+ return g.__PDFDIFF_PROFILE__ === true;
36447
+ })();
36448
+ const _counters = Object.create(null);
36449
+ const _NOOP_SPAN = Object.freeze({ stop() { } });
36450
+ const _noop = () => { };
36451
+ const _emptyDump = () => Object.freeze({});
36452
+ const _realPerf = {
36453
+ enabled: true,
36454
+ span(key) {
36455
+ const t0 = performance.now();
36456
+ return {
36457
+ stop() {
36458
+ _counters[key] = (_counters[key] ?? 0) + (performance.now() - t0);
36459
+ },
36460
+ };
36461
+ },
36462
+ incr(key, delta = 1) {
36463
+ _counters[key] = (_counters[key] ?? 0) + delta;
36464
+ },
36465
+ setMax(key, value) {
36466
+ const cur = _counters[key];
36467
+ if (cur === undefined || value > cur)
36468
+ _counters[key] = value;
36469
+ },
36470
+ merge(other) {
36471
+ for (const k of Object.keys(other)) {
36472
+ _counters[k] = (_counters[k] ?? 0) + other[k];
36473
+ }
36474
+ },
36475
+ dump() {
36476
+ return { ..._counters };
36477
+ },
36478
+ reset() {
36479
+ for (const k of Object.keys(_counters))
36480
+ delete _counters[k];
36481
+ },
39333
36482
  };
39334
- globalThis.$libmupdf_device = {
39335
- drop_device(id) {
39336
- $libmupdf_device_table.get(id)?.drop?.();
39337
- $libmupdf_device_table.delete(id);
39338
- },
39339
- close_device(id) {
39340
- $libmupdf_device_table.get(id)?.close?.();
39341
- },
39342
- fill_path(id, path, even_odd, ctm, colorspace, color_n, color_arr, alpha) {
39343
- $libmupdf_device_table.get(id)?.fillPath?.(new Path(path), !!even_odd, fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39344
- },
39345
- clip_path(id, path, even_odd, ctm) {
39346
- $libmupdf_device_table.get(id)?.clipPath?.(new Path(path), !!even_odd, fromMatrix(ctm));
39347
- },
39348
- stroke_path(id, path, stroke, ctm, colorspace, color_n, color_arr, alpha) {
39349
- $libmupdf_device_table.get(id)?.strokePath?.(new Path(path), new StrokeState(stroke), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39350
- },
39351
- clip_stroke_path(id, path, stroke, ctm) {
39352
- $libmupdf_device_table.get(id)?.clipStrokePath?.(new Path(path), new StrokeState(stroke), fromMatrix(ctm));
39353
- },
39354
- fill_text(id, text, ctm, colorspace, color_n, color_arr, alpha) {
39355
- $libmupdf_device_table.get(id)?.fillText?.(new Text(text), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39356
- },
39357
- stroke_text(id, text, stroke, ctm, colorspace, color_n, color_arr, alpha) {
39358
- $libmupdf_device_table.get(id)?.strokeText?.(new Text(text), new StrokeState(stroke), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39359
- },
39360
- clip_text(id, text, ctm) {
39361
- $libmupdf_device_table.get(id)?.clipText?.(new Text(text), fromMatrix(ctm));
39362
- },
39363
- clip_stroke_text(id, text, stroke, ctm) {
39364
- $libmupdf_device_table.get(id)?.clipStrokeText?.(new Text(text), new StrokeState(stroke), fromMatrix(ctm));
39365
- },
39366
- ignore_text(id, text, ctm) {
39367
- $libmupdf_device_table.get(id)?.ignoreText?.(new Text(text), fromMatrix(ctm));
39368
- },
39369
- fill_shade(id, shade, ctm, alpha) {
39370
- $libmupdf_device_table.get(id)?.fillShade?.(new Shade(shade), fromMatrix(ctm), alpha);
39371
- },
39372
- fill_image(id, image, ctm, alpha) {
39373
- $libmupdf_device_table.get(id)?.fillImage?.(new Image(image), fromMatrix(ctm), alpha);
39374
- },
39375
- fill_image_mask(id, image, ctm, colorspace, color_n, color_arr, alpha) {
39376
- $libmupdf_device_table.get(id)?.fillImageMask?.(new Image(image), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39377
- },
39378
- clip_image_mask(id, image, ctm) {
39379
- $libmupdf_device_table.get(id)?.clipImageMask?.(new Image(image), fromMatrix(ctm));
39380
- },
39381
- pop_clip(id) {
39382
- $libmupdf_device_table.get(id)?.popClip?.();
39383
- },
39384
- begin_mask(id, bbox, luminosity, colorspace, color_n, color_arr) {
39385
- $libmupdf_device_table.get(id)?.beginMask?.(fromRect(bbox), !!luminosity, new ColorSpace(colorspace), fromColorArray(color_n, color_arr));
39386
- },
39387
- begin_group(id, bbox, colorspace, isolated, knockout, blendmode, alpha) {
39388
- $libmupdf_device_table.get(id)?.beginGroup?.(fromRect(bbox), new ColorSpace(colorspace), !!isolated, !!knockout, Device.BLEND_MODES[blendmode], alpha);
39389
- },
39390
- begin_tile(id, area, view, xstep, ystep, ctm, tile_id) {
39391
- return $libmupdf_device_table.get(id)?.beginTile?.(fromRect(area), fromRect(view), xstep, ystep, fromMatrix(ctm), tile_id) || 0;
39392
- },
39393
- begin_layer(id, name) {
39394
- $libmupdf_device_table.get(id)?.beginLayer?.(fromString(name));
39395
- },
39396
- end_mask(id) {
39397
- $libmupdf_device_table.get(id)?.endMask?.();
39398
- },
39399
- end_group(id) {
39400
- $libmupdf_device_table.get(id)?.endGroup?.();
39401
- },
39402
- end_tile(id) {
39403
- $libmupdf_device_table.get(id)?.endTile?.();
39404
- },
39405
- end_layer(id) {
39406
- $libmupdf_device_table.get(id)?.endLayer?.();
39407
- },
36483
+ const _noopPerf = {
36484
+ enabled: false,
36485
+ span: () => _NOOP_SPAN,
36486
+ incr: _noop,
36487
+ setMax: _noop,
36488
+ merge: _noop,
36489
+ dump: _emptyDump,
36490
+ reset: _noop,
39408
36491
  };
36492
+ const perf = _enabled ? _realPerf : _noopPerf;
39409
36493
 
39410
- /*
39411
- * Copyright (C) 2025 Koutaro Mukai
39412
- *
39413
- * This program is free software: you can redistribute it and/or modify
39414
- * it under the terms of the GNU General Public License as published by
39415
- * the Free Software Foundation, either version 3 of the License, or
39416
- * (at your option) any later version.
39417
- *
39418
- * This program is distributed in the hope that it will be useful,
39419
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39420
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39421
- * GNU General Public License for more details.
39422
- *
39423
- * You should have received a copy of the GNU General Public License
39424
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39425
- */
39426
36494
  const alignStrategyValues = new Set([
39427
36495
  "resize",
39428
36496
  "top-left",
@@ -39437,22 +36505,6 @@ const alignStrategyValues = new Set([
39437
36505
  ]);
39438
36506
  const isValidAlignStrategy = (str) => alignStrategyValues.has(str);
39439
36507
 
39440
- /*
39441
- * Copyright (C) 2025 Koutaro Mukai
39442
- *
39443
- * This program is free software: you can redistribute it and/or modify
39444
- * it under the terms of the GNU General Public License as published by
39445
- * the Free Software Foundation, either version 3 of the License, or
39446
- * (at your option) any later version.
39447
- *
39448
- * This program is distributed in the hope that it will be useful,
39449
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39450
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39451
- * GNU General Public License for more details.
39452
- *
39453
- * You should have received a copy of the GNU General Public License
39454
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39455
- */
39456
36508
  const parseHex = (hex) => {
39457
36509
  if (/^#([0-9a-fA-F]{3})$/.test(hex)) {
39458
36510
  return [
@@ -39496,22 +36548,6 @@ const formatHex = ([r, g, b, a]) => "#" +
39496
36548
  })
39497
36549
  .join("");
39498
36550
 
39499
- /*
39500
- * Copyright (C) 2025 Koutaro Mukai
39501
- *
39502
- * This program is free software: you can redistribute it and/or modify
39503
- * it under the terms of the GNU General Public License as published by
39504
- * the Free Software Foundation, either version 3 of the License, or
39505
- * (at your option) any later version.
39506
- *
39507
- * This program is distributed in the hope that it will be useful,
39508
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39509
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39510
- * GNU General Public License for more details.
39511
- *
39512
- * You should have received a copy of the GNU General Public License
39513
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39514
- */
39515
36551
  async function* withIndex(iter, start = 0) {
39516
36552
  let index = start;
39517
36553
  for await (const item of iter) {
@@ -39520,24 +36556,122 @@ async function* withIndex(iter, start = 0) {
39520
36556
  }
39521
36557
  }
39522
36558
 
39523
- const VERSION = "0.2.1";
39524
-
39525
36559
  /*
39526
- * Copyright (C) 2025 Koutaro Mukai
39527
- *
39528
- * This program is free software: you can redistribute it and/or modify
39529
- * it under the terms of the GNU General Public License as published by
39530
- * the Free Software Foundation, either version 3 of the License, or
39531
- * (at your option) any later version.
36560
+ * Pages are rendered with Ghostscript (gs-wasm). Each gs() invocation spins up
36561
+ * its own worker and Ghostscript instance, so we render one page per call and
36562
+ * let the caller drive concurrency (e.g. by rendering A/B/mask together and by
36563
+ * running multiple page workers).
39532
36564
  *
39533
- * This program is distributed in the hope that it will be useful,
39534
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39535
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39536
- * GNU General Public License for more details.
39537
- *
39538
- * You should have received a copy of the GNU General Public License
39539
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
36565
+ * Ghostscript (via the `web-worker` package) must be invoked from a context
36566
+ * that can itself spawn a worker. In Node's `web-worker` this is only the main
36567
+ * thread, so rendering happens on the main thread and the resulting PNG bytes
36568
+ * are handed off to the diff workers for decoding. The PDF is placed in
36569
+ * Ghostscript's in-memory FS as `input.pdf` and the page is read back as a PNG.
36570
+ */
36571
+ const INPUT_VM_PATH = "input.pdf";
36572
+ /**
36573
+ * Count the pages of a PDF using Ghostscript's `pdfpagecount`. Runs with
36574
+ * `-dNODISPLAY` (no rendering) so it is cheap relative to a page render.
36575
+ */
36576
+ async function countPages(pdf) {
36577
+ const span = perf.span("pdf.countPages_ms");
36578
+ const out = [];
36579
+ const { exitCode } = await gs({
36580
+ args: [
36581
+ "-q",
36582
+ "-dNODISPLAY",
36583
+ "-dNOSAFER",
36584
+ "-c",
36585
+ `(${INPUT_VM_PATH}) (r) file runpdfbegin pdfpagecount = quit`,
36586
+ ],
36587
+ inputFiles: { [INPUT_VM_PATH]: pdf },
36588
+ onStdout: (charCode) => {
36589
+ if (charCode !== null)
36590
+ out.push(charCode);
36591
+ },
36592
+ });
36593
+ span.stop();
36594
+ if (exitCode !== 0) {
36595
+ throw new Error(`gs countPages failed (exit ${exitCode})`);
36596
+ }
36597
+ const text = String.fromCharCode(...out).trim();
36598
+ const n = Number.parseInt(text, 10);
36599
+ if (!Number.isFinite(n) || n < 0) {
36600
+ throw new Error(`gs countPages: unexpected output ${JSON.stringify(text)}`);
36601
+ }
36602
+ return n;
36603
+ }
36604
+ /**
36605
+ * Render an inclusive range of (0-based) pages of a PDF to PNG bytes in a single
36606
+ * Ghostscript invocation, returning a map keyed by 0-based page index. Batching
36607
+ * several pages per call amortizes Ghostscript's startup and PDF parsing, which
36608
+ * dominate a single-page render. `alpha` selects the device: `pngalpha` keeps
36609
+ * the page background transparent (so the diff can tell "no content" from
36610
+ * "content" via the alpha channel), while `png16m` renders opaque. Decoding to
36611
+ * RGBA is left to the caller (the diff workers) so it can run off this thread.
36612
+ */
36613
+ async function renderPageRangePng(pdf, firstIndex, lastIndex, dpi, alpha) {
36614
+ const device = alpha ? "pngalpha" : "png16m";
36615
+ const first = firstIndex + 1; // Ghostscript page numbers are 1-based.
36616
+ const last = lastIndex + 1;
36617
+ // `%d` in the output pattern is the 1-based index of the page *within this
36618
+ // run* (it restarts at 1 regardless of -dFirstPage), so the k-th output maps
36619
+ // back to absolute page (first + k - 1).
36620
+ const pageCount = last - first + 1;
36621
+ const names = [];
36622
+ for (let k = 1; k <= pageCount; k++)
36623
+ names.push(`out-${k}.png`);
36624
+ const sRender = perf.span("pdf.gsRender_ms");
36625
+ const { exitCode, outputFiles } = await gs({
36626
+ args: [
36627
+ "-dNOPAUSE",
36628
+ "-dBATCH",
36629
+ "-dQUIET",
36630
+ `-dFirstPage=${first}`,
36631
+ `-dLastPage=${last}`,
36632
+ `-sDEVICE=${device}`,
36633
+ `-r${dpi}`,
36634
+ "-dTextAlphaBits=4",
36635
+ "-dGraphicsAlphaBits=4",
36636
+ "-sOutputFile=out-%d.png",
36637
+ INPUT_VM_PATH,
36638
+ ],
36639
+ inputFiles: { [INPUT_VM_PATH]: pdf },
36640
+ outputFilePaths: names,
36641
+ });
36642
+ sRender.stop();
36643
+ if (exitCode !== 0) {
36644
+ throw new Error(`gs render failed (pages ${first}-${last}, exit ${exitCode})`);
36645
+ }
36646
+ const result = new Map();
36647
+ for (let k = 1; k <= pageCount; k++) {
36648
+ const png = outputFiles[`out-${k}.png`];
36649
+ if (!png) {
36650
+ throw new Error(`gs render produced no output (page ${first + k - 1})`);
36651
+ }
36652
+ result.set(firstIndex + (k - 1), png);
36653
+ }
36654
+ perf.incr("pdf.gsCalls");
36655
+ perf.incr("pdf.pagesRendered", pageCount);
36656
+ return result;
36657
+ }
36658
+
36659
+ /**
36660
+ * Slice an ArrayBufferView into a standalone backing buffer of the same kind
36661
+ * (ArrayBuffer in, ArrayBuffer out; SharedArrayBuffer in, SharedArrayBuffer
36662
+ * out). The buffer kind is preserved through the generic parameter.
39540
36663
  */
36664
+ function sliceBackingBuffer(src) {
36665
+ return src.buffer.slice(src.byteOffset, src.byteOffset + src.byteLength);
36666
+ }
36667
+
36668
+ const VERSION = "0.3.0";
36669
+
36670
+ // Default parallelism scales with the machine: rendering and diffing run across
36671
+ // several workers, so the out-of-the-box run uses the CPU rather than a single
36672
+ // core. Capped at 4 to keep the default memory footprint and oversubscription
36673
+ // modest; raise --workers explicitly for large jobs on big machines.
36674
+ const defaultWorkers = Math.max(1, Math.min(globalThis.navigator?.hardwareConcurrency ?? 1, 4));
39541
36675
  const defaultOptions = {
39542
36676
  dpi: 150,
39543
36677
  alpha: true,
@@ -39548,21 +36682,8 @@ const defaultOptions = {
39548
36682
  deletion: [0xff, 0x57, 0x24, 0xff],
39549
36683
  modification: [0xff, 0xc1, 0x05, 0xff],
39550
36684
  },
39551
- workers: 1,
36685
+ workers: defaultWorkers,
39552
36686
  };
39553
- function asSharedBytes(bytes) {
39554
- const isNode = typeof globalThis.process !== "undefined" &&
39555
- !!globalThis.process.versions?.node;
39556
- const coiOk = globalThis.crossOriginIsolated ===
39557
- true;
39558
- if (typeof SharedArrayBuffer !== "undefined" && (isNode || coiOk)) {
39559
- const sab = new SharedArrayBuffer(bytes.byteLength);
39560
- const view = new Uint8Array(sab);
39561
- view.set(bytes);
39562
- return view;
39563
- }
39564
- return new Uint8Array(bytes);
39565
- }
39566
36687
  class WorkerHandle {
39567
36688
  worker;
39568
36689
  loaded;
@@ -39607,12 +36728,22 @@ class WorkerHandle {
39607
36728
  this.worker.postMessage(msg);
39608
36729
  });
39609
36730
  }
39610
- processPage(index) {
36731
+ processDiff(index, a, b, mask) {
39611
36732
  return new Promise((resolve, reject) => {
39612
36733
  this.pendingResolve = resolve;
39613
36734
  this.pendingReject = reject;
39614
- const msg = { type: "page", index };
39615
- this.worker.postMessage(msg);
36735
+ const aBuf = a !== null ? sliceBackingBuffer(a) : null;
36736
+ const bBuf = b !== null ? sliceBackingBuffer(b) : null;
36737
+ const maskBuf = mask !== null ? sliceBackingBuffer(mask) : null;
36738
+ const msg = {
36739
+ type: "page",
36740
+ index,
36741
+ a: aBuf,
36742
+ b: bBuf,
36743
+ mask: maskBuf,
36744
+ };
36745
+ const transfer = [aBuf, bBuf, maskBuf].filter((buf) => buf !== null);
36746
+ this.worker.postMessage(msg, transfer);
39616
36747
  });
39617
36748
  }
39618
36749
  terminate() {
@@ -39623,8 +36754,17 @@ function workerUrl() {
39623
36754
  const file = import.meta.url.endsWith(".ts") ? "./worker.ts" : "./worker.js";
39624
36755
  return new URL(`${file}?v=${encodeURIComponent(VERSION)}`, import.meta.url);
39625
36756
  }
36757
+ function unpackCoords(buf) {
36758
+ const arr = new Int32Array(buf);
36759
+ const out = new Array(arr.length >>> 1);
36760
+ for (let i = 0, j = 0; j < out.length; i += 2, j++) {
36761
+ out[j] = [arr[i], arr[i + 1]];
36762
+ }
36763
+ return out;
36764
+ }
39626
36765
  function pageResultToResult(msg) {
39627
- return {
36766
+ const sP = perf.span("main.pageResultToResult_ms");
36767
+ const r = {
39628
36768
  a: Jimp.fromBitmap({
39629
36769
  width: msg.a.width,
39630
36770
  height: msg.a.height,
@@ -39640,10 +36780,15 @@ function pageResultToResult(msg) {
39640
36780
  height: msg.diff.height,
39641
36781
  data: new Uint8Array(msg.diff.data),
39642
36782
  }),
39643
- addition: msg.addition,
39644
- deletion: msg.deletion,
39645
- modification: msg.modification,
36783
+ addition: unpackCoords(msg.addition),
36784
+ deletion: unpackCoords(msg.deletion),
36785
+ modification: unpackCoords(msg.modification),
39646
36786
  };
36787
+ sP.stop();
36788
+ perf.incr("main.resultsReceived");
36789
+ if (msg.perf)
36790
+ perf.merge(msg.perf);
36791
+ return r;
39647
36792
  }
39648
36793
  async function* visualizeDifferences(a, b, options) {
39649
36794
  const merged = {
@@ -39658,49 +36803,111 @@ async function* visualizeDifferences(a, b, options) {
39658
36803
  },
39659
36804
  workers: options?.workers ?? defaultOptions.workers,
39660
36805
  };
39661
- const probe = PDFDocument.openDocument(a, "application/pdf");
39662
- const probeB = PDFDocument.openDocument(b, "application/pdf");
39663
- const probeMask = typeof merged.mask !== "undefined"
39664
- ? PDFDocument.openDocument(merged.mask, "application/pdf")
39665
- : new PDFDocument();
39666
- const maxPages = Math.max(probe.countPages(), probeB.countPages(), probeMask.countPages());
39667
- probe.destroy();
39668
- probeB.destroy();
39669
- probeMask.destroy();
36806
+ const [aPages, bPages, maskPages] = await Promise.all([
36807
+ countPages(a),
36808
+ countPages(b),
36809
+ typeof merged.mask !== "undefined"
36810
+ ? countPages(merged.mask)
36811
+ : Promise.resolve(0),
36812
+ ]);
36813
+ const maxPages = Math.max(aPages, bPages, maskPages);
39670
36814
  if (maxPages === 0)
39671
36815
  return;
39672
- const aBytes = asSharedBytes(a);
39673
- const bBytes = asSharedBytes(b);
39674
- const maskBytes = typeof merged.mask !== "undefined" ? asSharedBytes(merged.mask) : null;
36816
+ const mask = merged.mask;
36817
+ const hasMask = typeof mask !== "undefined" && maskPages > 0;
36818
+ const numDocs = hasMask ? 3 : 2;
39675
36819
  const initMsg = {
39676
36820
  type: "init",
39677
- aBytes,
39678
- bBytes,
39679
- maskBytes,
39680
- dpi: merged.dpi,
39681
- alpha: merged.alpha,
39682
36821
  pallet: merged.pallet,
39683
36822
  align: merged.align,
39684
36823
  };
39685
36824
  const N = Math.max(1, Math.min(merged.workers, maxPages));
39686
36825
  const url = workerUrl();
39687
- const worker0 = new WorkerHandle(url);
39688
- await worker0.init(initMsg);
39689
- const buffered = new Map();
39690
- let nextToAssign = 0;
39691
- const workers = [worker0];
39692
- for (let i = 1; i < N; i++) {
36826
+ const workers = [];
36827
+ for (let i = 0; i < N; i++) {
39693
36828
  const w = new WorkerHandle(url);
39694
36829
  await w.init(initMsg);
39695
36830
  workers.push(w);
39696
36831
  }
36832
+ let aborted = null;
36833
+ const makeSlots = (count) => Array.from({ length: maxPages }, (_, i) => {
36834
+ if (i >= count) {
36835
+ return { p: Promise.resolve(null), resolve: () => { }, reject: () => { } };
36836
+ }
36837
+ let resolve;
36838
+ let reject;
36839
+ const p = new Promise((res, rej) => {
36840
+ resolve = res;
36841
+ reject = rej;
36842
+ });
36843
+ p.catch(() => { });
36844
+ return { p, resolve, reject };
36845
+ });
36846
+ const slots = {
36847
+ a: makeSlots(aPages),
36848
+ b: makeSlots(bPages),
36849
+ mask: makeSlots(hasMask ? maskPages : 0),
36850
+ };
36851
+ // Render chunk tasks: batch several pages per gs() call to amortize startup,
36852
+ // interleaving A/B/mask by page range so the early pages of every document
36853
+ // become available together (which keeps the diff stage fed).
36854
+ // Render concurrency. Aim for ~2x as many chunks as render slots so pages
36855
+ // arrive in waves and the diff/decode stage overlaps later renders instead of
36856
+ // waiting for one big batch. A floor keeps each chunk large enough to amortize
36857
+ // Ghostscript's per-call startup: when there are many slots relative to pages,
36858
+ // the slots are already saturated, so batching beats finer streaming.
36859
+ const MIN_CHUNK = 4;
36860
+ const R = Math.max(merged.workers, numDocs);
36861
+ const totalRenderPages = aPages + bPages + (hasMask ? maskPages : 0);
36862
+ const chunkSize = Math.max(1, Math.min(maxPages, Math.max(MIN_CHUNK, Math.ceil(totalRenderPages / (2 * R)))));
36863
+ const tasks = [];
36864
+ const pushChunk = (bytes, count, target, start) => {
36865
+ if (bytes === undefined || start >= count)
36866
+ return;
36867
+ tasks.push({
36868
+ bytes,
36869
+ start,
36870
+ end: Math.min(start + chunkSize, count) - 1,
36871
+ slots: target,
36872
+ });
36873
+ };
36874
+ for (let start = 0; start < maxPages; start += chunkSize) {
36875
+ pushChunk(a, aPages, slots.a, start);
36876
+ pushChunk(b, bPages, slots.b, start);
36877
+ if (hasMask)
36878
+ pushChunk(mask, maskPages, slots.mask, start);
36879
+ }
36880
+ let taskIdx = 0;
36881
+ const renderLoops = Array.from({ length: Math.min(R, tasks.length) }, async () => {
36882
+ while (taskIdx < tasks.length && aborted === null) {
36883
+ const t = tasks[taskIdx++];
36884
+ try {
36885
+ const pngs = await renderPageRangePng(t.bytes, t.start, t.end, merged.dpi, merged.alpha);
36886
+ for (let i = t.start; i <= t.end; i++) {
36887
+ t.slots[i].resolve(pngs.get(i) ?? null);
36888
+ }
36889
+ }
36890
+ catch (e) {
36891
+ aborted = e;
36892
+ for (let i = t.start; i <= t.end; i++)
36893
+ t.slots[i].reject(e);
36894
+ }
36895
+ }
36896
+ });
36897
+ const buffered = new Map();
36898
+ let nextToAssign = 0;
39697
36899
  const resolvers = new Map();
39698
36900
  let workerError = null;
39699
- const loops = workers.map(async (w) => {
36901
+ const diffLoops = workers.map(async (w) => {
39700
36902
  while (nextToAssign < maxPages && workerError === null) {
39701
36903
  const idx = nextToAssign++;
39702
36904
  try {
39703
- const msg = await w.processPage(idx);
36905
+ const [aPng, bPng, maskPng] = await Promise.all([
36906
+ slots.a[idx].p,
36907
+ slots.b[idx].p,
36908
+ slots.mask[idx].p,
36909
+ ]);
36910
+ const msg = await w.processDiff(idx, aPng, bPng, maskPng);
39704
36911
  const result = pageResultToResult(msg);
39705
36912
  const resolve = resolvers.get(idx);
39706
36913
  if (resolve) {
@@ -39709,10 +36916,12 @@ async function* visualizeDifferences(a, b, options) {
39709
36916
  }
39710
36917
  else {
39711
36918
  buffered.set(idx, result);
36919
+ perf.setMax("main.bufferedPeak", buffered.size);
39712
36920
  }
39713
36921
  }
39714
36922
  catch (e) {
39715
36923
  workerError = e;
36924
+ aborted = e;
39716
36925
  for (const [, resolve] of resolvers)
39717
36926
  resolve(null);
39718
36927
  resolvers.clear();
@@ -39731,36 +36940,79 @@ async function* visualizeDifferences(a, b, options) {
39731
36940
  r = buf;
39732
36941
  }
39733
36942
  else {
36943
+ const sWait = perf.span("main.yieldWaitMain_ms");
39734
36944
  r = await new Promise((resolve) => resolvers.set(i, resolve));
36945
+ sWait.stop();
39735
36946
  if (workerError !== null)
39736
36947
  throw workerError;
39737
36948
  }
36949
+ const sYield = perf.span("main.consumerTime_ms");
39738
36950
  yield r;
36951
+ sYield.stop();
39739
36952
  }
39740
- await Promise.all(loops);
36953
+ await Promise.all(diffLoops);
36954
+ await Promise.all(renderLoops);
39741
36955
  }
39742
36956
  finally {
36957
+ aborted = aborted ?? new Error("aborted");
36958
+ await Promise.allSettled(renderLoops);
39743
36959
  for (const w of workers)
39744
36960
  w.terminate();
39745
36961
  }
39746
36962
  }
39747
36963
 
39748
- /*
39749
- * Copyright (C) 2025 Koutaro Mukai
39750
- *
39751
- * This program is free software: you can redistribute it and/or modify
39752
- * it under the terms of the GNU General Public License as published by
39753
- * the Free Software Foundation, either version 3 of the License, or
39754
- * (at your option) any later version.
39755
- *
39756
- * This program is distributed in the hope that it will be useful,
39757
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39758
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39759
- * GNU General Public License for more details.
39760
- *
39761
- * You should have received a copy of the GNU General Public License
39762
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39763
- */
36964
+ class PngWriterPool {
36965
+ workers = [];
36966
+ idle = [];
36967
+ waiting = [];
36968
+ constructor(size, scriptUrl) {
36969
+ for (let i = 0; i < size; i++) {
36970
+ const w = new Worker$1(scriptUrl);
36971
+ this.workers.push(w);
36972
+ this.idle.push(w);
36973
+ }
36974
+ }
36975
+ acquire() {
36976
+ const w = this.idle.pop();
36977
+ if (w)
36978
+ return Promise.resolve(w);
36979
+ return new Promise((resolve) => this.waiting.push(resolve));
36980
+ }
36981
+ release(w) {
36982
+ const next = this.waiting.shift();
36983
+ if (next)
36984
+ next(w);
36985
+ else
36986
+ this.idle.push(w);
36987
+ }
36988
+ async submit(job) {
36989
+ const w = await this.acquire();
36990
+ return new Promise((resolve, reject) => {
36991
+ const onMessage = (msg) => {
36992
+ w.off("message", onMessage);
36993
+ w.off("error", onError);
36994
+ this.release(w);
36995
+ if (msg.ok)
36996
+ resolve();
36997
+ else
36998
+ reject(new Error(msg.error));
36999
+ };
37000
+ const onError = (err) => {
37001
+ w.off("message", onMessage);
37002
+ w.off("error", onError);
37003
+ this.release(w);
37004
+ reject(err);
37005
+ };
37006
+ w.on("message", onMessage);
37007
+ w.once("error", onError);
37008
+ w.postMessage(job, [job.data]);
37009
+ });
37010
+ }
37011
+ async terminate() {
37012
+ await Promise.all(this.workers.map((w) => w.terminate()));
37013
+ }
37014
+ }
37015
+ const _wallSpan = perf.span("cli.wallTotal_ms");
39764
37016
  const { positionals, values: { dpi: dpi_, alpha: alpha_, mask: mask_, align: align_, "addition-color": additionColorHex, "deletion-color": deletionColorHex, "modification-color": modificationColorHex, workers: workers_, version, help, }, } = util$2.parseArgs({
39765
37017
  allowPositionals: true,
39766
37018
  options: {
@@ -39795,12 +37047,15 @@ OPTIONS:
39795
37047
  -h, --help
39796
37048
 
39797
37049
  NOTES:
39798
- Approximate per-worker memory:
39799
- a_size_MB + b_size_MB [+ mask_size_MB] (PDF buffers in wasm)
39800
- + 300 MB (mupdf + V8 base)
39801
- + (dpi / 150)^2 * 50 MB (pixmap working set)
39802
- The main process adds ~500 MB - 1 GB (varies with --workers).
39803
- Choose --workers so the total stays under ~80% of available memory.
37050
+ Pages are rendered with Ghostscript (gs-wasm). Each page render spins up a
37051
+ transient Ghostscript instance (~26 MB WASM binary plus a rasterization
37052
+ working set that grows with --dpi). A and B (and the mask) render
37053
+ concurrently, and --workers controls how many pages are rendered and diffed
37054
+ in parallel, so peak memory scales with both --workers and --dpi. Each
37055
+ in-flight page additionally holds decoded RGBA bitmaps of ~width*height*4
37056
+ bytes. --workers defaults to the CPU core count (capped at 4); lower it to
37057
+ reduce memory, or raise it for large jobs on big machines. Keep the total
37058
+ under ~80% of available memory.
39804
37059
  `);
39805
37060
  process.exit(0);
39806
37061
  }
@@ -39847,6 +37102,9 @@ if (Number.isNaN(workers) || workers < 1) {
39847
37102
  throw new Error("Invalid workers value");
39848
37103
  }
39849
37104
  fs.mkdirSync(outDir, { recursive: true });
37105
+ const writerPool = new PngWriterPool(workers, new URL("./cli-png-worker.js", import.meta.url));
37106
+ const pendingWrites = [];
37107
+ const _loopSpan = perf.span("cli.loopWall_ms");
39850
37108
  for await (const [i, { a, b, diff, addition, deletion, modification },] of withIndex(visualizeDifferences(pdfA, pdfB, {
39851
37109
  dpi,
39852
37110
  alpha,
@@ -39862,8 +37120,41 @@ for await (const [i, { a, b, diff, addition, deletion, modification },] of withI
39862
37120
  console.log(`Page ${i}, Addition: ${addition.length}, Deletion: ${deletion.length}, Modification: ${modification.length}`);
39863
37121
  const dir = path.join(outDir, i.toString(10));
39864
37122
  fs.mkdirSync(dir, { recursive: true });
39865
- fs.writeFileSync(path.join(dir, "a.png"), new Uint8Array(await a.getBuffer("image/png")));
39866
- fs.writeFileSync(path.join(dir, "b.png"), new Uint8Array(await b.getBuffer("image/png")));
39867
- fs.writeFileSync(path.join(dir, "diff.png"), new Uint8Array(await diff.getBuffer("image/png")));
37123
+ const sSubmit = perf.span("cli.poolSubmit_ms");
37124
+ const aBuf = sliceBackingBuffer(a.bitmap.data);
37125
+ const bBuf = sliceBackingBuffer(b.bitmap.data);
37126
+ const dBuf = sliceBackingBuffer(diff.bitmap.data);
37127
+ pendingWrites.push(writerPool.submit({
37128
+ width: a.width,
37129
+ height: a.height,
37130
+ data: aBuf,
37131
+ path: path.join(dir, "a.png"),
37132
+ }), writerPool.submit({
37133
+ width: b.width,
37134
+ height: b.height,
37135
+ data: bBuf,
37136
+ path: path.join(dir, "b.png"),
37137
+ }), writerPool.submit({
37138
+ width: diff.width,
37139
+ height: diff.height,
37140
+ data: dBuf,
37141
+ path: path.join(dir, "diff.png"),
37142
+ }));
37143
+ sSubmit.stop();
37144
+ }
37145
+ const sDrain = perf.span("cli.poolDrain_ms");
37146
+ await Promise.all(pendingWrites);
37147
+ sDrain.stop();
37148
+ await writerPool.terminate();
37149
+ _loopSpan.stop();
37150
+ _wallSpan.stop();
37151
+ if (perf.enabled) {
37152
+ const counters = perf.dump();
37153
+ process.stderr.write("\n=== PERF ===\n");
37154
+ const keys = Object.keys(counters).sort();
37155
+ const out = {};
37156
+ for (const k of keys)
37157
+ out[k] = Math.round(counters[k] * 1000) / 1000;
37158
+ process.stderr.write(JSON.stringify(out, null, 2) + "\n");
39868
37159
  }
39869
37160
  //# sourceMappingURL=cli.js.map