跳到主要内容
食谱编号130

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

用拉丁字母写的日文:啄木的《罗马字日记》

标记为ja-Latn的罗马字文本:其中的假名遵循日文规则,拉丁文不断词,拉丁字体的长音符号也嵌入PDF。

本页内容
输出
Canvas · PDF
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.25.0 · postext-pdf ≥ 1.25.0
许可证
更新于2026年10月10日
代码MIT · 文本CC BY 4.0
  • 英文样例:尚无中文版本
  • 成品尺寸148 × 210 mm
  • 1栏
  • Source Serif 4 10.5/15
  • Noto Serif JP
  • Source Sans 3
  • 4页
  • 难度
  • Postext 1.65.0
  • 排版用时119 ms
  • 131行代码

简单来说

诗人石川啄木1909年用拉丁字母写日文的日记的第一篇,每段下面附有日文文字的读法。

成品一览

《罗马字日记》(Rōmaji nikki)四页:1909年春,石川啄木在东京用拉丁字母记日记,好让妻子读不懂。内容是笔记本开头的标题行和第一篇,即4月7日的日记,到Kanasii koto da!为止。日记按研读本的样式排在A5页面上:罗马字用Source Serif 4,两端对齐,从不断词;每段下面是用Noto Serif JP排的假名汉字读法,字号更小、颜色更浅、向内缩进。啄木用的是日本式罗马字(Nippon-shiki),写si、tu、hu,长元音加抑扬符,如Tôkyô;编者的文字用带长音符号的赫本式,如Rōmaji,两者都能进入PDF。

这道食谱解答

  • 用罗马字写的日文怎么排:用哪个语言标记,怎样断词,PDF里的长音符号怎么办?
  • 哪些字体能排日文,并在屏幕和PDF中让汉字采用日文字形?

简短回答

script.js · 第35–50行在完整代码中
// ja-Latn is Japanese written in Latin script (BCP 47). Its language is ja, so the kana
// under each paragraph get the Japanese rules (kinsoku, 、。 spacing, the space after !)
// and the built-in strings are Japanese; and Japanese has no hyphenation patterns, so the
// rōmaji is never divided at a line end. English patterns would divide tatinobotta or
// Surigarasu by English syllables, not by the morae a Japanese reader hears.
const language = { locale: 'ja-Latn' }; // spread into the config
const bodyText = {
  fontFamily: SERIF, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  textAlign: 'justify', firstLineIndent: em(1.2), indentAfterHeading: false,
  optimalLineBreaking: true, maxWordSpacing: 1.8, // no hyphens: the spaces take the slack
};
// The reading in kana, under its paragraph: smaller, a shade lighter, indented.
const kana = { id: 'kana', fontFamily: MINCHO, fontSize: pt(8.6), lineHeight: pt(LEAD),
  color: col('kana'), textAlign: 'justify', indent: em(1.5), firstLineIndent: em(1),
  marginTop: pt(LEAD / 4), marginBottom: pt(LEAD * 0.75) };

用料

类型
Source Serif 4, Source Sans 3, Noto Serif JP(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 把文本标记为ja-Latn

代码见上文的简短回答。ja-Latn是BCP 47中“用拉丁字母书写的日文”的标记。它的语言是ja,所以每段下面的读法遵循日文规则:禁则、、。的间距,以及引擎在句中!后自动加的一字空格,如「ええ! ええ!」。日文没有断词规则,所以罗马字从不断开;若用en,英文断词规则会按英文音节去拆tatinobotta或Surigarasu这样的词,而不是按日本读者听到的音拍。没有连字符,间距就由空格吸收,所以行长约70个字符,maxWordSpacing设为1.8(断词)。

第2页:日期、地址、前两段罗马字,以及较小、缩进的假名读法。

#2 · 让长音符号进入PDF

script.js · 第201–203行在完整代码中
// Fontsource keeps ō ū ā in a latin-ext file. cjkPdfProvider serves Noto Serif JP from its
// slices, and adds that file after the latin one for a Latin face whose text needs it.
offerPdf(() => renderToPdf(doc, { fontProvider: cjkPdfProvider }), `${RECIPE}.pdf`);

Fontsource把ō ū ā放在单独的latin-ext文件里。屏幕会加载它(loadFonts看到样本中的ō),工具包的cjkPdfProvider在PDF里也这样做。它从日文分片提供Noto Serif JP;拉丁字体排到只有latin-ext才有的字母时,它返回两个文件,先latin后latin-ext,postext-pdf从第一个含有该字形的文件中取字。工具包普通的fontsourceProvider只把latin文件交给PDF,用它的话,PDF里就没有ō的字形。

#3 · 以日记自己的标题行开篇

script.js · 第54–86行在完整代码中
const onField = { color: col('paper'), align: 'center', overflow: 'wrap' };
const title = {
  id: 'title', numbered: false, toc: false, span: 'page', runningChapter: false,
  breakBefore: { enabled: true, parity: 'any' }, // its page is an opener: no running heads
  advancedDesign: { enabled: true, minHeight: mm(166), slot: { elements: [
    { kind: 'box', id: 'field', style: { backgroundColor: col('indigo') },
      placement: { anchor: { to: 'bleed', edge: 'top-left' }, size: { width: 'fill',
        height: 'fill' } } },
    { kind: 'text', id: 'lines', content: '{attr.lines}', ...onField, fontFamily: SERIF,
      fontWeight: 600, fontSize: pt(20), lineHeight: 1.9, letterSpacing: pt(5),
      placement: { anchor: { to: 'container', edge: 'top' }, offset: { y: mm(18) } } },
    { kind: 'rule', id: 'rule', thickness: pt(0.5), color: col('paper'),
      placement: { anchor: { to: 'container', edge: 'top' }, offset: { y: mm(122) },
        size: { width: mm(24) } } },
    { kind: 'text', id: 'note', content: '{attr.note}', inlineMarks: true, ...onField,
      fontFamily: SANS, fontSize: pt(8.5), lineHeight: 1.5, placement: { anchor: { to:
        'container', edge: 'top' }, offset: { y: mm(130) }, size: { width: mm(84) } } },
  ] } },
};
// The entry: its day as a heading, the address under it from an attribute.
const entry = {
  level: 2, fontFamily: SANS, fontWeight: 600, fontSize: pt(10), letterSpacing: pt(2.5),
  color: col('indigo'), marginTop: pt(LEAD), marginBottom: pt(LEAD), advancedDesign: {
    enabled: true, minHeight: pt(2 * LEAD), slot: { elements: [
      { kind: 'text', id: 'day', content: '{titleText}', fontFamily: SANS, fontWeight: 600,
        fontSize: pt(10), letterSpacing: pt(2.5), color: col('indigo'),
        placement: { anchor: { to: 'container', edge: 'top-left' } } },
      { kind: 'text', id: 'place', content: '{attr.place}', fontFamily: SANS, fontSize: pt(7.5),
        letterSpacing: pt(1), color: col('muted'), overflow: 'wrap', align: 'left',
        placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: pt(LEAD) },
          size: { width: 'fill' } } },
    ] } },
};

笔记本以NIKKI. I. MEIDI 42 NEN. 1909. APRIL. TOKYO.开头,每项一行。标题从属性中取出这些行,每个\n另起一行,用加宽字距的大写字母排在靛蓝底上,像笔记本的布面封皮。这一页是开篇页,所以不印书眉。

#4 · 书眉按奇偶页排

script.js · 第90–99行在完整代码中
const head = (id, content, parity, edge) => ({ kind: 'text', id, content, parity,
  pages: 'body', fontFamily: SANS, fontSize: pt(7.5), letterSpacing: pt(1.5), color: col('muted'),
  placement: { anchor: { to: 'page', edge }, offset: { y: mm(13), // 'top' centres on the page
    x: mm(edge.endsWith('left') ? 16 : edge.endsWith('right') ? -16 : 0) } } });
const header = { elements: [
  head('folio-even', '{pageNumber}', 'even', 'top-left'),
  head('book', 'ROMAZI NIKKI · 1909', 'even', 'top'),
  head('place', 'TOKYO · APRIL', 'odd', 'top'),
  head('folio-odd', '{pageNumber}', 'odd', 'top-right'),
] };

偶数页印日记名,奇数页印地点和月份,页码在外侧角上,用7.5 pt的Source Sans 3。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 130 · Japanese in Latin letters: Takuboku's Rōmaji Diary ════
// https://postext.dev/en/cookbook/romaji-nikki
// Code: MIT · Text: Ishikawa Takuboku, 1909 (public domain); transcription (CC BY 4.0)
// Fonts: Source Serif 4, Source Sans 3, Noto Serif JP (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// A diary written in Japanese with Latin letters, and its reading in kana under each paragraph.
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 note; the diary is Japanese in both editions
const RECIPE = 'romaji-nikki';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: diary ink, a faded indigo, the cream of a notebook
const palette = {
  ink: '#22201d', // the rōmaji
  indigo: '#34497a', // the date and the title (8.4:1 on the paper)
  kana: '#5a554e', // the transcription, a step lighter than the text (7:1)
  rule: '#c9c1b2',
  muted: '#6f6a62', // running heads, folios, the note
  paper: '#fcfaf4',
};
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: 'indigo (defaults)', value: { hex: palette.indigo, model: 'hex' } },
];
// #endregion

const SERIF = 'Source Serif 4'; // the rōmaji: ô û â ê in latin, ō ū in latin-ext
const SANS = 'Source Sans 3'; // labels, running heads, folios, the note
const MINCHO = 'Noto Serif JP'; // the kana: Noto Serif JP's Latin is Source Serif's
const [BODY, LEAD] = [10.5, 15]; // pt

// #region answer: a Japanese text in Latin letters is tagged ja-Latn
// ja-Latn is Japanese written in Latin script (BCP 47). Its language is ja, so the kana
// under each paragraph get the Japanese rules (kinsoku, 、。 spacing, the space after !)
// and the built-in strings are Japanese; and Japanese has no hyphenation patterns, so the
// rōmaji is never divided at a line end. English patterns would divide tatinobotta or
// Surigarasu by English syllables, not by the morae a Japanese reader hears.
const language = { locale: 'ja-Latn' }; // spread into the config
const bodyText = {
  fontFamily: SERIF, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  textAlign: 'justify', firstLineIndent: em(1.2), indentAfterHeading: false,
  optimalLineBreaking: true, maxWordSpacing: 1.8, // no hyphens: the spaces take the slack
};
// The reading in kana, under its paragraph: smaller, a shade lighter, indented.
const kana = { id: 'kana', fontFamily: MINCHO, fontSize: pt(8.6), lineHeight: pt(LEAD),

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

变化

#用赫本式重写罗马字

现代版本可以把日记改写成带长音符号的赫本式,如把Tôkyô写成Tōkyō。PDF已经能印出ō,只需改文本;另加一条说明,指出拼写出自编者。

#在音拍之间断词

窄栏需要连字符时,可在罗马字的音拍之间加软连字符(U+00AD),代码中写作Su­na-ho­ko­ri,或直接在文本里输入U+00AD:标记为ja-Latn的文档不会自动断词,但词放不下时会在软连字符处断开。

常见问题

易错点

日文文本标记为'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:两者都会取用包含文中字符的那些文件。 中日韩字体 →

  • Shippori Mincho没有ō和ū;Noto Serif JP和Source Serif 4的latin-ext文件里有。给赫本式文本选字体之前先查一下。
  • 用拉丁字母写的中文是拼音,在每个字上方标拼音的识字课本里作为注音排在汉字上方;啄木的罗马字本身就是正文,所以按拉丁文排,有自己的词间距,而不是作为注释。

致谢

文本
  • Ishikawa Takuboku, ROMAZI NIKKI (the Rōmaji Diary), the entry of 7 April 1909 to “Kanasii koto da!”, in Latin letters as he wrote it, after the Iwanami Bunko edition (ed. Kuwabara Takeo, 1977) as transcribed on the blog 啄木の息 · Ishikawa Takuboku (1886–1912) · 公有领域
  • The transcription into kana and kanji under each paragraph and the note on the text, written for this recipe · Postext Cookbook · CC BY 4.0
字体
Source Serif 4 (SIL OFL 1.1) · Source Sans 3 (SIL OFL 1.1) · Noto Serif JP (SIL OFL 1.1)
沙盒PDF