frontend: check that a sized figure keeps its own shape

The size is a width and nothing else, so the height comes from the picture's
aspect ratio and a figure cannot be deformed. That is worth asserting rather
than assuming, in both halves: the shape the browser actually drew, and that no
height was written into the style at all.

Measured on the paper this was built for, which now has all sixteen of its
figures at 50%: 638px wide each, aspect ratio within 0.2% of the file's own — the
rounding of the measurement, not the layout.
This commit is contained in:
2026-09-24 12:46:10 +08:00
parent 48f0c12fd2
commit b19f28492e
2 changed files with 26 additions and 1 deletions
+7 -1
View File
@@ -413,7 +413,13 @@ line":
The size is optional and comes from pandoc's attribute form — `{width=70%}`,
`{width=400px}` — and is validated to digits plus one unit before it becomes a
CSS length, so no second declaration can be smuggled through it. A bare number
CSS length, so no second declaration can be smuggled through it. It is a
**width and only a width**: the height is whatever keeps the picture's own shape,
which is why a figure can be resized but not stretched, and why the format has
nowhere to put a second number even if a writer wanted one. Measured on the
sixteen figures of the paper this was built for, every drawn rectangle matched
its picture's aspect ratio to within 0.2% — the rounding of the measurement,
not the layout. A bare number
is a **share of the column**, because `parseImageWidth` only reads a value as
pixels when it says `px`; the MCP tool that writes the same attribute follows
that rule rather than inventing its own.
+19
View File
@@ -173,6 +173,11 @@ try {
src: figureImage.getAttribute('src'),
natural: [figureImage.naturalWidth, figureImage.naturalHeight],
width: Math.round(figureBox.width),
height: Math.round(figureBox.height),
// What the markup asked the browser for. The height is expected to be
// empty: only a width is ever written, which is what makes a figure
// impossible to deform.
styleHeight: figureImage.style.height || '',
center: Math.round(figureBox.left + figureBox.width / 2),
caption: caption.textContent.trim(),
gapBelowImage: Math.round(caption.getBoundingClientRect().top - figureBox.bottom),
@@ -200,6 +205,20 @@ try {
Math.abs(figure.center - geometry.bodyCenter) <= 2,
`${figure.center} vs ${geometry.bodyCenter}`,
)
// The size is a *width* and nothing else: the height is whatever keeps the
// picture's own shape, so a figure cannot be stretched. Both halves of that
// are checked — the shape the browser drew, and that nothing asked for a
// height in the first place.
check(
'a sized figure keeps its own shape',
Math.abs((figure.width / figure.height) / (figure.natural[0] / figure.natural[1]) - 1) < 0.01,
`${figure.width}×${figure.height} vs natural ${figure.natural.join('×')}`,
)
check(
'and no height is ever written into the sentence',
figure.styleHeight === '',
`style.height = ${JSON.stringify(figure.styleHeight)}`,
)
check('the 图注 is the text in the brackets', figure.caption === '试验装置示意图', figure.caption)
check('…and it is printed under the picture', figure.gapBelowImage > 0, `${figure.gapBelowImage}`)
check('the figure owns its line', figure.siblings === 1, `${figure.siblings} sentences`)