跳到主要内容
食谱编号127

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

按词组空格的平假名图画书

一本竖排平假名图画书,词组之间留空格(分かち書き):用keep-all让每个词组保持完整,行只在空格处断开。

本页内容
输出
Canvas · PDF
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.25.0 · postext-pdf ≥ 1.25.0
许可证
更新于2026年10月10日
代码MIT · 文本CC BY 4.0
  • 英文样例:尚无中文版本
  • 成品尺寸260 × 200 mm
  • 一栏半, 栏间距14.1 mm
  • Klee One 20/36
  • Zen Maru Gothic
  • 10页
  • 难度
  • Postext 1.57.1
  • 排版用时46 ms
  • 136行代码

简单来说

一本给幼儿看的日文图画书,全部用平假名,每组词后留一个空格,换行时从不把一组词拆开,每页上方都有一幅画。

成品一览

一本十页的图画书《かみの ふね》(纸船),文字为本示例而作,配有十幅画:雨停后,女孩折了一只红色纸船,纸船顺河而下,经过青蛙、苍鹭和月亮,最后到达大海。排法与日本给最小读者的书一样:只用平假名,20 pt的Klee One(一种依照小学教科书字样设计的字体),词组之间留空格,即分かち書き(wakachigaki),让孩子一次读一个词组。页面为横长的260 × 200 mm,竖排,右侧装订:图在上方,下面是每行八个字的短行,纸船随着翻页向左行进。

这道食谱解答

  • 怎样给日文图画书按词组加空格(分かち書き),让行只在空格处断开?
  • 为什么日文的小假名、ー和闭括号不能出现在行首?怎样选择规则的严格程度?
  • 哪些字体能排日文,并在屏幕和PDF中让汉字采用日文字形?

简短回答

script.js · 第40–50行在完整代码中
// The story is typed with an ordinary space (U+0020) after each phrase (文節). Japanese
// text may break between any two kana, so the space alone does not keep a phrase whole:
// keep-all drops the breaks between two letters and keeps those at a space, after 、。」
// and before 「. Kinsoku still applies.
const phrases = { wordBreak: 'keep-all' }; // cjk.wordBreak
const bodyText = {
  fontFamily: HAND, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  // Ragged: a justified line would stretch its spaces and its kana to fill the tier.
  textAlign: 'left', firstLineIndent: em(0),
};

用料

类型
Klee One, Zen Maru Gothic(SIL OFL 1.1)
素材
  • bridge-1400.jpg
  • cover-1430.jpg
  • dusk-1400.jpg
  • frog-1400.jpg
  • heron-1400.jpg
  • night-1400.jpg
  • rain-1400.jpg
  • sea-1400.jpg
  • stream-1400.jpg
  • window-1400.jpg
  • The cover: a red paper boat on a river through green meadows, a clear sky on the right for the title (Generated With Diffusion Models, 原创)
  • Mii-chan folding the red paper boat at a low table by the window, after the rain (Generated With Diffusion Models, 原创)
  • Mii-chan waving goodbye to the boat on the stream (Generated With Diffusion Models, 原创)
  • A frog on a stone talking to the boat (Generated With Diffusion Models, 原创)
  • The boat under a wooden footbridge, little fish following it (Generated With Diffusion Models, 原创)
  • A white heron on the bank watching the boat on the wide river (Generated With Diffusion Models, 原创)
  • Dusk: a pink sky over the river mouth and the sea beyond (Generated With Diffusion Models, 原创)
  • Night: the full moon's reflection lighting the boat's way (Generated With Diffusion Models, 原创)
  • The boat at sea, greeted by seagulls (Generated With Diffusion Models, 原创)
  • Mii-chan at her window, looking towards the distant sea (Generated With Diffusion Models, 原创)

做法

#1 · 让每个词组保持完整

代码见上文的简短回答。日文可以在任意两个假名之间断行,所以词组间的空格只是多留一点空白,并不能阻止行在あか|い之间断开。cjk.wordBreak: 'keep-all'只保留空格处和标点旁的断点,即、。」之后和「之前,与CSS的word-break: keep-all相同,禁则照样有效(东亚排版)。空格是普通的U+0020,在Klee One中宽0.35字,落在行末时会被去掉。U+3000同样是词组之间的断点,但它是一个固定一字宽的字符,会留在行末。

第3页:八行,每行都在空格处或」、之后结束:「いって / らっしゃい。」 / みいちゃんが / そっと てを……

正文不两端对齐。若两端对齐,短行会把空格和假名间距拉开直到层底;图画书让每行停在最后一个词组结束的地方。

#2 · 图放在正文上方的一层

script.js · 第54–62行在完整代码中
// In vertical text 'left' is the top of the page: the side column is a tier that takes
// the pictures (floats), and the text runs in the tier under it, right to left.
const layout = {
  layoutType: 'oneAndHalf', writingMode: 'vertical-rl', sideColumnRole: 'floats',
  sideColumnSide: 'left', sideColumnPercent: 58, gutterWidth: pt(2 * BODY),
};
const cjk = { ...phrases, grid: { enabled: true, charsPerLine: CHARS, linesPerPage: LINES } };
const resourceTypes = [{ id: 'picture', name: 'え', shortLabel: 'え', numberingTemplate: '{n}',
  resetOn: 'never', counterFormat: 'decimal', captionPrefix: '' }]; // pictures, no captions

在竖排中,'left'指页面的上方,所以侧栏在左的“一栏半”版式就成了上下两层:图浮动到上层(深14字),正文排在下层(8字)。每页用::resource{id="…"}引用自己的图,并以:::pagebreak结束。图的资源类型不带题注前缀,图的宽高比为2.186:1,正是这一层的形状,因此正好排满(竖排)。

#3 · 封面做成整页图

script.js · 第66–83行在完整代码中
// In the flow frame of a vertical page x runs down the sheet and y leftward from its right
// edge; a box's width runs down the sheet.
const at = (down, across) => ({ anchor: { to: 'page', edge: 'top-left' },
  offset: { x: mm(down), y: mm(across) } });
const cover = {
  id: 'cover', numbered: false, toc: false, span: 'page', header: { elements: [] },
  breakBefore: { enabled: true, parity: 'any' },
  advancedDesign: { enabled: true, minHeight: mm(TRIM.w - 40), slot: { elements: [
    { kind: 'image', id: 'art', resourceId: 'cover', decorative: true,
      placement: { anchor: { to: 'bleed', edge: 'top-left' },
        size: { width: mm(TRIM.h), height: mm(TRIM.w) } } },
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: ROUND, fontWeight: 700,
      fontSize: pt(60), lineHeight: 1, letterSpacing: pt(6), color: col('boat'),
      placement: at(22, 34) },
    { kind: 'text', id: 'latin', content: '{attr.latin}', fontFamily: ROUND, fontWeight: 700,
      fontSize: pt(9), lineHeight: 1.4, color: col('ink'), placement: at(22, 62) },
  ] } },
};

封面是整页的标题设计:图锚定在出血框,大小与页面相同(在竖排流中它的width沿纸面向下),书名用60 pt的Zen Maru Gothic沿右侧天空竖排。

#4 · 把画作为位图载入

script.js · 第188–203行在完整代码中
// The scenes have the tier's shape, 612 : 280 (ART); the cover has the page's, 260 : 200.
const PICTURES = { // id: [file, width, height, alt text in the book's language]
  cover: ['cover-1430.jpg', 1430, 1100, 'あおい かわに あかい かみの ふねが うかんで いる。'],
  rain: ['rain-1400.jpg', 1400, 641, 'まどべの ちゃぶだいで、みいちゃんが あかい かみで ふねを おって いる。'],
  stream: ['stream-1400.jpg', 1400, 641, 'みいちゃんが おがわに ふねを ながし、てを ふって いる。'],
  frog: ['frog-1400.jpg', 1400, 641, 'かわの いしの うえの かえるが、ながれて いく ふねに はなしかけて いる。'],
  bridge: ['bridge-1400.jpg', 1400, 641, 'きの はしの したを ふねが くぐり、ちいさな さかなが ついて くる。'],
  heron: ['heron-1400.jpg', 1400, 641, 'ひろい かわの きしで、しろい さぎが ふねを みおくって いる。'],
  dusk: ['dusk-1400.jpg', 1400, 641, 'ももいろの ゆうやけの かわに ふねが うかび、とおくに うみが みえる。'],
  night: ['night-1400.jpg', 1400, 641, 'まんげつが かわに うつり、ひかりの みちの そばを ふねが すすむ。'],
  sea: ['sea-1400.jpg', 1400, 641, 'あおい うみに でた ふねの うえを、かもめが とんで いる。'],
  window: ['window-1400.jpg', 1400, 641, 'みいちゃんが まどから、まちの むこうの うみを みて いる。'],
};
const pictures = Object.entries(PICTURES).map(([id, [fileId, w, h, altText]]) => ({
  id, typeId: 'picture', kind: 'bitmap', createdAt: 0, updatedAt: 0, altText,
  placement: { span: 'side' }, bitmap: { fileId, format: 'jpeg', width: w, height: h } }));

十幅插图是绘画,即示例assets/里的JPEG文件。九个场景按图层的比例612 : 280裁切,封面按页面的比例260 : 200裁切,所以每幅都正好填满图层,不会被裁掉任何部分。每幅都是按像素尺寸声明的bitmap资源,带有用本书语言写的替代文字,排版前按资源的fileId载入文件。宽1,400 px的场景在图层宽度上约以165 dpi印出(资源类型)。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 127 · A hiragana picture book, spaced by phrase ══════════
// https://postext.dev/en/cookbook/hiragana-picture-book
// Code: MIT · Story: written for the recipe (CC BY 4.0) · Pictures: generated with diffusion models
// Fonts: Klee One, Zen Maru Gothic (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// A picture book for small readers: hiragana only, a space between phrases, vertical.
import {
  buildDocument, withLoadedFonts, renderPageToCanvas, registerResourceImage, loadVerticalAlternates,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';

const LANG = 'en'; // @lang: the language of the frame; the story is Japanese in both editions
const RECIPE = 'hiragana-picture-book';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: the paper boat's red, river blues, a warm paper
const palette = {
  ink: '#2e2a26', // the text: a soft near-black
  boat: '#cf4430', // the boat, the title (4.6:1 on the paper)
  river: '#5d93ad',
  grass: '#86b665',
  muted: '#6e675f', // the colophon
  paper: '#fbf6ea',
};
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: 'boat (defaults)', value: { hex: palette.boat, model: 'hex' } },
];
// #endregion

const HAND = 'Klee One'; // 教科書体: the hand a child learns to write kana from
const ROUND = 'Zen Maru Gothic'; // the cover's title
const [BODY, LEAD] = [20, 36]; // pt: large kana, a line gap of 0.8 em
const PT = 25.4 / 72; // mm in a point
const TRIM = { w: 260, h: 200 }; // mm: a landscape picture book (横長)
const [CHARS, PICTURE, LINES] = [8, 14, 17]; // characters: text tier, picture tier; lines
const ART = { w: LINES * LEAD * PT, h: PICTURE * BODY * PT }; // mm: 215.9 × 98.8

// #region answer: わかち書き — a space between phrases, and lines that break only there
// The story is typed with an ordinary space (U+0020) after each phrase (文節). Japanese
// text may break between any two kana, so the space alone does not keep a phrase whole:
// keep-all drops the breaks between two letters and keeps those at a space, after 、。」
// and before 「. Kinsoku still applies.
const phrases = { wordBreak: 'keep-all' }; // cjk.wordBreak
const bodyText = {
  fontFamily: HAND, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  // Ragged: a justified line would stretch its spaces and its kana to fill the tier.

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

变化

#两端对齐

两端对齐时,每行都排到层底:先拉开词组间的空格,最多到半字,然后拉开假名之间的间距(JLReq §3.8)。20 pt假名的短行会因此出现很宽的空隙,所以本示例让行尾不齐。

-  textAlign: 'left', firstLineIndent: em(0),
+  textAlign: 'justify', firstLineIndent: em(0),

常见问题

易错点

日文文本标记为'ja',不要用zh-Hans或LANG

食谱的版本是en和es,但日文示例在两个版本里都是日文:`locale: LANG`会把它标记为英文或西班牙文,中文标记则会按中文规则排它(开明式标点、小假名可以出现在行首、图代替図、PDF中用中文字形)。postext 1.30起,这个标记也是文字成形所用的语言(测量、绘制和嵌入都按它进行),所以'ja'以外的标记给出的不是日文字形(中文标记给出中文字形),断行的位置也不同。写'ja':它选用日本地区(JLReq断行和标点、芝麻着重号、振假名的分布、図和表标签),并关闭断词。lint会让带假名却标记为zh或ko的文本失败。 日文禁则处理 →

易错点

日文要用日文字体排

Noto Serif SC和TC里有假名,但汉字是中文字形(直、骨、角都不同),假名也是中文字体的设计。正文用Noto Serif JP或Shippori Mincho B1,标题用Noto Sans JP,用loadCjkFonts加载。Fontsource的日文文件里没有变体假名等历史假名(U+1B000–1B16F):loadCjkFonts遇到它们会失败,PDF里印出空框,所以要写现代假名,或者把含有这些字的字体放进食谱的assets。Shippori Mincho也没有带长音符号的ō和ū:罗马字要用拉丁字体排。 假名、汉字与罗马字 →

易错点

中文字体通过cjk块分片加载

Fontsource把一个中文、日文或韩文字体家族按每个字重约一百个文件提供,每个文件覆盖一段字符范围。loadFonts只获取latin文件,所以屏幕上的汉字来自系统字体,测量不准;fontsourceProvider交给PDF的也是这个latin文件,汉字印出来是空框。列出kit中的cjk块,在loadFonts之后调用loadCjkFonts(FONTS, markdown)(书中用到几种CJK字体时,每种字体调用一次,并传入它所排的文字),并给renderToPdf传fontProvider: cjkPdfProvider:两者都会取用包含文中字符的那些文件。 中日韩字体 →

  • keep-all只保留文本里已有的断点:Postext不会自己找出词组,空格要由作者输入。比一行还长的词组仍会在内部断开,断在禁则允许的最后一个位置。
  • 中文从不在词间留空格,中文读本右侧注音的竖排读本用注音帮助小读者;日文图画书不用汉字,所以不需要振假名,词组间的空格就起到引导阅读的作用。

致谢

文本
  • かみの ふね (The Paper Boat), a picture-book story in hiragana written for this recipe · Postext Cookbook · CC BY 4.0
图片
  • The cover: a red paper boat on a river through green meadows, a clear sky on the right for the title · Generated With Diffusion Models · 原创
  • Mii-chan folding the red paper boat at a low table by the window, after the rain · Generated With Diffusion Models · 原创
  • Mii-chan waving goodbye to the boat on the stream · Generated With Diffusion Models · 原创
  • A frog on a stone talking to the boat · Generated With Diffusion Models · 原创
  • The boat under a wooden footbridge, little fish following it · Generated With Diffusion Models · 原创
  • A white heron on the bank watching the boat on the wide river · Generated With Diffusion Models · 原创
  • Dusk: a pink sky over the river mouth and the sea beyond · Generated With Diffusion Models · 原创
  • Night: the full moon's reflection lighting the boat's way · Generated With Diffusion Models · 原创
  • The boat at sea, greeted by seagulls · Generated With Diffusion Models · 原创
  • Mii-chan at her window, looking towards the distant sea · Generated With Diffusion Models · 原创
字体
Klee One (SIL OFL 1.1) · Zen Maru Gothic (SIL OFL 1.1)
沙盒PDF