actualBoundingBoxLeft has the sign inverted relative to canvas's own rasteriser (3.2.0, 3.2.3)
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 68/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- javascript, node.js
- Ambito
- computer-graphics
Direzione di ricerca
Inizia eseguendo la riproduzione standalone di Node.js e confronta measureText().actualBoundingBoxLeft con il raster dipinto per i glifi elencati. Segui l'implementazione delle metriche del testo utilizzata da measureText(), quindi verifica che il valore sinistro riportato segua la specifica HTML e concordi con il risultato del raster, mentre actualBoundingBoxRight rimane invariato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
TextMetrics.actualBoundingBoxLeft comes back with the sign inverted relative to the library's own rasteriser. The magnitude is right, actualBoundingBoxRight is right, only the sign of left is wrong. Reproduced on canvas 3.2.0 and 3.2.3 (macOS arm64, Node 22).
Per the HTML spec, actualBoundingBoxLeft is the distance from the alignment point to the left side of the ink box, positive to the left (https://html.spec.whatwg.org/multipage/canvas.html#dom-textmetrics-actualboundingboxleft). So a glyph whose ink overhangs LEFT of the pen (a j with its hooked descender) should report a positive value, and a glyph whose ink starts RIGHT of the pen should report a negative one. @napi-rs/canvas and Chrome both do that. canvas does the opposite.
Minimal repro (standalone)
Paints the text, scans the raster for the leftmost ink pixel relative to the pen, and compares that against measureText().actualBoundingBoxLeft on the same canvas. Optionally set FONT_FILE to a TTF to register a known face; the default font works too as long as its j overhangs left.
// node repro.cjs
const { createCanvas, registerFont } = require('canvas');
const path = require('path');
const FONT_FILE = process.env.FONT_FILE; // optional
if (FONT_FILE) registerFont(path.resolve(FONT_FILE), { family: 'ReproFace' });
const FONT = `96px ${FONT_FILE ? 'ReproFace' : 'sans-serif'}`;
const SIZE = 400, PEN = 200;
function paintedLeft(text) {
const ctx = createCanvas(SIZE, SIZE).getContext('2d');
ctx.fillStyle = '#000'; ctx.fillRect(0, 0, SIZE, SIZE);
ctx.font = FONT; ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'left';
ctx.fillStyle = '#fff'; ctx.fillText(text, PEN, 250);
const d = ctx.getImageData(0, 0, SIZE, SIZE).data;
let minX = Infinity;
for (let y = 0; y < SIZE; y++) for (let x = 0; x < SIZE; x++)
if (d[(y * SIZE + x) * 4] > 128 && x < minX) minX = x;
return PEN - minX; // positive = ink starts LEFT of the pen
}
function measuredLeft(text) {
const ctx = createCanvas(10, 10).getContext('2d');
ctx.font = FONT; ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'left';
return ctx.measureText(text).actualBoundingBoxLeft;
}
for (const t of ['j', 'HAMBURGEFONS', 'W']) {
console.log(t.padEnd(14), 'painted left:', paintedLeft(t), ' measured actualBoundingBoxLeft:', measuredLeft(t));
}
Numbers (Inter Black, 96px, pen x=200)
| text | painted left (scanned off canvas's own raster) | canvas actualBoundingBoxLeft |
@napi-rs/canvas actualBoundingBoxLeft |
|---|---|---|---|
j |
+3 | −3.281 | +4.000 |
HAMBURGEFONS |
−5 | +4.500 | −4.000 |
bhpqfj |
−5 | +4.781 | −4.000 |
W |
−2 | +2.156 | −2.000 |
actualBoundingBoxRight agrees with @napi-rs/canvas to under 1px on every row, so it is specific to left.
Impact
Anything that computes an ink box as left + right gets a width off by 2 × |side bearing|, which scales with font size and face and looks like a metrics "convention difference" until you paint the glyph and check. We hit it in a rendering parity harness and initially attributed it to a node-canvas vs browser convention.
Environment
canvas3.2.0 and 3.2.3 (both reproduce)- macOS 15 arm64, Node 22
- Lingua principale
- JavaScript
- Stelle
- 10.7k
- Fork
- 1.2k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Nessuna guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Automattic/node-canvas
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
Automattic/node-canvas#2633 ·
-
Feature Text & Fonts
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
Automattic/node-canvas#2364 · 3 commenti · 2 reazioni ·
-
Feature
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
Automattic/node-canvas#2621 ·
-
Bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Automattic/node-canvas#2619 · 1 commento ·
-
The PDF file created by createPDFStream becomes larger in size, and createWriteStream is very slowApertaBug performance
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Automattic/node-canvas#2615 · 2 commenti ·
Tutte le issue di Automattic/node-canvas
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
capricorn86/happy-dom#2594 ·
I maintainer di solito rispondono entro 1 giorno
-
Decoding tag 1 (epoch date) with an 8-byte integer throws "Cannot mix BigInt and other types"Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
microsoft/monaco-editor#5575 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
Shrinking an array with `length =` and growing it back loses the patches for the truncated indicesForse già presa @RClaudiuM l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 4 giorni