Files
dtf-system/local/static/site-sheet.js
Cauê Faleiros 96f1d27221 refactor: split the Site's behaviour out of one 1,575-line inline script
dtf-site.html held commercial rules, the nesting engine, PDF analysis, the cart
and every handler in a single inline script, 42% of the runtime code in one
file, and the money logic lived in the middle of it.

It is now nine files under local/static, cut at the section markers the original
author left, so no function was split across a boundary: config, product modes,
upload, sheet analysis, PDF, quality, packing, cart, flow. They load as classic
scripts in the original order and share one global scope, so evaluation is
exactly what it was; the extraction was checked byte-identical against the
original before the tags replaced it. dtf-site.html is 1,394 lines of markup and
style.

With no inline script left anywhere, the policy no longer needs a hash
allowlist: script-src is now 'self' alone, which is stronger than what it
replaced and cannot drift as the page changes.

Three things depended on the old shape and were updated rather than worked
around. The pricing parity test read the ladder out of the HTML and now reads it
from site-config.js, still proving the server agrees with what the customer is
shown. The isolated artwork test served four hardcoded script paths and now
serves any script that resolves inside local/static, so the next file added does
not silently 404. The CSP assertion checked the whole policy for 'unsafe-inline'
and now checks the script-src directive alone, since style-src legitimately
carries it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 16:44:49 -03:00

108 lines
4.5 KiB
JavaScript

/* Site DTF — Reading a finished sheet from its alpha channel: pieces, gaps, waste.
Extracted verbatim from the single inline script in dtf-site.html.
Loaded as classic scripts in the order listed there: they share one global
scope and run top to bottom, exactly as the original did. */
// ── Análise real da folha: lê o canal alfa, separa cada ilha de arte e mede.
// Grade de 1 mm. Só roda em arquivo que o navegador abre; PDF precisa do servidor.
const MM=0.1; // grade fina: 1 mm
const CELULAS_MAX=1200000; // teto de custo da análise
// Folha grande vira grade mais grossa. Sem isso, 57 x 480 cm dá 2,7 milhões de
// células e o navegador congela antes de terminar.
function passoDe(larguraCm, alturaCm){
const fino=(larguraCm/MM)*(alturaCm/MM);
return fino<=CELULAS_MAX ? MM : MM*Math.sqrt(fino/CELULAS_MAX);
}
// Dilata a máscara em r células · usada para agrupar por distância real, não por caixa
function inchar(bits,cw,ch,r){
const a=new Uint8Array(cw*ch), b=new Uint8Array(cw*ch);
for(let y=0;y<ch;y++){ const o=y*cw;
for(let x=0;x<cw;x++){ if(!bits[o+x]) continue;
const x0=Math.max(0,x-r), x1=Math.min(cw-1,x+r);
for(let k=x0;k<=x1;k++) a[o+k]=1; } }
for(let x=0;x<cw;x++)
for(let y=0;y<ch;y++){ if(!a[y*cw+x]) continue;
const y0=Math.max(0,y-r), y1=Math.min(ch-1,y+r);
for(let k=y0;k<=y1;k++) b[k*cw+x]=1; }
return b;
}
// Componentes conexos de 8 vizinhos · devolve o mapa de rótulos e quantos grupos
function grupos(bits,cw,ch){
const marca=new Int32Array(cw*ch).fill(-1), fila=new Int32Array(cw*ch);
let n=0;
for(let p0=0;p0<bits.length;p0++){
if(!bits[p0]||marca[p0]>=0) continue;
const id=n++; let ini=0,fim=0; fila[fim++]=p0; marca[p0]=id;
while(ini<fim){
const p=fila[ini++], x=p%cw, y=(p/cw)|0;
for(let dy=-1;dy<=1;dy++) for(let dx=-1;dx<=1;dx++){
const nx=x+dx, ny=y+dy;
if(nx<0||ny<0||nx>=cw||ny>=ch) continue;
const q=ny*cw+nx;
if(bits[q]&&marca[q]<0){ marca[q]=id; fila[fim++]=q; }
}
}
}
return {marca,n};
}
// ── O que a folha permite medir, e só isso.
// Peça = pixels a menos de 2 mm entre si · letras de uma palavra ficam juntas.
// Encostadas = peças que só se unem quando a folga chega a 6 mm.
const R_PECA=1, R_PERTO=2; // peça: 2 mm · aviso de encostadas: 4 mm
function analisarFolha(img, larguraCm){
const alturaCmEst=larguraCm*img.height/img.width;
const passo=passoDe(larguraCm, alturaCmEst);
const cw=Math.max(8,Math.round(larguraCm/passo));
const ch=Math.max(8,Math.round(cw*img.height/img.width));
const cv=document.createElement('canvas'); cv.width=cw; cv.height=ch;
const ctx=cv.getContext('2d',{willReadFrequently:true});
ctx.clearRect(0,0,cw,ch); ctx.drawImage(img,0,0,cw,ch);
const d=ctx.getImageData(0,0,cw,ch).data;
const bits=new Uint8Array(cw*ch);
let opacos=0;
for(let i=0,j=3;i<bits.length;i++,j+=4) if(d[j]>10){ bits[i]=1; opacos++; }
// peças: agrupa pela distância real entre pixels
const gp=grupos(inchar(bits,cw,ch,R_PECA), cw, ch);
const caixas=[];
for(let p=0;p<bits.length;p++){
if(!bits[p]) continue;
const id=gp.marca[p]; if(id<0) continue;
const x=p%cw, y=(p/cw)|0;
const c=caixas[id] || (caixas[id]={x0:x,x1:x,y0:y,y1:y,area:0,id});
if(x<c.x0)c.x0=x; if(x>c.x1)c.x1=x; if(y<c.y0)c.y0=y; if(y>c.y1)c.y1=y;
c.area++;
}
const pecasTodas=caixas.filter(Boolean);
const limRes=Math.max(1, Math.round(0.2/passo)); // 2 mm na grade em uso
const residuos=pecasTodas.filter(o=>(o.x1-o.x0)<limRes && (o.y1-o.y0)<limRes);
const artes=pecasTodas.filter(o=>!((o.x1-o.x0)<limRes && (o.y1-o.y0)<limRes));
// encostadas: peças diferentes que se unem quando a folga chega a 6 mm
const gper=grupos(inchar(bits,cw,ch,R_PERTO), cw, ch);
const porPerto=new Map();
for(let p=0;p<bits.length;p++){
if(!bits[p]) continue;
const a=gp.marca[p], b=gper.marca[p];
if(a<0||b<0) continue;
if(!porPerto.has(b)) porPerto.set(b,new Set());
porPerto.get(b).add(a);
}
let encostadas=0;
porPerto.forEach(set=>{ if(set.size>1) encostadas+=set.size; });
return {
dpi: Math.round(img.width/(larguraCm/2.54)),
artes: artes.length,
residuos: residuos.length,
encostadas,
fundoChapado: opacos/(cw*ch) > 0.985,
aproveitamento: Math.max(1,Math.min(100,Math.round(opacos/(cw*ch)*100))),
alturaCm: ch*passo,
grade: +(passo*10).toFixed(1) // mm por célula analisada
};
}