简单来说
一部古典汉语字典的四页。每页外侧上角印出本页第一个和最后一个字头,方便翻页查字。
成品一览
一部《康熙字典》(1716)读者版中间的四页,按拼音从天 tiān排到庭 tíng。每个字头用红色的特粗宋体,上方标拼音;韵书读音(反切)和各义项接在同一段里,义项以同样的红色①②编号。开本为大32开,140 × 203 mm,两栏,每栏17个小五(9 pt)字,网格31行,每栏都排满到最后一行。每页外侧上角的书眉标出本页第一个和最后一个字头,所以查蜩的读者会翻到书眉为腆—髫的那一页。可以拿拉丁字母字典The Sailor’s Word-Book来对照。
这道食谱解答
- 怎样在词典的书眉里印出每页的首尾词目?
- 怎样在汉字上方加拼音?
- 怎样让汉字在PDF里不显示成空框?
简短回答
// Each headword is a level-2 heading, '## {天|tiān}'. {firstMark.h2} prints the text of the
// first one that starts on the page, without its reading, and {lastMark.h2} the last; a
// page on which none starts repeats the one in force. They stand at the outer corner, the
// folio outside them, over a hairline as wide as the type area.
const HEAD_Y = 11; // mm from the top edge to the top of the guide words
const [GUIDE, FOLIO] = [10.5, 8]; // pt
const head = (id, content, parity, x, size, extra) => ({ kind: 'text', id, content, parity,
fontFamily: HEI, fontWeight: 700, fontSize: pt(size), lineHeight: 1, color: col('ink'),
// A design line puts its baseline 0.8 of its height down: the folio drops to share it.
placement: { anchor: { to: 'page', edge: parity === 'even' ? 'top-left' : 'top-right' },
offset: { x: mm(parity === 'even' ? x : -x), y: mm(HEAD_Y + 0.8 * (GUIDE - size) * PT) } },
...extra });
const folio = { color: col('muted') };
const guideWords = [
head('verso-folio', '{pageNumber}', 'even', SIDE, FOLIO, folio),
head('verso-guide', '{firstMark.h2}—{lastMark.h2}', 'even', SIDE + 8, GUIDE),
head('recto-guide', '{firstMark.h2}—{lastMark.h2}', 'odd', SIDE + 8, GUIDE),
head('recto-folio', '{pageNumber}', 'odd', SIDE, FOLIO, folio),
{ kind: 'rule', id: 'hairline', direction: 'horizontal', thickness: pt(0.5), color: col('ink'),
placement: { anchor: { to: 'page', edge: 'top-left' },
offset: { x: mm(SIDE), y: mm(HEAD_Y + 5.5) }, size: { width: mm(AREA.width) } } },
];
用料
做法
#1 · 从字头取书眉检字
代码见上文的简短回答。每个字头都是一个二级标题,{firstMark.h2}和{lastMark.h2}印出本页开始的第一个和最后一个二级标题的文字(文本元素)。标记取的是标题中的汉字:## {天|tiān}标记的是天,读音仍在字头上方。从一页跨到下一页的词条,只在字头所在的那一页计入:第635页以殄条的结尾开始,书眉却是腆—髫。一级标题留给字母:这四页中没有字母开篇,不过即使有,字母标题也不会算作检字字头。
#2 · 字头上方的拼音
// A heading with no design of its own keeps its reading: '## {天|tiān}' sets tiān over 天.
// Two grid lines hold the headword and a 7.5 pt reading, so every entry stays on the grid.
// A Latin reading stands clear of its base by its descenders: the g of tīng stays off 汀.
const cjk = {
grid: { enabled: true, charsPerLine: CHARS, linesPerPage: LINES },
// The regular Song, not the heading's Black: a reading takes the face of its base.
ruby: { fontFamily: SONG, fontSize: em(0.5), color: col('cinnabar') },
};
const headword = { level: 2, fontSize: pt(15), lineHeight: pt(2 * LEAD), marginTop: pt(0),
marginBottom: pt(0) };
{天|tiān}是:ruby[天]{rt="tiān"}的简写,一个字上方标一个读音(中文标记、注音与割注)。没有自己版面设计的标题会排出注音;如果标题用了版面设计,就只印出一个天。注音默认使用标题本身的字体,即特粗宋体,除非cjk.ruby.fontFamily另外指定,所以这里指定了常规宋体。15 pt的一半是7.5 pt,标题30 pt的行高在字头上方为它留出了空间,因此标题占网格两行,下方的正文在两栏中都仍落在15 pt的行上。拼音注音会抬高其字体降部的深度再加0.04 em,所以tīng的g在汀的字身框上方0.6 pt处结束。
#3 · 一个词条一段
// As the Kangxi Dictionary sets an entry: the rhyme-book readings (反切), then the senses
// run in, each after its number. '**②**' marks a number, and the text prints its bold in
// cinnabar: the only bold on these pages. A circled number is a Chinese character to the
// composer: no line ends on one, away from the sense it opens, and no space follows it.
const bodyText = { fontFamily: SONG, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
boldColor: col('cinnabar'), italicColor: col('ink'), referenceColor: col('ink'),
textAlign: 'justify', firstLineIndent: pt(0) }; // no indent: the headword opens the entry
《康熙字典》的词条是连排的:先是韵书的读音,然后每个义项以“又”引出。这里把“又”换成了圈码,写作**②**,由boldColor用字头的朱红色印出;正文中没有别的粗体。《康熙字典》另有读音的义项,保留它自己的读音,如跳②的徒刀切。排版器把①到⑳按汉字排:行尾不会出现编号而它的义项到下一行才开始,编号与其后的文字之间也不加中西文间距,所以编号照原样输入即可。各栏齐底已关闭(headings.balancing.enabled: false),所以没有哪一栏会在字头上方加空,每个词条与前一条的间距都相同。
#4 · 字母T的书边索引
const INITIALS = 'ABCDEFGHJKLMNOPQRSTWXYZ'; // no pinyin syllable starts with I, U or V
const STEP = AREA.height / INITIALS.length; // mm: 7.13, the 23 tabs fill the type area's height
const tab = (parity) => {
const edge = parity === 'odd' ? 'top-right' : 'top-left'; // the fore-edge
const at = (x, width) => ({ anchor: { to: 'page', edge },
offset: { x: mm(parity === 'odd' ? x : -x), y: mm(TOP + INITIALS.indexOf('T') * STEP + 0.3) },
size: { width: mm(width), height: mm(STEP - 0.6) } });
// 6 mm inside the trim and 3 mm past it: the page cuts the tab square at the fore-edge
// (a print file with cutLines prints the 3 mm in its bleed); the letter on the 6 mm.
return [
{ kind: 'box', id: `tab-${parity}`, parity, placement: at(3, 9),
style: { backgroundColor: col('cinnabar'), borderRadius: mm(1.2) } },
{ kind: 'text', id: `tab-letter-${parity}`, parity, content: 'T', fontFamily: HEI,
fontWeight: 700, fontSize: pt(9), lineHeight: 1, color: col('paper'), align: 'center',
verticalAlign: 'middle', placement: at(0, 6) },
];
};
拼音音节以26个字母中的23个开头(没有以I、U、V开头的),所以版心高度被分成23档,每档7.13 mm,T占第19档。书边索引宽9 mm,从裁切线外3 mm处开始,因此裁切后在切口处是方角,只有靠内的两个角显出圆角;如果印刷文件设了page.cutLines,这3 mm就落在出血里。字母在页面内的6 mm中居中。四页都在T部,所以书边索引高度不变;the Sailor’s Word-Book则让它随字母沿书边逐级下移。
#5 · 每种字体加载自己的字符
// Fontsource cuts a Chinese face into about a hundred files (gotcha: cjk-fonts-slices).
// The regular Song sets the whole sample; each other voice gets only its own text.
const all = (re) => [...markdown.matchAll(re)].map((m) => m[1]).join('');
const heads = all(/^## \{(.+?)\|/gm);
await loadFonts(FONTS, markdown);
await loadCjkFonts({ [SONG]: ['400'] }, markdown);
await loadCjkFonts({ [SONG]: ['700'] }, all(/\*\*(.+?)\*\*/g));
await loadCjkFonts({ [SONG]: ['900'] }, heads);
await loadCjkFonts({ [HEI]: ['700'] }, `${heads}—0123456789T`);
await loadCjkFonts({ [KAI]: ['400'] }, all(/style="colophon-zh"\}\n(.+)\n/g));
Fontsource把每种中文字体按字符范围拆成约一百个文件,loadCjkFonts只加载包含所给文本的那些文件。常规宋体拿到整个样本,包括版权说明中的英文。特粗宋体拿到三十个字头,宋体粗体拿到义项编号,黑体拿到字头和书眉中的数字,楷体拿到版权说明中那句中文。cjkPdfProvider把同样的文件嵌入PDF,每个文件都裁剪到页面上实际用到的字符。
完整食谱
// ═══ Postext Cookbook · Nº 086 · A Chinese dictionary page with guide words ═════════
// https://postext.dev/en/cookbook/chinese-dictionary-page
// Code: MIT · Text: 康熙字典 (1716), Wikisource transcription, CC BY-SA 4.0 · Pinyin: CC BY 4.0
// Fonts: Noto Serif TC, Noto Sans TC, LXGW WenKai TC (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import { buildDocument, withLoadedFonts, renderPageToCanvas } from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'chinese-dictionary-page';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: black and one cinnabar, the second ink of a two-colour dictionary
const palette = {
ink: '#1f1b17', // text, guide words
cinnabar: '#a52f28', // headwords, their readings, sense numbers, the thumb tab
rule: '#bab2a4', // the column rule
muted: '#5c554d', // folios, colophon
paper: '#fbf8f2', // the page, and the letter reversed out of the tab
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
// The engine's defaults link to 'main-color': point it at the cinnabar.
const colorPalette = Object.entries({ ...palette, 'main-color': palette.cinnabar })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const [SONG, HEI, KAI] = ['Noto Serif TC', 'Noto Sans TC', 'LXGW WenKai TC'];
const PT = 25.4 / 72; // mm in a point
const [BODY, LEAD] = [9, 15]; // pt: 小五 on a 15 pt line, the grid both columns share
const [CHARS, LINES] = [17, 31]; // characters to a column's line, lines to a column
const TRIM = { width: 140, height: 203 }; // mm: 大32开
const MIN = { top: 20, bottom: 16, side: 12 }; // mm: minimums the grid grows to centre the area
// The type area the grid sets (two columns and a 2-em gutter) and the margins it leaves.
const AREA = { width: (2 * CHARS + 2) * BODY * PT, height: LINES * LEAD * PT }; // 114.3 × 164
const SIDE = (TRIM.width - AREA.width) / 2; // 12.85 mm
const TOP = MIN.top + (TRIM.height - MIN.top - MIN.bottom - AREA.height) / 2; // 21.5 mm
// #region answer: the first and the last headword of each page in its running head
// Each headword is a level-2 heading, '## {天|tiān}'. {firstMark.h2} prints the text of the
// first one that starts on the page, without its reading, and {lastMark.h2} the last; a
// page on which none starts repeats the one in force. They stand at the outer corner, the
// folio outside them, over a hairline as wide as the type area.
const HEAD_Y = 11; // mm from the top edge to the top of the guide words
const [GUIDE, FOLIO] = [10.5, 8]; // pt
const head = (id, content, parity, x, size, extra) => ({ kind: 'text', id, content, parity,
fontFamily: HEI, fontWeight: 700, fontSize: pt(size), lineHeight: 1, color: col('ink'),
// A design line puts its baseline 0.8 of its height down: the folio drops to share it.
placement: { anchor: { to: 'page', edge: parity === 'even' ? 'top-left' : 'top-right' },
offset: { x: mm(parity === 'even' ? x : -x), y: mm(HEAD_Y + 0.8 * (GUIDE - size) * PT) } },
...extra });
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#左页印第一个字头,右页印最后一个
有些字典每页只印一个检字字头,左页上方印第一个,右页上方印最后一个,于是一个跨页读作天……髫。
- head('verso-guide', '{firstMark.h2}—{lastMark.h2}', 'even', SIDE + 8, GUIDE),
- head('recto-guide', '{firstMark.h2}—{lastMark.h2}', 'odd', SIDE + 8, GUIDE),
+ head('verso-guide', '{firstMark.h2}', 'even', SIDE + 8, GUIDE),
+ head('recto-guide', '{lastMark.h2}', 'odd', SIDE + 8, GUIDE),#用ɑ和ɡ的拼音
中文字典的拼音常用单层a和g的字体。给注音指定Andika,并把它加入FONTS('Andika': ['400'],附上它的OFL出处)。
- ruby: { fontFamily: SONG, fontSize: em(0.5), color: col('cinnabar') },
+ ruby: { fontFamily: 'Andika', fontSize: em(0.5), color: col('cinnabar') },#字头与正文同字号
小型字典把字头用粗体排在段首。把词条放进:::paragraphs{style="entry"},每条以**{天|tiān}**开头,并印出{firstMark.entry}:以粗体开头的段落会用这段粗体标记所在的页面。
常见问题
易错点
注音印在正文中,不印在设计元素、题注或单元格中
注音会绘制在段落、标题、列表项、引文和框中。设计文本元素(章首页、书眉、徽标)、题注、脚注和表格单元格只印出基字,不带注音。需要拼音的标题应当是一个没有自身设计的标题:把它周围的内容(色带、课次编号)画在它前面那个标题的设计里,该设计中reserve: false的元素会画在span: 'page'章首页的文字下层。 注音:拼音与注音符号 →
易错点
中文字体通过cjk块分片加载
Fontsource把一个中文、日文或韩文字体家族按每个字重约一百个文件提供,每个文件覆盖一段字符范围。loadFonts只获取latin文件,所以屏幕上的汉字来自系统字体,测量不准;fontsourceProvider交给PDF的也是这个latin文件,汉字印出来是空框。列出kit中的cjk块,在loadFonts之后调用loadCjkFonts(FONTS, markdown)(书中用到几种CJK字体时,每种字体调用一次,并传入它所排的文字),并给renderToPdf传fontProvider: cjkPdfProvider:两者都会取用包含文中字符的那些文件。 中日韩字体 →
易错点
用zh-Hans或zh-Hant标记文档,不要用LANG
食谱的版本是en和es,但中文示例在两个版本里都是中文:`locale: LANG`会把它标记为英文或西班牙文,给其中的拉丁文词断词,把图标注为Figure或Figura,并给PDF标上错误的语言。postext 1.30起,这个标记也是文字成形所用的语言(测量、绘制和嵌入都按它进行),所以标记成英文或西班牙文时,中文字体的数字和与拉丁文共用的标点会换成另一套字形,断行的位置也随之改变。自己写出标记:'zh-Hans'(大陆规范:GB断行规则、开明式标点)或'zh-Hant'(台湾:全角居中标点);香港用'zh-HK'。单写'zh'按简体、大陆规范处理。日文示例写'ja'(见gotcha ja-locale-tag)。 中文断行 →
- 各栏齐底开启时,如果某一栏放不下下一个字头及其下方两行正文(
bodyText.widowMinLines),就会提前结束,剩下的行分到字头上方,每处最多一行:于是有的词条与前一条隔开一行,有的不隔。关闭后,这样的栏会短一截。
致谢
- 文本
- Thirty entries of the Kangxi Dictionary (康熙字典, 1716), 天 to 庭, each cut to a few senses: what is kept stands in the source’s order and under its attribution, a sense with a reading of its own keeps it, and of the ancient forms (古文) only 銕 is set, the others being outside the fonts. Variant forms are written in today’s standard Traditional characters and the punctuation is revised in places. From the zh.wikisource transcription, which gives the Siku Quanshu Huiyao copy (四庫全書薈要本) as its source and notes some of Wang Yinzhi’s corrections of 1831 (字典考證); 29 pages by radical and stroke count, revisions read on 29 September 2026: 大部 2535754, 水部/八畫 2542298, 田部 2543683, 心部/六畫 2537571, 土部/十畫 2535684, 門部/十畫 2553837, 心部/四畫 2537569, 歹部/五畫 2541608, 肉部/八畫 2549900, 人部/六畫 416510, 手部/六畫 2538445, 辵部/五畫 2553283, 木部/七畫 2540094, 虫部/八畫 2551174, 言部/八畫 2551690, 髟部/五畫 2556393, 穴部/六畫 2546633, 目部/六畫 2544863, 足部/六畫 2552906, 米部/十九畫 2548884, 巾部/五畫 2536199, 貝部/五畫 2552278, 金部/十三畫 2553630, 食部/九畫 2556072, 水部/二畫 2541898, 耳部/十六畫 2549782, 广部/二十二畫 2536291, 亠部/七畫 409671, 广部/七畫 2536277 · Zhang Yushu, Chen Tingjing and others (1716); transcription by Wikisource editors · CC BY-SA 4.0
- The pinyin readings, the sense numbers, the order of the entries and the colophon · Postext Cookbook · CC BY 4.0
- 字体
- Noto Serif TC (SIL OFL 1.1) · Noto Sans TC (SIL OFL 1.1) · LXGW WenKai TC (SIL OFL 1.1)


