跳到主要内容
食谱编号109

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

印欧洲数字的马格里布版阿拉伯文书

为摩洛哥印制的阿拉伯文书中的一章:locale 'ar-MA'保持从右向左和右侧装订,并把引擎生成的每个数字写作1、2、3。

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

页码41 · 第1页,共4页

  • 英文样例:尚无中文版本
  • 成品尺寸135 × 210 mm
  • 1栏
  • Noto Naskh Arabic 12.5/20.5
  • Noto Kufi Arabic
  • 4页
  • 难度
  • Postext 1.65.0
  • 排版用时106 ms
  • 88行代码

简单来说

在摩洛哥、阿尔及利亚和突尼斯,阿拉伯文图书用欧洲通行的1、2、3印数字,而不是١、٢、٣。只要设定文档的语言和国家,整本书就用这套数字。

成品一览

一本摩洛哥古城指南中写非斯城门的一章,按摩洛哥出版社的样子排:13.5 × 21 cm,Noto Naskh Arabic 12.5 pt,标题用Noto Kufi Arabic,颜色取非斯瓷砖的绿色。文字从右向左,书在右侧装订,和所有阿拉伯文书一样,但页面上的每个数字都写作1、2、3:页码41到44,الفصل 3,小节号3-1和3-2,城门列表1-到5-,脚注标记(1),还有插图编号شكل 3-1。这是从摩洛哥到突尼斯的马格里布用法,页面只用一项设置就做到了。右侧装订的阿拉伯文小说展示了用东部数字١٢的同类书。

这道食谱解答

  • 马格里布版本的阿拉伯文书怎样印欧洲数字(1、2、3)?

简短回答

script.js · 第40–67行在完整代码中
const config = () => ({
  // Written out, never LANG (gotcha: arabic-locale-tag). The Moroccan tag keeps the text right
  // to left and the binding on the right, and sets numerals: 'auto' to European digits: the
  // folios, the chapter and section numbers, the list, the notes and the figure number.
  locale: 'ar-MA',
  colorPalette,
  page: { width: mm(135), height: mm(210), dpi: 150, pageNumbering: { startAt: 41 },
    margins: { top: mm(22), bottom: mm(22), left: mm(19), right: mm(16), mirror: true } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: TEXT, fontSize: pt(12.5), lineHeight: pt(20.5), color: col('ink'),
    boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
    textAlign: 'justify', firstLineIndent: em(1.5), indentAfterHeading: false },
  headings: { fontFamily: LABEL, fontWeight: 700, color: col('green'), levels: [
    // A chapter opens on an odd page, with no blank page forced before it.
    { level: 1, fontSize: pt(26), lineHeight: pt(41), numberingTemplate: 'الفصل {1}',
      numberSeparator: ': ', breakBefore: { enabled: true, parity: 'odd' },
      marginTop: pt(0), marginBottom: pt(10) },
    { level: 2, fontSize: pt(13), lineHeight: pt(20.5), numberingTemplate: '{1}-{2}',
      numberSeparator: '  ', marginTop: pt(20.5), marginBottom: pt(0) },
  ] },
  orderedLists, footnotes, captionStyle,
  paragraphStyles: [{ id: 'colophon', fontFamily: TEXT, fontSize: pt(8.5), lineHeight: pt(11),
    color: col('muted'), textAlign: 'left', firstLineIndent: pt(0), marginTop: pt(20.5) }],
  header: { elements: [] },
  footer: { elements: [{ kind: 'text', id: 'folio', content: '{pageNumber}', fontFamily: LABEL,
    fontSize: pt(9), color: col('muted'), align: 'center',
    placement: { anchor: { to: 'page', edge: 'bottom' }, offset: { y: mm(-12) } } }] },
});

用料

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

做法

#1 · 国家决定数字

代码见上文的简短回答。numerals保持'auto',引擎从语言标记中读出数字系统:ar-MA、ar-DZ、ar-TN、ar-LY、ar-MR和ar-EH用欧洲数字,单独的ar和马什里克地区用١٢٣(数字系统与编号样式)。改变的只是引擎生成的数字:页码、标题计数、列表编号、脚注标记和插图编号。正文里的年份789和1913是作者按本版的数字写下的,引擎从不改写。标题上的{startAt=3}既认3也认٣,所以同一份Markdown可以用于两个版本。

#2 · 数字周围的符号

script.js · 第29–36行在完整代码中
// «1-» and «(1)», as Arabic books write them; the number itself follows numerals.
const orderedLists = { color: col('green'), fontWeight: 700, marginTop: pt(0),
  marginBottom: pt(0), levels: [{ level: 1, numberFormat: 'arabic', separator: '-' }] };
const footnotes = { markerTemplate: '({n})', numbering: 'page', noteNumberPosition: 'inline',
  fontSize: pt(10), lineHeight: pt(15), separator: { width: 0.3, color: col('green') } };
// A colon after the label: a full stop after a number reads as a decimal point.
const captionStyle = { fontFamily: TEXT, fontSize: pt(10), color: col('ink'), labelBold: true,
  labelColor: col('green'), labelSeparator: ': ', note: { color: col('muted') } };

阿拉伯文图书的列表编号后面跟一个连字符,写作1-,脚注标记加圆括号,写作(1);两个模板都采用文档的数字系统,所以马什里克版用同样的设置印出١-和(١)(脚注)。题注标签以冒号结尾,写作شكل 3-1:,因为数字后的句点会被读成小数点。脚注在每一页都从(1)重新编号,这是多数阿拉伯文图书的做法。

#3 · 用代码画的瓷砖条

script.js · 第120–148行在完整代码中
// The star is two squares turned 45°; between four stars a small square. Paths only.
const star = (cx, cy, r) => {
  const pts = [];
  for (let k = 0; k < 16; k++) {
    const a = (k * Math.PI) / 8;
    const d = k % 2 ? r * 0.7654 : r; // where the two squares' sides cross
    pts.push(`${(cx + d * Math.sin(a)).toFixed(2)} ${(cy - d * Math.cos(a)).toFixed(2)}`);
  }
  return `M${pts.join('L')}Z`;
};
function zellige(W, H, S) {
  let stars = '';
  let inner = '';
  let squares = '';
  for (let y = 0; y <= H + S; y += S) {
    for (let x = 0; x <= W + S; x += S) {
      stars += star(x, y, S * 0.47);
      inner += star(x, y, S * 0.2);
      squares += `M${x + S / 2} ${y + S / 2 - S * 0.17}l${S * 0.17} ${S * 0.17}`
        + `l${-S * 0.17} ${S * 0.17}l${-S * 0.17} ${-S * 0.17}Z`;
    }
  }
  return `<svg xmlns="http://www.w3.org/2000/svg" width="${W * 10}" height="${H * 10}" `
    + `viewBox="0 0 ${W} ${H}"><rect width="${W}" height="${H}" fill="${palette.green}"/>`
    + `<path d="${stars}" fill="${palette.cobalt}" stroke="${palette.paper}" stroke-width="0.5"/>`
    + `<path d="${inner}" fill="${palette.paper}"/>`
    + `<path d="${squares}" fill="${palette.ochre}" stroke="${palette.paper}" stroke-width="0.4"/>`
    + '</svg>';
}

插图是脚本里生成的SVG:用两个相错45°的正方形组成八角星,星与星之间是小方块,颜色取调色板里的绿、钴蓝和赭色。两个版本的题注都是阿拉伯文,按章编号{h1}-{n},这是阿拉伯文资源类型给插图编号的方式。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 109 · A Maghreb edition of an Arabic text, with European digits ═══
// https://postext.dev/en/cookbook/maghreb-edition-european-digits
// Code: MIT · Text: original Arabic prose (CC BY 4.0) · Pattern: drawn in code
// Fonts: Noto Naskh Arabic, Noto Kufi Arabic (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 frame; the chapter is Arabic in both editions
const RECIPE = 'maghreb-edition-european-digits';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// The green and cobalt of Fes tilework, on a white book paper.
const palette = {
  ink: '#1c1d1b', // text
  green: '#1d6650', // the accent: headings, the list numbers, the notes' rule
  cobalt: '#25488a', // the tile's second colour
  ochre: '#c99a3e', // the tile's third colour
  muted: '#62655f', // folios, captions' notes, the colophon
  paper: '#ffffff',
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = Object.entries({ ...palette, 'main-color': palette.green })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
const [TEXT, LABEL] = ['Noto Naskh Arabic', 'Noto Kufi Arabic'];

// #region marks: list numbers, note markers and figure numbers in the same digits
// «1-» and «(1)», as Arabic books write them; the number itself follows numerals.
const orderedLists = { color: col('green'), fontWeight: 700, marginTop: pt(0),
  marginBottom: pt(0), levels: [{ level: 1, numberFormat: 'arabic', separator: '-' }] };
const footnotes = { markerTemplate: '({n})', numbering: 'page', noteNumberPosition: 'inline',
  fontSize: pt(10), lineHeight: pt(15), separator: { width: 0.3, color: col('green') } };
// A colon after the label: a full stop after a number reads as a decimal point.
const captionStyle = { fontFamily: TEXT, fontSize: pt(10), color: col('ink'), labelBold: true,
  labelColor: col('green'), labelSeparator: ': ', note: { color: col('muted') } };
// #endregion

// #region answer: ar-MA prints 1, 2, 3 in every number the engine writes
const config = () => ({
  // Written out, never LANG (gotcha: arabic-locale-tag). The Moroccan tag keeps the text right
  // to left and the binding on the right, and sets numerals: 'auto' to European digits: the
  // folios, the chapter and section numbers, the list, the notes and the figure number.
  locale: 'ar-MA',
  colorPalette,
  page: { width: mm(135), height: mm(210), dpi: 150, pageNumbering: { startAt: 41 },
    margins: { top: mm(22), bottom: mm(22), left: mm(19), right: mm(16), mirror: true } },
  layout: { layoutType: 'single' },

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

变化

#印马什里克数字

同一章的埃及或黎凡特版本:只改语言标记,不改Markdown。

-  locale: 'ar-MA',
+  locale: 'ar-EG',

#保留摩洛哥语言标记,但印١٢٣

明确写出的设置优先于地区默认值。

   locale: 'ar-MA',
+  numerals: 'arab',

常见问题

易错点

阿拉伯文书标记为'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。 阿拉伯文字体 →

易错点

段首的'1998. '或'- '会开始一个列表

以数字、句点和空格开头,或以连字符和空格开头的段落,会变成列表项。在数字前放一个连接符(U+2060),对话用长破折号来写。 转义与字面字符 →

致谢

文本
  • “The gates of Fes”, a chapter written in Arabic for the recipe, from an invented book on the old cities of Morocco · Postext Cookbook · 原创
字体
Noto Naskh Arabic (SIL OFL 1.1) · Noto Kufi Arabic (SIL OFL 1.1)
沙盒PDF