From 29d299da76cb0dcd210100950150ee103fd2e41e Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 22:18:35 +0900 Subject: [PATCH 1/7] Write page numbers, page-break lines and tabbed list numbers in built-in TXT RTF, Word and ODT to TXT on the built-in route: - PAGE / NUMPAGES fields write the number the file showed when it was saved (Word's field result, ODT's text:page-number text, RTF's \fldrslt), kept as HField.Shown; without one, the page counted from the sections and page breaks. Before, the field came out empty. - A Word paragraph that holds only a page break is an empty line, as Word and LibreOffice save it. The DOCX reader still leaves it out of the layout and counts it on the next block (BreakParagraphsBefore). No form feed. - A list number or bullet is followed by a tab, or by the space or nothing a Word list level asks for (w:suff), as Word and LibreOffice write plain text. HWP/HWPX and e-book TXT use the same marker. - The built-in RTF, Word and ODT to TXT edges cost 15, below LibreOffice's fallback (25) with any engine order penalty, so they win wherever LibreOffice stands in the engine list. LibreOffice's separate RTF to TXT edge (10) goes. A Word 6.0 / 95 file still goes to LibreOffice. - ENGINES.md and the hwp-text engine description (all 23 languages) name RTF, Word and ODT to TXT. - bn-BD, fa, vi: the Linux LibreOffice guidance no longer mentions Java and H2Orestart. Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 23 +++- src/Filee.App/Assets/i18n/ar.json | 2 +- src/Filee.App/Assets/i18n/bn-BD.json | 4 +- src/Filee.App/Assets/i18n/de.json | 2 +- src/Filee.App/Assets/i18n/en.json | 2 +- src/Filee.App/Assets/i18n/es.json | 2 +- src/Filee.App/Assets/i18n/fa.json | 4 +- src/Filee.App/Assets/i18n/fr.json | 2 +- src/Filee.App/Assets/i18n/hi.json | 2 +- src/Filee.App/Assets/i18n/id.json | 2 +- src/Filee.App/Assets/i18n/it.json | 2 +- src/Filee.App/Assets/i18n/ja.json | 2 +- src/Filee.App/Assets/i18n/ko.json | 2 +- src/Filee.App/Assets/i18n/mn.json | 2 +- src/Filee.App/Assets/i18n/ne.json | 2 +- src/Filee.App/Assets/i18n/pl.json | 2 +- src/Filee.App/Assets/i18n/pt-BR.json | 2 +- src/Filee.App/Assets/i18n/pt-PT.json | 2 +- src/Filee.App/Assets/i18n/ru.json | 2 +- src/Filee.App/Assets/i18n/th.json | 2 +- src/Filee.App/Assets/i18n/tr.json | 2 +- src/Filee.App/Assets/i18n/vi.json | 4 +- src/Filee.App/Assets/i18n/zh-CN.json | 2 +- src/Filee.App/Assets/i18n/zh-TW.json | 2 +- src/Filee.Engines/Hwp/HwpTextConverter.cs | 23 ++-- src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs | 45 ++++--- src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs | 13 ++ src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs | 11 +- src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs | 111 ++++++++++++---- src/Filee.Engines/Hwp/Hwpx/RtfReader.cs | 21 ++- .../Office/LibreOfficeConverter.cs | 6 +- tests/Filee.Engines.Tests/EbookTests.cs | 2 +- .../FileeRendererConverterTests.cs | 53 ++++++-- tests/Filee.Engines.Tests/HwpTextTests.cs | 4 +- .../LibreOfficeFormatTests.cs | 5 +- .../LibreOfficeRouteTests.cs | 7 +- tests/Filee.Engines.Tests/RtfToTextTests.cs | 18 ++- tests/Filee.Engines.Tests/WordToTextTests.cs | 120 +++++++++++++++++- 38 files changed, 388 insertions(+), 126 deletions(-) diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 72f35b6f9..928517665 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -11,7 +11,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | ICNS | built in | images → ICNS (16–1024 px), ICNS → images | | PDFsharp | built in (library) | images → PDF (CMYK pictures stay CMYK), merge, split, page ranges, page organising, lossless rotate and crop-box edits, pages per sheet (N-up), spreadsheet print-studio assembly, **compression** (smaller PDFs, see below), PDFs without their metadata | | PDFium | built in (library) | PDF → images (multi-page TIFF/GIF supported); CMYK pages → CMYK JPG/TIFF through Ghostscript when it is installed | -| **HWP text** | built in | **HWP/HWPX → TXT, Markdown, HTML** with the [HWP](#hwp-reader) and [HWPX readers](#hwpx-reader) (pictures in a `_files` folder next to the Markdown, embedded in the HTML; see [HWP text, Markdown and HTML](#hwp-text-markdown-and-html)) | +| **HWP text** | built in | **HWP/HWPX → TXT, Markdown, HTML** with the [HWP](#hwp-reader) and [HWPX readers](#hwpx-reader) (pictures in a `_files` folder next to the Markdown, embedded in the HTML); **RTF, Word (DOCX + DOCM/DOTX/DOTM, DOC + DOT, WPS Office WPS) and ODT → TXT** a line per paragraph, as a word processor saves plain text (see [HWP text, Markdown and HTML](#hwp-text-markdown-and-html)) | | Text & Markdown (Markdig) | built in (library) | **Markdown → HTML, TXT**; **TXT, LOG, CXLOG, PY, PAC, BAT, CMD, COMMAND, LUA, CSS, PS1, SQL, AHK, INI, RSC, C, H, CPP, CC, CXX, HPP, HH, HXX, CS, DART, HQL, DBML, JAVA, INO, PDE → UTF-8 TXT**, preserving source text without running scripts (and → PDF through the Filee renderer, → DOCX through the DOCX writer) | | **Markup languages** | built in | **Jupyter notebooks (IPYNB), OPML, Textile, MediaWiki, reStructuredText, Org, DocBook 5 → Markdown, HTML, TXT and each other**; every document the built-in readers read → each of them; each → DOCX, ODT, HWPX, PDF through the document writers (see [Lightweight markup languages](#lightweight-markup-languages)); **FreeMind / Freeplane (MM) and XMind mind maps → Markdown, HTML, TXT, OPML** as outlines (see [Mind maps](#mind-maps)) | | Spreadsheets (ExcelDataReader for XLS) | built in (library) | **XLSX, XLSB, XLS, ODS (+ OTS, FODS), CSV, TSV → XLSX, ODS, CSV, TSV** (one CSV / TSV per sheet; OTS and FODS out through the next engine) | @@ -3688,9 +3688,10 @@ managed code only, so it works on every platform Filee runs on (Windows, Linux a files); the e-book writers write it out: - **TXT** (`PlainTextWriter`, UTF-8 with a byte order mark): paragraphs separated by an empty line; list items with the - number or bullet 한글 shows ("1.", "가)", "●") and indented by level; table rows as tab-separated cells; a text box - on lines of its own; footnotes and endnotes as `[1]` with their text after a `----` line at the end; equations as - their script. Headers, footers and page numbers are left out, as are tracked deletions. + number or bullet 한글 shows ("1.", "가)", "●") and a tab after it, indented by level; table rows as tab-separated + cells; a text box on lines of its own; footnotes and endnotes as `[1]` with their text after a `----` line at the + end; equations as their script. Headers and footers are left out, as are tracked deletions; a page number field in + the text writes the page counted from the sections and page breaks (plain text has no pages to lay out). - **Markdown** (`MarkdownWriter`): headings (outline paragraphs and heading styles), bold / italic / strikeout / super- and subscript, lists (numbered ones as `1.`, nested by level), pipe tables, links, footnotes (`[^1]`), and pictures in a `_files` folder next to the Markdown file (`MarkdownPictures`: `img0001.png`, formats editors @@ -3702,6 +3703,20 @@ files); the e-book writers write it out: The edges cost what Unhwp's did (TXT and Markdown 10, HTML 14) and the engine took Unhwp's place in the engine order (settings schema 100 renames `unhwp` to `hwp-text`), so every route keeps its cost. +**RTF, Word and ODT → TXT** (#1204) go through the same engine: the RTF, DOCX, [DOC](#doc-reader) and +ODT readers read the document model and `PlainTextWriter.WriteLines` writes it as Word and LibreOffice save plain +text. Each paragraph and each line break is a line, an empty paragraph an empty line (a paragraph that holds only a +page break too), and the text's own leading spaces, runs of spaces and tabs stay; the paragraph's indentation isn't +added. A list number or bullet is followed by a tab, or by the space or nothing a Word list level asks for (`w:suff`), +as Word and LibreOffice write it. Table rows are tab-separated cells (a table in a cell joins the cell's text), notes +come at the end as above. A PAGE or NUMPAGES field writes the number the file showed when it was saved (Word's field +result, the text of ODT's `text:page-number`, RTF's `\fldrslt`), else the count from the page breaks. These edges are +direct only (a TXT step in another route would lose the formatting) and cost 15: with any engine order's penalty +(0 to 9) that stays below LibreOffice's fallback edge (25), so they win wherever LibreOffice stands in the list, and +*Use LibreOffice for Word documents → PDF* is about PDF only. The DOC reader checks each file: a Word 6.0 / 95 file +goes to LibreOffice. Without them the cheapest route went through the Filee renderer's PDF and its text, which +wrapped long lines and lost the indentation and the empty lines. + Unhwp (a native Rust library through NuGet) did this before. It needed the Visual C++ runtime next to Filee.exe and had no Linux or Windows ARM64 build of its own (Filee built one, #236). Measured before it was removed, on the 30 documents generated in code by the tests (the rich DOCX of `DocumentFuzzTests` written as HWPX, the 24 diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index 64469576a..cfaa018ec 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "إنشاء ملفات PDF من الصور ودمج ملفات PDF أو تقسيمها أو استخراج نطاقات صفحات منها.\nويقلّل حجم PDF بتصغير الصور داخله.", "engine.rhwp.description": "التحويل بين HWP وHWPX، وتحويل HWP/HWPX إلى PDF عندما يكون مُصيِّر Filee متوقفًا أو لا يستطيع رسم الملف بعد.\nوعندها يعالج أيضًا المستندات التي تمر عبر HWPX للوصول إلى PDF ‏(DOCX وPPTX والكتب الإلكترونية…).", "engine.hwp-text.name": "نص HWP (مدمج)", - "engine.hwp-text.description": "استخراج النصوص وMarkdown وHTML من HWP وHWPX.", + "engine.hwp-text.description": "استخراج النصوص وMarkdown وHTML من HWP وHWPX، والنص العادي من Word وODT وRTF.", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "تحويل Markdown إلى HTML ونص عادي.\nتحويل ملفات النصوص والشيفرات المصدرية للبرامج النصية إلى TXT بترميز UTF-8، دون تشغيلها.", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/bn-BD.json b/src/Filee.App/Assets/i18n/bn-BD.json index 34be0d9cc..87630cc7c 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -291,7 +291,7 @@ "engine.pdfsharp.description": "ছবি থেকে পিডিএফ তৈরি করে এবং পিডিএফ থেকে পৃষ্ঠার পরিসর একত্রিত, বিভক্ত বা আলাদা করে।\nছবিগুলোর আকার ছোট করে পিডিএফ ফাইলের আকারও কমায়।", "engine.rhwp.description": "HWP ↔ HWPX রূপান্তর করে, এবং Filee রেন্ডারার বন্ধ থাকলে বা কোনো ফাইল এখনো আঁকতে না পারলে HWP/HWPX-কে পিডিএফ-এ রেন্ডার করে।\nতখন HWPX-এর মাধ্যমে পিডিএফ-এ রূপান্তরিত হওয়া ডকুমেন্টগুলোও (DOCX, PPTX, ই-বুক ইত্যাদি) এটি তৈরি করে।", "engine.hwp-text.name": "HWP টেক্সট (বিল্ট-ইন)", - "engine.hwp-text.description": "HWP ও HWPX থেকে টেক্সট, মার্কডাউন এবং এইচটিএমএল নিষ্কাশন করে।", + "engine.hwp-text.description": "HWP ও HWPX থেকে টেক্সট, মার্কডাউন এবং এইচটিএমএল, আর Word, ODT ও RTF থেকে সাধারণ টেক্সট নিষ্কাশন করে।", "engine.markdown.name": "মার্কডাউন (বিল্ট-ইন)", "engine.markdown.description": "মার্কডাউন → এইচটিএমএল এবং প্লেইন টেক্সট।\nস্ক্রিপ্ট না চালিয়েই টেক্সট এবং স্ক্রিপ্ট সোর্স ফাইল → UTF-8 TXT।", "engine.spreadsheet.name": "স্প্রেডশিট (বিল্ট-ইন)", @@ -1473,7 +1473,7 @@ "engines.guidance.macos.exiftool": "Homebrew দিয়ে ইনস্টল করুন (brew install exiftool), তারপর “আবার যাচাই করুন” বেছে নিন।\nঅথবা “আমার কপি ব্যবহার করুন…” বেছে নিয়ে এর ফোল্ডার নির্বাচন করুন।", "engines.guidance.macos.calibre": "ফাইলি যে calibre ডাউনলোড করে তার জন্য macOS 14 বা পরবর্তী সংস্করণ প্রয়োজন। calibre-ebook.com থেকে এই macOS-এর সর্বশেষ সংস্করণ calibre 7.26 ইনস্টল করুন, তারপর “আমার কপি ব্যবহার করুন…” বেছে নিয়ে এর .app ফোল্ডার নির্বাচন করুন।", "engines.guidance.linux": "আপনার ডিস্ট্রিবিউশনের প্যাকেজ ম্যানেজার দিয়ে {0} ইনস্টল করুন, তারপর “আমার কপি ব্যবহার করুন…” বেছে নিয়ে এর প্রোগ্রাম ফোল্ডারটি নির্বাচন করুন।", - "engines.guidance.linux.libreoffice": "আপনার প্যাকেজ ম্যানেজার দিয়ে এটি ইনস্টল করুন (Ubuntu/Debian: sudo apt install libreoffice), তারপর “আবার যাচাই করুন” বেছে নিন।\nHWP খুলতে Java এবং H2Orestart এক্সটেনশনও প্রয়োজন।", + "engines.guidance.linux.libreoffice": "আপনার প্যাকেজ ম্যানেজার দিয়ে এটি ইনস্টল করুন (Ubuntu/Debian: sudo apt install libreoffice), তারপর “আবার যাচাই করুন” বেছে নিন।", "engines.guidance.linux.ghostscript": "আপনার প্যাকেজ ম্যানেজার দিয়ে এটি ইনস্টল করুন (Ubuntu/Debian: sudo apt install ghostscript), তারপর “আবার যাচাই করুন” বেছে নিন।", "engines.guidance.linux.tesseract": "আপনার প্যাকেজ ম্যানেজার দিয়ে এটি ইনস্টল করুন (Ubuntu/Debian: sudo apt install tesseract-ocr), তারপর “আবার যাচাই করুন” বেছে নিন।", "engines.guidance.linux.exiftool": "আপনার প্যাকেজ ম্যানেজার দিয়ে ইনস্টল করুন (Ubuntu/Debian: sudo apt install libimage-exiftool-perl), তারপর “আবার যাচাই করুন” বেছে নিন।", diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.json index 4d2fe10d3..fd3fad326 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "Erstellt PDFs aus Bildern, führt PDFs zusammen, teilt sie oder entnimmt Seitenbereiche.\nVerkleinert PDFs auch durch Herunterskalieren der enthaltenen Bilder.", "engine.rhwp.description": "Konvertiert HWP ↔ HWPX und wandelt HWP/HWPX in PDF um, wenn der Filee-Renderer aus ist oder eine Datei noch nicht zeichnen kann.\nDann erzeugt es auch PDFs aus Dokumenten, die über HWPX konvertiert werden (DOCX, PPTX, E-Books…).", "engine.hwp-text.name": "HWP-Text (integriert)", - "engine.hwp-text.description": "Extrahiert Text, Markdown und HTML aus HWP und HWPX.", + "engine.hwp-text.description": "Extrahiert Text, Markdown und HTML aus HWP und HWPX sowie reinen Text aus Word, ODT und RTF.", "engine.markdown.name": "Markdown (integriert)", "engine.markdown.description": "Markdown → HTML und reiner Text.\nTextdateien und Skript-Quelltexte → UTF-8-TXT, ohne Skripte auszuführen.", "engine.spreadsheet.name": "Tabellen (integriert)", diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json index 9bafb1000..729a68afc 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "Makes PDFs from images and merges, splits or cuts page ranges out of PDFs.\nAlso makes PDFs smaller by scaling down their pictures.", "engine.rhwp.description": "Converts HWP ↔ HWPX, and renders HWP/HWPX to PDF when the Filee renderer is off or can't draw a file yet.\nThen it also draws documents that reach PDF through HWPX (DOCX, PPTX, e-books…).", "engine.hwp-text.name": "HWP text (built-in)", - "engine.hwp-text.description": "Extracts text, Markdown and HTML from HWP and HWPX.", + "engine.hwp-text.description": "Extracts text, Markdown and HTML from HWP and HWPX, and plain text from Word, ODT and RTF.", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML and plain text.\nText and script source files → UTF-8 TXT, without running scripts.", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.json index 268ebe65b..62f054932 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "Crea PDF a partir de imágenes y combina, divide o extrae rangos de páginas de PDF.\nTambién reduce el tamaño de los PDF reduciendo sus imágenes.", "engine.rhwp.description": "Convierte HWP ↔ HWPX y renderiza HWP/HWPX a PDF cuando el renderizador de Filee está desactivado o aún no puede dibujar un archivo.\nEntonces también dibuja los documentos que llegan a PDF a través de HWPX (DOCX, PPTX, libros electrónicos…).", "engine.hwp-text.name": "Texto HWP (integrado)", - "engine.hwp-text.description": "Extrae texto, Markdown y HTML de HWP y HWPX.", + "engine.hwp-text.description": "Extrae texto, Markdown y HTML de HWP y HWPX, y texto sin formato de Word, ODT y RTF.", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML y texto sin formato.\nArchivos de texto y código fuente de scripts → TXT en UTF-8, sin ejecutar scripts.", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/fa.json b/src/Filee.App/Assets/i18n/fa.json index 0e2eba9e0..8a4be2a9a 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -291,7 +291,7 @@ "engine.pdfsharp.description": "ایجاد PDF از تصاویر و ادغام، تقسیم یا برش محدوده‌های صفحه از PDF.\nهمچنین کوچک‌تر کردن PDF با کاهش اندازه تصاویر.", "engine.rhwp.description": "تبدیل HWP ↔ HWPX، و رندر HWP/HWPX به PDF وقتی رندرکننده Filee خاموش است یا هنوز نمی‌تواند فایلی را ترسیم کند.\nدر این حالت اسنادی را هم که از طریق HWPX به PDF تبدیل می‌شوند رندر می‌کند (DOCX، PPTX، کتاب‌های الکترونیکی…).", "engine.hwp-text.name": "متن HWP (داخلی)", - "engine.hwp-text.description": "استخراج متن، مارک‌داون و HTML از HWP و HWPX.", + "engine.hwp-text.description": "استخراج متن، مارک‌داون و HTML از HWP و HWPX، و متن ساده از Word، ODT و RTF.", "engine.markdown.name": "مارک‌داون (داخلی)", "engine.markdown.description": "تبدیل مارک‌داون به HTML و متن ساده.\nفایل‌های متنی و سورس‌کد اسکریپت‌ها به TXT با کدگذاری UTF-8، بدون اجرای اسکریپت‌ها.", "engine.spreadsheet.name": "صفحه‌گسترده‌ها (داخلی)", @@ -1473,7 +1473,7 @@ "engines.guidance.macos.exiftool": "آن را با Homebrew نصب کنید (brew install exiftool) و سپس «بررسی مجدد» را انتخاب کنید.\nیا «استفاده از نسخه من…» را انتخاب کنید و پوشه آن را برگزینید.", "engines.guidance.macos.calibre": "نسخه Calibre که Filee دانلود می‌کند به macOS 14 یا جدیدتر نیاز دارد. Calibre 7.26 (آخرین نسخه برای این macOS) را از calibre-ebook.com نصب کرده، سپس «استفاده از نسخه من…» را انتخاب و پوشه .app آن را برگزینید.", "engines.guidance.linux": "{0} را با مدیر بسته توزیع خود نصب کنید، سپس «استفاده از نسخه من…» را انتخاب کرده و پوشه برنامه را برگزینید.", - "engines.guidance.linux.libreoffice": "آن را با مدیر بسته خود نصب کنید (اوبونتو/دبیان: sudo apt install libreoffice)، سپس «بررسی مجدد» را بزنید.\nباز کردن HWP همچنین به جاوا و افزونه H2Orestart نیاز دارد.", + "engines.guidance.linux.libreoffice": "آن را با مدیر بسته خود نصب کنید (اوبونتو/دبیان: sudo apt install libreoffice)، سپس «بررسی مجدد» را بزنید.", "engines.guidance.linux.ghostscript": "آن را با مدیر بسته خود نصب کنید (اوبونتو/دبیان: sudo apt install ghostscript)، سپس «بررسی مجدد» را بزنید.", "engines.guidance.linux.tesseract": "آن را با مدیر بسته خود نصب کنید (اوبونتو/دبیان: sudo apt install tesseract-ocr)، سپس «بررسی مجدد» را بزنید.", "engines.guidance.linux.exiftool": "آن را با مدیر بسته خود نصب کنید (Ubuntu/Debian: sudo apt install libimage-exiftool-perl) و سپس «بررسی مجدد» را انتخاب کنید.", diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.json index 315b3b95f..3593ba953 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "Crée des PDF à partir d'images, fusionne et divise des PDF ou en extrait des plages de pages.\nRéduit aussi la taille des PDF en réduisant leurs images.", "engine.rhwp.description": "Convertit HWP ↔ HWPX, et HWP/HWPX en PDF quand le moteur de rendu Filee est désactivé ou ne sait pas encore dessiner un fichier.\nIl dessine alors aussi les documents qui passent par HWPX pour devenir des PDF (DOCX, PPTX, livres numériques…).", "engine.hwp-text.name": "Texte HWP (intégré)", - "engine.hwp-text.description": "Extrait le texte, le Markdown et le HTML des fichiers HWP et HWPX.", + "engine.hwp-text.description": "Extrait le texte, le Markdown et le HTML des fichiers HWP et HWPX, et le texte brut des fichiers Word, ODT et RTF.", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML et texte brut.\nFichiers texte et sources de scripts → TXT UTF-8, sans exécuter les scripts.", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.json index 32592b20c..6c3240852 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "इमेज से PDF बनाता है और PDF को मर्ज करता है, बाँटता है या उनमें से पेज रेंज काटता है।\nPDF की तस्वीरों का आकार घटाकर उन्हें छोटा भी करता है।", "engine.rhwp.description": "HWP ↔ HWPX कन्वर्ट करता है, और जब Filee रेंडरर बंद हो या कोई फ़ाइल अभी न बना सके तब HWP/HWPX को PDF में रेंडर करता है।\nतब यह उन दस्तावेज़ों को भी बनाता है जो HWPX के रास्ते PDF तक पहुँचते हैं (DOCX, PPTX, ई-बुक…)।", "engine.hwp-text.name": "HWP टेक्स्ट (अंतर्निहित)", - "engine.hwp-text.description": "HWP और HWPX से टेक्स्ट, Markdown और HTML निकालता है।", + "engine.hwp-text.description": "HWP और HWPX से टेक्स्ट, Markdown और HTML, और Word, ODT व RTF से सादा टेक्स्ट निकालता है।", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML और सादा टेक्स्ट।\nटेक्स्ट और स्क्रिप्ट की सोर्स फ़ाइलें → UTF-8 TXT, स्क्रिप्ट चलाए बिना।", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/id.json b/src/Filee.App/Assets/i18n/id.json index 861189981..4cbc5d261 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "Membuat PDF dari gambar, serta menggabungkan, memisahkan, atau memotong rentang halaman PDF.\nJuga memperkecil ukuran PDF dengan memperkecil resolusi gambarnya.", "engine.rhwp.description": "Mengonversi HWP ↔ HWPX dan merender HWP/HWPX ke PDF saat perender Filee mati atau belum bisa menggambar suatu berkas.\nSaat itu engine ini juga menggambar dokumen yang mencapai PDF melalui alur HWPX (DOCX, PPTX, e-book…).", "engine.hwp-text.name": "Teks HWP (bawaan)", - "engine.hwp-text.description": "Mengekstrak teks, Markdown, dan HTML dari file HWP dan HWPX.", + "engine.hwp-text.description": "Mengekstrak teks, Markdown, dan HTML dari file HWP dan HWPX, serta teks biasa dari file Word, ODT, dan RTF.", "engine.markdown.name": "Markdown (bawaan)", "engine.markdown.description": "Markdown → HTML dan teks polos.\nFile kode sumber skrip dan teks → UTF-8 TXT, tanpa mengeksekusi skrip.", "engine.spreadsheet.name": "Spreadsheet (bawaan)", diff --git a/src/Filee.App/Assets/i18n/it.json b/src/Filee.App/Assets/i18n/it.json index 9566c1a2c..84dacfab9 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -276,7 +276,7 @@ "engine.pdfsharp.description": "Crea PDF da immagini e unisce, divide o estrae intervalli di pagine dai PDF.\nRiduce anche il peso dei PDF ridimensionando le immagini.", "engine.rhwp.description": "Converte HWP ↔ HWPX ed esegue il rendering di HWP/HWPX in PDF quando il renderer di Filee è disattivato o non sa ancora disegnare un file.\nIn quel caso disegna anche i documenti che arrivano al PDF passando da HWPX (DOCX, PPTX, e-book…).", "engine.hwp-text.name": "Testo HWP (integrato)", -"engine.hwp-text.description": "Estrae testo, Markdown e HTML da HWP e HWPX.", +"engine.hwp-text.description": "Estrae testo, Markdown e HTML da HWP e HWPX, e testo semplice da Word, ODT e RTF.", "engine.markdown.name": "Markdown (integrato)", "engine.markdown.description": "Markdown → HTML e testo semplice.\nFile sorgente di testo e script → TXT UTF-8, senza eseguire gli script.", "engine.spreadsheet.name": "Fogli di calcolo (integrato)", diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json index 53ead4fd4..e7c64ff48 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "画像からPDFを作成し、PDFの結合、分割、ページ範囲の切り出しを行います。\n画像を縮小してPDFのファイルサイズを小さくすることもできます。", "engine.rhwp.description": "HWPとHWPXを相互変換し、Fileeレンダラーがオフのときやまだ描画できないファイルでは、HWP/HWPXをPDFに変換します。\nそのときはHWPXを経由してPDFにする文書(DOCX、PPTX、電子書籍など)の描画にも使われます。", "engine.hwp-text.name": "HWP テキスト (内蔵)", - "engine.hwp-text.description": "HWPとHWPXからテキスト、Markdown、HTMLを抽出します。", + "engine.hwp-text.description": "HWPとHWPXからテキスト、Markdown、HTMLを、Word、ODT、RTFからプレーンテキストを抽出します。", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "MarkdownをHTMLまたはプレーンテキストに変換します。\nテキストとスクリプトのソースファイル → UTF-8 TXT。スクリプトは実行しません。", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json index 6762ab219..2556c028f 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "이미지로 PDF를 만들고, PDF를 합치거나 나누거나 원하는 페이지만 잘라내요.\nPDF 속 이미지를 줄여 파일을 작게 만들기도 해요.", "engine.rhwp.description": "HWP ↔ HWPX를 바꾸고, Filee 렌더러가 꺼져 있거나 아직 그리지 못하는 파일이면 HWP/HWPX를 PDF로 그려요.\n그때는 HWPX를 거쳐 PDF가 되는 문서(DOCX, PPTX, 전자책 등)도 그려요.", "engine.hwp-text.name": "HWP 텍스트 (내장)", - "engine.hwp-text.description": "HWP와 HWPX에서 텍스트, 마크다운, HTML을 뽑아내요.", + "engine.hwp-text.description": "HWP와 HWPX에서 텍스트, 마크다운, HTML을, Word·ODT·RTF에서 일반 텍스트를 뽑아내요.", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "마크다운 → HTML, 텍스트.\n텍스트·스크립트 소스 파일 → UTF-8 TXT. 스크립트는 실행하지 않아요.", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/mn.json b/src/Filee.App/Assets/i18n/mn.json index fec353419..0561bc987 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -276,7 +276,7 @@ "engine.pdfsharp.description": "Зургуудаас PDF үүсгэж, PDF-үүдийг нэгтгэх, хуваах эсвэл хуудасны мужийг тасалж авна.\nМөн доторх зургуудыг нь жижигрүүлж PDF-ийн хэмжээг багасгана.", "engine.rhwp.description": "HWP ↔ HWPX хооронд хөрвүүлж, Filee буулгагч унтраалттай эсвэл файлыг одоогоор зурж чадахгүй үед HWP/HWPX-ийг PDF болгон буулгана.\nТэгэхэд HWPX-ээр дамжуулан PDF болдог баримтуудыг (DOCX, PPTX, цахим ном…) мөн зурна.", "engine.hwp-text.name": "HWP текст (суурилагдсан)", - "engine.hwp-text.description": "HWP болон HWPX-ээс текст, Markdown болон HTML-ийг гаргаж авна.", + "engine.hwp-text.description": "HWP болон HWPX-ээс текст, Markdown болон HTML-ийг, Word, ODT, RTF-ээс энгийн текстийг гаргаж авна.", "engine.markdown.name": "Markdown (суурилагдсан)", "engine.markdown.description": "Markdown → HTML болон энгийн текст.\nТекст болон скриптийн эх файлууд → скриптийг ажиллуулахгүйгээр UTF-8 TXT болгоно.", "engine.spreadsheet.name": "Хүснэгт (суурилагдсан)", diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.json index 7677d13db..e267fbc67 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "छविहरूबाट PDF बनाउँछ र PDF हरूलाई मर्ज गर्दछ, विभाजन गर्दछ वा पृष्ठ दायरा काट्छ।\nसाथै तस्बिरहरूको आकार घटाएर PDF लाई सानो बनाउँछ।", "engine.rhwp.description": "HWP ↔ HWPX रूपान्तरण गर्दछ, र Filee रेन्डरर बन्द हुँदा वा कुनै फाइल अझै कोर्न नसक्दा HWP/HWPX लाई PDF मा रेन्डर गर्दछ।\nत्यसबेला यसले HWPX मार्फत PDF सम्म पुग्ने कागजातहरू पनि तयार गर्दछ (DOCX, PPTX, इ-पुस्तक…)।", "engine.hwp-text.name": "HWP पाठ (अन्तर्निर्मित)", - "engine.hwp-text.description": "HWP र HWPX बाट पाठ, Markdown र HTML निकाल्छ।", + "engine.hwp-text.description": "HWP र HWPX बाट पाठ, Markdown र HTML, अनि Word, ODT र RTF बाट सादा पाठ निकाल्छ।", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML र सादा पाठ।\nपाठ र लिपि स्रोत फाइलहरू → UTF-8 TXT, लिपिहरू नचलाईकन।", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.json index 9547ca2f7..c0539dc45 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "Tworzy PDF z obrazów oraz łączy, dzieli i wycina zakresy stron z plików PDF.", "engine.rhwp.description": "Konwertuje HWP ↔ HWPX i renderuje HWP/HWPX do PDF, gdy renderer Filee jest wyłączony lub nie umie jeszcze narysować pliku.\nWtedy rysuje też dokumenty, które trafiają do PDF przez HWPX (DOCX, PPTX, e-booki…).", "engine.hwp-text.name": "Tekst HWP (wbudowany)", - "engine.hwp-text.description": "Wyodrębnia tekst, Markdown i HTML z HWP i HWPX.", + "engine.hwp-text.description": "Wyodrębnia tekst, Markdown i HTML z HWP i HWPX oraz czysty tekst z plików Word, ODT i RTF.", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML i zwykły tekst.\nPliki tekstowe i źródła skryptów → TXT w UTF-8, bez uruchamiania skryptów.", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.json index 519fbc23a..e715461e2 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "Cria PDFs a partir de imagens e mescla, divide ou recorta intervalos de páginas de PDFs.\nTambém deixa PDFs menores ao reduzir as imagens.", "engine.rhwp.description": "Converte HWP ↔ HWPX e renderiza HWP/HWPX em PDF quando o renderizador do Filee está desligado ou ainda não consegue desenhar um arquivo.\nNesse caso, também desenha documentos que chegam ao PDF pelo HWPX (DOCX, PPTX, e-books…).", "engine.hwp-text.name": "Texto de HWP (mecanismo integrado)", - "engine.hwp-text.description": "Extrai texto, Markdown e HTML de HWP e HWPX.", + "engine.hwp-text.description": "Extrai texto, Markdown e HTML de HWP e HWPX, e texto simples de Word, ODT e RTF.", "engine.markdown.name": "Markdown (mecanismo integrado)", "engine.markdown.description": "Markdown → HTML e texto simples.\nArquivos de texto e de código de script → TXT em UTF-8, sem executar scripts.", "engine.spreadsheet.name": "Planilhas (mecanismo integrado)", diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.json index 2a9b91ed3..0da25b893 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "Cria PDF a partir de imagens e junta, divide ou extrai intervalos de páginas de PDF.\nTambém reduz o tamanho dos PDF ao diminuir a resolução das imagens.", "engine.rhwp.description": "Converte HWP ↔ HWPX e renderiza HWP/HWPX para PDF quando o renderizador do Filee está desativado ou ainda não consegue desenhar um ficheiro.\nNesse caso, também renderiza documentos convertidos para PDF através de HWPX (DOCX, PPTX, livros digitais…).", "engine.hwp-text.name": "Texto de HWP (motor integrado)", - "engine.hwp-text.description": "Extrai texto, Markdown e HTML de HWP e HWPX.", + "engine.hwp-text.description": "Extrai texto, Markdown e HTML de HWP e HWPX, e texto simples de Word, ODT e RTF.", "engine.markdown.name": "Markdown (motor integrado)", "engine.markdown.description": "Markdown → HTML e texto simples.\nFicheiros de texto e código-fonte de scripts → TXT em UTF-8, sem executar scripts.", "engine.spreadsheet.name": "Folhas de cálculo (motor integrado)", diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json index 710f3622b..39d2a2130 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "Делает PDF из картинок, объединяет и разделяет PDF, вырезает диапазоны страниц.\nА ещё уменьшает PDF, сжимая картинки внутри.", "engine.rhwp.description": "Конвертирует HWP ↔ HWPX и превращает HWP/HWPX в PDF, когда рендерер Filee выключен или пока не может нарисовать файл.\nТогда он же отрисовывает документы, которые идут в PDF через HWPX (DOCX, PPTX, электронные книги…).", "engine.hwp-text.name": "Текст HWP (встроенный)", - "engine.hwp-text.description": "Достаёт текст, Markdown и HTML из HWP и HWPX.", + "engine.hwp-text.description": "Достаёт текст, Markdown и HTML из HWP и HWPX, а простой текст — из Word, ODT и RTF.", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML и простой текст.\nТекстовые файлы и исходники скриптов → TXT в UTF-8 без выполнения скриптов.", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.json index 477643330..c6faaaa69 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "สร้าง PDF จากรูปภาพ รวม แยก หรือตัดช่วงหน้าออกจาก PDF\nและช่วยลดขนาด PDF โดยการย่อขนาดรูปภาพภายในไฟล์", "engine.rhwp.description": "แปลงระหว่าง HWP ↔ HWPX และเรนเดอร์ HWP/HWPX เป็น PDF เมื่อปิดตัวเรนเดอร์ของ Filee หรือเมื่อยังวาดไฟล์นั้นไม่ได้\nในกรณีนั้นจะแสดงผลเอกสารที่แปลงเป็น PDF ผ่าน HWPX ด้วย (DOCX, PPTX, อีบุ๊ก…)", "engine.hwp-text.name": "ข้อความ HWP (ในตัว)", - "engine.hwp-text.description": "แยกข้อความ, Markdown และ HTML ออกจาก HWP และ HWPX", + "engine.hwp-text.description": "แยกข้อความ, Markdown และ HTML ออกจาก HWP และ HWPX และแยกข้อความล้วนออกจาก Word, ODT และ RTF", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML และข้อความล้วน\nไฟล์ต้นฉบับข้อความและสคริปต์ → UTF-8 TXT โดยไม่เรียกใช้สคริปต์", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.json index 4d3f5e717..a5a52236d 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -277,7 +277,7 @@ "engine.pdfsharp.description": "Görsellerden PDF oluşturur; PDF'leri birleştirir, böler veya sayfa aralıklarını çıkarır.\nİçlerindeki görselleri küçülterek PDF boyutunu da azaltır.", "engine.rhwp.description": "HWP ↔ HWPX dönüştürür; Filee işleyicisi kapalıyken ya da bir dosyayı henüz çizemediğinde HWP/HWPX'i PDF'ye dönüştürür.\nO zaman PDF'ye HWPX üzerinden dönüştürülen belgeleri de işler (DOCX, PPTX, e-kitaplar…).", "engine.hwp-text.name": "HWP metin (yerleşik)", - "engine.hwp-text.description": "HWP ve HWPX'ten metin, Markdown ve HTML çıkarır.", + "engine.hwp-text.description": "HWP ve HWPX'ten metin, Markdown ve HTML; Word, ODT ve RTF'den düz metin çıkarır.", "engine.markdown.name": "Markdown (yerleşik)", "engine.markdown.description": "Markdown → HTML ve düz metin.\nMetin ve betik kaynak dosyaları → UTF-8 TXT; betikler çalıştırılmaz.", "engine.spreadsheet.name": "Elektronik tablolar (yerleşik)", diff --git a/src/Filee.App/Assets/i18n/vi.json b/src/Filee.App/Assets/i18n/vi.json index 118ce8669..c0fb7a0bb 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "Tạo PDF từ ảnh và ghép, tách hoặc cắt các khoảng trang khỏi PDF.\nCũng làm PDF nhỏ hơn bằng cách thu nhỏ ảnh bên trong.", "engine.rhwp.description": "Chuyển đổi HWP ↔ HWPX, và kết xuất HWP/HWPX sang PDF khi trình kết xuất Filee đang tắt hoặc chưa vẽ được một tệp.\nKhi đó nó cũng vẽ các tài liệu được chuyển sang PDF qua HWPX (DOCX, PPTX, sách điện tử…).", "engine.hwp-text.name": "Văn bản HWP (tích hợp)", - "engine.hwp-text.description": "Trích xuất văn bản, Markdown và HTML từ HWP và HWPX.", + "engine.hwp-text.description": "Trích xuất văn bản, Markdown và HTML từ HWP và HWPX, và văn bản thuần từ Word, ODT và RTF.", "engine.markdown.name": "Markdown (tích hợp)", "engine.markdown.description": "Markdown → HTML và văn bản thuần.\nTệp văn bản và mã nguồn tập lệnh → TXT UTF-8, không chạy tập lệnh.", "engine.spreadsheet.name": "Bảng tính (tích hợp)", @@ -1491,7 +1491,7 @@ "engines.guidance.macos.exiftool": "Cài bằng Homebrew (brew install exiftool), rồi chọn “Kiểm tra lại”.\nHoặc chọn “Dùng bản của tôi…” và chọn thư mục của nó.", "engines.guidance.macos.calibre": "Bản calibre mà Filee tải xuống cần macOS 14 trở lên. Hãy cài calibre 7.26, phiên bản cuối cùng cho macOS này, từ calibre-ebook.com, rồi chọn “Dùng bản của tôi…” và chọn thư mục .app của nó.", "engines.guidance.linux": "Cài {0} bằng trình quản lý gói của bản phân phối, rồi chọn “Dùng bản của tôi…” và chọn thư mục chương trình của nó.", - "engines.guidance.linux.libreoffice": "Cài bằng trình quản lý gói (Ubuntu/Debian: sudo apt install libreoffice), rồi chọn “Kiểm tra lại”.\nMở HWP cũng cần Java và tiện ích mở rộng H2Orestart.", + "engines.guidance.linux.libreoffice": "Cài bằng trình quản lý gói (Ubuntu/Debian: sudo apt install libreoffice), rồi chọn “Kiểm tra lại”.", "engines.guidance.linux.ghostscript": "Cài bằng trình quản lý gói (Ubuntu/Debian: sudo apt install ghostscript), rồi chọn “Kiểm tra lại”.", "engines.guidance.linux.tesseract": "Cài bằng trình quản lý gói (Ubuntu/Debian: sudo apt install tesseract-ocr), rồi chọn “Kiểm tra lại”.", "engines.guidance.linux.exiftool": "Cài bằng trình quản lý gói (Ubuntu/Debian: sudo apt install libimage-exiftool-perl), rồi chọn “Kiểm tra lại”.", diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json index 417976ddd..58c71c71e 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "用图片生成 PDF,合并、拆分 PDF 或截取指定页码。\n还能缩小 PDF 中的图片来减小文件。", "engine.rhwp.description": "在 HWP ↔ HWPX 之间转换;Filee 渲染器关闭或暂时无法绘制某个文件时,将 HWP/HWPX 渲染为 PDF。\n此时经 HWPX 转为 PDF 的文档(DOCX、PPTX、电子书等)也由它渲染。", "engine.hwp-text.name": "HWP 文本(内置)", - "engine.hwp-text.description": "从 HWP 和 HWPX 中提取文本、Markdown 和 HTML。", + "engine.hwp-text.description": "从 HWP 和 HWPX 中提取文本、Markdown 和 HTML,并从 Word、ODT 和 RTF 中提取纯文本。", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML 和纯文本。\n文本和脚本源文件 → UTF-8 TXT,不会执行脚本。", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.json index dfb2450d6..8b0fb78c4 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -284,7 +284,7 @@ "engine.pdfsharp.description": "用圖片產生 PDF,合併、拆分 PDF 或擷取指定頁碼。\n還能縮小 PDF 中的圖片來減小檔案。", "engine.rhwp.description": "在 HWP ↔ HWPX 之間轉換;Filee 轉譯器關閉或暫時無法繪製某個檔案時,將 HWP/HWPX 渲染為 PDF。\n此時經 HWPX 轉為 PDF 的文件(DOCX、PPTX、電子書等)也由它渲染。", "engine.hwp-text.name": "HWP 文字(內建)", - "engine.hwp-text.description": "從 HWP 和 HWPX 中擷取文字、Markdown 和 HTML。", + "engine.hwp-text.description": "從 HWP 和 HWPX 中擷取文字、Markdown 和 HTML,並從 Word、ODT 和 RTF 中擷取純文字。", "engine.markdown.name": "Markdown (built-in)", "engine.markdown.description": "Markdown → HTML 和純文字。\n文字和指令碼來源檔案 → UTF-8 TXT,不會執行指令碼。", "engine.spreadsheet.name": "Spreadsheets (built-in)", diff --git a/src/Filee.Engines/Hwp/HwpTextConverter.cs b/src/Filee.Engines/Hwp/HwpTextConverter.cs index 35649f396..897389d76 100644 --- a/src/Filee.Engines/Hwp/HwpTextConverter.cs +++ b/src/Filee.Engines/Hwp/HwpTextConverter.cs @@ -8,7 +8,8 @@ // RTF → TXT is here too (#1204): RtfReader's document written a line per paragraph, as a word processor saves plain // text. Without it the cheapest built-in route went through a PDF (the Filee renderer, then the PDF's text), which // wraps long lines and loses the indentation and the empty lines, or through the HWPX writer. Word documents (DOCX and -// its macro-enabled and template variants, Word 97–2003 DOC, DOT and WPS) and ODT go the same way. +// its macro-enabled and template variants, Word 97–2003 DOC, DOT and WPS) and ODT go the same way. These take the +// conversion from LibreOffice too, wherever it stands in the engine list. using System.Text; using Filee.Core.Conversion; @@ -35,16 +36,13 @@ public sealed class HwpTextConverter : IConverter, ICapabilityCheck new("hwp", "txt"), new("hwpx", "txt"), new("hwp", "md"), new("hwpx", "md"), new("hwp", "html", 14), new("hwpx", "html", 14), - // Direct only, as LibreOffice's RTF → TXT: a TXT step in another route would lose the formatting. Dearer than - // LibreOffice's chosen edge (10 + 9 in the default order against 16 + 4), so it stays LibreOffice's while that - // is installed, and cheaper than the renderer's PDF and its text (1 + 9 + 10 + 9). - new("rtf", "txt", 16, DirectOnly: true), - // Word and ODT, direct only as well. LibreOffice's edge for them is its fallback (25), so the cost sits between: - // 24 + 4 is cheaper than the renderer's PDF and its text (29) and the routes through Pandoc's Markdown (29) or - // the HWPX writer (32), and dearer than LibreOffice moved to the top of the engine list (25 + 0), which keeps - // the conversion it had there. The DOC reader checks each file (a Word 6.0 / 95 file goes another way). - .. LineFormats.Where(f => f != "rtf") - .Select(f => new ConversionEdge(f, "txt", 24, DirectOnly: true, Checked: DocumentReaders.Checked(f))), + // RTF, Word and ODT → TXT, direct only: a TXT step in another route would lose the formatting. LibreOffice's + // edge for them is its fallback (25), and this one wins wherever either engine stands in the engine list + // (15 + 9 at most against 25 + 0 at least): the text is the document's own, line for line, as LibreOffice + // writes it. It is cheaper than the renderer's PDF and its text (1 + 9 + 10 + 9 in the default order) and the + // routes through Pandoc's Markdown (29) or the HWPX writer (32). The DOC reader checks each file (a Word 6.0 / + // 95 file goes another way). + .. LineFormats.Select(f => new ConversionEdge(f, "txt", LineCost, DirectOnly: true, Checked: DocumentReaders.Checked(f))), ]; /// @@ -53,6 +51,9 @@ .. LineFormats.Where(f => f != "rtf") /// internal static readonly string[] LineFormats = ["rtf", "docx", "docm", "dotx", "dotm", "doc", "dot", "wps", "odt"]; + /// The cost of RTF, Word and ODT → TXT: below LibreOffice's fallback (25) with any engine order's penalty (0 to 9). + internal const int LineCost = 15; + public EngineStatus GetStatus() => EngineStatus.Available("HWP, HWPX → TXT, Markdown, HTML; Word, ODT, RTF → TXT", EngineVersions.BuiltIn); /// A Word 6.0 / 95 or Works file with a .doc or .wps name, which the DOC reader doesn't read. diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs index ceb7acbac..804984498 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs @@ -75,6 +75,9 @@ internal sealed partial class DocxReader /// private List? _pageBreakFor; + /// The paragraph marks left out so for the next block (). + private int _pageBreakMarks; + /// Table cells, text boxes and notes around the blocks being read (see ). private int _depth; @@ -778,6 +781,8 @@ private void ReadBlock(XElement element, List output) { if (ReferenceEquals(_pageBreakFor, output)) _pageBreakFor = null; + output[count].BreakParagraphsBefore = _pageBreakMarks; + _pageBreakMarks = 0; switch (output[count]) { case HParagraph paragraph: @@ -976,7 +981,10 @@ public void Finish(bool endsSection = false) // with the next paragraph, not with an empty line and the paragraph's space after. if (_pageBroken && Current.Inlines.Count == 0 && Current.List is not { Numbered: true } && !endsSection) { + if (!ReferenceEquals(_reader._pageBreakFor, _output)) + _reader._pageBreakMarks = 0; _reader._pageBreakFor = _output; + _reader._pageBreakMarks++; return; } Emit(); @@ -1123,9 +1131,11 @@ private void RunChild(XElement child, ParagraphBuilder builder, HCharFormat form foreach (var text in SymbolFonts.Runs(child.Value, format)) builder.Add(text); } - else if (StoredResult() is { } stored && stored.Stored!.Length < 32) + else if (NumberResult() is { } number && (number.Shown?.Length ?? 0) < 32) { - stored.Stored += child.Value; + number.Shown += child.Value; + if (number.Stored is not null) + number.Stored += child.Value; } break; case "tab" or "ptab": @@ -1380,8 +1390,11 @@ private sealed class FieldState public bool Suppress; public string? Target; - /// The page number field whose result text is kept as . - public HField? Stored; + /// + /// The page number field whose result text is kept as , and as + /// where that isn't null. + /// + public HField? Number; /// A table of contents (TOC): its entries don't take the Hyperlink character style's look. public bool TableOfContents; @@ -1394,9 +1407,9 @@ private sealed class FieldState private bool ShowsText() => _fields.All(f => f.Result && !f.Suppress); /// The page number whose result text is being read, when only that field hides it. - private HField? StoredResult() => - _fields.Count > 0 && _fields[^1] is { Result: true, Stored: { } stored } && _fields.Take(_fields.Count - 1).All(f => f.Result && !f.Suppress) - ? stored + private HField? NumberResult() => + _fields.Count > 0 && _fields[^1] is { Result: true, Number: { } number } && _fields.Take(_fields.Count - 1).All(f => f.Result && !f.Suppress) + ? number : null; private void FieldChar(XElement fldChar, ParagraphBuilder builder, HCharFormat format) @@ -1441,7 +1454,7 @@ private void StartFieldResult(FieldState field, ParagraphBuilder builder, HCharF // which comes next, as text. The layout engine counts the pages, as for any other page number. var number = new HField(words[0].Equals("PAGE", StringComparison.OrdinalIgnoreCase) ? HFieldKind.PageNumber : HFieldKind.TotalPages, format) { Stored = "" }; builder.Add(number); - field.Stored = number; + field.Number = number; field.Suppress = true; break; } @@ -1466,14 +1479,14 @@ private void StartFieldResult(FieldState field, ParagraphBuilder builder, HCharF builder.OpenLink(field.Target); break; } - case "PAGE": - builder.Add(new HField(HFieldKind.PageNumber, format)); - field.Suppress = true; - break; - case "NUMPAGES" or "SECTIONPAGES": - builder.Add(new HField(HFieldKind.TotalPages, format)); - field.Suppress = true; - break; + case "PAGE" or "NUMPAGES" or "SECTIONPAGES": + { + var number = new HField(words[0].Equals("PAGE", StringComparison.OrdinalIgnoreCase) ? HFieldKind.PageNumber : HFieldKind.TotalPages, format); + builder.Add(number); + field.Number = number; + field.Suppress = true; + break; + } } } diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs index 3ee141b45..f72be8e1f 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs @@ -292,6 +292,13 @@ internal abstract class HBlock /// of a section); null = none. Read for layout only. /// public int? NewPageNumber { get; set; } + + /// + /// Word paragraphs that held nothing after their page break, right before this block: the layout leaves them out + /// (Word keeps their marks on the old page), and plain text writes each as an empty line, as Word and LibreOffice save + /// it. + /// + public int BreakParagraphsBefore { get; set; } } /// @@ -730,6 +737,12 @@ internal sealed class HField(HFieldKind kind, HCharFormat format) : HInline /// a page number in a table in a text box blank); null when it writes the field. The layout engine always counts. /// public string? Stored { get; set; } + + /// + /// The number the source showed for the field when it was saved (Word's field result, the text of ODT's + /// text:page-number, RTF's \fldrslt); null when it has none. Plain text, which has no pages to count, writes it. + /// + public string? Shown { get; set; } } /// diff --git a/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs b/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs index 589a3dc94..ed237bb76 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs @@ -518,6 +518,13 @@ private sealed class TextState public bool Space { get; set; } = true; } + /// + /// The number a text:page-number or text:page-count showed when the file was saved ("3", "iv"); null for none and + /// for a placeholder such as Impress's "<number>". + /// + private static string? Shown(XElement field) => + field.Value.Trim() is { Length: > 0 and <= 32 } shown && shown.All(char.IsLetterOrDigit) ? shown : null; + private void Inlines(XElement container, TextProps text, List output, TextState state) { if (text.Hidden) @@ -600,11 +607,11 @@ private void Inlines(XElement container, TextProps text, List output, T state.Space = false; break; case "page-number": - output.Add(new HField(HFieldKind.PageNumber, CharFormat(text))); + output.Add(new HField(HFieldKind.PageNumber, CharFormat(text)) { Shown = Shown(element) }); state.Space = false; break; case "page-count": - output.Add(new HField(HFieldKind.TotalPages, CharFormat(text))); + output.Add(new HField(HFieldKind.TotalPages, CharFormat(text)) { Shown = Shown(element) }); state.Space = false; break; case "ruby": diff --git a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs index dcbe49e5e..94a53504c 100644 --- a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs @@ -2,6 +2,11 @@ // as tab-separated cells, text boxes on lines of their own and footnotes collected at the end. Used for e-books and // HWP / HWPX → TXT. WriteLines is a word processor's "plain text" instead (RTF, Word and ODT → TXT): a line per // paragraph, an empty paragraph an empty line, and the text's own spaces and tabs as they are. +// +// Both write a list number or bullet followed by a tab, as Word and LibreOffice save a list as plain text (a Word list +// level that asks for a space or nothing after its number gets that). A page number or page count field writes the +// number the source showed when it was saved (Word's field result, ODT's text, RTF's \fldrslt), else the number counted +// from the page breaks and sections: plain text has no pages to lay out, and an empty field lost the number. using System.Globalization; using System.Text; @@ -19,49 +24,71 @@ internal static class PlainTextWriter /// public static string WriteLines(HDocument document) => Write(document, lines: true); + /// What the text collects on the way: the notes, and the page it is on for the page number fields. + private sealed class State + { + public List Notes { get; } = []; + + /// The page the text is on, counted from the sections and the page breaks. + public int Page { get; set; } = 1; + + /// The pages of the document, counted so. + public int Pages { get; init; } = 1; + } + private static string Write(HDocument document, bool lines) { Nesting.Limit(document); var sb = new StringBuilder(); - var notes = new List(); + var state = new State { Pages = document.Sections.Sum(s => 1 + s.Blocks.Count(BreaksPage)) }; var numbers = new ListNumbers(); - foreach (var section in document.Sections) + for (var i = 0; i < document.Sections.Count; i++) { + var section = document.Sections[i]; + state.Page = section.StartPageNumber ?? (i == 0 ? 1 : state.Page + 1); if (lines) - Lines(section.Blocks, sb, notes, numbers); + Lines(section.Blocks, sb, state, numbers); else - Blocks(section.Blocks, sb, notes, numbers); + Blocks(section.Blocks, sb, state, numbers); } - if (notes.Count > 0) + if (state.Notes.Count > 0) { sb.AppendLine().AppendLine("----"); - for (var i = 0; i < notes.Count; i++) - sb.AppendLine($"[{i + 1}] {notes[i]}"); + for (var i = 0; i < state.Notes.Count; i++) + sb.AppendLine($"[{i + 1}] {state.Notes[i]}"); } // Line by line, only the empty paragraphs at the end go: the last line keeps its own spaces. return (lines ? sb.ToString().TrimEnd('\r', '\n') : sb.ToString().TrimEnd()) + Environment.NewLine; } + private static bool BreaksPage(HBlock block) => block is HParagraph { PageBreakBefore: true } or HTable { PageBreakBefore: true }; + /// The blocks for : every paragraph and table row ends with a line end. - private static void Lines(List blocks, StringBuilder sb, List notes, ListNumbers numbers) + private static void Lines(List blocks, StringBuilder sb, State state, ListNumbers numbers) { foreach (var block in blocks) { + if (BreaksPage(block)) + state.Page++; + // A paragraph that held only its page break is an empty line, as Word and LibreOffice write it: the reader + // left it out, as the layout does. + for (var i = 0; i < block.BreakParagraphsBefore; i++) + sb.AppendLine(); switch (block) { case HParagraph paragraph: { - var text = Inlines(paragraph.Inlines, notes, trim: false); + var text = Inlines(paragraph.Inlines, state, trim: false); if (paragraph.Inlines.Count > 0 && paragraph.Inlines[^1] is HTextBox) text = text.TrimEnd('\n'); // the box's own line end, not an empty line if (paragraph.Inlines.Any(i => i is HShape { Kind: HShapeKind.Line }) && text.Trim().Length == 0) text = "* * *"; if (paragraph.List is { } list) { - var marker = list.Numbered ? numbers.Next(list) + " " : ""; + var marker = Marker(list, numbers); var indent = new string(' ', list.Level * 4); sb.Append(indent).Append(marker) - .Append(text.Replace("\n", Environment.NewLine + indent + new string(' ', marker.Length))); + .Append(text.Replace("\n", Environment.NewLine + indent + Under(marker))); } else { @@ -72,22 +99,24 @@ private static void Lines(List blocks, StringBuilder sb, List no } case HTable { Rows.Count: > 0 } table: foreach (var row in table.Rows) - sb.AppendJoin('\t', row.Cells.Select(c => Cell(c, notes))).AppendLine(); + sb.AppendJoin('\t', row.Cells.Select(c => Cell(c, state))).AppendLine(); break; } } } - private static void Blocks(List blocks, StringBuilder sb, List notes, ListNumbers numbers) + private static void Blocks(List blocks, StringBuilder sb, State state, ListNumbers numbers) { HBlock? previous = null; foreach (var block in blocks) { + if (BreaksPage(block)) + state.Page++; switch (block) { case HParagraph paragraph: { - var text = Inlines(paragraph.Inlines, notes); + var text = Inlines(paragraph.Inlines, state); if (paragraph.Inlines.Any(i => i is HShape { Kind: HShapeKind.Line }) && text.Length == 0) text = "* * *"; if (text.Trim().Length == 0 && paragraph.List is null) @@ -102,9 +131,10 @@ private static void Blocks(List blocks, StringBuilder sb, List n var indent = Math.Max(0, paragraph.Format.Left ?? 0) / 1000; if (paragraph.List is { } list) { - var marker = list.Numbered ? numbers.Next(list) + " " : ""; - sb.Append(new string(' ', list.Level * 4 + indent)).Append(marker) - .Append(text.Replace("\n", Environment.NewLine + new string(' ', list.Level * 4 + indent + marker.Length))); + var marker = Marker(list, numbers); + var start = new string(' ', list.Level * 4 + indent); + sb.Append(start).Append(marker) + .Append(text.Replace("\n", Environment.NewLine + start + Under(marker))); } else { @@ -116,28 +146,49 @@ private static void Blocks(List blocks, StringBuilder sb, List n case HTable { Rows.Count: > 0 } table: if (sb.Length > 0) sb.AppendLine().AppendLine(); - sb.AppendJoin(Environment.NewLine, table.Rows.Select(row => string.Join('\t', row.Cells.Select(c => Cell(c, notes))))); + sb.AppendJoin(Environment.NewLine, table.Rows.Select(row => string.Join('\t', row.Cells.Select(c => Cell(c, state))))); previous = table; break; } } } + /// + /// The number or bullet of a list item and what follows it: a tab, or the space or nothing its Word list level asks + /// for; nothing for the paragraphs that continue an item. + /// + private static string Marker(HListRef list, ListNumbers numbers) + { + if (!list.Numbered) + return ""; + var levels = list.Numbering.Levels; + var suffix = levels.Count == 0 ? null : levels[Math.Min(list.Level, levels.Count - 1)].Suffix; + return numbers.Next(list) + suffix switch + { + HNumberSuffix.Space => " ", + HNumberSuffix.Nothing => "", + _ => "\t", + }; + } + + /// What goes under a marker on the lines after a line break: a space for each character, and its tab. + private static string Under(string marker) => string.Concat(marker.Select(c => c == '\t' ? '\t' : ' ')); + private static bool IsCode(HParagraph paragraph) => paragraph.Inlines.Count > 0 && paragraph.Inlines.All(i => i is HText { Format.Shade: HtmlReader.CodeShade }); /// A cell's text on one line: its paragraphs, and the cells of a table inside it, separated by a space. - private static string Cell(HCell cell, List notes) => string.Join(" ", CellTexts(cell.Blocks, notes)); + private static string Cell(HCell cell, State state) => string.Join(" ", CellTexts(cell.Blocks, state)); - private static IEnumerable CellTexts(List blocks, List notes) + private static IEnumerable CellTexts(List blocks, State state) { foreach (var block in blocks) { IEnumerable texts = block switch { - HParagraph p => [Inlines(p.Inlines, notes).Replace('\n', ' ').Replace('\t', ' ')], + HParagraph p => [Inlines(p.Inlines, state).Replace('\n', ' ').Replace('\t', ' ')], // A table in the cell (Word, ODT and RTF nest them); Nesting.Limit bounds how deep this goes. - HTable table => table.Rows.SelectMany(r => r.Cells).SelectMany(c => CellTexts(c.Blocks, notes)), + HTable table => table.Rows.SelectMany(r => r.Cells).SelectMany(c => CellTexts(c.Blocks, state)), _ => [], }; foreach (var text in texts) @@ -152,7 +203,7 @@ private static IEnumerable CellTexts(List blocks, List n /// The text of inlines, lines separated by '\n' (the callers write the line ends), without the white space at the /// end unless is false. /// - private static string Inlines(List inlines, List notes, bool trim = true) + private static string Inlines(List inlines, State state, bool trim = true) { var sb = new StringBuilder(); foreach (var inline in inlines) @@ -168,21 +219,25 @@ private static string Inlines(List inlines, List notes, bool tr case HTab: sb.Append('\t'); break; + case HField field: + sb.Append(field.Shown + ?? ListNumbers.Format(field.Kind == HFieldKind.TotalPages ? state.Pages : state.Page, field.NumberFormat ?? "DIGIT")); + break; case HLink link: - sb.Append(Inlines(link.Content, notes)); + sb.Append(Inlines(link.Content, state)); break; case HNote note: { var content = new StringBuilder(); - Blocks(note.Blocks, content, notes, new ListNumbers()); - notes.Add(content.ToString().Trim().Replace(Environment.NewLine + Environment.NewLine, " ")); - sb.Append('[').Append(notes.Count).Append(']'); + Blocks(note.Blocks, content, state, new ListNumbers()); + state.Notes.Add(content.ToString().Trim().Replace(Environment.NewLine + Environment.NewLine, " ")); + sb.Append('[').Append(state.Notes.Count).Append(']'); break; } case HTextBox box: { // A text box is text of its own: on its own lines, apart from the paragraph it is anchored in. - var lines = box.Blocks.OfType().Select(p => Inlines(p.Inlines, notes)).Where(t => t.Length > 0).ToList(); + var lines = box.Blocks.OfType().Select(p => Inlines(p.Inlines, state)).Where(t => t.Length > 0).ToList(); if (lines.Count == 0) break; if (sb.Length > 0 && sb[^1] != '\n') diff --git a/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs b/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs index 66bed6bd5..63455e782 100644 --- a/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs @@ -45,6 +45,9 @@ private enum Dest /// The other name of the font being defined (\falt). FontAlternate, + + /// The result of a PAGE or NUMPAGES field: the number it showed, kept as . + FieldNumber, } /// Where text and paragraphs go: the body of a section, a note, a header or a footer. @@ -105,6 +108,9 @@ private sealed class Group public List? Sink { get; set; } public FieldContext? Field { get; set; } + /// The page number field whose result is being read (). + public HField? Number { get; set; } + /// The note this group started is an endnote (\ftnalt). public bool Endnote { get; set; } @@ -123,6 +129,7 @@ private sealed class Group Story = Story, Sink = Sink, Field = Field, + Number = Number, }; } @@ -616,6 +623,9 @@ private void Decoded(string text) if (_fontAlternate.Length < 256) _fontAlternate.Append(text); break; + case Dest.FieldNumber when _g.Number is { } number && (number.Shown?.Length ?? 0) < 32: + number.Shown += text; + break; case Dest.FieldInstruction: _g.Field?.Instruction.Append(text); break; @@ -952,13 +962,10 @@ private void StartFieldResult(FieldContext field) _g.Link = _budget.Enter(); _g.Sink = link.Content; break; - case "PAGE": - AddInline(new HField(HFieldKind.PageNumber, Format(_g.Char))); - _g.Dest = Dest.Skip; - break; - case "NUMPAGES" or "SECTIONPAGES": - AddInline(new HField(HFieldKind.TotalPages, Format(_g.Char))); - _g.Dest = Dest.Skip; + case "PAGE" or "NUMPAGES" or "SECTIONPAGES": + _g.Number = new HField(name == "PAGE" ? HFieldKind.PageNumber : HFieldKind.TotalPages, Format(_g.Char)); + AddInline(_g.Number); + _g.Dest = Dest.FieldNumber; break; } } diff --git a/src/Filee.Engines/Office/LibreOfficeConverter.cs b/src/Filee.Engines/Office/LibreOfficeConverter.cs index 17af6d673..11e427f9c 100644 --- a/src/Filee.Engines/Office/LibreOfficeConverter.cs +++ b/src/Filee.Engines/Office/LibreOfficeConverter.cs @@ -444,11 +444,9 @@ void Add(IEnumerable sources, IEnumerable targets, int cost) // turn into DOCX (Excel → DOCX → LibreOffice would cost 17 + 19). The Filee renderer costs less (1 + 9): it // leaves a chosen kind of file to LibreOffice itself (FileeRendererConverter.Edges). const int Chosen = 10; + // Word, ODT and RTF → TXT are the fallback too: Filee's own readers write their text a line per paragraph, as + // LibreOffice does, and win wherever LibreOffice stands in the engine list (HwpTextConverter.LineCost). Add(Writer, [.. Writer, "txt", "html"], Fallback); - // RTF → TXT (#1204): the built-in routes read RTF through Pandoc, which makes a code span of every run in a - // monospace font (through Markdown, about 29) or loses the line breaks (through the HWPX writer, about 32). - // Direct only, so it stays the fallback as a step towards other formats. - edges.Add(new ConversionEdge("rtf", "txt", Chosen, DirectOnly: true)); Add(Writer, ["pdf"], documents ? Chosen : Fallback); Add(Calc, [.. Calc, "html"], Fallback); Add(Calc, ["pdf"], sheets ? Chosen : Fallback); diff --git a/tests/Filee.Engines.Tests/EbookTests.cs b/tests/Filee.Engines.Tests/EbookTests.cs index aa7043872..9ce7ef095 100644 --- a/tests/Filee.Engines.Tests/EbookTests.cs +++ b/tests/Filee.Engines.Tests/EbookTests.cs @@ -180,7 +180,7 @@ public async Task Epub_to_txt_html_and_markdown() var epub = EbookBuilders.Epub(fx.NewFolder()); var text = await File.ReadAllTextAsync(await ConvertAsync(epub, "txt"), TestContext.Current.CancellationToken); - foreach (var expected in new[] { "첫 번째 장", "밤하늘에 별이 반짝였다.", "주석 내용입니다.", "● 목록 하나", "이름\t값" }) + foreach (var expected in new[] { "첫 번째 장", "밤하늘에 별이 반짝였다.", "주석 내용입니다.", "●\t목록 하나", "이름\t값" }) // a tab after the bullet, as in Word's TXT Assert.Contains(expected, text); Assert.DoesNotContain("<", text); diff --git a/tests/Filee.Engines.Tests/FileeRendererConverterTests.cs b/tests/Filee.Engines.Tests/FileeRendererConverterTests.cs index c871f7edb..a2619b51e 100644 --- a/tests/Filee.Engines.Tests/FileeRendererConverterTests.cs +++ b/tests/Filee.Engines.Tests/FileeRendererConverterTests.cs @@ -7,6 +7,7 @@ using Filee.Core.Conversion; using Filee.Core.Presets; using Filee.Core.Settings; +using Filee.Engines.Hwp; using Filee.Engines.Infrastructure; using Filee.Engines.Office; using Filee.Engines.Typesetting; @@ -40,10 +41,11 @@ private void Use(bool renderer) /// /// The engines with the renderer on and a LibreOffice that is installed (or not), with the switches of Settings → - /// Engines → "When LibreOffice is installed", in the default engine order (or with LibreOffice moved to its top). + /// Engines → "When LibreOffice is installed", in the default engine order (or with LibreOffice moved to its top, or + /// to ). /// private RoutePlanner WithLibreOffice(bool slides = false, bool documents = false, bool sheets = false, bool installed = true, - bool libreOfficeFirst = false) + bool libreOfficeFirst = false, int? libreOfficeAt = null) { EngineEnvironment.FileeRenderer = true; EngineEnvironment.LibreOfficeForSlides = slides; @@ -56,7 +58,12 @@ private RoutePlanner WithLibreOffice(bool slides = false, bool documents = false converters.Add(new InstalledLibreOffice(LibreOfficeConverter.BuildEdges(slides, documents, sheets))); var priority = new AppSettings().EnginePriority; if (libreOfficeFirst) - priority = ["libreoffice", .. priority.Where(id => id != "libreoffice")]; + libreOfficeAt = 0; + if (libreOfficeAt is { } at) + { + priority = [.. priority.Where(id => id != "libreoffice")]; + priority.Insert(at, "libreoffice"); + } return new RoutePlanner(converters, c => Math.Min(priority.IndexOf(c.Id) is >= 0 and var i ? i : priority.Count, 9)); } @@ -118,11 +125,12 @@ public void On_each_kind_of_document_goes_to_pdf_in_one_step(string from, string [Fact] public void On_rtf_to_txt_reads_the_rtf_not_the_text_of_its_pdf() { - // #1204: through the PDF the text came out wrapped, without its indentation and its empty lines. + // #1204: through the PDF the text came out wrapped, without its indentation and its empty lines. With + // LibreOffice installed the built-in reader keeps it too: it writes the text line for line as LibreOffice does. Assert.Equal(["hwp-text"], Engines(WithLibreOffice(installed: false).Plan("rtf", "txt"))); - Assert.Equal(["libreoffice"], Engines(WithLibreOffice().Plan("rtf", "txt"))); - Assert.Equal(["libreoffice"], Engines(WithLibreOffice(documents: true).Plan("rtf", "txt"))); - Assert.Equal(["libreoffice"], Engines(WithLibreOffice(libreOfficeFirst: true).Plan("rtf", "txt"))); + Assert.Equal(["hwp-text"], Engines(WithLibreOffice().Plan("rtf", "txt"))); + Assert.Equal(["hwp-text"], Engines(WithLibreOffice(documents: true).Plan("rtf", "txt"))); + Assert.Equal(["hwp-text"], Engines(WithLibreOffice(libreOfficeFirst: true).Plan("rtf", "txt"))); } [Theory] @@ -137,22 +145,41 @@ public void On_rtf_to_txt_reads_the_rtf_not_the_text_of_its_pdf() public void On_word_and_odt_to_txt_read_the_document_not_the_text_of_its_pdf(string format) { // As RTF (#1204): through the PDF every long line was wrapped and the indentation and the empty lines were lost. - // LibreOffice installed at the bottom of the engine list, as by default, and chosen for Word → PDF, which is - // about PDF only, leave it to the built-in text too. - foreach (var planner in new[] { WithLibreOffice(installed: false), WithLibreOffice(), WithLibreOffice(documents: true) }) + // LibreOffice installed, wherever it stands in the engine list, and chosen for Word → PDF, which is about PDF + // only, leave it to the built-in text too. + foreach (var planner in new[] { WithLibreOffice(installed: false), WithLibreOffice(), WithLibreOffice(documents: true), WithLibreOffice(libreOfficeFirst: true) }) { var step = Assert.Single(planner.Plan(format, "txt")!.Steps); Assert.Equal("hwp-text", step.Converter.Id); // A Word 6.0 / 95 file with a .doc name goes another way. Assert.Equal(format is "doc" or "dot" or "wps", step.Checked); } - // Moved to the top of the engine list, LibreOffice keeps the Word and ODT files it reads. - var first = Engines(WithLibreOffice(libreOfficeFirst: true).Plan(format, "txt")); - Assert.Equal(format is "docm" or "dotx" or "dotm" ? ["hwp-text"] : ["libreoffice"], first); // LibreOffice has no DOCM, DOTX or DOTM edges + // A Word 6.0 / 95 file goes to LibreOffice. + if (format is "doc" or "dot" or "wps") + Assert.Equal(["libreoffice"], Engines(WithLibreOffice().Plan(format, "txt", withoutChecked: true))); // Not a hop: Word → EPUB and the other routes don't start going through a TXT file. Assert.DoesNotContain(WithLibreOffice(installed: false).Plan(format, "epub")?.Steps ?? [], s => s.Converter.Id == "hwp-text"); } + [Fact] + public void Built_in_text_wins_wherever_libreoffice_stands_in_the_engine_list() + { + // LibreOffice's fallback edge (25) against the built-in one (15): neither engine's place in the list (a penalty of + // 0 to 9) makes them tie, so the route doesn't depend on where the two stand. + var engines = new AppSettings().EnginePriority.Count; + for (var at = 0; at < engines; at++) + { + var planner = WithLibreOffice(libreOfficeAt: at); + foreach (var format in HwpTextConverter.LineFormats) + { + var route = planner.Plan(format, "txt")!; + Assert.Equal("hwp-text", Assert.Single(route.Steps).Converter.Id); + Assert.True(route.Cost < LibreOfficeConverter.BuildEdges(false).Where(e => e.From == format && e.To == "txt").Select(e => e.Cost).DefaultIfEmpty(25).Min(), + $"{format} with LibreOffice at {at}"); + } + } + } + [Fact] public void On_slide_decks_go_to_pdf_and_pictures_through_it() { diff --git a/tests/Filee.Engines.Tests/HwpTextTests.cs b/tests/Filee.Engines.Tests/HwpTextTests.cs index eb7007ed9..ea23fc5fc 100644 --- a/tests/Filee.Engines.Tests/HwpTextTests.cs +++ b/tests/Filee.Engines.Tests/HwpTextTests.cs @@ -112,8 +112,8 @@ public async Task Text_keeps_list_numbers_cells_and_notes_of_a_hangul_document() var hwpx = HwpxReaderTests.HangulStylePackage(fx.NewFolder()); var text = Read(await ConvertAsync(hwpx, "txt")); - Assert.Contains("1. 제1장 개요", text); // the outline number 한글 shows - Assert.Contains("● 글머리 항목", text); + Assert.Contains("1.\t제1장 개요", text); // the outline number 한글 shows, and a tab as in Word's and RTF's TXT + Assert.Contains("●\t글머리 항목", text); Assert.Contains("병합\t세로", text); // table rows as tab-separated cells Assert.Contains("각주 참조[1][2]", text); // notes collected at the end Assert.EndsWith("[1] 각주 내용\n[2] 미주 내용\n", text); diff --git a/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs b/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs index 29f23218c..facb8f2a9 100644 --- a/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs +++ b/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs @@ -74,9 +74,8 @@ public void Every_edge_has_a_filter_of_its_application() } // Built-in engines stay first: LibreOffice's edges cost more than a normal direct conversion, except // presentations → PDF, which it lays out like PowerPoint (Settings → Engines, on by default). - // RTF → TXT: RTF has no built-in reader, and every other route reads it through Pandoc (#1204). - Assert.All(LibreOffice.Edges.Where(e => !(impress.Contains(e.From) && e.To == "pdf") && !(e.From == "rtf" && e.To == "txt")), - e => Assert.True(e.Cost >= 25)); + // RTF, Word and ODT → TXT too (#1204): the built-in reader writes them line for line (HwpTextConverter.LineCost). + Assert.All(LibreOffice.Edges.Where(e => !(impress.Contains(e.From) && e.To == "pdf")), e => Assert.True(e.Cost >= 25)); foreach (var from in new[] { "dot", "wps", "wpd", "lwp", "abw", "pages", "sdw", "cwk", "mcw", "602", "psw", "fodt", "ott", "et", "numbers", "sdc", "fods", "ots", "xlt", "key", "dps", "sda", "fodp", "otp", "pub", "cdr", "cgm", "vsd", "odg", "fodg" }) Assert.Contains(LibreOffice.Edges, e => e.From == from && e.To == "pdf"); // Read-only formats are never targets. diff --git a/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs b/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs index c52e7802f..c88809c55 100644 --- a/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs +++ b/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs @@ -75,12 +75,13 @@ public void Word_and_excel_go_the_built_in_way_unless_libreoffice_is_chosen(stri } [Fact] - public void Rtf_goes_to_txt_through_libreoffice() + public void Rtf_goes_to_txt_through_the_built_in_reader() { - // Issue #1204: through Markdown, code copied from an IDE came out full of backticks. + // Issue #1204: through Markdown, code copied from an IDE came out full of backticks. LibreOffice took it until + // the built-in RTF reader wrote the text line for line as LibreOffice does; now that reader keeps it. var planner = Planner(slides: false); - Assert.Equal("libreoffice", Assert.Single(planner.Plan("rtf", "txt")!.Steps).Converter.Id); + Assert.Equal("hwp-text", Assert.Single(planner.Plan("rtf", "txt")!.Steps).Converter.Id); Assert.Equal("pandoc", Assert.Single(planner.Plan("rtf", "md")!.Steps).Converter.Id); Assert.DoesNotContain(planner.Plan("rtf", "epub")!.Steps, s => s.From == "rtf" && s.To == "txt"); // not a hop } diff --git a/tests/Filee.Engines.Tests/RtfToTextTests.cs b/tests/Filee.Engines.Tests/RtfToTextTests.cs index bc8e93b9f..299d55acb 100644 --- a/tests/Filee.Engines.Tests/RtfToTextTests.cs +++ b/tests/Filee.Engines.Tests/RtfToTextTests.cs @@ -156,6 +156,21 @@ public async Task Plain_code_keeps_its_tabs_spaces_empty_lines_and_line_breaks() Assert.Equal(string.Join("\n", expected), text); } + [Fact] + public async Task Page_fields_and_page_breaks_come_out_as_word_saved_them() + { + // The number Word showed (\fldrslt), not an empty field; a paragraph holding only \page is an empty line. + const string Rtf = """ + {\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}} + \pard Page {\field{\*\fldinst { PAGE }}{\fldrslt {\b 3}}} of {\field{\*\fldinst NUMPAGES}{\fldrslt 7}}\par + before\par + \page\par + after\par + } + """; + Assert.Equal("Page 3 of 7\nbefore\n\nafter\n", await ConvertAsync(Rtf)); + } + [Fact] public void Lists_and_tables_take_a_line_each() { @@ -179,7 +194,8 @@ public void Lists_and_tables_take_a_line_each() section.Blocks.Add(table); section.Blocks.Add(Paragraph(" end")); - Assert.Equal("Steps:\n1. one\n2. two\na\tb\nc\td\n end\n", PlainTextWriter.WriteLines(document).ReplaceLineEndings("\n")); + // A tab after the number, as Word and LibreOffice write it. + Assert.Equal("Steps:\n1.\tone\n2.\ttwo\na\tb\nc\td\n end\n", PlainTextWriter.WriteLines(document).ReplaceLineEndings("\n")); static HParagraph Paragraph(string text) { diff --git a/tests/Filee.Engines.Tests/WordToTextTests.cs b/tests/Filee.Engines.Tests/WordToTextTests.cs index 15d8978f7..d3ca17bbf 100644 --- a/tests/Filee.Engines.Tests/WordToTextTests.cs +++ b/tests/Filee.Engines.Tests/WordToTextTests.cs @@ -1,6 +1,7 @@ // Word (DOCX, DOC) and ODT → TXT on the built-in route, as RTF → TXT (#1204, RtfToBuiltInTextTests): the document's -// own paragraphs a line each, with their leading spaces, tabs, runs of spaces and empty paragraphs, list numbers and -// table rows on lines of their own. Before, with the Filee renderer on, the text came from the renderer's PDF. The +// own paragraphs a line each, with their leading spaces, tabs, runs of spaces and empty paragraphs, list numbers (and +// the tab after them, as Word and LibreOffice write it) and table rows on lines of their own, page numbers as the file +// showed them and a paragraph that holds only a page break as an empty line. Before, with the Filee renderer on, the text came from the renderer's PDF. The // routes with LibreOffice are in FileeRendererConverterTests. The documents are built here. using Filee.Core.Conversion; @@ -21,9 +22,9 @@ public sealed class WordToBuiltInTextTests(EngineFixture fx) : IClassFixture" + R("iv") + "")) + .Save(Path.Combine(fx.NewFolder(), "pages.docx")); + Assert.Equal("Page 3 of 7\nSimple iv\n", await ConvertAsync(docx, "docx")); + + var odt = new OdtBuilder + { + Body = """ + Page 3 of 7 + Placeholder <number> + """, + }.Save(Path.Combine(fx.NewFolder(), "pages.odt")); + // A field without a number of its own (Impress's placeholder) is counted from the page breaks. + Assert.Equal("Page 3 of 7\nPlaceholder 1\n", await ConvertAsync(odt, "odt")); + } + + [Fact] + public void A_page_field_without_a_number_counts_the_page_breaks_and_sections() + { + var document = new HDocument(); + var first = new HSection(); + first.Blocks.Add(Paragraph("a", new HField(HFieldKind.PageNumber, default))); + var broken = Paragraph("b", new HField(HFieldKind.PageNumber, default), new HText(" of ", default), new HField(HFieldKind.TotalPages, default)); + broken.PageBreakBefore = true; + first.Blocks.Add(broken); + var second = new HSection { StartPageNumber = 10 }; + second.Blocks.Add(Paragraph("c", new HField(HFieldKind.PageNumber, default) { NumberFormat = "ROMAN_SMALL" })); + document.Sections.AddRange([first, second]); + + Assert.Equal("a1\nb2 of 3\ncx\n", PlainTextWriter.WriteLines(document).ReplaceLineEndings("\n")); + Assert.Equal("a1\n\nb2 of 3\n\ncx\n", PlainTextWriter.Write(document).ReplaceLineEndings("\n")); + + static HParagraph Paragraph(string text, params HInline[] inlines) + { + var paragraph = new HParagraph(); + paragraph.Inlines.Add(new HText(text, default)); + paragraph.Inlines.AddRange(inlines); + return paragraph; + } + } + + [Fact] + public async Task A_paragraph_that_holds_only_a_page_break_is_an_empty_line() + { + // Word keeps the paragraph's mark on the old page and the layout leaves it out, but its plain text has the line, + // as LibreOffice's has. No form feed: neither writes one. + const string PageBreak = ""; + var docx = new DocxBuilder() + .Paragraph(P(R("before"))) + .Paragraph(P(PageBreak)) + .Paragraph(P(R("after"))) + .Paragraph(P(R("end of a page") + PageBreak)) // the break ends the paragraph: its mark is an empty line too + .Paragraph(P(R("next"))) + .Paragraph(P(PageBreak)) + .Paragraph(P(PageBreak)) + .Paragraph(P(R("two pages on"))) + .Paragraph(P(R("in the") + PageBreak + R("middle"))) // no empty line + .Save(Path.Combine(fx.NewFolder(), "breaks.docx")); + var text = await ConvertAsync(docx, "docx"); + + Assert.Equal("before\n\nafter\nend of a page\n\nnext\n\n\ntwo pages on\nin the\nmiddle\n", text); + Assert.DoesNotContain('\f', text); + // The layout's model is as it was: the break paragraphs are left out, the next paragraph starts the page. + var model = Hwp.Hwpx.Docx.DocxReader.Read(docx, fx.NewFolder()); + var paragraphs = model.Sections.Single().Blocks.OfType().ToList(); + Assert.Equal(["before", "after", "end of a page", "next", "two pages on", "in the", "middle"], + paragraphs.Select(p => string.Concat(p.Inlines.OfType().Select(t => t.Text)))); + Assert.Equal([0, 1, 0, 1, 2, 0, 0], paragraphs.Select(p => p.BreakParagraphsBefore)); + } + + [Fact] + public async Task A_list_number_is_followed_by_what_its_word_level_asks_for() + { + var docx = new DocxBuilder + { + Numbering = """ + + + + + + """, + }; + static string Item(string text, int level) => P(R(text), $""); + docx.Paragraph(Item("spaced", 0)).Paragraph(Item("tight", 1)).Paragraph(P(R("line") + "" + R("break"), + "")); + var source = docx.Save(Path.Combine(fx.NewFolder(), "suffix.docx")); + + Assert.Equal("1. spaced\n (a)tight\n2. line\n break\n", await ConvertAsync(source, "docx")); + } + + [Fact] + public void A_line_break_in_a_list_item_lines_up_under_its_text() + { + var numbering = new HNumbering { Levels = { new HNumberingLevel("DIGIT", "^1.", 1, Bullet: false) } }; + var item = new HParagraph { List = new HListRef(numbering, 0, Numbered: true) }; + item.Inlines.AddRange([new HText("first", default), new HLineBreak(default), new HText("second", default)]); + var document = new HDocument(); + document.Sections.Add(new HSection { Blocks = { item } }); + + // The tab under the number's tab reaches the same tab stop in an editor. + Assert.Equal("1.\tfirst\n \tsecond\n", PlainTextWriter.WriteLines(document).ReplaceLineEndings("\n")); + } + [Fact] public void A_table_in_a_cell_keeps_its_text_in_the_cell() { From b5b652826e6107834eddb6c7aa96539a5f010ad8 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 22:27:55 +0900 Subject: [PATCH 2/7] Word layout: compat-mode table indent, wide fixed tables, cell spacing, list numbers, page border order - Word 2010-and-older documents (compatibilityMode < 15, or none given) measure tblInd to the first cell's text: the table's edge sits a cell margin further out (layout-only HTable.EdgeBeforeIndent); tblInd also comes from the table style. - A fixed table in the body wider than its column keeps its widths and runs into the margin (both margins when centred) instead of being narrowed. - w:tblCellSpacing: cells inset on every side, the table's border drawn around them; w:wBefore / w:wAfter size the grid columns a row leaves empty; RTF rows with \trautofit1 (and \trwWidth) are sized as Word's autofit tables. - List numbers: w:lvlJc center/right, the number in the paragraph mark's format with the level's w:rPr over it (bullets keep their mapped character's font), and tab stops between a hanging first line's start and the left indent. - Page borders go over the text unless w:zOrder="back". - In columns of their own widths, a table that goes on in a column of another width is laid out again for it from its next row group (list numbers restored). Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 20 +- .../Hwp/Hwpx/Docx/DocxReader.Styles.cs | 46 +++- .../Hwp/Hwpx/Docx/DocxReader.Tables.cs | 39 +++- src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs | 33 ++- src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs | 57 ++++- .../Hwp/Hwpx/HwpxWriter.Header.cs | 3 +- src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs | 12 ++ .../Hwp/Hwpx/RtfReader.Tables.cs | 44 +++- src/Filee.Engines/Typesetting/PageComposer.cs | 60 +++++- .../Typesetting/ParagraphLayout.cs | 16 +- .../Typesetting/ParagraphText.cs | 3 +- src/Filee.Engines/Typesetting/TableLayout.cs | 168 +++++++++++---- tests/Filee.Engines.Tests/DocxBuilder.cs | 4 + .../WordListLayoutTests.cs | 197 ++++++++++++++++++ .../WordTableWidthTests.cs | 175 +++++++++++++++- 15 files changed, 797 insertions(+), 80 deletions(-) create mode 100644 tests/Filee.Engines.Tests/WordListLayoutTests.cs diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 72f35b6f9..71c9e1478 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -1341,6 +1341,9 @@ in `src/Filee.Engines/Typesetting`. No converter uses it yet; the render quality - 한글's numbers: a list number in its own character format (paraHead charPrIDRef), the lines after it hanging under the text when the number indents its paragraph (autoIndent); a note's reference in its run's format (an exam's endnote numbers are white); a page that hides its header or footer (hp:pageHiding) shows none. + - Word's numbers: in the paragraph mark's run format with the level's `w:rPr` over it (a bullet keeps the mark's + font, its character being mapped to Unicode), starting at, centred on or ending at the first line's indent + (`w:lvlJc`); a tab stop between a hanging first line's start and the left indent counts on that line. - Headers and footers: both, odd or even pages by page number, none on a section's first page where it hides them, laid out in the header and footer areas. Page numbers count on across sections unless one restarts them; page number fields show their page's number, total pages fields the count (a first pass finds it when the body has one). @@ -1369,7 +1372,7 @@ in `src/Filee.Engines/Typesetting`. No converter uses it yet; the render quality (hideBorder, hideFill) leave them out. Word's page borders (w:pgBorders) take the same place: w:space points in from the paper's edges (offsetFrom="page") or out from the text and around the header and footer areas, on all pages, the first or all but the first (w:display); line styles it doesn't draw become a single or double line, - picture borders a single line. + picture borders a single line. Word draws its page border over the text and objects unless w:zOrder="back". - **Columns, footnotes and endnotes** (`PageComposer.cs`, GitHub Project 4 item *Layout engine: multi-column layout, footnotes and endnotes*): - Columns: a section's newspaper columns at their gap, with a separator line between them when the section has one; @@ -1377,9 +1380,9 @@ in `src/Filee.Engines/Typesetting`. No converter uses it yet; the render quality that started a column (the stored first-line-of-column flag) start one. - Columns of their own widths (Word w:cols equalWidth="0", 한글 sameSz="0", ODF style:column rel-width): each its width and the gap after it, scaled to the text width, the first at the right in a right-to-left section. A - paragraph going on in a column of another width is laid out anew from where it broke; they are not balanced - where columns change on a page (their lines are as wide as the column they were laid out for), and a table going - on in one keeps its first column's width. + paragraph going on in a column of another width is laid out anew from where it broke, a table from its next group + of rows (a row split across the columns keeps its first part's widths); they are not balanced where columns change + on a page (their lines are as wide as the column they were laid out for). - Columns changing on a page (한글 다단 설정 나누기, Word's continuous section break): the columns before are balanced, the first ones taking the odd line as 한글 does, and the new ones start under them. At the end of a section they are left as they are (neither 한글 nor Word balances there). @@ -1408,9 +1411,12 @@ in `src/Filee.Engines/Typesetting`. No converter uses it yet; the render quality spanning columns widens them together) and the table to its preferred width (`w:tblW`, a length or a share of the text column). A table wider than its room (from its indent to the column's edge, or its cell for a nested table, which never overflows it) gives up first what its columns have beyond their widest line, then down to their longest - words. Cell margins nothing gives are Word's 0.19 cm at the sides. Not yet: the indent of Word 2010 and older - documents measured to the cell's text (the table sits a cell margin further left there), `w:tblCellSpacing`, - `w:gridBefore` / `w:gridAfter` widths. + words; a fixed table in the body keeps its widths and runs into the margin (both when centred). Cell margins + nothing gives are Word's 0.19 cm at the sides. Grid columns a row leaves empty take its `w:wBefore` / `w:wAfter`; + cells spaced apart (`w:tblCellSpacing`) sit that far in from their columns and rows, the table's border around + them. Documents in Word 2010's compatibility mode or older (and those whose settings don't say) measure `w:tblInd` + to the first cell's text, so the table sits a cell margin further out. RTF rows with `\trautofit1` are sized as + Word's autofit tables. - Cells: padding (the cell's, the table's, else 한글's 1.8 × 0.5 mm), content laid out at the column width less it, nested tables included; lines on 한글's stored layout take their stored place in the cell. A row is as high as its own height or its tallest single-row cell, whose height is its content without the spacing below its last line (as diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.cs index e29201610..0095af0ca 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.cs @@ -289,8 +289,11 @@ private sealed class NumberingLevel public int Start = 1; public string Format = "decimal"; public string Text = "%1."; - public string? Suffix; + public string? Suffix, Jc; public XElement? PPr, RPr; + + /// The level's w:rPr without its fonts (a bullet keeps the text's: its character is mapped to Unicode). + public XElement? RPrWithoutFonts; } // ───────────────────────── Loading ───────────────────────── @@ -416,6 +419,7 @@ static void ApplyLevel(NumberingLevel[] levels, XElement lvl) level.Format = Val(lvl.Element(W + "numFmt")) ?? level.Format; level.Text = Val(lvl.Element(W + "lvlText")) ?? level.Text; level.Suffix = Val(lvl.Element(W + "suff")) ?? level.Suffix; + level.Jc = Val(lvl.Element(W + "lvlJc")) ?? level.Jc; level.PPr = lvl.Element(W + "pPr") ?? level.PPr; level.RPr = lvl.Element(W + "rPr") ?? level.RPr; } @@ -598,9 +602,10 @@ private HParaFormat ParaFormat(ParaProps p, int? fontSize) // Left to right unless w:bidi says otherwise (null: left to the style, which is left to right). RightToLeft: p.Bidi, Borders: ParagraphBorders(p), - // Word measures tab stops from the left margin, 한글 from the paragraph's left indent. + // Word measures tab stops from the left margin, 한글 from the paragraph's left indent. Stops between a + // hanging first line's start and the left indent count on that line (before the indent: negative). Tabs: p.Tabs.Count == 0 ? null : p.Tabs - .Where(t => t.Kind is not ("bar" or "num") && t.Position > (left ?? 0)) + .Where(t => t.Kind is not ("bar" or "num") && t.Position != (left ?? 0) && t.Position > (left ?? 0) + Math.Min(0, firstLine)) .Select(t => new HTabStop(HwpxUnits.FromTwips(t.Position - (left ?? 0)), t.Kind switch { "right" or "end" => HTabKind.Right, @@ -738,12 +743,45 @@ _ when theme.StartsWith("major", StringComparison.Ordinal) => theme.EndsWith("Ea var definition = bullet ? new HNumberingLevel("DIGIT", BulletText(level.Text, LevelFont(level.RPr)), level.Start, Bullet: true) : new HNumberingLevel(NumberFormat(level.Format), NumberText(level.Text, level.Format), level.Start, Bullet: false); - numbering.Levels.Add(definition with { Suffix = suffix }); + numbering.Levels.Add(definition with + { + Suffix = suffix, + Align = level.Jc switch + { + "center" => HAlign.Center, + "right" or "end" => HAlign.Right, + _ => null, + }, + }); } _numberings[numId] = numbering; return numbering; } + /// + /// The format Word draws a list number in: the paragraph mark's run format () with the + /// level's w:rPr over it. A bullet keeps the mark's fonts, as its character is mapped from the symbol font to Unicode. + /// + private HCharFormat? MarkerFormat(string? numId, int ilvl, RunProps mark) + { + if (numId is null || !_numberingLevels.TryGetValue(numId, out var levels) || ilvl < 0 || ilvl >= levels.Length) + return null; + var level = levels[ilvl]; + var rPr = level.RPr; + if (level.Format == "bullet" && rPr?.Element(W + "rFonts") is not null) + { + if (level.RPrWithoutFonts is null) + { + level.RPrWithoutFonts = new XElement(rPr); + level.RPrWithoutFonts.Elements(W + "rFonts").Remove(); + } + rPr = level.RPrWithoutFonts; + } + var run = mark.Clone(); + run.Apply(rPr); + return CharFormat(run); + } + private static string NumberFormat(string wordFormat) => wordFormat switch { "upperRoman" => "ROMAN_CAPITAL", diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs index 9d2415b40..fa81e7e16 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs @@ -2,8 +2,11 @@ // row, banded rows ... formatting), the table and each cell, cell shading, cell margins, vertical alignment, row // heights, repeated header rows and rows that do not split across pages. The table style's paragraph and run // formatting reaches the text in the cells. What the layout sizes the columns by (HTableSizing): fixed or autofit -// (w:tblLayout), the table's preferred width (w:tblW), the grid and each cell's preferred width (w:tcW); cell margins -// none of the table, its style or the cell give are Word's 0.19 cm left and right, none at the top and bottom. +// (w:tblLayout), the table's preferred width (w:tblW), the grid, each cell's preferred width (w:tcW) and the widths of +// the grid columns a row leaves empty (w:gridBefore / w:wBefore, w:gridAfter / w:wAfter); cell margins none of the +// table, its style or the cell give are Word's 0.19 cm left and right, none at the top and bottom. Cells spaced apart +// (w:tblCellSpacing) keep the table's outer border around them. A document in Word 2010's compatibility mode (or older) +// measures w:tblInd to the first cell's text, so the table's edge is that cell's start margin further out. using System.Globalization; using System.Xml.Linq; @@ -32,7 +35,7 @@ private HTable ReadTable(XElement tbl) "right" or "end" => HAlign.Right, _ => HAlign.Left, }, - Indent = HwpxUnits.FromTwips(Twips(tblPr?.Element(W + "tblInd"), "w") ?? 0), + Indent = HwpxUnits.FromTwips(tableProperties.Select(p => Twips(p.Element(W + "tblInd"), "w")).LastOrDefault(w => w is not null) ?? 0), // A right-to-left table: its first grid column is the rightmost, and jc, tblInd and the cells' left / right // borders and margins name the start and the end side (the layout mirrors them). RightToLeft = styleChain.Select(s => s.TblPr).Append(tblPr).OfType() @@ -60,6 +63,15 @@ private HTable ReadTable(XElement tbl) table.Borders = HBorders.Empty; var rows = tbl.Elements().SelectMany(e => e.Name == W + "tr" ? [e] : FlattenRows(e)).ToList(); + // Cells spaced apart: the table's, else its first row's spacing; the table's outer border then goes around them. + var spacing = tableProperties.Select(p => CellSpacing(p.Element(W + "tblCellSpacing"))).LastOrDefault(v => v is not null) + ?? CellSpacing(rows.FirstOrDefault()?.Element(W + "trPr")?.Element(W + "tblCellSpacing")) ?? 0; + if (spacing > 0) + { + table.CellSpacing = spacing; + table.Frame = new HBorders(borders.GetValueOrDefault("left", HBorder.None), borders.GetValueOrDefault("right", HBorder.None), + borders.GetValueOrDefault("top", HBorder.None), borders.GetValueOrDefault("bottom", HBorder.None)); + } var conditions = TableConditions(tblPr, styleChain, rows.Count, grid.Length); // vMerge: the cell that restarts a vertical merge, per grid column. @@ -79,6 +91,14 @@ private HTable ReadTable(XElement tbl) // Column numbers and spans of damaged files stay within the limit, so they can't overflow. var maxColumns = _budget.Limits.MaxTableColumns; var column = Math.Clamp(Twips(trPr?.Element(W + "gridBefore"), "val") ?? 0, 0, maxColumns); + if (column > 0) + row.WidthBefore = PreferredWidth(trPr?.Element(W + "wBefore")); + var after = Math.Clamp(Twips(trPr?.Element(W + "gridAfter"), "val") ?? 0, 0, maxColumns); + if (after > 0) + { + row.GridAfter = after; + row.WidthAfter = PreferredWidth(trPr?.Element(W + "wAfter")); + } foreach (var tc in tr.Elements().SelectMany(e => e.Name == W + "tc" ? [e] : FlattenCells(e))) { var tcPr = tc.Element(W + "tcPr"); @@ -158,8 +178,13 @@ [.. styleChain.Select(s => s.PPr).Concat(cellConditions.Select(c => c.Element(W column = Math.Min(column + span, maxColumns); } table.Rows.Add(row); - table.ColumnCount = Math.Max(table.ColumnCount, column); + table.ColumnCount = Math.Max(table.ColumnCount, Math.Min(column + after, maxColumns)); } + // Word 2010 and older (and Word in their compatibility mode) measure w:tblInd to the text of the first cell, not + // to the table's edge: the edge sits the first cell's start margin before it, in the margin at an indent of 0. + if (_compatibilityMode < 15 && table.Float is null && table.Align == HAlign.Left + && table.Rows.FirstOrDefault(r => r.Cells.Count > 0)?.Cells[0] is { } first) + table.EdgeBeforeIndent = Math.Max(0, (first.CellMargin ?? table.CellMargin ?? WordCellMargin).Left); return table; } @@ -340,6 +365,12 @@ private static HBorder Border(XElement edge) } } + /// A cell spacing (w:tblCellSpacing, twips) in HWPUNIT: null when the element is missing, 0 for nil or none. + private static int? CellSpacing(XElement? element) => + element is null ? null + : Attr(element, "type") is "nil" or "auto" or "pct" ? 0 + : HwpxUnits.FromTwips(Math.Clamp(Twips(element, "w") ?? 0, 0, 31680)); + private static HInsets CellMargins(XElement cellMar, HInsets? current) { int Get(string name, string alternative, int fallback) diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs index ceb7acbac..29dba0fe7 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs @@ -7,6 +7,7 @@ // DocxReader.Tables and DocxReader.Drawing handle tables and graphic objects. The package is read within // DocumentLimits (Documents/DocumentPackage); embedded fonts (word/fonts) are not read. +using System.Globalization; using System.IO.Compression; using System.Text; using System.Xml; @@ -57,6 +58,12 @@ internal sealed partial class DocxReader /// Neighbouring paragraphs keep the larger of their spaces between them, not both (see ). private bool _collapseSpacing = true; + /// + /// Word's compatibility mode (settings w:compatSetting compatibilityMode): 15 for Word 2013 and later, 14 or less for + /// documents kept in the way of Word 2010 and older, 12 (Word 2007) when the settings don't say. + /// + private int _compatibilityMode = 12; + /// Width of each section's text column (HWPUNIT), for equations that don't fit a line. private readonly List _columnWidths = []; @@ -229,6 +236,7 @@ private HDocument ReadDocument() _bordersAroundHeader = !(settings?.Element(W + "bordersDoNotSurroundHeader") is { } noHeader && Val(noHeader) is not ("0" or "false")); _bordersAroundFooter = !(settings?.Element(W + "bordersDoNotSurroundFooter") is { } noFooter && Val(noFooter) is not ("0" or "false")); _collapseSpacing = !(settings?.Element(W + "compat")?.Element(W + "doNotUseHTMLParagraphAutoSpacing") is { } sum && Val(sum) is not ("0" or "false")); + _compatibilityMode = CompatibilityMode(settings); _footnotesPart = RelatedPart(main, "/footnotes"); _endnotesPart = RelatedPart(main, "/endnotes"); foreach (var note in _footnotesPart?.Xml.Root?.Elements(W + "footnote") ?? []) @@ -476,6 +484,20 @@ private void SnapToGrid(HParagraph paragraph, ParaProps props) return kind == HGridKind.None ? null : new HDocumentGrid(kind, HwpxUnits.FromTwips(Twips(grid, "linePitch") ?? 0), CharSpace: Twips(grid, "charSpace") ?? 0); } + /// + /// The compatibility mode the settings give (w:compat/w:compatSetting name="compatibilityMode", Word's own uri), or + /// 12 when they give none: Word opens such a document as a Word 2007 one. + /// + private static int CompatibilityMode(XElement? settings) + { + foreach (var setting in settings?.Element(W + "compat")?.Elements(W + "compatSetting") ?? []) + { + if (Attr(setting, "name") == "compatibilityMode" && int.TryParse(Attr(setting, "val"), NumberStyles.Integer, CultureInfo.InvariantCulture, out var mode)) + return Math.Clamp(mode, 0, 1000); + } + return 12; + } + /// Custom East Asian line breaking characters of the document (settings: w:noLineBreaksBefore / After). private static void Kinsoku(XElement? settings, HDocument document) { @@ -662,7 +684,11 @@ private void PageBorders(XElement? sectPr, HSection section) var border = new HPageBorder(HPageType.Both, new HBorders(left, right, top, bottom), Fill: null, Picture: null, new HInsets(leftSpace, rightSpace, topSpace, bottomSpace), AroundBody: Attr(borders, "offsetFrom") != "page", - HeaderInside: _bordersAroundHeader, FooterInside: _bordersAroundFooter, HPageFillArea.Paper); + HeaderInside: _bordersAroundHeader, FooterInside: _bordersAroundFooter, HPageFillArea.Paper) + { + // Over the text unless the border says it goes behind it. + InFront = Attr(borders, "zOrder") != "back", + }; if (border.IsEmpty) return; section.PageBorders.Add(border); @@ -908,7 +934,10 @@ public ParagraphBuilder(DocxReader reader, List output, ParaProps props, _format = reader.ParaFormat(props, mark.Size); _mark = reader.CharFormat(mark); _heading = reader.HeadingLevel(props, pPr); - _list = reader.Numbering(props.NumId) is { } numbering ? new HListRef(numbering, Math.Clamp(props.Ilvl ?? 0, 0, 8), Numbered: true) : null; + var ilvl = Math.Clamp(props.Ilvl ?? 0, 0, 8); + _list = reader.Numbering(props.NumId) is { } numbering + ? new HListRef(numbering, ilvl, Numbered: true) { MarkerFormat = reader.MarkerFormat(props.NumId, ilvl, mark) } + : null; Current = NewParagraph(_list); Current.PageBreakBefore = props.PageBreakBefore == true; diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs index 3ee141b45..c8876e71e 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs @@ -169,6 +169,12 @@ internal enum HPageFillArea internal sealed record HPageBorder(HPageType Pages, HBorders Borders, string? Fill, string? Picture, HInsets Offsets, bool AroundBody, bool HeaderInside, bool FooterInside, HPageFillArea FillArea) { + /// + /// The border's lines go over the page's text and objects (Word w:pgBorders zOrder="front", its default), else + /// under them with the background. Only the layout engine uses it. + /// + public bool InFront { get; init; } + /// Draws nothing. public bool IsEmpty => Fill is null && Picture is null && Borders == HBorders.Empty; } @@ -343,7 +349,20 @@ internal sealed class HParagraph : HBlock /// List definition (shared by all items of one list). /// 0-based nesting level. /// False for continuation paragraphs of an item (no bullet or number). -internal sealed record HListRef(HNumbering Numbering, int Level, bool Numbered); +internal sealed record HListRef(HNumbering Numbering, int Level, bool Numbered) +{ + /// + /// The character format of this paragraph's number (Word: the paragraph mark's run format with the level's w:rPr + /// over it); null: the level's , else the text's. Only the layout engine + /// uses it, so it takes no part in equality. + /// + public HCharFormat? MarkerFormat { get; init; } + + public bool Equals(HListRef? other) => + other is not null && ReferenceEquals(Numbering, other.Numbering) && Level == other.Level && Numbered == other.Numbered; + + public override int GetHashCode() => HashCode.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(Numbering), Level, Numbered); +} /// A list definition: one entry per nesting level. internal sealed class HNumbering @@ -368,6 +387,12 @@ internal sealed record HNumberingLevel(string Format, string Text, int Start, bo /// public HNumberSuffix? Suffix { get; init; } + /// + /// How the number lines up with its place, the first line's indent (Word w:lvlJc): it starts there (null, left), + /// centres on it or ends there. Only the layout engine uses it, so it takes no part in equality. + /// + public HAlign? Align { get; init; } + public bool Equals(HNumberingLevel? other) => other is not null && Format == other.Format && Text == other.Text && Start == other.Start && Bullet == other.Bullet && MarkerFormat == other.MarkerFormat && AutoIndent == other.AutoIndent; @@ -397,6 +422,12 @@ internal sealed class HTable : HBlock public HAlign Align { get; set; } = HAlign.Left; public int Indent { get; set; } + /// + /// How far the table's edge lies before (HWPUNIT): a Word 2010 (or older) document measures its + /// indent to the first cell's text, so the edge is that cell's start margin further out. Only the layout engine uses it. + /// + public int EdgeBeforeIndent { get; set; } + /// /// The columns run from the right (Word w:bidiVisual, RTF \rtlrow): the first column is the rightmost, and /// , and each cell's left and right borders and padding name the start and @@ -465,6 +496,18 @@ internal sealed class HTable : HBlock /// The distance a floating table keeps from the text around it (w:leftFromText ...). public HInsets FloatDistance { get; set; } + /// + /// Space around each cell, inside the table (Word w:tblCellSpacing), in HWPUNIT: cells this far from the table's edge + /// and twice this far from each other; 0 = cells touch. Only the layout engine uses it. + /// + public int CellSpacing { get; set; } + + /// + /// The table's own outer border, drawn around its cells when they are spaced apart (); null = + /// none. Only the layout engine uses it. + /// + public HBorders? Frame { get; set; } + public List Caption { get; } = []; } @@ -504,6 +547,18 @@ internal sealed class HRow /// The row is not split across pages (Word w:cantSplit). public bool CantSplit { get; set; } + + /// + /// The width of the grid columns the row leaves empty before its first cell (Word w:gridBefore with w:wBefore); null + /// when it gives none. Only the layout engine uses it. + /// + public HPreferredWidth? WidthBefore { get; set; } + + /// Grid columns the row leaves empty after its last cell (Word w:gridAfter). Only the layout engine uses it. + public int GridAfter { get; set; } + + /// The width of those columns (w:wAfter); null when it gives none. Only the layout engine uses it. + public HPreferredWidth? WidthAfter { get; set; } } internal sealed class HCell diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.Header.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.Header.cs index b357a9bb5..0d66257a4 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.Header.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.Header.cs @@ -512,7 +512,8 @@ private string TabPrId(IReadOnlyList tabs) var container = RefList("tabProperties"); var id = (MaxId(container.Elements(Hh + "tabPr")) + 1).ToString(CultureInfo.InvariantCulture); var element = new XElement(Hh + "tabPr", new XAttribute("id", id), new XAttribute("autoTabLeft", 0), new XAttribute("autoTabRight", 0)); - foreach (var tab in tabs) + // 한글's stops lie right of the left indent; a Word stop on a hanging first line before it is left out. + foreach (var tab in tabs.Where(t => t.Position > 0)) { var type = tab.Kind switch { diff --git a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs index dcbe49e5e..1ed6e0307 100644 --- a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs @@ -226,6 +226,18 @@ public string Next(HListRef item) return text; } + /// The counters as they are now, to go back to () before laying something out again. + public IReadOnlyDictionary Save() => + _counters.ToDictionary(p => p.Key, p => (int[])p.Value.Clone(), (IEqualityComparer)ReferenceEqualityComparer.Instance); + + /// Puts the counters back as found them. + public void Restore(IReadOnlyDictionary saved) + { + _counters.Clear(); + foreach (var (numbering, counters) in saved) + _counters[numbering] = (int[])counters.Clone(); + } + /// The marker would give, without counting the item (to measure it ahead). public string Peek(HListRef item) { diff --git a/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs b/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs index 2be0e76c0..5be70045d 100644 --- a/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs +++ b/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs @@ -2,7 +2,8 @@ // // A table is a run of rows: \trowd starts a row's definition (its cells' right edges \cellxN, merges, shading and // borders), the paragraphs marked \intbl fill its cells, \cell ends a cell and \row the row. The columns of the -// table are every cell edge of every row, so rows with other divisions keep their cells where they are. Nested +// table are every cell edge of every row, so rows with other divisions keep their cells where they are. A first row +// with \trautofit1 makes the layout size the columns as Word's autofit tables (HTableSizing, with \trwWidth). Nested // tables (\itap > 1) are read as the paragraphs of their outer cell. // // Lists come from \listtable (levels with their number format and \leveltext) through \listoverridetable (\lsN → @@ -44,6 +45,13 @@ private sealed class RowDef /// \rtlrow: the cells run from the right. public bool RightToLeft { get; set; } + /// \trautofit1: the columns fit their content (Word's autofit); else they keep their \cellx widths. + public bool AutoFit { get; set; } + + /// The row's preferred width (\trwWidth) and its unit (\trftsWidth: 2 fiftieths of a percent, 3 twips). + public int Width { get; set; } + public int WidthUnit { get; set; } + /// The cells, shared with the rows before that kept this definition; copied when it changes. public List Cells { get; private set; } = []; private bool _shared; @@ -60,7 +68,18 @@ public void Add(CellDef cell) } /// The definition of the next row when it doesn't give its own: this one's, sharing its cells. - public RowDef Next() => new() { Left = Left, Height = Height, Align = Align, RightToLeft = RightToLeft, Cells = Cells, _shared = true }; + public RowDef Next() => new() + { + Left = Left, + Height = Height, + Align = Align, + RightToLeft = RightToLeft, + AutoFit = AutoFit, + Width = Width, + WidthUnit = WidthUnit, + Cells = Cells, + _shared = true, + }; } private sealed class TableBuilder @@ -108,6 +127,15 @@ private bool Table(string word, int? parameter) case "rtlrow" or "ltrrow": TableOf(story).Def.RightToLeft = word == "rtlrow"; return true; + case "trautofit": + TableOf(story).Def.AutoFit = (parameter ?? 1) != 0; + return true; + case "trwWidth": + TableOf(story).Def.Width = value; + return true; + case "trftsWidth": + TableOf(story).Def.WidthUnit = value; + return true; case "clmgf": TableOf(story).Def.Pending.Horizontal = Merge.First; return true; @@ -248,6 +276,18 @@ private void CloseTable(Story story) }; if (table.RightToLeft && builder.Rows[0].Def.Align is HAlign.Left or HAlign.Right) table.Align = builder.Rows[0].Def.Align == HAlign.Left ? HAlign.Right : HAlign.Left; + // \trautofit1: Word sizes the columns as its autofit tables, from the \cellx widths and the cells' content (and + // the preferred width); without it they keep their widths, as the other formats' tables. + if (builder.Rows[0].Def is { AutoFit: true } autoFit) + { + var width = autoFit switch + { + { WidthUnit: 3, Width: > 0 } => new HPreferredWidth(HWidthType.Absolute, HwpxUnits.FromTwips(Math.Min(autoFit.Width, 31680))), + { WidthUnit: 2, Width: > 0 } => new HPreferredWidth(HWidthType.Percent, Math.Min(10, autoFit.Width / 5000.0)), + _ => HPreferredWidth.Auto, + }; + table.Sizing = new HTableSizing(false, width, table.ColumnWidths ?? []); + } var openVertical = new Dictionary(); foreach (var (def, contents) in builder.Rows) { diff --git a/src/Filee.Engines/Typesetting/PageComposer.cs b/src/Filee.Engines/Typesetting/PageComposer.cs index a5f6b2ad5..f43e2446d 100644 --- a/src/Filee.Engines/Typesetting/PageComposer.cs +++ b/src/Filee.Engines/Typesetting/PageComposer.cs @@ -16,7 +16,8 @@ // break, or from the break's page when the break starts it. Word models: the footer grows up from the bottom of its // area, and a header or footer taller than its area moves the body (see Dress). // - Master pages (바탕쪽): drawn behind the page's body (or in front of it) from the top of the body; see MasterPages. -// - Page borders and backgrounds (쪽 테두리/배경) under everything else; see PageBorder. +// - Page borders and backgrounds (쪽 테두리/배경) under everything else; a Word page border in front of the text (its +// default, w:pgBorders zOrder) over everything instead. See PageBorder. // - Paragraph borders and backgrounds (BoxBorder): consecutive paragraphs with the same border share one box, which // takes room above its first line and below its last, is drawn under the text a line at a time and stays open where a // page or column break splits it. @@ -413,6 +414,8 @@ public ComposedDocument Compose() // objects behind the text too, as Word draws them. HeadersAndFooters(page); DrawObjects(page); + // A page border in front of the text (Word's default) goes over everything on the page. + PageBorder(page, front: true); } return new ComposedDocument([.. _pages.Select(p => p.Content)], _outline); } @@ -488,11 +491,14 @@ private void Body(List blocks) _guard.Paragraph(); if (blocks[i] is HTable table) { + // In columns of their own widths the table may go on in a column of another width and be laid out + // again: its list numbers then count from where they were before it. + var numbers = next is null && _columns.Sizes is not null ? _numbers.Save() : null; var tableBox = next as TableBox ?? Tables.Layout(table, ColumnWidth); next = null; if (holder is null && table.Float is { } anchor && Float(table, anchor, tableBox, blocks, i, ref next)) continue; - PlaceTable(table, tableBox, holder, holder is null ? TableLayout.Follower(blocks, i) : null); + PlaceTable(table, tableBox, holder, holder is null ? TableLayout.Follower(blocks, i) : null, numbers); holder = null; continue; } @@ -591,7 +597,11 @@ private void Body(List blocks) /// is the 한글 paragraph whose line the table is, or null. /// /// The rest of the 한글 paragraph the table starts, whose text follows right under it. - private void PlaceTable(HTable table, TableBox box, HParagraph? holder, HParagraph? follower) + /// + /// The list counters before the table was laid out: in columns of their own widths, a table going on in a column + /// of another width is laid out again for it from there (its rows from the next group on). Null keeps its widths. + /// + private void PlaceTable(HTable table, TableBox box, HParagraph? holder, HParagraph? follower, IReadOnlyDictionary? numbers = null) { if (table.PageBreakBefore && !_empty) HangulPage(); @@ -626,7 +636,7 @@ private void PlaceTable(HTable table, TableBox box, HParagraph? holder, HParagra // else the stored places hold again on 한글's next page. _inStep = _inStep && TableLayout.AsStored(table, box.Height, line); // A table set as a character is centred or set right with its outer margins, like a character that wide. - var x = box.Offset + (table.AsCharacter && !table.RightToLeft + var shift = (table.AsCharacter && !table.RightToLeft ? table.Align switch { HAlign.Center => (margin.Left - margin.Right) / 2f, @@ -634,10 +644,26 @@ private void PlaceTable(HTable table, TableBox box, HParagraph? holder, HParagra _ => margin.Left, } : margin.Left) / 100f; + var x = box.Offset + shift; var header = box.Rows.Take(box.HeaderRows).ToList(); + // A column of another width than the one the table was laid out for (columns of their own widths): the + // table is laid out again for it, its list numbers counted from where they were before it. + bool Refit() + { + if (numbers is null || _columns.Sizes is null || Math.Abs(box.Column - ColumnWidth) < Epsilon) + return false; + _numbers.Restore(numbers); + box = Tables.Layout(table, ColumnWidth); + x = box.Offset + shift; + header = [.. box.Rows.Take(box.HeaderRows)]; + return true; + } // A table that must not split goes to the next column whole when it fits in one. if (box.PageBreak == HTablePageBreak.None && !_columnEmpty && _y + box.Height > Bottom + Epsilon && box.Height <= ColumnHeight) + { NextColumn(); + Refit(); + } // The column holds nothing yet but, perhaps, this table's repeated header rows. var fresh = _columnEmpty; @@ -647,15 +673,19 @@ void Draw(IReadOnlyList rows) Put(x, height, (page, left, top) => TableLayout.Draw(page, rows, left, top)); _y += height; } - void NextPage(bool repeat) + // On in the next column or page: true when the table was laid out again for it. + bool NextPage(bool repeat) { NextColumn(); _inStep = false; + var refitted = Refit(); if (repeat && header.Count > 0) Draw(header); fresh = true; + return refitted; } + // The groups come from the table itself (cells spanning rows), so laid out again it has the same ones. foreach (var (first, count) in box.Groups()) { IReadOnlyList rows = [.. box.Rows.Skip(first).Take(count)]; @@ -675,6 +705,7 @@ void NextPage(bool repeat) var splits = rows.Count == 1 && !rows[0].CantSplit && box.PageBreak is not (HTablePageBreak.Cell or HTablePageBreak.None); if ((splits || fresh) && rows.Count == 1 && TableLayout.Split(rows[0], room) is var (part, rest)) { + // The rest of a split row keeps the widths its first part had. Draw([part]); NextPage(repeat); rows = [rest]; @@ -682,7 +713,8 @@ void NextPage(bool repeat) } if (!fresh) { - NextPage(repeat); + if (NextPage(repeat)) + rows = [.. box.Rows.Skip(first).Take(count)]; continue; } // Taller than an empty column and it can't split: placed as it is. @@ -1357,7 +1389,11 @@ private void Endnotes() /// draws something, else the one for both pages. 한글 places the border around the body grown by its offsets /// (around the header and footer areas too when they are inside), or its offsets in from the paper's edges. /// - private void PageBorder(Page page) + /// + /// Draws the page's border and background under everything ( false: the background, and the + /// border unless it goes in front), or the border that goes in front of the text over everything (true). + /// + private void PageBorder(Page page, bool front = false) { var section = page.Section; static bool Shows(HFirstPageRule rule, bool first) => rule switch @@ -1369,8 +1405,9 @@ private void PageBorder(Page page) var side = page.Number % 2 == 0 ? HPageType.Even : HPageType.Odd; if ((section.PageBorders.LastOrDefault(b => b.Pages == side) ?? section.PageBorders.LastOrDefault(b => b.Pages == HPageType.Both)) is not { } border) return; - var showBorder = Shows(section.PageBorderShows, page.FirstOfSection) && page.Hiding?.Border != true; - var showFill = Shows(section.PageFillShows, page.FirstOfSection) && page.Hiding?.Fill != true; + var showBorder = Shows(section.PageBorderShows, page.FirstOfSection) && page.Hiding?.Border != true && border.InFront == front; + var showFill = Shows(section.PageFillShows, page.FirstOfSection) && page.Hiding?.Fill != true && !front + && (border.Fill is not null || border.Picture is not null); if (!showBorder && !showFill) return; @@ -1412,7 +1449,7 @@ private void PageBorder(Page page) if (showBorder) TableLayout.Borders(canvas, border.Borders, frame); }); - page.Content.Add(layer, under: true); + page.Content.Add(layer, under: !front); } // ───────────────────────── Master pages ───────────────────────── @@ -1670,7 +1707,8 @@ private HParagraph Resolve(HParagraph paragraph, HBlock? next = null) Format = format, MarkFormat = _styles.Mark(paragraph) is var mark && code ? _styles.Character(paragraph.MarkFormat with { Font = paragraph.MarkFormat.Font ?? FontNames.MonospaceFamily }, paragraph) : mark, HeadingLevel = paragraph.HeadingLevel, - List = paragraph.List, + // A number's own format (Word's) is resolved as the text's. + List = paragraph.List is { MarkerFormat: { } marker } list ? list with { MarkerFormat = _styles.Character(marker, paragraph) } : paragraph.List, PageBreakBefore = paragraph.PageBreakBefore, ColumnBreakBefore = paragraph.ColumnBreakBefore, ColumnsChange = paragraph.ColumnsChange, diff --git a/src/Filee.Engines/Typesetting/ParagraphLayout.cs b/src/Filee.Engines/Typesetting/ParagraphLayout.cs index e2b1678f5..5fd673c1d 100644 --- a/src/Filee.Engines/Typesetting/ParagraphLayout.cs +++ b/src/Filee.Engines/Typesetting/ParagraphLayout.cs @@ -36,7 +36,9 @@ // neutral characters of a right-to-left run (Word w:rtl) read right to left, as Word reads them. A right-to-left // paragraph starts at the right: its alignment and indents name the start and end side (HParaFormat.RightToLeft). // Tab positions are measured from the paragraph's left indent, as 한글 measures them; a hanging indent is a tab stop -// on the first line (the text after a list number starts there). Default tab stops are 한글's, every 40 pt from the +// on the first line (the text after a list number starts there), and stops between the first line's start and the left +// indent count on that line only. A Word number centred on or ending at its place (w:lvlJc) starts the first line that +// much earlier. Default tab stops are 한글's, every 40 pt from the // left indent, or Word's, every w:defaultTabStop from the margin. // // 본 제품은 한글과컴퓨터의 한/글 문서 파일(.hwp) 공개 문서를 참고하여 개발하였습니다. @@ -319,13 +321,21 @@ private Prepared Prepare(HParagraph paragraph, float width, string? marker, Func HangPunctuation: breaking?.OverflowPunctuation == true, WordTabInterval: DefaultTabStop is > 0 and var interval ? interval / 100f : 0, TabIntervalFromIndent: DefaultTabFromIndent); + var listLevel = marker is { Length: > 0 } && paragraph.List is { Numbering.Levels: var levels, Level: var level } && level >= 0 && level < levels.Count + ? levels[level] : null; // A number that indents its paragraph (한글 autoIndent): the lines after the first hang under the text after it. - if (marker is { Length: > 0 } && paragraph.List is { Numbering.Levels: var levels, Level: var level } && level >= 0 && level < levels.Count - && levels[level].AutoIndent && marker.Length + 1 <= text.Length) + if (listLevel is { AutoIndent: true } && marker!.Length + 1 <= text.Length) { var hang = advances.AsSpan(0, marker.Length + 1).ToArray().Sum(); settings = settings with { Left = settings.Left + hang, FirstLine = settings.FirstLine - hang }; } + // A number centred on or ending at its place (Word w:lvlJc): the first line starts that much earlier, so the + // number is where Word draws it and what follows it goes to the stops it would from a number starting there. + if (listLevel is { Align: HAlign.Center or HAlign.Right } && marker!.Length <= text.Length) + { + var number = advances.AsSpan(0, marker.Length).ToArray().Sum(); + settings = settings with { FirstLine = settings.FirstLine - (listLevel.Align == HAlign.Center ? number / 2 : number) }; + } return new Prepared(format, source, language, border, items, advances, starts, breaking, rules, breaks, settings); } diff --git a/src/Filee.Engines/Typesetting/ParagraphText.cs b/src/Filee.Engines/Typesetting/ParagraphText.cs index c76c30f9d..250821d39 100644 --- a/src/Filee.Engines/Typesetting/ParagraphText.cs +++ b/src/Filee.Engines/Typesetting/ParagraphText.cs @@ -199,7 +199,8 @@ void Inlines(IEnumerable inlines, string? link) if (marker is { Length: > 0 }) { var listLevel = paragraph.List is { Numbering.Levels: var levels, Level: var level } && level >= 0 && level < levels.Count ? levels[level] : null; - var format = listLevel?.MarkerFormat ?? paragraph.Inlines.OfType().FirstOrDefault()?.Format ?? paragraph.MarkFormat; + // The paragraph's own number format (Word's), the level's (한글's), else the text's. + var format = paragraph.List?.MarkerFormat ?? listLevel?.MarkerFormat ?? paragraph.Inlines.OfType().FirstOrDefault()?.Format ?? paragraph.MarkFormat; // Word's level says what follows the number; 한글 puts a tab after a hanging number, else a space. Add(marker + (listLevel?.Suffix switch { diff --git a/src/Filee.Engines/Typesetting/TableLayout.cs b/src/Filee.Engines/Typesetting/TableLayout.cs index 1c89f63db..ea10c8265 100644 --- a/src/Filee.Engines/Typesetting/TableLayout.cs +++ b/src/Filee.Engines/Typesetting/TableLayout.cs @@ -15,16 +15,21 @@ // is narrower; a cell spanning columns spreads what it needs over them. A table wider than its room (from its indent // to the far edge: the text column, or the cell a nested table is in, which it never overflows) gives up first what // its columns have beyond their widest line, then down to their longest words, then shrinks evenly; a fixed table -// outside cells shrinks evenly at once, keeping its shape (Word lets it run into the margin). Content widths +// outside cells keeps its widths and runs into the margin (both margins when centred), as Word lets it. Content widths // come from ParagraphLayouter.ContentWidths, once per table (nested tables measure as the widths they need), for -// its first MaxMeasuredCells cells. +// its first MaxMeasuredCells cells. Grid columns a row leaves empty before or after its cells (w:gridBefore, +// w:gridAfter) take the width the row gives them (w:wBefore, w:wAfter) as a cell's preferred width. The indent is to +// the table's edge, or to the first cell's text in Word 2010's compatibility mode (HTable.EdgeBeforeIndent); a Word +// table may start in the margin. // - Cells: their blocks are laid out at the columns' width less the padding (the cell's, else the table's, else // 한글's default 1.8 mm by 0.5 mm), nested tables too; a row is as high as its tallest single-row cell or its own // height, whichever is more, and a cell spanning rows makes its last row higher when the rows are short of it. A // 한글 cell holds its content down to its last line's text; a Word cell (WordCells) holds the whole last line and // the space after its last paragraph too, as Word sizes rows. // Content sits at the top, middle or bottom of the cell. Paragraph borders and backgrounds in a stack (a cell, a -// header) are drawn as in the body (BoxBorder), each line with its part of the box. +// header) are drawn as in the body (BoxBorder), each line with its part of the box. Cells spaced apart (Word's +// w:tblCellSpacing) sit that far in from their grid columns and their row on every side (twice that between two +// cells), measured with it, and the table's outer border (HTable.Frame) is drawn around them. // - Right to left (Word w:bidiVisual, RTF \rtlrow): the first column is the rightmost, and the table's alignment and // indent and each cell's left and right borders and padding name the start and the end side, so they change places. // - Drawing: each cell's fill (a colour, a picture; EMF / WMF as vector drawing), then its content, then its borders (the cell's, else the table's, else a thin grid); @@ -82,10 +87,16 @@ internal sealed class CellBox /// The height of the content the cell holds and centres: Word's down to , 한글's . public float Held => Extent is { } extent ? Math.Max(ContentHeight, extent) : InkHeight; - /// The height the cell needs: the content it holds and its padding. - public float Needed => Held + (Padding.Top + Padding.Bottom) / 100f; + /// The space between the cell and its row's top and bottom (Word's cell spacing; across it is in X and Width). + public float Spacing { get; init; } + + /// The height the cell needs: the content it holds, its padding and the space above and below it. + public float Needed => Held + (Padding.Top + Padding.Bottom) / 100f + 2 * Spacing; } +/// A table's outer border around cells spaced apart, wide (Word's tblBorders with tblCellSpacing). +internal sealed record TableFrame(HBorders Borders, float Width); + /// A row: its height and the cells that start in it. internal sealed class RowBox { @@ -93,12 +104,20 @@ internal sealed class RowBox public required IReadOnlyList Cells { get; init; } public bool Header { get; init; } public bool CantSplit { get; init; } + + /// The table's border around spaced cells, drawn at the row's sides; its top over the first row, its bottom under the last. + public TableFrame? Frame { get; init; } + public bool FrameTop { get; init; } + public bool FrameBottom { get; init; } } /// A laid out table. /// Left edge from the column's left edge. internal sealed record TableBox(float Offset, float Width, IReadOnlyList Rows, HTablePageBreak? PageBreak, IReadOnlyList Caption) { + /// The width of the column the table was laid out in. + public float Column { get; init; } + public float Height => Rows.Sum(r => r.Height); /// Rows bound together by cells spanning them, as (first, count). @@ -330,6 +349,8 @@ public TableBox Layout(HTable table, float width) for (var r = 0; r < rowCount; r++) cells[r] = []; var mirror = table.RightToLeft; + // Cells spaced apart (Word's cell spacing) sit this far in from their grid columns and their row on every side. + var spacing = Math.Max(0, table.CellSpacing / 100f); foreach (var (row, column, cell) in placed) { var span = Math.Clamp(cell.ColSpan, 0, columns - column); @@ -340,7 +361,7 @@ public TableBox Layout(HTable table, float width) padding = padding with { Left = padding.Right, Right = padding.Left }; borders = borders with { Left = borders.Right, Right = borders.Left }; } - var cellWidth = left[column + span] - left[column]; + var cellWidth = spacing > 0 ? Math.Max(0, left[column + span] - left[column] - 2 * spacing) : left[column + span] - left[column]; var contentWidth = Math.Max(1, cellWidth - (padding.Left + padding.Right) / 100f); List content; float extent; @@ -357,8 +378,9 @@ public TableBox Layout(HTable table, float width) { Row = row, RowSpan = Math.Max(1, Math.Min(cell.RowSpan, rowCount - row)), - X = mirror ? tableWidth - left[column + span] : left[column], + X = (mirror ? tableWidth - left[column + span] : left[column]) + spacing, Width = cellWidth, + Spacing = spacing, Padding = padding, VerticalAlign = cell.VerticalAlign, Borders = borders, @@ -371,11 +393,23 @@ public TableBox Layout(HTable table, float width) // Row heights: the row's own, its single-row cells; then the cells spanning rows stretch their last row. var rows = new List(rowCount); + var outerFrame = spacing > 0 && table.Frame is { } outer + ? new TableFrame(mirror ? outer with { Left = outer.Right, Right = outer.Left } : outer, tableWidth) + : null; for (var r = 0; r < rowCount; r++) { var source = table.Rows[r]; var height = Math.Max((source.Height ?? 0) / 100f, cells[r].Where(c => c.RowSpan == 1).Select(c => c.Needed).DefaultIfEmpty(0).Max()); - rows.Add(new RowBox { Height = height, Cells = cells[r], Header = source.Header, CantSplit = source.CantSplit }); + rows.Add(new RowBox + { + Height = height, + Cells = cells[r], + Header = source.Header, + CantSplit = source.CantSplit, + Frame = outerFrame, + FrameTop = r == 0, + FrameBottom = r == rowCount - 1, + }); } // The rows' heights are summed through a Fenwick tree, so a table of many cells spanning many rows costs a // logarithm per cell rather than its rows. @@ -397,13 +431,18 @@ public TableBox Layout(HTable table, float width) (HAlign.Center, _) => (width - tableWidth) / 2, (HAlign.Right, false) => width - tableWidth, (HAlign.Right, true) => 0, - (_, true) => width - tableWidth - table.Indent / 100f, - _ => table.Indent / 100f, + (_, true) => width - tableWidth - EdgeIndent(table), + _ => EdgeIndent(table), }; var caption = table.Caption.Count > 0 ? Stack(table.Caption, tableWidth) : []; - return new TableBox(Math.Max(0, offset), tableWidth, rows, table.PageBreak, caption); + // A Word table may start in the margin (a negative indent, Word 2010's indent to the cell's text) and run into + // both margins when it is centred and wider than the column; other formats' tables start at the column's edge. + return new TableBox(table.Sizing is null ? Math.Max(0, offset) : offset, tableWidth, rows, table.PageBreak, caption) { Column = width }; } + /// Where a table's edge is from the column's start side, in points: its indent, less what lies before it (HTable.EdgeBeforeIndent). + private static float EdgeIndent(HTable table) => (table.Indent - table.EdgeBeforeIndent) / 100f; + /// /// Columns a table has at most (): cells of a damaged or hostile model /// that start or reach further are cut there. @@ -598,14 +637,14 @@ private sealed record ContentWidths(float[] Min, float[] Max); private float[] WordColumnWidths(HTable table, HTableSizing sizing, List<(int Row, int Column, HCell Cell)> placed, int columns, float width) { // The room the table has: from its indent to the column's far edge (a negative indent reaches into the margin). - var room = Math.Max(1, table.Align == HAlign.Left ? width - table.Indent / 100f : width); + var room = Math.Max(1, table.Align == HAlign.Left ? width - EdgeIndent(table) : width); float? tableWidth = sizing.Width.Type switch { HWidthType.Absolute => (float)(sizing.Width.Value / 100), HWidthType.Percent => (float)(sizing.Width.Value * width), _ => null, }; - var widths = Preferred(sizing, placed, columns, tableWidth ?? room); + var widths = Preferred(table, sizing, placed, columns, tableWidth ?? room); var open = widths.Contains(0f); var content = !sizing.Fixed || open || (_inCell > 0 && widths.Sum() > room + 0.5f) ? Measure(table, placed, columns) : null; if (content is { } measured) @@ -638,45 +677,62 @@ private float[] WordColumnWidths(HTable table, HTableSizing sizing, List<(int Ro widths[c] *= scale; total = widths.Sum(); } - // A fixed table in the body keeps its shape, narrowed evenly as other formats' tables (Word lets it run into the - // margin); one in a cell, and an autofit table, gives up what its content doesn't need first. - if (total > room + 0.5f) - Shrink(widths, sizing.Fixed && _inCell == 0 ? null : content ?? Measure(table, placed, columns), room); + // A fixed table in the body keeps its widths and runs into the margin, as Word lets it; one in a cell, and an + // autofit table, gives up what its content doesn't need first. + if (total > room + 0.5f && !(sizing.Fixed && _inCell == 0)) + Shrink(widths, content ?? Measure(table, placed, columns), room); return widths; } /// /// Each column's preferred width in points: the grid's, else the widest a cell of the column gives (a cell spanning - /// only such columns spreads its own over them), else 0. + /// only such columns spreads its own over them), else 0. The grid columns a row leaves empty before its first cell + /// and after its last take the width the row gives them (Word's w:wBefore, w:wAfter) the same way. /// /// What a cell's share of the table's width is a share of. - private static float[] Preferred(HTableSizing sizing, List<(int Row, int Column, HCell Cell)> placed, int columns, float basis) + private static float[] Preferred(HTable table, HTableSizing sizing, List<(int Row, int Column, HCell Cell)> placed, int columns, float basis) { var widths = new float[columns]; for (var c = 0; c < Math.Min(columns, sizing.Grid.Length); c++) widths[c] = Math.Max(0, sizing.Grid[c]) / 100f; var gridded = widths.Select(w => w > 0).ToArray(); - var spanning = new List<(int Column, int Span, float Width)>(); - foreach (var (_, column, cell) in placed) + float? Given(HPreferredWidth? width) => width switch { - float? given = cell.Width switch - { - { Type: HWidthType.Absolute, Value: var length } => (float)(length / 100), - { Type: HWidthType.Percent, Value: var share } => (float)(share * basis), - _ => null, - }; - if (given is not > 0 || column >= columns) + { Type: HWidthType.Absolute, Value: var length } => (float)(length / 100), + { Type: HWidthType.Percent, Value: var share } => (float)(share * basis), + _ => null, + }; + var given = new List<(int Column, int Span, float Width)>(); + // Each row's first column and the column after its last cell. + var extents = new Dictionary(); + foreach (var (row, column, cell) in placed) + { + if (column >= columns) continue; var span = Math.Clamp(cell.ColSpan, 1, columns - column); - if (span > 1) - spanning.Add((column, span, given.Value)); - else if (!gridded[column]) - widths[column] = Math.Max(widths[column], given.Value); + extents[row] = extents.TryGetValue(row, out var known) ? (Math.Min(known.First, column), Math.Max(known.End, column + span)) : (column, column + span); + if (Given(cell.Width) is > 0 and var width) + given.Add((column, span, width)); + } + foreach (var (row, (first, end)) in extents) + { + if (row >= table.Rows.Count) + continue; + var source = table.Rows[row]; + if (first > 0 && Given(source.WidthBefore) is > 0 and var before) + given.Add((0, first, before)); + if (source.GridAfter > 0 && end < columns && Given(source.WidthAfter) is > 0 and var after) + given.Add((end, Math.Min(source.GridAfter, columns - end), after)); } - foreach (var (column, span, given) in spanning.OrderBy(s => s.Span)) + foreach (var (column, _, width) in given.Where(g => g.Span == 1)) + { + if (!gridded[column]) + widths[column] = Math.Max(widths[column], width); + } + foreach (var (column, span, width) in given.Where(g => g.Span > 1).OrderBy(g => g.Span)) { if (!gridded.AsSpan(column, span).Contains(true)) - Spread(widths, column, span, given); + Spread(widths, column, span, width); } return widths; } @@ -696,7 +752,7 @@ private static float[] Preferred(HTableSizing sizing, List<(int Row, int Column, if (column >= columns) continue; var padding = cell.CellMargin ?? table.CellMargin ?? DefaultPadding; - var sides = Math.Max(0, (padding.Left + padding.Right) / 100f); + var sides = Math.Max(0, (padding.Left + padding.Right) / 100f) + 2 * Math.Max(0, table.CellSpacing / 100f); var (least, natural) = Measure(cell.Blocks); var span = Math.Clamp(cell.ColSpan, 1, columns - column); if (span > 1) @@ -747,7 +803,7 @@ private static float[] Preferred(HTableSizing sizing, List<(int Row, int Column, var placed = Place(table); var columns = Math.Max(1, Math.Max(Math.Min(table.ColumnCount, MaxColumns), placed.Count == 0 ? 0 : placed.Max(c => c.Column + ColumnSpan(c.Column, c.Cell)))); var margin = table.OuterMargin ?? default; - var around = Math.Max(0, (margin.Left + margin.Right) / 100f) + Math.Max(0, table.Indent / 100f); + var around = Math.Max(0, (margin.Left + margin.Right) / 100f) + Math.Max(0, EdgeIndent(table)); if (table.Sizing is not { } sizing) { var given = table.ColumnWidths is { } absolute ? absolute.Take(columns).Sum(w => Math.Max(0, w) / 100f) : 0; @@ -755,7 +811,7 @@ private static float[] Preferred(HTableSizing sizing, List<(int Row, int Column, } var content = Measure(table, placed, columns); var length = sizing.Width.Type == HWidthType.Absolute ? (float)(sizing.Width.Value / 100) : 0; - var preferred = Preferred(sizing, placed, columns, length); + var preferred = Preferred(table, sizing, placed, columns, length); float min = 0, max = 0; for (var c = 0; c < columns; c++) { @@ -831,7 +887,7 @@ public static (RowBox First, RowBox Remaining)? Split(RowBox row, float availabl var any = false; foreach (var cell in row.Cells) { - var room = available - (cell.Padding.Top + cell.Padding.Bottom) / 100f; + var room = available - (cell.Padding.Top + cell.Padding.Bottom) / 100f - 2 * cell.Spacing; var fits = cell.Content.Where(p => p.Top + p.Height <= room + 0.01f).ToList(); var after = cell.Content.Skip(fits.Count).ToList(); any |= fits.Count > 0; @@ -841,8 +897,24 @@ public static (RowBox First, RowBox Remaining)? Split(RowBox row, float availabl } if (!any) return null; - var firstRow = new RowBox { Height = available, Cells = first, Header = row.Header, CantSplit = row.CantSplit }; - var restRow = new RowBox { Height = rest.Select(c => c.Needed).DefaultIfEmpty(0).Max(), Cells = rest, Header = row.Header, CantSplit = row.CantSplit }; + var firstRow = new RowBox + { + Height = available, + Cells = first, + Header = row.Header, + CantSplit = row.CantSplit, + Frame = row.Frame, + FrameTop = row.FrameTop, + }; + var restRow = new RowBox + { + Height = rest.Select(c => c.Needed).DefaultIfEmpty(0).Max(), + Cells = rest, + Header = row.Header, + CantSplit = row.CantSplit, + Frame = row.Frame, + FrameBottom = row.FrameBottom, + }; return (firstRow, restRow); } @@ -859,6 +931,7 @@ public static (RowBox First, RowBox Remaining)? Split(RowBox row, float availabl FillPicture = cell.FillPicture, Content = content, Extent = extent, + Spacing = cell.Spacing, }; // ───────────────────────── Drawing ───────────────────────── @@ -876,7 +949,7 @@ public static void Draw(PageContent page, IReadOnlyList rows, float x, f foreach (var cell in rows[r].Cells) { var bottom = tops[Math.Min(rows.Count, r + cell.RowSpan)]; - cells.Add((cell, new SKRect(x + cell.X, tops[r], x + cell.X + cell.Width, bottom))); + cells.Add((cell, new SKRect(x + cell.X, tops[r] + cell.Spacing, x + cell.X + cell.Width, Math.Max(tops[r] + cell.Spacing, bottom - cell.Spacing)))); } } var fills = cells.Where(c => c.Cell.Fill is not null || c.Cell.FillPicture is not null).ToList(); @@ -926,6 +999,19 @@ public static void Draw(PageContent page, IReadOnlyList rows, float x, f { foreach (var (cell, area) in cells) Borders(canvas, cell.Borders, area); + // The table's own border around cells spaced apart: at each row's sides, over the first and under the last. + for (var r = 0; r < rows.Count; r++) + { + if (rows[r].Frame is not { } around) + continue; + var (left, right) = (x, x + around.Width); + Line(canvas, around.Borders.Left, left, tops[r], left, tops[r + 1]); + Line(canvas, around.Borders.Right, right, tops[r], right, tops[r + 1]); + if (rows[r].FrameTop) + Line(canvas, around.Borders.Top, left, tops[r], right, tops[r]); + if (rows[r].FrameBottom) + Line(canvas, around.Borders.Bottom, left, tops[r + 1], right, tops[r + 1]); + } }); } diff --git a/tests/Filee.Engines.Tests/DocxBuilder.cs b/tests/Filee.Engines.Tests/DocxBuilder.cs index bc7163d8b..24f9184e5 100644 --- a/tests/Filee.Engines.Tests/DocxBuilder.cs +++ b/tests/Filee.Engines.Tests/DocxBuilder.cs @@ -49,6 +49,10 @@ internal sealed class DocxBuilder public string? Numbering { get; set; } public string? Settings { get; set; } + /// Settings of a document Word 2013 or later saved (compatibility mode 15), for . + public const string Word2013Settings = + ""; + /// Content of w:fonts (word/fontTable.xml: w:font elements), or null for none. public string? FontTable { get; set; } diff --git a/tests/Filee.Engines.Tests/WordListLayoutTests.cs b/tests/Filee.Engines.Tests/WordListLayoutTests.cs new file mode 100644 index 000000000..1113c3694 --- /dev/null +++ b/tests/Filee.Engines.Tests/WordListLayoutTests.cs @@ -0,0 +1,197 @@ +// Word list numbers, first-line tab stops and page borders in the layout engine: a number centred on or ending at its +// place (w:lvlJc), a tab stop between a hanging first line's start and the left indent, the number's own format (the +// paragraph mark's with the level's w:rPr over it) and a page border in front of or behind the text (w:pgBorders +// zOrder). DOCX built in code, read by DocxReader and composed as the renderer composes Word documents; laid out with +// the Noto fonts of build/fetch-fonts.ps1 (RepositoryFonts), without which the layout tests skip. + +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Hwp.Hwpx.Docx; +using Filee.Engines.Rendering; +using Filee.Engines.Typesetting; +using static Filee.Engines.Tests.DocxBuilder; + +namespace Filee.Engines.Tests; + +public sealed class WordListLayoutTests : IDisposable +{ + /// The left margin of in points. + private const float Margin = 72; + + private readonly RepositoryFonts _fonts = RepositoryFonts.Load(); + private readonly Shaper _shaper = new(); + private readonly string _folder = Directory.CreateTempSubdirectory("filee-word-lists").FullName; + + public void Dispose() + { + _shaper.Dispose(); + try + { + Directory.Delete(_folder, recursive: true); + } + catch (IOException) + { + } + } + + // ───────────────────────── Number alignment ───────────────────────── + + [Fact] + public void A_number_starts_centres_on_or_ends_at_its_place() + { + // The number's place is the first line's start, 36 pt in (left 1440 less hanging 720); the text at the left + // indent, the hanging indent's stop, 72 pt in, whatever the alignment. + var item = Item("Item", ""); + var left = ComposeOne(item, Numbering("decimal", "%1.", "left")); + var centre = ComposeOne(item, Numbering("decimal", "%1.", "center")); + var right = ComposeOne(item, Numbering("decimal", "%1.", "right")); + + Assert.Equal(Margin + 36, Left(left, "1."), 0.1f); + var width = Left(left, "1.") - Left(right, "1."); + Assert.True(width > 3, $"The right-aligned number ends at its place: it starts {width} pt before it."); + Assert.Equal(Margin + 36 - width / 2, Left(centre, "1."), 0.1f); + foreach (var page in (PageContent[])[left, centre, right]) + Assert.Equal(Margin + 72, Left(page, "Item"), 0.1f); + } + + [Fact] + public void The_reader_keeps_the_number_alignment() + { + var levels = Levels(Read(Item("Item", ""), Numbering("decimal", "%1.", "right"))); + + Assert.Equal(HAlign.Right, levels[0].Align); + } + + // ───────────────────────── Tab stops on a hanging first line ───────────────────────── + + [Fact] + public void A_tab_stop_between_the_first_lines_start_and_the_left_indent_counts_on_the_first_line() + { + // First line at 0, left indent at 72 pt, a stop at 50 pt: the tab goes there, not to the hanging indent. + var page = ComposeOne(P(Run("a") + "" + Run("b"), "")); + + Assert.Equal(Margin, Left(page, "a"), 0.1f); + Assert.Equal(Margin + 50, Left(page, "b"), 0.1f); + } + + [Fact] + public void The_tab_after_a_number_goes_to_a_stop_before_the_left_indent() + { + var page = ComposeOne(Item("Item", ""), + Numbering("decimal", "%1.", "left")); + + Assert.Equal(Margin, Left(page, "1."), 0.1f); + Assert.Equal(Margin + 50, Left(page, "Item"), 0.1f); + } + + [Fact] + public void Lines_after_the_first_leave_a_stop_before_the_left_indent_out() + { + // The second line starts at the left indent (72 pt): its tab goes to the next default stop (every 36 pt from + // the margin), 108 pt, not back to the stop at 50 pt. + var page = ComposeOne(P(Run("a") + "" + Run("c") + "" + Run("d"), + "")); + + Assert.Equal(Margin + 72, Left(page, "c"), 0.1f); + Assert.Equal(Margin + 108, Left(page, "d"), 0.1f); + } + + // ───────────────────────── The number's format ───────────────────────── + + [Fact] + public void A_number_is_drawn_in_its_levels_format_over_the_paragraph_marks() + { + // The mark is 14 pt, the text 10 pt; the level makes the number red. Word draws the number 14 pt red. + var numbering = Numbering("decimal", "%1.", "left", ""); + var page = ComposeOne(Item("Item", ""), numbering); + + var number = Run(page, "1."); + Assert.Equal(14, number.Size, 0.01f); + Assert.Equal(SkiaSharp.SKColors.Red, number.Color); + var text = Run(page, "Item"); + Assert.Equal(10, text.Size, 0.01f); + Assert.Equal(SkiaSharp.SKColors.Black, text.Color); + } + + [Fact] + public void A_bullet_takes_its_levels_size_but_keeps_its_unicode_character() + { + var numbering = Numbering("bullet", "", "left", ""); + var page = ComposeOne(Item("Item", ""), numbering); + + var bullet = Run(page, "•"); + Assert.Equal(18, bullet.Size, 0.01f); + Assert.Equal(10, Run(page, "Item").Size, 0.01f); + } + + [Fact] + public void The_number_format_takes_no_part_in_the_lists_equality() + { + var levels = Read(Item("Item", "") + Item("Next", ""), Numbering("decimal", "%1.", "left")); + var lists = levels.Sections[0].Blocks.OfType().Select(p => p.List).OfType().ToList(); + + Assert.Equal(2, lists.Count); + Assert.NotEqual(lists[0].MarkerFormat, lists[1].MarkerFormat); + Assert.Equal(lists[0], lists[1]); + } + + // ───────────────────────── Page borders in front of the text ───────────────────────── + + [Theory] + [InlineData(null, true)] + [InlineData("front", true)] + [InlineData("back", false)] + public void A_page_border_goes_in_front_of_the_text_unless_it_says_back(string? zOrder, bool front) + { + var order = zOrder is null ? "" : $" w:zOrder=\"{zOrder}\""; + var builder = new DocxBuilder + { + SectionProperties = DefaultPage + $"", + }.Paragraph(P(Run("text"))); + var document = DocxReader.Read(builder.Save(Path.Combine(_folder, Guid.NewGuid().ToString("N") + ".docx")), Path.Combine(_folder, "media")); + Assert.Equal(front, Assert.Single(document.Sections[0].PageBorders).InFront); + + var items = Compose(document).Items.ToList(); + // The page holds the text and the border's drawing: after the text when in front, before it (under) when not. + var text = items.FindLastIndex(i => i is GlyphItem); + Assert.True(text >= 0); + Assert.Equal(front, items.Skip(text).Any(i => i is DrawingItem)); + Assert.Equal(!front, items.Take(text).Any(i => i is DrawingItem)); + } + + // ───────────────────────── Helpers ───────────────────────── + + private static string Run(string text) => R(text, $""); + + /// An item of list 1 (level 0) holding , with more paragraph properties. + private static string Item(string text, string pPr) => P(Run(text), "" + pPr); + + /// List 1: one level of showing , aligned by . + private static string Numbering(string format, string text, string jc, string extra = "") => + $"" + + $"{extra}"; + + private HDocument Read(string body, string? numbering = null) + { + var builder = new DocxBuilder { Settings = Word2013Settings, Numbering = numbering }.Paragraph(body); + return DocxReader.Read(builder.Save(Path.Combine(_folder, Guid.NewGuid().ToString("N") + ".docx")), Path.Combine(_folder, "media")); + } + + private static List Levels(HDocument document) => + document.Sections[0].Blocks.OfType().First(p => p.List is not null).List!.Numbering.Levels; + + private PageContent ComposeOne(string body, string? numbering = null) => Compose(Read(body, numbering)); + + private PageContent Compose(HDocument document) + { + Assert.SkipUnless(_fonts.Has(RepositoryFonts.Latin), "Noto Sans is not in src/Filee.App/Assets/Fonts (build/fetch-fonts.ps1)"); + return Assert.Single(new PageComposer(new FontResolver(_fonts), _shaper, styles: StyleResolver.FromTemplate()) + .Compose(document, TestContext.Current.CancellationToken).Pages); + } + + private static List Runs(PageContent page) => [.. page.Items.OfType().Select(i => i.Run)]; + + private static GlyphRun Run(PageContent page, string text) => Runs(page).First(r => r.Text.StartsWith(text, StringComparison.Ordinal)); + + /// Where the first run that starts with starts. + private static float Left(PageContent page, string text) => Run(page, text).Positions[0].X; +} diff --git a/tests/Filee.Engines.Tests/WordTableWidthTests.cs b/tests/Filee.Engines.Tests/WordTableWidthTests.cs index 8d4d7df05..6b9c2fd86 100644 --- a/tests/Filee.Engines.Tests/WordTableWidthTests.cs +++ b/tests/Filee.Engines.Tests/WordTableWidthTests.cs @@ -118,6 +118,156 @@ public void A_nested_table_wider_than_its_cell_fits_in_it_and_its_empty_column_g Assert.All(Runs(page), r => Assert.True(r.Positions[^1].X < Margin + 150, $"\"{r.Text}\" runs past the outer cell.")); } + // ───────────────────────── Indent, Word 2010's way ───────────────────────── + + [Theory] + [InlineData(0)] + [InlineData(720)] + public void In_Word_2010s_compatibility_mode_the_indent_is_measured_to_the_first_cells_text(int indent) + { + var table = Table($"", [2000, 2000], [Cell("A"), Cell("B")]); + const string word2010 = ""; + + // Word 2013 and later: the table's edge at the indent, the text a cell margin in. + Assert.Equal(Margin + indent / 20f + CellMargin, Left(ComposeOne(table), "A"), 0.5f); + // Word 2010 and older, and a document whose settings don't say (Word opens it as a Word 2007 one): the text at + // the indent, the table's edge a cell margin out, in the margin at an indent of 0. + Assert.Equal(Margin + indent / 20f, Left(ComposeOne(table, word2010), "A"), 0.5f); + Assert.Equal(Margin + indent / 20f, Left(ComposeOne(table, settings: null), "A"), 0.5f); + } + + [Fact] + public void A_centred_table_is_not_moved_by_the_compatibility_mode() + { + var table = Table("", [2000, 2000], [Cell("A"), Cell("B")]); + + Assert.Equal(Left(ComposeOne(table), "A"), Left(ComposeOne(table, settings: null), "A"), 0.1f); + } + + // ───────────────────────── Wider than the body ───────────────────────── + + [Fact] + public void A_fixed_table_wider_than_the_body_keeps_its_widths_and_runs_into_the_margin() + { + // 600 pt of columns in a 451 pt text column. + var page = ComposeOne(Table("", [6000, 6000], [Cell("A"), Cell("B")])); + + Assert.Equal(Margin + CellMargin, Left(page, "A"), 0.5f); + Assert.Equal(Margin + 300 + CellMargin, Left(page, "B"), 0.5f); + } + + [Fact] + public void A_centred_fixed_table_wider_than_the_body_runs_into_both_margins() + { + var page = ComposeOne(Table("", [6000, 6000], [Cell("A"), Cell("B")])); + + var edge = Margin + (TextWidth - 600) / 2; + Assert.True(edge < Margin - 70, "The table starts in the left margin."); + Assert.Equal(edge + CellMargin, Left(page, "A"), 0.5f); + Assert.Equal(edge + 300 + CellMargin, Left(page, "B"), 0.5f); + } + + [Fact] + public void An_autofit_table_wider_than_the_body_still_fits_it() + { + var page = ComposeOne(Table("", [6000, 6000], [Cell("A"), Cell("B")])); + + Assert.True(Left(page, "B") < Margin + TextWidth / 2 + CellMargin + 1, "The autofit table was narrowed to the text column."); + } + + // ───────────────────────── Cell spacing, empty grid columns ───────────────────────── + + [Fact] + public void Cells_spaced_apart_sit_in_from_their_columns_and_rows() + { + // 100 twips (5 pt) around each cell: 5 pt from the table's edge, 10 pt between two cells. + var spaced = ComposeOne(Table("", [2000, 2000], [Cell("A"), Cell("B")], [Cell("C"), Cell("D")])); + var touching = ComposeOne(Table("", [2000, 2000], [Cell("A"), Cell("B")], [Cell("C"), Cell("D")])); + + Assert.Equal(Margin + 5 + CellMargin, Left(spaced, "A"), 0.5f); + Assert.Equal(Margin + 100 + 5 + CellMargin, Left(spaced, "B"), 0.5f); + // Each row is 10 pt higher: 5 pt above and below its cells. + Assert.Equal(Top(touching, "A") + 5, Top(spaced, "A"), 0.5f); + Assert.Equal(Top(touching, "C") - Top(touching, "A") + 10, Top(spaced, "C") - Top(spaced, "A"), 0.5f); + } + + [Fact] + public void The_reader_keeps_the_cell_spacing_and_the_tables_border_around_the_cells() + { + var table = ReadTable(TableRows("", [2000], + $"{Cell("A")}")); + + Assert.Equal(HwpxUnits.FromTwips(40), table.CellSpacing); + var frame = Assert.IsType(table.Frame); + Assert.Equal(HBorderStyle.Solid, frame.Left.Style); + Assert.Equal(HBorderStyle.Solid, frame.Bottom.Style); + } + + [Fact] + public void Grid_columns_a_row_leaves_empty_take_the_width_it_gives_them() + { + // No grid widths: the first column is 50 pt only because the first row says the column it skips is (w:wBefore). + var before = ComposeOne(TableRows("", [0, 0], + $"{Cell("B", "")}", + $"{Cell("C")}{Cell("D")}")); + Assert.Equal(Margin + 50 + CellMargin, Left(before, "B"), 0.5f); + Assert.Equal(Margin + 50 + CellMargin, Left(before, "D"), 0.5f); + + // After the last cell (w:gridAfter, w:wAfter): a right-aligned table 50 + 100 pt wide. + var after = ComposeOne(TableRows("", [0, 0], + $"{Cell("A", "")}", + $"{Cell("C")}{Cell("D")}")); + Assert.Equal(Margin + TextWidth - 150 + CellMargin, Left(after, "C"), 0.5f); + Assert.Equal(Margin + TextWidth - 100 + CellMargin, Left(after, "D"), 0.5f); + } + + // ───────────────────────── RTF ───────────────────────── + + [Fact] + public void An_rtf_table_with_trautofit_widens_a_column_to_its_longest_word() + { + // A 30 pt first column (\cellx600): the word fits it only when the row fits its columns to their content. + string Rtf(string autofit) => + $@"{{\rtf1\ansi{{\fonttbl{{\f0 {RepositoryFonts.Latin};}}}}\f0\fs20\trowd{autofit}\cellx600\cellx3600\intbl {LongWord}\cell Next\cell\row\pard\par}}"; + HDocument Read(string rtf) => + RtfReader.Read(System.Text.Encoding.Latin1.GetBytes(rtf), Path.Combine(_folder, Guid.NewGuid().ToString("N")), Filee.Engines.Documents.DocumentLimits.Default); + + var autofit = Read(Rtf(@"\trautofit1")); + Assert.False(Assert.IsType(autofit.Sections.SelectMany(s => s.Blocks).OfType().Single().Sizing).Fixed); + Assert.Single(Runs(ComposeOne(autofit)), r => r.Text.Contains(LongWord, StringComparison.Ordinal)); + + var kept = Read(Rtf("")); + Assert.Null(kept.Sections.SelectMany(s => s.Blocks).OfType().Single().Sizing); + Assert.DoesNotContain(Runs(ComposeOne(kept)), r => r.Text.Contains(LongWord, StringComparison.Ordinal)); + } + + // ───────────────────────── Columns of their own widths ───────────────────────── + + [Fact] + public void A_table_going_on_in_a_narrower_column_is_laid_out_for_it() + { + // Columns of 300 pt and 126.3 pt, 25 pt apart (they add up to the text width). The table fills its column (100%); + // 70 rows don't fit the first, so it goes on in the second, as wide as that one. + var rows = Enumerable.Range(1, 70).Select(n => $"{Cell($"R{n}")}{Cell($"S{n}")}").ToArray(); + var body = TableRows("", [3000, 3000], rows) + P(""); + var path = new DocxBuilder + { + Settings = DocxBuilder.Word2013Settings, + SectionProperties = DefaultPage + "", + }.Paragraph(body).Save(Path.Combine(_folder, "columns.docx")); + Assert.SkipUnless(_fonts.Has(RepositoryFonts.Latin), "Noto Sans is not in src/Filee.App/Assets/Fonts (build/fetch-fonts.ps1)"); + var page = new PageComposer(new FontResolver(_fonts), _shaper, styles: StyleResolver.FromTemplate()) + .Compose(DocxReader.Read(path, Path.Combine(_folder, "media")), TestContext.Current.CancellationToken).Pages[0]; + + const float second = Margin + 325; + var narrow = Runs(page).Where(r => r.Text.StartsWith('S') && r.Positions[0].X > second).ToList(); + Assert.NotEmpty(narrow); + // In the first column the second cell starts half of its 300 pt in; in the second, half of its 126.3 pt. + Assert.Equal(Margin + 150 + CellMargin, Left(page, "S1"), 0.5f); + Assert.All(narrow, r => Assert.Equal(second + 126.3f / 2 + CellMargin, r.Positions[0].X, 1f)); + Assert.All(Runs(page), r => Assert.True(r.Positions[^1].X < Margin + TextWidth, $"\"{r.Text}\" runs past the second column.")); + } + // ───────────────────────── Reading ───────────────────────── [Fact] @@ -152,15 +302,34 @@ private static string Table(string properties, int[] grid, params string[][] row $"{properties}{string.Concat(grid.Select(w => $""))}" + string.Concat(rows.Select(r => $"{string.Concat(r)}")) + "" + P(""); - private PageContent ComposeOne(string body) + /// The one page of a document with , saved by Word 2013 or later unless say otherwise. + private PageContent ComposeOne(string body, string? settings = DocxBuilder.Word2013Settings) + { + var path = new DocxBuilder { Settings = settings }.Paragraph(body).Save(Path.Combine(_folder, Guid.NewGuid().ToString("N") + ".docx")); + return ComposeOne(DocxReader.Read(path, Path.Combine(_folder, "media"))); + } + + private PageContent ComposeOne(HDocument document) { Assert.SkipUnless(_fonts.Has(RepositoryFonts.Latin), "Noto Sans is not in src/Filee.App/Assets/Fonts (build/fetch-fonts.ps1)"); - var path = new DocxBuilder().Paragraph(body).Save(Path.Combine(_folder, Guid.NewGuid().ToString("N") + ".docx")); - var document = DocxReader.Read(path, Path.Combine(_folder, "media")); return Assert.Single(new PageComposer(new FontResolver(_fonts), _shaper, styles: StyleResolver.FromTemplate()) .Compose(document, TestContext.Current.CancellationToken).Pages); } + private HTable ReadTable(string body) + { + var path = new DocxBuilder { Settings = DocxBuilder.Word2013Settings }.Paragraph(body).Save(Path.Combine(_folder, Guid.NewGuid().ToString("N") + ".docx")); + return DocxReader.Read(path, Path.Combine(_folder, "media")).Sections.SelectMany(s => s.Blocks).OfType().Single(); + } + + /// How far down the page the first run that starts with is (its baseline). + private static float Top(PageContent page, string text) => Runs(page).First(r => r.Text.StartsWith(text, StringComparison.Ordinal)).Positions[0].Y; + + /// A table of the given rows (w:tr elements) after the table properties and grid, then an empty paragraph. + private static string TableRows(string properties, int[] grid, params string[] rows) => + $"{properties}{string.Concat(grid.Select(w => $""))}" + + string.Concat(rows) + "" + P(""); + private static List Runs(PageContent page) => [.. page.Items.OfType().Select(i => i.Run)]; /// Where the first run that starts with starts. From e818398f40ee67af46a9dc67262d99ed964e09e8 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 22:28:31 +0900 Subject: [PATCH 3/7] feat(renderer): say what the built-in engines leave out of DOCX text boxes, controls and slide decks - DOCX: text boxes written top to bottom (wps:bodyPr vert, VML layout-flow) are named as vertical text set horizontally; ActiveX controls are named as form controls drawn from their saved picture or left out. Both are flagged by the capability check, so such files keep LibreOffice's route. - DOCX: legacy check boxes and drop-downs (w:ffData) are drawn as Word prints them (a checked or empty box, the chosen entry) instead of nothing. - DOCX: a header or footer shared by several sections counts what it leaves out once, not once per section. - Slides: PPTX, PPT and ODP decks drawn by the slide renderer get the same note: the capability check's pass run to the end for PPTX (and what a PPT translation dropped), the ODP reader's own checks counted once per shape. ODG drawings get it through the drawing converter. - 11 new notice texts in all 23 languages. Co-Authored-By: Claude Opus 5.5 --- src/Filee.App/Assets/i18n/ar.json | 11 ++ src/Filee.App/Assets/i18n/bn-BD.json | 11 ++ src/Filee.App/Assets/i18n/de.json | 11 ++ src/Filee.App/Assets/i18n/en.json | 11 ++ src/Filee.App/Assets/i18n/es.json | 11 ++ src/Filee.App/Assets/i18n/fa.json | 11 ++ src/Filee.App/Assets/i18n/fr.json | 11 ++ src/Filee.App/Assets/i18n/hi.json | 11 ++ src/Filee.App/Assets/i18n/id.json | 11 ++ src/Filee.App/Assets/i18n/it.json | 11 ++ src/Filee.App/Assets/i18n/ja.json | 11 ++ src/Filee.App/Assets/i18n/ko.json | 11 ++ src/Filee.App/Assets/i18n/mn.json | 11 ++ src/Filee.App/Assets/i18n/ne.json | 11 ++ src/Filee.App/Assets/i18n/pl.json | 11 ++ src/Filee.App/Assets/i18n/pt-BR.json | 11 ++ src/Filee.App/Assets/i18n/pt-PT.json | 11 ++ src/Filee.App/Assets/i18n/ru.json | 11 ++ src/Filee.App/Assets/i18n/th.json | 11 ++ src/Filee.App/Assets/i18n/tr.json | 11 ++ src/Filee.App/Assets/i18n/vi.json | 11 ++ src/Filee.App/Assets/i18n/zh-CN.json | 11 ++ src/Filee.App/Assets/i18n/zh-TW.json | 11 ++ .../Hwp/Hwpx/Docx/DocxReader.Drawing.cs | 8 +- src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs | 63 ++++++- .../Hwp/Hwpx/HwpxModel.Layout.cs | 22 ++- src/Filee.Engines/Odf/Draw/OdfDrawReader.cs | 51 ++++-- src/Filee.Engines/Office/Ppt/PptReader.cs | 14 +- .../Office/Ppt/PptToPptx.Geometry.cs | 9 +- .../Office/Ppt/PptToPptx.Shapes.cs | 5 +- src/Filee.Engines/Office/Ppt/PptToPptx.cs | 19 +- src/Filee.Engines/Slides/PptxSlideReader.cs | 15 +- src/Filee.Engines/Slides/SlideCapabilities.cs | 111 ++++++++---- src/Filee.Engines/Slides/SlideFormats.cs | 7 +- src/Filee.Engines/Slides/SlideLeftOut.cs | 32 ++++ src/Filee.Engines/Slides/SlideModel.cs | 2 +- src/Filee.Engines/Slides/SlidePackage.cs | 7 +- .../Typesetting/FileeRendererConverter.cs | 34 +++- tests/Filee.Engines.Tests/PptReaderTests.cs | 3 + .../RendererLeftOutTests.cs | 162 +++++++++++++++++- 40 files changed, 731 insertions(+), 86 deletions(-) create mode 100644 src/Filee.Engines/Slides/SlideLeftOut.cs diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index 64469576a..86fae676f 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "صيغ كُتبت نصًا عاديًا: {0}", "notice.renderer_left_out.formulas_one": "كُتبت صيغة واحدة نصًا عاديًا", "notice.renderer_left_out.vertical_text": "وُضع النص العمودي أفقيًا", + "notice.renderer_left_out.form_controls_pictured": "عناصر تحكم نماذج رُسمت من صورها المحفوظة: {0}", + "notice.renderer_left_out.form_controls_pictured_one": "رُسم عنصر تحكم نموذج واحد من صورته المحفوظة", + "notice.renderer_left_out.shapes": "أشكال رُسمت مستطيلات: {0}", + "notice.renderer_left_out.shapes_one": "رُسم شكل واحد مستطيلًا", + "notice.renderer_left_out.shapes_left_out": "أشكال محذوفة: {0}", + "notice.renderer_left_out.shapes_left_out_one": "حُذف شكل واحد", + "notice.renderer_left_out.ink": "رسوم حبر محذوفة: {0}", + "notice.renderer_left_out.ink_one": "حُذف رسم حبر واحد", + "notice.renderer_left_out.wordart": "رُسم WordArt نصًا عاديًا", + "notice.renderer_left_out.effects": "حُذفت الظلال والتأثيرات ثلاثية الأبعاد وغيرها", + "notice.renderer_left_out.vertical_text_turned": "أُدير النص العمودي لشرق آسيا على جانبه", "notice.renderer_left_out.more": "وغير ذلك", "engine.reason.not_installed": "غير مثبّت", "engine.reason.error": "خطأ أثناء التحقق", diff --git a/src/Filee.App/Assets/i18n/bn-BD.json b/src/Filee.App/Assets/i18n/bn-BD.json index 34be0d9cc..fac7b6754 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -278,6 +278,17 @@ "notice.renderer_left_out.formulas": "{0}টি সূত্র সাধারণ লেখা হিসেবে লেখা হয়েছে", "notice.renderer_left_out.formulas_one": "1টি সূত্র সাধারণ লেখা হিসেবে লেখা হয়েছে", "notice.renderer_left_out.vertical_text": "খাড়া লেখা আড়াআড়ি বসানো হয়েছে", + "notice.renderer_left_out.form_controls_pictured": "{0}টি ফর্ম কন্ট্রোল তাদের সংরক্ষিত ছবি থেকে আঁকা হয়েছে", + "notice.renderer_left_out.form_controls_pictured_one": "1টি ফর্ম কন্ট্রোল তার সংরক্ষিত ছবি থেকে আঁকা হয়েছে", + "notice.renderer_left_out.shapes": "{0}টি আকৃতি আয়তক্ষেত্র হিসেবে আঁকা হয়েছে", + "notice.renderer_left_out.shapes_one": "1টি আকৃতি আয়তক্ষেত্র হিসেবে আঁকা হয়েছে", + "notice.renderer_left_out.shapes_left_out": "{0}টি আকৃতি বাদ দেওয়া হয়েছে", + "notice.renderer_left_out.shapes_left_out_one": "1টি আকৃতি বাদ দেওয়া হয়েছে", + "notice.renderer_left_out.ink": "{0}টি কালির আঁকা বাদ দেওয়া হয়েছে", + "notice.renderer_left_out.ink_one": "1টি কালির আঁকা বাদ দেওয়া হয়েছে", + "notice.renderer_left_out.wordart": "WordArt সাধারণ লেখা হিসেবে আঁকা হয়েছে", + "notice.renderer_left_out.effects": "ছায়া, 3D ও অন্যান্য ইফেক্ট বাদ দেওয়া হয়েছে", + "notice.renderer_left_out.vertical_text_turned": "পূর্ব এশীয় খাড়া লেখা কাত করে বসানো হয়েছে", "notice.renderer_left_out.more": "আরও কিছু", "engine.reason.not_installed": "ইনস্টল করা নেই", "engine.reason.error": "যাচাই করার সময় ত্রুটি", diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.json index 4d2fe10d3..5c66f9a17 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "{0} Formeln als einfacher Text geschrieben", "notice.renderer_left_out.formulas_one": "1 Formel als einfacher Text geschrieben", "notice.renderer_left_out.vertical_text": "senkrechter Text waagerecht gesetzt", + "notice.renderer_left_out.form_controls_pictured": "{0} Formularsteuerelemente aus ihrem gespeicherten Bild gezeichnet", + "notice.renderer_left_out.form_controls_pictured_one": "1 Formularsteuerelement aus seinem gespeicherten Bild gezeichnet", + "notice.renderer_left_out.shapes": "{0} Formen als Rechtecke gezeichnet", + "notice.renderer_left_out.shapes_one": "1 Form als Rechteck gezeichnet", + "notice.renderer_left_out.shapes_left_out": "{0} Formen weggelassen", + "notice.renderer_left_out.shapes_left_out_one": "1 Form weggelassen", + "notice.renderer_left_out.ink": "{0} Freihandzeichnungen weggelassen", + "notice.renderer_left_out.ink_one": "1 Freihandzeichnung weggelassen", + "notice.renderer_left_out.wordart": "WordArt als einfacher Text gezeichnet", + "notice.renderer_left_out.effects": "Schatten, 3D und andere Effekte weggelassen", + "notice.renderer_left_out.vertical_text_turned": "senkrechter ostasiatischer Text auf die Seite gedreht", "notice.renderer_left_out.more": "und mehr", "engine.reason.not_installed": "Nicht installiert", "engine.reason.error": "Fehler bei der Prüfung", diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json index 9bafb1000..8aaae26c5 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "{0} formulas written as plain text", "notice.renderer_left_out.formulas_one": "1 formula written as plain text", "notice.renderer_left_out.vertical_text": "vertical text set horizontally", + "notice.renderer_left_out.form_controls_pictured": "{0} form controls drawn from their saved pictures", + "notice.renderer_left_out.form_controls_pictured_one": "1 form control drawn from its saved picture", + "notice.renderer_left_out.shapes": "{0} shapes drawn as rectangles", + "notice.renderer_left_out.shapes_one": "1 shape drawn as a rectangle", + "notice.renderer_left_out.shapes_left_out": "{0} shapes left out", + "notice.renderer_left_out.shapes_left_out_one": "1 shape left out", + "notice.renderer_left_out.ink": "{0} ink drawings left out", + "notice.renderer_left_out.ink_one": "1 ink drawing left out", + "notice.renderer_left_out.wordart": "WordArt drawn as plain text", + "notice.renderer_left_out.effects": "shadows, 3D and other effects left out", + "notice.renderer_left_out.vertical_text_turned": "vertical East Asian text turned on its side", "notice.renderer_left_out.more": "and more", "engine.reason.not_installed": "Not installed", diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.json index 268ebe65b..a720f0cfa 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "{0} fórmulas escritas como texto sin formato", "notice.renderer_left_out.formulas_one": "1 fórmula escrita como texto sin formato", "notice.renderer_left_out.vertical_text": "texto vertical colocado en horizontal", + "notice.renderer_left_out.form_controls_pictured": "{0} controles de formulario dibujados a partir de su imagen guardada", + "notice.renderer_left_out.form_controls_pictured_one": "1 control de formulario dibujado a partir de su imagen guardada", + "notice.renderer_left_out.shapes": "{0} formas dibujadas como rectángulos", + "notice.renderer_left_out.shapes_one": "1 forma dibujada como rectángulo", + "notice.renderer_left_out.shapes_left_out": "{0} formas omitidas", + "notice.renderer_left_out.shapes_left_out_one": "1 forma omitida", + "notice.renderer_left_out.ink": "{0} dibujos de tinta omitidos", + "notice.renderer_left_out.ink_one": "1 dibujo de tinta omitido", + "notice.renderer_left_out.wordart": "WordArt dibujado como texto normal", + "notice.renderer_left_out.effects": "sombras, 3D y otros efectos omitidos", + "notice.renderer_left_out.vertical_text_turned": "texto vertical de Asia oriental girado de lado", "notice.renderer_left_out.more": "y más", "engine.reason.not_installed": "No instalado", "engine.reason.error": "Error al comprobar", diff --git a/src/Filee.App/Assets/i18n/fa.json b/src/Filee.App/Assets/i18n/fa.json index 0e2eba9e0..06dd50a04 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -278,6 +278,17 @@ "notice.renderer_left_out.formulas": "{0} فرمول به‌صورت متن ساده نوشته شد", "notice.renderer_left_out.formulas_one": "یک فرمول به‌صورت متن ساده نوشته شد", "notice.renderer_left_out.vertical_text": "متن عمودی افقی چیده شد", + "notice.renderer_left_out.form_controls_pictured": "{0} کنترل فرم از تصویر ذخیره‌شده‌اش رسم شد", + "notice.renderer_left_out.form_controls_pictured_one": "یک کنترل فرم از تصویر ذخیره‌شده‌اش رسم شد", + "notice.renderer_left_out.shapes": "{0} شکل به‌صورت مستطیل رسم شد", + "notice.renderer_left_out.shapes_one": "یک شکل به‌صورت مستطیل رسم شد", + "notice.renderer_left_out.shapes_left_out": "{0} شکل حذف شد", + "notice.renderer_left_out.shapes_left_out_one": "یک شکل حذف شد", + "notice.renderer_left_out.ink": "{0} طرح جوهری حذف شد", + "notice.renderer_left_out.ink_one": "یک طرح جوهری حذف شد", + "notice.renderer_left_out.wordart": "WordArt به‌صورت متن ساده رسم شد", + "notice.renderer_left_out.effects": "سایه‌ها، جلوه‌های سه‌بعدی و جلوه‌های دیگر حذف شد", + "notice.renderer_left_out.vertical_text_turned": "متن عمودی شرق آسیا به پهلو چرخانده شد", "notice.renderer_left_out.more": "و موارد دیگر", "engine.reason.not_installed": "نصب نشده است", "engine.reason.error": "خطا در بررسی", diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.json index 315b3b95f..4bb2dbd16 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "{0} formules écrites en texte brut", "notice.renderer_left_out.formulas_one": "1 formule écrite en texte brut", "notice.renderer_left_out.vertical_text": "texte vertical mis à l'horizontale", + "notice.renderer_left_out.form_controls_pictured": "{0} contrôles de formulaire dessinés d'après leur image enregistrée", + "notice.renderer_left_out.form_controls_pictured_one": "1 contrôle de formulaire dessiné d'après son image enregistrée", + "notice.renderer_left_out.shapes": "{0} formes dessinées en rectangles", + "notice.renderer_left_out.shapes_one": "1 forme dessinée en rectangle", + "notice.renderer_left_out.shapes_left_out": "{0} formes omises", + "notice.renderer_left_out.shapes_left_out_one": "1 forme omise", + "notice.renderer_left_out.ink": "{0} dessins à l'encre omis", + "notice.renderer_left_out.ink_one": "1 dessin à l'encre omis", + "notice.renderer_left_out.wordart": "WordArt dessiné en texte simple", + "notice.renderer_left_out.effects": "ombres, 3D et autres effets omis", + "notice.renderer_left_out.vertical_text_turned": "texte vertical d'Asie de l'Est couché sur le côté", "notice.renderer_left_out.more": "et plus", "engine.reason.not_installed": "Non installé", diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.json index 32592b20c..fedcaeece 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "{0} सूत्र सादे टेक्स्ट के रूप में लिखे गए", "notice.renderer_left_out.formulas_one": "1 सूत्र सादे टेक्स्ट के रूप में लिखा गया", "notice.renderer_left_out.vertical_text": "खड़ा टेक्स्ट आड़ा रखा गया", + "notice.renderer_left_out.form_controls_pictured": "{0} फ़ॉर्म कंट्रोल उनकी सहेजी गई तस्वीर से बनाए गए", + "notice.renderer_left_out.form_controls_pictured_one": "1 फ़ॉर्म कंट्रोल उसकी सहेजी गई तस्वीर से बनाया गया", + "notice.renderer_left_out.shapes": "{0} आकृतियाँ आयत के रूप में बनाई गईं", + "notice.renderer_left_out.shapes_one": "1 आकृति आयत के रूप में बनाई गई", + "notice.renderer_left_out.shapes_left_out": "{0} आकृतियाँ छोड़ दी गईं", + "notice.renderer_left_out.shapes_left_out_one": "1 आकृति छोड़ दी गई", + "notice.renderer_left_out.ink": "{0} इंक ड्रॉइंग छोड़ दी गईं", + "notice.renderer_left_out.ink_one": "1 इंक ड्रॉइंग छोड़ दी गई", + "notice.renderer_left_out.wordart": "WordArt सादे टेक्स्ट के रूप में बनाया गया", + "notice.renderer_left_out.effects": "छाया, 3D और अन्य इफ़ेक्ट छोड़ दिए गए", + "notice.renderer_left_out.vertical_text_turned": "पूर्वी एशियाई खड़ा टेक्स्ट करवट घुमाकर रखा गया", "notice.renderer_left_out.more": "और भी", "engine.reason.not_installed": "इंस्टॉल नहीं है", "engine.reason.error": "जाँच के दौरान त्रुटि", diff --git a/src/Filee.App/Assets/i18n/id.json b/src/Filee.App/Assets/i18n/id.json index 861189981..1cd470a6a 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "{0} rumus ditulis sebagai teks biasa", "notice.renderer_left_out.formulas_one": "1 rumus ditulis sebagai teks biasa", "notice.renderer_left_out.vertical_text": "teks vertikal ditata mendatar", + "notice.renderer_left_out.form_controls_pictured": "{0} kontrol formulir digambar dari gambar tersimpannya", + "notice.renderer_left_out.form_controls_pictured_one": "1 kontrol formulir digambar dari gambar tersimpannya", + "notice.renderer_left_out.shapes": "{0} bentuk digambar sebagai persegi panjang", + "notice.renderer_left_out.shapes_one": "1 bentuk digambar sebagai persegi panjang", + "notice.renderer_left_out.shapes_left_out": "{0} bentuk tidak disertakan", + "notice.renderer_left_out.shapes_left_out_one": "1 bentuk tidak disertakan", + "notice.renderer_left_out.ink": "{0} gambar tinta tidak disertakan", + "notice.renderer_left_out.ink_one": "1 gambar tinta tidak disertakan", + "notice.renderer_left_out.wordart": "WordArt digambar sebagai teks biasa", + "notice.renderer_left_out.effects": "bayangan, 3D, dan efek lain tidak disertakan", + "notice.renderer_left_out.vertical_text_turned": "teks vertikal Asia Timur diputar menyamping", "notice.renderer_left_out.more": "dan lainnya", "engine.reason.not_installed": "Belum terpasang", "engine.reason.error": "Kesalahan saat memeriksa", diff --git a/src/Filee.App/Assets/i18n/it.json b/src/Filee.App/Assets/i18n/it.json index 9566c1a2c..3d9242935 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -263,6 +263,17 @@ "notice.renderer_left_out.formulas": "{0} formule scritte come testo semplice", "notice.renderer_left_out.formulas_one": "1 formula scritta come testo semplice", "notice.renderer_left_out.vertical_text": "testo verticale reso in orizzontale", +"notice.renderer_left_out.form_controls_pictured": "{0} controlli modulo disegnati dalla loro immagine salvata", +"notice.renderer_left_out.form_controls_pictured_one": "1 controllo modulo disegnato dalla sua immagine salvata", +"notice.renderer_left_out.shapes": "{0} forme disegnate come rettangoli", +"notice.renderer_left_out.shapes_one": "1 forma disegnata come rettangolo", +"notice.renderer_left_out.shapes_left_out": "{0} forme omesse", +"notice.renderer_left_out.shapes_left_out_one": "1 forma omessa", +"notice.renderer_left_out.ink": "{0} disegni a penna omessi", +"notice.renderer_left_out.ink_one": "1 disegno a penna omesso", +"notice.renderer_left_out.wordart": "WordArt disegnato come testo semplice", +"notice.renderer_left_out.effects": "ombre, 3D e altri effetti omessi", +"notice.renderer_left_out.vertical_text_turned": "testo verticale dell'Asia orientale ruotato su un lato", "notice.renderer_left_out.more": "e altro", "engine.reason.not_installed": "Non installato", "engine.reason.error": "Errore durante la verifica", diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json index 53ead4fd4..cd6594ad3 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "数式 {0} 個をプレーンテキストで表示", "notice.renderer_left_out.formulas_one": "数式 1 個をプレーンテキストで表示", "notice.renderer_left_out.vertical_text": "縦書きを横書きで表示", + "notice.renderer_left_out.form_controls_pictured": "フォームコントロール {0} 個を保存済みの画像で描画", + "notice.renderer_left_out.form_controls_pictured_one": "フォームコントロール 1 個を保存済みの画像で描画", + "notice.renderer_left_out.shapes": "図形 {0} 個を四角形で描画", + "notice.renderer_left_out.shapes_one": "図形 1 個を四角形で描画", + "notice.renderer_left_out.shapes_left_out": "図形 {0} 個を省略", + "notice.renderer_left_out.shapes_left_out_one": "図形 1 個を省略", + "notice.renderer_left_out.ink": "インク {0} 個を省略", + "notice.renderer_left_out.ink_one": "インク 1 個を省略", + "notice.renderer_left_out.wordart": "ワードアートを通常の文字で描画", + "notice.renderer_left_out.effects": "影・3-D などの効果を省略", + "notice.renderer_left_out.vertical_text_turned": "縦書きを横に倒して表示", "notice.renderer_left_out.more": "ほか", "engine.reason.not_installed": "未インストール", "engine.reason.error": "確認中にエラーが発生しました", diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json index 6762ab219..af1ac0574 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "수식 {0}개는 일반 글자로 씀", "notice.renderer_left_out.formulas_one": "수식 1개는 일반 글자로 씀", "notice.renderer_left_out.vertical_text": "세로쓰기는 가로로 씀", + "notice.renderer_left_out.form_controls_pictured": "양식 컨트롤 {0}개는 저장된 그림으로 그림", + "notice.renderer_left_out.form_controls_pictured_one": "양식 컨트롤 1개는 저장된 그림으로 그림", + "notice.renderer_left_out.shapes": "도형 {0}개는 사각형으로 그림", + "notice.renderer_left_out.shapes_one": "도형 1개는 사각형으로 그림", + "notice.renderer_left_out.shapes_left_out": "도형 {0}개 빠짐", + "notice.renderer_left_out.shapes_left_out_one": "도형 1개 빠짐", + "notice.renderer_left_out.ink": "잉크 {0}개 빠짐", + "notice.renderer_left_out.ink_one": "잉크 1개 빠짐", + "notice.renderer_left_out.wordart": "WordArt는 일반 글자로 그림", + "notice.renderer_left_out.effects": "그림자, 3차원 등 효과 빠짐", + "notice.renderer_left_out.vertical_text_turned": "동아시아 세로쓰기는 옆으로 눕혀 씀", "notice.renderer_left_out.more": "그 밖에 더 있음", "engine.reason.not_installed": "설치되지 않음", diff --git a/src/Filee.App/Assets/i18n/mn.json b/src/Filee.App/Assets/i18n/mn.json index fec353419..dfe6ce043 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -263,6 +263,17 @@ "notice.renderer_left_out.formulas": "{0} томьёог энгийн текстээр бичлээ", "notice.renderer_left_out.formulas_one": "1 томьёог энгийн текстээр бичлээ", "notice.renderer_left_out.vertical_text": "босоо текстийг хэвтээгээр байрлууллаа", + "notice.renderer_left_out.form_controls_pictured": "{0} маягтын удирдлагыг хадгалсан зургаас нь зурлаа", + "notice.renderer_left_out.form_controls_pictured_one": "1 маягтын удирдлагыг хадгалсан зургаас нь зурлаа", + "notice.renderer_left_out.shapes": "{0} дүрсийг тэгш өнцөгт болгон зурлаа", + "notice.renderer_left_out.shapes_one": "1 дүрсийг тэгш өнцөгт болгон зурлаа", + "notice.renderer_left_out.shapes_left_out": "{0} дүрс орхигдлоо", + "notice.renderer_left_out.shapes_left_out_one": "1 дүрс орхигдлоо", + "notice.renderer_left_out.ink": "{0} бэхэн зураг орхигдлоо", + "notice.renderer_left_out.ink_one": "1 бэхэн зураг орхигдлоо", + "notice.renderer_left_out.wordart": "WordArt-ыг энгийн текстээр зурлаа", + "notice.renderer_left_out.effects": "сүүдэр, 3D болон бусад эффект орхигдлоо", + "notice.renderer_left_out.vertical_text_turned": "Зүүн Азийн босоо текстийг хажуу тийш нь эргүүллээ", "notice.renderer_left_out.more": "болон бусад", "engine.reason.not_installed": "Суулгаагүй", "engine.reason.error": "Шалгах үед алдаа гарлаа", diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.json index 7677d13db..7de87e328 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "{0} सूत्र सादा पाठका रूपमा लेखिए", "notice.renderer_left_out.formulas_one": "1 सूत्र सादा पाठका रूपमा लेखियो", "notice.renderer_left_out.vertical_text": "ठाडो पाठ तेर्सो राखियो", + "notice.renderer_left_out.form_controls_pictured": "{0} फारम नियन्त्रण तिनको सुरक्षित चित्रबाट कोरिए", + "notice.renderer_left_out.form_controls_pictured_one": "1 फारम नियन्त्रण यसको सुरक्षित चित्रबाट कोरियो", + "notice.renderer_left_out.shapes": "{0} आकृति आयतका रूपमा कोरिए", + "notice.renderer_left_out.shapes_one": "1 आकृति आयतका रूपमा कोरियो", + "notice.renderer_left_out.shapes_left_out": "{0} आकृति छोडिए", + "notice.renderer_left_out.shapes_left_out_one": "1 आकृति छोडियो", + "notice.renderer_left_out.ink": "{0} मसी चित्र छोडिए", + "notice.renderer_left_out.ink_one": "1 मसी चित्र छोडियो", + "notice.renderer_left_out.wordart": "WordArt सादा पाठका रूपमा कोरियो", + "notice.renderer_left_out.effects": "छाया, 3D र अन्य प्रभाव छोडिए", + "notice.renderer_left_out.vertical_text_turned": "पूर्वी एसियाली ठाडो पाठ कोल्टो पारेर राखियो", "notice.renderer_left_out.more": "र अन्य", "engine.reason.not_installed": "स्थापना गरिएको छैन", "engine.reason.error": "जाँच गर्दा त्रुटि भयो", diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.json index 9547ca2f7..50830a1f8 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "formuły zapisane zwykłym tekstem: {0}", "notice.renderer_left_out.formulas_one": "1 formuła zapisana zwykłym tekstem", "notice.renderer_left_out.vertical_text": "tekst pionowy złożony poziomo", + "notice.renderer_left_out.form_controls_pictured": "kontrolki formularza narysowane z zapisanych obrazów: {0}", + "notice.renderer_left_out.form_controls_pictured_one": "1 kontrolka formularza narysowana z zapisanego obrazu", + "notice.renderer_left_out.shapes": "kształty narysowane jako prostokąty: {0}", + "notice.renderer_left_out.shapes_one": "1 kształt narysowany jako prostokąt", + "notice.renderer_left_out.shapes_left_out": "pominięte kształty: {0}", + "notice.renderer_left_out.shapes_left_out_one": "pominięty 1 kształt", + "notice.renderer_left_out.ink": "pominięte rysunki odręczne: {0}", + "notice.renderer_left_out.ink_one": "pominięty 1 rysunek odręczny", + "notice.renderer_left_out.wordart": "WordArt narysowany jako zwykły tekst", + "notice.renderer_left_out.effects": "pominięte cienie, efekty 3D i inne efekty", + "notice.renderer_left_out.vertical_text_turned": "pionowy tekst wschodnioazjatycki obrócony na bok", "notice.renderer_left_out.more": "i inne", "engine.reason.not_installed": "Nie zainstalowano", diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.json index 519fbc23a..a160850fc 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "{0} fórmulas escritas como texto simples", "notice.renderer_left_out.formulas_one": "1 fórmula escrita como texto simples", "notice.renderer_left_out.vertical_text": "texto vertical posto na horizontal", + "notice.renderer_left_out.form_controls_pictured": "{0} controles de formulário desenhados a partir da imagem salva", + "notice.renderer_left_out.form_controls_pictured_one": "1 controle de formulário desenhado a partir da imagem salva", + "notice.renderer_left_out.shapes": "{0} formas desenhadas como retângulos", + "notice.renderer_left_out.shapes_one": "1 forma desenhada como retângulo", + "notice.renderer_left_out.shapes_left_out": "{0} formas deixadas de fora", + "notice.renderer_left_out.shapes_left_out_one": "1 forma deixada de fora", + "notice.renderer_left_out.ink": "{0} desenhos à tinta deixados de fora", + "notice.renderer_left_out.ink_one": "1 desenho à tinta deixado de fora", + "notice.renderer_left_out.wordart": "WordArt desenhado como texto simples", + "notice.renderer_left_out.effects": "sombras, 3D e outros efeitos deixados de fora", + "notice.renderer_left_out.vertical_text_turned": "texto vertical do Leste Asiático virado de lado", "notice.renderer_left_out.more": "e mais", "engine.reason.not_installed": "Não instalado", diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.json index 2a9b91ed3..46accd166 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "{0} fórmulas escritas como texto simples", "notice.renderer_left_out.formulas_one": "1 fórmula escrita como texto simples", "notice.renderer_left_out.vertical_text": "texto vertical colocado na horizontal", + "notice.renderer_left_out.form_controls_pictured": "{0} controlos de formulário desenhados a partir da imagem guardada", + "notice.renderer_left_out.form_controls_pictured_one": "1 controlo de formulário desenhado a partir da imagem guardada", + "notice.renderer_left_out.shapes": "{0} formas desenhadas como retângulos", + "notice.renderer_left_out.shapes_one": "1 forma desenhada como retângulo", + "notice.renderer_left_out.shapes_left_out": "{0} formas omitidas", + "notice.renderer_left_out.shapes_left_out_one": "1 forma omitida", + "notice.renderer_left_out.ink": "{0} desenhos a tinta omitidos", + "notice.renderer_left_out.ink_one": "1 desenho a tinta omitido", + "notice.renderer_left_out.wordart": "WordArt desenhado como texto simples", + "notice.renderer_left_out.effects": "sombras, 3D e outros efeitos omitidos", + "notice.renderer_left_out.vertical_text_turned": "texto vertical da Ásia Oriental virado de lado", "notice.renderer_left_out.more": "e mais", "engine.reason.not_installed": "Não instalado", diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json index 710f3622b..3b143346d 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "формул записано обычным текстом: {0}", "notice.renderer_left_out.formulas_one": "1 формула записана обычным текстом", "notice.renderer_left_out.vertical_text": "вертикальный текст набран горизонтально", + "notice.renderer_left_out.form_controls_pictured": "элементов управления формы нарисовано по сохранённым изображениям: {0}", + "notice.renderer_left_out.form_controls_pictured_one": "1 элемент управления формы нарисован по сохранённому изображению", + "notice.renderer_left_out.shapes": "фигур нарисовано прямоугольниками: {0}", + "notice.renderer_left_out.shapes_one": "1 фигура нарисована прямоугольником", + "notice.renderer_left_out.shapes_left_out": "пропущено фигур: {0}", + "notice.renderer_left_out.shapes_left_out_one": "пропущена 1 фигура", + "notice.renderer_left_out.ink": "пропущено рукописных рисунков: {0}", + "notice.renderer_left_out.ink_one": "пропущен 1 рукописный рисунок", + "notice.renderer_left_out.wordart": "WordArt нарисован обычным текстом", + "notice.renderer_left_out.effects": "пропущены тени, объём и другие эффекты", + "notice.renderer_left_out.vertical_text_turned": "вертикальный восточноазиатский текст повёрнут набок", "notice.renderer_left_out.more": "и другое", "engine.reason.not_installed": "Не установлен", diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.json index 477643330..6e165912c 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "เขียนสูตร {0} รายการเป็นข้อความธรรมดา", "notice.renderer_left_out.formulas_one": "เขียนสูตร 1 รายการเป็นข้อความธรรมดา", "notice.renderer_left_out.vertical_text": "จัดข้อความแนวตั้งเป็นแนวนอน", + "notice.renderer_left_out.form_controls_pictured": "วาดตัวควบคุมฟอร์ม {0} รายการจากรูปที่บันทึกไว้", + "notice.renderer_left_out.form_controls_pictured_one": "วาดตัวควบคุมฟอร์ม 1 รายการจากรูปที่บันทึกไว้", + "notice.renderer_left_out.shapes": "วาดรูปร่าง {0} รายการเป็นสี่เหลี่ยมผืนผ้า", + "notice.renderer_left_out.shapes_one": "วาดรูปร่าง 1 รายการเป็นสี่เหลี่ยมผืนผ้า", + "notice.renderer_left_out.shapes_left_out": "ละรูปร่าง {0} รายการ", + "notice.renderer_left_out.shapes_left_out_one": "ละรูปร่าง 1 รายการ", + "notice.renderer_left_out.ink": "ละภาพวาดหมึก {0} รายการ", + "notice.renderer_left_out.ink_one": "ละภาพวาดหมึก 1 รายการ", + "notice.renderer_left_out.wordart": "วาด WordArt เป็นข้อความธรรมดา", + "notice.renderer_left_out.effects": "ละเงา เอฟเฟกต์ 3 มิติ และเอฟเฟกต์อื่น", + "notice.renderer_left_out.vertical_text_turned": "หมุนข้อความแนวตั้งแบบเอเชียตะวันออกให้ตะแคง", "notice.renderer_left_out.more": "และอื่น ๆ", "engine.reason.not_installed": "ยังไม่ได้ติดตั้ง", "engine.reason.error": "เกิดข้อผิดพลาดขณะตรวจสอบ", diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.json index 4d3f5e717..4025712bd 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -264,6 +264,17 @@ "notice.renderer_left_out.formulas": "{0} formül düz metin olarak yazıldı", "notice.renderer_left_out.formulas_one": "1 formül düz metin olarak yazıldı", "notice.renderer_left_out.vertical_text": "dikey metin yatay dizildi", + "notice.renderer_left_out.form_controls_pictured": "{0} form denetimi kayıtlı resminden çizildi", + "notice.renderer_left_out.form_controls_pictured_one": "1 form denetimi kayıtlı resminden çizildi", + "notice.renderer_left_out.shapes": "{0} şekil dikdörtgen olarak çizildi", + "notice.renderer_left_out.shapes_one": "1 şekil dikdörtgen olarak çizildi", + "notice.renderer_left_out.shapes_left_out": "{0} şekil çıkarıldı", + "notice.renderer_left_out.shapes_left_out_one": "1 şekil çıkarıldı", + "notice.renderer_left_out.ink": "{0} mürekkep çizimi çıkarıldı", + "notice.renderer_left_out.ink_one": "1 mürekkep çizimi çıkarıldı", + "notice.renderer_left_out.wordart": "WordArt düz metin olarak çizildi", + "notice.renderer_left_out.effects": "gölgeler, 3B ve diğer efektler çıkarıldı", + "notice.renderer_left_out.vertical_text_turned": "Doğu Asya dikey metni yan çevrildi", "notice.renderer_left_out.more": "ve dahası", "engine.reason.not_installed": "Kurulu değil", "engine.reason.error": "Denetim sırasında hata", diff --git a/src/Filee.App/Assets/i18n/vi.json b/src/Filee.App/Assets/i18n/vi.json index 118ce8669..afdea2e1b 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "{0} công thức được viết dưới dạng văn bản thường", "notice.renderer_left_out.formulas_one": "1 công thức được viết dưới dạng văn bản thường", "notice.renderer_left_out.vertical_text": "chữ dọc được đặt nằm ngang", + "notice.renderer_left_out.form_controls_pictured": "{0} điều khiển biểu mẫu được vẽ từ ảnh đã lưu của chúng", + "notice.renderer_left_out.form_controls_pictured_one": "1 điều khiển biểu mẫu được vẽ từ ảnh đã lưu của nó", + "notice.renderer_left_out.shapes": "{0} hình được vẽ thành hình chữ nhật", + "notice.renderer_left_out.shapes_one": "1 hình được vẽ thành hình chữ nhật", + "notice.renderer_left_out.shapes_left_out": "bỏ qua {0} hình", + "notice.renderer_left_out.shapes_left_out_one": "bỏ qua 1 hình", + "notice.renderer_left_out.ink": "bỏ qua {0} nét vẽ mực", + "notice.renderer_left_out.ink_one": "bỏ qua 1 nét vẽ mực", + "notice.renderer_left_out.wordart": "WordArt được vẽ thành chữ thường", + "notice.renderer_left_out.effects": "bỏ qua bóng đổ, 3D và các hiệu ứng khác", + "notice.renderer_left_out.vertical_text_turned": "chữ dọc Đông Á được xoay nằm nghiêng", "notice.renderer_left_out.more": "và các mục khác", "engine.reason.not_installed": "Chưa cài đặt", diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json index 417976ddd..0eb98d11e 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "{0} 个公式以纯文本显示", "notice.renderer_left_out.formulas_one": "1 个公式以纯文本显示", "notice.renderer_left_out.vertical_text": "竖排文字按横排显示", + "notice.renderer_left_out.form_controls_pictured": "{0} 个表单控件按其保存的图片绘制", + "notice.renderer_left_out.form_controls_pictured_one": "1 个表单控件按其保存的图片绘制", + "notice.renderer_left_out.shapes": "{0} 个形状绘制为矩形", + "notice.renderer_left_out.shapes_one": "1 个形状绘制为矩形", + "notice.renderer_left_out.shapes_left_out": "省略了 {0} 个形状", + "notice.renderer_left_out.shapes_left_out_one": "省略了 1 个形状", + "notice.renderer_left_out.ink": "省略了 {0} 处墨迹", + "notice.renderer_left_out.ink_one": "省略了 1 处墨迹", + "notice.renderer_left_out.wordart": "艺术字按普通文字绘制", + "notice.renderer_left_out.effects": "省略了阴影、三维等效果", + "notice.renderer_left_out.vertical_text_turned": "竖排的东亚文字被侧转显示", "notice.renderer_left_out.more": "等", "engine.reason.not_installed": "未安装", diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.json index dfb2450d6..2b0988f92 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -270,6 +270,17 @@ "notice.renderer_left_out.formulas": "{0} 個公式以純文字顯示", "notice.renderer_left_out.formulas_one": "1 個公式以純文字顯示", "notice.renderer_left_out.vertical_text": "直書文字以橫書顯示", + "notice.renderer_left_out.form_controls_pictured": "{0} 個表單控制項以其儲存的圖片繪製", + "notice.renderer_left_out.form_controls_pictured_one": "1 個表單控制項以其儲存的圖片繪製", + "notice.renderer_left_out.shapes": "{0} 個圖案以矩形繪製", + "notice.renderer_left_out.shapes_one": "1 個圖案以矩形繪製", + "notice.renderer_left_out.shapes_left_out": "略過了 {0} 個圖案", + "notice.renderer_left_out.shapes_left_out_one": "略過了 1 個圖案", + "notice.renderer_left_out.ink": "略過了 {0} 個筆跡", + "notice.renderer_left_out.ink_one": "略過了 1 個筆跡", + "notice.renderer_left_out.wordart": "文字藝術師以一般文字繪製", + "notice.renderer_left_out.effects": "略過了陰影、立體等效果", + "notice.renderer_left_out.vertical_text_turned": "東亞直書文字側轉顯示", "notice.renderer_left_out.more": "等", "engine.reason.not_installed": "未安裝", diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs index c7187558d..3b5f421d2 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs @@ -306,6 +306,9 @@ private HTextBox TextBox(XElement wsp, XElement content, int width, int height, var warp = Attr(bodyPr?.Element(A + "prstTxWarp"), "prst"); if (warp is not null || Attr(bodyPr, "fromWordArt") is "1" or "true") box.TextArt = new HTextArt(warp is not (null or "textNoShape"), TextOpacity(content)); + // Text written top to bottom (turned, or East Asian vertical) is set horizontally in the box: the note says so. + if (Attr(bodyPr, "vert") is "vert" or "vert270" or "eaVert" or "wordArtVert" or "wordArtVertRtl" or "mongolianVert") + _leftOut.Add(HLeftOutKind.VerticalText); ReadTextBoxContent(content, box.Blocks); return box; } @@ -641,8 +644,11 @@ private IEnumerable Vml(XElement container, HCharFormat format) OuterMargin = distance, }; } - else if (shape.Element(V + "textbox")?.Element(W + "txbxContent") is { } content) + else if (shape.Element(V + "textbox") is { } textbox && textbox.Element(W + "txbxContent") is { } content) { + // A VML text box written top to bottom (layout-flow: vertical or vertical-ideographic) is set horizontally. + if (VmlStyle((string?)textbox.Attribute("style")).GetValueOrDefault("layout-flow") is { } flow && flow.StartsWith("vertical", StringComparison.Ordinal)) + _leftOut.Add(HLeftOutKind.VerticalText); var box = new TextBoxBuilder(width, height, anchor) { Fill = VmlFill(shape), diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs index ceb7acbac..fa7d63b8b 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs @@ -46,6 +46,10 @@ internal sealed partial class DocxReader private readonly Dictionary _endnotes = []; private readonly List _fields = []; private readonly HLeftOut _leftOut = new(); + + /// Header and footer parts read so far (see ). + private readonly HashSet _headerFooterParts = new(StringComparer.OrdinalIgnoreCase); + private Part? _footnotesPart; private Part? _endnotesPart; private bool _evenAndOddHeaders; @@ -379,13 +383,22 @@ private List HeaderFooterBlocks(XElement reference) if (Attr(reference, "id", R) is { } id && _part.Relationships.TryGetValue(id, out var rel) && LoadPart(rel.Target) is { } part) { _inFirstPageHeader = reference.Name == W + "headerReference" && Attr(reference, "type") == "first"; - WithPart(part, () => + // A header or footer is read again for every section that names it; what it leaves out is counted once. + _leftOut.Muted = !_headerFooterParts.Add(rel.Target); + try + { + WithPart(part, () => + { + foreach (var element in Flatten(part.Xml.Root?.Elements() ?? [])) + ReadBlock(element, blocks); + return 0; + }); + } + finally { - foreach (var element in Flatten(part.Xml.Root?.Elements() ?? [])) - ReadBlock(element, blocks); - return 0; - }); - _inFirstPageHeader = false; + _inFirstPageHeader = false; + _leftOut.Muted = false; + } } return blocks; } @@ -1191,6 +1204,9 @@ private void RunChild(XElement child, ParagraphBuilder builder, HCharFormat form } if (child.Element(O + "OLEObject") is not null) _leftOut.Add(pictured ? HLeftOutKind.ObjectPictured : HLeftOutKind.ObjectLeftOut); + // An ActiveX control (w:control): only the picture Word saved of it is drawn. + else if (child.Element(W + "control") is not null) + _leftOut.Add(pictured ? HLeftOutKind.FormControlPictured : HLeftOutKind.FormControl); } break; case "footnoteReference": @@ -1385,6 +1401,9 @@ private sealed class FieldState /// A table of contents (TOC): its entries don't take the Hyperlink character style's look. public bool TableOfContents; + + /// A legacy form field's settings (w:ffData in the begin w:fldChar): a check box's state, a drop-down's entries. + public XElement? FormData; } /// Text is in the entries of a table of contents. @@ -1410,7 +1429,7 @@ private void FieldChar(XElement fldChar, ParagraphBuilder builder, HCharFormat f if (_fields.Count >= Nesting.MaxDepth) _untrackedFields++; else - _fields.Add(new FieldState()); + _fields.Add(new FieldState { FormData = fldChar.Element(W + "ffData") }); break; case "separate": if (_untrackedFields == 0 && _fields.LastOrDefault() is { Result: false } field) @@ -1474,7 +1493,37 @@ private void StartFieldResult(FieldState field, ParagraphBuilder builder, HCharF builder.Add(new HField(HFieldKind.TotalPages, format)); field.Suppress = true; break; + case "FORMCHECKBOX" or "FORMDROPDOWN" when ShowsText() && FormFieldText(field.FormData) is { } shown: + // A legacy check box or drop-down has no result text: Word prints the box (checked or not) and the + // chosen entry. + builder.Add(new HText(shown, format)); + field.Suppress = true; + break; + } + } + + /// + /// What a legacy form field shows (w:ffData): a check box as ☒ or ☐ (w:checked, else its default), a drop-down as + /// its chosen entry (w:result, else w:default, else the first); null for other fields. + /// + private static string? FormFieldText(XElement? formData) + { + if (formData?.Element(W + "checkBox") is { } checkBox) + { + var state = checkBox.Element(W + "checked") ?? checkBox.Element(W + "default"); + var on = state is not null && Val(state) is not ("0" or "false" or "off") + && (state.Name.LocalName == "checked" || Val(state) is "1" or "true" or "on"); + return on ? "☒" : "☐"; + } + if (formData?.Element(W + "ddList") is { } list) + { + var entries = list.Elements(W + "listEntry").Select(Val).ToList(); + if (entries.Count == 0) + return null; + var chosen = int.TryParse(Val(list.Element(W + "result")) ?? Val(list.Element(W + "default")), out var index) ? index : 0; + return entries[chosen >= 0 && chosen < entries.Count ? chosen : 0] ?? ""; } + return null; } private void EndField(ParagraphBuilder builder) diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.Layout.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.Layout.cs index 47d1c82fc..01320b8ff 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.Layout.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.Layout.cs @@ -449,7 +449,7 @@ internal enum HLeftOutKind /// An embedded (OLE or ODF) object without a saved picture to draw. ObjectLeftOut, - /// A form control the reader doesn't draw (ODT draw:control). + /// A form control the reader doesn't draw (ODT draw:control, a DOCX ActiveX control without a picture). FormControl, /// A video (HWPX hp:video). @@ -458,10 +458,16 @@ internal enum HLeftOutKind /// An embedded object drawn from the picture its program saved with it (an OLE preview, an ODF replacement). ObjectPictured, + /// A form control drawn from the picture its program saved with it (a DOCX ActiveX control's picture). + FormControlPictured, + /// An ODF formula object written as its formula text. FormulaAsText, - /// Vertical text (a table cell, text box or section written top to bottom), set horizontally. Not counted. + /// + /// Vertical text (a table cell, text box or section written top to bottom; a DOCX text box's wps:bodyPr vert or VML + /// layout-flow), set horizontally. Not counted. + /// VerticalText, /// A chart Filee doesn't draw, drawn from the picture its program saved with it (an ODF chart's replacement). @@ -476,7 +482,17 @@ internal sealed class HLeftOut { private readonly Dictionary _counts = []; - public void Add(HLeftOutKind kind) => _counts[kind] = _counts.GetValueOrDefault(kind) + 1; + /// + /// While set, nothing is counted: a part read again for another place that shows it (a DOCX header shared by + /// several sections) was counted the first time. + /// + public bool Muted { get; set; } + + public void Add(HLeftOutKind kind) + { + if (!Muted) + _counts[kind] = _counts.GetValueOrDefault(kind) + 1; + } public int this[HLeftOutKind kind] => _counts.GetValueOrDefault(kind); diff --git a/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs b/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs index 8f4d31ac6..a2cc2573d 100644 --- a/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs +++ b/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs @@ -23,7 +23,8 @@ // their replacement picture and are named by the check); their points count against a ChartBudget. // Left out: charts OdfChartReader doesn't draw, OLE objects, 3D scenes, Fontwork, shadows, form controls, notes, animations; the capability check // (FirstUnsupported, a pass of this reader that draws nothing) names the first of them on a shown page, so such a file -// can keep LibreOffice's route. +// can keep LibreOffice's route. Read for drawing, the same places count each kind once per element for the deck's note +// (SlideDeck.LeftOut, SlideLeftOut), with objects drawn from their replacement pictures. // // Hostile input: the package through DocumentPackage, a flat file through SafeXml (OdfDrawPackage); pages up to // MaxPages; DocumentBudget for paragraphs, objects and nesting (groups, lists); a character count for text; limits for @@ -97,6 +98,12 @@ internal sealed partial class OdfDrawReader /// The first thing on a shown page this reader doesn't draw, found while reading. private string? _unsupported; + /// What the renderer leaves out or draws another way, for the deck's note (not counted by the check). + private readonly SlideLeftOut _leftOut = new(); + + /// Elements counted for the note, with the kind: a master's shapes are read again for every page on it. + private readonly HashSet<(XElement Element, string Kind)> _counted = []; + private OdfDrawReader(OdfDrawPackage package, CancellationToken cancellationToken, bool scan) { _package = package; @@ -135,6 +142,20 @@ public static SlideDeck Read(string path, DocumentLimits? limits = null, Cancell private void Unsupported(string feature) => _unsupported ??= feature; + /// Names for the check and counts for the note. + private void Unsupported(string feature, string kind, XElement? element) + { + Unsupported(feature); + LeftOut(kind, element); + } + + /// Counts (SlideLeftOut) for the note, once per . + private void LeftOut(string kind, XElement? element) + { + if (!_scan && (element is null || _counted.Add((element, kind)))) + _leftOut.Add(kind); + } + private static float? Points(string? value) => OdfGraphicStyles.Points(value) is { } points ? Math.Clamp(points, -MaxLength, MaxLength) : null; private static string? Attr(XElement? element, XName name) => (string?)element?.Attribute(name); @@ -221,7 +242,9 @@ private SlideDeck ReadDeck() }; deck.Slides.Add(ReadPage(page, pageProps, context)); } - return deck ?? NewDeck(_styles.MasterPages.Values.FirstOrDefault()); + deck ??= NewDeck(_styles.MasterPages.Values.FirstOrDefault()); + deck.LeftOut.AddRange(_leftOut.Parts); + return deck; } private SlideDeck NewDeck(XElement? master) @@ -266,7 +289,7 @@ private void Elements(IEnumerable elements, List output, return; if (element.Name == Dr3d + "scene") { - Unsupported("3D scenes"); + Unsupported("3D scenes", "shapes_left_out", element); continue; } if (element.Name.Namespace != Draw) @@ -319,7 +342,7 @@ private void Elements(IEnumerable elements, List output, case "polyline" or "polygon" or "path": return PathShape(element, context, scope); case "control": - Unsupported("form controls"); + Unsupported("form controls", "form_controls", element); return null; default: return null; @@ -490,11 +513,11 @@ private static SlidePath Rectangle(SKRect bounds, float radius = 0) if (!_geometries.TryGetValue(geometryElement, out var geometry)) _geometries[geometryElement] = geometry = OdfEnhancedGeometry.Parse(geometryElement); if (geometry.Extruded) - Unsupported("3D effects"); + Unsupported("3D effects", "effects", element); if (geometry.Fontwork) - Unsupported("Fontwork"); + Unsupported("Fontwork", "wordart", element); if (!geometry.HasPath && OdfEnhancedGeometry.Preset(geometry.Type) is null) - Unsupported($"the shape {geometry.Type}"); + Unsupported($"the shape {geometry.Type}", "shapes", element); Commands(geometry.Cost); var result = geometry.Evaluate(shape.Bounds.Width, shape.Bounds.Height, shape.Fill is not null, shape.Line is not null); shape.Paths = result.Paths; @@ -639,7 +662,7 @@ private static (double X, double Y, double Width, double Height) ViewBox(string? private void Shadow(OdfProps props) { if (props[Draw + "shadow"] == "visible") - Unsupported("shadows"); + Unsupported("shadows", "effects", null); } // ───────────────────────── Frames ───────────────────────── @@ -680,8 +703,10 @@ private void Shadow(OdfProps props) shape.Fill = new PictureFill(new SlideImage(() => ChartPicture.Png(chart, width, height) ?? []), SKRect.Empty, SKRect.Empty); return shape; } - if (content.Name == Draw + "object" || content.Name == Draw + "object-ole") - Unsupported(ObjectKind(content)); + // An object the renderer doesn't draw: its replacement picture, when there is one, stands in for it. + var embedded = content.Name == Draw + "object" || content.Name == Draw + "object-ole" ? ObjectKind(content) : null; + if (embedded is not null) + Unsupported(embedded); // A picture, or an object's replacement picture: the first one Filee can draw. var images = element.Elements(Draw + "image").ToList(); @@ -691,11 +716,15 @@ private void Shadow(OdfProps props) { shape.Fill = new PictureFill(new SlideImage(bytes), SKRect.Empty, SKRect.Empty); shape.IsPicture = true; + if (embedded is not null) + LeftOut(embedded == "charts" ? "charts_pictured" : "objects_pictured", element); return shape; } } + if (embedded is not null) + LeftOut(embedded == "charts" ? "charts" : "objects", element); if (images.Count > 0 && content.Name == Draw + "image") - Unsupported(PictureKind(Attr(images[0], XLink + "href"))); + Unsupported(PictureKind(Attr(images[0], XLink + "href")), "pictures", element); return shape.Fill is null && shape.Line is null ? null : shape; } diff --git a/src/Filee.Engines/Office/Ppt/PptReader.cs b/src/Filee.Engines/Office/Ppt/PptReader.cs index 22d708905..7d9f4096a 100644 --- a/src/Filee.Engines/Office/Ppt/PptReader.cs +++ b/src/Filee.Engines/Office/Ppt/PptReader.cs @@ -64,11 +64,21 @@ public static void ToPptx(string path, string output, DocumentLimits? limits = n /// The PPTX package of the presentation at in memory, with what the translation left out that /// would show (). /// - public static MemoryStream ToPptx(string path, DocumentLimits limits, out string? dropped, CancellationToken cancellationToken = default) + public static MemoryStream ToPptx(string path, DocumentLimits limits, out string? dropped, CancellationToken cancellationToken = default) => + ToPptx(path, limits, out dropped, out _, cancellationToken); + + /// + /// The PPTX package of the presentation at in memory, with what the translation left out that + /// would show (), and by the deck note's kind (). + /// + public static MemoryStream ToPptx(string path, DocumentLimits limits, out string? dropped, out IReadOnlyDictionary droppedKinds, + CancellationToken cancellationToken = default) { using var file = PptFile.Open(path, limits); var stream = new MemoryStream(); - dropped = PptToPptx.Write(file, stream, cancellationToken); + var kinds = new Dictionary(StringComparer.Ordinal); + dropped = PptToPptx.Write(file, stream, cancellationToken, kinds); + droppedKinds = kinds; stream.Position = 0; return stream; } diff --git a/src/Filee.Engines/Office/Ppt/PptToPptx.Geometry.cs b/src/Filee.Engines/Office/Ppt/PptToPptx.Geometry.cs index 18a8441a7..1ca0faa1c 100644 --- a/src/Filee.Engines/Office/Ppt/PptToPptx.Geometry.cs +++ b/src/Filee.Engines/Office/Ppt/PptToPptx.Geometry.cs @@ -96,13 +96,8 @@ private static List> ArrayElements(ArraySegment data) ? ArrayElements(segmentData).Select(s => (ushort)(s[0] | s[1] << 8)).ToList() : DefaultSegments(properties.GetValueOrDefault(ShapePath, 4u), points.Count); var builder = new PathBuilder(this, left, top, width, height); - return builder.Build(points, segments) ? builder.ToXml() : Drop("freeform shapes", null); - } - - private string? Drop(string feature, string? result) - { - Drop(feature); - return result; + // A path Filee can't draw: the shape is named and counted where it is written (WriteShape). + return builder.Build(points, segments) ? builder.ToXml() : null; } /// The segments shapePath means when there is no segment information: lines or Béziers, open or closed. diff --git a/src/Filee.Engines/Office/Ppt/PptToPptx.Shapes.cs b/src/Filee.Engines/Office/Ppt/PptToPptx.Shapes.cs index 97bac20ae..dcbfcc14c 100644 --- a/src/Filee.Engines/Office/Ppt/PptToPptx.Shapes.cs +++ b/src/Filee.Engines/Office/Ppt/PptToPptx.Shapes.cs @@ -318,7 +318,7 @@ private void WriteShape(StringBuilder xml, OfficeArtShape shape, Sheet sheet) } if ((shape.Flags & OleShape) != 0) { - Drop("OLE objects without a picture"); + Drop("OLE objects without a picture", "objects"); return; } if (WordArtText(shape, properties) is { } wordArt) @@ -334,7 +334,8 @@ private void WriteShape(StringBuilder xml, OfficeArtShape shape, Sheet sheet) geometry = custom; else { - Drop("freeform shapes"); + // Its text stays, in a rectangle; without text nothing of it is drawn. + Drop("freeform shapes", text is null ? "shapes_left_out" : "shapes"); if (text is null) return; } diff --git a/src/Filee.Engines/Office/Ppt/PptToPptx.cs b/src/Filee.Engines/Office/Ppt/PptToPptx.cs index 05896449f..8f6593d0f 100644 --- a/src/Filee.Engines/Office/Ppt/PptToPptx.cs +++ b/src/Filee.Engines/Office/Ppt/PptToPptx.cs @@ -18,7 +18,8 @@ // Left for later: adjust values of presets (they take their default shape), tables as tables (they are drawn as the // group of shapes PowerPoint 97–2003 keeps them as), headers and footers on slides, numbered lists, hyperlinks, // animations, sounds, OLE objects other than their picture. -// What the translation leaves out that shows is in Dropped, which the capability check names. +// What the translation leaves out that shows is in Dropped, which the capability check names, and counted by kind in +// DroppedKinds for the deck's note (SlideLeftOut). // // Hostile input: everything PptFile bounds, plus DocumentBudget for shapes (MaxObjects), paragraphs (MaxParagraphs) // and group nesting (MaxNestingDepth); copies of masters for colour schemes are capped (MaxVariants); pictures inflate @@ -66,17 +67,27 @@ private PptToPptx(PptFile file, ZipArchive zip, CancellationToken cancellationTo /// The first thing in the deck the translation leaves out that would show on a slide, or null. public string? Dropped { get; private set; } + private readonly Dictionary _droppedKinds = new(StringComparer.Ordinal); + /// Writes the PPTX package of to (left open). + /// Filled with what was left out by the deck note's kind (SlideLeftOut) and how often. /// What the translation left out that shows (for the capability check), or null. - public static string? Write(PptFile file, Stream output, CancellationToken cancellationToken = default) + public static string? Write(PptFile file, Stream output, CancellationToken cancellationToken = default, Dictionary? droppedKinds = null) { using var zip = new ZipArchive(output, ZipArchiveMode.Create, leaveOpen: true); var writer = new PptToPptx(file, zip, cancellationToken); writer.WritePackage(); + foreach (var (kind, count) in writer._droppedKinds) + droppedKinds?.Add(kind, count); return writer.Dropped; } - private void Drop(string feature) => Dropped ??= feature; + /// Names for the check and counts one for the note. + private void Drop(string feature, string kind) + { + Dropped ??= feature; + _droppedKinds[kind] = _droppedKinds.GetValueOrDefault(kind) + 1; + } // ───────────────────────── Package ───────────────────────── @@ -208,7 +219,7 @@ private void Write(string name, string xml, string? contentType = null) } else { - Drop("pictures in a format Filee doesn't read (PICT)"); + Drop("pictures in a format Filee doesn't read (PICT)", "pictures"); } _media[pib] = name; return name; diff --git a/src/Filee.Engines/Slides/PptxSlideReader.cs b/src/Filee.Engines/Slides/PptxSlideReader.cs index 3c8e0ff43..62840a399 100644 --- a/src/Filee.Engines/Slides/PptxSlideReader.cs +++ b/src/Filee.Engines/Slides/PptxSlideReader.cs @@ -16,7 +16,8 @@ // picture stored for them, mc:AlternateContent through its fallback. // - Fills: solid, gradient (linear, circle, rectangle; group fill), picture, pattern. Lines: width, dash, cap, join, // compound, arrowheads. -// Shadows, glow, reflection, 3D and WordArt warps are left out; the capability check (SlideCapabilities) names them. +// Shadows, glow, reflection, 3D and WordArt warps are left out; the capability check (SlideCapabilities) names them, +// and its pass run to the end is the deck's note when the renderer draws the deck anyway (SlideDeck.LeftOut). // // Hostile input: parts through DocumentPackage (sizes, inflate ratio, XML depth and elements), DocumentBudget for // paragraphs, objects and group nesting, and limits for the whole deck on path commands (each path counts as one more, @@ -92,10 +93,18 @@ private PptxSlideReader(SlidePackage package, CancellationToken cancellationToke } /// Reads the deck at . - public static SlideDeck Read(string path, DocumentLimits? limits = null, CancellationToken cancellationToken = default) + /// Also count what the renderer leaves out or draws another way, for the deck's note (). + public static SlideDeck Read(string path, DocumentLimits? limits = null, CancellationToken cancellationToken = default, bool leftOut = false) { using var package = SlidePackage.Open(path, limits); - return new PptxSlideReader(package, cancellationToken).ReadDeck(); + var deck = new PptxSlideReader(package, cancellationToken).ReadDeck(); + if (leftOut) + { + var counted = new SlideLeftOut(); + SlideCapabilities.Count(package, counted, cancellationToken); + deck.LeftOut.AddRange(counted.Parts); + } + return deck; } private static float Points(double emu) => (float)(emu / EmuPerPoint); diff --git a/src/Filee.Engines/Slides/SlideCapabilities.cs b/src/Filee.Engines/Slides/SlideCapabilities.cs index d375101b6..1a55552e6 100644 --- a/src/Filee.Engines/Slides/SlideCapabilities.cs +++ b/src/Filee.Engines/Slides/SlideCapabilities.cs @@ -12,6 +12,10 @@ // // Each slide, layout and master is looked through once, in one pass over its elements, however often the presentation // lists it or slides use it; the scan stops when the conversion is cancelled. +// +// The same pass, run to the end (Count), is the deck's note when the renderer draws it anyway (no LibreOffice, or the +// user's choice): each kind it met with its count (SlideLeftOut), and objects and charts drawn from their stored +// pictures, which the check lets through. using System.Xml.Linq; using Filee.Engines.Documents; @@ -35,7 +39,7 @@ internal static class SlideCapabilities if (PptReader.Unsupported(path) is { } old) return old; using var package = SlidePackage.Open(path); - return package.Dropped ?? Scan(package, cancellationToken); + return package.Dropped ?? Scan(package, null, cancellationToken); } catch (Exception ex) when (ex is InvalidDataException or IOException or Core.Conversion.ConversionRefusedException) { @@ -44,7 +48,20 @@ internal static class SlideCapabilities } } - private static string? Scan(SlidePackage package, CancellationToken cancellationToken) + /// + /// What the renderer leaves out of or draws another way in the deck in , counted by kind + /// for the deck's note: what a PowerPoint 97–2003 translation left out, everything the check would name, and + /// embedded objects and charts drawn from the picture stored for them. The check's pass, to the end. + /// + public static void Count(SlidePackage package, SlideLeftOut leftOut, CancellationToken cancellationToken = default) + { + foreach (var (kind, count) in package.DroppedKinds) + leftOut.Add(kind, count); + Scan(package, leftOut, cancellationToken); + } + + /// The first feature named; with , every element is looked at and counted. + private static string? Scan(SlidePackage package, SlideLeftOut? leftOut, CancellationToken cancellationToken) { var presentationPart = package.MainPart ?? "ppt/presentation.xml"; var presentation = package.Part(presentationPart); @@ -86,13 +103,17 @@ internal static class SlideCapabilities } } + string? first = null; foreach (var part in parts) { - if (package.Part(part) is not { } root || Part(package, part, root, themedEffects, cancellationToken) is not { } feature) + if (package.Part(part) is not { } root) continue; - return feature; + var feature = Part(package, part, root, themedEffects, leftOut, cancellationToken); + first ??= feature; + if (first is not null && leftOut is null) + return first; } - return null; + return first; } /// @@ -121,64 +142,86 @@ private static IEnumerable Drawn(XElement root) } } - private static string? Part(SlidePackage package, string part, XElement root, HashSet themedEffects, CancellationToken cancellationToken) + /// + /// What an element is to the check (Feature, null when it is drawn as it is) and to the note (Kind, + /// null when nothing is lost). + /// + private readonly record struct Finding(string? Feature, string? Kind); + + private static string? Part(SlidePackage package, string part, XElement root, HashSet themedEffects, SlideLeftOut? leftOut, + CancellationToken cancellationToken) { var count = 0; + string? first = null; foreach (var element in Drawn(root)) { if (++count % 4096 == 0) cancellationToken.ThrowIfCancellationRequested(); - var feature = element.Name.LocalName switch + var finding = element.Name.LocalName switch { "graphicData" => Graphic(package, part, element), - "contentPart" => "ink", - "effectLst" when element.Elements().Any(e => Effects.Contains(e.Name.LocalName)) => "shadows and effects", - "effectDag" => "shadows and effects", - "effectRef" when int.TryParse((string?)element.Attribute("idx"), out var index) && themedEffects.Contains(index) => "shadows and effects", - "sp3d" when element.HasElements || element.Attributes().Any(a => a.Name.LocalName is "extrusionH" or "contourW" or "z") => "3D effects", - "prstTxWarp" when (string?)element.Attribute("prst") is { } warp && warp != "textNoShape" => "WordArt text warp", + "contentPart" => new Finding("ink", "ink"), + "effectLst" when element.Elements().Any(e => Effects.Contains(e.Name.LocalName)) => new Finding("shadows and effects", "effects"), + "effectDag" => new Finding("shadows and effects", "effects"), + "effectRef" when int.TryParse((string?)element.Attribute("idx"), out var index) && themedEffects.Contains(index) => + new Finding("shadows and effects", "effects"), + "sp3d" when element.HasElements || element.Attributes().Any(a => a.Name.LocalName is "extrusionH" or "contourW" or "z") => + new Finding("3D effects", "effects"), + "prstTxWarp" when (string?)element.Attribute("prst") is { } warp && warp != "textNoShape" => new Finding("WordArt text warp", "wordart"), "ln" when element.Parent?.Name.LocalName is "rPr" or "defRPr" or "endParaRPr" && element.Elements().Any(e => e.Name.LocalName != "noFill") => - "outlined text", - "bodyPr" when (string?)element.Attribute("vert") is "eaVert" or "wordArtVert" or "mongolianVert" or "wordArtVertRtl" => "vertical East Asian text", - "prstGeom" when (string?)element.Attribute("prst") is { } preset && !PresetShapes.Known(preset) => $"the preset shape {preset}", + new Finding("outlined text", "effects"), + "bodyPr" when (string?)element.Attribute("vert") is "eaVert" or "wordArtVert" or "mongolianVert" or "wordArtVertRtl" => + new Finding("vertical East Asian text", "vertical_text_turned"), + "prstGeom" when (string?)element.Attribute("prst") is { } preset && !PresetShapes.Known(preset) => + new Finding($"the preset shape {preset}", "shapes"), "blip" => Picture(package, part, element), - _ => null, + _ => default, }; - if (feature is not null) - return feature; + if (finding.Kind is { } kind) + leftOut?.Add(kind); + first ??= finding.Feature; + if (first is not null && leftOut is null) + return first; } - return null; + return first; } - /// A graphic frame's content: tables are drawn; charts, SmartArt and objects only with what they store for drawing. - private static string? Graphic(SlidePackage package, string part, XElement data) + /// + /// A graphic frame's content: tables are drawn; charts, SmartArt and objects only with what they store for drawing. + /// An object, or a chart ChartReader doesn't draw, is drawn from its stored picture when it has one (the note says so). + /// + private static Finding Graphic(SlidePackage package, string part, XElement data) { var uri = (string?)data.Attribute("uri") ?? ""; - if (data.Element(A + "tbl") is not null || data.Descendants(P + "pic").Any()) - return null; + if (data.Element(A + "tbl") is not null) + return default; + var pictured = data.Descendants(P + "pic").Any(); if (uri.EndsWith("/chart", StringComparison.Ordinal)) { var target = package.Target(part, (string?)data.Element(Charts.ChartReader.C + "chart")?.Attribute(R + "id")); - return Charts.ChartReader.Read(target is null ? null : package.Part(target), DrawingColors.FromTheme(null)).LeftOut; + if (Charts.ChartReader.Read(target is null ? null : package.Part(target), DrawingColors.FromTheme(null)).LeftOut is not { } chart) + return default; + return pictured ? new Finding(null, "charts_pictured") : new Finding(chart, "charts"); } if (uri.EndsWith("/chartex", StringComparison.Ordinal)) - return "charts"; + return pictured ? new Finding(null, "charts_pictured") : new Finding("charts", "charts"); + // SmartArt is drawn from the drawing PowerPoint keeps with it, never from a picture. if (uri.EndsWith("/diagram", StringComparison.Ordinal)) - return package.Part(package.DiagramDrawing(part, data)) is null ? "SmartArt" : null; - if (uri.EndsWith("/ole", StringComparison.Ordinal)) - return "OLE objects"; - return "embedded objects"; + return package.Part(package.DiagramDrawing(part, data)) is not null ? default : new Finding(pictured ? null : "SmartArt", "smartart"); + if (pictured) + return new Finding(null, "objects_pictured"); + return new Finding(uri.EndsWith("/ole", StringComparison.Ordinal) ? "OLE objects" : "embedded objects", "objects"); } - private static string? Picture(SlidePackage package, string part, XElement blip) + private static Finding Picture(SlidePackage package, string part, XElement blip) { var target = package.Target(part, (string?)blip.Attribute(R + "embed")); var extension = target is null ? "" : DocumentPackage.SafeExtension(target); return extension switch { - ".tif" or ".tiff" => "TIFF pictures", - ".wdp" or ".jxr" or ".hdp" => "JPEG XR pictures", - _ => null, + ".tif" or ".tiff" => new Finding("TIFF pictures", "pictures"), + ".wdp" or ".jxr" or ".hdp" => new Finding("JPEG XR pictures", "pictures"), + _ => default, }; } } diff --git a/src/Filee.Engines/Slides/SlideFormats.cs b/src/Filee.Engines/Slides/SlideFormats.cs index f47780768..b66ce4395 100644 --- a/src/Filee.Engines/Slides/SlideFormats.cs +++ b/src/Filee.Engines/Slides/SlideFormats.cs @@ -18,11 +18,14 @@ internal static class SlideFormats /// What a deck becomes through the slide renderer. public static readonly string[] Targets = ["pdf", "png", "jpg"]; - /// Reads a deck of format with its reader (PPT goes through PptxSlideReader too). + /// + /// Reads a deck of format with its reader (PPT goes through PptxSlideReader too), with what + /// the renderer leaves out of it or draws another way (). + /// public static SlideDeck Read(string path, string format, CancellationToken cancellationToken) => OdfDrawReader.Presentations.Contains(format, StringComparer.OrdinalIgnoreCase) ? OdfDrawReader.Read(path, cancellationToken: cancellationToken) - : PptxSlideReader.Read(path, cancellationToken: cancellationToken); + : PptxSlideReader.Read(path, cancellationToken: cancellationToken, leftOut: true); /// The first thing in the deck its reader doesn't draw (the capability check of the format); null when it draws everything. public static string? FirstUnsupported(string path, string format, CancellationToken cancellationToken = default) => diff --git a/src/Filee.Engines/Slides/SlideLeftOut.cs b/src/Filee.Engines/Slides/SlideLeftOut.cs new file mode 100644 index 000000000..7f79f3e8c --- /dev/null +++ b/src/Filee.Engines/Slides/SlideLeftOut.cs @@ -0,0 +1,32 @@ +// What the slide readers met that the slide renderer leaves out or draws another way, counted by kind for the deck's +// note (SlideDeck.LeftOut, "notice.renderer_left_out."): PPTX and PPT through SlideCapabilities' pass over the +// package, ODP and ODG while OdfDrawReader reads. Each kind is named once, in a fixed order: what is left out first, +// then what is drawn another way; effects, WordArt and vertical text have no count. + +using Filee.Core.Conversion; + +namespace Filee.Engines.Slides; + +internal sealed class SlideLeftOut +{ + /// The kinds in the order the note names them. + private static readonly string[] Order = + [ + "charts", "smartart", "objects", "form_controls", "pictures", "shapes_left_out", "ink", + "objects_pictured", "charts_pictured", "shapes", "wordart", "effects", "vertical_text_turned", + ]; + + /// Kinds named without a count: they are a look, not things one could count on the slide. + private static readonly HashSet Uncounted = ["wordart", "effects", "vertical_text_turned"]; + + private readonly Dictionary _counts = new(StringComparer.Ordinal); + + public void Add(string kind, int count = 1) => _counts[kind] = _counts.GetValueOrDefault(kind) + count; + + public bool IsEmpty => _counts.Count == 0; + + /// The note's parts: each kind met once, with its count (0 for the uncounted ones). + public List Parts => + [.. _counts.OrderBy(pair => Array.IndexOf(Order, pair.Key) is var index and >= 0 ? index : Order.Length) + .Select(pair => new LeftOutPart("notice.renderer_left_out." + pair.Key, Uncounted.Contains(pair.Key) ? 0 : pair.Value))]; +} diff --git a/src/Filee.Engines/Slides/SlideModel.cs b/src/Filee.Engines/Slides/SlideModel.cs index c0faa2cc0..8e14e7271 100644 --- a/src/Filee.Engines/Slides/SlideModel.cs +++ b/src/Filee.Engines/Slides/SlideModel.cs @@ -28,7 +28,7 @@ internal sealed class SlideDeck public string? Title { get; init; } public List Slides { get; } = []; - /// What the reader left out (videos, pictures it can't decode), for the file's result. + /// What the reader left out (videos, pictures it can't decode) or drew another way, for the file's result. public List LeftOut { get; } = []; } diff --git a/src/Filee.Engines/Slides/SlidePackage.cs b/src/Filee.Engines/Slides/SlidePackage.cs index c421fdf06..46c057968 100644 --- a/src/Filee.Engines/Slides/SlidePackage.cs +++ b/src/Filee.Engines/Slides/SlidePackage.cs @@ -31,13 +31,16 @@ public static SlidePackage Open(string path, DocumentLimits? limits = null) static Exception NotAPackage() => new InvalidDataException("The file is not a PowerPoint presentation (not a ZIP package). It may be damaged."); if (!CompoundFile.HasSignature(path)) return new(DocumentPackage.Open(path, limits, NotAPackage)); - var package = PptReader.ToPptx(path, limits ?? DocumentLimits.Default, out var dropped); - return new(DocumentPackage.Open(package, limits, NotAPackage)) { Dropped = dropped }; + var package = PptReader.ToPptx(path, limits ?? DocumentLimits.Default, out var dropped, out var kinds); + return new(DocumentPackage.Open(package, limits, NotAPackage)) { Dropped = dropped, DroppedKinds = kinds }; } /// For a PowerPoint 97–2003 deck: what its translation left out that would show on a slide. public string? Dropped { get; private init; } + /// For a PowerPoint 97–2003 deck: what its translation left out, counted by the deck note's kind (SlideLeftOut). + public IReadOnlyDictionary DroppedKinds { get; private init; } = new Dictionary(); + public DocumentLimits Limits => _package.Limits; public void Dispose() => _package.Dispose(); diff --git a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs index 70ad5a08c..68d1726d0 100644 --- a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs +++ b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs @@ -11,8 +11,8 @@ // presentations or spreadsheets, the engine leaves those files to it (its edge would win on cost otherwise). // // Its edges are Checked: before a file is converted, FirstUnsupported looks through it for what the layout engine -// doesn't draw yet (OLE objects, charts other than the DrawingML ones ChartReader draws, vertical text, form controls, -// SmartArt), and such a file takes the route +// doesn't draw yet (OLE objects, charts other than the DrawingML ones ChartReader draws, vertical text, form controls +// and ActiveX controls, SmartArt), and such a file takes the route // it would take without the engine (JobQueue). An HWP is looked through as the HWPX package it becomes; a deck by // SlideCapabilities (charts, effects, ...). Pictures of document pages go on from the PDF through PDFium, which applies // the image options; pictures of slides are drawn directly, with the image options applied here. @@ -20,7 +20,8 @@ // A document the engine converts anyway (no other route, or the user's choice) says what it left out: the readers count // what they meet that the engine can't draw (HDocument.LeftOut: charts, embedded objects drawn from their saved // pictures or left out, vertical text, ...) and the file's result names each kind once, with LibreOffice to install -// when it would draw a Word or OpenDocument file faithfully (LeftOutNotice). +// when it would draw a Word or OpenDocument file faithfully (LeftOutNotice). A deck says the same from what the slide +// readers met (SlideDeck.LeftOut: charts, objects drawn from their pictures, effects, ...). using System.Text; using System.Text.RegularExpressions; @@ -322,6 +323,7 @@ private static readonly (Regex Pattern, string Feature)[] WordMarkers = (WordOle(), "OLE objects and charts"), (WordVertical(), "vertical text"), (WordSmartArt(), "SmartArt"), + (WordControl(), "form controls"), ]; private static readonly (Regex Pattern, string Feature)[] OdtMarkers = @@ -399,16 +401,33 @@ public async Task> ConvertAsync(ConversionStep step, IProg HLeftOutKind.FormControl => "form_controls", HLeftOutKind.Video => "videos", HLeftOutKind.ObjectPictured => "objects_pictured", + HLeftOutKind.FormControlPictured => "form_controls_pictured", HLeftOutKind.ChartPictured => "charts_pictured", HLeftOutKind.FormulaAsText => "formulas", _ => "vertical_text", }; + /// + /// What the slide readers met in that the slide renderer leaves out or draws another way, as + /// the file's note (as for documents, above); null when it drew everything. + /// + internal LeftOutNotice? LeftOutNotice(SlideDeck deck) => + deck.LeftOut.Count == 0 ? null + : new LeftOutNotice((_libreOffice ??= _findLibreOffice()) ? "notice.renderer_left_out" : "notice.renderer_left_out_libreoffice", deck.LeftOut); + /// A deck → one PDF, or a picture per slide (the PDF options' page range and resolution, then the image options). - private static async Task> ConvertSlidesAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) + private async Task> ConvertSlidesAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) { var deck = await Task.Run(() => SlideFormats.Read(step.InputPath, step.From, cancellationToken), cancellationToken); progress?.Report(0.2); + var written = await WriteSlidesAsync(step, deck, progress, cancellationToken); + if (written.Count > 0 && LeftOutNotice(deck) is { } leftOut) + step.LeftOut?.Invoke(leftOut); + return written; + } + + private static async Task> WriteSlidesAsync(ConversionStep step, SlideDeck deck, IProgress? progress, CancellationToken cancellationToken) + { var pages = progress is null ? null : new Progress(p => progress.Report(0.2 + 0.8 * p)); if (step.To == "pdf") { @@ -485,9 +504,14 @@ private static async Task> ConvertSlidesAsync(ConversionSt [GeneratedRegex(@"^word/charts/chart(?:Ex)?\d*\.xml$", RegexOptions.IgnoreCase)] private static partial Regex WordChartPart(); - [GeneratedRegex(@"Vertical text: a section or table cell (w:textDirection), a text box (wps:bodyPr vert, VML layout-flow). + [GeneratedRegex(@"]*?\svert=""(?:vert|vert270|eaVert|wordArtVert|wordArtVertRtl|mongolianVert)""|layout-flow:\s*vertical")] private static partial Regex WordVertical(); + /// An ActiveX control (w:control in a w:object), drawn only as the picture Word saved of it. + [GeneratedRegex(@"A floating text box (wps:wsp) with its text written as says. + private static string TextBox(string vert, string text) => DocxBuilder.Anchor(DocxBuilder.ShapeUri, + "" + + $"{DocxBuilder.P(DocxBuilder.R(text))}", + 900000, 1800000, "0", + "0"); + + /// A VML text box written top to bottom (Word's fallback for older readers, or an old document's own). + private const string VmlVerticalTextBox = + "" + + "Up"; + + /// An ActiveX control as Word writes it: the picture it saved of the control (when given) and the control. + private static string ActiveX(string? picture) => + "" + + "" + (picture is null ? "" : $"") + "" + + ""; + + /// A legacy form field (Developer → Legacy Tools): its settings in the begin fldChar, no result text. + private static string FormField(string instruction, string formData) => + $"{formData}" + + $" {instruction} "; + + [Fact] + public async Task Vertical_text_boxes_and_activex_controls_are_named_and_legacy_form_fields_drawn() + { + var builder = new DocxBuilder(); + var picture = builder.Image(DocumentFuzzTests.TinyPng(4, 3)); + builder.Paragraph(DocxBuilder.P(DocxBuilder.R("Boxes") + TextBox("eaVert", "세로") + TextBox("vert270", "Up"))) + .Paragraph(DocxBuilder.P(VmlVerticalTextBox)) + .Paragraph(DocxBuilder.P(DocxBuilder.R("Controls ") + ActiveX(picture) + ActiveX(null))) + .Paragraph(DocxBuilder.P(DocxBuilder.R("Agree ") + + FormField("FORMCHECKBOX", "") + + FormField("FORMCHECKBOX", "") + + DocxBuilder.R(" Pick ") + + FormField("FORMDROPDOWN", ""))); + var source = builder.Save(Path.Combine(fx.NewFolder(), "form.docx")); + + var notice = Assert.Single(await ConvertAsync(source, "docx")); + + // The legacy check boxes and drop-down are drawn as Word prints them, so the note doesn't name them. + Assert.Equal(WithLibreOffice, notice.Key); + Assert.Equal([Part("form_controls", 1), Part("form_controls_pictured", 1), Part("vertical_text")], notice.Parts); + var document = DocxReader.Read(source, Path.Combine(fx.NewFolder(), "media")); + var text = string.Concat(document.Sections.SelectMany(s => s.Blocks).OfType().SelectMany(p => p.Inlines).OfType().Select(t => t.Text)); + Assert.Contains("Agree ☒☐ Pick Beta", text); + + // The check names them too, so such a file keeps LibreOffice's route while LibreOffice is there. + var renderer = new FileeRendererConverter(); + var vertical = new DocxBuilder().Paragraph(DocxBuilder.P(TextBox("vert", "Down"))).Save(Path.Combine(fx.NewFolder(), "vertical.docx")); + var vml = new DocxBuilder().Paragraph(DocxBuilder.P(VmlVerticalTextBox)).Save(Path.Combine(fx.NewFolder(), "vml.docx")); + var control = new DocxBuilder().Paragraph(DocxBuilder.P(ActiveX(null))).Save(Path.Combine(fx.NewFolder(), "control.docx")); + var fields = new DocxBuilder().Paragraph(DocxBuilder.P(FormField("FORMCHECKBOX", ""))) + .Save(Path.Combine(fx.NewFolder(), "fields.docx")); + var across = new DocxBuilder().Paragraph(DocxBuilder.P(TextBox("horz", "Across"))).Save(Path.Combine(fx.NewFolder(), "across.docx")); + Assert.Equal("vertical text", renderer.FirstUnsupported(vertical, "docx")); + Assert.Equal("vertical text", renderer.FirstUnsupported(vml, "docx")); + Assert.Equal("form controls", renderer.FirstUnsupported(control, "docx")); + Assert.Null(renderer.FirstUnsupported(fields, "docx")); + Assert.Null(renderer.FirstUnsupported(across, "docx")); + Assert.Empty(await ConvertAsync(fields, "docx")); + Assert.Empty(await ConvertAsync(across, "docx")); + } + + [Fact] + public async Task A_chart_in_a_header_several_sections_share_is_counted_once() + { + var builder = new DocxBuilder(); + var header = builder.Header(DocxBuilder.P(Chart(1))); + var reference = $""; + // Three sections on their own pages, each naming the same header: it is read for each of them. + builder.Paragraph(DocxBuilder.P(DocxBuilder.R("One"), $"{reference}{DocxBuilder.DefaultPage}")) + .Paragraph(DocxBuilder.P(DocxBuilder.R("Two"), $"{reference}{DocxBuilder.DefaultPage}")) + .Paragraph(DocxBuilder.P(DocxBuilder.R("Three"))); + builder.SectionProperties = reference + DocxBuilder.DefaultPage; + var source = builder.Save(Path.Combine(fx.NewFolder(), "header.docx")); + + var document = DocxReader.Read(source, Path.Combine(fx.NewFolder(), "media")); + Assert.Equal(3, document.Sections.Count); + Assert.All(document.Sections, section => Assert.NotEmpty(section.Headers)); + + var notice = Assert.Single(await ConvertAsync(source, "docx")); + Assert.Equal([Part("charts", 1)], notice.Parts); + } + + // ───────────────────────── Slides ───────────────────────── + + private static string Frame(SlideDeckBuilder deck, string uri, string content, double x) => + $"" + + $"{SlideDeckBuilder.Xfrm(x, 60, 200, 150, element: "p:xfrm")}{content}"; + + [Fact] + public async Task A_powerpoint_deck_names_what_the_slide_renderer_leaves_out_and_draws_another_way() + { + var deck = new SlideDeckBuilder(); + var picture = deck.Media(SlideDeckBuilder.TestPicture(40, 30)); + // An OLE object with its picture, a chart PowerPoint 2016 made (chartex, no fallback), ink, a shadowed shape + // and a preset shape Filee doesn't know; the second slide has the same OLE object again. + var ole = Frame(deck, "http://schemas.openxmlformats.org/presentationml/2006/ole", + "" + + "", 20); + var chartEx = Frame(deck, "http://schemas.microsoft.com/office/drawing/2014/chartex", + "", 240); + var shadow = deck.Shape("rect", 480, 60, 100, 100, fill: SlideDeckBuilder.Solid("#2050A0") + + ""); + var unknown = deck.Shape("notAPresetShape", 600, 60, 100, 100, fill: SlideDeckBuilder.Solid("#20A050")); + deck.Slide(ole + chartEx + "" + shadow + unknown, pictures: new Dictionary { ["rId2"] = picture }) + .Slide(ole, pictures: new Dictionary { ["rId2"] = picture }); + var source = deck.Save(Path.Combine(fx.NewFolder(), "deck.pptx")); + + var notice = Assert.Single(await ConvertAsync(source, "pptx")); + + Assert.Equal(WithLibreOffice, notice.Key); + Assert.Equal([Part("charts", 1), Part("ink", 1), Part("objects_pictured", 2), Part("shapes", 1), Part("effects")], notice.Parts); + Assert.Equal(Key, Assert.Single(await ConvertAsync(source, "pptx", libreOffice: true)).Key); + } + + [Fact] + public async Task An_opendocument_presentation_names_what_the_slide_renderer_leaves_out_once_per_shape() + { + // An OLE object without a picture on the master shows on both pages and is counted once; a 3D scene, a shadow + // and an object drawn from its replacement picture are on the first page. + var builder = new OdpBuilder + { + ContentStyles = OdpBuilder.GraphicStyle("shadow", "draw:shadow=\"visible\" draw:fill=\"solid\""), + MasterStyles = "" + + "", + Pages = OdpBuilder.Page("" + + "" + + "" + + "") + + OdpBuilder.Page(""), + }; + builder.Parts["ObjectReplacements/Object 1"] = DocumentFuzzTests.TinyPng(4, 3); + var source = builder.Save(Path.Combine(fx.NewFolder(), "deck.odp")); + + var notice = Assert.Single(await ConvertAsync(source, "odp")); + + Assert.Equal(WithLibreOffice, notice.Key); + Assert.Equal([Part("objects", 1), Part("shapes_left_out", 1), Part("objects_pictured", 1), Part("effects")], notice.Parts); + } + + [Fact] + public async Task Decks_the_slide_renderer_draws_whole_get_no_note() + { + var deck = new SlideDeckBuilder(); + deck.Slide(deck.Shape("rect", 60, 60, 200, 100, fill: SlideDeckBuilder.Solid("#2050A0"), paragraphs: SlideDeckBuilder.Paragraph("Plain"))); + var odp = new OdpBuilder { Pages = OdpBuilder.Page("") }; + var folder = fx.NewFolder(); + + Assert.Empty(await ConvertAsync(deck.Save(Path.Combine(folder, "plain.pptx")), "pptx")); + Assert.Empty(await ConvertAsync(odp.Save(Path.Combine(folder, "plain.odp")), "odp")); + } } From fa40fba95db0253608a425e036741f5893bc1d83 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 22:59:06 +0900 Subject: [PATCH 4/7] feat(djvu): JBIG2 and G4 masks, links, bookmarks, indirect documents, Smmr - PDF masks are coded the smallest way: JBIG2 (the JB2 shapes as a symbol dictionary and a text region, MQ-coded, written from ITU-T T.88), CCITT G4 (own encoder from ITU-T T.6), or Flate for dithered pictures. The DjVu 3 specification goes from 6.0 MB to 1.45 MB, a 115-page scanned book from 13.9 MB to 1.5 MB; the pages draw pixel for pixel as before. - ANTa/ANTz hyperlinks become PDF link annotations: links into the document (by id, name, title, page number or +n/-n) and http, https, ftp and mailto addresses only. NAVM bookmarks become the PDF outline. - Indirect documents read their component files from below the index file's folder only (no "..", roots, drives, ':' or device names, no path through a link); a missing page or included file is still refused with error.djvu_indirect. - Smmr (G4) masks are read, whole or in stripes, inverted or not. - Bound annotation parts (DjvuLimits.MaxAnnotationNodes) and nesting. Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 57 ++- src/Filee.Engines/Djvu/CcittG4.cs | 442 +++++++++++++++++++++ src/Filee.Engines/Djvu/DjvuAnnotations.cs | 242 +++++++++++ src/Filee.Engines/Djvu/DjvuConverter.cs | 20 +- src/Filee.Engines/Djvu/DjvuDocument.cs | 370 ++++++++++++++--- src/Filee.Engines/Djvu/DjvuLimits.cs | 9 +- src/Filee.Engines/Djvu/DjvuPdfWriter.cs | 223 ++++++++++- src/Filee.Engines/Djvu/Jbig2Encoder.cs | 387 ++++++++++++++++++ tests/Filee.Engines.Tests/DjvuBuilder.cs | 97 ++++- tests/Filee.Engines.Tests/DjvuFuzzTests.cs | 25 +- tests/Filee.Engines.Tests/DjvuTests.cs | 437 +++++++++++++++++++- 11 files changed, 2202 insertions(+), 107 deletions(-) create mode 100644 src/Filee.Engines/Djvu/CcittG4.cs create mode 100644 src/Filee.Engines/Djvu/DjvuAnnotations.cs create mode 100644 src/Filee.Engines/Djvu/Jbig2Encoder.cs diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 72f35b6f9..2f48c38fa 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -36,7 +36,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | Data | built in (System.Text.Json, YamlDotNet, Tomlyn, System.Xml) | **JSON, JSON Lines, YAML, TOML, XML, CSV ↔ each other** (CSV for tables); **vCard, iCalendar ↔ CSV**; **property lists (.plist, XML and binary) → each of them and TXT**; FreeMASTER oscilloscope TXT → CSV; spreadsheets, contacts and events reach JSON / YAML / TOML / XML through CSV | | **E-books** | built in | **EPUB, MOBI/AZW/AZW3/PRC, FB2, HTMLZ, TXTZ, CHM, PDB, PML, TCR, RB, SNB, OEB → EPUB, AZW3, MOBI, PDB, PML, TCR, RB, SNB, HWPX (→ PDF), TXT, HTML, Markdown, FB2, HTMLZ, TXTZ**; HTML, Markdown, TXT, DOCX → EPUB/AZW3/MOBI/PDB/PML/TCR/RB/SNB/FB2/HTMLZ/TXTZ; HTML → TXT; comics CBZ/CBR/CB7/CBT/CBC → PDF, EPUB, CBZ; PDF → CBZ; AZW4 → PDF; **EPUB ↔ KEPUB** (Kobo) | | **Drawings** | built in | **Visio VSDX, ODG, OTG, FODG → PDF, SVG, PNG** with the slide renderer (a page per drawing page; see [Drawings](#drawings-visio-odg)); **XPS, OpenXPS → PDF, SVG, PNG** with Filee's own XPS reader ([XPS reader](#xps-and-openxps-reader)); **Excel chart templates (CRTX) → PDF, SVG, PNG** with the [chart drawer](#charts); **interactive whiteboards (IWB) and SMART Notebook → PDF, SVG, PNG, ODP, PPTX** ([Interactive whiteboards](#interactive-whiteboards-iwb-smart-notebook)); a binary `.vsd` and a drawing with what the readers don't draw yet (charts, OLE objects, 3D) keep LibreOffice's route | -| **DjVu** | built in | **DjVu (.djvu, .djv; one page or bundled) → PDF** (the layers kept: background JPEG, 1-bit masks; the hidden text searchable) and **PNG, JPG, TIFF** pictures of the pages (#346; see [DjVu](#djvu)) | +| **DjVu** | built in | **DjVu (.djvu, .djv; one page, bundled or indirect) → PDF** (the layers kept: background JPEG, 1-bit JBIG2 / G4 masks; the hidden text searchable; links and bookmarks) and **PNG, JPG, TIFF** pictures of the pages (#346; see [DjVu](#djvu)) | | CAD (ACadSharp + built-in renderer) | built in (library) | **DWG ↔ DXF**; DWG/DXF → **PDF** (vector), **SVG**, PNG, JPG, WEBP, TIFF, BMP, GIF, ICO, AVIF | | G-code | built in | **G-code (3D printers, CNC machines) → PDF** (vector), **SVG**, PNG, JPG, WEBP, TIFF, …: the toolpath from above or isometric, or the preview the slicer saved in the file | | **Subtitles** | built in | **SRT, ASS/SSA, WebVTT, SAMI (SMI), LRC, KuGou KRC, TTML, MicroDVD, SCC → SRT, ASS, WebVTT, SAMI, LRC, TTML, MicroDVD, TXT**, always UTF-8 (Korean CP949 / EUC-KR files read correctly) | @@ -4568,39 +4568,58 @@ and `.djv`, documents donut). `DjvuConverter` turns a DjVu document into **PDF** TIFF** pictures of its pages (the PDF options' page range, each page at its own resolution, then the image options; TIFF holds every page in one file). Other targets (DOCX, TXT, …) go on from the PDF. -- **Container:** IFF85 (`AT&T`, then a FORM chunk). One page (`FORM:DJVU`) or a bundled document (`FORM:DJVM`): the - `DIRM` directory (versions 0 and 1, its BZZ tables of sizes, flags and names) gives the components at their offsets - — pages, included files (`FORM:DJVI`) and thumbnails (skipped). An **indirect** document (pages in separate files) - is refused with `error.djvu_indirect`, which says to bundle it in a DjVu viewer first. A file cut short keeps the - chunks it still holds. +- **Container:** IFF85 (`AT&T`, then a FORM chunk). One page (`FORM:DJVU`) or a multi-page document (`FORM:DJVM`): + the `DIRM` directory (versions 0 and 1, its BZZ tables of sizes, flags, ids, names and titles) lists the components + — pages, included files (`FORM:DJVI`) and thumbnails (skipped) — and `NAVM` holds the bookmarks. A **bundled** + document has the components at offsets in the file. An **indirect** one names them as separate files next to its + index file: each is read only from below the index file's folder (a name with `..`, a drive, a root, a `:`, a + Windows device name, or a path through a symbolic link or junction counts as missing), with the same limits as a + bundled document. A missing page or included file refuses the document with `error.djvu_indirect` (which says to + bundle it in a DjVu viewer first); missing thumbnails don't matter. A file cut short keeps the chunks it still holds. - **Page:** `INFO` (size, resolution 25–6000 dpi, else 300; rotation → the PDF page's `/Rotate`, the picture turned), `INCL` (followed depth first, each file once, so include loops end), the mask `Sjbz` with its shared dictionary `Djbz` (in the page or an included file; a dictionary may build on another; decoded once per file and kept for the - next pages), the background `BG44` (or `BGjp`), foreground colours `FG44` (or `FGjp`) or the palette `FGbz` - (colours and a colour per shape), hidden text `TXTz` / `TXTa`. + next pages) or the G4 mask `Smmr` (whole or in stripes, black and white possibly swapped), the background `BG44` (or + `BGjp`), foreground colours `FG44` (or `FGjp`) or the palette `FGbz` (colours and a colour per shape), hidden text + `TXTz` / `TXTa`, and the hyperlinks of the annotations `ANTa` / `ANTz` (the page's and its included files'). - **Decoders:** the ZP-coder (the specification's probability table), BZZ (block sort transform, move-to-front with the specification's rank estimator), JB2 (all record types: new shapes coded directly, refinements against library - shapes, copies, non-mark data, comments, dictionary requests and number-coder resets) and IW44 (luminance and + shapes, copies, non-mark data, comments, dictionary requests and number-coder resets), IW44 (luminance and chrominance maps of 32 × 32 blocks, all bands and slices over several chunks, chrominance at half resolution or - starting late, the backward lifting transform, the YCbCr transform; grey pictures). + starting late, the backward lifting transform, the YCbCr transform; grey pictures) and CCITT G4 (`CcittG4`, written + from ITU-T T.6 and T.4's code tables; also the PDF writer's encoder). +- **Links and bookmarks:** a `(maparea url comment area …)` annotation with a `rect`, `oval`, `text` or `poly` area + (a polygon by its bounding box; lines aren't links) becomes a PDF link annotation. `#…` URLs go to a page — by its + id, name or title in the directory, its number from 1, or `#+n` / `#-n` pages from the link's page; other URLs only + when they are `http`, `https`, `ftp` or `mailto` addresses (a URI action), so `javascript:`, `file:`, relative files + and the like are no links. The bookmarks become the PDF outline (the first level open, at most 32 levels; deeper + ones go in at the 32nd); a bookmark Filee can't follow keeps its title without an action. - **Composition:** the background scaled up to the page (bilinear, by the reduction its size gives), then each mask pixel in its foreground colour (the FG44 picture, the shape's palette colour, or black); white without a background. - **PDF:** the layers stay layers (`DjvuPdfWriter`), so a scanned book stays small: the background as a JPEG at its - own resolution, the mask as 1-bit image masks (Flate) painted in their colour — one per palette colour, or the FG44 - picture shown through the mask (explicit masking). A page with more than 64 palette colours is one JPEG. The hidden + own resolution, the mask as 1-bit image masks painted in their colour — one per palette colour, or the FG44 picture + shown through the mask (explicit masking). Each mask is coded the smallest of three ways: **JBIG2** (`Jbig2Encoder`, + written from ITU-T T.88: the mask's JB2 shapes become a symbol dictionary and an immediate text region places them, + all MQ-coded — symbol heights and widths, generic template 0 bitmaps, strips of four rows, symbol ids), **CCITT G4** + (`/CCITTFaxDecode`, `/K -1`), or **Flate** (finely dithered pictures neither packs well). A text page's mask is + about the DjVu file's own size: the DjVu 3 specification (472 KB) went from 6.0 MB to 1.45 MB, a 115-page scanned + book (732 KB) from 13.9 MB to 1.5 MB. The masks draw exactly as before (checked pixel for pixel with PDFtoImage). A page with more than 64 palette colours is one JPEG. The hidden text's lines and words become an invisible text layer (the OCR layer's glyphless font, `Ocr/OcrTextLayer.cs`), so the PDF can be searched and its text copied in any script. - **Limits** (`DjvuLimits`, refusals with `error.document_too_complex`): 20,000 pages and 40,000 components; 64 MB per chunk and per BZZ stream; 150 million pixels per page; 64 million pixels per IW44 picture and 10,000 slices; 2 million shapes, 4 million placed shapes and 400 million shape pixels per mask (counted before a shape is - allocated); 20 million number-coder cells; 2 million text zones; 65,535 palette colours. Decoding is iterative (no - recursion over files or zones) and checks cancellation. -- **Left out:** `Smmr` masks (G4), JPEG 2000 layers (`BG2k`, `FG2k`) and JPEG layers Skia can't decode are counted as - pictures left out in the file's result. Not read: annotations (`ANTa`/`ANTz`: hyperlinks, highlights, background - colour), bookmarks (`NAVM`), thumbnails, standalone IW44 files (`FORM:PM44`/`BM44`), and hidden text of a page with - no zones below the page. + allocated); 20 million number-coder cells; 2 million text zones; 65,535 palette colours; 1 million parts (brackets, + words, strings) of one page's annotations. Annotations nested more than 64 deep or not balanced count as damage and + leave the page without links. Decoding is iterative (no recursion over files, zones, annotations or bookmarks) and + checks cancellation. +- **Left out:** JPEG 2000 layers (`BG2k`, `FG2k`) and JPEG layers Skia can't decode are counted as pictures left out + in the file's result. Not read: the other annotations (highlights, borders, background colour, zoom, metadata), + thumbnails, standalone IW44 files (`FORM:PM44`/`BM44`), and hidden text of a page with no zones below the page. - Tests (`DjvuTests.cs`, `DjvuFuzzTests.cs`) build every file in code: `DjvuBuilder.cs` has encoders for each piece - (ZP-coder, BZZ, JB2, IW44 with the forward transform, text zones, palettes, the bundled directory). + (ZP-coder, BZZ, JB2, IW44 with the forward transform, text zones, palettes, bookmarks, annotations, Smmr masks, the + bundled directory and indirect documents). The PDF's JBIG2, G4 and Flate masks are drawn back with PDFtoImage + (pdfium's own decoders) and compared with the page. ## FFmpeg diff --git a/src/Filee.Engines/Djvu/CcittG4.cs b/src/Filee.Engines/Djvu/CcittG4.cs new file mode 100644 index 000000000..ca75ccedc --- /dev/null +++ b/src/Filee.Engines/Djvu/CcittG4.cs @@ -0,0 +1,442 @@ +// CCITT Group 4 (ITU-T T.6, "MMR") coding of 1-bit pictures: every row coded two-dimensionally against the row above +// it (an all-white row above the first) with the pass, horizontal and vertical modes of T.4's 2-D coding, the +// horizontal runs in T.4's modified Huffman white and black codes (make-up codes for runs of 64 and more, the extended +// make-up codes from 1792 to 2560 shared by both colours). The data ends with EOFB (two EOLs). No uncompressed mode. +// +// DjvuPdfWriter codes a page mask with it (PDF's /CCITTFaxDecode with /K -1) when that is smaller than JBIG2 and +// Flate. DjvuDocument reads Smmr masks (DjVu's MMR-coded alternative to JB2) with the decoder. + +namespace Filee.Engines.Djvu; + +internal static class CcittG4 +{ + /// White run codes: (run, code bits, code length) — terminating 0–63, then make-up 64–1728. + private static readonly (int Run, int Code, int Length)[] WhiteCodes = + [ + (0, 0b00110101, 8), (1, 0b000111, 6), (2, 0b0111, 4), (3, 0b1000, 4), (4, 0b1011, 4), (5, 0b1100, 4), + (6, 0b1110, 4), (7, 0b1111, 4), (8, 0b10011, 5), (9, 0b10100, 5), (10, 0b00111, 5), (11, 0b01000, 5), + (12, 0b001000, 6), (13, 0b000011, 6), (14, 0b110100, 6), (15, 0b110101, 6), (16, 0b101010, 6), + (17, 0b101011, 6), (18, 0b0100111, 7), (19, 0b0001100, 7), (20, 0b0001000, 7), (21, 0b0010111, 7), + (22, 0b0000011, 7), (23, 0b0000100, 7), (24, 0b0101000, 7), (25, 0b0101011, 7), (26, 0b0010011, 7), + (27, 0b0100100, 7), (28, 0b0011000, 7), (29, 0b00000010, 8), (30, 0b00000011, 8), (31, 0b00011010, 8), + (32, 0b00011011, 8), (33, 0b00010010, 8), (34, 0b00010011, 8), (35, 0b00010100, 8), (36, 0b00010101, 8), + (37, 0b00010110, 8), (38, 0b00010111, 8), (39, 0b00101000, 8), (40, 0b00101001, 8), (41, 0b00101010, 8), + (42, 0b00101011, 8), (43, 0b00101100, 8), (44, 0b00101101, 8), (45, 0b00000100, 8), (46, 0b00000101, 8), + (47, 0b00001010, 8), (48, 0b00001011, 8), (49, 0b01010010, 8), (50, 0b01010011, 8), (51, 0b01010100, 8), + (52, 0b01010101, 8), (53, 0b00100100, 8), (54, 0b00100101, 8), (55, 0b01011000, 8), (56, 0b01011001, 8), + (57, 0b01011010, 8), (58, 0b01011011, 8), (59, 0b01001010, 8), (60, 0b01001011, 8), (61, 0b00110010, 8), + (62, 0b00110011, 8), (63, 0b00110100, 8), + (64, 0b11011, 5), (128, 0b10010, 5), (192, 0b010111, 6), (256, 0b0110111, 7), (320, 0b00110110, 8), + (384, 0b00110111, 8), (448, 0b01100100, 8), (512, 0b01100101, 8), (576, 0b01101000, 8), (640, 0b01100111, 8), + (704, 0b011001100, 9), (768, 0b011001101, 9), (832, 0b011010010, 9), (896, 0b011010011, 9), + (960, 0b011010100, 9), (1024, 0b011010101, 9), (1088, 0b011010110, 9), (1152, 0b011010111, 9), + (1216, 0b011011000, 9), (1280, 0b011011001, 9), (1344, 0b011011010, 9), (1408, 0b011011011, 9), + (1472, 0b010011000, 9), (1536, 0b010011001, 9), (1600, 0b010011010, 9), (1664, 0b011000, 6), + (1728, 0b010011011, 9), + ]; + + /// Black run codes: (run, code bits, code length) — terminating 0–63, then make-up 64–1728. + private static readonly (int Run, int Code, int Length)[] BlackCodes = + [ + (0, 0b0000110111, 10), (1, 0b010, 3), (2, 0b11, 2), (3, 0b10, 2), (4, 0b011, 3), (5, 0b0011, 4), + (6, 0b0010, 4), (7, 0b00011, 5), (8, 0b000101, 6), (9, 0b000100, 6), (10, 0b0000100, 7), + (11, 0b0000101, 7), (12, 0b0000111, 7), (13, 0b00000100, 8), (14, 0b00000111, 8), (15, 0b000011000, 9), + (16, 0b0000010111, 10), (17, 0b0000011000, 10), (18, 0b0000001000, 10), (19, 0b00001100111, 11), + (20, 0b00001101000, 11), (21, 0b00001101100, 11), (22, 0b00000110111, 11), (23, 0b00000101000, 11), + (24, 0b00000010111, 11), (25, 0b00000011000, 11), (26, 0b000011001010, 12), (27, 0b000011001011, 12), + (28, 0b000011001100, 12), (29, 0b000011001101, 12), (30, 0b000001101000, 12), (31, 0b000001101001, 12), + (32, 0b000001101010, 12), (33, 0b000001101011, 12), (34, 0b000011010010, 12), (35, 0b000011010011, 12), + (36, 0b000011010100, 12), (37, 0b000011010101, 12), (38, 0b000011010110, 12), (39, 0b000011010111, 12), + (40, 0b000001101100, 12), (41, 0b000001101101, 12), (42, 0b000011011010, 12), (43, 0b000011011011, 12), + (44, 0b000001010100, 12), (45, 0b000001010101, 12), (46, 0b000001010110, 12), (47, 0b000001010111, 12), + (48, 0b000001100100, 12), (49, 0b000001100101, 12), (50, 0b000001010010, 12), (51, 0b000001010011, 12), + (52, 0b000000100100, 12), (53, 0b000000110111, 12), (54, 0b000000111000, 12), (55, 0b000000100111, 12), + (56, 0b000000101000, 12), (57, 0b000001011000, 12), (58, 0b000001011001, 12), (59, 0b000000101011, 12), + (60, 0b000000101100, 12), (61, 0b000001011010, 12), (62, 0b000001100110, 12), (63, 0b000001100111, 12), + (64, 0b0000001111, 10), (128, 0b000011001000, 12), (192, 0b000011001001, 12), (256, 0b000001011011, 12), + (320, 0b000000110011, 12), (384, 0b000000110100, 12), (448, 0b000000110101, 12), + (512, 0b0000001101100, 13), (576, 0b0000001101101, 13), (640, 0b0000001001010, 13), + (704, 0b0000001001011, 13), (768, 0b0000001001100, 13), (832, 0b0000001001101, 13), + (896, 0b0000001110010, 13), (960, 0b0000001110011, 13), (1024, 0b0000001110100, 13), + (1088, 0b0000001110101, 13), (1152, 0b0000001110110, 13), (1216, 0b0000001110111, 13), + (1280, 0b0000001010010, 13), (1344, 0b0000001010011, 13), (1408, 0b0000001010100, 13), + (1472, 0b0000001010101, 13), (1536, 0b0000001011010, 13), (1600, 0b0000001011011, 13), + (1664, 0b0000001100100, 13), (1728, 0b0000001100101, 13), + ]; + + /// Make-up codes from 1792 to 2560, the same for both colours. + private static readonly (int Run, int Code, int Length)[] ExtendedCodes = + [ + (1792, 0b00000001000, 11), (1856, 0b00000001100, 11), (1920, 0b00000001101, 11), + (1984, 0b000000010010, 12), (2048, 0b000000010011, 12), (2112, 0b000000010100, 12), + (2176, 0b000000010101, 12), (2240, 0b000000010110, 12), (2304, 0b000000010111, 12), + (2368, 0b000000011100, 12), (2432, 0b000000011101, 12), (2496, 0b000000011110, 12), + (2560, 0b000000011111, 12), + ]; + + /// All codes of one colour, for tests: terminating, make-up and extended make-up. + internal static IEnumerable<(int Run, int Code, int Length)> Codes(bool black) => + (black ? BlackCodes : WhiteCodes).Concat(ExtendedCodes); + + private const int Pass = -100; + private const int Horizontal = -101; + private const int EndOfLine = -102; + + /// Mode codes: (mode, code bits, length); a vertical mode is the offset of a1 from b1. + private static readonly (int Mode, int Code, int Length)[] ModeCodes = + [ + (0, 0b1, 1), (1, 0b011, 3), (-1, 0b010, 3), (Horizontal, 0b001, 3), (Pass, 0b0001, 4), + (2, 0b000011, 6), (-2, 0b000010, 6), (3, 0b0000011, 7), (-3, 0b0000010, 7), + ]; + + /// The vertical mode codes by offset + 3. + private static readonly (int Mode, int Code, int Length)[] Vertical = [.. ModeCodes.Where(m => m.Mode is >= -3 and <= 3).OrderBy(m => m.Mode)]; + + private const int TableBits = 13; + + /// For each 13-bit window: (run, code length) of the run code it starts with; length 0 for none. + private static readonly (short Run, byte Length)[] WhiteTable = Table(WhiteCodes), BlackTable = Table(BlackCodes); + + /// For each 7-bit window: (mode, code length); length 0 for the start of an EOL or an extension. + private static readonly (int Mode, byte Length)[] ModeTable = Modes(); + + private static (short, byte)[] Table((int Run, int Code, int Length)[] codes) + { + var table = new (short, byte)[1 << TableBits]; + foreach (var (run, code, length) in codes.Concat(ExtendedCodes)) + { + var shift = TableBits - length; + for (var rest = 0; rest < 1 << shift; rest++) + table[(code << shift) | rest] = ((short)run, (byte)length); + } + return table; + } + + private static (int, byte)[] Modes() + { + var table = new (int, byte)[1 << 7]; + foreach (var (mode, code, length) in ModeCodes) + { + var shift = 7 - length; + for (var rest = 0; rest < 1 << shift; rest++) + table[(code << shift) | rest] = (mode, (byte)length); + } + return table; + } + + // ---- encoding ---- + + /// + /// rows of pixels, each row (width + 7) / 8 bytes, a set bit + /// for a black pixel, coded as G4 data ending with EOFB. + /// + public static byte[] Encode(byte[] bits, int width, int height, CancellationToken cancellationToken = default) + { + var stride = (width + 7) / 8; + var writer = new BitWriter(Math.Max(64, bits.Length / 16)); + var reference = new int[width + 4]; + var coding = new int[width + 4]; + Changes(bits, 0, width, reference, white: true); + for (var y = 0; y < height; y++) + { + if ((y & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + Changes(bits, (long)y * stride, width, coding, white: false); + EncodeRow(writer, reference, coding, width); + (reference, coding) = (coding, reference); + } + writer.Write(1, 12); // EOFB: two EOLs + writer.Write(1, 12); + return writer.ToArray(); + } + + /// + /// The changing elements of a row (positions whose colour differs from the pixel before; white before the first), + /// followed by three copies of . With the row is all white. + /// + private static void Changes(byte[] bits, long row, int width, int[] changes, bool white) + { + var n = 0; + if (!white) + { + var black = false; + var x = 0; + while (true) + { + x = NextChange(bits, row, x, width, black); + if (x >= width) + break; + changes[n++] = x; + black = !black; + } + } + changes[n] = changes[n + 1] = changes[n + 2] = width; + } + + /// The first position from on whose pixel isn't ; width when none. + private static int NextChange(byte[] bits, long row, int x, int width, bool black) + { + var skip = black ? 0xFF : 0x00; + while (x < width) + { + var value = bits[row + (x >> 3)]; + if ((x & 7) == 0 && value == skip) + { + x += 8; + continue; + } + // Bits of this byte from x on that differ from the run's colour. + var differ = (value ^ skip) & (0xFF >> (x & 7)); + if (differ != 0) + return Math.Min(width, (x & ~7) + System.Numerics.BitOperations.LeadingZeroCount((uint)differ) - 24); + x = (x & ~7) + 8; + } + return width; + } + + private static void EncodeRow(BitWriter writer, int[] reference, int[] coding, int width) + { + var a0 = -1; + var black = false; + int r = 0, c = 0; // first changes on the reference and coding rows right of a0 + while (a0 < width) + { + while (reference[r] <= a0 && reference[r] < width) + r++; + // b1: a change to the colour opposite a0's (even changes go to black, odd ones to white). + var b = r; + if (((b & 1) == 0) == black && reference[b] < width) + b++; + var b1 = reference[b]; + var b2 = b1 >= width ? width : reference[b + 1]; + while (coding[c] <= a0 && coding[c] < width) + c++; + var a1 = coding[c]; + if (b2 < a1) + { + writer.Write(0b0001, 4); + a0 = b2; + continue; + } + var d = a1 - b1; + if (d is >= -3 and <= 3) + { + var (_, code, length) = Vertical[d + 3]; + writer.Write(code, length); + a0 = a1; + black = !black; + continue; + } + var a2 = coding[c + 1]; + writer.Write(0b001, 3); + WriteRun(writer, a1 - Math.Max(a0, 0), black); + WriteRun(writer, a2 - a1, !black); + a0 = a2; + } + } + + private static void WriteRun(BitWriter writer, int run, bool black) + { + var codes = black ? BlackCodes : WhiteCodes; + while (run >= 2560) + { + writer.Write(ExtendedCodes[^1].Code, ExtendedCodes[^1].Length); + run -= 2560; + } + if (run >= 64) + { + var makeUp = run / 64; // 1 … 39 + var (_, code, length) = makeUp <= 27 ? codes[63 + makeUp] : ExtendedCodes[makeUp - 28]; + writer.Write(code, length); + run %= 64; + } + writer.Write(codes[run].Code, codes[run].Length); + } + + private sealed class BitWriter(int capacity) + { + private byte[] _data = new byte[capacity]; + private int _count; + private ulong _bits; + private int _held; + + public void Write(int code, int length) + { + _bits = (_bits << length) | (uint)code; + _held += length; + while (_held >= 8) + { + _held -= 8; + if (_count == _data.Length) + Array.Resize(ref _data, _data.Length * 2); + _data[_count++] = (byte)(_bits >> _held); + } + } + + public byte[] ToArray() + { + if (_held > 0) + Write(0, 8 - _held); + return _data[.._count]; + } + } + + // ---- decoding ---- + + /// + /// Decodes G4 data into rows … of (rows of (width + 7) / 8 + /// bytes, a set bit for black), at most rows, the row above the first taken as white. + /// Data that ends early (or with EOFB) leaves the remaining rows white; a code that isn't G4 is damage. + /// + public static void Decode(ReadOnlySpan data, int width, int rows, byte[] bits, int firstRow, CancellationToken cancellationToken = default) + { + var stride = (width + 7) / 8; + var reader = new BitReader(data); + var reference = new int[width + 4]; + var coding = new int[width + 4]; + Changes(bits, 0, width, reference, white: true); + for (var y = 0; y < rows; y++) + { + if ((y & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + if (!DecodeRow(ref reader, reference, coding, width)) + return; + Fill(bits, (long)(firstRow + y) * stride, coding, width); + (reference, coding) = (coding, reference); + } + } + + /// Decodes one row's changing elements into ; false at the end of the data. + private static bool DecodeRow(ref BitReader reader, int[] reference, int[] coding, int width) + { + var a0 = -1; + var black = false; + var n = 0; + var r = 0; + while (a0 < width) + { + if (reader.AtEnd) + return false; + var (mode, length) = ModeTable[reader.Peek(7)]; + if (length == 0) + { + // EOFB (two EOLs: eleven zeros and a one) at a row's start, or data that ends here, ends the picture; + // anything else (an EOL inside a row, the uncompressed mode) is damage. + if ((reader.Peek(12) == 1 && n == 0 && a0 < 0) || reader.RestIsZero) + return false; + throw new InvalidDataException("G4 data holds a code Filee doesn't read."); + } + reader.Skip(length); + while (reference[r] <= a0 && reference[r] < width) + r++; + var b = r; + if (((b & 1) == 0) == black && reference[b] < width) + b++; + var b1 = reference[b]; + var b2 = b1 >= width ? width : reference[b + 1]; + switch (mode) + { + case Pass: + a0 = b2; + break; + case Horizontal: + { + var run1 = ReadRun(ref reader, black, width); + var run2 = run1 < 0 ? -1 : ReadRun(ref reader, !black, width); + if (run2 < 0) + return false; // the data ends inside the row + var a1 = Math.Max(a0, 0) + run1; + var a2 = a1 + run2; + // Each change lies right of the one before (only the first run of a row may be empty), so a row + // has fewer changes than pixels. + if (a2 > width || (a0 >= 0 && a1 <= a0) || (a2 <= a1 && a1 < width)) + throw new InvalidDataException("A G4 row's runs don't fit the row."); + // A change at the row's end is only where the row stops. + if (a1 < width) + coding[n++] = a1; + if (a2 < width) + coding[n++] = a2; + a0 = a2; + break; + } + default: + { + var a1 = b1 + mode; + if (a1 < 0 || a1 > width || a1 <= a0) + throw new InvalidDataException("A G4 row's changes are out of order."); + if (a1 < width) + coding[n++] = a1; + a0 = a1; + black = !black; + break; + } + } + } + coding[n] = coding[n + 1] = coding[n + 2] = width; + return true; + } + + /// A run's length (its make-up codes and terminating code); -1 when the data ends first. + private static int ReadRun(ref BitReader reader, bool black, int width) + { + var table = black ? BlackTable : WhiteTable; + var total = 0; + while (true) + { + var (run, length) = table[reader.Peek(TableBits)]; + if (length == 0) + return reader.RestIsZero ? -1 : throw new InvalidDataException("G4 data holds a run code Filee doesn't read."); + reader.Skip(length); + total += run; + if (total > width) + throw new InvalidDataException("A G4 run is longer than the row."); + if (run < 64) + return total; + } + } + + /// Sets the black pixels of a row from its changing elements. + private static void Fill(byte[] bits, long row, int[] changes, int width) + { + for (var i = 0; changes[i] < width; i += 2) + { + int from = changes[i], to = Math.Min(changes[i + 1], width); + for (var x = from; x < to; x++) + bits[row + (x >> 3)] |= (byte)(0x80 >> (x & 7)); + } + } + + private ref struct BitReader(ReadOnlySpan data) + { + private readonly ReadOnlySpan _data = data; + private long _position; + + public readonly bool AtEnd => _position >= 8L * _data.Length; + + /// Whether only zero bits are left (the padding after the last code, or nothing). + public readonly bool RestIsZero + { + get + { + var at = (int)(_position >> 3); + if (at >= _data.Length) + return true; + if ((_data[at] & (0xFF >> (int)(_position & 7))) != 0) + return false; + return !_data[(at + 1)..].ContainsAnyExcept((byte)0); + } + } + + /// The next bits (≤ 24), zeros past the end. + public readonly int Peek(int count) + { + var at = (int)(_position >> 3); + var value = 0u; + for (var i = 0; i < 4; i++) + value = (value << 8) | (at + i < _data.Length ? _data[at + i] : 0u); + return (int)((value << (int)(_position & 7)) >> (32 - count)); + } + + public void Skip(int count) => _position += count; + } +} diff --git a/src/Filee.Engines/Djvu/DjvuAnnotations.cs b/src/Filee.Engines/Djvu/DjvuAnnotations.cs new file mode 100644 index 000000000..f9dd6a5bb --- /dev/null +++ b/src/Filee.Engines/Djvu/DjvuAnnotations.cs @@ -0,0 +1,242 @@ +// A DjVu document's bookmarks and its pages' hyperlinks (DjVu v3 specification, "Document Outline Chunk: NAVM" and +// "Annotation Chunk: ANTa, ANTz"). +// +// NAVM (BZZ-compressed): the number of bookmarks, then each bookmark depth first — its number of children, its title +// and its URL, each a 24-bit length and UTF-8 text. ANTa (ANTz BZZ-compressed) is text of parenthesised expressions; +// a (maparea url comment area …) gives a link: url a string or (url "href" "target"), area (rect x y w h), +// (oval …), (text …) or (poly x0 y0 x1 y1 …), in page pixels from the bottom-left corner. Lines and the other +// expressions (zoom, background colour, metadata, highlighting) aren't links and are skipped. +// +// A URL starting with '#' points into the document (DjvuDocument resolves it); other URLs are kept only when they are +// http, https, ftp or mailto addresses, so a link can't run a script or open a local file (javascript:, file:, …). + +using System.Text; +using Filee.Engines.Documents; + +namespace Filee.Engines.Djvu; + +/// A bookmark of the document outline. +internal sealed record DjvuBookmark(string Title, string Url, List Children); + +/// +/// A link area on a page, in page pixels from the bottom-left corner, and where it points: its URL, and the page it +/// leads to (from 0) when it points into the document. +/// +internal sealed record DjvuLink(int Left, int Bottom, int Width, int Height, string Url, int Page = -1); + +internal static class DjvuAnnotations +{ + /// Nesting of an annotation expression past which it is damage. + private const int MaxDepth = 64; + + /// The bookmarks of a (decompressed) NAVM chunk; a cut-short chunk keeps the bookmarks it holds. + public static List ReadOutline(byte[] data) + { + var roots = new List(); + var reader = new DjvuBytes(data); + try + { + var count = reader.U16(); + // The parents still waiting for children, with how many each still has. + var parents = new List<(List Children, int Left)>(); + for (var i = 0; i < count; i++) + { + var children = reader.U8(); + var title = Text(reader.Bytes(reader.U24())); + var url = Text(reader.Bytes(reader.U24())); + var bookmark = new DjvuBookmark(title, url, []); + if (parents.Count == 0) + { + roots.Add(bookmark); + } + else + { + var (siblings, left) = parents[^1]; + siblings.Add(bookmark); + if (left == 1) + parents.RemoveAt(parents.Count - 1); + else + parents[^1] = (siblings, left - 1); + } + if (children > 0) + parents.Add((bookmark.Children, children)); + } + } + catch (InvalidDataException) + { + // Cut short: the bookmarks read so far. + } + return roots; + } + + private static string Text(byte[] bytes) => Encoding.UTF8.GetString(bytes).Trim('\0').Trim(); + + /// Whether may be a link out of the document: an http, https, ftp or mailto address. + public static bool IsWebAddress(string url) => + Uri.TryCreate(url, UriKind.Absolute, out var uri) && + (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps || uri.Scheme == Uri.UriSchemeFtp || uri.Scheme == Uri.UriSchemeMailto) && + !url.Any(char.IsControl); + + /// + /// The link areas of an annotation text, with their URLs as written (DjvuDocument keeps the ones it can follow). + /// Damaged text (unbalanced, nested too deep) gives no links; more than the limit's expressions is refused. + /// + public static List ReadLinks(string text, DjvuLimits limits) + { + var links = new List(); + try + { + foreach (var expression in Parse(text, limits)) + { + if (Link(expression) is { } link) + links.Add(link); + } + } + catch (InvalidDataException) + { + // Damaged annotations leave the page without links; the page itself is still drawn. + } + return links; + } + + /// A (maparea …) expression as a link, or null when it isn't one Filee draws as a link. + private static DjvuLink? Link(List expression) + { + if (expression is not ["maparea", var target, _, List area, ..]) + return null; + var url = target switch + { + string s => s, + List { Count: >= 2 } list when list[0] is "url" && list[1] is string href => href, + _ => null, + }; + if (string.IsNullOrWhiteSpace(url)) + return null; + var numbers = new List(); + for (var i = 1; i < area.Count; i++) + { + if (area[i] is not string value || !int.TryParse(value, System.Globalization.NumberStyles.AllowLeadingSign, System.Globalization.CultureInfo.InvariantCulture, out var number)) + return null; + numbers.Add(number); + } + switch (area[0]) + { + case "rect" or "oval" or "text" when numbers.Count == 4 && numbers[2] > 0 && numbers[3] > 0: + return new DjvuLink(numbers[0], numbers[1], numbers[2], numbers[3], url.Trim()); + case "poly" when numbers.Count >= 6 && numbers.Count % 2 == 0: + { + int left = int.MaxValue, bottom = int.MaxValue, right = int.MinValue, top = int.MinValue; + for (var i = 0; i < numbers.Count; i += 2) + { + left = Math.Min(left, numbers[i]); + right = Math.Max(right, numbers[i]); + bottom = Math.Min(bottom, numbers[i + 1]); + top = Math.Max(top, numbers[i + 1]); + } + return right > left && top > bottom ? new DjvuLink(left, bottom, right - left, top - bottom, url.Trim()) : null; + } + default: + return null; + } + } + + /// The top-level expressions of an annotation text: lists of atoms (strings) and lists. + internal static List> Parse(string text, DjvuLimits limits) + { + var expressions = new List>(); + var open = new List>(); + var nodes = 0L; + var i = 0; + while (i < text.Length) + { + var c = text[i]; + if (char.IsWhiteSpace(c)) + { + i++; + continue; + } + if (++nodes > limits.MaxAnnotationNodes) + throw DocumentLimits.Exceeded($"A DjVu page's annotations have more than {DocumentLimits.Count(limits.MaxAnnotationNodes)} parts."); + switch (c) + { + case '(': + { + if (open.Count >= MaxDepth) + throw new InvalidDataException("DjVu annotations are nested too deep."); + var list = new List(); + if (open.Count > 0) + open[^1].Add(list); + open.Add(list); + i++; + break; + } + case ')': + if (open.Count == 0) + throw new InvalidDataException("DjVu annotations close more than they open."); + if (open.Count == 1) + expressions.Add(open[0]); + open.RemoveAt(open.Count - 1); + i++; + break; + case '"': + { + var value = QuotedString(text, ref i); + if (open.Count > 0) + open[^1].Add(value); + break; + } + default: + { + var start = i; + while (i < text.Length && !char.IsWhiteSpace(text[i]) && text[i] is not ('(' or ')' or '"')) + i++; + if (open.Count > 0) + open[^1].Add(text[start..i]); + break; + } + } + } + // An expression left open at the end is dropped; the ones before it stand. + return expressions; + } + + /// A string in double quotes from (at the quote), with C escapes. + private static string QuotedString(string text, ref int i) + { + var value = new StringBuilder(); + i++; + while (i < text.Length && text[i] != '"') + { + var c = text[i++]; + if (c != '\\' || i >= text.Length) + { + value.Append(c); + continue; + } + c = text[i++]; + if (c is >= '0' and <= '7') + { + var code = c - '0'; + for (var n = 0; n < 2 && i < text.Length && text[i] is >= '0' and <= '7'; n++) + code = code * 8 + (text[i++] - '0'); + value.Append((char)code); + continue; + } + value.Append(c switch + { + 'n' => '\n', + 't' => '\t', + 'r' => '\r', + 'b' => '\b', + 'f' => '\f', + 'v' => '\v', + 'a' => '\a', + _ => c, + }); + } + if (i >= text.Length) + throw new InvalidDataException("A DjVu annotation string isn't closed."); + i++; + return value.ToString(); + } +} diff --git a/src/Filee.Engines/Djvu/DjvuConverter.cs b/src/Filee.Engines/Djvu/DjvuConverter.cs index fcdf0ebfa..ba2f07574 100644 --- a/src/Filee.Engines/Djvu/DjvuConverter.cs +++ b/src/Filee.Engines/Djvu/DjvuConverter.cs @@ -1,7 +1,8 @@ -// Filee's own DjVu reader (engine id "djvu", #346): DjVu documents (.djvu, .djv; one page or bundled) to PDF — every -// page at its size, the layers kept (DjvuPdfWriter), the hidden text searchable — and pictures of the pages (PNG, JPEG, -// TIFF; the PDF options' page range, the pages' own resolution, then the image options; TIFF holds all pages in one -// file). Layers Filee doesn't decode are named in the file's result (ConversionStep.LeftOut). +// Filee's own DjVu reader (engine id "djvu", #346): DjVu documents (.djvu, .djv; one page, bundled or indirect) to PDF — +// every page at its size, the layers kept (DjvuPdfWriter), the hidden text searchable, the hyperlinks and bookmarks +// kept as PDF links and outline — and pictures of the pages (PNG, JPEG, TIFF; the PDF options' page range, the pages' +// own resolution, then the image options; TIFF holds all pages in one file). Layers Filee doesn't decode are named in +// the file's result (ConversionStep.LeftOut). using Filee.Core.Conversion; using Filee.Engines.Infrastructure; @@ -47,12 +48,21 @@ public Task> ConvertAsync(ConversionStep step, IProgress WritePdf(ConversionStep step, DjvuDocument document, IProgress? progress, CancellationToken cancellationToken) { using var writer = new OcrPdfWriter(); + var pages = new List(); + var links = new List<(int Page, IReadOnlyList Links, double Scale)>(); for (var i = 0; i < document.Count; i++) { cancellationToken.ThrowIfCancellationRequested(); - DjvuPdfWriter.AddPage(writer, document.ReadPage(i, cancellationToken), cancellationToken); + var page = document.ReadPage(i, cancellationToken); + pages.Add(DjvuPdfWriter.AddPage(writer, page, cancellationToken)); + if (page.Links.Count > 0) + links.Add((i, page.Links, DjvuPdfWriter.Scale(page))); progress?.Report(0.95 * (i + 1) / document.Count); } + // Links and bookmarks once every page they may point to is there. + foreach (var (page, pageLinks, scale) in links) + DjvuPdfWriter.AddLinks(writer.Document, pages[page], pageLinks, scale, pages); + DjvuPdfWriter.AddOutline(writer.Document, pages, document.Bookmarks, url => document.PageOf(url), cancellationToken); if (step.Output.Allocate("pdf") is not { } target) return []; writer.Save(target); diff --git a/src/Filee.Engines/Djvu/DjvuDocument.cs b/src/Filee.Engines/Djvu/DjvuDocument.cs index e7faeaab6..e6997caad 100644 --- a/src/Filee.Engines/Djvu/DjvuDocument.cs +++ b/src/Filee.Engines/Djvu/DjvuDocument.cs @@ -1,13 +1,17 @@ // A DjVu document (DjVu v3 specification): an IFF85 file ("AT&T" then a FORM chunk) holding one page (FORM:DJVU) or a -// bundled document (FORM:DJVM): a directory (DIRM) of components at offsets in the file — pages (FORM:DJVU), files -// pages include (FORM:DJVI: shared shape dictionaries, annotations) and thumbnails (FORM:THUM). An indirect document, -// whose directory names separate files, is refused with error.djvu_indirect. +// multi-page document (FORM:DJVM): a directory (DIRM) of components — pages (FORM:DJVU), files pages include +// (FORM:DJVI: shared shape dictionaries, annotations) and thumbnails (FORM:THUM) — and the bookmarks (NAVM). A bundled +// document holds the components at offsets in the file. An indirect one names them as separate files next to the +// index file: each is read only from inside the index file's folder (no absolute paths, no "..", no links on the way), +// and a missing page or included file refuses the document with error.djvu_indirect (thumbnails may be missing). // // A page is read when asked for: INFO (size, resolution, rotation), the mask (Sjbz, with the shape dictionary Djbz of -// the page or of a file it includes with INCL), the background (BG44 chunks, or BGjp), the foreground colours (FG44 or -// FGjp, or the palette FGbz) and the hidden text (TXTz / TXTa). Layers Filee doesn't decode (Smmr masks, JPEG 2000 -// BG2k / FG2k) are counted as pictures left out. Chunks are read one at a time, never more than DjvuLimits.MaxChunkBytes. +// the page or of a file it includes with INCL; or the G4-coded Smmr), the background (BG44 chunks, or BGjp), the +// foreground colours (FG44 or FGjp, or the palette FGbz), the hidden text (TXTz / TXTa) and the hyperlinks of the +// annotations (ANTa / ANTz, also those of the files the page includes). Layers Filee doesn't decode (JPEG 2000 BG2k / +// FG2k) are counted as pictures left out. Chunks are read one at a time, never more than DjvuLimits.MaxChunkBytes. +using System.Globalization; using System.Text; using Filee.Core.Conversion; using Filee.Engines.Documents; @@ -34,6 +38,9 @@ internal sealed class DjvuPage public DjvuPalette? Palette { get; init; } public IReadOnlyList Text { get; init; } = []; + /// Link areas Filee follows: to a page of the document () or a web address. + public IReadOnlyList Links { get; init; } = []; + /// Layers of the page Filee couldn't decode. public int LeftOut { get; init; } } @@ -41,30 +48,44 @@ internal sealed class DjvuPage /// An open DjVu file. internal sealed class DjvuDocument : IDisposable { - /// The error key of an indirect document (pages in separate files). + /// The error key of an indirect document (pages in separate files) whose files Filee can't all read. public const string IndirectKey = "error.djvu_indirect"; - private sealed record Chunk(string Id, long Offset, int Size); + /// A chunk of the opened file ( null) or of a file of an indirect document. + private sealed record Chunk(string Id, long Offset, int Size, string? Path); - private sealed class Component(string id, long offset, long end, string kind) + private sealed class Component(string id, string? path, long offset, long end, string kind) { public string Id { get; } = id; + + /// The component's own file in an indirect document; null for a part of the opened file. + public string? Path { get; } = path; + public long Offset { get; } = offset; public long End { get; } = end; public string Kind { get; } = kind; public List? Chunks { get; set; } } + /// Names Windows keeps for devices; a component file may not be called one. + private static readonly HashSet DeviceNames = new( + ["CON", "PRN", "AUX", "NUL", "CONIN$", "CONOUT$", .. Enumerable.Range(0, 10).SelectMany(i => new[] { $"COM{i}", $"LPT{i}" })], + StringComparer.OrdinalIgnoreCase); + private readonly FileStream _file; + private readonly string _folder; private readonly DjvuLimits _limits; private readonly List _pages = []; private readonly Dictionary _byId = new(StringComparer.Ordinal); + private readonly Dictionary _pageByName = new(StringComparer.Ordinal); private readonly Dictionary _dictionaries = new(StringComparer.Ordinal); + private FileStream? _part; private int _leftOut; private DjvuDocument(FileStream file, DjvuLimits limits) { _file = file; + _folder = System.IO.Path.GetDirectoryName(file.Name) ?? ""; _limits = limits; } @@ -73,10 +94,13 @@ private DjvuDocument(FileStream file, DjvuLimits limits) /// Layers left out of the pages read so far. public int LeftOut => _leftOut; + /// The document outline (NAVM) with its URLs as written; follows them. + public IReadOnlyList Bookmarks { get; private set; } = []; + /// Opens ; refuses a file that isn't a DjVu document Filee reads. public static DjvuDocument Open(string path, DjvuLimits? limits = null, CancellationToken cancellationToken = default) { - var file = File.OpenRead(path); + var file = File.OpenRead(System.IO.Path.GetFullPath(path)); var document = new DjvuDocument(file, limits ?? DjvuLimits.Default); try { @@ -90,22 +114,42 @@ public static DjvuDocument Open(string path, DjvuLimits? limits = null, Cancella } } - public void Dispose() => _file.Dispose(); + public void Dispose() + { + _file.Dispose(); + _part?.Dispose(); + } + + /// + /// The page a link into the document points to: "#" and a page's id, name or title, its number counted from 1, or + /// "+n" / "-n" pages from page ; -1 when it points to no page. + /// + public int PageOf(string url, int from = -1) + { + if (url.Length < 2 || url[0] != '#') + return -1; + var target = url[1..]; + if (target[0] is '+' or '-' && long.TryParse(target.AsSpan(1), NumberStyles.None, CultureInfo.InvariantCulture, out var delta)) + { + var page = from < 0 ? -1 : target[0] == '+' ? from + delta : from - delta; + return page >= 0 && page < Count ? (int)page : -1; + } + if (_pageByName.TryGetValue(target, out var named)) + return named; + return int.TryParse(target, NumberStyles.None, CultureInfo.InvariantCulture, out var number) && number >= 1 && number <= Count ? number - 1 : -1; + } private void ReadStructure(CancellationToken cancellationToken) { - var start = 0L; - var magic = ReadBytes(0, Math.Min(4, (int)_file.Length)); - if (magic.Length == 4 && magic.AsSpan().SequenceEqual("AT&T"u8)) - start = 4; - var (kind, end) = ReadForm(start) ?? throw new InvalidDataException("The file isn't a DjVu document."); + var start = FormStart(null); + var (kind, end) = ReadForm(null, start) ?? throw new InvalidDataException("The file isn't a DjVu document."); switch (kind) { case "DJVU": - _pages.Add(new Component("", start, end, kind)); + _pages.Add(new Component("", null, start, end, kind)); break; case "DJVM": - ReadBundle(start, end, cancellationToken); + ReadDirectory(start, end, cancellationToken); break; case "DJVI": throw new InvalidDataException("The file is a part DjVu pages include, not a document."); @@ -116,39 +160,48 @@ private void ReadStructure(CancellationToken cancellationToken) throw new InvalidDataException("The DjVu document has no pages."); } + /// Where a DjVu file's FORM chunk starts: after "AT&T" when the file starts with it. + private long FormStart(string? path) + { + var magic = ReadBytes(path, 0, (int)Math.Min(4, StreamOf(path).Length)); + return magic.AsSpan().SequenceEqual("AT&T"u8) ? 4 : 0; + } + /// The kind and end of a FORM chunk at , or null when there is none. - private (string Kind, long End)? ReadForm(long offset) + private (string Kind, long End)? ReadForm(string? path, long offset) { - if (offset < 0 || offset + 12 > _file.Length) + var length = StreamOf(path).Length; + if (offset < 0 || offset + 12 > length) return null; - var header = ReadBytes(offset, 12); + var header = ReadBytes(path, offset, 12); if (!header.AsSpan(0, 4).SequenceEqual("FORM"u8)) return null; var size = ((long)header[4] << 24) | ((long)header[5] << 16) | ((long)header[6] << 8) | header[7]; if (size < 4) return null; // A file cut short keeps the pages it still holds. - var end = Math.Min(offset + 8 + size, _file.Length); + var end = Math.Min(offset + 8 + size, length); return (Encoding.ASCII.GetString(header, 8, 4), end); } /// The chunks inside a form (from its data after the kind to its end). - private List ListChunks(long offset, long end, CancellationToken cancellationToken) + private List ListChunks(string? path, long offset, long end, CancellationToken cancellationToken) { var chunks = new List(); var at = offset + 12; var header = new byte[8]; + var file = StreamOf(path); while (at + 8 <= end) { if ((chunks.Count & 0xFF) == 0) cancellationToken.ThrowIfCancellationRequested(); - _file.Position = at; - _file.ReadExactly(header); + file.Position = at; + file.ReadExactly(header); var size = ((long)header[4] << 24) | ((long)header[5] << 16) | ((long)header[6] << 8) | header[7]; var data = at + 8; if (data + size > end) break; // a cut-short last chunk is left out - chunks.Add(new Chunk(Encoding.ASCII.GetString(header, 0, 4), data, (int)Math.Min(size, int.MaxValue))); + chunks.Add(new Chunk(Encoding.ASCII.GetString(header, 0, 4), data, (int)Math.Min(size, int.MaxValue), path)); if (chunks.Count > 1_000_000) throw DocumentLimits.Exceeded("A DjVu form has more than 1,000,000 chunks."); at = data + size; @@ -162,26 +215,74 @@ private byte[] ReadChunk(Chunk chunk) { if (chunk.Size > _limits.MaxChunkBytes) throw DocumentLimits.Exceeded($"A DjVu chunk is larger than {DocumentLimits.Megabytes(_limits.MaxChunkBytes)}."); - return ReadBytes(chunk.Offset, chunk.Size); + return ReadBytes(chunk.Path, chunk.Offset, chunk.Size); } - private byte[] ReadBytes(long offset, int count) + private byte[] ReadBytes(string? path, long offset, int count) { var data = new byte[count]; - _file.Position = offset; - _file.ReadExactly(data); + var file = StreamOf(path); + file.Position = offset; + file.ReadExactly(data); return data; } - private void ReadBundle(long start, long end, CancellationToken cancellationToken) + /// The opened file, or the file of an indirect document's component (one of them kept open at a time). + private FileStream StreamOf(string? path) + { + if (path is null) + return _file; + if (_part is null || _part.Name != path) + { + _part?.Dispose(); + _part = null; + _part = File.OpenRead(path); + } + return _part; + } + + /// + /// The file an indirect document's component names (as written, or URL-decoded): a file below + /// the index file's folder that exists, reached through no link (a symbolic link or junction could lead out of the + /// folder); null when there is none. + /// + private string? PartPath(string id) { - var chunks = ListChunks(start, end, cancellationToken); + var unescaped = Uri.UnescapeDataString(id); + string[] names = unescaped == id ? [id] : [id, unescaped]; + var root = System.IO.Path.TrimEndingDirectorySeparator(_folder) + System.IO.Path.DirectorySeparatorChar; + var comparison = OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + foreach (var name in names) + { + if (name.Length is 0 or > 1024 || name.Contains(':') || name.Any(char.IsControl) || System.IO.Path.IsPathRooted(name)) + continue; + var segments = name.Split('/', '\\'); + if (segments.Any(s => s.Length == 0 || s is "." or ".." || DeviceNames.Contains(s.Split('.')[0].TrimEnd()))) + continue; + var full = System.IO.Path.GetFullPath(System.IO.Path.Combine([_folder, .. segments])); + if (!full.StartsWith(root, comparison)) + continue; + var safe = true; + for (var at = full; safe && at.Length > root.Length; at = System.IO.Path.GetDirectoryName(at) ?? "") + { + FileSystemInfo info = at == full ? new FileInfo(at) : new DirectoryInfo(at); + safe = info.Exists && info.LinkTarget is null && (info.Attributes & FileAttributes.ReparsePoint) == 0; + } + if (safe) + return full; + } + return null; + } + + private void ReadDirectory(long start, long end, CancellationToken cancellationToken) + { + var chunks = ListChunks(null, start, end, cancellationToken); var dirm = chunks.FirstOrDefault(c => c.Id == "DIRM") ?? throw new InvalidDataException("The DjVu document has no directory."); var data = ReadChunk(dirm); var reader = new DjvuBytes(data); var version = reader.U8(); - if ((version & 0x80) == 0) - throw new ConversionRefusedException(IndirectKey, "The DjVu document's pages are separate files (an indirect document)."); + // Bundled: the components are in this file, at the offsets the directory gives. Indirect: each is a file. + var bundled = (version & 0x80) != 0; version &= 0x7F; if (version > 1) throw new InvalidDataException("The DjVu directory has a version Filee doesn't know."); @@ -190,7 +291,7 @@ private void ReadBundle(long start, long end, CancellationToken cancellationToke throw DocumentLimits.Exceeded($"The DjVu document has more than {DocumentLimits.Count(_limits.MaxComponents)} parts."); var offsets = new long[count]; var sizes = new int[count]; - for (var i = 0; i < count; i++) + for (var i = 0; bundled && i < count; i++) { offsets[i] = reader.U32(); if (version == 0) @@ -210,30 +311,71 @@ private void ReadBundle(long start, long end, CancellationToken cancellationToke if (version == 0) flags[i] = ((flags[i] & 1) != 0 ? 1 : 0) | ((flags[i] & 2) != 0 ? 0x80 : 0) | ((flags[i] & 4) != 0 ? 0x40 : 0); } + var names = new (string Id, string? Name, string? Title)[count]; for (var i = 0; i < count; i++) { var id = packed.CString(); - if ((flags[i] & 0x80) != 0) - packed.CString(); - if ((flags[i] & 0x40) != 0) - packed.CString(); - if (ReadForm(offsets[i]) is not { } form) + var name = (flags[i] & 0x80) != 0 ? packed.CString() : null; + var title = (flags[i] & 0x40) != 0 ? packed.CString() : null; + names[i] = (id, name, title); + } + // Links name a page by its id, else its name, else its title (the first page of each). + var byName = new[] { new List<(string, int)>(), [], [] }; + for (var i = 0; i < count; i++) + { + if ((i & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var (id, name, title) = names[i]; + var type = flags[i] & 0x3F; + string? path = null; + var offset = offsets[i]; + if (!bundled) + { + path = PartPath(id); + if (path is null) + { + if (type == 2) + continue; // thumbnails aren't needed + throw new ConversionRefusedException(IndirectKey, $"The file '{id}' of the indirect DjVu document isn't in the folder of its index file."); + } + offset = FormStart(path); + } + if (ReadForm(path, offset) is not { } form) throw new InvalidDataException("A part of the DjVu document isn't where its directory says."); - var component = new Component(id, offsets[i], form.End, form.Kind); + var component = new Component(id, path, offset, form.End, form.Kind); _byId.TryAdd(id, component); - if ((flags[i] & 0x3F) == 1) + if (type == 1) { if (form.Kind != "DJVU") throw new InvalidDataException("A page of the DjVu document isn't a page."); if (_pages.Count >= _limits.MaxPages) throw DocumentLimits.Exceeded($"The DjVu document has more than {DocumentLimits.Count(_limits.MaxPages)} pages."); + byName[0].Add((id, _pages.Count)); + if (name is not null) + byName[1].Add((name, _pages.Count)); + if (title is not null) + byName[2].Add((title, _pages.Count)); _pages.Add(component); } } + foreach (var (key, page) in byName.SelectMany(list => list)) + _pageByName.TryAdd(key, page); + + if (chunks.FirstOrDefault(c => c.Id == "NAVM") is { } navm) + { + try + { + Bookmarks = DjvuAnnotations.ReadOutline(BzzDecoder.Decode(ReadChunk(navm), _limits.MaxUnpackedBytes, cancellationToken)); + } + catch (InvalidDataException) + { + // Damaged bookmarks leave the PDF without an outline; the pages are still read. + } + } } private List ChunksOf(Component component, CancellationToken cancellationToken) => - component.Chunks ??= ListChunks(component.Offset, component.End, cancellationToken); + component.Chunks ??= ListChunks(component.Path, component.Offset, component.End, cancellationToken); /// Page decoded. public DjvuPage ReadPage(int index, CancellationToken cancellationToken = default) @@ -258,6 +400,9 @@ public DjvuPage ReadPage(int index, CancellationToken cancellationToken = defaul case "Sjbz": mask = Jb2Decoder.DecodeImage(ReadChunk(chunk), Dictionary(component, cancellationToken), _limits, cancellationToken); break; + case "Smmr": + mask = ReadSmmr(ReadChunk(chunk), cancellationToken); + break; case "BG44": background.Decode(ReadChunk(chunk), cancellationToken); break; @@ -283,13 +428,20 @@ public DjvuPage ReadPage(int index, CancellationToken cancellationToken = defaul case "TXTa": text = ReadText(ReadChunk(chunk)); break; - case "Smmr" or "BG2k" or "FG2k": + case "BG2k" or "FG2k": leftOut++; break; } } if (mask is not null && (mask.Width != width || mask.Height != height)) throw new InvalidDataException("A DjVu page's mask has another size than the page."); + // The links of the page and of the files it includes (annotations shared by several pages). + var links = new List(); + foreach (var chunk in own.Concat(Included(component, cancellationToken).SelectMany(c => ChunksOf(c, cancellationToken)))) + { + if (chunk.Id is "ANTa" or "ANTz") + links.AddRange(ReadLinks(chunk, index, cancellationToken)); + } _leftOut += leftOut; return new DjvuPage { @@ -302,6 +454,7 @@ public DjvuPage ReadPage(int index, CancellationToken cancellationToken = defaul Foreground = foreground.HasData ? foreground.ToPixmap(cancellationToken) : foregroundJpeg, Palette = palette, Text = text, + Links = links, LeftOut = leftOut, }; } @@ -337,6 +490,92 @@ private List ReadText(byte[] data) } } + /// The links of an annotation chunk Filee follows: to a page of the document, or to a web address. + private List ReadLinks(Chunk chunk, int page, CancellationToken cancellationToken) + { + string text; + try + { + var data = ReadChunk(chunk); + if (chunk.Id == "ANTz") + data = BzzDecoder.Decode(data, _limits.MaxUnpackedBytes, cancellationToken); + text = Encoding.UTF8.GetString(data); + } + catch (InvalidDataException) + { + // Damaged annotations leave the page without links. + return []; + } + var links = new List(); + foreach (var link in DjvuAnnotations.ReadLinks(text, _limits)) + { + if (link.Url.StartsWith('#')) + { + if (PageOf(link.Url, page) is var target and >= 0) + links.Add(link with { Page = target }); + } + else if (DjvuAnnotations.IsWebAddress(link.Url)) + { + links.Add(link); + } + } + return links; + } + + /// + /// An Smmr mask: "MMR", flags (bit 0: black and white swapped; bit 1: in stripes), width and height, then the G4 + /// data — in stripes, the rows per stripe, then each stripe's size and data, each coded on its own. + /// + private Jb2Image ReadSmmr(byte[] data, CancellationToken cancellationToken) + { + var reader = new DjvuBytes(data); + if (reader.U8() != 'M' || reader.U8() != 'M' || reader.U8() != 'R') + throw new InvalidDataException("A DjVu Smmr mask doesn't start with MMR."); + var flags = reader.U8(); + var width = reader.U16(); + var height = reader.U16(); + if (width == 0 || height == 0) + throw new InvalidDataException("A DjVu Smmr mask has no size."); + if ((long)width * height > Math.Min(_limits.MaxPagePixels, _limits.MaxShapePixels)) + throw DocumentLimits.Exceeded($"A DjVu mask has more than {DocumentLimits.Count(Math.Min(_limits.MaxPagePixels, _limits.MaxShapePixels))} pixels."); + var stride = (width + 7) / 8; + var bits = new byte[(long)stride * height]; + if ((flags & 2) == 0) + { + CcittG4.Decode(data.AsSpan(reader.Position), width, height, bits, 0, cancellationToken); + } + else + { + var rows = reader.U16(); + if (rows == 0) + throw new InvalidDataException("A DjVu Smmr mask has empty stripes."); + for (var y = 0; y < height && reader.Remaining > 0; y += rows) + { + var size = reader.U32(); + if (size > reader.Remaining) + throw new InvalidDataException("A DjVu Smmr stripe is cut short."); + CcittG4.Decode(data.AsSpan(reader.Position, (int)size), width, Math.Min(rows, height - y), bits, y, cancellationToken); + reader.Bytes((int)size); + } + } + // One shape covering the page, its row 0 at the bottom as JB2 shapes have it. + var shape = new Jb2Shape(width, height); + var invert = (flags & 1) != 0; + for (var y = 0; y < height; y++) + { + if ((y & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var row = (long)y * stride; + var target = (long)(height - 1 - y) * width; + for (var x = 0; x < width; x++) + { + var black = (bits[row + (x >> 3)] & (0x80 >> (x & 7))) != 0; + shape.Bits[target + x] = (byte)(black != invert ? 1 : 0); + } + } + return new Jb2Image(width, height, [shape], [new Jb2Blit(0, 0, 0)]); + } + private DjvuPalette ReadPalette(byte[] data, CancellationToken cancellationToken) { var reader = new DjvuBytes(data); @@ -376,6 +615,38 @@ private DjvuPalette ReadPalette(byte[] data, CancellationToken cancellationToken return new DjvuPalette(colors, indices); } + /// The files includes with INCL chunks (it names them by id). + private List Includes(Component start, HashSet seen, CancellationToken cancellationToken) + { + var included = new List(); + foreach (var include in ChunksOf(start, cancellationToken).Where(c => c.Id == "INCL")) + { + var id = Encoding.UTF8.GetString(ReadChunk(include)).Trim('\0', ' ', '\n', '\r'); + if (_byId.TryGetValue(id, out var target) && seen.Add(target)) + included.Add(target); + } + return included; + } + + /// The files includes, directly or through other includes, each once, depth first. + private List Included(Component page, CancellationToken cancellationToken) + { + var result = new List(); + var seen = new HashSet { page }; + var pending = new Stack(); + pending.Push(page); + while (pending.Count > 0) + { + var component = pending.Pop(); + if (!ReferenceEquals(component, page)) + result.Add(component); + var included = Includes(component, seen, cancellationToken); + for (var i = included.Count - 1; i >= 0; i--) + pending.Push(included[i]); + } + return result; + } + /// /// The first file with a Djbz chunk among (when ) and the /// files it includes, depth first in the order of its INCL chunks, skipping files already . @@ -388,16 +659,9 @@ private DjvuPalette ReadPalette(byte[] data, CancellationToken cancellationToken while (pending.Count > 0) { var (component, check) = pending.Pop(); - var chunks = ChunksOf(component, cancellationToken); - if (check && chunks.Any(c => c.Id == "Djbz")) + if (check && ChunksOf(component, cancellationToken).Any(c => c.Id == "Djbz")) return component; - var included = new List(); - foreach (var include in chunks.Where(c => c.Id == "INCL")) - { - var id = Encoding.UTF8.GetString(ReadChunk(include)).Trim('\0', ' ', '\n', '\r'); - if (_byId.TryGetValue(id, out var target) && seen.Add(target)) - included.Add(target); - } + var included = Includes(component, seen, cancellationToken); for (var i = included.Count - 1; i >= 0; i--) pending.Push((included[i], true)); } diff --git a/src/Filee.Engines/Djvu/DjvuLimits.cs b/src/Filee.Engines/Djvu/DjvuLimits.cs index 016872aa9..5d2c6770c 100644 --- a/src/Filee.Engines/Djvu/DjvuLimits.cs +++ b/src/Filee.Engines/Djvu/DjvuLimits.cs @@ -1,7 +1,7 @@ // What one DjVu document may make Filee read and build: pages, chunk sizes, decompressed (BZZ) bytes, page pixels, -// JB2 shapes, blits and their pixels, IW44 slices and coefficient blocks, text zones. Every limit is far beyond what -// scanners and DjVu encoders write (a 600 dpi A3 scan is 70 million pixels); a document past one is refused with -// error.document_too_complex, naming the limit. +// JB2 shapes, blits and their pixels, IW44 slices and coefficient blocks, text zones, annotation parts. Every limit is +// far beyond what scanners and DjVu encoders write (a 600 dpi A3 scan is 70 million pixels); a document past one is +// refused with error.document_too_complex, naming the limit. using Filee.Engines.Documents; @@ -53,4 +53,7 @@ internal sealed record DjvuLimits /// Colours of a foreground palette. public int MaxPaletteColors { get; init; } = 65_535; + + /// Parts (brackets, words, strings) of one page's annotations (its hyperlinks and display settings). + public int MaxAnnotationNodes { get; init; } = 1_000_000; } diff --git a/src/Filee.Engines/Djvu/DjvuPdfWriter.cs b/src/Filee.Engines/Djvu/DjvuPdfWriter.cs index 75bedafc9..e2b38ba31 100644 --- a/src/Filee.Engines/Djvu/DjvuPdfWriter.cs +++ b/src/Filee.Engines/Djvu/DjvuPdfWriter.cs @@ -1,9 +1,14 @@ // DjVu pages as PDF pages, each at its size at the page's resolution (INFO), its turn as the page's /Rotate. The // layers stay layers, so a scanned book stays small: the background as a JPEG at its own (reduced) resolution, the -// mask as 1-bit image masks (Flate) painted in the foreground colour — one per palette colour, or the FG44 picture -// (a JPEG) shown through the mask (explicit masking). A page with more than MaxStencils palette colours is drawn -// whole as one JPEG instead. The hidden text becomes an invisible text layer (OcrPdfWriter's glyphless font), so the -// PDF can be searched and its text copied. +// mask as 1-bit image masks painted in the foreground colour — one per palette colour, or the FG44 picture (a JPEG) +// shown through the mask (explicit masking). Each mask is coded the smallest way: JBIG2 (the mask's shapes as +// symbols, Jbig2Encoder — about the DjVu file's own size), CCITT G4 (CcittG4), or Flate for finely dithered pictures. +// A page with more than MaxStencils palette colours is drawn whole as one JPEG instead. The hidden text becomes an +// invisible text layer (OcrPdfWriter's glyphless font), so the PDF can be searched and its text copied. +// +// Once all pages are in, their hyperlinks become link annotations (to a page, or a URI action for a web address) and +// the bookmarks the document outline: links into the document go to their page, web addresses open, anything else +// (a relative file, javascript:, file:) leaves the bookmark without an action and is no link. using System.Globalization; using System.IO.Compression; @@ -15,6 +20,16 @@ namespace Filee.Engines.Djvu; +/// The ways a mask may be coded in the PDF (all but for tests). +[Flags] +internal enum MaskCodings +{ + Jbig2 = 1, + G4 = 2, + Flate = 4, + All = Jbig2 | G4 | Flate, +} + internal static class DjvuPdfWriter { /// Palette colours drawn as separate masks at most; a page with more is drawn as one picture. @@ -25,12 +40,21 @@ internal static class DjvuPdfWriter private const int JpegQuality = 85; - /// Adds to 's document. - public static void AddPage(OcrPdfWriter writer, DjvuPage page, CancellationToken cancellationToken = default) + /// Outline levels written at most; deeper bookmarks go in at the deepest level. + internal const int MaxOutlineDepth = 32; + + /// Points per page pixel. + public static double Scale(DjvuPage page) => Math.Min(72.0 / page.Dpi, MaxSide / Math.Max(page.Width, page.Height)); + + /// Adds to 's document; its links come with . + public static PdfPage AddPage(OcrPdfWriter writer, DjvuPage page, CancellationToken cancellationToken = default) => + AddPage(writer, page, MaskCodings.All, cancellationToken); + + /// Adds , its masks coded only in the ways allows. + internal static PdfPage AddPage(OcrPdfWriter writer, DjvuPage page, MaskCodings codings, CancellationToken cancellationToken) { var document = writer.Document; - var scale = 72.0 / page.Dpi; - scale = Math.Min(scale, MaxSide / Math.Max(page.Width, page.Height)); + var scale = Scale(page); var width = page.Width * scale; var height = page.Height * scale; var pdfPage = document.AddPage(); @@ -68,7 +92,7 @@ void Draw(PdfDictionary image, string? fill = null) if (page.Foreground is { } foreground) { var image = Jpeg(document, foreground); - image.Elements["/Mask"] = Stencil(document, page, null, cancellationToken).Reference; + image.Elements["/Mask"] = Stencil(document, page, null, codings, cancellationToken).Reference; Draw(image); } else if (used is not null) @@ -76,12 +100,12 @@ void Draw(PdfDictionary image, string? fill = null) foreach (var color in used) { var c = palette!.Colors[color]; - Draw(Stencil(document, page, color, cancellationToken), $"{N(c.Red / 255.0)} {N(c.Green / 255.0)} {N(c.Blue / 255.0)} rg"); + Draw(Stencil(document, page, color, codings, cancellationToken), $"{N(c.Red / 255.0)} {N(c.Green / 255.0)} {N(c.Blue / 255.0)} rg"); } } else { - Draw(Stencil(document, page, null, cancellationToken), "0 g"); + Draw(Stencil(document, page, null, codings, cancellationToken), "0 g"); } } } @@ -98,6 +122,140 @@ void Draw(PdfDictionary image, string? fill = null) pdfPage.Contents.AppendContent().CreateStream(Encoding.ASCII.GetBytes(content.ToString())); } writer.AddText(pdfPage, Lines(page), width, height); + return pdfPage; + } + + /// + /// Adds a page's (page pixels × , as gave for + /// it) to , a link into the document going to its page among . + /// + public static void AddLinks(PdfDocument document, PdfPage pdfPage, IReadOnlyList links, double scale, IReadOnlyList pages) + { + double width = pdfPage.Width.Point, height = pdfPage.Height.Point; + var annotations = new PdfArray(document); + foreach (var link in links) + { + var left = Math.Clamp(link.Left * scale, 0, width); + var bottom = Math.Clamp(link.Bottom * scale, 0, height); + var right = Math.Clamp((link.Left + (double)link.Width) * scale, 0, width); + var top = Math.Clamp((link.Bottom + (double)link.Height) * scale, 0, height); + if (right - left < 0.5 || top - bottom < 0.5) + continue; + var annotation = new PdfDictionary(document); + annotation.Elements.SetName("/Type", "/Annot"); + annotation.Elements.SetName("/Subtype", "/Link"); + annotation.Elements["/Rect"] = Numbers(document, left, bottom, right, top); + annotation.Elements["/Border"] = Numbers(document, 0, 0, 0); + annotation.Elements.SetInteger("/F", 4); // printed (PDF/A wants it on every annotation) + if (!Target(document, annotation, link.Url, link.Page, pages)) + continue; + document.Internals.AddObject(annotation); + annotations.Elements.Add(annotation.Reference!); + } + if (annotations.Elements.Count > 0) + pdfPage.Elements["/Annots"] = annotations; + } + + /// + /// The document outline from : a bookmark into the document goes to the page + /// gives, one with a web address opens it, others are only titles. The first level is + /// shown open. + /// + public static void AddOutline(PdfDocument document, IReadOnlyList pages, IReadOnlyList bookmarks, Func pageOf, CancellationToken cancellationToken = default) + { + if (bookmarks.Count == 0 || pages.Count == 0) + return; + var root = new OutlineItem(new PdfDictionary(document), -1); + root.Dictionary.Elements.SetName("/Type", "/Outlines"); + var pending = new Stack<(DjvuBookmark Bookmark, OutlineItem Parent)>(); + for (var i = bookmarks.Count - 1; i >= 0; i--) + pending.Push((bookmarks[i], root)); + var items = new List { root }; + while (pending.Count > 0) + { + if ((items.Count & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var (bookmark, parent) = pending.Pop(); + var item = new OutlineItem(new PdfDictionary(document), parent.Level + 1); + var title = bookmark.Title.Length > 0 ? bookmark.Title : bookmark.Url; + item.Dictionary.Elements["/Title"] = new PdfString(title.Length > 1000 ? title[..1000] : title, PdfStringEncoding.Unicode); + var page = bookmark.Url.StartsWith('#') ? pageOf(bookmark.Url) : -1; + Target(document, item.Dictionary, bookmark.Url, page, pages); + parent.Children.Add(item); + items.Add(item); + // Past the deepest level, children follow their parent as its siblings. + var holder = item.Level < MaxOutlineDepth - 1 ? item : parent; + for (var i = bookmark.Children.Count - 1; i >= 0; i--) + pending.Push((bookmark.Children[i], holder)); + } + foreach (var item in items) + document.Internals.AddObject(item.Dictionary); + foreach (var item in items) + { + var children = item.Children; + if (children.Count == 0) + continue; + for (var i = 0; i < children.Count; i++) + { + var child = children[i].Dictionary; + child.Elements["/Parent"] = item.Dictionary.Reference; + if (i > 0) + child.Elements["/Prev"] = children[i - 1].Dictionary.Reference; + if (i + 1 < children.Count) + child.Elements["/Next"] = children[i + 1].Dictionary.Reference; + } + item.Dictionary.Elements["/First"] = children[0].Dictionary.Reference; + item.Dictionary.Elements["/Last"] = children[^1].Dictionary.Reference; + // The first level is open, its children shown; deeper items are closed (a negative count). + item.Dictionary.Elements.SetInteger("/Count", item.Level switch + { + -1 => children.Count + children.Sum(c => c.Children.Count), + 0 => children.Count, + _ => -children.Count, + }); + } + document.Internals.Catalog.Elements["/Outlines"] = root.Dictionary.Reference; + document.PageMode = PdfPageMode.UseOutlines; + } + + private sealed class OutlineItem(PdfDictionary dictionary, int level) + { + public PdfDictionary Dictionary { get; } = dictionary; + public int Level { get; } = level; + public List Children { get; } = []; + } + + /// + /// Points a link or bookmark at page (/Dest) or at a web address (a URI action); false when + /// it goes nowhere Filee links to. + /// + private static bool Target(PdfDocument document, PdfDictionary item, string url, int page, IReadOnlyList pages) + { + if (page >= 0 && page < pages.Count) + { + var destination = new PdfArray(document); + destination.Elements.Add(pages[page].Reference!); + destination.Elements.Add(new PdfName("/Fit")); + item.Elements["/Dest"] = destination; + return true; + } + if (!url.StartsWith('#') && DjvuAnnotations.IsWebAddress(url)) + { + var action = new PdfDictionary(document); + action.Elements.SetName("/S", "/URI"); + action.Elements["/URI"] = new PdfString(new Uri(url).AbsoluteUri); + item.Elements["/A"] = action; + return true; + } + return false; + } + + private static PdfArray Numbers(PdfDocument document, params double[] values) + { + var array = new PdfArray(document); + foreach (var value in values) + array.Elements.Add(new PdfReal(Math.Round(value, 3))); + return array; } /// The palette colours the mask's shapes use, in the order first used. @@ -153,21 +311,50 @@ private static bool LaidOut(IReadOnlyList words) return true; } - /// An image mask of the page's shapes (one palette colour's, or all), a set sample painted. - private static PdfDictionary Stencil(PdfDocument document, DjvuPage page, int? color, CancellationToken cancellationToken) + /// + /// An image mask of the page's shapes (one palette colour's, or all), black painted, in the smallest of: JBIG2 + /// (the shapes as symbols, see Jbig2Encoder), CCITT G4, and — when neither packs the mask well (fine dithering) — + /// Flate. + /// + private static PdfDictionary Stencil(PdfDocument document, DjvuPage page, int? color, MaskCodings codings, CancellationToken cancellationToken) { + var mask = page.Mask!; + var blits = color is null ? mask.Blits : [.. mask.Blits.Where((_, b) => DjvuRenderer.ColorOf(page.Palette?.BlitColors, b, mask.Blits.Count) == color)]; + var jbig2 = codings.HasFlag(MaskCodings.Jbig2) ? Jbig2Encoder.Encode(page.Width, page.Height, mask.Shapes, blits, cancellationToken) : null; var bits = DjvuRenderer.MaskBits(page, color, cancellationToken); - var image = Stream(document, Deflate(bits), "/FlateDecode"); + var g4 = codings.HasFlag(MaskCodings.G4) ? CcittG4.Encode(bits, page.Width, page.Height, cancellationToken) : null; + var best = Math.Min(jbig2?.Length ?? int.MaxValue, g4?.Length ?? int.MaxValue); + var flate = codings.HasFlag(MaskCodings.Flate) && best > bits.Length / 16 ? Deflate(bits) : null; + PdfDictionary image; + if (flate is not null && flate.Length < best) + { + image = Stream(document, flate, "/FlateDecode"); + var decode = new PdfArray(document); + decode.Elements.Add(new PdfInteger(1)); + decode.Elements.Add(new PdfInteger(0)); + image.Elements["/Decode"] = decode; + } + else if (jbig2 is not null && jbig2.Length == best) + { + // JBIG2's black samples come out as 0, which an image mask paints by default. + image = Stream(document, jbig2, "/JBIG2Decode"); + } + else + { + // So do G4's (/BlackIs1 false). + image = Stream(document, g4!, "/CCITTFaxDecode"); + var parameters = new PdfDictionary(document); + parameters.Elements.SetInteger("/K", -1); + parameters.Elements.SetInteger("/Columns", page.Width); + parameters.Elements.SetInteger("/Rows", page.Height); + image.Elements["/DecodeParms"] = parameters; + } image.Elements.SetName("/Type", "/XObject"); image.Elements.SetName("/Subtype", "/Image"); image.Elements.SetInteger("/Width", page.Width); image.Elements.SetInteger("/Height", page.Height); image.Elements.SetBoolean("/ImageMask", true); image.Elements.SetInteger("/BitsPerComponent", 1); - var decode = new PdfArray(document); - decode.Elements.Add(new PdfInteger(1)); - decode.Elements.Add(new PdfInteger(0)); - image.Elements["/Decode"] = decode; return image; } diff --git a/src/Filee.Engines/Djvu/Jbig2Encoder.cs b/src/Filee.Engines/Djvu/Jbig2Encoder.cs new file mode 100644 index 000000000..05804c4c8 --- /dev/null +++ b/src/Filee.Engines/Djvu/Jbig2Encoder.cs @@ -0,0 +1,387 @@ +// JBIG2 (ITU-T T.88) encoding of a page mask for PDF's /JBIG2Decode filter, lossless, in the embedded stream +// organisation PDF uses (segments without a file header): a page information segment, a symbol dictionary holding +// the mask's shapes and an immediate text region placing them. A DjVu mask (JB2) already is a set of shapes and their +// places, so they go across one for one; that keeps the PDF's masks close to the DjVu file's size. +// +// Everything is coded with the MQ arithmetic coder (T.88 Annex E): the symbols' heights and widths with the integer +// coders IADH / IADW and their bitmaps with generic region coding (template 0, its nominal adaptive pixels), the export +// flags with IAEX; in the text region, strips of four rows (IADT, IAIT), each symbol's place along its strip (IAFS, +// IADS) and its id (IAID). No Huffman tables, no refinement. + +namespace Filee.Engines.Djvu; + +internal static class Jbig2Encoder +{ + /// Rows per text region strip (log2). + private const int LogStrips = 2; + + /// + /// The mask drawn by of (DjVu shapes, row 0 at the bottom, + /// placed by their bottom-left pixel from the page's bottom-left corner) on a page of + /// × , as JBIG2 segments. + /// + public static byte[] Encode(int width, int height, IReadOnlyList shapes, IReadOnlyList placements, CancellationToken cancellationToken = default) + { + // The shapes placed that have black pixels, as symbols ordered by height then width (small deltas between + // them); a symbol's id is its place in that order. + var ids = new Dictionary(); + var symbols = new List(); + foreach (var blit in placements) + { + var shape = shapes[blit.Shape]; + if (shape.Width > 0 && shape.Height > 0 && ids.TryAdd(blit.Shape, 0) && shape.Bits.AsSpan().ContainsAnyExcept((byte)0)) + symbols.Add(blit.Shape); + } + symbols.Sort((a, b) => shapes[a].Height != shapes[b].Height ? shapes[a].Height.CompareTo(shapes[b].Height) : shapes[a].Width.CompareTo(shapes[b].Width)); + ids.Clear(); + for (var i = 0; i < symbols.Count; i++) + ids[symbols[i]] = i; + + var output = new List(); + // Page information: size, no resolution, lossless, white by default, shapes combined with OR. + var page = new List(); + page.AddRange(U32(width)); + page.AddRange(U32(height)); + page.AddRange(U32(0)); + page.AddRange(U32(0)); + page.Add(0x01); + page.AddRange([0, 0]); + Segment(output, 0, 48, [], page); + if (symbols.Count == 0) + return [.. output]; + + Segment(output, 1, 0, [], SymbolDictionary(shapes, symbols, cancellationToken)); + Segment(output, 2, 6, [1], TextRegion(width, height, shapes, placements, ids, symbols.Count, cancellationToken)); + return [.. output]; + } + + private static void Segment(List output, int number, int type, int[] referred, List data) + { + output.AddRange(U32(number)); + output.Add((byte)type); + output.Add((byte)(referred.Length << 5)); + foreach (var segment in referred) + output.Add((byte)segment); + output.Add(1); // page 1 + output.AddRange(U32(data.Count)); + output.AddRange(data); + } + + private static byte[] U32(int value) => [(byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value]; + + /// The nominal adaptive pixels of generic template 0: (3, −1), (−3, −1), (2, −2), (−2, −2). + private static readonly byte[] TemplateZero = [3, unchecked((byte)-1), unchecked((byte)-3), unchecked((byte)-1), 2, unchecked((byte)-2), unchecked((byte)-2), unchecked((byte)-2)]; + + private static List SymbolDictionary(IReadOnlyList shapes, List symbols, CancellationToken cancellationToken) + { + var data = new List { 0, 0 }; // arithmetic coding, no aggregation, template 0 + data.AddRange(TemplateZero); + data.AddRange(U32(symbols.Count)); // exported + data.AddRange(U32(symbols.Count)); // new + var coder = new MqEncoder(); + var deltaHeight = new IntegerCoder(); + var deltaWidth = new IntegerCoder(); + var export = new IntegerCoder(); + var generic = new byte[1 << 16]; + var classHeight = 0; + for (var i = 0; i < symbols.Count;) + { + var height = shapes[symbols[i]].Height; + deltaHeight.Encode(coder, height - classHeight); + classHeight = height; + var symbolWidth = 0; + for (; i < symbols.Count && shapes[symbols[i]].Height == height; i++) + { + if ((i & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var shape = shapes[symbols[i]]; + deltaWidth.Encode(coder, shape.Width - symbolWidth); + symbolWidth = shape.Width; + Generic(coder, generic, shape); + } + deltaWidth.EncodeOutOfBand(coder); + } + // Export flags: a run of none not exported, then all exported. + export.Encode(coder, 0); + export.Encode(coder, symbols.Count); + data.AddRange(coder.Finish()); + return data; + } + + /// Codes a shape's bitmap (top row first) with generic region template 0 in the shared contexts. + private static void Generic(MqEncoder coder, byte[] contexts, Jb2Shape shape) + { + int w = shape.Width, h = shape.Height; + // The shape's rows top first, with two blank rows above and margins of four columns. + var stride = w + 8; + var pixels = new byte[(h + 2) * stride]; + for (var y = 0; y < h; y++) + Array.Copy(shape.Bits, (long)(h - 1 - y) * w, pixels, (y + 2) * stride + 4, w); + for (var y = 0; y < h; y++) + { + var row = (y + 2) * stride + 4; + var up = row - stride; + var up2 = up - stride; + for (var x = 0; x < w; x++) + { + var c = (pixels[row + x - 1] << 0) | (pixels[row + x - 2] << 1) | (pixels[row + x - 3] << 2) | (pixels[row + x - 4] << 3) + | (pixels[up + x + 2] << 4) | (pixels[up + x + 1] << 5) | (pixels[up + x] << 6) | (pixels[up + x - 1] << 7) | (pixels[up + x - 2] << 8) + | (pixels[up2 + x + 1] << 9) | (pixels[up2 + x] << 10) | (pixels[up2 + x - 1] << 11) + | (pixels[up + x + 3] << 12) | (pixels[up + x - 3] << 13) | (pixels[up2 + x + 2] << 14) | (pixels[up2 + x - 2] << 15); + coder.Encode(contexts, c, pixels[row + x]); + } + } + } + + private static List TextRegion(int width, int height, IReadOnlyList shapes, IReadOnlyList placements, Dictionary ids, int symbolCount, CancellationToken cancellationToken) + { + // Each placement by its bottom-left pixel in JBIG2's coordinates (rows from the top), in strips of rows. + var instances = new List<(int Strip, int Row, int S, int Id, int Width)>(); + foreach (var blit in placements) + { + if (!ids.TryGetValue(blit.Shape, out var id)) + continue; + var t = height - 1 - blit.Bottom; + instances.Add((t >> LogStrips, t & ((1 << LogStrips) - 1), blit.Left, id, shapes[blit.Shape].Width)); + } + instances.Sort((a, b) => a.Strip != b.Strip ? a.Strip.CompareTo(b.Strip) : a.S.CompareTo(b.S)); + + var data = new List(); + data.AddRange(U32(width)); + data.AddRange(U32(height)); + data.AddRange(U32(0)); + data.AddRange(U32(0)); + data.Add(0); // OR onto the page + // Arithmetic coding, no refinement, strips of 2^LogStrips rows, reference corner bottom left, OR, white. + var flags = LogStrips << 2; + data.AddRange([(byte)(flags >> 8), (byte)flags]); + data.AddRange(U32(instances.Count)); + + var coder = new MqEncoder(); + IntegerCoder deltaT = new(), firstS = new(), deltaS = new(), rowInStrip = new(); + var idCoder = new IdCoder(symbolCount); + deltaT.Encode(coder, 0); // STRIPT starts at 0 + int stripT = 0, firsts = 0; + for (var i = 0; i < instances.Count;) + { + if ((i & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var strip = instances[i].Strip; + deltaT.Encode(coder, strip - stripT); + stripT = strip; + var curs = 0; + for (var first = true; i < instances.Count && instances[i].Strip == strip; i++, first = false) + { + var (_, row, s, id, symbolWidth) = instances[i]; + if (first) + { + firstS.Encode(coder, s - firsts); + firsts = s; + } + else + { + deltaS.Encode(coder, s - curs); + } + rowInStrip.Encode(coder, row); + idCoder.Encode(coder, id); + curs = s + symbolWidth - 1; + } + deltaS.EncodeOutOfBand(coder); + } + data.AddRange(coder.Finish()); + return data; + } + + /// T.88's integer arithmetic coder (Annex A.2) in reverse: a sign, a range prefix, then the offset bits. + private sealed class IntegerCoder + { + private readonly byte[] _contexts = new byte[512]; + + public void Encode(MqEncoder coder, int value) + { + var prev = 1; + Bit(coder, value < 0 ? 1 : 0, ref prev); + var v = Math.Abs((long)value); + (int Prefix, int Bits, long Offset) range = v switch + { + <= 3 => (0, 2, 0), + <= 19 => (1, 4, 4), + <= 83 => (2, 6, 20), + <= 339 => (3, 8, 84), + <= 4435 => (4, 12, 340), + _ => (5, 32, 4436), + }; + for (var i = 0; i < range.Prefix; i++) + Bit(coder, 1, ref prev); + if (range.Prefix < 5) + Bit(coder, 0, ref prev); + var offset = v - range.Offset; + for (var i = range.Bits - 1; i >= 0; i--) + Bit(coder, (int)((offset >> i) & 1), ref prev); + } + + /// OOB: a negative zero. + public void EncodeOutOfBand(MqEncoder coder) + { + var prev = 1; + Bit(coder, 1, ref prev); + Bit(coder, 0, ref prev); + Bit(coder, 0, ref prev); + Bit(coder, 0, ref prev); + } + + private void Bit(MqEncoder coder, int bit, ref int prev) + { + coder.Encode(_contexts, prev, bit); + prev = prev < 256 ? (prev << 1) | bit : (((prev << 1) | bit) & 511) | 256; + } + } + + /// IAID: a symbol id in SBSYMCODELEN bits, each in the context of the bits before it. + private sealed class IdCoder(int symbols) + { + /// SBSYMCODELEN: the bits that tell symbols ids apart (none for one). + private readonly int _length = 32 - System.Numerics.BitOperations.LeadingZeroCount((uint)Math.Max(symbols - 1, 0)); + private byte[]? _contexts; + + public void Encode(MqEncoder coder, int id) + { + _contexts ??= new byte[1 << _length]; + var prev = 1; + for (var i = _length - 1; i >= 0; i--) + { + var bit = (id >> i) & 1; + coder.Encode(_contexts, prev, bit); + prev = (prev << 1) | bit; + } + } + } + + /// The MQ arithmetic encoder (T.88 Annex E.2); a context is a byte: its state index × 2 + its MPS. + private sealed class MqEncoder + { + private static readonly ushort[] Qe = + [ + 0x5601, 0x3401, 0x1801, 0x0AC1, 0x0521, 0x0221, 0x5601, 0x5401, 0x4801, 0x3801, 0x3001, 0x2401, 0x1C01, 0x1601, + 0x5601, 0x5401, 0x5101, 0x4801, 0x3801, 0x3401, 0x3001, 0x2801, 0x2401, 0x2201, 0x1C01, 0x1801, 0x1601, 0x1401, + 0x1201, 0x1101, 0x0AC1, 0x09C1, 0x08A1, 0x0521, 0x0441, 0x02A1, 0x0221, 0x0141, 0x0111, 0x0085, 0x0049, 0x0025, + 0x0015, 0x0009, 0x0005, 0x0001, 0x5601, + ]; + + private static readonly byte[] NextMps = + [ + 1, 2, 3, 4, 5, 38, 7, 8, 9, 10, 11, 12, 13, 29, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 45, 46, + ]; + + private static readonly byte[] NextLps = + [ + 1, 6, 9, 12, 29, 33, 6, 14, 14, 14, 17, 18, 20, 21, 14, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 27, + 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46, + ]; + + private static readonly bool[] Switch = [true, false, false, false, false, false, true, false, false, false, false, false, false, false, true]; + + private byte[] _out = new byte[4096]; + private int _bp; // _out[0] stands for the byte before the data (BPST − 1) + private uint _c; + private uint _a = 0x8000; + private int _ct = 12; + + public void Encode(byte[] contexts, int cx, int bit) + { + var state = contexts[cx] >> 1; + var mps = contexts[cx] & 1; + uint qe = Qe[state]; + _a -= qe; + if (bit == mps) + { + if ((_a & 0x8000) != 0) + { + _c += qe; + return; + } + if (_a < qe) + _a = qe; + else + _c += qe; + contexts[cx] = (byte)((NextMps[state] << 1) | mps); + } + else + { + if (_a < qe) + _c += qe; + else + _a = qe; + if (state < Switch.Length && Switch[state]) + mps = 1 - mps; + contexts[cx] = (byte)((NextLps[state] << 1) | mps); + } + do + { + _a <<= 1; + _c <<= 1; + if (--_ct == 0) + ByteOut(); + } + while ((_a & 0x8000) == 0); + } + + private void ByteOut() + { + if (_out[_bp] == 0xFF) + { + Put((byte)(_c >> 20)); + _c &= 0xFFFFF; + _ct = 7; + } + else if (_c < 0x8000000) + { + Put((byte)(_c >> 19)); + _c &= 0x7FFFF; + _ct = 8; + } + else + { + _out[_bp]++; + if (_out[_bp] == 0xFF) + { + _c &= 0x7FFFFFF; + Put((byte)(_c >> 20)); + _c &= 0xFFFFF; + _ct = 7; + } + else + { + Put((byte)(_c >> 19)); + _c &= 0x7FFFF; + _ct = 8; + } + } + } + + private void Put(byte value) + { + if (++_bp == _out.Length) + Array.Resize(ref _out, _out.Length * 2); + _out[_bp] = value; + } + + /// Flushes the coder and ends the data with the marker 0xFF 0xAC. + public byte[] Finish() + { + var temp = _c + _a; + _c |= 0xFFFF; + if (_c >= temp) + _c -= 0x8000; + _c <<= _ct; + ByteOut(); + _c <<= _ct; + ByteOut(); + if (_out[_bp] != 0xFF) + Put(0xFF); + Put(0xAC); + return _out[1..(_bp + 1)]; + } + } +} diff --git a/tests/Filee.Engines.Tests/DjvuBuilder.cs b/tests/Filee.Engines.Tests/DjvuBuilder.cs index 14c99a8ed..9c260ef8d 100644 --- a/tests/Filee.Engines.Tests/DjvuBuilder.cs +++ b/tests/Filee.Engines.Tests/DjvuBuilder.cs @@ -2,7 +2,9 @@ // reader decodes, written from the DjVu v3 specification as the mirror image of the decoders — the ZP-coder, BZZ (a // plain suffix sort for the block transform), JB2 (new shapes, copies, refinements, non-mark data, dictionaries, // comments, resets), IW44 (the forward lifting transform and a slice coder that decides each bit from the true -// coefficients), hidden text zones, palettes, INFO and the IFF container, one page or bundled with a directory. +// coefficients), hidden text zones, palettes, bookmarks (NAVM), annotations (ANTa / ANTz), G4 masks (Smmr, with the +// reader's own CCITT G4 encoder, which PDFtoImage checks independently), INFO and the IFF container, one page, bundled +// with a directory or indirect (an index file naming the component files). using System.Text; using Filee.Engines.Djvu; @@ -1098,6 +1100,9 @@ private static void Horizontal(short[] d, int w, int h, int rowsize, int scale) } } +/// A bookmark to write: its title, URL and children. +internal sealed record Bookmark(string Title, string Url, params Bookmark[] Children); + /// A text zone to write, with its box in page pixels (row 0 at the bottom) and the text it covers. internal sealed record TextZone(int Type, int Left, int Bottom, int Width, int Height, int Start, int Length, params TextZone[] Children); @@ -1194,10 +1199,95 @@ private static void WriteZone(List data, TextZone zone, TextZone? parent, } } + /// NAVM: the bookmarks, depth first, BZZ-compressed. + public static byte[] Outline(params Bookmark[] bookmarks) + { + var data = new List(); + var count = 0; + void Write(Bookmark bookmark) + { + count++; + data.Add((byte)bookmark.Children.Length); + foreach (var text in new[] { bookmark.Title, bookmark.Url }) + { + var bytes = Encoding.UTF8.GetBytes(text); + data.AddRange(BigEndian(bytes.Length, 3)); + data.AddRange(bytes); + } + foreach (var child in bookmark.Children) + Write(child); + } + foreach (var bookmark in bookmarks) + Write(bookmark); + return Chunk("NAVM", BzzEncoder.Encode([.. BigEndian(count, 2), .. data])); + } + + /// Annotations: ANTz (BZZ-compressed) or ANTa (plain). + public static byte[] AnnotationChunk(string text, bool compressed = true) => + compressed ? Chunk("ANTz", BzzEncoder.Encode(Encoding.UTF8.GetBytes(text))) : Chunk("ANTa", Encoding.UTF8.GetBytes(text)); + + /// + /// Smmr: a G4 mask of rows of (width + 7) / 8 bytes, a set bit black; in stripes of + /// rows when given, with black and white swapped in the data when . + /// + public static byte[] Smmr(byte[] bits, int width, int height, int rowsPerStripe = 0, bool inverted = false) + { + var stride = (width + 7) / 8; + var rows = bits; + if (inverted) + { + rows = [.. bits.Select(b => (byte)~b)]; + for (var y = 0; y < height && width % 8 != 0; y++) + rows[y * stride + stride - 1] &= (byte)(0xFF << (8 - width % 8)); + } + var data = new List { (byte)'M', (byte)'M', (byte)'R', (byte)((inverted ? 1 : 0) | (rowsPerStripe > 0 ? 2 : 0)) }; + data.AddRange(BigEndian(width, 2)); + data.AddRange(BigEndian(height, 2)); + if (rowsPerStripe == 0) + { + data.AddRange(CcittG4.Encode(rows, width, height)); + } + else + { + data.AddRange(BigEndian(rowsPerStripe, 2)); + for (var y = 0; y < height; y += rowsPerStripe) + { + var count = Math.Min(rowsPerStripe, height - y); + var stripe = CcittG4.Encode(rows[(y * stride)..((y + count) * stride)], width, count); + data.AddRange(BigEndian(stripe.Length, 4)); + data.AddRange(stripe); + } + } + return Chunk("Smmr", [.. data]); + } + + /// + /// An indirect document: the index file (its directory and chunks, e.g. NAVM) and the files + /// it names, given as (id, form, kind: 0 included file, 1 page, 2 thumbnails). + /// + public static (byte[] Index, (string Id, byte[] File)[] Files) Indirect(byte[][] extra, params (string Id, byte[] Form, int Kind)[] components) + { + var table = new List(); + foreach (var c in components) + table.AddRange(BigEndian(c.Form.Length - 8, 3)); + foreach (var c in components) + table.Add((byte)c.Kind); + foreach (var c in components) + { + table.AddRange(Encoding.UTF8.GetBytes(c.Id)); + table.Add(0); + } + var dirm = Chunk("DIRM", [0x01, .. BigEndian(components.Length, 2), .. BzzEncoder.Encode([.. table])]); + return (DjvuFile(Form("DJVM", [dirm, .. extra])), [.. components.Select(c => (c.Id, DjvuFile(c.Form)))]); + } + /// /// A bundled document: its directory and components, given as (id, form, is a page). Offsets are worked out here. /// - public static byte[] Bundle(params (string Id, byte[] Form, bool Page)[] components) + public static byte[] Bundle(params (string Id, byte[] Form, bool Page)[] components) => Bundle([], components); + + /// A bundled document with chunks (e.g. NAVM) after its directory. + public static byte[] Bundle(byte[][] extra, params (string Id, byte[] Form, bool Page)[] components) { var count = components.Length; byte[] Dirm(long[] offsets) @@ -1221,13 +1311,14 @@ byte[] Dirm(long[] offsets) } var dirmSize = Dirm(new long[count]).Length; var offsets = new long[count]; - long at = 4 + 12 + dirmSize; + long at = 4 + 12 + dirmSize + extra.Sum(c => c.Length); for (var i = 0; i < count; i++) { offsets[i] = at; at += components[i].Form.Length + (components[i].Form.Length & 1); } var parts = new List { Dirm(offsets) }; + parts.AddRange(extra); foreach (var c in components) parts.Add((c.Form.Length & 1) != 0 ? [.. c.Form, 0] : c.Form); return DjvuFile(Form("DJVM", [.. parts])); diff --git a/tests/Filee.Engines.Tests/DjvuFuzzTests.cs b/tests/Filee.Engines.Tests/DjvuFuzzTests.cs index b0cb34090..386e639e5 100644 --- a/tests/Filee.Engines.Tests/DjvuFuzzTests.cs +++ b/tests/Filee.Engines.Tests/DjvuFuzzTests.cs @@ -1,5 +1,6 @@ // Mutation fuzzing of the DjVu reader (Djvu/): the seeds are a page with a JB2 mask, a palette and hidden text, a -// bundled document with a shared dictionary, and a photo page (IW44 in two chunks), all written in code (DjvuBuilder). +// bundled document with a shared dictionary, a photo page (IW44 in two chunks), and a bundled document with bookmarks, +// hyperlinks and a G4 (Smmr) mask in stripes, all written in code (DjvuBuilder). // Every input must be read or refused with InvalidDataException / ConversionRefusedException, in time and on a 1 MB // stack; every page read is drawn and written as a PDF page. Run by FuzzHarness: a short fixed run normally, // FILEE_FUZZ_SECONDS for a longer random one, FILEE_FUZZ_SEED to repeat, FILEE_FUZZ_OUT to keep failing inputs. @@ -45,20 +46,38 @@ private static byte[] Photo() return DjvuFile(Form("DJVU", [Info(24, 16, 75, flags: 5), .. chunks])); } + private static byte[] LinkedBook() + { + var stride = 5; + var bits = new byte[stride * 24]; + for (var i = 0; i < bits.Length; i++) + bits[i] = (byte)(i * 37 % 7 == 0 ? 0xF0 : i % 3 == 0 ? 0x18 : 0); + var smmr = Page(40, 24, 100, Smmr(bits, 40, 24, rowsPerStripe: 10), AnnotationChunk("(maparea \"#2\" \"x\" (rect 1 1 10 10)) (maparea (url \"http://example.com/\" \"\") \"\" (poly 1 1 9 9 20 1))")); + var plain = Page(40, 24, 100, Chunk("Sjbz", new Jb2Writer().Start(40, 24).New(Letter, 5, 5).End()), AnnotationChunk("(maparea \"#-1\" \"\" (oval 2 2 8 8))", compressed: false)); + var outline = Outline(new Bookmark("One", "#1", new Bookmark("Two", "#p2")), new Bookmark("Web", "https://example.org/")); + return Bundle([outline], ("p1", smmr, true), ("p2", plain, true)); + } + [Fact] public void DjvuDocument_reads_or_refuses_every_mutated_file() => - new FuzzHarness(fx, QuickIterations, 20261120).Files("djvu", "djvu", [MaskPage(), BundledBook(), Photo()], FuzzHarness.Bytes, (path, _) => + new FuzzHarness(fx, QuickIterations, 20261120).Files("djvu", "djvu", [MaskPage(), BundledBook(), Photo(), LinkedBook()], FuzzHarness.Bytes, (path, _) => { // Smaller limits than real books need keep each input quick. var limits = DjvuLimits.Default with { MaxPagePixels = 4_000_000, MaxIw44Pixels = 1_000_000, MaxShapePixels = 4_000_000 }; using var document = DjvuDocument.Open(path, limits, TestContext.Current.CancellationToken); using var writer = new OcrPdfWriter(); + var pages = new List(); + var links = new List<(int Page, IReadOnlyList Links, double Scale)>(); for (var i = 0; i < Math.Min(document.Count, 4); i++) { var page = document.ReadPage(i, TestContext.Current.CancellationToken); DjvuRenderer.Compose(page, TestContext.Current.CancellationToken); - DjvuPdfWriter.AddPage(writer, page, TestContext.Current.CancellationToken); + pages.Add(DjvuPdfWriter.AddPage(writer, page, TestContext.Current.CancellationToken)); + links.Add((i, page.Links, DjvuPdfWriter.Scale(page))); } + foreach (var (page, pageLinks, scale) in links) + DjvuPdfWriter.AddLinks(writer.Document, pages[page], pageLinks, scale, pages); + DjvuPdfWriter.AddOutline(writer.Document, pages, document.Bookmarks, url => document.PageOf(url), TestContext.Current.CancellationToken); if (writer.PageCount > 0) writer.Document.Save(new MemoryStream()); }); diff --git a/tests/Filee.Engines.Tests/DjvuTests.cs b/tests/Filee.Engines.Tests/DjvuTests.cs index 8e2b4d306..483aa3d52 100644 --- a/tests/Filee.Engines.Tests/DjvuTests.cs +++ b/tests/Filee.Engines.Tests/DjvuTests.cs @@ -1,8 +1,9 @@ // Filee's own DjVu reader (Djvu/) and its converter: every piece checked against an encoder written in code // (DjvuBuilder) — the ZP-coder and BZZ round trips, JB2 masks (direct shapes, copies, refinements, non-mark data, a // shared dictionary, comments and a reset) against the shapes drawn by the test, IW44 pictures against their source -// pixels, hidden text as PDF words — then whole documents (one page and bundled) to PDF, PNG and TIFF, and hostile -// files refused quickly through the limits. +// pixels, hidden text as PDF words, the CCITT G4 codec (its code tables, round trips, Smmr masks), bookmarks and +// hyperlinks — then whole documents (one page, bundled and indirect) to PDF, PNG and TIFF, the PDF's JBIG2, G4 and +// Flate masks drawn back by PDFtoImage, its links and outline, and hostile files refused quickly through the limits. using System.Diagnostics; using Filee.Core.Conversion; @@ -10,6 +11,7 @@ using Filee.Core.Presets; using Filee.Engines.Djvu; using Filee.Engines.Documents; +using Filee.Engines.Ocr; using ImageMagick; using SkiaSharp; using UglyToad.PdfPig; @@ -551,7 +553,7 @@ public async Task Layers_filee_does_not_decode_are_named_in_the_result() } [Fact] - public async Task An_indirect_document_is_refused_with_its_own_message() + public async Task An_indirect_document_without_its_page_file_is_refused_with_its_own_message() { var dirm = Chunk("DIRM", [0x01, 0x00, 0x01, .. BzzEncoder.Encode([0, 0, 10, 1, .. "p1.djvu"u8.ToArray(), 0])]); var source = Save(DjvuFile(Form("DJVM", dirm))); @@ -559,6 +561,431 @@ public async Task An_indirect_document_is_refused_with_its_own_message() Assert.Equal(DjvuDocument.IndirectKey, refused.ErrorKey); } + // ---- CCITT G4 ---- + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void G4_run_codes_are_a_prefix_code_for_every_run(bool black) + { + var codes = CcittG4.Codes(black).ToList(); + Assert.Equal(64 + 27 + 13, codes.Count); + Assert.Equal(Enumerable.Range(0, 64).Concat(Enumerable.Range(1, 40).Select(i => i * 64)), codes.Select(c => c.Run)); + var words = codes.Select(c => System.Convert.ToString(c.Code, 2).PadLeft(c.Length, '0')).ToList(); + Assert.All(codes, c => Assert.True(c.Code < 1 << c.Length)); + for (var i = 0; i < words.Count; i++) + { + for (var j = 0; j < words.Count; j++) + Assert.False(i != j && words[j].StartsWith(words[i], StringComparison.Ordinal), $"{words[i]} starts {words[j]}"); + } + // With EOL's prefix (eight zeros) the codes fill the code space exactly. + var kraft = codes.Sum(c => Math.Pow(2, -c.Length)) + Math.Pow(2, -8); + Assert.Equal(1.0, kraft, 9); + } + + /// A 1-bit picture (rows of (width + 7) / 8 bytes, a set bit black) from a pixel rule. + private static byte[] Bits(int width, int height, Func black) + { + var stride = (width + 7) / 8; + var bits = new byte[stride * height]; + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + if (black(x, y)) + bits[y * stride + (x >> 3)] |= (byte)(0x80 >> (x & 7)); + } + } + return bits; + } + + [Theory] + [InlineData(1, 1)] + [InlineData(13, 9)] + [InlineData(64, 40)] + [InlineData(6001, 7)] // runs past 2560 and every make-up code + public void G4_round_trips_runs_text_noise_and_long_lines(int width, int height) + { + var random = new Random(width * 31 + height); + var noise = Bits(width, height, (_, _) => random.Next(3) == 0); + var stripes = Bits(width, height, (x, y) => (x / (1 + y)) % 2 == 0 || x == width - 1); + var lines = Bits(width, height, (x, y) => y % 3 == 0 ? x >= 2 : x % 1700 < 64 * y + 3); + foreach (var picture in new[] { noise, stripes, lines, new byte[noise.Length], Bits(width, height, (_, _) => true) }) + { + var g4 = CcittG4.Encode(picture, width, height, Token); + var back = new byte[picture.Length]; + CcittG4.Decode(g4, width, height, back, 0, Token); + Assert.Equal(picture, back); + } + } + + [Fact] + public void G4_damage_fails_as_damage_and_cut_short_data_leaves_white_rows() + { + var width = 100; + var picture = Bits(width, 50, (x, y) => (x + y) % 7 < 3); + var g4 = CcittG4.Encode(picture, width, 50, Token); + var half = new byte[picture.Length]; + CcittG4.Decode(g4.AsSpan(0, g4.Length / 2), width, 50, half, 0, Token); + Assert.Equal(picture.AsSpan(0, 13 * 5), half.AsSpan(0, 13 * 5)); + Assert.All(half[^13..], b => Assert.Equal(0, b)); + var random = new Random(4); + var watch = Stopwatch.StartNew(); + for (var i = 0; i < 200; i++) + { + var junk = new byte[random.Next(1, 400)]; + random.NextBytes(junk); + try + { + CcittG4.Decode(junk, 3000, 2000, new byte[375 * 2000], 0, Token); + } + catch (InvalidDataException) + { + } + } + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"took {watch.Elapsed}"); + } + + /// + /// A wide page in two palette colours: letters and bars copied along it (some across the page's edges), a rule + /// across the page and a long block (runs past 2560 pixels). + /// + private static byte[] WidePage() + { + var writer = new Jb2Writer().Start(3000, 60).New(Letter, 5, 40).New(Bar, 20, 44).Copy(0, 2995, 40).Copy(0, -3, 55).Copy(1, 1000, -2) + .New(Jb2Writer.Shape(new string('#', 2900)), 50, 20) + .New(Jb2Writer.Shape([.. Enumerable.Repeat(new string('#', 2700), 6)]), 100, 5); + for (var i = 0; i < 40; i++) + writer.Copy(i % 2, 200 + i * 60, 30 + i % 3); + var colors = Enumerable.Range(0, 47).Select(b => b % 3 == 0 ? 1 : 0).ToArray(); + return DjvuFile(Form("DJVU", Info(3000, 60, 100), Chunk("Sjbz", writer.End()), Palette([(0, 0, 0), (200, 0, 0)], colors))); + } + + [Theory] + [InlineData(1, "JBIG2Decode")] + [InlineData(2, "CCITTFaxDecode")] + [InlineData(4, "FlateDecode")] + public void Each_mask_coding_draws_back_as_the_page(int coding, string filter) + { + using var document = DjvuDocument.Open(Save(WidePage()), cancellationToken: Token); + var page = document.ReadPage(0, Token); + using var writer = new OcrPdfWriter(); + DjvuPdfWriter.AddPage(writer, page, (MaskCodings)coding, Token); + var output = new MemoryStream(); + writer.Document.Save(output); + var bytes = output.ToArray(); + using (var pdf = PdfDocument.Open(bytes)) + { + var images = pdf.GetPages().Single().GetImages().ToList(); + Assert.Equal(2, images.Count); // a mask per colour + Assert.All(images, image => Assert.Contains(filter, image.ImageDictionary.ToString())); + } + // PDFtoImage (pdfium's own decoders) draws it as the page looks. + var rgb = DjvuRenderer.Compose(page, Token); + using var bitmap = PDFtoImage.Conversion.ToImage(bytes, 0, null, new PDFtoImage.RenderOptions(Dpi: 100)); + Assert.Equal((3000, 60), (bitmap.Width, bitmap.Height)); + var differ = 0; + for (var y = 0; y < 60; y++) + { + for (var x = 0; x < 3000; x++) + { + var pixel = bitmap.GetPixel(x, y); + var at = (y * 3000 + x) * 3; + if (Math.Abs(pixel.Red - rgb[at]) > 64 || Math.Abs(pixel.Green - rgb[at + 1]) > 64 || Math.Abs(pixel.Blue - rgb[at + 2]) > 64) + differ++; + } + } + Assert.True(differ < 3000 * 60 / 200, $"{differ} pixels differ"); + } + + [Fact] + public async Task Text_masks_become_jbig2_symbols_much_smaller_than_g4() + { + // A page of 600 copies of two shapes: the symbols are coded once. + var writer = new Jb2Writer().Start(1200, 600).New(Letter, 5, 5).New(Bar, 20, 5); + for (var i = 0; i < 600; i++) + writer.Copy(i % 2, 40 + i % 30 * 38, 10 + i / 30 * 29); + var source = Save(DjvuFile(Form("DJVU", Info(1200, 600, 300), Chunk("Sjbz", writer.End())))); + var pdfPath = Assert.Single(await Convert(source, "pdf")); + using var pdf = PdfDocument.Open(pdfPath); + var image = Assert.Single(pdf.GetPages().Single().GetImages()); + Assert.Contains("JBIG2Decode", image.ImageDictionary.ToString()); + using var document = DjvuDocument.Open(source, cancellationToken: Token); + var page = document.ReadPage(0, Token); + var g4 = CcittG4.Encode(DjvuRenderer.MaskBits(page, cancellationToken: Token), 1200, 600, Token); + Assert.True(image.RawBytes.Length * 3 < g4.Length, $"JBIG2 {image.RawBytes.Length} bytes, G4 {g4.Length}"); + } + + [Fact] + public async Task A_dithered_mask_stays_flate_when_g4_and_jbig2_would_be_larger() + { + // Noise, each row the one above moved by a byte: G4 codes every change and JBIG2's few neighbours don't see + // the move, while Flate finds each row in the one before. + var random = new Random(8); + var noise = Enumerable.Range(0, 512).Select(_ => random.Next(2) == 0).ToArray(); + var rows = Enumerable.Range(0, 64).Select(y => string.Concat(Enumerable.Range(0, 512).Select(x => noise[(x + 8 * y) % 512] ? '#' : '.'))).ToArray(); + var mask = new Jb2Writer().Start(512, 64).New(Jb2Writer.Shape(rows), 0, 0).End(); + var source = Save(DjvuFile(Form("DJVU", Info(512, 64, 100), Chunk("Sjbz", mask)))); + using var pdf = PdfDocument.Open(Assert.Single(await Convert(source, "pdf"))); + var image = Assert.Single(pdf.GetPages().Single().GetImages()); + Assert.True(image.IsImageMask); + Assert.Contains("FlateDecode", image.ImageDictionary.ToString()); + } + + // ---- Smmr masks ---- + + [Theory] + [InlineData(0, false)] + [InlineData(7, false)] + [InlineData(0, true)] + [InlineData(16, true)] + public void An_smmr_mask_reads_as_the_page_mask(int rowsPerStripe, bool inverted) + { + var (_, expected) = TextMask(); + var bits = Bits(120, 60, (x, y) => expected[y, x]); + var source = Save(DjvuFile(Form("DJVU", Info(120, 60, 100), Smmr(bits, 120, 60, rowsPerStripe, inverted)))); + using var document = DjvuDocument.Open(source, cancellationToken: Token); + var page = document.ReadPage(0, Token); + Assert.Equal(expected, Mask(page)); + Assert.Equal(0, page.LeftOut); + } + + [Fact] + public void Damaged_smmr_masks_fail_as_damage() + { + byte[] Page(byte[] smmr) => DjvuFile(Form("DJVU", Info(120, 60, 100), Chunk("Smmr", smmr))); + foreach (var smmr in new byte[][] + { + [(byte)'M', (byte)'M', (byte)'X', 0, 0, 120, 0, 60], + [(byte)'M', (byte)'M', (byte)'R', 0, 0, 120, 0, 60, 0x00, 0x00, 0x00, 0x01, 0xFF], // an extension code + [(byte)'M', (byte)'M', (byte)'R', 2, 0, 120, 0, 60, 0, 10, 0x7F, 0xFF, 0xFF, 0xFF], // a stripe past the end + }) + { + using var document = DjvuDocument.Open(Save(Page(smmr), "damaged.djvu"), cancellationToken: Token); + Assert.Throws(() => document.ReadPage(0, Token)); + } + } + + // ---- bookmarks and links ---- + + /// + /// Three pages: the first with links to page 3 (by number), the next page ("#+1"), page 2 by id, a web address, a + /// polygon to the first page, a script, a local file and a line (none of the last three links); a shared + /// file with a link every page that includes it gets; bookmarks nested two deep, one to a web address and one to a + /// file next to the document (a title only). + /// + private static byte[] LinkedBook(bool compressed = true) + { + const string links = """ + (maparea "#3" "to page three" (rect 10 10 20 10) (xor)) + (maparea "#+1" "" (rect 40 10 20 10)) + (maparea (url "#p2" "_self") "" (oval 70 10 20 10)) + (maparea "https://example.com/a b?c=1" "web" (rect 10 30 30 10) (border #ff0000)) + (maparea "#1" "" (poly 50 30 60 45 70 30)) + (maparea "javascript:alert(1)" "" (rect 80 30 10 10)) + (maparea "file:///etc/passwd" "" (rect 95 30 10 10)) + (maparea "#2" "" (line 0 0 50 50)) + (zoom page) (background #FFFFFF) + """; + var shared = Form("DJVI", AnnotationChunk("(maparea \"http://shared.example/\" \"\" (text 0 50 120 10))", compressed)); + byte[] PageForm(params byte[][] extra) => Form("DJVU", [Info(120, 60, 100), .. extra]); + var outline = Outline( + new Bookmark("Chapter one", "#1", new Bookmark("Section", "#p2", new Bookmark("Deeper", "#3"))), + new Bookmark("Web", "https://example.org/"), + new Bookmark("Next volume", "volume2.djvu"), + new Bookmark("Script", "javascript:alert(1)")); + return Bundle([outline], + ("shared.djvi", shared, false), + ("p1", PageForm(Include("shared.djvi"), AnnotationChunk(links, compressed)), true), + ("p2", PageForm(), true), + ("p3", PageForm(Include("shared.djvi")), true)); + } + + [Fact] + public void Links_and_bookmarks_are_read_with_their_targets() + { + using var document = DjvuDocument.Open(Save(LinkedBook()), cancellationToken: Token); + var links = document.ReadPage(0, Token).Links; + Assert.Equal([2, 1, 1, -1, 0, -1], links.Select(l => l.Page)); + Assert.Equal("https://example.com/a b?c=1", links[3].Url); + Assert.Equal("http://shared.example/", links[5].Url); + Assert.Equal(new DjvuLink(50, 30, 20, 15, "#1", 0), links[4]); + Assert.Single(document.ReadPage(2, Token).Links); + Assert.Empty(document.ReadPage(1, Token).Links); + + Assert.Equal(["Chapter one", "Web", "Next volume", "Script"], document.Bookmarks.Select(b => b.Title)); + Assert.Equal("Deeper", document.Bookmarks[0].Children[0].Children[0].Title); + Assert.Equal(1, document.PageOf("#p2")); + Assert.Equal(-1, document.PageOf("#4")); + Assert.Equal(-1, document.PageOf("#-1", 0)); + Assert.Equal(-1, document.PageOf("#nowhere")); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task Links_become_pdf_link_annotations_and_bookmarks_its_outline(bool compressed) + { + var pdfPath = Assert.Single(await Convert(Save(LinkedBook(compressed)), "pdf")); + using (var pdf = PdfDocument.Open(pdfPath)) + { + var pages = pdf.GetPages().ToList(); + var links = pages[0].GetAnnotations().ToList(); + Assert.Equal(6, links.Count); + Assert.All(links, a => Assert.Equal(UglyToad.PdfPig.Annotations.AnnotationType.Link, a.Type)); + var text = string.Join("\n", links.Select(a => a.AnnotationDictionary.ToString())); + Assert.Contains("https://example.com/a%20b?c=1", text); + Assert.Contains("http://shared.example/", text); + Assert.DoesNotContain("javascript", text); + Assert.DoesNotContain("passwd", text); + // The first link (page pixels 10, 10, 20 × 10 at 100 dpi) at 72 / 100 points per pixel. + var first = links[0].Rectangle; + Assert.Equal(7.2, first.Left, 2); + Assert.Equal(7.2, first.Bottom, 2); + Assert.Equal(21.6, first.Right, 2); + Assert.Equal(14.4, first.Top, 2); + Assert.Single(pages[2].GetAnnotations()); + + Assert.True(pdf.TryGetBookmarks(out var outline)); + var chapter = Assert.IsType(outline.Roots[0]); + Assert.Equal(("Chapter one", 1), (chapter.Title, chapter.PageNumber)); + var section = Assert.IsType(Assert.Single(chapter.Children)); + Assert.Equal(2, section.PageNumber); + Assert.Equal(3, Assert.IsType(Assert.Single(section.Children)).PageNumber); + Assert.Equal("https://example.org/", Assert.IsType(outline.Roots[1]).Uri); + } + // The bookmarks that lead nowhere Filee links to keep only their titles. + var raw = System.Text.Encoding.Latin1.GetString(File.ReadAllBytes(pdfPath)); + Assert.Contains(Utf16Hex("Next volume"), raw); + Assert.Contains(Utf16Hex("Script"), raw); + Assert.DoesNotContain("javascript", raw); + Assert.DoesNotContain("volume2", raw); + } + + private static string Utf16Hex(string text) => string.Concat(System.Text.Encoding.BigEndianUnicode.GetBytes(text).Select(b => b.ToString("X2", System.Globalization.CultureInfo.InvariantCulture))); + + [Theory] + [InlineData("javascript:alert(1)", false)] + [InlineData("JavaScript:alert(1)", false)] + [InlineData("file:///C:/Windows/win.ini", false)] + [InlineData("vbscript:x", false)] + [InlineData("data:text/html,x", false)] + [InlineData("other.djvu", false)] + [InlineData("\\\\server\\share", false)] + [InlineData("https://example.com/", true)] + [InlineData("http://example.com/x?y=1#z", true)] + [InlineData("mailto:someone@example.com", true)] + [InlineData("ftp://example.com/file", true)] + public void Only_web_addresses_are_links_out_of_the_document(string url, bool allowed) => + Assert.Equal(allowed, DjvuAnnotations.IsWebAddress(url)); + + [Fact] + public void Damaged_annotations_leave_the_page_without_links() + { + var limits = DjvuLimits.Default; + Assert.Empty(DjvuAnnotations.ReadLinks("(maparea \"#1\" \"\" (rect 1 1 5 5)", limits)); + Assert.Empty(DjvuAnnotations.ReadLinks(new string('(', 100_000) + new string(')', 100_000), limits)); + Assert.Empty(DjvuAnnotations.ReadLinks("(maparea \"#1\" \"\" (rect 1 1 x 5))) (maparea \"#1", limits)); + Assert.Equal("a\"b\nc\u0001", Assert.Single(Assert.Single(DjvuAnnotations.Parse("(\"a\\\"b\\nc\\001\")", limits)))); + // One good link after damage still counts when the brackets balance. + Assert.Single(DjvuAnnotations.ReadLinks("(junk (more)) (maparea \"#1\" \"\" (rect 1 1 5 5))", limits)); + } + + // ---- indirect documents ---- + + /// Writes an indirect document's index and files into a new folder; the index path. + private string SaveIndirect((byte[] Index, (string Id, byte[] File)[] Files) document, params string[] skip) + { + var folder = Path.Combine(_folder, "indirect-" + Guid.NewGuid().ToString("N")[..6]); + Directory.CreateDirectory(folder); + foreach (var (id, file) in document.Files) + { + if (!skip.Contains(id)) + File.WriteAllBytes(Path.Combine(folder, id), file); + } + var index = Path.Combine(folder, "index.djvu"); + File.WriteAllBytes(index, document.Index); + return index; + } + + private static (byte[] Index, (string Id, byte[] File)[] Files) IndirectBook(string secondId = "p0002.djvu") + { + var dictionaryWriter = new Jb2Writer().Start(0, 0).New(Letter, type: 2).New(Bar, type: 2); + var shared = Form("DJVI", Chunk("Djbz", dictionaryWriter.End())); + var mask = new Jb2Writer().RequireDictionary(2, dictionaryWriter.Library).Start(120, 60).Copy(0, 5, 40).Copy(1, 20, 44).End(); + var page1 = Form("DJVU", Info(120, 60, 100), Include("dict.djbz"), Chunk("Sjbz", mask), AnnotationChunk("(maparea \"#2\" \"\" (rect 5 40 8 8))")); + var page2 = Form("DJVU", Info(120, 60, 100), Chunk("Sjbz", new Jb2Writer().Start(120, 60).New(Bar, 50, 30).End())); + var thumbnails = Form("THUM", Chunk("TH44", [0, 1])); + return Indirect([Outline(new Bookmark("Second", "#" + secondId))], + ("dict.djbz", shared, 0), ("p0001.djvu", page1, 1), (secondId, page2, 1), ("thumbs.djvu", thumbnails, 2)); + } + + [Fact] + public async Task An_indirect_document_reads_its_page_files_next_to_the_index() + { + // Thumbnails may be missing; pages and included files may not. + var index = SaveIndirect(IndirectBook(), "thumbs.djvu"); + using (var document = DjvuDocument.Open(index, cancellationToken: Token)) + { + Assert.Equal(2, document.Count); + Assert.Equal(Expected(120, 60, (Letter, 5, 40), (Bar, 20, 44)), Mask(document.ReadPage(0, Token))); + Assert.Equal(Expected(120, 60, (Bar, 50, 30)), Mask(document.ReadPage(1, Token))); + Assert.Equal(1, Assert.Single(document.ReadPage(0, Token).Links).Page); + Assert.Equal(1, document.PageOf(document.Bookmarks[0].Url)); + } + using var pdf = PdfDocument.Open(Assert.Single(await Convert(index, "pdf"))); + Assert.Equal(2, pdf.NumberOfPages); + Assert.Single(pdf.GetPage(1).GetAnnotations()); + + foreach (var missing in new[] { "p0001.djvu", "dict.djbz" }) + { + var refused = await Assert.ThrowsAsync(() => Convert(SaveIndirect(IndirectBook(), missing), "pdf")); + Assert.Equal(DjvuDocument.IndirectKey, refused.ErrorKey); + } + } + + [Theory] + [InlineData("../outside.djvu")] + [InlineData("sub/../../outside.djvu")] + [InlineData("..\\outside.djvu")] + [InlineData("C:/outside.djvu")] + [InlineData("/outside.djvu")] + [InlineData("//server/share/outside.djvu")] + [InlineData("NUL")] + [InlineData("p.djvu:stream")] + public void An_indirect_document_reads_no_file_outside_its_folder(string id) + { + // The page exists where the name leads (outside the index file's folder) and is still refused. + var book = IndirectBook(id); + var index = SaveIndirect(book, id); + var page = book.Files.First(f => f.Id == id).File; + var outside = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(index))!, "outside.djvu"); + File.WriteAllBytes(outside, page); + var refused = Assert.Throws(() => DjvuDocument.Open(index, cancellationToken: Token).Dispose()); + Assert.Equal(DjvuDocument.IndirectKey, refused.ErrorKey); + } + + [Fact] + public void An_indirect_document_follows_no_link_out_of_its_folder() + { + var book = IndirectBook("linked.djvu"); + var index = SaveIndirect(book, "linked.djvu"); + var elsewhere = Path.Combine(_folder, "elsewhere-" + Guid.NewGuid().ToString("N")[..6]); + Directory.CreateDirectory(elsewhere); + var target = Path.Combine(elsewhere, "linked.djvu"); + File.WriteAllBytes(target, book.Files.First(f => f.Id == "linked.djvu").File); + try + { + File.CreateSymbolicLink(Path.Combine(Path.GetDirectoryName(index)!, "linked.djvu"), target); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Assert.Skip("This machine doesn't let the test make a symbolic link."); + } + var refused = Assert.Throws(() => DjvuDocument.Open(index, cancellationToken: Token).Dispose()); + Assert.Equal(DjvuDocument.IndirectKey, refused.ErrorKey); + } + [Fact] public void DjVu_is_a_readable_document_format_with_a_route_to_pdf_and_pictures() { @@ -595,6 +1022,10 @@ public void Hostile_pages_are_refused_quickly() var text = DjvuBuilder.Text("x", new TextZone(1, 0, 0, 120, 60, 0, 1, new TextZone(5, 0, 0, 120, 10, 0, 1, zones))); var zoneData = BzzDecoder.Decode(text[8..(8 + ReadSize(text))], 1 << 20, Token); Assert.Throws(() => DjvuText.Read(zoneData, DjvuLimits.Default with { MaxTextZones = 10 })); + // Annotations with more parts than the (lowered) limit, and an Smmr mask claiming 65,535 × 65,535 pixels. + var annotations = string.Concat(Enumerable.Repeat("(maparea \"#1\" \"\" (rect 1 1 5 5)) ", 20)); + AssertRefused(DjvuFile(Form("DJVU", Info(100, 100), AnnotationChunk(annotations))), DjvuLimits.Default with { MaxAnnotationNodes = 50 }); + AssertRefused(DjvuFile(Form("DJVU", Info(100, 100), Chunk("Smmr", [(byte)'M', (byte)'M', (byte)'R', 0, 0xFF, 0xFF, 0xFF, 0xFF]))), limits: null); Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"took {watch.Elapsed}"); } From 8c6867ed4a423add7f7747a1caa91b88d4daa1d4 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 23:03:09 +0900 Subject: [PATCH 5/7] perf(djvu): code JBIG2 symbol bitmaps a row at a time A page-sized shape (an Smmr mask) no longer needs a padded copy of its pixels while it is coded. Co-Authored-By: Claude Opus 5.5 --- src/Filee.Engines/Djvu/Jbig2Encoder.cs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Filee.Engines/Djvu/Jbig2Encoder.cs b/src/Filee.Engines/Djvu/Jbig2Encoder.cs index 05804c4c8..ceef8bb14 100644 --- a/src/Filee.Engines/Djvu/Jbig2Encoder.cs +++ b/src/Filee.Engines/Djvu/Jbig2Encoder.cs @@ -112,23 +112,19 @@ private static List SymbolDictionary(IReadOnlyList shapes, List< private static void Generic(MqEncoder coder, byte[] contexts, Jb2Shape shape) { int w = shape.Width, h = shape.Height; - // The shape's rows top first, with two blank rows above and margins of four columns. - var stride = w + 8; - var pixels = new byte[(h + 2) * stride]; - for (var y = 0; y < h; y++) - Array.Copy(shape.Bits, (long)(h - 1 - y) * w, pixels, (y + 2) * stride + 4, w); + // The row being coded and the two above it (blank above the first), with margins of four columns. + byte[] row = new byte[w + 8], up = new byte[w + 8], up2 = new byte[w + 8]; for (var y = 0; y < h; y++) { - var row = (y + 2) * stride + 4; - var up = row - stride; - var up2 = up - stride; - for (var x = 0; x < w; x++) + (up2, up, row) = (up, row, up2); + Array.Copy(shape.Bits, (long)(h - 1 - y) * w, row, 4, w); + for (var x = 4; x < w + 4; x++) { - var c = (pixels[row + x - 1] << 0) | (pixels[row + x - 2] << 1) | (pixels[row + x - 3] << 2) | (pixels[row + x - 4] << 3) - | (pixels[up + x + 2] << 4) | (pixels[up + x + 1] << 5) | (pixels[up + x] << 6) | (pixels[up + x - 1] << 7) | (pixels[up + x - 2] << 8) - | (pixels[up2 + x + 1] << 9) | (pixels[up2 + x] << 10) | (pixels[up2 + x - 1] << 11) - | (pixels[up + x + 3] << 12) | (pixels[up + x - 3] << 13) | (pixels[up2 + x + 2] << 14) | (pixels[up2 + x - 2] << 15); - coder.Encode(contexts, c, pixels[row + x]); + var c = row[x - 1] | (row[x - 2] << 1) | (row[x - 3] << 2) | (row[x - 4] << 3) + | (up[x + 2] << 4) | (up[x + 1] << 5) | (up[x] << 6) | (up[x - 1] << 7) | (up[x - 2] << 8) + | (up2[x + 1] << 9) | (up2[x] << 10) | (up2[x - 1] << 11) + | (up[x + 3] << 12) | (up[x - 3] << 13) | (up2[x + 2] << 14) | (up2[x - 2] << 15); + coder.Encode(contexts, c, row[x]); } } } From fc3c3b3fcdeda8dbbb966ddb5678ad9009b33cd8 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 23:54:09 +0900 Subject: [PATCH 6/7] fix(markdown): Word, ODT and RTF to Markdown in one step, equations as math MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DOCX, ODT and RTF reached Markdown through the HWPX writer, which gave the equations back as 한글 equation script, written as italic text. The HWP text engine now writes Markdown from them directly (cost 15, direct only, like TXT), so equations come out as TeX math. An equation alone on its line is display math also when tabs centre it, and tabs at the start or end of a paragraph no longer write spaces that turned the paragraph into an indented code block. Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 6 ++++- src/Filee.Engines/Hwp/HwpTextConverter.cs | 7 +++-- src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs | 11 +++++--- .../LibreOfficeRouteTests.cs | 3 ++- .../Filee.Engines.Tests/TexMathWriterTests.cs | 27 +++++++++++++++++++ 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/docs/ENGINES.md b/docs/ENGINES.md index b049b3116..9fa194478 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -11,7 +11,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | ICNS | built in | images → ICNS (16–1024 px), ICNS → images | | PDFsharp | built in (library) | images → PDF (CMYK pictures stay CMYK), merge, split, page ranges, page organising, lossless rotate and crop-box edits, pages per sheet (N-up), spreadsheet print-studio assembly, **compression** (smaller PDFs, see below), PDFs without their metadata | | PDFium | built in (library) | PDF → images (multi-page TIFF/GIF supported); CMYK pages → CMYK JPG/TIFF through Ghostscript when it is installed | -| **HWP text** | built in | **HWP/HWPX → TXT, Markdown, HTML** with the [HWP](#hwp-reader) and [HWPX readers](#hwpx-reader) (pictures in a `_files` folder next to the Markdown, embedded in the HTML); **RTF, Word (DOCX + DOCM/DOTX/DOTM, DOC + DOT, WPS Office WPS) and ODT → TXT** a line per paragraph, as a word processor saves plain text (see [HWP text, Markdown and HTML](#hwp-text-markdown-and-html)) | +| **HWP text** | built in | **HWP/HWPX → TXT, Markdown, HTML** with the [HWP](#hwp-reader) and [HWPX readers](#hwpx-reader) (pictures in a `_files` folder next to the Markdown, embedded in the HTML); **RTF, Word (DOCX + DOCM/DOTX/DOTM, DOC + DOT, WPS Office WPS) and ODT → TXT** a line per paragraph, as a word processor saves plain text, **and → Markdown** (equations as TeX math) (see [HWP text, Markdown and HTML](#hwp-text-markdown-and-html)) | | Text & Markdown (Markdig) | built in (library) | **Markdown → HTML, TXT**; **TXT, LOG, CXLOG, PY, PAC, BAT, CMD, COMMAND, LUA, CSS, PS1, SQL, AHK, INI, RSC, C, H, CPP, CC, CXX, HPP, HH, HXX, CS, DART, HQL, DBML, JAVA, INO, PDE → UTF-8 TXT**, preserving source text without running scripts (and → PDF through the Filee renderer, → DOCX through the DOCX writer) | | **Markup languages** | built in | **Jupyter notebooks (IPYNB), OPML, Textile, MediaWiki, reStructuredText, Org, DocBook 5 → Markdown, HTML, TXT and each other**; every document the built-in readers read → each of them; each → DOCX, ODT, HWPX, PDF through the document writers (see [Lightweight markup languages](#lightweight-markup-languages)); **FreeMind / Freeplane (MM) and XMind mind maps → Markdown, HTML, TXT, OPML** as outlines (see [Mind maps](#mind-maps)) | | Spreadsheets (ExcelDataReader for XLS) | built in (library) | **XLSX, XLSB, XLS, ODS (+ OTS, FODS), CSV, TSV → XLSX, ODS, CSV, TSV** (one CSV / TSV per sheet; OTS and FODS out through the next engine) | @@ -3723,6 +3723,10 @@ direct only (a TXT step in another route would lose the formatting) and cost 15: goes to LibreOffice. Without them the cheapest route went through the Filee renderer's PDF and its text, which wrapped long lines and lost the indentation and the empty lines. +**RTF, Word and ODT → Markdown** take the same one step (also cost 15, direct only) with `MarkdownWriter`, as HWP +does: equations come out as TeX math (`$…$`, `$$…$$` for one alone on its line, also when tabs centre it). The route +before went through the HWPX writer, which gave the equations back as 한글 equation script, or through Pandoc. + Unhwp (a native Rust library through NuGet) did this before. It needed the Visual C++ runtime next to Filee.exe and had no Linux or Windows ARM64 build of its own (Filee built one, #236). Measured before it was removed, on the 30 documents generated in code by the tests (the rich DOCX of `DocumentFuzzTests` written as HWPX, the 24 diff --git a/src/Filee.Engines/Hwp/HwpTextConverter.cs b/src/Filee.Engines/Hwp/HwpTextConverter.cs index 897389d76..c5cc785cd 100644 --- a/src/Filee.Engines/Hwp/HwpTextConverter.cs +++ b/src/Filee.Engines/Hwp/HwpTextConverter.cs @@ -20,7 +20,7 @@ namespace Filee.Engines.Hwp; -/// Writes the text of HWP and HWPX documents as TXT, Markdown or HTML, and of Word, ODT and RTF as TXT. +/// Writes the text of HWP and HWPX documents as TXT, Markdown or HTML, and of Word, ODT and RTF as TXT or Markdown. public sealed class HwpTextConverter : IConverter, ICapabilityCheck { /// The engine's id; settings that named Unhwp ("unhwp", the engine this replaced) are moved to it. @@ -43,6 +43,9 @@ public sealed class HwpTextConverter : IConverter, ICapabilityCheck // routes through Pandoc's Markdown (29) or the HWPX writer (32). The DOC reader checks each file (a Word 6.0 / // 95 file goes another way). .. LineFormats.Select(f => new ConversionEdge(f, "txt", LineCost, DirectOnly: true, Checked: DocumentReaders.Checked(f))), + // RTF, Word and ODT → Markdown in one step, at the same cost: through the HWPX writer (the route before) the + // equations came back as 한글 equation script, while the document model keeps them as math for $…$. + .. LineFormats.Select(f => new ConversionEdge(f, "md", LineCost, DirectOnly: true, Checked: DocumentReaders.Checked(f))), ]; /// @@ -54,7 +57,7 @@ public sealed class HwpTextConverter : IConverter, ICapabilityCheck /// The cost of RTF, Word and ODT → TXT: below LibreOffice's fallback (25) with any engine order's penalty (0 to 9). internal const int LineCost = 15; - public EngineStatus GetStatus() => EngineStatus.Available("HWP, HWPX → TXT, Markdown, HTML; Word, ODT, RTF → TXT", EngineVersions.BuiltIn); + public EngineStatus GetStatus() => EngineStatus.Available("HWP, HWPX → TXT, Markdown, HTML; Word, ODT, RTF → TXT, Markdown", EngineVersions.BuiltIn); /// A Word 6.0 / 95 or Works file with a .doc or .wps name, which the DOC reader doesn't read. public string? FirstUnsupported(string sourcePath, string sourceFormat) => DocumentReaders.Unsupported(sourcePath, sourceFormat); diff --git a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs index eac8bcb52..8046ff18a 100644 --- a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs @@ -78,9 +78,14 @@ private static bool IsCode(HParagraph paragraph) => private void Paragraph(HParagraph paragraph, StringBuilder sb, string prefix, HBlock? next) { - _display = paragraph.Inlines.Count(i => i is not HBookmark) == 1 && paragraph.Inlines.OfType().FirstOrDefault() is { } lone - && HEquation.IsDisplay(lone, paragraph) ? lone : null; - var text = Inlines(paragraph.Inlines); + // An equation alone on its line is display math, also when tabs or spaces centre it (Word and 한글 do that). + var shown = paragraph.Inlines.Where(i => i is not (HBookmark or HTab) && !(i is HText run && string.IsNullOrWhiteSpace(run.Text))).ToList(); + _display = shown.Count == 1 && shown[0] is HEquation lone ? lone : null; + // Tabs at the start would write spaces that make the paragraph an indented code block, at the end a line break. + var first = paragraph.Inlines.FindIndex(i => i is not HTab); + var last = paragraph.Inlines.FindLastIndex(i => i is not HTab); + var text = _display is not null ? Inlines(shown) + : first < 0 ? "" : Inlines(paragraph.Inlines.GetRange(first, last - first + 1)); _display = null; if (paragraph.Inlines.Any(i => i is HShape { Kind: HShapeKind.Line }) && text.Trim().Length == 0) { diff --git a/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs b/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs index c88809c55..8135bf3f1 100644 --- a/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs +++ b/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs @@ -82,7 +82,8 @@ public void Rtf_goes_to_txt_through_the_built_in_reader() var planner = Planner(slides: false); Assert.Equal("hwp-text", Assert.Single(planner.Plan("rtf", "txt")!.Steps).Converter.Id); - Assert.Equal("pandoc", Assert.Single(planner.Plan("rtf", "md")!.Steps).Converter.Id); + // RTF → Markdown is built in too: one step through the document model, which keeps equations as math. + Assert.Equal("hwp-text", Assert.Single(planner.Plan("rtf", "md")!.Steps).Converter.Id); Assert.DoesNotContain(planner.Plan("rtf", "epub")!.Steps, s => s.From == "rtf" && s.To == "txt"); // not a hop } diff --git a/tests/Filee.Engines.Tests/TexMathWriterTests.cs b/tests/Filee.Engines.Tests/TexMathWriterTests.cs index 5b822e298..92010c497 100644 --- a/tests/Filee.Engines.Tests/TexMathWriterTests.cs +++ b/tests/Filee.Engines.Tests/TexMathWriterTests.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Xml.Linq; +using Filee.Engines.Hwp; using Filee.Engines.Hwp.Hwpx; using Filee.Engines.Hwp.Hwpx.Docx; @@ -174,6 +175,32 @@ public void Markdown_keeps_tex_it_read_as_written() Assert.Contains("$\\pi r^2$", MarkdownWriter.Write(read, _ => null), StringComparison.Ordinal); } + [Fact] + public void An_equation_set_between_tabs_is_display_math_and_tabs_make_no_code_block() + { + // Word and 한글 often centre an equation with tabs on a left-aligned line. + var centred = new HParagraph(); + centred.Inlines.AddRange([new HTab(default), new HEquation(new MathText("x"), default), new HTab(default), new HText(" ", default)]); + var indented = new HParagraph(); + indented.Inlines.AddRange([new HTab(default), new HText("Indented text.", default), new HTab(default)]); + + var markdown = MarkdownWriter.Write(Document(centred, indented), _ => null); + Assert.Contains("\n$$x$$\n", "\n" + markdown, StringComparison.Ordinal); + Assert.Contains("\nIndented text.\n", "\n" + markdown, StringComparison.Ordinal); + Assert.DoesNotContain(" ", markdown, StringComparison.Ordinal); + } + + [Theory] + [InlineData("docx")] + [InlineData("odt")] + [InlineData("rtf")] + public void Word_odt_and_rtf_reach_markdown_in_one_built_in_step(string source) + { + // Through the HWPX writer the equations came back as 한글 equation script. + var route = fx.Catalog.CreatePlanner().Plan(source, "md"); + Assert.Equal(HwpTextConverter.EngineId, Assert.Single(route!.Steps).Converter.Id); + } + // ───────────────────────── Depth ───────────────────────── [Fact] From 8ac2a626cd480c8a5e710c39f7e61f73a9d11c45 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Sat, 21 Nov 2026 04:16:44 +0900 Subject: [PATCH 7/7] =?UTF-8?q?Keep=20#391's=20Markdown=20tables=20now=20t?= =?UTF-8?q?hat=20DOCX=20=E2=86=92=20Markdown=20is=20built=20in?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A cell's paragraphs are joined by
and its list items marked "•" or "1.", as Pandoc's filter did; before, the built-in writer joined them with spaces. The two #391 tests no longer need Pandoc: DOCX → Markdown goes through hwp-text wherever Pandoc stands, so they check the built-in output (pictures linked relative to the Markdown file, merged cells as a Markdown table). Co-Authored-By: Claude Opus 5.5 --- src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs | 33 +++++++++++++++---- .../Filee.Engines.Tests/PandocMarkupTests.cs | 9 +++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs index 8046ff18a..1fec6e46d 100644 --- a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs @@ -151,13 +151,9 @@ private void Table(HTable table, StringBuilder sb, string prefix) break; if (cell.ColSpan <= 1) alignments.Add(column, cell); - // Header cells are bold by nature: no ** inside them. A cell is one line: its line breaks are
, - // which MarkdownReader and the usual Markdown renderers read as breaks. - var header = table.Rows[r].Header; - cells[column] = string.Join(" ", cell.Blocks.OfType() - .Select(p => header ? p.Inlines.Select(i => i is HText t ? new HText(t.Text, t.Format with { Bold = null }) : i).ToList() : p.Inlines) - .Select(inlines => Inlines(inlines).Replace(HardBreak, "
", StringComparison.Ordinal).Replace("\n", " ", StringComparison.Ordinal))) - .Replace("|", "\\|", StringComparison.Ordinal); + // Header cells are bold by nature: no ** inside them. A cell is one line: its line breaks and its + // paragraphs are
, which MarkdownReader and the usual Markdown renderers read as breaks (#391). + cells[column] = Cell(cell, table.Rows[r].Header).Replace("|", "\\|", StringComparison.Ordinal); } grid.Add(cells); } @@ -172,6 +168,29 @@ private void Table(HTable table, StringBuilder sb, string prefix) sb.Append('\n'); } + /// + /// A cell's paragraphs on one line, joined by <br>: empty ones left out, list items marked "•" or "1." (counted + /// within the cell), as a Markdown table can't hold a list. + /// + private string Cell(HCell cell, bool header) + { + var parts = new List(); + var number = 0; + foreach (var paragraph in cell.Blocks.OfType()) + { + var inlines = header + ? paragraph.Inlines.Select(i => i is HText t ? new HText(t.Text, t.Format with { Bold = null }) : i).ToList() + : paragraph.Inlines; + var text = Inlines(inlines).Replace(HardBreak, "
", StringComparison.Ordinal).Replace("\n", " ", StringComparison.Ordinal).Trim(); + var list = paragraph.List is { Numbered: true } item ? item : null; + number = list is not null && !IsBullet(list) ? number + 1 : 0; + if (text.Length == 0) + continue; + parts.Add(list is null ? text : (IsBullet(list) ? "• " : $"{number}. ") + text); + } + return string.Join("
", parts); + } + /// /// The alignment each column's cells share, written as the column's delimiter (":---:" centred, "---:" right). Left /// is how Markdown shows a column anyway, so a left, justified or unaligned column stays "---" (documents read from diff --git a/tests/Filee.Engines.Tests/PandocMarkupTests.cs b/tests/Filee.Engines.Tests/PandocMarkupTests.cs index dd3de807e..947e9e94f 100644 --- a/tests/Filee.Engines.Tests/PandocMarkupTests.cs +++ b/tests/Filee.Engines.Tests/PandocMarkupTests.cs @@ -203,16 +203,16 @@ public async Task Rst_to_docx_markdown_and_hwpx() [Fact] public async Task Docx_to_markdown_extracts_the_pictures_or_leaves_them_out() { - SkipUnlessInstalled(); + // DOCX → Markdown is built in (hwp-text) wherever Pandoc stands; Pandoc isn't needed. var source = await WriteAsync("memo.md", "# Memo\n\nBefore the chart.\n\n![](chart.png)\n\nAfter the chart.\n"); EngineFixture.MakeImage(Path.GetDirectoryName(source)!, "chart.png", ImageMagick.MagickFormat.Png); var docx = await ConvertAsync(source, "docx"); File.Delete(source); - Assert.Equal("pandoc", Assert.Single(fx.Catalog.CreatePlanner().Plan("docx", "md")!.Steps).Converter.Id); + Assert.Equal("hwp-text", Assert.Single(fx.Catalog.CreatePlanner().Plan("docx", "md")!.Steps).Converter.Id); var withPictures = await File.ReadAllTextAsync(await ConvertAsync(docx, "md"), TestContext.Current.CancellationToken); // Linked relative to the Markdown file, so both can be moved together (#391). - Assert.Contains("](memo_files/media/", withPictures); + Assert.Matches(@"\]\(, which Markdown editors show as code (#391). - SkipUnlessInstalled(); + // HTML , which Markdown editors show as code (#391). DOCX → Markdown is built in; Pandoc isn't needed. var html = await WriteAsync("history.html", """
VersionDate and author