Skip to main content
Recipe number 74

Cookbook · Chapter 2 · Type & text

A Chinese novel page on a 28 × 28 grid

Lu Xun’s “My Old Home” on a 大32开 page: 28 characters to the line, 28 lines to the page, GB line breaks, Kaiming punctuation and Han–Latin spacing.

On this page
Output
Canvas · PDF
Postext
Tested with Postext 1.65.0
Needs ≥ 1.65.0 · postext-pdf ≥ 1.65.0
Licence
Updated 31 Oct 2026
Code MIT · Text MIT

pp. 70–71 · 1–2 of 7

  • Trim 140 × 203 mm
  • 1 column
  • Noto Serif SC 10.5/16.5
  • Ma Shan Zheng
  • Noto Sans SC
  • 7 pages
  • Level
  • Postext 1.65.0
  • Laid out in 99 ms
  • 179 lines of code

In short

The opening of a Lu Xun story set as a Chinese paperback page. It shows how to fit exactly 28 characters on each line and 28 lines on each page, following Chinese punctuation rules.

What you'll build

The opening of Lu Xun’s “My Old Home” (故乡, 1921) as a mainland paperback of his first collection would set it: a 大32开 page, 140 × 203 mm, whose type area is counted in characters, 28 to the line and 28 lines to the page, in Noto Serif SC at 五号 (10.5 pt). Every paragraph opens two characters in; commas and quotation marks take half a character, as in most mainland books; no line starts with 。 or , and none ends with “. The story opens on a recto under its title in the Song face’s black weight. Facing it, a painted plate shows the moonlit melon field the narrator remembers, with the words that describe it in Ma Shan Zheng’s brush script. Justified Spanish in a pocket novel does the same job for a Latin script.

This recipe answers

  • How do I set the type area in characters, so many to the line and so many lines to the page?
  • How do I keep 。 and , off the start of a line?
  • How do I set Chinese punctuation Kaiming, as mainland books do, or half width?
  • How do I put a space between Chinese and Latin words without typing it?

The short answer

script.js · lines 37–53in full code
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
};

28 characters × 28 lines of 五号, mainland line breaks and Kaiming marks

Ingredients

Type
Noto Serif SC, Noto Sans SC, Ma Shan Zheng (SIL OFL 1.1)
Assets
  • plate-1120.jpg
  • The plate: the moonlit melon field of the story, a painting (Generated With Diffusion Models, original)

Method

#1 · A page counted in characters

The code is the short answer above. cjk.grid makes the measure 28 ems of bodyText.fontSize (294 pt, 103.7 mm) and the type area 28 lines of lineHeight (462 pt, 163 mm), then moves the margins to fit them, so a full line of Han holds exactly 28 characters and every full page ends on the same line (Character grid). The five settings under the grid are what locale: 'zh-Hans' gives by itself; they are written out so you can change one at a time, though compressAdjacent changes nothing until punctuationWidth is 'fullwidth': under Kaiming no two marks that meet take more than 1.5 em. On page 71 the closing 。 of a paragraph does not fit, and the line-start rules (every lineBreak level but 'none'; 'gb' adds the solidus) forbid it to open a line. The line takes it in by setting the 。 after 事 half a character wide (push-in), so 事。宏 closes up and the paragraph does not run to another line (Punctuation widths). Lines that fall short are spread between their characters, never between words, and on these pages by no more than 0.05 em (Chinese, Japanese and Korean).

Page 71: the last lines of a paragraph. 事。宏 is set close so that 看。 can stay on the line instead of opening one.

#2 · A 大32开 page with a deeper head

script.js · lines 57–62in full code
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 },
};

With the grid on, the margins are minimums: the grid places the type area in the room they leave and shares out what is over. 22 and 18 mm leave exactly 163 mm, so the head margin stays 4 mm deeper than the foot, as Chinese books keep it; the 18 mm at each side leave 0.3 mm to share. Ask for more characters or lines than fit and the grid is cut down to what fits, with a cjkGridClamped warning; the size and leading stay as written.

#3 · A title sunk a whole number of lines

script.js · lines 66–84in full code
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 is eight lines of 16.5 pt, so the text under the title starts on the ninth line of the grid, the same baselines as on every other page. The 23 pt of letter spacing puts half a character between 故 and 乡, the usual spacing for a two-character title; Postext leaves the spacing after the last character out when it centres the line, so the title stays on the page’s axis (Heading styles).

#4 · The book on the verso, the story on the recto

script.js · lines 88–107in full code
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) }] };

Mainland novels carry the book’s title over versos and the story’s over rectos, centred, with the folio at the outer edge and a hairline under both. pages: 'body' keeps them off the opener and the plate, and the opener gets its folio at the foot instead. The heads are set in Noto Sans SC, the Hei face, at 8 pt: small, but a different voice from the Song of the text.

#5 · A plate over the whole sheet

script.js · lines 111–143in full code
// 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' } } },
  ] } },
};

The picture belongs to a heading of its own, # 月下的瓜地 {style="plate" …}, whose style draws the page and clears its head and folio. The plate needs span: 'page', because a heading’s design that stays in the column is cut at the column’s foot, 18 mm short of the trim. runningChapter: false with toc: false leaves it out of the PDF outline, which opens on 故乡. The three lines of the quotation are three attributes and three text elements, because design text has a line breaker of its own, which sets no Chinese punctuation widths; broken by hand, the lines also drop their commas, as display lines in Chinese usually do.

#6 · Latin letters and digits in the Chinese text

script.js · lines 147–153in full code
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) },
];

The note and the colophon on page 76 are where digits and Latin names meet Han. latinSpacing puts a quarter em wherever they touch, as in the note’s 1921年 and the colophon’s 用Noto Serif SC五号, typed with no space at all (Space between Han and Latin); on a justified line that space grows first, up to half an em. The note is set at 9.1875 pt, not the 9 pt of 小五, so that 32 of its characters fill the measure: at 9 pt each line had two thirds of an em left over, and the Han–Latin spaces took it. In the Markdown the font names are joined with no-break spaces (U+00A0), which the Han–Latin space leaves alone: with ordinary spaces the colophon’s ragged lines broke Noto Sans / SC.

The whole recipe

Sandbox
// ═══ 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 = {

The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)

Variations

#Set every mark full width (全角式)

Commas, pause marks and quotation marks take a whole character, 。” takes two, and each line holds fewer characters of text.

-  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,

#Show the grid while you set the page

The canvas draws a light grey square for every character position of every line (稿纸); the PDF leaves it out unless renderToPdf gets characterGrid: true.

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

Pitfalls

Pitfall

Chinese faces load by slices, through the cjk block

Fontsource serves a Chinese, Japanese or Korean family as about a hundred files per weight, each covering a range of characters. loadFonts fetches only the latin file, so on screen the Han characters come from a system face and measure wrong, and fontsourceProvider hands the PDF that latin file, which prints them as empty boxes. List the cjk kit block, call loadCjkFonts(FONTS, markdown) after loadFonts (once per voice, with the text it sets, when the book uses several CJK faces) and give renderToPdf fontProvider: cjkPdfProvider: both take the files that hold the text's characters. Chinese, Japanese and Korean fonts →

Pitfall

Tag the document zh-Hans or zh-Hant, not with LANG

A recipe's editions are en and es, but a Chinese sample is Chinese in both: `locale: LANG` would tag it English or Spanish, hyphenate its Latin words, label its figures Figure or Figura and give the PDF the wrong language. From postext 1.30 the tag is also the language the text is shaped in (measured, painted and embedded), so under an English or Spanish tag a Chinese face sets its digits and the punctuation it shares with Latin text in other forms, and the lines break elsewhere. Write the tag yourself: 'zh-Hans' (mainland conventions: GB line breaking, Kaiming punctuation) or 'zh-Hant' (Taiwan: full-width centred punctuation); 'zh-HK' for Hong Kong. A bare 'zh' reads as Simplified, mainland. A Japanese sample takes 'ja' (gotcha ja-locale-tag). Chinese line breaking →

Pitfall

Set each region's punctuation in a face of that region

The punctuation widths move the blank beside each mark to the side the document's region puts it, not the side the font draws it: under zh-Hans a Kaiming comma keeps the left half of its box, where a Simplified face draws the glyph, and gives up the right half. A Traditional face centres ,。 in the box, so the half that goes holds part of the glyph and the comma crowds the next character. LXGW WenKai TC, the only Kai text face on Fontsource, does this in Simplified text. Set mainland text, notes and quotations in Noto Serif SC or Noto Sans SC, keep the TC faces for zh-Hant, and use a Simplified brush face (Ma Shan Zheng) only for display lines, where design text applies no punctuation widths. It also draws inherited forms that neither the mainland nor the Taiwan standard prints, 為 with the 爫 top of 爲 and 令 (in 冷, 領) with a 卩 foot: check the characters a page sets in it. Chinese punctuation widths →

Pitfall

Chinese has no italic

The CJK families ship no italic, and Chinese typography marks emphasis with dots beside the characters, not with a slant. In a document tagged Chinese, *…* sets emphasis dots on the Chinese characters it holds and keeps italics for the Latin words (cjk.emphasis: 'dots', the default for Chinese); :dots[…] sets them anywhere. With cjk.emphasis: 'italic', or in a document tagged en or es, *…* makes the canvas slant the upright glyphs, a synthetic oblique that Chinese typography never uses (the PDF falls back to the upright face): keep the Chinese tag, or set emphasis in bold or in a Kai face (LXGW WenKai) through a paragraph or chip style. Chinese, Japanese and Korean fonts →

Pitfall

A heading style inherits its level's page break

A headingStyles entry takes every field it leaves out from its heading level, breakBefore included. A contents page or a colophon styled on an H1 after a :::pagebreak inherits parity 'odd' and lands behind a blank page. Give such a style breakBefore: { enabled: false }. Heading styles →

Layout warning · cjkGridClamped

Character grid too large

Why. `cjk.grid` asks for more characters per line or lines per page than the page margins leave room for at the body size and line height, so the grid is set with the most that fit.

Fix. Lower `charsPerLine` or `linesPerPage`, narrow the margins (they are minimums), or set a smaller `bodyText.fontSize` or `lineHeight`. Docs →

  • The plate is a heading, and a heading gets a PDF bookmark: without runningChapter: false and toc: false in the plate’s style, the outline would open on 月下的瓜地, a first-level bookmark before 故乡. Either setting alone keeps it. The tagged PDF still keeps the plate’s title as an H1.
  • The headings’ own face must be one that FONTS loads, even when a design paints every title: with the default weight 700 the kit found Noto Serif SC 700 in the layout and loaded its Latin file with a warning. headings.fontWeight: 400 keeps the heading blocks in the text face.

Credits

Text
  • “My Old Home” (故乡, 1921), the first half, from the 1948 Complete Works, in simplified characters · Lu Xun (鲁迅) · public domain
  • The editor’s note and the colophon · Postext Cookbook · original
Images
  • The plate: the moonlit melon field of the story, a painting · Generated With Diffusion Models · original
Fonts
Noto Serif SC (SIL OFL 1.1) · Noto Sans SC (SIL OFL 1.1) · Ma Shan Zheng (SIL OFL 1.1)
SandboxPDF