From 1fa8ab23e718c83691fd64d865cb1c15a842dfd7 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Thu, 19 Nov 2026 11:03:53 +0900 Subject: [PATCH 1/6] ci: install the video test's FFmpeg without man-db, with apt timeouts and retries The metadata job hit its 30-minute limit on #1663, #1669, #1671, #1672 and #1679: apt took 23-30 minutes to install Ubuntu's FFmpeg. Skip the runner's man-db index rebuild, skip recommends, give apt network timeouts and three attempts, and limit the step to 12 minutes. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1628bf942..04d45c0a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -143,10 +143,25 @@ jobs: with: global-json-file: global.json + # Ubuntu's FFmpeg pulls in about a hundred packages. The runner's man-db trigger rebuilds the manual index after + # them (minutes on its own), and a slow mirror has stalled the step past the job's limit, so: no man-db update, + # no recommends, apt timeouts and retries, and a step limit that fails fast instead of eating the job's. - name: FFmpeg for the video test + timeout-minutes: 12 run: | - sudo apt-get update - sudo apt-get install -y ffmpeg + set -euo pipefail + sudo rm -f /var/lib/man-db/auto-update + apt="-o Acquire::Retries=3 -o Acquire::http::Timeout=30 -o Acquire::https::Timeout=30 -o Dpkg::Use-Pty=0" + for attempt in 1 2 3; do + if sudo timeout 240 apt-get $apt update -q && + sudo DEBIAN_FRONTEND=noninteractive timeout 420 apt-get $apt install -y -q --no-install-recommends ffmpeg; then + break + fi + [ "$attempt" = 3 ] && exit 1 + echo "::warning::apt attempt $attempt failed; trying again" + sleep 10 + done + ffmpeg -hide_banner -version | sed -n 1p - name: Fetch ExifTool run: | From faccbf75a294488c9fda780d172212dc74726607 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Thu, 19 Nov 2026 23:59:07 +0900 Subject: [PATCH 2/6] feat(tables): DOC, ODT, RTF, HWP and HWPX tables to XLSX, ODS and CSV (#1016) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The document tables route now takes every document the built-in readers read with tables: DOC (and DOT, WPS), ODT, RTF, HWP and HWPX, like DOCX and HTML. Each table is one sheet, merged cells stay merged, numbers and dates become real values and header rows are frozen. Before, these files had no route to a spreadsheet at all. - Cells keep italic and centred or right alignment besides bold, fill and borders; left and justified text keeps Excel's own alignment. - Rows under a merged cell get the height its lines need: Excel and Calc don't fit a row to a merged cell, so its text was cut off. - 한글 files are read as the renderer reads them, so a number typed as an equation is a number ("40"), not its script ("40~~~"). - DOC edges are checked, as on DOC's other routes. - Bound the workbook: at most 10,000 sheets, Excel's row count per sheet and 10 million cell positions in all, counted before anything is written; nested tables are walked without recursion. Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 35 ++- src/Filee.App/Assets/i18n/ar.json | 4 +- src/Filee.App/Assets/i18n/bn-BD.json | 4 +- src/Filee.App/Assets/i18n/de.json | 4 +- src/Filee.App/Assets/i18n/en.json | 4 +- src/Filee.App/Assets/i18n/es.json | 4 +- src/Filee.App/Assets/i18n/fa.json | 4 +- src/Filee.App/Assets/i18n/fr.json | 4 +- src/Filee.App/Assets/i18n/hi.json | 4 +- src/Filee.App/Assets/i18n/id.json | 4 +- src/Filee.App/Assets/i18n/it.json | 4 +- src/Filee.App/Assets/i18n/ja.json | 4 +- src/Filee.App/Assets/i18n/ko.json | 4 +- src/Filee.App/Assets/i18n/mn.json | 4 +- src/Filee.App/Assets/i18n/ne.json | 4 +- src/Filee.App/Assets/i18n/pl.json | 4 +- src/Filee.App/Assets/i18n/pt-BR.json | 4 +- src/Filee.App/Assets/i18n/pt-PT.json | 4 +- src/Filee.App/Assets/i18n/ru.json | 4 +- src/Filee.App/Assets/i18n/th.json | 4 +- src/Filee.App/Assets/i18n/tr.json | 4 +- src/Filee.App/Assets/i18n/vi.json | 4 +- src/Filee.App/Assets/i18n/zh-CN.json | 4 +- src/Filee.App/Assets/i18n/zh-TW.json | 4 +- .../Office/DocumentTablesConverter.cs | 103 +++++-- .../Office/Sheets/TableSheets.cs | 101 ++++++- .../DocumentTablesTests.cs | 271 +++++++++++++++++- 27 files changed, 501 insertions(+), 101 deletions(-) diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 7969aedab..9d506af31 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -27,7 +27,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status, | PDF slides | built in (PDFium, PdfPig) | **PDF → PPTX**: a picture of each page on its own slide (looks like the PDF, not editable), the page's text in the speaker notes | | PDF tables | built in (PdfPig) | **PDF → XLSX, ODS, CSV** (#267): each table drawn with lines on its own sheet, numbers and dates as real values, merged cells and column widths kept | | **PostScript writer** | built in | **PDF → PS (LanguageLevel 3), EPS** (one per page) without Ghostscript: vectors, text with its embedded fonts, pictures, shadings, patterns; pages with transparency as pictures (see [PDF → PostScript and EPS](#pdf--postscript-and-eps)) | -| Document tables | built in | **DOCX (+ DOCM/DOTX/DOTM), HTML, MHTML → XLSX, ODS, CSV** (#1016): each table on its own sheet, numbers and dates as real values, merged cells, borders and column widths kept | +| Document tables | built in | **DOCX (+ DOCM/DOTX/DOTM), DOC (+ DOT/WPS), ODT, RTF, HWP, HWPX, HTML, MHTML → XLSX, ODS, CSV** (#1016): each table on its own sheet, numbers and dates as real values, merged cells, borders, fill, alignment and column widths kept | | E-mail | built in (MimeKit, MsgReader) | **EML, Outlook MSG → HTML** (headers + body, inline pictures), **TXT**, **ZIP** (the attachments); **MSG → EML**; **MHTML → HTML** (the saved page in one file, pictures inlined; on to PDF, DOCX, HWPX, TXT, Markdown, EPUB from there) | | Signed files | built in (System.Security.Cryptography.Pkcs) | **P7M → the signed file inside** (CMS / PKCS #7, CAdES: Italian e-invoices `*.xml.p7m`, signed PDFs `*.pdf.p7m`), then on from its format (P7M → PDF → JPG); the signature isn't checked | | Data | built in (System.Text.Json, YamlDotNet, Tomlyn, System.Xml) | **JSON, JSON Lines, YAML, TOML, XML, CSV ↔ each other** (CSV for tables); **vCard, iCalendar ↔ CSV**; **property lists (.plist, XML and binary) → each of them and TXT**; FreeMASTER oscilloscope TXT → CSV; spreadsheets, contacts and events reach JSON / YAML / TOML / XML through CSV | @@ -2225,26 +2225,35 @@ not found. workbook. The same tables can be written as ODS, or as CSV (one file per sheet). These edges are direct only: an image made into a PDF has no ruled tables, so no picture reaches a spreadsheet through PDF. -## DOCX / HTML → XLSX +## Documents → XLSX -`Office/DocumentTablesConverter.cs` puts the tables of a Word document (DOCX and its DOCM / DOTX / DOTM variants), a web -page (HTML) or a saved web page (MHTML) into a workbook (#1016). The tables are the ones the DOCX and HTML readers give -the HWPX and DOCX writers, with their merged cells (`gridSpan` / `vMerge`, `colspan` / `rowspan`); text outside the -tables is left out. The cells are written as in PDF → XLSX (`Office/Sheets/TableSheets.cs`, shared by both). +`Office/DocumentTablesConverter.cs` puts the tables of a Word document (DOCX and its DOCM / DOTX / DOTM variants, DOC +and its DOT / WPS variants), an OpenDocument text (ODT), an RTF file, a 한글 file (HWP, HWPX), a web page (HTML) or a +saved web page (MHTML) into a workbook (#1016). The tables are the ones the built-in readers give the HWPX and DOCX +writers, with their merged cells (`gridSpan` / `vMerge`, DOC's merge flags, `number-columns-spanned` / +`number-rows-spanned`, `\clmgf` / `\clvmgf`, 한글's `cellSpan`, `colspan` / `rowspan`); text outside the tables is left +out. The cells are written as in PDF → XLSX (`Office/Sheets/TableSheets.cs`, shared by both). - **One sheet per table** ("Table 1", "Table 2" …) in reading order, a table inside a cell too (its text isn't repeated in the outer cell). Nothing is joined: a Word table running over a page break is one table already. A table without any text, such as a web page's layout table around the real one, gets no sheet. -- **Numbers and dates** become real values by the same rules as PDF → XLSX (above). +- **Numbers and dates** become real values by the same rules as PDF → XLSX (above). 한글 files are read as the renderer + reads them, so a number typed as an equation (common in 한글 tables) is its text ("40"), not its script ("40~~~"). - Cells keep their own borders (a Word table style's lines, the HTML grid unless `border="0"`), shading, vertical - alignment and bold. Column widths come from the Word grid or the HTML `` / first-row widths (shares of an A4 - text width); without either, a column is as wide as its longest line, up to 60 characters. Rows fit their text: a - Word row height is only a minimum. Leading header rows (Word's "repeat as header row", HTML `` or rows of - ``) are frozen. + alignment, bold and italic (when all the cell's text is), and centred or right alignment (when all its paragraphs + are; left and justified text keeps Excel's own, numbers to the right). Column widths come from the document's grid + (Word, DOC, RTF, ODT, 한글) or the HTML `` / first-row widths (shares of an A4 text width); without either, a + column is as wide as its longest line, up to 60 characters. Rows fit their text: a document's row height is only a + minimum. Excel doesn't fit a row to a merged cell, so the rows under a merged cell get the height its lines need. + Leading header rows (Word's and RTF's "repeat as header row", ODT header rows, 한글's title cells, HTML `` or + rows of ``) are frozen. - A file without tables fails with `error.document_no_tables`. The edges are direct only, like PDF → XLSX: a PDF, a picture or an e-book that became a DOCX on the way doesn't reach spreadsheets this way, and a document's tables - don't go on to JSON or XML. For the same reason DOC (which LibreOffice reads into a DOCX first) isn't an input: the - XLSX preset's note says to convert it to DOCX first. + don't go on to JSON or XML. DOC edges are checked: a Word 6.0 / 95 or Works file the DOC reader doesn't read gets + the reader's own message (LibreOffice has no Writer → spreadsheet route to fall back on). +- Limits: the tables of one file get at most 10,000 sheets, 1,048,576 rows each (Excel's) and 10,000,000 cell + positions together (`HwpxWriter.MaxTableCells`), counted before anything is written; more is refused with + `error.document_too_complex`. Nested tables are walked with a stack, not recursion. ## PDF → PPTX diff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.json index b190e5a2d..94d98dafe 100644 --- a/src/Filee.App/Assets/i18n/ar.json +++ b/src/Filee.App/Assets/i18n/ar.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "كاتب PostScript (مدمج)", "engine.pdf-ps.description": "يحوّل ملفات PDF إلى PostScript وEPS دون Ghostscript: يحتفظ النص بخطوطه وتبقى الرسوم متجهة. الصفحات التي فيها شفافية تصبح صورًا.", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "يحوّل جداول مستندات Word (DOCX) وصفحات الويب (HTML وMHTML) إلى مصنف Excel، لكل جدول ورقة، مع الأرقام والتواريخ كقيم حقيقية.", + "engine.document-tables.description": "يحوّل جداول المستندات (DOCX, DOC, ODT, RTF, HWP, HWPX) وصفحات الويب (HTML وMHTML) إلى مصنف Excel، لكل جدول ورقة، مع الأرقام والتواريخ كقيم حقيقية.", "engine.email.name": "E-mail (built-in)", "engine.email.description": "تحويل رسائل البريد (EML وOutlook MSG) إلى HTML ونص مع المرسل والمستلمين والتاريخ والصور، وتجميع المرفقات في ZIP.\nتحويل MSG إلى EML، وصفحات الويب المحفوظة (MHTML) إلى ملف HTML واحد مع صورها.", "engine.p7m.name": "Signed files (built-in)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "دقائق لكل جزء", "presets.video_parts_hint": "يُنسخ كما هو دون إعادة ترميز، لذا لا تنطبق إعدادات الجودة والحجم والترميز. ينتهي كل جزء عند إطار رئيسي، لذا قد يطول بضع ثوانٍ عمّا طُلب.", "presets.pdf_tables_note": "من ملف PDF يصبح كل جدول مرسوم بخطوط ورقة مستقلة (ويبقى الجدول الممتد إلى الصفحة التالية ورقة واحدة)، وتصبح الأرقام والتواريخ قيمًا حقيقية. يُترك النص خارج الجداول.", - "presets.document_tables_note": "من مستند Word (DOCX) أو صفحة ويب (HTML) يصبح كل جدول ورقة مستقلة، وتصبح الأرقام والتواريخ قيمًا حقيقية. يُترك النص خارج الجداول. حوّل ملف DOC إلى DOCX أولًا.", + "presets.document_tables_note": "من مستند (DOCX, DOC, ODT, RTF, HWP, HWPX) أو صفحة ويب (HTML) يصبح كل جدول ورقة مستقلة، وتصبح الأرقام والتواريخ قيمًا حقيقية. يُترك النص خارج الجداول.", "presets.pdf_compression": "الضغط", "presets.pdf_compression.None": "دون ضغط", "presets.pdf_compression.Screen": "أصغر حجم (الصور بدقة 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/bn-BD.json b/src/Filee.App/Assets/i18n/bn-BD.json index c5989b448..4793168df 100644 --- a/src/Filee.App/Assets/i18n/bn-BD.json +++ b/src/Filee.App/Assets/i18n/bn-BD.json @@ -312,7 +312,7 @@ "engine.pdf-ps.name": "PostScript রাইটার (বিল্ট-ইন)", "engine.pdf-ps.description": "Ghostscript ছাড়াই পিডিএফকে PostScript ও EPS-এ রূপান্তর করে: লেখা তার ফন্ট রাখে, অঙ্কন ভেক্টর থাকে। স্বচ্ছতা থাকা পৃষ্ঠাগুলো ছবি হয়ে যায়।", "engine.document-tables.name": "ডকুমেন্ট সারণি (বিল্ট-ইন)", - "engine.document-tables.description": "ওয়ার্ড ডকুমেন্ট (DOCX) ও ওয়েব পেজের (HTML, MHTML) সারণিগুলোকে এক্সেল ওয়ার্কবুকে পরিণত করে, প্রতিটির জন্য একটি শিট, যেখানে সংখ্যা এবং তারিখগুলো প্রকৃত মান হিসেবে থাকে।", + "engine.document-tables.description": "ডকুমেন্ট (DOCX, DOC, ODT, RTF, HWP, HWPX) ও ওয়েব পেজের (HTML, MHTML) সারণিগুলোকে এক্সেল ওয়ার্কবুকে পরিণত করে, প্রতিটির জন্য একটি শিট, যেখানে সংখ্যা এবং তারিখগুলো প্রকৃত মান হিসেবে থাকে।", "engine.email.name": "ই-মেইল (বিল্ট-ইন)", "engine.email.description": "ই-মেইল (EML, Outlook MSG) থেকে প্রেরক, প্রাপক ও তারিখসহ এইচটিএমএল এবং টেক্সটে রূপান্তর, ছবিসহ; সংযুক্ত ফাইলগুলো একটি ZIP-এ।\nMSG থেকে EML। সংরক্ষিত ওয়েব পেজ (Mএইচটিএমএল) ছবিসহ একটি একক এইচটিএমএল ফাইলে।", "engine.p7m.name": "স্বাক্ষরিত ফাইল (বিল্ট-ইন)", @@ -1114,7 +1114,7 @@ "presets.split_minutes": "মিনিট প্রতি ভাগ", "presets.video_parts_hint": "আবার এনকোড না করে যেমন আছে তেমন কপি হয়, তাই কোয়ালিটি, সাইজ আর কোডেকের সেটিং খাটে না। প্রতিটি ভাগ একটি কী ফ্রেমে শেষ হয়, তাই চাওয়ার চেয়ে কয়েক সেকেন্ড লম্বা হতে পারে।", "presets.pdf_tables_note": "একটি পিডিএফ থেকে রেখা দিয়ে আঁকা প্রতিটি সারণি নিজস্ব শিট হয়ে যায় (পরবর্তী পৃষ্ঠায় বিস্তৃত সারণি একটি একক শিটেই থাকে), যেখানে সংখ্যা ও তারিখ প্রকৃত মান হিসেবে থাকে। সারণির বাইরের টেক্সট বাদ দেওয়া হয়।", - "presets.document_tables_note": "একটি ওয়ার্ড ডকুমেন্ট (DOCX) বা ওয়েব পেজ (HTML) থেকে প্রতিটি সারণি নিজস্ব শিট হয়ে যায়, যেখানে সংখ্যা ও তারিখ প্রকৃত মান হিসেবে থাকে। সারণির বাইরের টেক্সট বাদ দেওয়া হয়। DOC ফাইল আগে DOCX-এ রূপান্তর করুন।", + "presets.document_tables_note": "একটি ডকুমেন্ট (DOCX, DOC, ODT, RTF, HWP, HWPX) বা ওয়েব পেজ (HTML) থেকে প্রতিটি সারণি নিজস্ব শিট হয়ে যায়, যেখানে সংখ্যা ও তারিখ প্রকৃত মান হিসেবে থাকে। সারণির বাইরের টেক্সট বাদ দেওয়া হয়।", "presets.pdf_compression": "সংকোচন (কম্প্রেশন)", "presets.pdf_compression.None": "সংকুচিত করবেন না", "presets.pdf_compression.Screen": "সবচেয়ে ছোট (৭২ dpi-তে ছবি)", diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.json index c36f41e20..b51797dec 100644 --- a/src/Filee.App/Assets/i18n/de.json +++ b/src/Filee.App/Assets/i18n/de.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "PostScript-Writer (integriert)", "engine.pdf-ps.description": "Wandelt PDFs ohne Ghostscript in PostScript und EPS um: Text behält seine Schriften, Zeichnungen bleiben Vektoren. Seiten mit Transparenz werden zu Bildern.", "engine.document-tables.name": "Dokumenttabellen (integriert)", - "engine.document-tables.description": "Überträgt die Tabellen aus Word-Dokumenten (DOCX) und Webseiten (HTML, MHTML) in eine Excel-Arbeitsmappe, jeweils auf ein eigenes Blatt. Zahlen und Datumsangaben bleiben echte Werte.", + "engine.document-tables.description": "Überträgt die Tabellen aus Dokumenten (DOCX, DOC, ODT, RTF, HWP, HWPX) und Webseiten (HTML, MHTML) in eine Excel-Arbeitsmappe, jeweils auf ein eigenes Blatt. Zahlen und Datumsangaben bleiben echte Werte.", "engine.email.name": "E-Mail (integriert)", "engine.email.description": "E-Mails (EML, Outlook MSG) in HTML und Text mit Absender, Empfängern, Datum und Bildern; Anhänge in eine ZIP-Datei.\nMSG in EML. Gespeicherte Webseiten (MHTML) in eine HTML-Datei mit ihren Bildern.", "engine.p7m.name": "Signierte Dateien (integriert)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "Minuten pro Teil", "presets.video_parts_hint": "Wird unverändert kopiert, ohne neu zu kodieren: Qualität, Größe und Codec gelten hier nicht. Jeder Teil endet an einem Schlüsselbild und kann daher ein paar Sekunden länger sein als gewählt.", "presets.pdf_tables_note": "Jede Tabelle mit eingezeichneten Linien in einer PDF wird zu einem eigenen Tabellenblatt. Eine über mehrere Seiten fortgesetzte Tabelle bleibt auf einem Blatt. Zahlen und Datumsangaben bleiben echte Werte. Text außerhalb der Tabellen wird weggelassen.", - "presets.document_tables_note": "Jede Tabelle in einem Word-Dokument (DOCX) oder einer Webseite (HTML) wird zu einem eigenen Tabellenblatt. Zahlen und Datumsangaben bleiben echte Werte. Text außerhalb der Tabellen wird weggelassen. Eine DOC-Datei zuerst in DOCX umwandeln.", + "presets.document_tables_note": "Jede Tabelle in einem Dokument (DOCX, DOC, ODT, RTF, HWP, HWPX) oder einer Webseite (HTML) wird zu einem eigenen Tabellenblatt. Zahlen und Datumsangaben bleiben echte Werte. Text außerhalb der Tabellen wird weggelassen.", "presets.pdf_compression": "Komprimieren", "presets.pdf_compression.None": "Nicht komprimieren", "presets.pdf_compression.Screen": "Kleinste Datei (Bilder mit 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.json index b37832e4a..c2abcf181 100644 --- a/src/Filee.App/Assets/i18n/en.json +++ b/src/Filee.App/Assets/i18n/en.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "PostScript writer (built-in)", "engine.pdf-ps.description": "Turns PDFs into PostScript and EPS without Ghostscript: text keeps its fonts, drawings stay vector. Pages with transparency become pictures.", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Turns the tables of Word documents (DOCX) and web pages (HTML, MHTML) into an Excel workbook, one sheet each, with numbers and dates as real values.", + "engine.document-tables.description": "Turns the tables of documents (DOCX, DOC, ODT, RTF, HWP, HWPX) and web pages (HTML, MHTML) into an Excel workbook, one sheet each, with numbers and dates as real values.", "engine.email.name": "E-mail (built-in)", "engine.email.description": "E-mails (EML, Outlook MSG) to HTML and text with sender, recipients and date, pictures included; attachments into a ZIP.\nMSG to EML. Saved web pages (MHTML) to one HTML file with their pictures.", "engine.p7m.name": "Signed files (built-in)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "minutes per part", "presets.video_parts_hint": "Copied as it is, without encoding again, so the quality, size and codec settings don't apply. Each part ends at a key frame, so it can run a few seconds longer than asked.", "presets.pdf_tables_note": "From a PDF, each table drawn with lines becomes its own sheet (a table that goes on over the next page stays one sheet), with numbers and dates as real values. Text outside the tables is left out.", - "presets.document_tables_note": "From a Word document (DOCX) or a web page (HTML), each table becomes its own sheet, with numbers and dates as real values. Text outside the tables is left out. Convert a DOC file to DOCX first.", + "presets.document_tables_note": "From a document (DOCX, DOC, ODT, RTF, HWP, HWPX) or a web page (HTML), each table becomes its own sheet, with numbers and dates as real values. Text outside the tables is left out.", "presets.pdf_compression": "Compress", "presets.pdf_compression.None": "Don't compress", "presets.pdf_compression.Screen": "Smallest (pictures at 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.json index 08816e86e..460a757b8 100644 --- a/src/Filee.App/Assets/i18n/es.json +++ b/src/Filee.App/Assets/i18n/es.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "Generador PostScript (integrado)", "engine.pdf-ps.description": "Convierte PDF en PostScript y EPS sin Ghostscript: el texto conserva sus fuentes y los dibujos siguen siendo vectoriales. Las páginas con transparencias se convierten en imágenes.", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Convierte las tablas de documentos de Word (DOCX) y páginas web (HTML, MHTML) en un libro de Excel, una hoja por tabla, con números y fechas como valores reales.", + "engine.document-tables.description": "Convierte las tablas de documentos (DOCX, DOC, ODT, RTF, HWP, HWPX) y páginas web (HTML, MHTML) en un libro de Excel, una hoja por tabla, con números y fechas como valores reales.", "engine.email.name": "E-mail (built-in)", "engine.email.description": "Correos electrónicos (EML, Outlook MSG) a HTML y texto con remitente, destinatarios y fecha, imágenes incluidas; los adjuntos van a un ZIP.\nMSG a EML. Páginas web guardadas (MHTML) a un solo archivo HTML con sus imágenes.", "engine.p7m.name": "Signed files (built-in)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "minutos por parte", "presets.video_parts_hint": "Se copia tal cual, sin volver a codificar, así que la calidad, el tamaño y el códec no se aplican. Cada parte termina en un fotograma clave, por lo que puede durar unos segundos más de lo pedido.", "presets.pdf_tables_note": "De un PDF, cada tabla dibujada con líneas pasa a su propia hoja (una tabla que sigue en la página siguiente queda en una sola hoja), con números y fechas como valores reales. El texto fuera de las tablas se omite.", - "presets.document_tables_note": "De un documento de Word (DOCX) o una página web (HTML), cada tabla pasa a su propia hoja, con números y fechas como valores reales. El texto fuera de las tablas se omite. Convierte primero un archivo DOC a DOCX.", + "presets.document_tables_note": "De un documento (DOCX, DOC, ODT, RTF, HWP, HWPX) o una página web (HTML), cada tabla pasa a su propia hoja, con números y fechas como valores reales. El texto fuera de las tablas se omite.", "presets.pdf_compression": "Comprimir", "presets.pdf_compression.None": "No comprimir", "presets.pdf_compression.Screen": "El más pequeño (imágenes a 72 ppp)", diff --git a/src/Filee.App/Assets/i18n/fa.json b/src/Filee.App/Assets/i18n/fa.json index c109d2bf6..7f0aec436 100644 --- a/src/Filee.App/Assets/i18n/fa.json +++ b/src/Filee.App/Assets/i18n/fa.json @@ -312,7 +312,7 @@ "engine.pdf-ps.name": "نویسنده PostScript (داخلی)", "engine.pdf-ps.description": "بدون Ghostscript فایل‌های PDF را به PostScript و EPS تبدیل می‌کند: متن قلم‌های خود را نگه می‌دارد و طرح‌ها وکتور می‌مانند. صفحه‌های دارای شفافیت به تصویر تبدیل می‌شوند.", "engine.document-tables.name": "جداول سند (داخلی)", - "engine.document-tables.description": "تبدیل جداول اسناد Word (DOCX) و صفحات وب (HTML، MHTML) به کارپوشه Excel، هر جدول در یک برگه، همراه با اعداد و تاریخ‌ها به عنوان مقادیر واقعی.", + "engine.document-tables.description": "تبدیل جداول اسناد (DOCX, DOC, ODT, RTF, HWP, HWPX) و صفحات وب (HTML، MHTML) به کارپوشه Excel، هر جدول در یک برگه، همراه با اعداد و تاریخ‌ها به عنوان مقادیر واقعی.", "engine.email.name": "ایمیل (داخلی)", "engine.email.description": "تبدیل ایمیل‌ها (EML، Outlook MSG) به HTML و متن با مشخصات فرستنده، گیرندگان و تاریخ، شامل تصاویر؛ پیوست‌ها در یک فایل ZIP قرار می‌گیرند.\nتبدیل MSG به EML. صفحات وب ذخیره‌شده (MHTML) به یک فایل HTML با تصاویر آن‌ها.", "engine.p7m.name": "فایل‌های امضاشده (داخلی)", @@ -1114,7 +1114,7 @@ "presets.split_minutes": "دقیقه در هر بخش", "presets.video_parts_hint": "بدون رمزگذاری دوباره و همان‌طور که هست کپی می‌شود، پس تنظیمات کیفیت، اندازه و کدک اعمال نمی‌شوند. هر بخش روی یک فریم کلیدی تمام می‌شود، پس ممکن است چند ثانیه بلندتر از مقدار خواسته‌شده باشد.", "presets.pdf_tables_note": "از یک فایل PDF، هر جدول کشیده‌شده با خطوط به یک برگه مجزا تبدیل می‌شود (جدولی که تا صفحه بعد ادامه دارد به صورت یک برگه می‌ماند)، با اعداد و تاریخ‌ها به عنوان مقادیر واقعی. متن خارج از جدول‌ها نادیده گرفته می‌شود.", - "presets.document_tables_note": "از یک سند Word (DOCX) یا یک صفحه وب (HTML)، هر جدول به یک برگه مجزا تبدیل می‌شود، با اعداد و تاریخ‌ها به عنوان مقادیر واقعی. متن خارج از جدول‌ها نادیده گرفته می‌شود. فایل DOC را ابتدا به DOCX تبدیل کنید.", + "presets.document_tables_note": "از یک سند (DOCX, DOC, ODT, RTF, HWP, HWPX) یا یک صفحه وب (HTML)، هر جدول به یک برگه مجزا تبدیل می‌شود، با اعداد و تاریخ‌ها به عنوان مقادیر واقعی. متن خارج از جدول‌ها نادیده گرفته می‌شود.", "presets.pdf_compression": "فشرده‌سازی تصاویر PDF", "presets.pdf_compression.None": "بدون تغییر (حفظ کیفیت)", "presets.pdf_compression.Screen": "مناسب نمایش روی صفحه (کوچک‌ترین اندازه)", diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.json index 2e3d07f7e..138df6b6a 100644 --- a/src/Filee.App/Assets/i18n/fr.json +++ b/src/Filee.App/Assets/i18n/fr.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "Générateur PostScript (intégré)", "engine.pdf-ps.description": "Convertit les PDF en PostScript et EPS sans Ghostscript : le texte garde ses polices, les dessins restent vectoriels. Les pages avec de la transparence deviennent des images.", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Transforme les tableaux des documents Word (DOCX) et des pages web (HTML, MHTML) en classeur Excel, une feuille par tableau, avec les nombres et les dates en vraies valeurs.", + "engine.document-tables.description": "Transforme les tableaux des documents (DOCX, DOC, ODT, RTF, HWP, HWPX) et des pages web (HTML, MHTML) en classeur Excel, une feuille par tableau, avec les nombres et les dates en vraies valeurs.", "engine.email.name": "E-mail (built-in)", "engine.email.description": "E-mails (EML, MSG Outlook) en HTML et texte avec expéditeur, destinataires et date, images comprises ; pièces jointes dans un ZIP.\nMSG en EML. Pages web enregistrées (MHTML) en un seul fichier HTML avec leurs images.", "engine.p7m.name": "Signed files (built-in)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "minutes par partie", "presets.video_parts_hint": "Copiée telle quelle, sans réencodage : la qualité, la taille et le codec ne s'appliquent pas. Chaque partie se termine sur une image clé et peut donc durer quelques secondes de plus que demandé.", "presets.pdf_tables_note": "D'un PDF, chaque tableau tracé avec des lignes devient une feuille (un tableau qui continue sur la page suivante reste une seule feuille), avec les nombres et les dates en vraies valeurs. Le texte hors des tableaux est omis.", - "presets.document_tables_note": "D'un document Word (DOCX) ou d'une page web (HTML), chaque tableau devient une feuille, avec les nombres et les dates en vraies valeurs. Le texte hors des tableaux est omis. Convertissez d'abord un fichier DOC en DOCX.", + "presets.document_tables_note": "D'un document (DOCX, DOC, ODT, RTF, HWP, HWPX) ou d'une page web (HTML), chaque tableau devient une feuille, avec les nombres et les dates en vraies valeurs. Le texte hors des tableaux est omis.", "presets.pdf_compression": "Compresser", "presets.pdf_compression.None": "Ne pas compresser", "presets.pdf_compression.Screen": "Le plus léger (images à 72 ppp)", diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.json index ef80cd11b..e8b621698 100644 --- a/src/Filee.App/Assets/i18n/hi.json +++ b/src/Filee.App/Assets/i18n/hi.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "PostScript लेखक (अंतर्निहित)", "engine.pdf-ps.description": "Ghostscript के बिना PDF को PostScript और EPS में बदलता है: टेक्स्ट अपने फ़ॉन्ट रखता है, चित्र वेक्टर रहते हैं। पारदर्शिता वाले पेज चित्र बन जाते हैं।", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Word दस्तावेज़ों (DOCX) और वेब पेजों (HTML, MHTML) की तालिकाओं को Excel वर्कबुक में बदलता है, हर तालिका एक शीट, संख्याओं और तारीखों को असली मानों के रूप में।", + "engine.document-tables.description": "दस्तावेज़ों (DOCX, DOC, ODT, RTF, HWP, HWPX) और वेब पेजों (HTML, MHTML) की तालिकाओं को Excel वर्कबुक में बदलता है, हर तालिका एक शीट, संख्याओं और तारीखों को असली मानों के रूप में।", "engine.email.name": "E-mail (built-in)", "engine.email.description": "ई-मेल (EML, Outlook MSG) को भेजने वाले, प्राप्तकर्ताओं और तारीख के साथ HTML और टेक्स्ट में बदलता है, तस्वीरों सहित; अटैचमेंट ZIP में।\nMSG से EML। सहेजे गए वेब पेज (MHTML) उनकी तस्वीरों सहित एक HTML फ़ाइल में।", "engine.p7m.name": "Signed files (built-in)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "मिनट प्रति हिस्सा", "presets.video_parts_hint": "बिना दोबारा एन्कोड किए जैसा है वैसा कॉपी होता है, इसलिए क्वालिटी, साइज़ और कोडेक की सेटिंग लागू नहीं होतीं। हर हिस्सा किसी की फ़्रेम पर खत्म होता है, इसलिए वह तय समय से कुछ सेकंड लंबा हो सकता है।", "presets.pdf_tables_note": "PDF से, रेखाओं से बनी हर तालिका अपनी अलग शीट बनती है (अगले पेज पर जारी तालिका एक ही शीट रहती है), और संख्याएँ व तारीखें असली मान बनती हैं। तालिकाओं के बाहर का टेक्स्ट छोड़ दिया जाता है।", - "presets.document_tables_note": "Word दस्तावेज़ (DOCX) या वेब पेज (HTML) से, हर तालिका अपनी अलग शीट बनती है, और संख्याएँ व तारीखें असली मान बनती हैं। तालिकाओं के बाहर का टेक्स्ट छोड़ दिया जाता है। DOC फ़ाइल को पहले DOCX में बदलें।", + "presets.document_tables_note": "दस्तावेज़ (DOCX, DOC, ODT, RTF, HWP, HWPX) या वेब पेज (HTML) से, हर तालिका अपनी अलग शीट बनती है, और संख्याएँ व तारीखें असली मान बनती हैं। तालिकाओं के बाहर का टेक्स्ट छोड़ दिया जाता है।", "presets.pdf_compression": "कंप्रेस करें", "presets.pdf_compression.None": "कंप्रेस न करें", "presets.pdf_compression.Screen": "सबसे छोटा (तस्वीरें 72 dpi पर)", diff --git a/src/Filee.App/Assets/i18n/id.json b/src/Filee.App/Assets/i18n/id.json index 9332026c8..d85557e2e 100644 --- a/src/Filee.App/Assets/i18n/id.json +++ b/src/Filee.App/Assets/i18n/id.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "Penulis PostScript (bawaan)", "engine.pdf-ps.description": "Mengubah PDF menjadi PostScript dan EPS tanpa Ghostscript: teks tetap memakai fontnya, gambar tetap vektor. Halaman dengan transparansi menjadi gambar.", "engine.document-tables.name": "Tabel dokumen (bawaan)", - "engine.document-tables.description": "Mengubah tabel dalam dokumen Word (DOCX) dan halaman web (HTML, MHTML) menjadi buku kerja Excel, masing-masing tabel satu lembar, dengan angka dan tanggal sebagai nilai data asli.", + "engine.document-tables.description": "Mengubah tabel dalam dokumen (DOCX, DOC, ODT, RTF, HWP, HWPX) dan halaman web (HTML, MHTML) menjadi buku kerja Excel, masing-masing tabel satu lembar, dengan angka dan tanggal sebagai nilai data asli.", "engine.email.name": "Email (bawaan)", "engine.email.description": "Email (EML, Outlook MSG) ke HTML dan teks dengan pengirim, penerima, tanggal, serta gambar terlampir; lampiran ke file ZIP.\nMSG ke EML. Halaman web tersimpan (MHTML) menjadi satu file HTML beserta gambarnya.", "engine.p7m.name": "File bertanda tangan (bawaan)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "menit per bagian", "presets.video_parts_hint": "Disalin apa adanya tanpa encode ulang, jadi pengaturan kualitas, ukuran, dan codec tidak berlaku. Setiap bagian berakhir di key frame, jadi bisa beberapa detik lebih panjang dari yang diminta.", "presets.pdf_tables_note": "Dari sebuah PDF, setiap tabel yang digambar dengan garis pembatas menjadi lembar tersendiri (tabel yang berlanjut ke halaman berikutnya tetap menjadi satu lembar), dengan angka dan tanggal sebagai nilai asli. Teks di luar tabel diabaikan.", - "presets.document_tables_note": "Dari dokumen Word (DOCX) atau halaman web (HTML), setiap tabel menjadi lembar tersendiri, dengan angka dan tanggal sebagai nilai asli. Teks di luar tabel diabaikan. Ubah file DOC ke DOCX terlebih dahulu.", + "presets.document_tables_note": "Dari dokumen (DOCX, DOC, ODT, RTF, HWP, HWPX) atau halaman web (HTML), setiap tabel menjadi lembar tersendiri, dengan angka dan tanggal sebagai nilai asli. Teks di luar tabel diabaikan.", "presets.pdf_compression": "Kompresi", "presets.pdf_compression.None": "Jangan kompres", "presets.pdf_compression.Screen": "Terkecil (gambar 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/it.json b/src/Filee.App/Assets/i18n/it.json index e1cf5b01e..f96dacd3b 100644 --- a/src/Filee.App/Assets/i18n/it.json +++ b/src/Filee.App/Assets/i18n/it.json @@ -297,7 +297,7 @@ "engine.pdf-ps.name": "Writer PostScript (integrato)", "engine.pdf-ps.description": "Converte i PDF in PostScript ed EPS senza Ghostscript: il testo mantiene i suoi font, i disegni restano vettoriali. Le pagine con trasparenze diventano immagini.", "engine.document-tables.name": "Tabelle dei documenti (integrato)", -"engine.document-tables.description": "Trasforma le tabelle dei documenti Word (DOCX) e delle pagine web (HTML, MHTML) in una cartella di lavoro Excel, un foglio ciascuna, con numeri e date come valori veri.", +"engine.document-tables.description": "Trasforma le tabelle dei documenti (DOCX, DOC, ODT, RTF, HWP, HWPX) e delle pagine web (HTML, MHTML) in una cartella di lavoro Excel, un foglio ciascuna, con numeri e date come valori veri.", "engine.email.name": "E-mail (integrato)", "engine.email.description": "E-mail (EML, Outlook MSG) in HTML e testo con mittente, destinatari e data, immagini incluse; gli allegati in un ZIP.\nMSG in EML. Pagine web salvate (MHTML) in un unico file HTML con le loro immagini.", "engine.p7m.name": "File firmati (integrato)", @@ -1114,7 +1114,7 @@ "presets.split_minutes": "minuti per parte", "presets.video_parts_hint": "Copiato così com'è, senza ricodificare: qualità, dimensione e codec non si applicano. Ogni parte finisce su un fotogramma chiave, quindi può durare qualche secondo in più del richiesto.", "presets.pdf_tables_note": "Da un PDF, ogni tabella disegnata con linee diventa un foglio a sé (una tabella che continua nella pagina successiva resta un solo foglio), con numeri e date come valori veri. Il testo fuori dalle tabelle viene omesso.", -"presets.document_tables_note": "Da un documento Word (DOCX) o da una pagina web (HTML), ogni tabella diventa un foglio a sé, con numeri e date come valori veri. Il testo fuori dalle tabelle viene omesso. Converti prima un file DOC in DOCX.", +"presets.document_tables_note": "Da un documento (DOCX, DOC, ODT, RTF, HWP, HWPX) o da una pagina web (HTML), ogni tabella diventa un foglio a sé, con numeri e date come valori veri. Il testo fuori dalle tabelle viene omesso.", "presets.pdf_compression": "Comprimi", "presets.pdf_compression.None": "Non comprimere", "presets.pdf_compression.Screen": "Minima (immagini a 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json index 657dc177a..04cd7e3c1 100644 --- a/src/Filee.App/Assets/i18n/ja.json +++ b/src/Filee.App/Assets/i18n/ja.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "PostScript ライター(組み込み)", "engine.pdf-ps.description": "Ghostscript なしで PDF を PostScript と EPS に変換します。文字はフォントのまま、図形はベクターのままです。透明効果のあるページは画像になります。", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Word 文書(DOCX)と Web ページ(HTML、MHTML)の表を Excel ブックに変換します。表ごとに 1 シートで、数値と日付は実際の値になります。", + "engine.document-tables.description": "文書(DOCX, DOC, ODT, RTF, HWP, HWPX)と Web ページ(HTML、MHTML)の表を Excel ブックに変換します。表ごとに 1 シートで、数値と日付は実際の値になります。", "engine.email.name": "E-mail (built-in)", "engine.email.description": "メール(EML、Outlook MSG)を、差出人、宛先、日付、画像を含めてHTMLとテキストに変換します。添付ファイルはZIPにまとめます。\nMSGからEMLへの変換にも対応します。画像を含む保存済みのWebページ(MHTML)を、1つのHTMLファイルに変換します。", "engine.p7m.name": "Signed files (built-in)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "分ずつ", "presets.video_parts_hint": "再エンコードせずそのままコピーするため、画質・サイズ・コーデックの設定は使われません。各パートはキーフレームで終わるので、指定より数秒長くなることがあります。", "presets.pdf_tables_note": "PDF からは、罫線で描かれた表が 1 つずつシートになり(次のページに続く表は 1 つのシートのまま)、数値と日付は実際の値になります。表の外のテキストは含めません。", - "presets.document_tables_note": "Word 文書(DOCX)や Web ページ(HTML)からは、表が 1 つずつシートになり、数値と日付は実際の値になります。表の外のテキストは含めません。DOC ファイルは先に DOCX に変換してください。", + "presets.document_tables_note": "文書(DOCX, DOC, ODT, RTF, HWP, HWPX)や Web ページ(HTML)からは、表が 1 つずつシートになり、数値と日付は実際の値になります。表の外のテキストは含めません。", "presets.pdf_compression": "圧縮", "presets.pdf_compression.None": "圧縮しない", "presets.pdf_compression.Screen": "最小サイズ(画像72 dpi)", diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json index 9308d6913..0ef98a896 100644 --- a/src/Filee.App/Assets/i18n/ko.json +++ b/src/Filee.App/Assets/i18n/ko.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "PostScript 작성기 (내장)", "engine.pdf-ps.description": "Ghostscript 없이 PDF를 PostScript와 EPS로 바꿔요. 글자는 글꼴 그대로, 도형은 벡터 그대로예요. 투명 효과가 있는 페이지는 그림이 돼요.", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Word 문서(DOCX)와 웹 페이지(HTML, MHTML)의 표를 Excel 통합 문서로 바꿔요. 표마다 시트 하나씩이고, 숫자와 날짜는 실제 값이 돼요.", + "engine.document-tables.description": "문서(DOCX, DOC, ODT, RTF, HWP, HWPX)와 웹 페이지(HTML, MHTML)의 표를 Excel 통합 문서로 바꿔요. 표마다 시트 하나씩이고, 숫자와 날짜는 실제 값이 돼요.", "engine.email.name": "E-mail (built-in)", "engine.email.description": "이메일(EML, Outlook MSG)을 보낸 사람·받는 사람·날짜와 본문 그림까지 담아 HTML·텍스트로 바꾸고, 첨부 파일은 ZIP으로 모아요.\nMSG는 EML로도 바꾸고, 저장한 웹 페이지(MHTML)는 그림까지 담긴 HTML 파일 하나로 바꿔요.", "engine.p7m.name": "Signed files (built-in)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "분씩", "presets.video_parts_hint": "다시 인코딩하지 않고 그대로 복사해서 화질·크기·코덱 설정은 쓰이지 않아요. 각 부분은 키프레임에서 끝나서 정한 것보다 몇 초 길어질 수 있어요.", "presets.pdf_tables_note": "PDF에서는 선으로 그린 표가 하나씩 시트가 되고(다음 페이지로 이어지는 표는 한 시트로 이어져요), 숫자와 날짜는 실제 값이 돼요. 표 밖의 텍스트는 빠져요.", - "presets.document_tables_note": "Word 문서(DOCX)나 웹 페이지(HTML)에서는 표가 하나씩 시트가 되고, 숫자와 날짜는 실제 값이 돼요. 표 밖의 텍스트는 빠져요. DOC 파일은 먼저 DOCX로 바꿔 주세요.", + "presets.document_tables_note": "문서(DOCX, DOC, ODT, RTF, HWP, HWPX)나 웹 페이지(HTML)에서는 표가 하나씩 시트가 되고, 숫자와 날짜는 실제 값이 돼요. 표 밖의 텍스트는 빠져요.", "presets.pdf_compression": "압축", "presets.pdf_compression.None": "압축 안 함", "presets.pdf_compression.Screen": "가장 작게 (이미지 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/mn.json b/src/Filee.App/Assets/i18n/mn.json index 1558d49fc..617befa35 100644 --- a/src/Filee.App/Assets/i18n/mn.json +++ b/src/Filee.App/Assets/i18n/mn.json @@ -297,7 +297,7 @@ "engine.pdf-ps.name": "PostScript бичигч (суурилагдсан)", "engine.pdf-ps.description": "PDF-ийг Ghostscript-гүйгээр PostScript болон EPS болгоно: текст фонтоо хадгалж, зураас вектороороо үлдэнэ. Тунгалаг хэсэгтэй хуудсууд зураг болно.", "engine.document-tables.name": "Баримтын хүснэгт (суурилагдсан)", - "engine.document-tables.description": "Word баримт (DOCX) болон вэб хуудасны (HTML, MHTML) хүснэгтүүдийг тоо болон огноог бодит утгаар нь хадгалан тус бүр нь нэг хуудастай Excel файл болгоно.", + "engine.document-tables.description": "Баримт (DOCX, DOC, ODT, RTF, HWP, HWPX) болон вэб хуудасны (HTML, MHTML) хүснэгтүүдийг тоо болон огноог бодит утгаар нь хадгалан тус бүр нь нэг хуудастай Excel файл болгоно.", "engine.email.name": "Имэйл (суурилагдсан)", "engine.email.description": "Имэйлүүдийг (EML, Outlook MSG) илгээгч, хүлээн авагч, огноо, зургуудын хамт HTML болон текст рүү, хавсралтуудыг ZIP рүү хөрвүүлнэ.\nMSG-г EML рүү. Хадгалсан вэб хуудсыг (MHTML) зургуудынх нь хамт нэг HTML файл болгоно.", "engine.p7m.name": "Гарын үсэгтэй файл (суурилагдсан)", @@ -1114,7 +1114,7 @@ "presets.split_minutes": "нэг хэсгийн минут", "presets.video_parts_hint": "Дахин кодлохгүйгээр байгаагаар нь хуулах тул чанар, хэмжээ болон кодекийн тохиргоо үйлчлэхгүй. Хэсэг бүр тулгуур кадр дээр дуусдаг тул хүссэнээс хэдэн секундээр урт байж болно.", "presets.pdf_tables_note": "PDF-ээс зураасаар зурсан хүснэгт бүр тусдаа ажлын хуудас болно (дараагийн хуудас руу үргэлжилсэн хүснэгт нэг хуудас хэвээр үлдэнэ), тоо болон огноо бодит утгаараа орно. Хүснэгтээс гадуурх текст орхигдоно.", - "presets.document_tables_note": "Word баримт (DOCX) эсвэл вэб хуудаснаас (HTML) хүснэгт бүр тусдаа ажлын хуудас болох ба тоо, огноо бодит утгаараа орно. Хүснэгтээс гадуурх текст орхигдоно. DOC файлыг эхлээд DOCX болгож хөрвүүлнэ үү.", + "presets.document_tables_note": "Баримт (DOCX, DOC, ODT, RTF, HWP, HWPX) эсвэл вэб хуудаснаас (HTML) хүснэгт бүр тусдаа ажлын хуудас болох ба тоо, огноо бодит утгаараа орно. Хүснэгтээс гадуурх текст орхигдоно.", "presets.pdf_compression": "Шахах", "presets.pdf_compression.None": "Шахахгүй", "presets.pdf_compression.Screen": "Хамгийн жижиг (зургууд 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.json index e4887eada..faf50e740 100644 --- a/src/Filee.App/Assets/i18n/ne.json +++ b/src/Filee.App/Assets/i18n/ne.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "PostScript लेखक (भित्री)", "engine.pdf-ps.description": "Ghostscript बिना PDF लाई PostScript र EPS मा बदल्छ: पाठले आफ्नो फन्ट राख्छ, चित्रहरू भेक्टर नै रहन्छन्। पारदर्शिता भएका पृष्ठहरू चित्र बन्छन्।", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Word कागजात (DOCX) र वेब पेज (HTML, MHTML) का तालिकाहरूलाई Excel वर्कबुकमा बदल्छ, हरेक तालिका एउटा शिट, अङ्क र मितिहरू वास्तविक मानका रूपमा।", + "engine.document-tables.description": "कागजात (DOCX, DOC, ODT, RTF, HWP, HWPX) र वेब पेज (HTML, MHTML) का तालिकाहरूलाई Excel वर्कबुकमा बदल्छ, हरेक तालिका एउटा शिट, अङ्क र मितिहरू वास्तविक मानका रूपमा।", "engine.email.name": "E-mail (built-in)", "engine.email.description": "इमेलहरू (EML, Outlook MSG) लाई प्रेषक, प्राप्तकर्ता र मितिसहित HTML र पाठमा रूपान्तरण गर्दछ, तस्बिरहरूसहित; संलग्नकहरू ZIP मा।\nMSG लाई EML मा। सुरक्षित वेब पृष्ठहरू (MHTML) लाई तिनका तस्बिरहरूसहित एउटै HTML फाइलमा।", "engine.p7m.name": "Signed files (built-in)", @@ -1111,7 +1111,7 @@ "presets.split_minutes": "मिनेट प्रति भाग", "presets.video_parts_hint": "फेरि इन्कोड नगरी जस्तो छ त्यस्तै कपी हुन्छ, त्यसैले गुणस्तर, आकार र कोडेक सेटिङ लागू हुँदैनन्। हरेक भाग की फ्रेममा सकिन्छ, त्यसैले तोकिएभन्दा केही सेकेन्ड लामो हुन सक्छ।", "presets.pdf_tables_note": "PDF बाट, रेखाले बनाइएको हरेक तालिका आफ्नै शिट बन्छ (अर्को पेजमा जारी तालिका एउटै शिट रहन्छ), र अङ्क र मितिहरू वास्तविक मान बन्छन्। तालिकाबाहिरको पाठ छोडिन्छ।", - "presets.document_tables_note": "Word कागजात (DOCX) वा वेब पेज (HTML) बाट, हरेक तालिका आफ्नै शिट बन्छ, र अङ्क र मितिहरू वास्तविक मान बन्छन्। तालिकाबाहिरको पाठ छोडिन्छ। DOC फाइललाई पहिले DOCX मा बदल्नुहोस्।", + "presets.document_tables_note": "कागजात (DOCX, DOC, ODT, RTF, HWP, HWPX) वा वेब पेज (HTML) बाट, हरेक तालिका आफ्नै शिट बन्छ, र अङ्क र मितिहरू वास्तविक मान बन्छन्। तालिकाबाहिरको पाठ छोडिन्छ।", "presets.pdf_compression": "कम्प्रेस गर्नुहोस्", "presets.pdf_compression.None": "कम्प्रेस नगर्नुहोस्", "presets.pdf_compression.Screen": "सबैभन्दा सानो (तस्बिरहरू ७२ dpi मा)", diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.json index 407170ba7..3d0705eec 100644 --- a/src/Filee.App/Assets/i18n/pl.json +++ b/src/Filee.App/Assets/i18n/pl.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "Generator PostScript (wbudowany)", "engine.pdf-ps.description": "Zamienia PDF na PostScript i EPS bez Ghostscripta: tekst zachowuje czcionki, rysunki pozostają wektorowe. Strony z przezroczystością stają się obrazami.", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Zamienia tabele z dokumentów Word (DOCX) i stron internetowych (HTML, MHTML) na skoroszyt Excel, każdą na osobnym arkuszu, z liczbami i datami jako prawdziwymi wartościami.", + "engine.document-tables.description": "Zamienia tabele z dokumentów (DOCX, DOC, ODT, RTF, HWP, HWPX) i stron internetowych (HTML, MHTML) na skoroszyt Excel, każdą na osobnym arkuszu, z liczbami i datami jako prawdziwymi wartościami.", "engine.email.name": "E-mail (built-in)", "engine.email.description": "E-maile (EML, Outlook MSG) do HTML i tekstu z nadawcą, odbiorcami i datą, razem z obrazami; załączniki do ZIP.\nMSG do EML. Zapisane strony internetowe (MHTML) do jednego pliku HTML razem z obrazami.", "engine.p7m.name": "Signed files (built-in)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "minut na część", "presets.video_parts_hint": "Kopiowane bez zmian, bez ponownego kodowania, więc jakość, rozmiar i kodek nie mają zastosowania. Każda część kończy się na klatce kluczowej, więc może trwać kilka sekund dłużej, niż wybrano.", "presets.pdf_tables_note": "Z PDF każda tabela narysowana liniami trafia na osobny arkusz (tabela ciągnąca się na następną stronę zostaje jednym arkuszem), a liczby i daty stają się prawdziwymi wartościami. Tekst poza tabelami jest pomijany.", - "presets.document_tables_note": "Z dokumentu Word (DOCX) lub strony internetowej (HTML) każda tabela trafia na osobny arkusz, a liczby i daty stają się prawdziwymi wartościami. Tekst poza tabelami jest pomijany. Plik DOC najpierw przekonwertuj na DOCX.", + "presets.document_tables_note": "Z dokumentu (DOCX, DOC, ODT, RTF, HWP, HWPX) lub strony internetowej (HTML) każda tabela trafia na osobny arkusz, a liczby i daty stają się prawdziwymi wartościami. Tekst poza tabelami jest pomijany.", "presets.pdf_compression": "Kompresja", "presets.pdf_compression.None": "Bez kompresji", "presets.pdf_compression.Screen": "Najmniejszy (obrazy w 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.json index 6fe00b692..8c4669138 100644 --- a/src/Filee.App/Assets/i18n/pt-BR.json +++ b/src/Filee.App/Assets/i18n/pt-BR.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "Gerador PostScript (mecanismo integrado)", "engine.pdf-ps.description": "Converte PDFs em PostScript e EPS sem o Ghostscript: o texto mantém as fontes e os desenhos continuam vetoriais. Páginas com transparência viram imagens.", "engine.document-tables.name": "Tabelas de documentos (mecanismo integrado)", - "engine.document-tables.description": "Transforma as tabelas de documentos do Word (DOCX) e de páginas da web (HTML, MHTML) em uma pasta de trabalho do Excel, uma planilha por tabela, com números e datas como valores reais.", + "engine.document-tables.description": "Transforma as tabelas de documentos (DOCX, DOC, ODT, RTF, HWP, HWPX) e de páginas da web (HTML, MHTML) em uma pasta de trabalho do Excel, uma planilha por tabela, com números e datas como valores reais.", "engine.email.name": "E-mail (mecanismo integrado)", "engine.email.description": "E-mails (EML, MSG do Outlook) para HTML e texto, com remetente, destinatários e data, imagens incluídas; anexos num ZIP.\nMSG para EML. Páginas da web salvas (MHTML) para um único arquivo HTML com as imagens.", "engine.p7m.name": "Arquivos assinados (mecanismo integrado)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "minutos por parte", "presets.video_parts_hint": "Copiado como está, sem codificar de novo, então qualidade, tamanho e codec não se aplicam. Cada parte termina num quadro-chave, por isso pode durar alguns segundos a mais do que o pedido.", "presets.pdf_tables_note": "De um PDF, cada tabela desenhada com linhas vira uma planilha própria (uma tabela que continua na página seguinte fica em uma só planilha), com números e datas como valores reais. O texto fora das tabelas fica de fora.", - "presets.document_tables_note": "De um documento do Word (DOCX) ou de uma página da web (HTML), cada tabela vira uma planilha própria, com números e datas como valores reais. O texto fora das tabelas fica de fora. Converta antes um arquivo DOC para DOCX.", + "presets.document_tables_note": "De um documento (DOCX, DOC, ODT, RTF, HWP, HWPX) ou de uma página da web (HTML), cada tabela vira uma planilha própria, com números e datas como valores reais. O texto fora das tabelas fica de fora.", "presets.pdf_compression": "Comprimir", "presets.pdf_compression.None": "Não comprimir", "presets.pdf_compression.Screen": "Menor (imagens a 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.json index 42ecf92e7..c68cb36ad 100644 --- a/src/Filee.App/Assets/i18n/pt-PT.json +++ b/src/Filee.App/Assets/i18n/pt-PT.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "Gerador PostScript (motor integrado)", "engine.pdf-ps.description": "Converte PDFs em PostScript e EPS sem o Ghostscript: o texto mantém os tipos de letra e os desenhos continuam vetoriais. As páginas com transparência passam a imagens.", "engine.document-tables.name": "Tabelas de documentos (motor integrado)", - "engine.document-tables.description": "Converte as tabelas de documentos do Word (DOCX) e de páginas web (HTML, MHTML) num livro do Excel, com uma folha por tabela e valores numéricos e de data.", + "engine.document-tables.description": "Converte as tabelas de documentos (DOCX, DOC, ODT, RTF, HWP, HWPX) e de páginas web (HTML, MHTML) num livro do Excel, com uma folha por tabela e valores numéricos e de data.", "engine.email.name": "E-mail (motor integrado)", "engine.email.description": "E-mails (EML, MSG do Outlook) para HTML e texto com remetente, destinatários, data e imagens; anexos para ZIP.\nMSG para EML. Páginas Web guardadas (MHTML) para um ficheiro HTML com as respetivas imagens.", "engine.p7m.name": "Ficheiros assinados (motor integrado)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "minutos por parte", "presets.video_parts_hint": "Copiado tal como está, sem nova codificação, por isso a qualidade, o tamanho e o codec não se aplicam. Cada parte termina num fotograma-chave, pelo que pode durar alguns segundos a mais do que o pedido.", "presets.pdf_tables_note": "A partir de um PDF, cada tabela delimitada por linhas ocupa uma folha (uma tabela que continua na página seguinte mantém-se na mesma folha), com valores numéricos e de data. O texto fora das tabelas é omitido.", - "presets.document_tables_note": "A partir de um documento do Word (DOCX) ou de uma página web (HTML), cada tabela ocupa uma folha, com valores numéricos e de data. O texto fora das tabelas é omitido. Converta primeiro um ficheiro DOC para DOCX.", + "presets.document_tables_note": "A partir de um documento (DOCX, DOC, ODT, RTF, HWP, HWPX) ou de uma página web (HTML), cada tabela ocupa uma folha, com valores numéricos e de data. O texto fora das tabelas é omitido.", "presets.pdf_compression": "Comprimir", "presets.pdf_compression.None": "Não comprimir", "presets.pdf_compression.Screen": "Tamanho mínimo (imagens a 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json index 1c05484fd..303a8c356 100644 --- a/src/Filee.App/Assets/i18n/ru.json +++ b/src/Filee.App/Assets/i18n/ru.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "Генератор PostScript (встроенный)", "engine.pdf-ps.description": "Преобразует PDF в PostScript и EPS без Ghostscript: текст сохраняет шрифты, рисунки остаются векторными. Страницы с прозрачностью становятся изображениями.", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "Превращает таблицы документов Word (DOCX) и веб-страниц (HTML, MHTML) в книгу Excel — по листу на таблицу, с числами и датами как настоящими значениями.", + "engine.document-tables.description": "Превращает таблицы документов (DOCX, DOC, ODT, RTF, HWP, HWPX) и веб-страниц (HTML, MHTML) в книгу Excel — по листу на таблицу, с числами и датами как настоящими значениями.", "engine.email.name": "E-mail (built-in)", "engine.email.description": "Письма (EML, Outlook MSG) — в HTML и текст с отправителем, получателями и датой, вместе с картинками; вложения — в ZIP.\nMSG в EML. Сохранённые веб-страницы (MHTML) — в один HTML-файл с картинками.", "engine.p7m.name": "Signed files (built-in)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "минут в части", "presets.video_parts_hint": "Копируется как есть, без перекодирования, поэтому качество, размер и кодек не применяются. Каждая часть заканчивается на ключевом кадре, поэтому может быть на несколько секунд длиннее заданного.", "presets.pdf_tables_note": "Из PDF каждая таблица, нарисованная линиями, становится отдельным листом (таблица, продолжающаяся на следующей странице, остаётся одним листом), а числа и даты — настоящими значениями. Текст вне таблиц не сохраняется.", - "presets.document_tables_note": "Из документа Word (DOCX) или веб-страницы (HTML) каждая таблица становится отдельным листом, а числа и даты — настоящими значениями. Текст вне таблиц не сохраняется. Файл DOC сначала преобразуйте в DOCX.", + "presets.document_tables_note": "Из документа (DOCX, DOC, ODT, RTF, HWP, HWPX) или веб-страницы (HTML) каждая таблица становится отдельным листом, а числа и даты — настоящими значениями. Текст вне таблиц не сохраняется.", "presets.pdf_compression": "Сжатие", "presets.pdf_compression.None": "Не сжимать", "presets.pdf_compression.Screen": "Минимальный размер (картинки 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.json index 00c42fb13..d777d4e4b 100644 --- a/src/Filee.App/Assets/i18n/th.json +++ b/src/Filee.App/Assets/i18n/th.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "ตัวเขียน PostScript (ในตัว)", "engine.pdf-ps.description": "แปลง PDF เป็น PostScript และ EPS โดยไม่ต้องใช้ Ghostscript ข้อความใช้ฟอนต์เดิม ภาพวาดยังเป็นเวกเตอร์ หน้าที่มีความโปร่งใสจะกลายเป็นรูปภาพ", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "แปลงตารางในเอกสาร Word (DOCX) และหน้าเว็บ (HTML, MHTML) เป็นสมุดงาน Excel ตารางละหนึ่งชีต โดยตัวเลขและวันที่เป็นค่าจริง", + "engine.document-tables.description": "แปลงตารางในเอกสาร (DOCX, DOC, ODT, RTF, HWP, HWPX) และหน้าเว็บ (HTML, MHTML) เป็นสมุดงาน Excel ตารางละหนึ่งชีต โดยตัวเลขและวันที่เป็นค่าจริง", "engine.email.name": "E-mail (built-in)", "engine.email.description": "อีเมล (EML, Outlook MSG) เป็น HTML และข้อความ พร้อมผู้ส่ง ผู้รับ วันที่ และรูปภาพ แนบไฟล์ลงใน ZIP\nMSG เป็น EML บันทึกหน้าเว็บ (MHTML) เป็นไฟล์ HTML ไฟล์เดียวพร้อมรูปภาพ", "engine.p7m.name": "Signed files (built-in)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "นาทีต่อส่วน", "presets.video_parts_hint": "คัดลอกตามเดิมโดยไม่เข้ารหัสใหม่ จึงไม่ใช้การตั้งค่าคุณภาพ ขนาด และโคเดก แต่ละส่วนจะจบที่คีย์เฟรม จึงอาจยาวกว่าที่ตั้งไว้สองสามวินาที", "presets.pdf_tables_note": "จาก PDF ตารางที่วาดด้วยเส้นแต่ละตารางจะเป็นชีตของตัวเอง (ตารางที่ต่อไปยังหน้าถัดไปยังคงเป็นชีตเดียว) และตัวเลขกับวันที่จะเป็นค่าจริง ข้อความนอกตารางจะถูกละไว้", - "presets.document_tables_note": "จากเอกสาร Word (DOCX) หรือหน้าเว็บ (HTML) ตารางแต่ละตารางจะเป็นชีตของตัวเอง และตัวเลขกับวันที่จะเป็นค่าจริง ข้อความนอกตารางจะถูกละไว้ ไฟล์ DOC ให้แปลงเป็น DOCX ก่อน", + "presets.document_tables_note": "จากเอกสาร (DOCX, DOC, ODT, RTF, HWP, HWPX) หรือหน้าเว็บ (HTML) ตารางแต่ละตารางจะเป็นชีตของตัวเอง และตัวเลขกับวันที่จะเป็นค่าจริง ข้อความนอกตารางจะถูกละไว้", "presets.pdf_compression": "บีบอัด", "presets.pdf_compression.None": "ไม่บีบอัด", "presets.pdf_compression.Screen": "ขนาดเล็กที่สุด (ภาพที่ 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.json index 99a46de17..55ad5284b 100644 --- a/src/Filee.App/Assets/i18n/tr.json +++ b/src/Filee.App/Assets/i18n/tr.json @@ -298,7 +298,7 @@ "engine.pdf-ps.name": "PostScript oluşturucu (yerleşik)", "engine.pdf-ps.description": "PDF'leri Ghostscript olmadan PostScript ve EPS'ye dönüştürür: metin yazı tiplerini korur, çizimler vektör kalır. Saydamlık içeren sayfalar resme dönüşür.", "engine.document-tables.name": "Belge tabloları (yerleşik)", - "engine.document-tables.description": "Word belgelerindeki (DOCX) ve web sayfalarındaki (HTML, MHTML) tabloları, her biri ayrı çalışma sayfasında olacak şekilde Excel'e dönüştürür; sayı ve tarihler gerçek değerler olarak tutulur.", + "engine.document-tables.description": "Belgelerdeki (DOCX, DOC, ODT, RTF, HWP, HWPX) ve web sayfalarındaki (HTML, MHTML) tabloları, her biri ayrı çalışma sayfasında olacak şekilde Excel'e dönüştürür; sayı ve tarihler gerçek değerler olarak tutulur.", "engine.email.name": "E-posta (yerleşik)", "engine.email.description": "E-postaları (EML, Outlook MSG) gönderen, alıcı, tarih ve görselleriyle HTML ve metne; ekleri ZIP'e dönüştürür.\nMSG → EML. Kaydedilmiş web sayfalarını (MHTML) görselleriyle tek bir HTML dosyasına dönüştürür.", "engine.p7m.name": "İmzalı dosyalar (yerleşik)", @@ -1115,7 +1115,7 @@ "presets.split_minutes": "dakikada bir parça", "presets.video_parts_hint": "Yeniden kodlanmadan olduğu gibi kopyalanır; kalite, boyut ve codec ayarları uygulanmaz. Her parça bir anahtar karede biter, bu yüzden istenenden birkaç saniye uzun olabilir.", "presets.pdf_tables_note": "PDF'deki çizgilerle oluşturulmuş her tablo ayrı çalışma sayfası olur (sonraki sayfada devam eden tablo aynı çalışma sayfasında kalır); sayı ve tarihler gerçek değerler olarak tutulur. Tabloların dışındaki metin çıkarılır.", - "presets.document_tables_note": "Word belgesindeki (DOCX) veya web sayfasındaki (HTML) her tablo ayrı çalışma sayfası olur; sayı ve tarihler gerçek değerler olarak tutulur. Tabloların dışındaki metin çıkarılır. DOC dosyasını önce DOCX'e dönüştürün.", + "presets.document_tables_note": "Bir belgedeki (DOCX, DOC, ODT, RTF, HWP, HWPX) veya web sayfasındaki (HTML) her tablo ayrı çalışma sayfası olur; sayı ve tarihler gerçek değerler olarak tutulur. Tabloların dışındaki metin çıkarılır.", "presets.pdf_compression": "Sıkıştır", "presets.pdf_compression.None": "Sıkıştırma", "presets.pdf_compression.Screen": "En küçük (görseller 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/vi.json b/src/Filee.App/Assets/i18n/vi.json index 22ff52c33..413231bc4 100644 --- a/src/Filee.App/Assets/i18n/vi.json +++ b/src/Filee.App/Assets/i18n/vi.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "Trình ghi PostScript (tích hợp)", "engine.pdf-ps.description": "Chuyển PDF sang PostScript và EPS mà không cần Ghostscript: chữ giữ nguyên phông, hình vẽ vẫn là vector. Các trang có độ trong suốt sẽ thành ảnh.", "engine.document-tables.name": "Bảng tài liệu (tích hợp)", - "engine.document-tables.description": "Chuyển các bảng trong tài liệu Word (DOCX) và trang web (HTML, MHTML) thành sổ làm việc Excel, mỗi bảng một trang tính, số và ngày là giá trị thực.", + "engine.document-tables.description": "Chuyển các bảng trong tài liệu (DOCX, DOC, ODT, RTF, HWP, HWPX) và trang web (HTML, MHTML) thành sổ làm việc Excel, mỗi bảng một trang tính, số và ngày là giá trị thực.", "engine.email.name": "Email (tích hợp)", "engine.email.description": "Email (EML, Outlook MSG) sang HTML và văn bản kèm người gửi, người nhận và ngày, có cả ảnh; tệp đính kèm vào một tệp ZIP.\nMSG sang EML. Trang web đã lưu (MHTML) sang một tệp HTML kèm ảnh.", "engine.p7m.name": "Tệp đã ký (tích hợp)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "phút mỗi phần", "presets.video_parts_hint": "Được sao chép nguyên trạng, không mã hóa lại, nên chất lượng, kích thước và codec không áp dụng. Mỗi phần kết thúc ở một khung hình chính, nên có thể dài hơn vài giây so với yêu cầu.", "presets.pdf_tables_note": "Từ PDF, mỗi bảng kẻ bằng đường kẻ thành một trang tính riêng (bảng kéo dài sang trang sau vẫn là một trang tính), số và ngày là giá trị thực. Văn bản bên ngoài bảng bị bỏ qua.", - "presets.document_tables_note": "Từ tài liệu Word (DOCX) hoặc trang web (HTML), mỗi bảng thành một trang tính riêng, số và ngày là giá trị thực. Văn bản bên ngoài bảng bị bỏ qua. Hãy chuyển tệp DOC sang DOCX trước.", + "presets.document_tables_note": "Từ tài liệu (DOCX, DOC, ODT, RTF, HWP, HWPX) hoặc trang web (HTML), mỗi bảng thành một trang tính riêng, số và ngày là giá trị thực. Văn bản bên ngoài bảng bị bỏ qua.", "presets.pdf_compression": "Nén", "presets.pdf_compression.None": "Không nén", "presets.pdf_compression.Screen": "Nhỏ nhất (ảnh 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.json index c69d574b9..396970caf 100644 --- a/src/Filee.App/Assets/i18n/zh-CN.json +++ b/src/Filee.App/Assets/i18n/zh-CN.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "PostScript 生成器(内置)", "engine.pdf-ps.description": "无需 Ghostscript 即可把 PDF 转换为 PostScript 和 EPS:文字保留字体,图形保持矢量。含透明效果的页面会变成图片。", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "把 Word 文档(DOCX)和网页(HTML、MHTML)中的表格转换为 Excel 工作簿,每个表格一个工作表,数字和日期为真正的数值。", + "engine.document-tables.description": "把文档(DOCX, DOC, ODT, RTF, HWP, HWPX)和网页(HTML、MHTML)中的表格转换为 Excel 工作簿,每个表格一个工作表,数字和日期为真正的数值。", "engine.email.name": "E-mail (built-in)", "engine.email.description": "将电子邮件(EML、Outlook MSG)连同发件人、收件人、日期和图片转为 HTML 和文本,附件打包为 ZIP。\nMSG 也可转为 EML。保存的网页(MHTML)可连同图片转为单个 HTML 文件。", "engine.p7m.name": "Signed files (built-in)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "分钟一段", "presets.video_parts_hint": "按原样复制,不重新编码,所以画质、大小和编码设置不起作用。每段在关键帧处结束,可能比设定的长几秒。", "presets.pdf_tables_note": "从 PDF 转换时,每个用线条画出的表格成为一个工作表(续到下一页的表格仍为一个工作表),数字和日期会成为真正的数值。表格外的文字不包含在内。", - "presets.document_tables_note": "从 Word 文档(DOCX)或网页(HTML)转换时,每个表格成为一个工作表,数字和日期会成为真正的数值。表格外的文字不包含在内。DOC 文件请先转换为 DOCX。", + "presets.document_tables_note": "从文档(DOCX, DOC, ODT, RTF, HWP, HWPX)或网页(HTML)转换时,每个表格成为一个工作表,数字和日期会成为真正的数值。表格外的文字不包含在内。", "presets.pdf_compression": "压缩", "presets.pdf_compression.None": "不压缩", "presets.pdf_compression.Screen": "最小(图片 72 dpi)", diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.json index 13efccba7..b2f47e05e 100644 --- a/src/Filee.App/Assets/i18n/zh-TW.json +++ b/src/Filee.App/Assets/i18n/zh-TW.json @@ -305,7 +305,7 @@ "engine.pdf-ps.name": "PostScript 產生器(內建)", "engine.pdf-ps.description": "不需 Ghostscript 即可把 PDF 轉成 PostScript 與 EPS:文字保留字型,圖形維持向量。含透明效果的頁面會變成圖片。", "engine.document-tables.name": "Document tables (built-in)", - "engine.document-tables.description": "把 Word 文件(DOCX)和網頁(HTML、MHTML)中的表格轉成 Excel 活頁簿,每個表格一個工作表,數字與日期為真正的數值。", + "engine.document-tables.description": "把文件(DOCX, DOC, ODT, RTF, HWP, HWPX)和網頁(HTML、MHTML)中的表格轉成 Excel 活頁簿,每個表格一個工作表,數字與日期為真正的數值。", "engine.email.name": "E-mail (built-in)", "engine.email.description": "將電子郵件(EML、Outlook MSG)連同發件人、收件人、日期和圖片轉為 HTML 和文字,附件打包為 ZIP。\nMSG 也可轉為 EML。儲存的網頁(MHTML)可連同圖片轉為單個 HTML 檔案。", "engine.p7m.name": "Signed files (built-in)", @@ -1130,7 +1130,7 @@ "presets.split_minutes": "分鐘一段", "presets.video_parts_hint": "照原樣複製,不重新編碼,所以畫質、大小和編碼設定不會套用。每段在關鍵影格處結束,可能比設定的長幾秒。", "presets.pdf_tables_note": "從 PDF 轉換時,每個以線條繪製的表格會成為一個工作表(延續到下一頁的表格仍為一個工作表),數字與日期會成為真正的數值。表格外的文字不包含在內。", - "presets.document_tables_note": "從 Word 文件(DOCX)或網頁(HTML)轉換時,每個表格會成為一個工作表,數字與日期會成為真正的數值。表格外的文字不包含在內。DOC 檔案請先轉換為 DOCX。", + "presets.document_tables_note": "從文件(DOCX, DOC, ODT, RTF, HWP, HWPX)或網頁(HTML)轉換時,每個表格會成為一個工作表,數字與日期會成為真正的數值。表格外的文字不包含在內。", "presets.pdf_compression": "壓縮", "presets.pdf_compression.None": "不壓縮", "presets.pdf_compression.Screen": "最小(圖片 72 dpi)", diff --git a/src/Filee.Engines/Office/DocumentTablesConverter.cs b/src/Filee.Engines/Office/DocumentTablesConverter.cs index 65871b454..82744b665 100644 --- a/src/Filee.Engines/Office/DocumentTablesConverter.cs +++ b/src/Filee.Engines/Office/DocumentTablesConverter.cs @@ -1,14 +1,18 @@ -// DOCX / HTML → XLSX (#1016): the tables of a Word document or a web page, one sheet each, so they can be calculated -// with. The readers that turn these files into HWPX and DOCX already know the rows, merged cells and column widths; -// text outside the tables is left out. +// Word, OpenDocument, RTF, 한글 and web pages → XLSX (#1016): the tables of a DOCX, DOC, ODT, RTF, HWP, HWPX or HTML +// file, one sheet each, so they can be calculated with. The readers that turn these files into HWPX and DOCX already +// know the rows, merged cells and column widths; text outside the tables is left out. // // - Every table is its own sheet ("Table 1", "Table 2" …), a table inside a cell too. Unlike PDF → XLSX nothing is // joined: a Word table that runs over a page break is one table already, and sections aren't pages. // - A table without any text (a web page's layout table around another one) gets no sheet. -// - Numbers and dates become real values as in PDF → XLSX (TableSheets). Cells keep their own borders and shading; -// leading header rows (DOCX "repeat as header row", HTML or all- rows) stay in view while scrolling. +// - Numbers and dates become real values as in PDF → XLSX (TableSheets). Cells keep their own borders, shading and +// alignment; leading header rows (Word "repeat as header row", 한글 "제목 셀", HTML or all- rows) stay in +// view while scrolling. // - A file without tables fails with a message instead of an empty workbook. -// - DOC isn't read here: it needs LibreOffice, and this edge is direct only (see Edges). +// - 한글 files are read as the renderer reads them (ReadHangul): an equation in a cell is its text ("40"), not its +// script ("40~~~"), so a number typed as an equation is a number. +// - DOC (and .dot, .wps) is read by the built-in DOC reader; a Word 6.0 / 95 or Works file it doesn't read is named by +// FirstUnsupported, as on the other routes from DOC (the edges are Checked). using Filee.Core.Conversion; using Filee.Engines.Hwp.Hwpx; @@ -17,11 +21,15 @@ namespace Filee.Engines.Office; -/// DOCX / HTML → XLSX: each table of the document on its own sheet. -public sealed class DocumentTablesConverter : IConverter +/// Documents and web pages → XLSX: each table of the document on its own sheet. +public sealed class DocumentTablesConverter : IConverter, ICapabilityCheck { - /// Word documents (and their macro and template variants), web pages and saved web pages. - private static readonly string[] Sources = ["docx", "docm", "dotx", "dotm", "html", "mhtml"]; + /// + /// Word documents (and their macro and template variants, Word 97–2003 and WPS), OpenDocument text, RTF, 한글 files, + /// web pages and saved web pages. + /// + private static readonly string[] Sources = + ["docx", "docm", "dotx", "dotm", .. Hwp.Hwpx.Doc.DocReader.Formats, "odt", "rtf", "hwp", "hwpx", "html", "mhtml"]; public string Id => "document-tables"; public string DisplayName => "Document tables (built-in)"; @@ -33,13 +41,20 @@ public sealed class DocumentTablesConverter : IConverter /// workbook of a document's tables must not go on to JSON or XML. /// public IReadOnlyList Edges { get; } = - [.. Sources.SelectMany(from => new[] { "xlsx", "ods", "csv" }.Select(to => new ConversionEdge(from, to, DirectOnly: true)))]; + [.. Sources.SelectMany(from => new[] { "xlsx", "ods", "csv" } + .Select(to => new ConversionEdge(from, to, DirectOnly: true, Checked: DocumentReaders.Checked(from))))]; + + public EngineStatus GetStatus() => + EngineStatus.Available("DOCX, DOC, ODT, RTF, HWP, HWPX, HTML, MHTML → XLSX, ODS, CSV", EngineVersions.BuiltIn); - public EngineStatus GetStatus() => EngineStatus.Available("DOCX, HTML, MHTML → XLSX, ODS, CSV", 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) { - var document = await DocumentReaders.ReadAsync(step.InputPath, step.From, step.WorkDirectory, cancellationToken); + var document = step.From is "hwp" or "hwpx" + ? await Task.Run(() => ReadHangul(step.InputPath, step.From, step.WorkDirectory), cancellationToken) + : await DocumentReaders.ReadAsync(step.InputPath, step.From, step.WorkDirectory, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); progress?.Report(0.7); var book = Build(document); @@ -51,14 +66,54 @@ public async Task> ConvertAsync(ConversionStep step, IProg return outputs; } + /// + /// A 한글 file read as the renderer reads it: its equations as equations, so a number typed as an equation (common + /// in 한글 tables) reads as that number rather than as its script ("40~~~"). + /// + private static HDocument ReadHangul(string path, string format, string workDirectory) + { + var media = Path.Combine(workDirectory, $"media-{Guid.NewGuid():N}"); + return format == "hwp" ? Hwp.Binary.HwpReader.ReadForLayout(path, media) : HwpxReader.ReadForLayout(path, media); + } + + /// Rows of a worksheet (Excel's limit): a longer table can't be one sheet. + internal const int MaxSheetRows = 1_048_576; + + /// + /// Sheets of one workbook. A document may hold up to tables, far + /// more than a workbook should get as tabs: Excel opens a few thousand sheets slowly. + /// + internal const int MaxSheets = 10_000; + /// The document's tables as sheets, in reading order: one per table that has any text. + /// + /// The tables need more sheets, rows or cells than a workbook takes (): counted + /// before anything is written, so a span over thousands of rows and columns is refused at once. + /// internal static Workbook Build(HDocument document) { - var book = new Workbook(); + var tables = new List(); + long cells = 0; foreach (var table in Tables(document.Sections.SelectMany(s => s.Blocks))) { if (!table.Rows.SelectMany(r => r.Cells).Any(c => TableSheets.Text(c).Length > 0)) continue; + if (tables.Count == MaxSheets) + throw Documents.DocumentLimits.Exceeded($"The document has more than {Documents.DocumentLimits.Count(MaxSheets)} tables, one sheet each."); + if (table.Rows.Count > MaxSheetRows) + throw Documents.DocumentLimits.Exceeded($"A table has more than {Documents.DocumentLimits.Count(MaxSheetRows)} rows."); + // Every position a cell covers is written (a merged area keeps its borders); the whole workbook gets the + // budget of one table. + cells += TableSheets.Positions(table); + if (cells > HwpxWriter.MaxTableCells) + throw Documents.DocumentLimits.Exceeded( + $"The tables have more than {Documents.DocumentLimits.Count(HwpxWriter.MaxTableCells)} cells (rows × columns) together."); + tables.Add(table); + } + + var book = new Workbook(); + foreach (var table in tables) + { var sheet = new Worksheet($"Table {book.Sheets.Count + 1}"); TableSheets.SetColumnWidths(sheet, table); TableSheets.Place(sheet, table, 0, 0, drawn: false); @@ -71,15 +126,27 @@ internal static Workbook Build(HDocument document) return book; } - /// Every table with rows, depth first: a table, then the tables inside its cells. + /// + /// Every table with rows, depth first: a table, then the tables inside its cells. Walked with a stack of its own, + /// so the depth the readers allow () costs no nested iterators. + /// private static IEnumerable Tables(IEnumerable blocks) { - foreach (var table in blocks.OfType()) + var pending = new Stack>(); + pending.Push(blocks.OfType().GetEnumerator()); + while (pending.Count > 0) { + var current = pending.Peek(); + if (!current.MoveNext()) + { + current.Dispose(); + pending.Pop(); + continue; + } + var table = current.Current; if (table.Rows.Count > 0) yield return table; - foreach (var nested in Tables(table.Rows.SelectMany(r => r.Cells).SelectMany(c => c.Blocks))) - yield return nested; + pending.Push(table.Rows.SelectMany(r => r.Cells).SelectMany(c => c.Blocks).OfType().GetEnumerator()); } } } diff --git a/src/Filee.Engines/Office/Sheets/TableSheets.cs b/src/Filee.Engines/Office/Sheets/TableSheets.cs index c2f29bf50..ad9b05d8a 100644 --- a/src/Filee.Engines/Office/Sheets/TableSheets.cs +++ b/src/Filee.Engines/Office/Sheets/TableSheets.cs @@ -2,6 +2,8 @@ // DOCX / HTML → XLSX (DocumentTablesConverter, #1016). // // - Merged cells stay merged; the rest of a merged area keeps the borders, as Excel draws them from every cell. +// - A cell is bold or italic when all its text is, and centred or right-aligned when all its paragraphs are; left and +// justified text keeps Excel's own alignment (text to the left, numbers to the right). // - Cells that read as numbers ("1,234.50", "(120)", "$9.99"), percentages ("12.5%") or dates with the year first // ("2026-10-23", "2026. 10. 23.") become real numbers and dates, in a number format that shows them as the document // did, so they can be calculated with. Anything else stays text: numbers with a leading zero (codes, phone numbers), @@ -123,6 +125,19 @@ private static int DisplayWidth(string line) } } + /// + /// The grid positions the table's cells cover (rows × columns of every cell), which writes one + /// cell each. + /// + /// The table is larger than takes. + public static long Positions(HTable table) + { + long positions = 0; + foreach (var (_, _, _, rows, columns) in Grid(table)) + positions += (long)rows * columns; + return positions; + } + /// /// Writes the table's rows from on, leaving out its first ; returns /// the next free row. @@ -146,11 +161,15 @@ public static int Place(Worksheet sheet, HTable table, int top, int skip, bool d } var next = top + Math.Max(0, table.Rows.Count - skip); var defaults = table.Borders ?? HBorders.Grid; + var merged = new List<(string Text, int Row, int Column, int Rows, int Columns)>(); foreach (var (cell, row, column, rows, columns) in Grid(table, skip)) { var style = StyleOf(cell, drawn ? null : cell.Borders ?? defaults); var at = top + row; - sheet.Set(at, column, Shown(Text(cell), style)); + var text = Text(cell); + sheet.Set(at, column, Shown(text, style)); + if (!drawn && (rows > 1 || columns > 1) && text.Length > 0) + merged.Add((text, at, column, rows, columns)); for (var r = at; r < at + rows; r++) for (var c = column; c < column + columns; c++) if (r != at || c != column) @@ -158,26 +177,78 @@ public static int Place(Worksheet sheet, HTable table, int top, int skip, bool d if (rows > 1 || columns > 1) sheet.Merges.Add(new CellRange(at, column, at + rows - 1, column + columns - 1)); } + foreach (var (text, row, column, rows, columns) in merged) + FitMergedRows(sheet, text, row, column, rows, columns); return next; } + /// Points of one line of Excel's default font (Calibri 11), its standard row height. + private const double LineHeight = 15; + + /// + /// Excel and Calc fit a row to the wrapped text of its cells, but not to a merged cell's: the rows under a merged + /// cell whose text takes more lines than they show get the height of those lines (shared out evenly), so the text + /// isn't cut off. Lines are counted with the merged columns' width in characters, as counts. + /// + private static void FitMergedRows(Worksheet sheet, string text, int row, int column, int rows, int columns) + { + var width = 0.0; + for (var c = column; c < column + columns; c++) + width += sheet.ColumnWidth(c); + var perLine = Math.Max(1, (int)Math.Floor(width - 1)); + var lines = text.Split('\n').Sum(line => Math.Max(1, (DisplayWidth(line.Replace("\t", " ")) + perLine - 1) / perLine)); + var height = Math.Round(lines * LineHeight / rows, 2); + if (height <= LineHeight) + return; + for (var r = row; r < row + rows; r++) + if (!sheet.RowHeights.TryGetValue(r, out var known) || known < height) + sheet.RowHeights[r] = height; + } + /// The cell's own borders; null draws a thin line on every side. - private static CellStyle StyleOf(HCell cell, HBorders? borders) => new() + private static CellStyle StyleOf(HCell cell, HBorders? borders) { - Bold = Texts(cell).ToList() is { Count: > 0 } texts && texts.All(t => t.Format.Bold == true || t.Text.Trim().Length == 0), - Fill = cell.Fill, - Wrap = true, - VerticalAlign = cell.VerticalAlign switch + var texts = Texts(cell).Where(t => t.Text.Trim().Length > 0).ToList(); + return new() { - HVerticalAlign.Center => CellVerticalAlign.Center, - HVerticalAlign.Bottom => CellVerticalAlign.Bottom, - _ => CellVerticalAlign.Top, - }, - Left = borders is { } b ? Side(b.Left) : Thin, - Right = borders is { } r ? Side(r.Right) : Thin, - Top = borders is { } t ? Side(t.Top) : Thin, - Bottom = borders is { } d ? Side(d.Bottom) : Thin, - }; + Bold = texts.Count > 0 && texts.All(t => t.Format.Bold == true), + Italic = texts.Count > 0 && texts.All(t => t.Format.Italic == true), + Align = AlignOf(cell), + Fill = cell.Fill, + Wrap = true, + VerticalAlign = cell.VerticalAlign switch + { + HVerticalAlign.Center => CellVerticalAlign.Center, + HVerticalAlign.Bottom => CellVerticalAlign.Bottom, + _ => CellVerticalAlign.Top, + }, + Left = borders is { } b ? Side(b.Left) : Thin, + Right = borders is { } r ? Side(r.Right) : Thin, + Top = borders is { } t ? Side(t.Top) : Thin, + Bottom = borders is { } d ? Side(d.Bottom) : Thin, + }; + } + + /// Centred or right-aligned when every paragraph with text is; else Excel's own alignment. + private static CellAlign AlignOf(HCell cell) + { + HAlign? shared = null; + foreach (var paragraph in cell.Blocks.OfType()) + { + if (!HDocumentWalker.InlinesOf(paragraph.Inlines).OfType().Any(t => t.Text.Trim().Length > 0)) + continue; + var align = paragraph.Format.Align ?? HAlign.Left; + if (shared is { } known && known != align) + return CellAlign.General; + shared = align; + } + return shared switch + { + HAlign.Center => CellAlign.Center, + HAlign.Right => CellAlign.Right, + _ => CellAlign.General, + }; + } private static readonly CellBorder Thin = new("thin", "#000000"); diff --git a/tests/Filee.Engines.Tests/DocumentTablesTests.cs b/tests/Filee.Engines.Tests/DocumentTablesTests.cs index 83ddf7fa2..57c144d69 100644 --- a/tests/Filee.Engines.Tests/DocumentTablesTests.cs +++ b/tests/Filee.Engines.Tests/DocumentTablesTests.cs @@ -1,9 +1,14 @@ -// DOCX / HTML → XLSX (#1016): each table of a Word document or web page on its own sheet, with merged cells, numbers -// and dates as real values, the cells' own borders and frozen header rows; a clear failure when there is no table; and -// no route that reaches spreadsheets through a document made from something else. +// Documents → XLSX (#1016): each table of a Word (DOCX, DOC), OpenDocument, RTF, 한글 (HWP, HWPX) document or web page +// on its own sheet, with merged cells, numbers and dates as real values, the cells' own borders, fill, bold and +// alignment and frozen header rows; rows tall enough for a merged cell's text; a clear failure when there is no table; +// no route that reaches spreadsheets through a document made from something else; and hostile tables (spans over +// millions of cells, thousands of tables, deep nesting) refused or written quickly. +using System.Diagnostics; using Filee.Core.Conversion; using Filee.Core.Presets; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Office; using Filee.Engines.Office.Sheets; using static Filee.Engines.Tests.DocxBuilder; @@ -133,20 +138,268 @@ public async Task A_document_without_tables_fails_with_a_message() Assert.Equal("error.document_no_tables", job.Files[0].ErrorKey); } + private const int Day0 = 693593; // new DateOnly(1899, 12, 30).DayNumber: Excel's day 0 + + /// + /// A Word table with what each format below keeps: a header row, a horizontal and a vertical merge, a shaded bold + /// centred number, a right-aligned date, a code with a leading zero and a merged cell of three lines. + /// + private static string WordTable(string percentCell = "") => + Table([2000, 2000, 2000], + Row(Cell("Region", "") + Cell("Sales"), header: true) + + Row(Cell("North", "") + Cell("Q1") + + $"{P(R("1,234.50", ""), "")}") + + Row(Cell("", "") + Cell("Q2") + $"{P(R("2026-03-31"), "")}") + + Row(Cell("007") + (percentCell.Length > 0 ? percentCell : Cell("12.5%")) + Cell("x")) + + Row($"{P(R("First line of a note"))}{P(R("second line"))}{P(R("third line"))}")); + + /// What looks like on its sheet, whichever format carried it. + private static void AssertWordTable(Worksheet sheet, string format) + { + Assert.Equal("Region", sheet.Get(0, 0)!.Text); + Assert.Contains(new CellRange(0, 0, 0, 1), sheet.Merges); // across columns + Assert.Contains(new CellRange(1, 0, 2, 0), sheet.Merges); // down rows + Assert.Contains(new CellRange(4, 0, 4, 2), sheet.Merges); + var amount = sheet.Get(1, 2)!; + Assert.Equal((1234.5, "#,##0.00"), (amount.Value!.Value, amount.NumberFormat)); + Assert.True(amount.Style.Bold, format); + Assert.Equal("#FFFF00", amount.Style.Fill, ignoreCase: true); + Assert.Equal(CellAlign.Center, amount.Style.Align); + Assert.Equal(new DateOnly(2026, 3, 31).DayNumber - Day0, sheet.Get(2, 2)!.Value); + Assert.Equal(CellAlign.Right, sheet.Get(2, 2)!.Style.Align); + Assert.Equal(CellAlign.General, sheet.Get(1, 1)!.Style.Align); // left text keeps Excel's own alignment + Assert.Equal("007", sheet.Get(3, 0)!.Text); + Assert.Null(sheet.Get(3, 0)!.Value); + Assert.Equal("thin", sheet.Get(1, 1)!.Style.Left.Style); + // Excel doesn't fit a row to a merged cell's text: the three lines get their height, other rows fit themselves. + Assert.True(sheet.RowHeights.GetValueOrDefault(4) >= 45, $"{format}: row height {sheet.RowHeights.GetValueOrDefault(4)}"); + Assert.False(sheet.RowHeights.ContainsKey(1)); + Assert.Equal(1, sheet.FrozenRows); + } + + [Fact] + public async Task Hwpx_and_hwp_tables_become_sheets_with_an_equation_number_as_a_number() + { + // A number typed as an equation, as 한글 users often do in tables: its text, not its script, is the value. + var docx = new DocxBuilder(); + docx.Paragraph(P(R("Before the table."))); + docx.Paragraph(WordTable("40")); + var word = docx.Save(Path.Combine(fx.NewFolder(), "sales.docx")); + var hwpx = Assert.Single((await fx.ConvertAsync([word], new Preset { TargetFormat = "hwpx" })).Outputs); + var hwp = Assert.Single((await fx.ConvertAsync([hwpx], new Preset { TargetFormat = "hwp" })).Outputs); + + foreach (var (path, format) in new[] { (hwpx, "hwpx"), (hwp, "hwp") }) + { + var sheet = Assert.Single((await ConvertAsync(path)).Sheets); + AssertWordTable(sheet, format); + Assert.Equal(40, sheet.Get(3, 1)!.Value); + } + } + + [Fact] + public async Task Odt_tables_keep_spans_styles_and_header_rows() + { + const string ruled = "fo:border=\"0.5pt solid #000000\""; + var odt = new OdtBuilder + { + ContentStyles = + "" + + "" + + "" + + $"" + + $"", + Body = + "Sales by region." + + "" + + "" + + "Region" + + "Sales" + + "North" + + "Q1" + + "1,234.50" + + "Q2" + + "2026-03-31" + + "007" + + "12.5%x" + + "First line of a notesecond linethird line" + + "" + + "After the table.", + }; + var sheet = Assert.Single((await ConvertAsync(odt.Save(Path.Combine(fx.NewFolder(), "sales.odt")))).Sheets); + + AssertWordTable(sheet, "odt"); + Assert.Equal(0.125, sheet.Get(3, 1)!.Value!.Value, 9); + Assert.DoesNotContain(sheet.Rows.Values.SelectMany(r => r.Values), c => c.Text.Contains("Sales by region")); + } + + [Fact] + public async Task Rtf_tables_keep_spans_shading_and_alignment() + { + const string ruled = @"\clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs"; + var rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}{\colortbl;\red255\green255\blue0;}" + + @"\pard Sales by region.\par" + + $@"\trowd\trhdr{ruled}\clmgf\cellx2000{ruled}\clmrg\cellx4000{ruled}\cellx6000" + + @"\pard\intbl Region\cell\cell Sales\cell\row" + + $@"\trowd{ruled}\clvmgf\cellx2000{ruled}\cellx4000{ruled}\clcbpat1\cellx6000" + + @"\pard\intbl North\cell Q1\cell\pard\intbl\qc{\b 1,234.50}\cell\row" + + $@"\trowd{ruled}\clvmrg\cellx2000{ruled}\cellx4000{ruled}\cellx6000" + + @"\pard\intbl\cell Q2\cell\pard\intbl\qr 2026-03-31\cell\row" + + $@"\trowd{ruled}\cellx2000{ruled}\cellx4000{ruled}\cellx6000" + + @"\pard\intbl 007\cell 12.5%\cell x\cell\row" + + $@"\trowd{ruled}\clmgf\cellx2000{ruled}\clmrg\cellx4000{ruled}\clmrg\cellx6000" + + @"\pard\intbl First line of a note\par second line\par third line\cell\cell\cell\row" + + @"\pard After the table.\par}"; + var path = Path.Combine(fx.NewFolder(), "sales.rtf"); + File.WriteAllText(path, rtf); + var sheet = Assert.Single((await ConvertAsync(path)).Sheets); + + AssertWordTable(sheet, "rtf"); + Assert.Equal(0.125, sheet.Get(3, 1)!.Value!.Value, 9); + } + + [Fact] + public async Task Doc_tables_become_sheets_too() + { + var single = DocBuilder.Brc80(4, 1); + var borders = DocBuilder.Sprm(0xD605, single, single, single, single, single, single); + short[] grid = [0, 2000, 4000, 6000]; + // Cell flags: 1 / 2 start and continue a merge across, 3 << 5 / 1 << 5 one down. + var header = (byte[])[.. DocBuilder.DefTable(grid, [1, 2, 0]), .. borders, .. DocBuilder.Sprm(0x3404, (byte)1)]; + var first = (byte[])[.. DocBuilder.DefTable(grid, [3 << 5, 0, 0]), .. borders, + // SHD per cell (foreground, background, pattern): automatic for the first two, yellow behind the third. + .. DocBuilder.Sprm(0xD612, (byte[])[0, 0, 0, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0, 0, 0, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0x00, 0, 0, 0])]; + var second = (byte[])[.. DocBuilder.DefTable(grid, [1 << 5, 0, 0]), .. borders]; + var plain = (byte[])[.. DocBuilder.DefTable(grid), .. borders]; + var builder = new DocBuilder() + .Paragraph(DocBuilder.R("Sales by region.")) + .Row(header, [DocBuilder.R("Region")], [], [DocBuilder.R("Sales")]) + .Row(first, [DocBuilder.R("North")], [DocBuilder.R("Q1")], [DocBuilder.R("1,234.50", DocBuilder.Sprm(0x0835, (byte)1))]) + .Row(second, [], [DocBuilder.R("Q2")], [DocBuilder.R("2026-03-31")]) + .Row(plain, [DocBuilder.R("007")], [DocBuilder.R("12.5%")], [DocBuilder.R("x")]) + .Paragraph(DocBuilder.R("After the table.")); + var path = Path.Combine(fx.NewFolder(), "sales.doc"); + File.WriteAllBytes(path, builder.Build()); + var sheet = Assert.Single((await ConvertAsync(path)).Sheets); + + Assert.Equal("Region", sheet.Get(0, 0)!.Text); + Assert.Contains(new CellRange(0, 0, 0, 1), sheet.Merges); + Assert.Contains(new CellRange(1, 0, 2, 0), sheet.Merges); + var amount = sheet.Get(1, 2)!; + Assert.Equal(1234.5, amount.Value); + Assert.True(amount.Style.Bold); + Assert.Equal("#FFFF00", amount.Style.Fill, ignoreCase: true); + Assert.Null(sheet.Get(1, 1)!.Style.Fill); + Assert.Equal(new DateOnly(2026, 3, 31).DayNumber - Day0, sheet.Get(2, 2)!.Value); + Assert.Null(sheet.Get(3, 0)!.Value); + Assert.Equal(0.125, sheet.Get(3, 1)!.Value!.Value, 9); + Assert.Equal("thin", sheet.Get(3, 2)!.Style.Bottom.Style); + Assert.Equal(1, sheet.FrozenRows); + Assert.True(sheet.ColumnWidth(0) > Worksheet.StandardColumnWidth); // 2000 twips from the cell boundaries + Assert.DoesNotContain(sheet.Rows.Values.SelectMany(r => r.Values), c => c.Text.Contains("After the table")); + } + [Fact] public void Only_the_document_itself_reaches_a_spreadsheet_through_its_tables() { var planner = fx.Catalog.CreatePlanner(); - foreach (var source in new[] { "docx", "docm", "html", "mhtml" }) + foreach (var source in new[] { "docx", "docm", "doc", "dot", "wps", "odt", "rtf", "hwp", "hwpx", "html", "mhtml" }) foreach (var target in new[] { "xlsx", "ods", "csv" }) Assert.Equal("document-tables", Assert.Single(planner.Plan(source, target)!.Steps).Converter.Id); - // Files that become a DOCX or HTML on the way (a PDF, a picture, Markdown, HWPX, an e-book) don't, even with every - // optional engine installed, and a document's tables don't go on to JSON. + // A Word 6.0 / 95 file with a .doc name is looked at first, as on DOC's other routes. + Assert.True(Assert.Single(planner.Plan("doc", "xlsx")!.Steps).Checked); + Assert.False(Assert.Single(planner.Plan("hwp", "xlsx")!.Steps).Checked); + // Files that become a DOCX or HTML on the way (a PDF, a picture, Markdown, an e-book, slides) don't, even with + // every optional engine installed, and a document's tables don't go on to JSON. var everything = fx.Catalog.CreatePlanner([.. fx.Catalog.All.Select(c => c.Id)]); - foreach (var source in new[] { "pdf", "jpg", "md", "hwpx", "epub", "odt", "doc" }) + foreach (var source in new[] { "pdf", "jpg", "md", "epub", "pptx" }) foreach (var target in new[] { "xlsx", "ods", "csv", "json" }) Assert.DoesNotContain(everything.Plan(source, target)?.Steps ?? [], s => s.Converter.Id == "document-tables"); - foreach (var target in new[] { "json", "xml", "tsv" }) - Assert.DoesNotContain(everything.Plan("docx", target)?.Steps ?? [], s => s.Converter.Id == "document-tables"); + foreach (var source in new[] { "docx", "hwpx", "odt" }) + foreach (var target in new[] { "json", "xml", "tsv" }) + Assert.DoesNotContain(everything.Plan(source, target)?.Steps ?? [], s => s.Converter.Id == "document-tables"); + } + + // ───────────────────────── Hostile tables ───────────────────────── + + [Fact] + public async Task A_span_over_millions_of_cells_is_refused_at_once() + { + // 700 rows of one cell spanning the widest grid: 11.5 million positions, from a file of a few kilobytes. + var docx = new DocxBuilder(); + docx.Paragraph(Table([2000], string.Concat(Enumerable.Range(0, 700).Select(i => Row(Cell($"row {i}", "")))))); + var path = docx.Save(Path.Combine(fx.NewFolder(), "wide.docx")); + var watch = Stopwatch.StartNew(); + var job = await fx.ConvertAsync([path], new Preset { TargetFormat = "xlsx" }); + + Assert.Equal(JobState.Failed, job.State); + Assert.Equal("error.document_too_complex", job.Files[0].ErrorKey); + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(30), $"took {watch.Elapsed}"); + } + + /// A table of one cell holding and then . + private static HTable Box(string text, HTable? inner = null) + { + var paragraph = new HParagraph(); + paragraph.Inlines.Add(new HText(text, default)); + var cell = new HCell(); + cell.Blocks.Add(paragraph); + if (inner is not null) + cell.Blocks.Add(inner); + var row = new HRow(); + row.Cells.Add(cell); + var table = new HTable { ColumnCount = 1 }; + table.Rows.Add(row); + return table; + } + + private static HDocument Document(params HBlock[] blocks) + { + var section = new HSection(); + section.Blocks.AddRange(blocks); + var document = new HDocument(); + document.Sections.Add(section); + return document; + } + + [Fact] + public void Deeply_nested_tables_are_walked_without_recursion() + { + // Far deeper than any reader lets through (DocumentLimits.MaxNestingDepth): each level is a sheet. + HTable? table = null; + for (var level = 5000; level > 0; level--) + table = Box($"level {level}", table); + var watch = Stopwatch.StartNew(); + var book = DocumentTablesConverter.Build(Document(table!)); + + Assert.Equal(5000, book.Sheets.Count); + Assert.Equal("level 1", book.Sheets[0].Get(0, 0)!.Text); + Assert.Equal("level 5000", book.Sheets[^1].Get(0, 0)!.Text); + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(30), $"took {watch.Elapsed}"); + } + + [Fact] + public void More_tables_or_cells_than_a_workbook_takes_are_refused() + { + var tables = Enumerable.Range(0, DocumentTablesConverter.MaxSheets + 1).Select(i => (HBlock)Box($"t{i}")).ToArray(); + Assert.Equal("error.document_too_complex", + Assert.Throws(() => DocumentTablesConverter.Build(Document(tables))).ErrorKey); + + // The tables share one budget of cells: eleven tables of a million positions each, none too large alone. + var wide = Enumerable.Range(0, 11).Select(_ => + { + var table = Box("wide"); + table.Rows[0].Cells[0].ColSpan = 16_384; + for (var r = 1; r < 62; r++) + { + var row = new HRow(); + row.Cells.Add(new HCell { ColSpan = 16_384 }); + table.Rows.Add(row); + } + return (HBlock)table; + }).ToArray(); + var watch = Stopwatch.StartNew(); + Assert.Equal("error.document_too_complex", + Assert.Throws(() => DocumentTablesConverter.Build(Document(wide))).ErrorKey); + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(10), $"took {watch.Elapsed}"); } } From 2c06c18d2127a94dce162b238b831a88ce3fbddf Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 00:21:57 +0900 Subject: [PATCH 3/6] Typeset TeX math in Markdown (#1652) Markdown's $...$, $$...$$, \(...\) and \[...\] are read into the equation tree Word and Hangul equations use, so the built-in engines typeset them instead of printing the TeX source. - MarkdownMath: a Markdig inline parser for the four delimiters with Pandoc's dollar rules (no space inside, no digit after the closing $, escaped $ is text, code spans win); Markdown -> HTML writes MathML. - LatexMath: our own reader for the TeX subset Markdown uses: fractions, roots, scripts, Greek and symbols, sums/integrals/lim with limits, \left...\right, matrices, cases, aligned/gather, accents, \text, \mathbf/\mathrm/\mathbb, spacing, \not, \tag. Unknown commands stay as their text. Formulas longer than 32,768 characters or nested deeper than 100 levels stay their source; equations count against MaxObjects. - MarkdownReader: equations for math; display math alone in its paragraph is a centred equation. - DOCX writer: equations as Word math (OmmlWriter, m:oMath/m:oMathPara). - HTML and EPUB writers: equations as MathML (MathMLWriter, TeX kept as the annotation); EPUB pages with MathML get properties="mathml". - Writers without equations keep the TeX source as written; the Markdown writer writes it back unescaped. - MathLayout: TeX's further relations and binary operators get their spacing. Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 16 +- src/Filee.Engines/Ebooks/EbookConverter.cs | 2 +- src/Filee.Engines/Ebooks/EpubWriter.cs | 7 +- .../Hwp/Hwpx/Docx/DocxWriter.Body.cs | 14 + src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs | 6 +- src/Filee.Engines/Hwp/Hwpx/Docx/OmmlReader.cs | 3 + src/Filee.Engines/Hwp/Hwpx/Docx/OmmlWriter.cs | 439 +++++++ src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs | 22 +- src/Filee.Engines/Hwp/Hwpx/LatexMath.cs | 1111 +++++++++++++++++ src/Filee.Engines/Hwp/Hwpx/MarkdownMath.cs | 215 ++++ src/Filee.Engines/Hwp/Hwpx/MarkdownReader.cs | 81 +- src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs | 3 + src/Filee.Engines/Hwp/Hwpx/MathMLWriter.cs | 350 ++++++ src/Filee.Engines/Hwp/Hwpx/XhtmlWriter.cs | 18 +- src/Filee.Engines/Typesetting/MathLayout.cs | 7 +- .../Filee.Engines.Tests/LatexMathFuzzTests.cs | 51 + .../Filee.Engines.Tests/MarkdownMathTests.cs | 335 +++++ 17 files changed, 2662 insertions(+), 18 deletions(-) create mode 100644 src/Filee.Engines/Hwp/Hwpx/Docx/OmmlWriter.cs create mode 100644 src/Filee.Engines/Hwp/Hwpx/LatexMath.cs create mode 100644 src/Filee.Engines/Hwp/Hwpx/MarkdownMath.cs create mode 100644 src/Filee.Engines/Hwp/Hwpx/MathMLWriter.cs create mode 100644 tests/Filee.Engines.Tests/LatexMathFuzzTests.cs create mode 100644 tests/Filee.Engines.Tests/MarkdownMathTests.cs diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 7969aedab..c0bbe9673 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -828,8 +828,20 @@ so DOCX → HWPX and HWP keep editable equations and DOCX → PDF draws them wit The box an equation is stored with (`hp:sz`) is measured like rhwp lays the script out (estimated character widths, script scale 0.7, fraction and root spacing, from rhwp's `equation/layout.rs`): rhwp stretches an equation to its stored width, so a wrong box would distort it. 한글 lays the script out again when the equation is edited. -The model's other writers (HTML, EPUB, Markdown, TXT …) write a linear form instead, `(a+b)/(2c)`, `x_i^2`, `√(x)`, -which is also the equation's alternative text in HWPX. +The model's other writers (Markdown, TXT, ODT, PPTX …) write a linear form instead, `(a+b)/(2c)`, `x_i^2`, `√(x)`, +which is also the equation's alternative text in HWPX. The DOCX writer writes every equation back as Word math +(`Hwp/Hwpx/Docx/OmmlWriter.cs`), the HTML and EPUB writers as MathML (`Hwp/Hwpx/MathMLWriter.cs`; EPUB pages with it +carry `properties="mathml"`; MOBI keeps the text). + +**TeX math in Markdown** (#1652): `$…$`, `$$…$$`, `\(…\)` and `\[…\]` (`Hwp/Hwpx/MarkdownMath.cs`, Pandoc's rules +for dollars: no space inside the delimiters, no digit after the closing `$`, code spans win) are read by +`Hwp/Hwpx/LatexMath.cs` into the same tree: fractions, roots, scripts, Greek letters and symbols, sums, integrals and +`\lim` with their limits, `\left … \right`, matrices, `cases`, `aligned` and `gather`, accents, `\text`, `\mathbf`, +`\mathrm`, `\mathbb` and the spacing commands. So Markdown → PDF typesets them, → DOCX gives Word math, → HWPX 한글 +equations, → HTML and EPUB MathML (with the TeX as its annotation). Display math alone in its paragraph is centred. +An unknown command stays as its text (`\foo`); a formula longer than 32,768 characters or nested deeper than 100 +levels stays its source, and the equations count against `MaxObjects`. Writers without equations keep the TeX +source as written (`$\frac{a}{b}$`). ## Spreadsheets diff --git a/src/Filee.Engines/Ebooks/EbookConverter.cs b/src/Filee.Engines/Ebooks/EbookConverter.cs index 7bde7435b..7e69720ba 100644 --- a/src/Filee.Engines/Ebooks/EbookConverter.cs +++ b/src/Filee.Engines/Ebooks/EbookConverter.cs @@ -176,7 +176,7 @@ private static void Write(Book book, ConversionStep step, string output, string { // One self-contained page: pictures are embedded as data: URIs. var images = EbookFiles.NewFolder(work, "html-images"); - var page = XhtmlWriter.Write(book.Document, new XhtmlOptions { ImageSource = path => DataUri(path, images) }).Single(); + var page = XhtmlWriter.Write(book.Document, new XhtmlOptions { ImageSource = path => DataUri(path, images), MathML = true }).Single(); File.WriteAllText(output, XhtmlWriter.Page(book.DisplayTitle(step.InputPath), page.Body, book.DisplayLanguage(), null, epub: false), new UTF8Encoding(false)); break; } diff --git a/src/Filee.Engines/Ebooks/EpubWriter.cs b/src/Filee.Engines/Ebooks/EpubWriter.cs index 82781e96e..c45df2919 100644 --- a/src/Filee.Engines/Ebooks/EpubWriter.cs +++ b/src/Filee.Engines/Ebooks/EpubWriter.cs @@ -28,6 +28,7 @@ public static void Write(Book book, string outputPath, string sourcePath, string ChapterFile = i => $"ch{i + 1:000}.xhtml", ImageSource = path => package.AddImage(path, imageFolder), Epub = true, + MathML = true, }); // A cover page for covers the content does not show already (MOBI and FB2 keep the cover apart). @@ -185,7 +186,11 @@ private string Opf(string identifier, bool fixedLayout) sb.Append("\n"); sb.Append("\n"); for (var i = 0; i < _pages.Count; i++) - sb.Append($"\n"); + { + // EPUB 3 names the pages with MathML in them. + var mathml = _pages[i].Content.Contains("\n"); + } for (var i = 0; i < _images.Count; i++) sb.Append($"\n"); sb.Append("\n\n"); diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.Body.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.Body.cs index 6aa53a202..92dc3a6ce 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.Body.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.Body.cs @@ -45,6 +45,9 @@ internal sealed partial class DocxWriter /// What the paragraph style of the current paragraph already sets (explicit "off" values override it). private readonly record struct RunContext(bool StyleBold, bool Link); + /// The paragraph whose runs are being written (whether an equation in it is a display equation). + private HParagraph? _paragraph; + // ───────────────────────── Sections ───────────────────────── private string Body(HDocument document) @@ -217,7 +220,10 @@ private string Paragraph(HParagraph paragraph, string? sectPr = null, string? st if (paragraph.ColumnBreakBefore) sb.Append(""); sb.Append(leadingRuns); + var outer = _paragraph; + _paragraph = paragraph; Inlines(paragraph.Inlines, sb, context); + _paragraph = outer; return sb.Append("").ToString(); } @@ -346,6 +352,14 @@ private void Inlines(IEnumerable inlines, StringBuilder sb, RunContext { switch (inline) { + case HEquation equation: + { + // Word math; its runs take the equation's size and colour, the math font and style their own. + var display = _paragraph is { } paragraph && HEquation.IsDisplay(equation, paragraph); + var format = equation.Format with { Font = null, EastAsianFont = null, Bold = null, Italic = null, Underline = null, Strike = null, Superscript = null, Subscript = null }; + sb.Append(OmmlWriter.Write(equation.Math, display, RunProperties(format, context with { Link = false, StyleBold = false }))); + break; + } case HText text: TextRuns(sb, text.Text, text.Format, context); break; diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs index 5c3bd282f..d0c4663cb 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs @@ -5,7 +5,8 @@ // Split over partial files: // * DocxWriter.cs package: parts, relationships, content types, styles, settings, numbering, notes; // * DocxWriter.Body.cs sections (page setup, columns, headers/footers), paragraphs, runs and tables; -// * DocxWriter.Objects.cs pictures (inline and floating), text boxes and shapes (wps), links, bookmarks, fields. +// * DocxWriter.Objects.cs pictures (inline and floating), text boxes and shapes (wps), links, bookmarks, fields; +// * OmmlWriter.cs equations as Word math (m:oMath; m:oMathPara for a display equation). // // Child elements are written in schema order (Word refuses files where they are not), see the notes in each file. @@ -31,7 +32,8 @@ internal sealed partial class DocxWriter "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " + "xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" " + "xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" " + - "xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\""; + "xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" " + + "xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\""; private const string Declaration = ""; diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/OmmlReader.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/OmmlReader.cs index 402e627b8..fc5090ec3 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/OmmlReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/OmmlReader.cs @@ -134,6 +134,9 @@ internal static class OmmlReader ['~'] = "SIM", }; + /// The 한글 equation command of a symbol character (α → alpha, ≤ → LEQ), or null; for LatexMath too. + internal static string? Command(char symbol) => Commands.GetValueOrDefault(symbol); + /// Large operators (m:nary, or the character typed as text) → their command. private static readonly Dictionary BigOperators = new() { diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/OmmlWriter.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/OmmlWriter.cs new file mode 100644 index 000000000..c3f05de2b --- /dev/null +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/OmmlWriter.cs @@ -0,0 +1,439 @@ +// The equation tree (MathNode) → Word math (Office Math, m:oMath, ECMA-376 part 1 §22.1), the other way from OmmlReader: +// the DOCX writer stores equations as Word's own, editable in Word's equation editor, instead of their linear text. +// +// Letters are italic (Word's default for math), function names, numbers and upright (rm) text plain (m:sty="p"), bold +// b / bi, quoted text normal text (m:nor). Fractions, roots, scripts, large operators (m:nary, its operand the items +// after it up to the next relation, + or −), lim and function names with their argument (m:func), brackets (m:d), +// matrices (m:m in m:d), cases (an equation array in a left brace), piles and aligned lines (m:eqArr, the cells joined +// by & as Word joins them), accents (m:acc), over- and underlines (m:bar) and crossed-out content (m:borderBox). + +using System.Globalization; +using System.Text; + +namespace Filee.Engines.Hwp.Hwpx.Docx; + +internal static class OmmlWriter +{ + /// The math font of every run, as Word writes it. + private const string MathFont = ""; + + private static readonly HashSet Relations = + [ + "=", "<", ">", "≤", "≥", "≠", "≈", "≡", "≅", "∼", "≃", "∝", "≪", "≫", "∈", "∉", "∋", "⊂", "⊃", "⊆", "⊇", "→", "←", + "↔", "⇒", "⇐", "⇔", "↦", "⟶", "⟹", "⟺", "⩽", "⩾", ":", + ]; + + /// How the text of a run is styled. + private readonly record struct Style(bool Upright, bool Bold, string RunProperties); + + /// + /// One equation: an m:oMath, or for a display equation an m:oMathPara (a paragraph of its own, centred). + /// is the content of each run's w:rPr after its font (size, colour). + /// + public static string Write(MathNode node, bool display, string runProperties = "") + { + var sb = new StringBuilder(); + if (display) + sb.Append(""); + sb.Append(""); + Node(node, sb, new Style(false, false, runProperties)); + sb.Append(""); + if (display) + sb.Append(""); + return sb.ToString(); + } + + private static void Node(MathNode node, StringBuilder sb, Style style) + { + switch (node) + { + case MathRow row: + Row(row.Items, sb, style); + break; + case MathText text: + Run(text.Text, sb, style, italic: !style.Upright && text.Text.Any(char.IsLetter)); + break; + case MathNumber number: + Run(number.Digits, sb, style, italic: false); + break; + case MathSymbol symbol: + Run(symbol.Text == "-" ? "−" : symbol.Text, sb, style, italic: false); + break; + case MathCommand command: + Run(command.Glyph, sb, style, italic: !style.Upright && command.Glyph.Any(char.IsLetter)); + break; + case MathQuoted quoted: + Run(quoted.Text, sb, style, italic: false, normal: true); + break; + case MathFunction function: + Run(function.Name, sb, style, italic: false); + break; + case MathSpace space: + Run(Space(space.Width), sb, style, italic: false); + break; + case MathAlign: + Run("&", sb, style, italic: false); + break; + case MathFraction fraction: + sb.Append(""); + if (!fraction.Bar) + sb.Append(""); + Argument("num", fraction.Numerator, sb, style); + Argument("den", fraction.Denominator, sb, style); + sb.Append(""); + break; + case MathRoot root: + sb.Append(""); + if (root.Index is null) + sb.Append(""); + else + Argument("deg", root.Index, sb, style); + Argument("e", root.Body, sb, style); + sb.Append(""); + break; + case MathScripts scripts: + { + var kind = (scripts.Sub, scripts.Sup) switch + { + (null, null) => null, + (null, _) => "sSup", + (_, null) => "sSub", + _ => "sSubSup", + }; + if (kind is null) + { + Node(scripts.Base, sb, style); + break; + } + sb.Append("'); + Argument("e", scripts.Base, sb, style); + if (scripts.Sub is not null) + Argument("sub", scripts.Sub, sb, style); + if (scripts.Sup is not null) + Argument("sup", scripts.Sup, sb, style); + sb.Append("'); + break; + } + case MathBigOperator big: + Nary(big, [], sb, style); + break; + case MathLimit limit: + Limit(limit, [], sb, style); + break; + case MathMatrix matrix: + { + var (open, close) = matrix.Command switch + { + "pmatrix" => ("(", ")"), + "bmatrix" => ("[", "]"), + "dmatrix" or "vmatrix" => ("|", "|"), + _ => ((string?)null, (string?)null), + }; + if (open is null) + { + Matrix(matrix.Rows, sb, style); + break; + } + DelimiterStart(open, close!, sb); + Matrix(matrix.Rows, sb, style); + sb.Append(""); + break; + } + case MathCases cases: + DelimiterStart("{", "", sb); + sb.Append(""); + foreach (var row in cases.Rows) + { + sb.Append(""); + for (var c = 0; c < row.Count; c++) + { + if (c > 0) + Run("&", sb, style, italic: false); + Node(row[c], sb, style); + } + sb.Append(""); + } + sb.Append(""); + break; + case MathPile pile: + sb.Append(""); + foreach (var row in pile.Rows) + Argument("e", row, sb, style); + sb.Append(""); + break; + case MathBrackets brackets: + DelimiterStart(brackets.Left, brackets.Right, sb); + Node(brackets.Body, sb, style); + sb.Append(""); + break; + case MathAccent accent: + Accent(accent, sb, style); + break; + case MathStyled styled: + Node(styled.Body, sb, styled.Command switch + { + "rm" => style with { Upright = true }, + "it" => style with { Upright = false }, + "bold" => style with { Bold = true }, + _ => style, + }); + break; + } + } + + /// Items side by side; a large operator, lim or a function name takes the items after it as its operand. + private static void Row(IReadOnlyList items, StringBuilder sb, Style style) + { + for (var i = 0; i < items.Count; i++) + { + switch (items[i]) + { + case MathBigOperator big: + { + var operand = Operand(items, i + 1); + Nary(big, operand, sb, style); + i += operand.Count; + break; + } + case MathLimit limit: + { + var operand = Operand(items, i + 1); + Limit(limit, operand, sb, style); + i += operand.Count; + break; + } + case MathFunction function when i + 1 < items.Count && !IsBreak(items[i + 1]): + { + // sin x, log (x+1): the function's argument is the next item, or a bracketed run. + var operand = items[i + 1] is MathSymbol { Text: "(" } ? Bracketed(items, i + 1) : [items[i + 1]]; + sb.Append(""); + Run(function.Name, sb, style, italic: false); + sb.Append(""); + Row(operand, sb, style); + sb.Append(""); + i += operand.Count; + break; + } + default: + Node(items[i], sb, style); + break; + } + } + } + + /// A relation, + or − (or an alignment mark) ends the operand of a sum or an integral. + private static bool IsBreak(MathNode node) => node switch + { + MathSymbol symbol => Relations.Contains(symbol.Text) || symbol.Text is "+" or "-" or "," or ";", + MathCommand command => Relations.Contains(command.Glyph) || command.Glyph is "±" or "∓", + MathAlign => true, + _ => false, + }; + + private static List Operand(IReadOnlyList items, int start) + { + var operand = new List(); + for (var i = start; i < items.Count && !IsBreak(items[i]); i++) + { + operand.Add(items[i]); + if (items[i] is MathBigOperator or MathLimit) + { + // A sum of a sum: the inner one takes the rest. + operand.AddRange(Operand(items, i + 1)); + break; + } + } + return operand; + } + + /// The items from a "(" up to its matching ")" (or the end). + private static List Bracketed(IReadOnlyList items, int start) + { + var operand = new List(); + var level = 0; + for (var i = start; i < items.Count; i++) + { + operand.Add(items[i]); + if (items[i] is MathSymbol { Text: "(" }) + level++; + else if (items[i] is MathSymbol { Text: ")" } && --level == 0) + break; + } + return operand; + } + + private static void Nary(MathBigOperator big, List operand, StringBuilder sb, Style style) + { + sb.Append(""); + sb.Append(""); + if (big.Lower is null) + sb.Append(""); + if (big.Upper is null) + sb.Append(""); + sb.Append(""); + Argument("sub", big.Lower ?? MathRow.Empty, sb, style); + Argument("sup", big.Upper ?? MathRow.Empty, sb, style); + sb.Append(""); + Row(operand, sb, style); + sb.Append(""); + } + + private static void Limit(MathLimit limit, List operand, StringBuilder sb, Style style) + { + sb.Append(""); + if (limit.Lower is null) + { + Run("lim", sb, style, italic: false); + } + else + { + sb.Append(""); + Run("lim", sb, style, italic: false); + sb.Append(""); + Argument("lim", limit.Lower, sb, style); + sb.Append(""); + } + sb.Append(""); + Row(operand, sb, style); + sb.Append(""); + } + + private static void Matrix(IReadOnlyList> rows, StringBuilder sb, Style style) + { + // Word wants every row as long as the matrix is wide. + var columns = Math.Max(1, rows.Count == 0 ? 1 : rows.Max(r => r.Count)); + sb.Append(""); + foreach (var row in rows) + { + sb.Append(""); + for (var c = 0; c < columns; c++) + Argument("e", c < row.Count ? row[c] : MathRow.Empty, sb, style); + sb.Append(""); + } + sb.Append(""); + } + + /// The start of m:d up to its argument's <m:e>; "" for a side without a bracket. + private static void DelimiterStart(string open, string close, StringBuilder sb) => + sb.Append(""); + + private static void Accent(MathAccent accent, StringBuilder sb, Style style) + { + switch (accent.Command) + { + case "bar" or "OVERLINE" or "overline" or "under" or "UNDERLINE" or "underline": + { + var top = accent.Command is "bar" or "OVERLINE" or "overline"; + sb.Append(""); + Argument("e", accent.Body, sb, style); + sb.Append(""); + return; + } + case "not": + sb.Append("") + .Append(""); + Argument("e", accent.Body, sb, style); + sb.Append(""); + return; + } + var mark = accent.Command switch + { + "hat" => "̂", + "check" => "̌", + "tilde" => "̃", + "acute" => "́", + "grave" => "̀", + "dot" => "̇", + "ddot" => "̈", + "breve" => "̆", + "vec" => "⃗", + "dyad" => "⃡", + "arch" => "̑", + _ => null, + }; + if (mark is null) + { + Node(accent.Body, sb, style); + return; + } + sb.Append(""); + Argument("e", accent.Body, sb, style); + sb.Append(""); + } + + private static void Argument(string name, MathNode node, StringBuilder sb, Style style) + { + if (node is MathRow { Items.Count: 0 }) + { + sb.Append(""); + return; + } + sb.Append("'); + Node(node, sb, style); + sb.Append("'); + } + + /// + /// A math run: letters italic unless is false (plain), bold where styled; normal text + /// (m:nor) for quoted text. + /// + private static void Run(string text, StringBuilder sb, Style style, bool italic, bool normal = false) + { + var clean = Escape(text); + if (clean.Length == 0) + return; + sb.Append(""); + var sty = (italic, style.Bold) switch + { + (true, false) => null, // Word's default for math + (true, true) => "bi", + (false, true) => "b", + _ => "p", + }; + if (normal) + sb.Append(style.Bold ? "" : ""); + else if (sty is not null) + sb.Append(""); + sb.Append("").Append(MathFont).Append(style.RunProperties).Append(""); + sb.Append(text.Length > 0 && (char.IsWhiteSpace(text[0]) || char.IsWhiteSpace(text[^1])) ? "" : ""); + sb.Append(clean).Append(""); + } + + /// + /// A space of (1 is half an em, see MathSpace) as Unicode spaces Word draws in math: thin + /// spaces below a quarter of an em, else quarter-em spaces (the ones OmmlReader reads as spacing, not as text). + /// + private static string Space(double width) + { + var em = width * 0.5; + return em < 0.22 ? " " : new string(' ', Math.Clamp((int)Math.Round(em / 0.25), 1, 64)); + } + + /// Text for XML, without the characters XML can't hold. + internal static string Escape(string text) + { + var sb = new StringBuilder(text.Length); + for (var i = 0; i < text.Length; i++) + { + var c = text[i]; + if (char.IsHighSurrogate(c) && i + 1 < text.Length && char.IsLowSurrogate(text[i + 1])) + { + sb.Append(c).Append(text[++i]); + continue; + } + if (char.IsSurrogate(c) || (c < 0x20 && c is not ('\t' or '\n' or '\r')) || c is '￾' or '￿') + continue; + sb.Append(c switch + { + '&' => "&", + '<' => "<", + '>' => ">", + '"' => """, + _ => c.ToString(), + }); + } + return sb.ToString(); + } +} diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs index 32a1ff001..fa6613c09 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs @@ -597,13 +597,29 @@ internal class HText(string text, HCharFormat format) : HInline } /// -/// An equation (from Word math): a 한글 equation object in HWPX. It is text too, its linear form ("(a)/(b)", "x^2"), -/// which the writers without equations (HTML, EPUB, Markdown, plain text) write instead. +/// An equation (from Word math, a 한글 equation or Markdown's TeX math): a 한글 equation object in HWPX, Word math in +/// DOCX, MathML in HTML and EPUB. It is text too, for the writers without equations (Markdown, plain text …): its TeX +/// source as written (, "$\frac{a}{b}$"), else its linear form ("(a)/(b)", "x^2"). /// -internal sealed class HEquation(MathNode math, HCharFormat format) : HText(EquationScript.Linear(math), format) +internal sealed class HEquation(MathNode math, HCharFormat format, string? source = null) + : HText(source ?? EquationScript.Linear(math), format) { public MathNode Math { get; } = math; + /// The TeX of an equation read from TeX math (without its $ or \( delimiters); null for others. + public string? Tex { get; init; } + + /// A display equation (TeX's $$ … $$ and \[ … \]), on a line of its own; inline ones are false. + public bool Display { get; init; } + + /// + /// Whether is a display equation: a one, or a Word equation + /// alone in a centred paragraph (as DocxReader reads m:oMathPara). + /// + public static bool IsDisplay(HEquation equation, HParagraph paragraph) => + equation.Display + || (paragraph.Format.Align is HAlign.Center && paragraph.Inlines.Count(i => i is not HBookmark) == 1 && paragraph.Inlines.Contains(equation)); + /// /// The box 한글 laid the equation out in (hp:sz), with its baseline in percent of the height from the top /// (baseLine), its font size (baseUnit) and colour; null where the source doesn't say. Read for layout only. diff --git a/src/Filee.Engines/Hwp/Hwpx/LatexMath.cs b/src/Filee.Engines/Hwp/Hwpx/LatexMath.cs new file mode 100644 index 000000000..5d1028853 --- /dev/null +++ b/src/Filee.Engines/Hwp/Hwpx/LatexMath.cs @@ -0,0 +1,1111 @@ +// TeX math (what Markdown writes between $ … $, $$ … $$, \( … \) and \[ … \]) read into the equation tree (MathNode) that +// Word and 한글 equations use, so the layout engine typesets it, the DOCX writer stores it as Word math, the HTML and +// EPUB writers as MathML and the HWPX writer as a 한글 equation. +// +// The subset Markdown documents use: braces group; ^ and _ take one token or a group ("x^23" is x² 3); ' is a prime; +// \frac, \dfrac, \tfrac, \cfrac, \binom, \over, \choose and \atop; \sqrt with an optional index; Greek letters and the +// common symbols, relations, arrows and dots; \sum, \prod, \int, \oint, \bigcup … with their limits, \lim with what goes +// under it; function names (\sin, \log, \max, \operatorname{…}); \left … \middle … \right and \big…; the environments +// matrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix, array, cases, aligned / align / split / alignat / eqnarray and +// gather / gathered / multline; top-level \\ and & as in an aligned block; accents (\hat, \bar, \vec, \tilde, \dot, +// \ddot, \overline, \underline, \widehat …); \text, \mathrm, \mathbf, \mathit, \boldsymbol, \mathbb, \mathcal, +// \mathfrak and the old switches \rm, \bf, \it; spacing (\, \: \; \quad \qquad ~ \hspace); \not; \tag. Commands for +// layout only (\displaystyle, \limits, \label, \color …) are skipped. A command this reader doesn't know is kept as its +// text ("\foo"), never dropped, and its arguments follow it as groups. +// +// Bounds: a formula longer than MaxLength characters, or one that nests deeper than MaxDepth (groups, fractions, +// scripts, brackets, environments), is not read: Parse returns null and the caller keeps the source as text, as +// EquationParser keeps a 한글 script that nests too deep. The walks over the tree (layout, writers) recurse per level. + +using System.Text; +using Filee.Engines.Hwp.Hwpx.Docx; + +namespace Filee.Engines.Hwp.Hwpx; + +internal static class LatexMath +{ + /// Characters of one formula read as math; longer ones stay text (a page of matrix is a few thousand). + public const int MaxLength = 32_768; + + /// Levels of nesting read, as for 한글's equation script. + public const int MaxDepth = EquationParser.MaxDepth; + + private enum Kind + { + Ordinary, + Binary, + Relation, + } + + /// Symbol commands: their character and how they space. + private static readonly Dictionary Symbols = Build(); + + private static Dictionary Build() + { + var symbols = new Dictionary(StringComparer.Ordinal); + void Add(Kind kind, string list) + { + foreach (var pair in list.Split(' ', StringSplitOptions.RemoveEmptyEntries)) + { + var split = pair.IndexOf('=', 1); + symbols[pair[..split]] = (pair[(split + 1)..], kind); + } + } + + Add(Kind.Ordinary, + "alpha=α beta=β gamma=γ delta=δ epsilon=ϵ varepsilon=ε zeta=ζ eta=η theta=θ vartheta=ϑ iota=ι kappa=κ " + + "varkappa=ϰ lambda=λ mu=μ nu=ν xi=ξ omicron=ο pi=π varpi=ϖ rho=ρ varrho=ϱ sigma=σ varsigma=ς tau=τ " + + "upsilon=υ phi=ϕ varphi=φ chi=χ psi=ψ omega=ω digamma=ϝ Gamma=Γ Delta=Δ Theta=Θ Lambda=Λ Xi=Ξ Pi=Π " + + "Sigma=Σ Upsilon=Υ Phi=Φ Psi=Ψ Omega=Ω varGamma=Γ varDelta=Δ varTheta=Θ varLambda=Λ varXi=Ξ varPi=Π " + + "varSigma=Σ varUpsilon=Υ varPhi=Φ varPsi=Ψ varOmega=Ω " + + "infty=∞ partial=∂ nabla=∇ forall=∀ exists=∃ nexists=∄ emptyset=∅ varnothing=∅ neg=¬ lnot=¬ hbar=ℏ " + + "hslash=ℏ ell=ℓ aleph=ℵ beth=ℶ gimel=ℷ wp=℘ Re=ℜ Im=ℑ angle=∠ measuredangle=∡ sphericalangle=∢ " + + "triangle=△ top=⊤ bot=⊥ prime=′ backprime=‵ surd=√ checkmark=✓ degree=° imath=ı jmath=ȷ " + + "ldots=… dots=… dotsc=… dotso=… cdots=⋯ dotsb=⋯ dotsm=⋯ dotsi=⋯ vdots=⋮ ddots=⋱ iddots=⋰ " + + "clubsuit=♣ diamondsuit=♢ heartsuit=♡ spadesuit=♠ flat=♭ natural=♮ sharp=♯ dag=† ddag=‡ S=§ P=¶ " + + "copyright=© pounds=£ yen=¥ euro=€ therefore=∴ because=∵ complement=∁ mho=℧ eth=ð Box=□ square=□ " + + "blacksquare=■ lozenge=◊ blacklozenge=⧫ bigstar=★ colon=: ldotp=. cdotp=· " + + "lbrace={ rbrace=} langle=⟨ rangle=⟩ lfloor=⌊ rfloor=⌋ lceil=⌈ rceil=⌉ vert=| lvert=| rvert=| Vert=‖ " + + "lVert=‖ rVert=‖ lbrack=[ rbrack=] backslash=\\ ulcorner=⌜ urcorner=⌝ llcorner=⌞ lrcorner=⌟"); + Add(Kind.Binary, + "pm=± mp=∓ times=× div=÷ cdot=· ast=∗ circ=∘ bullet=• oplus=⊕ ominus=⊖ otimes=⊗ oslash=⊘ odot=⊙ " + + "cup=∪ cap=∩ sqcup=⊔ sqcap=⊓ vee=∨ lor=∨ wedge=∧ land=∧ setminus=∖ smallsetminus=∖ wr=≀ amalg=⨿ " + + "diamond=⋄ bigcirc=◯ triangleleft=◁ triangleright=▷ bigtriangleup=△ bigtriangledown=▽ uplus=⊎ " + + "dagger=† ddagger=‡ ltimes=⋉ rtimes=⋊ boxplus=⊞ boxminus=⊟ boxtimes=⊠ boxdot=⊡ divideontimes=⋇ " + + "dotplus=∔ barwedge=⊼ veebar=⊻ curlyvee=⋎ curlywedge=⋏ centerdot=· star=⋆"); + Add(Kind.Relation, + "leq=≤ le=≤ geq=≥ ge=≥ leqslant=⩽ geqslant=⩾ leqq=≦ geqq=≧ neq=≠ ne=≠ equiv=≡ approx=≈ approxeq=≊ " + + "cong=≅ sim=∼ simeq=≃ thicksim=∼ thickapprox=≈ propto=∝ varpropto=∝ ll=≪ gg=≫ lll=⋘ ggg=⋙ prec=≺ succ=≻ " + + "preceq=⪯ succeq=⪰ lesssim=≲ gtrsim=≳ lessgtr=≶ gtrless=≷ subset=⊂ supset=⊃ subseteq=⊆ supseteq=⊇ " + + "subsetneq=⊊ supsetneq=⊋ nsubseteq=⊈ nsupseteq=⊉ sqsubset=⊏ sqsupset=⊐ sqsubseteq=⊑ sqsupseteq=⊒ " + + "in=∈ notin=∉ ni=∋ owns=∋ mid=∣ nmid=∤ parallel=∥ nparallel=∦ perp=⊥ models=⊨ vdash=⊢ dashv=⊣ " + + "vDash=⊨ asymp=≍ doteq=≐ doteqdot=≑ triangleq=≜ coloneqq=≔ eqqcolon=≕ bowtie=⋈ smile=⌣ frown=⌢ " + + "nless=≮ ngtr=≯ nleq=≰ ngeq=≱ nsim=≁ ncong=≇ neqsim=≂ nequiv=≢ " + + "to=→ rightarrow=→ gets=← leftarrow=← leftrightarrow=↔ Rightarrow=⇒ Leftarrow=⇐ Leftrightarrow=⇔ " + + "longrightarrow=⟶ longleftarrow=⟵ longleftrightarrow=⟷ Longrightarrow=⟹ Longleftarrow=⟸ " + + "Longleftrightarrow=⟺ implies=⟹ impliedby=⟸ iff=⟺ mapsto=↦ longmapsto=⟼ uparrow=↑ downarrow=↓ " + + "updownarrow=↕ Uparrow=⇑ Downarrow=⇓ Updownarrow=⇕ nearrow=↗ searrow=↘ nwarrow=↖ swarrow=↙ " + + "hookrightarrow=↪ hookleftarrow=↩ rightharpoonup=⇀ rightharpoondown=⇁ leftharpoonup=↼ " + + "leftharpoondown=↽ rightleftharpoons=⇌ leftrightharpoons=⇋ rightrightarrows=⇉ leftleftarrows=⇇ " + + "twoheadrightarrow=↠ rightsquigarrow=⇝ leadsto=⇝ circlearrowleft=↺ circlearrowright=↻"); + return symbols; + } + + /// Large operators: the command EquationScript writes and their character. + private static readonly Dictionary BigOperators = new(StringComparer.Ordinal) + { + ["sum"] = ("SUM", "∑"), + ["prod"] = ("PROD", "∏"), + ["coprod"] = ("COPROD", "∐"), + ["bigcup"] = ("BIGCUP", "⋃"), + ["bigcap"] = ("BIGCAP", "⋂"), + ["bigvee"] = ("BIGVEE", "⋁"), + ["bigwedge"] = ("BIGWEDGE", "⋀"), + ["bigoplus"] = ("BIGOPLUS", "⨁"), + ["bigotimes"] = ("BIGOTIMES", "⨂"), + ["int"] = ("INT", "∫"), + ["intop"] = ("INT", "∫"), + ["iint"] = ("DINT", "∬"), + ["iiint"] = ("TINT", "∭"), + ["oint"] = ("OINT", "∮"), + ["oiint"] = ("ODINT", "∯"), + ["oiiint"] = ("OTINT", "∰"), + }; + + /// Symbols typed as characters (≤, →, ∑): how they space, and the large operators' commands. + private static readonly Dictionary Kinds = Symbols.Values.DistinctBy(v => v.Glyph).ToDictionary(v => v.Glyph, v => v.Kind); + + private static readonly Dictionary BigGlyphs = BigOperators.Values.DistinctBy(v => v.Glyph).ToDictionary(v => v.Glyph, v => v.Command); + + /// Function names 한글 and rhwp know (written upright by themselves). + private static readonly HashSet Functions = new(StringComparer.Ordinal) + { + "sin", "cos", "tan", "cot", "sec", "csc", "arcsin", "arccos", "arctan", "sinh", "cosh", "tanh", "coth", "log", + "ln", "lg", "exp", "det", "dim", "ker", "hom", "arg", "gcd", "lcm", "max", "min", + }; + + /// Other operator names TeX writes upright: as upright letters. + private static readonly HashSet OperatorNames = new(StringComparer.Ordinal) + { + "sup", "inf", "limsup", "liminf", "Pr", "deg", "sgn", "arccot", "arcsec", "arccsc", "csch", "sech", "Tr", "tr", + "rank", "span", "id", "lb", "erf", + }; + + /// Accent commands → the decoration EquationScript and MathLayout know. + private static readonly Dictionary Accents = new(StringComparer.Ordinal) + { + ["hat"] = "hat", + ["widehat"] = "hat", + ["check"] = "check", + ["widecheck"] = "check", + ["tilde"] = "tilde", + ["widetilde"] = "tilde", + ["acute"] = "acute", + ["grave"] = "grave", + ["dot"] = "dot", + ["ddot"] = "ddot", + ["breve"] = "breve", + ["bar"] = "bar", + ["overline"] = "bar", + ["underline"] = "under", + ["vec"] = "vec", + ["overrightarrow"] = "vec", + ["overleftrightarrow"] = "dyad", + ["overgroup"] = "arch", + ["wideparen"] = "arch", + }; + + /// A relation after \not, as its own crossed-out character. + private static readonly Dictionary Negated = new(StringComparer.Ordinal) + { + ["="] = "≠", + ["<"] = "≮", + [">"] = "≯", + ["≤"] = "≰", + ["≥"] = "≱", + ["∈"] = "∉", + ["∋"] = "∌", + ["⊂"] = "⊄", + ["⊃"] = "⊅", + ["⊆"] = "⊈", + ["⊇"] = "⊉", + ["≡"] = "≢", + ["∼"] = "≁", + ["≈"] = "≉", + ["≅"] = "≇", + ["∣"] = "∤", + ["∥"] = "∦", + ["∃"] = "∄", + ["|"] = "∤", + }; + + /// Spacing commands → the width of a MathSpace (1 is half the size, see MathLayout). + private static readonly Dictionary Spaces = new(StringComparer.Ordinal) + { + [","] = 1 / 3.0, + ["thinspace"] = 1 / 3.0, + [":"] = 4 / 9.0, + [">"] = 4 / 9.0, + ["medspace"] = 4 / 9.0, + [";"] = 5 / 9.0, + ["thickspace"] = 5 / 9.0, + [" "] = 2 / 3.0, + ["enspace"] = 1, + ["quad"] = 2, + ["qquad"] = 4, + }; + + /// Commands that change only how TeX lays out or numbers, with how many braced arguments they take. + private static readonly Dictionary Skipped = new(StringComparer.Ordinal) + { + ["displaystyle"] = 0, + ["textstyle"] = 0, + ["scriptstyle"] = 0, + ["scriptscriptstyle"] = 0, + ["limits"] = 0, + ["nolimits"] = 0, + ["nonumber"] = 0, + ["notag"] = 0, + ["relax"] = 0, + ["allowbreak"] = 0, + ["nobreak"] = 0, + ["strut"] = 0, + ["mathstrut"] = 0, + ["!"] = 0, + ["negthinspace"] = 0, + ["negmedspace"] = 0, + ["negthickspace"] = 0, + ["hline"] = 0, + ["hdashline"] = 0, + ["label"] = 1, + ["color"] = 1, + ["vphantom"] = 1, + }; + + /// The tree of a formula's TeX source; null when it is longer than MaxLength or nests deeper than MaxDepth. + public static MathNode? Parse(string tex) + { + if (tex.Length > MaxLength) + return null; + try + { + var tree = new Reader(tex, 0).Top(); + return EquationScript.Deeper(tree, MaxDepth) ? null : tree; + } + catch (TooDeepException) + { + return null; + } + } + + private sealed class TooDeepException : Exception; + + [Flags] + private enum Stops + { + None = 0, + Brace = 1, // } + Bracket = 2, // ] (an optional argument) + Cells = 4, // & and \\ + Right = 8, // \right + End = 16, // \end + } + + /// What ended a row. + private enum Stop + { + Eof, + Brace, + Bracket, + Cell, + Line, + Right, + End, + } + + private sealed class Reader(string s, int depth) + { + private int _i; + private int _depth = depth; + private readonly List _tags = []; + + // ───────────────────────── Structure ───────────────────────── + + /// The whole formula: one row, or lines (\\) and cells (&) as an aligned block. + public MathNode Top() + { + var lines = new List(); + var line = new List(); + var multiline = false; + while (true) + { + var (items, stop) = Row(Stops.Cells); + line.AddRange(items); + if (stop == Stop.Cell) + { + line.Add(new MathAlign()); + multiline = true; + continue; + } + if (stop == Stop.Line) + { + lines.Add(new MathRow(line)); + line = []; + multiline = true; + SkipOptional(); + continue; + } + break; + } + if (line.Count > 0 || lines.Count == 0) + lines.Add(new MathRow(line)); + var tree = multiline && lines.Count > 1 ? new MathPile(lines) : multiline ? lines[0] : Single(line); + if (_tags.Count == 0) + return tree; + // \tag{1}: the number after the formula. + return new MathRow([tree, new MathSpace(4), .. _tags.Select(t => (MathNode)new MathQuoted($"({t})"))]); + } + + private static MathNode Single(List items) => items.Count == 1 ? items[0] : new MathRow(items); + + private void Enter() + { + if (++_depth > MaxDepth) + throw new TooDeepException(); + } + + /// Items up to one of (consumed) or the end. + private (List Items, Stop Stop) Row(Stops stops) + { + Enter(); + try + { + var items = new List(); + List? numerator = null; + (string Kind, bool Bar)? infix = null; + var stop = Stop.Eof; + while (true) + { + SkipSpace(); + if (_i >= s.Length) + break; + var c = s[_i]; + if (c == '}') + { + _i++; + if (stops.HasFlag(Stops.Brace)) + { + stop = Stop.Brace; + break; + } + continue; // an unmatched brace: syntax only + } + if (c == ']' && stops.HasFlag(Stops.Bracket)) + { + _i++; + stop = Stop.Bracket; + break; + } + if (c == '&') + { + _i++; + if (stops.HasFlag(Stops.Cells)) + { + stop = Stop.Cell; + break; + } + items.Add(new MathAlign()); + continue; + } + if (c is '^' or '_') + { + _i++; + Script(items, c == '^'); + continue; + } + if (c == '\'') + { + Primes(items); + continue; + } + if (c == '\\' && _i + 1 < s.Length) + { + var start = _i; + var name = CommandName(); + switch (name) + { + case "\\" or "newline" or "cr": + if (stops.HasFlag(Stops.Cells)) + { + stop = Stop.Line; + goto done; + } + continue; // a line break inside a group breaks nothing + case "right": + if (stops.HasFlag(Stops.Right)) + { + stop = Stop.Right; + goto done; + } + Delimiter(); + continue; + case "end": + RawArgument(); + if (stops.HasFlag(Stops.End)) + { + stop = Stop.End; + goto done; + } + continue; + case "over" or "atop" or "choose" or "brace" or "brack": + if (infix is { } pending) + items = [Infix(numerator!, items, pending)]; + numerator = items; + items = []; + infix = (name, name == "over"); + continue; + case "rm" or "bf" or "it" or "cal" or "sf" or "tt": + { + // Old switches style the rest of the group. + var (rest, restStop) = Row(stops); + items.Add(new MathStyled(SwitchStyle(name), Single(rest))); + stop = restStop; + goto done; + } + } + _i = start; + } + if (Atom(argument: false) is { } atom) + items.Add(atom); + } + done: + if (infix is { } last) + items = [Infix(numerator!, items, last)]; + return (items, stop); + } + finally + { + _depth--; + } + } + + private static string SwitchStyle(string name) => name switch + { + "bf" => "bold", + "it" => "it", + _ => "rm", + }; + + /// a \over b, a \choose b (in brackets), a \atop b. + private static MathNode Infix(List numerator, List denominator, (string Kind, bool Bar) infix) + { + var fraction = new MathFraction(Single(numerator), Single(denominator), infix.Bar); + return infix.Kind switch + { + "choose" => new MathBrackets("(", ")", fraction), + "brace" => new MathBrackets("{", "}", fraction), + "brack" => new MathBrackets("[", "]", fraction), + _ => fraction, + }; + } + + /// A sub- or superscript on the item before (an empty base at the start of a row). + private void Script(List items, bool super) + { + var script = Argument(); + var baseNode = items.Count > 0 ? items[^1] : null; + if (baseNode is not null) + items.RemoveAt(items.Count - 1); + items.Add(Attach(baseNode, super ? null : script, super ? script : null)); + } + + private static MathNode Attach(MathNode? baseNode, MathNode? sub, MathNode? sup) + { + switch (baseNode) + { + case MathBigOperator big when (sub is null || big.Lower is null) && (sup is null || big.Upper is null): + return big with { Lower = sub ?? big.Lower, Upper = sup ?? big.Upper }; + case MathLimit { Lower: null } when sub is not null && sup is null: + return new MathLimit(sub); + case MathScripts scripts when (sub is null || scripts.Sub is null) && (sup is null || scripts.Sup is null): + return scripts with { Sub = sub ?? scripts.Sub, Sup = sup ?? scripts.Sup }; + case MathScripts { Sup: MathRow primes } scripts when sup is not null && primes.Items.All(i => i is MathCommand { Command: "prime" }): + // f'^2: the prime and the power side by side above. + return scripts with { Sup = new MathRow([.. primes.Items, sup]) }; + default: + return new MathScripts(baseNode ?? MathRow.Empty, sub, sup); + } + } + + private void Primes(List items) + { + var primes = new List(); + while (_i < s.Length && s[_i] == '\'') + { + primes.Add(new MathCommand("prime", "′")); + _i++; + } + var baseNode = items.Count > 0 ? items[^1] : null; + if (baseNode is not null) + items.RemoveAt(items.Count - 1); + items.Add(Attach(baseNode, null, new MathRow(primes))); + } + + /// One argument: a group, a command with its own arguments, or one character. + private MathNode Argument() + { + while (true) + { + SkipSpace(); + if (_i >= s.Length || s[_i] is '}' or '&' or ']') + return MathRow.Empty; + if (Atom(argument: true) is { } atom) + return atom; + } + } + + private MathNode Group(Stops stop = Stops.Brace) + { + var (items, _) = Row(stop); + return Single(items); + } + + /// The item at the position: a group, a number, a letter, a symbol or a command; null for one that shows nothing. + private MathNode? Atom(bool argument) + { + Enter(); + try + { + var c = s[_i]; + if (c == '{') + { + _i++; + var (items, _) = Row(Stops.Brace); + return items.Count == 1 ? items[0] : new MathRow(items); + } + if (c == '\\' && _i + 1 < s.Length) + return Command(CommandName()); + if (char.IsAsciiDigit(c)) + { + var start = _i++; + if (!argument) + { + while (_i < s.Length && (char.IsAsciiDigit(s[_i]) || (s[_i] == '.' && _i + 1 < s.Length && char.IsAsciiDigit(s[_i + 1])))) + _i++; + } + return new MathNumber(s[start.._i]); + } + _i++; + if (char.IsHighSurrogate(c) && _i < s.Length && char.IsLowSurrogate(s[_i])) + return new MathText(s.Substring(_i++ - 1, 2)); + return Character(c); + } + finally + { + _depth--; + } + } + + private static MathNode Character(char c) + { + if (char.IsAsciiLetter(c)) + return new MathText(c.ToString()); + switch (c) + { + case '~': + return new MathSpace(Spaces[" "]); + case '#' or '"' or '^' or '_' or '`': + return new MathQuoted(c.ToString()); + case '−' or '–': + return new MathSymbol("-"); + case '+' or '-' or '=' or '<' or '>' or ',' or '.' or '!' or '|' or '/' or '*' or ':' or '(' or ')' or '[' or ']' or ';' or '\'': + return new MathSymbol(c.ToString()); + } + var glyph = c.ToString(); + if (BigGlyphs.TryGetValue(glyph, out var big)) + return new MathBigOperator(big, glyph, null, null); + return Glyph(glyph, Kinds.GetValueOrDefault(glyph)); + } + + /// A symbol: a 한글 equation command where there is one (so the HWPX writer can name it), else its character. + private static MathNode Glyph(string glyph, Kind kind) + { + if (glyph.Length == 1 && OmmlReader.Command(glyph[0]) is { } command) + return new MathCommand(command, glyph); + if (glyph is "(" or ")" or "[" or "]" or "|" or ":" or "." or "/" or "<" or ">" or "=") + return new MathSymbol(glyph); + return kind == Kind.Ordinary ? new MathText(glyph) : new MathSymbol(glyph); + } + + // ───────────────────────── Commands ───────────────────────── + + /// After a backslash: a run of letters, or the one character after it. + private string CommandName() + { + _i++; // the backslash + var start = _i; + while (_i < s.Length && char.IsAsciiLetter(s[_i])) + _i++; + if (_i == start) + { + var length = char.IsHighSurrogate(s[_i]) && _i + 1 < s.Length ? 2 : 1; + _i += length; + return s.Substring(_i - length, length); + } + if (_i < s.Length && s[_i] == '*' && s[start.._i] is "operatorname" or "tag") + _i++; + return s[start.._i].TrimEnd('*'); + } + + private MathNode? Command(string name) + { + if (Symbols.TryGetValue(name, out var symbol)) + return Glyph(symbol.Glyph, symbol.Kind); + if (BigOperators.TryGetValue(name, out var big)) + return new MathBigOperator(big.Command, big.Glyph, null, null); + if (Functions.Contains(name)) + return new MathFunction(name); + if (OperatorNames.Contains(name)) + return Upright(name); + if (Accents.TryGetValue(name, out var accent)) + return new MathAccent(accent, Argument()); + if (Spaces.TryGetValue(name, out var width)) + return new MathSpace(width); + if (Skipped.TryGetValue(name, out var arguments)) + { + for (var a = 0; a < arguments; a++) + RawArgument(); + return null; + } + switch (name) + { + case "{" or "}": + return Glyph(name, Kind.Ordinary); + case "|": + return new MathSymbol("‖"); + case "#" or "$" or "%" or "&" or "_": + return new MathQuoted(name); + case "lim": + return new MathLimit(null); + case "frac" or "dfrac" or "tfrac" or "cfrac": + if (name == "cfrac") + SkipOptional(); + return new MathFraction(Argument(), Argument()); + case "binom" or "dbinom" or "tbinom": + return new MathBrackets("(", ")", new MathFraction(Argument(), Argument(), Bar: false)); + case "sqrt": + { + MathNode? index = null; + SkipSpace(); + if (_i < s.Length && s[_i] == '[') + { + _i++; + index = Group(Stops.Bracket); + } + return new MathRoot(index, Argument()); + } + case "left": + { + var open = Delimiter(); + var (items, stop) = Row(Stops.Right); + var close = stop == Stop.Right ? Delimiter() : ""; + return Brackets(open, Single(items), close); + } + case "middle": + return Glyph(Delimiter() is { Length: > 0 } middle ? middle : "|", Kind.Relation); + case "big" or "Big" or "bigg" or "Bigg" or "bigl" or "Bigl" or "biggl" or "Biggl" or "bigr" or "Bigr" or "biggr" + or "Biggr" or "bigm" or "Bigm" or "biggm" or "Biggm": + return Delimiter() is { Length: > 0 } delimiter ? Glyph(delimiter, Kind.Ordinary) : null; + case "begin": + return Environment(RawArgument()); + case "text" or "textrm" or "textnormal" or "mbox" or "hbox" or "textup": + return Text(RawArgument(), null); + case "textbf": + return Text(RawArgument(), "bold"); + case "textit" or "emph" or "textsl": + return Text(RawArgument(), "it"); + case "texttt" or "textsf": + return Text(RawArgument(), null); + case "mathrm" or "mathup" or "mathsf" or "mathtt": + return new MathStyled("rm", Argument()); + case "mathnormal" or "mathit": + return new MathStyled("it", Argument()); + case "mathbf" or "mathbfup": + return new MathStyled("bold", new MathStyled("rm", Argument())); + case "boldsymbol" or "bm" or "pmb": + return new MathStyled("bold", Argument()); + case "mathbb" or "Bbb": + return Alphabet(RawArgument(), Alphabets.DoubleStruck); + case "mathcal" or "mathscr": + return Alphabet(RawArgument(), Alphabets.Script); + case "mathfrak": + return Alphabet(RawArgument(), Alphabets.Fraktur); + case "operatorname" or "mathop": + { + var raw = RawArgument(); + return Functions.Contains(raw) ? new MathFunction(raw) + : raw == "lim" ? new MathLimit(null) + : new MathStyled("rm", new Reader(raw, _depth).Top()); + } + case "bmod" or "mod": + return new MathRow([new MathSpace(Spaces[";"]), Upright("mod"), new MathSpace(Spaces[";"])]); + case "pmod": + return new MathRow([new MathSpace(Spaces["quad"] / 2), new MathSymbol("("), Upright("mod"), new MathSpace(Spaces[","]), Argument(), new MathSymbol(")")]); + case "not": + { + var negated = Argument(); + var text = negated switch + { + MathSymbol plain => plain.Text, + MathCommand command => command.Glyph, + MathText letter => letter.Text, + _ => null, + }; + return text is not null && Negated.TryGetValue(text, out var crossed) ? Glyph(crossed, Kind.Relation) : new MathAccent("not", negated); + } + case "overset" or "stackrel": + { + var over = Argument(); + return new MathScripts(Argument(), null, over); + } + case "underset": + { + var under = Argument(); + return new MathScripts(Argument(), under, null); + } + case "xrightarrow" or "xleftarrow" or "xLeftarrow" or "xRightarrow" or "xleftrightarrow": + { + MathNode? below = null; + SkipSpace(); + if (_i < s.Length && s[_i] == '[') + { + _i++; + below = Group(Stops.Bracket); + } + var arrow = name switch + { + "xleftarrow" => "←", + "xLeftarrow" => "⇐", + "xRightarrow" => "⇒", + "xleftrightarrow" => "↔", + _ => "→", + }; + return new MathScripts(Glyph(arrow, Kind.Relation), below, Argument()); + } + case "substack": + { + var rows = new List(); + SkipSpace(); + if (_i < s.Length && s[_i] == '{') + { + _i++; + while (true) + { + var (items, stop) = Row(Stops.Brace | Stops.Cells); + rows.Add(Single(items)); + if (stop != Stop.Line) + break; + } + } + return rows.Count == 1 ? rows[0] : new MathPile(rows); + } + case "boxed" or "fbox" or "overbrace" or "underbrace" or "mathclap" or "mathllap" or "mathrlap" or "smash" + or "displaylimits" or "ensuremath": + return name == "displaylimits" ? null : Argument(); + case "textcolor" or "colorbox": + RawArgument(); + return Argument(); + case "phantom" or "hphantom": + Argument(); + return new MathSpace(1); + case "hspace" or "mspace" or "hskip" or "kern" or "mkern" or "mskip": + if (name is "hspace" or "mspace") + RawArgument(); + else + Dimension(); + return new MathSpace(1); + case "tag": + _tags.Add(RawArgument()); + return null; + } + // Not known: kept as its text; arguments it takes follow as groups. + return new MathQuoted("\\" + name); + } + + private static MathNode Upright(string name) => new MathStyled("rm", new MathRow([.. name.Select(c => (MathNode)new MathText(c.ToString()))])); + + private static MathNode Brackets(string open, MathNode body, string close) + { + if (EquationScript.IsBracket(open) && EquationScript.IsBracket(close)) + return new MathBrackets(open, close, body); + // Brackets the tree can't stretch (arrows, slashes): characters around the content. + var row = new List(); + if (open.Length > 0) + row.Add(Glyph(open, Kind.Ordinary)); + row.Add(body); + if (close.Length > 0) + row.Add(Glyph(close, Kind.Ordinary)); + return new MathRow(row); + } + + /// The delimiter after \left, \right, \middle or \big: its character, "" for "." or nothing. + private string Delimiter() + { + SkipSpace(); + if (_i >= s.Length) + return ""; + var c = s[_i]; + if (c == '\\' && _i + 1 < s.Length) + { + var name = CommandName(); + return name switch + { + "{" or "lbrace" => "{", + "}" or "rbrace" => "}", + "|" or "Vert" or "lVert" or "rVert" => "‖", + "vert" or "lvert" or "rvert" => "|", + _ => Symbols.TryGetValue(name, out var symbol) ? symbol.Glyph : "", + }; + } + if (c is '{' or '}' or '&') + return ""; + _i++; + return c switch + { + '.' => "", + '<' => "⟨", + '>' => "⟩", + _ => char.IsHighSurrogate(c) || char.IsLetterOrDigit(c) ? "" : c.ToString(), + }; + } + + // ───────────────────────── Environments ───────────────────────── + + private MathNode Environment(string name) + { + var bare = name.TrimEnd('*'); + if (bare is "array" or "subarray" or "alignat" or "alignedat") + RawArgument(); // the column spec or the number of columns + else + SkipOptional(); // [t], [c] (aligned, gathered), [r] (mathtools' matrix*) + var rows = new List>(); + var cells = new List(); + while (true) + { + var (items, stop) = Row(Stops.Cells | Stops.End); + cells.Add(Single(items)); + if (stop == Stop.Cell) + continue; + rows.Add(cells); + cells = []; + if (stop != Stop.Line) + break; + SkipOptional(); + } + // A \\ before \end leaves an empty last row. + if (rows.Count > 1 && rows[^1] is [MathRow { Items.Count: 0 }]) + rows.RemoveAt(rows.Count - 1); + + IReadOnlyList> Grid() => rows.Select(r => (IReadOnlyList)r).ToList(); + switch (bare) + { + case "matrix" or "array" or "smallmatrix" or "subarray": + return new MathMatrix("matrix", Grid()); + case "pmatrix": + return new MathMatrix("pmatrix", Grid()); + case "bmatrix": + return new MathMatrix("bmatrix", Grid()); + case "vmatrix": + return new MathMatrix("dmatrix", Grid()); + case "Bmatrix": + return new MathBrackets("{", "}", new MathMatrix("matrix", Grid())); + case "Vmatrix": + return new MathBrackets("‖", "‖", new MathMatrix("matrix", Grid())); + case "cases" or "dcases" or "numcases": + return new MathCases(Grid()); + case "rcases": + return new MathBrackets("", "}", new MathMatrix("matrix", Grid())); + case "aligned" or "align" or "alignat" or "alignedat" or "split" or "eqnarray" or "flalign" or "darray": + return Lines(rows, aligned: true); + case "gather" or "gathered" or "multline" or "equation" or "displaymath" or "math": + return Lines(rows, aligned: false); + } + // Not known: its name kept as text around the lines. + return new MathRow([new MathQuoted($"\\begin{{{name}}}"), Lines(rows, aligned: true), new MathQuoted($"\\end{{{name}}}")]); + } + + /// Lines of an aligned block, the cells of each joined by alignment marks; one line by itself. + private static MathNode Lines(List> rows, bool aligned) + { + var lines = rows.Select(cells => + { + var items = new List(); + for (var c = 0; c < cells.Count; c++) + { + if (c > 0 && aligned) + items.Add(new MathAlign()); + if (cells[c] is MathRow row) + items.AddRange(row.Items); + else + items.Add(cells[c]); + } + return (MathNode)new MathRow(items); + }).ToList(); + return lines.Count == 1 ? lines[0] : new MathPile(lines); + } + + // ───────────────────────── Text ───────────────────────── + + /// \text{…}: upright text with its spaces, and $…$ inside it as math. + private MathNode Text(string raw, string? style) + { + var items = new List(); + var text = new StringBuilder(); + void Flush() + { + if (text.Length > 0) + items.Add(new MathQuoted(text.ToString())); + text.Clear(); + } + for (var i = 0; i < raw.Length; i++) + { + var c = raw[i]; + if (c == '$') + { + var end = raw.IndexOf('$', i + 1); + if (end > i + 1) + { + Flush(); + var inner = new Reader(raw[(i + 1)..end], _depth + 1).Top(); + items.Add(inner); + i = end; + continue; + } + } + if (c is '{' or '}') + continue; + if (c == '~') + { + text.Append(' '); + continue; + } + if (c == '\\' && i + 1 < raw.Length) + { + var start = ++i; + while (i < raw.Length && char.IsAsciiLetter(raw[i])) + i++; + if (i == start) + { + // \%, \&, \_, \{, \ …; \\ breaks nothing in a line of text. + if (raw[i] != '\\') + text.Append(raw[i]); + continue; + } + var command = raw[start..i]; + i--; + text.Append(command switch + { + "quad" => " ", + "qquad" => "  ", + "textbackslash" => "\\", + "textasciitilde" => "~", + "textasciicircum" => "^", + "textendash" => "–", + "textemdash" => "—", + "ldots" or "dots" or "textellipsis" => "…", + _ => "\\" + command, + }); + continue; + } + text.Append(char.IsControl(c) ? ' ' : c); + } + Flush(); + var node = Single(items); + return style is null ? node : new MathStyled(style, node); + } + + private enum Alphabets + { + DoubleStruck, + Script, + Fraktur, + } + + /// \mathbb{R}, \mathcal{L}, \mathfrak{g}: the letters of the Unicode math alphabet. + private static MathNode Alphabet(string raw, Alphabets alphabet) + { + var items = new List(); + foreach (var c in raw) + { + if (char.IsWhiteSpace(c) || c is '{' or '}') + continue; + items.Add(char.IsAsciiLetter(c) || (char.IsAsciiDigit(c) && alphabet == Alphabets.DoubleStruck) + ? new MathText(Letter(c, alphabet)) + : Character(c)); + } + return Single(items); + } + + private static string Letter(char c, Alphabets alphabet) + { + var exception = (alphabet, c) switch + { + (Alphabets.DoubleStruck, 'C') => 'ℂ', + (Alphabets.DoubleStruck, 'H') => 'ℍ', + (Alphabets.DoubleStruck, 'N') => 'ℕ', + (Alphabets.DoubleStruck, 'P') => 'ℙ', + (Alphabets.DoubleStruck, 'Q') => 'ℚ', + (Alphabets.DoubleStruck, 'R') => 'ℝ', + (Alphabets.DoubleStruck, 'Z') => 'ℤ', + (Alphabets.Script, 'B') => 'ℬ', + (Alphabets.Script, 'E') => 'ℰ', + (Alphabets.Script, 'F') => 'ℱ', + (Alphabets.Script, 'H') => 'ℋ', + (Alphabets.Script, 'I') => 'ℐ', + (Alphabets.Script, 'L') => 'ℒ', + (Alphabets.Script, 'M') => 'ℳ', + (Alphabets.Script, 'R') => 'ℛ', + (Alphabets.Script, 'e') => 'ℯ', + (Alphabets.Script, 'g') => 'ℊ', + (Alphabets.Script, 'o') => 'ℴ', + (Alphabets.Fraktur, 'C') => 'ℭ', + (Alphabets.Fraktur, 'H') => 'ℌ', + (Alphabets.Fraktur, 'I') => 'ℑ', + (Alphabets.Fraktur, 'R') => 'ℜ', + (Alphabets.Fraktur, 'Z') => 'ℨ', + _ => '\0', + }; + if (exception != '\0') + return exception.ToString(); + if (char.IsAsciiDigit(c)) + return char.ConvertFromUtf32(0x1D7D8 + (c - '0')); + var capitals = alphabet switch + { + Alphabets.DoubleStruck => 0x1D538, + Alphabets.Script => 0x1D49C, + _ => 0x1D504, + }; + return char.ConvertFromUtf32(capitals + (char.IsAsciiLetterUpper(c) ? c - 'A' : 26 + c - 'a')); + } + + // ───────────────────────── Characters ───────────────────────── + + /// Skips spaces and % comments. + private void SkipSpace() + { + while (_i < s.Length) + { + if (char.IsWhiteSpace(s[_i])) + { + _i++; + } + else if (s[_i] == '%') + { + while (_i < s.Length && s[_i] != '\n') + _i++; + } + else + { + break; + } + } + } + + /// An optional [ … ] argument, skipped. + private void SkipOptional() + { + SkipSpace(); + if (_i >= s.Length || s[_i] != '[') + return; + var end = s.IndexOf(']', _i); + _i = end < 0 ? s.Length : end + 1; + } + + /// A braced argument as its text (nested braces kept), or the next character or command. + private string RawArgument() + { + SkipSpace(); + if (_i >= s.Length) + return ""; + if (s[_i] != '{') + { + if (s[_i] == '\\' && _i + 1 < s.Length) + return "\\" + CommandName(); + return s[_i] is '}' or '&' ? "" : s[_i++].ToString(); + } + var start = ++_i; + var level = 1; + while (_i < s.Length) + { + var c = s[_i]; + if (c == '\\') + { + _i += 2; + continue; + } + if (c == '{') + level++; + else if (c == '}' && --level == 0) + break; + _i++; + } + var end = Math.Min(_i, s.Length); + _i = Math.Min(_i + 1, s.Length); + return s[start..end]; + } + + /// A TeX dimension after \kern or \mskip ("3mu", "-0.5em"), skipped. + private void Dimension() + { + SkipSpace(); + while (_i < s.Length && (char.IsAsciiDigit(s[_i]) || s[_i] is '.' or '-' or '+')) + _i++; + var letters = 0; + while (_i < s.Length && letters < 2 && char.IsAsciiLetter(s[_i])) + { + _i++; + letters++; + } + } + } +} diff --git a/src/Filee.Engines/Hwp/Hwpx/MarkdownMath.cs b/src/Filee.Engines/Hwp/Hwpx/MarkdownMath.cs new file mode 100644 index 000000000..55321f4d3 --- /dev/null +++ b/src/Filee.Engines/Hwp/Hwpx/MarkdownMath.cs @@ -0,0 +1,215 @@ +// TeX math in Markdown (#1652), for Markdig: where it starts and ends, and how Markdown → HTML writes it (MathML). +// +// Markdig's own math reads $ … $ and $$ … $$; this reads those and \( … \) (inline) and \[ … \] (display) too, with the +// rules Pandoc uses for dollars: an opening $ is followed by a non-space, a closing $ follows a non-space and is not +// followed by a digit ("$5 and $10" stays text), and a $ or \( escaped with a backslash is text. A code span wins over +// math: "$a `b$` c" is a code span, as backticks bind before every other inline. $$ … $$ on lines of their own is a +// math block (Markdig's MathBlockParser). +// +// The HTML renderer writes MathML with the TeX as its annotation; a formula LatexMath doesn't read stays its source. + +using System.Net; +using Markdig; +using Markdig.Extensions.Mathematics; +using Markdig.Helpers; +using Markdig.Parsers; +using Markdig.Parsers.Inlines; +using Markdig.Renderers; +using Markdig.Renderers.Html; +using Markdig.Syntax; + +namespace Filee.Engines.Hwp.Hwpx; + +/// Adds in place of Markdig's inline math, and MathML to the HTML renderer. +internal sealed class TexMathExtension : IMarkdownExtension +{ + public void Setup(MarkdownPipelineBuilder pipeline) + { + pipeline.InlineParsers.TryRemove(); + if (!pipeline.InlineParsers.Contains()) + { + // Before the escape parser, which would otherwise read \( and \[ as escaped brackets. + if (!pipeline.InlineParsers.InsertBefore(new TexMathInlineParser())) + pipeline.InlineParsers.Insert(0, new TexMathInlineParser()); + } + } + + public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) + { + if (renderer is not HtmlRenderer html) + return; + html.ObjectRenderers.TryRemove(); + html.ObjectRenderers.TryRemove(); + if (!html.ObjectRenderers.Contains()) + html.ObjectRenderers.Insert(0, new MathMLInlineRenderer()); + if (!html.ObjectRenderers.Contains()) + html.ObjectRenderers.Insert(0, new MathMLBlockRenderer()); + } +} + +/// $ … $, $$ … $$, \( … \) and \[ … \] as (DelimiterCount 2 for display math). +internal sealed class TexMathInlineParser : InlineParser +{ + public TexMathInlineParser() => OpeningCharacters = ['$', '\\']; + + public override bool Match(InlineProcessor processor, ref StringSlice slice) + { + var text = slice.Text; + var start = slice.Start; + var end = slice.End; // inclusive + int contentStart, contentEnd, after; // content [contentStart, contentEnd), the text after the closing delimiter + bool display; + char delimiter; + if (slice.CurrentChar == '\\') + { + var kind = slice.PeekChar(); + if (kind is not ('(' or '[')) + return false; + display = kind == '['; + var close = display ? ']' : ')'; + contentStart = start + 2; + var i = contentStart; + for (; i < end; i++) + { + if (text[i] != '\\') + continue; + if (text[i + 1] == close) + break; + i++; // \\, \{, \$ … inside the math + } + if (i >= end) + return false; + contentEnd = i; + after = i + 2; + delimiter = '\\'; + } + else + { + display = start < end && text[start + 1] == '$'; + contentStart = start + (display ? 2 : 1); + if (contentStart > end || (!display && char.IsWhiteSpace(text[contentStart]))) + return false; + var i = contentStart; + var found = false; + for (; i <= end; i++) + { + var c = text[i]; + if (c == '\\') + { + i++; + continue; + } + if (c != '$') + continue; + if (display) + { + if (i < end && text[i + 1] == '$') + { + found = true; + break; + } + continue; + } + if (!char.IsWhiteSpace(text[i - 1]) && (i == end || !char.IsAsciiDigit(text[i + 1]))) + { + found = true; + break; + } + } + if (!found) + return false; + contentEnd = i; + after = i + (display ? 2 : 1); + delimiter = '$'; + } + + if (contentEnd <= contentStart || text.AsSpan(contentStart, contentEnd - contentStart).IsWhiteSpace()) + return false; + if (CodeSpanCrosses(text, contentStart, contentEnd, end)) + return false; + + var math = new MathInline + { + Delimiter = delimiter, + DelimiterCount = display ? 2 : 1, + Content = new StringSlice(text, contentStart, contentEnd - 1), + Span = new SourceSpan(processor.GetSourcePosition(start, out var line, out var column), processor.GetSourcePosition(after - 1)), + Line = line, + Column = column, + }; + processor.Inline = math; + slice.Start = after; + return true; + } + + /// + /// Whether a code span starts inside the math and ends after it: a run of backticks in the content with a run as + /// long later in the paragraph, so the backticks win. + /// + private static bool CodeSpanCrosses(string text, int contentStart, int contentEnd, int end) + { + for (var i = contentStart; i < contentEnd; i++) + { + if (text[i] != '`') + continue; + var run = 1; + while (i + run <= end && text[i + run] == '`') + run++; + for (var j = i + run; j <= end; j++) + { + if (text[j] != '`') + continue; + var other = 1; + while (j + other <= end && text[j + other] == '`') + other++; + if (other == run) + return j + run > contentEnd; + j += other - 1; + } + i += run - 1; + } + return false; + } +} + +/// Inline (and $$ display) math as MathML; the source when it isn't read. +internal sealed class MathMLInlineRenderer : HtmlObjectRenderer +{ + protected override void Write(HtmlRenderer renderer, MathInline obj) => + MarkdownMath.Write(renderer, obj.Content.ToString(), obj.DelimiterCount == 2, obj.Delimiter, block: false); +} + +/// A $$ … $$ math block as display MathML. +internal sealed class MathMLBlockRenderer : HtmlObjectRenderer +{ + protected override void Write(HtmlRenderer renderer, MathBlock obj) + { + renderer.EnsureLine(); + MarkdownMath.Write(renderer, obj.Lines.ToString(), display: true, '$', block: true); + renderer.EnsureLine(); + } +} + +internal static class MarkdownMath +{ + /// The source of a formula as written: "$x$", "$$x$$", "\(x\)", "\[x\]". + public static string Source(string tex, bool display, char delimiter) => delimiter == '\\' + ? (display ? $"\\[{tex}\\]" : $"\\({tex}\\)") + : (display ? $"$${tex}$$" : $"${tex}$"); + + public static void Write(HtmlRenderer renderer, string tex, bool display, char delimiter, bool block) + { + var tree = renderer.EnableHtmlForInline ? LatexMath.Parse(tex.Trim()) : null; + if (tree is null) + { + // Plain text, or math that isn't read: its source. + if (block && renderer.EnableHtmlForBlock) + renderer.Write("

"); + renderer.Write(renderer.EnableHtmlEscape ? WebUtility.HtmlEncode(Source(tex, display, delimiter)) : Source(tex, display, delimiter)); + if (block && renderer.EnableHtmlForBlock) + renderer.Write("

"); + return; + } + renderer.Write(MathMLWriter.Write(tree, display, tex.Trim())); + } +} diff --git a/src/Filee.Engines/Hwp/Hwpx/MarkdownReader.cs b/src/Filee.Engines/Hwp/Hwpx/MarkdownReader.cs index e0aea1b74..24c16b31a 100644 --- a/src/Filee.Engines/Hwp/Hwpx/MarkdownReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/MarkdownReader.cs @@ -3,12 +3,15 @@ // Mirrors PandocAstReader: headings (with their ids as bookmarks), paragraphs, bold / italic / strike / sub- and // superscript / inserted / marked text, code, lists (1. / a. / i. / A. / I. with the start number, nested levels), // task lists, block quotes, pipe and grid tables with spans and column alignment (as the cells' paragraph alignment), -// definition lists, links, images, footnotes and math. The YAML front matter's "title", "subtitle", "author" and "date" +// definition lists, links, images, footnotes and TeX math (#1652: $ … $, $$ … $$, \( … \), \[ … \], see MarkdownMath), +// read by LatexMath into equations: a display equation alone in its paragraph is centred, and math LatexMath doesn't +// read stays its source as text. Equations count against DocumentLimits.MaxObjects. The YAML front matter's "title", "subtitle", "author" and "date" // become the document's metadata. Raw HTML is skipped, except
. For the PPTX writer, blocks keep what they were // (HBlockRole): code lines, thematic breaks and the content of Pandoc-style "::: notes" containers (speaker notes). A // code block is one shaded box in a monospace font: a borderless one-column table with a row per line, so it can break // across pages. +using Filee.Engines.Documents; using Filee.Engines.Infrastructure; using Markdig; using Markdig.Extensions.CustomContainers; @@ -39,6 +42,7 @@ internal sealed class MarkdownReader public static readonly MarkdownPipeline Pipeline = new MarkdownPipelineBuilder() .UseAdvancedExtensions() .UseYamlFrontMatter() + .Use() .Build(); private static readonly HCharFormat Plain = default; @@ -62,11 +66,15 @@ internal sealed class MarkdownReader /// A folder of pictures Filee wrote itself (a notebook's attachments), allowed besides the resource root. private readonly string? _mediaFolder; - private MarkdownReader(string baseFolder, string? resourceRoot, string? mediaFolder = null) + /// Counts the equations (a file of a million "$x$" would lay out a million of them). + private readonly DocumentBudget _budget; + + private MarkdownReader(string baseFolder, string? resourceRoot, string? mediaFolder, DocumentLimits? limits) { _baseFolder = baseFolder; _resourceRoot = resourceRoot; _mediaFolder = mediaFolder; + _budget = new DocumentBudget(limits ?? DocumentLimits.Default); } /// Parses Markdown text. @@ -77,10 +85,11 @@ public static HDocument Read(string markdown, string baseFolder, string? resourc Read(markdown, baseFolder, resourceRoot, mediaFolder: null); /// A folder of pictures Filee wrote itself, which images may name too (file: URLs). - internal static HDocument Read(string markdown, string baseFolder, string? resourceRoot, string? mediaFolder) + /// Limits of what is read; default . + internal static HDocument Read(string markdown, string baseFolder, string? resourceRoot, string? mediaFolder, DocumentLimits? limits = null) { var parsed = Markdown.Parse(markdown, Pipeline); - var reader = new MarkdownReader(baseFolder, resourceRoot, mediaFolder); + var reader = new MarkdownReader(baseFolder, resourceRoot, mediaFolder, limits); var section = new HSection(); reader.Blocks(parsed, section.Blocks, new Context()); var document = new HDocument(); @@ -123,8 +132,22 @@ private void Block(Block block, List output, Context context) Blocks(quote, output, context with { Indent = context.Indent + QuoteIndent, Quote = true }); break; case MathBlock math: - CodeLines(math, output, context, Plain with { Italic = true }, HBlockRole.Body); - break; + { + // $$ on lines of their own: a centred display equation; its source as before when it isn't read. + var tex = math.Lines.ToString(); + if (Equation(tex, display: true, '$', Plain) is HEquation equation) + { + var paragraph = NewParagraph(context); + paragraph.Format = paragraph.Format with { Align = HAlign.Center }; + paragraph.Inlines.Add(equation); + output.Add(paragraph); + } + else + { + CodeLines(math, output, context, Plain with { Italic = true }, HBlockRole.Body); + } + break; + } case CodeBlock code: // Speaker notes keep their lines as notes; elsewhere one shaded box of code lines. if (context.Role == HBlockRole.Body) @@ -181,9 +204,53 @@ private HParagraph Paragraph(ContainerInline? inlines, Context context) { var paragraph = NewParagraph(context); Inlines(inlines, paragraph.Inlines, Plain); + DisplayMath(paragraph); return paragraph; } + /// + /// A paragraph of display math only ($$x$$, \[x\], one or more on their lines): centred, one equation per line. + /// Display math among text stays in its line, as an inline equation. + /// + private static void DisplayMath(HParagraph paragraph) + { + if (!paragraph.Inlines.Any(i => i is HEquation { Display: true })) + return; + var alone = paragraph.Inlines.All(i => i is HEquation { Display: true } or HLineBreak or HBookmark + || (i is HText { Text: var text } and not HEquation && string.IsNullOrWhiteSpace(text))); + if (alone) + { + var kept = paragraph.Inlines.Where(i => i is HEquation or HBookmark).ToList(); + paragraph.Inlines.Clear(); + foreach (var inline in kept) + { + if (inline is HEquation && paragraph.Inlines.Any(i => i is HEquation)) + paragraph.Inlines.Add(new HLineBreak(Plain)); + paragraph.Inlines.Add(inline); + } + paragraph.Format = paragraph.Format with { Align = HAlign.Center }; + return; + } + for (var i = 0; i < paragraph.Inlines.Count; i++) + { + if (paragraph.Inlines[i] is HEquation { Display: true } display) + paragraph.Inlines[i] = new HEquation(display.Math, display.Format, display.Text) { Tex = display.Tex }; + } + } + + /// + /// TeX math as an equation; its source as text when LatexMath doesn't read it (too long or nested too deep). + /// + private HText Equation(string tex, bool display, char delimiter, HCharFormat format) + { + var source = MarkdownMath.Source(tex, display, delimiter); + var trimmed = tex.Trim(); + if (LatexMath.Parse(trimmed) is not { } tree) + return new HText(source, format); + _budget.Object(); + return new HEquation(tree, format, source) { Tex = trimmed, Display = display }; + } + private HParagraph Heading(HeadingBlock heading, Context context) { var paragraph = Paragraph(heading.Inline, context); @@ -429,7 +496,7 @@ private void Inlines(ContainerInline? container, List output, HCharForm output.Add(new HText(task.Checked ? "☑" : "☐", format)); // the following text starts with a space break; case MathInline math: - output.Add(new HText(math.Content.ToString(), format with { Italic = true })); + output.Add(Equation(math.Content.ToString(), math.DelimiterCount == 2, math.Delimiter, format)); break; case FootnoteLink { IsBackLink: false } footnote: { diff --git a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs index d743a0d33..da77087b9 100644 --- a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs @@ -208,6 +208,9 @@ private string Inlines(List inlines) { switch (inline) { + case HEquation { Tex: not null } equation: + sb.Append(equation.Text); // TeX math as it was written ($x$, \[x\]), not escaped + break; case HText text: sb.Append(Run(text.Text, text.Format)); break; diff --git a/src/Filee.Engines/Hwp/Hwpx/MathMLWriter.cs b/src/Filee.Engines/Hwp/Hwpx/MathMLWriter.cs new file mode 100644 index 000000000..51b6990bf --- /dev/null +++ b/src/Filee.Engines/Hwp/Hwpx/MathMLWriter.cs @@ -0,0 +1,350 @@ +// The equation tree (MathNode) → MathML (MathML Core, which browsers and EPUB 3 readers draw), for the HTML and EPUB +// writers and Markdown → HTML. An equation read from TeX keeps its source as the TeX annotation (and alttext), so a page +// can be copied back to TeX and readers without MathML have the source to show. +// +// Letters are (italic), several letters each their own unless upright; numbers ; operators, relations and +// brackets (unbracketed "(" and ")" don't stretch, as in TeX); quoted text ; Greek capitals upright. +// Fractions, roots, scripts, large operators (limits above and below, at the side for integrals), lim, matrices in +// their brackets, cases, piles and aligned lines (, the cells of a line at its & marks, right and left in +// turn), stretched brackets, accents and spaces. Bold and upright follow MathStyled. + +using System.Globalization; +using System.Text; +using Filee.Engines.Hwp.Hwpx.Docx; + +namespace Filee.Engines.Hwp.Hwpx; + +internal static class MathMLWriter +{ + private const string Namespace = "http://www.w3.org/1998/Math/MathML"; + + /// Symbols that are things rather than operators: identifiers in MathML. + private static readonly HashSet Ordinary = + [ + "∞", "∂", "∇", "∅", "ℏ", "ℓ", "ℵ", "℘", "°", "∠", "△", "⊤", "ℜ", "ℑ", "ı", "ȷ", "♠", "♣", "♡", "♢", "♭", "♮", "♯", + "†", "‡", "§", "¶", "∡", "ℶ", "ℷ", "□", "■", "✓", "€", "£", "¥", "©", + ]; + + private readonly record struct Style(bool Upright, bool Bold); + + /// One equation as a <math> element; (its TeX source) becomes its annotation. + public static string Write(MathNode node, bool display, string? tex = null) + { + var sb = new StringBuilder(); + sb.Append("'); + if (tex is not null) + sb.Append(""); + Group(node, sb, default); + if (tex is not null) + sb.Append("").Append(OmmlWriter.Escape(tex)).Append(""); + sb.Append(""); + return sb.ToString(); + } + + /// A node as one element (an <mrow> around several), as the children of mfrac, msub … must be. + private static void Group(MathNode node, StringBuilder sb, Style style) + { + sb.Append(""); + Node(node, sb, style); + sb.Append(""); + } + + private static void Node(MathNode node, StringBuilder sb, Style style) + { + switch (node) + { + case MathRow row: + foreach (var item in row.Items) + Node(item, sb, style); + break; + case MathText text: + Text(text.Text, sb, style); + break; + case MathNumber number: + Token("mn", number.Digits, sb, style); + break; + case MathSymbol symbol: + Operator(symbol.Text == "-" ? "−" : symbol.Text, sb, style); + break; + case MathCommand command: + if (IsGreek(command.Glyph) || Ordinary.Contains(command.Glyph)) + Text(command.Glyph, sb, style); + else + Operator(command.Glyph, sb, style); + break; + case MathQuoted quoted: + // Spaces at the ends of a token are dropped: kept as no-break spaces. + Token("mtext", quoted.Text.Length > 0 && (quoted.Text[0] == ' ' || quoted.Text[^1] == ' ') + ? quoted.Text.Replace(' ', ' ') : quoted.Text, sb, style with { Upright = true }); + break; + case MathFunction function: + // sin x, det A: a thin space after the name, as TeX sets it. + Token("mi", function.Name, sb, style with { Upright = true }); + sb.Append(""); + break; + case MathSpace space: + sb.Append(""); + break; + case MathAlign: + break; + case MathFraction fraction: + sb.Append(fraction.Bar ? "" : ""); + Group(fraction.Numerator, sb, style); + Group(fraction.Denominator, sb, style); + sb.Append(""); + break; + case MathRoot root: + if (root.Index is null) + { + sb.Append(""); + Node(root.Body, sb, style); + sb.Append(""); + } + else + { + sb.Append(""); + Group(root.Body, sb, style); + Group(root.Index, sb, style); + sb.Append(""); + } + break; + case MathScripts scripts: + Scripts(scripts.Base, scripts.Sub, scripts.Sup, sb, style, under: false); + break; + case MathBigOperator big: + Scripts(new MathSymbol(big.Glyph), big.Lower, big.Upper, sb, style, under: !big.Integral, large: true); + break; + case MathLimit limit: + if (limit.Lower is null) + { + sb.Append("lim"); + } + else + { + sb.Append("lim"); + Group(limit.Lower, sb, style); + sb.Append(""); + } + break; + case MathMatrix matrix: + { + var (open, close) = matrix.Command switch + { + "pmatrix" => ("(", ")"), + "bmatrix" => ("[", "]"), + "dmatrix" or "vmatrix" => ("|", "|"), + _ => ("", ""), + }; + sb.Append(""); + Fence(open, sb); + Table(matrix.Rows, sb, style, _ => "center"); + Fence(close, sb); + sb.Append(""); + break; + } + case MathCases cases: + sb.Append(""); + Fence("{", sb); + Table(cases.Rows, sb, style, _ => "left"); + sb.Append(""); + break; + case MathPile pile: + { + var rows = pile.Rows.Select(Cells).ToList(); + if (rows.Any(r => r.Count > 1)) + Table(rows, sb, style, column => column % 2 == 0 ? "right" : "left"); // aligned: =, ≤ … at the marks + else + Table(rows, sb, style, _ => pile.Align switch { "lpile" => "left", "rpile" => "right", _ => "center" }); + break; + } + case MathBrackets brackets: + sb.Append(""); + Fence(brackets.Left, sb); + Node(brackets.Body, sb, style); + Fence(brackets.Right, sb); + sb.Append(""); + break; + case MathAccent accent: + Accent(accent, sb, style); + break; + case MathStyled styled: + Node(styled.Body, sb, styled.Command switch + { + "rm" => style with { Upright = true }, + "it" => style with { Upright = false }, + "bold" => style with { Bold = true }, + _ => style, + }); + break; + } + } + + /// A line of a pile split at its alignment marks. + private static IReadOnlyList Cells(MathNode line) + { + if (line is not MathRow row || !row.Items.Any(i => i is MathAlign)) + return [line]; + var cells = new List(); + var cell = new List(); + foreach (var item in row.Items) + { + if (item is MathAlign) + { + cells.Add(new MathRow(cell)); + cell = []; + } + else + { + cell.Add(item); + } + } + cells.Add(new MathRow(cell)); + return cells; + } + + private static void Table(IReadOnlyList> rows, StringBuilder sb, Style style, Func align) + { + sb.Append(""); + foreach (var row in rows) + { + sb.Append(""); + for (var c = 0; c < row.Count; c++) + { + var side = align(c); + // columnalign for MathML 3 readers, text-align for MathML Core (browsers). + sb.Append(" 1) + sb.Append(side == "right" ? ";padding-right:0" : ";padding-left:0"); + sb.Append("\">"); + Node(row[c], sb, style); + sb.Append(""); + } + sb.Append(""); + } + sb.Append(""); + } + + private static void Scripts(MathNode baseNode, MathNode? sub, MathNode? sup, StringBuilder sb, Style style, bool under, bool large = false) + { + void Base() + { + if (large && baseNode is MathSymbol symbol) + sb.Append(under ? "" : "").Append(OmmlWriter.Escape(symbol.Text)).Append(""); + else + Group(baseNode, sb, style); + } + + var tag = (sub, sup, under) switch + { + (null, null, _) => null, + (_, null, false) => "msub", + (null, _, false) => "msup", + (_, _, false) => "msubsup", + (_, null, true) => "munder", + (null, _, true) => "mover", + _ => "munderover", + }; + if (tag is null) + { + Base(); + return; + } + sb.Append('<').Append(tag).Append('>'); + Base(); + if (sub is not null) + Group(sub, sb, style); + if (sup is not null) + Group(sup, sb, style); + sb.Append("'); + } + + private static void Accent(MathAccent accent, StringBuilder sb, Style style) + { + var (mark, below) = accent.Command switch + { + "hat" => ("^", false), + "check" => ("ˇ", false), + "tilde" => ("~", false), + "acute" => ("´", false), + "grave" => ("`", false), + "dot" => ("˙", false), + "ddot" => ("¨", false), + "breve" => ("˘", false), + "vec" => ("→", false), + "dyad" => ("↔", false), + "arch" => ("⏜", false), + "bar" or "OVERLINE" or "overline" => ("‾", false), + "under" or "UNDERLINE" or "underline" => ("_", true), + _ => ((string?)null, false), + }; + if (mark is null) + { + // \not: a crossed-out character where there is one, else the content. + if (accent.Command == "not" && accent.Body is MathSymbol or MathCommand) + { + var glyph = accent.Body is MathSymbol symbol ? symbol.Text : ((MathCommand)accent.Body).Glyph; + Operator(glyph + "̸", sb, style); + return; + } + Node(accent.Body, sb, style); + return; + } + // A line or arrow over several characters stretches across them. + var stretch = mark is "‾" or "_" or "→" or "↔" or "⏜" ? " stretchy=\"true\"" : " stretchy=\"false\""; + sb.Append(below ? "" : ""); + Group(accent.Body, sb, style); + sb.Append("').Append(OmmlWriter.Escape(mark)).Append(""); + sb.Append(below ? "" : ""); + } + + /// A stretched bracket ("" for none). + private static void Fence(string bracket, StringBuilder sb) + { + if (bracket.Length > 0) + sb.Append("").Append(OmmlWriter.Escape(bracket)).Append(""); + } + + private static void Operator(string text, StringBuilder sb, Style style) + { + // Brackets typed as characters keep their size, as TeX keeps them. + var fixedSize = text is "(" or ")" or "[" or "]" or "{" or "}" or "|" or "‖" or "⟨" or "⟩" or "⌊" or "⌋" or "⌈" or "⌉"; + // (and no room around them: | x | is an absolute value, not two relations) + sb.Append(fixedSize ? "').Append(OmmlWriter.Escape(text)).Append(""); + } + + /// Letters (each its own <mi> when italic), CJK and other text. + private static void Text(string text, StringBuilder sb, Style style) + { + if (!style.Upright && text.Length > 1 && text.All(char.IsAsciiLetter)) + { + foreach (var letter in text) + Token("mi", letter.ToString(), sb, style); + return; + } + // Greek capitals are upright in TeX and Word. + var upright = style.Upright || (text.Length == 1 && text[0] is >= 'Α' and <= 'Ω'); + Token("mi", text, sb, style with { Upright = upright }); + } + + private static void Token(string tag, string text, StringBuilder sb, Style style) + { + sb.Append('<').Append(tag); + if (tag == "mi" && style.Upright && text.Length == 1) + sb.Append(" mathvariant=\"").Append(style.Bold ? "bold" : "normal").Append('"'); + else if (tag == "mi" && style.Bold) + sb.Append(" mathvariant=\"").Append(style.Upright || text.Length > 1 ? "bold" : "bold-italic").Append('"'); + if (style.Bold) + sb.Append(" style=\"font-weight:bold\""); + sb.Append('>').Append(OmmlWriter.Escape(text)).Append("'); + } + + private static bool IsGreek(string glyph) => glyph.Length == 1 && glyph[0] is >= 'Ͱ' and <= 'Ͽ'; +} diff --git a/src/Filee.Engines/Hwp/Hwpx/XhtmlWriter.cs b/src/Filee.Engines/Hwp/Hwpx/XhtmlWriter.cs index 839ec25a5..f5e47d1d7 100644 --- a/src/Filee.Engines/Hwp/Hwpx/XhtmlWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/XhtmlWriter.cs @@ -1,6 +1,7 @@ // HDocument → clean XHTML with one small style sheet: the chapters of EPUB files, the page of HTMLZ, and single-page // HTML exports. Structure is kept (headings, paragraphs with alignment and indents, lists, tables with spans, -// pictures, links and anchors, footnotes); page setup, headers and footers are left to the reading system. +// pictures, links and anchors, footnotes, equations as MathML where the reader takes it); page setup, headers and +// footers are left to the reading system. // // Chapters are split at level-1 headings and page breaks (and when a chapter grows very long), and links to // bookmarks in other chapters point at the right file. @@ -24,6 +25,9 @@ internal sealed class XhtmlOptions /// Mark footnotes and references with epub:type (EPUB 3 pop-up notes). public bool Epub { get; init; } + + /// Equations as MathML (HTML, EPUB 3); false writes their text (MOBI). + public bool MathML { get; init; } } /// One output file of . @@ -69,6 +73,9 @@ internal sealed class XhtmlWriter private int _headingCount; private int _noteCount; private List _headings = []; + + /// The paragraph whose inlines are being written (whether an equation in it is a display equation). + private HParagraph? _paragraph; private StringBuilder _notes = new(); private XhtmlWriter(XhtmlOptions options, int bodySize) @@ -217,7 +224,10 @@ private void Paragraph(HParagraph paragraph, StringBuilder sb) sb.Append(Anchors(paragraph.Inlines)).Append("
\n"); return; } + var outer = _paragraph; + _paragraph = paragraph; var content = Inlines(paragraph.Inlines, paragraph.HeadingLevel > 0); + _paragraph = outer; if (!HasVisible(paragraph.Inlines)) { // Empty paragraphs are spacing in word processors; reading systems space paragraphs themselves. @@ -411,6 +421,12 @@ private string Inlines(List inlines, bool heading) { switch (inline) { + case HEquation equation when _options.MathML: + { + var display = _paragraph is { } paragraph && ReferenceEquals(paragraph.Inlines, inlines) && HEquation.IsDisplay(equation, paragraph); + sb.Append(MathMLWriter.Write(equation.Math, display, equation.Tex)); + break; + } case HText text: Run(text.Text, text.Format, heading, sb); break; diff --git a/src/Filee.Engines/Typesetting/MathLayout.cs b/src/Filee.Engines/Typesetting/MathLayout.cs index 580993dfa..2b029ff28 100644 --- a/src/Filee.Engines/Typesetting/MathLayout.cs +++ b/src/Filee.Engines/Typesetting/MathLayout.cs @@ -63,9 +63,14 @@ internal sealed class MathLayout(FontResolver fonts, Shaper shaper) [ "=", "<", ">", "≤", "≥", "≠", "≈", "≡", "≅", "∼", "≃", "∝", "≪", "≫", "∈", "∉", "∋", "⊂", "⊃", "⊆", "⊇", "⊄", "⊅", "→", "←", "↔", "⇒", "⇐", "⇔", "↦", "⊥", "∥", "≍", "≐", "≺", "≻", ":", + // TeX's further relations and arrows (LatexMath) + "⟶", "⟵", "⟷", "⟹", "⟸", "⟺", "⟼", "↪", "↩", "⇌", "⇋", "↑", "↓", "↕", "⇑", "⇓", "⇕", "↗", "↘", "↖", "↙", "⇀", "⇁", + "↼", "↽", "⇉", "⇇", "↠", "⇝", "⊨", "⊢", "⊣", "∣", "∤", "∦", "⊊", "⊋", "⊈", "⊉", "≲", "≳", "⪯", "⪰", "⩽", "⩾", "≦", + "≧", "≢", "≰", "≱", "≮", "≯", "≁", "≇", "≉", "∌", "⊏", "⊐", "⊑", "⊒", "≔", "≕", "≜", "≊", "≑", "⋈", "≶", "≷", "⋘", "⋙", ]; - private static readonly HashSet Binary = ["+", "-", "−", "±", "∓", "×", "÷", "·", "∘", "∪", "∩", "∧", "∨", "⊕", "⊖", "⊗", "⊙", "∗", "⋆", "⊔", "⊓", "\\", "/"]; + private static readonly HashSet Binary = ["+", "-", "−", "±", "∓", "×", "÷", "·", "∘", "∪", "∩", "∧", "∨", "⊕", "⊖", "⊗", "⊙", "∗", "⋆", "⊔", "⊓", "\\", "/", + "∖", "⊎", "⋄", "≀", "⨿", "⊘", "◁", "▷", "⋉", "⋊", "⊞", "⊟", "⊠", "⊡", "∔", "⊼", "⊻", "⋎", "⋏"]; /// /// Levels laid out, above what the readers build (EquationParser.MaxDepth, OmmlReader.MaxDepth): content deeper diff --git a/tests/Filee.Engines.Tests/LatexMathFuzzTests.cs b/tests/Filee.Engines.Tests/LatexMathFuzzTests.cs new file mode 100644 index 000000000..46e8c9b22 --- /dev/null +++ b/tests/Filee.Engines.Tests/LatexMathFuzzTests.cs @@ -0,0 +1,51 @@ +// Mutation fuzzing of TeX math in Markdown (#1652): Markdown with $ … $, $$ … $$, \( … \) and \[ … \] read by +// MarkdownReader (MarkdownMath finds the math, LatexMath reads it), the equations written as Word math, MathML and a 한글 +// script, laid out and drawn. Every input must be read or refused (ConversionRefusedException) in time and on the 1 MB +// stack FuzzHarness gives it. FILEE_FUZZ_SECONDS runs longer with a new random seed, FILEE_FUZZ_SEED repeats a run. + +using System.Text; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Hwp.Hwpx.Docx; + +namespace Filee.Engines.Tests; + +[Trait("Category", "Fuzz")] +public class LatexMathFuzzTests(EngineFixture fx) : IClassFixture +{ + private const int QuickIterations = 50; + + /// Each kind of structure LatexMath reads, inline and display. + private const string Seed = """ + Inline $x^2_i + \frac{a}{b} - \sqrt[3]{y'}$ and \(\alpha \le \beta\), not $5 or `$x$`. + + $$ + \sum_{k=1}^{n} k = \frac{n(n+1)}{2}, \quad \int_0^1 f(x)\,dx, \quad \lim_{x \to 0} \frac{\sin x}{x} + $$ + + \[ \left( \begin{pmatrix} a & b \\ c & d \end{pmatrix} \right\} \begin{cases} x & \text{if } x > 0 \\ -x & \text{else} \end{cases} \] + + $$\begin{aligned} f(x) &= \hat{\mathbf{v}} \cdot \vec{w} \\ &\neq \overline{AB} \not\in \mathbb{R} \end{aligned} \tag{1}$$ + + $$\binom{n}{k} = {n \choose k}, \operatorname{arg\,max}_x \boldsymbol{\mu}, \foo{x} \mathcal{L} \big( \Big]$$ + """; + + [Fact] + public void Markdown_math_reads_writes_and_lays_out_every_mutated_input() => + new FuzzHarness(fx, QuickIterations, 20261119).Files("latex", "md", [Encoding.UTF8.GetBytes(Seed)], + FuzzHarness.Text(["$", "$$", "\\(", "\\)", "\\[", "\\]", "{", "}", "^", "_", "&", "\\\\", "'", "`", "\\frac", "\\sqrt", + "\\left(", "\\right)", "\\middle|", "\\begin{matrix}", "\\end{matrix}", "\\begin{cases}", "\\begin{aligned}", "\\end{aligned}", + "\\text{", "\\mathbf", "\\rm ", "\\over ", "\\not", "\\sum", "\\lim", "\\hat", "\\mathbb{", "\\tag{", "[", "]", "%", "\\"]), + (path, _) => + { + var document = MarkdownReader.Read(File.ReadAllText(path), Path.GetDirectoryName(path)!); + using var shaper = new Typesetting.Shaper(); + var layout = new Typesetting.MathLayout(new Typesetting.FontResolver(), shaper); + foreach (var equation in HDocumentWalker.Inlines(document.Sections.SelectMany(s => s.Blocks)).OfType()) + { + EquationScript.Script(equation.Math); + MathMLWriter.Write(equation.Math, equation.Display, equation.Tex); + OmmlWriter.Write(equation.Math, equation.Display); + layout.Layout(equation.Math, 10, SkiaSharp.SKColors.Black).Paint(new Rendering.PageContent(200, 200), 0, 0, 1); + } + }); +} diff --git a/tests/Filee.Engines.Tests/MarkdownMathTests.cs b/tests/Filee.Engines.Tests/MarkdownMathTests.cs new file mode 100644 index 000000000..9a3063d4b --- /dev/null +++ b/tests/Filee.Engines.Tests/MarkdownMathTests.cs @@ -0,0 +1,335 @@ +// TeX math in Markdown (#1652): where $ … $, $$ … $$, \( … \) and \[ … \] start and end (MarkdownMath), the TeX read into +// the equation tree (LatexMath, checked as the 한글 script EquationScript writes for it), and the equations in each +// writer: Word math in DOCX (read back by DocxReader), MathML in HTML and EPUB, 한글 equations in HWPX, typeset on the +// page, the source kept where a writer has no equations. Hostile formulas (deep nesting, huge ones, a flood of them) +// stay text or are refused, quickly. + +using System.Diagnostics; +using System.IO.Compression; +using System.Text.RegularExpressions; +using System.Xml.Linq; +using Filee.Core.Conversion; +using Filee.Core.Presets; +using Filee.Engines.Documents; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Hwp.Hwpx.Docx; +using Filee.Engines.Rendering; +using Filee.Engines.Typesetting; + +namespace Filee.Engines.Tests; + +public sealed partial class MarkdownMathTests(EngineFixture fx) : IClassFixture +{ + // ───────────────────────── TeX ───────────────────────── + + [Theory] + [InlineData(@"\frac{a+b}{2}", "{{a + b} over {2}}")] + [InlineData(@"x^2", "x ^{2}")] + [InlineData(@"x_i^2", "x _{i} ^{2}")] + [InlineData(@"x^23", "x ^{2} 3")] // a script without braces is one token + [InlineData(@"\sqrt[3]{x}", "root {3} of {x}")] + [InlineData(@"\sqrt{x^2+1}", "sqrt {x ^{2} + 1}")] + [InlineData(@"\alpha + \beta \times \Gamma", "alpha + beta TIMES Gamma")] + [InlineData(@"\sum_{i=1}^{n} i", "SUM _{i = 1} ^{n} i")] + [InlineData(@"\int_0^1 f(x)\,dx", "INT _{0} ^{1} f ( x ) ~ d x")] + [InlineData(@"\lim_{x \to 0} \frac{\sin x}{x}", "lim _{x rarrow 0} {{sin x} over {x}}")] + [InlineData(@"\left( \frac{a}{b} \right)", "LEFT ( {{a} over {b}} RIGHT )")] + [InlineData(@"\left\{ x \right.", "LEFT LBRACE x RIGHT .")] + [InlineData(@"\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}", "pmatrix {1 & 0 # 0 & 1}")] + [InlineData(@"\begin{vmatrix} a & b \\ c & d \\ \end{vmatrix}", "dmatrix {a & b # c & d}")] + [InlineData(@"|x| = \begin{cases} x & x \ge 0 \\ -x & \text{otherwise} \end{cases}", "| x | = cases {x & x GEQ 0 # - x & \"otherwise\"}")] + [InlineData(@"\begin{aligned} a &= b \\ c &= d \end{aligned}", "pile {a = b # c = d}")] + [InlineData(@"\hat{x} + \vec{v} + \overline{AB}", "hat {x} + vec {v} + bar {A B}")] + [InlineData(@"\mathbf{v} \cdot \mathrm{d}x", "{bold {{rm {v}}}} CDOT {rm {d}} x")] + [InlineData(@"a \neq b \leq c \to \infty", "a NEQ b LEQ c rarrow INF")] + [InlineData(@"\binom{n}{k}", "LEFT ( {{n} atop {k}} RIGHT )")] + [InlineData(@"x \in \mathbb{R}", "x IN ℝ")] + [InlineData(@"f'(x)", "f ^{prime} ( x )")] + [InlineData(@"\{x\}", "LBRACE x RBRACE")] + [InlineData(@"\displaystyle a \over b", "{{a} over {b}}")] + [InlineData(@"e^{i\pi} + 1 = 0", "e ^{i pi} + 1 = 0")] + [InlineData(@"\not\in \not= \not\subset", "NOTIN NEQ ⊄")] + [InlineData(@"3.14 \quad x", "3.14 ~ x")] + public void Tex_reads_into_the_equation_tree(string tex, string script) => + Assert.Equal(script, EquationScript.Script(LatexMath.Parse(tex)!)); + + [Theory] + [InlineData(@"\foo{x} + 1", "\"\\foo\" x + 1")] + [InlineData(@"\begin{weird} a & b \end{weird}", "\"\\begin{weird}\" {a b} \"\\end{weird}\"")] + public void An_unknown_command_is_kept_as_its_text(string tex, string script) => + Assert.Equal(script, EquationScript.Script(LatexMath.Parse(tex)!)); + + [Theory] + [InlineData(@"\frac{")] + [InlineData(@"}}} {{ ^ _ & \\")] + [InlineData(@"\left( a")] + [InlineData(@"a \right) b \end{x}")] + [InlineData(@"\begin{matrix} 1 & 2")] + [InlineData(@"\sqrt[")] + [InlineData(@"\text{unclosed")] + [InlineData(@"x^")] + [InlineData(@"\")] + [InlineData(@"\mathbb{}")] + public void Broken_tex_keeps_what_it_can_without_failing(string tex) + { + var tree = LatexMath.Parse(tex); + Assert.NotNull(tree); + Assert.NotNull(EquationScript.Linear(tree)); + Assert.NotNull(MathMLWriter.Write(tree, display: true, tex)); + Assert.NotNull(OmmlWriter.Write(tree, display: true)); + } + + // ───────────────────────── Markdown ───────────────────────── + + [Theory] + [InlineData("a $x$ b", "x")] + [InlineData("$5$", "5")] + [InlineData("costs $5 and $10", "")] // a closing $ after a space doesn't close + [InlineData("$5-$10", "")] // nor one before a digit + [InlineData("$ x$", "")] // an opening $ before a space doesn't open + [InlineData("a \\$x$", "")] // an escaped $ is text + [InlineData("`$x$` code", "")] // a code span wins + [InlineData("$a `b$` c", "")] // a code span that starts inside wins too + [InlineData("$a`b$", "a`b")] // a backtick without its pair is math + [InlineData("\\(x\\) and \\(y\\)", "x|y")] + [InlineData("\\\\(x\\\\)", "")] // \\ is an escaped backslash: the bracket after it is text + [InlineData("text $$a+b$$ more", "a+b")] + [InlineData("$\\$$", "\\$")] + public void Math_delimiters_follow_the_usual_rules(string markdown, string expected) + { + var equations = Equations(MarkdownReader.Read(markdown, fx.NewFolder())); + Assert.Equal(expected, string.Join("|", equations.Select(e => e.Tex))); + } + + [Theory] + [InlineData("$$\n\\frac{a}{b}\n$$")] + [InlineData("$$\\frac{a}{b}$$")] + [InlineData("\\[\n\\frac{a}{b}\n\\]")] + [InlineData("\\[\\frac{a}{b}\\]")] + public void Display_math_is_a_centred_equation_of_its_own(string markdown) + { + var document = MarkdownReader.Read($"Before\n\n{markdown}\n\nAfter", fx.NewFolder()); + + var paragraph = document.Sections.Single().Blocks.OfType().Single(p => p.Inlines.Any(i => i is HEquation)); + var equation = Assert.IsType(Assert.Single(paragraph.Inlines)); + Assert.True(equation.Display); + Assert.Equal(HAlign.Center, paragraph.Format.Align); + Assert.Equal("{{a} over {b}}", EquationScript.Script(equation.Math)); + } + + [Fact] + public void Display_math_among_text_stays_in_its_line() + { + var paragraph = MarkdownReader.Read("where $$E = mc^2$$ holds", fx.NewFolder()).Sections.Single().Blocks.OfType().Single(); + + var equation = Assert.Single(paragraph.Inlines.OfType()); + Assert.False(equation.Display); + Assert.Null(paragraph.Format.Align); + Assert.Equal("$$E = mc^2$$", equation.Text); + } + + [Fact] + public void Writers_without_equations_keep_the_tex_source() + { + var document = MarkdownReader.Read("Area $\\pi r^2$ and\n\n$$\\sum_{i=1}^n i$$", fx.NewFolder()); + + var text = PlainTextWriter.Write(document); + Assert.Contains("$\\pi r^2$", text); + Assert.Contains("$$\\sum_{i=1}^n i$$", text); + + var markdown = MarkdownWriter.Write(document, _ => null); + Assert.Contains("$\\pi r^2$", markdown); // not escaped: it reads back as math + Assert.Equal(["\\pi r^2", "\\sum_{i=1}^n i"], Equations(MarkdownReader.Read(markdown, fx.NewFolder())).Select(e => e.Tex)); + } + + // ───────────────────────── Writers ───────────────────────── + + private const string Formulas = """ + # Formulas + + Inline $x^2 + y^2 = r^2$ and $\alpha \le \beta$. + + $$ + \sum_{k=1}^{n} k = \frac{n(n+1)}{2} + $$ + + $$\int_0^1 \sqrt{x}\,dx = \frac{2}{3}$$ + + $$\lim_{x \to 0} \frac{\sin x}{x} = 1$$ + + $$A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}, \quad |x| = \begin{cases} x & x \ge 0 \\ -x & \text{else} \end{cases}$$ + + $$\hat{\mathbf{v}} = \left( \frac{1}{\sqrt{2}} \right)$$ + """; + + [Fact] + public void Docx_gets_word_math_that_reads_back_as_the_same_equations() + { + var document = MarkdownReader.Read(Formulas, fx.NewFolder()); + var output = Path.Combine(fx.NewFolder(), "math.docx"); + + DocxWriter.Write(document, output); + + using (var zip = ZipFile.OpenRead(output)) + { + XNamespace m = "http://schemas.openxmlformats.org/officeDocument/2006/math"; + var body = XDocument.Load(zip.GetEntry("word/document.xml")!.Open()); + Assert.Equal(5, body.Descendants(m + "oMathPara").Count()); + Assert.Equal(7, body.Descendants(m + "oMath").Count()); + Assert.NotEmpty(body.Descendants(m + "nary")); + Assert.NotEmpty(body.Descendants(m + "f")); + Assert.NotEmpty(body.Descendants(m + "m")); + Assert.NotEmpty(body.Descendants(m + "eqArr")); + } + + // As text: Word keeps upright text (\text) as plain letters, the TeX as quoted text. + var written = Equations(document).Select(e => EquationScript.Linear(Flat(e.Math))).ToList(); + var read = Equations(DocxReader.Read(output, fx.NewFolder())).Select(e => EquationScript.Linear(Flat(e.Math))).ToList(); + Assert.Equal(written, read); + } + + [Fact] + public void Html_gets_mathml_with_the_tex_as_its_annotation() + { + var folder = fx.NewFolder(); + var input = Path.Combine(folder, "math.md"); + var html = Filee.Engines.Text.MarkdownConverter.ToHtml(Formulas, input, Path.Combine(folder, "math.html")); + + var maths = MathElement().Matches(html).Select(m => XElement.Parse(m.Value)).ToList(); + Assert.Equal(7, maths.Count); + XNamespace mml = "http://www.w3.org/1998/Math/MathML"; + Assert.Equal(5, maths.Count(e => (string?)e.Attribute("display") == "block")); + Assert.Contains(maths, e => e.Descendants(mml + "mfrac").Any()); + Assert.Contains(maths, e => e.Descendants(mml + "munderover").Any()); + Assert.Contains(maths, e => e.Descendants(mml + "mtable").Any()); + Assert.Contains(maths, e => (string?)e.Descendants(mml + "annotation").SingleOrDefault() == "x^2 + y^2 = r^2"); + Assert.DoesNotContain("\\frac", html.Replace("alttext", "", StringComparison.Ordinal)[..html.IndexOf(" $"{f.ErrorKey} {f.ErrorDetail}"))); + + using var zip = ZipFile.OpenRead(job.Outputs.Single()); + var opf = new StreamReader(zip.Entries.Single(e => e.FullName.EndsWith(".opf", StringComparison.Ordinal)).Open()).ReadToEnd(); + Assert.Contains("properties=\"mathml\"", opf); + var chapter = zip.Entries.First(e => e.FullName.EndsWith("ch001.xhtml", StringComparison.Ordinal)); + var page = XDocument.Load(chapter.Open()); // still well-formed XHTML + Assert.NotEmpty(page.Descendants(XName.Get("math", "http://www.w3.org/1998/Math/MathML"))); + } + + [Fact] + public void Hwpx_gets_hangul_equations() + { + var output = Path.Combine(fx.NewFolder(), "math.hwpx"); + + HwpxWriter.Write(MarkdownReader.Read(Formulas, fx.NewFolder()), output); + + using var zip = ZipFile.OpenRead(output); + var section = new StreamReader(zip.GetEntry("Contents/section0.xml")!.Open()).ReadToEnd(); + Assert.Equal(7, Regex.Matches(section, "(.*?)").Select(m => m.Groups[1].Value).ToList(); + Assert.Contains(scripts, s => s.Contains("SUM _{k = 1} ^{n} k", StringComparison.Ordinal)); + Assert.DoesNotContain(scripts, s => s.Contains('\\')); + } + + [Fact] + public void The_page_typesets_the_math_instead_of_printing_its_source() + { + var fonts = RepositoryFonts.Load(); + Assert.SkipUnless(fonts.Has(RepositoryFonts.Latin), "Noto Sans is not in src/Filee.App/Assets/Fonts (build/fetch-fonts.ps1)"); + using var shaper = new Shaper(); + var document = MarkdownReader.Read(Formulas, fx.NewFolder()); + + var pages = new PageComposer(new FontResolver(fonts), shaper).Compose(document, TestContext.Current.CancellationToken).Pages; + + var texts = pages.SelectMany(p => p.Items.OfType()).Select(g => g.Run.Text).ToList(); + Assert.DoesNotContain(texts, t => t.Contains('\\') || t.Contains('$')); + Assert.Contains(texts, t => t.Contains('∑')); + // The fraction bars and the root signs are drawn. + Assert.True(pages.SelectMany(p => p.Items).Count(i => i is not GlyphItem) >= 5); + } + + // ───────────────────────── Bounds ───────────────────────── + + [Fact] + public void Hostile_formulas_stay_text_quickly() + { + var watch = Stopwatch.StartNew(); + string[] hostile = + [ + new string('{', 50_000) + "x" + new string('}', 50_000), // longer than a formula may be + new string('{', 5_000) + "x" + new string('}', 5_000), + string.Concat(Enumerable.Repeat(@"\sqrt", 3_000)) + "x", + string.Concat(Enumerable.Repeat(@"\left(", 3_000)) + "x", + string.Concat(Enumerable.Repeat(@"\frac1", 3_000)) + "2", + "x" + string.Concat(Enumerable.Repeat("^", 10_000)) + "2", + string.Concat(Enumerable.Repeat(@"a \over ", 5_000)) + "b", + string.Concat(Enumerable.Repeat(@"\begin{matrix}", 3_000)), + string.Concat(Enumerable.Repeat(@"\text{$", 3_000)), + string.Concat(Enumerable.Repeat(@"\rm ", 5_000)) + "x", + ]; + foreach (var tex in hostile) + { + var tree = LatexMath.Parse(tex); + if (tree is not null) + { + Assert.False(EquationScript.Deeper(tree, LatexMath.MaxDepth)); + EquationScript.Script(tree); + MathMLWriter.Write(tree, display: true, tex); + OmmlWriter.Write(tree, display: true); + } + var paragraph = MarkdownReader.Read($"$${tex}$$", fx.NewFolder()).Sections.Single().Blocks.OfType().First(); + Assert.All(paragraph.Inlines.OfType(), e => Assert.False(EquationScript.Deeper(e.Math, LatexMath.MaxDepth))); + } + // A deep one is its source, as text. + Assert.Null(LatexMath.Parse(hostile[1])); + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"{watch.Elapsed}"); + } + + [Fact] + public void A_flood_of_equations_is_refused_with_the_usual_error() + { + var limits = DocumentLimits.Default with { MaxObjects = 10 }; + MarkdownReader.Read(string.Concat(Enumerable.Repeat("$x$ ", 10)), fx.NewFolder(), null, null, limits); + + var refused = Assert.Throws(() => + MarkdownReader.Read(string.Concat(Enumerable.Repeat("$x$ ", 11)), fx.NewFolder(), null, null, limits)); + Assert.Equal(DocumentLimits.ErrorKey, refused.ErrorKey); + } + + // ───────────────────────── Helpers ───────────────────────── + + private static List Equations(HDocument document) => + [.. HDocumentWalker.Inlines(document.Sections.SelectMany(s => s.Blocks)).OfType()]; + + /// + /// The tree with rows inside rows flattened (Word groups an operator's operand, the TeX doesn't) and spaces left out + /// (OmmlReader reads none in math). + /// + private static MathNode Flat(MathNode node) => node switch + { + MathRow row => new MathRow([.. row.Items.Where(i => i is not MathSpace).SelectMany(i => Flat(i) is MathRow inner ? inner.Items : [Flat(i)])]), + MathFraction f => f with { Numerator = Flat(f.Numerator), Denominator = Flat(f.Denominator) }, + MathRoot r => r with { Index = r.Index is null ? null : Flat(r.Index), Body = Flat(r.Body) }, + MathScripts s => s with { Base = Flat(s.Base), Sub = s.Sub is null ? null : Flat(s.Sub), Sup = s.Sup is null ? null : Flat(s.Sup) }, + MathBigOperator b => b with { Lower = b.Lower is null ? null : Flat(b.Lower), Upper = b.Upper is null ? null : Flat(b.Upper) }, + MathLimit l => l with { Lower = l.Lower is null ? null : Flat(l.Lower) }, + MathMatrix m => m with { Rows = [.. m.Rows.Select(r => (IReadOnlyList)[.. r.Select(Flat)])] }, + MathCases c => c with { Rows = [.. c.Rows.Select(r => (IReadOnlyList)[.. r.Select(Flat)])] }, + MathPile p => p with { Rows = [.. p.Rows.Select(Flat)] }, + MathBrackets b => b with { Body = Flat(b.Body) }, + MathAccent a => a with { Body = Flat(a.Body) }, + MathStyled s => s with { Body = Flat(s.Body) }, + _ => node, + }; + + [GeneratedRegex("", RegexOptions.Singleline)] + private static partial Regex MathElement(); +} From c915a9f69903406045cdeee088a3d3be9fe22712 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 00:37:27 +0900 Subject: [PATCH 4/6] Draw DrawingML charts in sheets, Word documents and slides Charts/ reads a chart part (c:chartSpace) from its cached values and draws it with Skia, text as glyph runs: bar and column (clustered, stacked, 100 %), line, area, pie, doughnut and scatter plots with titles, axes, gridlines, legend and data labels, series colours from c:spPr or the theme's accents in Office's order. - Sheet printer: chart frames become SheetChart drawings; a workbook whose charts are all drawn stays on the built-in printer. - Word: a chart becomes a picture of it for the writers that carries the chart for the renderer; the check only names charts it can't draw. - Slides: chart frames are drawn by the slide renderer; the ODP writer and the deck's text take a picture of the chart. - 3D, radar, stock, surface, bubble and chartex charts are still named. - Bound series and points per chart and points drawn per document; clamp sheet font sizes to Excel's range (a zero size crashed printing). Co-Authored-By: Claude Opus 5.5 --- docs/ENGINES.md | 53 +- src/Filee.Engines/Charts/ChartDrawer.cs | 1718 +++++++++++++++++ src/Filee.Engines/Charts/ChartModel.cs | 297 +++ src/Filee.Engines/Charts/ChartPicture.cs | 27 + src/Filee.Engines/Charts/ChartReader.cs | 560 ++++++ src/Filee.Engines/Charts/ChartText.cs | 106 + .../Hwp/Hwpx/Docx/DocxReader.Drawing.cs | 40 +- src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs | 6 + .../Office/Sheets/Print/SheetPageRenderer.cs | 28 +- .../Office/Sheets/Print/SheetText.cs | 4 + .../Office/Sheets/SheetCapabilities.cs | 8 +- src/Filee.Engines/Office/Sheets/Workbook.cs | 4 + src/Filee.Engines/Office/Sheets/XlsbReader.cs | 3 +- .../Office/Sheets/XlsxDrawing.cs | 39 +- src/Filee.Engines/Office/Sheets/XlsxReader.cs | 17 +- src/Filee.Engines/Slides/PptxSlideReader.cs | 22 +- src/Filee.Engines/Slides/SlideCapabilities.cs | 9 +- src/Filee.Engines/Slides/SlideModel.cs | 19 +- src/Filee.Engines/Slides/SlideRenderer.cs | 7 + .../Typesetting/FileeRendererConverter.cs | 27 +- .../Typesetting/FloatingObjects.cs | 10 + tests/Filee.Engines.Tests/ChartBuilder.cs | 119 ++ tests/Filee.Engines.Tests/ChartFuzzTests.cs | 136 ++ tests/Filee.Engines.Tests/ChartTests.cs | 356 ++++ tests/Filee.Engines.Tests/DocxBuilder.cs | 9 + 25 files changed, 3581 insertions(+), 43 deletions(-) create mode 100644 src/Filee.Engines/Charts/ChartDrawer.cs create mode 100644 src/Filee.Engines/Charts/ChartModel.cs create mode 100644 src/Filee.Engines/Charts/ChartPicture.cs create mode 100644 src/Filee.Engines/Charts/ChartReader.cs create mode 100644 src/Filee.Engines/Charts/ChartText.cs create mode 100644 tests/Filee.Engines.Tests/ChartBuilder.cs create mode 100644 tests/Filee.Engines.Tests/ChartFuzzTests.cs create mode 100644 tests/Filee.Engines.Tests/ChartTests.cs diff --git a/docs/ENGINES.md b/docs/ENGINES.md index 7969aedab..f8780eeb0 100644 --- a/docs/ENGINES.md +++ b/docs/ENGINES.md @@ -863,8 +863,8 @@ the text the spreadsheet shows, the value behind it and its Excel number format. The XLSX donut also offers **PDF + Edit** (`tool-sheet-studio`). This interactive tool preserves the original workbook, makes one disposable copy per selected sheet region, applies that region's print settings, and prints it with the same engine as an ordinary XLSX → PDF: Filee's own print engine (below), or LibreOffice when **Use -LibreOffice for spreadsheets → PDF** is on and it is installed (and, as in a conversion, for a workbook with charts or -other objects the print engine doesn't draw yet; without LibreOffice the studio shows the printer's notice). Every +LibreOffice for spreadsheets → PDF** is on and it is installed (and, as in a conversion, for a workbook with chart +sheets, 3D charts or other objects the print engine doesn't draw yet; without LibreOffice the studio shows the printer's notice). Every studio setting (paper, orientation, margins, scale or fit to pages, page order, print area, hidden rows and columns, manual breaks, sheet choice) is written into the copy and read by the print engine; the sheet's own gridlines, headings, centring and headers / footers are kept. PDFsharp combines the resulting PDFs. The live preview follows @@ -944,7 +944,10 @@ services (FontResolver with font fallback, the HarfBuzz shaper, the UAX #14 line Each sheet's page count is checked against `MaxPages` before anything is drawn (a cell in A1 and one in XFD1048576 would be about 35 million pages), and the layout runs under `LayoutGuard`. `DocumentFuzzTests` mutates an XLSX and an ODS built in code and prints them. -- **Not printed yet.** Charts, shapes, text boxes, comments, sparklines; data bars, icon sets and conditional formats +- **Charts** (bar, column, line, area, pie, doughnut, scatter) on a sheet are printed from their chart part by the + [chart drawer](#charts); 3D, radar, stock, surface and bubble charts, new chart types (`chartex`) and chart sheets + are named, so such a workbook takes LibreOffice where it is installed. +- **Not printed yet.** Chart sheets, shapes with text, text boxes, comments, sparklines; data bars, icon sets and conditional formats with formulas of their own; conditional formats of XLS and ODS files; rich text in XLS cells; text running past the edge of a page onto the next one; black-and-white and draft printing; the margins a real printer can't print in (a PDF has none). @@ -1053,9 +1056,9 @@ test turns it on. were always off by default and are left as the user set them. Tests: `FileeRendererConverterTests` (`Libreoffice_chosen_for_*`), `RoutePlannerTests` (the costs). - **The check before converting** (`ConversionEdge.Checked`, `ICapabilityCheck`): the engine looks through an HWPX, Word - or ODT package (an HWP as the HWPX package it becomes, a DOC as the DOCX package it becomes) for what it doesn't draw yet — OLE objects and charts, vertical - text, form controls, videos, SmartArt — and a deck for what the slide renderer doesn't draw (charts without a - stored picture, shadows and other effects, 3D, WordArt warps, presets it lacks, ...) — and `JobQueue` plans again + or ODT package (an HWP as the HWPX package it becomes, a DOC as the DOCX package it becomes) for what it doesn't draw yet — OLE objects, charts + other than the DrawingML ones the [chart drawer](#charts) draws, vertical text, form controls, videos, SmartArt — and a deck for what the slide + renderer doesn't draw (charts the chart drawer doesn't draw without a stored picture, shadows and other effects, 3D, WordArt warps, presets it lacks, ...) — and `JobQueue` plans again without it, so such a file takes its usual route (logged). The choice is made once, before the conversion. A checked edge is only ever a route's first step, where the file is the one the engine looked at. Text formats hold nothing it can't draw. - Its edges aren't `Unshaped`: with it on, Arabic, Hebrew, Indic and Thai documents are shaped by it instead of @@ -1540,8 +1543,8 @@ a slide. fonts (`+mj-lt`, `+mn-ea`, script fonts by the run's language); theme style references (`fillRef`, `lnRef`, `fontRef`, `bgRef`); tables with `tableStyles.xml` styles (PowerPoint's default *Medium Style 2 – Accent 1* is built in, as files often name it without writing it out); SmartArt through the drawing PowerPoint keeps with it (the data - part's `dsp:dataModelExt`); OLE objects and charts through their stored picture; `mc:AlternateContent` through its - fallback. + part's `dsp:dataModelExt`); charts from their chart part ([Charts](#charts)); OLE objects and other charts through + their stored picture; `mc:AlternateContent` through its fallback. - **Text frames:** insets, the geometry's text rectangle, anchoring (top, middle, bottom, centred across), wrapping on or off, columns, vertical frames (`vert`, `vert270`), the body's own rotation; "shrink text on overflow" with the font scale and line spacing reduction PowerPoint stored; line spacing as a multiple of the fonts' own line @@ -1573,7 +1576,7 @@ a slide. - **Pictures of slides:** the PDF options' page range and resolution (`RenderDpi`, lowered so one picture stays within 200 million pixels), then the image options (`ImageEncoder.ApplyOptions`), one file per slide (`_p2`, ...). - **The check** (`SlideCapabilities.cs`) looks at the shown slides and the layouts and masters they use, leaving out - what a fallback replaces, and names: charts without a stored picture, SmartArt without its drawing, OLE objects and + what a fallback replaces, and names: charts the chart drawer doesn't draw without a stored picture, SmartArt without its drawing, OLE objects and other embedded content without a picture, ink, shadows and other effects (on shapes, pictures and text, or through the theme's effect styles), 3D, WordArt warps, outlined text, East Asian vertical text, preset names outside ST_ShapeType, and TIFF and JPEG XR pictures. @@ -1596,7 +1599,37 @@ Every slide's text reads back, nothing is outside a slide, every font is embedde Not yet: shadows, glow, reflection, soft edges and 3D (named by the check); WordArt warps and text outlines; East Asian vertical text (upright characters); gradient-filled text (the first stop's colour); tiled pictures at their own resolution (taken as 96 dpi); a gradient that doesn't turn with its shape; "rect" path gradients (drawn as an ellipse -through the corners); charts drawn from their data; notes pages and handouts. +through the corners); 3D and other charts the chart drawer doesn't draw; notes pages and handouts. + +## Charts + +`Charts/` reads a DrawingML chart part (`c:chartSpace`, ECMA-376 Part 1 §21.2) and draws it with Skia, text as glyph +runs, for the sheet printer (`XlsxDrawing`, XLSX and XLSB), the document renderer (Word `c:chart` through +`DocxReader` and `FloatingObjects`) and the slide renderer (`PptxSlideReader`, `SlideRenderer`). + +- **Read** (`ChartReader`): bar and column (clustered, stacked, 100 %, horizontal), line (markers, smooth, stacked), + area, pie (first slice angle, pulled-out slices), doughnut and scatter plots, several in one plot area against + their own axes; values and categories from the part's caches (`c:numCache`, `c:strCache`, `c:multiLvlStrCache`, + literals), so no formula is evaluated; title (rich text, a cell's text, or the lone series' name Office shows), + axis titles, category / date / value axes (scaling, logarithmic base, orientation, number format, gridlines, tick + marks, label position, crossing, label skip and turn), legend (position, deleted entries), data labels (value, + category, series, percent, position), fills and outlines of the chart area, plot area, series and points (solid, + first colour of a gradient or pattern, none), the theme's accents in Office's order (then darker and lighter + variations), manual layouts of plot area, title and legend. +- **Drawn** (`ChartDrawer`): Office's automatic layout (title on top, legend on its side, axes' labels and titles + around the plot); value axes scale as Excel's (zero kept unless the values sit far from it, 1-2-5 units for the + axis' length, a little room above the top value); category labels level, wrapped at spaces, turned 45° or thinned + out. Writers that take pictures get a PNG of the chart (`ChartPicture`): the Word chart's picture in HWPX, ODT, + EPUB, ...; the slide's picture in ODP and in a deck's text. +- **Named instead** (the left-out notice says "charts"; the checks send the file to LibreOffice where it is + installed): 3D, radar, bubble, stock, surface and bar-of-pie charts and new chart types (`chartex`); a fallback + picture, when the file stores one, is drawn for those on slides. +- **Hostile input:** the part comes through the package's XML limits; more than 1,024 series or 1,048,576 points per + chart (a cache's `ptCount` or highest index counts, not only the points it holds) is refused with + `error.document_too_complex`, as is a document whose charts draw more than ten times that (`ChartBudget`, a chart + placed many times counting each time); texts are cut at 1,024 characters, values clamped to ±1e100, data labels + (4,096), markers (100,000), measured category labels (2,000) and ticks per axis (1,000) capped. `ChartTests` builds + the charts in code; `ChartFuzzTests` mutates chart parts alone and in a printed workbook. ## DOCX writer diff --git a/src/Filee.Engines/Charts/ChartDrawer.cs b/src/Filee.Engines/Charts/ChartDrawer.cs new file mode 100644 index 000000000..c215c825a --- /dev/null +++ b/src/Filee.Engines/Charts/ChartDrawer.cs @@ -0,0 +1,1718 @@ +// Draws a chart (ChartModel.cs) into a box of a page: fills and lines with Skia, text as glyph runs (ChartText), so +// the sheet printer, the document renderer and the slide renderer all draw charts the same way, as vectors. +// +// Layout follows Office's automatic one: the chart area less a margin; the title at the top; the legend on its side +// (right, left, top, bottom or the top-right corner) unless it overlays the plot; the plot area what is left, less its +// axes' labels and titles, or where a manual layout puts it. Value axes scale themselves as Excel does when the file +// sets no bounds: zero is kept unless the values sit far from it, the unit is 1, 2 or 5 times a power of ten giving +// a readable number of gridlines for the axis' length, and the top gets a little room above the highest value. +// Category labels are drawn level, wrapped at spaces, turned 45° or thinned out, whichever fits first. +// +// Plots: bars and columns (clustered with gap width and overlap, stacked, 100 %), lines (stacked, smooth, markers), +// areas, scatter (lines and markers), pies (first slice angle, pulled-out slices) and doughnuts (one ring per series), +// with data labels where the file asks for them. Combined plots share the plot area, each against its own axes. +// +// Bounds: data labels and markers drawn per chart are capped (MaxLabels, MaxMarkers), category labels are measured +// for at most MaxMeasured of them (the rest thinned out), and gridlines and ticks per axis at most MaxTicks. + +using System.Globalization; +using Filee.Engines.Office.Sheets; +using Filee.Engines.Rendering; +using SkiaSharp; + +namespace Filee.Engines.Charts; + +internal sealed class ChartDrawer +{ + private const int MaxLabels = 4096; + private const int MaxMarkers = 100_000; + private const int MaxMeasured = 2000; + private const int MaxTicks = 1000; + + private static readonly string[] AutoSymbols = ["diamond", "square", "triangle", "x", "star", "circle", "plus", "dash", "dot"]; + + /// Lines of axes and gridlines when the file doesn't say. + private static readonly SKColor AxisGray = new(0x86, 0x86, 0x86), GridGray = new(0xD9, 0xD9, 0xD9); + + private readonly Chart _chart; + private readonly PageContent _page; + private readonly ChartFonts _fonts; + private readonly float _s; + private readonly SKMatrix? _transform; + private readonly SKRect? _clip; + private readonly NumberFormatter _formatter; + private readonly List> _shapes = []; + private readonly List<(ChartTextLine Line, float X, float Baseline, SKMatrix? Local)> _texts = []; + private int _labels; + private int _markers; + + private ChartDrawer(Chart chart, PageContent page, ChartFonts fonts, float scale, SKMatrix? transform, SKRect? clip) + { + _chart = chart; + _page = page; + _fonts = fonts; + _s = scale; + _transform = transform is { IsIdentity: false } ? transform : null; + _clip = clip; + _formatter = new NumberFormatter(chart.Date1904); + } + + /// + /// Draws into , its text and lines times their + /// size (a sheet printed smaller or larger); maps the box's coordinates onto the page and + /// only what lies in (page coordinates) is drawn. + /// + public static void Draw(PageContent page, Chart chart, SKRect box, ChartFonts fonts, float scale = 1, SKMatrix? transform = null, SKRect? clip = null) + { + if (!(box.Width >= 2 && box.Height >= 2) || !float.IsFinite(box.Left) || !float.IsFinite(box.Top) || !float.IsFinite(box.Width) + || !float.IsFinite(box.Height) || !(scale > 0) || !float.IsFinite(scale)) + return; + var drawer = new ChartDrawer(chart, page, fonts, scale, transform, clip); + drawer.Run(box); + drawer.Flush(); + } + + // ───────────────────────── Output ───────────────────────── + + private void Flush() + { + var shapes = _shapes.ToArray(); + var (transform, clip) = (_transform, _clip); + _page.Draw(canvas => + { + if (clip is { } area) + canvas.ClipRect(area); + if (transform is { } matrix) + canvas.Concat(in matrix); + foreach (var shape in shapes) + shape(canvas); + }); + foreach (var (line, x, baseline, local) in _texts) + { + SKMatrix? matrix = (_transform, local) switch + { + ({ } outer, { } inner) => outer.PreConcat(inner), + ({ } outer, null) => outer, + (null, { } inner) => inner, + _ => null, + }; + line.Draw(_page, x, baseline, matrix, _clip); + } + } + + private void Shape(Action draw) => _shapes.Add(draw); + + private void Clipped(SKRect area, Action draw) => _shapes.Add(canvas => + { + canvas.Save(); + canvas.ClipRect(area); + draw(canvas); + canvas.Restore(); + }); + + private static void FillPath(SKCanvas canvas, SKPath path, SKColor? fill) + { + if (fill is not { } color || color.Alpha == 0) + return; + using var paint = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Fill, Color = color }; + canvas.DrawPath(path, paint); + } + + private static void StrokePath(SKCanvas canvas, SKPath path, Pen? pen, bool round = false) + { + if (pen is not { } p) + return; + using var dash = p.Dash is { Length: >= 2 } pattern + ? SKPathEffect.CreateDash([.. (pattern.Length % 2 == 0 ? pattern : [.. pattern, .. pattern]).Select(d => Math.Max(0.05f, d * p.Width))], 0) + : null; + using var paint = new SKPaint + { + IsAntialias = true, + Style = SKPaintStyle.Stroke, + Color = p.Color, + StrokeWidth = p.Width, + PathEffect = dash, + StrokeJoin = round ? SKStrokeJoin.Round : SKStrokeJoin.Miter, + StrokeCap = round ? SKStrokeCap.Round : SKStrokeCap.Butt, + }; + canvas.DrawPath(path, paint); + } + + /// A visible line: colour, width in page points, dashes. + private readonly record struct Pen(SKColor Color, float Width, float[]? Dash); + + /// The pen of a line: the file's, or colour and width; null for no line. + private Pen? PenOf(ChartLine? line, SKColor? auto, float autoWidth) + { + if (line is { None: true }) + return null; + var color = line?.Color ?? auto; + var width = line?.Width ?? autoWidth; + if (color is not { } c || c.Alpha == 0 || !(width > 0)) + return null; + return new Pen(c, Math.Max(width, 0.25f) * _s, line?.Dash); + } + + private static SKColor? FillOf(ChartShapeStyle style, SKColor? auto) => + style.Fill is { } fill ? fill.Alpha == 0 ? null : fill : auto; + + private ChartTextLine Text(string text, ChartTextStyle style) => _fonts.Line(text, style, style.Size * _s); + + /// Text with its top-left corner at (x, y). + private void TextAt(ChartTextLine line, float x, float y) => _texts.Add((line, x, y + line.Ascent, null)); + + /// Text centred on (cx, cy), turned by clockwise about that point. + private void TextCentred(ChartTextLine line, float cx, float cy, float degrees = 0) + { + if (degrees == 0) + { + _texts.Add((line, cx - line.Width / 2, cy - line.Height / 2 + line.Ascent, null)); + return; + } + var local = SKMatrix.CreateTranslation(cx, cy).PreConcat(SKMatrix.CreateRotationDegrees(degrees)); + _texts.Add((line, -line.Width / 2, -line.Height / 2 + line.Ascent, local)); + } + + private string Format(double value, string? format) + { + var text = _formatter.Format(value, format); + return text.Length > ChartReader.MaxText ? text[..ChartReader.MaxText] : text; + } + + // ───────────────────────── Chart ───────────────────────── + + private void Run(SKRect box) + { + // The chart area: white with a light outline unless the file says otherwise. + var area = _chart.Area; + var fill = FillOf(area, SKColors.White); + var outline = PenOf(area.Line, area.Line is null ? GridGray : null, 0.75f); + var corner = _chart.RoundedCorners && outline is not null ? Math.Min(10 * _s, Math.Min(box.Width, box.Height) / 8) : 0; + Shape(canvas => + { + using var path = new SKPath(); + var inset = outline is { } pen ? pen.Width / 2 : 0; + var rect = SKRect.Inflate(box, -inset, -inset); + if (corner > 0) + path.AddRoundRect(rect, corner, corner); + else + path.AddRect(rect); + FillPath(canvas, path, fill); + StrokePath(canvas, path, outline); + }); + + var pad = 7 * _s; + var content = new SKRect(box.Left + pad, box.Top + pad, box.Right - pad, box.Bottom - pad); + if (content.Width <= 0 || content.Height <= 0) + return; + content = Title(content, box); + content = Legend(content, box); + if (content.Width <= 4 || content.Height <= 4) + return; + Plots(content, box); + } + + private SKRect Title(SKRect content, SKRect box) + { + if (_chart.Title is not { Lines.Count: > 0 } title) + return content; + var lines = title.Lines.Select(l => Text(l, title.Style)).ToList(); + var height = lines.Sum(l => l.Height); + var y = content.Top; + float? x = null; + if (title.Layout is { } layout) + { + x = box.Left + (float)layout.X * box.Width; + y = box.Top + (float)layout.Y * box.Height; + } + foreach (var line in lines) + { + TextAt(line, x ?? content.MidX - line.Width / 2, y); + y += line.Height; + } + if (title.Overlay || title.Layout is not null) + return content; + return new SKRect(content.Left, content.Top + height + 6 * _s, content.Right, content.Bottom); + } + + // ───────────────────────── Legend ───────────────────────── + + private enum KeyKind + { + Box, + Line, + } + + private sealed record LegendEntry(string Text, KeyKind Kind, SKColor? Fill, Pen? Line, ChartMarker? Marker, SKColor MarkerColor); + + private List LegendEntries() + { + var entries = new List(); + var plots = _chart.Plots; + // One pie, doughnut or other plot of one series in varied colours: an entry per category. + if (plots.Count == 1 && plots[0] is { Series.Count: > 0 } only + && (only.Kind is ChartKind.Pie || only.Kind == ChartKind.Doughnut || only.VaryColors && only.Series.Count == 1 && only.Kind == ChartKind.Bar)) + { + var series = only.Series[0]; + var count = Math.Min(series.Values.Count, MaxLabels); + for (var i = 0; i < count; i++) + { + var style = series.Points.GetValueOrDefault(i) ?? series.Style; + var auto = _chart.Palette.Color(i); + entries.Add(new LegendEntry(Category(series, i), KeyKind.Box, FillOf(style, auto), PenOf(style.Line, null, 0.75f), null, auto)); + } + return entries; + } + foreach (var plot in plots) + { + foreach (var series in plot.Series) + { + if (entries.Count >= MaxLabels) + return entries; + var auto = _chart.Palette.Color(series.Index); + var name = series.Name ?? $"Series{series.Index + 1}"; + if (plot.Kind is ChartKind.Line or ChartKind.Scatter) + { + var line = plot.Lines ? PenOf(series.Style.Line, auto, plot.Kind == ChartKind.Line ? 2.25f : 1.5f) : null; + entries.Add(new LegendEntry(name, KeyKind.Line, null, line, MarkerOf(plot, series), auto)); + } + else + { + entries.Add(new LegendEntry(name, KeyKind.Box, FillOf(series.Style, auto), PenOf(series.Style.Line, null, 0.75f), null, auto)); + } + } + } + return entries; + } + + private SKRect Legend(SKRect content, SKRect box) + { + if (_chart.Legend is not { } legend) + return content; + var entries = LegendEntries(); + entries = [.. entries.Where((_, i) => !legend.Deleted.Contains(i))]; + if (entries.Count == 0) + return content; + var s = _s; + var texts = entries.Select(e => Text(e.Text, legend.Style)).ToList(); + var keyWidth = entries.Any(e => e.Kind == KeyKind.Line) ? 20 * s : 7 * s; + var gap = 4 * s; + var lineHeight = Math.Max(texts.Max(t => t.Height), 7 * s); + var vertical = legend.Position is "r" or "l" or "tr"; + var maxText = Math.Min(texts.Max(t => t.Width), (vertical ? 0.45f : 0.9f) * content.Width); + var entryWidth = keyWidth + gap + maxText; + + // Entries in rows (top, bottom) or one column (right, left). + var placed = new List<(int Index, float X, float Y)>(); + float width, height; + if (vertical) + { + var rows = Math.Max(1, (int)Math.Min(entries.Count, (content.Height + 2 * s) / (lineHeight + 2 * s))); + width = entryWidth + 2 * gap; + height = rows * lineHeight + (rows - 1) * 2 * s + 2 * gap; + for (var i = 0; i < rows; i++) + placed.Add((i, gap, gap + i * (lineHeight + 2 * s))); + } + else + { + var rows = new List>(); + var row = new List<(int Index, float Width)>(); + var x = 0f; + for (var i = 0; i < entries.Count; i++) + { + var w = keyWidth + gap + Math.Min(texts[i].Width, maxText); + if (row.Count > 0 && x + w > content.Width - 2 * gap) + { + rows.Add(row); + row = []; + x = 0; + if ((rows.Count + 1) * (lineHeight + 2 * s) > content.Height / 2) + break; + } + row.Add((i, w)); + x += w + 12 * s; + } + if (row.Count > 0 && (rows.Count + 1) * (lineHeight + 2 * s) <= Math.Max(content.Height / 2, lineHeight + 2 * s)) + rows.Add(row); + float RowWidth(List<(int Index, float Width)> r) => r.Sum(e => e.Width) + 12 * s * (r.Count - 1); + var widest = rows.Count == 0 ? 0 : rows.Max(RowWidth); + width = widest + 2 * gap; + height = rows.Count * lineHeight + Math.Max(0, rows.Count - 1) * 2 * s + 2 * gap; + for (var r = 0; r < rows.Count; r++) + { + var left0 = gap + (widest - RowWidth(rows[r])) / 2; + foreach (var (index, w) in rows[r]) + { + placed.Add((index, left0, gap + r * (lineHeight + 2 * s))); + left0 += w + 12 * s; + } + } + } + + float left, top; + if (legend.Layout is { } layout) + { + left = box.Left + (float)layout.X * box.Width; + top = box.Top + (float)layout.Y * box.Height; + } + else + { + (left, top) = legend.Position switch + { + "l" => (content.Left, content.MidY - height / 2), + "t" => (content.MidX - width / 2, content.Top), + "b" => (content.MidX - width / 2, content.Bottom - height), + "tr" => (content.Right - width, content.Top), + _ => (content.Right - width, content.MidY - height / 2), + }; + } + foreach (var (index, x, y) in placed) + { + var entry = entries[index]; + var keyLeft = left + x; + var middle = top + y + lineHeight / 2; + LegendKey(entry, new SKRect(keyLeft, middle - 3.5f * s, keyLeft + keyWidth, middle + 3.5f * s)); + var text = texts[index]; + TextAt(text, keyLeft + keyWidth + gap, middle - text.Height / 2); + } + if (legend.Overlay || legend.Layout is not null) + return content; + var margin = 6 * s; + return legend.Position switch + { + "l" => new SKRect(content.Left + width + margin, content.Top, content.Right, content.Bottom), + "t" => new SKRect(content.Left, content.Top + height + margin, content.Right, content.Bottom), + "b" => new SKRect(content.Left, content.Top, content.Right, content.Bottom - height - margin), + _ => new SKRect(content.Left, content.Top, content.Right - width - margin, content.Bottom), + }; + } + + private void LegendKey(LegendEntry entry, SKRect key) + { + if (entry.Kind == KeyKind.Box) + { + var square = SKRect.Create(key.Left, key.MidY - key.Height / 2, key.Height, key.Height); + var (fill, line) = (entry.Fill, entry.Line); + Shape(canvas => + { + using var path = new SKPath(); + path.AddRect(square); + FillPath(canvas, path, fill); + StrokePath(canvas, path, line); + }); + return; + } + var pen = entry.Line; + var (y, x0, x1) = (key.MidY, key.Left, key.Right); + Shape(canvas => + { + using var path = new SKPath(); + path.MoveTo(x0, y); + path.LineTo(x1, y); + StrokePath(canvas, path, pen); + }); + if (entry.Marker is { } marker) + Marker(marker, entry.MarkerColor, new SKPoint(key.MidX, y), counted: false); + } + + // ───────────────────────── Plot area ───────────────────────── + + /// An axis as drawn: which way it runs, its scale (values) or count (categories), and its labels. + private sealed class Axis(ChartAxis definition, bool horizontal) + { + public ChartAxis Definition { get; } = definition; + public bool Horizontal { get; } = horizontal; + public bool IsValue => Definition.IsValue; + public double Low { get; set; } = double.PositiveInfinity; + public double High { get; set; } = double.NegativeInfinity; + public bool Percent { get; set; } + public string? Format { get; set; } + public Scale? Scale { get; set; } + public int Count { get; set; } + public IReadOnlyList Labels { get; set; } = []; + + /// Points between the category ticks (bars always are). + public bool Between { get; set; } = true; + + /// The other axis of the pair. + public Axis? Cross { get; set; } + + public bool Shown => !Definition.Deleted; + + /// A bar plot is drawn against the axis (its points sit between the ticks). + public bool HasBars { get; set; } + + /// The category labels last set for a length of the axis. + public (float Length, CategoryLabels? Labels)? Measured { get; set; } + + /// The side the labels and title are on: b or t (horizontal), l or r (vertical). + public string Side => Horizontal ? Definition.Position == "t" ? "t" : "b" : Definition.Position == "r" ? "r" : "l"; + + public void Include(double value) + { + if (!double.IsFinite(value)) + return; + Low = Math.Min(Low, value); + High = Math.Max(High, value); + } + + /// Where a category (0-based) or value sits along the axis, 0 to 1. + public double Fraction(double value) + { + double f; + if (Scale is { } scale) + return scale.Fraction(value); + var n = Math.Max(1, Count); + f = Between ? (value + 0.5) / n : n == 1 ? 0.5 : value / (n - 1); + return Definition.Reversed ? 1 - f : f; + } + + /// The edge of category slot 's start (0) or end (1) along the axis. + public double Edge(double index) + { + var n = Math.Max(1, Count); + var f = Between ? index / n : n == 1 ? index : (index - 0.5) / (n - 1); + return Definition.Reversed ? 1 - f : f; + } + } + + /// A value axis' range and unit. + private sealed class Scale(double min, double max, double unit, double? log, bool reversed) + { + public double Min { get; } = min; + public double Max { get; } = max; + public double Unit { get; } = unit; + public double? Log { get; } = log; + + public double Fraction(double value) + { + double f; + if (Log is not null) + { + var lo = Math.Log(Min); + var hi = Math.Log(Max); + f = value > 0 ? (Math.Log(value) - lo) / (hi - lo) : double.NegativeInfinity; + } + else + { + f = (value - Min) / (Max - Min); + } + if (double.IsNaN(f)) + f = 0; + f = Math.Clamp(f, -100, 100); + return reversed ? 1 - f : f; + } + + public List Ticks() + { + var ticks = new List(); + if (Log is { } logBase) + { + for (var v = Min; v <= Max * (1 + 1e-9) && ticks.Count < MaxTicks; v *= logBase) + ticks.Add(v); + return ticks; + } + for (var k = 0; ticks.Count < MaxTicks; k++) + { + var v = Min + k * Unit; + if (v > Max + Unit * 1e-9) + break; + ticks.Add(Math.Abs(v) < Unit * 1e-9 ? 0 : v); + } + return ticks; + } + + /// Where the crossing axis stands on this one (c:crosses), as a value. + public double Crossing(ChartAxis crossing) => crossing.Crosses switch + { + ChartCrossing.Min => Min, + ChartCrossing.Max => Max, + ChartCrossing.At => Math.Clamp(crossing.CrossesAt, Min, Max), + _ => Log is not null ? Min : Math.Clamp(0, Min, Max), + }; + } + + private void Plots(SKRect content, SKRect box) + { + var plotStyle = _chart.PlotArea; + if (_chart.Plots.All(p => p.Kind is ChartKind.Pie or ChartKind.Doughnut)) + { + var area = Manual(content, box); + PlotBackground(area, plotStyle); + foreach (var plot in _chart.Plots) + Round(plot, area); + return; + } + + // Axes in pairs, each plot against its own. + var axes = new Dictionary(); + var pairs = new List<(ChartPlot Plot, Axis Category, Axis Value)>(); + foreach (var plot in _chart.Plots) + { + if (plot.Kind is ChartKind.Pie or ChartKind.Doughnut) + continue; // a pie among other plots: not drawn (Office can't combine them either) + var horizontal = plot is { Kind: ChartKind.Bar, Horizontal: true }; + var first = AxisOf(axes, plot.AxisIds.ElementAtOrDefault(0), plot.AxisIds.Count > 0, value: plot.Kind == ChartKind.Scatter, horizontal: !horizontal, fallbackId: uint.MaxValue - 1); + var second = AxisOf(axes, plot.AxisIds.ElementAtOrDefault(1), plot.AxisIds.Count > 1, value: true, horizontal: horizontal, fallbackId: uint.MaxValue); + first.Cross = second; + second.Cross = first; + pairs.Add((plot, first, second)); + Extents(plot, first, second); + } + if (pairs.Count == 0) + return; + + // The plot area less what its axes need: vertical axes first (their labels don't depend on the width). + var manual = _chart.PlotLayout; + var outer = manual is { Inner: false } ? Manual(content, box) : content; + var inner = outer; + var vertical = axes.Values.Where(a => !a.Horizontal).ToList(); + var level = axes.Values.Where(a => a.Horizontal).ToList(); + foreach (var axis in vertical) + Prepare(axis, inner.Height * 0.85f); + if (manual is not { Inner: true }) + { + foreach (var axis in vertical) + { + var need = Need(axis, inner.Height, 0.4f * outer.Width); + inner = axis.Side == "r" ? new SKRect(inner.Left, inner.Top, inner.Right - need, inner.Bottom) : new SKRect(inner.Left + need, inner.Top, inner.Right, inner.Bottom); + } + } + foreach (var axis in level) + Prepare(axis, inner.Width); + if (manual is not { Inner: true }) + { + foreach (var axis in level) + { + var need = Need(axis, inner.Width, 0.5f * outer.Height); + inner = axis.Side == "t" ? new SKRect(inner.Left, inner.Top + need, inner.Right, inner.Bottom) : new SKRect(inner.Left, inner.Top, inner.Right, inner.Bottom - need); + } + // Room for the top value label's upper half and the right end of a level axis' last label. + inner = new SKRect(inner.Left, inner.Top + 4 * _s, inner.Right - 4 * _s, inner.Bottom); + } + else + { + inner = Manual(content, box); + } + if (inner.Width <= 2 || inner.Height <= 2) + return; + foreach (var axis in vertical) + Prepare(axis, inner.Height); + + PlotBackground(inner, plotStyle); + foreach (var axis in axes.Values) + Gridlines(axis, inner); + foreach (var (plot, category, value) in pairs) + { + switch (plot.Kind) + { + case ChartKind.Bar: + Bars(plot, category, value, inner); + break; + case ChartKind.Area: + Areas(plot, category, value, inner); + break; + case ChartKind.Line or ChartKind.Scatter: + Lines(plot, category, value, inner); + break; + } + } + foreach (var axis in axes.Values) + AxisLine(axis, inner); + } + + private Axis AxisOf(Dictionary axes, uint id, bool given, bool value, bool horizontal, uint fallbackId) + { + if (given && axes.TryGetValue(id, out var known)) + return known; + var definition = given ? _chart.Axis(id) : null; + // An axis the file doesn't define is drawn as Office's default one would be, without labels. + definition ??= new ChartAxis { Id = given ? id : fallbackId, IsValue = value, Position = horizontal ? "b" : "l", Deleted = !given }; + if (definition.IsValue != value) + definition = definition with { IsValue = value }; + var axis = new Axis(definition, horizontal); + axes[given ? id : fallbackId] = axis; + return axis; + } + + private SKRect Manual(SKRect content, SKRect box) => + _chart.PlotLayout is { } layout + ? SKRect.Create(box.Left + (float)layout.X * box.Width, box.Top + (float)layout.Y * box.Height, + (float)(layout.Width ?? 1 - layout.X) * box.Width, (float)(layout.Height ?? 1 - layout.Y) * box.Height) + : content; + + private void PlotBackground(SKRect area, ChartShapeStyle style) + { + var fill = FillOf(style, null); + var line = PenOf(style.Line, null, 0.75f); + if (fill is null && line is null) + return; + Shape(canvas => + { + using var path = new SKPath(); + path.AddRect(area); + FillPath(canvas, path, fill); + StrokePath(canvas, path, line); + }); + } + + // ───────────────────────── Axes ───────────────────────── + + /// Gathers the values (and category count) a plot puts on its two axes. + private static void Extents(ChartPlot plot, Axis first, Axis second) + { + var series = plot.Series; + var count = series.Count == 0 ? 0 : series.Max(s => Math.Max(s.Values.Count, s.Categories?.Count ?? 0)); + if (plot.Kind == ChartKind.Scatter) + { + foreach (var s in series) + { + for (var i = 0; i < s.Values.Count; i++) + { + if (s.Values[i] is not { } y) + continue; + var x = s.XValues is { } xs ? i < xs.Count ? xs[i] : null : i + 1; + if (x is not { } xv) + continue; + first.Include(xv); + second.Include(y); + } + } + first.Format ??= first.Definition.Format; + second.Format ??= second.Definition.Format ?? series.FirstOrDefault(s => s.Format is not null)?.Format; + return; + } + if (!first.IsValue) + { + first.Count = Math.Max(first.Count, count); + if (first.Labels.Count < count) + first.Labels = Enumerable.Range(0, Math.Min(count, ChartReader.MaxPoints)).Select(i => Category(series, i)).ToList(); + // Bars sit between ticks; lines and areas wherever the value axis says. + first.HasBars |= plot.Kind == ChartKind.Bar; + first.Between = first.HasBars || second.Definition.Between; + } + else + { + for (var i = 0; i < count; i++) + first.Include(i + 1); + } + second.Format ??= second.Definition.Format ?? (plot.Grouping == ChartGrouping.PercentStacked ? "0%" : series.FirstOrDefault(s => s.Format is not null)?.Format); + if (plot.Grouping == ChartGrouping.Standard) + { + foreach (var s in series) + { + foreach (var v in s.Values) + { + if (v is { } value) + second.Include(value); + } + } + if (plot.Kind is ChartKind.Bar or ChartKind.Area) + second.Include(0); + return; + } + var percent = plot.Grouping == ChartGrouping.PercentStacked; + second.Percent |= percent; + for (var i = 0; i < count; i++) + { + double up = 0, down = 0; + foreach (var s in series) + { + if (i < s.Values.Count && s.Values[i] is { } v) + { + if (v >= 0) + up += v; + else + down += v; + } + } + if (percent) + { + var total = up - down; + if (total > 0) + { + second.Include(up / total); + second.Include(down / total); + } + } + else + { + second.Include(up); + second.Include(down); + } + } + second.Include(0); + } + + /// Scales a value axis for points, or thins out a category axis' labels. + private void Prepare(Axis axis, float length) + { + if (!axis.IsValue) + return; + var definition = axis.Definition; + var (low, high) = double.IsFinite(axis.Low) ? (axis.Low, axis.High) : (0.0, 1.0); + if (axis.Percent && definition.Min is null && definition.Max is null) + { + axis.Scale = new Scale(low < -1e-9 ? -1 : 0, 1, definition.MajorUnit ?? (length / _s > 250 ? 0.1 : 0.2), null, definition.Reversed); + return; + } + if (definition.LogBase is { } logBase) + { + var positive = low > 0 ? low : high > 0 ? high / 100 : 1; + var top = high > 0 ? high : 1; + var min = definition.Min is > 0 and var lo ? lo : Math.Pow(logBase, Math.Floor(Math.Log(positive, logBase))); + var max = definition.Max is { } hi && hi > min ? hi : Math.Pow(logBase, Math.Ceiling(Math.Log(top, logBase))); + if (max <= min) + max = min * logBase; + axis.Scale = new Scale(min, max, logBase, logBase, definition.Reversed); + return; + } + var label = Text("0", definition.Style).Height; + var spacing = axis.Horizontal ? Math.Max(50 * _s, label * 4) : Math.Max(label * 2.2f, 22 * _s); + var target = Math.Clamp((int)(length / spacing), 2, 10); + for (var attempt = 0; attempt < 4; attempt++) + { + axis.Scale = AutoScale(low, high, definition, target); + if (!axis.Horizontal || !axis.Shown) + break; + // Level value labels must not touch: fewer of them when they do. + var ticks = axis.Scale.Ticks(); + var widest = ticks.Take(MaxMeasured).Select(t => Text(Format(t, axis.Format), definition.Style).Width).DefaultIfEmpty(0).Max(); + if (ticks.Count <= 1 || widest * 1.3f <= length / (ticks.Count - 1) || target <= 2) + break; + target = Math.Max(2, (int)(length / (widest * 1.4f))); + } + } + + private static Scale AutoScale(double low, double high, ChartAxis axis, int target) + { + if (axis.Min is { } fixedMin) + low = Math.Min(low, fixedMin); + if (axis.Max is { } fixedMax) + high = Math.Max(high, fixedMax); + if (high < low) + (low, high) = (high, low); + // Zero is kept unless all values sit far above (or below) it. + var min = axis.Min ?? (low >= 0 ? (high > 0 && (high - low) / high < 1 / 6.0 ? low : 0) : low); + var max = axis.Max ?? (high <= 0 ? (low < 0 && (high - low) / -low < 1 / 6.0 ? high : 0) : high); + if (max <= min) + { + var spread = Math.Abs(min) > 0 ? Math.Abs(min) / 10 : 1; + (min, max) = axis.Min is not null ? (min, min + spread * 10) : axis.Max is not null ? (max - spread * 10, max) : (min - spread, max + spread); + } + var unit = axis.MajorUnit ?? Nice((max - min) / target); + if (!(unit > 0) || !double.IsFinite(unit)) + unit = Math.Max(Math.Abs(max - min), 1e-300) / 5; + if ((max - min) / unit > MaxTicks) + unit = Nice((max - min) / 10); + // A little room above the highest value (and below the lowest), to the next unit. + if (axis.Max is null) + max = high <= 0 && max <= 0 ? Math.Ceiling(max / unit - 1e-9) * unit : Math.Ceiling((max + 0.05 * (max - min)) / unit - 1e-9) * unit; + if (axis.Min is null) + min = low >= 0 && min >= 0 && min == 0 ? 0 : Math.Floor((min - (min < 0 ? 0.05 * (max - min) : 0)) / unit + 1e-9) * unit; + if (max <= min) + max = min + unit; + return new Scale(min, max, unit, null, axis.Reversed); + } + + /// 1, 2 or 5 times a power of ten, at least . + private static double Nice(double value) + { + if (!(value > 0) || !double.IsFinite(value)) + return 1; + var power = Math.Pow(10, Math.Floor(Math.Log10(value))); + var f = value / power; + return (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10) * power; + } + + /// The label of category : the first series' that has one, else its number. + private static string Category(IReadOnlyList series, int index) + { + foreach (var s in series) + { + if (s.Categories is { } categories && index < categories.Count && categories[index] is { } label) + return label; + } + return series.Any(s => s.Categories is not null) ? "" : (index + 1).ToString(CultureInfo.InvariantCulture); + } + + private static string Category(ChartSeries series, int index) => Category([series], index); + + /// How a category axis' labels are set: every n-th one, level, wrapped or turned. + private sealed record CategoryLabels(int Step, float Rotation, List<(int Index, List Lines)> Labels); + + private CategoryLabels? Categories(Axis axis, float length) + { + if (axis.Measured is { } cached && cached.Length == length) + return cached.Labels; + var labels = MeasureCategories(axis, length); + axis.Measured = (length, labels); + return labels; + } + + private CategoryLabels? MeasureCategories(Axis axis, float length) + { + if (axis.IsValue || axis.Definition.LabelPosition == "none" || axis.Count == 0) + return null; + var style = axis.Definition.Style; + var n = axis.Count; + var slot = length / n; + var step = axis.Definition.LabelSkip > 0 ? axis.Definition.LabelSkip : Math.Max(1, (int)Math.Ceiling(n / (double)MaxMeasured)); + var lineHeight = Text("Ag", style).Height; + List<(int, List)> Measure(int every) => + [.. Enumerable.Range(0, (n + every - 1) / every).Select(k => k * every).Select(i => (i, new List { Text(Label(axis, i), style) }))]; + + if (!axis.Horizontal) + { + // Labels beside a vertical axis: as many as fit one above the other. + if (axis.Definition.LabelSkip == 0) + step = Math.Max(step, (int)Math.Ceiling(lineHeight * 1.1 / Math.Max(slot, 0.01))); + return new CategoryLabels(step, 0, Measure(step)); + } + var first = step; + var labels = Measure(step); + var widest = labels.Count == 0 ? 0 : labels.Max(l => l.Item2[0].Width); + var fixedRotation = axis.Definition.LabelRotation; + if (fixedRotation is { } turn && turn != 0) + { + var across = Math.Abs(Math.Sin(turn * Math.PI / 180)) > 0.5 ? lineHeight : widest; + if (axis.Definition.LabelSkip == 0) + step = Math.Max(step, (int)Math.Ceiling(across * 1.1 / Math.Max(slot, 0.01))); + return new CategoryLabels(step, (float)turn, step == first ? labels : Measure(step)); + } + if (widest <= slot * step * 0.95f) + return new CategoryLabels(step, 0, labels); + // Wrapped at spaces into up to three lines, each within its slot. + var wrapped = new List<(int, List)>(); + var fits = true; + foreach (var (index, lines) in labels) + { + var broken = Wrap(lines[0].Text, style, slot * step * 0.95f, 3); + if (broken is null) + { + fits = false; + break; + } + wrapped.Add((index, broken)); + } + if (fits) + return new CategoryLabels(step, 0, wrapped); + if (fixedRotation is null) + { + // Turned 45°: thinned out until their line heights fit side by side. + if (axis.Definition.LabelSkip == 0) + step = Math.Max(step, (int)Math.Ceiling(lineHeight * 1.5 / Math.Max(slot, 0.01))); + return new CategoryLabels(step, -45, step == first ? labels : Measure(step)); + } + if (axis.Definition.LabelSkip == 0) + step = Math.Max(step, (int)Math.Ceiling(widest * 1.1 / Math.Max(slot, 0.01))); + return new CategoryLabels(step, 0, Measure(step)); + } + + private static string Label(Axis axis, int index) => index < axis.Labels.Count ? axis.Labels[index] : (index + 1).ToString(CultureInfo.InvariantCulture); + + /// The text broken at spaces into lines no wider than ; null when a word is wider or more lines are needed. + private List? Wrap(string text, ChartTextStyle style, float width, int maxLines) + { + var whole = Text(text, style); + if (whole.Width <= width) + return [whole]; + var words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (words.Length < 2) + return null; + var lines = new List(); + var current = ""; + foreach (var word in words) + { + var candidate = current.Length == 0 ? word : current + " " + word; + if (Text(candidate, style).Width <= width) + { + current = candidate; + continue; + } + if (current.Length == 0) + return null; + lines.Add(Text(current, style)); + current = word; + if (Text(word, style).Width > width || lines.Count >= maxLines) + return null; + } + lines.Add(Text(current, style)); + return lines.Count <= maxLines ? lines : null; + } + + /// How far an axis' labels, ticks and title reach out from the plot (points). + private float Need(Axis axis, float length, float cap) + { + if (!axis.Shown) + return 0; + var definition = axis.Definition; + var need = definition.MajorTicks is "out" or "cross" ? 4 * _s : 0; + if (definition.LabelPosition != "none") + { + need += 3 * _s; + if (axis.IsValue && axis.Scale is { } scale) + { + var texts = scale.Ticks().Take(MaxMeasured).Select(t => Text(Format(t, axis.Format), definition.Style)).ToList(); + if (texts.Count > 0) + need += axis.Horizontal ? texts.Max(t => t.Height) : texts.Max(t => t.Width); + } + else if (Categories(axis, length) is { } categories && categories.Labels.Count > 0) + { + if (!axis.Horizontal) + need += categories.Labels.Max(l => l.Lines.Max(t => t.Width)); + else if (categories.Rotation != 0) + { + var radians = Math.Abs(categories.Rotation) * Math.PI / 180; + var widest = categories.Labels.Max(l => l.Lines.Max(t => t.Width)); + var height = categories.Labels.Max(l => l.Lines.Max(t => t.Height)); + need += (float)(widest * Math.Sin(radians) + height * Math.Cos(radians)); + } + else + { + need += categories.Labels.Max(l => l.Lines.Sum(t => t.Height)); + } + } + } + if (definition.Title is { Lines.Count: > 0 } title) + need += title.Lines.Sum(l => Text(l, title.Style).Height) + 4 * _s; + return Math.Min(need, cap); + } + + private float Along(Axis axis, double fraction, SKRect inner) => + axis.Horizontal ? inner.Left + (float)fraction * inner.Width : inner.Bottom - (float)fraction * inner.Height; + + /// Where an axis' line stands across the plot: at the crossing on the other axis (points). + private float LinePosition(Axis axis, SKRect inner) + { + if (axis.Cross is not { } other) + return axis.Horizontal ? inner.Bottom : inner.Left; + double fraction; + if (other.Scale is { } scale) + fraction = scale.Fraction(scale.Crossing(axis.Definition)); + else + { + var n = Math.Max(1, other.Count); + var index = axis.Definition.Crosses switch + { + ChartCrossing.Max => other.Between ? n : n - 1, + ChartCrossing.At => Math.Clamp(axis.Definition.CrossesAt - 1, 0, other.Between ? n : n - 1), + _ => 0, + }; + fraction = other.Between ? other.Edge(index) : other.Fraction(index); + } + return Along(other, Math.Clamp(fraction, 0, 1), inner); + } + + private void Gridlines(Axis axis, SKRect inner) + { + var definition = axis.Definition; + if (definition.MajorGridlines is null && definition.MinorGridlines is null) + return; + var lines = new List<(float Position, Pen Pen)>(); + void Add(ChartLine? line, IEnumerable fractions) + { + if (PenOf(line, GridGray, 0.75f) is not { } pen) + return; + foreach (var f in fractions) + { + if (f is >= -1e-6 and <= 1 + 1e-6) + lines.Add((Along(axis, f, inner), pen)); + } + } + if (axis.Scale is { } scale) + { + var ticks = scale.Ticks(); + if (definition.MinorGridlines is not null && scale.Log is null && ticks.Count < MaxTicks / 5) + Add(definition.MinorGridlines, Enumerable.Range(0, (ticks.Count - 1) * 5 + 1).Select(k => scale.Fraction(scale.Min + k * scale.Unit / 5))); + Add(definition.MajorGridlines, ticks.Select(scale.Fraction)); + } + else if (axis.Count > 0) + { + var count = Math.Min(axis.Count, MaxTicks); + Add(definition.MajorGridlines, axis.Between + ? Enumerable.Range(0, count + 1).Select(i => axis.Edge(i)) + : Enumerable.Range(0, count).Select(i => axis.Fraction(i))); + } + if (lines.Count == 0) + return; + var horizontal = axis.Horizontal; + Shape(canvas => + { + foreach (var (position, pen) in lines) + { + using var path = new SKPath(); + if (horizontal) + { + path.MoveTo(position, inner.Top); + path.LineTo(position, inner.Bottom); + } + else + { + path.MoveTo(inner.Left, position); + path.LineTo(inner.Right, position); + } + StrokePath(canvas, path, pen); + } + }); + } + + /// An axis' line, ticks, labels and title. + private void AxisLine(Axis axis, SKRect inner) + { + if (!axis.Shown) + return; + var definition = axis.Definition; + var at = LinePosition(axis, inner); + var side = axis.Side; + var outward = side is "b" or "r" ? 1 : -1; + var pen = PenOf(definition.Line, AxisGray, 0.75f); + var tick = 4 * _s; + var (tickFrom, tickTo) = definition.MajorTicks switch + { + "out" => (0f, tick), + "in" => (-tick, 0f), + "cross" => (-tick, tick), + _ => (0f, 0f), + }; + var fractions = axis.Scale is { } scale ? scale.Ticks().Select(scale.Fraction).ToList() + : axis.Between ? [.. Enumerable.Range(0, Math.Min(axis.Count, MaxTicks) + 1).Select(i => axis.Edge(i))] + : [.. Enumerable.Range(0, Math.Min(axis.Count, MaxTicks)).Select(i => axis.Fraction(i))]; + if (pen is { } p) + { + var horizontal = axis.Horizontal; + var marks = tickFrom != tickTo ? fractions.Where(f => f is >= -1e-6 and <= 1 + 1e-6).Select(f => Along(axis, f, inner)).ToList() : []; + Shape(canvas => + { + using var path = new SKPath(); + if (horizontal) + { + path.MoveTo(inner.Left, at); + path.LineTo(inner.Right, at); + foreach (var x in marks) + { + path.MoveTo(x, at + tickFrom * outward); + path.LineTo(x, at + tickTo * outward); + } + } + else + { + path.MoveTo(at, inner.Top); + path.LineTo(at, inner.Bottom); + foreach (var y in marks) + { + path.MoveTo(at + tickFrom * outward, y); + path.LineTo(at + tickTo * outward, y); + } + } + StrokePath(canvas, path, p); + }); + } + + // Labels next to the line, or at the low or high edge of the plot. + var labelsAt = definition.LabelPosition switch + { + "low" => axis.Horizontal ? (axis.Cross?.Definition.Reversed == true ? inner.Top : inner.Bottom) : (axis.Cross?.Definition.Reversed == true ? inner.Right : inner.Left), + "high" => axis.Horizontal ? (axis.Cross?.Definition.Reversed == true ? inner.Bottom : inner.Top) : (axis.Cross?.Definition.Reversed == true ? inner.Left : inner.Right), + _ => at, + }; + var offset = (definition.MajorTicks is "out" or "cross" ? tick : 0) + 3 * _s; + var reach = offset; + if (definition.LabelPosition != "none") + { + if (axis.Scale is { } valueScale) + { + foreach (var value in valueScale.Ticks()) + { + var text = Text(Format(value, axis.Format), definition.Style); + var position = Along(axis, valueScale.Fraction(value), inner); + Place(text, axis.Horizontal, position, labelsAt, offset, outward); + reach = Math.Max(reach, offset + (axis.Horizontal ? text.Height : text.Width)); + } + } + else if (Categories(axis, axis.Horizontal ? inner.Width : inner.Height) is { } categories) + { + foreach (var (index, lines) in categories.Labels) + { + var position = Along(axis, axis.Fraction(index), inner); + if (categories.Rotation != 0) + { + var line = lines[0]; + // Turned labels end at their tick. + var radians = categories.Rotation * (float)Math.PI / 180; + var anchor = axis.Horizontal ? new SKPoint(position, labelsAt + offset * outward) : new SKPoint(labelsAt + offset * outward, position); + var local = SKMatrix.CreateTranslation(anchor.X, anchor.Y).PreConcat(SKMatrix.CreateRotationDegrees(categories.Rotation)); + var x = categories.Rotation < 0 ? -line.Width : 0; + _texts.Add((line, x, (line.Ascent - line.Descent) / 2, local)); + reach = Math.Max(reach, offset + (float)Math.Abs(line.Width * Math.Sin(radians)) + line.Height); + continue; + } + var total = lines.Sum(l => l.Height); + var y = 0f; + foreach (var line in lines) + { + if (axis.Horizontal) + { + var top = outward > 0 ? labelsAt + offset + y : labelsAt - offset - total + y; + TextAt(line, position - line.Width / 2, top); + } + else + { + Place(line, false, position - total / 2 + y + line.Height / 2, labelsAt, offset, outward); + } + y += line.Height; + } + reach = Math.Max(reach, offset + (axis.Horizontal ? total : lines.Max(l => l.Width))); + } + } + } + + // The title beyond the labels, turned for a vertical axis. + if (definition.Title is { Lines.Count: > 0 } title) + { + var lines = title.Lines.Select(l => Text(l, title.Style)).ToList(); + var height = lines.Sum(l => l.Height); + var edge = axis.Horizontal ? (outward > 0 ? Math.Max(labelsAt, inner.Bottom) : Math.Min(labelsAt, inner.Top)) : (outward > 0 ? Math.Max(labelsAt, inner.Right) : Math.Min(labelsAt, inner.Left)); + var distance = reach + 4 * _s; + var y = -height / 2; + foreach (var line in lines) + { + if (axis.Horizontal) + TextCentred(line, inner.MidX, edge + outward * (distance + height / 2) + y + line.Height / 2); + else + TextCentred(line, edge + outward * (distance + height / 2) + (y + line.Height / 2) * -outward, inner.MidY, outward > 0 ? 90 : -90); + y += line.Height; + } + } + } + + /// A label beside a level (or vertical) axis at along it. + private void Place(ChartTextLine text, bool horizontal, float position, float line, float offset, int outward) + { + if (horizontal) + { + var top = outward > 0 ? line + offset : line - offset - text.Height; + TextAt(text, position - text.Width / 2, top); + } + else + { + var left = outward > 0 ? line + offset : line - offset - text.Width; + TextAt(text, left, position - text.Height / 2); + } + } + + // ───────────────────────── Bars ───────────────────────── + + private SKColor SeriesColor(ChartPlot plot, ChartSeries series, int point) => + plot.VaryColors && plot.Series.Count == 1 && plot.Kind is ChartKind.Bar or ChartKind.Pie or ChartKind.Doughnut + ? _chart.Palette.Color(point) + : _chart.Palette.Color(series.Index); + + private void Bars(ChartPlot plot, Axis category, Axis value, SKRect inner) + { + if (value.Scale is not { } scale) + return; + var series = plot.Series; + var n = series.Count; + if (n == 0 || category.Count == 0) + return; + var stacked = plot.Grouping != ChartGrouping.Standard; + var percent = plot.Grouping == ChartGrouping.PercentStacked; + var gap = plot.GapWidth / 100.0; + var overlap = stacked ? 1 : plot.Overlap / 100.0; + var bars = stacked ? 1 : n; + // In slot units: bars of width w overlapping by overlap, a gap of gap × w shared out on both sides. + var width = 1 / (bars - (bars - 1) * overlap + gap); + var baseline = scale.Crossing(category.Definition); + var count = Math.Min(category.Count, ChartReader.MaxPoints); + var totals = percent ? Totals(series, count) : null; + var rects = new List<(SKRect Rect, SKColor? Fill, Pen? Line)>(); + var labels = new List<(ChartSeries Series, int Index, double Value, SKRect Rect, bool Negative)>(); + var up = new double[count]; + var down = new double[count]; + for (var k = 0; k < n; k++) + { + var s = series[k]; + for (var i = 0; i < Math.Min(count, s.Values.Count); i++) + { + if (s.Values[i] is not { } v) + continue; + var shown = percent ? totals![i] > 0 ? v / totals[i] : 0 : v; + double from, to; + if (stacked) + { + if (shown >= 0) + { + from = up[i]; + up[i] += shown; + to = up[i]; + } + else + { + from = down[i]; + down[i] += shown; + to = down[i]; + } + } + else + { + (from, to) = (baseline, shown); + } + // The slot from its start: the half gap, then the k-th bar. + var start = gap * width / 2 + (stacked ? 0 : k * width * (1 - overlap)); + var a = category.Edge(i); + var b = category.Edge(i + 1); + var c0 = a + (b - a) * start; + var c1 = a + (b - a) * (start + width); + var v0 = scale.Fraction(from); + var v1 = scale.Fraction(to); + SKRect rect; + if (category.Horizontal) + rect = new SKRect(Along(category, Math.Min(c0, c1), inner), Along(value, Math.Max(v0, v1), inner), Along(category, Math.Max(c0, c1), inner), Along(value, Math.Min(v0, v1), inner)); + else + rect = new SKRect(Along(value, Math.Min(v0, v1), inner), Along(category, Math.Max(c0, c1), inner), Along(value, Math.Max(v0, v1), inner), Along(category, Math.Min(c0, c1), inner)); + var style = s.Points.GetValueOrDefault(i) ?? s.Style; + var auto = SeriesColor(plot, s, i); + rects.Add((rect, FillOf(style, auto), PenOf(style.Line, null, 0.75f))); + labels.Add((s, i, v, rect, to < from)); + } + } + Clipped(inner, canvas => + { + foreach (var (rect, fill, line) in rects) + { + using var path = new SKPath(); + path.AddRect(rect); + FillPath(canvas, path, fill); + StrokePath(canvas, path, line); + } + }); + foreach (var (s, i, v, rect, negative) in labels) + { + if ((s.Labels ?? plot.Labels) is not { Any: true } options || DataLabel(plot, s, i, v, options) is not { } text) + continue; + var position = options.Position ?? (stacked ? "ctr" : "outEnd"); + // Along the bar: its far end is the top of a column (or bottom of a negative one), the right of a bar. + var grows = category.Horizontal ? (negative ^ value.Definition.Reversed ? 1 : -1) : (negative ^ value.Definition.Reversed ? -1 : 1); + var pad = 3 * _s; + if (category.Horizontal) + { + var end = grows < 0 ? rect.Top : rect.Bottom; + var baseEnd = grows < 0 ? rect.Bottom : rect.Top; + var cy = position switch + { + "ctr" => rect.MidY, + "inEnd" => end - grows * (pad + text.Height / 2), + "inBase" => baseEnd + grows * (pad + text.Height / 2), + _ => end + grows * (pad + text.Height / 2), + }; + TextCentred(text, rect.MidX, cy); + } + else + { + var end = grows > 0 ? rect.Right : rect.Left; + var baseEnd = grows > 0 ? rect.Left : rect.Right; + var cx = position switch + { + "ctr" => rect.MidX, + "inEnd" => end - grows * (pad + text.Width / 2), + "inBase" => baseEnd + grows * (pad + text.Width / 2), + _ => end + grows * (pad + text.Width / 2), + }; + TextCentred(text, cx, rect.MidY); + } + } + } + + private static double[] Totals(List series, int count) + { + var totals = new double[count]; + foreach (var s in series) + { + for (var i = 0; i < Math.Min(count, s.Values.Count); i++) + totals[i] += Math.Abs(s.Values[i] ?? 0); + } + return totals; + } + + /// The text of a data label (series name, category, value, percent), or null when it shows nothing or too many are drawn. + private ChartTextLine? DataLabel(ChartPlot plot, ChartSeries series, int index, double value, ChartLabels options, double? percent = null) + { + if (++_labels > MaxLabels) + return null; + var parts = new List(4); + if (options.Series && series.Name is { Length: > 0 } name) + parts.Add(name); + if (options.Category) + parts.Add(Category(series, index)); + if (options.Value) + parts.Add(Format(value, options.Format ?? series.Format)); + if (options.Percent && percent is { } share) + parts.Add(Format(share, "0%")); + _ = plot; + return parts.Count == 0 ? null : Text(string.Join(", ", parts), options.Style); + } + + // ───────────────────────── Lines, areas, scatter ───────────────────────── + + /// The marker a line or scatter series draws, or null for none. + private static ChartMarker? MarkerOf(ChartPlot plot, ChartSeries series) + { + var symbol = series.Marker?.Symbol ?? "auto"; + if (symbol == "none" || series.Marker is null && !plot.Markers) + return null; + if (symbol == "auto") + symbol = AutoSymbols[series.Index % AutoSymbols.Length]; + return new ChartMarker(symbol, series.Marker?.Size ?? 5, series.Marker?.Style ?? ChartShapeStyle.Auto); + } + + private void Lines(ChartPlot plot, Axis x, Axis y, SKRect inner) + { + if (y.Scale is not { } scale) + return; + var scatter = plot.Kind == ChartKind.Scatter; + var percent = plot.Grouping == ChartGrouping.PercentStacked; + var stacked = plot.Grouping != ChartGrouping.Standard; + var count = plot.Series.Count == 0 ? 0 : plot.Series.Max(s => s.Values.Count); + var totals = percent ? Totals(plot.Series, count) : null; + var sums = new double[count]; + foreach (var series in plot.Series) + { + var points = new List(series.Values.Count); + for (var i = 0; i < series.Values.Count; i++) + { + var v = series.Values[i]; + if (v is null && !scatter && _chart.BlanksAs == "zero") + v = 0; + if (v is not { } value) + { + points.Add(null); + continue; + } + if (stacked) + { + sums[i] += percent ? totals![i] > 0 ? value / totals[i] : 0 : value; + value = sums[i]; + } + double position; + if (scatter) + { + var xv = series.XValues is { } xs ? i < xs.Count ? xs[i] : null : i + 1; + if (xv is not { } xValue || x.Scale is not { } xScale) + { + points.Add(null); + continue; + } + position = xScale.Fraction(xValue); + } + else + { + position = x.Fraction(i); + } + var px = Along(x, position, inner); + var py = Along(y, scale.Fraction(value), inner); + points.Add(x.Horizontal ? new SKPoint(px, py) : new SKPoint(py, px)); + } + var auto = _chart.Palette.Color(series.Index); + var pen = plot.Lines ? PenOf(series.Style.Line, auto, scatter ? 1.5f : 2.25f) : null; + var span = _chart.BlanksAs == "span" || scatter; + if (pen is not null) + { + var runs = Runs(points, span); + var smooth = series.Smooth; + Clipped(inner, canvas => + { + foreach (var run in runs) + { + using var path = Polyline(run, smooth); + StrokePath(canvas, path, pen, round: true); + } + }); + } + if (MarkerOf(plot, series) is { } marker) + { + for (var i = 0; i < points.Count; i++) + { + if (points[i] is { } point && SKRect.Inflate(inner, 1, 1).Contains(point)) + Marker(marker with { Style = series.Points.GetValueOrDefault(i) is { } own ? own : marker.Style }, auto, point, counted: true); + } + } + if ((series.Labels ?? plot.Labels) is { Any: true } options) + { + for (var i = 0; i < points.Count; i++) + { + if (points[i] is not { } point || series.Values[i] is not { } v || DataLabel(plot, series, i, v, options) is not { } text) + continue; + var pad = 4 * _s + (MarkerOf(plot, series)?.Size ?? 0) * _s / 2; + var (cx, cy) = (options.Position ?? "r") switch + { + "t" => (point.X, point.Y - pad - text.Height / 2), + "b" => (point.X, point.Y + pad + text.Height / 2), + "l" => (point.X - pad - text.Width / 2, point.Y), + "ctr" => (point.X, point.Y), + _ => (point.X + pad + text.Width / 2, point.Y), + }; + TextCentred(text, cx, cy); + } + } + } + } + + /// The unbroken stretches of a line: blanks break it unless it spans them. + private static List Runs(List points, bool span) + { + var runs = new List(); + var current = new List(); + foreach (var point in points) + { + if (point is { } p) + { + current.Add(p); + continue; + } + if (span) + continue; + if (current.Count > 0) + runs.Add([.. current]); + current.Clear(); + } + if (current.Count > 0) + runs.Add([.. current]); + return runs; + } + + private static SKPath Polyline(SKPoint[] points, bool smooth) + { + var path = new SKPath(); + if (points.Length == 0) + return path; + path.MoveTo(points[0]); + if (points.Length == 1) + { + path.LineTo(points[0]); + return path; + } + for (var i = 1; i < points.Length; i++) + { + if (!smooth) + { + path.LineTo(points[i]); + continue; + } + // Catmull-Rom through the points, as cubic Béziers. + var p0 = points[Math.Max(0, i - 2)]; + var p1 = points[i - 1]; + var p2 = points[i]; + var p3 = points[Math.Min(points.Length - 1, i + 1)]; + path.CubicTo(p1.X + (p2.X - p0.X) / 6, p1.Y + (p2.Y - p0.Y) / 6, p2.X - (p3.X - p1.X) / 6, p2.Y - (p3.Y - p1.Y) / 6, p2.X, p2.Y); + } + return path; + } + + private void Marker(ChartMarker marker, SKColor auto, SKPoint center, bool counted) + { + if (counted && ++_markers > MaxMarkers) + return; + var size = marker.Size * _s; + var fill = FillOf(marker.Style, auto); + var line = PenOf(marker.Style.Line, auto, 0.75f); + var symbol = marker.Symbol; + Shape(canvas => + { + using var path = new SKPath(); + var (x, y, h) = (center.X, center.Y, size / 2); + var open = false; + switch (symbol) + { + case "circle": + path.AddCircle(x, y, h); + break; + case "dot": + path.AddCircle(x, y, h / 2); + break; + case "diamond": + path.AddPoly([new(x, y - h), new(x + h, y), new(x, y + h), new(x - h, y)]); + break; + case "triangle": + path.AddPoly([new(x, y - h), new(x + h, y + h), new(x - h, y + h)]); + break; + case "x": + path.MoveTo(x - h, y - h); + path.LineTo(x + h, y + h); + path.MoveTo(x + h, y - h); + path.LineTo(x - h, y + h); + open = true; + break; + case "plus": + path.MoveTo(x - h, y); + path.LineTo(x + h, y); + path.MoveTo(x, y - h); + path.LineTo(x, y + h); + open = true; + break; + case "star": + path.MoveTo(x - h, y - h); + path.LineTo(x + h, y + h); + path.MoveTo(x + h, y - h); + path.LineTo(x - h, y + h); + path.MoveTo(x, y - h); + path.LineTo(x, y + h); + open = true; + break; + case "dash": + path.AddRect(new SKRect(x - h, y - h / 4, x + h, y + h / 4)); + break; + default: + path.AddRect(new SKRect(x - h, y - h, x + h, y + h)); + break; + } + if (!open) + FillPath(canvas, path, fill); + StrokePath(canvas, path, open ? line ?? (fill is { } f ? new Pen(f, 0.75f, null) : null) : line); + }); + } + + private void Areas(ChartPlot plot, Axis category, Axis value, SKRect inner) + { + if (value.Scale is not { } scale) + return; + var count = plot.Series.Count == 0 ? 0 : Math.Min(plot.Series.Max(s => s.Values.Count), category.Count); + if (count == 0) + return; + var stacked = plot.Grouping != ChartGrouping.Standard; + var percent = plot.Grouping == ChartGrouping.PercentStacked; + var totals = percent ? Totals(plot.Series, count) : null; + var below = new double[count]; + var baseline = stacked ? 0 : scale.Crossing(category.Definition); + SKPoint Point(int i, double v) + { + var along = Along(category, category.Fraction(i), inner); + var across = Along(value, scale.Fraction(v), inner); + return category.Horizontal ? new SKPoint(along, across) : new SKPoint(across, along); + } + foreach (var series in plot.Series) + { + var top = new SKPoint[count]; + var bottom = new SKPoint[count]; + for (var i = 0; i < count; i++) + { + var v = i < series.Values.Count ? series.Values[i] ?? 0 : 0; + if (percent) + v = totals![i] > 0 ? v / totals[i] : 0; + var from = stacked ? below[i] : baseline; + var to = stacked ? below[i] + v : v; + bottom[i] = Point(i, from); + top[i] = Point(i, to); + if (stacked) + below[i] = to; + } + var auto = _chart.Palette.Color(series.Index); + var fill = FillOf(series.Style, auto); + var line = PenOf(series.Style.Line, null, 0.75f); + Clipped(inner, canvas => + { + using var path = new SKPath(); + path.MoveTo(top[0]); + for (var i = 1; i < top.Length; i++) + path.LineTo(top[i]); + for (var i = bottom.Length - 1; i >= 0; i--) + path.LineTo(bottom[i]); + path.Close(); + FillPath(canvas, path, fill); + StrokePath(canvas, path, line); + }); + if ((series.Labels ?? plot.Labels) is { Any: true } options) + { + for (var i = 0; i < count; i++) + { + if (i < series.Values.Count && series.Values[i] is { } v && DataLabel(plot, series, i, v, options) is { } text) + TextCentred(text, (top[i].X + bottom[i].X) / 2, (top[i].Y + bottom[i].Y) / 2); + } + } + } + } + + // ───────────────────────── Pies and doughnuts ───────────────────────── + + private void Round(ChartPlot plot, SKRect area) + { + var rings = plot.Kind == ChartKind.Doughnut ? plot.Series : plot.Series.Take(1).ToList(); + if (rings.Count == 0) + return; + var labelled = rings.Any(s => (s.Labels ?? plot.Labels) is { Any: true, Position: "outEnd" }); + var radius = Math.Min(area.Width, area.Height) / 2 * (labelled ? 0.75f : 0.95f); + var pulled = rings.SelectMany(s => s.Explosion.Values).DefaultIfEmpty(0).Max(); + radius /= 1 + pulled / 100f; + if (radius <= 1) + return; + var center = new SKPoint(area.MidX, area.MidY); + var hole = plot.Kind == ChartKind.Doughnut ? radius * plot.HoleSize / 100f : 0; + var ring = (radius - hole) / rings.Count; + for (var r = 0; r < rings.Count; r++) + { + var series = rings[r]; + var inner = hole + r * ring; + var outer = inner + ring; + var total = series.Values.Sum(v => Math.Abs(v ?? 0)); + if (!(total > 0)) + continue; + var angle = -90 + plot.FirstSliceAngle; + var slices = new List<(float Start, float Sweep, SKPoint Center, SKColor? Fill, Pen? Line)>(); + for (var i = 0; i < series.Values.Count; i++) + { + var v = Math.Abs(series.Values[i] ?? 0); + if (v <= 0) + continue; + var sweep = (float)(v / total * 360); + var style = series.Points.GetValueOrDefault(i) ?? series.Style; + var auto = plot.VaryColors ? _chart.Palette.Color(i) : _chart.Palette.Color(series.Index); + var explosion = series.Explosion.TryGetValue(i, out var own) ? own : series.Explosion.GetValueOrDefault(-1); + var middle = (angle + sweep / 2) * Math.PI / 180; + var shift = plot.Kind == ChartKind.Pie ? radius * explosion / 100f : 0; + var c = new SKPoint(center.X + (float)Math.Cos(middle) * shift, center.Y + (float)Math.Sin(middle) * shift); + slices.Add(((float)angle, sweep, c, FillOf(style, auto), PenOf(style.Line, SKColors.White, 0.75f))); + if ((series.Labels ?? plot.Labels) is { Any: true } options && DataLabel(plot, series, i, series.Values[i] ?? 0, options, v / total) is { } text) + { + var position = options.Position ?? "bestFit"; + var distance = position switch + { + "outEnd" => outer + 6 * _s + Math.Max(text.Width, text.Height) / 2, + "inEnd" => outer - Math.Max(text.Width, text.Height) / 2 - 3 * _s, + "ctr" when plot.Kind == ChartKind.Pie => outer / 2, + _ => plot.Kind == ChartKind.Pie ? outer * 0.62f : (inner + outer) / 2, + }; + TextCentred(text, c.X + (float)Math.Cos(middle) * distance, c.Y + (float)Math.Sin(middle) * distance); + } + angle += sweep; + } + var (innerRadius, outerRadius) = (inner, outer); + Shape(canvas => + { + foreach (var (start, sweep, c, fill, line) in slices) + { + using var path = new SKPath(); + var outerBox = new SKRect(c.X - outerRadius, c.Y - outerRadius, c.X + outerRadius, c.Y + outerRadius); + if (sweep >= 359.999f && innerRadius <= 0) + { + path.AddOval(outerBox); + } + else if (innerRadius <= 0) + { + path.MoveTo(c); + path.ArcTo(outerBox, start, sweep, false); + path.Close(); + } + else + { + var innerBox = new SKRect(c.X - innerRadius, c.Y - innerRadius, c.X + innerRadius, c.Y + innerRadius); + path.ArcTo(outerBox, start, Math.Min(sweep, 359.99f), true); + path.ArcTo(innerBox, start + Math.Min(sweep, 359.99f), -Math.Min(sweep, 359.99f), false); + path.Close(); + } + FillPath(canvas, path, fill); + StrokePath(canvas, path, line); + } + }); + } + } +} diff --git a/src/Filee.Engines/Charts/ChartModel.cs b/src/Filee.Engines/Charts/ChartModel.cs new file mode 100644 index 000000000..efba05742 --- /dev/null +++ b/src/Filee.Engines/Charts/ChartModel.cs @@ -0,0 +1,297 @@ +// A DrawingML chart as ChartDrawer draws it (ECMA-376 Part 1, 21.2): the plots of the plot area with their series +// (the values cached in the chart part), the axes they use, the title, the legend and the chart's and plot area's +// fills. ChartReader builds it from a c:chartSpace part; the sheet printer, the document renderer and the slide +// renderer draw it. + +using SkiaSharp; + +namespace Filee.Engines.Charts; + +/// The kinds of plot drawn. +internal enum ChartKind +{ + Bar, + Line, + Area, + Pie, + Doughnut, + Scatter, +} + +/// How a plot's series stand to one another (c:grouping). +internal enum ChartGrouping +{ + /// Side by side (bars) or each on its own (lines, areas): clustered or standard. + Standard, + Stacked, + PercentStacked, +} + +/// +/// A line: colour (null = the automatic one), width in points (null = the automatic one) and dashes (multiples of the +/// width, null = solid); = no line. +/// +internal sealed record ChartLine(SKColor? Color = null, float? Width = null, float[]? Dash = null, bool None = false) +{ + public static readonly ChartLine Hidden = new(None: true); +} + +/// How a bar, slice, area, marker or box is filled and outlined; null parts are the automatic ones. +internal sealed record ChartShapeStyle +{ + /// The fill; null = automatic (the series colour), = none. + public SKColor? Fill { get; init; } + + /// The outline; null = automatic. + public ChartLine? Line { get; init; } + + public static readonly ChartShapeStyle Auto = new(); +} + +/// How text looks: font family (null = the theme's), size in points, weight, slant and colour. +internal sealed record ChartTextStyle(string? Family, float Size, bool Bold, bool Italic, SKColor Color) +{ + public static readonly ChartTextStyle Default = new(null, 10, false, false, SKColors.Black); +} + +/// A title (chart or axis): its lines and how they look. +internal sealed record ChartTitle(IReadOnlyList Lines, ChartTextStyle Style, bool Overlay = false) +{ + /// Where the file places the title (fractions of the chart), or null for the automatic place. + public ChartLayout? Layout { get; init; } +} + +/// c:manualLayout: a box as fractions of the chart area; = the plot without its axis labels. +internal readonly record struct ChartLayout(double X, double Y, double? Width, double? Height, bool Inner); + +/// What data labels show and where (c:dLbls). +internal sealed record ChartLabels(bool Value, bool Category, bool Series, bool Percent, string? Position, string? Format, ChartTextStyle Style) +{ + public bool Any => Value || Category || Series || Percent; +} + +/// A marker of a line or scatter series: symbol (none, circle, square, ...), size in points, style. +internal sealed record ChartMarker(string Symbol, float Size, ChartShapeStyle Style); + +/// One series: its name, categories and values (null = blank), and how it looks. +internal sealed class ChartSeries +{ + public string? Name { get; init; } + + /// c:idx: the series' place in the automatic colours. + public int Index { get; init; } + + /// Category labels (c:cat), or null for 1, 2, 3, .... + public IReadOnlyList? Categories { get; init; } + + /// X values of a scatter series (c:xVal numbers), or null for 1, 2, 3, .... + public IReadOnlyList? XValues { get; init; } + + public IReadOnlyList Values { get; init; } = []; + + /// The number format of the cached values (c:numCache c:formatCode), or null for General. + public string? Format { get; init; } + + public ChartShapeStyle Style { get; init; } = ChartShapeStyle.Auto; + + /// Points styled on their own (c:dPt), by index. + public IReadOnlyDictionary Points { get; init; } = new Dictionary(); + + /// Slices pulled out of a pie, in percent of the radius (c:explosion), by index; -1 for the whole series. + public IReadOnlyDictionary Explosion { get; init; } = new Dictionary(); + + /// The series' marker, or null for the plot's automatic one. + public ChartMarker? Marker { get; init; } + + public ChartLabels? Labels { get; init; } + + /// A line or scatter series drawn as a smooth curve. + public bool Smooth { get; init; } +} + +/// One plot of the plot area (c:barChart, c:lineChart, ...): its series, drawn against two axes. +internal sealed class ChartPlot +{ + public ChartKind Kind { get; init; } + public ChartGrouping Grouping { get; init; } + + /// Bars run left to right (c:barDir bar) instead of upwards. + public bool Horizontal { get; init; } + + /// Each point in its own colour (c:varyColors), as pie slices are. + public bool VaryColors { get; init; } + + /// Space between bar groups, in percent of a bar's width. + public int GapWidth { get; init; } = 150; + + /// How far the bars of a group overlap, in percent of a bar's width (negative: a gap). + public int Overlap { get; init; } + + /// The hole of a doughnut, in percent of its radius. + public int HoleSize { get; init; } = 50; + + /// Where a pie's first slice starts, in degrees clockwise from the top. + public double FirstSliceAngle { get; init; } + + /// Lines (and markers) of a line or scatter plot: c:marker of c:lineChart, c:scatterStyle. + public bool Lines { get; init; } = true; + + public bool Markers { get; init; } + + public ChartLabels? Labels { get; init; } + + /// The axes the plot is drawn against (c:axId): category (or X) first, value (or Y) second. + public IReadOnlyList AxisIds { get; init; } = []; + + public List Series { get; } = []; +} + +/// Where an axis crosses the other (c:crosses, c:crossesAt). +internal enum ChartCrossing +{ + AutoZero, + Min, + Max, + At, +} + +/// An axis: c:catAx, c:dateAx (categories) or c:valAx (values). +internal sealed record ChartAxis +{ + public uint Id { get; init; } + + /// A value axis (c:valAx): numbers along it, scaled; else categories. + public bool IsValue { get; init; } + + /// c:axPos: b, l, r or t. + public string Position { get; init; } = "b"; + + public bool Deleted { get; init; } + + /// Runs from its maximum to its minimum (c:orientation maxMin). + public bool Reversed { get; init; } + + public double? Min { get; init; } + public double? Max { get; init; } + public double? MajorUnit { get; init; } + + /// A logarithmic axis' base (c:logBase), or null. + public double? LogBase { get; init; } + + public ChartTitle? Title { get; init; } + + /// The labels' number format, or null for the values' own (c:numFmt sourceLinked). + public string? Format { get; init; } + + public ChartLine? MajorGridlines { get; init; } + public ChartLine? MinorGridlines { get; init; } + + /// The axis line; null = automatic. + public ChartLine? Line { get; init; } + + /// c:tickLblPos: nextTo, low, high or none. + public string LabelPosition { get; init; } = "nextTo"; + + /// c:majorTickMark: out, in, cross or none. + public string MajorTicks { get; init; } = "out"; + + public ChartCrossing Crosses { get; init; } + public double CrossesAt { get; init; } + + /// The other axis of the pair (c:crossAx). + public uint CrossAxis { get; init; } + + /// Points sit between the category ticks (c:crossBetween between) rather than on them (midCat). + public bool Between { get; init; } = true; + + /// Category labels skipped: every n-th is drawn (c:tickLblSkip), or 0 for automatic. + public int LabelSkip { get; init; } + + /// Turn of the labels in degrees (c:txPr a:bodyPr rot), or null for automatic. + public double? LabelRotation { get; init; } + + public ChartTextStyle Style { get; init; } = ChartTextStyle.Default; +} + +/// The legend: where it is, which entries are left out, how its text looks. +internal sealed class ChartLegend +{ + /// c:legendPos: r, l, t, b or tr. + public string Position { get; init; } = "r"; + + public bool Overlay { get; init; } + + /// Entries left out (c:legendEntry c:delete), by index. + public IReadOnlySet Deleted { get; init; } = new HashSet(); + + public ChartLayout? Layout { get; init; } + + public ChartTextStyle Style { get; init; } = ChartTextStyle.Default; +} + +/// +/// Office's automatic chart colours: the theme's six accents, then the same six darker and lighter in turn (Office's +/// colour variations: 60 %, 80 % + 20 %, 80 %, 60 % + 40 %, 50 %, 70 % + 30 %, 70 %, 50 % + 50 % luminance). +/// +internal sealed class ChartPalette(IReadOnlyList accents) +{ + private static readonly (double Mod, double Off)[] Variations = + [(1, 0), (0.6, 0), (0.8, 0.2), (0.8, 0), (0.6, 0.4), (0.5, 0), (0.7, 0.3), (0.7, 0), (0.5, 0.5)]; + + public static readonly ChartPalette Default = new( + [ + new SKColor(0x44, 0x72, 0xC4), new SKColor(0xED, 0x7D, 0x31), new SKColor(0xA5, 0xA5, 0xA5), + new SKColor(0xFF, 0xC0, 0x00), new SKColor(0x5B, 0x9B, 0xD5), new SKColor(0x70, 0xAD, 0x47), + ]); + + public SKColor Color(int index) + { + index = Math.Max(0, index); + var accent = accents.Count == 0 ? SKColors.Gray : accents[index % accents.Count]; + var (mod, off) = Variations[index / Math.Max(1, accents.Count) % Variations.Length]; + if (mod == 1 && off == 0) + return accent; + accent.ToHsl(out var h, out var s, out var l); + return SKColor.FromHsl(h, s, (float)Math.Clamp(l * mod + off * 100, 0, 100)); + } +} + +/// A chart part, read. +internal sealed class Chart +{ + public ChartTitle? Title { get; init; } + public List Plots { get; } = []; + public List Axes { get; } = []; + public ChartLegend? Legend { get; init; } + + /// The chart area's fill and outline (c:chartSpace c:spPr). + public ChartShapeStyle Area { get; init; } = ChartShapeStyle.Auto; + + /// The plot area's fill and outline (c:plotArea c:spPr). + public ChartShapeStyle PlotArea { get; init; } = ChartShapeStyle.Auto; + + public ChartLayout? PlotLayout { get; init; } + + /// The chart area has rounded corners (c:roundedCorners). + public bool RoundedCorners { get; init; } + + /// How blank values are drawn (c:dispBlanksAs): gap, zero or span (the line goes on over them). + public string BlanksAs { get; init; } = "gap"; + + /// Dates in the cached values count from 1904 (c:date1904). + public bool Date1904 { get; init; } + + /// The chart's text when nothing else says (c:chartSpace c:txPr). + public ChartTextStyle Text { get; init; } = ChartTextStyle.Default; + + /// The automatic colours of series and points. + public ChartPalette Palette { get; init; } = ChartPalette.Default; + + /// The theme's body font (+mn-lt), for text without a family. + public string? ThemeFont { get; init; } + + /// Points of all series together (what drawing the chart costs). + public long PointCount => Plots.Sum(p => p.Series.Sum(s => (long)s.Values.Count)); + + public ChartAxis? Axis(uint id) => Axes.FirstOrDefault(a => a.Id == id); +} diff --git a/src/Filee.Engines/Charts/ChartPicture.cs b/src/Filee.Engines/Charts/ChartPicture.cs new file mode 100644 index 000000000..8ffe5a062 --- /dev/null +++ b/src/Filee.Engines/Charts/ChartPicture.cs @@ -0,0 +1,27 @@ +// A chart drawn into a picture, for what takes pictures rather than drawings: the document writers (a Word chart +// becomes a picture in HWPX, ODT, EPUB, ...) and the ODP writer. + +using Filee.Engines.Rendering; +using SkiaSharp; + +namespace Filee.Engines.Charts; + +internal static class ChartPicture +{ + /// Pixels of the longer side at most. + private const int MaxSide = 4000; + + /// The chart drawn × points at , as PNG; null for an empty box. + public static byte[]? Png(Chart chart, float width, float height, float dpi = 192) + { + if (!(width >= 1 && height >= 1) || !float.IsFinite(width) || !float.IsFinite(height)) + return null; + dpi = Math.Min(dpi, MaxSide * 72f / Math.Max(width, height)); + var page = new PageContent(width, height); + using (var fonts = new ChartFonts()) + ChartDrawer.Draw(page, chart, new SKRect(0, 0, width, height), fonts); + using var bitmap = page.Render(dpi); + using var data = bitmap.Encode(SKEncodedImageFormat.Png, 100); + return data?.ToArray(); + } +} diff --git a/src/Filee.Engines/Charts/ChartReader.cs b/src/Filee.Engines/Charts/ChartReader.cs new file mode 100644 index 000000000..770d6c1f1 --- /dev/null +++ b/src/Filee.Engines/Charts/ChartReader.cs @@ -0,0 +1,560 @@ +// A DrawingML chart part (c:chartSpace, ECMA-376 Part 1, 21.2.2) → the chart model ChartDrawer draws. Everything comes +// from what the part caches (c:strCache, c:numCache, c:multiLvlStrCache, literals), so no formula is evaluated and no +// workbook is opened: what Excel, Word and PowerPoint saved is what is drawn. +// +// Read: bar and column (clustered, stacked, 100 %), line (with markers, stacked), area, pie, doughnut and scatter plots, +// several in one plot area (a column and line combination on two value axes); the title (rich text or a cell's text, +// or the series name Office shows for a lone series), axis titles, category, date and value axes with their scaling, +// number format, gridlines, tick marks, label position and crossing; the legend with deleted entries; data labels; +// fills and outlines of the chart area, plot area, series and single points (c:spPr: solid, the first colour of a +// gradient or pattern, none) and the theme's accent colours for the rest; manual layouts of the plot area and legend. +// 3D charts, radar, bubble, stock, surface and bar-of-pie charts are named instead (LeftOut), and their documents say +// "charts" were left out, or take LibreOffice where the capability checks send them. +// +// Hostile input: the part comes through the package's XML limits; series per chart and points per chart (a cache's +// ptCount or highest c:idx, not only the points it holds) are bounded and a part beyond is refused with +// error.document_too_complex; texts are cut at MaxText characters and titles at MaxLines lines; numbers that aren't +// finite are blanks and the rest are clamped to ±MaxValue, so scaling can't overflow. + +using System.Globalization; +using System.Xml.Linq; +using Filee.Engines.Documents; +using Filee.Engines.Office.Sheets; +using Filee.Engines.Slides; +using SkiaSharp; + +namespace Filee.Engines.Charts; + +/// A chart part read: the chart when it can be drawn, else what is left out ("charts"). +internal readonly record struct ChartRead(Chart? Chart, string? LeftOut); + +internal sealed class ChartReader +{ + public static readonly XNamespace C = "http://schemas.openxmlformats.org/drawingml/2006/chart"; + private static readonly XNamespace A = DrawingColors.A; + + /// Series in one chart at most (Excel allows 255). + internal const int MaxSeries = 1024; + + /// Points of all series of one chart together at most (Excel's rows: a column of a whole sheet). + internal const int MaxPoints = 1_048_576; + + /// Characters of one text (a title, a label, a series name) kept; the rest is cut. + internal const int MaxText = 1024; + + /// Lines of one title kept. + internal const int MaxLines = 16; + + /// The largest value magnitude kept; larger ones are clamped (Excel's largest number is about 1e308). + internal const double MaxValue = 1e100; + + /// The plots ChartDrawer draws, by element name. + private static readonly Dictionary Kinds = new(StringComparer.Ordinal) + { + ["barChart"] = ChartKind.Bar, + ["lineChart"] = ChartKind.Line, + ["areaChart"] = ChartKind.Area, + ["pieChart"] = ChartKind.Pie, + ["doughnutChart"] = ChartKind.Doughnut, + ["scatterChart"] = ChartKind.Scatter, + }; + + /// Plots that are named instead of drawn. + private static readonly HashSet Unsupported = new(StringComparer.Ordinal) + { + "bar3DChart", "line3DChart", "area3DChart", "pie3DChart", "radarChart", "bubbleChart", "stockChart", "surfaceChart", + "surface3DChart", "ofPieChart", + }; + + private readonly DrawingColors _colors; + private readonly string? _themeFont; + private readonly NumberFormatter _formatter; + private int _points; + + private ChartReader(DrawingColors colors, string? themeFont, bool date1904) + { + _colors = colors; + _themeFont = themeFont; + _formatter = new NumberFormatter(date1904); + } + + /// The theme's body font (a:minorFont a:latin) of a theme part, or null. + public static string? ThemeFont(XElement? theme) => + (string?)theme?.Descendants(A + "minorFont").FirstOrDefault()?.Element(A + "latin")?.Attribute("typeface") is { Length: > 0 } font ? font : null; + + /// + /// Reads chart part (its c:chartSpace) with the host's theme colours and body font. + /// + /// The chart has more series or points than Filee draws. + public static ChartRead Read(XElement? root, DrawingColors colors, string? themeFont = null) + { + if (root is null || root.Name != C + "chartSpace" || root.Element(C + "chart") is not { } chart) + return new(null, "charts"); + var date1904 = Bool(root.Element(C + "date1904"), false); + if (root.Element(C + "clrMapOvr") is { } map) + colors = colors.WithMap(map); + return new ChartReader(colors, themeFont, date1904).ReadChart(root, chart, date1904); + } + + private ChartRead ReadChart(XElement root, XElement chart, bool date1904) + { + var plotArea = chart.Element(C + "plotArea"); + if (plotArea is null) + return new(null, "charts"); + var text = Style(new ChartTextStyle(_themeFont, 10, false, false, SKColors.Black), DefaultRun(root.Element(C + "txPr"))); + var plots = new List(); + foreach (var element in plotArea.Elements()) + { + var name = element.Name.LocalName; + if (element.Name.Namespace != C) + continue; + if (Unsupported.Contains(name)) + return new(null, "charts"); + if (Kinds.TryGetValue(name, out var kind)) + plots.Add(Plot(element, kind, text)); + } + if (plots.Count == 0 || plots.All(p => p.Series.Count == 0)) + return new(null, "charts"); + + var series = plots.SelectMany(p => p.Series).ToList(); + var result = new Chart + { + Title = ChartTitle(chart, text, series), + Legend = Legend(chart.Element(C + "legend"), text), + Area = Shape(root.Element(C + "spPr")), + PlotArea = Shape(plotArea.Element(C + "spPr")), + PlotLayout = Layout(plotArea.Element(C + "layout")), + RoundedCorners = Bool(root.Element(C + "roundedCorners"), true), + BlanksAs = Val(chart.Element(C + "dispBlanksAs")) is ("zero" or "span") and var blanks ? blanks : "gap", + Date1904 = date1904, + Text = text, + Palette = new ChartPalette([.. Enumerable.Range(1, 6).Select(i => _colors.Scheme($"accent{i}"))]), + ThemeFont = _themeFont, + }; + result.Plots.AddRange(plots); + foreach (var axis in plotArea.Elements()) + { + if (axis.Name.Namespace == C && axis.Name.LocalName is "catAx" or "valAx" or "dateAx") + result.Axes.Add(Axis(axis, text)); + } + return new(result, null); + } + + // ───────────────────────── Plots and series ───────────────────────── + + private ChartPlot Plot(XElement element, ChartKind kind, ChartTextStyle text) + { + var grouping = Val(element.Element(C + "grouping")) switch + { + "stacked" => ChartGrouping.Stacked, + "percentStacked" => ChartGrouping.PercentStacked, + _ => ChartGrouping.Standard, + }; + var scatterStyle = Val(element.Element(C + "scatterStyle")) ?? "marker"; + var plot = new ChartPlot + { + Kind = kind, + Grouping = kind is ChartKind.Pie or ChartKind.Doughnut or ChartKind.Scatter ? ChartGrouping.Standard : grouping, + Horizontal = Val(element.Element(C + "barDir")) == "bar", + // Pies and doughnuts colour their slices unless the file says not to. + VaryColors = Bool(element.Element(C + "varyColors"), kind is ChartKind.Pie or ChartKind.Doughnut ? element.Element(C + "varyColors") is null : false), + GapWidth = (int)Math.Clamp(Number(element.Element(C + "gapWidth")) ?? 150, 0, 500), + Overlap = (int)Math.Clamp(Number(element.Element(C + "overlap")) ?? (grouping == ChartGrouping.Standard ? 0 : 100), -100, 100), + HoleSize = (int)Math.Clamp(Number(element.Element(C + "holeSize")) ?? 50, 1, 90), + FirstSliceAngle = Math.Clamp(Number(element.Element(C + "firstSliceAng")) ?? 0, 0, 360), + Lines = kind != ChartKind.Scatter || scatterStyle is "lineMarker" or "line" or "smooth" or "smoothMarker", + Markers = kind == ChartKind.Line ? Bool(element.Element(C + "marker"), true) + : kind == ChartKind.Scatter && scatterStyle is "lineMarker" or "marker" or "smoothMarker", + Labels = Labels(element.Element(C + "dLbls"), text), + AxisIds = [.. element.Elements(C + "axId").Select(a => (uint)Math.Clamp(Number(a) ?? 0, 0, uint.MaxValue))], + }; + foreach (var ser in element.Elements(C + "ser")) + { + if (plot.Series.Count >= MaxSeries) + throw DocumentLimits.Exceeded($"A chart has more than {DocumentLimits.Count(MaxSeries)} series."); + plot.Series.Add(Series(ser, kind, text, plot.Series.Count)); + } + // c:order sets the drawing order. + var order = element.Elements(C + "ser").Select((s, i) => (Order: Number(s.Element(C + "order")) ?? i, Index: i)).ToList(); + if (order.Select(o => o.Order).Distinct().Count() == order.Count) + { + var sorted = order.OrderBy(o => o.Order).Select(o => plot.Series[o.Index]).ToList(); + plot.Series.Clear(); + plot.Series.AddRange(sorted); + } + return plot; + } + + private ChartSeries Series(XElement ser, ChartKind kind, ChartTextStyle text, int position) + { + var scatter = kind == ChartKind.Scatter; + var values = Numbers(ser.Element(C + (scatter ? "yVal" : "val")), out var format); + IReadOnlyList? categories = null; + IReadOnlyList? xValues = null; + if (ser.Element(C + (scatter ? "xVal" : "cat")) is { } cat) + { + if (scatter && (cat.Element(C + "numRef") is not null || cat.Element(C + "numLit") is not null)) + xValues = Numbers(cat, out _); + else + categories = Labels(cat); + } + var points = new Dictionary(); + var explosion = new Dictionary(); + if (Number(ser.Element(C + "explosion")) is { } whole) + explosion[-1] = (int)Math.Clamp(whole, 0, 400); + foreach (var point in ser.Elements(C + "dPt")) + { + if (Number(point.Element(C + "idx")) is not { } index || index < 0 || index >= MaxPoints) + continue; + points[(int)index] = Shape(point.Element(C + "spPr")); + if (Number(point.Element(C + "explosion")) is { } pulled) + explosion[(int)index] = (int)Math.Clamp(pulled, 0, 400); + } + return new ChartSeries + { + Name = SeriesName(ser.Element(C + "tx")), + Index = (int)Math.Clamp(Number(ser.Element(C + "idx")) ?? position, 0, MaxSeries * 64), + Categories = categories, + XValues = xValues, + Values = values, + Format = format, + Style = Shape(ser.Element(C + "spPr")), + Points = points, + Explosion = explosion, + Marker = ser.Element(C + "marker") is { } marker ? Marker(marker) : null, + Labels = Labels(ser.Element(C + "dLbls"), text), + Smooth = Bool(ser.Element(C + "smooth"), false), + }; + } + + private ChartMarker Marker(XElement marker) => new( + Val(marker.Element(C + "symbol")) ?? "auto", + (float)Math.Clamp(Number(marker.Element(C + "size")) ?? 5, 2, 72), + Shape(marker.Element(C + "spPr"))); + + private string? SeriesName(XElement? tx) + { + if (tx is null) + return null; + if (tx.Element(C + "v") is { } literal) + return Cut(literal.Value); + var cache = tx.Element(C + "strRef")?.Element(C + "strCache"); + return cache?.Elements(C + "pt").FirstOrDefault()?.Element(C + "v")?.Value is { } name ? Cut(name) : null; + } + + /// Category labels of c:cat: text, numbers in their format, or the innermost level of several. + private IReadOnlyList? Labels(XElement cat) + { + if ((cat.Element(C + "strRef")?.Element(C + "strCache") ?? cat.Element(C + "strLit")) is { } strings) + return Points(strings, v => Cut(v)); + if ((cat.Element(C + "numRef")?.Element(C + "numCache") ?? cat.Element(C + "numLit")) is { } numbers) + { + var code = (string?)numbers.Element(C + "formatCode"); + return Points(numbers, v => Finite(v) is { } value ? Cut(_formatter.Format(value, code)) : Cut(v)); + } + if (cat.Element(C + "multiLvlStrRef")?.Element(C + "multiLvlStrCache") is { } levels && levels.Element(C + "lvl") is { } inner) + { + return Points(inner, v => Cut(v), Count(levels)); + } + return null; + } + + /// The numbers of c:val, c:yVal or c:xVal (blanks null), with their format code. + private IReadOnlyList Numbers(XElement? element, out string? format) + { + format = null; + var cache = element?.Element(C + "numRef")?.Element(C + "numCache") ?? element?.Element(C + "numLit"); + if (cache is null) + return []; + format = (string?)cache.Element(C + "formatCode") is { Length: > 0 } code && !NumberFormatter.IsGeneral(code) ? code : null; + return Points(cache, Finite); + } + + /// A cache's points by c:idx, as many as its c:ptCount or highest index says, counted against . + private List Points(XElement cache, Func value, int? count = null) + { + var points = new List<(int Index, string Value)>(); + var highest = -1; + foreach (var pt in cache.Elements(C + "pt")) + { + if (Number(pt.Attribute("idx")) is not { } index || index < 0) + continue; + if (index >= MaxPoints) + throw TooMany(); + points.Add(((int)index, pt.Element(C + "v")?.Value ?? "")); + highest = Math.Max(highest, (int)index); + } + var length = (long)Math.Max(highest + 1, Math.Min(count ?? Count(cache), MaxPoints + 1L)); + if ((_points += (int)Math.Min(length, MaxPoints + 1L)) > MaxPoints) + throw TooMany(); + var result = new List((int)length); + for (var i = 0; i < length; i++) + result.Add(default); + foreach (var (index, text) in points) + result[index] = value(text); + return result; + } + + private static int Count(XElement cache) => (int)Math.Clamp(Number(cache.Element(C + "ptCount")) ?? 0, 0, MaxPoints + 1L); + + private static Core.Conversion.ConversionRefusedException TooMany() => + DocumentLimits.Exceeded($"A chart has more than {DocumentLimits.Count(MaxPoints)} points."); + + private static double? Finite(string text) => + double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) && double.IsFinite(value) + ? Math.Clamp(value, -MaxValue, MaxValue) + : null; + + // ───────────────────────── Axes ───────────────────────── + + private ChartAxis Axis(XElement axis, ChartTextStyle text) + { + var scaling = axis.Element(C + "scaling"); + var numberFormat = axis.Element(C + "numFmt"); + var txPr = axis.Element(C + "txPr"); + var rotation = Number(txPr?.Element(A + "bodyPr")?.Attribute("rot")) is { } rot && rot is > -5_400_001 and < 5_400_001 ? rot / 60000 : (double?)null; + var crosses = Val(axis.Element(C + "crosses")); + var value = axis.Name.LocalName == "valAx"; + return new ChartAxis + { + Id = (uint)Math.Clamp(Number(axis.Element(C + "axId")) ?? 0, 0, uint.MaxValue), + IsValue = value, + Position = Val(axis.Element(C + "axPos")) is ("l" or "r" or "t" or "b") and var position ? position : "b", + Deleted = Bool(axis.Element(C + "delete"), false), + Reversed = Val(scaling?.Element(C + "orientation")) == "maxMin", + Min = Number(scaling?.Element(C + "min")) is { } min ? Math.Clamp(min, -MaxValue, MaxValue) : null, + Max = Number(scaling?.Element(C + "max")) is { } max ? Math.Clamp(max, -MaxValue, MaxValue) : null, + MajorUnit = Number(axis.Element(C + "majorUnit")) is > 0 and var unit ? Math.Min(unit, MaxValue) : null, + LogBase = Number(scaling?.Element(C + "logBase")) is >= 2 and <= 1000 and var log ? log : null, + Title = axis.Element(C + "title") is { } title ? Title(title, text with { Bold = true }, null) : null, + Format = numberFormat is not null && !Bool(numberFormat.Attribute("sourceLinked"), false) ? (string?)numberFormat.Attribute("formatCode") : null, + MajorGridlines = axis.Element(C + "majorGridlines") is { } major ? Line(major.Element(C + "spPr")?.Element(A + "ln")) ?? new ChartLine() : null, + MinorGridlines = axis.Element(C + "minorGridlines") is { } minor ? Line(minor.Element(C + "spPr")?.Element(A + "ln")) ?? new ChartLine() : null, + Line = Line(axis.Element(C + "spPr")?.Element(A + "ln")), + LabelPosition = Val(axis.Element(C + "tickLblPos")) ?? "nextTo", + MajorTicks = Val(axis.Element(C + "majorTickMark")) ?? "out", + Crosses = axis.Element(C + "crossesAt") is not null ? ChartCrossing.At : crosses switch { "min" => ChartCrossing.Min, "max" => ChartCrossing.Max, _ => ChartCrossing.AutoZero }, + CrossesAt = Math.Clamp(Number(axis.Element(C + "crossesAt")) ?? 0, -MaxValue, MaxValue), + CrossAxis = (uint)Math.Clamp(Number(axis.Element(C + "crossAx")) ?? 0, 0, uint.MaxValue), + Between = Val(axis.Element(C + "crossBetween")) != "midCat", + LabelSkip = (int)Math.Clamp(Number(axis.Element(C + "tickLblSkip")) ?? 0, 0, MaxPoints), + LabelRotation = rotation, + Style = Style(text, DefaultRun(txPr)), + }; + } + + // ───────────────────────── Title, legend, labels ───────────────────────── + + private ChartTitle? ChartTitle(XElement chart, ChartTextStyle text, List series) + { + var deleted = Bool(chart.Element(C + "autoTitleDeleted"), false); + var style = text with { Size = text.Size * 1.8f, Bold = true }; + var lone = series.Count == 1 && series[0].Name is { Length: > 0 } name ? name : null; + if (chart.Element(C + "title") is { } title) + return Title(title, style, lone ?? "Chart Title"); + // Office titles a chart of one named series with its name until the title is deleted. + return !deleted && lone is not null ? new ChartTitle([lone], style) : null; + } + + /// A c:title: its rich text or a cell's text, else (null: no title). + private ChartTitle? Title(XElement title, ChartTextStyle style, string? automatic) + { + style = Style(style, DefaultRun(title.Element(C + "txPr"))); + var overlay = Bool(title.Element(C + "overlay"), false); + var layout = Layout(title.Element(C + "layout")); + var tx = title.Element(C + "tx"); + if (tx?.Element(C + "rich") is { } rich) + { + var lines = new List(); + var first = true; + foreach (var paragraph in rich.Elements(A + "p").Take(MaxLines)) + { + var defaults = paragraph.Element(A + "pPr")?.Element(A + "defRPr"); + if (first) + style = Style(style, defaults); + var line = new System.Text.StringBuilder(); + foreach (var run in paragraph.Elements()) + { + if (run.Name == A + "br") + { + lines.Add(Cut(line.ToString())); + line.Clear(); + continue; + } + if (run.Name != A + "r" && run.Name != A + "fld") + continue; + if (first) + { + style = Style(style, run.Element(A + "rPr")); + first = false; + } + if (line.Length < MaxText) + line.Append(run.Element(A + "t")?.Value); + } + lines.Add(Cut(line.ToString())); + if (lines.Count >= MaxLines) + break; + } + return new ChartTitle([.. lines.Take(MaxLines)], style, overlay) { Layout = layout }; + } + if (tx?.Element(C + "strRef")?.Element(C + "strCache")?.Elements(C + "pt").FirstOrDefault()?.Element(C + "v")?.Value is { } cell) + return new ChartTitle([.. Cut(cell).Split('\n').Take(MaxLines)], style, overlay) { Layout = layout }; + return automatic is null ? null : new ChartTitle([automatic], style, overlay) { Layout = layout }; + } + + private ChartLegend? Legend(XElement? legend, ChartTextStyle text) + { + if (legend is null) + return null; + var deleted = new HashSet(); + foreach (var entry in legend.Elements(C + "legendEntry")) + { + if (Bool(entry.Element(C + "delete"), false) && Number(entry.Element(C + "idx")) is { } index && index is >= 0 and < MaxPoints) + deleted.Add((int)index); + } + return new ChartLegend + { + Position = Val(legend.Element(C + "legendPos")) is ("r" or "l" or "t" or "b" or "tr") and var position ? position : "r", + Overlay = Bool(legend.Element(C + "overlay"), false), + Deleted = deleted, + Layout = Layout(legend.Element(C + "layout")), + Style = Style(text, DefaultRun(legend.Element(C + "txPr"))), + }; + } + + private ChartLabels? Labels(XElement? labels, ChartTextStyle text) + { + if (labels is null) + return null; + if (Bool(labels.Element(C + "delete"), false)) + return new ChartLabels(false, false, false, false, null, null, text); + var format = labels.Element(C + "numFmt") is { } numFmt && !Bool(numFmt.Attribute("sourceLinked"), false) ? (string?)numFmt.Attribute("formatCode") : null; + return new ChartLabels( + Bool(labels.Element(C + "showVal"), false), + Bool(labels.Element(C + "showCatName"), false), + Bool(labels.Element(C + "showSerName"), false), + Bool(labels.Element(C + "showPercent"), false), + Val(labels.Element(C + "dLblPos")), + format, + Style(text, DefaultRun(labels.Element(C + "txPr")))); + } + + private static ChartLayout? Layout(XElement? layout) + { + if (layout?.Element(C + "manualLayout") is not { } manual) + return null; + // Only boxes placed from the chart's edges are taken; factors of the automatic place are left to it. + if (Val(manual.Element(C + "xMode")) is "factor" || Val(manual.Element(C + "yMode")) is "factor" + || Number(manual.Element(C + "x")) is not { } x || Number(manual.Element(C + "y")) is not { } y) + return null; + static double? Fraction(double? value) => value is { } v ? Math.Clamp(v, 0, 1) : null; + return new ChartLayout(Math.Clamp(x, 0, 1), Math.Clamp(y, 0, 1), Fraction(Number(manual.Element(C + "w"))), Fraction(Number(manual.Element(C + "h"))), + Val(manual.Element(C + "layoutTarget")) == "inner"); + } + + // ───────────────────────── Fills, lines, text ───────────────────────── + + private ChartShapeStyle Shape(XElement? properties) => + properties is null ? ChartShapeStyle.Auto : new ChartShapeStyle { Fill = Fill(properties), Line = Line(properties.Element(A + "ln")) }; + + private SKColor? Fill(XElement properties) + { + var fill = properties.Elements().FirstOrDefault(e => e.Name.Namespace == A && e.Name.LocalName is "noFill" or "solidFill" or "gradFill" or "pattFill"); + return fill?.Name.LocalName switch + { + "noFill" => SKColors.Transparent, + "solidFill" => _colors.Color(fill), + "gradFill" => _colors.Color(fill.Element(A + "gsLst")?.Elements(A + "gs").FirstOrDefault()), + "pattFill" => _colors.Color(fill.Element(A + "fgClr")), + _ => null, + }; + } + + private ChartLine? Line(XElement? line) + { + if (line is null) + return null; + var fill = line.Elements().FirstOrDefault(e => e.Name.Namespace == A && e.Name.LocalName is "noFill" or "solidFill" or "gradFill" or "pattFill"); + if (fill?.Name.LocalName == "noFill") + return ChartLine.Hidden; + var color = fill?.Name.LocalName switch + { + "solidFill" => _colors.Color(fill), + "gradFill" => _colors.Color(fill.Element(A + "gsLst")?.Elements(A + "gs").FirstOrDefault()), + "pattFill" => _colors.Color(fill.Element(A + "fgClr")), + _ => null, + }; + var width = Number(line.Attribute("w")) is { } w ? (float)Math.Clamp(w / 12700, 0, 100) : (float?)null; + var dash = Val(line.Element(A + "prstDash")) switch + { + "dot" or "sysDot" => [1f, 1.5f], + "dash" or "sysDash" => [4f, 3f], + "lgDash" => [8f, 3f], + "dashDot" or "sysDashDot" => [4f, 3f, 1f, 3f], + "lgDashDot" => [8f, 3f, 1f, 3f], + "lgDashDotDot" or "sysDashDotDot" => [8f, 3f, 1f, 3f, 1f, 3f], + _ => (float[]?)null, + }; + return new ChartLine(color, width, dash); + } + + /// The default run properties of a c:txPr (its first paragraph's a:defRPr). + private static XElement? DefaultRun(XElement? txPr) => txPr?.Element(A + "p")?.Element(A + "pPr")?.Element(A + "defRPr"); + + /// with what run properties (a:rPr, a:defRPr) set. + private ChartTextStyle Style(ChartTextStyle style, XElement? run) + { + if (run is null) + return style; + if (Number(run.Attribute("sz")) is { } size && size >= 100) + style = style with { Size = (float)Math.Min(size / 100, 400) }; + if (run.Attribute("b") is { } bold) + style = style with { Bold = Bool(bold, false) }; + if (run.Attribute("i") is { } italic) + style = style with { Italic = Bool(italic, false) }; + if (run.Element(A + "solidFill") is { } fill && _colors.Color(fill) is { } color) + style = style with { Color = color }; + if ((string?)(run.Element(A + "latin") ?? run.Element(A + "ea"))?.Attribute("typeface") is { Length: > 0 } family) + style = style with { Family = family.StartsWith('+') ? _themeFont : family }; + return style; + } + + // ───────────────────────── Values ───────────────────────── + + private static string? Val(XElement? element) => (string?)element?.Attribute("val"); + + private static double? Number(XElement? element) => Number(element?.Attribute("val")); + + private static double? Number(XAttribute? attribute) => + double.TryParse((string?)attribute, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) && double.IsFinite(value) ? value : null; + + /// A CT_Boolean: an element without val is true; when there is no element. + private static bool Bool(XElement? element, bool absent) => element is null ? absent : Bool(element.Attribute("val"), true); + + private static bool Bool(XAttribute? attribute, bool absent) => (string?)attribute switch + { + null => absent, + "1" or "true" or "on" => true, + _ => false, + }; + + private static string Cut(string text) => text.Length > MaxText ? text[..MaxText] : text; +} + +/// +/// The points one document's charts draw together, each chart counted every time it is placed (a part placed many +/// times draws many times), so a small file can't make drawing take hours; beyond, the document is refused. +/// +internal sealed class ChartBudget +{ + /// Points drawn per document at most: ten full-sheet charts. + internal const long MaxPoints = 10L * ChartReader.MaxPoints; + + private long _points; + + public void Charge(Chart chart) + { + if ((_points += Math.Max(1, chart.PointCount)) > MaxPoints) + throw DocumentLimits.Exceeded($"The document's charts draw more than {DocumentLimits.Count(MaxPoints)} points."); + } +} diff --git a/src/Filee.Engines/Charts/ChartText.cs b/src/Filee.Engines/Charts/ChartText.cs new file mode 100644 index 000000000..d380746c4 --- /dev/null +++ b/src/Filee.Engines/Charts/ChartText.cs @@ -0,0 +1,106 @@ +// Text of a chart (titles, axis and data labels, legend entries) on the layout engine's services: the font a style +// names (or the theme's body font, else Calibri and what stands in for it), the fonts its characters fall back to, +// shaped with HarfBuzz and drawn as glyph runs, so a PDF keeps the chart's text as text. + +using Filee.Engines.Rendering; +using Filee.Engines.Typesetting; +using SkiaSharp; + +namespace Filee.Engines.Charts; + +/// Fonts and shaping for chart text; dispose to release a shaper it made itself. +internal sealed class ChartFonts : IDisposable +{ + private readonly FontResolver _resolver; + private readonly Shaper _shaper; + private readonly bool _ownsShaper; + private readonly Dictionary<(string? Family, bool Bold, bool Italic), ResolvedFont?> _fonts = []; + private readonly Dictionary _metrics = new(ReferenceEqualityComparer.Instance); + + /// The renderer's font resolver, or null for the installed fonts. + /// The renderer's shaper (not disposed here), or null for one of its own. + public ChartFonts(FontResolver? resolver = null, Shaper? shaper = null) + { + _resolver = resolver ?? new FontResolver(); + _ownsShaper = shaper is null; + _shaper = shaper ?? new Shaper(); + } + + public void Dispose() + { + if (_ownsShaper) + _shaper.Dispose(); + } + + /// One line of in at points. + public ChartTextLine Line(string text, ChartTextStyle style, float size) + { + var line = new ChartTextLine(text, style.Color, size); + if (Font(style) is not { } primary || size <= 0 || !float.IsFinite(size)) + return line; + var metrics = Metrics(primary.Font); + var em = (float)Math.Max(1, metrics.UnitsPerEm); + line.Ascent = metrics.Ascender / em * size; + line.Descent = -metrics.Descender / em * size; + if (line.Ascent <= 0) + (line.Ascent, line.Descent) = (size * 0.8f, size * 0.2f); + if (text.Length == 0) + return line; + foreach (var run in _resolver.Fallback(text, 0, text.Length, primary, FontStyle.From(style.Bold, style.Italic))) + { + var shaped = _shaper.Shape(run.Font.Font, text, run.Start, run.Length); + line.Runs.Add((run.Font, shaped, run.Start, run.Length)); + line.Width += shaped.Width(size); + } + return line; + } + + private ResolvedFont? Font(ChartTextStyle style) + { + var key = (style.Family, style.Bold, style.Italic); + if (!_fonts.TryGetValue(key, out var font)) + { + var fontStyle = FontStyle.From(style.Bold, style.Italic); + _fonts[key] = font = _resolver.Resolve(style.Family ?? "Calibri", fontStyle) ?? _resolver.Resolve(null, fontStyle); + } + return font; + } + + private FontMetrics Metrics(PageFont font) + { + if (!_metrics.TryGetValue(font, out var metrics)) + _metrics[font] = metrics = _shaper.Metrics(font); + return metrics; + } +} + +/// A line of chart text, shaped: its width, ascent and descent in points, drawn at a pen position. +internal sealed class ChartTextLine(string text, SKColor color, float size) +{ + public string Text { get; } = text; + public float Width { get; set; } + public float Ascent { get; set; } + public float Descent { get; set; } + public float Height => Ascent + Descent; + + internal List<(ResolvedFont Font, ShapedRun Run, int Start, int Length)> Runs { get; } = []; + + /// Draws the line with its pen at (, ) in 's frame. + public void Draw(PageContent page, float x, float baseline, SKMatrix? transform, SKRect? clip) + { + foreach (var (font, run, start, length) in Runs) + { + var glyphs = run.ToGlyphRun(font.Font, size, new SKPoint(x, baseline), Text.Substring(start, length)) with + { + Color = color, + Embolden = font.SyntheticBold, + SkewX = font.SyntheticItalic ? GlyphRun.SyntheticSlant : 0, + Transform = transform, + Clip = clip, + }; + if (glyphs.Glyphs.Length > 0) + page.DrawGlyphs(glyphs); + x += run.Width(size); + } + } +} diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs index d57231c10..c7187558d 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs @@ -1,8 +1,11 @@ // DOCX graphic objects: DrawingML pictures, text boxes (wps), groups and drawing canvases, plus legacy VML -// (w:pict / w:object) used by older documents and as the fallback of mc:AlternateContent. +// (w:pict / w:object) used by older documents and as the fallback of mc:AlternateContent. Charts are read from their +// chart part (ChartReader) and become a picture of the chart for the writers that carries the chart for the renderer; +// those ChartReader doesn't draw are left out and counted. using System.Globalization; using System.Xml.Linq; +using Filee.Engines.Charts; using Filee.Engines.Documents; using ImageMagick; @@ -22,6 +25,12 @@ internal sealed partial class DocxReader private readonly Dictionary _themeColors = []; + /// The theme part's root (chart colours and fonts), or null. + private XElement? _theme; + + private Slides.DrawingColors? _chartColors; + private readonly ChartBudget _charts = new(); + private IEnumerable Drawing(XElement drawing) { foreach (var container in drawing.Elements()) @@ -83,10 +92,14 @@ private IEnumerable GraphicObjects(IEnumerable objects, int w foreach (var inline in Group(obj, width, height, anchor, groupTransform)) yield return inline; } - // Charts and SmartArt have no picture in the document part to fall back on: the renderer says so. + // Charts ChartReader doesn't draw and SmartArt have no picture in the document part to fall back on: the + // renderer says so. else if (obj.Name == Chart + "chart") { - _leftOut.Add(HLeftOutKind.Chart); + if (ChartPicture(obj, width, height, anchor) is { } chart) + yield return chart; + else + _leftOut.Add(HLeftOutKind.Chart); } else if (obj.Name == Dgm + "relIds") { @@ -143,6 +156,26 @@ private IEnumerable Group(XElement group, int width, int height, HAncho _ => position, }; + /// A chart from its part, as a picture of it (for the writers) carrying the chart (for the renderer); null when it isn't drawn. + private HImage? ChartPicture(XElement chart, int width, int height, HAnchor? anchor) + { + var id = Attr(chart, "id", R); + if (id is null || !_part.Relationships.TryGetValue(id, out var rel) || rel.External || width <= 0 || height <= 0) + return null; + _chartColors ??= Slides.DrawingColors.FromTheme(_theme); + if (ChartReader.Read(LoadPart(rel.Target)?.Xml.Root, _chartColors, ChartReader.ThemeFont(_theme)).Chart is not { } drawn) + return null; + _charts.Charge(drawn); + // Points are HWPUNIT / 100. + if (Charts.ChartPicture.Png(drawn, width / 100f, height / 100f, 150) is not { } png) + return null; + Directory.CreateDirectory(_mediaFolder); + var path = Path.Combine(_mediaFolder, $"docx-chart-{_media.Count + 1}.png"); + File.WriteAllBytes(path, png); + _media[$"chart|{path}"] = path; + return new HImage(path) { Width = width, Height = height, Anchor = anchor, Chart = drawn }; + } + private HImage? Picture(XElement pic, int width, int height, HAnchor? anchor) { var blip = pic.Descendants(A + "blip").FirstOrDefault(); @@ -404,6 +437,7 @@ private HBorder Line(XElement? ln, XElement? style) private void LoadThemeColors(Part? theme) { + _theme = theme?.Xml.Root; foreach (var color in theme?.Xml.Descendants(A + "clrScheme").FirstOrDefault()?.Elements() ?? []) { var value = Val(color.Element(A + "srgbClr")) ?? Attr(color.Element(A + "sysClr"), "lastClr"); diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs index 32a1ff001..74e431b73 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs @@ -748,6 +748,12 @@ internal sealed class HImage(string path) : HObject /// Colour changes drawn over the picture (Word's washout, grey scale, transparency); null = none. Read for layout only. public HPictureEffects? Effects { get; set; } + + /// + /// A chart (DOCX c:chart) the renderer draws instead of the picture, which is a picture of it for the writers; + /// null for a picture. Read for layout only. + /// + public Charts.Chart? Chart { get; set; } } /// A box with its own paragraphs (DOCX text box, 한글 글상자). diff --git a/src/Filee.Engines/Office/Sheets/Print/SheetPageRenderer.cs b/src/Filee.Engines/Office/Sheets/Print/SheetPageRenderer.cs index 1c7df0df6..922dfe28f 100644 --- a/src/Filee.Engines/Office/Sheets/Print/SheetPageRenderer.cs +++ b/src/Filee.Engines/Office/Sheets/Print/SheetPageRenderer.cs @@ -6,6 +6,7 @@ // footer with their fields. using System.Globalization; +using Filee.Engines.Charts; using Filee.Engines.Rendering; using Filee.Engines.Slides; using SkiaSharp; @@ -27,6 +28,7 @@ internal sealed class SheetPageRenderer private readonly Dictionary> _blanks = []; private readonly IReadOnlyList<(SheetDrawing Drawing, SKImage? Image)> _drawings; private readonly float _scale; + private ChartFonts? _charts; /// Characters "fill" alignment repeats a cell's text to at most (Excel's limit for a cell's text). private const int MaxFillCharacters = 32_767; @@ -832,12 +834,28 @@ private void DrawDrawings(PageContent page, List columns, List r } if (placed.Count == 0) return; - page.Draw(canvas => + // Pictures and shapes in runs between charts, which add their text as text. + for (var start = 0; start < placed.Count;) { - canvas.ClipRect(area); - foreach (var (drawing, image, rect) in placed) - Draw(canvas, drawing, image, rect, s); - }); + if (placed[start].Item1 is SheetChart chart) + { + _charts ??= new ChartFonts(_fonts.Resolver, _fonts.Shaper); + ChartDrawer.Draw(page, chart.Chart, placed[start].Item3, _charts, s, null, area); + start++; + continue; + } + var end = start; + while (end < placed.Count && placed[end].Item1 is not SheetChart) + end++; + var run = placed.GetRange(start, end - start); + page.Draw(canvas => + { + canvas.ClipRect(area); + foreach (var (drawing, image, rect) in run) + Draw(canvas, drawing, image, rect, s); + }); + start = end; + } } /// One picture or shape in its anchor's box, turned and mirrored about the box's centre. diff --git a/src/Filee.Engines/Office/Sheets/Print/SheetText.cs b/src/Filee.Engines/Office/Sheets/Print/SheetText.cs index 28dbbe12b..0a94129b4 100644 --- a/src/Filee.Engines/Office/Sheets/Print/SheetText.cs +++ b/src/Filee.Engines/Office/Sheets/Print/SheetText.cs @@ -48,6 +48,8 @@ internal sealed class SheetFonts : IDisposable public Shaper Shaper => _shaper; + public FontResolver Resolver => _resolver; + /// The font that draws a family (the family, a font for its script, or the language's default). public ResolvedFont? Font(string family, bool bold, bool italic) { @@ -361,6 +363,8 @@ public float Draw(PageContent page, int start, int end, float x, float baseline, if (from >= to) continue; var size = piece.Size * scale; + if (!(size > 0) || !float.IsFinite(size)) + continue; // a size no font is drawn at (a damaged style) var rise = piece.Style.Script > 0 ? -piece.Ascent * 0.35f * scale : piece.Style.Script < 0 ? piece.Ascent * 0.15f * scale : 0; var x0 = x; // Words one by one when spaces are widened; else the piece in one run. diff --git a/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs b/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs index 67f8a3ebc..cf475ad2c 100644 --- a/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs +++ b/src/Filee.Engines/Office/Sheets/SheetCapabilities.cs @@ -2,10 +2,10 @@ // LibreOffice when it is installed, and otherwise prints it and says what was left out. // // Only what prints is looked at: visible sheets, and drawings not set to stay off the printout. -// * XLSX: a visible chart sheet ("charts"); in each visible worksheet's drawing what XlsxDrawing leaves out (charts, -// 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. +// * XLSX: a visible chart sheet ("charts"); in each visible worksheet's drawing what XlsxDrawing leaves out (charts +// ChartReader doesn't draw, 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. diff --git a/src/Filee.Engines/Office/Sheets/Workbook.cs b/src/Filee.Engines/Office/Sheets/Workbook.cs index b52ac4243..fb87c3d57 100644 --- a/src/Filee.Engines/Office/Sheets/Workbook.cs +++ b/src/Filee.Engines/Office/Sheets/Workbook.cs @@ -331,6 +331,10 @@ internal sealed record SheetPicture(byte[] Data, int Row, int Column, double Off public (double Left, double Top, double Right, double Bottom) Crop { get; init; } } +/// A chart on a sheet (a graphic frame of a chart part), drawn by ChartDrawer. +internal sealed record SheetChart(Charts.Chart Chart, int Row, int Column, double OffsetX, double OffsetY, double Width, double Height) + : SheetDrawing(Row, Column, OffsetX, OffsetY, Width, Height); + /// A preset shape on a sheet (rectangle, oval, line, arrow, ...), filled and outlined in one colour each. /// The a:prstGeom name, one draws. internal sealed record SheetShape(string Preset, int Row, int Column, double OffsetX, double OffsetY, double Width, double Height) diff --git a/src/Filee.Engines/Office/Sheets/XlsbReader.cs b/src/Filee.Engines/Office/Sheets/XlsbReader.cs index 3af80a055..c13065fb9 100644 --- a/src/Filee.Engines/Office/Sheets/XlsbReader.cs +++ b/src/Filee.Engines/Office/Sheets/XlsbReader.cs @@ -105,6 +105,7 @@ internal sealed class XlsbReader private readonly List _fonts = []; private OoxmlColors _colors = OoxmlColors.FromTheme(null); private XDocument? _theme; + private readonly Charts.ChartBudget _charts = new(); private NumberFormatter _formatter = new(); private bool _date1904; private int _records; @@ -713,7 +714,7 @@ private Worksheet ReadSheet(string name, string partPath) 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); + sheet.Drawings.AddRange(XlsxDrawing.Read(_package, drawingPart, _theme?.Root, read: true, _charts).Drawings); return sheet; } diff --git a/src/Filee.Engines/Office/Sheets/XlsxDrawing.cs b/src/Filee.Engines/Office/Sheets/XlsxDrawing.cs index e62ab2c0a..fcb696cc9 100644 --- a/src/Filee.Engines/Office/Sheets/XlsxDrawing.cs +++ b/src/Filee.Engines/Office/Sheets/XlsxDrawing.cs @@ -5,6 +5,8 @@ // group's anchor. What the printer doesn't draw is named instead: charts, SmartArt, embedded objects, ink, text in // shapes, freeform shapes, gradient, pattern and picture fills, arrowheads, turned groups, EMF / WMF / TIFF and linked // pictures. SheetCapabilities asks for that name, so such a workbook can go to LibreOffice where it is installed. +// Charts are read from their chart part (ChartReader, with the workbook's theme) and drawn; those ChartReader doesn't +// draw (3D, radar, stock, ...) and new chart types (chartex) are named "charts". // // Excel anchors a turned picture or shape by the box it covers on the sheet: one turned by 90° has an anchor as wide as // the picture is tall. The unturned size (a:xfrm ext) tells which way round the anchor is (SheetDrawing.Size). @@ -12,6 +14,7 @@ using System.Globalization; using System.Xml.Linq; +using Filee.Engines.Charts; using Filee.Engines.Office.Ooxml; using Filee.Engines.Slides; using SkiaSharp; @@ -46,6 +49,8 @@ internal sealed class XlsxDrawing private readonly string _part; private readonly DrawingColors _colors; private readonly XElement? _formatScheme; + private readonly string? _themeFont; + private readonly ChartBudget? _charts; private readonly bool _read; /// What is drawn, in drawing order; empty when only the check was asked for. @@ -54,12 +59,14 @@ internal sealed class XlsxDrawing /// The first thing in the part the printer doesn't draw, or null. public string? FirstLeftOut { get; private set; } - private XlsxDrawing(OpcPackage package, string part, XElement? theme, bool read) + private XlsxDrawing(OpcPackage package, string part, XElement? theme, bool read, ChartBudget? charts) { _package = package; _part = part; _colors = DrawingColors.FromTheme(theme); _formatScheme = theme?.Descendants(A + "fmtScheme").FirstOrDefault(); + _themeFont = ChartReader.ThemeFont(theme); + _charts = charts; _read = read; } @@ -68,9 +75,10 @@ private XlsxDrawing(OpcPackage package, string part, XElement? theme, bool read) /// no picture is unpacked and only is found). /// /// The workbook's theme part (colours and line styles shapes refer to), or null. - public static XlsxDrawing Read(OpcPackage package, string part, XElement? theme, bool read) + /// The workbook's count of chart points drawn (with ). + public static XlsxDrawing Read(OpcPackage package, string part, XElement? theme, bool read, ChartBudget? charts = null) { - var drawing = new XlsxDrawing(package, part, theme, read); + var drawing = new XlsxDrawing(package, part, theme, read, charts); foreach (var anchor in package.Part(part)?.Root?.Elements() ?? []) drawing.Anchor(anchor); return drawing; @@ -169,7 +177,7 @@ private void Object(XElement element, Box box, Space? space, int depth) Group(element, box, space, depth); break; case "graphicFrame": - LeftOut(Graphic((string?)element.Element(A + "graphic")?.Element(A + "graphicData")?.Attribute("uri") ?? "")); + Frame(element, box, space); break; case "contentPart": LeftOut("ink"); @@ -177,6 +185,29 @@ private void Object(XElement element, Box box, Space? space, int depth) } } + /// A graphic frame: a chart ChartReader draws, or what else it holds, named. + private void Frame(XElement frame, Box box, Space? space) + { + var data = frame.Element(A + "graphic")?.Element(A + "graphicData"); + var uri = (string?)data?.Attribute("uri") ?? ""; + if (!uri.EndsWith("/chart", StringComparison.Ordinal)) + { + LeftOut(Graphic(uri)); + return; + } + var target = _package.Target(_part, (string?)data?.Element(ChartReader.C + "chart")?.Attribute(R + "id")); + var read = ChartReader.Read(target is null ? null : _package.Part(target)?.Root, _colors, _themeFont); + if (read.Chart is not { } chart) + { + LeftOut(read.LeftOut ?? "charts"); + return; + } + if (!_read) + return; + _charts?.Charge(chart); + Add(Placed(new SheetChart(chart, box.Row, box.Column, box.X, box.Y, box.Width, box.Height), box, space, frame.Element(Xdr + "xfrm"))); + } + /// What a graphic frame holds, named. private static string Graphic(string uri) => uri.EndsWith("/chart", StringComparison.Ordinal) || uri.EndsWith("/chartex", StringComparison.Ordinal) ? "charts" diff --git a/src/Filee.Engines/Office/Sheets/XlsxReader.cs b/src/Filee.Engines/Office/Sheets/XlsxReader.cs index e2e954d49..062d4c2a6 100644 --- a/src/Filee.Engines/Office/Sheets/XlsxReader.cs +++ b/src/Filee.Engines/Office/Sheets/XlsxReader.cs @@ -3,14 +3,15 @@ // borders (diagonals too), alignment (wrap, shrink, rotation, indent, justify, fill, centre across), merged cells, // column widths, row heights (custom and the autofit Excel saved), hidden rows / columns / sheets, frozen header // rows, and what printing needs: the page setup, margins, print options, headers and footers (with their -// pictures), print areas, print titles, manual page breaks, conditional formats that colour cells, and the pictures -// and shapes of each sheet's drawing (XlsxDrawing, which names what it leaves out: charts, SmartArt, ...). Comments -// are not read. The package is read within DocumentLimits. +// pictures), print areas, print titles, manual page breaks, conditional formats that colour cells, and the pictures, +// shapes and charts of each sheet's drawing (XlsxDrawing, which names what it leaves out: 3D charts, SmartArt, ...). +// Comments are not read. The package is read within DocumentLimits; the charts' points within ChartBudget. using System.Globalization; using System.Text; using System.Text.RegularExpressions; using System.Xml.Linq; +using Filee.Engines.Charts; using Filee.Engines.Documents; using Filee.Engines.Office.Ooxml; @@ -42,6 +43,7 @@ internal sealed class XlsxReader private NumberFormatter _formatter = new(); private bool _date1904; private int _columnEntries; + private readonly ChartBudget _charts = new(); private XlsxReader(OpcPackage package) => _package = package; @@ -232,7 +234,7 @@ private void ReadSharedStrings(XDocument? xml) Italic = font.Element(S + "i") is { } i ? On(i) : null, Underline = font.Element(S + "u") is { } u ? (string?)u.Attribute("val") != "none" : null, Strike = font.Element(S + "strike") is { } st ? On(st) : null, - FontSize = Number(font.Element(S + "sz")?.Attribute("val")), + FontSize = FontSize(font.Element(S + "sz")?.Attribute("val")), Color = _colors.Spreadsheet(font.Element(S + "color")), FontName = (string?)font.Element(S + "rFont")?.Attribute("val"), Script = (string?)font.Element(S + "vertAlign")?.Attribute("val") switch { "superscript" => 1, "subscript" => -1, "baseline" => 0, _ => null }, @@ -372,7 +374,7 @@ private FontInfo Font(XElement f) underline != "none", underline is "double" or "doubleAccounting", On(f.Element(S + "strike")), - Number(f.Element(S + "sz")?.Attribute("val")), + FontSize(f.Element(S + "sz")?.Attribute("val")), _colors.Spreadsheet(f.Element(S + "color")), (string?)f.Element(S + "name")?.Attribute("val"), (string?)f.Element(S + "vertAlign")?.Attribute("val") switch { "superscript" => 1, "subscript" => -1, _ => 0 }); @@ -417,6 +419,9 @@ CellBorder Side(XElement? side) => side is null || (string?)side.Attribute("styl private static int Index(XElement element, string attribute) => int.TryParse((string?)element.Attribute(attribute), out var value) && value >= 0 ? value : 0; + /// A font size in points, within Excel's 1 to 409. + private static double? FontSize(XAttribute? attribute) => Number(attribute) is { } size ? Math.Clamp(size, 1, 409) : null; + private static double? Number(XAttribute? attribute) => double.TryParse((string?)attribute, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) && double.IsFinite(value) ? value : null; @@ -525,7 +530,7 @@ private Worksheet ReadSheet(string name, XElement xml, string partPath) foreach (var formatting in xml.Elements(S + "conditionalFormatting")) ConditionalFormats(formatting, sheet); if (_package.Target(partPath, (string?)xml.Element(S + "drawing")?.Attribute(R + "id")) is { } drawing) - sheet.Drawings.AddRange(XlsxDrawing.Read(_package, drawing, _theme?.Root, read: true).Drawings); + sheet.Drawings.AddRange(XlsxDrawing.Read(_package, drawing, _theme?.Root, read: true, _charts).Drawings); return sheet; } diff --git a/src/Filee.Engines/Slides/PptxSlideReader.cs b/src/Filee.Engines/Slides/PptxSlideReader.cs index 6bff562e2..3c8e0ff43 100644 --- a/src/Filee.Engines/Slides/PptxSlideReader.cs +++ b/src/Filee.Engines/Slides/PptxSlideReader.cs @@ -11,7 +11,8 @@ // layout's and slide's overrides), theme fonts (+mj-lt, +mn-ea, script fonts by language) and theme style // references (fillRef, lnRef, fontRef, bgRef). // - Shapes (preset and custom geometry), connectors, pictures (crop, fill rectangle, tiles, recolouring), groups, tables -// (with tableStyles.xml styles), SmartArt from the drawing PowerPoint keeps with it, OLE objects and charts as the +// (with tableStyles.xml styles), SmartArt from the drawing PowerPoint keeps with it, charts from their chart part +// (ChartReader; drawn by ChartDrawer, a picture of them for what takes pictures), OLE objects and other charts as the // picture stored for them, mc:AlternateContent through its fallback. // - Fills: solid, gradient (linear, circle, rectangle; group fill), picture, pattern. Lines: width, dash, cap, join, // compound, arrowheads. @@ -26,6 +27,7 @@ using System.Globalization; using System.Xml.Linq; +using Filee.Engines.Charts; using Filee.Engines.Documents; using SkiaSharp; @@ -64,6 +66,7 @@ internal sealed partial class PptxSlideReader private long _commands; private long _visits; private readonly long _maxVisits; + private readonly ChartBudget _charts = new(); /// Custom geometry (a:custGeom) read, with what one use of it costs against . private readonly Dictionary _customGeometry = new(ReferenceEqualityComparer.Instance); @@ -477,6 +480,8 @@ private Placeholders PlaceholdersOf(XElement owner) return Table(context, part, table, xfrm); if (uri.EndsWith("/diagram", StringComparison.Ordinal)) return SmartArt(context, part, data, xfrm); + if (uri.EndsWith("/chart", StringComparison.Ordinal) && Chart(context, part, data, xfrm) is { } chart) + return chart; // An OLE object, chart or other content with a picture of it (p:oleObj/p:pic, or a fallback picture). if (data.Descendants(P + "pic").FirstOrDefault() is { } picture) { @@ -487,6 +492,21 @@ private Placeholders PlaceholdersOf(XElement owner) return null; } + /// A chart drawn from its chart part, as a picture-filled shape that carries the chart; null when ChartReader doesn't draw it. + private SlideShape? Chart(SlideContext context, string part, XElement data, XElement xfrm) + { + var target = _package.Target(part, (string?)data.Element(ChartReader.C + "chart")?.Attribute(R + "id")); + if (ChartReader.Read(target is null ? null : _package.Part(target), context.Colors, context.Fonts.Minor.Latin).Chart is not { } chart) + return null; + _charts.Charge(chart); + var shape = new SlideShape { IsPicture = true, Chart = chart }; + Place(shape, xfrm); + var (width, height) = (shape.Bounds.Width, shape.Bounds.Height); + shape.Fill = new PictureFill(new SlideImage(() => ChartPicture.Png(chart, width, height) ?? []), SKRect.Empty, SKRect.Empty); + shape.Paths = Geometry([], shape.Bounds).Paths; + return shape; + } + /// /// SmartArt as the drawing PowerPoint stores with it (the data part's dsp:dataModelExt names it), in the frame's /// coordinates; null when there is none. diff --git a/src/Filee.Engines/Slides/SlideCapabilities.cs b/src/Filee.Engines/Slides/SlideCapabilities.cs index 34f173653..d375101b6 100644 --- a/src/Filee.Engines/Slides/SlideCapabilities.cs +++ b/src/Filee.Engines/Slides/SlideCapabilities.cs @@ -2,7 +2,7 @@ // FileeRendererConverter): such a deck takes the route it would take without the renderer (LibreOffice). Only the // shown slides and the layouts and masters they use are looked at, as the reader reads them. // -// Named: charts without a stored picture, SmartArt without its stored drawing, OLE objects and other embedded content +// Named: charts ChartReader doesn't draw (3D, radar, stock, ..., and chartex) without a stored picture, SmartArt without its stored drawing, OLE objects and other embedded content // without a picture, ink, shadows and other effects (glow, reflection, soft edges, blur; on shapes, pictures and text, // or through the theme's effect styles), 3D, WordArt warps, outlined text, East Asian vertical text, preset shapes not // in PresetShapes, and pictures Skia can't decode (TIFF, JPEG XR). @@ -156,7 +156,12 @@ private static IEnumerable Drawn(XElement root) var uri = (string?)data.Attribute("uri") ?? ""; if (data.Element(A + "tbl") is not null || data.Descendants(P + "pic").Any()) return null; - if (uri.EndsWith("/chart", StringComparison.Ordinal) || uri.EndsWith("/chartex", StringComparison.Ordinal)) + if (uri.EndsWith("/chart", StringComparison.Ordinal)) + { + var target = package.Target(part, (string?)data.Element(Charts.ChartReader.C + "chart")?.Attribute(R + "id")); + return Charts.ChartReader.Read(target is null ? null : package.Part(target), DrawingColors.FromTheme(null)).LeftOut; + } + if (uri.EndsWith("/chartex", StringComparison.Ordinal)) return "charts"; if (uri.EndsWith("/diagram", StringComparison.Ordinal)) return package.Part(package.DiagramDrawing(part, data)) is null ? "SmartArt" : null; diff --git a/src/Filee.Engines/Slides/SlideModel.cs b/src/Filee.Engines/Slides/SlideModel.cs index efc86c0cb..42f609e06 100644 --- a/src/Filee.Engines/Slides/SlideModel.cs +++ b/src/Filee.Engines/Slides/SlideModel.cs @@ -63,6 +63,12 @@ internal sealed class SlideShape : SlideElement /// A picture fill keeps its place in the box when the shape turns (DrawingML rotWithShape) — always, for pictures. public bool IsPicture { get; set; } + + /// + /// A chart: the renderer draws it; what takes pictures (the ODP writer, the deck's text) takes the picture of it the + /// shape's fill makes when asked. + /// + public Charts.Chart? Chart { get; set; } } /// How a path's inside is filled. @@ -205,10 +211,17 @@ internal enum GradientShape internal sealed record GradientFill(IReadOnlyList Stops, GradientShape Shape, float Angle, bool Scaled, SKRect Focus, bool RotateWithShape = true) : SlideFill; -/// A picture's encoded bytes (PNG, JPEG, GIF, BMP, TIFF, EMF, WMF). -internal sealed class SlideImage(byte[] data) +/// A picture's encoded bytes (PNG, JPEG, GIF, BMP, TIFF, EMF, WMF), or what makes them when they're first asked for. +internal sealed class SlideImage { - public byte[] Data { get; } = data; + private byte[]? _data; + private readonly Func? _make; + + public SlideImage(byte[] data) => _data = data; + + public SlideImage(Func make) => _make = make; + + public byte[] Data => _data ??= _make?.Invoke() ?? []; } /// Colour changes of a picture. diff --git a/src/Filee.Engines/Slides/SlideRenderer.cs b/src/Filee.Engines/Slides/SlideRenderer.cs index 63baf4056..a586caa61 100644 --- a/src/Filee.Engines/Slides/SlideRenderer.cs +++ b/src/Filee.Engines/Slides/SlideRenderer.cs @@ -9,7 +9,9 @@ // unwrapped when the frame doesn't wrap, turned for vertical frames. Text turns with its shape but isn't mirrored // (PowerPoint turns it upside down for a vertically flipped shape). // - Tables: rows as high as their content needs at least, cells' fills, text and borders, merged cells over their span. +// - Charts: drawn by ChartDrawer in the shape's box, turned with it. +using Filee.Engines.Charts; using Filee.Engines.Rendering; using Filee.Engines.Typesetting; using SkiaSharp; @@ -22,12 +24,14 @@ internal sealed class SlideRenderer : IDisposable private const float Unbounded = 100_000; private readonly ParagraphLayouter _layouter; + private readonly ChartFonts _charts; private readonly SlideImages _images = new(); private readonly CancellationToken _cancellationToken; public SlideRenderer(FontResolver fonts, Shaper shaper, CancellationToken cancellationToken = default) { _layouter = new ParagraphLayouter(fonts, shaper, storedLines: false); + _charts = new ChartFonts(fonts, shaper); _cancellationToken = cancellationToken; } @@ -97,6 +101,9 @@ private void Element(PageContent page, SlideElement element, SKMatrix parent) foreach (var item in group.Children) Element(page, item, inner); break; + case SlideShape { Chart: { } chart } shape: + ChartDrawer.Draw(page, chart, new SKRect(0, 0, shape.Bounds.Width, shape.Bounds.Height), _charts, 1, matrix); + break; case SlideShape shape: Shape(page, shape, matrix); if (shape.Text is { IsEmpty: false } text) diff --git a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs index da0902919..bddc101a3 100644 --- a/src/Filee.Engines/Typesetting/FileeRendererConverter.cs +++ b/src/Filee.Engines/Typesetting/FileeRendererConverter.cs @@ -11,7 +11,8 @@ // presentations or spreadsheets, the engine leaves those files to it (its edge would win on cost otherwise). // // Its edges are Checked: before a file is converted, FirstUnsupported looks through it for what the layout engine -// doesn't draw yet (OLE objects and charts, vertical text, form controls, SmartArt), and such a file takes the route +// doesn't draw yet (OLE objects, charts other than the DrawingML ones ChartReader draws, vertical text, form controls, +// SmartArt), and such a file takes the route // it would take without the engine (JobQueue). An HWP is looked through as the HWPX package it becomes; a deck by // SlideCapabilities (charts, effects, ...). Pictures of document pages go on from the PDF through PDFium, which applies // the image options; pictures of slides are drawn directly, with the image options applied here. @@ -156,12 +157,21 @@ public EngineStatus GetStatus() { using var package = DocumentPackage.Open(sourcePath); long scanned = 0; + var referencesCharts = false; + List? charts = null; foreach (var entry in package.Entries) { var name = entry.FullName; if (!name.EndsWith(".xml", StringComparison.OrdinalIgnoreCase) || (scanned += entry.Length) > MaxScannedBytes || package.BytesRead > MaxScannedBytes) continue; + // A Word chart part: drawn when ChartReader draws it. + if (markers == WordMarkers && WordChartPart().IsMatch(name)) + { + charts ??= []; + charts.Add(Charts.ChartReader.Read(package.LoadXml(entry, preserveWhitespace: false).Root, Filee.Engines.Slides.DrawingColors.FromTheme(null)).LeftOut); + continue; + } using var reader = new StreamReader(package.Open(entry), Encoding.UTF8); var xml = reader.ReadToEnd(); foreach (var (pattern, feature) in markers) @@ -169,10 +179,12 @@ public EngineStatus GetStatus() if (pattern.IsMatch(xml)) return feature; } + referencesCharts |= markers == WordMarkers && WordChart().IsMatch(xml); } - return null; + // Charts are drawn when every chart part is one ChartReader draws. + return referencesCharts && (charts is null || charts.Any(c => c is not null)) ? "OLE objects and charts" : null; } - catch (ConversionRefusedException) + catch (Exception ex) when (ex is ConversionRefusedException or System.Xml.XmlException) { return null; } @@ -371,9 +383,16 @@ private static async Task> ConvertSlidesAsync(ConversionSt [GeneratedRegex(@"<\w+:video\b")] private static partial Regex HwpxVideo(); - [GeneratedRegex(@"A chart part of a Word package (word/charts/chart1.xml; chartEx parts too, which aren't drawn). + [GeneratedRegex(@"^word/charts/chart(?:Ex)?\d*\.xml$", RegexOptions.IgnoreCase)] + private static partial Regex WordChartPart(); + [GeneratedRegex(@" Transformed(canvas, picture.Transform, area, () => { diff --git a/tests/Filee.Engines.Tests/ChartBuilder.cs b/tests/Filee.Engines.Tests/ChartBuilder.cs new file mode 100644 index 000000000..9fd778133 --- /dev/null +++ b/tests/Filee.Engines.Tests/ChartBuilder.cs @@ -0,0 +1,119 @@ +// DrawingML chart parts (c:chartSpace) written in code for the chart tests: plots of series with cached categories and +// values, two axes, a title and a legend, in the shape Excel, Word and PowerPoint save them. + +using System.Globalization; +using System.Text; + +namespace Filee.Engines.Tests; + +internal static class ChartBuilder +{ + public const string Namespaces = + "xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" " + + "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\""; + + public const string ContentType = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"; + + public const string GraphicUri = "http://schemas.openxmlformats.org/drawingml/2006/chart"; + + private static string N(double value) => value.ToString(CultureInfo.InvariantCulture); + + public static string Solid(string rgb) => $""; + + /// A series: name, categories (text) and values (null = blank), with more elements (c:spPr, c:dLbls, ...). + public static string Series(int index, string name, string[] categories, double?[] values, string extra = "", string valueFormat = "General") + { + var sb = new StringBuilder(); + sb.Append($""); + sb.Append($"Sheet1!$B${index + 1}{System.Security.SecurityElement.Escape(name)}"); + sb.Append(extra); + if (categories.Length > 0) + { + sb.Append($"Sheet1!$A$2:$A${categories.Length + 1}"); + for (var i = 0; i < categories.Length; i++) + sb.Append($"{System.Security.SecurityElement.Escape(categories[i])}"); + sb.Append(""); + } + sb.Append($"Sheet1!$B$2:$B${values.Length + 1}{valueFormat}"); + for (var i = 0; i < values.Length; i++) + { + if (values[i] is { } v) + sb.Append($"{N(v)}"); + } + sb.Append(""); + return sb.ToString(); + } + + /// A scatter series: X and Y numbers. + public static string ScatterSeries(int index, string name, double[] x, double[] y, string extra = "") + { + static string Cache(double[] values) => + $"xGeneral" + + string.Concat(values.Select((v, i) => $"{N(v)}")) + ""; + return $"{name}{extra}{Cache(x)}{Cache(y)}"; + } + + /// A bar or column plot (c:barChart) against axes 1 and 2. + public static string Bars(string series, string direction = "col", string grouping = "clustered", string extra = "") => + $"{series}{extra}" + + (grouping == "clustered" ? "" : "") + ""; + + public static string Lines(string series, string grouping = "standard", bool markers = true) => + $"{series}"; + + public static string Areas(string series, string grouping = "standard") => + $"{series}"; + + public static string Pie(string series, string extra = "") => + $"{series}{extra}"; + + public static string Doughnut(string series, int hole = 50) => + $"{series}"; + + public static string Scatter(string series, string style = "lineMarker") => + $"{series}"; + + /// A category axis (id 1) and a value axis (id 2) with gridlines; value and X axes for scatter plots. + public static string Axes(bool horizontal = false, bool scatter = false, string valueExtra = "", string categoryExtra = "", string categoryTitle = "", string valueTitle = "") => + (scatter ? "" : "") + "" + + $"{Title(categoryTitle)}" + + $"{categoryExtra}" + (scatter ? "" : "") + + "" + + $"{Title(valueTitle)}" + + $"{valueExtra}"; + + public static string Title(string text) => + text.Length == 0 ? "" : $"{System.Security.SecurityElement.Escape(text)}"; + + /// A whole chart part. + public static string Chart(string plots, string axes = "", string title = "", string legend = "r", string spPr = "", string extra = "") => + $"" + + (title.Length > 0 ? Title(title) + "" : "") + + $"{plots}{axes}" + + (legend.Length > 0 ? $"" : "") + + $"{extra}{spPr}"; + + /// A Word run with a chart (wp:inline around c:chart) of × EMU. + public static string InlineChart(string relationshipId, long cx, long cy) => + $"" + + $""; + + /// Three series of four quarters as a clustered column chart, red, green and blue. + public static string Columns(string grouping = "clustered", string direction = "col", string title = "Sales by quarter") + { + string[] quarters = ["Q1", "Q2", "Q3", "Q4"]; + var series = Series(0, "North", quarters, [40, 55, 70, 62], Solid("C00000")) + + Series(1, "South", quarters, [30, 45, 35, 50], Solid("00B050")) + + Series(2, "West", quarters, [20, 25, 40, 30], Solid("0070C0")); + return Chart(Bars(series, direction, grouping), Axes(horizontal: direction == "bar", valueTitle: "Units", categoryTitle: "Quarter"), title); + } + + /// A pie of four slices in red, green, blue and yellow (c:dPt fills). + public static string Slices(string labels = "") + { + string[] names = ["Rent", "Food", "Travel", "Other"]; + var points = string.Concat(new[] { "FF0000", "00C000", "0000FF", "FFC000" }.Select((rgb, i) => + $"")); + return Chart(Pie(Series(0, "Budget", names, [40, 30, 20, 10], points + labels)), title: "Budget"); + } +} diff --git a/tests/Filee.Engines.Tests/ChartFuzzTests.cs b/tests/Filee.Engines.Tests/ChartFuzzTests.cs new file mode 100644 index 000000000..1174d310f --- /dev/null +++ b/tests/Filee.Engines.Tests/ChartFuzzTests.cs @@ -0,0 +1,136 @@ +// Mutation fuzzing of DrawingML charts (Charts/): the seeds are ChartTests' column chart, a pie with labels and a +// chart with what costs the reader and the drawer the most (several plots on two pairs of axes, stacked bars, smooth +// lines with markers and data labels, a doughnut's rings, scatter, a log axis, manual layouts, turned labels, dates), +// written in code, as a chart part alone (XmlFuzzer) and inside a workbook the sheet printer prints (DocumentFuzzer). +// Every input must be read or refused with InvalidDataException / ConversionRefusedException, in time and on a 1 MB +// stack; what is read is drawn small. 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.Text; +using System.Xml; +using Filee.Engines.Charts; +using Filee.Engines.Documents; +using Filee.Engines.Infrastructure; +using Filee.Engines.Office.Sheets; +using Filee.Engines.Rendering; +using Filee.Engines.Slides; +using SkiaSharp; +using static Filee.Engines.Tests.ChartBuilder; + +namespace Filee.Engines.Tests; + +[Trait("Category", "Fuzz")] +public class ChartFuzzTests(EngineFixture fx) : IClassFixture +{ + /// Inputs per seed in a normal test run. + private const int QuickIterations = 40; + + private static readonly string[] Tokens = + [ + "", "NaN", "1e309", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", "", "", "", + "", + "", + "", "", + "", "", "&", "", + ]; + + [Fact] + public void ChartReader_reads_or_refuses_every_mutated_chart_part() => + new FuzzHarness(fx, QuickIterations, 20261120).Data("chart", "xml", + [.. new[] { Columns(), Slices(Labels), Costly(), Rings(), Points() }.Select(Encoding.UTF8.GetBytes)], FuzzHarness.Xml(Tokens), DrawPart); + + [Fact] + public void A_workbook_with_charts_is_read_or_refused_whatever_its_parts_say() => + new FuzzHarness(fx, QuickIterations, 20261120).Files("chart-xlsx", "xlsx", [Workbook(Columns()), Workbook(Costly()), Workbook(Rings())], FuzzHarness.Packages, Print); + + private const string Labels = ""; + + private static void DrawPart(byte[] bytes) + { + System.Xml.Linq.XDocument xml; + try + { + xml = BoundedXml.Bytes(bytes, "chart1.xml", System.Xml.Linq.LoadOptions.None); + } + catch (XmlException ex) + { + throw new InvalidDataException(ex.Message, ex); + } + if (ChartReader.Read(xml.Root, DrawingColors.FromTheme(null)).Chart is not { } chart) + return; + var page = new PageContent(240, 160); + using (var fonts = new ChartFonts()) + ChartDrawer.Draw(page, chart, new SKRect(0, 0, 240, 160), fonts); + page.Render(24).Dispose(); + } + + private static void Print(string path, string media) + { + _ = media; + SheetCapabilities.FirstUnsupported(path, "xlsx"); + var book = XlsxReader.Read(path); + using var printed = SheetPrinter.Print(book, new SheetPrintSource("book.xlsx", "C:\\"), DocumentLimits.Default, DateTime.Now, null, default); + if (printed.Pages.Count > 0) + printed.Pages[0].Render(12).Dispose(); + } + + private byte[] Workbook(string chart) + { + var book = new XlsxBuilder(); + book.Sheet("Data", XlsxBuilder.Row(1, book.Text("A1", "Sales")), after: ""); + book.SheetRelationships[1] = [("rIdD", "drawing", "../drawings/drawing1.xml")]; + book.Parts["xl/drawings/drawing1.xml"] = + "10" + + "1060120" + + "" + + $"" + + ""; + book.Parts["xl/drawings/_rels/drawing1.xml.rels"] = OpcWriter.Relationships([("rIdC", "chart", "../charts/chart1.xml")]); + book.Parts["xl/charts/chart1.xml"] = chart; + return File.ReadAllBytes(book.Save(Path.Combine(fx.NewFolder(), "seed.xlsx"))); + } + + /// What costs the chart reader and drawer the most, in one chart. + internal static string Costly() + { + string[] months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"]; + double?[] up = [1, 4, 9, 16, null, 36, 49, 64]; + double?[] down = [-3, 2, -1, 5, 8, -2, 4, 0]; + var marker = ""; + var point = ""; + var bars = Bars(Series(0, "Up", months, up, point + Labels) + Series(1, "Down", months, down), grouping: "stacked"); + var lines = "" + + Series(2, "Trend", months, up, marker + "", "0.0%") + + ""; + var axes = Axes(categoryExtra: "", + valueExtra: "", categoryTitle: "Month", valueTitle: "Units") + + "" + + "" + + ""; + var layout = ""; + var chart = Chart(bars + lines, axes, "Costly\nchart", legend: "b", + spPr: ""); + return chart.Replace("", "" + layout, StringComparison.Ordinal); + } + + /// A doughnut of three rings with a pulled-out slice and labels outside. + internal static string Rings() + { + string[] parts = ["a", "b", "c", "d", "e"]; + var rings = string.Concat(Enumerable.Range(0, 3).Select(i => Series(i, $"Ring {i}", parts, [.. parts.Select((_, k) => (double?)(k + i + 1))], + "" + Labels))); + return Chart(Doughnut(rings, 35), title: "Rings", legend: "tr"); + } + + /// Two scatter series, smooth with markers and markers only, on a logarithmic X axis. + internal static string Points() => + Chart(Scatter(ScatterSeries(0, "Smooth", [1, 10, 100, 1000], [2, 3, 1, 4], "") + + ScatterSeries(1, "Dots", [5, 50, 500], [1, 2, 3], ""), "smoothMarker"), + Axes(scatter: true).Replace("", "", StringComparison.Ordinal), + "Points", legend: "l"); +} diff --git a/tests/Filee.Engines.Tests/ChartTests.cs b/tests/Filee.Engines.Tests/ChartTests.cs new file mode 100644 index 000000000..b7d8340ec --- /dev/null +++ b/tests/Filee.Engines.Tests/ChartTests.cs @@ -0,0 +1,356 @@ +// DrawingML charts (Charts/): the chart part read into the model (ChartReader), drawn as vectors with text as glyph +// runs (ChartDrawer), and drawn where they appear: on printed sheets (XlsxDrawing, SheetCapabilities), in Word +// documents (DocxReader, FloatingObjects, the renderer's check) and on slides (PptxSlideReader, SlideRenderer, +// SlideCapabilities). Charts are built in code (ChartBuilder); pictures are checked by how much of each series' colour +// they hold, not by where the fonts put the text, so Linux fonts don't move the goal posts. + +using System.Diagnostics; +using System.Xml.Linq; +using Filee.Core.Conversion; +using Filee.Engines.Charts; +using Filee.Engines.Documents; +using Filee.Engines.Hwp.Hwpx; +using Filee.Engines.Hwp.Hwpx.Docx; +using Filee.Engines.Office; +using Filee.Engines.Office.Sheets; +using Filee.Engines.Rendering; +using Filee.Engines.Slides; +using Filee.Engines.Typesetting; +using SkiaSharp; +using static Filee.Engines.Tests.ChartBuilder; + +namespace Filee.Engines.Tests; + +public sealed class ChartTests(EngineFixture fx) : IClassFixture +{ + private static readonly SKColor Red = new(0xC0, 0x00, 0x00), Green = new(0x00, 0xB0, 0x50), Blue = new(0x00, 0x70, 0xC0); + + private static ChartRead Read(string xml) => ChartReader.Read(XDocument.Parse(xml).Root, DrawingColors.FromTheme(null)); + + private static Chart Drawn(string xml) => Read(xml).Chart ?? throw new InvalidOperationException("The chart isn't drawn."); + + /// The chart drawn alone on a page of its size, and the page. + private static (SKBitmap Bitmap, PageContent Page) Picture(string xml, float width = 480, float height = 300) + { + var page = new PageContent(width, height); + using (var fonts = new ChartFonts()) + ChartDrawer.Draw(page, Drawn(xml), new SKRect(0, 0, width, height), fonts); + return (page.Render(72), page); + } + + /// Pixels within a few steps of , in columns to (fractions of the width). + private static int Count(SKBitmap bitmap, SKColor color, double from = 0, double to = 1) + { + var count = 0; + for (var x = (int)(from * bitmap.Width); x < (int)(to * bitmap.Width); x++) + { + for (var y = 0; y < bitmap.Height; y++) + { + var p = bitmap.GetPixel(x, y); + if (Math.Abs(p.Red - color.Red) <= 12 && Math.Abs(p.Green - color.Green) <= 12 && Math.Abs(p.Blue - color.Blue) <= 12) + count++; + } + } + return count; + } + + private static string Text(PageContent page) => + string.Concat(page.Items.OfType().Select(g => g.Run.Text)); + + // ───────────────────────── Reading ───────────────────────── + + [Fact] + public void A_column_chart_is_read_from_its_cached_values() + { + var chart = Drawn(Columns()); + + var plot = Assert.Single(chart.Plots); + Assert.Equal((ChartKind.Bar, ChartGrouping.Standard, false), (plot.Kind, plot.Grouping, plot.Horizontal)); + Assert.Equal(["North", "South", "West"], plot.Series.Select(s => s.Name)); + Assert.Equal([40.0, 55, 70, 62], plot.Series[0].Values.Select(v => v!.Value)); + Assert.Equal(["Q1", "Q2", "Q3", "Q4"], plot.Series[0].Categories!); + Assert.Equal(Red, plot.Series[0].Style.Fill); + Assert.Equal(["Sales by quarter"], chart.Title!.Lines); + Assert.Equal("r", chart.Legend!.Position); + Assert.Equal([false, true], chart.Axes.Select(a => a.IsValue)); + Assert.Equal(["Units"], chart.Axes[1].Title!.Lines); + } + + [Fact] + public void Series_without_a_colour_take_the_theme_accents_in_Office_order() + { + var series = string.Concat(Enumerable.Range(0, 8).Select(i => Series(i, $"S{i}", ["a"], [i + 1]))); + var chart = Drawn(Chart(Bars(series), Axes())); + + Assert.Equal(new SKColor(0x44, 0x72, 0xC4), chart.Palette.Color(0)); + Assert.Equal(new SKColor(0xED, 0x7D, 0x31), chart.Palette.Color(1)); + Assert.Equal(new SKColor(0x70, 0xAD, 0x47), chart.Palette.Color(5)); + // The seventh is the first accent darker (60 % of its luminance). + chart.Palette.Color(6).ToHsl(out _, out _, out var dark); + chart.Palette.Color(0).ToHsl(out _, out _, out var light); + Assert.InRange(dark / light, 0.55, 0.65); + } + + [Theory] + [InlineData("bar3DChart")] + [InlineData("radarChart")] + [InlineData("stockChart")] + [InlineData("surfaceChart")] + public void Other_chart_types_are_named_instead(string kind) + { + var xml = Chart($"{Series(0, "S", ["a"], [1])}", Axes()); + Assert.Equal(new ChartRead(null, "charts"), Read(xml)); + } + + [Fact] + public void A_single_series_takes_its_name_as_title_until_the_title_is_deleted() + { + var xml = $"{Pie(Series(0, "Budget", ["a", "b"], [1, 2]))}"; + Assert.Equal(["Budget"], Drawn(xml).Title!.Lines); + Assert.Null(Drawn(xml.Replace("", "", StringComparison.Ordinal)).Title); + } + + // ───────────────────────── Drawing ───────────────────────── + + [Fact] + public void Columns_are_drawn_in_their_colours_as_high_as_their_values() + { + var (bitmap, page) = Picture(Columns()); + using (bitmap) + { + Assert.True(Count(bitmap, Red) > 500 && Count(bitmap, Green) > 500 && Count(bitmap, Blue) > 500); + // North's Q3 (70) stands higher than its Q1 (40): more red in the third quarter of the plot than the first. + var q1 = Count(bitmap, Red, 0.05, 0.3); + var q3 = Count(bitmap, Red, 0.45, 0.7); + Assert.InRange(q3 / (double)q1, 1.4, 2.1); + } + // Title, axis labels, axis titles and the legend are text in the page, not pixels. + var text = Text(page); + foreach (var expected in new[] { "Sales by quarter", "Q1", "Q4", "North", "West", "Units", "Quarter", "40" }) + Assert.Contains(expected, text); + } + + private const string RedLine = ""; + + [Fact] + public void Stacked_and_horizontal_bars_and_lines_areas_and_scatter_plots_are_drawn() + { + string[] q = ["Q1", "Q2", "Q3", "Q4"]; + foreach (var xml in new[] + { + Columns("stacked"), Columns("percentStacked"), Columns(direction: "bar"), + Chart(Lines(Series(0, "A", q, [1, 3, null, 2], RedLine)), Axes()), + Chart(Areas(Series(0, "A", q, [1, 3, 2, 2], Solid("C00000")), "stacked"), Axes()), + Chart(Scatter(ScatterSeries(0, "A", [1, 2, 4], [3, 1, 2], RedLine)), Axes(scatter: true)), + }) + { + var (bitmap, _) = Picture(xml); + using (bitmap) + Assert.True(Count(bitmap, Red) > 100, xml[xml.IndexOf("", StringComparison.Ordinal)..][..60]); + } + } + + [Fact] + public void Pie_slices_take_their_share_of_the_circle() + { + var (bitmap, page) = Picture(Slices("")); + using (bitmap) + { + var shares = new[] { new SKColor(0xFF, 0, 0), new SKColor(0, 0xC0, 0), new SKColor(0, 0, 0xFF), new SKColor(0xFF, 0xC0, 0) } + .Select(c => (double)Count(bitmap, c)).ToList(); + var total = shares.Sum(); + // 40 %, 30 %, 20 %, 10 % of the pie (labels and the legend's keys take a little). + Assert.InRange(shares[0] / total, 0.36, 0.44); + Assert.InRange(shares[3] / total, 0.07, 0.13); + } + var text = Text(page); + Assert.Contains("40%", text); + Assert.Contains("Travel", text); + } + + [Fact] + public void A_picture_of_a_chart_is_a_png() + { + var png = ChartPicture.Png(Drawn(Columns()), 240, 150, 96); + using var bitmap = SKBitmap.Decode(png); + Assert.Equal((320, 200), (bitmap.Width, bitmap.Height)); + } + + // ───────────────────────── Hostile input ───────────────────────── + + [Fact] + public void A_cache_that_claims_more_points_than_Filee_draws_is_refused() + { + var claimed = Columns().Replace("", "", StringComparison.Ordinal); + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => Read(claimed)).ErrorKey); + + var far = Columns().Replace("", "", StringComparison.Ordinal); + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => Read(far)).ErrorKey); + + var series = string.Concat(Enumerable.Range(0, ChartReader.MaxSeries + 1).Select(i => $"")); + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => Read(Chart(Bars(series), Axes()))).ErrorKey); + } + + [Fact] + public void Huge_values_long_texts_and_many_categories_are_drawn_in_time() + { + var categories = Enumerable.Range(0, 50_000).Select(i => new string('w', 200) + i).ToArray(); + var values = Enumerable.Range(0, 50_000).Select(i => (double?)(i % 3 == 0 ? 1e308 : i % 3 == 1 ? -1e308 : double.Epsilon)).ToArray(); + var labels = ""; + var xml = Chart(Lines(Series(0, new string('t', 100_000), categories, values, labels)), Axes(valueExtra: ""), + title: new string('x', 100_000)); + var watch = Stopwatch.StartNew(); + + var chart = Drawn(xml); + Assert.Equal(ChartReader.MaxText, chart.Title!.Lines[0].Length); + var (bitmap, _) = Picture(xml); + bitmap.Dispose(); + + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(60), $"took {watch.Elapsed}"); + } + + [Fact] + public void A_chart_placed_over_and_over_is_refused_by_the_workbook_budget() + { + var points = (int)(ChartBudget.MaxPoints / 20); + var big = Chart(Lines(Series(0, "S", [], [.. Enumerable.Range(0, points).Select(i => (double?)i)])), Axes()); + var anchors = string.Concat(Enumerable.Repeat(SheetAnchor(), 21)); + var path = Save(Workbook(anchors, big)); + + Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => XlsxReader.Read(path)).ErrorKey); + } + + // ───────────────────────── Sheets ───────────────────────── + + private const string Xdr = "xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\""; + + /// A chart from B2 to I20, as Excel anchors one. + private static string SheetAnchor() => + "1010" + + "80190" + + "" + + $"" + + $""; + + private static XlsxBuilder Workbook(string anchors, string chart) + { + var book = new XlsxBuilder(); + book.Sheet("Data", XlsxBuilder.Row(1, book.Text("A1", "Sales")), after: ""); + book.SheetRelationships[1] = [("rIdD", "drawing", "../drawings/drawing1.xml")]; + book.Parts["xl/drawings/drawing1.xml"] = $"{anchors}"; + book.Parts["xl/drawings/_rels/drawing1.xml.rels"] = OpcWriter.Relationships([("rIdC", "chart", "../charts/chart1.xml")]); + book.Parts["xl/charts/chart1.xml"] = chart; + return book; + } + + private string Save(XlsxBuilder book) => book.Save(Path.Combine(fx.NewFolder(), "book.xlsx")); + + [Fact] + public void A_workbook_with_a_chart_stays_on_the_sheet_printer_and_prints_it() + { + var path = Save(Workbook(SheetAnchor(), Columns())); + Assert.Null(SheetCapabilities.FirstUnsupported(path, "xlsx")); + + var book = XlsxReader.Read(path); + Assert.IsType(Assert.Single(book.Sheets[0].Drawings)); + using var printed = SheetPrinter.Print(book, new SheetPrintSource("book.xlsx", "C:\\"), DocumentLimits.Default, DateTime.Now, null, TestContext.Current.CancellationToken); + using var page = printed.Pages[0].Render(72); + Assert.True(Count(page, Red) > 300 && Count(page, Blue) > 300); + Assert.Contains("Sales by quarter", Text(printed.Pages[0])); + } + + [Fact] + public void A_workbook_with_a_3D_chart_is_still_named() + { + var xml = Chart($"{Series(0, "S", ["a"], [1])}", Axes()); + Assert.Equal("charts", SheetCapabilities.FirstUnsupported(Save(Workbook(SheetAnchor(), xml)), "xlsx")); + } + + // ───────────────────────── Word ───────────────────────── + + private string WordWithChart(string chart) + { + var docx = new DocxBuilder(); + var id = docx.Chart(chart); + docx.Paragraph(DocxBuilder.P(DocxBuilder.R("Before the chart."))); + docx.Paragraph(DocxBuilder.P(InlineChart(id, 432 * 12700, 252 * 12700))); + return docx.Save(Path.Combine(fx.NewFolder(), "chart.docx")); + } + + [Fact] + public void A_Word_chart_is_drawn_by_the_renderer_and_a_picture_of_it_goes_to_the_writers() + { + var path = WordWithChart(Columns()); + Assert.Null(new FileeRendererConverter().FirstUnsupported(path, "docx")); + + var document = DocxReader.Read(path, Path.Combine(fx.NewFolder(), "media")); + Assert.True(document.LeftOut.IsEmpty); + var picture = document.Sections.SelectMany(s => s.Blocks).OfType().SelectMany(p => p.Inlines).OfType().Single(); + Assert.NotNull(picture.Chart); + using (var png = SKBitmap.Decode(picture.Path)) + Assert.True(Count(png, Red) > 300); + + using var page = SKBitmap.Decode(FileeRenderer.ToImages(document, 72, cancellationToken: TestContext.Current.CancellationToken).First()); + Assert.True(Count(page, Red) > 300 && Count(page, Green) > 300); + } + + [Fact] + public void A_Word_chart_Filee_doesnt_draw_is_named_and_left_out() + { + var xml = Chart($"{Series(0, "S", ["a"], [1])}", Axes()); + var path = WordWithChart(xml); + Assert.Equal("OLE objects and charts", new FileeRendererConverter().FirstUnsupported(path, "docx")); + + var document = DocxReader.Read(path, Path.Combine(fx.NewFolder(), "media")); + Assert.Contains(document.LeftOut.Kinds, k => k.Kind == HLeftOutKind.Chart); + } + + // ───────────────────────── Slides ───────────────────────── + + private string DeckWithChart(string chart) + { + var deck = new SlideDeckBuilder(); + deck.Part("ppt/charts/chart1.xml", chart, "drawingml.chart"); + var frame = "" + + $"" + + $""; + deck.Slide(frame, relationships: [("rIdC", "chart", "../charts/chart1.xml")]); + return deck.Save(Path.Combine(fx.NewFolder(), "deck.pptx")); + } + + [Fact] + public void A_slide_chart_is_drawn_and_its_picture_made_for_what_takes_pictures() + { + var path = DeckWithChart(Columns()); + Assert.Null(new FileeRendererConverter().FirstUnsupported(path, "pptx")); + + var deck = PptxSlideReader.Read(path, cancellationToken: TestContext.Current.CancellationToken); + var shape = Assert.Single(deck.Slides[0].Elements.OfType(), s => s.Chart is not null); + Assert.NotNull(shape.Chart); + using (var bitmap = Assert.Single(SlideOutput.ToBitmaps(deck, 72, cancellationToken: TestContext.Current.CancellationToken))) + Assert.True(Count(bitmap, Red) > 500 && Count(bitmap, Blue) > 500); + // The ODP writer and the deck's text take the picture. + using var picture = SKBitmap.Decode(Assert.IsType(shape.Fill).Image.Data); + Assert.True(Count(picture, Red) > 500); + } + + [Fact] + public void A_slide_chart_Filee_doesnt_draw_is_named() + { + var xml = Chart($"{Series(0, "S", ["a"], [1])}"); + Assert.Equal("charts", new FileeRendererConverter().FirstUnsupported(DeckWithChart(xml), "pptx")); + } + + // ───────────────────────── Text ───────────────────────── + + [Fact] + public void Chart_text_in_other_scripts_is_drawn_as_text() + { + var xml = Chart(Bars(Series(0, "매출", ["一月", "二月"], [1, 2])), Axes(), "분기별 매출"); + var (bitmap, page) = Picture(xml); + bitmap.Dispose(); + var text = Text(page); + Assert.Contains("분기별 매출", text); + Assert.Contains("一月", text); + } +} diff --git a/tests/Filee.Engines.Tests/DocxBuilder.cs b/tests/Filee.Engines.Tests/DocxBuilder.cs index c3af4dd08..bc7163d8b 100644 --- a/tests/Filee.Engines.Tests/DocxBuilder.cs +++ b/tests/Filee.Engines.Tests/DocxBuilder.cs @@ -66,6 +66,14 @@ public string Image(byte[] png) return Relationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", name); } + /// Adds a chart part (a c:chartSpace) and returns its relationship id. + public string Chart(string xml) + { + var name = $"charts/chart{_parts.Keys.Count(k => k.StartsWith("charts/", StringComparison.Ordinal)) + 1}.xml"; + _parts[name] = xml; + return Relationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart", name); + } + public string Hyperlink(string url) => Relationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", url, external: true); @@ -148,6 +156,7 @@ private string Relationship(string type, string target, bool external = false) "footnotes.xml" => "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", "endnotes.xml" => "application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml", _ when part.StartsWith("header", StringComparison.Ordinal) => "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", + _ when part.StartsWith("charts/", StringComparison.Ordinal) => "application/vnd.openxmlformats-officedocument.drawingml.chart+xml", _ => "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", }; From 1d1c1eb34232d15b18112230d41cd4fdc650afdd Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 00:40:46 +0900 Subject: [PATCH 5/6] Draw TeX primes after their base and keep unknown commands as written - f', f'', f^\prime: MathLayout draws a superscript that is only primes at the base's size on its baseline after the base (the prime character is a raised mark already), instead of shrinking and raising it over an italic letter. Word math and MathML keep it as a superscript. - An unknown TeX command keeps its [ ] and { } arguments literally (\foo{x}, not \foo followed by x); brackets alone stay the formula's. Co-Authored-By: Claude Opus 5.5 --- src/Filee.Engines/Hwp/Hwpx/LatexMath.cs | 33 +++++++++++-- src/Filee.Engines/Typesetting/MathLayout.cs | 22 +++++++-- .../Filee.Engines.Tests/MarkdownMathTests.cs | 49 ++++++++++++++++++- 3 files changed, 95 insertions(+), 9 deletions(-) diff --git a/src/Filee.Engines/Hwp/Hwpx/LatexMath.cs b/src/Filee.Engines/Hwp/Hwpx/LatexMath.cs index 5d1028853..f9b766120 100644 --- a/src/Filee.Engines/Hwp/Hwpx/LatexMath.cs +++ b/src/Filee.Engines/Hwp/Hwpx/LatexMath.cs @@ -11,7 +11,8 @@ // \ddot, \overline, \underline, \widehat …); \text, \mathrm, \mathbf, \mathit, \boldsymbol, \mathbb, \mathcal, // \mathfrak and the old switches \rm, \bf, \it; spacing (\, \: \; \quad \qquad ~ \hspace); \not; \tag. Commands for // layout only (\displaystyle, \limits, \label, \color …) are skipped. A command this reader doesn't know is kept as its -// text ("\foo"), never dropped, and its arguments follow it as groups. +// text with its arguments as written ("\foo{x}"), never dropped. Primes (f', f'', f^\prime) are a superscript prime +// on the item before, which MathLayout draws after it at full size. // // Bounds: a formula longer than MaxLength characters, or one that nests deeper than MaxDepth (groups, fractions, // scripts, brackets, environments), is not read: Parse returns null and the caller keeps the source as text, as @@ -768,8 +769,34 @@ private string CommandName() _tags.Add(RawArgument()); return null; } - // Not known: kept as its text; arguments it takes follow as groups. - return new MathQuoted("\\" + name); + // Not known: kept as its text, with the [ … ] and { … } arguments right after it as written ("\foo{x}"). + return new MathQuoted("\\" + name + LiteralArguments()); + } + + /// The optional and braced arguments at the position, as their source text (consumed). + private string LiteralArguments() + { + var start = _i; + var end = _i; + while (true) + { + var at = _i; + SkipSpace(); + // [ … ] only before a group: "\foo [0, 1]" alone is an interval, not an argument. + if (_i < s.Length && s[_i] == '[' && s.IndexOf(']', _i) is var close and > 0 + && s.AsSpan(close + 1).TrimStart().StartsWith("{")) + _i = close + 1; + else if (_i < s.Length && s[_i] == '{') + RawArgument(); + else + { + _i = at; + break; + } + end = _i; + } + _i = end; + return s[start..end].Trim(); } private static MathNode Upright(string name) => new MathStyled("rm", new MathRow([.. name.Select(c => (MathNode)new MathText(c.ToString()))])); diff --git a/src/Filee.Engines/Typesetting/MathLayout.cs b/src/Filee.Engines/Typesetting/MathLayout.cs index 2b029ff28..5f7ee708c 100644 --- a/src/Filee.Engines/Typesetting/MathLayout.cs +++ b/src/Filee.Engines/Typesetting/MathLayout.cs @@ -455,18 +455,22 @@ private EquationBox Scripts(EquationBox body, MathNode? subNode, MathNode? supNo { var size = style.Size; var small = style.Smaller(ScriptScale); - var sup = supNode is null ? null : Lay(supNode, small); + // A prime (f′, TeX's f' = f^\prime) is already a raised mark: drawn at the base's size on its baseline, after + // the base (clear of an italic letter's overhang), not shrunk and raised again. + var prime = supNode is not null && IsPrimes(supNode); + var sup = supNode is null ? null : Lay(supNode, prime ? style : small); var sub = subNode is null ? null : Lay(subNode, small); var parts = new List<(float, float, EquationBox)> { (0, 0, body) }; - var supShift = sup is null ? 0 : Math.Max(size * 0.42f, body.Ascent - sup.Ascent * 0.6f); + var supShift = sup is null || prime ? 0 : Math.Max(size * 0.42f, body.Ascent - sup.Ascent * 0.6f); var subShift = sub is null ? 0 : Math.Max(size * 0.18f, body.Descent - sub.Descent * 0.2f); - if (sup is not null && sub is not null && supShift - sup.Descent - (sub.Ascent - subShift) < size * 0.15f) + if (sup is not null && sub is not null && !prime && supShift - sup.Descent - (sub.Ascent - subShift) < size * 0.15f) subShift += size * 0.15f - (supShift - sup.Descent - (sub.Ascent - subShift)); var width = body.Width; if (sup is not null) { - parts.Add((body.Width + (italicBase ? size * 0.03f : 0), -supShift, sup)); - width = Math.Max(width, body.Width + sup.Width); + var x = body.Width + (italicBase ? size * (prime ? 0.08f : 0.03f) : 0); + parts.Add((x, -supShift, sup)); + width = Math.Max(width, x + sup.Width); } if (sub is not null) { @@ -479,6 +483,14 @@ private EquationBox Scripts(EquationBox body, MathNode? subNode, MathNode? supNo return Place(width, ascent, descent, [.. parts]); } + /// One or more primes (′ ″ ‴) and nothing else. + private static bool IsPrimes(MathNode node) => node switch + { + MathCommand { Glyph: "′" or "″" or "‴" } or MathText { Text: "′" or "″" or "‴" } or MathSymbol { Text: "′" or "″" or "‴" } => true, + MathRow { Items.Count: > 0 } row => row.Items.All(IsPrimes), + _ => false, + }; + /// with centred above and below. private static EquationBox Limits(EquationBox body, EquationBox? upper, EquationBox? lower, float gap) { diff --git a/tests/Filee.Engines.Tests/MarkdownMathTests.cs b/tests/Filee.Engines.Tests/MarkdownMathTests.cs index 9a3063d4b..6648d57e9 100644 --- a/tests/Filee.Engines.Tests/MarkdownMathTests.cs +++ b/tests/Filee.Engines.Tests/MarkdownMathTests.cs @@ -45,6 +45,8 @@ public sealed partial class MarkdownMathTests(EngineFixture fx) : IClassFixture< [InlineData(@"\binom{n}{k}", "LEFT ( {{n} atop {k}} RIGHT )")] [InlineData(@"x \in \mathbb{R}", "x IN ℝ")] [InlineData(@"f'(x)", "f ^{prime} ( x )")] + [InlineData(@"f''(x) + g'''", "f ^{prime prime} ( x ) + g ^{prime prime prime}")] + [InlineData(@"f^\prime + f'_i + f'^2", "f ^{prime} + f _{i} ^{prime} + f ^{prime 2}")] [InlineData(@"\{x\}", "LBRACE x RBRACE")] [InlineData(@"\displaystyle a \over b", "{{a} over {b}}")] [InlineData(@"e^{i\pi} + 1 = 0", "e ^{i pi} + 1 = 0")] @@ -54,7 +56,9 @@ public void Tex_reads_into_the_equation_tree(string tex, string script) => Assert.Equal(script, EquationScript.Script(LatexMath.Parse(tex)!)); [Theory] - [InlineData(@"\foo{x} + 1", "\"\\foo\" x + 1")] + [InlineData(@"\foo{x} + 1", "\"\\foo{x}\" + 1")] + [InlineData(@"\foo[a]{x}{y}", "\"\\foo[a]{x}{y}\"")] + [InlineData(@"\foo [0, 1]", "\"\\foo\" [ 0 , 1 ]")] // brackets alone are the formula's, not an argument [InlineData(@"\begin{weird} a & b \end{weird}", "\"\\begin{weird}\" {a b} \"\\end{weird}\"")] public void An_unknown_command_is_kept_as_its_text(string tex, string script) => Assert.Equal(script, EquationScript.Script(LatexMath.Parse(tex)!)); @@ -256,6 +260,49 @@ public void The_page_typesets_the_math_instead_of_printing_its_source() Assert.True(pages.SelectMany(p => p.Items).Count(i => i is not GlyphItem) >= 5); } + [Theory] + [InlineData(@"f'(x)", "′")] + [InlineData(@"f''", "′′")] + [InlineData(@"f^\prime", "′")] + public void A_prime_is_drawn_after_its_base_at_full_size(string tex, string primes) + { + var fonts = RepositoryFonts.Load(); + Assert.SkipUnless(fonts.Has(RepositoryFonts.Latin), "Noto Sans is not in src/Filee.App/Assets/Fonts (build/fetch-fonts.ps1)"); + using var shaper = new Shaper(); + const float size = 20; + var page = new PageContent(400, 200); + + new MathLayout(new FontResolver(fonts), shaper).Layout(LatexMath.Parse(tex)!, size, SkiaSharp.SKColors.Black).Paint(page, 10, 100, 1); + + var runs = page.Items.OfType().Select(i => i.Run).ToList(); + var f = runs.First(r => r.Text == "f"); + var prime = runs.Where(r => r.Text.All(c => c == '′')).ToList(); + Assert.Equal(primes, string.Concat(prime.Select(r => r.Text))); + Assert.All(prime, r => + { + Assert.Equal(size, r.Size); // not shrunk to a script's size + Assert.True(r.Positions[0].X >= f.Positions[0].X + size * 0.3f, $"prime at {r.Positions[0].X}, f at {f.Positions[0].X}"); + Assert.True(Math.Abs(r.Positions[0].Y - f.Positions[0].Y) < size * 0.05f, $"prime baseline {r.Positions[0].Y}, f {f.Positions[0].Y}"); + }); + } + + [Fact] + public void A_prime_is_a_superscript_after_its_base_in_word_math_and_mathml() + { + var tree = LatexMath.Parse("f'(x)")!; + + XNamespace m = "http://schemas.openxmlformats.org/officeDocument/2006/math"; + var omml = XElement.Parse($"{OmmlWriter.Write(tree, display: false)}"); + var sup = Assert.Single(omml.Descendants(m + "sSup")); + Assert.Equal("f", sup.Element(m + "e")!.Value); + Assert.Equal("′", sup.Element(m + "sup")!.Value); + Assert.Equal("(", ((XElement)sup.NextNode!).Value); // the bracket follows the scripted f + + XNamespace mml = "http://www.w3.org/1998/Math/MathML"; + var msup = Assert.Single(XElement.Parse(MathMLWriter.Write(tree, display: false)).Descendants(mml + "msup")); + Assert.Equal(["f", "′"], msup.Elements().Select(e => e.Value)); + } + // ───────────────────────── Bounds ───────────────────────── [Fact] From 24d32cd71cca641481a2d8364cceae20d7e31fd0 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Fri, 20 Nov 2026 01:26:50 +0900 Subject: [PATCH 6/6] perf(archives): faster bzip2 suffix sorting, folded xz CRC-64, earlier chain prefetch - bzip2: SA-IS rewritten to read less per step: no suffix-type array (types follow from two characters and the scan, a bit per position marks LMS ones), entries carry by their sign whether the scan has a suffix to place from them, LMS substrings named with their lengths in the unused half of the array and compared in one go. About 13 % less sorting time, same transform. - bzip2: the smallest-rotation race also tells whether the block is periodic, so the prefix function only runs for blocks that are. - xz: CRC-64 folds 16 bytes at a time with PCLMULQDQ on x64 (constants worked out from the polynomial at start-up); tables elsewhere and for short input. - LZMA fast level: the chain's first candidate (link and bytes) is asked into the cache before the two- and three-byte candidates are checked. Same bytes. - Tests: large blocks against rotations sorted by doubling, smallest rotation and periodicity against a plain search, CRC-64 folding against the tables. Co-Authored-By: Claude Opus 5.5 --- src/Filee.Engines/Archives/Bzip2Sort.cs | 211 +++++++++++------- src/Filee.Engines/Archives/Lzma/Crc64.cs | 78 ++++++- .../Archives/Lzma/MatchFinder.cs | 17 ++ tests/Filee.Engines.Tests/Bzip2WriterTests.cs | 87 ++++++++ tests/Filee.Engines.Tests/LzmaTests.cs | 33 +++ 5 files changed, 338 insertions(+), 88 deletions(-) diff --git a/src/Filee.Engines/Archives/Bzip2Sort.cs b/src/Filee.Engines/Archives/Bzip2Sort.cs index 796d5f9fb..41672b7ee 100644 --- a/src/Filee.Engines/Archives/Bzip2Sort.cs +++ b/src/Filee.Engines/Archives/Bzip2Sort.cs @@ -24,14 +24,15 @@ public static int Transform(ReadOnlySpan block, Span output, Cancell block.CopyTo(output); return 0; } - var shift = SmallestRotation(block); + var shift = SmallestRotation(block, out var periodic); var word = ArrayPool.Shared.Rent(n); var sa = ArrayPool.Shared.Rent(n); try { block[shift..].CopyTo(word); block[..shift].CopyTo(word.AsSpan(n - shift)); - var root = RootLength(word.AsSpan(0, n), sa); + // Only a block that equals one of its other rotations has a shorter root (and needs the prefix function). + var root = periodic ? RootLength(word.AsSpan(0, n), sa) : n; var repeats = n / root; SuffixArray(new ByteText(word), root, 256, sa, cancellationToken); @@ -60,8 +61,16 @@ public static int Transform(ReadOnlySpan block, Span output, Cancell } } - /// Where the lexicographically smallest rotation of starts (two candidates race). - internal static int SmallestRotation(ReadOnlySpan text) + /// + /// Where the lexicographically smallest rotation of starts (two candidates race), and whether + /// equals another of its rotations (is a power of a shorter piece). + /// + /// + /// A candidate is only dropped, with the k places after it, when a rotation there is larger than another, so a + /// smallest rotation is never dropped. A periodic text has two or more: the candidates end up on two of them and + /// match all the way round. A text with one runs out of candidates before. + /// + internal static int SmallestRotation(ReadOnlySpan text, out bool periodic) { var n = text.Length; int i = 0, j = 1, k = 0; @@ -83,6 +92,7 @@ internal static int SmallestRotation(ReadOnlySpan text) j++; k = 0; } + periodic = k >= n; return Math.Min(i, j); } @@ -111,22 +121,37 @@ private static int RootLength(ReadOnlySpan text, int[] scratch) private interface IText { int this[int index] { get; } + + /// Whether the characters at and are the same. + bool Same(int a, int b, int length); } private readonly struct ByteText(byte[] text) : IText { public int this[int index] => text[index]; + + public bool Same(int a, int b, int length) => text.AsSpan(a, length).SequenceEqual(text.AsSpan(b, length)); } private readonly struct IntText(int[] text) : IText { public int this[int index] => text[index]; + + public bool Same(int a, int b, int length) => text.AsSpan(a, length).SequenceEqual(text.AsSpan(b, length)); } /// /// The suffix array of the first characters of (values below /// ), with an implied end character smaller than all others, into . /// + /// + /// SA-IS, written so that each step reads as little as it can: + /// - No array of suffix types: a suffix's type follows from its first two characters and the scan that places it + /// (), and a bit per position marks the LMS ones. + /// - LMS substrings are named by comparing their characters in one go, their lengths kept in the unused half of + /// the suffix array. + /// The string of names is sorted the same way when names repeat; its alphabet is at most half the size. + /// private static void SuffixArray(T text, int n, int alphabet, int[] sa, CancellationToken cancellationToken) where T : struct, IText { @@ -143,29 +168,19 @@ private static void SuffixArray(T text, int n, int alphabet, int[] sa, Cancel sa[1] = firstSmaller ? 1 : 0; return; } + ArgumentOutOfRangeException.ThrowIfGreaterThan(n, sa.Length); cancellationToken.ThrowIfCancellationRequested(); - // S-type: the suffix is smaller than the one after it. The last character is L-type (the end is smaller). - var stype = ArrayPool.Shared.Rent(n); var starts = ArrayPool.Shared.Rent(alphabet); var ends = ArrayPool.Shared.Rent(alphabet); var fill = ArrayPool.Shared.Rent(alphabet); - // Leftmost S-type positions (LMS) are never next to each other, so position / 2 indexes them. - var lmsIndex = ArrayPool.Shared.Rent(n / 2 + 1); var lms = ArrayPool.Shared.Rent(n / 2 + 1); + var isLms = ArrayPool.Shared.Rent((n >> 6) + 1); int[]? sorted = null; int[]? names = null; int[]? reducedSa = null; try { - stype[n - 1] = false; - for (var i = n - 2; i >= 0; i--) - { - var a = text[i]; - var b = text[i + 1]; - stype[i] = a < b || a == b && stype[i + 1]; - } - Array.Clear(starts, 0, alphabet); for (var i = 0; i < n; i++) starts[text[i]]++; @@ -178,123 +193,157 @@ private static void SuffixArray(T text, int n, int alphabet, int[] sa, Cancel ends[c] = sum; } + // LMS positions (an S-type suffix after an L-type one; the last suffix is L-type), found right to left. + // S-type: smaller than the suffix after it, so the first character is smaller, or equal and that one S-type. + Array.Clear(isLms, 0, (n >> 6) + 1); var m = 0; - for (var i = 1; i < n; i++) + var nextS = 0; + var next = text[n - 1]; + for (var i = n - 2; i >= 0; i--) { - if (stype[i] && !stype[i - 1]) - { - lmsIndex[i >> 1] = m; - lms[m++] = i; - } + var c = text[i]; + var s = c < next ? 1 : c == next ? nextS : 0; + // Written every time and counted only for an LMS position: no branch to mispredict. + var lmsHere = nextS & (s ^ 1); + lms[m] = i + 1; + m += lmsHere; + isLms[(i + 1) >> 6] |= (ulong)lmsHere << (i + 1); + nextS = s; + next = c; } - - // First pass: the LMS positions in text order; inducing sorts them by their LMS substrings. - Induce(text, n, alphabet, stype, starts, ends, fill, lms.AsSpan(0, m), sa); if (m == 0) + { + // No S-type suffix: the characters never rise, and the L-type ones induce from the last suffix alone. + Array.Clear(sa, 0, n); + Induce(text, n, sa, starts, ends, fill, alphabet); return; + } + lms.AsSpan(0, m).Reverse(); + + // First pass: the LMS positions at the ends of their buckets; inducing sorts them by their LMS substrings. + Array.Clear(sa, 0, n); + Array.Copy(ends, fill, alphabet); + for (var k = m - 1; k >= 0; k--) + sa[--fill[text[lms[k]]]] = lms[k]; + Induce(text, n, sa, starts, ends, fill, alphabet); cancellationToken.ThrowIfCancellationRequested(); - sorted = ArrayPool.Shared.Rent(m); - var k = 0; + // The LMS positions in that order, to the front. + var found = 0; for (var i = 0; i < n; i++) { var p = sa[i]; - if (p > 0 && stype[p] && !stype[p - 1]) - sorted[k++] = p; + sa[found] = p; + found += (int)(isLms[p >> 6] >> p) & 1; } - // Name the LMS substrings (from one LMS position to the next, both included) by their sorted order. - names = ArrayPool.Shared.Rent(m); + // Name the LMS substrings (from one LMS position to the next, both included) in that order. Each one's + // length, then its name, goes to sa[m + p / 2] (LMS positions are never next to each other); the last one + // runs into the end, so it is unlike any other (length -1). + Array.Clear(sa, m, n - m); + for (var k = 0; k < m; k++) + sa[m + (lms[k] >> 1)] = k + 1 < m ? lms[k + 1] - lms[k] + 1 : -1; var name = 0; - names[lmsIndex[sorted[0] >> 1]] = 0; - for (var i = 1; i < m; i++) + int previous = -1, previousLength = 0; + for (var i = 0; i < m; i++) { - if (!SameLmsSubstring(text, n, lms, m, lmsIndex, sorted[i - 1], sorted[i])) + var p = sa[i]; + var length = sa[m + (p >> 1)]; + if (length < 0 || length != previousLength || !text.Same(p, previous, length)) name++; - names[lmsIndex[sorted[i] >> 1]] = name; + sa[m + (p >> 1)] = name; // 1 and up; 0 is a gap + previous = p; + previousLength = length; } - if (name + 1 < m) + sorted = ArrayPool.Shared.Rent(m); + if (name < m) { // Some substrings repeat: sort the string of names (in text order) to order the LMS suffixes. + names = ArrayPool.Shared.Rent(m); + var k = 0; + for (var i = m; i < n && k < m; i++) + { + if (sa[i] != 0) + names[k++] = sa[i] - 1; + } reducedSa = ArrayPool.Shared.Rent(m); - SuffixArray(new IntText(names), m, name + 1, reducedSa, cancellationToken); + SuffixArray(new IntText(names), m, name, reducedSa, cancellationToken); for (var i = 0; i < m; i++) sorted[i] = lms[reducedSa[i]]; } else { - for (var i = 0; i < m; i++) - sorted[names[i]] = lms[i]; + for (var k = 0; k < m; k++) + sorted[sa[m + (lms[k] >> 1)] - 1] = lms[k]; } - Induce(text, n, alphabet, stype, starts, ends, fill, sorted.AsSpan(0, m), sa); + // Second pass: the sorted LMS suffixes at the ends of their buckets, the rest induced from them. + Array.Clear(sa, 0, n); + Array.Copy(ends, fill, alphabet); + for (var k = m - 1; k >= 0; k--) + sa[--fill[text[sorted[k]]]] = sorted[k]; + Induce(text, n, sa, starts, ends, fill, alphabet); } finally { if (reducedSa is not null) ArrayPool.Shared.Return(reducedSa); if (names is not null) ArrayPool.Shared.Return(names); if (sorted is not null) ArrayPool.Shared.Return(sorted); + ArrayPool.Shared.Return(isLms); ArrayPool.Shared.Return(lms); - ArrayPool.Shared.Return(lmsIndex); ArrayPool.Shared.Return(fill); ArrayPool.Shared.Return(ends); ArrayPool.Shared.Return(starts); - ArrayPool.Shared.Return(stype); } } /// - /// Induced sorting: the LMS positions go to the ends of their buckets in the given order, the L-type suffixes are - /// placed from them left to right, then the S-type ones right to left. + /// Induced sorting from the LMS suffixes placed at the ends of their buckets (the rest of + /// zero): L-type suffixes left to right from the bucket starts, then S-type ones right to left from the bucket ends, + /// which places the LMS ones again. /// - private static void Induce(T text, int n, int alphabet, bool[] stype, int[] starts, int[] ends, int[] fill, - ReadOnlySpan lms, int[] sa) + /// + /// An entry placed as p means that the scan reaching it places p − 1; ~p (below zero) means it has nothing to place + /// in that scan. Which it is follows from two characters: in the L scan, p − 1 is L-type when its character is not + /// below p's (p being L-type, or LMS, whose character is below the one before it); in the S scan, p − 1 is S-type + /// when its character is not above p's (p being S-type). The L scan turns over every entry it passes: an L-type + /// suffix whose predecessor is L-type has nothing to place in the S scan, one whose predecessor is S-type has. The + /// S scan leaves every entry as the plain position. 0 is both a gap and suffix 0, which never places anything. + /// + private static void Induce(T text, int n, int[] sa, int[] starts, int[] ends, int[] fill, int alphabet) where T : struct, IText { - Array.Fill(sa, -1, 0, n); - Array.Copy(ends, fill, alphabet); - for (var i = lms.Length - 1; i >= 0; i--) - { - var p = lms[i]; - sa[--fill[text[p]]] = p; - } - Array.Copy(starts, fill, alphabet); - // The suffix before the end: the last character, L-type. - sa[fill[text[n - 1]]++] = n - 1; + // The last suffix, L-type (the end comes after it), first in its bucket. + var last = n - 1; + var c = text[last]; + sa[fill[c]++] = text[last - 1] >= c ? last : ~last; for (var i = 0; i < n; i++) { - var p = sa[i] - 1; - if (p >= 0 && !stype[p]) - sa[fill[text[p]]++] = p; + var v = sa[i]; + sa[i] = ~v; + if (v <= 0) + continue; + var j = v - 1; + c = text[j]; + sa[fill[c]++] = j > 0 && text[j - 1] >= c ? j : ~j; } Array.Copy(ends, fill, alphabet); for (var i = n - 1; i >= 0; i--) { - var p = sa[i] - 1; - if (p >= 0 && stype[p]) - sa[--fill[text[p]]] = p; - } - } - - /// Whether the LMS substrings at and are equal. - private static bool SameLmsSubstring(T text, int n, int[] lms, int m, int[] lmsIndex, int a, int b) - where T : struct, IText - { - var nextA = lmsIndex[a >> 1] + 1; - var nextB = lmsIndex[b >> 1] + 1; - var endA = nextA < m ? lms[nextA] : n; - var endB = nextB < m ? lms[nextB] : n; - // The substring that runs into the end is unlike any other. - if (endA == n || endB == n || endA - a != endB - b) - return false; - for (; a <= endA; a++, b++) - { - if (text[a] != text[b]) - return false; + var v = sa[i]; + if (v > 0) + { + var j = v - 1; + c = text[j]; + sa[--fill[c]] = j > 0 && text[j - 1] <= c ? j : ~j; + } + else if (v < 0) + { + sa[i] = ~v; + } } - return true; } } diff --git a/src/Filee.Engines/Archives/Lzma/Crc64.cs b/src/Filee.Engines/Archives/Lzma/Crc64.cs index 2f9288e96..94c887d52 100644 --- a/src/Filee.Engines/Archives/Lzma/Crc64.cs +++ b/src/Filee.Engines/Archives/Lzma/Crc64.cs @@ -1,5 +1,20 @@ // CRC-64 as the .xz format uses it (ECMA-182 polynomial, reflected: 0xC96C5795D7870F42, initial and final value all // ones). System.IO.Hashing's Crc64 is the unreflected variant, so it does not give the same value. +// +// Long inputs are folded 16 bytes at a time with the processor's carry-less multiply (x64 PCLMULQDQ), the way CRCs are +// usually computed at speed: in the reflected bit order a 16-byte piece stands for a polynomial of degree below 128 +// (the first byte's lowest bit the highest power), and the piece so far times x^128, plus the next piece, is the same +// modulo the polynomial as its low half times (x^192 mod P) and its high half times (x^128 mod P), plus the next +// piece. A carry-less product of two reflected 64-bit values comes out one place short of the 128-bit order, so the +// constants used are those powers divided by x: x^191 and x^127 mod P. What is left at the end (one 16-byte piece, +// whose CRC from a zero register is the folded state's, and the last 0 … 15 bytes) goes through the tables. The +// constants are worked out once from the polynomial. Elsewhere (and for short inputs) the tables do all of it. + +using System.Buffers.Binary; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; namespace Filee.Engines.Archives.Lzma; @@ -8,9 +23,15 @@ internal sealed class Crc64 { private const ulong Polynomial = 0xC96C5795D7870F42; + /// Inputs shorter than this go through the tables only. + private const int FoldFrom = 64; + // Four tables, so four bytes are folded in per step. private static readonly ulong[][] Tables = CreateTables(); + // x^191 mod P (for the low half) and x^127 mod P (for the high half), reflected. + private static readonly Vector128 FoldConstants = Vector128.Create(PowerOfX(191), PowerOfX(127)); + private ulong _crc = ulong.MaxValue; /// The checksum of everything passed to so far. @@ -19,26 +40,69 @@ internal sealed class Crc64 public void Append(ReadOnlySpan data) { var crc = _crc; + if (data.Length >= FoldFrom && Pclmulqdq.IsSupported) + { + var whole = data.Length & ~15; + crc = Fold(crc, data[..whole]); + data = data[whole..]; + } + _crc = Update(crc, data); + } + + /// CRC-64 of . + public static ulong Compute(ReadOnlySpan data) + { + var crc = new Crc64(); + crc.Append(data); + return crc.Value; + } + + /// The register after (a multiple of 16 bytes, 32 or more), by folding. + private static ulong Fold(ulong crc, ReadOnlySpan data) + { + ref var at = ref MemoryMarshal.GetReference(data); + // The register so far goes into the first eight bytes (the same as starting from zero with them changed). + var state = Vector128.LoadUnsafe(ref at) ^ Vector128.CreateScalar(crc).AsByte(); + var constants = FoldConstants; + for (var offset = 16; offset < data.Length; offset += 16) + { + var low = Pclmulqdq.CarrylessMultiply(state.AsUInt64(), constants, 0x00); + var high = Pclmulqdq.CarrylessMultiply(state.AsUInt64(), constants, 0x11); + state = (low ^ high).AsByte() ^ Vector128.LoadUnsafe(ref at, (nuint)offset); + } + Span last = stackalloc byte[16]; + state.CopyTo(last); + return Update(0, last); + } + + /// The register after , four bytes per step through the tables. + private static ulong Update(ulong crc, ReadOnlySpan data) + { var (t0, t1, t2, t3) = (Tables[0], Tables[1], Tables[2], Tables[3]); var i = 0; for (; i + 4 <= data.Length; i += 4) { - var word = (uint)crc ^ (data[i] | (uint)data[i + 1] << 8 | (uint)data[i + 2] << 16 | (uint)data[i + 3] << 24); + var word = (uint)crc ^ BinaryPrimitives.ReadUInt32LittleEndian(data[i..]); crc = t3[word & 0xFF] ^ t2[(word >> 8) & 0xFF] ^ t1[(word >> 16) & 0xFF] ^ t0[word >> 24] ^ (crc >> 32); } for (; i < data.Length; i++) crc = t0[(crc ^ data[i]) & 0xFF] ^ (crc >> 8); - _crc = crc; + return crc; } - /// CRC-64 of . - public static ulong Compute(ReadOnlySpan data) + /// x^ mod P, reflected (bit i stands for x^(63 − i)). + private static ulong PowerOfX(int n) { - var crc = new Crc64(); - crc.Append(data); - return crc.Value; + var value = 1UL << 63; // x^0 + for (var i = 0; i < n; i++) + value = MultiplyByX(value); + return value; } + /// Times x, mod P, in the reflected order: x^63 (bit 0) becomes x^64, which is P less its top term. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong MultiplyByX(ulong value) => (value >> 1) ^ ((value & 1) != 0 ? Polynomial : 0); + private static ulong[][] CreateTables() { var tables = new ulong[4][]; diff --git a/src/Filee.Engines/Archives/Lzma/MatchFinder.cs b/src/Filee.Engines/Archives/Lzma/MatchFinder.cs index fbb2ad515..0bcd78ea4 100644 --- a/src/Filee.Engines/Archives/Lzma/MatchFinder.cs +++ b/src/Filee.Engines/Archives/Lzma/MatchFinder.cs @@ -369,6 +369,7 @@ private int ChainSearch(int pos, int maxLength, int[] lengths, uint[] distances) ref var links = ref MemoryMarshal.GetArrayDataReference(_links); var mask = _mask; Unsafe.Add(ref links, stored & mask) = candidate; + PrefetchCandidate(ref links, ref data, candidate, offset, mask, buffer.Length); // 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) @@ -415,6 +416,22 @@ private unsafe void PrefetchSlots(uint value) Sse.Prefetch0(Unsafe.AsPointer(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_head4), (int)((value * 2654435761u) >> _hash4Shift)))); } + /// + /// A hint to load the first candidate of the chain (its link and its first bytes) into the cache (x86; elsewhere + /// nothing), so the walk's first steps don't wait for them after the two- and three-byte candidates are checked. + /// Only addresses inside the tables and the buffer are used; nothing is read through them. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe void PrefetchCandidate(ref int links, ref byte data, int candidate, int offset, int mask, int length) + { + if (!Sse.IsSupported || candidate <= 0) + return; + Sse.Prefetch0(Unsafe.AsPointer(ref Unsafe.Add(ref links, candidate & mask))); + var at = candidate - offset; + if ((uint)at < (uint)length) + Sse.Prefetch0(Unsafe.AsPointer(ref Unsafe.Add(ref data, at))); + } + /// 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)); diff --git a/tests/Filee.Engines.Tests/Bzip2WriterTests.cs b/tests/Filee.Engines.Tests/Bzip2WriterTests.cs index f0841393c..e61a6fc46 100644 --- a/tests/Filee.Engines.Tests/Bzip2WriterTests.cs +++ b/tests/Filee.Engines.Tests/Bzip2WriterTests.cs @@ -217,6 +217,93 @@ public void The_transform_matches_a_plain_sort_of_all_rotations() } } + /// + /// The transform of larger blocks against rotations sorted by prefix doubling: text, x86-like code, noise over two + /// letters, long repeats with a few changes and a Fibonacci word (whose LMS substrings repeat level after level), so + /// the sort's naming and its recursion on the string of names are exercised well past the small blocks above. + /// + [Fact] + public void The_transform_of_large_blocks_matches_rotations_sorted_by_doubling() + { + var random = new Random(9); + var fibonacci = new StringBuilder("a"); + var previous = "b"; + while (fibonacci.Length < 30_000) + { + var next = fibonacci.ToString() + previous; + previous = fibonacci.ToString(); + fibonacci.Clear().Append(next); + } + var repeats = Noise(3000, 5); + var changed = new byte[40_000]; + for (var i = 0; i < changed.Length; i++) + changed[i] = repeats[i % repeats.Length]; + for (var i = 0; i < 20; i++) + changed[random.Next(changed.Length)] ^= 0x55; + List blocks = + [ + LzmaTests.Prose(40_000), + Mixed(40_000), + [.. Enumerable.Range(0, 40_000).Select(_ => (byte)('a' + random.Next(2)))], + changed, + Encoding.ASCII.GetBytes(fibonacci.ToString(0, 30_000)), + ]; + foreach (var block in blocks) + { + var n = block.Length; + var order = RotationsByDoubling(block); + var expected = order.Select(start => block[(start + n - 1) % n]).ToArray(); + var output = new byte[n]; + var origin = Bzip2Sort.Transform(block, output, Ct); + Assert.Equal(expected, output); + byte[] unrotated = [.. block.AsSpan(order[origin]), .. block.AsSpan(0, order[origin])]; + Assert.Equal(block, unrotated); + } + } + + /// The rotation starts of in sorted order (equal rotations in any order). + private static int[] RotationsByDoubling(byte[] block) + { + var n = block.Length; + var rank = block.Select(b => (int)b).ToArray(); + var order = Enumerable.Range(0, n).ToArray(); + for (var k = 1; ; k *= 2) + { + var current = rank; + var step = k % n; + var keys = order.Select(i => ((long)current[i] << 32) | (uint)current[(i + step) % n]).ToArray(); + Array.Sort(keys, order); + var next = new int[n]; + for (var i = 1; i < n; i++) + next[order[i]] = next[order[i - 1]] + (keys[i] != keys[i - 1] ? 1 : 0); + rank = next; + if (next[order[n - 1]] == n - 1 || k >= n) + return order; + } + } + + /// The smallest rotation and whether the text is a power of a shorter piece, against trying every rotation. + [Fact] + public void The_smallest_rotation_and_periodicity_match_a_plain_search() + { + var random = new Random(12); + for (var i = 0; i < 4000; i++) + { + var unit = Enumerable.Range(0, random.Next(1, 7)).Select(_ => (byte)('a' + random.Next(random.Next(1, 4)))).ToArray(); + var text = random.Next(2) == 0 + ? Enumerable.Range(0, unit.Length * random.Next(1, 6)).Select(k => unit[k % unit.Length]).ToArray() + : Enumerable.Range(0, random.Next(1, 40)).Select(_ => (byte)('a' + random.Next(3))).ToArray(); + var n = text.Length; + byte[] Rotation(int start) => [.. text.AsSpan(start), .. text.AsSpan(0, start)]; + var smallest = Enumerable.Range(0, n).Select(Rotation).Min(Comparer.Create((a, b) => a.AsSpan().SequenceCompareTo(b)))!; + var periodic = Enumerable.Range(1, n - 1).Any(start => Rotation(start).AsSpan().SequenceEqual(text)); + + var shift = Bzip2Sort.SmallestRotation(text, out var foundPeriodic); + Assert.Equal(smallest, Rotation(shift)); + Assert.Equal(periodic, foundPeriodic); + } + } + [Fact] public void One_thread_and_several_write_the_same_bytes() { diff --git a/tests/Filee.Engines.Tests/LzmaTests.cs b/tests/Filee.Engines.Tests/LzmaTests.cs index 91f098e7a..f1e5945fb 100644 --- a/tests/Filee.Engines.Tests/LzmaTests.cs +++ b/tests/Filee.Engines.Tests/LzmaTests.cs @@ -116,6 +116,39 @@ public void Crc64_matches_the_xz_check_value() Assert.Equal(Crc64.Compute(data), crc.Value); } + /// + /// Long pieces are folded with carry-less multiplies where the processor has them, short ones go through the tables: + /// both give the same value, for lengths around the cut-over and the 16-byte steps and for pieces of any split. + /// + [Fact] + public void Crc64_of_long_pieces_matches_the_tables() + { + var data = Noise(200_000, 8); + static ulong ByteByByte(ReadOnlySpan bytes) + { + var crc = new Crc64(); + foreach (var b in bytes) + crc.Append([b]); + return crc.Value; + } + foreach (var length in Enumerable.Range(0, 300).Concat([1000, 4095, 4096, 4097, 65_536, 199_999])) + Assert.Equal(ByteByByte(data.AsSpan(0, length)), Crc64.Compute(data.AsSpan(0, length))); + + var random = new Random(8); + var expected = ByteByByte(data); + for (var round = 0; round < 20; round++) + { + var crc = new Crc64(); + for (var at = 0; at < data.Length;) + { + var count = Math.Min(data.Length - at, random.Next(4) == 0 ? random.Next(1, 20) : random.Next(1, 30_000)); + crc.Append(data.AsSpan(at, count)); + at += count; + } + Assert.Equal(expected, crc.Value); + } + } + [Theory] [InlineData(ArchiveLevel.Store)] [InlineData(ArchiveLevel.Fast)]