简单来说
一本收录腓特烈二世与伏尔泰三封通信的小书。示例演示怎样排出每封信的日期、称呼和署名,信与信接着排,不另起一页。
成品一览
Mon sort est changé的五页,这是一个14 × 21 cm的法文三封书信版本:两封是腓特烈二世在1740年6月成为普鲁士国王六天和十二天后写给伏尔泰的信,另一封是伏尔泰1778年4月1日的信,写于他去世前两个月。封面上,烫金双线框内是用代码绘制的绿色摩洛哥皮面,上面放着一张由扩散模型生成的照片,拍的是两封折好的信和一枚红色火漆印。书内的信接排,不另起页。每个信头用红色小型大写字母印出编号,用Fell斜体印出两位通信人,下方右对齐排着地点和日期。称呼单独占一行;署名用小型大写字母,靠右侧页边距结束全信;腓特烈的附言排在署名之后,小一号。每个右页的书眉印出该信的日期。
这道食谱解答
- 怎样排题词、献词、署名,或带大号引号的引文框?
- 怎样让西班牙文、法文或德文获得良好的两端对齐和断词?
- 怎样设置书眉:左页放书名,右页放章名,页码在外侧?
简短回答
// Each letter is a level-1 heading that carries its place and date as attributes:
// # Frédéric à Voltaire {place="À Charlottembourg" date="6 juin 1740"}
// (a value holds no { or }, and one with " goes in single quotes: gotcha attr-values)
const letterHead = { enabled: true, slot: { elements: [
// {number} prints the count as numberingTemplate '{1:I}' writes it.
{ kind: 'text', id: 'number', content: 'Lettre {number}', ...sc, fontSize: pt(9),
letterSpacing: pt(1.8), color: col('seal'), placement: at('container', 'top-left') },
{ kind: 'text', id: 'title', content: '{titleText}', ...fell, fontSize: pt(15),
color: col('ink'),
placement: at('#number', 'below', 0, 1) },
// The dateline spans the measure under the title and sets its words flush right;
// inlineMarks reads the attribute's marks, so date="1^er^ avril 1778" raises its ordinal.
{ kind: 'text', id: 'dateline', content: '{attr.place}, le {attr.date}.', ...crimson,
italic: true, fontSize: pt(10.4), color: col('ink'), align: 'right', inlineMarks: true,
placement: { ...at('#title', 'below', 0, 1.5), size: { width: 'fill' } } },
] } };
// The salutation, the signature and the postscript, each a :::paragraphs{style="…"} container:
// :::paragraphs{style="signature"}
// Fédéric.
// :::
const letterParts = [
{ id: 'vedette', firstLineIndent: pt(0) }, // 'Sire,' on a line of its own, flush left
{ id: 'signature', ...sc, fontSize: pt(10.5), textAlign: 'right', marginTop: pt(LEAD / 2) },
{ id: 'postscript', fontSize: pt(9), lineHeight: pt(12.6), marginTop: pt(LEAD / 2) },
];
// Hooked up below: letterHead designs the level-1 heading, letterParts joins paragraphStyles.
用料
- 功能
- 标题属性锚定设计元素段落样式设计过的章首页编号标题从右页开始的章标题样式封面、扉页和版权页页面设计中的图片标题中的换行书眉与页码逐行排诗对称页边距纸张颜色断词与文档语言上标与下标文档元数据语义调色板在Canvas上绘制页面
- 类型
- Crimson Pro, IM Fell French Canon, IM Fell DW Pica SC(SIL OFL 1.1)
- 素材
letters-1160.jpg- The cover: two folded letters and a wax seal, a photograph (Generated With Diffusion Models, 原创)
- The cover’s green morocco and gilt fillet, drawn in code (Ignacio Ferro, CC BY 4.0)
做法
#1 · 从标题读取日期行
代码就是上面的简短回答。每封信是一个1级标题,属性里存着地点和日期,信头把它们印成{attr.place}, le {attr.date}.(标题属性)。日期行的框伸到栏的右边缘(width: 'fill'),align: 'right'让文字靠这条边排;不设的话,设计文字居中。称呼、署名和附言是用:::paragraphs{style="…"}应用的段落样式。署名使用IM Fell DW Pica SC,这个字体族的小写字母本身就刻成小型大写字母。inlineMarks: true让日期行读取属性里的标记,所以date="1^er^ avril 1778"会把序数词尾排成上标。
#2 · 让书信接排
const letters = { level: 1, numberingTemplate: '{1:I}', advancedDesign: letterHead,
// No break: left at the level's default, each letter would open on a recto.
breakBefore: { enabled: false }, marginTop: pt(2 * LEAD),
// The hidden heading line is measured in the heading face: italic keeps it the IM Fell cut
// that FONTS loads. Upright, it would need the roman, which FONTS
// leaves out; the layout would change only for a title long enough to wrap.
italic: true };
关闭breakBefore后,一封信从上一封信结束处那条网格线往下两条网格线(marginTop)开始。在第4页上,附言用12.6 pt行距排,结束在两条网格线之间,所以第三封信上方留出约三行空白。在第2页上,第一封信排到页面底部,第二封信从第3页开始。如果保留1级标题的默认值,每封信都会从右页开始(标题前分页)。numberingTemplate: '{1:I}'把书信编为I至III,信头通过{number}印出这个数字。
#3 · 让右页随信标注日期
const HEAD_Y = 12; // mm from the top edge
const head = (id, content, parity, placement, look = {}) => ({ kind: 'text', id, content,
parity, pages: 'body', ...sc, fontSize: pt(8.5), letterSpacing: pt(0.9), color: col('muted'),
placement, ...look });
const folio = { ...crimson, fontSize: pt(9), letterSpacing: pt(0), color: col('ink') };
const header = { elements: [
head('verso-folio', '{pageNumber}', 'even', at('page', 'top-left', MARGIN.outer, HEAD_Y), folio),
head('verso-names', '{author}', 'even', at('page', 'top-left', MARGIN.outer + 8, HEAD_Y)),
// {attr.date} reads the last letter that starts on or before the page.
head('recto-date', '{attr.date}', 'odd', at('page', 'top-right', -(MARGIN.outer + 8), HEAD_Y),
{ ...crimson, italic: true, fontSize: pt(9.5), letterSpacing: pt(0), inlineMarks: true }),
head('recto-folio', '{pageNumber}', 'odd', at('page', 'top-right', -MARGIN.outer, HEAD_Y),
folio),
] };
在页眉中,{attr.date}取在该页或之前开始的最后一个1级标题的属性。第3页印出12 juin 1740;第5页上,伏尔泰的信从第4页延续过来,印出序数词尾排成上标的1er avril 1778(文本元素)。书眉位于页面上边缘下方12 mm处,页码在外侧页边,文字再往里8 mm。pages: 'body'让书眉不出现在封面上,封面的标题是通栏的,所以它算作章首页。日期用它所重复的日期行的Crimson Pro斜体排,左页上的人名用书信编号的小型大写字母排。
#4 · 把封面做成标题
// The Markdown: # Mon sort \\ est changé {style="cover"}. The binding fills the page, so the
// design reserves all of it and the headnote starts on page 2.
const onCover = (y) => at('page', 'top', 0, y); // centred, y mm below the top edge
const LETTERS = { x: 12, y: 72, w: 116, h: 120 }; // mm: the photograph, inside the fillet
// numbered: false keeps the cover out of the count, so the first letter is I.
const cover = { id: 'cover', numbered: false,
// span: 'page' although the book has one column. Kept in the column, the design stops at
// the column's foot (a band of paper under the leather), and page 1 counts as a 'body' page
// and prints the running heads.
span: 'page', advancedDesign: { enabled: true, slot: { elements: [
{ kind: 'image', id: 'binding', resourceId: 'binding',
placement: { ...at('bleed', 'top-left'), size: { width: 'fill', height: 'fill' } } },
{ kind: 'image', id: 'letters', resourceId: 'letters', placement: { ...at('page', 'top-left',
LETTERS.x, LETTERS.y), size: { width: mm(LETTERS.w), height: mm(LETTERS.h) } } },
{ kind: 'text', id: 'names', content: '{author}', ...sc, fontSize: pt(9.5),
letterSpacing: pt(2), color: col('gilt'), placement: onCover(18) },
// \\ in the heading breaks the title here.
{ kind: 'text', id: 'title', content: '{titleText}', ...fell, fontSize: pt(50),
lineHeight: 1, color: col('paper'), align: 'center', overflow: 'wrap',
placement: onCover(24) },
{ kind: 'text', id: 'subtitle', content: '{subtitle}', ...crimson, italic: true,
fontSize: pt(12), color: col('paper'), placement: onCover(62) },
] } } };
封面是第一个标题,使用cover标题样式:一个锚定在出血上的图像元素承载代码绘制的封皮,第二个图像元素把照片放在往下72 mm处,标题、人名和副标题来自标题和前置元数据。numbered: false让它不参与计数,所以腓特烈的第一封信是I。封皮图像铺满整页,而章首页的设计会一直预留到最低的元素,所以封面占满第1页,编者按不需要:::pagebreak就从第2页开始。
#5 · 按法语断词,保持空格舒展
// Justification, hyphenation, whole-paragraph line breaking and the widow, orphan and runt
// rules are defaults; locale 'fr' (in the config) picks the French patterns.
// The letters keep the transcription's unspaced ; : ? and !; a narrow no-break space
// (U+202F) typed before each would give French spacing and never end a line.
const bodyText = { fontFamily: 'Crimson Pro', fontSize: pt(10), lineHeight: pt(LEAD),
color: col('ink'), firstLineIndent: mm(5),
// No :ref here: written out with the other colours, for the default-skin check.
referenceColor: col('ink'),
// A word space never shrinks below 75 % of the font's. At the default 60 %, the tightest
// line on page 5 sets its spaces at 0.70 (each VDT line carries its justifiedSpaceRatio).
minWordSpacing: 0.75 };
locale: 'fr'使用法语模式断词(第2页的ou-vrage,第4页的représen-tation)。两端对齐、整段断行以及段末孤行、段首孤行和孤字规则默认都已开启。minWordSpacing: 0.75让词间空格不会缩到Crimson Pro空格的四分之三以下;按默认的0.6,第5页最紧的两端对齐行会把空格排成该宽度的0.70。行距为14.4 pt时,每页容纳33行,下页边距就是这些行下面剩下的20.4 mm。
#6 · 把每种颜色都链接到调色板
const palette = {
ink: '#2a2320', // the text: a warm near-black
paper: '#f6efe2', // the page, and the lettering on the cover
seal: '#9c2b24', // the letter numbers
leather: '#2a4536', // the cover: a green morocco binding
gilt: '#d0b67c', // its tooled border and the names on it
muted: '#75695d', // the running heads
};
// col(id): a colour linked to its palette entry, in text styles and design elements 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 default colours, the italic of the headnote among them, link to 'main-color';
// here it is the ink, not the default blue.
{ id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];
col()把每种颜色链接到调色板里的条目,这个链接在文本样式和设计元素里同样有效:信头、书眉和封面都是如此。编者按和版权页信息的斜体使用默认斜体颜色,它链接到main-color;把这一项设为墨色后,斜体就印成墨色,而不是引擎的蓝色#295AA3。
完整食谱
// ═══ Postext Cookbook · Nº 064 · Letters edition: datelines and signatures ════════════
// https://postext.dev/en/cookbook/letters-edition
// Code: MIT · Text: Frederick II and Voltaire, 1740 and 1778 (PD) · Cover photo: diffusion models
// Fonts: Crimson Pro, IM Fell French Canon, IM Fell DW Pica SC (SIL OFL) · Needs postext ≥ 1.25.0
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'letters-edition';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: iron-gall ink on cream paper, wax red, green morocco and gilt
const palette = {
ink: '#2a2320', // the text: a warm near-black
paper: '#f6efe2', // the page, and the lettering on the cover
seal: '#9c2b24', // the letter numbers
leather: '#2a4536', // the cover: a green morocco binding
gilt: '#d0b67c', // its tooled border and the names on it
muted: '#75695d', // the running heads
};
// col(id): a colour linked to its palette entry, in text styles and design elements 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 default colours, the italic of the headnote among them, link to 'main-color';
// here it is the ink, not the default blue.
{ id: 'main-color', name: 'ink (defaults)', value: { hex: palette.ink, model: 'hex' } },
];
// #endregion
const TRIM = { width: 140, height: 210 }; // the French 14 × 21 format
const LEAD = 14.4; // pt: the body's leading, the grid every letter starts on
const LINES = 33; // lines of text on a full page
const TOP = 22; // mm: the top margin
const MARGIN = { top: TOP, inner: 17.5, outer: 14.5, // mm, mirrored
bottom: TRIM.height - TOP - (LINES * LEAD * 25.4) / 72 }; // ends the page on line 33
const sc = { fontFamily: 'IM Fell DW Pica SC' }; // its lower case is cut as small capitals
const fell = { fontFamily: 'IM Fell French Canon', italic: true }; // the display italic
const crimson = { fontFamily: 'Crimson Pro' }; // the text face
const at = (to, edge, x = 0, y = 0) => ({ anchor: { to, edge }, offset: { x: mm(x), y: mm(y) } });
// #region answer: a letter head read from the heading, and styles for the letter's parts
// Each letter is a level-1 heading that carries its place and date as attributes:
// # Frédéric à Voltaire {place="À Charlottembourg" date="6 juin 1740"}
// (a value holds no { or }, and one with " goes in single quotes: gotcha attr-values)
const letterHead = { enabled: true, slot: { elements: [
// {number} prints the count as numberingTemplate '{1:I}' writes it.
{ kind: 'text', id: 'number', content: 'Lettre {number}', ...sc, fontSize: pt(9),
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#在书眉里写出地点
同样的属性可以给右页印出完整的日期行:À Charlottembourg, le 12 juin 1740。
- head('recto-date', '{attr.date}', 'odd', at('page', 'top-right', -(MARGIN.outer + 8), HEAD_Y),
+ head('recto-date', '{attr.place}, le {attr.date}', 'odd',
+ at('page', 'top-right', -(MARGIN.outer + 8), HEAD_Y),常见问题
- 右页取的是在该页或之前开始的最后一封信的日期。一页上结束一封信又开始下一封时,印的是新信的日期,就像词典书眉标出该页最后一个词条。
- 缺少属性的标题会印出空字符串,没有警告:一封没有
place="…"的信,日期行会以逗号开头。检查每封信的标题。 - 这些信保留了转写本中不加空格的; : ?和!。法语惯例是在它们前面加一个窄不换行空格(U+202F);输入这个空格,标点就会和它前面的词留在同一行。
- Crimson Pro没有修饰字母ᵉ(U+1D49)和ʳ(U+02B3),所以用Unicode写的1ᵉʳ会用后备字体印出。改写成
1^er^:编者按是正文,把它排成上标;日期行和右页书眉的文本元素带有inlineMarks: true,也这样排。
致谢
- 文本
- Frederick II’s letters to Voltaire of 6 and 12 June 1740 and Voltaire’s letter to him of 1 April 1778, in Correspondance de Voltaire avec le roi de Prusse (Paris, Librairie de la Bibliothèque nationale, 1889), one misprint corrected (ouvragés), the salutations and signatures set apart · Frédéric II · Voltaire · 公有领域
- The headnote and the colophon · Ignacio Ferro · CC BY 4.0
- 图片
- The cover: two folded letters and a wax seal, a photograph · Generated With Diffusion Models · 原创
- The cover’s green morocco and gilt fillet, drawn in code · Ignacio Ferro · CC BY 4.0
- 字体
- Crimson Pro (SIL OFL 1.1) · IM Fell French Canon (SIL OFL 1.1) · IM Fell DW Pica SC (SIL OFL 1.1)


