// The plan on paper: every floor as a clean black-on-white drawing (walls, doors and windows, room names with their living area, // the outer measurements), one page each, and a table of the areas at the end. Opened in a window of its own and printed from there, // so the browser's print dialog can also save it as PDF. import { centroid, type Building, type Floor, type Vec2 } from "../../model.ts"; import { floorAreas, totalOf, type RoomArea } from "../../geometry/living-area.ts"; import { generateWalls, locateOpening, openingHost } from "../../geometry/walls.ts"; export interface PrintTexts { title: string; room: string; floorArea: string; living: string; low: string; total: string; note: string; area(m2: number): string; length(m: number): string; } const esc = (s: string) => s.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]!); const pts = (ring: readonly Vec2[]) => ring.map(([x, z]) => `${x.toFixed(3)},${z.toFixed(3)}`).join(" "); /** One floor as SVG in metres (1 unit = 1 m), with room for the measurements around it. */ export function floorSvg(b: Building, floor: Floor, areas: readonly RoomArea[], texts: PrintTexts): string { const walls = generateWalls(floor.rooms, { exterior: b.settings.wall_exterior, interior: b.settings.wall_interior }, floor.walls ?? []).walls; const all = [...floor.rooms.flatMap((r) => r.points), ...walls.flatMap((w) => w.footprint)]; if (!all.length) return ""; const [x0, x1] = [Math.min(...all.map((p) => p[0])), Math.max(...all.map((p) => p[0]))]; const [z0, z1] = [Math.min(...all.map((p) => p[1])), Math.max(...all.map((p) => p[1]))]; const pad = 1.2; const parts: string[] = []; for (const r of floor.rooms) parts.push(``); for (const w of walls) parts.push(``); // doors and windows: a white gap in the wall, a window with its glass line, a door with its swing for (const o of floor.openings) { const host = openingHost(o, floor.rooms, floor.walls ?? []); const at = host ? locateOpening(walls, o, host) : null; if (!at) continue; const { wall, s } = at; const len = Math.hypot(wall.b[0] - wall.a[0], wall.b[1] - wall.a[1]); if (len < 1e-6) continue; const u: Vec2 = [(wall.b[0] - wall.a[0]) / len, (wall.b[1] - wall.a[1]) / len]; const n: Vec2 = [-u[1], u[0]]; const p = (along: number, side: number): Vec2 => [wall.a[0] + u[0] * along + n[0] * side, wall.a[1] + u[1] * along + n[1] * side]; const [s0, s1] = [s - o.width / 2, s + o.width / 2]; const [l, r] = [wall.left + 0.01, -(wall.right + 0.01)]; parts.push(``); if (o.type === "garage") { const mid = (wall.left - wall.right) / 2; parts.push(``); } else if (o.type === "window") { const mid = (wall.left - wall.right) / 2; parts.push(``); parts.push(``); } else { // a quarter circle on the room side of the wall const side = o.room_id === wall.roomRight ? -1 : 1; const hinge = p(s0, side * (side > 0 ? wall.left : wall.right)); const tip = p(s0, side * ((side > 0 ? wall.left : wall.right) + o.width)); const end = p(s1, side * (side > 0 ? wall.left : wall.right)); parts.push(``); } } const byRoom = new Map(areas.map((a) => [a.roomId, a])); for (const r of floor.rooms) { if (r.points.length < 3) continue; const [cx, cz] = centroid(r.points); const a = byRoom.get(r.id); parts.push(`${esc(r.name)}`); if (a) parts.push(`${esc(texts.area(a.living > 0 ? a.living : a.floor))}`); } // the outer measurements under and beside the drawing const dim = (ax: number, az: number, bx: number, bz: number, label: string, vertical: boolean) => { const tick = 0.15; const mx = (ax + bx) / 2; const mz = (az + bz) / 2; const ticks = vertical ? `M ${ax - tick} ${az} L ${ax + tick} ${az} M ${bx - tick} ${bz} L ${bx + tick} ${bz}` : `M ${ax} ${az - tick} L ${ax} ${az + tick} M ${bx} ${bz - tick} L ${bx} ${bz + tick}`; const text = vertical ? `${esc(label)}` : `${esc(label)}`; return `${text}`; }; parts.push(dim(x0, z1 + 0.5, x1, z1 + 0.5, texts.length(x1 - x0), false)); parts.push(dim(x0 - 0.5, z0, x0 - 0.5, z1, texts.length(z1 - z0), true)); // a metre bar for the scale (the drawing is 1:100 while it fits the page, smaller beyond) parts.push(`${esc(texts.length(1))}`); const [w, h] = [x1 - x0 + 2 * pad, z1 - z0 + 2 * pad]; const vb = `${x0 - pad} ${z0 - pad} ${w} ${h}`; return `${parts.join("")}`; } /** The whole printout: a page per floor and the table of areas. */ export function printHtml(b: Building, texts: PrintTexts): string { const floors = b.floors.filter((f) => !f.layer && f.rooms.length); const pages: string[] = []; const rows: string[] = []; const all: RoomArea[] = []; for (const floor of [...floors].sort((p, q) => p.elevation - q.elevation)) { const areas = floorAreas(b, floor); all.push(...areas); const sum = totalOf(areas); pages.push(`

${esc(floor.name)}

${esc(texts.living)}: ${esc(texts.area(sum.living))}
${floorSvg(b, floor, areas, texts)}
`); rows.push(`${esc(floor.name)}`); for (const a of areas) { const room = floor.rooms.find((r) => r.id === a.roomId)!; rows.push(`${esc(room.name)}${esc(texts.area(a.floor))}${a.low > 0.05 ? esc(texts.area(a.low)) : ""}${esc(texts.area(a.living))}`); } rows.push(`${esc(texts.total)}${esc(texts.area(sum.floor))}${sum.low > 0.05 ? esc(texts.area(sum.low)) : ""}${esc(texts.area(sum.living))}`); } const total = totalOf(all); const table = `

${esc(texts.living)}

${rows.join("")}
${esc(texts.room)}${esc(texts.floorArea)}${esc(texts.low)}${esc(texts.living)}
${esc(texts.total)}${esc(texts.area(total.floor))}${total.low > 0.05 ? esc(texts.area(total.low)) : ""}${esc(texts.area(total.living))}

${esc(texts.note)}

`; return `${esc(texts.title)}${pages.join("")}${table}`; }