Kurz gesagt
Sagen Sie dem Layout, welcher Teil jedes Fotos immer sichtbar sein muss. Fehlen einer Spalte ein paar Zeilen, wird das Foto höher, statt eine Lücke zu lassen.
Was Sie setzen
Drei Seiten eines Küstenmagazins: eine Reportage über die Handwerke einer Stadt an einer Flussmündung, mit einem randabfallenden Foto über den Auftakt und fünf Fotos in den Spalten. Der Pen setzt die Reportage zweimal aus demselben Text. In der ersten Fassung behalten die Fotos das Format ihrer Dateien, und auf Seite 2 enden beide Spalten eine Zeile vor dem Ende des Rasters: die linke unter ihrem letzten Absatz, die rechte über dem Foto an ihrem Fuß. In der zweiten gibt jedes Foto den Teil des Bildausschnitts an, der immer sichtbar bleiben muss, und die Engine beschneidet außerhalb davon: Auf derselben Seite wachsen die Muschelsammlerin und der Netzflicker um je eine Zeile, und beide Spalten enden auf der letzten Rasterzeile. Niemand musste die Zuschnitte wählen.
Dieses Rezept beantwortet
- Wie lasse ich ein Foto beschneiden, damit es eine kurze Spalte füllt, während sein Motiv immer sichtbar bleibt?
- Wie erreiche ich bündige Spaltenenden und eine ausgeglichene letzte Seite (vertikaler Ausgleich)?
- Wie bestimme ich, wo eine Abbildung steht: oben auf der Seite, über beide Spalten, genau hier oder am Rand?
Die kurze Antwort
// Each photo names the rectangle that must always show, in fractions of the file:
// x and width of its width, y and height of its height. Outside it the engine may crop,
// so the photo can stand taller (sides cut, down to the area's width) or lower (top and
// bottom cut, down to its height) than the file, always as wide as its column.
const SAFE_AREAS = {
mariscadora: { x: 0.2, y: 0.36, width: 0.34, height: 0.46 }, // Carmen, her rake and basket
redeira: { x: 0.34, y: 0.14, width: 0.4, height: 0.66 }, // Rosa and the net in her lap
carpintero: { x: 0.12, y: 0.2, width: 0.64, height: 0.62 }, // Manuel and the whole hull
faro: { x: 0.34, y: 0.14, width: 0.32, height: 0.56 }, // the tower and the walker below it
pulpeira: { x: 0.2, y: 0.08, width: 0.56, height: 0.82 }, // Lucía, the octopus, the cauldron
};
// Columns end flush on this grid by whole lines. A heading or a photo that does not fit a
// column's foot leaves lines empty there; these settings forbid the usual fixes (space above
// a heading, under a photo, or a paragraph run long), so only a photo with a safe area can
// take them, by growing a line at a time. Without safe areas the short columns stay short.
const balancing = { enabled: true, maxLinesPerHeading: 0, stretchAfterLists: false,
stretchAfterFloats: false, looseParagraphs: false };
Zutaten
- Funktionen
- Sicherer BildbereichSpaltenausgleichPlatzierung von AbbildungenAbbildungen und Tabellen als RessourcenVerweise, die Abbildungen platzierenAbbildungen genau hierFormat der BildunterschriftEigene RessourcentypenGestaltete AuftakteBilder in SeitengestaltungenÜberschriftenattributeEine oder zwei SpaltenGespiegelte RänderKolumnentitel und SeitenzahlenKopfzeilen nach SeitenrolleSemantische FarbpaletteAbsatzformateSeiten auf einem CanvasPDF-Export
- Nutzt außerdem
- Abbildung und Tabelle in Ihrer SpracheKapitelband über die ganze BreiteIm PDF eingebettete Schriften
- Schrift
- Newsreader, Archivo, Archivo Narrow (SIL OFL 1.1)
- Dateien
carpintero-1600.jpgestuario-1700.jpgfaro-1600.jpgmariscadora-1600.jpgpulpeira-1600.jpgredeira-1600.jpg- The estuary at low water at dawn, with shellfish gatherers (Generated With Diffusion Models, eigener Text)
- A shellfish gatherer kneeling on the wet sand (Generated With Diffusion Models, eigener Text)
- A net mender on the quay (Generated With Diffusion Models, eigener Text)
- A shipwright planing a wooden boat in his shed (Generated With Diffusion Models, eigener Text)
- A lighthouse on a granite point and a walker below it (Generated With Diffusion Models, eigener Text)
- A cook lifting an octopus from a copper cauldron at a market (Generated With Diffusion Models, eigener Text)
Zubereitung
#1 · Markieren, was bleiben muss, sonst nichts
Der Code steht oben in der kurzen Antwort. Ein Sicherheitsbereich besteht aus vier Bruchteilen des Bilds, gemessen von seiner oberen linken Ecke. Die Engine darf das Foto in jedem Format zwischen der ganzen Datei und diesem Rechteck zeigen, so breit wie seine Spalte: höher, indem sie die Seiten beschneidet, niedriger, indem sie oben und unten beschneidet. Was außerhalb des Sicherheitsbereichs liegt, fällt im Verhältnis zu den Rändern auf jeder Seite weg, sodass Rosa, rechts der Mitte auf dem Kai, rechts der Mitte bleibt, wenn die Seiten beschnitten werden.
#2 · Den Ausgleich den Spielraum nur über die Fotos nehmen lassen
Dieselbe Region stellt den Ausgleich ein. Seine üblichen Hebel fügen Weißraum hinzu: eine Zeile über einer Überschrift, Luft unter einem Foto, einen Absatz, der eine Zeile länger läuft. Hier sind sie ausgeschaltet, sodass die schlichte Fassung jede kurze Spalte so zeigt, wie sie ist. Ein Foto mit Sicherheitsbereich wird ohnehin zuerst versucht, vor jedem dieser Hebel, weil ein höheres Foto kein Loch im Text hinterlässt.
#3 · Der Sicherheitsbereich gehört zur Ressource
const FILES = { estuario: [1700, 1133] }; // px, declared as they are; the rest 1600 × 1067
// Caption and alt text of each photo, one block per photo: content.figures.<lang>.md.
const figureTexts = String.raw`mariscadora
Carmen Lago sorts cockles on her plot of the estuary at low water.
A woman in green boots kneels on wet sand beside a rake and a mesh basket.
redeira
Rosa Doval mends a purse-seine panel on the quay.
A woman on a stool sews a green net spread around her; fishing boats behind.
carpintero
Manuel Barreiro planes the planking of the gamela.
An old man planes the side of a half-built wooden boat inside a shed.
faro
Arnela point: the lighthouse and the coastal path.
A white lighthouse on granite rocks; a walker with a red backpack below it.
pulpeira
Lucía Fraga lifts an octopus out of the copper cauldron.
A woman in an apron lifts a boiled octopus from a steaming copper pot.
`;
const CAPTIONS = Object.fromEntries(figureTexts.trim().split(/\n\s*\n/)
.map((block) => block.split('\n').map((line) => line.trim()))
.map(([id, caption, alt]) => [id, [caption, alt]]));
const PLACEMENT = { pulpeira: { position: 'here' } }; // the rest float to the first free slot
const photo = (id, safe) => {
const [w, h] = FILES[id] ?? [1600, 1067];
const [caption, alt] = CAPTIONS[id] ?? [];
return { id, typeId: 'figure', kind: 'bitmap', createdAt: 0, updatedAt: 0,
bitmap: { fileId: `${id}-${w}.jpg`, format: 'jpeg', width: w, height: h },
...(caption && { caption, altText: alt, placement: PLACEMENT[id] ?? { position: 'auto' } }),
...(safe && SAFE_AREAS[id] && { safeArea: SAFE_AREAS[id] }) };
};
const resources = (safe) => ['estuario', ...Object.keys(CAPTIONS)].map((id) => photo(id, safe));
safeArea steht an der Ressource neben Datei, Legende und Platzierung, sodass ein Foto sie behält, wohin es auch gleitet, und in jedem Dokument, das es verwendet. Der Pen übergibt sie oder lässt sie weg, und das ist der einzige Unterschied zwischen den beiden Fassungen. Die Flussmündung des Auftakts zeichnet der Überschriftenstil, sie gleitet nicht und braucht deshalb keine.
#4 · Beide bauen und vergleichen
const build = (safe) => buildDocumentWithFonts({ markdown, resources: resources(safe) }, config(),
kitFonts(FONTS));
const docs = { plain: await build(false), safe: await build(true) };
Beide Dokumente entstehen aus einer Konfiguration und einem Markdown-String. Nur die Seiten, auf denen der Text weiterläuft, werden bis zum Fuß gefüllt: Seite 3 schließt die Reportage ab, und auf einer Schlussseite müssen die Spalten nur untereinander gleich hoch enden, was sie hier schon tun, also wächst dort kein Foto. Die PDF-Schaltfläche baut die Fassung, die gerade auf dem Tisch liegt, und das PDF beschneidet jedes Foto auf dieselbe Weise auf seinen Rahmen.
Das ganze Rezept
// ═══ Postext Cookbook · Nº 116 · Photos that grow to fill a short column ═══════════
// https://postext.dev/en/cookbook/photos-fill-short-columns
// Code: MIT · Text: original (CC BY 4.0) · Photos: diffusion models
// Fonts: Newsreader, Archivo, Archivo Narrow (SIL OFL 1.1) · Needs postext ≥ 1.25.0
//
// A magazine feature set twice. Without safe areas some columns end short; with them the
// engine crops each photo outside its area to set it taller, and the photos fill those lines.
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage, defaultResourceTypes,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';
const LANG = 'en'; // @lang: the language of the sample document ('es' | 'en')
const RECIPE = 'photos-fill-short-columns';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: six named colours, the accents taken from the photos: sea slate, net green
const palette = {
ink: '#1d2326', // text: a cold near-black
sea: '#2f5d73', // the accent: kicker, caption labels, folios, references
net: '#3f6b55', // the second colour: the opener's rule
rule: '#c9d1d3', // hairlines
muted: '#5f6a6e', // running heads, credits, the colophon
paper: '#ffffff',
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
// The engine's defaults link to 'main-color': pointed at the accent, no default blue shows.
{ id: 'main-color', name: 'sea (defaults)', value: { hex: palette.sea, model: 'hex' } },
];
// #endregion
const [TEXT, DISPLAY, LABEL] = ['Newsreader', 'Archivo', 'Archivo Narrow'];
const LEAD = 13.6; // body leading in pt: the grid the photos grow by
const [PAGE_W, PAGE_H, TOP, BOTTOM, INNER, OUTER, GUTTER] = [225, 297, 22, 22, 18, 16, 7]; // mm
const PHOTO_H = 150; // the opener's bleed photo: 225 × 150 mm, the shape of the file
const at = (to, edge, x, y, size) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) },
...(size && { size }) });
const text = (id, content, family, size, color, placement, extra) => ({ kind: 'text', id,
content, fontFamily: family, fontSize: pt(size), color: col(color), placement,
align: 'left', overflow: 'wrap', ...extra }); // the running heads ask for an ellipsis instead
const caps = (size) => ({ fontWeight: 700, textTransform: 'uppercase',
letterSpacing: pt(size * 0.18) });
// #region answer: a safe area per photo, and balancing that may only grow pictures
// Each photo names the rectangle that must always show, in fractions of the file:
// x and width of its width, y and height of its height. Outside it the engine may crop,
// so the photo can stand taller (sides cut, down to the area's width) or lower (top and
Das zusammengesetzte script.js läuft unverändert: Fügen Sie es in das Modulskript einer beliebigen Seite ein, oder öffnen Sie das Rezept auf CodePen. Ordner des Rezepts auf GitHub ↗ (öffnet sich in einem neuen Tab)
Varianten
#Ein Foto im Textfluss bei seinem Text halten
Ein mit position: 'here' gesetztes Foto mit Sicherheitsbereich wird oben und unten beschnitten, wenn es für den Platz, der in seiner Spalte bleibt, etwas zu hoch ist, statt in die nächste zu rücken.
-const PLACEMENT = { pulpeira: { position: 'here' } }; // the rest float to the first free slot
+const PLACEMENT = { pulpeira: { position: 'here' }, faro: { position: 'here' } };#Dem Ausgleich seine übrigen Hebel zurückgeben
Mit den Standardwerten kommen die Fotos weiterhin zuerst, und Überschriften und Gleitobjekte übernehmen, was der Sicherheitsbereich eines Fotos nicht schafft.
-const balancing = { enabled: true, maxLinesPerHeading: 0, stretchAfterLists: false,
- stretchAfterFloats: false, looseParagraphs: false };
+const balancing = { enabled: true };Häufige Fehler
- Ein Sicherheitsbereich, der das ganze Bild abdeckt, oder einer mit einer Seite unter 2 % davon wird ignoriert: Das Foto wird ganz gezeigt, wie ohne Sicherheitsbereich.
- Nur Gleitobjekte in einer Spalte und Fotos im Textfluss wachsen. Ein Foto, das über beide Spalten gleitet, behält sein Format, weil sein Wachsen jede Spalte darunter zugleich verschieben würde.
Nachweise
- Rezept
- Ignacio Ferro
- Text
- The feature, the captions and the colophon, in Spanish and English; Arnela and its people are imaginary · Postext Cookbook · CC BY 4.0
- Bilder
- The estuary at low water at dawn, with shellfish gatherers · Generated With Diffusion Models · eigener Text
- A shellfish gatherer kneeling on the wet sand · Generated With Diffusion Models · eigener Text
- A net mender on the quay · Generated With Diffusion Models · eigener Text
- A shipwright planing a wooden boat in his shed · Generated With Diffusion Models · eigener Text
- A lighthouse on a granite point and a walker below it · Generated With Diffusion Models · eigener Text
- A cook lifting an octopus from a copper cauldron at a market · Generated With Diffusion Models · eigener Text
- Schriften
- Newsreader (SIL OFL 1.1) · Archivo (SIL OFL 1.1) · Archivo Narrow (SIL OFL 1.1)
- Code
- MIT, wie Postext
Diesen Text bearbeiten ↗ (öffnet sich in einem neuen Tab)Ordner des Rezepts auf GitHub ↗ (öffnet sich in einem neuen Tab)


