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:
@@ -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`)
|
||||
|
||||
Reference in New Issue
Block a user