diff --git a/.github/workflows/portable.yml b/.github/workflows/portable.yml index 713ac57d9..9e69199aa 100644 --- a/.github/workflows/portable.yml +++ b/.github/workflows/portable.yml @@ -181,6 +181,10 @@ jobs: if ($project -eq 'Filee.App.Tests') { $testArguments += @('--blame-crash', '--blame-hang-timeout', '5m', '--logger', 'console;verbosity=normal') } + # A native crash in the engine tests (Skia, HarfBuzz) names the test that was running and leaves a dump. + if ($project -eq 'Filee.Engines.Tests') { + $testArguments += @('--blame-crash') + } if ($IsLinux) { xvfb-run -a env -u WAYLAND_DISPLAY XDG_SESSION_TYPE=x11 dotnet @testArguments } else { diff --git a/README.md b/README.md index d752e7e8d..162c4a78f 100644 --- a/README.md +++ b/README.md @@ -172,8 +172,8 @@ 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) 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 (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) and UYAP UDF (Turkish court documents) 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, XLS, XLT, ODS, CSV, TSV
OTS, FODS, Numbers, ET² | PDF, XLSX, ODS, CSV, TSV, JSON, HWPX, HTML
XLT, OTS² | +| **Documents**: DOCX, DOCM, DOTX, DOC, DOT, ODT, EML, Outlook MSG;
OFD (Chinese e-invoices, official documents), UYAP UDF (Turkish court documents) and XPS / OpenXPS 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 | | **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³ | @@ -181,7 +181,7 @@ To undo it, remove the entries in Windows Security → App & browser control → | **Video**⁵: MP4, MOV, MKV, WEBM, AVI, WMV, FLV, MPEG, Video CD (DAT), TS, M2TS, 3GP, OGV, VOB, …;
Wisenet WAVE / Nx Witness exports (NOV) read | MP4 (H.264, HEVC, AV1), WEBM, MOV, MKV, AVI, GIF, APNG, WEBP, MP3, M4A, 720p, split into parts;
subtitle tracks kept (MKV, MP4, MOV, WebM);
subtitles (SRT, ASS, WebVTT) | | **Audio**⁵: MP3, M4A, AAC, WAV, FLAC, OGG, OPUS, WMA, AIFF, AMR, APE, WavPack, TTA, DSD (DSF, DFF), DTS, AC3, E-AC3, …;
tracker modules (XM, IT, S3M) read | MP3, M4A (AAC, ALAC), M4R (iPhone ringtone), WAV, FLAC, OGG, OPUS, AAC, WavPack, TTA, W64, AC3, E-AC3 | | **Subtitles**: SRT, ASS/SSA, WebVTT, SAMI (SMI), LRC, KRC, TTML, MicroDVD, SCC | each other as UTF-8 (Korean CP949 files read correctly) | -| **Archives**: ZIP, ANSYS Workbench (WBPZ), 7Z, RAR, TAR, TAR.GZ, TAR.BZ2, TAR.XZ, GZ, XZ, ISO, UDF, IMG, DMG, CAB, LHA, ARJ, ALZ, EGG, Garry's Mod addon (GMA), Valve pak (VPK), files packed into a Source map (BSP), …;
JAR, NuGet (NUPKG), Android packages (APKS, XAPK, APKM, AAB), Adobe extensions (ZXP) read | extract, ZIP, 7Z, TAR, TAR.GZ, TAR.XZ, XZ, GMA;
any files → one ZIP; folders → ZIP, ISO (Joliet or UDF 2.01), GMA | +| **Archives**: ZIP, ANSYS Workbench (WBPZ), 7Z, RAR, TAR, TAR.GZ, TAR.BZ2, TAR.XZ, TAR.ZST, GZ, XZ, ZST, ISO, UDF, IMG, DMG, CAB, LHA, ARJ, ALZ, EGG, Garry's Mod addon (GMA), Valve pak (VPK), files packed into a Source map (BSP), …;
JAR, NuGet (NUPKG), Android packages (APKS, XAPK, APKM, AAB), Adobe extensions (ZXP) read | extract, ZIP, 7Z, TAR, TAR.GZ, TAR.XZ, XZ, GMA;
any files → one ZIP; folders → ZIP, ISO (Joliet or UDF 2.01), GMA | | **CAD**: DWG, DXF
Gerber PCB layers (GBR, GTL, GBL, GTO, …) read;
G-code of 3D printers and CNC machines (GCODE, GCO, NGC) read | PDF, SVG, PNG, DWG ↔ DXF;
G-code → toolpath (from above or isometric) or the slicer's preview | | **Fonts**: TTF, OTF, WOFF, WOFF2, EOT;
FON (Windows bitmap fonts) read | each other;
FON → one font per size | | **Data**: JSON, JSON Lines, YAML, TOML, XML
vCard, iCalendar | each other, CSV and XLSX (tables);
vCard, iCalendar ↔ CSV | diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 6a1838b01..7969aedab 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -14,8 +14,8 @@ 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)) | -| Spreadsheets (ExcelDataReader for XLS) | built in (library) | **XLSX, XLS, ODS, CSV, TSV → XLSX, ODS, CSV, TSV** (one CSV / TSV per sheet) | -| **Spreadsheet printing** | built in | **XLSX (+ XLSM/XLTX), 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, 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)) | | Office Open XML | built in | **DOCM / DOTX / DOTM ↔ DOCX, XLSM / XLTX ↔ XLSX, PPTM / POTX / POTM / PPSX ↔ PPTX** (macros removed for macro-free types) | | 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 → HWPX** (and with rhwp → PDF and images); reStructuredText, LaTeX → HWPX with Pandoc | @@ -32,7 +32,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | 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)); 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, 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)); 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) | @@ -41,7 +41,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | **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) → 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), GZ, BZ2, XZ, LZMA, Z, 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 | +| 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 | @@ -833,13 +833,14 @@ which is also the equation's alternative text in HWPX. ## Spreadsheets -`SpreadsheetConverter` (engine id `spreadsheet`) converts between XLSX, XLS (and XLT templates, read only), ODS, CSV and TSV (also +`SpreadsheetConverter` (engine id `spreadsheet`) converts between XLSX, XLSB (read only), XLS (and XLT templates, read only), ODS, CSV and TSV (also `.tab`) in-process; every format goes through one in-memory workbook (`Office/Sheets/Workbook.cs`) whose cells keep the text the spreadsheet shows, the value behind it and its Excel number format. | Format | Read | Written | |---|---|---| | XLSX | own reader: values, number formats, rich text, fonts, fills, borders, alignment, merges, widths, heights, hidden rows / columns / sheets, frozen rows, print titles; for printing the page setup, print areas, page breaks, headers and footers, conditional formats and pictures | every sheet, numbers and dates as numbers with their formats, booleans, styles, merges, widths, heights, hidden rows / columns, frozen rows | +| XLSB (Excel binary workbook) | own reader from [MS-XLSB] (`XlsbReader.cs`): the BIFF12 records of the workbook, shared strings, styles and sheets give what the XLSX reader gives (values, formulas' cached values, number formats, rich text, fonts, fills, borders, alignment, merges, widths, heights, hidden rows / columns / sheets, frozen rows, print areas and titles, page setup, breaks, headers and footers, pictures); conditional formats and comments are not read | no | | XLS (Excel 97–2003, also Excel 5/95) | ExcelDataReader (MIT): values, number formats, merges; fonts, fills, borders and alignment (BIFF8), widths, heights, hidden rows / columns and the page setup from the BIFF records (`XlsLayout.cs`) | no (LibreOffice) | | ODS | own reader (`content.xml`, `styles.xml`, `settings.xml`): repeated rows / columns (the empty repeats up to the end of the sheet are skipped), spans, every value type, the text Calc shows (`text:p`), data styles as Excel codes, cell styles, widths, heights, hidden rows / columns / sheets, frozen and header rows | every sheet, typed cells (float, percentage, currency, date, time, boolean) with data styles and the shown text, styles, merges, widths, heights, hidden rows / columns, frozen rows | | CSV / TSV | delimiter (CSV: comma, semicolon or tab; TSV: tab) and encoding (UTF-8 / UTF-16 with BOM, CP949 when it looks Korean or on Korean Windows, else the PC's code page) | UTF-8 with BOM, CRLF, one file per sheet | @@ -852,8 +853,9 @@ the text the spreadsheet shows, the value behind it and its Excel number format. measures with its own fonts, so a width can differ by up to a fifth after LibreOffice opens the file. - XLS fonts, fills and borders come from the FONT, XF and PALETTE records of BIFF8 files (Excel 97–2003); Excel 5/95 files keep the default look. XLS output still needs LibreOffice. Password-protected XLS and ODS files stop with a - clear message. XLSX and ODS packages are read within `DocumentLimits` (sizes, inflate ratio, XML depth and - elements); ODS repeats expand to at most 20,000,000 cells. + clear message. XLSX, XLSB and ODS packages are read within `DocumentLimits` (sizes, inflate ratio, XML depth and + elements; XLSB records count as elements, a record the reader takes apart holds at most 1 MB); ODS repeats expand + to at most 20,000,000 cells. - Not converted to other spreadsheets: formulas (their cached results are kept), charts, pictures, comments, conditional formatting, data validation, print areas and page breaks (the print engine reads the page setup, print areas, breaks, conditional formats and pictures for PDF). @@ -871,7 +873,7 @@ selected large page. ## Spreadsheet printing -`SheetPrintConverter` (engine id `sheet-print`) prints XLSX (and XLSM / XLTX), XLS (and XLT) and ODS to PDF the way +`SheetPrintConverter` (engine id `sheet-print`) prints XLSX (and XLSM / XLTX), XLSB, XLS (and XLT) and ODS to PDF the way Excel prints a whole workbook, without Excel or LibreOffice (GitHub Project 4, item *Spreadsheet print layout → PDF*). The readers above keep each sheet's page setup; `Office/Sheets/Print` lays the pages out with Excel's page logic and draws them with the Filee renderer's PDF writer (`Rendering/PdfPageWriter`, fonts subset by Filee) and its text @@ -2733,6 +2735,8 @@ its reader, one line in `Drawings/DrawingFormats.cs`: 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). +- **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`). Its edges are `Checked` (`ICapabilityCheck`): a format's check names what its reader doesn't draw, and such a file takes the route it would take without this engine, LibreOffice Draw. That is a binary `.vsd` (Visio 2003–2010, a @@ -2756,6 +2760,55 @@ sections, shadows, rounding, the full arrowhead table, text backgrounds, formula `.vssx` / `.vstx`. The slide model has one size per deck, so pages of other sizes sit at the top left of the largest page's size. Checked against drawings built from the specification, not against files Visio saved. +## XPS and OpenXPS reader + +Built in (#613), in `Xps/`, written from the XPS 1.0 specification and ECMA-388 (OpenXPS); both namespaces are read. +The [drawing engine](#drawings-visio-odg) converts `.xps` and `.oxps` (one format, id `xps`, documents donut) to PDF, +SVG and PNG, page by page: `XpsDocument` reads the package's page list once and each page when it is drawn +(`XpsPageReader` → `XpsPageWriter` → the page model `PageContent`), so pictures of a few pages don't read the rest. +PDF goes through `PdfPageWriter` (fonts subset, text mapped), PNG through `PageContent.Render`, SVG through the slide +output's SVG writer. + +- **Package:** the fixed representation relationship of either spec (else the `.fdseq` part), its FixedDocuments and + their PageContent list, LinkTargets, the title from the core properties. Part names are URIs (`%20`), resolved + against the referring part and never outside the package; interleaved parts (`[0].piece` … `[n].last.piece`) are + joined, and one with a gap is damage. +- **Elements:** Canvas, Path and Glyphs with RenderTransform, Clip, Opacity, OpacityMask (a DstIn layer), + `RenderOptions.EdgeMode`, `FixedPage.NavigateUri` (URL links and links to link targets, which become named + destinations), properties as attributes or property elements, `{StaticResource}` from FixedPage and Canvas + resources (inline or a remote ResourceDictionary part), markup compatibility's AlternateContent (Fallback, else the + first Choice). +- **Geometry:** the abbreviated syntax (F, M, L, H, V, C, Q, S, A, Z, relative forms) and PathGeometry with PathFigure + (IsFilled, IsClosed), PolyLine, PolyBezier, PolyQuadraticBezier and Arc segments (IsStroked), with its Transform. + Strokes: thickness, joins, miter limit, start / dash caps, dash arrays and offsets. +- **Brushes:** SolidColorBrush (`#RRGGBB`, `#AARRGGBB`, scRGB, ContextColor without its ICC profile), + LinearGradientBrush and RadialGradientBrush (spread methods, stops outside 0–1 cut there), ImageBrush (PNG, JPEG, + GIF, BMP by Skia, TIFF by ImageMagick; sized by the picture's resolution, 96 dpi when it has none; `TileMode` none + draws the picture as it is, so a PDF keeps the JPEG) and VisualBrush (untiled: drawn as vector; tiled: a picture + shader, which Skia's PDF writer rasterises). +- **Glyphs:** embedded fonts, obfuscated `.odttf` ones restored (the first 32 bytes XORed with the GUID of the part + name) and read by Filee's SFNT parser before Skia sees them; `Indices` (glyph, advance, offsets, cluster maps), + `UnicodeString` (with the `{}` escape), BidiLevel (odd: right to left), StyleSimulations (bold and italic drawn + synthetically), FontRenderingEmSize, OriginX / OriginY. A run with a solid colour whose clips are rectangles is a + glyph item of the page model (the PDF writer subsets its font and maps its text); others are drawn by + `XpsPainter` as text blobs of the whole font. A run whose font is missing or damaged draws its text with an installed + font. +- **Left out and named in the file's result** (`notice.renderer_left_out` with `.pictures`): JPEG XR (HD Photo) + pictures and pictures that are damaged or too large. Not drawn: IsSideways glyphs are upright, the Triangle cap is + flat, scRGB gradients interpolate in sRGB, ICC profiles aren't applied; print tickets, signatures, document + structure and story fragments aren't read. +- **Hostile input:** parts through `DocumentPackage` (`DocumentLimits`: part and package sizes counted while + unpacking, inflate ratio, XML depth and elements, joined pieces included); `XpsLimits`: pages (`MaxPages`), canvas + and visual brush nesting (64, which also ends a visual brush that draws itself), elements (2 million), points of all + paths and glyphs (40 million), one path's data (32 M characters), dash patterns (1,024 entries, longer ones are + drawn solid), page sides cut at 200 inches; pictures within `MaxPartBytes` and `MaxPicturePixels` (read from the + header before decoding). Refusals are `error.document_too_complex`. `XpsFuzzTests` fuzzes the reader and the + drawing together. +- Tests: `XpsTests` (packages built in code by `XpsBuilder`, checked as pixels at 96 dpi, one pixel per XPS unit, and + as PDF text), `XpsSettingsTests` (settings v105: existing documents donuts learn `.xps` and `.oxps`). Compared by eye + with WPF's own rendering of documents its XPS writer saved (text in Latin, Korean, Japanese, Arabic and Hebrew, + gradients, pictures at 96 and 192 dpi, dashes, transparency, clips, tiled image and visual brushes, opacity masks). + ## Gerber PCB layers Built in (`Filee.Engines/Vector/Gerber`), read only. A Gerber file (RS-274X, Gerber X2 and X3, "The Gerber Layer @@ -3478,11 +3531,12 @@ paths leaving the folder, bombs, entry floods) are final and never retried. Sett | 7Z | Filee (`SevenZipReader`: Copy, LZMA, LZMA2, BCJ, BCJ2, ARM64, ARM, ARMT, PPC, SPARC, IA64, Delta, Deflate, BZip2; solid blocks decoded once, see below); other methods (PPMd, RISC-V …) by 7-Zip when it is installed, else SharpCompress, else the method is named in the "install 7-Zip" error | in-process (Filee's `SevenZipWriter`, LZMA2): UTF-16 names, times, attributes, empty folders | | RAR 4 / 5 | SharpCompress (only unpacks, under the unRAR terms, see THIRD-PARTY-NOTICES), with the other volumes (`.part2.rar`, `.r00`) | — | | TAR | .NET's System.Formats.Tar (ustar, PAX, GNU, V7): tells symbolic from hard links | in-process (System.Formats.Tar, PAX: UTF-8 names, times) | -| TAR.GZ, TAR.BZ2, TAR.XZ, TAR.Z, TAR.LZ, TAR.7Z | in two stages: the outer layer as below, then the TAR | TAR.GZ, TAR.BZ2 and TAR.XZ in-process | +| TAR.GZ, TAR.BZ2, TAR.XZ, TAR.Z, TAR.LZ, TAR.ZST, TAR.7Z | in two stages: the outer layer as below, then the TAR | TAR.GZ, TAR.BZ2 and TAR.XZ in-process | | GZ | .NET's gzip decoder (CRC-32 checked) | in-process (GZipStream); one file only | | BZ2 | SharpCompress | in-process (Filee's `Bzip2Writer`); one file only | | XZ | Filee (`XzReader`) | in-process (Filee's `XzWriter`); one file only | | LZMA (`.lzma`) | Filee (`LzmaRawReader`) | — | +| ZST (Zstandard, #1646) | SharpCompress through `ZstdReader`: frames one after another, skippable frames passed over; not starting with a frame is "not an archive"; windows over 128 MiB (`zstd --long=28` and up) are left to 7-Zip, as the zstd tool needs `--memory` for them | — (rule 5 of [FORMAT-RULES](FORMAT-RULES.md): a new output only developers make waits while that field is on by default) | | Z (Unix compress) | Filee (`UnixCompressStream`: LZW, 9–16-bit codes, block mode with table clears) | — | | LZ (lzip) | Filee: members found from the end, each LZMA stream decoded by SharpCompress, CRC-32 and sizes checked (data longer than the trailer states is refused) | — | | CPIO | Filee (`CpioReader`: newc, crc with its byte sum checked, odc, old binary in either byte order; newc hard link groups become copies) | — | @@ -3583,7 +3637,7 @@ paths leaving the folder, bombs, entry floods) are final and never retried. Sett levels and chunk buffers over 64 MB are refused before DiscUtils reads them, and DiscUtils itself only reads through a window with a budget per call (256 MB, plus the file being copied) and a stack check, so structures that lead in circles end as damage instead of running on. -- **Single compressed streams** (GZ, BZ2, XZ, LZMA, Z, and the first stage of TAR.GZ, TAR.BZ2, TAR.XZ, TAR.Z) state +- **Single compressed streams** (GZ, BZ2, XZ, LZMA, Z, ZST, and the first stage of TAR.GZ, TAR.BZ2, TAR.XZ, TAR.Z, TAR.ZST) state no size (bzip2) or one their author chose (gzip, modulo 4 GiB), so the listing can't be trusted: the in-process decoders stop once one passes the room on the drive less a tenth of the free space (`ArchiveSafety.StreamLimit`, "unpacks to more than …, but only … is free") and delete what was written; when 7-Zip decodes one (the fallback), @@ -3776,7 +3830,7 @@ 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); Apple + (`.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 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 diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index adb5532da..b190e5a2d 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "حُذف عنصر تحكم نموذج واحد", "notice.renderer_left_out.videos": "مقاطع فيديو محذوفة: {0}", "notice.renderer_left_out.videos_one": "حُذف مقطع فيديو واحد", + "notice.renderer_left_out.pictures": "صور محذوفة: {0}", + "notice.renderer_left_out.pictures_one": "حُذفت صورة واحدة", "notice.renderer_left_out.objects_pictured": "كائنات مضمّنة رُسمت من صورها المحفوظة: {0}", "notice.renderer_left_out.objects_pictured_one": "رُسم كائن مضمّن واحد من صورته المحفوظة", "notice.renderer_left_out.formulas": "صيغ كُتبت نصًا عاديًا: {0}", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (built-in)", "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لا تزال ملفات Visio الثنائية القديمة (.vsd) تحتاج إلى LibreOffice.", + "engine.drawing.description": "تحويل رسومات Visio (VSDX) ورسومات OpenDocument (ODG وFODG) إلى PDF وSVG وPNG دون Visio أو LibreOffice.\nوكذلك مستندات XPS وOpenXPS (.xps و.oxps).\nلا تزال ملفات Visio الثنائية القديمة (.vsd) تحتاج إلى LibreOffice.", "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 747731f1d..c5989b448 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -260,6 +260,8 @@ "notice.renderer_left_out.form_controls_one": "1টি ফর্ম কন্ট্রোল বাদ দেওয়া হয়েছে", "notice.renderer_left_out.videos": "{0}টি ভিডিও বাদ দেওয়া হয়েছে", "notice.renderer_left_out.videos_one": "1টি ভিডিও বাদ দেওয়া হয়েছে", + "notice.renderer_left_out.pictures": "{0}টি ছবি বাদ দেওয়া হয়েছে", + "notice.renderer_left_out.pictures_one": "1টি ছবি বাদ দেওয়া হয়েছে", "notice.renderer_left_out.objects_pictured": "{0}টি এমবেড করা অবজেক্ট তাদের সংরক্ষিত ছবি থেকে আঁকা হয়েছে", "notice.renderer_left_out.objects_pictured_one": "1টি এমবেড করা অবজেক্ট তার সংরক্ষিত ছবি থেকে আঁকা হয়েছে", "notice.renderer_left_out.formulas": "{0}টি সূত্র সাধারণ লেখা হিসেবে লেখা হয়েছে", @@ -328,7 +330,7 @@ "engine.gcode.name": "G-code (বিল্ট-ইন)", "engine.gcode.description": "3D প্রিন্টার ও CNC মেশিনের টুলপাথ (G-code) PDF, SVG ও ছবিতে, অথবা স্লাইসারের নিজের প্রিভিউ।", "engine.drawing.name": "ড্রয়িং (বিল্ট-ইন)", - "engine.drawing.description": "Visio ড্রয়িং (VSDX) ও OpenDocument ড্রয়িং (ODG, FODG) থেকে পিডিএফ, SVG ও PNG, Visio বা LibreOffice ছাড়াই।\nপুরোনো বাইনারি Visio ফাইলের (.vsd) জন্য এখনও LibreOffice লাগে।", + "engine.drawing.description": "Visio ড্রয়িং (VSDX) ও OpenDocument ড্রয়িং (ODG, FODG) থেকে পিডিএফ, SVG ও PNG, Visio বা LibreOffice ছাড়াই।\nXPS ও OpenXPS ডকুমেন্টও (.xps, .oxps)।\nপুরোনো বাইনারি Visio ফাইলের (.vsd) জন্য এখনও LibreOffice লাগে।", "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 018f41739..c36f41e20 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "1 Formularsteuerelement weggelassen", "notice.renderer_left_out.videos": "{0} Videos weggelassen", "notice.renderer_left_out.videos_one": "1 Video weggelassen", + "notice.renderer_left_out.pictures": "{0} Bilder weggelassen", + "notice.renderer_left_out.pictures_one": "1 Bild weggelassen", "notice.renderer_left_out.objects_pictured": "{0} eingebettete Objekte aus ihrem gespeicherten Bild gezeichnet", "notice.renderer_left_out.objects_pictured_one": "1 eingebettetes Objekt aus seinem gespeicherten Bild gezeichnet", "notice.renderer_left_out.formulas": "{0} Formeln als einfacher Text geschrieben", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (integriert)", "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.\nFür alte binäre Visio-Dateien (.vsd) ist weiterhin LibreOffice nötig.", + "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).\nFür alte binäre Visio-Dateien (.vsd) ist weiterhin LibreOffice nötig.", "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 7b4914fa4..b37832e4a 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "1 form control left out", "notice.renderer_left_out.videos": "{0} videos left out", "notice.renderer_left_out.videos_one": "1 video left out", + "notice.renderer_left_out.pictures": "{0} pictures left out", + "notice.renderer_left_out.pictures_one": "1 picture left out", "notice.renderer_left_out.objects_pictured": "{0} embedded objects drawn from their saved pictures", "notice.renderer_left_out.objects_pictured_one": "1 embedded object drawn from its saved picture", "notice.renderer_left_out.formulas": "{0} formulas written as plain text", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (built-in)", "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.\nOld binary Visio files (.vsd) still need LibreOffice.", + "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).\nOld binary Visio files (.vsd) still need LibreOffice.", "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 e1add24a7..08816e86e 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "1 control de formulario omitido", "notice.renderer_left_out.videos": "{0} vídeos omitidos", "notice.renderer_left_out.videos_one": "1 vídeo omitido", + "notice.renderer_left_out.pictures": "{0} imágenes omitidas", + "notice.renderer_left_out.pictures_one": "1 imagen omitida", "notice.renderer_left_out.objects_pictured": "{0} objetos incrustados dibujados a partir de su imagen guardada", "notice.renderer_left_out.objects_pictured_one": "1 objeto incrustado dibujado a partir de su imagen guardada", "notice.renderer_left_out.formulas": "{0} fórmulas escritas como texto sin formato", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (built-in)", "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.\nLos archivos binarios antiguos de Visio (.vsd) siguen necesitando LibreOffice.", + "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).\nLos archivos binarios antiguos de Visio (.vsd) siguen necesitando LibreOffice.", "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 f9d12bfef..c109d2bf6 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -260,6 +260,8 @@ "notice.renderer_left_out.form_controls_one": "یک کنترل فرم حذف شد", "notice.renderer_left_out.videos": "{0} ویدیو حذف شد", "notice.renderer_left_out.videos_one": "یک ویدیو حذف شد", + "notice.renderer_left_out.pictures": "{0} تصویر حذف شد", + "notice.renderer_left_out.pictures_one": "یک تصویر حذف شد", "notice.renderer_left_out.objects_pictured": "{0} شیء جاسازی‌شده از تصویر ذخیره‌شده‌اش رسم شد", "notice.renderer_left_out.objects_pictured_one": "یک شیء جاسازی‌شده از تصویر ذخیره‌شده‌اش رسم شد", "notice.renderer_left_out.formulas": "{0} فرمول به‌صورت متن ساده نوشته شد", @@ -328,7 +330,7 @@ "engine.gcode.name": "G-code (داخلی)", "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فایل‌های باینری قدیمی Visio (.vsd) همچنان به LibreOffice نیاز دارند.", + "engine.drawing.description": "نمودارهای Visio (VSDX) و نقشه‌های OpenDocument (ODG، FODG) به PDF، SVG و PNG، بدون Visio یا LibreOffice.\nهمچنین سندهای XPS و OpenXPS (.xps، .oxps).\nفایل‌های باینری قدیمی Visio (.vsd) همچنان به LibreOffice نیاز دارند.", "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 53a5a241e..2e3d07f7e 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "1 contrôle de formulaire omis", "notice.renderer_left_out.videos": "{0} vidéos omises", "notice.renderer_left_out.videos_one": "1 vidéo omise", + "notice.renderer_left_out.pictures": "{0} images omises", + "notice.renderer_left_out.pictures_one": "1 image omise", "notice.renderer_left_out.objects_pictured": "{0} objets incorporés dessinés d'après leur image enregistrée", "notice.renderer_left_out.objects_pictured_one": "1 objet incorporé dessiné d'après son image enregistrée", "notice.renderer_left_out.formulas": "{0} formules écrites en texte brut", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (built-in)", "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.\nLes anciens fichiers Visio binaires (.vsd) nécessitent toujours LibreOffice.", + "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).\nLes anciens fichiers Visio binaires (.vsd) nécessitent toujours LibreOffice.", "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 a5b41b416..ef80cd11b 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "1 फ़ॉर्म कंट्रोल छोड़ दिया गया", "notice.renderer_left_out.videos": "{0} वीडियो छोड़ दिए गए", "notice.renderer_left_out.videos_one": "1 वीडियो छोड़ दिया गया", + "notice.renderer_left_out.pictures": "{0} चित्र छोड़ दिए गए", + "notice.renderer_left_out.pictures_one": "1 चित्र छोड़ दिया गया", "notice.renderer_left_out.objects_pictured": "{0} एम्बेड किए गए ऑब्जेक्ट उनकी सहेजी गई तस्वीर से बनाए गए", "notice.renderer_left_out.objects_pictured_one": "1 एम्बेड किया गया ऑब्जेक्ट उसकी सहेजी गई तस्वीर से बनाया गया", "notice.renderer_left_out.formulas": "{0} सूत्र सादे टेक्स्ट के रूप में लिखे गए", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (built-in)", "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पुरानी बाइनरी Visio फ़ाइलों (.vsd) के लिए अब भी LibreOffice चाहिए।", + "engine.drawing.description": "Visio ड्रॉइंग (VSDX) और OpenDocument ड्रॉइंग (ODG, FODG) से PDF, SVG और PNG, बिना Visio या LibreOffice के।\nXPS और OpenXPS दस्तावेज़ (.xps, .oxps) भी।\nपुरानी बाइनरी Visio फ़ाइलों (.vsd) के लिए अब भी LibreOffice चाहिए।", "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 9fa323f6c..9332026c8 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "1 kontrol formulir tidak disertakan", "notice.renderer_left_out.videos": "{0} video tidak disertakan", "notice.renderer_left_out.videos_one": "1 video tidak disertakan", + "notice.renderer_left_out.pictures": "{0} gambar tidak disertakan", + "notice.renderer_left_out.pictures_one": "1 gambar tidak disertakan", "notice.renderer_left_out.objects_pictured": "{0} objek tersemat digambar dari gambar tersimpannya", "notice.renderer_left_out.objects_pictured_one": "1 objek tersemat digambar dari gambar tersimpannya", "notice.renderer_left_out.formulas": "{0} rumus ditulis sebagai teks biasa", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (bawaan)", "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.\nFile Visio biner lama (.vsd) masih memerlukan LibreOffice.", + "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).\nFile Visio biner lama (.vsd) masih memerlukan LibreOffice.", "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 bd2145de7..e1cf5b01e 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -245,6 +245,8 @@ "notice.renderer_left_out.form_controls_one": "1 controllo modulo omesso", "notice.renderer_left_out.videos": "{0} video omessi", "notice.renderer_left_out.videos_one": "1 video omesso", +"notice.renderer_left_out.pictures": "{0} immagini omesse", +"notice.renderer_left_out.pictures_one": "1 immagine omessa", "notice.renderer_left_out.objects_pictured": "{0} oggetti incorporati disegnati dalla loro immagine salvata", "notice.renderer_left_out.objects_pictured_one": "1 oggetto incorporato disegnato dalla sua immagine salvata", "notice.renderer_left_out.formulas": "{0} formule scritte come testo semplice", @@ -313,7 +315,7 @@ "engine.gcode.name": "G-code (integrato)", "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.\nI vecchi file binari di Visio (.vsd) richiedono ancora LibreOffice.", +"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).\nI vecchi file binari di Visio (.vsd) richiedono ancora LibreOffice.", "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 e73378a70..657dc177a 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "フォームコントロール 1 個を省略", "notice.renderer_left_out.videos": "動画 {0} 本を省略", "notice.renderer_left_out.videos_one": "動画 1 本を省略", + "notice.renderer_left_out.pictures": "画像 {0} 枚を省略", + "notice.renderer_left_out.pictures_one": "画像 1 枚を省略", "notice.renderer_left_out.objects_pictured": "埋め込みオブジェクト {0} 個を保存済みの画像で描画", "notice.renderer_left_out.objects_pictured_one": "埋め込みオブジェクト 1 個を保存済みの画像で描画", "notice.renderer_left_out.formulas": "数式 {0} 個をプレーンテキストで表示", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (built-in)", "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に変換します。\n古いバイナリ形式のVisioファイル(.vsd)には引き続きLibreOfficeが必要です。", + "engine.drawing.description": "Visio図面(VSDX)とOpenDocument図面(ODG、FODG)を、VisioやLibreOfficeなしでPDF、SVG、PNGに変換します。\nXPS・OpenXPS文書(.xps、.oxps)にも対応しています。\n古いバイナリ形式のVisioファイル(.vsd)には引き続きLibreOfficeが必要です。", "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 d982b93bc..9308d6913 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "양식 컨트롤 1개 빠짐", "notice.renderer_left_out.videos": "동영상 {0}개 빠짐", "notice.renderer_left_out.videos_one": "동영상 1개 빠짐", + "notice.renderer_left_out.pictures": "그림 {0}개 빠짐", + "notice.renderer_left_out.pictures_one": "그림 1개 빠짐", "notice.renderer_left_out.objects_pictured": "삽입된 개체 {0}개는 저장된 그림으로 그림", "notice.renderer_left_out.objects_pictured_one": "삽입된 개체 1개는 저장된 그림으로 그림", "notice.renderer_left_out.formulas": "수식 {0}개는 일반 글자로 씀", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (built-in)", "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로 바꿔요.\n오래된 바이너리 Visio 파일(.vsd)은 여전히 LibreOffice가 필요해요.", + "engine.drawing.description": "Visio 도면(VSDX)과 오픈도큐먼트 도면(ODG, FODG)을 Visio나 LibreOffice 없이 PDF, SVG, PNG로 바꿔요.\nXPS·OpenXPS 문서(.xps, .oxps)도 바꿔요.\n오래된 바이너리 Visio 파일(.vsd)은 여전히 LibreOffice가 필요해요.", "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 361f66c50..1558d49fc 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -245,6 +245,8 @@ "notice.renderer_left_out.form_controls_one": "1 маягтын удирдлага орхигдлоо", "notice.renderer_left_out.videos": "{0} видео орхигдлоо", "notice.renderer_left_out.videos_one": "1 видео орхигдлоо", + "notice.renderer_left_out.pictures": "{0} зураг орхигдлоо", + "notice.renderer_left_out.pictures_one": "1 зураг орхигдлоо", "notice.renderer_left_out.objects_pictured": "{0} шигтгэсэн объектыг хадгалсан зургаас нь зурлаа", "notice.renderer_left_out.objects_pictured_one": "1 шигтгэсэн объектыг хадгалсан зургаас нь зурлаа", "notice.renderer_left_out.formulas": "{0} томьёог энгийн текстээр бичлээ", @@ -313,7 +315,7 @@ "engine.gcode.name": "G-code (суурилагдсан)", "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Хуучин хоёртын Visio файлд (.vsd) LibreOffice хэрэгтэй хэвээр.", + "engine.drawing.description": "Visio зураг (VSDX) болон OpenDocument зургийг (ODG, FODG) Visio эсвэл LibreOffice-гүйгээр PDF, SVG болон PNG болгоно.\nМөн XPS болон OpenXPS баримт бичиг (.xps, .oxps).\nХуучин хоёртын Visio файлд (.vsd) LibreOffice хэрэгтэй хэвээр.", "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 68de61aca..e4887eada 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "1 फारम नियन्त्रण छोडियो", "notice.renderer_left_out.videos": "{0} भिडियो छोडिए", "notice.renderer_left_out.videos_one": "1 भिडियो छोडियो", + "notice.renderer_left_out.pictures": "{0} चित्र छोडिए", + "notice.renderer_left_out.pictures_one": "1 चित्र छोडियो", "notice.renderer_left_out.objects_pictured": "{0} सम्मिलित वस्तु तिनको सुरक्षित चित्रबाट कोरिए", "notice.renderer_left_out.objects_pictured_one": "1 सम्मिलित वस्तु यसको सुरक्षित चित्रबाट कोरियो", "notice.renderer_left_out.formulas": "{0} सूत्र सादा पाठका रूपमा लेखिए", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (built-in)", "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पुराना बाइनरी Visio फाइलहरू (.vsd) का लागि अझै LibreOffice चाहिन्छ।", + "engine.drawing.description": "Visio रेखाचित्र (VSDX) र OpenDocument रेखाचित्र (ODG, FODG) बाट PDF, SVG र PNG, Visio वा LibreOffice बिना।\nXPS र OpenXPS कागजातहरू (.xps, .oxps) पनि।\nपुराना बाइनरी Visio फाइलहरू (.vsd) का लागि अझै LibreOffice चाहिन्छ।", "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 e109dfee5..407170ba7 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "pominięta 1 kontrolka formularza", "notice.renderer_left_out.videos": "pominięte filmy: {0}", "notice.renderer_left_out.videos_one": "pominięty 1 film", + "notice.renderer_left_out.pictures": "pominięte obrazy: {0}", + "notice.renderer_left_out.pictures_one": "pominięty 1 obraz", "notice.renderer_left_out.objects_pictured": "obiekty osadzone narysowane z zapisanych obrazów: {0}", "notice.renderer_left_out.objects_pictured_one": "1 obiekt osadzony narysowany z zapisanego obrazu", "notice.renderer_left_out.formulas": "formuły zapisane zwykłym tekstem: {0}", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (built-in)", "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.\nStare binarne pliki Visio (.vsd) nadal wymagają LibreOffice.", + "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).\nStare binarne pliki Visio (.vsd) nadal wymagają LibreOffice.", "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 cfe7fd8cb..6fe00b692 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "1 controle de formulário deixado de fora", "notice.renderer_left_out.videos": "{0} vídeos deixados de fora", "notice.renderer_left_out.videos_one": "1 vídeo deixado de fora", + "notice.renderer_left_out.pictures": "{0} imagens deixadas de fora", + "notice.renderer_left_out.pictures_one": "1 imagem deixada de fora", "notice.renderer_left_out.objects_pictured": "{0} objetos incorporados desenhados a partir da imagem salva", "notice.renderer_left_out.objects_pictured_one": "1 objeto incorporado desenhado a partir da imagem salva", "notice.renderer_left_out.formulas": "{0} fórmulas escritas como texto simples", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (mecanismo integrado)", "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.\nArquivos binários antigos do Visio (.vsd) ainda precisam do LibreOffice.", + "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).\nArquivos binários antigos do Visio (.vsd) ainda precisam do LibreOffice.", "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 56dfa97d5..42ecf92e7 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "1 controlo de formulário omitido", "notice.renderer_left_out.videos": "{0} vídeos omitidos", "notice.renderer_left_out.videos_one": "1 vídeo omitido", + "notice.renderer_left_out.pictures": "{0} imagens omitidas", + "notice.renderer_left_out.pictures_one": "1 imagem omitida", "notice.renderer_left_out.objects_pictured": "{0} objetos incorporados desenhados a partir da imagem guardada", "notice.renderer_left_out.objects_pictured_one": "1 objeto incorporado desenhado a partir da imagem guardada", "notice.renderer_left_out.formulas": "{0} fórmulas escritas como texto simples", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (motor integrado)", "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.\nOs ficheiros binários antigos do Visio (.vsd) continuam a precisar do LibreOffice.", + "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).\nOs ficheiros binários antigos do Visio (.vsd) continuam a precisar do LibreOffice.", "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 5bdefe355..1c05484fd 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "пропущен 1 элемент управления формы", "notice.renderer_left_out.videos": "пропущено видео: {0}", "notice.renderer_left_out.videos_one": "пропущено 1 видео", + "notice.renderer_left_out.pictures": "пропущено изображений: {0}", + "notice.renderer_left_out.pictures_one": "пропущено 1 изображение", "notice.renderer_left_out.objects_pictured": "внедрённых объектов нарисовано по сохранённым изображениям: {0}", "notice.renderer_left_out.objects_pictured_one": "1 внедрённый объект нарисован по сохранённому изображению", "notice.renderer_left_out.formulas": "формул записано обычным текстом: {0}", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (built-in)", "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Для старых двоичных файлов Visio (.vsd) по-прежнему нужен LibreOffice.", + "engine.drawing.description": "Схемы Visio (VSDX) и рисунки OpenDocument (ODG, FODG) — в PDF, SVG и PNG без Visio и LibreOffice.\nА также документы XPS и OpenXPS (.xps, .oxps).\nДля старых двоичных файлов Visio (.vsd) по-прежнему нужен LibreOffice.", "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 3d897be04..00c42fb13 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "ละตัวควบคุมฟอร์ม 1 รายการ", "notice.renderer_left_out.videos": "ละวิดีโอ {0} รายการ", "notice.renderer_left_out.videos_one": "ละวิดีโอ 1 รายการ", + "notice.renderer_left_out.pictures": "ละรูปภาพ {0} รายการ", + "notice.renderer_left_out.pictures_one": "ละรูปภาพ 1 รายการ", "notice.renderer_left_out.objects_pictured": "วาดวัตถุที่ฝังไว้ {0} รายการจากรูปที่บันทึกไว้", "notice.renderer_left_out.objects_pictured_one": "วาดวัตถุที่ฝังไว้ 1 รายการจากรูปที่บันทึกไว้", "notice.renderer_left_out.formulas": "เขียนสูตร {0} รายการเป็นข้อความธรรมดา", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (built-in)", "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ไฟล์ Visio แบบไบนารีรุ่นเก่า (.vsd) ยังต้องใช้ LibreOffice", + "engine.drawing.description": "แปลงไดอะแกรม Visio (VSDX) และภาพวาด OpenDocument (ODG, FODG) เป็น PDF, SVG และ PNG โดยไม่ต้องใช้ Visio หรือ LibreOffice\nรวมถึงเอกสาร XPS และ OpenXPS (.xps, .oxps)\nไฟล์ Visio แบบไบนารีรุ่นเก่า (.vsd) ยังต้องใช้ LibreOffice", "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 513729ab1..99a46de17 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -246,6 +246,8 @@ "notice.renderer_left_out.form_controls_one": "1 form denetimi çıkarıldı", "notice.renderer_left_out.videos": "{0} video çıkarıldı", "notice.renderer_left_out.videos_one": "1 video çıkarıldı", + "notice.renderer_left_out.pictures": "{0} resim çıkarıldı", + "notice.renderer_left_out.pictures_one": "1 resim çıkarıldı", "notice.renderer_left_out.objects_pictured": "{0} gömülü nesne kayıtlı resminden çizildi", "notice.renderer_left_out.objects_pictured_one": "1 gömülü nesne kayıtlı resminden çizildi", "notice.renderer_left_out.formulas": "{0} formül düz metin olarak yazıldı", @@ -314,7 +316,7 @@ "engine.gcode.name": "G-code (yerleşik)", "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.\nEski ikili Visio dosyaları (.vsd) için hâlâ LibreOffice gerekir.", + "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.\nEski ikili Visio dosyaları (.vsd) için hâlâ LibreOffice gerekir.", "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 c2dc87ecd..22ff52c33 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "bỏ qua 1 điều khiển biểu mẫu", "notice.renderer_left_out.videos": "bỏ qua {0} video", "notice.renderer_left_out.videos_one": "bỏ qua 1 video", + "notice.renderer_left_out.pictures": "bỏ qua {0} hình ảnh", + "notice.renderer_left_out.pictures_one": "bỏ qua 1 hình ảnh", "notice.renderer_left_out.objects_pictured": "{0} đối tượng nhúng được vẽ từ ảnh đã lưu của chúng", "notice.renderer_left_out.objects_pictured_one": "1 đối tượng nhúng được vẽ từ ảnh đã lưu của nó", "notice.renderer_left_out.formulas": "{0} công thức được viết dưới dạng văn bản thường", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (tích hợp)", "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.\nTệp Visio nhị phân cũ (.vsd) vẫn cần LibreOffice.", + "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).\nTệp Visio nhị phân cũ (.vsd) vẫn cần LibreOffice.", "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 1289411e3..c69d574b9 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "省略了 1 个表单控件", "notice.renderer_left_out.videos": "省略了 {0} 个视频", "notice.renderer_left_out.videos_one": "省略了 1 个视频", + "notice.renderer_left_out.pictures": "省略了 {0} 张图片", + "notice.renderer_left_out.pictures_one": "省略了 1 张图片", "notice.renderer_left_out.objects_pictured": "{0} 个嵌入对象按其保存的图片绘制", "notice.renderer_left_out.objects_pictured_one": "1 个嵌入对象按其保存的图片绘制", "notice.renderer_left_out.formulas": "{0} 个公式以纯文本显示", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (built-in)", "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旧的二进制 Visio 文件(.vsd)仍需 LibreOffice。", + "engine.drawing.description": "将 Visio 图表(VSDX)和 OpenDocument 绘图(ODG、FODG)转为 PDF、SVG 和 PNG,无需 Visio 或 LibreOffice。\n也支持 XPS 和 OpenXPS 文档(.xps、.oxps)。\n旧的二进制 Visio 文件(.vsd)仍需 LibreOffice。", "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 1ff2fe25c..13efccba7 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -252,6 +252,8 @@ "notice.renderer_left_out.form_controls_one": "略過了 1 個表單控制項", "notice.renderer_left_out.videos": "略過了 {0} 個影片", "notice.renderer_left_out.videos_one": "略過了 1 個影片", + "notice.renderer_left_out.pictures": "略過了 {0} 張圖片", + "notice.renderer_left_out.pictures_one": "略過了 1 張圖片", "notice.renderer_left_out.objects_pictured": "{0} 個內嵌物件以其儲存的圖片繪製", "notice.renderer_left_out.objects_pictured_one": "1 個內嵌物件以其儲存的圖片繪製", "notice.renderer_left_out.formulas": "{0} 個公式以純文字顯示", @@ -321,7 +323,7 @@ "engine.gcode.name": "G-code (built-in)", "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舊的二進位 Visio 檔案(.vsd)仍需 LibreOffice。", + "engine.drawing.description": "將 Visio 圖表(VSDX)和 OpenDocument 繪圖(ODG、FODG)轉為 PDF、SVG 和 PNG,不需 Visio 或 LibreOffice。\n也支援 XPS 和 OpenXPS 文件(.xps、.oxps)。\n舊的二進位 Visio 檔案(.vsd)仍需 LibreOffice。", "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 e4d7393f6..fa3b90d5e 100644 --- a/src/Filee.Core/Formats/FormatFields.cs +++ b/src/Filee.Core/Formats/FormatFields.cs @@ -65,8 +65,8 @@ .. 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", - "docm", "dotx", "dotm", "doc", "odt", "ott", "rtf", "wps", "ofd", "udf", "abw", "pages", "pub", "eml", "msg", - "p7m", "xlsm", "xltx", "xls", "ods", "ots", "tsv", "et", "numbers", "pptm", "potx", "potm", "ppsx", "ppt", + "docm", "dotx", "dotm", "doc", "odt", "ott", "rtf", "wps", "ofd", "udf", "xps", "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", "m2ts", "ogv", "m4b", "m4r", "aac", "flac", "ogg", "opus", "wma", "aiff", "weba", "mka", "qta", "7z", "rar", @@ -80,8 +80,8 @@ .. Enum.GetValues().Select(category => category.ToString().ToLow "fodt", "fods", "fodp", "rst", "tex", "org", "adoc", "typ", "mediawiki", "dbk", "ipynb", "textile", "man", "jsonl", "yaml", "toml", "har", "plist", "htmlz", "txtz", "chm", "dv", "mxf", "cavs", "usm", "m4s", "dav", "nov", "bik", "h265", "h264", "ac3", "caf", "mp2", "dss", "wv", "tta", "w64", "eac3", "ape", "tak", "dsf", "dff", - "dts", "tar", "tgz", "tbz2", "txz", "tar7z", "tlz", "gz", "bz2", "xz", "lz", "lzma", "jar", "zxp", "nupkg", "apks", - "aab", "msix", "mcpack", "mcaddon", "mcworld", "mctemplate", "mrpack", "cab", "gerber", + "dts", "tar", "tgz", "tbz2", "txz", "tar7z", "tlz", "tzst", "gz", "bz2", "xz", "lz", "lzma", "zst", "jar", "zxp", + "nupkg", "apks", "aab", "msix", "mcpack", "mcaddon", "mcworld", "mctemplate", "mrpack", "cab", "gerber", "cpio", "deb", "rpm", "dwg", "ttf", "otf", "woff", "woff2", "ass", "ssa", "ttml", "scc", "glb", "gltf", "stl", "obj", "3mf", "ply", "fbx", "gcode", "geojson", "kml", "kmz", "gpx", "shp", "fit", "osm", "xm", "it", "ani", "vtf", "gma", "vpk", "bsp" ]), @@ -107,8 +107,8 @@ .. Enum.GetValues().Select(category => category.ToString().ToLow // ODF for version control, man pages, streaming segments, packages and compressed archives. ([ "ppm", "icns", "qoi", "cur", "ani", "xpm", "xbm", "pam", "pfm", "miff", "fodg", "fodt", "fods", "fodp", "man", - "jsonl", "yaml", "toml", "har", "plist", "m4s", "tar", "tgz", "tbz2", "txz", "tar7z", "tlz", "gz", "bz2", "xz", - "lz", "lzma", "jar", "zxp", "nupkg", "apks", "aab", "msix", "cab", "cpio", "deb", "rpm" + "jsonl", "yaml", "toml", "har", "plist", "m4s", "tar", "tgz", "tbz2", "txz", "tar7z", "tlz", "tzst", "gz", "bz2", + "xz", "lz", "lzma", "zst", "jar", "zxp", "nupkg", "apks", "aab", "msix", "cab", "cpio", "deb", "rpm" ], ["developer"]), // Markup sources, papers and outlines, maintenance-doc drawings (CGM), Calibre's ebook formats. (["cgm", "rst", "tex", "org", "adoc", "typ", "mediawiki", "dbk", "textile", "htmlz", "txtz"], ["writing"]), diff --git a/src/Filee.Core/Formats/FormatRegistry.cs b/src/Filee.Core/Formats/FormatRegistry.cs index b4c89b00f..c78c4e2d7 100644 --- a/src/Filee.Core/Formats/FormatRegistry.cs +++ b/src/Filee.Core/Formats/FormatRegistry.cs @@ -119,6 +119,8 @@ public static class FormatRegistry // UYAP Document Editor files (Turkish courts and lawyers, #1338), read by the built-in UYAP reader. ".udf" is also // a Universal Disk Format disc image: told apart by content, such a file unpacks with Extract (archive engine). 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), new("wpd", "WPD", FormatCategory.Document, ["wpd"], Writable: false), new("lwp", "LWP", FormatCategory.Document, ["lwp"], Writable: false), new("abw", "ABW", FormatCategory.Document, ["abw", "zabw"], Writable: false), @@ -143,6 +145,8 @@ public static class FormatRegistry new("xlsx", "XLSX", FormatCategory.Spreadsheet, ["xlsx"]), new("xlsm", "XLSM", FormatCategory.Spreadsheet, ["xlsm"]), new("xltx", "XLTX", FormatCategory.Spreadsheet, ["xltx"]), + // Excel binary workbooks, read by the built-in XLSB reader (#1535). + new("xlsb", "XLSB", FormatCategory.Spreadsheet, ["xlsb"], Writable: false), new("xls", "XLS", FormatCategory.Spreadsheet, ["xls"]), new("xlt", "XLT", FormatCategory.Spreadsheet, ["xlt"]), new("ods", "ODS", FormatCategory.Spreadsheet, ["ods"]), @@ -335,12 +339,16 @@ public static class FormatRegistry new("tar7z", "TAR.7Z", FormatCategory.Archive, ["tar.7z"], Writable: false), new("tlz", "TAR.LZ", FormatCategory.Archive, ["tar.lz", "tlz"], Writable: false), new("tz", "TAR.Z", FormatCategory.Archive, ["tar.z", "tz"], Writable: false), + // Zstandard (#1646): read only. Writing would add a developer-only output, which docs/FORMAT-RULES.md rule 5 + // keeps out while the developer field is on by default. + new("tzst", "TAR.ZST", FormatCategory.Archive, ["tar.zst", "tzst"], Writable: false), new("gz", "GZ", FormatCategory.Archive, ["gz"]), new("bz2", "BZ2", FormatCategory.Archive, ["bz2", "bz"]), new("xz", "XZ", FormatCategory.Archive, ["xz"]), new("lz", "LZ", FormatCategory.Archive, ["lz"], Writable: false), new("lzma", "LZMA", FormatCategory.Archive, ["lzma"], Writable: false), new("z", "Z", FormatCategory.Archive, ["z"], Writable: false), + new("zst", "ZST", FormatCategory.Archive, ["zst"], Writable: false), new("jar", "JAR", FormatCategory.Archive, ["jar"], Writable: false), // Adobe extension packages for After Effects, Premiere Pro, Photoshop … (#1382): signed ZIPs. Never a target, // since installing one needs the signature its developer makes with Adobe's ZXPSignCmd. diff --git a/src/Filee.Core/Settings/AppSettings.cs b/src/Filee.Core/Settings/AppSettings.cs index b591d04c8..e9007b535 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 = 103; + public const int CurrentSchemaVersion = 106; public int SchemaVersion { get; set; } = CurrentSchemaVersion; diff --git a/src/Filee.Core/Settings/UserDataStore.cs b/src/Filee.Core/Settings/UserDataStore.cs index 1a16a004e..c8fe19b25 100644 --- a/src/Filee.Core/Settings/UserDataStore.cs +++ b/src/Filee.Core/Settings/UserDataStore.cs @@ -1221,6 +1221,15 @@ public static bool ApplyToLibrary(int from, List presets, ListCompressed single files (no names or folders inside); they convert among each other. - private static readonly string[] SingleFileSources = ["gz", "bz2", "xz", "lzma", "z", "lz"]; + private static readonly string[] SingleFileSources = ["gz", "bz2", "xz", "lzma", "z", "lz", "zst"]; private static readonly IReadOnlyList AllEdges = CreateEdges(); diff --git a/src/Filee.Engines/Archives/ArchiveExtractor.cs b/src/Filee.Engines/Archives/ArchiveExtractor.cs index 7e2a2e62d..6513af2c2 100644 --- a/src/Filee.Engines/Archives/ArchiveExtractor.cs +++ b/src/Filee.Engines/Archives/ArchiveExtractor.cs @@ -39,7 +39,7 @@ internal sealed class ArchiveExtractor(SevenZip? sevenZip, Func? s public static readonly IReadOnlyList BuiltInFormats = [ "zip", "jar", "zxp", "nupkg", "apks", "aab", "msix", "7z", "rar", "tar", "tgz", "tbz2", "txz", "tar7z", "tz", "tlz", - "gz", "bz2", "xz", "lzma", "z", "lz", "cpio", "deb", "rpm", "iso", "alz", "egg", "split", "cab", "lha", "arj", "dmg", + "tzst", "gz", "bz2", "xz", "lzma", "z", "lz", "zst", "cpio", "deb", "rpm", "iso", "alz", "egg", "split", "cab", "lha", "arj", "dmg", "img", "gma", "vpk", "bsp", "mcpack", "mcaddon", "mcworld", "mctemplate", "mrpack", ]; @@ -47,19 +47,19 @@ internal sealed class ArchiveExtractor(SevenZip? sevenZip, Func? s /// Formats 7-Zip can read too, so it may try again after an in-process reader gave up. private static readonly HashSet SevenZipAlsoReads = [ - "zip", "jar", "zxp", "nupkg", "apks", "aab", "msix", "7z", "rar", "tar", "tgz", "tbz2", "txz", "tar7z", "tz", "gz", - "bz2", "xz", "lzma", "z", "cpio", "deb", "rpm", "iso", "split", "cab", "lha", "arj", "dmg", "img", "mcpack", + "zip", "jar", "zxp", "nupkg", "apks", "aab", "msix", "7z", "rar", "tar", "tgz", "tbz2", "txz", "tar7z", "tz", "tzst", + "gz", "bz2", "xz", "lzma", "z", "zst", "cpio", "deb", "rpm", "iso", "split", "cab", "lha", "arj", "dmg", "img", "mcpack", "mcaddon", "mcworld", "mctemplate", "mrpack", ]; /// A TAR inside a compressed stream (or a 7z): unpacking the outer layer yields one .tar file. - private static readonly HashSet TarWrapped = ["tgz", "tbz2", "txz", "tar7z", "tz", "tlz"]; + private static readonly HashSet TarWrapped = ["tgz", "tbz2", "txz", "tar7z", "tz", "tlz", "tzst"]; /// /// One compressed stream, no names or sizes to check beforehand (bzip2 states no size, gzip one modulo 4 GiB that /// its author chose). /// - private static readonly HashSet SingleStream = ["gz", "bz2", "xz", "lzma", "z", "zst", "tgz", "tbz2", "txz", "tz"]; + private static readonly HashSet SingleStream = ["gz", "bz2", "xz", "lzma", "z", "zst", "tgz", "tbz2", "txz", "tz", "tzst"]; /// Formats an RPM payload comes in (7-Zip shows it as one compressed CPIO file). private static readonly HashSet RpmPayload = ["cpio", "gz", "bz2", "xz", "lzma", "z", "zst"]; diff --git a/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs b/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs index 443d6516d..55a52488f 100644 --- a/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs +++ b/src/Filee.Engines/Archives/Bzip2BlockEncoder.cs @@ -192,7 +192,7 @@ private int MoveToFront(ReadOnlySpan bwt, ReadOnlySpan number, int i run = 0; } var j = list[1..inUse].IndexOf(value) + 1; - list[..j].CopyTo(list[1..]); + Bzip2Decoder.MoveUp(list, j); list[0] = value; symbols[count++] = (ushort)(j + 1); frequencies[j + 1]++; diff --git a/src/Filee.Engines/Archives/Bzip2Decoder.cs b/src/Filee.Engines/Archives/Bzip2Decoder.cs index fb973b8f4..0f501a14c 100644 --- a/src/Filee.Engines/Archives/Bzip2Decoder.cs +++ b/src/Filee.Engines/Archives/Bzip2Decoder.cs @@ -15,7 +15,10 @@ using System.Buffers; using System.Collections.Concurrent; +using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; namespace Filee.Engines.Archives; @@ -299,15 +302,7 @@ private Block ReadBlock(int[] tt, int blockMax, uint? expectedCrc) if (index >= inUseCount) throw new InvalidDataException("invalid bzip2 symbol"); var front = mtf[index]; - if (index < 16) - { - for (var k = index; k > 0; k--) - mtf[k] = mtf[k - 1]; - } - else - { - mtf[..index].CopyTo(mtf[1..]); - } + MoveUp(mtf, index); mtf[0] = front; var b = seqToUnseq[front]; counts[b]++; @@ -340,6 +335,25 @@ int NextSymbol() } } + /// + /// Moves the first bytes of the 256-byte move-to-front list one place up (count below + /// 256): 16 bytes at a time from the top while there are that many, each load taken before the store above it, + /// then byte by byte. One thread decodes the symbols of every block (the inverse transforms run on others), so + /// this step sets the pace; Bzip2BlockEncoder uses it too. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void MoveUp(Span mtf, int count) + { + if ((uint)count >= (uint)mtf.Length || mtf.Length < 256) + throw new ArgumentOutOfRangeException(nameof(count)); + ref var list = ref MemoryMarshal.GetReference(mtf); + var i = count; + for (; i >= 16; i -= 16) + Vector128.LoadUnsafe(ref list, (nuint)(i - 16)).StoreUnsafe(ref list, (nuint)(i - 15)); + for (; i > 0; i--) + Unsafe.Add(ref list, i) = Unsafe.Add(ref list, i - 1); + } + /// /// Canonical Huffman decode tables as in the reference bzip2 (limit / base / perm per code length), plus a table /// of the codes of up to bits found the same way. diff --git a/src/Filee.Engines/Archives/Bzip2Sort.cs b/src/Filee.Engines/Archives/Bzip2Sort.cs index d62eb7333..796d5f9fb 100644 --- a/src/Filee.Engines/Archives/Bzip2Sort.cs +++ b/src/Filee.Engines/Archives/Bzip2Sort.cs @@ -44,7 +44,11 @@ public static int Transform(ReadOnlySpan block, Span output, Cancell var at = sa[i]; if (at == start) origin = row; - output.Slice(row, repeats).Fill(word[at == 0 ? root - 1 : at - 1]); + var last = word[at == 0 ? root - 1 : at - 1]; + if (repeats == 1) + output[row] = last; // nearly every block: no repeating root + else + output.Slice(row, repeats).Fill(last); row += repeats; } return origin; diff --git a/src/Filee.Engines/Archives/CompressedFile.cs b/src/Filee.Engines/Archives/CompressedFile.cs index e11345e2f..ce8f724a0 100644 --- a/src/Filee.Engines/Archives/CompressedFile.cs +++ b/src/Filee.Engines/Archives/CompressedFile.cs @@ -1,14 +1,13 @@ -// Unpacks a compressed single file (.gz, .bz2, .xz, .lzma, .Z, .zst and the outer layer of .tar.gz, .tar.bz2, .tar.xz -// and .tar.Z) in-process: gzip with .NET's own decoder, zstd with SharpCompress (MIT), bzip2, xz, LZMA and Unix -// compress with Filee's own decoders (Bzip2Decoder, XzReader, LzmaRawReader, UnixCompressStream; bzip2 with -// randomised blocks from old encoders goes back to SharpCompress). None of these +// Unpacks a compressed single file (.gz, .bz2, .xz, .lzma, .Z, .zst and the outer layer of .tar.gz, .tar.bz2, .tar.xz, +// .tar.Z and .tar.zst) in-process: gzip with .NET's own decoder, zstd with SharpCompress (MIT, see ZstdReader), bzip2, +// xz, LZMA and Unix compress with Filee's own decoders (Bzip2Decoder, XzReader, LzmaRawReader, UnixCompressStream; +// bzip2 with randomised blocks from old encoders goes back to SharpCompress). None of these // formats declares a size it must keep (gzip's is the size modulo 4 GiB), so what comes out is counted by // UnpackBudget against bombs and a full drive. using System.IO.Compression; using Filee.Engines.Archives.Lzma; using SharpCompress.Compressors.BZip2; -using SharpCompress.Compressors.ZStandard; using SharpCompressMode = SharpCompress.Compressors.CompressionMode; namespace Filee.Engines.Archives; @@ -18,7 +17,7 @@ internal static class CompressedFile { /// Formats (and TAR wrappers) reads. public static readonly IReadOnlySet Formats = - new HashSet { "gz", "tgz", "bz2", "tbz2", "xz", "txz", "lzma", "z", "tz", "zst" }; + new HashSet { "gz", "tgz", "bz2", "tbz2", "xz", "txz", "lzma", "z", "tz", "zst", "tzst" }; /// /// The largest dictionary a .lzma (or EGG LZMA) header may name: real files use at most a few hundred MiB. (Filee's @@ -90,7 +89,8 @@ internal static Stream Open(string format, Stream input, string name, ref long d case "tz": return new UnixCompressStream(new NonClosingStream(input)); case "zst": - return new DecompressionStream(new NonClosingStream(input)); + case "tzst": + return new ZstdReader(input, name); case "lzma": { // "LZMA alone": properties (lc/lp/pb, dictionary size), unpacked size u64 (all ones: not known, diff --git a/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs b/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs index 7066a4e0f..a8bfdc33f 100644 --- a/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs +++ b/src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs @@ -9,6 +9,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; namespace Filee.Engines.Archives.Lzma; @@ -511,7 +512,9 @@ private int MatchRun(RangeDecoder rc, bool allowEndMarker) where TInput var pbMask = _pbMask; ref var probs = ref MemoryMarshal.GetArrayDataReference(_probabilities); var state = _state; - uint rep0 = _rep0, rep1 = _rep1, rep2 = _rep2, rep3 = _rep3; + // The last distance stays in a register; the three before it, used only by repeats, stay in the fields, so the + // coder and the loop's other values have the registers. + var rep0 = _rep0; ref var input = ref MemoryMarshal.GetArrayDataReference(rc.Buffer); var inEnd = rc.End; rc.Take(out var range, out var code, out var inPos); @@ -539,30 +542,30 @@ private int MatchRun(RangeDecoder rc, bool allowEndMarker) where TInput uint distance; if (Bit(ref Unsafe.Add(ref probs, IsRepG1At + state), ref range, ref code, ref input, ref inPos, ref inEnd, rc) == 0) { - distance = rep1; + distance = _rep1; } else { if (Bit(ref Unsafe.Add(ref probs, IsRepG2At + state), ref range, ref code, ref input, ref inPos, ref inEnd, rc) == 0) { - distance = rep2; + distance = _rep2; } else { - distance = rep3; - rep3 = rep2; + distance = _rep3; + _rep3 = _rep2; } - rep2 = rep1; + _rep2 = _rep1; } - rep1 = rep0; + _rep1 = rep0; rep0 = distance; } } else { - rep3 = rep2; - rep2 = rep1; - rep1 = rep0; + _rep3 = _rep2; + _rep2 = _rep1; + _rep1 = rep0; lengthModel = MatchLengthAt; } @@ -623,7 +626,9 @@ private int MatchRun(RangeDecoder rc, bool allowEndMarker) where TInput // The bytes are copied later, several matches at a time: the copies' sources are far apart in the window // and mostly not in the cache, and done back to back their loads overlap. Nothing before the copies reads - // the window (only literals do), so the bytes come out the same. + // the window (only literals do), so the bytes come out the same. The source is asked into the cache now, + // so it arrives while the next symbols are decoded. + Prefetch(window, pos - (int)rep0 - 1, copyLength); Unsafe.Add(ref deferred, copies * 3) = pos; Unsafe.Add(ref deferred, copies * 3 + 1) = (int)rep0 + 1; Unsafe.Add(ref deferred, copies * 3 + 2) = copyLength; @@ -650,13 +655,27 @@ private int MatchRun(RangeDecoder rc, bool allowEndMarker) where TInput _pos = pos; _state = state; _rep0 = rep0; - _rep1 = rep1; - _rep2 = rep2; - _rep3 = rep3; rc.Put(range, code, inPos); return next; } + /// + /// A hint to load the source of a copy into the cache (x86; elsewhere nothing). may be + /// below zero (the source wrapped to the window's end); only the address is used, nothing is read through it. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe void Prefetch(byte[] window, int source, int count) + { + if (!Sse.IsSupported) + return; + if (source < 0) + source += window.Length; + // Both addresses stay inside the window (a source that wraps around its end is only hinted up to there). + ref var data = ref MemoryMarshal.GetArrayDataReference(window); + Sse.Prefetch0(Unsafe.AsPointer(ref Unsafe.Add(ref data, source))); + Sse.Prefetch0(Unsafe.AsPointer(ref Unsafe.Add(ref data, Math.Min(source + count - 1, window.Length - 1)))); + } + /// Does the first copies MatchRun put off, in order. private void CopyDeferred(byte[] window, int count) { diff --git a/src/Filee.Engines/Archives/Lzma/MatchFinder.cs b/src/Filee.Engines/Archives/Lzma/MatchFinder.cs index f3d5eb3d5..fbb2ad515 100644 --- a/src/Filee.Engines/Archives/Lzma/MatchFinder.cs +++ b/src/Filee.Engines/Archives/Lzma/MatchFinder.cs @@ -6,7 +6,8 @@ // rebuilds the tree on the way with the cursor as the new root (everything smaller to its left, larger to its // right). Bytes already known to match on both sides are not compared again, so the longest matches are found in // far fewer steps than a chain needs; inserting a position costs the same walk. -// Two chainless tables keyed by the next two and three bytes catch close short matches either way. +// Two chainless tables keyed by the next two and three bytes catch close short matches either way (the two-byte one +// a small hash of them with the chain, so the fast level's tables stay in the cache). // // The window is one array: the dictionary behind the cursor plus room ahead for new input, slid back when the room // is used up. Positions are stored as buffer index + an offset that grows with each slide, so the tables never need @@ -27,6 +28,7 @@ using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; +using System.Runtime.Intrinsics.X86; namespace Filee.Engines.Archives.Lzma; @@ -48,7 +50,10 @@ internal sealed class MatchFinder private readonly int[] _links; // chain: one previous position per slot; tree: left and right child per slot private readonly int[] _head4; private readonly int[] _head3 = new int[1 << Hash3Bits]; - private readonly int[] _head2 = new int[1 << 16]; + // Two-byte table: the bytes themselves with the tree (every pair its slot); a 10-bit hash of them with the chain, + // whose small dictionary and many threads want tables that stay in the cache. + private const int ChainHash2Bits = 10; + private readonly int[] _head2; private readonly int _hash4Shift; private readonly int _depth; private readonly int _nice; @@ -73,6 +78,7 @@ public MatchFinder(int dictionary, int ahead, int depth, int nice, bool tree) _mask = dictionary - 1; _tree = tree; _links = new int[tree ? 2L * dictionary : dictionary]; + _head2 = new int[tree ? 1 << 16 : 1 << ChainHash2Bits]; var hashBits = Math.Clamp(BitOperations.Log2((uint)dictionary) - 1, 16, 24); _head4 = new int[1 << hashBits]; _hash4Shift = 32 - hashBits; @@ -85,7 +91,7 @@ public MatchFinder(int dictionary, int ahead, int depth, int nice, bool tree) public static long TableBytes(int dictionary, bool tree) { var hashBits = Math.Clamp(BitOperations.Log2((uint)dictionary) - 1, 16, 24); - return 4L * ((tree ? 2L : 1L) * dictionary + (1 << hashBits) + (1 << Hash3Bits) + (1 << 16)); + return 4L * ((tree ? 2L : 1L) * dictionary + (1 << hashBits) + (1 << Hash3Bits) + (tree ? 1 << 16 : 1 << ChainHash2Bits)); } /// Forgets the input and every position entered, keeping the buffer and the tables' memory. @@ -172,7 +178,9 @@ static void Rebase(int[] table, int delta) /// bytes, each longer than the one before; returns how many. /// public int FindAll(int pos, int maxLength, int[] lengths, uint[] distances) => - _ahead is { } ahead ? ahead.Take(pos, maxLength, lengths, distances) : Search(pos, maxLength, lengths, distances, out _); + _ahead is { } ahead ? ahead.Take(pos, maxLength, lengths, distances) + : _tree ? Search(pos, maxLength, lengths, distances, out _) + : ChainSearch(pos, maxLength, lengths, distances); /// /// Starts searching every position from to on @@ -266,8 +274,10 @@ private void NearSearch(int pos, int maxLength, ref Found found) } } + /// A search of the binary tree (the hash chain has ). private int Search(int pos, int maxLength, int[]? lengths, uint[]? distances, out uint distance) { + System.Diagnostics.Debug.Assert(_tree, "the chain searches with ChainSearch"); distance = 0; System.Diagnostics.Debug.Assert(pos >= _nextInsert, "a position is searched twice"); SkipTo(pos); @@ -306,33 +316,15 @@ private int Search(int pos, int maxLength, int[]? lengths, uint[]? distances, ou } } - if (_tree) - { - TreeWalk(pos, stored, candidate, ref found); - // The tree compares at most the nice length; a match that long may go on. - if (found.Best >= _nice && found.Best < maxLength) - { - var at = pos - (int)found.Distance - 1; - var longer = Common(buffer, pos, at, maxLength); - found.Best = longer; - if (lengths is not null) - lengths[found.Count - 1] = longer; - } - } - else + TreeWalk(pos, stored, candidate, ref found); + // The tree compares at most the nice length; a match that long may go on. + if (found.Best >= _nice && found.Best < maxLength) { - _links[stored & _mask] = candidate; - for (var depth = _depth; candidate > 0 && depth > 0 && found.Best < maxLength && found.Best < _nice; depth--) - { - var gap = stored - candidate; - if (gap <= 0 || gap >= _dictionary) - break; - var at = candidate - _offset; - candidate = _links[candidate & _mask]; - if (buffer[at + found.Best] != buffer[pos + found.Best] || buffer[at] != buffer[pos]) - continue; - found.Offer(Common(buffer, pos, at, maxLength), gap); - } + var at = pos - (int)found.Distance - 1; + var longer = Common(buffer, pos, at, maxLength); + found.Best = longer; + if (lengths is not null) + lengths[found.Count - 1] = longer; } distance = found.Distance; @@ -341,6 +333,104 @@ private int Search(int pos, int maxLength, int[]? lengths, uint[]? distances, ou return lengths is null ? found.Best : found.Count; } + /// + /// for the hash chain (the fast level), on its own so its loop keeps its values in registers: + /// the same tables, candidates and lists. A candidate is only measured when its two bytes ending at the best length + /// so far agree with the cursor's (a match longer than the best has them), which takes one load per candidate. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + private int ChainSearch(int pos, int maxLength, int[] lengths, uint[] distances) + { + System.Diagnostics.Debug.Assert(pos >= _nextInsert, "a position is searched twice"); + SkipTo(pos); + _nextInsert = pos + 1; + var buffer = Buffer; + if (pos + 4 > Filled || (uint)pos > (uint)(buffer.Length - Math.Max(4, maxLength))) + return 0; + + ref var data = ref MemoryMarshal.GetArrayDataReference(buffer); + ref var cursor = ref Unsafe.Add(ref data, pos); + var value = Unsafe.ReadUnaligned(ref cursor); + if (!BitConverter.IsLittleEndian) + value = BinaryPrimitives.ReverseEndianness(value); + var offset = _offset; + var dictionary = _dictionary; + var stored = pos + offset; + // The hashes are below the tables' sizes by construction: 16 bits, Hash3Bits and the shift for _head4. + ref var slot2 = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head2), ChainHash2(value)); + ref var slot3 = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head3), (int)(((value & 0xFFFFFF) * 0x9E3779B1u) >> (32 - Hash3Bits))); + ref var slot4 = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head4), (int)((value * 2654435761u) >> _hash4Shift)); + var candidate2 = slot2; + slot2 = stored; + var candidate3 = slot3; + slot3 = stored; + var candidate = slot4; + slot4 = stored; + ref var links = ref MemoryMarshal.GetArrayDataReference(_links); + var mask = _mask; + Unsafe.Add(ref links, stored & mask) = candidate; + // The next position is searched or entered right after this one: its table slots (mostly not in the cache) + // are asked for now, so they arrive while this chain is walked. + if (pos + 5 <= Filled) + PrefetchSlots(Unsafe.ReadUnaligned(ref Unsafe.Add(ref cursor, 1))); + + // Candidates lie within the dictionary behind pos, so they and maxLength bytes after them fit the buffer. + var best = 1; + var count = 0; + if (maxLength >= 2) + { + if (candidate2 > 0 && stored - candidate2 < dictionary && Unsafe.Add(ref data, candidate2 - offset) == cursor) + Offer(Common(buffer, pos, candidate2 - offset, maxLength), stored - candidate2, ref best, ref count, lengths, distances); + if (candidate3 > 0 && stored - candidate3 < dictionary && best < maxLength && Unsafe.Add(ref data, candidate3 - offset) == cursor) + Offer(Common(buffer, pos, candidate3 - offset, maxLength), stored - candidate3, ref best, ref count, lengths, distances); + } + var stopAt = Math.Min(maxLength, _nice); + for (var depth = _depth; candidate > 0 && depth > 0 && best < stopAt; depth--) + { + var gap = stored - candidate; + if (gap <= 0 || gap >= dictionary) + break; + var at = candidate - offset; + candidate = Unsafe.Add(ref links, candidate & mask); + if (Unsafe.ReadUnaligned(ref Unsafe.Add(ref data, at + best - 1)) != Unsafe.ReadUnaligned(ref Unsafe.Add(ref cursor, best - 1))) + continue; + Offer(Common(buffer, pos, at, maxLength), gap, ref best, ref count, lengths, distances); + } + return best < 2 ? 0 : count; + } + + /// + /// A hint to load the three- and four-byte table slots of the position whose first bytes are + /// (little-endian) into the cache (x86; elsewhere nothing). Nothing is read through the + /// addresses. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private unsafe void PrefetchSlots(uint value) + { + if (!Sse.IsSupported) + return; + if (!BitConverter.IsLittleEndian) + value = BinaryPrimitives.ReverseEndianness(value); + Sse.Prefetch0(Unsafe.AsPointer(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head3), (int)(((value & 0xFFFFFF) * 0x9E3779B1u) >> (32 - Hash3Bits))))); + Sse.Prefetch0(Unsafe.AsPointer(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head4), (int)((value * 2654435761u) >> _hash4Shift)))); + } + + /// The chain's two-byte slot for the bytes in the low half of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ChainHash2(uint value) => (int)(((value & 0xFFFF) * 2654435761u) >> (32 - ChainHash2Bits)); + + /// on locals. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void Offer(int length, int gap, ref int best, ref int count, int[] lengths, uint[] distances) + { + if (length <= best) + return; + best = length; + lengths[count] = length; + distances[count] = (uint)(gap - 1); + count++; + } + /// /// Walks the tree of (the old root) for the position , offering /// what matches, and makes the root. @@ -448,11 +538,13 @@ private void Insert(int pos) var found = new Found(0, null, null); for (var p = _nextInsert; p < end; p++) { + if (!_tree && p + 8 < end) + PrefetchSlots(BinaryPrimitives.ReadUInt32LittleEndian(buffer.AsSpan(p + 4))); var value = BinaryPrimitives.ReadUInt32LittleEndian(buffer.AsSpan(p)); var h4 = (int)((value * 2654435761u) >> _hash4Shift); var h3 = (int)(((value & 0xFFFFFF) * 0x9E3779B1u) >> (32 - Hash3Bits)); var stored = p + _offset; - Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head2), (int)(value & 0xFFFF)) = stored; + Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head2), _tree ? (int)(value & 0xFFFF) : ChainHash2(value)) = stored; Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head3), h3) = stored; ref var slot4 = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head4), h4); var candidate = slot4; diff --git a/src/Filee.Engines/Archives/SevenZipCoders.cs b/src/Filee.Engines/Archives/SevenZipCoders.cs index c8fc638b3..3af63398a 100644 --- a/src/Filee.Engines/Archives/SevenZipCoders.cs +++ b/src/Filee.Engines/Archives/SevenZipCoders.cs @@ -404,7 +404,10 @@ private int Convert(Span data) /// internal sealed class Bcj2Stream : ReadOnlyDecoderStream { - private readonly ByteInput _main; + private readonly Stream _main; + private readonly byte[] _mainBuffer = new byte[1 << 16]; + private int _mainPosition; + private int _mainEnd; private readonly ByteInput _call; private readonly ByteInput _jump; private readonly RangeDecoder _rc = new(); @@ -418,7 +421,7 @@ internal sealed class Bcj2Stream : ReadOnlyDecoderStream public Bcj2Stream(Stream main, Stream call, Stream jump, Stream rc, long size) { - _main = new ByteInput(main); + _main = main; _call = new ByteInput(call); _jump = new ByteInput(jump); _size = size; @@ -437,17 +440,41 @@ public override int Read(Span buffer) _done++; continue; } - var b = _main.ReadByte(); - if (b < 0) - throw new InvalidDataException("the BCJ2 main stream ends too early"); - buffer[n++] = (byte)b; - _done++; - if (!IsJump(_previous, (byte)b) || _done == _size) + if (_mainPosition == _mainEnd) { - _previous = (byte)b; + _mainPosition = 0; + _mainEnd = Math.Max(0, _main.Read(_mainBuffer, 0, _mainBuffer.Length)); + if (_mainEnd == 0) + throw new InvalidDataException("the BCJ2 main stream ends too early"); + } + // The main stream's bytes up to and with the next opcode that may have a target taken out, in one copy. + var run = _mainBuffer.AsSpan(_mainPosition, (int)Math.Min(Math.Min(_mainEnd - _mainPosition, buffer.Length - n), _size - _done)); + var previous = _previous; + var length = 0; + var jump = false; + while (length < run.Length) + { + var c = run[length++]; + if (IsJump(previous, c)) + { + jump = true; + previous = c; + break; + } + previous = c; + } + run[..length].CopyTo(buffer[n..]); + n += length; + _done += length; + _mainPosition += length; + var b = run[length - 1]; + var before = length >= 2 ? run[length - 2] : _previous; + if (!jump || _done == _size) + { + _previous = previous; continue; } - var index = b == 0xE8 ? _previous : b == 0xE9 ? 256 : 257; + var index = b == 0xE8 ? before : b == 0xE9 ? 256 : 257; if (_rc.DecodeBit(ref _probabilities[index]) == 0) { _previous = (byte)b; diff --git a/src/Filee.Engines/Archives/ZstdReader.cs b/src/Filee.Engines/Archives/ZstdReader.cs new file mode 100644 index 000000000..6d7ce53fa --- /dev/null +++ b/src/Filee.Engines/Archives/ZstdReader.cs @@ -0,0 +1,89 @@ +// Reads Zstandard data (.zst, the outer layer of .tar.zst, zstd RPM payloads) with SharpCompress's decoder (MIT). +// Frames follow one another (what "zstd -c a b > ab.zst" makes), and skippable frames (magic 0x184D2A5?) are passed +// over. Filee checks the first frame's magic itself, so a file that isn't zstd is "not an archive" instead of an empty +// result, and turns the decoder's errors into Filee's messages. A frame whose window is larger than 128 MiB (made with +// "zstd --long=28" or more) is left to 7-Zip, as the zstd tool refuses it without --memory: the decoder would set the +// window aside before a single byte shows the file is real. + +using System.Buffers.Binary; +using SharpCompress.Compressors.ZStandard; +using SharpCompress.Compressors.ZStandard.Unsafe; + +namespace Filee.Engines.Archives; + +/// A read-only stream of the data inside a zstd file. +internal sealed class ZstdReader : Stream +{ + private const uint FrameMagic = 0xFD2FB528; + private const uint SkippableMagic = 0x184D2A50; + + private readonly DecompressionStream _decoder; + private readonly string _name; + + /// Reads zstd frames from the current position of (which stays open). + public ZstdReader(Stream input, string name) + { + _name = name; + Span magic = stackalloc byte[4]; + var start = input.Position; + if (input.ReadAtLeast(magic, magic.Length, throwOnEndOfStream: false) < magic.Length || !IsFrameStart(magic)) + throw ArchiveSafety.NotAnArchive(name); + input.Position = start; + _decoder = new DecompressionStream(new NonClosingStream(input), leaveOpen: false); + } + + /// True for the magic of a zstd frame or a skippable frame. + internal static bool IsFrameStart(ReadOnlySpan magic) + { + var value = BinaryPrimitives.ReadUInt32LittleEndian(magic); + return value == FrameMagic || (value & 0xFFFFFFF0) == SkippableMagic; + } + + public override bool CanRead => true; + public override bool CanSeek => false; + public override bool CanWrite => false; + public override long Length => throw new NotSupportedException(); + + public override long Position + { + get => throw new NotSupportedException(); + set => throw new NotSupportedException(); + } + + public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count)); + + public override int Read(Span buffer) + { + try + { + return _decoder.Read(buffer); + } + catch (ZstdException ex) when (ex.Code == ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge) + { + throw new NotSupportedException("The zstd window is larger than 128 MiB (made with zstd --long).", ex); + } + catch (ZstdException ex) + { + throw ArchiveSafety.Damaged(_name, "zstd: " + ex.Message, ex); + } + catch (EndOfStreamException ex) + { + throw ArchiveSafety.Damaged(_name, "the zstd data ends early", ex); + } + } + + public override void Flush() + { + } + + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); + public override void SetLength(long value) => throw new NotSupportedException(); + public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); + + protected override void Dispose(bool disposing) + { + if (disposing) + _decoder.Dispose(); + base.Dispose(disposing); + } +} diff --git a/src/Filee.Engines/Drawings/DrawingConverter.cs b/src/Filee.Engines/Drawings/DrawingConverter.cs index c94203c7a..2f1746214 100644 --- a/src/Filee.Engines/Drawings/DrawingConverter.cs +++ b/src/Filee.Engines/Drawings/DrawingConverter.cs @@ -6,12 +6,17 @@ // Formats whose reader draws only part of what they hold come with a check (DrawingFormat.FirstUnsupported), which // FirstUnsupported (ICapabilityCheck) asks: their edges are Checked, so a file the reader can't draw (a binary .vsd) // keeps the route it would take without this converter. +// +// Fixed-layout documents (XPS, DrawingFormat.ReadPages) draw their own pages into the page model; the same outputs +// are written from those pages, and what the reader left out is named in the file's result (ConversionStep.LeftOut). using Filee.Core.Conversion; using Filee.Engines.Infrastructure; using Filee.Engines.Magick; +using Filee.Engines.Rendering; using Filee.Engines.Slides; using ImageMagick; +using SkiaSharp; namespace Filee.Engines.Drawings; @@ -58,7 +63,15 @@ public Task> ConvertAsync(ConversionStep step, IProgress>(() => { var format = Find(step.From) ?? throw new NotSupportedException($"The drawing converter doesn't read {step.From}."); - var deck = format.Read(step.InputPath, cancellationToken); + if (format.ReadPages is { } open) + { + using var document = open(step.InputPath, cancellationToken); + var written = WriteFixed(step, document, progress, cancellationToken); + if (document.LeftOut.Count > 0) + step.LeftOut?.Invoke(new LeftOutNotice("notice.renderer_left_out", document.LeftOut)); + return written; + } + var deck = format.Read!(step.InputPath, cancellationToken); if (deck.Slides.Count == 0) throw new InvalidDataException("The drawing has no pages."); progress?.Report(0.2); @@ -77,6 +90,82 @@ public Task> ConvertAsync(ConversionStep step, IProgress _formats.FirstOrDefault(f => f.Id == format); + /// A fixed-layout document's pages as a PDF, SVG files or pictures. + private static List WriteFixed(ConversionStep step, IFixedPages document, IProgress? progress, CancellationToken cancellationToken) + { + progress?.Report(0.1); + if (step.To == "pdf") + { + var all = new List(document.Count); + for (var i = 0; i < document.Count; i++) + { + all.Add(document.Page(i, cancellationToken)); + progress?.Report(0.1 + 0.4 * (i + 1) / document.Count); + } + if (step.Output.Allocate("pdf") is not { } target) + return []; + using (var output = File.Create(target)) + { + PdfPageWriter.Write(output, all, new PdfDocumentInfo { Title = document.Title }, + progress is null ? null : new Progress(p => progress.Report(0.5 + 0.5 * p)), cancellationToken); + } + return [target]; + } + var pages = PageRange.Parse(step.Preset.Pdf.PageRange, document.Count); + var extension = step.To == "svg" ? "svg" : ImageEncoder.OutputFormat(step.To, step.Preset.Image).Extension; + var written = new List(); + var done = 0; + foreach (var index in pages) + { + cancellationToken.ThrowIfCancellationRequested(); + var page = document.Page(index, cancellationToken); + var suffix = pages.Count > 1 ? $"_p{index + 1}" : null; + if (step.To == "svg") + { + var svg = SlideOutput.Svg(page, cancellationToken); + if (step.Output.Allocate(extension, suffix) is { } path) + { + File.WriteAllBytes(path, svg); + written.Add(path); + } + } + else + { + var dpi = Resolution(step, page.Width, page.Height); + using var bitmap = page.Render(dpi); + if (WritePicture(step, bitmap, dpi, extension, suffix) is { } path) + written.Add(path); + } + done++; + progress?.Report(0.1 + 0.9 * done / pages.Count); + } + return written; + } + + /// + /// The preset's resolution, lowered so a page of × points fits + /// : a large page at a high resolution would take gigabytes. + /// + private static float Resolution(ConversionStep step, float width, float height) + { + var dpi = (float)Math.Clamp(step.Preset.Pdf.RenderDpi, 36, 1200); + var inches = width / 72.0 * (height / 72.0); + return (float)Math.Min(dpi, Math.Sqrt(MaxPagePixels / Math.Max(inches, 1e-6))); + } + + /// Writes a page's picture with the image options applied; null when the output was skipped. + private static string? WritePicture(ConversionStep step, SKBitmap bitmap, float dpi, string extension, string? suffix) + { + using var data = bitmap.Encode(SKEncodedImageFormat.Png, 100); + using var image = new MagickImage(data.ToArray()); + image.Density = new Density(dpi, dpi, DensityUnit.PixelsPerInch); + ImageEncoder.ApplyOptions(image, step.Preset.Image, step.To); + if (step.Output.Allocate(extension, suffix) is not { } path) + return null; + ImageEncoder.WriteImage(image, path, step.To); + return path; + } + private static List WriteSvg(ConversionStep step, SlideDeck deck, IReadOnlyList pages, IProgress? progress, CancellationToken cancellationToken) { var written = new List(); @@ -96,26 +185,16 @@ private static List WriteSvg(ConversionStep step, SlideDeck deck, IReadO private static List WritePictures(ConversionStep step, SlideDeck deck, IReadOnlyList pages, IProgress? progress, CancellationToken cancellationToken) { - var dpi = (float)Math.Clamp(step.Preset.Pdf.RenderDpi, 36, 1200); - // A large page at a high resolution would take gigabytes: the resolution comes down to fit. - var inches = deck.Width / 72.0 * (deck.Height / 72.0); - dpi = (float)Math.Min(dpi, Math.Sqrt(MaxPagePixels / Math.Max(inches, 1e-6))); + var dpi = Resolution(step, deck.Width, deck.Height); var (_, extension) = ImageEncoder.OutputFormat(step.To, step.Preset.Image); var written = new List(); var done = 0; foreach (var bitmap in SlideOutput.ToBitmaps(deck, dpi, pages, cancellationToken)) { using (bitmap) - using (var data = bitmap.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100)) - using (var image = new MagickImage(data.ToArray())) { - image.Density = new Density(dpi, dpi, DensityUnit.PixelsPerInch); - ImageEncoder.ApplyOptions(image, step.Preset.Image, step.To); - if (step.Output.Allocate(extension, pages.Count > 1 ? $"_p{pages[done] + 1}" : null) is { } path) - { - ImageEncoder.WriteImage(image, path, step.To); + if (WritePicture(step, bitmap, dpi, extension, pages.Count > 1 ? $"_p{pages[done] + 1}" : null) is { } path) written.Add(path); - } } done++; progress?.Report(0.2 + 0.8 * done / pages.Count); diff --git a/src/Filee.Engines/Drawings/DrawingFormats.cs b/src/Filee.Engines/Drawings/DrawingFormats.cs index 19d8b25e3..6aab66c51 100644 --- a/src/Filee.Engines/Drawings/DrawingFormats.cs +++ b/src/Filee.Engines/Drawings/DrawingFormats.cs @@ -2,22 +2,51 @@ // into the slide model (one slide per page, the page's size as the slide's) and, when the reader draws only part of // what the format can hold, a check that names the first thing it can't draw (once the converter's edges are Checked, // see DrawingConverter, such a file keeps the route it would take without the converter, LibreOffice Draw). The -// readers add their line here: VSDX (Visio/), ODG / FODG (Odf/Draw). +// readers add their line here: VSDX (Visio/), ODG / FODG (Odf/Draw), XPS / OpenXPS (Xps/). +// +// Fixed-layout documents (XPS) are not slides: their reader draws pages straight into the page model (IFixedPages). +using Filee.Core.Conversion; using Filee.Engines.Odf.Draw; +using Filee.Engines.Rendering; using Filee.Engines.Slides; using Filee.Engines.Visio; +using Filee.Engines.Xps; namespace Filee.Engines.Drawings; /// A drawing format the converter reads. /// The format id (FormatRegistry). -/// Reads a file into pages; refuses a damaged or hostile one with InvalidDataException or the document limits' error. +/// +/// Reads a file into pages; refuses a damaged or hostile one with InvalidDataException or the document limits' error. +/// Null for a format read by . +/// /// The first thing in a file the reader can't draw, or null; null when it draws everything. internal sealed record DrawingFormat( string Id, - Func Read, - Func? FirstUnsupported = null); + Func? Read, + Func? FirstUnsupported = null) +{ + /// Opens a fixed-layout document whose pages its reader draws itself (instead of ). + public Func? ReadPages { get; init; } +} + +/// +/// A fixed-layout document opened for drawing: its pages are read and drawn when asked for, so pictures of a few pages +/// don't read the rest. +/// +internal interface IFixedPages : IDisposable +{ + int Count { get; } + + string? Title { get; } + + /// Page drawn into the page model. + PageContent Page(int index, CancellationToken cancellationToken = default); + + /// What the pages drawn so far left out (pictures that can't be decoded), for the file's result. + IReadOnlyList LeftOut { get; } +} internal static class DrawingFormats { @@ -29,6 +58,8 @@ internal static class DrawingFormats // OpenDocument drawings, packaged and flat; charts, OLE objects and 3D scenes are left to LibreOffice Draw. new("odg", (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) }, ]; /// What every drawing format is converted to. diff --git a/src/Filee.Engines/Filee.Engines.csproj b/src/Filee.Engines/Filee.Engines.csproj index 867ac31fb..00856d175 100644 --- a/src/Filee.Engines/Filee.Engines.csproj +++ b/src/Filee.Engines/Filee.Engines.csproj @@ -5,6 +5,8 @@ Filee.Engines + + true diff --git a/src/Filee.Engines/Hwp/HwpTextConverter.cs b/src/Filee.Engines/Hwp/HwpTextConverter.cs index c429f5abe..35649f396 100644 --- a/src/Filee.Engines/Hwp/HwpTextConverter.cs +++ b/src/Filee.Engines/Hwp/HwpTextConverter.cs @@ -7,7 +7,8 @@ // // RTF → TXT is here too (#1204): RtfReader's document written a line per paragraph, as a word processor saves plain // text. Without it the cheapest built-in route went through a PDF (the Filee renderer, then the PDF's text), which -// wraps long lines and loses the indentation and the empty lines, or through the HWPX writer. +// wraps long lines and loses the indentation and the empty lines, or through the HWPX writer. Word documents (DOCX and +// its macro-enabled and template variants, Word 97–2003 DOC, DOT and WPS) and ODT go the same way. using System.Text; using Filee.Core.Conversion; @@ -18,8 +19,8 @@ namespace Filee.Engines.Hwp; -/// Writes the text of HWP and HWPX documents as TXT, Markdown or HTML, and of RTF documents as TXT. -public sealed class HwpTextConverter : IConverter +/// Writes the text of HWP and HWPX documents as TXT, Markdown or HTML, and of Word, ODT and RTF as TXT. +public sealed class HwpTextConverter : IConverter, ICapabilityCheck { /// The engine's id; settings that named Unhwp ("unhwp", the engine this replaced) are moved to it. public const string EngineId = "hwp-text"; @@ -38,9 +39,24 @@ public sealed class HwpTextConverter : IConverter // LibreOffice's chosen edge (10 + 9 in the default order against 16 + 4), so it stays LibreOffice's while that // is installed, and cheaper than the renderer's PDF and its text (1 + 9 + 10 + 9). new("rtf", "txt", 16, DirectOnly: true), + // Word and ODT, direct only as well. LibreOffice's edge for them is its fallback (25), so the cost sits between: + // 24 + 4 is cheaper than the renderer's PDF and its text (29) and the routes through Pandoc's Markdown (29) or + // the HWPX writer (32), and dearer than LibreOffice moved to the top of the engine list (25 + 0), which keeps + // the conversion it had there. The DOC reader checks each file (a Word 6.0 / 95 file goes another way). + .. LineFormats.Where(f => f != "rtf") + .Select(f => new ConversionEdge(f, "txt", 24, DirectOnly: true, Checked: DocumentReaders.Checked(f))), ]; - public EngineStatus GetStatus() => EngineStatus.Available("HWP, HWPX → TXT, Markdown, HTML; RTF → TXT", EngineVersions.BuiltIn); + /// + /// Word processor documents, whose TXT is a line per paragraph () as the + /// word processor itself saves plain text. + /// + internal static readonly string[] LineFormats = ["rtf", "docx", "docm", "dotx", "dotm", "doc", "dot", "wps", "odt"]; + + public EngineStatus GetStatus() => EngineStatus.Available("HWP, HWPX → TXT, Markdown, HTML; Word, ODT, RTF → TXT", EngineVersions.BuiltIn); + + /// A Word 6.0 / 95 or Works file with a .doc or .wps name, which the DOC reader doesn't read. + public string? FirstUnsupported(string sourcePath, string sourceFormat) => DocumentReaders.Unsupported(sourcePath, sourceFormat); public async Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) { @@ -54,7 +70,7 @@ public async Task> ConvertAsync(ConversionStep step, IProg return []; var content = step.To switch { - "txt" when step.From == "rtf" => PlainTextWriter.WriteLines(document), + "txt" when LineFormats.Contains(step.From) => PlainTextWriter.WriteLines(document), "txt" => PlainTextWriter.Write(document), "md" => Markdown(document, step, path), "html" => Html(document, step), diff --git a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs index 1898362c5..8bd797608 100644 --- a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs +++ b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs @@ -48,6 +48,7 @@ internal static class DocumentReaders ["xlsx"] = (path, _) => SheetDocument.Build(XlsxReader.Read(path)), ["xlsm"] = (path, _) => SheetDocument.Build(XlsxReader.Read(path)), ["xltx"] = (path, _) => SheetDocument.Build(XlsxReader.Read(path)), + [XlsbReader.Format] = (path, _) => SheetDocument.Build(XlsbReader.Read(path)), ["csv"] = (path, _) => SheetDocument.Build(CsvFormat.Read(path)), ["tsv"] = (path, _) => SheetDocument.Build(SpreadsheetConverter.Read(path, "tsv")), ["xls"] = (path, _) => SheetDocument.Build(SpreadsheetConverter.Read(path, "xls")), diff --git a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs index 5a6cd618d..dcbe49e5e 100644 --- a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs @@ -1,7 +1,7 @@ // HDocument → plain text: paragraphs separated by an empty line, list markers and numbers written out, table rows // as tab-separated cells, text boxes on lines of their own and footnotes collected at the end. Used for e-books and -// HWP / HWPX → TXT. WriteLines is a word processor's "plain text" instead (RTF → TXT): a line per paragraph, an empty -// paragraph an empty line, and the text's own spaces and tabs as they are. +// HWP / HWPX → TXT. WriteLines is a word processor's "plain text" instead (RTF, Word and ODT → TXT): a line per +// paragraph, an empty paragraph an empty line, and the text's own spaces and tabs as they are. using System.Globalization; using System.Text; @@ -126,8 +126,27 @@ private static void Blocks(List blocks, StringBuilder sb, List n private static bool IsCode(HParagraph paragraph) => paragraph.Inlines.Count > 0 && paragraph.Inlines.All(i => i is HText { Format.Shade: HtmlReader.CodeShade }); - private static string Cell(HCell cell, List notes) => - string.Join(" ", cell.Blocks.OfType().Select(p => Inlines(p.Inlines, notes).Replace('\n', ' ').Replace('\t', ' ')).Where(t => t.Length > 0)); + /// A cell's text on one line: its paragraphs, and the cells of a table inside it, separated by a space. + private static string Cell(HCell cell, List notes) => string.Join(" ", CellTexts(cell.Blocks, notes)); + + private static IEnumerable CellTexts(List blocks, List notes) + { + foreach (var block in blocks) + { + IEnumerable texts = block switch + { + HParagraph p => [Inlines(p.Inlines, notes).Replace('\n', ' ').Replace('\t', ' ')], + // A table in the cell (Word, ODT and RTF nest them); Nesting.Limit bounds how deep this goes. + HTable table => table.Rows.SelectMany(r => r.Cells).SelectMany(c => CellTexts(c.Blocks, notes)), + _ => [], + }; + foreach (var text in texts) + { + if (text.Length > 0) + yield return text; + } + } + } /// /// The text of inlines, lines separated by '\n' (the callers write the line ends), without the white space at the diff --git a/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs b/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs index 995972b1a..ab9160a77 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", "csv", "tsv", "xls", "xlt", "ods"], StringComparer.OrdinalIgnoreCase); + new HashSet(["xlsx", "xlsm", "xltx", "xlsb", "csv", "tsv", "xls", "xlt", "ods"], 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; @@ -53,8 +53,9 @@ private static HSection Section(Worksheet sheet, bool withTitle) columns = columns.Take(MaxColumns).ToList(); // Excel widths count digits of Calibri 11 (7 px each + 5 px padding). The 한글 fonts that render the table have - // wider digits, so a character gets 8 px plus room for the cell margins; 75 HWPUNIT per pixel. - var widths = columns.Select(c => (int)Math.Round((sheet.ColumnWidth(c) * 8 + 10) * 75)).ToArray(); + // wider digits, so a character gets 8 px plus room for the cell margins; 75 HWPUNIT per pixel. Excel's widest + // column is 255 characters: a file that claims more can't overflow the table's width. + var widths = columns.Select(c => (int)Math.Round((Math.Clamp(sheet.ColumnWidth(c), 0, 255) * 8 + 10) * 75)).ToArray(); var total = Math.Max(1, widths.Sum()); var landscape = total > A4Short - 2 * Margin; var page = landscape diff --git a/src/Filee.Engines/Infrastructure/ComplexScripts.cs b/src/Filee.Engines/Infrastructure/ComplexScripts.cs index b760b74c8..79636104d 100644 --- a/src/Filee.Engines/Infrastructure/ComplexScripts.cs +++ b/src/Filee.Engines/Infrastructure/ComplexScripts.cs @@ -358,6 +358,9 @@ static bool InWord(string name, int before, int after) { try { + // Excel binary workbooks keep their text in records: their cells, as the built-in reader reads them. + if (format == XlsbReader.Format) + return FindInCells(XlsbReader.Read(path), classifier, skipBelow); if (IsZip(path)) return FindInPackage(path, format == "hwp" ? "hwpx" : format, classifier, skipBelow); // an .hwp that is HWPX // HWP 5.0 is binary: its records written out as the HWPX package 한글 saves, whose sections are scanned. diff --git a/src/Filee.Engines/Magick/ImageReader.cs b/src/Filee.Engines/Magick/ImageReader.cs index e4be76847..e4d65a310 100644 --- a/src/Filee.Engines/Magick/ImageReader.cs +++ b/src/Filee.Engines/Magick/ImageReader.cs @@ -27,7 +27,7 @@ internal static class ImageReader /// Reads as format . Layered formats come back as a single, /// flattened image; other multi-frame formats (TIFF pages, GIF frames, ICO sizes) keep all frames. /// - /// Stops the merge of a GIMP file between rows; other formats are read at once. + /// Stops the merge of a GIMP file between rows and the drawing of an EMF / WMF between records; other formats are read at once. public static MagickImageCollection Read(string path, string formatId, Preset preset, CancellationToken cancellationToken = default) { @@ -51,7 +51,7 @@ public static MagickImageCollection Read(string path, string formatId, Preset pr return ReadPackaged(ProgramIcon.Read(path), "ico", preset); if (formatId == "fits") return [FitsRendering.ToMagick(Fits.Read(path), preset.Image.FitsDebayer)]; - if (formatId is "emf" or "wmf" && ReadMetafile(path, preset) is { } metafile) + if (formatId is "emf" or "wmf" && ReadMetafile(path, preset, cancellationToken) is { } metafile) return metafile; var images = new MagickImageCollection(); @@ -106,12 +106,12 @@ public static MagickImageCollection Read(string path, string formatId, Preset pr /// a transparent background. Null for an EMF+ only file on Windows, which ImageMagick then reads through GDI+; /// elsewhere such a file fails with the renderer's reason. /// - private static MagickImageCollection? ReadMetafile(string path, Preset preset) + private static MagickImageCollection? ReadMetafile(string path, Preset preset, CancellationToken cancellationToken) { var dpi = Math.Max(MinimumMetafileDpi, Math.Clamp(preset.Pdf.RenderDpi, 36, 1200)); try { - var (bitmap, used) = Metafile.Render(path, dpi); + var (bitmap, used) = Metafile.Render(path, dpi, cancellationToken: cancellationToken); using (bitmap) return [SvgRenderer.ToMagick(bitmap, used)]; } diff --git a/src/Filee.Engines/Metafiles/Dib.cs b/src/Filee.Engines/Metafiles/Dib.cs index a21e40b0f..6ba6288af 100644 --- a/src/Filee.Engines/Metafiles/Dib.cs +++ b/src/Filee.Engines/Metafiles/Dib.cs @@ -1,8 +1,9 @@ // Device-independent bitmaps as metafiles carry them ([MS-WMF] 2.2.2.9 DeviceIndependentBitmap): a BITMAPCOREHEADER // or BITMAPINFOHEADER (and its V4 / V5 extensions), a colour table, and the pixels in rows padded to 32 bits, bottom-up // unless the height is negative. 1, 4, 8, 16, 24 and 32 bits per pixel, uncompressed, RLE4 / RLE8 and bit fields; -// JPEG and PNG pixels are decoded by Skia. The pixel count is checked against the metafile's budget before anything is -// allocated. +// JPEG and PNG pixels are decoded by Skia. The pixel count is checked against the metafile's budget, and the pixel data +// against the size the header gives, before anything is allocated; the pixels are then decoded straight into the +// bitmap Skia draws from, the one copy of them. using System.Buffers.Binary; using System.Runtime.InteropServices; @@ -59,23 +60,30 @@ internal static class Dib if (header.Compression is Jpeg or Png) return Encoded(bits, budget); + var runLength = header.Compression == Rle8 && header.BitCount == 8 || header.Compression == Rle4 && header.BitCount == 4; + if (!runLength && header.Compression is not (Rgb or BitFields or AlphaBitFields)) + return null; // CMYK or an unknown compression: nothing GDI would draw either budget.Bitmap(width, height); var colors = ColorTable(info, header, options); - var pixels = new uint[(long)width * height]; // BGRA, top-down + if (!runLength) + CheckUncompressed(bits, header, width, height); + + var alphaType = options.Alpha && header.BitCount == 32 ? SKAlphaType.Premul : SKAlphaType.Unpremul; + using var bitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888, alphaType)); + if (bitmap.GetPixels() == IntPtr.Zero) + throw new InvalidOperationException("Skia could not allocate the bitmap."); + if (bitmap.RowBytes != width * 4) + throw new InvalidOperationException("Skia gave the bitmap unexpected row bytes."); + var pixels = MemoryMarshal.Cast(bitmap.GetPixelSpan()); // BGRA, top-down var bottomUp = header.Height > 0; - switch (header.Compression) + if (runLength) { - case Rle8 when header.BitCount == 8: - case Rle4 when header.BitCount == 4: - RunLength(bits, header.Compression == Rle4, width, height, bottomUp, colors, pixels); - break; - case Rgb: - case BitFields: - case AlphaBitFields: - Uncompressed(bits, header, info, colors, options, width, height, bottomUp, pixels); - break; - default: - return null; // CMYK or an unknown compression: nothing GDI would draw either + pixels.Clear(); // pixels the runs skip stay transparent + RunLength(bits, header.Compression == Rle4, width, height, bottomUp, colors, pixels); + } + else + { + Uncompressed(bits, header, info, colors, options, width, height, bottomUp, pixels); } if (options.TransparentColor is { } key) @@ -87,13 +95,6 @@ internal static class Dib pixels[i] = 0; } } - - var alphaType = options.Alpha && header.BitCount == 32 ? SKAlphaType.Premul : SKAlphaType.Unpremul; - using var bitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888, alphaType)); - var bytes = MemoryMarshal.AsBytes(pixels.AsSpan()); - if (bitmap.RowBytes != width * 4) - throw new InvalidOperationException("Skia gave the bitmap unexpected row bytes."); - Marshal.Copy(bytes.ToArray(), 0, bitmap.GetPixels(), bytes.Length); bitmap.SetImmutable(); var image = SKImage.FromBitmap(bitmap) ?? throw new InvalidOperationException("Skia could not make an image of the bitmap."); return new DibImage(image, header.BitCount == 1); @@ -140,15 +141,25 @@ private static SKColor[] ColorTable(ReadOnlySpan info, Header header, DibO return colors; } - private static void Uncompressed(ReadOnlySpan bits, Header header, ReadOnlySpan info, SKColor[] colors, DibOptions options, - int width, int height, bool bottomUp, uint[] pixels) + /// Refuses uncompressed pixels of a depth GDI doesn't have, or fewer than the header's size needs. + private static void CheckUncompressed(ReadOnlySpan bits, Header header, int width, int height) { var bpp = header.BitCount; if (bpp is not (1 or 2 or 4 or 8 or 16 or 24 or 32)) throw new DibFormatException($"has a bitmap of {bpp} bits per pixel"); - var stride = ((long)width * bpp + 31) / 32 * 4; - if (stride * height > bits.Length) + if (Stride(width, bpp) * height > bits.Length) throw new DibFormatException($"has a bitmap of {width} × {height} pixels whose pixel data is cut off"); + } + + /// Bytes of one row of pixels, padded to 32 bits. + private static long Stride(int width, int bpp) => ((long)width * bpp + 31) / 32 * 4; + + /// Uncompressed pixels, once passed them. + private static void Uncompressed(ReadOnlySpan bits, Header header, ReadOnlySpan info, SKColor[] colors, DibOptions options, + int width, int height, bool bottomUp, Span pixels) + { + var bpp = header.BitCount; + var stride = Stride(width, bpp); var palette = new uint[colors.Length]; for (var i = 0; i < colors.Length; i++) palette[i] = Bgra(colors[i]); @@ -217,13 +228,13 @@ private static uint Channel(uint value, uint mask) } /// RLE8 and RLE4 ([MS-WMF] 3.1.6): runs and literal groups, end of line, end of bitmap and deltas. - private static void RunLength(ReadOnlySpan bits, bool four, int width, int height, bool bottomUp, SKColor[] colors, uint[] pixels) + private static void RunLength(ReadOnlySpan bits, bool four, int width, int height, bool bottomUp, SKColor[] colors, Span pixels) { var palette = new uint[colors.Length]; for (var i = 0; i < colors.Length; i++) palette[i] = Bgra(colors[i]); int x = 0, y = 0, at = 0; - void Put(int index) + void Put(Span pixels, int index) { if (x < width && y < height) pixels[(bottomUp ? height - 1 - y : y) * width + x] = palette[index % palette.Length]; @@ -236,7 +247,7 @@ void Put(int index) if (count > 0) { for (var i = 0; i < count; i++) - Put(four ? (i % 2 == 0 ? value >> 4 : value & 0x0F) : value); + Put(pixels, four ? (i % 2 == 0 ? value >> 4 : value & 0x0F) : value); continue; } switch (value) @@ -259,7 +270,7 @@ void Put(int index) if (at + bytes > bits.Length) throw new DibFormatException("has a run-length bitmap that is cut off"); for (var i = 0; i < value; i++) - Put(four ? (i % 2 == 0 ? bits[at + i / 2] >> 4 : bits[at + i / 2] & 0x0F) : bits[at + i]); + Put(pixels, four ? (i % 2 == 0 ? bits[at + i / 2] >> 4 : bits[at + i / 2] & 0x0F) : bits[at + i]); at += (bytes + 1) & ~1; break; } diff --git a/src/Filee.Engines/Metafiles/Metafile.cs b/src/Filee.Engines/Metafiles/Metafile.cs index 90dd5e4f5..5d3893af6 100644 --- a/src/Filee.Engines/Metafiles/Metafile.cs +++ b/src/Filee.Engines/Metafiles/Metafile.cs @@ -212,6 +212,7 @@ public static (SKBitmap Bitmap, float Dpi) Render(byte[] data, float dpi, Metafi if (longEdge > 0) scale = Math.Min(scale, Math.Min(MaximumLongEdge / longEdge, MathF.Sqrt(MaximumPixels / Math.Max(1e-6f, size.Width * size.Height)))); using var drawing = Draw(data, scale, limits, cancellationToken); + cancellationToken.ThrowIfCancellationRequested(); return (Rasterize(drawing, scale), scale * 72); } diff --git a/src/Filee.Engines/Office/LibreOfficeConverter.cs b/src/Filee.Engines/Office/LibreOfficeConverter.cs index 95fc48b9f..17af6d673 100644 --- a/src/Filee.Engines/Office/LibreOfficeConverter.cs +++ b/src/Filee.Engines/Office/LibreOfficeConverter.cs @@ -51,7 +51,7 @@ public sealed class LibreOfficeConverter : IConverter // LibreOffice 26.2: the DocumentService of each type's preferred import filter). ClarisWorks / AppleWorks files can // also be spreadsheets or drawings; they are taken as word processor files, the common case. private static readonly string[] WriterImport = ["dot", "wps", "wpd", "lwp", "abw", "pages", "sdw", "cwk", "mcw", "602", "psw"]; - private static readonly string[] CalcImport = ["et", "numbers", "sdc", "fods"]; + private static readonly string[] CalcImport = ["et", "numbers", "sdc", "fods", "xlsb"]; /// "sda" also covers .sdd: StarDraw files (.sda) open in Draw, StarImpress files (.sdd) in Impress. private static readonly string[] ImpressImport = ["key", "dps", "sda", "fodp"]; @@ -257,7 +257,7 @@ internal static Func OutputComplete(string outDir) /// private static string FirstPage(ConversionStep step) { - using var source = PdfReader.Open(step.InputPath, PdfDocumentOpenMode.Import); + using var source = PdfObjectNesting.Open(step.InputPath, PdfDocumentOpenMode.Import); var pages = PageRange.Parse(step.Preset.Pdf.PageRange, source.PageCount); if (pages.Count == 0) throw new InvalidOperationException("The PDF has no pages in the selected range."); diff --git a/src/Filee.Engines/Office/Ooxml/OpcPackage.cs b/src/Filee.Engines/Office/Ooxml/OpcPackage.cs index afa7a74fb..3755c91a1 100644 --- a/src/Filee.Engines/Office/Ooxml/OpcPackage.cs +++ b/src/Filee.Engines/Office/Ooxml/OpcPackage.cs @@ -76,6 +76,19 @@ private XDocument Load(ZipArchiveEntry entry) return copy.Length > max ? null : copy.ToArray(); } + /// + /// A binary part to read from start to end (an XLSB record stream), counted against the package's limits while it + /// is read; null when it doesn't exist. + /// + public Stream? OpenPart(string partPath) + { + if (Entry(partPath) is not { } entry) + return null; + if (_limited is not null) + return _limited.Open(entry); + return entry.Open(); + } + /// The main part (target of the package's officeDocument relationship), e.g. xl/workbook.xml. public string? MainPartPath => Relationships("").Values.FirstOrDefault(r => r.Type.EndsWith("/officeDocument", StringComparison.Ordinal)).Target; diff --git a/src/Filee.Engines/Office/SheetPrintConverter.cs b/src/Filee.Engines/Office/SheetPrintConverter.cs index cb556fc49..03768498f 100644 --- a/src/Filee.Engines/Office/SheetPrintConverter.cs +++ b/src/Filee.Engines/Office/SheetPrintConverter.cs @@ -1,4 +1,4 @@ -// XLSX (and XLSM / XLTX), 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 → 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", "xls", "xlt", "ods"]; + private static readonly string[] Sources = ["xlsx", "xlsm", "xltx", "xlsb", "xls", "xlt", "ods"]; 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, XLS, ODS → PDF", EngineVersions.BuiltIn); + public EngineStatus GetStatus() => EngineStatus.Available("XLSX, XLSB, XLS, ODS → 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/NumberFormatter.cs b/src/Filee.Engines/Office/Sheets/NumberFormatter.cs index 2cbc8521a..15bd9461b 100644 --- a/src/Filee.Engines/Office/Sheets/NumberFormatter.cs +++ b/src/Filee.Engines/Office/Sheets/NumberFormatter.cs @@ -25,11 +25,20 @@ internal sealed class NumberFormatter(bool date1904 = false) /// The short date pattern of as an Excel format code ("M/d/yyyy" → "m/d/yyyy"), or /// for a calendar other than the Gregorian one or a pattern with more than day, month and year. /// - internal static string ShortDate(CultureInfo culture) + internal static string ShortDate(CultureInfo culture) => ExcelShortDate(culture) ?? IsoDate; + + /// + /// True when is the PC's short date (format 14 as this PC shows it), so a writer can store + /// it as format 14 and Excel shows the reader's own short date. Not the ISO date this PC falls back to. + /// + public static bool IsShortDate(string code) => ExcelShortDate(CultureInfo.CurrentCulture) == code; + + /// , or null where the culture's short date can't be written as an Excel format. + private static string? ExcelShortDate(CultureInfo culture) { var format = culture.DateTimeFormat; if (format.Calendar is not GregorianCalendar) - return IsoDate; + return null; var pattern = format.ShortDatePattern; var code = new System.Text.StringBuilder(); for (var i = 0; i < pattern.Length;) @@ -56,7 +65,7 @@ internal static string ShortDate(CultureInfo culture) { var end = pattern.IndexOf(c, i + 1); if (end < 0) - return IsoDate; + return null; Literal(code, pattern[(i + 1)..end]); i = end + 1; continue; @@ -67,7 +76,7 @@ internal static string ShortDate(CultureInfo culture) continue; default: if (char.IsLetter(c)) - return IsoDate; // an era, a day or month name: not a short date Excel would show + return null; // an era, a day or month name: not a short date Excel would show Literal(code, new string(c, run)); break; } diff --git a/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs b/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs index 994ce977a..67f8a3ebc 100644 --- a/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs +++ b/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs @@ -6,6 +6,7 @@ // SmartArt, embedded objects, ink, text in shapes, freeform shapes, ...); form controls (ctrlProp parts, or legacy // VML controls); embedded OLE objects. Comments are not printed unless the page setup asks for them, and are // left out of the check. +// * XLSB: the same relationships as XLSX; the visible sheets come from the binary workbook part. // * XLS: the reader takes no drawing at all, so every object of a visible sheet is named by its OBJ record's type // (charts, pictures, text boxes, shapes, form controls; comments aside), and a visible chart sheet. // * ODS: the reader takes no drawing either: pictures, charts and other embedded objects, shapes, text boxes and @@ -39,7 +40,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, xls, xlt or ods. + /// Format id: xlsx, xlsm, xltx, xlsb, xls, xlt or ods. public static string? FirstUnsupported(string path, string format) { try @@ -47,6 +48,8 @@ internal static partial class SheetCapabilities return format switch { "xlsx" or "xlsm" or "xltx" => Xlsx(path), + // An Excel 97–2003 workbook with an .xlsb name is read as one (XlsbReader). + 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), @@ -79,27 +82,51 @@ private static bool IsZip(string path) var relationships = package.Relationships(workbookPath); foreach (var sheet in workbook.Element(S + "sheets")?.Elements(S + "sheet") ?? []) { - if ((string?)sheet.Attribute("state") is "hidden" or "veryHidden" - || (string?)sheet.Attribute(R + "id") is not { } id || !relationships.TryGetValue(id, out var relationship) || relationship.External) + if ((string?)sheet.Attribute("state") is "hidden" or "veryHidden") continue; - if (relationship.Type.EndsWith("/chartsheet", StringComparison.Ordinal)) - return "charts"; - if (!relationship.Type.EndsWith("/worksheet", StringComparison.Ordinal)) - continue; - // The sheet's parts tell what it holds, without reading its cells. - foreach (var part in package.Relationships(relationship.Target).Values.Where(r => !r.External)) + if (SheetFeature(package, relationships, (string?)sheet.Attribute(R + "id"), theme) is { } feature) + return feature; + } + return null; + } + + private static string? Xlsb(string path) + { + using var package = new OpcPackage(path, DocumentLimits.Default); + var workbookPath = package.MainPartPath ?? "xl/workbook.bin"; + if (workbookPath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) + return Xlsx(path); // an XLSX with an .xlsb name (XlsbReader) + var theme = package.TargetOfType(workbookPath, "/theme") is { } themePath ? package.Part(themePath)?.Root : null; + var relationships = package.Relationships(workbookPath); + foreach (var sheet in XlsbReader.ListSheets(package, workbookPath, DocumentLimits.Default)) + { + if (sheet.State == 0 && SheetFeature(package, relationships, sheet.RelationshipId, theme) is { } feature) + return feature; + } + return null; + } + + /// What a visible sheet holds that the printer doesn't draw, from its relationships, without reading its cells. + private static string? SheetFeature(OpcPackage package, Dictionary relationships, string? id, XElement? theme) + { + if (id is null || !relationships.TryGetValue(id, out var relationship) || relationship.External) + return null; + if (relationship.Type.EndsWith("/chartsheet", StringComparison.Ordinal)) + return "charts"; + if (!relationship.Type.EndsWith("/worksheet", StringComparison.Ordinal)) + return null; + foreach (var part in package.Relationships(relationship.Target).Values.Where(r => !r.External)) + { + var feature = part.Type switch { - var feature = part.Type switch - { - var type when type.EndsWith("/drawing", StringComparison.Ordinal) => XlsxDrawing.Read(package, part.Target, theme, read: false).FirstLeftOut, - var type when type.EndsWith("/ctrlProp", StringComparison.Ordinal) => "form controls", - var type when type.EndsWith("/oleObject", StringComparison.Ordinal) || type.EndsWith("/package", StringComparison.Ordinal) => "embedded objects", - var type when type.EndsWith("/vmlDrawing", StringComparison.Ordinal) => Vml(package, part.Target), - _ => null, - }; - if (feature is not null) - return feature; - } + var type when type.EndsWith("/drawing", StringComparison.Ordinal) => XlsxDrawing.Read(package, part.Target, theme, read: false).FirstLeftOut, + var type when type.EndsWith("/ctrlProp", StringComparison.Ordinal) => "form controls", + var type when type.EndsWith("/oleObject", StringComparison.Ordinal) || type.EndsWith("/package", StringComparison.Ordinal) => "embedded objects", + var type when type.EndsWith("/vmlDrawing", StringComparison.Ordinal) => Vml(package, part.Target), + _ => null, + }; + if (feature is not null) + return feature; } return null; } diff --git a/src/Filee.Engines/Office/Sheets/XlsbReader.cs b/src/Filee.Engines/Office/Sheets/XlsbReader.cs new file mode 100644 index 000000000..3af80a055 --- /dev/null +++ b/src/Filee.Engines/Office/Sheets/XlsbReader.cs @@ -0,0 +1,1044 @@ +// XLSB (Excel binary workbook) → Workbook, read directly from Microsoft's published format ([MS-XLSB]): the same ZIP +// package and relationships as XLSX, with the workbook, shared strings, styles and worksheets stored as BIFF12 +// records (a 1–2 byte type and a 1–4 byte size, 7 bits per byte) instead of XML. It fills the same sheet model as the +// XLSX reader, so every sheet output (XLSX, ODS, CSV, PDF, HWPX, HTML, ...) works from it. +// +// Read: sheets (hidden ones skipped), the 1904 date system, print areas and print titles (defined names), shared +// strings with rich text runs, number formats, fonts, fills (solid, patterns, gradients), borders, cell formats with +// their alignment, cell values (numbers, RK numbers, text, booleans, errors; formulas as the value Excel last +// calculated, the formula itself is not read), column widths and hidden columns, row heights and hidden rows, merged +// cells, frozen rows, right-to-left sheets, manual page breaks, the page setup, margins, print options, headers and +// footers (with their pictures) and each sheet's drawing (pictures and shapes; drawings are DrawingML XML in XLSB too). +// Left out: conditional formats (binary rules), comments, tables, data validation. +// +// The input is untrusted: the package is read within DocumentLimits (part and total sizes counted while unpacking, +// inflate ratio), records count against MaxXmlElements like XML elements do, a record the reader takes apart may hold +// at most MaxRecordBytes, every field is read within its record, and rows, columns and expanded column ranges are +// bounded as in the XLSX reader. + +using System.Buffers.Binary; +using System.Globalization; +using System.Text; +using System.Xml.Linq; +using Filee.Engines.Documents; +using Filee.Engines.Office.Ooxml; + +namespace Filee.Engines.Office.Sheets; + +internal sealed class XlsbReader +{ + public const string Format = "xlsb"; + + /// Bytes of one record the reader takes apart (the largest real ones, strings and names, stay below 200 KB). + internal const int MaxRecordBytes = 1024 * 1024; + + /// Column entries (BrtColInfo ranges expanded) one workbook may set, as in the XLSX reader. + private const int MaxColumnEntries = 4_000_000; + + /// Days between the 1900 and 1904 date systems. + private const double Date1904Offset = 1462; + + // Record types ([MS-XLSB] 2.3). + private const int RowHdr = 0; + private const int CellBlank = 1; + private const int CellRk = 2; + private const int CellError = 3; + private const int CellBool = 4; + private const int CellReal = 5; + private const int CellSt = 6; + private const int CellIsst = 7; + private const int FmlaString = 8; + private const int FmlaNum = 9; + private const int FmlaBool = 10; + private const int FmlaError = 11; + private const int SstItem = 19; + private const int Name = 39; + private const int Font = 43; + private const int Fmt = 44; + private const int Fill = 45; + private const int Border = 46; + private const int Xf = 47; + private const int ColInfo = 60; + private const int CellRString = 62; + private const int BeginWsView = 137; + private const int EndWsView = 138; + private const int WsProp = 147; + private const int Pane = 151; + private const int WbProp = 153; + private const int BundleSh = 156; + private const int MergeCell = 176; + private const int BeginRwBrk = 392; + private const int EndRwBrk = 393; + private const int BeginColBrk = 394; + private const int EndColBrk = 395; + private const int Brk = 396; + private const int Margins = 476; + private const int PrintOptions = 477; + private const int PageSetup = 478; + private const int BeginHeaderFooter = 479; + private const int WsFmtInfo = 485; + private const int Drawing = 550; + private const int LegacyDrawingHF = 552; + private const int BeginCellXfs = 617; + private const int EndCellXfs = 618; + private const int BeginCellStyleXfs = 626; + private const int EndCellStyleXfs = 627; + + /// Fill patterns by BrtFill fls (1 = solid, 0x28 = gradient), as SpreadsheetML names them. + private static readonly string?[] Patterns = + [ + null, "solid", "mediumGray", "darkGray", "lightGray", "darkHorizontal", "darkVertical", "darkDown", "darkUp", "darkGrid", + "darkTrellis", "lightHorizontal", "lightVertical", "lightDown", "lightUp", "lightGrid", "lightTrellis", "gray125", "gray0625", + ]; + + /// Border styles by Blxf dg. + private static readonly string[] BorderStyles = + [ + "none", "thin", "medium", "dashed", "dotted", "thick", "double", "hair", "mediumDashed", "dashDot", "mediumDashDot", + "dashDotDot", "mediumDashDotDot", "slantDashDot", + ]; + + private readonly OpcPackage _package; + private readonly DocumentLimits _limits; + private readonly List<(string Text, IReadOnlyList? Runs)> _sharedStrings = []; + private readonly List<(CellStyle Style, string Format)> _cellFormats = []; + private readonly List _fonts = []; + private OoxmlColors _colors = OoxmlColors.FromTheme(null); + private XDocument? _theme; + private NumberFormatter _formatter = new(); + private bool _date1904; + private int _records; + private int _columnEntries; + + private XlsbReader(OpcPackage package, DocumentLimits limits) + { + _package = package; + _limits = limits; + } + + /// Reads every visible sheet of . + public static Workbook Read(string path) => Read(path, DocumentLimits.Default); + + internal static Workbook Read(string path, DocumentLimits limits) + { + // An encrypted workbook is a compound file holding the encrypted package; an Excel 97–2003 workbook may also + // carry an .xlsb name. + if (CompoundFile.HasSignature(path)) + { + using (var cfb = CompoundFile.Open(path, limits)) + { + if (cfb.Exists("EncryptedPackage") || cfb.Exists("EncryptionInfo")) + throw new InvalidDataException("The workbook is protected with a password (encrypted). Remove the password in Excel and convert it again."); + } + return XlsReader.Read(path); + } + using var package = new OpcPackage(path, limits); + var workbookPath = package.MainPartPath ?? "xl/workbook.bin"; + // An XLSX saved with an .xlsb name: its workbook part is XML. + if (workbookPath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) + return XlsxReader.Read(path); + return new XlsbReader(package, limits).ReadWorkbook(workbookPath); + } + + // ───────────────────────── Workbook ───────────────────────── + + /// A sheet of the workbook part: its visibility (0 visible, 1 hidden, 2 very hidden), relationship and name. + internal readonly record struct SheetEntry(uint State, string? RelationshipId, string Name); + + private sealed class WorkbookPart + { + public List Sheets { get; } = []; + public bool Date1904 { get; set; } + + /// Print titles and print areas by the sheet's position (hidden sheets count). + public Dictionary> Titles { get; } = []; + public Dictionary> Areas { get; } = []; + } + + /// The sheets the workbook part lists, in order (for SheetCapabilities, which reads no cells). + internal static List ListSheets(OpcPackage package, string workbookPath, DocumentLimits limits) => + new XlsbReader(package, limits).ReadWorkbookPart(workbookPath).Sheets; + + private WorkbookPart ReadWorkbookPart(string workbookPath) + { + var result = new WorkbookPart(); + using var stream = _package.OpenPart(workbookPath) + ?? throw new InvalidDataException("The file is not an Excel binary workbook (no xl/workbook.bin)."); + var records = new Records(stream, workbookPath, this); + while (records.Next()) + { + switch (records.Type) + { + case WbProp: + var properties = records.Fields(); + result.Date1904 = (properties.U32() & 1) != 0; + break; + case BundleSh: + // Sheet ids run from 1 to 0xFFFF: more sheets than that is no workbook Excel wrote. + if (result.Sheets.Count >= 0xFFFF) + throw DocumentLimits.Exceeded("The workbook lists more than 65,535 sheets."); + var sheet = records.Fields(); + var state = sheet.U32(); + sheet.U32(); // iTabID + var relationship = sheet.String(nullable: true); + result.Sheets.Add(new SheetEntry(state, relationship, sheet.String() ?? "")); + break; + case Name: + DefinedName(records.Fields(), result); + break; + } + } + return result; + } + + /// A BrtName: print titles and print areas, which are built-in names scoped to a sheet. + private static void DefinedName(Fields name, WorkbookPart result) + { + var flags = name.U32(); + name.U8(); // chKey + var sheet = name.U32(); + var text = name.String() ?? ""; + // A built-in name is "_xlnm.Print_Area", or "Print_Area" with fBuiltin set. + if (sheet > int.MaxValue || (flags & 0x20) == 0 && !text.StartsWith("_xlnm.", StringComparison.OrdinalIgnoreCase)) + return; + var target = (text.StartsWith("_xlnm.", StringComparison.OrdinalIgnoreCase) ? text[6..] : text) switch + { + var n when n.Equals("Print_Titles", StringComparison.OrdinalIgnoreCase) => result.Titles, + var n when n.Equals("Print_Area", StringComparison.OrdinalIgnoreCase) => result.Areas, + _ => null, + }; + if (target is null) + return; + var length = name.U32(); + if (length > name.Left) + return; + target[(int)sheet] = Areas(name.Bytes((int)length)); + } + + /// + /// The ranges of a defined name's parsed formula: 3-D references and areas, joined by unions (PtgUnion inside a + /// PtgMemFunc); anything else ends the list. + /// + private static List Areas(ReadOnlySpan rgce) + { + var ranges = new List(); + var at = 0; + while (at < rgce.Length) + { + var ptg = rgce[at++]; + switch (ptg) + { + case 0x3B or 0x5B or 0x7B when at + 14 <= rgce.Length: // PtgArea3d: ixti, RgceAreaRel + Add(BinaryPrimitives.ReadUInt32LittleEndian(rgce[(at + 2)..]), BinaryPrimitives.ReadUInt32LittleEndian(rgce[(at + 6)..]), + BinaryPrimitives.ReadUInt16LittleEndian(rgce[(at + 10)..]) & 0x3FFF, BinaryPrimitives.ReadUInt16LittleEndian(rgce[(at + 12)..]) & 0x3FFF); + at += 14; + break; + case 0x3A or 0x5A or 0x7A when at + 8 <= rgce.Length: // PtgRef3d: ixti, RgceLocRel + var row = BinaryPrimitives.ReadUInt32LittleEndian(rgce[(at + 2)..]); + var column = BinaryPrimitives.ReadUInt16LittleEndian(rgce[(at + 6)..]) & 0x3FFF; + Add(row, row, column, column); + at += 8; + break; + case 0x29 or 0x49 or 0x69 when at + 2 <= rgce.Length: // PtgMemFunc: the expression follows + at += 2; + break; + case 0x10: // PtgUnion + break; + default: + return ranges; + } + } + return ranges; + + void Add(uint top, uint bottom, int left, int right) + { + if (top <= bottom && bottom <= SheetPagination.MaxRow && left <= right && right <= SheetPagination.MaxColumn) + ranges.Add(new CellRange((int)top, left, (int)bottom, right)); + } + } + + private Workbook ReadWorkbook(string workbookPath) + { + var part = ReadWorkbookPart(workbookPath); + _date1904 = part.Date1904; + _formatter = new NumberFormatter(_date1904); + _theme = _package.TargetOfType(workbookPath, "/theme") is { } theme ? _package.Part(theme) : null; + _colors = OoxmlColors.FromTheme(_theme); + + var result = new Workbook(); + // Styles first: rich text runs of the shared strings point into the fonts. + if (_package.TargetOfType(workbookPath, "/styles") is { } styles) + ReadStyles(styles, result); + if (_package.TargetOfType(workbookPath, "/sharedStrings") is { } shared) + ReadSharedStrings(shared); + + var relationships = _package.Relationships(workbookPath); + for (var index = 0; index < part.Sheets.Count; index++) + { + var entry = part.Sheets[index]; + if (entry.State != 0 || entry.RelationshipId is null || !relationships.TryGetValue(entry.RelationshipId, out var relationship) + || relationship.External || !relationship.Type.EndsWith("/worksheet", StringComparison.Ordinal)) + continue; // hidden sheets, chart sheets, dialog and macro sheets + var sheet = ReadSheet(entry.Name.Length > 0 ? entry.Name : "Sheet", relationship.Target); + if (part.Titles.TryGetValue(index, out var titles)) + { + // Whole rows repeat at the top, whole columns at the left. + foreach (var range in titles) + { + if (range.Left == 0 && range.Right == SheetPagination.MaxColumn) + sheet.TitleRows ??= (range.Top, range.Bottom); + else if (range.Top == 0 && range.Bottom == SheetPagination.MaxRow) + sheet.TitleColumns ??= (range.Left, range.Right); + } + } + if (part.Areas.TryGetValue(index, out var areas)) + sheet.PrintArea.AddRange(areas); + result.Sheets.Add(sheet); + } + return result; + } + + // ───────────────────────── Shared strings ───────────────────────── + + private void ReadSharedStrings(string partPath) + { + using var stream = _package.OpenPart(partPath); + if (stream is null) + return; + var records = new Records(stream, partPath, this); + while (records.Next()) + { + if (records.Type == SstItem) + { + var item = records.Fields(); + _sharedStrings.Add(RichText(ref item)); + } + } + } + + /// A RichStr: the text and, when it has runs, the pieces in their fonts (phonetic text skipped). + private (string Text, IReadOnlyList? Runs) RichText(ref Fields fields) + { + var flags = fields.U8(); + var text = fields.String() ?? ""; + if ((flags & 1) == 0) + return (text, null); + var count = fields.U32(); + if (count == 0 || count > 0x7FFF || count > fields.Left / 4) + return (text, null); + var starts = new List<(int Start, int Font)>(); + for (var i = 0; i < count; i++) + { + var start = fields.U16(); + var font = fields.U16(); + if (start < text.Length && (starts.Count == 0 || start > starts[^1].Start)) + starts.Add((start, font)); + } + if (starts.Count == 0) + return (text, null); + var runs = new List(); + if (starts[0].Start > 0) + runs.Add(new CellRun(text[..starts[0].Start])); + for (var i = 0; i < starts.Count; i++) + { + var end = i + 1 < starts.Count ? starts[i + 1].Start : text.Length; + var piece = text[starts[i].Start..end]; + if (starts[i].Font >= _fonts.Count) + { + runs.Add(new CellRun(piece)); + continue; + } + var font = _fonts[starts[i].Font]; + runs.Add(new CellRun(piece) + { + Bold = font.Bold, + Italic = font.Italic, + Underline = font.Underline, + Strike = font.Strike, + FontSize = font.Size, + Color = font.Color, + FontName = font.Name, + Script = font.Script, + }); + } + return (text, runs); + } + + // ───────────────────────── Styles ───────────────────────── + + private sealed record FontInfo(bool Bold, bool Italic, bool Underline, bool DoubleUnderline, bool Strike, double? Size, string? Color, string? Name, int Script); + + private void ReadStyles(string partPath, Workbook book) + { + using var stream = _package.OpenPart(partPath); + if (stream is null) + return; + var formats = new Dictionary(); + var fills = new List<(string? Color, string? Pattern, string? Background, CellGradient? Gradient)>(); + var borders = new List<(CellBorder Left, CellBorder Right, CellBorder Top, CellBorder Bottom, CellBorder Up, CellBorder Down)>(); + var styleFonts = new List(); + var cellXfs = new List<(int Format, int Font, int Fill, int Border, int Rotation, int Indent, int Alignment)>(); + var inCellXfs = false; + var inStyleXfs = false; + var records = new Records(stream, partPath, this); + while (records.Next()) + { + switch (records.Type) + { + case Fmt: + var format = records.Fields(); + var id = format.U16(); + var code = format.String(); + formats.TryAdd(id, code ?? "General"); + break; + case Font: + _fonts.Add(ReadFont(records.Fields())); + break; + case Fill: + fills.Add(ReadFill(records.Fields())); + break; + case Border: + borders.Add(ReadBorder(records.Fields())); + break; + case BeginCellXfs: + inCellXfs = true; + break; + case EndCellXfs: + inCellXfs = false; + break; + case BeginCellStyleXfs: + inStyleXfs = true; + break; + case EndCellStyleXfs: + inStyleXfs = false; + break; + case Xf when inCellXfs || inStyleXfs: + var xf = records.Fields(); + xf.U16(); // ixfeParent + var numberFormat = xf.U16(); + var font = xf.U16(); + var fill = xf.U16(); + var border = xf.U16(); + var rotation = xf.U8(); + var indent = xf.U8(); + var alignment = xf.U16(); + if (inStyleXfs) + styleFonts.Add(font); + else + cellXfs.Add((numberFormat, font, fill, border, rotation, indent, alignment)); + break; + } + } + + // The Normal style's font (the first cell style): Excel measures column widths in its digits. + var normal = styleFonts.Count > 0 && styleFonts[0] < _fonts.Count ? _fonts[styleFonts[0]] : _fonts.FirstOrDefault(); + if (normal?.Name is { Length: > 0 } fontName) + book.DefaultFontName = fontName; + if (normal?.Size is > 0 and <= 409 && normal.Size is { } fontSize) + book.DefaultFontSize = fontSize; + + foreach (var (formatId, fontId, fillId, borderId, rotation, indent, alignment) in cellXfs) + { + var font = fontId < _fonts.Count ? _fonts[fontId] : null; + var fill = fillId < fills.Count ? fills[fillId] : default; + var border = borderId < borders.Count ? borders[borderId] : NoBorders; + var horizontal = alignment & 0x07; + var vertical = (alignment >> 3) & 0x07; + var style = new CellStyle + { + Bold = font?.Bold ?? false, + Italic = font?.Italic ?? false, + Underline = font?.Underline ?? false, + DoubleUnderline = font?.DoubleUnderline ?? false, + Strike = font?.Strike ?? false, + FontSize = font?.Size, + FontName = font?.Name, + Script = font?.Script ?? 0, + Color = font?.Color, + Fill = fill.Color, + FillPattern = fill.Pattern, + PatternBackground = fill.Background, + Gradient = fill.Gradient, + // alc: general, left, center, right, fill, justify, center across selection, distributed. + Align = horizontal switch + { + 1 => CellAlign.Left, + 2 or 6 => CellAlign.Center, + 3 => CellAlign.Right, + _ => CellAlign.General, + }, + SpecialAlign = horizontal switch + { + 4 => CellSpecialAlign.Fill, + 5 => CellSpecialAlign.Justify, + 6 => CellSpecialAlign.CenterAcross, + 7 => CellSpecialAlign.Distributed, + _ => CellSpecialAlign.None, + }, + // alcv: top, center, bottom, justify, distributed. + VerticalAlign = vertical switch + { + 0 or 3 or 4 => CellVerticalAlign.Top, + 1 => CellVerticalAlign.Center, + _ => CellVerticalAlign.Bottom, + }, + VerticalJustify = vertical is 3 or 4, + Wrap = (alignment & 0x40) != 0, + Shrink = (alignment & 0x100) != 0, + Indent = Math.Min(indent, 250), + Rotation = XlsxReader.Rotation(rotation), + Left = border.Left, + Right = border.Right, + Top = border.Top, + Bottom = border.Bottom, + DiagonalUp = border.Up, + DiagonalDown = border.Down, + }; + _cellFormats.Add((style, formats.GetValueOrDefault(formatId) ?? NumberFormatter.BuiltIn(formatId) ?? "General")); + } + } + + private static readonly (CellBorder Left, CellBorder Right, CellBorder Top, CellBorder Bottom, CellBorder Up, CellBorder Down) NoBorders = + (CellBorder.None, CellBorder.None, CellBorder.None, CellBorder.None, CellBorder.None, CellBorder.None); + + private FontInfo ReadFont(Fields font) + { + var height = font.U16(); + var flags = font.U16(); + var weight = font.U16(); + var script = font.U16(); + var underline = font.U8(); + font.Skip(3); // bFamily, bCharSet, unused + var color = Color(ref font); + font.U8(); // bFontScheme + var name = font.String(); + return new FontInfo( + weight >= 700, + (flags & 0x02) != 0, + underline is not 0, + underline is 0x02 or 0x22, + (flags & 0x08) != 0, + height is >= 20 and <= 0x1FFF ? height / 20.0 : null, + color, + name is { Length: > 0 } ? name : null, + script switch { 1 => 1, 2 => -1, _ => 0 }); + } + + private (string? Color, string? Pattern, string? Background, CellGradient? Gradient) ReadFill(Fields fill) + { + var pattern = fill.U32(); + var foreground = Color(ref fill); + var background = Color(ref fill); + if (pattern == 0x28) + { + var type = fill.U32(); + var degree = fill.F64(); + fill.Skip(32); // the inner rectangle of a rectangular gradient + var count = fill.U32(); + var stops = new List<(double Position, string Color)>(); + for (var i = 0; i < Math.Min(count, 16u) && fill.Left >= 16; i++) + { + var color = Color(ref fill); + var position = fill.F64(); + if (color is not null) + stops.Add((double.IsFinite(position) ? Math.Clamp(position, 0, 1) : 0, color)); + } + if (stops.Count == 0) + return default; + // A rectangular (path) gradient is drawn as a linear one from its first colour to its last. + return (stops[0].Color, null, null, stops.Count > 1 ? new CellGradient(type == 0 && double.IsFinite(degree) ? degree : 0, stops) : null); + } + if (pattern >= Patterns.Length || Patterns[pattern] is not { } name || name == "gray125") + return default; + return name == "solid" + ? (foreground ?? background, null, null, null) + : (foreground ?? "#000000", name, background, null); + } + + private (CellBorder Left, CellBorder Right, CellBorder Top, CellBorder Bottom, CellBorder Up, CellBorder Down) ReadBorder(Fields border) + { + var flags = border.U8(); + var top = Side(ref border); + var bottom = Side(ref border); + var left = Side(ref border); + var right = Side(ref border); + var diagonal = Side(ref border); + return (left, right, top, bottom, (flags & 2) != 0 ? diagonal : CellBorder.None, (flags & 1) != 0 ? diagonal : CellBorder.None); + } + + /// A Blxf: the line style and colour of one border side. + private CellBorder Side(ref Fields fields) + { + var style = fields.U8(); + fields.U8(); + var color = Color(ref fields); + return style is 0 or >= 14 ? CellBorder.None : new CellBorder(BorderStyles[style], color); + } + + /// + /// A BrtColor as "#RRGGBB", or null for automatic. Theme indexes and tints mean what they mean in SpreadsheetML, + /// so they go through the same resolution. + /// + private string? Color(ref Fields fields) + { + var type = fields.U8() >> 1; + var index = fields.U8(); + var tint = fields.I16(); + var (red, green, blue) = (fields.U8(), fields.U8(), fields.U8()); + fields.U8(); // alpha + var color = type switch + { + 1 => new XElement("color", new XAttribute("indexed", index)), + 2 => new XElement("color", new XAttribute("rgb", $"FF{red:X2}{green:X2}{blue:X2}")), + 3 => new XElement("color", new XAttribute("theme", index)), + _ => null, + }; + if (color is null) + return null; + if (tint != 0) + color.SetAttributeValue("tint", Math.Clamp(tint / 32767.0, -1, 1).ToString("R", CultureInfo.InvariantCulture)); + return _colors.Spreadsheet(color); + } + + // ───────────────────────── Sheets ───────────────────────── + + private Worksheet ReadSheet(string name, string partPath) + { + var sheet = new Worksheet(name); + using var stream = _package.OpenPart(partPath); + if (stream is null) + return sheet; + + var row = -1; + var views = 0; + var inView = false; + var breaks = 0; // 1 = row breaks, 2 = column breaks + var fitToPage = false; + (uint Paper, uint Scale, int FirstPage, uint FitWidth, uint FitHeight, int Flags)? page = null; + double[]? margins = null; + int? options = null; + SheetHeaderFooter? headerFooter = null; + string? headerPictures = null; + string? drawing = null; + + var records = new Records(stream, partPath, this); + while (records.Next()) + { + switch (records.Type) + { + case RowHdr: + row = ReadRow(records.Fields(), sheet); + break; + case >= CellBlank and <= FmlaError or CellRString: + if (row >= 0) + ReadCell(records.Type, records.Fields(), row, sheet); + break; + case WsProp: + fitToPage = (records.Fields().U16() & 0x100) != 0; + break; + case WsFmtInfo: + ReadFormatInfo(records.Fields(), sheet); + break; + case BeginWsView: + // The first view is the one Excel opens with. + inView = views++ == 0; + if (inView) + sheet.RightToLeft = (records.Fields().U16() & 0x20) != 0; + break; + case EndWsView: + inView = false; + break; + case Pane when inView: + var pane = records.Fields(); + pane.F64(); // xnumXSplit: columns (the spec's prose swaps the two; the order is BIFF8 PANE's x, y) + var frozen = pane.F64(); + pane.Skip(12); + if ((pane.U8() & 0x03) != 0 && double.IsFinite(frozen) && frozen is >= 0 and <= SheetPagination.MaxRow) + sheet.FrozenRows = (int)frozen; + break; + case ColInfo: + ReadColumns(records.Fields(), sheet); + break; + case MergeCell: + var merge = records.Fields(); + var (top, bottom, left, right) = (merge.U32(), merge.U32(), merge.U32(), merge.U32()); + if (top <= bottom && left <= right && (bottom > top || right > left) && bottom <= SheetPagination.MaxRow && right <= SheetPagination.MaxColumn) + sheet.Merges.Add(new CellRange((int)top, (int)left, (int)bottom, (int)right)); + break; + case BeginRwBrk: + breaks = 1; + break; + case BeginColBrk: + breaks = 2; + break; + case EndRwBrk or EndColBrk: + breaks = 0; + break; + case Brk when breaks != 0: + var brk = records.Fields(); + var at = brk.U32(); + brk.Skip(8); + var manual = brk.U32() != 0; + if (manual && at > 0 && at <= (breaks == 1 ? SheetPagination.MaxRow : SheetPagination.MaxColumn)) + (breaks == 1 ? sheet.RowBreaks : sheet.ColumnBreaks).Add((int)at); + break; + case PrintOptions: + options = records.Fields().U16(); + break; + case Margins: + var fields = records.Fields(); + margins = [fields.F64(), fields.F64(), fields.F64(), fields.F64(), fields.F64(), fields.F64()]; + break; + case PageSetup: + var setup = records.Fields(); + var paper = setup.U32(); + var scale = setup.U32(); + setup.Skip(12); // iRes, iVRes, iCopies + var firstPage = setup.I32(); + var fitWidth = setup.U32(); + var fitHeight = setup.U32(); + page = (paper, scale, firstPage, fitWidth, fitHeight, setup.U16()); + break; + case BeginHeaderFooter: + headerFooter = ReadHeaderFooter(records.Fields()); + break; + case Drawing: + drawing = records.Fields().String(nullable: true); + break; + case LegacyDrawingHF: + headerPictures = records.Fields().String(nullable: true); + break; + } + } + + sheet.PageSetup = PageSetupOf(page, margins, options, fitToPage, headerFooter, partPath, headerPictures); + if (_package.Target(partPath, drawing) is { } drawingPart) + sheet.Drawings.AddRange(XlsxDrawing.Read(_package, drawingPart, _theme?.Root, read: true).Drawings); + return sheet; + } + + /// A BrtRowHdr: the row's height and visibility; returns the row (or -1 for one beyond the sheet). + private static int ReadRow(Fields fields, Worksheet sheet) + { + var row = fields.U32(); + fields.U32(); // ixfe + var height = fields.U16(); + fields.U8(); + var flags = fields.U8(); + if (row > SheetPagination.MaxRow) + return -1; + var r = (int)row; + if ((flags & 0x10) != 0) // fDyZero + sheet.HiddenRows.Add(r); + // miyRw in twips: the custom height (fUnsynced), else the height Excel last fitted the row to. + if (height is > 0 and <= 8192) + { + if ((flags & 0x20) != 0) + sheet.RowHeights[r] = height / 20.0; + else + sheet.AutoRowHeights[r] = height / 20.0; + } + return r; + } + + private void ReadCell(int type, Fields fields, int row, Worksheet sheet) + { + var column = fields.U32(); + var s = (int)(fields.U32() & 0xFFFFFF); + if (column > SheetPagination.MaxColumn) + return; + var (style, format) = s < _cellFormats.Count ? _cellFormats[s] : (CellStyle.Default, "General"); + var cell = type switch + { + CellBlank => new SheetCell("", style), + CellRk => Number(Rk(fields.U32()), style, format), + CellReal or FmlaNum => Number(fields.F64(), style, format), + CellError or FmlaError => new SheetCell(ErrorText(fields.U8()), style) { IsError = true }, + CellBool or FmlaBool => fields.U8() != 0 + ? new SheetCell("TRUE", style) { Value = 1, IsBoolean = true } + : new SheetCell("FALSE", style) { Value = 0, IsBoolean = true }, + CellSt or FmlaString => new SheetCell(fields.String() ?? "", style), + CellIsst => fields.U32() is var index && index < _sharedStrings.Count + ? new SheetCell(_sharedStrings[(int)index].Text, style) { Runs = _sharedStrings[(int)index].Runs } + : new SheetCell("", style), + _ => RichCell(ref fields, style), + }; + if (cell.HasContent) + sheet.Set(row, (int)column, cell); + } + + private SheetCell RichCell(ref Fields fields, CellStyle style) + { + var (text, runs) = RichText(ref fields); + return new SheetCell(text, style) { Runs = runs }; + } + + /// An RkNumber: the high 30 bits of a double or a 30-bit integer, perhaps × 100. + internal static double Rk(uint raw) + { + var value = (raw & 0x02) != 0 ? (int)raw >> 2 : BitConverter.Int64BitsToDouble((long)(raw & 0xFFFFFFFC) << 32); + return (raw & 0x01) != 0 ? value / 100 : value; + } + + /// The stored number, in the workbook's own date system. + private SheetCell Number(double value, CellStyle style, string format) => !double.IsFinite(value) + ? new SheetCell("#NUM!", style) { IsError = true } + : new SheetCell(_formatter.Format(value, format), style, IsNumber: true) + { + Value = _date1904 && _formatter.IsDate(format) ? value + Date1904Offset : value, + NumberFormat = NumberFormatter.IsGeneral(format) ? null : format, + }; + + private static string ErrorText(byte code) => code switch + { + 0x00 => "#NULL!", + 0x07 => "#DIV/0!", + 0x0F => "#VALUE!", + 0x17 => "#REF!", + 0x1D => "#NAME?", + 0x24 => "#NUM!", + 0x2A => "#N/A", + _ => "#GETTING_DATA", + }; + + /// A BrtWsFmtInfo: the default column width (or Excel's base width) and the default row height. + private static void ReadFormatInfo(Fields fields, Worksheet sheet) + { + var width = fields.U32(); + var baseWidth = fields.U16(); + var height = fields.U16(); + if (width != 0xFFFFFFFF && width / 256.0 <= 255) + { + sheet.DefaultColumnWidth = width / 256.0; + sheet.HasDefaultColumnWidth = true; + } + else if (width == 0xFFFFFFFF && baseWidth <= 255) + { + sheet.BaseColumnWidth = baseWidth; + } + if (height is > 0 and <= 8190) + sheet.DefaultRowHeight = height / 20.0; + } + + /// A BrtColInfo: the width (in characters × 256) and visibility of a range of columns. + private void ReadColumns(Fields fields, Worksheet sheet) + { + var first = fields.U32(); + var last = Math.Min(fields.U32(), (uint)SheetPagination.MaxColumn); + var width = Math.Min(fields.U32(), 65535) / 256.0; // Excel's widest column is 255 characters + fields.U32(); // ixfe + var hidden = (fields.U16() & 0x01) != 0 || width == 0; + if (first > last) + return; + if ((_columnEntries += (int)(last - first + 1)) > MaxColumnEntries) + throw DocumentLimits.Exceeded("The workbook sets more than 4,000,000 column widths."); + for (var c = (int)first; c <= last; c++) + { + sheet.ColumnWidths[c] = width; + if (hidden) + sheet.HiddenColumns.Add(c); + } + } + + private static SheetHeaderFooter ReadHeaderFooter(Fields fields) + { + var flags = fields.U16(); + return new SheetHeaderFooter + { + OddHeader = fields.String(nullable: true), + OddFooter = fields.String(nullable: true), + EvenHeader = fields.String(nullable: true), + EvenFooter = fields.String(nullable: true), + FirstHeader = fields.String(nullable: true), + FirstFooter = fields.String(nullable: true), + DifferentOddEven = (flags & 0x01) != 0, + DifferentFirst = (flags & 0x02) != 0, + ScaleWithDocument = (flags & 0x04) != 0, + AlignWithMargins = (flags & 0x08) != 0, + }; + } + + /// The page setup the way the XLSX reader makes it from pageSetup, pageMargins, printOptions and headerFooter. + private SheetPageSetup PageSetupOf((uint Paper, uint Scale, int FirstPage, uint FitWidth, uint FitHeight, int Flags)? page, double[]? margins, + int? options, bool fitToPage, SheetHeaderFooter? headerFooter, string partPath, string? headerPictures) + { + var setup = page is null ? SheetPageSetup.ForRegion() : new SheetPageSetup { PaperWidth = Sheets.Paper.Letter.Width, PaperHeight = Sheets.Paper.Letter.Height }; + if (page is { } p) + { + if (p.Paper <= int.MaxValue && Sheets.Paper.FromCode((int)p.Paper) is { } paper) + setup = setup with { PaperWidth = paper.Width, PaperHeight = paper.Height }; + setup = setup with + { + // fNoOrient leaves the orientation to the printer. + Landscape = (p.Flags & 0x02) != 0 && (p.Flags & 0x40) == 0, + Scale = p.Scale is >= 10 and <= 400 ? (int)p.Scale : 100, + FitWidth = p.FitWidth <= 32767 ? (int)p.FitWidth : 1, + FitHeight = p.FitHeight <= 32767 ? (int)p.FitHeight : 1, + OverThenDown = (p.Flags & 0x01) != 0, + FirstPageNumber = (p.Flags & 0x80) != 0 && p.FirstPage is >= -32768 and <= 32767 ? p.FirstPage : null, + }; + } + setup = setup with { FitToPage = fitToPage }; + if (margins is not null) + { + double Margin(double inches, double fallback) => double.IsFinite(inches) && inches is >= 0 and <= 50 ? inches * 72 : fallback; + setup = setup with + { + LeftMargin = Margin(margins[0], setup.LeftMargin), + RightMargin = Margin(margins[1], setup.RightMargin), + TopMargin = Margin(margins[2], setup.TopMargin), + BottomMargin = Margin(margins[3], setup.BottomMargin), + HeaderMargin = Margin(margins[4], setup.HeaderMargin), + FooterMargin = Margin(margins[5], setup.FooterMargin), + }; + } + if (options is { } o) + { + setup = setup with + { + CenterHorizontally = (o & 0x01) != 0, + CenterVertically = (o & 0x02) != 0, + PrintHeadings = (o & 0x04) != 0, + PrintGridLines = (o & 0x08) != 0, + }; + } + if (headerFooter is not null) + { + setup = setup with + { + HeaderFooter = headerFooter with + { + Pictures = _package.Target(partPath, headerPictures) is { } vml ? XlsxReader.HeaderPictures(_package, vml) : [], + }, + }; + } + return setup; + } + + // ───────────────────────── Records ───────────────────────── + + /// Counts a record against what one workbook may hold (as many as the XML elements an XLSX may). + private void Count() + { + if (++_records > _limits.MaxXmlElements) + throw DocumentLimits.Exceeded($"The workbook has more than {DocumentLimits.Count(_limits.MaxXmlElements)} records."); + } + + /// The records of one part, read one after the other. + private sealed class Records(Stream stream, string part, XlsbReader reader) + { + private readonly BufferedStream _stream = new(stream, 64 * 1024); + private byte[] _data = new byte[256]; + private int _length; + private bool _tooLarge; + + public int Type { get; private set; } + + /// Moves to the next record; false at the end of the part. + public bool Next() + { + var low = _stream.ReadByte(); + if (low < 0) + return false; + var type = low & 0x7F; + if ((low & 0x80) != 0) + type |= (Byte() & 0x7F) << 7; + var size = 0; + for (var i = 0; i < 4; i++) + { + var b = Byte(); + size |= (b & 0x7F) << (7 * i); + if ((b & 0x80) == 0 || i == 3) + break; + } + reader.Count(); + Type = type; + _tooLarge = size > MaxRecordBytes; + if (_tooLarge) + { + // Kept out of memory: only records the reader skips may be this large. + Skip(size); + _length = 0; + return true; + } + if (_data.Length < size) + _data = new byte[Math.Max(size, _data.Length * 2)]; + if (_stream.ReadAtLeast(_data.AsSpan(0, size), size, throwOnEndOfStream: false) != size) + throw Damaged(); + _length = size; + return true; + } + + /// The fields of the current record. + public Fields Fields() => _tooLarge + ? throw DocumentLimits.Exceeded($"A record of {part} holds more than {DocumentLimits.Megabytes(MaxRecordBytes)}.") + : new Fields(_data.AsSpan(0, _length), part); + + private int Byte() + { + var b = _stream.ReadByte(); + return b < 0 ? throw Damaged() : b; + } + + private void Skip(int count) + { + Span buffer = stackalloc byte[4096]; + while (count > 0) + { + var read = _stream.Read(buffer[..Math.Min(count, buffer.Length)]); + if (read <= 0) + throw Damaged(); + count -= read; + } + } + + private InvalidDataException Damaged() => new($"The part {part} of the workbook ends inside a record. The file may be damaged."); + } + + /// The fields of one record, read in order; reading past its end is damage. + private ref struct Fields(ReadOnlySpan data, string part) + { + private readonly ReadOnlySpan _data = data; + private int _at; + + public readonly int Left => _data.Length - _at; + + public byte U8() => Take(1)[0]; + + public ushort U16() => BinaryPrimitives.ReadUInt16LittleEndian(Take(2)); + + public short I16() => BinaryPrimitives.ReadInt16LittleEndian(Take(2)); + + public uint U32() => BinaryPrimitives.ReadUInt32LittleEndian(Take(4)); + + public int I32() => BinaryPrimitives.ReadInt32LittleEndian(Take(4)); + + public double F64() => BinaryPrimitives.ReadDoubleLittleEndian(Take(8)); + + public void Skip(int count) => Take(count); + + public ReadOnlySpan Bytes(int count) => Take(count); + + /// An XLWideString (or, , an XLNullableWideString whose 0xFFFFFFFF length is null). + public string? String(bool nullable = false) + { + var count = U32(); + if (nullable && count == 0xFFFFFFFF) + return null; + if (count > Left / 2) + throw Damaged(); + return Encoding.Unicode.GetString(Take((int)count * 2)); + } + + private ReadOnlySpan Take(int count) + { + if (count < 0 || count > Left) + throw Damaged(); + var span = _data.Slice(_at, count); + _at += count; + return span; + } + + private readonly InvalidDataException Damaged() => new($"A record of {part} in the workbook is shorter than its fields. The file may be damaged."); + } +} diff --git a/src/Filee.Engines/Office/Sheets/XlsxReader.cs b/src/Filee.Engines/Office/Sheets/XlsxReader.cs index fab1cc8e4..e2e954d49 100644 --- a/src/Filee.Engines/Office/Sheets/XlsxReader.cs +++ b/src/Filee.Engines/Office/Sheets/XlsxReader.cs @@ -592,7 +592,7 @@ private SheetPageSetup PageSetup(XElement xml, string partPath) DifferentFirst = On(hf.Attribute("differentFirst")), ScaleWithDocument = hf.Attribute("scaleWithDoc") is not { } scaleWith || On(scaleWith), AlignWithMargins = hf.Attribute("alignWithMargins") is not { } align || On(align), - Pictures = _package.Target(partPath, (string?)xml.Element(S + "legacyDrawingHF")?.Attribute(R + "id")) is { } vml ? HeaderPictures(vml) : [], + Pictures = _package.Target(partPath, (string?)xml.Element(S + "legacyDrawingHF")?.Attribute(R + "id")) is { } vml ? HeaderPictures(_package, vml) : [], }, }; } @@ -600,13 +600,13 @@ private SheetPageSetup PageSetup(XElement xml, string partPath) } /// The pictures of headers and footers: VML shapes named LH, CH, RH, LF, CF, RF (+ FIRST / EVEN). - private Dictionary HeaderPictures(string vmlPath) + internal static Dictionary HeaderPictures(OpcPackage package, string vmlPath) { var pictures = new Dictionary(StringComparer.OrdinalIgnoreCase); XDocument? vml; try { - vml = _package.Part(vmlPath); + vml = package.Part(vmlPath); } catch (InvalidDataException) { @@ -616,7 +616,7 @@ private SheetPageSetup PageSetup(XElement xml, string partPath) { var id = (string?)shape.Attribute("id"); var relation = (string?)shape.Element(V + "imagedata")?.Attribute(O + "relid") ?? (string?)shape.Element(V + "imagedata")?.Attribute(R + "id"); - if (id is null || _package.Target(vmlPath, relation) is not { } image || _package.Bytes(image, MaxPictureBytes) is not { } data) + if (id is null || package.Target(vmlPath, relation) is not { } image || package.Bytes(image, MaxPictureBytes) is not { } data) continue; var style = ((string?)shape.Attribute("style") ?? "").Split(';', StringSplitOptions.RemoveEmptyEntries) .Select(p => p.Split(':', 2)).Where(p => p.Length == 2).GroupBy(p => p[0].Trim(), StringComparer.OrdinalIgnoreCase) diff --git a/src/Filee.Engines/Office/Sheets/XlsxWriter.cs b/src/Filee.Engines/Office/Sheets/XlsxWriter.cs index c8fba27cc..e98283c39 100644 --- a/src/Filee.Engines/Office/Sheets/XlsxWriter.cs +++ b/src/Filee.Engines/Office/Sheets/XlsxWriter.cs @@ -28,6 +28,9 @@ internal static class XlsxWriter ["@"] = 49, }; + /// Excel's built-in short date, which it shows in the PC's regional format (#1315). + private const int ShortDateId = 14; + public static void Write(Workbook book, string path) { var sheets = book.Sheets.Count > 0 ? book.Sheets : [new Worksheet("Sheet1")]; @@ -188,7 +191,12 @@ public int Index(CellStyle style, string? format) { if (_xfs.TryGetValue((style, format), out var index)) return index; - var formatId = format is null ? 0 : BuiltInIds.TryGetValue(format, out var builtIn) ? builtIn : FormatId(format); + // The PC's short date (what format 14 is read as here) is written as format 14, not as a format of the + // file's own: Excel then shows the date in the short date of whoever opens the file. + var formatId = format is null ? 0 + : BuiltInIds.TryGetValue(format, out var builtIn) ? builtIn + : NumberFormatter.IsShortDate(format) ? ShortDateId + : FormatId(format); var fontId = Add(_fonts, Font(style)); var fillId = style.Fill is { } fill ? Add(_fills, $"""""") : 0; var borderId = Add(_borders, $"{Side("left", style.Left)}{Side("right", style.Right)}{Side("top", style.Top)}{Side("bottom", style.Bottom)}"); diff --git a/src/Filee.Engines/Office/SpreadsheetConverter.cs b/src/Filee.Engines/Office/SpreadsheetConverter.cs index a5754a46e..98e49393e 100644 --- a/src/Filee.Engines/Office/SpreadsheetConverter.cs +++ b/src/Filee.Engines/Office/SpreadsheetConverter.cs @@ -1,4 +1,4 @@ -// Spreadsheets between XLSX, XLS, ODS, CSV and TSV without Excel or LibreOffice (XLS / XLT and the XLSM / XLTX +// Spreadsheets between XLSX, XLS, ODS, CSV and TSV without Excel or LibreOffice (XLS / XLT, XLSB and the XLSM / XLTX // variants are read, not written). // 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 +12,7 @@ namespace Filee.Engines.Office; /// Converts between spreadsheet formats in-process. public sealed class SpreadsheetConverter : IConverter { - private static readonly string[] Sources = ["xlsx", "xlsm", "xltx", "xls", "xlt", "ods", "csv", "tsv"]; + private static readonly string[] Sources = ["xlsx", "xlsm", "xltx", "xlsb", "xls", "xlt", "ods", "csv", "tsv"]; private static readonly string[] Targets = ["xlsx", "ods", "csv", "tsv"]; public string Id => "spreadsheet"; @@ -25,7 +25,7 @@ [.. Sources.SelectMany(from => Targets.Where(to => to != from && !(to == "xlsx" .Select(to => new ConversionEdge(from, to)))]; public EngineStatus GetStatus() => - EngineStatus.Available("XLSX, XLS, ODS, CSV, TSV → XLSX, ODS, CSV, TSV", + EngineStatus.Available("XLSX, XLSB, XLS, ODS, CSV, TSV → XLSX, ODS, CSV, TSV", $"{EngineVersions.BuiltIn} · {EngineVersions.Library("ExcelDataReader", typeof(ExcelDataReader.ExcelReaderFactory))}"); public Task> ConvertAsync(ConversionStep step, IProgress? progress, CancellationToken cancellationToken) => @@ -79,6 +79,7 @@ internal static List Write(Workbook book, ConversionStep step) internal static Workbook Read(string path, string format) => format switch { "xls" or "xlt" => XlsReader.Read(path), + XlsbReader.Format => XlsbReader.Read(path), "ods" => OdsReader.Read(path), "csv" => CsvFormat.Read(path), "tsv" => CsvFormat.ReadTsv(path), diff --git a/src/Filee.Engines/Pdf/PdfBlankPages.cs b/src/Filee.Engines/Pdf/PdfBlankPages.cs index 1b80e7a98..e81cfe297 100644 --- a/src/Filee.Engines/Pdf/PdfBlankPages.cs +++ b/src/Filee.Engines/Pdf/PdfBlankPages.cs @@ -36,7 +36,7 @@ public static int Remove(string path) try { int removed; - using (var document = PdfReader.Open(fullPath, PdfDocumentOpenMode.Modify)) + using (var document = PdfObjectNesting.Open(fullPath, PdfDocumentOpenMode.Modify)) { var blank = Enumerable.Range(0, document.PageCount).Where(i => IsBlank(document.Pages[i])).ToList(); if (blank.Count == 0 || blank.Count == document.PageCount) diff --git a/src/Filee.Engines/Pdf/PdfObjectNesting.cs b/src/Filee.Engines/Pdf/PdfObjectNesting.cs new file mode 100644 index 000000000..80a679097 --- /dev/null +++ b/src/Filee.Engines/Pdf/PdfObjectNesting.cs @@ -0,0 +1,691 @@ +// PDFsharp's object parser reads arrays and dictionaries recursively, with no limit, and PdfReader.Open reads every +// object of a file at once, the ones packed in object streams and the ones nothing refers to included: about 1,900 +// "[" in one object (a few kilobytes) overflow a 1 MB stack, and a stack overflow ends the process with every +// conversion in it. Every PDFsharp open of a file Filee did not write itself goes through Open here, which reads the +// file first and refuses one nested more than MaxDepth deep with error.document_too_complex. PdfContentNesting does +// the same for content streams before PDFsharp's ContentReader. +// +// PDFsharp jumps to each object from the cross-reference table, so reading the file from front to back is not enough: +// a string that never closes, or the data of a stream, would hide an object from such a reading while PDFsharp still +// parses it. The check therefore reads from every "obj" and "trailer" keyword in the file, wherever it is (in a string, +// in stream data), the way PDFsharp's parser reads from there: strings with balanced parentheses and escapes, +// comments, hex strings and names, up to the end of the object (a keyword such as endobj or stream at the top level). +// Readings overlap only in damaged or crafted files; the bytes read in all are capped at a few times the file. +// +// Object streams hold objects without an "obj" keyword. PDFsharp finds them through the cross-reference stream and +// doesn't need /Type /ObjStm, so every stream whose dictionary has /N is unpacked (with PDFsharp's own filters, which +// it also applies without decrypting first, so the check sees exactly what the parser gets) and each object read from +// the offset the stream's header gives. + +using System.Globalization; +using System.Text; +using Filee.Engines.Documents; +using Microsoft.Win32.SafeHandles; +using PdfSharp.Pdf; +using PdfSharp.Pdf.Filters; +using PdfSharp.Pdf.IO; + +namespace Filee.Engines.Pdf; + +/// Opens PDFs with PDFsharp once their arrays and dictionaries are known to nest few enough levels. +internal static class PdfObjectNesting +{ + /// + /// Arrays and dictionaries inside each other at most. Real files nest a handful of levels (a page tree, an outline + /// and a structure tree link their entries by reference); PDFsharp overflowed a 1 MB stack at about 1,900. + /// + public const int MaxDepth = 256; + + /// How many times its size (and its unpacked object streams) a file may be read in all. + private const int MaxReadsPerByte = 4; + + /// What may always be read, whatever the file's size. + private const long ReadFloor = 16L * 1024 * 1024; + + /// Opens with PDFsharp after checking it. + /// The file nests too deeply. + public static PdfDocument Open(string path, PdfDocumentOpenMode mode, CancellationToken cancellationToken = default) + { + Check(path, cancellationToken); + return PdfReader.Open(path, mode); + } + + /// Opens a PDF in memory with PDFsharp after checking it. + /// The file nests too deeply. + public static PdfDocument Open(byte[] pdf, PdfDocumentOpenMode mode, CancellationToken cancellationToken = default) + { + Check(pdf, cancellationToken); + return PdfReader.Open(new MemoryStream(pdf), mode); + } + + /// Checks the PDF at , reading it in windows (it is not loaded whole). + /// The file nests too deeply. + public static void Check(string path, CancellationToken cancellationToken = default) + { + using var file = File.OpenHandle(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + new Checker(new Source(file), cancellationToken).Run(); + } + + /// Checks a PDF in memory. + /// The file nests too deeply. + public static void Check(byte[] pdf, CancellationToken cancellationToken = default) => + new Checker(new Source(pdf), cancellationToken).Run(); + + private static Core.Conversion.ConversionRefusedException TooDeep() => + DocumentLimits.Exceeded($"The PDF nests arrays or dictionaries more than {MaxDepth} deep."); + + // ───────────────────────── The check ───────────────────────── + + private sealed class Checker(Source file, CancellationToken cancellationToken) + { + /// Where the value of each numbered object starts (after "N G obj"); more than one after an update. + private readonly Dictionary> _objects = []; + + private long _read; + private long _allowed = file.Length * MaxReadsPerByte + ReadFloor; + + public void Run() + { + var starts = new List(); + Keywords("obj"u8, starts, numbered: true); + Keywords("trailer"u8, starts, numbered: false); + starts.Sort(); + foreach (var start in starts) + { + var (streamAt, hasN) = Read(file, start); + if (streamAt >= 0 && hasN) + ObjectStream(start, streamAt); + } + } + + /// Every standing as a token of its own, with where the reading starts after it. + private void Keywords(ReadOnlySpan keyword, List starts, bool numbered) + { + for (var at = file.IndexOf(keyword, 0); at >= 0; at = file.IndexOf(keyword, at + 1)) + { + if (!Ends(file[at - 1]) || !Ends(file[at + keyword.Length])) + continue; + var start = at + keyword.Length; + starts.Add(start); + if (numbered && Number(at) is { } number) + { + if (!_objects.TryGetValue(number, out var list)) + _objects[number] = list = []; + list.Add(start); + } + if ((starts.Count & 0xFFF) == 0) + cancellationToken.ThrowIfCancellationRequested(); + } + } + + /// The object number of "N G obj" whose keyword is at . + private long? Number(long keyword) + { + var at = keyword - 1; + return Digits(ref at, out _, out _) && Digits(ref at, out var from, out var to) + && long.TryParse(Text(file, from, to - from), NumberStyles.None, CultureInfo.InvariantCulture, out var number) + ? number + : null; + } + + /// Steps back over white space and then up to 12 digits, which are at [from, to); false without any digit. + private bool Digits(ref long at, out long from, out long to) + { + while (at >= 0 && IsSpace(file[at])) + at--; + to = at + 1; + while (at >= 0 && file[at] is >= '0' and <= '9' && to - at <= 12) + at--; + from = at + 1; + return from < to; + } + + /// + /// Reads one object as PDFsharp would from , refusing it when it nests too deeply. Returns + /// where its stream data begins (after "stream", else -1) and whether its dictionary has /N. An object after + /// "obj" ends at a keyword such as endobj; with (in an object stream) after its value. + /// + private (long StreamAt, bool HasN) Read(Source source, long at, bool oneValue = false) + { + var checkpoint = at; + var depth = 0; + var topIsDictionary = false; + var hasN = false; + while (true) + { + if (at - checkpoint > 1 << 20) + { + Spend(at - checkpoint); + checkpoint = at; + } + var kind = Next(source, ref at, out var start, out var length); + switch (kind) + { + case Kind.End: + Spend(at - checkpoint); + return (-1, hasN); + case Kind.DictionaryOpen or Kind.ArrayOpen: + if (++depth > MaxDepth) + throw TooDeep(); + if (depth == 1) + topIsDictionary = kind == Kind.DictionaryOpen; + break; + case Kind.DictionaryClose or Kind.ArrayClose: + if (depth == 0) + { + Spend(at - checkpoint); + return (-1, hasN); // a stray closer: the parser stops with an error + } + depth--; + break; + case Kind.Name when depth == 1 && topIsDictionary && NameIs(source, start, length, "N"): + hasN = true; + break; + case Kind.Regular when depth == 0 && length <= 9: + var word = Text(source, start, length); + if (word is "stream" or "endobj" or "obj" or "endstream" or "trailer" or "xref" or "startxref") + { + Spend(at - checkpoint); + return (word == "stream" ? at : -1, hasN); + } + break; + } + if (oneValue && depth == 0) + { + Spend(at - checkpoint); + return (-1, hasN); + } + } + } + + /// Counts bytes read; refuses a file that would be read too many times over. + private void Spend(long bytes) + { + cancellationToken.ThrowIfCancellationRequested(); + _read += bytes; + if (_read > _allowed) + throw DocumentLimits.Exceeded("The PDF's objects overlap each other too often to be checked safely."); + } + + // ───────────────────────── Object streams ───────────────────────── + + /// Unpacks the stream of the object at and reads the objects its header lists. + private void ObjectStream(long start, long streamAt) + { + var at = start; + if (Value(file, ref at) is not Dictionary dictionary) + return; + var counts = Integers(dictionary.GetValueOrDefault("N")); + if (counts.Count == 0) + return; // no object can be looked up in it + // PDFsharp takes /N and /First only as integers in the dictionary itself (it fails on one by reference); + // references are followed here all the same. + var firsts = dictionary.TryGetValue("First", out var first) ? Integers(first) : [0]; + if (firsts.Count == 0) + return; + + if (Unpack(dictionary, streamAt) is not { } data) + return; // PDFsharp can't unpack it either, so it parses nothing of it + _allowed += (long)data.Length * MaxReadsPerByte; + var unpacked = new Source(data); + + // The header: pairs of object number and offset (from /First), as many as /N says (and the data can hold). + var offsets = new List(); + var pairs = Math.Min(counts.Max(), data.Length / 4 + 1); + long position = 0; + for (var i = 0L; i < pairs * 2; i++) + { + if (Next(unpacked, ref position, out var tokenStart, out var length) != Kind.Regular + || !long.TryParse(Text(unpacked, tokenStart, length), NumberStyles.Integer, CultureInfo.InvariantCulture, out var number)) + break; + if (i % 2 == 1) + offsets.Add(number); + } + foreach (var from in firsts) + { + foreach (var offset in offsets) + { + var objectAt = from + offset; + if (objectAt >= 0 && objectAt < data.Length) + Read(unpacked, objectAt, oneValue: true); + } + } + } + + /// The stream's data unpacked with PDFsharp's filters, or null when they can't unpack it. + private byte[]? Unpack(Dictionary dictionary, long streamAt) + { + // The data starts after the end of line that follows "stream". + var dataAt = streamAt; + if (file[dataAt] == '\r') + dataAt++; + if (file[dataAt] == '\n') + dataAt++; + // Its end: the longest of /Length and the next "endstream" (PDFsharp's filters stop at the end of the + // packed data, so more bytes than it takes never unpack to less). + var end = file.IndexOf("endstream"u8, dataAt); + foreach (var length in Integers(dictionary.GetValueOrDefault("Length"))) + { + if (length >= 0 && length <= file.Length - dataAt) + end = Math.Max(end, dataAt + length); + } + if (end < 0) + end = file.Length; + if (end - dataAt > DocumentLimits.Default.MaxPartBytes) + throw DocumentLimits.Exceeded($"An object stream of the PDF is larger than {DocumentLimits.Megabytes(DocumentLimits.Default.MaxPartBytes)}."); + var data = file.Read(dataAt, (int)(end - dataAt)); + + var filters = Resolve(dictionary.GetValueOrDefault("Filter")) switch + { + PdfNameValue name => [name.Value], + List list => list.Select(Resolve).OfType().Select(n => n.Value).ToList(), + _ => new List(), + }; + var parameters = Resolve(dictionary.GetValueOrDefault("DecodeParms")) switch + { + Dictionary one => [one], + List list => list.Select(p => Resolve(p) as Dictionary).ToList(), + _ => new List?>(), + }; + try + { + for (var i = 0; i < filters.Count; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + var parms = i < parameters.Count && parameters[i] is { } given ? new FilterParms(Parameters(given)) : null; + data = (parms is null ? Filtering.Decode(data, "/" + filters[i]) : Filtering.Decode(data, "/" + filters[i], parms))!; + if (data is null) + return null; + } + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + return null; + } + return data; + } + + /// The integer entries of decode parameters (/Predictor, /Columns ...) as PDFsharp's filters take them. + private static PdfDictionary Parameters(Dictionary given) + { + var parameters = new PdfDictionary(); + foreach (var (key, value) in given) + { + if (value is long number && number is >= int.MinValue and <= int.MaxValue) + parameters.Elements.SetInteger("/" + key, (int)number); + } + return parameters; + } + + /// The integers stands for: itself, or what the objects it refers to hold. + private List Integers(object? value) => value switch + { + long number => [number], + Reference reference => Targets(reference).OfType().ToList(), + _ => [], + }; + + /// , or the first thing the object it refers to holds. + private object? Resolve(object? value) => value is Reference reference ? Targets(reference).FirstOrDefault() : value; + + /// The values of every object numbered as says (more than one after an update). + private IEnumerable Targets(Reference reference) + { + if (!_objects.TryGetValue(reference.Number, out var starts)) + yield break; + foreach (var start in starts) + { + var at = start; + if (Value(file, ref at) is var value and not Reference) + yield return value; + } + } + } + + // ───────────────────────── Values (object stream dictionaries) ───────────────────────── + + private sealed record PdfNameValue(string Value); + + private sealed record Reference(long Number); + + /// Tokens one reads at most (the dictionaries it is for have a few dozen). + private const int MaxValueTokens = 100_000; + + /// + /// One value read from : an integer (long), a real (double), a name, an array (list), a dictionary + /// (by key without the slash), a reference, or null for strings, keywords and damage. Past + /// tokens the rest is left out. + /// + private static object? Value(Source source, ref long at) + { + var tokens = MaxValueTokens; + return Value(source, ref at, 0, ref tokens); + } + + private static object? Value(Source source, ref long at, int depth, ref int tokens) + { + if (--tokens < 0) + return null; + var kind = Next(source, ref at, out var start, out var length); + switch (kind) + { + case Kind.DictionaryOpen: + { + if (depth >= MaxDepth) + throw TooDeep(); + var dictionary = new Dictionary(); + while (tokens-- > 0) + { + var before = at; + var next = Next(source, ref at, out var keyStart, out var keyLength); + if (next is Kind.DictionaryClose or Kind.End) + return dictionary; + if (next != Kind.Name) + { + at = before; + Value(source, ref at, depth + 1, ref tokens); // damage, which the parser fails on anyway + continue; + } + var key = NameText(source, keyStart, keyLength); + dictionary.TryAdd(key, Value(source, ref at, depth + 1, ref tokens)); + } + return dictionary; + } + case Kind.ArrayOpen: + { + if (depth >= MaxDepth) + throw TooDeep(); + var list = new List(); + while (tokens-- > 0) + { + var before = at; + if (Next(source, ref at, out _, out _) is Kind.ArrayClose or Kind.End) + return list; + at = before; + list.Add(Value(source, ref at, depth + 1, ref tokens)); + } + return list; + } + case Kind.Name: + return new PdfNameValue(NameText(source, start, length)); + case Kind.Regular when length <= 32: + var text = Text(source, start, length); + if (long.TryParse(text, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var number)) + { + // "N G R" is a reference. + var after = at; + if (Next(source, ref after, out var genStart, out var genLength) == Kind.Regular + && long.TryParse(Text(source, genStart, Math.Min(genLength, 12)), NumberStyles.None, CultureInfo.InvariantCulture, out _) + && Next(source, ref after, out var rStart, out var rLength) == Kind.Regular && rLength == 1 && source[rStart] == 'R') + { + at = after; + return new Reference(number); + } + return number; + } + return double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out var real) ? real : null; + default: + return null; + } + } + + // ───────────────────────── Tokens ───────────────────────── + + private enum Kind { End, DictionaryOpen, DictionaryClose, ArrayOpen, ArrayClose, Name, String, Regular, Other } + + /// The next token from , as PDFsharp's lexer splits the file; white space and comments are skipped. + private static Kind Next(Source source, ref long at, out long start, out long length) + { + while (true) + { + var c = source[at]; + start = at; + if (c < 0) + { + length = 0; + return Kind.End; + } + if (IsSpace(c)) + { + at++; + continue; + } + if (c == '%') + { + while (source[at] is >= 0 and not '\n' and not '\r') + at++; + continue; + } + Kind kind; + switch (c) + { + case '(': + at = AfterString(source, at); + kind = Kind.String; + break; + case '<' when source[at + 1] == '<': + at += 2; + kind = Kind.DictionaryOpen; + break; + case '<': + while (source[at] is >= 0 and not '>') + at++; + if (source[at] == '>') + at++; + kind = Kind.String; + break; + case '>' when source[at + 1] == '>': + at += 2; + kind = Kind.DictionaryClose; + break; + case '[': + at++; + kind = Kind.ArrayOpen; + break; + case ']': + at++; + kind = Kind.ArrayClose; + break; + case '/': + at++; + while (IsRegular(source[at])) + at++; + kind = Kind.Name; + break; + default: + if (IsRegular(c)) + { + while (IsRegular(source[at])) + at++; + kind = Kind.Regular; + } + else + { + at++; // ")", a lone ">", "{" or "}": the parser fails on them + kind = Kind.Other; + } + break; + } + length = at - start; + return kind; + } + } + + /// After the literal string at : parentheses in it balance, a backslash escapes the next byte. + private static long AfterString(Source source, long at) + { + var open = 0; + for (; ; at++) + { + switch (source[at]) + { + case < 0: + return at; + case '\\': + at++; + break; + case '(': + open++; + break; + case ')' when --open == 0: + return at + 1; + } + } + } + + /// The token's bytes as text (Latin-1), at most 128 of them. + private static string Text(Source source, long start, long length) + { + var count = (int)Math.Min(length, 128); + var chars = new char[count]; + for (var i = 0; i < count; i++) + chars[i] = (char)source[start + i]; + return new string(chars); + } + + /// A name token without its slash, #xx escapes decoded. + private static string NameText(Source source, long start, long length) + { + var raw = Text(source, start + 1, length - 1); + if (!raw.Contains('#')) + return raw; + var text = new StringBuilder(raw.Length); + for (var i = 0; i < raw.Length; i++) + { + if (raw[i] == '#' && i + 2 < raw.Length && int.TryParse(raw.AsSpan(i + 1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var code)) + { + text.Append((char)code); + i += 2; + } + else + { + text.Append(raw[i]); + } + } + return text.ToString(); + } + + private static bool NameIs(Source source, long start, long length, string name) => + length <= 3 * name.Length + 1 && NameText(source, start, length) == name; + + private static bool IsSpace(int c) => c is 0 or 9 or 10 or 12 or 13 or 32; + + private static bool IsDelimiter(int c) => c is '(' or ')' or '<' or '>' or '[' or ']' or '{' or '}' or '/' or '%'; + + private static bool IsRegular(int c) => c >= 0 && !IsSpace(c) && !IsDelimiter(c); + + /// True where a token ends: white space, a delimiter, or outside the data. + private static bool Ends(int c) => c < 0 || IsSpace(c) || IsDelimiter(c); + + // ───────────────────────── Bytes ───────────────────────── + + /// The bytes of a PDF in memory, or of a file read through a window that follows the reading. + private sealed class Source + { + private const int WindowBytes = 1 << 20; + + /// Kept before a position when the window moves, for the number before "obj". + private const int Behind = 64; + + private readonly SafeFileHandle? _file; + private readonly byte[] _window; + private long _start; + private int _count; + + public Source(byte[] data) + { + _window = data; + _count = data.Length; + Length = data.Length; + } + + public Source(SafeFileHandle file) + { + _file = file; + Length = RandomAccess.GetLength(file); + _window = new byte[WindowBytes]; + } + + public long Length { get; } + + /// The byte at , or -1 outside the data. + public int this[long at] + { + get + { + var i = at - _start; + return (ulong)i < (ulong)_count ? _window[i] : Load(at); + } + } + + private int Load(long at) + { + if (_file is null || at < 0 || at >= Length) + return -1; + Move(at); + var i = at - _start; + return i < _count ? _window[i] : -1; + } + + /// Reads the window around (from a little before it). + private void Move(long at) + { + _start = Math.Max(0, at - Behind); + _count = Fill(_window, _start); + } + + private int Fill(Span buffer, long at) + { + var done = 0; + while (done < buffer.Length) + { + var read = RandomAccess.Read(_file!, buffer[done..], at + done); + if (read <= 0) + break; + done += read; + } + return done; + } + + /// Where next occurs from , or -1. + public long IndexOf(ReadOnlySpan needle, long from) + { + if (_file is null) + { + if (from < 0 || from >= _count) + return -1; + var found = _window.AsSpan((int)from).IndexOf(needle); + return found < 0 ? -1 : from + found; + } + while (from >= 0 && from < Length) + { + if ((ulong)(from - _start) >= (ulong)_count) + Move(from); + var offset = (int)(from - _start); + if (offset >= _count) + return -1; + var found = _window.AsSpan(offset, _count - offset).IndexOf(needle); + if (found >= 0) + return from + found; + if (_start + _count >= Length) + return -1; + // The next window overlaps this one by the needle, so a match across the edge is found. + from = Math.Max(from + 1, _start + _count - needle.Length + 1); + Move(from); + } + return -1; + } + + /// bytes from (fewer at the end of the data). + public byte[] Read(long at, int count) + { + count = (int)Math.Max(0, Math.Min(count, Length - at)); + var bytes = new byte[count]; + if (_file is null) + Array.Copy(_window, at, bytes, 0, count); + else + Array.Resize(ref bytes, Fill(bytes, at)); + return bytes; + } + } +} diff --git a/src/Filee.Engines/Pdf/PdfPrintableAnnotations.cs b/src/Filee.Engines/Pdf/PdfPrintableAnnotations.cs index bd2cc5fed..5dda701e1 100644 --- a/src/Filee.Engines/Pdf/PdfPrintableAnnotations.cs +++ b/src/Filee.Engines/Pdf/PdfPrintableAnnotations.cs @@ -42,9 +42,9 @@ internal static PdfAPreparation Prepare(string input, string folder, Cancellatio bool? hasFormFields = null; try { - if (!PageTreeIsSafe(input)) + if (!PageTreeIsSafe(input, cancellationToken)) return new(null, null); - using var document = PdfReader.Open(input, PdfDocumentOpenMode.Modify); + using var document = PdfObjectNesting.Open(input, PdfDocumentOpenMode.Modify, cancellationToken); hasFormFields = HasFormFields(document); var changed = 0; var seen = new HashSet(ReferenceEqualityComparer.Instance); // one annotation on several pages @@ -108,9 +108,9 @@ private static bool HasFormFields(PdfDocument document) => /// changes, recursing without such checks, so a page tree that contains itself would overflow the stack. /// Opening for import doesn't flatten it. /// - private static bool PageTreeIsSafe(string input) + private static bool PageTreeIsSafe(string input, CancellationToken cancellationToken) { - using var document = PdfReader.Open(input, PdfDocumentOpenMode.Import); + using var document = PdfObjectNesting.Open(input, PdfDocumentOpenMode.Import, cancellationToken); if (PdfCompressor.Resolve(document.Internals.Catalog.Elements["/Pages"]) is not PdfDictionary root) return false; var visited = new HashSet(ReferenceEqualityComparer.Instance); diff --git a/src/Filee.Engines/Pdf/PdfSharpConverter.cs b/src/Filee.Engines/Pdf/PdfSharpConverter.cs index 2ea4932be..006e6525a 100644 --- a/src/Filee.Engines/Pdf/PdfSharpConverter.cs +++ b/src/Filee.Engines/Pdf/PdfSharpConverter.cs @@ -438,11 +438,13 @@ internal static void CopyWritable(string source, string destination) /// Opens a PDF. A file PDFsharp reads is opened as it is; one it refuses gets one mended try (, /// issue #1175), and when that does not help either the original error is thrown. The file on disk is never changed. /// is the mended file when that was needed (else null), for code that reads the file a - /// second time (PDFsharp's XPdfForm) and must read the mended bytes, not the file. + /// second time (PDFsharp's XPdfForm) and must read the mended bytes, not the file. Both are checked for nesting + /// PDFsharp's parser can't take first (). /// private static PdfDocument OpenTolerant(string path, PdfDocumentOpenMode mode, CancellationToken cancellationToken, out byte[]? mended) { mended = null; + PdfObjectNesting.Check(path, cancellationToken); try { return PdfReader.Open(path, mode); @@ -454,7 +456,7 @@ private static PdfDocument OpenTolerant(string path, PdfDocumentOpenMode mode, C throw; try { - var document = PdfReader.Open(new MemoryStream(repaired), mode); + var document = PdfObjectNesting.Open(repaired, mode, cancellationToken); mended = repaired; return document; } diff --git a/src/Filee.Engines/Vector/GhostscriptConverter.cs b/src/Filee.Engines/Vector/GhostscriptConverter.cs index edf79674c..9fb87e6d1 100644 --- a/src/Filee.Engines/Vector/GhostscriptConverter.cs +++ b/src/Filee.Engines/Vector/GhostscriptConverter.cs @@ -14,6 +14,7 @@ using Filee.Core.Presets; using Filee.Engines.Infrastructure; using Filee.Engines.Magick; +using Filee.Engines.Pdf; using ImageMagick; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; @@ -195,7 +196,7 @@ internal static bool DeclaresPdfA(string path) { try { - using var document = PdfReader.Open(path, PdfDocumentOpenMode.Import); + using var document = PdfObjectNesting.Open(path, PdfDocumentOpenMode.Import); if (document.Internals.Catalog.Elements.GetDictionary("/Metadata")?.Stream?.UnfilteredValue is not { } xmp) return false; return Encoding.UTF8.GetString(xmp).Contains("pdfaid:part", StringComparison.Ordinal); @@ -332,7 +333,7 @@ private static async Task> ToPngAsync(string gs, Conversio return null; try { - using var document = PdfReader.Open(step.InputPath, PdfDocumentOpenMode.Import); + using var document = PdfObjectNesting.Open(step.InputPath, PdfDocumentOpenMode.Import); return PageRange.Parse(step.Preset.Pdf.PageRange, document.PageCount); } catch (Exception ex) when (ex is not OperationCanceledException) diff --git a/src/Filee.Engines/Xps/XpsDocument.cs b/src/Filee.Engines/Xps/XpsDocument.cs new file mode 100644 index 000000000..b2e09be7f --- /dev/null +++ b/src/Filee.Engines/Xps/XpsDocument.cs @@ -0,0 +1,346 @@ +// Filee's own XPS and OpenXPS reader (XPS 1.0, ECMA-388 OpenXPS): the package's FixedDocumentSequence (found by the +// fixed representation relationship of either spec, else by its .fdseq name), its FixedDocuments and their pages, read +// one page at a time as they are drawn (XpsPageReader → XpsPageWriter → PageContent), so pictures of a few pages don't +// read the whole document. The drawing converter writes the pages as PDF (PdfPageWriter, fonts subset), PNG and SVG. +// +// Embedded fonts (obfuscated or not) are checked by Filee's own SFNT parser before Skia sees them; a run whose font is +// missing or damaged draws its UnicodeString with an installed font. Pictures: PNG, JPEG, GIF and BMP through Skia, +// TIFF through ImageMagick; their resolution sets their size (96 dpi when they have none). JPEG XR pictures (HD Photo, +// which Windows' XPS writer may use) and pictures that are damaged or claim more than MaxPicturePixels are left out and +// counted: the converter names them in the file's result. +// +// Not drawn: IsSideways glyphs are drawn upright, the Triangle line cap is flat, scRGB gradients interpolate in sRGB, +// ICC profiles of ContextColor and ColorConvertedBitmap are not applied, and print tickets, signatures, document +// structure and story fragments are not read. + +using System.Buffers.Binary; +using System.Xml.Linq; +using Filee.Core.Conversion; +using Filee.Engines.Documents; +using Filee.Engines.Drawings; +using Filee.Engines.Fonts; +using Filee.Engines.Rendering; +using Filee.Engines.Typesetting; +using ImageMagick; +using SkiaSharp; + +namespace Filee.Engines.Xps; + +internal sealed class XpsDocument : IFixedPages +{ + /// XPS units (1/96 inch) → points. + public const float PointsPerUnit = 72f / 96f; + + /// Letter in 1/96 inch, for a page that gives no size. + private static readonly SKSize DefaultPage = new(816, 1056); + + private readonly XpsPackage _package; + private readonly List _pages = []; + private readonly HashSet _targets = new(StringComparer.Ordinal); + private readonly HashSet _destinations = new(StringComparer.Ordinal); + private readonly Dictionary _fonts = new(StringComparer.OrdinalIgnoreCase); + private readonly Dictionary _images = new(StringComparer.OrdinalIgnoreCase); + private readonly Dictionary _dictionaries = new(StringComparer.OrdinalIgnoreCase); + private readonly List _owned = []; + private PageFont? _fallback; + private bool _fallbackLooked; + private int _picturesLeftOut; + + private sealed record PageReference(string Part, SKSize? Size); + + private XpsDocument(XpsPackage package, XpsLimits limits) + { + _package = package; + Budget = new XpsBudget(limits); + } + + public XpsBudget Budget { get; } + + public int Count => _pages.Count; + + public string? Title { get; private set; } + + /// The names links can point to (FixedDocument LinkTargets). + public IReadOnlySet Targets => _targets; + + /// Opens an XPS or OpenXPS file and reads its page list. + public static XpsDocument Open(string path, CancellationToken cancellationToken = default, XpsLimits? limits = null) + { + limits ??= XpsLimits.Default; + var package = XpsPackage.Open(path, limits.Document); + var document = new XpsDocument(package, limits); + try + { + document.ReadStructure(cancellationToken); + return document; + } + catch + { + document.Dispose(); + throw; + } + } + + private void ReadStructure(CancellationToken cancellationToken) + { + var sequencePart = _package.RootRelationships("/fixedrepresentation").FirstOrDefault(_package.Has) + ?? _package.Names.FirstOrDefault(n => n.EndsWith(".fdseq", StringComparison.OrdinalIgnoreCase)) + ?? throw new InvalidDataException("The file is not an XPS document: it has no fixed document sequence."); + var sequence = _package.Xml(sequencePart) ?? throw new InvalidDataException("The XPS document's page list is missing."); + IEnumerable documents = sequence.Name.LocalName switch + { + "FixedDocumentSequence" => sequence.Elements() + .Where(e => e.Name.LocalName == "DocumentReference") + .Select(e => XpsPackage.Resolve(sequencePart, (string?)e.Attribute("Source"))) + .OfType(), + "FixedDocument" => [sequencePart], + _ => throw new InvalidDataException("The file is not an XPS document: its page list is no fixed document sequence."), + }; + foreach (var part in documents) + { + cancellationToken.ThrowIfCancellationRequested(); + if (_package.Xml(part) is not { Name.LocalName: "FixedDocument" } fixedDocument) + continue; + foreach (var content in fixedDocument.Elements().Where(e => e.Name.LocalName == "PageContent")) + { + if (XpsPackage.Resolve(part, (string?)content.Attribute("Source")) is not { } page) + continue; + if (_pages.Count >= Budget.Limits.MaxPages) + throw DocumentLimits.Exceeded($"The document has more than {DocumentLimits.Count(Budget.Limits.MaxPages)} pages."); + var width = XpsValues.Number((string?)content.Attribute("Width")); + var height = XpsValues.Number((string?)content.Attribute("Height")); + _pages.Add(new PageReference(page, width > 0 && height > 0 ? new SKSize(width.Value, height.Value) : null)); + foreach (var target in content.Descendants().Where(e => e.Name.LocalName == "LinkTarget")) + { + if ((string?)target.Attribute("Name") is { Length: > 0 } name) + _targets.Add(name); + } + } + } + if (_pages.Count == 0) + throw new InvalidDataException("The XPS document has no pages."); + Title = ReadTitle(); + } + + private string? ReadTitle() + { + var core = _package.RootRelationships("/metadata/core-properties").FirstOrDefault(_package.Has); + var title = _package.Xml(core)?.Elements().FirstOrDefault(e => e.Name.LocalName == "title")?.Value.Trim(); + return string.IsNullOrEmpty(title) ? null : title; + } + + // ───────────────────────── Pages ───────────────────────── + + /// Page 's elements and its size in 1/96 inch. + public (XpsCanvas Canvas, SKSize Size) ReadPage(int index, CancellationToken cancellationToken = default) + { + var reference = _pages[index]; + var page = _package.Xml(reference.Part); + if (page is not { Name.LocalName: "FixedPage" }) + throw new InvalidDataException($"The XPS page {reference.Part} is missing or damaged."); + var width = XpsValues.Number((string?)page.Attribute("Width")); + var height = XpsValues.Number((string?)page.Attribute("Height")); + var size = width > 0 && height > 0 ? new SKSize(width.Value, height.Value) : reference.Size ?? DefaultPage; + var max = Budget.Limits.MaxPageSide / PointsPerUnit; + size = new SKSize(Math.Min(size.Width, max), Math.Min(size.Height, max)); + return (new XpsPageReader(this, reference.Part, cancellationToken).Read(page), size); + } + + /// Page drawn into the page model. + public PageContent Page(int index, CancellationToken cancellationToken = default) + { + var (canvas, size) = ReadPage(index, cancellationToken); + return XpsPageWriter.Write(canvas, size, this); + } + + /// True the first time a link target is placed (a PDF destination is defined once). + internal bool PlaceDestination(string name) => _targets.Contains(name) && _destinations.Add(name); + + public IReadOnlyList LeftOut => + _picturesLeftOut > 0 ? [new LeftOutPart("notice.renderer_left_out.pictures", _picturesLeftOut)] : []; + + // ───────────────────────── Resources ───────────────────────── + + /// Keeps until the document is disposed (the pages draw with it). + internal T Track(T owned) where T : IDisposable + { + _owned.Add(owned); + return owned; + } + + /// The root of a remote resource dictionary part. + internal XElement? Dictionary(string part) + { + if (!_dictionaries.TryGetValue(part, out var dictionary)) + _dictionaries[part] = dictionary = _package.Xml(part) is { Name.LocalName: "ResourceDictionary" } root ? root : null; + return dictionary; + } + + /// An embedded font (face of a collection); null when it is missing or damaged. + internal PageFont? Font(string? part, string? fragment) + { + if (part is null) + return null; + var face = int.TryParse(fragment, out var index) && index is >= 0 and < 256 ? index : 0; + var key = $"{part}#{face}"; + if (_fonts.TryGetValue(key, out var font)) + return font; + font = null; + if (_package.Font(part) is { } data) + { + try + { + // Filee's own parser reads the tables before Skia or HarfBuzz do. + SfntFont.ReadFace(data, face); + font = PageFont.FromData(data, face); + _ = font.Typeface; + Track(font); + } + catch (Exception ex) when (ex is InvalidDataException or ArgumentException or IndexOutOfRangeException or OverflowException) + { + font?.Dispose(); + font = null; + } + } + _fonts[key] = font; + return font; + } + + /// An installed font for runs whose own font is missing. + internal PageFont? FallbackFont + { + get + { + if (!_fallbackLooked) + { + _fallbackLooked = true; + try + { + _fallback = new FontResolver().Resolve(null, FontStyle.Regular)?.Font; + } + catch (InvalidDataException) + { + _fallback = null; + } + } + return _fallback; + } + } + + /// A picture part; null (and counted as left out) when it can't be drawn. + internal XpsImage? Image(string? part) + { + if (part is null) + return null; + if (_images.TryGetValue(part, out var image)) + return image; + image = Decode(_package.Bytes(part, Budget.Limits.Document.MaxPartBytes)); + if (image is null) + _picturesLeftOut++; + else + Track(image.Image); + _images[part] = image; + return image; + } + + private XpsImage? Decode(byte[]? bytes) + { + if (bytes is null or { Length: < 8 }) + return null; + var maxPixels = Budget.Limits.Document.MaxPicturePixels; + try + { + if (bytes is [0x49, 0x49, 0x2A, 0x00, ..] or [0x4D, 0x4D, 0x00, 0x2A, ..]) + { + var settings = new MagickReadSettings { Format = MagickFormat.Tiff }; + var info = new MagickImageInfo(bytes, settings); + if ((long)info.Width * info.Height > maxPixels || info.Width == 0 || info.Height == 0) + return null; + using var tiff = new MagickImage(bytes, settings); + var dpi = Resolution(tiff.Density); + var png = tiff.ToByteArray(MagickFormat.Png); + return SKImage.FromEncodedData(png) is { } decoded ? Sized(decoded, dpi) : null; + } + // PNG, JPEG, GIF, BMP; anything else (JPEG XR among them) has no codec here. + if (bytes is not ([0x89, 0x50, 0x4E, 0x47, ..] or [0xFF, 0xD8, 0xFF, ..] or [0x47, 0x49, 0x46, 0x38, ..] or [0x42, 0x4D, ..])) + return null; + using var data = SKData.CreateCopy(bytes); + using (var codec = SKCodec.Create(data)) + { + if (codec is null || codec.Info.Width <= 0 || codec.Info.Height <= 0 || (long)codec.Info.Width * codec.Info.Height > maxPixels) + return null; + } + return SKImage.FromEncodedData(data) is { } image ? Sized(image, PngResolution(bytes) ?? JpegResolution(bytes) ?? (96, 96)) : null; + } + catch (MagickException) + { + return null; + } + } + + private static XpsImage Sized(SKImage image, (double X, double Y) dpi) => + new(image, (float)(image.Width * 96 / dpi.X), (float)(image.Height * 96 / dpi.Y)); + + /// A TIFF's resolution in dots per inch; 96 when it gives none. + private static (double X, double Y) Resolution(Density density) + { + var factor = density.Units switch + { + DensityUnit.PixelsPerInch => 1, + DensityUnit.PixelsPerCentimeter => 2.54, + _ => 0, + }; + return factor > 0 && density.X > 1 && density.Y > 1 ? (density.X * factor, density.Y * factor) : (96, 96); + } + + /// A PNG's pHYs chunk in dots per inch; null when it has none in metres. + internal static (double X, double Y)? PngResolution(ReadOnlySpan png) + { + if (png is not [0x89, 0x50, 0x4E, 0x47, ..]) + return null; + var at = 8; + // Chunks up to the image data, a few dozen at most. + for (var chunks = 0; chunks < 64 && at + 12 <= png.Length; chunks++) + { + var length = BinaryPrimitives.ReadUInt32BigEndian(png[at..]); + var type = png.Slice(at + 4, 4); + if (type.SequenceEqual("IDAT"u8) || length > int.MaxValue - 12 || at + 12 + length > png.Length) + return null; + if (type.SequenceEqual("pHYs"u8) && length >= 9 && png[at + 16] == 1) + { + var x = BinaryPrimitives.ReadUInt32BigEndian(png[(at + 8)..]) * 0.0254; + var y = BinaryPrimitives.ReadUInt32BigEndian(png[(at + 12)..]) * 0.0254; + return x >= 1 && y >= 1 ? (x, y) : null; + } + at += 12 + (int)length; + } + return null; + } + + /// A JPEG's JFIF density in dots per inch; null when it has none in inches or centimetres. + internal static (double X, double Y)? JpegResolution(ReadOnlySpan jpeg) + { + // APP0 "JFIF\0": version (2), units (1), x and y density (2 each). + if (jpeg.Length < 18 || jpeg is not [0xFF, 0xD8, 0xFF, 0xE0, ..] || !jpeg.Slice(6, 5).SequenceEqual("JFIF\0"u8)) + return null; + var units = jpeg[13]; + var x = BinaryPrimitives.ReadUInt16BigEndian(jpeg[14..]); + var y = BinaryPrimitives.ReadUInt16BigEndian(jpeg[16..]); + if (x == 0 || y == 0) + return null; + return units switch + { + 1 => (x, y), + 2 => (x * 2.54, y * 2.54), + _ => null, + }; + } + + public void Dispose() + { + foreach (var owned in _owned) + owned.Dispose(); + _owned.Clear(); + _package.Dispose(); + } +} diff --git a/src/Filee.Engines/Xps/XpsGeometry.cs b/src/Filee.Engines/Xps/XpsGeometry.cs new file mode 100644 index 000000000..2d2190886 --- /dev/null +++ b/src/Filee.Engines/Xps/XpsGeometry.cs @@ -0,0 +1,380 @@ +// Geometry of XPS paths and clips: the abbreviated syntax ("F1 M 0,0 L 10,0 A 5,5 0 0 1 20,0 Z", XPS 1.0 §5.2 / +// ECMA-388 §15.2.2) and PathGeometry elements with PathFigure and its segments. A figure that isn't filled +// (IsFilled="false") is only stroked, a segment that isn't stroked (IsStroked="false") only filled, so a geometry is +// two Skia paths: what is filled and what is stroked (the same one when everything is both). +// +// Data that breaks the syntax ends the geometry where it breaks: what was read before is drawn, as viewers do. +// Points count against the document's budget, and one path's data is limited in length (XpsLimits). + +using System.Globalization; +using System.Xml.Linq; +using SkiaSharp; + +namespace Filee.Engines.Xps; + +/// A geometry: the path that is filled and the path that is stroked, in the element's coordinates. +internal sealed class XpsGeometry(SKPath fill, SKPath stroke) : IDisposable +{ + public SKPath Fill { get; } = fill; + + public SKPath Stroke { get; } = stroke; + + public void Dispose() + { + Fill.Dispose(); + if (!ReferenceEquals(Stroke, Fill)) + Stroke.Dispose(); + } + + /// The geometry of an abbreviated path string ("F1 M 0,0 L …"). + public static XpsGeometry Parse(string data, XpsBudget budget, SKMatrix? transform = null) + { + if (data.Length > budget.Limits.MaxPathData) + throw Documents.DocumentLimits.Exceeded($"A path's data is longer than {Documents.DocumentLimits.Count(budget.Limits.MaxPathData)} characters."); + var path = new SKPath { FillType = SKPathFillType.EvenOdd }; + new Abbreviated(data, path, budget).Read(); + if (transform is { IsIdentity: false } matrix) + path.Transform(matrix); + return new XpsGeometry(path, path); + } + + /// The geometry of a PathGeometry element (its namespace's PathFigure children and Figures attribute). + public static XpsGeometry FromElement(XElement element, XpsBudget budget, SKMatrix? transform) + { + var ns = element.Name.Namespace; + var nonZero = string.Equals((string?)element.Attribute("FillRule"), "NonZero", StringComparison.OrdinalIgnoreCase); + var fill = new SKPath { FillType = nonZero ? SKPathFillType.Winding : SKPathFillType.EvenOdd }; + var stroke = new SKPath { FillType = fill.FillType }; + var split = false; + if ((string?)element.Attribute("Figures") is { Length: > 0 } figures) + { + if (figures.Length > budget.Limits.MaxPathData) + throw Documents.DocumentLimits.Exceeded($"A path's data is longer than {Documents.DocumentLimits.Count(budget.Limits.MaxPathData)} characters."); + new Abbreviated(figures, fill, budget, fillRule: false).Read(); + stroke.AddPath(fill); + } + foreach (var figure in element.Elements(ns + "PathFigure")) + split |= Figure(figure, fill, stroke, budget); + if (transform is { IsIdentity: false } matrix) + { + fill.Transform(matrix); + stroke.Transform(matrix); + } + if (split) + return new XpsGeometry(fill, stroke); + stroke.Dispose(); + return new XpsGeometry(fill, fill); + } + + /// Adds a PathFigure; true when its fill and stroke differ. + private static bool Figure(XElement figure, SKPath fill, SKPath stroke, XpsBudget budget) + { + if (XpsValues.Point((string?)figure.Attribute("StartPoint")) is not { } start) + return false; + var filled = XpsValues.Bool((string?)figure.Attribute("IsFilled"), true); + var closed = XpsValues.Bool((string?)figure.Attribute("IsClosed"), false); + var split = !filled; + using var own = new SKPath(); + using var stroked = new SKPath(); + own.MoveTo(start); + stroked.MoveTo(start); + var current = start; + budget.Points(1); + foreach (var segment in figure.Elements()) + { + var isStroked = XpsValues.Bool((string?)segment.Attribute("IsStroked"), true); + split |= !isStroked; + var from = current; + Action? apply = null; + switch (segment.Name.LocalName) + { + case "PolyLineSegment": + { + var points = Points(segment, budget); + if (points.Count > 0) + current = points[^1]; + apply = path => + { + foreach (var point in points) + path.LineTo(point); + }; + break; + } + case "PolyBezierSegment": + { + var points = Points(segment, budget); + var count = points.Count / 3 * 3; + if (count > 0) + current = points[count - 1]; + apply = path => + { + for (var i = 0; i + 2 < count; i += 3) + path.CubicTo(points[i], points[i + 1], points[i + 2]); + }; + break; + } + case "PolyQuadraticBezierSegment": + { + var points = Points(segment, budget); + var count = points.Count / 2 * 2; + if (count > 0) + current = points[count - 1]; + apply = path => + { + for (var i = 0; i + 1 < count; i += 2) + path.QuadTo(points[i], points[i + 1]); + }; + break; + } + case "ArcSegment": + if (XpsValues.Point((string?)segment.Attribute("Point")) is { } end && XpsValues.Point((string?)segment.Attribute("Size")) is { } size) + { + budget.Points(4); + var large = XpsValues.Bool((string?)segment.Attribute("IsLargeArc"), false); + var clockwise = string.Equals((string?)segment.Attribute("SweepDirection"), "Clockwise", StringComparison.OrdinalIgnoreCase); + var rotation = XpsValues.Number((string?)segment.Attribute("RotationAngle")) ?? 0; + current = end; + apply = path => Arc(path, from, size, rotation, large, clockwise, end); + } + break; + } + if (apply is null) + continue; + apply(own); + // The stroke takes the segment, or moves past it. + if (isStroked) + apply(stroked); + else + stroked.MoveTo(current); + } + if (closed) + { + own.Close(); + stroked.Close(); + } + if (filled) + fill.AddPath(own); + stroke.AddPath(stroked); + return split; + } + + private static List Points(XElement segment, XpsBudget budget) + { + var points = XpsValues.Points((string?)segment.Attribute("Points")); + budget.Points(points.Count); + return points; + } + + /// An elliptical arc as SVG and XPS define it; radii of 0 make a line. + internal static void Arc(SKPath path, SKPoint from, SKPoint size, float rotation, bool large, bool clockwise, SKPoint to) + { + if (from == to) + return; + var rx = Math.Abs(size.X); + var ry = Math.Abs(size.Y); + if (rx == 0 || ry == 0) + { + path.LineTo(to); + return; + } + path.ArcTo(new SKPoint(rx, ry), rotation, large ? SKPathArcSize.Large : SKPathArcSize.Small, + clockwise ? SKPathDirection.Clockwise : SKPathDirection.CounterClockwise, to); + } + + /// Reads the abbreviated syntax into a path. + private sealed class Abbreviated(string text, SKPath path, XpsBudget budget, bool fillRule = true) + { + private int _at; + private SKPoint _current; + private SKPoint _start; + private SKPoint? _lastControl; + private bool _open; + + public void Read() + { + var command = '\0'; + while (true) + { + SkipSpace(); + if (_at >= text.Length) + return; + var c = text[_at]; + if (char.IsAsciiLetter(c)) + { + command = c; + _at++; + if (!Command(command, first: true)) + return; + continue; + } + // More numbers repeat the command (after M they are lines). + if (command is '\0' or 'Z' or 'z' or 'F' or 'f') + return; + if (command is 'M') + command = 'L'; + else if (command is 'm') + command = 'l'; + if (!Command(command, first: false)) + return; + } + } + + private bool Command(char command, bool first) + { + var relative = char.IsAsciiLetterLower(command); + var origin = relative ? _current : default; + switch (char.ToUpperInvariant(command)) + { + case 'F': + if (!fillRule || !first || Number() is not { } rule) + return false; + path.FillType = rule == 1 ? SKPathFillType.Winding : SKPathFillType.EvenOdd; + return true; + case 'M': + if (Point() is not { } move) + return false; + _current = _start = origin + move; + path.MoveTo(_current); + _open = true; + _lastControl = null; + budget.Points(1); + return true; + case 'L': + if (Point() is not { } line) + return false; + LineTo(origin + line); + return true; + case 'H': + if (Number() is not { } x) + return false; + LineTo(new SKPoint((relative ? _current.X : 0) + x, _current.Y)); + return true; + case 'V': + if (Number() is not { } y) + return false; + LineTo(new SKPoint(_current.X, (relative ? _current.Y : 0) + y)); + return true; + case 'C': + { + if (Point() is not { } c1 || Point() is not { } c2 || Point() is not { } end) + return false; + EnsureOpen(); + path.CubicTo(origin + c1, origin + c2, origin + end); + _lastControl = origin + c2; + _current = origin + end; + budget.Points(3); + return true; + } + case 'S': + { + if (Point() is not { } c2 || Point() is not { } end) + return false; + EnsureOpen(); + var c1 = _lastControl is { } last ? new SKPoint(2 * _current.X - last.X, 2 * _current.Y - last.Y) : _current; + path.CubicTo(c1, origin + c2, origin + end); + _lastControl = origin + c2; + _current = origin + end; + budget.Points(3); + return true; + } + case 'Q': + { + if (Point() is not { } c || Point() is not { } end) + return false; + EnsureOpen(); + path.QuadTo(origin + c, origin + end); + _current = origin + end; + _lastControl = null; + budget.Points(2); + return true; + } + case 'A': + { + if (Point() is not { } size || Number() is not { } rotation || Flag() is not { } large || Flag() is not { } sweep || Point() is not { } end) + return false; + EnsureOpen(); + Arc(path, _current, size, rotation, large, sweep, origin + end); + _current = origin + end; + _lastControl = null; + budget.Points(4); + return true; + } + case 'Z': + if (_open) + path.Close(); + _current = _start; + _open = false; + _lastControl = null; + return true; + default: + return false; + } + } + + private void LineTo(SKPoint point) + { + EnsureOpen(); + path.LineTo(point); + _current = point; + _lastControl = null; + budget.Points(1); + } + + /// A drawing command after Z starts a new figure at the current point. + private void EnsureOpen() + { + if (_open) + return; + path.MoveTo(_current); + _start = _current; + _open = true; + } + + private SKPoint? Point() => + Number() is { } x && Number() is { } y ? new SKPoint(x, y) : null; + + private bool? Flag() => Number() switch + { + 0 => false, + 1 => true, + _ => null, + }; + + private float? Number() + { + SkipSpace(); + var start = _at; + if (_at < text.Length && text[_at] is '+' or '-') + _at++; + while (_at < text.Length && (char.IsAsciiDigit(text[_at]) || text[_at] == '.')) + _at++; + if (_at < text.Length && text[_at] is 'e' or 'E' && _at > start) + { + var mark = _at++; + if (_at < text.Length && text[_at] is '+' or '-') + _at++; + if (_at < text.Length && char.IsAsciiDigit(text[_at])) + { + while (_at < text.Length && char.IsAsciiDigit(text[_at])) + _at++; + } + else + { + _at = mark; + } + } + if (_at == start || !float.TryParse(text.AsSpan(start, _at - start), NumberStyles.Float, CultureInfo.InvariantCulture, out var value) || !float.IsFinite(value)) + { + _at = start; + return null; + } + return value; + } + + private void SkipSpace() + { + while (_at < text.Length && (char.IsWhiteSpace(text[_at]) || text[_at] == ',')) + _at++; + } + } +} diff --git a/src/Filee.Engines/Xps/XpsGlyphLayout.cs b/src/Filee.Engines/Xps/XpsGlyphLayout.cs new file mode 100644 index 000000000..cd85b4909 --- /dev/null +++ b/src/Filee.Engines/Xps/XpsGlyphLayout.cs @@ -0,0 +1,112 @@ +// Places the glyphs of an XPS Glyphs element (XPS 1.0 §5.1, ECMA-388 §12.1): UnicodeString and Indices, whose entries +// are "[(chars:glyphs)][glyph][,advance[,uOffset[,vOffset]]]" separated by ";". A missing glyph number is the font's +// glyph for the character (cmap), a missing advance the font's own (hmtx); advances and offsets are hundredths of +// FontRenderingEmSize. A cluster map "(2:1)" makes the next glyphs one cluster of that many UTF-16 code units, which +// the PDF's text keeps; characters after the last entry get a glyph each. An odd BidiLevel runs right to left: +// advances go leftwards from OriginX. vOffset moves a glyph up. + +using System.Globalization; +using SkiaSharp; + +namespace Filee.Engines.Xps; + +internal static class XpsGlyphLayout +{ + /// Glyphs, their baseline origins and the index in the text where each one's cluster starts. + public readonly record struct Layout(ushort[] Glyphs, SKPoint[] Positions, int[] Clusters); + + private struct Entry + { + public ushort Glyph; + public float? Advance; + public float OffsetX; + public float OffsetY; + public int Cluster; + } + + public static Layout Lay(SKTypeface typeface, float size, SKPoint origin, string text, string? indices, bool rightToLeft) + { + var glyphCount = typeface.GlyphCount; + var entries = new List(); + var at = 0; // the code unit the next cluster starts at + var clusterStart = 0; + var clusterLength = 0; + var glyphsLeft = 0; + foreach (var raw in string.IsNullOrEmpty(indices) ? [] : indices.Split(';')) + { + var item = raw.Trim(); + if (item.StartsWith('(') && item.IndexOf(')') is var close and > 0) + { + var map = item[1..close].Split(':'); + var chars = Count(map[0]); + var glyphs = map.Length > 1 ? Count(map[1]) : 1; + clusterStart = at; + clusterLength = Math.Max(1, chars); + glyphsLeft = Math.Max(1, glyphs); + item = item[(close + 1)..]; + } + else if (glyphsLeft == 0) + { + clusterStart = at; + clusterLength = at < text.Length && char.IsSurrogatePair(text, at) ? 2 : 1; + glyphsLeft = 1; + } + var fields = item.Split(','); + var entry = new Entry { Cluster = Math.Min(clusterStart, text.Length) }; + entry.Glyph = fields[0].Trim() is { Length: > 0 } number && ushort.TryParse(number, NumberStyles.None, CultureInfo.InvariantCulture, out var glyph) + ? glyph + : Cmap(typeface, text, clusterStart); + entry.Advance = fields.Length > 1 ? Hundredths(fields[1], size) : null; + entry.OffsetX = fields.Length > 2 ? Hundredths(fields[2], size) ?? 0 : 0; + entry.OffsetY = fields.Length > 3 ? Hundredths(fields[3], size) ?? 0 : 0; + if (entry.Glyph >= glyphCount) + entry.Glyph = 0; + entries.Add(entry); + if (--glyphsLeft == 0) + at = clusterStart + clusterLength; + } + // Characters no entry covers: a glyph each, from the cmap. + while (at < text.Length) + { + var length = char.IsSurrogatePair(text, at) ? 2 : 1; + entries.Add(new Entry { Glyph = Cmap(typeface, text, at), Cluster = at }); + at += length; + } + + var glyphsOut = entries.Select(e => e.Glyph).ToArray(); + using var font = new SKFont(typeface, size) { Subpixel = true, Hinting = SKFontHinting.None, LinearMetrics = true }; + var widths = font.GetGlyphWidths(glyphsOut); + var positions = new SKPoint[entries.Count]; + var pen = origin.X; + for (var i = 0; i < entries.Count; i++) + { + var advance = entries[i].Advance ?? (i < widths.Length ? widths[i] : 0); + if (rightToLeft) + { + pen -= advance; + positions[i] = new SKPoint(pen - entries[i].OffsetX, origin.Y - entries[i].OffsetY); + } + else + { + positions[i] = new SKPoint(pen + entries[i].OffsetX, origin.Y - entries[i].OffsetY); + pen += advance; + } + } + return new Layout(glyphsOut, positions, [.. entries.Select(e => e.Cluster)]); + } + + private static int Count(string text) => + int.TryParse(text.Trim(), NumberStyles.None, CultureInfo.InvariantCulture, out var count) ? Math.Min(count, 1 << 20) : 1; + + private static float? Hundredths(string text, float size) => + text.Trim().Length > 0 && XpsValues.Number(text) is { } value ? value * size / 100 : null; + + /// The font's glyph for the character at ; 0 past the text. + private static ushort Cmap(SKTypeface typeface, string text, int index) + { + if (index >= text.Length) + return 0; + var codePoint = char.IsSurrogatePair(text, index) ? char.ConvertToUtf32(text, index) : text[index]; + return typeface.GetGlyph(codePoint); + } +} diff --git a/src/Filee.Engines/Xps/XpsLimits.cs b/src/Filee.Engines/Xps/XpsLimits.cs new file mode 100644 index 000000000..bb2a848b4 --- /dev/null +++ b/src/Filee.Engines/Xps/XpsLimits.cs @@ -0,0 +1,57 @@ +// What one XPS document may make Filee build, on top of DocumentLimits (package sizes, XML depth and elements): +// pages, nesting of canvases and visual brushes, drawn elements, points of paths and glyphs, and the length of one +// path's data. Every limit is far beyond what printers and Office write; a document past one is refused with +// error.document_too_complex, naming the limit. + +using Filee.Engines.Documents; + +namespace Filee.Engines.Xps; + +/// Limits of the XPS reader. +internal sealed record XpsLimits +{ + public static XpsLimits Default { get; } = new(); + + /// The package and picture limits. + public DocumentLimits Document { get; init; } = DocumentLimits.Default with + { + // Canvases in canvases and visual brushes in visual brushes; printer drivers nest a handful. + MaxNestingDepth = 64, + // Paths, glyph runs and canvases: a CAD plot may draw a few hundred thousand. + MaxObjects = 2_000_000, + }; + + /// Pages of one document. + public int MaxPages { get; init; } = DocumentLimits.Default.MaxPages; + + /// Points of all paths and glyphs of the pages read together (each costs some 10 bytes). + public long MaxPoints { get; init; } = 40_000_000; + + /// Characters of one path's data (the abbreviated syntax). + public int MaxPathData { get; init; } = 32 * 1024 * 1024; + + /// Entries of a dash pattern used; longer patterns are drawn solid. + public int MaxDashes { get; init; } = 1024; + + /// Largest page side in points (PDF's own limit, 200 inches); larger pages are cut there. + public float MaxPageSide { get; init; } = 14_400; +} + +/// Counts what the reader builds against . +internal sealed class XpsBudget(XpsLimits limits) +{ + private long _points; + + public XpsLimits Limits { get; } = limits; + + /// Nesting and object counts. + public DocumentBudget Document { get; } = new(limits.Document); + + /// Counts points of a path or glyph run. + public void Points(long count) + { + _points += count; + if (_points > Limits.MaxPoints) + throw DocumentLimits.Exceeded($"The document's paths and glyphs have more than {DocumentLimits.Count(Limits.MaxPoints)} points."); + } +} diff --git a/src/Filee.Engines/Xps/XpsModel.cs b/src/Filee.Engines/Xps/XpsModel.cs new file mode 100644 index 000000000..a6839fcd1 --- /dev/null +++ b/src/Filee.Engines/Xps/XpsModel.cs @@ -0,0 +1,188 @@ +// What the XPS reader makes of a fixed page before it is drawn: canvases, paths and glyph runs with their transforms, +// clips, opacity and brushes, in the page's own units (1/96 inch). XpsPainter draws them with Skia, XpsPageWriter +// hands them to the page model (PageContent) for PDF and pictures. + +using Filee.Engines.Rendering; +using SkiaSharp; + +namespace Filee.Engines.Xps; + +/// A drawn element: Canvas, Path or Glyphs. +internal abstract class XpsNode +{ + /// RenderTransform: the element's coordinates in its parent's. + public SKMatrix Transform { get; set; } = SKMatrix.Identity; + + /// Clip in the element's coordinates (after ); null = none. + public XpsGeometry? Clip { get; set; } + + public float Opacity { get; set; } = 1; + + /// A brush whose alpha masks the element; null = none. + public XpsBrush? OpacityMask { get; set; } + + /// RenderOptions.EdgeMode="Aliased": edges drawn without anti-aliasing. + public bool Aliased { get; set; } + + /// FixedPage.NavigateUri: a link (URL, or "#Name" in the document). + public string? NavigateUri { get; set; } + + /// The element's Name, a link target when the document lists it. + public string? Name { get; set; } + + /// True when drawing the element needs a layer (group opacity or a mask). + public bool NeedsLayer => Opacity < 1 || OpacityMask is not null; +} + +internal sealed class XpsCanvas : XpsNode +{ + public List Children { get; } = []; +} + +/// Stroke settings of a path. +internal sealed record XpsStrokeStyle( + float Thickness = 1, + SKStrokeCap StartCap = SKStrokeCap.Butt, + SKStrokeCap EndCap = SKStrokeCap.Butt, + SKStrokeCap DashCap = SKStrokeCap.Square, + SKStrokeJoin Join = SKStrokeJoin.Miter, + float MiterLimit = 10, + float[]? Dashes = null, + float DashOffset = 0); + +internal sealed class XpsPath : XpsNode +{ + public required XpsGeometry Data { get; init; } + + public XpsBrush? Fill { get; init; } + + public XpsBrush? Stroke { get; init; } + + public XpsStrokeStyle StrokeStyle { get; init; } = new(); +} + +/// A glyph run with its glyphs placed (baseline origins in the element's coordinates). +internal sealed class XpsGlyphs : XpsNode +{ + public required PageFont Font { get; init; } + + public required float Size { get; init; } + + public required ushort[] Glyphs { get; init; } + + public required SKPoint[] Positions { get; init; } + + /// UnicodeString, "" when the run has none. + public required string Text { get; init; } + + /// Per glyph, the index in where its cluster starts. + public required int[] Clusters { get; init; } + + public XpsBrush? Fill { get; init; } + + public bool Bold { get; init; } + + public bool Italic { get; init; } + + /// A Skia font of the run as PageContent draws it (unhinted, subpixel positions). + public SKFont SkiaFont(SKTypeface typeface) => new(typeface, Size) + { + Embolden = Bold, + SkewX = Italic ? GlyphRun.SyntheticSlant : 0, + Subpixel = true, + Edging = SKFontEdging.Antialias, + Hinting = SKFontHinting.None, + LinearMetrics = true, + }; +} + +/// How a brush's tile repeats (TileMode). +internal enum XpsTile +{ + None, + Tile, + FlipX, + FlipY, + FlipXY, +} + +/// A brush: what fills a path or glyphs, in the element's coordinates through . +internal abstract class XpsBrush +{ + public float Opacity { get; init; } = 1; + + public SKMatrix Transform { get; init; } = SKMatrix.Identity; +} + +internal sealed class XpsSolidBrush(SKColor color) : XpsBrush +{ + public SKColor Color { get; } = color; + + /// The colour with the brush's opacity and in its alpha. + public SKColor WithOpacity(float opacity) => + Color.WithAlpha((byte)Math.Clamp(MathF.Round(Color.Alpha * Opacity * opacity), 0, 255)); +} + +/// A linear or radial gradient; stops are sorted and lie within 0…1. +internal sealed class XpsGradientBrush : XpsBrush +{ + public required SKColor[] Colors { get; init; } + + public required float[] Offsets { get; init; } + + public SKShaderTileMode Spread { get; init; } = SKShaderTileMode.Clamp; + + /// Linear: StartPoint and EndPoint. Radial: GradientOrigin and Center. + public SKPoint Start { get; init; } + + public SKPoint End { get; init; } + + /// Radial gradients only: RadiusX and RadiusY; null for a linear gradient. + public SKSize? Radius { get; init; } +} + +/// A tile (an image or a visual) placed by its Viewbox into its Viewport. +internal abstract class XpsTileBrush : XpsBrush +{ + /// The part of the content used, in its own units. + public SKRect Viewbox { get; init; } + + /// Where the viewbox goes, in the element's coordinates (before ). + public SKRect Viewport { get; init; } + + public XpsTile Tile { get; init; } + + /// Viewbox → viewport. + public SKMatrix ViewboxToViewport => + SKMatrix.CreateTranslation(Viewport.Left, Viewport.Top) + .PreConcat(SKMatrix.CreateScale(Viewport.Width / Viewbox.Width, Viewport.Height / Viewbox.Height)) + .PreConcat(SKMatrix.CreateTranslation(-Viewbox.Left, -Viewbox.Top)); + + /// False for an empty viewbox or viewport: the brush draws nothing. + public bool IsDrawable => Viewbox.Width > 0 && Viewbox.Height > 0 && Viewport.Width > 0 && Viewport.Height > 0; + + public (SKShaderTileMode X, SKShaderTileMode Y) TileModes => Tile switch + { + XpsTile.Tile => (SKShaderTileMode.Repeat, SKShaderTileMode.Repeat), + XpsTile.FlipX => (SKShaderTileMode.Mirror, SKShaderTileMode.Repeat), + XpsTile.FlipY => (SKShaderTileMode.Repeat, SKShaderTileMode.Mirror), + XpsTile.FlipXY => (SKShaderTileMode.Mirror, SKShaderTileMode.Mirror), + _ => (SKShaderTileMode.Decal, SKShaderTileMode.Decal), + }; +} + +/// A decoded picture and its size in 1/96 inch (from its resolution; 96 dpi when it has none). +internal sealed record XpsImage(SKImage Image, float Width, float Height); + +internal sealed class XpsImageBrush : XpsTileBrush +{ + public required XpsImage Image { get; init; } + + /// Picture pixels → its 1/96-inch units (the viewbox's). + public SKMatrix PixelsToUnits => SKMatrix.CreateScale(Image.Width / Image.Image.Width, Image.Height / Image.Image.Height); +} + +internal sealed class XpsVisualBrush : XpsTileBrush +{ + public required XpsNode Visual { get; init; } +} diff --git a/src/Filee.Engines/Xps/XpsPackage.cs b/src/Filee.Engines/Xps/XpsPackage.cs new file mode 100644 index 000000000..474199902 --- /dev/null +++ b/src/Filee.Engines/Xps/XpsPackage.cs @@ -0,0 +1,236 @@ +// The OPC package of an XPS or OpenXPS document (Microsoft XPS 1.0, ECMA-388), read defensively through +// DocumentPackage: part names are looked up inside the package and never used for files, references are resolved +// against the referring part and can't leave the package, and every read counts against DocumentLimits (part and +// package sizes counted while unpacking, the inflate ratio, XML without DTDs, depth and element limits). +// +// Part names are URIs: "%20" in a reference names the part "a b", and ZIP items may store either form. A part may be +// split into interleaved pieces ("a.fpage/[0].piece" … "a.fpage/[3].last.piece"), which are joined in order; a part +// whose pieces don't run from [0] to a last piece without gaps is damaged and counts as missing. +// +// Embedded fonts may be obfuscated (.odttf, both specs): the first 32 bytes are XORed with the GUID that names the part, +// its 16 bytes taken from the GUID's text in reverse order. + +using System.Globalization; +using System.IO.Compression; +using System.Text.RegularExpressions; +using System.Xml; +using System.Xml.Linq; +using Filee.Engines.Documents; +using Filee.Engines.Infrastructure; + +namespace Filee.Engines.Xps; + +/// An XPS / OpenXPS package opened for reading. +internal sealed partial class XpsPackage : IDisposable +{ + private static readonly XNamespace Relationships = "http://schemas.openxmlformats.org/package/2006/relationships"; + + private readonly DocumentPackage _package; + + /// The ZIP items of each part (one, or its pieces in order), by unescaped part name without the leading "/". + private readonly Dictionary _parts = new(StringComparer.OrdinalIgnoreCase); + + /// XML elements of joined pieces, which DocumentPackage doesn't see. + private int _pieceElements; + + private XpsPackage(DocumentPackage package) + { + _package = package; + var pieces = new Dictionary>(StringComparer.OrdinalIgnoreCase); + foreach (var entry in package.Entries) + { + if (entry.FullName.EndsWith('/')) + continue; + var name = Unescape(entry.FullName.Replace('\\', '/').TrimStart('/')); + if (PiecePattern().Match(name) is { Success: true } piece) + { + if (!int.TryParse(piece.Groups[2].ValueSpan, NumberStyles.None, CultureInfo.InvariantCulture, out var index)) + continue; + if (!pieces.TryGetValue(piece.Groups[1].Value, out var list)) + pieces[piece.Groups[1].Value] = list = []; + list.TryAdd(index, (entry, piece.Groups[3].Success)); + continue; + } + // Of two items with one name the first wins. + _parts.TryAdd(name, [entry]); + } + foreach (var (name, list) in pieces) + { + // [0] to [n].last without gaps; anything else is a damaged part. + var ordered = list.ToList(); + var complete = ordered[^1].Value.Last && ordered.Select((p, i) => p.Key == i && (p.Value.Last == (i == ordered.Count - 1))).All(ok => ok); + if (complete) + _parts.TryAdd(name, [.. ordered.Select(p => p.Value.Entry)]); + } + } + + [GeneratedRegex(@"^(.+)/\[(\d{1,9})\](\.last)?\.piece$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)] + private static partial Regex PiecePattern(); + + public static XpsPackage Open(string path, DocumentLimits limits) => + new(DocumentPackage.Open(path, limits, NotAPackage)); + + public static XpsPackage Open(Stream stream, DocumentLimits limits) => + new(DocumentPackage.Open(stream, limits, NotAPackage)); + + private static InvalidDataException NotAPackage() => + new("The file is not an XPS document (it is not a ZIP package). It may be damaged."); + + public DocumentLimits Limits => _package.Limits; + + public void Dispose() => _package.Dispose(); + + /// True when the package has a part of that (resolved) name. + public bool Has(string? name) => name is not null && _parts.ContainsKey(name); + + /// Names of all parts, resolved. + public IEnumerable Names => _parts.Keys; + + /// + /// A reference as a part name: relative to the folder of (a part name), or to the package + /// root when it starts with "/". Fragments and queries are dropped, "." and ".." applied, escapes undone. Null when + /// the reference is empty, leaves the package, or is a URL or drive path. + /// + public static string? Resolve(string from, string? reference) + { + if (string.IsNullOrWhiteSpace(reference)) + return null; + reference = reference.Trim(); + var cut = reference.IndexOfAny(['#', '?']); + if (cut >= 0) + reference = reference[..cut]; + reference = Unescape(reference).Replace('\\', '/'); + if (reference.Length == 0 || reference.Contains(':')) + return null; + var parts = new List(); + if (!reference.StartsWith('/')) + { + var folder = from.LastIndexOf('/'); + if (folder > 0) + parts.AddRange(from[..folder].Split('/', StringSplitOptions.RemoveEmptyEntries)); + } + foreach (var segment in reference.Split('/', StringSplitOptions.RemoveEmptyEntries)) + { + if (segment == ".") + continue; + if (segment == "..") + { + if (parts.Count == 0) + return null; + parts.RemoveAt(parts.Count - 1); + continue; + } + parts.Add(segment); + } + return parts.Count == 0 ? null : string.Join('/', parts); + } + + /// The fragment of a reference ("1" of "font.ttc#1"), or null. + public static string? Fragment(string? reference) + { + var hash = reference?.IndexOf('#') ?? -1; + return hash < 0 ? null : Unescape(reference![(hash + 1)..]); + } + + private static string Unescape(string text) + { + try + { + return text.Contains('%') ? Uri.UnescapeDataString(text) : text; + } + catch (UriFormatException) + { + return text; + } + } + + /// The root element of an XML part; null when the package has no such part. + public XElement? Xml(string? name) + { + if (name is null || !_parts.TryGetValue(name, out var entries)) + return null; + try + { + if (entries.Length == 1) + return _package.LoadXml(entries[0], preserveWhitespace: false).Root; + var bytes = Join(entries, Limits.MaxPartBytes) ?? throw PartTooLarge(name); + var limits = Limits.Xml(_package.ElementsRead + _pieceElements, preserveWhitespace: false); + var document = SafeXml.Load(new MemoryStream(bytes, writable: false), limits, + message => DocumentLimits.Exceeded($"{name}: {message}"), out var elements); + _pieceElements += elements; + return document.Root; + } + catch (Exception ex) when (ex is XmlException or System.Text.DecoderFallbackException or ArgumentException) + { + throw new InvalidDataException($"The XPS part {name} is not valid XML. The file may be damaged. {ex.Message}", ex); + } + } + + /// The bytes of a part; null when it is missing or larger than . + public byte[]? Bytes(string? name, long max) + { + if (name is null || !_parts.TryGetValue(name, out var entries)) + return null; + return entries.Length == 1 ? _package.ReadBytes(entries[0], max) : Join(entries, max); + } + + /// The pieces of a part joined; null when they hold more than . + private byte[]? Join(ZipArchiveEntry[] entries, long max) + { + using var joined = new MemoryStream(); + foreach (var entry in entries) + { + if (_package.ReadBytes(entry, max - joined.Length) is not { } bytes) + return null; + joined.Write(bytes); + } + return joined.ToArray(); + } + + private Exception PartTooLarge(string name) => + DocumentLimits.Exceeded($"The part {name} is larger than {DocumentLimits.Megabytes(Limits.MaxPartBytes)}."); + + /// The targets of the package's own relationships of a type ending in . + public IEnumerable RootRelationships(string typeSuffix) + { + var rels = Xml("_rels/.rels"); + if (rels is null) + yield break; + foreach (var relationship in rels.Elements(Relationships + "Relationship")) + { + if ((string?)relationship.Attribute("TargetMode") is { } mode && mode.Equals("External", StringComparison.OrdinalIgnoreCase)) + continue; + if (((string?)relationship.Attribute("Type"))?.EndsWith(typeSuffix, StringComparison.OrdinalIgnoreCase) != true) + continue; + if (Resolve("", (string?)relationship.Attribute("Target")) is { } target) + yield return target; + } + } + + /// + /// The font part's bytes, with the obfuscation of an .odttf part undone; null when the part is missing, too large + /// or obfuscated under a name that is no GUID. + /// + public byte[]? Font(string name) + { + var bytes = Bytes(name, Limits.MaxPartBytes); + if (bytes is null || !name.EndsWith(".odttf", StringComparison.OrdinalIgnoreCase)) + return bytes; + var file = name[(name.LastIndexOf('/') + 1)..^".odttf".Length]; + return Deobfuscate(bytes, file) ? bytes : null; + } + + /// XORs the first 32 bytes with the GUID ; false when it is no GUID or the font is too short. + internal static bool Deobfuscate(byte[] font, string guid) + { + if (font.Length < 32 || !Guid.TryParse(guid, out var parsed)) + return false; + var hex = parsed.ToString("N", CultureInfo.InvariantCulture); + Span key = stackalloc byte[16]; + for (var i = 0; i < 16; i++) + key[i] = byte.Parse(hex.AsSpan(i * 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture); + for (var i = 0; i < 32; i++) + font[i] ^= key[15 - i % 16]; + return true; + } +} diff --git a/src/Filee.Engines/Xps/XpsPageReader.cs b/src/Filee.Engines/Xps/XpsPageReader.cs new file mode 100644 index 000000000..d5ebd4fac --- /dev/null +++ b/src/Filee.Engines/Xps/XpsPageReader.cs @@ -0,0 +1,471 @@ +// Reads a FixedPage part into XPS elements (XpsModel): Canvas, Path and Glyphs with their properties written as +// attributes ("Fill=\"#FF0000\"", "RenderTransform=\"1,0,0,1,10,10\"", abbreviated geometry) or as property elements +// (), resources from FixedPage.Resources and Canvas.Resources (inline +// or in a remote ResourceDictionary part) by "{StaticResource key}", and markup compatibility's AlternateContent +// (the Fallback, else the first Choice). The XPS 1.0 and OpenXPS namespaces are both read: elements are matched by +// their local names in the page's own namespace. +// +// Bounds (XpsLimits): canvases and visual brushes nest at most MaxNestingDepth deep (also what stops a visual brush +// that draws itself), elements count as objects, path and glyph points against the document's budget. + +using System.Xml.Linq; +using Filee.Engines.Rendering; +using SkiaSharp; + +namespace Filee.Engines.Xps; + +internal sealed class XpsPageReader(XpsDocument document, string part, CancellationToken cancellationToken) +{ + private static readonly XNamespace Compatibility = "http://schemas.openxmlformats.org/markup-compatibility/2006"; + + /// Resources already read, by their element. + private readonly Dictionary _resources = []; + + private XpsBudget Budget => document.Budget; + + /// A resource dictionary in scope: its entries, the part they were written in, and the scope around it. + private sealed class Scope(Scope? parent) + { + public Dictionary Entries { get; } = new(StringComparer.Ordinal); + + public (XElement Element, string Part, Scope Scope)? Find(string key) + { + for (var scope = this; scope is not null; scope = scope.Parent) + { + if (scope.Entries.TryGetValue(key, out var entry)) + return entry; + } + return null; + } + + public Scope? Parent { get; } = parent; + } + + /// The page's elements as one canvas. + public XpsCanvas Read(XElement page) + { + var root = new XpsCanvas(); + var scope = Resources(page, new Scope(null), part); + foreach (var child in Children(page)) + { + if (Element(child, scope, part, aliased: false) is { } node) + root.Children.Add(node); + } + return root; + } + + // ───────────────────────── Elements ───────────────────────── + + /// The element's children with AlternateContent replaced by what it chooses, property elements left out. + private static IEnumerable Children(XElement element) + { + foreach (var child in element.Elements()) + { + if (child.Name == Compatibility + "AlternateContent") + { + var chosen = child.Element(Compatibility + "Fallback") ?? child.Element(Compatibility + "Choice"); + if (chosen is null) + continue; + foreach (var inner in Children(chosen)) + yield return inner; + continue; + } + if (!child.Name.LocalName.Contains('.')) + yield return child; + } + } + + private XpsNode? Element(XElement element, Scope scope, string where, bool aliased) + { + cancellationToken.ThrowIfCancellationRequested(); + // A canvas's resources are in scope for its own properties too. + if (element.Name.LocalName == "Canvas") + scope = Resources(element, scope, where); + XpsNode? node = element.Name.LocalName switch + { + "Canvas" => Canvas(element, scope, where, aliased), + "Path" => Path(element, scope, where), + "Glyphs" => Glyphs(element, scope, where), + _ => null, + }; + if (node is null) + return null; + Budget.Document.Object(); + node.Aliased = aliased || Aliased(element); + node.Transform = Transform(element, "RenderTransform", scope) ?? SKMatrix.Identity; + node.Clip = Geometry(element, "Clip", scope); + node.Opacity = Math.Clamp(XpsValues.Number((string?)element.Attribute("Opacity")) ?? 1, 0, 1); + node.OpacityMask = Brush(element, "OpacityMask", scope, where); + node.Name = (string?)element.Attribute("Name"); + node.NavigateUri = (string?)element.Attribute("FixedPage.NavigateUri"); + return node; + } + + private static bool Aliased(XElement element) => + string.Equals((string?)element.Attribute("RenderOptions.EdgeMode"), "Aliased", StringComparison.OrdinalIgnoreCase); + + private XpsCanvas Canvas(XElement element, Scope scope, string where, bool aliased) + { + using var level = Budget.Document.Enter(); + var canvas = new XpsCanvas(); + var childAliased = aliased || Aliased(element); + foreach (var child in Children(element)) + { + if (Element(child, scope, where, childAliased) is { } node) + canvas.Children.Add(node); + } + return canvas; + } + + private XpsPath? Path(XElement element, Scope scope, string where) + { + var data = Geometry(element, "Data", scope); + if (data is null) + return null; + var fill = Brush(element, "Fill", scope, where); + var stroke = Brush(element, "Stroke", scope, where); + var thickness = XpsValues.Number((string?)element.Attribute("StrokeThickness")) ?? 1; + float[]? dashes = null; + if (XpsValues.Numbers((string?)element.Attribute("StrokeDashArray")) is { Length: > 0 } pattern + && pattern.Length <= Budget.Limits.MaxDashes && pattern.All(d => d >= 0) && pattern.Sum() > 0) + { + // In multiples of the thickness; an odd count is repeated to make pairs. + var pairs = pattern.Length % 2 == 0 ? pattern : [.. pattern, .. pattern]; + dashes = [.. pairs.Select(d => d * thickness)]; + } + return new XpsPath + { + Data = data, + Fill = fill, + Stroke = stroke, + StrokeStyle = new XpsStrokeStyle( + thickness, + Cap((string?)element.Attribute("StrokeStartLineCap")), + Cap((string?)element.Attribute("StrokeEndLineCap")), + Cap((string?)element.Attribute("StrokeDashCap") ?? "Square"), + ((string?)element.Attribute("StrokeLineJoin"))?.Trim() switch + { + "Round" => SKStrokeJoin.Round, + "Bevel" => SKStrokeJoin.Bevel, + _ => SKStrokeJoin.Miter, + }, + XpsValues.Number((string?)element.Attribute("StrokeMiterLimit")) ?? 10, + dashes, + (XpsValues.Number((string?)element.Attribute("StrokeDashOffset")) ?? 0) * thickness), + }; + } + + /// Flat and Triangle ends are drawn flat (Skia has no triangle cap). + private static SKStrokeCap Cap(string? value) => value?.Trim() switch + { + "Round" => SKStrokeCap.Round, + "Square" => SKStrokeCap.Square, + _ => SKStrokeCap.Butt, + }; + + // ───────────────────────── Glyphs ───────────────────────── + + private XpsGlyphs? Glyphs(XElement element, Scope scope, string where) + { + var size = XpsValues.Number((string?)element.Attribute("FontRenderingEmSize")); + // Larger than any page; such a run draws nothing anyone could read. + if (size is not (> 0 and <= MaxEmSize)) + return null; + var text = (string?)element.Attribute("UnicodeString") ?? ""; + if (text.StartsWith("{}", StringComparison.Ordinal)) + text = text[2..]; + var indices = (string?)element.Attribute("Indices"); + if (text.Length == 0 && string.IsNullOrWhiteSpace(indices)) + return null; + var uri = (string?)element.Attribute("FontUri"); + var embedded = document.Font(XpsPackage.Resolve(where, uri), XpsPackage.Fragment(uri)); + var font = embedded ?? document.FallbackFont; + if (font is null) + return null; + // Counted before the glyphs are laid out: at most a glyph per character and one per Indices entry. + Budget.Points(text.Length + (indices?.Count(c => c == ';') + 1 ?? 0)); + var simulations = ((string?)element.Attribute("StyleSimulations"))?.Trim() ?? ""; + var rightToLeft = (int)(XpsValues.Number((string?)element.Attribute("BidiLevel")) ?? 0) % 2 == 1; + var origin = new SKPoint(XpsValues.Number((string?)element.Attribute("OriginX")) ?? 0, XpsValues.Number((string?)element.Attribute("OriginY")) ?? 0); + // Glyph numbers of another font mean nothing in the fallback: it draws the text's characters. + var layout = XpsGlyphLayout.Lay(font.Typeface, size.Value, origin, text, embedded is null ? StripIndices(indices) : indices, rightToLeft); + if (layout.Glyphs.Length == 0 || !layout.Positions.All(p => float.IsFinite(p.X) && float.IsFinite(p.Y))) + return null; + return new XpsGlyphs + { + Font = font, + Size = size.Value, + Glyphs = layout.Glyphs, + Positions = layout.Positions, + Text = text, + Clusters = layout.Clusters, + Fill = Brush(element, "Fill", scope, where), + Bold = simulations.Contains("Bold", StringComparison.OrdinalIgnoreCase), + Italic = simulations.Contains("Italic", StringComparison.OrdinalIgnoreCase), + }; + } + + /// Largest FontRenderingEmSize drawn (in 1/96 inch: some 26 metres). + private const float MaxEmSize = 100_000; + + /// Indices with their glyph numbers taken out, advances and cluster maps kept. + private static string? StripIndices(string? indices) + { + if (string.IsNullOrEmpty(indices)) + return indices; + return string.Join(';', indices.Split(';').Select(entry => + { + var cluster = entry.StartsWith('(') && entry.IndexOf(')') is var close and > 0 ? entry[..(close + 1)] : ""; + var rest = entry[cluster.Length..]; + var comma = rest.IndexOf(','); + return cluster + (comma < 0 ? "" : rest[comma..]); + })); + } + + // ───────────────────────── Properties ───────────────────────── + + /// A property as an attribute's text, or as the first element inside its property element. + private static (string? Text, XElement? Element) Property(XElement element, string name) + { + if ((string?)element.Attribute(name) is { } text) + return (text, null); + var property = element.Element(element.Name.Namespace + $"{element.Name.LocalName}.{name}"); + return (null, property is null ? null : Children(property).FirstOrDefault()); + } + + /// The key of "{StaticResource key}", or null. + private static string? ResourceKey(string text) + { + text = text.Trim(); + if (!text.StartsWith('{') || !text.EndsWith('}') || text.StartsWith("{}", StringComparison.Ordinal)) + return null; + var inner = text[1..^1].Trim(); + return inner.StartsWith("StaticResource", StringComparison.Ordinal) ? inner["StaticResource".Length..].Trim() : null; + } + + private SKMatrix? Transform(XElement element, string name, Scope scope) + { + var (text, value) = Property(element, name); + if (text is not null) + { + if (ResourceKey(text) is not { } key) + return Finite(XpsValues.Matrix(text)); + value = scope.Find(key)?.Element; + } + return value?.Name.LocalName == "MatrixTransform" ? Finite(XpsValues.Matrix((string?)value.Attribute("Matrix"))) : null; + } + + /// A matrix Skia can draw with: finite and invertible (a singular one draws nothing). + private static SKMatrix? Finite(SKMatrix? matrix) => + matrix is { } m && float.IsFinite(m.ScaleX + m.SkewX + m.TransX + m.SkewY + m.ScaleY + m.TransY) ? m : null; + + private XpsGeometry? Geometry(XElement element, string name, Scope scope) + { + var (text, value) = Property(element, name); + if (text is not null) + { + if (ResourceKey(text) is not { } key) + return document.Track(XpsGeometry.Parse(text, Budget)); + if (scope.Find(key) is not { } entry) + return null; + if (_resources.TryGetValue(entry.Element, out var known)) + return known as XpsGeometry; + var shared = entry.Element.Name.LocalName == "PathGeometry" ? PathGeometry(entry.Element, entry.Scope) : null; + _resources[entry.Element] = shared; + return shared; + } + return value?.Name.LocalName == "PathGeometry" ? PathGeometry(value, scope) : null; + } + + private XpsGeometry PathGeometry(XElement element, Scope scope) => + document.Track(XpsGeometry.FromElement(element, Budget, Transform(element, "Transform", scope))); + + // ───────────────────────── Brushes ───────────────────────── + + private XpsBrush? Brush(XElement element, string name, Scope scope, string where) + { + var (text, value) = Property(element, name); + if (text is not null) + { + if (ResourceKey(text) is not { } key) + return XpsValues.Color(text) is { } color ? new XpsSolidBrush(color) : null; + if (scope.Find(key) is not { } entry) + return null; + if (_resources.TryGetValue(entry.Element, out var known)) + return known as XpsBrush; + // Marked before it is read, so a brush that names itself through a visual ends there. + _resources[entry.Element] = null; + var shared = BrushElement(entry.Element, entry.Scope, entry.Part); + _resources[entry.Element] = shared; + return shared; + } + return value is null ? null : BrushElement(value, scope, where); + } + + private XpsBrush? BrushElement(XElement brush, Scope scope, string where) + { + var opacity = Math.Clamp(XpsValues.Number((string?)brush.Attribute("Opacity")) ?? 1, 0, 1); + var transform = Transform(brush, "Transform", scope) ?? SKMatrix.Identity; + switch (brush.Name.LocalName) + { + case "SolidColorBrush": + return XpsValues.Color((string?)brush.Attribute("Color")) is { } color + ? new XpsSolidBrush(color) { Opacity = opacity } + : null; + case "LinearGradientBrush": + case "RadialGradientBrush": + { + var radial = brush.Name.LocalName == "RadialGradientBrush"; + var (colors, offsets) = Stops(brush); + var spread = ((string?)brush.Attribute("SpreadMethod"))?.Trim() switch + { + "Reflect" => SKShaderTileMode.Mirror, + "Repeat" => SKShaderTileMode.Repeat, + _ => SKShaderTileMode.Clamp, + }; + return new XpsGradientBrush + { + Opacity = opacity, + Transform = transform, + Colors = colors, + Offsets = offsets, + Spread = spread, + Start = XpsValues.Point((string?)brush.Attribute(radial ? "GradientOrigin" : "StartPoint")) ?? default, + End = XpsValues.Point((string?)brush.Attribute(radial ? "Center" : "EndPoint")) ?? default, + Radius = radial + ? new SKSize(XpsValues.Number((string?)brush.Attribute("RadiusX")) ?? 0, XpsValues.Number((string?)brush.Attribute("RadiusY")) ?? 0) + : null, + }; + } + case "ImageBrush": + { + if (document.Image(ImageSource((string?)brush.Attribute("ImageSource"), where)) is not { } image) + return null; + return new XpsImageBrush + { + Opacity = opacity, + Transform = transform, + Image = image, + Viewbox = XpsValues.Rect((string?)brush.Attribute("Viewbox")) ?? SKRect.Create(image.Width, image.Height), + Viewport = XpsValues.Rect((string?)brush.Attribute("Viewport")) ?? SKRect.Create(image.Width, image.Height), + Tile = Tile((string?)brush.Attribute("TileMode")), + }; + } + case "VisualBrush": + { + using var level = Budget.Document.Enter(); + var (text, value) = Property(brush, "Visual"); + var visualScope = scope; + var visualPart = where; + if (text is not null && ResourceKey(text) is { } key && scope.Find(key) is { } entry) + (value, visualScope, visualPart) = (entry.Element, entry.Scope, entry.Part); + if (value is null || Element(value, visualScope, visualPart, aliased: false) is not { } visual) + return null; + return new XpsVisualBrush + { + Opacity = opacity, + Transform = transform, + Visual = visual, + Viewbox = XpsValues.Rect((string?)brush.Attribute("Viewbox")) ?? SKRect.Empty, + Viewport = XpsValues.Rect((string?)brush.Attribute("Viewport")) ?? SKRect.Empty, + Tile = Tile((string?)brush.Attribute("TileMode")), + }; + } + default: + return null; + } + } + + /// + /// The part an ImageSource names: a part reference, or the picture of "{ColorConvertedBitmap picture source + /// profile}" (drawn without the colour conversion). + /// + private static string? ImageSource(string? source, string where) + { + if (source is null) + return null; + source = source.Trim(); + if (source.StartsWith("{ColorConvertedBitmap", StringComparison.Ordinal) && source.EndsWith('}')) + source = source["{ColorConvertedBitmap".Length..^1].Split(' ', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); + return XpsPackage.Resolve(where, source); + } + + private static XpsTile Tile(string? mode) => mode?.Trim() switch + { + "Tile" => XpsTile.Tile, + "FlipX" => XpsTile.FlipX, + "FlipY" => XpsTile.FlipY, + "FlipXY" => XpsTile.FlipXY, + _ => XpsTile.None, + }; + + /// + /// A gradient's stops sorted by offset, cut to 0…1 with the colours there found between the stops around them + /// (Skia takes positions in 0…1 only). + /// + private static (SKColor[] Colors, float[] Offsets) Stops(XElement brush) + { + var stops = new List<(float Offset, SKColor Color)>(); + var property = brush.Element(brush.Name.Namespace + $"{brush.Name.LocalName}.GradientStops"); + foreach (var stop in property?.Elements(brush.Name.Namespace + "GradientStop") ?? []) + { + if (XpsValues.Color((string?)stop.Attribute("Color")) is { } color && XpsValues.Number((string?)stop.Attribute("Offset")) is { } offset) + stops.Add((offset, color)); + } + if (stops.Count == 0) + return ([], []); + var sorted = stops.Select((s, i) => (s, i)).OrderBy(p => p.s.Offset).ThenBy(p => p.i).Select(p => p.s).ToList(); + if (sorted.All(s => s.Offset is >= 0 and <= 1)) + return ([.. sorted.Select(s => s.Color)], [.. sorted.Select(s => s.Offset)]); + var inside = sorted.Where(s => s.Offset is > 0 and < 1).ToList(); + inside.Insert(0, (0, ColorAt(sorted, 0))); + inside.Add((1, ColorAt(sorted, 1))); + return ([.. inside.Select(s => s.Color)], [.. inside.Select(s => s.Offset)]); + } + + private static SKColor ColorAt(List<(float Offset, SKColor Color)> stops, float at) + { + if (at <= stops[0].Offset) + return stops[0].Color; + for (var i = 1; i < stops.Count; i++) + { + if (at > stops[i].Offset) + continue; + var (a, b) = (stops[i - 1], stops[i]); + var t = b.Offset == a.Offset ? 1 : (at - a.Offset) / (b.Offset - a.Offset); + return new SKColor(Mix(a.Color.Red, b.Color.Red, t), Mix(a.Color.Green, b.Color.Green, t), Mix(a.Color.Blue, b.Color.Blue, t), Mix(a.Color.Alpha, b.Color.Alpha, t)); + } + return stops[^1].Color; + + static byte Mix(byte from, byte to, float t) => (byte)Math.Clamp(MathF.Round(from + (to - from) * t), 0, 255); + } + + // ───────────────────────── Resources ───────────────────────── + + /// + /// The scope inside an element with resources (FixedPage.Resources, Canvas.Resources): their ResourceDictionary, + /// inline or the remote one its Source names. Returns when there are none. + /// + private Scope Resources(XElement element, Scope scope, string where) + { + var property = element.Element(element.Name.Namespace + $"{element.Name.LocalName}.Resources"); + var dictionary = property?.Elements().FirstOrDefault(e => e.Name.LocalName == "ResourceDictionary"); + if (dictionary is null) + return scope; + var inner = new Scope(scope); + var entriesPart = where; + var entries = dictionary; + if ((string?)dictionary.Attribute("Source") is { } source && XpsPackage.Resolve(where, source) is { } remote) + { + entriesPart = remote; + entries = document.Dictionary(remote) ?? dictionary; + } + foreach (var entry in entries.Elements()) + { + var key = entry.Attributes().FirstOrDefault(a => a.Name.LocalName == "Key")?.Value; + if (key is not null) + inner.Entries.TryAdd(key, (entry, entriesPart, inner)); + } + return inner; + } +} diff --git a/src/Filee.Engines/Xps/XpsPageWriter.cs b/src/Filee.Engines/Xps/XpsPageWriter.cs new file mode 100644 index 000000000..e0187e8d7 --- /dev/null +++ b/src/Filee.Engines/Xps/XpsPageWriter.cs @@ -0,0 +1,135 @@ +// Hands a read XPS page (XpsModel) to the page model (PageContent) that the PDF writer and the picture output draw: +// a glyph run with a solid colour whose clips are all rectangles becomes a glyph item (the PDF writer subsets its +// font and maps its text), everything else a drawing item that applies the clips around it and lets XpsPainter draw +// it. A canvas that needs a layer (group opacity, an opacity mask) is drawn as one drawing item, since a layer +// can't span items. Links (FixedPage.NavigateUri) to URLs and to the document's link targets, and the targets +// themselves, become link areas and named destinations. + +using Filee.Engines.Rendering; +using SkiaSharp; + +namespace Filee.Engines.Xps; + +internal static class XpsPageWriter +{ + /// The page of (1/96 inch) as page content in points. + public static PageContent Write(XpsCanvas root, SKSize size, XpsDocument document) + { + var page = new PageContent(size.Width * XpsDocument.PointsPerUnit, size.Height * XpsDocument.PointsPerUnit); + var toPage = SKMatrix.CreateScale(XpsDocument.PointsPerUnit, XpsDocument.PointsPerUnit); + foreach (var child in root.Children) + Emit(page, child, toPage, [], document); + return page; + } + + /// The node's parent's coordinates → page points. + /// Clips of the node's ancestors, in page points. + private static void Emit(PageContent page, XpsNode node, SKMatrix parent, IReadOnlyList clips, XpsDocument document) + { + Links(page, node, parent, document); + var matrix = parent.PreConcat(node.Transform); + switch (node) + { + case XpsCanvas canvas when !canvas.NeedsLayer: + { + var inner = clips; + if (canvas.Clip is { } clip) + inner = [.. clips, document.Track(Mapped(clip.Fill, matrix))]; + foreach (var child in canvas.Children) + Emit(page, child, matrix, inner, document); + return; + } + case XpsGlyphs glyphs when TryRun(glyphs, matrix, clips, document, out var run): + if (run is not null) + page.DrawGlyphs(run); + return; + } + page.Draw(canvas => + { + foreach (var clip in clips) + canvas.ClipPath(clip, SKClipOperation.Intersect, antialias: !node.Aliased); + canvas.Concat(parent); + XpsPainter.Paint(canvas, node); + }); + } + + private static SKPath Mapped(SKPath path, SKMatrix matrix) + { + var mapped = new SKPath(path); + mapped.Transform(matrix); + return mapped; + } + + /// + /// A plain glyph run as a glyph item ( null when nothing of it shows); false when it needs + /// the painter (a brush other than a solid colour, a mask, a clip that isn't a rectangle). + /// + private static bool TryRun(XpsGlyphs glyphs, SKMatrix matrix, IReadOnlyList clips, XpsDocument document, out GlyphRun? run) + { + run = null; + if (glyphs.Fill is not XpsSolidBrush solid || glyphs.OpacityMask is not null) + return glyphs.Fill is null; + SKRect? area = null; + var own = glyphs.Clip is { } clip ? document.Track(Mapped(clip.Fill, matrix)) : null; + foreach (var path in own is null ? clips : [.. clips, own]) + { + if (!path.IsRect) + return false; + var rect = path.Bounds; + area = area is { } before ? SKRect.Intersect(before, rect) : rect; + } + var color = solid.WithOpacity(glyphs.Opacity); + if (area is { IsEmpty: true } || color.Alpha == 0) + return true; + run = new GlyphRun(glyphs.Font, glyphs.Size, glyphs.Glyphs, glyphs.Positions, glyphs.Text, glyphs.Clusters) + { + Color = color, + Embolden = glyphs.Bold, + SkewX = glyphs.Italic ? GlyphRun.SyntheticSlant : 0, + Transform = matrix, + Clip = area, + }; + return true; + } + + // ───────────────────────── Links ───────────────────────── + + private static void Links(PageContent page, XpsNode node, SKMatrix parent, XpsDocument document) + { + if (node.Name is { Length: > 0 } name && document.PlaceDestination(name)) + { + var top = Bounds(node) is { } box ? new SKPoint(box.Left, box.Top) : default; + page.Destination(parent.MapPoint(top), name); + } + if (node.NavigateUri is not { Length: > 0 } uri || Bounds(node) is not { } bounds) + return; + var area = parent.MapRect(bounds); + if (Uri.TryCreate(uri.Trim(), UriKind.Absolute, out var absolute) && absolute.Scheme is "http" or "https" or "mailto" or "ftp") + { + page.LinkToUrl(area, absolute.AbsoluteUri); + return; + } + var hash = uri.IndexOf('#'); + if (hash >= 0 && uri[(hash + 1)..] is { Length: > 0 } target && document.Targets.Contains(target)) + page.LinkToDestination(area, target); + } + + /// What a node covers, in its parent's coordinates; null when it draws nothing. + private static SKRect? Bounds(XpsNode node) + { + SKRect? own = node switch + { + XpsPath path => Union(path.Data.Fill.IsEmpty ? null : path.Data.Fill.Bounds, path.Data.Stroke.IsEmpty ? null : path.Data.Stroke.Bounds), + XpsGlyphs glyphs when glyphs.Positions.Length > 0 => SKRect.Create( + glyphs.Positions.Min(p => p.X), glyphs.Positions.Min(p => p.Y) - glyphs.Size, + glyphs.Positions.Max(p => p.X) - glyphs.Positions.Min(p => p.X) + glyphs.Size, + glyphs.Positions.Max(p => p.Y) - glyphs.Positions.Min(p => p.Y) + glyphs.Size * 1.25f), + XpsCanvas canvas => canvas.Children.Select(Bounds).Aggregate((SKRect?)null, Union), + _ => null, + }; + return own is { } rect ? node.Transform.MapRect(rect) : null; + } + + private static SKRect? Union(SKRect? a, SKRect? b) => + a is { } first ? b is { } second ? SKRect.Union(first, second) : first : b; +} diff --git a/src/Filee.Engines/Xps/XpsPainter.cs b/src/Filee.Engines/Xps/XpsPainter.cs new file mode 100644 index 000000000..5c60ddcec --- /dev/null +++ b/src/Filee.Engines/Xps/XpsPainter.cs @@ -0,0 +1,279 @@ +// Draws XPS elements (XpsModel) onto a Skia canvas, in the order and with the compositing XPS 1.0 §18 and ECMA-388 +// describe: an element's RenderTransform, then its clip, then its content drawn in a layer when it has an opacity +// below 1 or an opacity mask (the mask's alpha is applied to the whole layer, DstIn). Paths are filled, then stroked. +// +// Brushes: solid colours and gradients are Skia paints; an image or visual brush that isn't tiled is drawn into the +// clipped geometry directly (so a PDF keeps the JPEG as it is, and a visual stays vector), a tiled one is a shader +// (image) or a picture shader (visual). Strokes with image or visual brushes fill the stroke's outline. +// +// Glyph runs drawn here (inside layers, with brushes other than a solid colour, or with clips that aren't rectangles) +// are text blobs of the whole font; XpsPageWriter hands the plain ones to PageContent instead, whose PDF writer +// subsets fonts. + +using SkiaSharp; + +namespace Filee.Engines.Xps; + +internal static class XpsPainter +{ + /// Draws ; the canvas is in the node's parent's coordinates. + public static void Paint(SKCanvas canvas, XpsNode node) + { + canvas.Save(); + canvas.Concat(node.Transform); + if (node.Clip is { } clip) + canvas.ClipPath(clip.Fill, SKClipOperation.Intersect, antialias: !node.Aliased); + if (node.NeedsLayer) + { + using var alpha = new SKPaint { Color = SKColors.White.WithAlpha(Alpha(node.Opacity)) }; + canvas.SaveLayer(alpha); + Content(canvas, node); + if (node.OpacityMask is { } mask) + { + using var dstIn = new SKPaint { BlendMode = SKBlendMode.DstIn }; + canvas.SaveLayer(dstIn); + using var everywhere = new SKPath(); + everywhere.AddRect(canvas.LocalClipBounds); + Fill(canvas, everywhere, mask, antialias: true); + canvas.Restore(); + } + canvas.Restore(); + } + else + { + Content(canvas, node); + } + canvas.Restore(); + } + + private static void Content(SKCanvas canvas, XpsNode node) + { + switch (node) + { + case XpsCanvas group: + foreach (var child in group.Children) + Paint(canvas, child); + break; + case XpsPath path: + if (path.Fill is { } fill) + Fill(canvas, path.Data.Fill, fill, !path.Aliased); + if (path.Stroke is { } stroke) + Stroke(canvas, path.Data.Stroke, stroke, path.StrokeStyle, !path.Aliased); + break; + case XpsGlyphs glyphs: + Glyphs(canvas, glyphs); + break; + } + } + + public static byte Alpha(float opacity) => (byte)Math.Clamp(MathF.Round(opacity * 255), 0, 255); + + /// Fills (in the canvas's coordinates) with . + public static void Fill(SKCanvas canvas, SKPath path, XpsBrush brush, bool antialias) + { + if (path.IsEmpty) + return; + switch (brush) + { + case XpsTileBrush { IsDrawable: false }: + return; + case XpsTileBrush { Tile: XpsTile.None } tile: + canvas.Save(); + canvas.ClipPath(path, SKClipOperation.Intersect, antialias); + canvas.Concat(tile.Transform); + canvas.ClipRect(tile.Viewport, SKClipOperation.Intersect, antialias); + Tile(canvas, tile); + canvas.Restore(); + return; + } + using var paint = Paint(brush, antialias); + if (paint is not null) + canvas.DrawPath(path, paint); + } + + /// Strokes with . + public static void Stroke(SKCanvas canvas, SKPath path, XpsBrush brush, XpsStrokeStyle style, bool antialias) + { + if (path.IsEmpty || style.Thickness <= 0 || !float.IsFinite(style.Thickness)) + return; + using var stroke = new SKPaint + { + Style = SKPaintStyle.Stroke, + StrokeWidth = style.Thickness, + StrokeJoin = style.Join, + StrokeMiter = Math.Max(1, style.MiterLimit), + StrokeCap = style.Dashes is null ? style.StartCap : style.DashCap, + IsAntialias = antialias, + }; + using var dash = style.Dashes is { } dashes ? SKPathEffect.CreateDash(dashes, style.DashOffset) : null; + stroke.PathEffect = dash; + if (brush is XpsSolidBrush or XpsGradientBrush) + { + using var paint = Paint(brush, antialias); + if (paint is null) + return; + stroke.Color = paint.Color; + stroke.Shader = paint.Shader; + canvas.DrawPath(path, stroke); + return; + } + using var outline = stroke.GetFillPath(path); + if (outline is not null) + Fill(canvas, outline, brush, antialias); + } + + /// + /// A Skia paint for a solid colour, gradient or tiled brush (in the canvas's coordinates); null for a brush that + /// draws nothing. Untiled image and visual brushes are drawn by . + /// + public static SKPaint? Paint(XpsBrush brush, bool antialias) + { + switch (brush) + { + case XpsSolidBrush solid: + return new SKPaint { Color = solid.WithOpacity(1), IsAntialias = antialias }; + case XpsGradientBrush gradient: + { + if (gradient.Colors.Length == 0) + return null; + var colors = gradient.Colors.Select(c => c.WithAlpha(Alpha(c.Alpha / 255f * gradient.Opacity))).ToArray(); + if (colors.Length == 1) + return new SKPaint { Color = colors[0], IsAntialias = antialias }; + SKShader shader; + if (gradient.Radius is not { } radius) + { + if (gradient.Start == gradient.End) + return new SKPaint { Color = colors[^1], IsAntialias = antialias }; + shader = SKShader.CreateLinearGradient(gradient.Start, gradient.End, colors, gradient.Offsets, gradient.Spread, gradient.Transform); + } + else + { + if (radius.Width <= 0 || radius.Height <= 0) + return new SKPaint { Color = colors[^1], IsAntialias = antialias }; + // An ellipse is a circle of RadiusX squeezed vertically about the centre. + var center = gradient.End; + var squeeze = radius.Height / radius.Width; + var origin = new SKPoint(gradient.Start.X, center.Y + (gradient.Start.Y - center.Y) / squeeze); + var matrix = gradient.Transform + .PreConcat(SKMatrix.CreateTranslation(center.X, center.Y)) + .PreConcat(SKMatrix.CreateScale(1, squeeze)) + .PreConcat(SKMatrix.CreateTranslation(-center.X, -center.Y)); + shader = SKShader.CreateTwoPointConicalGradient(origin, 0, center, radius.Width, colors, gradient.Offsets, gradient.Spread, matrix); + } + return new SKPaint { Shader = shader, IsAntialias = antialias }; + } + case XpsImageBrush image when image.IsDrawable: + { + var (x, y) = image.TileModes; + if (Viewbox(image) is not var (tile, offset, owned)) + return null; + // Tile pixels → picture pixels → picture units (the viewbox's) → viewport → brush transform. + var matrix = image.Transform.PreConcat(image.ViewboxToViewport).PreConcat(image.PixelsToUnits) + .PreConcat(SKMatrix.CreateTranslation(offset.X, offset.Y)); + var shader = tile.ToShader(x, y, new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.None), matrix); + if (owned) + tile.Dispose(); // the shader holds its own reference + return new SKPaint { Shader = shader, IsAntialias = antialias, Color = SKColors.White.WithAlpha(Alpha(image.Opacity)) }; + } + case XpsVisualBrush visual when visual.IsDrawable: + { + var (x, y) = visual.TileModes; + using var recorder = new SKPictureRecorder(); + var canvas = recorder.BeginRecording(visual.Viewbox); + canvas.ClipRect(visual.Viewbox); + Paint(canvas, visual.Visual); + using var picture = recorder.EndRecording(); + var shader = SKShader.CreatePicture(picture, x, y, SKFilterMode.Linear, visual.Transform.PreConcat(visual.ViewboxToViewport), visual.Viewbox); + return new SKPaint { Shader = shader, IsAntialias = antialias, Color = SKColors.White.WithAlpha(Alpha(visual.Opacity)) }; + } + default: + return null; + } + } + + /// + /// The pixels of an image brush's viewbox as a picture of their own (the whole picture when the viewbox covers it, + /// not owned), and the pixel where they start; null when the viewbox lies outside the picture. + /// + private static (SKImage Tile, SKPointI Offset, bool Owned)? Viewbox(XpsImageBrush brush) + { + var picture = brush.Image.Image; + brush.PixelsToUnits.TryInvert(out var toPixels); + var bounds = SKRectI.Create(0, 0, picture.Width, picture.Height); + var area = SKRectI.Intersect(bounds, SKRectI.Round(toPixels.MapRect(brush.Viewbox))); + if (area.IsEmpty) + return null; + if (area == bounds) + return (picture, default, false); + return picture.Subset(area) is { } subset ? (subset, new SKPointI(area.Left, area.Top), true) : null; + } + + /// One untiled tile: the canvas is in the brush's coordinates, clipped to the viewport. + private static void Tile(SKCanvas canvas, XpsTileBrush brush) + { + switch (brush) + { + case XpsImageBrush image: + { + image.PixelsToUnits.TryInvert(out var toPixels); + using var paint = new SKPaint { Color = SKColors.White.WithAlpha(Alpha(image.Opacity)), IsAntialias = true }; + canvas.DrawImage(image.Image.Image, toPixels.MapRect(image.Viewbox), image.Viewport, + new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear), paint); + break; + } + case XpsVisualBrush visual: + { + canvas.Concat(visual.ViewboxToViewport); + if (visual.Opacity < 1) + { + using var alpha = new SKPaint { Color = SKColors.White.WithAlpha(Alpha(visual.Opacity)) }; + canvas.SaveLayer(alpha); + Paint(canvas, visual.Visual); + canvas.Restore(); + } + else + { + Paint(canvas, visual.Visual); + } + break; + } + } + } + + /// A glyph run as a text blob of its whole font, or as outlines filled with an image or visual brush. + private static void Glyphs(SKCanvas canvas, XpsGlyphs glyphs) + { + if (glyphs.Fill is not { } brush || glyphs.Glyphs.Length == 0) + return; + using var font = glyphs.SkiaFont(glyphs.Font.Typeface); + if (brush is XpsSolidBrush or XpsGradientBrush) + { + using var paint = Paint(brush, !glyphs.Aliased); + if (paint is null) + return; + using var builder = new SKTextBlobBuilder(); + var run = builder.AllocatePositionedRun(font, glyphs.Glyphs.Length); + run.SetGlyphs(glyphs.Glyphs); + run.SetPositions(glyphs.Positions); + using var blob = builder.Build(); + if (blob is not null) + canvas.DrawText(blob, 0, 0, paint); + return; + } + using var outlines = Outlines(glyphs, font); + Fill(canvas, outlines, brush, !glyphs.Aliased); + } + + /// The outlines of a run's glyphs, placed. + public static SKPath Outlines(XpsGlyphs glyphs, SKFont font) + { + var outlines = new SKPath(); + for (var i = 0; i < glyphs.Glyphs.Length; i++) + { + using var glyph = font.GetGlyphPath(glyphs.Glyphs[i]); + if (glyph is not null && !glyph.IsEmpty) + outlines.AddPath(glyph, glyphs.Positions[i].X, glyphs.Positions[i].Y); + } + return outlines; + } +} diff --git a/src/Filee.Engines/Xps/XpsValues.cs b/src/Filee.Engines/Xps/XpsValues.cs new file mode 100644 index 000000000..900867586 --- /dev/null +++ b/src/Filee.Engines/Xps/XpsValues.cs @@ -0,0 +1,125 @@ +// Attribute values of XPS markup: numbers (invariant culture, finite only), points ("x,y"), point lists, booleans, +// matrices ("m11,m12,m21,m22,dx,dy") and colours: "#RRGGBB", "#AARRGGBB", scRGB ("sc#A,R,G,B" or "sc#R,G,B", linear +// light) and ContextColor (an ICC profile and its channels: grey, RGB and CMYK are converted without the profile). + +using System.Globalization; +using SkiaSharp; + +namespace Filee.Engines.Xps; + +internal static class XpsValues +{ + private static readonly char[] Separators = [',', ' ', '\t', '\r', '\n']; + + public static float? Number(string? text) => + text is not null && float.TryParse(text.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out var value) && float.IsFinite(value) + ? value + : null; + + public static bool Bool(string? text, bool fallback) => text?.Trim().ToLowerInvariant() switch + { + "true" => true, + "false" => false, + _ => fallback, + }; + + /// The numbers of a comma or space separated list; null when one isn't a number. + public static float[]? Numbers(string? text) + { + if (text is null) + return null; + var parts = text.Split(Separators, StringSplitOptions.RemoveEmptyEntries); + var numbers = new float[parts.Length]; + for (var i = 0; i < parts.Length; i++) + { + if (Number(parts[i]) is not { } value) + return null; + numbers[i] = value; + } + return numbers; + } + + public static SKPoint? Point(string? text) => + Numbers(text) is [var x, var y] ? new SKPoint(x, y) : null; + + /// "x,y x,y …" as points; a damaged list ends where it breaks. + public static List Points(string? text) + { + var points = new List(); + if (text is null) + return points; + var parts = text.Split(Separators, StringSplitOptions.RemoveEmptyEntries); + for (var i = 0; i + 1 < parts.Length; i += 2) + { + if (Number(parts[i]) is not { } x || Number(parts[i + 1]) is not { } y) + break; + points.Add(new SKPoint(x, y)); + } + return points; + } + + /// "x,y,width,height" as a rectangle; null when damaged or of negative size. + public static SKRect? Rect(string? text) => + Numbers(text) is [var x, var y, var width, var height] && width >= 0 && height >= 0 + ? SKRect.Create(x, y, width, height) + : null; + + /// "m11,m12,m21,m22,dx,dy" as a Skia matrix (x' = m11·x + m21·y + dx). + public static SKMatrix? Matrix(string? text) => + Numbers(text) is [var m11, var m12, var m21, var m22, var dx, var dy] + ? new SKMatrix(m11, m21, dx, m12, m22, dy, 0, 0, 1) + : null; + + /// A colour; null when it can't be read. + public static SKColor? Color(string? text) + { + if (string.IsNullOrWhiteSpace(text)) + return null; + text = text.Trim(); + if (text.StartsWith('#')) + { + var hex = text.AsSpan(1); + if (hex.Length is not (6 or 8) || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var value)) + return null; + return hex.Length == 6 ? new SKColor(value | 0xFF000000) : new SKColor(value); + } + if (text.StartsWith("sc#", StringComparison.OrdinalIgnoreCase)) + { + return Numbers(text[3..]) switch + { + [var a, var r, var g, var b] => FromLinear(a, r, g, b), + [var r, var g, var b] => FromLinear(1, r, g, b), + _ => null, + }; + } + if (text.StartsWith("ContextColor", StringComparison.OrdinalIgnoreCase)) + { + // "ContextColor /profile.icc a,c1,c2…": the channels after the profile's name. + var space = text.IndexOf(' ', "ContextColor".Length + 1); + if (space < 0 || Numbers(text[(space + 1)..]) is not { Length: >= 2 } values) + return null; + var alpha = values[0]; + return values.Length switch + { + 2 => Gamma(alpha, values[1], values[1], values[1]), + 4 => Gamma(alpha, values[1], values[2], values[3]), + 5 => Gamma(alpha, (1 - values[1]) * (1 - values[4]), (1 - values[2]) * (1 - values[4]), (1 - values[3]) * (1 - values[4])), + _ => Gamma(alpha, values[1], values[Math.Min(2, values.Length - 1)], values[Math.Min(3, values.Length - 1)]), + }; + } + return null; + } + + /// Channels already in sRGB (0–1). + private static SKColor Gamma(float a, float r, float g, float b) => + new(Byte(r), Byte(g), Byte(b), Byte(a)); + + /// scRGB channels (linear light) as sRGB. + private static SKColor FromLinear(float a, float r, float g, float b) => + new(Byte(Encode(r)), Byte(Encode(g)), Byte(Encode(b)), Byte(a)); + + private static float Encode(float linear) => + linear <= 0.0031308f ? 12.92f * linear : 1.055f * MathF.Pow(linear, 1 / 2.4f) - 0.055f; + + private static byte Byte(float value) => (byte)Math.Clamp(MathF.Round(value * 255), 0, 255); +} diff --git a/tests/Filee.Core.Tests/FormatFieldsTests.cs b/tests/Filee.Core.Tests/FormatFieldsTests.cs index e85b59999..fbd154e7d 100644 --- a/tests/Filee.Core.Tests/FormatFieldsTests.cs +++ b/tests/Filee.Core.Tests/FormatFieldsTests.cs @@ -35,8 +35,8 @@ private static string[] Hidden(IReadOnlySet turnedOff) => "org", "osm", "otf", "pam", "pcd", "pcx", "pdb", "pdn", "pfm", "plist", "plt", "ply", "pml", "ppm", "prc", "ps", "psb", "psd", "psw", "qoi", "ras", "raw", "rb", "rm", "rpm", "rst", "s3m", "scc", "sda", "sdc", "sdw", "sgi", "shp", "snb", "spx", "ssa", "stl", "sub", "swf", "tak", "tar", "tar7z", "tbz2", "tcr", "tex", "textile", "tgz", "tlz", "toml", - "tta", "ttf", "ttml", "txtz", "txz", "typ", "tz", "usm", "vob", "voc", "vpk", "vtf", "w64", "wbmp", "wmf", "woff", "woff2", - "wpd", "wtv", "wv", "xbm", "xcf", "xlt", "xm", "xpm", "xz", "yaml", "z", "zxp" + "tta", "ttf", "ttml", "txtz", "txz", "typ", "tz", "tzst", "usm", "vob", "voc", "vpk", "vtf", "w64", "wbmp", "wmf", "woff", + "woff2", "wpd", "wtv", "wv", "xbm", "xcf", "xlt", "xm", "xpm", "xz", "yaml", "z", "zst", "zxp" ]; public static TheoryData HiddenWithOneFieldOff => new() @@ -51,7 +51,7 @@ private static string[] Hidden(IReadOnlySet turnedOff) => { "developer", [ "aab", "ani", "apks", "bz2", "cab", "cpio", "cur", "deb", "fodg", "fodp", "fods", "fodt", "gz", "har", "icns", "jar", "jsonl", "lz", "lzma", "m4s", "man", "miff", "msix", "nupkg", "pam", "pfm", "plist", "ppm", "qoi", - "rpm", "tar", "tar7z", "tbz2", "tgz", "tlz", "toml", "txz", "xbm", "xpm", "xz", "yaml", "zxp" + "rpm", "tar", "tar7z", "tbz2", "tgz", "tlz", "toml", "txz", "tzst", "xbm", "xpm", "xz", "yaml", "zst", "zxp" ] }, { "writing", ["adoc", "cgm", "dbk", "htmlz", "mediawiki", "org", "rst", "tex", "textile", "txtz", "typ"] }, { "video-pro", [ @@ -78,7 +78,8 @@ private static string[] Hidden(IReadOnlySet turnedOff) => { "developer", [ "aab", "ani", "apks", "bz2", "cab", "caf", "chm", "cpio", "cur", "deb", "fodg", "fodp", "fods", "fodt", "gz", "har", "icns", "ipynb", "jar", "jsonl", "lz", "lzma", "m4s", "man", "miff", "msix", "nupkg", "pam", "pfm", - "plist", "ppm", "qoi", "rpm", "tar", "tar7z", "tbz2", "tgz", "tlz", "toml", "txz", "xbm", "xpm", "xz", "yaml", "zxp" + "plist", "ppm", "qoi", "rpm", "tar", "tar7z", "tbz2", "tgz", "tlz", "toml", "txz", "tzst", "xbm", "xpm", "xz", "yaml", + "zst", "zxp" ] }, { "writing", ["adoc", "cgm", "chm", "dbk", "htmlz", "mediawiki", "org", "rst", "tex", "textile", "txtz", "typ"] }, { "video-pro", [ @@ -144,8 +145,8 @@ public void Grade_counts_match_the_published_table() new Dictionary { [FormatGrade.Everyday] = 19, - [FormatGrade.General] = 100, - [FormatGrade.Specialist] = 131, + [FormatGrade.General] = 102, + [FormatGrade.Specialist] = 133, [FormatGrade.Legacy] = 52, }, counts); diff --git a/tests/Filee.Core.Tests/XlsbSettingsTests.cs b/tests/Filee.Core.Tests/XlsbSettingsTests.cs new file mode 100644 index 000000000..9fd09a0aa --- /dev/null +++ b/tests/Filee.Core.Tests/XlsbSettingsTests.cs @@ -0,0 +1,30 @@ +using Filee.Core.Presets; +using Filee.Core.Profiles; +using Filee.Core.Settings; + +namespace Filee.Core.Tests; + +public class XlsbSettingsTests +{ + [Fact] + public void An_excel_binary_workbook_has_the_spreadsheets_donut_in_a_new_library() => + Assert.Equal("spreadsheets", ProfileSelector.Select(BuiltInData.CreateProfiles(), ["Budget.xlsb"])?.Id); + + [Fact] + public void Version_106_donuts_learn_xlsb_without_taking_custom_claims() + { + var presets = BuiltInData.CreatePresets(); + var profiles = BuiltInData.CreateProfiles(); + var donut = profiles.Single(p => p.Id == "spreadsheets").Extensions; + donut.Remove("xlsb"); + + Assert.True(SettingsMigrations.ApplyToLibrary(105, presets, profiles)); + Assert.Contains("xlsb", donut); + Assert.False(SettingsMigrations.ApplyToLibrary(105, presets, profiles)); + + donut.Remove("xlsb"); + profiles.Insert(0, new ToolbarProfile { Id = "my-sheets", Name = "Binary sheets", Extensions = ["XLSB"] }); + Assert.False(SettingsMigrations.ApplyToLibrary(105, presets, profiles)); + Assert.DoesNotContain("xlsb", donut); // the user's own donut has it + } +} diff --git a/tests/Filee.Core.Tests/XpsSettingsTests.cs b/tests/Filee.Core.Tests/XpsSettingsTests.cs new file mode 100644 index 000000000..b97ac2b0c --- /dev/null +++ b/tests/Filee.Core.Tests/XpsSettingsTests.cs @@ -0,0 +1,37 @@ +using Filee.Core.Presets; +using Filee.Core.Profiles; +using Filee.Core.Settings; + +namespace Filee.Core.Tests; + +public class XpsSettingsTests +{ + [Theory] + [InlineData("printout.xps")] + [InlineData("printout.oxps")] + public void An_xps_document_has_a_donut_in_a_new_library(string file) => + Assert.NotNull(ProfileSelector.Select(BuiltInData.CreateProfiles(), [file])); + + [Fact] + public void Version_105_donuts_learn_xps_and_oxps_without_taking_custom_claims() + { + var presets = BuiltInData.CreatePresets(); + var profiles = BuiltInData.CreateProfiles(); + var id = ProfileSelector.Select(profiles, ["printout.xps"])!.Id; + var donut = profiles.Single(p => p.Id == id).Extensions; + donut.Remove("xps"); + donut.Remove("oxps"); + + Assert.True(SettingsMigrations.ApplyToLibrary(104, presets, profiles)); + Assert.Contains("xps", donut); + Assert.Contains("oxps", donut); + Assert.False(SettingsMigrations.ApplyToLibrary(104, presets, profiles)); + + donut.Remove("xps"); + donut.Remove("oxps"); + profiles.Insert(0, new ToolbarProfile { Id = "my-prints", Name = "Prints", Extensions = ["XPS"] }); + Assert.True(SettingsMigrations.ApplyToLibrary(104, presets, profiles)); + Assert.DoesNotContain("xps", donut); // the user's own donut has it + Assert.Contains("oxps", donut); + } +} diff --git a/tests/Filee.Core.Tests/ZstSettingsTests.cs b/tests/Filee.Core.Tests/ZstSettingsTests.cs new file mode 100644 index 000000000..261c6c2c9 --- /dev/null +++ b/tests/Filee.Core.Tests/ZstSettingsTests.cs @@ -0,0 +1,34 @@ +using Filee.Core.Presets; +using Filee.Core.Profiles; +using Filee.Core.Settings; + +namespace Filee.Core.Tests; + +public class ZstSettingsTests +{ + private static readonly string[] Extensions = ["tar.zst", "tzst", "zst"]; + + [Theory] + [InlineData("logs.zst")] + [InlineData("backup.tar.zst")] + [InlineData("backup.tzst")] + public void A_zstandard_file_has_the_archives_donut_in_a_new_library(string name) => + Assert.Equal("archives", ProfileSelector.Select(BuiltInData.CreateProfiles(), [name])?.Id); + + [Fact] + public void Version_104_archives_learn_zstandard_without_taking_custom_claims() + { + var presets = BuiltInData.CreatePresets(); + var profiles = BuiltInData.CreateProfiles(); + var archives = profiles.Single(p => p.Id == "archives"); + archives.Extensions.RemoveAll(e => Extensions.Contains(e)); + profiles.Insert(0, new ToolbarProfile { Id = "mine", Name = "Mine", Extensions = ["ZST"] }); + + Assert.True(SettingsMigrations.ApplyToLibrary(103, presets, profiles)); + Assert.DoesNotContain("zst", archives.Extensions, StringComparer.OrdinalIgnoreCase); + Assert.Contains("tar.zst", archives.Extensions); + Assert.Contains("tzst", archives.Extensions); + Assert.Equal("mine", ProfileSelector.Select(profiles, ["logs.zst"])?.Id); + Assert.False(SettingsMigrations.ApplyToLibrary(103, presets, profiles)); + } +} diff --git a/tests/Filee.Engines.Tests/ArchiveFormatBuilders.cs b/tests/Filee.Engines.Tests/ArchiveFormatBuilders.cs index b51141026..610a33198 100644 --- a/tests/Filee.Engines.Tests/ArchiveFormatBuilders.cs +++ b/tests/Filee.Engines.Tests/ArchiveFormatBuilders.cs @@ -765,6 +765,19 @@ public static byte[] Gzip(byte[] data) return ms.ToArray(); } + /// One zstd frame of (SharpCompress's encoder), with a window of 2^windowLog bytes. + public static byte[] Zstd(byte[] data, int level = 3, int? windowLog = null) + { + using var ms = new MemoryStream(); + using (var zstd = new SharpCompress.Compressors.ZStandard.CompressionStream(ms, level, leaveOpen: true)) + { + if (windowLog is { } log) + zstd.SetParameter(SharpCompress.Compressors.ZStandard.Unsafe.ZSTD_cParameter.ZSTD_c_windowLog, log); + zstd.Write(data); + } + return ms.ToArray(); + } + /// A TAR (PAX) of . public static byte[] Tar(IReadOnlyList entries) { diff --git a/tests/Filee.Engines.Tests/ArchiveFuzzTests.cs b/tests/Filee.Engines.Tests/ArchiveFuzzTests.cs index a6a88a9d8..cb17339ef 100644 --- a/tests/Filee.Engines.Tests/ArchiveFuzzTests.cs +++ b/tests/Filee.Engines.Tests/ArchiveFuzzTests.cs @@ -83,9 +83,10 @@ public void Unix_compress_reader_unpacks_or_refuses_every_mutated_file() => Fuzz("z", [UnixCompress(Words(20_000)), UnixCompress(Noise(5_000, 1), maxBits: 9)]); [Fact] - public void Lzip_alz_and_egg_readers_unpack_or_refuse_every_mutated_file() + public void Lzip_zstd_alz_and_egg_readers_unpack_or_refuse_every_mutated_file() { Fuzz("lz", [Lzip(Words(20_000))]); + Fuzz("zst", [Zstd(Words(20_000)), [.. Zstd(Words(3_000), level: 1), .. Zstd(Noise(2_000, 2))]]); var folder = fx.NewFolder(); var alz = Path.Combine(folder, "seed.alz"); WriteAlz(alz, [new("폴더\\", null), new("폴더\\a.txt", Words(2000), Method: 2), new("b.txt", Words(1000), Method: 1, Packed: ToAlzBzip2(Bzip2(Words(1000))))]); diff --git a/tests/Filee.Engines.Tests/ArchiveInProcessTests.cs b/tests/Filee.Engines.Tests/ArchiveInProcessTests.cs index 11f9abfbd..49073268f 100644 --- a/tests/Filee.Engines.Tests/ArchiveInProcessTests.cs +++ b/tests/Filee.Engines.Tests/ArchiveInProcessTests.cs @@ -150,6 +150,7 @@ public async Task Tar_and_compressed_tars_unpack_in_process() await AssertUnpacksAsync(Write("backup.tar.xz", Xz(tar)), "txz", entries); await AssertUnpacksAsync(Write("backup.tar.Z", UnixCompress(tar)), "tz", entries); await AssertUnpacksAsync(Write("backup.tar.lz", Lzip(tar)), "tlz", entries); + await AssertUnpacksAsync(Write("backup.tar.zst", Zstd(tar)), "tzst", entries); await AssertUnpacksAsync(Write("backup.tar.7z", SevenZip([new("backup.tar", tar)])), "tar7z", entries); } @@ -160,6 +161,7 @@ public async Task Tar_and_compressed_tars_unpack_in_process() [InlineData("lzma")] [InlineData("z")] [InlineData("lz")] + [InlineData("zst")] public async Task Compressed_single_files_unpack_in_process(string format) { var data = Words(1_500_000); @@ -170,12 +172,46 @@ public async Task Compressed_single_files_unpack_in_process(string format) "xz" => Xz(data), "lzma" => [0x5D, 0x00, 0x00, 0x10, 0x00, .. BitConverter.GetBytes((long)data.Length), .. RawLzma(data, endMarker: false)], "z" => UnixCompress(data), + "zst" => Zstd(data), _ => Lzip(data), }; var folder = await UnpackAsync(Write("log.txt." + format, packed), format); Assert.Equal(data, await File.ReadAllBytesAsync(Path.Combine(folder, "log.txt"), Ct)); } + [Fact] + public async Task Zstd_frames_follow_one_another_and_other_data_damage_or_wide_windows_are_refused() + { + // Two frames (as "zstd -c a b" writes them) with skippable frames around them, as seekable-format tools add. + byte[] skippable = [0x5E, 0x2A, 0x4D, 0x18, 3, 0, 0, 0, 1, 2, 3]; + var first = Words(400_000); + var second = Noise(50_000, 7); + var folder = await UnpackAsync(Write("two.txt.zst", [.. skippable, .. Zstd(first), .. Zstd(second, level: 19), .. skippable]), "zst"); + byte[] expected = [.. first, .. second]; + Assert.Equal(expected, await File.ReadAllBytesAsync(Path.Combine(folder, "two.txt"), Ct)); + + Assert.Contains("not a valid archive", await RefusedAsync(Write("fake.zst", "not zstd at all"u8.ToArray()), "zst")); + Assert.Contains("not a valid archive", await RefusedAsync(Write("empty.tar.zst", []), "tzst")); + var zst = Zstd(Words(200_000)); + Assert.Contains("damaged", await RefusedAsync(Write("cut.zst", zst[..(zst.Length / 2)]), "zst")); + Assert.Contains("damaged", await RefusedAsync(Write("trailing.zst", [.. zst, 1, 2, 3, 4, 5, 6, 7, 8]), "zst")); + // A 256 MiB window (zstd --long=28) is left to 7-Zip, as the zstd tool needs --memory for it. + var wide = await Assert.ThrowsAsync( + () => UnpackAsync(Write("wide.tar.zst", Zstd(Words(300_000), windowLog: 28)), "tzst")); + Assert.Equal(ArchiveExtractor.NeedsSevenZip, wide.ErrorKey); + } + + [Theory] + [InlineData("backup.tar.zst", "tzst")] + [InlineData("backup.tzst", "tzst")] + [InlineData("BACKUP.TAR.ZST", "tzst")] + [InlineData("log.txt.zst", "zst")] + public void Zstd_names_are_recognised(string name, string format) + { + Assert.Equal(format, FormatRegistry.Detect(name)?.Id); + Assert.False(FormatRegistry.Get(format).Writable); + } + [Theory] [InlineData(9)] [InlineData(12)] @@ -222,11 +258,11 @@ public async Task Cpio_hard_link_groups_become_copies() } [Fact] - public async Task Rpm_payloads_in_gzip_and_xz_unpack_to_their_files() + public async Task Rpm_payloads_in_gzip_xz_and_zstd_unpack_to_their_files() { var entries = Entries(); var cpio = Cpio(entries); - foreach (var payload in new[] { Gzip(cpio), Xz(cpio), cpio }) + foreach (var payload in new[] { Gzip(cpio), Xz(cpio), Zstd(cpio), cpio }) await AssertUnpacksAsync(Write("filee-test.rpm", Rpm(payload)), "rpm", entries); } diff --git a/tests/Filee.Engines.Tests/ArchiveTests.cs b/tests/Filee.Engines.Tests/ArchiveTests.cs index 65ed45033..003f66911 100644 --- a/tests/Filee.Engines.Tests/ArchiveTests.cs +++ b/tests/Filee.Engines.Tests/ArchiveTests.cs @@ -57,6 +57,8 @@ private async Task ConvertOkAsync(string source, Preset preset) [InlineData("egg")] [InlineData("lz")] [InlineData("tlz")] + [InlineData("tzst")] + [InlineData("zst")] [InlineData("split")] public void Every_readable_archive_converts_in_one_step(string from) { @@ -75,6 +77,8 @@ public void Single_file_compressors_convert_among_each_other_but_archives_do_not { var planner = fx.Catalog.CreatePlanner(["archive"]); Assert.Single(planner.Plan("lz", "gz")!.Steps); + Assert.Single(planner.Plan("zst", "xz")!.Steps); + Assert.Null(planner.Plan("gz", "zst")); // zstd is read, not written Assert.Single(planner.Plan("xz", "gz")!.Steps); Assert.Single(planner.Plan("gz", "xz")!.Steps); // Filee's own XZ writer Assert.Single(planner.Plan("lzma", "bz2")!.Steps); // and its own bzip2 writer diff --git a/tests/Filee.Engines.Tests/Bzip2WriterTests.cs b/tests/Filee.Engines.Tests/Bzip2WriterTests.cs index ede9905ed..f0841393c 100644 --- a/tests/Filee.Engines.Tests/Bzip2WriterTests.cs +++ b/tests/Filee.Engines.Tests/Bzip2WriterTests.cs @@ -169,6 +169,23 @@ public async Task Output_unpacks_byte_for_byte_with_three_decoders(Kind kind, in Assert.True(bz2.Length < data.Length / 2, $"{bz2.Length} bytes for {data.Length}"); } + /// The move-to-front shift (16 bytes at a time, then single bytes) against a plain byte loop, for every count. + [Fact] + public void The_move_to_front_shift_matches_a_byte_loop() + { + var random = new Random(9); + for (var count = 0; count < 256; count++) + { + var list = new byte[256]; + random.NextBytes(list); + var expected = (byte[])list.Clone(); + for (var k = count; k > 0; k--) + expected[k] = expected[k - 1]; + Bzip2Decoder.MoveUp(list, count); + Assert.Equal(expected, list); + } + } + /// /// The transform against sorting every rotation by comparing it whole, for short blocks over small alphabets, where /// periodic blocks, repeats and ties are common: the same last column, and the origin row is the unrotated block. diff --git a/tests/Filee.Engines.Tests/DrawingConverterTests.cs b/tests/Filee.Engines.Tests/DrawingConverterTests.cs index fa5905fa4..0631f00df 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"], engine.Edges.Select(e => e.From).Distinct()); + Assert.Equal(["vsd", "odg", "fodg", "xps"], 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/FileeRendererConverterTests.cs b/tests/Filee.Engines.Tests/FileeRendererConverterTests.cs index e146e2c07..c871f7edb 100644 --- a/tests/Filee.Engines.Tests/FileeRendererConverterTests.cs +++ b/tests/Filee.Engines.Tests/FileeRendererConverterTests.cs @@ -40,9 +40,10 @@ private void Use(bool renderer) /// /// The engines with the renderer on and a LibreOffice that is installed (or not), with the switches of Settings → - /// Engines → "When LibreOffice is installed", in the default engine order. + /// Engines → "When LibreOffice is installed", in the default engine order (or with LibreOffice moved to its top). /// - private RoutePlanner WithLibreOffice(bool slides = false, bool documents = false, bool sheets = false, bool installed = true) + private RoutePlanner WithLibreOffice(bool slides = false, bool documents = false, bool sheets = false, bool installed = true, + bool libreOfficeFirst = false) { EngineEnvironment.FileeRenderer = true; EngineEnvironment.LibreOfficeForSlides = slides; @@ -54,6 +55,8 @@ private RoutePlanner WithLibreOffice(bool slides = false, bool documents = false if (installed) converters.Add(new InstalledLibreOffice(LibreOfficeConverter.BuildEdges(slides, documents, sheets))); var priority = new AppSettings().EnginePriority; + if (libreOfficeFirst) + priority = ["libreoffice", .. priority.Where(id => id != "libreoffice")]; return new RoutePlanner(converters, c => Math.Min(priority.IndexOf(c.Id) is >= 0 and var i ? i : priority.Count, 9)); } @@ -119,6 +122,35 @@ public void On_rtf_to_txt_reads_the_rtf_not_the_text_of_its_pdf() Assert.Equal(["hwp-text"], Engines(WithLibreOffice(installed: false).Plan("rtf", "txt"))); Assert.Equal(["libreoffice"], Engines(WithLibreOffice().Plan("rtf", "txt"))); Assert.Equal(["libreoffice"], Engines(WithLibreOffice(documents: true).Plan("rtf", "txt"))); + Assert.Equal(["libreoffice"], Engines(WithLibreOffice(libreOfficeFirst: true).Plan("rtf", "txt"))); + } + + [Theory] + [InlineData("docx")] + [InlineData("docm")] + [InlineData("dotx")] + [InlineData("dotm")] + [InlineData("doc")] + [InlineData("dot")] + [InlineData("wps")] + [InlineData("odt")] + public void On_word_and_odt_to_txt_read_the_document_not_the_text_of_its_pdf(string format) + { + // As RTF (#1204): through the PDF every long line was wrapped and the indentation and the empty lines were lost. + // LibreOffice installed at the bottom of the engine list, as by default, and chosen for Word → PDF, which is + // about PDF only, leave it to the built-in text too. + foreach (var planner in new[] { WithLibreOffice(installed: false), WithLibreOffice(), WithLibreOffice(documents: true) }) + { + var step = Assert.Single(planner.Plan(format, "txt")!.Steps); + Assert.Equal("hwp-text", step.Converter.Id); + // A Word 6.0 / 95 file with a .doc name goes another way. + Assert.Equal(format is "doc" or "dot" or "wps", step.Checked); + } + // Moved to the top of the engine list, LibreOffice keeps the Word and ODT files it reads. + var first = Engines(WithLibreOffice(libreOfficeFirst: true).Plan(format, "txt")); + Assert.Equal(format is "docm" or "dotx" or "dotm" ? ["hwp-text"] : ["libreoffice"], first); // LibreOffice has no DOCM, DOTX or DOTM edges + // Not a hop: Word → EPUB and the other routes don't start going through a TXT file. + Assert.DoesNotContain(WithLibreOffice(installed: false).Plan(format, "epub")?.Steps ?? [], s => s.Converter.Id == "hwp-text"); } [Fact] diff --git a/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs b/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs index 98436529f..29f23218c 100644 --- a/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs +++ b/tests/Filee.Engines.Tests/LibreOfficeFormatTests.cs @@ -62,7 +62,7 @@ public void Every_edge_has_a_filter_of_its_application() { string[] draw = ["pub", "cdr", "vsd", "odg", "fodg", "emf", "wmf", "svg", "pdf"]; string[] impress = ["pptx", "ppt", "odp", "otp", "fodp", "key", "dps", "sda", "cgm"]; - string[] calc = ["xlsx", "xls", "xlt", "ods", "ots", "fods", "csv", "et", "numbers", "sdc"]; + string[] calc = ["xlsx", "xls", "xlt", "ods", "ots", "fods", "csv", "et", "numbers", "sdc", "xlsb"]; foreach (var edge in LibreOffice.Edges) { var filter = LibreOfficeConverter.FilterFor(edge.From, edge.To, pdfA: false); diff --git a/tests/Filee.Engines.Tests/MetafileTests.cs b/tests/Filee.Engines.Tests/MetafileTests.cs index 887529ccb..f8cecfd31 100644 --- a/tests/Filee.Engines.Tests/MetafileTests.cs +++ b/tests/Filee.Engines.Tests/MetafileTests.cs @@ -613,4 +613,66 @@ public void Random_damage_stops_with_a_clean_error() } Assert.True(watch.Elapsed < TimeSpan.FromSeconds(60), $"600 damaged files took {watch.Elapsed}"); } + + [Fact] + public void A_bitmap_is_decoded_straight_into_the_bitmap_Skia_draws() + { + // 1,024 × 1,024 at 24 bits, bottom-up: the first row of the data is the bottom row of the picture. + const int size = 1024; + var info = EmfBuilder.Dib(size, size, 24); + var bits = new byte[size * 3 * size]; + bits[0] = 0xFF; // blue, bottom left + bits[^1] = 0xFF; // red, top right + var before = GC.GetAllocatedBytesForCurrentThread(); + using var image = Dib.Decode(info, bits, DibOptions.Default, new MetafileBudget(MetafileLimits.Default)); + var allocated = GC.GetAllocatedBytesForCurrentThread() - before; + Assert.NotNull(image); + // The pixels (4 MB) are written once, into Skia's memory: no array of them, no copy of that array. + Assert.True(allocated < 512 * 1024, $"{allocated:N0} bytes allocated"); + using var bitmap = SKBitmap.FromImage(image.Image); + Assert.Equal(new SKColor(0, 0, 255), bitmap.GetPixel(0, size - 1)); + Assert.Equal(new SKColor(255, 0, 0), bitmap.GetPixel(size - 1, 0)); + Assert.Equal(SKColors.Black, bitmap.GetPixel(size / 2, size / 2)); + } + + [Fact] + public void A_bitmap_whose_pixels_are_cut_off_is_refused_before_they_are_allocated() + { + var before = GC.GetAllocatedBytesForCurrentThread(); + Assert.Throws(() => + Dib.Decode(EmfBuilder.Dib(8_192, 8_192, 24), new byte[16], DibOptions.Default, new MetafileBudget(MetafileLimits.Default))); + Assert.True(GC.GetAllocatedBytesForCurrentThread() - before < 1024 * 1024); + } + + [Fact] + public void A_run_length_bitmap_leaves_the_pixels_its_runs_skip_transparent() + { + // RLE8, 4 × 2, bottom-up: two pixels of colour 1 on the bottom row, then the end of the bitmap. + var header = EmfBuilder.Dib(4, 2, 8); + BitConverter.GetBytes(1).CopyTo(header, 16); // BI_RLE8 + var table = new byte[256 * 4]; + table[4 + 2] = 0xFF; // colour 1: red + var bits = new byte[] { 2, 1, 0, 1 }; + using var image = Dib.Decode([.. header, .. table], bits, DibOptions.Default, new MetafileBudget(MetafileLimits.Default)); + using var bitmap = SKBitmap.FromImage(image!.Image); + Assert.Equal(new SKColor(255, 0, 0), bitmap.GetPixel(0, 1)); + Assert.Equal(new SKColor(255, 0, 0), bitmap.GetPixel(1, 1)); + Assert.Equal(0, bitmap.GetPixel(2, 1).Alpha); + Assert.Equal(0, bitmap.GetPixel(0, 0).Alpha); + } + + [Fact] + public void A_metafile_read_for_a_cancelled_conversion_stops() + { + // More records than the player plays between two looks at the token. + var emf = new EmfBuilder(96, 96); + for (var i = 0; i < 5_000; i++) + emf.Add(18, 1); // SETBKMODE + var path = Path.Combine(fx.NewFolder(), "many.emf"); + File.WriteAllBytes(path, emf.ToArray()); + Assert.ThrowsAny(() => + Magick.ImageReader.Read(path, "emf", new Preset(), new CancellationToken(canceled: true)).Dispose()); + using var images = Magick.ImageReader.Read(path, "emf", new Preset(), TestContext.Current.CancellationToken); + Assert.Single(images); + } } diff --git a/tests/Filee.Engines.Tests/PdfObjectNestingFuzzTests.cs b/tests/Filee.Engines.Tests/PdfObjectNestingFuzzTests.cs new file mode 100644 index 000000000..5a3e3aa1f --- /dev/null +++ b/tests/Filee.Engines.Tests/PdfObjectNestingFuzzTests.cs @@ -0,0 +1,52 @@ +// Mutation fuzzing of the nesting check PDFsharp's object parser gets every PDF through (PdfObjectNesting): seeds are +// PDFs written in code (PdfObjectNestingTests), one with an object stream, mutated as text with PDF's own tokens and as +// bytes. The check must read or refuse each input, and PDFsharp must then open what it let through without overflowing +// the 1 MB stack (what PDFsharp itself refuses counts as refused). 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.Core.Conversion; +using Filee.Engines.Pdf; +using PdfSharp.Pdf.IO; + +namespace Filee.Engines.Tests; + +[Trait("Category", "Fuzz")] +public class PdfObjectNestingFuzzTests(EngineFixture fx) : IClassFixture +{ + private const int QuickIterations = 60; + + private static readonly string[] Tokens = + [ + "[", "]", "<<", ">>", "(", ")", "\\", "<5B>", "%", "obj", "endobj", "stream\n", "\nendstream", "trailer", "/N 1", "/First 4", + "/Type/ObjStm", "/Length 7 0 R", " 0 R", new string('[', 300), new string('[', 3_000), string.Concat(Enumerable.Repeat("[(])", 500)), + ]; + + private static List Seeds() => + [ + PdfObjectNestingTests.Pdf("4 0 obj\n<>]>>\nendobj\n"), + PdfObjectNestingTests.ObjectStream("<>", deflate: false, typed: false, indirect: true), + ]; + + [Fact] + public void Check_and_PDFsharp_read_or_refuse_every_mutated_pdf_as_text() => + Harness().Data("pdfnesting-text", "pdf", Seeds(), FuzzHarness.Text(Tokens), CheckThenOpen); + + [Fact] + public void Check_and_PDFsharp_read_or_refuse_every_mutated_pdf_as_bytes() => + Harness().Data("pdfnesting-bytes", "pdf", Seeds(), FuzzHarness.Bytes, CheckThenOpen); + + private FuzzHarness Harness() => new(fx, QuickIterations, 20261119); + + private static void CheckThenOpen(byte[] pdf) + { + PdfObjectNesting.Check(pdf, TestContext.Current.CancellationToken); + try + { + using var document = PdfReader.Open(new MemoryStream(pdf), PdfDocumentOpenMode.Import); + } + catch (Exception ex) when (ex is not ConversionRefusedException) + { + throw new InvalidDataException("PDFsharp refused it.", ex); + } + } +} diff --git a/tests/Filee.Engines.Tests/PdfObjectNestingTests.cs b/tests/Filee.Engines.Tests/PdfObjectNestingTests.cs new file mode 100644 index 000000000..c25853910 --- /dev/null +++ b/tests/Filee.Engines.Tests/PdfObjectNestingTests.cs @@ -0,0 +1,318 @@ +// PDFs nested too deeply for PDFsharp's object parser, built in code: arrays and dictionaries a million levels deep in an +// object, the trailer, an object stream (with and without /Type, deflated or not, its length given by reference), and +// in places a reading from front to back would miss (after a string that never closes, inside another stream's data, +// with the closing brackets inside strings). Each must be refused with error.document_too_complex before PDFsharp +// parses it, on the smallest stack a conversion runs on (a regression overflows that stack and ends the test run), and +// files nested as real ones are still open. + +using System.IO.Compression; +using System.Text; +using Filee.Core.Conversion; +using Filee.Engines.Documents; +using Filee.Engines.Pdf; +using Filee.Engines.Vector; +using PdfSharp.Pdf; +using PdfSharp.Pdf.IO; + +namespace Filee.Engines.Tests; + +public class PdfObjectNestingTests(EngineFixture fx) : IClassFixture +{ + private const int Deep = 1_000_000; + + // ───────────────────────── Helpers ───────────────────────── + + private static string Nest(int levels) => new string('[', levels) + new string(']', levels); + + private static byte[] Latin1(string text) => Encoding.Latin1.GetBytes(text); + + /// + /// A one-page PDF with after the catalog, page tree and page (objects 1–3); the + /// cross-reference table points at the first "N 0 obj" of each of the objects. + /// + internal static byte[] Pdf(string objects, int count = 4, string trailer = "") + { + var text = "%PDF-1.4\n1 0 obj\n<>\nendobj\n2 0 obj\n<>\nendobj\n" + + "3 0 obj\n<>\nendobj\n" + objects; + var xref = new StringBuilder($"xref\n0 {count + 1}\n0000000000 65535 f \n"); + for (var n = 1; n <= count; n++) + xref.Append($"{text.IndexOf($"\n{n} 0 obj", StringComparison.Ordinal) + 1:D10} 00000 n \n"); + return Latin1(text + xref + $"trailer\n<>\nstartxref\n{text.Length}\n%%EOF\n"); + } + + /// + /// A PDF 1.5 file whose object 4 () lives in object stream 5, found through a + /// cross-reference stream (object 6). Object 7 holds the stream's /Length when (PDFsharp + /// takes /N and /First only as they are). + /// + internal static byte[] ObjectStream(string value, bool deflate = true, bool typed = true, bool indirect = false) + { + var output = new MemoryStream(); + void Write(string text) => output.Write(Latin1(text)); + var offsets = new long[9]; + Write("%PDF-1.5\n"); + offsets[1] = output.Position; + Write("1 0 obj\n<>\nendobj\n"); + offsets[2] = output.Position; + Write("2 0 obj\n<>\nendobj\n"); + offsets[3] = output.Position; + Write("3 0 obj\n<>\nendobj\n"); + + const string header = "4 0 "; + var data = Latin1(header + value); + if (deflate) + { + var packed = new MemoryStream(); + using (var zlib = new ZLibStream(packed, CompressionLevel.Optimal, leaveOpen: true)) + zlib.Write(data); + data = packed.ToArray(); + } + offsets[5] = output.Position; + var length = indirect ? "7 0 R" : data.Length.ToString(System.Globalization.CultureInfo.InvariantCulture); + Write($"5 0 obj\n<<{(typed ? "/Type/ObjStm" : "")}/N 1/First {header.Length}{(deflate ? "/Filter/FlateDecode" : "")}/Length {length}>>\nstream\n"); + output.Write(data); + Write("\nendstream\nendobj\n"); + offsets[7] = output.Position; + Write($"7 0 obj\n{data.Length}\nendobj\n"); + offsets[8] = output.Position; + Write("8 0 obj\nnull\nendobj\n"); + + offsets[6] = output.Position; + var rows = new MemoryStream(); + void Row(int type, long field2, int field3) + { + rows.WriteByte((byte)type); + rows.Write([(byte)(field2 >> 24), (byte)(field2 >> 16), (byte)(field2 >> 8), (byte)field2, (byte)(field3 >> 8), (byte)field3]); + } + Row(0, 0, 65535); + for (var n = 1; n <= 8; n++) + { + if (n == 4) + Row(2, 5, 0); + else + Row(1, offsets[n], 0); + } + var table = rows.ToArray(); + Write($"6 0 obj\n<>\nstream\n"); + output.Write(table); + Write($"\nendstream\nendobj\nstartxref\n{offsets[6]}\n%%EOF\n"); + return output.ToArray(); + } + + /// + /// Runs on a thread with a 1 MB stack (the smallest a conversion gets) and returns what + /// it threw; fails when it takes longer than . + /// + private static Exception? OnSmallStack(Action action, int seconds = 60) + { + Exception? error = null; + var thread = new Thread(() => + { + try + { + action(); + } + catch (Exception ex) + { + error = ex; + } + }, 1024 * 1024); + thread.IsBackground = true; + thread.Start(); + Assert.True(thread.Join(TimeSpan.FromSeconds(seconds)), $"Still running after {seconds} s"); + return error; + } + + private static ConversionRefusedException Refused(Action action) + { + var refusal = Assert.IsType(OnSmallStack(action)); + Assert.Equal(DocumentLimits.ErrorKey, refusal.ErrorKey); + return refusal; + } + + private string Save(byte[] pdf) + { + var path = Path.Combine(fx.NewFolder(), "nested.pdf"); + File.WriteAllBytes(path, pdf); + return path; + } + + public static readonly TheoryData Hiding = + [ + "object", "dictionary", "trailer", "after an unclosed string", "inside stream data", "closed in strings", + "object stream", "plain object stream", "untyped object stream of indirect length", + ]; + + private static byte[] Hostile(string where) => where switch + { + "object" => Pdf($"4 0 obj\n{Nest(Deep)}\nendobj\n"), + "dictionary" => Pdf($"4 0 obj\n{string.Concat(Enumerable.Repeat("<>", Deep / 10))}\nendobj\n"), + "trailer" => Pdf("", count: 3, trailer: "/X " + Nest(Deep)), + // Read from front to back, the "(" makes everything after it a string; PDFsharp jumps to object 4 all the same. + "after an unclosed string" => Pdf($"% (\n(\n4 0 obj\n{Nest(Deep)}\nendobj\n"), + // Object 5 is the data of stream 4, which a reading from front to back skips. + "inside stream data" => Pdf($"4 0 obj\n<>\nstream\n\n5 0 obj\n{Nest(Deep)}\nendobj\nendstream\nendobj\n", count: 5), + // Every "]" is inside a string, so PDFsharp nests a level per "[(])". + "closed in strings" => Pdf($"4 0 obj\n{string.Concat(Enumerable.Repeat("[(])", Deep / 4))}\nendobj\n"), + "object stream" => ObjectStream(Nest(Deep)), + "plain object stream" => ObjectStream(Nest(Deep), deflate: false), + "untyped object stream of indirect length" => ObjectStream(Nest(Deep), typed: false, indirect: true), + _ => throw new ArgumentOutOfRangeException(nameof(where)), + }; + + // ───────────────────────── Refused ───────────────────────── + + [Theory] + [MemberData(nameof(Hiding))] + public void Nesting_too_deep_for_PDFsharp_is_refused_before_it_parses_the_file(string where) + { + var path = Save(Hostile(where)); + var refusal = Refused(() => PdfSharpConverter.Open(path, PdfDocumentOpenMode.Import, TestContext.Current.CancellationToken).Dispose()); + Assert.Contains("nests", refusal.Message, StringComparison.Ordinal); + } + + [Fact] + public void Every_route_that_opens_a_PDF_with_PDFsharp_refuses_it_or_leaves_it_to_another_engine() + { + var path = Save(Hostile("object")); + var ct = TestContext.Current.CancellationToken; + Refused(() => PdfSharpConverter.OpenForChanges(path, ct).Dispose()); + Refused(() => PdfBlankPages.Remove(path)); + Refused(() => PdfObjectNesting.Open(File.ReadAllBytes(path), PdfDocumentOpenMode.Import, ct).Dispose()); + // PDF/A: the PDF goes to Ghostscript as it is. + Assert.Null(OnSmallStack(() => + { + Assert.Equal(new PdfAPreparation(null, null), PdfPrintableAnnotations.Prepare(path, fx.NewFolder(), ct)); + Assert.False(GhostscriptConverter.DeclaresPdfA(path)); + })); + } + + [Fact] + public void Nesting_just_past_the_limit_is_refused_and_at_the_limit_read() + { + var ct = TestContext.Current.CancellationToken; + PdfObjectNesting.Check(Pdf($"4 0 obj\n{Nest(PdfObjectNesting.MaxDepth)}\nendobj\n"), ct); + PdfObjectNesting.Check(ObjectStream(Nest(PdfObjectNesting.MaxDepth)), ct); + Assert.Throws(() => PdfObjectNesting.Check(Pdf($"4 0 obj\n{Nest(PdfObjectNesting.MaxDepth + 1)}\nendobj\n"), ct)); + Assert.Throws(() => PdfObjectNesting.Check(ObjectStream(Nest(PdfObjectNesting.MaxDepth + 1)), ct)); + // About 1,900 levels already overflowed a 1 MB stack in PDFsharp. + var path = Save(Pdf($"4 0 obj\n{Nest(2_000)}\nendobj\n")); + Refused(() => PdfSharpConverter.Open(path, PdfDocumentOpenMode.Import, ct).Dispose()); + } + + [Fact] + public void Objects_read_over_and_over_are_refused_quickly() + { + // 250 objects open inside each other, each read to the end of the file: hundreds of times its size. + var text = string.Concat(Enumerable.Range(4, 250).Select(n => $"\n{n} 0 obj [")) + + string.Concat(Enumerable.Repeat("1 ", 1_000_000)) + new string(']', 250) + "\nendobj\n"; + var watch = System.Diagnostics.Stopwatch.StartNew(); + var refusal = Assert.Throws(() => PdfObjectNesting.Check(Pdf(text, count: 253), TestContext.Current.CancellationToken)); + Assert.Contains("overlap", refusal.Message, StringComparison.Ordinal); + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(10), $"Took {watch.Elapsed}"); + } + + [Fact] + public void A_file_is_read_through_its_window_as_memory_is() + { + // The "obj" keyword of the deep object crosses the end of the first 1 MB window. + var head = Pdf(""); + var prefix = "%PDF-1.4\n1 0 obj\n<>\nendobj\n2 0 obj\n<>\nendobj\n" + + "3 0 obj\n<>\nendobj\n"; + var padding = (1 << 20) - 2 - prefix.Length - "%\n4 0 ".Length; + var objects = "%" + new string(' ', padding) + "\n4 0 obj\n" + Nest(10_000) + "\nendobj\n"; + var deep = Pdf(objects); + Assert.Equal((1 << 20) - 2, Encoding.Latin1.GetString(deep).IndexOf("\n4 0 obj", StringComparison.Ordinal) + 5); + var ct = TestContext.Current.CancellationToken; + Assert.Throws(() => PdfObjectNesting.Check(Save(deep), ct)); + Assert.Throws(() => PdfObjectNesting.Check(deep, ct)); + PdfObjectNesting.Check(Save(Pdf(objects.Replace(Nest(10_000), Nest(10), StringComparison.Ordinal))), ct); + PdfObjectNesting.Check(Save(head), ct); + } + + [Fact] + public void A_cancelled_check_stops() + { + var pdf = Pdf($"4 0 obj\n{Nest(10)}\nendobj\n"); + Assert.ThrowsAny(() => PdfObjectNesting.Check(pdf, new CancellationToken(canceled: true))); + } + + // ───────────────────────── Read ───────────────────────── + + [Fact] + public void Real_nesting_opens_from_objects_and_object_streams() + { + var ct = TestContext.Current.CancellationToken; + using (var document = PdfObjectNesting.Open(Pdf($"4 0 obj\n<>]] %]]]\n/C <5B5B>>>\nendobj\n"), PdfDocumentOpenMode.Import, ct)) + Assert.Equal(1, document.PageCount); + using (var document = PdfObjectNesting.Open(ObjectStream("<>", typed: false, indirect: true), PdfDocumentOpenMode.Import, ct)) + { + var value = Assert.IsType(((PdfSharp.Pdf.Advanced.PdfReference)document.Pages[0].Elements["/X"]!).Value); + Assert.Equal(3, value.Elements.GetArray("/A")!.Elements.Count); + } + } + + [Fact] + public void Files_PDFsharp_saves_and_Filee_packs_into_object_streams_are_checked_object_by_object() + { + static byte[] Saved(int levels) + { + using var document = new PdfDocument(); + var page = document.AddPage(); + var outer = new PdfArray(document); + var inner = outer; + for (var i = 1; i < levels; i++) + { + var next = new PdfArray(document); + inner.Elements.Add(next); + inner = next; + } + document.Internals.AddObject(outer); + page.Elements["/X"] = outer.Reference; + var output = new MemoryStream(); + document.Save(output); + return output.ToArray(); + } + + var ct = TestContext.Current.CancellationToken; + var shallow = Saved(20); + var packed = PdfObjectStreams.Pack(shallow); + Assert.NotNull(packed); + PdfObjectNesting.Check(shallow, ct); + PdfObjectNesting.Check(packed, ct); + using (var document = PdfObjectNesting.Open(packed, PdfDocumentOpenMode.Import, ct)) + Assert.Equal(1, document.PageCount); + + var deep = PdfObjectStreams.Pack(Saved(PdfObjectNesting.MaxDepth + 10)); + Assert.NotNull(deep); + Assert.DoesNotContain("[[[", Encoding.Latin1.GetString(deep), StringComparison.Ordinal); // only in the deflated stream + Assert.Throws(() => PdfObjectNesting.Check(deep, ct)); + } + + [Fact] + public void Every_PDFsharp_open_of_a_file_Filee_did_not_write_goes_through_the_check() + { + var dir = new DirectoryInfo(AppContext.BaseDirectory); + while (dir is not null && !Directory.Exists(Path.Combine(dir.FullName, "src", "Filee.Engines"))) + dir = dir.Parent; + if (dir is null) + return; // run outside the repository + // The checked opens, and PDFs Filee or Tesseract wrote itself. + string[] allowed = + [ + "Pdf/PdfObjectNesting.cs", // the checked open + "Pdf/PdfSharpConverter.cs", // OpenTolerant checks the file first + "Pdf/PdfSheets.cs", // XPdfForm reads the file PdfSharpConverter.Open checked, or its mended bytes + "Rendering/PdfPageWriter.cs", // the PDF the page writer just wrote + "Ocr/TesseractConverter.cs", // the pages and text layer Tesseract wrote + ]; + var src = Path.Combine(dir.FullName, "src"); + var unchecked_ = Directory.EnumerateFiles(src, "*.cs", SearchOption.AllDirectories) + .Where(file => File.ReadAllText(file) is var text + && (text.Contains("PdfReader.Open(", StringComparison.Ordinal) || text.Contains("XPdfForm.From", StringComparison.Ordinal))) + .Select(file => Path.GetRelativePath(Path.Combine(src, "Filee.Engines"), file).Replace('\\', '/')) + .Where(file => !allowed.Contains(file)) + .ToList(); + Assert.Empty(unchecked_); + } +} diff --git a/tests/Filee.Engines.Tests/SpreadsheetFormatTests.cs b/tests/Filee.Engines.Tests/SpreadsheetFormatTests.cs index 7890c5f7d..ae7e6faf9 100644 --- a/tests/Filee.Engines.Tests/SpreadsheetFormatTests.cs +++ b/tests/Filee.Engines.Tests/SpreadsheetFormatTests.cs @@ -148,16 +148,15 @@ XElement Part(string name) var cells = Part("xl/worksheets/sheet1.xml").Descendants(S + "c").ToDictionary(c => (string)c.Attribute("r")!); var styles = Part("xl/styles.xml"); var xfs = styles.Element(S + "cellXfs")!.Elements(S + "xf").ToList(); - string FormatCode(string reference) - { - var id = (int)xfs[(int)cells[reference].Attribute("s")!].Attribute("numFmtId")!; - return (string)styles.Descendants(S + "numFmt").Single(f => (int)f.Attribute("numFmtId")! == id).Attribute("formatCode")!; - } + int FormatId(string reference) => (int)xfs[(int)cells[reference].Attribute("s")!].Attribute("numFmtId")!; + string FormatCode(string reference) => + (string)styles.Descendants(S + "numFmt").Single(f => (int)f.Attribute("numFmtId")! == FormatId(reference)).Attribute("formatCode")!; // Real numbers with their formats, not text: Excel can calculate with them. Assert.Null(cells["B3"].Attribute("t")); Assert.Equal("1350000", (string)cells["B3"].Element(S + "v")!); Assert.Equal("#,##0\"원\"", FormatCode("B3")); - Assert.Equal(("46296", "m/d/yyyy"), ((string)cells["C3"].Element(S + "v")!, FormatCode("C3"))); + // The short date stays Excel's format 14, which shows in the regional format of whoever opens the file. + Assert.Equal(("46296", 14), ((string)cells["C3"].Element(S + "v")!, FormatId("C3"))); Assert.Equal("b", (string?)cells["C5"].Attribute("t")); } @@ -168,6 +167,47 @@ string FormatCode(string reference) Assert.Equal(14, result.Sheets[0].ColumnWidth(0)); } + [Theory] + [InlineData("en-US", "m/d/yyyy", true)] + [InlineData("zh-CN", "yyyy/m/d", true)] + [InlineData("en-US", "yyyy-mm-dd", false)] // a date format of the file's own + [InlineData("en-US", "d/m/yyyy", false)] + [InlineData("th-TH", "yyyy-mm-dd", false)] // Buddhist years: the ISO date read for 14 here isn't Excel's 14 there + public void Xlsx_writer_stores_the_pcs_short_date_as_format_14(string culture, string code, bool builtIn) + { + using var scope = new CultureScope(culture); + var book = new Workbook(); + var sheet = new Worksheet("Sheet1"); + sheet.Set(0, 0, new SheetCell("date", CellStyle.Default, IsNumber: true) { Value = October1, NumberFormat = code }); + book.Sheets.Add(sheet); + var path = Path.Combine(fx.NewFolder(), "dates.xlsx"); + + XlsxWriter.Write(book, path); + + XElement styles, cell; + using (var zip = ZipFile.OpenRead(path)) + { + using (var stream = zip.GetEntry("xl/styles.xml")!.Open()) + styles = XElement.Load(stream); + using (var stream = zip.GetEntry("xl/worksheets/sheet1.xml")!.Open()) + cell = XElement.Load(stream).Descendants(S + "c").Single(); + } + var id = (int)styles.Element(S + "cellXfs")!.Elements(S + "xf").ElementAt((int)cell.Attribute("s")!).Attribute("numFmtId")!; + var custom = styles.Descendants(S + "numFmt").Select(f => (string)f.Attribute("formatCode")!).ToList(); + if (builtIn) + { + Assert.Equal(14, id); + Assert.Empty(custom); + } + else + { + Assert.True(id >= 164, $"{id}"); + Assert.Equal([code], custom); + } + // Read back on the same PC, the date shows as before. + Assert.Equal(code, XlsxReader.Read(path).Sheets[0].Get(0, 0)!.NumberFormat); + } + // ───────────────────────── ODS ───────────────────────── private const string OdsStyles = diff --git a/tests/Filee.Engines.Tests/VisioTests.cs b/tests/Filee.Engines.Tests/VisioTests.cs index 83bad2978..b89eb70c7 100644 --- a/tests/Filee.Engines.Tests/VisioTests.cs +++ b/tests/Filee.Engines.Tests/VisioTests.cs @@ -380,7 +380,7 @@ public void The_drawing_converter_reads_vsdx_and_leaves_binary_vsd_to_libreoffic { var converter = new DrawingConverter(); Assert.Equal([("vsd", "pdf"), ("vsd", "svg"), ("vsd", "png")], converter.Edges.Where(e => e.From == "vsd").Select(e => (e.From, e.To))); - Assert.All(converter.Edges, e => Assert.True(e.Checked)); + Assert.All(converter.Edges.Where(e => e.From == "vsd"), e => Assert.True(e.Checked)); var binary = Path.Combine(_folder, "old.vsd"); File.WriteAllBytes(binary, [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1, 0, 0, 0, 0]); diff --git a/tests/Filee.Engines.Tests/WordToTextTests.cs b/tests/Filee.Engines.Tests/WordToTextTests.cs new file mode 100644 index 000000000..15d8978f7 --- /dev/null +++ b/tests/Filee.Engines.Tests/WordToTextTests.cs @@ -0,0 +1,205 @@ +// Word (DOCX, DOC) and ODT → TXT on the built-in route, as RTF → TXT (#1204, RtfToBuiltInTextTests): the document's +// own paragraphs a line each, with their leading spaces, tabs, runs of spaces and empty paragraphs, list numbers and +// table rows on lines of their own. Before, with the Filee renderer on, the text came from the renderer's PDF. The +// routes with LibreOffice are in FileeRendererConverterTests. The documents are built here. + +using Filee.Core.Conversion; +using Filee.Core.Settings; +using Filee.Engines.Hwp; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Infrastructure; +using static Filee.Engines.Tests.DocxBuilder; + +namespace Filee.Engines.Tests; + +[Collection("EngineEnvironment")] // the Filee renderer is on, as in the app, so the route through a PDF is there to lose +public sealed class WordToBuiltInTextTests(EngineFixture fx) : IClassFixture, IDisposable +{ + /// What every document here comes out as. + private static readonly string[] Expected = + [ + "Notes on the build", + "", // the empty paragraph, and no empty line between the others + "Steps:", + "1. Install the tools", + "2. Run the build", + " • on Windows", + "int main(void) {", + " int total = 0; // two spaces", + "\tfor (int i = 0; i < 3; i++)", + " total += i;", + " return total;", + "}", + "first", // a line break + "second", + "Name\tValue", + "Width\t120", + "End.", + "", + ]; + + public void Dispose() + { + EngineEnvironment.FileeRenderer = false; + fx.Catalog.Refresh(); + } + + private async Task ConvertAsync(string source, string format) + { + EngineEnvironment.FileeRenderer = true; + var catalog = new ConverterCatalog(fx.Converters.Where(c => c.Id is not ("libreoffice" or "pandoc"))) + { + Priority = new AppSettings().EnginePriority, + }; + // Not the renderer's PDF and the PDF's text, which wrapped long lines and lost the indentation. + Assert.Equal(HwpTextConverter.EngineId, Assert.Single(catalog.CreatePlanner().Plan(format, "txt")!.Steps).Converter.Id); + + var output = await MarkupTestKit.ConvertAsync(catalog, source, "txt"); + return (await File.ReadAllTextAsync(output, TestContext.Current.CancellationToken)).TrimStart('').ReplaceLineEndings("\n"); + } + + [Theory] + [InlineData("docx")] + [InlineData("docm")] + [InlineData("dotx")] + public async Task Docx_comes_out_a_line_per_paragraph(string format) + { + var docx = new DocxBuilder + { + Numbering = """ + + + + + + """, + }; + static string Item(string text, int level) => P(R(text), $""); + static string Cell(string text) => $"{P(R(text))}"; + docx.Paragraph(P(R("Notes on the build"), "")) + .Paragraph(P("")) + .Paragraph(P(R("Steps:"))) + .Paragraph(Item("Install the tools", 0)) + .Paragraph(Item("Run the build", 0)) + .Paragraph(Item("on Windows", 1)) + .Paragraph(P(R("int main(void) {"), "")) // the indentation of the paragraph isn't text + .Paragraph(P(R(" int total = 0;") + R(" // two spaces", ""))) + .Paragraph(P("" + R("for (int i = 0; i < 3; i++)"))) + .Paragraph(P(R(" total += i;"))) + .Paragraph(P(R(" return total;"))) + .Paragraph(P(R("}"))) + .Paragraph(P(R("first") + "" + R("second"))) + .Paragraph("" + + $"{Cell("Name")}{Cell("Value")}{Cell("Width")}{Cell("120")}") + .Paragraph(P(R("End."))) + .Paragraph(P("")); // Word's last empty paragraph: no empty line at the end + var source = docx.Save(Path.Combine(fx.NewFolder(), "notes." + format)); + + Assert.Equal(string.Join("\n", Expected), await ConvertAsync(source, format)); + } + + [Fact] + public async Task Odt_comes_out_a_line_per_paragraph() + { + var odt = new OdtBuilder + { + ContentStyles = """ + + + + + """, + Body = """ + Notes on the build + + Steps: + + Install the tools + Run the build + on Windows + + + int main(void) { + int total = 0; // two spaces + for (int i = 0; i < 3; i++) + total += i; + return total; + } + firstsecond + + + NameValue + Width120 + + End. + """, + }; + var source = odt.Save(Path.Combine(fx.NewFolder(), "notes.odt")); + + Assert.Equal(string.Join("\n", Expected), await ConvertAsync(source, "odt")); + } + + [Fact] + public async Task Doc_comes_out_a_line_per_paragraph() + { + var doc = new DocBuilder(); + var numbered = doc.List(100, [new DocBuilder.Level(0, "\u0000.", 1, DocBuilder.Sprm(0x845E, (short)720)), + new DocBuilder.Level(23, "", 1, DocBuilder.Sprm(0x845E, (short)1440))]); + byte[] Item(int level) => [.. DocBuilder.Sprm(0x460B, (short)numbered), .. DocBuilder.Sprm(0x260A, (byte)level)]; + var tap = (byte[])[.. DocBuilder.DefTable([0, 2000, 4000])]; + doc.Paragraph(DocBuilder.R("Notes on the build")) + .Paragraph() + .Paragraph(DocBuilder.R("Steps:")) + .Paragraph(0, Item(0), DocBuilder.R("Install the tools")) + .Paragraph(0, Item(0), DocBuilder.R("Run the build")) + .Paragraph(0, Item(1), DocBuilder.R("on Windows")) + .Paragraph(DocBuilder.R("int main(void) {")) + .Paragraph(DocBuilder.R(" int total = 0; // two spaces")) + .Paragraph(DocBuilder.R("\tfor (int i = 0; i < 3; i++)")) + .Paragraph(DocBuilder.R(" total += i;")) + .Paragraph(DocBuilder.R(" return total;")) + .Paragraph(DocBuilder.R("}")) + .Paragraph(DocBuilder.R("first\vsecond")) // Shift+Enter + .Row(tap, [DocBuilder.R("Name")], [DocBuilder.R("Value")]) + .Row(tap, [DocBuilder.R("Width")], [DocBuilder.R("120")]) + .Paragraph(DocBuilder.R("End.")); + var source = Path.Combine(fx.NewFolder(), "notes.doc"); + await File.WriteAllBytesAsync(source, doc.Build(), TestContext.Current.CancellationToken); + + Assert.Equal(string.Join("\n", Expected), await ConvertAsync(source, "doc")); + } + + [Fact] + public void A_table_in_a_cell_keeps_its_text_in_the_cell() + { + var inner = new HTable(); + var innerRow = new HRow(); + innerRow.Cells.AddRange([Cell(Paragraph("inner a")), Cell(Paragraph("inner b"))]); + inner.Rows.Add(innerRow); + var outer = new HTable(); + var row = new HRow(); + var nested = Cell(Paragraph("before")); + nested.Blocks.Add(inner); + row.Cells.AddRange([Cell(Paragraph("left")), nested]); + outer.Rows.Add(row); + var document = new HDocument(); + document.Sections.Add(new HSection { Blocks = { outer } }); + + Assert.Equal("left\tbefore inner a inner b\n", PlainTextWriter.WriteLines(document).ReplaceLineEndings("\n")); + Assert.Equal("left\tbefore inner a inner b\n", PlainTextWriter.Write(document).ReplaceLineEndings("\n")); + + static HParagraph Paragraph(string text) + { + var paragraph = new HParagraph(); + paragraph.Inlines.Add(new HText(text, default)); + return paragraph; + } + + static HCell Cell(HParagraph paragraph) + { + var cell = new HCell(); + cell.Blocks.Add(paragraph); + return cell; + } + } +} diff --git a/tests/Filee.Engines.Tests/XlsbBuilder.cs b/tests/Filee.Engines.Tests/XlsbBuilder.cs new file mode 100644 index 000000000..360e07593 --- /dev/null +++ b/tests/Filee.Engines.Tests/XlsbBuilder.cs @@ -0,0 +1,765 @@ +// A workbook written in code twice: as an Excel binary workbook (BIFF12 records, [MS-XLSB]: a 1–2 byte record type +// and a 1–4 byte size, 7 bits per byte) and as the same workbook in XLSX, so the tests compare what the XLSB reader +// reads with what the XLSX reader reads. Both share one style sheet (fonts with theme, tinted and indexed colours, +// solid, pattern and gradient fills, borders with diagonals, number formats, alignment). + +using System.Buffers.Binary; +using System.Globalization; +using System.Text; + +namespace Filee.Engines.Tests; + +internal sealed class XlsbBuilder +{ + // Cell formats (cell XF index) of the shared style sheet. + public const int Plain = 0; + public const int Header = 1; // bold white on #1F4E79, thin borders, centred both ways + public const int Won = 2; // #,##0"원" (custom 164) + public const int Date = 3; // built-in 14 + public const int Percent = 4; // 0.0% (custom 165) + public const int Accent = 5; // italic struck-through underlined 14 pt dark red, right, top, wrapped + public const int Pattern = 6; // dark grey pattern (red on white), left, indent 2 + public const int Gradient = 7; // white to blue, turned 45°, shrunk to fit + public const int Diagonal = 8; // both diagonals (medium, blue), stacked letters + public const int Across = 9; // tinted accent1 맑은 고딕, double underline; double top, dashed red bottom; centre across, justified vertically + public const int Decimals = 10; // 0.00, distributed + + /// Fonts: height in twips, bold, italic, underline (BrtFont uls), strike, script (sss), colour, name. + private static readonly (int Height, bool Bold, bool Italic, int Underline, bool Strike, int Script, XlsbColor Color, string Name)[] Fonts = + [ + (220, false, false, 0, false, 0, XlsbColor.Theme(1), "Calibri"), + (220, true, false, 0, false, 0, XlsbColor.Rgb(0xFFFFFF), "Calibri"), + (280, false, true, 1, true, 0, XlsbColor.Rgb(0xC00000), "Calibri"), + (220, false, false, 2, false, 0, XlsbColor.Theme(4, 0.39997558519241921), "맑은 고딕"), + (220, false, false, 0, false, 1, XlsbColor.Indexed(10), "Calibri"), + ]; + + /// Fills: BrtFill fls (0x28 gradient), foreground, background; gradient degree and stops. + private static readonly (int Pattern, XlsbColor Fore, XlsbColor Back, double Degree, (double Position, XlsbColor Color)[] Stops)[] Fills = + [ + (0, XlsbColor.Auto, XlsbColor.Auto, 0, []), + (17, XlsbColor.Auto, XlsbColor.Auto, 0, []), + (1, XlsbColor.Rgb(0x1F4E79), XlsbColor.Indexed(64), 0, []), + (3, XlsbColor.Rgb(0xFF0000), XlsbColor.Rgb(0xFFFFFF), 0, []), + (0x28, XlsbColor.Auto, XlsbColor.Auto, 90, [(0, XlsbColor.Rgb(0xFFFFFF)), (1, XlsbColor.Rgb(0x0000FF))]), + ]; + + /// Borders: diagonal flags (1 down, 2 up), then top, bottom, left, right, diagonal as (Blxf dg, colour). + private static readonly (int Flags, (int Style, XlsbColor Color)[] Sides)[] Borders = + [ + (0, [(0, XlsbColor.Auto), (0, XlsbColor.Auto), (0, XlsbColor.Auto), (0, XlsbColor.Auto), (0, XlsbColor.Auto)]), + (0, [(1, XlsbColor.Auto), (1, XlsbColor.Auto), (1, XlsbColor.Auto), (1, XlsbColor.Auto), (0, XlsbColor.Auto)]), + (3, [(0, XlsbColor.Auto), (0, XlsbColor.Auto), (0, XlsbColor.Auto), (0, XlsbColor.Auto), (2, XlsbColor.Rgb(0x0000FF))]), + (0, [(6, XlsbColor.Auto), (3, XlsbColor.Indexed(10)), (0, XlsbColor.Auto), (0, XlsbColor.Auto), (0, XlsbColor.Auto)]), + ]; + + private static readonly (int Id, string Code)[] Formats = [(164, "#,##0\"원\""), (165, "0.0%")]; + + /// Cell XFs: number format, font, fill, border, rotation, indent, alc, alcv, wrap, shrink. + private static readonly (int Format, int Font, int Fill, int Border, int Rotation, int Indent, int Horizontal, int Vertical, bool Wrap, bool Shrink)[] Xfs = + [ + (0, 0, 0, 0, 0, 0, 0, 2, false, false), + (0, 1, 2, 1, 0, 0, 2, 1, false, false), + (164, 0, 0, 0, 0, 0, 0, 2, false, false), + (14, 0, 0, 0, 0, 0, 0, 2, false, false), + (165, 0, 0, 0, 0, 0, 0, 2, false, false), + (0, 2, 0, 0, 0, 0, 3, 0, true, false), + (0, 0, 3, 0, 0, 2, 1, 2, false, false), + (0, 0, 4, 0, 45, 0, 0, 2, false, true), + (0, 0, 0, 2, 255, 0, 0, 2, false, false), + (0, 3, 0, 3, 0, 0, 6, 3, false, false), + (2, 4, 0, 0, 0, 0, 7, 2, false, false), + ]; + + private static readonly string[] Horizontal = ["general", "left", "center", "right", "fill", "justify", "centerContinuous", "distributed"]; + private static readonly string[] Vertical = ["top", "center", "bottom", "justify", "distributed"]; + private static readonly string[] BorderNames = ["none", "thin", "medium", "dashed", "dotted", "thick", "double", "hair"]; + private static readonly string?[] PatternNames = [null, "solid", "mediumGray", "darkGray"]; + + private readonly List<(string Text, (int Start, int Font)[]? Runs)> _strings = []; + + public List Sheets { get; } = []; + + public bool Date1904 { get; set; } + + /// Print areas and print titles: the sheet's position, "Print_Area" or "Print_Titles", the ranges (0-based, inclusive). + public List<(int Sheet, string Name, (int Top, int Left, int Bottom, int Right)[] Ranges)> Names { get; } = []; + + /// More parts (drawings, VML) shared by both packages, by zip path. + public Dictionary Parts { get; } = []; + + /// Binary parts (pictures), by zip path. + public Dictionary Media { get; } = []; + + /// Records the workbook part gets after its sheets (for hostile tests). + public List ExtraWorkbookRecords { get; } = []; + + public XlsbSheet Sheet(string name, int state = 0) + { + var sheet = new XlsbSheet(this, name, state); + Sheets.Add(sheet); + return sheet; + } + + /// The index of a shared string (rich text when are given: start, font). + public int StringIndex(string text, (int Start, int Font)[]? runs = null) + { + var index = _strings.FindIndex(s => s.Text == text && (s.Runs ?? []).SequenceEqual(runs ?? [])); + if (index >= 0) + return index; + _strings.Add((text, runs)); + return _strings.Count - 1; + } + + // ───────────────────────── XLSB ───────────────────────── + + public string SaveXlsb(string path) + { + var parts = new Dictionary + { + ["[Content_Types].xml"] = Utf8(ContentTypes(binary: true)), + ["_rels/.rels"] = Utf8(OpcWriter.Declaration + OpcWriter.Relationships([("rId1", "officeDocument", "xl/workbook.bin")])), + ["xl/_rels/workbook.bin.rels"] = Utf8(OpcWriter.Declaration + WorkbookRelationships("bin")), + ["xl/workbook.bin"] = WorkbookBin(), + ["xl/styles.bin"] = StylesBin(), + ["xl/sharedStrings.bin"] = SharedStringsBin(), + }; + for (var i = 0; i < Sheets.Count; i++) + { + parts[$"xl/worksheets/sheet{i + 1}.bin"] = Sheets[i].Bin(); + if (Sheets[i].Relationships.Count > 0) + parts[$"xl/worksheets/_rels/sheet{i + 1}.bin.rels"] = Utf8(OpcWriter.Declaration + OpcWriter.Relationships(Sheets[i].Relationships)); + } + return Save(path, parts); + } + + private byte[] WorkbookBin() + { + var w = new RecordWriter(); + w.Record(131, []); // BrtBeginBook + w.Record(153, [.. U32(Date1904 ? 1u : 0u), .. U32(0), .. WideString("ThisWorkbook")]); // BrtWbProp + w.Record(143, []); // BrtBeginBundleShs + for (var i = 0; i < Sheets.Count; i++) + w.Record(156, [.. U32((uint)Sheets[i].State), .. U32((uint)i + 1), .. WideString($"rId{i + 1}"), .. WideString(Sheets[i].Name)]); + w.Record(144, []); // BrtEndBundleShs + foreach (var (sheet, name, ranges) in Names) + { + var rgce = new List(); + if (ranges.Length > 1) + rgce.AddRange([0x29, .. U16((ushort)(ranges.Length * 15 + ranges.Length - 1))]); // PtgMemFunc + for (var i = 0; i < ranges.Length; i++) + { + var (top, left, bottom, right) = ranges[i]; + rgce.AddRange([0x3B, .. U16(0), .. U32((uint)top), .. U32((uint)bottom), .. U16((ushort)left), .. U16((ushort)right)]); // PtgArea3d + if (i > 0) + rgce.Add(0x10); // PtgUnion + } + w.Record(39, [.. U32(0x20), 0, .. U32((uint)sheet), .. WideString("_xlnm." + name), .. U32((uint)rgce.Count), .. rgce, .. U32(0), + .. NullString(), .. NullString(), .. NullString(), .. NullString(), .. NullString()]); + } + foreach (var record in ExtraWorkbookRecords) + w.Raw(record); + w.Record(132, []); // BrtEndBook + return w.ToArray(); + } + + private static byte[] StylesBin() + { + var w = new RecordWriter(); + w.Record(278, []); // BrtBeginStyleSheet + w.Record(615, U32((uint)Formats.Length)); + foreach (var (id, code) in Formats) + w.Record(44, [.. U16((ushort)id), .. WideString(code)]); + w.Record(616, []); + w.Record(611, U32((uint)Fonts.Length)); + foreach (var f in Fonts) + { + ushort flags = (ushort)((f.Italic ? 0x02 : 0) | (f.Strike ? 0x08 : 0)); + w.Record(43, [.. U16((ushort)f.Height), .. U16(flags), .. U16((ushort)(f.Bold ? 700 : 400)), .. U16((ushort)f.Script), (byte)f.Underline, 2, 1, 0, + .. f.Color.Bytes(), 2, .. WideString(f.Name)]); + } + w.Record(612, []); + w.Record(603, U32((uint)Fills.Length)); + foreach (var fill in Fills) + { + var data = new List([.. U32((uint)fill.Pattern), .. fill.Fore.Bytes(), .. fill.Back.Bytes(), .. U32(0), .. F64(fill.Degree), + .. F64(0), .. F64(0), .. F64(0), .. F64(0), .. U32((uint)fill.Stops.Length)]); + foreach (var (position, color) in fill.Stops) + data.AddRange([.. color.Bytes(), .. F64(position)]); + w.Record(45, [.. data]); + } + w.Record(604, []); + w.Record(613, U32((uint)Borders.Length)); + foreach (var (flags, sides) in Borders) + w.Record(46, [(byte)flags, .. sides.SelectMany(s => (byte[])[(byte)s.Style, 0, .. s.Color.Bytes()])]); + w.Record(614, []); + w.Record(626, U32(1)); // BrtBeginCellStyleXFs + w.Record(47, Xf(0xFFFF, (0, 0, 0, 0, 0, 0, 0, 2, false, false))); + w.Record(627, []); + w.Record(617, U32((uint)Xfs.Length)); // BrtBeginCellXFs + foreach (var xf in Xfs) + w.Record(47, Xf(0, xf)); + w.Record(618, []); + w.Record(279, []); + return w.ToArray(); + } + + private static byte[] Xf(int parent, (int Format, int Font, int Fill, int Border, int Rotation, int Indent, int Horizontal, int Vertical, bool Wrap, bool Shrink) xf) + { + var alignment = (ushort)(xf.Horizontal | xf.Vertical << 3 | (xf.Wrap ? 0x40 : 0) | (xf.Shrink ? 0x100 : 0)); + return [.. U16((ushort)parent), .. U16((ushort)xf.Format), .. U16((ushort)xf.Font), .. U16((ushort)xf.Fill), .. U16((ushort)xf.Border), + (byte)xf.Rotation, (byte)xf.Indent, .. U16(alignment), .. U16(0)]; + } + + private byte[] SharedStringsBin() + { + var w = new RecordWriter(); + w.Record(159, [.. U32((uint)_strings.Count), .. U32((uint)_strings.Count)]); // BrtBeginSst + foreach (var (text, runs) in _strings) + { + if (runs is null) + w.Record(19, [0, .. WideString(text)]); + else + w.Record(19, [1, .. WideString(text), .. U32((uint)runs.Length), .. runs.SelectMany(r => (byte[])[.. U16((ushort)r.Start), .. U16((ushort)r.Font)])]); + } + w.Record(160, []); + return w.ToArray(); + } + + // ───────────────────────── XLSX ───────────────────────── + + private const string Namespaces = "xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\""; + + public string SaveXlsx(string path) + { + var names = Names.Count == 0 ? "" : "" + string.Concat(Names.Select(n => + $"" + + string.Join(",", n.Ranges.Select(r => $"'{OpcWriter.Escape(Sheets[n.Sheet].Name)}'!{Reference(r)}")) + "")) + ""; + var parts = new Dictionary + { + ["[Content_Types].xml"] = Utf8(ContentTypes(binary: false)), + ["_rels/.rels"] = Utf8(OpcWriter.Declaration + OpcWriter.Relationships([("rId1", "officeDocument", "xl/workbook.xml")])), + ["xl/_rels/workbook.xml.rels"] = Utf8(OpcWriter.Declaration + WorkbookRelationships("xml")), + ["xl/workbook.xml"] = Utf8(OpcWriter.Declaration + $"" + (Date1904 ? "" : "") + "" + + string.Concat(Sheets.Select((s, i) => $" " state=\"hidden\"", 2 => " state=\"veryHidden\"", _ => "" }) + $" r:id=\"rId{i + 1}\"/>")) + + "" + names + ""), + ["xl/styles.xml"] = Utf8(OpcWriter.Declaration + $"{StylesXml()}"), + ["xl/sharedStrings.xml"] = Utf8(OpcWriter.Declaration + $"" + string.Concat(_strings.Select(StringXml)) + ""), + }; + for (var i = 0; i < Sheets.Count; i++) + { + parts[$"xl/worksheets/sheet{i + 1}.xml"] = Utf8(OpcWriter.Declaration + Sheets[i].Xml()); + if (Sheets[i].Relationships.Count > 0) + parts[$"xl/worksheets/_rels/sheet{i + 1}.xml.rels"] = Utf8(OpcWriter.Declaration + OpcWriter.Relationships(Sheets[i].Relationships)); + } + return Save(path, parts); + } + + private static string Reference((int Top, int Left, int Bottom, int Right) r) + { + if (r.Left == 0 && r.Right == 16383) + return $"${r.Top + 1}:${r.Bottom + 1}"; + if (r.Top == 0 && r.Bottom == 1048575) + return $"${Column(r.Left)}:${Column(r.Right)}"; + return $"${Column(r.Left)}${r.Top + 1}:${Column(r.Right)}${r.Bottom + 1}"; + } + + internal static string Column(int column) => column < 26 ? ((char)('A' + column)).ToString() : Column(column / 26 - 1) + (char)('A' + column % 26); + + private static string StylesXml() + { + var sb = new StringBuilder(); + sb.Append(""); + foreach (var (id, code) in Formats) + sb.Append(CultureInfo.InvariantCulture, $""); + sb.Append(""); + foreach (var f in Fonts) + sb.Append("").Append(FontXml(f, "name")).Append(""); + sb.Append(""); + foreach (var fill in Fills) + { + if (fill.Pattern == 0x28) + sb.Append(CultureInfo.InvariantCulture, $"") + .Append(string.Concat(fill.Stops.Select(s => $"{s.Color.Xml("color")}"))) + .Append(""); + else + sb.Append($"{fill.Fore.Xml("fgColor")}{fill.Back.Xml("bgColor")}"); + } + sb.Append(""); + foreach (var (flags, sides) in Borders) + { + string Side(string element, (int Style, XlsbColor Color) side) => + side.Style == 0 ? $"<{element}/>" : $"<{element} style=\"{BorderNames[side.Style]}\">{side.Color.Xml("color")}"; + sb.Append("') + .Append(Side("left", sides[2])).Append(Side("right", sides[3])).Append(Side("top", sides[0])).Append(Side("bottom", sides[1])) + .Append(Side("diagonal", sides[4])).Append(""); + } + sb.Append(""); + foreach (var xf in Xfs) + { + sb.Append(CultureInfo.InvariantCulture, $"") + .Append(CultureInfo.InvariantCulture, $""); + } + sb.Append(""); + return sb.ToString(); + } + + /// A font's properties as SpreadsheetML elements, all of them (so a rich text run sets each one). + private static string FontXml((int Height, bool Bold, bool Italic, int Underline, bool Strike, int Script, XlsbColor Color, string Name) f, string nameElement) => + $"" + + $" "single", 2 => "double", _ => "none" }}\"/>" + + $" "superscript", 2 => "subscript", _ => "baseline" }}\"/>" + + $"{f.Color.Xml("color")}<{nameElement} val=\"{OpcWriter.Escape(f.Name)}\"/>"; + + private static string StringXml((string Text, (int Start, int Font)[]? Runs) s) + { + if (s.Runs is not { } runs) + return $"{OpcWriter.Escape(s.Text)}"; + var sb = new StringBuilder(""); + if (runs[0].Start > 0) + sb.Append($"{OpcWriter.Escape(s.Text[..runs[0].Start])}"); + for (var i = 0; i < runs.Length; i++) + { + var end = i + 1 < runs.Length ? runs[i + 1].Start : s.Text.Length; + sb.Append($"{FontXml(Fonts[runs[i].Font], "rFont")}{OpcWriter.Escape(s.Text[runs[i].Start..end])}"); + } + return sb.Append("").ToString(); + } + + // ───────────────────────── Package ───────────────────────── + + private string WorkbookRelationships(string extension) => OpcWriter.Relationships( + Sheets.Select((_, i) => ($"rId{i + 1}", "worksheet", $"worksheets/sheet{i + 1}.{extension}")) + .Append(("rIdS", "sharedStrings", $"sharedStrings.{extension}")) + .Append(("rIdT", "styles", $"styles.{extension}"))); + + private string ContentTypes(bool binary) => OpcWriter.Declaration + + "" + + "" + + "" + + (binary + ? "" + : "") + + string.Concat(Parts.Keys.Where(p => p.StartsWith("xl/drawings/drawing", StringComparison.Ordinal)) + .Select(p => $"")) + + ""; + + private string Save(string path, Dictionary parts) + { + foreach (var (name, xml) in Parts) + parts[name] = Utf8(OpcWriter.Declaration + xml); + foreach (var (name, bytes) in Media) + parts[name] = bytes; + using var zip = System.IO.Compression.ZipFile.Open(path, System.IO.Compression.ZipArchiveMode.Create); + foreach (var (name, bytes) in parts) + { + using var stream = zip.CreateEntry(name).Open(); + stream.Write(bytes); + } + return path; + } + + private static byte[] Utf8(string text) => new UTF8Encoding(false).GetBytes(text); + + // ───────────────────────── Fields ───────────────────────── + + public static byte[] U16(ushort value) + { + var bytes = new byte[2]; + BinaryPrimitives.WriteUInt16LittleEndian(bytes, value); + return bytes; + } + + public static byte[] U32(uint value) + { + var bytes = new byte[4]; + BinaryPrimitives.WriteUInt32LittleEndian(bytes, value); + return bytes; + } + + public static byte[] F64(double value) + { + var bytes = new byte[8]; + BinaryPrimitives.WriteDoubleLittleEndian(bytes, value); + return bytes; + } + + public static byte[] WideString(string text) => [.. U32((uint)text.Length), .. Encoding.Unicode.GetBytes(text)]; + + public static byte[] NullString() => U32(0xFFFFFFFF); + + /// An RkNumber of an integer (fInt), perhaps stored × 100 (fx100). + public static uint RkInteger(int value, bool times100 = false) => (uint)(value << 2) | 0x02u | (times100 ? 1u : 0u); + + /// An RkNumber of a double whose low 34 bits are zero. + public static uint RkDouble(double value) => (uint)(BitConverter.DoubleToInt64Bits(value) >> 32) & 0xFFFFFFFC; +} + +/// A BrtColor and its SpreadsheetML twin. +internal readonly record struct XlsbColor(int Type, int Index, short Tint, int Value) +{ + public static XlsbColor Auto => new(0, 0, 0, 0); + + public static XlsbColor Rgb(int rgb) => new(2, 0, 0, rgb); + + public static XlsbColor Indexed(int index) => new(1, index, 0, 0); + + public static XlsbColor Theme(int index, double tint = 0) => new(3, index, (short)Math.Round(tint * 32767), 0); + + public byte[] Bytes() => + [ + (byte)((Type == 2 ? 1 : 0) | Type << 1), (byte)Index, .. XlsbBuilder.U16((ushort)Tint), + (byte)(Value >> 16), (byte)(Value >> 8), (byte)Value, 0xFF, + ]; + + public string Xml(string element) + { + var tint = Tint == 0 ? "" : $" tint=\"{(Tint / 32767.0).ToString("R", CultureInfo.InvariantCulture)}\""; + return Type switch + { + 1 => $"<{element} indexed=\"{Index}\"{tint}/>", + 2 => $"<{element} rgb=\"FF{Value:X6}\"{tint}/>", + 3 => $"<{element} theme=\"{Index}\"{tint}/>", + _ => $"<{element} auto=\"1\"/>", + }; + } +} + +/// Writes BIFF12 records: the type in one or two bytes, the size in one to four, 7 bits each. +internal sealed class RecordWriter +{ + private readonly MemoryStream _stream = new(); + + public RecordWriter Record(int type, byte[] data) + { + Raw(Encode(type, data)); + return this; + } + + public void Raw(byte[] record) => _stream.Write(record); + + public static byte[] Encode(int type, byte[] data) => [.. Header(type, data.Length), .. data]; + + /// A record header claiming bytes (which need not follow). + public static byte[] Header(int type, int size) + { + var bytes = new List(); + if (type < 128) + bytes.Add((byte)type); + else + bytes.AddRange([(byte)(type & 0x7F | 0x80), (byte)(type >> 7)]); + do + { + var b = (byte)(size & 0x7F); + size >>= 7; + bytes.Add(size > 0 ? (byte)(b | 0x80) : b); + } + while (size > 0); + return [.. bytes]; + } + + public byte[] ToArray() => _stream.ToArray(); +} + +/// One sheet of an workbook; rows and columns are 0-based. +internal sealed class XlsbSheet(XlsbBuilder book, string name, int state) +{ + private enum Kind { Blank, Rk, Real, Shared, Inline, Bool, Error, FormulaNumber, FormulaText, FormulaBool, FormulaError } + + private readonly SortedDictionary<(int Row, int Column), (Kind Kind, int Xf, object Value)> _cells = []; + private readonly SortedDictionary _rows = []; + private readonly List<(int First, int Last, double Width, bool Hidden)> _columns = []; + private readonly List<(int Top, int Left, int Bottom, int Right)> _merges = []; + private readonly List _rowBreaks = []; + private readonly List _columnBreaks = []; + + public string Name => name; + public int State => state; + + public int FrozenRows { get; set; } + public bool RightToLeft { get; set; } + + /// Paper code, landscape, scale, fit to page (wide, tall), page order over then down, first page number. + public (int Paper, bool Landscape, int Scale, bool Fit, int Wide, int Tall, bool OverThenDown, int? FirstPage)? PageSetup { get; set; } + + /// Margins in inches: left, right, top, bottom, header, footer. + public double[]? Margins { get; set; } + + public bool GridLines { get; set; } + public bool CenterHorizontally { get; set; } + public (string? Header, string? Footer, string? FirstHeader, bool DifferentFirst)? HeaderFooter { get; set; } + + /// The relationship id of the sheet's drawing (its relationship goes into ). + public string? DrawingId { get; set; } + + public List<(string Id, string Type, string Target)> Relationships { get; } = []; + + /// Records written into the sheet data after the cells (for hostile tests). + public List ExtraRecords { get; } = []; + + public XlsbSheet Text(int row, int column, string text, int xf = XlsbBuilder.Plain) => Cell(row, column, Kind.Shared, xf, book.StringIndex(text)); + + public XlsbSheet Rich(int row, int column, string text, params (int Start, int Font)[] runs) => + Cell(row, column, Kind.Shared, XlsbBuilder.Plain, book.StringIndex(text, runs)); + + /// A string stored in the cell itself (BrtCellSt). + public XlsbSheet Inline(int row, int column, string text, int xf = XlsbBuilder.Plain) => Cell(row, column, Kind.Inline, xf, text); + + public XlsbSheet Number(int row, int column, double value, int xf = XlsbBuilder.Plain) => Cell(row, column, Kind.Real, xf, value); + + /// A number as an RkNumber (, ). + public XlsbSheet Rk(int row, int column, uint rk, int xf = XlsbBuilder.Plain) => Cell(row, column, Kind.Rk, xf, rk); + + public XlsbSheet Bool(int row, int column, bool value) => Cell(row, column, Kind.Bool, XlsbBuilder.Plain, value); + + public XlsbSheet Error(int row, int column, byte code) => Cell(row, column, Kind.Error, XlsbBuilder.Plain, code); + + public XlsbSheet Blank(int row, int column, int xf) => Cell(row, column, Kind.Blank, xf, 0); + + /// A formula cell: only the cached value is written (as XLSX, its formula text is a placeholder). + public XlsbSheet Formula(int row, int column, object cached, int xf = XlsbBuilder.Plain) => cached switch + { + double number => Cell(row, column, Kind.FormulaNumber, xf, number), + string text => Cell(row, column, Kind.FormulaText, xf, text), + bool on => Cell(row, column, Kind.FormulaBool, xf, on), + byte error => Cell(row, column, Kind.FormulaError, xf, error), + _ => throw new ArgumentException("Not a cached value", nameof(cached)), + }; + + public XlsbSheet Height(int row, double points, bool custom = true, bool hidden = false) + { + _rows[row] = (points, custom, hidden); + return this; + } + + public XlsbSheet Width(int first, int last, double width, bool hidden = false) + { + _columns.Add((first, last, width, hidden)); + return this; + } + + public XlsbSheet Merge(int top, int left, int bottom, int right) + { + _merges.Add((top, left, bottom, right)); + return this; + } + + public XlsbSheet RowBreak(int row) + { + _rowBreaks.Add(row); + return this; + } + + public XlsbSheet ColumnBreak(int column) + { + _columnBreaks.Add(column); + return this; + } + + private XlsbSheet Cell(int row, int column, Kind kind, int xf, object value) + { + _cells[(row, column)] = (kind, xf, value); + return this; + } + + private IEnumerable RowNumbers => _cells.Keys.Select(k => k.Row).Concat(_rows.Keys).Distinct().Order(); + + private (double Height, bool Custom, bool Hidden) RowOf(int row) => _rows.TryGetValue(row, out var r) ? r : (15, false, false); + + // ───────────────────────── XLSB ───────────────────────── + + public byte[] Bin() + { + var w = new RecordWriter(); + w.Record(129, []); // BrtBeginSheet + var fit = PageSetup is { Fit: true } ? 0x100 : 0; + w.Record(147, [.. XlsbBuilder.U16((ushort)(0x04C0 | fit)), 0, .. XlsbColor.Auto.Bytes(), .. XlsbBuilder.U32(0xFFFFFFFF), .. XlsbBuilder.U32(0xFFFFFFFF), .. XlsbBuilder.U32(0)]); // BrtWsProp + w.Record(133, []); // BrtBeginWsViews + w.Record(137, [.. XlsbBuilder.U16((ushort)(0x03DC | (RightToLeft ? 0x20 : 0))), .. XlsbBuilder.U32(0), .. XlsbBuilder.U32(0), .. XlsbBuilder.U32(0), 64, 0, + .. XlsbBuilder.U16(0), .. XlsbBuilder.U16(100), .. XlsbBuilder.U16(0), .. XlsbBuilder.U16(0), .. XlsbBuilder.U16(0), .. XlsbBuilder.U32(0)]); + if (FrozenRows > 0) + w.Record(151, [.. XlsbBuilder.F64(0), .. XlsbBuilder.F64(FrozenRows), .. XlsbBuilder.U32((uint)FrozenRows), .. XlsbBuilder.U32(0), .. XlsbBuilder.U32(2), 0x02]); + w.Record(138, []); + w.Record(134, []); + w.Record(485, [.. XlsbBuilder.U32(0xFFFFFFFF), .. XlsbBuilder.U16(8), .. XlsbBuilder.U16(300), .. XlsbBuilder.U16(0), 0, 0]); // BrtWsFmtInfo + if (_columns.Count > 0) + { + w.Record(390, []); + foreach (var (first, last, width, hidden) in _columns) + w.Record(60, [.. XlsbBuilder.U32((uint)first), .. XlsbBuilder.U32((uint)last), .. XlsbBuilder.U32((uint)Math.Round(width * 256)), .. XlsbBuilder.U32(0), + .. XlsbBuilder.U16((ushort)((hidden ? 1 : 0) | 2))]); + w.Record(391, []); + } + w.Record(145, []); // BrtBeginSheetData + foreach (var row in RowNumbers) + { + var (height, custom, hidden) = RowOf(row); + var flags = (byte)((hidden ? 0x10 : 0) | (custom ? 0x20 : 0)); + w.Record(0, [.. XlsbBuilder.U32((uint)row), .. XlsbBuilder.U32(0), .. XlsbBuilder.U16((ushort)Math.Round(height * 20)), 0, flags, 0, .. XlsbBuilder.U32(0)]); + foreach (var ((_, column), (kind, xf, value)) in _cells.Where(c => c.Key.Row == row)) + { + List data = [.. XlsbBuilder.U32((uint)column), .. XlsbBuilder.U32((uint)xf)]; + var type = kind switch + { + Kind.Blank => 1, + Kind.Rk => 2, + Kind.Error => 3, + Kind.Bool => 4, + Kind.Real => 5, + Kind.Inline => 6, + Kind.Shared => 7, + Kind.FormulaText => 8, + Kind.FormulaNumber => 9, + Kind.FormulaBool => 10, + _ => 11, + }; + data.AddRange(kind switch + { + Kind.Blank => [], + Kind.Rk => XlsbBuilder.U32((uint)value), + Kind.Error or Kind.FormulaError => [(byte)value], + Kind.Bool or Kind.FormulaBool => [(byte)((bool)value ? 1 : 0)], + Kind.Real or Kind.FormulaNumber => XlsbBuilder.F64((double)value), + Kind.Shared => XlsbBuilder.U32((uint)(int)value), + _ => XlsbBuilder.WideString((string)value), + }); + if (type >= 8) + data.AddRange(Formula()); + w.Record(type, [.. data]); + } + } + foreach (var record in ExtraRecords) + w.Raw(record); + w.Record(146, []); // BrtEndSheetData + if (_merges.Count > 0) + { + w.Record(177, XlsbBuilder.U32((uint)_merges.Count)); + foreach (var (top, left, bottom, right) in _merges) + w.Record(176, [.. XlsbBuilder.U32((uint)top), .. XlsbBuilder.U32((uint)bottom), .. XlsbBuilder.U32((uint)left), .. XlsbBuilder.U32((uint)right)]); + w.Record(178, []); + } + w.Record(477, XlsbBuilder.U16((ushort)((CenterHorizontally ? 0x01 : 0) | (GridLines ? 0x08 : 0)))); + if (Margins is { } m) + w.Record(476, [.. m.SelectMany(x => XlsbBuilder.F64(x))]); + if (PageSetup is { } p) + { + var flags = (ushort)((p.OverThenDown ? 0x01 : 0) | (p.Landscape ? 0x02 : 0) | (p.FirstPage is not null ? 0x80 : 0)); + w.Record(478, [.. XlsbBuilder.U32((uint)p.Paper), .. XlsbBuilder.U32((uint)p.Scale), .. XlsbBuilder.U32(600), .. XlsbBuilder.U32(600), .. XlsbBuilder.U32(1), + .. XlsbBuilder.U32((uint)(p.FirstPage ?? 1)), .. XlsbBuilder.U32((uint)p.Wide), .. XlsbBuilder.U32((uint)p.Tall), .. XlsbBuilder.U16(flags), .. XlsbBuilder.NullString()]); + } + if (HeaderFooter is { } hf) + { + byte[] Text(string? text) => text is null ? XlsbBuilder.NullString() : XlsbBuilder.WideString(text); + w.Record(479, [.. XlsbBuilder.U16((ushort)((hf.DifferentFirst ? 0x02 : 0) | 0x0C)), .. Text(hf.Header), .. Text(hf.Footer), .. Text(null), .. Text(null), + .. Text(hf.FirstHeader), .. Text(null)]); + w.Record(480, []); + } + Breaks(w, 392, 393, _rowBreaks, 16383); + Breaks(w, 394, 395, _columnBreaks, 1048575); + if (DrawingId is not null) + w.Record(550, XlsbBuilder.WideString(DrawingId)); + w.Record(130, []); // BrtEndSheet + return w.ToArray(); + } + + /// A CellParsedFormula of one PtgInt (1) and no extra data. + private static byte[] Formula() => [.. XlsbBuilder.U16(0), .. XlsbBuilder.U32(3), 0x1E, .. XlsbBuilder.U16(1), .. XlsbBuilder.U32(0)]; + + private static void Breaks(RecordWriter w, int begin, int end, List breaks, int span) + { + if (breaks.Count == 0) + return; + w.Record(begin, [.. XlsbBuilder.U32((uint)breaks.Count), .. XlsbBuilder.U32((uint)breaks.Count)]); + foreach (var at in breaks) + w.Record(396, [.. XlsbBuilder.U32((uint)at), .. XlsbBuilder.U32(0), .. XlsbBuilder.U32((uint)span), .. XlsbBuilder.U32(1), .. XlsbBuilder.U32(0)]); + w.Record(end, []); + } + + // ───────────────────────── XLSX ───────────────────────── + + public string Xml() + { + var sb = new StringBuilder(""); + if (PageSetup is { Fit: true }) + sb.Append(""); + sb.Append("'); + if (FrozenRows > 0) + sb.Append(CultureInfo.InvariantCulture, $""); + sb.Append(""); + if (_columns.Count > 0) + { + sb.Append(""); + foreach (var (first, last, width, hidden) in _columns) + sb.Append(CultureInfo.InvariantCulture, $""); + sb.Append(""); + } + sb.Append(""); + foreach (var row in RowNumbers) + { + var (height, custom, hidden) = RowOf(row); + sb.Append(CultureInfo.InvariantCulture, $""); + } + sb.Append(""); + if (_merges.Count > 0) + { + sb.Append(""); + foreach (var (top, left, bottom, right) in _merges) + sb.Append($""); + sb.Append(""); + } + sb.Append(""); + if (Margins is { } m) + sb.Append(CultureInfo.InvariantCulture, $""); + if (PageSetup is { } p) + sb.Append(CultureInfo.InvariantCulture, + $""); + if (HeaderFooter is { } hf) + { + sb.Append(""); + if (hf.Header is not null) + sb.Append($"{OpcWriter.Escape(hf.Header)}"); + if (hf.Footer is not null) + sb.Append($"{OpcWriter.Escape(hf.Footer)}"); + if (hf.FirstHeader is not null) + sb.Append($"{OpcWriter.Escape(hf.FirstHeader)}"); + sb.Append(""); + } + if (_rowBreaks.Count > 0) + sb.Append("").Append(string.Concat(_rowBreaks.Select(b => $""))).Append(""); + if (_columnBreaks.Count > 0) + sb.Append("").Append(string.Concat(_columnBreaks.Select(b => $""))).Append(""); + if (DrawingId is not null) + sb.Append($""); + return sb.Append("").ToString(); + } + + private static string ErrorText(byte code) => code switch + { + 0x00 => "#NULL!", + 0x07 => "#DIV/0!", + 0x0F => "#VALUE!", + 0x17 => "#REF!", + 0x1D => "#NAME?", + 0x24 => "#NUM!", + _ => "#N/A", + }; +} diff --git a/tests/Filee.Engines.Tests/XlsbFuzzTests.cs b/tests/Filee.Engines.Tests/XlsbFuzzTests.cs new file mode 100644 index 000000000..416a0f55b --- /dev/null +++ b/tests/Filee.Engines.Tests/XlsbFuzzTests.cs @@ -0,0 +1,161 @@ +// Mutation fuzzing of the XLSB reader (Office/Sheets/XlsbReader): the seeds are XlsbTests' workbook with every kind of +// cell, format and page setting, and a small one, written in code. Most inputs change the BIFF12 records of a binary +// part (a field set to an edge value, a record's size or type changed, records cut, dropped or repeated), some the +// part's bytes, some the package (relationships, ZIP). Every input must be read or refused with InvalidDataException / +// ConversionRefusedException, in time and on a 1 MB stack; what is read is checked for what the printer leaves out and +// written as XLSX and as tables. 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 System.Buffers.Binary; +using System.IO.Compression; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Office.Sheets; + +namespace Filee.Engines.Tests; + +[Trait("Category", "Fuzz")] +public class XlsbFuzzTests(EngineFixture fx) : IClassFixture +{ + /// Inputs per seed in a normal test run. + private const int QuickIterations = 60; + + [Fact] + public void XlsbReader_reads_or_refuses_every_mutated_workbook() => + new FuzzHarness(fx, QuickIterations, 20261119).Files("xlsb", "xlsb", [Seed(XlsbTests.Twin()), Seed(Small())], Mutator, Read); + + private static void Read(string path, string media) + { + var book = XlsbReader.Read(path); + SheetCapabilities.FirstUnsupported(path, XlsbReader.Format); + SheetDocument.Build(book); + Directory.CreateDirectory(media); // emptied before each input + XlsxWriter.Write(book, Path.Combine(media, "out.xlsx")); + } + + private static XlsbBuilder Small() + { + var book = new XlsbBuilder(); + book.Sheet("A").Text(0, 0, "a").Rk(0, 1, XlsbBuilder.RkInteger(7)).Merge(1, 0, 2, 1).Width(0, 2, 10); + return book; + } + + private byte[] Seed(XlsbBuilder book) => File.ReadAllBytes(book.SaveXlsb(Path.Combine(fx.NewFolder(), "seed.xlsb"))); + + private static Func Mutator(byte[] seed, Random random) + { + var parts = new List<(string Name, byte[] Bytes)>(); + using (var zip = new ZipArchive(new MemoryStream(seed), ZipArchiveMode.Read)) + { + foreach (var entry in zip.Entries) + { + using var stream = entry.Open(); + using var copy = new MemoryStream(); + stream.CopyTo(copy); + parts.Add((entry.FullName, copy.ToArray())); + } + } + var binary = Enumerable.Range(0, parts.Count).Where(i => parts[i].Name.EndsWith(".bin", StringComparison.Ordinal)).ToList(); + var package = FuzzHarness.Packages(seed, random); + return () => + { + var choice = random.Next(100); + if (choice < 20) + return package(); + var mutated = parts.ToList(); + var index = binary[random.Next(binary.Count)]; + mutated[index] = (mutated[index].Name, choice < 35 + ? new BinaryFuzzer(mutated[index].Bytes, random).Next() + : MutateRecords(mutated[index].Bytes, random)); + using var output = new MemoryStream(); + using (var zip = new ZipArchive(output, ZipArchiveMode.Create, leaveOpen: true)) + { + foreach (var (name, bytes) in mutated) + { + using var stream = zip.CreateEntry(name, CompressionLevel.Fastest).Open(); + stream.Write(bytes); + } + } + return output.ToArray(); + }; + } + + private static readonly uint[] Interesting = [0, 1, 0x7F, 0xFF, 0x3FFF, 0x4000, 0xFFFF, 16383, 16384, 1048575, 1048576, 0x7FFFFFFF, 0x80000000, 0xFFFFFFFE, 0xFFFFFFFF]; + + /// The part's records with a few changed: fields, sizes, types, copies. + private static byte[] MutateRecords(byte[] part, Random random) + { + var records = Split(part); + if (records.Count == 0) + return part; + for (var changes = 1 + random.Next(3); changes > 0; changes--) + { + var i = random.Next(records.Count); + var (type, data) = records[i]; + switch (random.Next(7)) + { + case 0 or 1 when data.Length >= 4: + { + // A 32-bit (or 16-bit) field at a field boundary set to an edge value. + var copy = (byte[])data.Clone(); + var at = random.Next(data.Length - 3) & ~1; + var value = Interesting[random.Next(Interesting.Length)]; + if (random.Next(3) == 0) + BinaryPrimitives.WriteUInt16LittleEndian(copy.AsSpan(at), (ushort)value); + else + BinaryPrimitives.WriteUInt32LittleEndian(copy.AsSpan(at), value); + records[i] = (type, copy); + break; + } + case 2: + records[i] = (type, data[..random.Next(data.Length + 1)]); // cut short + break; + case 3: + records[i] = (records[random.Next(records.Count)].Type, data); // another record's type + break; + case 4: + for (var copies = random.Next(1, 200); copies > 0; copies--) + records.Insert(i, (type, data)); + break; + case 5: + records.RemoveAt(i); + if (records.Count == 0) + return []; + break; + default: + // A size that claims more than the record holds: the rest of the part becomes its data. + var join = new List(RecordWriter.Header(type, data.Length + random.Next(1, 64))); + join.AddRange(data); + foreach (var (t, d) in records.Skip(i + 1)) + join.AddRange(RecordWriter.Encode(t, d)); + return [.. records.Take(i).SelectMany(r => RecordWriter.Encode(r.Type, r.Data)), .. join]; + } + } + return [.. records.SelectMany(r => RecordWriter.Encode(r.Type, r.Data))]; + } + + /// The records of a BIFF12 part (as far as they are whole). + private static List<(int Type, byte[] Data)> Split(byte[] part) + { + var records = new List<(int, byte[])>(); + var at = 0; + while (at < part.Length) + { + var type = part[at] & 0x7F; + if ((part[at++] & 0x80) != 0 && at < part.Length) + type |= (part[at++] & 0x7F) << 7; + var size = 0; + for (var i = 0; i < 4 && at < part.Length; i++) + { + var b = part[at++]; + size |= (b & 0x7F) << (7 * i); + if ((b & 0x80) == 0) + break; + } + if (size > part.Length - at) + break; + records.Add((type, part[at..(at + size)])); + at += size; + } + return records; + } +} diff --git a/tests/Filee.Engines.Tests/XlsbTests.cs b/tests/Filee.Engines.Tests/XlsbTests.cs new file mode 100644 index 000000000..2878e46ac --- /dev/null +++ b/tests/Filee.Engines.Tests/XlsbTests.cs @@ -0,0 +1,421 @@ +// Excel binary workbooks (.xlsb, #1535) read by Filee's own XLSB reader. Every workbook is written here in code twice +// (XlsbBuilder): as BIFF12 records and as the same workbook in XLSX, and the XLSB reader must read what the XLSX +// reader reads, so every output the sheet model feeds (XLSX, ODS, CSV, PDF, HWPX, HTML) comes out the same. Hostile +// packages (records that lie about their size, too many records, ranges that expand without end) are refused quickly +// with the usual reasons. Set FILEE_RENDER_OUT to a folder to keep the printed pages of both twins. + +using System.Diagnostics; +using Filee.Core.Conversion; +using Filee.Core.Formats; +using Filee.Core.Presets; +using Filee.Engines.Documents; +using Filee.Engines.Office.Sheets; +using UglyToad.PdfPig; +using static Filee.Engines.Tests.XlsbBuilder; + +namespace Filee.Engines.Tests; + +public sealed class XlsbTests(EngineFixture fx) : IClassFixture +{ + private static readonly DateTime Now = new(2026, 11, 19, 9, 0, 0); + + /// A workbook with every kind of cell, format, row and column setting and page setup the reader takes. + internal static XlsbBuilder Twin() + { + var book = new XlsbBuilder(); + var sales = book.Sheet("지점별 매출"); + sales.Text(0, 0, "지점별 매출", Header).Merge(0, 0, 0, 4) + .Text(1, 0, "지점", Header).Text(1, 1, "매출", Header).Text(1, 2, "날짜", Header).Text(1, 3, "비율", Header).Text(1, 4, "확인", Header) + .Text(2, 0, "서울 강남").Number(2, 1, 1350000, Won).Rk(2, 2, RkInteger(46296), Date).Number(2, 3, 0.128, Percent).Bool(2, 4, true) + .Inline(3, 0, "부산 서면").Rk(3, 1, RkInteger(870000), Won).Rk(3, 2, RkInteger(46297), Date).Rk(3, 3, RkInteger(9, times100: true), Percent).Bool(3, 4, false) + .Text(4, 0, "합계", Accent).Formula(4, 1, 2220000.0, Won).Formula(4, 2, "계산됨").Formula(4, 3, (byte)0x07).Formula(4, 4, true) + .Rich(5, 0, "Bold and red text", (0, 1), (9, 2)).Rk(5, 1, RkDouble(2.5)).Rk(5, 2, RkInteger(-1234, times100: true), Decimals).Error(5, 3, 0x2A) + .Text(6, 0, "Pattern", Pattern).Text(6, 1, "Gradient", Gradient).Text(6, 2, "Diagonal", Diagonal).Text(6, 3, "Across", Across).Blank(6, 4, Pattern) + .Height(0, 30).Height(6, 40).Height(7, 15, hidden: true).Text(8, 0, "after the hidden row").Height(8, 21.75, custom: false) + .Width(0, 0, 18.7109375).Width(1, 3, 16.5).Width(5, 5, 9, hidden: true) + .RowBreak(5).ColumnBreak(3); + sales.FrozenRows = 2; + sales.PageSetup = (9, true, 80, false, 1, 0, true, 3); + sales.Margins = [0.5, 0.5, 1, 1, 0.4, 0.4]; + sales.GridLines = true; + sales.CenterHorizontally = true; + sales.HeaderFooter = ("&C&\"Calibri,Bold\"지점별 매출", "&R&P / &N", "&L첫 쪽", true); + book.Sheet("Hidden", state: 1).Text(0, 0, "secret"); + var notes = book.Sheet("Notes"); + notes.Text(0, 0, "오른쪽에서 왼쪽").Number(1, 0, 3.25).Text(19, 9, "End"); + notes.RightToLeft = true; + notes.PageSetup = (1, false, 100, true, 1, 2, false, null); + book.Names.Add((0, "Print_Area", [(0, 0, 8, 4)])); + book.Names.Add((0, "Print_Titles", [(1, 0, 1, 16383)])); + book.Names.Add((2, "Print_Area", [(0, 0, 9, 4), (15, 5, 19, 9)])); + return book; + } + + private (string Xlsb, string Xlsx) Save(XlsbBuilder book, string name = "book") + { + var folder = fx.NewFolder(); + return (book.SaveXlsb(Path.Combine(folder, name + ".xlsb")), book.SaveXlsx(Path.Combine(folder, name + ".xlsx"))); + } + + // ───────────────────────── The same workbook as its XLSX twin ───────────────────────── + + [Fact] + public void An_xlsb_reads_as_its_xlsx_twin() + { + var (xlsb, xlsx) = Save(Twin()); + + AssertSame(XlsxReader.Read(xlsx), XlsbReader.Read(xlsb)); + } + + [Fact] + public void Cells_read_with_their_values_and_formats() + { + var book = XlsbReader.Read(Save(Twin()).Xlsb); + + Assert.Equal(["지점별 매출", "Notes"], book.Sheets.Select(s => s.Name)); // the hidden sheet is left out + var sheet = book.Sheets[0]; + Assert.Equal("1,350,000원", sheet.Get(2, 1)!.Text); + Assert.Equal(1350000, sheet.Get(2, 1)!.Value); + Assert.Equal("#,##0\"원\"", sheet.Get(2, 1)!.NumberFormat); + Assert.Equal(46296, sheet.Get(2, 2)!.Value); // an RK integer + Assert.Equal("12.8%", sheet.Get(2, 3)!.Text); + Assert.Equal(0.09, sheet.Get(3, 3)!.Value!.Value, 10); // an RK integer stored × 100 + Assert.Equal("부산 서면", sheet.Get(3, 0)!.Text); // a string in the cell + Assert.Equal(2.5, sheet.Get(5, 1)!.Value); // an RK double + Assert.Equal("-12.34", sheet.Get(5, 2)!.Text); + Assert.True(sheet.Get(2, 4)!.IsBoolean); + Assert.Equal("TRUE", sheet.Get(2, 4)!.Text); + + // Formulas: the value Excel last calculated. + Assert.Equal("2,220,000원", sheet.Get(4, 1)!.Text); + Assert.Equal("계산됨", sheet.Get(4, 2)!.Text); + Assert.True(sheet.Get(4, 3)!.IsError); + Assert.Equal("#DIV/0!", sheet.Get(4, 3)!.Text); + Assert.Equal("TRUE", sheet.Get(4, 4)!.Text); + Assert.Equal("#N/A", sheet.Get(5, 3)!.Text); + + // Styles: fonts, fills, borders and alignment. + var header = sheet.Get(1, 0)!.Style; + Assert.True(header.Bold); + Assert.Equal("#FFFFFF", header.Color); + Assert.Equal("#1F4E79", header.Fill); + Assert.Equal(CellAlign.Center, header.Align); + Assert.Equal(CellVerticalAlign.Center, header.VerticalAlign); + Assert.Equal("thin", header.Bottom.Style); + var accent = sheet.Get(4, 0)!.Style; + Assert.True(accent is { Italic: true, Underline: true, Strike: true, Wrap: true, Align: CellAlign.Right, VerticalAlign: CellVerticalAlign.Top }); + Assert.Equal(14, accent.FontSize); + Assert.Equal("#C00000", accent.Color); + Assert.Equal(("darkGray", "#FF0000", "#FFFFFF"), (sheet.Get(6, 0)!.Style.FillPattern, sheet.Get(6, 0)!.Style.Fill, sheet.Get(6, 0)!.Style.PatternBackground)); + Assert.Equal(2, sheet.Get(6, 0)!.Style.Indent); + Assert.Equal([(0.0, "#FFFFFF"), (1.0, "#0000FF")], sheet.Get(6, 1)!.Style.Gradient!.Stops); + Assert.Equal(45, sheet.Get(6, 1)!.Style.Rotation); + Assert.True(sheet.Get(6, 1)!.Style.Shrink); + Assert.True(sheet.Get(6, 2)!.Style.DiagonalUp.IsVisible && sheet.Get(6, 2)!.Style.DiagonalDown.IsVisible); + Assert.Equal(CellStyle.Stacked, sheet.Get(6, 2)!.Style.Rotation); + var across = sheet.Get(6, 3)!.Style; + Assert.Equal(CellSpecialAlign.CenterAcross, across.SpecialAlign); + Assert.True(across.DoubleUnderline && across.VerticalJustify); + Assert.Equal("맑은 고딕", across.FontName); + Assert.Equal(("double", "dashed", "#FF0000"), (across.Top.Style, across.Bottom.Style, across.Bottom.Color)); + Assert.NotNull(sheet.Get(6, 4)); // a blank cell with a fill + + // Rich text: the runs in their fonts. + var rich = sheet.Get(5, 0)!; + Assert.Equal(["Bold and ", "red text"], rich.Runs!.Select(r => r.Text)); + Assert.True(rich.Runs![0].Bold); + Assert.Equal("#C00000", rich.Runs[1].Color); + + Assert.Equal("Calibri", book.DefaultFontName); + Assert.Equal(11, book.DefaultFontSize); + } + + [Fact] + public void Rows_columns_and_the_page_setup_are_read() + { + var book = XlsbReader.Read(Save(Twin()).Xlsb); + var sheet = book.Sheets[0]; + + Assert.Equal(30, sheet.RowHeights[0]); + Assert.Equal(21.75, sheet.AutoRowHeights[8]); + Assert.Contains(7, sheet.HiddenRows); + Assert.Contains(5, sheet.HiddenColumns); + Assert.Equal(16.5, sheet.ColumnWidth(2)); + Assert.Equal(new CellRange(0, 0, 0, 4), Assert.Single(sheet.Merges)); + Assert.Equal(2, sheet.FrozenRows); + Assert.Equal([5], sheet.RowBreaks); + Assert.Equal([3], sheet.ColumnBreaks); + Assert.Equal(15, sheet.DefaultRowHeight); + Assert.Equal(8, sheet.BaseColumnWidth); + Assert.Equal((1, 1), sheet.TitleRows); + Assert.Equal(new CellRange(0, 0, 8, 4), Assert.Single(sheet.PrintArea)); + + var setup = sheet.PageSetup; + Assert.True(setup is { Landscape: true, Scale: 80, OverThenDown: true, FirstPageNumber: 3, PrintGridLines: true, CenterHorizontally: true }); + Assert.Equal(Paper.A4, (setup.PaperWidth, setup.PaperHeight)); + Assert.Equal(36, setup.LeftMargin); + Assert.Equal(72, setup.TopMargin); + Assert.Equal("&R&P / &N", setup.HeaderFooter.OddFooter); + Assert.True(setup.HeaderFooter.DifferentFirst); + Assert.Equal("&L첫 쪽", setup.HeaderFooter.FirstHeader); + + var notes = book.Sheets[1]; + Assert.True(notes.RightToLeft); + Assert.True(notes.PageSetup is { FitToPage: true, FitWidth: 1, FitHeight: 2 }); + Assert.Equal([new CellRange(0, 0, 9, 4), new CellRange(15, 5, 19, 9)], notes.PrintArea); + } + + [Fact] + public void Dates_of_a_1904_workbook_mean_the_same_day() + { + var book = new XlsbBuilder { Date1904 = true }; + book.Sheet("Dates").Rk(0, 0, RkInteger(44834), Date); + var (xlsb, xlsx) = Save(book); + + var cell = XlsbReader.Read(xlsb).Sheets[0].Get(0, 0)!; + Assert.Equal(44834 + 1462, cell.Value); + Assert.Equal(XlsxReader.Read(xlsx).Sheets[0].Get(0, 0)!.Text, cell.Text); + } + + [Fact] + public void A_picture_on_the_sheet_is_read_from_its_drawing() + { + var book = new XlsbBuilder(); + var sheet = book.Sheet("Picture").Text(19, 5, "End"); + sheet.DrawingId = "rIdD"; + sheet.Relationships.Add(("rIdD", "drawing", "../drawings/drawing1.xml")); + book.Parts["xl/drawings/drawing1.xml"] = + "" + + "" + + ""; + book.Parts["xl/drawings/_rels/drawing1.xml.rels"] = OpcWriter.Relationships([("rIdP", "image", "../media/image1.png")]); + book.Media["xl/media/image1.png"] = OdpBuilder.Png(4, 4, SkiaSharp.SKColors.Teal); + var (xlsb, xlsx) = Save(book); + + var picture = Assert.IsType(Assert.Single(XlsbReader.Read(xlsb).Sheets[0].Drawings)); + Assert.Equal((80.0, 40.0), (picture.Width, picture.Height)); + Assert.Null(SheetCapabilities.FirstUnsupported(xlsb, "xlsb")); + Assert.Single(XlsxReader.Read(xlsx).Sheets[0].Drawings); + } + + [Fact] + public void A_chart_is_named_before_printing() + { + var book = new XlsbBuilder(); + var sheet = book.Sheet("Chart").Text(0, 0, "x"); + sheet.DrawingId = "rIdD"; + sheet.Relationships.Add(("rIdD", "drawing", "../drawings/drawing1.xml")); + book.Parts["xl/drawings/drawing1.xml"] = + "" + + "" + + "" + + ""; + var (xlsb, _) = Save(book); + + Assert.Equal("charts", SheetCapabilities.FirstUnsupported(xlsb, "xlsb")); + } + + // ───────────────────────── Routes and outputs ───────────────────────── + + [Fact] + public void Xlsb_is_a_spreadsheet_with_its_routes() + { + Assert.Equal("xlsb", FormatRegistry.Detect("Budget.xlsb")?.Id); + Assert.False(FormatRegistry.Get("xlsb").Writable); + var planner = fx.Catalog.CreatePlanner(); + Assert.Equal("sheet-print", Assert.Single(planner.Plan("xlsb", "pdf")!.Steps).Converter.Id); + foreach (var to in new[] { "xlsx", "ods", "csv", "tsv" }) + Assert.Equal("spreadsheet", Assert.Single(planner.Plan("xlsb", to)!.Steps).Converter.Id); + Assert.Equal(["hwpx-writer"], planner.Plan("xlsb", "hwpx")!.Steps.Select(s => s.Converter.Id)); + Assert.NotNull(planner.Plan("xlsb", "html")); + } + + [Fact] + public async Task An_xlsb_converts_to_xlsx_csv_and_pdf_in_a_job() + { + var (xlsb, xlsx) = Save(Twin(), "sales"); + + var toXlsx = await fx.ConvertAsync([xlsb], new Preset { TargetFormat = "xlsx" }); + Assert.Equal(JobState.Completed, toXlsx.State); + var written = XlsxReader.Read(toXlsx.Outputs.Single()); + Assert.Equal("1,350,000원", written.Sheets[0].Get(2, 1)!.Text); + Assert.Equal(1350000, written.Sheets[0].Get(2, 1)!.Value); + + var toCsv = await fx.ConvertAsync([xlsb], new Preset { TargetFormat = "csv" }); + Assert.Equal(JobState.Completed, toCsv.State); + Assert.Contains("서울 강남", File.ReadAllText(toCsv.Outputs.First())); + + var toPdf = await fx.ConvertAsync([xlsb], new Preset { TargetFormat = "pdf" }); + Assert.Equal(JobState.Completed, toPdf.State); + using var pdf = PdfDocument.Open(toPdf.Outputs.Single()); + var text = string.Join(" ", pdf.GetPages().SelectMany(p => p.GetWords()).Select(w => w.Text)); + Assert.Contains("1,350,000원", text); + Assert.Contains("계산됨", text); + } + + [Fact] + public void The_printed_pages_are_those_of_the_xlsx_twin() + { + var (xlsb, xlsx) = Save(Twin(), "sales"); + + var fromXlsb = Pages(XlsbReader.Read(xlsb), "sales-xlsb"); + var fromXlsx = Pages(XlsxReader.Read(xlsx), "sales-xlsx"); + Assert.Equal(fromXlsx.Count, fromXlsb.Count); + Assert.Equal(fromXlsx, fromXlsb); + } + + /// The words of each printed page (and, with FILEE_RENDER_OUT, the PDF and a picture of each page). + private static List Pages(Workbook book, string name) + { + var source = new SheetPrintSource(name + ".xlsb", "C:\\Reports\\"); + var bytes = SheetPrinter.ToPdf(book, source, now: Now); + if (Environment.GetEnvironmentVariable("FILEE_RENDER_OUT") is { Length: > 0 } folder) + { + Directory.CreateDirectory(folder); + File.WriteAllBytes(Path.Combine(folder, name + ".pdf"), bytes); + using var printed = SheetPrinter.Print(book, source, DocumentLimits.Default, Now, null, default); + for (var i = 0; i < printed.Pages.Count; i++) + { + using var bitmap = printed.Pages[i].Render(100); + using var png = bitmap.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100); + File.WriteAllBytes(Path.Combine(folder, $"{name}-{i + 1}.png"), png.ToArray()); + } + } + using var pdf = PdfDocument.Open(bytes); + return [.. pdf.GetPages().Select(p => string.Join(" ", p.GetWords().Select(w => w.Text)))]; + } + + // ───────────────────────── Files that are something else ───────────────────────── + + [Fact] + public void An_xlsx_or_xls_with_an_xlsb_name_is_read_as_what_it_is() + { + var folder = fx.NewFolder(); + var xlsx = Twin().SaveXlsx(Path.Combine(folder, "web.xlsb")); + Assert.Equal("1,350,000원", XlsbReader.Read(xlsx).Sheets[0].Get(2, 1)!.Text); + + var xls = new XlsBuilder(); + xls.Sheet("Old").Text(0, 0, "BIFF8"); + var path = xls.Save(Path.Combine(folder, "old.xlsb")); + Assert.Equal("BIFF8", XlsbReader.Read(path).Sheets[0].Get(0, 0)!.Text); + } + + [Fact] + public void An_encrypted_workbook_asks_for_the_password_to_be_removed() + { + var path = Path.Combine(fx.NewFolder(), "locked.xlsb"); + CompoundFile.Write(path, "EncryptedPackage", new byte[600]); + + var error = Assert.Throws(() => XlsbReader.Read(path)); + Assert.Contains("password", error.Message, StringComparison.Ordinal); + } + + // ───────────────────────── Hostile input ───────────────────────── + + [Fact] + public void Hostile_records_are_refused_quickly() + { + var watch = Stopwatch.StartNew(); + + // A record that claims 200 MB which aren't there: damage, found without reading or allocating them. + Assert.Throws(() => XlsbReader.Read(Hostile(sheet => sheet.ExtraRecords.Add(RecordWriter.Header(5, 200 * 1024 * 1024))))); + + // A cell record of 2 MB: more than a record the reader takes apart may hold. + var refused = Assert.Throws(() => XlsbReader.Read(Hostile(sheet => + sheet.ExtraRecords.Add(RecordWriter.Encode(6, [.. U32(0), .. U32(0), .. U32(1_000_000), .. new byte[2_000_000]]))))); + Assert.Equal(DocumentLimits.ErrorKey, refused.ErrorKey); + + // A string whose length is more than its record holds. + Assert.Throws(() => XlsbReader.Read(Hostile(sheet => + sheet.ExtraRecords.Add(RecordWriter.Encode(6, [.. U32(0), .. U32(0), .. U32(0x7FFFFFFF), 0x41, 0]))))); + + // Column ranges over the whole sheet, again and again. + var columns = Hostile(sheet => + { + for (var i = 0; i < 300; i++) + sheet.ExtraRecords.Add(RecordWriter.Encode(60, [.. U32(0), .. U32(16383), .. U32(2560), .. U32(0), .. U16(0)])); + }); + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => XlsbReader.Read(columns)).ErrorKey); + + // More records than a workbook may hold (a lowered limit stands for 5,000,000). + var many = Hostile(sheet => + { + for (var i = 0; i < 2000; i++) + sheet.ExtraRecords.Add(RecordWriter.Encode(1, [.. U32((uint)(i % 100)), .. U32(0)])); + }); + Assert.Equal(DocumentLimits.ErrorKey, + Assert.Throws(() => XlsbReader.Read(many, DocumentLimits.Default with { MaxXmlElements = 1000 })).ErrorKey); + + // Cells and ranges beyond the sheet, styles and strings that don't exist, names with foreign tokens: read, left out. + var odd = Hostile(sheet => + { + sheet.ExtraRecords.Add(RecordWriter.Encode(5, [.. U32(2), .. U32(0), .. F64(double.NaN)])); + sheet.ExtraRecords.Add(RecordWriter.Encode(7, [.. U32(16384), .. U32(0), .. U32(0)])); + sheet.ExtraRecords.Add(RecordWriter.Encode(7, [.. U32(1), .. U32(0xFFFFFF), .. U32(999999)])); + sheet.ExtraRecords.Add(RecordWriter.Encode(0, [.. U32(0xFFFFFFFF), .. U32(0), .. U16(0), 0, 0, 0, .. U32(0)])); + }, book => + { + book.ExtraWorkbookRecords.Add(RecordWriter.Encode(39, [.. U32(0x20), 0, .. U32(0), .. WideString("_xlnm.Print_Area"), .. U32(3), 0x17, 0xFF, 0xFF, .. U32(0)])); + book.ExtraWorkbookRecords.Add(RecordWriter.Encode(39, [.. U32(0x20), 0, .. U32(0x7FFFFFFF), .. WideString("Print_Titles"), .. U32(99)])); + }); + var read = XlsbReader.Read(odd); + Assert.Equal("#NUM!", read.Sheets[0].Get(0, 2)!.Text); + + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"Took {watch.Elapsed}"); + } + + /// A one-sheet XLSB whose sheet data ends with what adds. + private string Hostile(Action sheet, Action? book = null) + { + var builder = new XlsbBuilder(); + sheet(builder.Sheet("Data").Text(0, 0, "x")); + book?.Invoke(builder); + return builder.SaveXlsb(Path.Combine(fx.NewFolder(), "hostile.xlsb")); + } + + // ───────────────────────── Comparison ───────────────────────── + + internal static void AssertSame(Workbook expected, Workbook actual) + { + Assert.Equal(expected.DefaultFontName, actual.DefaultFontName); + Assert.Equal(expected.DefaultFontSize, actual.DefaultFontSize); + Assert.Equal(expected.Sheets.Select(s => s.Name), actual.Sheets.Select(s => s.Name)); + for (var i = 0; i < expected.Sheets.Count; i++) + { + var (x, y) = (expected.Sheets[i], actual.Sheets[i]); + Assert.Equal(x.Rows.SelectMany(r => r.Value.Keys.Select(c => (r.Key, c))), y.Rows.SelectMany(r => r.Value.Keys.Select(c => (r.Key, c)))); + foreach (var (row, cells) in x.Rows) + { + foreach (var (column, cell) in cells) + { + var other = y.Get(row, column)!; + var at = $"{x.Name}!{XlsbBuilder.Column(column)}{row + 1}"; + Assert.True((cell.Text, cell.Value, cell.NumberFormat, cell.IsNumber, cell.IsBoolean, cell.IsError) + == (other.Text, other.Value, other.NumberFormat, other.IsNumber, other.IsBoolean, other.IsError), $"{at}: {cell} / {other}"); + Assert.True(cell.Style with { Gradient = null } == other.Style with { Gradient = null }, $"{at}: {cell.Style} / {other.Style}"); + Assert.Equal(cell.Style.Gradient?.Degree, other.Style.Gradient?.Degree); + Assert.Equal(cell.Style.Gradient?.Stops ?? [], other.Style.Gradient?.Stops ?? []); + Assert.Equal(cell.Runs ?? [], other.Runs ?? []); + } + } + Assert.Equal(x.ColumnWidths.OrderBy(p => p.Key), y.ColumnWidths.OrderBy(p => p.Key)); + Assert.Equal(x.RowHeights.OrderBy(p => p.Key), y.RowHeights.OrderBy(p => p.Key)); + Assert.Equal(x.AutoRowHeights.OrderBy(p => p.Key), y.AutoRowHeights.OrderBy(p => p.Key)); + Assert.Equal(x.HiddenRows.Order(), y.HiddenRows.Order()); + Assert.Equal(x.HiddenColumns.Order(), y.HiddenColumns.Order()); + Assert.Equal(x.Merges, y.Merges); + Assert.Equal(x.RowBreaks, y.RowBreaks); + Assert.Equal(x.ColumnBreaks, y.ColumnBreaks); + Assert.Equal(x.PrintArea, y.PrintArea); + Assert.Equal((x.FrozenRows, x.TitleRows, x.TitleColumns, x.RightToLeft), (y.FrozenRows, y.TitleRows, y.TitleColumns, y.RightToLeft)); + Assert.Equal((x.DefaultColumnWidth, x.HasDefaultColumnWidth, x.BaseColumnWidth, x.DefaultRowHeight), + (y.DefaultColumnWidth, y.HasDefaultColumnWidth, y.BaseColumnWidth, y.DefaultRowHeight)); + var empty = new Dictionary(); + Assert.Equal(x.PageSetup with { HeaderFooter = x.PageSetup.HeaderFooter with { Pictures = empty } }, + y.PageSetup with { HeaderFooter = y.PageSetup.HeaderFooter with { Pictures = empty } }); + Assert.Equal(x.Drawings.Count, y.Drawings.Count); + } + } +} diff --git a/tests/Filee.Engines.Tests/XpsBuilder.cs b/tests/Filee.Engines.Tests/XpsBuilder.cs new file mode 100644 index 000000000..8dba794e3 --- /dev/null +++ b/tests/Filee.Engines.Tests/XpsBuilder.cs @@ -0,0 +1,154 @@ +// Writes XPS and OpenXPS packages in code for the XPS reader's tests: the package relationships, a fixed document +// sequence with one fixed document, its pages (FixedPage markup given by the test), fonts (plain or obfuscated as +// .odttf), pictures and other parts. Pages can be written as interleaved pieces. + +using System.Globalization; +using System.IO.Compression; +using System.Text; + +namespace Filee.Engines.Tests; + +internal sealed class XpsBuilder +{ + public const string XpsNamespace = "http://schemas.microsoft.com/xps/2005/06"; + public const string OpenXpsNamespace = "http://schemas.openxps.org/oxps/v1.0"; + + private readonly List<(string Body, double Width, double Height)> _pages = []; + private readonly Dictionary _parts = new(StringComparer.Ordinal); + private int _resources; + + /// Writes the OpenXPS (ECMA-388) namespaces instead of XPS 1.0's. + public bool OpenXps { get; init; } + + /// Writes each page as this many interleaved pieces (0 = whole). + public int Pieces { get; init; } + + public string? Title { get; init; } + + /// LinkTarget names of the first page. + public List LinkTargets { get; } = []; + + public string Namespace => OpenXps ? OpenXpsNamespace : XpsNamespace; + + private string RelationshipBase => OpenXps ? "http://schemas.openxps.org/oxps/v1.0" : "http://schemas.microsoft.com/xps/2005/06"; + + /// Adds a page whose FixedPage holds (in the page's default namespace). + public XpsBuilder Page(string body, double width = 816, double height = 1056) + { + _pages.Add((body, width, height)); + return this; + } + + /// Adds a part; returns its absolute name ("/Resources/…"). + public string Part(string name, byte[] data) + { + _parts[name.TrimStart('/')] = data; + return "/" + name.TrimStart('/'); + } + + /// Adds a picture; returns its part name. + public string Image(byte[] data, string extension) => Part($"Resources/Images/image{++_resources}.{extension}", data); + + /// Adds a font, obfuscated as an .odttf part under a new GUID when ; returns its part name. + public string Font(byte[] font, bool obfuscate = true) + { + if (!obfuscate) + return Part($"Resources/Fonts/font{++_resources}.ttf", font); + var guid = Guid.NewGuid(); + var hex = guid.ToString("N", CultureInfo.InvariantCulture); + var key = new byte[16]; + for (var i = 0; i < 16; i++) + key[i] = byte.Parse(hex.AsSpan(i * 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture); + var bytes = (byte[])font.Clone(); + for (var i = 0; i < 32; i++) + bytes[i] ^= key[15 - i % 16]; + return Part($"Resources/Fonts/{guid.ToString("D", CultureInfo.InvariantCulture).ToUpperInvariant()}.odttf", bytes); + } + + /// Adds a remote resource dictionary holding ; returns its part name. + public string Dictionary(string entries) => + Part($"Resources/dictionary{++_resources}.dict", Encoding.UTF8.GetBytes( + $"{entries}")); + + /// The namespace of x:Key in resource dictionaries. + public string KeyNamespace => OpenXps ? "http://schemas.openxps.org/oxps/v1.0/resourcedictionary-key" : "http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key"; + + public byte[] ToBytes() + { + using var buffer = new MemoryStream(); + using (var zip = new ZipArchive(buffer, ZipArchiveMode.Create, leaveOpen: true)) + { + Write(zip, "[Content_Types].xml", + "" + + "" + + "" + + "" + + "" + + "" + + ""); + var core = Title is null ? "" : + ""; + Write(zip, "_rels/.rels", + "" + + $"{core}"); + if (Title is not null) + { + Write(zip, "docProps/core.xml", + "" + + $"{Title}"); + } + Write(zip, "FixedDocumentSequence.fdseq", + $""); + var document = new StringBuilder($""); + for (var i = 0; i < _pages.Count; i++) + { + document.Append(CultureInfo.InvariantCulture, $""); + if (i == 0 && LinkTargets.Count > 0) + document.Append("").Append(string.Concat(LinkTargets.Select(t => $""))).Append(""); + document.Append(""); + } + Write(zip, "Documents/1/FixedDocument.fdoc", document.Append("").ToString()); + for (var i = 0; i < _pages.Count; i++) + { + var (body, width, height) = _pages[i]; + var page = string.Create(CultureInfo.InvariantCulture, + $"{body}"); + var name = $"Documents/1/Pages/{i + 1}.fpage"; + if (Pieces <= 1) + { + Write(zip, name, page); + continue; + } + var bytes = Encoding.UTF8.GetBytes(page); + var size = (bytes.Length + Pieces - 1) / Pieces; + for (var p = 0; p < Pieces; p++) + { + var piece = bytes.AsSpan(Math.Min(p * size, bytes.Length), Math.Max(0, Math.Min(size, bytes.Length - p * size))); + var entry = zip.CreateEntry($"{name}/[{p}]{(p == Pieces - 1 ? ".last" : "")}.piece"); + using var stream = entry.Open(); + stream.Write(piece); + } + } + foreach (var (name, data) in _parts) + { + var entry = zip.CreateEntry(name); + using var stream = entry.Open(); + stream.Write(data); + } + } + return buffer.ToArray(); + } + + public string Save(string path) + { + File.WriteAllBytes(path, ToBytes()); + return path; + } + + private static void Write(ZipArchive zip, string name, string text) + { + var entry = zip.CreateEntry(name); + using var stream = entry.Open(); + stream.Write(Encoding.UTF8.GetBytes(text)); + } +} diff --git a/tests/Filee.Engines.Tests/XpsFuzzTests.cs b/tests/Filee.Engines.Tests/XpsFuzzTests.cs new file mode 100644 index 000000000..d16965596 --- /dev/null +++ b/tests/Filee.Engines.Tests/XpsFuzzTests.cs @@ -0,0 +1,81 @@ +// Mutation fuzzing of the XPS reader (Xps/) and the drawing of what it read: the seed is a document written in code +// (XpsBuilder) with what costs the reader the most (an obfuscated embedded font with Indices and cluster maps, PNG, +// JPEG and TIFF pictures, gradients, tiled image and visual brushes, resources inline and in a remote dictionary, +// clips, opacity masks, path geometry with every segment kind, links and link targets). DocumentFuzzer changes the +// package, and every input must be read or refused with InvalidDataException / ConversionRefusedException, in time +// and on a 1 MB stack. 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.Xps; +using ImageMagick; + +namespace Filee.Engines.Tests; + +[Trait("Category", "Fuzz")] +public class XpsFuzzTests(EngineFixture fx) : IClassFixture +{ + /// Inputs per seed in a normal test run. + private const int QuickIterations = 50; + + [Fact] + public void XpsReader_reads_or_refuses_every_mutated_document() => + new FuzzHarness(fx, QuickIterations, 20261119).Files("xps", "xps", [Seed(openXps: false), Seed(openXps: true)], FuzzHarness.Packages, (path, _) => + { + using var document = XpsDocument.Open(path, CancellationToken.None); + var pages = new List(); + for (var i = 0; i < document.Count; i++) + { + pages.Add(document.Page(i, CancellationToken.None)); + using var bitmap = pages[^1].Render(12); + } + // The PDF writer subsets the embedded fonts. + using var pdf = new MemoryStream(); + Rendering.PdfPageWriter.Write(pdf, pages); + }); + + /// A document with every kind of thing the reader reads. + internal static byte[] Seed(bool openXps) + { + var builder = new XpsBuilder { OpenXps = openXps, Title = "Fuzz", LinkTargets = { "Target" } }; + var font = builder.Font(File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Fonts", "FileeTest-TrueType.ttf"))); + var png = builder.Image(DocumentFuzzTests.TinyPng(4, 3), "png"); + string jpeg, tiff; + using (var image = new MagickImage(MagickColors.Orange, 4, 4)) + { + jpeg = builder.Image(image.ToByteArray(MagickFormat.Jpeg), "jpg"); + tiff = builder.Image(image.ToByteArray(MagickFormat.Tiff), "tif"); + } + var remote = builder.Dictionary(""); + var stops = ""; + builder.Page( + "" + + $"{stops}" + + "" + + "" + + $"" + + $"" + + "" + + "" + + "" + + $"{stops}" + + "" + + $"" + + $"" + + $"" + + "" + + "" + + "" + + "" + + "" + + $"" + + $"" + + "" + + $"{stops}" + + "" + + "" + + "", + 400, 300); + builder.Page("", 200, 100); + return builder.ToBytes(); + } +} diff --git a/tests/Filee.Engines.Tests/XpsTests.cs b/tests/Filee.Engines.Tests/XpsTests.cs new file mode 100644 index 000000000..401920790 --- /dev/null +++ b/tests/Filee.Engines.Tests/XpsTests.cs @@ -0,0 +1,460 @@ +// Filee's own XPS / OpenXPS reader (Xps/) and its route through the drawing converter: packages written in code +// (XpsBuilder) in both namespaces, drawn at 96 dpi (one pixel per XPS unit) and checked by their pixels, glyph runs +// with an obfuscated embedded font checked by the PDF's text, and hostile packages refused quickly. + +using System.Diagnostics; +using Filee.Core.Conversion; +using Filee.Core.Formats; +using Filee.Core.Presets; +using Filee.Engines.Documents; +using Filee.Engines.Drawings; +using Filee.Engines.Rendering; +using Filee.Engines.Xps; +using ImageMagick; +using SkiaSharp; +using UglyToad.PdfPig; + +namespace Filee.Engines.Tests; + +public sealed class XpsTests(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 static byte[] TestFont() => File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Fonts", "FileeTest-TrueType.ttf")); + + private string Save(XpsBuilder builder, string name = "test.xps") => builder.Save(Path.Combine(_folder, name)); + + /// Page of the package as a picture at 96 dpi: one pixel per XPS unit. + private SKBitmap Render(XpsBuilder builder, int index = 0) + { + using var document = XpsDocument.Open(Save(builder), Token); + return document.Page(index, Token).Render(96); + } + + private static void AssertColor(SKBitmap bitmap, int x, int y, string expected, int tolerance = 24) + { + var want = SKColor.Parse(expected); + var got = bitmap.GetPixel(x, y); + Assert.True(Math.Abs(got.Red - want.Red) <= tolerance && Math.Abs(got.Green - want.Green) <= tolerance && Math.Abs(got.Blue - want.Blue) <= tolerance, + $"({x}, {y}) is {got}, expected {want}"); + } + + private static string Box(double x, double y, double width, double height) => + FormattableString.Invariant($"M {x},{y} L {x + width},{y} {x + width},{y + height} {x},{y + height} Z"); + + private static byte[] Png(SKColor color, int width = 4, int height = 4, double dpi = 0) + { + using var image = new MagickImage(new MagickColor(color.Red, color.Green, color.Blue), (uint)width, (uint)height); + if (dpi > 0) + image.Density = new Density(dpi, dpi, DensityUnit.PixelsPerInch); + return image.ToByteArray(MagickFormat.Png); + } + + private static byte[] Picture(SKColor color, MagickFormat format) + { + using var image = new MagickImage(new MagickColor(color.Red, color.Green, color.Blue), 4, 4); + return image.ToByteArray(format); + } + + // ───────────────────────── Package ───────────────────────── + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Both_namespaces_give_their_pages_sizes_and_title(bool openXps) + { + var builder = new XpsBuilder { OpenXps = openXps, Title = "Quarterly plan" } + .Page($"") + .Page("", 400, 300); + using var document = XpsDocument.Open(Save(builder), Token); + Assert.Equal(2, document.Count); + Assert.Equal("Quarterly plan", document.Title); + Assert.Equal((612f, 792f), (document.Page(0, Token).Width, document.Page(0, Token).Height)); + Assert.Equal((300f, 225f), (document.Page(1, Token).Width, document.Page(1, Token).Height)); + } + + [Fact] + public void Interleaved_pages_are_joined_and_a_page_missing_a_piece_is_damage() + { + using (var bitmap = Render(new XpsBuilder { Pieces = 3 }.Page($"", 100, 100))) + AssertColor(bitmap, 30, 30, "#00FF00"); + + var path = Save(new XpsBuilder { Pieces = 3 }.Page(""), "pieces.xps"); + using (var zip = System.IO.Compression.ZipFile.Open(path, System.IO.Compression.ZipArchiveMode.Update)) + zip.GetEntry("Documents/1/Pages/1.fpage/[1].piece")!.Delete(); + using var document = XpsDocument.Open(path, Token); + Assert.Throws(() => document.Page(0, Token)); + } + + [Fact] + public void Obfuscated_fonts_are_restored_with_the_guid_of_their_name() + { + var font = new byte[40]; + Assert.True(XpsPackage.Deobfuscate(font, "00112233-4455-6677-8899-AABBCCDDEEFF")); + // Byte i is XORed with byte 15 - i % 16 of the GUID's text. + Assert.Equal([0xFF, 0xEE, 0xDD], font[..3]); + Assert.Equal(0x00, font[15]); + Assert.Equal(0xFF, font[16]); + Assert.Equal(0x00, font[32]); + Assert.False(XpsPackage.Deobfuscate(new byte[40], "not-a-guid")); + } + + [Theory] + [InlineData("Documents/1/Pages/1.fpage", "../../../Resources/a%20b.png", "Resources/a b.png")] + [InlineData("Documents/1/Pages/1.fpage", "/Resources/Fonts/x.odttf#1", "Resources/Fonts/x.odttf")] + [InlineData("Documents/1/Pages/1.fpage", "../../../../escape.png", null)] + [InlineData("Documents/1/Pages/1.fpage", "http://example.com/a.png", null)] + public void References_resolve_inside_the_package(string from, string reference, string? expected) => + Assert.Equal(expected, XpsPackage.Resolve(from, reference)); + + [Fact] + public void A_zip_without_a_page_list_is_not_an_xps_document() + { + var path = Path.Combine(_folder, "plain.xps"); + using (var zip = System.IO.Compression.ZipFile.Open(path, System.IO.Compression.ZipArchiveMode.Create)) + zip.CreateEntry("readme.txt"); + Assert.Throws(() => XpsDocument.Open(path, Token)); + File.WriteAllText(path, "not a zip"); + Assert.Throws(() => XpsDocument.Open(path, Token)); + } + + // ───────────────────────── Paths ───────────────────────── + + [Fact] + public void Paths_are_filled_and_stroked_where_their_transforms_put_them() + { + var builder = new XpsBuilder().Page( + $"" + + $"", + 300, 300); + using var bitmap = Render(builder); + AssertColor(bitmap, 125, 125, "#FF0000"); + AssertColor(bitmap, 100, 125, "#0000FF"); + AssertColor(bitmap, 30, 30, "#00FF00"); + AssertColor(bitmap, 42, 42, "#FFFFFF"); + AssertColor(bitmap, 200, 200, "#FFFFFF"); + } + + [Fact] + public void The_abbreviated_syntax_reads_every_command() + { + var budget = new XpsBudget(XpsLimits.Default); + using (var geometry = XpsGeometry.Parse("F1 M10,10 h20 v20 H10 Z m 40,0 c 0,10 10,10 10,0 s 10,-10 10,0 q 5,5 10,0 l 5,5 L 100 100", budget)) + { + Assert.Equal(SKPathFillType.Winding, geometry.Fill.FillType); + // Control points count: the smooth curve's reflected one lies at y = 0. + Assert.Equal(new SKRect(10, 0, 100, 100), geometry.Fill.Bounds); + } + using (var geometry = XpsGeometry.Parse("M 0,0 L 10,0 garbage 20,20", budget)) + Assert.Equal(new SKRect(0, 0, 10, 0), geometry.Fill.Bounds); // ends where the data breaks + Assert.Equal(SKPathFillType.EvenOdd, XpsGeometry.Parse("M 0,0 L 1,1", budget).Fill.FillType); + + // A circle of two arcs: filled inside, not in the corners of its box. + using var bitmap = Render(new XpsBuilder().Page("", 200, 200)); + AssertColor(bitmap, 100, 100, "#000000"); + AssertColor(bitmap, 56, 100, "#000000"); + AssertColor(bitmap, 55, 55, "#FFFFFF"); + } + + [Fact] + public void Path_geometry_elements_fill_and_stroke_their_figures_and_segments() + { + // The first figure isn't filled, only stroked; the second one's middle segment isn't stroked. + var geometry = "" + + "" + + "" + + ""; + using var bitmap = Render(new XpsBuilder().Page($"{geometry}", 200, 100)); + AssertColor(bitmap, 50, 50, "#FFFFFF"); + AssertColor(bitmap, 10, 50, "#0000FF"); + AssertColor(bitmap, 150, 30, "#FF0000"); + AssertColor(bitmap, 186, 50, "#FF0000"); // the unstroked edge + AssertColor(bitmap, 150, 10, "#0000FF"); + } + + [Fact] + public void Dashes_draw_gaps() + { + using var bitmap = Render(new XpsBuilder().Page( + "", 200, 20)); + AssertColor(bitmap, 10, 10, "#000000"); + AssertColor(bitmap, 30, 10, "#FFFFFF"); + AssertColor(bitmap, 50, 10, "#000000"); + } + + [Fact] + public void Clips_opacity_and_opacity_masks_apply() + { + var builder = new XpsBuilder().Page( + $"" + + $"" + + $"" + + "" + + "", + 200, 200); + using var bitmap = Render(builder); + AssertColor(bitmap, 25, 25, "#FF0000"); + AssertColor(bitmap, 75, 75, "#FFFFFF"); + AssertColor(bitmap, 125, 25, "#FF8080", 8); + AssertColor(bitmap, 2, 125, "#0000FF"); + AssertColor(bitmap, 198, 125, "#FFFFFF"); + } + + // ───────────────────────── Brushes ───────────────────────── + + [Fact] + public void Gradients_run_from_their_first_stop_to_their_last() + { + var stops = ""; + var builder = new XpsBuilder().Page( + $"" + + $"{stops}" + + $"" + + $"{stops}" + + // Stops beyond 0…1 are cut there: the colour at 0 lies halfway between the stops at -1 and 1. + $"" + + "" + + "", + 200, 200); + using var bitmap = Render(builder); + AssertColor(bitmap, 3, 25, "#FF0000"); + AssertColor(bitmap, 197, 25, "#0000FF"); + AssertColor(bitmap, 50, 150, "#FF0000"); + AssertColor(bitmap, 97, 150, "#0000FF", 40); + AssertColor(bitmap, 101, 150, "#808080", 16); + } + + [Fact] + public void Image_brushes_draw_png_jpeg_and_tiff_at_their_resolution() + { + var builder = new XpsBuilder(); + // 4 × 4 pixels at 192 dpi are 2 × 2 units. + var png = builder.Image(Png(SKColors.Lime, dpi: 192), "png"); + var jpeg = builder.Image(Picture(SKColors.Red, MagickFormat.Jpeg), "jpg"); + var tiff = builder.Image(Picture(SKColors.Blue, MagickFormat.Tiff), "tif"); + builder.Page( + $"" + + $"" + + $"", + 300, 100); + _ = jpeg; + using var bitmap = Render(builder); + AssertColor(bitmap, 90, 90, "#00FF00"); + AssertColor(bitmap, 150, 50, "#FF0000", 40); + AssertColor(bitmap, 250, 50, "#0000FF"); + } + + [Fact] + public void Tiled_image_and_visual_brushes_repeat_and_flip() + { + var builder = new XpsBuilder(); + using var halves = new MagickImage(MagickColors.White, 2, 1); + halves.GetPixels().SetPixel(0, 0, [0, 0, 0]); + var picture = builder.Image(halves.ToByteArray(MagickFormat.Png), "png"); + var visual = $""; + builder.Page( + $"" + + $"" + + $"{visual}", + 100, 60); + using var bitmap = Render(builder); + // Tile: black, white, black, white… + AssertColor(bitmap, 5, 10, "#000000"); + AssertColor(bitmap, 15, 10, "#FFFFFF"); + AssertColor(bitmap, 25, 10, "#000000"); + // FlipX: black, white, white, black… + AssertColor(bitmap, 25, 30, "#FFFFFF"); + AssertColor(bitmap, 35, 30, "#000000"); + // The visual: its left half black in every 20-unit tile. + AssertColor(bitmap, 25, 50, "#000000"); + AssertColor(bitmap, 35, 50, "#FFFFFF"); + } + + [Fact] + public void Static_resources_come_from_the_page_canvases_and_remote_dictionaries() + { + var builder = new XpsBuilder(); + var remote = builder.Dictionary($""); + builder.Page( + "" + + $"" + + $"" + + "", + 100, 50); + using var bitmap = Render(builder); + AssertColor(bitmap, 20, 20, "#FF0000"); + AssertColor(bitmap, 70, 20, "#0000FF"); + } + + // ───────────────────────── Glyphs ───────────────────────── + + [Fact] + public async Task Glyphs_in_an_obfuscated_font_become_text_in_the_pdf() + { + var builder = new XpsBuilder(); + var font = builder.Font(TestFont()); + builder.Page($"" + + $"", + 400, 150); + var source = Save(builder); + using (var document = XpsDocument.Open(source, Token)) + { + var page = document.Page(0, Token); + // Plain runs go to the page model as text, so the PDF writer subsets their font. + var runs = page.Items.OfType().Select(g => g.Run).ToList(); + Assert.Equal(2, runs.Count); + Assert.Equal("{Hamburg}", runs[1].Text); // "{}" escapes a string that starts with a brace + Assert.True(runs[1].Embolden); + using var bitmap = page.Render(96); + Assert.Contains(Enumerable.Range(20, 120), x => bitmap.GetPixel(x, 50).Red < 100); + } + var written = await new DrawingConverter().ConvertAsync( + new ConversionStep(source, "xps", "pdf", new Preset(), new FolderOutput(_folder), _folder), null, Token); + using var pdf = PdfDocument.Open(Assert.Single(written)); + var text = string.Concat(pdf.GetPages().Select(p => p.Text)); + Assert.Contains("Filee", text, StringComparison.Ordinal); + Assert.Contains("Hamburg", text, StringComparison.Ordinal); + } + + [Fact] + public void Indices_set_glyphs_advances_offsets_and_clusters() + { + using var font = PageFont.FromData(TestFont()); + var typeface = font.Typeface; + var e = typeface.GetGlyph('e'); + // "ffi" as one glyph for three characters, then 'e' with an advance of half the em and one moved up by a tenth. + var layout = XpsGlyphLayout.Lay(typeface, 100, new SKPoint(10, 200), "ffiee", $"(3:1){e};{e},50;{e},,0,10", rightToLeft: false); + Assert.Equal([e, e, e], layout.Glyphs); + Assert.Equal([0, 3, 4], layout.Clusters); + Assert.Equal(200f - 10, layout.Positions[2].Y); + Assert.Equal(50f, layout.Positions[2].X - layout.Positions[1].X, 3); + + // Right to left: each glyph left of the one before. + var rtl = XpsGlyphLayout.Lay(typeface, 100, new SKPoint(500, 200), "Fil", null, rightToLeft: true); + Assert.True(rtl.Positions[0].X < 500 && rtl.Positions[1].X < rtl.Positions[0].X && rtl.Positions[2].X < rtl.Positions[1].X); + } + + // ───────────────────────── Converter ───────────────────────── + + [Fact] + public void Xps_and_oxps_are_one_format_the_drawing_engine_reads() + { + Assert.Equal("xps", FormatRegistry.FindByExtension("oxps")?.Id); + Assert.Equal("xps", FormatRegistry.FindByExtension("xps")?.Id); + var engine = Assert.Single(fx.Converters.OfType()); + Assert.Equal([("xps", "pdf"), ("xps", "svg"), ("xps", "png")], engine.Edges.Where(e => e.From == "xps").Select(e => (e.From, e.To))); + Assert.All(engine.Edges.Where(e => e.From == "xps"), e => Assert.False(e.Checked)); + Assert.Equal("drawing", fx.Catalog.CreatePlanner([]).Plan("xps", "pdf")!.Steps[0].Converter.Id); + } + + [Theory] + [InlineData("pdf", 1)] + [InlineData("svg", 2)] + [InlineData("png", 2)] + public async Task An_xps_is_converted_page_by_page(string target, int files) + { + var source = Save(new XpsBuilder() + .Page($"", 200, 100) + .Page($"", 200, 100)); + var written = await new DrawingConverter().ConvertAsync( + new ConversionStep(source, "xps", target, new Preset(), new FolderOutput(_folder), _folder), null, Token); + Assert.Equal(files, written.Count); + Assert.All(written, path => Assert.True(new FileInfo(path).Length > 100)); + if (target == "pdf") + { + using var pdf = PdfDocument.Open(written[0]); + Assert.Equal(2, pdf.NumberOfPages); + Assert.Equal(150, pdf.GetPage(1).Width, 1); + Assert.Single(pdf.GetPage(1).GetAnnotations()); + Assert.Contains("https://filee.sh/", System.Text.Encoding.Latin1.GetString(File.ReadAllBytes(written[0])), StringComparison.Ordinal); + } + } + + [Fact] + public async Task A_jpeg_xr_picture_is_left_out_and_named_in_the_result() + { + var builder = new XpsBuilder(); + var hdPhoto = builder.Image([0x49, 0x49, 0xBC, 0x01, .. new byte[60]], "wdp"); + builder.Page($"" + + $"", 100, 50); + var source = Save(builder); + LeftOutNotice? notice = null; + var written = await new DrawingConverter().ConvertAsync( + new ConversionStep(source, "xps", "pdf", new Preset(), new FolderOutput(_folder), _folder, LeftOut: n => notice = n), null, Token); + Assert.Single(written); + Assert.NotNull(notice); + Assert.Equal("notice.renderer_left_out", notice.Key); + Assert.Equal(new LeftOutPart("notice.renderer_left_out.pictures", 1), Assert.Single(notice.Parts)); + } + + // ───────────────────────── Hostile input ───────────────────────── + + [Fact] + public void Hostile_documents_are_refused_or_cut_quickly() + { + var clock = Stopwatch.StartNew(); + + // Canvases nested deeper than the limit. + var deep = string.Concat(Enumerable.Repeat("", 200)) + string.Concat(Enumerable.Repeat("", 200)); + using (var document = XpsDocument.Open(Save(new XpsBuilder().Page(deep), "deep.xps"), Token)) + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => document.Page(0, Token)).ErrorKey); + + // A visual brush that draws itself ends at the nesting limit. + var self = "" + + $"" + + $""; + using (var document = XpsDocument.Open(Save(new XpsBuilder().Page(self, 100, 100), "self.xps"), Token)) + using (var bitmap = document.Page(0, Token).Render(12)) + Assert.NotNull(bitmap); + + // More points, pages or path data than allowed. + var points = "M 0,0" + string.Concat(Enumerable.Repeat(" L 1,1", 2_000)); + var limits = XpsLimits.Default with { MaxPoints = 1_000, MaxPages = 3, MaxPathData = 100_000 }; + using (var document = XpsDocument.Open(Save(new XpsBuilder().Page($""), "points.xps"), Token, limits)) + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => document.Page(0, Token)).ErrorKey); + var many = new XpsBuilder(); + for (var i = 0; i < 4; i++) + many.Page(""); + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => XpsDocument.Open(Save(many, "pages.xps"), Token, limits)).ErrorKey); + var data = "M 0,0" + string.Concat(Enumerable.Repeat(" ", 200_000)); + using (var document = XpsDocument.Open(Save(new XpsBuilder().Page($""), "data.xps"), Token, limits)) + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => document.Page(0, Token)).ErrorKey); + + // A picture whose header claims 100,000 × 100,000 pixels is left out, not decoded. + var bomb = DocumentFuzzTests.TinyPng(4, 4); + System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(bomb.AsSpan(16), 100_000); + System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(bomb.AsSpan(20), 100_000); + var builder = new XpsBuilder(); + var part = builder.Image(bomb, "png"); + builder.Page($"", 50, 50); + using (var document = XpsDocument.Open(Save(builder, "bomb.xps"), Token)) + { + using var bitmap = document.Page(0, Token).Render(24); + Assert.Equal(1, Assert.Single(document.LeftOut).Count); + } + + // A huge page is cut to PDF's 200 inches. + using (var document = XpsDocument.Open(Save(new XpsBuilder().Page("", 1e9, 1e9), "huge.xps"), Token)) + Assert.Equal(14_400f, document.Page(0, Token).Width, 1); + + Assert.True(clock.Elapsed < TimeSpan.FromSeconds(20), $"took {clock.Elapsed}"); + } + + private sealed class FolderOutput(string folder) : IOutputAllocator + { + public string? Allocate(string extension, string? suffix = null) => Path.Combine(folder, $"out{suffix}.{extension}"); + } +}