跳到主要内容
食谱编号66

排版食谱 · 第3章 · 标题与章首页

带题头画和提要的童书章节

一个标题样式开启每一章;每个标题在属性中指定自己的题头画和斜体提要,由首页设计绘出。

本页内容
输出
Canvas
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.25.0
许可证
更新于2026年10月10日
代码MIT · 文本CC BY 4.0

页码6–7 · 第1–2页,共5页

  • 英文样例:尚无中文版本
  • 成品尺寸190 × 240 mm
  • 1栏
  • Averia Serif Libre 13/19
  • Fredericka the Great
  • Quicksand
  • 5页
  • 难度
  • Postext 1.57.1
  • 排版用时59 ms
  • 162行代码

简单来说

意大利语儿童版《木偶奇遇记》的开头几页。每章开头都有一幅水彩小插图和一段简短的内容提要。

成品一览

把科洛迪《Le avventure di Pinocchio》一个意大利语童书版本的开头几页,用Averia Serif Libre以13/19 pt排在190 × 240 mm的页面上。左页的书名页与第一章相对:一幅托斯卡纳丘陵的水彩画,一辆驴车把柴火运往木匠作坊,下方是Fredericka the Great字体的书名。每章都在一幅渐隐入纸色的水彩题头画下开始(第一章是举斧对着会说话的木头的Maestro Ciliegia,第二章是戴黄色假发接过木头的Geppetto),接着是章序行和居中斜体的科洛迪提要。四幅图都是用扩散模型根据同一个风格提示词和页面调色板生成的。书眉只印在第8页和第9页,即没有章首页的两页。页码放在木色圆片中,两颗樱桃为第一章收尾,不带题注和编号。

这道食谱解答

  • 怎样给每章不同的照片、颜色或章首页样式?
  • 怎样排不编号的插图:饰图、小花饰、标志?
  • 怎样给章首页加作者行、提要或带首字下沉的导语?
  • 怎样在章首页和空白页上隐藏书眉,或者把空白左页涂成篇的颜色?

简短回答

script.js · 第44–64行在完整代码中
// # Capitolo I {style="capitolo" vignetta="ceppo" summary="Come andò che Maestro Ciliegia…"}
// resourceId takes placeholders like a design text: '{attr.vignetta}' draws the resource the
// heading names, so every chapter shares one style.
const VIGNETTE = { width: 84, height: 56 }; // mm, centred at the head of the text block
const SUMMARY = 124; // mm: the summary's measure, narrower than the text's
const opener = { enabled: true,
  // A floor for the height the opener reserves, in whole lines, so the text under chapter I's
  // two-line summary starts on the same grid line as the text under chapter II's three lines.
  minHeight: pt(14 * LEAD),
  slot: { elements: [
    { kind: 'image', id: 'vignette', resourceId: '{attr.vignetta}',
      placement: at('container', 'top', 0, 0,
        { width: mm(VIGNETTE.width), height: mm(VIGNETTE.height) }) },
    // The chapter line 6 mm under the picture's foot, the summary 5 mm under the chapter line.
    text('chapter', '{titleText}', DISPLAY, 24, at('container', 'top-left', 0,
      VIGNETTE.height + 6, { width: mm(MEASURE) }), { ...caps(2.4), lineHeight: 1 }),
    text('summary', '{attr.summary}', TEXT, 12.5, at('#chapter', 'below',
      (MEASURE - SUMMARY) / 2, 5, { width: mm(SUMMARY) }), { italic: true, lineHeight: 1.38 }),
  ] },
};
const chapter = { id: 'capitolo', advancedDesign: opener };

用料

类型
Averia Serif Libre, Fredericka the Great, Quicksand(SIL OFL 1.1)
素材
  • ceppo-1200.jpg
  • ciliegie-600.jpg
  • frontespizio-1400.jpg
  • polenta-1200.jpg
  • The frontispiece: Tuscan hills, a hill village and a donkey cart of firewood on its way to the carpenter’s workshop (Generated With Diffusion Models, 原创)
  • Chapter I’s vignette: Maestro Ciliegia, axe raised over the talking log (Generated With Diffusion Models, 原创)
  • Chapter II’s vignette: Geppetto in his yellow wig receives the log, beside a pot of polenta (Generated With Diffusion Models, 原创)
  • The cherry tailpiece (Generated With Diffusion Models, 原创)

做法

#1 · 一个标题样式,每个标题指定一幅题头画

这一步的代码见简短回答。图片元素的resourceId接受与设计文字相同的占位符,所以'{attr.vignetta}'绘制标题指定的资源,就像{attr.summary}印出它的提要一样。两章共用capitolo样式,并在标题行中指定各自的图:# Capitolo II {style="capitolo" vignetta="polenta" summary="…"}。再加一章只需多一个资源和一个标题,不用再建样式(图片元素)。章序行从容器顶部定位,位于题头画下方6 mm处,提要挂在它下方5 mm处;章首预留的高度一直到提要的最后一行。用minHeight设一个14行的下限后,第一章正文与第二章正文从同一行开始,尽管第二章的提要多一行;没有它时,第一章正文会高出6.7 mm(span与高级版面设计)。

#2 · 把画作作为位图资源载入

script.js · 第122–133行在完整代码中
const PICTURES = { // id: [file, width, height, alt text in the book's language]
  frontespizio: ['frontespizio-1400.jpg', 1400, 1105, 'Colline toscane con cipressi e un borgo; '
    + 'un contadino guida un asinello con un carretto di legna verso la bottega di un falegname.'],
  ceppo: ['ceppo-1200.jpg', 1200, 800, 'Maestro Ciliegia, con la scure alzata sopra un pezzo di '
    + 'legno sul banco, si guarda intorno spaventato.'],
  polenta: ['polenta-1200.jpg', 1200, 800, 'Geppetto, con la parrucca gialla, riceve il pezzo di '
    + 'legno da Maestro Ciliegia; accanto, un paiolo di polenta fumante.'],
  ciliegie: ['ciliegie-600.jpg', 600, 626, 'Due ciliegie rosse su un picciolo, con una foglia.'],
};
const resources = Object.entries(PICTURES).map(([id, [fileId, w, h, altText]]) => ({
  id, typeId: 'fregio', kind: 'bitmap', createdAt: 0, updatedAt: 0, altText,
  bitmap: { fileId, format: 'jpeg', width: w, height: h } }));

每幅图都是食谱assets/文件夹中的一个JPEG,按像素尺寸声明:版面只读取这些尺寸,Canvas绘制页面时,画出loadImage()在各个文件id下登记的图像。题头画和樱桃都在白底上生成,再与纸色#fffaf0正片叠底,所以它们的留白印出来就是页面本身,图片没有可见的边缘。卷首插图裁成190:150,即其区域的比例,因此缩放后的图像能填满区域,不留纸色条带。风景画和题头画由版面设计中的图片元素绘制,樱桃则由Markdown中的::resource{id="ciliegie"}绘制。

#3 · 把书名页做成一个标题样式

script.js · 第68–85行在完整代码中
const SKY = 150; // mm from the top edge: the foot of the painted field
// # Pinocchio {style="frontespizio" kicker="Le avventure di"}; subtitle and author come from
// the frontmatter. Every element hangs from the page, so the field runs off three edges.
const frontispiece = {
  id: 'frontespizio',
  footer: { elements: [] }, // no folio; the running heads already skip openers
  advancedDesign: { enabled: true, slot: { elements: [ // array order is paint order
    { kind: 'image', id: 'landscape', resourceId: 'frontespizio', // cut to the field's 190:150
      placement: at('page', 'top-left', 0, 0, { width: 'fill', height: mm(SKY) }) },
    text('kicker', '{attr.kicker}', LABEL, 11, at('page', 'top', 0, SKY + 11),
      { ...caps(2.6), fontWeight: 700 }),
    text('title', '{titleText}', DISPLAY, 70, at('page', 'top', 0, SKY + 17),
      { lineHeight: 1, color: col('bark') }),
    text('subtitle', '{subtitle}', TEXT, 15, at('page', 'top', 0, SKY + 44), { italic: true }),
    text('author', '{author}', LABEL, 9, at('page', 'top', 0, SKY + 58),
      { ...caps(2), fontWeight: 600 }),
  ] } },
};

书名页是Markdown的第一个标题# Pinocchio {style="frontespizio" kicker="Le avventure di"},它的样式绘出页面上的一切:从上边缘起150 mm高的风景画,然后是属性中的眉题、书名,以及前置元数据中的副标题和作者。所有元素都从页面定位,而不是从版心定位,所以标题虽然留在全书唯一的一栏里,画作仍然铺到页面的上边缘和左右两边。空的footer去掉了章首页印的页码圆片,而pages: 'body'已经让书眉不出现在这一页(标题样式)。

#4 · 正文页上的书眉,圆片中的页码

script.js · 第89–110行在完整代码中
// The excerpt opens on book page 6, a verso, so the title page faces chapter I.
const continuation = { pageIndexOffset: 5, pageNumbering: { startAt: 6 } };
const SHIFT = (INNER - OUTER) / 2; // mm: the text block's centre sits off the page's centre
const head = (id, content, parity, x) => text(id, content, LABEL, 8,
  at('page', 'top', x, 12), { ...caps(1.8), fontWeight: 600, parity, pages: 'body' });
const DISC = 7.4; // mm: the folio's disc
// A fixed square with a radius of half its side is a circle; the figures sit in its middle.
// lineHeight 1.17: the baseline falls at 0.8 of the line, 0.94 em down, so Quicksand's
// figures (0.70 em tall) leave 0.23 em above and below them, and 'middle' centres that line.
const disc = (id, parity, pages, edge, x) => text(id, '{pageNumber}', LABEL, 8.5,
  at('page', edge, x, -(BOTTOM - DISC) / 2, { width: mm(DISC), height: mm(DISC) }),
  { fontWeight: 700, color: col('paper'), lineHeight: 1.17, verticalAlign: 'middle', parity,
    pages, box: { backgroundColor: col('wood'), borderRadius: mm(DISC / 2) } });
const header = { elements: [
  head('verso-title', '{title}', 'even', -SHIFT), // the book on the left-hand page
  head('recto-chapter', '{chapterTitle}', 'odd', SHIFT), // the chapter on the right
] };
const footer = { elements: [
  disc('verso-folio', 'even', 'body', 'bottom-left', OUTER),
  disc('recto-folio', 'odd', 'body', 'bottom-right', -OUTER),
  disc('opener-folio', 'all', 'opener', 'bottom', 0), // a chapter opens on either side
] };

continuation从书中第6页(一个左页)开始给节选编页码,所以书名页与第一章相对,印出的第一个页码是7。pages: 'body'让书眉不出现在书名页和两个章首页上,所以书眉只印在第8页和第9页,左页印书名,右页印章名(文字元素)。页码是一个放在7.4 mm固定方框里的设计文字,圆角半径为边长的一半,于是成了一个圆;verticalAlign: 'middle'加上注释中算出的行距,让数字在圆中居中。纸色数字与木色的对比度为4.98:1。加大字距的标签不用任何偏移:居中一行时,最后一个字母后的那份字距不计在内。

#5 · 用不编号的章尾饰图收束一章

script.js · 第114–118行在完整代码中
// No caption prefix and no caption, so the cherries that close chapter I print with no
// label or number. The pictures the design slots paint are of this type too.
const resourceTypes = [{ id: 'fregio', name: 'Fregio', namePlural: 'Fregi', shortLabel: 'Fregio',
  captionPrefix: '', numberingTemplate: '{n}', resetOn: 'never', counterFormat: 'decimal',
  defaultPlacement: { position: 'here', width: 0.16, align: 'center' } }];

资源类型会在题注前印出它的captionPrefix和编号。前缀为空、又没有题注时,樱桃下面什么也不印;设captionPrefix: 'Fregio'时,同一个资源会在图下印出Fregio 1.,高度增加10 mm(资源类型)。该类型的defaultPlacement把每个资源放在其::resource行所在的位置,宽为行长的六分之一并居中,所以Markdown只需在章的最后一段后写::resource{id="ciliegie"}。其他图片也属于这个类型,但没有任何:ref或::resource指向它们,所以它们既不会被放置,也不会被计数。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 066 · Storybook chapters with vignettes and summaries ═══
// https://postext.dev/en/cookbook/storybook-chapter-vignettes
// Code: MIT · Text: C. Collodi, 1883 (PD, Gutenberg #52484) · Pictures: diffusion models
// Fonts: Averia Serif Libre, Fredericka the Great, Quicksand (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document (the sample is Italian in both)
const RECIPE = 'storybook-chapter-vignettes';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { // every colour in the config comes from here; the pictures were painted to it
  ink: '#2b2118', // text: a brown near-black
  paper: '#fffaf0', // a warm book paper, which the vignettes' margins were tinted to
  bark: '#6b4224', // chapter lines, running heads, the title
  wood: '#9a5f30', // the folio discs
};
// col(id): a colour linked to its palette entry, with the entry's hex beside the id.
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 defaults linked to main-color, bold among them, print in bark instead of blue.
  { id: 'main-color', name: 'bark (defaults)', value: { hex: palette.bark, model: 'hex' } },
];
// Text and summaries; the book's title and the chapter lines; running heads and folios.
const [TEXT, DISPLAY, LABEL] = ['Averia Serif Libre', 'Fredericka the Great', 'Quicksand'];
const TRIM = { width: 190, height: 240 }; // mm
const [TOP, INNER, OUTER] = [22, 22, 20]; // mm; mirrored, so the spine margin swaps sides
const MEASURE = TRIM.width - INNER - OUTER; // 148 mm
const [BODY, LEAD, LINES] = [13, 19, 29]; // pt, pt, and whole lines in the text block
const BOTTOM = TRIM.height - TOP - (LINES * LEAD * 25.4) / 72; // mm: 23.6
// Design-slot shorthands: a placement from an anchor's edge (mm), and a design text.
const at = (to, edge, x = 0, y = 0, size) => ({ anchor: { to, edge },
  offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const text = (id, content, fontFamily, size, placement, extra = {}) => ({ kind: 'text', id,
  content, fontFamily, fontSize: pt(size), color: col('ink'), align: 'center',
  overflow: 'wrap', // a long line wraps in every slot, the running heads too
  placement, ...extra });
const caps = (tracking) => ({ textTransform: 'uppercase', letterSpacing: pt(tracking),
  color: col('bark') });

// #region answer: one opener for every chapter: the vignette the heading names, then its summary
// # Capitolo I {style="capitolo" vignetta="ceppo" summary="Come andò che Maestro Ciliegia…"}
// resourceId takes placeholders like a design text: '{attr.vignetta}' draws the resource the
// heading names, so every chapter shares one style.
const VIGNETTE = { width: 84, height: 56 }; // mm, centred at the head of the text block
const SUMMARY = 124; // mm: the summary's measure, narrower than the text's

组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)

变化

#每章从右页开始

设为'odd'时,第二章移到第11页,第10页留空;书名页要关闭自己的分页,因为它继承了这一级的设置,否则会在空白的第6页之后移到第7页。

@@ the frontispiece style @@
   id: 'frontespizio',
+  breakBefore: { enabled: false },
@@ headings, in config() @@
-    levels: [{ level: 1, breakBefore: { enabled: true, parity: 'any' } }],
+    levels: [{ level: 1, breakBefore: { enabled: true, parity: 'odd' } }],

常见问题

易错点

属性值:不能含{ or };含"的值用单引号

属性值在右花括号处结束,所以不能包含{ or }。含双引号的值要放在单引号里;美元符号没有问题。 标题属性 →

  • 在Markdown中,每个科洛迪对白破折号都用一个不换行空格(U+00A0)与其后的词连在一起,所以收尾的破折号不会出现在行首;另外两个不换行空格让di legno和in piedi在各自段落末尾不被拆开。段落含粗体或斜体时,这些空格同样不会断行,例如第9页含ohi的那段,其中有四处。

致谢

文本
  • Le avventure di Pinocchio (1883): chapter I and the opening of chapter II with Collodi’s chapter summaries, from the 1902 Bemporad edition, with typographic apostrophes, dashes and ellipses; chapter I’s summary has the comma after “falegname” that the edition’s table of contents prints and its chapter head leaves out · Carlo Collodi · 公有领域
图片
  • The frontispiece: Tuscan hills, a hill village and a donkey cart of firewood on its way to the carpenter’s workshop · Generated With Diffusion Models · 原创
  • Chapter I’s vignette: Maestro Ciliegia, axe raised over the talking log · Generated With Diffusion Models · 原创
  • Chapter II’s vignette: Geppetto in his yellow wig receives the log, beside a pot of polenta · Generated With Diffusion Models · 原创
  • The cherry tailpiece · Generated With Diffusion Models · 原创
字体
Averia Serif Libre (SIL OFL 1.1) · Fredericka the Great (SIL OFL 1.1) · Quicksand (SIL OFL 1.1)
沙盒