@u1f992/pdfdiff 0.2.2 → 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 (65) hide show
  1. package/.clang-format +3 -0
  2. package/LICENSE +68 -81
  3. package/dist/browser.js +239 -3109
  4. package/dist/browser.js.map +1 -1
  5. package/dist/cli-png-worker.d.ts.map +1 -1
  6. package/dist/cli-png-worker.js +0 -16
  7. package/dist/cli-png-worker.js.map +1 -1
  8. package/dist/cli.js +240 -3150
  9. package/dist/cli.js.map +1 -1
  10. package/dist/core.wasm +0 -0
  11. package/dist/decode.d.ts +9 -0
  12. package/dist/decode.d.ts.map +1 -0
  13. package/dist/diff.d.ts.map +1 -1
  14. package/dist/gs-wasm/gs.js +5821 -0
  15. package/dist/gs-wasm/gs.wasm +0 -0
  16. package/dist/gs-wasm/index.js +120 -0
  17. package/dist/gs-wasm/index.js.map +1 -0
  18. package/dist/gs-wasm/worker.js +764 -0
  19. package/dist/gs-wasm/worker.js.map +1 -0
  20. package/dist/image.d.ts.map +1 -1
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.html +1 -1
  24. package/dist/index.js +238 -3109
  25. package/dist/index.js.map +1 -1
  26. package/dist/iterable.d.ts.map +1 -1
  27. package/dist/jimp.d.ts +23 -1
  28. package/dist/jimp.d.ts.map +1 -1
  29. package/dist/pdf.d.ts +15 -4
  30. package/dist/pdf.d.ts.map +1 -1
  31. package/dist/perf.d.ts.map +1 -1
  32. package/dist/rgba-color.d.ts.map +1 -1
  33. package/dist/transferable.d.ts +6 -2
  34. package/dist/transferable.d.ts.map +1 -1
  35. package/dist/version.d.ts +1 -1
  36. package/dist/worker.d.ts +6 -8
  37. package/dist/worker.d.ts.map +1 -1
  38. package/dist/worker.js +70 -3311
  39. package/dist/worker.js.map +1 -1
  40. package/package.json +9 -4
  41. package/rollup.config.js +63 -5
  42. package/scripts/build-wasm.sh +32 -0
  43. package/src/browser.ts +9 -6
  44. package/src/cli-png-worker.ts +0 -17
  45. package/src/cli.ts +9 -23
  46. package/src/decode.ts +15 -0
  47. package/src/diff.ts +0 -17
  48. package/src/image.ts +1 -18
  49. package/src/index.html +1 -1
  50. package/src/index.test.ts +10 -18
  51. package/src/index.ts +163 -74
  52. package/src/iterable.test.ts +0 -17
  53. package/src/iterable.ts +0 -17
  54. package/src/jimp.ts +25 -7
  55. package/src/pdf.ts +98 -69
  56. package/src/perf.ts +0 -17
  57. package/src/rgba-color.test.ts +0 -17
  58. package/src/rgba-color.ts +0 -17
  59. package/src/transferable.ts +6 -21
  60. package/src/worker.ts +91 -87
  61. package/wasm/Makefile +34 -0
  62. package/wasm/bindings.cpp +76 -0
  63. package/wasm/core.c +176 -0
  64. package/wasm/core.h +69 -0
  65. package/dist/mupdf-wasm.wasm +0 -0
package/dist/cli.js CHANGED
@@ -10,6 +10,7 @@ import require$$1$1 from 'zlib';
10
10
  import require$$0$2 from 'assert';
11
11
  import require$$3 from 'buffer';
12
12
  import Worker from 'web-worker';
13
+ import { gs } from '@u1f992/gs-wasm';
13
14
 
14
15
  var HeaderTypes;
15
16
  (function (HeaderTypes) {
@@ -36430,3000 +36431,6 @@ const Jimp = createJimp({
36430
36431
  plugins: defaultPlugins,
36431
36432
  });
36432
36433
 
36433
- 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};
36434
-
36435
- // Copyright (C) 2004-2025 Artifex Software, Inc.
36436
- //
36437
- // This file is part of MuPDF WASM Library.
36438
- //
36439
- // MuPDF is free software: you can redistribute it and/or modify it under the
36440
- // terms of the GNU Affero General Public License as published by the Free
36441
- // Software Foundation, either version 3 of the License, or (at your option)
36442
- // any later version.
36443
- //
36444
- // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
36445
- // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
36446
- // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
36447
- // details.
36448
- //
36449
- // You should have received a copy of the GNU Affero General Public License
36450
- // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
36451
- //
36452
- // Alternative licensing terms are available from the licensor.
36453
- // For commercial licensing, see <https://www.artifex.com/> or contact
36454
- // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
36455
- // CA 94129, USA, for further information.
36456
- var node_fs = null;
36457
- if (typeof process !== "undefined" && process.versions && process.versions.node)
36458
- node_fs = await import('node:fs');
36459
- const libmupdf = await _(globalThis["$libmupdf_wasm_Module"]);
36460
- libmupdf._wasm_init_context();
36461
- function Malloc(size) {
36462
- return libmupdf._wasm_malloc(size);
36463
- }
36464
- function Free(ptr) {
36465
- libmupdf._wasm_free(ptr);
36466
- }
36467
- /* -------------------------------------------------------------------------- */
36468
- // To pass Rect and Matrix as pointer arguments
36469
- const _wasm_int = Malloc(4);
36470
- const _wasm_point = Malloc(4 * 6) >> 2;
36471
- const _wasm_rect = Malloc(4 * 8) >> 2;
36472
- const _wasm_matrix = Malloc(4 * 6) >> 2;
36473
- const _wasm_color = Malloc(4 * 4) >> 2;
36474
- const _wasm_quad = Malloc(4 * 8) >> 2;
36475
- const _wasm_string = [0, 0];
36476
- function checkType(value, type) {
36477
- if (typeof type === "string" && typeof value !== type)
36478
- throw new TypeError("expected " + type);
36479
- if (typeof type === "function" && !(value instanceof type))
36480
- throw new TypeError("expected " + type.name);
36481
- }
36482
- function checkPoint(value) {
36483
- if (!Array.isArray(value) || value.length !== 2)
36484
- throw new TypeError("expected point");
36485
- }
36486
- function checkRect(value) {
36487
- if (!Array.isArray(value) || value.length !== 4)
36488
- throw new TypeError("expected rectangle");
36489
- }
36490
- function checkMatrix(value) {
36491
- if (!Array.isArray(value) || value.length !== 6)
36492
- throw new TypeError("expected matrix");
36493
- }
36494
- function checkQuad(value) {
36495
- if (!Array.isArray(value) || value.length !== 8)
36496
- throw new TypeError("expected quad");
36497
- }
36498
- function checkColor(value) {
36499
- if (!Array.isArray(value) || (value.length !== 1 && value.length !== 3 && value.length !== 4))
36500
- throw new TypeError("expected color array");
36501
- }
36502
- function checkAnnotColor(value) {
36503
- if (!Array.isArray(value) || (value.length !== 0 && value.length !== 1 && value.length !== 3 && value.length !== 4))
36504
- throw new TypeError("expected color array");
36505
- }
36506
- function BUFFER(input) {
36507
- if (input instanceof Buffer$1)
36508
- return input.pointer;
36509
- if (input instanceof ArrayBuffer || input instanceof Uint8Array)
36510
- return new Buffer$1(input).pointer;
36511
- if (typeof input === "string")
36512
- return new Buffer$1(input).pointer;
36513
- throw new TypeError("expected buffer");
36514
- }
36515
- function ENUM(value, list) {
36516
- if (typeof value === "number") {
36517
- if (value >= 0 && value < list.length)
36518
- return value;
36519
- }
36520
- if (typeof value === "string") {
36521
- let idx = list.indexOf(value);
36522
- if (idx >= 0)
36523
- return idx;
36524
- }
36525
- throw new TypeError(`invalid enum value ("${value}"; expected ${list.join(", ")})`);
36526
- }
36527
- function allocateUTF8(str) {
36528
- var size = libmupdf.lengthBytesUTF8(str) + 1;
36529
- var pointer = Malloc(size);
36530
- libmupdf.stringToUTF8(str, pointer, size);
36531
- return pointer;
36532
- }
36533
- function STRING_N(s, i) {
36534
- if (_wasm_string[i]) {
36535
- Free(_wasm_string[i]);
36536
- _wasm_string[i] = 0;
36537
- }
36538
- return _wasm_string[i] = allocateUTF8(s);
36539
- }
36540
- function STRING(s) {
36541
- return STRING_N(s, 0);
36542
- }
36543
- function STRING2(s) {
36544
- return STRING_N(s, 1);
36545
- }
36546
- function STRING_OPT(s) {
36547
- return typeof s === "string" ? STRING_N(s, 0) : 0;
36548
- }
36549
- function STRING2_OPT(s) {
36550
- return typeof s === "string" ? STRING_N(s, 1) : 0;
36551
- }
36552
- function POINT(p) {
36553
- libmupdf.HEAPF32[_wasm_point + 0] = p[0];
36554
- libmupdf.HEAPF32[_wasm_point + 1] = p[1];
36555
- return _wasm_point << 2;
36556
- }
36557
- function POINT2(p) {
36558
- libmupdf.HEAPF32[_wasm_point + 2] = p[0];
36559
- libmupdf.HEAPF32[_wasm_point + 3] = p[1];
36560
- return (_wasm_point + 2) << 2;
36561
- }
36562
- function POINT3(p) {
36563
- libmupdf.HEAPF32[_wasm_point + 4] = p[0];
36564
- libmupdf.HEAPF32[_wasm_point + 5] = p[1];
36565
- return (_wasm_point + 4) << 2;
36566
- }
36567
- function RECT(r) {
36568
- libmupdf.HEAPF32[_wasm_rect + 0] = r[0];
36569
- libmupdf.HEAPF32[_wasm_rect + 1] = r[1];
36570
- libmupdf.HEAPF32[_wasm_rect + 2] = r[2];
36571
- libmupdf.HEAPF32[_wasm_rect + 3] = r[3];
36572
- return _wasm_rect << 2;
36573
- }
36574
- function RECT2(r) {
36575
- libmupdf.HEAPF32[_wasm_rect + 4] = r[0];
36576
- libmupdf.HEAPF32[_wasm_rect + 5] = r[1];
36577
- libmupdf.HEAPF32[_wasm_rect + 6] = r[2];
36578
- libmupdf.HEAPF32[_wasm_rect + 7] = r[3];
36579
- return (_wasm_rect + 4) << 2;
36580
- }
36581
- function MATRIX(m) {
36582
- libmupdf.HEAPF32[_wasm_matrix + 0] = m[0];
36583
- libmupdf.HEAPF32[_wasm_matrix + 1] = m[1];
36584
- libmupdf.HEAPF32[_wasm_matrix + 2] = m[2];
36585
- libmupdf.HEAPF32[_wasm_matrix + 3] = m[3];
36586
- libmupdf.HEAPF32[_wasm_matrix + 4] = m[4];
36587
- libmupdf.HEAPF32[_wasm_matrix + 5] = m[5];
36588
- return _wasm_matrix << 2;
36589
- }
36590
- function QUAD(q) {
36591
- libmupdf.HEAPF32[_wasm_quad + 0] = q[0];
36592
- libmupdf.HEAPF32[_wasm_quad + 1] = q[1];
36593
- libmupdf.HEAPF32[_wasm_quad + 2] = q[2];
36594
- libmupdf.HEAPF32[_wasm_quad + 3] = q[3];
36595
- libmupdf.HEAPF32[_wasm_quad + 4] = q[4];
36596
- libmupdf.HEAPF32[_wasm_quad + 5] = q[5];
36597
- libmupdf.HEAPF32[_wasm_quad + 6] = q[6];
36598
- libmupdf.HEAPF32[_wasm_quad + 7] = q[7];
36599
- return _wasm_quad << 2;
36600
- }
36601
- function COLOR(c) {
36602
- if (typeof c !== "undefined") {
36603
- switch (c.length) {
36604
- case 0:
36605
- break;
36606
- case 1:
36607
- libmupdf.HEAPF32[_wasm_color + 0] = c[0];
36608
- break;
36609
- case 3:
36610
- libmupdf.HEAPF32[_wasm_color + 0] = c[0];
36611
- libmupdf.HEAPF32[_wasm_color + 1] = c[1];
36612
- libmupdf.HEAPF32[_wasm_color + 2] = c[2];
36613
- break;
36614
- case 4:
36615
- libmupdf.HEAPF32[_wasm_color + 0] = c[0];
36616
- libmupdf.HEAPF32[_wasm_color + 1] = c[1];
36617
- libmupdf.HEAPF32[_wasm_color + 2] = c[2];
36618
- libmupdf.HEAPF32[_wasm_color + 3] = c[3];
36619
- break;
36620
- }
36621
- }
36622
- return _wasm_color << 2;
36623
- }
36624
- /* -------------------------------------------------------------------------- */
36625
- function fromColor(n) {
36626
- if (n === 1)
36627
- return [
36628
- libmupdf.HEAPF32[_wasm_color]
36629
- ];
36630
- if (n === 3)
36631
- return [
36632
- libmupdf.HEAPF32[_wasm_color + 0],
36633
- libmupdf.HEAPF32[_wasm_color + 1],
36634
- libmupdf.HEAPF32[_wasm_color + 2],
36635
- ];
36636
- if (n === 4)
36637
- return [
36638
- libmupdf.HEAPF32[_wasm_color + 0],
36639
- libmupdf.HEAPF32[_wasm_color + 1],
36640
- libmupdf.HEAPF32[_wasm_color + 2],
36641
- libmupdf.HEAPF32[_wasm_color + 3],
36642
- ];
36643
- throw new TypeError("invalid number of components for Color: " + n);
36644
- }
36645
- function fromAnnotColor(n) {
36646
- if (n === 0)
36647
- return [];
36648
- return fromColor(n);
36649
- }
36650
- function fromColorArray(n, ptr) {
36651
- let addr = ptr >> 2;
36652
- let color = [];
36653
- for (let i = 0; i < n; ++i)
36654
- color.push(libmupdf.HEAPF32[addr + i]);
36655
- return color;
36656
- }
36657
- function fromStringOrNull(ptr) {
36658
- if (ptr === 0)
36659
- return null;
36660
- return libmupdf.UTF8ToString(ptr);
36661
- }
36662
- function fromString(ptr) {
36663
- return libmupdf.UTF8ToString(ptr);
36664
- }
36665
- function fromStringFree(ptr) {
36666
- let str = libmupdf.UTF8ToString(ptr);
36667
- Free(ptr);
36668
- return str;
36669
- }
36670
- function fromPoint(ptr) {
36671
- let addr = ptr >> 2;
36672
- return [
36673
- libmupdf.HEAPF32[addr + 0],
36674
- libmupdf.HEAPF32[addr + 1],
36675
- ];
36676
- }
36677
- function fromRect(ptr) {
36678
- let addr = ptr >> 2;
36679
- return [
36680
- libmupdf.HEAPF32[addr + 0],
36681
- libmupdf.HEAPF32[addr + 1],
36682
- libmupdf.HEAPF32[addr + 2],
36683
- libmupdf.HEAPF32[addr + 3],
36684
- ];
36685
- }
36686
- function fromMatrix(ptr) {
36687
- let addr = ptr >> 2;
36688
- return [
36689
- libmupdf.HEAPF32[addr + 0],
36690
- libmupdf.HEAPF32[addr + 1],
36691
- libmupdf.HEAPF32[addr + 2],
36692
- libmupdf.HEAPF32[addr + 3],
36693
- libmupdf.HEAPF32[addr + 4],
36694
- libmupdf.HEAPF32[addr + 5],
36695
- ];
36696
- }
36697
- function fromQuad(ptr) {
36698
- let addr = ptr >> 2;
36699
- return [
36700
- libmupdf.HEAPF32[addr + 0],
36701
- libmupdf.HEAPF32[addr + 1],
36702
- libmupdf.HEAPF32[addr + 2],
36703
- libmupdf.HEAPF32[addr + 3],
36704
- libmupdf.HEAPF32[addr + 4],
36705
- libmupdf.HEAPF32[addr + 5],
36706
- libmupdf.HEAPF32[addr + 6],
36707
- libmupdf.HEAPF32[addr + 7],
36708
- ];
36709
- }
36710
- function fromBuffer(ptr) {
36711
- let data = libmupdf._wasm_buffer_get_data(ptr);
36712
- let size = libmupdf._wasm_buffer_get_len(ptr);
36713
- return libmupdf.HEAPU8.slice(data, data + size);
36714
- }
36715
- /* unused for now
36716
- function rgbFromColor(c?: Color): [number, number, number] {
36717
- var r = 0, g = 0, b = 0
36718
- if (typeof c !== "undefined") {
36719
- switch (c.length) {
36720
- case 1:
36721
- r = g = b = c[0]
36722
- break
36723
- case 3:
36724
- r = c[0]
36725
- g = c[1]
36726
- b = c[2]
36727
- break
36728
- case 4:
36729
- r = 1 - Math.min(1, c[0] + c[3])
36730
- g = 1 - Math.min(1, c[1] + c[3])
36731
- b = 1 - Math.min(1, c[2] + c[3])
36732
- break
36733
- }
36734
- }
36735
- return [ r, g, b ]
36736
- }
36737
-
36738
- function numberFromColor(c?: Color): number {
36739
- var [ r, g, b ] = rgbFromColor(c)
36740
- return (255 << 24) | (r << 16) | (g << 8) | b
36741
- }
36742
- */
36743
- function colorFromNumber(argb) {
36744
- var r = (argb >> 16) & 255;
36745
- var g = (argb >> 8) & 255;
36746
- var b = (argb) & 255;
36747
- return [r / 255, g / 255, b / 255];
36748
- }
36749
- function runSearch(searchFun, searchThis, needle, max_hits = 500) {
36750
- checkType(needle, "string");
36751
- let hits = 0;
36752
- let marks = 0;
36753
- try {
36754
- hits = Malloc(32 * max_hits);
36755
- marks = Malloc(4 * max_hits);
36756
- let n = searchFun(searchThis, STRING(needle), marks, hits, max_hits);
36757
- let outer = [];
36758
- if (n > 0) {
36759
- let inner = [];
36760
- for (let i = 0; i < n; ++i) {
36761
- let mark = libmupdf.HEAP32[(marks >> 2) + i];
36762
- let quad = fromQuad(hits + i * 32);
36763
- if (i > 0 && mark) {
36764
- outer.push(inner);
36765
- inner = [];
36766
- }
36767
- inner.push(quad);
36768
- }
36769
- outer.push(inner);
36770
- }
36771
- return outer;
36772
- }
36773
- finally {
36774
- Free(marks);
36775
- Free(hits);
36776
- }
36777
- }
36778
- /* -------------------------------------------------------------------------- */
36779
- class Userdata {
36780
- constructor(pointer) {
36781
- if (typeof pointer !== "number")
36782
- throw new Error("invalid pointer: " + typeof pointer);
36783
- if (pointer !== 0) {
36784
- let ctor = this.constructor;
36785
- if (!ctor._finalizer)
36786
- ctor._finalizer = new FinalizationRegistry(ctor._drop);
36787
- ctor._finalizer.register(this, pointer, this);
36788
- }
36789
- this.pointer = pointer;
36790
- }
36791
- destroy() {
36792
- if (this.pointer !== 0) {
36793
- let ctor = this.constructor;
36794
- ctor._finalizer.unregister(this);
36795
- ctor._drop(this.pointer);
36796
- }
36797
- this.pointer = 0;
36798
- }
36799
- // Custom "console.log" formatting for Node
36800
- [Symbol.for("nodejs.util.inspect.custom")]() {
36801
- return this.toString();
36802
- }
36803
- toString() {
36804
- return `[${this.constructor.name} ${this.pointer}]`;
36805
- }
36806
- valueOf() {
36807
- throw new Error("cannot convert Userdata to Javascript value");
36808
- }
36809
- }
36810
- let Buffer$1 = class Buffer extends Userdata {
36811
- constructor(arg) {
36812
- if (typeof arg === "undefined")
36813
- super(libmupdf._wasm_new_buffer(1024));
36814
- else if (typeof arg === "number")
36815
- super(arg);
36816
- else if (typeof arg === "string") {
36817
- let data_len = libmupdf.lengthBytesUTF8(arg);
36818
- let data_ptr = Malloc(data_len + 1);
36819
- libmupdf.stringToUTF8(arg, data_ptr, data_len + 1);
36820
- super(libmupdf._wasm_new_buffer_from_data(data_ptr, data_len));
36821
- }
36822
- else if (arg instanceof ArrayBuffer || arg instanceof Uint8Array) {
36823
- let data_len = arg.byteLength;
36824
- let data_ptr = Malloc(data_len);
36825
- libmupdf.HEAPU8.set(new Uint8Array(arg), data_ptr);
36826
- super(libmupdf._wasm_new_buffer_from_data(data_ptr, data_len));
36827
- }
36828
- }
36829
- get length() {
36830
- return this.getLength();
36831
- }
36832
- set length(_) {
36833
- throw new TypeError("buffer length is read-only");
36834
- }
36835
- getLength() {
36836
- return libmupdf._wasm_buffer_get_len(this.pointer);
36837
- }
36838
- readByte(at) {
36839
- let data = libmupdf._wasm_buffer_get_data(this.pointer);
36840
- return libmupdf.HEAPU8[data + at];
36841
- }
36842
- write(s) {
36843
- libmupdf._wasm_append_string(this.pointer, STRING(s));
36844
- }
36845
- writeByte(b) {
36846
- libmupdf._wasm_append_byte(this.pointer, b);
36847
- }
36848
- writeLine(s) {
36849
- this.write(s);
36850
- this.writeByte(10);
36851
- }
36852
- writeBuffer(other) {
36853
- libmupdf._wasm_append_buffer(this.pointer, BUFFER(other));
36854
- }
36855
- asUint8Array() {
36856
- let data = libmupdf._wasm_buffer_get_data(this.pointer);
36857
- let size = libmupdf._wasm_buffer_get_len(this.pointer);
36858
- return libmupdf.HEAPU8.subarray(data, data + size);
36859
- }
36860
- slice(start, end) {
36861
- if (typeof end === "undefined")
36862
- end = this.getLength();
36863
- return new Buffer(libmupdf._wasm_slice_buffer(this.pointer, start, end));
36864
- }
36865
- asString() {
36866
- return fromString(libmupdf._wasm_string_from_buffer(this.pointer));
36867
- }
36868
- save(filename) {
36869
- if (node_fs)
36870
- node_fs.writeFileSync(filename, this.asUint8Array());
36871
- else
36872
- throw new Error("missing 'fs' module");
36873
- }
36874
- };
36875
- Buffer$1._drop = libmupdf._wasm_drop_buffer;
36876
- class ColorSpace extends Userdata {
36877
- constructor(from, name) {
36878
- if (typeof from === "number") {
36879
- super(from);
36880
- }
36881
- else {
36882
- if (typeof from === "string") {
36883
- if (node_fs)
36884
- from = node_fs.readFileSync(from);
36885
- else
36886
- throw new Error("missing 'fs' module");
36887
- }
36888
- super(libmupdf._wasm_new_icc_colorspace(STRING_OPT(name), BUFFER(from)));
36889
- }
36890
- }
36891
- getName() {
36892
- return fromString(libmupdf._wasm_colorspace_get_name(this.pointer));
36893
- }
36894
- getType() {
36895
- return ColorSpace.COLORSPACE_TYPES[libmupdf._wasm_colorspace_get_type(this.pointer)] || "None";
36896
- }
36897
- getNumberOfComponents() {
36898
- return libmupdf._wasm_colorspace_get_n(this.pointer);
36899
- }
36900
- isGray() { return this.getType() === "Gray"; }
36901
- isRGB() { return this.getType() === "RGB"; }
36902
- isCMYK() { return this.getType() === "CMYK"; }
36903
- isIndexed() { return this.getType() === "Indexed"; }
36904
- isLab() { return this.getType() === "Lab"; }
36905
- isDeviceN() { return this.getType() === "Separation"; }
36906
- isSubtractive() { return this.getType() === "CMYK" || this.getType() === "Separation"; }
36907
- toString() {
36908
- return "[ColorSpace " + this.getName() + "]";
36909
- }
36910
- }
36911
- ColorSpace._drop = libmupdf._wasm_drop_colorspace;
36912
- ColorSpace.COLORSPACE_TYPES = [
36913
- "None",
36914
- "Gray",
36915
- "RGB",
36916
- "BGR",
36917
- "CMYK",
36918
- "Lab",
36919
- "Indexed",
36920
- "Separation"
36921
- ];
36922
- ColorSpace.DeviceGray = new ColorSpace(libmupdf._wasm_device_gray());
36923
- ColorSpace.DeviceRGB = new ColorSpace(libmupdf._wasm_device_rgb());
36924
- ColorSpace.DeviceBGR = new ColorSpace(libmupdf._wasm_device_bgr());
36925
- ColorSpace.DeviceCMYK = new ColorSpace(libmupdf._wasm_device_cmyk());
36926
- ColorSpace.Lab = new ColorSpace(libmupdf._wasm_device_lab());
36927
- class Font extends Userdata {
36928
- constructor(name_or_pointer, data, subfont = 0) {
36929
- let pointer = 0;
36930
- if (typeof name_or_pointer === "number") {
36931
- pointer = libmupdf._wasm_keep_font(name_or_pointer);
36932
- }
36933
- else {
36934
- if (typeof data === "string") {
36935
- if (node_fs)
36936
- data = node_fs.readFileSync(data);
36937
- else
36938
- throw new Error("missing 'fs' module");
36939
- }
36940
- if (data)
36941
- pointer = libmupdf._wasm_new_font_from_buffer(STRING(name_or_pointer), BUFFER(data), subfont);
36942
- else if (name_or_pointer === "zh-Hant")
36943
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_CNS);
36944
- else if (name_or_pointer === "zh-Hans")
36945
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_GB);
36946
- else if (name_or_pointer === "ja")
36947
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_JAPAN);
36948
- else if (name_or_pointer === "ko")
36949
- pointer = libmupdf._wasm_new_cjk_font(Font.ADOBE_KOREA);
36950
- else
36951
- pointer = libmupdf._wasm_new_base14_font(STRING(name_or_pointer));
36952
- }
36953
- super(pointer);
36954
- }
36955
- getName() {
36956
- return fromString(libmupdf._wasm_font_get_name(this.pointer));
36957
- }
36958
- encodeCharacter(uni) {
36959
- if (typeof uni === "string")
36960
- uni = uni.charCodeAt(0);
36961
- return libmupdf._wasm_encode_character(this.pointer, uni);
36962
- }
36963
- advanceGlyph(gid, wmode = 0) {
36964
- return libmupdf._wasm_advance_glyph(this.pointer, gid, wmode);
36965
- }
36966
- isMono() {
36967
- return !!libmupdf._wasm_font_is_monospaced(this.pointer);
36968
- }
36969
- isSerif() {
36970
- return !!libmupdf._wasm_font_is_serif(this.pointer);
36971
- }
36972
- isBold() {
36973
- return !!libmupdf._wasm_font_is_bold(this.pointer);
36974
- }
36975
- isItalic() {
36976
- return !!libmupdf._wasm_font_is_italic(this.pointer);
36977
- }
36978
- }
36979
- Font._drop = libmupdf._wasm_drop_font;
36980
- Font.SIMPLE_ENCODING = [
36981
- "Latin",
36982
- "Greek",
36983
- "Cyrillic"
36984
- ];
36985
- Font.ADOBE_CNS = 0;
36986
- Font.ADOBE_GB = 1;
36987
- Font.ADOBE_JAPAN = 2;
36988
- Font.ADOBE_KOREA = 3;
36989
- Font.CJK_ORDERING_BY_LANG = {
36990
- "Adobe-CNS1": 0,
36991
- "Adobe-GB1": 1,
36992
- "Adobe-Japan1": 2,
36993
- "Adobe-Korea1": 3,
36994
- "zh-Hant": 0,
36995
- "zh-TW": 0,
36996
- "zh-HK": 0,
36997
- "zh-Hans": 1,
36998
- "zh-CN": 1,
36999
- "ja": 2,
37000
- "ko": 3,
37001
- };
37002
- class Image extends Userdata {
37003
- constructor(data, mask) {
37004
- let pointer = 0;
37005
- if (typeof data === "number") {
37006
- pointer = libmupdf._wasm_keep_image(data);
37007
- }
37008
- else if (data instanceof Pixmap) {
37009
- pointer = libmupdf._wasm_new_image_from_pixmap(data.pointer, mask ? mask.pointer : 0);
37010
- }
37011
- else {
37012
- if (typeof data === "string") {
37013
- if (node_fs)
37014
- data = node_fs.readFileSync(data);
37015
- else
37016
- throw new Error("missing 'fs' module");
37017
- }
37018
- pointer = libmupdf._wasm_new_image_from_buffer(BUFFER(data));
37019
- }
37020
- super(pointer);
37021
- }
37022
- getWidth() {
37023
- return libmupdf._wasm_image_get_w(this.pointer);
37024
- }
37025
- getHeight() {
37026
- return libmupdf._wasm_image_get_h(this.pointer);
37027
- }
37028
- getNumberOfComponents() {
37029
- return libmupdf._wasm_image_get_n(this.pointer);
37030
- }
37031
- getBitsPerComponent() {
37032
- return libmupdf._wasm_image_get_bpc(this.pointer);
37033
- }
37034
- getXResolution() {
37035
- return libmupdf._wasm_image_get_xres(this.pointer);
37036
- }
37037
- getYResolution() {
37038
- return libmupdf._wasm_image_get_yres(this.pointer);
37039
- }
37040
- getImageMask() {
37041
- return !!libmupdf._wasm_image_get_imagemask(this.pointer);
37042
- }
37043
- getColorSpace() {
37044
- let cs = libmupdf._wasm_image_get_colorspace(this.pointer);
37045
- if (cs)
37046
- return new ColorSpace(libmupdf._wasm_keep_colorspace(cs));
37047
- return null;
37048
- }
37049
- getMask() {
37050
- let mask = libmupdf._wasm_image_get_mask(this.pointer);
37051
- if (mask)
37052
- return new Image(libmupdf._wasm_keep_image(mask));
37053
- return null;
37054
- }
37055
- toPixmap() {
37056
- return new Pixmap(libmupdf._wasm_get_pixmap_from_image(this.pointer));
37057
- }
37058
- }
37059
- Image._drop = libmupdf._wasm_drop_image;
37060
- class StrokeState extends Userdata {
37061
- constructor(data) {
37062
- if (typeof data === "number") {
37063
- super(data);
37064
- return this;
37065
- }
37066
- super(libmupdf._wasm_new_stroke_state(data?.dashes?.length ?? 0));
37067
- let lineCap = ENUM(data.lineCap, StrokeState.LINE_CAP);
37068
- let lineJoin = ENUM(data.lineJoin, StrokeState.LINE_JOIN);
37069
- libmupdf._wasm_stroke_state_set_start_cap(this.pointer, lineCap);
37070
- libmupdf._wasm_stroke_state_set_dash_cap(this.pointer, lineCap);
37071
- libmupdf._wasm_stroke_state_set_end_cap(this.pointer, lineCap);
37072
- libmupdf._wasm_stroke_state_set_linejoin(this.pointer, lineJoin);
37073
- libmupdf._wasm_stroke_state_set_linewidth(this.pointer, data.lineWidth);
37074
- libmupdf._wasm_stroke_state_set_miterlimit(this.pointer, data.miterLimit);
37075
- libmupdf._wasm_stroke_state_set_dash_phase(this.pointer, data.dashPhase ?? 0);
37076
- if (data.dashes) {
37077
- for (let i = 0; i < data.dashes.length; ++i)
37078
- libmupdf._wasm_stroke_state_set_dash_item(this.pointer, i, data.dashes[i] ?? 0);
37079
- }
37080
- }
37081
- getLineCap() {
37082
- return libmupdf._wasm_stroke_state_get_start_cap(this.pointer);
37083
- }
37084
- getLineJoin() {
37085
- return libmupdf._wasm_stroke_state_get_linejoin(this.pointer);
37086
- }
37087
- getLineWidth() {
37088
- return libmupdf._wasm_stroke_state_get_linewidth(this.pointer);
37089
- }
37090
- getMiterLimit() {
37091
- return libmupdf._wasm_stroke_state_get_miterlimit(this.pointer);
37092
- }
37093
- getDashPhase() {
37094
- return libmupdf._wasm_stroke_state_get_dash_phase(this.pointer);
37095
- }
37096
- getDashes() {
37097
- var n = libmupdf._wasm_stroke_state_get_dash_len(this.pointer);
37098
- if (n > 0) {
37099
- var out = [];
37100
- for (let i = 0; i < n; ++i)
37101
- out[i] = libmupdf._wasm_stroke_state_get_dash_item(this.pointer, i);
37102
- return out;
37103
- }
37104
- return null;
37105
- }
37106
- }
37107
- StrokeState._drop = libmupdf._wasm_drop_stroke_state;
37108
- StrokeState.LINE_CAP = [
37109
- "Butt",
37110
- "Round",
37111
- "Square",
37112
- "Triangle"
37113
- ];
37114
- StrokeState.LINE_JOIN = [
37115
- "Miter",
37116
- "Round",
37117
- "Bevel",
37118
- "MiterXPS"
37119
- ];
37120
- class Path extends Userdata {
37121
- constructor(pointer) {
37122
- if (typeof pointer === "number")
37123
- super(pointer);
37124
- else
37125
- super(libmupdf._wasm_new_path());
37126
- }
37127
- getBounds(strokeState, transform) {
37128
- if (strokeState !== null)
37129
- checkType(strokeState, StrokeState);
37130
- checkMatrix(transform);
37131
- return fromRect(libmupdf._wasm_bound_path(this.pointer, strokeState?.pointer, MATRIX(transform)));
37132
- }
37133
- moveTo(x, y) {
37134
- checkType(x, "number");
37135
- checkType(y, "number");
37136
- libmupdf._wasm_moveto(this.pointer, x, y);
37137
- }
37138
- lineTo(x, y) {
37139
- checkType(x, "number");
37140
- checkType(y, "number");
37141
- libmupdf._wasm_lineto(this.pointer, x, y);
37142
- }
37143
- curveTo(x1, y1, x2, y2, x3, y3) {
37144
- checkType(x1, "number");
37145
- checkType(y1, "number");
37146
- checkType(x2, "number");
37147
- checkType(y2, "number");
37148
- checkType(x3, "number");
37149
- checkType(y3, "number");
37150
- libmupdf._wasm_curveto(this.pointer, x1, y1, x2, y2, x3, y3);
37151
- }
37152
- curveToV(cx, cy, ex, ey) {
37153
- checkType(cx, "number");
37154
- checkType(cy, "number");
37155
- checkType(ex, "number");
37156
- checkType(ey, "number");
37157
- libmupdf._wasm_curvetov(this.pointer, cx, cy, ex, ey);
37158
- }
37159
- curveToY(cx, cy, ex, ey) {
37160
- checkType(cx, "number");
37161
- checkType(cy, "number");
37162
- checkType(ex, "number");
37163
- checkType(ey, "number");
37164
- libmupdf._wasm_curvetoy(this.pointer, cx, cy, ex, ey);
37165
- }
37166
- closePath() {
37167
- libmupdf._wasm_closepath(this.pointer);
37168
- }
37169
- rect(x1, y1, x2, y2) {
37170
- checkType(x1, "number");
37171
- checkType(y1, "number");
37172
- checkType(x2, "number");
37173
- checkType(y2, "number");
37174
- libmupdf._wasm_rectto(this.pointer, x1, y1, x2, y2);
37175
- }
37176
- transform(matrix) {
37177
- checkMatrix(matrix);
37178
- libmupdf._wasm_transform_path(this.pointer, MATRIX(matrix));
37179
- }
37180
- walk(walker) {
37181
- let id = $libmupdf_path_id++;
37182
- $libmupdf_path_table.set(id, walker);
37183
- libmupdf._wasm_walk_path(this.pointer, id);
37184
- $libmupdf_path_table.delete(id);
37185
- }
37186
- }
37187
- Path._drop = libmupdf._wasm_drop_path;
37188
- class Text extends Userdata {
37189
- constructor(pointer) {
37190
- if (typeof pointer === "number")
37191
- super(pointer);
37192
- else
37193
- super(libmupdf._wasm_new_text());
37194
- }
37195
- getBounds(strokeState, transform) {
37196
- if (strokeState !== null)
37197
- checkType(strokeState, StrokeState);
37198
- checkMatrix(transform);
37199
- return fromRect(libmupdf._wasm_bound_text(this.pointer, strokeState?.pointer, MATRIX(transform)));
37200
- }
37201
- showGlyph(font, trm, gid, uni, wmode = 0) {
37202
- checkType(font, Font);
37203
- checkMatrix(trm);
37204
- checkType(gid, "number");
37205
- checkType(uni, "number");
37206
- libmupdf._wasm_show_glyph(this.pointer, font.pointer, MATRIX(trm), gid, uni, wmode);
37207
- }
37208
- showString(font, trm, str, wmode = 0) {
37209
- checkType(font, Font);
37210
- checkMatrix(trm);
37211
- checkType(str, "string");
37212
- return fromMatrix(libmupdf._wasm_show_string(this.pointer, font.pointer, MATRIX(trm), STRING(str), wmode));
37213
- }
37214
- walk(walker) {
37215
- let id = $libmupdf_text_id++;
37216
- $libmupdf_text_table.set(id, walker);
37217
- libmupdf._wasm_walk_text(this.pointer, id);
37218
- $libmupdf_text_table.delete(id);
37219
- }
37220
- }
37221
- Text._drop = libmupdf._wasm_drop_text;
37222
- class DisplayList extends Userdata {
37223
- constructor(arg1) {
37224
- let pointer = 0;
37225
- if (typeof arg1 === "number") {
37226
- pointer = arg1;
37227
- }
37228
- else {
37229
- checkRect(arg1);
37230
- pointer = libmupdf._wasm_new_display_list(RECT(arg1));
37231
- }
37232
- super(pointer);
37233
- }
37234
- getBounds() {
37235
- return fromRect(libmupdf._wasm_bound_display_list(this.pointer));
37236
- }
37237
- toPixmap(matrix, colorspace, alpha = false) {
37238
- checkMatrix(matrix);
37239
- checkType(colorspace, ColorSpace);
37240
- return new Pixmap(libmupdf._wasm_new_pixmap_from_display_list(this.pointer, MATRIX(matrix), colorspace.pointer, alpha));
37241
- }
37242
- toStructuredText(options = "") {
37243
- checkType(options, "string");
37244
- return new StructuredText(libmupdf._wasm_new_stext_page_from_display_list(this.pointer, STRING(options)));
37245
- }
37246
- run(device, matrix) {
37247
- checkType(device, Device);
37248
- checkMatrix(matrix);
37249
- libmupdf._wasm_run_display_list(this.pointer, device.pointer, MATRIX(matrix));
37250
- }
37251
- search(needle, max_hits = 500) {
37252
- return runSearch(libmupdf._wasm_search_display_list, this.pointer, needle, max_hits);
37253
- }
37254
- }
37255
- DisplayList._drop = libmupdf._wasm_drop_display_list;
37256
- class Pixmap extends Userdata {
37257
- constructor(arg1, bbox, alpha = false) {
37258
- if (typeof arg1 === "number") {
37259
- super(arg1);
37260
- }
37261
- if (arg1 instanceof ColorSpace) {
37262
- checkRect(bbox);
37263
- super(libmupdf._wasm_new_pixmap_with_bbox(arg1.pointer, RECT(bbox), alpha));
37264
- }
37265
- if (arg1 === null) {
37266
- checkRect(bbox);
37267
- super(libmupdf._wasm_new_pixmap_with_bbox(0, RECT(bbox), alpha));
37268
- }
37269
- }
37270
- getBounds() {
37271
- let x = libmupdf._wasm_pixmap_get_x(this.pointer);
37272
- let y = libmupdf._wasm_pixmap_get_y(this.pointer);
37273
- let w = libmupdf._wasm_pixmap_get_w(this.pointer);
37274
- let h = libmupdf._wasm_pixmap_get_h(this.pointer);
37275
- return [x, y, x + w, y + h];
37276
- }
37277
- clear(value) {
37278
- if (typeof value === "undefined")
37279
- libmupdf._wasm_clear_pixmap(this.pointer);
37280
- else
37281
- libmupdf._wasm_clear_pixmap_with_value(this.pointer, value);
37282
- }
37283
- getWidth() {
37284
- return libmupdf._wasm_pixmap_get_w(this.pointer);
37285
- }
37286
- getHeight() {
37287
- return libmupdf._wasm_pixmap_get_h(this.pointer);
37288
- }
37289
- getX() {
37290
- return libmupdf._wasm_pixmap_get_x(this.pointer);
37291
- }
37292
- getY() {
37293
- return libmupdf._wasm_pixmap_get_y(this.pointer);
37294
- }
37295
- getStride() {
37296
- return libmupdf._wasm_pixmap_get_stride(this.pointer);
37297
- }
37298
- getNumberOfComponents() {
37299
- return libmupdf._wasm_pixmap_get_n(this.pointer);
37300
- }
37301
- getAlpha() {
37302
- return libmupdf._wasm_pixmap_get_alpha(this.pointer);
37303
- }
37304
- getXResolution() {
37305
- return libmupdf._wasm_pixmap_get_xres(this.pointer);
37306
- }
37307
- getYResolution() {
37308
- return libmupdf._wasm_pixmap_get_yres(this.pointer);
37309
- }
37310
- setResolution(x, y) {
37311
- libmupdf._wasm_pixmap_set_xres(this.pointer, x);
37312
- libmupdf._wasm_pixmap_set_yres(this.pointer, y);
37313
- }
37314
- getColorSpace() {
37315
- let cs = libmupdf._wasm_pixmap_get_colorspace(this.pointer);
37316
- if (cs)
37317
- return new ColorSpace(libmupdf._wasm_keep_colorspace(cs));
37318
- return null;
37319
- }
37320
- getPixels() {
37321
- let s = libmupdf._wasm_pixmap_get_stride(this.pointer);
37322
- let h = libmupdf._wasm_pixmap_get_h(this.pointer);
37323
- let p = libmupdf._wasm_pixmap_get_samples(this.pointer);
37324
- return new Uint8ClampedArray(libmupdf.HEAPU8.buffer, p, s * h);
37325
- }
37326
- asPNG() {
37327
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_png(this.pointer);
37328
- try {
37329
- return fromBuffer(buf);
37330
- }
37331
- finally {
37332
- libmupdf._wasm_drop_buffer(buf);
37333
- }
37334
- }
37335
- asPSD() {
37336
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_psd(this.pointer);
37337
- try {
37338
- return fromBuffer(buf);
37339
- }
37340
- finally {
37341
- libmupdf._wasm_drop_buffer(buf);
37342
- }
37343
- }
37344
- asPAM() {
37345
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_pam(this.pointer);
37346
- try {
37347
- return fromBuffer(buf);
37348
- }
37349
- finally {
37350
- libmupdf._wasm_drop_buffer(buf);
37351
- }
37352
- }
37353
- asJPEG(quality, invert_cmyk = false) {
37354
- let buf = libmupdf._wasm_new_buffer_from_pixmap_as_jpeg(this.pointer, quality, invert_cmyk);
37355
- try {
37356
- return fromBuffer(buf);
37357
- }
37358
- finally {
37359
- libmupdf._wasm_drop_buffer(buf);
37360
- }
37361
- }
37362
- invert() {
37363
- libmupdf._wasm_invert_pixmap(this.pointer);
37364
- }
37365
- invertLuminance() {
37366
- libmupdf._wasm_invert_pixmap_luminance(this.pointer);
37367
- }
37368
- gamma(p) {
37369
- libmupdf._wasm_gamma_pixmap(this.pointer, p);
37370
- }
37371
- tint(black, white) {
37372
- let black_hex = 0x000000;
37373
- let white_hex = 0xffffff;
37374
- if (typeof black === "number")
37375
- black_hex = black;
37376
- else if (black instanceof Array && black.length === 3)
37377
- black_hex = (((black[0] * 255) << 16) | ((black[1] * 255) << 8) | ((black[2] * 255)));
37378
- if (typeof white === "number")
37379
- white_hex = white;
37380
- else if (white instanceof Array && white.length === 3)
37381
- white = (((white[0] * 255) << 16) | ((white[1] * 255) << 8) | ((white[2] * 255)));
37382
- libmupdf._wasm_tint_pixmap(this.pointer, black_hex, white_hex);
37383
- }
37384
- convertToColorSpace(colorspace, keepAlpha = false) {
37385
- checkType(colorspace, ColorSpace);
37386
- checkType(keepAlpha, "boolean");
37387
- return new Pixmap(libmupdf._wasm_convert_pixmap(this.pointer, colorspace.pointer, keepAlpha));
37388
- }
37389
- warp(points, width, height) {
37390
- let quad = points.flat();
37391
- checkQuad(quad);
37392
- checkType(width, "number");
37393
- checkType(height, "number");
37394
- return new Pixmap(libmupdf._wasm_warp_pixmap(this.pointer, QUAD(quad), width, height));
37395
- }
37396
- }
37397
- Pixmap._drop = libmupdf._wasm_drop_pixmap;
37398
- class Shade extends Userdata {
37399
- getBounds() {
37400
- return fromRect(libmupdf._wasm_bound_shade(this.pointer));
37401
- }
37402
- }
37403
- Shade._drop = libmupdf._wasm_drop_shade;
37404
- class StructuredText extends Userdata {
37405
- walk(walker) {
37406
- let block = libmupdf._wasm_stext_page_get_first_block(this.pointer);
37407
- while (block) {
37408
- let block_type = libmupdf._wasm_stext_block_get_type(block);
37409
- let block_bbox = fromRect(libmupdf._wasm_stext_block_get_bbox(block));
37410
- if (block_type === 1) {
37411
- if (walker.onImageBlock) {
37412
- let matrix = fromMatrix(libmupdf._wasm_stext_block_get_transform(block));
37413
- let image = new Image(libmupdf._wasm_stext_block_get_image(block));
37414
- walker.onImageBlock(block_bbox, matrix, image);
37415
- }
37416
- }
37417
- else {
37418
- if (walker.beginTextBlock)
37419
- walker.beginTextBlock(block_bbox);
37420
- let line = libmupdf._wasm_stext_block_get_first_line(block);
37421
- while (line) {
37422
- let line_bbox = fromRect(libmupdf._wasm_stext_line_get_bbox(line));
37423
- let line_wmode = libmupdf._wasm_stext_line_get_wmode(line);
37424
- let line_dir = fromPoint(libmupdf._wasm_stext_line_get_dir(line));
37425
- if (walker.beginLine)
37426
- walker.beginLine(line_bbox, line_wmode, line_dir);
37427
- if (walker.onChar) {
37428
- let ch = libmupdf._wasm_stext_line_get_first_char(line);
37429
- while (ch) {
37430
- let ch_rune = String.fromCharCode(libmupdf._wasm_stext_char_get_c(ch));
37431
- let ch_origin = fromPoint(libmupdf._wasm_stext_char_get_origin(ch));
37432
- let ch_font = new Font(libmupdf._wasm_stext_char_get_font(ch));
37433
- let ch_size = libmupdf._wasm_stext_char_get_size(ch);
37434
- let ch_quad = fromQuad(libmupdf._wasm_stext_char_get_quad(ch));
37435
- let ch_color = colorFromNumber(libmupdf._wasm_stext_char_get_argb(ch));
37436
- walker.onChar(ch_rune, ch_origin, ch_font, ch_size, ch_quad, ch_color);
37437
- ch = libmupdf._wasm_stext_char_get_next(ch);
37438
- }
37439
- }
37440
- if (walker.endLine)
37441
- walker.endLine();
37442
- line = libmupdf._wasm_stext_line_get_next(line);
37443
- }
37444
- if (walker.endTextBlock)
37445
- walker.endTextBlock();
37446
- }
37447
- block = libmupdf._wasm_stext_block_get_next(block);
37448
- }
37449
- }
37450
- asJSON(scale = 1) {
37451
- return fromStringFree(libmupdf._wasm_print_stext_page_as_json(this.pointer, scale));
37452
- }
37453
- asHTML(id) {
37454
- return fromStringFree(libmupdf._wasm_print_stext_page_as_html(this.pointer, id));
37455
- }
37456
- asText() {
37457
- return fromStringFree(libmupdf._wasm_print_stext_page_as_text(this.pointer));
37458
- }
37459
- snap(p, q, mode) {
37460
- let mm = ENUM(mode, StructuredText.SELECT_MODE);
37461
- return fromQuad(libmupdf._wasm_snap_selection(this.pointer, POINT(p), POINT2(q), mm));
37462
- }
37463
- copy(p, q) {
37464
- return fromStringFree(libmupdf._wasm_copy_selection(this.pointer, POINT(p), POINT2(q)));
37465
- }
37466
- highlight(p, q, max_hits = 100) {
37467
- let hits = 0;
37468
- let result = [];
37469
- try {
37470
- hits = Malloc(32 * max_hits);
37471
- let n = libmupdf._wasm_highlight_selection(this.pointer, POINT(p), POINT2(q), hits, max_hits);
37472
- for (let i = 0; i < n; ++i)
37473
- result.push(fromQuad(hits + i * 32));
37474
- }
37475
- finally {
37476
- Free(hits);
37477
- }
37478
- return result;
37479
- }
37480
- search(needle, max_hits = 500) {
37481
- return runSearch(libmupdf._wasm_search_stext_page, this.pointer, needle, max_hits);
37482
- }
37483
- }
37484
- StructuredText._drop = libmupdf._wasm_drop_stext_page;
37485
- StructuredText.SELECT_MODE = [
37486
- "chars",
37487
- "words",
37488
- "lines"
37489
- ];
37490
- StructuredText.SELECT_CHARS = "chars";
37491
- StructuredText.SELECT_WORDS = "words";
37492
- StructuredText.SELECT_LINES = "lines";
37493
- class Device extends Userdata {
37494
- constructor(pointer_or_callbacks) {
37495
- if (typeof pointer_or_callbacks === "number")
37496
- super(pointer_or_callbacks);
37497
- else {
37498
- let id = $libmupdf_device_id++;
37499
- $libmupdf_device_table.set(id, pointer_or_callbacks);
37500
- super(libmupdf._wasm_new_js_device(id));
37501
- }
37502
- }
37503
- fillPath(path, evenOdd, ctm, colorspace, color, alpha) {
37504
- checkType(path, Path);
37505
- checkMatrix(ctm);
37506
- checkType(colorspace, ColorSpace);
37507
- checkColor(color);
37508
- libmupdf._wasm_fill_path(this.pointer, path.pointer, evenOdd, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37509
- }
37510
- strokePath(path, stroke, ctm, colorspace, color, alpha) {
37511
- checkType(path, Path);
37512
- checkType(stroke, StrokeState);
37513
- checkMatrix(ctm);
37514
- checkType(colorspace, ColorSpace);
37515
- checkColor(color);
37516
- libmupdf._wasm_stroke_path(this.pointer, path.pointer, stroke.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37517
- }
37518
- clipPath(path, evenOdd, ctm) {
37519
- checkType(path, Path);
37520
- checkMatrix(ctm);
37521
- libmupdf._wasm_clip_path(this.pointer, path.pointer, evenOdd, MATRIX(ctm));
37522
- }
37523
- clipStrokePath(path, stroke, ctm) {
37524
- checkType(path, Path);
37525
- checkType(stroke, StrokeState);
37526
- checkMatrix(ctm);
37527
- libmupdf._wasm_clip_stroke_path(this.pointer, path.pointer, stroke.pointer, MATRIX(ctm));
37528
- }
37529
- fillText(text, ctm, colorspace, color, alpha) {
37530
- checkType(text, Text);
37531
- checkMatrix(ctm);
37532
- checkType(colorspace, ColorSpace);
37533
- checkColor(color);
37534
- libmupdf._wasm_fill_text(this.pointer, text.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37535
- }
37536
- strokeText(text, stroke, ctm, colorspace, color, alpha) {
37537
- checkType(text, Text);
37538
- checkType(stroke, StrokeState);
37539
- checkMatrix(ctm);
37540
- checkType(colorspace, ColorSpace);
37541
- checkColor(color);
37542
- libmupdf._wasm_stroke_text(this.pointer, text.pointer, stroke.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37543
- }
37544
- clipText(text, ctm) {
37545
- checkType(text, Text);
37546
- checkMatrix(ctm);
37547
- libmupdf._wasm_clip_text(this.pointer, text.pointer, MATRIX(ctm));
37548
- }
37549
- clipStrokeText(text, stroke, ctm) {
37550
- checkType(text, Text);
37551
- checkType(stroke, StrokeState);
37552
- checkMatrix(ctm);
37553
- libmupdf._wasm_clip_stroke_text(this.pointer, text.pointer, stroke.pointer, MATRIX(ctm));
37554
- }
37555
- ignoreText(text, ctm) {
37556
- checkType(text, Text);
37557
- checkMatrix(ctm);
37558
- libmupdf._wasm_ignore_text(this.pointer, text.pointer, MATRIX(ctm));
37559
- }
37560
- fillShade(shade, ctm, alpha) {
37561
- checkType(shade, Shade);
37562
- checkMatrix(ctm);
37563
- libmupdf._wasm_fill_shade(this.pointer, shade.pointer, MATRIX(ctm), alpha);
37564
- }
37565
- fillImage(image, ctm, alpha) {
37566
- checkType(image, Image);
37567
- checkMatrix(ctm);
37568
- libmupdf._wasm_fill_image(this.pointer, image.pointer, MATRIX(ctm), alpha);
37569
- }
37570
- fillImageMask(image, ctm, colorspace, color, alpha) {
37571
- checkType(image, Image);
37572
- checkMatrix(ctm);
37573
- checkType(colorspace, ColorSpace);
37574
- checkColor(color);
37575
- libmupdf._wasm_fill_image_mask(this.pointer, image.pointer, MATRIX(ctm), colorspace.pointer, COLOR(color), alpha);
37576
- }
37577
- clipImageMask(image, ctm) {
37578
- checkType(image, Image);
37579
- checkMatrix(ctm);
37580
- libmupdf._wasm_clip_image_mask(this.pointer, image.pointer, MATRIX(ctm));
37581
- }
37582
- popClip() {
37583
- libmupdf._wasm_pop_clip(this.pointer);
37584
- }
37585
- beginMask(area, luminosity, colorspace, color) {
37586
- checkRect(area);
37587
- checkType(colorspace, ColorSpace);
37588
- checkColor(color);
37589
- libmupdf._wasm_begin_mask(this.pointer, RECT(area), luminosity, colorspace.pointer, COLOR(color));
37590
- }
37591
- endMask() {
37592
- libmupdf._wasm_end_mask(this.pointer);
37593
- }
37594
- beginGroup(area, colorspace, isolated, knockout, blendmode, alpha) {
37595
- checkRect(area);
37596
- checkType(colorspace, ColorSpace);
37597
- let blendmode_ix = ENUM(blendmode, Device.BLEND_MODES);
37598
- libmupdf._wasm_begin_group(this.pointer, RECT(area), colorspace.pointer, isolated, knockout, blendmode_ix, alpha);
37599
- }
37600
- endGroup() {
37601
- libmupdf._wasm_end_group(this.pointer);
37602
- }
37603
- beginTile(area, view, xstep, ystep, ctm, id) {
37604
- checkRect(area);
37605
- checkRect(view);
37606
- checkMatrix(ctm);
37607
- return libmupdf._wasm_begin_tile(this.pointer, RECT(area), RECT2(view), xstep, ystep, MATRIX(ctm), id);
37608
- }
37609
- endTile() {
37610
- libmupdf._wasm_end_tile(this.pointer);
37611
- }
37612
- beginLayer(name) {
37613
- libmupdf._wasm_begin_layer(this.pointer, STRING(name));
37614
- }
37615
- endLayer() {
37616
- libmupdf._wasm_end_layer(this.pointer);
37617
- }
37618
- close() {
37619
- libmupdf._wasm_close_device(this.pointer);
37620
- }
37621
- }
37622
- Device._drop = libmupdf._wasm_drop_device;
37623
- Device.BLEND_MODES = [
37624
- "Normal",
37625
- "Multiply",
37626
- "Screen",
37627
- "Overlay",
37628
- "Darken",
37629
- "Lighten",
37630
- "ColorDodge",
37631
- "ColorBurn",
37632
- "HardLight",
37633
- "SoftLight",
37634
- "Difference",
37635
- "Exclusion",
37636
- "Hue",
37637
- "Saturation",
37638
- "Color",
37639
- "Luminosity",
37640
- ];
37641
- class DocumentWriter extends Userdata {
37642
- constructor(buffer, format, options) {
37643
- super(libmupdf._wasm_new_document_writer_with_buffer(BUFFER(buffer), STRING(format), STRING2(options)));
37644
- }
37645
- beginPage(mediabox) {
37646
- checkRect(mediabox);
37647
- return new Device(libmupdf._wasm_begin_page(this.pointer, RECT(mediabox)));
37648
- }
37649
- endPage() {
37650
- libmupdf._wasm_end_page(this.pointer);
37651
- }
37652
- close() {
37653
- libmupdf._wasm_close_document_writer(this.pointer);
37654
- }
37655
- }
37656
- DocumentWriter._drop = libmupdf._wasm_drop_document_writer;
37657
- class Document extends Userdata {
37658
- static openDocument(from, magic) {
37659
- let pointer = 0;
37660
- if (typeof from === "string") {
37661
- magic = from;
37662
- if (node_fs)
37663
- from = node_fs.readFileSync(from);
37664
- else
37665
- throw new Error("missing 'fs' module");
37666
- }
37667
- else {
37668
- if (typeof magic === "undefined")
37669
- magic = "application/pdf";
37670
- }
37671
- checkType(magic, "string");
37672
- if (from instanceof ArrayBuffer || from instanceof Uint8Array)
37673
- from = new Buffer$1(from);
37674
- if (from instanceof Buffer$1)
37675
- pointer = libmupdf._wasm_open_document_with_buffer(STRING(magic), from.pointer);
37676
- else if (from instanceof Stream)
37677
- pointer = libmupdf._wasm_open_document_with_stream(STRING(magic), from.pointer);
37678
- else
37679
- throw new Error("not a Buffer or Stream");
37680
- let pdf = libmupdf._wasm_pdf_document_from_fz_document(pointer);
37681
- if (pdf)
37682
- return new PDFDocument(pdf);
37683
- return new Document(pointer);
37684
- }
37685
- formatLinkURI(dest) {
37686
- 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));
37687
- }
37688
- asPDF() {
37689
- if (this instanceof PDFDocument)
37690
- return this;
37691
- return null;
37692
- }
37693
- isPDF() {
37694
- return this instanceof PDFDocument;
37695
- }
37696
- needsPassword() {
37697
- return !!libmupdf._wasm_needs_password(this.pointer);
37698
- }
37699
- authenticatePassword(password) {
37700
- return libmupdf._wasm_authenticate_password(this.pointer, STRING(password));
37701
- }
37702
- hasPermission(perm) {
37703
- let perm_ix = Document.PERMISSION[perm];
37704
- return !!libmupdf._wasm_has_permission(this.pointer, perm_ix);
37705
- }
37706
- getMetaData(key) {
37707
- let value = libmupdf._wasm_lookup_metadata(this.pointer, STRING(key));
37708
- if (value)
37709
- return fromString(value);
37710
- return undefined;
37711
- }
37712
- setMetaData(key, value) {
37713
- libmupdf._wasm_set_metadata(this.pointer, STRING(key), STRING2(value));
37714
- }
37715
- countPages() {
37716
- return libmupdf._wasm_count_pages(this.pointer);
37717
- }
37718
- isReflowable() {
37719
- libmupdf._wasm_is_document_reflowable(this.pointer);
37720
- }
37721
- layout(w, h, em) {
37722
- libmupdf._wasm_layout_document(this.pointer, w, h, em);
37723
- }
37724
- loadPage(index) {
37725
- let fz_ptr = libmupdf._wasm_load_page(this.pointer, index);
37726
- if (this instanceof PDFDocument) {
37727
- let pdf_ptr = libmupdf._wasm_pdf_page_from_fz_page(fz_ptr);
37728
- if (pdf_ptr)
37729
- return new PDFPage(this, pdf_ptr);
37730
- }
37731
- return new Page(fz_ptr);
37732
- }
37733
- loadOutline() {
37734
- let doc = this.pointer;
37735
- function to_outline(outline) {
37736
- let result = [];
37737
- while (outline) {
37738
- let title = libmupdf._wasm_outline_get_title(outline);
37739
- let uri = libmupdf._wasm_outline_get_uri(outline);
37740
- let open = libmupdf._wasm_outline_get_is_open(outline);
37741
- let item = {
37742
- title: title ? fromString(title) : undefined,
37743
- uri: uri ? fromString(uri) : undefined,
37744
- open: !!open,
37745
- };
37746
- let page = libmupdf._wasm_outline_get_page(doc, outline);
37747
- if (page >= 0)
37748
- item.page = page;
37749
- let down = libmupdf._wasm_outline_get_down(outline);
37750
- if (down)
37751
- item.down = to_outline(down);
37752
- result.push(item);
37753
- outline = libmupdf._wasm_outline_get_next(outline);
37754
- }
37755
- return result;
37756
- }
37757
- let root = libmupdf._wasm_load_outline(doc);
37758
- if (root)
37759
- return to_outline(root);
37760
- return null;
37761
- }
37762
- resolveLink(link) {
37763
- if (link instanceof Link)
37764
- return libmupdf._wasm_resolve_link(this.pointer, libmupdf._wasm_link_get_uri(link.pointer));
37765
- return libmupdf._wasm_resolve_link(this.pointer, STRING(link));
37766
- }
37767
- resolveLinkDestination(link) {
37768
- let dest;
37769
- if (link instanceof Link)
37770
- dest = libmupdf._wasm_resolve_link_dest(this.pointer, libmupdf._wasm_link_get_uri(link.pointer));
37771
- else
37772
- dest = libmupdf._wasm_resolve_link_dest(this.pointer, STRING(link));
37773
- return {
37774
- type: Document.LINK_DEST[libmupdf._wasm_link_dest_get_type(dest)],
37775
- chapter: libmupdf._wasm_link_dest_get_chapter(dest),
37776
- page: libmupdf._wasm_link_dest_get_page(dest),
37777
- x: libmupdf._wasm_link_dest_get_x(dest),
37778
- y: libmupdf._wasm_link_dest_get_y(dest),
37779
- width: libmupdf._wasm_link_dest_get_w(dest),
37780
- height: libmupdf._wasm_link_dest_get_h(dest),
37781
- zoom: libmupdf._wasm_link_dest_get_zoom(dest),
37782
- };
37783
- }
37784
- outlineIterator() {
37785
- return new OutlineIterator(libmupdf._wasm_new_outline_iterator(this.pointer));
37786
- }
37787
- }
37788
- Document._drop = libmupdf._wasm_drop_document;
37789
- Document.META_FORMAT = "format";
37790
- Document.META_ENCRYPTION = "encryption";
37791
- Document.META_INFO_AUTHOR = "info:Author";
37792
- Document.META_INFO_TITLE = "info:Title";
37793
- Document.META_INFO_SUBJECT = "info:Subject";
37794
- Document.META_INFO_KEYWORDS = "info:Keywords";
37795
- Document.META_INFO_CREATOR = "info:Creator";
37796
- Document.META_INFO_PRODUCER = "info:Producer";
37797
- Document.META_INFO_CREATIONDATE = "info:CreationDate";
37798
- Document.META_INFO_MODIFICATIONDATE = "info:ModDate";
37799
- Document.PERMISSION = {
37800
- "print": "p".charCodeAt(0),
37801
- "copy": "c".charCodeAt(0),
37802
- "edit": "e".charCodeAt(0),
37803
- "annotate": "n".charCodeAt(0),
37804
- "form": "f".charCodeAt(0),
37805
- "accessibility": "y".charCodeAt(0),
37806
- "assemble": "a".charCodeAt(0),
37807
- "print-hq": "h".charCodeAt(0),
37808
- };
37809
- Document.LINK_DEST = [
37810
- "Fit",
37811
- "FitB",
37812
- "FitH",
37813
- "FitBH",
37814
- "FitV",
37815
- "FitBV",
37816
- "FitR",
37817
- "XYZ",
37818
- ];
37819
- class OutlineIterator extends Userdata {
37820
- item() {
37821
- let item = libmupdf._wasm_outline_iterator_item(this.pointer);
37822
- if (item) {
37823
- let title_ptr = libmupdf._wasm_outline_item_get_title(item);
37824
- let uri_ptr = libmupdf._wasm_outline_item_get_uri(item);
37825
- let is_open = libmupdf._wasm_outline_item_get_is_open(item);
37826
- return {
37827
- title: title_ptr ? fromString(title_ptr) : undefined,
37828
- uri: uri_ptr ? fromString(uri_ptr) : undefined,
37829
- open: !!is_open,
37830
- };
37831
- }
37832
- return null;
37833
- }
37834
- next() {
37835
- return libmupdf._wasm_outline_iterator_next(this.pointer);
37836
- }
37837
- prev() {
37838
- return libmupdf._wasm_outline_iterator_prev(this.pointer);
37839
- }
37840
- up() {
37841
- return libmupdf._wasm_outline_iterator_up(this.pointer);
37842
- }
37843
- down() {
37844
- return libmupdf._wasm_outline_iterator_down(this.pointer);
37845
- }
37846
- delete() {
37847
- return libmupdf._wasm_outline_iterator_delete(this.pointer);
37848
- }
37849
- insert(item) {
37850
- return libmupdf._wasm_outline_iterator_insert(this.pointer, STRING_OPT(item.title), STRING2_OPT(item.uri), item.open);
37851
- }
37852
- update(item) {
37853
- libmupdf._wasm_outline_iterator_update(this.pointer, STRING_OPT(item.title), STRING2_OPT(item.uri), item.open);
37854
- }
37855
- }
37856
- OutlineIterator._drop = libmupdf._wasm_drop_outline_iterator;
37857
- OutlineIterator.RESULT_DID_NOT_MOVE = -1;
37858
- OutlineIterator.RESULT_AT_ITEM = 0;
37859
- OutlineIterator.RESULT_AT_EMPTY = 1;
37860
- class Link extends Userdata {
37861
- getBounds() {
37862
- return fromRect(libmupdf._wasm_link_get_rect(this.pointer));
37863
- }
37864
- setBounds(rect) {
37865
- checkRect(rect);
37866
- libmupdf._wasm_link_set_rect(this.pointer, RECT(rect));
37867
- }
37868
- getURI() {
37869
- return fromString(libmupdf._wasm_link_get_uri(this.pointer));
37870
- }
37871
- setURI(uri) {
37872
- checkType(uri, "string");
37873
- libmupdf._wasm_link_set_uri(this.pointer, STRING(uri));
37874
- }
37875
- isExternal() {
37876
- return /^\w[\w+-.]*:/.test(this.getURI());
37877
- }
37878
- }
37879
- Link._drop = libmupdf._wasm_drop_link;
37880
- class Page extends Userdata {
37881
- isPDF() {
37882
- return this instanceof PDFPage;
37883
- }
37884
- getBounds(box = "CropBox") {
37885
- let box_ix = ENUM(box, Page.BOXES);
37886
- return fromRect(libmupdf._wasm_bound_page(this.pointer, box_ix));
37887
- }
37888
- getLabel() {
37889
- return fromString(libmupdf._wasm_page_label(this.pointer));
37890
- }
37891
- run(device, matrix) {
37892
- checkType(device, Device);
37893
- checkMatrix(matrix);
37894
- libmupdf._wasm_run_page(this.pointer, device.pointer, MATRIX(matrix));
37895
- }
37896
- runPageContents(device, matrix) {
37897
- checkType(device, Device);
37898
- checkMatrix(matrix);
37899
- libmupdf._wasm_run_page_contents(this.pointer, device.pointer, MATRIX(matrix));
37900
- }
37901
- runPageAnnots(device, matrix) {
37902
- checkType(device, Device);
37903
- checkMatrix(matrix);
37904
- libmupdf._wasm_run_page_annots(this.pointer, device.pointer, MATRIX(matrix));
37905
- }
37906
- runPageWidgets(device, matrix) {
37907
- checkType(device, Device);
37908
- checkMatrix(matrix);
37909
- libmupdf._wasm_run_page_widgets(this.pointer, device.pointer, MATRIX(matrix));
37910
- }
37911
- toPixmap(matrix, colorspace, alpha = false, showExtras = true) {
37912
- checkType(colorspace, ColorSpace);
37913
- checkMatrix(matrix);
37914
- let result;
37915
- if (showExtras)
37916
- result = libmupdf._wasm_new_pixmap_from_page(this.pointer, MATRIX(matrix), colorspace.pointer, alpha);
37917
- else
37918
- result = libmupdf._wasm_new_pixmap_from_page_contents(this.pointer, MATRIX(matrix), colorspace.pointer, alpha);
37919
- return new Pixmap(result);
37920
- }
37921
- toDisplayList(showExtras = true) {
37922
- let result;
37923
- if (showExtras)
37924
- result = libmupdf._wasm_new_display_list_from_page(this.pointer);
37925
- else
37926
- result = libmupdf._wasm_new_display_list_from_page_contents(this.pointer);
37927
- return new DisplayList(result);
37928
- }
37929
- toStructuredText(options = "") {
37930
- checkType(options, "string");
37931
- return new StructuredText(libmupdf._wasm_new_stext_page_from_page(this.pointer, STRING(options)));
37932
- }
37933
- getLinks() {
37934
- let links = [];
37935
- let link = libmupdf._wasm_load_links(this.pointer);
37936
- while (link) {
37937
- links.push(new Link(libmupdf._wasm_keep_link(link)));
37938
- link = libmupdf._wasm_link_get_next(link);
37939
- }
37940
- return links;
37941
- }
37942
- createLink(bbox, uri) {
37943
- checkRect(bbox);
37944
- return new Link(libmupdf._wasm_create_link(this.pointer, RECT(bbox), STRING(uri)));
37945
- }
37946
- deleteLink(link) {
37947
- checkType(link, Link);
37948
- libmupdf._wasm_delete_link(this.pointer, link.pointer);
37949
- }
37950
- search(needle, max_hits = 500) {
37951
- return runSearch(libmupdf._wasm_search_page, this.pointer, needle, max_hits);
37952
- }
37953
- }
37954
- Page._drop = libmupdf._wasm_drop_page;
37955
- Page.BOXES = [
37956
- "MediaBox",
37957
- "CropBox",
37958
- "BleedBox",
37959
- "TrimBox",
37960
- "ArtBox"
37961
- ];
37962
- /* -------------------------------------------------------------------------- */
37963
- class PDFDocument extends Document {
37964
- constructor(arg1) {
37965
- if (typeof arg1 === "undefined")
37966
- super(libmupdf._wasm_pdf_create_document());
37967
- else if (typeof arg1 === "number")
37968
- super(arg1);
37969
- else if (arg1 instanceof PDFDocument) {
37970
- super(arg1.pointer);
37971
- libmupdf._wasm_keep_document(this.pointer);
37972
- }
37973
- else {
37974
- let doc = Document.openDocument(arg1, "application/pdf");
37975
- if (doc instanceof PDFDocument)
37976
- return doc;
37977
- throw new Error("not a PDF document");
37978
- }
37979
- }
37980
- loadPage(index) {
37981
- return super.loadPage(index);
37982
- }
37983
- // PDFObject instances are always bound to a document, so the WASM/JS value interface lives here.
37984
- // Wrap a pdf_obj in a Userdata object. The pointer must be newly created or we already own it.
37985
- _fromPDFObjectNew(ptr) {
37986
- if (ptr === 0)
37987
- return PDFObject.Null;
37988
- return new PDFObject(this, ptr);
37989
- }
37990
- // Wrap a pdf_obj in a Userdata object. The pointer must be a borrowed pointer, so we have to take ownership.
37991
- _fromPDFObjectKeep(ptr) {
37992
- if (ptr === 0)
37993
- return PDFObject.Null;
37994
- return new PDFObject(this, libmupdf._wasm_pdf_keep_obj(ptr));
37995
- }
37996
- _toPDFObject(obj) {
37997
- if (obj instanceof PDFObject)
37998
- return obj;
37999
- if (obj === null || obj === undefined)
38000
- return this.newNull();
38001
- if (typeof obj === "string") {
38002
- // if a JS string is surrounded by parens, convert it to a PDF string
38003
- if (obj.startsWith("(") && obj.endsWith(")"))
38004
- return this.newString(obj.slice(1, -1));
38005
- // otherwise treat it as a name
38006
- return this.newName(obj);
38007
- }
38008
- if (typeof obj === "number") {
38009
- if (obj === (obj | 0))
38010
- return this.newInteger(obj);
38011
- return this.newReal(obj);
38012
- }
38013
- if (typeof obj === "boolean")
38014
- return this.newBoolean(obj);
38015
- if (obj instanceof Array) {
38016
- let result = this.newArray();
38017
- for (let item of obj)
38018
- result.push(item);
38019
- return result;
38020
- }
38021
- if (obj instanceof Object) {
38022
- let result = this.newDictionary();
38023
- for (let key in obj)
38024
- result.put(key, obj[key]);
38025
- return result;
38026
- }
38027
- throw new TypeError("cannot convert value to PDFObject");
38028
- }
38029
- _PDFOBJ(obj) {
38030
- // Note: We have to create a PDFObject instance for garbage collection.
38031
- return this._toPDFObject(obj).pointer;
38032
- }
38033
- getVersion() {
38034
- return libmupdf._wasm_pdf_version(this.pointer);
38035
- }
38036
- getLanguage() {
38037
- return fromStringOrNull(libmupdf._wasm_pdf_document_language(this.pointer));
38038
- }
38039
- setLanguage(lang) {
38040
- libmupdf._wasm_pdf_set_document_language(this.pointer, STRING(lang));
38041
- }
38042
- countObjects() {
38043
- return libmupdf._wasm_pdf_xref_len(this.pointer);
38044
- }
38045
- getTrailer() {
38046
- return new PDFObject(this, libmupdf._wasm_pdf_trailer(this.pointer));
38047
- }
38048
- createObject() {
38049
- let num = libmupdf._wasm_pdf_create_object(this.pointer);
38050
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_indirect(this.pointer, num));
38051
- }
38052
- newNull() { return PDFObject.Null; }
38053
- newBoolean(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_bool(v)); }
38054
- newInteger(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_int(v)); }
38055
- newReal(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_real(v)); }
38056
- newName(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_name(STRING(v))); }
38057
- newString(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_text_string(STRING(v))); }
38058
- newByteString(v) {
38059
- if (v instanceof Array)
38060
- v = Uint8Array.from(v);
38061
- checkType(v, Uint8Array);
38062
- let len = v.byteLength;
38063
- let ptr = Malloc(len);
38064
- libmupdf.HEAPU8.set(v, ptr);
38065
- try {
38066
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_string(ptr, len));
38067
- }
38068
- finally {
38069
- Free(ptr);
38070
- }
38071
- }
38072
- newIndirect(v) { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_indirect(this.pointer, v)); }
38073
- newArray() { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_array(this.pointer)); }
38074
- newDictionary() { return this._fromPDFObjectNew(libmupdf._wasm_pdf_new_dict(this.pointer)); }
38075
- deleteObject(num) {
38076
- if (num instanceof PDFObject)
38077
- num = num.asIndirect();
38078
- else
38079
- checkType(num, "number");
38080
- libmupdf._wasm_pdf_delete_object(this.pointer, num);
38081
- }
38082
- addObject(obj) {
38083
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_object(this.pointer, this._PDFOBJ(obj)));
38084
- }
38085
- addStream(buf, obj) {
38086
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_stream(this.pointer, BUFFER(buf), this._PDFOBJ(obj), 0));
38087
- }
38088
- addRawStream(buf, obj) {
38089
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_stream(this.pointer, BUFFER(buf), this._PDFOBJ(obj), 1));
38090
- }
38091
- newGraftMap() {
38092
- return new PDFGraftMap(this, libmupdf._wasm_pdf_new_graft_map(this.pointer));
38093
- }
38094
- graftObject(obj) {
38095
- checkType(obj, PDFObject);
38096
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_graft_object(this.pointer, obj.pointer));
38097
- }
38098
- graftPage(to, srcDoc, srcPage) {
38099
- checkType(to, "number");
38100
- checkType(srcDoc, PDFDocument);
38101
- checkType(srcPage, "number");
38102
- libmupdf._wasm_pdf_graft_page(this.pointer, to, srcDoc.pointer, srcPage);
38103
- }
38104
- addSimpleFont(font, encoding = "Latin") {
38105
- checkType(font, Font);
38106
- var encoding_ix = ENUM(encoding, Font.SIMPLE_ENCODING);
38107
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_simple_font(this.pointer, font.pointer, encoding_ix));
38108
- }
38109
- addCJKFont(font, lang, wmode = 0, serif = true) {
38110
- checkType(font, Font);
38111
- if (typeof lang === "string")
38112
- lang = Font.CJK_ORDERING_BY_LANG[lang];
38113
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_cjk_font(this.pointer, font.pointer, lang, wmode, serif));
38114
- }
38115
- addFont(font) {
38116
- checkType(font, Font);
38117
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_cid_font(this.pointer, font.pointer));
38118
- }
38119
- addImage(image) {
38120
- checkType(image, Image);
38121
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_image(this.pointer, image.pointer));
38122
- }
38123
- loadImage(ref) {
38124
- checkType(ref, PDFObject);
38125
- return new Image(libmupdf._wasm_pdf_load_image(this.pointer, ref.pointer));
38126
- }
38127
- findPage(index) {
38128
- checkType(index, "number");
38129
- return this._fromPDFObjectKeep(libmupdf._wasm_pdf_lookup_page_obj(this.pointer, index));
38130
- }
38131
- addPage(mediabox, rotate, resources, contents) {
38132
- checkRect(mediabox);
38133
- checkType(rotate, "number");
38134
- return this._fromPDFObjectNew(libmupdf._wasm_pdf_add_page(this.pointer, RECT(mediabox), rotate, this._PDFOBJ(resources), BUFFER(contents)));
38135
- }
38136
- insertPage(at, obj) {
38137
- checkType(at, "number");
38138
- libmupdf._wasm_pdf_insert_page(this.pointer, at, this._PDFOBJ(obj));
38139
- }
38140
- deletePage(at) {
38141
- checkType(at, "number");
38142
- libmupdf._wasm_pdf_delete_page(this.pointer, at);
38143
- }
38144
- isEmbeddedFile(ref) {
38145
- checkType(ref, PDFObject);
38146
- return !!libmupdf._wasm_pdf_is_embedded_file(ref.pointer);
38147
- }
38148
- addEmbeddedFile(filename, mimetype, contents, created, modified, checksum = false) {
38149
- checkType(filename, "string");
38150
- checkType(mimetype, "string");
38151
- checkType(created, Date);
38152
- checkType(modified, Date);
38153
- checkType(checksum, "boolean");
38154
- 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));
38155
- }
38156
- getFilespecParams(ref) {
38157
- checkType(ref, PDFObject);
38158
- let ptr = libmupdf._wasm_pdf_get_filespec_params(ref.pointer);
38159
- return {
38160
- filename: fromString(libmupdf._wasm_pdf_filespec_params_get_filename(ptr)),
38161
- mimetype: fromString(libmupdf._wasm_pdf_filespec_params_get_mimetype(ptr)),
38162
- size: libmupdf._wasm_pdf_filespec_params_get_filename(ptr),
38163
- creationDate: new Date(libmupdf._wasm_pdf_filespec_params_get_created(ptr) * 1000),
38164
- modificationDate: new Date(libmupdf._wasm_pdf_filespec_params_get_modified(ptr) * 1000),
38165
- };
38166
- }
38167
- getEmbeddedFileContents(ref) {
38168
- checkType(ref, PDFObject);
38169
- let contents = libmupdf._wasm_pdf_load_embedded_file_contents(ref.pointer);
38170
- if (contents)
38171
- return new Buffer$1(contents);
38172
- return null;
38173
- }
38174
- getEmbeddedFiles() {
38175
- function _getEmbeddedFilesRec(result, N) {
38176
- var i, n;
38177
- if (N.isDictionary()) {
38178
- var NN = N.get("Names");
38179
- if (NN)
38180
- for (i = 0, n = NN.length; i < n; i += 2)
38181
- result[NN.get(i + 0).asString()] = NN.get(i + 1);
38182
- var NK = N.get("Kids");
38183
- if (NK)
38184
- for (i = 0, n = NK.length; i < n; i += 1)
38185
- _getEmbeddedFilesRec(result, NK.get(i));
38186
- }
38187
- return result;
38188
- }
38189
- return _getEmbeddedFilesRec({}, this.getTrailer().get("Root", "Names", "EmbeddedFiles"));
38190
- }
38191
- loadNameTree(treeName) {
38192
- function _loadNameTreeRec(dict, node) {
38193
- var kids = node.get("Kids");
38194
- if (kids && kids.isArray())
38195
- for (var i = 0; i < kids.length; i += 1)
38196
- _loadNameTreeRec(dict, kids.get(i));
38197
- var names = node.get("Names");
38198
- if (names && names.isArray())
38199
- for (var i = 0; i < names.length; i += 2)
38200
- dict[names.get(i).asString()] = names.get(i + 1);
38201
- }
38202
- var node = this.getTrailer().get("Root").get("Names").get(treeName);
38203
- var dict = {};
38204
- if (node.isDictionary())
38205
- _loadNameTreeRec(dict, node);
38206
- return dict;
38207
- }
38208
- insertEmbeddedFile(filename, filespec) {
38209
- var efs = this.getEmbeddedFiles();
38210
- efs[filename] = filespec;
38211
- this._rewriteEmbeddedFiles(efs);
38212
- }
38213
- deleteEmbeddedFile(filename) {
38214
- var efs = this.getEmbeddedFiles();
38215
- delete efs[filename];
38216
- this._rewriteEmbeddedFiles(efs);
38217
- }
38218
- _rewriteEmbeddedFiles(efs) {
38219
- var efs_keys = Object.keys(efs);
38220
- efs_keys.sort();
38221
- var root = this.getTrailer().get("Root");
38222
- var root_names = root.get("Names");
38223
- if (!root_names.isDictionary())
38224
- root_names = root.put("Names", this.newDictionary());
38225
- var root_names_efs = root_names.put("EmbeddedFiles", this.newDictionary());
38226
- var root_names_efs_names = root_names_efs.put("Names", this.newArray());
38227
- for (var key of efs_keys) {
38228
- root_names_efs_names.push(this.newString(key));
38229
- root_names_efs_names.push(efs[key]);
38230
- }
38231
- }
38232
- saveToBuffer(options = "") {
38233
- var options_string;
38234
- if (typeof options === "object") {
38235
- options_string = Object.entries(options).map(kv => {
38236
- var k = kv[0];
38237
- var v = kv[1];
38238
- if (v === true)
38239
- return k + "=" + "yes";
38240
- else if (v === false)
38241
- return k + "=" + "no";
38242
- else
38243
- return k + "=" + String(v).replaceAll(",", ":");
38244
- }).join(",");
38245
- }
38246
- else {
38247
- options_string = options;
38248
- }
38249
- return new Buffer$1(libmupdf._wasm_pdf_write_document_buffer(this.pointer, STRING(options_string)));
38250
- }
38251
- save(filename, options = "") {
38252
- if (node_fs)
38253
- node_fs.writeFileSync(filename, this.saveToBuffer(options).asUint8Array());
38254
- else
38255
- throw new Error("missing 'fs' module");
38256
- }
38257
- setPageLabels(index, style = "D", prefix = "", start = 1) {
38258
- libmupdf._wasm_pdf_set_page_labels(this.pointer, index, style.charCodeAt(0), STRING(prefix), start);
38259
- }
38260
- deletePageLabels(index) {
38261
- libmupdf._wasm_pdf_delete_page_labels(this.pointer, index);
38262
- }
38263
- wasRepaired() {
38264
- return !!libmupdf._wasm_pdf_was_repaired(this.pointer);
38265
- }
38266
- hasUnsavedChanges() {
38267
- return !!libmupdf._wasm_pdf_has_unsaved_changes(this.pointer);
38268
- }
38269
- countVersions() {
38270
- return libmupdf._wasm_pdf_count_versions(this.pointer);
38271
- }
38272
- countUnsavedVersions() {
38273
- return libmupdf._wasm_pdf_count_unsaved_versions(this.pointer);
38274
- }
38275
- validateChangeHistory() {
38276
- return libmupdf._wasm_pdf_validate_change_history(this.pointer);
38277
- }
38278
- canBeSavedIncrementally() {
38279
- return !!libmupdf._wasm_pdf_can_be_saved_incrementally(this.pointer);
38280
- }
38281
- enableJournal() {
38282
- libmupdf._wasm_pdf_enable_journal(this.pointer);
38283
- }
38284
- getJournal() {
38285
- let position = libmupdf._wasm_pdf_undoredo_state_position(this.pointer);
38286
- let n = libmupdf._wasm_pdf_undoredo_state_count(this.pointer);
38287
- let steps = [];
38288
- for (let i = 0; i < n; ++i)
38289
- steps.push(fromString(libmupdf._wasm_pdf_undoredo_step(this.pointer, i)));
38290
- return { position, steps };
38291
- }
38292
- beginOperation(op) {
38293
- libmupdf._wasm_pdf_begin_operation(this.pointer, STRING(op));
38294
- }
38295
- beginImplicitOperation() {
38296
- libmupdf._wasm_pdf_begin_implicit_operation(this.pointer);
38297
- }
38298
- endOperation() {
38299
- libmupdf._wasm_pdf_end_operation(this.pointer);
38300
- }
38301
- abandonOperation() {
38302
- libmupdf._wasm_pdf_abandon_operation(this.pointer);
38303
- }
38304
- canUndo() {
38305
- return !!libmupdf._wasm_pdf_can_undo(this.pointer);
38306
- }
38307
- canRedo() {
38308
- return !!libmupdf._wasm_pdf_can_redo(this.pointer);
38309
- }
38310
- undo() {
38311
- libmupdf._wasm_pdf_undo(this.pointer);
38312
- }
38313
- redo() {
38314
- libmupdf._wasm_pdf_redo(this.pointer);
38315
- }
38316
- isJSSupported() {
38317
- return !!libmupdf._wasm_pdf_js_supported(this.pointer);
38318
- }
38319
- enableJS() {
38320
- libmupdf._wasm_pdf_enable_js(this.pointer);
38321
- }
38322
- disableJS() {
38323
- libmupdf._wasm_pdf_disable_js(this.pointer);
38324
- }
38325
- setJSEventListener(_listener) {
38326
- throw "TODO";
38327
- }
38328
- rearrangePages(pages) {
38329
- let n = pages.length;
38330
- let ptr = Malloc(n << 2);
38331
- for (let i = 0; i < n; ++i)
38332
- libmupdf.HEAPU32[(ptr >> 2) + i] = pages[i] || 0;
38333
- try {
38334
- libmupdf._wasm_pdf_rearrange_pages(this.pointer, n, ptr);
38335
- }
38336
- finally {
38337
- Free(ptr);
38338
- }
38339
- }
38340
- subsetFonts() {
38341
- libmupdf._wasm_pdf_subset_fonts(this.pointer);
38342
- }
38343
- bake(bakeAnnots = true, bakeWidgets = true) {
38344
- libmupdf._wasm_pdf_bake_document(this.pointer, bakeAnnots, bakeWidgets);
38345
- }
38346
- countLayers() {
38347
- return libmupdf._wasm_pdf_count_layers(this.pointer);
38348
- }
38349
- isLayerVisible(layer) {
38350
- return !!libmupdf._wasm_pdf_layer_is_enabled(this.pointer, layer);
38351
- }
38352
- setLayerVisible(layer, visible) {
38353
- libmupdf._wasm_pdf_enable_layer(this.pointer, layer, Number(visible));
38354
- }
38355
- getLayerName(layer) {
38356
- return fromString(libmupdf._wasm_pdf_layer_name(this.pointer, layer));
38357
- }
38358
- resetForm(fields, exclude) {
38359
- libmupdf._wasm_pdf_reset_form(this.pointer, this._PDFOBJ(fields), Number(exclude));
38360
- }
38361
- }
38362
- PDFDocument.PAGE_LABEL_NONE = "\0";
38363
- PDFDocument.PAGE_LABEL_DECIMAL = "D";
38364
- PDFDocument.PAGE_LABEL_ROMAN_UC = "R";
38365
- PDFDocument.PAGE_LABEL_ROMAN_LC = "r";
38366
- PDFDocument.PAGE_LABEL_ALPHA_UC = "A";
38367
- PDFDocument.PAGE_LABEL_ALPHA_LC = "a";
38368
- class PDFPage extends Page {
38369
- // PRIVATE
38370
- constructor(doc, pointer) {
38371
- super(pointer);
38372
- this._doc = doc;
38373
- this._annots = null;
38374
- this._widgets = null;
38375
- }
38376
- getObject() {
38377
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_page_get_obj(this.pointer));
38378
- }
38379
- getTransform() {
38380
- return fromMatrix(libmupdf._wasm_pdf_page_transform(this.pointer));
38381
- }
38382
- setPageBox(box, rect) {
38383
- let box_ix = ENUM(box, Page.BOXES);
38384
- checkRect(rect);
38385
- libmupdf._wasm_pdf_set_page_box(this.pointer, box_ix, RECT(rect));
38386
- }
38387
- toPixmap(matrix, colorspace, alpha = false, showExtras = true, usage = "View", box = "CropBox") {
38388
- checkMatrix(matrix);
38389
- checkType(colorspace, ColorSpace);
38390
- let box_ix = ENUM(box, Page.BOXES);
38391
- let result;
38392
- if (showExtras)
38393
- result = libmupdf._wasm_pdf_new_pixmap_from_page_with_usage(this.pointer, MATRIX(matrix), colorspace.pointer, alpha, STRING(usage), box_ix);
38394
- else
38395
- result = libmupdf._wasm_pdf_new_pixmap_from_page_contents_with_usage(this.pointer, MATRIX(matrix), colorspace.pointer, alpha, STRING(usage), box_ix);
38396
- return new Pixmap(result);
38397
- }
38398
- getWidgets() {
38399
- if (!this._widgets) {
38400
- this._widgets = [];
38401
- let widget = libmupdf._wasm_pdf_first_widget(this.pointer);
38402
- while (widget) {
38403
- this._widgets.push(new PDFWidget(this._doc, libmupdf._wasm_pdf_keep_annot(widget)));
38404
- widget = libmupdf._wasm_pdf_next_widget(widget);
38405
- }
38406
- }
38407
- return this._widgets;
38408
- }
38409
- getAnnotations() {
38410
- if (!this._annots) {
38411
- this._annots = [];
38412
- let annot = libmupdf._wasm_pdf_first_annot(this.pointer);
38413
- while (annot) {
38414
- this._annots.push(new PDFAnnotation(this._doc, libmupdf._wasm_pdf_keep_annot(annot)));
38415
- annot = libmupdf._wasm_pdf_next_annot(annot);
38416
- }
38417
- }
38418
- return this._annots;
38419
- }
38420
- createAnnotation(type) {
38421
- let type_ix = ENUM(type, PDFAnnotation.ANNOT_TYPES);
38422
- let annot = new PDFAnnotation(this._doc, libmupdf._wasm_pdf_create_annot(this.pointer, type_ix));
38423
- if (this._annots)
38424
- this._annots.push(annot);
38425
- return annot;
38426
- }
38427
- deleteAnnotation(annot) {
38428
- checkType(annot, PDFAnnotation);
38429
- libmupdf._wasm_pdf_delete_annot(this.pointer, annot.pointer);
38430
- if (this._annots) {
38431
- let ix = this._annots.indexOf(annot);
38432
- if (ix >= 0)
38433
- this._annots.splice(ix, 1);
38434
- }
38435
- }
38436
- applyRedactions(black_boxes = true, image_method = 2, line_art_method = 1, text_method = 0) {
38437
- libmupdf._wasm_pdf_redact_page(this.pointer, Number(black_boxes), image_method, line_art_method, text_method);
38438
- }
38439
- update() {
38440
- return !!libmupdf._wasm_pdf_update_page(this.pointer);
38441
- }
38442
- }
38443
- PDFPage.REDACT_IMAGE_NONE = 0;
38444
- PDFPage.REDACT_IMAGE_REMOVE = 1;
38445
- PDFPage.REDACT_IMAGE_PIXELS = 2;
38446
- PDFPage.REDACT_IMAGE_UNLESS_INVISIBLE = 3;
38447
- PDFPage.REDACT_LINE_ART_NONE = 0;
38448
- PDFPage.REDACT_LINE_ART_REMOVE_IF_COVERED = 1;
38449
- PDFPage.REDACT_LINE_ART_REMOVE_IF_TOUCHED = 2;
38450
- PDFPage.REDACT_TEXT_REMOVE = 0;
38451
- PDFPage.REDACT_TEXT_NONE = 1;
38452
- class PDFObject extends Userdata {
38453
- // PRIVATE
38454
- constructor(doc, pointer) {
38455
- super(libmupdf._wasm_pdf_keep_obj(pointer));
38456
- this._doc = doc;
38457
- }
38458
- isNull() { return this === PDFObject.Null; }
38459
- isIndirect() { return !!libmupdf._wasm_pdf_is_indirect(this.pointer); }
38460
- isBoolean() { return !!libmupdf._wasm_pdf_is_bool(this.pointer); }
38461
- isInteger() { return !!libmupdf._wasm_pdf_is_int(this.pointer); }
38462
- isReal() { return !!libmupdf._wasm_pdf_is_real(this.pointer); }
38463
- isNumber() { return !!libmupdf._wasm_pdf_is_number(this.pointer); }
38464
- isName() { return !!libmupdf._wasm_pdf_is_name(this.pointer); }
38465
- isString() { return !!libmupdf._wasm_pdf_is_string(this.pointer); }
38466
- isArray() { return !!libmupdf._wasm_pdf_is_array(this.pointer); }
38467
- isDictionary() { return !!libmupdf._wasm_pdf_is_dict(this.pointer); }
38468
- isStream() { return !!libmupdf._wasm_pdf_is_stream(this.pointer); }
38469
- asIndirect() { return libmupdf._wasm_pdf_to_num(this.pointer); }
38470
- asBoolean() { return !!libmupdf._wasm_pdf_to_bool(this.pointer); }
38471
- asNumber() { return libmupdf._wasm_pdf_to_real(this.pointer); }
38472
- asName() { return fromString(libmupdf._wasm_pdf_to_name(this.pointer)); }
38473
- asString() { return fromString(libmupdf._wasm_pdf_to_text_string(this.pointer)); }
38474
- asByteString() {
38475
- let ptr = libmupdf._wasm_pdf_to_string(this.pointer, _wasm_int);
38476
- let len = libmupdf.HEAPU32[_wasm_int >> 2];
38477
- return libmupdf.HEAPU8.slice(ptr, ptr + len);
38478
- }
38479
- readStream() { return new Buffer$1(libmupdf._wasm_pdf_load_stream(this.pointer)); }
38480
- readRawStream() { return new Buffer$1(libmupdf._wasm_pdf_load_raw_stream(this.pointer)); }
38481
- writeObject(obj) {
38482
- if (!this.isIndirect())
38483
- throw new TypeError("can only call PDFObject.writeObject on an indirect reference");
38484
- libmupdf._wasm_pdf_update_object(this._doc.pointer, this.asIndirect(), this._doc._PDFOBJ(obj));
38485
- }
38486
- writeStream(buf) {
38487
- if (!this.isIndirect())
38488
- throw new TypeError("can only call PDFObject.writeStream on an indirect reference");
38489
- libmupdf._wasm_pdf_update_stream(this._doc.pointer, this.pointer, BUFFER(buf), 0);
38490
- }
38491
- writeRawStream(buf) {
38492
- if (!this.isIndirect())
38493
- throw new TypeError("can only call PDFObject.writeRawStream on an indirect reference");
38494
- libmupdf._wasm_pdf_update_stream(this._doc.pointer, this.pointer, BUFFER(buf), 1);
38495
- }
38496
- resolve() {
38497
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_resolve_indirect(this.pointer));
38498
- }
38499
- get length() {
38500
- return libmupdf._wasm_pdf_array_len(this.pointer);
38501
- }
38502
- set length(_) {
38503
- throw new TypeError("object length is read-only");
38504
- }
38505
- _get(path) {
38506
- let obj = this.pointer;
38507
- for (let key of path) {
38508
- if (typeof key === "number")
38509
- obj = libmupdf._wasm_pdf_array_get(obj, key);
38510
- else if (key instanceof PDFObject)
38511
- obj = libmupdf._wasm_pdf_dict_get(obj, key.pointer);
38512
- else
38513
- obj = libmupdf._wasm_pdf_dict_gets(obj, STRING(key));
38514
- if (obj === 0)
38515
- break;
38516
- }
38517
- return obj;
38518
- }
38519
- get(...path) {
38520
- return this._doc._fromPDFObjectKeep(this._get(path));
38521
- }
38522
- getInheritable(key) {
38523
- if (key instanceof PDFObject)
38524
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_get_inheritable(this.pointer, key.pointer));
38525
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_gets_inheritable(this.pointer, STRING(key)));
38526
- }
38527
- put(key, value) {
38528
- value = this._doc._toPDFObject(value);
38529
- if (typeof key === "number")
38530
- libmupdf._wasm_pdf_array_put(this.pointer, key, value.pointer);
38531
- else if (key instanceof PDFObject)
38532
- libmupdf._wasm_pdf_dict_put(this.pointer, key.pointer, value.pointer);
38533
- else
38534
- libmupdf._wasm_pdf_dict_puts(this.pointer, STRING(key), value.pointer);
38535
- return value;
38536
- }
38537
- push(value) {
38538
- value = this._doc._toPDFObject(value);
38539
- libmupdf._wasm_pdf_array_push(this.pointer, value.pointer);
38540
- return value;
38541
- }
38542
- delete(key) {
38543
- if (typeof key === "number")
38544
- libmupdf._wasm_pdf_array_delete(this.pointer, key);
38545
- else if (key instanceof PDFObject)
38546
- libmupdf._wasm_pdf_dict_del(this.pointer, key.pointer);
38547
- else
38548
- libmupdf._wasm_pdf_dict_dels(this.pointer, STRING(key));
38549
- }
38550
- valueOf() {
38551
- if (this.isNull())
38552
- return null;
38553
- if (this.isBoolean())
38554
- return this.asBoolean();
38555
- if (this.isNumber())
38556
- return this.asNumber();
38557
- if (this.isName())
38558
- return this.asName();
38559
- if (this.isString())
38560
- return this.asString();
38561
- if (this.isIndirect())
38562
- return `${this.asIndirect()} 0 R`;
38563
- return this;
38564
- }
38565
- toString(tight = true, ascii = true) {
38566
- return fromStringFree(libmupdf._wasm_pdf_sprint_obj(this.pointer, tight, ascii));
38567
- }
38568
- forEach(fn) {
38569
- if (this.isArray()) {
38570
- let n = this.length;
38571
- for (let i = 0; i < n; ++i)
38572
- fn(this.get(i), i, this);
38573
- }
38574
- else if (this.isDictionary()) {
38575
- let n = libmupdf._wasm_pdf_dict_len(this.pointer);
38576
- for (let i = 0; i < n; ++i) {
38577
- let key = this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_get_key(this.pointer, i));
38578
- let val = this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_dict_get_val(this.pointer, i));
38579
- fn(val, key.asName(), this);
38580
- }
38581
- }
38582
- }
38583
- // Convert to plain Javascript values, objects, and arrays.
38584
- // If you want to resolve indirect references, pass an empty object or array as the first argument.
38585
- // On exit, this object will contain all indirect objects encountered indexed by object number.
38586
- // Note: This function will omit cyclic references.
38587
- asJS(seen) {
38588
- if (this.isIndirect()) {
38589
- let ref = this.asIndirect();
38590
- if (!seen)
38591
- return `${ref} 0 R`;
38592
- if (ref in seen)
38593
- return seen[ref];
38594
- seen[ref] = PDFObject.Null; // stop recursion!
38595
- return seen[ref] = this.resolve().asJS(seen);
38596
- }
38597
- if (this.isArray()) {
38598
- let result = [];
38599
- this.forEach(val => {
38600
- result.push(val.asJS(seen));
38601
- });
38602
- return result;
38603
- }
38604
- if (this.isDictionary()) {
38605
- let result = {};
38606
- this.forEach((val, key) => {
38607
- result[key] = val.asJS(seen);
38608
- });
38609
- return result;
38610
- }
38611
- return this.valueOf();
38612
- }
38613
- }
38614
- PDFObject._drop = libmupdf._wasm_pdf_drop_obj;
38615
- PDFObject.Null = new PDFObject(null, 0);
38616
- class PDFGraftMap extends Userdata {
38617
- // PRIVATE
38618
- constructor(doc, pointer) {
38619
- super(pointer);
38620
- this._doc = doc;
38621
- }
38622
- graftObject(obj) {
38623
- checkType(obj, PDFObject);
38624
- return this._doc._fromPDFObjectNew(libmupdf._wasm_pdf_graft_mapped_object(this.pointer, obj.pointer));
38625
- }
38626
- graftPage(to, srcDoc, srcPage) {
38627
- checkType(to, "number");
38628
- checkType(srcDoc, PDFDocument);
38629
- checkType(srcPage, "number");
38630
- libmupdf._wasm_pdf_graft_mapped_page(this.pointer, to, srcDoc.pointer, srcPage);
38631
- }
38632
- }
38633
- PDFGraftMap._drop = libmupdf._wasm_pdf_drop_graft_map;
38634
- class PDFAnnotation extends Userdata {
38635
- // PRIVATE
38636
- constructor(doc, pointer) {
38637
- super(pointer);
38638
- this._doc = doc;
38639
- }
38640
- getObject() {
38641
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_annot_obj(this.pointer));
38642
- }
38643
- getBounds() {
38644
- return fromRect(libmupdf._wasm_pdf_bound_annot(this.pointer));
38645
- }
38646
- run(device, matrix) {
38647
- checkType(device, Device);
38648
- checkMatrix(matrix);
38649
- libmupdf._wasm_pdf_run_annot(this.pointer, device.pointer, MATRIX(matrix));
38650
- }
38651
- toPixmap(matrix, colorspace, alpha = false) {
38652
- checkMatrix(matrix);
38653
- checkType(colorspace, ColorSpace);
38654
- return new Pixmap(libmupdf._wasm_pdf_new_pixmap_from_annot(this.pointer, MATRIX(matrix), colorspace.pointer, alpha));
38655
- }
38656
- toDisplayList() {
38657
- return new DisplayList(libmupdf._wasm_pdf_new_display_list_from_annot(this.pointer));
38658
- }
38659
- update() {
38660
- return !!libmupdf._wasm_pdf_update_annot(this.pointer);
38661
- }
38662
- getType() {
38663
- let type = libmupdf._wasm_pdf_annot_type(this.pointer);
38664
- return PDFAnnotation.ANNOT_TYPES[type] || "Text";
38665
- }
38666
- getLanguage() {
38667
- return fromStringOrNull(libmupdf._wasm_pdf_annot_language(this.pointer));
38668
- }
38669
- setLanguage(lang) {
38670
- libmupdf._wasm_pdf_set_annot_language(this.pointer, STRING(lang));
38671
- }
38672
- getFlags() {
38673
- return libmupdf._wasm_pdf_annot_flags(this.pointer);
38674
- }
38675
- setFlags(flags) {
38676
- return libmupdf._wasm_pdf_set_annot_flags(this.pointer, flags);
38677
- }
38678
- getContents() {
38679
- return fromString(libmupdf._wasm_pdf_annot_contents(this.pointer));
38680
- }
38681
- setContents(text) {
38682
- libmupdf._wasm_pdf_set_annot_contents(this.pointer, STRING(text));
38683
- }
38684
- getAuthor() {
38685
- return fromString(libmupdf._wasm_pdf_annot_author(this.pointer));
38686
- }
38687
- setAuthor(text) {
38688
- libmupdf._wasm_pdf_set_annot_author(this.pointer, STRING(text));
38689
- }
38690
- getCreationDate() {
38691
- return new Date(libmupdf._wasm_pdf_annot_creation_date(this.pointer) * 1000);
38692
- }
38693
- setCreationDate(date) {
38694
- checkType(date, Date);
38695
- libmupdf._wasm_pdf_set_annot_creation_date(this.pointer, date.getTime() / 1000);
38696
- }
38697
- getModificationDate() {
38698
- return new Date(libmupdf._wasm_pdf_annot_modification_date(this.pointer) * 1000);
38699
- }
38700
- setModificationDate(date) {
38701
- checkType(date, Date);
38702
- libmupdf._wasm_pdf_set_annot_modification_date(this.pointer, date.getTime() / 1000);
38703
- }
38704
- hasRect() {
38705
- return !!libmupdf._wasm_pdf_annot_has_rect(this.pointer);
38706
- }
38707
- hasInkList() {
38708
- return !!libmupdf._wasm_pdf_annot_has_ink_list(this.pointer);
38709
- }
38710
- hasQuadPoints() {
38711
- return !!libmupdf._wasm_pdf_annot_has_quad_points(this.pointer);
38712
- }
38713
- hasVertices() {
38714
- return !!libmupdf._wasm_pdf_annot_has_vertices(this.pointer);
38715
- }
38716
- hasLine() {
38717
- return !!libmupdf._wasm_pdf_annot_has_line(this.pointer);
38718
- }
38719
- hasInteriorColor() {
38720
- return !!libmupdf._wasm_pdf_annot_has_interior_color(this.pointer);
38721
- }
38722
- hasLineEndingStyles() {
38723
- return !!libmupdf._wasm_pdf_annot_has_line_ending_styles(this.pointer);
38724
- }
38725
- hasBorder() {
38726
- return !!libmupdf._wasm_pdf_annot_has_border(this.pointer);
38727
- }
38728
- hasBorderEffect() {
38729
- return !!libmupdf._wasm_pdf_annot_has_border_effect(this.pointer);
38730
- }
38731
- hasIcon() {
38732
- return !!libmupdf._wasm_pdf_annot_has_icon_name(this.pointer);
38733
- }
38734
- hasOpen() {
38735
- return !!libmupdf._wasm_pdf_annot_has_open(this.pointer);
38736
- }
38737
- hasAuthor() {
38738
- return !!libmupdf._wasm_pdf_annot_has_author(this.pointer);
38739
- }
38740
- hasFilespec() {
38741
- return !!libmupdf._wasm_pdf_annot_has_filespec(this.pointer);
38742
- }
38743
- hasCallout() {
38744
- return !!libmupdf._wasm_pdf_annot_has_callout(this.pointer);
38745
- }
38746
- hasRichContents() {
38747
- return !!libmupdf._wasm_pdf_annot_has_rich_contents(this.pointer);
38748
- }
38749
- getRect() {
38750
- return fromRect(libmupdf._wasm_pdf_annot_rect(this.pointer));
38751
- }
38752
- setRect(rect) {
38753
- checkRect(rect);
38754
- libmupdf._wasm_pdf_set_annot_rect(this.pointer, RECT(rect));
38755
- }
38756
- getPopup() {
38757
- return fromRect(libmupdf._wasm_pdf_annot_popup(this.pointer));
38758
- }
38759
- setPopup(rect) {
38760
- checkRect(rect);
38761
- libmupdf._wasm_pdf_set_annot_popup(this.pointer, RECT(rect));
38762
- }
38763
- getIsOpen() {
38764
- return !!libmupdf._wasm_pdf_annot_is_open(this.pointer);
38765
- }
38766
- setIsOpen(isOpen) {
38767
- checkType(isOpen, "boolean");
38768
- libmupdf._wasm_pdf_set_annot_is_open(this.pointer, isOpen);
38769
- }
38770
- getHiddenForEditing() {
38771
- return !!libmupdf._wasm_pdf_annot_hidden_for_editing(this.pointer);
38772
- }
38773
- setHiddenForEditing(isHidden) {
38774
- checkType(isHidden, "boolean");
38775
- libmupdf._wasm_pdf_set_annot_hidden_for_editing(this.pointer, isHidden);
38776
- }
38777
- getIcon() {
38778
- return fromString(libmupdf._wasm_pdf_annot_icon_name(this.pointer));
38779
- }
38780
- setIcon(text) {
38781
- checkType(text, "string");
38782
- libmupdf._wasm_pdf_set_annot_icon_name(this.pointer, STRING(text));
38783
- }
38784
- getOpacity() {
38785
- return libmupdf._wasm_pdf_annot_opacity(this.pointer);
38786
- }
38787
- setOpacity(opacity) {
38788
- checkType(opacity, "number");
38789
- libmupdf._wasm_pdf_set_annot_opacity(this.pointer, opacity);
38790
- }
38791
- getQuadding() {
38792
- return libmupdf._wasm_pdf_annot_quadding(this.pointer);
38793
- }
38794
- setQuadding(quadding) {
38795
- checkType(quadding, "number");
38796
- libmupdf._wasm_pdf_set_annot_quadding(this.pointer, quadding);
38797
- }
38798
- getLine() {
38799
- let a = fromPoint(libmupdf._wasm_pdf_annot_line_1(this.pointer));
38800
- let b = fromPoint(libmupdf._wasm_pdf_annot_line_2(this.pointer));
38801
- return [a, b];
38802
- }
38803
- setLine(a, b) {
38804
- checkPoint(a);
38805
- checkPoint(b);
38806
- libmupdf._wasm_pdf_set_annot_line(this.pointer, POINT(a), POINT2(b));
38807
- }
38808
- getLineEndingStyles() {
38809
- let a = libmupdf._wasm_pdf_annot_line_ending_styles_start(this.pointer);
38810
- let b = libmupdf._wasm_pdf_annot_line_ending_styles_end(this.pointer);
38811
- return {
38812
- start: PDFAnnotation.LINE_ENDING[a] || "None",
38813
- end: PDFAnnotation.LINE_ENDING[b] || "None",
38814
- };
38815
- }
38816
- setLineEndingStyles(start, end) {
38817
- let start_ix = ENUM(start, PDFAnnotation.LINE_ENDING);
38818
- let end_ix = ENUM(end, PDFAnnotation.LINE_ENDING);
38819
- libmupdf._wasm_pdf_set_annot_line_ending_styles(this.pointer, start_ix, end_ix);
38820
- }
38821
- getLineCaption() {
38822
- return libmupdf._wasm_pdf_annot_line_caption(this.pointer);
38823
- }
38824
- setLineCaption(on) {
38825
- return libmupdf._wasm_pdf_set_annot_line_caption(this.pointer, on);
38826
- }
38827
- getLineCaptionOffset() {
38828
- return fromPoint(libmupdf._wasm_pdf_annot_line_caption_offset(this.pointer));
38829
- }
38830
- setLineCaptionOffset(p) {
38831
- return libmupdf._wasm_pdf_set_annot_line_caption_offset(this.pointer, POINT(p));
38832
- }
38833
- getLineLeader() {
38834
- return libmupdf._wasm_pdf_annot_line_leader(this.pointer);
38835
- }
38836
- getLineLeaderExtension() {
38837
- return libmupdf._wasm_pdf_annot_line_leader_extension(this.pointer);
38838
- }
38839
- getLineLeaderOffset() {
38840
- return libmupdf._wasm_pdf_annot_line_leader_offset(this.pointer);
38841
- }
38842
- setLineLeader(v) {
38843
- return libmupdf._wasm_pdf_set_annot_line_leader(this.pointer, v);
38844
- }
38845
- setLineLeaderExtension(v) {
38846
- return libmupdf._wasm_pdf_set_annot_line_leader_extension(this.pointer, v);
38847
- }
38848
- setLineLeaderOffset(v) {
38849
- return libmupdf._wasm_pdf_set_annot_line_leader_offset(this.pointer, v);
38850
- }
38851
- getCalloutStyle() {
38852
- let style = libmupdf._wasm_pdf_annot_callout_style(this.pointer);
38853
- return PDFAnnotation.LINE_ENDING[style] || "None";
38854
- }
38855
- setCalloutStyle(style) {
38856
- let style_ix = ENUM(style, PDFAnnotation.LINE_ENDING);
38857
- libmupdf._wasm_pdf_set_annot_callout_style(this.pointer, style_ix);
38858
- }
38859
- getCalloutLine() {
38860
- let n = libmupdf._wasm_pdf_annot_callout_line(this.pointer, (_wasm_point << 2));
38861
- if (n == 3)
38862
- return [
38863
- fromPoint((_wasm_point + 0) << 2),
38864
- fromPoint((_wasm_point + 1) << 2),
38865
- fromPoint((_wasm_point + 2) << 2)
38866
- ];
38867
- if (n == 2)
38868
- return [
38869
- fromPoint((_wasm_point + 0) << 2),
38870
- fromPoint((_wasm_point + 1) << 2)
38871
- ];
38872
- return null;
38873
- }
38874
- setCalloutLine(line) {
38875
- let a = line[0] || [0, 0];
38876
- let b = line[1] || [0, 0];
38877
- let c = line[2] || [0, 0];
38878
- libmupdf._wasm_pdf_set_annot_callout_line(this.pointer, line.length, POINT(a), POINT2(b), POINT3(c));
38879
- }
38880
- getCalloutPoint() {
38881
- let line = this.getCalloutLine();
38882
- if (line)
38883
- return line[0];
38884
- return undefined;
38885
- }
38886
- setCalloutPoint(p) {
38887
- libmupdf._wasm_pdf_set_annot_callout_point(this.pointer, POINT(p));
38888
- }
38889
- getColor() {
38890
- return fromAnnotColor(libmupdf._wasm_pdf_annot_color(this.pointer, COLOR()));
38891
- }
38892
- getInteriorColor() {
38893
- return fromAnnotColor(libmupdf._wasm_pdf_annot_interior_color(this.pointer, COLOR()));
38894
- }
38895
- setColor(color) {
38896
- checkAnnotColor(color);
38897
- libmupdf._wasm_pdf_set_annot_color(this.pointer, color.length, COLOR(color));
38898
- }
38899
- setInteriorColor(color) {
38900
- checkAnnotColor(color);
38901
- libmupdf._wasm_pdf_set_annot_interior_color(this.pointer, color.length, COLOR(color));
38902
- }
38903
- getBorderWidth() {
38904
- return libmupdf._wasm_pdf_annot_border_width(this.pointer);
38905
- }
38906
- setBorderWidth(value) {
38907
- checkType(value, "number");
38908
- return libmupdf._wasm_pdf_set_annot_border_width(this.pointer, value);
38909
- }
38910
- getBorderStyle() {
38911
- return PDFAnnotation.BORDER_STYLE[libmupdf._wasm_pdf_annot_border_style(this.pointer)] || "Solid";
38912
- }
38913
- setBorderStyle(value) {
38914
- let value_ix = ENUM(value, PDFAnnotation.BORDER_STYLE);
38915
- return libmupdf._wasm_pdf_set_annot_border_style(this.pointer, value_ix);
38916
- }
38917
- getBorderEffect() {
38918
- return PDFAnnotation.BORDER_EFFECT[libmupdf._wasm_pdf_annot_border_effect(this.pointer)] || "None";
38919
- }
38920
- setBorderEffect(value) {
38921
- let value_ix = ENUM(value, PDFAnnotation.BORDER_EFFECT);
38922
- return libmupdf._wasm_pdf_set_annot_border_effect(this.pointer, value_ix);
38923
- }
38924
- getBorderEffectIntensity() {
38925
- return libmupdf._wasm_pdf_annot_border_effect_intensity(this.pointer);
38926
- }
38927
- setBorderEffectIntensity(value) {
38928
- checkType(value, "number");
38929
- return libmupdf._wasm_pdf_set_annot_border_effect_intensity(this.pointer, value);
38930
- }
38931
- getBorderDashCount() {
38932
- return libmupdf._wasm_pdf_annot_border_dash_count(this.pointer);
38933
- }
38934
- getBorderDashItem(idx) {
38935
- return libmupdf._wasm_pdf_annot_border_dash_item(this.pointer, idx);
38936
- }
38937
- clearBorderDash() {
38938
- libmupdf._wasm_pdf_clear_annot_border_dash(this.pointer);
38939
- }
38940
- addBorderDashItem(v) {
38941
- checkType(v, "number");
38942
- return libmupdf._wasm_pdf_add_annot_border_dash_item(this.pointer, v);
38943
- }
38944
- getBorderDashPattern() {
38945
- let n = this.getBorderDashCount();
38946
- let result = new Array(n);
38947
- for (let i = 0; i < n; ++i)
38948
- result[i] = this.getBorderDashItem(i);
38949
- return result;
38950
- }
38951
- setBorderDashPattern(list) {
38952
- this.clearBorderDash();
38953
- for (let v of list)
38954
- this.addBorderDashItem(v);
38955
- }
38956
- getIntent() {
38957
- return PDFAnnotation.INTENT[libmupdf._wasm_pdf_annot_intent(this.pointer)] || null;
38958
- }
38959
- setIntent(value) {
38960
- let value_ix = ENUM(value, PDFAnnotation.INTENT);
38961
- return libmupdf._wasm_pdf_set_annot_intent(this.pointer, value_ix);
38962
- }
38963
- setDefaultAppearance(fontName, size, color) {
38964
- checkType(fontName, "string");
38965
- checkType(size, "number");
38966
- checkAnnotColor(color);
38967
- libmupdf._wasm_pdf_set_annot_default_appearance(this.pointer, STRING(fontName), size, color.length, COLOR(color));
38968
- }
38969
- getDefaultAppearance() {
38970
- let font = fromString(libmupdf._wasm_pdf_annot_default_appearance_font(this.pointer));
38971
- let size = libmupdf._wasm_pdf_annot_default_appearance_size(this.pointer);
38972
- let color = fromAnnotColor(libmupdf._wasm_pdf_annot_default_appearance_color(this.pointer, COLOR()));
38973
- return { font, size, color };
38974
- }
38975
- getFileSpec() {
38976
- return this._doc._fromPDFObjectKeep(libmupdf._wasm_pdf_annot_filespec(this.pointer));
38977
- }
38978
- setFileSpec(fs) {
38979
- return libmupdf._wasm_pdf_set_annot_filespec(this.pointer, this._doc._PDFOBJ(fs));
38980
- }
38981
- getQuadPoints() {
38982
- let n = libmupdf._wasm_pdf_annot_quad_point_count(this.pointer);
38983
- let result = [];
38984
- for (let i = 0; i < n; ++i)
38985
- result.push(fromQuad(libmupdf._wasm_pdf_annot_quad_point(this.pointer, i)));
38986
- return result;
38987
- }
38988
- clearQuadPoints() {
38989
- libmupdf._wasm_pdf_clear_annot_quad_points(this.pointer);
38990
- }
38991
- addQuadPoint(quad) {
38992
- checkQuad(quad);
38993
- libmupdf._wasm_pdf_add_annot_quad_point(this.pointer, QUAD(quad));
38994
- }
38995
- setQuadPoints(quadlist) {
38996
- this.clearQuadPoints();
38997
- for (let quad of quadlist)
38998
- this.addQuadPoint(quad);
38999
- }
39000
- getVertices() {
39001
- let n = libmupdf._wasm_pdf_annot_vertex_count(this.pointer);
39002
- let result = new Array(n);
39003
- for (let i = 0; i < n; ++i)
39004
- result[i] = fromPoint(libmupdf._wasm_pdf_annot_vertex(this.pointer, i));
39005
- return result;
39006
- }
39007
- clearVertices() {
39008
- libmupdf._wasm_pdf_clear_annot_vertices(this.pointer);
39009
- }
39010
- addVertex(vertex) {
39011
- checkPoint(vertex);
39012
- libmupdf._wasm_pdf_add_annot_vertex(this.pointer, POINT(vertex));
39013
- }
39014
- setVertices(vertexlist) {
39015
- this.clearVertices();
39016
- for (let vertex of vertexlist)
39017
- this.addVertex(vertex);
39018
- }
39019
- getInkList() {
39020
- let n = libmupdf._wasm_pdf_annot_ink_list_count(this.pointer);
39021
- let outer = [];
39022
- for (let i = 0; i < n; ++i) {
39023
- let m = libmupdf._wasm_pdf_annot_ink_list_stroke_count(this.pointer, i);
39024
- let inner = new Array(m);
39025
- for (let k = 0; k < m; ++k)
39026
- inner[k] = fromPoint(libmupdf._wasm_pdf_annot_ink_list_stroke_vertex(this.pointer, i, k));
39027
- outer.push(inner);
39028
- }
39029
- return outer;
39030
- }
39031
- clearInkList() {
39032
- libmupdf._wasm_pdf_clear_annot_ink_list(this.pointer);
39033
- }
39034
- addInkListStroke() {
39035
- libmupdf._wasm_pdf_add_annot_ink_list_stroke(this.pointer);
39036
- }
39037
- addInkListStrokeVertex(v) {
39038
- checkPoint(v);
39039
- libmupdf._wasm_pdf_add_annot_ink_list_stroke_vertex(this.pointer, POINT(v));
39040
- }
39041
- setInkList(inklist) {
39042
- this.clearInkList();
39043
- for (let stroke of inklist) {
39044
- this.addInkListStroke();
39045
- for (let vertex of stroke)
39046
- this.addInkListStrokeVertex(vertex);
39047
- }
39048
- }
39049
- getRichContents() {
39050
- return fromString(libmupdf._wasm_pdf_annot_rich_contents(this.pointer));
39051
- }
39052
- setRichContents(plain, html) {
39053
- checkType(plain, "string");
39054
- checkType(html, "string");
39055
- libmupdf._wasm_pdf_set_annot_rich_contents(this.pointer, STRING(plain), STRING2(html));
39056
- }
39057
- getRichDefaults() {
39058
- return fromString(libmupdf._wasm_pdf_annot_rich_defaults(this.pointer));
39059
- }
39060
- setRichDefaults(style) {
39061
- checkType(style, "string");
39062
- libmupdf._wasm_pdf_set_annot_rich_defaults(this.pointer, STRING(style));
39063
- }
39064
- setStampImage(image) {
39065
- libmupdf._wasm_pdf_set_annot_stamp_image(this.pointer, image.pointer);
39066
- }
39067
- setAppearanceFromDisplayList(appearance, state, transform, list) {
39068
- checkMatrix(transform);
39069
- checkType(list, DisplayList);
39070
- libmupdf._wasm_pdf_set_annot_appearance_from_display_list(this.pointer, STRING_OPT(appearance), STRING2_OPT(state), MATRIX(transform), list.pointer);
39071
- }
39072
- setAppearance(appearance, state, transform, bbox, resources, contents) {
39073
- checkMatrix(transform);
39074
- checkRect(bbox);
39075
- libmupdf._wasm_pdf_set_annot_appearance(this.pointer, STRING_OPT(appearance), STRING2_OPT(state), MATRIX(transform), RECT(bbox), this._doc._PDFOBJ(resources), BUFFER(contents));
39076
- }
39077
- applyRedaction(black_boxes = 1, image_method = 2, line_art_method = 1, text_method = 0) {
39078
- libmupdf._wasm_pdf_apply_redaction(this.pointer, black_boxes, image_method, line_art_method, text_method);
39079
- }
39080
- }
39081
- PDFAnnotation._drop = libmupdf._wasm_pdf_drop_annot;
39082
- /* IMPORTANT: Keep in sync with mupdf/pdf/annot.h and PDFAnnotation.java */
39083
- PDFAnnotation.ANNOT_TYPES = [
39084
- "Text",
39085
- "Link",
39086
- "FreeText",
39087
- "Line",
39088
- "Square",
39089
- "Circle",
39090
- "Polygon",
39091
- "PolyLine",
39092
- "Highlight",
39093
- "Underline",
39094
- "Squiggly",
39095
- "StrikeOut",
39096
- "Redact",
39097
- "Stamp",
39098
- "Caret",
39099
- "Ink",
39100
- "Popup",
39101
- "FileAttachment",
39102
- "Sound",
39103
- "Movie",
39104
- "RichMedia",
39105
- "Widget",
39106
- "Screen",
39107
- "PrinterMark",
39108
- "TrapNet",
39109
- "Watermark",
39110
- "3D",
39111
- "Projection",
39112
- ];
39113
- PDFAnnotation.LINE_ENDING = [
39114
- "None",
39115
- "Square",
39116
- "Circle",
39117
- "Diamond",
39118
- "OpenArrow",
39119
- "ClosedArrow",
39120
- "Butt",
39121
- "ROpenArrow",
39122
- "RClosedArrow",
39123
- "Slash",
39124
- ];
39125
- PDFAnnotation.BORDER_STYLE = ["Solid", "Dashed", "Beveled", "Inset", "Underline"];
39126
- PDFAnnotation.BORDER_EFFECT = ["None", "Cloudy"];
39127
- PDFAnnotation.INTENT = [
39128
- null,
39129
- "FreeTextCallout",
39130
- "FreeTextTypeWriter",
39131
- "LineArrow",
39132
- "LineDimension",
39133
- "PloyLine",
39134
- "PolygonCloud",
39135
- "PolygonDimension",
39136
- "StampImage",
39137
- "StampSnapshot"
39138
- ];
39139
- // Bit masks for getFlags and setFlags
39140
- PDFAnnotation.IS_INVISIBLE = 1 << (1 - 1);
39141
- PDFAnnotation.IS_HIDDEN = 1 << (2 - 1);
39142
- PDFAnnotation.IS_PRINT = 1 << (3 - 1);
39143
- PDFAnnotation.IS_NO_ZOOM = 1 << (4 - 1);
39144
- PDFAnnotation.IS_NO_ROTATE = 1 << (5 - 1);
39145
- PDFAnnotation.IS_NO_VIEW = 1 << (6 - 1);
39146
- PDFAnnotation.IS_READ_ONLY = 1 << (7 - 1);
39147
- PDFAnnotation.IS_LOCKED = 1 << (8 - 1);
39148
- PDFAnnotation.IS_TOGGLE_NO_VIEW = 1 << (9 - 1);
39149
- PDFAnnotation.IS_LOCKED_CONTENTS = 1 << (10 - 1);
39150
- class PDFWidget extends PDFAnnotation {
39151
- getFieldType() {
39152
- return PDFWidget.WIDGET_TYPES[libmupdf._wasm_pdf_annot_field_type(this.pointer)] || "button";
39153
- }
39154
- isButton() {
39155
- let type = this.getFieldType();
39156
- return type === "button" || type === "checkbox" || type === "radiobutton";
39157
- }
39158
- isPushButton() {
39159
- return this.getFieldType() === "button";
39160
- }
39161
- isCheckbox() {
39162
- return this.getFieldType() === "checkbox";
39163
- }
39164
- isRadioButton() {
39165
- return this.getFieldType() === "radiobutton";
39166
- }
39167
- isText() {
39168
- return this.getFieldType() === "text";
39169
- }
39170
- isChoice() {
39171
- let type = this.getFieldType();
39172
- return type === "combobox" || type === "listbox";
39173
- }
39174
- isListBox() {
39175
- return this.getFieldType() === "listbox";
39176
- }
39177
- isComboBox() {
39178
- return this.getFieldType() === "combobox";
39179
- }
39180
- getFieldFlags() {
39181
- return libmupdf._wasm_pdf_annot_field_flags(this.pointer);
39182
- }
39183
- isMultiline() {
39184
- return (this.getFieldFlags() & PDFWidget.TX_FIELD_IS_MULTILINE) !== 0;
39185
- }
39186
- isPassword() {
39187
- return (this.getFieldFlags() & PDFWidget.TX_FIELD_IS_PASSWORD) !== 0;
39188
- }
39189
- isComb() {
39190
- return (this.getFieldFlags() & PDFWidget.TX_FIELD_IS_COMB) !== 0;
39191
- }
39192
- isReadOnly() {
39193
- return (this.getFieldFlags() & PDFWidget.FIELD_IS_READ_ONLY) !== 0;
39194
- }
39195
- getLabel() {
39196
- return fromString(libmupdf._wasm_pdf_annot_field_label(this.pointer));
39197
- }
39198
- getName() {
39199
- return fromStringFree(libmupdf._wasm_pdf_load_field_name(this.pointer));
39200
- }
39201
- getValue() {
39202
- return fromString(libmupdf._wasm_pdf_annot_field_value(this.pointer));
39203
- }
39204
- setTextValue(value) {
39205
- return libmupdf._wasm_pdf_set_annot_text_field_value(this.pointer, STRING(value));
39206
- }
39207
- getMaxLen() {
39208
- return libmupdf._wasm_pdf_annot_text_widget_max_len(this.pointer);
39209
- }
39210
- setChoiceValue(value) {
39211
- return libmupdf._wasm_pdf_set_annot_choice_field_value(this.pointer, STRING(value));
39212
- }
39213
- getOptions(isExport = false) {
39214
- let result = [];
39215
- let n = libmupdf._wasm_pdf_annot_choice_field_option_count(this.pointer);
39216
- for (let i = 0; i < n; ++i) {
39217
- result.push(fromString(libmupdf._wasm_pdf_annot_choice_field_option(this.pointer, isExport, i)));
39218
- }
39219
- return result;
39220
- }
39221
- toggle() {
39222
- return libmupdf._wasm_pdf_toggle_widget(this.pointer);
39223
- }
39224
- }
39225
- /* IMPORTANT: Keep in sync with mupdf/pdf/widget.h and PDFWidget.java */
39226
- PDFWidget.WIDGET_TYPES = [
39227
- "widget", // unknown
39228
- "button",
39229
- "checkbox",
39230
- "combobox",
39231
- "listbox",
39232
- "radiobutton",
39233
- "signature",
39234
- "text",
39235
- ];
39236
- /* Field flags */
39237
- PDFWidget.FIELD_IS_READ_ONLY = 1;
39238
- PDFWidget.FIELD_IS_REQUIRED = 1 << 1;
39239
- PDFWidget.FIELD_IS_NO_EXPORT = 1 << 2;
39240
- /* Text fields */
39241
- PDFWidget.TX_FIELD_IS_MULTILINE = 1 << 12;
39242
- PDFWidget.TX_FIELD_IS_PASSWORD = 1 << 13;
39243
- PDFWidget.TX_FIELD_IS_COMB = 1 << 24;
39244
- /* Button fields */
39245
- PDFWidget.BTN_FIELD_IS_NO_TOGGLE_TO_OFF = 1 << 14;
39246
- PDFWidget.BTN_FIELD_IS_RADIO = 1 << 15;
39247
- PDFWidget.BTN_FIELD_IS_PUSHBUTTON = 1 << 16;
39248
- /* Choice fields */
39249
- PDFWidget.CH_FIELD_IS_COMBO = 1 << 17;
39250
- PDFWidget.CH_FIELD_IS_EDIT = 1 << 18;
39251
- PDFWidget.CH_FIELD_IS_SORT = 1 << 19;
39252
- PDFWidget.CH_FIELD_IS_MULTI_SELECT = 1 << 21;
39253
- var $libmupdf_stm_id = 0;
39254
- var $libmupdf_stm_table = new Map();
39255
- globalThis.$libmupdf_stm_close = function (id) {
39256
- let handle = $libmupdf_stm_table.get(id);
39257
- if (handle) {
39258
- handle.close();
39259
- $libmupdf_stm_table.delete(id);
39260
- return;
39261
- }
39262
- throw new Error("invalid file handle");
39263
- };
39264
- globalThis.$libmupdf_stm_seek = function (id, pos, offset, whence) {
39265
- let handle = $libmupdf_stm_table.get(id);
39266
- if (handle) {
39267
- if (whence === 0)
39268
- return offset;
39269
- if (whence === 1)
39270
- return pos + offset;
39271
- if (whence === 2) {
39272
- let size = handle.fileSize();
39273
- if (size < 0)
39274
- return -1;
39275
- return size + offset;
39276
- }
39277
- throw new Error("invalid whence argument");
39278
- }
39279
- throw new Error("invalid file handle");
39280
- };
39281
- globalThis.$libmupdf_stm_read = function (id, pos, addr, size) {
39282
- let handle = $libmupdf_stm_table.get(id);
39283
- if (handle) {
39284
- return handle.read(libmupdf.HEAPU8, addr, size, pos);
39285
- }
39286
- throw new Error("invalid file handle");
39287
- };
39288
- class Stream extends Userdata {
39289
- constructor(handle) {
39290
- let id = $libmupdf_stm_id++;
39291
- $libmupdf_stm_table.set(id, handle);
39292
- super(libmupdf._wasm_new_stream(id));
39293
- }
39294
- }
39295
- Stream._drop = libmupdf._wasm_drop_stream;
39296
- globalThis.$libmupdf_load_font_file = function (name, script, bold, italic) {
39297
- return 0;
39298
- };
39299
- var $libmupdf_device_id = 0;
39300
- var $libmupdf_device_table = new Map();
39301
- var $libmupdf_path_id = 0;
39302
- var $libmupdf_path_table = new Map();
39303
- var $libmupdf_text_id = 0;
39304
- var $libmupdf_text_table = new Map();
39305
- globalThis.$libmupdf_path_walk = {
39306
- moveto(id, x, y) {
39307
- $libmupdf_path_table.get(id)?.moveTo?.(x, y);
39308
- },
39309
- lineto(id, x, y) {
39310
- $libmupdf_path_table.get(id)?.lineTo?.(x, y);
39311
- },
39312
- curveto(id, x1, y1, x2, y2, x3, y3) {
39313
- $libmupdf_path_table.get(id)?.curveTo?.(x1, y1, x2, y2, x3, y3);
39314
- },
39315
- closepath(id) {
39316
- $libmupdf_path_table.get(id)?.closePath?.();
39317
- },
39318
- };
39319
- var $libmupdf_text_font = null;
39320
- globalThis.$libmupdf_text_walk = {
39321
- begin_span(id, font, trm, wmode, bidi, dir, lang) {
39322
- if (font !== $libmupdf_text_font?.pointer)
39323
- $libmupdf_text_font = new Font(font);
39324
- $libmupdf_text_table.get(id)?.beginSpan?.($libmupdf_text_font, fromMatrix(trm), wmode, bidi, dir, fromString(lang));
39325
- },
39326
- end_span(id) {
39327
- $libmupdf_text_table.get(id)?.endSpan?.();
39328
- },
39329
- show_glyph(id, font, trm, glyph, unicode, wmode, bidi) {
39330
- if (font !== $libmupdf_text_font?.pointer)
39331
- $libmupdf_text_font = new Font(font);
39332
- $libmupdf_text_table.get(id)?.showGlyph?.($libmupdf_text_font, fromMatrix(trm), glyph, unicode, wmode, bidi);
39333
- },
39334
- };
39335
- globalThis.$libmupdf_device = {
39336
- drop_device(id) {
39337
- $libmupdf_device_table.get(id)?.drop?.();
39338
- $libmupdf_device_table.delete(id);
39339
- },
39340
- close_device(id) {
39341
- $libmupdf_device_table.get(id)?.close?.();
39342
- },
39343
- fill_path(id, path, even_odd, ctm, colorspace, color_n, color_arr, alpha) {
39344
- $libmupdf_device_table.get(id)?.fillPath?.(new Path(path), !!even_odd, fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39345
- },
39346
- clip_path(id, path, even_odd, ctm) {
39347
- $libmupdf_device_table.get(id)?.clipPath?.(new Path(path), !!even_odd, fromMatrix(ctm));
39348
- },
39349
- stroke_path(id, path, stroke, ctm, colorspace, color_n, color_arr, alpha) {
39350
- $libmupdf_device_table.get(id)?.strokePath?.(new Path(path), new StrokeState(stroke), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39351
- },
39352
- clip_stroke_path(id, path, stroke, ctm) {
39353
- $libmupdf_device_table.get(id)?.clipStrokePath?.(new Path(path), new StrokeState(stroke), fromMatrix(ctm));
39354
- },
39355
- fill_text(id, text, ctm, colorspace, color_n, color_arr, alpha) {
39356
- $libmupdf_device_table.get(id)?.fillText?.(new Text(text), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39357
- },
39358
- stroke_text(id, text, stroke, ctm, colorspace, color_n, color_arr, alpha) {
39359
- $libmupdf_device_table.get(id)?.strokeText?.(new Text(text), new StrokeState(stroke), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39360
- },
39361
- clip_text(id, text, ctm) {
39362
- $libmupdf_device_table.get(id)?.clipText?.(new Text(text), fromMatrix(ctm));
39363
- },
39364
- clip_stroke_text(id, text, stroke, ctm) {
39365
- $libmupdf_device_table.get(id)?.clipStrokeText?.(new Text(text), new StrokeState(stroke), fromMatrix(ctm));
39366
- },
39367
- ignore_text(id, text, ctm) {
39368
- $libmupdf_device_table.get(id)?.ignoreText?.(new Text(text), fromMatrix(ctm));
39369
- },
39370
- fill_shade(id, shade, ctm, alpha) {
39371
- $libmupdf_device_table.get(id)?.fillShade?.(new Shade(shade), fromMatrix(ctm), alpha);
39372
- },
39373
- fill_image(id, image, ctm, alpha) {
39374
- $libmupdf_device_table.get(id)?.fillImage?.(new Image(image), fromMatrix(ctm), alpha);
39375
- },
39376
- fill_image_mask(id, image, ctm, colorspace, color_n, color_arr, alpha) {
39377
- $libmupdf_device_table.get(id)?.fillImageMask?.(new Image(image), fromMatrix(ctm), new ColorSpace(colorspace), fromColorArray(color_n, color_arr), alpha);
39378
- },
39379
- clip_image_mask(id, image, ctm) {
39380
- $libmupdf_device_table.get(id)?.clipImageMask?.(new Image(image), fromMatrix(ctm));
39381
- },
39382
- pop_clip(id) {
39383
- $libmupdf_device_table.get(id)?.popClip?.();
39384
- },
39385
- begin_mask(id, bbox, luminosity, colorspace, color_n, color_arr) {
39386
- $libmupdf_device_table.get(id)?.beginMask?.(fromRect(bbox), !!luminosity, new ColorSpace(colorspace), fromColorArray(color_n, color_arr));
39387
- },
39388
- begin_group(id, bbox, colorspace, isolated, knockout, blendmode, alpha) {
39389
- $libmupdf_device_table.get(id)?.beginGroup?.(fromRect(bbox), new ColorSpace(colorspace), !!isolated, !!knockout, Device.BLEND_MODES[blendmode], alpha);
39390
- },
39391
- begin_tile(id, area, view, xstep, ystep, ctm, tile_id) {
39392
- return $libmupdf_device_table.get(id)?.beginTile?.(fromRect(area), fromRect(view), xstep, ystep, fromMatrix(ctm), tile_id) || 0;
39393
- },
39394
- begin_layer(id, name) {
39395
- $libmupdf_device_table.get(id)?.beginLayer?.(fromString(name));
39396
- },
39397
- end_mask(id) {
39398
- $libmupdf_device_table.get(id)?.endMask?.();
39399
- },
39400
- end_group(id) {
39401
- $libmupdf_device_table.get(id)?.endGroup?.();
39402
- },
39403
- end_tile(id) {
39404
- $libmupdf_device_table.get(id)?.endTile?.();
39405
- },
39406
- end_layer(id) {
39407
- $libmupdf_device_table.get(id)?.endLayer?.();
39408
- },
39409
- };
39410
-
39411
- /*
39412
- * Copyright (C) 2025 Koutaro Mukai
39413
- *
39414
- * This program is free software: you can redistribute it and/or modify
39415
- * it under the terms of the GNU General Public License as published by
39416
- * the Free Software Foundation, either version 3 of the License, or
39417
- * (at your option) any later version.
39418
- *
39419
- * This program is distributed in the hope that it will be useful,
39420
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39421
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39422
- * GNU General Public License for more details.
39423
- *
39424
- * You should have received a copy of the GNU General Public License
39425
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39426
- */
39427
36434
  const _enabled = (() => {
39428
36435
  try {
39429
36436
  if (typeof process !== "undefined" &&
@@ -39484,22 +36491,6 @@ const _noopPerf = {
39484
36491
  };
39485
36492
  const perf = _enabled ? _realPerf : _noopPerf;
39486
36493
 
39487
- /*
39488
- * Copyright (C) 2025 Koutaro Mukai
39489
- *
39490
- * This program is free software: you can redistribute it and/or modify
39491
- * it under the terms of the GNU General Public License as published by
39492
- * the Free Software Foundation, either version 3 of the License, or
39493
- * (at your option) any later version.
39494
- *
39495
- * This program is distributed in the hope that it will be useful,
39496
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39497
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39498
- * GNU General Public License for more details.
39499
- *
39500
- * You should have received a copy of the GNU General Public License
39501
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39502
- */
39503
36494
  const alignStrategyValues = new Set([
39504
36495
  "resize",
39505
36496
  "top-left",
@@ -39514,22 +36505,6 @@ const alignStrategyValues = new Set([
39514
36505
  ]);
39515
36506
  const isValidAlignStrategy = (str) => alignStrategyValues.has(str);
39516
36507
 
39517
- /*
39518
- * Copyright (C) 2025 Koutaro Mukai
39519
- *
39520
- * This program is free software: you can redistribute it and/or modify
39521
- * it under the terms of the GNU General Public License as published by
39522
- * the Free Software Foundation, either version 3 of the License, or
39523
- * (at your option) any later version.
39524
- *
39525
- * This program is distributed in the hope that it will be useful,
39526
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39527
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39528
- * GNU General Public License for more details.
39529
- *
39530
- * You should have received a copy of the GNU General Public License
39531
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39532
- */
39533
36508
  const parseHex = (hex) => {
39534
36509
  if (/^#([0-9a-fA-F]{3})$/.test(hex)) {
39535
36510
  return [
@@ -39573,22 +36548,6 @@ const formatHex = ([r, g, b, a]) => "#" +
39573
36548
  })
39574
36549
  .join("");
39575
36550
 
39576
- /*
39577
- * Copyright (C) 2025 Koutaro Mukai
39578
- *
39579
- * This program is free software: you can redistribute it and/or modify
39580
- * it under the terms of the GNU General Public License as published by
39581
- * the Free Software Foundation, either version 3 of the License, or
39582
- * (at your option) any later version.
39583
- *
39584
- * This program is distributed in the hope that it will be useful,
39585
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39586
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39587
- * GNU General Public License for more details.
39588
- *
39589
- * You should have received a copy of the GNU General Public License
39590
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39591
- */
39592
36551
  async function* withIndex(iter, start = 0) {
39593
36552
  let index = start;
39594
36553
  for await (const item of iter) {
@@ -39597,24 +36556,122 @@ async function* withIndex(iter, start = 0) {
39597
36556
  }
39598
36557
  }
39599
36558
 
39600
- const VERSION = "0.2.2";
39601
-
39602
36559
  /*
39603
- * Copyright (C) 2025 Koutaro Mukai
39604
- *
39605
- * This program is free software: you can redistribute it and/or modify
39606
- * it under the terms of the GNU General Public License as published by
39607
- * the Free Software Foundation, either version 3 of the License, or
39608
- * (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).
39609
36564
  *
39610
- * This program is distributed in the hope that it will be useful,
39611
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39612
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39613
- * GNU General Public License for more details.
39614
- *
39615
- * You should have received a copy of the GNU General Public License
39616
- * 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.
39617
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.
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));
39618
36675
  const defaultOptions = {
39619
36676
  dpi: 150,
39620
36677
  alpha: true,
@@ -39625,21 +36682,8 @@ const defaultOptions = {
39625
36682
  deletion: [0xff, 0x57, 0x24, 0xff],
39626
36683
  modification: [0xff, 0xc1, 0x05, 0xff],
39627
36684
  },
39628
- workers: 1,
36685
+ workers: defaultWorkers,
39629
36686
  };
39630
- function asSharedBytes(bytes) {
39631
- const isNode = typeof globalThis.process !== "undefined" &&
39632
- !!globalThis.process.versions?.node;
39633
- const coiOk = globalThis.crossOriginIsolated ===
39634
- true;
39635
- if (typeof SharedArrayBuffer !== "undefined" && (isNode || coiOk)) {
39636
- const sab = new SharedArrayBuffer(bytes.byteLength);
39637
- const view = new Uint8Array(sab);
39638
- view.set(bytes);
39639
- return view;
39640
- }
39641
- return new Uint8Array(bytes);
39642
- }
39643
36687
  class WorkerHandle {
39644
36688
  worker;
39645
36689
  loaded;
@@ -39684,12 +36728,22 @@ class WorkerHandle {
39684
36728
  this.worker.postMessage(msg);
39685
36729
  });
39686
36730
  }
39687
- processPage(index) {
36731
+ processDiff(index, a, b, mask) {
39688
36732
  return new Promise((resolve, reject) => {
39689
36733
  this.pendingResolve = resolve;
39690
36734
  this.pendingReject = reject;
39691
- const msg = { type: "page", index };
39692
- 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);
39693
36747
  });
39694
36748
  }
39695
36749
  terminate() {
@@ -39700,6 +36754,14 @@ function workerUrl() {
39700
36754
  const file = import.meta.url.endsWith(".ts") ? "./worker.ts" : "./worker.js";
39701
36755
  return new URL(`${file}?v=${encodeURIComponent(VERSION)}`, import.meta.url);
39702
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
+ }
39703
36765
  function pageResultToResult(msg) {
39704
36766
  const sP = perf.span("main.pageResultToResult_ms");
39705
36767
  const r = {
@@ -39718,9 +36780,9 @@ function pageResultToResult(msg) {
39718
36780
  height: msg.diff.height,
39719
36781
  data: new Uint8Array(msg.diff.data),
39720
36782
  }),
39721
- addition: msg.addition,
39722
- deletion: msg.deletion,
39723
- modification: msg.modification,
36783
+ addition: unpackCoords(msg.addition),
36784
+ deletion: unpackCoords(msg.deletion),
36785
+ modification: unpackCoords(msg.modification),
39724
36786
  };
39725
36787
  sP.stop();
39726
36788
  perf.incr("main.resultsReceived");
@@ -39741,49 +36803,111 @@ async function* visualizeDifferences(a, b, options) {
39741
36803
  },
39742
36804
  workers: options?.workers ?? defaultOptions.workers,
39743
36805
  };
39744
- const probe = PDFDocument.openDocument(a, "application/pdf");
39745
- const probeB = PDFDocument.openDocument(b, "application/pdf");
39746
- const probeMask = typeof merged.mask !== "undefined"
39747
- ? PDFDocument.openDocument(merged.mask, "application/pdf")
39748
- : new PDFDocument();
39749
- const maxPages = Math.max(probe.countPages(), probeB.countPages(), probeMask.countPages());
39750
- probe.destroy();
39751
- probeB.destroy();
39752
- 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);
39753
36814
  if (maxPages === 0)
39754
36815
  return;
39755
- const aBytes = asSharedBytes(a);
39756
- const bBytes = asSharedBytes(b);
39757
- 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;
39758
36819
  const initMsg = {
39759
36820
  type: "init",
39760
- aBytes,
39761
- bBytes,
39762
- maskBytes,
39763
- dpi: merged.dpi,
39764
- alpha: merged.alpha,
39765
36821
  pallet: merged.pallet,
39766
36822
  align: merged.align,
39767
36823
  };
39768
36824
  const N = Math.max(1, Math.min(merged.workers, maxPages));
39769
36825
  const url = workerUrl();
39770
- const worker0 = new WorkerHandle(url);
39771
- await worker0.init(initMsg);
39772
- const buffered = new Map();
39773
- let nextToAssign = 0;
39774
- const workers = [worker0];
39775
- for (let i = 1; i < N; i++) {
36826
+ const workers = [];
36827
+ for (let i = 0; i < N; i++) {
39776
36828
  const w = new WorkerHandle(url);
39777
36829
  await w.init(initMsg);
39778
36830
  workers.push(w);
39779
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;
39780
36899
  const resolvers = new Map();
39781
36900
  let workerError = null;
39782
- const loops = workers.map(async (w) => {
36901
+ const diffLoops = workers.map(async (w) => {
39783
36902
  while (nextToAssign < maxPages && workerError === null) {
39784
36903
  const idx = nextToAssign++;
39785
36904
  try {
39786
- 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);
39787
36911
  const result = pageResultToResult(msg);
39788
36912
  const resolve = resolvers.get(idx);
39789
36913
  if (resolve) {
@@ -39797,6 +36921,7 @@ async function* visualizeDifferences(a, b, options) {
39797
36921
  }
39798
36922
  catch (e) {
39799
36923
  workerError = e;
36924
+ aborted = e;
39800
36925
  for (const [, resolve] of resolvers)
39801
36926
  resolve(null);
39802
36927
  resolvers.clear();
@@ -39825,55 +36950,17 @@ async function* visualizeDifferences(a, b, options) {
39825
36950
  yield r;
39826
36951
  sYield.stop();
39827
36952
  }
39828
- await Promise.all(loops);
36953
+ await Promise.all(diffLoops);
36954
+ await Promise.all(renderLoops);
39829
36955
  }
39830
36956
  finally {
36957
+ aborted = aborted ?? new Error("aborted");
36958
+ await Promise.allSettled(renderLoops);
39831
36959
  for (const w of workers)
39832
36960
  w.terminate();
39833
36961
  }
39834
36962
  }
39835
36963
 
39836
- /*
39837
- * Copyright (C) 2025 Koutaro Mukai
39838
- *
39839
- * This program is free software: you can redistribute it and/or modify
39840
- * it under the terms of the GNU General Public License as published by
39841
- * the Free Software Foundation, either version 3 of the License, or
39842
- * (at your option) any later version.
39843
- *
39844
- * This program is distributed in the hope that it will be useful,
39845
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39846
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39847
- * GNU General Public License for more details.
39848
- *
39849
- * You should have received a copy of the GNU General Public License
39850
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39851
- */
39852
- /**
39853
- * Slice a typed-array view into a standalone backing buffer of the same kind
39854
- * (ArrayBuffer in, ArrayBuffer out; SharedArrayBuffer in, SharedArrayBuffer
39855
- * out). The buffer kind is preserved through the generic parameter.
39856
- */
39857
- function sliceBackingBuffer(src) {
39858
- return src.buffer.slice(src.byteOffset, src.byteOffset + src.byteLength);
39859
- }
39860
-
39861
- /*
39862
- * Copyright (C) 2025 Koutaro Mukai
39863
- *
39864
- * This program is free software: you can redistribute it and/or modify
39865
- * it under the terms of the GNU General Public License as published by
39866
- * the Free Software Foundation, either version 3 of the License, or
39867
- * (at your option) any later version.
39868
- *
39869
- * This program is distributed in the hope that it will be useful,
39870
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
39871
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39872
- * GNU General Public License for more details.
39873
- *
39874
- * You should have received a copy of the GNU General Public License
39875
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
39876
- */
39877
36964
  class PngWriterPool {
39878
36965
  workers = [];
39879
36966
  idle = [];
@@ -39960,12 +37047,15 @@ OPTIONS:
39960
37047
  -h, --help
39961
37048
 
39962
37049
  NOTES:
39963
- Approximate per-worker memory:
39964
- a_size_MB + b_size_MB [+ mask_size_MB] (PDF buffers in wasm)
39965
- + 300 MB (mupdf + V8 base)
39966
- + (dpi / 150)^2 * 50 MB (pixmap working set)
39967
- The main process adds ~500 MB - 1 GB (varies with --workers).
39968
- 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.
39969
37059
  `);
39970
37060
  process.exit(0);
39971
37061
  }