跳到主要内容
食谱编号74

排版食谱 · 第2章 · 字体与文字

28 × 28字格上的中文小说页

大32开页面上的鲁迅《故乡》:每行28字,每页28行,GB断行规则,开明式标点,汉字与拉丁字母之间加空。

本页内容
输出
Canvas · PDF
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.65.0 · postext-pdf ≥ 1.65.0
许可证
更新于2026年10月31日
代码MIT · 文本MIT

页码70–71 · 第1–2页,共7页

  • 英文样例:尚无中文版本
  • 成品尺寸140 × 203 mm
  • 1栏
  • Noto Serif SC 10.5/16.5
  • Ma Shan Zheng
  • Noto Sans SC
  • 7页
  • 难度
  • Postext 1.65.0
  • 排版用时99 ms
  • 179行代码

简单来说

鲁迅《故乡》的开头,按大陆平装书的样子排版。示例演示怎样做到每行正好28字、每页28行,并遵守中文标点规则。

成品一览

鲁迅《故乡》(1921)的开头,按大陆出版的他第一部小说集平装本的样子来排:大32开,140 × 203 mm,版心以字计,每行28字,每页28行,用Noto Serif SC五号(10.5 pt)。每段首行缩进两字;逗号和引号占半个字宽,与大多数大陆图书相同;句号。和逗号,不出现在行首,前引号“不出现在行末。小说在右页开篇,标题用宋体的特粗字重。对页是一幅绘画插页,画的是叙述者记忆中月下的瓜地,并用Ma Shan Zheng毛笔字体配上描写这一景象的文字。口袋本小说里的西班牙文两端对齐为拉丁文字做了同样的事。

这道食谱解答

  • 怎样按字数设定版心,每行多少字、每页多少行?
  • 怎样避免“。”和“,”出现在行首?
  • 怎样像大陆图书那样用开明式排中文标点,或者排成半宽?
  • 怎样不手动输入空格,就在中文和西文之间留出间隔?

简短回答

script.js · 第37–53行在完整代码中
const cjk = {
  // The type area in characters: 28 ems wide, 28 lines of LEAD tall. page.margins become
  // minimums, and the grid centres the type area in the room they leave.
  grid: { enabled: true, charsPerLine: CHARS, linesPerPage: LINES },
  // What 'zh-Hans' gives by itself, written out so a reader can compare regions and styles.
  region: 'mainland',
  lineBreak: 'gb', // no 。,、”》 opens a line, no “《( ends one, and no / at either end
  punctuationWidth: 'kaiming', // ,、:; quotes, brackets ½ em; 。?! one em, ½ at a line end
  compressAdjacent: true, // idle under Kaiming (no pair tops 1.5 em); 'fullwidth' needs it
  latinSpacing: em(0.25), // 1921年, 用Noto Serif SC五号: a quarter em, none of it typed
};
const bodyText = {
  fontFamily: SONG, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  textAlign: 'justify', // spread between the characters, never between words
  firstLineIndent: em(2), indentAfterHeading: true, // two characters, every paragraph
};

用料

类型
Noto Serif SC, Noto Sans SC, Ma Shan Zheng(SIL OFL 1.1)
素材
  • plate-1120.jpg
  • The plate: the moonlit melon field of the story, a painting (Generated With Diffusion Models, 原创)

做法

#1 · 以字计的页面

代码见上文的简短回答。cjk.grid把行长设为bodyText.fontSize的28个全角(294 pt,103.7 mm),把版心设为28行lineHeight(462 pt,163 mm),再调整页边距来容纳它们,所以一整行汉字正好28个,每一整页都结束在同一行(字符网格)。网格下面的五项设置是locale: 'zh-Hans'本来就会给出的值;把它们写出来,是为了让你可以逐项修改,不过在punctuationWidth设为'fullwidth'之前,compressAdjacent不起作用:开明式下,任何两个相邻标点加起来都不超过1.5个全角。在第71页上,某段末尾的。放不下,而避头尾规则(lineBreak除'none'以外的每一级都有;'gb'还加上斜线)不允许它出现在行首。于是这一行把事后面的。排成半个字宽,把它挤进来(push-in),事。宏紧排,段落也不必多出一行(标点宽度)。不满的行把空隙分到字与字之间,而不是词与词之间,在这几页上每处不超过0.05 em(中日韩文字)。

第71页:一段的最后几行。事。宏紧排,使看。留在本行,而不必另起一行。

#2 · 天头更深的大32开页面

script.js · 第57–62行在完整代码中
const page = {
  width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, backgroundColor: col('paper'),
  // 22 + 18 mm leave 163 mm, exactly 28 lines of 16.5 pt; 18 mm a side leave the 103.7 mm
  // measure 0.3 mm to share. A grid that no longer fits is cut down (cjkGridClamped).
  margins: { top: mm(22), bottom: mm(18), left: mm(18), right: mm(18), mirror: true },
};

打开网格后,页边距是最小值:网格在它们留下的空间里放置版心,再把多余的部分分配出去。22 mm和18 mm正好留出163 mm,所以天头比地脚深4 mm,这是中文图书的习惯;两侧各18 mm,留下0.3 mm可分。如果要求的字数或行数超出能容纳的范围,网格会缩减到能放下为止,并给出cjkGridClamped警告;字号和行距保持不变。

#3 · 下沉整数行的标题

script.js · 第66–84行在完整代码中
const SINK = 8; // lines of LEAD: a whole number, so the text under it keeps to the grid
const centred = (y) => ({ anchor: { to: 'container', edge: 'top' }, offset: { y: mm(y) } });
const opener = {
  enabled: true,
  minHeight: pt(SINK * LEAD),
  slot: { elements: [
    // The spacing after the last character is advance, not ink: the title stays centred.
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: SONG, fontWeight: 900,
      fontSize: pt(46), letterSpacing: pt(23), lineHeight: 1.1, color: col('ink'),
      align: 'center', placement: centred(6) },
    { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1.2),
      color: col('night'), placement: { ...centred(30), size: { width: mm(8) } } },
    { kind: 'text', id: 'author', content: '{author}', fontFamily: HEI, fontSize: pt(10.5),
      letterSpacing: pt(5), color: col('ink'), align: 'center', placement: centred(34) },
  ] },
};
// parity 'any': a story opens on the next page, left or right.
const chapter = { level: 1, breakBefore: { enabled: true, parity: 'any' },
  marginTop: pt(0), marginBottom: pt(0), advancedDesign: opener };

minHeight是八行16.5 pt,所以标题下方的正文从网格第九行开始,基线与其他各页相同。23 pt的字距在故和乡之间隔出半个字,这是两字标题常用的间隔;Postext在居中排行时不计最后一个字后面的字距,所以标题仍在页面的中轴上(标题样式)。

#4 · 左页印书名,右页印篇名

script.js · 第88–107行在完整代码中
const HEAD_Y = 11; // mm from the top edge to the heads; the hairline 5 mm lower
const label = { fontFamily: HEI, fontSize: pt(8), color: col('muted'), pages: 'body' };
const at = (edge, x, y = HEAD_Y) => ({ anchor: { to: 'page', edge },
  offset: { x: mm(x), y: mm(y) } });
const header = { elements: [
  { kind: 'text', id: 'verso-folio', content: '{pageNumber}', parity: 'even', ...label,
    placement: at('top-left', SIDE) },
  { kind: 'text', id: 'verso-book', content: '{title}', parity: 'even', ...label,
    letterSpacing: pt(4), placement: at('top', 0) },
  { kind: 'text', id: 'recto-story', content: '{chapterTitle}', parity: 'odd', ...label,
    letterSpacing: pt(4), placement: at('top', 0) },
  { kind: 'text', id: 'recto-folio', content: '{pageNumber}', parity: 'odd', ...label,
    placement: at('top-right', -SIDE) },
  { kind: 'rule', id: 'hairline', direction: 'horizontal', thickness: pt(0.5), color: col('rule'),
    pages: 'body', placement: { ...at('top-left', SIDE, HEAD_Y + 5), size: { width: mm(MEASURE) } },
  },
] };
// The opener has no head: its folio drops to the foot, centred under the text.
const footer = { elements: [{ kind: 'text', id: 'drop-folio', content: '{pageNumber}',
  ...label, pages: 'opener', align: 'center', placement: at('bottom', 0, -10) }] };

大陆的小说在左页书眉印书名,右页书眉印篇名,居中,页码在外侧,两者下方各有一条细线。pages: 'body'让书眉不出现在开篇页和插页上,开篇页的页码改排在页面底部。书眉用黑体Noto Sans SC,8 pt:字虽小,但与正文的宋体风格分明。

#5 · 占满整张纸的插页

script.js · 第111–143行在完整代码中
// The plate, a painting: a JPEG in assets/ cut to the 140 × 203 mm trim, declared at its pixels.
const resources = [{
  id: 'moon', typeId: 'figure', kind: 'bitmap', createdAt: 0, updatedAt: 0,
  bitmap: { fileId: 'plate-1120.jpg', format: 'jpeg', width: 1120, height: 1624 },
  altText: t({
    en: 'A golden full moon in a deep blue sky over a strip of sea. On the sand below, among '
      + 'rows of striped watermelons, a boy with a silver collar stabs a steel fork at the '
      + 'sand ahead of him, and the small animal he aimed at runs off between his legs.',
    es: 'Una luna llena dorada en un cielo azul oscuro sobre una franja de mar. En la arena, '
      + 'entre hileras de sandías rayadas, un muchacho con un aro de plata al cuello clava una '
      + 'horquilla de acero en la arena, delante de él, y el animalillo al que apuntaba huye '
      + 'entre sus piernas.',
  }),
}];
const quote = { fontFamily: KAI, fontSize: pt(14), lineHeight: 1.5, color: col('paper') };
// # 月下的瓜地 {style="plate" line1="…" line2="…" line3="…"}: a page with no head or folio.
// 'page' spans the design over the sheet: a heading's design in the column is cut at its foot.
const plate = {
  id: 'plate', span: 'page', runningChapter: false, toc: false, // out of the PDF outline
  breakBefore: { enabled: true, parity: 'any' }, // (gotcha: style-inherits-break)
  header: { elements: [] }, footer: { elements: [] },
  advancedDesign: { enabled: true, slot: { elements: [
    { kind: 'image', id: 'picture', resourceId: 'moon', placement: { // the whole trim
      anchor: { to: 'bleed', edge: 'top-left' },
      size: { width: mm(TRIM.width), height: mm(TRIM.height) } } },
    { kind: 'text', id: 'line1', content: '{attr.line1}', ...quote, align: 'left',
      placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: mm(2) } } },
    { kind: 'text', id: 'line2', content: '{attr.line2}', ...quote, align: 'left',
      placement: { anchor: { to: '#line1', edge: 'below' } } },
    { kind: 'text', id: 'line3', content: '{attr.line3}', ...quote, align: 'left',
      placement: { anchor: { to: '#line2', edge: 'below' } } },
  ] } },
};

插图属于一个单独的标题# 月下的瓜地 {style="plate" …},它的样式绘制整页,并清掉这一页的书眉和页码。插页需要span: 'page',因为留在栏内的标题设计会在栏底被截断,离裁切线还差18 mm。runningChapter: false加上toc: false让它不进入PDF书签,书签从故乡开始。引文的三行是三个属性和三个文本元素,因为设计文本有自己的断行器,不处理中文标点宽度;手工断行后,这几行也去掉了逗号,中文的展示性文字通常就是这样处理的。

#6 · 中文正文里的拉丁字母和数字

script.js · 第147–153行在完整代码中
const paragraphStyles = [
  // 小五 (9 pt) nudged to 9.1875 pt: 32 of its characters fill the 28-em measure exactly.
  { id: 'note', fontSize: pt((CHARS * BODY) / 32), lineHeight: pt(LEAD), color: col('ink'),
    firstLineIndent: em(2), marginTop: pt(LEAD) }, // on the grid, a line below the text
  { id: 'colophon', fontFamily: HEI, fontSize: pt(7), lineHeight: pt(11), color: col('muted'),
    textAlign: 'left', firstLineIndent: pt(0), marginTop: pt(LEAD / 2) },
];

第76页上的注释和版权说明是数字、拉丁名称与汉字相遇的地方。latinSpacing在它们相接的地方加上四分之一个全角,如注释中的1921年和版权说明中的用Noto Serif SC五号,这些在输入时都没有空格(汉字与拉丁字母之间的间距);在两端对齐的行中,这个间距最先被拉宽,最多到半个全角。注释的字号是9.1875 pt,而不是小五的9 pt,这样32个字正好排满行长:用9 pt时每行剩下三分之二个全角,被中西文间距吃掉了。Markdown中字体名称的单词之间用不间断空格(U+00A0)连接,中西文间距不会动它们:如果用普通空格,版权说明中齐左排的各行会把Noto Sans / SC断开。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 074 · A Chinese novel page on a 28 × 28 grid ════════════
// https://postext.dev/en/cookbook/chinese-novel-horizontal
// Code: MIT · Text: Lu Xun, 故乡 (1921), public domain, zh.wikisource · Plate: diffusion models
// Fonts: Noto Serif SC, Noto Sans SC, Ma Shan Zheng (SIL OFL 1.1) · Needs postext ≥ 1.65.0
import {
  buildDocument, withLoadedFonts, renderPageToCanvas, registerResourceImage,
} 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-novel-horizontal';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// The night of the story's memory, and ink on a cream paper.
const palette = {
  ink: '#1f1c19', // text: a warm near-black
  night: '#1d3150', // the accent: the deep blue of the plate's sky, the opener's rule
  rule: '#c9c1b2', // the hairline under the running heads
  muted: '#6a645b', // running heads, folios, the colophon
  paper: '#fbf8f1', // a cream book paper
};
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: 'night (defaults)', value: { hex: palette.night, model: 'hex' } },
];
const SONG = 'Noto Serif SC'; // 宋: the text, and the title in its black weight
const HEI = 'Noto Sans SC'; // 黑: running heads, folios, the author, the colophon
const KAI = 'Ma Shan Zheng'; // 楷: a brush regular script for the plate's quotation
const [BODY, LEAD] = [10.5, 16.5]; // pt: 五号 on a 6 pt line gap, the grid's pitch
const [CHARS, LINES] = [28, 28];
const TRIM = { width: 140, height: 203 }; // mm: 大32开
const MEASURE = (CHARS * BODY * 25.4) / 72; // 103.7 mm: 28 ems of 五号
const SIDE = (TRIM.width - MEASURE) / 2; // 18.1 mm: the grid centres the measure

// #region answer: 28 characters × 28 lines of 五号, mainland line breaks and Kaiming marks
const cjk = {
  // The type area in characters: 28 ems wide, 28 lines of LEAD tall. page.margins become
  // minimums, and the grid centres the type area in the room they leave.
  grid: { enabled: true, charsPerLine: CHARS, linesPerPage: LINES },
  // What 'zh-Hans' gives by itself, written out so a reader can compare regions and styles.
  region: 'mainland',
  lineBreak: 'gb', // no 。,、”》 opens a line, no “《( ends one, and no / at either end
  punctuationWidth: 'kaiming', // ,、:; quotes, brackets ½ em; 。?! one em, ½ at a line end
  compressAdjacent: true, // idle under Kaiming (no pair tops 1.5 em); 'fullwidth' needs it
  latinSpacing: em(0.25), // 1921年, 用Noto Serif SC五号: a quarter em, none of it typed
};
const bodyText = {

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

变化

#所有标点都排全角(全角式)

逗号、顿号和引号各占一个字宽,。”占两个字宽,每行容纳的正文字数就少了。

-  punctuationWidth: 'kaiming', // ,、:; quotes, brackets ½ em; 。?! one em, ½ at a line end
-  compressAdjacent: true, // idle under Kaiming (no pair tops 1.5 em); 'fullwidth' needs it
+  punctuationWidth: 'fullwidth',
+  compressAdjacent: false,

#排版时显示网格

Canvas为每一行的每个字位画一个浅灰色方格(稿纸);PDF不画它,除非给renderToPdf传入characterGrid: true。

-  grid: { enabled: true, charsPerLine: CHARS, linesPerPage: LINES },
+  grid: { enabled: true, charsPerLine: CHARS, linesPerPage: LINES, show: true },

常见问题

易错点

中文字体通过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)。 中文断行 →

易错点

各地区的标点要用该地区的字体排

标点宽度调整会把每个标点旁的空白移到文档所属地区规定的一侧,而不是字体绘制时的那一侧:在zh-Hans下,开明式的逗号保留字框的左半部分(简体字体把字形画在这里),让出右半部分。繁体字体把,。画在字框中央,所以让出的那一半含有部分字形,逗号就挤到了下一个字上。LXGW WenKai TC是Fontsource上唯一的楷体正文字体,在简体文字中就会这样。大陆的正文、注释和引文用Noto Serif SC或Noto Sans SC排,TC字体留给zh-Hant,简体毛笔字体(Ma Shan Zheng)只用于展示行,因为设计文本不做标点宽度调整。LXGW WenKai TC还会画出大陆和台湾标准都不采用的传承字形:為用爲的爫头,令(在冷、領中)用卩底。要检查页面上用它排的字。 中文标点宽度 →

易错点

中文没有斜体

CJK字体家族不带斜体,中文排版用字旁的着重号标示强调,而不是倾斜。在标记为中文的文档中,*…*给其中的汉字加着重号,拉丁文词仍用斜体(cjk.emphasis: 'dots',中文的默认值);:dots[…]可以在任何地方加着重号。设置cjk.emphasis: 'italic'时,或在标记为en或es的文档中,*…*会让Canvas把正体字形倾斜,这种合成的伪斜体是中文排版从来不用的(PDF会退回正体字型):保留中文标记,或者通过段落样式或标签样式,用粗体或楷体(LXGW WenKai)表示强调。 中日韩字体 →

易错点

标题样式会继承其级别的分页设置

headingStyles中的条目没有写出的字段都取自它所属的标题级别,breakBefore也不例外。在:::pagebreak之后以H1设置样式的目录页或版权页会继承parity 'odd',结果落在一张空白页之后。给这类样式设置breakBefore: { enabled: false }。 标题样式 →

版面警告 · cjkGridClamped

字符网格太大

原因. `cjk.grid`要求的每行字数或每页行数,按正文字号和行距,超出了页边距留出的空间,因此网格按能容纳的最大值排出。

解决. 调低`charsPerLine`或`linesPerPage`,缩小页边距(它们是最小值),或调小`bodyText.fontSize`或`lineHeight`。 文档 →

  • 插页是一个标题,而标题会得到一个PDF书签:如果插页的样式里没有runningChapter: false和toc: false,书签就会从月下的瓜地开始,它是故乡之前的一个一级书签。只设其中一项,书签仍会保留。带标签的PDF仍会把插页的标题保留为H1。
  • 标题本身的字体必须是FONTS加载了的,即使每个标题都由版面设计绘制:在默认字重700下,工具包在版面中发现了Noto Serif SC 700,于是加载了它的拉丁文件并给出警告。headings.fontWeight: 400让标题块使用正文字体。

致谢

文本
  • “My Old Home” (故乡, 1921), the first half, from the 1948 Complete Works, in simplified characters · Lu Xun (鲁迅) · 公有领域
  • The editor’s note and the colophon · Postext Cookbook · 原创
图片
  • The plate: the moonlit melon field of the story, a painting · Generated With Diffusion Models · 原创
字体
Noto Serif SC (SIL OFL 1.1) · Noto Sans SC (SIL OFL 1.1) · Ma Shan Zheng (SIL OFL 1.1)
沙盒PDF