Vai al contenuto principale
Ricetta numero 17

Ricettario · Capitolo 3 · Titoli e aperture

Cinque aperture di capitolo in un libro

Un’apertura sul livello 1 e quattro stili di titolo indicati nel Markdown; ogni stile cambia l’accento, alcuni anche margini, colonne o numeri di pagina.

In questa pagina
Output
Canvas
Livello
Avanzato
Postext
Provata con Postext 1.65.0
Richiede ≥ 1.25.0
Licenza
Aggiornata il 10 ott 2026
Codice MIT · Testo CC BY 4.0

pp. 4–5 di 7

  • Esempio in inglese: non c’è ancora un’edizione italiana
  • Formato 180 × 240 mm
  • 1 colonna
  • Lora 10,8/14,5
  • Fraunces
  • Geist
  • Geist Mono
  • 7 pagine
  • Livello
  • Postext 1.57.1
  • Impaginato in 72 ms
  • 249 righe di codice

In breve

Un breve libro su come possono cominciare i capitoli, in cui ogni capitolo si apre in uno stile diverso. Mostra come dare a un capitolo colori, margini e impaginazione propri con una sola impostazione.

Che cosa comporrai

Five Ways to Begin è un libro di sette pagine sulle aperture di capitolo, 180 × 240 mm, e ogni capitolo è composto nell’apertura che descrive. Il capitolo uno è quella di un romanzo: un occhiello spaziato, un titolo in corsivo chiaro e un’iniziale rossa di cinque righe, con le testatine nelle pagine che seguono. Il capitolo due mette il titolo e un fregio tipografico in un cartiglio color crema disegnato con due filetti blu. Il capitolo tre impila il titolo in Fraunces Black bianco su un campo magenta che esce al vivo dalla pagina, sopra un’iniziale monospaziata in negativo su un riquadro. Il capitolo quattro, il manuale, ha testatine monospaziate, un numero in un bollino arancione accanto al titolo e due colonne di Geist. La coda compone il titolo a metà di una pagina vuota, sopra un’iniziale rialzata. Il capitolo uno usa l’apertura propria del livello 1; gli altri quattro indicano uno stile di titolo nella riga del loro titolo.

Questa ricetta risponde a

  • Come do a ogni capitolo una foto, un colore o una variante di apertura propri?
  • Come aggiungo a un’apertura una riga d’autore, un sommario o un attacco con capolettera?
  • Come compongo il titolo di un capitolo a tutta larghezza mentre il testo scorre sotto su due colonne?
  • Come costruisco un badge numerico accanto al titolo che si allarga quando il numero cresce (9 → 10)?

La risposta breve

script.js · righe 16–52nel codice completo
// A heading line picks a style by id, and its attributes feed the opener's {attr.…} texts:
//   # The Frame {style="framed" kicker="Chapter the Second"}
// A style overrides the level fields it names (opener, drop cap) and, until the next level-1
// heading, the section fields it names; the rest comes from the level and the document.
const level1 = () => ({ level: 1,
  // span 'page' opens each chapter at the head of a new page and runs its opener across
  // every column (chapter four has two). By default an H1 waits for a recto, with a blank
  // page before it; parity 'any' lets these short chapters open on either side.
  span: 'page',
  breakBefore: { enabled: true, parity: 'any' },
  marginBottom: pt(0), // each opener ends where its first paragraph starts
  advancedDesign: literary, // chapter one, unstyled: the book's own opener
  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
    gap: mm(2.5) } }); // a five-line drop in rubric red
const headingStyles = () => [ // factories, called by config() once every design exists
  // Each style brings its opener and swaps 'accent' for a colour of its own, on its pages.
  { id: 'framed', advancedDesign: framed, palette: { accent: palette.lapis },
    margins: { left: mm(EVEN), right: mm(EVEN) }, // a centred text block; 'accent' is blue:
    dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 600, color: col('accent'),
      gap: mm(2) } },
  { id: 'stacked', advancedDesign: stacked, palette: { accent: palette.magenta }, // white, on
    dropCap: { lines: BOXED, fontFamily: 'Geist Mono', fontWeight: 700, color: col('paper'),
      gap: mm(2.2) } }, // the square the opener draws for it
  { id: 'spec', advancedDesign: spec, palette: { accent: palette.signal }, dropCap: false,
    marginBottom: pt(0), // the hairline under the title is the separator
    margins: { left: mm(SPEC), right: mm(SPEC) }, // a 144 mm text block
    layout: { layoutType: 'double', gutterWidth: mm(7) },
    bodyStyle: { fontFamily: 'Geist', fontSize: pt(9.8), textAlign: 'left' }, // on the grid
    header: { elements: [...specHead('even'), ...specHead('odd')] },
    footer: { elements: [] } }, // no folio at the foot: the head gives the page
  { id: 'quiet', numbered: false, // a coda: it advances no chapter counter
    advancedDesign: quiet, palette: { accent: palette.graphite }, footer: tinyFolio,
    dropCap: { lines: 1, fontFamily: 'Fraunces', fontWeight: 300, fontSize: pt(46), // raised:
      color: col('accent'), gap: mm(1.2) }, // on the first baseline, rising into the white
    margins: { left: mm(QUIET.inner), right: mm(QUIET.outer) }, // a 106 mm measure
    bodyStyle: { fontSize: pt(QUIET.size), lineHeight: pt(QUIET.lead), textAlign: 'left' } },
]; // hook-up in config(): headings: { levels: [level1(), …] }, headingStyles()

Ingredienti

Tipo
Lora, Fraunces, Geist, Geist Mono (SIL OFL 1.1)
Risorse
Nessuna: ogni immagine è disegnata nel codice

Preparazione

#1 · Imposta sul livello l’apertura propria del libro

script.js · righe 97–105nel codice completo
// The first paragraph, in :::paragraphs{style="lead"}, starts 17 mm under the title.
const literary = design([
  text('kicker', '{attr.kicker}', 'Geist', 8, centred(22), { ...caps(1.7), align: 'center' }),
  text('title', '{titleText}', 'Fraunces', 32, at('#kicker', 'below', 0, 4, 'fill'),
    { fontWeight: 300, italic: true, lineHeight: 1.05, align: 'center' }),
  { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(0.8), color: col('accent'),
    placement: at('#title', 'below', (MEASURE - 18) / 2, 6, 18) }, // an 18 mm rule, centred
  room('#title', 17),
]);

Il capitolo uno non ha stile, quindi prende la grafica propria del livello 1, da cui parte ogni stile (la risposta breve mostra il livello e gli stili). Il primo paragrafo del capitolo sta nel Markdown, in un gruppo :::paragraphs{style="lead"} un corpo più grande del testo (12/17 pt contro 10,8/14,5), e il dropCap del livello lo apre (drop caps): la M poggia sulla quinta linea di base e, senza fontSize, la sua sommità è alla stessa altezza delle maiuscole della prima riga, con entrambe le altezze delle maiuscole misurate da Fraunces e Lora. L’apertura finisce con un riquadro vuoto, room(), 17 mm sotto il titolo, dove comincia il paragrafo d’apertura; un :::space sotto di esso lascia una riga e fa partire il testo senza rientro.

#2 · Incornicia il titolo in due riquadri

script.js · righe 109–127nel codice completo
const FRAME = 62; // mm: the outer box's height
const INSET = 3; // mm: the hairline box runs this far inside it
const FLEURON = 44; // mm: the ornament's width, centred under the title
const framed = design([ // array order is paint order: the boxes first, the type on top
  { kind: 'box', id: 'frame', placement: { ...at('container', 'top-left', 0, 0),
    size: { width: 'fill', height: mm(FRAME) } },
  style: { backgroundColor: col('tint'), borderColor: col('accent'), borderWidth: pt(1.6) } },
  { kind: 'box', id: 'hairline', placement: { ...at('#frame', 'align-top', INSET, INSET),
    size: { width: mm(MEASURE - 2 * INSET), height: mm(FRAME - 2 * INSET) } },
  style: { borderColor: col('accent'), borderWidth: pt(0.5) } },
  text('kicker', '{attr.kicker}', 'Geist', 7.5, centred(14), { ...caps(1.6), align: 'center' }),
  text('title', '{titleText}', 'Fraunces', 34, at('#kicker', 'below', 0, 3.5, 'fill'),
    { fontWeight: 600, lineHeight: 1.05, align: 'center' }),
  // An image element draws a registered resource, never numbered or captioned. Its resourceId
  // may be '{attr.photo}' (postext ≥ 1.8): then each heading names its own photo.
  { kind: 'image', id: 'fleuron', resourceId: 'fleuron',
    placement: at('#title', 'below', (MEASURE - FLEURON) / 2, 4.5, FLEURON) },
  room('#frame', 9), // the text starts 9 mm under the frame
]);

L’ordine dell’array è l’ordine di disegno, quindi prima vengono i riquadri e sopra il testo; il fregio è un elemento immagine, che disegna una risorsa SVG registrata mai numerata né citata. Un elemento immagine è anche il modo in cui ogni capitolo riceve una foto sua: da postext 1.8 il suo resourceId accetta segnaposto, quindi con resourceId: '{attr.photo}' un solo stile disegna la foto che ogni titolo indica, # The Frame {style="framed" photo="lake"}, come fanno i piatti della scheda ricetta (image elements). Lo stile framed nella risposta breve imposta palette per rendere blu accent nelle pagine di questo capitolo, così i filetti, l’occhiello e il numero di pagina in basso si stampano in blu, e i suoi margins centrano la gabbia del testo sotto il cartiglio. Il dropCap dello stile usa col('accent'), che la tavolozza rende anch’esso blu, così la L di tre righe si accorda con il cartiglio.

#3 · Impila il titolo, chiudi l’iniziale in un riquadro

script.js · righe 131–160nel codice completo
const FIELD = 130; // mm from the trim: the foot of the colour field
const S_BEARING = 1.2; // mm: the white left of the title's S (0.0365 em at 94 pt)
// The boxed initial: the style's drop cap sets the white letter; the opener draws the square,
// fitted by hand in pt. The text starts on the grid line 8 mm under the field (from the trim).
const TEXT_TOP = Math.ceil(((FIELD - TOP + 8) * 72) / 25.4 / LEAD) * LEAD; // pt
const BASE = 0.8; // a line's baseline stands 0.8 down its line box
const CAPS = 0.7; // the height of a capital in Lora, em
// Geist Mono, em: cap height, the advance every capital shares, the white either side of an M
const MONO = { caps: 0.71, advance: 0.6, side: 0.044 };
const BOXED = 3; // lines the initial spans
const capsTop = TEXT_TOP + BASE * LEADIN.lead - CAPS * LEADIN.size; // the first line's capitals
const foot = TEXT_TOP + (BOXED - 1 + BASE) * LEADIN.lead; // the third baseline: the letter's foot
// The M runs from the first line's capitals to its foot; the square runs past both as far as
// the M's sides sit in, so the colour is as wide on all four sides.
const letter = (foot - capsTop) / MONO.caps, pad = MONO.side * letter;
const stacked = design([
  { kind: 'box', id: 'field', style: { backgroundColor: col('accent') },
    placement: { anchor: { to: 'bleed', edge: 'top-left' }, size: { height: mm(FIELD) } } },
  text('kicker', '{attr.kicker}', 'Geist', 8, at('container', 'top-left', 0, 4),
    { ...caps(1.8), color: col('paper') }),
  // {titleText} keeps the \\ breaks from the Markdown, so each line ends at the writer's \\.
  // Moved left by the S's side bearing, so its curve meets the text edge.
  text('title', '{titleText}', 'Fraunces', 94,
    at('container', 'top-left', -S_BEARING, 15, 'fill'),
    { fontWeight: 900, lineHeight: 0.84, color: col('paper') }),
  { kind: 'box', id: 'initial', style: { backgroundColor: col('accent') }, reserve: false,
    placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: pt(capsTop - pad) },
      size: { width: pt(MONO.advance * letter), height: pt(foot - capsTop + 2 * pad) } } },
  room('container', (TEXT_TOP * 25.4) / 72, 'top-left'), // the opener ends where the text starts
]);

La pila è scritta nel Markdown: # Say it \\ out \\ loud. spezza {titleText} a ogni \\, mentre le testatine e la struttura del PDF leggono il titolo su una riga (line breaks in titles). Il campo pende dalla testa dell’abbondanza, a tutta larghezza e profondo 130 mm, e il paragrafo d’apertura comincia sulla prima riga della griglia 8 mm sotto (TEXT_TOP). L’iniziale in riquadro è fatta di due pezzi. Il dropCap dello stile compone la M su tre righe del paragrafo, in bianco, e l’apertura disegna sotto di essa un riquadro magenta, collocato a partire da TEXT_TOP, dall’interlinea del paragrafo e dalle metriche di Geist Mono, così il colore sopra e sotto la M è largo quanto i suoi spazi laterali. Il riquadro non occupa spazio nell’apertura (reserve: false); un riquadro room vuoto chiude l’apertura dove comincia il paragrafo.

#4 · Dai al manuale un badge, due colonne e testatine proprie

script.js · righe 181–208nel codice completo
const SPEC = 18; // mm: both side margins, for a 144 mm text block in two columns
const PAD = 1.6; // mm of colour over the badge's figures
const spec = design([
  { kind: 'rule', id: 'top-rule', direction: 'horizontal', thickness: pt(2.4), color: col('ink'),
    placement: at('container', 'top-left', 0, 0, 'fill') },
  text('kicker', '{attr.kicker}', 'Geist Mono', 7.5, at('container', 'top-left', 0, 3.2),
    { ...caps(1.2, 500), color: col('graphite') }),
  // The badge's width follows its text, with padding on both sides, so 9 → 10 widens it. Its
  // line box holds 1 mm more under the figures than over them, so the foot pads 1 mm less.
  text('badge', '{chapterNumber}', 'Geist Mono', 26, at('container', 'top-left', 0, 12),
    { fontWeight: 700, lineHeight: 1, box: { backgroundColor: col('accent'), padding: {
      top: mm(PAD), right: mm(2.4), bottom: mm(PAD - 1), left: mm(2.4) } } }),
  // Same size and line height as the badge, dropped by its top padding, so the baselines meet.
  text('title', '{titleText}', 'Geist', 26, at('#badge', 'right-of', 4, PAD),
    { fontWeight: 700, lineHeight: 1 }),
  { kind: 'rule', id: 'hairline', direction: 'horizontal', thickness: pt(0.5), color: col('ink'),
    placement: at('container', 'top-left', 0, 31, 'fill') }, // room for a one-line title
]);
const specHead = (parity) => { // mono labels over the opener's rule, on every page of the section
  const [edge, s] = parity === 'even' ? ['top-left', 1] : ['top-right', -1];
  const mono = { ...caps(1.1, 500), color: col('graphite'), parity, pages: 'all' };
  return [
    text(`sp-page-${parity}`, '{pageNumber} / {totalPages}', 'Geist Mono', 7,
      at('container', edge, 0, HEAD_Y), mono),
    text(`sp-chap-${parity}`, '§ {chapterNumber} · {chapterTitle}', 'Geist Mono', 7,
      at('container', edge, s * 16, HEAD_Y), mono),
  ];
};

Il bollino è un elemento di testo con un fondo e una spaziatura interna ma senza larghezza, quindi la sua larghezza segue {chapterNumber}, e il titolo è collocato right-of rispetto a esso: al capitolo 10 il bollino cresce di una cifra, 5,5 mm, e il titolo si sposta con lui. La spaziatura inferiore è 1 mm più piccola di quella superiore perché il riquadro della riga contiene altrettanto spazio in più sotto le cifre, così il 4 resta centrato. Nello stile spec, layout compone la sezione su due colonne e lo span: 'page' del livello stende l’apertura su entrambe. I suoi margins allargano la gabbia del testo a 144 mm, bodyStyle compone Geist in bandiera sulla griglia da 14,5 pt del libro, header sostituisce le testatine del libro e un footer vuoto toglie il numero di pagina.

#5 · Lascia spazio alla coda

script.js · righe 212–221nel codice completo
const quiet = design([ // the kicker is in the accent, which this style turns graphite
  text('kicker', '{attr.kicker}', 'Geist', 7.5, at('container', 'top-left', 0, 92),
    caps(1.6, 500)),
  text('title', '{titleText}', 'Fraunces', 26, at('#kicker', 'below', 0, 3),
    { fontWeight: 300, lineHeight: 1.1 }),
  room('#title', 3), // and the initial's rise, in whole lines: the text 16 mm under the title
]);
const tiny = (parity, edge) => text(`tiny-${parity}`, '{pageNumber}', 'Geist', 6.5,
  at('container', edge, 0, -12), { fontWeight: 500, color: col('muted'), parity });
const tinyFolio = { elements: [tiny('even', 'bottom-left'), tiny('odd', 'bottom-right')] };

L’occhiello pende 92 mm sotto la testa della gabbia del testo, e l’iniziale rialzata è il capolettera di una riga dello stile quiet: il suo piede poggia sulla prima linea di base e, a 46 pt, sale nel bianco sopra invece di affondare nel paragrafo. Il paragrafo lascia libera quella salita sopra di sé in righe intere, quindi il room dell’apertura sotto il titolo è di soli 3 mm. Nello stile quiet, margins restringono la giustezza a 106 mm, bodyStyle compone il testo in bandiera a 12,5/17,5 pt, footer sostituisce il numero di pagina in basso con uno piccolo nell’angolo esterno e palette rende grigio l’occhiello. Niente in questa pagina stampa un numero di capitolo, quindi numbered: false si vedrebbe solo in un capitolo aggiunto dopo la coda, che sarebbe comunque il quinto.

La ricetta completa

Sandbox
// ═══ Postext Cookbook · Nº 017 · Five chapter openers in one book ═════════════════
// https://postext.dev/en/cookbook/five-chapter-openers
// Code: MIT · Text: original (CC BY 4.0) · Ornament: generated in code (CC BY 4.0)
// Fonts: Lora, Fraunces, Geist, Geist Mono (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// A small book about openers, each chapter set in the one it describes. Every heading style
// brings its opener and palette; three move the margins, two bring their own type and folios.
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'five-chapter-openers';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region answer: one opener per chapter: the level's own, then heading styles by name
// A heading line picks a style by id, and its attributes feed the opener's {attr.…} texts:
//   # The Frame {style="framed" kicker="Chapter the Second"}
// A style overrides the level fields it names (opener, drop cap) and, until the next level-1
// heading, the section fields it names; the rest comes from the level and the document.
const level1 = () => ({ level: 1,
  // span 'page' opens each chapter at the head of a new page and runs its opener across
  // every column (chapter four has two). By default an H1 waits for a recto, with a blank
  // page before it; parity 'any' lets these short chapters open on either side.
  span: 'page',
  breakBefore: { enabled: true, parity: 'any' },
  marginBottom: pt(0), // each opener ends where its first paragraph starts
  advancedDesign: literary, // chapter one, unstyled: the book's own opener
  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
    gap: mm(2.5) } }); // a five-line drop in rubric red
const headingStyles = () => [ // factories, called by config() once every design exists
  // Each style brings its opener and swaps 'accent' for a colour of its own, on its pages.
  { id: 'framed', advancedDesign: framed, palette: { accent: palette.lapis },
    margins: { left: mm(EVEN), right: mm(EVEN) }, // a centred text block; 'accent' is blue:
    dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 600, color: col('accent'),
      gap: mm(2) } },
  { id: 'stacked', advancedDesign: stacked, palette: { accent: palette.magenta }, // white, on
    dropCap: { lines: BOXED, fontFamily: 'Geist Mono', fontWeight: 700, color: col('paper'),
      gap: mm(2.2) } }, // the square the opener draws for it
  { id: 'spec', advancedDesign: spec, palette: { accent: palette.signal }, dropCap: false,
    marginBottom: pt(0), // the hairline under the title is the separator
    margins: { left: mm(SPEC), right: mm(SPEC) }, // a 144 mm text block
    layout: { layoutType: 'double', gutterWidth: mm(7) },
    bodyStyle: { fontFamily: 'Geist', fontSize: pt(9.8), textAlign: 'left' }, // on the grid
    header: { elements: [...specHead('even'), ...specHead('odd')] },
    footer: { elements: [] } }, // no folio at the foot: the head gives the page
  { id: 'quiet', numbered: false, // a coda: it advances no chapter counter
    advancedDesign: quiet, palette: { accent: palette.graphite }, footer: tinyFolio,
    dropCap: { lines: 1, fontFamily: 'Fraunces', fontWeight: 300, fontSize: pt(46), // raised:

Lo script.js composto funziona così com’è: incollalo nello script modulo di qualsiasi pagina, oppure apri la ricetta su CodePen. Cartella della ricetta su GitHub ↗ (si apre in una nuova scheda)

Varianti

#Manda ogni capitolo su un recto

Con 'odd' ogni capitolo si apre su una pagina destra e i capitoli dal due al cinque ricevono ognuno un verso bianco prima, così il libro passa da 7 a 11 pagine.

-  breakBefore: { enabled: true, parity: 'any' },
+  breakBefore: { enabled: true, parity: 'odd' },

#Un’iniziale di tre righe

Senza fontSize il corpo segue il numero di righe, quindi un’iniziale di tre righe si allinea ancora alle maiuscole della prima riga.

-  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
+  dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),

#Maiuscoletto dopo l’iniziale

leadIn compone in maiuscoletto le prime parole dopo la lettera, come fanno molti romanzi; words: 'line' prende l’intera prima riga.

-    gap: mm(2.5) } });
+    gap: mm(2.5), leadIn: { words: 'line' } } });

Errori frequenti

Errore frequente

Valori degli attributi: niente { o }; virgolette semplici per un valore con "

Il valore di un attributo finisce alla graffa di chiusura, quindi non può contenere { o }. Un valore che contiene una virgoletta doppia va tra virgolette semplici; il simbolo del dollaro va bene. Attributi dei titoli →

  • I paragrafi d’apertura sono composti in bandiera, come nell’apertura di una rivista; un paragrafo giustificato accanto a un’iniziale di cinque righe può allargare molto gli spazi in una riga di poche parole.
  • Il riquadro magenta è adattato a mano al Lora 12/17, alla M di Geist Mono e alla linea di base della riga, a 0,8 dell’altezza del suo riquadro di riga (BASE). Se cambi il carattere, il corpo o l’interlinea del paragrafo, la lettera, il numero di righe o la profondità del campo, ricontrolla il riquadro rispetto alle maiuscole della prima riga.
  • I titoli non ruotano e non scorrono in verticale: il testo grafico è sempre orizzontale. Impila invece le parole con \\, come fa il capitolo tre.

Crediti

Testo
Testo originale, CC BY 4.0
Font
Lora (SIL OFL 1.1) · Fraunces (SIL OFL 1.1) · Geist (SIL OFL 1.1) · Geist Mono (SIL OFL 1.1)
Sandbox