frontend: let a figure be sized by its height as well as its width

A writer with a tall panel does not want to shrink it from the left edge: what
they know is "this one is too tall". The format had a width and nothing else, so
the only way to make a figure shorter was to guess a percentage and check the
page — and the previous commit's answer, "type 638px", asked them to do the
arithmetic themselves.

So `{height=300px}` joins `{width=…}`, in the same attribute block, and the
editor grows a second box beside the first. Both are typeable as well as
pre-set, both write through the reader's own parse function, and the hint beside
them says what the picture will measure on the page.

Three rules, and the point of all three is that a figure cannot be stretched:

* **a width is exact, a height is a cap.** A width has a reference — the column,
  or a length the writer chose — while nothing measures a height against
  anything, and enlarging a picture past its own pixels only makes it soft. In
  the flow of a sentence a height is always a downscale, which comes to the same
  thing as exact.
* **given one, the other follows from the picture's own shape** — `max-height`
  for a height, `width` for a width — so the height is never written by hand.
* **given both, they are a box to fit inside**, not a box to fill. Two numbers
  that need not have the picture's proportions can only mean "no bigger than",
  and the check asserts the drawn rectangle still matches the file's ratio.

Whichever box is left empty keeps the other's setting, so a size can be set in
two steps, and clearing either goes back to the natural size.

`check-image.mjs`: 26 checks to 36. Two of the new ones found real bugs while
being written. The first: with both a width and a height in one `{…}` block,
neither side parsed — the patterns were anchored to the whole block, so a figure
with two numbers silently printed at its natural size. The attribute block is now
split into declarations first. The second was mine: returning "contain" for a
height-only figure left a 1276×2000 box around a small picture, which the caption
then sat under. A height is a cap, and a cap needs no contain.

The MCP tool takes the same two arguments, so a model can write either number,
and a percentage height is refused with the reason it cannot work: a sentence is
flowed text, so there is nothing for it to be a percentage of and the browser
ignores it. v8 is deployed and DSH has verified both numbers round-tripping.
This commit is contained in:
2026-09-24 13:18:52 +08:00
parent b19f28492e
commit 1531263bd2
10 changed files with 499 additions and 104 deletions
+29 -23
View File
@@ -412,30 +412,36 @@ line":
| an image inside a run of text | **inline**, on the line with the words, height capped at `12em` |
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. 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.
`{width=400px}`, `{height=300px}`, or both in one block — and each declaration is
validated to digits plus at most one unit before it becomes a CSS length, so no
second declaration can be smuggled through it. A **width is exact and a height is
a cap**: a width has a reference (the column, or a length the writer chose), while
nothing measures a height against anything, and enlarging a picture past its own
pixels only makes it soft. In the flow of a sentence a height is always a
downscale, which comes to the same thing as exact. A bare number is a share of
the column for a width — `parseImageWidth` only reads pixels when the value says
`px` — and pixels for a height, because there is nothing else it could be; a
percentage *height* is refused rather than stored, since it has nothing to be a
percentage of and the browser's answer to one is to ignore it.
Leaving the size off means the picture prints at its own width, capped to the
column — and that turned out to be the wrong default to *live* with. Figures
come out of a plotting tool at whatever size they were made: one paper's are
1441, 1857, 2409 and 4169 pixels wide, so every one of them is wider than the
1276px column and they all printed at the full width, with the near-square ones
(1857×1986, 1974×2068) filling a whole page at 1365px tall. A written size is
therefore part of the editor: a row that is a figure carries a 宽度 control —
presets, or any value typed in — and beside it what the picture will measure on
the page (`原图 1857×1986 · 显示 893×955`), measured against the real column
behind the dialog. The control writes the attribute through the reader's own
`parseImageWidth`, so what it shows is what gets printed, and a value that
function cannot read is refused instead of stored as a size that does nothing.
Whichever of the two is given alone, the other follows from the picture's own
shape, so a figure can be sized but not stretched; when a writer does give both,
they are read as a box to fit *inside* rather than a box to fill. Measured across
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.
Leaving the size off was the wrong default to *live* with. Figures come out of a
plotting tool at whatever size they were made: that paper's are 1441, 1857, 2409
and 4169 pixels wide, so all sixteen were wider than the 1276px column and every
one printed at the full width, with the near-square ones (1857×1986, 1974×2068)
filling a page at 1365px tall. So the size became part of the editor: a row that
is a figure carries a 宽度 and a 高度 box — presets, or any value typed in — with
what the picture will measure on the page beside them (`原图 1857×1986 · 显示
638×682`), worked out against the column actually rendered behind the dialog.
Both boxes write through the reader's own `parseImageWidth` /
`parseImageHeight`, so what they show is what gets printed, and a value those
functions cannot read is refused instead of stored as a size that does nothing.
Configuration is three variables (`MINIO_ENDPOINT`, `MINIO_ACCESS_KEY`,
`MINIO_SECRET_KEY`, plus `MINIO_BUCKET`). Left empty, the paper still writes and