1import{f as at}from"./worker-entrypoint-qNcG03R4.js";let C=null;function it(l){return l?{bodySizeThreshold:typeof l.bodySizeThreshold=="number"?l.bodySizeThreshold:void 0,levelThresholds:Array.isArray(l.levelThresholds)&&l.levelThresholds.length===2?[Number(l.levelThresholds[0]),Number(l.levelThresholds[1])]:void 0}:{}}function lt(l){const e=l.filter(n=>n.fontSize>=4&&n.fontSize<=72).map(n=>({size:Math.round(n.fontSize*10)/10,weight:Math.max(n.text.length,1)}));if(e.length===0)return 12;const c=new Map;let t=0;for(const{size:n,weight:i}of e)c.set(n,(c.get(n)??0)+i),t+=i;let a=12,o=0;for(const[n,i]of c)i>o&&(o=i,a=n);if(o<t*.2){const n=[...c.keys()].sort((i,b)=>i-b);return n[Math.floor(n.length/2)]??a}return a}function ct(l){const e=new Set;for(const[c,t]of l){const a=t.length>0?t[0].pageHeight:842,o=a*.03,n=a*.08;for(const i of t){const b=a-i.y,d=i.y;if(b<o||d<o){e.add(i);continue}c>3&&(b<n||d<n)&&e.add(i)}}return e}function O(l){const e=l.replace(/[^a-zA-Z\u00C0-\u024F]/g,"");return e.length>3&&e===e.toUpperCase()}function L(l){const e=l.toUpperCase();return e.includes("BOLD")||e.includes("HEAVY")||e.includes("BLACK")||e.includes("DEMI")}function rt(l){const e=l.trim();return e.length<3||e.length>200||/[.!?]$/.test(e)&&e.length>30||/^[\d\s\-âââ¢*#_]+$/.test(e)?!1:!!(/^(chapter|section|part|appendix|table|figure|introduction|conclusion|references?|index|notes?)\b/i.test(e)||/^(?:[IVXLCDM]+\.\s|[A-Z]\.\s|\d+(?:\.\d+)*[\s.)]|(?:Chapter|Section|Part)\s+\d)/i.test(e)||/^[A-Z\u00C0-\u024F]/.test(e))}function st(l,e,c,t,a){const o=l/e,[n,i]=a??[1.7,1.25];return o>=n?1:o>=i?2:(o>=1.05||(c||t)&&o>=.9,3)}async function ft(l,e){var i,b,d;const c=await import("./pdf-CgeV8iME.js");if(c.GlobalWorkerOptions&&!c.GlobalWorkerOptions.workerSrc)try{const s=await import("./pdf.worker.min-Dk6g9Itu.js");s.default&&(c.GlobalWorkerOptions.workerSrc=s.default)}catch{}const a=await c.getDocument({data:new Uint8Array(l),disableWorker:!0,verbosity:((i=c.VerbosityLevel)==null?void 0:i.ERRORS)??0}).promise,o=[],n=a.numPages;try{for(let s=1;s<=n;s++){const p=await a.getPage(s),S=p.getViewport({scale:1}).height;try{const T=await p.getTextContent();for(const u of T.items){if(!u||typeof u.str!="string")continue;const w=u.str.trim();if(!w||!Array.isArray(u.transform))continue;const k=Math.hypot(u.transform[2],u.transform[3])||Number(u.height)||8;k<4||o.push({text:w,fontSize:k,fontName:u.fontName??"",x:u.transform[4],y:u.transform[5],pageHeight:S,pageIndex:s-1})}}finally{(b=p.cleanup)==null||b.call(p)}e==null||e(s,n)}}finally{await((d=a.destroy)==null?void 0:d.call(a))}return o}function dt(l,e){if(l.length===0)return[];const t=[...l].sort((o,n)=>{if(o.pageIndex!==n.pageIndex)return o.pageIndex-n.pageIndex;const i=n.y-o.y;return Math.abs(i)>e*1.5?i:o.x-n.x}).filter(o=>o.fontSize>=e*1.05||L(o.fontName)||O(o.text)),a=[];for(const o of t){const n=a[a.length-1];if(n&&n[0].pageIndex===o.pageIndex&&Math.abs(n[0].y-o.y)<e*1.5){const i=n[n.length-1];o.x-(i.x+i.text.length*i.fontSize*.4)<e*3?n.push(o):a.push([o])}else a.push([o])}return a.map(o=>({text:o.map(n=>n.text).join(" ").replace(/\s+/g," ").trim(),fontSize:Math.max(...o.map(n=>n.fontSize)),fontName:o[0].fontName,x:Math.min(...o.map(n=>n.x)),y:o[0].y,pageHeight:o[0].pageHeight,pageIndex:o[0].pageIndex}))}function ht(l){const e=[],c=[];for(const t of l){if(!t.enabled)continue;const a={title:t.text,pageIndex:t.pageIndex,y:t.y,level:t.level,children:[]};for(;c.length>0&&c[c.length-1].level>=t.level;)c.pop();c.length>0?c[c.length-1].node.children.push(a):e.push(a),c.push({node:a,level:t.level})}return e}async function ut(l,e,c=0){const{PDFDocument:t,PDFName:a,PDFHexString:o}=await import("./worker-entrypoint-qNcG03R4.js").then(function(T){return T.r}),n=await t.load(l),i=n.context,b=n.getPageCount();function d(T,u){const w=[];let k=null,B=null,F=0;for(let r=0;r<T.length;r++){const h=T[r],v=Math.max(0,Math.min(h.pageIndex+c,b-1)),I=n.getPage(v),z=I.ref,N=I.getSize().height,H=Math.max(0,Math.min(N,N-h.y)),U=i.register(i.obj([z,a.of("XYZ"),null,H,null])),f=i.obj({Title:o.fromText(h.title),Dest:U}),x=i.register(f);if(f.set(a.of("Parent"),u),r>0&&(f.set(a.of("Prev"),w[r-1]),i.lookup(w[r-1]).set(a.of("Next"),x)),h.children.length>0){const y=d(h.children,x);f.set(a.of("First"),y.firstRef),f.set(a.of("Last"),y.lastRef),f.set(a.of("Count"),i.obj(y.totalCount>0?y.totalCount:0)),F+=y.totalCount}w.push(x),k===null&&(k=x),B=x,F++}return{refs:w,firstRef:k,lastRef:B,totalCount:F}}if(e.length===0)throw new Error("No headings to create bookmarks");const s=i.trailerInfo.Root;s&&i.lookup(s).get(a.of("Outlines"))&&console.warn("Existing PDF bookmarks will be replaced by auto-toc");const p=i.obj({Type:a.of("Outlines")}),g=i.register(p),S=d(e,g);return p.set(a.of("First"),S.firstRef),p.set(a.of("Last"),S.lastRef),p.set(a.of("Count"),i.obj(S.totalCount)),s&&i.lookup(s).set(a.of("Outlines"),g),await n.save()}async function K(l){let e=l;if(typeof self<"u"&&self.location&&!l.startsWith("http://")&&!l.startsWith("https://")&&!l.startsWith("blob:")&&!l.startsWith("data:")){const t=self.location.protocol==="blob:"?self.location.origin:self.location.href;e=new URL(l,t).href}if(!e.startsWith("http://")&&!e.startsWith("https://")&&!e.startsWith("blob:")&&!e.startsWith("data:")){const t=await import("./__vite-browser-external-9wXp6ZBx.js"),a=await import("./__vite-browser-external-9wXp6ZBx.js"),o=e.startsWith("file://")?e.slice(7):e,n=a.isAbsolute(o)?o:a.resolve(o),i=t.readFileSync(n);return new Uint8Array(i)}const c=await fetch(e);if(!c.ok)throw new Error(`Failed to load font: ${e}`);return new Uint8Array(await c.arrayBuffer())}async function pt(){var l;if(C!=null&&C.robotoUrl&&(C!=null&&C.robotoBoldUrl))return{normalUrl:C.robotoUrl,boldUrl:C.robotoBoldUrl};if(typeof process<"u"&&((l=process.versions)!=null&&l.node)){const e=await import("./__vite-browser-external-9wXp6ZBx.js");return{normalUrl:e.join(process.cwd(),"public/fonts/Roboto-Regular.ttf"),boldUrl:e.join(process.cwd(),"public/fonts/Roboto-Bold.ttf")}}return{normalUrl:"/fonts/Roboto-Regular.ttf",boldUrl:"/fonts/Roboto-Bold.ttf"}}async function xt(l,e){const{PDFDocument:c,PDFName:t,StandardFonts:a,rgb:o}=await import("./worker-entrypoint-qNcG03R4.js").then(function(f){return f.r}),n=await c.load(l),i=n.context,b=n.getPageCount(),d=n.insertPage(0,[595,842]),s=595,p=842,g=56,S=s-g*2,T=await n.embedFont(a.Helvetica),u=await n.embedFont(a.Helvetica
1Bold);let w=null,k=null;try{n.registerFontkit(at);const f=await pt(),[x,y]=await Promise.all([K(f.normalUrl),K(f.boldUrl)]);w=await n.embedFont(x,{subset:!0}),k=await n.embedFont(y,{subset:!0})}catch(f){console.error("Failed to load Roboto fonts, falling back to Helvetica:",f)}const B=(f,x)=>{var R;try{f.widthOfTextAtSize(x.length>0?x:" ",12)}catch{return!1}const y=(R=f.embedder)==null?void 0:R.font;if(y&&typeof y.hasGlyphForCodePoint=="function")for(let M=0;M<x.length;M++){const m=x.codePointAt(M);if(m!==void 0&&!y.hasGlyphForCodePoint(m))return!1}return!0},F=(f,x)=>{const y=x?[k,u]:[w,T];for(const R of y)if(R&&B(R,f))return R;return x?u:T},r=22,h=11,v=10,I=22;let z=p-g-40;const N="Table of Contents",H=F(N,!0);d.drawText(N,{x:g,y:z,size:r,font:H,color:o(.1,.1,.1)}),z-=40;const U=[];for(const f of e){if(!f.enabled)continue;if(z<g+30)break;const x=f.pageIndex+1,y=n.getPage(Math.max(0,Math.min(x,b))),R=y.ref,M=y.getSize().height,m=Math.max(0,Math.min(M,M-f.y)),X=(f.level-1)*16,W=g+X,$=S-X-50;let A=f.text;const j=F(A,!1);if(j.widthOfTextAtSize(A,h)>$){for(;j.widthOfTextAtSize(A+"â¦",h)>$&&A.length>3;)A=A.slice(0,-1);A+="â¦"}d.drawText(A,{x:W,y:z,size:h,font:j,color:f.level===1?o(.15,.15,.15):o(.3,.3,.3)});const D=String(x+1),G=F(D,!1),Y=G.widthOfTextAtSize(D,v);d.drawText(D,{x:s-g-Y,y:z,size:v,font:G,color:o(.45,.45,.45)});const Z=W+j.widthOfTextAtSize(A,h)+4,V=s-g-Y-6;if(V>Z){const q=F(".",!1),nt=q.widthOfTextAtSize(".",v),ot=6;let E=Z,J=0;for(;E<V&&J<120;)d.drawText(".",{x:E,y:z,size:v,font:q,color:o(.7,.7,.7)}),E+=nt+ot,J++}const _=s-g-W,Q=z-4,P=z+h+4,tt=i.obj({Type:t.of("Annot"),Subtype:t.of("Link"),Rect:[W,Q,W+_,P],Border:[0,0,0],A:i.obj({Type:t.of("Action"),S:t.of("GoTo"),D:[R,t.of("XYZ"),null,m,null]})}),et=i.register(tt);U.push(et),z-=I}return U.length>0&&d.node.set(t.of("Annots"),i.obj(U)),await n.save()}async function bt(l,e,c,t,a){t==null||t(10);const o=await c.read(l[0]),n=await o.getBlob();let i=new Uint8Array(await n.arrayBuffer());t==null||t(30);const b=ht(e);if(b.length===0)throw new Error("No enabled headings to generate bookmarks");t==null||t(50);let d=0;if(a){const S=await xt(i,e);i=new Uint8Array(S),d=1,t==null||t(65)}const s=await ut(i,b,d);t==null||t(80);const p=a?"-with-toc.pdf":"-bookmarks.pdf",g=await c.write(new File([s.buffer],o.getName().replace(/\.pdf$/i,"")+p,{type:"application/pdf"}));return t==null||t(100),{outputIds:[g.id]}}const Tt=async({inputIds:l,options:e,fs:c,emitProgress:t})=>{if(e!=null&&e.fontUrls?C=e.fontUrls:C=null,l.length===0)throw new Error("Auto-TOC requires at least one input file");if(it(e),(e==null?void 0:e.action)==="apply")return bt(l,e.headers,c,t,e.generateTocPage===!0);const a=[];for(let o=0;o<l.length;o++){t==null||t(5);const i=await(await c.read(l[o])).getBlob(),b=new Uint8Array(await i.arrayBuffer());t==null||t(10);const d=await ft(b,(r,h)=>{const v=10+Math.round(r/h*25);t==null||t(Math.min(v,35))});if(d.length===0){const r={headers:[],error:"No text layer found in this PDF. The document may be scanned. Please run OCR first.",bodyTextSize:null,totalSpansExtracted:0,headingCandidatesFound:0},h=await c.write(new Blob([JSON.stringify(r)],{type:"application/json"}));a.push(h.id),t==null||t(100);continue}t==null||t(40);const s=lt(d),p=new Map;for(const r of d){const h=p.get(r.pageIndex);h?h.push(r):p.set(r.pageIndex,[r])}const g=ct(p),S=d.filter(r=>{if(g.has(r))return!1;const h=r.fontSize>=s*1.05,v=L(r.fontName)&&r.fontSize>=s*.9,I=O(r.text)&&r.fontSize>=s*.9;return!(!h&&!v&&!I)});t==null||t(60);const u=dt(S,s).filter(r=>rt(r.text));t==null||t(70);const w=u.map((r,h)=>({id:`f${o}-p${r.pageIndex}-h${h}`,text:r.text,pageIndex:r.pageIndex,y:r.pageHeight-r.y,level:st(r.fontSize,s,L(r.fontName),O(r.text)),enabled:!0}));t==null||t(80);const k=new Set(d.map(r=>r.pageIndex)).size,B={headers:w,bodyTextSize:s,totalPages:k,totalSpansExtracted:d.length,headingCandidatesFound:u.length},F=await c.write(new Blob([JSON.stringify(B,null,2)],{type:"application/json"}));a.push(F.id),t==null||t(100)}return{outputIds:a}};export{st as assignLevel,ht as buildOutlineTree,lt as computeBodyFontSize,O as isAllCaps,L as isBoldFont,rt as isLikelyHeading,Tt as run};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.