From 2f32dd75250e68714bb358046a684bdf9debe328 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 18:04:50 +0900 Subject: [PATCH 1/6] Convert OpenDocument templates and flat files without LibreOffice A new built-in engine, odf-variants (OdfVariantConverter), turns each form of an OpenDocument type into the others without reading the document into a model: ODT/OTT/FODT, ODS/OTS/FODS, ODP/OTP/FODP and ODG/OTG/FODG. A template is the package with another media type in mimetype and the manifest; a flat file is the package's parts put together into one office:document (pictures as office:binary-data, embedded objects as office:document inside draw:object) or taken apart again (Odf/OdfFlat). Automatic styles of styles.xml that clash with content.xml are renamed with their references. The readers read every form: OTT and OTS directly, FODT and FODS as the package they split into, so FODT/OTT reach PDF through the Filee renderer and the writers, FODS/OTS reach PDF through sheet printing and XLSX/CSV/TSV. Out, DOCX -> ODT -> OTT/FODT and XLSX -> ODS -> OTS/FODS chain through the new engine; the ODP writer writes OTP and FODP itself (its edges are direct only). LibreOffice keeps its edges at fallback cost. OTG is a new vector format; FODS, FODP and FODG become writable. Settings v111 teaches existing vector donuts .otg. New engine texts in all locales; README and docs/ENGINES.md drop the LibreOffice mark for these formats. Flat files are read through SafeXml within the document limits and at most four times a package part; pictures decode within the part, package and entry limits; objects are bounded in count and depth; a flat file made from a package is bounded in size. Hostile tests build their inputs in code; a fuzz test mutates flat texts, flat sheets and packages turned flat. Co-Authored-By: Claude Opus 5.5 --- README.md | 8 +- docs/ENGINES.md | 96 +++- src/Filee.App/Assets/i18n/ar.json | 2 + src/Filee.App/Assets/i18n/bn-BD.json | 2 + 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 | 2 + 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 | 2 + src/Filee.App/Assets/i18n/zh-CN.json | 2 + src/Filee.App/Assets/i18n/zh-TW.json | 2 + src/Filee.Core/Formats/FormatFields.cs | 2 +- src/Filee.Core/Formats/FormatRegistry.cs | 8 +- src/Filee.Core/Settings/AppSettings.cs | 2 +- src/Filee.Core/Settings/UserDataStore.cs | 3 + src/Filee.Engines/Drawings/DrawingFormats.cs | 6 +- src/Filee.Engines/EngineRegistry.cs | 1 + src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs | 10 + src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs | 2 +- .../Infrastructure/ComplexScripts.cs | 4 +- src/Filee.Engines/Odf/Draw/OdfDrawReader.cs | 4 +- src/Filee.Engines/Odf/OdfFlat.cs | 482 ++++++++++++++++++ src/Filee.Engines/Odf/OdfVariants.cs | 228 +++++++++ .../Office/DocumentTablesConverter.cs | 6 +- .../Office/OdfVariantConverter.cs | 48 ++ .../Office/OdpWriterConverter.cs | 38 +- .../Office/OdtWriterConverter.cs | 7 +- .../Office/SheetPrintConverter.cs | 6 +- .../Office/Sheets/SheetCapabilities.cs | 6 +- .../Office/SpreadsheetConverter.cs | 18 +- .../Typesetting/FileeRendererConverter.cs | 26 +- tests/Filee.Core.Tests/FormatFieldsTests.cs | 2 +- tests/Filee.Core.Tests/OtgSettingsTests.cs | 36 ++ .../DrawingConverterTests.cs | 2 +- tests/Filee.Engines.Tests/OdfDrawTests.cs | 2 +- .../OdfVariantFuzzTests.cs | 70 +++ tests/Filee.Engines.Tests/OdfVariantTests.cs | 399 +++++++++++++++ 51 files changed, 1497 insertions(+), 71 deletions(-) create mode 100644 src/Filee.Engines/Odf/OdfFlat.cs create mode 100644 src/Filee.Engines/Odf/OdfVariants.cs create mode 100644 src/Filee.Engines/Office/OdfVariantConverter.cs create mode 100644 tests/Filee.Core.Tests/OtgSettingsTests.cs create mode 100644 tests/Filee.Engines.Tests/OdfVariantFuzzTests.cs create mode 100644 tests/Filee.Engines.Tests/OdfVariantTests.cs diff --git a/README.md b/README.md index d4cf89488..fffa0ff24 100644 --- a/README.md +++ b/README.md @@ -170,11 +170,11 @@ To undo it, remove the entries in Windows Security → App & browser control → | From | To | |---|---| | **Images**: JPG, PNG, WEBP, AVIF, TIFF, BMP, GIF, APNG, ICO, ICNS, JPEG XL, JPEG 2000, PSD/PSB, TGA, PPM, HEIC, QOI, DDS, PCX, MNG, CUR, …;
camera RAW (CR2, CR3, NEF, ARW, DNG, …), DICOM, OpenEXR, HDR, FITS, JPEG XR (Windows), iPhone Live Photo (LIVP), Insta360 INSP, Windows program icons (EXE, DLL), Valve Texture VTF (Source engine, Garry's Mod), animated cursor (ANI) read;
GIMP XCF and Paint.NET PDN (layers kept between XCF, PDN and PSD) | any of these (HEIC⁵), GIMP XCF, Paint.NET PDN, VTF, PDF, SVG (traced), resized, grayscale;
Live/Motion Photos (iPhone, Android) → their video (MP4, MOV⁵), and a video → Live Photo (iPhone) or Motion Photo (Android)⁵, experimental | -| **Vector**: SVG, SVGZ, EMF, WMF, AI, Visio VSDX, ODG, FODG
EPS, PS¹
CDR, binary Visio VSD, CGM²
Obsidian Canvas (JSON Canvas) and Excel chart templates (CRTX) read | PDF (vector), PNG and other images,
SVG ↔ SVGZ, SVG → DXF/DWG, PDF → EPS/PS, SVG → EMF, WMF, ODG²;
Obsidian Canvas → its text as Markdown | +| **Vector**: SVG, SVGZ, EMF, WMF, AI, Visio VSDX, ODG, OTG, FODG
EPS, PS¹
CDR, binary Visio VSD, CGM²
Obsidian Canvas (JSON Canvas) and Excel chart templates (CRTX) read | PDF (vector), PNG and other images,
SVG ↔ SVGZ, SVG → DXF/DWG, PDF → EPS/PS, ODG ↔ OTG ↔ FODG, SVG → EMF, WMF, ODG²;
Obsidian Canvas → its text as Markdown | | **PDF** | DOCX, XLSX (tables), HWPX, TXT, PNG, JPG, TIFF, CBZ, PPTX (editable slides, or a picture of each page), SVG², PDF/A¹;
merge (in an order you choose first), split (by pages, every N pages or into equal parts), page ranges, compress (smaller PDFs) | -| **Documents**: DOCX, DOCM, DOTX, DOC, DOT, ODT, EML, Outlook MSG;
OFD (Chinese e-invoices, official documents), UYAP UDF (Turkish court documents), XPS / OpenXPS and FreeMind / Freeplane / XMind mind maps (as outlines) read
FODT, OTT, RTF, WPS, WPD, Pages, ClarisWorks, MacWrite, T602, …² | PDF, DOCX, ODT, HWPX, EPUB, TXT, HTML, PPTX and ODP slides;
DOCX, HTML tables → XLSX, ODS, CSV;
MSG → EML
FODT, OTT² | -| **Spreadsheets**: XLSX, XLSM, XLSB, XLS, XLT, ODS, CSV, TSV
OTS, FODS, Numbers, ET² | PDF, XLSX, ODS, CSV, TSV, JSON, HWPX, HTML
XLT, OTS² | -| **Presentations**: PPTX, PPTM, POTX, POTM, PPSX, PPT, ODP, OTP, FODP
PowerPoint 95 PPT, Keynote² | PDF, PNG, JPG, ODP, DOCX, HWPX; PPT → PPTX
OTP² | +| **Documents**: DOCX, DOCM, DOTX, DOC, DOT, ODT, OTT, FODT, EML, Outlook MSG;
OFD (Chinese e-invoices, official documents), UYAP UDF (Turkish court documents), XPS / OpenXPS and FreeMind / Freeplane / XMind mind maps (as outlines) read
RTF, WPS, WPD, Pages, ClarisWorks, MacWrite, T602, …² | PDF, DOCX, ODT, OTT, FODT, HWPX, EPUB, TXT, HTML, PPTX and ODP slides;
DOCX, HTML tables → XLSX, ODS, CSV;
MSG → EML | +| **Spreadsheets**: XLSX, XLSM, XLSB, XLS, XLT, ODS, OTS, FODS, CSV, TSV
Numbers, ET² | PDF, XLSX, ODS, OTS, FODS, CSV, TSV, JSON, HWPX, HTML
XLT² | +| **Presentations**: PPTX, PPTM, POTX, POTM, PPSX, PPT, ODP, OTP, FODP
PowerPoint 95 PPT, Keynote² | PDF, PNG, JPG, ODP, OTP, FODP, DOCX, HWPX; PPT → PPTX | | **HWP**: HWP, HWPX | PDF, PNG, DOCX, HWPX ↔ HWP, TXT, Markdown, HTML, PPTX and ODP slides | | **Text**: TXT, LOG, script source (PY, PAC, BAT, COMMAND; read, never run), Arduino and Processing sketches (INO, PDE), Markdown, HTML;
MHTML (saved web pages) read
Jupyter, reStructuredText, Org, MediaWiki, DocBook, Textile, OPML
LaTeX, AsciiDoc, Typst, man³ | UTF-8 TXT, PDF, DOCX, HWPX, EPUB, PPTX and ODP slides (a slide per heading)
each other, PPTX and ODP slides³ | | **E-books**: EPUB, KEPUB, MOBI, AZW3, AZW, AZW4, FB2, CBZ, CBR, CB7, HTMLZ, TXTZ, CHM, PDB, PML, TCR, RB, SNB, OEB
LIT, LRF⁴ | PDF, EPUB, KEPUB (Kobo), DOCX, TXT, HTML, HWPX
MOBI, AZW3 | diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 8046fdbd1..9c549d851 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -14,35 +14,36 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | **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)) | | 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, CSV, TSV → XLSX, ODS, CSV, TSV** (one CSV / TSV per sheet) | -| **Spreadsheet printing** | built in | **XLSX (+ XLSM/XLTX), XLSB, XLS (+ XLT), ODS → PDF** printed the way Excel prints them: page setup, print areas and titles, scaling and fit to pages, page breaks, headers and footers (see [Spreadsheet printing](#spreadsheet-printing)) | +| 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) | +| **Spreadsheet printing** | built in | **XLSX (+ XLSM/XLTX), XLSB, XLS (+ XLT), ODS (+ OTS, FODS) → PDF** printed the way Excel prints them: page setup, print areas and titles, scaling and fit to pages, page breaks, headers and footers (see [Spreadsheet printing](#spreadsheet-printing)) | | Office Open XML | built in | **DOCM / DOTX / DOTM ↔ DOCX, XLSM / XLTX ↔ XLSX, PPTM / POTX / POTM / PPSX ↔ PPTX** (macros removed for macro-free types) | +| **OpenDocument templates and flat files** | built in | **ODT ↔ OTT ↔ FODT, ODS ↔ OTS ↔ FODS, ODP ↔ OTP ↔ FODP, ODG ↔ OTG ↔ FODG** without reading the document into a model, so nothing is lost (see [OpenDocument templates and flat files](#opendocument-templates-and-flat-files)); after the ODT writer and the spreadsheet engine, every document → OTT / FODT and every workbook → OTS / FODS | | Fonts | built in | **TTF, OTF, WOFF, WOFF2, EOT ↔ each other**; **FON (NE monochrome FNT v2/v3, supported single-byte charsets) → TTF, OTF, WOFF, WOFF2, EOT only**; CFF (PostScript) outlines become TrueType for TTF and EOT | -| **HWPX writer** | built in | **HWP, DOCX (+ DOCM/DOTX/DOTM), DOC (+ DOT, WPS Office WPS), ODT, XLSX (+ XLSM/XLTX), XLS, ODS, CSV, TSV, PPTX (+ PPTM/POTX/POTM/PPSX), PPT, ODP (+ OTP, FODP), PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2, CHM, PDB, PML, TCR, RB, SNB, OEB, HWPX, TXT, Markdown, RTF, mind maps (MM, XMind) → HWPX** (and with rhwp → PDF and images); reStructuredText, LaTeX → HWPX with Pandoc | -| **DOCX writer** | built in | **DOC (+ DOT, WPS Office WPS), ODT, HWPX, HWP, PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), Markdown, TXT, RTF, XLSX, XLS, ODS, CSV, PPTX (+ variants), PPT, ODP (+ OTP, FODP), mind maps (MM, XMind) → DOCX** | -| **ODT writer** | built in | **DOCX (+ variants), DOC (+ DOT, WPS), HWPX, HWP, PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), Markdown, TXT, RTF, XLSX, XLS, ODS, CSV, PPTX (+ variants), PPT, ODP (+ OTP, FODP), mind maps (MM, XMind) → ODT** (OpenDocument text, LibreOffice Writer) | -| **PPTX writer** | built in | **Markdown, DOCX (+ variants), DOC (+ DOT, WPS), ODT, HWPX, HWP, OFD, HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), TXT, RTF, XLSX, XLS, ODS, CSV → PPTX slides** (a slide per heading, Pandoc's rules; Pandoc's markup languages too when Pandoc is installed); **PPT → PPTX** as the [PPT reader](#ppt-reader) translates it | -| **ODP writer** | built in | **PPTX (+ PPTM/POTX/POTM/PPSX), PPT → ODP** with masters, shapes, pictures, tables, lists and speaker notes; **Markdown, DOCX (+ variants), DOC (+ DOT, WPS), ODT, HWPX, HWP, OFD, HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), TXT, RTF, XLSX, XLS, ODS, CSV → ODP slides** laid out as the PPTX writer lays them out (see [ODP writer](#odp-writer)); a deck with what the slide model leaves out (charts without a picture, effects, 3D) keeps LibreOffice's route | +| **HWPX writer** | built in | **HWP, DOCX (+ DOCM/DOTX/DOTM), DOC (+ DOT, WPS Office WPS), ODT (+ OTT, FODT), XLSX (+ XLSM/XLTX), XLS, ODS (+ OTS, FODS), CSV, TSV, PPTX (+ PPTM/POTX/POTM/PPSX), PPT, ODP (+ OTP, FODP), PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2, CHM, PDB, PML, TCR, RB, SNB, OEB, HWPX, TXT, Markdown, RTF, mind maps (MM, XMind) → HWPX** (and with rhwp → PDF and images); reStructuredText, LaTeX → HWPX with Pandoc | +| **DOCX writer** | built in | **DOC (+ DOT, WPS Office WPS), ODT (+ OTT, FODT), HWPX, HWP, PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), Markdown, TXT, RTF, XLSX, XLS, ODS (+ OTS, FODS), CSV, PPTX (+ variants), PPT, ODP (+ OTP, FODP), mind maps (MM, XMind) → DOCX** | +| **ODT writer** | built in | **DOCX (+ variants), DOC (+ DOT, WPS), HWPX, HWP, PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), Markdown, TXT, RTF, XLSX, XLS, ODS (+ OTS, FODS), CSV, PPTX (+ variants), PPT, ODP (+ OTP, FODP), mind maps (MM, XMind) → ODT** (OpenDocument text, LibreOffice Writer) | +| **PPTX writer** | built in | **Markdown, DOCX (+ variants), DOC (+ DOT, WPS), ODT (+ OTT, FODT), HWPX, HWP, OFD, HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), TXT, RTF, XLSX, XLS, ODS (+ OTS, FODS), CSV → PPTX slides** (a slide per heading, Pandoc's rules; Pandoc's markup languages too when Pandoc is installed); **PPT → PPTX** as the [PPT reader](#ppt-reader) translates it | +| **ODP writer** | built in | **PPTX (+ PPTM/POTX/POTM/PPSX), PPT → ODP, OTP, FODP** with masters, shapes, pictures, tables, lists and speaker notes; **Markdown, DOCX (+ variants), DOC (+ DOT, WPS), ODT (+ OTT, FODT), HWPX, HWP, OFD, HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), TXT, RTF, XLSX, XLS, ODS, CSV → ODP slides** laid out as the PPTX writer lays them out (see [ODP writer](#odp-writer)); a deck with what the slide model leaves out (charts without a picture, effects, 3D) keeps LibreOffice's route | | PDF text | built in (PdfPig) | **PDF → TXT, Markdown** (reading order, also two columns; Markdown keeps headings, emphasis, links and the pictures; no OCR) | | PDF slides | built in (PdfPig, PDFium) | **PDF → PPTX**: each page rebuilt as an editable slide (text boxes with the PDF's fonts, pictures, shapes, ruled tables); or, as a preset option, a picture of each page with its text in the speaker notes | | PDF tables | built in (PdfPig) | **PDF → XLSX, ODS, CSV** (#267): each table drawn with lines on its own sheet, numbers and dates as real values, merged cells and column widths kept | | **PostScript writer** | built in | **PDF → PS (LanguageLevel 3), EPS** (one per page) without Ghostscript: vectors, text with its embedded fonts, pictures, shadings, patterns; pages with transparency as pictures (see [PDF → PostScript and EPS](#pdf--postscript-and-eps)) | -| Document tables | built in | **DOCX (+ DOCM/DOTX/DOTM), DOC (+ DOT/WPS), ODT, RTF, HWP, HWPX, HTML, MHTML → XLSX, ODS, CSV** (#1016): each table on its own sheet, numbers and dates as real values, merged cells, borders, fill, alignment and column widths kept | +| Document tables | built in | **DOCX (+ DOCM/DOTX/DOTM), DOC (+ DOT/WPS), ODT (+ OTT, FODT), RTF, HWP, HWPX, HTML, MHTML → XLSX, ODS, CSV** (#1016): each table on its own sheet, numbers and dates as real values, merged cells, borders, fill, alignment and column widths kept | | E-mail | built in (MimeKit, MsgReader) | **EML, Outlook MSG → HTML** (headers + body, inline pictures), **TXT**, **ZIP** (the attachments); **MSG → EML**; **MHTML → HTML** (the saved page in one file, pictures inlined; on to PDF, DOCX, HWPX, TXT, Markdown, EPUB from there) | | Signed files | built in (System.Security.Cryptography.Pkcs) | **P7M → the signed file inside** (CMS / PKCS #7, CAdES: Italian e-invoices `*.xml.p7m`, signed PDFs `*.pdf.p7m`), then on from its format (P7M → PDF → JPG); the signature isn't checked | | 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, HWPX (→ PDF), TXT, HTML, Markdown, FB2, HTMLZ, TXTZ**; HTML, Markdown, TXT, DOCX → EPUB/AZW3/MOBI/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, 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); a binary `.vsd` and a drawing with what the readers don't draw yet (charts, OLE objects, 3D) keep LibreOffice's route | +| **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); a binary `.vsd` and a drawing with what the readers don't draw yet (charts, OLE objects, 3D) keep LibreOffice's route | | 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) | | 3D models (SharpGLTF for glTF) | built in (library) | **GLB, glTF, STL, OBJ (+ MTL), 3MF, PLY ↔ each other**; FBX → each of them; → PNG, JPG, … (an isometric preview) | | Pictures to 3D relief | built in | **PNG, JPG, WEBP, … (every picture ImageMagick reads) → GLB, glTF, STL, OBJ, 3MF, PLY**: a relief, brighter areas higher | | **Geospatial vector data** | built in | **GeoJSON, KML, KMZ, GPX, Shapefile ↔ each other and CSV** (longitude / latitude or WKT); Garmin FIT (activities) and OpenStreetMap XML (`.osm`), both read only, → each of them and CSV; projected Shapefiles (UTM, Web Mercator, Lambert, Albers) are converted to WGS 84; through CSV to XLSX, JSON, … (see below) | -| **Filee renderer** | built in, on by default (Settings → Engines → *Use the Filee renderer*) | **HWPX, HWP, Word documents (DOCX and DOC), ODT, RTF, TXT, Markdown, HTML, e-books, CSV / TSV, OFD, UYAP (UDF), the markup languages Filee reads itself (and Pandoc's formats), mind maps as outlines → PDF** with Filee's own layout engine; **PPTX, PPTM, POTX, POTM, PPSX, PPT ([PPT reader](#ppt-reader)), ODP, OTP, FODP ([ODP / ODG reader](#odp--odg-reader)) → PDF, PNG, JPG** with its slide renderer; files with what it doesn't draw yet take the HWPX writer and rhwp (see [Filee renderer: PDF output](#filee-renderer-pdf-output) and [slides](#filee-renderer-slides)) | +| **Filee renderer** | built in, on by default (Settings → Engines → *Use the Filee renderer*) | **HWPX, HWP, Word documents (DOCX and DOC), ODT (+ OTT, FODT), RTF, TXT, Markdown, HTML, e-books, CSV / TSV, OFD, UYAP (UDF), the markup languages Filee reads itself (and Pandoc's formats), mind maps as outlines → PDF** with Filee's own layout engine; **PPTX, PPTM, POTX, POTM, PPSX, PPT ([PPT reader](#ppt-reader)), ODP, OTP, FODP ([ODP / ODG reader](#odp--odg-reader)) → PDF, PNG, JPG** with its slide renderer; files with what it doesn't draw yet take the HWPX writer and rhwp (see [Filee renderer: PDF output](#filee-renderer-pdf-output) and [slides](#filee-renderer-slides)) | | rhwp | bundled with the installer (`engines/rhwp`, with the Microsoft C++ runtime, see below) | HWP/HWPX → PDF when the Filee renderer is off or can't draw a file yet, **HWPX → HWP** (HWP → HWPX too, ranked below the built-in HWP reader) | | Archives | built in (SharpCompress, DiscUtils, .NET and Filee's own readers and writers); 7-Zip optional (Settings → *Engines*, ~2 MB download) | **in-process**: ZIP (including ANSYS Workbench WBPZ and Adobe ZXP, and Windows app packages MSIX / APPX), Minecraft and Modrinth packs, Garry's Mod addons (GMA), Valve paks (VPK), the files packed into Source maps (BSP), 7Z (solid ones too), RAR, TAR (+ GZ/BZ2/XZ/Z/7Z/LZ/ZST), GZ, BZ2, XZ, LZMA, Z, ZST, lzip, CPIO, DEB, RPM, ISO (UDF discs too), IMG and DMG disk images, split (.001), ALZ, EGG, CAB, LHA, ARJ; **7-Zip, when installed**: the rare variants the in-process readers leave to it (see [Archives](#archives)) **→ folder, ZIP, 7Z, TAR, TAR.GZ, TAR.BZ2, TAR.XZ, BZ2, XZ, the packs, GMA** (in-process); "Compress into one archive" for any files | -| LibreOffice | **optional download** (~370 MB, 1.1 GB on disk) | older and rare formats only: Word 6.0 / 95 and Works documents, PowerPoint 95 and older PPT, binary Visio (`.vsd`), and ODP / ODG with what Filee doesn't draw yet (charts, OLE objects, 3D) → PDF; XLS, PPT, RTF and OpenDocument to each other, output as DOCX/ODT/ODS, and ODP for what the [ODP writer](#odp-writer) doesn't take (OpenDocument presentations themselves, Keynote, decks with charts, effects or 3D); read-only formats (see below) → PDF and their kind's editable formats; FODT, OpenDocument templates (OTT, OTS, OTP) and XLT out; SVG → EMF/WMF/ODG, a PDF page → SVG | +| LibreOffice | **optional download** (~370 MB, 1.1 GB on disk) | older and rare formats only: Word 6.0 / 95 and Works documents, PowerPoint 95 and older PPT, binary Visio (`.vsd`), and ODP / ODG with what Filee doesn't draw yet (charts, OLE objects, 3D) → PDF; XLS, PPT, RTF and OpenDocument to each other, output as DOCX/ODT/ODS, and ODP for what the [ODP writer](#odp-writer) doesn't take (OpenDocument presentations themselves, Keynote, decks with charts, effects or 3D); read-only formats (see below) → PDF and their kind's editable formats; XLT out; SVG → EMF/WMF/ODG, a PDF page → SVG | | Pandoc | **optional download** (~42 MB, 240 MB on disk) | Markdown ↔ DOCX/ODT/RTF, DOCX/ODT/HTML/RTF → Markdown, HTML ↔ DOCX/ODT, markup languages (LaTeX, AsciiDoc, Typst, man; and RTF / EPUB from the built-in ones) ↔ Markdown/HTML/DOCX/ODT and each other (and → RTF, EPUB, TXT); their AST feeds the built-in PPTX writer (Pandoc's own slides only where Filee assumes a Pandoc that isn't installed) | | Ghostscript | **optional download** (~20 MB, 31 MB on disk) | EPS/PS → PDF, PostScript-based AI → PDF (images through PDF and PDFium), EPS/PS → PNG; PDF → EPS/PS only when moved above the built-in PostScript writer; smaller PDFs when a preset asks for it; PDF/A-2b for PDF/A presets; renders CMYK PDF/AI pages for CMYK JPG/TIFF | | Tesseract | **optional download** (~50 MB, 160 MB on disk; each language 0.4–10 MB when first used) | **text recognition (OCR)**: scans and pictures → searchable PDF, and their text into DOCX, TXT, Markdown and HWPX ("Recognize text" in the preset, see below) | @@ -1760,7 +1761,10 @@ so it wins over Pandoc and LibreOffice. ODT → PDF goes through the Filee rende Not yet: numbering restarts in the middle of a list (only an item's start value on the first item counts), outline numbering of headings (`text:outline-style`), different first-page headers with content, gradients (a gradient fills with its start colour), rotated or cropped pictures, charts and other OLE objects, drop caps, change tracking, fields other -than page number / count (their text is kept), flat ODT (`.fodt`) and templates (`.ott`, still LibreOffice's). +than page number / count (their text is kept). + +Templates (`.ott`) and flat files (`.fodt`) are read by the same reader and written from its ODT, see +[OpenDocument templates and flat files](#opendocument-templates-and-flat-files). ## DOC reader @@ -1909,7 +1913,7 @@ Checked against decks built from the specification, not against files PowerPoint ## ODP / ODG reader -OpenDocument presentations (`.odp`, templates `.otp`, flat `.fodp`) and drawings (`.odg`, flat `.fodg`) are read +OpenDocument presentations (`.odp`, templates `.otp`, flat `.fodp`) and drawings (`.odg`, templates `.otg`, flat `.fodg`) are read in-process into the slide model, written from OpenDocument 1.3 Part 3 (`Odf/Draw`), without LibreOffice: presentations to PDF, PNG and JPG through the [slide renderer](#filee-renderer-slides) (the Filee renderer, `Slides/SlideFormats.cs`), drawings to PDF, SVG and PNG through the [drawing engine](#drawings-visio-odg). @@ -2065,8 +2069,10 @@ same model, which the tests check shape by shape and as pictures. columns and turned text (`draw:text-rotate-angle`). - **Routes.** Its edges cost 8, as the PPTX writer's, below LibreOffice's fallback (25), and are `DirectOnly`. Deck edges are checked with the renderer's capability check (charts without a picture, effects, 3D, WordArt, …), so such - a deck keeps LibreOffice's route where it is installed. OpenDocument presentations (ODP, OTP, FODP) are not - sources: ODP → PPTX and the template formats stay with LibreOffice. + a deck keeps LibreOffice's route where it is installed. Every source also goes to OTP and FODP: the writer + writes the ODP and turns it into the template or the flat file (`Odf/OdfVariants.cs`), since a `DirectOnly` + edge can't lead on. OpenDocument presentations (ODP, OTP, FODP) are not sources: they become each other through + the [forms engine](#opendocument-templates-and-flat-files), and ODP → PPTX stays with LibreOffice. - **Hostile input:** the model comes from the bounded readers; the writer caps the XML it makes (512 million characters), group nesting (`MaxNestingDepth`) and the pictures' bytes (`MaxPackageBytes`), refused with `error.document_too_complex`; non-finite boxes and turns are left out, coordinates cut at ±10 km, characters XML @@ -2077,7 +2083,7 @@ same model, which the tests check shape by shape and as pictures. Not yet: shapes as placeholders (`presentation:class`; the model doesn't say which shapes were), the preset name and adjust handles of a shape (its outline is written, not the formula), shadows and other effects, charts, SmartArt as -anything but its drawing, animations and transitions, compound lines, ODG output, OTP and FODP output. Checked against +anything but its drawing, animations and transitions, compound lines, ODG output. Checked against Filee's own reader; not yet opened in LibreOffice by the tests. ## PDF reader @@ -2627,6 +2633,50 @@ file became and once on a merged PDF, so the box means the same whichever engine are converted and the PDF is left as it is, with `notice.pdfa_needs_ghostscript` in the result, unless LibreOffice made it PDF/A. +## OpenDocument templates and flat files + +An OpenDocument type comes in three forms (OpenDocument 1.3 Part 2 and Part 3): the package (`.odt`, `.ods`, `.odp`, +`.odg`), the template (`.ott`, `.ots`, `.otp`, `.otg`: the same package with the `-template` media type) and the flat +XML file (`.fodt`, `.fods`, `.fodp`, `.fodg`: one `office:document` with meta, settings, scripts, font faces, styles, +automatic styles, master styles and body, pictures as base64 `office:binary-data` and embedded objects as +`office:document`s inside `draw:object`). `OdfVariantConverter` (engine `odf-variants`, cost 2) turns each form into +the others of its type without LibreOffice and without reading the document into a model (`Odf/OdfVariants.cs`, +`Odf/OdfFlat.cs`), so macros, settings, thumbnails and everything the readers don't know stay: + +- **Package ↔ template:** every part copied as it is through `DocumentPackage`, with the new media type in `mimetype` + (first, stored) and on the manifest's root entry. +- **Package → flat file:** the parts put together; the automatic styles of `styles.xml` that have the name of one in + `content.xml` are renamed (with the references in its automatic and master styles) so both fit in one element; each + picture and OLE object an `xlink:href` names inside the package becomes `office:binary-data`, each object folder + (`Object 1/`, a chart) an `office:document` with the media type from the manifest. +- **Flat file → package:** the reverse: `content.xml`, `styles.xml` (both with the automatic styles and font faces), + `meta.xml`, `settings.xml`, a part under `Pictures/` per picture (one for a picture stored more than once, named by + its signature: PNG, JPEG, GIF, BMP, TIFF, WebP, EMF, WMF, SVG), an `Object N/` folder per embedded document, and the + manifest. Pictures that aren't base64 are left out, as the readers leave them out. + +The readers read every form: the ODT reader takes `.ott`, the spreadsheet reader `.ots`, the ODP / ODG reader the +templates and flat files itself; a `.fodt` or `.fods` is read as the package it splits into, in a temporary file +(`OdfVariants.AsPackage`), so FODT / OTT go to PDF through the Filee renderer, to DOCX, HWPX, PPTX, ODP and tables +through the writers, and FODS / OTS to PDF through sheet printing and to XLSX / CSV / TSV. The renderer and sheet +printing check a flat file as that package. Out: DOCX → ODT (ODT writer) → OTT / FODT, XLSX → ODS (spreadsheet engine) +→ OTS / FODS; the ODP writer writes OTP and FODP itself. All of it costs far less than LibreOffice's fallback, whose +FODT and template edges stay for the formats only it reads. + +- **Hostile input:** a flat file is read through `SafeXml` within `DocumentLimits`' XML depth and element counts and + at most four times what a package part may hold (256 MB, as the ODP / ODG reader reads one); a picture decodes to at + most a part (64 MB), all pictures to at most the package size, in at most as many parts as a package may have, + objects at most `MaxObjects` and eight deep. A package is read through `DocumentPackage` (part, package and inflate + limits); the flat file made from it may hold at most what a flat file may be read with, so a picture used again and + again can't make it grow without end. Each refusal is `error.document_too_complex`; another type's media type, + damaged XML and encrypted packages are refused with their reason. +- Tests: `OdfVariantTests` (packages built in code by `OdtBuilder` / `OdpBuilder` / the ODS writer: round trips with + text, pictures, styles and a chart object, the readers, routes, DOCX and PPTX conversions, hostile flat files and + packages) and `OdfVariantFuzzTests` (mutated flat texts and spreadsheets, mutated packages turned flat). + +Not yet: a flat file's `office:binary-data` in elements other than pictures, fill images, background images, list +images and OLE objects is dropped; a flat file is read as its document even when it says it is a template. Checked +against Filee's own readers; not yet opened in LibreOffice by the tests. + ## Office Open XML variants `Office/OoxmlConverter.cs` converts between the variants of a family (Word, Excel, PowerPoint) by copying the @@ -2857,7 +2907,7 @@ its reader, one line in `Drawings/DrawingFormats.cs`: rows, groups, connectors (the geometry they carry) and pictures (Foreign shapes: PNG, JPEG, GIF, BMP, TIFF, EMF, WMF). Cells, geometry and text are inherited from master shapes and style sheets; colours by index come from the document's colour table, fonts from its face names. Only stored values (`V`) are read; formulas aren't calculated. -- **ODG and FODG** (OpenDocument drawings, packaged and flat): the [ODP / ODG reader](#odp--odg-reader). +- **ODG, OTG and FODG** (OpenDocument drawings, packaged, templates and flat): the [ODP / ODG reader](#odp--odg-reader). - **XPS and OpenXPS** (`.xps`, `.oxps`, format id `xps`): not slides; the [XPS reader](#xps-and-openxps-reader) draws its pages straight into the page model (`DrawingFormat.ReadPages`, `IFixedPages`). - **Excel chart templates** (`.crtx`, format id `crtx`, #1354): `ChartTemplate`, one page of Excel's default chart size @@ -3959,19 +4009,19 @@ all expected bytes have been written; failures and cancellation preserve any pre - It is the only reader of rarer formats, converted to PDF and to the editable formats of their kind: Word 6.0 / 95 and Microsoft Works documents (a `.doc`, `.dot` or `.wps` the [DOC reader](#doc-reader) names as such), WordPerfect, Lotus Word Pro, AbiWord, Apple Pages, StarWriter, ClarisWorks / AppleWorks (`.cwk`), MacWrite (`.mcw`), T602 - (`.602`) and Pocket Word (`.psw`) (→ DOCX, ODT, FODT, OTT, DOC, RTF, TXT, HTML); WPS Spreadsheets (ET), Apple Numbers, StarCalc and flat ODS (`.fods`) (→ XLSX, ODS, OTS, XLS, XLT, CSV, HTML); XLSB too, where the built-in reader has no route (→ XLS, XLT, OTS) and for a workbook with what the print engine doesn't draw; Apple + (`.602`) and Pocket Word (`.psw`) (→ DOCX, ODT, DOC, RTF, TXT, HTML, and on to OTT and FODT); WPS Spreadsheets (ET), Apple Numbers and StarCalc (→ XLSX, ODS, XLS, XLT, CSV, HTML, and on to OTS and FODS); XLSB too, where the built-in reader has no route (→ XLS, XLT) and for a workbook with what the print engine doesn't draw; Apple Keynote, WPS Presentation (DPS), StarImpress / StarDraw and PowerPoint 95 and older PPT (→ PPTX, ODP, OTP, PPT); - Publisher, CorelDRAW, binary Visio (`.vsd`) and CGM (→ ODG, SVG, PNG, EMF, WMF). VSDX, ODP, FODP, ODG and FODG - are read by Filee itself ([Drawings](#drawings-visio-odg), [ODP / ODG reader](#odp--odg-reader)); LibreOffice keeps + Publisher, CorelDRAW, binary Visio (`.vsd`) and CGM (→ ODG, SVG, PNG, EMF, WMF). VSDX, ODP, OTP, FODP, ODG, OTG and FODG + are read by Filee itself (FODT, OTT, FODS and OTS too, by the text and spreadsheet readers) ([Drawings](#drawings-visio-odg), [ODP / ODG reader](#odp--odg-reader)); LibreOffice keeps them when their check names what Filee doesn't draw yet (charts, OLE objects, 3D), and converts them to the editable formats (ODP → PPTX, ODG → EMF, …). The `--convert-to` filter follows the application that opens the file (checked against LibreOffice's filter registry): Publisher, CorelDRAW and Visio open in Draw, CGM in Impress, and "SDA" files in Draw (.sda) or Impress (.sdd). ClarisWorks files can also be spreadsheets or drawings; Filee treats them as text documents, the common case. SVG, PNG, EMF and WMF export the first page. -- Written only by LibreOffice: FODT (`OpenDocument Text Flat XML`), the OpenDocument templates OTT / OTS / OTP - (`writer8_template`, `calc8_template`, `impress8_template`) and XLT (`MS Excel 97 Vorlage/Template`). XLT is read - by the built-in spreadsheet engine like XLS. +- Written only by LibreOffice: XLT (`MS Excel 97 Vorlage/Template`), read by the built-in spreadsheet engine like + XLS. Its FODT and template filters (`OpenDocument Text Flat XML`, `writer8_template`, `calc8_template`, + `impress8_template`) stay as edges, but the [forms engine](#opendocument-templates-and-flat-files) costs less. - SVG opens in Draw only for EMF, WMF and ODG; the vector engine keeps SVG → PDF and images. PDF opens in Draw (`draw_pdf_import`) only for SVG and ODG: Draw's SVG export draws every page of a PDF on top of each other, so Filee first copies the first page of the preset's page range into a PDF of its own (PDFsharp). EPS and PS reach SVG diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index 111ab5f92..51b44f9d8 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "ينشئ شرائح PowerPoint ‏(PPTX) من ملفات Markdown ولغات الترميز ومستندات Word وODT وHWP/HWPX وHTML وجداول البيانات والكتب الإلكترونية: شريحة لكل عنوان، مع القوائم والجداول والصور والتعليمات البرمجية.", "engine.odp-writer.name": "كاتب ODP (مدمج)", "engine.odp-writer.description": "ينشئ عروض OpenDocument التقديمية ‏(ODP) دون LibreOffice: شرائح PowerPoint ‏(PPTX وPPT) مع أشكالها وصورها وجداولها وملاحظات المتحدث، وملفات Markdown ولغات الترميز ومستندات Word وODT وHWP/HWPX وHTML وجداول البيانات والكتب الإلكترونية بشريحة لكل عنوان.", + "engine.odf-variants.name": "قوالب OpenDocument وملفات XML المسطحة (مدمج)", + "engine.odf-variants.description": "يحوّل ملفات OpenDocument إلى قوالبها ‏(OTT وOTS وOTP وOTG) وملفات XML المسطحة ‏(FODT وFODS وFODP وFODG) والعكس دون LibreOffice، مع الحفاظ على المستند كاملًا.", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "استخراج نصوص PDF بترتيب القراءة، بما في ذلك الصفحات ذات العمودين (دون تعرّف ضوئي OCR للمستندات الممسوحة).", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/bn-BD.json b/src/Filee.App/Assets/i18n/bn-BD.json index 11ea3129b..a04002702 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -307,6 +307,8 @@ "engine.pptx-writer.description": "মার্কডাউন ও মার্কআপ ফাইল, ওয়ার্ড ডকুমেন্ট, ODT, HWP/HWPX, এইচটিএমএল, স্প্রেডশিট এবং ই-বুক থেকে পাওয়ারপয়েন্ট স্লাইড (PPTX) তৈরি করে: প্রতিটি শিরোনামের জন্য একটি স্লাইড, তালিকা, সারণি, ছবি ও কোডসহ।", "engine.odp-writer.name": "ODP রাইটার (বিল্ট-ইন)", "engine.odp-writer.description": "লিব্রেঅফিস ছাড়াই ওপেনডকুমেন্ট উপস্থাপনা (ODP) তৈরি করে: পাওয়ারপয়েন্ট স্লাইড (PPTX, PPT) তাদের আকৃতি, ছবি, সারণি ও বক্তার নোটসহ, এবং মার্কডাউন ও মার্কআপ ফাইল, ওয়ার্ড ডকুমেন্ট, ODT, HWP/HWPX, এইচটিএমএল, স্প্রেডশিট ও ই-বুক থেকে প্রতিটি শিরোনামের জন্য একটি স্লাইড।", + "engine.odf-variants.name": "ওপেনডকুমেন্ট টেমপ্লেট ও ফ্ল্যাট XML ফাইল (বিল্ট-ইন)", + "engine.odf-variants.description": "লিব্রেঅফিস ছাড়াই ওপেনডকুমেন্ট ফাইলকে তার টেমপ্লেট (OTT, OTS, OTP, OTG) ও ফ্ল্যাট XML ফাইলে (FODT, FODS, FODP, FODG) এবং আবার আগের রূপে বদলায়, পুরো ডকুমেন্ট অক্ষত রেখে।", "engine.pdf-text.name": "পিডিএফ টেক্সট (বিল্ট-ইন)", "engine.pdf-text.description": "পড়ার ক্রম অনুযায়ী পিডিএফ-এর টেক্সট নিষ্কাশন করে, দুই কলামের পৃষ্ঠা থেকেও (স্ক্যান করা ফাইলের জন্য OCR নয়)।", "engine.pdf-slides.name": "পিডিএফ স্লাইড (বিল্ট-ইন)", diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.json index 31d8f9826..f2454c097 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "Erstellt PowerPoint-Folien (PPTX) aus Markdown- und Markup-Dateien, Word-Dokumenten, ODT, HWP/HWPX, HTML, Tabellen und E-Books: eine Folie pro Überschrift, mit Listen, Tabellen, Bildern und Code.", "engine.odp-writer.name": "ODP-Writer (integriert)", "engine.odp-writer.description": "Erstellt OpenDocument-Präsentationen (ODP) ohne LibreOffice: PowerPoint-Folien (PPTX, PPT) mit Formen, Bildern, Tabellen und Notizen sowie Markdown- und Markup-Dateien, Word-Dokumente, ODT, HWP/HWPX, HTML, Tabellen und E-Books mit einer Folie pro Überschrift.", + "engine.odf-variants.name": "OpenDocument-Vorlagen und Flat-XML-Dateien (integriert)", + "engine.odf-variants.description": "Wandelt OpenDocument-Dateien ohne LibreOffice in ihre Vorlagen (OTT, OTS, OTP, OTG) und Flat-XML-Dateien (FODT, FODS, FODP, FODG) um und zurück; das ganze Dokument bleibt erhalten.", "engine.pdf-text.name": "PDF-Text (integriert)", "engine.pdf-text.description": "Extrahiert PDF-Text in Lesereihenfolge, auch aus zweispaltigen Seiten (keine Texterkennung für Scans).", "engine.pdf-slides.name": "PDF-Folien (integriert)", diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json index 98fb4b38c..2ea123fb6 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "Makes PowerPoint slides (PPTX) from Markdown and markup files, Word documents, ODT, HWP/HWPX, HTML, spreadsheets and e-books: a slide per heading, with lists, tables, pictures and code.", "engine.odp-writer.name": "ODP writer (built-in)", "engine.odp-writer.description": "Makes OpenDocument presentations (ODP) without LibreOffice: PowerPoint decks (PPTX, PPT) with their shapes, pictures, tables and speaker notes, and Markdown and markup files, Word documents, ODT, HWP/HWPX, HTML, spreadsheets and e-books with a slide per heading.", + "engine.odf-variants.name": "OpenDocument templates and flat files (built-in)", + "engine.odf-variants.description": "Turns OpenDocument files into their templates (OTT, OTS, OTP, OTG) and flat XML files (FODT, FODS, FODP, FODG) and back without LibreOffice, keeping the whole document.", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "Extracts the text of PDFs in reading order, also from two-column pages (no OCR for scans).", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.json index f52142523..9f3635727 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "Crea diapositivas de PowerPoint (PPTX) a partir de archivos Markdown y de marcado, documentos de Word, ODT, HWP/HWPX, HTML, hojas de cálculo y libros electrónicos: una diapositiva por encabezado, con listas, tablas, imágenes y código.", "engine.odp-writer.name": "Generador ODP (integrado)", "engine.odp-writer.description": "Crea presentaciones OpenDocument (ODP) sin LibreOffice: diapositivas de PowerPoint (PPTX, PPT) con sus formas, imágenes, tablas y notas, y archivos Markdown y de marcado, documentos de Word, ODT, HWP/HWPX, HTML, hojas de cálculo y libros electrónicos, una diapositiva por encabezado.", + "engine.odf-variants.name": "Plantillas y archivos XML planos de OpenDocument (integrado)", + "engine.odf-variants.description": "Convierte archivos OpenDocument en sus plantillas (OTT, OTS, OTP, OTG) y archivos XML planos (FODT, FODS, FODP, FODG), y viceversa, sin LibreOffice y conservando todo el documento.", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "Extrae el texto de los PDF en orden de lectura, también de páginas en dos columnas (sin OCR para escaneos).", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/fa.json b/src/Filee.App/Assets/i18n/fa.json index 8de6630ad..d70c894a0 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -307,6 +307,8 @@ "engine.pptx-writer.description": "ساخت اسلایدهای PowerPoint (PPTX) از فایل‌های مارک‌داون و نشانه‌گذاری، اسناد Word، ODT، HWP/HWPX، HTML، صفحه‌گسترده‌ها و کتاب‌های الکترونیکی: یک اسلاید برای هر عنوان، با فهرست‌ها، جدول‌ها، تصاویر و کد.", "engine.odp-writer.name": "نویسنده ODP (داخلی)", "engine.odp-writer.description": "ساخت ارائه‌های OpenDocument ‏(ODP) بدون LibreOffice: اسلایدهای PowerPoint ‏(PPTX، PPT) با شکل‌ها، تصاویر، جدول‌ها و یادداشت‌های سخنران، و فایل‌های مارک‌داون و نشانه‌گذاری، اسناد Word، ODT، HWP/HWPX، HTML، صفحه‌گسترده‌ها و کتاب‌های الکترونیکی با یک اسلاید برای هر عنوان.", + "engine.odf-variants.name": "قالب‌ها و فایل‌های XML تخت OpenDocument (داخلی)", + "engine.odf-variants.description": "فایل‌های OpenDocument را بدون LibreOffice به قالب‌ها ‏(OTT، OTS، OTP، OTG) و فایل‌های XML تخت ‏(FODT، FODS، FODP، FODG) و برعکس تبدیل می‌کند و کل سند را حفظ می‌کند.", "engine.pdf-text.name": "متن PDF (داخلی)", "engine.pdf-text.description": "استخراج متن PDF به ترتیب خواندن، همچنین از صفحات دو ستونه (بدون OCR برای اسکن‌ها).", "engine.pdf-slides.name": "اسلایدهای PDF (داخلی)", diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.json index 8ba6306ca..1d96ae5d9 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "Crée des diapositives PowerPoint (PPTX) à partir de fichiers Markdown et de balisage, de documents Word, ODT, HWP/HWPX, HTML, de feuilles de calcul et de livres numériques : une diapositive par titre, avec listes, tableaux, images et code.", "engine.odp-writer.name": "Générateur ODP (intégré)", "engine.odp-writer.description": "Crée des présentations OpenDocument (ODP) sans LibreOffice : diapositives PowerPoint (PPTX, PPT) avec leurs formes, images, tableaux et notes, ainsi que fichiers Markdown et de balisage, documents Word, ODT, HWP/HWPX, HTML, feuilles de calcul et livres numériques, une diapositive par titre.", + "engine.odf-variants.name": "Modèles et fichiers XML plats OpenDocument (intégré)", + "engine.odf-variants.description": "Convertit les fichiers OpenDocument en modèles (OTT, OTS, OTP, OTG) et en fichiers XML plats (FODT, FODS, FODP, FODG), et inversement, sans LibreOffice, en gardant tout le document.", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "Extrait le texte des PDF dans l'ordre de lecture, y compris sur deux colonnes (pas d'OCR pour les numérisations).", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.json index f6965d2f9..9a0c9de8f 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "Markdown और मार्कअप फ़ाइलों, Word दस्तावेज़, ODT, HWP/HWPX, HTML, स्प्रेडशीट और ई-बुक से PowerPoint स्लाइड (PPTX) बनाता है: हर शीर्षक के लिए एक स्लाइड, सूचियों, तालिकाओं, चित्रों और कोड के साथ।", "engine.odp-writer.name": "ODP लेखक (अंतर्निहित)", "engine.odp-writer.description": "LibreOffice के बिना OpenDocument प्रस्तुतियाँ (ODP) बनाता है: PowerPoint स्लाइड (PPTX, PPT) उनकी आकृतियों, चित्रों, तालिकाओं और वक्ता नोट्स के साथ, और Markdown व मार्कअप फ़ाइलें, Word दस्तावेज़, ODT, HWP/HWPX, HTML, स्प्रेडशीट और ई-बुक, हर शीर्षक के लिए एक स्लाइड।", + "engine.odf-variants.name": "OpenDocument टेम्पलेट और फ़्लैट XML फ़ाइलें (अंतर्निहित)", + "engine.odf-variants.description": "LibreOffice के बिना OpenDocument फ़ाइलों को उनके टेम्पलेट (OTT, OTS, OTP, OTG) और फ़्लैट XML फ़ाइलों (FODT, FODS, FODP, FODG) में और वापस बदलता है, पूरा दस्तावेज़ जैसा का तैसा रखते हुए।", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "PDF का टेक्स्ट पढ़ने के क्रम में निकालता है, दो-कॉलम वाले पेजों से भी (स्कैन के लिए OCR नहीं)।", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/id.json b/src/Filee.App/Assets/i18n/id.json index 9030c5343..a3c2e6013 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "Membuat slide PowerPoint (PPTX) dari file Markdown dan markup, dokumen Word, ODT, HWP/HWPX, HTML, spreadsheet, dan e-book: satu slide per judul, dengan daftar, tabel, gambar, dan kode.", "engine.odp-writer.name": "Penulis ODP (bawaan)", "engine.odp-writer.description": "Membuat presentasi OpenDocument (ODP) tanpa LibreOffice: slide PowerPoint (PPTX, PPT) beserta bentuk, gambar, tabel, dan catatan pembicara, serta file Markdown dan markup, dokumen Word, ODT, HWP/HWPX, HTML, spreadsheet, dan e-book dengan satu slide per judul.", + "engine.odf-variants.name": "Templat dan file XML datar OpenDocument (bawaan)", + "engine.odf-variants.description": "Mengubah file OpenDocument menjadi templatnya (OTT, OTS, OTP, OTG) dan file XML datar (FODT, FODS, FODP, FODG) serta sebaliknya tanpa LibreOffice, dengan seluruh dokumen tetap utuh.", "engine.pdf-text.name": "Teks PDF (bawaan)", "engine.pdf-text.description": "Mengekstrak teks PDF sesuai urutan baca, termasuk halaman dua kolom (tanpa OCR untuk dokumen pindai).", "engine.pdf-slides.name": "Slide PDF (bawaan)", diff --git a/src/Filee.App/Assets/i18n/it.json b/src/Filee.App/Assets/i18n/it.json index 3d900fcd2..f2beae2a9 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -292,6 +292,8 @@ "engine.pptx-writer.description": "Crea diapositive PowerPoint (PPTX) da file Markdown e di markup, documenti Word, ODT, HWP/HWPX, HTML, fogli di calcolo ed e-book: una diapositiva per titolo, con elenchi, tabelle, immagini e codice.", "engine.odp-writer.name": "Writer ODP (integrato)", "engine.odp-writer.description": "Crea presentazioni OpenDocument (ODP) senza LibreOffice: diapositive PowerPoint (PPTX, PPT) con forme, immagini, tabelle e note, e file Markdown e di markup, documenti Word, ODT, HWP/HWPX, HTML, fogli di calcolo ed e-book, una diapositiva per titolo.", +"engine.odf-variants.name": "Modelli e file XML flat OpenDocument (integrato)", +"engine.odf-variants.description": "Converte i file OpenDocument nei loro modelli (OTT, OTS, OTP, OTG) e in file XML flat (FODT, FODS, FODP, FODG) e viceversa senza LibreOffice, mantenendo tutto il documento.", "engine.pdf-text.name": "Testo PDF (integrato)", "engine.pdf-text.description": "Estrae il testo dei PDF nell'ordine di lettura, anche dalle pagine a due colonne (niente OCR per le scansioni).", "engine.pdf-slides.name": "Slide PDF (integrato)", diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json index 890ee52c3..ceeb0d541 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "Markdown やマークアップ、Word 文書、ODT、HWP/HWPX、HTML、表計算ファイル、電子書籍から PowerPoint スライド(PPTX)を作成します。見出しごとに 1 枚のスライドで、リスト・表・画像・コードも入ります。", "engine.odp-writer.name": "ODP ライター(組み込み)", "engine.odp-writer.description": "LibreOffice なしで OpenDocument プレゼンテーション(ODP)を作成します。PowerPoint スライド(PPTX、PPT)は図形・画像・表・発表者ノートごと移し、Markdown やマークアップ、Word 文書、ODT、HWP/HWPX、HTML、表計算ファイル、電子書籍は見出しごとに 1 枚のスライドにします。", + "engine.odf-variants.name": "OpenDocument テンプレート・フラット XML(内蔵)", + "engine.odf-variants.description": "LibreOffice なしで OpenDocument ファイルをテンプレート(OTT、OTS、OTP、OTG)やフラット XML ファイル(FODT、FODS、FODP、FODG)に、またその逆に変換します。文書の内容はそのまま保たれます。", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "PDFのテキストを読み順に抽出します。2段組みのページにも対応します(スキャン画像のOCRには非対応)。", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json index e1dae63ca..518301fd8 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "마크다운과 마크업 파일, 워드 문서, ODT, HWP/HWPX, HTML, 스프레드시트, 전자책으로 파워포인트 슬라이드(PPTX)를 만들어요. 제목마다 슬라이드 한 장이고, 목록·표·그림·코드도 들어가요.", "engine.odp-writer.name": "ODP 작성기 (내장)", "engine.odp-writer.description": "LibreOffice 없이 오픈도큐먼트 프레젠테이션(ODP)을 만들어요. 파워포인트 슬라이드(PPTX, PPT)는 도형·그림·표·발표자 노트까지 옮기고, 마크다운과 마크업 파일, 워드 문서, ODT, HWP/HWPX, HTML, 스프레드시트, 전자책은 제목마다 슬라이드 한 장으로 만들어요.", + "engine.odf-variants.name": "오픈도큐먼트 서식 파일·단일 XML 파일 (내장)", + "engine.odf-variants.description": "LibreOffice 없이 오픈도큐먼트 파일을 서식 파일(OTT, OTS, OTP, OTG)과 단일 XML 파일(FODT, FODS, FODP, FODG)로, 또 그 반대로 바꿔요. 문서 내용은 그대로 유지돼요.", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "PDF의 글자를 읽는 순서대로 뽑아내요.\n2단 편집도 순서대로 읽어요(스캔 이미지 글자 인식은 안 해요).", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/mn.json b/src/Filee.App/Assets/i18n/mn.json index f3d78b428..dfb815515 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -292,6 +292,8 @@ "engine.pptx-writer.description": "Markdown болон тэмдэглэгээний файл, Word баримт, ODT, HWP/HWPX, HTML, хүснэгт болон цахим номноос PowerPoint слайд (PPTX) үүсгэнэ: гарчиг бүрт нэг слайд, жагсаалт, хүснэгт, зураг болон кодтой.", "engine.odp-writer.name": "ODP бичигч (суурилагдсан)", "engine.odp-writer.description": "LibreOffice-гүйгээр OpenDocument үзүүлэн (ODP) үүсгэнэ: PowerPoint үзүүлэнг (PPTX, PPT) дүрс, зураг, хүснэгт болон илтгэгчийн тэмдэглэлтэй нь, мөн Markdown болон тэмдэглэгээний файл, Word баримт, ODT, HWP/HWPX, HTML, хүснэгт болон цахим номыг гарчиг бүрт нэг слайдтайгаар.", + "engine.odf-variants.name": "OpenDocument загвар ба хавтгай XML файл (суурилагдсан)", + "engine.odf-variants.description": "LibreOffice-гүйгээр OpenDocument файлыг загвар (OTT, OTS, OTP, OTG) болон хавтгай XML файл (FODT, FODS, FODP, FODG) болгож, мөн буцааж хөрвүүлнэ; баримт бичиг бүтнээрээ хадгалагдана.", "engine.pdf-text.name": "PDF текст (суурилагдсан)", "engine.pdf-text.description": "PDF-ийн текстийг унших дарааллаар нь, мөн хоёр баганатай хуудаснаас гаргаж авна (сканнердсан файлд OCR хийхгүй).", "engine.pdf-slides.name": "PDF слайд (суурилагдсан)", diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.json index 14b2c4002..86b864c62 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "Markdown र मार्कअप फाइल, Word कागजात, ODT, HWP/HWPX, HTML, स्प्रेडसिट र इ-पुस्तकबाट PowerPoint स्लाइड (PPTX) बनाउँछ: हरेक शीर्षकका लागि एउटा स्लाइड, सूची, तालिका, चित्र र कोडसहित।", "engine.odp-writer.name": "ODP लेखक (भित्री)", "engine.odp-writer.description": "LibreOffice बिना OpenDocument प्रस्तुतीकरण (ODP) बनाउँछ: PowerPoint स्लाइड (PPTX, PPT) तिनका आकृति, चित्र, तालिका र वक्ता टिप्पणीसहित, र Markdown तथा मार्कअप फाइल, Word कागजात, ODT, HWP/HWPX, HTML, स्प्रेडसिट र इ-पुस्तक, हरेक शीर्षकका लागि एउटा स्लाइड।", + "engine.odf-variants.name": "OpenDocument टेम्प्लेट र फ्ल्याट XML फाइलहरू (बिल्ट-इन)", + "engine.odf-variants.description": "LibreOffice बिना OpenDocument फाइलहरूलाई तिनका टेम्प्लेट (OTT, OTS, OTP, OTG) र फ्ल्याट XML फाइल (FODT, FODS, FODP, FODG) मा र फेरि उल्टो बदल्छ, पूरै कागजात जस्ताको तस्तै राख्दै।", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "पढ्ने क्रममा PDF को पाठ निकाल्छ, दुई-स्तम्भ पृष्ठहरूबाट पनि (स्क्यानका लागि OCR होइन)।", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.json index 658fb1946..db14519d4 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "Tworzy slajdy PowerPoint (PPTX) z plików Markdown i języków znaczników, dokumentów Word, ODT, HWP/HWPX, HTML, arkuszy i e-booków: slajd na każdy nagłówek, z listami, tabelami, obrazami i kodem.", "engine.odp-writer.name": "Generator ODP (wbudowany)", "engine.odp-writer.description": "Tworzy prezentacje OpenDocument (ODP) bez LibreOffice: slajdy PowerPoint (PPTX, PPT) z kształtami, obrazami, tabelami i notatkami oraz pliki Markdown i języków znaczników, dokumenty Word, ODT, HWP/HWPX, HTML, arkusze i e-booki, slajd na każdy nagłówek.", + "engine.odf-variants.name": "Szablony i płaskie pliki XML OpenDocument (wbudowane)", + "engine.odf-variants.description": "Zamienia pliki OpenDocument na ich szablony (OTT, OTS, OTP, OTG) i płaskie pliki XML (FODT, FODS, FODP, FODG) oraz z powrotem bez LibreOffice, zachowując cały dokument.", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "Wyodrębnia tekst z PDF w kolejności czytania, także ze stron dwukolumnowych (bez OCR skanów).", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.json index 628fade4a..5af4ec3c8 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "Cria slides do PowerPoint (PPTX) a partir de arquivos Markdown e de marcação, documentos do Word, ODT, HWP/HWPX, HTML, planilhas e e-books: um slide por título, com listas, tabelas, imagens e código.", "engine.odp-writer.name": "Gerador ODP (mecanismo integrado)", "engine.odp-writer.description": "Cria apresentações OpenDocument (ODP) sem o LibreOffice: slides do PowerPoint (PPTX, PPT) com formas, imagens, tabelas e anotações, e arquivos Markdown e de marcação, documentos do Word, ODT, HWP/HWPX, HTML, planilhas e e-books, um slide por título.", + "engine.odf-variants.name": "Modelos e arquivos XML planos do OpenDocument (integrado)", + "engine.odf-variants.description": "Converte arquivos OpenDocument em seus modelos (OTT, OTS, OTP, OTG) e arquivos XML planos (FODT, FODS, FODP, FODG), e vice-versa, sem LibreOffice, mantendo o documento inteiro.", "engine.pdf-text.name": "Texto de PDF (mecanismo integrado)", "engine.pdf-text.description": "Extrai o texto de PDFs na ordem de leitura, também de páginas em duas colunas (sem OCR para digitalizações).", "engine.pdf-slides.name": "Slides de PDF (mecanismo integrado)", diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.json index 09302ed56..bea94031e 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "Cria diapositivos do PowerPoint (PPTX) a partir de ficheiros Markdown e de marcação, documentos Word, ODT, HWP/HWPX, HTML, folhas de cálculo e livros digitais: um diapositivo por título, com listas, tabelas, imagens e código.", "engine.odp-writer.name": "Gerador ODP (motor integrado)", "engine.odp-writer.description": "Cria apresentações OpenDocument (ODP) sem o LibreOffice: diapositivos do PowerPoint (PPTX, PPT) com formas, imagens, tabelas e notas, e ficheiros Markdown e de marcação, documentos Word, ODT, HWP/HWPX, HTML, folhas de cálculo e livros digitais, um diapositivo por título.", + "engine.odf-variants.name": "Modelos e ficheiros XML planos OpenDocument (integrado)", + "engine.odf-variants.description": "Converte ficheiros OpenDocument nos seus modelos (OTT, OTS, OTP, OTG) e em ficheiros XML planos (FODT, FODS, FODP, FODG), e vice-versa, sem LibreOffice, mantendo o documento inteiro.", "engine.pdf-text.name": "Texto de PDF (motor integrado)", "engine.pdf-text.description": "Extrai o texto de PDF pela ordem de leitura, incluindo páginas com duas colunas (sem OCR para digitalizações).", "engine.pdf-slides.name": "Diapositivos de PDF (motor integrado)", diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json index d74ffafdf..13f345316 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "Создаёт слайды PowerPoint (PPTX) из файлов Markdown и разметки, документов Word, ODT, HWP/HWPX, HTML, таблиц и электронных книг: по слайду на каждый заголовок, со списками, таблицами, рисунками и кодом.", "engine.odp-writer.name": "Генератор ODP (встроенный)", "engine.odp-writer.description": "Создаёт презентации OpenDocument (ODP) без LibreOffice: слайды PowerPoint (PPTX, PPT) с фигурами, рисунками, таблицами и заметками, а также файлы Markdown и разметки, документы Word, ODT, HWP/HWPX, HTML, таблицы и электронные книги — по слайду на каждый заголовок.", + "engine.odf-variants.name": "Шаблоны и плоские XML-файлы OpenDocument (встроено)", + "engine.odf-variants.description": "Превращает файлы OpenDocument в шаблоны (OTT, OTS, OTP, OTG) и плоские XML-файлы (FODT, FODS, FODP, FODG) и обратно без LibreOffice, сохраняя документ целиком.", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "Достаёт текст из PDF в порядке чтения, в том числе со страниц в две колонки (сканы не распознаются).", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.json index 338a98ec3..3e422beed 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "สร้างสไลด์ PowerPoint (PPTX) จากไฟล์ Markdown และภาษามาร์กอัป เอกสาร Word, ODT, HWP/HWPX, HTML, สเปรดชีต และอีบุ๊ก: หนึ่งสไลด์ต่อหนึ่งหัวข้อ พร้อมรายการ ตาราง รูปภาพ และโค้ด", "engine.odp-writer.name": "ตัวเขียน ODP (ในตัว)", "engine.odp-writer.description": "สร้างงานนำเสนอ OpenDocument (ODP) โดยไม่ต้องใช้ LibreOffice: สไลด์ PowerPoint (PPTX, PPT) พร้อมรูปร่าง รูปภาพ ตาราง และบันทึกผู้บรรยาย รวมถึงไฟล์ Markdown และภาษามาร์กอัป เอกสาร Word, ODT, HWP/HWPX, HTML, สเปรดชีต และอีบุ๊ก โดยหนึ่งสไลด์ต่อหนึ่งหัวข้อ", + "engine.odf-variants.name": "เทมเพลตและไฟล์ XML แบบแบน OpenDocument (ในตัว)", + "engine.odf-variants.description": "แปลงไฟล์ OpenDocument เป็นเทมเพลต (OTT, OTS, OTP, OTG) และไฟล์ XML แบบแบน (FODT, FODS, FODP, FODG) และแปลงกลับได้โดยไม่ต้องใช้ LibreOffice โดยเก็บเอกสารไว้ครบถ้วน", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "แยกข้อความของ PDF ตามลำดับการอ่าน รวมถึงหน้าที่มีสองคอลัมน์ (ไม่มี OCR สำหรับเอกสารสแกน)", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.json index 999ce28b5..9a355d68a 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -293,6 +293,8 @@ "engine.pptx-writer.description": "Markdown ve işaretleme dosyaları, Word belgeleri, ODT, HWP/HWPX, HTML, elektronik tablolar ve e-kitaplardan PowerPoint slaytları (PPTX) oluşturur: her başlık için bir slayt; listeler, tablolar, resimler ve kod dahil.", "engine.odp-writer.name": "ODP oluşturucu (yerleşik)", "engine.odp-writer.description": "LibreOffice olmadan OpenDocument sunuları (ODP) oluşturur: PowerPoint slaytlarını (PPTX, PPT) şekilleri, resimleri, tabloları ve konuşmacı notlarıyla; Markdown ve işaretleme dosyalarını, Word belgelerini, ODT, HWP/HWPX, HTML, elektronik tabloları ve e-kitapları her başlık için bir slaytla.", + "engine.odf-variants.name": "OpenDocument şablonları ve düz XML dosyaları (yerleşik)", + "engine.odf-variants.description": "OpenDocument dosyalarını LibreOffice olmadan şablonlarına (OTT, OTS, OTP, OTG) ve düz XML dosyalarına (FODT, FODS, FODP, FODG) dönüştürür ve geri çevirir; belgenin tamamı korunur.", "engine.pdf-text.name": "PDF metin (yerleşik)", "engine.pdf-text.description": "İki sütunlu sayfalar dahil PDF metnini okuma sırasıyla çıkarır (taramalar için OCR yoktur).", "engine.pdf-slides.name": "PDF slayt (yerleşik)", diff --git a/src/Filee.App/Assets/i18n/vi.json b/src/Filee.App/Assets/i18n/vi.json index 360b5f07d..e0118738b 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "Tạo trang chiếu PowerPoint (PPTX) từ tệp Markdown và tệp đánh dấu, tài liệu Word, ODT, HWP/HWPX, HTML, bảng tính và sách điện tử: mỗi tiêu đề một trang chiếu, kèm danh sách, bảng, hình ảnh và mã.", "engine.odp-writer.name": "Trình ghi ODP (tích hợp)", "engine.odp-writer.description": "Tạo bản trình bày OpenDocument (ODP) không cần LibreOffice: trang chiếu PowerPoint (PPTX, PPT) kèm hình vẽ, hình ảnh, bảng và ghi chú, cùng tệp Markdown và tệp đánh dấu, tài liệu Word, ODT, HWP/HWPX, HTML, bảng tính và sách điện tử với mỗi tiêu đề một trang chiếu.", + "engine.odf-variants.name": "Mẫu và tệp XML phẳng OpenDocument (tích hợp)", + "engine.odf-variants.description": "Chuyển tệp OpenDocument thành mẫu (OTT, OTS, OTP, OTG) và tệp XML phẳng (FODT, FODS, FODP, FODG) và ngược lại mà không cần LibreOffice, giữ nguyên toàn bộ tài liệu.", "engine.pdf-text.name": "Văn bản PDF (tích hợp)", "engine.pdf-text.description": "Trích xuất văn bản của PDF theo thứ tự đọc, kể cả trang hai cột (không OCR cho bản quét).", "engine.pdf-slides.name": "Trang chiếu PDF (tích hợp)", diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json index ecfbb5bd7..f62f0b610 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "用 Markdown 和标记语言文件、Word 文档、ODT、HWP/HWPX、HTML、表格和电子书生成 PowerPoint 幻灯片(PPTX):每个标题一张幻灯片,包含列表、表格、图片和代码。", "engine.odp-writer.name": "ODP 生成器(内置)", "engine.odp-writer.description": "无需 LibreOffice 即可生成 OpenDocument 演示文稿(ODP):PowerPoint 幻灯片(PPTX、PPT)连同形状、图片、表格和演讲者备注一并转换;Markdown 和标记语言文件、Word 文档、ODT、HWP/HWPX、HTML、表格和电子书则每个标题一张幻灯片。", + "engine.odf-variants.name": "OpenDocument 模板和平面 XML 文件(内置)", + "engine.odf-variants.description": "无需 LibreOffice,即可将 OpenDocument 文件转换为模板(OTT、OTS、OTP、OTG)和平面 XML 文件(FODT、FODS、FODP、FODG),也可反向转换,文档内容完整保留。", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "按阅读顺序提取 PDF 中的文字,双栏页面也能正确排序(不对扫描件做文字识别)。", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.json index d53a7df1e..82023eaa6 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -300,6 +300,8 @@ "engine.pptx-writer.description": "用 Markdown 和標記語言檔案、Word 文件、ODT、HWP/HWPX、HTML、表格和電子書產生 PowerPoint 投影片(PPTX):每個標題一張投影片,包含清單、表格、圖片和程式碼。", "engine.odp-writer.name": "ODP 產生器(內建)", "engine.odp-writer.description": "不需 LibreOffice 即可產生 OpenDocument 簡報(ODP):PowerPoint 投影片(PPTX、PPT)連同圖形、圖片、表格和演講者備忘稿一併轉換;Markdown 和標記語言檔案、Word 文件、ODT、HWP/HWPX、HTML、表格和電子書則每個標題一張投影片。", + "engine.odf-variants.name": "OpenDocument 範本與平面 XML 檔案(內建)", + "engine.odf-variants.description": "不需 LibreOffice,即可將 OpenDocument 檔案轉換為範本(OTT、OTS、OTP、OTG)與平面 XML 檔案(FODT、FODS、FODP、FODG),也能反向轉換,文件內容完整保留。", "engine.pdf-text.name": "PDF text (built-in)", "engine.pdf-text.description": "按閱讀順序擷取 PDF 中的文字,雙欄頁面也能正確排序(不對掃描檔做文字辨識)。", "engine.pdf-slides.name": "PDF slides (built-in)", diff --git a/src/Filee.Core/Formats/FormatFields.cs b/src/Filee.Core/Formats/FormatFields.cs index dcff6d3e5..3884a2980 100644 --- a/src/Filee.Core/Formats/FormatFields.cs +++ b/src/Filee.Core/Formats/FormatFields.cs @@ -64,7 +64,7 @@ .. Enum.GetValues().Select(category => category.ToString().ToLow ]), (FormatGrade.General, [ - "tiff", "bmp", "ico", "avif", "jxl", "tga", "apng", "jxr", "livp", "exe", "svg", "svgz", "emf", "vsd", "odg", "canvas", "crtx", + "tiff", "bmp", "ico", "avif", "jxl", "tga", "apng", "jxr", "livp", "exe", "svg", "svgz", "emf", "vsd", "odg", "otg", "canvas", "crtx", "docm", "dotx", "dotm", "doc", "odt", "ott", "rtf", "wps", "ofd", "udf", "xps", "mm", "xmind", "abw", "pages", "pub", "eml", "msg", "p7m", "xlsm", "xltx", "xlsb", "xls", "ods", "ots", "tsv", "et", "numbers", "pptm", "potx", "potm", "ppsx", "ppt", "odp", "otp", "key", "dps", "md", "html", "mhtml", "opml", "json", "xml", "vcf", "ics", "epub", "kepub", diff --git a/src/Filee.Core/Formats/FormatRegistry.cs b/src/Filee.Core/Formats/FormatRegistry.cs index 5f21d3c48..28acb094d 100644 --- a/src/Filee.Core/Formats/FormatRegistry.cs +++ b/src/Filee.Core/Formats/FormatRegistry.cs @@ -94,10 +94,12 @@ public static class FormatRegistry // Tajima embroidery designs: the stitches drawn as lines (issue #397). new("dst", "DST", FormatCategory.Vector, ["dst"], Writable: false), new("odg", "ODG", FormatCategory.Vector, ["odg"]), + // OpenDocument drawing templates: the ODG package with the template media type. + new("otg", "OTG", FormatCategory.Vector, ["otg"]), // Obsidian's visual boards (JSON Canvas 1.0): cards, groups and the lines between them, read by Filee itself and // drawn as SVG (then PDF and pictures), or their text as Markdown. A .canvas that isn't one is refused. new("canvas", "Obsidian Canvas", FormatCategory.Vector, ["canvas"], Writable: false), - new("fodg", "FODG", FormatCategory.Vector, ["fodg"], Writable: false), + new("fodg", "FODG", FormatCategory.Vector, ["fodg"]), // Excel chart templates (#1354): the chart drawn by the drawing engine (PDF, SVG, PNG), sample values where the // template holds none, as Excel's template preview shows it. new("crtx", "CRTX", FormatCategory.Vector, ["crtx"], Writable: false), @@ -158,7 +160,7 @@ public static class FormatRegistry new("xlt", "XLT", FormatCategory.Spreadsheet, ["xlt"]), new("ods", "ODS", FormatCategory.Spreadsheet, ["ods"]), new("ots", "OTS", FormatCategory.Spreadsheet, ["ots"]), - new("fods", "FODS", FormatCategory.Spreadsheet, ["fods"], Writable: false), + new("fods", "FODS", FormatCategory.Spreadsheet, ["fods"]), new("csv", "CSV", FormatCategory.Spreadsheet, ["csv"]), new("tsv", "TSV", FormatCategory.Spreadsheet, ["tsv", "tab"]), new("et", "ET", FormatCategory.Spreadsheet, ["et"], Writable: false), @@ -174,7 +176,7 @@ public static class FormatRegistry new("ppt", "PPT", FormatCategory.Presentation, ["ppt", "pps", "pot"]), new("odp", "ODP", FormatCategory.Presentation, ["odp"]), new("otp", "OTP", FormatCategory.Presentation, ["otp"]), - new("fodp", "FODP", FormatCategory.Presentation, ["fodp"], Writable: false), + new("fodp", "FODP", FormatCategory.Presentation, ["fodp"]), new("key", "Keynote", FormatCategory.Presentation, ["key"], Writable: false), new("dps", "DPS", FormatCategory.Presentation, ["dps"], Writable: false), new("sda", "SDA", FormatCategory.Presentation, ["sda", "sdd"], Writable: false), diff --git a/src/Filee.Core/Settings/AppSettings.cs b/src/Filee.Core/Settings/AppSettings.cs index 1984d1bb1..47f8cf6aa 100644 --- a/src/Filee.Core/Settings/AppSettings.cs +++ b/src/Filee.Core/Settings/AppSettings.cs @@ -170,7 +170,7 @@ public static bool IsFlipKeyAllowed(string key, IEnumerable gest public sealed class AppSettings { /// Current schema version. Increase when the format changes and add a migration. - public const int CurrentSchemaVersion = 109; + public const int CurrentSchemaVersion = 111; public int SchemaVersion { get; set; } = CurrentSchemaVersion; diff --git a/src/Filee.Core/Settings/UserDataStore.cs b/src/Filee.Core/Settings/UserDataStore.cs index 51cd46eaa..175fac835 100644 --- a/src/Filee.Core/Settings/UserDataStore.cs +++ b/src/Filee.Core/Settings/UserDataStore.cs @@ -1236,6 +1236,9 @@ public static bool ApplyToLibrary(int from, List presets, List OdfDrawReader.Read(path, cancellationToken: token), path => OdfDrawReader.FirstUnsupported(path)), + new("otg", (path, token) => OdfDrawReader.Read(path, cancellationToken: token), path => OdfDrawReader.FirstUnsupported(path)), new("fodg", (path, token) => OdfDrawReader.Read(path, cancellationToken: token), path => OdfDrawReader.FirstUnsupported(path)), // XPS and OpenXPS documents (.xps, .oxps): Filee's own reader draws every page. new("xps", null) { ReadPages = (path, token) => XpsDocument.Open(path, token) }, diff --git a/src/Filee.Engines/EngineRegistry.cs b/src/Filee.Engines/EngineRegistry.cs index d10d7640f..a9b6fcc75 100644 --- a/src/Filee.Engines/EngineRegistry.cs +++ b/src/Filee.Engines/EngineRegistry.cs @@ -66,6 +66,7 @@ public static IReadOnlyList CreateAll(EngineEnvironment env, ILogger new MarkupConverter(), new PptxWriterConverter(), new OdpWriterConverter(), + new OdfVariantConverter(), new PdfTextConverter(), new PdfSlidesConverter(), new PdfTablesConverter(), diff --git a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs index 8f5dd14ba..5d9f48a3a 100644 --- a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs +++ b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs @@ -13,6 +13,7 @@ using Filee.Engines.Hwp.Hwpx.Odt; using Filee.Engines.Hwp.Hwpx.Pptx; using Filee.Engines.Infrastructure; +using Filee.Engines.Odf; using Filee.Engines.Odf.Draw; using Filee.Engines.Ofd; using Filee.Engines.Office; @@ -42,6 +43,10 @@ internal static class DocumentReaders ["dot"] = DocReader.Read, ["wps"] = DocReader.Read, ["odt"] = OdtReader.Read, + // OpenDocument text templates are the package with another media type; a flat file is read as the package it + // splits into (Odf/OdfVariants), beside the pictures. + ["ott"] = OdtReader.Read, + ["fodt"] = (path, media) => OdfVariants.Read(path, "fodt", MediaParent(media), package => OdtReader.Read(package, media)), ["txt"] = (path, _) => TextDocument(File.ReadAllBytes(path)), ["rtf"] = RtfReader.Read, ["md"] = (path, _) => MarkdownReader.Read(DecodeText(File.ReadAllBytes(path)), Path.GetDirectoryName(Path.GetFullPath(path))!), @@ -54,6 +59,8 @@ internal static class DocumentReaders ["xls"] = (path, _) => SheetDocument.Build(SpreadsheetConverter.Read(path, "xls")), ["xlt"] = (path, _) => SheetDocument.Build(SpreadsheetConverter.Read(path, "xlt")), ["ods"] = (path, _) => SheetDocument.Build(SpreadsheetConverter.Read(path, "ods")), + ["ots"] = (path, _) => SheetDocument.Build(SpreadsheetConverter.Read(path, "ots")), + ["fods"] = (path, _) => SheetDocument.Build(SpreadsheetConverter.Read(path, "fods")), ["pptx"] = PptxReader.Read, ["pptm"] = PptxReader.Read, ["potx"] = PptxReader.Read, @@ -171,6 +178,9 @@ public static async Task ReadAsync(string path, string format, string throw new NotSupportedException($"There is no document reader for {format.ToUpperInvariant()} files."); } + /// The job's scratch folder the media folder is in, for files a reader makes on the way. + private static string MediaParent(string mediaFolder) => Path.GetDirectoryName(Path.GetFullPath(mediaFolder)) ?? Path.GetTempPath(); + private static HDocument ReadSlides(string path, string mediaFolder) => SlideDeckText.Build(OdfDrawReader.Read(path), mediaFolder); // ───────────────────────── Pandoc ───────────────────────── diff --git a/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs b/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs index ab9160a77..03570139c 100644 --- a/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs +++ b/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs @@ -14,7 +14,7 @@ internal static class SheetDocument { /// The formats DocumentReaders reads into a document through . public static readonly IReadOnlySet SourceFormats = - new HashSet(["xlsx", "xlsm", "xltx", "xlsb", "csv", "tsv", "xls", "xlt", "ods"], StringComparer.OrdinalIgnoreCase); + new HashSet(["xlsx", "xlsm", "xltx", "xlsb", "csv", "tsv", "xls", "xlt", "ods", "ots", "fods"], StringComparer.OrdinalIgnoreCase); /// Largest table written; bigger sheets are cut with a note (a PDF of 100 000 rows helps nobody). private const int MaxRows = 5000; diff --git a/src/Filee.Engines/Infrastructure/ComplexScripts.cs b/src/Filee.Engines/Infrastructure/ComplexScripts.cs index 2defa4d08..b5c49c32c 100644 --- a/src/Filee.Engines/Infrastructure/ComplexScripts.cs +++ b/src/Filee.Engines/Infrastructure/ComplexScripts.cs @@ -384,7 +384,7 @@ static bool InWord(string name, int before, int after) return FindInString(message.HtmlBody ?? message.TextBody ?? "", classifier, skipBelow); } return format is "txt" or "md" or "html" or "csv" or "tsv" or "fb2" or "rtf" or "rst" or "tex" or "org" or "adoc" - or "typ" or "mediawiki" or "dbk" or "textile" or "man" or "opml" or "mm" + or "typ" or "mediawiki" or "dbk" or "textile" or "man" or "opml" or "mm" or "fodt" or "fods" or "fodp" or "fodg" ? FindInText(path, classifier, skipBelow) : null; // other binary formats (MOBI, ...): left to the engines } @@ -503,7 +503,7 @@ bool Xml(string folder, string prefix) => "pptx" or "pptm" or "potx" or "potm" or "ppsx" => Xml("ppt/slides/", "slide"), "xlsx" or "xlsm" or "xltx" => Xml("xl/", "sharedStrings") || Xml("xl/worksheets/", "sheet"), "hwpx" => Xml("Contents/", "section"), - "odt" or "ods" or "odp" or "udf" => name.Equals("content.xml", StringComparison.OrdinalIgnoreCase), + "odt" or "ott" or "ods" or "ots" or "odp" or "otp" or "odg" or "otg" or "udf" => name.Equals("content.xml", StringComparison.OrdinalIgnoreCase), "xmind" => name.Equals("content.json", StringComparison.OrdinalIgnoreCase) || name.Equals("content.xml", StringComparison.OrdinalIgnoreCase), // OFD: the pages and templates (Doc_0/Pages/Page_0/Content.xml, Doc_0/Tpls/Tpl_0/Content.xml). "ofd" => name.EndsWith("/Content.xml", StringComparison.OrdinalIgnoreCase), diff --git a/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs b/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs index f8daddc41..8f4d31ac6 100644 --- a/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs +++ b/src/Filee.Engines/Odf/Draw/OdfDrawReader.cs @@ -1,4 +1,4 @@ -// OpenDocument presentations (.odp, .otp, .fodp) and drawings (.odg, .fodg) → the slide model (Slides/SlideModel.cs), +// OpenDocument presentations (.odp, .otp, .fodp) and drawings (.odg, .otg, .fodg) → the slide model (Slides/SlideModel.cs), // read straight from the package or flat file per OpenDocument 1.3 Part 3, for pictures of each page drawn by the slide // renderer (presentations through FileeRendererConverter, drawings through DrawingConverter) without LibreOffice. // @@ -47,7 +47,7 @@ internal sealed partial class OdfDrawReader public static readonly string[] Presentations = ["odp", "otp", "fodp"]; /// Drawing formats (pages through the drawing converter). - public static readonly string[] Drawings = ["odg", "fodg"]; + public static readonly string[] Drawings = ["odg", "otg", "fodg"]; /// Path commands and points of all shapes of a deck at most, every use counting again. private const int MaxDeckCommands = 2_000_000; diff --git a/src/Filee.Engines/Odf/OdfFlat.cs b/src/Filee.Engines/Odf/OdfFlat.cs new file mode 100644 index 000000000..11ddf7fd0 --- /dev/null +++ b/src/Filee.Engines/Odf/OdfFlat.cs @@ -0,0 +1,482 @@ +// Flat OpenDocument files (.fodt, .fods, .fodp, .fodg; OpenDocument 1.3 Part 3, 2.2) to and from packages. A flat +// file is one office:document holding what the package keeps in its parts: office:meta (meta.xml), office:settings +// (settings.xml), office:scripts, office:font-face-decls and office:automatic-styles (content.xml and styles.xml), +// office:styles and office:master-styles (styles.xml) and office:body (content.xml). Pictures are base64 in +// office:binary-data where the package has an xlink:href to a part, and an embedded object (a chart) is an +// office:document of its own inside draw:object where the package has a folder ("Object 1/"). +// +// Taking a flat file apart, the automatic styles go into both content.xml and styles.xml (the master pages use them +// too) and each picture becomes a part under Pictures/ (one part for a picture stored more than once). Putting a package together, the automatic styles of styles.xml +// that have the name of one in content.xml are renamed (with what refers to them) so both sets fit in one element. +// +// Limits: a flat file is read through SafeXml within the document limits' depth and element counts and at most +// FlatPartFactor times what one package part may hold (as the ODP / ODG reader reads one, Odf/Draw/OdfDrawPackage); a +// picture decodes to at most a part's size, all of them to at most the package size, in at most as many parts as a +// package may have. A package is read through DocumentPackage, and a flat file made from it may hold at most what a flat +// file may be read with: one picture referred to again and again is stored each time, and that can't grow without end. + +using System.Globalization; +using System.Security.Cryptography; +using System.Text; +using System.Xml; +using System.Xml.Linq; +using Filee.Engines.Documents; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Infrastructure; +using Filee.Engines.Metafiles; +using OdfNames = Filee.Engines.Hwp.Hwpx.Odt.Odf; + +namespace Filee.Engines.Odf; + +internal static class OdfFlat +{ + /// A flat file may be this many times a package part: it holds every part and the pictures in base64. + internal const int FlatPartFactor = 4; + + /// Objects inside objects followed at most. + private const int MaxObjectDepth = 8; + + private static readonly XNamespace Office = OdfNames.Office; + private static readonly XNamespace Style = OdfNames.Style; + private static readonly XNamespace Text = OdfNames.Text; + private static readonly XNamespace Draw = OdfNames.Draw; + private static readonly XNamespace XLink = OdfNames.XLink; + private static readonly XNamespace Manifest = OdfNames.Manifest; + + /// The elements whose picture (or OLE object) is a part in a package and office:binary-data in a flat file. + private static readonly HashSet Binaries = + [ + Draw + "image", Draw + "fill-image", Draw + "object-ole", Style + "background-image", Text + "list-level-style-image", + ]; + + /// The most a flat file may be, read or written, within . + internal static long MaxBytes(DocumentLimits limits) => limits.MaxPartBytes * FlatPartFactor; + + // ───────────────────────── Flat → package ───────────────────────── + + /// Writes the flat file as the package with . + /// The media types the flat file may say it has. + /// The type for messages ("an OpenDocument text"). + public static void ToPackage(string input, string output, string mimeType, IReadOnlyCollection accepted, string kind, + DocumentLimits limits, CancellationToken cancellationToken) + { + var document = Load(input, kind, limits); + if ((string?)document.Attribute(Office + "mimetype") is { Length: > 0 } type && !accepted.Contains(type.Trim())) + throw new InvalidDataException($"The file is not {kind} ({type.Trim()})."); + var split = new Splitter(limits, cancellationToken); + split.Split(document, "", mimeType, 0); + OdfPackageWriter.Write(output, mimeType, (string?)document.Attribute(Office + "version") ?? OdfNames.Version, split.Parts, cancellationToken); + } + + /// The office:document of a flat file, read within the limits. + internal static XElement Load(string path, string kind, DocumentLimits limits) + { + var maxBytes = MaxBytes(limits); + if (new FileInfo(path).Length > maxBytes) + throw DocumentLimits.Exceeded($"The file is larger than {DocumentLimits.Megabytes(maxBytes)}."); + XDocument document; + using (var stream = File.OpenRead(path)) + { + try + { + document = SafeXml.Load(stream, new XmlLimits(limits.MaxXmlDepth, limits.MaxXmlElements, maxBytes), DocumentLimits.Exceeded); + } + catch (Exception ex) when (ex is XmlException or DecoderFallbackException or ArgumentException) + { + throw new InvalidDataException($"The file is not valid flat OpenDocument XML. It may be damaged. {ex.Message}", ex); + } + } + if (document.Root is not { } root || root.Name != Office + "document") + throw new InvalidDataException($"The file is not {kind} (no office:document)."); + return root; + } + + /// Takes office:document elements apart into the parts of a package. + private sealed class Splitter(DocumentLimits limits, CancellationToken cancellationToken) + { + public List Parts { get; } = []; + + /// The part of each picture by its bytes' hash and folder: a picture stored again and again is one part. + private readonly Dictionary _stored = new(StringComparer.Ordinal); + + private long _bytes; + private int _objects; + + public void Split(XElement document, string folder, string mimeType, int depth) + { + Walk(document, folder, depth); + XElement Root(string name, params XName[] children) + { + var root = new XElement(Office + name, document.Attributes().Where(a => a.IsNamespaceDeclaration)); + if (document.Attribute(Office + "version") is { } version) + root.Add(new XAttribute(version)); + foreach (var child in children) + { + if (document.Element(child) is not { } element) + continue; + // Font faces and automatic styles go into both content.xml and styles.xml; the rest moves. + if (child == Office + "font-face-decls" || child == Office + "automatic-styles") + { + root.Add(new XElement(element)); + } + else + { + element.Remove(); + root.Add(element); + } + } + return root; + } + Add(folder + "content.xml", Root("document-content", Office + "scripts", Office + "font-face-decls", Office + "automatic-styles", Office + "body")); + Add(folder + "styles.xml", Root("document-styles", Office + "font-face-decls", Office + "styles", Office + "automatic-styles", Office + "master-styles")); + if (document.Element(Office + "meta") is not null) + Add(folder + "meta.xml", Root("document-meta", Office + "meta")); + if (document.Element(Office + "settings") is not null) + Add(folder + "settings.xml", Root("document-settings", Office + "settings")); + if (folder.Length > 0) + Parts.Add(new OdfPart(folder, mimeType)); + } + + private void Add(string path, XElement root) + { + using var stream = new MemoryStream(); + using (var writer = XmlWriter.Create(stream, new XmlWriterSettings { Encoding = new UTF8Encoding(false) })) + new XDocument(new XDeclaration("1.0", "UTF-8", null), root).Save(writer); + Parts.Add(new OdfPart(path, "text/xml", Bytes: stream.ToArray())); + } + + /// + /// Turns office:binary-data into parts and embedded office:documents into folders, below + /// but not inside the documents of its objects (those are split on their own). + /// + private void Walk(XElement element, string folder, int depth) + { + foreach (var child in element.Elements().ToList()) + { + cancellationToken.ThrowIfCancellationRequested(); + if (child.Name == Office + "binary-data") + { + Picture(child, folder); + continue; + } + if (child.Name == Draw + "object" && child.Element(Office + "document") is { } inner) + { + Object(child, inner, folder, depth); + continue; + } + if (child.Name != Office + "document") + Walk(child, folder, depth); + } + } + + private void Picture(XElement data, string folder) + { + var owner = data.Parent!; + data.Remove(); + if (!Binaries.Contains(owner.Name)) + return; + var text = data.Value; + if (text.Length / 4L * 3 > limits.MaxPartBytes) + throw DocumentLimits.Exceeded($"A picture stored in the file is larger than {DocumentLimits.Megabytes(limits.MaxPartBytes)}."); + byte[] bytes; + try + { + bytes = System.Convert.FromBase64String(text.Trim()); + } + catch (FormatException) + { + return; // not base64: the picture is left out, as the readers leave it out + } + _bytes += bytes.Length; + if (_bytes > limits.MaxPackageBytes) + throw DocumentLimits.Exceeded($"The pictures stored in the file are larger than {DocumentLimits.Megabytes(limits.MaxPackageBytes)} together."); + var key = folder + ":" + System.Convert.ToHexString(SHA256.HashData(bytes)); + if (_stored.TryGetValue(key, out var known)) + { + Link(owner, known); + return; + } + if (Parts.Count >= limits.MaxPackageEntries) + throw DocumentLimits.Exceeded($"The file holds more than {DocumentLimits.Count(limits.MaxPackageEntries)} pictures and objects."); + var (extension, mediaType) = owner.Name == Draw + "object-ole" ? ("", "application/vnd.sun.star.oleobject") : Kind(bytes); + var name = (owner.Name == Draw + "object-ole" ? "OleObject" : "Pictures/image") + + (Parts.Count + 1).ToString(CultureInfo.InvariantCulture) + extension; + Parts.Add(new OdfPart(folder + name, mediaType, Bytes: bytes)); + _stored[key] = name; + Link(owner, name); + } + + private void Object(XElement frame, XElement inner, string folder, int depth) + { + inner.Remove(); + if (depth >= MaxObjectDepth) + return; + if (++_objects > limits.MaxObjects) + throw DocumentLimits.Exceeded($"The file holds more than {DocumentLimits.Count(limits.MaxObjects)} objects."); + var name = "Object " + _objects.ToString(CultureInfo.InvariantCulture); + var type = ((string?)inner.Attribute(Office + "mimetype"))?.Trim() is { Length: > 0 } declared ? declared : "application/vnd.oasis.opendocument.chart"; + Split(inner, folder + name + "/", type, depth + 1); + Link(frame, "./" + name); + } + + private static void Link(XElement element, string href) + { + element.SetAttributeValue(XLink + "href", href); + element.SetAttributeValue(XLink + "type", "simple"); + element.SetAttributeValue(XLink + "show", "embed"); + element.SetAttributeValue(XLink + "actuate", "onLoad"); + } + } + + /// The extension and media type of a picture, by its signature. + internal static (string Extension, string MediaType) Kind(byte[] data) + { + var span = data.AsSpan(); + if (span.StartsWith((ReadOnlySpan)[0x89, (byte)'P', (byte)'N', (byte)'G'])) + return (".png", "image/png"); + if (span.StartsWith((ReadOnlySpan)[0xFF, 0xD8, 0xFF])) + return (".jpg", "image/jpeg"); + if (span.StartsWith("GIF8"u8)) + return (".gif", "image/gif"); + if (span.StartsWith("BM"u8)) + return (".bmp", "image/bmp"); + if (span.StartsWith("II*\0"u8) || span.StartsWith("MM\0*"u8)) + return (".tif", "image/tiff"); + if (span.Length >= 12 && span.StartsWith("RIFF"u8) && span[8..12].SequenceEqual("WEBP"u8)) + return (".webp", "image/webp"); + switch (Metafile.Detect(span[..Math.Min(span.Length, 64)])) + { + case MetafileFormat.Emf: + return (".emf", "image/x-emf"); + case MetafileFormat.Wmf: + return (".wmf", "image/x-wmf"); + } + var head = Encoding.UTF8.GetString(span[..Math.Min(span.Length, 1024)]); + if (head.Contains("Writes the package as the flat file with . + /// The media types the package may say it has. + /// The type for messages ("an OpenDocument text"). + public static void ToFlat(string input, string output, string mimeType, IReadOnlyCollection accepted, string kind, + DocumentLimits limits, CancellationToken cancellationToken) + { + using var package = OdfVariants.OpenPackage(input, accepted, kind, limits); + var joiner = new Joiner(package, limits, cancellationToken); + var document = joiner.Join("", mimeType, 0) ?? throw new InvalidDataException($"The file is not {kind} (no content.xml)."); + WriterTempFile.Write(output, file => + { + using var writer = XmlWriter.Create(file, new XmlWriterSettings { Encoding = new UTF8Encoding(false) }); + new XDocument(new XDeclaration("1.0", "UTF-8", null), document).Save(writer); + }); + } + + /// Puts the parts of a package (and of its objects) together into office:document elements. + private sealed class Joiner(DocumentPackage package, DocumentLimits limits, CancellationToken cancellationToken) + { + private readonly Dictionary _pictures = new(StringComparer.Ordinal); + private XElement? _manifest; + + /// What the flat file holds so far: the XML parts as they are, the pictures in base64. + private long _stored; + + /// The document of the package's folder ("" or "Object 1/"); null when it has no content.xml. + public XElement? Join(string folder, string mimeType, int depth) + { + var content = Load(folder + "content.xml"); + if (content is null) + return null; + var styles = Load(folder + "styles.xml"); + var meta = Load(folder + "meta.xml"); + var settings = Load(folder + "settings.xml"); + + var document = new XElement(Office + "document"); + foreach (var part in new[] { content, styles, meta, settings }) + { + foreach (var declaration in part?.Attributes().Where(a => a.IsNamespaceDeclaration) ?? []) + { + if (document.Attribute(declaration.Name) is null) + document.Add(new XAttribute(declaration)); + } + } + document.SetAttributeValue(Office + "version", (string?)content.Attribute(Office + "version") ?? (string?)styles?.Attribute(Office + "version") ?? OdfNames.Version); + document.SetAttributeValue(Office + "mimetype", mimeType); + + Add(document, meta?.Element(Office + "meta")); + Add(document, settings?.Element(Office + "settings")); + Add(document, content.Element(Office + "scripts")); + Add(document, Merged(Office + "font-face-decls", content.Element(Office + "font-face-decls"), styles?.Element(Office + "font-face-decls"), [], master: null)); + Add(document, styles?.Element(Office + "styles")); + var renames = new Dictionary(StringComparer.Ordinal); + Add(document, Merged(Office + "automatic-styles", content.Element(Office + "automatic-styles"), styles?.Element(Office + "automatic-styles"), + renames, styles?.Element(Office + "master-styles"))); + Add(document, styles?.Element(Office + "master-styles")); + Add(document, content.Element(Office + "body")); + Embed(document, folder, depth); + return document; + } + + private static void Add(XElement document, XElement? child) + { + if (child is null) + return; + if (child.Parent is not null) + child.Remove(); + document.Add(child); + } + + /// + /// One element with the children of and . A child of the second + /// with the style:name of one of the first is renamed, and so is what refers to it in the second and in + /// ; for font faces, the first one of a name is kept. + /// + private static XElement? Merged(XName name, XElement? first, XElement? second, Dictionary renames, XElement? master) + { + if (first is null && second is null) + return null; + var merged = new XElement(name); + var names = new HashSet(StringComparer.Ordinal); + foreach (var child in first?.Elements().ToList() ?? []) + { + if ((string?)child.Attribute(Style + "name") is { } styleName) + names.Add(styleName); + child.Remove(); + merged.Add(child); + } + var fonts = name == Office + "font-face-decls"; + var all = new HashSet(names, StringComparer.Ordinal); + foreach (var child in second?.Elements() ?? []) + { + if ((string?)child.Attribute(Style + "name") is { } styleName) + all.Add(styleName); + } + foreach (var child in second?.Elements().ToList() ?? []) + { + if ((string?)child.Attribute(Style + "name") is { } styleName && names.Contains(styleName)) + { + if (fonts) + continue; + if (!renames.ContainsKey(styleName)) + { + var renamed = "M" + styleName; + for (var i = 2; all.Contains(renamed); i++) + renamed = "M" + styleName + "_" + i.ToString(CultureInfo.InvariantCulture); + all.Add(renamed); + renames[styleName] = renamed; + } + } + } + if (renames.Count > 0 && second is not null) + { + foreach (var child in second.Elements()) + { + if (child.Attribute(Style + "name") is { } own && renames.TryGetValue(own.Value, out var renamed)) + own.Value = renamed; + } + foreach (var element in second.Descendants().Concat(master?.DescendantsAndSelf() ?? [])) + { + foreach (var attribute in element.Attributes()) + { + if (IsStyleReference(attribute.Name) && renames.TryGetValue(attribute.Value, out var renamed)) + attribute.Value = renamed; + } + } + } + foreach (var child in second?.Elements().ToList() ?? []) + { + if (fonts && (string?)child.Attribute(Style + "name") is { } font && names.Contains(font)) + continue; + child.Remove(); + merged.Add(child); + } + return merged; + } + + /// An attribute that names a style: text:style-name, style:parent-style-name, style:page-layout-name and the like. + private static bool IsStyleReference(XName name) => + !name.NamespaceName.Equals(XNamespace.Xmlns.NamespaceName, StringComparison.Ordinal) + && (name.LocalName.EndsWith("style-name", StringComparison.Ordinal) || name.LocalName == "page-layout-name"); + + /// Replaces links to pictures and objects inside the package with the pictures and objects themselves. + private void Embed(XElement document, string folder, int depth) + { + foreach (var element in document.Descendants().ToList()) + { + cancellationToken.ThrowIfCancellationRequested(); + if (element.Name == Draw + "object") + { + if (Internal(element) is { } objectFolder && depth < MaxObjectDepth) + { + var inner = Join(folder + objectFolder.TrimEnd('/') + "/", MediaType(folder + objectFolder.TrimEnd('/') + "/") ?? "application/vnd.oasis.opendocument.chart", depth + 1); + if (inner is not null) + { + Unlink(element); + element.AddFirst(inner); + } + } + continue; + } + if (!Binaries.Contains(element.Name) || Internal(element) is not { } path || Picture(folder + path) is not { } bytes) + continue; + Count((bytes.Length + 2L) / 3 * 4); + Unlink(element); + element.AddFirst(new XElement(Office + "binary-data", System.Convert.ToBase64String(bytes))); + } + } + + private static string? Internal(XElement element) => Filee.Engines.Odf.Draw.OdfDrawPackage.InternalPath((string?)element.Attribute(XLink + "href")); + + private static void Unlink(XElement element) + { + foreach (var name in new[] { "href", "type", "show", "actuate" }) + element.Attribute(XLink + name)?.Remove(); + } + + private byte[]? Picture(string path) + { + if (_pictures.TryGetValue(path, out var known)) + return known; + if (package.GetEntry(path) is not { } entry) + return null; + var bytes = package.ReadBytes(entry, limits.MaxPartBytes) + ?? throw DocumentLimits.Exceeded($"The part {path} of the package is larger than {DocumentLimits.Megabytes(limits.MaxPartBytes)}."); + _pictures[path] = bytes; + return bytes; + } + + /// The media type the manifest gives a folder of the package ("Object 1/"), or null. + private string? MediaType(string folder) + { + _manifest ??= OdfVariants.LoadManifest(package)?.Root ?? new XElement(Manifest + "manifest"); + return _manifest.Elements(Manifest + "file-entry") + .FirstOrDefault(e => string.Equals(((string?)e.Attribute(Manifest + "full-path"))?.TrimEnd('/'), folder.TrimEnd('/'), StringComparison.Ordinal)) + ?.Attribute(Manifest + "media-type")?.Value is { Length: > 0 } type ? type : null; + } + + private void Count(long bytes) + { + _stored += bytes; + if (_stored > MaxBytes(limits)) + throw DocumentLimits.Exceeded($"The flat file would be larger than {DocumentLimits.Megabytes(MaxBytes(limits))}."); + } + + private XElement? Load(string name) + { + if (package.GetEntry(name) is not { } entry) + return null; + Count(entry.Length); + try + { + return package.LoadXml(entry, preserveWhitespace: true).Root; + } + catch (XmlException ex) + { + throw new InvalidDataException($"The file cannot be read ({name} is not valid XML). It may be damaged. {ex.Message}", ex); + } + } + } +} diff --git a/src/Filee.Engines/Odf/OdfVariants.cs b/src/Filee.Engines/Odf/OdfVariants.cs new file mode 100644 index 000000000..655792681 --- /dev/null +++ b/src/Filee.Engines/Odf/OdfVariants.cs @@ -0,0 +1,228 @@ +// The forms one OpenDocument type comes in (OpenDocument 1.3 Part 2 and Part 3): the package (.odt, .ods, .odp, +// .odg), the template (.ott, .ots, .otp, .otg: the same package with the "-template" media type) and the flat XML file +// (.fodt, .fods, .fodp, .fodg: one office:document with every part and the pictures in it, see OdfFlat). One form turns +// into another of its type without reading the document: a template is the package with its media type changed in +// "mimetype" and the manifest, a flat file is the package's parts put together or taken apart. Everything else in the +// file (macros, settings, thumbnails, objects) stays as it is. +// +// The readers of the package form read the other forms through AsPackage: a template as it is (they take the template +// media type), a flat file as the package it splits into, in a temporary file beside the job's pictures. + +using System.IO.Compression; +using System.Xml; +using System.Xml.Linq; +using Filee.Engines.Documents; +using Filee.Engines.Hwp.Hwpx; +using OdfNames = Filee.Engines.Hwp.Hwpx.Odt.Odf; + +namespace Filee.Engines.Odf; + +/// The form of an OpenDocument file. +internal enum OdfForm +{ + Package, + Template, + Flat, +} + +/// One form of an OpenDocument type: its format id, the format id of its package form, and its media type. +internal sealed record OdfVariant(string Format, string Family, OdfForm Form, string MimeType, string Kind); + +internal static class OdfVariants +{ + public const string SpreadsheetMimeType = "application/vnd.oasis.opendocument.spreadsheet"; + public const string SpreadsheetTemplateMimeType = "application/vnd.oasis.opendocument.spreadsheet-template"; + + /// Every form of every type, the package form first. + public static IReadOnlyList All { get; } = + [ + new("odt", "odt", OdfForm.Package, OdfNames.MimeType, "an OpenDocument text"), + new("ott", "odt", OdfForm.Template, OdfNames.TemplateMimeType, "an OpenDocument text"), + new("fodt", "odt", OdfForm.Flat, OdfNames.MimeType, "an OpenDocument text"), + new("ods", "ods", OdfForm.Package, SpreadsheetMimeType, "an OpenDocument spreadsheet"), + new("ots", "ods", OdfForm.Template, SpreadsheetTemplateMimeType, "an OpenDocument spreadsheet"), + new("fods", "ods", OdfForm.Flat, SpreadsheetMimeType, "an OpenDocument spreadsheet"), + new("odp", "odp", OdfForm.Package, OdfNames.PresentationMimeType, "an OpenDocument presentation"), + new("otp", "odp", OdfForm.Template, OdfNames.PresentationTemplateMimeType, "an OpenDocument presentation"), + new("fodp", "odp", OdfForm.Flat, OdfNames.PresentationMimeType, "an OpenDocument presentation"), + new("odg", "odg", OdfForm.Package, OdfNames.GraphicsMimeType, "an OpenDocument drawing"), + new("otg", "odg", OdfForm.Template, OdfNames.GraphicsTemplateMimeType, "an OpenDocument drawing"), + new("fodg", "odg", OdfForm.Flat, OdfNames.GraphicsMimeType, "an OpenDocument drawing"), + ]; + + /// The form of format id , or null for a format that is none. + public static OdfVariant? Find(string format) => + All.FirstOrDefault(v => string.Equals(v.Format, format, StringComparison.OrdinalIgnoreCase)); + + /// The media types a file of the type of may say it has (document and template). + private static IEnumerable MimeTypes(OdfVariant variant) => All.Where(v => v.Family == variant.Family).Select(v => v.MimeType); + + /// + /// Writes (format ) as in form + /// of the same type, replacing it at the end. + /// + /// The file is another type, damaged or encrypted. + /// The file is past the document limits. + public static void Convert(string input, string from, string output, string to, DocumentLimits? limits = null, CancellationToken cancellationToken = default) + { + limits ??= DocumentLimits.Default; + var source = Find(from) ?? throw new ArgumentException($"{from} is no OpenDocument format.", nameof(from)); + var target = Find(to) ?? throw new ArgumentException($"{to} is no OpenDocument format.", nameof(to)); + if (source.Family != target.Family || source.Form == target.Form) + throw new ArgumentException($"{from} doesn't become {to}.", nameof(to)); + if (source.Form == OdfForm.Flat) + { + OdfFlat.ToPackage(input, output, target.MimeType, [.. MimeTypes(source)], source.Kind, limits, cancellationToken); + return; + } + if (target.Form == OdfForm.Flat) + OdfFlat.ToFlat(input, output, target.MimeType, [.. MimeTypes(source)], source.Kind, limits, cancellationToken); + else + Retype(input, output, target.MimeType, [.. MimeTypes(source)], source.Kind, limits, cancellationToken); + } + + /// + /// The package a reader of the package form reads for : the file itself for a package or a + /// template, a new package in for a flat file (removed when the result is disposed). + /// + public static PackageFile AsPackage(string path, string format, string folder, DocumentLimits? limits = null) + { + var variant = Find(format) ?? throw new ArgumentException($"{format} is no OpenDocument format.", nameof(format)); + if (variant.Form != OdfForm.Flat) + return new PackageFile(path, temporary: false); + Directory.CreateDirectory(folder); + var package = Path.Combine(folder, $"flat-{Guid.NewGuid():N}.{variant.Family}"); + try + { + OdfFlat.ToPackage(path, package, variant.MimeType, [.. MimeTypes(variant)], variant.Kind, limits ?? DocumentLimits.Default, CancellationToken.None); + } + catch + { + WriterTempFile.Discard(package); + throw; + } + return new PackageFile(package, temporary: true); + } + + /// Reads with , a reader of the package form. + public static T Read(string path, string format, string folder, Func read) + { + using var package = AsPackage(path, format, folder); + return read(package.Path); + } + + /// A package on disk; a temporary one is removed on dispose. + internal sealed class PackageFile(string path, bool temporary) : IDisposable + { + public string Path { get; } = path; + + public void Dispose() + { + if (temporary) + WriterTempFile.Discard(Path); + } + } + + // ───────────────────────── Package ↔ template ───────────────────────── + + /// + /// Copies the package with in "mimetype" (first, stored) and on the manifest's root + /// entry; every other part is copied as it is, read through the document limits. + /// + private static void Retype(string input, string output, string mimeType, IReadOnlyCollection accepted, string kind, + DocumentLimits limits, CancellationToken cancellationToken) + { + using var package = OpenPackage(input, accepted, kind, limits); + var manifest = Manifest(package, mimeType); + WriterTempFile.Write(output, file => + { + using var zip = new ZipArchive(file, ZipArchiveMode.Create, leaveOpen: true); + using (var writer = new StreamWriter(zip.CreateEntry("mimetype", CompressionLevel.NoCompression).Open(), new System.Text.UTF8Encoding(false))) + writer.Write(mimeType); + var written = new HashSet(StringComparer.OrdinalIgnoreCase) { "mimetype", ManifestPath }; + foreach (var entry in package.Entries) + { + cancellationToken.ThrowIfCancellationRequested(); + var name = entry.FullName; + if (name.EndsWith('/') || name.EndsWith('\\') || !written.Add(name)) + continue; + // Pictures stored as they are (PNG, JPEG) stay stored: deflating them again gains nothing. + var level = entry.CompressedLength >= entry.Length ? CompressionLevel.NoCompression : CompressionLevel.Optimal; + var copy = zip.CreateEntry(name, level); + if (entry.LastWriteTime.Year is >= 1980 and <= 2107) + copy.LastWriteTime = entry.LastWriteTime; + using var source = package.Open(entry); + using var target = copy.Open(); + source.CopyTo(target); + } + using var part = zip.CreateEntry(ManifestPath, CompressionLevel.Optimal).Open(); + using var xml = XmlWriter.Create(part, new XmlWriterSettings { Encoding = new System.Text.UTF8Encoding(false) }); + manifest.Save(xml); + }); + } + + internal const string ManifestPath = "META-INF/manifest.xml"; + + /// + /// Opens a package of the type named by : one whose "mimetype" is another type's, or whose + /// parts are encrypted, is refused with its reason. + /// + internal static DocumentPackage OpenPackage(string path, IReadOnlyCollection accepted, string kind, DocumentLimits limits) + { + var package = DocumentPackage.Open(path, limits, + () => new InvalidDataException($"The file is not {kind} (it is not a ZIP package). It may be damaged.")); + try + { + if (package.GetEntry("mimetype") is { } mimetype && package.ReadBytes(mimetype, 256) is { } bytes) + { + var type = System.Text.Encoding.ASCII.GetString(bytes).Trim(); + if (type.Length > 0 && !accepted.Contains(type)) + throw new InvalidDataException($"The file is not {kind} ({type})."); + } + if (package.GetEntry("content.xml") is null) + throw new InvalidDataException($"The file is not {kind} (no content.xml)."); + if (LoadManifest(package)?.Descendants(OdfNames.Manifest + "encryption-data").Any() == true) + throw new InvalidDataException("The file is protected with a password (encrypted). Open it in LibreOffice, remove the password (File → Properties → Security), save it and convert it again."); + return package; + } + catch + { + package.Dispose(); + throw; + } + } + + /// The manifest, or null when the package has none; a damaged one is invalid data. + internal static XDocument? LoadManifest(DocumentPackage package) + { + if (package.GetEntry(ManifestPath) is not { } entry) + return null; + try + { + return package.LoadXml(entry, preserveWhitespace: false); + } + catch (XmlException ex) + { + throw new InvalidDataException($"The file cannot be read ({ManifestPath} is not valid XML). It may be damaged. {ex.Message}", ex); + } + } + + /// The package's manifest with on its root entry; a new one when it has none. + private static XDocument Manifest(DocumentPackage package, string mimeType) + { + var m = OdfNames.Manifest; + var manifest = LoadManifest(package) is { Root: { } root } loaded && root.Name == m + "manifest" + ? loaded + : new XDocument(new XElement(m + "manifest", new XAttribute(XNamespace.Xmlns + "manifest", m.NamespaceName), + new XAttribute(m + "version", OdfNames.Version))); + manifest.Declaration = new XDeclaration("1.0", "UTF-8", null); + var entry = manifest.Root!.Elements(m + "file-entry").FirstOrDefault(e => (string?)e.Attribute(m + "full-path") == "/"); + if (entry is null) + { + entry = new XElement(m + "file-entry", new XAttribute(m + "full-path", "/")); + manifest.Root.AddFirst(entry); + } + entry.SetAttributeValue(m + "media-type", mimeType); + return manifest; + } +} diff --git a/src/Filee.Engines/Office/DocumentTablesConverter.cs b/src/Filee.Engines/Office/DocumentTablesConverter.cs index 82744b665..41f7ffded 100644 --- a/src/Filee.Engines/Office/DocumentTablesConverter.cs +++ b/src/Filee.Engines/Office/DocumentTablesConverter.cs @@ -25,11 +25,11 @@ namespace Filee.Engines.Office; public sealed class DocumentTablesConverter : IConverter, ICapabilityCheck { /// - /// Word documents (and their macro and template variants, Word 97–2003 and WPS), OpenDocument text, RTF, 한글 files, - /// web pages and saved web pages. + /// Word documents (and their macro and template variants, Word 97–2003 and WPS), OpenDocument text (and its template + /// and flat file), RTF, 한글 files, web pages and saved web pages. /// private static readonly string[] Sources = - ["docx", "docm", "dotx", "dotm", .. Hwp.Hwpx.Doc.DocReader.Formats, "odt", "rtf", "hwp", "hwpx", "html", "mhtml"]; + ["docx", "docm", "dotx", "dotm", .. Hwp.Hwpx.Doc.DocReader.Formats, "odt", "ott", "fodt", "rtf", "hwp", "hwpx", "html", "mhtml"]; public string Id => "document-tables"; public string DisplayName => "Document tables (built-in)"; diff --git a/src/Filee.Engines/Office/OdfVariantConverter.cs b/src/Filee.Engines/Office/OdfVariantConverter.cs new file mode 100644 index 000000000..13363ebe9 --- /dev/null +++ b/src/Filee.Engines/Office/OdfVariantConverter.cs @@ -0,0 +1,48 @@ +// The forms of one OpenDocument type to each other without LibreOffice (Odf/OdfVariants): ODT ↔ OTT ↔ FODT, +// ODS ↔ OTS ↔ FODS, ODP ↔ OTP ↔ FODP, ODG ↔ OTG ↔ FODG. Nothing is read into a model or written again: a template +// is the package with another media type, a flat file the package's parts in one XML document (Odf/OdfFlat), so the +// document comes out as it went in. +// +// The edges cost 2, below every writer, and are steps of longer routes too: DOCX → ODT (the ODT writer) → OTT, XLSX → +// ODS (the spreadsheet engine) → FODS, each far below LibreOffice's fallback (25). The writers that only make the +// package form directly (the ODP writer's edges are direct only) write the other forms themselves through +// OdfVariants.Convert. + +using Filee.Core.Conversion; +using Filee.Engines.Infrastructure; +using Filee.Engines.Odf; + +namespace Filee.Engines.Office; + +/// Converts between the package, template and flat forms of an OpenDocument type. +public sealed class OdfVariantConverter : IConverter +{ + /// Below every writer: the forms of a type convert without loss. + internal const int Cost = 2; + + public string Id => "odf-variants"; + public string DisplayName => "OpenDocument templates and flat files (built-in)"; + public int MaxParallelism => 0; + + public IReadOnlyList Edges { get; } = + [ + .. from source in OdfVariants.All + from target in OdfVariants.All + where source.Family == target.Family && source.Form != target.Form + select new ConversionEdge(source.Format, target.Format, Cost), + ]; + + public EngineStatus GetStatus() => + EngineStatus.Available("ODT ↔ OTT ↔ FODT, ODS ↔ OTS ↔ FODS, ODP ↔ OTP ↔ FODP, ODG ↔ OTG ↔ FODG", EngineVersions.BuiltIn); + + public async Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) + { + progress?.Report(0.1); + var output = step.Output.Allocate(step.To); + if (output is null) + return []; + await Task.Run(() => OdfVariants.Convert(step.InputPath, step.From, output, step.To, cancellationToken: cancellationToken), cancellationToken); + progress?.Report(1); + return [output]; + } +} diff --git a/src/Filee.Engines/Office/OdpWriterConverter.cs b/src/Filee.Engines/Office/OdpWriterConverter.cs index 8b07053db..3343c93ea 100644 --- a/src/Filee.Engines/Office/OdpWriterConverter.cs +++ b/src/Filee.Engines/Office/OdpWriterConverter.cs @@ -10,13 +10,16 @@ // then written as the same deck in ODP, so both writers give the same slides. // // The edges cost 8, as the PPTX writer's, below LibreOffice's fallback (25); they are DirectOnly, so a deck written -// here is never a step towards another format (a document → PDF stays off slides). OpenDocument presentations -// themselves (ODP, OTP, FODP) are not sources: they keep LibreOffice's route to each other, as ODP → PPTX does. +// here is never a step towards another format (a document → PDF stays off slides). That keeps them from leading on +// to the template and the flat file too, so the writer makes those itself: the ODP it writes, turned into OTP or FODP +// (Odf/OdfVariants). OpenDocument presentations themselves (ODP, OTP, FODP) are not sources: they become each other +// through OdfVariantConverter, which keeps the whole deck, and keep LibreOffice's route to PPTX. using Filee.Core.Conversion; using Filee.Engines.Hwp.Hwpx; using Filee.Engines.Hwp.Hwpx.Pptx; using Filee.Engines.Infrastructure; +using Filee.Engines.Odf; using Filee.Engines.Odf.Draw; using Filee.Engines.Office.Ppt; using Filee.Engines.Slides; @@ -29,6 +32,9 @@ public sealed class OdpWriterConverter : IConverter, ICapabilityCheck /// As the PPTX writer's: below LibreOffice's fallback (25) whatever the engine order adds (at most 9). internal const int Cost = 8; + /// What the writer writes: the presentation, its template and its flat file. + private static readonly string[] Targets = ["odp", "otp", "fodp"]; + /// The PowerPoint decks the slide reader reads. internal static readonly string[] Decks = [.. SlideFormats.Decks.Where(f => !OdfDrawReader.Presentations.Contains(f))]; @@ -48,8 +54,10 @@ public IReadOnlyList Edges var documents = Documents(_pandoc ??= PandocConverter.Locate() is not null); return [ - .. Decks.Select(from => new ConversionEdge(from, "odp", Cost, DirectOnly: true, Checked: true)), - .. documents.Select(from => new ConversionEdge(from, "odp", Cost, DirectOnly: true, Checked: DocumentReaders.Checked(from))), + .. from source in Decks from to in Targets select new ConversionEdge(source, to, Cost, DirectOnly: true, Checked: true), + .. from source in documents + from to in Targets + select new ConversionEdge(source, to, Cost, DirectOnly: true, Checked: DocumentReaders.Checked(source)), ]; } } @@ -68,7 +76,8 @@ public IReadOnlyList Edges public EngineStatus GetStatus() { _pandoc = PandocConverter.Locate() is not null; - return EngineStatus.Available(string.Join(", ", Decks.Concat(Documents(_pandoc.Value)).Select(f => f.ToUpperInvariant())) + " → ODP", EngineVersions.BuiltIn); + return EngineStatus.Available(string.Join(", ", Decks.Concat(Documents(_pandoc.Value)).Select(f => f.ToUpperInvariant())) + " → ODP, OTP, FODP", + EngineVersions.BuiltIn); } public async Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) @@ -94,14 +103,29 @@ public async Task> ConvertAsync(ConversionStep step, IProg } progress?.Report(0.6); - var output = step.Output.Allocate("odp"); + var output = step.Output.Allocate(step.To); if (output is null) return []; await Task.Run(() => { var deck = PptxSlideReader.Read(deckPath, cancellationToken: cancellationToken); var notes = PptxNotes.Read(deckPath, cancellationToken: cancellationToken); - OdpWriter.Write(deck, output, notes, cancellationToken: cancellationToken); + if (step.To == "odp") + { + OdpWriter.Write(deck, output, notes, cancellationToken: cancellationToken); + return; + } + // The template or the flat file: the presentation written first, then turned into it. + var odp = Path.Combine(step.WorkDirectory, $"deck-{Guid.NewGuid():N}.odp"); + try + { + OdpWriter.Write(deck, odp, notes, cancellationToken: cancellationToken); + OdfVariants.Convert(odp, "odp", output, step.To, cancellationToken: cancellationToken); + } + finally + { + File.Delete(odp); + } }, cancellationToken); progress?.Report(1); return [output]; diff --git a/src/Filee.Engines/Office/OdtWriterConverter.cs b/src/Filee.Engines/Office/OdtWriterConverter.cs index c7430f476..15c683201 100644 --- a/src/Filee.Engines/Office/OdtWriterConverter.cs +++ b/src/Filee.Engines/Office/OdtWriterConverter.cs @@ -19,9 +19,12 @@ public sealed class OdtWriterConverter : IConverter, ICapabilityCheck public string DisplayName => "ODT writer (built-in)"; public int MaxParallelism => 0; - /// ODT → ODT is no conversion. + /// + /// ODT → ODT is no conversion; the template and the flat file (OTT, FODT) become ODT through OdfVariantConverter, + /// which keeps the whole document. OTT and FODT come from the ODT the same way (DOCX → ODT → OTT). + /// public IReadOnlyList Edges { get; } = - DocumentReaders.BuiltInFormats.Where(f => f != "odt") + DocumentReaders.BuiltInFormats.Where(f => f is not ("odt" or "ott" or "fodt")) .Select(f => new ConversionEdge(f, "odt", f == "pdf" ? Cost + DocumentReaders.PdfPenalty : Cost, Checked: DocumentReaders.Checked(f))).ToList(); /// 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/Office/SheetPrintConverter.cs b/src/Filee.Engines/Office/SheetPrintConverter.cs index 03768498f..76fa5e797 100644 --- a/src/Filee.Engines/Office/SheetPrintConverter.cs +++ b/src/Filee.Engines/Office/SheetPrintConverter.cs @@ -1,4 +1,4 @@ -// XLSX (and XLSM / XLTX), XLSB, XLS (and XLT) and ODS → PDF printed the way Excel prints them, without Excel or LibreOffice: +// XLSX (and XLSM / XLTX), XLSB, XLS (and XLT) and ODS (and OTS / FODS) → PDF printed the way Excel prints them, without Excel or LibreOffice: // the built-in readers (Office/Sheets) keep each sheet's page setup, and SheetPrinter lays the pages out with Excel's // page logic and writes them with the PDF writer of the Filee renderer. CSV and TSV have no page setup and keep their // route through the HWPX writer (each sheet fitted to the page width). @@ -28,7 +28,7 @@ public sealed class SheetPrintConverter : IConverter /// Shown with a workbook printed without what named. internal const string LeftOutNotice = "notice.sheet_objects_left_out"; - private static readonly string[] Sources = ["xlsx", "xlsm", "xltx", "xlsb", "xls", "xlt", "ods"]; + private static readonly string[] Sources = ["xlsx", "xlsm", "xltx", "xlsb", "xls", "xlt", "ods", "ots", "fods"]; public string Id => "sheet-print"; public string DisplayName => "Spreadsheet printing (built-in)"; @@ -42,7 +42,7 @@ public sealed class SheetPrintConverter : IConverter /// internal Func?, CancellationToken, Task>?>? Fallback { get; set; } - public EngineStatus GetStatus() => EngineStatus.Available("XLSX, XLSB, XLS, ODS → PDF", EngineVersions.BuiltIn); + public EngineStatus GetStatus() => EngineStatus.Available("XLSX, XLSB, XLS, ODS, OTS, FODS → PDF", EngineVersions.BuiltIn); /// Gives the sheet printer among LibreOffice, when it is there, for what it doesn't draw. internal static IReadOnlyList WithFallback(IReadOnlyList converters) diff --git a/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs b/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs index 5330e1bcd..333690fcb 100644 --- a/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs +++ b/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs @@ -42,7 +42,7 @@ internal static partial class SheetCapabilities private const long MaxVmlBytes = 16L * 1024 * 1024; /// The first thing in the workbook the sheet printer doesn't draw; null when it draws everything (or can't read the file). - /// Format id: xlsx, xlsm, xltx, xlsb, xls, xlt or ods. + /// Format id: xlsx, xlsm, xltx, xlsb, xls, xlt, ods, ots or fods. public static string? FirstUnsupported(string path, string format) { try @@ -54,7 +54,9 @@ internal static partial class SheetCapabilities XlsbReader.Format => IsZip(path) ? Xlsb(path) : XlsLayout.FirstObject(path), // Web apps often save XLSX with an .xls name; the XLSX reader reads those (XlsReader). "xls" or "xlt" => IsZip(path) ? Xlsx(path) : XlsLayout.FirstObject(path), - "ods" => Ods(path), + "ods" or "ots" => Ods(path), + // A flat file is looked through as the package it splits into. + "fods" => Odf.OdfVariants.Read(path, "fods", Path.GetTempPath(), Ods), _ => null, }; } diff --git a/src/Filee.Engines/Office/SpreadsheetConverter.cs b/src/Filee.Engines/Office/SpreadsheetConverter.cs index 98e49393e..fa66809a4 100644 --- a/src/Filee.Engines/Office/SpreadsheetConverter.cs +++ b/src/Filee.Engines/Office/SpreadsheetConverter.cs @@ -1,5 +1,6 @@ -// Spreadsheets between XLSX, XLS, ODS, CSV and TSV without Excel or LibreOffice (XLS / XLT, XLSB and the XLSM / XLTX -// variants are read, not written). +// Spreadsheets between XLSX, XLS, ODS, CSV and TSV without Excel or LibreOffice (XLS / XLT, XLSB, the XLSM / XLTX +// variants and the OpenDocument template and flat file, OTS / FODS, are read, not written: OTS and FODS come from the +// ODS through OdfVariantConverter). // XLSX / XLS / ODS → PDF are printed by SheetPrintConverter; CSV / TSV → PDF and every sheet → HWPX and HTML go // through the HWPX writer (sheets become 한글 tables), see HwpxConverter and SheetDocument. @@ -12,7 +13,7 @@ namespace Filee.Engines.Office; /// Converts between spreadsheet formats in-process. public sealed class SpreadsheetConverter : IConverter { - private static readonly string[] Sources = ["xlsx", "xlsm", "xltx", "xlsb", "xls", "xlt", "ods", "csv", "tsv"]; + private static readonly string[] Sources = ["xlsx", "xlsm", "xltx", "xlsb", "xls", "xlt", "ods", "ots", "fods", "csv", "tsv"]; private static readonly string[] Targets = ["xlsx", "ods", "csv", "tsv"]; public string Id => "spreadsheet"; @@ -20,12 +21,13 @@ public sealed class SpreadsheetConverter : IConverter public int MaxParallelism => 0; public IReadOnlyList Edges { get; } = - // XLSM / XLTX → XLSX is left to the ooxml engine, which keeps the whole package instead of rewriting the sheets. - [.. Sources.SelectMany(from => Targets.Where(to => to != from && !(to == "xlsx" && from is "xlsm" or "xltx")) + // XLSM / XLTX → XLSX is left to the ooxml engine, which keeps the whole package instead of rewriting the sheets, + // and OTS / FODS → ODS to the OpenDocument forms engine, which keeps the whole document. + [.. Sources.SelectMany(from => Targets.Where(to => to != from && !(to == "xlsx" && from is "xlsm" or "xltx") && !(to == "ods" && from is "ots" or "fods")) .Select(to => new ConversionEdge(from, to)))]; public EngineStatus GetStatus() => - EngineStatus.Available("XLSX, XLSB, XLS, ODS, CSV, TSV → XLSX, ODS, CSV, TSV", + EngineStatus.Available("XLSX, XLSB, XLS, ODS, OTS, FODS, CSV, TSV → XLSX, ODS, CSV, TSV", $"{EngineVersions.BuiltIn} · {EngineVersions.Library("ExcelDataReader", typeof(ExcelDataReader.ExcelReaderFactory))}"); public Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) => @@ -80,7 +82,9 @@ internal static List Write(Workbook book, ConversionStep step) { "xls" or "xlt" => XlsReader.Read(path), XlsbReader.Format => XlsbReader.Read(path), - "ods" => OdsReader.Read(path), + // An OpenDocument template is the package with another media type; a flat file is read as the package it splits into. + "ods" or "ots" => OdsReader.Read(path), + "fods" => Odf.OdfVariants.Read(path, "fods", Path.GetTempPath(), OdsReader.Read), "csv" => CsvFormat.Read(path), "tsv" => CsvFormat.ReadTsv(path), _ => XlsxReader.Read(path), diff --git a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs index 5711d7e3a..4975495da 100644 --- a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs +++ b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs @@ -55,17 +55,18 @@ public sealed partial class FileeRendererConverter : IConverter, ICapabilityChec /// private static readonly string[] Documents = [ - "hwpx", "hwp", "docx", "docm", "dotx", "dotm", "doc", "dot", "wps", "odt", "rtf", "txt", "md", "html", "mhtml", + "hwpx", "hwp", "docx", "docm", "dotx", "dotm", "doc", "dot", "wps", "odt", "ott", "fodt", "rtf", "txt", "md", "html", "mhtml", "epub", "kepub", "mobi", "azw3", "azw", "prc", "fb2", "htmlz", "txtz", "chm", "pdb", "pml", "tcr", "rb", "snb", "oeb", "ipynb", "opml", "textile", "mediawiki", "rst", "org", "dbk", "csv", "tsv", "ofd", "udf", "mm", "xmind", ]; /// /// Word processor files left to LibreOffice when Settings → Engines chooses it for Word documents: the ones its chosen - /// edge reads (10 + 9, which loses to this engine's 1 + 9). The variants (DOCM, DOTX, DOTM, DOT, WPS) stay: LibreOffice - /// takes them only at its fallback cost, so without this engine they would go to the HWPX writer and rhwp instead. + /// edge reads (10 + 9, which loses to this engine's 1 + 9), the OpenDocument template and flat file among them. The + /// variants (DOCM, DOTX, DOTM, DOT, WPS) stay: LibreOffice takes them only at its fallback cost, so without this + /// engine they would go to the HWPX writer and rhwp instead. /// - private static readonly string[] WordProcessor = ["docx", "doc", "odt", "rtf"]; + private static readonly string[] WordProcessor = ["docx", "doc", "odt", "ott", "fodt", "rtf"]; /// /// Tables left to LibreOffice when Settings → Engines chooses it for spreadsheets, as XLSX goes to it. Not TSV, which @@ -148,7 +149,9 @@ public EngineStatus GetStatus() return HwpUnsupported(sourcePath); if (DocumentReaders.Checked(sourceFormat)) return DocReader.Unsupported(sourcePath) ?? DocUnsupported(sourcePath); - var markers = sourceFormat == "hwpx" ? HwpxMarkers : sourceFormat == "odt" ? OdtMarkers : WordFormats.Contains(sourceFormat) ? WordMarkers : null; + if (sourceFormat == "fodt") + return FlatUnsupported(sourcePath); + var markers = sourceFormat == "hwpx" ? HwpxMarkers : sourceFormat is "odt" or "ott" ? OdtMarkers : WordFormats.Contains(sourceFormat) ? WordMarkers : null; if (markers is null) return null; // Parts are read through the document limits (counted as they unpack, whatever their headers claim); a package @@ -274,6 +277,19 @@ private static bool HwpxChartsOnly(DocumentPackage package, string xml) } } + /// A flat OpenDocument text looked through as the ODT package it splits into (as an HWP is, above). + private string? FlatUnsupported(string sourcePath) + { + try + { + return Odf.OdfVariants.Read(sourcePath, "fodt", Path.GetTempPath(), package => FirstUnsupported(package, "odt")); + } + catch (Exception ex) when (ex is InvalidDataException or ConversionRefusedException) + { + return null; + } + } + /// A Word 97–2003 document looked through as the DOCX package it becomes (as an HWP is, above). private string? DocUnsupported(string sourcePath) { diff --git a/tests/Filee.Core.Tests/FormatFieldsTests.cs b/tests/Filee.Core.Tests/FormatFieldsTests.cs index 26baf228b..b54d49bd7 100644 --- a/tests/Filee.Core.Tests/FormatFieldsTests.cs +++ b/tests/Filee.Core.Tests/FormatFieldsTests.cs @@ -145,7 +145,7 @@ public void Grade_counts_match_the_published_table() new Dictionary { [FormatGrade.Everyday] = 19, - [FormatGrade.General] = 105, + [FormatGrade.General] = 106, [FormatGrade.Specialist] = 133, [FormatGrade.Legacy] = 52, }, diff --git a/tests/Filee.Core.Tests/OtgSettingsTests.cs b/tests/Filee.Core.Tests/OtgSettingsTests.cs new file mode 100644 index 000000000..ccbcd1e25 --- /dev/null +++ b/tests/Filee.Core.Tests/OtgSettingsTests.cs @@ -0,0 +1,36 @@ +using Filee.Core.Formats; +using Filee.Core.Presets; +using Filee.Core.Profiles; +using Filee.Core.Settings; + +namespace Filee.Core.Tests; + +public class OtgSettingsTests +{ + [Fact] + public void A_drawing_template_is_a_writable_vector_format_with_the_vector_donut() + { + Assert.Equal(("otg", FormatCategory.Vector, true), (FormatRegistry.Get("otg").Id, FormatRegistry.Get("otg").Category, FormatRegistry.Get("otg").Writable)); + Assert.Equal("vector", ProfileSelector.Select(BuiltInData.CreateProfiles(), ["Poster.otg"])?.Id); + // The flat files are written too now. + Assert.All(new[] { "fodt", "fods", "fodp", "fodg" }, id => Assert.True(FormatRegistry.Get(id).Writable, id)); + } + + [Fact] + public void Version_111_donuts_learn_otg_without_taking_custom_claims() + { + var presets = BuiltInData.CreatePresets(); + var profiles = BuiltInData.CreateProfiles(); + var donut = profiles.Single(p => p.Id == "vector").Extensions; + donut.Remove("otg"); + + Assert.True(SettingsMigrations.ApplyToLibrary(110, presets, profiles)); + Assert.Contains("otg", donut); + Assert.False(SettingsMigrations.ApplyToLibrary(110, presets, profiles)); + + donut.Remove("otg"); + profiles.Insert(0, new ToolbarProfile { Id = "my-templates", Name = "Templates", Extensions = ["OTG"] }); + Assert.False(SettingsMigrations.ApplyToLibrary(110, presets, profiles)); + Assert.DoesNotContain("otg", donut); // the user's own donut has it + } +} diff --git a/tests/Filee.Engines.Tests/DrawingConverterTests.cs b/tests/Filee.Engines.Tests/DrawingConverterTests.cs index d3160904a..8c999d954 100644 --- a/tests/Filee.Engines.Tests/DrawingConverterTests.cs +++ b/tests/Filee.Engines.Tests/DrawingConverterTests.cs @@ -115,7 +115,7 @@ public void The_drawing_engine_is_registered_with_its_formats() { var engine = Assert.Single(EngineRegistry.CreateAll(new Infrastructure.EngineEnvironment(_folder)).OfType()); Assert.Equal("drawing", engine.Id); - Assert.Equal(["vsd", "odg", "fodg", "xps", "crtx"], engine.Edges.Select(e => e.From).Distinct()); + Assert.Equal(["vsd", "odg", "otg", "fodg", "xps", "crtx"], engine.Edges.Select(e => e.From).Distinct()); Assert.True(engine.GetStatus().IsAvailable); Assert.Null(engine.FirstUnsupported(Path.Combine(_folder, "a.odg"), "odg")); } diff --git a/tests/Filee.Engines.Tests/OdfDrawTests.cs b/tests/Filee.Engines.Tests/OdfDrawTests.cs index 2199dd816..53abc5a8b 100644 --- a/tests/Filee.Engines.Tests/OdfDrawTests.cs +++ b/tests/Filee.Engines.Tests/OdfDrawTests.cs @@ -484,7 +484,7 @@ public async Task A_drawing_goes_through_the_drawing_converter_a_file_per_page(s public void Presentations_go_to_the_slide_renderer_and_drawings_to_the_drawing_converter() { Assert.All(new[] { "odp", "otp", "fodp" }, format => Assert.Contains(format, SlideFormats.Decks)); - Assert.Equal(["odg", "fodg"], DrawingFormats.BuiltIn.Select(f => f.Id).Where(OdfDrawReader.Drawings.Contains)); + Assert.Equal(["odg", "otg", "fodg"], DrawingFormats.BuiltIn.Select(f => f.Id).Where(OdfDrawReader.Drawings.Contains)); // ODP → PPTX stays with LibreOffice: the PPTX writer doesn't take OpenDocument presentations. Assert.DoesNotContain(new PptxWriterConverter().Edges, e => OdfDrawReader.Presentations.Contains(e.From)); } diff --git a/tests/Filee.Engines.Tests/OdfVariantFuzzTests.cs b/tests/Filee.Engines.Tests/OdfVariantFuzzTests.cs new file mode 100644 index 000000000..867f13865 --- /dev/null +++ b/tests/Filee.Engines.Tests/OdfVariantFuzzTests.cs @@ -0,0 +1,70 @@ +// Mutation fuzzing of the flat OpenDocument reader and writer (Odf/OdfFlat through OdfVariants): the seeds are the +// test document of OdtTests and a spreadsheet as flat files (XmlFuzzer, with tokens for pictures, objects and styles), +// and the test document as a package turned into a flat file (DocumentFuzzer). Every input must be converted or +// refused with InvalidDataException / ConversionRefusedException, in time and on a 1 MB stack, and what a flat file +// becomes must read. 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. + +using Filee.Engines.Hwp.Hwpx.Odt; +using Filee.Engines.Odf; +using Filee.Engines.Office; + +namespace Filee.Engines.Tests; + +[Trait("Category", "Fuzz")] +public class OdfVariantFuzzTests(EngineFixture fx) : IClassFixture +{ + /// Inputs per seed in a normal test run. + private const int QuickIterations = 40; + + private static readonly string[] Tokens = + [ + "iVBORw0KGgo=", "@@", + "R0lGODlhAQABAAAAACw=", + "x", + "", "", " xlink:href=\"Pictures/x.png\"", " xlink:href=\"../../x\"", " xlink:href=\"./Object 1\"", + "", + " office:mimetype=\"application/vnd.oasis.opendocument.spreadsheet\"", " text:style-name=\"P1\"", "", + " table:number-columns-repeated=\"100000\"", "&", "", + ]; + + [Fact] + public void Flat_files_convert_or_are_refused_after_any_mutation() + { + var folder = fx.NewFolder(); + var odt = OdtTests.DocumentBuilder().Save(Path.Combine(folder, "seed.odt")); + var fodt = Path.Combine(folder, "seed.fodt"); + OdfVariants.Convert(odt, "odt", fodt, "fodt", cancellationToken: TestContext.Current.CancellationToken); + new FuzzHarness(fx, QuickIterations, 20261120).Files("fodt", "fodt", [File.ReadAllBytes(fodt)], FuzzHarness.Xml(Tokens), (path, media) => + { + var package = Path.Combine(Path.GetDirectoryName(path)!, "flat.odt"); + OdfVariants.Convert(path, "fodt", package, "odt"); + OdtReader.Read(package, media); + File.Delete(package); + }); + } + + [Fact] + public void Flat_spreadsheets_read_or_are_refused_after_any_mutation() + { + const string Sheet = + "" + + "" + + "Flat cell" + + "42"; + new FuzzHarness(fx, QuickIterations, 20261120).Files("fods", "fods", [System.Text.Encoding.UTF8.GetBytes(Sheet)], FuzzHarness.Xml(Tokens), + (path, _) => SpreadsheetConverter.Read(path, "fods")); + } + + [Fact] + public void Packages_become_flat_files_or_are_refused_after_any_mutation() + { + var seed = File.ReadAllBytes(OdtTests.DocumentBuilder().Save(Path.Combine(fx.NewFolder(), "seed.odt"))); + new FuzzHarness(fx, QuickIterations, 20261120).Files("odt-flat", "odt", [seed], FuzzHarness.Packages, (path, _) => + { + var flat = Path.Combine(Path.GetDirectoryName(path)!, "package.fodt"); + OdfVariants.Convert(path, "odt", flat, "fodt"); + File.Delete(flat); + }); + } +} diff --git a/tests/Filee.Engines.Tests/OdfVariantTests.cs b/tests/Filee.Engines.Tests/OdfVariantTests.cs new file mode 100644 index 000000000..5928fe97a --- /dev/null +++ b/tests/Filee.Engines.Tests/OdfVariantTests.cs @@ -0,0 +1,399 @@ +// The OpenDocument templates and flat files without LibreOffice (Odf/OdfVariants, Odf/OdfFlat, OdfVariantConverter): +// packages turned into templates and flat files and back keep their parts, the readers read every form, the routes +// take the built-in engines, and hostile flat files and packages are refused through the document limits in time. +// Every input is built in code. + +using System.IO.Compression; +using System.Text; +using System.Xml.Linq; +using Filee.Core.Conversion; +using Filee.Core.Presets; +using Filee.Engines.Documents; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Hwp.Hwpx.Odt; +using Filee.Engines.Infrastructure; +using Filee.Engines.Odf; +using Filee.Engines.Odf.Draw; +using Filee.Engines.Office; +using Filee.Engines.Office.Sheets; + +namespace Filee.Engines.Tests; + +public class OdfVariantTests(EngineFixture fx) : IClassFixture +{ + private static readonly XNamespace Office = "urn:oasis:names:tc:opendocument:xmlns:office:1.0"; + private static readonly XNamespace Style = "urn:oasis:names:tc:opendocument:xmlns:style:1.0"; + private static readonly XNamespace Draw = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"; + private static readonly XNamespace XLink = "http://www.w3.org/1999/xlink"; + private static readonly XNamespace Manifest = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"; + + private static CancellationToken Token => TestContext.Current.CancellationToken; + + private string Odt(OdtBuilder? builder = null) => (builder ?? OdtTests.DocumentBuilder()).Save(Path.Combine(fx.NewFolder(), "doc.odt")); + + private static string Convert(string input, string from, string to, DocumentLimits? limits = null) + { + var output = Path.Combine(Path.GetDirectoryName(input)!, $"out-{Guid.NewGuid():N}.{to}"); + OdfVariants.Convert(input, from, output, to, limits, Token); + return output; + } + + private static string Text(HDocument document) => PlainTextWriter.Write(document); + + private string Media() => Path.Combine(fx.NewFolder(), "media"); + + private static string Mimetype(string package) + { + using var zip = ZipFile.OpenRead(package); + using var reader = new StreamReader(zip.GetEntry("mimetype")!.Open()); + return reader.ReadToEnd(); + } + + private static byte[] Part(string package, string name) + { + using var zip = ZipFile.OpenRead(package); + using var stream = zip.GetEntry(name)!.Open(); + using var copy = new MemoryStream(); + stream.CopyTo(copy); + return copy.ToArray(); + } + + // ───────────────────────── Text ───────────────────────── + + [Fact] + public void A_text_becomes_a_flat_file_and_back_with_its_text_and_pictures() + { + var odt = Odt(); + var flat = Convert(odt, "odt", "fodt"); + + var root = XDocument.Load(flat).Root!; + Assert.Equal(Office + "document", root.Name); + Assert.Equal("application/vnd.oasis.opendocument.text", (string?)root.Attribute(Office + "mimetype")); + Assert.Equal(["meta", "styles", "automatic-styles", "master-styles", "body"], root.Elements().Select(e => e.Name.LocalName)); + // The picture is in the XML, not a link to a part. + Assert.NotEmpty(root.Descendants(Draw + "image").Elements(Office + "binary-data")); + Assert.DoesNotContain(root.Descendants(Draw + "image"), e => ((string?)e.Attribute(XLink + "href"))?.StartsWith("Pictures/", StringComparison.Ordinal) == true); + + var expectedMedia = Media(); + var expected = OdtReader.Read(odt, expectedMedia); + var media = Media(); + var fromFlat = OdtReader.Read(Convert(flat, "fodt", "odt"), media); + Assert.Equal(Text(expected), Text(fromFlat)); + Assert.Equal(expected.Title, fromFlat.Title); + Assert.Equal(expected.Sections.Count, fromFlat.Sections.Count); + // The pictures, back out of the XML. + Assert.Equal(Directory.GetFiles(expectedMedia).Select(File.ReadAllBytes).Select(b => b.Length).Order(), + Directory.GetFiles(media).Select(File.ReadAllBytes).Select(b => b.Length).Order()); + } + + [Fact] + public void A_text_becomes_a_template_with_only_its_media_type_changed() + { + var odt = Odt(); + var ott = Convert(odt, "odt", "ott"); + + Assert.Equal("application/vnd.oasis.opendocument.text-template", Mimetype(ott)); + using (var zip = ZipFile.OpenRead(ott)) + { + Assert.Equal("mimetype", zip.Entries[0].FullName); + Assert.Equal(zip.Entries[0].Length, zip.Entries[0].CompressedLength); // stored + } + var manifest = XDocument.Parse(Encoding.UTF8.GetString(Part(ott, "META-INF/manifest.xml"))).Root!; + Assert.Equal("application/vnd.oasis.opendocument.text-template", + (string?)manifest.Elements(Manifest + "file-entry").Single(e => (string?)e.Attribute(Manifest + "full-path") == "/").Attribute(Manifest + "media-type")); + foreach (var name in new[] { "content.xml", "styles.xml", "meta.xml", "Pictures/chart.png" }) + Assert.Equal(Part(odt, name), Part(ott, name)); + + var back = Convert(ott, "ott", "odt"); + Assert.Equal("application/vnd.oasis.opendocument.text", Mimetype(back)); + Assert.Equal(Text(OdtReader.Read(odt, Media())), Text(OdtReader.Read(back, Media()))); + } + + [Fact] + public async Task The_document_readers_read_the_template_and_the_flat_file() + { + var odt = Odt(); + var expected = Text(await DocumentReaders.ReadAsync(odt, "odt", fx.NewFolder(), Token)); + var ott = Convert(odt, "odt", "ott"); + var fodt = Convert(odt, "odt", "fodt"); + var work = fx.NewFolder(); + + Assert.Equal(expected, Text(await DocumentReaders.ReadAsync(ott, "ott", fx.NewFolder(), Token))); + Assert.Equal(expected, Text(await DocumentReaders.ReadAsync(fodt, "fodt", work, Token))); + // The package the flat file was read as is gone; only the pictures stay with the job. + Assert.DoesNotContain(Directory.GetFiles(work), f => f.EndsWith(".odt", StringComparison.Ordinal)); + Assert.Null(new Typesetting.FileeRendererConverter(() => false).FirstUnsupported(fodt, "fodt")); + } + + [Fact] + public void Automatic_styles_of_both_parts_keep_apart_in_the_flat_file() + { + // P1 is a content style and a header style: the header's one is renamed, with the header's reference. + var builder = new OdtBuilder + { + ContentStyles = "", + StylesAutomatic = "" + + "", + MasterStyles = "head", + Body = "body", + }; + var root = XDocument.Load(Convert(Odt(builder), "odt", "fodt")).Root!; + var automatic = root.Element(Office + "automatic-styles")!.Elements().Select(e => (string?)e.Attribute(Style + "name")).ToList(); + Assert.Equal(["P1", "PM1", "MP1"], automatic); + var header = root.Element(Office + "master-styles")!.Descendants().Single(e => e.Name.LocalName == "p"); + Assert.Equal("MP1", (string?)header.Attribute("{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name")); + Assert.Equal("P1", (string?)root.Element(Office + "body")!.Descendants().Single(e => e.Name.LocalName == "p").Attribute("{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name")); + } + + [Fact] + public void An_embedded_chart_goes_into_the_flat_file_and_out_again() + { + var builder = new OdtBuilder + { + Body = "" + + "", + }; + builder.Parts["Object 1/content.xml"] = Encoding.UTF8.GetBytes(OdfChartBuilder.Columns()); + builder.Parts["ObjectReplacements/Object 1"] = DocumentFuzzTests.TinyPng(4, 3); + var odt = Odt(builder); + + var flat = Convert(odt, "odt", "fodt"); + var inner = XDocument.Load(flat).Root!.Descendants(Draw + "object").Single().Element(Office + "document"); + Assert.NotNull(inner); + Assert.NotNull(inner.Element(Office + "body")?.Element(Office + "chart")); + + var back = Convert(flat, "fodt", "odt"); + using var zip = ZipFile.OpenRead(back); + Assert.NotNull(zip.GetEntry("Object 1/content.xml")); + var manifest = XDocument.Load(zip.GetEntry("META-INF/manifest.xml")!.Open()).Root!; + Assert.Contains(manifest.Elements(Manifest + "file-entry"), e => (string?)e.Attribute(Manifest + "full-path") == "Object 1/" + && (string?)e.Attribute(Manifest + "media-type") == "application/vnd.oasis.opendocument.chart"); + Assert.Null(new Typesetting.FileeRendererConverter(() => false).FirstUnsupported(back, "odt")); + } + + // ───────────────────────── Spreadsheets, presentations, drawings ───────────────────────── + + private string Ods() + { + var book = new Workbook(); + var sheet = new Worksheet("Data"); + sheet.Set(0, 0, new SheetCell("Flat cell", CellStyle.Default)); + sheet.Set(0, 1, new SheetCell("42", CellStyle.Default, IsNumber: true) { Value = 42 }); + book.Sheets.Add(sheet); + var path = Path.Combine(fx.NewFolder(), "sheet.ods"); + OdsWriter.Write(book, path); + return path; + } + + [Theory] + [InlineData("ots")] + [InlineData("fods")] + public void A_spreadsheet_template_and_flat_file_read_as_the_spreadsheet(string form) + { + var ods = Ods(); + var variant = Convert(ods, "ods", form); + + var book = SpreadsheetConverter.Read(variant, form); + Assert.Equal("Data", book.Sheets.Single().Name); + Assert.Equal("Flat cell", book.Sheets[0].Get(0, 0)?.Text); + Assert.Equal("42", book.Sheets[0].Get(0, 1)?.Text); + Assert.Null(SheetCapabilities.FirstUnsupported(variant, form)); + Assert.Equal("Flat cell", SpreadsheetConverter.Read(Convert(variant, form, "ods"), "ods").Sheets[0].Get(0, 0)?.Text); + } + + [Theory] + [InlineData("otp")] + [InlineData("fodp")] + public void A_presentation_template_and_flat_file_keep_their_slides(string form) + { + var odp = OdfDrawFuzzTests.Costly().Save(Path.Combine(fx.NewFolder(), "deck.odp")); + var expected = OdfDrawReader.Read(odp, cancellationToken: Token); + + var variant = Convert(odp, "odp", form); + var deck = OdfDrawReader.Read(variant, cancellationToken: Token); + Assert.Equal(expected.Slides.Count, deck.Slides.Count); + Assert.Equal(expected.Slides.Select(s => s.Elements.Count), deck.Slides.Select(s => s.Elements.Count)); + var back = OdfDrawReader.Read(Convert(variant, form, "odp"), cancellationToken: Token); + Assert.Equal(expected.Slides.Select(s => s.Elements.Count), back.Slides.Select(s => s.Elements.Count)); + } + + [Fact] + public void A_flat_drawing_becomes_a_drawing_template() + { + var flat = new OdpBuilder { Drawing = true, Pages = "" } + .SaveFlat(Path.Combine(fx.NewFolder(), "shape.fodg")); + var otg = Convert(flat, "fodg", "otg"); + Assert.Equal("application/vnd.oasis.opendocument.graphics-template", Mimetype(otg)); + Assert.Single(OdfDrawReader.Read(otg, cancellationToken: Token).Slides); + } + + [Fact] + public async Task A_pptx_converts_to_a_presentation_template_and_a_flat_presentation() + { + var pptx = OdpWriterTests.Sample().Save(Path.Combine(fx.NewFolder(), "deck.pptx")); + foreach (var form in new[] { "otp", "fodp" }) + { + var job = await fx.ConvertAsync([pptx], new Preset { TargetFormat = form }); + Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}"))); + Assert.Equal(3, OdfDrawReader.Read(job.Outputs.Single(), cancellationToken: Token).Slides.Count); + } + } + + [Fact] + public async Task A_word_document_converts_to_a_text_template_and_a_flat_text() + { + var odt = Odt(); + var expected = Text(OdtReader.Read(odt, Media())).Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0]; + var docx = Path.Combine(fx.NewFolder(), "doc.docx"); + var toDocx = await fx.ConvertAsync([odt], new Preset { TargetFormat = "docx" }); + File.Copy(toDocx.Outputs.Single(), docx); + foreach (var form in new[] { "ott", "fodt" }) + { + var job = await fx.ConvertAsync([docx], new Preset { TargetFormat = form }); + Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}"))); + var document = await DocumentReaders.ReadAsync(job.Outputs.Single(), form, fx.NewFolder(), Token); + Assert.Contains(expected, Text(document), StringComparison.Ordinal); + } + } + + // ───────────────────────── Hostile files ───────────────────────── + + private static readonly string Namespaces = OdtBuilder.Namespaces; + + private string Flat(string body, string mimetype = "application/vnd.oasis.opendocument.text", string name = "hostile.fodt") + { + var path = Path.Combine(fx.NewFolder(), name); + File.WriteAllText(path, $"" + + $"{body}"); + return path; + } + + private static string Picture(byte[] bytes) => + $"{System.Convert.ToBase64String(bytes)}"; + + private static ConversionRefusedException RefusedQuickly(Action action) + { + var watch = System.Diagnostics.Stopwatch.StartNew(); + var refusal = Assert.Throws(action); + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"took {watch.Elapsed}"); + Assert.Equal(DocumentLimits.ErrorKey, refusal.ErrorKey); + return refusal; + } + + [Fact] + public void A_flat_file_past_its_size_is_refused_before_it_is_read() + { + var limits = DocumentLimits.Default with { MaxPartBytes = 1024 }; + var flat = Flat(string.Concat(Enumerable.Repeat("words words words", 500))); + Assert.Contains("larger than", RefusedQuickly(() => Convert(flat, "fodt", "odt", limits)).Detail); + } + + [Fact] + public void Pictures_in_a_flat_file_count_against_the_part_package_and_entry_limits() + { + // One picture larger than a part may be. + var limits = DocumentLimits.Default with { MaxPartBytes = 64 * 1024 }; + var big = Flat(Picture([.. DocumentFuzzTests.TinyPng(2, 2), .. new byte[100 * 1024]])); + Assert.Contains("A picture stored in the file", RefusedQuickly(() => Convert(big, "fodt", "odt", limits)).Detail); + + // More pictures than a package may have parts. + var many = Flat(string.Concat(Enumerable.Range(0, 40).Select(i => Picture([.. DocumentFuzzTests.TinyPng(2, 2), (byte)i])))); + Assert.Contains("pictures and objects", RefusedQuickly(() => Convert(many, "fodt", "odt", DocumentLimits.Default with { MaxPackageEntries = 10 })).Detail); + + // Together more than a package may unpack to. + var sum = Flat(string.Concat(Enumerable.Repeat(Picture([.. DocumentFuzzTests.TinyPng(2, 2), .. new byte[20 * 1024]]), 10))); + Assert.Contains("together", RefusedQuickly(() => Convert(sum, "fodt", "odt", DocumentLimits.Default with { MaxPackageBytes = 100 * 1024 })).Detail); + + // Not base64: the picture is left out, the text converts. + var broken = Flat("kept@@not base64@@"); + Assert.Contains("kept", Text(OdtReader.Read(Convert(broken, "fodt", "odt"), Media())), StringComparison.Ordinal); + } + + [Fact] + public void Deep_and_large_flat_xml_is_refused_by_the_xml_limits() + { + var deep = Flat("" + string.Concat(Enumerable.Repeat("", 200)) + "x" + string.Concat(Enumerable.Repeat("", 200)) + ""); + RefusedQuickly(() => Convert(deep, "fodt", "odt", DocumentLimits.Default with { MaxXmlDepth = 64 })); + var wide = Flat(string.Concat(Enumerable.Repeat("", 5000))); + RefusedQuickly(() => Convert(wide, "fodt", "odt", DocumentLimits.Default with { MaxXmlElements = 1000 })); + } + + [Fact] + public void Objects_nested_without_end_stop_at_the_object_depth() + { + var inner = "bottom"; + for (var i = 0; i < 40; i++) + inner = $"{inner}"; + var odt = Convert(Flat(inner), "fodt", "odt"); + using var zip = ZipFile.OpenRead(odt); + Assert.InRange(zip.Entries.Count(e => e.FullName.EndsWith("/content.xml", StringComparison.Ordinal)), 1, 8); + } + + [Fact] + public void A_picture_used_again_and_again_cant_make_the_flat_file_grow_without_end() + { + var builder = new OdtBuilder { Body = string.Concat(Enumerable.Repeat("", 500)) }; + builder.Parts["Pictures/p.png"] = [.. DocumentFuzzTests.TinyPng(2, 2), .. new byte[40 * 1024]]; + var odt = Odt(builder); + var limits = DocumentLimits.Default with { MaxPartBytes = 1024 * 1024 }; + Assert.Contains("flat file would be larger", RefusedQuickly(() => Convert(odt, "odt", "fodt", limits)).Detail); + } + + [Fact] + public void Another_type_and_encrypted_packages_are_refused_with_their_reason() + { + var sheet = Flat("x", "application/vnd.oasis.opendocument.spreadsheet"); + Assert.Contains("not an OpenDocument text", Assert.Throws(() => Convert(sheet, "fodt", "odt")).Message); + Assert.Contains("not an OpenDocument spreadsheet", Assert.Throws(() => Convert(Odt(), "ods", "ots")).Message); + var notXml = Path.Combine(fx.NewFolder(), "bad.fodt"); + File.WriteAllText(notXml, "(() => Convert(notXml, "fodt", "odt")); + + var encrypted = new OdtBuilder { ManifestEntries = "" }; + Assert.Contains("password", Assert.Throws(() => Convert(Odt(encrypted), "odt", "fodt")).Message); + } +} + +[Collection("EngineEnvironment")] // the Filee renderer's switch is process-wide +public sealed class OdfVariantRouteTests(EngineFixture fx) : IClassFixture, IDisposable +{ + public void Dispose() + { + EngineEnvironment.FileeRenderer = false; + fx.Catalog.Refresh(); + } + + [Fact] + public void Routes_take_the_built_in_engines_before_libreoffice() + { + EngineEnvironment.FileeRenderer = true; + fx.Catalog.Refresh(); + var planner = fx.Catalog.CreatePlanner(["libreoffice"]); + string[] Engines(string from, string to) => [.. planner.Plan(from, to)!.Steps.Select(s => s.Converter.Id)]; + + Assert.Equal(["odf-variants"], Engines("odt", "ott")); + Assert.Equal(["odf-variants"], Engines("odt", "fodt")); + Assert.Equal(["odf-variants"], Engines("fodt", "ott")); + Assert.Equal(["odf-variants"], Engines("ods", "fods")); + Assert.Equal(["odf-variants"], Engines("otp", "odp")); + Assert.Equal(["odf-variants"], Engines("odg", "fodg")); + Assert.Equal(["odt-writer", "odf-variants"], Engines("docx", "ott")); + Assert.Equal(["odt-writer", "odf-variants"], Engines("md", "fodt")); + Assert.Equal(["spreadsheet", "odf-variants"], Engines("xlsx", "ots")); + Assert.Equal(["odp-writer"], Engines("pptx", "otp")); + Assert.Equal(["odp-writer"], Engines("pptx", "fodp")); + Assert.Equal(["layout"], Engines("fodt", "pdf")); + Assert.Equal(["layout"], Engines("ott", "pdf")); + Assert.Equal(["docx-writer"], Engines("fodt", "docx")); + Assert.Equal(["sheet-print"], Engines("fods", "pdf")); + Assert.Equal(["sheet-print"], Engines("ots", "pdf")); + Assert.Equal(["spreadsheet"], Engines("fods", "xlsx")); + Assert.Equal(["drawing"], Engines("otg", "pdf")); + + // The forms of one type only, never one flat file to another. + var variants = fx.Converters.Single(c => c.Id == "odf-variants"); + Assert.Equal(24, variants.Edges.Count); + Assert.All(variants.Edges, e => Assert.Equal(OdfVariants.Find(e.From)!.Family, OdfVariants.Find(e.To)!.Family)); + Assert.All(variants.Edges, e => Assert.True(Core.Formats.FormatRegistry.Get(e.To).Writable, e.To)); + } +} From 61607b024315b7d1eb908e923bbdd9959cef590f Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 18:06:14 +0900 Subject: [PATCH 2/6] Add a built-in RTF writer; read RTF font alternates and default tabs - RtfWriter writes RTF from the document model: fonts with \falt, colours, styles, lists, tables with merged, shaded and bordered cells and nested tables, pictures (PNG/JPEG kept, EMF/WMF as metafiles, others as PNG), text boxes and shapes, headers and footers, sections, links, fields, bookmarks and notes; text beyond ASCII as \uN with a code-page fallback and \ucN. Output size, tables, pictures and nesting stay within DocumentLimits. - RtfWriterConverter (rtf-writer) takes every built-in reader's format to RTF at cost 8, ahead of Pandoc and LibreOffice. - RTF reader: font alternates (\falt), \deftab (720 twips by default), tab stops (\tx with kind and leader), more list number formats, \header and \footer as every page, \clmrg after a \clvmrg cell, and the space after a note's number. - ODT reader: default tab interval from the default paragraph style (LibreOffice's 1.25 cm otherwise), from the indent unless settings.xml turns TabsRelativeToIndent off; the layout honours it. Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 81 +- src/Filee.App/Assets/i18n/ar.json | 2 + src/Filee.App/Assets/i18n/bn-BD.json | 2 + 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 | 2 + 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 | 2 + src/Filee.App/Assets/i18n/zh-CN.json | 2 + src/Filee.App/Assets/i18n/zh-TW.json | 2 + src/Filee.Engines/EngineRegistry.cs | 1 + src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs | 12 +- src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs | 30 + .../Hwp/Hwpx/RtfReader.Tables.cs | 11 + src/Filee.Engines/Hwp/Hwpx/RtfReader.cs | 86 +- src/Filee.Engines/Hwp/Hwpx/RtfWriter.Body.cs | 727 ++++++++++++++++ .../Hwp/Hwpx/RtfWriter.Objects.cs | 323 ++++++++ src/Filee.Engines/Hwp/Hwpx/RtfWriter.cs | 777 ++++++++++++++++++ .../Office/RtfWriterConverter.cs | 46 ++ src/Filee.Engines/Typesetting/PageComposer.cs | 1 + .../Typesetting/ParagraphLayout.cs | 12 +- .../DefaultTabStopTests.cs | 91 ++ .../LibreOfficeRouteTests.cs | 9 +- tests/Filee.Engines.Tests/RtfWriterTests.cs | 466 +++++++++++ 38 files changed, 2694 insertions(+), 25 deletions(-) create mode 100644 src/Filee.Engines/Hwp/Hwpx/RtfWriter.Body.cs create mode 100644 src/Filee.Engines/Hwp/Hwpx/RtfWriter.Objects.cs create mode 100644 src/Filee.Engines/Hwp/Hwpx/RtfWriter.cs create mode 100644 src/Filee.Engines/Office/RtfWriterConverter.cs create mode 100644 tests/Filee.Engines.Tests/DefaultTabStopTests.cs create mode 100644 tests/Filee.Engines.Tests/RtfWriterTests.cs diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 8046fdbd1..721bf5ccf 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -20,6 +20,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | Fonts | built in | **TTF, OTF, WOFF, WOFF2, EOT ↔ each other**; **FON (NE monochrome FNT v2/v3, supported single-byte charsets) → TTF, OTF, WOFF, WOFF2, EOT only**; CFF (PostScript) outlines become TrueType for TTF and EOT | | **HWPX writer** | built in | **HWP, DOCX (+ DOCM/DOTX/DOTM), DOC (+ DOT, WPS Office WPS), ODT, XLSX (+ XLSM/XLTX), XLS, ODS, CSV, TSV, PPTX (+ PPTM/POTX/POTM/PPSX), PPT, ODP (+ OTP, FODP), PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2, CHM, PDB, PML, TCR, RB, SNB, OEB, HWPX, TXT, Markdown, RTF, mind maps (MM, XMind) → HWPX** (and with rhwp → PDF and images); reStructuredText, LaTeX → HWPX with Pandoc | | **DOCX writer** | built in | **DOC (+ DOT, WPS Office WPS), ODT, HWPX, HWP, PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), Markdown, TXT, RTF, XLSX, XLS, ODS, CSV, PPTX (+ variants), PPT, ODP (+ OTP, FODP), mind maps (MM, XMind) → DOCX** | +| **RTF writer** | built in | **DOCX (+ variants), DOC (+ DOT, WPS), ODT, HWPX, HWP, PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), Markdown, TXT, XLSX, XLS, ODS, CSV, PPTX (+ variants), PPT, ODP (+ OTP, FODP), mind maps (MM, XMind) → RTF** (Rich Text Format, see [RTF writer](#rtf-writer)) | | **ODT writer** | built in | **DOCX (+ variants), DOC (+ DOT, WPS), HWPX, HWP, PDF, OFD, UYAP (UDF), HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), Markdown, TXT, RTF, XLSX, XLS, ODS, CSV, PPTX (+ variants), PPT, ODP (+ OTP, FODP), mind maps (MM, XMind) → ODT** (OpenDocument text, LibreOffice Writer) | | **PPTX writer** | built in | **Markdown, DOCX (+ variants), DOC (+ DOT, WPS), ODT, HWPX, HWP, OFD, HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), TXT, RTF, XLSX, XLS, ODS, CSV → PPTX slides** (a slide per heading, Pandoc's rules; Pandoc's markup languages too when Pandoc is installed); **PPT → PPTX** as the [PPT reader](#ppt-reader) translates it | | **ODP writer** | built in | **PPTX (+ PPTM/POTX/POTM/PPSX), PPT → ODP** with masters, shapes, pictures, tables, lists and speaker notes; **Markdown, DOCX (+ variants), DOC (+ DOT, WPS), ODT, HWPX, HWP, OFD, HTML, MHTML, EPUB, MOBI/AZW3, FB2 (and the older e-books), TXT, RTF, XLSX, XLS, ODS, CSV → ODP slides** laid out as the PPTX writer lays them out (see [ODP writer](#odp-writer)); a deck with what the slide model leaves out (charts without a picture, effects, 3D) keeps LibreOffice's route | @@ -43,7 +44,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | rhwp | bundled with the installer (`engines/rhwp`, with the Microsoft C++ runtime, see below) | HWP/HWPX → PDF when the Filee renderer is off or can't draw a file yet, **HWPX → HWP** (HWP → HWPX too, ranked below the built-in HWP reader) | | Archives | built in (SharpCompress, DiscUtils, .NET and Filee's own readers and writers); 7-Zip optional (Settings → *Engines*, ~2 MB download) | **in-process**: ZIP (including ANSYS Workbench WBPZ and Adobe ZXP, and Windows app packages MSIX / APPX), Minecraft and Modrinth packs, Garry's Mod addons (GMA), Valve paks (VPK), the files packed into Source maps (BSP), 7Z (solid ones too), RAR, TAR (+ GZ/BZ2/XZ/Z/7Z/LZ/ZST), GZ, BZ2, XZ, LZMA, Z, ZST, lzip, CPIO, DEB, RPM, ISO (UDF discs too), IMG and DMG disk images, split (.001), ALZ, EGG, CAB, LHA, ARJ; **7-Zip, when installed**: the rare variants the in-process readers leave to it (see [Archives](#archives)) **→ folder, ZIP, 7Z, TAR, TAR.GZ, TAR.BZ2, TAR.XZ, BZ2, XZ, the packs, GMA** (in-process); "Compress into one archive" for any files | | LibreOffice | **optional download** (~370 MB, 1.1 GB on disk) | older and rare formats only: Word 6.0 / 95 and Works documents, PowerPoint 95 and older PPT, binary Visio (`.vsd`), and ODP / ODG with what Filee doesn't draw yet (charts, OLE objects, 3D) → PDF; XLS, PPT, RTF and OpenDocument to each other, output as DOCX/ODT/ODS, and ODP for what the [ODP writer](#odp-writer) doesn't take (OpenDocument presentations themselves, Keynote, decks with charts, effects or 3D); read-only formats (see below) → PDF and their kind's editable formats; FODT, OpenDocument templates (OTT, OTS, OTP) and XLT out; SVG → EMF/WMF/ODG, a PDF page → SVG | -| Pandoc | **optional download** (~42 MB, 240 MB on disk) | Markdown ↔ DOCX/ODT/RTF, DOCX/ODT/HTML/RTF → Markdown, HTML ↔ DOCX/ODT, markup languages (LaTeX, AsciiDoc, Typst, man; and RTF / EPUB from the built-in ones) ↔ Markdown/HTML/DOCX/ODT and each other (and → RTF, EPUB, TXT); their AST feeds the built-in PPTX writer (Pandoc's own slides only where Filee assumes a Pandoc that isn't installed) | +| Pandoc | **optional download** (~42 MB, 240 MB on disk) | Markdown ↔ DOCX/ODT, DOCX/ODT/HTML/RTF → Markdown, HTML ↔ DOCX/ODT, markup languages (LaTeX, AsciiDoc, Typst, man; and RTF / EPUB from the built-in ones) ↔ Markdown/HTML/DOCX/ODT and each other (and → RTF, EPUB, TXT); their AST feeds the built-in PPTX writer (Pandoc's own slides only where Filee assumes a Pandoc that isn't installed) | | Ghostscript | **optional download** (~20 MB, 31 MB on disk) | EPS/PS → PDF, PostScript-based AI → PDF (images through PDF and PDFium), EPS/PS → PNG; PDF → EPS/PS only when moved above the built-in PostScript writer; smaller PDFs when a preset asks for it; PDF/A-2b for PDF/A presets; renders CMYK PDF/AI pages for CMYK JPG/TIFF | | Tesseract | **optional download** (~50 MB, 160 MB on disk; each language 0.4–10 MB when first used) | **text recognition (OCR)**: scans and pictures → searchable PDF, and their text into DOCX, TXT, Markdown and HWPX ("Recognize text" in the preset, see below) | | Apple Vision | part of macOS (Mac only) | **text recognition (OCR) on a Mac**, the same step as Tesseract without a download; Tesseract reads the languages Vision doesn't (see "Apple Vision on macOS") | @@ -774,11 +775,15 @@ those shapes, which `Read` leaves out as before; it also takes an OLE object as - **RTF** (`RtfReader.cs`, no Pandoc needed) as Word, WordPad, LibreOffice and 한글 write it: text in the code page of its font (`\fcharset`, `\cpg`) or the document's (`\ansicpg`, so Korean, Chinese and Japanese RTF reads right) and `\uN` characters; bold, italic, underline, strike-through, raised / lowered, size, font, colour and highlight; - alignment, indents, spacing, line spacing and keep options; the style sheet (headings by `heading N` or + alignment, indents, spacing, line spacing, keep options and tab stops (`\tx` with their kind and leader, measured + from the indent as the model has them); the font table's other names (`{\*\falt …}`, used like Word's + `w:altName` where a font is not installed) and the default tab interval (`\deftab`, Word's 720 twips when the file + gives none, from the margin); the style sheet (headings by `heading N` or `\outlinelevel`); lists from `\listtable` / `\listoverridetable`; tables (`\trowd` … `\row`, merged and shaded cells, borders as a grid or none, nested tables flattened into their cell); PNG, JPEG, EMF and WMF pictures sized - by `\picwgoal`; footnotes and endnotes; hyperlink, page and page count fields; bookmarks; headers and footers - (first-page ones are left out); sections with their paper, margins, orientation and columns. Unknown `\*` + by `\picwgoal`; footnotes and endnotes (the space after a note's own number is dropped, as in the DOCX reader); + hyperlink, page and page count fields; bookmarks; headers and footers (`\header` / `\footer` for every page, + `\headerr` / `\headerl` for odd and even ones; first-page ones are left out); sections with their paper, margins, orientation and columns. Unknown `\*` destinations, index entries, comments and object data are skipped; group depth, paragraphs and pictures count against the reader limits. - Markdown, HTML and Pandoc keep structure only, so page setup comes from the built-in template (A4). @@ -1737,7 +1742,10 @@ specification, so ODT → DOCX / HWPX / PDF / Markdown / HTML and anything → O text boxes, as characters or floating with position, relation, wrap, outer margins, border and padding; rectangles, rounded rectangles, ellipses (also LibreOffice's custom shapes of those kinds) and lines, with fill and stroke; shapes with text become text boxes; formulas (embedded Math objects) become their StarMath text; -- footnotes and endnotes with their numbering (`text:notes-configuration`). +- footnotes and endnotes with their numbering (`text:notes-configuration`); +- default tab stops as LibreOffice sets them: every `style:tab-stop-distance` of the default paragraph style, else + LibreOffice's 1.25 cm, counted from the paragraph's indent unless `settings.xml` has `TabsRelativeToIndent` false + (documents that came from Word); the layout engine then puts a tab after the text at the next of them. The package is read within DocumentLimits (see **Limits** under *HWPX reader*): `DocumentPackage` counts every part, SafeXml reads the XML, `DocumentBudget` counts paragraphs, objects and nesting (tables, notes, frames, groups and lists); @@ -1762,6 +1770,64 @@ of headings (`text:outline-style`), different first-page headers with content, g start colour), rotated or cropped pictures, charts and other OLE objects, drop caps, change tracking, fields other than page number / count (their text is kept), flat ODT (`.fodt`) and templates (`.ott`, still LibreOffice's). +## RTF writer + +`Hwp/Hwpx/RtfWriter*.cs` writes Rich Text Format from the document model, by hand from the RTF 1.9 specification, so +anything Filee reads becomes an RTF file without LibreOffice or Pandoc. `RtfWriterConverter` (engine `rtf-writer`) +offers every built-in reader's format → RTF at cost 8 (PDF at 14), like the DOCX and ODT writers, so it wins over +Pandoc (Markdown and the markup languages → RTF) and LibreOffice (Word, ODT, HWP → RTF). + +- **The file:** `{\rtf1\ansi\ansicpg1252\deff0\uc1` with the font table (each font with its `\fcharset` and, where + one is known, its other name in `{\*\falt …}`: the document's own (DOCX `w:altName`), the Korean or English name + of a Korean font, a font of the same widths), the colour table, Normal and `heading 1`..`heading 9` with outline + levels, `\listtable` / `\listoverridetable`, `\info` (title, authors), the first section's page, `\deftab` (the + model's default tab interval, else 한글's 40 pt) and note numbering. The body is written first into a temporary + file beside the output (the tables fill as it is written), then the header and the body go into the output through + a temporary file of their own. +- **Text:** ASCII only. Other characters are `\uN` (negative past 32767, surrogate pairs as two) followed by the + same character in the code page of its font's `\fcharset`, with `\ucN` saying how many bytes that is (two for + Korean in CP949), or `?` where the code page has none. Text is split where its script changes: Hangul, kana and + Chinese characters in the East Asian font with an East Asian `\fcharset` (which Word and Filee's reader take as + the East Asian font), Greek, Cyrillic, Hebrew, Arabic and Thai in the Latin font with their own `\fcharset`; + spaces and punctuation stay with the text around them. Arabic and Hebrew pieces are `\rtlch`. +- **Paragraphs** repeat their style's formatting after `\pard\plain` (RTF paragraphs carry it themselves), then + alignment, indents (`\lin` / `\rin` too, for right-to-left paragraphs), spacing, line spacing (as the DOCX writer + converts it), keeps, widow control, page breaks, tab stops (measured from the margin), borders and shading, and the + list (`\ls`, `\ilvl`; continuation paragraphs indented to the item's text). Runs are groups with only the + control words that differ from the paragraph mark's formatting. +- **Links, fields, notes:** HYPERLINK fields (`\l` for a bookmark), bookmarks, PAGE / NUMPAGES fields with a result, + footnotes and endnotes (`\footnote`, `\ftnalt`) after their reference mark; a note outside the body text and + its tables (in a header, note or text box) stays in place in brackets, as in the DOCX writer. Equations are their + text (TeX source or linear form). +- **Tables:** `\trowd` with the row's padding, indent, alignment, height, header and keep, a cell definition per + grid column (`\clmgf` / `\clmrg` across columns, `\clvmgf` / `\clvmrg` down rows, vertical alignment, borders, + shading, padding, width, `\cellx` at its right edge), the cells' paragraphs (`\intbl`, each cell ending with + `\cell`) and `\row`. Tables in cells are nested tables (`\itapN`, `\nestcell`, `\nesttableprops` with + `\nestrow`). Column widths come from the model as in the DOCX writer, and the cell grid goes through + `HwpxWriter.PlaceCells` with its limits. +- **Pictures:** `\pict` groups, PNG and JPEG as they are (`\pngblip`, `\jpegblip`), EMF as `\emfblip`, WMF as + `\wmetafile8` (without the placeable header), any other picture converted to PNG; sized by `\picwgoal` / + `\pichgoal`, in the text like a character. **Text boxes and shapes** are Word drawing objects (`\shp` with their + placement, wrap, fill and outline); a text box's paragraphs are in `\shptxt`, and its text in `\shprslt` too, + for programs (Filee's reader among them) that don't draw shapes. +- **Sections:** `\sectd` with the paper, margins (the model's Top / Header become `\margtsxn` and `\headery`), + gutter, orientation, columns (own widths, separator, right to left), page number restarts, headers and footers + (`\header`, or `\headerr` / `\headerl` with facing pages; `\titlepg` with `\headerf` for a first page that + hides or has its own); columns that change in a section start a section without a page break (`\sbknone`). +- **Limits:** the model is opened up past 32 levels of nesting (`Nesting.Limit`); a file that would pass + `DocumentLimits.MaxPackageBytes` (what the RTF reader takes), tables past the cell limit and font, colour or list + tables past `MaxObjects` are refused (`error.document_too_complex`) and leave no file; pictures past + `MaxPartBytes` or claiming more than `MaxPicturePixels` are left out before anything decodes them; cancelling stops + the write and removes its temporary files. +- Tests: `RtfWriterTests` builds models in code, writes them and reads them back with the RTF reader into the same + model (`HDocumentAssert.Similar`), checks the file is valid RTF (one balanced `{\rtf1` group, ASCII only), the + Unicode fallbacks, the fonts and their other names, the routes, and the refusals. + +Not yet: floating pictures stay in the text (no `\shp` with a picture), picture crops and rotation, first-page +headers' own content when the model has none (`\headerf` repeats the others), paragraph styles other than Normal +and the headings, table and paragraph borders other than solid lines on all sides, comments and tracked changes. +Checked by reading the files back with Filee's RTF reader, not with Word or LibreOffice. + ## DOC reader Word 97–2003 documents (`.doc`, templates `.dot`, and WPS Office's `.wps`, which WPS Writer saves in the same format) @@ -3933,9 +3999,8 @@ all expected bytes have been written; failures and cancellation preserve any pre - Each parallel worker gets its own profile under `%APPDATA%\Filee\libreoffice-profiles` (LibreOffice silently fails when two conversions share a profile). - LibreOffice doesn't open HWP or HWPX: Filee reads them itself ([HWP reader](#hwp-reader), [HWPX - reader](#hwpx-reader)) and writes DOCX, ODT, HWPX, TXT, Markdown, HTML and EPUB with its own writers; PDF is the - Filee renderer's (rhwp's with it off). Only HWP → RTF still takes an optional engine, as there is no built-in RTF writer: Filee - writes Markdown that Pandoc turns into RTF, or, without Pandoc, DOCX or ODT that LibreOffice does. Until settings + reader](#hwpx-reader)) and writes DOCX, ODT, RTF, HWPX, TXT, Markdown, HTML and EPUB with its own writers; PDF is the + Filee renderer's (rhwp's with it off), and RTF with the [RTF writer](#rtf-writer). Until settings schema 101 LibreOffice opened HWP with the H2Orestart extension (GPL, Java) and a Java runtime downloaded with it (about 50 MB, 130 MB on disk); Filee removes the copies an earlier version installed (`EngineInstaller.RemoveRetired`, at start-up), and whoever had *Use LibreOffice for HWP·HWPX → PDF* on gets the Filee renderer instead. diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index 111ab5f92..ff75c4206 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "إنشاء مستندات Word ‏(DOCX) من PDF وOFD وHWP/HWPX وMarkdown وHTML وجداول البيانات والعروض والكتب الإلكترونية.", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "إنشاء مستندات نصية OpenDocument ‏(ODT) من مستندات Word وPDF وOFD وHWP/HWPX وMarkdown وHTML وجداول البيانات والعروض والكتب الإلكترونية.", + "engine.rtf-writer.name": "كاتب RTF (مدمج)", + "engine.rtf-writer.description": "إنشاء ملفات نص منسق (RTF، لبرنامجَي Word وWordPad) من مستندات Word وODT وPDF وOFD وHWP/HWPX وMarkdown وHTML وجداول البيانات والعروض والكتب الإلكترونية.", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "دفاتر Jupyter وOPML وTextile وMediaWiki وreStructuredText وOrg وDocBook دون Pandoc.\nتحويل المستندات إلى هذه الصيغ، وتحويلها إلى Markdown وHTML ونص وDOCX وHWPX وPDF.\nوأيضًا خرائط FreeMind / Freeplane وXMind الذهنية، تُقرأ كمخططات تفصيلية.", "engine.pptx-writer.name": "كاتب PPTX (مدمج)", diff --git a/src/Filee.App/Assets/i18n/bn-BD.json b/src/Filee.App/Assets/i18n/bn-BD.json index 11ea3129b..fd1eb8c48 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -301,6 +301,8 @@ "engine.docx-writer.description": "পিডিএফ, OFD, HWP/HWPX, মার্কডাউন , এইচটিএমএল, স্প্রেডশিট, স্লাইড এবং ই-বুক থেকে ওয়ার্ড ডকুমেন্ট (DOCX) তৈরি করে।", "engine.odt-writer.name": "ODT রাইটার (বিল্ট-ইন)", "engine.odt-writer.description": "ওয়ার্ড ডকুমেন্ট, পিডিএফ, OFD, HWP/HWPX, মার্কডাউন, এইচটিএমএল, স্প্রেডশিট, স্লাইড এবং ই-বুক থেকে OpenDocument টেক্সট (ODT, LibreOffice Writer) তৈরি করে।", + "engine.rtf-writer.name": "RTF রাইটার (বিল্ট-ইন)", + "engine.rtf-writer.description": "ওয়ার্ড ডকুমেন্ট, ODT, পিডিএফ, OFD, HWP/HWPX, মার্কডাউন, এইচটিএমএল, স্প্রেডশিট, স্লাইড এবং ই-বুক থেকে রিচ টেক্সট ফরম্যাট (RTF, Word ও WordPad-এর জন্য) তৈরি করে।", "engine.markup.name": "মার্কআপ ভাষা (বিল্ট-ইন)", "engine.markup.description": "Pandoc ছাড়াই Jupyter নোটবুক, OPML, Textile, MediaWiki, reStructuredText, Org এবং DocBook।\nডকুমেন্ট থেকে এই ফরম্যাটগুলোতে, এবং এগুলো থেকে মার্কডাউন, এইচটিএমএল, টেক্সট, DOCX, HWPX ও পিডিএফ-এ।\nএছাড়া FreeMind / Freeplane ও XMind মাইন্ড ম্যাপ, আউটলাইন হিসেবে পড়া হয়।", "engine.pptx-writer.name": "PPTX রাইটার (বিল্ট-ইন)", diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.json index 31d8f9826..f213c2627 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "Erstellt Word-Dokumente (DOCX) aus PDF, OFD, HWP/HWPX, Markdown, HTML, Tabellen, Folien und E-Books.", "engine.odt-writer.name": "ODT-Writer (integriert)", "engine.odt-writer.description": "Erstellt OpenDocument-Textdokumente (ODT) aus Word-Dokumenten, PDF, OFD, HWP/HWPX, Markdown, HTML, Tabellen, Folien und E-Books.", + "engine.rtf-writer.name": "RTF-Writer (integriert)", + "engine.rtf-writer.description": "Erstellt Rich-Text-Dateien (RTF, für Word und WordPad) aus Word-Dokumenten, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, Tabellen, Folien und E-Books.", "engine.markup.name": "Auszeichnungssprachen (integriert)", "engine.markup.description": "Jupyter-Notebooks, OPML, Textile, MediaWiki, reStructuredText, Org und DocBook ohne Pandoc.\nDokumente in diese Formate und diese in Markdown, HTML, Text, DOCX, HWPX und PDF.\nAußerdem Mindmaps aus FreeMind / Freeplane und XMind, als Gliederung gelesen.", "engine.pptx-writer.name": "PPTX-Writer (integriert)", diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json index 98fb4b38c..f8560bf2f 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "Writes Word documents (DOCX) from PDF, OFD, HWP/HWPX, Markdown, HTML, spreadsheets, slides and e-books.", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Writes OpenDocument text (ODT, LibreOffice Writer) from Word documents, PDF, OFD, HWP/HWPX, Markdown, HTML, spreadsheets, slides and e-books.", + "engine.rtf-writer.name": "RTF writer (built-in)", + "engine.rtf-writer.description": "Writes Rich Text Format (RTF, for Word and WordPad) from Word documents, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, spreadsheets, slides and e-books.", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Jupyter notebooks, OPML, Textile, MediaWiki, reStructuredText, Org and DocBook without Pandoc.\nDocuments to these formats, and these to Markdown, HTML, text, DOCX, HWPX and PDF.\nAlso FreeMind / Freeplane and XMind mind maps, read as outlines.", "engine.pptx-writer.name": "PPTX writer (built-in)", diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.json index f52142523..75502a8f1 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "Escribe documentos de Word (DOCX) desde PDF, OFD, HWP/HWPX, Markdown, HTML, hojas de cálculo, diapositivas y libros electrónicos.", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Escribe documentos de texto OpenDocument (ODT) desde documentos de Word, PDF, OFD, HWP/HWPX, Markdown, HTML, hojas de cálculo, diapositivas y libros electrónicos.", + "engine.rtf-writer.name": "Escritor RTF (integrado)", + "engine.rtf-writer.description": "Escribe documentos de texto enriquecido (RTF, para Word y WordPad) desde documentos de Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, hojas de cálculo, diapositivas y libros electrónicos.", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Cuadernos de Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org y DocBook sin Pandoc.\nDocumentos a estos formatos, y estos a Markdown, HTML, texto, DOCX, HWPX y PDF.\nTambién mapas mentales de FreeMind / Freeplane y XMind, leídos como esquemas.", "engine.pptx-writer.name": "Generador PPTX (integrado)", diff --git a/src/Filee.App/Assets/i18n/fa.json b/src/Filee.App/Assets/i18n/fa.json index 8de6630ad..ccd927770 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -301,6 +301,8 @@ "engine.docx-writer.description": "نوشتن اسناد Word (DOCX) از PDF، OFD، HWP/HWPX، مارک‌داون، HTML، صفحه‌گسترده‌ها، اسلایدها و کتاب‌های الکترونیکی.", "engine.odt-writer.name": "نویسنده ODT (داخلی)", "engine.odt-writer.description": "نوشتن متن OpenDocument (ODT، LibreOffice Writer) از اسناد Word، PDF، OFD، HWP/HWPX، مارک‌داون، HTML، صفحه‌گسترده‌ها، اسلایدها و کتاب‌های الکترونیکی.", + "engine.rtf-writer.name": "نویسنده RTF (داخلی)", + "engine.rtf-writer.description": "نوشتن متن قالب‌دار (RTF، برای Word و WordPad) از اسناد Word، ODT، PDF، OFD، HWP/HWPX، مارک‌داون، HTML، صفحه‌گسترده‌ها، اسلایدها و کتاب‌های الکترونیکی.", "engine.markup.name": "زبان‌های نشانه‌گذاری (داخلی)", "engine.markup.description": "دفترچه‌های Jupyter، OPML، Textile، MediaWiki، reStructuredText، Org و DocBook بدون Pandoc.\nاسناد به این قالب‌ها، و این قالب‌ها به مارک‌داون، HTML، متن، DOCX، HWPX و PDF.\nهمچنین نقشه‌های ذهنی FreeMind / Freeplane و XMind که به‌صورت طرح کلی خوانده می‌شوند.", "engine.pptx-writer.name": "نویسنده PPTX (داخلی)", diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.json index 8ba6306ca..bb7ba3ca8 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "Écrit des documents Word (DOCX) à partir de PDF, OFD, HWP/HWPX, Markdown, HTML, feuilles de calcul, diapositives et livres numériques.", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Écrit des documents texte OpenDocument (ODT) à partir de documents Word, PDF, OFD, HWP/HWPX, Markdown, HTML, feuilles de calcul, diapositives et livres numériques.", + "engine.rtf-writer.name": "Écrivain RTF (intégré)", + "engine.rtf-writer.description": "Écrit du texte enrichi (RTF, pour Word et WordPad) à partir de documents Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, feuilles de calcul, diapositives et livres numériques.", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Notebooks Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org et DocBook sans Pandoc.\nDes documents vers ces formats, et ceux-ci en Markdown, HTML, texte, DOCX, HWPX et PDF.\nAussi les cartes mentales FreeMind / Freeplane et XMind, lues comme des plans.", "engine.pptx-writer.name": "Générateur PPTX (intégré)", diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.json index f6965d2f9..7f57e372a 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "PDF, OFD, HWP/HWPX, Markdown, HTML, स्प्रेडशीट, स्लाइड और ई-बुक से Word दस्तावेज़ (DOCX) लिखता है।", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Word दस्तावेज़, PDF, OFD, HWP/HWPX, Markdown, HTML, स्प्रेडशीट, स्लाइड और ई-बुक से OpenDocument टेक्स्ट (ODT) लिखता है।", + "engine.rtf-writer.name": "RTF writer (built-in)", + "engine.rtf-writer.description": "Word दस्तावेज़, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, स्प्रेडशीट, स्लाइड और ई-बुक से रिच टेक्स्ट फ़ॉर्मेट (RTF, Word और WordPad के लिए) लिखता है।", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Pandoc के बिना Jupyter नोटबुक, OPML, Textile, MediaWiki, reStructuredText, Org और DocBook।\nदस्तावेज़ों से ये फ़ॉर्मैट, और इनसे Markdown, HTML, टेक्स्ट, DOCX, HWPX और PDF।\nसाथ ही FreeMind / Freeplane और XMind माइंड मैप, आउटलाइन के रूप में पढ़े जाते हैं।", "engine.pptx-writer.name": "PPTX लेखक (अंतर्निहित)", diff --git a/src/Filee.App/Assets/i18n/id.json b/src/Filee.App/Assets/i18n/id.json index 9030c5343..6d9aaeead 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "Menulis dokumen Word (DOCX) dari PDF, OFD, HWP/HWPX, Markdown, HTML, spreadsheet, slide, dan e-book.", "engine.odt-writer.name": "Penulis ODT (bawaan)", "engine.odt-writer.description": "Menulis dokumen teks OpenDocument (ODT) dari dokumen Word, PDF, OFD, HWP/HWPX, Markdown, HTML, spreadsheet, slide, dan e-book.", + "engine.rtf-writer.name": "Penulis RTF (bawaan)", + "engine.rtf-writer.description": "Menulis dokumen Rich Text Format (RTF, untuk Word dan WordPad) dari dokumen Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, spreadsheet, slide, dan e-book.", "engine.markup.name": "Bahasa markup (bawaan)", "engine.markup.description": "Notebook Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org, dan DocBook tanpa Pandoc.\nDokumen ke format ini, dan format ini ke Markdown, HTML, teks, DOCX, HWPX, dan PDF.\nJuga peta pikiran FreeMind / Freeplane dan XMind, dibaca sebagai kerangka.", "engine.pptx-writer.name": "Penulis PPTX (bawaan)", diff --git a/src/Filee.App/Assets/i18n/it.json b/src/Filee.App/Assets/i18n/it.json index 3d900fcd2..449c144b5 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -286,6 +286,8 @@ "engine.docx-writer.description": "Scrive documenti Word (DOCX) da PDF, OFD, HWP/HWPX, Markdown, HTML, fogli di calcolo, presentazioni ed e-book.", "engine.odt-writer.name": "Writer ODT (integrato)", "engine.odt-writer.description": "Scrive documenti di testo OpenDocument (ODT) da documenti Word, PDF, OFD, HWP/HWPX, Markdown, HTML, fogli di calcolo, presentazioni ed e-book.", +"engine.rtf-writer.name": "Writer RTF (integrato)", +"engine.rtf-writer.description": "Scrive documenti Rich Text Format (RTF, per Word e WordPad) da documenti Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, fogli di calcolo, presentazioni ed e-book.", "engine.markup.name": "Linguaggi di markup (integrato)", "engine.markup.description": "Notebook Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org e DocBook senza Pandoc.\nDocumenti in questi formati e questi in Markdown, HTML, testo, DOCX, HWPX e PDF.\nAnche mappe mentali FreeMind / Freeplane e XMind, lette come schemi.", "engine.pptx-writer.name": "Writer PPTX (integrato)", diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json index 890ee52c3..0e4ecba3e 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "PDF、OFD、HWP/HWPX、Markdown、HTML、表計算ファイル、スライド、電子書籍からWord文書(DOCX)を作成します。", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Word文書、PDF、OFD、HWP/HWPX、Markdown、HTML、表計算ファイル、スライド、電子書籍からOpenDocumentテキスト(ODT)を作成します。", + "engine.rtf-writer.name": "RTF writer (built-in)", + "engine.rtf-writer.description": "Word 文書、ODT、PDF、OFD、HWP/HWPX、Markdown、HTML、スプレッドシート、スライド、電子書籍からリッチテキスト形式 (RTF、Word・ワードパッド用) を作成します。", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Pandocなしで Jupyter ノートブック、OPML、Textile、MediaWiki、reStructuredText、Org、DocBook を読み書きします。\n文書をこれらの形式に、これらをMarkdown、HTML、テキスト、DOCX、HWPX、PDFに変換します。\nFreeMind / Freeplane と XMind のマインドマップもアウトラインとして読み込みます。", "engine.pptx-writer.name": "PPTX ライター(組み込み)", diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json index e1dae63ca..d4f2ed443 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "PDF, OFD, HWP/HWPX, 마크다운, HTML, 스프레드시트, 슬라이드, 전자책으로 워드 문서(DOCX)를 만들어요.", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "워드 문서, PDF, OFD, HWP/HWPX, 마크다운, HTML, 스프레드시트, 슬라이드, 전자책으로 오픈문서 텍스트(ODT, LibreOffice Writer)를 만들어요.", + "engine.rtf-writer.name": "RTF writer (built-in)", + "engine.rtf-writer.description": "워드 문서, ODT, PDF, OFD, HWP/HWPX, 마크다운, HTML, 스프레드시트, 슬라이드, 전자책으로 서식 있는 텍스트(RTF, 워드·워드패드용)를 만들어요.", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Pandoc 없이 주피터 노트북, OPML, Textile, MediaWiki, reStructuredText, Org, DocBook을 읽고 써요.\n문서를 이 형식으로, 이 형식을 마크다운, HTML, 텍스트, DOCX, HWPX, PDF로 바꿔요.\nFreeMind / Freeplane, XMind 마인드맵도 개요로 읽어요.", "engine.pptx-writer.name": "PPTX 작성기 (내장)", diff --git a/src/Filee.App/Assets/i18n/mn.json b/src/Filee.App/Assets/i18n/mn.json index f3d78b428..e529108b9 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -286,6 +286,8 @@ "engine.docx-writer.description": "PDF, OFD, HWP/HWPX, Markdown, HTML, хүснэгт, слайд болон цахим номноос Word баримт (DOCX) бичнэ.", "engine.odt-writer.name": "ODT бичигч (суурилагдсан)", "engine.odt-writer.description": "Word баримт, PDF, OFD, HWP/HWPX, Markdown, HTML, хүснэгт, слайд болон цахим номноос OpenDocument текст (ODT, LibreOffice Writer) бичнэ.", + "engine.rtf-writer.name": "RTF бичигч (суурилуулсан)", + "engine.rtf-writer.description": "Word баримт, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, хүснэгт, слайд, цахим номоос Rich Text Format (RTF, Word болон WordPad-д) файл бичнэ.", "engine.markup.name": "Тэмдэглэгээний хэлүүд (суурилагдсан)", "engine.markup.description": "Jupyter notebook, OPML, Textile, MediaWiki, reStructuredText, Org болон DocBook-ийг Pandoc-гүйгээр.\nБаримтыг эдгээр формат руу, эдгээрийг Markdown, HTML, текст, DOCX, HWPX болон PDF руу.\nМөн FreeMind / Freeplane болон XMind оюун ухааны газрын зургийг тойм хэлбэрээр уншина.", "engine.pptx-writer.name": "PPTX бичигч (суурилагдсан)", diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.json index 14b2c4002..4c684db71 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "PDF, OFD, HWP/HWPX, Markdown, HTML, स्प्रेडसिट, स्लाइड र इ-पुस्तकबाट Word कागजातहरू (DOCX) लेख्छ।", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Word कागजात, PDF, OFD, HWP/HWPX, Markdown, HTML, स्प्रेडसिट, स्लाइड र इ-पुस्तकबाट OpenDocument पाठ (ODT) लेख्छ।", + "engine.rtf-writer.name": "RTF लेखक (बिल्ट-इन)", + "engine.rtf-writer.description": "Word कागजात, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, स्प्रेडसिट, स्लाइड र ई-पुस्तकबाट रिच टेक्स्ट फर्म्याट (RTF, Word र WordPad का लागि) लेख्छ।", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Pandoc बिना Jupyter नोटबुक, OPML, Textile, MediaWiki, reStructuredText, Org र DocBook पढ्छ र लेख्छ।\nकागजातलाई यी ढाँचामा, र यिनलाई Markdown, HTML, पाठ, DOCX, HWPX र PDF मा रूपान्तरण गर्दछ।\nसाथै FreeMind / Freeplane र XMind माइन्ड म्याप, रूपरेखाका रूपमा पढिन्छ।", "engine.pptx-writer.name": "PPTX लेखक (भित्री)", diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.json index 658fb1946..dc905b613 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "Zapisuje dokumenty Word (DOCX) z PDF, OFD, HWP/HWPX, Markdown, HTML, arkuszy, slajdów i e-booków.", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Zapisuje dokumenty tekstowe OpenDocument (ODT) z dokumentów Word, PDF, OFD, HWP/HWPX, Markdown, HTML, arkuszy, slajdów i e-booków.", + "engine.rtf-writer.name": "Zapis RTF (wbudowany)", + "engine.rtf-writer.description": "Zapisuje pliki Rich Text Format (RTF, dla Worda i WordPada) z dokumentów Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, arkuszy kalkulacyjnych, slajdów i e-booków.", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Notatniki Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org i DocBook bez Pandoc.\nDokumenty do tych formatów, a te do Markdown, HTML, tekstu, DOCX, HWPX i PDF.\nTakże mapy myśli FreeMind / Freeplane i XMind, czytane jako konspekt.", "engine.pptx-writer.name": "Generator PPTX (wbudowany)", diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.json index 628fade4a..204bb1deb 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "Grava documentos do Word (DOCX) a partir de PDF, OFD, HWP/HWPX, Markdown, HTML, planilhas, slides e e-books.", "engine.odt-writer.name": "Gerador ODT (mecanismo integrado)", "engine.odt-writer.description": "Grava documentos de texto OpenDocument (ODT) a partir de documentos do Word, PDF, OFD, HWP/HWPX, Markdown, HTML, planilhas, slides e e-books.", + "engine.rtf-writer.name": "Gravador RTF (integrado)", + "engine.rtf-writer.description": "Grava documentos Rich Text Format (RTF, para Word e WordPad) a partir de documentos do Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, planilhas, slides e e-books.", "engine.markup.name": "Linguagens de marcação (mecanismo integrado)", "engine.markup.description": "Notebooks Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org e DocBook sem Pandoc.\nDocumentos para esses formatos e eles para Markdown, HTML, texto, DOCX, HWPX e PDF.\nTambém mapas mentais do FreeMind / Freeplane e do XMind, lidos como tópicos.", "engine.pptx-writer.name": "Gerador PPTX (mecanismo integrado)", diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.json index 09302ed56..d0c348bf4 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "Cria documentos Word (DOCX) a partir de PDF, OFD, HWP/HWPX, Markdown, HTML, folhas de cálculo, diapositivos e livros digitais.", "engine.odt-writer.name": "Gerador ODT (motor integrado)", "engine.odt-writer.description": "Cria documentos de texto OpenDocument (ODT) a partir de documentos Word, PDF, OFD, HWP/HWPX, Markdown, HTML, folhas de cálculo, diapositivos e livros digitais.", + "engine.rtf-writer.name": "Escritor RTF (integrado)", + "engine.rtf-writer.description": "Escreve documentos Rich Text Format (RTF, para Word e WordPad) a partir de documentos do Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, folhas de cálculo, diapositivos e livros eletrónicos.", "engine.markup.name": "Linguagens de marcação (motor integrado)", "engine.markup.description": "Blocos de notas Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org e DocBook sem Pandoc.\nDocumentos para estes formatos e estes para Markdown, HTML, texto, DOCX, HWPX e PDF.\nTambém mapas mentais do FreeMind / Freeplane e do XMind, lidos como esquemas.", "engine.pptx-writer.name": "Gerador PPTX (motor integrado)", diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json index d74ffafdf..711fbfa38 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "Создаёт документы Word (DOCX) из PDF, OFD, HWP/HWPX, Markdown, HTML, таблиц, презентаций и электронных книг.", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "Создаёт текстовые документы OpenDocument (ODT) из документов Word, PDF, OFD, HWP/HWPX, Markdown, HTML, таблиц, презентаций и электронных книг.", + "engine.rtf-writer.name": "Запись RTF (встроенная)", + "engine.rtf-writer.description": "Создаёт файлы Rich Text Format (RTF, для Word и WordPad) из документов Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, таблиц, слайдов и электронных книг.", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Блокноты Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org и DocBook без Pandoc.\nДокументы — в эти форматы, а их — в Markdown, HTML, текст, DOCX, HWPX и PDF.\nА также интеллект-карты FreeMind / Freeplane и XMind, читаемые как план.", "engine.pptx-writer.name": "Генератор PPTX (встроенный)", diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.json index 338a98ec3..5cdd4b8ad 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "เขียนเอกสาร Word (DOCX) จาก PDF, OFD, HWP/HWPX, Markdown, HTML, สเปรดชีต, สไลด์ และอีบุ๊ก", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "เขียนเอกสารข้อความ OpenDocument (ODT) จากเอกสาร Word, PDF, OFD, HWP/HWPX, Markdown, HTML, สเปรดชีต, สไลด์ และอีบุ๊ก", + "engine.rtf-writer.name": "ตัวเขียน RTF (ในตัว)", + "engine.rtf-writer.description": "เขียนไฟล์ Rich Text Format (RTF สำหรับ Word และ WordPad) จากเอกสาร Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, สเปรดชีต, สไลด์ และอีบุ๊ก", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "Jupyter notebook, OPML, Textile, MediaWiki, reStructuredText, Org และ DocBook โดยไม่ต้องใช้ Pandoc\nแปลงเอกสารเป็นรูปแบบเหล่านี้ และแปลงรูปแบบเหล่านี้เป็น Markdown, HTML, ข้อความ, DOCX, HWPX และ PDF\nรวมถึงแผนผังความคิดของ FreeMind / Freeplane และ XMind ซึ่งอ่านเป็นโครงร่าง", "engine.pptx-writer.name": "ตัวเขียน PPTX (ในตัว)", diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.json index 999ce28b5..f92121760 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -287,6 +287,8 @@ "engine.docx-writer.description": "PDF, OFD, HWP/HWPX, Markdown, HTML, elektronik tablo, slayt ve e-kitaplardan Word belgesi (DOCX) oluşturur.", "engine.odt-writer.name": "ODT oluşturucu (yerleşik)", "engine.odt-writer.description": "Word belgeleri, PDF, OFD, HWP/HWPX, Markdown, HTML, elektronik tablo, slayt ve e-kitaplardan OpenDocument metin belgesi (ODT) oluşturur.", + "engine.rtf-writer.name": "RTF yazıcı (yerleşik)", + "engine.rtf-writer.description": "Word belgeleri, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, elektronik tablolar, slaytlar ve e-kitaplardan Zengin Metin Biçimi (RTF, Word ve WordPad için) yazar.", "engine.markup.name": "İşaretleme dilleri (yerleşik)", "engine.markup.description": "Pandoc olmadan Jupyter not defterleri, OPML, Textile, MediaWiki, reStructuredText, Org ve DocBook.\nBelgeleri bu biçimlere, bunları da Markdown, HTML, metin, DOCX, HWPX ve PDF'ye dönüştürür.\nAyrıca FreeMind / Freeplane ve XMind zihin haritaları, ana hat olarak okunur.", "engine.pptx-writer.name": "PPTX oluşturucu (yerleşik)", diff --git a/src/Filee.App/Assets/i18n/vi.json b/src/Filee.App/Assets/i18n/vi.json index 360b5f07d..199b966a8 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "Ghi tài liệu Word (DOCX) từ PDF, OFD, HWP/HWPX, Markdown, HTML, bảng tính, trang chiếu và sách điện tử.", "engine.odt-writer.name": "Trình ghi ODT (tích hợp)", "engine.odt-writer.description": "Ghi văn bản OpenDocument (ODT, LibreOffice Writer) từ tài liệu Word, PDF, OFD, HWP/HWPX, Markdown, HTML, bảng tính, trang chiếu và sách điện tử.", + "engine.rtf-writer.name": "Trình ghi RTF (tích hợp)", + "engine.rtf-writer.description": "Ghi tệp Rich Text Format (RTF, cho Word và WordPad) từ tài liệu Word, ODT, PDF, OFD, HWP/HWPX, Markdown, HTML, bảng tính, trang chiếu và sách điện tử.", "engine.markup.name": "Ngôn ngữ đánh dấu (tích hợp)", "engine.markup.description": "Sổ tay Jupyter, OPML, Textile, MediaWiki, reStructuredText, Org và DocBook mà không cần Pandoc.\nTài liệu sang các định dạng này, và các định dạng này sang Markdown, HTML, văn bản, DOCX, HWPX và PDF.\nCả sơ đồ tư duy FreeMind / Freeplane và XMind, được đọc dưới dạng dàn ý.", "engine.pptx-writer.name": "Trình ghi PPTX (tích hợp)", diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json index ecfbb5bd7..d9e64ada6 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "用 PDF、OFD、HWP/HWPX、Markdown、HTML、表格、幻灯片和电子书生成 Word 文档(DOCX)。", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "用 Word 文档、PDF、OFD、HWP/HWPX、Markdown、HTML、表格、幻灯片和电子书生成 OpenDocument 文本(ODT)。", + "engine.rtf-writer.name": "RTF 写入器(内置)", + "engine.rtf-writer.description": "从 Word 文档、ODT、PDF、OFD、HWP/HWPX、Markdown、HTML、电子表格、幻灯片和电子书写出 RTF 富文本(供 Word 和写字板使用)。", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "无需 Pandoc 即可读写 Jupyter 笔记本、OPML、Textile、MediaWiki、reStructuredText、Org 和 DocBook。\n把文档转为这些格式,也把它们转为 Markdown、HTML、文本、DOCX、HWPX 和 PDF。\n还可把 FreeMind / Freeplane 和 XMind 思维导图读成大纲。", "engine.pptx-writer.name": "PPTX 生成器(内置)", diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.json index d53a7df1e..3a5ae90ec 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -294,6 +294,8 @@ "engine.docx-writer.description": "用 PDF、OFD、HWP/HWPX、Markdown、HTML、表格、投影片和電子書產生 Word 文件(DOCX)。", "engine.odt-writer.name": "ODT writer (built-in)", "engine.odt-writer.description": "用 Word 文件、PDF、OFD、HWP/HWPX、Markdown、HTML、表格、投影片和電子書產生 OpenDocument 文字文件(ODT)。", + "engine.rtf-writer.name": "RTF 寫入器(內建)", + "engine.rtf-writer.description": "從 Word 文件、ODT、PDF、OFD、HWP/HWPX、Markdown、HTML、試算表、投影片和電子書寫出 RTF 豐富文字(供 Word 和 WordPad 使用)。", "engine.markup.name": "Markup languages (built-in)", "engine.markup.description": "不需 Pandoc 即可讀寫 Jupyter 筆記本、OPML、Textile、MediaWiki、reStructuredText、Org 和 DocBook。\n把文件轉為這些格式,也把它們轉為 Markdown、HTML、文字、DOCX、HWPX 和 PDF。\n也能把 FreeMind / Freeplane 和 XMind 心智圖讀成大綱。", "engine.pptx-writer.name": "PPTX 產生器(內建)", diff --git a/src/Filee.Engines/EngineRegistry.cs b/src/Filee.Engines/EngineRegistry.cs index d10d7640f..4329e41ad 100644 --- a/src/Filee.Engines/EngineRegistry.cs +++ b/src/Filee.Engines/EngineRegistry.cs @@ -63,6 +63,7 @@ public static IReadOnlyList CreateAll(EngineEnvironment env, ILogger new FontConverter(), new DocxWriterConverter(), new OdtWriterConverter(), + new RtfWriterConverter(), new MarkupConverter(), new PptxWriterConverter(), new OdpWriterConverter(), diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs index 26ae151d5..3ee141b45 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs @@ -41,10 +41,18 @@ internal sealed class HDocument public Dictionary FontAlternates { get; } = new(StringComparer.OrdinalIgnoreCase); /// - /// Word's distance between default tab stops (settings w:defaultTabStop), in HWPUNIT, measured from the margin; - /// null: 한글's 40 pt from the paragraph's left indent. Only the layout engine uses it. + /// The distance between default tab stops of the source's application (Word's w:defaultTabStop, RTF \deftab, + /// ODF style:tab-stop-distance), in HWPUNIT, measured from the margin (or the indent, see + /// ); null: 한글's 40 pt from the paragraph's left indent. Only the layout + /// engine and the RTF writer use it. /// public int? DefaultTabStop { get; set; } + + /// + /// counts from the paragraph's left indent, as LibreOffice's own documents have it + /// (TabsRelativeToIndent), not from the margin as Word's. Only the layout engine uses it. + /// + public bool DefaultTabStopFromIndent { get; set; } } /// A run of pages sharing page setup, headers and footers (한글 "구역"). diff --git a/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs b/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs index df857ab3d..589a3dc94 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Odt/OdtReader.cs @@ -104,6 +104,7 @@ private HDocument ReadDocument() var styles = Load("styles.xml"); LoadStyles(styles, content); NotesConfiguration(styles?.Root?.Element(Office + "styles")); + DefaultTabs(); _document.Title = Load("meta.xml")?.Root?.Element(Office + "meta")?.Element(Dc + "title")?.Value is { } title && title.Trim().Length > 0 ? title.Trim() : null; var body = new Sink([], body: true); @@ -114,6 +115,35 @@ private HDocument ReadDocument() return _document; } + /// LibreOffice's distance between default tab stops when the default paragraph style gives none: 1.25 cm. + private const int LibreOfficeTabStop = 3543; + + /// + /// Default tab stops as LibreOffice sets them: every style:tab-stop-distance of the default paragraph style (else + /// 1.25 cm), measured from the paragraph's indent unless settings.xml turns TabsRelativeToIndent off (documents + /// that came from Word measure from the margin). + /// + private void DefaultTabs() + { + var distance = _defaults.TryGetValue("paragraph", out var defaults) + ? Odf.Length(Attr(defaults.Element(Style + "paragraph-properties"), Style + "tab-stop-distance")) + : null; + _document.DefaultTabStop = distance is > 0 and < 7_200_000 ? distance : LibreOfficeTabStop; + string? relative; + try + { + relative = Load("settings.xml")?.Descendants(Config + "config-item") + .FirstOrDefault(item => Attr(item, Config + "name") == "TabsRelativeToIndent")?.Value.Trim(); + } + catch (InvalidDataException) + { + relative = null; // damaged view settings don't stop the text from being read + } + _document.DefaultTabStopFromIndent = !string.Equals(relative, "false", StringComparison.OrdinalIgnoreCase); + } + + private static readonly XNamespace Config = "urn:oasis:names:tc:opendocument:xmlns:config:1.0"; + private void NotesConfiguration(XElement? styles) { foreach (var configuration in styles?.Elements(Text + "notes-configuration") ?? []) diff --git a/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs b/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs index 9e0aa8e00..2be0e76c0 100644 --- a/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs +++ b/src/Filee.Engines/Hwp/Hwpx/RtfReader.Tables.cs @@ -258,6 +258,8 @@ private void CloseTable(Story story) }; var left = def.Left; HCell? previous = null; + // The cell before was below a cell spanning rows (\clvmrg): \clmrg cells after it belong to that cell too. + var continuing = false; for (var i = 0; i < contents.Count; i++) { var cellDef = i < def.Cells.Count ? def.Cells[i] : new CellDef { Right = left + 1440 }; @@ -272,12 +274,16 @@ private void CloseTable(Story story) previous.ColSpan += span; continue; } + if (cellDef.Horizontal == Merge.Continue && continuing) + continue; if (cellDef.Vertical == Merge.Continue && openVertical.TryGetValue(from, out var above)) { above.RowSpan++; previous = null; + continuing = true; continue; } + continuing = false; var cell = new HCell { Column = from, @@ -429,6 +435,11 @@ private static HNumbering Numbering(ListDef list) 2 => "ROMAN_SMALL", 3 => "LATIN_CAPITAL", 4 => "LATIN_SMALL", + 10 => "IDEOGRAPH", + 18 => "CIRCLED_DIGIT", + 24 => "HANGUL_SYLLABLE", + 25 => "HANGUL_JAMO", + 40 => "HANGUL_PHONETIC", _ => "DIGIT", }; numbering.Levels.Add(new HNumberingLevel(format, level.Text.Length > 0 ? level.Text : $"^{numbering.Levels.Count + 1}.", level.Start, Bullet: false)); diff --git a/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs b/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs index 0822a6d5d..66bed6bd5 100644 --- a/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/RtfReader.cs @@ -42,6 +42,9 @@ private enum Dest Picture, FieldInstruction, BookmarkStart, + + /// The other name of the font being defined (\falt). + FontAlternate, } /// Where text and paragraphs go: the body of a section, a note, a header or a footer. @@ -79,9 +82,11 @@ private record struct CharProps(bool Bold, bool Italic, bool Underline, bool Str /// \li and \ri: the left and right side. /// \lin and \rin: the start and end side (the right and left one in a right-to-left paragraph); they win. /// \rtlpar. + /// Tab stops (\tx, measured from the margin, with their kind and leader), or null. + /// The kind and leader the next \tx takes (\tqr, \tldot ...). private record struct ParaProps(HAlign? Align, int Left, int Right, int FirstLine, int Before, int After, int Line, bool Multiple, bool KeepNext, bool Keep, bool PageBreakBefore, bool InTable, int Style, int ListOverride, int ListLevel, int Outline, - int? Start = null, int? End = null, bool RightToLeft = false) + int? Start = null, int? End = null, bool RightToLeft = false, TabList? Tabs = null, (HTabKind Kind, string? Leader) TabKind = default) { public static ParaProps Default => new() { Style = -1, ListOverride = -1, Outline = -1 }; } @@ -141,6 +146,7 @@ private sealed class Group private int _fontCharset = -1; private int _fontCodePage; private readonly StringBuilder _collected = new(); + private readonly StringBuilder _fontAlternate = new(); private int _red, _green, _blue; private int _styleId = -1; @@ -262,6 +268,8 @@ private HDocument Run() foreach (var list in _runs.Keys.ToList()) EndRun(list); ApplyPage(); + // Default tab stops every \deftab from the margin, as Word sets them; RTF's default is half an inch. + _document.DefaultTabStop ??= HwpxUnits.FromTwips(720); return _document; } @@ -302,6 +310,7 @@ private void Word(string word, int? parameter) case "pc": _ansiCodePage = 437; return; case "pca": _ansiCodePage = 850; return; case "deff": _defaultFont = value; return; + case "deftab": _document.DefaultTabStop = parameter is > 0 ? HwpxUnits.FromTwips(Math.Min(parameter.Value, 31680)) : _document.DefaultTabStop; return; case "uc": _g.Uc = Math.Clamp(value, 0, 16); return; case "u": Unicode(value); return; @@ -370,6 +379,15 @@ private void Word(string word, int? parameter) case "ls": _g.Para = p with { ListOverride = value }; return; case "ilvl": _g.Para = p with { ListLevel = Math.Clamp(value, 0, 8) }; return; case "outlinelevel": _g.Para = p with { Outline = value }; return; + case "tqr": _g.Para = p with { TabKind = (HTabKind.Right, p.TabKind.Leader) }; return; + case "tqc": _g.Para = p with { TabKind = (HTabKind.Center, p.TabKind.Leader) }; return; + case "tqdec": _g.Para = p with { TabKind = (HTabKind.Decimal, p.TabKind.Leader) }; return; + case "tldot" or "tlmdot": _g.Para = p with { TabKind = (p.TabKind.Kind, "DOT") }; return; + case "tlhyph": _g.Para = p with { TabKind = (p.TabKind.Kind, "DASH") }; return; + case "tlul" or "tlth" or "tleq": _g.Para = p with { TabKind = (p.TabKind.Kind, "SOLID") }; return; + case "tx" or "tb": + _g.Para = p with { Tabs = (p.Tabs ?? TabList.Empty).Add(value, p.TabKind.Kind, p.TabKind.Leader ?? "NONE"), TabKind = default }; + return; // Text case "par" or "line" or "tab" or "page" or "emdash" or "endash" or "bullet" or "lquote" or "rquote" or "ldblquote" @@ -468,6 +486,11 @@ private bool Destination(string word, int? parameter) AddInline(new HBookmark(name)); }; return true; + case "falt" when _g.Dest == Dest.FontTable: + // The font's other name, used where the font is not installed (as Word's w:altName). + _g.Dest = Dest.FontAlternate; + _fontAlternate.Clear(); + return true; case "nonshppict" or "listtext" or "pntext" or "xe" or "tc" or "txe" or "rxe" or "bkmkend" or "annotation" or "atnid" or "atnauthor" or "template" or "comment" or "author" or "operator" or "company" or "manager" or "category" or "keywords" or "subject" or "doccomm" or "hlinkbase" or "creatim" or "revtim" or "printim" or "buptim" @@ -589,6 +612,10 @@ private void Decoded(string text) case Dest.Title or Dest.LevelText or Dest.BookmarkStart: _collected.Append(text); break; + case Dest.FontAlternate: + if (_fontAlternate.Length < 256) + _fontAlternate.Append(text); + break; case Dest.FieldInstruction: _g.Field?.Instruction.Append(text); break; @@ -610,8 +637,12 @@ private void FontName(string text) Definitions(_fonts.Count, "fonts"); var codePage = _fontCodePage > 0 ? _fontCodePage : CharsetCodePage(_fontCharset); var eastAsian = _fontCharset is 128 or 129 or 130 or 134 or 136; - _fonts[_fontIndex] = (_collected.ToString().Trim(), codePage, eastAsian); + var name = _collected.ToString().Trim(); + _fonts[_fontIndex] = (name, codePage, eastAsian); + if (name.Length > 0 && _fontAlternate.ToString().Trim() is { Length: > 0 } alternate && !alternate.Equals(name, StringComparison.OrdinalIgnoreCase)) + _document.FontAlternates.TryAdd(name, alternate); } + _fontAlternate.Clear(); _collected.Clear(); _fontIndex = _fontCharset = -1; _fontCodePage = 0; @@ -621,7 +652,7 @@ private void FontName(string text) /// The code page text is decoded with: the font's, else the document's. private int CodePage() { - if (_g.Dest == Dest.FontTable) + if (_g.Dest is Dest.FontTable or Dest.FontAlternate) return _fontCodePage > 0 ? _fontCodePage : CharsetCodePage(_fontCharset) is > 0 and var charset ? charset : _ansiCodePage; var font = _g.Char.Font >= 0 ? _g.Char.Font : _defaultFont; return _fonts.TryGetValue(font, out var entry) && entry.CodePage > 0 ? entry.CodePage : _ansiCodePage; @@ -754,7 +785,48 @@ private HCharFormat Format(CharProps c) _ => new HLineSpacing(HLineSpacingKind.AtLeast, HwpxUnits.FromTwips(p.Line)), }, KeepWithNext: p.KeepNext ? true : null, - KeepLines: p.Keep ? true : null); + KeepLines: p.Keep ? true : null, + Tabs: TabStops(p)); + + /// + /// The paragraph's tab stops as the model has them: measured from its start indent (RTF measures from the margin), + /// in order; null when it has none. + /// + private static List? TabStops(ParaProps p) + { + if (p.Tabs is not { Count: > 0 } tabs) + return null; + var indent = p.Start ?? (p.RightToLeft ? p.Right : p.Left); + return [.. tabs.Stops.OrderBy(t => t.Position).Select(t => t with { Position = Math.Max(0, HwpxUnits.FromTwips(t.Position - indent)) })]; + } + + /// + /// A paragraph's tab stops as read (positions in twips from the margin). It is never changed, so the copies of the + /// properties each group makes share it. + /// + private sealed class TabList + { + /// Stops a paragraph keeps (Word allows 64); further \tx are ignored. + private const int MaxStops = 256; + + public static readonly TabList Empty = new([]); + + private TabList(List stops) => Stops = stops; + + public IReadOnlyList Stops { get; } + + public int Count => Stops.Count; + + /// A list with one more stop (one at the same position is replaced). + public TabList Add(int position, HTabKind kind, string leader) + { + if (Stops.Count >= MaxStops) + return this; + var stops = Stops.Where(t => t.Position != position).ToList(); + stops.Add(new HTabStop(position, kind, leader)); + return new TabList(stops); + } + } // ───────────────────────── Paragraphs and sections ───────────────────────── @@ -832,6 +904,10 @@ private void StartNote() level.Dispose(); var note = new HNote(group.Endnote); note.Blocks.AddRange(story.Blocks); + // Word's notes start with their number (\chftn) and a space; the layout puts the number there itself. + if (note.Blocks.FirstOrDefault() is HParagraph first && first.Inlines.FindIndex(i => i is not HNoteNumber) is var index and > 0 + && first.Inlines[index] is HText { Text: var leading } text) + first.Inlines[index] = new HText(leading.TrimStart(), text.Format); EndRun(parent); parent.Add(note); }; @@ -855,7 +931,7 @@ private void StartHeaderFooter(string word) // First-page headers and footers have no place in the model: the others apply. if (word.EndsWith('f')) return; - var pages = word.EndsWith('l') ? HPageType.Even : word.EndsWith('r') ? HPageType.Odd : HPageType.Both; + var pages = word is "headerl" or "footerl" ? HPageType.Even : word is "headerr" or "footerr" ? HPageType.Odd : HPageType.Both; var part = new HHeaderFooter(pages); part.Blocks.AddRange(story.Blocks); (word.StartsWith("header", StringComparison.Ordinal) ? section.Headers : section.Footers).Add(part); diff --git a/src/Filee.Engines/Hwp/Hwpx/RtfWriter.Body.cs b/src/Filee.Engines/Hwp/Hwpx/RtfWriter.Body.cs new file mode 100644 index 000000000..67125c508 --- /dev/null +++ b/src/Filee.Engines/Hwp/Hwpx/RtfWriter.Body.cs @@ -0,0 +1,727 @@ +// Document body of an RTF file: paragraphs (\pard ... \par), their character formatting, links, bookmarks, fields, +// notes and tables. +// +// RTF paragraphs carry all of their formatting: after \pard\plain each one repeats what its style has (the heading +// styles' bold and size, Normal's 10 pt and line spacing), then what the model sets. A run is a group whose control +// words are only those that differ from the paragraph mark's, so the run's formatting ends with its group. Text is +// split where it changes from Latin to East Asian script, each part in the font of its slot (RTF has one \f at a time). +// +// A table is a run of rows: \trowd with the row's cell definitions (\cellx at each cell's right edge), then the +// cells' paragraphs (\intbl), each cell ending with \cell and the row with \row. Cells spanning columns are \clmgf +// with \clmrg cells after them, cells spanning rows \clvmgf with \clvmrg cells below, so every row has a cell at each +// edge of the grid. Tables in cells are nested tables (\itapN, \nestcell, \nestrow with their \nesttableprops). + +using System.Text; +using Filee.Engines.Infrastructure; + +namespace Filee.Engines.Hwp.Hwpx; + +internal sealed partial class RtfWriter +{ + /// What is being written: notes are only allowed in the body (and its tables). + private enum Story + { + Body, + Note, + Header, + TextBox, + } + + /// How the last paragraph of a block list ends: with the story (no mark) or the cell (\cell, \nestcell). + private enum BlockEnd + { + Story, + Cell, + } + + /// Width (HWPUNIT) available to the content being written: column, table cell or text box. + private int _width = DefaultPage.TextWidth; + + // ───────────────────────── Blocks ───────────────────────── + + /// Tables around the blocks (0 outside a table, 1 in a cell of a table in the text ...). + private void Blocks(IReadOnlyList blocks, Story story, int depth, BlockEnd end) + { + // A table's caption is its paragraphs after it. + if (blocks.Any(b => b is HTable { Caption.Count: > 0 })) + blocks = blocks.SelectMany(b => b is HTable { Caption.Count: > 0 } t ? t.Caption.Prepend(b) : [b]).ToList(); + for (var i = 0; i < blocks.Count; i++) + { + _token.ThrowIfCancellationRequested(); + var last = i == blocks.Count - 1; + switch (blocks[i]) + { + case HParagraph paragraph: + var mark = !last ? "\\par" : end == BlockEnd.Cell ? CellEnd(depth) : ""; + Paragraph(paragraph, story, depth, mark); + break; + case HTable table: + // Two tables in a row would become one: a paragraph keeps them apart. + if (i > 0 && blocks[i - 1] is HTable) + EmptyParagraph(depth, "\\par"); + Table(table, story, depth + 1); + if (last) + EmptyParagraph(depth, end == BlockEnd.Cell ? CellEnd(depth) : ""); + break; + } + } + if (blocks.Count == 0 && end == BlockEnd.Cell) + EmptyParagraph(depth, CellEnd(depth)); + } + + private static string CellEnd(int depth) => depth > 1 ? "\\nestcell" : "\\cell"; + + /// A paragraph without text (at a table's end it also resets the paragraph properties the table had). + private void EmptyParagraph(int depth, string mark) + { + _out.Control("\\pard\\plain" + InTable(depth) + mark); + _out.Line(); + } + + private static string InTable(int depth) => depth == 0 ? "" : depth == 1 ? "\\intbl" : $"\\intbl\\itap{Number(depth)}"; + + // ───────────────────────── Paragraphs ───────────────────────── + + /// The formatting a paragraph's style gives it, which the paragraph repeats. + private readonly record struct StyleBase(int Style, bool Bold, int HalfPoints, int Before, int After, HLineSpacing Line, bool Keep, + IReadOnlyList? Tabs); + + /// Normal's line spacing: 160 % of the font size in 한글's terms (Word's 1.23 lines, \sl295\slmult1). + private static readonly HLineSpacing NormalLine = new(HLineSpacingKind.Percent, 160); + + /// Single lines (headers, footers and notes, as the DOCX writer's styles). + private static readonly HLineSpacing SingleLine = new(HLineSpacingKind.Percent, 130); + + /// The tab stops of the DOCX writer's header and footer styles: centre and right of an A4 text width. + private static readonly HTabStop[] HeaderTabs = [new(45130, HTabKind.Center, "NONE"), new(90260, HTabKind.Right, "NONE")]; + + private static StyleBase BaseOf(HParagraph paragraph, Story story) + { + if (paragraph.HeadingLevel is >= 1 and <= 9 and var level) + return new StyleBase(level, true, HeadingSizes[level - 1], level <= 2 ? 1200 : 800, 400, NormalLine, true, null); + return story switch + { + Story.Note => new StyleBase(0, false, 18, 0, 0, SingleLine, false, null), + Story.Header => new StyleBase(0, false, DefaultHalfPoints, 0, 0, SingleLine, false, HeaderTabs), + _ => new StyleBase(0, false, DefaultHalfPoints, 0, 0, NormalLine, false, null), + }; + } + + /// What ends the paragraph: \par, \cell, \nestcell, or nothing for the last one of its story. + private void Paragraph(HParagraph paragraph, Story story, int depth, string mark) + { + var o = _out; + var style = BaseOf(paragraph, story); + o.Control("\\pard\\plain" + ParagraphProperties(paragraph, style, depth)); + + // The paragraph mark's formatting is the paragraph's: runs change only what differs from it. + var markChars = Resolve(paragraph.MarkFormat, style); + o.Control(Difference(PlainChars, markChars) + $"\\f{Number(FontIndex(markChars.Latin, 0))}"); + var state = new RunState(markChars, FontIndex(markChars.Latin, 0)); + if (paragraph.ColumnBreakBefore) + o.Control("\\column"); + if (story == Story.Note && _noteNumber) + { + _noteNumber = false; + // The note's own number first (the model's note numbers are where the source had them; they are left out). + o.Open(); + o.Control("\\super\\chftn"); + o.Close(); + o.Raw(" "); + } + Inlines(paragraph.Inlines, state, style, story, depth); + o.Control(mark); + o.Line(); + } + + /// The note being written still needs its number (\chftn) in front of its first paragraph. + private bool _noteNumber; + + /// Paragraph control words after \pard: style, table, list, alignment, indents, spacing, keeps, tabs, borders. + private string ParagraphProperties(HParagraph paragraph, StyleBase style, int depth) + { + var f = paragraph.Format; + var sb = new StringBuilder(); + if (style.Style > 0) + sb.Append($"\\s{Number(style.Style)}\\outlinelevel{Number(style.Style - 1)}"); + sb.Append(InTable(depth)); + if (paragraph.List is { Numbered: true } list) + sb.Append($"\\ls{Number(ListNumber(list.Numbering))}\\ilvl{Number(Math.Clamp(list.Level, 0, 8))}"); + + // Right to left, the model's Align, Left and Right name the start and end side; RTF's \ql, \li ... are the + // left and right ones, and \lin / \rin the start and end. + var rtl = f.RightToLeft == true; + if (rtl) + sb.Append("\\rtlpar"); + var align = f.Align ?? HAlign.Left; + if (rtl && align is HAlign.Left or HAlign.Right) + align = align == HAlign.Left ? HAlign.Right : HAlign.Left; + sb.Append(align switch + { + HAlign.Center => "\\qc", + HAlign.Right => "\\qr", + HAlign.Justify => "\\qj", + HAlign.Distribute => "\\qd", + _ => "\\ql", + }); + + var (left, firstLine) = (f.Left, f.FirstLine); + if (paragraph.List is { } item && left is null) + { + // List paragraphs take their level's indent; continuation paragraphs line up with the item's text. + left = ListIndent(item.Level) * 5; + firstLine ??= item.Numbered ? -2000 : 0; + } + var start = Twips(left ?? 0); + var end = Twips(f.Right ?? 0); + sb.Append($"\\li{Number(rtl ? end : start)}\\ri{Number(rtl ? start : end)}\\lin{Number(start)}\\rin{Number(end)}\\fi{Number(Twips(firstLine ?? 0))}"); + sb.Append($"\\sb{Number(Math.Max(0, Twips(f.Before ?? style.Before)))}\\sa{Number(Math.Max(0, Twips(f.After ?? style.After)))}"); + var line = f.LineSpacing ?? style.Line; + sb.Append(line.Kind switch + { + HLineSpacingKind.Fixed => $"\\sl-{Number(Math.Max(20, Twips(line.Value)))}\\slmult0", + HLineSpacingKind.AtLeast => $"\\sl{Number(Math.Max(20, Twips(line.Value)))}\\slmult0", + // 한글 percentages are of the font size, RTF's multiples of the font's line (≈ 1.3 em), as in DOCX. + _ => $"\\sl{Number(Math.Clamp((int)Math.Round(line.Value * 240 / 130.0), 24, 2400))}\\slmult1", + }); + if (f.KeepWithNext ?? style.Keep) + sb.Append("\\keepn"); + if (f.KeepLines ?? style.Keep) + sb.Append("\\keep"); + sb.Append(f.WidowOrphan == false ? "\\nowidctlpar" : "\\widctlpar"); + if (paragraph.PageBreakBefore) + sb.Append("\\pagebb"); + + foreach (var tab in (f.Tabs ?? style.Tabs ?? []).OrderBy(t => t.Position)) + { + sb.Append(tab.Kind switch + { + HTabKind.Right => "\\tqr", + HTabKind.Center => "\\tqc", + HTabKind.Decimal => "\\tqdec", + _ => "", + }); + sb.Append(tab.Leader switch + { + "DOT" => "\\tldot", + "DASH" => "\\tlhyph", + "SOLID" => "\\tlul", + _ => "", + }); + // The model measures tab stops from the paragraph's indent, RTF from the margin. + sb.Append($"\\tx{Number(Math.Max(0, Twips(tab.Position + (left ?? 0))))}"); + } + + if (f.Borders is { } borders) + { + foreach (var (side, border) in new[] { ("t", borders.Top), ("l", borders.Left), ("b", borders.Bottom), ("r", borders.Right) }) + { + if (border.Style != HBorderStyle.None) + sb.Append($"\\brdr{side}").Append(Border(border)).Append("\\brsp20"); + } + } + else if (f.LeftBar is { } bar && bar.Style != HBorderStyle.None) + { + sb.Append("\\brdrl").Append(Border(bar)).Append("\\brsp120"); + } + if (ColorIndex(f.Fill) is > 0 and var fill) + sb.Append($"\\cbpat{Number(fill)}"); + return sb.ToString(); + } + + /// \brdrs\brdrw15\brdrcf1: a border's style, width (twips, at most 75) and colour. + private string Border(HBorder border) + { + var style = border.Style switch + { + HBorderStyle.Dash => "\\brdrdash", + HBorderStyle.Dot => "\\brdrdot", + HBorderStyle.DashDot => "\\brdrdashd", + HBorderStyle.Double => "\\brdrdb", + _ => "\\brdrs", + }; + var width = Math.Clamp((int)Math.Round(border.WidthMm / 25.4 * 1440), 1, 75); + var color = ColorIndex(border.Color); + return $"{style}\\brdrw{Number(width)}{(color > 0 ? $"\\brdrcf{Number(color)}" : "")}"; + } + + // ───────────────────────── Character formatting ───────────────────────── + + /// Character formatting with every member decided (the model's null ones from the paragraph's style). + /// 0 normal, 1 superscript, 2 subscript. + /// Letter spacing in twips (\expndtw). + private readonly record struct Chars(bool Bold, bool Italic, bool Underline, bool Strike, int Script, int HalfPoints, + string Latin, string EastAsian, int Color, int Shade, int Expand); + + /// What \plain leaves: RTF's 12 pt in the default font, nothing else. + private static readonly Chars PlainChars = new(false, false, false, false, 0, 24, DefaultLatinFont, DefaultEastAsianFont, 0, 0, 0); + + private Chars Resolve(HCharFormat f, StyleBase style) + { + var halfPoints = f.Size is { } size ? Math.Clamp((int)Math.Round(size / 50.0), 2, 3276) : style.HalfPoints; + // 한글 letter spacing is a percentage of the font size, RTF's an absolute distance in twips. + var expand = f.Spacing is { } spacing and not 0 ? (int)Math.Round(spacing * halfPoints * 10 / 100.0) : 0; + return new Chars( + f.Bold ?? style.Bold, + f.Italic ?? false, + f.Underline ?? false, + f.Strike ?? false, + f.Superscript == true ? 1 : f.Subscript == true ? 2 : 0, + halfPoints, + f.Font is { Length: > 0 } latin ? latin : DefaultLatinFont, + f.EastAsianFont is { Length: > 0 } eastAsian ? eastAsian : f.Font is { Length: > 0 } both ? both : DefaultEastAsianFont, + ColorIndex(f.Color), + ColorIndex(f.Shade), + expand); + } + + /// The control words that turn into (fonts are set per piece of text). + private static string Difference(Chars from, Chars to) + { + var sb = new StringBuilder(); + if (from.Bold != to.Bold) + sb.Append(to.Bold ? "\\b" : "\\b0"); + if (from.Italic != to.Italic) + sb.Append(to.Italic ? "\\i" : "\\i0"); + if (from.Underline != to.Underline) + sb.Append(to.Underline ? "\\ul" : "\\ulnone"); + if (from.Strike != to.Strike) + sb.Append(to.Strike ? "\\strike" : "\\strike0"); + if (from.Script != to.Script) + sb.Append(to.Script switch { 1 => "\\super", 2 => "\\sub", _ => "\\nosupersub" }); + if (from.HalfPoints != to.HalfPoints) + sb.Append($"\\fs{Number(to.HalfPoints)}"); + if (from.Color != to.Color) + sb.Append($"\\cf{Number(to.Color)}"); + if (from.Shade != to.Shade) + sb.Append($"\\chshdng0\\chcbpat{Number(to.Shade)}"); + if (from.Expand != to.Expand) + sb.Append($"\\expndtw{Number(to.Expand)}"); + return sb.ToString(); + } + + /// The formatting in effect where runs are written (the paragraph mark's), and the \f set there. + private sealed record RunState(Chars Chars, int Font); + + private void Inlines(IEnumerable inlines, RunState state, StyleBase style, Story story, int depth) + { + var o = _out; + foreach (var inline in inlines) + { + switch (inline) + { + case HText text: // equations too: their TeX source or linear text + Run(text.Format, state, style, () => Text(text.Text, Resolve(text.Format, style), state.Font)); + break; + case HLineBreak lineBreak: + Run(lineBreak.Format, state, style, () => o.Control("\\line")); + break; + case HTab tab: + Run(tab.Format, state, style, () => o.Control("\\tab")); + break; + case HLink link: + Link(link, state, style, story, depth); + break; + case HBookmark bookmark: + Bookmark(bookmark.Name); + break; + case HField field: + Run(field.Format, state, style, () => + { + o.Open(); + o.Control("\\field"); + o.Open(); + o.Control("\\*\\fldinst"); + o.Raw(field.Kind == HFieldKind.PageNumber ? "PAGE" : "NUMPAGES"); + o.Close(); + o.Open(); + o.Control("\\fldrslt"); + o.Text(field.Stored is { Length: > 0 } stored ? stored : "1", 1252); + o.Close(); + o.Close(); + }); + break; + case HNote note: + Note(note, state, style, story); + break; + case HImage image: + Picture(image); + break; + case HTextBox box: + TextBox(box, state, style); + break; + case HShape shape: + Shape(shape); + break; + } + } + } + + /// A group with the formatting of where it differs from the paragraph mark's. + private void Run(HCharFormat format, RunState state, StyleBase style, Action content) + { + _out.Open(); + _out.Control(Difference(state.Chars, Resolve(format, style))); + content(); + _out.Close(); + } + + /// + /// Text in pieces of one script: East Asian text in the East Asian font (of an East Asian \fcharset), other text + /// in the Latin font of its script's \fcharset; spaces and punctuation stay with the piece around them. Arabic + /// and Hebrew pieces are marked right to left (\rtlch), as Word does. + /// + private void Text(string text, Chars chars, int paragraphFont) + { + var o = _out; + var current = paragraphFont; + var start = 0; + var kind = Script.Neutral; + for (var i = 0; i <= text.Length; i++) + { + var next = i < text.Length ? ScriptOf(text, i) : Script.Neutral; + if (i < text.Length && (next == Script.Neutral || next == kind || kind == Script.Neutral)) + { + if (kind == Script.Neutral) + kind = next; + if (char.IsHighSurrogate(text[i]) && i + 1 < text.Length) + i++; + continue; + } + if (i > start) + { + var piece = text[start..i]; + var (font, codePage) = FontFor(kind, chars, piece); + if (font != current) + { + o.Control($"\\f{Number(font)}"); + current = font; + } + if (kind is Script.Arabic or Script.Hebrew) + o.Control("\\rtlch"); + o.Text(piece, codePage); + if (kind is Script.Arabic or Script.Hebrew) + o.Control("\\ltrch"); + } + start = i; + kind = next; + } + } + + /// The scripts text pieces are split by (each has an \fcharset of its own). + private enum Script + { + /// Spaces, digits and punctuation: they go with the text around them. + Neutral, + Latin, + Greek, + Cyrillic, + Hebrew, + Arabic, + Thai, + + /// Hangul, kana and Chinese characters, with CJK punctuation and full-width forms. + EastAsian, + } + + private static Script ScriptOf(string text, int i) + { + var c = text[i]; + if (char.IsHighSurrogate(c)) + return c is >= '\uD840' and <= '\uD87F' ? Script.EastAsian : Script.Latin; // CJK extensions B..F + if (char.IsLowSurrogate(c)) + return Script.Neutral; + if (c < '\u0080') + return char.IsAsciiLetter(c) ? Script.Latin : Script.Neutral; + return c switch + { + >= '\u0370' and <= '\u03FF' => Script.Greek, + >= '\u0400' and <= '\u052F' => Script.Cyrillic, + >= '\u0590' and <= '\u05FF' => Script.Hebrew, + >= '\u0600' and <= '\u06FF' or >= '\u0750' and <= '\u077F' => Script.Arabic, + >= '\u0E00' and <= '\u0E7F' => Script.Thai, + >= '\u1100' and <= '\u11FF' or >= '\u3000' and <= '\u4DBF' or >= '\u4E00' and <= '\u9FFF' or >= '\uAC00' and <= '\uD7AF' + or >= '\uF900' and <= '\uFAFF' or >= '\uFF00' and <= '\uFFEF' => Script.EastAsian, + >= '\u2000' and <= '\u206F' or '\u00A0' => Script.Neutral, + _ => Script.Latin, + }; + } + + /// The font index and code page of a piece of text of . + private (int Font, int CodePage) FontFor(Script script, Chars chars, string piece) + { + if (script == Script.EastAsian) + { + // The text that first uses the font decides its \fcharset: Korean, Japanese, else by the font's name. + var charset = piece.Any(c => c is >= '\uAC00' and <= '\uD7AF' or >= '\u1100' and <= '\u11FF' or >= '\u3130' and <= '\u318F') ? 129 + : piece.Any(c => c is >= '\u3040' and <= '\u30FF' or >= '\u31F0' and <= '\u31FF') ? 128 + : HanCharset(chars.EastAsian); + var index = EastAsianFontIndex(chars.EastAsian, charset); + return (index, CodePage(_fonts[index].Charset)); + } + var latin = script switch + { + Script.Greek => 161, + Script.Cyrillic => 204, + Script.Hebrew => 177, + Script.Arabic => 178, + Script.Thai => 222, + _ => 0, + }; + return (FontIndex(chars.Latin, latin), CodePage(latin)); + } + + /// The \fcharset of Chinese characters in a font: Korean, Japanese or Chinese by the font's name. + private static int HanCharset(string font) + { + if (font.Any(c => c is >= '\uAC00' and <= '\uD7AF') || font.Contains("Gothic", StringComparison.OrdinalIgnoreCase) && font.Contains("Malgun", StringComparison.OrdinalIgnoreCase) + || font is "Batang" or "BatangChe" or "Gulim" or "GulimChe" or "Dotum" or "DotumChe" or "Gungsuh") + return 129; + if (font.Any(c => c is >= '\u3040' and <= '\u30FF') || font.StartsWith("MS ", StringComparison.Ordinal) || font.StartsWith("Yu ", StringComparison.Ordinal) + || font.StartsWith("Meiryo", StringComparison.Ordinal)) + return 128; + if (font.Contains("Ming", StringComparison.OrdinalIgnoreCase) || font is "新細明體" or "細明體" or "標楷體" or "微軟正黑體" or "Microsoft JhengHei" or "DFKai-SB") + return 136; + if (font.Any(c => c is >= '\u4E00' and <= '\u9FFF') || font.StartsWith("Sim", StringComparison.Ordinal) || font.Contains("YaHei", StringComparison.Ordinal)) + return 134; + return 129; // the default East Asian font, 맑은 고딕 + } + + // ───────────────────────── Links, bookmarks, notes ───────────────────────── + + /// A HYPERLINK field: its instruction (\l for a bookmark in the document) and the link's text as its result. + private void Link(HLink link, RunState state, StyleBase style, Story story, int depth) + { + var o = _out; + var target = link.Target.Replace("\"", "%22", StringComparison.Ordinal); + o.Open(); + o.Control("\\field"); + o.Open(); + o.Control("\\*\\fldinst"); + o.Raw("HYPERLINK "); + if (target.StartsWith('#')) + { + o.Text("\\l \"" + target[1..] + "\"", 1252); + } + else + { + o.Text("\"" + target + "\"", 1252); + } + o.Close(); + o.Open(); + o.Control("\\fldrslt"); + Inlines(link.Content, state, style, story, depth); + o.Close(); + o.Close(); + } + + private void Bookmark(string name) + { + foreach (var kind in new[] { "\\*\\bkmkstart", "\\*\\bkmkend" }) + { + _out.Open(); + _out.Control(kind); + _out.Text(name, 1252); + _out.Close(); + } + } + + /// + /// A footnote or endnote (\footnote, \ftnalt for an endnote) after its reference mark (\chftn). Notes are only in + /// the body text and its tables, as in Word: elsewhere the note's text stays in place, in brackets. + /// + private void Note(HNote note, RunState state, StyleBase style, Story story) + { + var o = _out; + if (story != Story.Body) + { + var text = string.Concat(note.Blocks.OfType().SelectMany(p => p.Inlines).OfType().Select(t => t.Text)); + Run(note.Format ?? default, state, style, () => Text($" ({text})", Resolve(note.Format ?? default, style), state.Font)); + return; + } + var reference = Resolve(note.Format ?? default, style) with { Script = 1 }; + o.Open(); + o.Control(Difference(state.Chars, reference) + "\\chftn"); + o.Open(); + o.Control(note.Endnote ? "\\footnote\\ftnalt" : "\\footnote"); + var outer = _width; + _noteNumber = true; + Blocks(note.Blocks.Count > 0 ? note.Blocks : [new HParagraph()], Story.Note, 0, BlockEnd.Story); + _width = outer; + _noteNumber = false; + o.Close(); + o.Close(); + } + + // ───────────────────────── Tables ───────────────────────── + + /// Cell padding where neither the cell nor the table gives one: Word's (and the DOCX writer's) 0.19 cm left and right. + private static readonly HInsets DefaultCellMargin = new(540, 540, 0, 0); + + /// The table's level: 1 in the text, 2 in a cell of such a table ... + private void Table(HTable table, Story story, int depth) + { + var o = _out; + if (table.Rows.Count == 0) + return; + var (placed, columnCount) = HwpxWriter.PlaceCells(table); + var available = Math.Max(4000, _width - (table.Align == HAlign.Left ? table.Indent : 0)); + var widths = HwpxWriter.ColumnWidths(table, columnCount, available).Select(w => Math.Max(1, w)).ToArray(); + // Each grid edge in twips from the margin, rounded once so the cells add up. + var left = table.Align == HAlign.Left ? table.Indent : 0; + var edges = new int[columnCount + 1]; + long position = left; + edges[0] = Twips(left); + for (var c = 0; c < columnCount; c++) + { + position += widths[c]; + edges[c + 1] = Math.Max(edges[c] + 1, (int)Math.Round(position / 5.0)); + } + + // Per column: the row a cell written so far covers it to (exclusive), and that cell with its first column. + var coveredTo = new int[columnCount]; + var coveredBy = new (HCell Cell, int Column)[columnCount]; + var margin = table.CellMargin ?? DefaultCellMargin; + var headerRows = table.Rows.TakeWhile(r => r.Header).Count(); + var outerWidth = _width; + for (var r = 0; r < table.Rows.Count; r++) + { + _token.ThrowIfCancellationRequested(); + var row = table.Rows[r]; + var starts = r < placed.Count ? placed[r].GroupBy(p => p.Column).ToDictionary(g => g.Key, g => g.First().Cell) : []; + + // The row's cells: (definition, content, the cell's text width) for each grid position. + var cells = new List<(string Definition, HCell? Content, int Width)>(); + for (var column = 0; column < columnCount;) + { + if (starts.TryGetValue(column, out var cell)) + { + var span = Math.Clamp(cell.ColSpan, 1, columnCount - column); + var rowSpan = Math.Clamp(cell.RowSpan, 1, table.Rows.Count - r); + for (var c = column; c < column + span; c++) + { + coveredTo[c] = r + rowSpan; + coveredBy[c] = (cell, column); + } + var cellMargin = cell.CellMargin ?? margin; + var width = Math.Max(1000, widths.Skip(column).Take(span).Sum() - cellMargin.Left - cellMargin.Right); + for (var k = 0; k < span; k++) + { + var merge = (rowSpan > 1 ? "\\clvmgf" : "") + (span > 1 ? k == 0 ? "\\clmgf" : "\\clmrg" : ""); + cells.Add((CellDefinition(cell, table, merge, edges[column + k + 1], edges[column + k + 1] - edges[column + k]), k == 0 ? cell : null, width)); + } + column += span; + } + else if (coveredTo[column] > r && coveredBy[column] is (HCell origin, var originColumn) && originColumn == column) + { + // Below a cell spanning rows: continuation cells, as many columns wide. + var span = Math.Clamp(origin.ColSpan, 1, columnCount - column); + for (var k = 0; k < span; k++) + { + var merge = "\\clvmrg" + (span > 1 ? k == 0 ? "\\clmgf" : "\\clmrg" : ""); + cells.Add((CellDefinition(origin, table, merge, edges[column + k + 1], edges[column + k + 1] - edges[column + k]), null, 0)); + } + column += span; + } + else + { + // A gap in a ragged row (or inside a span that started left of it): an empty cell keeps the grid. + cells.Add(($"\\cellx{Number(edges[column + 1])}", null, 0)); + column++; + } + } + + var definition = RowDefinition(table, row, r < headerRows, margin, edges[0]) + string.Concat(cells.Select(c => c.Definition)); + if (depth == 1) + { + o.Control(definition); + o.Line(); + } + var firstCell = true; + foreach (var (_, content, width) in cells) + { + if (content is null) + { + EmptyParagraph(depth, CellEnd(depth)); + continue; + } + _width = width; + var blocks = content.Blocks.Count > 0 ? content.Blocks : [new HParagraph()]; + if (firstCell && r == 0 && table.PageBreakBefore && blocks[0] is HParagraph opening) + { + // The page break goes with the table's first paragraph. + var copy = new HParagraph { Format = opening.Format, MarkFormat = opening.MarkFormat, HeadingLevel = opening.HeadingLevel, List = opening.List, PageBreakBefore = true }; + copy.Inlines.AddRange(opening.Inlines); + blocks = [copy, .. blocks.Skip(1)]; + } + firstCell = false; + Blocks(blocks, story, depth, BlockEnd.Cell); + } + _width = outerWidth; + if (depth == 1) + { + o.Control("\\row"); + } + else + { + // A nested table's row ends with its definition. + o.Open(); + o.Control("\\*\\nesttableprops" + definition + "\\nestrow"); + o.Close(); + } + o.Line(); + } + } + + /// \trowd and the row's own properties: padding, indent, alignment, height, header and keep. + private string RowDefinition(HTable table, HRow row, bool header, HInsets margin, int leftEdge) + { + var sb = new StringBuilder("\\trowd"); + sb.Append($"\\trgaph{Number(Twips(margin.Left))}\\trleft{Number(leftEdge)}"); + sb.Append(table.Align switch + { + HAlign.Center => "\\trqc", + HAlign.Right => "\\trqr", + _ => "\\trql", + }); + if (table.RightToLeft) + sb.Append("\\rtlrow"); + if (row.Height is > 0 and var height) + sb.Append($"\\trrh{Number(Twips(height))}"); + if (header) + sb.Append("\\trhdr"); + if (row.CantSplit) + sb.Append("\\trkeep"); + sb.Append($"\\trpaddl{Number(Twips(margin.Left))}\\trpaddr{Number(Twips(margin.Right))}\\trpaddt{Number(Twips(margin.Top))}\\trpaddb{Number(Twips(margin.Bottom))}") + .Append("\\trpaddfl3\\trpaddfr3\\trpaddft3\\trpaddfb3"); + return sb.ToString(); + } + + /// A cell's definition up to its \cellx: merges, vertical alignment, borders, shading, padding and width. + private string CellDefinition(HCell cell, HTable table, string merge, int right, int width) + { + var sb = new StringBuilder(merge); + sb.Append(cell.VerticalAlign switch + { + HVerticalAlign.Center => "\\clvertalc", + HVerticalAlign.Bottom => "\\clvertalb", + _ => "\\clvertalt", + }); + var borders = cell.Borders ?? table.Borders ?? HBorders.Grid; + foreach (var (side, border) in new[] { ("t", borders.Top), ("l", borders.Left), ("b", borders.Bottom), ("r", borders.Right) }) + { + if (border.Style != HBorderStyle.None) + sb.Append($"\\clbrdr{side}").Append(Border(border)); + } + if (ColorIndex(cell.Fill) is > 0 and var fill) + sb.Append($"\\clcbpat{Number(fill)}"); + if (cell.CellMargin is { } m) + { + // Word reads \clpadl as the top padding and \clpadt as the left one, and writes them so; so does this. + sb.Append($"\\clpadt{Number(Twips(m.Left))}\\clpadft3\\clpadr{Number(Twips(m.Right))}\\clpadfr3") + .Append($"\\clpadl{Number(Twips(m.Top))}\\clpadfl3\\clpadb{Number(Twips(m.Bottom))}\\clpadfb3"); + } + sb.Append($"\\clftsWidth3\\clwWidth{Number(Math.Max(1, width))}\\cellx{Number(right)}"); + return sb.ToString(); + } +} diff --git a/src/Filee.Engines/Hwp/Hwpx/RtfWriter.Objects.cs b/src/Filee.Engines/Hwp/Hwpx/RtfWriter.Objects.cs new file mode 100644 index 000000000..151ab689e --- /dev/null +++ b/src/Filee.Engines/Hwp/Hwpx/RtfWriter.Objects.cs @@ -0,0 +1,323 @@ +// Objects in the text of an RTF file: pictures as \pict groups, text boxes and shapes as Word drawing objects (\shp). +// +// A picture keeps its PNG or JPEG bytes (\pngblip, \jpegblip), an EMF or WMF stays a metafile (\emfblip, \wmetafile8 +// without the placeable header WMF files start with); any other picture becomes PNG. Its bytes are hex, 64 to a line. +// Pictures stay in the text like characters (floating ones too, where they were anchored). +// +// A text box is a shape (\shp) with its paragraphs in \shptxt, and the same text in \shprslt for programs that don't +// draw shapes (RtfReader reads it from there); shapes without text have no result. + +using Filee.Engines.Documents; +using Filee.Engines.Magick; +using ImageMagick; + +namespace Filee.Engines.Hwp.Hwpx; + +internal sealed partial class RtfWriter +{ + // ───────────────────────── Pictures ───────────────────────── + + private void Picture(HImage image) + { + if (!File.Exists(image.Path) || PictureData(image.Path) is not { } picture) + return; // missing or unreadable picture: skip rather than write a broken one + + var (width, height) = (image.Width, image.Height); + if ((width is null || height is null) && picture.Pixels is { } pixels && pixels.Width > 0 && pixels.Height > 0) + { + // Pixels at 96 dpi, like Word and browsers show pictures without a physical size. + if (width is null && height is null) + (width, height) = ((int)(pixels.Width * HwpxUnits.PerPixel), (int)(pixels.Height * HwpxUnits.PerPixel)); + else if (width is not null) + height = (int)(width.Value * (double)pixels.Height / pixels.Width); + else + width = (int)(height!.Value * (double)pixels.Width / pixels.Height); + } + var w = Math.Max(1, width ?? 8504); + var h = Math.Max(1, height ?? w); + if (image.Anchor is null && w > _width) + { + // Pictures in the text never overflow its width. + h = (int)((long)h * _width / w); + w = _width; + } + + var o = _out; + o.Open(); + // \picw and \pich: pixels for bitmaps, 0.01 mm for metafiles. + var (picW, picH) = picture.Blip is "emfblip" or "wmetafile8" + ? ((int)Math.Round(w / HwpxUnits.PerMm * 100), (int)Math.Round(h / HwpxUnits.PerMm * 100)) + : (picture.Pixels?.Width ?? Math.Max(1, (int)(w / HwpxUnits.PerPixel)), picture.Pixels?.Height ?? Math.Max(1, (int)(h / HwpxUnits.PerPixel))); + o.Control($"\\pict\\{picture.Blip}\\picw{Number(picW)}\\pich{Number(picH)}\\picwgoal{Number(Twips(w))}\\pichgoal{Number(Twips(h))}"); + o.Line(); + var data = picture.Bytes; + const int PerLine = 64; + for (var i = 0; i < data.Length; i += PerLine) + { + _token.ThrowIfCancellationRequested(); + o.Raw(Convert.ToHexString(data, i, Math.Min(PerLine, data.Length - i)).ToLowerInvariant()); + o.Line(); + } + o.Close(); + } + + /// A picture's bytes as RTF takes them, its blip word and its pixel size; null when it can't be written. + private sealed record PictureBytes(byte[] Bytes, string Blip, (int Width, int Height)? Pixels); + + private readonly Dictionary _pictures = new(StringComparer.OrdinalIgnoreCase); + + private PictureBytes? PictureData(string path) + { + if (_pictures.TryGetValue(path, out var known)) + return known; + return _pictures[path] = ReadPicture(path); + } + + private PictureBytes? ReadPicture(string path) + { + // A picture larger than a part of a document may be, or claiming more pixels than any picture has, is left + // out before anything decodes it (RtfReader refuses such pictures too). + if (new FileInfo(path).Length > _limits.MaxPartBytes) + return null; + var pixels = PictureSize.Read(path); + if (pixels is { } size && (long)size.Width * size.Height > _limits.MaxPicturePixels) + return null; + var bytes = File.ReadAllBytes(path); + if (bytes is [0x89, (byte)'P', (byte)'N', (byte)'G', ..]) + return new PictureBytes(bytes, "pngblip", pixels); + if (bytes is [0xFF, 0xD8, 0xFF, ..]) + return new PictureBytes(bytes, "jpegblip", pixels); + if (bytes.Length > 44 && bytes[0] == 1 && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0 && bytes[40] == 0x20 && bytes[41] == 0x45 && bytes[42] == 0x4D && bytes[43] == 0x46) + return new PictureBytes(bytes, "emfblip", pixels); + if (bytes is [0xD7, 0xCD, 0xC6, 0x9A, ..] && bytes.Length > 22) + return new PictureBytes(bytes[22..], "wmetafile8", pixels); // RTF has the metafile without its placeable header + if (bytes is [1 or 2, 0, 9, 0, ..]) + return new PictureBytes(bytes, "wmetafile8", pixels); + + // GIF, BMP, WebP, TIFF ... → PNG, which every RTF reader shows. + try + { + var info = new MagickImageInfo(path); + if ((long)info.Width * info.Height > _limits.MaxPicturePixels) + return null; + using var image = new MagickImage(path); + DdsChannelOrder.Correct(path, image); + return new PictureBytes(image.ToByteArray(MagickFormat.Png), "pngblip", ((int)image.Width, (int)image.Height)); + } + catch (MagickException) + { + return null; + } + } + + // ───────────────────────── Text boxes and shapes ───────────────────────── + + private void TextBox(HTextBox box, RunState state, StyleBase style) + { + var o = _out; + var w = Math.Max(1, box.Width); + var h = Math.Max(1, box.Height); + o.Open(); + o.Control("\\shp"); + o.Open(); + o.Control("\\*\\shpinst" + ShapePlacement(box.Anchor, w, h, box.ZOrder)); + var type = box.Shape == HShapeKind.Ellipse ? 3 : box.CornerRatio > 0 ? 2 : 202; + Property("shapeType", Number(type)); + Fill(box.Fill ?? box.Gradient?.Colors.FirstOrDefault()); + Line(box.Line); + Property("dxTextLeft", Number(Emu(box.Padding.Left))); + Property("dxTextRight", Number(Emu(box.Padding.Right))); + Property("dyTextTop", Number(Emu(box.Padding.Top))); + Property("dyTextBottom", Number(Emu(box.Padding.Bottom))); + Property("anchorText", box.VerticalAlign switch { HVerticalAlign.Center => "1", HVerticalAlign.Bottom => "2", _ => "0" }); + if (box.CornerRatio > 0) + Property("adjustValue", Number((int)Math.Round(Math.Clamp(box.CornerRatio, 0, 50) / 100.0 * 21600))); + Transform(box.Transform); + + var outer = _width; + _width = Math.Max(1000, w - box.Padding.Left - box.Padding.Right); + o.Open(); + o.Control("\\shptxt"); + Blocks(box.Blocks.Count > 0 ? box.Blocks : [new HParagraph()], Story.TextBox, 0, BlockEnd.Story); + o.Close(); + _width = outer; + o.Close(); + + // The same text, in the paragraph, for programs that don't draw shapes: its paragraphs as lines. + o.Open(); + o.Control("\\shprslt"); + var first = true; + foreach (var paragraph in box.Blocks.OfType()) + { + if (!first) + o.Control("\\line"); + first = false; + Inlines(paragraph.Inlines.Where(i => i is HText or HTab or HLineBreak or HLink).ToList(), state, style, Story.TextBox, 0); + } + o.Close(); + o.Close(); + } + + private void Shape(HShape shape) + { + var type = shape.Kind switch + { + HShapeKind.Rectangle => shape.CornerRatio > 0 ? 2 : 1, + HShapeKind.Ellipse => 3, + HShapeKind.Line => 20, + _ => 0, // polygons, curves and arcs come only from models read for layout + }; + if (type == 0) + return; + var o = _out; + var w = Math.Max(1, shape.Width); + var h = Math.Max(shape.Kind == HShapeKind.Line ? 0 : 1, shape.Height); + o.Open(); + o.Control("\\shp"); + o.Open(); + o.Control("\\*\\shpinst" + ShapePlacement(shape.Anchor, w, h, shape.ZOrder)); + Property("shapeType", Number(type)); + if (shape.Kind == HShapeKind.Line) + Property("fFilled", "0"); + else + Fill(shape.Fill ?? shape.Gradient?.Colors.FirstOrDefault()); + Line(shape.Line); + if (shape.Kind == HShapeKind.Rectangle && shape.CornerRatio > 0) + Property("adjustValue", Number((int)Math.Round(Math.Clamp(shape.CornerRatio, 0, 50) / 100.0 * 21600))); + if (shape.FlipHorizontal) + Property("fFlipH", "1"); + if (shape.FlipVertical) + Property("fFlipV", "1"); + Transform(shape.Transform); + o.Close(); + o.Close(); + } + + /// + /// \shpleft ... \shpbottom with what they are measured from, the wrap and the stacking order. An object in the + /// text (no anchor) is placed at its paragraph, with text above and below it. + /// + private string ShapePlacement(HAnchor? anchor, int width, int height, int? zOrder) + { + var x = anchor is null ? 0 : Twips(anchor.HorizontalOffset); + var y = anchor is null ? 0 : Twips(anchor.VerticalOffset); + var horizontal = anchor?.HorizontalRelativeTo switch + { + "PAPER" => "\\shpbxpage", + "PAGE" => "\\shpbxmargin", + _ => "\\shpbxcolumn", + }; + var vertical = anchor?.VerticalRelativeTo switch + { + "PAPER" => "\\shpbypage", + "PAGE" => "\\shpbymargin", + _ => "\\shpbypara", + }; + var wrap = anchor?.Wrap switch + { + null or "TOP_AND_BOTTOM" => "\\shpwr1", + "BEHIND_TEXT" => "\\shpwr3\\shpfblwtxt1", + "IN_FRONT_OF_TEXT" => "\\shpwr3\\shpfblwtxt0", + _ => "\\shpwr2\\shpwrk0", + }; + var id = ++_shapes; + return $"\\shpleft{Number(x)}\\shptop{Number(y)}\\shpright{Number(x + Twips(width))}\\shpbottom{Number(y + Twips(height))}" + + $"\\shpfhdr0{horizontal}\\shpbxignore{vertical}\\shpbyignore{wrap}\\shpz{Number(Math.Max(0, zOrder ?? id))}\\shplid{Number(1024 + id)}" + + PositionProperties(anchor); + } + + private int _shapes; + + /// An aligned anchor (centre, right ... without an offset) as Word's posh / posv shape properties. + private static string PositionProperties(HAnchor? anchor) + { + if (anchor is null) + return ""; + var sb = new System.Text.StringBuilder(); + var posh = anchor.HorizontalOffset != 0 ? 0 : anchor.HorizontalAlign switch + { + "CENTER" => 2, + "RIGHT" => 3, + "INSIDE" => 4, + "OUTSIDE" => 5, + _ => 0, + }; + if (posh > 0) + sb.Append($"{{\\sp{{\\sn posh}}{{\\sv {Number(posh)}}}}}"); + var posv = anchor.VerticalOffset != 0 ? 0 : anchor.VerticalAlign switch + { + "CENTER" => 2, + "BOTTOM" => 3, + "INSIDE" => 4, + "OUTSIDE" => 5, + _ => 0, + }; + if (posv > 0) + sb.Append($"{{\\sp{{\\sn posv}}{{\\sv {Number(posv)}}}}}"); + return sb.ToString(); + } + + /// A shape property: {\sp{\sn name}{\sv value}}. + private void Property(string name, string value) + { + _out.Control($"{{\\sp{{\\sn {name}}}{{\\sv {value}}}}}"); + } + + private void Fill(string? color) + { + if (Odt.Odf.Color(color) is { } fill) + { + Property("fFilled", "1"); + Property("fillColor", Number(Bgr(fill))); + } + else + { + Property("fFilled", "0"); + } + } + + private void Line(HBorder line) + { + if (line.Style == HBorderStyle.None) + { + Property("fLine", "0"); + return; + } + Property("fLine", "1"); + Property("lineColor", Number(Bgr(Odt.Odf.Color(line.Color) ?? "#000000"))); + Property("lineWidth", Number((int)Math.Round(Math.Max(0, line.WidthMm) * 36000))); + var dash = line.Style switch + { + HBorderStyle.Dash => 1, + HBorderStyle.Dot => 2, + HBorderStyle.DashDot => 3, + _ => 0, + }; + if (dash > 0) + Property("lineDashing", Number(dash)); + if (line.Style == HBorderStyle.Double) + Property("lineStyle", "1"); + } + + private void Transform(HTransform? transform) + { + if (transform is not { IsIdentity: false } t) + return; + if (t.Rotation != 0) + Property("rotation", Number((int)Math.Round(t.Rotation * 65536))); + if (t.FlipHorizontal) + Property("fFlipH", "1"); + if (t.FlipVertical) + Property("fFlipV", "1"); + } + + /// "#RRGGBB" as the BGR number shape properties take. + private static int Bgr(string hex) + { + var value = Convert.ToInt32(hex[1..], 16); + return (value >> 16 & 0xFF) | (value & 0xFF00) | (value & 0xFF) << 16; + } + + /// HWPUNIT → EMU (shape properties' lengths). + private static int Emu(int hwpUnit) => (int)Math.Min(int.MaxValue, Math.Round(hwpUnit * 127.0)); +} diff --git a/src/Filee.Engines/Hwp/Hwpx/RtfWriter.cs b/src/Filee.Engines/Hwp/Hwpx/RtfWriter.cs new file mode 100644 index 000000000..6b4c9bebb --- /dev/null +++ b/src/Filee.Engines/Hwp/Hwpx/RtfWriter.cs @@ -0,0 +1,777 @@ +// Writes Rich Text Format from the HDocument model — the counterpart of RtfReader, written by hand from the RTF 1.9 +// specification like the DOCX, ODT and HWPX writers, so anything Filee's readers understand becomes an RTF file that +// Word, WordPad and LibreOffice open, without LibreOffice or Pandoc. +// +// Split over partial files: +// * RtfWriter.cs the file: its header (fonts with their other names, colours, styles, lists, information, +// document settings), sections with their page setup, headers and footers; the output and +// how text is escaped; +// * RtfWriter.Body.cs paragraphs, character formatting, links, bookmarks, fields, notes and tables; +// * RtfWriter.Objects.cs pictures (PNG and JPEG as they are, EMF and WMF as metafiles, others as PNG), text boxes +// and shapes. +// +// The body is written first, into a temporary file beside the output, because the font, colour and list tables that +// come before it fill as the body uses them. The file is ASCII: other characters are \uN followed by the same +// character in the code page of the font it is in (\ucN says how many bytes that takes), or "?" where that code page +// has none, so programs without Unicode RTF still show what they can. + +using System.Globalization; +using System.Text; +using Filee.Engines.Documents; +using Filee.Engines.Typesetting; + +namespace Filee.Engines.Hwp.Hwpx; + +/// Converts an into an .rtf file. +internal sealed partial class RtfWriter +{ + /// A4 with one-inch margins and headers / footers 1.5 cm from the edge, as the DOCX writer has it. + private static readonly HPage DefaultPage = new(59528, 84188, 7200, 7200, 4255, 4255, 2945, 2945, 0); + + /// The fonts the model leaves open: the DOCX writer's Calibri and 맑은 고딕 (font 0 and 1). + private const string DefaultLatinFont = "Calibri"; + private const string DefaultEastAsianFont = "맑은 고딕"; + + /// Body text size (half points): 10 pt, the DOCX writer's Normal. + private const int DefaultHalfPoints = 20; + + /// Word's distance between default tab stops (twips) when the model gives none: 한글's 40 pt. + private const int DefaultTabTwips = 800; + + /// Heading sizes (half points) of "heading 1".."heading 9", as the DOCX writer's. + private static readonly int[] HeadingSizes = [32, 28, 26, 24, 22, 20, 20, 20, 20]; + + private readonly HDocument _document; + private readonly DocumentLimits _limits; + private readonly CancellationToken _token; + private Output _out = null!; + + private RtfWriter(HDocument document, DocumentLimits limits, CancellationToken token) + { + _document = document; + _limits = limits; + _token = token; + // Font 0 and 1 are the defaults, whatever the text uses. + FontIndex(DefaultLatinFont, 0); + EastAsianFontIndex(DefaultEastAsianFont, 129); + } + + /// Writes as an .rtf file (replaced atomically, through a temporary file of its own). + public static void Write(HDocument document, string outputPath, CancellationToken cancellationToken = default) => + Write(document, outputPath, DocumentLimits.Default, cancellationToken); + + /// Writes within other than (for tests). + internal static void Write(HDocument document, string outputPath, DocumentLimits limits, CancellationToken cancellationToken = default) + { + Nesting.Limit(document); // the writer recurses per table, text box and note level + System.Text.Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + var writer = new RtfWriter(document, limits, cancellationToken); + + // The body goes into a file of its own first: the tables before it are only known once it is written. + using var body = WriterTempFile.Create(outputPath, out var bodyPath); + try + { + writer._out = new Output(body, limits.MaxPackageBytes, cancellationToken); + writer.Body(); + writer._out.Flush(); + var bodyLength = body.Length; + WriterTempFile.Write(outputPath, file => + { + var header = new Output(file, limits.MaxPackageBytes - bodyLength, cancellationToken); + writer._out = header; + writer.Header(); + header.Flush(); + body.Position = 0; + body.CopyTo(file); + file.WriteByte((byte)'}'); + }); + } + finally + { + body.Dispose(); + WriterTempFile.Discard(bodyPath); + } + } + + // ───────────────────────── Output ───────────────────────── + + /// + /// The RTF text being written: control words, groups and escaped text. It counts what it writes against the limit + /// a file may have (the RTF reader refuses larger ones) and keeps the \uc value of each open group. + /// + private sealed class Output(Stream stream, long limit, CancellationToken token) + { + private readonly StreamWriter _writer = new(stream, Encoding.ASCII, 1 << 16, leaveOpen: true); + private readonly Stack _ucs = new(); + private long _written; + private long _checked; + + /// A control word was written last: text after it needs a space between. + private bool _delimit; + + /// Bytes of fallback after each \uN in the current group. + public int Uc { get; private set; } = 1; + + public int Depth => _ucs.Count; + + public void Flush() => _writer.Flush(); + + private void Put(string text) + { + _written += text.Length; + if (_written > limit) + throw DocumentLimits.Exceeded($"The RTF file would be larger than {limit / (1024 * 1024)} MB."); + if (_written - _checked > 1 << 16) + { + _checked = _written; + token.ThrowIfCancellationRequested(); + } + _writer.Write(text); + } + + private void Put(char c) + { + if (++_written > limit) + throw DocumentLimits.Exceeded($"The RTF file would be larger than {limit / (1024 * 1024)} MB."); + _writer.Write(c); + } + + public void Open() + { + Put('{'); + _ucs.Push(Uc); + _delimit = false; + } + + public void Close() + { + Put('}'); + if (_ucs.Count > 0) + Uc = _ucs.Pop(); + _delimit = false; + } + + /// One or more control words ("\b\fs20"), or a control symbol. + public void Control(string words) + { + Put(words); + _delimit = words.Length > 0 && (char.IsAsciiLetterOrDigit(words[^1]) || words[^1] == '-'); + } + + /// A byte as \'hh (in \leveltext and \levelnumbers). + public void Byte(int value) + { + Put($"\\'{value & 0xFF:x2}"); + _delimit = false; + } + + /// A line end between pieces of the file (RTF ignores it; it keeps lines short for people reading it). + public void Line() + { + Put("\r\n"); + _delimit = false; + } + + /// ASCII text that needs no escaping (hex digits of a picture). + public void Raw(string text) + { + if (_delimit) + Put(' '); + Put(text); + _delimit = false; + } + + /// + /// Text with RTF's special characters escaped, tabs and line ends as \tab and \line, and characters beyond ASCII + /// as \uN with their bytes in (or "?") for programs that don't read \u. + /// + public void Text(string text, int codePage) + { + for (var i = 0; i < text.Length; i++) + { + var c = text[i]; + switch (c) + { + case '\\' or '{' or '}': + Put('\\'); + Put(c); + _delimit = false; + continue; + case '\t': + Control("\\tab"); + continue; + case '\n': + Control("\\line"); + continue; + case '\r': + if (i + 1 >= text.Length || text[i + 1] != '\n') + Control("\\line"); + continue; + case '\u00A0': + Control("\\~"); + continue; + case '\u2011': + Control("\\_"); + continue; + case '\u00AD': + Control("\\-"); + continue; + } + if (c < ' ' || c == '\u007F') + continue; + if (c < '\u0080') + { + if (_delimit) + Put(' '); + _delimit = false; + Put(c); + continue; + } + var fallback = Fallback(c, codePage); + if (fallback.Length != Uc) + { + Uc = fallback.Length; + Control($"\\uc{Uc}"); + } + Control($"\\u{(short)c}"); + foreach (var b in fallback) + { + if (b is >= 0x20 and < 0x7F && b is not ((byte)'\\' or (byte)'{' or (byte)'}')) + { + // After \uN only a digit or a space would be taken as part of the control word. + if (_delimit && (char.IsAsciiDigit((char)b) || b == ' ')) + Put(' '); + Put((char)b); + } + else + { + Put($"\\'{b:x2}"); + } + _delimit = false; + } + } + } + } + + private static readonly Dictionary Encodings = []; + + /// The bytes of in , or "?" when it has none. + private static byte[] Fallback(char c, int codePage) + { + if (char.IsSurrogate(c)) + return [(byte)'?']; + Encoding encoding; + lock (Encodings) + { + if (!Encodings.TryGetValue(codePage, out encoding!)) + { + try + { + encoding = System.Text.Encoding.GetEncoding(codePage, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback); + } + catch (Exception ex) when (ex is ArgumentException or NotSupportedException) + { + encoding = System.Text.Encoding.GetEncoding(1252, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback); + } + Encodings[codePage] = encoding; + } + } + try + { + var bytes = encoding.GetBytes([c]); + // Only a character the code page has itself, not a look-alike, and at most a double-byte one. + return bytes.Length is 1 or 2 && encoding.GetString(bytes) == c.ToString() ? bytes : [(byte)'?']; + } + catch (Exception ex) when (ex is EncoderFallbackException or DecoderFallbackException or ArgumentException) + { + return [(byte)'?']; + } + } + + // ───────────────────────── Fonts and colours ───────────────────────── + + /// A font table entry: its name, \fcharset and the other name of the font. + private sealed record FontEntry(string Name, int Charset, bool EastAsian); + + private readonly List _fonts = []; + private readonly Dictionary<(string Name, int Charset), int> _latinFonts = []; + private readonly Dictionary _eastAsianFonts = new(StringComparer.Ordinal); + private readonly List _colors = []; + private readonly Dictionary _colorIndex = new(StringComparer.OrdinalIgnoreCase); + + /// The index of a font for text of , added on first use. + private int FontIndex(string name, int charset) + { + name = CleanFontName(name); + if (_latinFonts.TryGetValue((name, charset), out var index)) + return index; + CheckEntries(_fonts.Count, "fonts"); + _fonts.Add(new FontEntry(name, charset, EastAsian: false)); + return _latinFonts[(name, charset)] = _fonts.Count - 1; + } + + /// + /// The index of an East Asian font, added on first use with (the script of the text + /// that first used it): RtfReader and Word take a font of an East Asian \fcharset as the East Asian font. + /// + private int EastAsianFontIndex(string name, int charset) + { + name = CleanFontName(name); + if (_eastAsianFonts.TryGetValue(name, out var index)) + return index; + CheckEntries(_fonts.Count, "fonts"); + _fonts.Add(new FontEntry(name, charset, EastAsian: true)); + return _eastAsianFonts[name] = _fonts.Count - 1; + } + + /// A font name without the characters that end or open a font table entry. + private static string CleanFontName(string name) + { + var clean = name.Replace(";", "", StringComparison.Ordinal).Trim(); + return clean.Length == 0 ? DefaultLatinFont : clean; + } + + /// The colour table index of "#RRGGBB" (0, the automatic colour, for none). + private int ColorIndex(string? color) + { + if (Odt.Odf.Color(color) is not { } hex) + return 0; + if (_colorIndex.TryGetValue(hex, out var index)) + return index; + CheckEntries(_colors.Count, "colours"); + _colors.Add(hex); + return _colorIndex[hex] = _colors.Count; + } + + private void CheckEntries(int count, string what) + { + if (count >= _limits.MaxObjects) + throw DocumentLimits.Exceeded($"The RTF file would define more than {DocumentLimits.Count(_limits.MaxObjects)} {what}."); + } + + /// Windows code page of an \fcharset (the inverse of RtfReader's table), 1252 for ANSI. + internal static int CodePage(int charset) => charset switch + { + 128 => 932, + 129 => 949, + 134 => 936, + 136 => 950, + 161 => 1253, + 162 => 1254, + 163 => 1258, + 177 => 1255, + 178 => 1256, + 186 => 1257, + 204 => 1251, + 222 => 874, + 238 => 1250, + _ => 1252, + }; + + /// + /// The other name of a font, which Word and RtfReader use when the font is not installed: the document's own + /// (DOCX w:altName), else the font's name in the other language (맑은 고딕 / Malgun Gothic) or a font of the same + /// widths (Calibri / Carlito). + /// + private string? Alternate(string name) + { + if (_document.FontAlternates.TryGetValue(name, out var known) && !string.IsNullOrWhiteSpace(known)) + return known; + return FontNames.Alternatives(name).FirstOrDefault(other => !other.Equals(name, StringComparison.OrdinalIgnoreCase)); + } + + // ───────────────────────── Header ───────────────────────── + + /// Everything before the body: the tables the body filled, the document's information and settings. + private void Header() + { + var o = _out; + o.Control("{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1"); + o.Line(); + + o.Open(); + o.Control("\\fonttbl"); + for (var i = 0; i < _fonts.Count; i++) + { + var font = _fonts[i]; + o.Line(); + o.Open(); + var family = FontNames.IsMonospace(font.Name) ? "\\fmodern" : "\\fnil"; + o.Control($"\\f{Number(i)}{family}\\fcharset{Number(font.Charset)}\\fprq{(family == "\\fmodern" ? 1 : 2)}"); + o.Text(font.Name, CodePage(font.Charset)); + if (Alternate(font.Name) is { } alternate) + { + o.Open(); + o.Control("\\*\\falt"); + o.Text(CleanFontName(alternate), CodePage(font.Charset)); + o.Close(); + } + o.Raw(";"); + o.Close(); + } + o.Close(); + o.Line(); + + o.Open(); + o.Control("\\colortbl"); + o.Raw(";"); + foreach (var color in _colors) + { + var value = int.Parse(color.AsSpan(1), NumberStyles.HexNumber, CultureInfo.InvariantCulture); + o.Control($"\\red{Number(value >> 16 & 0xFF)}\\green{Number(value >> 8 & 0xFF)}\\blue{Number(value & 0xFF)}"); + o.Raw(";"); + } + o.Close(); + o.Line(); + + StyleSheet(); + ListTables(); + Information(); + + // Document settings: the first section's page, the default tab interval, notes. + var first = _document.Sections.FirstOrDefault(); + var page = first?.Page ?? DefaultPage; + o.Control($"\\paperw{Number(Twips(page.Width))}\\paperh{Number(Twips(page.Height))}" + + $"\\margl{Number(Twips(page.Left))}\\margr{Number(Twips(page.Right))}" + + $"\\margt{Number(Twips(page.Top + page.Header))}\\margb{Number(Twips(page.Bottom + page.Footer))}" + + $"\\gutter{Number(Twips(page.Gutter))}{(page.Landscape ? "\\landscape" : "")}"); + if (_facingPages) + o.Control("\\facingp"); + var tab = _document.DefaultTabStop is > 0 and var interval ? Twips(interval) : DefaultTabTwips; + o.Control($"\\deftab{Number(Math.Max(1, tab))}\\widowctrl"); + o.Control($"\\ftnbj{NoteNumbering("ftn", (first?.Footnotes ?? HNoteShape.Footnote).Format)}\\aenddoc{NoteNumbering("aftn", (first?.Endnotes ?? HNoteShape.Endnote).Format)}\\fet2"); + o.Line(); + } + + /// \ftnnar, \aftnnrlc ...: how notes are numbered. + private static string NoteNumbering(string prefix, HNoteFormat format) => "\\" + prefix + format.Type switch + { + "ROMAN_SMALL" => "nrlc", + "ROMAN_CAPITAL" => "nruc", + "LATIN_SMALL" => "nalc", + "LATIN_CAPITAL" => "nauc", + "HANGUL_SYLLABLE" => "nganada", + "HANGUL_JAMO" => "nchosung", + "CIRCLED_DIGIT" => "ncnum", + _ => "nar", + }; + + /// + /// Normal and Word's "heading 1".."heading 9" (outline levels, which RtfReader and Word read as headings), with + /// the properties paragraphs of those styles repeat (RTF paragraphs carry their formatting themselves). + /// + private void StyleSheet() + { + var o = _out; + o.Open(); + o.Control("\\stylesheet"); + o.Line(); + o.Open(); + o.Control($"\\s0\\ql\\widctlpar\\sl295\\slmult1\\f0\\fs{Number(DefaultHalfPoints)}\\snext0"); + o.Raw("Normal;"); + o.Close(); + for (var level = 1; level <= 9; level++) + { + o.Line(); + o.Open(); + o.Control($"\\s{Number(level)}\\ql\\keepn\\keep\\widctlpar\\sb{Number(level <= 2 ? 240 : 160)}\\sa80\\sl295\\slmult1\\outlinelevel{Number(level - 1)}" + + $"\\b\\f0\\fs{Number(HeadingSizes[level - 1])}\\sbasedon0\\snext0"); + o.Raw($"heading {Number(level)};"); + o.Close(); + } + o.Close(); + o.Line(); + } + + private void Information() + { + var o = _out; + if (string.IsNullOrWhiteSpace(_document.Title) && _document.Authors.Count == 0) + return; + o.Open(); + o.Control("\\info"); + if (!string.IsNullOrWhiteSpace(_document.Title)) + { + o.Open(); + o.Control("\\title"); + o.Text(_document.Title.Trim(), 1252); + o.Close(); + } + if (_document.Authors.Count > 0) + { + o.Open(); + o.Control("\\author"); + o.Text(string.Join("; ", _document.Authors), 1252); + o.Close(); + } + o.Close(); + o.Line(); + } + + // ───────────────────────── Lists ───────────────────────── + + /// Indent of list level (twips): 400 per level with a 400 hanging number, as the DOCX writer's. + private static int ListIndent(int level) => (level + 1) * 400; + + private readonly Dictionary _numberings = new(ReferenceEqualityComparer.Instance); + + /// The \ls number of a list definition, added on first use. + private int ListNumber(HNumbering numbering) + { + if (_numberings.TryGetValue(numbering, out var number)) + return number; + CheckEntries(_numberings.Count, "lists"); + return _numberings[numbering] = _numberings.Count + 1; + } + + /// \listtable with nine levels per list, and \listoverridetable with one \ls per list. + private void ListTables() + { + if (_numberings.Count == 0) + return; + var o = _out; + o.Open(); + o.Control("\\*\\listtable"); + foreach (var (numbering, number) in _numberings) + { + o.Line(); + o.Open(); + o.Control($"\\list\\listtemplateid{Number(ListId(number))}\\listhybrid"); + for (var level = 0; level < 9; level++) + { + var definition = level < numbering.Levels.Count ? numbering.Levels[level] : new HNumberingLevel("DIGIT", $"^{level + 1}.", 1, Bullet: false); + o.Open(); + var format = definition.Bullet ? 23 : LevelFormat(definition.Format); + o.Control($"\\listlevel\\levelnfc{Number(format)}\\levelnfcn{Number(format)}\\leveljc0\\leveljcn0\\levelfollow0\\levelstartat{Number(Math.Max(0, definition.Start))}"); + var (text, numbers) = LevelText(definition); + o.Open(); + o.Control("\\leveltext"); + // The length first, then the text with \'00..\'08 for the numbers of those levels. + o.Byte(Math.Min(text.Length, 255)); + foreach (var c in text.Take(255)) + { + if (c < 9) + o.Byte(c); + else + o.Text(c.ToString(), 1252); + } + o.Raw(";"); + o.Close(); + o.Open(); + o.Control("\\levelnumbers"); + foreach (var position in numbers) + o.Byte(position); + o.Raw(";"); + o.Close(); + o.Control($"\\fi-400\\li{Number(ListIndent(level))}\\lin{Number(ListIndent(level))}"); + o.Close(); + } + o.Open(); + o.Control("\\listname"); + o.Raw(";"); + o.Close(); + o.Control($"\\listid{Number(ListId(number))}"); + o.Close(); + } + o.Close(); + o.Line(); + + o.Open(); + o.Control("\\*\\listoverridetable"); + foreach (var number in _numberings.Values) + { + o.Line(); + o.Open(); + o.Control($"\\listoverride\\listid{Number(ListId(number))}\\listoverridecount0\\ls{Number(number)}"); + o.Close(); + } + o.Close(); + o.Line(); + } + + private static int ListId(int number) => 1000 + number; + + /// OWPML number format → \levelnfc (Word's number formats, the inverse of RtfReader's). + internal static int LevelFormat(string owpml) => owpml switch + { + "ROMAN_CAPITAL" => 1, + "ROMAN_SMALL" => 2, + "LATIN_CAPITAL" => 3, + "LATIN_SMALL" => 4, + "IDEOGRAPH" => 10, + "CIRCLED_DIGIT" => 18, + "HANGUL_SYLLABLE" => 24, + "HANGUL_JAMO" => 25, + "HANGUL_PHONETIC" => 40, + _ => 0, + }; + + /// + /// A level's text with (char)0..(char)8 for the numbers of levels 1..9 ("^1.^2." → "\0.\1."), and the 1-based + /// positions of those numbers in it (\levelnumbers). A bullet is its character. + /// + internal static (string Text, List Numbers) LevelText(HNumberingLevel level) + { + if (level.Bullet) + return (level.Text.Length > 0 ? StringInfo.GetNextTextElement(level.Text) : "•", []); + var text = new StringBuilder(); + var numbers = new List(); + for (var i = 0; i < level.Text.Length; i++) + { + if (level.Text[i] == '^' && i + 1 < level.Text.Length && level.Text[i + 1] is >= '1' and <= '9') + { + text.Append((char)(level.Text[i + 1] - '1')); + numbers.Add(text.Length); + i++; + continue; + } + text.Append(level.Text[i]); + } + return (text.ToString(), numbers); + } + + // ───────────────────────── Sections ───────────────────────── + + /// Some section has a header or footer for even pages: the document has facing pages (\facingp). + private bool _facingPages; + + private void Body() + { + var sections = _document.Sections.Count > 0 ? _document.Sections : [new HSection()]; + _facingPages = sections.Any(s => s.Headers.Concat(s.Footers).Any(h => h.Pages == HPageType.Even)); + for (var s = 0; s < sections.Count; s++) + { + var section = sections[s]; + var page = section.Page ?? DefaultPage; + var parts = SplitAtColumnChanges(section); + for (var p = 0; p < parts.Count; p++) + { + _token.ThrowIfCancellationRequested(); + var (columns, blocks) = parts[p]; + if (s > 0 || p > 0) + { + _out.Control("\\sect"); + _out.Line(); + } + SectionProperties(section, page, columns, continuous: p > 0); + _width = ColumnWidth(page.TextWidth, columns); + Blocks(blocks, Story.Body, 0, BlockEnd.Story); + } + } + } + + /// Splits a section where its columns change: each piece becomes a section of its own, without a page break. + private static List<(HColumns Columns, List Blocks)> SplitAtColumnChanges(HSection section) + { + var parts = new List<(HColumns, List)>(); + var columns = section.Columns; + var current = new List(); + foreach (var block in section.Blocks) + { + if (block is HParagraph { ColumnsChange: { } change }) + { + if (current.Count > 0) + { + parts.Add((columns, current)); + current = []; + } + columns = change; + } + current.Add(block); + } + parts.Add((columns, current)); + return parts; + } + + private static int ColumnWidth(int textWidth, HColumns columns) => + columns.Count <= 1 ? textWidth : Math.Max(2000, (textWidth - columns.Gap * (columns.Count - 1)) / columns.Count); + + /// + /// \sectd and the section's page, columns, numbering, headers and footers. The model's Top / Bottom run from the + /// paper edge to the header / footer and Header / Footer are their heights (as in 한글); RTF's margins reach the + /// body and \headery / \footery are the distances. + /// + private void SectionProperties(HSection section, HPage page, HColumns columns, bool continuous) + { + var o = _out; + var sb = new StringBuilder("\\sectd"); + if (continuous) + sb.Append("\\sbknone"); + sb.Append($"\\pgwsxn{Number(Twips(page.Width))}\\pghsxn{Number(Twips(page.Height))}") + .Append($"\\marglsxn{Number(Twips(page.Left))}\\margrsxn{Number(Twips(page.Right))}") + .Append($"\\margtsxn{Number(Twips(page.Top + page.Header))}\\margbsxn{Number(Twips(page.Bottom + page.Footer))}") + .Append($"\\guttersxn{Number(Twips(page.Gutter))}\\headery{Number(Twips(page.Top))}\\footery{Number(Twips(page.Bottom))}"); + if (page.Landscape) + sb.Append("\\lndscpsxn"); + if (columns.Count > 1) + { + sb.Append($"\\cols{Number(Math.Min(columns.Count, 255))}\\colsx{Number(Twips(columns.Gap))}"); + if (columns.Separator) + sb.Append("\\linebetcol"); + if (columns.Sizes is { } sizes && sizes.Count == columns.Count) + { + for (var i = 0; i < sizes.Count; i++) + { + sb.Append($"\\colno{Number(i + 1)}\\colw{Number(Twips(sizes[i].Width))}"); + if (i + 1 < sizes.Count) + sb.Append($"\\colsr{Number(Twips(sizes[i].Gap))}"); + } + } + } + if (columns.RightToLeft) + sb.Append("\\rtlsect"); + if (!continuous && section.StartPageNumber is { } start) + sb.Append($"\\pgnstarts{Number(Math.Max(0, start))}\\pgnrestart"); + var titlePage = !continuous && (section.HideFirstHeader || section.HideFirstFooter || section.FirstPageHeader is not null || section.FirstPageFooter is not null); + if (titlePage) + sb.Append("\\titlepg"); + o.Control(sb.ToString()); + o.Line(); + if (continuous) + return; + + _width = page.TextWidth; + HeadersFooters("header", section.Headers, section.HideFirstHeader ? [] : section.FirstPageHeader, titlePage); + HeadersFooters("footer", section.Footers, section.HideFirstFooter ? [] : section.FirstPageFooter, titlePage); + } + + /// + /// \header (every page), \headerr and \headerl (odd and even pages, with facing pages) and \headerf (the first + /// page, empty when the section hides its header there). + /// + private void HeadersFooters(string kind, List items, List? firstPage, bool titlePage) + { + var both = items.LastOrDefault(i => i.Pages == HPageType.Both)?.Blocks; + var odd = items.LastOrDefault(i => i.Pages == HPageType.Odd)?.Blocks; + var even = items.LastOrDefault(i => i.Pages == HPageType.Even)?.Blocks; + if (_facingPages) + { + Part(kind + "r", odd ?? both); + Part(kind + "l", even ?? both); + } + else + { + Part(kind, odd ?? both); + } + if (titlePage) + Part(kind + "f", firstPage ?? odd ?? both ?? []); + } + + private void Part(string destination, List? blocks) + { + if (blocks is null) + return; + _out.Open(); + _out.Control("\\" + destination); + Blocks(blocks, Story.Header, 0, BlockEnd.Story); + _out.Close(); + _out.Line(); + } + + // ───────────────────────── Helpers ───────────────────────── + + /// HWPUNIT → twips (1/1440 inch). + private static int Twips(int hwpUnit) => (int)Math.Round(hwpUnit / 5.0); + + private static string Number(int value) => value.ToString(CultureInfo.InvariantCulture); +} diff --git a/src/Filee.Engines/Office/RtfWriterConverter.cs b/src/Filee.Engines/Office/RtfWriterConverter.cs new file mode 100644 index 000000000..6cc297586 --- /dev/null +++ b/src/Filee.Engines/Office/RtfWriterConverter.cs @@ -0,0 +1,46 @@ +// Anything → RTF without LibreOffice or Pandoc: a built-in reader (DocumentReaders) turns the source into the document +// model and RtfWriter writes Rich Text Format. Its edges cost less than Pandoc's and LibreOffice's, so the built-in +// writer is used wherever it can read the source. + +using Filee.Core.Conversion; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Infrastructure; + +namespace Filee.Engines.Office; + +/// Writes RTF files with the built-in Rich Text Format writer. +public sealed class RtfWriterConverter : IConverter, ICapabilityCheck +{ + /// Below Pandoc's and LibreOffice's direct edges (10 and more), so the built-in writer wins. + private const int Cost = 8; + + public string Id => "rtf-writer"; + public string DisplayName => "RTF writer (built-in)"; + public int MaxParallelism => 0; + + /// RTF → RTF is no conversion. + public IReadOnlyList Edges { get; } = + DocumentReaders.BuiltInFormats.Where(f => f != "rtf") + .Select(f => new ConversionEdge(f, "rtf", f == "pdf" ? Cost + DocumentReaders.PdfPenalty : Cost, Checked: DocumentReaders.Checked(f))).ToList(); + + /// 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); + + public EngineStatus GetStatus() => + EngineStatus.Available(string.Join(", ", Edges.Select(e => e.From.ToUpperInvariant())) + " → RTF", EngineVersions.BuiltIn); + + public async Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) + { + progress?.Report(0.1); + var document = await DocumentReaders.ReadAsync(step.InputPath, step.From, step.WorkDirectory, cancellationToken, + ocrLayer: step.Preset.Pdf.Ocr); + progress?.Report(0.6); + + var output = step.Output.Allocate("rtf"); + if (output is null) + return []; + await Task.Run(() => RtfWriter.Write(document, output, cancellationToken), cancellationToken); + progress?.Report(1); + return [output]; + } +} diff --git a/src/Filee.Engines/Typesetting/PageComposer.cs b/src/Filee.Engines/Typesetting/PageComposer.cs index 6cd8bf80e..a5f6b2ad5 100644 --- a/src/Filee.Engines/Typesetting/PageComposer.cs +++ b/src/Filee.Engines/Typesetting/PageComposer.cs @@ -89,6 +89,7 @@ public ComposedDocument Compose(HDocument document, CancellationToken cancellati { _layouter.FontAlternates = document.FontAlternates; _layouter.DefaultTabStop = document.DefaultTabStop; + _layouter.DefaultTabFromIndent = document.DefaultTabStopFromIndent; // The body's total pages fields need the count first: a first pass finds it. int? total = null; if (document.Sections.Any(s => Fields(s.Blocks).Any(f => f.Kind == HFieldKind.TotalPages))) diff --git a/src/Filee.Engines/Typesetting/ParagraphLayout.cs b/src/Filee.Engines/Typesetting/ParagraphLayout.cs index 6b6e2f69c..e2b1678f5 100644 --- a/src/Filee.Engines/Typesetting/ParagraphLayout.cs +++ b/src/Filee.Engines/Typesetting/ParagraphLayout.cs @@ -122,6 +122,9 @@ public IReadOnlyDictionary? FontAlternates /// public int? DefaultTabStop { get; set; } + /// counts from the left indent (HDocument.DefaultTabStopFromIndent: LibreOffice). + public bool DefaultTabFromIndent { get; set; } + /// Lines laid out between checks of . private const int GuardLines = 256; private readonly string? _notAtLineStart; @@ -314,7 +317,8 @@ private Prepared Prepare(HParagraph paragraph, float width, string? marker, Func SpreadEastAsian: breaking?.SpreadEastAsian == true, Language: language, HangPunctuation: breaking?.OverflowPunctuation == true, - WordTabInterval: DefaultTabStop is > 0 and var interval ? interval / 100f : 0); + WordTabInterval: DefaultTabStop is > 0 and var interval ? interval / 100f : 0, + TabIntervalFromIndent: DefaultTabFromIndent); // 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) @@ -478,7 +482,7 @@ private static HLineSegment WholeLine(HLineSegment first, HParaFormat format, fl /// Per UTF-16 index, the auto-spacing after the cluster starting there; null = none. private sealed record Settings(HAlign Align, bool RightToLeft, float Left, float Right, float FirstLine, IReadOnlyList Tabs, HLineWrap Wrap, float Condense, HLineHeight LineHeight, bool FontLineHeight, bool SpreadEastAsian = false, string Language = "", - float[]? AutoSpace = null, bool HangPunctuation = false, float WordTabInterval = 0); + float[]? AutoSpace = null, bool HangPunctuation = false, float WordTabInterval = 0, bool TabIntervalFromIndent = false); // ───────────────────────── Items ───────────────────────── @@ -790,8 +794,8 @@ private static TabStop NextStop(Settings settings, bool firstLine, float x) if (settings.WordTabInterval > 0) { // Word's default stops are every interval from the margin, not from the indent: a list number wider than its - // hanging indent sends the text to the next of them. - var start = settings.Left + indent; + // hanging indent sends the text to the next of them. LibreOffice's own documents count from the indent. + var start = (settings.TabIntervalFromIndent ? 0 : settings.Left) + indent; var stop = (MathF.Floor((x + start) / settings.WordTabInterval + Epsilon) + 1) * settings.WordTabInterval - start; return new TabStop(stop, HTabKind.Left, "NONE"); } diff --git a/tests/Filee.Engines.Tests/DefaultTabStopTests.cs b/tests/Filee.Engines.Tests/DefaultTabStopTests.cs new file mode 100644 index 000000000..556b3b5ac --- /dev/null +++ b/tests/Filee.Engines.Tests/DefaultTabStopTests.cs @@ -0,0 +1,91 @@ +// Default tab stops and fonts' other names as the readers take them from their formats: RTF's \deftab (Word's half +// inch when the file gives none) and \falt, explicit \tx stops, and ODF's style:tab-stop-distance (LibreOffice's +// 1.25 cm when the default paragraph style gives none), counted from the indent unless settings.xml says otherwise. + +using System.IO.Compression; +using System.Text; +using Filee.Engines.Documents; +using Filee.Engines.Hwp.Hwpx; + +namespace Filee.Engines.Tests; + +public class DefaultTabStopTests(EngineFixture fx) : IClassFixture +{ + private HDocument Rtf(string rtf) => RtfReader.Read(Encoding.Latin1.GetBytes(rtf), fx.NewFolder(), DocumentLimits.Default); + + [Fact] + public void Rtf_default_tabs_are_deftab_or_half_an_inch() + { + Assert.Equal(3600, Rtf("{\\rtf1\\ansi Text}").DefaultTabStop); + Assert.Equal(5670, Rtf("{\\rtf1\\ansi\\deftab1134 Text}").DefaultTabStop); + Assert.False(Rtf("{\\rtf1\\ansi Text}").DefaultTabStopFromIndent); + } + + [Fact] + public void Rtf_tab_stops_are_read_from_the_indent() + { + var document = Rtf("{\\rtf1\\ansi\\pard\\li720\\tx1440\\tqr\\tldot\\tx4320\\tqc\\tx2880 a\\tab b\\par}"); + + var tabs = Assert.IsType(Assert.Single(document.Sections[0].Blocks)).Format.Tabs!; + Assert.Equal([new HTabStop(3600, HTabKind.Left, "NONE"), new HTabStop(10800, HTabKind.Center, "NONE"), new HTabStop(18000, HTabKind.Right, "DOT")], tabs); + } + + [Fact] + public void Rtf_fonts_keep_their_other_names() + { + var document = Rtf("{\\rtf1\\ansi{\\fonttbl{\\f0\\fnil\\fcharset0 Corporate Sans{\\*\\falt Arial};}{\\f1\\fnil\\fcharset129 \\'b8\\'bc\\'c0\\'ba \\'b0\\'ed\\'b5\\'f1{\\*\\falt Malgun Gothic};}" + + "{\\f2\\fnil Same{\\*\\falt Same};}}\\f0 Text}"); + + Assert.Equal("Arial", document.FontAlternates["Corporate Sans"]); + Assert.Equal("Malgun Gothic", document.FontAlternates["맑은 고딕"]); + Assert.False(document.FontAlternates.ContainsKey("Same")); + Assert.Equal("Corporate Sans", Assert.IsType(Assert.IsType(document.Sections[0].Blocks[0]).Inlines[0]).Format.Font); + } + + // ───────────────────────── ODT ───────────────────────── + + private string Odt(string? defaultParagraph, string? settings) + { + var path = Path.Combine(fx.NewFolder(), "tabs.odt"); + using (var zip = ZipFile.Open(path, ZipArchiveMode.Create)) + { + void Entry(string name, string text) + { + using var writer = new StreamWriter(zip.CreateEntry(name).Open(), new UTF8Encoding(false)); + writer.Write(text); + } + Entry("mimetype", Hwp.Hwpx.Odt.Odf.MimeType); + Entry("content.xml", $"ab"); + Entry("styles.xml", $"" + + (defaultParagraph is null ? "" : $"") + + ""); + if (settings is not null) + { + Entry("settings.xml", "" + + "" + + $"{settings}" + + ""); + } + } + return path; + } + + [Fact] + public void Odt_default_tabs_come_from_the_default_paragraph_style() + { + var document = Hwp.Hwpx.Odt.OdtReader.Read(Odt("2cm", null), fx.NewFolder()); + + Assert.Equal((int)Math.Round(2 * 7200 / 2.54), document.DefaultTabStop); + Assert.True(document.DefaultTabStopFromIndent); + } + + [Fact] + public void Odt_without_a_distance_has_libreoffice_s_and_settings_can_count_from_the_margin() + { + var document = Hwp.Hwpx.Odt.OdtReader.Read(Odt(null, "false"), fx.NewFolder()); + + Assert.Equal((int)Math.Round(1.25 * 7200 / 2.54), document.DefaultTabStop); + Assert.False(document.DefaultTabStopFromIndent); + Assert.True(Hwp.Hwpx.Odt.OdtReader.Read(Odt(null, "true"), fx.NewFolder()).DefaultTabStopFromIndent); + } +} diff --git a/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs b/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs index 1075600b0..c52e7802f 100644 --- a/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs +++ b/tests/Filee.Engines.Tests/LibreOfficeRouteTests.cs @@ -53,13 +53,10 @@ public void Hwp_routes_never_need_libreoffice(string from) foreach (var to in new[] { "pdf", "png", "docx", "odt", "hwp", "hwpx", "txt", "md", "html", "epub" }.Where(to => to != from)) Assert.DoesNotContain(planner.Plan(from, to)!.Steps, s => s.Converter.Id == "libreoffice"); - // RTF has no built-in writer: Filee writes Markdown that Pandoc turns into RTF, or DOCX / ODT that LibreOffice does. - Assert.Equal(["hwp-text", "pandoc"], planner.Plan(from, "rtf")!.Steps.Select(s => s.Converter.Id)); + // RTF comes from the built-in writer, with or without Pandoc. + Assert.Equal("rtf-writer", Assert.Single(planner.Plan(from, "rtf")!.Steps).Converter.Id); var withoutPandoc = Planner(slides: true, documents: true, sheets: true, without: "pandoc"); - var rtf = withoutPandoc.Plan(from, "rtf")!.Steps.Select(s => s.Converter.Id).ToList(); - Assert.Equal(2, rtf.Count); - Assert.Contains(rtf[0], new[] { "docx-writer", "odt-writer" }); - Assert.Equal("libreoffice", rtf[1]); + Assert.Equal("rtf-writer", Assert.Single(withoutPandoc.Plan(from, "rtf")!.Steps).Converter.Id); } [Theory] diff --git a/tests/Filee.Engines.Tests/RtfWriterTests.cs b/tests/Filee.Engines.Tests/RtfWriterTests.cs new file mode 100644 index 000000000..dbf544b7a --- /dev/null +++ b/tests/Filee.Engines.Tests/RtfWriterTests.cs @@ -0,0 +1,466 @@ +// The built-in RTF writer (Hwp/Hwpx/RtfWriter): a model built in code with what the RTF reader understands (page +// setup, headers and footers, headings, character and paragraph formatting, tab stops, lists, links, bookmarks, +// fields, notes, tables with merged and shaded cells, pictures) is written as RTF and read back by Filee's own RTF +// reader into the same model; the file is valid RTF (one balanced group starting with \rtf1, ASCII only); text beyond +// ASCII is \uN with a code-page fallback; fonts carry their other names (\falt); anything → RTF takes the writer; and +// models that pass the document limits are refused without leaving a file. + +using System.Text; +using Filee.Core.Conversion; +using Filee.Engines.Documents; +using Filee.Engines.Hwp.Hwpx; +using ImageMagick; + +namespace Filee.Engines.Tests; + +public class RtfWriterTests(EngineFixture fx) : IClassFixture +{ + static RtfWriterTests() => Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + private static readonly string[] OptionalEngines = ["pandoc", "libreoffice", "calibre", "ghostscript"]; + + /// Writes , checks it is valid RTF and reads it back with Filee's RTF reader. + private (HDocument Document, string Rtf) RoundTrip(HDocument document, DocumentLimits? limits = null) + { + var folder = fx.NewFolder(); + var path = Path.Combine(folder, "out.rtf"); + RtfWriter.Write(document, path, limits ?? DocumentLimits.Default, TestContext.Current.CancellationToken); + var bytes = File.ReadAllBytes(path); + AssertValidRtf(bytes); + Assert.Single(Directory.GetFiles(folder)); // no temporary file left beside it + return (RtfReader.Read(bytes, Path.Combine(folder, "media"), DocumentLimits.Default), Encoding.ASCII.GetString(bytes)); + } + + /// + /// One group from the first byte to the last, opened by \rtf1, with every brace balanced (escaped ones and \bin + /// data aside) and nothing but ASCII in the file. + /// + internal static void AssertValidRtf(byte[] rtf) + { + Assert.True(rtf.AsSpan().StartsWith("{\\rtf1"u8), "The file doesn't start with {\\rtf1."); + Assert.All(rtf, b => Assert.True(b < 0x80, $"Byte 0x{b:x2} is not ASCII.")); + var depth = 0; + var end = -1; + for (var i = 0; i < rtf.Length; i++) + { + switch (rtf[i]) + { + case (byte)'\\' when i + 1 < rtf.Length: + if (rtf[i + 1] is (byte)'{' or (byte)'}' or (byte)'\\') + { + i++; + } + else if (rtf.AsSpan(i + 1).StartsWith("bin"u8)) + { + var j = i + 4; + var length = 0; + while (j < rtf.Length && char.IsAsciiDigit((char)rtf[j])) + length = length * 10 + (rtf[j++] - '0'); + if (j < rtf.Length && rtf[j] == ' ') + j++; + i = j + length - 1; + } + break; + case (byte)'{': + Assert.True(end < 0, "Something follows the document's group."); + depth++; + break; + case (byte)'}': + depth--; + Assert.True(depth >= 0, $"A closing brace without a group at byte {i}."); + if (depth == 0) + end = i; + break; + default: + Assert.True(end < 0 || rtf[i] is (byte)'\r' or (byte)'\n', "Something follows the document's group."); + break; + } + } + Assert.Equal(0, depth); + Assert.True(end > 0); + } + + private string Picture(MagickColor color, uint width = 40, uint height = 30, string extension = "png") + { + var path = Path.Combine(fx.NewFolder(), $"picture.{extension}"); + File.WriteAllBytes(path, extension == "png" ? EbookBuilders.Png(width, height, color) : Encode(color, width, height, extension)); + return path; + } + + private static byte[] Encode(MagickColor color, uint width, uint height, string format) + { + using var image = new MagickImage(color, width, height); + return image.ToByteArray(format == "gif" ? MagickFormat.Gif : MagickFormat.Bmp); + } + + private static HParagraph Para(params HInline[] inlines) + { + var paragraph = new HParagraph(); + paragraph.Inlines.AddRange(inlines); + return paragraph; + } + + private static HParagraph Para(string text, HCharFormat format = default) => Para(new HText(text, format)); + + private static HCell Cell(string text, Action? setup = null) + { + var cell = new HCell(); + cell.Blocks.Add(Para(text)); + setup?.Invoke(cell); + return cell; + } + + // ───────────────────────── Round trip ───────────────────────── + + /// A document with what the RTF reader reads back; lengths are multiples of a twip (5 HWPUNIT). + private HDocument Sample() + { + var document = new HDocument { Title = "RTF sample" }; + var section = new HSection + { + Page = new HPage(59530, 84190, 7200, 6000, 3600, 3000, 2400, 1800, 0), + StartPageNumber = 3, + }; + document.Sections.Add(section); + var header = new HHeaderFooter(HPageType.Both); + header.Blocks.Add(Para("Header text")); + section.Headers.Add(header); + var footer = new HHeaderFooter(HPageType.Both); + footer.Blocks.Add(Para(new HText("Page ", default), new HField(HFieldKind.PageNumber, default), new HText(" of ", default), + new HField(HFieldKind.TotalPages, default))); + section.Footers.Add(footer); + + var blocks = section.Blocks; + blocks.Add(new HParagraph { HeadingLevel = 1, Inlines = { new HText("Chapter one", default) } }); + blocks.Add(Para( + new HText("Plain, ", default), + new HText("bold", new HCharFormat(Bold: true)), + new HText(" italic", new HCharFormat(Italic: true, Size: 1400)), + new HText(" under", new HCharFormat(Underline: true, Color: "#C00000")), + new HText(" struck", new HCharFormat(Strike: true, Shade: "#FFFF00")), + new HText("up", new HCharFormat(Superscript: true)), + new HText("down", new HCharFormat(Subscript: true)), + new HLineBreak(default), + new HText("after a break", default), + new HTab(default), + new HText("after a tab {braces} and \\backslash", default))); + blocks.Add(new HParagraph + { + Format = new HParaFormat(Align: HAlign.Center, Left: 1000, Right: 500, FirstLine: 400, Before: 600, After: 300, + LineSpacing: new HLineSpacing(HLineSpacingKind.Percent, 160), KeepWithNext: true), + Inlines = { new HText("Centred and indented", default) }, + }); + blocks.Add(new HParagraph + { + Format = new HParaFormat(Align: HAlign.Right, LineSpacing: new HLineSpacing(HLineSpacingKind.Fixed, 3000), KeepLines: true), + PageBreakBefore = true, + Inlines = { new HText("Right, fixed lines, new page", default) }, + }); + blocks.Add(new HParagraph + { + Format = new HParaFormat(Align: HAlign.Justify, Left: 2000, LineSpacing: new HLineSpacing(HLineSpacingKind.AtLeast, 2500), + Tabs: [new HTabStop(5000, HTabKind.Center, "NONE"), new HTabStop(10000, HTabKind.Right, "DOT"), new HTabStop(15000, HTabKind.Decimal, "NONE")]), + Inlines = { new HText("Tabs", default), new HTab(default), new HText("centre", default), new HTab(default), new HText("right", default) }, + }); + + var numbers = new HNumbering(); + numbers.Levels.Add(new HNumberingLevel("DIGIT", "^1.", 1, Bullet: false)); + numbers.Levels.Add(new HNumberingLevel("LATIN_SMALL", "(^2)", 1, Bullet: false)); + numbers.Levels.Add(new HNumberingLevel("HANGUL_SYLLABLE", "^1.^2.^3", 3, Bullet: false)); + var bullets = new HNumbering(); + bullets.Levels.Add(new HNumberingLevel("DIGIT", "•", 1, Bullet: true)); + blocks.Add(new HParagraph { List = new HListRef(numbers, 0, true), Inlines = { new HText("First item", default) } }); + blocks.Add(new HParagraph { List = new HListRef(numbers, 1, true), Inlines = { new HText("Nested item", default) } }); + blocks.Add(new HParagraph { List = new HListRef(numbers, 2, true), Inlines = { new HText("Third level", default) } }); + blocks.Add(new HParagraph { List = new HListRef(bullets, 0, true), Inlines = { new HText("A bullet", default) } }); + + var note = new HNote(false); + note.Blocks.Add(Para("The footnote's text.")); + var endnote = new HNote(true); + endnote.Blocks.Add(Para("An endnote.")); + var link = new HLink("https://example.com/a?b=1&c=2"); + link.Content.Add(new HText("a link", new HCharFormat(Underline: true, Color: "#0563C1"))); + var local = new HLink("#target"); + local.Content.Add(new HText("to the bookmark", default)); + blocks.Add(Para(new HBookmark("target"), new HText("Notes", default), note, new HText(", links ", default), link, + new HText(" and ", default), local, endnote)); + blocks.Add(Para(new HImage(Picture(MagickColors.Teal)) { Width = 4000, Height = 3000 })); + + var table = new HTable { ColumnCount = 3, ColumnWidths = [10000, 15000, 20000] }; + var head = new HRow { Header = true, Height = 1500 }; + head.Cells.Add(Cell("Wide head", c => + { + c.ColSpan = 2; + c.Fill = "#DDEEFF"; + })); + head.Cells.Add(Cell("Tall", c => + { + c.RowSpan = 2; + c.VerticalAlign = HVerticalAlign.Center; + })); + table.Rows.Add(head); + var middle = new HRow(); + middle.Cells.Add(Cell("Block", c => + { + c.RowSpan = 2; + c.ColSpan = 2; + c.Borders = HBorders.Empty; + })); + table.Rows.Add(middle); + var last = new HRow(); + var two = new HCell { VerticalAlign = HVerticalAlign.Bottom }; + two.Blocks.Add(Para("Two")); + two.Blocks.Add(Para("paragraphs")); + last.Cells.Add(two); + table.Rows.Add(last); + blocks.Add(table); + blocks.Add(Para("After the table")); + return document; + } + + [Fact] + public void A_document_read_back_from_its_rtf_is_the_same_model() + { + var document = Sample(); + var (read, rtf) = RoundTrip(document); + + HDocumentAssert.Similar(document, read); + Assert.Contains("\\pngblip", rtf, StringComparison.Ordinal); + Assert.Contains("\\clmgf", rtf, StringComparison.Ordinal); + Assert.Contains("\\clvmrg", rtf, StringComparison.Ordinal); + Assert.Equal(3, read.Sections[0].StartPageNumber); + } + + [Fact] + public void Sections_columns_and_landscape_pages_come_back() + { + var document = new HDocument(); + var portrait = new HSection { Page = new HPage(59530, 84190, 7200, 7200, 3600, 3600, 2400, 2400, 0) }; + portrait.Blocks.Add(Para("Portrait")); + var landscape = new HSection + { + Page = new HPage(84190, 59530, 5000, 5000, 3000, 3000, 2000, 2000, 0), + Columns = new HColumns(2, 1000, false), + }; + landscape.Blocks.Add(Para("Landscape in two columns")); + document.Sections.Add(portrait); + document.Sections.Add(landscape); + + var (read, rtf) = RoundTrip(document); + + HDocumentAssert.Similar(document, read); + Assert.Contains("\\sect", rtf, StringComparison.Ordinal); + Assert.True(read.Sections[1].Page!.Landscape); + } + + [Fact] + public void Text_beyond_ascii_is_unicode_with_a_code_page_fallback() + { + // é is in Windows-1252, 한 in CP949 (two bytes: \uc2), 😀 in no code page (a surrogate pair, "?"). + var text = "Café 한국어 😀 non\u00A0breaking"; + var document = new HDocument(); + var section = new HSection(); + section.Blocks.Add(Para(text)); + document.Sections.Add(section); + + var (read, rtf) = RoundTrip(document); + + var paragraph = Assert.IsType(Assert.Single(read.Sections[0].Blocks)); + Assert.Equal(text, string.Concat(paragraph.Inlines.OfType().Select(t => t.Text))); + Assert.Contains("\\" + "u233\\'e9", rtf, StringComparison.Ordinal); + Assert.Contains("\\uc2", rtf, StringComparison.Ordinal); + Assert.Contains("\\" + "u-10916\\'c7\\'d1", rtf, StringComparison.Ordinal); // 한 (U+D55C, past 32767: negative) = C7 D1 in CP949 + Assert.Contains("\\" + "u-10179?", rtf, StringComparison.Ordinal); // the high surrogate of 😀 + Assert.Contains("\\~", rtf, StringComparison.Ordinal); + } + + [Fact] + public void Fonts_go_by_script_with_their_other_names() + { + var document = new HDocument(); + document.FontAlternates["Fancy Sans"] = "Arial"; + var section = new HSection(); + section.Blocks.Add(Para("Hello 안녕하세요", new HCharFormat(Font: "Arial", EastAsianFont: "바탕"))); + section.Blocks.Add(Para("Display", new HCharFormat(Font: "Fancy Sans"))); + document.Sections.Add(section); + + var (read, rtf) = RoundTrip(document); + + var first = Assert.IsType(read.Sections[0].Blocks[0]).Inlines.OfType().ToList(); + Assert.Equal(2, first.Count); + Assert.Equal(("Hello ", "Arial", null), (first[0].Text, first[0].Format.Font, first[0].Format.EastAsianFont)); + Assert.Equal(("안녕하세요", "바탕", "바탕"), (first[1].Text, first[1].Format.Font, first[1].Format.EastAsianFont)); + Assert.Equal("Fancy Sans", Assert.IsType(Assert.IsType(read.Sections[0].Blocks[1]).Inlines[0]).Format.Font); + // The other names: the document's own, and the English name of a Korean font. + Assert.Equal("Arial", read.FontAlternates["Fancy Sans"]); + Assert.Equal("Batang", read.FontAlternates["바탕"]); + Assert.Equal("Carlito", read.FontAlternates["Calibri"]); + Assert.Contains("\\fcharset129", rtf, StringComparison.Ordinal); + } + + [Fact] + public void Default_tab_stops_and_equations_are_written() + { + var document = new HDocument { DefaultTabStop = 3600 }; + var section = new HSection(); + section.Blocks.Add(Para(new HText("E = ", default), new HEquation(EquationParser.Parse("a over b"), default))); + document.Sections.Add(section); + + var (read, rtf) = RoundTrip(document); + + Assert.Contains("\\deftab720", rtf, StringComparison.Ordinal); + Assert.Equal(3600, read.DefaultTabStop); + var text = string.Concat(Assert.IsType(read.Sections[0].Blocks[0]).Inlines.OfType().Select(t => t.Text)); + Assert.StartsWith("E = ", text, StringComparison.Ordinal); + Assert.Contains('a', text); + Assert.Contains('b', text); + } + + [Fact] + public void Nested_tables_text_boxes_and_other_pictures_make_valid_rtf() + { + var inner = new HTable { ColumnCount = 2 }; + var innerRow = new HRow(); + innerRow.Cells.Add(Cell("inner one")); + innerRow.Cells.Add(Cell("inner two")); + inner.Rows.Add(innerRow); + var outerCell = new HCell(); + outerCell.Blocks.Add(Para("outer")); + outerCell.Blocks.Add(inner); + var outer = new HTable { ColumnCount = 2 }; + var row = new HRow(); + row.Cells.Add(outerCell); + row.Cells.Add(Cell("right")); + outer.Rows.Add(row); + + var box = new HTextBox { Width = 20000, Height = 6000, Fill = "#FFEECC" }; + box.Blocks.Add(Para("Boxed text")); + var shape = new HShape(HShapeKind.Ellipse) { Width = 5000, Height = 3000, Fill = "#336699" }; + var floating = new HAnchor("PAGE", "CENTER", 0, "PARA", "TOP", 500, "SQUARE", false); + var gif = new HImage(Picture(MagickColors.Orange, 16, 8, "gif")) { Anchor = floating }; + var bmp = new HImage(Picture(MagickColors.Purple, 12, 6, "bmp")); + + var document = new HDocument(); + var section = new HSection(); + section.Blocks.Add(Para("Before")); + section.Blocks.Add(outer); + section.Blocks.Add(Para(new HText("A box ", default), box, shape, gif, bmp)); + document.Sections.Add(section); + + var (read, rtf) = RoundTrip(document); + + Assert.Contains("\\nestcell", rtf, StringComparison.Ordinal); + Assert.Contains("\\nesttableprops", rtf, StringComparison.Ordinal); + Assert.Contains("\\shptxt", rtf, StringComparison.Ordinal); + Assert.DoesNotContain("\\dibitmap", rtf, StringComparison.Ordinal); + var table = Assert.IsType(read.Sections[0].Blocks[1]); + // The reader opens up nested tables into their outer cell's paragraphs. + var cellText = string.Join("|", table.Rows[0].Cells[0].Blocks.OfType().Select(p => string.Concat(p.Inlines.OfType().Select(t => t.Text)))); + Assert.Equal("outer|inner one|inner two", cellText); + var last = Assert.IsType(read.Sections[0].Blocks[2]); + Assert.Contains("Boxed text", string.Concat(last.Inlines.OfType().Select(t => t.Text)), StringComparison.Ordinal); + // GIF and BMP become PNG. + Assert.Equal(2, last.Inlines.OfType().Count()); + Assert.All(last.Inlines.OfType(), image => Assert.Equal(".png", Path.GetExtension(image.Path))); + } + + // ───────────────────────── Routes ───────────────────────── + + [Fact] + public void Documents_go_to_rtf_through_the_built_in_writer() + { + var builtIn = new ConverterCatalog(fx.Converters.Where(c => !OptionalEngines.Contains(c.Id))) { Priority = fx.Catalog.Priority }.CreatePlanner(["rhwp"]); + var all = fx.Catalog.CreatePlanner(["pandoc", "libreoffice", "rhwp"]); + foreach (var from in new[] { "docx", "odt", "hwp", "hwpx", "md", "html", "txt", "doc" }) + { + Assert.Equal("rtf-writer", Assert.Single(builtIn.Plan(from, "rtf")!.Steps).Converter.Id); + Assert.Equal("rtf-writer", Assert.Single(all.Plan(from, "rtf")!.Steps).Converter.Id); + } + Assert.Null(builtIn.Plan("rtf", "rtf")); + } + + // ───────────────────────── Limits ───────────────────────── + + [Fact] + public void A_file_beyond_the_size_limit_is_refused_without_leaving_anything() + { + var document = new HDocument(); + var section = new HSection(); + for (var i = 0; i < 2000; i++) + section.Blocks.Add(Para($"Paragraph {i} with some text to fill the file.")); + document.Sections.Add(section); + var folder = fx.NewFolder(); + var path = Path.Combine(folder, "big.rtf"); + + var refused = Assert.Throws(() => + RtfWriter.Write(document, path, new DocumentLimits { MaxPackageBytes = 20_000 }, TestContext.Current.CancellationToken)); + + Assert.Equal(DocumentLimits.ErrorKey, refused.ErrorKey); + Assert.Empty(Directory.GetFiles(folder)); + } + + [Fact] + public void Hostile_models_are_refused_or_written_quickly() + { + // Tables 5,000 deep are opened up into paragraphs past the nesting limit. + var deep = new HDocument(); + var section = new HSection(); + deep.Sections.Add(section); + var blocks = section.Blocks; + for (var i = 0; i < 5000; i++) + { + var table = new HTable { ColumnCount = 1 }; + var row = new HRow(); + var cell = new HCell(); + cell.Blocks.Add(Para($"level {i}")); + row.Cells.Add(cell); + table.Rows.Add(row); + blocks.Add(table); + blocks = cell.Blocks; + } + var started = System.Diagnostics.Stopwatch.StartNew(); + var (read, _) = RoundTrip(deep); + Assert.True(started.Elapsed < TimeSpan.FromSeconds(30), $"Took {started.Elapsed}."); + Assert.NotEmpty(read.Sections[0].Blocks); + + // A cell spanning 16,384 columns over 1,000 rows passes the table cell limit. + var wide = new HDocument(); + var wideSection = new HSection(); + var huge = new HTable { ColumnCount = 16_384 }; + for (var r = 0; r < 1000; r++) + { + var row = new HRow(); + row.Cells.Add(new HCell { ColSpan = 16_384 }); + huge.Rows.Add(row); + } + wideSection.Blocks.Add(huge); + wide.Sections.Add(wideSection); + var folder = fx.NewFolder(); + Assert.Throws(() => RtfWriter.Write(wide, Path.Combine(folder, "wide.rtf"), TestContext.Current.CancellationToken)); + Assert.Empty(Directory.GetFiles(folder)); + + // A picture whose header claims ten gigapixels is left out. + var claiming = DocumentFuzzTests.TinyPng(4, 3); + System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(claiming.AsSpan(16), 100_000); + System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(claiming.AsSpan(20), 100_000); + var picture = Path.Combine(fx.NewFolder(), "huge.png"); + File.WriteAllBytes(picture, claiming); + var pictured = new HDocument(); + var pictureSection = new HSection(); + pictureSection.Blocks.Add(Para(new HText("x", default), new HImage(picture))); + pictured.Sections.Add(pictureSection); + Assert.DoesNotContain("\\pict", RoundTrip(pictured).Rtf, StringComparison.Ordinal); + } + + [Fact] + public void A_cancelled_write_leaves_no_file() + { + var folder = fx.NewFolder(); + using var cancelled = new CancellationTokenSource(); + cancelled.Cancel(); + + Assert.ThrowsAny(() => RtfWriter.Write(Sample(), Path.Combine(folder, "out.rtf"), cancelled.Token)); + + Assert.Empty(Directory.GetFiles(folder)); + } +} From 71eb28b6a051daf86f4f2e6d3cd120a210da339f Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 18:10:18 +0900 Subject: [PATCH 3/6] Write PDF/A-2b without Ghostscript PDFs of a PDF/A preset are made PDF/A-2b by the PDF engine first, with Ghostscript as the fallback for what it can't make conform: - PdfAConformance changes a PDF opened with PDFsharp to keep the ISO 19005-2 level B rules: one GTS_PDFA1 output intent (sRGB, or a CMYK/RGB PDF/A intent the file brings), no encryption, LZW or external streams, no forbidden actions, JavaScript, AA, embedded files or XFA, annotations that print with only their normal appearance, cleaned graphics states, pictures and forms, named optional content configs. It refuses with a pdfa.reason.* key when a font isn't embedded, CMYK has no CMYK intent, an annotation has no appearance, or the file holds PostScript, XFA-only forms or data it can't read. - PdfAMetadata saves it and appends an incremental update that rewrites the document information and XMP (pdfaid 2/B) so both match: PDFsharp always writes an XMP packet of its own on save. - SrgbProfile builds a version 2.1 sRGB ICC profile from IEC 61966-2-1 in code, so no third-party profile is shipped. - PdfSharpConverter is an IPdfArchiver; JobQueue tries archivers in engine order, a refusal hands the PDF to the next, and a PDF none archives gets the notice with the refusal's reason. - The Filee renderer (documents, sheets, slides, drawings) writes PDF/A directly when the preset asks for it. - A PDF/A preset for PDFs no longer needs Ghostscript to be usable. Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 77 +- src/Filee.App/Assets/i18n/ar.json | 6 + src/Filee.App/Assets/i18n/bn-BD.json | 6 + src/Filee.App/Assets/i18n/de.json | 6 + src/Filee.App/Assets/i18n/en.json | 6 + src/Filee.App/Assets/i18n/es.json | 6 + src/Filee.App/Assets/i18n/fa.json | 6 + src/Filee.App/Assets/i18n/fr.json | 6 + src/Filee.App/Assets/i18n/hi.json | 6 + src/Filee.App/Assets/i18n/id.json | 6 + src/Filee.App/Assets/i18n/it.json | 6 + src/Filee.App/Assets/i18n/ja.json | 6 + src/Filee.App/Assets/i18n/ko.json | 6 + src/Filee.App/Assets/i18n/mn.json | 6 + src/Filee.App/Assets/i18n/ne.json | 6 + src/Filee.App/Assets/i18n/pl.json | 6 + src/Filee.App/Assets/i18n/pt-BR.json | 6 + src/Filee.App/Assets/i18n/pt-PT.json | 6 + src/Filee.App/Assets/i18n/ru.json | 6 + src/Filee.App/Assets/i18n/th.json | 6 + src/Filee.App/Assets/i18n/tr.json | 6 + src/Filee.App/Assets/i18n/vi.json | 6 + src/Filee.App/Assets/i18n/zh-CN.json | 6 + src/Filee.App/Assets/i18n/zh-TW.json | 6 + src/Filee.App/Services/PresetAvailability.cs | 7 +- src/Filee.Core/Conversion/IConverter.cs | 9 +- src/Filee.Core/Conversion/JobQueue.cs | 36 +- .../Drawings/DrawingConverter.cs | 5 +- .../Office/SheetPrintConverter.cs | 3 +- .../Office/Sheets/Print/SheetPrinter.cs | 5 +- src/Filee.Engines/Pdf/PdfAConformance.cs | 856 ++++++++++++++++++ src/Filee.Engines/Pdf/PdfAMetadata.cs | 262 ++++++ src/Filee.Engines/Pdf/PdfSharpConverter.cs | 51 +- src/Filee.Engines/Pdf/SrgbProfile.cs | 179 ++++ src/Filee.Engines/Rendering/PdfPageWriter.cs | 49 +- src/Filee.Engines/Slides/SlideOutput.cs | 5 +- .../Typesetting/FileeRenderer.cs | 5 +- .../Typesetting/FileeRendererConverter.cs | 5 +- .../Filee.App.Tests/PdfAAvailabilityTests.cs | 18 +- tests/Filee.Core.Tests/PdfAQueueTests.cs | 77 ++ tests/Filee.Engines.Tests/PdfA2bChecks.cs | 445 +++++++++ .../PdfAConformanceTests.cs | 465 ++++++++++ 42 files changed, 2645 insertions(+), 52 deletions(-) create mode 100644 src/Filee.Engines/Pdf/PdfAConformance.cs create mode 100644 src/Filee.Engines/Pdf/PdfAMetadata.cs create mode 100644 src/Filee.Engines/Pdf/SrgbProfile.cs create mode 100644 tests/Filee.Engines.Tests/PdfA2bChecks.cs create mode 100644 tests/Filee.Engines.Tests/PdfAConformanceTests.cs diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 8046fdbd1..c8f2c4526 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -44,7 +44,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | Archives | built in (SharpCompress, DiscUtils, .NET and Filee's own readers and writers); 7-Zip optional (Settings → *Engines*, ~2 MB download) | **in-process**: ZIP (including ANSYS Workbench WBPZ and Adobe ZXP, and Windows app packages MSIX / APPX), Minecraft and Modrinth packs, Garry's Mod addons (GMA), Valve paks (VPK), the files packed into Source maps (BSP), 7Z (solid ones too), RAR, TAR (+ GZ/BZ2/XZ/Z/7Z/LZ/ZST), GZ, BZ2, XZ, LZMA, Z, ZST, lzip, CPIO, DEB, RPM, ISO (UDF discs too), IMG and DMG disk images, split (.001), ALZ, EGG, CAB, LHA, ARJ; **7-Zip, when installed**: the rare variants the in-process readers leave to it (see [Archives](#archives)) **→ folder, ZIP, 7Z, TAR, TAR.GZ, TAR.BZ2, TAR.XZ, BZ2, XZ, the packs, GMA** (in-process); "Compress into one archive" for any files | | LibreOffice | **optional download** (~370 MB, 1.1 GB on disk) | older and rare formats only: Word 6.0 / 95 and Works documents, PowerPoint 95 and older PPT, binary Visio (`.vsd`), and ODP / ODG with what Filee doesn't draw yet (charts, OLE objects, 3D) → PDF; XLS, PPT, RTF and OpenDocument to each other, output as DOCX/ODT/ODS, and ODP for what the [ODP writer](#odp-writer) doesn't take (OpenDocument presentations themselves, Keynote, decks with charts, effects or 3D); read-only formats (see below) → PDF and their kind's editable formats; FODT, OpenDocument templates (OTT, OTS, OTP) and XLT out; SVG → EMF/WMF/ODG, a PDF page → SVG | | Pandoc | **optional download** (~42 MB, 240 MB on disk) | Markdown ↔ DOCX/ODT/RTF, DOCX/ODT/HTML/RTF → Markdown, HTML ↔ DOCX/ODT, markup languages (LaTeX, AsciiDoc, Typst, man; and RTF / EPUB from the built-in ones) ↔ Markdown/HTML/DOCX/ODT and each other (and → RTF, EPUB, TXT); their AST feeds the built-in PPTX writer (Pandoc's own slides only where Filee assumes a Pandoc that isn't installed) | -| Ghostscript | **optional download** (~20 MB, 31 MB on disk) | EPS/PS → PDF, PostScript-based AI → PDF (images through PDF and PDFium), EPS/PS → PNG; PDF → EPS/PS only when moved above the built-in PostScript writer; smaller PDFs when a preset asks for it; PDF/A-2b for PDF/A presets; renders CMYK PDF/AI pages for CMYK JPG/TIFF | +| Ghostscript | **optional download** (~20 MB, 31 MB on disk) | EPS/PS → PDF, PostScript-based AI → PDF (images through PDF and PDFium), EPS/PS → PNG; PDF → EPS/PS only when moved above the built-in PostScript writer; smaller PDFs when a preset asks for it; PDF/A-2b for what the built-in PDF/A writer refuses; renders CMYK PDF/AI pages for CMYK JPG/TIFF | | Tesseract | **optional download** (~50 MB, 160 MB on disk; each language 0.4–10 MB when first used) | **text recognition (OCR)**: scans and pictures → searchable PDF, and their text into DOCX, TXT, Markdown and HWPX ("Recognize text" in the preset, see below) | | Apple Vision | part of macOS (Mac only) | **text recognition (OCR) on a Mac**, the same step as Tesseract without a download; Tesseract reads the languages Vision doesn't (see "Apple Vision on macOS") | | FFmpeg | **optional download** (~100 MB, 272 MB on disk) | **all video and audio**: video ↔ video (H.264, HEVC, AV1), video → animated GIF / APNG / WebP, a still frame or every frame, audio and subtitle extraction, audio ↔ audio (also APE, WavPack, TTA, DSD, DTS, ALAC, …), GIF, APNG and photos → MP4/WEBM/MOV, Live / Motion Photos → their video, a clip of a video → an iPhone Live Photo or Android Motion Photo (experimental), images → HEIC, clips or audio files joined into one, pictures → a slideshow MP4, a video split into parts every so often or into equal parts (copied, not encoded), copies without metadata (see "Media tools") | @@ -2612,20 +2612,67 @@ wrote it (PDFsharp, rhwp, LibreOffice, Ghostscript …), and once on a merged PD ## PDF/A A preset with *PDF/A (archival)* ticked (`DocumentOptions.PdfA`; the built-in "PDF/A" preset, #1367) makes every PDF -it writes a PDF/A-2b, PDF → PDF too. `JobQueue` runs `IPdfArchiver` (Ghostscript) after the finishing, on each PDF a -file became and once on a merged PDF, so the box means the same whichever engine wrote the PDF. - -- **Already PDF/A**: LibreOffice (`SelectPdfVersion` 2) and SVG → PDF (Skia) write PDF/A themselves. A PDF whose - catalog XMP has `pdfaid:part` is kept as it is, unless a watermark or page numbers were added since: then it is - rewritten like any other. -- **Ghostscript** (`GhostscriptConverter.ConvertToPdfAAsync`): `pdfwrite` with `-dPDFA=2 -dPDFACompatibilityPolicy=1 - -sColorConversionStrategy=RGB`, after a small PostScript prelude (as Ghostscript's `PDFA_def.ps`) that adds the sRGB - output intent; the profile is Magick.NET's sRGB, written into the prelude as a hex string so safe mode needn't read - a file. Colours become RGB, fonts are embedded, what PDF/A forbids is left out (policy 1), and a tagged PDF loses its - tags (part B doesn't need them). -- **Without Ghostscript**: a preset whose files are all PDFs is greyed out with the usual engine reason. Other files - are converted and the PDF is left as it is, with `notice.pdfa_needs_ghostscript` in the result, unless LibreOffice - made it PDF/A. +it writes a PDF/A-2b, PDF → PDF too. `JobQueue` runs the `IPdfArchiver`s after the finishing, on each PDF a file +became and once on a merged PDF, so the box means the same whichever engine wrote the PDF. They are tried in engine +order (PDFsharp before Ghostscript by default): one that refuses a PDF (`ConversionRefusedException`) hands it to the +next. + +- **Already PDF/A**: the Filee renderer (below), LibreOffice (`SelectPdfVersion` 2) and SVG → PDF (Skia) write PDF/A + themselves. A PDF whose catalog XMP has `pdfaid:part` is kept as it is, unless a watermark or page numbers were + added since: then it is rewritten like any other. +- **Built in** (`PdfSharpConverter.ArchiveAsync` → `Pdf/PdfAConformance.cs`, `PdfAMetadata.cs`): the PDF is opened with + PDFsharp and changed in place to keep the PDF/A-2b rules (ISO 19005-2), form fields staying fields: + - one `GTS_PDFA1` output intent: a CMYK or RGB PDF/A intent the PDF already has for the device colours it draws in + (a PDF/X file keeps its CMYK profile), else **sRGB made in code** (`SrgbProfile`, below); PDFsharp's page + transparency groups get the intent's colour space; + - no encryption, no LZW (unpacked and deflated), no external stream data; graphics states lose `TR`, `TR2` (but + `/Default`), `HT`, `HTO`, unknown `RI` and blend modes; pictures lose `Alternates`, `OPI`, `Interpolate`; forms + lose `OPI`, `PS`, `Ref`; + - annotations: 3D, sound, screen, movie, rich media, file attachments and hidden ones go; the rest print and keep + only their normal appearance (a button's single stream becomes its one state, a state set outside buttons the + state shown); popups lose flags PDF/A forbids; + - JavaScript, launch and the other forbidden actions (also in `/Next` chains, open actions, outlines), every `AA`, + the actions of fields and widgets, XFA, `NeedAppearances`, embedded files, alternate presentations, `PresSteps`, + `Requirements`, `Collection`, other `Perms` than UR3/DocMDP, XMP of pages and pictures; optional-content + configurations get unique names, no `AS`, and an `Order` that lists every group; CIDFontType2 gets + `CIDToGIDMap /Identity` when it has none; PDF 2.0 is written as 1.7. + + Saving: PDFsharp always replaces `/Metadata` with an XMP packet of its own (no `pdfaid`, dates that don't always + match) and puts its name in the producer, and it can't be told not to (`SetPdfA` claims PDF/A-1a). So the PDF is + saved by PDFsharp, then an **incremental update** (which PDF/A allows) writes the document information and the XMP + packet again under their own object numbers: `pdfaid:part 2`, `conformance B`, title, author, subject, keywords, + creator tool, producer and dates the same in both (text cleaned of what XML can't carry, so both always match), + the trailer's `/ID` kept, `/Prev` to PDFsharp's table. Custom information entries and `Trapped` are dropped. + + It **refuses** (`pdfa.reason.*`, the PDF left as it is) a font drawn with that isn't embedded (any font listed in a + `/Resources` dictionary; a form's `/DR` doesn't count), device CMYK without a CMYK intent or CMYK and RGB together, + an annotation or field without its own appearance (but links, popups, zero-size ones), XFA-only forms, PostScript + XObjects, external stream data, LZW mixed with filters PDFsharp can't unpack, inline pictures with LZW or + interpolation, content streams it can't read, a password-protected PDF, and (`error.document_too_complex`) more + than `MaxObjects` objects or more content than `PdfFontUses` reads. + + **Not checked**: that embedded fonts hold every glyph drawn and their widths match the dictionary, TrueType + symbolic/non-symbolic encodings and cmaps, the document's own ICC-based colour spaces, JPEG 2000 specifics, halftone + details beyond dropping `HT`, the implementation limits (string, name and number sizes, `q` nesting), page-level + colour spaces through `DefaultCMYK`, and content stream operators outside ISO 32000-1. Conformance is tested by a + structural check of the rules above (`tests/Filee.Engines.Tests/PdfA2bChecks.cs`); veraPDF isn't run. +- **sRGB profile** (`Pdf/SrgbProfile.cs`): built from the numbers of IEC 61966-2-1 rather than shipped: primaries + and D65 adapted to D50 with the Bradford transform, the sRGB curve sampled at 1024 points shared by the three + channels, an ICC version 2.1 display profile of about 2.3 KB with the description "sRGB IEC61966-2.1" and the + copyright "No copyright, use freely". No one's profile file is copied, so there is no licence to carry; skcms + (Skia) reads it as sRGB's gamut and curve. +- **Filee renderer** (`PdfPageWriter`, `PdfDocumentInfo.PdfA`): DOCX, HWP/HWPX, sheets, slides and drawings drawn by + the renderer are written as PDF/A-2b directly: Skia's PDF/A mode, then the PDFsharp pass with `PdfAConformance` and + `PdfAMetadata`, creator and producer "Filee". Fonts are already embedded and subset with ToUnicode maps, links get + the print flag. Should the pass refuse, the plain PDF is written and the job queue's archivers try again. +- **Ghostscript** (`GhostscriptConverter.ConvertToPdfAAsync`), for what the built-in one refuses: `pdfwrite` with + `-dPDFA=2 -dPDFACompatibilityPolicy=1 -sColorConversionStrategy=RGB`, after a small PostScript prelude (as + Ghostscript's `PDFA_def.ps`) that adds the sRGB output intent; the profile is Magick.NET's sRGB, written into the + prelude as a hex string so safe mode needn't read a file. Colours become RGB, fonts are embedded, what PDF/A forbids + is left out (policy 1), form fields become page content, and a tagged PDF loses its tags (part B doesn't need them). +- **When none makes it PDF/A**: the PDF is left as it is, with `notice.pdfa_needs_ghostscript` and the built-in one's + reason in brackets. A preset whose files are all PDFs is greyed out only when neither PDFsharp nor Ghostscript is + among the engines. ## Office Open XML variants diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index 111ab5f92..fcef87910 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "هذا الملف بنظام CMYK لكنه حُفظ بنظام RGB: ثبّت Ghostscript من الإعدادات ← المحركات للإبقاء على CMYK", "notice.pdfa_needs_ghostscript": "هذا الملف ليس PDF/A: ثبّت Ghostscript من الإعدادات ← المحركات لإنشاء PDF/A", "notice.pdfa_form_fields": "تم إنشاء PDF/A: لم يعد بالإمكان تعبئة حقول النموذج", + "pdfa.reason.font_not_embedded": "خط مستخدم غير مضمّن", + "pdfa.reason.cmyk": "يجب تحويل ألوان CMYK فيه", + "pdfa.reason.no_appearance": "تعليق توضيحي أو حقل نموذج بلا مظهر خاص به", + "pdfa.reason.unsupported": "فيه محتوى لا يسمح به PDF/A ولا يستطيع Filee إزالته", + "pdfa.reason.unreadable": "تعذّر على Filee قراءته", + "pdfa.reason.locked": "محمي بكلمة مرور", "notice.cmyk_render_failed": "هذا الملف بنظام CMYK، لكن Ghostscript لم يتمكن من رسمه، لذلك حُفظ بنظام RGB", "notice.original_kept": "تم الاحتفاظ بالأصل وحُفظت النتيجة كنسخة", "notice.original_in_trash": "الأصل في سلة المحذوفات، واحتفظت النتيجة باسمها", diff --git a/src/Filee.App/Assets/i18n/bn-BD.json b/src/Filee.App/Assets/i18n/bn-BD.json index 11ea3129b..57db4edf4 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -217,6 +217,12 @@ "notice.cmyk_needs_ghostscript": "এই ফাইলটি CMYK হলেও RGB হিসেবে সংরক্ষণ করা হয়েছে: CMYK রাখতে সেটিংস → ইঞ্জিন-এ Ghostscript ইনস্টল করুন", "notice.pdfa_needs_ghostscript": "এই পিডিএফটি PDF/A নয়: PDF/A বানাতে সেটিংস → ইঞ্জিন-এ Ghostscript ইনস্টল করুন", "notice.pdfa_form_fields": "PDF/A তৈরি হয়েছে: ফর্মের ঘরগুলো আর পূরণ করা যাবে না", + "pdfa.reason.font_not_embedded": "ব্যবহৃত একটি ফন্ট এমবেড করা নেই", + "pdfa.reason.cmyk": "এর CMYK রং রূপান্তর করতে হবে", + "pdfa.reason.no_appearance": "কোনো টীকা বা ফর্ম ফিল্ডের নিজস্ব চেহারা নেই", + "pdfa.reason.unsupported": "এতে এমন বিষয়বস্তু আছে যা PDF/A অনুমতি দেয় না এবং Filee সরাতে পারে না", + "pdfa.reason.unreadable": "Filee এটি পড়তে পারেনি", + "pdfa.reason.locked": "এটি পাসওয়ার্ড দিয়ে লক করা", "notice.cmyk_render_failed": "এই ফাইলটি CMYK, কিন্তু Ghostscript এটি আঁকতে পারেনি, তাই RGB হিসেবে সংরক্ষণ করা হয়েছে", "notice.original_kept": "মূল ফাইলটি রাখা হয়েছে, ফলাফলটি একটি কপি হিসেবে সংরক্ষণ করা হয়েছে", "notice.original_in_trash": "মূল ফাইলটি রিসাইকেল বিন / ট্র্যাশে রয়েছে, ফলাফলটি নিজস্ব নাম বজায় রেখেছে", diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.json index 31d8f9826..03b98eada 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "Diese Datei ist CMYK, wurde aber als RGB gespeichert: Installiere Ghostscript unter Einstellungen → Module, um CMYK zu behalten", "notice.pdfa_needs_ghostscript": "Diese PDF ist kein PDF/A: Installiere Ghostscript unter Einstellungen → Module, um PDF/A zu erstellen", "notice.pdfa_form_fields": "PDF/A erstellt: Die Formularfelder können nicht mehr ausgefüllt werden", + "pdfa.reason.font_not_embedded": "eine verwendete Schrift ist nicht eingebettet", + "pdfa.reason.cmyk": "die CMYK-Farben müssten umgewandelt werden", + "pdfa.reason.no_appearance": "eine Anmerkung oder ein Formularfeld hat kein eigenes Aussehen", + "pdfa.reason.unsupported": "sie enthält Inhalte, die PDF/A nicht erlaubt und die Filee nicht entfernen kann", + "pdfa.reason.unreadable": "Filee konnte sie nicht lesen", + "pdfa.reason.locked": "sie ist mit einem Passwort geschützt", "notice.cmyk_render_failed": "Diese Datei ist CMYK, aber Ghostscript konnte sie nicht zeichnen, daher wurde sie als RGB gespeichert", "notice.original_kept": "Das Original wurde beibehalten und das Ergebnis als Kopie gespeichert", "notice.original_in_trash": "Das Original liegt im Papierkorb. Das Ergebnis hat seinen eigenen Namen behalten", diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json index 98fb4b38c..3e95ac4d6 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "This file is CMYK but was saved as RGB: install Ghostscript in Settings → Engines to keep CMYK", "notice.pdfa_needs_ghostscript": "This PDF isn't PDF/A: install Ghostscript in Settings → Engines to make it PDF/A", "notice.pdfa_form_fields": "Made PDF/A: its form fields can no longer be filled in", + "pdfa.reason.font_not_embedded": "a font it uses isn't embedded", + "pdfa.reason.cmyk": "its CMYK colours have to be converted", + "pdfa.reason.no_appearance": "an annotation or form field has no appearance of its own", + "pdfa.reason.unsupported": "it has content PDF/A doesn't allow that Filee can't take out", + "pdfa.reason.unreadable": "Filee couldn't read it", + "pdfa.reason.locked": "it is locked with a password", "notice.cmyk_render_failed": "This file is CMYK, but Ghostscript couldn't draw it, so it was saved as RGB", "notice.original_kept": "The original was kept, the result was saved as a copy", "notice.original_in_trash": "The original is in the Recycle Bin / Trash, the result kept its own name", diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.json index f52142523..71234236d 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "Este archivo es CMYK, pero se guardó en RGB: instala Ghostscript en Ajustes → Motores para mantener CMYK", "notice.pdfa_needs_ghostscript": "Este PDF no es PDF/A: instala Ghostscript en Ajustes → Motores para crear PDF/A", "notice.pdfa_form_fields": "Se creó un PDF/A: los campos del formulario ya no se pueden rellenar", + "pdfa.reason.font_not_embedded": "una fuente que usa no está incrustada", + "pdfa.reason.cmyk": "habría que convertir sus colores CMYK", + "pdfa.reason.no_appearance": "una anotación o un campo de formulario no tiene apariencia propia", + "pdfa.reason.unsupported": "tiene contenido que PDF/A no permite y que Filee no puede quitar", + "pdfa.reason.unreadable": "Filee no pudo leerlo", + "pdfa.reason.locked": "está protegido con contraseña", "notice.cmyk_render_failed": "Este archivo es CMYK, pero Ghostscript no pudo dibujarlo, así que se guardó en RGB", "notice.original_kept": "Se conservó el original, el resultado se guardó como copia", "notice.original_in_trash": "El original está en la Papelera, el resultado conservó su propio nombre", diff --git a/src/Filee.App/Assets/i18n/fa.json b/src/Filee.App/Assets/i18n/fa.json index 8de6630ad..0d2f8586f 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -217,6 +217,12 @@ "notice.cmyk_needs_ghostscript": "این فایل CMYK است اما به صورت RGB ذخیره شد: برای حفظ CMYK، نرم‌افزار Ghostscript را در تنظیمات ← موتورها نصب کنید", "notice.pdfa_needs_ghostscript": "این PDF از نوع PDF/A نیست: برای ساخت PDF/A، نرم‌افزار Ghostscript را در تنظیمات ← موتورها نصب کنید", "notice.pdfa_form_fields": "PDF/A ساخته شد: دیگر نمی‌توان فیلدهای فرم را پر کرد", + "pdfa.reason.font_not_embedded": "فونتی که به کار رفته جاسازی نشده است", + "pdfa.reason.cmyk": "رنگ‌های CMYK آن باید تبدیل شوند", + "pdfa.reason.no_appearance": "یک یادداشت یا فیلد فرم ظاهر خودش را ندارد", + "pdfa.reason.unsupported": "محتوایی دارد که PDF/A اجازه نمی‌دهد و Filee نمی‌تواند حذفش کند", + "pdfa.reason.unreadable": "Filee نتوانست آن را بخواند", + "pdfa.reason.locked": "با گذرواژه قفل شده است", "notice.cmyk_render_failed": "این فایل CMYK است، اما Ghostscript نتوانست آن را رسم کند، بنابراین به صورت RGB ذخیره شد", "notice.original_kept": "فایل اصلی حفظ شد و نتیجه به عنوان نسخه کپی ذخیره گردید", "notice.original_in_trash": "فایل اصلی در سطل زباله است، نتیجه نام خود را حفظ کرد", diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.json index 8ba6306ca..377c60294 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "Ce fichier est en CMJN mais a été enregistré en RVB : installez Ghostscript dans Paramètres → Moteurs pour garder le CMJN", "notice.pdfa_needs_ghostscript": "Ce PDF n'est pas un PDF/A : installez Ghostscript dans Paramètres → Moteurs pour créer du PDF/A", "notice.pdfa_form_fields": "PDF/A créé : les champs du formulaire ne peuvent plus être remplis", + "pdfa.reason.font_not_embedded": "une police utilisée n'est pas incorporée", + "pdfa.reason.cmyk": "ses couleurs CMJN devraient être converties", + "pdfa.reason.no_appearance": "une annotation ou un champ de formulaire n'a pas d'apparence propre", + "pdfa.reason.unsupported": "il contient des éléments interdits en PDF/A que Filee ne peut pas retirer", + "pdfa.reason.unreadable": "Filee n'a pas pu le lire", + "pdfa.reason.locked": "il est protégé par un mot de passe", "notice.cmyk_render_failed": "Ce fichier est en CMJN, mais Ghostscript n'a pas pu le dessiner : il a été enregistré en RVB", "notice.original_kept": "L'original a été conservé, le résultat a été enregistré en copie", "notice.original_in_trash": "L'original est dans la Corbeille, le résultat a gardé son propre nom", diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.json index f6965d2f9..f7c75ee62 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "यह फ़ाइल CMYK है, लेकिन RGB में सेव हुई: CMYK बनाए रखने के लिए सेटिंग्स → इंजन में Ghostscript इंस्टॉल करें", "notice.pdfa_needs_ghostscript": "यह PDF, PDF/A नहीं है: PDF/A बनाने के लिए सेटिंग्स → इंजन में Ghostscript इंस्टॉल करें", "notice.pdfa_form_fields": "PDF/A बनाया गया: फ़ॉर्म के फ़ील्ड अब भरे नहीं जा सकते", + "pdfa.reason.font_not_embedded": "इस्तेमाल किया गया एक फ़ॉन्ट एम्बेड नहीं है", + "pdfa.reason.cmyk": "इसके CMYK रंगों को बदलना होगा", + "pdfa.reason.no_appearance": "किसी टिप्पणी या फ़ॉर्म फ़ील्ड का अपना रूप नहीं है", + "pdfa.reason.unsupported": "इसमें ऐसी सामग्री है जिसकी PDF/A अनुमति नहीं देता और Filee उसे हटा नहीं सकता", + "pdfa.reason.unreadable": "Filee इसे पढ़ नहीं सका", + "pdfa.reason.locked": "यह पासवर्ड से सुरक्षित है", "notice.cmyk_render_failed": "यह फ़ाइल CMYK है, लेकिन Ghostscript इसे नहीं बना सका, इसलिए यह RGB में सेव हुई", "notice.original_kept": "मूल फ़ाइल रखी गई, परिणाम कॉपी के रूप में सहेजा गया", "notice.original_in_trash": "मूल फ़ाइल रीसायकल बिन / ट्रैश में है, परिणाम ने अपना नाम रखा", diff --git a/src/Filee.App/Assets/i18n/id.json b/src/Filee.App/Assets/i18n/id.json index 9030c5343..7f94c7a14 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "File ini CMYK, tetapi disimpan sebagai RGB: pasang Ghostscript di Pengaturan → Engine agar tetap CMYK", "notice.pdfa_needs_ghostscript": "PDF ini bukan PDF/A: pasang Ghostscript di Pengaturan → Engine untuk membuat PDF/A", "notice.pdfa_form_fields": "PDF/A dibuat: kolom formulir tidak lagi bisa diisi", + "pdfa.reason.font_not_embedded": "font yang dipakai tidak disematkan", + "pdfa.reason.cmyk": "warna CMYK-nya perlu dikonversi", + "pdfa.reason.no_appearance": "anotasi atau kolom formulir tidak punya tampilan sendiri", + "pdfa.reason.unsupported": "ada konten yang tidak diizinkan PDF/A dan tidak bisa dihapus Filee", + "pdfa.reason.unreadable": "Filee tidak bisa membacanya", + "pdfa.reason.locked": "dikunci dengan kata sandi", "notice.cmyk_render_failed": "File ini CMYK, tetapi Ghostscript tidak bisa menggambarnya, jadi disimpan sebagai RGB", "notice.original_kept": "File asli dipertahankan, hasil konversi disimpan sebagai salinan", "notice.original_in_trash": "File asli ada di Tong Sampah, hasil konversi mempertahankan namanya sendiri", diff --git a/src/Filee.App/Assets/i18n/it.json b/src/Filee.App/Assets/i18n/it.json index 3d900fcd2..e348cb050 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -202,6 +202,12 @@ "notice.cmyk_needs_ghostscript": "Questo file è CMYK ma è stato salvato in RGB: installa Ghostscript in Impostazioni → Motori per mantenere il CMYK", "notice.pdfa_needs_ghostscript": "Questo PDF non è PDF/A: installa Ghostscript in Impostazioni → Motori per creare PDF/A", "notice.pdfa_form_fields": "PDF/A creato: i campi del modulo non possono più essere compilati", +"pdfa.reason.font_not_embedded": "un font usato non è incorporato", +"pdfa.reason.cmyk": "i suoi colori CMYK andrebbero convertiti", +"pdfa.reason.no_appearance": "un'annotazione o un campo modulo non ha un aspetto proprio", +"pdfa.reason.unsupported": "contiene elementi non ammessi in PDF/A che Filee non può rimuovere", +"pdfa.reason.unreadable": "Filee non è riuscito a leggerlo", +"pdfa.reason.locked": "è protetto da password", "notice.cmyk_render_failed": "Questo file è CMYK, ma Ghostscript non è riuscito a disegnarlo, quindi è stato salvato in RGB", "notice.original_kept": "L'originale è stato conservato, il risultato è stato salvato come copia", "notice.original_in_trash": "L'originale è nel Cestino, il risultato ha mantenuto il proprio nome", diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json index 890ee52c3..890f150e3 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "CMYK のファイルですが RGB で保存しました。CMYK のまま保存するには「設定」→「変換エンジン」で Ghostscript をインストールしてください", "notice.pdfa_needs_ghostscript": "この PDF は PDF/A ではありません。PDF/A にするには「設定」→「変換エンジン」で Ghostscript をインストールしてください", "notice.pdfa_form_fields": "PDF/A に変換しました。フォームの入力欄には入力できなくなりました", + "pdfa.reason.font_not_embedded": "使われているフォントが埋め込まれていません", + "pdfa.reason.cmyk": "CMYK の色を変換する必要があります", + "pdfa.reason.no_appearance": "注釈かフォームフィールドに外観がありません", + "pdfa.reason.unsupported": "PDF/A で使えない内容を Filee が取り除けません", + "pdfa.reason.unreadable": "Filee がファイルを読み込めませんでした", + "pdfa.reason.locked": "パスワードで保護されています", "notice.cmyk_render_failed": "CMYK のファイルですが、Ghostscript で描画できなかったため RGB で保存しました", "notice.original_kept": "元のファイルを残し、結果をコピーとして保存しました", "notice.original_in_trash": "元のファイルはごみ箱にあります。結果は別の名前のままです", diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json index e1dae63ca..0c1ac3df6 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "CMYK 파일인데 RGB로 저장했어요. CMYK로 받으려면 설정 → 변환 엔진에서 Ghostscript를 설치하세요", "notice.pdfa_needs_ghostscript": "이 PDF는 PDF/A가 아니에요. PDF/A로 받으려면 설정 → 변환 엔진에서 Ghostscript를 설치하세요", "notice.pdfa_form_fields": "PDF/A로 만들면서 양식 입력 칸은 더 이상 채울 수 없게 됐어요", + "pdfa.reason.font_not_embedded": "사용한 글꼴이 파일에 들어 있지 않아요", + "pdfa.reason.cmyk": "CMYK 색을 변환해야 해요", + "pdfa.reason.no_appearance": "주석이나 양식 필드에 자체 모양이 없어요", + "pdfa.reason.unsupported": "PDF/A에서 허용하지 않는 내용을 Filee가 뺄 수 없어요", + "pdfa.reason.unreadable": "Filee가 파일을 읽지 못했어요", + "pdfa.reason.locked": "암호로 잠겨 있어요", "notice.cmyk_render_failed": "CMYK 파일인데 Ghostscript가 그리지 못해서 RGB로 저장했어요", "notice.original_kept": "원본은 그대로 두고 결과를 사본으로 저장했어요", "notice.original_in_trash": "원본은 휴지통으로 옮겼고, 결과는 저장된 이름 그대로예요", diff --git a/src/Filee.App/Assets/i18n/mn.json b/src/Filee.App/Assets/i18n/mn.json index f3d78b428..112771fb3 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -202,6 +202,12 @@ "notice.cmyk_needs_ghostscript": "Энэ файл CMYK боловч RGB хэлбэрээр хадгалагдлаа: CMYK хэвээр хадгалахын тулд Тохиргоо → Хөдөлгүүрүүд хэсгээс Ghostscript суулгана уу", "notice.pdfa_needs_ghostscript": "Энэ PDF нь PDF/A биш байна: PDF/A болгохын тулд Тохиргоо → Хөдөлгүүрүүд хэсгээс Ghostscript суулгана уу", "notice.pdfa_form_fields": "PDF/A болголоо: маягтын талбаруудыг цаашид бөглөх боломжгүй", + "pdfa.reason.font_not_embedded": "ашигласан фонт шингээгдээгүй байна", + "pdfa.reason.cmyk": "CMYK өнгийг хөрвүүлэх шаардлагатай", + "pdfa.reason.no_appearance": "тайлбар эсвэл маягтын талбарт өөрийн дүр төрх алга", + "pdfa.reason.unsupported": "PDF/A зөвшөөрдөггүй, Filee хасаж чадахгүй агуулга байна", + "pdfa.reason.unreadable": "Filee уншиж чадсангүй", + "pdfa.reason.locked": "нууц үгээр түгжигдсэн байна", "notice.cmyk_render_failed": "Энэ файл CMYK боловч Ghostscript зурж чадсангүй тул RGB хэлбэрээр хадгаллаа", "notice.original_kept": "Эх файлыг хэвээр үлдээж, үр дүнг хуулбар болгон хадгаллаа", "notice.original_in_trash": "Эх файл Хогийн саванд орж, үр дүнгийн файл нэрийг нь авлаа", diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.json index 14b2c4002..3bdfeb8c9 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "यो फाइल CMYK हो, तर RGB मा सेभ भयो: CMYK राख्न सेटिङहरू → इन्जिनहरूमा Ghostscript स्थापना गर्नुहोस्", "notice.pdfa_needs_ghostscript": "यो PDF, PDF/A होइन: PDF/A बनाउन सेटिङहरू → इन्जिनहरूमा Ghostscript स्थापना गर्नुहोस्", "notice.pdfa_form_fields": "PDF/A बनाइयो: फारमका फिल्डहरू अब भर्न सकिँदैन", + "pdfa.reason.font_not_embedded": "प्रयोग गरिएको एउटा फन्ट इम्बेड गरिएको छैन", + "pdfa.reason.cmyk": "यसका CMYK रङहरू रूपान्तरण गर्नुपर्छ", + "pdfa.reason.no_appearance": "कुनै टिप्पणी वा फारम फिल्डको आफ्नै रूप छैन", + "pdfa.reason.unsupported": "यसमा PDF/A ले अनुमति नदिने र Filee ले हटाउन नसक्ने सामग्री छ", + "pdfa.reason.unreadable": "Filee ले यसलाई पढ्न सकेन", + "pdfa.reason.locked": "यो पासवर्डले लक गरिएको छ", "notice.cmyk_render_failed": "यो फाइल CMYK हो, तर Ghostscript ले यसलाई कोर्न सकेन, त्यसैले RGB मा सेभ भयो", "notice.original_kept": "मूल फाइल राखियो, नतिजा प्रतिलिपिको रूपमा सुरक्षित भयो", "notice.original_in_trash": "मूल फाइल रिसाइकल बिन / ट्र्यासमा छ, नतिजाले आफ्नै नाम राख्यो", diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.json index 658fb1946..280d24a4f 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "Ten plik jest w CMYK, ale zapisano go w RGB: zainstaluj Ghostscript w Ustawienia → Silniki, aby zachować CMYK", "notice.pdfa_needs_ghostscript": "Ten PDF nie jest w formacie PDF/A: zainstaluj Ghostscript w Ustawienia → Silniki, aby tworzyć PDF/A", "notice.pdfa_form_fields": "Utworzono PDF/A: pól formularza nie można już wypełniać", + "pdfa.reason.font_not_embedded": "użyta czcionka nie jest osadzona", + "pdfa.reason.cmyk": "jego kolory CMYK trzeba by przekonwertować", + "pdfa.reason.no_appearance": "adnotacja lub pole formularza nie ma własnego wyglądu", + "pdfa.reason.unsupported": "zawiera treść niedozwoloną w PDF/A, której Filee nie może usunąć", + "pdfa.reason.unreadable": "Filee nie mógł go odczytać", + "pdfa.reason.locked": "jest chroniony hasłem", "notice.cmyk_render_failed": "Ten plik jest w CMYK, ale Ghostscript nie mógł go narysować, więc zapisano go w RGB", "notice.original_kept": "Oryginał został zachowany, wynik zapisano jako kopię", "notice.original_in_trash": "Oryginał jest w Koszu, wynik zachował własną nazwę", diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.json index 628fade4a..80e5142dd 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "Este arquivo é CMYK, mas foi salvo em RGB: instale o Ghostscript em Configurações → Motores para manter o CMYK", "notice.pdfa_needs_ghostscript": "Este PDF não é PDF/A: instale o Ghostscript em Configurações → Motores para criar PDF/A", "notice.pdfa_form_fields": "PDF/A criado: os campos do formulário não podem mais ser preenchidos", + "pdfa.reason.font_not_embedded": "uma fonte usada não está incorporada", + "pdfa.reason.cmyk": "as cores CMYK precisariam ser convertidas", + "pdfa.reason.no_appearance": "uma anotação ou campo de formulário não tem aparência própria", + "pdfa.reason.unsupported": "tem conteúdo que o PDF/A não permite e que o Filee não consegue remover", + "pdfa.reason.unreadable": "o Filee não conseguiu lê-lo", + "pdfa.reason.locked": "está protegido por senha", "notice.cmyk_render_failed": "Este arquivo é CMYK, mas o Ghostscript não conseguiu desenhá-lo, então foi salvo em RGB", "notice.original_kept": "O original foi mantido, o resultado foi salvo como cópia", "notice.original_in_trash": "O original está na Lixeira, o resultado manteve o próprio nome", diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.json index 09302ed56..be1434dec 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "Este ficheiro é CMYK, mas foi guardado em RGB: instala o Ghostscript em Definições → Motores para manter o CMYK", "notice.pdfa_needs_ghostscript": "Este PDF não é PDF/A: instala o Ghostscript em Definições → Motores para criar PDF/A", "notice.pdfa_form_fields": "PDF/A criado: os campos do formulário já não podem ser preenchidos", + "pdfa.reason.font_not_embedded": "um tipo de letra usado não está incorporado", + "pdfa.reason.cmyk": "as cores CMYK teriam de ser convertidas", + "pdfa.reason.no_appearance": "uma anotação ou campo de formulário não tem aparência própria", + "pdfa.reason.unsupported": "tem conteúdo que o PDF/A não permite e que o Filee não consegue remover", + "pdfa.reason.unreadable": "o Filee não o conseguiu ler", + "pdfa.reason.locked": "está protegido por palavra-passe", "notice.cmyk_render_failed": "Este ficheiro é CMYK, mas o Ghostscript não o conseguiu desenhar, por isso foi guardado em RGB", "notice.original_kept": "O original foi mantido e o resultado foi guardado como cópia", "notice.original_in_trash": "O original está na Reciclagem ou no Lixo; o resultado manteve o seu próprio nome", diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json index d74ffafdf..61c2a658f 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "Это файл CMYK, но он сохранён в RGB: чтобы сохранить CMYK, установите Ghostscript в разделе Настройки → Движки", "notice.pdfa_needs_ghostscript": "Этот PDF не в формате PDF/A: чтобы создавать PDF/A, установите Ghostscript в разделе Настройки → Движки", "notice.pdfa_form_fields": "Создан PDF/A: поля формы больше нельзя заполнять", + "pdfa.reason.font_not_embedded": "использованный шрифт не встроен", + "pdfa.reason.cmyk": "его цвета CMYK нужно преобразовать", + "pdfa.reason.no_appearance": "у аннотации или поля формы нет собственного вида", + "pdfa.reason.unsupported": "в нём есть содержимое, запрещённое в PDF/A, которое Filee не может убрать", + "pdfa.reason.unreadable": "Filee не удалось его прочитать", + "pdfa.reason.locked": "он защищён паролем", "notice.cmyk_render_failed": "Это файл CMYK, но Ghostscript не смог его отрисовать, поэтому он сохранён в RGB", "notice.original_kept": "Оригинал оставлен, результат сохранён как копия", "notice.original_in_trash": "Оригинал в Корзине, результат сохранил своё имя", diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.json index 338a98ec3..eda672591 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "ไฟล์นี้เป็น CMYK แต่บันทึกเป็น RGB: ติดตั้ง Ghostscript ในการตั้งค่า → เครื่องมือแปลงไฟล์ เพื่อคง CMYK ไว้", "notice.pdfa_needs_ghostscript": "PDF นี้ไม่ใช่ PDF/A: ติดตั้ง Ghostscript ในการตั้งค่า → เครื่องมือแปลงไฟล์ เพื่อสร้าง PDF/A", "notice.pdfa_form_fields": "สร้าง PDF/A แล้ว: ไม่สามารถกรอกช่องแบบฟอร์มได้อีก", + "pdfa.reason.font_not_embedded": "ฟอนต์ที่ใช้ไม่ได้ฝังอยู่ในไฟล์", + "pdfa.reason.cmyk": "ต้องแปลงสี CMYK ในไฟล์", + "pdfa.reason.no_appearance": "คำอธิบายประกอบหรือช่องฟอร์มไม่มีลักษณะที่แสดงของตัวเอง", + "pdfa.reason.unsupported": "มีเนื้อหาที่ PDF/A ไม่อนุญาตและ Filee เอาออกไม่ได้", + "pdfa.reason.unreadable": "Filee อ่านไฟล์นี้ไม่ได้", + "pdfa.reason.locked": "ไฟล์ถูกล็อกด้วยรหัสผ่าน", "notice.cmyk_render_failed": "ไฟล์นี้เป็น CMYK แต่ Ghostscript วาดไม่ได้ จึงบันทึกเป็น RGB", "notice.original_kept": "เก็บไฟล์ต้นฉบับไว้ และบันทึกผลลัพธ์เป็นสำเนา", "notice.original_in_trash": "ไฟล์ต้นฉบับอยู่ในถังขยะ ผลลัพธ์ยังใช้ชื่อของตัวเอง", diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.json index 999ce28b5..d0b24a144 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -203,6 +203,12 @@ "notice.cmyk_needs_ghostscript": "Bu dosya CMYK ama RGB olarak kaydedildi: CMYK'yı korumak için Ayarlar → Dönüştürme motorları'ndan Ghostscript'i yükleyin", "notice.pdfa_needs_ghostscript": "Bu PDF, PDF/A değil: PDF/A oluşturmak için Ayarlar → Dönüştürme motorları'ndan Ghostscript'i yükleyin", "notice.pdfa_form_fields": "PDF/A oluşturuldu: form alanları artık doldurulamaz", + "pdfa.reason.font_not_embedded": "kullanılan bir yazı tipi gömülü değil", + "pdfa.reason.cmyk": "CMYK renklerinin dönüştürülmesi gerekiyor", + "pdfa.reason.no_appearance": "bir açıklamanın ya da form alanının kendi görünümü yok", + "pdfa.reason.unsupported": "PDF/A'nın izin vermediği ve Filee'nin çıkaramadığı içerik var", + "pdfa.reason.unreadable": "Filee dosyayı okuyamadı", + "pdfa.reason.locked": "parolayla korunuyor", "notice.cmyk_render_failed": "Bu dosya CMYK ama Ghostscript onu çizemedi, bu yüzden RGB olarak kaydedildi", "notice.original_kept": "Özgün dosya korundu, sonuç kopya olarak kaydedildi", "notice.original_in_trash": "Özgün dosya Geri Dönüşüm Kutusu'nda / Çöp Sepeti'nde; sonuç kendi adını korudu", diff --git a/src/Filee.App/Assets/i18n/vi.json b/src/Filee.App/Assets/i18n/vi.json index 360b5f07d..3aa21bb42 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "Tệp này là CMYK nhưng đã được lưu dưới dạng RGB: hãy cài Ghostscript trong Cài đặt → Bộ xử lý để giữ CMYK", "notice.pdfa_needs_ghostscript": "Tệp PDF này không phải PDF/A: hãy cài Ghostscript trong Cài đặt → Bộ xử lý để tạo PDF/A", "notice.pdfa_form_fields": "Đã tạo PDF/A: không thể điền các trường biểu mẫu nữa", + "pdfa.reason.font_not_embedded": "một phông chữ được dùng chưa được nhúng", + "pdfa.reason.cmyk": "cần chuyển đổi màu CMYK của tệp", + "pdfa.reason.no_appearance": "một chú thích hoặc trường biểu mẫu không có hình thức riêng", + "pdfa.reason.unsupported": "tệp có nội dung PDF/A không cho phép mà Filee không thể gỡ bỏ", + "pdfa.reason.unreadable": "Filee không đọc được tệp", + "pdfa.reason.locked": "tệp được khóa bằng mật khẩu", "notice.cmyk_render_failed": "Tệp này là CMYK, nhưng Ghostscript không vẽ được nên đã lưu dưới dạng RGB", "notice.original_kept": "Bản gốc được giữ lại, kết quả được lưu thành bản sao", "notice.original_in_trash": "Bản gốc đã ở trong Thùng rác, kết quả giữ tên riêng của nó", diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json index ecfbb5bd7..6f301d092 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "这是 CMYK 文件,但保存成了 RGB:要保留 CMYK,请在 设置 → 转换引擎 中安装 Ghostscript", "notice.pdfa_needs_ghostscript": "这个 PDF 不是 PDF/A:要生成 PDF/A,请在 设置 → 转换引擎 中安装 Ghostscript", "notice.pdfa_form_fields": "已生成 PDF/A:表单字段不能再填写", + "pdfa.reason.font_not_embedded": "使用的字体没有嵌入", + "pdfa.reason.cmyk": "需要转换其中的 CMYK 颜色", + "pdfa.reason.no_appearance": "某个注释或表单域没有自己的外观", + "pdfa.reason.unsupported": "其中有 PDF/A 不允许、Filee 又无法去掉的内容", + "pdfa.reason.unreadable": "Filee 无法读取该文件", + "pdfa.reason.locked": "该文件受密码保护", "notice.cmyk_render_failed": "这是 CMYK 文件,但 Ghostscript 无法绘制,所以保存成了 RGB", "notice.original_kept": "已保留原文件,结果另存为副本", "notice.original_in_trash": "原文件已在回收站/废纸篓中,结果保留了自己的名称", diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.json index d53a7df1e..3ffd7429f 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -209,6 +209,12 @@ "notice.cmyk_needs_ghostscript": "這是 CMYK 檔案,但儲存成了 RGB:要保留 CMYK,請在「設定 → 轉換引擎」中安裝 Ghostscript", "notice.pdfa_needs_ghostscript": "這個 PDF 不是 PDF/A:要產生 PDF/A,請在「設定 → 轉換引擎」中安裝 Ghostscript", "notice.pdfa_form_fields": "已產生 PDF/A:表單欄位無法再填寫", + "pdfa.reason.font_not_embedded": "使用的字型沒有內嵌", + "pdfa.reason.cmyk": "需要轉換其中的 CMYK 色彩", + "pdfa.reason.no_appearance": "某個註解或表單欄位沒有自己的外觀", + "pdfa.reason.unsupported": "其中有 PDF/A 不允許、Filee 又無法移除的內容", + "pdfa.reason.unreadable": "Filee 無法讀取這個檔案", + "pdfa.reason.locked": "這個檔案受密碼保護", "notice.cmyk_render_failed": "這是 CMYK 檔案,但 Ghostscript 無法繪製,所以儲存成了 RGB", "notice.original_kept": "已保留原始檔案,結果另存為副本", "notice.original_in_trash": "原始檔案已在資源回收筒/垃圾桶中,結果保留了自己的名稱", diff --git a/src/Filee.App/Services/PresetAvailability.cs b/src/Filee.App/Services/PresetAvailability.cs index 4558bd8d7..8133a62ee 100644 --- a/src/Filee.App/Services/PresetAvailability.cs +++ b/src/Filee.App/Services/PresetAvailability.cs @@ -314,9 +314,10 @@ bool Joinable(RoutePlanner planner) => formats.Any(f => preset.Media.Slideshow : new(PresetUse.Unusable, _loc["error.no_route"]); } - // PDF/A of PDFs is Ghostscript's (the job queue's IPdfArchiver): without it they would only be copied. Other files - // may get it from LibreOffice, or are converted with a notice that the PDF isn't PDF/A. - static bool Archives(RoutePlanner planner) => planner.Includes("ghostscript"); + // PDF/A of PDFs is the job queue's IPdfArchiver: the PDF engine's own, with Ghostscript for what that one can't + // make conform. With neither they would only be copied. Other files may get it from their engine (the Filee + // renderer, LibreOffice), or are converted with a notice that the PDF isn't PDF/A. + static bool Archives(RoutePlanner planner) => planner.Includes("pdfsharp") || planner.Includes("ghostscript"); if (preset.TargetFormat == "pdf" && preset.Document.PdfA && formats.All(f => f.Id == "pdf") && !Archives(installedPlanner())) { return packageEnabling(Archives) is { } archiver diff --git a/src/Filee.Core/Conversion/IConverter.cs b/src/Filee.Core/Conversion/IConverter.cs index 30f3c877b..2049abc72 100644 --- a/src/Filee.Core/Conversion/IConverter.cs +++ b/src/Filee.Core/Conversion/IConverter.cs @@ -334,9 +334,10 @@ public interface IPdfFinisher } /// -/// Makes a finished PDF a PDF/A (). Implemented by the Ghostscript engine; -/// runs it after the finishing on every PDF a job writes, whichever engine made it, and on a -/// merged PDF once, so the box means the same for every route. +/// Makes a finished PDF a PDF/A (). Implemented by the PDF engine (built in) +/// and the Ghostscript engine; runs them after the finishing on every PDF a job writes, +/// whichever engine made it, and on a merged PDF once, so the box means the same for every route. They are tried in +/// engine order until one makes the PDF PDF/A. /// public interface IPdfArchiver { @@ -346,6 +347,8 @@ public interface IPdfArchiver /// /// What it did; when it can't (Ghostscript isn't installed): /// the PDF is left as it is. + /// This PDF can't be made to conform here (its error key says why); + /// the PDF is left as it is and the next archiver gets it. Task ArchiveAsync(string path, bool changed, string workDirectory, CancellationToken cancellationToken); } diff --git a/src/Filee.Core/Conversion/JobQueue.cs b/src/Filee.Core/Conversion/JobQueue.cs index 78321a207..d7da9750e 100644 --- a/src/Filee.Core/Conversion/JobQueue.cs +++ b/src/Filee.Core/Conversion/JobQueue.cs @@ -274,8 +274,10 @@ private async Task FinishPdfsAsync(Preset preset, IReadOnlyList outputs, /// /// Makes the PDFs among PDF/A when the preset asks for it (), - /// after the finishing, which would undo it. Without Ghostscript they stay as they are and the file says so, rather - /// than pass for PDF/A. Otherwise the file says when form fields were made page content, which can't be filled in. + /// after the finishing, which would undo it. The archivers are tried in engine order (the built-in one before + /// Ghostscript): one that refuses a PDF hands it to the next. A PDF none made PDF/A stays as it is and the file says + /// so, with why the built-in one refused it, rather than pass for PDF/A. Otherwise the file says when form fields + /// were made page content, which can't be filled in. /// /// Rewritten since the engine wrote it: a PDF/A from LibreOffice no longer is one. private async Task ArchivePdfsAsync(Preset preset, IReadOnlyList outputs, FileResult file, string workDir, bool changed, @@ -283,18 +285,42 @@ private async Task ArchivePdfsAsync(Preset preset, IReadOnlyList outputs { if (!preset.Document.PdfA) return; - var archiver = _catalog.All.OfType().FirstOrDefault(); + var archivers = _catalog.Ordered.OfType().ToList(); var notArchived = false; + string? refusal = null; var withoutFormFields = false; foreach (var pdf in outputs.Where(o => FormatRegistry.Detect(o)?.Id == "pdf")) { - var outcome = archiver is null ? PdfArchiveOutcome.NotArchived : await archiver.ArchiveAsync(pdf, changed, workDir, ct); - notArchived |= outcome == PdfArchiveOutcome.NotArchived; + var outcome = PdfArchiveOutcome.NotArchived; + string? why = null; + foreach (var archiver in archivers) + { + try + { + outcome = await archiver.ArchiveAsync(pdf, changed, workDir, ct); + } + catch (ConversionRefusedException refused) + { + why ??= refused.ErrorKey; + continue; + } + if (outcome != PdfArchiveOutcome.NotArchived) + break; + } + if (outcome == PdfArchiveOutcome.NotArchived) + { + notArchived = true; + refusal ??= why; + } withoutFormFields |= outcome == PdfArchiveOutcome.ArchivedWithoutFormFields; } // A PDF that didn't become PDF/A matters more than fields that can't be filled in any more. if (notArchived) + { file.NoticeKey = "notice.pdfa_needs_ghostscript"; + file.NoticeReasonKey = refusal; + file.NoticeDetail = null; + } else if (withoutFormFields) { file.NoticeKey = "notice.pdfa_form_fields"; diff --git a/src/Filee.Engines/Drawings/DrawingConverter.cs b/src/Filee.Engines/Drawings/DrawingConverter.cs index 2f1746214..707c0c37a 100644 --- a/src/Filee.Engines/Drawings/DrawingConverter.cs +++ b/src/Filee.Engines/Drawings/DrawingConverter.cs @@ -77,7 +77,8 @@ public Task> ConvertAsync(ConversionStep step, IProgress(p => progress.Report(0.2 + 0.8 * p)), cancellationToken); + var pdf = SlideOutput.ToPdf(deck, progress is null ? null : new Progress(p => progress.Report(0.2 + 0.8 * p)), cancellationToken, + step.Preset.Document.PdfA); if (step.Output.Allocate("pdf") is not { } target) return []; File.WriteAllBytes(target, pdf); @@ -106,7 +107,7 @@ private static List WriteFixed(ConversionStep step, IFixedPages document return []; using (var output = File.Create(target)) { - PdfPageWriter.Write(output, all, new PdfDocumentInfo { Title = document.Title }, + PdfPageWriter.Write(output, all, new PdfDocumentInfo { Title = document.Title, PdfA = step.Preset.Document.PdfA }, progress is null ? null : new Progress(p => progress.Report(0.5 + 0.5 * p)), cancellationToken); } return [target]; diff --git a/src/Filee.Engines/Office/SheetPrintConverter.cs b/src/Filee.Engines/Office/SheetPrintConverter.cs index 03768498f..891bbad9b 100644 --- a/src/Filee.Engines/Office/SheetPrintConverter.cs +++ b/src/Filee.Engines/Office/SheetPrintConverter.cs @@ -70,7 +70,8 @@ public async Task> ConvertAsync(ConversionStep step, IProg cancellationToken.ThrowIfCancellationRequested(); progress?.Report(0.2); var pages = progress is null ? null : new Progress(p => progress.Report(0.2 + 0.8 * p)); - var pdf = SheetPrinter.ToPdf(book, SheetPrintSource.Of(step.OriginalSourcePath ?? step.InputPath), cancellationToken, pages); + var pdf = SheetPrinter.ToPdf(book, SheetPrintSource.Of(step.OriginalSourcePath ?? step.InputPath), cancellationToken, pages, + pdfA: step.Preset.Document.PdfA); var target = step.Output.Allocate("pdf"); if (target is null) return []; diff --git a/src/Filee.Engines/Office/Sheets/Print/SheetPrinter.cs b/src/Filee.Engines/Office/Sheets/Print/SheetPrinter.cs index a7e84b978..59838ec85 100644 --- a/src/Filee.Engines/Office/Sheets/Print/SheetPrinter.cs +++ b/src/Filee.Engines/Office/Sheets/Print/SheetPrinter.cs @@ -25,13 +25,14 @@ internal static class SheetPrinter /// The workbook as a PDF. /// Pages drawn and written, as a fraction. /// The fonts to draw with; the installed ones when null. + /// Write it as PDF/A-2b. public static byte[] ToPdf(Workbook book, SheetPrintSource source, CancellationToken cancellationToken = default, - IProgress? progress = null, DocumentLimits? limits = null, DateTime? now = null, FontResolver? fonts = null) + IProgress? progress = null, DocumentLimits? limits = null, DateTime? now = null, FontResolver? fonts = null, bool pdfA = false) { using var printed = Print(book, source, limits ?? DocumentLimits.Default, now ?? DateTime.Now, progress is null ? null : new Progress(p => progress.Report(p * 0.5)), cancellationToken, fonts); using var output = new MemoryStream(); - PdfPageWriter.Write(output, printed.Pages, new PdfDocumentInfo { Outline = printed.Outline }, + PdfPageWriter.Write(output, printed.Pages, new PdfDocumentInfo { Outline = printed.Outline, PdfA = pdfA }, progress is null ? null : new Progress(p => progress.Report(0.5 + p * 0.5)), cancellationToken); return output.ToArray(); } diff --git a/src/Filee.Engines/Pdf/PdfAConformance.cs b/src/Filee.Engines/Pdf/PdfAConformance.cs new file mode 100644 index 000000000..3aba25d52 --- /dev/null +++ b/src/Filee.Engines/Pdf/PdfAConformance.cs @@ -0,0 +1,856 @@ +// PDF/A-2b (ISO 19005-2, level B) without Ghostscript: a PDF opened with PDFsharp is changed in place to keep the rules +// below, then saved by PdfAMetadata, which gives it the XMP and document information PDF/A asks for. Used for every +// PDF the job queue makes PDF/A (PdfSharpConverter's IPdfArchiver, before Ghostscript in the engine order) and by the +// Filee renderer's page writer, which writes its PDFs as PDF/A directly when the preset asks for it. +// +// What is changed (clause of ISO 19005-2): +// - 6.1.3/6.1.7: no encryption, no streams kept in external files, no LZW (such streams are unpacked and deflated). +// - 6.2.2-6.2.4: one GTS_PDFA1 output intent. The sRGB profile made in code (SrgbProfile) unless the PDF already has +// a PDF/A intent of the device colours it uses; a PDF drawn in CMYK keeps a CMYK intent it brings. +// - 6.2.5: graphics states lose transfer functions (TR, TR2 other than Default), halftone overrides and unknown +// rendering intents and blend modes. 6.2.8/6.2.9: pictures lose alternates, OPI and interpolation; forms lose OPI, +// PostScript and their reference to another PDF (their own content stays). +// - 6.3: annotations of types PDF/A forbids (3D, sound, screen, movie, rich media, file attachments for level B) and +// hidden ones are left out; every other one prints and keeps only its normal appearance, as the rules want. +// - 6.4: forms lose XFA, NeedAppearances and the actions of fields and widgets; 6.5: JavaScript, launch and the other forbidden actions, and every +// additional-actions entry, are removed (links, go-to and URI actions stay); 6.8: embedded files go (level B). +// - 6.9: optional content configurations get unique names, no AS entry and an order listing every group. +// - Other XMP packets (pages, pictures) go: the document's own is written new by PdfAMetadata. +// +// What makes it give up (ConversionRefusedException with a pdfa.reason.* key; the job queue then tries Ghostscript, +// and without it the file says why it isn't PDF/A): a font drawn with that isn't embedded, DeviceCMYK without a CMYK +// intent (or CMYK and RGB device colours together), an annotation or field that has no appearance of its own, +// XFA-only forms, PostScript, external stream data, LZW mixed with filters PDFsharp can't unpack, inline pictures +// with LZW or interpolation, and content it can't read. A document past the walk's limits is refused as too complex. +// +// Not checked (docs/ENGINES.md lists them): that embedded fonts hold every glyph drawn and their widths match, the +// symbolic/non-symbolic TrueType encodings, ICC profiles of the document's own ICCBased colour spaces, JPEG 2000 +// details and the implementation limits of 6.1.13 (string, name and number sizes, q nesting). + +using Filee.Core.Conversion; +using PdfSharp.Pdf; +using PdfSharp.Pdf.Advanced; +using Name = Filee.Engines.Pdf.PdfContentLexer.Name; +using Operator = Filee.Engines.Pdf.PdfContentLexer.Operator; + +namespace Filee.Engines.Pdf; + +/// Makes a PDF opened with PDFsharp keep the rules of PDF/A-2b. +internal static class PdfAConformance +{ + /// Why a PDF can't become PDF/A: a font it draws with isn't embedded. + public const string FontNotEmbedded = "pdfa.reason.font_not_embedded"; + + /// Its colours are CMYK without a CMYK profile, or device CMYK and RGB together. + public const string DeviceCmyk = "pdfa.reason.cmyk"; + + /// An annotation or form field has no appearance of its own. + public const string NoAppearance = "pdfa.reason.no_appearance"; + + /// It holds something PDF/A forbids that Filee can't take out (XFA-only forms, PostScript, external data). + public const string Unsupported = "pdfa.reason.unsupported"; + + /// Filee couldn't read it. + public const string Unreadable = "pdfa.reason.unreadable"; + + /// It is locked with a password. + public const string Locked = "pdfa.reason.locked"; + + /// Objects walked at most; a real document has some ten thousand. + internal const int MaxObjects = 2_000_000; + + /// Fields, outline items and actions in a chain followed at most. + private const int MaxTreeNodes = 100_000; + + private const int MaxActionChain = 32; + + private const int Invisible = 1; + private const int Hidden = 2; + private const int Print = 4; + private const int NoView = 32; + private const int ToggleNoView = 256; + private const int HiddenFlags = Invisible | Hidden | NoView | ToggleNoView; + + /// Annotation types of ISO 32000-1 that PDF/A-2b keeps (6.3.1); file attachments go with embedded files. + private static readonly HashSet Annotations = new(StringComparer.Ordinal) + { + "/Text", "/Link", "/FreeText", "/Line", "/Square", "/Circle", "/Polygon", "/PolyLine", "/Highlight", "/Underline", + "/Squiggly", "/StrikeOut", "/Stamp", "/Caret", "/Ink", "/Popup", "/Widget", "/PrinterMark", "/TrapNet", + "/Watermark", "/Redact", + }; + + /// Actions PDF/A-2 allows (6.5.1); named actions only for these four pages. + private static readonly HashSet Actions = new(StringComparer.Ordinal) + { + "/GoTo", "/GoToR", "/GoToE", "/Thread", "/URI", "/SubmitForm", "/Named", + }; + + private static readonly HashSet NamedActions = new(StringComparer.Ordinal) { "/NextPage", "/PrevPage", "/FirstPage", "/LastPage" }; + + private static readonly HashSet BlendModes = new(StringComparer.Ordinal) + { + "/Normal", "/Compatible", "/Multiply", "/Screen", "/Overlay", "/Darken", "/Lighten", "/ColorDodge", "/ColorBurn", + "/HardLight", "/SoftLight", "/Difference", "/Exclusion", "/Hue", "/Saturation", "/Color", "/Luminosity", + }; + + private static readonly HashSet RenderingIntents = new(StringComparer.Ordinal) + { + "/RelativeColorimetric", "/AbsoluteColorimetric", "/Perceptual", "/Saturation", + }; + + private static ConversionRefusedException Refuse(string reason) => new(reason); + + /// + /// Changes to keep the rules of PDF/A-2b and gives it its output intent. Save it with + /// , which writes the XMP and document information to match. + /// + /// It can't keep them (a pdfa.reason.* key, or + /// error.document_too_complex). The document may be changed in part then. + public static void Prepare(PdfDocument document, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + var catalog = document.Internals.Catalog; + var intents = ExistingIntents(catalog); + catalog.Elements.Remove("/OutputIntents"); + catalog.Elements.Remove("/Metadata"); + CleanCatalog(catalog); + CleanForm(catalog); + CleanOutline(catalog); + CleanOptionalContent(catalog); + foreach (var page in document.Pages) + { + cancellationToken.ThrowIfCancellationRequested(); + CleanPage(document, page); + } + + var walk = Walk(catalog, cancellationToken); + foreach (var dictionary in walk.Dictionaries) + CleanObject(dictionary); + foreach (var state in walk.GraphicsStates) + CleanGraphicsState(state); + foreach (var font in walk.Fonts) + CheckFont(font); + + var colours = new Colours(); + colours.ReadNames(walk); + colours.ReadContent(walk.ContentStreams, cancellationToken); + catalog.Elements["/OutputIntents"] = OutputIntents(document, intents, colours); + + if (document.SecuritySettings.IsEncrypted) + document.SecurityHandler.SetEncryptionToNoneAndResetPasswords(); + // PDF/A-2 is based on PDF 1.7 (6.1.2); a PDF 2.0 file is written as 1.7. + if (document.Version > 17) + document.Version = 17; + } + + private static PdfItem? Resolve(PdfItem? item) => PdfCompressor.Resolve(item); + + private static string Name(PdfDictionary dictionary, string key) => Resolve(dictionary.Elements[key]) is PdfName name ? name.Value : ""; + + // ---- the catalog, forms, outline and optional content + + private static void CleanCatalog(PdfDictionary catalog) + { + if (Resolve(catalog.Elements["/NeedsRendering"]) is PdfBoolean { Value: true } or PdfBooleanObject { Value: true }) + throw Refuse(Unsupported); // an XFA form only: nothing to show without the XFA + foreach (var key in new[] { "/AA", "/NeedsRendering", "/Requirements", "/AF", "/Collection" }) + catalog.Elements.Remove(key); + if (Resolve(catalog.Elements["/Names"]) is PdfDictionary names) + { + foreach (var key in new[] { "/JavaScript", "/EmbeddedFiles", "/AlternatePresentations" }) + names.Elements.Remove(key); + } + if (Resolve(catalog.Elements["/OpenAction"]) is PdfDictionary { Stream: null } action && !CleanAction(action)) + catalog.Elements.Remove("/OpenAction"); + if (Resolve(catalog.Elements["/Perms"]) is PdfDictionary permissions) + { + foreach (var key in permissions.Elements.Keys.Where(k => k is not "/UR3" and not "/DocMDP").ToList()) + permissions.Elements.Remove(key); + } + if (Resolve(catalog.Elements["/Version"]) is PdfName { Value: not ("/1.0" or "/1.1" or "/1.2" or "/1.3" or "/1.4" or "/1.5" or "/1.6" or "/1.7") }) + catalog.Elements.Remove("/Version"); + } + + /// + /// Whether may stay; actions chained after it (/Next) that may not are taken out of + /// the chain. + /// + private static bool CleanAction(PdfDictionary action, int depth = 0) + { + var type = Name(action, "/S"); + if (!Actions.Contains(type) || (type == "/Named" && !NamedActions.Contains(Name(action, "/N")))) + return false; + switch (Resolve(action.Elements["/Next"])) + { + case PdfDictionary next when depth >= MaxActionChain || !CleanAction(next, depth + 1): + action.Elements.Remove("/Next"); + break; + case PdfArray chain: + for (var i = chain.Elements.Count - 1; i >= 0; i--) + { + if (depth >= MaxActionChain || Resolve(chain.Elements[i]) is not PdfDictionary next || !CleanAction(next, depth + 1)) + chain.Elements.RemoveAt(i); + } + if (chain.Elements.Count == 0) + action.Elements.Remove("/Next"); + break; + } + return true; + } + + /// The entry's action, removed when PDF/A forbids it. + private static void CleanActionOf(PdfDictionary dictionary) + { + dictionary.Elements.Remove("/AA"); + if (Resolve(dictionary.Elements["/A"]) is PdfDictionary { Stream: null } action && !CleanAction(action)) + dictionary.Elements.Remove("/A"); + } + + private static void CleanForm(PdfDictionary catalog) + { + if (Resolve(catalog.Elements["/AcroForm"]) is not PdfDictionary form) + return; + form.Elements.Remove("/XFA"); + form.Elements.Remove("/NeedAppearances"); + // 6.4.1: fields and their widgets have no actions at all. + foreach (var field in Tree(form.Elements["/Fields"], "/Kids")) + { + field.Elements.Remove("/A"); + field.Elements.Remove("/AA"); + } + } + + private static void CleanOutline(PdfDictionary catalog) + { + if (Resolve(catalog.Elements["/Outlines"]) is not PdfDictionary outlines) + return; + var pending = new Stack([outlines.Elements["/First"]]); + var seen = new HashSet(ReferenceEqualityComparer.Instance); + while (pending.TryPop(out var next) && seen.Count < MaxTreeNodes) + { + if (Resolve(next) is not PdfDictionary item || !seen.Add(item)) + continue; + CleanActionOf(item); + pending.Push(item.Elements["/Next"]); + pending.Push(item.Elements["/First"]); + } + } + + /// The dictionaries of a tree of arrays (form fields and their kids), each once, within the limits. + private static List Tree(PdfItem? root, string children) + { + var found = new List(); + var seen = new HashSet(ReferenceEqualityComparer.Instance); + var pending = new Stack([root]); + while (pending.TryPop(out var item) && found.Count < MaxTreeNodes) + { + switch (Resolve(item)) + { + case PdfArray array: + foreach (var element in array.Elements) + pending.Push(element); + break; + case PdfDictionary dictionary when seen.Add(dictionary): + found.Add(dictionary); + pending.Push(dictionary.Elements[children]); + break; + } + } + return found; + } + + /// 6.9: every configuration named, uniquely, without AS, its order listing every group. + private static void CleanOptionalContent(PdfDictionary catalog) + { + if (Resolve(catalog.Elements["/OCProperties"]) is not PdfDictionary properties) + return; + var configurations = new List(); + if (Resolve(properties.Elements["/D"]) is PdfDictionary standard) + configurations.Add(standard); + if (Resolve(properties.Elements["/Configs"]) is PdfArray others) + configurations.AddRange(others.Elements.Select(Resolve).OfType()); + var groups = Resolve(properties.Elements["/OCGs"]) is PdfArray all + ? all.Elements.Where(e => Resolve(e) is PdfDictionary).ToList() + : []; + var names = new HashSet(StringComparer.Ordinal); + var number = 0; + foreach (var configuration in configurations.Distinct(ReferenceEqualityComparer.Instance).Cast()) + { + configuration.Elements.Remove("/AS"); + var name = configuration.Elements.GetString("/Name"); + if (name.Length == 0 || !names.Add(name)) + { + do + name = $"Configuration {++number}"; + while (!names.Add(name)); + configuration.Elements.SetString("/Name", name); + } + if (Resolve(configuration.Elements["/Order"]) is not PdfArray order) + continue; + var listed = new HashSet(ReferenceEqualityComparer.Instance); + foreach (var group in Tree(order, "/None")) + listed.Add(group); + foreach (var group in groups.Where(g => !listed.Contains((PdfDictionary)Resolve(g)!))) + order.Elements.Add(group); + } + } + + // ---- pages and annotations + + private static void CleanPage(PdfDocument document, PdfPage page) + { + page.Elements.Remove("/AA"); + page.Elements.Remove("/PresSteps"); + if (Resolve(page.Elements["/Annots"]) is not PdfArray annotations) + return; + for (var i = annotations.Elements.Count - 1; i >= 0; i--) + { + if (Resolve(annotations.Elements[i]) is not PdfDictionary annotation || !KeepAnnotation(document, annotation)) + annotations.Elements.RemoveAt(i); + } + } + + /// Whether the annotation may stay; it is made to keep the rules when it does. + private static bool KeepAnnotation(PdfDocument document, PdfDictionary annotation) + { + var subtype = Name(annotation, "/Subtype"); + if (!Annotations.Contains(subtype)) + return false; + CleanActionOf(annotation); + if (subtype == "/Widget") + annotation.Elements.Remove("/A"); // 6.4.1 + var flags = Resolve(annotation.Elements["/F"]) switch + { + null => 0, + PdfInteger value => value.Value, + PdfIntegerObject value => value.Value, + _ => -1, + }; + if (subtype == "/Popup") + { + // A popup needs no flags; ones it may not have go (6.3.2), and it opens with its parent as before. + if (flags < 0 || (flags & HiddenFlags) != 0) + annotation.Elements.Remove("/F"); + else if (annotation.Elements.ContainsKey("/F")) + annotation.Elements["/F"] = new PdfInteger(flags | Print); + annotation.Elements.Remove("/AP"); + return true; + } + if (flags < 0) + flags = 0; + if ((flags & HiddenFlags) != 0) + return false; // never shown: PDF/A has no hidden annotations + annotation.Elements["/F"] = new PdfInteger(flags | Print); + CleanAppearance(document, annotation, subtype); + return true; + } + + /// 6.3.3: an appearance dictionary with only the normal appearance, a stream, or for buttons a set of states. + private static void CleanAppearance(PdfDocument document, PdfDictionary annotation, string subtype) + { + var exempt = subtype == "/Link" || HasNoArea(annotation); + if (Resolve(annotation.Elements["/AP"]) is not PdfDictionary appearance || Resolve(appearance.Elements["/N"]) is not PdfDictionary normal) + { + if (!exempt) + throw Refuse(NoAppearance); + annotation.Elements.Remove("/AP"); + return; + } + foreach (var key in appearance.Elements.Keys.Where(k => k != "/N").ToList()) + appearance.Elements.Remove(key); + var button = subtype == "/Widget" && FieldType(annotation) == "/Btn"; + var state = Name(annotation, "/AS"); + if (normal.Stream is not null && button) + { + // A button's appearance is a set of states: the one it has becomes its only state. + if (state.Length == 0) + state = "/Off"; + var states = new PdfDictionary(document); + states.Elements[state] = appearance.Elements["/N"]; + appearance.Elements["/N"] = states; + annotation.Elements.SetName("/AS", state); + } + else if (normal.Stream is null && !button) + { + // States outside a button: the one it shows becomes its appearance. + if (state.Length > 0 && Resolve(normal.Elements[state]) is PdfDictionary { Stream: not null }) + appearance.Elements["/N"] = normal.Elements[state]; + else if (exempt) + annotation.Elements.Remove("/AP"); + else + throw Refuse(NoAppearance); + } + } + + private static bool HasNoArea(PdfDictionary annotation) + { + if (Resolve(annotation.Elements["/Rect"]) is not PdfArray { Elements.Count: 4 } rect) + return true; + double At(int i) => Resolve(rect.Elements[i]) switch + { + PdfInteger value => value.Value, + PdfReal value => value.Value, + PdfIntegerObject value => value.Value, + PdfRealObject value => value.Value, + _ => 0, + }; + return At(0) == At(2) || At(1) == At(3); + } + + /// A widget's field type, from it or the fields it is a kid of. + private static string FieldType(PdfDictionary widget) + { + PdfDictionary? field = widget; + for (var depth = 0; field is not null && depth < 64; depth++) + { + if (Name(field, "/FT") is { Length: > 0 } type) + return type; + field = Resolve(field.Elements["/Parent"]) as PdfDictionary; + } + return ""; + } + + // ---- every object + + /// What the walk from the catalog reached. + private sealed class Reached + { + public List Dictionaries { get; } = []; + public List Arrays { get; } = []; + public HashSet Fonts { get; } = new(ReferenceEqualityComparer.Instance); + public HashSet GraphicsStates { get; } = new(ReferenceEqualityComparer.Instance); + public HashSet ContentStreams { get; } = new(ReferenceEqualityComparer.Instance); + } + + /// + /// Every dictionary and array the catalog leads to, each once (from a work stack, not by recursion), and among + /// them the fonts and graphics states resources list and the content streams. + /// + private static Reached Walk(PdfDictionary catalog, CancellationToken cancellationToken) + { + var reached = new Reached(); + var seen = new HashSet(ReferenceEqualityComparer.Instance); + var pending = new Stack([catalog]); + while (pending.TryPop(out var item)) + { + switch (Resolve(item)) + { + case PdfDictionary dictionary when seen.Add(dictionary): + reached.Dictionaries.Add(dictionary); + foreach (var (_, value) in dictionary.Elements) + pending.Push(value); + break; + case PdfArray array when seen.Add(array): + reached.Arrays.Add(array); + foreach (var value in array.Elements) + pending.Push(value); + break; + default: + continue; + } + if (seen.Count > MaxObjects) + throw new ConversionRefusedException("error.document_too_complex"); + if (seen.Count % 4096 == 0) + cancellationToken.ThrowIfCancellationRequested(); + } + + foreach (var dictionary in reached.Dictionaries) + { + // AcroForm's /DR lists fonts for filling in fields, not for drawing: only /Resources count. + if (Resolve(dictionary.Elements["/Resources"]) is PdfDictionary resources) + { + Add(reached.Fonts, resources.Elements["/Font"]); + Add(reached.GraphicsStates, resources.Elements["/ExtGState"]); + } + if (dictionary.Stream is not null && (Name(dictionary, "/Subtype") == "/Form" || IsTilingPattern(dictionary))) + reached.ContentStreams.Add(dictionary); + if (Name(dictionary, "/Type") == "/Page") + AddStreams(reached.ContentStreams, dictionary.Elements["/Contents"]); + if (Name(dictionary, "/Subtype") == "/Type3" && Resolve(dictionary.Elements["/CharProcs"]) is PdfDictionary glyphs) + AddStreams(reached.ContentStreams, glyphs); + if (Resolve(dictionary.Elements["/AP"]) is PdfDictionary appearance) + AddStreams(reached.ContentStreams, appearance.Elements["/N"]); + } + return reached; + + static void Add(HashSet set, PdfItem? listing) + { + if (Resolve(listing) is PdfDictionary entries) + { + foreach (var (_, value) in entries.Elements) + { + if (Resolve(value) is PdfDictionary entry) + set.Add(entry); + } + } + } + } + + private static bool IsTilingPattern(PdfDictionary dictionary) => + Resolve(dictionary.Elements["/PatternType"]) is PdfInteger { Value: 1 } or PdfIntegerObject { Value: 1 }; + + /// A stream, an array of streams or a dictionary of them (states, Type 3 glyphs). + private static void AddStreams(HashSet set, PdfItem? item) + { + switch (Resolve(item)) + { + case PdfDictionary { Stream: not null } stream: + set.Add(stream); + break; + case PdfDictionary states: + foreach (var (_, value) in states.Elements) + { + if (Resolve(value) is PdfDictionary { Stream: not null } state) + set.Add(state); + } + break; + case PdfArray parts: + foreach (var part in parts.Elements) + { + if (Resolve(part) is PdfDictionary { Stream: not null } stream) + set.Add(stream); + } + break; + } + } + + /// Streams, pictures, forms and XMP packets of one object. + private static void CleanObject(PdfDictionary dictionary) + { + // Only the document has an XMP packet (written new on saving). + dictionary.Elements.Remove("/Metadata"); + var subtype = Name(dictionary, "/Subtype"); + if (dictionary.Stream is { } stream) + { + if (dictionary.Elements.ContainsKey("/F") || dictionary.Elements.ContainsKey("/FFilter") || dictionary.Elements.ContainsKey("/FDecodeParms")) + throw Refuse(Unsupported); // data in another file (6.1.7.1) + var filters = Filters(dictionary); + if (filters.Contains("/Crypt")) + throw Refuse(Unsupported); + if (filters.Contains("/LZWDecode")) + { + // 6.1.7.2: no LZW. Unpacked (with the filters around it, when PDFsharp knows them all) and deflated. + if (!filters.All(UnpackedFilters.Contains)) + throw Refuse(Unsupported); + var data = stream.UnfilteredValue; + dictionary.Elements.Remove("/Filter"); + dictionary.Elements.Remove("/DecodeParms"); + stream.Value = data; + stream.Zip(); + } + if (subtype == "/PS") + throw Refuse(Unsupported); // a PostScript XObject (6.2.9) + } + switch (subtype) + { + case "/Image": + dictionary.Elements.Remove("/Alternates"); + dictionary.Elements.Remove("/OPI"); + dictionary.Elements.Remove("/Interpolate"); + break; + case "/Form": + if (Name(dictionary, "/Subtype2") == "/PS") + throw Refuse(Unsupported); + dictionary.Elements.Remove("/OPI"); + dictionary.Elements.Remove("/PS"); + dictionary.Elements.Remove("/Ref"); // the form's own content is the stand-in + break; + } + } + + /// Filters PDFsharp unpacks, predictors and all. + private static readonly HashSet UnpackedFilters = new(StringComparer.Ordinal) + { + "/LZWDecode", "/FlateDecode", "/ASCII85Decode", "/ASCIIHexDecode", + }; + + private static string[] Filters(PdfDictionary dictionary) => Resolve(dictionary.Elements["/Filter"]) switch + { + PdfName name => [name.Value], + PdfArray array => [.. array.Elements.Select(e => Resolve(e) is PdfName n ? n.Value : "?")], + _ => [], + }; + + /// 6.2.5: no transfer functions, halftone overrides, unknown rendering intents or blend modes. + private static void CleanGraphicsState(PdfDictionary state) + { + state.Elements.Remove("/TR"); + if (state.Elements.ContainsKey("/TR2") && Name(state, "/TR2") != "/Default") + state.Elements.SetName("/TR2", "/Default"); + state.Elements.Remove("/HTO"); + state.Elements.Remove("/HT"); // the device's own halftone; it only matters on a printing press + if (state.Elements.ContainsKey("/RI") && !RenderingIntents.Contains(Name(state, "/RI"))) + state.Elements.Remove("/RI"); + switch (Resolve(state.Elements["/BM"])) + { + case null: + break; + case PdfName name when BlendModes.Contains(name.Value): + break; + case PdfArray modes: + // A list: the first one a reader knows is used, so that one stays. + var known = modes.Elements.Select(Resolve).OfType().FirstOrDefault(m => BlendModes.Contains(m.Value))?.Value; + state.Elements.SetName("/BM", known ?? "/Normal"); + break; + default: + state.Elements.SetName("/BM", "/Normal"); + break; + } + } + + /// 6.2.11.4: every font drawn with is embedded (Type 3 fonts are drawings themselves). + private static void CheckFont(PdfDictionary font) + { + switch (Name(font, "/Subtype")) + { + case "/Type3": + return; + case "/Type0": + if (Resolve(font.Elements["/DescendantFonts"]) is not PdfArray descendants + || descendants.Elements.Select(Resolve).OfType().ToList() is not [_, ..] cidFonts) + throw Refuse(FontNotEmbedded); + foreach (var cidFont in cidFonts) + CheckEmbedded(cidFont); + return; + case "/Type1" or "/MMType1" or "/TrueType" or "/CIDFontType0" or "/CIDFontType2": + CheckEmbedded(font); + return; + default: + throw Refuse(FontNotEmbedded); + } + } + + private static void CheckEmbedded(PdfDictionary font) + { + if (Resolve(font.Elements["/FontDescriptor"]) is not PdfDictionary descriptor + || !new[] { "/FontFile", "/FontFile2", "/FontFile3" }.Any(key => Resolve(descriptor.Elements[key]) is PdfDictionary { Stream: not null })) + throw Refuse(FontNotEmbedded); + // 6.2.11.3.2: a CIDFontType2 says how CIDs map to glyphs; Identity is what PDF readers assume without it. + if (Name(font, "/Subtype") == "/CIDFontType2" && !font.Elements.ContainsKey("/CIDToGIDMap")) + font.Elements.SetName("/CIDToGIDMap", "/Identity"); + } + + // ---- colours and the output intent + + /// The device colour spaces a document draws in, from its objects and its content streams. + private sealed class Colours + { + private long _bytes; + private long _tokens; + + public bool Rgb { get; private set; } + public bool Cmyk { get; private set; } + + private void See(string name) + { + switch (name) + { + case "DeviceRGB" or "RGB": + Rgb = true; + break; + case "DeviceCMYK" or "CMYK": + Cmyk = true; + break; + } + } + + /// Colour space names in the objects: pictures, colour spaces of resources, shadings, groups. + public void ReadNames(Reached reached) + { + foreach (var dictionary in reached.Dictionaries) + { + var icc = dictionary.Stream is not null && dictionary.Elements.ContainsKey("/N"); + foreach (var (key, value) in dictionary.Elements) + { + // An ICC profile's alternate is used only when the profile can't be: not a use of its own. + if (key == "/Alternate" && icc) + continue; + if (Resolve(value) is PdfName { Value: "/DeviceRGB" or "/DeviceCMYK" } name) + See(name.Value[1..]); + } + } + foreach (var array in reached.Arrays) + { + foreach (var value in array.Elements) + { + if (Resolve(value) is PdfName { Value: "/DeviceRGB" or "/DeviceCMYK" } name) + See(name.Value[1..]); + } + } + } + + /// Colour operators and colour space names in the content streams, inline pictures' too. + public void ReadContent(IEnumerable streams, CancellationToken cancellationToken) + { + foreach (var stream in streams) + { + cancellationToken.ThrowIfCancellationRequested(); + byte[] data; + try + { + data = stream.Stream!.UnfilteredValue; + } + catch (Exception ex) when (ex is not OperationCanceledException and not ConversionRefusedException) + { + throw Refuse(Unreadable); + } + _bytes += data.Length; + if (_bytes > PdfFontUses.MaxContentBytes) + throw new ConversionRefusedException("error.document_too_complex"); + try + { + Read(new PdfContentLexer(data)); + } + catch (InvalidDataException) + { + throw Refuse(Unreadable); + } + } + } + + private void Read(PdfContentLexer lexer) + { + while (lexer.Next() is { } token) + { + if (++_tokens > PdfFontUses.MaxWork) + throw new ConversionRefusedException("error.document_too_complex"); + switch (token) + { + case Operator { Name: "k" or "K" }: + Cmyk = true; + break; + case Operator { Name: "rg" or "RG" }: + Rgb = true; + break; + case Name { Value: "DeviceRGB" or "DeviceCMYK" } name: + See(name.Value); + break; + case Operator { Name: "BI" }: + InlineImage(lexer.ReadInlineImage(_ => null).Parameters); + break; + } + } + } + + private void InlineImage(Dictionary parameters) + { + foreach (var (key, value) in parameters) + { + switch (key) + { + case "CS" or "ColorSpace": + Names(value, See); + break; + case "F" or "Filter": + Names(value, filter => + { + if (filter is "LZW" or "LZWDecode") + throw Refuse(Unsupported); // content can't be rewritten here + }); + break; + case "I" or "Interpolate" when value is Operator { Name: "true" }: + throw Refuse(Unsupported); + } + } + } + + private static void Names(object? value, Action each) + { + switch (value) + { + case Name name: + each(name.Value); + break; + case List list: + foreach (var item in list) + { + if (item is Name inner) + each(inner.Value); + } + break; + } + } + } + + /// The output intents a PDF brings, with the components of their profiles. + private static List<(PdfDictionary Intent, PdfDictionary Profile, int Components, bool PdfA)> ExistingIntents(PdfDictionary catalog) + { + var found = new List<(PdfDictionary, PdfDictionary, int, bool)>(); + if (Resolve(catalog.Elements["/OutputIntents"]) is not PdfArray intents) + return found; + foreach (var item in intents.Elements) + { + if (Resolve(item) is not PdfDictionary intent || Resolve(intent.Elements["/DestOutputProfile"]) is not PdfDictionary { Stream: not null } profile) + continue; + byte[] bytes; + try + { + bytes = profile.Stream!.UnfilteredValue; + } + catch (Exception ex) when (ex is not OperationCanceledException and not ConversionRefusedException) + { + continue; + } + // 6.2.3: an output or display profile of ICC version 4 at most, of RGB or CMYK. + if (bytes.Length < 128 || bytes[8] > 4 || System.Text.Encoding.ASCII.GetString(bytes, 12, 4) is not ("prtr" or "mntr")) + continue; + var components = System.Text.Encoding.ASCII.GetString(bytes, 16, 4) switch + { + "RGB " => 3, + "CMYK" => 4, + _ => 0, + }; + if (components == 0 || profile.Elements.GetInteger("/N") != components) + continue; + found.Add((intent, profile, components, Name(intent, "/S") == "/GTS_PDFA1")); + } + return found; + } + + /// + /// The PDF/A output intent: one the PDF brings for the device colours it draws in (a PDF/A intent first), else + /// sRGB. A PDF in device CMYK without a CMYK intent, or in device CMYK and RGB together, can't have one. + /// + private static PdfArray OutputIntents(PdfDocument document, List<(PdfDictionary Intent, PdfDictionary Profile, int Components, bool PdfA)> existing, + Colours colours) + { + if (colours.Cmyk && colours.Rgb) + throw Refuse(DeviceCmyk); + var wanted = colours.Cmyk ? 4 : 3; + var kept = existing.Where(e => e.Components == wanted).OrderByDescending(e => e.PdfA).FirstOrDefault(); + if (kept.Profile is null && colours.Cmyk) + throw Refuse(DeviceCmyk); + // PDFsharp gives every page without one a transparency group in this colour space when it saves. + document.Options.ColorMode = colours.Cmyk ? PdfColorMode.Cmyk : PdfColorMode.Rgb; + + var intent = new PdfDictionary(document); + intent.Elements.SetName("/Type", "/OutputIntent"); + intent.Elements.SetName("/S", "/GTS_PDFA1"); + if (kept.Profile is not null) + { + var identifier = kept.Intent.Elements.GetString("/OutputConditionIdentifier"); + intent.Elements.SetString("/OutputConditionIdentifier", identifier.Length > 0 ? identifier : "Custom"); + foreach (var key in new[] { "/OutputCondition", "/RegistryName", "/Info" }) + { + if (kept.Intent.Elements.GetString(key) is { Length: > 0 } text) + intent.Elements.SetString(key, text); + } + intent.Elements["/DestOutputProfile"] = kept.Profile.Reference ?? (PdfItem)kept.Profile; + } + else + { + intent.Elements.SetString("/OutputConditionIdentifier", SrgbProfile.Name); + intent.Elements.SetString("/RegistryName", "http://www.color.org"); + intent.Elements.SetString("/Info", SrgbProfile.Name); + var profile = new PdfDictionary(document); + profile.Elements.SetInteger("/N", 3); + profile.CreateStream(SrgbProfile.Bytes); + profile.Stream.Zip(); + document.Internals.AddObject(profile); + intent.Elements["/DestOutputProfile"] = profile.Reference; + } + var intents = new PdfArray(document); + intents.Elements.Add(intent); + return intents; + } +} diff --git a/src/Filee.Engines/Pdf/PdfAMetadata.cs b/src/Filee.Engines/Pdf/PdfAMetadata.cs new file mode 100644 index 000000000..000687684 --- /dev/null +++ b/src/Filee.Engines/Pdf/PdfAMetadata.cs @@ -0,0 +1,262 @@ +// Saves a PDF made to keep the PDF/A-2b rules (PdfAConformance) with the metadata PDF/A asks for (ISO 19005-2 6.6): +// an XMP packet that says pdfaid:part 2, conformance B, and holds the same title, author, subject, keywords, creator +// tool, producer and dates as the document information dictionary, uncompressed in the catalog's /Metadata. +// +// PDFsharp can't be stopped from writing metadata of its own when it saves: it puts its name into the producer, sets +// the modification date, and replaces /Metadata with an XMP packet of its own, without pdfaid and with dates that +// don't always match the dictionary (PdfDocument.SetPdfA claims PDF/A-1a, which this isn't). So the PDF is saved by +// PDFsharp first and then given an incremental update (ISO 32000-1 7.5.6, which PDF/A allows) that writes the two +// objects again under their own numbers: the document information and the XMP packet. The catalog keeps pointing to +// the packet's number; the update's trailer has the same /ID PDFsharp wrote and a /Prev to PDFsharp's cross-reference +// table. Text in the dictionary is UTF-16 with a byte order mark; the XMP holds the same characters, with those XML +// can't carry left out of both, so the two always match. + +using System.Globalization; +using System.Security; +using System.Text; +using System.Text.RegularExpressions; +using PdfSharp.Pdf; + +namespace Filee.Engines.Pdf; + +/// Document information written into a PDF/A; null entries are left out. +internal sealed record PdfAInfo +{ + public string? Title { get; init; } + public string? Author { get; init; } + public string? Subject { get; init; } + public string? Keywords { get; init; } + + /// The program the document was made in (Info /Creator, xmp:CreatorTool). + public string? Creator { get; init; } + + /// The program that wrote the PDF (Info /Producer, pdf:Producer). + public string? Producer { get; init; } + + public DateTimeOffset? Created { get; init; } +} + +/// Saves a PDF/A with matching XMP and document information. +internal static partial class PdfAMetadata +{ + /// Characters of one text entry kept at most (a PDF/A string holds 32767 bytes, 6.1.13). + internal const int MaxText = 4000; + + /// The document information has now, for . + public static PdfAInfo InfoOf(PdfDocument document) + { + var info = document.Info; + string? Text(string key) => info.Elements.GetString(key) is { Length: > 0 } text ? text : null; + DateTimeOffset? created = null; + if (info.Elements.ContainsKey("/CreationDate")) + { + try + { + if (info.CreationDate is var date && date != DateTime.MinValue) + created = new DateTimeOffset(DateTime.SpecifyKind(date, date.Kind == DateTimeKind.Utc ? DateTimeKind.Utc : DateTimeKind.Local)); + } + catch (Exception ex) when (ex is FormatException or ArgumentException or InvalidCastException) + { + // A date PDFsharp can't read: the PDF/A gets today's. + } + } + return new PdfAInfo + { + Title = Text("/Title"), + Author = Text("/Author"), + Subject = Text("/Subject"), + Keywords = Text("/Keywords"), + Creator = Text("/Creator"), + Producer = Text("/Producer"), + Created = created, + }; + } + + /// + /// Saves to as PDF/A-2b with (by + /// default what the document has, read before PDFsharp changes it on saving); the modification date is now. + /// + public static void Save(PdfDocument document, Stream output, PdfAInfo? info = null, DateTimeOffset? now = null) + { + info ??= InfoOf(document); + var modified = Seconds(now ?? DateTimeOffset.Now); + var created = Seconds(info.Created ?? modified); + + using var buffer = new MemoryStream(); + document.Save(buffer, closeStream: false); + var saved = buffer.ToArray(); + + var metadata = document.Internals.Catalog.Elements.GetReference("/Metadata") + ?? throw new InvalidOperationException("PDFsharp wrote no XMP packet."); + var information = document.Info.Reference ?? throw new InvalidOperationException("PDFsharp wrote no document information."); + var trailer = Trailer.Read(saved); + + var entries = new List<(string Key, string Text)> + { + ("/Title", Clean(info.Title)), + ("/Author", Clean(info.Author)), + ("/Subject", Clean(info.Subject)), + ("/Keywords", Clean(info.Keywords)), + ("/Creator", Clean(info.Creator)), + ("/Producer", Clean(info.Producer)), + }; + entries.RemoveAll(e => e.Text.Length == 0); + + using var update = new MemoryStream(); + output.Write(saved); + var offset = (long)saved.Length; + if (saved.Length > 0 && saved[^1] is not (byte)'\n' and not (byte)'\r') + Write(update, "\n"); + + var offsets = new SortedDictionary(); + offsets[information.ObjectNumber] = (offset + update.Length, information.GenerationNumber); + var dictionary = new StringBuilder($"{information.ObjectNumber} {information.GenerationNumber} obj\n<<"); + foreach (var (key, text) in entries) + dictionary.Append(key).Append(' ').Append(HexText(text)); + dictionary.Append("/CreationDate ").Append(PdfDate(created)).Append("/ModDate ").Append(PdfDate(modified)).Append(">>\nendobj\n"); + Write(update, dictionary.ToString()); + + var xmp = Encoding.UTF8.GetBytes(Xmp(entries.ToDictionary(e => e.Key, e => e.Text), created, modified, trailer.Id[0])); + offsets[metadata.ObjectNumber] = (offset + update.Length, metadata.GenerationNumber); + Write(update, $"{metadata.ObjectNumber} {metadata.GenerationNumber} obj\n<>\nstream\n"); + update.Write(xmp); + Write(update, "\nendstream\nendobj\n"); + + var xref = offset + update.Length; + var table = new StringBuilder("xref\n"); + var numbers = offsets.Keys.ToList(); + for (var start = 0; start < numbers.Count;) + { + var end = start; + while (end + 1 < numbers.Count && numbers[end + 1] == numbers[end] + 1) + end++; + table.Append(CultureInfo.InvariantCulture, $"{numbers[start]} {end - start + 1}\n"); + for (var i = start; i <= end; i++) + { + var (at, generation) = offsets[numbers[i]]; + table.Append(CultureInfo.InvariantCulture, $"{at:D10} {generation:D5} n\r\n"); // 20 bytes an entry + } + start = end + 1; + } + var size = Math.Max(trailer.Size, numbers[^1] + 1); + table.Append(CultureInfo.InvariantCulture, + $"trailer\n<<{trailer.Id[1]}>]/Prev {trailer.StartXref}>>\nstartxref\n{xref}\n%%EOF\n"); + Write(update, table.ToString()); + update.Position = 0; + update.CopyTo(output); + } + + private static void Write(Stream stream, string text) => stream.Write(Encoding.Latin1.GetBytes(text)); + + private static DateTimeOffset Seconds(DateTimeOffset date) => + new(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Offset); + + /// + /// Text both the dictionary and XML can carry the same: no control characters but tab and line feed (XML reads a + /// carriage return as a line feed), lone surrogates as U+FFFD, at most characters. + /// + internal static string Clean(string? text) + { + if (string.IsNullOrEmpty(text)) + return ""; + var normalized = text.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n'); + var kept = new StringBuilder(Math.Min(normalized.Length, MaxText)); + foreach (var rune in normalized.EnumerateRunes()) + { + if (kept.Length + rune.Utf16SequenceLength > MaxText) + break; + if (rune.Value is < 0x20 and not ('\t' or '\n') or 0xFFFE or 0xFFFF) + continue; + kept.Append(rune.ToString()); + } + return kept.ToString().Trim(); + } + + /// A text string as UTF-16 with a byte order mark, in hex. + private static string HexText(string text) => ""; + + /// A PDF date (ISO 32000-1 7.9.4) with its offset from UTC. + internal static string PdfDate(DateTimeOffset date) + { + var offset = date.Offset; + var sign = offset < TimeSpan.Zero ? '-' : '+'; + offset = offset.Duration(); + return string.Create(CultureInfo.InvariantCulture, $"(D:{date:yyyyMMddHHmmss}{sign}{offset.Hours:00}'{offset.Minutes:00}')"); + } + + /// The same date as XMP writes it (ISO 8601). + internal static string XmpDate(DateTimeOffset date) => + date.ToString("yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture); + + /// The XMP packet: PDF/A identification and the document information's entries in their XMP properties. + internal static string Xmp(IReadOnlyDictionary entries, DateTimeOffset created, DateTimeOffset modified, string id) + { + static string X(string text) => SecurityElement.Escape(text); + var xmp = new StringBuilder(); + xmp.Append("\n"); + xmp.Append("\n\n"); + xmp.Append("\n"); + xmp.Append("2\nB\napplication/pdf\n"); + if (entries.TryGetValue("/Title", out var title)) + xmp.Append("").Append(X(title)).Append("\n"); + if (entries.TryGetValue("/Author", out var author)) + xmp.Append("").Append(X(author)).Append("\n"); + if (entries.TryGetValue("/Subject", out var subject)) + xmp.Append("").Append(X(subject)).Append("\n"); + if (entries.TryGetValue("/Keywords", out var keywords)) + xmp.Append("").Append(X(keywords)).Append("\n"); + if (entries.TryGetValue("/Creator", out var creator)) + xmp.Append("").Append(X(creator)).Append("\n"); + if (entries.TryGetValue("/Producer", out var producer)) + xmp.Append("").Append(X(producer)).Append("\n"); + xmp.Append("").Append(XmpDate(created)).Append("\n"); + xmp.Append("").Append(XmpDate(modified)).Append("\n"); + xmp.Append("").Append(XmpDate(modified)).Append("\n"); + xmp.Append("").Append(Uuid(id)).Append("\n"); + xmp.Append("\n\n\n"); + return xmp.ToString(); + } + + /// The file identifier as a UUID URN when it has 16 bytes, else as it is. + private static string Uuid(string id) => id.Length == 32 && Guid.TryParseExact(id, "N", out var guid) + ? "uuid:" + guid.ToString("D") + : "uuid:" + id.ToLowerInvariant(); + + /// What the update needs from the trailer PDFsharp wrote. + private sealed record Trailer(string Root, int Size, string[] Id, long StartXref) + { + /// Reads the last trailer of , which must be PDFsharp's cross-reference table. + public static Trailer Read(byte[] pdf) + { + // The trailer, startxref and %%EOF are in the last kilobytes. + var tail = Encoding.Latin1.GetString(pdf, Math.Max(0, pdf.Length - 4096), Math.Min(pdf.Length, 4096)); + var at = tail.LastIndexOf("trailer", StringComparison.Ordinal); + if (at < 0) + throw new InvalidOperationException("PDFsharp wrote no trailer to update."); + var text = tail[at..]; + var root = RootEntry().Match(text); + var size = SizeEntry().Match(text); + var start = StartXrefEntry().Match(text); + if (!root.Success || !size.Success || !start.Success) + throw new InvalidOperationException("PDFsharp's trailer can't be read."); + var id = IdEntry().Match(text); + var first = id.Success ? id.Groups[1].Value : Convert.ToHexString(Guid.NewGuid().ToByteArray()); + var second = id.Success ? id.Groups[2].Value : first; + return new Trailer($"{root.Groups[1].Value} {root.Groups[2].Value} R", int.Parse(size.Groups[1].Value, CultureInfo.InvariantCulture), + [first, second], long.Parse(start.Groups[1].Value, CultureInfo.InvariantCulture)); + } + } + + [GeneratedRegex(@"/Root\s*(\d+)\s+(\d+)\s+R", RegexOptions.CultureInvariant)] + private static partial Regex RootEntry(); + + [GeneratedRegex(@"/Size\s*(\d+)", RegexOptions.CultureInvariant)] + private static partial Regex SizeEntry(); + + [GeneratedRegex(@"/ID\s*\[\s*<([0-9A-Fa-f]*)>\s*<([0-9A-Fa-f]*)>\s*\]", RegexOptions.CultureInvariant)] + private static partial Regex IdEntry(); + + [GeneratedRegex(@"startxref\s+(\d+)\s+%%EOF\s*$", RegexOptions.CultureInvariant)] + private static partial Regex StartXrefEntry(); +} diff --git a/src/Filee.Engines/Pdf/PdfSharpConverter.cs b/src/Filee.Engines/Pdf/PdfSharpConverter.cs index 6fe668cce..def9b6fb4 100644 --- a/src/Filee.Engines/Pdf/PdfSharpConverter.cs +++ b/src/Filee.Engines/Pdf/PdfSharpConverter.cs @@ -1,6 +1,8 @@ // Builds PDFs from images, splits / extracts PDF pages, merges PDFs, makes PDFs smaller (PDFsharp, MIT; the // compression itself is in PdfCompressor, with Ghostscript as an optional second try) and removes their metadata. -// It also finishes every PDF a job writes with a watermark, page numbers or a password (PdfFinishing). +// It also finishes every PDF a job writes with a watermark, page numbers or a password (PdfFinishing), and makes +// every PDF of a PDF/A preset PDF/A-2b (PdfAConformance, PdfAMetadata): first in the engine order, so Ghostscript +// gets only the PDFs this can't make conform (a font that isn't embedded, CMYK without a profile, ...). using System.Runtime.ExceptionServices; using Filee.Core.Conversion; @@ -16,7 +18,7 @@ namespace Filee.Engines.Pdf; /// Image → PDF, PDF → PDF (split / page range / compression) and PDF merging. -public sealed class PdfSharpConverter : IConverter, IPdfMerger, IPdfFinisher, IImageTransformer +public sealed class PdfSharpConverter : IConverter, IPdfMerger, IPdfFinisher, IPdfArchiver, IImageTransformer { private const double PointsPerInch = 72.0; private const double MillimetresPerInch = 25.4; @@ -49,6 +51,51 @@ public Task> ConvertAsync(ConversionStep step, IProgress Task.Run(() => PdfFinishing.Finish(path, options, cancellationToken), cancellationToken); + /// + /// PDF/A-2b without Ghostscript (): a PDF that says it is PDF/A and wasn't changed since + /// stays as it is; any other is rewritten in place. Form fields stay fields. + /// + /// It can't conform (a pdfa.reason.* key): the job queue tries + /// the next archiver (Ghostscript), and the PDF is left as it is. + public Task ArchiveAsync(string path, bool changed, string workDirectory, CancellationToken cancellationToken) => + Task.Run(() => Archive(path, changed, workDirectory, cancellationToken), cancellationToken); + + internal static PdfArchiveOutcome Archive(string path, bool changed, string workDirectory, CancellationToken cancellationToken) + { + if (!changed && GhostscriptConverter.DeclaresPdfA(path)) + return PdfArchiveOutcome.Archived; + PdfDocument document; + try + { + document = Open(path, PdfDocumentOpenMode.Modify, cancellationToken); + } + catch (Exception ex) when (ex is not OperationCanceledException and not ConversionRefusedException) + { + throw new ConversionRefusedException(ex.Message.Contains("password", StringComparison.OrdinalIgnoreCase) + ? PdfAConformance.Locked : PdfAConformance.Unreadable); + } + var target = Path.Combine(workDirectory, $"pdfa-{Guid.NewGuid():N}.pdf"); + try + { + using (document) + { + var info = PdfAMetadata.InfoOf(document); + PdfAConformance.Prepare(document, cancellationToken); + cancellationToken.ThrowIfCancellationRequested(); + Directory.CreateDirectory(workDirectory); + using var stream = File.Create(target); + PdfAMetadata.Save(document, stream, info); + } + File.Move(target, path, overwrite: true); + return PdfArchiveOutcome.Archived; + } + finally + { + if (File.Exists(target)) + File.Delete(target); + } + } + /// Imports PDFs and their supported structure, optionally fitting every page to one paper. public Task MergeAsync(IReadOnlyList inputs, string outputPath, bool sourceBookmarks, Core.Conversion.PdfMergePaper? paper, CancellationToken cancellationToken) => diff --git a/src/Filee.Engines/Pdf/SrgbProfile.cs b/src/Filee.Engines/Pdf/SrgbProfile.cs new file mode 100644 index 000000000..38c3fe26a --- /dev/null +++ b/src/Filee.Engines/Pdf/SrgbProfile.cs @@ -0,0 +1,179 @@ +// The sRGB ICC profile of the PDF/A output intent Filee writes itself (PdfAConformance), built here from the numbers +// of IEC 61966-2-1 rather than shipped as a file: nobody's profile is copied, so there is no licence to carry. It is a +// version 2.1 display profile (ICC.1:2001-04) in the smallest form the format has: the sRGB primaries adapted from +// D65 to the D50 connection space with the Bradford transform, and one tone curve, the sRGB transfer function sampled +// at 1024 points, shared by the three channels. PDF/A-2 takes any ICC profile up to version 4 whose colour space +// matches the output intent's components (ISO 19005-2 6.2.3), so nothing more is needed. + +using System.Buffers.Binary; +using System.Text; + +namespace Filee.Engines.Pdf; + +/// An sRGB ICC profile made in code. +internal static class SrgbProfile +{ + /// The name the output intent and the profile's description give. + public const string Name = "sRGB IEC61966-2.1"; + + /// Points of the tone curve. + internal const int CurvePoints = 1024; + + private static readonly Lazy Profile = new(Build); + + /// The profile's bytes (a copy: callers may keep or change it). + public static byte[] Bytes => (byte[])Profile.Value.Clone(); + + // D50, the profile connection space's white (ICC.1 7.2.16), and D65, sRGB's own (IEC 61966-2-1 table 1). + private static readonly double[] D50 = [0.9642, 1.0, 0.8249]; + private static readonly (double X, double Y)[] Primaries = [(0.64, 0.33), (0.30, 0.60), (0.15, 0.06)]; + private static readonly (double X, double Y) D65 = (0.3127, 0.3290); + + private static byte[] Build() + { + var columns = AdaptedPrimaries(); + var tags = new List<(string Signature, byte[] Data)> + { + ("desc", Description(Name)), + ("cprt", Text("No copyright, use freely")), + ("wtpt", Xyz(D50)), + ("rXYZ", Xyz(columns[0])), + ("gXYZ", Xyz(columns[1])), + ("bXYZ", Xyz(columns[2])), + }; + var curve = Curve(); + + // Header (128 bytes), tag count, 12 bytes a tag, then the tag data, each on a 4-byte boundary. + var tableEnd = 128 + 4 + 12 * (tags.Count + 3); + using var data = new MemoryStream(); + var entries = new List<(string Signature, int Offset, int Size)>(); + foreach (var (signature, bytes) in tags) + { + entries.Add((signature, tableEnd + (int)data.Length, bytes.Length)); + Append(data, bytes); + } + var curveOffset = tableEnd + (int)data.Length; + Append(data, curve); + foreach (var signature in new[] { "rTRC", "gTRC", "bTRC" }) + entries.Add((signature, curveOffset, curve.Length)); // one curve for all three channels + + var profile = new byte[tableEnd + data.Length]; + var span = profile.AsSpan(); + BinaryPrimitives.WriteUInt32BigEndian(span, (uint)profile.Length); + BinaryPrimitives.WriteUInt32BigEndian(span[8..], 0x02100000); // version 2.1.0 + Ascii(span[12..], "mntr"); + Ascii(span[16..], "RGB "); + Ascii(span[20..], "XYZ "); + // A fixed date: the same profile on every run and every machine. + ushort[] date = [2026, 11, 20, 0, 0, 0]; + for (var i = 0; i < date.Length; i++) + BinaryPrimitives.WriteUInt16BigEndian(span[(24 + 2 * i)..], date[i]); + Ascii(span[36..], "acsp"); + // Platform, flags, maker, model, attributes and the perceptual intent stay zero. + for (var i = 0; i < 3; i++) + BinaryPrimitives.WriteInt32BigEndian(span[(68 + 4 * i)..], Fixed(D50[i])); + BinaryPrimitives.WriteUInt32BigEndian(span[128..], (uint)entries.Count); + for (var i = 0; i < entries.Count; i++) + { + var entry = span[(132 + 12 * i)..]; + Ascii(entry, entries[i].Signature); + BinaryPrimitives.WriteUInt32BigEndian(entry[4..], (uint)entries[i].Offset); + BinaryPrimitives.WriteUInt32BigEndian(entry[8..], (uint)entries[i].Size); + } + data.ToArray().CopyTo(profile, tableEnd); + return profile; + } + + /// The sRGB transfer function (IEC 61966-2-1 5.2) from encoded value to linear light. + internal static double ToLinear(double encoded) => + encoded <= 0.04045 ? encoded / 12.92 : Math.Pow((encoded + 0.055) / 1.055, 2.4); + + /// The XYZ of the red, green and blue primaries at full strength, adapted to D50 (Bradford). + internal static double[][] AdaptedPrimaries() + { + // RGB → XYZ for D65: the primaries' chromaticities, scaled so that full RGB is the white point. + var xyz = Primaries.Select(p => new[] { p.X / p.Y, 1.0, (1 - p.X - p.Y) / p.Y }).ToArray(); + double[] white = [D65.X / D65.Y, 1.0, (1 - D65.X - D65.Y) / D65.Y]; + var scale = Solve([[xyz[0][0], xyz[1][0], xyz[2][0]], [xyz[0][1], xyz[1][1], xyz[2][1]], [xyz[0][2], xyz[1][2], xyz[2][2]]], white); + var primaries = xyz.Select((p, i) => p.Select(v => v * scale[i]).ToArray()).ToArray(); + + // Bradford: into cone responses, scale each from D65's to D50's, back to XYZ. + double[][] bradford = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; + var from = Multiply(bradford, white); + var to = Multiply(bradford, D50); + return primaries.Select(p => + { + var cone = Multiply(bradford, p); + var adapted = cone.Select((c, i) => c * to[i] / from[i]).ToArray(); + return Solve(bradford, adapted); + }).ToArray(); + } + + private static double[] Multiply(double[][] matrix, double[] vector) => + matrix.Select(row => row[0] * vector[0] + row[1] * vector[1] + row[2] * vector[2]).ToArray(); + + /// Solves a 3 × 3 system by Cramer's rule. + private static double[] Solve(double[][] m, double[] b) + { + static double Det(double[][] a) => + a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1]) + - a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0]) + + a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]); + var det = Det(m); + return Enumerable.Range(0, 3) + .Select(column => Det(m.Select((row, r) => row.Select((v, c) => c == column ? b[r] : v).ToArray()).ToArray()) / det) + .ToArray(); + } + + /// s15Fixed16Number (ICC.1 4.6). + private static int Fixed(double value) => (int)Math.Round(value * 65536); + + private static byte[] Xyz(double[] value) + { + var bytes = new byte[20]; + Ascii(bytes, "XYZ "); + for (var i = 0; i < 3; i++) + BinaryPrimitives.WriteInt32BigEndian(bytes.AsSpan(8 + 4 * i), Fixed(value[i])); + return bytes; + } + + private static byte[] Curve() + { + var bytes = new byte[12 + 2 * CurvePoints]; + Ascii(bytes, "curv"); + BinaryPrimitives.WriteUInt32BigEndian(bytes.AsSpan(8), CurvePoints); + for (var i = 0; i < CurvePoints; i++) + { + var value = (ushort)Math.Round(ToLinear(i / (double)(CurvePoints - 1)) * ushort.MaxValue); + BinaryPrimitives.WriteUInt16BigEndian(bytes.AsSpan(12 + 2 * i), value); + } + return bytes; + } + + private static byte[] Text(string text) + { + var bytes = new byte[8 + text.Length + 1]; + Ascii(bytes, "text"); + Encoding.ASCII.GetBytes(text, bytes.AsSpan(8)); + return bytes; + } + + /// textDescriptionType (ICC.1:2001-04 6.5.17): the ASCII name, no Unicode or ScriptCode names. + private static byte[] Description(string text) + { + var bytes = new byte[12 + text.Length + 1 + 4 + 4 + 2 + 1 + 67]; + Ascii(bytes, "desc"); + BinaryPrimitives.WriteUInt32BigEndian(bytes.AsSpan(8), (uint)(text.Length + 1)); + Encoding.ASCII.GetBytes(text, bytes.AsSpan(12)); + return bytes; + } + + private static void Ascii(Span target, string text) => Encoding.ASCII.GetBytes(text, target); + + private static void Append(MemoryStream stream, byte[] bytes) + { + stream.Write(bytes); + while (stream.Length % 4 != 0) + stream.WriteByte(0); + } +} diff --git a/src/Filee.Engines/Rendering/PdfPageWriter.cs b/src/Filee.Engines/Rendering/PdfPageWriter.cs index cf8533d43..efaebb36b 100644 --- a/src/Filee.Engines/Rendering/PdfPageWriter.cs +++ b/src/Filee.Engines/Rendering/PdfPageWriter.cs @@ -8,6 +8,11 @@ // /ActualText for glyphs that don't map one to one (see PageContent.DrawRun). Links and named destinations are Skia // annotations; the outline (bookmarks) is added afterwards with PDFsharp, which Skia has no API for. // +// PDF/A (PdfDocumentInfo.PdfA, a preset's "PDF/A" box): the PDF goes through PDFsharp too and is made PDF/A-2b there +// (PdfAConformance: the sRGB output intent, links that print, nothing PDF/A forbids) and saved with matching XMP and +// document information (PdfAMetadata). Every font is embedded already, subset, with its ToUnicode map. Should that +// ever fail, the PDF is written as it is and the job queue's PDF/A step tries again. +// // A glyph no character of the cmap leads to (a font feature put it in place of the character's own glyph: Noto Sans // CJK's digits after a Latin letter, small caps, a ligature) is written to the ToUnicode map as U+0000, its text only // as /ActualText, which many readers ignore (they copy "H" and a NUL for "H1"). Where such a glyph always draws the @@ -18,6 +23,7 @@ using System.Text; using System.Text.RegularExpressions; using Filee.Engines.Fonts; +using Filee.Engines.Pdf; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; using SkiaSharp; @@ -33,6 +39,9 @@ internal sealed record PdfDocumentInfo /// Outline (bookmark) entries in document order. public IReadOnlyList Outline { get; init; } = []; + + /// Write the PDF as PDF/A-2b. + public bool PdfA { get; init; } } /// Page model → PDF. @@ -45,7 +54,7 @@ public static void Write(Stream output, IReadOnlyList pages, PdfDoc { info ??= new PdfDocumentInfo(); using var fonts = SubsetFonts(pages, cancellationToken); - if (info.Outline.Count == 0 && fonts.Unmapped.Count == 0 && pages.All(p => !NeedsExactSize(p))) + if (!info.PdfA && info.Outline.Count == 0 && fonts.Unmapped.Count == 0 && pages.All(p => !NeedsExactSize(p))) { WritePages(output, pages, info, fonts, progress, cancellationToken); return; @@ -53,7 +62,7 @@ public static void Write(Stream output, IReadOnlyList pages, PdfDoc using var buffer = new MemoryStream(); WritePages(buffer, pages, info, fonts, progress, cancellationToken); buffer.Position = 0; - Finish(buffer, output, pages, info.Outline, fonts.Unmapped); + Finish(buffer, output, pages, info, fonts.Unmapped, cancellationToken); } /// @@ -77,7 +86,7 @@ private static void WritePages(Stream output, IReadOnlyList pages, Modified = DateTime.Now, RasterDpi = SKDocument.DefaultRasterDpi, EncodingQuality = 100, - PdfA = false, + PdfA = info.PdfA, }; using var stream = new SKManagedWStream(output); using var document = SKDocument.CreatePdf(stream, metadata) ?? throw new InvalidOperationException("Skia can't write PDF."); @@ -171,10 +180,10 @@ private static SKTypeface SubsetTypeface(PageFont font, IEnumerable glyphs, /// /// What Skia can't write, added to the PDF it wrote: exact page sizes (a media box that ends at the top edge Skia - /// drew from, so nothing moves) and the outline. + /// drew from, so nothing moves), the outline and PDF/A. /// - private static void Finish(Stream pdf, Stream output, IReadOnlyList pages, IReadOnlyList entries, - IReadOnlyDictionary> unmapped) + private static void Finish(Stream pdf, Stream output, IReadOnlyList pages, PdfDocumentInfo info, + IReadOnlyDictionary> unmapped, CancellationToken cancellationToken) { using var document = PdfReader.Open(pdf, PdfDocumentOpenMode.Modify); AddGlyphText(document, unmapped); @@ -188,10 +197,36 @@ private static void Finish(Stream pdf, Stream output, IReadOnlyList box.Elements.Add(new PdfReal(Math.Round(value, 3))); document.Pages[i].Elements["/MediaBox"] = box; } - AddOutline(document, pages, entries); + AddOutline(document, pages, info.Outline); + if (info.PdfA && TryPdfA(document, cancellationToken)) + { + PdfAMetadata.Save(document, output, new PdfAInfo + { + Title = info.Title, + Author = info.Author, + Subject = info.Subject, + Creator = "Filee", + Producer = "Filee", + }); + return; + } document.Save(output, closeStream: false); } + /// Makes the PDF keep the PDF/A-2b rules; false when it can't (it is saved as a plain PDF then). + private static bool TryPdfA(PdfDocument document, CancellationToken cancellationToken) + { + try + { + PdfAConformance.Prepare(document, cancellationToken); + return true; + } + catch (Core.Conversion.ConversionRefusedException) + { + return false; + } + } + /// /// Gives the glyphs Skia mapped to U+0000 in a font's ToUnicode map their text. Fonts are matched by PostScript name /// (Skia's BaseFont is a subset tag and the typeface's PostScript name); codes are glyph IDs (Identity-H with an diff --git a/src/Filee.Engines/Slides/SlideOutput.cs b/src/Filee.Engines/Slides/SlideOutput.cs index 3b648e3d0..b2f1faca7 100644 --- a/src/Filee.Engines/Slides/SlideOutput.cs +++ b/src/Filee.Engines/Slides/SlideOutput.cs @@ -15,13 +15,14 @@ namespace Filee.Engines.Slides; internal static class SlideOutput { /// Slides written, as a fraction of all slides. - public static byte[] ToPdf(SlideDeck deck, IProgress? progress = null, CancellationToken cancellationToken = default) + /// Write it as PDF/A-2b. + public static byte[] ToPdf(SlideDeck deck, IProgress? progress = null, CancellationToken cancellationToken = default, bool pdfA = false) { using var shaper = new Shaper(); using var renderer = new SlideRenderer(new FontResolver(), shaper, cancellationToken); var pages = renderer.Render(deck); using var output = new MemoryStream(); - PdfPageWriter.Write(output, pages, new PdfDocumentInfo { Title = deck.Title }, progress, cancellationToken); + PdfPageWriter.Write(output, pages, new PdfDocumentInfo { Title = deck.Title, PdfA = pdfA }, progress, cancellationToken); return output.ToArray(); } diff --git a/src/Filee.Engines/Typesetting/FileeRenderer.cs b/src/Filee.Engines/Typesetting/FileeRenderer.cs index 113defef7..a2fae8e3d 100644 --- a/src/Filee.Engines/Typesetting/FileeRenderer.cs +++ b/src/Filee.Engines/Typesetting/FileeRenderer.cs @@ -17,12 +17,13 @@ internal static class FileeRenderer /// whose formats are complete. /// /// Pages written, as a fraction of all pages. + /// Write it as PDF/A-2b. public static byte[] ToPdf(HDocument document, CancellationToken cancellationToken = default, bool storedLines = true, - StyleResolver? styles = null, IProgress? progress = null) + StyleResolver? styles = null, IProgress? progress = null, bool pdfA = false) { var composed = Compose(document, cancellationToken, storedLines, styles); using var output = new MemoryStream(); - PdfPageWriter.Write(output, composed.Pages, new PdfDocumentInfo { Title = document.Title, Outline = composed.Outline }, + PdfPageWriter.Write(output, composed.Pages, new PdfDocumentInfo { Title = document.Title, Outline = composed.Outline, PdfA = pdfA }, progress, cancellationToken); return output.ToArray(); } diff --git a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs index 5711d7e3a..88a8516a0 100644 --- a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs +++ b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs @@ -342,7 +342,8 @@ public async Task> ConvertAsync(ConversionStep step, IProg // Reading takes the first fifth; pages written the rest. var pages = progress is null ? null : new Progress(p => progress.Report(0.2 + 0.8 * p)); - var pdf = await Task.Run(() => FileeRenderer.ToPdf(document, cancellationToken, styles: styles, progress: pages), cancellationToken); + var pdf = await Task.Run(() => FileeRenderer.ToPdf(document, cancellationToken, styles: styles, progress: pages, + pdfA: step.Preset.Document.PdfA), cancellationToken); var target = step.Output.Allocate("pdf"); if (target is null) return []; @@ -395,7 +396,7 @@ private static async Task> ConvertSlidesAsync(ConversionSt var pages = progress is null ? null : new Progress(p => progress.Report(0.2 + 0.8 * p)); if (step.To == "pdf") { - var pdf = await Task.Run(() => SlideOutput.ToPdf(deck, pages, cancellationToken), cancellationToken); + var pdf = await Task.Run(() => SlideOutput.ToPdf(deck, pages, cancellationToken, step.Preset.Document.PdfA), cancellationToken); if (step.Output.Allocate("pdf") is not { } target) return []; await File.WriteAllBytesAsync(target, pdf, cancellationToken); diff --git a/tests/Filee.App.Tests/PdfAAvailabilityTests.cs b/tests/Filee.App.Tests/PdfAAvailabilityTests.cs index 30544df1c..80e46136c 100644 --- a/tests/Filee.App.Tests/PdfAAvailabilityTests.cs +++ b/tests/Filee.App.Tests/PdfAAvailabilityTests.cs @@ -1,5 +1,6 @@ -// PDF/A (#1367): a PDF/A preset for PDFs needs Ghostscript, which rewrites them after the route, so without it the -// slice says which engine to get. Other files still convert (LibreOffice writes PDF/A itself, or a notice says it isn't). +// PDF/A (#1367): a PDF/A preset for PDFs needs an engine that rewrites them after the route: the built-in PDF engine, +// with Ghostscript for what that one can't make conform. With neither the slice says which engine to get. Other files +// still convert (their engine writes PDF/A itself, or a notice says it isn't). using Avalonia.Headless.XUnit; using Filee.App.Services; @@ -15,7 +16,7 @@ namespace Filee.App.Tests; public class PdfAAvailabilityTests { [AvaloniaFact] - public void Pdf_a_of_pdfs_waits_for_ghostscript_and_other_files_do_not() + public void Pdf_a_of_pdfs_is_built_in_and_waits_for_ghostscript_only_without_the_pdf_engine() { TestServices.EnsureInitialized("en"); var loc = AppHost.Get(); @@ -23,14 +24,19 @@ public void Pdf_a_of_pdfs_waits_for_ghostscript_and_other_files_do_not() var docx = FormatRegistry.Get("docx"); var preset = BuiltInData.CreatePresets().Single(p => p.Id == BuiltInData.PdfAId); var ghostscript = new FakeConverter("ghostscript", ("eps", "pdf")) { Available = false }; - var catalog = new ConverterCatalog([new FakeConverter("pdfsharp", ("pdf", "pdf")), new FakeConverter("ooxml", ("docx", "pdf")), ghostscript]); var package = new EnginePackageState(new EnginePackage("ghostscript", [], 0, ["ghostscript"]), loc) { Status = EnginePackageStatus.NotInstalled }; - var availability = new PresetAvailability(catalog, loc, [package]); + // The PDF engine is always there: PDF/A of PDFs needs nothing more. + var withPdfEngine = new ConverterCatalog([new FakeConverter("pdfsharp", ("pdf", "pdf")), new FakeConverter("ooxml", ("docx", "pdf")), ghostscript]); + Assert.Null(new PresetAvailability(withPdfEngine, loc, [package]).Check(preset, [pdf])); + Assert.Null(new PresetAvailability(withPdfEngine, loc, []).Check(preset, [pdf, docx])); + + // Without it (a test catalog), Ghostscript is what makes PDFs PDF/A. + var catalog = new ConverterCatalog([new FakeConverter("copier", ("pdf", "pdf")), new FakeConverter("ooxml", ("docx", "pdf")), ghostscript]); + var availability = new PresetAvailability(catalog, loc, [package]); Assert.Equal(loc.Format("engine.reason.needs_package", package.Name), availability.Check(preset, [pdf])); Assert.Equal(loc["error.no_route"], new PresetAvailability(catalog, loc, []).Check(preset, [pdf])); Assert.Null(availability.Check(preset, [docx])); - Assert.Null(availability.Check(preset, [pdf, docx])); Assert.Null(availability.Check(new Preset { TargetFormat = "pdf" }, [pdf])); // without the box, a plain copy ghostscript.Available = true; diff --git a/tests/Filee.Core.Tests/PdfAQueueTests.cs b/tests/Filee.Core.Tests/PdfAQueueTests.cs index 0c86f8d73..4ba61eea5 100644 --- a/tests/Filee.Core.Tests/PdfAQueueTests.cs +++ b/tests/Filee.Core.Tests/PdfAQueueTests.cs @@ -1,6 +1,8 @@ // PDF/A (#1367): JobQueue has every PDF a PDF/A preset writes made PDF/A after the finishing, whichever engine wrote it, // and a merged PDF once. Without the archiver (no Ghostscript) the PDF stays as it is and the file says so. A PDF whose // form fields became page content (#1496) says that instead, unless another PDF of the file didn't become PDF/A. +// Archivers are tried in engine order: one that refuses a PDF (the built-in one, when the PDF can't conform) hands it +// to the next, and when none makes it PDF/A the file says why the first refused. using Filee.Core.Conversion; using Filee.Core.Presets; @@ -230,4 +232,79 @@ public async Task A_merged_form_says_so_on_the_first_file() Assert.Equal(JobState.Completed, job.State); Assert.Equal(["notice.pdfa_form_fields", null], job.Files.Select(f => f.NoticeKey)); } + + /// Stands in for the built-in archiver: refuses every PDF with . + private sealed class RefusingArchiver(List log, string reason) : IConverter, IPdfArchiver + { + public string Id => "pdfsharp"; + public string DisplayName => "PDFsharp"; + public IReadOnlyList Edges => []; + public int MaxParallelism => 0; + public EngineStatus GetStatus() => EngineStatus.Available(); + + public Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) => + throw new NotSupportedException(); + + public Task ArchiveAsync(string path, bool changed, string workDirectory, CancellationToken cancellationToken) + { + lock (log) + log.Add("refuse " + Path.GetFileName(path)); + throw new ConversionRefusedException(reason); + } + } + + [Fact] + public async Task A_pdf_the_first_archiver_refuses_goes_to_the_next() + { + using var dir = new TempDir(); + var log = new List(); + var office = new FakeConverter("ooxml", new ConversionEdge("docx", "pdf")); + await using var queue = new JobQueue(new ConverterCatalog([office, new RefusingArchiver(log, "pdfa.reason.cmyk"), new FakeArchiver(log)])); + + var job = new ConversionJob([dir.File("report.docx")], PdfA(), "Test"); + await queue.RunAsync(job); + + Assert.Equal(JobState.Completed, job.State); + Assert.Equal(["refuse report_pdfa.pdf", "archive report_pdfa.pdf"], log); + Assert.Null(job.Files.Single().NoticeKey); + Assert.Null(job.Files.Single().NoticeReasonKey); + } + + [Fact] + public async Task Archivers_go_in_engine_order() + { + using var dir = new TempDir(); + var log = new List(); + var office = new FakeConverter("ooxml", new ConversionEdge("docx", "pdf")); + var catalog = new ConverterCatalog([office, new RefusingArchiver(log, "pdfa.reason.cmyk"), new FakeArchiver(log)]) + { + Priority = ["ooxml", "ghostscript", "pdfsharp"], + }; + await using var queue = new JobQueue(catalog); + + await queue.RunAsync(new ConversionJob([dir.File("report.docx")], PdfA(), "Test")); + + Assert.Equal(["archive report_pdfa.pdf"], log); // Ghostscript first, and it made it PDF/A + } + + [Fact] + public async Task When_no_archiver_makes_it_pdf_a_the_file_says_why_the_built_in_one_refused() + { + using var dir = new TempDir(); + var log = new List(); + var office = new FakeConverter("ooxml", new ConversionEdge("docx", "pdf")); + await using var queue = new JobQueue(new ConverterCatalog( + [office, new RefusingArchiver(log, "pdfa.reason.font_not_embedded"), new FakeArchiver(log, installed: false)])); + var job = new ConversionJob([dir.File("report.docx")], PdfA(), "Test"); + job.Files[0].NoticeDetail = "earlier"; + + await queue.RunAsync(job); + + Assert.Equal(JobState.Completed, job.State); + Assert.True(File.Exists(job.Outputs.Single())); + var file = job.Files.Single(); + Assert.Equal("notice.pdfa_needs_ghostscript", file.NoticeKey); + Assert.Equal("pdfa.reason.font_not_embedded", file.NoticeReasonKey); + Assert.Null(file.NoticeDetail); + } } diff --git a/tests/Filee.Engines.Tests/PdfA2bChecks.cs b/tests/Filee.Engines.Tests/PdfA2bChecks.cs new file mode 100644 index 000000000..00b9e612f --- /dev/null +++ b/tests/Filee.Engines.Tests/PdfA2bChecks.cs @@ -0,0 +1,445 @@ +// A structural check of the PDF/A-2b rules Filee's own PDF/A writer keeps (Pdf/PdfAConformance.cs, PdfAMetadata.cs), +// for tests: veraPDF isn't run here. Each rule is named by its clause of ISO 19005-2. It reads the file twice, with +// PDFsharp for the objects and as bytes for the header, the trailers and the end of the file, and gives back the +// rules broken, so a failing test says all of them at once. + +using System.Globalization; +using System.Text; +using System.Text.RegularExpressions; +using System.Xml.Linq; +using PdfSharp.Pdf; +using PdfSharp.Pdf.Advanced; +using PdfSharp.Pdf.IO; + +namespace Filee.Engines.Tests; + +internal static partial class PdfA2bChecks +{ + private static readonly XNamespace Rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; + private static readonly XNamespace Dc = "http://purl.org/dc/elements/1.1/"; + private static readonly XNamespace Xmp = "http://ns.adobe.com/xap/1.0/"; + private static readonly XNamespace Pdf = "http://ns.adobe.com/pdf/1.3/"; + private static readonly XNamespace PdfAId = "http://www.aiim.org/pdfa/ns/id/"; + private static readonly XNamespace Xml = "http://www.w3.org/XML/1998/namespace"; + + private static readonly HashSet Annotations = + [ + "/Text", "/Link", "/FreeText", "/Line", "/Square", "/Circle", "/Polygon", "/PolyLine", "/Highlight", "/Underline", + "/Squiggly", "/StrikeOut", "/Stamp", "/Caret", "/Ink", "/Popup", "/Widget", "/PrinterMark", "/TrapNet", "/Watermark", "/Redact", + ]; + + private static readonly HashSet Actions = ["/GoTo", "/GoToR", "/GoToE", "/Thread", "/URI", "/SubmitForm", "/Named"]; + + private static readonly HashSet BlendModes = + [ + "/Normal", "/Compatible", "/Multiply", "/Screen", "/Overlay", "/Darken", "/Lighten", "/ColorDodge", "/ColorBurn", + "/HardLight", "/SoftLight", "/Difference", "/Exclusion", "/Hue", "/Saturation", "/Color", "/Luminosity", + ]; + + /// Fails with every rule breaks. + public static void Assert2b(string path) + { + var broken = Check(File.ReadAllBytes(path)); + Assert.True(broken.Count == 0, "Not PDF/A-2b: " + string.Join("; ", broken)); + } + + private static PdfItem? V(PdfItem? item) => item is PdfReference reference ? reference.Value : item; + + private static string Name(PdfDictionary dictionary, string key) => V(dictionary.Elements[key]) is PdfName name ? name.Value : ""; + + public static List Check(byte[] bytes) + { + var broken = new List(); + var text = Encoding.Latin1.GetString(bytes); + + // 6.1.2: the header, then a comment of four bytes above 127. + if (!HeaderLine().IsMatch(text)) + broken.Add("6.1.2 header"); + // 6.1.3: an /ID in the (last) trailer, no /Encrypt, nothing after the last %%EOF but one end of line. + var trailer = text[text.LastIndexOf("trailer", StringComparison.Ordinal)..]; + if (!IdEntry().IsMatch(trailer)) + broken.Add("6.1.3 trailer /ID"); + foreach (Match each in TrailerDictionary().Matches(text)) + { + if (each.Value.Contains("/Encrypt", StringComparison.Ordinal)) + broken.Add("6.1.3 /Encrypt"); + } + if (!EndOfFile().IsMatch(text)) + broken.Add("6.1.3 data after %%EOF"); + + using var document = PdfReader.Open(new MemoryStream(bytes), PdfDocumentOpenMode.Import); + var catalog = document.Internals.Catalog; + CheckMetadata(document, broken); + var components = CheckOutputIntent(catalog, broken); + + // Everything the catalog leads to. + var dictionaries = new List(); + var arrays = new List(); + var seen = new HashSet(ReferenceEqualityComparer.Instance); + var pending = new Stack([catalog]); + while (pending.TryPop(out var item)) + { + switch (V(item)) + { + case PdfDictionary dictionary when seen.Add(dictionary): + dictionaries.Add(dictionary); + foreach (var (_, value) in dictionary.Elements) + pending.Push(value); + break; + case PdfArray array when seen.Add(array): + arrays.Add(array); + foreach (var value in array.Elements) + pending.Push(value); + break; + } + } + + if (V(catalog.Elements["/Names"]) is PdfDictionary names) + { + foreach (var key in new[] { "/JavaScript", "/EmbeddedFiles", "/AlternatePresentations" }) + { + if (names.Elements.ContainsKey(key)) + broken.Add($"names {key}"); + } + } + if (V(catalog.Elements["/AcroForm"]) is PdfDictionary form) + { + if (form.Elements.ContainsKey("/XFA")) + broken.Add("6.4.2 XFA"); + if (V(form.Elements["/NeedAppearances"]) is PdfBoolean { Value: true }) + broken.Add("6.4.1 NeedAppearances"); + } + if (V(catalog.Elements["/OCProperties"]) is PdfDictionary properties) + CheckOptionalContent(properties, broken); + + var fonts = new HashSet(ReferenceEqualityComparer.Instance); + foreach (var dictionary in dictionaries) + { + var type = Name(dictionary, "/Type"); + var subtype = Name(dictionary, "/Subtype"); + if (dictionary.Elements.ContainsKey("/AA")) + broken.Add($"6.5.2 /AA in {type}{subtype}"); + if (dictionary.Elements.ContainsKey("/Metadata") && !ReferenceEquals(dictionary, catalog)) + broken.Add($"metadata in {type}{subtype}"); + foreach (var key in new[] { "/A", "/OpenAction", "/Next" }) + { + var actions = V(dictionary.Elements[key]) switch + { + PdfDictionary { Stream: null } one when one.Elements.ContainsKey("/S") => [one], + PdfArray many when key == "/Next" => many.Elements.Select(V).OfType().ToList(), + _ => new List(), + }; + foreach (var action in actions.Select(a => Name(a, "/S"))) + { + if (!Actions.Contains(action)) + broken.Add($"6.5.1 action {action}"); + } + if (actions.FirstOrDefault(a => Name(a, "/S") == "/Named") is { } named + && Name(named, "/N") is not ("/NextPage" or "/PrevPage" or "/FirstPage" or "/LastPage")) + broken.Add("6.5.1 named action"); + } + if (type == "/Annot" || (Annotations.Contains(subtype) && dictionary.Elements.ContainsKey("/Rect"))) + CheckAnnotation(dictionary, subtype, broken); + if (dictionary.Stream is not null) + { + foreach (var key in new[] { "/F", "/FFilter", "/FDecodeParms" }) + { + if (dictionary.Elements.ContainsKey(key)) + broken.Add($"6.1.7.1 stream {key}"); + } + var filters = V(dictionary.Elements["/Filter"]) switch + { + PdfName one => [one.Value], + PdfArray many => many.Elements.Select(V).OfType().Select(n => n.Value).ToList(), + _ => new List(), + }; + if (filters.Contains("/LZWDecode") || filters.Contains("/Crypt")) + broken.Add("6.1.7.2 LZW or Crypt"); + } + if (subtype == "/Image") + { + if (V(dictionary.Elements["/Interpolate"]) is PdfBoolean { Value: true }) + broken.Add("6.2.8 Interpolate"); + if (dictionary.Elements.ContainsKey("/Alternates") || dictionary.Elements.ContainsKey("/OPI")) + broken.Add("6.2.8 Alternates or OPI"); + } + if (subtype == "/Form" && (dictionary.Elements.ContainsKey("/OPI") || dictionary.Elements.ContainsKey("/PS") + || dictionary.Elements.ContainsKey("/Ref") || Name(dictionary, "/Subtype2") == "/PS")) + broken.Add("6.2.9 form XObject"); + if (subtype == "/PS") + broken.Add("6.2.9 PostScript XObject"); + if (V(dictionary.Elements["/Resources"]) is PdfDictionary resources) + { + if (V(resources.Elements["/Font"]) is PdfDictionary listed) + { + foreach (var (_, font) in listed.Elements) + { + if (V(font) is PdfDictionary f) + fonts.Add(f); + } + } + if (V(resources.Elements["/ExtGState"]) is PdfDictionary states) + { + foreach (var (_, state) in states.Elements) + { + if (V(state) is PdfDictionary g) + CheckGraphicsState(g, broken); + } + } + } + } + foreach (var font in fonts) + CheckFont(font, broken); + + // 6.2.4.3: device colours only of the output intent's kind (gray goes with either). + var deviceNames = dictionaries.SelectMany(d => d.Elements.Where(e => !(e.Key == "/Alternate" && d.Stream is not null)).Select(e => V(e.Value))) + .Concat(arrays.SelectMany(a => a.Elements.Select(V))) + .OfType().Select(n => n.Value).ToHashSet(); + var content = string.Concat(dictionaries.Where(d => d.Stream is not null && (Name(d, "/Subtype") == "/Form" || Name(d, "/Type") == "/XObject" && Name(d, "/Subtype") != "/Image")) + .Concat(document.Pages.Cast().SelectMany(p => V(p.Elements["/Contents"]) switch + { + PdfDictionary one => [one], + PdfArray many => many.Elements.Select(V).OfType(), + _ => [], + })) + .Select(d => Encoding.Latin1.GetString(d.Stream!.UnfilteredValue))); + var rgb = deviceNames.Contains("/DeviceRGB") || RgbOperator().IsMatch(content) || content.Contains("/DeviceRGB", StringComparison.Ordinal); + var cmyk = deviceNames.Contains("/DeviceCMYK") || CmykOperator().IsMatch(content) || content.Contains("/DeviceCMYK", StringComparison.Ordinal); + if (rgb && components != 3) + broken.Add("6.2.4.3 DeviceRGB without an RGB intent"); + if (cmyk && components != 4) + broken.Add("6.2.4.3 DeviceCMYK without a CMYK intent"); + return broken; + } + + /// 6.6: an XMP packet with pdfaid 2 B, unfiltered, whose properties match the document information. + private static void CheckMetadata(PdfDocument document, List broken) + { + if (V(document.Internals.Catalog.Elements["/Metadata"]) is not PdfDictionary { Stream: not null } metadata) + { + broken.Add("6.6.2 no XMP"); + return; + } + if (metadata.Elements.ContainsKey("/Filter")) + broken.Add("6.6.2 filtered XMP"); + if (Name(metadata, "/Type") != "/Metadata" || Name(metadata, "/Subtype") != "/XML") + broken.Add("6.6.2 metadata type"); + var packet = Encoding.UTF8.GetString(metadata.Stream.Value); + if (!packet.StartsWith("", StringComparison.Ordinal); + XDocument xmp; + try + { + xmp = XDocument.Parse(packet[start..(end + "".Length)]); + } + catch (Exception ex) when (ex is System.Xml.XmlException or ArgumentOutOfRangeException) + { + broken.Add("6.6.2 XMP doesn't parse"); + return; + } + string? Property(XNamespace ns, string name) + { + var element = xmp.Descendants(ns + name).FirstOrDefault(); + var attribute = xmp.Descendants(Rdf + "Description").Attributes(ns + name).FirstOrDefault(); + return element?.Value ?? attribute?.Value; + } + if (Property(PdfAId, "part") != "2" || Property(PdfAId, "conformance") != "B") + broken.Add("6.6.4 pdfaid"); + + // 6.6.3: each Info entry equals its XMP property. + var info = document.Info; + void Same(string key, string? xmpValue) + { + if (info.Elements.ContainsKey(key) && info.Elements.GetString(key) != xmpValue) + broken.Add($"6.6.3 {key}: '{info.Elements.GetString(key)}' vs '{xmpValue}'"); + } + string? Alt(XNamespace ns, string name) => + xmp.Descendants(ns + name).Descendants(Rdf + "li").FirstOrDefault(li => (string?)li.Attribute(Xml + "lang") == "x-default")?.Value; + Same("/Title", Alt(Dc, "title")); + Same("/Subject", Alt(Dc, "description")); + var creators = xmp.Descendants(Dc + "creator").Descendants(Rdf + "li").Select(li => li.Value).ToList(); + if (info.Elements.ContainsKey("/Author") && (creators.Count != 1 || creators[0] != info.Elements.GetString("/Author"))) + broken.Add("6.6.3 /Author"); + Same("/Keywords", Property(Pdf, "Keywords")); + Same("/Creator", Property(Xmp, "CreatorTool")); + Same("/Producer", Property(Pdf, "Producer")); + if (info.Elements.ContainsKey("/Trapped")) + broken.Add("6.6.3 /Trapped"); + SameDate("/CreationDate", Property(Xmp, "CreateDate")); + SameDate("/ModDate", Property(Xmp, "ModifyDate")); + + void SameDate(string key, string? xmpValue) + { + if (!info.Elements.ContainsKey(key)) + return; + var pdf = PdfDateValue().Match(info.Elements.GetString(key)); + if (!pdf.Success || xmpValue is null || !DateTimeOffset.TryParse(xmpValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var fromXmp)) + { + broken.Add($"6.6.3 {key} format"); + return; + } + var offset = pdf.Groups[7].Value is "Z" or "" ? TimeSpan.Zero + : TimeSpan.FromHours(int.Parse(pdf.Groups[8].Value, CultureInfo.InvariantCulture)) + TimeSpan.FromMinutes(int.Parse(pdf.Groups[9].Value, CultureInfo.InvariantCulture)); + if (pdf.Groups[7].Value == "-") + offset = -offset; + int G(int i) => int.Parse(pdf.Groups[i].Value, CultureInfo.InvariantCulture); + var fromPdf = new DateTimeOffset(G(1), G(2), G(3), G(4), G(5), G(6), offset); + if (fromPdf != fromXmp) + broken.Add($"6.6.3 {key}: {fromPdf:o} vs {fromXmp:o}"); + } + } + + /// 6.2.3: GTS_PDFA1 intents with one profile (ICC version 4 at most, RGB or CMYK); its components. + private static int CheckOutputIntent(PdfCatalog catalog, List broken) + { + if (V(catalog.Elements["/OutputIntents"]) is not PdfArray intents) + { + broken.Add("6.2.3 no output intent"); + return 0; + } + var profiles = intents.Elements.Select(V).OfType().Where(i => Name(i, "/S") == "/GTS_PDFA1") + .Select(i => V(i.Elements["/DestOutputProfile"])).OfType().Distinct(ReferenceEqualityComparer.Instance).Cast().ToList(); + if (profiles.Count != 1) + { + broken.Add($"6.2.3 {profiles.Count} PDF/A profiles"); + return 0; + } + var icc = profiles[0].Stream!.UnfilteredValue; + var space = Encoding.ASCII.GetString(icc, 16, 4); + var components = space == "RGB " ? 3 : space == "CMYK" ? 4 : 0; + if (icc[8] > 4 || Encoding.ASCII.GetString(icc, 12, 4) is not ("mntr" or "prtr") || Encoding.ASCII.GetString(icc, 36, 4) != "acsp" + || components == 0 || profiles[0].Elements.GetInteger("/N") != components) + broken.Add("6.2.3 profile"); + return components; + } + + private static void CheckAnnotation(PdfDictionary annotation, string subtype, List broken) + { + if (!Annotations.Contains(subtype)) + broken.Add($"6.3.1 annotation {subtype}"); + var flags = V(annotation.Elements["/F"]) is PdfInteger f ? f.Value : (int?)null; + if (subtype != "/Popup" && flags is null) + broken.Add($"6.3.2 {subtype} without /F"); + if (flags is { } value && ((value & 4) == 0 || (value & (1 | 2 | 32 | 256)) != 0)) + broken.Add($"6.3.2 {subtype} flags {value}"); + if (subtype == "/Widget" && (annotation.Elements.ContainsKey("/A") || annotation.Elements.ContainsKey("/AA"))) + broken.Add("6.4.1 widget action"); + var rect = V(annotation.Elements["/Rect"]) as PdfArray; + var area = rect is { Elements.Count: 4 } && rect.Elements.GetReal(0) != rect.Elements.GetReal(2) && rect.Elements.GetReal(1) != rect.Elements.GetReal(3); + if (V(annotation.Elements["/AP"]) is not PdfDictionary appearance) + { + if (subtype is not ("/Link" or "/Popup") && area) + broken.Add($"6.3.3 {subtype} without appearance"); + return; + } + if (appearance.Elements.Keys.Any(k => k != "/N")) + broken.Add($"6.3.3 {subtype} appearance keys {string.Join(',', appearance.Elements.Keys)}"); + var normal = V(appearance.Elements["/N"]) as PdfDictionary; + var button = subtype == "/Widget" && FieldType(annotation) == "/Btn"; + if (normal is null || (button ? normal.Stream is not null : normal.Stream is null)) + broken.Add($"6.3.3 {subtype} normal appearance"); + } + + private static string FieldType(PdfDictionary widget) + { + for (PdfDictionary? field = widget; field is not null; field = V(field.Elements["/Parent"]) as PdfDictionary) + { + if (Name(field, "/FT") is { Length: > 0 } type) + return type; + } + return ""; + } + + private static void CheckGraphicsState(PdfDictionary state, List broken) + { + if (state.Elements.ContainsKey("/TR") || state.Elements.ContainsKey("/HTO")) + broken.Add("6.2.5 TR or HTO"); + if (state.Elements.ContainsKey("/TR2") && Name(state, "/TR2") != "/Default") + broken.Add("6.2.5 TR2"); + if (state.Elements.ContainsKey("/BM") && !BlendModes.Contains(Name(state, "/BM"))) + broken.Add("6.2.10 blend mode"); + if (state.Elements.ContainsKey("/RI") && Name(state, "/RI") is not ("/RelativeColorimetric" or "/AbsoluteColorimetric" or "/Perceptual" or "/Saturation")) + broken.Add("6.2.5 RI"); + } + + private static void CheckFont(PdfDictionary font, List broken) + { + var subtype = Name(font, "/Subtype"); + if (subtype == "/Type3") + return; + var fonts = subtype == "/Type0" + ? (V(font.Elements["/DescendantFonts"]) as PdfArray)?.Elements.Select(V).OfType().ToList() ?? [] + : [font]; + if (fonts.Count == 0) + broken.Add("6.2.11.4 Type 0 without a CIDFont"); + foreach (var each in fonts) + { + if (V(each.Elements["/FontDescriptor"]) is not PdfDictionary descriptor + || !new[] { "/FontFile", "/FontFile2", "/FontFile3" }.Any(k => V(descriptor.Elements[k]) is PdfDictionary { Stream: not null })) + broken.Add($"6.2.11.4.1 {Name(each, "/BaseFont")} not embedded"); + if (Name(each, "/Subtype") == "/CIDFontType2" && !each.Elements.ContainsKey("/CIDToGIDMap")) + broken.Add("6.2.11.3.2 CIDToGIDMap"); + } + } + + private static void CheckOptionalContent(PdfDictionary properties, List broken) + { + var configurations = new List(); + if (V(properties.Elements["/D"]) is PdfDictionary standard) + configurations.Add(standard); + if (V(properties.Elements["/Configs"]) is PdfArray others) + configurations.AddRange(others.Elements.Select(V).OfType()); + var names = configurations.Select(c => c.Elements.GetString("/Name")).ToList(); + if (names.Any(n => n.Length == 0) || names.Distinct().Count() != names.Count) + broken.Add("6.9 configuration names"); + if (configurations.Any(c => c.Elements.ContainsKey("/AS"))) + broken.Add("6.9 AS"); + var groups = (V(properties.Elements["/OCGs"]) as PdfArray)?.Elements.Select(V).OfType().ToList() ?? []; + foreach (var configuration in configurations) + { + if (V(configuration.Elements["/Order"]) is not PdfArray order) + continue; + var listed = new HashSet(ReferenceEqualityComparer.Instance); + var pending = new Stack(order.Elements); + while (pending.TryPop(out var item)) + { + switch (V(item)) + { + case PdfArray nested: + foreach (var element in nested.Elements) + pending.Push(element); + break; + case PdfDictionary group: + listed.Add(group); + break; + } + } + if (groups.Any(g => !listed.Contains(g))) + broken.Add("6.9 Order misses a group"); + } + } + + [GeneratedRegex(@"\A%PDF-1\.[0-7](\r\n|\r|\n)%[\x80-\xFF]{4}")] + private static partial Regex HeaderLine(); + + [GeneratedRegex(@"/ID\s*\[\s*<[0-9A-Fa-f]+>\s*<[0-9A-Fa-f]+>\s*\]")] + private static partial Regex IdEntry(); + + [GeneratedRegex(@"trailer\s*<<.*?>>\s*(%[^\r\n]*)?\s*startxref", RegexOptions.Singleline)] + private static partial Regex TrailerDictionary(); + + [GeneratedRegex(@"%%EOF(\r\n|\r|\n)?\z")] + private static partial Regex EndOfFile(); + + [GeneratedRegex(@"D:(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(?:([Z+-])(?:(\d{2})'(\d{2})'?)?)?")] + private static partial Regex PdfDateValue(); + + [GeneratedRegex(@"(^|\s)[-.0-9]+\s+[-.0-9]+\s+[-.0-9]+\s+(rg|RG)(\s|$)", RegexOptions.Multiline)] + private static partial Regex RgbOperator(); + + [GeneratedRegex(@"(^|\s)[-.0-9]+\s+[-.0-9]+\s+[-.0-9]+\s+[-.0-9]+\s+(k|K)(\s|$)", RegexOptions.Multiline)] + private static partial Regex CmykOperator(); +} diff --git a/tests/Filee.Engines.Tests/PdfAConformanceTests.cs b/tests/Filee.Engines.Tests/PdfAConformanceTests.cs new file mode 100644 index 000000000..2c6c088ac --- /dev/null +++ b/tests/Filee.Engines.Tests/PdfAConformanceTests.cs @@ -0,0 +1,465 @@ +// PDF/A-2b without Ghostscript (Pdf/PdfAConformance.cs, PdfAMetadata.cs, SrgbProfile.cs): the sRGB profile made in +// code, the XMP and document information that must match, a PDF full of what PDF/A forbids made to conform, the PDFs +// it refuses (and leaves as they are) with why, a CMYK PDF keeping its CMYK intent, the Filee renderer writing PDF/A +// directly, and the job queue without Ghostscript. Conformance is checked by PdfA2bChecks, a structural check of the +// rules implemented: veraPDF isn't run. Every input is built in the test. + +using System.Buffers.Binary; +using System.Text; +using Filee.Core.Conversion; +using Filee.Core.Presets; +using Filee.Engines.Pdf; +using Filee.Engines.Rendering; +using Filee.Engines.Vector; +using PdfSharp.Pdf; +using PdfSharp.Pdf.Advanced; +using PdfSharp.Pdf.IO; +using SkiaSharp; +using PigDocument = UglyToad.PdfPig.PdfDocument; + +namespace Filee.Engines.Tests; + +public class PdfAConformanceTests(EngineFixture fx) : IClassFixture +{ + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + private static PdfItem? V(PdfItem? item) => item is PdfReference reference ? reference.Value : item; + + private static Preset PdfAPreset() => + new() { TargetFormat = "pdf", Document = { PdfA = true }, Output = { FileNamePattern = "{name}_pdfa" } }; + + // ---- inputs + + /// + /// LZW data (ISO 32000-1 7.4.4) of nine-bit literal codes only: a clear code, each byte, the end code. Short data + /// never fills the table far enough for wider codes. + /// + private static byte[] Lzw(byte[] data) + { + Assert.True(data.Length < 200); + var codes = new List { 256 }; + codes.AddRange(data.Select(b => (int)b)); + codes.Add(257); + var bits = new List(); + foreach (var code in codes) + { + for (var bit = 8; bit >= 0; bit--) + bits.Add(((code >> bit) & 1) == 1); + } + var bytes = new byte[(bits.Count + 7) / 8]; + for (var i = 0; i < bits.Count; i++) + { + if (bits[i]) + bytes[i / 8] |= (byte)(0x80 >> (i % 8)); + } + return bytes; + } + + private static string Latin1(byte[] bytes) => Encoding.Latin1.GetString(bytes); + + /// The header of an ICC profile of (nothing else: what PDF/A looks at). + private static byte[] IccHeader(string deviceClass, string space) + { + var icc = new byte[132]; + BinaryPrimitives.WriteUInt32BigEndian(icc, (uint)icc.Length); + BinaryPrimitives.WriteUInt32BigEndian(icc.AsSpan(8), 0x02100000); + Encoding.ASCII.GetBytes(deviceClass, icc.AsSpan(12)); + Encoding.ASCII.GetBytes(space, icc.AsSpan(16)); + Encoding.ASCII.GetBytes("Lab ", icc.AsSpan(20)); + Encoding.ASCII.GetBytes("acsp", icc.AsSpan(36)); + return icc; + } + + private const string Embedded = + "<< /Type /Font /Subtype /TrueType /BaseFont /Dummy /FirstChar 72 /LastChar 105 /FontDescriptor 6 0 R >>"; + + private const string Descriptor = + "<< /Type /FontDescriptor /FontName /Dummy /Flags 32 /FontBBox [0 0 1000 1000] /ItalicAngle 0 /Ascent 800 " + + "/Descent -200 /CapHeight 700 /StemV 80 /FontFile2 7 0 R >>"; + + /// + /// One page drawing with an embedded font (its program is a stand-in: the rules ask + /// that it be there), objects 5 … 7 the font; objects are 8 …. + /// + private static string Simple(string path, string content = "BT /F1 12 Tf 72 700 Td (Hi) Tj ET 0 0 1 rg 72 600 200 100 re f", + string annotations = "", string catalog = "", string resources = "", IReadOnlyList? extra = null, string font = Embedded) => + RawPdf.Write(path, + [ + $"<< /Type /Catalog /Pages 2 0 R {catalog} >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + $"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> {resources} >> {annotations} >>", + RawPdf.Stream("", content), + font, + Descriptor, + RawPdf.Stream("/Length1 4", "true"), + .. extra ?? [], + ]); + + /// Everything PDF/A-2b forbids or wants otherwise that Filee can change, in one PDF. + private static string Forbidden(string path) + { + var picture = Enumerable.Range(0, 16).Select(i => (byte)(i * 16)).ToArray(); + return RawPdf.Write(path, + [ + /* 1 */ "<< /Type /Catalog /Pages 2 0 R /Metadata 26 0 R /Outlines 21 0 R /OpenAction << /S /JavaScript /JS (app.alert\\(1\\)) >> " + + "/AA << /WC << /S /JavaScript /JS (x) >> >> /Names << /JavaScript << /Names [(a) << /S /JavaScript /JS (y) >>] >> " + + "/EmbeddedFiles << /Names [] >> >> /OCProperties << /OCGs [24 0 R 25 0 R] /D << /Name (Same) " + + "/AS [<< /Event /View /OCGs [24 0 R] /Category [/View] >>] /Order [24 0 R] >> /Configs [<< /Name (Same) >>] >> " + + "/AcroForm << /Fields [20 0 R] /NeedAppearances true /XFA (xfa) >> >>", + /* 2 */ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + /* 3 */ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /AA << /O << /S /JavaScript /JS (z) >> >> " + + "/Resources << /Font << /F1 10 0 R /F2 11 0 R >> /ExtGState << /G1 14 0 R >> /XObject << /Im1 15 0 R /Fm1 16 0 R >> >> " + + "/Annots [5 0 R 6 0 R 7 0 R 8 0 R 9 0 R 18 0 R 19 0 R 20 0 R 22 0 R] >>", + /* 4 */ RawPdf.Stream("", "q 0 0 1 rg 72 600 200 100 re f Q BT /F1 12 Tf 72 700 Td (Hi) Tj ET /G1 gs /Im1 Do /Fm1 Do"), + /* 5 */ "<< /Type /Annot /Subtype /Link /Rect [72 650 200 670] /A << /S /Launch /F (calc.exe) >> /NM (launch) >>", + /* 6 */ "<< /Type /Annot /Subtype /Link /Rect [72 620 200 640] /A << /S /URI /URI (https://example.com) >> /NM (uri) >>", + /* 7 */ "<< /Type /Annot /Subtype /Square /Rect [300 600 400 650] /F 2 /AP << /N 12 0 R >> /NM (hidden) >>", + /* 8 */ "<< /Type /Annot /Subtype /FileAttachment /Rect [300 500 320 520] /FS << /Type /Filespec /F (a.txt) >> /NM (attachment) >>", + /* 9 */ "<< /Type /Annot /Subtype /Square /Rect [300 400 400 450] /AP << /N 12 0 R /D 12 0 R /R 12 0 R >> /NM (square) >>", + /* 10 */ "<< /Type /Font /Subtype /TrueType /BaseFont /Dummy /FirstChar 72 /LastChar 105 /FontDescriptor 13 0 R >>", + /* 11 */ "<< /Type /Font /Subtype /Type0 /BaseFont /DummyCID /Encoding /Identity-H /DescendantFonts [<< /Type /Font " + + "/Subtype /CIDFontType2 /BaseFont /DummyCID /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> " + + "/FontDescriptor 13 0 R >>] >>", + /* 12 */ RawPdf.Stream("/Type /XObject /Subtype /Form /BBox [0 0 100 50]", "0 g 0 0 100 50 re f"), + /* 13 */ "<< /Type /FontDescriptor /FontName /Dummy /Flags 32 /FontBBox [0 0 1000 1000] /ItalicAngle 0 /Ascent 800 " + + "/Descent -200 /CapHeight 700 /StemV 80 /FontFile2 17 0 R >>", + /* 14 */ "<< /Type /ExtGState /TR /Identity /TR2 /Identity /HTO [0 0] /BM /Weird /RI /Bogus /ca 0.5 >>", + /* 15 */ RawPdf.Stream("/Type /XObject /Subtype /Image /Width 4 /Height 4 /ColorSpace /DeviceGray /BitsPerComponent 8 " + + "/Interpolate true /Alternates [] /OPI << >> /Filter /LZWDecode", Latin1(Lzw(picture))), + /* 16 */ RawPdf.Stream("/Type /XObject /Subtype /Form /BBox [0 0 10 10] /Ref << /F (other.pdf) /Page 0 >> /OPI << >>", "0 g 0 0 10 10 re f"), + /* 17 */ RawPdf.Stream("/Length1 4", "true"), + /* 18 */ "<< /Type /Annot /Subtype /Popup /Rect [400 700 500 750] /F 2 /NM (popup) >>", + /* 19 */ "<< /Type /Annot /Subtype /Text /Rect [100 100 120 120] /AS /On /AP << /N << /On 12 0 R /Off 12 0 R >> >> /NM (note) >>", + /* 20 */ "<< /Type /Annot /Subtype /Widget /FT /Btn /T (ok) /Rect [100 200 200 220] /AP << /N 12 0 R >> " + + "/A << /S /JavaScript /JS (q) >> /AA << /D << /S /JavaScript /JS (w) >> >> /NM (button) >>", + /* 21 */ "<< /Type /Outlines /First 23 0 R /Last 23 0 R /Count 1 >>", + /* 22 */ "<< /Type /Annot /Subtype /Movie /Rect [100 300 200 350] /NM (movie) >>", + /* 23 */ "<< /Title (Go) /Parent 21 0 R /A << /S /JavaScript /JS (o) >> >>", + /* 24 */ "<< /Type /OCG /Name (Layer 1) >>", + /* 25 */ "<< /Type /OCG /Name (Layer 2) >>", + /* 26 */ RawPdf.Stream("/Type /Metadata /Subtype /XML", ""), + /* 27 */ "<< /Title (Forbidden things) /Author (Kim) /Trapped /True /Creator (Raw) /Producer (Hand) /CreationDate (D:20240101120000Z) >>", + ], "/Info 27 0 R"); + } + + private static Dictionary AnnotationsByName(PdfDocument document) => + ((PdfArray)V(document.Pages[0].Elements["/Annots"])!).Elements.Select(V).Cast() + .ToDictionary(a => a.Elements.GetString("/NM")); + + // ---- the profile and the metadata + + [Fact] + public void The_srgb_profile_is_a_version_2_display_profile_skia_reads_as_srgb() + { + var icc = SrgbProfile.Bytes; + + Assert.Equal((uint)icc.Length, BinaryPrimitives.ReadUInt32BigEndian(icc)); + Assert.Equal(2, icc[8]); + Assert.Equal("mntr", Encoding.ASCII.GetString(icc, 12, 4)); + Assert.Equal("RGB ", Encoding.ASCII.GetString(icc, 16, 4)); + Assert.Equal("XYZ ", Encoding.ASCII.GetString(icc, 20, 4)); + Assert.Equal("acsp", Encoding.ASCII.GetString(icc, 36, 4)); + Assert.True(icc.Length < 3000, $"{icc.Length} bytes"); + Assert.Equal(icc, SrgbProfile.Bytes); // the same every time + + // skcms (Skia's ICC reader) takes it and finds sRGB's gamut and curve in it. + using var parsed = SKColorSpace.CreateIcc(icc); + Assert.NotNull(parsed); + using var srgb = SKColorSpace.CreateSrgb(); + var (ours, theirs) = (parsed.ToColorSpaceXyz(), srgb.ToColorSpaceXyz()); + for (var row = 0; row < 3; row++) + { + for (var column = 0; column < 3; column++) + Assert.Equal(theirs[row, column], ours[row, column], 0.002); + } + Assert.True(parsed.GetNumericalTransferFunction(out var curve)); + foreach (var x in new[] { 0.01f, 0.2f, 0.5f, 0.8f, 1f }) + Assert.Equal(SrgbProfile.ToLinear(x), curve.Transform(x), 0.002); + } + + [Fact] + public void Xmp_and_document_information_say_the_same_whatever_the_text() + { + var dir = fx.NewFolder(); + var path = Path.Combine(dir, "info.pdf"); + using (var document = new PdfDocument()) + { + document.AddPage(); + PdfAConformance.Prepare(document, Ct); + using var output = File.Create(path); + PdfAMetadata.Save(document, output, new PdfAInfo + { + Title = "Ünïcödé 한글 <&> \"quotes\"\r\nnext line\u0001", + Author = "Kim & Lee", + Subject = " spaced ", + Keywords = "pdf, archive", + Creator = "Writer", + Producer = "Filee", + Created = new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.FromHours(-5.5)), + }, now: new DateTimeOffset(2026, 11, 20, 12, 0, 0, TimeSpan.FromHours(9))); + } + + PdfA2bChecks.Assert2b(path); + using (var document = PdfReader.Open(path, PdfDocumentOpenMode.Import)) + { + Assert.Equal("Ünïcödé 한글 <&> \"quotes\"\nnext line", document.Info.Title); + Assert.Equal("spaced", document.Info.Subject); + Assert.Equal("(D:20240102030405-05'30')", "(" + document.Info.Elements.GetString("/CreationDate") + ")"); + Assert.True(GhostscriptConverter.DeclaresPdfA(path)); + } + using var pig = PigDocument.Open(path); + Assert.Equal(1, pig.NumberOfPages); + Assert.Equal("Kim & Lee", pig.Information.Author); + } + + // ---- PDF → PDF/A + + [Fact] + public void A_pdf_full_of_what_pdf_a_forbids_conforms_afterwards() + { + var dir = fx.NewFolder(); + var path = Forbidden(Path.Combine(dir, "forbidden.pdf")); + Assert.NotEmpty(PdfA2bChecks.Check(File.ReadAllBytes(path))); + + Assert.Equal(PdfArchiveOutcome.Archived, PdfSharpConverter.Archive(path, changed: false, fx.NewFolder(), Ct)); + + PdfA2bChecks.Assert2b(path); + using var document = PdfReader.Open(path, PdfDocumentOpenMode.Import); + var catalog = document.Internals.Catalog; + Assert.False(catalog.Elements.ContainsKey("/OpenAction")); + Assert.False(catalog.Elements.ContainsKey("/AA")); + + // Hidden, attachment and movie annotations are gone; the rest print and keep their appearance. + var annotations = AnnotationsByName(document); + Assert.Equal(["button", "launch", "note", "popup", "square", "uri"], annotations.Keys.Order()); + Assert.False(annotations["launch"].Elements.ContainsKey("/A")); + Assert.Equal("https://example.com", ((PdfDictionary)V(annotations["uri"].Elements["/A"])!).Elements.GetString("/URI")); + Assert.False(annotations["popup"].Elements.ContainsKey("/F")); + Assert.Equal("/Off", annotations["button"].Elements.GetName("/AS")); + Assert.NotNull(((PdfDictionary)V(annotations["note"].Elements.GetDictionary("/AP")!.Elements["/N"])!).Stream); + + // The picture is deflated now, its pixels the same. + var page = document.Pages[0]; + var resources = page.Elements.GetDictionary("/Resources")!; + var image = (PdfDictionary)V(resources.Elements.GetDictionary("/XObject")!.Elements["/Im1"])!; + Assert.Equal("/FlateDecode", image.Elements.GetName("/Filter")); + Assert.Equal(Enumerable.Range(0, 16).Select(i => (byte)(i * 16)).ToArray(), image.Stream!.UnfilteredValue); + + var cid = (PdfDictionary)V(((PdfArray)V(((PdfDictionary)V(resources.Elements.GetDictionary("/Font")!.Elements["/F2"])!).Elements["/DescendantFonts"])!).Elements[0])!; + Assert.Equal("/Identity", cid.Elements.GetName("/CIDToGIDMap")); + var form = catalog.Elements.GetDictionary("/AcroForm")!; + Assert.False(form.Elements.ContainsKey("/XFA")); + var outline = (PdfDictionary)V(catalog.Elements.GetDictionary("/Outlines")!.Elements["/First"])!; + Assert.Equal("Go", outline.Elements.GetString("/Title")); + Assert.False(outline.Elements.ContainsKey("/A")); + + // Its document information, without what PDF/A has no place for. + Assert.Equal("Forbidden things", document.Info.Title); + Assert.Equal("Hand", document.Info.Elements.GetString("/Producer")); + Assert.False(document.Info.Elements.ContainsKey("/Trapped")); + } + + public static TheoryData Refused() => new() + { + { "font", PdfAConformance.FontNotEmbedded }, + { "cmyk", PdfAConformance.DeviceCmyk }, + { "cmyk-and-rgb", PdfAConformance.DeviceCmyk }, + { "appearance", PdfAConformance.NoAppearance }, + { "xfa", PdfAConformance.Unsupported }, + { "postscript", PdfAConformance.Unsupported }, + { "inline-lzw", PdfAConformance.Unsupported }, + { "external", PdfAConformance.Unsupported }, + { "damaged", PdfAConformance.Unreadable }, + }; + + [Theory] + [MemberData(nameof(Refused))] + public void What_it_cannot_make_conform_is_refused_with_why_and_left_as_it_is(string kind, string reason) + { + var path = Path.Combine(fx.NewFolder(), kind + ".pdf"); + _ = kind switch + { + "font" => Simple(path, font: "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"), + "cmyk" => Simple(path, content: "0 0 0 1 k 72 600 200 100 re f"), + "cmyk-and-rgb" => Simple(path, content: "0 0 0 1 k 72 600 200 100 re f 1 0 0 rg 0 0 10 10 re f", + extra: [IccHeaderObject("prtr", "CMYK")], catalog: Intent("GTS_PDFA1", 8)), + "appearance" => Simple(path, annotations: "/Annots [<< /Type /Annot /Subtype /Square /Rect [10 10 50 50] >>]"), + "xfa" => Simple(path, catalog: "/NeedsRendering true /AcroForm << /Fields [] /XFA (x) >>"), + "postscript" => Simple(path, resources: "/XObject << /P 8 0 R >>", content: "/P Do", + extra: [RawPdf.Stream("/Type /XObject /Subtype /PS", "showpage")]), + "inline-lzw" => Simple(path, content: $"BI /W 4 /H 4 /CS /G /BPC 8 /F /LZW ID {Latin1(Lzw(new byte[16]))} EI"), + "external" => Simple(path, resources: "/XObject << /X 8 0 R >>", content: "/X Do", + extra: [RawPdf.Stream("/Type /XObject /Subtype /Form /BBox [0 0 1 1] /FFilter /FlateDecode", "")]), + _ => Simple(path, content: "BT /F1 12 Tf (never closed"), + }; + var bytes = File.ReadAllBytes(path); + var work = fx.NewFolder(); + + var refused = Assert.Throws(() => PdfSharpConverter.Archive(path, changed: true, work, Ct)); + + Assert.Equal(reason, refused.ErrorKey); + Assert.Equal(bytes, File.ReadAllBytes(path)); + Assert.Empty(Directory.EnumerateFileSystemEntries(work)); + } + + private static string IccHeaderObject(string deviceClass, string space) => + RawPdf.Stream($"/N {(space == "CMYK" ? 4 : 3)}", Latin1(IccHeader(deviceClass, space))); + + private static string Intent(string kind, int profile, string identifier = "FOGRA39") => + $"/OutputIntents [<< /Type /OutputIntent /S /{kind} /OutputConditionIdentifier ({identifier}) /DestOutputProfile {profile} 0 R >>]"; + + [Fact] + public void A_pdf_locked_with_a_password_is_refused_as_locked() + { + var path = Simple(Path.Combine(fx.NewFolder(), "locked.pdf")); + PdfBuilder.Protect(path, "secret"); + + var refused = Assert.Throws(() => PdfSharpConverter.Archive(path, changed: false, fx.NewFolder(), Ct)); + + Assert.Equal(PdfAConformance.Locked, refused.ErrorKey); + } + + [Fact] + public void A_cmyk_pdf_keeps_the_cmyk_intent_it_brings() + { + var path = Simple(Path.Combine(fx.NewFolder(), "press.pdf"), content: "0 0 0 1 k 72 600 200 100 re f", + extra: [IccHeaderObject("prtr", "CMYK")], catalog: Intent("GTS_PDFX", 8)); + + Assert.Equal(PdfArchiveOutcome.Archived, PdfSharpConverter.Archive(path, changed: false, fx.NewFolder(), Ct)); + + PdfA2bChecks.Assert2b(path); + using var document = PdfReader.Open(path, PdfDocumentOpenMode.Import); + var intent = (PdfDictionary)V(document.Internals.Catalog.Elements.GetArray("/OutputIntents")!.Elements.Single())!; + Assert.Equal("/GTS_PDFA1", intent.Elements.GetName("/S")); + Assert.Equal("FOGRA39", intent.Elements.GetString("/OutputConditionIdentifier")); + Assert.Equal(4, ((PdfDictionary)V(intent.Elements["/DestOutputProfile"])!).Elements.GetInteger("/N")); + } + + [Fact] + public void An_rgb_pdf_gets_the_srgb_intent_and_a_pdf_a_already_is_left_alone() + { + var dir = fx.NewFolder(); + var path = Simple(Path.Combine(dir, "plain.pdf")); + + Assert.Equal(PdfArchiveOutcome.Archived, PdfSharpConverter.Archive(path, changed: false, fx.NewFolder(), Ct)); + + PdfA2bChecks.Assert2b(path); + using (var document = PdfReader.Open(path, PdfDocumentOpenMode.Import)) + { + var intent = (PdfDictionary)V(document.Internals.Catalog.Elements.GetArray("/OutputIntents")!.Elements.Single())!; + Assert.Equal(SrgbProfile.Name, intent.Elements.GetString("/OutputConditionIdentifier")); + Assert.Equal(SrgbProfile.Bytes, ((PdfDictionary)V(intent.Elements["/DestOutputProfile"])!).Stream!.UnfilteredValue); + } + var bytes = File.ReadAllBytes(path); + Assert.Equal(PdfArchiveOutcome.Archived, PdfSharpConverter.Archive(path, changed: false, fx.NewFolder(), Ct)); + Assert.Equal(bytes, File.ReadAllBytes(path)); + + // Changed since (a watermark): made PDF/A again, and still one. + Assert.Equal(PdfArchiveOutcome.Archived, PdfSharpConverter.Archive(path, changed: true, fx.NewFolder(), Ct)); + PdfA2bChecks.Assert2b(path); + } + + [Fact] + public void Preparing_stops_when_cancelled() + { + var path = Simple(Path.Combine(fx.NewFolder(), "plain.pdf")); + using var cancelled = new CancellationTokenSource(); + cancelled.Cancel(); + + Assert.ThrowsAny(() => PdfSharpConverter.Archive(path, changed: true, fx.NewFolder(), cancelled.Token)); + } + + // ---- the Filee renderer + + [Fact] + public void The_renderer_writes_pdf_a_directly_with_embedded_subset_fonts_and_printing_links() + { + var fonts = RepositoryFonts.Load(); + Assert.SkipUnless(fonts.Has(RepositoryFonts.Latin), "Noto Sans is not in src/Filee.App/Assets/Fonts (build/fetch-fonts.ps1)"); + using var shaper = new TestShaper(); + var page = new PageContent(595.28f, 841.89f); // A4: not whole points, so PDFsharp sets the media box too + page.DrawGlyphs(shaper.Shape(fonts.Font(RepositoryFonts.Latin), 20, "Archived for later", 50, 100).Run); + page.LinkToUrl(new SKRect(50, 80, 250, 105), "https://example.com/archive"); + using var output = new MemoryStream(); + + PdfPageWriter.Write(output, [page], new PdfDocumentInfo { Title = "Archive", Outline = [new("Start", 0, 80, 0)], PdfA = true }, + cancellationToken: Ct); + + var path = Path.Combine(fx.NewFolder(), "rendered.pdf"); + File.WriteAllBytes(path, output.ToArray()); + PdfA2bChecks.Assert2b(path); + Assert.True(GhostscriptConverter.DeclaresPdfA(path)); + using (var document = PdfReader.Open(path, PdfDocumentOpenMode.Import)) + { + Assert.Equal("Archive", document.Info.Title); + Assert.Equal("Filee", document.Info.Elements.GetString("/Producer")); + Assert.Equal("Filee", document.Info.Elements.GetString("/Creator")); + var fontsUsed = document.Pages[0].Elements.GetDictionary("/Resources")!.Elements.GetDictionary("/Font")!.Elements.Values + .Select(V).Cast().ToList(); + Assert.NotEmpty(fontsUsed); + Assert.All(fontsUsed, f => Assert.True(f.Elements.ContainsKey("/ToUnicode"), f.Elements.GetName("/BaseFont"))); + Assert.All(fontsUsed, f => Assert.Matches("^/[A-Z]{6}\\+", f.Elements.GetName("/BaseFont"))); // subset + var link = (PdfDictionary)V(document.Pages[0].Elements.GetArray("/Annots")!.Elements.Single())!; + Assert.Equal(4, link.Elements.GetInteger("/F")); + Assert.Equal("Start", Assert.Single(document.Outlines).Title); + } + using var pig = PigDocument.Open(path); + Assert.Equal("Archivedforlater", string.Concat(pig.GetPage(1).Text.Where(c => !char.IsWhiteSpace(c)))); + } + + [Fact] + public void Without_the_box_the_renderer_writes_no_pdf_a() + { + var page = new PageContent(200, 100); + page.Draw(canvas => canvas.DrawRect(10, 10, 50, 50, new SKPaint { Color = SKColors.Teal })); + using var output = new MemoryStream(); + + PdfPageWriter.Write(output, [page], new PdfDocumentInfo(), cancellationToken: Ct); + + Assert.DoesNotContain("pdfaid", Encoding.Latin1.GetString(output.ToArray())); + } + + [Fact] + public async Task A_document_the_renderer_draws_comes_out_pdf_a_from_its_own_step() + { + var dir = fx.NewFolder(); + var hwpx = await fx.MakeHwpxAsync(dir); + var renderer = fx.Converters.Single(c => c.Id == "layout"); + var target = Path.Combine(dir, "out.pdf"); + var step = new ConversionStep(hwpx, "hwpx", "pdf", PdfAPreset(), new FixedOutput(target), fx.NewFolder()); + + var written = await renderer.ConvertAsync(step, null, Ct); + + Assert.Equal(target, Assert.Single(written)); + PdfA2bChecks.Assert2b(target); + Assert.True(GhostscriptConverter.DeclaresPdfA(target)); + } + + private sealed class FixedOutput(string path) : IOutputAllocator + { + public string? Allocate(string extension, string? suffix = null) => path; + } + + // ---- the job queue without Ghostscript + + private ConverterCatalog WithoutGhostscript() => + new(fx.Converters.Where(c => c.Id != "ghostscript")) { Priority = fx.Catalog.Priority }; + + [Fact] + public async Task Without_ghostscript_a_pdf_becomes_pdf_a_and_one_it_cannot_says_why() + { + var dir = fx.NewFolder(); + var good = Simple(Path.Combine(dir, "good.pdf")); + var helvetica = Simple(Path.Combine(dir, "helvetica.pdf"), font: "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"); + + var job = await fx.ConvertAsync([good, helvetica], BuiltInData.CreatePresets().Single(p => p.Id == BuiltInData.PdfAId), WithoutGhostscript()); + + Assert.Equal(JobState.Completed, job.State); + var (first, second) = (job.Files[0], job.Files[1]); + Assert.Null(first.NoticeKey); + PdfA2bChecks.Assert2b(first.Outputs.Single()); + Assert.Equal("notice.pdfa_needs_ghostscript", second.NoticeKey); + Assert.Equal(PdfAConformance.FontNotEmbedded, second.NoticeReasonKey); + Assert.False(GhostscriptConverter.DeclaresPdfA(second.Outputs.Single())); + } +} From 971d0dca3d8dff5593b74be9190e5d14452bfa96 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 18:22:46 +0900 Subject: [PATCH 4/6] feat(djvu): read DjVu documents to PDF and pictures (#346) Filee's own DjVu reader, written from the DjVu v3 specification: one page or bundled documents, the ZP-coder, BZZ, JB2 masks with shared dictionaries, IW44 backgrounds and foregrounds, palettes and hidden text. PDF pages keep the layers (JPEG background, 1-bit masks) with the hidden text as an invisible, searchable layer; pages also become PNG, JPG and TIFF. Indirect documents are refused with their own message; sizes, counts and unpacked bytes are bounded by DjvuLimits. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/ENGINES.md | 44 +- src/Filee.App/Assets/i18n/ar.json | 3 + src/Filee.App/Assets/i18n/bn-BD.json | 3 + src/Filee.App/Assets/i18n/de.json | 3 + src/Filee.App/Assets/i18n/en.json | 3 + src/Filee.App/Assets/i18n/es.json | 3 + src/Filee.App/Assets/i18n/fa.json | 3 + src/Filee.App/Assets/i18n/fr.json | 3 + src/Filee.App/Assets/i18n/hi.json | 3 + src/Filee.App/Assets/i18n/id.json | 3 + src/Filee.App/Assets/i18n/it.json | 3 + src/Filee.App/Assets/i18n/ja.json | 3 + src/Filee.App/Assets/i18n/ko.json | 3 + src/Filee.App/Assets/i18n/mn.json | 3 + src/Filee.App/Assets/i18n/ne.json | 3 + src/Filee.App/Assets/i18n/pl.json | 3 + src/Filee.App/Assets/i18n/pt-BR.json | 3 + src/Filee.App/Assets/i18n/pt-PT.json | 3 + src/Filee.App/Assets/i18n/ru.json | 3 + src/Filee.App/Assets/i18n/th.json | 3 + src/Filee.App/Assets/i18n/tr.json | 3 + src/Filee.App/Assets/i18n/vi.json | 3 + src/Filee.App/Assets/i18n/zh-CN.json | 3 + src/Filee.App/Assets/i18n/zh-TW.json | 3 + src/Filee.Core/Formats/FormatFields.cs | 2 +- src/Filee.Core/Formats/FormatRegistry.cs | 2 + src/Filee.Engines/Djvu/BzzDecoder.cs | 179 +++ src/Filee.Engines/Djvu/DjvuConverter.cs | 135 ++ src/Filee.Engines/Djvu/DjvuDocument.cs | 460 +++++++ src/Filee.Engines/Djvu/DjvuLimits.cs | 56 + src/Filee.Engines/Djvu/DjvuPdfWriter.cs | 220 ++++ src/Filee.Engines/Djvu/DjvuRenderer.cs | 185 +++ src/Filee.Engines/Djvu/DjvuText.cs | 239 ++++ src/Filee.Engines/Djvu/Iw44Decoder.cs | 700 +++++++++++ src/Filee.Engines/Djvu/Jb2Decoder.cs | 519 ++++++++ src/Filee.Engines/Djvu/ZpDecoder.cs | 218 ++++ src/Filee.Engines/EngineRegistry.cs | 1 + src/Filee.Engines/Ocr/OcrTextLayer.cs | 9 + tests/Filee.Core.Tests/FormatFieldsTests.cs | 2 +- tests/Filee.Engines.Tests/DjvuBuilder.cs | 1245 +++++++++++++++++++ tests/Filee.Engines.Tests/DjvuFuzzTests.cs | 65 + tests/Filee.Engines.Tests/DjvuTests.cs | 610 +++++++++ 43 files changed, 4954 insertions(+), 8 deletions(-) create mode 100644 src/Filee.Engines/Djvu/BzzDecoder.cs create mode 100644 src/Filee.Engines/Djvu/DjvuConverter.cs create mode 100644 src/Filee.Engines/Djvu/DjvuDocument.cs create mode 100644 src/Filee.Engines/Djvu/DjvuLimits.cs create mode 100644 src/Filee.Engines/Djvu/DjvuPdfWriter.cs create mode 100644 src/Filee.Engines/Djvu/DjvuRenderer.cs create mode 100644 src/Filee.Engines/Djvu/DjvuText.cs create mode 100644 src/Filee.Engines/Djvu/Iw44Decoder.cs create mode 100644 src/Filee.Engines/Djvu/Jb2Decoder.cs create mode 100644 src/Filee.Engines/Djvu/ZpDecoder.cs create mode 100644 tests/Filee.Engines.Tests/DjvuBuilder.cs create mode 100644 tests/Filee.Engines.Tests/DjvuFuzzTests.cs create mode 100644 tests/Filee.Engines.Tests/DjvuTests.cs diff --git a/README.md b/README.md index d4cf89488..30a2ab23a 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ To undo it, remove the entries in Windows Security → App & browser control → | **Images**: JPG, PNG, WEBP, AVIF, TIFF, BMP, GIF, APNG, ICO, ICNS, JPEG XL, JPEG 2000, PSD/PSB, TGA, PPM, HEIC, QOI, DDS, PCX, MNG, CUR, …;
camera RAW (CR2, CR3, NEF, ARW, DNG, …), DICOM, OpenEXR, HDR, FITS, JPEG XR (Windows), iPhone Live Photo (LIVP), Insta360 INSP, Windows program icons (EXE, DLL), Valve Texture VTF (Source engine, Garry's Mod), animated cursor (ANI) read;
GIMP XCF and Paint.NET PDN (layers kept between XCF, PDN and PSD) | any of these (HEIC⁵), GIMP XCF, Paint.NET PDN, VTF, PDF, SVG (traced), resized, grayscale;
Live/Motion Photos (iPhone, Android) → their video (MP4, MOV⁵), and a video → Live Photo (iPhone) or Motion Photo (Android)⁵, experimental | | **Vector**: SVG, SVGZ, EMF, WMF, AI, Visio VSDX, ODG, FODG
EPS, PS¹
CDR, binary Visio VSD, CGM²
Obsidian Canvas (JSON Canvas) and Excel chart templates (CRTX) read | PDF (vector), PNG and other images,
SVG ↔ SVGZ, SVG → DXF/DWG, PDF → EPS/PS, SVG → EMF, WMF, ODG²;
Obsidian Canvas → its text as Markdown | | **PDF** | DOCX, XLSX (tables), HWPX, TXT, PNG, JPG, TIFF, CBZ, PPTX (editable slides, or a picture of each page), SVG², PDF/A¹;
merge (in an order you choose first), split (by pages, every N pages or into equal parts), page ranges, compress (smaller PDFs) | -| **Documents**: DOCX, DOCM, DOTX, DOC, DOT, ODT, EML, Outlook MSG;
OFD (Chinese e-invoices, official documents), UYAP UDF (Turkish court documents), XPS / OpenXPS and FreeMind / Freeplane / XMind mind maps (as outlines) read
FODT, OTT, RTF, WPS, WPD, Pages, ClarisWorks, MacWrite, T602, …² | PDF, DOCX, ODT, HWPX, EPUB, TXT, HTML, PPTX and ODP slides;
DOCX, HTML tables → XLSX, ODS, CSV;
MSG → EML
FODT, OTT² | +| **Documents**: DOCX, DOCM, DOTX, DOC, DOT, ODT, EML, Outlook MSG;
OFD (Chinese e-invoices, official documents), UYAP UDF (Turkish court documents), XPS / OpenXPS, DjVu (scanned books) and FreeMind / Freeplane / XMind mind maps (as outlines) read
FODT, OTT, RTF, WPS, WPD, Pages, ClarisWorks, MacWrite, T602, …² | PDF, DOCX, ODT, HWPX, EPUB, TXT, HTML, PPTX and ODP slides;
DOCX, HTML tables → XLSX, ODS, CSV;
MSG → EML
FODT, OTT² | | **Spreadsheets**: XLSX, XLSM, XLSB, XLS, XLT, ODS, CSV, TSV
OTS, FODS, Numbers, ET² | PDF, XLSX, ODS, CSV, TSV, JSON, HWPX, HTML
XLT, OTS² | | **Presentations**: PPTX, PPTM, POTX, POTM, PPSX, PPT, ODP, OTP, FODP
PowerPoint 95 PPT, Keynote² | PDF, PNG, JPG, ODP, DOCX, HWPX; PPT → PPTX
OTP² | | **HWP**: HWP, HWPX | PDF, PNG, DOCX, HWPX ↔ HWP, TXT, Markdown, HTML, PPTX and ODP slides | diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 8046fdbd1..444852f25 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -33,6 +33,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, HWPX (→ PDF), TXT, HTML, Markdown, FB2, HTMLZ, TXTZ**; HTML, Markdown, TXT, DOCX → EPUB/AZW3/MOBI/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, 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); 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)) | | 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) | @@ -4238,11 +4239,44 @@ SRT or SAMI file in CP949 comes out as UTF-8). ## DjVu -Not supported: DjVuLibre (GPL) has no pinned, redistributable Windows build in an archive the installer can unpack. -The official Windows release is an NSIS installer (`DjVuLibre-3.5.29_DjView-4.12_Setup.exe` on SourceForge), which -would have to be run (a system install) or unpacked with 7-Zip; MSYS2's packages are `.pkg.tar.zst` archives with -five dependency packages. A DjVu engine needs a zip of `ddjvu.exe` / `djvutxt.exe` and their DLLs published for -Filee (with the GPL source offer), then an `engines.json` component and an optional package. +Built in (#346), in `Djvu/`, written from the DjVu v3 specification (engine id `djvu`, format id `djvu` for `.djvu` +and `.djv`, documents donut). `DjvuConverter` turns a DjVu document into **PDF** (every page) and into **PNG, JPG and +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. +- **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`. +- **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 + 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). +- **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 + 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. +- 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). ## FFmpeg diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index 111ab5f92..006f99070 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "الملف داخل ملف .p7m هذا من نوع لا يحوّله Filee", "error.udf_not_uyap": "ملف ‎.udf هذا ليس مستند UYAP. إذا كان صورة قرص (Universal Disk Format)، فافتحه باستخدام «استخراج»", "error.chart_template_unsupported": "لا يرسم Filee هذا النوع من المخططات بعد (راداري، فقاعي، أسهم، سطحي، شريط من دائري، أو نوع أحدث مثل الشلال أو الخريطة الشجرية)", + "error.djvu_indirect": "هذا مستند DjVu غير مباشر: صفحاته ملفات منفصلة. اجمعه في ملف واحد باستخدام عارض DjVu (مثل DjView: الحفظ كملف مجمّع)، ثم حوّل ذلك الملف", "error.p7m_too_large": "الملف الموقّع أكبر من أن يُفتح", "error.document_too_large": "يتوسّع هذا المستند إلى بيانات أكثر من اللازم أو أنه متداخل بعمق شديد بحيث لا يمكن فتحه بأمان", "format_request.request": "طلب دعم هذه الصيغة", @@ -321,6 +322,8 @@ "engine.gcode.description": "مسارات الطابعات ثلاثية الأبعاد وآلات CNC (G-code) إلى PDF وSVG وصور، أو معاينة برنامج التقطيع.", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "تحويل رسومات Visio (VSDX) ورسومات OpenDocument (ODG وFODG) إلى PDF وSVG وPNG دون Visio أو LibreOffice.\nوكذلك مستندات XPS وOpenXPS (.xps و.oxps).\nوكذلك قوالب مخططات Excel (.crtx).\nلا تزال ملفات Visio الثنائية القديمة (.vsd) تحتاج إلى LibreOffice.", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "كتب ومستندات DjVu الممسوحة ضوئيًا (.djvu، .djv) إلى PDF مع إمكانية البحث في نصها المخفي، وإلى صور PNG وJPG وTIFF للصفحات.", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "التحويل بين النماذج ثلاثية الأبعاد: GLB وglTF وSTL وOBJ و3MF وPLY، وتحويل FBX إلى أي منها، وإنشاء معاينة PNG.", "engine.image3d.name": "صور إلى نقش بارز ثلاثي الأبعاد (مدمج)", diff --git a/src/Filee.App/Assets/i18n/bn-BD.json b/src/Filee.App/Assets/i18n/bn-BD.json index 11ea3129b..2005e6916 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -126,6 +126,7 @@ "error.p7m_unknown_content": "এই .p7m-এর ভেতরের ফাইলটি এমন ধরনের যা ফাইলি রূপান্তর করে না", "error.udf_not_uyap": "এই .udf ফাইলটি UYAP ডকুমেন্ট নয়। এটি ডিস্ক ইমেজ (Universal Disk Format) হলে «নিষ্কাশন করুন» দিয়ে খুলুন", "error.chart_template_unsupported": "Filee এখনও এই ধরনের চার্ট আঁকে না (রাডার, বাবল, স্টক, সারফেস, পাই-এর বার, বা ওয়াটারফল বা ট্রিম্যাপের মতো নতুন ধরন)", + "error.djvu_indirect": "এটি একটি পরোক্ষ DjVu নথি: এর পাতাগুলো আলাদা ফাইল। একটি DjVu ভিউয়ারে এটিকে এক ফাইলে জুড়ে নিন (যেমন DjView: বান্ডেল হিসেবে সংরক্ষণ), তারপর সেই ফাইলটি রূপান্তর করুন", "error.p7m_too_large": "স্বাক্ষরিত ফাইলটি খোলার জন্য অতিরিক্ত বড়", "error.document_too_large": "এই নথিটি খুললে অতিরিক্ত ডেটা তৈরি হয় বা এটি অতিরিক্ত গভীরভাবে নেস্ট করা, তাই নিরাপদে খোলা যাচ্ছে না", "format_request.request": "এই ফরম্যাটের অনুরোধ করুন", @@ -335,6 +336,8 @@ "engine.gcode.description": "3D প্রিন্টার ও CNC মেশিনের টুলপাথ (G-code) PDF, SVG ও ছবিতে, অথবা স্লাইসারের নিজের প্রিভিউ।", "engine.drawing.name": "ড্রয়িং (বিল্ট-ইন)", "engine.drawing.description": "Visio ড্রয়িং (VSDX) ও OpenDocument ড্রয়িং (ODG, FODG) থেকে পিডিএফ, SVG ও PNG, Visio বা LibreOffice ছাড়াই।\nXPS ও OpenXPS ডকুমেন্টও (.xps, .oxps)।\nExcel চার্ট টেমপ্লেটও (.crtx)।\nপুরোনো বাইনারি Visio ফাইলের (.vsd) জন্য এখনও LibreOffice লাগে।", + "engine.djvu.name": "DjVu (বিল্ট-ইন)", + "engine.djvu.description": "DjVu স্ক্যান করা বই ও নথি (.djvu, .djv) লুকানো লেখা খোঁজা যায় এমন PDF-এ, এবং পাতার PNG, JPG ও TIFF ছবিতে।", "engine.model3d.name": "3D মডেল (বিল্ট-ইন)", "engine.model3d.description": "3D মডেল: GLB, glTF, STL, OBJ, 3MF এবং PLY একে অপরে রূপান্তর, যেকোনোটিতে FBX রূপান্তর এবং একটি PNG প্রিভিউ।", "engine.image3d.name": "ছবি থেকে 3D রিলিফ (বিল্ট-ইন)", diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.json index 31d8f9826..e932ea74d 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "Die Datei in dieser .p7m hat ein Format, das Filee nicht konvertiert", "error.udf_not_uyap": "Diese .udf-Datei ist kein UYAP-Dokument. Ist sie ein Disk-Image (Universal Disk Format), öffnen Sie sie mit „Entpacken“", "error.chart_template_unsupported": "Diese Diagrammart zeichnet Filee noch nicht (Netz, Blase, Kurs, Oberfläche, Balken aus Kreis oder ein neuerer Typ wie Wasserfall oder Treemap)", + "error.djvu_indirect": "Dies ist ein indirektes DjVu-Dokument: Seine Seiten sind einzelne Dateien. Fassen Sie es in einem DjVu-Viewer zu einer Datei zusammen (z. B. DjView: als gebündelt speichern) und konvertieren Sie dann diese Datei", "error.p7m_too_large": "Die signierte Datei ist zu groß zum Öffnen", "error.document_too_large": "Dieses Dokument entpackt sich zu zu vielen Daten oder ist zu tief verschachtelt, um es sicher zu öffnen", "format_request.request": "Dieses Format vorschlagen", @@ -321,6 +322,8 @@ "engine.gcode.description": "Werkzeugwege von 3D-Druckern und CNC-Maschinen (G-Code) als PDF, SVG und Bild, oder die Vorschau des Slicers.", "engine.drawing.name": "Zeichnungen (integriert)", "engine.drawing.description": "Visio-Zeichnungen (VSDX) und OpenDocument-Zeichnungen (ODG, FODG) in PDF, SVG und PNG, ohne Visio oder LibreOffice.\nAuch XPS- und OpenXPS-Dokumente (.xps, .oxps).\nAuch Excel-Diagrammvorlagen (.crtx).\nFür alte binäre Visio-Dateien (.vsd) ist weiterhin LibreOffice nötig.", + "engine.djvu.name": "DjVu (integriert)", + "engine.djvu.description": "Gescannte DjVu-Bücher und -Dokumente (.djvu, .djv) in PDF mit durchsuchbarem verborgenem Text sowie in PNG-, JPG- und TIFF-Bilder der Seiten.", "engine.model3d.name": "3D-Modelle (integriert)", "engine.model3d.description": "3D-Modelle: Konvertiert GLB, glTF, STL, OBJ, 3MF und PLY untereinander, FBX in jedes dieser Formate und erstellt eine PNG-Vorschau.", "engine.image3d.name": "Bilder zu 3D-Relief (integriert)", diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json index 98fb4b38c..5f0c15650 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "The file inside this .p7m is of a type Filee doesn't convert", "error.udf_not_uyap": "This .udf file isn't a UYAP document. If it's a disc image (Universal Disk Format), use Extract to unpack it", "error.chart_template_unsupported": "Filee doesn't draw this kind of chart yet (radar, bubble, stock, surface, bar of pie, or a newer type such as waterfall or treemap)", + "error.djvu_indirect": "This is an indirect DjVu document: its pages are separate files. Bundle it into one file in a DjVu viewer (for example DjView: Save as bundled), then convert that file", "error.p7m_too_large": "The signed file is too large to open", "error.document_too_large": "This document unpacks to too much data or is nested too deeply to open safely", "format_request.request": "Request this format", @@ -328,6 +329,8 @@ "engine.gcode.description": "Toolpaths of 3D printers and CNC machines (G-code) to PDF, SVG and pictures, or the slicer's own preview.", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Visio drawings (VSDX) and OpenDocument drawings (ODG, FODG) to PDF, SVG and PNG, without Visio or LibreOffice.\nAlso XPS and OpenXPS documents (.xps, .oxps).\nAlso Excel chart templates (.crtx).\nOld binary Visio files (.vsd) still need LibreOffice.", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "DjVu scanned books and documents (.djvu, .djv) to PDF with their hidden text searchable, and to PNG, JPG and TIFF pictures of the pages.", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "3D models: GLB, glTF, STL, OBJ, 3MF and PLY into each other, FBX into any of them, and a PNG preview.", "engine.image3d.name": "Pictures to 3D relief (built-in)", diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.json index f52142523..907b49da6 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "El archivo dentro de este .p7m es de un tipo que Filee no convierte", "error.udf_not_uyap": "Este archivo .udf no es un documento de UYAP. Si es una imagen de disco (Universal Disk Format), ábrelo con «Extraer»", "error.chart_template_unsupported": "Filee aún no dibuja este tipo de gráfico (radial, de burbujas, de cotizaciones, de superficie, barra de gráfico circular o un tipo nuevo como cascada o rectángulos)", + "error.djvu_indirect": "Es un documento DjVu indirecto: sus páginas son archivos separados. Agrúpelo en un solo archivo en un visor DjVu (por ejemplo DjView: guardar como agrupado) y convierta ese archivo", "error.p7m_too_large": "El archivo firmado es demasiado grande para abrirlo", "error.document_too_large": "Este documento se descomprime en demasiados datos o está anidado a demasiada profundidad para abrirlo de forma segura", "format_request.request": "Solicitar este formato", @@ -321,6 +322,8 @@ "engine.gcode.description": "Trayectorias de impresoras 3D y máquinas CNC (G-code) a PDF, SVG e imágenes, o la vista previa del laminador.", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Dibujos de Visio (VSDX) y de OpenDocument (ODG, FODG) a PDF, SVG y PNG, sin Visio ni LibreOffice.\nTambién documentos XPS y OpenXPS (.xps, .oxps).\nTambién plantillas de gráficos de Excel (.crtx).\nLos archivos binarios antiguos de Visio (.vsd) siguen necesitando LibreOffice.", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "Libros y documentos escaneados DjVu (.djvu, .djv) a PDF con su texto oculto buscable, y a imágenes PNG, JPG y TIFF de las páginas.", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "Modelos 3D: GLB, glTF, STL, OBJ, 3MF y PLY entre sí, FBX a cualquiera de ellos, además de una vista previa en PNG.", "engine.image3d.name": "Imágenes a relieve 3D (integrado)", diff --git a/src/Filee.App/Assets/i18n/fa.json b/src/Filee.App/Assets/i18n/fa.json index 8de6630ad..1a1a7dfcb 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -126,6 +126,7 @@ "error.p7m_unknown_content": "فایل داخل این .p7m از نوعی است که Filee تبدیل نمی‌کند", "error.udf_not_uyap": "این فایل ‎.udf سند UYAP نیست. اگر ایمیج دیسک (Universal Disk Format) است، آن را با «استخراج» باز کنید", "error.chart_template_unsupported": "Filee هنوز این نوع نمودار را نمی‌کشد (راداری، حبابی، سهام، سطحی، میله‌ای از دایره‌ای یا نوعی تازه‌تر مانند آبشاری یا نقشهٔ درختی)", + "error.djvu_indirect": "این یک سند DjVu غیرمستقیم است: صفحه‌هایش فایل‌های جداگانه‌اند. آن را در یک نمایشگر DjVu در یک فایل جمع کنید (مثلاً DjView: ذخیره به‌صورت بسته‌ای) و سپس همان فایل را تبدیل کنید", "error.p7m_too_large": "فایل امضاشده برای باز شدن بیش از حد بزرگ است", "error.document_too_large": "داده‌های استخراج‌شده از این سند بیش از حد مجاز است یا ساختار آن برای باز شدن ایمن بیش از حد تودرتو است", "format_request.request": "درخواست این فرمت", @@ -335,6 +336,8 @@ "engine.gcode.description": "مسیرهای چاپگرهای سه‌بعدی و دستگاه‌های CNC (G-code) به PDF، SVG و تصویر، یا پیش‌نمایش خود اسلایسر.", "engine.drawing.name": "نمودارها و نقشه‌ها (داخلی)", "engine.drawing.description": "نمودارهای Visio (VSDX) و نقشه‌های OpenDocument (ODG، FODG) به PDF، SVG و PNG، بدون Visio یا LibreOffice.\nهمچنین سندهای XPS و OpenXPS (.xps، .oxps).\nهمچنین الگوهای نمودار Excel (.crtx).\nفایل‌های باینری قدیمی Visio (.vsd) همچنان به LibreOffice نیاز دارند.", + "engine.djvu.name": "DjVu (داخلی)", + "engine.djvu.description": "کتاب‌ها و اسناد اسکن‌شدهٔ DjVu (.djvu، .djv) به PDF با متن پنهانِ قابل جست‌وجو، و به تصاویر PNG، JPG و TIFF صفحه‌ها.", "engine.model3d.name": "مدل‌های سه‌بعدی (داخلی)", "engine.model3d.description": "مدل‌های سه‌بعدی: GLB، glTF، STL، OBJ، 3MF و PLY به یکدیگر، FBX به هر یک از آن‌ها، همراه با پیش‌نمایش PNG.", "engine.image3d.name": "تبدیل تصاویر به برجسته‌سازی سه‌بعدی (داخلی)", diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.json index 8ba6306ca..0fc828ade 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "Le fichier contenu dans ce .p7m est d'un type que Filee ne convertit pas", "error.udf_not_uyap": "Ce fichier .udf n'est pas un document UYAP. S'il s'agit d'une image disque (Universal Disk Format), ouvrez-le avec « Extraire »", "error.chart_template_unsupported": "Filee ne dessine pas encore ce type de graphique (radar, bulles, boursier, surface, barres de secteurs ou un type récent comme cascade ou compartimentage)", + "error.djvu_indirect": "Ce document DjVu est indirect : ses pages sont des fichiers séparés. Regroupez-le en un seul fichier dans une visionneuse DjVu (par exemple DjView : enregistrer en groupé), puis convertissez ce fichier", "error.p7m_too_large": "Le fichier signé est trop volumineux pour être ouvert", "error.document_too_large": "Ce document se décompresse en trop de données ou est imbriqué trop profondément pour être ouvert en toute sécurité", "format_request.request": "Demander ce format", @@ -328,6 +329,8 @@ "engine.gcode.description": "Parcours d'imprimantes 3D et de machines CNC (G-code) en PDF, SVG et images, ou l'aperçu du slicer.", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Dessins Visio (VSDX) et OpenDocument (ODG, FODG) en PDF, SVG et PNG, sans Visio ni LibreOffice.\nAussi les documents XPS et OpenXPS (.xps, .oxps).\nAussi les modèles de graphiques Excel (.crtx).\nLes anciens fichiers Visio binaires (.vsd) nécessitent toujours LibreOffice.", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "Livres et documents numérisés DjVu (.djvu, .djv) en PDF avec leur texte caché consultable, et en images PNG, JPG et TIFF des pages.", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "Modèles 3D : GLB, glTF, STL, OBJ, 3MF et PLY entre eux, FBX vers chacun d'eux, et un aperçu PNG.", "engine.image3d.name": "Images vers relief 3D (intégré)", diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.json index f6965d2f9..7624a5310 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "इस .p7m के अंदर की फ़ाइल ऐसे प्रकार की है जिसे Filee नहीं बदलता", "error.udf_not_uyap": "यह .udf फ़ाइल UYAP दस्तावेज़ नहीं है। अगर यह डिस्क इमेज (Universal Disk Format) है, तो इसे «निकालें» से खोलें", "error.chart_template_unsupported": "Filee अभी इस तरह का चार्ट नहीं बनाता (रडार, बबल, स्टॉक, सरफ़ेस, पाई का बार, या वॉटरफ़ॉल या ट्रीमैप जैसा नया प्रकार)", + "error.djvu_indirect": "यह एक अप्रत्यक्ष DjVu दस्तावेज़ है: इसके पेज अलग-अलग फ़ाइलें हैं। इसे किसी DjVu व्यूअर में एक फ़ाइल में जोड़ें (जैसे DjView: बंडल के रूप में सहेजें), फिर उस फ़ाइल को बदलें", "error.p7m_too_large": "साइन की गई फ़ाइल खोलने के लिए बहुत बड़ी है", "error.document_too_large": "यह दस्तावेज़ खोलने पर बहुत ज़्यादा डेटा बनता है या बहुत गहराई तक नेस्टेड है, इसलिए इसे सुरक्षित रूप से नहीं खोला जा सकता", "format_request.request": "इस फ़ॉर्मैट का अनुरोध करें", @@ -321,6 +322,8 @@ "engine.gcode.description": "3D प्रिंटर और CNC मशीनों के टूलपाथ (G-code) को PDF, SVG और चित्र में, या स्लाइसर का अपना प्रीव्यू।", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Visio ड्रॉइंग (VSDX) और OpenDocument ड्रॉइंग (ODG, FODG) से PDF, SVG और PNG, बिना Visio या LibreOffice के।\nXPS और OpenXPS दस्तावेज़ (.xps, .oxps) भी।\nExcel चार्ट टेम्पलेट (.crtx) भी।\nपुरानी बाइनरी Visio फ़ाइलों (.vsd) के लिए अब भी LibreOffice चाहिए।", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "DjVu स्कैन की गई किताबें और दस्तावेज़ (.djvu, .djv) को खोजे जा सकने वाले छिपे टेक्स्ट सहित PDF में, और पेजों की PNG, JPG व TIFF तस्वीरों में।", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "3D मॉडल: GLB, glTF, STL, OBJ, 3MF और PLY एक-दूसरे में, FBX इनमें से किसी में भी, और PNG प्रीव्यू।", "engine.image3d.name": "चित्र से 3D रिलीफ़ (बिल्ट-इन)", diff --git a/src/Filee.App/Assets/i18n/id.json b/src/Filee.App/Assets/i18n/id.json index 9030c5343..d91a6ee55 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "File di dalam .p7m ini berjenis yang tidak dikonversi Filee", "error.udf_not_uyap": "File .udf ini bukan dokumen UYAP. Jika ini image disk (Universal Disk Format), buka dengan “Ekstrak”", "error.chart_template_unsupported": "Filee belum dapat menggambar jenis bagan ini (radar, gelembung, saham, permukaan, batang dari pai, atau jenis baru seperti air terjun atau peta pohon)", + "error.djvu_indirect": "Ini dokumen DjVu tidak langsung: halamannya berupa file terpisah. Gabungkan menjadi satu file di penampil DjVu (misalnya DjView: simpan sebagai bundel), lalu konversi file tersebut", "error.p7m_too_large": "File bertanda tangan terlalu besar untuk dibuka", "error.document_too_large": "Dokumen ini terurai menjadi data yang terlalu besar atau bersarang terlalu dalam untuk dibuka dengan aman", "format_request.request": "Minta format ini", @@ -321,6 +322,8 @@ "engine.gcode.description": "Jalur printer 3D dan mesin CNC (G-code) ke PDF, SVG, dan gambar, atau pratinjau dari slicer.", "engine.drawing.name": "Diagram (bawaan)", "engine.drawing.description": "Diagram Visio (VSDX) dan gambar OpenDocument (ODG, FODG) ke PDF, SVG, dan PNG, tanpa Visio atau LibreOffice.\nJuga dokumen XPS dan OpenXPS (.xps, .oxps).\nJuga templat bagan Excel (.crtx).\nFile Visio biner lama (.vsd) masih memerlukan LibreOffice.", + "engine.djvu.name": "DjVu (bawaan)", + "engine.djvu.description": "Buku dan dokumen pindaian DjVu (.djvu, .djv) ke PDF dengan teks tersembunyi yang bisa dicari, serta ke gambar halaman PNG, JPG, dan TIFF.", "engine.model3d.name": "Model 3D (bawaan)", "engine.model3d.description": "Model 3D: GLB, glTF, STL, OBJ, 3MF, dan PLY saling konversi, FBX ke format mana pun, serta pratinjau gambar PNG.", "engine.image3d.name": "Gambar ke relief 3D (bawaan)", diff --git a/src/Filee.App/Assets/i18n/it.json b/src/Filee.App/Assets/i18n/it.json index 3d900fcd2..034d9f808 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -111,6 +111,7 @@ "error.p7m_unknown_content": "Il file dentro questo .p7m è di un tipo che Filee non converte", "error.udf_not_uyap": "Questo file .udf non è un documento UYAP. Se è un'immagine disco (Universal Disk Format), aprilo con «Estrai»", "error.chart_template_unsupported": "Filee non disegna ancora questo tipo di grafico (radar, a bolle, azionario, a superficie, barra di torta o un tipo più recente come a cascata o treemap)", + "error.djvu_indirect": "È un documento DjVu indiretto: le sue pagine sono file separati. Riuniscilo in un unico file con un visualizzatore DjVu (ad esempio DjView: salva come raggruppato), poi converti quel file", "error.p7m_too_large": "Il file firmato è troppo grande da aprire", "error.document_too_large": "Questo documento si decomprime in troppi dati o è annidato troppo in profondità per essere aperto in sicurezza", "format_request.request": "Richiedi questo formato", @@ -320,6 +321,8 @@ "engine.gcode.description": "Percorsi di stampanti 3D e macchine CNC (G-code) in PDF, SVG e immagini, o l'anteprima dello slicer.", "engine.drawing.name": "Disegni (integrato)", "engine.drawing.description": "Disegni Visio (VSDX) e OpenDocument (ODG, FODG) in PDF, SVG e PNG, senza Visio né LibreOffice.\nAnche documenti XPS e OpenXPS (.xps, .oxps).\nAnche modelli di grafico di Excel (.crtx).\nI vecchi file binari di Visio (.vsd) richiedono ancora LibreOffice.", + "engine.djvu.name": "DjVu (integrato)", + "engine.djvu.description": "Libri e documenti scansionati DjVu (.djvu, .djv) in PDF con il testo nascosto ricercabile, e in immagini PNG, JPG e TIFF delle pagine.", "engine.model3d.name": "Modelli 3D (integrato)", "engine.model3d.description": "Modelli 3D: GLB, glTF, STL, OBJ, 3MF e PLY convertibili tra loro, FBX in uno qualsiasi di essi, e un'anteprima PNG.", "engine.image3d.name": "Immagini in rilievo 3D (integrato)", diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json index 890ee52c3..067c4cbe1 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "この .p7m の中のファイルは、Filee が変換できない形式です", "error.udf_not_uyap": "この .udf ファイルは UYAP 文書ではありません。ディスクイメージ (Universal Disk Format) なら「展開」で開いてください", "error.chart_template_unsupported": "この種類のグラフはまだ描画できません(レーダー、バブル、株価、等高線、補助縦棒付き円、ウォーターフォールやツリーマップなどの新しいグラフ)", + "error.djvu_indirect": "ページが別々のファイルになった間接形式のDjVu文書です。DjVuビューアー(例:DjViewの「バンドル形式で保存」)で1つのファイルにまとめてから変換してください", "error.p7m_too_large": "署名付きファイルが大きすぎて開けません", "error.document_too_large": "この文書は展開するとデータが大きすぎるか、入れ子が深すぎるため、安全に開けません", "format_request.request": "この形式への対応をリクエスト", @@ -321,6 +322,8 @@ "engine.gcode.description": "3D プリンターと CNC マシンのツールパス (G-code) を PDF、SVG、画像に。スライサーのプレビューも使えます。", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Visio図面(VSDX)とOpenDocument図面(ODG、FODG)を、VisioやLibreOfficeなしでPDF、SVG、PNGに変換します。\nXPS・OpenXPS文書(.xps、.oxps)にも対応しています。\nExcel のグラフ テンプレート(.crtx)にも対応しています。\n古いバイナリ形式のVisioファイル(.vsd)には引き続きLibreOfficeが必要です。", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "DjVuのスキャン本や文書(.djvu、.djv)を、隠しテキストを検索できるPDFに、またページのPNG・JPG・TIFF画像に変換します。", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "3Dモデル(GLB、glTF、STL、OBJ、3MF、PLY)を相互変換します。FBXから各形式への変換と、PNGプレビューの作成にも対応します。", "engine.image3d.name": "画像から3Dレリーフ(内蔵)", diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json index e1dae63ca..b22f5679e 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "이 .p7m 안의 파일은 Filee가 변환하지 않는 형식이에요", "error.udf_not_uyap": "이 .udf 파일은 UYAP 문서가 아니에요. 디스크 이미지(Universal Disk Format)라면 압축 풀기로 열어 주세요", "error.chart_template_unsupported": "이 종류의 차트는 아직 그리지 못해요(방사형, 거품형, 주식형, 표면형, 원형 대 막대형, 폭포·트리맵 같은 새 차트)", + "error.djvu_indirect": "페이지가 따로따로 된 간접(indirect) DjVu 문서예요. DjVu 뷰어(예: DjView의 '묶음으로 저장')에서 한 파일로 묶은 다음 그 파일을 변환하세요", "error.p7m_too_large": "서명 파일이 너무 커서 열 수 없어요", "error.document_too_large": "이 문서는 풀면 데이터가 너무 크거나 너무 깊이 중첩되어 있어서 안전하게 열 수 없어요", "format_request.request": "이 형식 요청하기", @@ -328,6 +329,8 @@ "engine.gcode.description": "3D 프린터와 CNC 기계의 툴패스(G-code)를 PDF, SVG, 그림으로 바꿔요. 슬라이서가 저장한 미리보기도 쓸 수 있어요.", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Visio 도면(VSDX)과 오픈도큐먼트 도면(ODG, FODG)을 Visio나 LibreOffice 없이 PDF, SVG, PNG로 바꿔요.\nXPS·OpenXPS 문서(.xps, .oxps)도 바꿔요.\nExcel 차트 서식 파일(.crtx)도 바꿔요.\n오래된 바이너리 Visio 파일(.vsd)은 여전히 LibreOffice가 필요해요.", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "DjVu 스캔 책·문서(.djvu, .djv)를 숨은 텍스트까지 검색되는 PDF로, 페이지를 PNG·JPG·TIFF 그림으로 바꿔요.", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "3D 모델: GLB, glTF, STL, OBJ, 3MF, PLY를 서로 변환하고, FBX도 이 형식들로 바꾸고, PNG 미리보기도 만들어요.", "engine.image3d.name": "이미지를 3D 부조로 (내장)", diff --git a/src/Filee.App/Assets/i18n/mn.json b/src/Filee.App/Assets/i18n/mn.json index f3d78b428..8a6dcb77e 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -111,6 +111,7 @@ "error.p7m_unknown_content": "Энэ .p7m доторх файл нь Filee хөрвүүлдэггүй төрлийн файл байна", "error.udf_not_uyap": "Энэ .udf файл UYAP баримт биш. Хэрэв дискний дүрс (Universal Disk Format) бол «Задлах»-аар нээнэ үү", "error.chart_template_unsupported": "Filee энэ төрлийн диаграмыг одоогоор зурахгүй (радар, бөмбөлөг, хувьцаа, гадаргуу, бялуугийн багана эсвэл хүрхрээ, модон зураглал зэрэг шинэ төрөл)", + "error.djvu_indirect": "Энэ бол шууд бус DjVu баримт: хуудсууд нь тусдаа файлууд. DjVu үзэгч дээр нэг файл болгон нэгтгээд (жишээ нь DjView: багцаар хадгалах), дараа нь тэр файлыг хөрвүүлнэ үү", "error.p7m_too_large": "Гарын үсэгтэй файл нээхэд хэт том байна", "error.document_too_large": "Энэ баримт нь задлахад хэт их өгөгдөлтэй эсвэл аюулгүй нээхэд хэт гүн давхарлагдсан байна", "format_request.request": "Энэ форматыг хүсэх", @@ -320,6 +321,8 @@ "engine.gcode.description": "3D принтер болон CNC машины замыг (G-code) PDF, SVG, зураг руу, эсвэл слайсерын өөрийн урьдчилан харах зураг руу.", "engine.drawing.name": "Зураг төсөл (суурилагдсан)", "engine.drawing.description": "Visio зураг (VSDX) болон OpenDocument зургийг (ODG, FODG) Visio эсвэл LibreOffice-гүйгээр PDF, SVG болон PNG болгоно.\nМөн XPS болон OpenXPS баримт бичиг (.xps, .oxps).\nМөн Excel диаграмын загвар (.crtx).\nХуучин хоёртын Visio файлд (.vsd) LibreOffice хэрэгтэй хэвээр.", + "engine.djvu.name": "DjVu (суурилагдсан)", + "engine.djvu.description": "DjVu скан хийсэн ном, баримтыг (.djvu, .djv) далд текстээр нь хайж болох PDF болон хуудсуудын PNG, JPG, TIFF зураг болгоно.", "engine.model3d.name": "3D загвар (суурилагдсан)", "engine.model3d.description": "3D загварууд: GLB, glTF, STL, OBJ, 3MF болон PLY хоорондоо, FBX-ийг эдгээрийн аль руу ч хөрвүүлэх ба PNG урьдчилж харах зураг үүсгэнэ.", "engine.image3d.name": "Зургийг 3D рельеф болгох (суурилагдсан)", diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.json index 14b2c4002..8fb2fdce5 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "यो .p7m भित्रको फाइल Filee ले रूपान्तरण नगर्ने प्रकारको हो", "error.udf_not_uyap": "यो .udf फाइल UYAP कागजात होइन। यो डिस्क इमेज (Universal Disk Format) हो भने «निकाल्नुहोस्» ले खोल्नुहोस्", "error.chart_template_unsupported": "Filee ले अझै यस्तो चार्ट कोर्दैन (राडार, बबल, स्टक, सतह, पाईको बार, वा वाटरफल वा ट्रीम्याप जस्ता नयाँ प्रकार)", + "error.djvu_indirect": "यो अप्रत्यक्ष DjVu कागजात हो: यसका पानाहरू छुट्टाछुट्टै फाइल हुन्। यसलाई कुनै DjVu भ्युअरमा एउटै फाइलमा जोड्नुहोस् (जस्तै DjView: बन्डलको रूपमा सेभ), त्यसपछि त्यो फाइल रूपान्तरण गर्नुहोस्", "error.p7m_too_large": "हस्ताक्षरित फाइल खोल्न धेरै ठूलो छ", "error.document_too_large": "यो कागजात खोल्दा धेरै ठूलो डेटा बन्छ वा धेरै गहिरोसम्म नेस्ट गरिएको छ, त्यसैले सुरक्षित रूपमा खोल्न सकिँदैन", "error.merge_paper_geometry": "पृष्ठको आकार, घुमाइ वा एकाइ मान्य छैन, त्यसैले यसलाई छानिएको कागजमा मिलाउन सकिँदैन", @@ -321,6 +322,8 @@ "engine.gcode.description": "3D प्रिन्टर र CNC मेसिनका टूलपाथ (G-code) लाई PDF, SVG र चित्रमा, वा स्लाइसरकै पूर्वावलोकन।", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Visio रेखाचित्र (VSDX) र OpenDocument रेखाचित्र (ODG, FODG) बाट PDF, SVG र PNG, Visio वा LibreOffice बिना।\nXPS र OpenXPS कागजातहरू (.xps, .oxps) पनि।\nExcel चार्ट टेम्प्लेटहरू (.crtx) पनि।\nपुराना बाइनरी Visio फाइलहरू (.vsd) का लागि अझै LibreOffice चाहिन्छ।", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "DjVu स्क्यान गरिएका किताब र कागजात (.djvu, .djv) लाई लुकेको पाठ खोज्न मिल्ने PDF मा, र पानाहरूको PNG, JPG र TIFF तस्बिरमा।", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "थ्रीडी मोडलहरू: GLB, glTF, STL, OBJ, 3MF र PLY एकअर्कामा, FBX यीमध्ये कुनैमा पनि, र PNG पूर्वावलोकन।", "engine.image3d.name": "चित्रबाट थ्रीडी रिलिफ (बिल्ट-इन)", diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.json index 658fb1946..1d278424d 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "Plik w tym .p7m jest typu, którego Filee nie konwertuje", "error.udf_not_uyap": "Ten plik .udf nie jest dokumentem UYAP. Jeśli to obraz dysku (Universal Disk Format), otwórz go poleceniem „Rozpakuj”", "error.chart_template_unsupported": "Filee nie rysuje jeszcze tego rodzaju wykresu (radarowy, bąbelkowy, giełdowy, powierzchniowy, słupkowy kołowego lub nowszy typ, np. kaskadowy czy mapa drzewa)", + "error.djvu_indirect": "To pośredni dokument DjVu: jego strony są osobnymi plikami. Połącz go w jeden plik w przeglądarce DjVu (np. DjView: zapisz jako pakiet), a potem przekonwertuj ten plik", "error.p7m_too_large": "Podpisany plik jest za duży, by go otworzyć", "error.document_too_large": "Ten dokument rozpakowuje się do zbyt dużej ilości danych lub jest zagnieżdżony zbyt głęboko, by bezpiecznie go otworzyć", "error.merge_paper_geometry": "Strona ma nieprawidłowy rozmiar, obrót lub jednostki i nie można jej dopasować do wybranego papieru", @@ -328,6 +329,8 @@ "engine.gcode.description": "Ścieżki drukarek 3D i maszyn CNC (G-code) do PDF, SVG i obrazów albo podgląd zapisany przez slicer.", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Rysunki Visio (VSDX) i OpenDocument (ODG, FODG) do PDF, SVG i PNG, bez Visio i LibreOffice.\nTakże dokumenty XPS i OpenXPS (.xps, .oxps).\nTakże szablony wykresów Excela (.crtx).\nStare binarne pliki Visio (.vsd) nadal wymagają LibreOffice.", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "Zeskanowane książki i dokumenty DjVu (.djvu, .djv) do PDF z przeszukiwalnym ukrytym tekstem oraz do obrazów stron PNG, JPG i TIFF.", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "Modele 3D: GLB, glTF, STL, OBJ, 3MF i PLY między sobą, FBX do każdego z nich oraz podgląd PNG.", "engine.image3d.name": "Obrazy na płaskorzeźbę 3D (wbudowany)", diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.json index 628fade4a..95f5a4a69 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "O arquivo dentro deste .p7m é de um tipo que o Filee não converte", "error.udf_not_uyap": "Este arquivo .udf não é um documento do UYAP. Se for uma imagem de disco (Universal Disk Format), abra-o com “Extrair”", "error.chart_template_unsupported": "O Filee ainda não desenha este tipo de gráfico (radar, bolhas, ações, superfície, barra de pizza ou um tipo mais novo como cascata ou mapa de árvore)", + "error.djvu_indirect": "Este é um documento DjVu indireto: as páginas são arquivos separados. Junte-o em um único arquivo em um visualizador DjVu (por exemplo DjView: salvar como agrupado) e converta esse arquivo", "error.p7m_too_large": "O arquivo assinado é grande demais para abrir", "error.document_too_large": "Este documento se descompacta em dados demais ou está aninhado fundo demais para ser aberto com segurança", "error.merge_paper_geometry": "A página tem tamanho, rotação ou unidades inválidos e não pode ser ajustada ao papel selecionado", @@ -328,6 +329,8 @@ "engine.gcode.description": "Percursos de impressoras 3D e máquinas CNC (G-code) para PDF, SVG e imagens, ou a prévia do fatiador.", "engine.drawing.name": "Desenhos (mecanismo integrado)", "engine.drawing.description": "Desenhos do Visio (VSDX) e do OpenDocument (ODG, FODG) para PDF, SVG e PNG, sem Visio nem LibreOffice.\nTambém documentos XPS e OpenXPS (.xps, .oxps).\nTambém modelos de gráfico do Excel (.crtx).\nArquivos binários antigos do Visio (.vsd) ainda precisam do LibreOffice.", + "engine.djvu.name": "DjVu (mecanismo integrado)", + "engine.djvu.description": "Livros e documentos digitalizados DjVu (.djvu, .djv) para PDF com o texto oculto pesquisável, e para imagens PNG, JPG e TIFF das páginas.", "engine.model3d.name": "Modelos 3D (mecanismo integrado)", "engine.model3d.description": "Modelos 3D: GLB, glTF, STL, OBJ, 3MF e PLY entre si, FBX para qualquer um deles, e uma prévia em PNG.", "engine.image3d.name": "Imagens para relevo 3D (mecanismo integrado)", diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.json index 09302ed56..d281841f4 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "O ficheiro dentro deste .p7m é de um tipo que o Filee não converte", "error.udf_not_uyap": "Este ficheiro .udf não é um documento do UYAP. Se for uma imagem de disco (Universal Disk Format), abra-o com «Extrair»", "error.chart_template_unsupported": "O Filee ainda não desenha este tipo de gráfico (radar, bolhas, cotações, superfície, barra de circular ou um tipo mais recente como cascata ou mapa de árvore)", + "error.djvu_indirect": "Este é um documento DjVu indireto: as páginas são ficheiros separados. Junte-o num único ficheiro num visualizador DjVu (por exemplo DjView: guardar como agrupado) e converta esse ficheiro", "error.p7m_too_large": "O ficheiro assinado é demasiado grande para abrir", "error.document_too_large": "Este documento descomprime-se em demasiados dados ou está aninhado demasiado fundo para ser aberto em segurança", "error.merge_paper_geometry": "A página tem um tamanho, uma rotação ou unidades inválidos e não pode ser ajustada ao papel selecionado", @@ -328,6 +329,8 @@ "engine.gcode.description": "Percursos de impressoras 3D e máquinas CNC (G-code) para PDF, SVG e imagens, ou a pré-visualização do fatiador.", "engine.drawing.name": "Desenhos (motor integrado)", "engine.drawing.description": "Desenhos do Visio (VSDX) e do OpenDocument (ODG, FODG) para PDF, SVG e PNG, sem Visio nem LibreOffice.\nTambém documentos XPS e OpenXPS (.xps, .oxps).\nTambém modelos de gráfico do Excel (.crtx).\nOs ficheiros binários antigos do Visio (.vsd) continuam a precisar do LibreOffice.", + "engine.djvu.name": "DjVu (motor integrado)", + "engine.djvu.description": "Livros e documentos digitalizados DjVu (.djvu, .djv) para PDF com o texto oculto pesquisável, e para imagens PNG, JPG e TIFF das páginas.", "engine.model3d.name": "Modelos 3D (motor integrado)", "engine.model3d.description": "Converte modelos 3D entre GLB, glTF, STL, OBJ, 3MF e PLY, de FBX para qualquer um destes formatos e para uma pré-visualização PNG.", "engine.image3d.name": "Imagens para relevo 3D (motor integrado)", diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json index d74ffafdf..6f004ac1b 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "Файл внутри этого .p7m имеет тип, который Filee не конвертирует", "error.udf_not_uyap": "Этот файл .udf не является документом UYAP. Если это образ диска (Universal Disk Format), откройте его командой «Распаковать»", "error.chart_template_unsupported": "Filee пока не рисует такие диаграммы (лепестковые, пузырьковые, биржевые, поверхностные, вторичные линейчатые или новые типы, например каскадные и древовидные)", + "error.djvu_indirect": "Это непрямой (indirect) документ DjVu: его страницы — отдельные файлы. Соберите его в один файл в просмотрщике DjVu (например, DjView: «Сохранить как пакетный»), затем конвертируйте этот файл", "error.p7m_too_large": "Подписанный файл слишком большой, чтобы его открыть", "error.document_too_large": "Этот документ распаковывается в слишком большой объём данных или вложен слишком глубоко, чтобы его можно было безопасно открыть", "error.merge_paper_geometry": "У страницы неверный размер, поворот или единицы измерения — её нельзя вписать в выбранный формат бумаги", @@ -328,6 +329,8 @@ "engine.gcode.description": "Траектории 3D-принтеров и станков с ЧПУ (G-code) в PDF, SVG и изображения или превью слайсера.", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "Схемы Visio (VSDX) и рисунки OpenDocument (ODG, FODG) — в PDF, SVG и PNG без Visio и LibreOffice.\nА также документы XPS и OpenXPS (.xps, .oxps).\nА также шаблоны диаграмм Excel (.crtx).\nДля старых двоичных файлов Visio (.vsd) по-прежнему нужен LibreOffice.", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "Отсканированные книги и документы DjVu (.djvu, .djv) в PDF с поиском по скрытому тексту, а также в изображения страниц PNG, JPG и TIFF.", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "3D-модели: GLB, glTF, STL, OBJ, 3MF и PLY друг в друга, FBX — в любой из них, плюс превью в PNG.", "engine.image3d.name": "Изображения в 3D-рельеф (встроенный)", diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.json index 338a98ec3..2d42faf12 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "ไฟล์ใน .p7m นี้เป็นชนิดที่ Filee ไม่แปลง", "error.udf_not_uyap": "ไฟล์ .udf นี้ไม่ใช่เอกสาร UYAP หากเป็นอิมเมจดิสก์ (Universal Disk Format) ให้เปิดด้วย “แตกไฟล์”", "error.chart_template_unsupported": "Filee ยังวาดแผนภูมิชนิดนี้ไม่ได้ (เรดาร์ ฟอง หุ้น พื้นผิว แท่งของวงกลม หรือชนิดใหม่อย่างน้ำตกหรือทรีแมป)", + "error.djvu_indirect": "นี่คือเอกสาร DjVu แบบแยกไฟล์ (indirect): แต่ละหน้าเป็นไฟล์แยกกัน โปรดรวมเป็นไฟล์เดียวในโปรแกรมดู DjVu (เช่น DjView: บันทึกแบบรวม) แล้วจึงแปลงไฟล์นั้น", "error.p7m_too_large": "ไฟล์ที่ลงนามใหญ่เกินกว่าจะเปิดได้", "error.document_too_large": "เอกสารนี้แตกออกเป็นข้อมูลมากเกินไปหรือซ้อนกันลึกเกินไป จึงเปิดอย่างปลอดภัยไม่ได้", "format_request.request": "ขอให้รองรับฟอร์แมตนี้", @@ -321,6 +322,8 @@ "engine.gcode.description": "เส้นทางของเครื่องพิมพ์ 3 มิติและเครื่อง CNC (G-code) เป็น PDF, SVG และรูปภาพ หรือภาพตัวอย่างของสไลเซอร์", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "แปลงไดอะแกรม Visio (VSDX) และภาพวาด OpenDocument (ODG, FODG) เป็น PDF, SVG และ PNG โดยไม่ต้องใช้ Visio หรือ LibreOffice\nรวมถึงเอกสาร XPS และ OpenXPS (.xps, .oxps)\nรวมถึงเทมเพลตแผนภูมิ Excel (.crtx)\nไฟล์ Visio แบบไบนารีรุ่นเก่า (.vsd) ยังต้องใช้ LibreOffice", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "แปลงหนังสือและเอกสารสแกน DjVu (.djvu, .djv) เป็น PDF ที่ค้นหาข้อความแฝงได้ และเป็นภาพ PNG, JPG, TIFF ของแต่ละหน้า", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "โมเดล 3D: GLB, glTF, STL, OBJ, 3MF และ PLY สลับกันได้ FBX เป็นฟอร์แมตใดก็ได้ และแสดงตัวอย่างเป็น PNG", "engine.image3d.name": "รูปภาพเป็นภาพนูน 3D (ในตัว)", diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.json index 999ce28b5..ef02fbc3d 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -112,6 +112,7 @@ "error.p7m_unknown_content": "Bu .p7m içindeki dosya, Filee'nin dönüştürmediği bir türde", "error.udf_not_uyap": "Bu .udf dosyası bir UYAP belgesi değil. Bir disk görüntüsüyse (Universal Disk Format), açmak için Arşivi aç'ı kullanın", "error.chart_template_unsupported": "Filee bu tür grafiği henüz çizmiyor (radar, kabarcık, hisse senedi, yüzey, pastanın çubuğu ya da şelale veya ağaç haritası gibi yeni bir tür)", + "error.djvu_indirect": "Bu dolaylı bir DjVu belgesi: sayfaları ayrı dosyalar. Bir DjVu görüntüleyicide tek dosyada birleştirin (örneğin DjView: paketlenmiş olarak kaydet), ardından o dosyayı dönüştürün", "error.p7m_too_large": "İmzalı dosya açılamayacak kadar büyük", "error.document_too_large": "Bu belge açıldığında çok fazla veriye dönüşüyor ya da çok derin iç içe geçmiş, güvenle açılamıyor", "format_request.request": "Bu biçimi talep et", @@ -321,6 +322,8 @@ "engine.gcode.description": "3B yazıcıların ve CNC makinelerinin takım yolları (G-code) PDF, SVG ve resme, ya da dilimleyicinin kendi önizlemesi.", "engine.drawing.name": "Çizimler (yerleşik)", "engine.drawing.description": "Visio çizimleri (VSDX) ve OpenDocument çizimleri (ODG, FODG) → PDF, SVG ve PNG; Visio veya LibreOffice gerekmez.\nXPS ve OpenXPS belgeleri (.xps, .oxps) de.\nExcel grafik şablonları (.crtx) da.\nEski ikili Visio dosyaları (.vsd) için hâlâ LibreOffice gerekir.", + "engine.djvu.name": "DjVu (yerleşik)", + "engine.djvu.description": "Taranmış DjVu kitap ve belgelerini (.djvu, .djv) gizli metni aranabilir PDF'ye ve sayfaların PNG, JPG ve TIFF resimlerine dönüştürür.", "engine.model3d.name": "3B modeller (yerleşik)", "engine.model3d.description": "3B modeller: GLB, glTF, STL, OBJ, 3MF ve PLY arasında dönüştürme; FBX'ten bunların tümüne ve PNG önizlemesine dönüştürme.", "engine.image3d.name": "Resimden 3B kabartma (yerleşik)", diff --git a/src/Filee.App/Assets/i18n/vi.json b/src/Filee.App/Assets/i18n/vi.json index 360b5f07d..7309c17ed 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "Tệp bên trong .p7m này thuộc loại Filee không chuyển đổi", "error.udf_not_uyap": "Tệp .udf này không phải tài liệu UYAP. Nếu đây là ảnh đĩa (Universal Disk Format), hãy mở bằng “Giải nén”", "error.chart_template_unsupported": "Filee chưa vẽ được loại biểu đồ này (radar, bong bóng, chứng khoán, bề mặt, thanh của hình tròn hoặc loại mới như thác nước hay bản đồ dạng cây)", + "error.djvu_indirect": "Đây là tài liệu DjVu gián tiếp: các trang là những tệp riêng. Hãy gộp thành một tệp trong trình xem DjVu (ví dụ DjView: lưu dạng gộp), rồi chuyển đổi tệp đó", "error.p7m_too_large": "Tệp đã ký quá lớn để mở", "error.document_too_large": "Tài liệu này giải nén ra quá nhiều dữ liệu hoặc lồng nhau quá sâu nên không thể mở an toàn", "format_request.request": "Yêu cầu định dạng này", @@ -328,6 +329,8 @@ "engine.gcode.description": "Đường chạy của máy in 3D và máy CNC (G-code) sang PDF, SVG và ảnh, hoặc ảnh xem trước của slicer.", "engine.drawing.name": "Bản vẽ (tích hợp)", "engine.drawing.description": "Bản vẽ Visio (VSDX) và bản vẽ OpenDocument (ODG, FODG) sang PDF, SVG và PNG, không cần Visio hay LibreOffice.\nCả tài liệu XPS và OpenXPS (.xps, .oxps).\nCả mẫu biểu đồ Excel (.crtx).\nTệp Visio nhị phân cũ (.vsd) vẫn cần LibreOffice.", + "engine.djvu.name": "DjVu (tích hợp)", + "engine.djvu.description": "Sách và tài liệu quét DjVu (.djvu, .djv) sang PDF tìm kiếm được văn bản ẩn, và sang ảnh PNG, JPG, TIFF của từng trang.", "engine.model3d.name": "Mô hình 3D (tích hợp)", "engine.model3d.description": "Mô hình 3D: GLB, glTF, STL, OBJ, 3MF và PLY chuyển đổi qua lại, FBX sang bất kỳ định dạng nào trong số đó, và ảnh xem trước PNG.", "engine.image3d.name": "Ảnh sang phù điêu 3D (tích hợp)", diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json index ecfbb5bd7..acfc0a1e6 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "这个 .p7m 中的文件是 Filee 不转换的类型", "error.udf_not_uyap": "这个 .udf 文件不是 UYAP 文档。如果它是光盘映像(Universal Disk Format),请用“解压”打开", "error.chart_template_unsupported": "暂不支持绘制此类图表(雷达图、气泡图、股价图、曲面图、复合条饼图,或瀑布图、树状图等新图表类型)", + "error.djvu_indirect": "这是间接 DjVu 文档:各页是单独的文件。请先在 DjVu 阅读器中将其合并为一个文件(例如 DjView:另存为捆绑文档),再转换该文件", "error.p7m_too_large": "已签名的文件太大,无法打开", "error.document_too_large": "此文档解压后数据过多或嵌套过深,无法安全打开", "format_request.request": "请求支持此格式", @@ -328,6 +329,8 @@ "engine.gcode.description": "将 3D 打印机和 CNC 机床的刀路 (G-code) 转为 PDF、SVG 和图片,或使用切片软件保存的预览图。", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "将 Visio 图表(VSDX)和 OpenDocument 绘图(ODG、FODG)转为 PDF、SVG 和 PNG,无需 Visio 或 LibreOffice。\n也支持 XPS 和 OpenXPS 文档(.xps、.oxps)。\n也支持 Excel 图表模板(.crtx)。\n旧的二进制 Visio 文件(.vsd)仍需 LibreOffice。", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "将 DjVu 扫描书籍和文档(.djvu、.djv)转换为可搜索隐藏文字的 PDF,以及页面的 PNG、JPG、TIFF 图片。", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "GLB、glTF、STL、OBJ、3MF 和 PLY 3D 模型互相转换,FBX 可转换为其中任意格式,并可生成 PNG 预览。", "engine.image3d.name": "图片转 3D 浮雕(内置)", diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.json index d53a7df1e..5773b832e 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -118,6 +118,7 @@ "error.p7m_unknown_content": "這個 .p7m 中的檔案是 Filee 不轉換的類型", "error.udf_not_uyap": "這個 .udf 檔案不是 UYAP 文件。如果它是光碟映像(Universal Disk Format),請用「解壓縮」開啟", "error.chart_template_unsupported": "暫不支援繪製此類圖表(雷達圖、泡泡圖、股票圖、曲面圖、子母長條圖,或瀑布圖、矩形式樹狀結構圖等新圖表類型)", + "error.djvu_indirect": "這是間接 DjVu 文件:各頁是個別的檔案。請先在 DjVu 檢視器中將其合併為一個檔案(例如 DjView:另存為綑綁文件),再轉換該檔案", "error.p7m_too_large": "已簽署的檔案太大,無法開啟", "error.document_too_large": "此文件解壓縮後資料過多或巢狀層級過深,無法安全開啟", "format_request.request": "請求支援此格式", @@ -328,6 +329,8 @@ "engine.gcode.description": "將 3D 印表機和 CNC 機台的刀具路徑 (G-code) 轉為 PDF、SVG 和圖片,或使用切片軟體儲存的預覽圖。", "engine.drawing.name": "Drawings (built-in)", "engine.drawing.description": "將 Visio 圖表(VSDX)和 OpenDocument 繪圖(ODG、FODG)轉為 PDF、SVG 和 PNG,不需 Visio 或 LibreOffice。\n也支援 XPS 和 OpenXPS 文件(.xps、.oxps)。\n也支援 Excel 圖表範本(.crtx)。\n舊的二進位 Visio 檔案(.vsd)仍需 LibreOffice。", + "engine.djvu.name": "DjVu (built-in)", + "engine.djvu.description": "將 DjVu 掃描書籍與文件(.djvu、.djv)轉換為可搜尋隱藏文字的 PDF,以及頁面的 PNG、JPG、TIFF 圖片。", "engine.model3d.name": "3D models (built-in)", "engine.model3d.description": "GLB、glTF、STL、OBJ、3MF 和 PLY 3D 模型互相轉換,FBX 可轉換為其中任意格式,並可產生 PNG 預覽。", "engine.image3d.name": "圖片轉 3D 浮雕(內建)", diff --git a/src/Filee.Core/Formats/FormatFields.cs b/src/Filee.Core/Formats/FormatFields.cs index dcff6d3e5..922c766e3 100644 --- a/src/Filee.Core/Formats/FormatFields.cs +++ b/src/Filee.Core/Formats/FormatFields.cs @@ -65,7 +65,7 @@ .. Enum.GetValues().Select(category => category.ToString().ToLow (FormatGrade.General, [ "tiff", "bmp", "ico", "avif", "jxl", "tga", "apng", "jxr", "livp", "exe", "svg", "svgz", "emf", "vsd", "odg", "canvas", "crtx", - "docm", "dotx", "dotm", "doc", "odt", "ott", "rtf", "wps", "ofd", "udf", "xps", "mm", "xmind", "abw", "pages", "pub", "eml", "msg", + "docm", "dotx", "dotm", "doc", "odt", "ott", "rtf", "wps", "ofd", "udf", "xps", "djvu", "mm", "xmind", "abw", "pages", "pub", "eml", "msg", "p7m", "xlsm", "xltx", "xlsb", "xls", "ods", "ots", "tsv", "et", "numbers", "pptm", "potx", "potm", "ppsx", "ppt", "odp", "otp", "key", "dps", "md", "html", "mhtml", "opml", "json", "xml", "vcf", "ics", "epub", "kepub", "azw3", "fb2", "cbz", "cbr", "cb7", "cbt", "cbc", "m4v", "mkv", "webm", "avi", "wmv", "mpg", "ts", diff --git a/src/Filee.Core/Formats/FormatRegistry.cs b/src/Filee.Core/Formats/FormatRegistry.cs index 5f21d3c48..7272bf812 100644 --- a/src/Filee.Core/Formats/FormatRegistry.cs +++ b/src/Filee.Core/Formats/FormatRegistry.cs @@ -124,6 +124,8 @@ public static class FormatRegistry new("udf", "UYAP (UDF)", FormatCategory.Document, ["udf"], Writable: false), // XPS and OpenXPS (ECMA-388) fixed-layout documents (#613), read by the built-in XPS reader (drawing engine). new("xps", "XPS", FormatCategory.Document, ["xps", "oxps"], Writable: false), + // DjVu scanned books and documents (#346), one page or bundled, read by the built-in DjVu reader. + new("djvu", "DjVu", FormatCategory.Document, ["djvu", "djv"], Writable: false), // Mind maps (#1543, #1004), read by Filee itself as an outline: topics as headings and lists, notes under them (the // map isn't drawn). A .mm that isn't XML is Objective-C++ source, read as plain text. new("mm", "FreeMind / Freeplane (MM)", FormatCategory.Document, ["mm"], Writable: false), diff --git a/src/Filee.Engines/Djvu/BzzDecoder.cs b/src/Filee.Engines/Djvu/BzzDecoder.cs new file mode 100644 index 000000000..2c10f8306 --- /dev/null +++ b/src/Filee.Engines/Djvu/BzzDecoder.cs @@ -0,0 +1,179 @@ +// BZZ, DjVu's general-purpose compression (DjVu v3 specification, "BZZ"): the data in blocks of at most 4 MB, each a +// Burrows-Wheeler transform whose output is coded as move-to-front ranks with the ZP-coder. TXTz (hidden text), the +// colour indices of FGbz, the DIRM directory's tables, ANTz and NAVM are BZZ streams. +// +// A block is its size (24 raw bits; 0 ends the stream), the speed of the rank estimator (0-2), then one rank per byte +// (the end-of-block marker is the 256th rank). The contexts of the rank decisions carry on from block to block; the +// move-to-front list and its frequencies start again in each one. + +namespace Filee.Engines.Djvu; + +/// Unpacks BZZ streams. +internal static class BzzDecoder +{ + /// Largest block the format allows. + private const int MaxBlock = 4096 * 1024; + + private const int FrequencyCount = 4; + private const int ContextIds = 3; + + /// Unpacks ; refuses one that unpacks to more than . + public static byte[] Decode(byte[] data, long maxBytes, CancellationToken cancellationToken = default) => + Decode(data, 0, data.Length, maxBytes, cancellationToken); + + public static byte[] Decode(byte[] data, int offset, int count, long maxBytes, CancellationToken cancellationToken = default) + { + var zp = new ZpDecoder(data, offset, count); + var contexts = new byte[300]; + using var output = new MemoryStream(); + while (true) + { + cancellationToken.ThrowIfCancellationRequested(); + var size = DecodeRaw(zp, 24); + if (size == 0) + break; + if (size > MaxBlock) + throw new InvalidDataException("A BZZ block is larger than the format allows."); + if (output.Length + size - 1 > maxBytes) + throw Documents.DocumentLimits.Exceeded($"A compressed part of the DjVu file unpacks to more than {Documents.DocumentLimits.Megabytes(maxBytes)}."); + var block = DecodeBlock(zp, contexts, size, cancellationToken); + output.Write(block); + } + return output.ToArray(); + } + + private static int DecodeRaw(ZpDecoder zp, int bits) + { + var n = 1; + var m = 1 << bits; + while (n < m) + n = (n << 1) | zp.DecodeRaw(); + return n - m; + } + + /// A binary number of bits, a context per node of its tree from . + private static int DecodeBinary(ZpDecoder zp, byte[] contexts, int first, int bits) + { + var n = 1; + var m = 1 << bits; + while (n < m) + n = (n << 1) | zp.Decode(ref contexts[first + n - 1]); + return n - m; + } + + /// One block, its marker removed and the transform undone. + private static byte[] DecodeBlock(ZpDecoder zp, byte[] contexts, int size, CancellationToken cancellationToken) + { + var data = new byte[size]; + var fshift = 0; + if (zp.DecodeRaw() != 0) + { + fshift++; + if (zp.DecodeRaw() != 0) + fshift++; + } + var mtf = new byte[256]; + for (var i = 0; i < 256; i++) + mtf[i] = (byte)i; + var freq = new uint[FrequencyCount]; + uint fadd = 4; + var mtfno = 3; + var marker = -1; + for (var i = 0; i < size; i++) + { + if ((i & 0xFFFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var ctxid = Math.Min(ContextIds - 1, mtfno); + if (zp.Decode(ref contexts[ctxid]) != 0) + mtfno = 0; + else if (zp.Decode(ref contexts[ContextIds + ctxid]) != 0) + mtfno = 1; + else + { + mtfno = -1; + // Ranks 2-3, 4-7, ... 128-255: a flag, then the rank's low bits as a binary number. + var at = 2 * ContextIds; + for (var bits = 1; bits <= 7; bits++) + { + if (zp.Decode(ref contexts[at]) != 0) + { + mtfno = (1 << bits) + DecodeBinary(zp, contexts, at + 1, bits); + break; + } + at += 1 << bits; + } + if (mtfno < 0) + { + mtfno = 256; + if (marker >= 0) + throw new InvalidDataException("A BZZ block has two end markers."); + marker = i; + continue; + } + } + var c = mtf[mtfno]; + data[i] = c; + // The rank list is ordered by how often each byte came lately, not strictly move-to-front. + fadd += fadd >> fshift; + if (fadd > 0x10000000) + { + fadd >>= 24; + for (var k = 0; k < FrequencyCount; k++) + freq[k] >>= 24; + } + var fc = fadd; + if (mtfno < FrequencyCount) + fc += freq[mtfno]; + int at2; + for (at2 = mtfno; at2 >= FrequencyCount; at2--) + mtf[at2] = mtf[at2 - 1]; + for (; at2 > 0 && fc >= freq[at2 - 1]; at2--) + { + mtf[at2] = mtf[at2 - 1]; + freq[at2] = freq[at2 - 1]; + } + mtf[at2] = c; + freq[at2] = fc; + } + if (marker < 1 || marker >= size) + throw new InvalidDataException("A BZZ block's end marker is missing or misplaced."); + return Untransform(data, size, marker); + } + + /// Undoes the Burrows-Wheeler transform of a block whose marker is at . + private static byte[] Untransform(byte[] data, int size, int marker) + { + var position = new int[size]; + var count = new int[256]; + for (var i = 0; i < size; i++) + { + if (i == marker) + continue; + var c = data[i]; + position[i] = (c << 24) | (count[c] & 0xFFFFFF); + count[c]++; + } + // Row 0 is the rotation that starts with the marker, which sorts before every byte. + var last = 1; + for (var i = 0; i < 256; i++) + { + var n = count[i]; + count[i] = last; + last += n; + } + var result = new byte[size - 1]; + var row = 0; + for (var at = size - 2; at >= 0; at--) + { + var n = position[row]; + var c = (byte)((uint)n >> 24); + result[at] = c; + row = count[c] + (n & 0xFFFFFF); + if ((uint)row >= (uint)size) + throw new InvalidDataException("A BZZ block is damaged."); + } + if (row != marker) + throw new InvalidDataException("A BZZ block is damaged."); + return result; + } +} diff --git a/src/Filee.Engines/Djvu/DjvuConverter.cs b/src/Filee.Engines/Djvu/DjvuConverter.cs new file mode 100644 index 000000000..fcdf0ebfa --- /dev/null +++ b/src/Filee.Engines/Djvu/DjvuConverter.cs @@ -0,0 +1,135 @@ +// 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). + +using Filee.Core.Conversion; +using Filee.Engines.Infrastructure; +using Filee.Engines.Magick; +using Filee.Engines.Ocr; +using ImageMagick; + +namespace Filee.Engines.Djvu; + +/// DjVu → PDF and pictures. +public sealed class DjvuConverter : IConverter, IImageTransformer +{ + /// The picture formats pages are written to. + internal static readonly string[] PictureTargets = ["png", "jpg", "tiff"]; + + public string Id => "djvu"; + public string DisplayName => "DjVu (built-in)"; + public int MaxParallelism => 0; + + public IReadOnlyList Edges { get; } = + [ + new ConversionEdge("djvu", "pdf"), + .. PictureTargets.Select(target => ImageEncoder.Edge("djvu", target)), + ]; + + /// Pictures of pages are written here, with the image options (turn, mirror, size) applied. + public bool Transforms(string from, string to) => to != "pdf"; + + public EngineStatus GetStatus() => EngineStatus.Available(version: EngineVersions.BuiltIn); + + public Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) => + Task.Run>(() => + { + using var document = DjvuDocument.Open(step.InputPath, cancellationToken: cancellationToken); + var written = step.To == "pdf" + ? WritePdf(step, document, progress, cancellationToken) + : WritePictures(step, document, progress, cancellationToken); + if (document.LeftOut > 0) + step.LeftOut?.Invoke(new LeftOutNotice("notice.renderer_left_out", [new LeftOutPart("notice.renderer_left_out.pictures", document.LeftOut)])); + return written; + }, cancellationToken); + + private static List WritePdf(ConversionStep step, DjvuDocument document, IProgress? progress, CancellationToken cancellationToken) + { + using var writer = new OcrPdfWriter(); + for (var i = 0; i < document.Count; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + DjvuPdfWriter.AddPage(writer, document.ReadPage(i, cancellationToken), cancellationToken); + progress?.Report(0.95 * (i + 1) / document.Count); + } + if (step.Output.Allocate("pdf") is not { } target) + return []; + writer.Save(target); + progress?.Report(1); + return [target]; + } + + private static List WritePictures(ConversionStep step, DjvuDocument document, IProgress? progress, CancellationToken cancellationToken) + { + var pages = PageRange.Parse(step.Preset.Pdf.PageRange, document.Count); + var (_, extension) = ImageEncoder.OutputFormat(step.To, step.Preset.Image); + var written = new List(); + var collected = new List>(); + try + { + var done = 0; + foreach (var index in pages) + { + cancellationToken.ThrowIfCancellationRequested(); + var image = Picture(document.ReadPage(index, cancellationToken), cancellationToken); + ImageEncoder.ApplyOptions(image, step.Preset.Image, step.To); + if (step.To == "tiff") + { + collected.Add(image); + } + else + { + using (image) + { + if (step.Output.Allocate(extension, pages.Count > 1 ? $"_p{index + 1}" : null) is { } path) + { + ImageEncoder.WriteImage(image, path, step.To); + written.Add(path); + } + } + } + done++; + progress?.Report(0.95 * done / pages.Count); + } + if (collected.Count > 0) + written.AddRange(ImageEncoder.Write(collected, step.To, step.Output, step.Preset.Image)); + } + finally + { + foreach (var image in collected) + image.Dispose(); + } + progress?.Report(1); + return written; + } + + /// A page drawn at its own resolution, turned as its INFO says; grey when it has no colour. + internal static MagickImage Picture(DjvuPage page, CancellationToken cancellationToken) + { + var rgb = DjvuRenderer.Compose(page, cancellationToken); + var grey = true; + for (var i = 0; i < rgb.Length && grey; i += 3) + grey = rgb[i] == rgb[i + 1] && rgb[i] == rgb[i + 2]; + MagickImage image; + if (grey) + { + // One channel, read as a PGM: a third of the memory of a colour picture. + var header = System.Text.Encoding.ASCII.GetBytes($"P5\n{page.Width} {page.Height}\n255\n"); + var pgm = new byte[header.Length + (long)page.Width * page.Height]; + header.CopyTo(pgm, 0); + for (long i = 0, o = header.Length; o < pgm.Length; i += 3, o++) + pgm[o] = rgb[i]; + image = new MagickImage(pgm, MagickFormat.Pgm); + } + else + { + image = new MagickImage(); + image.ReadPixels(rgb, new PixelReadSettings((uint)page.Width, (uint)page.Height, StorageType.Char, PixelMapping.RGB)); + } + image.Density = new Density(page.Dpi, page.Dpi, DensityUnit.PixelsPerInch); + if (page.Rotation != 0) + image.Rotate(-90 * page.Rotation); + return image; + } +} diff --git a/src/Filee.Engines/Djvu/DjvuDocument.cs b/src/Filee.Engines/Djvu/DjvuDocument.cs new file mode 100644 index 000000000..e7faeaab6 --- /dev/null +++ b/src/Filee.Engines/Djvu/DjvuDocument.cs @@ -0,0 +1,460 @@ +// 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. +// +// 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. + +using System.Text; +using Filee.Core.Conversion; +using Filee.Engines.Documents; +using SkiaSharp; + +namespace Filee.Engines.Djvu; + +/// A page's foreground palette: its colours and the colour of each blit of the mask. +internal sealed record DjvuPalette(SKColor[] Colors, int[]? BlitColors); + +/// One decoded page. +internal sealed class DjvuPage +{ + public required int Width { get; init; } + public required int Height { get; init; } + public required int Dpi { get; init; } + + /// Quarter turns counter-clockwise the page is shown with (INFO's orientation). + public int Rotation { get; init; } + + public Jb2Image? Mask { get; init; } + public DjvuPixmap? Background { get; init; } + public DjvuPixmap? Foreground { get; init; } + public DjvuPalette? Palette { get; init; } + public IReadOnlyList Text { get; init; } = []; + + /// Layers of the page Filee couldn't decode. + public int LeftOut { get; init; } +} + +/// An open DjVu file. +internal sealed class DjvuDocument : IDisposable +{ + /// The error key of an indirect document (pages in separate files). + public const string IndirectKey = "error.djvu_indirect"; + + private sealed record Chunk(string Id, long Offset, int Size); + + private sealed class Component(string id, long offset, long end, string kind) + { + public string Id { get; } = id; + public long Offset { get; } = offset; + public long End { get; } = end; + public string Kind { get; } = kind; + public List? Chunks { get; set; } + } + + private readonly FileStream _file; + private readonly DjvuLimits _limits; + private readonly List _pages = []; + private readonly Dictionary _byId = new(StringComparer.Ordinal); + private readonly Dictionary _dictionaries = new(StringComparer.Ordinal); + private int _leftOut; + + private DjvuDocument(FileStream file, DjvuLimits limits) + { + _file = file; + _limits = limits; + } + + public int Count => _pages.Count; + + /// Layers left out of the pages read so far. + public int LeftOut => _leftOut; + + /// 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 document = new DjvuDocument(file, limits ?? DjvuLimits.Default); + try + { + document.ReadStructure(cancellationToken); + return document; + } + catch + { + document.Dispose(); + throw; + } + } + + public void Dispose() => _file.Dispose(); + + 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."); + switch (kind) + { + case "DJVU": + _pages.Add(new Component("", start, end, kind)); + break; + case "DJVM": + ReadBundle(start, end, cancellationToken); + break; + case "DJVI": + throw new InvalidDataException("The file is a part DjVu pages include, not a document."); + default: + throw new InvalidDataException($"The DjVu file holds a {kind} form, not pages Filee reads."); + } + if (_pages.Count == 0) + throw new InvalidDataException("The DjVu document has no pages."); + } + + /// The kind and end of a FORM chunk at , or null when there is none. + private (string Kind, long End)? ReadForm(long offset) + { + if (offset < 0 || offset + 12 > _file.Length) + return null; + var header = ReadBytes(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); + 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) + { + var chunks = new List(); + var at = offset + 12; + var header = new byte[8]; + while (at + 8 <= end) + { + if ((chunks.Count & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + _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))); + if (chunks.Count > 1_000_000) + throw DocumentLimits.Exceeded("A DjVu form has more than 1,000,000 chunks."); + at = data + size; + if ((at & 1) != 0) + at++; + } + return chunks; + } + + 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); + } + + private byte[] ReadBytes(long offset, int count) + { + var data = new byte[count]; + _file.Position = offset; + _file.ReadExactly(data); + return data; + } + + private void ReadBundle(long start, long end, CancellationToken cancellationToken) + { + var chunks = ListChunks(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)."); + version &= 0x7F; + if (version > 1) + throw new InvalidDataException("The DjVu directory has a version Filee doesn't know."); + var count = reader.U16(); + if (count > _limits.MaxComponents) + 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++) + { + offsets[i] = reader.U32(); + if (version == 0) + sizes[i] = reader.U24(); + } + var table = BzzDecoder.Decode(data, reader.Position, data.Length - reader.Position, _limits.MaxUnpackedBytes, cancellationToken); + var packed = new DjvuBytes(table); + if (version > 0) + { + for (var i = 0; i < count; i++) + sizes[i] = packed.U24(); + } + var flags = new int[count]; + for (var i = 0; i < count; i++) + { + flags[i] = packed.U8(); + if (version == 0) + flags[i] = ((flags[i] & 1) != 0 ? 1 : 0) | ((flags[i] & 2) != 0 ? 0x80 : 0) | ((flags[i] & 4) != 0 ? 0x40 : 0); + } + 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) + 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); + _byId.TryAdd(id, component); + if ((flags[i] & 0x3F) == 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."); + _pages.Add(component); + } + } + } + + private List ChunksOf(Component component, CancellationToken cancellationToken) => + component.Chunks ??= ListChunks(component.Offset, component.End, cancellationToken); + + /// Page decoded. + public DjvuPage ReadPage(int index, CancellationToken cancellationToken = default) + { + var component = _pages[index]; + var own = ChunksOf(component, cancellationToken); + var info = own.FirstOrDefault(c => c.Id == "INFO") ?? throw new InvalidDataException("A DjVu page has no INFO chunk."); + var (width, height, dpi, rotation) = ReadInfo(ReadChunk(info)); + + var leftOut = 0; + Jb2Image? mask = null; + DjvuPalette? palette = null; + var background = new Iw44Decoder(_limits); + var foreground = new Iw44Decoder(_limits); + DjvuPixmap? backgroundJpeg = null, foregroundJpeg = null; + List text = []; + foreach (var chunk in own) + { + cancellationToken.ThrowIfCancellationRequested(); + switch (chunk.Id) + { + case "Sjbz": + mask = Jb2Decoder.DecodeImage(ReadChunk(chunk), Dictionary(component, cancellationToken), _limits, cancellationToken); + break; + case "BG44": + background.Decode(ReadChunk(chunk), cancellationToken); + break; + case "FG44": + foreground.Decode(ReadChunk(chunk), cancellationToken); + break; + case "BGjp": + backgroundJpeg = DecodeJpeg(ReadChunk(chunk)); + if (backgroundJpeg is null) + leftOut++; + break; + case "FGjp": + foregroundJpeg = DecodeJpeg(ReadChunk(chunk)); + if (foregroundJpeg is null) + leftOut++; + break; + case "FGbz": + palette = ReadPalette(ReadChunk(chunk), cancellationToken); + break; + case "TXTz": + text = ReadText(BzzDecoder.Decode(ReadChunk(chunk), _limits.MaxUnpackedBytes, cancellationToken)); + break; + case "TXTa": + text = ReadText(ReadChunk(chunk)); + break; + case "Smmr" or "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."); + _leftOut += leftOut; + return new DjvuPage + { + Width = width, + Height = height, + Dpi = dpi, + Rotation = rotation, + Mask = mask, + Background = background.HasData ? background.ToPixmap(cancellationToken) : backgroundJpeg, + Foreground = foreground.HasData ? foreground.ToPixmap(cancellationToken) : foregroundJpeg, + Palette = palette, + Text = text, + LeftOut = leftOut, + }; + } + + private (int Width, int Height, int Dpi, int Rotation) ReadInfo(byte[] data) + { + if (data.Length < 5) + throw new InvalidDataException("A DjVu page's INFO chunk is damaged."); + var width = (data[0] << 8) | data[1]; + var height = (data[2] << 8) | data[3]; + var dpi = data.Length >= 8 && data[7] != 0xFF ? (data[7] << 8) | data[6] : 300; + if (dpi is < 25 or > 6000) + dpi = 300; + var flags = data.Length >= 10 ? data[9] : 0; + var rotation = (flags & 7) switch { 6 => 1, 2 => 2, 5 => 3, _ => 0 }; + if (width == 0 || height == 0) + throw new InvalidDataException("A DjVu page has no size."); + if ((long)width * height > _limits.MaxPagePixels) + throw DocumentLimits.Exceeded($"A DjVu page has more than {DocumentLimits.Count(_limits.MaxPagePixels)} pixels."); + return (width, height, dpi, rotation); + } + + private List ReadText(byte[] data) + { + try + { + return DjvuText.Read(data, _limits); + } + catch (InvalidDataException) + { + // Damaged hidden text leaves the page without its text layer; the picture is still the page. + return []; + } + } + + private DjvuPalette ReadPalette(byte[] data, CancellationToken cancellationToken) + { + var reader = new DjvuBytes(data); + var version = reader.U8(); + if ((version & 0x7F) != 0) + throw new InvalidDataException("A DjVu palette has a version Filee doesn't know."); + var size = reader.U16(); + if (size > _limits.MaxPaletteColors) + throw DocumentLimits.Exceeded("A DjVu palette has too many colours."); + var colors = new SKColor[size]; + for (var i = 0; i < size; i++) + { + // Stored blue, green, red. + var b = (byte)reader.U8(); + var g = (byte)reader.U8(); + var r = (byte)reader.U8(); + colors[i] = new SKColor(r, g, b); + } + int[]? indices = null; + if ((version & 0x80) != 0) + { + var count = reader.U24(); + if (count > _limits.MaxBlits) + throw DocumentLimits.Exceeded($"A DjVu palette colours more than {DocumentLimits.Count(_limits.MaxBlits)} shapes."); + var packed = BzzDecoder.Decode(data, reader.Position, data.Length - reader.Position, Math.Min(_limits.MaxUnpackedBytes, 2L * count), cancellationToken); + if (packed.Length < 2L * count) + throw new InvalidDataException("A DjVu palette's colour indices are cut short."); + indices = new int[count]; + for (var i = 0; i < count; i++) + { + var index = (short)((packed[2 * i] << 8) | packed[2 * i + 1]); + if (index < 0 || index >= size) + throw new InvalidDataException("A DjVu palette's colour index is out of range."); + indices[i] = index; + } + } + return new DjvuPalette(colors, indices); + } + + /// + /// 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 . + /// + private Component? FindDictionary(Component start, bool includeSelf, HashSet seen, CancellationToken cancellationToken) + { + var pending = new Stack<(Component Component, bool Check)>(); + pending.Push((start, includeSelf)); + seen.Add(start); + while (pending.Count > 0) + { + var (component, check) = pending.Pop(); + var chunks = ChunksOf(component, cancellationToken); + if (check && chunks.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); + } + for (var i = included.Count - 1; i >= 0; i--) + pending.Push((included[i], true)); + } + return null; + } + + /// A JPEG layer (BGjp, FGjp), or null when Skia can't decode it or it is too large. + private DjvuPixmap? DecodeJpeg(byte[] data) + { + using var codec = SKCodec.Create(new MemoryStream(data)); + if (codec is null || (long)codec.Info.Width * codec.Info.Height > _limits.MaxIw44Pixels) + return null; + using var bitmap = SKBitmap.Decode(codec, new SKImageInfo(codec.Info.Width, codec.Info.Height, SKColorType.Rgba8888, SKAlphaType.Opaque)); + if (bitmap is null) + return null; + var pixels = bitmap.GetPixelSpan(); + var rgb = new byte[(long)bitmap.Width * bitmap.Height * 3]; + for (int i = 0, o = 0; o < rgb.Length; i += 4, o += 3) + { + rgb[o] = pixels[i]; + rgb[o + 1] = pixels[i + 1]; + rgb[o + 2] = pixels[i + 2]; + } + return new DjvuPixmap(bitmap.Width, bitmap.Height, rgb); + } + + /// + /// The shape dictionary a page's mask may build on: the page's own Djbz, or that of the first file it includes + /// (directly or through other includes) that has one. Decoded once per file. + /// + private Jb2Dictionary? Dictionary(Component page, CancellationToken cancellationToken) + { + // The chain of files whose dictionaries build on each other, each file looked at once. + var chain = new List(); + var seen = new HashSet(); + var holder = FindDictionary(page, includeSelf: true, seen, cancellationToken); + while (holder is not null) + { + chain.Add(holder); + holder = FindDictionary(holder, includeSelf: false, seen, cancellationToken); + } + Jb2Dictionary? dictionary = null; + for (var i = chain.Count - 1; i >= 0; i--) + { + var component = chain[i]; + var key = component.Id + "@" + component.Offset; + if (!_dictionaries.TryGetValue(key, out var decoded)) + { + var djbz = ChunksOf(component, cancellationToken).First(c => c.Id == "Djbz"); + decoded = Jb2Decoder.DecodeDictionary(ReadChunk(djbz), dictionary, _limits, cancellationToken); + // A few shared dictionaries serve a whole book; keep only the latest ones. + if (_dictionaries.Count >= 8) + _dictionaries.Clear(); + _dictionaries[key] = decoded; + } + dictionary = decoded; + } + return dictionary; + } +} diff --git a/src/Filee.Engines/Djvu/DjvuLimits.cs b/src/Filee.Engines/Djvu/DjvuLimits.cs new file mode 100644 index 000000000..016872aa9 --- /dev/null +++ b/src/Filee.Engines/Djvu/DjvuLimits.cs @@ -0,0 +1,56 @@ +// 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. + +using Filee.Engines.Documents; + +namespace Filee.Engines.Djvu; + +/// Limits of the DjVu reader. +internal sealed record DjvuLimits +{ + public static DjvuLimits Default { get; } = new(); + + /// Pages of one document. + public int MaxPages { get; init; } = DocumentLimits.Default.MaxPages; + + /// Components (pages, shared dictionaries, thumbnails) of a bundled document. + public int MaxComponents { get; init; } = 2 * DocumentLimits.Default.MaxPages; + + /// Bytes of one chunk read into memory (a page's mask, background or text). + public long MaxChunkBytes { get; init; } = DocumentLimits.Default.MaxPartBytes; + + /// Bytes one BZZ stream (text, palette indices, the directory) may unpack to. + public long MaxUnpackedBytes { get; init; } = DocumentLimits.Default.MaxPartBytes; + + /// Pixels of one page (its INFO size), and of one IW44 picture. + public long MaxPagePixels { get; init; } = 150_000_000; + + /// + /// Pixels of one IW44 picture (a background or foreground layer; encoders store them at a third of the page's + /// resolution or less, photographs at full size). Three maps of two bytes per pixel are decoded. + /// + public long MaxIw44Pixels { get; init; } = 64_000_000; + + /// Shapes of one JB2 image or dictionary. + public int MaxShapes { get; init; } = 2_000_000; + + /// Blits (placed shapes) of one JB2 image. + public int MaxBlits { get; init; } = 4_000_000; + + /// Pixels of all shapes of one JB2 image or dictionary together (each costs a byte while decoded). + public long MaxShapePixels { get; init; } = 400_000_000; + + /// Cells of the JB2 number coder between two resets (each costs 9 bytes). + public int MaxNumberCells { get; init; } = 20_000_000; + + /// Coding slices of one IW44 picture (all its chunks together; real files have 100 or fewer). + public int MaxSlices { get; init; } = 10_000; + + /// Text zones of one page. + public int MaxTextZones { get; init; } = 2_000_000; + + /// Colours of a foreground palette. + public int MaxPaletteColors { get; init; } = 65_535; +} diff --git a/src/Filee.Engines/Djvu/DjvuPdfWriter.cs b/src/Filee.Engines/Djvu/DjvuPdfWriter.cs new file mode 100644 index 000000000..9d3e2a607 --- /dev/null +++ b/src/Filee.Engines/Djvu/DjvuPdfWriter.cs @@ -0,0 +1,220 @@ +// 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. + +using System.Globalization; +using System.IO.Compression; +using System.Text; +using Filee.Engines.Ocr; +using PdfSharp.Drawing; +using PdfSharp.Pdf; +using SkiaSharp; + +namespace Filee.Engines.Djvu; + +internal static class DjvuPdfWriter +{ + /// Palette colours drawn as separate masks at most; a page with more is drawn as one picture. + internal const int MaxStencils = 64; + + /// Largest page side in points (PDF's own limit, 200 inches). + private const double MaxSide = 14_400; + + private const int JpegQuality = 85; + + /// Adds to 's document. + public static void AddPage(OcrPdfWriter writer, DjvuPage page, CancellationToken cancellationToken = default) + { + var document = writer.Document; + var scale = 72.0 / page.Dpi; + scale = Math.Min(scale, MaxSide / Math.Max(page.Width, page.Height)); + var width = page.Width * scale; + var height = page.Height * scale; + var pdfPage = document.AddPage(); + pdfPage.Width = XUnit.FromPoint(width); + pdfPage.Height = XUnit.FromPoint(height); + if (page.Rotation != 0) + pdfPage.Elements.SetInteger("/Rotate", page.Rotation switch { 1 => 270, 2 => 180, _ => 90 }); + + var content = new StringBuilder(); + var images = new List<(string Name, PdfDictionary Image)>(); + void Draw(PdfDictionary image, string? fill = null) + { + var name = "/Im" + images.Count.ToString(CultureInfo.InvariantCulture); + images.Add((name, image)); + content.Append("q "); + if (fill is not null) + content.Append(fill).Append(' '); + content.Append(CultureInfo.InvariantCulture, $"{N(width)} 0 0 {N(height)} 0 0 cm {name} Do Q\n"); + } + + var mask = page.Mask is { Blits.Count: > 0 } ? page.Mask : null; + var palette = page.Palette is { Colors.Length: > 0 } p ? p : null; + var used = mask is null || palette is null || page.Foreground is not null ? null : UsedColors(mask, palette); + if (used is { Count: > MaxStencils }) + { + // Too many colours to draw one mask each: the page as it looks. + Draw(Jpeg(document, new DjvuPixmap(page.Width, page.Height, DjvuRenderer.Compose(page, cancellationToken)))); + } + else + { + if (page.Background is { } background) + Draw(Jpeg(document, background)); + if (mask is not null) + { + if (page.Foreground is { } foreground) + { + var image = Jpeg(document, foreground); + image.Elements["/Mask"] = Stencil(document, page, null, cancellationToken).Reference; + Draw(image); + } + else if (used is not 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"); + } + } + else + { + Draw(Stencil(document, page, null, cancellationToken), "0 g"); + } + } + } + + if (images.Count > 0) + { + if (pdfPage.Resources.Elements.GetDictionary("/XObject") is not { } objects) + { + objects = new PdfDictionary(document); + pdfPage.Resources.Elements["/XObject"] = objects; + } + foreach (var (name, image) in images) + objects.Elements.SetReference(name, image); + pdfPage.Contents.AppendContent().CreateStream(Encoding.ASCII.GetBytes(content.ToString())); + } + writer.AddText(pdfPage, Lines(page), width, height); + } + + /// The palette colours the mask's shapes use, in the order first used. + private static List UsedColors(Jb2Image mask, DjvuPalette palette) + { + var used = new List(); + var seen = new HashSet(); + for (var b = 0; b < mask.Blits.Count; b++) + { + var color = DjvuRenderer.ColorOf(palette.BlitColors, b, mask.Blits.Count); + if (seen.Add(color)) + { + used.Add(color); + if (used.Count > MaxStencils) + break; + } + } + return used; + } + + /// The page's text lines as fractions of the page, as the OCR layer takes them. + internal static List Lines(DjvuPage page) + { + double w = page.Width, h = page.Height; + var lines = new List(); + foreach (var line in page.Text) + { + var box = line.Box; + if (line.Words.Count == 0) + { + lines.Add(OcrLine.Estimate(line.Text, box.Left / w, box.Bottom / h, box.Width / w, box.Height / h)); + continue; + } + var words = line.Words.Select(word => new OcrWord(word.Text, word.Box.Left / w, word.Box.Width / w)).ToList(); + lines.Add(new OcrLine(box.Left / w, box.Bottom / h, box.Width / w, box.Height / h, words)); + } + return lines; + } + + /// 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) + { + var bits = DjvuRenderer.MaskBits(page, color, cancellationToken); + var image = Stream(document, Deflate(bits), "/FlateDecode"); + 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; + } + + /// A JPEG image of , in grey when every pixel is grey. + private static PdfDictionary Jpeg(PdfDocument document, DjvuPixmap pixmap) + { + var rgb = pixmap.Rgb; + var grey = true; + for (var i = 0; i < rgb.Length && grey; i += 3) + grey = rgb[i] == rgb[i + 1] && rgb[i] == rgb[i + 2]; + byte[] jpeg; + if (grey) + { + var pixels = new byte[(long)pixmap.Width * pixmap.Height]; + for (int i = 0, o = 0; o < pixels.Length; i += 3, o++) + pixels[o] = rgb[i]; + jpeg = Encode(pixels, new SKImageInfo(pixmap.Width, pixmap.Height, SKColorType.Gray8, SKAlphaType.Opaque)); + } + else + { + var pixels = new byte[(long)pixmap.Width * pixmap.Height * 4]; + for (int i = 0, o = 0; o < pixels.Length; i += 3, o += 4) + { + pixels[o] = rgb[i]; + pixels[o + 1] = rgb[i + 1]; + pixels[o + 2] = rgb[i + 2]; + pixels[o + 3] = 255; + } + jpeg = Encode(pixels, new SKImageInfo(pixmap.Width, pixmap.Height, SKColorType.Rgba8888, SKAlphaType.Opaque)); + } + var image = Stream(document, jpeg, "/DCTDecode"); + image.Elements.SetName("/Type", "/XObject"); + image.Elements.SetName("/Subtype", "/Image"); + image.Elements.SetInteger("/Width", pixmap.Width); + image.Elements.SetInteger("/Height", pixmap.Height); + image.Elements.SetName("/ColorSpace", grey ? "/DeviceGray" : "/DeviceRGB"); + image.Elements.SetInteger("/BitsPerComponent", 8); + return image; + } + + private static byte[] Encode(byte[] pixels, SKImageInfo info) + { + using var image = SKImage.FromPixelCopy(info, pixels, info.RowBytes) ?? throw new InvalidOperationException("Skia can't hold the picture."); + using var data = image.Encode(SKEncodedImageFormat.Jpeg, JpegQuality) ?? throw new InvalidOperationException("Skia can't write the JPEG."); + return data.ToArray(); + } + + private static byte[] Deflate(byte[] data) + { + using var buffer = new MemoryStream(); + using (var zlib = new ZLibStream(buffer, CompressionLevel.Optimal, leaveOpen: true)) + zlib.Write(data); + return buffer.ToArray(); + } + + private static PdfDictionary Stream(PdfDocument document, byte[] data, string filter) + { + var dictionary = new PdfDictionary(document); + document.Internals.AddObject(dictionary); + dictionary.CreateStream(data); + dictionary.Elements.SetName("/Filter", filter); + return dictionary; + } + + private static string N(double value) => Math.Round(value, 4).ToString("0.####", CultureInfo.InvariantCulture); +} diff --git a/src/Filee.Engines/Djvu/DjvuRenderer.cs b/src/Filee.Engines/Djvu/DjvuRenderer.cs new file mode 100644 index 000000000..9da34912e --- /dev/null +++ b/src/Filee.Engines/Djvu/DjvuRenderer.cs @@ -0,0 +1,185 @@ +// Draws a decoded DjVu page as DjVu viewers compose it: the background scaled up to the page (it is stored at a +// fraction of the page's resolution: the reduction is the smallest whole factor that gives its size), then every +// pixel the mask covers in the foreground colour: the FG44 / FGjp picture at that point (scaled the same way), the +// palette colour of the shape that covers it, or black. A page without a background is white. +// +// The mask is also given as 1-bit rows for the PDF writer, all shapes or only those of one palette colour. + +namespace Filee.Engines.Djvu; + +internal static class DjvuRenderer +{ + /// The reduction of a layer of × on the page. + public static int Reduction(int width, int height, int layerWidth, int layerHeight) + { + for (var red = 1; red < 16; red++) + { + if ((width + red - 1) / red == layerWidth && (height + red - 1) / red == layerHeight) + return red; + } + return 16; + } + + /// + /// The page's mask as 1-bit rows, top row first, a set bit for a black pixel, each row padded to a byte; + /// keeps only the shapes of that palette colour. + /// + public static byte[] MaskBits(DjvuPage page, int? color = null, CancellationToken cancellationToken = default) + { + int width = page.Width, height = page.Height; + var stride = (width + 7) / 8; + var bits = new byte[(long)stride * height]; + if (page.Mask is not { } mask) + return bits; + var blitColors = page.Palette?.BlitColors; + for (var b = 0; b < mask.Blits.Count; b++) + { + if ((b & 0x3FF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + if (color is { } wanted && ColorOf(blitColors, b, mask.Blits.Count) != wanted) + continue; + var blit = mask.Blits[b]; + var shape = mask.Shapes[blit.Shape]; + for (var y = 0; y < shape.Height; y++) + { + var pageY = height - 1 - (blit.Bottom + y); + if ((uint)pageY >= (uint)height) + continue; + var row = (long)pageY * stride; + var source = y * shape.Width; + for (var x = 0; x < shape.Width; x++) + { + if (shape.Bits[source + x] == 0) + continue; + var pageX = blit.Left + x; + if ((uint)pageX < (uint)width) + bits[row + (pageX >> 3)] |= (byte)(0x80 >> (pageX & 7)); + } + } + } + return bits; + } + + /// The palette colour of blit (the first colour when the file has no indices). + public static int ColorOf(int[]? blitColors, int blit, int blits) => + blitColors is not null && blitColors.Length == blits ? blitColors[blit] : 0; + + /// The page drawn as RGB bytes, three per pixel, top row first. + public static byte[] Compose(DjvuPage page, CancellationToken cancellationToken = default) + { + int width = page.Width, height = page.Height; + var rgb = new byte[(long)width * height * 3]; + if (page.Background is { } background) + Upscale(background, rgb, width, height, cancellationToken); + else + Array.Fill(rgb, (byte)255); + if (page.Mask is not { } mask) + return rgb; + + if (page.Foreground is null && page.Palette is { Colors.Length: > 0 } palette) + { + // Each shape in its own colour, in the order they are placed. + for (var b = 0; b < mask.Blits.Count; b++) + { + if ((b & 0x3FF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var c = palette.Colors[ColorOf(palette.BlitColors, b, mask.Blits.Count)]; + Paint(mask, b, rgb, width, height, c.Red, c.Green, c.Blue); + } + return rgb; + } + + var bits = MaskBits(page, null, cancellationToken); + var stride = (width + 7) / 8; + var foreground = page.Foreground; + var red = foreground is null ? 1 : Reduction(width, height, foreground.Width, foreground.Height); + for (var y = 0; y < height; y++) + { + if ((y & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var row = (long)y * stride; + var o = (long)y * width * 3; + var fy = foreground is null ? 0 : Math.Min(y / red, foreground.Height - 1); + for (var x = 0; x < width; x++, o += 3) + { + if ((bits[row + (x >> 3)] & (0x80 >> (x & 7))) == 0) + continue; + if (foreground is null) + { + rgb[o] = rgb[o + 1] = rgb[o + 2] = 0; + continue; + } + var f = ((long)fy * foreground.Width + Math.Min(x / red, foreground.Width - 1)) * 3; + rgb[o] = foreground.Rgb[f]; + rgb[o + 1] = foreground.Rgb[f + 1]; + rgb[o + 2] = foreground.Rgb[f + 2]; + } + } + return rgb; + } + + private static void Paint(Jb2Image mask, int b, byte[] rgb, int width, int height, byte r, byte g, byte bl) + { + var blit = mask.Blits[b]; + var shape = mask.Shapes[blit.Shape]; + for (var y = 0; y < shape.Height; y++) + { + var pageY = height - 1 - (blit.Bottom + y); + if ((uint)pageY >= (uint)height) + continue; + var source = y * shape.Width; + for (var x = 0; x < shape.Width; x++) + { + var pageX = blit.Left + x; + if (shape.Bits[source + x] == 0 || (uint)pageX >= (uint)width) + continue; + var o = ((long)pageY * width + pageX) * 3; + rgb[o] = r; + rgb[o + 1] = g; + rgb[o + 2] = bl; + } + } + } + + /// Scales up to the page, bilinear, each layer pixel centred on its block of page pixels. + public static void Upscale(DjvuPixmap layer, byte[] rgb, int width, int height, CancellationToken cancellationToken) + { + var red = Reduction(width, height, layer.Width, layer.Height); + int lw = layer.Width, lh = layer.Height; + var source = layer.Rgb; + var xs = new int[width]; + var xf = new int[width]; + for (var x = 0; x < width; x++) + { + var sx = Math.Clamp((x + 0.5) / red - 0.5, 0, lw - 1); + xs[x] = (int)sx; + xf[x] = (int)((sx - xs[x]) * 256); + } + for (var y = 0; y < height; y++) + { + if ((y & 0xFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + var sy = Math.Clamp((y + 0.5) / red - 0.5, 0, lh - 1); + var y0 = (int)sy; + var y1 = Math.Min(y0 + 1, lh - 1); + var fy = (int)((sy - y0) * 256); + var row0 = (long)y0 * lw * 3; + var row1 = (long)y1 * lw * 3; + var o = (long)y * width * 3; + for (var x = 0; x < width; x++, o += 3) + { + var x0 = xs[x]; + var x1 = Math.Min(x0 + 1, lw - 1); + var fx = xf[x]; + for (var c = 0; c < 3; c++) + { + int a = source[row0 + x0 * 3 + c], b = source[row0 + x1 * 3 + c]; + int d = source[row1 + x0 * 3 + c], e = source[row1 + x1 * 3 + c]; + var top = a * 256 + (b - a) * fx; + var bottom = d * 256 + (e - d) * fx; + rgb[o + c] = (byte)((top * 256 + (bottom - top) * fy + 32768) >> 16); + } + } + } + } +} diff --git a/src/Filee.Engines/Djvu/DjvuText.cs b/src/Filee.Engines/Djvu/DjvuText.cs new file mode 100644 index 000000000..f1670ef6b --- /dev/null +++ b/src/Filee.Engines/Djvu/DjvuText.cs @@ -0,0 +1,239 @@ +// The hidden text of a DjVu page (DjVu v3 specification, "Text Chunk": TXTa, or TXTz which is TXTa packed with BZZ): +// the page's text in UTF-8, then a tree of zones (page, column, region, paragraph, line, word, character), each with +// its box in page pixels (row 0 at the bottom) and the range of the text it covers. A zone's box is coded relative to +// the previous zone at its level or to its parent. The lines and words become the invisible text layer of the PDF. + +using System.Text; +using Filee.Engines.Documents; + +namespace Filee.Engines.Djvu; + +/// A box in page pixels, row 0 at the bottom. +internal readonly record struct DjvuRect(int Left, int Bottom, int Width, int Height); + +/// A word of a text line and its box. +internal sealed record DjvuWord(string Text, DjvuRect Box); + +/// A line of text: its box and words (one estimated word per run of text when the file has no word zones). +internal sealed record DjvuLine(DjvuRect Box, IReadOnlyList Words, string Text); + +/// Reads TXTa data. +internal static class DjvuText +{ + private const int Line = 5, Word = 6; + + private sealed class Zone(int type, DjvuRect box, int textStart, int textLength) + { + public int Type { get; } = type; + public DjvuRect Box { get; } = box; + public int TextStart { get; } = textStart; + public int TextLength { get; } = textLength; + public List Children { get; } = []; + } + + /// The lines of a page's text; empty when it has no zones. Damaged text throws InvalidDataException. + public static List Read(byte[] data, DjvuLimits limits) + { + var reader = new DjvuBytes(data); + var size = reader.U24(); + if (size <= 0 || size > data.Length - 3) + throw new InvalidDataException("A DjVu text chunk is damaged."); + var text = reader.Bytes(size); + if (reader.Remaining == 0) + return []; + if (reader.U8() != 1) + throw new InvalidDataException("A DjVu text chunk has a version Filee doesn't know."); + var root = ReadZones(reader, size, limits); + return Lines(root, text); + } + + /// The zone tree, read without recursion. + private static Zone ReadZones(DjvuBytes reader, int textSize, DjvuLimits limits) + { + var zones = 0; + // Each open zone with how many children it still has and its last child read. + var stack = new Stack<(Zone Zone, int Remaining, Zone? Last)>(); + Zone? root = null; + Zone? parent = null; + Zone? previous = null; + while (true) + { + if (++zones > limits.MaxTextZones) + throw DocumentLimits.Exceeded($"A DjVu page's text has more than {DocumentLimits.Count(limits.MaxTextZones)} zones."); + var type = reader.U8(); + if (type is < 1 or > 7 || (parent is not null && type <= parent.Type)) + throw new InvalidDataException("A DjVu text zone is damaged."); + var x = reader.U16() - 0x8000; + var y = reader.U16() - 0x8000; + var width = reader.U16() - 0x8000; + var height = reader.U16() - 0x8000; + var start = reader.U16() - 0x8000; + var length = reader.U24(); + if (previous is not null) + { + if (type is 1 or 4 or 5) + { + x += previous.Box.Left; + y = previous.Box.Bottom - (y + height); + } + else + { + x += previous.Box.Left + previous.Box.Width; + y += previous.Box.Bottom; + } + start += previous.TextStart + previous.TextLength; + } + else if (parent is not null) + { + x += parent.Box.Left; + y = parent.Box.Bottom + parent.Box.Height - (y + height); + start += parent.TextStart; + } + var children = reader.U24(); + if (start < 0 || length < 0 || (long)start + length > textSize) + throw new InvalidDataException("A DjVu text zone points outside the text."); + var zone = new Zone(type, new DjvuRect(x, y, width, height), start, length); + if (parent is null) + root = zone; + else + parent.Children.Add(zone); + if (stack.Count > 0) + { + var top = stack.Pop(); + stack.Push((top.Zone, top.Remaining - 1, zone)); + } + if (children > 0) + { + stack.Push((zone, children, null)); + parent = zone; + previous = null; + continue; + } + // Closes the zones whose children are all read. + while (stack.Count > 0 && stack.Peek().Remaining == 0) + stack.Pop(); + if (stack.Count == 0) + return root!; + (parent, _, previous) = stack.Peek(); + } + } + + private static List Lines(Zone root, byte[] text) + { + var lines = new List(); + var pending = new Stack(); + pending.Push(root); + while (pending.Count > 0) + { + var zone = pending.Pop(); + if (zone.Type == Line || zone.Type >= Word || (zone.Children.Count == 0 && zone.Type > 1)) + { + var line = MakeLine(zone, text); + if (line is not null) + lines.Add(line); + continue; + } + for (var i = zone.Children.Count - 1; i >= 0; i--) + pending.Push(zone.Children[i]); + } + return lines; + } + + private static DjvuLine? MakeLine(Zone zone, byte[] text) + { + if (zone.Box.Width <= 0 || zone.Box.Height <= 0) + return null; + var whole = Text(text, zone.TextStart, zone.TextLength); + if (whole.Length == 0) + return null; + var words = new List(); + if (zone.Type == Line) + { + foreach (var child in zone.Children) + { + if (child.Type != Word || child.Box.Width <= 0) + continue; + var word = Text(text, child.TextStart, child.TextLength); + if (word.Length > 0) + words.Add(new DjvuWord(word, child.Box)); + } + } + else if (zone.Type >= Word) + { + words.Add(new DjvuWord(whole, zone.Box)); + } + return new DjvuLine(zone.Box, words, whole); + } + + /// A zone's text without the separators DjVu ends zones with (space, line feed, 0x0B, 0x1D, 0x1F). + private static string Text(byte[] text, int start, int length) + { + var value = Encoding.UTF8.GetString(text, start, length); + return value.Trim().Trim('\v', '\u001d', '\u001f', '\0').Trim(); + } +} + +/// Big-endian reading of a chunk's bytes; reading past the end is damage. +internal sealed class DjvuBytes(byte[] data, int offset = 0) +{ + private int _at = offset; + + public int Remaining => data.Length - _at; + + public int Position => _at; + + public int U8() + { + Need(1); + return data[_at++]; + } + + public int U16() + { + Need(2); + var value = (data[_at] << 8) | data[_at + 1]; + _at += 2; + return value; + } + + public int U24() + { + Need(3); + var value = (data[_at] << 16) | (data[_at + 1] << 8) | data[_at + 2]; + _at += 3; + return value; + } + + public long U32() + { + Need(4); + var value = ((long)data[_at] << 24) | ((long)data[_at + 1] << 16) | ((long)data[_at + 2] << 8) | data[_at + 3]; + _at += 4; + return value; + } + + /// A zero-terminated UTF-8 string. + public string CString() + { + var end = Array.IndexOf(data, (byte)0, _at); + if (end < 0) + throw new InvalidDataException("A DjVu chunk ends early."); + var value = Encoding.UTF8.GetString(data, _at, end - _at); + _at = end + 1; + return value; + } + + public byte[] Bytes(int count) + { + Need(count); + var value = data.AsSpan(_at, count).ToArray(); + _at += count; + return value; + } + + private void Need(int count) + { + if (count < 0 || count > data.Length - _at) + throw new InvalidDataException("A DjVu chunk ends early."); + } +} diff --git a/src/Filee.Engines/Djvu/Iw44Decoder.cs b/src/Filee.Engines/Djvu/Iw44Decoder.cs new file mode 100644 index 000000000..467e1081c --- /dev/null +++ b/src/Filee.Engines/Djvu/Iw44Decoder.cs @@ -0,0 +1,700 @@ +// IW44, DjVu's wavelet picture coding (DjVu v3 specification, "IW44"): the background (BG44) and foreground colours +// (FG44) of a page. A picture is a luminance map and, in colour, two chrominance maps (YCbCr, chrominance optionally at +// half resolution and starting a few slices late), each in blocks of 32 × 32 wavelet coefficients. The coefficients +// arrive in slices, from the coarsest band and the highest bit down: each slice tells which coefficients become +// significant at the current threshold, their signs, and one more bit of those already known. A picture may come in +// several chunks, each adding slices. +// +// Reconstruction: the coefficients of all blocks in one array, the lifting transform run backwards from scale 16 to 1 +// (scale 2 for half-resolution chrominance, then each value repeated over 2 × 2), each value rounded (six fraction +// bits) to −128..127, then the "Pigeon" YCbCr → RGB transform, or 127 − Y for a grey picture. + +using Filee.Engines.Documents; + +namespace Filee.Engines.Djvu; + +/// A decoded picture: RGB bytes, three per pixel, top row first. +internal sealed class DjvuPixmap(int width, int height, byte[] rgb) +{ + public int Width { get; } = width; + public int Height { get; } = height; + public byte[] Rgb { get; } = rgb; +} + +/// Decodes the chunks of one IW44 picture, in order. +internal sealed class Iw44Decoder(DjvuLimits limits) +{ + private Iw44Map? _y, _cb, _cr; + private Iw44Codec? _yCodec, _cbCodec, _crCodec; + private int _serial; + private int _slices; + private int _chromaDelay; + private bool _chromaHalf; + + public bool HasData => _y is not null; + + /// Adds one chunk (BG44, FG44, PM44, BM44). + public void Decode(byte[] chunk, CancellationToken cancellationToken = default) + { + if (chunk.Length < 2) + throw new InvalidDataException("An IW44 chunk is too short."); + var serial = chunk[0]; + var slices = chunk[1]; + if (serial != _serial) + throw new InvalidDataException("IW44 chunks are out of order."); + var at = 2; + if (serial == 0) + { + if (chunk.Length < 9) + throw new InvalidDataException("An IW44 chunk is too short."); + var major = chunk[2]; + var minor = chunk[3]; + if ((major & 0x7F) != 1) + throw new InvalidDataException("An IW44 picture uses a coder version Filee doesn't know."); + if (minor > 2) + throw new InvalidDataException("An IW44 picture uses a newer coder version than Filee knows."); + var width = (chunk[4] << 8) | chunk[5]; + var height = (chunk[6] << 8) | chunk[7]; + at = 8; + _chromaDelay = 0; + _chromaHalf = false; + if (minor >= 2) + { + var delay = chunk[at++]; + _chromaDelay = delay & 0x7F; + _chromaHalf = (delay & 0x80) == 0; + } + if ((major & 0x80) != 0) + _chromaDelay = -1; + if (width == 0 || height == 0) + throw new InvalidDataException("An IW44 picture has no size."); + if ((long)width * height > limits.MaxIw44Pixels) + throw DocumentLimits.Exceeded($"A DjVu picture has more than {DocumentLimits.Count(limits.MaxIw44Pixels)} pixels."); + _y = new Iw44Map(width, height); + _yCodec = new Iw44Codec(_y); + if (_chromaDelay >= 0) + { + _cb = new Iw44Map(width, height); + _cr = new Iw44Map(width, height); + _cbCodec = new Iw44Codec(_cb); + _crCodec = new Iw44Codec(_cr); + } + } + if (_yCodec is null) + throw new InvalidDataException("An IW44 picture is missing its first chunk."); + var total = _slices + slices; + if (total > limits.MaxSlices) + throw DocumentLimits.Exceeded($"A DjVu picture has more than {DocumentLimits.Count(limits.MaxSlices)} slices."); + var zp = new ZpDecoder(chunk, at, chunk.Length - at); + var more = true; + while (more && _slices < total) + { + cancellationToken.ThrowIfCancellationRequested(); + more = _yCodec.DecodeSlice(zp); + if (_cbCodec is not null && _crCodec is not null && _chromaDelay <= _slices) + { + more |= _cbCodec.DecodeSlice(zp); + more |= _crCodec.DecodeSlice(zp); + } + _slices++; + } + _serial++; + } + + /// The picture so far. + public DjvuPixmap ToPixmap(CancellationToken cancellationToken = default) + { + var y = _y ?? throw new InvalidDataException("An IW44 picture has no data."); + int w = y.Width, h = y.Height; + var luma = y.Reconstruct(half: false, cancellationToken); + var rgb = new byte[(long)w * h * 3]; + var color = _cb is not null && _cr is not null && _chromaDelay >= 0; + var cb = color ? _cb!.Reconstruct(_chromaHalf, cancellationToken) : null; + var cr = color ? _cr!.Reconstruct(_chromaHalf, cancellationToken) : null; + // The maps' row 0 is the bottom of the picture (as DjVu draws everything); the pixmap's is the top. + for (var row = 0; row < h; row++) + { + var i = row * w; + var o = (h - 1 - row) * w * 3; + for (var x = 0; x < w; x++, i++, o += 3) + { + int yy = luma[i]; + if (!color) + { + rgb[o] = rgb[o + 1] = rgb[o + 2] = (byte)(127 - yy); + continue; + } + int b = cb![i], r = cr![i]; + var t1 = b >> 2; + var t2 = r + (r >> 1); + var t3 = yy + 128 - t1; + rgb[o] = Clamp(yy + 128 + t2); + rgb[o + 1] = Clamp(t3 - (t2 >> 1)); + rgb[o + 2] = Clamp(t3 + (b << 1)); + } + } + return new DjvuPixmap(w, h, rgb); + } + + private static byte Clamp(int value) => (byte)Math.Clamp(value, 0, 255); +} + +/// The coefficients of one IW44 component, block by block, each bucket allocated when first coded. +internal sealed class Iw44Map +{ + public Iw44Map(int width, int height) + { + Width = width; + Height = height; + BlockWidth = (width + 31) & ~31; + BlockHeight = (height + 31) & ~31; + Blocks = new Iw44Block[BlockWidth / 32 * (BlockHeight / 32)]; + } + + public int Width { get; } + public int Height { get; } + public int BlockWidth { get; } + public int BlockHeight { get; } + public Iw44Block[] Blocks { get; } + + /// The component's values (−128..127), row 0 at the bottom of the picture. + public sbyte[] Reconstruct(bool half, CancellationToken cancellationToken) + { + int bw = BlockWidth, bh = BlockHeight; + var data = new short[(long)bw * bh]; + var index = 0; + for (var i = 0; i < bh; i += 32) + { + for (var j = 0; j < bw; j += 32) + { + var block = Blocks[index++]; + if (block.Coefficients is not { } coefficients) + continue; + for (var n = 0; n < 1024; n++) + { + if (!block.Has(n >> 4)) + { + n |= 15; + continue; + } + var at = ZigZag[n]; + data[(i + (at >> 5)) * bw + j + (at & 31)] = coefficients[n]; + } + } + } + cancellationToken.ThrowIfCancellationRequested(); + Iw44Transform.Backward(data, Width, Height, bw, 32, half ? 2 : 1); + if (half) + { + for (var i = 0; i < bh; i += 2) + { + var row = i * bw; + for (var j = 0; j < bw; j += 2) + { + var v = data[row + j]; + data[row + j + 1] = v; + if (i + 1 < bh) + data[row + bw + j] = data[row + bw + j + 1] = v; + } + } + } + var result = new sbyte[(long)Width * Height]; + for (var i = 0; i < Height; i++) + { + var row = i * bw; + var o = i * Width; + for (var j = 0; j < Width; j++) + result[o + j] = (sbyte)Math.Clamp((data[row + j] + 32) >> 6, -128, 127); + } + return result; + } + + /// + /// Where coefficient n of a block lies in its 32 × 32 square (row × 32 + column): n's bits, from the lowest, are + /// alternately the column's and the row's, from their highest bit down. + /// + internal static readonly int[] ZigZag = BuildZigZag(); + + private static int[] BuildZigZag() + { + var table = new int[1024]; + for (var n = 0; n < 1024; n++) + { + int x = 0, y = 0; + for (var bit = 0; bit < 5; bit++) + { + x |= ((n >> (2 * bit)) & 1) << (4 - bit); + y |= ((n >> (2 * bit + 1)) & 1) << (4 - bit); + } + table[n] = y * 32 + x; + } + return table; + } +} + +/// A block's 64 buckets of 16 coefficients; a bucket not yet coded is absent. +internal struct Iw44Block +{ + private ulong _present; + + public short[]? Coefficients { get; private set; } + + public readonly bool Has(int bucket) => (_present & (1UL << bucket)) != 0; + + /// Allocates bucket (zeroes) when it is absent. + public void Allocate(int bucket) + { + Coefficients ??= new short[1024]; + _present |= 1UL << bucket; + } +} + +/// Decodes slices into one component's coefficients. +internal sealed class Iw44Codec +{ + private const int Zero = 1, Active = 2, New = 4, Unknown = 8; + + internal static readonly int[] Quantization = + [ + 0x004000, 0x008000, 0x008000, 0x010000, 0x010000, 0x010000, 0x020000, 0x020000, + 0x020000, 0x040000, 0x040000, 0x040000, 0x080000, 0x040000, 0x040000, 0x080000, + ]; + + /// The first bucket and the number of buckets of each band. + internal static readonly (int Start, int Size)[] Bands = + [(0, 1), (1, 1), (2, 1), (3, 1), (4, 4), (8, 4), (12, 4), (16, 16), (32, 16), (48, 16)]; + + private readonly Iw44Map _map; + private readonly int[] _quantLow = new int[16]; + private readonly int[] _quantHigh = new int[10]; + private readonly byte[] _coefficientState = new byte[256]; + private readonly byte[] _bucketState = new byte[16]; + private readonly byte[] _contextStart = new byte[32]; + private readonly byte[,] _contextBucket = new byte[10, 8]; + private byte _contextMantissa; + private byte _contextRoot; + private int _band; + private int _bit = 1; + + public Iw44Codec(Iw44Map map) + { + _map = map; + var i = 0; + var q = 0; + for (; i < 4; i++) + _quantLow[i] = Quantization[q++]; + for (var group = 0; group < 3; group++, q++) + { + for (var j = 0; j < 4; j++) + _quantLow[i++] = Quantization[q]; + } + _quantHigh[0] = 0; + for (var j = 1; j < 10; j++) + _quantHigh[j] = Quantization[q++]; + } + + /// Decodes one slice; false once the component has no more. + public bool DecodeSlice(ZpDecoder zp) + { + if (_bit < 0) + return false; + if (!IsNullSlice()) + { + var (start, size) = Bands[_band]; + var blocks = _map.Blocks; + for (var b = 0; b < blocks.Length; b++) + DecodeBuckets(zp, _band, ref blocks[b], start, size); + } + return FinishSlice(); + } + + private bool IsNullSlice() + { + if (_band == 0) + { + var isNull = true; + for (var i = 0; i < 16; i++) + { + var threshold = _quantLow[i]; + _coefficientState[i] = Zero; + if (threshold is > 0 and < 0x8000) + { + _coefficientState[i] = Unknown; + isNull = false; + } + } + return isNull; + } + var high = _quantHigh[_band]; + return !(high is > 0 and < 0x8000); + } + + private bool FinishSlice() + { + _quantHigh[_band] >>= 1; + if (_band == 0) + { + for (var i = 0; i < 16; i++) + _quantLow[i] >>= 1; + } + if (++_band >= Bands.Length) + { + _band = 0; + _bit++; + if (_quantHigh[Bands.Length - 1] == 0) + { + _bit = -1; + return false; + } + } + return true; + } + + /// The states of the band's buckets and coefficients before a slice; their union. + private int Prepare(int first, int count, ref Iw44Block block) + { + var all = 0; + var coefficients = block.Coefficients; + if (first != 0) + { + for (var bucket = 0; bucket < count; bucket++) + { + var state = 0; + if (!block.Has(first + bucket)) + { + state = Unknown; + } + else + { + var at = (first + bucket) << 4; + for (var i = 0; i < 16; i++) + { + var c = coefficients![at + i] != 0 ? Active : Unknown; + _coefficientState[(bucket << 4) + i] = (byte)c; + state |= c; + } + } + _bucketState[bucket] = (byte)state; + all |= state; + } + } + else + { + if (!block.Has(0)) + { + all = Unknown; + } + else + { + for (var i = 0; i < 16; i++) + { + int c = _coefficientState[i]; + if (c != Zero) + c = coefficients![i] != 0 ? Active : Unknown; + _coefficientState[i] = (byte)c; + all |= c; + } + } + _bucketState[0] = (byte)all; + } + return all; + } + + private void DecodeBuckets(ZpDecoder zp, int band, ref Iw44Block block, int first, int count) + { + var all = Prepare(first, count, ref block); + if (count < 16 || (all & Active) != 0) + all |= New; + else if ((all & Unknown) != 0 && zp.Decode(ref _contextRoot) != 0) + all |= New; + + if ((all & New) != 0) + { + for (var bucket = 0; bucket < count; bucket++) + { + if ((_bucketState[bucket] & Unknown) == 0) + continue; + var ctx = 0; + if (band > 0) + { + // How many of the four coefficients of the parent bucket are significant. + var k = (first + bucket) << 2; + if (block.Has(k >> 4)) + { + var c = block.Coefficients!; + var at = ((k >> 4) << 4) + (k & 15); + if (c[at] != 0) + ctx++; + if (c[at + 1] != 0) + ctx++; + if (c[at + 2] != 0) + ctx++; + if (ctx < 3 && c[at + 3] != 0) + ctx++; + } + } + if ((all & Active) != 0) + ctx |= 4; + if (zp.Decode(ref _contextBucket[band, ctx]) != 0) + _bucketState[bucket] |= New; + } + + var threshold = _quantHigh[band]; + for (var bucket = 0; bucket < count; bucket++) + { + if ((_bucketState[bucket] & New) == 0) + continue; + var state = bucket << 4; + if (!block.Has(first + bucket)) + { + block.Allocate(first + bucket); + for (var i = 0; i < 16; i++) + { + if (first != 0 || _coefficientState[state + i] != Zero) + _coefficientState[state + i] = Unknown; + } + } + var coefficients = block.Coefficients!; + var at = (first + bucket) << 4; + var gotcha = 0; + for (var i = 0; i < 16; i++) + { + if ((_coefficientState[state + i] & Unknown) != 0) + gotcha++; + } + for (var i = 0; i < 16; i++) + { + if ((_coefficientState[state + i] & Unknown) == 0) + continue; + if (band == 0) + threshold = _quantLow[i]; + var ctx = Math.Min(gotcha, 7); + if ((_bucketState[bucket] & Active) != 0) + ctx |= 8; + if (zp.Decode(ref _contextStart[ctx]) != 0) + { + _coefficientState[state + i] |= New; + var half = threshold >> 1; + var value = threshold + half - (half >> 2); + coefficients[at + i] = (short)(zp.DecodeIw() != 0 ? -value : value); + } + if ((_coefficientState[state + i] & New) != 0) + gotcha = 0; + else if (gotcha > 0) + gotcha--; + } + } + } + + if ((all & Active) != 0) + { + var threshold = _quantHigh[band]; + var coefficients = block.Coefficients!; + for (var bucket = 0; bucket < count; bucket++) + { + if ((_bucketState[bucket] & Active) == 0) + continue; + var state = bucket << 4; + var at = (first + bucket) << 4; + for (var i = 0; i < 16; i++) + { + if ((_coefficientState[state + i] & Active) == 0) + continue; + int value = coefficients[at + i]; + var magnitude = Math.Abs(value); + if (band == 0) + threshold = _quantLow[i]; + if (magnitude <= 3 * threshold) + { + magnitude += threshold >> 2; + magnitude += zp.Decode(ref _contextMantissa) != 0 ? threshold >> 1 : -threshold + (threshold >> 1); + } + else + { + magnitude += zp.DecodeIw() != 0 ? threshold >> 1 : -threshold + (threshold >> 1); + } + coefficients[at + i] = (short)(value > 0 ? magnitude : -magnitude); + } + } + } + } +} + +/// The IW44 lifting transform, backwards (coefficients → pixels). +internal static class Iw44Transform +{ + /// Runs scales /2 down to over . + public static void Backward(short[] p, int w, int h, int rowsize, int begin, int end) + { + for (var scale = begin >> 1; scale >= end; scale >>= 1) + { + Vertical(p, w, h, rowsize, scale); + Horizontal(p, w, h, rowsize, scale); + } + } + + private static void Vertical(short[] d, int w, int h, int rowsize, int scale) + { + var y = 0; + var s = scale * rowsize; + var s3 = s + s + s; + h = ((h - 1) / scale) + 1; + var p = 0; + while (y - 3 < h) + { + // Undoes the prediction of the odd rows. + var q = p; + var e = q + w; + if (y >= 3 && y + 3 < h) + { + for (; q < e; q += scale) + { + var a = d[q - s] + d[q + s]; + var b = d[q - s3] + d[q + s3]; + d[q] = (short)(d[q] - (((a << 3) + a - b + 16) >> 5)); + } + } + else if (y < h) + { + var q1 = y + 1 < h ? q + s : -1; + var q3 = y + 3 < h ? q + s3 : -1; + for (; q < e; q += scale) + { + var below1 = q1 >= 0 ? d[q1] : 0; + var below3 = q3 >= 0 ? d[q3] : 0; + int a, b; + if (y >= 3) + { + a = d[q - s] + below1; + b = d[q - s3] + below3; + } + else if (y >= 1) + { + a = d[q - s] + below1; + b = below3; + } + else + { + a = below1; + b = below3; + } + d[q] = (short)(d[q] - (((a << 3) + a - b + 16) >> 5)); + if (q1 >= 0) + q1 += scale; + if (q3 >= 0) + q3 += scale; + } + } + // Undoes the update of the even rows three rows up. + q = p - s3; + e = q + w; + if (y >= 6 && y < h) + { + for (; q < e; q += scale) + { + var a = d[q - s] + d[q + s]; + var b = d[q - s3] + d[q + s3]; + d[q] = (short)(d[q] + (((a << 3) + a - b + 8) >> 4)); + } + } + else if (y >= 3) + { + var q1 = y - 2 < h ? q + s : q - s; + for (; q < e; q += scale, q1 += scale) + { + var a = d[q - s] + d[q1]; + d[q] = (short)(d[q] + ((a + 1) >> 1)); + } + } + y += 2; + p += s + s; + } + } + + private static void Horizontal(short[] d, int w, int h, int rowsize, int scale) + { + var s = scale; + var s3 = s + s + s; + rowsize *= scale; + var p = 0; + for (var y = 0; y < h; y += scale, p += rowsize) + { + var q = p; + var e = p + w; + int a0 = 0, a1 = 0, a2 = 0, a3 = 0; + int b0 = 0, b1 = 0, b2 = 0, b3 = 0; + if (q < e) + { + if (q + s < e) + a2 = d[q + s]; + if (q + s3 < e) + a3 = d[q + s3]; + b2 = b3 = d[q] - ((((a1 + a2) << 3) + (a1 + a2) - a0 - a3 + 16) >> 5); + d[q] = (short)b3; + q += s + s; + } + if (q < e) + { + a0 = a1; + a1 = a2; + a2 = a3; + if (q + s3 < e) + a3 = d[q + s3]; + b3 = d[q] - ((((a1 + a2) << 3) + (a1 + a2) - a0 - a3 + 16) >> 5); + d[q] = (short)b3; + q += s + s; + } + if (q < e) + { + b1 = b2; + b2 = b3; + a0 = a1; + a1 = a2; + a2 = a3; + if (q + s3 < e) + a3 = d[q + s3]; + b3 = d[q] - ((((a1 + a2) << 3) + (a1 + a2) - a0 - a3 + 16) >> 5); + d[q] = (short)b3; + d[q - s3] = (short)(d[q - s3] + ((b1 + b2 + 1) >> 1)); + q += s + s; + } + while (q + s3 < e) + { + a0 = a1; + a1 = a2; + a2 = a3; + a3 = d[q + s3]; + b0 = b1; + b1 = b2; + b2 = b3; + b3 = d[q] - ((((a1 + a2) << 3) + (a1 + a2) - a0 - a3 + 16) >> 5); + d[q] = (short)b3; + d[q - s3] = (short)(d[q - s3] + ((((b1 + b2) << 3) + (b1 + b2) - b0 - b3 + 8) >> 4)); + q += s + s; + } + while (q < e) + { + a0 = a1; + a1 = a2; + a2 = a3; + a3 = 0; + b0 = b1; + b1 = b2; + b2 = b3; + b3 = d[q] - ((((a1 + a2) << 3) + (a1 + a2) - a0 - a3 + 16) >> 5); + d[q] = (short)b3; + d[q - s3] = (short)(d[q - s3] + ((((b1 + b2) << 3) + (b1 + b2) - b0 - b3 + 8) >> 4)); + q += s + s; + } + while (q - s3 < e) + { + b0 = b1; + b1 = b2; + b2 = b3; + if (q - s3 >= p) + d[q - s3] = (short)(d[q - s3] + ((b1 + b2 + 1) >> 1)); + q += s + s; + } + } + } +} diff --git a/src/Filee.Engines/Djvu/Jb2Decoder.cs b/src/Filee.Engines/Djvu/Jb2Decoder.cs new file mode 100644 index 000000000..4d54d3ed9 --- /dev/null +++ b/src/Filee.Engines/Djvu/Jb2Decoder.cs @@ -0,0 +1,519 @@ +// JB2, DjVu's bilevel picture coding (DjVu v3 specification, "JB2"): the mask of a page (Sjbz) or a dictionary of +// shapes several pages share (Djbz). A stream is a list of records coded with the ZP-coder: new shapes coded pixel by +// pixel (a 10-pixel context), shapes coded as refinements of a library shape (an 11-pixel context over both), copies of +// library shapes, and where each one goes (relative to the shape before it or to the start of its line). +// +// Numbers are coded in a binary tree of contexts that grows as values are seen (the "number coder"): its cells are +// counted against DjvuLimits.MaxNumberCells, the shapes, blits and their pixels against the other JB2 limits. +// +// Bitmaps are kept as the specification draws them: one byte per pixel, row 0 at the bottom. + +using Filee.Engines.Documents; + +namespace Filee.Engines.Djvu; + +/// One shape: its pixels (1 = black), row 0 at the bottom, and the box of its black pixels. +internal sealed class Jb2Shape(int width, int height) +{ + public int Width { get; } = width; + public int Height { get; } = height; + public byte[] Bits { get; } = new byte[(long)width * height]; + + /// The black pixels' box (left, bottom, right, top, inclusive); right < left when the shape is empty. + public (int Left, int Bottom, int Right, int Top) Box { get; private set; } + + public byte this[int x, int y] => (uint)x < (uint)Width && (uint)y < (uint)Height ? Bits[y * Width + x] : (byte)0; + + public void ComputeBox() + { + int left = Width, right = -1, bottom = Height, top = -1; + for (var y = 0; y < Height; y++) + { + var row = y * Width; + for (var x = 0; x < Width; x++) + { + if (Bits[row + x] == 0) + continue; + if (x < left) + left = x; + if (x > right) + right = x; + if (y < bottom) + bottom = y; + top = y; + } + } + // An empty shape gets the box the specification's bounding box search ends with. + Box = right < 0 ? (0, 0, -1, -1) : (left, bottom, right, top); + } +} + +/// A placed shape: its index and where its bottom-left pixel goes (row 0 at the bottom of the page). +internal readonly record struct Jb2Blit(int Shape, int Left, int Bottom); + +/// Shapes a JB2 dictionary (Djbz) holds, inherited ones first. +internal sealed class Jb2Dictionary(List shapes) +{ + public IReadOnlyList Shapes { get; } = shapes; +} + +/// A decoded JB2 page mask. +internal sealed class Jb2Image(int width, int height, List shapes, List blits) +{ + public int Width { get; } = width; + public int Height { get; } = height; + public IReadOnlyList Shapes { get; } = shapes; + public IReadOnlyList Blits { get; } = blits; +} + +/// Decodes JB2 streams. +internal sealed class Jb2Decoder +{ + private const int StartOfData = 0; + private const int NewMark = 1; + private const int NewMarkLibraryOnly = 2; + private const int NewMarkImageOnly = 3; + private const int MatchedRefine = 4; + private const int MatchedRefineLibraryOnly = 5; + private const int MatchedRefineImageOnly = 6; + private const int MatchedCopy = 7; + private const int NonMarkData = 8; + private const int RequiredDictOrReset = 9; + private const int PreservedComment = 10; + private const int EndOfData = 11; + + private const int BigPositive = 262142; + private const int BigNegative = -262143; + + private readonly ZpDecoder _zp; + private readonly DjvuLimits _limits; + private readonly Jb2Dictionary? _inherited; + private readonly CancellationToken _cancellationToken; + + // The number coder's tree: a bit context per cell and its two children (0 = none yet). Cell 0 is unused. + private byte[] _bits = new byte[1024]; + private int[] _left = new int[1024]; + private int[] _right = new int[1024]; + private int _cells = 1; + + private int _recordType, _matchIndex, _commentLength, _commentByte; + private int _absLocX, _absLocY, _absSizeX, _absSizeY, _imageSize, _inheritedCount; + private int _relLocXCurrent, _relLocXLast, _relLocYCurrent, _relLocYLast, _relSizeX, _relSizeY; + private byte _refinementFlag, _offsetType; + private readonly byte[] _direct = new byte[1024]; + private readonly byte[] _cross = new byte[2048]; + + private bool _started; + private bool _usesInherited; + private int _columns, _rows; + private int _lastLeft, _lastRight, _lastBottom, _lastRowLeft, _lastRowBottom; + private readonly int[] _shortList = new int[3]; + private int _shortListPosition; + + private readonly List _shapes = []; + private readonly List _library = []; // library index → shape index + private readonly List _blits = []; + private long _shapePixels; + + private Jb2Decoder(byte[] data, Jb2Dictionary? inherited, DjvuLimits limits, CancellationToken cancellationToken) + { + _zp = new ZpDecoder(data); + _inherited = inherited; + _limits = limits; + _cancellationToken = cancellationToken; + } + + /// + /// Decodes a page mask (Sjbz); is the shared dictionary it may ask for, or null. + /// + public static Jb2Image DecodeImage(byte[] data, Jb2Dictionary? dictionary, DjvuLimits limits, CancellationToken cancellationToken = default) + { + var decoder = new Jb2Decoder(data, dictionary, limits, cancellationToken); + decoder.Run(image: true); + return new Jb2Image(decoder._columns, decoder._rows, decoder._shapes, decoder._blits); + } + + /// Decodes a shared dictionary (Djbz); is a dictionary it may build on. + public static Jb2Dictionary DecodeDictionary(byte[] data, Jb2Dictionary? inherited, DjvuLimits limits, CancellationToken cancellationToken = default) + { + var decoder = new Jb2Decoder(data, inherited, limits, cancellationToken); + decoder.Run(image: false); + return new Jb2Dictionary(decoder._shapes); + } + + private void Run(bool image) + { + var records = 0; + while (true) + { + if ((++records & 0x3FF) == 0) + _cancellationToken.ThrowIfCancellationRequested(); + var type = DecodeNumber(StartOfData, EndOfData, ref _recordType); + if (type == EndOfData) + break; + if (!image && type is NewMark or NewMarkImageOnly or MatchedRefine or MatchedRefineImageOnly or MatchedCopy or NonMarkData) + throw new InvalidDataException("A JB2 dictionary holds a record only a page may hold."); + if (type is not (StartOfData or RequiredDictOrReset or PreservedComment) && !_started) + throw new InvalidDataException("A JB2 stream places shapes before its start record."); + switch (type) + { + case StartOfData: + Start(image); + break; + case NewMark: + AddShape(DecodeDirect(DecodeAbsoluteSize()), library: true, placed: true); + break; + case NewMarkLibraryOnly: + AddShape(DecodeDirect(DecodeAbsoluteSize()), library: true, placed: false); + break; + case NewMarkImageOnly: + AddShape(DecodeDirect(DecodeAbsoluteSize()), library: false, placed: true); + break; + case MatchedRefine: + AddShape(DecodeRefinement(), library: true, placed: true); + break; + case MatchedRefineLibraryOnly: + AddShape(DecodeRefinement(), library: true, placed: false); + break; + case MatchedRefineImageOnly: + AddShape(DecodeRefinement(), library: false, placed: true); + break; + case MatchedCopy: + { + var match = DecodeMatch(); + var shapeIndex = _library[match]; + var box = _shapes[shapeIndex].Box; + var (left, bottom) = DecodeRelativeLocation(box.Top - box.Bottom + 1, box.Right - box.Left + 1); + AddBlit(shapeIndex, left - box.Left, bottom - box.Bottom); + break; + } + case NonMarkData: + { + var shape = DecodeDirect(DecodeAbsoluteSize()); + var index = Keep(shape, library: false); + var left = DecodeNumber(1, _columns, ref _absLocX); + var top = DecodeNumber(1, _rows, ref _absLocY); + AddBlit(index, left - 1, top - shape.Height); + break; + } + case RequiredDictOrReset: + if (!_started) + RequireDictionary(); + else + ResetNumbers(); + break; + case PreservedComment: + { + var length = DecodeNumber(0, BigPositive, ref _commentLength); + for (var i = 0; i < length; i++) + DecodeNumber(0, 255, ref _commentByte); + break; + } + default: + throw new InvalidDataException("A JB2 stream has a record of unknown type."); + } + } + if (!_started) + throw new InvalidDataException("A JB2 stream has no start record."); + } + + private void Start(bool image) + { + if (_started) + throw new InvalidDataException("A JB2 stream has two start records."); + var columns = DecodeNumber(0, BigPositive, ref _imageSize); + var rows = DecodeNumber(0, BigPositive, ref _imageSize); + if (image) + { + if (columns == 0 || rows == 0) + throw new InvalidDataException("A JB2 page mask has no size."); + if ((long)columns * rows > _limits.MaxPagePixels) + throw DocumentLimits.Exceeded($"A DjVu page mask has more than {DocumentLimits.Count(_limits.MaxPagePixels)} pixels."); + _columns = columns; + _rows = rows; + _lastLeft = 1 + columns; + _lastRowBottom = rows; + } + else + { + if (columns != 0 || rows != 0) + throw new InvalidDataException("A JB2 dictionary has a page size."); + _lastLeft = 1; + _lastRowBottom = 0; + } + _lastRowLeft = 0; + _lastRight = 0; + FillShortList(_lastRowBottom); + _started = true; + _zp.Decode(ref _refinementFlag); + // The library starts with the inherited dictionary's shapes (all of them were library shapes there). + if (_usesInherited && _inherited is not null) + { + foreach (var shape in _inherited.Shapes) + { + _library.Add(_shapes.Count); + _shapes.Add(shape); + } + } + } + + private void RequireDictionary() + { + var count = DecodeNumber(0, BigPositive, ref _inheritedCount); + if (count == 0) + return; + if (_inherited is null) + throw new InvalidDataException("A DjVu page needs a shared shape dictionary its file doesn't include."); + if (_inherited.Shapes.Count != count) + throw new InvalidDataException("A DjVu page's shared shape dictionary has another number of shapes than it expects."); + _usesInherited = true; + } + + private void ResetNumbers() + { + _recordType = _matchIndex = _commentLength = _commentByte = 0; + _absLocX = _absLocY = _absSizeX = _absSizeY = _imageSize = _inheritedCount = 0; + _relLocXCurrent = _relLocXLast = _relLocYCurrent = _relLocYLast = _relSizeX = _relSizeY = 0; + Array.Clear(_bits, 0, _cells); + Array.Clear(_left, 0, _cells); + Array.Clear(_right, 0, _cells); + _cells = 1; + } + + private int DecodeMatch() + { + if (_library.Count == 0) + throw new InvalidDataException("A JB2 stream refers to a library shape before it has any."); + return DecodeNumber(0, _library.Count - 1, ref _matchIndex); + } + + private (int Width, int Height) DecodeAbsoluteSize() + { + var width = DecodeNumber(0, BigPositive, ref _absSizeX); + var height = DecodeNumber(0, BigPositive, ref _absSizeY); + return CheckSize(width, height); + } + + private (int Width, int Height) CheckSize(int width, int height) + { + if (width is < 0 or > ushort.MaxValue || height is < 0 or > ushort.MaxValue) + throw new InvalidDataException("A JB2 shape has an impossible size."); + _shapePixels += (long)width * height; + if (_shapePixels > _limits.MaxShapePixels) + throw DocumentLimits.Exceeded($"The shapes of a DjVu mask have more than {DocumentLimits.Count(_limits.MaxShapePixels)} pixels."); + if (_shapes.Count >= _limits.MaxShapes) + throw DocumentLimits.Exceeded($"A DjVu mask has more than {DocumentLimits.Count(_limits.MaxShapes)} shapes."); + return (width, height); + } + + /// A new shape coded pixel by pixel, top row first. + private Jb2Shape DecodeDirect((int Width, int Height) size) + { + var shape = new Jb2Shape(size.Width, size.Height); + var bits = shape.Bits; + var w = size.Width; + for (var y = size.Height - 1; y >= 0; y--) + { + if ((y & 0x3F) == 0) + _cancellationToken.ThrowIfCancellationRequested(); + for (var x = 0; x < w; x++) + { + var context = + (shape[x - 1, y + 2] << 9) | (shape[x, y + 2] << 8) | (shape[x + 1, y + 2] << 7) | + (shape[x - 2, y + 1] << 6) | (shape[x - 1, y + 1] << 5) | (shape[x, y + 1] << 4) | + (shape[x + 1, y + 1] << 3) | (shape[x + 2, y + 1] << 2) | + (shape[x - 2, y] << 1) | shape[x - 1, y]; + bits[y * w + x] = (byte)_zp.Decode(ref _direct[context]); + } + } + return shape; + } + + /// A shape coded as a refinement of a library shape (its size relative to the library shape's box). + private Jb2Shape DecodeRefinement() + { + var match = DecodeMatch(); + var reference = _shapes[_library[match]]; + var box = reference.Box; + var dx = DecodeNumber(BigNegative, BigPositive, ref _relSizeX); + var dy = DecodeNumber(BigNegative, BigPositive, ref _relSizeY); + var (w, h) = CheckSize(box.Right - box.Left + 1 + dx, box.Top - box.Bottom + 1 + dy); + var shape = new Jb2Shape(w, h); + // Aligns the centres of the new shape and the reference's box. + var xd2c = (w / 2 - w + 1) - ((box.Right - box.Left + 1) / 2 - box.Right); + var yd2c = (h / 2 - h + 1) - ((box.Top - box.Bottom + 1) / 2 - box.Top); + if (xd2c is < -15 or > 15 || yd2c is < -15 or > 15) + throw new InvalidDataException("A JB2 refinement is too far from its library shape."); + var bits = shape.Bits; + for (var y = h - 1; y >= 0; y--) + { + if ((y & 0x3F) == 0) + _cancellationToken.ThrowIfCancellationRequested(); + var cy = y + yd2c; + for (var x = 0; x < w; x++) + { + var cx = x + xd2c; + var context = + (shape[x - 1, y + 1] << 10) | (shape[x, y + 1] << 9) | (shape[x + 1, y + 1] << 8) | + (shape[x - 1, y] << 7) | + (reference[cx, cy + 1] << 6) | + (reference[cx - 1, cy] << 5) | (reference[cx, cy] << 4) | (reference[cx + 1, cy] << 3) | + (reference[cx - 1, cy - 1] << 2) | (reference[cx, cy - 1] << 1) | reference[cx + 1, cy - 1]; + bits[y * w + x] = (byte)_zp.Decode(ref _cross[context]); + } + } + return shape; + } + + private int Keep(Jb2Shape shape, bool library) + { + shape.ComputeBox(); + var index = _shapes.Count; + _shapes.Add(shape); + if (library) + _library.Add(index); + return index; + } + + private void AddShape(Jb2Shape shape, bool library, bool placed) + { + var index = Keep(shape, library); + if (!placed) + return; + var (left, bottom) = DecodeRelativeLocation(shape.Height, shape.Width); + AddBlit(index, left, bottom); + } + + private void AddBlit(int shape, int left, int bottom) + { + if (_blits.Count >= _limits.MaxBlits) + throw DocumentLimits.Exceeded($"A DjVu mask places more than {DocumentLimits.Count(_limits.MaxBlits)} shapes."); + _blits.Add(new Jb2Blit(shape, left, bottom)); + } + + /// Where a shape goes: after the one before it on its line, or at the start of a new line. + private (int Left, int Bottom) DecodeRelativeLocation(int rows, int columns) + { + int left, bottom; + if (_zp.Decode(ref _offsetType) != 0) + { + var dx = DecodeNumber(BigNegative, BigPositive, ref _relLocXLast); + var dy = DecodeNumber(BigNegative, BigPositive, ref _relLocYLast); + left = _lastRowLeft + dx; + var top = _lastRowBottom + dy; + bottom = top - rows + 1; + _lastLeft = _lastRowLeft = left; + _lastRight = left + columns - 1; + _lastBottom = _lastRowBottom = bottom; + FillShortList(bottom); + } + else + { + var dx = DecodeNumber(BigNegative, BigPositive, ref _relLocXCurrent); + var dy = DecodeNumber(BigNegative, BigPositive, ref _relLocYCurrent); + left = _lastRight + dx; + bottom = _lastBottom + dy; + _lastLeft = left; + _lastRight = left + columns - 1; + _lastBottom = UpdateShortList(bottom); + } + return (left - 1, bottom - 1); + } + + private void FillShortList(int value) + { + _shortList[0] = _shortList[1] = _shortList[2] = value; + _shortListPosition = 0; + } + + /// Adds a bottom to the last three and returns their median. + private int UpdateShortList(int value) + { + if (++_shortListPosition == 3) + _shortListPosition = 0; + var s = _shortList; + s[_shortListPosition] = value; + return s[0] >= s[1] + ? (s[0] > s[2] ? (s[1] >= s[2] ? s[1] : s[2]) : s[0]) + : (s[0] < s[2] ? (s[1] >= s[2] ? s[2] : s[1]) : s[0]); + } + + /// A number in [, ] from the tree rooted at . + private int DecodeNumber(int low, int high, ref int root) + { + var negative = false; + var cutoff = 0; + var phase = 1; + var range = -1; + if (root == 0) + root = NewCell(); + var cell = root; + while (true) + { + var decision = low >= cutoff || (high >= cutoff && _zp.Decode(ref _bits[cell]) != 0); + switch (phase) + { + case 1: + negative = !decision; + if (negative) + { + var temp = -low - 1; + low = -high - 1; + high = temp; + } + phase = 2; + cutoff = 1; + break; + case 2: + if (!decision) + { + phase = 3; + range = (cutoff + 1) / 2; + if (range == 1) + cutoff = 0; + else + cutoff -= range / 2; + } + else + { + cutoff += cutoff + 1; + } + break; + default: + range /= 2; + if (range != 1) + cutoff += decision ? range / 2 : -(range / 2); + else if (!decision) + cutoff--; + break; + } + if (range == 1) + break; + var next = decision ? _right[cell] : _left[cell]; + if (next == 0) + { + next = NewCell(); + if (decision) + _right[cell] = next; + else + _left[cell] = next; + } + cell = next; + } + return negative ? -cutoff - 1 : cutoff; + } + + private int NewCell() + { + if (_cells >= _limits.MaxNumberCells) + throw DocumentLimits.Exceeded("A DjVu mask codes more numbers than Filee reads."); + if (_cells == _bits.Length) + { + var size = Math.Min((long)_bits.Length * 2, _limits.MaxNumberCells); + Array.Resize(ref _bits, (int)size); + Array.Resize(ref _left, (int)size); + Array.Resize(ref _right, (int)size); + } + return _cells++; + } +} diff --git a/src/Filee.Engines/Djvu/ZpDecoder.cs b/src/Filee.Engines/Djvu/ZpDecoder.cs new file mode 100644 index 000000000..6f1539709 --- /dev/null +++ b/src/Filee.Engines/Djvu/ZpDecoder.cs @@ -0,0 +1,218 @@ +// The ZP-coder of DjVu (DjVu v3 specification, appendix: the adaptive binary arithmetic coder every compressed DjVu +// chunk uses: JB2 masks, IW44 wavelet pictures, BZZ streams). Decoding only. A bit is decoded either with an adaptive +// context (a byte holding a state of the probability table; its low bit is the more probable value) or without one +// (the "raw" and IW44 forms, a fixed probability). +// +// The table is the specification's (P: probability of the less probable value, M: the threshold above which a more +// probable value moves the state up, UP and DN: the next states). Past the end of its data the coder reads 0xFF bytes, +// as the specification says; a stream that asks for 25 bytes more than it has is damaged. + +namespace Filee.Engines.Djvu; + +/// Decodes bits of one ZP-coded stream. +internal sealed class ZpDecoder +{ + private readonly byte[] _data; + private readonly int _end; + private int _position; + private int _delay = 25; + + private uint _a; + private uint _code; + private uint _fence; + private uint _buffer; + private int _scount; + + /// Starts decoding bytes of from . + public ZpDecoder(byte[] data, int offset, int count) + { + _data = data; + _position = offset; + _end = offset + count; + _code = (uint)(NextByte() << 8); + _code |= (uint)NextByte(); + Preload(); + _fence = _code >= 0x8000 ? 0x7FFF : _code; + } + + public ZpDecoder(byte[] data) : this(data, 0, data.Length) + { + } + + /// Decodes a bit with the adaptive context , which it updates. + public int Decode(ref byte context) + { + var z = _a + P[context]; + if (z <= _fence) + { + _a = z; + return context & 1; + } + return DecodeAdaptive(ref context, z); + } + + /// A bit of even probability (BZZ's block sizes, JB2 has none). + public int DecodeRaw() => DecodeFixed(0x8000 + (_a >> 1)); + + /// A bit of IW44's fixed probability (signs and refinement of large coefficients). + public int DecodeIw() => DecodeFixed(0x8000 + ((_a + _a + _a) >> 3)); + + private int DecodeAdaptive(ref byte context, uint z) + { + var bit = context & 1; + // Keeps the interval from reversing (the P of the ZP-coder). + var d = 0x6000 + ((z + _a) >> 2); + if (z > d) + z = d; + if (z > _code) + { + z = 0x10000 - z; + _a += z; + _code += z; + context = Dn[context]; + Renormalize(); + return bit ^ 1; + } + if (_a >= M[context]) + context = Up[context]; + ShiftOne(z); + return bit; + } + + private int DecodeFixed(uint z) + { + if (z > _code) + { + z = 0x10000 - z; + _a += z; + _code += z; + Renormalize(); + return 1; + } + ShiftOne(z); + return 0; + } + + private void ShiftOne(uint z) + { + _scount--; + _a = (z << 1) & 0xFFFF; + _code = ((_code << 1) & 0xFFFF) | ((_buffer >> _scount) & 1); + if (_scount < 16) + Preload(); + _fence = _code >= 0x8000 ? 0x7FFF : _code; + } + + /// After a less probable value: shifts until the interval is back above half. + private void Renormalize() + { + var shift = LeadingOnes(_a); + _scount -= shift; + _a = (_a << shift) & 0xFFFF; + _code = ((_code << shift) & 0xFFFF) | ((_buffer >> _scount) & ((1u << shift) - 1)); + if (_scount < 16) + Preload(); + _fence = _code >= 0x8000 ? 0x7FFF : _code; + } + + /// How many of the top bits of the 16-bit are set. + private static int LeadingOnes(uint x) => System.Numerics.BitOperations.LeadingZeroCount(~(x << 16) | 0xFFFF); + + private void Preload() + { + while (_scount <= 24) + { + _buffer = (_buffer << 8) | (uint)NextByte(); + _scount += 8; + } + } + + private int NextByte() + { + if (_position < _end) + return _data[_position++]; + if (--_delay < 1) + throw new InvalidDataException("A compressed DjVu stream ends early."); + return 0xFF; + } + + internal static readonly ushort[] P = + [ + 0x8000, 0x8000, 0x8000, 0x6bbd, 0x6bbd, 0x5d45, 0x5d45, 0x51b9, 0x51b9, 0x4813, 0x4813, 0x3fd5, + 0x3fd5, 0x38b1, 0x38b1, 0x3275, 0x3275, 0x2cfd, 0x2cfd, 0x2825, 0x2825, 0x23ab, 0x23ab, 0x1f87, + 0x1f87, 0x1bbb, 0x1bbb, 0x1845, 0x1845, 0x1523, 0x1523, 0x1253, 0x1253, 0x0fcf, 0x0fcf, 0x0d95, + 0x0d95, 0x0b9d, 0x0b9d, 0x09e3, 0x09e3, 0x0861, 0x0861, 0x0711, 0x0711, 0x05f1, 0x05f1, 0x04f9, + 0x04f9, 0x0425, 0x0425, 0x0371, 0x0371, 0x02d9, 0x02d9, 0x0259, 0x0259, 0x01ed, 0x01ed, 0x0193, + 0x0193, 0x0149, 0x0149, 0x010b, 0x010b, 0x00d5, 0x00d5, 0x00a5, 0x00a5, 0x007b, 0x007b, 0x0057, + 0x0057, 0x003b, 0x003b, 0x0023, 0x0023, 0x0013, 0x0013, 0x0007, 0x0007, 0x0001, 0x0001, 0x5695, + 0x24ee, 0x8000, 0x0d30, 0x481a, 0x0481, 0x3579, 0x017a, 0x24ef, 0x007b, 0x1978, 0x0028, 0x10ca, + 0x000d, 0x0b5d, 0x0034, 0x078a, 0x00a0, 0x050f, 0x0117, 0x0358, 0x01ea, 0x0234, 0x0144, 0x0173, + 0x0234, 0x00f5, 0x0353, 0x00a1, 0x05c5, 0x011a, 0x03cf, 0x01aa, 0x0285, 0x0286, 0x01ab, 0x03d3, + 0x011a, 0x05c5, 0x00ba, 0x08ad, 0x007a, 0x0ccc, 0x01eb, 0x1302, 0x02e6, 0x1b81, 0x045e, 0x24ef, + 0x0690, 0x2865, 0x09de, 0x3987, 0x0dc8, 0x2c99, 0x10ca, 0x3b5f, 0x0b5d, 0x5695, 0x078a, 0x8000, + 0x050f, 0x24ee, 0x0358, 0x0d30, 0x0234, 0x0481, 0x0173, 0x017a, 0x00f5, 0x007b, 0x00a1, 0x0028, + 0x011a, 0x000d, 0x01aa, 0x0034, 0x0286, 0x00a0, 0x03d3, 0x0117, 0x05c5, 0x01ea, 0x08ad, 0x0144, + 0x0ccc, 0x0234, 0x1302, 0x0353, 0x1b81, 0x05c5, 0x24ef, 0x03cf, 0x2b74, 0x0285, 0x201d, 0x01ab, + 0x1715, 0x011a, 0x0fb7, 0x00ba, 0x0a67, 0x01eb, 0x06e7, 0x02e6, 0x0496, 0x045e, 0x030d, 0x0690, + 0x0206, 0x09de, 0x0155, 0x0dc8, 0x00e1, 0x2b74, 0x0094, 0x201d, 0x0188, 0x1715, 0x0252, 0x0fb7, + 0x0383, 0x0a67, 0x0547, 0x06e7, 0x07e2, 0x0496, 0x0bc0, 0x030d, 0x1178, 0x0206, 0x19da, 0x0155, + 0x24ef, 0x00e1, 0x320e, 0x0094, 0x432a, 0x0188, 0x447d, 0x0252, 0x5ece, 0x0383, 0x8000, 0x0547, + 0x481a, 0x07e2, 0x3579, 0x0bc0, 0x24ef, 0x1178, 0x1978, 0x19da, 0x2865, 0x24ef, 0x3987, 0x320e, + 0x2c99, 0x432a, 0x3b5f, 0x447d, 0x5695, 0x5ece, 0x8000, 0x8000, 0x5695, 0x481a, 0x481a, + 0, 0, 0, 0, 0, + ]; + + internal static readonly ushort[] M = Pad( + [ + 0x0000, 0x0000, 0x0000, 0x10a5, 0x10a5, 0x1f28, 0x1f28, 0x2bd3, 0x2bd3, 0x36e3, 0x36e3, 0x408c, + 0x408c, 0x48fd, 0x48fd, 0x505d, 0x505d, 0x56d0, 0x56d0, 0x5c71, 0x5c71, 0x615b, 0x615b, 0x65a5, + 0x65a5, 0x6962, 0x6962, 0x6ca2, 0x6ca2, 0x6f74, 0x6f74, 0x71e6, 0x71e6, 0x7404, 0x7404, 0x75d6, + 0x75d6, 0x7768, 0x7768, 0x78c2, 0x78c2, 0x79ea, 0x79ea, 0x7ae7, 0x7ae7, 0x7bbe, 0x7bbe, 0x7c75, + 0x7c75, 0x7d0f, 0x7d0f, 0x7d91, 0x7d91, 0x7dfe, 0x7dfe, 0x7e5a, 0x7e5a, 0x7ea6, 0x7ea6, 0x7ee6, + 0x7ee6, 0x7f1a, 0x7f1a, 0x7f45, 0x7f45, 0x7f6b, 0x7f6b, 0x7f8d, 0x7f8d, 0x7faa, 0x7faa, 0x7fc3, + 0x7fc3, 0x7fd7, 0x7fd7, 0x7fe7, 0x7fe7, 0x7ff2, 0x7ff2, 0x7ffa, 0x7ffa, 0x7fff, 0x7fff, + ]); + + internal static readonly byte[] Up = + [ + 84, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 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, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, + 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, + 82, 81, 82, 9, 86, 5, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 82, 99, 76, 101, + 70, 103, 66, 105, 106, 107, 66, 109, 60, 111, 56, 69, 114, 65, 116, 61, 118, 57, 120, 53, + 122, 49, 124, 43, 72, 39, 60, 33, 56, 29, 52, 23, 48, 23, 42, 137, 38, 21, 140, 15, + 142, 9, 144, 141, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 70, 157, 66, 81, 62, 75, + 58, 69, 54, 65, 50, 167, 44, 65, 40, 59, 34, 55, 30, 175, 24, 177, 178, 179, 180, 181, + 182, 183, 184, 69, 186, 59, 188, 55, 190, 51, 192, 47, 194, 41, 196, 37, 198, 199, 72, 201, + 62, 203, 58, 205, 54, 207, 50, 209, 46, 211, 40, 213, 36, 215, 30, 217, 26, 219, 20, 71, + 14, 61, 14, 57, 8, 53, 228, 49, 230, 45, 232, 39, 234, 35, 138, 29, 24, 25, 240, 19, + 22, 13, 16, 13, 10, 7, 244, 249, 10, 89, 230, + 0, 0, 0, 0, 0, + ]; + + internal static readonly byte[] Dn = + [ + 145, 4, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 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, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, + 78, 79, 80, 85, 226, 6, 176, 143, 138, 141, 112, 135, 104, 133, 100, 129, 98, 127, 72, 125, + 102, 123, 60, 121, 110, 119, 108, 117, 54, 115, 48, 113, 134, 59, 132, 55, 130, 51, 128, 47, + 126, 41, 62, 37, 66, 31, 54, 25, 50, 131, 46, 17, 40, 15, 136, 7, 32, 139, 172, 9, + 170, 85, 168, 248, 166, 247, 164, 197, 162, 95, 160, 173, 158, 165, 156, 161, 60, 159, 56, 71, + 52, 163, 48, 59, 42, 171, 38, 169, 32, 53, 26, 47, 174, 193, 18, 191, 222, 189, 218, 187, + 216, 185, 214, 61, 212, 53, 210, 49, 208, 45, 206, 39, 204, 195, 202, 31, 200, 243, 64, 239, + 56, 237, 52, 235, 48, 233, 44, 231, 38, 229, 34, 227, 28, 225, 22, 223, 16, 221, 220, 63, + 8, 55, 224, 51, 2, 47, 87, 43, 246, 37, 244, 33, 238, 27, 236, 21, 16, 15, 8, 241, + 242, 7, 10, 245, 2, 1, 83, 250, 2, 143, 246, + 0, 0, 0, 0, 0, + ]; + + private static ushort[] Pad(ushort[] values) + { + var table = new ushort[256]; + values.CopyTo(table, 0); + return table; + } +} diff --git a/src/Filee.Engines/EngineRegistry.cs b/src/Filee.Engines/EngineRegistry.cs index d10d7640f..7407fb139 100644 --- a/src/Filee.Engines/EngineRegistry.cs +++ b/src/Filee.Engines/EngineRegistry.cs @@ -82,6 +82,7 @@ public static IReadOnlyList CreateAll(EngineEnvironment env, ILogger new FfmpegConverter(loggers?.CreateLogger()), new CadConverter(), new DrawingConverter(), + new Djvu.DjvuConverter(), new Gcode.GcodeConverter(loggers?.CreateLogger()), new ModelConverter(), new ImageTo3DConverter(), diff --git a/src/Filee.Engines/Ocr/OcrTextLayer.cs b/src/Filee.Engines/Ocr/OcrTextLayer.cs index 3f2bc2e74..a4df92918 100644 --- a/src/Filee.Engines/Ocr/OcrTextLayer.cs +++ b/src/Filee.Engines/Ocr/OcrTextLayer.cs @@ -94,6 +94,15 @@ public void AddPage(string picture, double width, double height, IReadOnlyListThe document the pages go into (for a writer that draws its pages itself, DjvuPdfWriter). + internal PdfDocument Document => _document; + + /// Adds as invisible text over a page of × points. + internal void AddText(PdfPage page, IReadOnlyList lines, double width, double height) + { var text = TextLayer(lines, width, height); if (text.Length == 0) return; diff --git a/tests/Filee.Core.Tests/FormatFieldsTests.cs b/tests/Filee.Core.Tests/FormatFieldsTests.cs index 26baf228b..b54d49bd7 100644 --- a/tests/Filee.Core.Tests/FormatFieldsTests.cs +++ b/tests/Filee.Core.Tests/FormatFieldsTests.cs @@ -145,7 +145,7 @@ public void Grade_counts_match_the_published_table() new Dictionary { [FormatGrade.Everyday] = 19, - [FormatGrade.General] = 105, + [FormatGrade.General] = 106, [FormatGrade.Specialist] = 133, [FormatGrade.Legacy] = 52, }, diff --git a/tests/Filee.Engines.Tests/DjvuBuilder.cs b/tests/Filee.Engines.Tests/DjvuBuilder.cs new file mode 100644 index 000000000..14c99a8ed --- /dev/null +++ b/tests/Filee.Engines.Tests/DjvuBuilder.cs @@ -0,0 +1,1245 @@ +// Writes DjVu files in code for the DjVu reader's tests (DjvuTests, DjvuFuzzTests): small encoders for every piece the +// 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. + +using System.Text; +using Filee.Engines.Djvu; + +namespace Filee.Engines.Tests; + +/// The ZP-coder, encoding. +internal sealed class ZpEncoder +{ + private readonly MemoryStream _output = new(); + private uint _a; + private uint _subend; + private uint _buffer = 0xFFFFFF; + private int _nrun; + private int _delay = 25; + private int _byte; + private int _scount; + + public void Encode(int bit, ref byte context) + { + var z = _a + ZpDecoder.P[context]; + if (bit != (context & 1)) + EncodeLps(ref context, z); + else if (z >= 0x8000) + EncodeMps(ref context, z); + else + _a = z; + } + + public void EncodeRaw(int bit) => EncodeFixed(bit, 0x8000 + (_a >> 1)); + + public void EncodeIw(int bit) => EncodeFixed(bit, 0x8000 + ((_a + _a + _a) >> 3)); + + private void EncodeFixed(int bit, uint z) + { + if (bit != 0) + { + z = 0x10000 - z; + _subend += z; + _a += z; + while (_a >= 0x8000) + Shift(); + } + else + { + _a = z; + if (_a >= 0x8000) + Shift(); + } + } + + private static uint Clamp(uint z, uint a) + { + var d = 0x6000 + ((z + a) >> 2); + return z > d ? d : z; + } + + private void EncodeMps(ref byte context, uint z) + { + z = Clamp(z, _a); + if (_a >= ZpDecoder.M[context]) + context = ZpDecoder.Up[context]; + _a = z; + if (_a >= 0x8000) + Shift(); + } + + private void EncodeLps(ref byte context, uint z) + { + z = Clamp(z, _a); + context = ZpDecoder.Dn[context]; + z = 0x10000 - z; + _subend += z; + _a += z; + while (_a >= 0x8000) + Shift(); + } + + private void Shift() + { + Emit((int)(1 - (_subend >> 15))); + _subend = (_subend << 1) & 0xFFFF; + _a = (_a << 1) & 0xFFFF; + } + + private void Emit(int b) + { + _buffer = (_buffer << 1) + (uint)b; + b = (int)(_buffer >> 24); + _buffer &= 0xFFFFFF; + switch (b) + { + case 1: + OutBit(1); + while (_nrun-- > 0) + OutBit(0); + _nrun = 0; + break; + case 0xFF: + OutBit(0); + while (_nrun-- > 0) + OutBit(1); + _nrun = 0; + break; + case 0: + _nrun++; + break; + default: + throw new InvalidOperationException("ZP encoder state"); + } + } + + private void OutBit(int bit) + { + if (_delay > 0) + { + if (_delay < 0xFF) + _delay--; + return; + } + _byte = (_byte << 1) | bit; + if (++_scount == 8) + { + _output.WriteByte((byte)_byte); + _scount = 0; + _byte = 0; + } + } + + public byte[] Finish() + { + if (_subend > 0x8000) + _subend = 0x10000; + else if (_subend > 0) + _subend = 0x8000; + while (_buffer != 0xFFFFFF || _subend != 0) + { + Emit((int)(1 - (_subend >> 15))); + _subend = (_subend << 1) & 0xFFFF; + } + OutBit(1); + while (_nrun-- > 0) + OutBit(0); + _nrun = 0; + while (_scount > 0) + OutBit(1); + _delay = 0xFF; + return _output.ToArray(); + } +} + +/// BZZ, encoding. +internal static class BzzEncoder +{ + public static byte[] Encode(byte[] data, int blockSize = 1 << 20) + { + var zp = new ZpEncoder(); + var contexts = new byte[300]; + for (var at = 0; at < data.Length; at += blockSize) + EncodeBlock(zp, contexts, data.AsSpan(at, Math.Min(blockSize, data.Length - at)).ToArray()); + EncodeRaw(zp, 24, 0); + return zp.Finish(); + } + + private static void EncodeRaw(ZpEncoder zp, int bits, int value) + { + for (var bit = bits - 1; bit >= 0; bit--) + zp.EncodeRaw((value >> bit) & 1); + } + + private static void EncodeBlock(ZpEncoder zp, byte[] contexts, byte[] block) + { + var n = block.Length; + var size = n + 1; + // The suffixes of block + marker, sorted (the marker sorts before every byte). + var order = Enumerable.Range(0, size).ToArray(); + Array.Sort(order, (x, y) => + { + while (x < n && y < n) + { + if (block[x] != block[y]) + return block[x].CompareTo(block[y]); + x++; + y++; + } + return (x == n ? 0 : 1).CompareTo(y == n ? 0 : 1); + }); + var last = new byte[size]; + var marker = -1; + for (var r = 0; r < size; r++) + { + if (order[r] == 0) + marker = r; + else + last[r] = block[order[r] - 1]; + } + + EncodeRaw(zp, 24, size); + zp.EncodeRaw(0); // estimation speed 0 + var mtf = new byte[256]; + var rmtf = new int[256]; + for (var i = 0; i < 256; i++) + { + mtf[i] = (byte)i; + rmtf[i] = i; + } + var freq = new uint[4]; + uint fadd = 4; + var mtfno = 3; + for (var i = 0; i < size; i++) + { + var c = last[i]; + var ctxid = Math.Min(2, mtfno); + mtfno = i == marker ? 256 : rmtf[c]; + zp.Encode(mtfno == 0 ? 1 : 0, ref contexts[ctxid]); + if (mtfno != 0) + { + zp.Encode(mtfno == 1 ? 1 : 0, ref contexts[3 + ctxid]); + if (mtfno != 1) + { + var at = 6; + var found = false; + for (var bits = 1; bits <= 7; bits++) + { + var inside = mtfno < (2 << bits) ? 1 : 0; + zp.Encode(inside, ref contexts[at]); + if (inside == 1) + { + var value = mtfno - (1 << bits); + var node = 1; + for (var bit = bits - 1; bit >= 0; bit--) + { + var b = (value >> bit) & 1; + zp.Encode(b, ref contexts[at + node]); + node = (node << 1) | b; + } + found = true; + break; + } + at += 1 << bits; + } + if (!found) + continue; // the marker: no rotation + } + } + fadd += fadd >> 0; + if (fadd > 0x10000000) + { + fadd >>= 24; + for (var k = 0; k < 4; k++) + freq[k] >>= 24; + } + var fc = fadd; + if (mtfno < 4) + fc += freq[mtfno]; + int k2; + for (k2 = mtfno; k2 >= 4; k2--) + { + mtf[k2] = mtf[k2 - 1]; + rmtf[mtf[k2]] = k2; + } + for (; k2 > 0 && fc >= freq[k2 - 1]; k2--) + { + mtf[k2] = mtf[k2 - 1]; + freq[k2] = freq[k2 - 1]; + rmtf[mtf[k2]] = k2; + } + mtf[k2] = c; + freq[k2] = fc; + rmtf[c] = k2; + } + } +} + +/// A JB2 stream, written record by record. +internal sealed class Jb2Writer +{ + private readonly ZpEncoder _zp = new(); + private byte[] _bits = new byte[1 << 20]; + private int[] _left = new int[1 << 20]; + private int[] _right = new int[1 << 20]; + private int _cells = 1; + private int _recordType, _matchIndex, _commentLength, _commentByte; + private int _absLocX, _absLocY, _absSizeX, _absSizeY, _imageSize, _inheritedCount; + private int _relLocXCurrent, _relLocXLast, _relLocYCurrent, _relLocYLast, _relSizeX, _relSizeY; + private byte _refinementFlag, _offsetType; + private readonly byte[] _direct = new byte[1024]; + private readonly byte[] _cross = new byte[2048]; + private int _lastLeft, _lastRight, _lastBottom, _lastRowLeft, _lastRowBottom; + private readonly int[] _shortList = new int[3]; + private int _shortListPosition; + private int _columns, _rows; + + /// The library: shapes a later record may copy or refine, by library index. + public List Library { get; } = []; + + /// Makes a shape from rows of '#' (black) and '.', top row first. + public static Jb2Shape Shape(params string[] rows) + { + var shape = new Jb2Shape(rows.Length == 0 ? 0 : rows[0].Length, rows.Length); + for (var r = 0; r < rows.Length; r++) + { + for (var x = 0; x < rows[r].Length; x++) + shape.Bits[(rows.Length - 1 - r) * shape.Width + x] = rows[r][x] == '#' ? (byte)1 : (byte)0; + } + shape.ComputeBox(); + return shape; + } + + public Jb2Writer RequireDictionary(int shapes, IReadOnlyList dictionary) + { + Number(9, 0, 11, ref _recordType); + Number(shapes, 0, 262142, ref _inheritedCount); + Library.AddRange(dictionary); + return this; + } + + public Jb2Writer Start(int columns, int rows) + { + Number(0, 0, 11, ref _recordType); + Number(columns, 0, 262142, ref _imageSize); + Number(rows, 0, 262142, ref _imageSize); + _zp.Encode(0, ref _refinementFlag); + _columns = columns; + _rows = rows; + _lastLeft = columns == 0 ? 1 : 1 + columns; + _lastRowBottom = rows; + _lastRowLeft = 0; + _lastRight = 0; + FillShortList(_lastRowBottom); + return this; + } + + /// A new shape coded directly: placed and in the library (type 1), library only (2) or placed only (3). + public Jb2Writer New(Jb2Shape shape, int left = 0, int bottom = 0, int type = 1) + { + Number(type, 0, 11, ref _recordType); + Number(shape.Width, 0, 262142, ref _absSizeX); + Number(shape.Height, 0, 262142, ref _absSizeY); + Direct(shape); + if (type != 2) + Location(left, bottom, shape.Height, shape.Width); + if (type != 3) + Library.Add(shape); + return this; + } + + /// A refinement of library shape (types 4, 5, 6 as for ). + public Jb2Writer Refine(int match, Jb2Shape shape, int left = 0, int bottom = 0, int type = 4) + { + Number(type, 0, 11, ref _recordType); + Number(match, 0, Library.Count - 1, ref _matchIndex); + var reference = Library[match]; + var box = reference.Box; + Number(shape.Width - (box.Right - box.Left + 1), -262143, 262142, ref _relSizeX); + Number(shape.Height - (box.Top - box.Bottom + 1), -262143, 262142, ref _relSizeY); + Cross(shape, reference); + if (type != 5) + Location(left, bottom, shape.Height, shape.Width); + if (type != 6) + Library.Add(shape); + return this; + } + + /// A copy of library shape , its bitmap's bottom-left at (left, bottom). + public Jb2Writer Copy(int match, int left, int bottom) + { + Number(7, 0, 11, ref _recordType); + Number(match, 0, Library.Count - 1, ref _matchIndex); + var box = Library[match].Box; + Location(left + box.Left, bottom + box.Bottom, box.Top - box.Bottom + 1, box.Right - box.Left + 1); + return this; + } + + /// Non-mark data at an absolute place (its top-left pixel at column left, row top from the bottom, 0-based). + public Jb2Writer NonMark(Jb2Shape shape, int left, int top) + { + Number(8, 0, 11, ref _recordType); + Number(shape.Width, 0, 262142, ref _absSizeX); + Number(shape.Height, 0, 262142, ref _absSizeY); + Direct(shape); + Number(left + 1, 1, _columns, ref _absLocX); + Number(top + 1, 1, _rows, ref _absLocY); + return this; + } + + public Jb2Writer Comment(string text) + { + Number(10, 0, 11, ref _recordType); + var bytes = Encoding.UTF8.GetBytes(text); + Number(bytes.Length, 0, 262142, ref _commentLength); + foreach (var b in bytes) + Number(b, 0, 255, ref _commentByte); + return this; + } + + /// Resets the number coder (record 9 after the start). + public Jb2Writer Reset() + { + Number(9, 0, 11, ref _recordType); + _recordType = _matchIndex = _commentLength = _commentByte = 0; + _absLocX = _absLocY = _absSizeX = _absSizeY = _imageSize = _inheritedCount = 0; + _relLocXCurrent = _relLocXLast = _relLocYCurrent = _relLocYLast = _relSizeX = _relSizeY = 0; + Array.Clear(_bits); + Array.Clear(_left); + Array.Clear(_right); + _cells = 1; + return this; + } + + /// Codes a raw record type (for hostile streams). + public Jb2Writer Record(int type) + { + Number(type, 0, 11, ref _recordType); + return this; + } + + /// Codes a raw number with the absolute size context (for hostile streams). + public Jb2Writer Size(int width, int height) + { + Number(width, 0, 262142, ref _absSizeX); + Number(height, 0, 262142, ref _absSizeY); + return this; + } + + public byte[] End() + { + Number(11, 0, 11, ref _recordType); + return _zp.Finish(); + } + + private void Direct(Jb2Shape shape) + { + for (var y = shape.Height - 1; y >= 0; y--) + { + for (var x = 0; x < shape.Width; x++) + { + var context = + (shape[x - 1, y + 2] << 9) | (shape[x, y + 2] << 8) | (shape[x + 1, y + 2] << 7) | + (shape[x - 2, y + 1] << 6) | (shape[x - 1, y + 1] << 5) | (shape[x, y + 1] << 4) | + (shape[x + 1, y + 1] << 3) | (shape[x + 2, y + 1] << 2) | + (shape[x - 2, y] << 1) | shape[x - 1, y]; + _zp.Encode(shape[x, y], ref _direct[context]); + } + } + } + + private void Cross(Jb2Shape shape, Jb2Shape reference) + { + var box = reference.Box; + int w = shape.Width, h = shape.Height; + var xd2c = (w / 2 - w + 1) - ((box.Right - box.Left + 1) / 2 - box.Right); + var yd2c = (h / 2 - h + 1) - ((box.Top - box.Bottom + 1) / 2 - box.Top); + for (var y = h - 1; y >= 0; y--) + { + var cy = y + yd2c; + for (var x = 0; x < w; x++) + { + var cx = x + xd2c; + var context = + (shape[x - 1, y + 1] << 10) | (shape[x, y + 1] << 9) | (shape[x + 1, y + 1] << 8) | + (shape[x - 1, y] << 7) | + (reference[cx, cy + 1] << 6) | + (reference[cx - 1, cy] << 5) | (reference[cx, cy] << 4) | (reference[cx + 1, cy] << 3) | + (reference[cx - 1, cy - 1] << 2) | (reference[cx, cy - 1] << 1) | reference[cx + 1, cy - 1]; + _zp.Encode(shape[x, y], ref _cross[context]); + } + } + } + + private void Location(int left0, int bottom0, int rows, int columns) + { + var left = left0 + 1; + var bottom = bottom0 + 1; + var right = left + columns - 1; + var top = bottom + rows - 1; + var newRow = left < _lastLeft; + _zp.Encode(newRow ? 1 : 0, ref _offsetType); + if (newRow) + { + Number(left - _lastRowLeft, -262143, 262142, ref _relLocXLast); + Number(top - _lastRowBottom, -262143, 262142, ref _relLocYLast); + _lastLeft = _lastRowLeft = left; + _lastRight = right; + _lastBottom = _lastRowBottom = bottom; + FillShortList(bottom); + } + else + { + Number(left - _lastRight, -262143, 262142, ref _relLocXCurrent); + Number(bottom - _lastBottom, -262143, 262142, ref _relLocYCurrent); + _lastLeft = left; + _lastRight = right; + _lastBottom = UpdateShortList(bottom); + } + } + + private void FillShortList(int value) + { + _shortList[0] = _shortList[1] = _shortList[2] = value; + _shortListPosition = 0; + } + + private int UpdateShortList(int value) + { + if (++_shortListPosition == 3) + _shortListPosition = 0; + var s = _shortList; + s[_shortListPosition] = value; + return s[0] >= s[1] + ? (s[0] > s[2] ? (s[1] >= s[2] ? s[1] : s[2]) : s[0]) + : (s[0] < s[2] ? (s[1] >= s[2] ? s[2] : s[1]) : s[0]); + } + + private void Number(int v, int low, int high, ref int root) + { + var cutoff = 0; + var phase = 1; + var range = -1; + if (root == 0) + root = _cells++; + var cell = root; + while (true) + { + bool decision; + if (low < cutoff && high >= cutoff) + { + decision = v >= cutoff; + _zp.Encode(decision ? 1 : 0, ref _bits[cell]); + } + else + { + decision = v >= cutoff; + } + switch (phase) + { + case 1: + if (!decision) + { + v = -v - 1; + var temp = -low - 1; + low = -high - 1; + high = temp; + } + phase = 2; + cutoff = 1; + break; + case 2: + if (!decision) + { + phase = 3; + range = (cutoff + 1) / 2; + if (range == 1) + cutoff = 0; + else + cutoff -= range / 2; + } + else + { + cutoff += cutoff + 1; + } + break; + default: + range /= 2; + if (range != 1) + cutoff += decision ? range / 2 : -(range / 2); + else if (!decision) + cutoff--; + break; + } + if (range == 1) + break; + var next = decision ? _right[cell] : _left[cell]; + if (next == 0) + { + next = _cells++; + if (decision) + _right[cell] = next; + else + _left[cell] = next; + } + cell = next; + } + } +} + +/// IW44, encoding: planes of −128..127 (top row first) to chunks. +internal static class Iw44Encoder +{ + /// + /// Encodes a grey picture ( and null; the decoder shows 127 − y) or a + /// colour one, in chunks of slices each. + /// + public static List Encode(int width, int height, sbyte[] y, sbyte[]? cb = null, sbyte[]? cr = null, + int slices = 60, int chunks = 1, bool halfChroma = false, int chromaDelay = 0) + { + var color = cb is not null && cr is not null; + var yc = new Coder(width, height, y); + var cbc = color ? new Coder(width, height, cb!) : null; + var crc = color ? new Coder(width, height, cr!) : null; + var result = new List(); + var done = 0; + for (var chunk = 0; chunk < chunks; chunk++) + { + var zp = new ZpEncoder(); + var header = new List { (byte)chunk, (byte)slices }; + if (chunk == 0) + { + header.Add((byte)(color ? 1 : 0x81)); + header.Add(2); + header.Add((byte)(width >> 8)); + header.Add((byte)width); + header.Add((byte)(height >> 8)); + header.Add((byte)height); + header.Add((byte)((halfChroma ? 0 : 0x80) | chromaDelay)); + } + var more = true; + var total = done + slices; + while (more && done < total) + { + more = yc.Slice(zp); + if (cbc is not null && crc is not null && chromaDelay <= done) + { + more |= cbc.Slice(zp); + more |= crc.Slice(zp); + } + done++; + } + result.Add([.. header, .. zp.Finish()]); + } + return result; + } + + /// The slice coder of one component: the true coefficients and what the decoder holds so far. + private sealed class Coder + { + private const int Zero = 1, Active = 2, New = 4, Unknown = 8; + private readonly short[][] _target; + private readonly short[]?[] _current; + private readonly ulong[] _present; + private readonly int[] _quantLow = new int[16]; + private readonly int[] _quantHigh = new int[10]; + private readonly byte[] _coefficientState = new byte[256]; + private readonly byte[] _bucketState = new byte[16]; + private readonly byte[] _contextStart = new byte[32]; + private readonly byte[,] _contextBucket = new byte[10, 8]; + private byte _contextMantissa; + private byte _contextRoot; + private int _band; + private int _bit = 1; + + public Coder(int width, int height, sbyte[] plane) + { + var bw = (width + 31) & ~31; + var bh = (height + 31) & ~31; + var data = new short[bw * bh]; + for (var i = 0; i < height; i++) + { + // The decoder's row 0 is the bottom. + var source = (height - 1 - i) * width; + for (var j = 0; j < width; j++) + data[i * bw + j] = (short)(plane[source + j] << 6); + } + Forward(data, width, height, bw); + var blocks = bw / 32 * (bh / 32); + _target = new short[blocks][]; + _current = new short[blocks][]; + _present = new ulong[blocks]; + var index = 0; + for (var i = 0; i < bh; i += 32) + { + for (var j = 0; j < bw; j += 32) + { + var block = new short[1024]; + for (var n = 0; n < 1024; n++) + { + var at = Iw44Map.ZigZag[n]; + block[n] = data[(i + (at >> 5)) * bw + j + (at & 31)]; + } + _target[index++] = block; + } + } + var q = Iw44Codec.Quantization; + var k = 0; + var p = 0; + for (; k < 4; k++) + _quantLow[k] = q[p++]; + for (var group = 0; group < 3; group++, p++) + { + for (var j = 0; j < 4; j++) + _quantLow[k++] = q[p]; + } + for (var j = 1; j < 10; j++) + _quantHigh[j] = q[p++]; + } + + public bool Slice(ZpEncoder zp) + { + if (_bit < 0) + return false; + if (!IsNull()) + { + var (start, size) = Iw44Codec.Bands[_band]; + for (var b = 0; b < _target.Length; b++) + Buckets(zp, b, start, size); + } + _quantHigh[_band] >>= 1; + if (_band == 0) + { + for (var i = 0; i < 16; i++) + _quantLow[i] >>= 1; + } + if (++_band >= 10) + { + _band = 0; + _bit++; + if (_quantHigh[9] == 0) + { + _bit = -1; + return false; + } + } + return true; + } + + private bool IsNull() + { + if (_band == 0) + { + var isNull = true; + for (var i = 0; i < 16; i++) + { + _coefficientState[i] = Zero; + if (_quantLow[i] is > 0 and < 0x8000) + { + _coefficientState[i] = Unknown; + isNull = false; + } + } + return isNull; + } + return !(_quantHigh[_band] is > 0 and < 0x8000); + } + + private bool Has(int block, int bucket) => (_present[block] & (1UL << bucket)) != 0; + + private void Buckets(ZpEncoder zp, int b, int first, int count) + { + var target = _target[b]; + var current = _current[b]; + // Prepare, as the decoder does. + var all = 0; + if (first != 0) + { + for (var bucket = 0; bucket < count; bucket++) + { + var state = 0; + if (!Has(b, first + bucket)) + { + state = Unknown; + for (var i = 0; i < 16; i++) + _coefficientState[(bucket << 4) + i] = Unknown; // what it becomes when allocated + } + else + { + for (var i = 0; i < 16; i++) + { + var c = current![((first + bucket) << 4) + i] != 0 ? Active : Unknown; + _coefficientState[(bucket << 4) + i] = (byte)c; + state |= c; + } + } + _bucketState[bucket] = (byte)state; + all |= state; + } + } + else + { + if (!Has(b, 0)) + { + all = Unknown; + // What the states become when the bucket is allocated (the decoder keeps the last block's until then). + for (var i = 0; i < 16; i++) + { + if (_coefficientState[i] != Zero) + _coefficientState[i] = Unknown; + } + } + else + { + for (var i = 0; i < 16; i++) + { + int c = _coefficientState[i]; + if (c != Zero) + c = current![i] != 0 ? Active : Unknown; + _coefficientState[i] = (byte)c; + all |= c; + } + } + _bucketState[0] = (byte)all; + } + + // Which buckets get a new significant coefficient at this threshold. + var becomes = new bool[count]; + var any = false; + for (var bucket = 0; bucket < count; bucket++) + { + if ((_bucketState[bucket] & Unknown) == 0) + continue; + for (var i = 0; i < 16; i++) + { + if ((_coefficientState[(bucket << 4) + i] & Unknown) == 0) + continue; + var threshold = _band == 0 ? _quantLow[i] : _quantHigh[_band]; + if (Math.Abs((int)target[((first + bucket) << 4) + i]) >= threshold) + becomes[bucket] = true; + } + any |= becomes[bucket]; + } + + if (count < 16 || (all & Active) != 0) + { + all |= New; + } + else if ((all & Unknown) != 0) + { + zp.Encode(any ? 1 : 0, ref _contextRoot); + if (any) + all |= New; + } + + if ((all & New) != 0) + { + for (var bucket = 0; bucket < count; bucket++) + { + if ((_bucketState[bucket] & Unknown) == 0) + continue; + var ctx = 0; + if (_band > 0) + { + var k = (first + bucket) << 2; + if (Has(b, k >> 4)) + { + if (current![k] != 0) + ctx++; + if (current[k + 1] != 0) + ctx++; + if (current[k + 2] != 0) + ctx++; + if (ctx < 3 && current[k + 3] != 0) + ctx++; + } + } + if ((all & Active) != 0) + ctx |= 4; + zp.Encode(becomes[bucket] ? 1 : 0, ref _contextBucket[_band, ctx]); + if (becomes[bucket]) + _bucketState[bucket] |= New; + } + + for (var bucket = 0; bucket < count; bucket++) + { + if ((_bucketState[bucket] & New) == 0) + continue; + var state = bucket << 4; + if (!Has(b, first + bucket)) + { + _current[b] ??= new short[1024]; + current = _current[b]; + _present[b] |= 1UL << (first + bucket); + for (var i = 0; i < 16; i++) + { + if (first != 0 || _coefficientState[state + i] != Zero) + _coefficientState[state + i] = Unknown; + } + } + var at = (first + bucket) << 4; + var gotcha = 0; + for (var i = 0; i < 16; i++) + { + if ((_coefficientState[state + i] & Unknown) != 0) + gotcha++; + } + for (var i = 0; i < 16; i++) + { + if ((_coefficientState[state + i] & Unknown) == 0) + continue; + var threshold = _band == 0 ? _quantLow[i] : _quantHigh[_band]; + var ctx = Math.Min(gotcha, 7); + if ((_bucketState[bucket] & Active) != 0) + ctx |= 8; + var value = target[at + i]; + var significant = Math.Abs((int)value) >= threshold; + zp.Encode(significant ? 1 : 0, ref _contextStart[ctx]); + if (significant) + { + _coefficientState[state + i] |= New; + var half = threshold >> 1; + var magnitude = threshold + half - (half >> 2); + zp.EncodeIw(value < 0 ? 1 : 0); + current![at + i] = (short)(value < 0 ? -magnitude : magnitude); + } + if ((_coefficientState[state + i] & New) != 0) + gotcha = 0; + else if (gotcha > 0) + gotcha--; + } + } + } + + if ((all & Active) != 0) + { + for (var bucket = 0; bucket < count; bucket++) + { + if ((_bucketState[bucket] & Active) == 0) + continue; + var state = bucket << 4; + var at = (first + bucket) << 4; + for (var i = 0; i < 16; i++) + { + if ((_coefficientState[state + i] & Active) == 0) + continue; + int value = current![at + i]; + var magnitude = Math.Abs(value); + var threshold = _band == 0 ? _quantLow[i] : _quantHigh[_band]; + var wanted = Math.Abs((int)target[at + i]); + if (magnitude <= 3 * threshold) + { + magnitude += threshold >> 2; + var bit = wanted >= magnitude ? 1 : 0; + zp.Encode(bit, ref _contextMantissa); + magnitude += bit == 1 ? threshold >> 1 : -threshold + (threshold >> 1); + } + else + { + var bit = wanted >= magnitude ? 1 : 0; + zp.EncodeIw(bit); + magnitude += bit == 1 ? threshold >> 1 : -threshold + (threshold >> 1); + } + current[at + i] = (short)(value > 0 ? magnitude : -magnitude); + } + } + } + } + } + + /// The forward lifting transform over scales 1 to 16. + private static void Forward(short[] p, int w, int h, int rowsize) + { + for (var scale = 1; scale < 32; scale <<= 1) + { + Horizontal(p, w, h, rowsize, scale); + Vertical(p, w, h, rowsize, scale); + } + } + + private static void Vertical(short[] d, int w, int h, int rowsize, int scale) + { + var s = scale * rowsize; + var s3 = s + s + s; + h = h > 0 ? ((h - 1) / scale) + 1 : 0; + var y = 1; + var p = s; + while (y - 3 < h) + { + var q = p; + var e = q + w; + if (y >= 3 && y + 3 < h) + { + for (; q < e; q += scale) + { + var a = d[q - s] + d[q + s]; + var b = d[q - s3] + d[q + s3]; + d[q] = (short)(d[q] - (((a << 3) + a - b + 8) >> 4)); + } + } + else if (y < h) + { + var q1 = y + 1 < h ? q + s : q - s; + for (; q < e; q += scale, q1 += scale) + { + var a = d[q - s] + d[q1]; + d[q] = (short)(d[q] - ((a + 1) >> 1)); + } + } + q = p - s3; + e = q + w; + if (y >= 6 && y < h) + { + for (; q < e; q += scale) + { + var a = d[q - s] + d[q + s]; + var b = d[q - s3] + d[q + s3]; + d[q] = (short)(d[q] + (((a << 3) + a - b + 16) >> 5)); + } + } + else if (y >= 3) + { + var q1 = y - 2 < h ? q + s : -1; + var q3 = y < h ? q + s3 : -1; + for (; q < e; q += scale) + { + var below1 = q1 >= 0 ? d[q1] : 0; + var below3 = q3 >= 0 ? d[q3] : 0; + int a, b; + if (y >= 6) + { + a = d[q - s] + below1; + b = d[q - s3] + below3; + } + else if (y >= 4) + { + a = d[q - s] + below1; + b = below3; + } + else + { + a = below1; + b = below3; + } + d[q] = (short)(d[q] + (((a << 3) + a - b + 16) >> 5)); + if (q1 >= 0) + q1 += scale; + if (q3 >= 0) + q3 += scale; + } + } + y += 2; + p += s + s; + } + } + + private static void Horizontal(short[] d, int w, int h, int rowsize, int scale) + { + var s = scale; + var s3 = s + s + s; + rowsize *= scale; + var p = 0; + for (var y = 0; y < h; y += scale, p += rowsize) + { + var q = p + s; + var e = p + w; + int a0 = 0, a1 = 0, a2 = 0, a3 = 0; + int b0 = 0, b1 = 0, b2 = 0, b3 = 0; + if (q < e) + { + a1 = a2 = a3 = d[q - s]; + if (q + s < e) + a2 = d[q + s]; + if (q + s3 < e) + a3 = d[q + s3]; + b3 = d[q] - ((a1 + a2 + 1) >> 1); + d[q] = (short)b3; + q += s + s; + } + while (q + s3 < e) + { + a0 = a1; + a1 = a2; + a2 = a3; + a3 = d[q + s3]; + b0 = b1; + b1 = b2; + b2 = b3; + b3 = d[q] - ((((a1 + a2) << 3) + (a1 + a2) - a0 - a3 + 8) >> 4); + d[q] = (short)b3; + d[q - s3] = (short)(d[q - s3] + ((((b1 + b2) << 3) + (b1 + b2) - b0 - b3 + 16) >> 5)); + q += s + s; + } + while (q < e) + { + a1 = a2; + a2 = a3; + b0 = b1; + b1 = b2; + b2 = b3; + b3 = d[q] - ((a1 + a2 + 1) >> 1); + d[q] = (short)b3; + d[q - s3] = (short)(d[q - s3] + ((((b1 + b2) << 3) + (b1 + b2) - b0 - b3 + 16) >> 5)); + q += s + s; + } + while (q - s3 < e) + { + b0 = b1; + b1 = b2; + b2 = b3; + b3 = 0; + if (q - s3 >= p) + d[q - s3] = (short)(d[q - s3] + ((((b1 + b2) << 3) + (b1 + b2) - b0 - b3 + 16) >> 5)); + q += s + s; + } + } + } +} + +/// 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); + +/// A DjVu file's chunks and forms. +internal static class DjvuBuilder +{ + public static byte[] Chunk(string id, byte[] data) + { + var result = new List(Encoding.ASCII.GetBytes(id)); + result.AddRange(BigEndian(data.Length, 4)); + result.AddRange(data); + if ((data.Length & 1) != 0) + result.Add(0); + return [.. result]; + } + + /// A FORM of with (each already padded). + public static byte[] Form(string kind, params byte[][] chunks) + { + var body = new List(Encoding.ASCII.GetBytes(kind)); + foreach (var chunk in chunks) + body.AddRange(chunk); + return [.. Encoding.ASCII.GetBytes("FORM"), .. BigEndian(body.Count, 4), .. body]; + } + + public static byte[] DjvuFile(byte[] form) => [.. "AT&T"u8.ToArray(), .. form]; + + public static byte[] Info(int width, int height, int dpi = 300, int flags = 1) => + Chunk("INFO", [(byte)(width >> 8), (byte)width, (byte)(height >> 8), (byte)height, 26, 0, (byte)dpi, (byte)(dpi >> 8), 22, (byte)flags]); + + /// A page: INFO then the other chunks. + public static byte[] Page(int width, int height, int dpi, params byte[][] chunks) => Form("DJVU", [Info(width, height, dpi), .. chunks]); + + /// A palette (FGbz) with a colour index per blit; colours as (r, g, b). + public static byte[] Palette((byte R, byte G, byte B)[] colors, int[]? blitColors) + { + var data = new List { (byte)(blitColors is null ? 0 : 0x80) }; + data.AddRange(BigEndian(colors.Length, 2)); + foreach (var (r, g, b) in colors) + data.AddRange([b, g, r]); + if (blitColors is not null) + { + data.AddRange(BigEndian(blitColors.Length, 3)); + data.AddRange(BzzEncoder.Encode([.. blitColors.SelectMany(i => BigEndian(i, 2))])); + } + return Chunk("FGbz", [.. data]); + } + + /// TXTz: the text and its zone tree. + public static byte[] Text(string text, TextZone page) + { + var bytes = Encoding.UTF8.GetBytes(text); + var data = new List(); + data.AddRange(BigEndian(bytes.Length, 3)); + data.AddRange(bytes); + data.Add(1); + WriteZone(data, page, null, null); + return Chunk("TXTz", BzzEncoder.Encode([.. data])); + } + + private static void WriteZone(List data, TextZone zone, TextZone? parent, TextZone? previous) + { + int x = zone.Left, y = zone.Bottom, start = zone.Start; + if (previous is not null) + { + if (zone.Type is 1 or 4 or 5) + { + x -= previous.Left; + y = previous.Bottom - zone.Bottom - zone.Height; + } + else + { + x -= previous.Left + previous.Width; + y -= previous.Bottom; + } + start -= previous.Start + previous.Length; + } + else if (parent is not null) + { + x -= parent.Left; + y = parent.Bottom + parent.Height - zone.Bottom - zone.Height; + start -= parent.Start; + } + data.Add((byte)zone.Type); + foreach (var value in new[] { x, y, zone.Width, zone.Height, start }) + data.AddRange(BigEndian(value + 0x8000, 2)); + data.AddRange(BigEndian(zone.Length, 3)); + data.AddRange(BigEndian(zone.Children.Length, 3)); + TextZone? last = null; + foreach (var child in zone.Children) + { + WriteZone(data, child, zone, last); + last = child; + } + } + + /// + /// 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) + { + var count = components.Length; + byte[] Dirm(long[] offsets) + { + var data = new List { 0x81 }; + data.AddRange(BigEndian(count, 2)); + foreach (var offset in offsets) + data.AddRange(BigEndian((int)offset, 4)); + 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.Page ? 1 : 0)); + foreach (var c in components) + { + table.AddRange(Encoding.UTF8.GetBytes(c.Id)); + table.Add(0); + } + data.AddRange(BzzEncoder.Encode([.. table])); + return Chunk("DIRM", [.. data]); + } + var dirmSize = Dirm(new long[count]).Length; + var offsets = new long[count]; + long at = 4 + 12 + dirmSize; + 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) }; + foreach (var c in components) + parts.Add((c.Form.Length & 1) != 0 ? [.. c.Form, 0] : c.Form); + return DjvuFile(Form("DJVM", [.. parts])); + } + + public static byte[] Include(string id) => Chunk("INCL", Encoding.UTF8.GetBytes(id)); + + public static byte[] BigEndian(int value, int bytes) + { + var result = new byte[bytes]; + for (var i = 0; i < bytes; i++) + result[i] = (byte)(value >> (8 * (bytes - 1 - i))); + return result; + } +} diff --git a/tests/Filee.Engines.Tests/DjvuFuzzTests.cs b/tests/Filee.Engines.Tests/DjvuFuzzTests.cs new file mode 100644 index 000000000..b0cb34090 --- /dev/null +++ b/tests/Filee.Engines.Tests/DjvuFuzzTests.cs @@ -0,0 +1,65 @@ +// 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). +// 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. + +using Filee.Engines.Djvu; +using Filee.Engines.Ocr; +using static Filee.Engines.Tests.DjvuBuilder; + +namespace Filee.Engines.Tests; + +[Trait("Category", "Fuzz")] +public class DjvuFuzzTests(EngineFixture fx) : IClassFixture +{ + /// Inputs per seed in a normal test run. + private const int QuickIterations = 60; + + private static readonly Jb2Shape Letter = Jb2Writer.Shape(".##.", "#..#", "####", "#..#"); + + private static byte[] MaskPage() + { + var mask = new Jb2Writer().Start(64, 32).New(Letter, 2, 20).Copy(0, 8, 20).Refine(0, Jb2Writer.Shape("###.", "#..#", "####", "#..#"), 14, 20) + .Copy(0, 2, 4).NonMark(Letter, 40, 10).End(); + var text = DjvuBuilder.Text("ab cd\n", new TextZone(1, 0, 0, 64, 32, 0, 6, + new TextZone(5, 2, 20, 20, 4, 0, 6, new TextZone(6, 2, 20, 8, 4, 0, 3), new TextZone(6, 14, 20, 4, 4, 3, 3)))); + return DjvuFile(Page(64, 32, 150, Chunk("Sjbz", mask), Palette([(255, 0, 0), (0, 0, 255)], [0, 1, 0, 1, 0]), text)); + } + + private static byte[] BundledBook() + { + var dictionary = new Jb2Writer().Start(0, 0).New(Letter, type: 2); + var shared = Form("DJVI", Chunk("Djbz", dictionary.End())); + var page = Form("DJVU", Info(40, 20, 100), Include("shared"), Chunk("Sjbz", new Jb2Writer().RequireDictionary(1, dictionary.Library).Start(40, 20).Copy(0, 1, 1).Copy(0, 10, 1).End())); + var y = Enumerable.Range(0, 8 * 4).Select(i => (sbyte)(i * 3 - 40)).ToArray(); + var background = Iw44Encoder.Encode(8, 4, y, new sbyte[32], new sbyte[32], slices: 40).Select(c => Chunk("BG44", c)); + var compound = Form("DJVU", [Info(40, 20, 100), Chunk("Sjbz", new Jb2Writer().Start(40, 20).New(Letter, 5, 5).End()), .. background]); + return Bundle(("shared", shared, false), ("p1", page, true), ("p2", compound, true)); + } + + private static byte[] Photo() + { + var y = Enumerable.Range(0, 24 * 16).Select(i => (sbyte)((i % 24) * 8 - 90)).ToArray(); + var chunks = Iw44Encoder.Encode(24, 16, y, slices: 30, chunks: 2).Select(c => Chunk("BG44", c)); + return DjvuFile(Form("DJVU", [Info(24, 16, 75, flags: 5), .. chunks])); + } + + [Fact] + public void DjvuDocument_reads_or_refuses_every_mutated_file() => + new FuzzHarness(fx, QuickIterations, 20261120).Files("djvu", "djvu", [MaskPage(), BundledBook(), Photo()], 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(); + 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); + } + 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 new file mode 100644 index 000000000..096699e5c --- /dev/null +++ b/tests/Filee.Engines.Tests/DjvuTests.cs @@ -0,0 +1,610 @@ +// 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. + +using System.Diagnostics; +using Filee.Core.Conversion; +using Filee.Core.Formats; +using Filee.Core.Presets; +using Filee.Engines.Djvu; +using Filee.Engines.Documents; +using ImageMagick; +using SkiaSharp; +using UglyToad.PdfPig; +using static Filee.Engines.Tests.DjvuBuilder; + +#pragma warning disable CA1416 // PDFtoImage draws the PDF back on every platform the tests run on + +namespace Filee.Engines.Tests; + +public sealed class DjvuTests(EngineFixture fx) : IClassFixture, IDisposable +{ + private readonly string _folder = fx.NewFolder(); + + private static CancellationToken Token => TestContext.Current.CancellationToken; + + public void Dispose() + { + try + { + Directory.Delete(_folder, recursive: true); + } + catch (IOException) + { + } + } + + private string Save(byte[] file, string name = "test.djvu") + { + var path = Path.Combine(_folder, name); + File.WriteAllBytes(path, file); + return path; + } + + private async Task> Convert(string source, string target, Preset? preset = null, Action? leftOut = null) + { + var output = Path.Combine(_folder, "out-" + Guid.NewGuid().ToString("N")[..6]); + Directory.CreateDirectory(output); + return await new DjvuConverter().ConvertAsync( + new ConversionStep(source, "djvu", target, preset ?? new Preset(), new FolderOutput(output), _folder, LeftOut: leftOut), null, Token); + } + + // ---- shapes and masks used by several tests ---- + + private static readonly Jb2Shape Letter = Jb2Writer.Shape( + "..####..", + ".##..##.", + "##....##", + "##....##", + "########", + "##....##", + "##....##", + "##....##"); + + private static readonly Jb2Shape Bar = Jb2Writer.Shape( + "##########", + "##########", + "..........", + "##########"); + + /// Letter with its top-left pixel set: refines Letter. + private static readonly Jb2Shape LetterMark = Jb2Writer.Shape( + "#.####..", + ".##..##.", + "##....##", + "##....##", + "########", + "##....##", + "##....##", + "##.#..##"); + + /// The page drawn from (shape, left, bottom) placements, top row first. + private static bool[,] Expected(int width, int height, params (Jb2Shape Shape, int Left, int Bottom)[] blits) + { + var page = new bool[height, width]; + foreach (var (shape, left, bottom) in blits) + { + for (var y = 0; y < shape.Height; y++) + { + for (var x = 0; x < shape.Width; x++) + { + var px = left + x; + var py = height - 1 - (bottom + y); + if (shape[x, y] != 0 && px >= 0 && px < width && py >= 0 && py < height) + page[py, px] = true; + } + } + } + return page; + } + + private static bool[,] Mask(DjvuPage page) + { + var bits = DjvuRenderer.MaskBits(page, cancellationToken: Token); + var stride = (page.Width + 7) / 8; + var result = new bool[page.Height, page.Width]; + for (var y = 0; y < page.Height; y++) + { + for (var x = 0; x < page.Width; x++) + result[y, x] = (bits[y * stride + (x >> 3)] & (0x80 >> (x & 7))) != 0; + } + return result; + } + + /// A 120 × 60 mask: letters on two lines, a copy, a refinement, an image-only shape, non-mark data. + private static (byte[] Data, bool[,] Expected) TextMask() + { + var writer = new Jb2Writer().Start(120, 60) + .New(Letter, 5, 40) + .New(Bar, 20, 44) + .Copy(0, 35, 40) + .Comment("Filee test") + .Refine(0, LetterMark, 50, 39) + .New(Bar, 70, 45, type: 3) + .Reset() + .Copy(0, 5, 10) // a new line + .Copy(1, 20, 12) + .NonMark(Bar, 100, 30); + var expected = Expected(120, 60, + (Letter, 5, 40), (Bar, 20, 44), (Letter, 35, 40), (LetterMark, 50, 39), (Bar, 70, 45), + (Letter, 5, 10), (Bar, 20, 12), (Bar, 100, 27)); + return (writer.End(), expected); + } + + private static byte[] BilevelPage(int dpi = 100, int flags = 1, params byte[][] extra) + { + var (mask, _) = TextMask(); + return Form("DJVU", [Info(120, 60, dpi, flags), Chunk("Sjbz", mask), .. extra]); + } + + /// Hidden text: one line "Hello DjVu" with two words over the first letters, and a line of Cyrillic. + private static byte[] HelloText() + { + const string text = "Hello DjVu\nПривет\n"; + var hello = new TextZone(6, 5, 38, 25, 12, 0, 6); + var djvu = new TextZone(6, 35, 38, 30, 12, 6, 5); + var line1 = new TextZone(5, 5, 38, 60, 12, 0, 11, hello, djvu); + var line2 = new TextZone(5, 5, 8, 50, 12, 11, System.Text.Encoding.UTF8.GetByteCount("Привет\n")); + var paragraph = new TextZone(4, 5, 8, 60, 42, 0, System.Text.Encoding.UTF8.GetByteCount(text), line1, line2); + var page = new TextZone(1, 0, 0, 120, 60, 0, System.Text.Encoding.UTF8.GetByteCount(text), paragraph); + return DjvuBuilder.Text(text, page); + } + + // ---- the pieces ---- + + [Fact] + public void Zp_coder_round_trips_adaptive_raw_and_iw44_bits() + { + var random = new Random(346); + var steps = Enumerable.Range(0, 20_000).Select(_ => (Kind: random.Next(3), Context: random.Next(8), Bit: random.Next(10) < 8 ? 0 : 1)).ToList(); + var encoder = new ZpEncoder(); + var contexts = new byte[8]; + foreach (var (kind, context, bit) in steps) + { + if (kind == 0) + encoder.Encode(bit, ref contexts[context]); + else if (kind == 1) + encoder.EncodeRaw(bit); + else + encoder.EncodeIw(bit); + } + var data = encoder.Finish(); + var decoder = new ZpDecoder(data); + Array.Clear(contexts); + var decoded = steps.Select(s => s.Kind switch + { + 0 => decoder.Decode(ref contexts[s.Context]), + 1 => decoder.DecodeRaw(), + _ => decoder.DecodeIw(), + }).ToList(); + Assert.Equal(steps.Select(s => s.Bit), decoded); + Assert.True(data.Length < 20_000 / 8, "biased bits compress"); + } + + [Theory] + [InlineData(0, 1 << 20)] + [InlineData(1, 1 << 20)] + [InlineData(5_000, 1 << 20)] + [InlineData(5_000, 1_000)] // several blocks: the contexts carry on + public void Bzz_round_trips(int length, int blockSize) + { + var random = new Random(length); + var words = new[] { "Filee ", "DjVu ", "scanned ", "book ", "책 ", "книга " }; + var text = new System.Text.StringBuilder(); + while (text.Length < length) + text.Append(words[random.Next(words.Length)]); + var data = System.Text.Encoding.UTF8.GetBytes(text.ToString())[..Math.Min(length, System.Text.Encoding.UTF8.GetByteCount(text.ToString()))]; + var packed = BzzEncoder.Encode(data, blockSize); + Assert.Equal(data, BzzDecoder.Decode(packed, 1 << 20, Token)); + var noise = new byte[length]; + random.NextBytes(noise); + Assert.Equal(noise, BzzDecoder.Decode(BzzEncoder.Encode(noise, blockSize), 1 << 20, Token)); + } + + [Fact] + public void Bzz_refuses_to_unpack_past_its_limit() + { + // Bytes of two values: they pack well and sort quickly. + var random = new Random(346); + var packed = BzzEncoder.Encode([.. Enumerable.Range(0, 50_000).Select(_ => (byte)random.Next(2))]); + Assert.True(packed.Length < 12_000); + var refused = Assert.Throws(() => BzzDecoder.Decode(packed, 10_000, Token)); + Assert.Equal(DocumentLimits.ErrorKey, refused.ErrorKey); + } + + [Fact] + public void Jb2_mask_draws_new_shapes_copies_refinements_and_non_mark_data() + { + var (data, expected) = TextMask(); + var image = Jb2Decoder.DecodeImage(data, null, DjvuLimits.Default, Token); + Assert.Equal(120, image.Width); + Assert.Equal(60, image.Height); + Assert.Equal(8, image.Blits.Count); + var page = new DjvuPage { Width = 120, Height = 60, Dpi = 300, Mask = image }; + Assert.Equal(expected, Mask(page)); + } + + [Fact] + public void Jb2_mask_builds_on_a_shared_dictionary() + { + var dictionaryWriter = new Jb2Writer().Start(0, 0).New(Letter, type: 2).New(Bar, type: 2).Refine(0, LetterMark, type: 5); + var dictionaryData = dictionaryWriter.End(); + var dictionary = Jb2Decoder.DecodeDictionary(dictionaryData, null, DjvuLimits.Default, Token); + Assert.Equal(3, dictionary.Shapes.Count); + + var image = new Jb2Writer().RequireDictionary(3, dictionaryWriter.Library).Start(60, 30) + .Copy(2, 2, 15).Copy(1, 15, 17).Copy(0, 30, 15).New(Bar, 45, 2).End(); + var mask = Jb2Decoder.DecodeImage(image, dictionary, DjvuLimits.Default, Token); + var page = new DjvuPage { Width = 60, Height = 30, Dpi = 300, Mask = mask }; + Assert.Equal(Expected(60, 30, (LetterMark, 2, 15), (Bar, 15, 17), (Letter, 30, 15), (Bar, 45, 2)), Mask(page)); + + // Without the dictionary it asks for, the mask is damage, not a crash. + Assert.Throws(() => Jb2Decoder.DecodeImage(image, null, DjvuLimits.Default, Token)); + } + + /// A grey test picture: a gradient with a dark block near the top left and a light stripe. + private static byte[] GreySource(int width, int height) + { + var grey = new byte[width * height]; + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + var value = 40 + 180 * x / width; + if (x is >= 8 and < 30 && y is >= 5 and < 20) + value = 20; + if (y is >= 40 and < 44) + value = 235; + grey[y * width + x] = (byte)value; + } + } + return grey; + } + + // The pictures are 128 wide: a row of 4 to 7 samples at some scale doesn't come back exactly (the lifting filters + // of the encoder and decoder treat such short rows differently), which a real decoder shares. + [Theory] + [InlineData(1)] + [InlineData(3)] + public void Iw44_grey_picture_comes_back_close_to_its_source_the_right_way_up(int chunks) + { + const int width = 128, height = 70; + var grey = GreySource(width, height); + var y = grey.Select(g => (sbyte)(127 - g)).ToArray(); + var decoder = new Iw44Decoder(DjvuLimits.Default); + foreach (var chunk in Iw44Encoder.Encode(width, height, y, slices: 240 / chunks, chunks: chunks)) + decoder.Decode(chunk, Token); + var pixmap = decoder.ToPixmap(Token); + Assert.Equal(width, pixmap.Width); + Assert.Equal(height, pixmap.Height); + var errors = Enumerable.Range(0, width * height).Select(i => Math.Abs(pixmap.Rgb[3 * i] - grey[i])).ToList(); + Assert.True(errors.Average() < 2.0, $"mean error {errors.Average()}"); + Assert.True(errors.Max() < 24, $"max error {errors.Max()}"); + Assert.True(pixmap.Rgb[3 * (10 * width + 15)] < 40, "the dark block is at the top"); + } + + [Theory] + [InlineData(false, 0)] + [InlineData(true, 10)] + public void Iw44_colour_picture_comes_back_through_the_ycbcr_transform(bool halfChroma, int chromaDelay) + { + const int width = 128, height = 48; + var y = new sbyte[width * height]; + var cb = new sbyte[width * height]; + var cr = new sbyte[width * height]; + for (var row = 0; row < height; row++) + { + for (var x = 0; x < width; x++) + { + var i = row * width + x; + y[i] = (sbyte)(-60 + x); + cb[i] = (sbyte)(row < height / 2 ? 30 : -20); + cr[i] = (sbyte)(x < width / 2 ? -25 : 35); + } + } + var decoder = new Iw44Decoder(DjvuLimits.Default); + foreach (var chunk in Iw44Encoder.Encode(width, height, y, cb, cr, slices: 240, halfChroma: halfChroma, chromaDelay: chromaDelay)) + decoder.Decode(chunk, Token); + var pixmap = decoder.ToPixmap(Token); + var total = 0.0; + for (var i = 0; i < width * height; i++) + { + int b = cb[i], r = cr[i]; + var t2 = r + (r >> 1); + var t3 = y[i] + 128 - (b >> 2); + int[] want = [Math.Clamp(y[i] + 128 + t2, 0, 255), Math.Clamp(t3 - (t2 >> 1), 0, 255), Math.Clamp(t3 + (b << 1), 0, 255)]; + for (var c = 0; c < 3; c++) + total += Math.Abs(pixmap.Rgb[3 * i + c] - want[c]); + } + var mean = total / (3 * width * height); + Assert.True(mean < (halfChroma ? 8 : 4), $"mean error {mean}"); + } + + [Fact] + public void Hidden_text_reads_lines_and_words() + { + var chunk = HelloText(); + var data = BzzDecoder.Decode(chunk[8..(8 + ReadSize(chunk))], 1 << 20, Token); + var lines = DjvuText.Read(data, DjvuLimits.Default); + Assert.Equal(2, lines.Count); + Assert.Equal(["Hello", "DjVu"], lines[0].Words.Select(w => w.Text)); + Assert.Equal(new DjvuRect(35, 38, 30, 12), lines[0].Words[1].Box); + Assert.Equal("Привет", lines[1].Text); + Assert.Empty(lines[1].Words); + } + + private static int ReadSize(byte[] chunk) => (chunk[4] << 24) | (chunk[5] << 16) | (chunk[6] << 8) | chunk[7]; + + // ---- documents ---- + + [Fact] + public async Task A_bilevel_page_becomes_a_pdf_page_of_its_size_with_a_mask_and_searchable_text() + { + var source = Save(DjvuFile(BilevelPage(100, 1, HelloText()))); + var pdfPath = Assert.Single(await Convert(source, "pdf")); + using (var pdf = PdfDocument.Open(pdfPath)) + { + var page = Assert.Single(pdf.GetPages()); + Assert.Equal(120 * 72 / 100.0, page.Width, 1); + Assert.Equal(60 * 72 / 100.0, page.Height, 1); + var image = Assert.Single(page.GetImages()); + Assert.True(image.IsImageMask); + var words = page.GetWords().Select(w => w.Text).ToList(); + Assert.Contains("Hello", words); + Assert.Contains("DjVu", words); + Assert.Contains("Привет", words); + } + // Drawn back, black where the mask is. + using var bitmap = PDFtoImage.Conversion.ToImage(File.ReadAllBytes(pdfPath), 0, null, new PDFtoImage.RenderOptions(Dpi: 100)); + var (_, expected) = TextMask(); + var agree = 0; + for (var y = 0; y < 60; y++) + { + for (var x = 0; x < 120; x++) + { + if ((bitmap.GetPixel(x, y).Red < 128) == expected[y, x]) + agree++; + } + } + Assert.True(agree > 0.97 * 120 * 60, $"{agree} pixels agree"); + } + + [Fact] + public async Task A_bilevel_page_becomes_a_picture_at_its_resolution() + { + var source = Save(DjvuFile(BilevelPage(100))); + var png = Assert.Single(await Convert(source, "png")); + using var image = new MagickImage(png); + Assert.Equal(120u, image.Width); + Assert.Equal(60u, image.Height); + Assert.Equal(100, image.Density.ChangeUnits(DensityUnit.PixelsPerInch).X, 0); + var (_, expected) = TextMask(); + using var pixels = image.GetPixels(); + for (var y = 0; y < 60; y++) + { + for (var x = 0; x < 120; x++) + Assert.Equal(expected[y, x], pixels.GetPixel(x, y).GetChannel(0) < 128); + } + Assert.Single(await Convert(source, "jpg")); + } + + /// + /// A bundled document: a shared dictionary (DJVI) the first page includes, a compound page (background, two + /// palette colours, text) and a photo page turned a quarter counter-clockwise. + /// + private static byte[] Book() + { + var dictionaryWriter = new Jb2Writer().Start(0, 0).New(Letter, type: 2).New(Bar, type: 2); + var shared = Form("DJVI", Chunk("Djbz", dictionaryWriter.End())); + var page1Mask = new Jb2Writer().RequireDictionary(2, dictionaryWriter.Library).Start(120, 60) + .Copy(0, 5, 40).Copy(1, 20, 44).Copy(0, 35, 40).End(); + var page1 = Form("DJVU", Info(120, 60, 100), Include("dict0001.djbz"), Chunk("Sjbz", page1Mask)); + + var bgWidth = 40; + var bgHeight = 20; // a third of 120 × 60 + var bgY = new sbyte[bgWidth * bgHeight]; + var bgCb = new sbyte[bgWidth * bgHeight]; + var bgCr = new sbyte[bgWidth * bgHeight]; + Array.Fill(bgY, (sbyte)90); // light + Array.Fill(bgCr, (sbyte)-20); + var background = Iw44Encoder.Encode(bgWidth, bgHeight, bgY, bgCb, bgCr, slices: 100).Select(c => Chunk("BG44", c)).ToArray(); + var page2Mask = new Jb2Writer().Start(120, 60).New(Letter, 5, 40).New(Bar, 20, 44).Copy(0, 35, 40).End(); + var palette = Palette([(200, 0, 0), (0, 0, 180)], [0, 1, 0]); + var page2 = Form("DJVU", [Info(120, 60, 100), Chunk("Sjbz", page2Mask), palette, .. background, HelloText()]); + + var photoY = GreySource(80, 50).Select(g => (sbyte)(127 - g)).ToArray(); + var photo = Iw44Encoder.Encode(80, 50, photoY, slices: 100).Select(c => Chunk("BG44", c)).ToArray(); + var page3 = Form("DJVU", [Info(80, 50, 50, flags: 6), .. photo]); + + return Bundle(("dict0001.djbz", shared, false), ("p0001.djvu", page1, true), ("p0002.djvu", page2, true), ("p0003.djvu", page3, true)); + } + + [Fact] + public void A_bundled_document_reads_its_pages_with_the_shared_dictionary() + { + var source = Save(Book()); + using var document = DjvuDocument.Open(source, cancellationToken: Token); + Assert.Equal(3, document.Count); + var page1 = document.ReadPage(0, Token); + Assert.Equal(Expected(120, 60, (Letter, 5, 40), (Bar, 20, 44), (Letter, 35, 40)), Mask(page1)); + + var page2 = document.ReadPage(1, Token); + var rgb = DjvuRenderer.Compose(page2, Token); + (byte R, byte G, byte B) At(int x, int y) => (rgb[(y * 120 + x) * 3], rgb[(y * 120 + x) * 3 + 1], rgb[(y * 120 + x) * 3 + 2]); + // The first letter red, the bar blue, the background light (Y 90 is light: 218 on the red channel minus chroma). + Assert.Equal((200, 0, 0), At(5 + 3, 60 - 1 - (40 + 3))); + Assert.Equal((0, 0, 180), At(25, 60 - 1 - (44 + 3))); + var paper = At(100, 5); + Assert.True(paper.R > 150 && paper.G > 150, $"background {paper}"); + + var page3 = document.ReadPage(2, Token); + Assert.Equal(1, page3.Rotation); + Assert.Null(page3.Mask); + Assert.Equal(0, document.LeftOut); + } + + [Fact] + public async Task A_bundled_document_becomes_a_pdf_with_its_layers_turns_and_text() + { + var source = Save(Book()); + var pdfPath = Assert.Single(await Convert(source, "pdf")); + using var pdf = PdfDocument.Open(pdfPath); + var pages = pdf.GetPages().ToList(); + Assert.Equal(3, pages.Count); + Assert.Single(pages[0].GetImages(), i => i.IsImageMask); + var images = pages[1].GetImages().ToList(); + Assert.Equal(3, images.Count); // the background and a mask per colour + Assert.Equal(2, images.Count(i => i.IsImageMask)); + Assert.Contains("Hello", pages[1].GetWords().Select(w => w.Text)); + Assert.Equal(270, pages[2].Rotation.Value); + Assert.Equal(80 * 72 / 50.0, pages[2].MediaBox.Bounds.Width, 1); + } + + [Fact] + public async Task A_bundled_document_becomes_one_tiff_or_pictures_of_the_chosen_pages() + { + var source = Save(Book()); + var tiff = Assert.Single(await Convert(source, "tiff")); + using (var frames = new MagickImageCollection(tiff)) + { + Assert.Equal(3, frames.Count); + // The photo page turned a quarter counter-clockwise: 50 wide, 80 high. + Assert.Equal(50u, frames[2].Width); + Assert.Equal(80u, frames[2].Height); + } + var preset = new Preset(); + preset.Pdf.PageRange = "2"; + var png = Assert.Single(await Convert(source, "png", preset)); + using var image = new MagickImage(png); + using var pixels = image.GetPixels(); + var red = pixels.GetPixel(8, 60 - 1 - 43).ToColor()!; + Assert.True(red.R > 180 && red.G < 40 && red.B < 40, $"red letter {red}"); + } + + [Fact] + public async Task A_page_with_a_foreground_picture_shows_it_through_the_mask() + { + var mask = new Jb2Writer().Start(120, 60).New(Letter, 5, 40).New(Bar, 20, 44).End(); + var fgY = new sbyte[10 * 5]; + var fgCb = new sbyte[10 * 5]; + var fgCr = new sbyte[10 * 5]; + Array.Fill(fgY, (sbyte)-60); + Array.Fill(fgCb, (sbyte)60); // blue + var foreground = Iw44Encoder.Encode(10, 5, fgY, fgCb, fgCr, slices: 100).Select(c => Chunk("FG44", c)).ToArray(); + var source = Save(DjvuFile(Form("DJVU", [Info(120, 60, 100), Chunk("Sjbz", mask), .. foreground]))); + using (var document = DjvuDocument.Open(source, cancellationToken: Token)) + { + var page = document.ReadPage(0, Token); + Assert.NotNull(page.Foreground); + var rgb = DjvuRenderer.Compose(page, Token); + var at = ((60 - 1 - 43) * 120 + 8) * 3; + Assert.True(rgb[at + 2] > rgb[at] + 60, "blue letter"); + Assert.Equal(255, rgb[(5 * 120 + 100) * 3]); // white paper + } + using var pdf = PdfDocument.Open(Assert.Single(await Convert(source, "pdf"))); + var image = Assert.Single(pdf.GetPages().Single().GetImages()); + Assert.False(image.IsImageMask); + } + + [Fact] + public async Task Layers_filee_does_not_decode_are_named_in_the_result() + { + var source = Save(DjvuFile(BilevelPage(100, 1, Chunk("BG2k", [1, 2, 3, 4])))); + LeftOutNotice? notice = null; + Assert.Single(await Convert(source, "pdf", leftOut: n => notice = n)); + Assert.NotNull(notice); + Assert.Equal(new LeftOutPart("notice.renderer_left_out.pictures", 1), Assert.Single(notice.Parts)); + } + + [Fact] + public async Task An_indirect_document_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))); + var refused = await Assert.ThrowsAsync(() => Convert(source, "pdf")); + Assert.Equal(DjvuDocument.IndirectKey, refused.ErrorKey); + } + + [Fact] + public void DjVu_is_a_readable_document_format_with_a_route_to_pdf_and_pictures() + { + var format = FormatRegistry.Detect("x.djv"); + Assert.NotNull(format); + Assert.Equal("djvu", format.Id); + Assert.Equal(FormatCategory.Document, format.Category); + var converter = Assert.Single(fx.Converters.OfType()); + Assert.Equal(["pdf", "png", "jpg", "tiff"], converter.Edges.Select(e => e.To)); + } + + // ---- hostile files ---- + + [Fact] + public void Hostile_pages_are_refused_quickly() + { + var watch = Stopwatch.StartNew(); + // A page claiming 65,535 × 65,535 pixels. + AssertRefused(DjvuFile(Form("DJVU", Info(65535, 65535))), limits: null); + // A shape claiming 65,535 × 65,535 pixels. + var huge = new Jb2Writer().Start(100, 100).Record(1).Size(65535, 65535).End(); + AssertRefused(DjvuFile(Form("DJVU", Info(100, 100), Chunk("Sjbz", huge))), limits: null); + // An IW44 picture claiming 65,535 × 65,535 pixels. + byte[] bigPicture = [0, 1, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0, 0, 0]; + AssertRefused(DjvuFile(Form("DJVU", Info(100, 100), Chunk("BG44", bigPicture))), limits: null); + // More shapes, number cells and text zones than the (lowered) limits allow. + var shapes = new Jb2Writer().Start(200, 200).New(Letter, 0, 0); + for (var i = 0; i < 50; i++) + shapes.Copy(0, (i * 7) % 190, (i * 3) % 190); + AssertRefused(DjvuFile(Form("DJVU", Info(200, 200), Chunk("Sjbz", shapes.End()))), DjvuLimits.Default with { MaxBlits = 20 }); + AssertRefused(DjvuFile(Form("DJVU", Info(200, 200), Chunk("Sjbz", new Jb2Writer().Start(200, 200).New(Letter, 0, 0).New(Bar, 20, 0).End()))), + DjvuLimits.Default with { MaxNumberCells = 40 }); + var zones = Enumerable.Range(0, 30).Select(i => new TextZone(6, i * 3, 0, 3, 10, 0, 1)).ToArray(); + 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 })); + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"took {watch.Elapsed}"); + } + + private void AssertRefused(byte[] file, DjvuLimits? limits) + { + var source = Save(file, "hostile.djvu"); + using var document = DjvuDocument.Open(source, limits, Token); + var refused = Assert.Throws(() => document.ReadPage(0, Token)); + Assert.Equal(DocumentLimits.ErrorKey, refused.ErrorKey); + } + + [Fact] + public void Damaged_and_looping_documents_fail_as_damage() + { + // Two included files including each other, and a page asking for a dictionary neither has. + var a = Form("DJVI", Include("b")); + var b = Form("DJVI", Include("a")); + var mask = new Jb2Writer().RequireDictionary(2, [Letter, Bar]).Start(50, 50).Copy(0, 1, 1).End(); + var page = Form("DJVU", Info(50, 50), Include("a"), Chunk("Sjbz", mask)); + var source = Save(Bundle(("a", a, false), ("b", b, false), ("p", page, true))); + using (var document = DjvuDocument.Open(source, cancellationToken: Token)) + Assert.Throws(() => document.ReadPage(0, Token)); + + // A bundle cut short in its last page, and a directory that claims more parts than it holds. + var book = Book(); + var cut = Save(book[..(book.Length - 40)], "cut.djvu"); + try + { + using var document = DjvuDocument.Open(cut, cancellationToken: Token); + for (var i = 0; i < document.Count; i++) + _ = document.ReadPage(i, Token); + } + catch (InvalidDataException) + { + } + var lying = Chunk("DIRM", [0x81, 0x01, 0x00, .. new byte[16]]); + Assert.Throws(() => DjvuDocument.Open(Save(DjvuFile(Form("DJVM", lying)), "lying.djvu"), cancellationToken: Token).Dispose()); + Assert.Throws(() => DjvuDocument.Open(Save("not a djvu file"u8.ToArray(), "text.djvu"), cancellationToken: Token).Dispose()); + } + + private sealed class FolderOutput(string folder) : IOutputAllocator + { + public string? Allocate(string extension, string? suffix = null) => Path.Combine(folder, $"out{suffix}.{extension}"); + } +} From 0b86379003b8635373e1ea05aa4e58b3ddae62e1 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 18:22:46 +0900 Subject: [PATCH 5/6] settings: v112 teaches existing libraries DjVu documents (.djvu, .djv, #346) Co-Authored-By: Claude Opus 5.5 --- src/Filee.Core/Settings/AppSettings.cs | 2 +- src/Filee.Core/Settings/UserDataStore.cs | 3 ++ tests/Filee.Core.Tests/DjvuSettingsTests.cs | 37 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/Filee.Core.Tests/DjvuSettingsTests.cs diff --git a/src/Filee.Core/Settings/AppSettings.cs b/src/Filee.Core/Settings/AppSettings.cs index 1984d1bb1..e149598b2 100644 --- a/src/Filee.Core/Settings/AppSettings.cs +++ b/src/Filee.Core/Settings/AppSettings.cs @@ -170,7 +170,7 @@ public static bool IsFlipKeyAllowed(string key, IEnumerable gest public sealed class AppSettings { /// Current schema version. Increase when the format changes and add a migration. - public const int CurrentSchemaVersion = 109; + public const int CurrentSchemaVersion = 112; public int SchemaVersion { get; set; } = CurrentSchemaVersion; diff --git a/src/Filee.Core/Settings/UserDataStore.cs b/src/Filee.Core/Settings/UserDataStore.cs index 51cd46eaa..cfd4f114e 100644 --- a/src/Filee.Core/Settings/UserDataStore.cs +++ b/src/Filee.Core/Settings/UserDataStore.cs @@ -1236,6 +1236,9 @@ public static bool ApplyToLibrary(int from, List presets, List + Assert.NotNull(ProfileSelector.Select(BuiltInData.CreateProfiles(), [file])); + + [Fact] + public void Version_112_donuts_learn_djvu_and_djv_without_taking_custom_claims() + { + var presets = BuiltInData.CreatePresets(); + var profiles = BuiltInData.CreateProfiles(); + var id = ProfileSelector.Select(profiles, ["book.djvu"])!.Id; + var donut = profiles.Single(p => p.Id == id).Extensions; + donut.Remove("djvu"); + donut.Remove("djv"); + + Assert.True(SettingsMigrations.ApplyToLibrary(111, presets, profiles)); + Assert.Contains("djvu", donut); + Assert.Contains("djv", donut); + Assert.False(SettingsMigrations.ApplyToLibrary(111, presets, profiles)); + + donut.Remove("djvu"); + donut.Remove("djv"); + profiles.Insert(0, new ToolbarProfile { Id = "my-books", Name = "Scanned books", Extensions = ["DJVU"] }); + Assert.True(SettingsMigrations.ApplyToLibrary(111, presets, profiles)); + Assert.DoesNotContain("djvu", donut); // the user's own donut has it + Assert.Contains("djv", donut); + } +} From 8f3b35b54bd5ff676616b9480bf7e9badef82e53 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 20:07:55 +0900 Subject: [PATCH 6/6] fix(djvu): spread words that all have their line's box Some DjVu files, the DjVu v3 specification among them, give every word zone the box of its whole line. Placed as is, the words of a line overlapped in the PDF and text extraction interleaved their letters. Lines whose word boxes don't follow each other left to right now get their words spread over the line by length, like lines without words. Checked on public DjVu files: pages render the same as the reference pictures, and the specification's text now extracts in reading order. Co-Authored-By: Claude Opus 5.5 --- src/Filee.Engines/Djvu/DjvuPdfWriter.cs | 18 +++++++++++++- tests/Filee.Engines.Tests/DjvuTests.cs | 32 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Filee.Engines/Djvu/DjvuPdfWriter.cs b/src/Filee.Engines/Djvu/DjvuPdfWriter.cs index 9d3e2a607..75bedafc9 100644 --- a/src/Filee.Engines/Djvu/DjvuPdfWriter.cs +++ b/src/Filee.Engines/Djvu/DjvuPdfWriter.cs @@ -126,7 +126,7 @@ internal static List Lines(DjvuPage page) foreach (var line in page.Text) { var box = line.Box; - if (line.Words.Count == 0) + if (line.Words.Count == 0 || !LaidOut(line.Words)) { lines.Add(OcrLine.Estimate(line.Text, box.Left / w, box.Bottom / h, box.Width / w, box.Height / h)); continue; @@ -137,6 +137,22 @@ internal static List Lines(DjvuPage page) return lines; } + /// + /// Whether a line's word boxes follow each other left to right. Some files give every word the box of its whole + /// line; their words are spread over the line by their length instead, so they don't overlap in the PDF. + /// + private static bool LaidOut(IReadOnlyList words) + { + for (var i = 1; i < words.Count; i++) + { + var previous = words[i - 1].Box; + var word = words[i].Box; + if (previous.Left + previous.Width - word.Left > Math.Min(previous.Width, word.Width) / 2) + return false; + } + 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) { diff --git a/tests/Filee.Engines.Tests/DjvuTests.cs b/tests/Filee.Engines.Tests/DjvuTests.cs index 096699e5c..8e2b4d306 100644 --- a/tests/Filee.Engines.Tests/DjvuTests.cs +++ b/tests/Filee.Engines.Tests/DjvuTests.cs @@ -339,6 +339,38 @@ public void Hidden_text_reads_lines_and_words() // ---- documents ---- + [Fact] + public async Task Words_that_all_have_their_lines_box_are_spread_over_the_line() + { + // Some files, the DjVu v3 specification among them, give every word the box of its whole line. + const string text = "Hello DjVu\n"; + var hello = new TextZone(6, 5, 38, 60, 12, 0, 6); + var djvu = new TextZone(6, 5, 38, 60, 12, 6, 5); + var line = new TextZone(5, 5, 38, 60, 12, 0, 11, hello, djvu); + var page = new TextZone(1, 0, 0, 120, 60, 0, 11, line); + var source = Save(DjvuFile(BilevelPage(100, 1, DjvuBuilder.Text(text, page)))); + using (var document = DjvuDocument.Open(source, cancellationToken: Token)) + { + var words = Assert.Single(DjvuPdfWriter.Lines(document.ReadPage(0, Token))).Words; + Assert.Equal(["Hello", "DjVu"], words.Select(w => w.Text)); + Assert.True(words[0].Left + words[0].Width <= words[1].Left, "the words don't overlap"); + } + var pdfPath = Assert.Single(await Convert(source, "pdf")); + using var pdf = PdfDocument.Open(pdfPath); + var pdfWords = Assert.Single(pdf.GetPages()).GetWords().Select(w => w.Text).ToList(); + Assert.Equal(["Hello", "DjVu"], pdfWords); + } + + [Fact] + public void Words_with_boxes_of_their_own_keep_them() + { + var source = Save(DjvuFile(BilevelPage(100, 1, HelloText()))); + using var document = DjvuDocument.Open(source, cancellationToken: Token); + var words = DjvuPdfWriter.Lines(document.ReadPage(0, Token))[0].Words; + Assert.Equal(5 / 120.0, words[0].Left, 6); + Assert.Equal(35 / 120.0, words[1].Left, 6); + } + [Fact] public async Task A_bilevel_page_becomes_a_pdf_page_of_its_size_with_a_mask_and_searchable_text() {