简单来说
一部1878年西班牙小说的开头几页,排成袖珍本。示例演示怎样让词距均匀,并按西班牙语音节正确断词。
成品一览
把Galdós的《Marianela》(1878)开头排成11.5 × 18 cm的口袋本,第一章对面是一幅推想的Socartes矿区地图。在章的Libre Bodoni斜体标题上方,用小型大写字母拼写出capítulo primero;正文用Gentium Book Plus,从页面四分之一高度处开始,首字是一个抬高的牛血红色字母。单词按西班牙语音节断开(hie-rro、esta-blecimiento),词间距保持在正常宽度的1.7倍以内;没有哪一段以断词后的词尾结束。对白用raya排,这种长破折号也用来开合叙述者的incisos(插入语)。旅人的口令放在«comillas latinas»中,而在版本说明于引文内部再次引用它时,用英文双引号。地图是Figura 1,在正文中引作[fig. 1]。书眉用Marcellus SC,左页印作者,右页印书名。
这道食谱解答
- 怎样让西班牙文、法文或德文获得良好的两端对齐和断词?
- 怎样避免段末孤行、段首孤行和只有一个词的末行(孤字),并让标题与正文不分开?
- 怎样写对话破折号、段首的年份、价格和字面符号,而不被Markdown误读?
- 怎样让“图”和“表”的标签使用文档的语言?
- 怎样给章首页加作者行、提要或带首字下沉的导语?
简短回答
const LOCALE = 'es'; // config().locale: Spanish hyphenation, and Spanish caption labels
const bodyText = { // config().bodyText
fontFamily: TEXT, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
referenceBold: false, // '[fig. 1]' reads in roman, like the words around it
firstLineIndent: mm(4), indentAfterHeading: false, // a paragraph after a heading starts flush
// The defaults justify, hyphenate, break by Knuth–Plass and keep widows and orphans out.
// Long Spanish words on an 86 mm measure need two more settings. Spaces under 1.7×: at
// the default 2×, eight lines here open past 1.6×; at 1.7 it hyphenates hie-rro, ca-lles.
// A last line under 26 space widths is a runt; at 20, 'siem- / pre adelante.' got through.
maxWordSpacing: 1.7, runtMinCharacters: 26,
};
// config().resourceTypes: the built-in types, 'Figura' and 'Fig.' in Spanish, renumbered
// through the book (Figura 1) where the default counts by chapter (Figura 1.1).
const resourceTypes = defaultResourceTypes(LOCALE)
.map((type) => ({ ...type, numberingTemplate: '{n}', resetOn: 'never' }));
用料
- 功能
- 断词与文档语言最优断行(Knuth–Plass)正文段落的首字下沉设计过的章首页标题属性页面设计中的文字、线条和框段末孤行、段首孤行与孤字对称页边距从右页开始的章按页面角色显示书眉书眉与页码决定图位置的引用编号题注用你的语言显示“图”和“表”图就放在这里标题样式显式竖向间距段落样式转义与字面字符纸张颜色
- 类型
- Gentium Book Plus, Libre Bodoni, Marcellus SC(SIL OFL 1.1)
- 素材
- The conjectural map of the Socartes mines, drawn in code in the page’s palette (Ignacio Ferro, MIT)
做法
#1 · 西班牙语音节,收紧词间距
代码见上面的简短回答。locale: 'es'选用西班牙语的断词规则,'es-ES'或'es-MX'这样带地区的标签也一样;默认语言的美式规则则会断成cabal-los、hi-erro和establec-imiento(支持的语言)。两端对齐、Knuth–Plass以及段首孤行、段末孤行规则默认都已开启。86 mm的行长还需要收紧词间距:用默认的maxWordSpacing: 2时,有八行超过1.6倍,最宽的达到1.89倍;设为1.7时最宽为1.65倍,因为断行程序改为断开hie-rro和ca-lles(词间距上下限)。超过上限的行代价高得多,但仍然允许,所以在更窄、没有更好断点的行长上,仍可能超出。runtMinCharacters: 26能抓住默认的20个空格宽度放过的断词残片:siem- / pre adelante.变成Adelante, / siempre adelante.,So- / cartes [fig. 1].变成de / Socartes [fig. 1].(孤字)。
#2 · 由整行构成的口袋本页面
const TRIM = { width: 115, height: 180 }; // mm: 11.5 × 18 cm
const [TOP, INNER, OUTER] = [16, 15, 14]; // mm: a pocket page keeps its margins tight
const LINES = 30; // lines of LEAD per page, so every full page ends on the same line
const MEASURE = TRIM.width - INNER - OUTER; // 86 mm: about 57 characters of Gentium at 10 pt
const page = {
width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, backgroundColor: col('paper'),
margins: { top: mm(TOP), bottom: mm(TRIM.height - TOP - (LINES * LEAD * 25.4) / 72),
left: mm(INNER), right: mm(OUTER), mirror: true }, // left is inner on a recto
};
下页边距由成品尺寸、上页边距和LINES算出,所以版心恰好容纳30行13.8 pt,每个满页都止于同一条基线,第10页和第11页就是如此。设mirror: true后,在跨页的两页上,15 mm的内侧页边距都在书脊一侧,14 mm的外侧页边距都在切口一侧(对称页边距),86 mm的行长可容纳约57个10 pt的Gentium Book Plus字符。
#3 · 用文字写出的章序与抬高的首字
const [LABEL_Y, RULE_Y, TITLE_Y] = [4, 10.5, 13.5]; // mm below the top of the text block
const SINK = 8; // lines of LEAD above the text: the chapter drops a quarter of the page
const INITIAL = 3 * LEAD; // pt: the initial's size, three leads
const RISE = 2; // lines of LEAD the raised initial keeps clear above the first line
const centred = (y) => ({ anchor: { to: 'container', edge: 'top' }, offset: { y: mm(y) } });
const opener = {
enabled: true,
slot: { elements: [
// The ordinal is written in the heading, # Perdido {ordinal="primero"}: the plate before
// the chapter is a level-1 heading too, so the level carries no counter.
{ kind: 'text', id: 'chapter', content: 'capítulo {attr.ordinal}', ...smallCaps,
placement: centred(LABEL_Y) },
{ kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(0.6),
color: col('oxblood'), placement: { ...centred(RULE_Y), size: { width: mm(9) } } },
{ kind: 'text', id: 'title', content: '{titleText}', fontFamily: DISPLAY, italic: true,
fontSize: pt(26), lineHeight: 1.1, color: col('ink'), align: 'center',
overflow: 'wrap', // a longer title takes a second line
placement: centred(TITLE_Y) },
] },
// The text starts SINK lines down; the initial's rise takes the last RISE of them.
minHeight: pt((SINK - RISE) * LEAD),
};
// parity 'any': each chapter opens on the next page, as pocket books do.
// marginBottom replaces the level's default, a blank line of its own. The first paragraph
// of the chapter opens with a raised initial: lines: 1 stands it on the first baseline,
// three leads tall, and the paragraph keeps its rise clear above it.
const chapter = { level: 1, breakBefore: { enabled: true, parity: 'any' },
marginBottom: pt(0), advancedDesign: opener,
dropCap: { lines: 1, fontFamily: DISPLAY, fontWeight: 700, fontSize: pt(INITIAL),
color: col('oxblood') } };
capítulo primero来自标题自己的ordinal属性(标题属性)。用'{1:ordinal}'编号的级别会自己拼出这个词,但章前的插图页也是一级标题,所以这一级不带计数器。首字属于章这一级:它的dropCap给每个一级标题后的第一段开头,段落就按原样写在Markdown里(首字下沉)。lines: 1加上相当于三个行距的fontSize,让它成为抬高的首字,立在第一条基线上;第一行让出首字的宽度和默认间距,与段落其余各行一起两端对齐。字母高出第一行,段落会在上方以整行为单位为这段升高留出空间:这里是两行(RISE),所以minHeight让章首页在第SINK - RISE行结束,正文第一行仍落在第SINK行,即页面往下四分之一处。章这一级的marginBottom: 0取代默认的外边距(默认值会多出一个空行)。带首字下沉的段落不用首行缩进。

#4 · 地图作为Figura 1与章首相对
const MAP_H = 112; // mm: a frontispiece map, the measure wide
const resources = [{
id: 'plano', typeId: 'figure', kind: 'svg', createdAt: 0, updatedAt: 0,
svg: { fileId: 'plano.svg', width: MEASURE, height: MAP_H }, // the ratio: set column-wide
placement: { position: 'here' }, // where ::resource{id="plano"} stands, not a float
caption: 'El camino de Golfín: de Villafangosa, por la pasadera y el cerro, al talud de '
+ 'las minas de Socartes.',
note: 'Plano conjetural dibujado para esta edición a partir del capítulo primero.',
altText: 'Plano: abajo, la villa, el río y la pasadera; en medio, un cerro arbolado; arriba, '
+ 'las minas en gradas. Un punteado lleva de la villa al talud de las minas.',
}];
// The plate's heading, # Las minas de Socartes {style="lamina"}, opens a page the heads skip.
const frontispiece = {
id: 'lamina', // its own break, or it takes the chapter's (gotcha: style-inherits-break)
breakBefore: { enabled: true, parity: 'any' }, marginBottom: pt(0),
footer: { elements: [] }, // no drop folio
advancedDesign: { enabled: true, slot: { elements: [{ kind: 'text', id: 'name',
content: '{titleText}', ...smallCaps, // a line down: the plate centres on the page
placement: { anchor: { to: 'container', edge: 'top' }, offset: { y: pt(LEAD) } } }] } },
};
const captionStyle = { fontFamily: TEXT, fontSize: pt(8.3), gap: mm(2),
labelColor: col('oxblood'), descriptionItalic: true, note: { color: col('muted') } };
position: 'here'把地图放在::resource{id="plano"}所在的位置,位于一个单独的标题之下。这个标题让书中第8页成为首页,而书眉会跳过首页。它的样式清空页脚并自带分页,因此即使各章都从右页开始,这幅插图仍与第一章相对,不带书眉和页码(标题样式)。简短回答中的西班牙语资源类型在地图下印Figura 1.(不设resourceTypes时印Figura 1.1,按章编号),在正文中印Fig. 1,Markdown中:ref的case="lower"把它变成fig. 1。引用放在方括号里,西班牙语版本用方括号标出编者在作者原文之外添加的内容。
#5 · 左页印作者,右页印书名
const [HEAD_Y, DROP_Y] = [9, -10]; // mm: heads from the top edge, drop folio from the foot
const head = (id, content, parity, edge, x, y = HEAD_Y, pages = 'body') => ({
kind: 'text', id, content, parity, pages, // running heads skip the openers
fontFamily: LABEL, fontSize: pt(8), letterSpacing: pt(1.2), color: col('muted'),
placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(y) } },
});
const SHIFT = (INNER - OUTER) / 2; // mm: the text block's centre is off the page's centre
// Folios in the text face (Marcellus SC's 1 and 0 read as I and O), on the heads' baseline.
const folio = { fontFamily: TEXT, letterSpacing: pt(0), color: col('ink') };
const header = { elements: [
{ ...head('verso-folio', '{pageNumber}', 'even', 'top-left', OUTER), ...folio },
head('verso-author', '{author}', 'even', 'top', -SHIFT),
head('recto-title', '{title}', 'odd', 'top', SHIFT),
{ ...head('recto-folio', '{pageNumber}', 'odd', 'top-right', -OUTER), ...folio },
] };
const drop = (parity, x) => ({ ...head(`drop-${parity}`, '{pageNumber}', parity, 'bottom', x,
DROP_Y, 'opener'), ...folio }); // a chapter can open on either side of the spread
const footer = { elements: [drop('odd', SHIFT), drop('even', -SHIFT)] };
parity: 'even'把作者放在左页,'odd'把书名放在右页,pages: 'body'让两者都不出现在首页上,首页唯一的页面元素是用pages: 'opener'设置的页脚页码。每条书眉都锚定在页面上,并用SHIFT移到版心的中线上;由于内外页边距不同,版心中线与页面中线相差半毫米。出于同样的原因,页脚页码分奇偶两种。页码用Gentium排,因为Marcellus SC把1和0画得像I和O。
#6 · Markdown中的西班牙语标点
const paragraphStyles = [
{ id: 'asterismo', fontFamily: DISPLAY, fontSize: pt(11), color: col('oxblood'),
textAlign: 'center' },
// In ink: a muted note would need italicColor too, for its italic Marianela.
{ id: 'nota', fontSize: pt(8.3), lineHeight: pt(LEAD * 0.8), firstLineIndent: pt(0),
marginTop: pt(LEAD) },
{ id: 'colofon', fontSize: pt(7.5), color: col('muted'), textAlign: 'center',
firstLineIndent: pt(0), marginTop: pt(LEAD / 2) },
];
最后一页第12页以三个段落样式收尾。牛血红色Bodoni的星号居中排在一个:::space{lines=2}之后,因为空行不会增加空白。版本说明是8.3 pt、11 pt行距,版权页信息是7.5 pt,用书眉的灰色。版本说明保持墨色;如果改成灰色,样式还要设italicColor,否则斜体的Marianela会比周围的字印得更深。在Markdown中,对白以raya(—)开头,绝不能用连字符,因为行首的连字符加空格会开启一个列表。说明开头的*1878.*会开启一个有序列表,所以在年份前加一个词连接符(U+2060)。星号要转义(\*);不转义的话,第一个会开启无序列表,另两个会开启一段斜体,这一行就消失了。
完整食谱
// ═══ Postext Cookbook · Nº 016 · Justified Spanish in a pocket novel ═════════════════
// https://postext.dev/en/cookbook/spanish-pocket-novel
// Code: MIT · Text: B. Pérez Galdós, Marianela, 1878 (PD, Gutenberg #17340) · Map: drawn in code
// Fonts: Gentium Book Plus, Libre Bodoni, Marcellus SC (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
buildDocumentWithFonts, renderPageToCanvas, defaultResourceTypes, registerResourceImage,
inlineSvgFonts,
} from 'https://esm.sh/postext';
const LANG = 'es'; // @lang: the language of the sample document (this recipe is Spanish only)
const RECIPE = 'spanish-pocket-novel';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = {
ink: '#1e1b18', // text: a warm near-black
oxblood: '#8b2e2a', // the one accent: chapter label, rule, initial, asterisks, route
muted: '#6d645a', // running heads, the map's paths, the caption's credit
paper: '#f7f2e8', // an ivory book paper
slag: '#c8553d', moss: '#6f7a4f', water: '#58707f', // the map: mined earth, woods, river
};
// col(id): a colour linked to its palette entry, in text styles and design slots 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' } })),
{ id: 'main-color', name: 'oxblood (defaults)', value: { hex: palette.oxblood, model: 'hex' } },
];
const TEXT = 'Gentium Book Plus'; // the text face
const DISPLAY = 'Libre Bodoni'; // the chapter's title and the initial
const LABEL = 'Marcellus SC'; // small capitals: chapter label, running heads, map names
const [BODY, LEAD] = [10, 13.8]; // pt: the body size, and its leading: the grid's pitch
const smallCaps = { fontFamily: LABEL, fontSize: pt(9), letterSpacing: pt(1.6), // 2 labels
color: col('oxblood'), align: 'center' };
// #region page: a pocket paperback; the text block holds 30 whole lines
const TRIM = { width: 115, height: 180 }; // mm: 11.5 × 18 cm
const [TOP, INNER, OUTER] = [16, 15, 14]; // mm: a pocket page keeps its margins tight
const LINES = 30; // lines of LEAD per page, so every full page ends on the same line
const MEASURE = TRIM.width - INNER - OUTER; // 86 mm: about 57 characters of Gentium at 10 pt
const page = {
width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, backgroundColor: col('paper'),
margins: { top: mm(TOP), bottom: mm(TRIM.height - TOP - (LINES * LEAD * 25.4) / 72),
left: mm(INNER), right: mm(OUTER), mirror: true }, // left is inner on a recto
};
// #endregion
// #region answer: Spanish syllables, word spaces under 1.7×, captions that say Figura
const LOCALE = 'es'; // config().locale: Spanish hyphenation, and Spanish caption labels
const bodyText = { // config().bodyText
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#每章从右页开始
设为'odd'时,本该从左页开始的章前面会多出一个空白页,就像版面更宽裕的版本那样。第一章本来就从右页开始,插图也保持在原页,因为它的样式自带分页。
-const chapter = { level: 1, breakBefore: { enabled: true, parity: 'any' },
+const chapter = { level: 1, breakBefore: { enabled: true, parity: 'odd' },#按章给图编号
直接使用内置的西班牙语资源类型时,图在每个一级标题内编号:地图变成Figura 1.1,引用变成fig. 1.1。
-const resourceTypes = defaultResourceTypes(LOCALE)
- .map((type) => ({ ...type, numberingTemplate: '{n}', resetOn: 'never' }));
+const resourceTypes = defaultResourceTypes(LOCALE);常见问题
易错点
标题样式会继承其级别的分页设置
headingStyles中的条目没有写出的字段都取自它所属的标题级别,breakBefore也不例外。在:::pagebreak之后以H1设置样式的目录页或版权页会继承parity 'odd',结果落在一张空白页之后。给这类样式设置breakBefore: { enabled: false }。 标题样式 →
- 章首页正文起点下移九行而不是八行时,第11页短了一行,因为
avoidWidows不允许在页面最后一行开始一个段落。修改设置或文字之后,要检查每页的页脚。 RISE是抬高的首字在正文上方留出的行数。换字体或字号会改变这段升高:较小的首字可能只需一行,这时除非minHeight随之调整,否则正文会上移一行。- 这几款字体的Fontsource latin文件中都没有星号三角⁂(U+2042),浏览器会从系统字体中取用。无论如何,西班牙语图书用三个隔开的星号标出段落分隔。
致谢
- 文本
- Marianela (1878), the opening of chapter I, “Perdido”, with today’s spelling and punctuation · Benito Pérez Galdós · 公有领域
- The edition’s note and the map’s caption · Postext Cookbook · 原创
- 图片
- The conjectural map of the Socartes mines, drawn in code in the page’s palette · Ignacio Ferro · MIT
- 字体
- Gentium Book Plus (SIL OFL 1.1) · Libre Bodoni (SIL OFL 1.1) · Marcellus SC (SIL OFL 1.1)


