انتقل إلى المحتوى الرئيسي
الوصفة رقم 148

دليل الوصفات · الفصل 11 · القصص المصوّرة والمانغا

كتاب EPUB مصوّر يُقرأ إطارًا إطارًا

قصة مصوّرة قصيرة تُصدَّر ملف PDF، وكتاب EPUB بتخطيط ثابت مع ملاحة بالمناطق للقراءة إطارًا إطارًا، وكتاب EPUB مع Kindle Panel View، وكتاب EPUB مرن.

في هذه الصفحة
المُخرَج
Canvas · PDF · EPUB
المستوى
متوسط
Postext
اختُبرت مع Postext 1.65.0
تتطلب ≥ 1.65.0 · postext-pdf ≥ 1.65.0
الترخيص
حُدّثت في 31 أكتوبر 2026
الشيفرة MIT · النص CC BY 4.0

ص ٢–٣ · 2–3 من 3

  • مقاس القص 168 × 259 مم
  • عمود واحد
  • Playpen Sans Arabic 10.5/15
  • Lalezar
  • 3 صفحات
  • المستوى
  • Postext 1.65.0
  • أُخرجت في 265 ms
  • أسطر الشيفرة: 184

باختصار

قصة مصوّرة من صفحتين تُحفظ ملف PDF للطباعة وكتبًا إلكترونية: واحد يحتفظ بالصفحات ويسمح بالتنقّل من إطار إلى إطار، وآخر لكيندل، وثالث يجعل الحوار نصًّا.

ما الذي ستنضده

الفصل الأول من منارة بورثكوف: صفحة عنوان وصفحتان مصوّرتان، تُنضّد مرة واحدة وتُصدَّر بأربع طرق. ملف PDF للطباعة. وكتاب EPUB بتخطيط ثابت يحتفظ بالصفحات المطبوعة ويتضمّن ملاحة بالمناطق، فيستطيع نظام القراءة أن يعرض الكتاب إطارًا إطارًا، مكبّرًا من إطار الوصول إلى المنارة إلى المذياع ثم إلى مايا. وكتاب EPUB ثابت ثانٍ يضيف Kindle Panel View، أما الكتاب القابل لإعادة التدفق فينضّد كل إطار صورتَه المقصوصة يتبعها حواره. وتعرض الصفحة كل ملف كما سيفتحه القارئ، مع رابط تنزيله، بعشر لغات.

تجيب هذه الوصفة عن

  • كيف أصنع EPUB لقصة مصورة يُقرأ إطارًا إطارًا؟

الجواب المختصر

script.js · الأسطر 264–279في الشيفرة الكاملة
// The fixed layout keeps every printed page and writes regions.xhtml (EPUB Region-Based
// Navigation): one panel region per panel in reading order, its balloons nested in it.
// kindlePanelView adds Kindle's tap-to-magnify targets; the reflowable book sets each panel
// as its cropped picture followed by its lines as text.
const book = {
  metadata: { title: doc.metadata.title, creators: [doc.metadata.author], language: config().locale,
    modified: new Date('2026-10-07T00:00:00Z') }, // a fixed date: the same bytes on every run
  fonts: await epubFonts([LETTERING, SFX], markdown), // every face the pages set, as bytes
  resourceBytes: (fileId) => ({ bytes: imageBytes(fileId), mediaType: 'image/jpeg' }),
  onWarning: (w) => console.warn(`[epub] ${w.kind}`, w.family ?? w.fileId ?? w.detail),
};
const epubs = {
  fixed: await renderToEpub(doc, { ...book, layout: 'fixed' }),
  kindle: await renderToEpub(doc, { ...book, layout: 'fixed', kindlePanelView: true }),
  reflowable: await renderToEpub(doc, { ...book, layout: 'reflowable' }),
};

المكونات

الخطوط
Comic Neue, Bangers, Zen Antique, Dela Gothic One, ZCOOL KuaiLe, ZCOOL QingKe HuangYou, Playpen Sans Arabic, Lalezar (SIL OFL 1.1)
الأصول
  • lh-arrive.jpg
  • lh-beam.jpg
  • lh-biscuit.jpg
  • lh-maya.jpg
  • lh-radio.jpg
  • Panel picture: Maya arrives at the lighthouse (Generated With Diffusion Models, أصلي)
  • Panel picture: Tomás at the radio (Generated With Diffusion Models, أصلي)
  • Panel picture: Maya finds the problem (Generated With Diffusion Models, أصلي)
  • Panel picture: Biscuit asleep on the cable (Generated With Diffusion Models, أصلي)
  • Panel picture: the lamp lit at night (Generated With Diffusion Models, أصلي)

طريقة التحضير

#1 · صدّر المستند نفسه إلى EPUB

الشيفرة هي الجواب المختصر في الأعلى. يأخذ renderToEpub المستندات التي يطبعها renderToPdf، فصفحات EPUB هي صفحات PDF نفسها (إخراج القصص المصورة). ومع layout: 'fixed' ووجود صفحات مصوّرة في الكتاب، يكتب أيضًا regions.xhtml: منطقة panel لكل إطار بترتيب القراءة، كلٌّ منها مستطيل بالنسب المئوية من صفحته، وفقاعاته ومربعات السرد فيه ومؤثراته الصوتية مناطق متداخلة داخله. ويتبع اتجاه تقليب الصفحات اتجاه قراءة القصة، فتُقلّب الطبعتان اليابانية والعربية من اليمين إلى اليسار. ويضيف kindlePanelView: true منطقة لمس لكل إطار ونسخة مكبّرة، مع البيانات الوصفية التي يطلبها كيندل. أما النسخة القابلة لإعادة التدفق فلا صفحات لها تحتفظ بها: كل إطار هو صورته، مقصوصة كما في الصفحة، ثم حواره، «مايا: بسكويت! أين جدّي؟»، والأسماء من comics.cast.

الإطار الأول في الصفحة ٢: أول منطقة في القراءة الموجّهة، والتعليق وفقاعة مايا داخلها.

#2 · ضمّن الخطوط التي تستعملها الصفحات

script.js · الأسطر 210–239في الشيفرة الكاملة
// For each weight and style the lettering asks for, the file loadComicFonts chose (the
// nearest one Fontsource ships), only the slices whose characters the book sets. A file that
// serves several weights is embedded once, with a weight range ('400 700').
const inRange = (range, cp) => range.split(',').some((part) => {
  const [lo, hi = lo] = part.trim().slice(2).split('-');
  return cp >= parseInt(lo, 16) && cp <= parseInt(hi, 16);
});
async function epubFonts(families, text) {
  const cps = [...new Set(text)].map((ch) => ch.codePointAt(0));
  const byFile = new Map(); // url → the face it serves
  for (const family of families) {
    const meta = await fontsourceMeta(family);
    const asks = [[400, 'normal'], [700, 'normal'], [400, 'italic'], [700, 'italic']];
    for (const [weight, style] of asks) {
      const w = meta.weights.reduce((a, b) => (Math.abs(b - weight) < Math.abs(a - weight) ? b : a),
        400);
      const s = style === 'italic' && meta.styles.includes('italic') ? 'italic' : 'normal';
      for (const { url, range } of await comicFaceFiles(family, w, s, meta, text)) {
        if (range && !cps.some((cp) => inRange(range, cp))) continue;
        const face = byFile.get(url) ?? { family, style: s, weights: new Set(), range };
        face.weights.add(weight);
        byFile.set(url, face);
      }
    }
  }
  return Promise.all([...byFile].map(async ([url, { family, style, weights, range }]) => ({
    family, style, format: 'woff2', unicodeRange: range,
    weight: weights.size > 1 ? `${Math.min(...weights)} ${Math.max(...weights)}` : [...weights][0],
    bytes: new Uint8Array(await (await fetch(url)).arrayBuffer()) })));
}

يرسم كتاب EPUB الثابت نصّه بالخطوط التي يتضمّنها، والخط الغائب يُستبدل بخط القارئ. يطلب الحوار العادي والعريض والمائل، والخطوط المصوّرة تأتي بأقل من ذلك، فيأخذ كل طلب الملف الذي اختاره loadComicFonts، والملف الذي يخدم الوزنين يُضمَّن مرة واحدة بالمدى '400 700'. ولا يُضمَّن من الخطوط اليابانية والصينية إلا الأجزاء التي فيها حروف طبعتها، فيبقى الكتاب الياباني في ١٫٤ ميغابايت.

#3 · اقرأ كل ملف من جديد

script.js · الأسطر 283–304في الشيفرة الكاملة
const el = (tag, css, text) => Object.assign(document.createElement(tag),
  { textContent: text ?? '' }, { style: css });
const shelf = el('section', 'display: flex; flex-wrap: wrap; gap: 16px; justify-content: center;'
  + 'padding: 24px 16px 0; color: #d9d5cc; font: 13px/1.5 system-ui, sans-serif');
for (const [name, bytes] of Object.entries(epubs)) {
  const epub = readEpub(bytes);
  const regions = new TextDecoder().decode(epub.files.get(`${epub.root}regions.xhtml`)
    ?? new Uint8Array());
  const panels = regions.split('<li epub:type="panel">').slice(1); // one per panel
  const card = el('article', 'width: min(300px, 92vw); padding: 16px; border: 1px solid #2c3038');
  const steps = el('ol', 'margin: 8px 0; padding-left: 22px');
  steps.append(...panels.map((p, i) => el('li', '', `panel ${i + 1}: ${(p.match(
    /epub:type="(balloon|caption|sound-area|text-area)"/g) ?? []).length} text regions`)));
  const link = Object.assign(el('a', 'color: #d8a21a', `Download ${RECIPE}-${name}.epub`), {
    href: URL.createObjectURL(new Blob([bytes])), download: `${RECIPE}-${name}.epub` });
  card.append(el('h2', 'margin: 0; font-size: 15px; color: #f4f1ea', `EPUB 3 · ${name}`),
    el('p', 'margin: 4px 0 0', `${epub.layout} · ${epub.pageProgression} · `
      + `${epub.spine.length} documents · ${Math.round(bytes.length / 1024)} KB`),
    panels.length ? steps : el('p', '', 'No region navigation: the panels are text.'), link);
  shelf.append(card);
}
document.getElementById('pages').before(shelf);

يفتح readEpub البايتات كما يفعل نظام القراءة: التخطيط، واتجاه التقليب، والعمود الفقري. والقائمة تحت كل بطاقة هي القراءة الموجّهة نفسها، مقروءة من regions.xhtml: خمسة إطارات ومناطقها النصية، بالترتيب الذي يمرّ به القارئ. وتجتاز الملفات الثلاثة EPUBCheck 5.4 بلا أخطاء ولا تحذيرات بالإنجليزية واليابانية والعربية.

#4 · تنضيد واحد وعشر طبعات

script.js · الأسطر 36–41في الشيفرة الكاملة
const [LETTERING, SFX] = t({
  en: ['Comic Neue', 'Bangers'], es: ['Comic Neue', 'Bangers'], ca: ['Comic Neue', 'Bangers'],
  ja: ['Zen Antique', 'Dela Gothic One'], zh: ['ZCOOL KuaiLe', 'ZCOOL QingKe HuangYou'],
  ar: ['Playpen Sans Arabic', 'Lalezar'], pt: ['Comic Neue', 'Bangers'],
  fr: ['Comic Neue', 'Bangers'], de: ['Comic Neue', 'Bangers'], it: ['Comic Neue', 'Bangers'],
});

يتضمّن كل content.<lang>.md كلمات طبعة واحدة بمعرّفات الشخصيات نفسها. مناطق الإطارات واحدة في كل اللغات، أما مناطق الفقاعات فتتحرك معها، لأنها تُكتب من جديد حول نص كل لغة.

الوصفة كاملة

Sandbox
// ═══ Postext Cookbook · Nº 148 · A comic EPUB read panel by panel ═══════════════════
// https://postext.dev/en/cookbook/comic-epub-guided-view
// Code: MIT · Text: original (CC BY 4.0) · Pictures: generated with diffusion models
// Fonts: Comic Neue, Bangers and the faces of five editions (OFL) · Needs postext ≥ 1.65.0
//
// A short comic laid out once and exported four ways: a PDF for print, a fixed-layout EPUB
// whose region-based navigation lets a reading system step through it panel by panel, the
// same EPUB with Kindle Panel View, and a reflowable EPUB with each panel's lines as text.
// Each file is read back to list what a reader will find in it.
import {
  buildDocument, withLoadedFonts, renderPageToCanvas, registerResourceImage, loadVerticalAlternates,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';
import { renderToEpub, readEpub } from 'https://esm.sh/postext-epub';

const LANG = 'ar'; // @lang: 'en' | 'es' | 'ca' | 'zh' | 'ar' | 'ja' | 'pt' | 'fr' | 'de' | 'it'
const RECIPE = 'comic-epub-guided-view';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: the night sea, the lighthouse red and the yellow of the captions
const palette = {
  ink: '#1b2430', // panel borders, lettering and the title page
  accent: '#b8322a', // the lighthouse red: title and sound effects
  caption: '#f4e7bf', // caption boxes
  muted: '#5f6873', // folios and the imprint
  paper: '#fdfbf6',
};
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: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// #endregion

// #region editions: six editions, each lettered in faces for its script
const [LETTERING, SFX] = t({
  en: ['Comic Neue', 'Bangers'], es: ['Comic Neue', 'Bangers'], ca: ['Comic Neue', 'Bangers'],
  ja: ['Zen Antique', 'Dela Gothic One'], zh: ['ZCOOL KuaiLe', 'ZCOOL QingKe HuangYou'],
  ar: ['Playpen Sans Arabic', 'Lalezar'], pt: ['Comic Neue', 'Bangers'],
  fr: ['Comic Neue', 'Bangers'], de: ['Comic Neue', 'Bangers'], it: ['Comic Neue', 'Bangers'],
});
// #endregion

const comics = {
  gutter: { horizontal: mm(4), vertical: mm(3) },
  panel: { borderWidth: pt(1), borderColor: col('ink'), background: col('paper') },
  lettering: { fontFamily: LETTERING, fontSize: pt(8), color: col('ink'), inset: mm(1.5) },
  balloonStyles: [{ id: 'speech', stroke: col('ink') }, { id: 'caption', fill: col('caption'),

يعمل ملف script.js المجمّع كما هو: الصقه في سكربت الوحدة (module) لأي صفحة، أو افتح الوصفة على CodePen. مجلد الوصفة على GitHub ↗ (يفتح في تبويب جديد)

تنويعات

#استغنِ عن طبعة كيندل

الملاحة بالمناطق تُكتب في كل EPUB ثابت؛ وKindle Panel View يضاف إليها فقط.

-  kindle: await renderToEpub(doc, { ...book, layout: 'fixed', kindlePanelView: true }),

#أعطِ الكتاب المرن غلافًا

-  reflowable: await renderToEpub(doc, { ...book, layout: 'reflowable' }),
+  reflowable: await renderToEpub(doc, { ...book, layout: 'reflowable',
+    cover: { bytes: imageBytes('lh-beam.jpg'), mediaType: 'image/jpeg' } }),

أخطاء شائعة

  • الخط المضمَّن مرة لكل وزن يخدمه يُنسخ أربع مرات؛ كان وزن الطبعة الصينية ٥٫٣ ميغابايت قبل مدى الأوزان.

الحقوق

الوصفة
Ignacio Ferro
النص
نص أصلي, CC BY 4.0
الصور
  • Panel picture: Maya arrives at the lighthouse · Generated With Diffusion Models · أصلي
  • Panel picture: Tomás at the radio · Generated With Diffusion Models · أصلي
  • Panel picture: Maya finds the problem · Generated With Diffusion Models · أصلي
  • Panel picture: Biscuit asleep on the cable · Generated With Diffusion Models · أصلي
  • Panel picture: the lamp lit at night · Generated With Diffusion Models · أصلي
الخطوط
Comic Neue (SIL OFL 1.1) · Bangers (SIL OFL 1.1) · Zen Antique (SIL OFL 1.1) · Dela Gothic One (SIL OFL 1.1) · ZCOOL KuaiLe (SIL OFL 1.1) · ZCOOL QingKe HuangYou (SIL OFL 1.1) · Playpen Sans Arabic (SIL OFL 1.1) · Lalezar (SIL OFL 1.1)
الشيفرة
MIT، مثل Postext
SandboxPDF