跳到主要内容
食谱编号19

排版食谱 · 第5章 · 图书结构

用一个属性给各篇配色

一本袖珍野外指南,每个:::part设定一个调色板条目,它的篇名页、涂色的背面、书口色标、识别特征和目录行都印成这种颜色。

第2–3页,共10页

  • 英文样例:尚无中文版本
  • 成品尺寸150 × 200 mm
  • 1栏
  • Alegreya 10.5/14.5
  • Barlow Condensed
  • Zilla Slab
  • 10页
  • 难度
  • Postext 1.57.1
  • 排版用时70 ms
  • 245行代码

简单来说

一本河口鸟类袖珍图鉴,分泥滩褐和芦苇绿两部分。示例演示每部分只设一个颜色,就能染上它的篇名页、书边标签、页码和目录行。

成品一览

《Birds of the Estuary》是一本分两篇的袖珍野外指南,每篇一种生境。泥滩篇印成泥褐色,芦苇丛篇印成芦苇绿,每种颜色只写一次,写在开启这一生境的:::part中。每张篇名页都是满版出血的色块,上面有150 pt的罗马数字,这一生境的鸟种用米色列出。这一页的背面涂成同样的颜色,书口处有反白的篇色标。在鸟种页上,书口色标、页码、眉题、拉丁学名下的线、粗体的识别特征、项目符号和图版标签都取该篇的颜色。目录页上方是一条河口图带,每篇占一条自己颜色的色带,并标出篇名页的页码。封面、图带和四幅图版是用扩散模型生成的画,以JPEG位图载入;篇的调色板作用不到图片,所以图片保留自己的颜色。

这道食谱解答

  • 怎样把书分成若干篇或部分,每篇有自己的颜色和隔页?
  • 怎样在章首页和空白页上隐藏书眉,或者把空白左页涂成篇的颜色?
  • 怎样添加自动更新的目录(前导符、页码、作者、篇名行)?
  • 怎样给正文或框里的关键词(粗体或斜体)着色?

简短回答

script.js · 第53–86行在完整代码中
// In the Markdown:  :::part{number="I" title="The \\ Mudflats" palette="band=#8c5e24"}
// Every colour below that is linked to 'band' takes #8c5e24 until the next part.
const parts = { // passed to the config as `parts`
  // A divider opens on a recto by default. The break after it goes to the next recto too, so
  // the back of the leaf stays blank and versoDesign paints it (gotcha: verso-design-breakafter).
  breakAfter: { parity: 'odd' },
  margins: { top: mm(125) }, // the fence's text starts low, under the title
  design: { elements: [ // the divider: its container is the whole trim
    box('field', col('band'), { ...at('top-left', 0, 0, 'bleed'), ...fill }),
    text('part', t({ en: 'Part', es: 'Parte' }), { ...label, fontSize: pt(10),
      letterSpacing: pt(2.4), color: col('paper') },
    at('top-left', MARGIN.inner, MARGIN.top)), // a recto: the inner margin is on the left
    // Roman for the parts, Arabic for the species. {numberRoman} re-formats number="I" (or
    // "1"); the species design prints {number}, from numberingTemplate '{1}'.
    text('numeral', '{numberRoman}', { ...display, fontSize: pt(150), lineHeight: 0.9,
      color: col('paper') }, below('part', 0)),
    // The \\ in the title breaks the line here; the contents and the heads get one line.
    text('title', '{titleText}', { ...display, fontSize: pt(46), lineHeight: 0.98,
      color: col('paper') }, below('numeral', 2, { width: mm(MEASURE) })),
    rule('rule', col('paper'), 1, below('title', 7, { width: mm(14) })),
  ] },
  // The back of the leaf: the same band, edge to edge, the part's tab in reverse at the
  // fore-edge (a verso's is on the left) and its name at the foot.
  versoDesign: { elements: [
    box('field', col('band'), { ...at('top-left', 0, 0, 'bleed'), ...fill }),
    text('tab', '{partNumber}', { ...label, fontSize: pt(9), color: col('band'), align: 'center',
      box: { backgroundColor: col('paper') } }, { ...at('top-left', 0, TAB.y), size: TAB.size }),
    text('name', '{partTitle}', { ...label, fontSize: pt(9), letterSpacing: pt(2.4),
      color: col('paper') }, at('bottom-left', MARGIN.outer, -MARGIN.bottom)),
  ] },
  // The fence's list of species, in the paper colour on the band.
  bodyStyle: { fontSize: pt(11), color: col('paper'), textAlign: 'left', numberColor: col('paper'),
    orderedLists: { fontFamily: 'Barlow Condensed', separator: '', gap: mm(4) } },
};

用料

类型
Alegreya, Zilla Slab, Barlow Condensed(SIL OFL 1.1)
素材
  • cover-1152.jpg
  • curlew-1200.jpg
  • redshank-1200.jpg
  • reedling-1200.jpg
  • strip-1536.jpg
  • warbler-1200.jpg
  • The cover: a curlew on the estuary mud, reeds in front (Generated With Diffusion Models, 原创)
  • The contents strip: a curlew and a redshank on the mud, the reedbed beyond (Generated With Diffusion Models, 原创)
  • Plate 1: Eurasian curlew (Generated With Diffusion Models, 原创)
  • Plate 2: common redshank (Generated With Diffusion Models, 原创)
  • Plate 3: bearded reedling (Generated With Diffusion Models, 原创)
  • Plate 4: common reed warbler (Generated With Diffusion Models, 原创)

做法

#1 · 一个供各篇覆盖的调色板条目

script.js · 第16–29行在完整代码中
const palette = {
  ink: '#1f2624', // text: a green-tinted near-black
  band: '#3c4b4f', // the house slate, before any part; each :::part brings its own
  paper: '#f6f3ea', // the page, and the type set on a band
  rule: '#d5d1c4', // hairlines
  muted: '#61675f', // running heads, Latin names, the colophon
};
// The paletteId is the link a part's palette="band=#…" follows, in text and designs alike.
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': point it at the ink, so nothing prints blue.
  { id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];

篇按id覆盖调色板条目,所以一切应随生境变化的内容都链接到同一个条目band。它自己的值是石板灰,只在篇外印出(封面的眉题和目录下方说明的标题),配置中没有其他颜色用同一个十六进制值。col()给每个颜色带上paletteId,篇的覆盖正是沿着这个id生效,文字样式和设计元素都一样。

#2 · 分隔页、它的背面和它的列表

这一步的代码见上面的简短回答。:::part在右页开启一张分隔页,parts.design在整个成品尺寸上排出它。围栏之间的Markdown从距顶边125 mm处、标题之下开始:先是用habitat段落样式排的几行生境介绍,然后是用bodyStyle排的鸟种列表。versoDesign只在篇名页背面为空白时才涂色(色块和反白色标),而breakAfter: { parity: 'odd' }把第一个鸟种推到下一个右页,使背面保持空白。{numberRoman}把篇的number="I"印成罗马数字(写"1"也会印成I),而鸟种用阿拉伯数字编为1到4。标题中的\\只在篇名页上换行;目录和书眉把标题排在一行。

#3 · 正文的强调色跟随篇

script.js · 第90–108行在完整代码中
const bodyText = {
  fontFamily: 'Alegreya', fontSize: pt(10.5), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('band'), // the field marks: **Bill**, **Voice.**
  italicColor: col('ink'), referenceColor: col('ink'),
  firstLineIndent: mm(4), indentAfterHeading: false,
  // Tighter than the 0.6–2 defaults. runtMinCharacters counts word spaces, not letters:
  // 45 are about 20 letters of Alegreya (the default 20, about 9); a shorter last line is a runt.
  minWordSpacing: 0.75, maxWordSpacing: 1.7, runtMinCharacters: 45,
};
const unorderedLists = { bulletChar: '•', color: col('band'), // the field marks' bullets
  marginTop: pt(0), marginBottom: pt(0) };
// The plates are numbered with the species, and their label is in the part's colour.
const resourceTypes = [{ id: 'plate', numberingTemplate: '{n}', resetOn: 'never',
  counterFormat: 'decimal', ...t({
    en: { name: 'Plate', namePlural: 'Plates', shortLabel: 'Pl.', captionPrefix: 'Plate' },
    es: { name: 'Lámina', namePlural: 'Láminas', shortLabel: 'Lám.', captionPrefix: 'Lámina' },
  }) }];
const captionStyle = { fontSize: pt(8.5), labelColor: col('band'), descriptionItalic: true,
  gap: mm(1.8) };

从一个:::part到下一个,凡是等于band自身值的文字颜色都换成该篇的值(:::part容器)。这里指的是粗体的识别特征、项目符号和图版标签。正文和斜体仍用墨色。图版有自己的资源类型,标签为Plate,从不重置,所以编号为1到4,与鸟种同步。

#4 · 鸟种首页

script.js · 第112–123行在完整代码中
const species = { enabled: true, slot: { elements: [
  text('kicker', '{number} · {attr.status}', { ...label, fontSize: pt(8.5),
    letterSpacing: pt(1.7), color: col('band') }, at('top-left', 0, 1, 'container')),
  // 'cm' is a unit symbol: it keeps its lower case, so the size is not set in capitals.
  text('size', '{attr.size}', { ...label, textTransform: 'none', fontSize: pt(8.5),
    letterSpacing: pt(0.5), color: col('muted') }, at('top-right', 0, 1, 'container')),
  text('name', '{titleText}', { ...display, fontSize: pt(22), lineHeight: 1.05,
    color: col('ink') }, below('kicker', 1.5, { width: 'fill' })),
  text('latin', '{attr.latin}', { fontFamily: 'Alegreya', italic: true, fontSize: pt(11.5),
    color: col('ink') }, below('name', 0.8)),
  rule('rule', col('band'), 0.75, below('latin', 2, { width: mm(MEASURE) })),
] } };

鸟种首页印出标题的三个属性:status在眉题中编号之后,size在眉题右端,latin在名称之下。眉题和拉丁学名下的线链接到band,所以一个设计通用于两种生境。标题本身不设分页:每个鸟种都在:::pagebreak之后开始,或在篇之后的分页之后开始,所以它的页面算作正文页,印有书眉。尺寸不随标签改成大写,因为cm是单位符号。

#5 · 目录中每篇一条色带

script.js · 第127–145行在完整代码中
const contents = {
  levels: [{ level: 1, fontFamily: 'Zilla Slab', fontSize: pt(12), fontWeight: 600,
    numberFontFamily: 'Barlow Condensed', numberFontWeight: 600, numberColor: col('muted'),
    numberWidth: mm(5), numberGap: mm(3), marginTop: pt(4) }],
  pageNumber: { fontFamily: 'Barlow Condensed', fontSize: pt(10), fontWeight: 600, width: mm(7) },
  leader: { char: '. ' },
  subtitle: { enabled: true, attr: 'latin', fontFamily: 'Alegreya', fontSize: pt(9.5),
    color: col('muted') }, // the Latin name, in italic by default
  // A part row lays out this design with the part's number, title, page and palette.
  parts: { height: mm(8.5), marginTop: pt(LEAD), design: { elements: [
    box('row', col('band'), { ...at('top-left', 0, 0, 'container'), ...fill }),
    text('part', t({ en: 'Part {number}', es: 'Parte {number}' }), { ...label, fontSize: pt(8),
      letterSpacing: pt(1.6), color: col('paper') }, at('left', 3, 0, 'container')),
    text('title', '{titleText}', { ...display, fontSize: pt(12), color: col('paper') },
      at('left', 20, 0, 'container')),
    text('page', '{pageNumber}', { ...label, fontSize: pt(10), color: col('paper'),
      align: 'right' }, at('right', -2.5, 0, 'container')),
  ] } },
};

:::toc给每个:::part一行,在其中用该篇的编号、标题、篇名页页码和调色板排出toc.parts.design(目录),所以同一个框在泥滩篇印成褐色,在芦苇丛篇印成绿色。色带下的每个鸟种行显示编号、点状前导符和页码,下一行是取自latin属性的拉丁学名。

#6 · 书眉和色标中的篇

script.js · 第149–162行在完整代码中
const head = (id, content, parity, placement, style = {}) => ({ ...text(id, content, { ...label,
  fontSize: pt(8), letterSpacing: pt(1.4), color: col('muted'), overflow: 'clip', ...style },
placement), parity, pages: 'body' }); // dividers are 'part' pages, their versos 'blank'
const folio = { fontSize: pt(9), fontWeight: 700, color: col('band') };
const tab = (parity, edge) => head(`tab-${parity}`, '{partNumber}', parity,
  { ...at(edge, 0, TAB.y), size: TAB.size },
  { fontSize: pt(9), color: col('paper'), align: 'center', box: { backgroundColor: col('band') } });
const header = { elements: [
  head('verso-folio', '{pageNumber}', 'even', at('top-left', MARGIN.outer, HEAD.y), folio),
  head('verso-title', '{title}', 'even', at('top-left', MARGIN.outer + HEAD.gap, HEAD.y)),
  head('recto-title', '{partTitle}', 'odd', at('top-right', -(MARGIN.outer + HEAD.gap), HEAD.y)),
  head('recto-folio', '{pageNumber}', 'odd', at('top-right', -MARGIN.outer, HEAD.y), folio),
  tab('even', 'top-left'), tab('odd', 'top-right'), // on the fore-edge, left on a verso
] };

设置pages: 'body'后,书眉只印在正文页上。篇名页属于part页,涂色的背面属于blank页;封面和目录都以一个通页宽标题开头,因此算作首页。左页书眉印出{title},即frontmatter中的标题,书名因此留在 Markdown 里。右页书眉在一行中印出{partTitle},书口处的色标把{partNumber}印在一个背景链接到band的框上。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 019 · Parts in colour from one attribute ═══════════════════════
// https://postext.dev/en/cookbook/parts-in-colour
// Code: MIT · Text: original (CC BY 4.0) · Pictures: diffusion models
// Fonts: Alegreya, Zilla Slab, Barlow Condensed (SIL OFL 1.1) · Needs postext ≥ 1.57.0
// A pocket field guide to two habitats. Each :::part names its own 'band' colour, and every
// colour linked to 'band' takes it: the divider and its verso, the tab, the field marks.
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'parts-in-colour';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: 'band' is the entry the parts override; the others keep their value
const palette = {
  ink: '#1f2624', // text: a green-tinted near-black
  band: '#3c4b4f', // the house slate, before any part; each :::part brings its own
  paper: '#f6f3ea', // the page, and the type set on a band
  rule: '#d5d1c4', // hairlines
  muted: '#61675f', // running heads, Latin names, the colophon
};
// The paletteId is the link a part's palette="band=#…" follows, in text and designs alike.
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': point it at the ink, so nothing prints blue.
  { id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];
// #endregion
const TRIM = { width: 150, height: 200 }; // a pocket guide
const MARGIN = { top: 22, bottom: 20, inner: 19, outer: 21 }; // mirrored; room for a thumb
const MEASURE = TRIM.width - MARGIN.inner - MARGIN.outer; // 110 mm, about 70 characters
const LEAD = 14.5; // body leading in pt: the baseline grid
const STRIP = 44; // the contents' picture strip, from the top edge (mm)
const HEAD = { y: 12, gap: 8 }; // running heads from the top edge; folio to title (mm)
const TAB = { y: 26, size: { width: mm(8), height: mm(24) } }; // the thumb tab, at the fore-edge
const TITLE_DROP = 6; // mm from the foot of the contents strip to the title's box
const BOOK = t({ en: 'Birds of the Estuary', es: 'Aves del estuario' });
const label = { fontFamily: 'Barlow Condensed', fontWeight: 600, textTransform: 'uppercase' };
const display = { fontFamily: 'Zilla Slab', fontWeight: 700 };
const at = (edge, x, y, to = 'page') => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
const below = (id, y, size) => ({ ...at('below', 0, y, `#${id}`), ...(size && { size }) });
const fill = { size: { width: 'fill', height: 'fill' } };
const text = (id, content, style, placement) => ({ kind: 'text', id, content, overflow: 'wrap',
  align: 'left', ...style, placement });
const box = (id, color, placement) => ({ kind: 'box', id, style: { backgroundColor: color },
  placement });

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

变化

#加第三种生境

在第一个盐沼鸟种之前加一个围栏,就多出第三种颜色,连同它的篇名页、涂色的背面和目录行,配置不用改。同时去掉目录下方的说明,因为第三条色带和它的鸟种需要这块空间。

+:::part{number="III" title="The \\ Saltmarsh" palette="band=#6b4a7a"}
+:::
+
+# Common Shelduck {latin="Tadorna tadorna" size="58–67 cm" status="Resident"}

#让鸟种与分隔页相对

去掉跳到下一个右页的分页后,第一个鸟种从篇名页背面开始,也就没有空白的背面留给versoDesign涂色。

-  breakAfter: { parity: 'odd' },

常见问题

易错点

parts.versoDesign需要parts.breakAfter的parity为'odd'

只有当parts.breakAfter { enabled: true, parity: 'odd' }让篇章页的背面(左页)空出来时,这一页才会被绘制。 篇页 →

易错点

第1页是右页:按物理页码规划页面

第1页是右页(奇数页),第2页是第一个左页(偶数页),所以要按物理页码规划跨页:偶数页上的章首页对着它后面的奇数页。 换页与换栏 →

  • 篇按值比较来给文字换色:任何等于band自身值的文字颜色都会随篇切换,即使它链接到另一个条目或没有链接。这个十六进制值只留给band。
  • 色标印{partNumber},而它在篇外为空,所以第一个:::part之前的正文页会得到一个空的石板灰色标。让每个正文页都在某个篇之内。
  • 图片和行内色块保留其中写定的颜色。这里的图版从代码中的HABITAT取得各自生境的颜色,所以在:::part中加新颜色时,也要加到那里。

致谢

文本
原创文字, CC BY 4.0
图片
  • The cover: a curlew on the estuary mud, reeds in front · Generated With Diffusion Models · 原创
  • The contents strip: a curlew and a redshank on the mud, the reedbed beyond · Generated With Diffusion Models · 原创
  • Plate 1: Eurasian curlew · Generated With Diffusion Models · 原创
  • Plate 2: common redshank · Generated With Diffusion Models · 原创
  • Plate 3: bearded reedling · Generated With Diffusion Models · 原创
  • Plate 4: common reed warbler · Generated With Diffusion Models · 原创
字体
Alegreya (SIL OFL 1.1) · Zilla Slab (SIL OFL 1.1) · Barlow Condensed (SIL OFL 1.1)
沙盒