Datasets:
File size: 6,391 Bytes
56342ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // fax_forms.js - builds hyperbots-inc/synthetic-fax-forms. Load docgen.js first (see the other generators), then FAX.one(i).
const FAX = {};
FAX.types = ['patient_intake_form','kyc_form','patient_intake_form','kyc_form','claim_form','credit_application','patient_intake_form','kyc_form','bank_details_form','maintenance_report'];
FAX.INITIALS = ['AK','R.S.','JD','MP','SK','NB','TR','LW','ok - SK','JD 12/3','seen RT','PB'];
FAX.typeInfo = t=>({doc_type:t,taxonomy_id:TYPES[t][0],taxonomy_name:TYPES[t][2],category:TYPES[t][1]+'. '+G.CAT[TYPES[t][1]]});
FAX.goldFor = (doc,o)=>{const e={}; for(const k in doc.entities){const v=doc.entities[k]; if(Array.isArray(v)){const t=o.tables[k]; e[k]=t?v.slice(0,t.rows_rendered):v;} else e[k]=v;} return e;};
FAX.inter = (a,b)=>!(a[2]<b[0]||b[2]<a[0]||a[3]<b[1]||b[3]<a[1]);
FAX.one = (i)=>{
const seed=16000*1000+i; const t=FAX.types[i%FAX.types.length]; const r=RNG(seed); const d=generate(t,seed); d.form=true; d.blocks.forEach(b=>{if(b.t==='kv') b.items.forEach(it=>{it.hand=true;});});
const st=G.handStyle(r); const o=render(d,{rnd:r,hand:true,handStyle:st,signProb:0.9}); if(!o.checkboxes.length) return null; const q=RNG(seed+13); const ctx=o.ctx||o.canvas.getContext('2d'); const W=o.canvas.width,H=o.canvas.height;
const markStyle=q.pick(['tick','tick','cross','filled']); const ink=st.color||'#1c3f94';
const cbs=o.checkboxes.map(c=>({key:c.key,label:c.label,checked:c.checked,options:c.options.map(op=>Object.assign({},op,{mark_style:op.checked?markStyle:null}))}));
if(markStyle!=='tick'){ for(const c of cbs) for(const op of c.options){ if(!op.checked) continue; const [x0,y0,x1,y1]=op.bbox; ctx.fillStyle='#ffffff'; ctx.fillRect(x0+2,y0+2,x1-x0-4,y1-y0-4); ctx.strokeStyle=ink; ctx.fillStyle=ink; ctx.lineWidth=2.2; if(markStyle==='cross'){ctx.beginPath(); ctx.moveTo(x0+2,y0+2); ctx.lineTo(x1-1,y1-2); ctx.moveTo(x1-2,y0+1); ctx.lineTo(x0+3,y1-1); ctx.stroke();} else { ctx.fillRect(x0+3,y0+3,x1-x0-6,y1-y0-6); } } }
const tgt=[]; o.fields.filter(f=>f.bbox&&f.bbox[1]>150).forEach(f=>{ if(f.handwritten) tgt.push({kind:'field_label',text:f.label,bbox:[f.bbox[0],f.bbox[1]-24,f.bbox[0]+Math.max(60,Math.min(220,f.label.length*9)),f.bbox[1]-6]}); else tgt.push({kind:'printed_field',text:String(f.value),bbox:f.bbox}); }); o.blocks.filter(bk=>/section_header|title/.test(bk.label)&&bk.bbox[1]>120).forEach(bk=>tgt.push({kind:bk.label,text:bk.text.split('\n')[0],bbox:[bk.bbox[0],bk.bbox[1],Math.min(bk.bbox[2],bk.bbox[0]+360),bk.bbox[3]]}));
const nI=q.int(1,3); const initials=[]; const used=new Set(); for(let k=0;k<nI&&tgt.length;k++){ const pool=tgt.filter(x=>!used.has(x)); if(!pool.length) break; const T=q.pick(pool); used.add(T); const tb=T.bbox; const txt=q.pick(FAX.INITIALS); const x=Math.round(tb[0]+q.int(0,Math.max(1,Math.round((tb[2]-tb[0])*0.4)))); const y=Math.round(tb[1]-q.int(4,12)); const bb=o.drawHand(txt,x,y,q.int(26,36)); initials.push({text:txt,bbox:bb.map(Math.round),over:{kind:T.kind,text:T.text}}); }
const skew=Math.round(q.float(-3,3)*100)/100||0.7; const fine=q.bool(0.3); const thr=q.int(140,175); const a=skew*Math.PI/180, cx=W/2, cy=H/2;
const c1=document.createElement('canvas'); c1.width=W; c1.height=H; const g1=c1.getContext('2d'); g1.fillStyle='#fff'; g1.fillRect(0,0,W,H); g1.translate(cx,cy); g1.rotate(a); g1.translate(-cx,-cy); g1.drawImage(o.canvas,0,0);
let src=c1; if(!fine){ const c2=document.createElement('canvas'); c2.width=W; c2.height=Math.round(H/2); const g2=c2.getContext('2d'); g2.imageSmoothingEnabled=true; g2.drawImage(c1,0,0,W,c2.height); const c3=document.createElement('canvas'); c3.width=W; c3.height=H; const g3=c3.getContext('2d'); g3.imageSmoothingEnabled=false; g3.drawImage(c2,0,0,W,H); src=c3; }
const out=document.createElement('canvas'); out.width=W; out.height=H; const go=out.getContext('2d'); go.drawImage(src,0,0); const img=go.getImageData(0,0,W,H), px=img.data; const sp=q.float(0.0002,0.0009);
for(let p=0;p<px.length;p+=4){ let v=(px[p]*0.3+px[p+1]*0.59+px[p+2]*0.11)<thr?0:255; if(q.f?false:Math.random()<0) v=255-v; px[p]=px[p+1]=px[p+2]=v; }
const nSp=Math.round(W*H*sp); for(let k=0;k<nSp;k++){ const x=q.int(0,W-1), y=q.int(0,H-1); const p=(y*W+x)*4; px[p]=px[p+1]=px[p+2]=0; }
go.putImageData(img,0,0); const streaks=[]; for(let k=0,n=q.int(0,3);k<n;k++){ const y=q.int(140,H-60), h=q.int(1,2); go.fillStyle=q.bool(0.6)?'#000':'#fff'; go.fillRect(0,y,W,h); streaks.push({y,height:h,color:go.fillStyle==='#000000'?'black':'white'}); }
const fromNo='+44 20 7946 0'+q.int(100,999), toName=q.pick(['ACCOUNTS PAYABLE','REFERRALS','CLAIMS DEPT','ONBOARDING','ADMISSIONS']); const dd=String(q.int(1,28)).padStart(2,'0'), mm=String(q.int(1,12)).padStart(2,'0'), hh=String(q.int(8,18)).padStart(2,'0'), mi=String(q.int(0,59)).padStart(2,'0'); const header='FROM: '+fromNo+' TO: '+toName+' '+dd+'/'+mm+'/2026 '+hh+':'+mi+' P.01/01';
go.fillStyle='#fff'; go.fillRect(0,0,W,34); go.fillStyle='#000'; go.font='16px "Courier New", Courier, monospace'; go.textBaseline='alphabetic'; go.fillText(header,40,24); const hm=go.measureText(header); const hb=[40,Math.round(24-hm.actualBoundingBoxAscent),Math.round(40+hm.width),Math.round(24+hm.actualBoundingBoxDescent)];
const tf=b=>{ if(!b) return b; const pts=[[b[0],b[1]],[b[2],b[1]],[b[0],b[3]],[b[2],b[3]]].map(([x,y])=>{const dx=x-cx,dy=y-cy; return [cx+dx*Math.cos(a)-dy*Math.sin(a), cy+dx*Math.sin(a)+dy*Math.cos(a)];}); return [Math.round(Math.min(...pts.map(p=>p[0]))),Math.round(Math.min(...pts.map(p=>p[1]))),Math.round(Math.max(...pts.map(p=>p[0]))),Math.round(Math.max(...pts.map(p=>p[1])))]; };
cbs.forEach(c=>c.options.forEach(op=>{op.bbox=tf(op.bbox);})); initials.forEach(x=>{x.bbox=tf(x.bbox);});
const name='fax-'+String(i+1).padStart(4,'0')+'.jpg';
const row=Object.assign({file_name:name,id:name.replace('.jpg','')},FAX.typeInfo(t),{fax:{skew_deg:skew,resolution:fine?'fine':'standard',threshold:thr,speckle:Math.round(sp*1e5)/1e5,streaks},fax_header:{text:header,bbox:hb},writer_font:st.font,ink:st.ink,mark_style:markStyle,checkboxes:cbs,initials,fields:o.fields.map(f=>({key:f.key,label:f.label,value:f.value,bbox:tf(f.bbox),handwritten:!!f.handwritten})),signatures:(o.signatures||[]).map(s=>Object.assign({},s,{bbox:tf(s.bbox)})),labels:JSON.stringify(FAX.goldFor(d,o)),ocr_text:header+'\n\n'+ocrFromRender(o).text});
return {cv:out,row,name};
};
window.FAX=FAX; |