@embedpdf/engines 1.0.10 → 1.0.12
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.
- package/dist/engine-B7CS6Qyp.cjs +2 -0
- package/dist/engine-B7CS6Qyp.cjs.map +1 -0
- package/dist/engine-M0_XZhss.js +5043 -0
- package/dist/engine-M0_XZhss.js.map +1 -0
- package/dist/index.cjs +1 -5846
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1613
- package/dist/index.js +339 -5824
- package/dist/index.js.map +1 -1
- package/dist/lib/converters/index.cjs +2 -0
- package/dist/lib/converters/index.cjs.map +1 -0
- package/dist/{converters.d.ts → lib/converters/index.d.ts} +7 -10
- package/dist/lib/converters/index.js +80 -0
- package/dist/{converters.cjs.map → lib/converters/index.js.map} +1 -1
- package/dist/lib/index.d.ts +7 -0
- package/dist/lib/mock/index.d.ts +25 -0
- package/dist/lib/pdfium/cache.d.ts +62 -0
- package/dist/{pdfium-direct-engine.d.ts → lib/pdfium/engine.d.ts} +73 -8
- package/dist/lib/pdfium/helper.d.ts +21 -0
- package/dist/lib/pdfium/index.cjs +2 -0
- package/dist/lib/pdfium/index.cjs.map +1 -0
- package/dist/lib/pdfium/index.d.ts +3 -0
- package/dist/lib/pdfium/index.js +13 -0
- package/dist/lib/pdfium/index.js.map +1 -0
- package/dist/lib/pdfium/runner.d.ts +16 -0
- package/dist/lib/pdfium/web/direct-engine.cjs +2 -0
- package/dist/lib/pdfium/web/direct-engine.cjs.map +1 -0
- package/dist/lib/pdfium/web/direct-engine.d.ts +3 -0
- package/dist/lib/pdfium/web/direct-engine.js +12 -0
- package/dist/lib/pdfium/web/direct-engine.js.map +1 -0
- package/dist/lib/pdfium/web/worker-engine.cjs +2 -0
- package/dist/lib/pdfium/web/worker-engine.cjs.map +1 -0
- package/dist/lib/pdfium/web/worker-engine.d.ts +7 -0
- package/dist/lib/pdfium/web/worker-engine.js +15 -0
- package/dist/lib/pdfium/web/worker-engine.js.map +1 -0
- package/dist/lib/pdfium/worker.d.ts +1 -0
- package/dist/lib/pdfium-engine.d.ts +1 -0
- package/dist/lib/webworker/engine.cjs +2 -0
- package/dist/lib/webworker/engine.cjs.map +1 -0
- package/dist/{worker.d.ts → lib/webworker/engine.d.ts} +4 -34
- package/dist/lib/webworker/engine.js +887 -0
- package/dist/lib/webworker/engine.js.map +1 -0
- package/dist/lib/webworker/index.d.ts +2 -0
- package/dist/lib/webworker/runner.d.ts +128 -0
- package/dist/lib/webworker-engine.d.ts +1 -0
- package/dist/preact/adapter.d.ts +1 -0
- package/dist/preact/index.cjs +2 -0
- package/dist/preact/index.cjs.map +1 -0
- package/dist/preact/index.d.ts +1 -0
- package/dist/preact/index.js +37 -0
- package/dist/preact/index.js.map +1 -0
- package/dist/react/adapter.d.ts +1 -0
- package/dist/react/index.cjs +2 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +37 -0
- package/dist/react/index.js.map +1 -0
- package/dist/runner-BcS-WEof.cjs +2 -0
- package/dist/runner-BcS-WEof.cjs.map +1 -0
- package/dist/runner-DUp_7Uu_.js +269 -0
- package/dist/runner-DUp_7Uu_.js.map +1 -0
- package/dist/shared-preact/hooks/index.d.ts +1 -0
- package/dist/{preact.d.ts → shared-preact/hooks/use-pdfium-engine.d.ts} +3 -5
- package/dist/shared-preact/index.d.ts +1 -0
- package/dist/shared-react/hooks/index.d.ts +1 -0
- package/dist/{react.d.ts → shared-react/hooks/use-pdfium-engine.d.ts} +3 -5
- package/dist/shared-react/index.d.ts +1 -0
- package/dist/vue/hooks/index.d.ts +1 -0
- package/dist/vue/hooks/use-pdfium-engine.d.ts +18 -0
- package/dist/vue/index.cjs +2 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.ts +1 -0
- package/dist/vue/index.js +39 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +41 -41
- package/dist/converters.cjs +0 -139
- package/dist/converters.js +0 -133
- package/dist/converters.js.map +0 -1
- package/dist/pdfium-direct-engine.cjs +0 -4400
- package/dist/pdfium-direct-engine.cjs.map +0 -1
- package/dist/pdfium-direct-engine.js +0 -4398
- package/dist/pdfium-direct-engine.js.map +0 -1
- package/dist/pdfium-worker-engine.cjs +0 -851
- package/dist/pdfium-worker-engine.cjs.map +0 -1
- package/dist/pdfium-worker-engine.d.ts +0 -314
- package/dist/pdfium-worker-engine.js +0 -849
- package/dist/pdfium-worker-engine.js.map +0 -1
- package/dist/pdfium.cjs +0 -4667
- package/dist/pdfium.cjs.map +0 -1
- package/dist/pdfium.d.ts +0 -1311
- package/dist/pdfium.js +0 -4661
- package/dist/pdfium.js.map +0 -1
- package/dist/preact.cjs +0 -41
- package/dist/preact.cjs.map +0 -1
- package/dist/preact.js +0 -39
- package/dist/preact.js.map +0 -1
- package/dist/react.cjs +0 -41
- package/dist/react.cjs.map +0 -1
- package/dist/react.js +0 -39
- package/dist/react.js.map +0 -1
- package/dist/worker.cjs +0 -839
- package/dist/worker.cjs.map +0 -1
- package/dist/worker.js +0 -836
- package/dist/worker.js.map +0 -1
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";const e=require("@embedpdf/models");function t(e,t,o,i=100){let n=e.wasmExports.malloc(i);for(let s=0;s<i;s++)e.HEAP8[n+s]=0;const r=t(n,i);let d;if(r>i){e.wasmExports.free(n),n=e.wasmExports.malloc(r);for(let t=0;t<r;t++)e.HEAP8[n+t]=0;t(n,r),d=o(n)}else d=o(n);return e.wasmExports.free(n),d}function o(e,t){const o=t(0,0),i=e.wasmExports.malloc(o);t(i,o);const n=new ArrayBuffer(o),r=new DataView(n);for(let d=0;d<o;d++)r.setInt8(d,e.getValue(i+d,"i8"));return e.wasmExports.free(i),n}class i{constructor(e){this.pdfium=e,this.docs=new Map}setDocument(e,t,o){let i=this.docs.get(e);i||(i=new n(t,o,this.pdfium),this.docs.set(e,i))}getContext(e){return this.docs.get(e)}closeDocument(e){const t=this.docs.get(e);return!!t&&(t.dispose(),this.docs.delete(e),!0)}}class n{constructor(e,t,o){this.filePtr=e,this.docPtr=t,this.pageCache=new r(o,t)}acquirePage(e){return this.pageCache.acquire(e)}dispose(){this.pageCache.forceReleaseAll(),this.pageCache.pdf.FPDF_CloseDocument(this.docPtr),this.pageCache.pdf.pdfium.wasmExports.free(this.filePtr)}}class r{constructor(e,t){this.pdf=e,this.docPtr=t,this.cache=new Map}acquire(e){let t=this.cache.get(e);if(!t){const o=this.pdf.FPDF_LoadPage(this.docPtr,e);t=new d(this.pdf,this.docPtr,e,o,(()=>{this.cache.delete(e)})),this.cache.set(e,t)}return t.clearExpiryTimer(),t.bumpRefCount(),t}forceReleaseAll(){for(const e of this.cache.values())e.disposeImmediate();this.cache.clear()}}class d{constructor(e,t,o,i,n){this.pdf=e,this.docPtr=t,this.pageIdx=o,this.pagePtr=i,this.onFinalDispose=n,this.refCount=0,this.disposed=!1}bumpRefCount(){if(this.disposed)throw new Error("Context already disposed");this.refCount++}clearExpiryTimer(){this.expiryTimer&&(clearTimeout(this.expiryTimer),this.expiryTimer=void 0)}release(){this.disposed||(this.refCount--,0===this.refCount&&(this.expiryTimer=setTimeout((()=>this.disposeImmediate()),5e3)))}disposeImmediate(){this.disposed||(this.disposed=!0,void 0!==this.textPagePtr&&this.pdf.FPDFText_ClosePage(this.textPagePtr),void 0!==this.formHandle&&(this.pdf.FORM_OnBeforeClosePage(this.pagePtr,this.formHandle),this.pdf.PDFiumExt_ExitFormFillEnvironment(this.formHandle)),void 0!==this.formInfoPtr&&this.pdf.PDFiumExt_CloseFormFillInfo(this.formInfoPtr),this.pdf.FPDF_ClosePage(this.pagePtr),this.onFinalDispose())}getTextPage(){return this.ensureAlive(),void 0===this.textPagePtr&&(this.textPagePtr=this.pdf.FPDFText_LoadPage(this.pagePtr)),this.textPagePtr}getFormHandle(){return this.ensureAlive(),void 0===this.formHandle&&(this.formInfoPtr=this.pdf.PDFiumExt_OpenFormFillInfo(),this.formHandle=this.pdf.PDFiumExt_InitFormFillEnvironment(this.docPtr,this.formInfoPtr),this.pdf.FORM_OnAfterLoadPage(this.pagePtr,this.formHandle)),this.formHandle}withAnnotation(e,t){this.ensureAlive();const o=this.pdf.FPDFPage_GetAnnot(this.pagePtr,e);try{return t(o)}finally{this.pdf.FPDFPage_CloseAnnot(o)}}ensureAlive(){if(this.disposed)throw new Error("PageContext already disposed")}}var s=(e=>(e[e.Bitmap_Gray=1]="Bitmap_Gray",e[e.Bitmap_BGR=2]="Bitmap_BGR",e[e.Bitmap_BGRx=3]="Bitmap_BGRx",e[e.Bitmap_BGRA=4]="Bitmap_BGRA",e))(s||{}),a=(e=>(e[e.ANNOT=1]="ANNOT",e[e.LCD_TEXT=2]="LCD_TEXT",e[e.NO_NATIVETEXT=4]="NO_NATIVETEXT",e[e.GRAYSCALE=8]="GRAYSCALE",e[e.DEBUG_INFO=128]="DEBUG_INFO",e[e.NO_CATCH=256]="NO_CATCH",e[e.RENDER_LIMITEDIMAGECACHE=512]="RENDER_LIMITEDIMAGECACHE",e[e.RENDER_FORCEHALFTONE=1024]="RENDER_FORCEHALFTONE",e[e.PRINTING=2048]="PRINTING",e[e.REVERSE_BYTE_ORDER=16]="REVERSE_BYTE_ORDER",e))(a||{});const u="PDFiumEngine",l="Engine";var h=(e=>(e[e.Success=0]="Success",e[e.Unknown=1]="Unknown",e[e.File=2]="File",e[e.Format=3]="Format",e[e.Password=4]="Password",e[e.Security=5]="Security",e[e.Page=6]="Page",e[e.XFALoad=7]="XFALoad",e[e.XFALayout=8]="XFALayout",e))(h||{});const f=(e,t="image/webp")=>{if("undefined"==typeof OffscreenCanvas)throw new Error("OffscreenCanvas is not available in this environment. This converter is intended for browser use only. Please use createNodeImageDataToBlobConverter() or createNodeCanvasImageDataToBlobConverter() for Node.js.");const o=new ImageData(e.data,e.width,e.height),i=new OffscreenCanvas(o.width,o.height);return i.getContext("2d").putImageData(o,0,0),i.convertToBlob({type:t})};exports.BitmapFormat=s,exports.PdfiumEngine=class{constructor(t,o=new e.NoopLogger,n=f){this.pdfiumModule=t,this.logger=o,this.imageDataConverter=n,this.cache=new i(this.pdfiumModule)}initialize(){return this.logger.debug(u,l,"initialize"),this.logger.perf(u,l,"Initialize","Begin","General"),this.pdfiumModule.PDFiumExt_Init(),this.logger.perf(u,l,"Initialize","End","General"),e.PdfTaskHelper.resolve(!0)}destroy(){return this.logger.debug(u,l,"destroy"),this.logger.perf(u,l,"Destroy","Begin","General"),this.pdfiumModule.FPDF_DestroyLibrary(),this.logger.perf(u,l,"Destroy","End","General"),e.PdfTaskHelper.resolve(!0)}openDocumentUrl(t,o){const i=(null==o?void 0:o.mode)??"auto",n=(null==o?void 0:o.password)??"";this.logger.debug(u,l,"openDocumentUrl called",t.url,i);const r=e.PdfTaskHelper.create();return(async()=>{try{if("full-fetch"===i){(await this.fetchFullAndOpen(t,n)).wait((e=>r.resolve(e)),(e=>r.reject(e.reason)))}else if("range-request"===i){(await this.openDocumentWithRangeRequest(t,n)).wait((e=>r.resolve(e)),(e=>r.reject(e.reason)))}else{const{supportsRanges:e,fileLength:o,content:i}=await this.checkRangeSupport(t.url);if(e){(await this.openDocumentWithRangeRequest(t,n,o)).wait((e=>r.resolve(e)),(e=>r.reject(e.reason)))}else if(i){const e={id:t.id,content:i};this.openDocumentFromBuffer(e,n).wait((e=>r.resolve(e)),(e=>r.reject(e.reason)))}else{(await this.fetchFullAndOpen(t,n)).wait((e=>r.resolve(e)),(e=>r.reject(e.reason)))}}}catch(o){this.logger.error(u,l,"openDocumentUrl error",o),r.reject({code:e.PdfErrorCode.Unknown,message:String(o)})}})(),r}async checkRangeSupport(e){try{this.logger.debug(u,l,"checkRangeSupport",e);const t=await fetch(e,{method:"HEAD"}),o=t.headers.get("Content-Length");if("bytes"===t.headers.get("Accept-Ranges"))return{supportsRanges:!0,fileLength:parseInt(o??"0"),content:null};const i=await fetch(e,{headers:{Range:"bytes=0-1"}});if(200===i.status){const e=await i.arrayBuffer();return{supportsRanges:!1,fileLength:parseInt(o??"0"),content:e}}return{supportsRanges:206===i.status,fileLength:parseInt(o??"0"),content:null}}catch(t){throw this.logger.error(u,l,"checkRangeSupport failed",t),new Error("Failed to check range support: "+t)}}async fetchFullAndOpen(e,t){this.logger.debug(u,l,"fetchFullAndOpen",e.url);const o=await fetch(e.url);if(!o.ok)throw new Error(`Could not fetch PDF: ${o.statusText}`);const i=await o.arrayBuffer(),n={id:e.id,content:i};return this.openDocumentFromBuffer(n,t)}async openDocumentWithRangeRequest(e,t,o){this.logger.debug(u,l,"openDocumentWithRangeRequest",e.url);const i=o??(await this.retrieveFileLength(e.url)).fileLength;return this.openDocumentFromLoader({id:e.id,fileLength:i,callback:(t,o)=>{const i=new XMLHttpRequest;if(i.open("GET",e.url,!1),i.overrideMimeType("text/plain; charset=x-user-defined"),i.setRequestHeader("Range",`bytes=${t}-${t+o-1}`),i.send(null),206===i.status||200===i.status)return this.convertResponseToUint8Array(i.responseText);throw new Error(`Range request failed with status ${i.status}`)}},t)}async retrieveFileLength(e){this.logger.debug(u,l,"retrieveFileLength",e);const t=await fetch(e,{method:"HEAD"});if(!t.ok)throw new Error(`Failed HEAD request for file length: ${t.statusText}`);const o=t.headers.get("Content-Length")||"0",i=parseInt(o,10)||0;if(!i)throw new Error("Content-Length not found or zero.");return{fileLength:i}}convertResponseToUint8Array(e){const t=new Uint8Array(e.length);for(let o=0;o<e.length;o++)t[o]=255&e.charCodeAt(o);return t}openDocumentFromBuffer(t,o=""){this.logger.debug(u,l,"openDocumentFromBuffer",t,o),this.logger.perf(u,l,"OpenDocumentFromBuffer","Begin",t.id);const i=new Uint8Array(t.content),n=i.length,r=this.malloc(n);this.pdfiumModule.pdfium.HEAPU8.set(i,r);const d=this.pdfiumModule.FPDF_LoadMemDocument(r,n,o);if(!d){const o=this.pdfiumModule.FPDF_GetLastError();return this.logger.error(u,l,`FPDF_LoadMemDocument failed with ${o}`),this.free(r),this.logger.perf(u,l,"OpenDocumentFromBuffer","End",t.id),e.PdfTaskHelper.reject({code:o,message:"FPDF_LoadMemDocument failed"})}const s=this.pdfiumModule.FPDF_GetPageCount(d),a=[],h=this.malloc(8);for(let c=0;c<s;c++){if(!this.pdfiumModule.FPDF_GetPageSizeByIndexF(d,c,h)){const o=this.pdfiumModule.FPDF_GetLastError();return this.logger.error(u,l,`FPDF_GetPageSizeByIndexF failed with ${o}`),this.free(h),this.pdfiumModule.FPDF_CloseDocument(d),this.free(r),this.logger.perf(u,l,"OpenDocumentFromBuffer","End",t.id),e.PdfTaskHelper.reject({code:o,message:"FPDF_GetPageSizeByIndexF failed"})}const o={index:c,size:{width:this.pdfiumModule.pdfium.getValue(h,"float"),height:this.pdfiumModule.pdfium.getValue(h+4,"float")}};a.push(o)}this.free(h);const f={id:t.id,pageCount:s,pages:a};return this.cache.setDocument(t.id,r,d),this.logger.perf(u,l,"OpenDocumentFromBuffer","End",t.id),e.PdfTaskHelper.resolve(f)}openDocumentFromLoader(t,o=""){const{fileLength:i,callback:n,...r}=t;this.logger.debug(u,l,"openDocumentFromLoader",r,o),this.logger.perf(u,l,"OpenDocumentFromLoader","Begin",r.id);const d=this.pdfiumModule.pdfium.addFunction(((e,t,o,r)=>{try{if(this.logger.debug(u,l,"readBlock",t,r,o),t<0||t>=i)return this.logger.error(u,l,"Offset out of bounds:",t),0;const e=n(t,r);return new Uint8Array(this.pdfiumModule.pdfium.HEAPU8.buffer,o,e.length).set(e),e.length}catch(d){return this.logger.error(u,l,"ReadBlock error:",d),0}}),"iiiii"),s=this.malloc(12);this.pdfiumModule.pdfium.setValue(s,i,"i32"),this.pdfiumModule.pdfium.setValue(s+4,d,"i32"),this.pdfiumModule.pdfium.setValue(s+8,0,"i32");const a=this.pdfiumModule.FPDF_LoadCustomDocument(s,o);if(!a){const t=this.pdfiumModule.FPDF_GetLastError();return this.logger.error(u,l,`FPDF_LoadCustomDocument failed with ${t}`),this.free(s),this.logger.perf(u,l,"OpenDocumentFromLoader","End",r.id),e.PdfTaskHelper.reject({code:t,message:"FPDF_LoadCustomDocument failed"})}const h=this.pdfiumModule.FPDF_GetPageCount(a),f=[],c=this.malloc(8);for(let p=0;p<h;p++){if(!this.pdfiumModule.FPDF_GetPageSizeByIndexF(a,p,c)){const t=this.pdfiumModule.FPDF_GetLastError();return this.logger.error(u,l,`FPDF_GetPageSizeByIndexF failed with ${t}`),this.free(c),this.pdfiumModule.FPDF_CloseDocument(a),this.free(s),this.logger.perf(u,l,"OpenDocumentFromLoader","End",r.id),e.PdfTaskHelper.reject({code:t,message:"FPDF_GetPageSizeByIndexF failed"})}const t={index:p,size:{width:this.pdfiumModule.pdfium.getValue(c,"float"),height:this.pdfiumModule.pdfium.getValue(c+4,"float")}};f.push(t)}this.free(c);const g={id:r.id,pageCount:h,pages:f};return this.cache.setDocument(r.id,s,a),this.logger.perf(u,l,"OpenDocumentFromLoader","End",r.id),e.PdfTaskHelper.resolve(g)}getMetadata(t){this.logger.debug(u,l,"getMetadata",t),this.logger.perf(u,l,"GetMetadata","Begin",t.id);const o=this.cache.getContext(t.id);if(!o)return this.logger.perf(u,l,"GetMetadata","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const i={title:this.readMetaText(o.docPtr,"Title"),author:this.readMetaText(o.docPtr,"Author"),subject:this.readMetaText(o.docPtr,"Subject"),keywords:this.readMetaText(o.docPtr,"Keywords"),producer:this.readMetaText(o.docPtr,"Producer"),creator:this.readMetaText(o.docPtr,"Creator"),creationDate:this.readMetaText(o.docPtr,"CreationDate"),modificationDate:this.readMetaText(o.docPtr,"ModDate")};return this.logger.perf(u,l,"GetMetadata","End",t.id),e.PdfTaskHelper.resolve(i)}getDocPermissions(t){this.logger.debug(u,l,"getDocPermissions",t),this.logger.perf(u,l,"getDocPermissions","Begin",t.id);const o=this.cache.getContext(t.id);if(!o)return this.logger.perf(u,l,"getDocPermissions","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const i=this.pdfiumModule.FPDF_GetDocPermissions(o.docPtr);return e.PdfTaskHelper.resolve(i)}getDocUserPermissions(t){this.logger.debug(u,l,"getDocUserPermissions",t),this.logger.perf(u,l,"getDocUserPermissions","Begin",t.id);const o=this.cache.getContext(t.id);if(!o)return this.logger.perf(u,l,"getDocUserPermissions","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const i=this.pdfiumModule.FPDF_GetDocUserPermissions(o.docPtr);return e.PdfTaskHelper.resolve(i)}getSignatures(i){this.logger.debug(u,l,"getSignatures",i),this.logger.perf(u,l,"GetSignatures","Begin",i.id);const n=this.cache.getContext(i.id);if(!n)return this.logger.perf(u,l,"GetSignatures","End",i.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const r=[],d=this.pdfiumModule.FPDF_GetSignatureCount(n.docPtr);for(let e=0;e<d;e++){const i=this.pdfiumModule.FPDF_GetSignatureObject(n.docPtr,e),d=o(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFSignatureObj_GetContents(i,e,t))),s=o(this.pdfiumModule.pdfium,((e,t)=>4*this.pdfiumModule.FPDFSignatureObj_GetByteRange(i,e,t))),a=o(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFSignatureObj_GetSubFilter(i,e,t))),u=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFSignatureObj_GetReason(i,e,t)),this.pdfiumModule.pdfium.UTF16ToString),l=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFSignatureObj_GetTime(i,e,t)),this.pdfiumModule.pdfium.UTF8ToString),h=this.pdfiumModule.FPDFSignatureObj_GetDocMDPPermission(i);r.push({contents:d,byteRange:s,subFilter:a,reason:u,time:l,docMDP:h})}return this.logger.perf(u,l,"GetSignatures","End",i.id),e.PdfTaskHelper.resolve(r)}getBookmarks(t){this.logger.debug(u,l,"getBookmarks",t),this.logger.perf(u,l,"GetBookmarks","Begin",t.id);const o=this.cache.getContext(t.id);if(!o)return this.logger.perf(u,l,"getBookmarks","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const i=this.readPdfBookmarks(o.docPtr,0);return this.logger.perf(u,l,"GetBookmarks","End",t.id),e.PdfTaskHelper.resolve({bookmarks:i})}renderPage(t,o,i=1,n=e.Rotation.Degree0,r=1,d={withAnnotations:!1},s="image/webp"){const a=new e.Task;this.logger.debug(u,l,"renderPage",t,o,i,n,r,d),this.logger.perf(u,l,"RenderPage","Begin",`${t.id}-${o.index}`);const h=this.cache.getContext(t.id);if(!h)return this.logger.perf(u,l,"RenderPage","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const f=this.renderPageRectToImageData(h,o,{origin:{x:0,y:0},size:o.size},i,n,r,d);return this.logger.perf(u,l,"RenderPage","End",`${t.id}-${o.index}`),this.imageDataConverter(f,s).then((e=>a.resolve(e))),a}renderPageRect(t,o,i,n,r,d,s,a="image/webp"){const h=new e.Task;this.logger.debug(u,l,"renderPageRect",t,o,i,n,r,d,s),this.logger.perf(u,l,"RenderPageRect","Begin",`${t.id}-${o.index}`);const f=this.cache.getContext(t.id);if(!f)return this.logger.perf(u,l,"RenderPageRect","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const c=this.renderPageRectToImageData(f,o,d,i,n,r,s);return this.logger.perf(u,l,"RenderPageRect","End",`${t.id}-${o.index}`),this.imageDataConverter(c,a).then((e=>h.resolve(e))),h}getAllAnnotations(t){this.logger.debug(u,l,"getAllAnnotations",t),this.logger.perf(u,l,"GetAllAnnotations","Begin",t.id);const o=this.cache.getContext(t.id);if(!o)return this.logger.perf(u,l,"GetAllAnnotations","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const i=this.readAllAnnotations(t,o);return this.logger.perf(u,l,"GetAllAnnotations","End",t.id),e.PdfTaskHelper.resolve(i)}readAllAnnotations(e,t){const o={};for(let i=0;i<e.pageCount;i++){const n=this.readPageAnnotations(t,e.pages[i]);o[i]=n}return o}getPageAnnotations(t,o){this.logger.debug(u,l,"getPageAnnotations",t,o),this.logger.perf(u,l,"GetPageAnnotations","Begin",`${t.id}-${o.index}`);const i=this.cache.getContext(t.id);if(!i)return this.logger.perf(u,l,"GetPageAnnotations","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const n=this.readPageAnnotations(i,o);return this.logger.perf(u,l,"GetPageAnnotations","End",`${t.id}-${o.index}`),this.logger.debug(u,l,"GetPageAnnotations",`${t.id}-${o.index}`,n),e.PdfTaskHelper.resolve(n)}createPageAnnotation(t,o,i){this.logger.debug(u,l,"createPageAnnotation",t,o,i),this.logger.perf(u,l,"CreatePageAnnotation","Begin",`${t.id}-${o.index}`);const n=this.cache.getContext(t.id);if(!n)return this.logger.perf(u,l,"CreatePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const r=n.acquirePage(o.index),d=this.pdfiumModule.FPDFPage_CreateAnnot(r.pagePtr,i.type);if(!d)return this.logger.perf(u,l,"CreatePageAnnotation","End",`${t.id}-${o.index}`),r.release(),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantCreateAnnot,message:"can not create annotation with specified type"});if(!this.setPageAnnoRect(o,r.pagePtr,d,i.rect))return this.pdfiumModule.FPDFPage_CloseAnnot(d),r.release(),this.logger.perf(u,l,"CreatePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantSetAnnotRect,message:"can not set the rect of the annotation"});let s=!1;switch(i.type){case e.PdfAnnotationSubtype.INK:s=this.addInkStroke(o,r.pagePtr,d,i);break;case e.PdfAnnotationSubtype.STAMP:s=this.addStampContent(n.docPtr,o,r.pagePtr,d,i.rect,i.contents);break;case e.PdfAnnotationSubtype.UNDERLINE:case e.PdfAnnotationSubtype.STRIKEOUT:case e.PdfAnnotationSubtype.SQUIGGLY:case e.PdfAnnotationSubtype.HIGHLIGHT:s=this.addTextMarkupContent(o,r.pagePtr,d,i)}if(!s)return this.pdfiumModule.FPDFPage_RemoveAnnot(r.pagePtr,d),r.release(),this.logger.perf(u,l,"CreatePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantSetAnnotContent,message:"can not add content of the annotation"});void 0!==i.blendMode?this.pdfiumModule.EPDFAnnot_GenerateAppearanceWithBlend(d,i.blendMode):this.pdfiumModule.EPDFAnnot_GenerateAppearance(d),this.pdfiumModule.FPDFPage_GenerateContent(r.pagePtr);const a=this.pdfiumModule.FPDFPage_GetAnnotIndex(r.pagePtr,d);return this.pdfiumModule.FPDFPage_CloseAnnot(d),r.release(),this.logger.perf(u,l,"CreatePageAnnotation","End",`${t.id}-${o.index}`),a>=0?e.PdfTaskHelper.resolve(a):e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantCreateAnnot,message:"annotation created but index could not be determined"})}updatePageAnnotation(t,o,i){this.logger.debug(u,l,"updatePageAnnotation",t,o,i),this.logger.perf(u,l,"UpdatePageAnnotation","Begin",`${t.id}-${o.index}`);const n=this.cache.getContext(t.id);if(!n)return this.logger.perf(u,l,"UpdatePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const r=n.acquirePage(o.index),d=this.pdfiumModule.FPDFPage_GetAnnot(r.pagePtr,i.id);if(!d)return r.release(),this.logger.perf(u,l,"UpdatePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"annotation not found"});if(!this.setPageAnnoRect(o,r.pagePtr,d,i.rect))return this.pdfiumModule.FPDFPage_CloseAnnot(d),r.release(),this.logger.perf(u,l,"UpdatePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantSetAnnotRect,message:"failed to move annotation"});let s=!1;switch(i.type){case e.PdfAnnotationSubtype.INK:if(!this.pdfiumModule.FPDFAnnot_RemoveInkList(d))break;s=this.addInkStroke(o,r.pagePtr,d,i);break;case e.PdfAnnotationSubtype.STAMP:for(let e=this.pdfiumModule.FPDFAnnot_GetObjectCount(d)-1;e>=0;e--)this.pdfiumModule.FPDFAnnot_RemoveObject(d,e);s=this.addStampContent(n.docPtr,o,r.pagePtr,d,i.rect,i.contents);break;case e.PdfAnnotationSubtype.HIGHLIGHT:case e.PdfAnnotationSubtype.UNDERLINE:case e.PdfAnnotationSubtype.STRIKEOUT:case e.PdfAnnotationSubtype.SQUIGGLY:s=this.addTextMarkupContent(o,r.pagePtr,d,i);break;default:s=!1}return s&&(void 0!==i.blendMode?this.pdfiumModule.EPDFAnnot_GenerateAppearanceWithBlend(d,i.blendMode):this.pdfiumModule.EPDFAnnot_GenerateAppearance(d),this.pdfiumModule.FPDFPage_GenerateContent(r.pagePtr)),this.pdfiumModule.FPDFPage_CloseAnnot(d),r.release(),this.logger.perf(u,l,"UpdatePageAnnotation","End",`${t.id}-${o.index}`),s?e.PdfTaskHelper.resolve(!0):e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantSetAnnotContent,message:"failed to update annotation"})}removePageAnnotation(t,o,i){this.logger.debug(u,l,"removePageAnnotation",t,o,i),this.logger.perf(u,l,"RemovePageAnnotation","Begin",`${t.id}-${o.index}`);const n=this.cache.getContext(t.id);if(!n)return this.logger.perf(u,l,"RemovePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const r=n.acquirePage(o.index);let d=!1;return d=this.pdfiumModule.FPDFPage_RemoveAnnot(r.pagePtr,i.id),d?(d=this.pdfiumModule.FPDFPage_GenerateContent(r.pagePtr),d||this.logger.error(u,l,"FPDFPage_GenerateContent Failed",`${t.id}-${o.index}`)):this.logger.error(u,l,"FPDFPage_RemoveAnnot Failed",`${t.id}-${o.index}`),r.release(),this.logger.perf(u,l,"RemovePageAnnotation","End",`${t.id}-${o.index}`),e.PdfTaskHelper.resolve(d)}getPageTextRects(t,o,i,n){this.logger.debug(u,l,"getPageTextRects",t,o,i,n),this.logger.perf(u,l,"GetPageTextRects","Begin",`${t.id}-${o.index}`);const r=this.cache.getContext(t.id);if(!r)return this.logger.perf(u,l,"GetPageTextRects","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const d=r.acquirePage(o.index),s=this.pdfiumModule.FPDFText_LoadPage(d.pagePtr),a=this.readPageTextRects(o,d.docPtr,d.pagePtr,s);return this.pdfiumModule.FPDFText_ClosePage(s),d.release(),this.logger.perf(u,l,"GetPageTextRects","End",`${t.id}-${o.index}`),e.PdfTaskHelper.resolve(a)}renderThumbnail(t,o,i,n,r){this.logger.debug(u,l,"renderThumbnail",t,o,i,n,r),this.logger.perf(u,l,"RenderThumbnail","Begin",`${t.id}-${o.index}`);if(!this.cache.getContext(t.id))return this.logger.perf(u,l,"RenderThumbnail","End",`${t.id}-${o.index}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});i=Math.max(i,.5);const d=this.renderPage(t,o,i,n,r,{withAnnotations:!0});return this.logger.perf(u,l,"RenderThumbnail","End",`${t.id}-${o.index}`),d}getAttachments(t){this.logger.debug(u,l,"getAttachments",t),this.logger.perf(u,l,"GetAttachments","Begin",t.id);const o=this.cache.getContext(t.id);if(!o)return this.logger.perf(u,l,"GetAttachments","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const i=[],n=this.pdfiumModule.FPDFDoc_GetAttachmentCount(o.docPtr);for(let e=0;e<n;e++){const t=this.readPdfAttachment(o.docPtr,e);i.push(t)}return this.logger.perf(u,l,"GetAttachments","End",t.id),e.PdfTaskHelper.resolve(i)}readAttachmentContent(t,o){this.logger.debug(u,l,"readAttachmentContent",t,o),this.logger.perf(u,l,"ReadAttachmentContent","Begin",t.id);const i=this.cache.getContext(t.id);if(!i)return this.logger.perf(u,l,"ReadAttachmentContent","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const n=this.pdfiumModule.FPDFDoc_GetAttachment(i.docPtr,o.index),r=this.malloc(8);if(!this.pdfiumModule.FPDFAttachment_GetFile(n,0,0,r))return this.free(r),this.logger.perf(u,l,"ReadAttachmentContent","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantReadAttachmentSize,message:"can not read attachment size"});const d=this.pdfiumModule.pdfium.getValue(r,"i64"),s=this.malloc(d);if(!this.pdfiumModule.FPDFAttachment_GetFile(n,s,d,r))return this.free(r),this.free(s),this.logger.perf(u,l,"ReadAttachmentContent","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantReadAttachmentContent,message:"can not read attachment content"});const a=new ArrayBuffer(d),h=new DataView(a);for(let e=0;e<d;e++)h.setInt8(e,this.pdfiumModule.pdfium.getValue(s+e,"i8"));return this.free(r),this.free(s),this.logger.perf(u,l,"ReadAttachmentContent","End",t.id),e.PdfTaskHelper.resolve(a)}setFormFieldValue(t,o,i,n){this.logger.debug(u,l,"SetFormFieldValue",t,i,n),this.logger.perf(u,l,"SetFormFieldValue","Begin",`${t.id}-${i.id}`);const r=this.cache.getContext(t.id);if(!r)return this.logger.debug(u,l,"SetFormFieldValue","document is not opened"),this.logger.perf(u,l,"SetFormFieldValue","End",`${t.id}-${i.id}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const d=this.pdfiumModule.PDFiumExt_OpenFormFillInfo(),s=this.pdfiumModule.PDFiumExt_InitFormFillEnvironment(r.docPtr,d),a=r.acquirePage(o.index);this.pdfiumModule.FORM_OnAfterLoadPage(a.pagePtr,s);const h=this.pdfiumModule.FPDFPage_GetAnnot(a.pagePtr,i.id);if(!this.pdfiumModule.FORM_SetFocusedAnnot(s,h))return this.logger.debug(u,l,"SetFormFieldValue","failed to set focused annotation"),this.logger.perf(u,l,"SetFormFieldValue","End",`${t.id}-${i.id}`),this.pdfiumModule.FPDFPage_CloseAnnot(h),this.pdfiumModule.FORM_OnBeforeClosePage(a.pagePtr,s),a.release(),this.pdfiumModule.PDFiumExt_ExitFormFillEnvironment(s),this.pdfiumModule.PDFiumExt_CloseFormFillInfo(d),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantFocusAnnot,message:"failed to set focused annotation"});switch(n.kind){case"text":{if(!this.pdfiumModule.FORM_SelectAllText(s,a.pagePtr))return this.logger.debug(u,l,"SetFormFieldValue","failed to select all text"),this.logger.perf(u,l,"SetFormFieldValue","End",`${t.id}-${i.id}`),this.pdfiumModule.FORM_ForceToKillFocus(s),this.pdfiumModule.FPDFPage_CloseAnnot(h),this.pdfiumModule.FORM_OnBeforeClosePage(a.pagePtr,s),a.release(),this.pdfiumModule.PDFiumExt_ExitFormFillEnvironment(s),this.pdfiumModule.PDFiumExt_CloseFormFillInfo(d),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantSelectText,message:"failed to select all text"});const o=2*(n.text.length+1),r=this.malloc(o);this.pdfiumModule.pdfium.stringToUTF16(n.text,r,o),this.pdfiumModule.FORM_ReplaceSelection(s,a.pagePtr,r),this.free(r)}break;case"selection":if(!this.pdfiumModule.FORM_SetIndexSelected(s,a.pagePtr,n.index,n.isSelected))return this.logger.debug(u,l,"SetFormFieldValue","failed to set index selected"),this.logger.perf(u,l,"SetFormFieldValue","End",`${t.id}-${i.id}`),this.pdfiumModule.FORM_ForceToKillFocus(s),this.pdfiumModule.FPDFPage_CloseAnnot(h),this.pdfiumModule.FORM_OnBeforeClosePage(a.pagePtr,s),a.release(),this.pdfiumModule.PDFiumExt_ExitFormFillEnvironment(s),this.pdfiumModule.PDFiumExt_CloseFormFillInfo(d),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantSelectOption,message:"failed to set index selected"});break;case"checked":{const o=13;if(!this.pdfiumModule.FORM_OnChar(s,a.pagePtr,o,0))return this.logger.debug(u,l,"SetFormFieldValue","failed to set field checked"),this.logger.perf(u,l,"SetFormFieldValue","End",`${t.id}-${i.id}`),this.pdfiumModule.FORM_ForceToKillFocus(s),this.pdfiumModule.FPDFPage_CloseAnnot(h),this.pdfiumModule.FORM_OnBeforeClosePage(a.pagePtr,s),a.release(),this.pdfiumModule.PDFiumExt_ExitFormFillEnvironment(s),this.pdfiumModule.PDFiumExt_CloseFormFillInfo(d),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantCheckField,message:"failed to set field checked"})}}return this.pdfiumModule.FORM_ForceToKillFocus(s),this.pdfiumModule.FPDFPage_CloseAnnot(h),this.pdfiumModule.FORM_OnBeforeClosePage(a.pagePtr,s),a.release(),this.pdfiumModule.PDFiumExt_ExitFormFillEnvironment(s),this.pdfiumModule.PDFiumExt_CloseFormFillInfo(d),e.PdfTaskHelper.resolve(!0)}flattenPage(t,o,i){this.logger.debug(u,l,"flattenPage",t,o,i),this.logger.perf(u,l,"flattenPage","Begin",t.id);const n=this.cache.getContext(t.id);if(!n)return this.logger.perf(u,l,"flattenPage","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const r=n.acquirePage(o.index),d=this.pdfiumModule.FPDFPage_Flatten(r.pagePtr,i);return r.release(),this.logger.perf(u,l,"flattenPage","End",t.id),e.PdfTaskHelper.resolve(d)}extractPages(t,o){this.logger.debug(u,l,"extractPages",t,o),this.logger.perf(u,l,"ExtractPages","Begin",t.id);const i=this.cache.getContext(t.id);if(!i)return this.logger.perf(u,l,"ExtractPages","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const n=this.pdfiumModule.FPDF_CreateNewDocument();if(!n)return this.logger.perf(u,l,"ExtractPages","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantCreateNewDoc,message:"can not create new document"});const r=this.malloc(4*o.length);for(let e=0;e<o.length;e++)this.pdfiumModule.pdfium.setValue(r+4*e,o[e],"i32");if(!this.pdfiumModule.FPDF_ImportPagesByIndex(n,i.docPtr,r,o.length,0))return this.pdfiumModule.FPDF_CloseDocument(n),this.logger.perf(u,l,"ExtractPages","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantImportPages,message:"can not import pages to new document"});const d=this.saveDocument(n);return this.pdfiumModule.FPDF_CloseDocument(n),this.logger.perf(u,l,"ExtractPages","End",t.id),e.PdfTaskHelper.resolve(d)}extractText(t,o){this.logger.debug(u,l,"extractText",t,o),this.logger.perf(u,l,"ExtractText","Begin",t.id);const i=this.cache.getContext(t.id);if(!i)return this.logger.perf(u,l,"ExtractText","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const n=[];for(let e=0;e<o.length;e++){const t=i.acquirePage(o[e]),r=this.pdfiumModule.FPDFText_LoadPage(t.pagePtr),d=this.pdfiumModule.FPDFText_CountChars(r),s=this.malloc(2*(d+1));this.pdfiumModule.FPDFText_GetText(r,0,d,s);const a=this.pdfiumModule.pdfium.UTF16ToString(s);this.free(s),n.push(a),this.pdfiumModule.FPDFText_ClosePage(r),t.release()}const r=n.join("\n\n");return this.logger.perf(u,l,"ExtractText","End",t.id),e.PdfTaskHelper.resolve(r)}getTextSlices(t,o){if(this.logger.debug(u,l,"getTextSlices",t,o),this.logger.perf(u,l,"GetTextSlices","Begin",t.id),0===o.length)return this.logger.perf(u,l,"GetTextSlices","End",t.id),e.PdfTaskHelper.resolve([]);const i=this.cache.getContext(t.id);if(!i)return this.logger.perf(u,l,"GetTextSlices","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});try{const n=new Array(o.length),r=new Map;o.forEach(((e,t)=>{(r.get(e.pageIndex)??r.set(e.pageIndex,[]).get(e.pageIndex)).push({slice:e,pos:t})}));for(const[t,o]of r){const r=i.acquirePage(t),d=r.getTextPage();for(const{slice:t,pos:i}of o){const o=this.malloc(2*(t.charCount+1));this.pdfiumModule.FPDFText_GetText(d,t.charIndex,t.charCount,o),n[i]=e.stripPdfUnwantedMarkers(this.pdfiumModule.pdfium.UTF16ToString(o)),this.free(o)}r.release()}return this.logger.perf(u,l,"GetTextSlices","End",t.id),e.PdfTaskHelper.resolve(n)}catch(n){return this.logger.error(u,l,"getTextSlices error",n),this.logger.perf(u,l,"GetTextSlices","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.Unknown,message:String(n)})}}merge(t){this.logger.debug(u,l,"merge",t);const o=t.map((e=>e.id)).join(".");this.logger.perf(u,l,"Merge","Begin",o);const i=this.pdfiumModule.FPDF_CreateNewDocument();if(!i)return this.logger.perf(u,l,"Merge","End",o),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantCreateNewDoc,message:"can not create new document"});const n=[];for(const s of t.reverse()){const t=new Uint8Array(s.content),r=t.length,d=this.malloc(r);this.pdfiumModule.pdfium.HEAPU8.set(t,d);const a=this.pdfiumModule.FPDF_LoadMemDocument(d,r,"");if(!a){const t=this.pdfiumModule.FPDF_GetLastError();this.logger.error(u,l,`FPDF_LoadMemDocument failed with ${t}`),this.free(d);for(const e of n)this.pdfiumModule.FPDF_CloseDocument(e.docPtr),this.free(e.filePtr);return this.logger.perf(u,l,"Merge","End",o),e.PdfTaskHelper.reject({code:t,message:"FPDF_LoadMemDocument failed"})}if(n.push({filePtr:d,docPtr:a}),!this.pdfiumModule.FPDF_ImportPages(i,a,"",0)){this.pdfiumModule.FPDF_CloseDocument(i);for(const e of n)this.pdfiumModule.FPDF_CloseDocument(e.docPtr),this.free(e.filePtr);return this.logger.perf(u,l,"Merge","End",o),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantImportPages,message:"can not import pages to new document"})}}const r=this.saveDocument(i);this.pdfiumModule.FPDF_CloseDocument(i);for(const e of n)this.pdfiumModule.FPDF_CloseDocument(e.docPtr),this.free(e.filePtr);const d={id:`${Math.random()}`,content:r};return this.logger.perf(u,l,"Merge","End",o),e.PdfTaskHelper.resolve(d)}mergePages(t){const o=t.map((e=>`${e.docId}:${e.pageIndices.join(",")}`)).join("|");this.logger.debug(u,l,"mergePages",t),this.logger.perf(u,l,"MergePages","Begin",o);const i=this.pdfiumModule.FPDF_CreateNewDocument();if(!i)return this.logger.perf(u,l,"MergePages","End",o),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantCreateNewDoc,message:"Cannot create new document"});try{for(const e of[...t].reverse()){const t=this.cache.getContext(e.docId);if(!t){this.logger.warn(u,l,`Document ${e.docId} is not open, skipping`);continue}const o=this.pdfiumModule.FPDF_GetPageCount(t.docPtr),n=e.pageIndices.filter((e=>e>=0&&e<o));if(0===n.length)continue;const r=n.map((e=>e+1)).join(",");try{if(!this.pdfiumModule.FPDF_ImportPages(i,t.docPtr,r,0))throw new Error(`Failed to import pages ${r} from document ${e.docId}`)}finally{}}const n=this.saveDocument(i),r={id:`${Math.random()}`,content:n};return this.logger.perf(u,l,"MergePages","End",o),e.PdfTaskHelper.resolve(r)}catch(n){return this.logger.error(u,l,"mergePages failed",n),this.logger.perf(u,l,"MergePages","End",o),e.PdfTaskHelper.reject({code:e.PdfErrorCode.CantImportPages,message:n instanceof Error?n.message:"Failed to merge pages"})}finally{i&&this.pdfiumModule.FPDF_CloseDocument(i)}}saveAsCopy(t){this.logger.debug(u,l,"saveAsCopy",t),this.logger.perf(u,l,"SaveAsCopy","Begin",t.id);const o=this.cache.getContext(t.id);if(!o)return this.logger.perf(u,l,"SaveAsCopy","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const i=this.saveDocument(o.docPtr);return this.logger.perf(u,l,"SaveAsCopy","End",t.id),e.PdfTaskHelper.resolve(i)}closeDocument(t){this.logger.debug(u,l,"closeDocument",t),this.logger.perf(u,l,"CloseDocument","Begin",t.id);const o=this.cache.getContext(t.id);return o?(o.dispose(),this.logger.perf(u,l,"CloseDocument","End",t.id),e.PdfTaskHelper.resolve(!0)):(this.logger.perf(u,l,"CloseDocument","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"}))}malloc(e){const t=this.pdfiumModule.pdfium.wasmExports.malloc(e);for(let o=0;o<e;o++)this.pdfiumModule.pdfium.HEAP8[t+o]=0;return t}free(e){this.pdfiumModule.pdfium.wasmExports.free(e)}addInkStroke(t,o,i,n){return!!this.setBorderStyle(i,e.PdfAnnotationBorderStyle.SOLID,n.strokeWidth)&&(!!this.setPageAnnoRect(t,o,i,n.rect)&&(!!this.setInkList(t,i,n.inkList)&&(!!this.setAnnotString(i,"T",n.author||"")&&(!!this.setAnnotString(i,"M",e.dateToPdfDate(n.modified))&&!!this.setAnnotationColor(i,{color:n.color??"#FFFF00",opacity:n.opacity??1},e.PdfAnnotationColorType.Color)))))}addTextMarkupContent(t,o,i,n){return!!this.setPageAnnoRect(t,o,i,n.rect)&&(!!this.syncQuadPointsAnno(t,i,n.segmentRects)&&(!!this.setAnnotString(i,"Contents",n.contents??"")&&(!!this.setAnnotString(i,"T",n.author||"")&&(!!this.setAnnotString(i,"M",e.dateToPdfDate(n.modified))&&!!this.setAnnotationColor(i,{color:n.color??"#FFFF00",opacity:n.opacity??1},e.PdfAnnotationColorType.Color)))))}addStampContent(t,o,i,n,r,d){for(const s of d)if(s.type===e.PdfPageObjectType.IMAGE)return this.addImageObject(t,o,i,n,r.origin,s.imageData);return!1}addImageObject(e,t,o,i,n,r){const d=r.width*r.height,s=this.malloc(4*d);if(!s)return!1;for(let h=0;h<d;h++){const e=r.data[4*h],t=r.data[4*h+1],o=r.data[4*h+2],i=r.data[4*h+3];this.pdfiumModule.pdfium.setValue(s+4*h,o,"i8"),this.pdfiumModule.pdfium.setValue(s+4*h+1,t,"i8"),this.pdfiumModule.pdfium.setValue(s+4*h+2,e,"i8"),this.pdfiumModule.pdfium.setValue(s+4*h+3,i,"i8")}const a=this.pdfiumModule.FPDFBitmap_CreateEx(r.width,r.height,4,s,0);if(!a)return this.free(s),!1;const u=this.pdfiumModule.FPDFPageObj_NewImageObj(e);if(!u)return this.pdfiumModule.FPDFBitmap_Destroy(a),this.free(s),!1;if(!this.pdfiumModule.FPDFImageObj_SetBitmap(o,0,u,a))return this.pdfiumModule.FPDFBitmap_Destroy(a),this.pdfiumModule.FPDFPageObj_Destroy(u),this.free(s),!1;const l=this.malloc(24);return this.pdfiumModule.pdfium.setValue(l,r.width,"float"),this.pdfiumModule.pdfium.setValue(l+4,0,"float"),this.pdfiumModule.pdfium.setValue(l+8,0,"float"),this.pdfiumModule.pdfium.setValue(l+12,r.height,"float"),this.pdfiumModule.pdfium.setValue(l+16,0,"float"),this.pdfiumModule.pdfium.setValue(l+20,0,"float"),this.pdfiumModule.FPDFPageObj_SetMatrix(u,l)?(this.free(l),this.pdfiumModule.FPDFPageObj_Transform(u,1,0,0,1,n.x,n.y),this.pdfiumModule.FPDFAnnot_AppendObject(i,u)?(this.pdfiumModule.FPDFPage_GenerateContent(o),this.pdfiumModule.FPDFBitmap_Destroy(a),this.free(s),!0):(this.pdfiumModule.FPDFBitmap_Destroy(a),this.pdfiumModule.FPDFPageObj_Destroy(u),this.free(s),!1)):(this.free(l),this.pdfiumModule.FPDFBitmap_Destroy(a),this.pdfiumModule.FPDFPageObj_Destroy(u),this.free(s),!1)}saveDocument(e){const t=this.pdfiumModule.PDFiumExt_OpenFileWriter();this.pdfiumModule.PDFiumExt_SaveAsCopy(e,t);const o=this.pdfiumModule.PDFiumExt_GetFileWriterSize(t),i=this.malloc(o);this.pdfiumModule.PDFiumExt_GetFileWriterData(t,i,o);const n=new ArrayBuffer(o),r=new DataView(n);for(let d=0;d<o;d++)r.setInt8(d,this.pdfiumModule.pdfium.getValue(i+d,"i8"));return this.free(i),this.pdfiumModule.PDFiumExt_CloseFileWriter(t),n}readMetaText(e,o){return t(this.pdfiumModule.pdfium,((t,i)=>this.pdfiumModule.FPDF_GetMetaText(e,o,t,i)),this.pdfiumModule.pdfium.UTF16ToString)}readPdfBookmarks(e,t=0){let o=this.pdfiumModule.FPDFBookmark_GetFirstChild(e,t);const i=[];for(;o;){const t=this.readPdfBookmark(e,o);i.push(t);o=this.pdfiumModule.FPDFBookmark_GetNextSibling(e,o)}return i}readPdfBookmark(e,o){const i=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFBookmark_GetTitle(o,e,t)),this.pdfiumModule.pdfium.UTF16ToString),n=this.readPdfBookmarks(e,o);return{title:i,target:this.readPdfBookmarkTarget(e,(()=>this.pdfiumModule.FPDFBookmark_GetAction(o)),(()=>this.pdfiumModule.FPDFBookmark_GetDest(e,o))),children:n}}readPageTextRects(e,t,o,i){const n=this.pdfiumModule.FPDFText_CountRects(i,0,-1),r=[];for(let d=0;d<n;d++){const t=this.malloc(8),n=this.malloc(8),s=this.malloc(8),a=this.malloc(8);if(!this.pdfiumModule.FPDFText_GetRect(i,d,n,t,s,a)){this.free(n),this.free(t),this.free(s),this.free(a);continue}const u=this.pdfiumModule.pdfium.getValue(n,"double"),l=this.pdfiumModule.pdfium.getValue(t,"double"),h=this.pdfiumModule.pdfium.getValue(s,"double"),f=this.pdfiumModule.pdfium.getValue(a,"double");this.free(n),this.free(t),this.free(s),this.free(a);const c=this.malloc(4),g=this.malloc(4);this.pdfiumModule.FPDF_PageToDevice(o,0,0,e.size.width,e.size.height,0,u,l,c,g);const p=this.pdfiumModule.pdfium.getValue(c,"i32"),m=this.pdfiumModule.pdfium.getValue(g,"i32");this.free(c),this.free(g);const P={origin:{x:p,y:m},size:{width:Math.ceil(Math.abs(h-u)),height:Math.ceil(Math.abs(l-f))}},F=this.pdfiumModule.FPDFText_GetBoundedText(i,u,l,h,f,0,0),M=2*(F+1),A=this.malloc(M);this.pdfiumModule.FPDFText_GetBoundedText(i,u,l,h,f,A,F);const D=this.pdfiumModule.pdfium.UTF16ToString(A);this.free(A);const T=this.pdfiumModule.FPDFText_GetCharIndexAtPos(i,u,l,2,2);let x="",C=P.size.height;if(T>=0){C=this.pdfiumModule.FPDFText_GetFontSize(i,T);const e=this.pdfiumModule.FPDFText_GetFontInfo(i,T,0,0,0)+1,t=this.malloc(e),o=this.malloc(4);this.pdfiumModule.FPDFText_GetFontInfo(i,T,t,e,o),x=this.pdfiumModule.pdfium.UTF8ToString(t),this.free(t),this.free(o)}const E={content:D,rect:P,font:{family:x,size:C}};r.push(E)}return r}getPageGeometry(t,o){const i="getPageGeometry";this.logger.perf(u,l,i,"Begin",t.id);const n=this.cache.getContext(t.id);if(!n)return this.logger.perf(u,l,i,"End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const r=n.acquirePage(o.index),d=r.getTextPage(),s=this.pdfiumModule.FPDFText_CountChars(d),a=[];for(let e=0;e<s;e++){const t=this.readGlyphInfo(o,r.pagePtr,d,e);a.push(t)}const h=this.buildRunsFromGlyphs(a,d);return r.release(),this.logger.perf(u,l,i,"End",t.id),e.PdfTaskHelper.resolve({runs:h})}buildRunsFromGlyphs(e,t){const o=[];let i=null,n=null,r=null;for(let d=0;d<e.length;d++){const s=e[d],a=this.pdfiumModule.FPDFText_GetTextObject(t,d);if(a!==n&&(n=a,i={rect:{x:s.origin.x,y:s.origin.y,width:s.size.width,height:s.size.height},charStart:d,glyphs:[]},r={minX:s.origin.x,minY:s.origin.y,maxX:s.origin.x+s.size.width,maxY:s.origin.y+s.size.height},o.push(i)),i.glyphs.push({x:s.origin.x,y:s.origin.y,width:s.size.width,height:s.size.height,flags:s.isEmpty?2:s.isSpace?1:0}),s.isEmpty)continue;const u=s.origin.x+s.size.width,l=s.origin.y+s.size.height;r.minX=Math.min(r.minX,s.origin.x),r.minY=Math.min(r.minY,s.origin.y),r.maxX=Math.max(r.maxX,u),r.maxY=Math.max(r.maxY,l),i.rect.x=r.minX,i.rect.y=r.minY,i.rect.width=r.maxX-r.minX,i.rect.height=r.maxY-r.minY}return o}readGlyphInfo(e,t,o,i){const n=this.malloc(4),r=this.malloc(4),d=this.malloc(4),s=this.malloc(4),a=this.malloc(16);let u=0,l=0,h=0,f=0,c=!1;if(this.pdfiumModule.FPDFText_GetLooseCharBox(o,i,a)){const g=this.pdfiumModule.pdfium.getValue(a,"float"),p=this.pdfiumModule.pdfium.getValue(a+4,"float"),m=this.pdfiumModule.pdfium.getValue(a+8,"float"),P=this.pdfiumModule.pdfium.getValue(a+12,"float");if(g===m||p===P)return{origin:{x:0,y:0},size:{width:0,height:0},isEmpty:!0};this.pdfiumModule.FPDF_PageToDevice(t,0,0,e.size.width,e.size.height,0,g,p,n,r),this.pdfiumModule.FPDF_PageToDevice(t,0,0,e.size.width,e.size.height,0,m,P,d,s);const F=this.pdfiumModule.pdfium.getValue(n,"i32"),M=this.pdfiumModule.pdfium.getValue(r,"i32"),A=this.pdfiumModule.pdfium.getValue(d,"i32"),D=this.pdfiumModule.pdfium.getValue(s,"i32");u=Math.min(F,A),l=Math.min(M,D),h=Math.max(1,Math.abs(A-F)),f=Math.max(1,Math.abs(D-M));c=32===this.pdfiumModule.FPDFText_GetUnicode(o,i)}return[a,n,r,d,s].forEach((e=>this.free(e))),{origin:{x:u,y:l},size:{width:h,height:f},...c&&{isSpace:c}}}getPageGlyphs(t,o){this.logger.debug(u,l,"getPageGlyphs",t,o),this.logger.perf(u,l,"getPageGlyphs","Begin",t.id);const i=this.cache.getContext(t.id);if(!i)return this.logger.perf(u,l,"getPageGlyphs","End",t.id),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const n=i.acquirePage(o.index),r=n.getTextPage(),d=this.pdfiumModule.FPDFText_CountChars(r),s=new Array(d);for(let e=0;e<d;e++){const t=this.readGlyphInfo(o,n.pagePtr,r,e);t.isEmpty||(s[e]={...t})}return n.release(),this.logger.perf(u,l,"getPageGlyphs","End",t.id),e.PdfTaskHelper.resolve(s)}readCharBox(e,t,o,i){const n=this.malloc(8),r=this.malloc(8),d=this.malloc(8),s=this.malloc(8);let a=0,u=0,l=0,h=0;if(this.pdfiumModule.FPDFText_GetCharBox(o,i,r,s,d,n)){const o=this.pdfiumModule.pdfium.getValue(n,"double"),i=this.pdfiumModule.pdfium.getValue(r,"double"),f=this.pdfiumModule.pdfium.getValue(d,"double"),c=this.pdfiumModule.pdfium.getValue(s,"double"),g=this.malloc(4),p=this.malloc(4);this.pdfiumModule.FPDF_PageToDevice(t,0,0,e.size.width,e.size.height,0,i,o,g,p),a=this.pdfiumModule.pdfium.getValue(g,"i32"),u=this.pdfiumModule.pdfium.getValue(p,"i32"),this.free(g),this.free(p),l=Math.ceil(Math.abs(c-i)),h=Math.ceil(Math.abs(o-f))}return this.free(n),this.free(r),this.free(d),this.free(s),{origin:{x:a,y:u},size:{width:l,height:h}}}readPageAnnotations(e,t){const o=e.acquirePage(t.index),i=this.pdfiumModule.FPDFPage_GetAnnotCount(o.pagePtr),n=[];for(let r=0;r<i;r++)o.withAnnotation(r,(e=>{const i=this.readPageAnnotation(t,o,e,r);i&&n.push(i)}));return n}readPageAnnotation(t,o,i,n){const r=this.pdfiumModule.FPDFAnnot_GetSubtype(i);let d;switch(r){case e.PdfAnnotationSubtype.TEXT:d=this.readPdfTextAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.FREETEXT:d=this.readPdfFreeTextAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.LINK:d=this.readPdfLinkAnno(t,o.docPtr,o.pagePtr,o.getTextPage(),i,n);break;case e.PdfAnnotationSubtype.WIDGET:d=this.readPdfWidgetAnno(t,o.pagePtr,i,o.getFormHandle(),n);break;case e.PdfAnnotationSubtype.FILEATTACHMENT:d=this.readPdfFileAttachmentAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.INK:d=this.readPdfInkAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.POLYGON:d=this.readPdfPolygonAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.POLYLINE:d=this.readPdfPolylineAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.LINE:d=this.readPdfLineAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.HIGHLIGHT:d=this.readPdfHighlightAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.STAMP:d=this.readPdfStampAnno(o.docPtr,t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.SQUARE:d=this.readPdfSquareAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.CIRCLE:d=this.readPdfCircleAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.UNDERLINE:d=this.readPdfUnderlineAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.SQUIGGLY:d=this.readPdfSquigglyAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.STRIKEOUT:d=this.readPdfStrikeOutAnno(t,o.pagePtr,i,n);break;case e.PdfAnnotationSubtype.CARET:d=this.readPdfCaretAnno(t,o.pagePtr,i,n);break;default:d=this.readPdfAnno(t,o.pagePtr,r,i,n)}return d}readAnnotationColor(t,o=e.PdfAnnotationColorType.Color){const i=this.malloc(4),n=this.malloc(4),r=this.malloc(4),d=this.malloc(4);let s;return this.pdfiumModule.EPDFAnnot_GetColor(t,o,i,n,r,d)&&(s={red:255&this.pdfiumModule.pdfium.getValue(i,"i32"),green:255&this.pdfiumModule.pdfium.getValue(n,"i32"),blue:255&this.pdfiumModule.pdfium.getValue(r,"i32"),alpha:255&this.pdfiumModule.pdfium.getValue(d,"i32")}),this.free(i),this.free(n),this.free(r),this.free(d),s}resolveAnnotationColor(t,o=e.PdfAnnotationColorType.Color,i={red:255,green:245,blue:155,alpha:255}){const n=this.readAnnotationColor(t,o)??i;return e.pdfAlphaColorToWebAlphaColor(n)}setAnnotationColor(t,o,i=e.PdfAnnotationColorType.Color){const n=e.webAlphaColorToPdfAlphaColor(o);return this.pdfiumModule.EPDFAnnot_SetColor(t,i,255&n.red,255&n.green,255&n.blue,255&(n.alpha??255))}getBorderStyle(t){const o=this.malloc(4);let i=0,n=e.PdfAnnotationBorderStyle.UNKNOWN,r=!1;return n=this.pdfiumModule.EPDFAnnot_GetBorderStyle(t,o),i=this.pdfiumModule.pdfium.getValue(o,"float"),r=n!==e.PdfAnnotationBorderStyle.UNKNOWN,this.free(o),{ok:r,style:n,width:i}}setBorderStyle(e,t,o){return this.pdfiumModule.EPDFAnnot_SetBorderStyle(e,t,o)}getBorderEffect(e){const t=this.malloc(4),o=!!this.pdfiumModule.EPDFAnnot_GetBorderEffect(e,t),i=o?this.pdfiumModule.pdfium.getValue(t,"float"):0;return this.free(t),{ok:o,intensity:i}}getRectangleDifferences(e){const t=this.malloc(4),o=this.malloc(4),i=this.malloc(4),n=this.malloc(4),r=!!this.pdfiumModule.EPDFAnnot_GetRectangleDifferences(e,t,o,i,n),d=this.pdfiumModule.pdfium,s=d.getValue(t,"float"),a=d.getValue(o,"float"),u=d.getValue(i,"float"),l=d.getValue(n,"float");return this.free(t),this.free(o),this.free(i),this.free(n),{ok:r,left:s,top:a,right:u,bottom:l}}getBorderDashPattern(e){const t=this.pdfiumModule.EPDFAnnot_GetBorderDashPatternCount(e);if(0===t)return{ok:!1,pattern:[]};const o=this.malloc(4*t),i=!!this.pdfiumModule.EPDFAnnot_GetBorderDashPattern(e,o,t),n=[];if(i){const e=this.pdfiumModule.pdfium;for(let i=0;i<t;i++)n.push(e.getValue(o+4*i,"float"))}return this.free(o),{ok:i,pattern:n}}getQuadPointsAnno(t,o){const i=this.pdfiumModule.FPDFAnnot_CountAttachmentPoints(o);if(0===i)return[];const n=[];for(let e=0;e<i;e++){const i=this.malloc(32);if(this.pdfiumModule.FPDFAnnot_GetAttachmentPoints(o,e,i)){const e=[],o=[];for(let t=0;t<4;t++){const n=i+8*t;e.push(this.pdfiumModule.pdfium.getValue(n,"float")),o.push(this.pdfiumModule.pdfium.getValue(n+4,"float"))}const r=this.convertPagePointToDevicePoint(t,{x:e[0],y:o[0]}),d=this.convertPagePointToDevicePoint(t,{x:e[1],y:o[1]}),s=this.convertPagePointToDevicePoint(t,{x:e[2],y:o[2]}),a=this.convertPagePointToDevicePoint(t,{x:e[3],y:o[3]});n.push({p1:r,p2:d,p3:s,p4:a})}this.free(i)}return n.map(e.quadToRect)}syncQuadPointsAnno(t,o,i){const n=this.pdfiumModule.pdfium,r=this.pdfiumModule.FPDFAnnot_CountAttachmentPoints(o),d=this.malloc(32),s=o=>{const i=e.rectToQuad(o),r=this.convertDevicePointToPagePoint(t,i.p1),s=this.convertDevicePointToPagePoint(t,i.p2),a=this.convertDevicePointToPagePoint(t,i.p3),u=this.convertDevicePointToPagePoint(t,i.p4);n.setValue(d+0,r.x,"float"),n.setValue(d+4,r.y,"float"),n.setValue(d+8,s.x,"float"),n.setValue(d+12,s.y,"float"),n.setValue(d+16,u.x,"float"),n.setValue(d+20,u.y,"float"),n.setValue(d+24,a.x,"float"),n.setValue(d+28,a.y,"float")},a=Math.min(r,i.length);for(let e=0;e<a;e++)if(s(i[e]),!this.pdfiumModule.FPDFAnnot_SetAttachmentPoints(o,e,d))return this.free(d),!1;for(let e=r;e<i.length;e++)if(s(i[e]),!this.pdfiumModule.FPDFAnnot_AppendAttachmentPoints(o,d))return this.free(d),!1;return this.free(d),!0}getInkList(e,t){const o=[],i=this.pdfiumModule.FPDFAnnot_GetInkListCount(t);for(let n=0;n<i;n++){const i=[],r=this.pdfiumModule.FPDFAnnot_GetInkListPath(t,n,0,0);if(r>0){const o=8,d=this.malloc(r*o);this.pdfiumModule.FPDFAnnot_GetInkListPath(t,n,d,r);for(let t=0;t<r;t++){const o=this.pdfiumModule.pdfium.getValue(d+8*t,"float"),n=this.pdfiumModule.pdfium.getValue(d+8*t+4,"float"),{x:r,y:s}=this.convertPagePointToDevicePoint(e,{x:o,y:n});i.push({x:r,y:s})}this.free(d)}o.push({points:i})}return o}setInkList(e,t,o){for(const i of o){const o=i.points.length,n=this.malloc(8*o);for(let t=0;t<o;t++){const o=i.points[t],{x:r,y:d}=this.convertDevicePointToPagePoint(e,o);this.pdfiumModule.pdfium.setValue(n+8*t,r,"float"),this.pdfiumModule.pdfium.setValue(n+8*t+4,d,"float")}if(-1===this.pdfiumModule.FPDFAnnot_AddInkStroke(t,n,o))return this.free(n),!1;this.free(n)}return!0}readPdfTextAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.getAnnotString(i,"Contents")||"",h=this.getAnnotString(i,"State"),f=this.getAnnotString(i,"StateModel"),c=this.resolveAnnotationColor(i),g=this.getInReplyToId(o,i);return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.TEXT,contents:l,...c,rect:d,inReplyToId:g,author:s,modified:u,state:h,stateModel:f}}readPdfFreeTextAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"Contents")||"",a=this.getAnnotString(i,"T"),u=this.getAnnotString(i,"M"),l=e.pdfDateToDate(u),h=this.getAnnotRichContent(i);return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.FREETEXT,richContent:h,contents:s,author:a,modified:l,rect:d}}readPdfLinkAnno(t,o,i,n,r,d){const s=this.pdfiumModule.FPDFAnnot_GetLink(r);if(!s)return;const a=this.readPageAnnoRect(r),{left:u,top:l,right:h,bottom:f}=a,c=this.convertPageRectToDeviceRect(t,i,a),g=this.getAnnotString(r,"T"),p=this.getAnnotString(r,"M"),m=e.pdfDateToDate(p),P=this.pdfiumModule.FPDFText_GetBoundedText(n,u,l,h,f,0,0),F=2*(P+1),M=this.malloc(F);this.pdfiumModule.FPDFText_GetBoundedText(n,u,l,h,f,M,P);const A=this.pdfiumModule.pdfium.UTF16ToString(M);this.free(M);const D=this.readPdfLinkAnnoTarget(o,(()=>this.pdfiumModule.FPDFLink_GetAction(s)),(()=>this.pdfiumModule.FPDFLink_GetDest(o,s)));return{pageIndex:t.index,id:d,type:e.PdfAnnotationSubtype.LINK,text:A,target:D,rect:c,author:g,modified:m}}readPdfWidgetAnno(t,o,i,n,r){const d=this.readPageAnnoRect(i),s=this.convertPageRectToDeviceRect(t,o,d),a=this.getAnnotString(i,"T"),u=this.getAnnotString(i,"M"),l=e.pdfDateToDate(u),h=this.readPdfWidgetAnnoField(n,i);return{pageIndex:t.index,id:r,type:e.PdfAnnotationSubtype.WIDGET,rect:s,field:h,author:a,modified:l}}readPdfFileAttachmentAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a);return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.FILEATTACHMENT,rect:d,author:s,modified:u}}readPdfInkAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.resolveAnnotationColor(i),{width:h}=this.getBorderStyle(i),f=this.getInkList(t,i),c=this.pdfiumModule.EPDFAnnot_GetBlendMode(i),g=this.getAnnotIntent(i);return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.INK,...g&&{intent:g},blendMode:c,...l,strokeWidth:h,rect:d,inkList:f,author:s,modified:u}}readPdfPolygonAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.readPdfAnnoVertices(t,o,i);return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.POLYGON,rect:d,vertices:l,author:s,modified:u}}readPdfPolylineAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.readPdfAnnoVertices(t,o,i);return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.POLYLINE,rect:d,vertices:l,author:s,modified:u}}readPdfLineAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.malloc(8),h=this.malloc(8);this.pdfiumModule.FPDFAnnot_GetLine(i,l,h);const f=this.pdfiumModule.pdfium.getValue(l,"float"),c=this.pdfiumModule.pdfium.getValue(l+4,"float"),g=this.convertPagePointToDevicePoint(t,{x:f,y:c}),p=this.pdfiumModule.pdfium.getValue(h,"float"),m=this.pdfiumModule.pdfium.getValue(h+4,"float"),P=this.convertPagePointToDevicePoint(t,{x:p,y:m});return this.free(l),this.free(h),{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.LINE,rect:d,startPoint:g,endPoint:P,author:s,modified:u}}readPdfHighlightAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getQuadPointsAnno(t,i),a=this.resolveAnnotationColor(i),u=this.pdfiumModule.EPDFAnnot_GetBlendMode(i),l=this.getAnnotString(i,"T"),h=this.getAnnotString(i,"M"),f=e.pdfDateToDate(h),c=this.getAnnotString(i,"Contents")||"";return{pageIndex:t.index,id:n,blendMode:u,type:e.PdfAnnotationSubtype.HIGHLIGHT,rect:d,contents:c,segmentRects:s,...a,author:l,modified:f}}readPdfUnderlineAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.getQuadPointsAnno(t,i),h=this.getAnnotString(i,"Contents")||"",f=this.resolveAnnotationColor(i),c=this.pdfiumModule.EPDFAnnot_GetBlendMode(i);return{pageIndex:t.index,id:n,blendMode:c,type:e.PdfAnnotationSubtype.UNDERLINE,rect:d,contents:h,segmentRects:l,...f,author:s,modified:u}}readPdfStrikeOutAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.getQuadPointsAnno(t,i),h=this.getAnnotString(i,"Contents")||"",f=this.resolveAnnotationColor(i),c=this.pdfiumModule.EPDFAnnot_GetBlendMode(i);return{pageIndex:t.index,id:n,blendMode:c,type:e.PdfAnnotationSubtype.STRIKEOUT,rect:d,contents:h,segmentRects:l,...f,author:s,modified:u}}readPdfSquigglyAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a),l=this.getQuadPointsAnno(t,i),h=this.getAnnotString(i,"Contents")||"",f=this.resolveAnnotationColor(i),c=this.pdfiumModule.EPDFAnnot_GetBlendMode(i);return{pageIndex:t.index,id:n,blendMode:c,type:e.PdfAnnotationSubtype.SQUIGGLY,rect:d,contents:h,segmentRects:l,...f,author:s,modified:u}}readPdfCaretAnno(t,o,i,n){const r=this.readPageAnnoRect(i),d=this.convertPageRectToDeviceRect(t,o,r),s=this.getAnnotString(i,"T"),a=this.getAnnotString(i,"M"),u=e.pdfDateToDate(a);return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.CARET,rect:d,author:s,modified:u}}readPdfStampAnno(t,o,i,n,r){const d=this.readPageAnnoRect(n),s=this.convertPageRectToDeviceRect(o,i,d),a=this.getAnnotString(n,"T"),u=this.getAnnotString(n,"M"),l=e.pdfDateToDate(u),h=[],f=this.pdfiumModule.FPDFAnnot_GetObjectCount(n);for(let e=0;e<f;e++){const t=this.pdfiumModule.FPDFAnnot_GetObject(n,e),o=this.readPdfPageObject(t);o&&h.push(o)}return{pageIndex:o.index,id:r,type:e.PdfAnnotationSubtype.STAMP,rect:s,contents:h,author:a,modified:l}}readPdfPageObject(t){switch(this.pdfiumModule.FPDFPageObj_GetType(t)){case e.PdfPageObjectType.PATH:return this.readPathObject(t);case e.PdfPageObjectType.IMAGE:return this.readImageObject(t);case e.PdfPageObjectType.FORM:return this.readFormObject(t)}}readPathObject(t){const o=this.pdfiumModule.FPDFPath_CountSegments(t),i=this.malloc(4),n=this.malloc(4),r=this.malloc(4),d=this.malloc(4);this.pdfiumModule.FPDFPageObj_GetBounds(t,i,n,r,d);const s={left:this.pdfiumModule.pdfium.getValue(i,"float"),bottom:this.pdfiumModule.pdfium.getValue(n,"float"),right:this.pdfiumModule.pdfium.getValue(r,"float"),top:this.pdfiumModule.pdfium.getValue(d,"float")};this.free(i),this.free(n),this.free(r),this.free(d);const a=[];for(let e=0;e<o;e++){const o=this.readPdfSegment(t,e);a.push(o)}const u=this.readPdfPageObjectTransformMatrix(t);return{type:e.PdfPageObjectType.PATH,bounds:s,segments:a,matrix:u}}readPdfSegment(e,t){const o=this.pdfiumModule.FPDFPath_GetPathSegment(e,t),i=this.pdfiumModule.FPDFPathSegment_GetType(o),n=this.pdfiumModule.FPDFPathSegment_GetClose(o),r=this.malloc(4),d=this.malloc(4);this.pdfiumModule.FPDFPathSegment_GetPoint(o,r,d);const s=this.pdfiumModule.pdfium.getValue(r,"float"),a=this.pdfiumModule.pdfium.getValue(d,"float");return this.free(r),this.free(d),{type:i,point:{x:s,y:a},isClosed:n}}readImageObject(t){const o=this.pdfiumModule.FPDFImageObj_GetBitmap(t),i=this.pdfiumModule.FPDFBitmap_GetBuffer(o),n=this.pdfiumModule.FPDFBitmap_GetWidth(o),r=this.pdfiumModule.FPDFBitmap_GetHeight(o),d=this.pdfiumModule.FPDFBitmap_GetFormat(o),s=n*r,a=new Uint8ClampedArray(4*s);for(let e=0;e<s;e++)if(2===d){const t=this.pdfiumModule.pdfium.getValue(i+3*e,"i8"),o=this.pdfiumModule.pdfium.getValue(i+3*e+1,"i8"),n=this.pdfiumModule.pdfium.getValue(i+3*e+2,"i8");a[4*e]=n,a[4*e+1]=o,a[4*e+2]=t,a[4*e+3]=100}const u=new ImageData(a,n,r),l=this.readPdfPageObjectTransformMatrix(t);return{type:e.PdfPageObjectType.IMAGE,imageData:u,matrix:l}}readFormObject(t){const o=this.pdfiumModule.FPDFFormObj_CountObjects(t),i=[];for(let e=0;e<o;e++){const o=this.pdfiumModule.FPDFFormObj_GetObject(t,e),n=this.readPdfPageObject(o);n&&i.push(n)}const n=this.readPdfPageObjectTransformMatrix(t);return{type:e.PdfPageObjectType.FORM,objects:i,matrix:n}}readPdfPageObjectTransformMatrix(e){const t=this.malloc(24);if(this.pdfiumModule.FPDFPageObj_GetMatrix(e,t)){const e=this.pdfiumModule.pdfium.getValue(t,"float"),o=this.pdfiumModule.pdfium.getValue(t+4,"float"),i=this.pdfiumModule.pdfium.getValue(t+8,"float"),n=this.pdfiumModule.pdfium.getValue(t+12,"float"),r=this.pdfiumModule.pdfium.getValue(t+16,"float"),d=this.pdfiumModule.pdfium.getValue(t+20,"float");return this.free(t),{a:e,b:o,c:i,d:n,e:r,f:d}}return this.free(t),{a:1,b:0,c:0,d:1,e:0,f:0}}getStrokeWidth(e){const t=this.malloc(4),o=this.malloc(4),i=this.malloc(4),n=this.pdfiumModule.FPDFAnnot_GetBorder(e,t,o,i)?this.pdfiumModule.pdfium.getValue(i,"float"):1;return this.free(t),this.free(o),this.free(i),n}getAnnotationFlags(t){const o=this.pdfiumModule.FPDFAnnot_GetFlags(t);return e.flagsToNames(o)}readPdfCircleAnno(t,o,i,n){const r=this.getAnnotationFlags(i),d=this.readPageAnnoRect(i),s=this.convertPageRectToDeviceRect(t,o,d),a=this.getAnnotString(i,"T"),u=this.getAnnotString(i,"M"),l=e.pdfDateToDate(u),{color:h,opacity:f}=this.resolveAnnotationColor(i,e.PdfAnnotationColorType.InteriorColor),{color:c}=this.resolveAnnotationColor(i);let g,p,m,{style:P,width:F}=this.getBorderStyle(i);if(P===e.PdfAnnotationBorderStyle.CLOUDY||P===e.PdfAnnotationBorderStyle.UNKNOWN){const{ok:t,intensity:o}=this.getBorderEffect(i);if(t){g=o,P=e.PdfAnnotationBorderStyle.CLOUDY;const{ok:t,left:n,top:r,right:d,bottom:s}=this.getRectangleDifferences(i);t&&(p=[n,r,d,s])}}if(P===e.PdfAnnotationBorderStyle.DASHED){const{ok:e,pattern:t}=this.getBorderDashPattern(i);e&&(m=t)}return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.CIRCLE,flags:r,color:h,opacity:f,strokeWidth:F,strokeColor:c,strokeStyle:P,rect:s,author:a,modified:l,...void 0!==g&&{cloudyBorderIntensity:g},...void 0!==p&&{cloudyBorderInset:p},...void 0!==m&&{strokeDashArray:m}}}readPdfSquareAnno(t,o,i,n){const r=this.getAnnotationFlags(i),d=this.readPageAnnoRect(i),s=this.convertPageRectToDeviceRect(t,o,d),a=this.getAnnotString(i,"T"),u=this.getAnnotString(i,"M"),l=e.pdfDateToDate(u),{color:h,opacity:f}=this.resolveAnnotationColor(i,e.PdfAnnotationColorType.InteriorColor),{color:c}=this.resolveAnnotationColor(i);let g,p,m,{style:P,width:F}=this.getBorderStyle(i);if(P===e.PdfAnnotationBorderStyle.CLOUDY||P===e.PdfAnnotationBorderStyle.UNKNOWN){const{ok:t,intensity:o}=this.getBorderEffect(i);if(t){g=o,P=e.PdfAnnotationBorderStyle.CLOUDY;const{ok:t,left:n,top:r,right:d,bottom:s}=this.getRectangleDifferences(i);t&&(p=[n,r,d,s])}}if(P===e.PdfAnnotationBorderStyle.DASHED){const{ok:e,pattern:t}=this.getBorderDashPattern(i);e&&(m=t)}return{pageIndex:t.index,id:n,type:e.PdfAnnotationSubtype.SQUARE,flags:r,color:h,opacity:f,strokeColor:c,strokeWidth:F,strokeStyle:P,rect:s,author:a,modified:l,...void 0!==g&&{cloudyBorderIntensity:g},...void 0!==p&&{cloudyBorderInset:p},...void 0!==m&&{strokeDashArray:m}}}readPdfAnno(t,o,i,n,r){const d=this.readPageAnnoRect(n),s=this.convertPageRectToDeviceRect(t,o,d),a=this.getAnnotString(n,"T"),u=this.getAnnotString(n,"M"),l=e.pdfDateToDate(u);return{pageIndex:t.index,id:r,type:i,rect:s,author:a,modified:l}}getInReplyToId(e,t){const o=this.pdfiumModule.FPDFAnnot_GetLinkedAnnot(t,"IRT");if(!o)return;const i=this.pdfiumModule.FPDFPage_GetAnnotIndex(e,o);return i>=0?i:void 0}getAnnotString(e,t){const o=this.pdfiumModule.FPDFAnnot_GetStringValue(e,t,0,0);if(0===o)return;const i=2*(o+1),n=this.malloc(i);this.pdfiumModule.FPDFAnnot_GetStringValue(e,t,n,i);const r=this.pdfiumModule.pdfium.UTF16ToString(n);return this.free(n),r||void 0}getAnnotIntent(e){const t=this.pdfiumModule.EPDFAnnot_GetIntent(e,0,0);if(0===t)return;const o=2*(t+1),i=this.malloc(o);this.pdfiumModule.EPDFAnnot_GetIntent(e,i,o);const n=this.pdfiumModule.pdfium.UTF16ToString(i);return this.free(i),n&&"undefined"!==n?n:void 0}getAnnotRichContent(e){const t=this.pdfiumModule.EPDFAnnot_GetRichContent(e,0,0);if(0===t)return;const o=2*(t+1),i=this.malloc(o);this.pdfiumModule.EPDFAnnot_GetRichContent(e,i,o);const n=this.pdfiumModule.pdfium.UTF16ToString(i);return this.free(i),n||void 0}setAnnotString(e,t,o){const i=2*(o.length+1),n=this.malloc(i);this.pdfiumModule.pdfium.stringToUTF16(o,n,i);const r=this.pdfiumModule.FPDFAnnot_SetStringValue(e,t,n);return this.free(n),r}readPdfAnnoVertices(e,t,o){const i=[],n=this.pdfiumModule.FPDFAnnot_GetVertices(o,0,0),r=this.malloc(8*n);this.pdfiumModule.FPDFAnnot_GetVertices(o,r,n);for(let d=0;d<n;d++){const t=this.pdfiumModule.pdfium.getValue(r+8*d,"float"),o=this.pdfiumModule.pdfium.getValue(r+8*d+4,"float"),{x:n,y:s}=this.convertPagePointToDevicePoint(e,{x:t,y:o});i.push({x:n,y:s})}return this.free(r),i}readPdfBookmarkTarget(e,t,o){const i=t();if(i){return{type:"action",action:this.readPdfAction(e,i)}}{const t=o();if(t){return{type:"destination",destination:this.readPdfDestination(e,t)}}}}readPdfWidgetAnnoField(o,i){const n=this.pdfiumModule.FPDFAnnot_GetFormFieldFlags(o,i),r=this.pdfiumModule.FPDFAnnot_GetFormFieldType(o,i),d=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAnnot_GetFormFieldName(o,i,e,t)),this.pdfiumModule.pdfium.UTF16ToString),s=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAnnot_GetFormFieldAlternateName(o,i,e,t)),this.pdfiumModule.pdfium.UTF16ToString),a=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAnnot_GetFormFieldValue(o,i,e,t)),this.pdfiumModule.pdfium.UTF16ToString),u=[];if(r===e.PDF_FORM_FIELD_TYPE.COMBOBOX||r===e.PDF_FORM_FIELD_TYPE.LISTBOX){const e=this.pdfiumModule.FPDFAnnot_GetOptionCount(o,i);for(let n=0;n<e;n++){const e=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAnnot_GetOptionLabel(o,i,n,e,t)),this.pdfiumModule.pdfium.UTF16ToString),r=this.pdfiumModule.FPDFAnnot_IsOptionSelected(o,i,n);u.push({label:e,isSelected:r})}}let l=!1;return r!==e.PDF_FORM_FIELD_TYPE.CHECKBOX&&r!==e.PDF_FORM_FIELD_TYPE.RADIOBUTTON||(l=this.pdfiumModule.FPDFAnnot_IsChecked(o,i)),{flag:n,type:r,name:d,alternateName:s,value:a,isChecked:l,options:u}}renderAnnotation(t,o,i,n,r,d=1,s=e.AppearanceMode.Normal,a="image/webp"){this.logger.debug(u,l,"renderAnnotation",t,o,i,n,r,d,s,a),this.logger.perf(u,l,"RenderAnnotation","Begin",`${t.id}-${o.index}-${i.id}`);const h=new e.Task,f=this.cache.getContext(t.id);if(!f)return this.logger.perf(u,l,"RenderAnnotation","End",`${t.id}-${o.index}-${i.id}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.DocNotOpen,message:"document does not open"});const c=f.acquirePage(o.index),g=this.pdfiumModule.FPDFPage_GetAnnot(c.pagePtr,i.id);if(!g)return c.release(),this.logger.perf(u,l,"RenderAnnotation","End",`${t.id}-${o.index}-${i.id}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"annotation not found"});const p=n*d,m=i.rect,P=e.toIntRect(e.transformRect(o.size,m,r,p)),F=P.size.width*P.size.height*4,M=this.malloc(F),A=this.pdfiumModule.FPDFBitmap_CreateEx(P.size.width,P.size.height,4,M,4*P.size.width);this.pdfiumModule.FPDFBitmap_FillRect(A,0,0,P.size.width,P.size.height,0);const D=e.makeMatrix(i.rect,r,p),T=this.malloc(24);new Float32Array(this.pdfiumModule.pdfium.HEAPF32.buffer,T,6).set([D.a,D.b,D.c,D.d,D.e,D.f]);const x=!!this.pdfiumModule.EPDF_RenderAnnotBitmap(A,c.pagePtr,g,s,T,16);if(this.free(T),this.pdfiumModule.FPDFBitmap_Destroy(A),this.pdfiumModule.FPDFPage_CloseAnnot(g),c.release(),!x)return this.free(M),this.logger.perf(u,l,"RenderAnnotation","End",`${t.id}-${o.index}-${i.id}`),e.PdfTaskHelper.reject({code:e.PdfErrorCode.Unknown,message:"EPDF_RenderAnnotBitmap failed"});const C=this.pdfiumModule.pdfium.HEAPU8.subarray(M,M+F),E={data:new Uint8ClampedArray(C),width:P.size.width,height:P.size.height};return this.free(M),this.logger.perf(u,l,"RenderAnnotation","End",`${t.id}-${o.index}-${i.id}`),this.imageDataConverter(E,a).then((e=>h.resolve(e))).catch((t=>h.reject({code:e.PdfErrorCode.Unknown,message:String(t)}))),h}renderPageRectToImageData(t,o,i,n,r,d,s){const a=e.toIntRect(e.transformRect(o.size,i,r,n*d)),u=e.toIntSize(e.transformSize(o.size,r,n*d)),l=a.size.width*a.size.height*4,h=this.malloc(l),f=this.pdfiumModule.FPDFBitmap_CreateEx(a.size.width,a.size.height,4,h,4*a.size.width);this.pdfiumModule.FPDFBitmap_FillRect(f,0,0,a.size.width,a.size.height,4294967295);let c=16;(null==s?void 0:s.withAnnotations)&&(c|=1);const g=t.acquirePage(o.index);this.pdfiumModule.FPDF_RenderPageBitmap(f,g.pagePtr,-a.origin.x,-a.origin.y,u.width,u.height,r,c),this.pdfiumModule.FPDFBitmap_Destroy(f),g.release();const p=this.pdfiumModule.pdfium.HEAPU8.subarray(h,h+l),m={data:new Uint8ClampedArray(p),width:a.size.width,height:a.size.height};return this.free(h),m}readPdfLinkAnnoTarget(e,t,o){const i=o();if(i){return{type:"destination",destination:this.readPdfDestination(e,i)}}{const o=t();if(o){return{type:"action",action:this.readPdfAction(e,o)}}}}readPdfAction(o,i){let n;switch(this.pdfiumModule.FPDFAction_GetType(i)){case e.PdfActionType.Unsupported:n={type:e.PdfActionType.Unsupported};break;case e.PdfActionType.Goto:{const t=this.pdfiumModule.FPDFAction_GetDest(o,i);if(t){const i=this.readPdfDestination(o,t);n={type:e.PdfActionType.Goto,destination:i}}else n={type:e.PdfActionType.Unsupported}}break;case e.PdfActionType.RemoteGoto:n={type:e.PdfActionType.Unsupported};break;case e.PdfActionType.URI:{const r=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAction_GetURIPath(o,i,e,t)),this.pdfiumModule.pdfium.UTF8ToString);n={type:e.PdfActionType.URI,uri:r}}break;case e.PdfActionType.LaunchAppOrOpenFile:{const o=t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAction_GetFilePath(i,e,t)),this.pdfiumModule.pdfium.UTF8ToString);n={type:e.PdfActionType.LaunchAppOrOpenFile,path:o}}}return n}readPdfDestination(t,o){const i=this.pdfiumModule.FPDFDest_GetDestPageIndex(t,o),n=this.malloc(4),r=this.malloc(16),d=this.pdfiumModule.FPDFDest_GetView(o,n,r),s=this.pdfiumModule.pdfium.getValue(n,"i32"),a=[];for(let e=0;e<s;e++){const t=r+4*e;a.push(this.pdfiumModule.pdfium.getValue(t,"float"))}if(this.free(n),this.free(r),d===e.PdfZoomMode.XYZ){const e=this.malloc(1),t=this.malloc(1),n=this.malloc(1),r=this.malloc(4),s=this.malloc(4),u=this.malloc(4);if(this.pdfiumModule.FPDFDest_GetLocationInPage(o,e,t,n,r,s,u)){const o=this.pdfiumModule.pdfium.getValue(e,"i8"),l=this.pdfiumModule.pdfium.getValue(t,"i8"),h=this.pdfiumModule.pdfium.getValue(n,"i8"),f=o?this.pdfiumModule.pdfium.getValue(r,"float"):0,c=l?this.pdfiumModule.pdfium.getValue(s,"float"):0,g=h?this.pdfiumModule.pdfium.getValue(u,"float"):0;return this.free(e),this.free(t),this.free(n),this.free(r),this.free(s),this.free(u),{pageIndex:i,zoom:{mode:d,params:{x:f,y:c,zoom:g}},view:a}}return this.free(e),this.free(t),this.free(n),this.free(r),this.free(s),this.free(u),{pageIndex:i,zoom:{mode:d,params:{x:0,y:0,zoom:0}},view:a}}return{pageIndex:i,zoom:{mode:d},view:a}}readPdfAttachment(e,o){const i=this.pdfiumModule.FPDFDoc_GetAttachment(e,o);return{index:o,name:t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAttachment_GetName(i,e,t)),this.pdfiumModule.pdfium.UTF16ToString),creationDate:t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAttachment_GetStringValue(i,"CreationDate",e,t)),this.pdfiumModule.pdfium.UTF16ToString),checksum:t(this.pdfiumModule.pdfium,((e,t)=>this.pdfiumModule.FPDFAttachment_GetStringValue(i,"Checksum",e,t)),this.pdfiumModule.pdfium.UTF16ToString)}}convertDevicePointToPagePoint(e,t){return{x:t.x,y:e.size.height-t.y}}convertPagePointToDevicePoint(e,t){return{x:t.x,y:e.size.height-t.y}}convertPageRectToDeviceRect(e,t,o){const{x:i,y:n}=this.convertPagePointToDevicePoint(e,{x:o.left,y:o.top});return{origin:{x:i,y:n},size:{width:Math.abs(o.right-o.left),height:Math.abs(o.top-o.bottom)}}}readPageAnnoAppearanceStreams(t){return{normal:this.readPageAnnoAppearanceStream(t,e.AppearanceMode.Normal),rollover:this.readPageAnnoAppearanceStream(t,e.AppearanceMode.Rollover),down:this.readPageAnnoAppearanceStream(t,e.AppearanceMode.Down)}}readPageAnnoAppearanceStream(t,o=e.AppearanceMode.Normal){const i=2*(this.pdfiumModule.FPDFAnnot_GetAP(t,o,0,0)+1),n=this.malloc(i);this.pdfiumModule.FPDFAnnot_GetAP(t,o,n,i);const r=this.pdfiumModule.pdfium.UTF16ToString(n);return this.free(n),r}updateAnnotationColor(t,o,i,n,r=0){this.logger.debug(u,l,"setAnnotationColor",t,o,i,n,r),this.logger.perf(u,l,"setAnnotationColor","Begin",t.id);const d=e.PdfTaskHelper.create();try{const e=this.cache.getContext(t.id);if(!e)return this.logger.perf(u,l,"setAnnotationColor","End",t.id),this.logger.warn(u,l,"setAnnotationColor: doc closed"),d.resolve(!1),d;const s=e.acquirePage(o.index),a=this.pdfiumModule.FPDFPage_GetAnnot(s.pagePtr,i.id);if(!a)return this.logger.perf(u,l,"setAnnotationColor","End",t.id),this.logger.warn(u,l,"setAnnotationColor: annot not found"),s.release(),d.resolve(!1),d;const h=this.setAnnotationColor(a,n,r);h&&this.pdfiumModule.FPDFPage_GenerateContent(s.pagePtr),this.pdfiumModule.FPDFPage_CloseAnnot(a),s.release(),this.logger.perf(u,l,"setAnnotationColor","End",t.id),d.resolve(!!h)}catch(s){this.logger.perf(u,l,"setAnnotationColor","End",t.id),this.logger.error(u,l,"setAnnotationColor: error",s),d.reject({code:e.PdfErrorCode.Unknown,message:`Failed to set annotation color: ${s instanceof Error?s.message:String(s)}`})}return d}setPageAnnoRect(e,t,o,i){const n=this.malloc(8),r=this.malloc(8);if(!this.pdfiumModule.FPDF_DeviceToPage(t,0,0,e.size.width,e.size.height,0,i.origin.x,i.origin.y,n,r))return this.free(n),this.free(r),!1;const d=this.pdfiumModule.pdfium.getValue(n,"double"),s=this.pdfiumModule.pdfium.getValue(r,"double");this.free(n),this.free(r);const a=this.malloc(16);return this.pdfiumModule.pdfium.setValue(a,d,"float"),this.pdfiumModule.pdfium.setValue(a+4,s,"float"),this.pdfiumModule.pdfium.setValue(a+8,d+i.size.width,"float"),this.pdfiumModule.pdfium.setValue(a+12,s-i.size.height,"float"),this.pdfiumModule.FPDFAnnot_SetRect(o,a)?(this.free(a),!0):(this.free(a),!1)}readPageAnnoRect(e){const t=this.malloc(16),o={left:0,top:0,right:0,bottom:0};return this.pdfiumModule.FPDFAnnot_GetRect(e,t)&&(o.left=this.pdfiumModule.pdfium.getValue(t,"float"),o.top=this.pdfiumModule.pdfium.getValue(t+4,"float"),o.right=this.pdfiumModule.pdfium.getValue(t+8,"float"),o.bottom=this.pdfiumModule.pdfium.getValue(t+12,"float")),this.free(t),o}getHighlightRects(e,t,o,i,n){const r=this.pdfiumModule.FPDFText_CountRects(o,i,n),d=[];for(let s=0;s<r;s++){const i=this.malloc(8),n=this.malloc(8),r=this.malloc(8),a=this.malloc(8);if(!this.pdfiumModule.FPDFText_GetRect(o,s,n,i,r,a)){this.free(n),this.free(i),this.free(r),this.free(a);continue}const u=this.pdfiumModule.pdfium.getValue(n,"double"),l=this.pdfiumModule.pdfium.getValue(i,"double"),h=this.pdfiumModule.pdfium.getValue(r,"double"),f=this.pdfiumModule.pdfium.getValue(a,"double");this.free(n),this.free(i),this.free(r),this.free(a);const c=this.malloc(4),g=this.malloc(4);this.pdfiumModule.FPDF_PageToDevice(t,0,0,e.size.width,e.size.height,0,u,l,c,g);const p=this.pdfiumModule.pdfium.getValue(c,"i32"),m=this.pdfiumModule.pdfium.getValue(g,"i32");this.free(c),this.free(g);const P=Math.ceil(Math.abs(h-u)),F=Math.ceil(Math.abs(l-f));d.push({origin:{x:p,y:m},size:{width:P,height:F}})}return d}searchAllPages(t,o,i=[]){this.logger.debug(u,l,"searchAllPages",t,o,i),this.logger.perf(u,l,"SearchAllPages","Begin",t.id);const n=this.cache.getContext(t.id);if(!n)return this.logger.perf(u,l,"SearchAllPages","End",t.id),e.PdfTaskHelper.resolve({results:[],total:0});const r=2*(o.length+1),d=this.malloc(r);this.pdfiumModule.pdfium.stringToUTF16(o,d,r);const s=i.reduce(((e,t)=>e|t),e.MatchFlag.None),a=[],h=e.PdfTaskHelper.create();return(async()=>{for(let e=0;e<t.pageCount;e++){const o=this.searchAllInPage(n,t.pages[e],d,s);a.push(...o)}this.free(d),this.logger.perf(u,l,"SearchAllPages","End",t.id),h.resolve({results:a,total:a.length})})().catch((o=>{this.free(d),this.logger.perf(u,l,"SearchAllPages","End",t.id),h.reject({code:e.PdfErrorCode.Unknown,message:`Error searching document: ${o}`})})),h}buildContext(e,t,o,i=30){const n=/[\s\u00A0.,;:!?()\[\]{}<>/\\\-"'`"”\u2013\u2014]/;let r=t;for(;r>0&&n.test(e[r-1]);)r--;let d=0;for(;r>0&&d<i;)r--,n.test(e[r])||d++;r=(t=>{for(;t>0&&!n.test(e[t-1]);)t--;return t})(r);let s=t+o;for(;s<e.length&&n.test(e[s]);)s++;for(d=0;s<e.length&&d<i;)n.test(e[s])||d++,s++;s=(t=>{for(;t<e.length&&!n.test(e[t]);)t++;return t})(s);const a=e.slice(r,t).replace(/\s+/g," ").trimStart(),u=e.slice(t,t+o),l=e.slice(t+o,s).replace(/\s+/g," ").trimEnd();return{before:this.tidy(a),match:this.tidy(u),after:this.tidy(l),truncatedLeft:r>0,truncatedRight:s<e.length}}tidy(e){return e.replace(/-\uFFFE\s*/g,"").replace(/[\uFFFE\u00AD\u200B\u2060\uFEFF]/g,"").replace(/\s+/g," ")}searchAllInPage(e,t,o,i){const n=t.index,r=e.acquirePage(n),d=r.getTextPage(),s=this.pdfiumModule.FPDFText_CountChars(d),a=this.malloc(2*(s+1));this.pdfiumModule.FPDFText_GetText(d,0,s,a);const u=this.pdfiumModule.pdfium.UTF16ToString(a);this.free(a);const l=[],h=this.pdfiumModule.FPDFText_FindStart(d,o,i,0);for(;this.pdfiumModule.FPDFText_FindNext(h);){const e=this.pdfiumModule.FPDFText_GetSchResultIndex(h),o=this.pdfiumModule.FPDFText_GetSchCount(h),i=this.getHighlightRects(t,r.pagePtr,d,e,o),s=this.buildContext(u,e,o);l.push({pageIndex:n,charIndex:e,charCount:o,rects:i,context:s})}return this.pdfiumModule.FPDFText_FindClose(h),r.release(),l}},exports.PdfiumErrorCode=h,exports.RenderFlag=a,exports.browserImageDataToBlobConverter=f,exports.readArrayBuffer=o,exports.readString=t;
|
|
2
|
+
//# sourceMappingURL=engine-B7CS6Qyp.cjs.map
|