Files
dtf-system/web/site-sheet.js
Cauê Faleiros e3c31e43e5
Some checks failed
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Integration suite on a real stack (push) Failing after 4m10s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Has been skipped
feat: residue and a solid background stop a ready sheet before payment
Dropstar's items 2 and 3 (2026-10-21): both were warnings the customer
could pay past; now one dot is enough to stop the order.

- every pixel is read (images in strips at 1:1, large PNGs while they
  stream, PDFs rendered in strips at about 0,2 mm): the old check shrank
  the image first and could miss a small dot, or sampled every n-th pixel
- a residue is ink under 2 mm on both sides, measured from where the ink
  starts and ends in each cell: the same dot gets the same verdict wherever
  it falls on the grid, from 0,5 to 4 mm (his acceptance test)
- a solid background is under 1,5 % of transparent pixels; a JPG sheet
  always has one
- the sheet says how many dots there are, circles them on the preview, and
  asks to erase them and send again; the order waits for the check to end
- the page no longer offers JPG for a ready sheet

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:37:46 -03:00

149 lines
6.6 KiB
JavaScript

/* Site DTF — Reading a finished sheet from its alpha channel: pieces, gaps, waste.
Extracted verbatim from the single inline script in web/index.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.
// A grade de 1 mm agrupa a tinta; a medida de cada ilha sai dos pixels.
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};
}
// ── Mapa de tinta: cada pixel da folha lido, nenhum pulado.
// Regra da Dropstar (21/10/2026): resíduo é uma ilha de tinta menor que 2 mm
// nos dois lados, e um ponto só já barra o pedido. Reduzir a imagem antes de
// olhar perde ponto pequeno, e medir pela grade erra até uma célula: o mesmo
// ponto de 1,5 mm saía resíduo ou arte conforme caía na grade. Aqui a grade só
// agrupa; cada célula guarda onde a tinta começa e termina, em pixels, e cada
// ilha é medida por esses extremos, em milímetros reais.
const ALFA_TINTA=10; // acima disto o pixel leva tinta (e branco de base)
function novoMapa(wPx,hPx,larguraCm){
const pxPorCm=wPx/larguraCm, passo=passoDe(larguraCm,hPx/pxPorCm), pxCel=passo*pxPorCm;
const cw=Math.max(1,Math.ceil(wPx/pxCel)), ch=Math.max(1,Math.ceil(hPx/pxCel)), n=cw*ch;
const colDe=new Uint32Array(wPx);
for(let x=0;x<wPx;x++) colDe[x]=Math.min(cw-1,Math.floor(x/pxCel));
return {wPx,hPx,larguraCm,pxPorCm,passo,pxCel,cw,ch,colDe,bits:new Uint8Array(n),
x0:new Uint32Array(n),x1:new Uint32Array(n),y0:new Uint32Array(n),y1:new Uint32Array(n),tinta:0};
}
// A linha y da folha: o alfa de cada pixel em alfa[inicio + x*de].
function mapaLinha(m,y,alfa,inicio,de){
const base=Math.min(m.ch-1,Math.floor(y/m.pxCel))*m.cw;
const {bits,x0,x1,y0,y1,colDe}=m;
let tinta=0;
for(let x=0,i=inicio;x<m.wPx;x++,i+=de){
if(alfa[i]<=ALFA_TINTA) continue;
tinta++;
const c=base+colDe[x];
if(!bits[c]){ bits[c]=1; x0[c]=x1[c]=x; y0[c]=y1[c]=y; }
else{ if(x<x0[c]) x0[c]=x; else if(x>x1[c]) x1[c]=x; y1[c]=y; }
}
m.tinta+=tinta;
}
// Uma imagem que o navegador abre, lida em faixas no tamanho real (1:1).
async function mapaDaImagem(img,larguraCm){
const w=img.naturalWidth||img.width, h=img.naturalHeight||img.height;
const m=novoMapa(w,h,larguraCm);
const faixa=Math.max(1,Math.min(h,Math.floor(4e6/w)));
const cv=document.createElement('canvas'); cv.width=w; cv.height=faixa;
const ctx=cv.getContext('2d',{willReadFrequently:true});
ctx.imageSmoothingEnabled=false;
for(let topo=0;topo<h;topo+=faixa){
const n=Math.min(faixa,h-topo);
ctx.clearRect(0,0,w,faixa);
ctx.drawImage(img,0,topo,w,n,0,0,w,n);
const d=ctx.getImageData(0,0,w,n).data;
for(let r=0;r<n;r++) mapaLinha(m,topo+r,d,r*w*4+3,4);
await new Promise(r=>setTimeout(r)); // a página não congela
}
return m;
}
// ── O que a folha permite medir, e só isso.
// Peça = tinta 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
const RESIDUO_MM=2;
function analisarMapa(m){
const {cw,ch,bits}=m;
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 c=caixas[id] || (caixas[id]={x0:m.x0[p],x1:m.x1[p],y0:m.y0[p],y1:m.y1[p],id});
if(m.x0[p]<c.x0)c.x0=m.x0[p]; if(m.x1[p]>c.x1)c.x1=m.x1[p];
if(m.y0[p]<c.y0)c.y0=m.y0[p]; if(m.y1[p]>c.y1)c.y1=m.y1[p];
}
const mm=px=>px/m.pxPorCm*10;
const ilhas=caixas.filter(Boolean).map(c=>({...c, wMm:mm(c.x1-c.x0+1), hMm:mm(c.y1-c.y0+1)}));
const residuos=ilhas.filter(c=>c.wMm<RESIDUO_MM && c.hMm<RESIDUO_MM);
const deResiduo=new Set(residuos.map(c=>c.id));
// 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||deResiduo.has(a)) 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; });
const total=m.wPx*m.hPx;
return {
dpi: Math.round(m.wPx/(m.larguraCm/2.54)),
artes: ilhas.length-residuos.length,
residuos: residuos.length,
// Onde está cada ponto, em fração da folha: os círculos da prévia.
pontos: residuos.map(c=>({x:(c.x0+c.x1+1)/2/m.wPx, y:(c.y0+c.y1+1)/2/m.hPx,
mm:+Math.max(c.wMm,c.hMm).toFixed(1)})),
encostadas,
// Sem transparência: o que parece branco na tela sai branco no filme.
fundoChapado: (total-m.tinta)/total < 0.015,
aproveitamento: Math.max(1,Math.min(100,Math.round(m.tinta/total*100))),
alturaCm: m.hPx/m.pxPorCm,
grade: +(m.passo*10).toFixed(1) // mm por célula que agrupa
};
}