diff --git a/app/src/ui/board-view.ts b/app/src/ui/board-view.ts index 0c7a73f..de04816 100644 --- a/app/src/ui/board-view.ts +++ b/app/src/ui/board-view.ts @@ -614,33 +614,67 @@ function addRing( function drawBand(svg: SVGSVGElement, g: Geometry, text: string, before = 0): void { const middle = g.height / 2; const width = 6 * g.pointWidth - 24 - before; - const height = Math.max(44, g.bandFont + 20); const x = g.x0 + 12 + before; - // Une consigne longue (« Partie Crawford · … ») tient dans la pastille : - // la police se resserre, sans descendre sous 13 px (charte). Largeur - // moyenne d'un caractère d'Alegreya Sans en gras : un peu moins de - // 0,45 em. - const fontSize = Math.max(13, Math.min(g.bandFont, (width - 24) / (0.45 * text.length))); + // La consigne garde toujours sa taille (12 px au moins, charte) : trop + // longue pour une ligne, elle passe sur deux, une phrase par ligne de + // préférence. Largeur moyenne d'un caractère d'Alegreya Sans en gras : + // un peu moins de 0,45 em. + const fontSize = g.bandFont; + const lines = bandLines(text, (width - 24) / (0.45 * fontSize)); + const lineHeight = fontSize * 1.2; + const height = Math.max(44, fontSize + 20, lines.length * lineHeight + 16); add(svg, "rect", { class: "band-pill", x, y: middle - height / 2, width, height, - rx: height / 2, + rx: Math.min(height / 2, 22), }); - add( - svg, - "text", - { - class: "band-text", - id: "band-text", - x: x + width / 2, - y: middle + fontSize * 0.35, - "font-size": fontSize, - }, - text, - ); + const label = add(svg, "text", { + class: "band-text", + id: "band-text", + x: x + width / 2, + y: middle + fontSize * 0.35 - ((lines.length - 1) * lineHeight) / 2, + "font-size": fontSize, + }); + lines.forEach((line, i) => { + add( + label, + "tspan", + { x: x + width / 2, dy: i === 0 ? 0 : lineHeight }, + // L'espace entre les deux lignes reste dans le texte (lecteurs + // d'écran, tests) : à la fin de la première. + i < lines.length - 1 ? `${line} ` : line, + ); + }); +} + +/** Une consigne en une ligne, ou en deux si elle dépasse capacity + * caractères : coupée après la première phrase quand les deux tiennent, + * sinon à l'espace le plus proche du milieu. */ +function bandLines(text: string, capacity: number): string[] { + if (text.length <= capacity) { + return [text]; + } + const sentence = text.indexOf(". "); + if (sentence > 0) { + const first = text.slice(0, sentence + 1); + const second = text.slice(sentence + 2); + if (first.length <= capacity && second.length <= capacity) { + return [first, second]; + } + } + let cut = -1; + for (let i = 0; i < text.length; i++) { + if ( + text[i] === " " && + (cut < 0 || Math.abs(i - text.length / 2) < Math.abs(cut - text.length / 2)) + ) { + cut = i; + } + } + return cut < 0 ? [text] : [text.slice(0, cut), text.slice(cut + 1)]; } /** Largeur de la pastille du conseil : pictogramme, mot (estimé à 0,55 em diff --git a/tests/browser/retours-2.mjs b/tests/browser/retours-2.mjs index ce53546..27224ff 100644 --- a/tests/browser/retours-2.mjs +++ b/tests/browser/retours-2.mjs @@ -173,7 +173,45 @@ async function resignState() { /** Le drapeau : en portrait dans la barre d'actions, en paysage dans la * colonne de commandes. */ +/** La pastille de la bande, en paysage : sa police (en unités et en px à + * l'écran), et si elle tient dans la bande sans chevaucher de pion. */ +async function bandMeasure() { + return page.evaluate(() => { + const svg = document.getElementById("board"); + const text = document.getElementById("band-text"); + const pill = svg.querySelector(".band-pill"); + if (text === null || pill === null) { + return null; + } + const box = svg.getBoundingClientRect(); + const vb = svg.viewBox.baseVal; + const scale = Math.min(box.width / vb.width, box.height / vb.height); + const p = pill.getBBox(); + const t = text.getBBox(); + const overlaps = (a, b) => + a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height; + const checkers = [...svg.querySelectorAll("[data-checker]")].filter((c) => overlaps(p, c.getBBox())); + return { + text: text.textContent, + font: Number(text.getAttribute("font-size")), + px: Number(text.getAttribute("font-size")) * scale, + lines: text.querySelectorAll("tspan").length, + fits: t.width <= p.width - 16 && t.y >= p.y && t.y + t.height <= p.y + p.height, + overlaps: checkers.length, + }; + }); +} + +// La police de la consigne ordinaire, mesurée en paysage (« À vous : lancez +// les dés. ») : celle de toute pastille, jamais sous 12 px. +let normalBandFont = null; + async function flagShown(size) { + if (size.width > size.height) { + const band = await bandMeasure(); + check(band !== null && band.px >= 12 && band.overlaps === 0, `consigne ordinaire : ${JSON.stringify(band)}`); + normalBandFont = band.font; + } const s = await resignState(); if (size.width < size.height) { check(s.flag && s.flagLabel === "Abandonner la partie", `drapeau absent : ${JSON.stringify(s)}`); @@ -346,15 +384,12 @@ const bandFits = (expected) => async (size) => { if (size.width <= size.height) { return; } - const band = await page.evaluate(() => { - const text = document.getElementById("band-text"); - const pill = document.querySelector("#board .band-pill"); - return { - text: text?.textContent ?? "", - fits: text !== null && pill !== null && text.getBBox().width <= pill.getBBox().width - 16, - }; - }); - check(band.text === expected && band.fits, `bande du dé injouable : ${JSON.stringify(band)}`); + const band = await bandMeasure(); + // Même police que la consigne ordinaire (12 px au moins), sur deux + // lignes s'il le faut, dans la pastille, sans chevaucher de pion. + check(band !== null && band.text === expected && band.fits && band.overlaps === 0 && + band.font === normalBandFont && band.px >= 12, + `bande du dé injouable : ${JSON.stringify(band)} (police ordinaire ${normalBandFont})`); }; // Dès le lancer, le dé qui ne rentre pas est barré (il n'apparaît dans aucun // coup légal du moteur).