跳到主要内容
食谱编号105

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

从右向左排、右侧装订的阿拉伯文小说

14 × 21 cm页面上的阿拉伯文小说开头:locale 'ar'让文字从右向左排,书在右侧装订,页码印作١٢。

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

页码١٠–١١ · 第1–2页,共7页

  • 英文样例:尚无中文版本
  • 成品尺寸140 × 210 mm
  • 1栏
  • Markazi Text 13.5/22
  • Noto Kufi Arabic
  • Reem Kufi
  • 7页
  • 难度
  • Postext 1.57.1
  • 排版用时94 ms
  • 161行代码

简单来说

一部阿拉伯文小说的头几页,按阿拉伯出版社的样子排:每行从右往左读,书从西文读者眼中的封底那一侧翻开,页码用阿拉伯文数字。

成品一览

小说《حروف من رصاص》(铅字)的开头:孙子回到开罗一条小巷,重新打开祖父的印刷铺。按阿拉伯平装书排:14 × 21 cm,Markazi Text 13.5 pt,行距22 pt,两端对齐,首行缩进在右侧。节选从第10页开始,先是一张部扉页,满版是印刷铺大门的那种蓝色,上面是用代码画的八角星格纹,部名用64 pt的Reem Kufi。对面的左页是章首页,章序用词语写成:الفصل الأول。页码印作١٠、١١、١٢;右页书眉是书名,左页书眉是章名。平装小说从左向右排同一类书。

这道食谱解答

  • 怎样让阿拉伯文书从右向左排并右装订?

简短回答

script.js · 第35–50行在完整代码中
const page = {
  width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, backgroundColor: col('paper'),
  // left and right are the flow's sides: in a right-to-left book left is the spine side,
  // so these margins put 20 mm at the spine and 16 mm outside on both pages of a spread.
  margins: { top: mm(23), bottom: mm(22), left: mm(20), right: mm(OUTER), mirror: true },
  // binding: 'auto' (unset) is 'right' for a right-to-left locale: page 1 is a left page.
};
const bodyText = {
  fontFamily: TEXT, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  // justify ends every line on both sides; the engine lengthens joined letters (kashida,
  // on by default for Arabic) after it opens the spaces a quarter. Never hyphenated.
  textAlign: 'justify', optimalLineBreaking: true,
  firstLineIndent: em(1.5), indentAfterHeading: false, // the indent is on the right
  avoidWidows: true, avoidOrphans: true, avoidRunts: true,
};

用料

类型
Markazi Text, Reem Kufi, Noto Kufi Arabic(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 一个语言标记,三项决定

代码见上文的简短回答。配置里只写了locale: 'ar',引擎从中得出三件事:文字从右向左排(direction: 'auto'),书在右侧装订(page.binding: 'auto'),引擎生成的所有数字,包括页码,都用阿拉伯-印度数字(numerals: 'auto')(开始一本阿拉伯文书)。页边距和对齐仍然写left和right,但在从右向左的书里它们指排版流的两侧:margins.left是订口一侧,首行缩进落在右边。两端对齐的行先放宽词间空格,再用tatweel拉长相连的字母,也就是阿拉伯图书里的卡希达;正文从不断词加连字符(两端对齐与卡希达)。

第12页:用卡希达撑满的两端对齐行,右侧的首行缩进,外侧的页码١٢。

#2 · 用词语写的章序

script.js · 第54–75行在完整代码中
const SINK = 58; // mm: where the text starts on an opener
const centred = (y, size) => ({ anchor: { to: 'container', edge: 'top' },
  offset: { y: mm(y) }, ...(size && { size }) });
const opener = {
  enabled: true, minHeight: mm(SINK),
  slot: { elements: [
    { kind: 'image', id: 'star', resourceId: 'star', placement: centred(8, { width: mm(9) }) },
    // {number} is the level's numbering template below: الفصل الأول, الفصل الثاني…
    { kind: 'text', id: 'kicker', content: '{number}', fontFamily: LABEL, fontSize: pt(10),
      fontWeight: 600, color: col('door'), align: 'center', placement: centred(22) },
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: DISPLAY, fontSize: pt(30),
      fontWeight: 600, lineHeight: 1.3, color: col('ink'), align: 'center', overflow: 'wrap',
      placement: centred(29, { width: 'fill' }) },
  ] },
};
const chapter = {
  level: 1, fontFamily: DISPLAY, fontWeight: 600, marginTop: pt(0), marginBottom: pt(0),
  // Each chapter opens a page, on either side of the spine.
  breakBefore: { enabled: true, parity: 'any' },
  // A masculine ordinal in words: {1:ordinal-feminine} would give الأولى, الثانية…
  numberingTemplate: 'الفصل {1:ordinal}', advancedDesign: opener,
};

这一级的numberingTemplate写出الفصل,再把序号写成阳性序数词,所以第二章是الفصل الثاني,Markdown不用改;遇到الليلة这样的阴性名词,{1:ordinal-feminine}给出الأولى、الثانية(序数词与基数词)。章首设计用{number}把它印在标题上方。阿拉伯文的章首通常居中,align: 'center'也省去了设计从哪一侧开始的问题。

#3 · 书眉留在自己的页面上

script.js · 第79–96行在完整代码中
// Running heads and folios keep their physical sides in a right-to-left book. The right page
// is an even page here and is read first: the book's title; the left page, the chapter's.
const label = { fontFamily: LABEL, fontSize: pt(8), color: col('muted'), pages: 'body' };
const at = (edge, x, y = 12) => ({ anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(y) } });
const folio = { ...label, fontWeight: 600, color: col('ink') };
const header = { elements: [
  { kind: 'text', id: 'r-folio', content: '{pageNumber}', parity: 'even', ...folio,
    align: 'right', placement: at('top-right', -OUTER) },
  { kind: 'text', id: 'r-title', content: '{title}', parity: 'even', ...label, align: 'right',
    placement: at('top-right', -(OUTER + 9)) },
  { kind: 'text', id: 'l-chapter', content: '{chapterTitle}', parity: 'odd', ...label,
    align: 'left', placement: at('top-left', OUTER + 9) },
  { kind: 'text', id: 'l-folio', content: '{pageNumber}', parity: 'odd', ...folio,
    align: 'left', placement: at('top-left', OUTER) },
] };
// The openers carry no head: their folio drops to the foot, centred.
const footer = { elements: [{ kind: 'text', id: 'drop-folio', content: '{pageNumber}', ...folio,
  pages: 'opener', align: 'center', placement: at('bottom', 0, -11) }] };

正文跟随排版流,书眉和页码却保持物理位置。第12页是偶数页,在这本书里是右页,所以页码在右上角,书名在它旁边;第13页在左边,书眉是章名。像阿拉伯读者那样从右往左读一个跨页,先读到的是书名。showBook把页面镜像摆放,第11页在第10页左边,屏幕上的跨页和书翻开时一样。

#4 · 用代码画的部扉页

script.js · 第100–120行在完整代码中
// # الدرب {style="plate" kicker="…" line="…"}: a heading that paints a whole page.
const onPage = (y) => ({ anchor: { to: 'page', edge: 'top' }, offset: { y: mm(y) } });
const plate = {
  id: 'plate', span: 'page', numbered: false, runningChapter: false, toc: false,
  breakBefore: { enabled: true, parity: 'any' }, // (gotcha: style-inherits-break)
  header: { elements: [] }, footer: { elements: [] },
  advancedDesign: { enabled: true, slot: { elements: [
    // The drawing carries 3 mm of bleed a side: a box of the trim would fit it by its width
    // and leave a strip of paper at the head and the foot.
    { kind: 'image', id: 'field', resourceId: 'lattice', placement: {
      anchor: { to: 'page', edge: 'top-left' }, offset: { x: mm(-3), y: mm(-3) },
      size: { width: mm(TRIM.width + 6), height: mm(TRIM.height + 6) } } },
    { kind: 'text', id: 'part', content: '{attr.kicker}', fontFamily: LABEL, fontSize: pt(11),
      fontWeight: 600, color: col('brass'), align: 'center', placement: onPage(76) },
    { kind: 'text', id: 'name', content: '{titleText}', fontFamily: DISPLAY, fontSize: pt(64),
      fontWeight: 600, lineHeight: 1.2, color: col('paper'), align: 'center',
      placement: onPage(86) },
    { kind: 'text', id: 'line', content: '{attr.line}', fontFamily: TEXT, fontSize: pt(13),
      color: col('paper'), align: 'center', placement: onPage(124) },
  ] } },
};

部扉页是一个一级标题# الدرب {style="plate" …},它的样式画满整张页面,并去掉书眉和页码。格纹是脚本里生成的SVG:两个正方形相错45°,就是伊斯兰几何纹样里的八角星(khātam),颜色取调色板里的蓝和铜,所以PDF把它保留为矢量路径。numbered: false让部扉页不计入章序,后面的章首仍是الفصل الأول。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 105 · An Arabic novel set right to left, bound on the right ═══
// https://postext.dev/en/cookbook/arabic-novel-right-bound
// Code: MIT · Text: original Arabic prose (CC BY 4.0) · Pattern: drawn in code
// Fonts: Markazi Text, Reem Kufi, Noto Kufi Arabic (SIL OFL 1.1) · Needs postext ≥ 1.25.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 frame; the novel is Arabic in both editions
const RECIPE = 'arabic-novel-right-bound';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// The blue of the press's door, the brass of its key, ink on a cream book paper.
// #region palette: the door's blue, the key's brass, every colour linked by id
const palette = {
  ink: '#1f1c19', // text: a warm near-black
  door: '#2b4766', // the accent: the part page's field, the openers' kickers
  brass: '#b08d4a', // the second accent: the medallion's line and the openers' star
  lattice: '#3d5d82', // the pattern's lines, a step lighter than the field
  rule: '#cbc3b5', // hairlines
  muted: '#6b645b', // 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, 'main-color': palette.door })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const [TEXT, DISPLAY, LABEL] = ['Markazi Text', 'Reem Kufi', 'Noto Kufi Arabic'];
const [BODY, LEAD] = [13.5, 22]; // pt: Naskh sets small, so 13.5 pt on 1.63 × the size
const TRIM = { width: 140, height: 210 }; // mm: 14 × 21 cm, the Arab trade novel
const OUTER = 16; // mm: the outer margin, where the folios stand

// #region answer: the locale sets the direction, the binding and the digits
const page = {
  width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, backgroundColor: col('paper'),
  // left and right are the flow's sides: in a right-to-left book left is the spine side,
  // so these margins put 20 mm at the spine and 16 mm outside on both pages of a spread.
  margins: { top: mm(23), bottom: mm(22), left: mm(20), right: mm(OUTER), mirror: true },
  // binding: 'auto' (unset) is 'right' for a right-to-left locale: page 1 is a left page.
};
const bodyText = {
  fontFamily: TEXT, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  // justify ends every line on both sides; the engine lengthens joined letters (kashida,
  // on by default for Arabic) after it opens the spaces a quarter. Never hyphenated.
  textAlign: 'justify', optimalLineBreaking: true,
  firstLineIndent: em(1.5), indentAfterHeading: false, // the indent is on the right

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

变化

#印欧洲数字

马格里布版本印1、2、3;locale: 'ar-MA'本身就会这样做,马格里布版本展示整本书。

-  locale: 'ar', // written out, never LANG (gotcha: arabic-locale-tag)
+  locale: 'ar', numerals: 'latn',

#只用空格两端对齐

有些出版社的正文里不用tatweel:差额全部由空格承担。

   textAlign: 'justify', optimalLineBreaking: true,
+  kashida: 'none',

常见问题

易错点

阿拉伯文书标记为'ar',不要用LANG

食谱的版本是en和es,但阿拉伯文示例在两个版本里都是阿拉伯文:`locale: LANG`会让它从左向右排、左装订、页码印成1 2 3,并把图标注为Figure或Figura。postext 1.30起,这个标记也是文字成形所用的语言(测量、绘制和嵌入都按它进行),所以标记成英文或西班牙文时,Markazi Text这样的字体不画尖角引号(« »)和句点的阿拉伯文字形,断行的位置也随之改变。自己写出标记:'ar'(阿拉伯-印度数字,马什里克惯例),或带地区的'ar-EG'、'ar-SA';马格里布版本用欧洲数字,写'ar-MA'、'ar-DZ'或'ar-TN'。只引用阿拉伯文的拉丁文页面保留自己的locale。 从右向左的文字 →

易错点

阿拉伯文字体需要通过arabic块加载其arabic文件

Fontsource把Amiri、Noto Naskh Arabic或Scheherazade New按子集分成多个文件提供,阿拉伯字母在arabic文件里。loadFonts只获取latin(以及latin-ext),所以屏幕上的阿拉伯文来自系统字体,测量不准;fontsourceProvider交给PDF的也是这个latin文件,印出来是空框。列出kit中的arabic块,在loadFonts之后调用loadArabicFonts(FONTS, markdown),并在FONTS中列出页面用于阿拉伯文的每个字重,再给renderToPdf传fontProvider: arabicPdfProvider。 阿拉伯文字体 →

易错点

右装订的书用showBook显示跨页

在右装订的书中(阿拉伯文、希伯来文或波斯文,竖排中文,或page.binding 'right'),第1页仍是奇数页(recto),但它位于书脊左侧,跨页读作[3 | 2]。showPages把所有书都按左装订排列;book块中的showBook(cjk块中也有同一个函数)读取doc.binding,把跨页左右镜像。capture.hero仍按阅读顺序[偶数页, 奇数页]指定跨页:[2, 3]。 右翻书 →

易错点

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

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

  • 阿拉伯文书末尾的英文或西班牙文版权说明,块上要加{dir=ltr}:不加的话,这行拉丁文会按从右向左的段落对齐和标点,句号跑到左边。

致谢

文本
  • The opening of a novel, حروف من رصاص (Letters of Lead), written in Arabic for the recipe; its author and its people are invented · Postext Cookbook · 原创
字体
Markazi Text (SIL OFL 1.1) · Reem Kufi (SIL OFL 1.1) · Noto Kufi Arabic (SIL OFL 1.1)
沙盒PDF