简单来说
一本里斯本步行游览的袖珍指南。每条路线有自己的颜色,印在页边的色标上,用拇指一翻就能找到。
成品一览
Lisbon on Foot的前七页,这是一本120 × 200 mm的口袋导览书。封面是一面蓝白相间的azulejos瓷砖墙,书名印在一块像里斯本路牌那样加框的铭牌上;第2页介绍三条路线,附有路线图。每条路线有一种颜色,Alfama是陶土色,Baixa是黄色;切口上的书口色标以这种颜色和路线编号,出现在这条路线每一页的同一高度。章首色带、编号站点、右页书眉和Don’t miss框的底色都随之改变。把路线2送到右页的那页空白左页,涂成路线2的黄色,并印出它的编号。正文用Albert Sans,齐左排;地名用DM Serif Display,标签用Asap Condensed。
这道食谱解答
- 怎样在章首页和空白页上隐藏书眉,或者把空白左页涂成篇的颜色?
- 怎样设置书眉:左页放书名,右页放章名,页码在外侧?
- 怎样加编号标签(“BOX 1-1”)、角标图标,或带线的边栏图标?
- 怎样在正文、题注或表注里加入色块图例?
简短回答
// '# Alfama {style="alfama"}' opens a section that runs to the next level-1 heading. Its
// palette gives band, bandInk, onBand and wash the walk's values on the section's pages,
// in the page furniture and in the text alike.
const walkStyles = Object.keys(WALKS).map((id) => ({ id, palette: WALKS[id] }));
// The tab has one size and one place on every page, hanging from the foot of the opener
// band; only its colour and its number change from walk to walk.
const TAB = { y: BAND, width: 7, height: 24 }; // mm
const tab = (parity, edge) => text(`tab-${parity}`, '{chapterNumber}', {
...label, fontSize: pt(11), fontWeight: 700, color: col('onBand'), align: 'center',
verticalAlign: 'middle', overflow: 'clip', box: { backgroundColor: col('band') },
parity, pages: 'all', // the walk's first page is an 'opener' page: the tab goes there too
}, { ...at('bleed', edge, 0, TAB.y), ...size(mm(TAB.width), mm(TAB.height)) });
const tabs = [tab('odd', 'top-right'), tab('even', 'top-left')]; // on the fore-edge
// The blank verso that pushes a walk onto a recto belongs to that walk (gotcha:
// section-last-wins), so the field takes the next walk's colour and its numeral the number.
const blankPage = [
box('field', { backgroundColor: col('band') },
{ ...at('bleed', 'top-left', 0, 0), ...size('fill', 'fill') }),
tileImage('field-tiles', 'tiles-page', 'top-left', 'fill', 'auto'),
text('field-number', '{chapterNumber}', { ...display, fontSize: pt(220), lineHeight: 1,
color: col('onBand'), overflow: 'clip' }, at('page', 'bottom-left', MARGIN.outer, -24)),
].map((el) => ({ ...el, pages: 'blank' }));
用料
做法
#1 · 给路线要改变的颜色命名
const palette = {
ink: '#1b2430', // text: a blue-black
paper: '#fffdf8', // the page
band: '#1f4e9c', // the section colour: tile blue until a walk replaces it
bandInk: '#1d4a94', // the section colour for type on paper
onBand: '#ffffff', // type set on the band
wash: '#e8eef8', // the section's box tint
muted: '#5e6875', // running heads, notes, chip outlines
};
// Each walk's values for those four. Baixa yellow is too light for white type, so its
// onBand is the ink. The text is recoloured by value, so the four base values must differ.
const WALKS = {
alfama: { band: '#b9533a', bandInk: '#a1432b', onBand: '#ffffff', wash: '#f7e7e0' },
baixa: { band: '#e2b23d', bandInk: '#7d5a0f', onBand: '#1b2430', wash: '#fbf0d5' },
belem: { band: '#2b7a78', bandInk: '#236866', onBand: '#ffffff', wash: '#e1eeed' },
};
const NAMES = { alfama: 'Alfama', baixa: 'Baixa', belem: 'Belém' };
// A walk's palette follows the paletteId; the hex beside it is the colour's base value.
const col = (id) => ({ hex: palette[id] ?? WALKS[id].band, model: 'hex', paletteId: id });
const entry = (id, hex) => ({ id, name: id, value: { hex, model: 'hex' } });
const colorPalette = [
...Object.entries(palette).map(([id, hex]) => entry(id, hex)),
...Object.entries(WALKS).map(([id, w]) => entry(id, w.band)), // for :swatch{color="alfama"}
entry('main-color', palette.ink), // the engine's defaults link here: nothing prints blue
];
标题样式的palette在它所在分节的页面上,按id替换调色板条目(标题样式),所以每一种应当随路线变化的颜色,都链接到四个条目之一:band用于色块、色标和站点圆圈,bandInk用于纸面上的文字,onBand用于色块上的文字,wash用于框。白字在Baixa的黄色上对比度只有2:1,所以这条路线的onBand改用墨色,对比度为8:1。设计元素按id跟随,而文本按取值重新着色:与某个条目基础十六进制值相同的颜色,会取这条路线的值,所以四个基础值各不相同。第4页上的框标题和第5页上引你前往路线2的那一行,都是bandInk颜色的文本,两者都印成陶土色。
#2 · 只在正文页上放书眉
const head = (id, content, parity, placement, style = {}) => ({ ...text(id, content, {
...label, fontSize: pt(7.8), letterSpacing: pt(1.3), color: col('muted'), overflow: 'clip',
...style }, placement), parity, pages: 'body' }); // never on openers or blank pages
const folio = { fontSize: pt(8.5), fontWeight: 700, color: col('ink') };
// Anchored to the page: from its top-right corner a negative x runs inwards
// (gotcha: negative-offsets).
const heads = [
head('verso-folio', '{pageNumber}', 'even', at('page', 'top-left', MARGIN.outer, HEAD.y), folio),
head('verso-title', '{title}', 'even', at('page', 'top-left', MARGIN.outer + HEAD.gap, HEAD.y)),
head('recto-title', `${t({ en: 'Walk', es: 'Paseo' })} {chapterNumber} · {chapterTitle}`, 'odd',
at('page', 'top-right', -(MARGIN.outer + HEAD.gap), HEAD.y), { color: col('bandInk') }),
head('recto-folio', '{pageNumber}', 'odd', at('page', 'top-right', -MARGIN.outer, HEAD.y), folio),
];
// Array order is paint order: the blank page's field comes last and covers the tab there.
const header = { elements: [...heads, ...tabs, ...blankPage] };
// A walk's first page has no running head: its folio drops to the foot.
const footer = { elements: [{ ...head('drop-folio', '{pageNumber}', 'all',
at('container', 'top', 0, 7), folio), pages: 'opener', align: 'center' }] };
// Page 2 comes before Walk 1, where {chapterNumber} is empty, so its header has no tabs. With
// the level's break off (gotcha: style-inherits-break), it is a 'body' page.
const introStyle = { id: 'intro', numbered: false, header: { elements: heads },
breakBefore: { enabled: false }, advancedDesign: { enabled: false },
fontSize: pt(24), lineHeight: pt(2 * LEAD), marginBottom: pt(LEAD / 2) };
排版完成后,每一页都会得到一个角色(文本元素)。路线的第一页是opener,因为它的标题会分页;为了转到右页而补的那一页是blank;其余的是body。书眉取pages: 'body',所以第3页和第7页的章首页、第6页的空白页上都没有书眉,页脚页码取'opener'。左页印前置元数据里的{title},右页用bandInk印Walk {chapterNumber} · {chapterTitle},在第5页上是陶土色。简短回答里的色标取'all'。空白页的色块在数组中排在它们后面,由于数组顺序就是绘制顺序,它在第6页上盖住了色标。第2页是引言,位于路线1之前,所以那里的{chapterNumber}为空,它的样式有自己的header,含四个书眉而没有色标。没有这个header,第2页会印出一个空的蓝色色标。
#3 · 每条路线在色带下开始,并给站点编号
const walkLevel = { level: 1, breakBefore: { enabled: true, parity: 'odd' }, // a recto
// The band ends 30 mm into the text area; a floor of 8 grid lines, with nothing added
// under it, starts the text 7.3 mm below the band.
marginBottom: pt(0), advancedDesign: { enabled: true, minHeight: pt(8 * LEAD), slot: {
elements: [
box('band', { backgroundColor: col('band') },
{ ...at('bleed', 'top-left', 0, 0), ...size('fill', mm(BAND)) }),
tileImage('tiles', 'tiles-band', 'top-right', 'auto', mm(BAND)),
text('kicker', `${t({ en: 'Walk', es: 'Paseo' })} {chapterNumber}`, { ...label,
fontSize: pt(9), letterSpacing: pt(2), color: col('onBand') },
at('page', 'top-left', MARGIN.inner, 17)), // walks open on rectos: inner is left
text('title', '{titleText}', { ...display, fontSize: pt(34), lineHeight: 1,
color: col('onBand') }, { ...at('#kicker', 'below', 0, 1), ...size(mm(70), 'auto') }),
text('route', '{attr.route}', { fontFamily: 'Albert Sans', italic: true, fontSize: pt(10),
color: col('onBand') }, { ...at('#title', 'below', 0, 1.5), ...size(mm(70), 'auto') }),
] } } };
const STOP = 5.4; // mm: the diameter of a stop's circle
const stopLevel = { level: 3, numberingTemplate: '{3}', // restarts at every walk
// A grid line above a stop and no margin under it, so the circle can sit 3.2 mm down its
// two grid lines: about 8.3 mm of white above a stop and 2.1 under it, 6.4 between paragraphs.
marginTop: pt(LEAD), marginBottom: pt(0), advancedDesign: { enabled: true, slot: { elements: [
text('number', '{number}', { ...label, fontSize: pt(9), fontWeight: 700, align: 'center',
verticalAlign: 'middle', color: col('onBand'), overflow: 'clip',
box: { backgroundColor: col('band'), borderRadius: mm(STOP / 2) } },
{ ...at('container', 'top-left', 0, 3.2), ...size(mm(STOP), mm(STOP)) }),
text('name', '{titleText}', { ...display, fontSize: pt(13.5), lineHeight: 1.1,
color: col('ink') }, { ...at('#number', 'right-of', 2.4, 0.2), ...size('fill', 'auto') }),
] } } };
色带锚定在出血上,这里出血就是成品边缘,色带伸进正文区30 mm。设计在色带处结束,minHeight预留八条网格线,即37.3 mm;章首页取两者中较大的深度,所以在第3页和第7页上,正文从色带下方7.3 mm处开始。每个站点是一个三级标题,它的设计把{number}排在一个用band填充的5.4 mm圆圈里。模板{3}在每条路线重新计数,因为一级标题会重置更低级别的计数器,地图上的标记也带着同样的编号。站点上方有一条网格线的外边距,下方没有,圆圈位于标题所占的两条网格线中、往下3.2 mm处。这样圆圈上方约有8.3 mm空白,下方约有2.1 mm;段落之间相距6.4 mm。
#4 · 在框上挂一块瓷砖,行内标签保持墨色
const calloutStyles = [{ id: 'dontmiss', title: t({ en: 'Don’t miss', es: 'No te lo pierdas' }),
background: col('wash'), // one device: the walk's tint
// The tile hangs half off the box's outer side, flush with its top: the left side on a verso.
// With 6.5 mm of padding on both sides the title stays in line with the box's text there too.
// The tile's colours are drawn into the SVG, so it stays tile blue in every walk.
padding: { top: mm(3.4), right: mm(6.5), bottom: mm(3.4), left: mm(6.5) },
icon: { kind: 'resource', resourceId: 'tile', size: mm(10), position: 'corner',
cornerSide: 'outer' },
titleStyle: { ...label, fontSize: pt(8.5), fontWeight: 700, letterSpacing: pt(1.5),
color: col('bandInk') },
body: { fontSize: pt(9), lineHeight: pt(12.4), color: col('ink') } }];
// The facts are ink on paper in every walk: the walk's colour is kept for the tab, the stops
// and the box, and a chip linked to 'band' would follow it too.
const chipStyles = [{ id: 'fact', background: col('paper'), borderColor: col('muted'),
borderWidth: pt(0.6), borderRadius: mm(0.8), paddingX: mm(1.4), paddingY: mm(0.5),
fontFamily: 'Asap Condensed', fontSize: em(0.92), color: col('ink'), bold: true }];
这个框没有线条或边框,只有路线的底色,以及角上一块用代码画的瓷砖(标注框样式)。cornerSide: 'outer'把瓷砖一半挂在框的外侧之外,与框顶平齐;第4页是左页,外侧就是左边,也就是标题开始的那一边。两侧各有6.5 mm的内边距,标题不会碰到瓷砖,并且无论在哪一页都与框内文字对齐。路线的调色板会给底色和标题重新着色,瓷砖则保持蓝色,因为它的颜色画在SVG里。链接到band的行内标签也会跟着换成路线的颜色;这里不让路线信息参与换色,所以行内标签用纸面上的墨色,外加一圈muted的灰色轮廓,每条路线都一样。
#5 · 用路线的色块给地图做图例
const map = svg('routes', MAP.width, MAP.height, { placement: { position: 'here' }, ...t({ en: {
caption: 'The three walks. The numbered pins are the stops in the text.',
note: ':swatch{color="alfama"} Walk 1, Alfama · :swatch{color="baixa"} Walk 2, Baixa · '
+ ':swatch{color="belem"} Walk 3, Belém, 6 km west (inset)',
altText: 'Map of central Lisbon north of the river: the yellow route runs from the '
+ 'waterfront up the Baixa grid to Rossio; the terracotta route climbs east from the '
+ 'cathedral through Alfama. An inset shows the teal Belém route.',
}, es: {
caption: 'Los tres paseos. Los pines numerados son las paradas del texto.',
note: ':swatch{color="alfama"} Paseo 1, Alfama · :swatch{color="baixa"} Paseo 2, Baixa · '
+ ':swatch{color="belem"} Paseo 3, Belém, 6 km al oeste (en el detalle)',
altText: 'Plano del centro de Lisboa al norte del río: la ruta amarilla sube desde el '
+ 'muelle por la cuadrícula de la Baixa hasta el Rossio; la terracota sube hacia el este '
+ 'desde la catedral por Alfama. En un detalle, la ruta verde azulada de Belém.',
} }) });
地图是一个SVG资源,排在::resource{id="routes"}所在的位置,它有自己的资源类型,所以题注写作Map 1。题注用三个色块给路线做图例::swatch{color="alfama"},baixa和belem同理。色块取调色板条目的id(行内格式)。调色板从WALKS为每条路线取一个条目,图画也从同一个对象取路线颜色,所以图例与路线和色标一致。标记里的数字画成笔画,地图因此不带任何字体。
完整食谱
// ═══ Postext Cookbook · Nº 047 · Walking guide with thumb tabs ═══════════════════
// https://postext.dev/en/cookbook/walking-guide-thumb-tabs
// Code: MIT · Text: original (CC BY 4.0) · Map and tiles: drawn in code (CC BY 4.0)
// Fonts: Albert Sans, DM Serif Display, Asap Condensed (SIL OFL 1.1) · Needs postext ≥ 1.34.0
// A pocket guide to Lisbon. Each walk is a heading style that sets four colours, and the tab,
// the stop numbers, the box's tint and title and the blank page before the walk take them.
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'walking-guide-thumb-tabs';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: 'band' and three partners are the entries a walk overrides
const palette = {
ink: '#1b2430', // text: a blue-black
paper: '#fffdf8', // the page
band: '#1f4e9c', // the section colour: tile blue until a walk replaces it
bandInk: '#1d4a94', // the section colour for type on paper
onBand: '#ffffff', // type set on the band
wash: '#e8eef8', // the section's box tint
muted: '#5e6875', // running heads, notes, chip outlines
};
// Each walk's values for those four. Baixa yellow is too light for white type, so its
// onBand is the ink. The text is recoloured by value, so the four base values must differ.
const WALKS = {
alfama: { band: '#b9533a', bandInk: '#a1432b', onBand: '#ffffff', wash: '#f7e7e0' },
baixa: { band: '#e2b23d', bandInk: '#7d5a0f', onBand: '#1b2430', wash: '#fbf0d5' },
belem: { band: '#2b7a78', bandInk: '#236866', onBand: '#ffffff', wash: '#e1eeed' },
};
const NAMES = { alfama: 'Alfama', baixa: 'Baixa', belem: 'Belém' };
// A walk's palette follows the paletteId; the hex beside it is the colour's base value.
const col = (id) => ({ hex: palette[id] ?? WALKS[id].band, model: 'hex', paletteId: id });
const entry = (id, hex) => ({ id, name: id, value: { hex, model: 'hex' } });
const colorPalette = [
...Object.entries(palette).map(([id, hex]) => entry(id, hex)),
...Object.entries(WALKS).map(([id, w]) => entry(id, w.band)), // for :swatch{color="alfama"}
entry('main-color', palette.ink), // the engine's defaults link here: nothing prints blue
];
// #endregion
const TRIM = { width: 120, height: 200 }; // a pocket guide
// Mirrored, and tighter than a book's at top and foot: a pocket guide keeps 164 mm of text.
const MARGIN = { top: 18, bottom: 18, inner: 14, outer: 16 };
const MEASURE = TRIM.width - MARGIN.inner - MARGIN.outer; // 90 mm, about 55 characters
const LEAD = 13.2; // body leading in pt: the baseline grid
const BAND = 48; // mm: a walk opener's colour band, from the top edge
const HEAD = { y: 10, gap: 7 }; // running heads from the top edge; folio to title (mm)
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#不给空白左页涂色
去掉色块后,第6页保留色标:pages: 'all'也把它印在空白页上,是路线2的黄色,印着2。
-const header = { elements: [...heads, ...tabs, ...blankPage] };
+const header = { elements: [...heads, ...tabs] };#加上第三条路线
WALKS里已经有Belém的颜色,walkStyles里也有它的样式,所以这条路线只需在内容里加上标题。它从第9页开始,前面的第8页是涂成青绿色、印着白色3的空白页。
+# Belém {style="belem" route="From the monastery to the tower"}
+
+Tram 15 runs from Praça da Figueira to Belém along the river.常见问题
易错点
标题样式会继承其级别的分页设置
headingStyles中的条目没有写出的字段都取自它所属的标题级别,breakBefore也不例外。在:::pagebreak之后以H1设置样式的目录页或版权页会继承parity 'odd',结果落在一张空白页之后。给这类样式设置breakBefore: { enabled: false }。 标题样式 →
易错点
相对容器的负偏移什么也渲染不出来
自动宽度的设计文本被限制在容器内,所以相对容器的负偏移会把它推出去,结果什么也不渲染。把这类元素用明确的mm偏移锚定到页面或出血上,或者给它们一个固定宽度。 锚定设计元素 →
- 第6页之所以存在,是因为路线1在第5页,也就是一个右页上结束。在左页上结束的路线不会留下空白页,下一条路线的颜色就只出现在它自己的页面上。
- 在第一条编号路线之前,
{chapterNumber}为空,所以印出它的元素(比如色标)在那里需要一个不含它的样式。
致谢
- 文本
- 原创文字, CC BY 4.0
- 图片
- The tiles, the map of the routes and its pins, drawn in code · Ignacio Ferro · CC BY 4.0
- 字体
- Albert Sans (SIL OFL 1.1) · DM Serif Display (SIL OFL 1.1) · Asap Condensed (SIL OFL 1.1)


