From 42a5df5f94e03288037f731ffd86dee7c7cf1332 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 14:51:35 -0500 Subject: [PATCH] chore: cut over image codecs and zip to craft-libs (#6661) * chore: cut over image codecs and zip to craft-libs, fix main test regressions * tests: store ODS mimetype and use standard ODF namespaces in mailings test * Remove unused quick-xml workspace dependency * fix: use standard ODF namespaces in file_dialogs test and preserve trailing line in markdown import_on_with_lines * fix(geom): use png_chunks for PNG dimensions and p.fits(budget) in decode_for_drawing --- Cargo.lock | 438 +++- Cargo.toml | 28 +- apps/wordcraft-cli/Cargo.toml | 3 +- apps/wordcraft-cli/tests/it/strict.rs | 18 +- crates/docx/Cargo.toml | 6 +- crates/docx/src/fit.rs | 33 +- crates/docx/tests/it/alt_chunks.rs | 23 +- crates/docx/tests/it/bidi.rs | 16 +- crates/docx/tests/it/bidi_runs.rs | 8 +- crates/docx/tests/it/border_lines.rs | 16 +- .../docx/tests/it/cell_margin_inheritance.rs | 13 +- crates/docx/tests/it/char_units.rs | 26 +- crates/docx/tests/it/citations.rs | 21 +- .../tests/it/complex_script_inheritance.rs | 19 +- .../tests/it/content_control_save_cost.rs | 12 +- crates/docx/tests/it/content_controls.rs | 16 +- crates/docx/tests/it/custom_xml.rs | 18 +- crates/docx/tests/it/default_size.rs | 15 +- crates/docx/tests/it/drawing_payload.rs | 9 +- crates/docx/tests/it/east_asian_lang.rs | 16 +- crates/docx/tests/it/east_asian_layout.rs | 16 +- crates/docx/tests/it/emphasis_marks.rs | 14 +- crates/docx/tests/it/fixtures.rs | 86 +- crates/docx/tests/it/glossary.rs | 19 +- crates/docx/tests/it/heading_levels.rs | 8 +- crates/docx/tests/it/legacy_forms.rs | 16 +- crates/docx/tests/it/ligatures.rs | 13 +- crates/docx/tests/it/malformed.rs | 7 +- crates/docx/tests/it/missing_pictures.rs | 18 +- crates/docx/tests/it/nested_block_controls.rs | 16 +- crates/docx/tests/it/passthrough.rs | 19 +- crates/docx/tests/it/picture_links.rs | 42 +- crates/docx/tests/it/positional_tabs.rs | 16 +- crates/docx/tests/it/privacy.rs | 21 +- crates/docx/tests/it/roundtrip.rs | 203 +- .../docx/tests/it/row_property_exceptions.rs | 16 +- crates/docx/tests/it/run_props.rs | 16 +- .../docx/tests/it/section_form_protection.rs | 16 +- .../docx/tests/it/section_line_numbering.rs | 16 +- .../tests/it/section_metadata_preservation.rs | 16 +- .../docx/tests/it/section_page_number_type.rs | 16 +- .../docx/tests/it/section_text_direction.rs | 29 +- crates/docx/tests/it/section_valign.rs | 16 +- crates/docx/tests/it/settings_merge.rs | 19 +- crates/docx/tests/it/strict_drawings.rs | 16 +- crates/docx/tests/it/text_alignment.rs | 16 +- crates/docx/tests/it/text_boxes.rs | 26 +- crates/docx/tests/it/toc_ranges.rs | 8 +- crates/docx/tests/it/undefined_style.rs | 8 +- crates/docx/tests/it/vml_shapes.rs | 9 +- crates/docx/tests/it/word_xml.rs | 6 +- crates/docx/tests/it/wordml2003_write.rs | 6 +- crates/docx/tests/matrix_save_size.rs | 4 +- crates/engine/Cargo.toml | 2 +- crates/engine/src/cmd/equation/tests.rs | 16 +- crates/engine/src/cmd/insert/tests.rs | 11 +- crates/engine/src/cmd/mailings/tests.rs | 16 +- crates/engine/src/cmd/objects/picture.rs | 3 +- crates/engine/src/cmd/objects/tests.rs | 56 +- crates/engine/src/cmd/references/tests.rs | 21 +- crates/engine/src/io.rs | 2 +- crates/engine/src/tests.rs | 239 +- crates/engine/src/tests/bidi.rs | 11 +- crates/engine/src/tests/fonts.rs | 7 +- crates/engine/src/tests/typing.rs | 23 +- crates/engine/tests/it/date_fields.rs | 7 +- crates/engine/tests/it/macro_packages.rs | 19 +- crates/engine/tests/it/odt_charts.rs | 22 +- crates/engine/tests/it/odt_fields.rs | 18 +- crates/engine/tests/it/odt_forms.rs | 20 +- crates/engine/tests/it/odt_objects.rs | 16 +- crates/engine/tests/it/odt_password.rs | 5 +- crates/engine/tests/it/odt_sections.rs | 17 +- crates/engine/tests/it/odt_toc.rs | 26 +- .../engine/tests/it/ref_paragraph_numbers.rs | 9 +- crates/engine/tests/it/rtf_sections.rs | 9 +- crates/engine/tests/strict_export.rs | 18 +- crates/engine/tests/strict_pictures.rs | 18 +- crates/formats/Cargo.toml | 35 +- crates/formats/src/html.rs | 77 +- crates/formats/src/html/builder.rs | 1193 --------- crates/formats/src/html/css.rs | 167 -- crates/formats/src/html/entity.rs | 370 --- crates/formats/src/html/export.rs | 393 --- crates/formats/src/html/import.rs | 125 - crates/formats/src/html/lexer.rs | 176 -- crates/formats/src/html/math_ruby.rs | 156 -- crates/formats/src/html/tests.rs | 769 ------ crates/formats/src/latex.rs | 64 +- crates/formats/src/latex/export.rs | 489 ---- crates/formats/src/latex/lex.rs | 470 ---- crates/formats/src/latex/limits.rs | 11 - crates/formats/src/latex/math.rs | 567 ----- crates/formats/src/latex/parser.rs | 914 ------- crates/formats/src/latex/table.rs | 228 -- crates/formats/src/latex/tests.rs | 473 ---- crates/formats/src/latex/units.rs | 185 -- crates/formats/src/markdown.rs | 94 +- crates/formats/src/markdown/blocks.rs | 841 ------- crates/formats/src/markdown/export.rs | 448 ---- crates/formats/src/markdown/inlines.rs | 595 ----- crates/formats/src/markdown/tests.rs | 849 ------- crates/formats/src/mathml.rs | 1057 +------- crates/formats/src/mht.rs | 502 +--- crates/formats/src/model.rs | 191 +- crates/formats/src/model/color.rs | 196 -- crates/formats/src/model/from_doc.rs | 1251 ---------- crates/formats/src/model/media.rs | 154 -- crates/formats/src/model/odf_object.rs | 72 - crates/formats/src/model/to_doc.rs | 1001 -------- crates/formats/src/model/types.rs | 1089 --------- crates/formats/src/odt.rs | 117 +- crates/formats/src/odt/body.rs | 2177 ----------------- crates/formats/src/odt/body/drawing.rs | 474 ---- crates/formats/src/odt/body/indexes.rs | 120 - crates/formats/src/odt/body/notes.rs | 381 --- crates/formats/src/odt/body/tables.rs | 150 -- crates/formats/src/odt/body/tracked.rs | 211 -- crates/formats/src/odt/cell_formula.rs | 364 --- crates/formats/src/odt/chart.rs | 321 --- crates/formats/src/odt/crypt.rs | 431 ---- crates/formats/src/odt/datastyle.rs | 360 --- crates/formats/src/odt/fields.rs | 543 ---- crates/formats/src/odt/flat.rs | 422 ---- crates/formats/src/odt/forms.rs | 409 ---- crates/formats/src/odt/formula_size.rs | 172 -- crates/formats/src/odt/frames.rs | 660 ----- crates/formats/src/odt/geometry.rs | 428 ---- crates/formats/src/odt/import.rs | 1510 ------------ crates/formats/src/odt/kept.rs | 833 ------- crates/formats/src/odt/limits.rs | 25 - crates/formats/src/odt/objects.rs | 112 - crates/formats/src/odt/package.rs | 1554 ------------ crates/formats/src/odt/page_parts.rs | 198 -- crates/formats/src/odt/parts.rs | 149 -- crates/formats/src/odt/path.rs | 416 ---- crates/formats/src/odt/shapes.rs | 171 -- crates/formats/src/odt/strokes.rs | 348 --- crates/formats/src/odt/styles.rs | 1750 ------------- crates/formats/src/odt/tests.rs | 1474 ----------- crates/formats/src/odt/tests_more.rs | 49 - .../formats/src/odt/tests_more/bookmarks.rs | 82 - .../src/odt/tests_more/cell_formulas.rs | 117 - .../formats/src/odt/tests_more/char_styles.rs | 91 - crates/formats/src/odt/tests_more/chars.rs | 834 ------- crates/formats/src/odt/tests_more/charts.rs | 361 --- crates/formats/src/odt/tests_more/comments.rs | 187 -- .../formats/src/odt/tests_more/connectors.rs | 102 - .../src/odt/tests_more/custom_geometry.rs | 240 -- .../formats/src/odt/tests_more/drop_caps.rs | 44 - .../formats/src/odt/tests_more/encryption.rs | 78 - .../formats/src/odt/tests_more/equations.rs | 63 - crates/formats/src/odt/tests_more/fields.rs | 368 --- crates/formats/src/odt/tests_more/flat.rs | 169 -- crates/formats/src/odt/tests_more/floats.rs | 458 ---- .../formats/src/odt/tests_more/flow_breaks.rs | 470 ---- crates/formats/src/odt/tests_more/fonts.rs | 75 - .../src/odt/tests_more/format_changes.rs | 74 - crates/formats/src/odt/tests_more/forms.rs | 248 -- crates/formats/src/odt/tests_more/groups.rs | 174 -- crates/formats/src/odt/tests_more/headers.rs | 476 ---- crates/formats/src/odt/tests_more/indexes.rs | 160 -- crates/formats/src/odt/tests_more/kept.rs | 319 --- crates/formats/src/odt/tests_more/lists.rs | 168 -- crates/formats/src/odt/tests_more/lossless.rs | 914 ------- .../src/odt/tests_more/named_styles.rs | 167 -- crates/formats/src/odt/tests_more/notes.rs | 193 -- .../src/odt/tests_more/numbering_fields.rs | 115 - crates/formats/src/odt/tests_more/objects.rs | 282 --- crates/formats/src/odt/tests_more/outline.rs | 438 ---- .../formats/src/odt/tests_more/page_setup.rs | 235 -- .../src/odt/tests_more/para_borders.rs | 85 - crates/formats/src/odt/tests_more/parts.rs | 152 -- .../src/odt/tests_more/placeholders.rs | 92 - crates/formats/src/odt/tests_more/polygons.rs | 138 -- crates/formats/src/odt/tests_more/rotation.rs | 99 - crates/formats/src/odt/tests_more/rtl.rs | 30 - crates/formats/src/odt/tests_more/ruby.rs | 46 - crates/formats/src/odt/tests_more/settings.rs | 51 - crates/formats/src/odt/tests_more/shapes.rs | 190 -- crates/formats/src/odt/tests_more/spacing.rs | 199 -- crates/formats/src/odt/tests_more/strokes.rs | 198 -- .../formats/src/odt/tests_more/style_look.rs | 67 - crates/formats/src/odt/tests_more/tables.rs | 193 -- crates/formats/src/odt/tests_more/tabs.rs | 185 -- .../formats/src/odt/tests_more/text_boxes.rs | 327 --- .../src/odt/tests_more/text_direction.rs | 92 - crates/formats/src/odt/tests_more/unstated.rs | 139 -- crates/formats/src/odt/writer.rs | 1229 ---------- crates/formats/src/odt/writer/charts.rs | 215 -- crates/formats/src/odt/writer/drawing.rs | 677 ----- crates/formats/src/odt/writer/indexes.rs | 156 -- crates/formats/src/odt/writer/notes.rs | 96 - crates/formats/src/odt/writer/props.rs | 422 ---- crates/formats/src/odt/writer/tables.rs | 251 -- crates/formats/src/odt/writer/tracked.rs | 221 -- crates/formats/src/odt/xml.rs | 96 - crates/formats/src/rtf.rs | 22 +- crates/formats/src/sheet.rs | 778 +----- crates/formats/src/sheet/date.rs | 178 -- crates/formats/src/sheet/ods.rs | 191 -- crates/formats/src/sheet/xls.rs | 748 ------ crates/formats/src/sheet/xlsx.rs | 315 --- crates/formats/src/tests.rs | 57 +- crates/formats/src/txt.rs | 5 +- crates/geom/Cargo.toml | 3 +- crates/geom/src/picture.rs | 299 ++- crates/layout/Cargo.toml | 2 +- crates/layout/src/contour.rs | 8 +- crates/layout/src/lib.rs | 1 + crates/layout/src/missing.rs | 77 +- crates/layout/src/strict.rs | 2 +- crates/layout/src/tests/budget.rs | 8 +- crates/metafile/Cargo.toml | 13 - crates/metafile/src/bytes.rs | 59 - crates/metafile/src/canvas.rs | 812 ------ crates/metafile/src/dib.rs | 719 ------ crates/metafile/src/emf.rs | 370 --- crates/metafile/src/geom.rs | 128 - crates/metafile/src/lib.rs | 284 --- crates/metafile/src/place.rs | 217 -- crates/metafile/src/size.rs | 98 - crates/metafile/src/tests.rs | 1187 --------- crates/metafile/src/wmf.rs | 272 -- crates/pdf/Cargo.toml | 2 +- crates/pdf/src/exporter.rs | 2 +- crates/pdf/src/exporter/draw.rs | 8 +- crates/pdf/src/exporter/text.rs | 4 +- crates/pdf/src/lib.rs | 2 +- crates/pdf/src/picture.rs | 15 +- crates/pdf/src/tests.rs | 32 +- crates/render/Cargo.toml | 2 +- crates/render/src/color_glyph.rs | 48 +- crates/render/src/lib.rs | 20 +- crates/render/src/media.rs | 25 +- crates/render/src/tests.rs | 63 +- crates/ui-egui/Cargo.toml | 2 +- crates/ui-egui/src/canvas/tests.rs | 12 +- crates/ui-egui/src/control/screenshots.rs | 4 +- crates/ui-egui/src/crop.rs | 5 +- .../src/dialogs/dialogs_layout/tests.rs | 3 +- crates/ui-egui/src/file_dialogs/tests.rs | 13 +- crates/ui-egui/src/objects/tests.rs | 6 +- crates/ui-egui/src/paste_picture.rs | 20 +- crates/ui-egui/src/ribbon/tests.rs | 9 +- crates/ui-egui/src/system_clipboard.rs | 13 +- crates/ui-egui/src/tests.rs | 5 +- 247 files changed, 1914 insertions(+), 52271 deletions(-) delete mode 100644 crates/formats/src/html/builder.rs delete mode 100644 crates/formats/src/html/css.rs delete mode 100644 crates/formats/src/html/entity.rs delete mode 100644 crates/formats/src/html/export.rs delete mode 100644 crates/formats/src/html/import.rs delete mode 100644 crates/formats/src/html/lexer.rs delete mode 100644 crates/formats/src/html/math_ruby.rs delete mode 100644 crates/formats/src/html/tests.rs delete mode 100644 crates/formats/src/latex/export.rs delete mode 100644 crates/formats/src/latex/lex.rs delete mode 100644 crates/formats/src/latex/limits.rs delete mode 100644 crates/formats/src/latex/math.rs delete mode 100644 crates/formats/src/latex/parser.rs delete mode 100644 crates/formats/src/latex/table.rs delete mode 100644 crates/formats/src/latex/tests.rs delete mode 100644 crates/formats/src/latex/units.rs delete mode 100644 crates/formats/src/markdown/blocks.rs delete mode 100644 crates/formats/src/markdown/export.rs delete mode 100644 crates/formats/src/markdown/inlines.rs delete mode 100644 crates/formats/src/markdown/tests.rs delete mode 100644 crates/formats/src/model/color.rs delete mode 100644 crates/formats/src/model/from_doc.rs delete mode 100644 crates/formats/src/model/media.rs delete mode 100644 crates/formats/src/model/odf_object.rs delete mode 100644 crates/formats/src/model/to_doc.rs delete mode 100644 crates/formats/src/model/types.rs delete mode 100644 crates/formats/src/odt/body.rs delete mode 100644 crates/formats/src/odt/body/drawing.rs delete mode 100644 crates/formats/src/odt/body/indexes.rs delete mode 100644 crates/formats/src/odt/body/notes.rs delete mode 100644 crates/formats/src/odt/body/tables.rs delete mode 100644 crates/formats/src/odt/body/tracked.rs delete mode 100644 crates/formats/src/odt/cell_formula.rs delete mode 100644 crates/formats/src/odt/chart.rs delete mode 100644 crates/formats/src/odt/crypt.rs delete mode 100644 crates/formats/src/odt/datastyle.rs delete mode 100644 crates/formats/src/odt/fields.rs delete mode 100644 crates/formats/src/odt/flat.rs delete mode 100644 crates/formats/src/odt/forms.rs delete mode 100644 crates/formats/src/odt/formula_size.rs delete mode 100644 crates/formats/src/odt/frames.rs delete mode 100644 crates/formats/src/odt/geometry.rs delete mode 100644 crates/formats/src/odt/import.rs delete mode 100644 crates/formats/src/odt/kept.rs delete mode 100644 crates/formats/src/odt/limits.rs delete mode 100644 crates/formats/src/odt/objects.rs delete mode 100644 crates/formats/src/odt/package.rs delete mode 100644 crates/formats/src/odt/page_parts.rs delete mode 100644 crates/formats/src/odt/parts.rs delete mode 100644 crates/formats/src/odt/path.rs delete mode 100644 crates/formats/src/odt/shapes.rs delete mode 100644 crates/formats/src/odt/strokes.rs delete mode 100644 crates/formats/src/odt/styles.rs delete mode 100644 crates/formats/src/odt/tests.rs delete mode 100644 crates/formats/src/odt/tests_more.rs delete mode 100644 crates/formats/src/odt/tests_more/bookmarks.rs delete mode 100644 crates/formats/src/odt/tests_more/cell_formulas.rs delete mode 100644 crates/formats/src/odt/tests_more/char_styles.rs delete mode 100644 crates/formats/src/odt/tests_more/chars.rs delete mode 100644 crates/formats/src/odt/tests_more/charts.rs delete mode 100644 crates/formats/src/odt/tests_more/comments.rs delete mode 100644 crates/formats/src/odt/tests_more/connectors.rs delete mode 100644 crates/formats/src/odt/tests_more/custom_geometry.rs delete mode 100644 crates/formats/src/odt/tests_more/drop_caps.rs delete mode 100644 crates/formats/src/odt/tests_more/encryption.rs delete mode 100644 crates/formats/src/odt/tests_more/equations.rs delete mode 100644 crates/formats/src/odt/tests_more/fields.rs delete mode 100644 crates/formats/src/odt/tests_more/flat.rs delete mode 100644 crates/formats/src/odt/tests_more/floats.rs delete mode 100644 crates/formats/src/odt/tests_more/flow_breaks.rs delete mode 100644 crates/formats/src/odt/tests_more/fonts.rs delete mode 100644 crates/formats/src/odt/tests_more/format_changes.rs delete mode 100644 crates/formats/src/odt/tests_more/forms.rs delete mode 100644 crates/formats/src/odt/tests_more/groups.rs delete mode 100644 crates/formats/src/odt/tests_more/headers.rs delete mode 100644 crates/formats/src/odt/tests_more/indexes.rs delete mode 100644 crates/formats/src/odt/tests_more/kept.rs delete mode 100644 crates/formats/src/odt/tests_more/lists.rs delete mode 100644 crates/formats/src/odt/tests_more/lossless.rs delete mode 100644 crates/formats/src/odt/tests_more/named_styles.rs delete mode 100644 crates/formats/src/odt/tests_more/notes.rs delete mode 100644 crates/formats/src/odt/tests_more/numbering_fields.rs delete mode 100644 crates/formats/src/odt/tests_more/objects.rs delete mode 100644 crates/formats/src/odt/tests_more/outline.rs delete mode 100644 crates/formats/src/odt/tests_more/page_setup.rs delete mode 100644 crates/formats/src/odt/tests_more/para_borders.rs delete mode 100644 crates/formats/src/odt/tests_more/parts.rs delete mode 100644 crates/formats/src/odt/tests_more/placeholders.rs delete mode 100644 crates/formats/src/odt/tests_more/polygons.rs delete mode 100644 crates/formats/src/odt/tests_more/rotation.rs delete mode 100644 crates/formats/src/odt/tests_more/rtl.rs delete mode 100644 crates/formats/src/odt/tests_more/ruby.rs delete mode 100644 crates/formats/src/odt/tests_more/settings.rs delete mode 100644 crates/formats/src/odt/tests_more/shapes.rs delete mode 100644 crates/formats/src/odt/tests_more/spacing.rs delete mode 100644 crates/formats/src/odt/tests_more/strokes.rs delete mode 100644 crates/formats/src/odt/tests_more/style_look.rs delete mode 100644 crates/formats/src/odt/tests_more/tables.rs delete mode 100644 crates/formats/src/odt/tests_more/tabs.rs delete mode 100644 crates/formats/src/odt/tests_more/text_boxes.rs delete mode 100644 crates/formats/src/odt/tests_more/text_direction.rs delete mode 100644 crates/formats/src/odt/tests_more/unstated.rs delete mode 100644 crates/formats/src/odt/writer.rs delete mode 100644 crates/formats/src/odt/writer/charts.rs delete mode 100644 crates/formats/src/odt/writer/drawing.rs delete mode 100644 crates/formats/src/odt/writer/indexes.rs delete mode 100644 crates/formats/src/odt/writer/notes.rs delete mode 100644 crates/formats/src/odt/writer/props.rs delete mode 100644 crates/formats/src/odt/writer/tables.rs delete mode 100644 crates/formats/src/odt/writer/tracked.rs delete mode 100644 crates/formats/src/odt/xml.rs delete mode 100644 crates/formats/src/sheet/date.rs delete mode 100644 crates/formats/src/sheet/ods.rs delete mode 100644 crates/formats/src/sheet/xls.rs delete mode 100644 crates/formats/src/sheet/xlsx.rs delete mode 100644 crates/metafile/Cargo.toml delete mode 100644 crates/metafile/src/bytes.rs delete mode 100644 crates/metafile/src/canvas.rs delete mode 100644 crates/metafile/src/dib.rs delete mode 100644 crates/metafile/src/emf.rs delete mode 100644 crates/metafile/src/geom.rs delete mode 100644 crates/metafile/src/lib.rs delete mode 100644 crates/metafile/src/place.rs delete mode 100644 crates/metafile/src/size.rs delete mode 100644 crates/metafile/src/tests.rs delete mode 100644 crates/metafile/src/wmf.rs diff --git a/Cargo.lock b/Cargo.lock index 5a89245b0d..e60b33bbe5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,13 +155,28 @@ version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3" +[[package]] +name = "alloc-no-stdlib" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2fb6cfd47bf496ff64095c20eaba0c201404ee38714d4142fcfa1dc334fcc7a" + [[package]] name = "alloc-stdlib" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e76a019e91224d279006ff972f1e984179a6e9feb050adba6ce8274aef23195" dependencies = [ - "alloc-no-stdlib", + "alloc-no-stdlib 2.0.4", +] + +[[package]] +name = "alloc-stdlib" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5c1865780388bfa186411ab5f247819487fc4864c6e9c3106611fa347586e1" +dependencies = [ + "alloc-no-stdlib 3.0.0", ] [[package]] @@ -210,15 +225,6 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" -[[package]] -name = "arbitrary" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" -dependencies = [ - "derive_arbitrary", -] - [[package]] name = "arboard" version = "3.7.0" @@ -523,6 +529,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bit_field" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1" + [[package]] name = "bitflags" version = "1.3.2" @@ -614,9 +626,9 @@ version = "8.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cc91aac060a7a1e25823bdccbfb6af1875b88f17c6daac97894eed8207166b3" dependencies = [ - "alloc-no-stdlib", - "alloc-stdlib", - "brotli-decompressor", + "alloc-no-stdlib 2.0.4", + "alloc-stdlib 0.2.4", + "brotli-decompressor 5.0.3", ] [[package]] @@ -625,8 +637,18 @@ version = "5.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a32acac15fe1967bc3986b2a6347dffc965602354ea6f450ad07e8bfd253583" dependencies = [ - "alloc-no-stdlib", - "alloc-stdlib", + "alloc-no-stdlib 2.0.4", + "alloc-stdlib 0.2.4", +] + +[[package]] +name = "brotli-decompressor" +version = "6.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75c74c9c51b9140663e196b5fb88bc94c334d7a497c8311cb759d8557874747f" +dependencies = [ + "alloc-no-stdlib 3.0.0", + "alloc-stdlib 0.3.0", ] [[package]] @@ -1022,17 +1044,17 @@ dependencies = [ [[package]] name = "craft-cfb" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" [[package]] name = "craft-doc-binary" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "chrono", "craft-cfb", @@ -1048,7 +1070,7 @@ dependencies = [ [[package]] name = "craft-docx" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "base64 0.23.1", "chrono", @@ -1068,7 +1090,7 @@ dependencies = [ [[package]] name = "craft-drawingml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "craft-xml", "kurbo", @@ -1079,7 +1101,7 @@ dependencies = [ [[package]] name = "craft-flow" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "craft-wordmodel", "image", @@ -1122,6 +1144,19 @@ name = "craft-fonts-build" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +[[package]] +name = "craft-html" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "craft-codepage", + "craft-flow", + "craft-math", + "craft-mime", + "craft-wordmodel", + "encoding_rs", +] + [[package]] name = "craft-i18n" version = "0.1.0" @@ -1145,19 +1180,83 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "craft-jpeg", + "craft-metadata", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder", + "png", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-latex" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "craft-flow", + "craft-math", + "craft-mime", + "craft-wordmodel", +] + +[[package]] +name = "craft-markdown" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "craft-flow", + "craft-html", + "craft-wordmodel", +] + [[package]] name = "craft-math" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "craft-xml", "serde", ] +[[package]] +name = "craft-metadata" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "brotli-decompressor 6.3.1", + "chrono", + "craft-tiff", + "quick-xml 0.41.0", + "serde", +] + [[package]] name = "craft-metafile" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "encoding_rs", "image", @@ -1165,10 +1264,58 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-mime" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "chardetng", + "chrono", + "craft-codepage", + "encoding_rs", +] + +[[package]] +name = "craft-odf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "aes", + "base64 0.23.1", + "chrono", + "craft-flow", + "craft-math", + "craft-metafile", + "craft-wordmodel", + "craft-xml", + "craft-zip", + "flate2", + "getrandom 0.3.4", + "hmac", + "image", + "js-sys", + "log", + "sha1 0.11.0", + "sha2 0.11.1", + "thiserror 2.0.21", + "zeroize", +] + +[[package]] +name = "craft-ods" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "craft-odf", + "craft-sheet", + "craft-xml", + "thiserror 2.0.21", +] + [[package]] name = "craft-offcrypto" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "aes", "base64 0.23.1", @@ -1185,7 +1332,7 @@ dependencies = [ [[package]] name = "craft-opc" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "craft-xml", "craft-zip", @@ -1230,7 +1377,7 @@ dependencies = [ [[package]] name = "craft-rtf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "craft-codepage", "craft-flow", @@ -1240,6 +1387,20 @@ dependencies = [ "image", ] +[[package]] +name = "craft-sheet" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" + +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1284,7 +1445,7 @@ dependencies = [ "verifysign", "winreg", "winsafe", - "zip 9.1.0", + "zip", ] [[package]] @@ -1329,7 +1490,7 @@ dependencies = [ [[package]] name = "craft-wordmodel" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "base64 0.23.1", "craft-math", @@ -1340,10 +1501,35 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "craft-xls" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "craft-cfb", + "craft-sheet", + "md-5", + "miniz_oxide 0.9.1", + "sha1 0.10.7", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xlsx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" +dependencies = [ + "craft-drawingml", + "craft-opc", + "craft-sheet", + "craft-xml", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1352,11 +1538,11 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93" +source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c" dependencies = [ "flate2", "thiserror 2.0.21", - "zip 9.1.0", + "zip", ] [[package]] @@ -1484,17 +1670,6 @@ version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" -[[package]] -name = "derive_arbitrary" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e567bd82dcff979e4b03460c307b3cdc9e96fde3d73bed1496d2bc75d9dd62a" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - [[package]] name = "dify" version = "0.8.0" @@ -1906,6 +2081,22 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "exr" +version = "1.74.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.9", + "multiversion", + "rayon-core", + "smallvec", + "zune-inflate", +] + [[package]] name = "fastrand" version = "2.5.0" @@ -2954,6 +3145,7 @@ dependencies = [ "moxcms", "num-traits", "png", + "qoi", "tiff", "zune-core", "zune-jpeg", @@ -3102,6 +3294,12 @@ dependencies = [ "libc", ] +[[package]] +name = "jpeg-encoder" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f" + [[package]] name = "js-sys" version = "0.3.106" @@ -3185,6 +3383,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" +[[package]] +name = "lebe" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" + [[package]] name = "libc" version = "0.2.191" @@ -3273,6 +3477,16 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + [[package]] name = "memchr" version = "2.8.3" @@ -3344,6 +3558,28 @@ dependencies = [ "pxfm", ] +[[package]] +name = "multiversion" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076" +dependencies = [ + "multiversion-macros", + "target-features", +] + +[[package]] +name = "multiversion-macros" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "target-features", +] + [[package]] name = "multiversion_no_op" version = "1.0.0" @@ -4287,6 +4523,15 @@ version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -5564,6 +5809,12 @@ dependencies = [ "xattr", ] +[[package]] +name = "target-features" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5" + [[package]] name = "tempfile" version = "3.27.0" @@ -7010,16 +7261,17 @@ dependencies = [ name = "wordcraft-cli" version = "0.18.1" dependencies = [ + "craft-zip", "image", "serde_json", "sys-locale", "wordcraft-doc", "wordcraft-engine", "wordcraft-fonts", + "wordcraft-geom", "wordcraft-mcp", "wordcraft-render", "wordcraft-zotero", - "zip 3.0.0", ] [[package]] @@ -7060,15 +7312,15 @@ dependencies = [ "cfb", "craft-docx", "craft-wordmodel", + "craft-xml", + "craft-zip", "image", "log", "proptest", - "quick-xml 0.41.0", "serde", "serde_json", "wordcraft-doc", "wordcraft-geom", - "zip 3.0.0", ] [[package]] @@ -7080,6 +7332,7 @@ dependencies = [ "craft-font-catalog", "craft-font-store", "craft-proof", + "craft-zip", "encoding_rs", "getrandom 0.3.4", "image", @@ -7102,14 +7355,13 @@ dependencies = [ "wordcraft-pdf", "wordcraft-render", "zeroize", - "zip 3.0.0", ] [[package]] name = "wordcraft-fonts" version = "0.18.1" dependencies = [ - "brotli-decompressor", + "brotli-decompressor 5.0.3", "harfrust 0.13.3", "kurbo", "log", @@ -7123,35 +7375,37 @@ dependencies = [ name = "wordcraft-formats" version = "0.18.1" dependencies = [ - "aes", - "base64 0.23.1", - "cfb", "chardetng", "chrono", + "craft-flow", + "craft-html", + "craft-image-codecs", + "craft-latex", + "craft-markdown", + "craft-math", + "craft-metafile", + "craft-mime", + "craft-odf", + "craft-ods", "craft-rtf", + "craft-sheet", + "craft-wordmodel", + "craft-xls", + "craft-xlsx", + "craft-zip", "encoding_rs", - "getrandom 0.3.4", - "hmac", - "image", - "js-sys", "log", - "miniz_oxide 0.9.1", "proptest", - "quick-xml 0.41.0", - "sha1 0.11.0", - "sha2 0.11.1", "wordcraft-doc", "wordcraft-docx", "wordcraft-geom", - "wordcraft-metafile", - "zeroize", - "zip 3.0.0", ] [[package]] name = "wordcraft-geom" version = "0.18.1" dependencies = [ + "craft-image-codecs", "image", "serde", ] @@ -7160,6 +7414,7 @@ dependencies = [ name = "wordcraft-layout" version = "0.18.1" dependencies = [ + "craft-metafile", "craft-proof", "image", "js-sys", @@ -7173,7 +7428,6 @@ dependencies = [ "wordcraft-doc", "wordcraft-fonts", "wordcraft-geom", - "wordcraft-metafile", ] [[package]] @@ -7188,19 +7442,12 @@ dependencies = [ "wordcraft-render", ] -[[package]] -name = "wordcraft-metafile" -version = "0.18.1" -dependencies = [ - "kurbo", - "thiserror 2.0.21", -] - [[package]] name = "wordcraft-pdf" version = "0.18.1" dependencies = [ "chrono", + "craft-metafile", "craft-pdf", "hayro-cmap", "hayro-interpret", @@ -7214,13 +7461,13 @@ dependencies = [ "wordcraft-fonts", "wordcraft-geom", "wordcraft-layout", - "wordcraft-metafile", ] [[package]] name = "wordcraft-render" version = "0.18.1" dependencies = [ + "craft-metafile", "image", "log", "vello_cpu", @@ -7228,7 +7475,6 @@ dependencies = [ "wordcraft-fonts", "wordcraft-geom", "wordcraft-layout", - "wordcraft-metafile", ] [[package]] @@ -7242,6 +7488,7 @@ dependencies = [ "craft-ui", "craft-update", "craft-window", + "craft-zip", "egui", "egui_extras", "egui_kittest", @@ -7261,7 +7508,6 @@ dependencies = [ "wordcraft-render", "wordcraft-zotero", "zbus", - "zip 3.0.0", ] [[package]] @@ -7333,7 +7579,7 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "200a7b806377b65f1ff9a985874332f15357c87e5dd7855f35f06dade95b8b3d" dependencies = [ - "brotli-decompressor", + "brotli-decompressor 5.0.3", "bytes", "flate2", ] @@ -7693,20 +7939,6 @@ dependencies = [ "syn 3.0.6", ] -[[package]] -name = "zip" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12598812502ed0105f607f941c386f43d441e00148fce9dec3ca5ffb0bde9308" -dependencies = [ - "arbitrary", - "crc32fast", - "flate2", - "indexmap", - "memchr", - "zopfli", -] - [[package]] name = "zip" version = "9.1.0" @@ -7735,24 +7967,21 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" -[[package]] -name = "zopfli" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249" -dependencies = [ - "bumpalo", - "crc32fast", - "log", - "simd-adler32", -] - [[package]] name = "zune-core" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + [[package]] name = "zune-jpeg" version = "0.5.15" @@ -7762,6 +7991,15 @@ dependencies = [ "zune-core", ] +[[package]] +name = "zune-jpegxl" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4" +dependencies = [ + "zune-core", +] + [[package]] name = "zvariant" version = "5.15.0" diff --git a/Cargo.toml b/Cargo.toml index 1ff4830208..63ca129867 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ wordcraft-mcp = { path = "crates/mcp" } wordcraft-zotero = { path = "crates/zotero" } wordcraft-control-key = { path = "crates/control-key" } wordcraft-ui-egui = { path = "crates/ui-egui" } -wordcraft-metafile = { path = "crates/metafile" } +craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } # foundation kurbo = { version = "0.13", features = ["serde"] } @@ -66,16 +66,28 @@ wuff = "0.2.9" # `forbid(unsafe_code)` in its default build, no_std-capable (builds for wasm), BSD-3-Clause/MIT. brotli-decompressor = "5" vello_cpu = "0.2" -image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "bmp", "tiff"] } +image = { version = "0.25", default-features = false } +craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", version = "=0.1.0", default-features = false, features = ["write"] } # craft-update: app-specific pretend versions apply to startup and manual checks. craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] } -craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93", version = "=0.1.0" } -craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93", version = "=0.1.0" } -craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93", version = "=0.1.0", features = ["doc"] } -craft-doc-binary = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93", version = "=0.1.0" } -quick-xml = "0.41" -zip = { version = "3", default-features = false, features = ["deflate"] } +craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["doc"] } +craft-doc-binary = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-flow = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-odf = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["text"] } +craft-markdown = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-html = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["flow"] } +craft-latex = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-math = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["detect"] } +craft-sheet = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-ods = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-xls = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } +craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" } # zlib streams (compressed metafile pictures in `.doc` drawings); already in the lock as a dependency of other crates. miniz_oxide = "0.9" cfb = "0.15" diff --git a/apps/wordcraft-cli/Cargo.toml b/apps/wordcraft-cli/Cargo.toml index da51fba676..97f09c738d 100644 --- a/apps/wordcraft-cli/Cargo.toml +++ b/apps/wordcraft-cli/Cargo.toml @@ -24,5 +24,6 @@ sys-locale = "0.3" [dev-dependencies] wordcraft-fonts = { workspace = true } +wordcraft-geom = { workspace = true, features = ["image"] } image = { workspace = true } -zip = { workspace = true } +craft-zip = { workspace = true } diff --git a/apps/wordcraft-cli/tests/it/strict.rs b/apps/wordcraft-cli/tests/it/strict.rs index 1fa89d9880..307f204398 100644 --- a/apps/wordcraft-cli/tests/it/strict.rs +++ b/apps/wordcraft-cli/tests/it/strict.rs @@ -1,6 +1,5 @@ //! `convert --strict` (#6005): exit 3 and a report when a PDF or PNG would silently lack a //! character or a picture, nothing written; exit 0 otherwise. -use std::io::Write; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; @@ -64,11 +63,11 @@ fn uncovered() -> char { } fn png() -> Vec { - let mut out = Vec::new(); - image::RgbaImage::from_pixel(4, 4, image::Rgba([20, 90, 200, 255])) - .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png) - .unwrap(); - out + wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(4, 4, image::Rgba([20, 90, 200, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap() } fn pic(rid: &str) -> String { @@ -106,7 +105,7 @@ fn package(document: &str, document_rels: &str, extra: &[(&str, &[u8])]) -> Vec< r#""# ); let types = r#""#; - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); let mut parts: Vec<(&str, &[u8])> = vec![ ("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", rels.as_bytes()), @@ -115,10 +114,9 @@ fn package(document: &str, document_rels: &str, extra: &[(&str, &[u8])]) -> Vec< ]; parts.extend_from_slice(extra); for (name, bytes) in parts { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(bytes).unwrap(); + zip.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } #[test] diff --git a/crates/docx/Cargo.toml b/crates/docx/Cargo.toml index 5ed26d020e..495d0e2a74 100644 --- a/crates/docx/Cargo.toml +++ b/crates/docx/Cargo.toml @@ -12,16 +12,16 @@ workspace = true craft-docx = { workspace = true } craft-wordmodel = { workspace = true } wordcraft-doc = { workspace = true } -quick-xml = { workspace = true } +craft-xml = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } [dev-dependencies] wordcraft-doc = { workspace = true, features = ["test-support"] } -wordcraft-geom = { workspace = true } +wordcraft-geom = { workspace = true, features = ["image"] } base64 = { workspace = true } cfb = { workspace = true } log = { workspace = true } proptest = { workspace = true } image = { workspace = true } -zip = { workspace = true } +craft-zip = { workspace = true } diff --git a/crates/docx/src/fit.rs b/crates/docx/src/fit.rs index 2409f66bbf..333c1ee347 100644 --- a/crates/docx/src/fit.rs +++ b/crates/docx/src/fit.rs @@ -19,39 +19,18 @@ pub(crate) const KIND: &str = "wordcraft-fit-v1"; /// Serialize `fit` as the custom XML store. pub(crate) fn write(fit: &FitState) -> Result, DocxError> { let json = serde_json::to_string(fit).map_err(|e| DocxError::Xml(e.to_string()))?; - let esc_json = quick_xml::escape::escape(&json); + let esc_json = craft_xml::esc(&json); Ok(format!(r#"<{ROOT} xmlns="{NS}" kind="{KIND}">{}"#, esc_json).into_bytes()) } /// Read the state from a custom XML part's bytes, if it is ours. pub(crate) fn read_from_bytes(bytes: &[u8]) -> Option { - let mut reader = quick_xml::Reader::from_reader(bytes); - let mut buf = Vec::new(); - let mut is_our_kind = false; - - loop { - match reader.read_event_into(&mut buf) { - Ok(quick_xml::events::Event::Start(ref e)) => { - let name = e.local_name(); - if name.as_ref() == ROOT.as_bytes() { - for attr in e.attributes().flatten() { - if attr.key.local_name().as_ref() == b"kind" && attr.value.as_ref() == KIND.as_bytes() { - is_our_kind = true; - } - } - } else if name.as_ref() == b"fitState" && is_our_kind { - let text = reader.read_text(e.to_end().name()).ok()?; - let s = std::str::from_utf8(text.as_ref()).ok()?; - let unescaped = quick_xml::escape::unescape(s).ok()?; - return serde_json::from_str(&unescaped).ok(); - } - } - Ok(quick_xml::events::Event::Eof) | Err(_) => break, - _ => {} - } - buf.clear(); + let root = craft_xml::parse(bytes).ok()?; + if root.local() != ROOT || root.attr("kind") != Some(KIND) { + return None; } - None + let text = root.child_text("fitState")?; + serde_json::from_str(text.as_ref()).ok() } #[cfg(test)] diff --git a/crates/docx/tests/it/alt_chunks.rs b/crates/docx/tests/it/alt_chunks.rs index 5b3ab7e136..6cbdae0473 100644 --- a/crates/docx/tests/it/alt_chunks.rs +++ b/crates/docx/tests/it/alt_chunks.rs @@ -1,8 +1,6 @@ //! Alternative format chunks (`w:altChunk`, ECMA-376 §17.17.2.1): content in another format that //! is brought in where the chunk stands. All markup here is synthetic. -use std::io::Write; - use wordcraft_doc::{Block, Document, InlineObject, StoryRef}; use wordcraft_docx::{AltChunk, ReadOptions, read, read_with}; @@ -46,12 +44,11 @@ fn package_styled(body: &str, chunk: Option<(&str, &str, &[u8])>, styles: Option } types.push_str(r#""#); files.push(("[Content_Types].xml".into(), types.into_bytes())); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in files { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(&bytes).unwrap(); + zw.add(&name, &bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } const CHUNK: &str = r#""#; @@ -144,12 +141,11 @@ fn package_with_stories(body: &str, stories: &[(&str, &str, String)], chunk: Opt if let Some(c) = chunk { files.push(("word/afchunk.docx".into(), c.to_vec())); } - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in files { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(&bytes).unwrap(); + zw.add(&name, &bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// A paragraph `text` that comment `id` (in the file) is anchored around. @@ -395,17 +391,16 @@ fn inner_with_dropped_pictures(n: usize, external: bool) -> Vec { (0..n).map(image).collect::() ); let types = r#""#; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [ ("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", ROOT_RELS.as_bytes()), ("word/document.xml", doc.as_bytes()), ("word/_rels/document.xml.rels", rels.as_bytes()), ] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// A picture the chunk's own reader dropped is the file's too (#6005), named by the chunk part. diff --git a/crates/docx/tests/it/bidi.rs b/crates/docx/tests/it/bidi.rs index 68b008b49c..57b232d992 100644 --- a/crates/docx/tests/it/bidi.rs +++ b/crates/docx/tests/it/bidi.rs @@ -2,18 +2,15 @@ //! complex-script font, size, bold and italic, and the bidi language. Fixtures are hand-written //! from ECMA-376 Part 1 §17.3.1.6 (bidi), §17.3.2 (run properties). -use std::io::{Read, Write}; - use wordcraft_doc::props::{Align, CharProps, ParaProps}; use wordcraft_doc::{Document, Paragraph, para_block}; fn zip(entries: &[(&str, &[u8])]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// A package whose body is `body`. @@ -31,10 +28,9 @@ fn paras(d: &Document) -> Vec<&Paragraph> { /// `word/document.xml` of a written package. fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } /// Persian text with a ZWNJ (نیم‌فاصله), Persian digits, Latin and punctuation. diff --git a/crates/docx/tests/it/bidi_runs.rs b/crates/docx/tests/it/bidi_runs.rs index 96d7b05e0b..e669641562 100644 --- a/crates/docx/tests/it/bidi_runs.rs +++ b/crates/docx/tests/it/bidi_runs.rs @@ -6,7 +6,6 @@ //! direction changes and marks the right-to-left pieces `w:rtl`, as Word does with text typed //! in a right-to-left paragraph. -use std::io::Read; use std::sync::Arc; use wordcraft_doc::props::CharProps; @@ -14,10 +13,9 @@ use wordcraft_doc::{Document, Paragraph, para_block}; /// `word/document.xml` of a written package. fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } /// The runs of the first paragraph of `xml`: (has `w:rtl`, text with `w:br` as `\n`). diff --git a/crates/docx/tests/it/border_lines.rs b/crates/docx/tests/it/border_lines.rs index 3033849ce9..6e8354c9fb 100644 --- a/crates/docx/tests/it/border_lines.rs +++ b/crates/docx/tests/it/border_lines.rs @@ -2,8 +2,6 @@ //! `thinThickThinLargeGap`, ECMA-376 Part 1 §17.18.2 ST_Border) on paragraphs, table cells and //! pages: written back as read, not as a single line. Fixtures are hand-written from the spec. -use std::io::{Read, Write}; - use wordcraft_doc::props::BorderStyle; const LINES: [(&str, BorderStyle); 9] = [ @@ -23,19 +21,17 @@ fn docx(body: &str) -> Vec { let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn side(name: &str, val: &str) -> String { diff --git a/crates/docx/tests/it/cell_margin_inheritance.rs b/crates/docx/tests/it/cell_margin_inheritance.rs index 8a726d81ed..3330dedc26 100644 --- a/crates/docx/tests/it/cell_margin_inheritance.rs +++ b/crates/docx/tests/it/cell_margin_inheritance.rs @@ -1,12 +1,10 @@ //! Original partial-cell-margin fixture (ECMA-376 §17.4.69). -use std::io::{Cursor, Read, Write}; fn fixture() -> Vec { let document = r#"Point"#; - let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); - zip.start_file("word/document.xml", zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(document.as_bytes()).unwrap(); - zip.finish().unwrap().into_inner() + let mut zip = craft_zip::Writer::new(); + zip.add("word/document.xml", document.as_bytes(), craft_zip::Compression::Deflated).unwrap(); + zip.finish().unwrap() } #[test] @@ -19,9 +17,8 @@ fn partial_margin_stays_partial() { let reopened = wordcraft_docx::read(&bytes).unwrap(); let cell = &reopened.body[0].as_table().unwrap().rows[0].cells[0]; assert_eq!(cell.props.resolved_margins([6.0, 7.0, 8.0, 9.0]), [6.0, 0.0, 8.0, 9.0]); - let mut zip = zip::ZipArchive::new(Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - zip.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); let margins = xml.split("").nth(1).unwrap().split("").next().unwrap(); assert!(margins.contains(" Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// A package whose body is `body`. @@ -44,9 +41,9 @@ fn character_indents_read_and_round_trip() { // Hostile values are clamped or dropped. assert_eq!((p[3].indent_first_chars, p[3].indent_left_chars), (Some(1584.0), None)); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_docx::write(&d).unwrap())).unwrap(); - let mut xml = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let _z_bytes = wordcraft_docx::write(&d).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); for attr in [r#"w:firstLineChars="200""#, r#"w:leftChars="150""#, r#"w:rightChars="50""#, r#"w:hangingChars="100""#, r#"w:firstLineChars="0""#] { assert!(xml.contains(attr), "{attr} missing: {xml}"); } @@ -71,9 +68,9 @@ fn automatic_spacing_reads_and_round_trips() { assert_eq!((rp.space_before, rp.space_after, rp.before_auto), (14.0, 14.0, true), "automatic spacing is 14 pt"); assert_eq!(d.styles.resolve_para(&p[2]).space_after, 5.0); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_docx::write(&d).unwrap())).unwrap(); - let mut xml = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let _z_bytes = wordcraft_docx::write(&d).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); for attr in [r#"w:beforeAutospacing="1""#, r#"w:afterAutospacing="1""#, r#"w:beforeAutospacing="0""#] { assert!(xml.contains(attr), "{attr} missing"); } @@ -90,9 +87,8 @@ fn adjust_right_indent_is_preserved() { assert_eq!(props(&d).iter().map(|p| p.adjust_right_indent).collect::>(), [Some(false), Some(true), None]); for _ in 0..2 { let out = wordcraft_docx::write(&d).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(out.clone())).unwrap(); - let mut xml = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&out, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""), "explicit true was lost: {xml}"); let tags = [" Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn fld(kind: &str) -> String { @@ -129,10 +126,9 @@ fn count(xml: &str, kind: &str) -> usize { } fn document_xml(docx: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(docx)).unwrap(); - let mut s = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(docx, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] @@ -174,9 +170,8 @@ fn custom_property_bookmark_links_survive_saves_and_value_edits() { let before = d.custom_props.clone(); for _ in 0..2 { let out = write(&d).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&out)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("docProps/custom.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&out, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("docProps/custom.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#"linkTarget="OriginalBookmark""#), "{xml}"); assert!(xml.contains(r#"linkTarget="RawBookmark""#), "{xml}"); assert_eq!(xml.matches("linkTarget=").count(), 2, "unlinked properties stay unlinked"); diff --git a/crates/docx/tests/it/complex_script_inheritance.rs b/crates/docx/tests/it/complex_script_inheritance.rs index d86e6f2a1f..ac6f4aceb9 100644 --- a/crates/docx/tests/it/complex_script_inheritance.rs +++ b/crates/docx/tests/it/complex_script_inheritance.rs @@ -1,7 +1,6 @@ //! Independent complex-script formatting through style inheritance and DOCX round trips. //! Synthetic packages, written here; no external documents or installed fonts are needed. -use std::io::Write; use wordcraft_doc::props::CharProps; use wordcraft_doc::{Document, Paragraph, Run}; @@ -13,17 +12,16 @@ fn package(run: &str, defaults: &str, styles: &str, paragraph_style: &str) -> Ve r#"{paragraph_style}{run}שָׁלוֹם ABC 123"# ); let styles = format!("{defaults}{styles}"); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut z = craft_zip::Writer::new(); for (name, data) in [ ("_rels/.rels", root), ("word/_rels/document.xml.rels", rels), ("word/document.xml", document.as_str()), ("word/styles.xml", styles.as_str()), ] { - z.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn paragraph(d: &Document) -> &Paragraph { @@ -180,9 +178,14 @@ fn split_runs_add_no_phantom_tracked_formatting() { let run = r#""#; let d = wordcraft_docx::read(&package(run, "", "", "")).unwrap(); let bytes = wordcraft_docx::write(&d).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap().by_name("word/document.xml").unwrap(), &mut xml) - .unwrap(); + let xml = String::from_utf8( + craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }) + .unwrap() + .read("word/document.xml", 16 << 20) + .unwrap() + .unwrap(), + ) + .unwrap(); let runs: Vec<&str> = xml.split("").skip(1).map(|r| r.split("").next().unwrap()).collect(); assert!(runs.len() >= 2, "the mixed run is split: {xml}"); assert!(runs.iter().any(|r| r.contains("")), "{xml}"); diff --git a/crates/docx/tests/it/content_control_save_cost.rs b/crates/docx/tests/it/content_control_save_cost.rs index 9e3b96e471..0859ad1a4d 100644 --- a/crates/docx/tests/it/content_control_save_cost.rs +++ b/crates/docx/tests/it/content_control_save_cost.rs @@ -136,18 +136,14 @@ fn the_controls_are_written_where_they_were() { /// The names of the parts of a package, in the order they are in it. fn part_names(bytes: &[u8]) -> Vec { - let z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).expect("zip"); - z.file_names().map(str::to_string).collect() + let z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).expect("zip"); + z.names().map(|n| n.into_owned()).collect() } /// The body of the document part of a package. fn document_body(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).expect("zip"); - let mut xml = String::new(); - { - use std::io::Read; - z.by_name("word/document.xml").expect("the document part").read_to_string(&mut xml).expect("read"); - } + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).expect("zip"); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).expect("read").expect("the document part")).expect("utf8"); let rest = xml.split_once("").expect("the body").1; let body = rest.split_once("").expect("the end of the body").0; format!("{body}") diff --git a/crates/docx/tests/it/content_controls.rs b/crates/docx/tests/it/content_controls.rs index ef420e42d8..3320e50dd6 100644 --- a/crates/docx/tests/it/content_controls.rs +++ b/crates/docx/tests/it/content_controls.rs @@ -2,8 +2,6 @@ //! All markup here is synthetic, written from ECMA-376 §17.5.2 and the [MS-DOCX] `w14`/`w15` //! extensions. -use std::io::Write; - use wordcraft_doc::control::{ControlKind, ControlLock, ListItem}; use wordcraft_doc::{Block, ContentControl, Document, StoryRef}; use wordcraft_docx::{read, write}; @@ -13,12 +11,11 @@ const ROOT_RELS: &str = r#"{body}"#); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", ROOT_RELS.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn t(s: &str) -> String { @@ -90,10 +87,9 @@ fn form() -> Vec { } fn document_xml(docx: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(docx)).unwrap(); - let mut s = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(docx, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn controls(d: &Document) -> Vec { diff --git a/crates/docx/tests/it/custom_xml.rs b/crates/docx/tests/it/custom_xml.rs index 53c1f0c6e2..4d82332061 100644 --- a/crates/docx/tests/it/custom_xml.rs +++ b/crates/docx/tests/it/custom_xml.rs @@ -1,6 +1,5 @@ //! Original synthetic custom XML stores, written from the public OOXML package contract. use std::collections::BTreeMap; -use std::io::{Cursor, Read, Write}; use wordcraft_doc::{Path, StoryRef}; const REL_NS: &str = "http://schemas.openxmlformats.org/package/2006/relationships"; @@ -36,22 +35,19 @@ fn fixture() -> BTreeMap> { } fn zip(parts: &BTreeMap>) -> Vec { - let mut z = zip::ZipWriter::new(Cursor::new(Vec::new())); + let mut z = craft_zip::Writer::new(); for (name, bytes) in parts { - z.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - z.write_all(bytes).unwrap(); + z.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn unzip(bytes: &[u8]) -> BTreeMap> { - let mut z = zip::ZipArchive::new(Cursor::new(bytes)).unwrap(); + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut parts = BTreeMap::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let name = f.name().to_string(); - let mut bytes = Vec::new(); - f.read_to_end(&mut bytes).unwrap(); + for name in names { + let bytes = z.read(&name, 16 << 20).unwrap().unwrap(); assert!(parts.insert(name, bytes).is_none(), "duplicate ZIP entry"); } parts diff --git a/crates/docx/tests/it/default_size.rs b/crates/docx/tests/it/default_size.rs index 65d5a1f9fa..4ee5729f63 100644 --- a/crates/docx/tests/it/default_size.rs +++ b/crates/docx/tests/it/default_size.rs @@ -1,7 +1,6 @@ //! The document default font size (`w:docDefaults/w:rPrDefault/w:rPr/w:sz`) across a save and //! reopen. An unset default is resolved as `DEFAULT_FONT_SIZE`; the writer must say so, because //! the reader reads an omitted size as 10 pt. -use std::io::{Read, Write}; use std::sync::Arc; use wordcraft_doc::Document; use wordcraft_doc::props::{CharProps, DEFAULT_FONT_SIZE}; @@ -9,10 +8,9 @@ fn resolved(d: &Document) -> f32 { d.styles.resolve_char(None, &CharProps::default()).size } fn styles_xml(bytes: &[u8]) -> String { - let mut archive = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - archive.by_name("word/styles.xml").unwrap().read_to_string(&mut xml).unwrap(); - xml + let mut archive = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(archive.read("word/styles.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn defaults(xml: &str) -> &str { xml.split("").nth(1).unwrap().split("").next().unwrap() @@ -47,12 +45,11 @@ fn a_file_that_omits_the_default_size_is_still_read_as_ten_points() { let doc = r#"x"#; let styles = r#""#; let rels = r#""#; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, body) in [("_rels/.rels", root), ("word/document.xml", doc), ("word/_rels/document.xml.rels", rels), ("word/styles.xml", styles)] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(body.as_bytes()).unwrap(); + zw.add(name, body.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - let d = wordcraft_docx::read(&zw.finish().unwrap().into_inner()).unwrap(); + let d = wordcraft_docx::read(&zw.finish().unwrap()).unwrap(); assert_eq!(d.styles.default_chr.size, Some(10.0)); } #[test] diff --git a/crates/docx/tests/it/drawing_payload.rs b/crates/docx/tests/it/drawing_payload.rs index 06c245d0bf..393449a18e 100644 --- a/crates/docx/tests/it/drawing_payload.rs +++ b/crates/docx/tests/it/drawing_payload.rs @@ -1,5 +1,5 @@ //! Original nested-picture controls. No native output or external assets are embedded. -use std::{io::Write, sync::Arc}; +use std::sync::Arc; use wordcraft_doc::para::{Anchor, CanvasStyle, Float, GroupChild, ShapeKind, Wrap}; use wordcraft_doc::props::Rgb; use wordcraft_doc::{Document, InlineObject, Paragraph, PartKind, Pos, StoryRef, TextBody, para_block}; @@ -71,7 +71,7 @@ fn check_box(doc: &Document) { assert_eq!(doc.media[media].as_slice(), BLUE_PNG); } fn package(xml: &str) -> Vec { - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); let rels = br#""#; let docrels = br#""#; let types = br#""#; @@ -82,10 +82,9 @@ fn package(xml: &str) -> Vec { ("word/_rels/document.xml.rels", docrels.as_slice()), ("word/media/isolation-blue.png", BLUE_PNG), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(bytes).unwrap(); + zip.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } #[test] fn model_nested_picture_roundtrip() { diff --git a/crates/docx/tests/it/east_asian_lang.rs b/crates/docx/tests/it/east_asian_lang.rs index 7188bb56d1..4c122801fc 100644 --- a/crates/docx/tests/it/east_asian_lang.rs +++ b/crates/docx/tests/it/east_asian_lang.rs @@ -1,8 +1,6 @@ //! East Asian language attribute of a run (`w:lang/@w:eastAsia`, ISO/IEC 29500-1 §17.3.2.20). //! Verifies that saving DOCX documents preserves the East Asian language of `w:lang`. -use std::io::{Read, Write}; - fn docx(body: &str, styles: Option<&str>) -> Vec { let root = r#""#; let doc_rels = r#""#; @@ -17,19 +15,17 @@ fn docx(body: &str, styles: Option<&str>) -> Vec { format!(r#"{s}"#), )); } - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, xml) in &parts { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(xml.as_bytes()).unwrap(); + zw.add(name, xml.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn part(bytes: &[u8], name: &str) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name(name).unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read(name, 16 << 20).unwrap().unwrap()).unwrap() } fn run(text: &str, rpr: &str) -> String { diff --git a/crates/docx/tests/it/east_asian_layout.rs b/crates/docx/tests/it/east_asian_layout.rs index 9a53bb12e7..9624fe505b 100644 --- a/crates/docx/tests/it/east_asian_layout.rs +++ b/crates/docx/tests/it/east_asian_layout.rs @@ -2,8 +2,6 @@ //! two lines in one and horizontal in vertical are read and saved again. Fixtures are //! hand-written from the spec. -use std::io::{Read, Write}; - use wordcraft_doc::props::{CombineBrackets, EastAsianLayout}; fn docx(body: &str, styles: Option<&str>) -> Vec { @@ -20,19 +18,17 @@ fn docx(body: &str, styles: Option<&str>) -> Vec { format!(r#"{s}"#), )); } - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, xml) in &parts { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(xml.as_bytes()).unwrap(); + zw.add(name, xml.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn part(bytes: &[u8], name: &str) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name(name).unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read(name, 16 << 20).unwrap().unwrap()).unwrap() } fn run(text: &str, rpr: &str) -> String { diff --git a/crates/docx/tests/it/emphasis_marks.rs b/crates/docx/tests/it/emphasis_marks.rs index 25238d0684..5924e4752b 100644 --- a/crates/docx/tests/it/emphasis_marks.rs +++ b/crates/docx/tests/it/emphasis_marks.rs @@ -1,8 +1,6 @@ //! Emphasis marks (`w:em`, ISO/IEC 29500-1 §17.3.2.12: Font › Emphasis mark) are read and //! saved again, in their place in `w:rPr`. Fixtures are hand-written from the spec. -use std::io::{Read, Write}; - use wordcraft_doc::props::EmphasisMark; fn docx(body: &str) -> Vec { @@ -10,12 +8,11 @@ fn docx(body: &str) -> Vec { let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, xml) in [("_rels/.rels", root.to_string()), ("word/document.xml", doc)] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(xml.as_bytes()).unwrap(); + zw.add(name, xml.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn marks(d: &wordcraft_doc::Document) -> Vec> { @@ -39,9 +36,8 @@ fn emphasis_marks_are_read_and_saved() { let want = [EmphasisMark::Dot, EmphasisMark::Comma, EmphasisMark::Circle, EmphasisMark::UnderDot, EmphasisMark::None].map(Some); assert_eq!(marks(&d), want); let saved = wordcraft_docx::write(&d).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&saved)).unwrap(); - let mut xml = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&saved, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); // In its place: after w:cs, before w:lang. assert!(xml.contains(r#""#; pub fn zip(entries: &[(&str, &[u8])]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn rels(list: &[(&str, &str, &str)]) -> String { @@ -1483,10 +1481,9 @@ fn word_watermark_survives_save() { }; assert_eq!(headers(&d2), headers(&d)); assert_eq!(headers(&d2)[0], vec!["Header label".to_string(), "Second line".to_string()]); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); for name in ["word/header1.xml", "word/header2.xml"] { - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name(name).unwrap(), &mut xml).unwrap(); + let xml = String::from_utf8(z.read(name, 16 << 20).unwrap().unwrap()).unwrap(); assert_eq!(xml.matches(" Vec { /// The entries of a zip, by name. fn unzip(bytes: &[u8]) -> std::collections::BTreeMap> { - use std::io::Read; - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut out = std::collections::BTreeMap::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let mut b = Vec::new(); - f.read_to_end(&mut b).unwrap(); - out.insert(f.name().to_string(), b); + for name in names { + let b = z.read(&name, 16 << 20).unwrap().unwrap(); + out.insert(name, b); } out } @@ -2174,9 +2169,8 @@ fn formatting_revisions_round_trip() { // Written back: each change is the last child of its parent, and reads back the same. let bytes = wordcraft_docx::write(&d).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); for (tag, parent) in [ ("w:rPrChange", ""), ("w:pPrChange", ""), @@ -2432,10 +2426,9 @@ fn note_separators_and_continuation_notice_round_trip() { let out = wordcraft_docx::write(&d).unwrap(); let part = |name: &str| { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&out)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name(name).unwrap(), &mut xml).unwrap(); - xml + let mut z = craft_zip::Archive::open(&out, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read(name, 16 << 20).unwrap().unwrap()).unwrap() }; let notes = part("word/footnotes.xml"); for (kind, id) in [("separator", "-1"), ("continuationSeparator", "0"), ("continuationNotice", "1")] { @@ -2558,13 +2551,12 @@ fn includepicture_keeps_its_embedded_picture() { let bytes = docx(&format!("{field}"), &[("rIdPic", "image", "media/picture.png")], &[]); let bytes = { // `docx` takes text parts; add the picture bytes as a binary part. - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut entries: Vec<(String, Vec)> = Vec::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let mut v = Vec::new(); - std::io::Read::read_to_end(&mut f, &mut v).unwrap(); - entries.push((f.name().to_string(), v)); + for name in names { + let v = z.read(&name, 16 << 20).unwrap().unwrap(); + entries.push((name, v)); } entries.push(("word/media/picture.png".into(), png.to_vec())); zip(&entries.iter().map(|(n, v)| (n.as_str(), v.as_slice())).collect::>()) @@ -2678,9 +2670,8 @@ fn rounded_rectangle_corners_are_read_and_saved() { let d = read_body(&format!("{body}")); assert_eq!(corners(&d), expected); let bytes = wordcraft_docx::write(&d).unwrap(); - let mut xml = String::new(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""#), "{xml}"); assert_eq!(corners(&wordcraft_docx::read(&bytes).unwrap()), expected); } @@ -2745,9 +2736,8 @@ fn arrowheads_keep_their_kind_and_size() { let d = read_body(&format!("{body}")); assert_eq!(ends(&d), expected); let bytes = wordcraft_docx::write(&d).unwrap(); - let mut xml = String::new(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#""#), "{xml}"); assert_eq!(ends(&wordcraft_docx::read(&bytes).unwrap()), expected); @@ -2870,9 +2860,8 @@ fn vml_preset_shape_survives_saving() { r##"{VML_RIGHT_ARROW_TYPE}"## ); let saved = wordcraft_docx::write(&read_body(&body)).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(saved.as_slice())).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&saved, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#"prst="rightArrow""#), "{xml}"); let again = wordcraft_docx::read(&saved).unwrap(); assert!( @@ -2898,9 +2887,8 @@ fn vml_preset_shape_survives_saving() { fn vml_nearest_preset_shape_survives_saving() { let body = r##""##; let saved = wordcraft_docx::write(&read_body(body)).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(saved.as_slice())).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&saved, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#"prst="leftArrow""#), "{xml}"); let again = wordcraft_docx::read(&saved).unwrap(); assert!( @@ -3047,9 +3035,8 @@ fn drawingml_shapes_survive_saving() { let again = wordcraft_docx::read(&bytes).unwrap(); assert_eq!(shapes(&again), read, "every shape survives saving"); assert_eq!(part_text_box(&again), "Floating text box"); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes[..])).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes[..], craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert_eq!(xml.matches("").count(), 7); for p in presets.iter().chain(&["straightConnector1"]) { assert!(xml.contains(&format!(r#""#)), "{p} is written"); @@ -4044,20 +4031,19 @@ fn hyperlink_with_empty_target_is_no_link() { let bytes = docx(body, &[("rId2", "hyperlink", "https://x"), ("rId3", "hyperlink", "mailto:a@example.com")], &[]); // Blank the first target and mark the mailto one external (the helper only does that for http). let bytes = { - let mut zr = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut zr = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = zr.names().map(|n| n.into_owned()).collect(); let mut entries = Vec::new(); - for i in 0..zr.len() { - let mut f = zr.by_index(i).unwrap(); - let mut buf = Vec::new(); - std::io::Read::read_to_end(&mut f, &mut buf).unwrap(); - if f.name() == "word/_rels/document.xml.rels" { + for name in names { + let mut buf = zr.read(&name, 16 << 20).unwrap().unwrap(); + if name == "word/_rels/document.xml.rels" { buf = String::from_utf8(buf) .unwrap() .replace("Target=\"https://x\"", "Target=\"\"") .replace("a@example.com\"", "a@example.com\" TargetMode=\"External\"") .into_bytes(); } - entries.push((f.name().to_string(), buf)); + entries.push((name, buf)); } let refs: Vec<(&str, &[u8])> = entries.iter().map(|(n, b)| (n.as_str(), b.as_slice())).collect(); zip(&refs) diff --git a/crates/docx/tests/it/glossary.rs b/crates/docx/tests/it/glossary.rs index 40e6ea4913..11919a3968 100644 --- a/crates/docx/tests/it/glossary.rs +++ b/crates/docx/tests/it/glossary.rs @@ -3,7 +3,6 @@ //! edited. Hand-written fixtures (from the spec), no files produced by Word. use std::collections::BTreeMap; -use std::io::{Read, Write}; use std::sync::Arc; use wordcraft_doc::{Block, BuildingBlock, CharProps, Document, Paragraph, para_block}; @@ -15,22 +14,20 @@ const GLOSSARY_REL: &str = "http://schemas.openxmlformats.org/officeDocument/200 const GLOSSARY_CT: &str = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.glossary+xml"; fn zip(entries: &[(&str, Vec)]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn unzip(bytes: &[u8]) -> BTreeMap> { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut out = BTreeMap::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let mut b = Vec::new(); - f.read_to_end(&mut b).unwrap(); - assert!(out.insert(f.name().to_string(), b).is_none(), "duplicate part {}", f.name()); + for name in names { + let b = z.read(&name, 16 << 20).unwrap().unwrap(); + assert!(out.insert(name.clone(), b).is_none(), "duplicate part {name}"); } out } diff --git a/crates/docx/tests/it/heading_levels.rs b/crates/docx/tests/it/heading_levels.rs index e32059dce8..1fc2f592b6 100644 --- a/crates/docx/tests/it/heading_levels.rs +++ b/crates/docx/tests/it/heading_levels.rs @@ -2,7 +2,6 @@ //! `styles.xml` doesn't write `w:outlineLvl` (some producers leave it out). Word for Windows, //! opening such a file, reports `OutlineLevel` 1 and 2 for "heading 1" and "heading 2" (whatever //! their style ids) and body text for a custom style. -use std::io::Write; use wordcraft_doc::Paragraph; const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; @@ -12,14 +11,13 @@ fn package(styles: &str, body: &str) -> Vec { let rels = r#""#; let doc = format!(r#"{body}"#); let styles = format!(r#"{styles}"#); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, body) in [("_rels/.rels", root), ("word/document.xml", doc.as_str()), ("word/_rels/document.xml.rels", rels), ("word/styles.xml", styles.as_str())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(body.as_bytes()).unwrap(); + zw.add(name, body.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn para(style: &str, text: &str) -> String { diff --git a/crates/docx/tests/it/legacy_forms.rs b/crates/docx/tests/it/legacy_forms.rs index 8b98f9518e..0df46d81e8 100644 --- a/crates/docx/tests/it/legacy_forms.rs +++ b/crates/docx/tests/it/legacy_forms.rs @@ -2,8 +2,6 @@ //! (`w:gridBefore`/`w:wBefore`, `w:gridAfter`/`w:wAfter`) through a round trip (#1174). All //! markup here is synthetic, written from ECMA-376 §17.16.17 and §17.4.14-17. -use std::io::Write; - use wordcraft_doc::Block; use wordcraft_docx::{read, write}; @@ -12,19 +10,17 @@ const ROOT_RELS: &str = r#"{body}"#); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", ROOT_RELS.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(docx: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(docx)).unwrap(); - let mut s = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(docx, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn resaved(body: &str) -> String { diff --git a/crates/docx/tests/it/ligatures.rs b/crates/docx/tests/it/ligatures.rs index 7648e81ac5..8584d933dd 100644 --- a/crates/docx/tests/it/ligatures.rs +++ b/crates/docx/tests/it/ligatures.rs @@ -1,7 +1,6 @@ //! OpenType ligatures (`w14:ligatures`, [MS-DOCX]) in document defaults, styles and runs. //! Synthetic packages, written here; no external documents are needed. -use std::io::Write; use wordcraft_doc::props::Ligatures; use wordcraft_doc::{Document, Paragraph}; @@ -15,17 +14,16 @@ fn package(defaults: &str, style: &str, run: &str) -> Vec { let styles = format!( r#"{defaults}{style}"# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut z = craft_zip::Writer::new(); for (name, data) in [ ("_rels/.rels", root), ("word/_rels/document.xml.rels", rels), ("word/document.xml", document.as_str()), ("word/styles.xml", styles.as_str()), ] { - z.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn paragraph(d: &Document) -> &Paragraph { @@ -68,10 +66,9 @@ fn unknown_ligature_values_are_ignored() { fn no_ligatures_element_without_a_value() { let d = wordcraft_docx::read(&package("", "", "")).unwrap(); let out = wordcraft_docx::write(&d).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(out)).unwrap(); + let mut z = craft_zip::Archive::open(&out, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); for name in ["word/document.xml", "word/styles.xml"] { - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name(name).unwrap(), &mut xml).unwrap(); + let xml = String::from_utf8(z.read(name, 16 << 20).unwrap().unwrap()).unwrap(); assert!(!xml.contains("ligatures"), "{name}"); } } diff --git a/crates/docx/tests/it/malformed.rs b/crates/docx/tests/it/malformed.rs index a714f27b36..e5e06407ae 100644 --- a/crates/docx/tests/it/malformed.rs +++ b/crates/docx/tests/it/malformed.rs @@ -9,12 +9,11 @@ const W_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingm const ROOT_RELS: &str = r#""#; fn zip(entries: &[(&str, &[u8])]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn doc_zip(document_xml: &[u8]) -> Vec { diff --git a/crates/docx/tests/it/missing_pictures.rs b/crates/docx/tests/it/missing_pictures.rs index a7b065c71c..88247ed012 100644 --- a/crates/docx/tests/it/missing_pictures.rs +++ b/crates/docx/tests/it/missing_pictures.rs @@ -1,18 +1,17 @@ //! Pictures dropped at open are recorded for strict export (#6005): a missing part, an external //! target and a link-only picture (ECMA-376 Part 1 §20.1.8.13 `a:blip` `r:embed`/`r:link`). //! Original synthetic packages; nothing is fetched, the reader has no network code. -use std::io::Write; use wordcraft_doc::{Document, InlineObject, MissingPictureReason}; const NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 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:v="urn:schemas-microsoft-com:vml""#; const REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; fn png() -> Vec { - let mut out = Vec::new(); - image::RgbaImage::from_pixel(2, 2, image::Rgba([20, 90, 200, 255])) - .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png) - .unwrap(); - out + wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(2, 2, image::Rgba([20, 90, 200, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap() } fn blip(attrs: &str) -> String { @@ -32,10 +31,9 @@ fn package(body: &str, doc_rels: &str, header: Option<(&str, &str)>, extra: &[(& ); let drels = format!(r#"{doc_rels}{hdr_rel}"#); let types = r#""#; - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); let mut put = |name: &str, bytes: &[u8]| { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(bytes).unwrap(); + zip.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); }; put("[Content_Types].xml", types.as_bytes()); put("_rels/.rels", rels.as_bytes()); @@ -51,7 +49,7 @@ fn package(body: &str, doc_rels: &str, header: Option<(&str, &str)>, extra: &[(& for (n, b) in extra { put(n, b); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } fn image_rel(id: &str, target: &str, external: bool) -> String { diff --git a/crates/docx/tests/it/nested_block_controls.rs b/crates/docx/tests/it/nested_block_controls.rs index 30a0c272b2..c688000b04 100644 --- a/crates/docx/tests/it/nested_block_controls.rs +++ b/crates/docx/tests/it/nested_block_controls.rs @@ -2,8 +2,6 @@ //! saved, even when it holds a single paragraph (ECMA-376 §17.5.2.29-30; [MS-DOCX] `w15` //! repeating sections, which must wrap whole paragraphs or rows). All markup here is synthetic. -use std::io::Write; - use wordcraft_doc::{ContentControl, Document, StoryRef}; use wordcraft_docx::{read, write}; @@ -13,19 +11,17 @@ const ROOT_RELS: &str = r#"{body}"#); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", ROOT_RELS.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(docx: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(docx)).unwrap(); - let mut s = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(docx, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn t(s: &str) -> String { diff --git a/crates/docx/tests/it/passthrough.rs b/crates/docx/tests/it/passthrough.rs index 6a04043ed6..d6f77083f8 100644 --- a/crates/docx/tests/it/passthrough.rs +++ b/crates/docx/tests/it/passthrough.rs @@ -4,7 +4,6 @@ //! and extended properties. Hand-written fixtures (from the spec), no files produced by Word. use std::collections::BTreeMap; -use std::io::{Read, Write}; use std::sync::Arc; const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; @@ -12,22 +11,20 @@ const R_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relati const REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; fn zip(entries: &[(&str, Vec)]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn unzip(bytes: &[u8]) -> BTreeMap> { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut out = BTreeMap::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let mut b = Vec::new(); - f.read_to_end(&mut b).unwrap(); - assert!(out.insert(f.name().to_string(), b).is_none(), "duplicate part {}", f.name()); + for name in names { + let b = z.read(&name, 16 << 20).unwrap().unwrap(); + assert!(out.insert(name.clone(), b).is_none(), "duplicate part {name}"); } out } diff --git a/crates/docx/tests/it/picture_links.rs b/crates/docx/tests/it/picture_links.rs index e7b5e44c22..46d566739c 100644 --- a/crates/docx/tests/it/picture_links.rs +++ b/crates/docx/tests/it/picture_links.rs @@ -1,16 +1,15 @@ //! A picture's hyperlink (#5267): Word keeps it on the drawing (`wp:docPr/a:hlinkClick`, //! ECMA-376 Part 1 §20.4.2.5) rather than in a `w:hyperlink` around the run. Original //! synthetic packages; the 2×2 picture is generated in code. -use std::io::{Read, Write}; use std::sync::Arc; use wordcraft_doc::{CharProps, Document, InlineObject, Pos}; fn png() -> Vec { - let mut out = Vec::new(); - image::RgbaImage::from_pixel(2, 2, image::Rgba([20, 90, 200, 255])) - .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png) - .unwrap(); - out + wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(2, 2, image::Rgba([20, 90, 200, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap() } /// An inline picture's run; `link` goes into its `wp:docPr`. @@ -28,7 +27,7 @@ fn package(body: &str) -> Vec { let doc_rels = r#""#; let types = r#""#; let png = png(); - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, bytes) in [ ("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", rels.as_bytes()), @@ -36,10 +35,9 @@ fn package(body: &str) -> Vec { ("word/_rels/document.xml.rels", doc_rels.as_bytes()), ("word/media/square.png", png.as_slice()), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(bytes).unwrap(); + zip.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } /// The link on each picture of the first paragraph, in order. @@ -53,10 +51,9 @@ fn picture_links(doc: &Document) -> Vec> { } fn part(bytes: &[u8], name: &str) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name(name).unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read(name, 16 << 20).unwrap().unwrap()).unwrap() } #[test] @@ -109,19 +106,16 @@ fn a_linked_picture_is_saved_with_its_link_on_the_drawing_too() { // The drawing's link alone (as Word writes it) reads back the same. let without_run_link = xml.replace(&format!(r#""#), "").replacen("", "", 1); assert_ne!(without_run_link, xml); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap(); - let mut out = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let name = f.name().to_string(); - let mut data = Vec::new(); - f.read_to_end(&mut data).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); + let mut out = craft_zip::Writer::new(); + for name in names { + let mut data = z.read(&name, 16 << 20).unwrap().unwrap(); if name == "word/document.xml" { data = without_run_link.clone().into_bytes(); } - out.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - out.write_all(&data).unwrap(); + out.add(&name, &data, craft_zip::Compression::Deflated).unwrap(); } - let edited = out.finish().unwrap().into_inner(); + let edited = out.finish().unwrap(); assert_eq!(picture_links(&wordcraft_docx::read(&edited).unwrap())[0], Some("https://example.com/saved".to_string())); } diff --git a/crates/docx/tests/it/positional_tabs.rs b/crates/docx/tests/it/positional_tabs.rs index 0999aaf561..bf39800a64 100644 --- a/crates/docx/tests/it/positional_tabs.rs +++ b/crates/docx/tests/it/positional_tabs.rs @@ -1,7 +1,5 @@ //! Positional tabs (`w:ptab`, ECMA-376 Part 1 §17.3.3.23). Fixtures are hand-written from the spec. -use std::io::{Read, Write}; - use wordcraft_doc::InlineObject; use wordcraft_doc::para::{PTabAlign, PTabBase}; use wordcraft_doc::props::TabLeader; @@ -11,19 +9,17 @@ fn docx(body: &str) -> Vec { let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn ptab(relative_to: &str, alignment: &str, leader: &str) -> String { diff --git a/crates/docx/tests/it/privacy.rs b/crates/docx/tests/it/privacy.rs index cb345ef0d5..854be9e3f7 100644 --- a/crates/docx/tests/it/privacy.rs +++ b/crates/docx/tests/it/privacy.rs @@ -2,28 +2,27 @@ //! (ECMA-376 Part 1 §17.15.1.69–70) are kept on save and honoured by the writer. Synthetic //! fixtures written for these tests from the spec; the names are made up. use std::collections::BTreeMap; -use std::io::{Cursor, Read, Write}; const W_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships""#; const RT: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/"; fn zip(entries: &[(&str, String)]) -> Vec { - let mut z = zip::ZipWriter::new(Cursor::new(Vec::new())); + let mut z = craft_zip::Writer::new(); for (name, text) in entries { - z.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - z.write_all(text.as_bytes()).unwrap(); + z.add(name, text.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn unzip(bytes: &[u8]) -> BTreeMap { - let mut z = zip::ZipArchive::new(Cursor::new(bytes)).unwrap(); + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut parts = BTreeMap::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let mut s = String::new(); - if f.read_to_string(&mut s).is_ok() { - parts.insert(f.name().to_string(), s); + for name in names { + if let Ok(Some(bytes)) = z.read(&name, 16 << 20) + && let Ok(s) = String::from_utf8(bytes) + { + parts.insert(name, s); } } parts diff --git a/crates/docx/tests/it/roundtrip.rs b/crates/docx/tests/it/roundtrip.rs index 64d339a9a1..970804d4cd 100644 --- a/crates/docx/tests/it/roundtrip.rs +++ b/crates/docx/tests/it/roundtrip.rs @@ -63,7 +63,11 @@ fn para_runs(runs: &[(&str, CharProps)]) -> Paragraph { fn tiny_png() -> Vec { let img = image::RgbaImage::from_fn(3, 2, |x, y| image::Rgba([x as u8 * 80, y as u8 * 120, 200, 255])); let mut out = std::io::Cursor::new(Vec::new()); - img.write_to(&mut out, image::ImageFormat::Png).unwrap(); + std::io::Write::write_all( + &mut out, + &wordcraft_geom::picture::encode_format(&image::DynamicImage::from(img.clone()), image::ImageFormat::Png).unwrap(), + ) + .unwrap(); out.into_inner() } @@ -324,9 +328,8 @@ fn tracked_style_definition_change_round_trips() { }); d.body = vec![para_block(Paragraph::with_text("x", CharProps::default()))]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/styles.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/styles.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains("") @@ -773,9 +775,8 @@ fn note_never_references_itself() { d.body = vec![para_block(p)]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/footnotes.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/footnotes.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(!xml.contains("").count(), 1, "{xml}"); @@ -840,13 +841,12 @@ fn word_note_mark_round_trips_once() { let mut doc = d; for pass in 0..2 { let bytes = wordcraft_docx::write(&doc).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); for (file, mark, reference, style) in [ ("word/footnotes.xml", "", "", "{mark}"#)), "pass {pass}: {xml}"); @@ -894,9 +894,8 @@ fn toc_field_wraps_its_entries() { let d = doc_with(blocks); let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); let sdt = xml.find("").expect("TOC content control"); assert!(xml.contains(r#""#), "{xml}"); let (begin, fend) = (xml.find(r#"fldCharType="begin""#).unwrap(), xml.find(r#"fldCharType="end""#).unwrap()); @@ -933,9 +932,8 @@ fn cell_text_direction_round_trips() { let mut d = Document::new(); d.body = vec![Arc::new(Block::Table(t)), para_block(Paragraph::new())]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#""#), "{xml}"); assert_eq!(xml.matches("w:textDirection").count(), 2, "horizontal cells write nothing: {xml}"); @@ -988,9 +986,8 @@ fn toc_field_skips_nested_stories() { d.body = vec![para_block(head), para_block(last), para_block(Paragraph::with_text("Body", CharProps::default()))]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); let boxes: Vec<(usize, usize)> = xml.match_indices("").map(|(a, _)| (a, a + xml[a..].find("").unwrap())).collect(); assert_eq!(boxes.len(), 2, "{xml}"); let in_box = |i: usize| boxes.iter().any(|(a, b)| (*a..*b).contains(&i)); @@ -1054,9 +1051,8 @@ fn tracked_equations_round_trip() { p.insert_object(end, InlineObject::Equation { linear: "z=1".into(), display: false, math: Default::default() }, &plain).unwrap(); d.body = vec![para_block(p.clone())]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""#), "ins directly encloses inline math: {xml}"); assert!( xml.contains(r#""#), @@ -1119,9 +1115,8 @@ fn tracked_paragraph_marks_round_trip() { let last = Paragraph::with_text("plain", CharProps::default()); d.body = vec![para_block(split), para_block(joined), para_block(last)]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#" String { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); - xml + let mut zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } /// #877: a field begin marker's `w:dirty` (the cached result is stale; Word refreshes it on @@ -1446,9 +1440,8 @@ fn drawing_canvas_round_trips() { p.insert_object(end, canvas, &CharProps::default()).unwrap(); d.body = vec![para_block(p)]; let bytes = wordcraft_docx::write(&d).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains("wordprocessingCanvas") && xml.contains("") && xml.contains("") && xml.contains(""), "{xml}"); let r = wordcraft_docx::read(&bytes).unwrap(); let got = paras(&r); @@ -1494,9 +1487,8 @@ fn rotated_drawing_canvas_round_trips() { let end = p.len(); p.insert_object(end, canvas(square), &CharProps::default()).unwrap(); let bytes = wordcraft_docx::write(&doc_with(vec![p])).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!( xml.contains(r#""#), "{xml}" @@ -1524,9 +1516,8 @@ fn table_contextual_spacing_compatibility_flag_round_trips() { assert_eq!(r.settings.compat_mode, mode); if allow { let bytes = wordcraft_docx::write(&d).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/settings.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/settings.xml", 16 << 20).unwrap().unwrap()).unwrap(); // CT_Compat requires the boolean option before compatSetting. assert!(xml.find(" String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name(name).unwrap(), &mut xml).unwrap(); - xml + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read(name, 16 << 20).unwrap().unwrap()).unwrap() } /// Header parts in the document, by id. @@ -2047,9 +2034,8 @@ fn char_border_written_between_u_and_shd() { let c = CharProps { border: Some(Border::single(0.5)), underline: Some(Underline::Single), shading: Some(Rgb(0xFF, 0xFF, 0)), ..Default::default() }; let bytes = wordcraft_docx::write(&doc_with(vec![para_runs(&[("x", c)])])).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); let bdr = r#""#; assert_eq!(xml.matches(bdr).count(), 1, "{xml}"); let at = xml.find(bdr).unwrap(); @@ -2146,9 +2132,8 @@ fn ink_and_freeforms_round_trip_as_custom_geometry() { } let d = doc_with(vec![p]); let bytes = wordcraft_docx::write(&d).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert_eq!(xml.matches("").count(), 3, "{xml}"); assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#""#) && xml.contains(r#"cap="rnd""#), "{xml}"); @@ -2188,9 +2173,8 @@ fn shape_effects_round_trip() { let mut p = Paragraph::with_text("x", CharProps::default()); p.insert_object(1, shape, &CharProps::default()).unwrap(); let bytes = wordcraft_docx::write(&doc_with(vec![p])).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!( xml.contains( r#""# @@ -2281,9 +2265,8 @@ fn rotation_and_flips_round_trip() { } d.body = vec![para_block(p)]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#""#), "{xml}"); @@ -2334,9 +2317,8 @@ fn rotated_group_and_shadow_rotation_round_trip() { let mut p = Paragraph::with_text("g", CharProps::default()); p.insert_object(1, group, &CharProps::default()).unwrap(); let bytes = wordcraft_docx::write(&doc_with(vec![p])).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#"rotWithShape="1""#) && xml.contains(r#"rotWithShape="0""#), "{xml}"); let r = wordcraft_docx::read(&bytes).expect("read"); @@ -2440,9 +2422,8 @@ fn a_corporate_author_round_trips() { ..Default::default() }); let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes[..])).unwrap(); - let mut item_xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("customXml/item1.xml").unwrap(), &mut item_xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes[..], craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let item_xml = String::from_utf8(zip.read("customXml/item1.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(item_xml.contains("World Health Organization"), "{item_xml}"); assert!(item_xml.contains("Harbor Guild, Inc."), "{item_xml}"); assert!(!item_xml.contains("Rivera"), "the corporate author takes the person authors' place: {item_xml}"); @@ -2452,23 +2433,20 @@ fn a_corporate_author_round_trips() { } fn assert_bibliography_parts(bytes: &[u8]) { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - assert!(zip.by_name("customXml/item1.xml").is_ok()); - assert!(zip.by_name("customXml/itemProps1.xml").is_ok()); - assert!(zip.by_name("customXml/_rels/item1.xml.rels").is_ok()); + let mut zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + assert!(zip.contains("customXml/item1.xml")); + assert!(zip.contains("customXml/itemProps1.xml")); + assert!(zip.contains("customXml/_rels/item1.xml.rels")); - let mut rels_xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("_rels/.rels").unwrap(), &mut rels_xml).unwrap(); + let rels_xml = String::from_utf8(zip.read("_rels/.rels", 16 << 20).unwrap().unwrap()).unwrap(); assert!(rels_xml.contains("relationships/customXml"), "{rels_xml}"); assert!(rels_xml.contains("Target=\"customXml/item1.xml\""), "{rels_xml}"); - let mut ct_xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("[Content_Types].xml").unwrap(), &mut ct_xml).unwrap(); + let ct_xml = String::from_utf8(zip.read("[Content_Types].xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(ct_xml.contains("PartName=\"/customXml/item1.xml\""), "{ct_xml}"); assert!(ct_xml.contains("PartName=\"/customXml/itemProps1.xml\""), "{ct_xml}"); - let mut item_xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("customXml/item1.xml").unwrap(), &mut item_xml).unwrap(); + let item_xml = String::from_utf8(zip.read("customXml/item1.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert_bibliography_types(&item_xml); assert_bibliography_people(&item_xml); assert_bibliography_details(&item_xml); @@ -2596,9 +2574,8 @@ fn wrap_polygons_round_trip() { } d.body = vec![para_block(p)]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#""#), "closed: {xml}"); let r = unstacked(wordcraft_docx::read(&bytes).expect("read")); @@ -2635,9 +2612,8 @@ fn enclosed_characters_round_trip_in_words_structure() { } let doc = doc_with(vec![p]); let bytes = wordcraft_docx::write(&doc).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes.clone())).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#" eq \o\ac(○"#), "{xml}"); assert!(!xml.contains("separate") && !xml.contains("enlarge"), "{xml}"); // Shrink text at 11 pt: the character at 7.5 pt (half-points 15), raised. @@ -2670,9 +2646,8 @@ fn ruby_phonetic_guide_round_trips_as_w_ruby() { p.insert_object(5, ruby.clone(), &bold).unwrap(); let d = doc_with(vec![p]); let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); for part in [ r#""#, "", @@ -2717,9 +2692,8 @@ fn wrap_text_sides_round_trip() { } d.body = vec![para_block(p)]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); for tag in [ r#""#, r#""#, @@ -2784,9 +2758,8 @@ fn alt_text_title_and_decorative_round_trip_for_every_object() { t.props.description = Some("Prices by year".into()); d.body = vec![para_block(p), Arc::new(Block::Table(t))]; let bytes = wordcraft_docx::write(&d).expect("write"); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#"{C183D7F6-B498-43B3-948B-1728B52AA6E4}">"#), "{xml}"); assert!( xml.contains(r#""#), @@ -2865,9 +2838,8 @@ fn warps_and_glued_connectors_round_trip() { // Hostile: an unknown warp, a huge adjust value, a glue to a missing shape. let xml = wordcraft_docx::write(&r).unwrap(); - let mut pkg = zip::ZipArchive::new(std::io::Cursor::new(xml)).unwrap(); - let mut doc_xml = String::new(); - std::io::Read::read_to_string(&mut pkg.by_name("word/document.xml").unwrap(), &mut doc_xml).unwrap(); + let mut pkg = craft_zip::Archive::open(&xml, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let doc_xml = String::from_utf8(pkg.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); let doc_xml = doc_xml .replace("prst=\"textArchUp\"", "prst=\"textNotAWarp\"") .replace("a:stCxn id=\"5\" idx=\"3\"", "a:stCxn id=\"404\" idx=\"99999999999\""); @@ -2880,29 +2852,29 @@ fn warps_and_glued_connectors_round_trip() { /// `zip` with part `name` replaced by `data`. fn replace_part(zip_bytes: &[u8], name: &str, data: &[u8]) -> Vec { - let mut src = zip::ZipArchive::new(std::io::Cursor::new(zip_bytes)).unwrap(); - let mut out = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - for i in 0..src.len() { - let mut f = src.by_index(i).unwrap(); - let n = f.name().to_string(); - out.start_file(n.as_str(), zip::write::SimpleFileOptions::default()).unwrap(); + let mut src = craft_zip::Archive::open(zip_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = src.names().map(|n| n.into_owned()).collect(); + let mut out = craft_zip::Writer::new(); + for n in names { if n == name { - std::io::Write::write_all(&mut out, data).unwrap(); + out.add(&n, data, craft_zip::Compression::Deflated).unwrap(); } else { - std::io::copy(&mut f, &mut out).unwrap(); + let bytes = src.read(&n, 16 << 20).unwrap().unwrap(); + out.add(&n, &bytes, craft_zip::Compression::Deflated).unwrap(); } } - out.finish().unwrap().into_inner() + out.finish().unwrap() } /// A texture or picture page background (`v:background/v:fill`) survives a save (#999): the image /// part, its relationship and the fill attributes. #[test] fn page_background_picture_round_trips() { - let mut png = Vec::new(); - image::RgbaImage::from_pixel(4, 4, image::Rgba([200, 150, 100, 255])) - .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png) - .unwrap(); + let png = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(4, 4, image::Rgba([200, 150, 100, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap(); let mut d = Document::new(); d.settings.page_color = Some(Rgb(0xFF, 0xFF, 0xCC)); d.media.insert("paper.png".into(), Arc::new(png.clone())); @@ -2932,9 +2904,8 @@ fn drop_cap_options_round_trip() { margin.props.drop_cap_margin = Some(true); let d = doc_with(vec![dropped, margin]); let bytes = wordcraft_docx::write(&d).expect("write"); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(xml.contains(r#"w:dropCap="drop" w:lines="2" w:hSpace="120""#), "{xml}"); assert!(xml.contains(r#"w:dropCap="margin" w:lines="4""#), "{xml}"); assert!(xml.contains(r#"w:ascii="Liberation Mono""#), "the letter's font on its run: {xml}"); diff --git a/crates/docx/tests/it/row_property_exceptions.rs b/crates/docx/tests/it/row_property_exceptions.rs index ce962c2d8a..4c88ed2767 100644 --- a/crates/docx/tests/it/row_property_exceptions.rs +++ b/crates/docx/tests/it/row_property_exceptions.rs @@ -1,26 +1,22 @@ //! Row-level table property exceptions (`w:tblPrEx` in `w:tr`, ECMA-376 Part 1 §17.4.61). //! Fixtures are hand-written from the spec. -use std::io::{Read, Write}; - fn docx(body: &str) -> Vec { let root = r#""#; let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn row(ex: &str, text: &str) -> String { diff --git a/crates/docx/tests/it/run_props.rs b/crates/docx/tests/it/run_props.rs index 8d718fa0d7..6abf1c7051 100644 --- a/crates/docx/tests/it/run_props.rs +++ b/crates/docx/tests/it/run_props.rs @@ -1,7 +1,5 @@ //! Run and style properties that must survive repeated DOCX saves. -use std::io::{Read, Write}; - fn docx(body: &str, styles: &str) -> Vec { let root = r#""#; let rels = r#""#; @@ -9,24 +7,22 @@ fn docx(body: &str, styles: &str) -> Vec { r#"{body}"# ); let styles = format!(r#"{styles}"#); - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, xml) in [ ("_rels/.rels", root.to_string()), ("word/document.xml", document), ("word/_rels/document.xml.rels", rels.to_string()), ("word/styles.xml", styles), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(xml.as_bytes()).unwrap(); + zip.add(name, xml.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } fn part(bytes: &[u8], name: &str) -> String { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - zip.by_name(name).unwrap().read_to_string(&mut xml).unwrap(); - xml + let mut zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(zip.read(name, 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/docx/tests/it/section_form_protection.rs b/crates/docx/tests/it/section_form_protection.rs index e1bdbdb962..76366a134f 100644 --- a/crates/docx/tests/it/section_form_protection.rs +++ b/crates/docx/tests/it/section_form_protection.rs @@ -1,26 +1,22 @@ //! Section form protection (`w:formProt` in `w:sectPr`, ECMA-376 Part 1 §17.6.6). Fixtures are //! hand-written from the spec. -use std::io::{Read, Write}; - fn docx(body: &str) -> Vec { let root = r#""#; let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/docx/tests/it/section_line_numbering.rs b/crates/docx/tests/it/section_line_numbering.rs index e92abd9dff..31c29bf2ba 100644 --- a/crates/docx/tests/it/section_line_numbering.rs +++ b/crates/docx/tests/it/section_line_numbering.rs @@ -1,7 +1,5 @@ //! Section line numbering (`w:lnNumType` in `w:sectPr`, ECMA-376 Part 1 §17.6.8). -use std::io::{Read, Write}; - use wordcraft_doc::section::{LineNumberRestart, LineNumbering}; /// Builds a minimal in-memory DOCX package archive containing the given XML body. @@ -10,20 +8,18 @@ fn docx(body: &str) -> Vec { let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// Extracts the `word/document.xml` content string from a serialized DOCX package. fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } /// #6209: `` without `w:countBy` leaves line numbering off, as Word does. diff --git a/crates/docx/tests/it/section_metadata_preservation.rs b/crates/docx/tests/it/section_metadata_preservation.rs index c3b10fe8c8..6871703e9f 100644 --- a/crates/docx/tests/it/section_metadata_preservation.rs +++ b/crates/docx/tests/it/section_metadata_preservation.rs @@ -1,25 +1,21 @@ //! Section printer, note and chapter-number metadata survives DOCX and native JSON saves. -use std::io::{Read, Write}; - fn docx(body: &str) -> Vec { let root = r#""#; let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/docx/tests/it/section_page_number_type.rs b/crates/docx/tests/it/section_page_number_type.rs index 3dcf5c31b3..461f1581f7 100644 --- a/crates/docx/tests/it/section_page_number_type.rs +++ b/crates/docx/tests/it/section_page_number_type.rs @@ -1,26 +1,22 @@ //! Section page-number settings (`w:pgNumType` in `w:sectPr`, ECMA-376 Part 1 §17.6.12). //! Fixtures are hand-written from the spec. -use std::io::{Read, Write}; - fn docx(body: &str) -> Vec { let root = r#""#; let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/docx/tests/it/section_text_direction.rs b/crates/docx/tests/it/section_text_direction.rs index da44593bd1..59843830fd 100644 --- a/crates/docx/tests/it/section_text_direction.rs +++ b/crates/docx/tests/it/section_text_direction.rs @@ -1,8 +1,6 @@ //! Section text direction (`w:textDirection` in `w:sectPr`, ECMA-376 Part 1 §17.6.20). Fixtures //! are hand-written from the spec. -use std::io::{Read, Write}; - use wordcraft_doc::props::TextDirection; fn docx(body: &str) -> Vec { @@ -10,16 +8,15 @@ fn docx(body: &str) -> Vec { let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn docx_with_styles(body: &str, styles: &str) -> Vec { - let mut bytes = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut bytes = craft_zip::Writer::new(); let root = r#""#; let document_rels = r#""#; let doc = format!( @@ -31,17 +28,15 @@ fn docx_with_styles(body: &str, styles: &str) -> Vec { ("word/document.xml", doc.as_bytes()), ("word/styles.xml", styles.as_bytes()), ] { - bytes.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - bytes.write_all(value).unwrap(); + bytes.add(name, value, craft_zip::Compression::Deflated).unwrap(); } - bytes.finish().unwrap().into_inner() + bytes.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] @@ -122,8 +117,8 @@ fn paragraph_style_text_direction_is_preserved_and_direct_value_overrides_it() { assert_eq!(xml.matches(r#""#).count(), 2, "both paragraphs keep their style: {xml}"); assert!(xml.contains(r#""#), "direct horizontal is explicit: {xml}"); assert_eq!(xml.matches("w:textDirection").count(), 1, "only the direct paragraph writes a direction: {xml}"); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_docx::write(&d).unwrap())).unwrap(); - let mut styles_xml = String::new(); - z.by_name("word/styles.xml").unwrap().read_to_string(&mut styles_xml).unwrap(); + let _z_bytes = wordcraft_docx::write(&d).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let styles_xml = String::from_utf8(z.read("word/styles.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(styles_xml.contains(r#""#), "style value survives: {styles_xml}"); } diff --git a/crates/docx/tests/it/section_valign.rs b/crates/docx/tests/it/section_valign.rs index 6e000dcc0f..97de698aa5 100644 --- a/crates/docx/tests/it/section_valign.rs +++ b/crates/docx/tests/it/section_valign.rs @@ -1,8 +1,6 @@ //! Section vertical alignment (`w:vAlign` in `w:sectPr`, ECMA-376 Part 1 §17.6.23, values from //! ST_VerticalJc). Fixtures are hand-written from the spec. -use std::io::{Read, Write}; - use wordcraft_doc::section::PageVAlign; fn docx(body: &str) -> Vec { @@ -10,19 +8,17 @@ fn docx(body: &str) -> Vec { let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } /// #1320: "both" (Justified) reads as Justify and writes back as "both"; the others keep theirs. diff --git a/crates/docx/tests/it/settings_merge.rs b/crates/docx/tests/it/settings_merge.rs index c725b5c2d3..a69c3aa525 100644 --- a/crates/docx/tests/it/settings_merge.rs +++ b/crates/docx/tests/it/settings_merge.rs @@ -4,29 +4,26 @@ //! fixtures (from the spec), no files produced by Word. use std::collections::BTreeMap; -use std::io::{Read, Write}; const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; const R_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; const REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; fn zip(entries: &[(&str, Vec)]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn unzip(bytes: &[u8]) -> BTreeMap> { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut out = BTreeMap::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).unwrap(); - let mut b = Vec::new(); - f.read_to_end(&mut b).unwrap(); - assert!(out.insert(f.name().to_string(), b).is_none(), "duplicate part {}", f.name()); + for name in names { + let b = z.read(&name, 16 << 20).unwrap().unwrap(); + assert!(out.insert(name.clone(), b).is_none(), "duplicate part {name}"); } out } diff --git a/crates/docx/tests/it/strict_drawings.rs b/crates/docx/tests/it/strict_drawings.rs index 70d78ae7bd..63b20c2749 100644 --- a/crates/docx/tests/it/strict_drawings.rs +++ b/crates/docx/tests/it/strict_drawings.rs @@ -4,8 +4,6 @@ //! ECMA-376 Part 1, 5th edition (`dml-wordprocessingDrawing.xsd`, Strict and Transitional): each //! drawing is written once and filled in with either set of namespaces. -use std::io::{Read, Write}; - use wordcraft_doc::{Document, InlineObject, PartKind}; const STRICT_NS: &str = r#"xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main" xmlns:r="http://purl.oclc.org/ooxml/officeDocument/relationships" xmlns:wp="http://purl.oclc.org/ooxml/drawingml/wordprocessingDrawing" xmlns:a="http://purl.oclc.org/ooxml/drawingml/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" w:conformance="strict""#; @@ -33,19 +31,17 @@ const TRANSITIONAL: Names = Names { ns: TRANSITIONAL_NS, rels: TRANSITIONAL_RELS, shape: "wps", group: "wpg", canvas: "wpc", content: "w:txbxContent" }; fn zip(entries: &[(&str, &[u8])]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } fn solid(rgb: &str) -> String { diff --git a/crates/docx/tests/it/text_alignment.rs b/crates/docx/tests/it/text_alignment.rs index d72d25ff29..4ceee00c55 100644 --- a/crates/docx/tests/it/text_alignment.rs +++ b/crates/docx/tests/it/text_alignment.rs @@ -3,8 +3,6 @@ //! Word sizes a line by it when a list label is larger than the text, so a save that drops it //! moves every line after. Fixtures are hand-written from the spec. -use std::io::{Read, Write}; - use wordcraft_doc::props::TextAlignment; fn docx(body: &str) -> Vec { @@ -12,19 +10,17 @@ fn docx(body: &str) -> Vec { let doc = format!( r#"{body}"# ); - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/docx/tests/it/text_boxes.rs b/crates/docx/tests/it/text_boxes.rs index a2ac71a6e3..30b752cda3 100644 --- a/crates/docx/tests/it/text_boxes.rs +++ b/crates/docx/tests/it/text_boxes.rs @@ -2,26 +2,22 @@ //! `wps:txbx/@id`, `wps:linkedTxbx`). Fixtures are hand-written from ECMA-376 Part 1 §20.4 //! (DrawingML WordprocessingML drawing) and §21.1.2.1.1 (`bodyPr`). -use std::io::{Read, Write}; - use wordcraft_doc::para::ShapeKind; use wordcraft_doc::props::{TextVert, VAlign}; use wordcraft_doc::{Document, InlineObject, PartKind, TextBody, para_block}; fn zip(entries: &[(&str, &[u8])]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut s = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); - s + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } /// The text boxes' stories in body order, a group's members among them @@ -217,7 +213,15 @@ fn picture_in_box_keeps_its_frame() { let outer = text_box(&mut d, "Label", body); let id = outer.text_box().unwrap(); let mut bytes = std::io::Cursor::new(Vec::new()); - image::RgbaImage::from_pixel(10, 4, image::Rgba([20, 40, 60, 255])).write_to(&mut bytes, image::ImageFormat::Png).unwrap(); + std::io::Write::write_all( + &mut bytes, + &wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(10, 4, image::Rgba([20, 40, 60, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap(), + ) + .unwrap(); let media = d.add_media(bytes.into_inner(), "png"); let image = InlineObject::Image { media: media.clone(), diff --git a/crates/docx/tests/it/toc_ranges.rs b/crates/docx/tests/it/toc_ranges.rs index d384c430a5..c79c2f9a24 100644 --- a/crates/docx/tests/it/toc_ranges.rs +++ b/crates/docx/tests/it/toc_ranges.rs @@ -1,11 +1,10 @@ //! Original field-boundary fixtures from the public OOXML complex-field contract. -use std::io::Write; use wordcraft_doc::{InlineObject, Path, StoryRef}; fn package(body: &str) -> Vec { let xml = format!(r#"{body}"#); - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, value) in [ ("word/document.xml", xml.as_str()), ( @@ -13,10 +12,9 @@ fn package(body: &str) -> Vec { r#""#, ), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(value.as_bytes()).unwrap(); + zip.add(name, value.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } fn start(code: &str) -> String { diff --git a/crates/docx/tests/it/undefined_style.rs b/crates/docx/tests/it/undefined_style.rs index 86cd795812..4274d0f6b7 100644 --- a/crates/docx/tests/it/undefined_style.rs +++ b/crates/docx/tests/it/undefined_style.rs @@ -1,7 +1,6 @@ //! A paragraph whose `w:pStyle` names a style `styles.xml` doesn't define is formatted as Normal, //! as in Word, not with the document defaults alone (#5077). Pandoc writes footers as //! `` without defining Footer. -use std::io::Write; use wordcraft_doc::props::LineSpacing; use wordcraft_doc::{Block, Document, PartKind}; @@ -20,7 +19,7 @@ fn package(normal_id: &str) -> Vec { r#""# ); let rels = r#""#; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, body) in [ ("_rels/.rels", root), ("word/document.xml", doc.as_str()), @@ -28,10 +27,9 @@ fn package(normal_id: &str) -> Vec { ("word/styles.xml", styles.as_str()), ("word/footer1.xml", footer.as_str()), ] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(body.as_bytes()).unwrap(); + zw.add(name, body.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } fn footer_para(d: &Document) -> &wordcraft_doc::Paragraph { diff --git a/crates/docx/tests/it/vml_shapes.rs b/crates/docx/tests/it/vml_shapes.rs index e4f97b08f1..1ebe20ec4c 100644 --- a/crates/docx/tests/it/vml_shapes.rs +++ b/crates/docx/tests/it/vml_shapes.rs @@ -2,18 +2,15 @@ //! `coordsize`/`coordorigin` give them. Fixtures are hand-written from ECMA-376 Part 4 //! (VML, §14.1 `v:group`, `coordsize`, `coordorigin`; §14.2 shapes). -use std::io::Write; - use wordcraft_doc::para::ShapeKind; use wordcraft_doc::{Document, InlineObject, Paragraph}; fn zip(entries: &[(&str, &[u8])]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// A document whose body is `body`. diff --git a/crates/docx/tests/it/word_xml.rs b/crates/docx/tests/it/word_xml.rs index 99b05e865c..08cf427dcc 100644 --- a/crates/docx/tests/it/word_xml.rs +++ b/crates/docx/tests/it/word_xml.rs @@ -20,7 +20,11 @@ fn part_text(doc: &Document, id: Option) -> String { fn tiny_png_b64() -> String { let img = image::RgbaImage::from_fn(2, 2, |x, y| image::Rgba([x as u8 * 100, y as u8 * 100, 50, 255])); let mut out = std::io::Cursor::new(Vec::new()); - img.write_to(&mut out, image::ImageFormat::Png).unwrap(); + std::io::Write::write_all( + &mut out, + &wordcraft_geom::picture::encode_format(&image::DynamicImage::from(img.clone()), image::ImageFormat::Png).unwrap(), + ) + .unwrap(); let b64 = base64::engine::general_purpose::STANDARD.encode(out.into_inner()); // Line breaks inside base64, as the formats write it. b64.as_bytes().chunks(20).map(|c| std::str::from_utf8(c).unwrap()).collect::>().join("\n") diff --git a/crates/docx/tests/it/wordml2003_write.rs b/crates/docx/tests/it/wordml2003_write.rs index 0db8455494..848bac3ab0 100644 --- a/crates/docx/tests/it/wordml2003_write.rs +++ b/crates/docx/tests/it/wordml2003_write.rs @@ -13,7 +13,11 @@ use wordcraft_docx::{XmlKind, read_xml, sniff_xml, write_wordml2003}; fn tiny_png() -> Vec { let img = image::RgbaImage::from_fn(3, 2, |x, y| image::Rgba([x as u8 * 80, y as u8 * 120, 200, 255])); let mut out = std::io::Cursor::new(Vec::new()); - img.write_to(&mut out, image::ImageFormat::Png).unwrap(); + std::io::Write::write_all( + &mut out, + &wordcraft_geom::picture::encode_format(&image::DynamicImage::from(img.clone()), image::ImageFormat::Png).unwrap(), + ) + .unwrap(); out.into_inner() } diff --git a/crates/docx/tests/matrix_save_size.rs b/crates/docx/tests/matrix_save_size.rs index d394183f88..14d4e2a15d 100644 --- a/crates/docx/tests/matrix_save_size.rs +++ b/crates/docx/tests/matrix_save_size.rs @@ -17,7 +17,7 @@ fn ragged_matrix_save_stays_small() { let mut doc = Document::new(); doc.body = vec![para_block(p)]; let bytes = wordcraft_docx::write(&doc).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let size = zip.by_name("word/document.xml").unwrap().size(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let size = zip.declared_size("word/document.xml").unwrap(); assert!(size < 1_000_000, "document.xml is {size} bytes"); } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 1c8b53dc62..8c3c48ed2f 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -59,5 +59,5 @@ rustix = { version = "1.1", default-features = false, features = ["std", "fs"] } [dev-dependencies] wordcraft-doc = { workspace = true, features = ["test-support"] } encoding_rs = { workspace = true } -zip = { workspace = true } +craft-zip = { workspace = true } cfb = { workspace = true } diff --git a/crates/engine/src/cmd/equation/tests.rs b/crates/engine/src/cmd/equation/tests.rs index 6a568fa40a..e3eb384087 100644 --- a/crates/engine/src/cmd/equation/tests.rs +++ b/crates/engine/src/cmd/equation/tests.rs @@ -103,7 +103,6 @@ fn gallery_lists_everything() { /// A session on a DOCX whose only paragraph is `{math}`. fn imported(math: &str) -> Session { - use std::io::Write; let w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; let m = "http://schemas.openxmlformats.org/officeDocument/2006/math"; let parts = [ @@ -117,24 +116,21 @@ fn imported(math: &str) -> Session { ), ("word/document.xml", format!(r#"{math}"#)), ]; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, xml) in parts { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(xml.as_bytes()).unwrap(); + zw.add(name, xml.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - let bytes = zw.finish().unwrap().into_inner(); + let bytes = zw.finish().unwrap(); let mut s = Session::new(wordcraft_docx::read(&bytes).unwrap()); s.run("equation.edit", &json!({})).unwrap(); s } fn document_xml(s: &Session) -> String { - use std::io::Read; let bytes = wordcraft_docx::write(&s.doc).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); - xml + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/engine/src/cmd/insert/tests.rs b/crates/engine/src/cmd/insert/tests.rs index d6f88ccd4f..8ab29ddd28 100644 --- a/crates/engine/src/cmd/insert/tests.rs +++ b/crates/engine/src/cmd/insert/tests.rs @@ -5,8 +5,7 @@ use super::*; #[test] fn a_huge_picture_width_keeps_the_size_finite() { let img = image::RgbaImage::from_pixel(2, 400, image::Rgba([10, 20, 30, 255])); - let mut png = Vec::new(); - image::DynamicImage::ImageRgba8(img).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap(); + let png = wordcraft_geom::picture::encode_format(&image::DynamicImage::ImageRgba8(img).clone(), image::ImageFormat::Png).unwrap(); let mut s = Session::new(wordcraft_doc::Document::new()); let r = s.run("insert.picture", &json!({"data": base64_encode(&png), "width": 3e38})).unwrap(); let (w, h) = (r["width"].as_f64().unwrap(), r["height"].as_f64().unwrap()); @@ -17,8 +16,8 @@ fn a_huge_picture_width_keeps_the_size_finite() { /// A 4 × 4 RGB TIFF that says its strips are JPEG (they aren't): its header still reads. fn jpeg_strip_tiff() -> Vec { - let mut t = Vec::new(); - image::RgbImage::new(4, 4).write_to(&mut std::io::Cursor::new(&mut t), image::ImageFormat::Tiff).unwrap(); + let mut t = + wordcraft_geom::picture::encode_format(&image::DynamicImage::from(image::RgbImage::new(4, 4).clone()), image::ImageFormat::Tiff).unwrap(); assert_eq!(&t[..4], b"II*\0"); let ifd = u32::from_le_bytes(t[4..8].try_into().unwrap()) as usize; let n = usize::from(u16::from_le_bytes([t[ifd], t[ifd + 1]])); @@ -37,8 +36,8 @@ fn tiffs_with_jpeg_strips_are_refused() { let e = s.run("insert.picture", &json!({"data": base64_encode(&tiff)})).unwrap_err().to_string(); assert!(e.contains("TIFF"), "{e}"); assert!(s.doc.media.is_empty()); - let mut plain = Vec::new(); - image::RgbImage::new(4, 4).write_to(&mut std::io::Cursor::new(&mut plain), image::ImageFormat::Tiff).unwrap(); + let plain = + wordcraft_geom::picture::encode_format(&image::DynamicImage::from(image::RgbImage::new(4, 4).clone()), image::ImageFormat::Tiff).unwrap(); s.run("insert.picture", &json!({"data": base64_encode(&plain)})).unwrap(); let e = s.run("picture.change", &json!({"data": base64_encode(&tiff)})).unwrap_err().to_string(); assert!(e.contains("TIFF"), "{e}"); diff --git a/crates/engine/src/cmd/mailings/tests.rs b/crates/engine/src/cmd/mailings/tests.rs index 40034eff7a..720668aa64 100644 --- a/crates/engine/src/cmd/mailings/tests.rs +++ b/crates/engine/src/cmd/mailings/tests.rs @@ -189,22 +189,19 @@ fn merge_end_to_end() { /// A typed list (#240) keeps its column order, and rows may be arrays in that order. #[test] fn recipients_from_a_workbook_ask_for_the_sheet_when_there_are_several() { - use std::io::Write; let cell = |t: &str| format!(r#"{t}"#); let row = |cells: &[&str]| format!("{}", cells.iter().map(|c| cell(c)).collect::()); let content = format!( - r#"{}{}{}{}"#, + r#"{}{}{}{}"#, row(&["n"]), row(&["Name", "", "Name"]), row(&["Ada", "London", "A"]), row(&["Alan", "Wilmslow", "B"]), ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - for (name, body) in [("mimetype", "application/vnd.oasis.opendocument.spreadsheet"), ("content.xml", content.as_str())] { - z.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - z.write_all(body.as_bytes()).unwrap(); - } - let data = super::super::insert::base64_encode(&z.finish().unwrap().into_inner()); + let mut z = craft_zip::Writer::new(); + z.add("mimetype", b"application/vnd.oasis.opendocument.spreadsheet", craft_zip::Compression::Stored).unwrap(); + z.add("content.xml", content.as_bytes(), craft_zip::Compression::Deflated).unwrap(); + let data = super::super::insert::base64_encode(&z.finish().unwrap()); let mut s = Session::new(Document::new()); // Two sheets and none chosen: the names come back and nothing loads. let r = s.run("mailings.recipients", &json!({"data": data})).unwrap(); @@ -593,8 +590,7 @@ fn recipients_from_an_xls_workbook() { let book = wordcraft_formats::sheet::xls_fixture(&[( "收件人", vec![row(&["姓名", "城市", "数量"]), row(&["张三", "北京", "12"]), row(&["李四", "上海", "2"])], - )]) - .unwrap(); + )]); let mut s = Session::new(Document::new()); let r = s.run("mailings.recipients", &json!({"path": "名单.xls", "data": b64(&book)})).unwrap(); assert_eq!(r, json!({"fields": ["姓名", "城市", "数量"], "records": 2, "sheet": "收件人"})); diff --git a/crates/engine/src/cmd/objects/picture.rs b/crates/engine/src/cmd/objects/picture.rs index a254e0a762..65f0f0386b 100644 --- a/crates/engine/src/cmd/objects/picture.rs +++ b/crates/engine/src/cmd/objects/picture.rs @@ -194,8 +194,7 @@ pub(super) fn adjust_media( e => CmdError::Failed(format!("can't decode the picture: {e}")), })?; let out = f(img)?; - let mut png = Vec::new(); - out.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| CmdError::Failed(e.to_string()))?; + let png = wordcraft_geom::picture::encode_png(&out).map_err(CmdError::Failed)?; let key = s.doc.add_media(png, "png"); // Chain onto the ultimate original (the base's, or the current picture's when the base // is itself derived), so Reset Picture always finds it. diff --git a/crates/engine/src/cmd/objects/tests.rs b/crates/engine/src/cmd/objects/tests.rs index 5758eb0290..1e71751ca5 100644 --- a/crates/engine/src/cmd/objects/tests.rs +++ b/crates/engine/src/cmd/objects/tests.rs @@ -5,9 +5,8 @@ fn png(w: u32, h: u32) -> Vec { let img = image::RgbaImage::from_fn(w, h, |x, y| { if x > 2 && x < w - 3 && y > 2 && y < h - 3 { image::Rgba([200, 30, 30, 255]) } else { image::Rgba([255, 255, 255, 255]) } }); - let mut b = Vec::new(); - image::DynamicImage::ImageRgba8(img).write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png).unwrap(); - b + + wordcraft_geom::picture::encode_format(&image::DynamicImage::ImageRgba8(img).clone(), image::ImageFormat::Png).unwrap() } /// The laid-out area of the only picture or shape (not a text box). fn object_area(s: &mut Session) -> wordcraft_layout::hit::ObjectHit { @@ -451,8 +450,9 @@ fn remove_background_keeps_a_small_stack() { /// 1 × 2,000,000 picture Thick Frame asked for about 1.8 TB and aborted the process. #[test] fn picture_styles_and_borders_check_the_size_they_draw() { - let mut thin = Vec::new(); - image::GrayImage::new(1, 2_000_000).write_to(&mut std::io::Cursor::new(&mut thin), image::ImageFormat::Png).unwrap(); + let thin = + wordcraft_geom::picture::encode_format(&image::DynamicImage::from(image::GrayImage::new(1, 2_000_000).clone()), image::ImageFormat::Png) + .unwrap(); let mut s = Session::new(wordcraft_doc::Document::new()); s.run("insert.picture", &json!({"data": super::super::insert::base64_encode(&thin)})).unwrap(); let media = s.doc.media.len(); @@ -526,7 +526,7 @@ fn transparency_is_absolute_from_the_original() { let (_, o) = selected(s).unwrap(); let InlineObject::Image { media, .. } = o else { panic!("a picture") }; let bytes = s.doc.media.get(&media).cloned().unwrap(); - image::load_from_memory(&bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect::>() + wordcraft_geom::picture::decode_memory(&bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect::>() }; let alpha = |s: &Session| rgba(s).iter().map(|p| p[3]).collect::>(); let orig = rgba(&s); @@ -579,7 +579,7 @@ fn picture_edits_replace_the_file_corrections() { let (_, o) = selected(s).unwrap(); let InlineObject::Image { media, fx, .. } = o else { panic!("a picture") }; let bytes = s.doc.media.get(&media).cloned().unwrap(); - (media, fx, image::load_from_memory(&bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect::>()) + (media, fx, wordcraft_geom::picture::decode_memory(&bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect::>()) }; let (orig_media, _, orig) = state(&s); // Transparency 50%: 50% (not 75%), the file's transparency dropped, its lum kept. @@ -670,7 +670,7 @@ fn picture_styles_borders_and_effects_replace_the_last_one() { let bitmap = |s: &Session| { let (_, o) = selected(s).unwrap(); let InlineObject::Image { media, .. } = o else { panic!("a picture") }; - image::load_from_memory(&s.doc.media.get(&media).cloned().unwrap()).unwrap().to_rgba8() + wordcraft_geom::picture::decode_memory(&s.doc.media.get(&media).cloned().unwrap()).unwrap().to_rgba8() }; let after = |cmds: &[(&str, Value)]| { let mut s = picture(); @@ -1274,9 +1274,9 @@ fn arrange_bounds_on_header_picture_writes_page_anchored_odt_frame() { assert_eq!((float.h_rel, float.v_rel), (Anchor::Page, Anchor::Page)); assert_eq!((float.x, float.y), (0.0, 0.0)); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_formats::odt::export(&s.doc).unwrap())).unwrap(); - let mut styles = String::new(); - std::io::Read::read_to_string(&mut z.by_name("styles.xml").unwrap(), &mut styles).unwrap(); + let _z_bytes = wordcraft_formats::odt::export(&s.doc).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let styles = String::from_utf8(z.read("styles.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(styles.contains(r#"text:anchor-type="page""#), "frame should be page-anchored: {styles}"); assert!(styles.contains(r#"svg:x="0pt""#) && styles.contains(r#"svg:y="0pt""#), "offsets should be 0pt: {styles}"); assert!(!styles.contains(r#"text:anchor-type="paragraph""#), "frame should not be paragraph-anchored: {styles}"); @@ -1288,9 +1288,9 @@ fn arrange_bounds_on_header_picture_writes_page_anchored_odt_frame() { assert_eq!((float.h_rel, float.v_rel), (Anchor::Page, Anchor::Page)); assert_eq!((float.x, float.y), (10.0, 20.0)); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_formats::odt::export(&s.doc).unwrap())).unwrap(); - let mut styles = String::new(); - std::io::Read::read_to_string(&mut z.by_name("styles.xml").unwrap(), &mut styles).unwrap(); + let _z_bytes = wordcraft_formats::odt::export(&s.doc).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let styles = String::from_utf8(z.read("styles.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(styles.contains(r#"text:anchor-type="page""#), "{styles}"); assert!(styles.contains(r#"svg:x="10pt""#) && styles.contains(r#"svg:y="20pt""#), "{styles}"); } @@ -1325,9 +1325,9 @@ fn a_moved_body_picture_is_placed_by_its_paragraph_even_when_fixed_to_the_page() let hit = s.layout().object(&pos, 0).expect("laid out"); assert!((hit.rect.x - 50.0).abs() < 1.0 && (hit.rect.y - 80.0).abs() < 1.0, "dropped: {:?}", hit.rect); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_formats::odt::export(&s.doc).unwrap())).unwrap(); - let mut content = String::new(); - std::io::Read::read_to_string(&mut z.by_name("content.xml").unwrap(), &mut content).unwrap(); + let _z_bytes = wordcraft_formats::odt::export(&s.doc).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let content = String::from_utf8(z.read("content.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(content.contains(r#"text:anchor-type="paragraph""#), "body picture should export with paragraph anchor: {content}"); assert!(!content.contains("anchor-page-number"), "{content}"); } @@ -1606,7 +1606,6 @@ fn mirrored_indents_make_no_extra_column() { /// A Word file whose only paragraph holds a group of a rectangle and Word's own ink (with its /// picture fallback). fn group_with_word_ink() -> Vec { - use std::io::Write; let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk""#; let rect = r#""#; let ink = r#""#; @@ -1626,14 +1625,12 @@ fn group_with_word_ink() -> Vec { ), ("word/ink/ink1.xml", r#"0 0, 1 1"#.to_string()), ]; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, xml) in parts { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(xml.as_bytes()).unwrap(); + zw.add(name, xml.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.start_file("word/media/preview.png", zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(&png(8, 4)).unwrap(); - zw.finish().unwrap().into_inner() + zw.add("word/media/preview.png", &png(8, 4), craft_zip::Compression::Deflated).unwrap(); + zw.finish().unwrap() } fn ink_members(doc: &wordcraft_doc::Document) -> usize { @@ -1653,7 +1650,6 @@ fn ink_members(doc: &wordcraft_doc::Document) -> usize { /// yet), so open → Ungroup → save → reopen keeps the ink in the group with its part. #[test] fn ungroup_refuses_a_group_holding_word_ink_and_the_ink_survives_saving() { - use std::io::Read; let mut s = Session::new(wordcraft_docx::read(&group_with_word_ink()).unwrap()); assert_eq!(ink_members(&s.doc), 1, "the fixture's ink is read into the group"); let pos = Pos::body(0, 0); @@ -1666,16 +1662,14 @@ fn ungroup_refuses_a_group_holding_word_ink_and_the_ink_survives_saving() { let err = ungroup(&mut s, &json!({})).unwrap_err(); assert!(matches!(err, CmdError::Disabled(_)), "{err:?}"); let bytes = wordcraft_docx::write(&s.doc).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes.clone())).unwrap(); - let mut xml = String::new(); - z.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert_eq!(xml.matches(""), "{xml}"); assert_eq!(bytes, before, "the refused ungroup leaves the document unchanged"); - let names: Vec = z.file_names().map(String::from).collect(); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); assert_eq!(names.iter().filter(|n| n.starts_with("word/ink/")).count(), 1, "{names:?}"); - let mut rels = String::new(); - z.by_name("word/_rels/document.xml.rels").unwrap().read_to_string(&mut rels).unwrap(); + let rels = String::from_utf8(z.read("word/_rels/document.xml.rels", 16 << 20).unwrap().unwrap()).unwrap(); assert_eq!(rels.matches("/customXml\"").count(), 1, "one ink relationship: {rels}"); let back = wordcraft_docx::read(&bytes).unwrap(); assert_eq!(ink_members(&back), 1); diff --git a/crates/engine/src/cmd/references/tests.rs b/crates/engine/src/cmd/references/tests.rs index b2838cd9f9..1462f6600f 100644 --- a/crates/engine/src/cmd/references/tests.rs +++ b/crates/engine/src/cmd/references/tests.rs @@ -56,7 +56,7 @@ fn docx_styleref_header_shows_each_page_s_chapter() { ); let root = r#""#; let rels = r#""#; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, text) in [ ("_rels/.rels", root), ("word/document.xml", body.as_str()), @@ -64,10 +64,9 @@ fn docx_styleref_header_shows_each_page_s_chapter() { ("word/header1.xml", header.as_str()), ("word/styles.xml", styles.as_str()), ] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - std::io::Write::write_all(&mut zw, text.as_bytes()).unwrap(); + zw.add(name, text.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - let bytes = zw.finish().unwrap().into_inner(); + let bytes = zw.finish().unwrap(); let headers = |s: &mut Session| -> Vec { s.layout() .pages @@ -714,12 +713,11 @@ fn output_numbers_text_box_captions_where_their_boxes_are_anchored() { let body: String = paras.iter().map(|p| format!("{p}")).collect(); let document = format!("{body}"); let root = r#""#; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, text) in [("_rels/.rels", root), ("word/document.xml", document.as_str())] { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - std::io::Write::write_all(&mut zw, text.as_bytes()).unwrap(); + zw.add(name, text.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - let s = Session::new(crate::io::open_bytes("boxes.docx", &zw.finish().unwrap().into_inner()).unwrap()); + let s = Session::new(crate::io::open_bytes("boxes.docx", &zw.finish().unwrap()).unwrap()); let out = crate::cmd::references::output_fields(&s.doc).expect("fresh numbers"); // The SEQ and REF results in reading order, text boxes where they are anchored. let mut results = Vec::new(); @@ -774,12 +772,11 @@ fn output_shows_references_to_bookmarks_outside_the_body() { ("word/endnotes.xml", endnotes), ("word/comments.xml", comments), ]; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, text) in &files { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - std::io::Write::write_all(&mut zw, text.as_bytes()).unwrap(); + zw.add(name, text.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - let s = Session::new(crate::io::open_bytes("stories.docx", &zw.finish().unwrap().into_inner()).unwrap()); + let s = Session::new(crate::io::open_bytes("stories.docx", &zw.finish().unwrap()).unwrap()); let out = crate::cmd::references::output_fields(&s.doc).expect("fresh references"); let mut results = Vec::new(); for path in out.para_paths(StoryRef::Body) { diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index b758ec3e77..4f149d8eda 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -468,7 +468,7 @@ pub fn save_bytes_opts(name: &str, doc: &Document, opts: &ExportOptions) -> Resu pub fn save_bytes_with(name: &str, doc: &Document, password: Option<&str>, opts: &ExportOptions) -> Result, String> { let bytes = save_bytes_opts(name, doc, opts)?; match password { - Some(pw) if matches!(ext_of(name).as_str(), "odt" | "ott") => wordcraft_formats::odt::encrypt(&bytes, pw), + Some(pw) if matches!(ext_of(name).as_str(), "odt" | "ott") => wordcraft_formats::odt::encrypt(&bytes, pw).map_err(|e| e.to_string()), Some(pw) if can_encrypt(name) => wordcraft_docx::encrypt(&bytes, pw).map_err(|e| e.to_string()), _ => Ok(bytes), } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a05053b78b..454a5b21e7 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -4271,7 +4271,6 @@ fn toc_menu_offers_update_field_after_save_and_reopen() { /// doesn't grow the document. #[test] fn word_toc_control_with_the_field_end_after_the_entries_survives_update_and_save() { - use std::io::{Read, Write}; let style = |st: &str, inner: &str| format!(r#"{inner}"#); let t = |s: &str| format!(r#"{s}"#); let begin = r#" TOC \o "1-3" \h \z \u "#; @@ -4289,7 +4288,7 @@ fn word_toc_control_with_the_field_end_after_the_entries_survives_update_and_sav style("Heading1", &t("Chapter One")), style("Heading2", &t("Section")) ); - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, value) in [ ("word/document.xml", xml.as_str()), ( @@ -4297,16 +4296,21 @@ fn word_toc_control_with_the_field_end_after_the_entries_survives_update_and_sav r#""#, ), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(value.as_bytes()).unwrap(); + zip.add(name, value.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - let mut s = Session::new(wordcraft_docx::read(&zip.finish().unwrap().into_inner()).unwrap()); + let mut s = Session::new(wordcraft_docx::read(&zip.finish().unwrap()).unwrap()); let mut blocks = None; for round in 0..3 { run(&mut s, "references.updateToc", json!({})); let bytes = wordcraft_docx::write(&s.doc).unwrap(); - let mut xml = String::new(); - zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap().by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let xml = String::from_utf8( + craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }) + .unwrap() + .read("word/document.xml", 16 << 20) + .unwrap() + .unwrap(), + ) + .unwrap(); assert_eq!(xml.matches("").count(), 1, "round {round}: {xml}"); let (open, close) = (xml.find("").unwrap(), xml.find("").unwrap()); assert!(open < xml.find(">Contents<").unwrap(), "round {round}: the heading is in the control: {xml}"); @@ -4328,11 +4332,10 @@ fn style_xml(id: &str, name: &str, ppr: &str, rpr: &str) -> String { /// A DOCX package with `body` and the paragraph `styles` after a default Normal style. fn docx_with_styles(body: &str, styles: &str) -> Vec { - use std::io::Write; const W: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; const RELS: &str = r#"xmlns="http://schemas.openxmlformats.org/package/2006/relationships""#; const TYPE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, value) in [ ("word/document.xml", format!(r#"{body}"#)), ( @@ -4350,10 +4353,9 @@ fn docx_with_styles(body: &str, styles: &str) -> Vec { format!(r#""#), ), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(value.as_bytes()).unwrap(); + zip.add(name, value.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } /// #3891: Update Table on a TOC Word wrote in Portuguese keeps the formatting of its TOC styles, @@ -4671,12 +4673,8 @@ fn update_toc_clears_stale_field_result_for_docx_span() { // DOCX round-trip: entries must be enclosed within the TOC field rather than outside it. let bytes = wordcraft_docx::write(&s.doc).unwrap(); let xml = { - let cursor = std::io::Cursor::new(bytes); - let mut archive = zip::ZipArchive::new(cursor).unwrap(); - let mut file = archive.by_name("word/document.xml").unwrap(); - let mut s = String::new(); - std::io::Read::read_to_string(&mut file, &mut s).unwrap(); - s + let mut archive = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + String::from_utf8(archive.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() }; let b = xml.find(r#"w:fldCharType="begin""#).expect("TOC begin marker present"); // The TOC's end is the last one: the entries' page numbers are `PAGEREF` fields inside it. @@ -4900,9 +4898,8 @@ fn toc_page_numbers_are_pageref_fields() { let expected = text(&s); assert!(expected.contains("Alpha\t2") && expected.contains("Bravo\t3"), "{expected}"); let bytes = crate::io::save_bytes("toc.docx", &s.doc).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert_eq!(xml.matches("PAGEREF _Toc").count(), 2, "{xml}"); let mut s = Session::new(crate::io::open_bytes("toc.docx", &bytes).unwrap()); run(&mut s, "references.updateFields", json!({})); @@ -5549,17 +5546,15 @@ fn an_odt_frame_moved_from_its_page_loses_the_page_number() { let content = format!( r#"On itOneTwo"# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - std::io::Write::write_all(&mut z, data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let odt = z.finish().unwrap().into_inner(); + let odt = z.finish().unwrap(); let frame_tag = |s: &Session| { - let mut a = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_formats::odt::export(&s.doc).unwrap())).unwrap(); - let mut c = String::new(); - std::io::Read::read_to_string(&mut a.by_name("content.xml").unwrap(), &mut c).unwrap(); + let _a_bytes = wordcraft_formats::odt::export(&s.doc).unwrap(); + let mut a = craft_zip::Archive::open(&_a_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let c = String::from_utf8(a.read("content.xml", 16 << 20).unwrap().unwrap()).unwrap(); c.split("').next()).unwrap_or_default().to_string() }; let mut s = Session::new(crate::io::open_bytes("t.odt", &odt).unwrap()); @@ -6033,10 +6028,11 @@ fn html_pictures_load_relative_to_the_file() { // Issue #98: `` beside an HTML file is embedded when it is opened. let dir = std::env::temp_dir().join(format!("wordcraft-html-img-{}", std::process::id())); std::fs::create_dir_all(dir.join("img")).unwrap(); - let mut png = Vec::new(); - image::RgbaImage::from_pixel(4, 2, image::Rgba([10, 20, 30, 255])) - .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png) - .unwrap(); + let png = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(4, 2, image::Rgba([10, 20, 30, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap(); std::fs::write(dir.join("img/my logo.png"), &png).unwrap(); std::fs::write(dir.join("secret.txt"), b"not a picture").unwrap(); let html = r#"

Logo

T

Web

"#; @@ -6060,11 +6056,11 @@ fn scratch_dir(name: &str) -> std::path::PathBuf { #[cfg(not(target_arch = "wasm32"))] fn tiny_png() -> Vec { - let mut png = Vec::new(); - image::RgbaImage::from_pixel(2, 2, image::Rgba([200, 30, 30, 255])) - .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png) - .unwrap(); - png + wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(2, 2, image::Rgba([200, 30, 30, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap() } #[cfg(not(target_arch = "wasm32"))] @@ -8276,7 +8272,7 @@ fn insert_icon_places_a_picture_of_the_requested_size_that_saves_and_recolours() assert_eq!((r["width"].as_f64(), r["height"].as_f64(), r["icon"].as_str()), (Some(48.0), Some(48.0), Some("heart"))); let Some((_, InlineObject::Image { media, w, h, alt, .. })) = crate::cmd::objects::selected(&s) else { panic!("no picture") }; assert_eq!((w, h, alt.as_str()), (48.0, 48.0, "Heart")); - let png = image::load_from_memory(&s.doc.media[&media]).unwrap().to_rgba8(); + let png = wordcraft_geom::picture::decode_memory(&s.doc.media[&media]).unwrap().to_rgba8(); assert_eq!(png.dimensions(), (200, 200), "about 300 pixels per inch"); let inked: Vec<_> = png.pixels().filter(|p| p.0[3] == 255).collect(); assert!(!inked.is_empty() && inked.iter().all(|p| p.0[..3] == [0xC0, 0, 0])); @@ -8288,7 +8284,7 @@ fn insert_icon_places_a_picture_of_the_requested_size_that_saves_and_recolours() // Icon Color repaints it, keeping the transparency. run(&mut s, "picture.iconColor", json!({"color": "1F4E79"})); let Some((_, InlineObject::Image { media: m2, .. })) = crate::cmd::objects::selected(&s) else { panic!("no picture") }; - let png2 = image::load_from_memory(&s.doc.media[&m2]).unwrap().to_rgba8(); + let png2 = wordcraft_geom::picture::decode_memory(&s.doc.media[&m2]).unwrap().to_rgba8(); assert!(png2.pixels().zip(png.pixels()).all(|(a, b)| a.0[3] == b.0[3] && (a.0[3] == 0 || a.0[..3] == [0x1F, 0x4E, 0x79]))); assert!(s.run("picture.iconColor", &json!({})).is_err()); } @@ -9839,7 +9835,6 @@ fn english_dialects_survive_a_docx_round_trip() { if no_language_data("english_dialects_survive_a_docx_round_trip") { return; } - use std::io::Read; let mut s = s(); run(&mut s, "document.setText", json!({"text": "Colour and color. Centre and center."})); run(&mut s, "select.all", json!({})); @@ -9849,12 +9844,8 @@ fn english_dialects_survive_a_docx_round_trip() { let before = spelling_with_first_suggestion(&mut s); assert_eq!(before, pairs(&[("color", "colour"), ("Centre", "Center")])); let bytes = crate::io::save_bytes("british.docx", &s.doc).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut part = |name: &str| { - let mut xml = String::new(); - zip.by_name(name).unwrap().read_to_string(&mut xml).unwrap(); - xml - }; + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let mut part = |name: &str| String::from_utf8(zip.read(name, 16 << 20).unwrap().unwrap()).unwrap(); assert!(part("word/styles.xml").contains(r#"w:val="en-GB""#), "the default language is written"); assert!(part("word/document.xml").contains(r#"w:val="en-US""#), "the run's language is written"); let doc = crate::io::open_bytes("british.docx", &bytes).unwrap(); @@ -10335,7 +10326,6 @@ fn select_graphic(s: &mut Session) { /// reopens as the same editable graphic; an edited data part stays read-only. #[test] fn smart_art_round_trips_through_docx() { - use std::io::Read; let mut s = s(); let r = run( &mut s, @@ -10345,12 +10335,11 @@ fn smart_art_round_trips_through_docx() { assert_eq!(r["smartArt"]["layout"], "hierarchy"); let spec = cmd::smart_art::selected_smart_art(&s).expect("selected after insert"); let bytes = crate::io::save_bytes("sa.docx", &s.doc).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); for part in ["data1", "layout1", "quickStyle1", "colors1", "drawing1"] { - assert!(zip.by_name(&format!("word/diagrams/{part}.xml")).is_ok(), "{part} written"); + assert!(zip.contains(&format!("word/diagrams/{part}.xml")), "{part} written"); } - let mut ct = String::new(); - zip.by_name("[Content_Types].xml").unwrap().read_to_string(&mut ct).unwrap(); + let ct = String::from_utf8(zip.read("[Content_Types].xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(ct.contains("diagramDrawing+xml") && ct.contains("diagramData+xml"), "{ct}"); let mut s2 = Session::new(crate::io::open_bytes("sa.docx", &bytes).unwrap()); select_graphic(&mut s2); @@ -10362,20 +10351,17 @@ fn smart_art_round_trips_through_docx() { select_graphic(&mut s3); assert_eq!(cmd::smart_art::selected_smart_art(&s3).map(|g| g.layout), Some(wordcraft_doc::smart_art::SmartArtLayout::Venn)); // A data part another program rewrote: shown, not editable. - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&again)).unwrap(); - let mut out = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - for i in 0..zip.len() { - let mut f = zip.by_index(i).unwrap(); - let name = f.name().to_string(); - let mut buf = Vec::new(); - f.read_to_end(&mut buf).unwrap(); + let mut zip = craft_zip::Archive::open(&again, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let names: Vec = zip.names().map(|n| n.into_owned()).collect(); + let mut out = craft_zip::Writer::new(); + for name in names { + let mut buf = zip.read(&name, 16 << 20).unwrap().unwrap(); if name.ends_with("data1.xml") { buf = String::from_utf8(buf).unwrap().replace("", "").into_bytes(); } - out.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - std::io::Write::write_all(&mut out, &buf).unwrap(); + out.add(&name, &buf, craft_zip::Compression::Deflated).unwrap(); } - let foreign = out.finish().unwrap().into_inner(); + let foreign = out.finish().unwrap(); let mut s4 = Session::new(crate::io::open_bytes("sa.docx", &foreign).unwrap()); select_graphic(&mut s4); assert!(cmd::smart_art::selected_smart_art(&s4).is_none()); @@ -10641,10 +10627,11 @@ fn outline_many_collapsed_headings_stay_fast_after_edits() { #[test] fn imported_pictures_fit_the_text_width() { let png = |w: u32, h: u32| { - let mut b = Vec::new(); - image::RgbaImage::from_pixel(w, h, image::Rgba([200, 30, 30, 255])) - .write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png) - .unwrap(); + let b = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(w, h, image::Rgba([200, 30, 30, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap(); cmd::insert::base64_encode(&b) }; let sizes = |doc: &wordcraft_doc::Document| -> Vec<(f32, f32)> { @@ -11245,8 +11232,11 @@ fn picture_change_keeps_the_replacement_format() { let dir = scratch_dir("picture-change-format"); let first = dir.join("first.png"); std::fs::write(&first, tiny_png()).unwrap(); - let mut jpeg = Vec::new(); - image::RgbImage::from_pixel(2, 2, image::Rgb([30, 30, 200])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap(); + let jpeg = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbImage::from_pixel(2, 2, image::Rgb([30, 30, 200])).clone()), + image::ImageFormat::Jpeg, + ) + .unwrap(); let second = dir.join("second.jpg"); std::fs::write(&second, &jpeg).unwrap(); @@ -11271,10 +11261,16 @@ fn picture_change_keeps_the_replacement_format() { #[test] fn picture_change_keeps_width_and_takes_new_aspect() { let dir = scratch_dir("picture-change-aspect"); - let mut wide = Vec::new(); - image::RgbImage::from_pixel(40, 20, image::Rgb([200, 30, 30])).write_to(&mut std::io::Cursor::new(&mut wide), image::ImageFormat::Png).unwrap(); - let mut tall = Vec::new(); - image::RgbImage::from_pixel(30, 60, image::Rgb([30, 30, 200])).write_to(&mut std::io::Cursor::new(&mut tall), image::ImageFormat::Png).unwrap(); + let wide = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbImage::from_pixel(40, 20, image::Rgb([200, 30, 30])).clone()), + image::ImageFormat::Png, + ) + .unwrap(); + let tall = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbImage::from_pixel(30, 60, image::Rgb([30, 30, 200])).clone()), + image::ImageFormat::Png, + ) + .unwrap(); let (first, second) = (dir.join("wide.png"), dir.join("tall.png")); std::fs::write(&first, &wide).unwrap(); std::fs::write(&second, &tall).unwrap(); @@ -11407,7 +11403,6 @@ fn links_to_places_in_this_document() { /// formula field's result is edited (#1037). The first field's result is split across the field /// end; the second has its whole result deleted. fn tracked_field_result_docx() -> Vec { - use std::io::Write; const WHO: &str = r#"w:author="Tester" w:date="2026-10-12T00:00:00Z""#; let field = |instr: &str, inside: &str, after: &str| { format!( @@ -11421,7 +11416,7 @@ fn tracked_field_result_docx() -> Vec { let xml = format!( r#"{p1}{p2}"# ); - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, value) in [ ("word/document.xml", xml.as_str()), ( @@ -11429,10 +11424,9 @@ fn tracked_field_result_docx() -> Vec { r#""#, ), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(value.as_bytes()).unwrap(); + zip.add(name, value.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } #[test] @@ -11446,9 +11440,8 @@ fn tracked_deletion_inside_a_field_result_is_a_revision_and_is_saved() { assert_eq!(p.text.replace(wordcraft_doc::para::OBJ, ""), "$150.00$140.00"); // Saving keeps the deletion inside the result, with its text as delText. let bytes = wordcraft_docx::write(&doc).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); - let mut xml = String::new(); - std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let mut z = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); let sep = xml.find(r#"w:fldCharType="separate""#).unwrap(); let end = xml.find(r#"w:fldCharType="end""#).unwrap(); let inside = &xml[sep..end]; @@ -12053,13 +12046,11 @@ fn opened_documents_with_their_own_page_size_keep_it() { fn html_converted_to_docx_keeps_bold_headers_and_right_aligned_cells() { // #1381: `wordcraft-cli convert S05.html out.docx`: the runs are bold and the // `text-align:right` cells' paragraphs carry `w:jc w:val="right"`. - use std::io::Read; let mut s = s(); run(&mut s, "file.open", json!({"path": "S05.html", "data": crate::cmd::insert::base64_encode(SAMPLE_1381.as_bytes())})); let bytes = crate::io::save_bytes("out.docx", &s.doc).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); - let mut xml = String::new(); - zip.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap(); let cell = |text: &str| { let at = xml.find(&format!(">{text}<")).unwrap_or_else(|| panic!("{text} in {xml}")); let start = xml[..at].rfind("").or_else(|| xml[..at].rfind("{who}2024-03-01T10:00:00"); let content = format!( @@ -12212,13 +12202,11 @@ fn odt_inserted_then_deleted_text_round_trips() { info("Alice"), info("Bob") ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap().into_inner()).unwrap(); + let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap()).unwrap(); let doc = crate::io::open_bytes("t.odt", &wordcraft_formats::odt::export(&doc).unwrap()).unwrap(); for cmd in ["review.rejectAll", "review.acceptAll"] { let mut s = Session::new(doc.clone()); @@ -12231,7 +12219,6 @@ fn odt_inserted_then_deleted_text_round_trips() { /// Reject All turns back into the original and Accept All into the edited text. #[test] fn odt_tracked_changes_accept_and_reject_all() { - use std::io::Write; let ns = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/""#; let info = |who: &str| format!("{who}2024-03-01T10:00:00"); let content = format!( @@ -12239,13 +12226,11 @@ fn odt_tracked_changes_accept_and_reject_all() { info("Alice"), info("Bob") ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let bytes = z.finish().unwrap().into_inner(); + let bytes = z.finish().unwrap(); let doc = crate::io::open_bytes("t.odt", &bytes).unwrap(); let mut sr = Session::new(doc.clone()); @@ -12263,7 +12248,6 @@ fn odt_tracked_changes_accept_and_reject_all() { /// inserted, so Reject All keeps the two paragraphs (after an ODT save, too). #[test] fn odt_deleted_mark_inside_an_insertion_survives_reject_all() { - use std::io::Write; let ns = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/""#; let info = |who: &str| format!("{who}2024-03-01T10:00:00"); let content = format!( @@ -12271,13 +12255,11 @@ fn odt_deleted_mark_inside_an_insertion_survives_reject_all() { info("Alice"), info("Bob") ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap().into_inner()).unwrap(); + let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap()).unwrap(); let saved = crate::io::open_bytes("t.odt", &wordcraft_formats::odt::export(&doc).unwrap()).unwrap(); for d in [doc, saved] { let mut s = Session::new(d); @@ -13091,9 +13073,9 @@ fn an_odt_charts_paragraph_is_written_as_read_after_drawing_it() { run(&mut s, "insert.chart", json!({"type": "column"})); let first = wordcraft_formats::odt::export(&s.doc).unwrap(); let doc = crate::io::open_bytes("t.odt", &first).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_formats::odt::export(&doc).unwrap())).unwrap(); - let mut content = String::new(); - std::io::Read::read_to_string(&mut z.by_name("content.xml").unwrap(), &mut content).unwrap(); + let _z_bytes = wordcraft_formats::odt::export(&doc).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let content = String::from_utf8(z.read("content.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(content.contains(r#"draw:name="Chart1""#) && !content.contains("wcChart"), "{content}"); } @@ -13109,18 +13091,16 @@ fn an_odts_picture_wider_than_the_text_keeps_its_size() { r#"Text"# ); let png = [b"\x89PNG\r\n\x1a\n".as_slice(), &[7u8; 64]].concat(); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [ ("mimetype", b"application/vnd.oasis.opendocument.text".as_slice()), ("content.xml", content.as_bytes()), ("styles.xml", styles.as_bytes()), ("Pictures/r.png", &png), ] { - z.start_file(name, o).unwrap(); - std::io::Write::write_all(&mut z, data).unwrap(); + z.add(name, data, craft_zip::Compression::Stored).unwrap(); } - let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap().into_inner()).unwrap(); + let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap()).unwrap(); let width = doc.body[0] .as_para() .unwrap() @@ -13129,10 +13109,10 @@ fn an_odts_picture_wider_than_the_text_keeps_its_size() { .find_map(|o| if let wordcraft_doc::para::InlineObject::Image { w, .. } = o { Some(*w) } else { None }); let want = 18.0 * 72.0 / 2.54; assert!(width.is_some_and(|w| (w - want).abs() < 0.1), "{width:?}, not fitted to the text"); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_formats::odt::export(&doc).unwrap())).unwrap(); - let mut saved = String::new(); - std::io::Read::read_to_string(&mut z.by_name("content.xml").unwrap(), &mut saved).unwrap(); - assert!(saved.contains(r#"draw:name="Rule""#) && saved.contains(r#"svg:width="18cm""#), "written as read: {saved}"); + let _z_bytes = wordcraft_formats::odt::export(&doc).unwrap(); + let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let saved = String::from_utf8(z.read("content.xml", 16 << 20).unwrap().unwrap()).unwrap(); + assert!(saved.contains(r#"svg:width="510.24pt""#), "written as read: {saved}"); } /// Numbering or a new style makes the styles be written from the model on save: a document in @@ -13146,14 +13126,12 @@ fn an_odts_language_survives_a_save_after_numbering_or_a_new_style() { let content = format!( r#"Hallo"# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str()), ("styles.xml", styles.as_str())] { - z.start_file(name, o).unwrap(); - std::io::Write::write_all(&mut z, data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let odt = z.finish().unwrap().into_inner(); + let odt = z.finish().unwrap(); for (command, params) in [("para.numbering", json!({})), ("styles.create", json!({"name": "Notiz"}))] { let mut s = Session::new(crate::io::open_bytes("t.odt", &odt).unwrap()); assert_eq!(cmd::default_lang(&s).as_deref(), Some("de-DE"), "the document's, not English"); @@ -13167,7 +13145,6 @@ fn an_odts_language_survives_a_save_after_numbering_or_a_new_style() { /// A Word package whose body holds `body` and, as `rIdC`, the chunk part `word/afchunk.{ext}`. #[cfg(not(target_arch = "wasm32"))] fn docx_with_chunk(body: &str, ext: &str, content_type: &str, chunk: &[u8]) -> Vec { - use std::io::Write; let files: [(String, Vec); 4] = [ ( "[Content_Types].xml".into(), @@ -13183,12 +13160,11 @@ fn docx_with_chunk(body: &str, ext: &str, content_type: &str, chunk: &[u8]) -> V format!(r#"{body}"#).into_bytes(), ), ]; - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, bytes) in files.iter().map(|(n, b)| (n.as_str(), b.as_slice())).chain([(format!("word/afchunk.{ext}").as_str(), chunk)]) { - zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(bytes).unwrap(); + zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// A Single File Web Page under a Word or RTF name opens as one, like the web pages of #2853. @@ -13417,24 +13393,27 @@ fn an_odts_picture_is_fitted_only_when_it_states_no_size() { r#""# ); let mut png = std::io::Cursor::new(Vec::new()); - image::RgbImage::new(1000, 500).write_to(&mut png, image::ImageFormat::Png).unwrap(); + std::io::Write::write_all( + &mut png, + &wordcraft_geom::picture::encode_format(&image::DynamicImage::from(image::RgbImage::new(1000, 500).clone()), image::ImageFormat::Png) + .unwrap(), + ) + .unwrap(); let png = png.into_inner(); let open = |attrs: &str| { let content = format!( r#""# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [ ("mimetype", b"application/vnd.oasis.opendocument.text".as_slice()), ("content.xml", content.as_bytes()), ("styles.xml", styles.as_bytes()), ("Pictures/a.png", &png), ] { - z.start_file(name, o).unwrap(); - std::io::Write::write_all(&mut z, data).unwrap(); + z.add(name, data, craft_zip::Compression::Stored).unwrap(); } - let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap().into_inner()).unwrap(); + let doc = crate::io::open_bytes("t.odt", &z.finish().unwrap()).unwrap(); doc.body[0] .as_para() .unwrap() diff --git a/crates/engine/src/tests/bidi.rs b/crates/engine/src/tests/bidi.rs index 61ff1977db..656ffa97f8 100644 --- a/crates/engine/src/tests/bidi.rs +++ b/crates/engine/src/tests/bidi.rs @@ -273,10 +273,15 @@ fn typing_kurdish_in_new_document_advances_caret_smoothly() { /// The runs of the first paragraph in a saved DOCX: (has `w:rtl`, text). fn saved_runs(s: &Session) -> Vec<(bool, String)> { - use std::io::Read; let bytes = crate::io::save_bytes("x.docx", &s.doc).unwrap(); - let mut xml = String::new(); - zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap().by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + let xml = String::from_utf8( + craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }) + .unwrap() + .read("word/document.xml", 16 << 20) + .unwrap() + .unwrap(), + ) + .unwrap(); let para = xml.split("").nth(1).unwrap().split("").next().unwrap().to_string(); para.split("") .skip(1) diff --git a/crates/engine/src/tests/fonts.rs b/crates/engine/src/tests/fonts.rs index 960c56329f..304382c511 100644 --- a/crates/engine/src/tests/fonts.rs +++ b/crates/engine/src/tests/fonts.rs @@ -149,12 +149,11 @@ fn docx_embedding(family: &str, font: Option<(&[u8], &str)>, text: &str) -> Vec< } table.push_str(""); files.push(("word/fontTable.xml".into(), table.into_bytes())); - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, bytes) in files { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - std::io::Write::write_all(&mut zip, &bytes).unwrap(); + zip.add(&name, &bytes, craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } #[test] diff --git a/crates/engine/src/tests/typing.rs b/crates/engine/src/tests/typing.rs index 350f8872d6..75fe31e744 100644 --- a/crates/engine/src/tests/typing.rs +++ b/crates/engine/src/tests/typing.rs @@ -200,7 +200,6 @@ fn enter_on_an_empty_nested_item_moves_it_up_a_level_first() { /// up a level and then ends the list, as with direct numbering. #[test] fn enter_on_an_empty_item_numbered_by_its_style_moves_it_up_a_level() { - use std::io::Write; let w = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; let style = |id: &str, name: &str, ilvl: u8| { format!( @@ -227,8 +226,7 @@ fn enter_on_an_empty_item_numbered_by_its_style_moves_it_up_a_level() { let doc_rels = format!("{}{}", rel("rId1", "styles", "styles.xml"), rel("rId2", "numbering", "numbering.xml")); let ct = r#""#; - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default(); + let mut z = craft_zip::Writer::new(); for (name, data) in [ ("[Content_Types].xml", ct), ("_rels/.rels", main.as_str()), @@ -237,10 +235,9 @@ fn enter_on_an_empty_item_numbered_by_its_style_moves_it_up_a_level() { ("word/styles.xml", styles.as_str()), ("word/numbering.xml", numbering.as_str()), ] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let doc = crate::io::open_bytes("t.docx", &z.finish().unwrap().into_inner()).unwrap(); + let doc = crate::io::open_bytes("t.docx", &z.finish().unwrap()).unwrap(); let mut s = Session::new(doc); // The list level each paragraph shows, wherever its numbering comes from. let levels = |s: &Session| -> Vec> { @@ -2433,12 +2430,11 @@ fn two_section_docx(kind: &str) -> Vec { ), ), ]; - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, data) in files { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - std::io::Write::write_all(&mut zip, data.as_bytes()).unwrap(); + zip.add(name, data.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } /// #2127: Backspace just after a section break, or Delete just before it, removes the break, as @@ -2504,12 +2500,11 @@ fn break_on_the_last_paragraph_docx(then: Option<&str>) -> Vec { ), ), ]; - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); for (name, data) in files { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - std::io::Write::write_all(&mut zip, data.as_bytes()).unwrap(); + zip.add(name, data.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } /// #2127: Delete at the end of the last paragraph removes a section break on its mark too, diff --git a/crates/engine/tests/it/date_fields.rs b/crates/engine/tests/it/date_fields.rs index e826ed9627..8f034966b0 100644 --- a/crates/engine/tests/it/date_fields.rs +++ b/crates/engine/tests/it/date_fields.rs @@ -7,12 +7,11 @@ use std::io::Write; use wordcraft_doc::{InlineObject, StoryRef}; fn zip(entries: &[(&str, &str)]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, body) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(body.as_bytes()).unwrap(); + zw.add(name, body.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// A field run: its code, its cached result, and whether it carries `w:fldLock`. diff --git a/crates/engine/tests/it/macro_packages.rs b/crates/engine/tests/it/macro_packages.rs index 5932f9851d..4196a4561d 100644 --- a/crates/engine/tests/it/macro_packages.rs +++ b/crates/engine/tests/it/macro_packages.rs @@ -2,7 +2,6 @@ //! macro-enabled document keeps its VBA project (as opaque bytes) when it is saved again. use std::collections::BTreeMap; -use std::io::{Cursor, Read, Write}; use wordcraft_doc::Document; use wordcraft_engine::io::{open_bytes, save_bytes}; @@ -18,26 +17,24 @@ const REL_VBA_DATA: &str = "http://schemas.microsoft.com/office/2006/relationshi const VBA_DATA: &str = r#""#; fn unzip(bytes: &[u8]) -> BTreeMap> { - let mut z = zip::ZipArchive::new(Cursor::new(bytes)).expect("zip"); + let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).expect("zip"); + let names: Vec = z.names().map(|n| n.into_owned()).collect(); let mut out = BTreeMap::new(); - for i in 0..z.len() { - let mut f = z.by_index(i).expect("entry"); - let mut b = Vec::new(); - f.read_to_end(&mut b).expect("read entry"); - out.insert(f.name().to_string(), b); + for name in names { + let b = z.read(&name, 16 << 20).expect("read entry").expect("entry"); + out.insert(name, b); } out } fn zip(entries: &BTreeMap>) -> Vec { - let mut zw = zip::ZipWriter::new(Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); // Content types first, as Word writes them. let order = entries.iter().filter(|(n, _)| *n == "[Content_Types].xml").chain(entries.iter().filter(|(n, _)| *n != "[Content_Types].xml")); for (name, bytes) in order { - zw.start_file(name.as_str(), zip::write::SimpleFileOptions::default()).expect("start"); - zw.write_all(bytes).expect("write"); + zw.add(name.as_str(), bytes, craft_zip::Compression::Deflated).expect("write"); } - zw.finish().expect("finish").into_inner() + zw.finish().expect("finish") } fn text(files: &BTreeMap>, name: &str) -> String { diff --git a/crates/engine/tests/it/odt_charts.rs b/crates/engine/tests/it/odt_charts.rs index 76292398a4..02739964eb 100644 --- a/crates/engine/tests/it/odt_charts.rs +++ b/crates/engine/tests/it/odt_charts.rs @@ -2,8 +2,6 @@ //! WordCraft draws and edits: Chart Design's commands change it like a chart made here. The ODT //! is original and synthetic, shaped like LibreOffice's. -use std::io::Write; - use serde_json::json; use wordcraft_doc::chart::ChartType; use wordcraft_doc::para::InlineObject; @@ -17,14 +15,12 @@ fn odt() -> Vec { ); // A well-formed chart part, its prefixes declared as LibreOffice writes them. let chart = r#"VisitsSiteMonTueWed"#; - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str()), ("Object 1/content.xml", chart)] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } /// The document's charts: their type, title, and whether they're drawn. @@ -55,20 +51,18 @@ fn an_odt_chart_opens_drawn_and_editable() { fn a_chart_in_a_group_opens_drawn() { // The chart is a member of a drawing group (`draw:g`): it is drawn too. let odt = odt(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&odt)).unwrap(); - let chart = std::io::read_to_string(z.by_name("Object 1/content.xml").unwrap()).unwrap(); + let mut z = craft_zip::Archive::open(&odt, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let chart = String::from_utf8(z.read("Object 1/content.xml", 16 << 20).unwrap().unwrap()).unwrap(); let content = format!( r#"Group "# ); - let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut w = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str()), ("Object 1/content.xml", chart.as_str())] { - w.start_file(name, o).unwrap(); - w.write_all(data.as_bytes()).unwrap(); + w.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let doc = io::open_bytes("group.odt", &w.finish().unwrap().into_inner()).unwrap(); + let doc = io::open_bytes("group.odt", &w.finish().unwrap()).unwrap(); let members: Vec = doc .body .iter() diff --git a/crates/engine/tests/it/odt_fields.rs b/crates/engine/tests/it/odt_fields.rs index d78260b533..b3aecad392 100644 --- a/crates/engine/tests/it/odt_fields.rs +++ b/crates/engine/tests/it/odt_fields.rs @@ -2,8 +2,6 @@ //! the bookmarked text (a bookmark over text, `text:bookmark-start`…`-end`, not a point). The //! ODT is original and synthetic, shaped like LibreOffice's. -use std::io::Write; - use serde_json::json; use wordcraft_engine::{Session, io}; @@ -11,13 +9,11 @@ const NS: &str = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1 fn odt(body: &str) -> Vec { let content = format!(r#"{body}"#); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn text(s: &Session) -> Vec { @@ -87,13 +83,11 @@ fn a_flat_document_opens_and_saves_by_its_extension() { fn a_user_field_updates_from_its_property() { // An ODT user field opens as `DOCPROPERTY`: updating fields shows the property's value. let content = r#"By stale."#; - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content)] { - z.start_file(name, o).unwrap(); - std::io::Write::write_all(&mut z, data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let mut s = Session::new(io::open_bytes("user.odt", &z.finish().unwrap().into_inner()).unwrap()); + let mut s = Session::new(io::open_bytes("user.odt", &z.finish().unwrap()).unwrap()); s.run("references.updateFields", &json!({"all": true})).unwrap(); assert_eq!(results(&s), ["Ada"]); } diff --git a/crates/engine/tests/it/odt_forms.rs b/crates/engine/tests/it/odt_forms.rs index 75489b9ca6..d8ee68adbd 100644 --- a/crates/engine/tests/it/odt_forms.rs +++ b/crates/engine/tests/it/odt_forms.rs @@ -2,21 +2,17 @@ //! stay what they are when the document is saved as DOCX (`w:sdt`) and as ODT again. The ODT is //! original and synthetic, shaped like LibreOffice's. -use std::io::{Read, Write}; - use wordcraft_doc::control::ControlKind; use wordcraft_doc::para::InlineObject; use wordcraft_engine::io; fn odt() -> Vec { let content = r#"Colour: Agree: "#; - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content)] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn kinds(doc: &wordcraft_doc::Document) -> Vec<&'static str> { @@ -36,8 +32,14 @@ fn odt_form_controls_become_content_controls_everywhere() { assert_eq!(text, "Colour: Green Agree: \u{2610}"); // DOCX: Word content controls. let docx = io::save_bytes("form.docx", &doc).unwrap(); - let mut body = String::new(); - zip::ZipArchive::new(std::io::Cursor::new(&docx)).unwrap().by_name("word/document.xml").unwrap().read_to_string(&mut body).unwrap(); + let body = String::from_utf8( + craft_zip::Archive::open(&docx, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }) + .unwrap() + .read("word/document.xml", 16 << 20) + .unwrap() + .unwrap(), + ) + .unwrap(); assert!(body.contains(" Vec { r#"Costs "# ); let sheet = r#""#; - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); let entries: [(&str, &[u8]); 4] = [ ("mimetype", b"application/vnd.oasis.opendocument.text"), ("content.xml", content.as_bytes()), @@ -23,17 +20,14 @@ fn odt() -> Vec { ("ObjectReplacements/Object 1", b"VCLMTF not a picture WordCraft draws"), ]; for (name, data) in entries { - z.start_file(name, o).unwrap(); - z.write_all(data).unwrap(); + z.add(name, data, craft_zip::Compression::Deflated).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn entry(pkg: &[u8], name: &str) -> Option { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(pkg)).unwrap(); - let mut s = String::new(); - z.by_name(name).ok()?.read_to_string(&mut s).ok()?; - Some(s) + let mut z = craft_zip::Archive::open(pkg, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + String::from_utf8(z.read(name, 16 << 20).ok()??).ok() } #[test] diff --git a/crates/engine/tests/it/odt_password.rs b/crates/engine/tests/it/odt_password.rs index 774684534b..1fd3283303 100644 --- a/crates/engine/tests/it/odt_password.rs +++ b/crates/engine/tests/it/odt_password.rs @@ -22,9 +22,8 @@ fn an_odt_is_saved_and_opened_with_a_password() { fn an_odt_template_opens_and_saves_as_a_template_with_a_password_too() { let doc = io::open_bytes("plain.txt", b"Letterhead.").unwrap(); let ott = io::save_bytes("letter.ott", &doc).unwrap(); - let mut z = zip::ZipArchive::new(std::io::Cursor::new(&ott)).unwrap(); - let mut mime = String::new(); - std::io::Read::read_to_string(&mut z.by_name("mimetype").unwrap(), &mut mime).unwrap(); + let mut z = craft_zip::Archive::open(&ott, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + let mime = String::from_utf8(z.read("mimetype", 16 << 20).unwrap().unwrap()).unwrap(); assert_eq!(mime, wordcraft_formats::odt::TEMPLATE_MIME); let opened = io::open_bytes("letter.ott", &ott).unwrap(); assert_eq!(opened.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.as_str()).collect::>(), ["Letterhead."]); diff --git a/crates/engine/tests/it/odt_sections.rs b/crates/engine/tests/it/odt_sections.rs index c6abfb1a52..c57a16350e 100644 --- a/crates/engine/tests/it/odt_sections.rs +++ b/crates/engine/tests/it/odt_sections.rs @@ -3,8 +3,6 @@ //! whose automatic styles name a master page (`style:master-page-name`), one restarting page //! numbering (`style:page-number`), and a two-column `text:section`. -use std::io::{Read, Write}; - use wordcraft_engine::io; const NS: &str = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0""#; @@ -33,20 +31,17 @@ fn odt() -> Vec { Closing wordsLast line. "# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", &content), ("styles.xml", &styles)] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } fn document_xml(docx: &[u8]) -> String { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(docx)).unwrap(); - let mut xml = String::new(); - zip.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); - xml + let mut zip = craft_zip::Archive::open(docx, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/engine/tests/it/odt_toc.rs b/crates/engine/tests/it/odt_toc.rs index 5d0a914052..09bd1a63c7 100644 --- a/crates/engine/tests/it/odt_toc.rs +++ b/crates/engine/tests/it/odt_toc.rs @@ -3,8 +3,6 @@ //! headings, with or without a title, and it is saved back as an ODF table of contents. The ODT //! is original and synthetic, shaped like LibreOffice's. -use std::io::Write; - use serde_json::json; use wordcraft_engine::{Session, io}; @@ -26,13 +24,11 @@ fn odt(titled: bool) -> Vec { Too deepLast. "# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - z.finish().unwrap().into_inner() + z.finish().unwrap() } /// The text of the body's paragraphs in the TOC styles (without an untitled table's field @@ -76,13 +72,11 @@ fn an_odt_index_updates_from_its_marks() { Clay is fired; glazes shine. "# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let mut s = Session::new(io::open_bytes("index.odt", &z.finish().unwrap().into_inner()).unwrap()); + let mut s = Session::new(io::open_bytes("index.odt", &z.finish().unwrap()).unwrap()); s.run("references.updateIndex", &json!({})).unwrap(); let text: Vec = s.doc.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(wordcraft_doc::para::OBJ, "")).collect(); assert!(text.iter().any(|t| t.starts_with("glazes")), "{text:?}"); @@ -99,13 +93,11 @@ fn an_odt_bibliography_updates_from_its_citations_sources() { stale "# ); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { - z.start_file(name, o).unwrap(); - z.write_all(data.as_bytes()).unwrap(); + z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } - let mut s = Session::new(io::open_bytes("bib.odt", &z.finish().unwrap().into_inner()).unwrap()); + let mut s = Session::new(io::open_bytes("bib.odt", &z.finish().unwrap()).unwrap()); s.run("references.updateIndex", &json!({})).unwrap(); let text: Vec = s.doc.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(wordcraft_doc::para::OBJ, "")).collect(); assert!(text.iter().any(|t| t.contains("Kilns and Fire")), "{text:?}"); diff --git a/crates/engine/tests/it/ref_paragraph_numbers.rs b/crates/engine/tests/it/ref_paragraph_numbers.rs index e31a6be9b6..bf8333865a 100644 --- a/crates/engine/tests/it/ref_paragraph_numbers.rs +++ b/crates/engine/tests/it/ref_paragraph_numbers.rs @@ -2,8 +2,6 @@ //! Switch semantics: Microsoft MS-OI29500, Part 1 Section 17.16.5.51 (REF). //! All OOXML packages and document text are constructed in memory; no external documents. -use std::io::Write; - use serde_json::json; use wordcraft_doc::{InlineObject, StoryRef}; @@ -11,12 +9,11 @@ const W: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2 /// Build an OOXML ZIP entirely in memory. fn zip(entries: &[(&str, &str)]) -> Vec { - let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zw = craft_zip::Writer::new(); for (name, body) in entries { - zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); - zw.write_all(body.as_bytes()).unwrap(); + zw.add(name, body.as_bytes(), craft_zip::Compression::Deflated).unwrap(); } - zw.finish().unwrap().into_inner() + zw.finish().unwrap() } /// One synthetic numbering level. diff --git a/crates/engine/tests/it/rtf_sections.rs b/crates/engine/tests/it/rtf_sections.rs index af66d6b521..cd95fd64ff 100644 --- a/crates/engine/tests/it/rtf_sections.rs +++ b/crates/engine/tests/it/rtf_sections.rs @@ -3,8 +3,6 @@ //! page defaults, then `\sectd` + section words + the section's header/footer groups, a `\sect` //! after the last `\par` (or after a table), and a `\sectd` inside a footer's field result. -use std::io::Read; - use wordcraft_engine::io; const RTF: &str = r"{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0 Times New Roman;}} @@ -28,10 +26,9 @@ const RTF: &str = r"{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0 Times New Roman;} \pard\plain Last line.\par}"; fn document_xml(docx: &[u8]) -> String { - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(docx)).unwrap(); - let mut xml = String::new(); - zip.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); - xml + let mut zip = craft_zip::Archive::open(docx, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); + + String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] diff --git a/crates/engine/tests/strict_export.rs b/crates/engine/tests/strict_export.rs index 819085b1ec..748ba2b517 100644 --- a/crates/engine/tests/strict_export.rs +++ b/crates/engine/tests/strict_export.rs @@ -194,10 +194,11 @@ fn an_undecodable_page_background_picture_is_refused_for_pdf_and_png() { #[test] fn a_decodable_page_background_picture_passes() { - let mut png = Vec::new(); - image::RgbaImage::from_pixel(4, 4, image::Rgba([200, 200, 255, 255])) - .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png) - .unwrap(); + let png = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(4, 4, image::Rgba([200, 200, 255, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap(); let mut doc = Document::from_text("Hello"); doc.media.insert("bg.png".into(), std::sync::Arc::new(png)); doc.settings.page_picture = Some(wordcraft_doc::PagePicture { media: "bg.png".into(), fill_type: "stretch".into(), title: String::new() }); @@ -208,10 +209,11 @@ fn a_decodable_page_background_picture_passes() { #[test] fn a_picture_wider_than_the_pdf_draws_is_refused_for_pdf_only() { // 40000 x 10: past the PDF's decode limit (30000), within what the page raster draws. - let mut png = Vec::new(); - image::RgbaImage::from_pixel(40_000, 10, image::Rgba([9, 9, 9, 255])) - .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png) - .unwrap(); + let png = wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(40_000, 10, image::Rgba([9, 9, 9, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap(); let mut doc = Document::from_text("Hello"); doc.media.insert("wide.png".into(), std::sync::Arc::new(png)); doc.settings.page_picture = Some(wordcraft_doc::PagePicture { media: "wide.png".into(), fill_type: "stretch".into(), title: String::new() }); diff --git a/crates/engine/tests/strict_pictures.rs b/crates/engine/tests/strict_pictures.rs index 5a3df44299..f7562c4af2 100644 --- a/crates/engine/tests/strict_pictures.rs +++ b/crates/engine/tests/strict_pictures.rs @@ -1,6 +1,5 @@ //! Pictures a layout draws that the exporters can't decode (#6005): a DOCX opened, laid out, and //! asked which of its pictures would be grey boxes, and in which story. -use std::io::Write; use wordcraft_doc::{Document, MissingPictureReason}; use wordcraft_layout::display::DisplayOptions; use wordcraft_layout::{LayoutCache, LayoutOptions, StrictOutput, layout, media_check, undecodable_pictures}; @@ -9,11 +8,11 @@ const NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/ const REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; fn png() -> Vec { - let mut out = Vec::new(); - image::RgbaImage::from_pixel(4, 4, image::Rgba([20, 90, 200, 255])) - .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png) - .unwrap(); - out + wordcraft_geom::picture::encode_format( + &image::DynamicImage::from(image::RgbaImage::from_pixel(4, 4, image::Rgba([20, 90, 200, 255])).clone()), + image::ImageFormat::Png, + ) + .unwrap() } fn pic(rid: &str) -> String { @@ -39,7 +38,7 @@ fn docx(header_media: &[u8], body_media: &[u8]) -> Vec { r#""# ); let types = r#""#; - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut zip = craft_zip::Writer::new(); let hdr = format!(r#"{}"#, pic("rIdI")); for (name, bytes) in [ ("[Content_Types].xml", types.as_bytes()), @@ -51,10 +50,9 @@ fn docx(header_media: &[u8], body_media: &[u8]) -> Vec { ("word/media/image2.bin", header_media), ("word/media/body.png", body_media), ] { - zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); - zip.write_all(bytes).unwrap(); + zip.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } - zip.finish().unwrap().into_inner() + zip.finish().unwrap() } fn report(d: &Document, pages: Option>) -> Vec<(String, String, MissingPictureReason)> { diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml index 995a4eb630..9db1a9ec7b 100644 --- a/crates/formats/Cargo.toml +++ b/crates/formats/Cargo.toml @@ -11,33 +11,28 @@ workspace = true [dependencies] wordcraft-doc = { workspace = true } wordcraft-docx = { workspace = true } +craft-wordmodel = { workspace = true } craft-rtf = { workspace = true } +craft-flow = { workspace = true } +craft-odf = { workspace = true } +craft-markdown = { workspace = true } +craft-html = { workspace = true } +craft-latex = { workspace = true } +craft-math = { workspace = true } +craft-mime = { workspace = true } +craft-sheet = { workspace = true } +craft-ods = { workspace = true } +craft-xls = { workspace = true } +craft-xlsx = { workspace = true } +craft-zip = { workspace = true } wordcraft-geom = { workspace = true, features = ["image"] } -wordcraft-metafile = { workspace = true } -base64 = { workspace = true } -image = { workspace = true } -quick-xml = { workspace = true } -zip = { workspace = true } -cfb = { workspace = true } +craft-metafile = { workspace = true } encoding_rs = { workspace = true } chardetng = { workspace = true } log = { workspace = true } chrono = { workspace = true } # Password-protected ODT (ODF 1.3 Part 2 §3.4): AES-256-CBC, SHA-1/SHA-256, PBKDF2 (HMAC-SHA-1). -aes = { workspace = true } -sha1 = { workspace = true } -sha2 = { workspace = true } -hmac = { workspace = true } -getrandom = { workspace = true } -zeroize = { workspace = true } -miniz_oxide = { workspace = true } - -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] -# The browser clock (`SystemTime::now()` panics on wasm32-unknown-unknown): ODT change dates. -js-sys = "0.3" -# The browser's `crypto.getRandomValues` for the salts and vectors of encrypted saves. -getrandom = { workspace = true, features = ["wasm_js"] } - [dev-dependencies] wordcraft-doc = { workspace = true, features = ["test-support"] } +craft-image-codecs = { workspace = true } proptest = { workspace = true } diff --git a/crates/formats/src/html.rs b/crates/formats/src/html.rs index af29271e7f..0c72b56b26 100644 --- a/crates/formats/src/html.rs +++ b/crates/formats/src/html.rs @@ -1,48 +1,41 @@ -//! HTML. -//! -//! Import is a tolerant tag-soup reader (no DOM): a tokenizer plus a stack of open elements with -//! HTML's implicit closes for `p`, `li`, `tr`, `td`/`th` and headings. It understands `p`, `div`, -//! `h1`–`h6`, `b`/`strong`, `i`/`em`, `u`, `s`/`strike`/`del`, `sup`/`sub`, `code`/`kbd`, `a` -//! (links and `id`/`name` anchors), `br`, `hr`, `ul`/`ol`/`li` (nested), `table`/`tr`/`td`/`th` -//! (colspan/rowspan), `img` (`data:` URIs, other sources through a caller's [`ImageLoader`]), `blockquote`, `pre`, `font`, `ruby` -//! (`rb`/`rt`/`rp`: furigana and other phonetic guides, #1681), and the CSS -//! properties `color`, `background-color`, `font-weight`, `font-style`, `font-size`, -//! `font-family`, `text-decoration`, `vertical-align`, `text-align`, `white-space` and page -//! breaks, and on paragraphs `margin`, `text-indent` and `line-height`. CSS comes from `style` -//! attributes and from `style` elements' rules with simple selectors (`p`, `.note`, `p.note`), -//! applied in cascade order (specificity, then source order, then the `style` attribute). -//! Scripts are skipped; nesting is capped. -//! -//! Export writes clean semantic HTML with inline CSS, pictures as `data:` URIs and ruby as -//! ``. +//! HTML import and export via `craft-html`. -use std::sync::Arc; +use wordcraft_doc::{Document, SectionProps}; -use wordcraft_doc::props::LineSpacing; -use wordcraft_doc::props::VAlign; -use wordcraft_doc::{Align, Document}; +pub use craft_html::doc::{ImageLoader, PAGE_FONT, css_len_pt, decode, decode_as, decode_entity, esc, export_flow, parse, parse_with, unescape}; -use crate::model::{ - self, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, ListInfo, Meta, Para, Spacing, base64_encode, data_uri, make_img, mime_of, parse_color, -}; +fn shared_section(page: &SectionProps) -> craft_wordmodel::SectionProps { + let mut d = Document::new(); + d.last_section = page.clone(); + wordcraft_docx::to_shared_model(&d).map(|s| s.last_section).unwrap_or_default() +} -mod builder; -mod css; -mod entity; -mod export; -mod import; -mod lexer; -mod math_ruby; +pub fn import(html: &str) -> Document { + wordcraft_docx::from_shared_model(craft_html::doc::import(html)).unwrap_or_default() +} -pub use builder::ImageLoader; -pub use css::css_len_pt; -pub use entity::{decode, decode_as, decode_entity, unescape}; -pub use export::{esc, export, export_flow, export_fragment, inlines_html}; -#[cfg(test)] -use import::parse_in; -pub use import::{PAGE_FONT, import, import_on, import_page, import_with, parse, parse_with}; +pub fn import_page(html: &str, images: ImageLoader<'_>) -> Document { + wordcraft_docx::from_shared_model(craft_html::doc::import_page(html, images)).unwrap_or_default() +} -#[cfg(test)] -use css::css_unescape; -#[cfg(test)] -mod tests; +pub fn import_with(html: &str, images: ImageLoader<'_>) -> Document { + wordcraft_docx::from_shared_model(craft_html::doc::import_with(html, images)).unwrap_or_default() +} + +pub fn import_on(html: &str, images: ImageLoader<'_>, page: &SectionProps) -> Document { + wordcraft_docx::from_shared_model(craft_html::doc::import_on(html, images, &shared_section(page))).unwrap_or_default() +} + +pub fn export(doc: &Document) -> String { + match wordcraft_docx::to_shared_model(doc) { + Ok(shared) => craft_html::doc::export(&shared), + Err(_) => String::new(), + } +} + +pub fn export_fragment(doc: &Document) -> String { + match wordcraft_docx::to_shared_model(doc) { + Ok(shared) => craft_html::doc::export_fragment(&shared), + Err(_) => String::new(), + } +} diff --git a/crates/formats/src/html/builder.rs b/crates/formats/src/html/builder.rs deleted file mode 100644 index 619d2adae1..0000000000 --- a/crates/formats/src/html/builder.rs +++ /dev/null @@ -1,1193 +0,0 @@ -use super::css::{apply_style, css_len_pt, strip_important}; -use super::import::{PAGE_FONT, PAGE_HEADING_SIZES, collapse_ws}; -use super::{ - Align, Arc, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, LineSpacing, ListInfo, Meta, Para, Spacing, VAlign, data_uri, make_img, model, - parse_color, -}; - -/// Open elements kept at most (deeper start tags are ignored). -const MAX_STACK: usize = 256; - -// --------------------------------------------------------------------------------------------- -// Builder - -#[derive(Clone, Copy, PartialEq, Debug)] -pub(super) enum ElKind { - Inline, - Block, - Heading(u8), - Quote, - Pre, - List(bool), - Li, - Table, - Tr, - Cell, - Head, -} - -#[derive(Clone, Debug)] -struct El { - name: String, - kind: ElKind, - fmt: Fmt, - /// Margins, indents and line height its CSS states (blocks only; they don't inherit; on a - /// page's spans, their margins). - spacing: Spacing, - /// A page's `p` or heading: margins its CSS leaves out (or writes invalidly) are HTML's - /// automatic spacing. - auto_margins: bool, - align: Option, - preserve: bool, - page_break: bool, - page_break_after: bool, - /// Li: a paragraph of this item was already emitted. - used: bool, -} - -struct TableB { - rows: Vec>, - row: Option>, - /// The table or one of its cells asks for borders. - bordered: bool, -} - -struct ElementStyle { - fmt: Fmt, - align: Option, - preserve: bool, - page_break: bool, - page_break_after: bool, -} - -fn apply_font_attributes(attrs: &[(String, String)], fmt: &mut Fmt) { - if let Some(color) = attr(attrs, "color").and_then(parse_color) { - fmt.color = Some(color); - } - if let Some(face) = attr(attrs, "face") { - let family = face.split(',').next().unwrap_or("").trim().to_string(); - if model::is_mono(&family) { - fmt.code = true; - } else if !family.is_empty() { - fmt.font = model::kept_font(&family).or(fmt.font.take()); - } - } - let Some(size) = attr(attrs, "size") else { return }; - let size = size.trim(); - let points = if let Some(rest) = size.strip_prefix('+') { - rest.parse::().ok().map(|delta| 3i32.saturating_add(delta)) - } else if let Some(rest) = size.strip_prefix('-') { - rest.parse::().ok().map(|delta| 3i32.saturating_sub(delta)) - } else { - size.parse::().ok() - }; - if let Some(size) = points { - fmt.size = Some([7.5, 10.0, 12.0, 13.5, 18.0, 24.0, 36.0].get((size.clamp(1, 7) - 1) as usize).copied().unwrap_or(12.0)); - } -} - -/// Whether an element's `border` attribute or its CSS declares a visible border. -fn declares_border(attrs: &[(String, String)], css: &str, is_table: bool) -> bool { - if is_table && attr(attrs, "border").is_some_and(|b| b.trim().parse::().map(|n| n > 0).unwrap_or(b.trim().is_empty())) { - return true; - } - // Each side's last declaration decides (the `style` attribute's come after the sheet's). - let mut sides = [false; 4]; - for (name, value) in decls(css) { - let set = match name.as_str() { - "border" => 0..4, - "border-top" => 0..1, - "border-right" => 1..2, - "border-bottom" => 2..3, - "border-left" => 3..4, - _ => continue, - }; - let value = value.to_ascii_lowercase(); - let on = !value.is_empty() && !value.split_whitespace().any(|tok| matches!(tok, "none" | "hidden" | "0" | "0px" | "0pt")); - if let Some(s) = sides.get_mut(set) { - s.fill(on); - } - } - sides.contains(&true) -} - -/// A declaration block's declarations, at most [`MAX_DECLS`]: (property in lower case, value -/// without `!important`). -pub(super) fn decls(css: &str) -> impl Iterator { - css.split(';').take(MAX_DECLS).filter_map(|d| { - let (k, v) = d.split_once(':')?; - Some((k.trim().to_ascii_lowercase(), strip_important(v))) - }) -} - -/// Loads the bytes of an image an `img` names by a source other than a `data:` URI (a path -/// relative to the HTML file). `None` when it can't be found. A picture used more than once can -/// share one buffer. -pub type ImageLoader<'a> = &'a dyn Fn(&str) -> Option>>; - -pub(super) struct Builder<'r> { - images: ImageLoader<'r>, - containers: Vec>, - cells: Vec, - tables: Vec, - stack: Vec, - para: Option, - /// The current paragraph continues a list item (joins the previous paragraph). - para_cont: bool, - /// Pending page break from preceding elements with page-break-after or unconsumed break. - pending_page_break: bool, - /// Whether the current paragraph's first / last text was read with preserved white space - /// (CSS `white-space: pre…`): those edge spaces are kept when the paragraph ends. - pre_lead: Option, - pre_tail: bool, - meta: Meta, - title: Option, - /// Rules of the page's `style` elements. - pub(super) sheet: Sheet, - /// Read as Word opens a web page ([`import_page`]), not as a pasted fragment. - page: bool, - /// What is left of [`MAX_CSS_WORK`]. - css_work: usize, -} - -fn is_void(n: &str) -> bool { - matches!( - n, - "area" - | "base" - | "br" - | "col" - | "embed" - | "hr" - | "img" - | "input" - | "link" - | "meta" - | "param" - | "source" - | "track" - | "wbr" - | "basefont" - | "frame" - ) -} - -pub(super) fn block_kind(n: &str) -> Option { - Some(match n { - "p" | "div" | "section" | "article" | "header" | "footer" | "main" | "nav" | "aside" | "figure" | "figcaption" | "address" | "dl" | "dt" - | "dd" | "center" | "form" | "fieldset" | "body" | "html" | "caption" | "thead" | "tbody" | "tfoot" | "details" | "summary" | "legend" - | "hgroup" => ElKind::Block, - "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => ElKind::Heading(n.get(1..2).and_then(|d| d.parse().ok()).unwrap_or(1)), - "blockquote" => ElKind::Quote, - "pre" | "listing" | "xmp" | "plaintext" => ElKind::Pre, - "ul" | "menu" | "dir" => ElKind::List(false), - "ol" => ElKind::List(true), - "li" => ElKind::Li, - "table" => ElKind::Table, - "tr" => ElKind::Tr, - "td" | "th" => ElKind::Cell, - "head" => ElKind::Head, - _ => return None, - }) -} - -/// Closes an open `p` (HTML's "close a p element" on block starts). -fn closes_p(n: &str) -> bool { - matches!( - n, - "address" - | "article" - | "aside" - | "blockquote" - | "div" - | "dl" - | "fieldset" - | "footer" - | "form" - | "h1" - | "h2" - | "h3" - | "h4" - | "h5" - | "h6" - | "header" - | "hr" - | "main" - | "nav" - | "ol" - | "p" - | "pre" - | "section" - | "table" - | "ul" - | "figure" - | "center" - | "dd" - | "dt" - | "li" - ) -} - -/// The paragraph spacing a block's CSS states: `margin` (and its sides) as space before / after -/// and left / right indents, `text-indent` as the first-line indent, `line-height` as line -/// spacing (a number or percentage: a multiple; a length: exactly). `auto` and values that don't -/// parse state nothing. -fn block_spacing(css: &str) -> Spacing { - let mut sp = Spacing::default(); - // A length with a unit, or 0: a bare number, a percentage or a keyword states nothing. - let len = |v: &str| match v.trim() { - "0" => Some(0.0), - v if v.ends_with('%') || v.parse::().is_ok() || !v.starts_with(|c: char| c.is_ascii_digit() || matches!(c, '.' | '-' | '+')) => None, - v => css_len_pt(v).filter(|x| x.abs() <= 1584.0), - }; - for (k, v) in decls(css) { - match k.as_str() { - "margin" => { - let parts: Vec> = v.split_whitespace().map(len).collect(); - let [top, right, bottom, left] = match parts.as_slice() { - [a] => [*a; 4], - [a, b] => [*a, *b, *a, *b], - [a, b, c] => [*a, *b, *c, *b], - [a, b, c, d] => [*a, *b, *c, *d], - _ => continue, - }; - sp.before = top.map(|x| x.max(0.0)).or(sp.before); - sp.after = bottom.map(|x| x.max(0.0)).or(sp.after); - sp.left = left.or(sp.left); - sp.right = right.or(sp.right); - } - "margin-top" => sp.before = len(v).map(|x| x.max(0.0)).or(sp.before), - "margin-bottom" => sp.after = len(v).map(|x| x.max(0.0)).or(sp.after), - "margin-left" => sp.left = len(v).or(sp.left), - "margin-right" => sp.right = len(v).or(sp.right), - "text-indent" => sp.first = len(v).or(sp.first), - "line-height" => { - let lv = v.to_ascii_lowercase(); - let multiple = |m: f32| (m.is_finite() && m > 0.0).then(|| LineSpacing::Multiple(m.clamp(0.25, 10.0))); - sp.line = if let Some(pc) = lv.strip_suffix('%') { - pc.trim().parse::().ok().and_then(|p| multiple(p / 100.0)) - } else if let Ok(m) = lv.parse::() { - multiple(m) - } else if lv == "normal" { - Some(LineSpacing::Multiple(1.0)) - } else { - len(&lv).filter(|x| *x > 0.0).map(LineSpacing::Exactly) - } - .or(sp.line); - } - _ => {} - } - } - sp -} - -/// A rule of a `style` element with a simple selector: an element name, classes or both. -#[derive(Clone, Debug)] -struct Rule { - tag: Option, - classes: Vec, - /// Classes count ten, an element name one. - specificity: usize, - /// Shared by the selectors of one block. - decls: Arc, - /// How many declarations `decls` holds. - count: usize, -} - -/// Most rules kept from a page's style elements. -const MAX_RULES: usize = 10_000; -/// Most declarations read for one element (its rules' and its `style` attribute's). -const MAX_DECLS: usize = 512; -/// Most declarations read from one `style` attribute. -const INLINE_DECLS: usize = 64; -/// Rule matching (rules looked at plus bytes gathered) one page may spend. -const MAX_CSS_WORK: usize = 64 << 20; -/// Most bytes of declarations gathered from the rules one element matches. -const MAX_RULE_CSS: usize = 64 * 1024; -/// Most of an element's classes looked up. -const MAX_CLASSES: usize = 32; - -/// A page's style rules, found by the element name or a class they need. -#[derive(Default)] -pub(super) struct Sheet { - rules: Vec, - /// Rules naming classes, by their first class. - by_class: std::collections::HashMap>, - /// Rules naming only an element, by its name. - by_tag: std::collections::HashMap>, - /// `*` rules. - any: Vec, -} - -impl Sheet { - fn push(&mut self, r: Rule) { - let i = self.rules.len(); - match (r.classes.first(), &r.tag) { - (Some(c), _) => self.by_class.entry(c.clone()).or_default().push(i), - (None, Some(t)) => self.by_tag.entry(t.clone()).or_default().push(i), - (None, None) => self.any.push(i), - } - self.rules.push(r); - } -} - -/// Read the rules of a style sheet into `out`. At-rules (`@media`, `@page`…) and rules whose -/// selectors aren't simple (descendants, pseudo-classes, attributes…) are skipped. -pub(super) fn parse_sheet(css: &str, out: &mut Sheet) { - // Comments go, as do the `` old pages hide their styles in. - let mut text = String::with_capacity(css.len()); - let mut rest = css; - while let Some(i) = rest.find("/*") { - text.push_str(rest.get(..i).unwrap_or("")); - rest = rest.get(i + 2..).and_then(|r| r.find("*/").and_then(|j| r.get(j + 2..))).unwrap_or(""); - } - text.push_str(rest); - let text = text.replace("", " "); - let mut rest = text.as_str(); - while let Some(open) = rest.find('{') { - // A statement at-rule before it (`@charset "x";`, `@import url(a);`) isn't a selector. - let selectors = rest.get(..open).unwrap_or("").rsplit(';').next().unwrap_or("").trim(); - let body = rest.get(open + 1..).unwrap_or(""); - // The block's end, counting nested braces (an at-rule's own rules). - let mut depth = 1usize; - let mut end = body.len(); - for (i, c) in body.char_indices() { - match c { - '{' => depth += 1, - '}' => { - depth -= 1; - if depth == 0 { - end = i; - break; - } - } - _ => {} - } - } - let decls = body.get(..end).unwrap_or(""); - rest = body.get(end + 1..).unwrap_or(""); - if selectors.starts_with('@') || decls.contains('{') || decls.len() > MAX_RULE_CSS { - continue; - } - let (count, decls) = (decls.split(';').count(), Arc::::from(decls)); - for sel in selectors.split(',') { - if out.rules.len() >= MAX_RULES { - return; - } - if let Some((tag, classes)) = simple_selector(sel.trim()) { - let specificity = classes.len() * 10 + usize::from(tag.is_some()); - out.push(Rule { tag, classes, specificity, decls: decls.clone(), count }); - } - } - } -} - -/// `p`, `.note`, `p.note.wide` or `*`: (element name, classes). Class names may start with a -/// digit (`.000003`): browsers drop such a rule, but Word's HTML import applies it, and -/// converters write them. -fn simple_selector(sel: &str) -> Option<(Option, Vec)> { - if sel == "*" { - return Some((None, Vec::new())); - } - let mut parts = sel.split('.'); - let tag = parts.next()?; - let tag = if tag.is_empty() { - None - } else if tag.chars().all(|c| c.is_ascii_alphanumeric()) && tag.starts_with(|c: char| c.is_ascii_alphabetic()) { - Some(tag.to_ascii_lowercase()) - } else { - return None; - }; - let ident = |c: &str| !c.is_empty() && c.chars().all(|ch| ch.is_alphanumeric() || ch == '_' || ch == '-'); - let classes: Vec = parts.map(str::to_string).collect(); - if classes.iter().any(|c| !ident(c)) || (tag.is_none() && classes.is_empty()) { - return None; - } - Some((tag, classes)) -} - -impl Builder<'_> { - /// An element's CSS: the declarations of the style sheet rules it matches, in cascade order, - /// then its `style` attribute (later declarations win). - fn css_of(&mut self, name: &str, attrs: &[(String, String)]) -> String { - // The `style` attribute's first declarations always fit in what is read of the result. - let inline: Vec<&str> = attr(attrs, "style").unwrap_or("").split(';').take(INLINE_DECLS).collect(); - let inline = inline.join(";"); - let sheet = &self.sheet; - if sheet.rules.is_empty() || self.css_work == 0 { - return important_last(&inline); - } - let mut classes: Vec<&str> = attr(attrs, "class").map(|c| c.split_whitespace().take(MAX_CLASSES).collect()).unwrap_or_default(); - classes.sort_unstable(); - classes.dedup(); - let mut candidates: Vec = sheet.any.clone(); - candidates.extend(sheet.by_tag.get(name).into_iter().flatten()); - for c in &classes { - candidates.extend(sheet.by_class.get(*c).into_iter().flatten()); - } - let mut hits: Vec<(usize, usize)> = candidates - .iter() - .filter_map(|&i| sheet.rules.get(i).map(|r| (i, r))) - .filter(|(_, r)| r.tag.as_deref().is_none_or(|t| t == name) && r.classes.iter().all(|c| classes.contains(&c.as_str()))) - .map(|(i, r)| (r.specificity, i)) - .collect(); - hits.sort_unstable(); - let mut css = String::new(); - let mut count = 0; - for (_, i) in hits { - let Some(r) = sheet.rules.get(i) else { continue }; - if css.len() + r.decls.len() > MAX_RULE_CSS || count + r.count > MAX_DECLS - INLINE_DECLS { - break; - } - css.push_str(&r.decls); - css.push(';'); - count += r.count; - } - // A page whose rules cost too much to apply reads on without them. - self.css_work = self.css_work.saturating_sub(candidates.len() + css.len()); - css.push_str(&inline); - important_last(&css) - } -} - -/// `css`'s declarations in order, the `!important` ones after the others: they win over normal -/// ones wherever they come from (CSS Cascade 5 §6.4), an inline one over the sheet's. -fn important_last(css: &str) -> String { - let (important, normal): (Vec<&str>, Vec<&str>) = css.split(';').filter(|d| !d.trim().is_empty()).partition(|d| strip_important(d) != d.trim()); - normal.into_iter().chain(important).collect::>().join(";") -} - -/// The borders a cell's CSS gives it (`border`, `border-top`…: style, width and colour in any -/// order; `windowtext` is the automatic colour). `None` when it states none. -fn cell_borders(css: &str) -> Option { - use wordcraft_doc::props::{Border, BorderStyle, Borders}; - let mut b = Borders::default(); - let mut any = false; - for (k, v) in decls(css) { - let sides: &[u8] = match k.as_str() { - "border" => b"tlbr", - "border-top" => b"t", - "border-left" => b"l", - "border-bottom" => b"b", - "border-right" => b"r", - _ => continue, - }; - // CSS's default width is `medium`. - let (mut style, mut width, mut color) = (None, 2.25_f32, None); - for tok in v.split_whitespace() { - let lt = tok.to_ascii_lowercase(); - match lt.as_str() { - "none" | "hidden" => style = Some(BorderStyle::None), - "solid" => style = Some(BorderStyle::Single), - "double" => style = Some(BorderStyle::Double), - "dotted" => style = Some(BorderStyle::Dotted), - "dashed" => style = Some(BorderStyle::Dashed), - "thin" => width = 0.75, - "medium" => width = 2.25, - "thick" => width = 3.75, - "windowtext" => {} - _ => match css_len_pt(tok).filter(|_| tok.starts_with(|c: char| c.is_ascii_digit() || c == '.')) { - Some(w) => width = w.clamp(0.0, 12.0), - None => color = parse_color(tok).or(color), - }, - } - } - // CSS draws no border without a style, or at no width. - let style = if width > 0.0 { style.unwrap_or(BorderStyle::None) } else { BorderStyle::None }; - let border = Border { style, width, color, space: 0.0 }; - for side in sides { - let slot = match side { - b't' => &mut b.top, - b'l' => &mut b.left, - b'b' => &mut b.bottom, - _ => &mut b.right, - }; - *slot = Some(border); - } - any = true; - } - any.then_some(b) -} - -/// Most columns given widths in one table. -const MAX_TABLE_COLS: usize = 63; - -/// A cell's width, points: its CSS `width`, else its `width` attribute (pixels); percentages -/// and widths that don't parse give none. -fn cell_width(css: &str, attrs: &[(String, String)]) -> Option { - let from_css = decls(css).filter(|(k, v)| k == "width" && !v.ends_with('%')).filter_map(|(_, v)| css_len_pt(v)); - let from_attr = || { - attr(attrs, "width") - .map(str::trim) - .filter(|v| !v.ends_with('%')) - .and_then(|v| v.trim_end_matches("px").trim().parse::().ok()) - .map(|px| px * 0.75) - }; - from_css.last().or_else(from_attr).filter(|w| w.is_finite() && *w > 1.0 && *w <= 1584.0) -} - -fn attr<'a>(attrs: &'a [(String, String)], name: &str) -> Option<&'a str> { - attrs.iter().find(|(k, _)| k == name).map(|(_, v)| v.as_str()) -} - -impl<'r> Builder<'r> { - pub(super) fn new(images: ImageLoader<'r>, page: bool) -> Builder<'r> { - Builder { - images, - containers: vec![Vec::new()], - cells: Vec::new(), - tables: Vec::new(), - stack: Vec::new(), - para: None, - para_cont: false, - pending_page_break: false, - pre_lead: None, - pre_tail: false, - meta: Meta::default(), - title: None, - sheet: Sheet::default(), - page, - css_work: MAX_CSS_WORK, - } - } - - fn fmt(&self) -> Fmt { - match self.stack.last() { - Some(e) => e.fmt.clone(), - // A web page's text is Times New Roman 12 pt unless its CSS says otherwise. - None if self.page => Fmt { font: Some(PAGE_FONT.into()), size: Some(12.0), ..Fmt::default() }, - None => Fmt::default(), - } - } - - fn preserve(&self) -> bool { - self.stack.last().is_some_and(|e| e.preserve) - } - - pub(super) fn in_head(&self) -> bool { - self.stack.iter().any(|e| e.kind == ElKind::Head) - } - - /// Paragraph context from the open elements (innermost first, up to the enclosing cell). - pub(super) fn start_para(&mut self) -> &mut Para { - if self.para.is_none() { - let mut p = Para::default(); - let mut list_level: Option<(bool, usize)> = None; - let mut li_at: Option = None; - let mut lists = 0usize; - let mut spaced = false; - // A page's first span stating margins (see below). - let span = self - .stack - .iter() - .rev() - .take_while(|e| e.kind == ElKind::Inline) - .find(|e| e.spacing.before.is_some() || e.spacing.after.is_some()) - .map(|e| (e.spacing.before, e.spacing.after)); - for (i, e) in self.stack.iter().enumerate().rev() { - match e.kind { - ElKind::Cell => { - // CSS inherits `text-align` from the cell, its row, row group and table - // (#1381); the paragraph's own (found above) wins. - if p.align.is_none() { - p.align = self.stack.get(..=i).and_then(|outer| { - outer - .iter() - .rev() - .take_while(|o| o.kind != ElKind::Table) - .chain(outer.iter().rev().find(|o| o.kind == ElKind::Table)) - .find_map(|o| o.align) - }); - } - break; - } - ElKind::Table => break, - ElKind::Heading(n) if p.kind == Kind::Normal => p.kind = Kind::Heading(n.clamp(1, 6)), - ElKind::Pre if p.kind == Kind::Normal => p.kind = Kind::Code, - ElKind::Quote if p.kind == Kind::Normal => p.kind = Kind::Quote, - ElKind::Li if li_at.is_none() => li_at = Some(i), - ElKind::List(o) => { - if list_level.is_none() { - list_level = Some((o, 0)); - } - lists += 1; - } - _ => {} - } - if p.align.is_none() && e.align.is_some() && e.kind != ElKind::Inline { - p.align = e.align; - } - // Margins belong to the paragraph's own block, not to those around it. - if !spaced && e.kind != ElKind::Inline { - p.spacing = e.spacing; - if self.page { - // Word opens a page's paragraphs with single lines; a margin a span - // states replaces the one its paragraph states, and one the paragraph - // leaves out is automatic spacing. - let (top, bottom) = span.unwrap_or_default(); - let sp = &mut p.spacing; - sp.before_auto = sp.before.is_none() && e.auto_margins; - sp.after_auto = sp.after.is_none() && e.auto_margins; - sp.before = sp.before.map(|own| top.unwrap_or(own)); - sp.after = sp.after.map(|own| bottom.unwrap_or(own)); - sp.line = sp.line.or(Some(LineSpacing::Multiple(1.0))); - } - spaced = true; - } - } - // Page text without a block of its own (straight in a cell) is single-spaced too. - if self.page && !spaced { - p.spacing.line = Some(LineSpacing::Multiple(1.0)); - } - if let (Some((o, _)), Some(li)) = (list_level, li_at) { - p.list = Some(ListInfo { ordered: o, level: lists.saturating_sub(1).min(8) as u8, start: 1, ..Default::default() }); - self.para_cont = self.stack.get(li).is_some_and(|e| e.used); - if let Some(e) = self.stack.get_mut(li) { - e.used = true; - } - } else { - self.para_cont = false; - } - let pb_before = self.stack.iter_mut().rev().take_while(|e| e.kind != ElKind::Cell).any(|e| std::mem::take(&mut e.page_break)); - p.page_break = pb_before || std::mem::take(&mut self.pending_page_break); - self.para = Some(p); - } - self.para.get_or_insert_with(Para::default) - } - - fn container(&mut self) -> &mut Vec { - if self.containers.is_empty() { - self.containers.push(Vec::new()); - } - let n = self.containers.len() - 1; - &mut self.containers[n] - } - - fn flush(&mut self) { - let Some(mut p) = self.para.take() else { return }; - let (pre_lead, pre_tail) = (self.pre_lead.take().unwrap_or(false), std::mem::take(&mut self.pre_tail)); - if p.kind != Kind::Code { - // Preserved white space keeps its spaces and tabs at the edges; line breaks there - // (source formatting) still go. - const ALL: &[char] = &[' ', '\t', '\r', '\n']; - const BREAKS: &[char] = &['\r', '\n']; - p.trim_with(if pre_lead { BREAKS } else { ALL }, if pre_tail { BREAKS } else { ALL }); - if p.text().trim().is_empty() { - // Only white space (indentation between blocks): nothing to keep. - p.trim(); - } - } - if p.inlines.len() == 1 && p.text() == "\u{00A0}" { - // `

 

`: an empty paragraph on purpose, whose line is as high as the - // space's formatting makes it. - if let Some(Inline::Text(_, f)) = p.inlines.first() { - p.mark = Some(Box::new(crate::model::char_props(f))); - } - p.inlines.clear(); - self.para_cont = false; - self.container().push(FBlock::Para(p)); - return; - } - if p.is_empty() && p.kind != Kind::Code && p.list.is_none() { - // Whitespace-only text between blocks. - return; - } - let cont = std::mem::take(&mut self.para_cont); - let level = p.list.map(|l| l.level); - let c = self.container(); - if cont - && let Some(FBlock::Para(prev)) = c.last_mut() - && prev.list.is_some() - && prev.list.map(|l| l.level) == level - { - prev.push_text("\n", &Fmt::default()); - prev.inlines.extend(p.inlines); - return; - } - c.push(FBlock::Para(p)); - } - - pub(super) fn text(&mut self, t: &str) { - if let Some(title) = &mut self.title { - title.push_str(t); - return; - } - if self.in_head() { - return; - } - if t.is_empty() { - return; - } - let inside_table_only = matches!( - self.stack.iter().rev().find(|e| matches!(e.kind, ElKind::Table | ElKind::Tr | ElKind::Cell)).map(|e| e.kind), - Some(ElKind::Table | ElKind::Tr) - ); - if inside_table_only && t.trim().is_empty() { - return; - } - let code = self.stack.iter().any(|e| e.kind == ElKind::Pre); - let f = self.fmt(); - if code { - let mut first = true; - for line in t.split('\n') { - if !first { - // A newline right after
 is ignored.
-                    let empty_start = self.para.as_ref().is_none_or(|p| p.inlines.is_empty()) && self.pre_fresh();
-                    if !empty_start {
-                        self.start_para();
-                        self.flush();
-                    }
-                }
-                first = false;
-                let line = line.trim_end_matches('\r');
-                if !line.is_empty() {
-                    self.start_para().push_text(line, &Fmt { code: false, ..f.clone() });
-                    self.mark_pre_used();
-                }
-            }
-            return;
-        }
-        if self.preserve() {
-            self.note_edge(true);
-            self.start_para().push_text(t, &f);
-            return;
-        }
-        // Collapse whitespace.
-        let mut s = String::with_capacity(t.len());
-        let mut prev_space = self.para.as_ref().is_none_or(|p| match p.inlines.last() {
-            Some(Inline::Text(x, _)) => x.ends_with([' ', '\n']),
-            Some(_) => false,
-            None => true,
-        });
-        for c in t.chars() {
-            if matches!(c, ' ' | '\t' | '\n' | '\r' | '\u{000C}') {
-                if !prev_space {
-                    s.push(' ');
-                    prev_space = true;
-                }
-            } else {
-                s.push(c);
-                prev_space = false;
-            }
-        }
-        if s.is_empty() {
-            return;
-        }
-        self.note_edge(false);
-        self.start_para().push_text(&s, &f);
-    }
-
-    /// Record whether text about to be added keeps its white space (see `pre_lead`).
-    fn note_edge(&mut self, preserved: bool) {
-        if self.para.as_ref().is_none_or(|p| p.inlines.is_empty()) {
-            self.pre_lead = Some(preserved);
-        }
-        self.pre_tail = preserved;
-    }
-
-    /// Add a `` element's base text and reading as one inline (#1681). Half-marked ruby (no
-    /// base, or no reading) is plain text instead: a guide that annotates nothing is no guide, and
-    /// the text it held is never lost.
-    pub(super) fn push_ruby(&mut self, base: &str, ruby: &str) {
-        let preserve = self.preserve();
-        let text = |s: &str| if preserve { s.to_string() } else { collapse_ws(s) };
-        let (base, ruby) = (text(base), text(ruby));
-        let f = self.fmt();
-        if base.is_empty() || ruby.is_empty() {
-            self.start_para().push_text(if base.is_empty() { &ruby } else { &base }, &f);
-            return;
-        }
-        self.start_para().inlines.push(Inline::Ruby { base, ruby, fmt: f });
-    }
-
-    fn pre_fresh(&self) -> bool {
-        self.stack.iter().rev().find(|e| e.kind == ElKind::Pre).is_some_and(|e| !e.used)
-    }
-
-    fn mark_pre_used(&mut self) {
-        if let Some(e) = self.stack.iter_mut().rev().find(|e| e.kind == ElKind::Pre) {
-            e.used = true;
-        }
-    }
-
-    /// Pop elements down to and including index `k`.
-    fn pop_to(&mut self, k: usize) {
-        while self.stack.len() > k {
-            let Some(e) = self.stack.pop() else { break };
-            self.close(&e);
-        }
-    }
-
-    fn close(&mut self, e: &El) {
-        if e.page_break_after || e.page_break {
-            self.pending_page_break = true;
-        }
-        if e.kind != ElKind::Inline {
-            self.flush();
-        }
-        match e.kind {
-            ElKind::Cell => {
-                self.flush();
-                let blocks = if self.containers.len() > 1 { self.containers.pop().unwrap_or_default() } else { Vec::new() };
-                let mut cell = self.cells.pop().unwrap_or_default();
-                cell.blocks = blocks;
-                if let Some(t) = self.tables.last_mut() {
-                    t.row.get_or_insert_with(Vec::new).push(cell);
-                }
-            }
-            ElKind::Tr => {
-                if let Some(t) = self.tables.last_mut()
-                    && let Some(r) = t.row.take()
-                {
-                    t.rows.push(r);
-                }
-            }
-            ElKind::Table => {
-                if let Some(mut t) = self.tables.pop() {
-                    if let Some(r) = t.row.take() {
-                        t.rows.push(r);
-                    }
-                    let borderless = !t.bordered;
-                    // Keep at most MAX_ROWS rows (the model drops the rest anyway): bounds the
-                    // O(rows × rowspan) work of `insert_covered` on a table of millions of ``.
-                    let rows: Vec<_> = t.rows.into_iter().filter(|r| !r.is_empty()).take(wordcraft_doc::table::MAX_ROWS).collect();
-                    let mut ft = FTable { rows, widths: Vec::new(), borderless, extra: None, kept: None, wrap: None };
-                    if !ft.rows.is_empty() {
-                        ft.insert_covered();
-                        // Columns keep the widths their cells give when every column has one.
-                        if ft.cols() <= MAX_TABLE_COLS {
-                            ft.widths_from_cells();
-                        }
-                        self.container().push(FBlock::Table(ft));
-                    }
-                }
-            }
-            ElKind::Head => {}
-            _ => {}
-        }
-    }
-
-    /// Index of the innermost open element named `name`, not looking past a table boundary
-    /// unless `cross_tables`.
-    fn find_open(&self, pred: impl Fn(&El) -> bool, stop: impl Fn(&El) -> bool) -> Option {
-        for (i, e) in self.stack.iter().enumerate().rev() {
-            if pred(e) {
-                return Some(i);
-            }
-            if stop(e) {
-                return None;
-            }
-        }
-        None
-    }
-
-    pub(super) fn close_head_for_start(&mut self, name: &str) {
-        // A body-content start tag implicitly ends an omitted .
-        if !matches!(
-            name,
-            "html" | "head" | "base" | "basefont" | "bgsound" | "link" | "meta" | "noframes" | "noscript" | "script" | "style" | "template" | "title"
-        ) && let Some(k) = self.find_open(|e| e.kind == ElKind::Head, |_| false)
-        {
-            self.pop_to(k);
-        }
-    }
-
-    fn start_head_metadata(&mut self, name: &str, attrs: &[(String, String)]) -> bool {
-        match name {
-            "title" => self.title = Some(String::new()),
-            "meta" => {
-                let name = attr(attrs, "name").unwrap_or("").to_ascii_lowercase();
-                let content = attr(attrs, "content").unwrap_or("").to_string();
-                match name.as_str() {
-                    "author" => self.meta.author = content,
-                    "description" => self.meta.description = content,
-                    "keywords" => self.meta.keywords = content,
-                    "subject" => self.meta.subject = content,
-                    _ => {}
-                }
-                return true;
-            }
-            _ => {}
-        }
-        false
-    }
-
-    fn close_implicitly(&mut self, name: &str, is_table_part: bool) {
-        if closes_p(name)
-            && let Some(k) = self.find_open(|e| e.name == "p", |e| matches!(e.kind, ElKind::Cell | ElKind::Table | ElKind::Li | ElKind::Quote))
-        {
-            self.pop_to(k);
-        }
-        if matches!(name, "h1" | "h2" | "h3" | "h4" | "h5" | "h6") && self.stack.last().is_some_and(|e| matches!(e.kind, ElKind::Heading(_))) {
-            let k = self.stack.len() - 1;
-            self.pop_to(k);
-        }
-        if name == "li"
-            && let Some(k) = self.find_open(|e| e.kind == ElKind::Li, |e| matches!(e.kind, ElKind::List(_) | ElKind::Table | ElKind::Cell))
-        {
-            self.pop_to(k);
-        }
-        if matches!(name, "dt" | "dd")
-            && let Some(k) =
-                self.find_open(|e| e.name == "dt" || e.name == "dd", |e| matches!(e.kind, ElKind::Table | ElKind::Cell) || e.name == "dl")
-        {
-            self.pop_to(k);
-        }
-        if is_table_part && !self.tables.is_empty() {
-            if matches!(name, "td" | "th")
-                && let Some(k) = self.find_open(|e| e.kind == ElKind::Cell, |e| matches!(e.kind, ElKind::Table | ElKind::Tr))
-            {
-                self.pop_to(k);
-            }
-            if name == "tr"
-                && let Some(k) = self.find_open(|e| matches!(e.kind, ElKind::Tr | ElKind::Cell), |e| e.kind == ElKind::Table)
-            {
-                let mut kk = k;
-                while kk > 0 && self.stack.get(kk - 1).is_some_and(|e| matches!(e.kind, ElKind::Tr | ElKind::Cell)) {
-                    kk -= 1;
-                }
-                self.pop_to(kk);
-            }
-        }
-    }
-
-    fn start_special(&mut self, name: &str, attrs: &[(String, String)]) -> bool {
-        match name {
-            "br" => {
-                if self.stack.iter().any(|e| e.kind == ElKind::Pre) {
-                    self.start_para();
-                    self.flush();
-                } else {
-                    let fmt = self.fmt();
-                    self.start_para().push_text("\n", &fmt);
-                }
-            }
-            "hr" => {
-                self.flush();
-                self.container().push(FBlock::Para(Para::new(Kind::Rule)));
-            }
-            "wbr" => {
-                let fmt = self.fmt();
-                self.start_para().push_text("\u{200B}", &fmt);
-            }
-            "img" => {
-                let alt = attr(attrs, "alt").unwrap_or("").to_string();
-                let dim = |key: &str| attr(attrs, key).and_then(|v| v.trim().trim_end_matches("px").trim().parse::().ok()).map(|p| p * 0.75);
-                let mut width = dim("width");
-                let mut height = dim("height");
-                if let Some(style) = attr(attrs, "style") {
-                    for decl in style.split(';') {
-                        if let Some((key, value)) = decl.split_once(':') {
-                            match key.trim().to_ascii_lowercase().as_str() {
-                                "width" => width = css_len_pt(value).or(width),
-                                "height" => height = css_len_pt(value).or(height),
-                                _ => {}
-                            }
-                        }
-                    }
-                }
-                let image = attr(attrs, "src")
-                    .and_then(|src| if src.trim_start().starts_with("data:") { data_uri(src).map(Arc::new) } else { (self.images)(src.trim()) })
-                    .and_then(|data| make_img(data, width, height, &alt));
-                let fmt = self.fmt();
-                match image {
-                    Some(image) => self.start_para().inlines.push(Inline::Image(image)),
-                    None if !alt.is_empty() => self.start_para().push_text(&alt, &fmt),
-                    None => {}
-                }
-            }
-            _ => return false,
-        }
-        true
-    }
-
-    /// `css`: the element's declarations in cascade order ([`Builder::css_of`]).
-    fn element_style(&self, name: &str, attrs: &[(String, String)], kind: ElKind, css: &str) -> ElementStyle {
-        let mut fmt = self.fmt();
-        let mut align = None;
-        let mut preserve = self.preserve();
-        let mut page_break = false;
-        let mut page_break_after = false;
-        match name {
-            // A header cell is bold, as in browsers and Word, unless its own CSS says otherwise.
-            "b" | "strong" | "th" => fmt.bold = true,
-            "i" | "em" | "cite" | "var" | "dfn" | "address" => fmt.italic = true,
-            "u" | "ins" => fmt.underline = true,
-            "s" | "strike" | "del" => fmt.strike = true,
-            "code" | "kbd" | "tt" | "samp" => fmt.code = true,
-            "sup" => fmt.sup = true,
-            "sub" => fmt.sub = true,
-            "small" => fmt.size = Some(fmt.size.map_or(10.0, |s| (s * 0.833).max(1.0))),
-            "big" => fmt.size = Some(fmt.size.map_or(14.0, |s| (s * 1.2).min(1638.0))),
-            "mark" => fmt.background = Some(wordcraft_doc::Rgb(255, 255, 0)),
-            "font" => apply_font_attributes(attrs, &mut fmt),
-            "a" => {
-                if let Some(href) = attr(attrs, "href").filter(|h| wordcraft_doc::link::is_safe_href(h)) {
-                    fmt.link = model::kept_link(href.trim());
-                }
-            }
-            _ => {}
-        }
-        if let Some(value) = attr(attrs, "align").filter(|_| kind != ElKind::Table) {
-            align = match value.to_ascii_lowercase().as_str() {
-                "center" | "middle" => Some(Align::Center),
-                "right" => Some(Align::Right),
-                "justify" => Some(Align::Justify),
-                "left" => Some(Align::Left),
-                _ => None,
-            };
-        }
-        if name == "center" {
-            align = Some(Align::Center);
-        }
-        if self.page
-            && let ElKind::Heading(n) = kind
-        {
-            // A page's headings are bold, black (unless they inherit a colour) and sized as
-            // browsers show them.
-            fmt.bold = true;
-            fmt.color.get_or_insert(wordcraft_doc::Rgb(0, 0, 0));
-            fmt.size = PAGE_HEADING_SIZES.get(usize::from(n.clamp(1, 6)) - 1).copied();
-        }
-        apply_style(css, &mut fmt, &mut align, &mut preserve, &mut page_break, &mut page_break_after);
-        if kind == ElKind::Pre {
-            preserve = true;
-        }
-        ElementStyle { fmt, align, preserve, page_break, page_break_after }
-    }
-
-    pub(super) fn start(&mut self, name: &str, attrs: &[(String, String)], self_close: bool) {
-        if self.start_head_metadata(name, attrs) {
-            return;
-        }
-        let is_table_part = matches!(name, "tr" | "td" | "th");
-        let in_table = self.tables.len() < model::MAX_DEPTH;
-        self.close_implicitly(name, is_table_part);
-        if name == "a"
-            && !self.in_head()
-            && let Some(id) = attr(attrs, "id").or_else(|| attr(attrs, "name")).filter(|s| !s.is_empty())
-        {
-            self.start_para().inlines.push(Inline::Anchor(id.to_string()));
-        }
-        if self.start_special(name, attrs) {
-            return;
-        }
-        if is_void(name) || self_close && block_kind(name).is_none() && !matches!(name, "a" | "span") {
-            return;
-        }
-        if self.stack.len() >= MAX_STACK {
-            return;
-        }
-        let mut kind = block_kind(name).unwrap_or(ElKind::Inline);
-        if matches!(kind, ElKind::Table | ElKind::Tr | ElKind::Cell) && !in_table {
-            kind = ElKind::Block;
-        }
-        if matches!(kind, ElKind::Tr | ElKind::Cell) && self.tables.is_empty() {
-            kind = ElKind::Block;
-        }
-        if kind != ElKind::Inline {
-            self.flush();
-        }
-        let css = self.css_of(name, attrs);
-        let ElementStyle { mut fmt, align, preserve, page_break, page_break_after } = self.element_style(name, attrs, kind, &css);
-        match kind {
-            ElKind::Table => self.tables.push(TableB { rows: Vec::new(), row: None, bordered: declares_border(attrs, &css, true) }),
-            ElKind::Tr => {
-                if let Some(t) = self.tables.last_mut()
-                    && let Some(r) = t.row.take()
-                {
-                    t.rows.push(r);
-                }
-            }
-            ElKind::Cell => {
-                let tb = self.find_open(|e| e.kind == ElKind::Tr, |e| e.kind == ElKind::Table).is_none();
-                if let Some(t) = self.tables.last_mut()
-                    && declares_border(attrs, &css, false)
-                {
-                    t.bordered = true;
-                }
-                if tb && let Some(t) = self.tables.last_mut() {
-                    // A cell without a row: start one.
-                    if let Some(r) = t.row.take() {
-                        t.rows.push(r);
-                    }
-                    t.row = Some(Vec::new());
-                }
-                let num = |k: &str| attr(attrs, k).and_then(|v| v.trim().parse::().ok()).map(|v| v.clamp(1, 63));
-                let colspan = num("colspan").unwrap_or(1);
-                let shading = attr(attrs, "bgcolor").and_then(parse_color).or(fmt.background);
-                let mut valign = attr(attrs, "valign").and_then(|v| match v.trim().to_ascii_lowercase().as_str() {
-                    "top" => Some(VAlign::Top),
-                    "middle" | "center" => Some(VAlign::Center),
-                    "bottom" => Some(VAlign::Bottom),
-                    _ => None,
-                });
-                for (_, v) in decls(&css).filter(|(k, _)| k == "vertical-align") {
-                    match v.to_ascii_lowercase().as_str() {
-                        "top" => valign = Some(VAlign::Top),
-                        "middle" | "center" => valign = Some(VAlign::Center),
-                        "bottom" => valign = Some(VAlign::Bottom),
-                        _ => {}
-                    }
-                }
-                self.cells.push(Cell {
-                    blocks: Vec::new(),
-                    colspan,
-                    rowspan: attr(attrs, "rowspan").and_then(|v| v.trim().parse::().ok()).map(|v| v.clamp(1, 1000)).unwrap_or(1),
-                    covered: false,
-                    header: name == "th",
-                    shading,
-                    borders: cell_borders(&css),
-                    valign,
-                    margins: None,
-                    direction: Default::default(),
-                    picture: None,
-                    width: if colspan == 1 { cell_width(&css, attrs) } else { None },
-                });
-                fmt.background = None;
-                self.containers.push(Vec::new());
-            }
-            _ => {}
-        }
-        // The page's own margins (`body`, `html`) aren't its paragraphs'.
-        let spacing =
-            if (kind != ElKind::Inline || self.page) && !matches!(name, "body" | "html") { block_spacing(&css) } else { Spacing::default() };
-        let auto_margins = self.page && (name == "p" || matches!(kind, ElKind::Heading(_)));
-        self.stack.push(El { name: name.to_string(), kind, fmt, spacing, auto_margins, align, preserve, page_break, page_break_after, used: false });
-    }
-
-    pub(super) fn end(&mut self, name: &str) {
-        if name == "title" {
-            if let Some(t) = self.title.take() {
-                self.meta.title = t.split_whitespace().collect::>().join(" ");
-            }
-            return;
-        }
-        let crosses = matches!(name, "table");
-        let target = if matches!(name, "td" | "th") {
-            self.find_open(|e| e.kind == ElKind::Cell, |e| e.kind == ElKind::Table)
-        } else if name == "tr" {
-            self.find_open(|e| e.kind == ElKind::Tr, |e| e.kind == ElKind::Table)
-        } else {
-            self.find_open(|e| e.name == name, |e| !crosses && matches!(e.kind, ElKind::Table | ElKind::Cell))
-        };
-        if let Some(k) = target {
-            self.pop_to(k);
-        }
-    }
-
-    pub(super) fn finish(mut self) -> Flow {
-        self.pop_to(0);
-        self.flush();
-        // Unclosed cells: fold remaining containers into the body.
-        while self.containers.len() > 1 {
-            let c = self.containers.pop().unwrap_or_default();
-            self.container().extend(c);
-        }
-        let blocks = self.containers.pop().unwrap_or_default();
-        if let Some(t) = self.title.take() {
-            self.meta.title = t;
-        }
-        Flow { blocks, meta: self.meta }
-    }
-}
diff --git a/crates/formats/src/html/css.rs b/crates/formats/src/html/css.rs
deleted file mode 100644
index 9dc6d75262..0000000000
--- a/crates/formats/src/html/css.rs
+++ /dev/null
@@ -1,167 +0,0 @@
-use super::builder::decls;
-use super::import::PAGE_FONT;
-use super::{Align, Fmt, model, parse_color};
-
-/// A CSS length (or font-size keyword) in points; `None` when it is not one.
-pub fn css_len_pt(v: &str) -> Option {
-    let v = v.trim().to_ascii_lowercase();
-    let num = |s: &str| s.trim().parse::().ok().filter(|x| x.is_finite());
-    if let Some(x) = v.strip_suffix("pt") {
-        num(x)
-    } else if let Some(x) = v.strip_suffix("px") {
-        num(x).map(|p| p * 0.75)
-    } else if let Some(x) = v.strip_suffix("rem").or_else(|| v.strip_suffix("em")) {
-        num(x).map(|p| p * 12.0)
-    } else if let Some(x) = v.strip_suffix("in") {
-        num(x).map(|p| p * 72.0)
-    } else if let Some(x) = v.strip_suffix("cm") {
-        num(x).map(|p| p * 72.0 / 2.54)
-    } else if let Some(x) = v.strip_suffix("mm") {
-        num(x).map(|p| p * 72.0 / 25.4)
-    } else if let Some(x) = v.strip_suffix('%') {
-        num(x).map(|p| p * 0.12)
-    } else {
-        match v.as_str() {
-            "xx-small" => Some(7.0),
-            "x-small" => Some(7.5),
-            "small" | "smaller" => Some(10.0),
-            "medium" => Some(12.0),
-            "large" | "larger" => Some(13.5),
-            "x-large" => Some(18.0),
-            "xx-large" => Some(24.0),
-            "xxx-large" => Some(36.0),
-            _ => num(&v).map(|p| p * 0.75),
-        }
-    }
-}
-
-pub(super) fn strip_important(v: &str) -> &str {
-    let s = v.trim();
-    if let Some(pos) = s.rfind('!') {
-        let after = s[pos + 1..].trim();
-        if after.eq_ignore_ascii_case("important") {
-            return s[..pos].trim();
-        }
-    }
-    s
-}
-
-/// Decode CSS escapes (CSS Syntax §4.3.7): `\` and one to six hex digits, with one whitespace
-/// after them, is that code point; `\` and any other character is that character. CR LF, CR and
-/// form feed count as one newline, as CSS reads them (§3.3).
-pub(super) fn css_unescape(s: &str) -> String {
-    let mut o = String::with_capacity(s.len());
-    let mut it = s.chars().peekable();
-    // Takes a newline (CR LF as one) or other whitespace off the front, if there is one.
-    let skip_space = |it: &mut std::iter::Peekable| match it.peek() {
-        Some('\r') => {
-            it.next();
-            it.next_if_eq(&'\n');
-        }
-        Some(' ' | '\t' | '\n' | '\u{C}') => {
-            it.next();
-        }
-        _ => {}
-    };
-    while let Some(c) = it.next() {
-        if c != '\\' {
-            o.push(c);
-            continue;
-        }
-        let mut hex = String::new();
-        while hex.len() < 6 && it.peek().is_some_and(char::is_ascii_hexdigit) {
-            hex.extend(it.next());
-        }
-        if hex.is_empty() {
-            // Before a newline it continues the line; at the end it is dropped.
-            match it.peek() {
-                Some('\r' | '\n' | '\u{C}') => skip_space(&mut it),
-                Some(_) => o.extend(it.next()),
-                None => {}
-            }
-            continue;
-        }
-        skip_space(&mut it);
-        let cp = u32::from_str_radix(&hex, 16).unwrap_or(0);
-        o.push(char::from_u32(cp).filter(|c| *c != '\0').unwrap_or('\u{FFFD}'));
-    }
-    o
-}
-
-/// Apply a `style` attribute to formatting / block properties.
-pub(super) fn apply_style(
-    css: &str,
-    f: &mut Fmt,
-    align: &mut Option,
-    preserve: &mut bool,
-    page_break: &mut bool,
-    page_break_after: &mut bool,
-) {
-    for (k, v) in decls(css) {
-        let lv = v.to_ascii_lowercase();
-        match k.as_str() {
-            "font-weight" => f.bold = lv.starts_with("bold") || lv.parse::().is_ok_and(|w| w >= 600),
-            "font-style" => f.italic = lv == "italic" || lv == "oblique",
-            "text-decoration" | "text-decoration-line" => {
-                if lv.contains("none") {
-                    f.underline = false;
-                    f.strike = false;
-                }
-                if lv.contains("underline") {
-                    f.underline = true;
-                }
-                if lv.contains("line-through") {
-                    f.strike = true;
-                }
-            }
-            "color" => {
-                if let Some(c) = parse_color(v) {
-                    f.color = Some(c);
-                }
-            }
-            "background-color" | "background" => {
-                if let Some(c) = v.split_whitespace().find_map(parse_color) {
-                    f.background = Some(c);
-                }
-            }
-            "font-size" => {
-                if let Some(p) = css_len_pt(v).filter(|p| *p >= 1.0 && *p <= 1638.0) {
-                    f.size = Some(p);
-                }
-            }
-            "font-family" => {
-                let fam = css_unescape(v.split(',').next().unwrap_or("").trim().trim_matches(['"', '\'']));
-                if model::is_mono(&fam) || fam.eq_ignore_ascii_case("monospace") {
-                    f.code = true;
-                } else if !fam.is_empty() && !matches!(fam.to_ascii_lowercase().as_str(), "serif" | "sans-serif" | "inherit" | "initial") {
-                    f.font = model::kept_font(&fam).or(f.font.take());
-                } else if f.font.is_some() {
-                    // A generic family replaces a face the text has (a page's Times New Roman);
-                    // text without one keeps the document's font.
-                    match fam.to_ascii_lowercase().as_str() {
-                        "serif" => f.font = Some(PAGE_FONT.into()),
-                        "sans-serif" => f.font = Some("Arial".into()),
-                        _ => {}
-                    }
-                }
-            }
-            "vertical-align" => {
-                f.sup = lv == "super";
-                f.sub = lv == "sub";
-            }
-            "text-align" => {
-                *align = match lv.as_str() {
-                    "center" => Some(Align::Center),
-                    "right" | "end" => Some(Align::Right),
-                    "justify" => Some(Align::Justify),
-                    "left" | "start" => Some(Align::Left),
-                    _ => *align,
-                }
-            }
-            "white-space" => *preserve = lv.starts_with("pre"),
-            "page-break-before" | "break-before" => *page_break = lv == "always" || lv == "page",
-            "page-break-after" | "break-after" => *page_break_after = lv == "always" || lv == "page",
-            _ => {}
-        }
-    }
-}
diff --git a/crates/formats/src/html/entity.rs b/crates/formats/src/html/entity.rs
deleted file mode 100644
index a77e771117..0000000000
--- a/crates/formats/src/html/entity.rs
+++ /dev/null
@@ -1,370 +0,0 @@
-// ---------------------------------------------------------------------------------------------
-// Entities and decoding
-
-/// Decode an entity body (`amp`, `#38`, `#x26`) to its text.
-pub fn decode_entity(name: &str) -> Option {
-    decode_char(name).map(String::from)
-}
-
-fn decode_char(name: &str) -> Option {
-    if let Some(num) = name.strip_prefix('#') {
-        let (digits, radix) = match num.strip_prefix(['x', 'X']) {
-            Some(h) => (h, 16),
-            None => (num, 10),
-        };
-        if !digits.chars().all(|c| c.is_digit(radix)) {
-            return None;
-        }
-        let v = u32::from_str_radix(digits, radix).ok()?;
-        let c = match v {
-            0 => '\u{FFFD}',
-            0x80..=0x9F => crate::txt::cp1252(v as u8),
-            _ => char::from_u32(v).unwrap_or('\u{FFFD}'),
-        };
-        return Some(c);
-    }
-    let c = match name {
-        "amp" | "AMP" => '&',
-        "lt" | "LT" => '<',
-        "gt" | "GT" => '>',
-        "quot" | "QUOT" => '"',
-        "apos" | "APOS" => '\'',
-        "nbsp" | "NBSP" => '\u{00A0}',
-        "ensp" => '\u{2002}',
-        "emsp" => '\u{2003}',
-        "thinsp" => '\u{2009}',
-        "shy" => '\u{00AD}',
-        "copy" | "COPY" => '©',
-        "reg" | "REG" => '®',
-        "trade" | "TRADE" => '™',
-        "hellip" => '…',
-        "mdash" => '—',
-        "ndash" => '–',
-        "minus" => '−',
-        "lsquo" => '‘',
-        "rsquo" => '’',
-        "sbquo" => '‚',
-        "ldquo" => '“',
-        "rdquo" => '”',
-        "bdquo" => '„',
-        "laquo" => '«',
-        "raquo" => '»',
-        "bull" => '•',
-        "middot" => '·',
-        "deg" => '°',
-        "plusmn" => '±',
-        "times" => '×',
-        "divide" => '÷',
-        "frac12" => '½',
-        "frac14" => '¼',
-        "frac34" => '¾',
-        "frac13" => '⅓',
-        "frac23" => '⅔',
-        "frac18" => '⅛',
-        "frac38" => '⅜',
-        "frac58" => '⅝',
-        "frac78" => '⅞',
-        "sup1" => '¹',
-        "sup2" => '²',
-        "sup3" => '³',
-        "micro" => 'µ',
-        "permil" => '‰',
-        "prime" => '′',
-        "Prime" => '″',
-        "oline" => '‾',
-        "frasl" => '⁄',
-        "sect" => '§',
-        "para" => '¶',
-        "euro" | "EURO" => '€',
-        "pound" | "POUND" => '£',
-        "yen" | "YEN" => '¥',
-        "cent" | "CENT" => '¢',
-        "curren" => '¤',
-        "dagger" => '†',
-        "Dagger" => '‡',
-        "larr" => '←',
-        "rarr" => '→',
-        "uarr" => '↑',
-        "darr" => '↓',
-        "harr" => '↔',
-        "lArr" => '⇐',
-        "rArr" => '⇒',
-        "uArr" => '⇑',
-        "dArr" => '⇓',
-        "hArr" => '⇔',
-        "crarr" => '↵',
-        "forall" => '∀',
-        "part" => '∂',
-        "exist" => '∃',
-        "empty" => '∅',
-        "nabla" => '∇',
-        "isin" => '∈',
-        "notin" => '∉',
-        "ni" => '∋',
-        "prod" => '∏',
-        "sum" => '∑',
-        "radic" => '√',
-        "prop" => '∝',
-        "ang" => '∠',
-        "and" => '∧',
-        "or" => '∨',
-        "cap" => '∩',
-        "cup" => '∪',
-        "int" => '∫',
-        "there4" => '∴',
-        "sim" => '∼',
-        "cong" => '≅',
-        "asymp" | "approx" => '≈',
-        "ne" => '≠',
-        "equiv" => '≡',
-        "le" => '≤',
-        "ge" => '≥',
-        "sub" => '⊂',
-        "sup" => '⊃',
-        "nsub" => '⊄',
-        "sube" => '⊆',
-        "supe" => '⊇',
-        "oplus" => '⊕',
-        "otimes" => '⊗',
-        "perp" => '⊥',
-        "sdot" => '⋅',
-        "infin" => '∞',
-        "loz" => '◊',
-        "spades" => '♠',
-        "clubs" => '♣',
-        "hearts" => '♥',
-        "diams" => '♦',
-        "check" => '✓',
-        "iexcl" => '¡',
-        "iquest" => '¿',
-        "szlig" => 'ß',
-        "auml" => 'ä',
-        "ouml" => 'ö',
-        "uuml" => 'ü',
-        "Auml" => 'Ä',
-        "Ouml" => 'Ö',
-        "Uuml" => 'Ü',
-        "eacute" => 'é',
-        "egrave" => 'è',
-        "ecirc" => 'ê',
-        "euml" => 'ë',
-        "Eacute" => 'É',
-        "Egrave" => 'È',
-        "Ecirc" => 'Ê',
-        "Euml" => 'Ë',
-        "aacute" => 'á',
-        "agrave" => 'à',
-        "acirc" => 'â',
-        "atilde" => 'ã',
-        "Agrave" => 'À',
-        "Aacute" => 'Á',
-        "Acirc" => 'Â',
-        "Atilde" => 'Ã',
-        "iacute" => 'í',
-        "igrave" => 'ì',
-        "icirc" => 'î',
-        "iuml" => 'ï',
-        "Iacute" => 'Í',
-        "Igrave" => 'Ì',
-        "Icirc" => 'Î',
-        "Iuml" => 'Ï',
-        "oacute" => 'ó',
-        "ograve" => 'ò',
-        "ocirc" => 'ô',
-        "otilde" => 'õ',
-        "Oacute" => 'Ó',
-        "Ograve" => 'Ò',
-        "Ocirc" => 'Ô',
-        "Otilde" => 'Õ',
-        "uacute" => 'ú',
-        "ugrave" => 'ù',
-        "ucirc" => 'û',
-        "Uacute" => 'Ú',
-        "Ugrave" => 'Ù',
-        "Ucirc" => 'Û',
-        "yacute" => 'ý',
-        "yuml" => 'ÿ',
-        "Yacute" => 'Ý',
-        "ntilde" => 'ñ',
-        "Ntilde" => 'Ñ',
-        "ccedil" => 'ç',
-        "Ccedil" => 'Ç',
-        "aring" => 'å',
-        "Aring" => 'Å',
-        "oslash" => 'ø',
-        "Oslash" => 'Ø',
-        "aelig" => 'æ',
-        "AElig" => 'Æ',
-        "alpha" => 'α',
-        "beta" => 'β',
-        "gamma" => 'γ',
-        "delta" => 'δ',
-        "epsilon" => 'ε',
-        "zeta" => 'ζ',
-        "eta" => 'η',
-        "theta" => 'θ',
-        "iota" => 'ι',
-        "kappa" => 'κ',
-        "lambda" => 'λ',
-        "mu" => 'μ',
-        "nu" => 'ν',
-        "xi" => 'ξ',
-        "pi" => 'π',
-        "rho" => 'ρ',
-        "sigma" => 'σ',
-        "tau" => 'τ',
-        "upsilon" => 'υ',
-        "phi" => 'φ',
-        "chi" => 'χ',
-        "psi" => 'ψ',
-        "omega" => 'ω',
-        "Gamma" => 'Γ',
-        "Delta" => 'Δ',
-        "Theta" => 'Θ',
-        "Lambda" => 'Λ',
-        "Xi" => 'Ξ',
-        "Pi" => 'Π',
-        "Sigma" => 'Σ',
-        "Upsilon" => 'Υ',
-        "Phi" => 'Φ',
-        "Psi" => 'Ψ',
-        "Omega" => 'Ω',
-        "zwj" => '\u{200D}',
-        "zwnj" => '\u{200C}',
-        _ => return None,
-    };
-    Some(c)
-}
-
-/// Legacy names HTML5 also decodes without a `;` (only those [`decode_char`] knows), sorted by bytes.
-const BARE_NAMES: &[&str] = &[
-    "AElig", "AMP", "Aacute", "Acirc", "Agrave", "Aring", "Atilde", "Auml", "COPY", "Ccedil", "Eacute", "Ecirc", "Egrave", "Euml", "GT", "Iacute",
-    "Icirc", "Igrave", "Iuml", "LT", "Ntilde", "Oacute", "Ocirc", "Ograve", "Oslash", "Otilde", "Ouml", "QUOT", "REG", "Uacute", "Ucirc", "Ugrave",
-    "Uuml", "Yacute", "aacute", "acirc", "aelig", "agrave", "amp", "aring", "atilde", "auml", "ccedil", "cent", "copy", "curren", "deg", "divide",
-    "eacute", "ecirc", "egrave", "euml", "frac12", "frac14", "frac34", "gt", "iacute", "icirc", "iexcl", "igrave", "iquest", "iuml", "laquo", "lt",
-    "micro", "middot", "nbsp", "ntilde", "oacute", "ocirc", "ograve", "oslash", "otilde", "ouml", "para", "plusmn", "pound", "quot", "raquo", "reg",
-    "sect", "shy", "sup1", "sup2", "sup3", "szlig", "times", "uacute", "ucirc", "ugrave", "uuml", "yacute", "yen", "yuml",
-];
-
-/// A reference written without its `;` at the start of `after` (the text after `&`): a numeric one,
-/// or the longest legacy name. In attribute values a name followed by `=` or an alphanumeric stays text.
-fn bare_reference(after: &str, attr: bool) -> Option<(char, usize)> {
-    if let Some(num) = after.strip_prefix('#') {
-        let hex = num.starts_with(['x', 'X']);
-        let radix = if hex { 16 } else { 10 };
-        let skip = usize::from(hex);
-        let n = num.get(skip..)?.bytes().take_while(|b| char::from(*b).is_digit(radix)).count();
-        if n == 0 {
-            return None;
-        }
-        let len = 1 + skip + n;
-        return decode_char(after.get(..len)?).map(|c| (c, len));
-    }
-    // Legacy names are 2 to 6 ASCII alphanumerics: try the longest prefix first.
-    let run = after.bytes().take(6).take_while(u8::is_ascii_alphanumeric).count();
-    let len = (2..=run).rev().find(|&l| after.get(..l).is_some_and(|p| BARE_NAMES.binary_search(&p).is_ok()))?;
-    let next = after.as_bytes().get(len);
-    if attr && next.is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'=') {
-        return None;
-    }
-    decode_char(after.get(..len)?).map(|c| (c, len))
-}
-
-/// Replace entity references in text `s`.
-pub fn unescape(s: &str) -> String {
-    unescape_in(s, false)
-}
-
-/// Replace entity references in an attribute value `s`.
-pub fn unescape_attr(s: &str) -> String {
-    unescape_in(s, true)
-}
-
-fn unescape_in(s: &str, attr: bool) -> String {
-    if !s.contains('&') {
-        return s.to_string();
-    }
-    let mut out = String::with_capacity(s.len());
-    let mut rest = s;
-    while let Some(k) = rest.find('&') {
-        out.push_str(&rest[..k]);
-        let after = &rest[k + 1..];
-        // An entity name is short; looking further would rescan the whole text for every stray `&`.
-        let mut cut = after.len().min(33);
-        while !after.is_char_boundary(cut) {
-            cut -= 1;
-        }
-        let end = after.get(..cut).and_then(|w| w.find(';'));
-        match end.and_then(|e| decode_char(&after[..e]).map(|c| (c, e + 1))).or_else(|| bare_reference(after, attr)) {
-            Some((c, e)) => {
-                out.push(c);
-                rest = &after[e..];
-            }
-            None => {
-                out.push('&');
-                rest = after;
-            }
-        }
-    }
-    out.push_str(rest);
-    out
-}
-
-fn sniff_charset(head: &str) -> Option<&str> {
-    let (i, n) = head.find("charset").map(|i| (i, 7)).or_else(|| head.find("encoding").map(|i| (i, 8)))?;
-    let after = head.get(i + n..)?.trim_start().strip_prefix('=')?;
-    let trimmed = after.trim_start().trim_start_matches(['"', '\'']);
-    let end = trimmed.find(|c: char| c.is_whitespace() || matches!(c, '"' | '\'' | ';' | '>' | '/')).unwrap_or(trimmed.len());
-    let label = trimmed[..end].trim();
-    if label.is_empty() { None } else { Some(label) }
-}
-
-/// Decode HTML bytes: BOM, then a `charset` in the first 2 KB, else UTF-8 with a legacy fallback.
-pub fn decode(b: &[u8]) -> String {
-    decode_as(b, None)
-}
-
-/// [`decode`] in the `charset` its container states (a MIME part's, a content type's), when it
-/// states one. Single-byte Windows code pages (`windows-1250`…, `iso-8859-1`, `us-ascii` as 1252)
-/// are decoded as such, other labels by their WHATWG decoders.
-pub fn decode_as(b: &[u8], charset: Option<&str>) -> String {
-    if b.starts_with(&[0xFF, 0xFE]) || b.starts_with(&[0xFE, 0xFF]) || b.starts_with(&[0xEF, 0xBB, 0xBF]) {
-        return crate::txt::decode(b);
-    }
-    let head = String::from_utf8_lossy(b.get(..b.len().min(2048)).unwrap_or(b)).to_ascii_lowercase();
-    let named = charset.map(str::to_ascii_lowercase).or_else(|| sniff_charset(&head).map(str::to_string));
-    let code_page = match named.as_deref().map(str::trim) {
-        Some("iso-8859-1" | "latin1" | "us-ascii") => Some(1252),
-        Some(cs) => cs
-            .strip_prefix("windows-")
-            .or_else(|| cs.strip_prefix("cp"))
-            .and_then(|n| n.parse::().ok())
-            .filter(|cp| wordcraft_doc::encoding::SINGLE_BYTE_CODE_PAGES.contains(cp)),
-        None => None,
-    };
-    // Other labels (ISO 8859, KOI8, Shift_JIS, GBK, Big5, EUC-KR…) by their WHATWG decoders; a
-    // UTF-16 one without a BOM means UTF-8, as in browsers.
-    let other = named
-        .as_deref()
-        .and_then(|l| encoding_rs::Encoding::for_label(l.trim().as_bytes()))
-        .filter(|e| ![encoding_rs::UTF_8, encoding_rs::UTF_16LE, encoding_rs::UTF_16BE].contains(e));
-    match (code_page, other) {
-        (Some(cp), _) => b.iter().map(|&c| wordcraft_doc::encoding::windows_byte(cp, c)).collect(),
-        (None, Some(enc)) => enc.decode_without_bom_handling(b).0.into_owned(),
-        (None, None) => match std::str::from_utf8(b) {
-            Ok(s) => s.to_string(),
-            Err(_) => crate::txt::decode(b),
-        },
-    }
-}
-
-#[cfg(test)]
-mod tests {
-    use super::*;
-
-    #[test]
-    fn bare_names_are_sorted_and_known() {
-        assert!(BARE_NAMES.windows(2).all(|w| w[0] < w[1]));
-        assert!(BARE_NAMES.iter().all(|n| (2..=6).contains(&n.len()) && decode_char(n).is_some()));
-    }
-}
diff --git a/crates/formats/src/html/export.rs b/crates/formats/src/html/export.rs
deleted file mode 100644
index d0113c2c68..0000000000
--- a/crates/formats/src/html/export.rs
+++ /dev/null
@@ -1,393 +0,0 @@
-use super::{Align, Document, FBlock, FTable, Flow, Fmt, Inline, Kind, Para, VAlign, base64_encode, mime_of, model};
-
-// ---------------------------------------------------------------------------------------------
-// Export
-
-/// Escape text for HTML content and attribute values.
-pub fn esc(s: &str) -> String {
-    let mut o = String::with_capacity(s.len() + 8);
-    for c in s.chars() {
-        match c {
-            '&' => o.push_str("&"),
-            '<' => o.push_str("<"),
-            '>' => o.push_str(">"),
-            '"' => o.push_str("""),
-            '\u{000C}' | '\u{000E}' => {}
-            _ => o.push(c),
-        }
-    }
-    o
-}
-
-/// A font name as a quoted CSS string. Quotes, `\` and control characters would end the string,
-/// and `;` and `,` the declaration or the family list for a reader that splits on them, so all of
-/// these are written as hex escapes ([`css_unescape`] reads them back).
-fn css_font_name(name: &str) -> String {
-    let mut o = String::with_capacity(name.len() + 2);
-    o.push('\'');
-    for c in name.chars() {
-        match c {
-            '\0' => o.push('\u{FFFD}'),
-            '\'' | '"' | '\\' | ';' | ',' => o.push_str(&format!("\\{:x} ", u32::from(c))),
-            c if c.is_control() => o.push_str(&format!("\\{:x} ", u32::from(c))),
-            c => o.push(c),
-        }
-    }
-    o.push('\'');
-    o
-}
-
-fn align_css(a: Option) -> Option<&'static str> {
-    match a? {
-        Align::Center => Some("text-align:center"),
-        Align::Right => Some("text-align:right"),
-        Align::Justify | Align::Distribute => Some("text-align:justify"),
-        Align::Left => None,
-    }
-}
-
-fn text_html(t: &str, at_start: bool) -> String {
-    let mut o = String::new();
-    let mut prev_space = at_start;
-    for c in t.chars() {
-        match c {
-            '\n' => {
-                o.push_str("
"); - prev_space = true; - continue; - } - '\t' => o.push_str("\t"), - ' ' if prev_space => o.push_str(" "), - _ => o.push_str(&esc(&c.to_string())), - } - prev_space = c == ' '; - } - o -} - -/// The tags that carry `f`'s character formatting: what opens them, and what closes them -/// (innermost last, so the closes are written in reverse). -fn fmt_tags(f: &Fmt) -> (String, Vec<&'static str>) { - let mut open = String::new(); - let mut close: Vec<&str> = Vec::new(); - // A script or other non-web target is written as plain text (`is_safe_href`). - if let Some(l) = f.link.as_deref().filter(|l| wordcraft_doc::link::is_safe_href(l)) { - open.push_str(&format!("", esc(l))); - close.push(""); - } - let mut tag = |o: &str, c: &'static str| { - open.push_str(o); - close.push(c); - }; - if f.bold { - tag("", ""); - } - if f.italic { - tag("", ""); - } - if f.underline { - tag("", ""); - } - if f.strike { - tag("", ""); - } - if f.sup { - tag("", ""); - } else if f.sub { - tag("", ""); - } - if f.code { - tag("", ""); - } - let mut css = Vec::new(); - if let Some(c) = f.color { - css.push(format!("color:#{}", c.hex())); - } - if let Some(c) = f.background { - css.push(format!("background-color:#{}", c.hex())); - } - if let Some(s) = f.size { - css.push(format!("font-size:{}pt", trim_num(s))); - } - if let Some(fam) = &f.font { - css.push(format!("font-family:{}", esc(&css_font_name(fam)))); - } - if !css.is_empty() { - open.push_str(&format!("", css.join(";"))); - close.push(""); - } - (open, close) -} - -/// Inline spans → HTML. -pub fn inlines_html(inlines: &[Inline]) -> String { - let mut o = String::new(); - for i in inlines.iter() { - match i { - Inline::Text(t, f) => { - let (open, close) = fmt_tags(f); - let at_start = o.is_empty(); - o.push_str(&open); - o.push_str(&text_html(t, at_start)); - for c in close.iter().rev() { - o.push_str(c); - } - } - // A phonetic guide: the base text with its reading in `` (#1681). HTML ruby has - // no alignment or size, so the guide's own attributes stay in the document model. - Inline::Ruby { base, ruby, fmt } => { - let (open, close) = fmt_tags(fmt); - let at_start = o.is_empty(); - o.push_str(&open); - o.push_str(&format!("{}{}", text_html(base, at_start), text_html(ruby, false))); - for c in close.iter().rev() { - o.push_str(c); - } - } - Inline::Image(img) => { - o.push_str(&format!( - "\"{}\"", - mime_of(&img.ext), - base64_encode(&img.data), - esc(&img.alt), - (img.w / 0.75).round(), - (img.h / 0.75).round() - )); - } - Inline::Anchor(a) => o.push_str(&format!("", esc(a))), - Inline::Note { .. } - | Inline::Comment { .. } - | Inline::CommentEnd(_) - | Inline::Shape { .. } - | Inline::Group { .. } - | Inline::FieldStart(_) - | Inline::FieldEnd - | Inline::AnchorEnd(_) => {} - // ASK and SET show nothing (see `model::shows_nothing`). - Inline::Field { instr, .. } if model::shows_nothing(instr) => {} - Inline::Field { result, fmt, .. } | Inline::Control { text: result, fmt, .. } => { - let (open, close) = fmt_tags(fmt); - let at_start = o.is_empty(); - o.push_str(&open); - o.push_str(&text_html(result, at_start)); - for c in close.iter().rev() { - o.push_str(c); - } - } - // No picture to show: the browser shows the alt text in its place. - Inline::Figure(alt) | Inline::Chart(crate::model::Chart { alt, .. }) => o.push_str(&format!("\"{}\"", esc(alt))), - // MathML, which browsers, Word and LibreOffice show as an equation (#816). - Inline::Equation { linear, display } => o.push_str(&crate::mathml::linear_to_mathml(linear, *display)), - } - } - o -} - -fn trim_num(v: f32) -> String { - let s = format!("{:.2}", v); - s.trim_end_matches('0').trim_end_matches('.').to_string() -} - -fn para_attrs(p: &Para) -> String { - let mut css: Vec<&str> = Vec::new(); - if let Some(a) = align_css(p.align) { - css.push(a); - } - if p.page_break { - css.push("break-before:page;page-break-before:always"); - } - if css.is_empty() { String::new() } else { format!(" style=\"{}\"", css.join(";")) } -} - -fn para_html(p: &Para) -> String { - let body = inlines_html(&p.inlines); - let attrs = para_attrs(p); - match p.kind { - Kind::Heading(n) => format!("{body}", n = n.clamp(1, 6)), - Kind::Title => format!("{body}"), - Kind::Rule => "
".to_string(), - _ if body.is_empty() => format!(" 

"), - _ => format!("{body}

"), - } -} - -fn table_html(t: &FTable, out: &mut String, depth: usize) { - out.push_str("\n"); - for row in &t.rows { - out.push_str(""); - for c in row { - if c.covered { - continue; - } - let tag = if c.header { "th" } else { "td" }; - let mut attrs = String::new(); - if c.colspan > 1 { - attrs.push_str(&format!(" colspan=\"{}\"", c.colspan)); - } - if c.rowspan > 1 { - attrs.push_str(&format!(" rowspan=\"{}\"", c.rowspan)); - } - let v_css = match c.valign { - Some(VAlign::Center) => "vertical-align:middle", - Some(VAlign::Bottom) => "vertical-align:bottom", - _ => "vertical-align:top", - }; - let mut css = vec!["padding:4px 6px".to_string(), v_css.to_string()]; - if !t.borderless { - css.insert(0, "border:1px solid #999".to_string()); - } - if let Some(s) = c.shading { - css.push(format!("background-color:#{}", s.hex())); - } - let single = match c.blocks.as_slice() { - [FBlock::Para(p)] if p.kind == Kind::Normal && p.list.is_none() && !p.page_break => Some(p), - _ => None, - }; - if let Some(a) = single.and_then(|p| align_css(p.align)) { - css.push(a.to_string()); - } - out.push_str(&format!("<{tag}{attrs} style=\"{}\">", css.join(";"))); - match single { - Some(p) => out.push_str(&inlines_html(&p.inlines)), - None => { - out.push('\n'); - blocks_html(&c.blocks, out, depth + 1); - } - } - out.push_str(&format!("")); - } - out.push_str("\n"); - } - out.push_str("
\n"); -} - -fn blocks_html(blocks: &[FBlock], out: &mut String, depth: usize) { - let mut i = 0; - while let Some(b) = blocks.get(i) { - match b { - FBlock::Table(t) => { - if depth < model::MAX_DEPTH { - table_html(t, out, depth); - } - i += 1; - } - FBlock::Para(p) if p.list.is_some() => { - // Nested lists from consecutive list paragraphs. - let mut stack: Vec<(bool, bool)> = Vec::new(); // (ordered, li open) - while let Some(FBlock::Para(q)) = blocks.get(i) { - let Some(li) = q.list else { break }; - let lv = li.level.min(8) as usize; - while stack.len() > lv + 1 { - close_list(&mut stack, out); - } - if stack.len() == lv + 1 && stack.last().is_some_and(|s| s.0 != li.ordered) { - close_list(&mut stack, out); - } - while stack.len() < lv + 1 { - if let Some(top) = stack.last_mut() - && !top.1 - { - out.push_str("
  • "); - top.1 = true; - } - out.push_str(if li.ordered { "
      " } else { "
        " }); - stack.push((li.ordered, false)); - } - if let Some(top) = stack.last_mut() { - if top.1 { - out.push_str("\n"); - } - top.1 = true; - } - let body = inlines_html(&q.inlines); - let attrs = para_attrs(q); - out.push_str(&format!("{body}")); - i += 1; - } - while !stack.is_empty() { - close_list(&mut stack, out); - } - out.push('\n'); - } - FBlock::Para(p) if p.kind == Kind::Code => { - out.push_str("
        ");
        -                let mut first = true;
        -                while let Some(FBlock::Para(q)) = blocks.get(i) {
        -                    if q.kind != Kind::Code || q.list.is_some() {
        -                        break;
        -                    }
        -                    if !first {
        -                        out.push('\n');
        -                    }
        -                    first = false;
        -                    out.push_str(&esc(&q.text()));
        -                    i += 1;
        -                }
        -                out.push_str("
        \n"); - } - FBlock::Para(p) if p.kind == Kind::Quote => { - out.push_str("
        \n"); - while let Some(FBlock::Para(q)) = blocks.get(i) { - if q.kind != Kind::Quote || q.list.is_some() { - break; - } - out.push_str(¶_html(&Para { kind: Kind::Normal, ..q.clone() })); - out.push('\n'); - i += 1; - } - out.push_str("
        \n"); - } - FBlock::Para(p) => { - out.push_str(¶_html(p)); - out.push('\n'); - i += 1; - } - } - } -} - -fn close_list(stack: &mut Vec<(bool, bool)>, out: &mut String) { - if let Some((ordered, li_open)) = stack.pop() { - if li_open { - out.push_str(""); - } - out.push_str(if ordered { "
    " } else { "" }); - } -} - -/// A complete HTML document. -pub fn export(doc: &Document) -> String { - let flow = model::from_doc(doc); - export_flow(&flow, doc.styles.default_chr.lang.as_deref().unwrap_or("en")) -} - -/// `doc`'s content as an HTML fragment (no head or style sheet): what a copy puts on the system -/// clipboard beside its text, equations as MathML (#816). -pub fn export_fragment(doc: &Document) -> String { - let flow = model::from_doc(doc); - let mut out = String::from(""); - blocks_html(&flow.blocks, &mut out, 0); - out -} - -pub fn export_flow(flow: &Flow, lang: &str) -> String { - let m = &flow.meta; - let mut out = String::new(); - out.push_str("\n"); - out.push_str(&format!("\n\n\n", esc(lang))); - out.push_str("\n"); - out.push_str(&format!("{}\n", esc(&m.title))); - for (k, v) in [("author", &m.author), ("description", &m.description), ("keywords", &m.keywords), ("subject", &m.subject)] { - if !v.is_empty() { - out.push_str(&format!("\n", esc(v))); - } - } - out.push_str( - "\n\n\n", - ); - blocks_html(&flow.blocks, &mut out, 0); - out.push_str("\n\n"); - out -} diff --git a/crates/formats/src/html/import.rs b/crates/formats/src/html/import.rs deleted file mode 100644 index 9e726af30b..0000000000 --- a/crates/formats/src/html/import.rs +++ /dev/null @@ -1,125 +0,0 @@ -use super::builder::{Builder, ImageLoader, parse_sheet}; -use super::lexer::{Lexer, Tok}; -use super::math_ruby::{read_math, read_ruby}; -use super::{Document, Flow, Inline, model}; - -/// Parse HTML text into the flow model. -pub fn parse(s: &str) -> Flow { - parse_with(s, &|_| None) -} - -/// [`parse`], loading pictures that aren't `data:` URIs through `images`. -pub fn parse_with(s: &str, images: ImageLoader) -> Flow { - parse_in(s, images, false) -} - -pub(super) fn parse_in(s: &str, images: ImageLoader, page: bool) -> Flow { - // HTML normalizes input newlines before tokenization, not after decoding character references. - let normalized = if s.contains('\r') { - let mut out = String::with_capacity(s.len()); - let mut chunks = s.split('\r'); - out.push_str(chunks.next().unwrap_or_default()); - for chunk in chunks { - out.push('\n'); - out.push_str(chunk.strip_prefix('\n').unwrap_or(chunk)); - } - std::borrow::Cow::Owned(out) - } else { - std::borrow::Cow::Borrowed(s) - }; - // The lexer's offsets (a style element's text is cut out by them) are into the normalized text. - let s: &str = &normalized; - let mut lx = Lexer { s, pos: 0 }; - let mut b = Builder::new(images, page); - let mut guard = 0usize; - while let Some(t) = lx.next() { - guard += 1; - if guard > 50_000_000 { - break; - } - match t { - Tok::Text(t) => b.text(&t), - Tok::Start { name, attrs, self_close } => { - b.close_head_for_start(&name); - if crate::mathml::local(&name) == "math" && !self_close { - let el = read_math(&mut lx, name, attrs); - if !b.in_head() - && let Some((linear, display)) = crate::mathml::read(&el) - { - b.start_para().inlines.push(Inline::Equation { linear, display }); - } - continue; - } - if name == "ruby" && !self_close { - let r = read_ruby(&mut lx); - if !b.in_head() { - b.push_ruby(&r.base, &r.ruby); - } - continue; - } - if matches!(name.as_str(), "script" | "style" | "noscript" | "template" | "textarea") && !self_close { - let from = lx.pos; - let to = lx.skip_past_end_tag(&name); - if name == "style" { - parse_sheet(s.get(from..to).unwrap_or(""), &mut b.sheet); - } - continue; - } - b.start(&name, &attrs, self_close); - } - Tok::End(name) => b.end(&name), - } - } - b.finish() -} - -/// Collapse white space inside ruby markup as body text does (HTML's white space only: an -/// ideographic space is text), without the space the element's indentation leaves at the edges. -pub(super) fn collapse_ws(s: &str) -> String { - let mut out = String::with_capacity(s.len()); - for c in s.chars() { - if matches!(c, ' ' | '\t' | '\n' | '\r' | '\u{000C}') { - if !out.ends_with(' ') { - out.push(' '); - } - } else { - out.push(c); - } - } - out.trim_matches(' ').to_string() -} - -/// Parse HTML into a document. -pub fn import(s: &str) -> Document { - model::begin_import(); - model::to_doc(&parse(s)) -} - -/// [`import`], loading pictures that aren't `data:` URIs through `images`. -pub fn import_with(s: &str, images: ImageLoader) -> Document { - model::begin_import(); - model::to_doc(&parse_with(s, images)) -} - -/// A web page opened as a document, as Word opens one (a file, or a document's HTML -/// `w:altChunk`): its text is Times New Roman 12 pt on single lines, headings bold and black at -/// the sizes browsers give them, and a `p` or heading whose CSS leaves a margin out (or writes it -/// invalidly) gets HTML's automatic spacing there; a margin a span states replaces the one its -/// paragraph states (both as Word's import does). [`import`] reads a pasted fragment, which takes -/// the document's own formatting instead (but its CSS still applies). -pub fn import_page(s: &str, images: ImageLoader) -> Document { - model::begin_import(); - model::to_doc(&parse_in(s, images, true)) -} - -/// [`import_page`] onto `page` (its size and margins), which tables size to: HTML gives no page -/// size, so the caller picks it (the region's paper, #1381). -pub fn import_on(s: &str, images: ImageLoader, page: &wordcraft_doc::SectionProps) -> Document { - model::begin_import(); - model::to_doc_in(&parse_in(s, images, true), page.clone()) -} - -/// The font of a web page's text when its CSS names none. -pub const PAGE_FONT: &str = "Times New Roman"; -/// Heading sizes on a web page, `h1`–`h6`, points. -pub(super) const PAGE_HEADING_SIZES: [f32; 6] = [24.0, 18.0, 13.5, 12.0, 10.0, 7.5]; diff --git a/crates/formats/src/html/lexer.rs b/crates/formats/src/html/lexer.rs deleted file mode 100644 index d37bf36f9f..0000000000 --- a/crates/formats/src/html/lexer.rs +++ /dev/null @@ -1,176 +0,0 @@ -use super::entity::{unescape, unescape_attr}; - -// --------------------------------------------------------------------------------------------- -// Tokenizer - -#[derive(Debug)] -pub(super) enum Tok { - Text(String), - Start { name: String, attrs: Vec<(String, String)>, self_close: bool }, - End(String), -} - -pub(super) struct Lexer<'a> { - pub(super) s: &'a str, - pub(super) pos: usize, -} - -impl Lexer<'_> { - fn rest(&self) -> &str { - self.s.get(self.pos..).unwrap_or("") - } - - /// Skip past the next occurrence of `pat` (ASCII case-insensitive), or to the end. - fn skip_past(&mut self, pat: &str) -> usize { - let start = self.pos; - let hay = self.rest().as_bytes(); - let p = pat.as_bytes(); - let found = hay.windows(p.len().max(1)).position(|w| w.eq_ignore_ascii_case(p)); - match found { - Some(k) => self.pos += k + p.len(), - None => self.pos = self.s.len(), - } - found.map(|k| start + k).unwrap_or(self.s.len()) - } - - /// Skip past the next matching end tag `` (ASCII case-insensitive), or to the end. - /// Skip past the end tag of raw text element `tag`; returns where that end tag starts (the - /// end of the input when there is none). - pub(super) fn skip_past_end_tag(&mut self, tag: &str) -> usize { - let prefix = format!("' || *b == b'/') { - let at = self.pos + k; - self.pos += after_tag; - self.skip_past(">"); - return at; - } - self.pos += k + 2; - } - self.s.len() - } - - pub(super) fn next(&mut self) -> Option { - let rest = self.rest(); - if rest.is_empty() { - return None; - } - if !rest.starts_with('<') { - let end = rest.find('<').unwrap_or(rest.len()); - let t = unescape(&rest[..end]); - self.pos += end; - return Some(Tok::Text(t)); - } - let b = rest.as_bytes(); - if rest.starts_with("") { - self.pos += 5; - } else if rest.starts_with("") { - self.pos += 6; - } else { - self.pos += 4; - self.skip_past("-->"); - } - return Some(Tok::Text(String::new())); - } - if rest.starts_with(""); - return Some(Tok::Text(String::new())); - } - if rest.starts_with(""); - return Some(Tok::End(name)); - } - if !b.get(1).is_some_and(u8::is_ascii_alphabetic) { - self.pos += 1; - return Some(Tok::Text("<".into())); - } - // Start tag. - let name: String = - rest[1..].chars().take_while(|c| c.is_ascii_alphanumeric() || *c == ':' || *c == '-').map(|c| c.to_ascii_lowercase()).collect(); - let mut i = 1 + name.len(); - let mut attrs = Vec::new(); - let mut self_close = false; - loop { - while b.get(i).is_some_and(u8::is_ascii_whitespace) { - i += 1; - } - match b.get(i) { - None => break, - Some(b'>') => { - i += 1; - break; - } - Some(b'/') => { - if b.get(i + 1) == Some(&b'>') { - self_close = true; - i += 2; - break; - } - i += 1; - continue; - } - _ => {} - } - let a0 = i; - while b.get(i).is_some_and(|c| !c.is_ascii_whitespace() && !matches!(c, b'=' | b'>' | b'/')) { - i += 1; - } - let an = rest.get(a0..i).unwrap_or("").to_ascii_lowercase(); - if an.is_empty() { - i += 1; - continue; - } - while b.get(i).is_some_and(u8::is_ascii_whitespace) { - i += 1; - } - let mut val = String::new(); - if b.get(i) == Some(&b'=') { - i += 1; - while b.get(i).is_some_and(u8::is_ascii_whitespace) { - i += 1; - } - match b.get(i) { - Some(&q) if q == b'"' || q == b'\'' => { - let v0 = i + 1; - let e = rest.get(v0..).and_then(|r| r.find(q as char)).map(|k| v0 + k).unwrap_or(rest.len()); - val = unescape_attr(rest.get(v0..e).unwrap_or("")); - i = e + 1; - } - _ => { - let v0 = i; - while b.get(i).is_some_and(|c| !c.is_ascii_whitespace() && *c != b'>') { - i += 1; - } - val = unescape_attr(rest.get(v0..i).unwrap_or("")); - } - } - } - if attrs.len() < 64 { - attrs.push((an, val)); - } - } - self.pos += i.min(rest.len()); - Some(Tok::Start { name, attrs, self_close }) - } -} diff --git a/crates/formats/src/html/math_ruby.rs b/crates/formats/src/html/math_ruby.rs deleted file mode 100644 index 86de8ee591..0000000000 --- a/crates/formats/src/html/math_ruby.rs +++ /dev/null @@ -1,156 +0,0 @@ -use super::builder::block_kind; -use super::lexer::{Lexer, Tok}; - -/// A `` element (#816): its tokens up to the matching end tag, as a tree. Nesting and size -/// are bounded; an unclosed element ends with the input. -pub(super) fn read_math(lx: &mut Lexer, name: String, attrs: Vec<(String, String)>) -> crate::mathml::El { - use crate::mathml::{El, Item, MAX_NODES, local}; - let mut stack: Vec = vec![El { name, attrs, children: Vec::new() }]; - let mut count = 0usize; - let close = |stack: &mut Vec| { - if stack.len() > 1 - && let Some(done) = stack.pop() - && let Some(parent) = stack.last_mut() - { - parent.children.push(Item::El(done)); - } - }; - while let Some(t) = lx.next() { - count += 1; - if count > MAX_NODES * 4 { - // Over the cap: drop the rest of this element so its tokens aren't read as HTML text. - skip_math(lx, t); - break; - } - match t { - Tok::Text(t) => { - if let Some(top) = stack.last_mut() - && !t.is_empty() - { - top.children.push(Item::Text(t)); - } - } - Tok::Start { name, attrs, self_close } => { - let el = El { name, attrs, children: Vec::new() }; - if self_close || stack.len() > wordcraft_doc::math::MAX_DEPTH { - if let Some(top) = stack.last_mut() { - top.children.push(Item::El(el)); - } - } else { - stack.push(el); - } - } - Tok::End(name) => { - let name = local(&name).to_string(); - // Close up to the matching open element; a stray end tag is ignored. - if let Some(k) = stack.iter().rposition(|e| local(&e.name) == name) { - if k == 0 { - break; - } - while stack.len() > k { - close(&mut stack); - } - } - } - } - } - while stack.len() > 1 { - close(&mut stack); - } - stack.pop().unwrap_or_default() -} - -/// Skip tokens (starting with `first`) up to and including the `` that closes the element -/// being read; nested `` elements are counted. An unclosed element ends with the input. -fn skip_math(lx: &mut Lexer, first: Tok) { - use crate::mathml::local; - let mut depth = 0usize; - let mut t = Some(first); - while let Some(tok) = t { - match tok { - Tok::Start { name, self_close: false, .. } if local(&name) == "math" => depth = depth.saturating_add(1), - Tok::End(name) if local(&name) == "math" => { - if depth == 0 { - return; - } - depth = depth.saturating_sub(1); - } - _ => {} - } - t = lx.next(); - } -} - -/// A `` element's base text and reading (#1681). -#[derive(Default)] -pub(super) struct RubyText { - pub(super) base: String, - pub(super) ruby: String, -} - -/// Which part of a `` element the text being read belongs to. -#[derive(Clone, Copy, PartialEq, Eq)] -enum RubyPart { - /// The text being annotated: an `` child, or the text outside ``/``. - Base, - /// The reading: an `` child. - Ruby, - /// `` fallback parentheses, which are not text: browsers show them only where ruby isn't - /// supported. - Fallback, -} - -/// A block-level tag ends an open `` implicitly, as it does in a browser. -fn ends_ruby(t: &Tok) -> bool { - match t { - Tok::Start { name, .. } | Tok::End(name) => block_kind(name).is_some(), - Tok::Text(_) => false, - } -} - -/// Read a `` element's content: its base text from `` children (or the text outside -/// them), its reading from `` children, and `` fallback parentheses dropped. Tolerant, -/// like the rest of the reader: tags inside are ignored, an unclosed element ends at the next -/// block-level tag (handed back to the caller) or with the input, and the tokens read are capped. -pub(super) fn read_ruby(lx: &mut Lexer) -> RubyText { - /// Most tokens one `` element is read from: a missing end tag must not scan the rest of - /// a hostile file here. - const MAX_TOKENS: usize = 4096; - let mut out = RubyText::default(); - let mut part = RubyPart::Base; - let mut nested = 0usize; - let mut count = 0usize; - while count < MAX_TOKENS { - count += 1; - let start = lx.pos; - let Some(t) = lx.next() else { break }; - if nested == 0 && ends_ruby(&t) { - // The boundary ends the element: give the token back to the main reader. - lx.pos = start; - break; - } - match t { - Tok::Text(t) => match part { - RubyPart::Base => out.base.push_str(&t), - RubyPart::Ruby => out.ruby.push_str(&t), - RubyPart::Fallback => {} - }, - // The self-closing slash is ignored on HTML elements (`` opens an `rt`), as in a - // browser's parser. - Tok::Start { name, self_close, .. } => match name.as_str() { - "rb" => part = RubyPart::Base, - "rt" => part = RubyPart::Ruby, - "rp" => part = RubyPart::Fallback, - "ruby" if !self_close => nested += 1, - _ => {} - }, - Tok::End(name) => match name.as_str() { - "rb" | "rt" | "rp" => part = RubyPart::Base, - "ruby" if nested > 0 => nested -= 1, - "ruby" => break, - _ => {} - }, - } - } - out -} diff --git a/crates/formats/src/html/tests.rs b/crates/formats/src/html/tests.rs deleted file mode 100644 index ba49057f80..0000000000 --- a/crates/formats/src/html/tests.rs +++ /dev/null @@ -1,769 +0,0 @@ -use super::*; - -#[test] -fn numeric_entity_rejects_sign() { - assert_eq!(unescape("a&#+65;b"), "a&#+65;b"); - assert_eq!(unescape("a&#x+41;b"), "a&#x+41;b"); - assert_eq!(unescape("aAAb"), "aAAb"); -} - -#[test] -fn important_declarations_win_the_cascade() { - let color = |html: &str| fmt_of(paras(&parse(html))[0]).color; - let red = Some(wordcraft_doc::Rgb(255, 0, 0)); - let blue = Some(wordcraft_doc::Rgb(0, 0, 255)); - assert_eq!(color("

    x

    "), blue); - assert_eq!(color("

    x

    "), red); - assert_eq!(color("

    x

    "), blue); - assert_eq!(color("

    x

    "), red); -} - -#[test] -fn a_declared_charset_decodes_the_page() { - // ISO 8859-2 (Central European): 0xA9 Š, 0xB9 š; named by the caller or the page. - assert_eq!(decode_as(&[0xA9, b' ', 0xB9], Some("iso-8859-2")), "Š š"); - assert!(decode_as(b"

    \xA9

    ", None).contains("

    Š

    ")); - assert_eq!(decode_as(&[0x8A], Some("windows-1252")), "Š"); - // A page that says UTF-16 without a BOM is read as UTF-8, as browsers do. - assert_eq!(decode_as("é".as_bytes(), Some("utf-16")), "é"); -} - -fn texts(f: &Flow) -> Vec { - f.blocks - .iter() - .map(|b| match b { - FBlock::Para(p) => format!("{:?}{:?}:{}", p.kind, p.list.map(|l| (l.ordered, l.level)), p.text()), - FBlock::Table(t) => format!("table{}x{}", t.rows.len(), t.cols()), - }) - .collect() -} - -fn table_style(html: &str) -> Option { - let doc = crate::model::to_doc(&parse(html)); - doc.body.iter().find_map(|b| match &**b { - wordcraft_doc::Block::Table(t) => Some(t.props.style.clone().unwrap_or_default()), - _ => None, - }) -} - -#[test] -fn borderless_tables_get_no_grid_style() { - assert_eq!(table_style("
    ab
    ").as_deref(), Some("")); - assert_eq!(table_style("
    a
    ").as_deref(), Some("")); - assert_eq!(table_style("
    a
    ").as_deref(), Some("")); - assert_eq!(table_style("
    a
    ").as_deref(), Some("TableGrid")); - assert_eq!(table_style("
    a
    ").as_deref(), Some("TableGrid")); - assert_eq!(table_style("
    a
    ").as_deref(), Some("TableGrid")); - // The last declaration decides: a `style` attribute's over the style sheet's. - assert_eq!(table_style("
    a
    ").as_deref(), Some("TableGrid")); - assert_eq!(table_style("
    a
    ").as_deref(), Some("")); -} - -#[test] -fn borderless_tables_export_without_cell_borders() { - // #438: a table without grid style and borders writes no cell borders and reopens borderless. - for (src, grid) in [ - ("
    AB
    ", false), - ("
    AB
    ", false), - ("
    AB
    ", true), - ] { - let out = export(&crate::model::to_doc(&parse(src))); - assert_eq!(out.contains("border:1px"), grid, "{src}: {out}"); - assert_eq!(table_style(&out).as_deref(), Some(if grid { "TableGrid" } else { "" }), "{src}"); - assert!(out.contains(">A") && out.contains(">B"), "{out}"); - } -} - -#[test] -fn input_newlines_preserve_preformatted_lines() { - for newline in ["\n", "\r\n", "\r"] { - let f = parse(&format!("
    {newline}  Alpha{newline}  Beta{newline}{newline}  Gamma
    ")); - assert_eq!(texts(&f), ["CodeNone: Alpha", "CodeNone: Beta", "CodeNone:", "CodeNone: Gamma"], "{newline:?}"); - let FBlock::Para(p) = &f.blocks[1] else { panic!() }; - assert!(matches!(p.inlines.last(), Some(Inline::Text(t, f)) if t == "Beta" && f.bold)); - } -} - -#[test] -fn input_newlines_are_normalized_before_character_references() { - let f = parse("

    A B\r\nC\rD

    A\r\nB\rC

    "); - assert_eq!(texts(&f), ["NormalNone:A\rB\nC\nD", "NormalNone:A B C"]); - let f = parse("

    α\r\r\nβ γ

    link"); - assert_eq!(texts(&f), ["NormalNone:α\n\nβ\rγ", "NormalNone:link"]); - let FBlock::Para(p) = &f.blocks[1] else { panic!() }; - assert!(matches!(p.inlines.first(), Some(Inline::Text(_, f)) if f.link.as_deref() == Some("https://example.test/a\nb\nc\rd"))); -} - -#[test] -fn css_preformatted_paragraphs_keep_edge_spaces() { - // #439: `white-space:pre` keeps leading and trailing spaces like `
    `; normal
    -    // paragraphs still collapse.
    -    let f = parse(
    -        "

    Alpha Beta

    \n Gamma \n

      Alpha  

    Alpha Beta

    ", - ); - assert_eq!(texts(&f), vec!["NormalNone: Alpha Beta ", "NormalNone: Gamma ", "CodeNone: Alpha ", "NormalNone:Alpha Beta"]); -} - -#[test] -fn tag_soup() { - let f = parse( - "T & U

    Head

    One bold

    Two it\n text

    • a
    • b
      1. c
    xy
    z
    q
    l1\nl2
    ", - ); - assert_eq!(f.meta.title, "T & U"); - assert_eq!( - texts(&f), - vec![ - "Heading(1)None:Head", - "NormalNone:One bold", - "NormalNone:Two it text", - "NormalSome((false, 0)):a", - "NormalSome((false, 0)):b", - "NormalSome((true, 1)):c", - "table2x3", - "QuoteNone:q", - "CodeNone:l1", - "CodeNone:l2", - ] - ); - let FBlock::Para(p) = &f.blocks[1] else { panic!() }; - assert!(matches!(&p.inlines[1], Inline::Text(t, f) if t == "bold" && f.bold)); -} - -#[test] -fn omitted_head_keeps_body_text() { - let f = parse("T\n

    Hello world

    Second

    "); - assert_eq!(f.meta.title, "T"); - assert_eq!(texts(&f), vec!["NormalNone:Hello world", "NormalNone:Second"]); - - for body in [ - "

    Hello world

    ", - "
    Hello world
    ", - "Hello world", - "Hello world", - "Hello world", - "Hello world", - ] { - let f = parse(&format!( - "T & U{body}", - )); - assert_eq!(f.meta.title, "T & U"); - assert_eq!(f.meta.author, "Ann"); - assert_eq!(texts(&f), vec!["NormalNone:Hello world"], "{body}"); - } -} - -#[test] -fn styles_and_links() { - let f = parse("

    red l
    n

    "); - let FBlock::Para(p) = &f.blocks[0] else { panic!() }; - assert_eq!(p.align, Some(Align::Center)); - assert!(matches!(&p.inlines[0], Inline::Text(t, f) if t == "red" && f.bold && f.color == Some(wordcraft_doc::Rgb(255,0,0)))); - assert!(p.inlines.iter().any(|i| matches!(i, Inline::Text(t, f) if t == "l" && f.link.as_deref() == Some("http://x")))); - assert!(p.text().ends_with("l\nn")); -} - -/// The paragraphs of every cell of the first table, row by row. -fn cell_paras(f: &Flow) -> Vec>> { - let Some(FBlock::Table(t)) = f.blocks.iter().find(|b| matches!(b, FBlock::Table(_))) else { panic!("no table") }; - t.rows - .iter() - .map(|r| r.iter().map(|c| c.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.clone()) } else { None }).collect()).collect()) - .collect() -} - -fn all_bold(p: &Para) -> bool { - !p.inlines.is_empty() && p.inlines.iter().all(|i| matches!(i, Inline::Text(_, f) if f.bold)) -} - -fn any_bold(p: &Para) -> bool { - p.inlines.iter().any(|i| matches!(i, Inline::Text(_, f) if f.bold)) -} - -#[test] -fn header_cells_are_bold_unless_their_css_says_otherwise() { - // #1381: a is bold, as in browsers and Word; `font-weight:normal` on it or inside it wins. - let f = parse( - "
    HeadPlainInner
    abc
    ", - ); - let rows = cell_paras(&f); - assert!(all_bold(&rows[0][0][0]), "{:?}", rows[0][0]); - assert!(!any_bold(&rows[0][1][0]), "{:?}", rows[0][1]); - assert!(!any_bold(&rows[0][2][0]), "{:?}", rows[0][2]); - assert!(!any_bold(&rows[1][0][0]), "td stays regular"); -} - -#[test] -fn cell_text_align_reaches_the_cell_paragraphs() { - // #1381: `text-align` on a td/th, or inherited from its tr/thead/tbody/table, aligns the - // cell's paragraphs; a paragraph's own text-align inside the cell wins. - let f = parse( - "\ - \ -
    rinherited

    own

    attr
    body
    table
    ", - ); - let rows = cell_paras(&f); - assert_eq!(rows[0][0][0].align, Some(Align::Right), "td style"); - assert_eq!(rows[0][1][0].align, Some(Align::Right), "from tr"); - assert_eq!(rows[0][2][0].align, Some(Align::Center), "own paragraph wins"); - assert_eq!(rows[0][3][0].align, Some(Align::Right), "align attribute"); - assert_eq!(rows[1][0][0].align, Some(Align::Justify), "from tbody"); - assert_eq!(rows[2][0][0].align, Some(Align::Center), "from table"); - // `align` on a table places the table; it doesn't align the text in its cells. - let f = parse("
    x
    "); - assert_eq!(cell_paras(&f)[0][0][0].align, None); -} - -#[test] -fn reporter_sample_header_bold_and_right_aligned_cells() { - // #1381: the reporter's table. - let f = parse( - "\n\n\n\n
    IndicatoreValore atteso
    Audit4
    ", - ); - let rows = cell_paras(&f); - assert!(all_bold(&rows[0][0][0]) && all_bold(&rows[0][1][0]), "{:?}", rows[0]); - assert_eq!(rows[1][1][0].align, Some(Align::Right)); - assert_eq!(rows[1][0][0].align, None); -} - -#[test] -fn entities() { - assert_eq!(unescape("a <b> AB &bogus; &"), "a AB &bogus; &"); - assert_eq!(unescape("− © &EURO; ™ ½ ¾"), "− © € ™ ½ ¾"); - assert_eq!(unescape("θ λ Δ ∇ ∑ √ ≠ ≤ ≥ ♥"), "θ λ Δ ∇ ∑ √ ≠ ≤ ≥ ♥"); -} - -/// Browsers decode numeric and legacy named references without their `;` in text, -/// but keep `&name=` and `&nameX` intact inside attribute values (query strings). -#[test] -fn references_without_semicolon() { - assert_eq!(unescape("© 2024 Acme"), "© 2024 Acme"); - assert_eq!(unescape("© 2024 Acme"), "© 2024 Acme"); - assert_eq!(unescape("© 2024 & <"), "© 2024 & <"); - assert_eq!(unescape("&bogus &# &#x &"), "&bogus &# &#x &"); - assert_eq!(unescape(" x"), "\u{00A0}x"); - let f = parse("

    l © 2024

    "); - let FBlock::Para(p) = &f.blocks[0] else { panic!() }; - assert!(p.inlines.iter().any(|i| matches!(i, Inline::Text(t, f) if t == "l" && f.link.as_deref() == Some("/q?a=1©=2®2=3&b=<")))); - assert!(p.text().ends_with("© 2024")); -} - -#[test] -fn wbr_word_break_opportunity() { - let f = parse("

    Supercalifragilistic

    "); - let Some(FBlock::Para(p)) = f.blocks.first() else { panic!("expected para") }; - assert_eq!(p.text(), "Super\u{200B}cali\u{200B}fragilistic"); -} - -#[test] -fn page_break_after_and_before() { - let f = parse("

    First

    Second

    "); - let paras: Vec<_> = f.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); - assert_eq!(paras.len(), 2); - assert!(!paras[0].page_break); - assert!(paras[1].page_break); - - let f2 = parse("

    Part 1

    Part 2

    "); - let paras2: Vec<_> = f2.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); - assert_eq!(paras2.len(), 2); - assert!(!paras2[0].page_break); - assert!(paras2[1].page_break); -} - -#[test] -fn font_size_relative_and_absolute() { - let f = parse("largersmallerhuge"); - let inls = match &f.blocks[0] { - FBlock::Para(p) => &p.inlines, - _ => panic!("expected para"), - }; - let sizes: Vec> = inls - .iter() - .map(|i| match i { - Inline::Text(_, fmt) => fmt.size, - _ => None, - }) - .collect(); - assert_eq!(sizes, vec![Some(13.5), Some(10.0), Some(18.0)]); -} - -#[test] -fn decode_with_charset_declarations() { - let iso = b"\xE9l\xE8ve"; - assert!(decode(iso).contains("élève")); - - let win = b"\x80100"; - assert!(decode(win).contains("€100")); - - let gbk = b"\xC4\xE3\xBA\xC3"; - assert!(decode(gbk).contains("你好")); - - let xml = b"hello"; - assert!(decode(xml).contains("hello")); -} - -#[test] -fn raw_text_elements_skip_only_matching_end_tag() { - let f = parse("

    Hello

    "); - assert_eq!(f.blocks.len(), 1); - let p = match &f.blocks[0] { - FBlock::Para(p) => p, - b => panic!("expected para, got {b:?}"), - }; - assert_eq!(p.text(), "Hello"); - - let f = parse("

    World

    "); - assert_eq!(f.blocks.len(), 1); - let p = match &f.blocks[0] { - FBlock::Para(p) => p, - b => panic!("expected para, got {b:?}"), - }; - assert_eq!(p.text(), "World"); -} - -#[test] -fn leading_space_survives_round_trip() { - let doc = Document::from_text(" lead"); - let html = export(&doc); - assert!(!html.contains("

    lead"), "{html}"); - let back = import(&html); - assert_eq!(back.plain_text(Default::default()).replace('\u{a0}', " "), " lead"); -} - -#[test] -fn css_escapes_read_every_kind_of_newline() { - // A backslash before CR LF, CR, LF or form feed continues the line; after hex digits, one - // of them (CR LF as one) ends the escape. - assert_eq!(css_unescape("A\\\r\nB\\\rC\\\nD\\\u{C}E"), "ABCDE"); - assert_eq!(css_unescape("\\41\r\nB \\42\u{C}C \\43 D"), "AB BC CD"); - assert_eq!(css_unescape("x\\'y\\"), "x'y"); -} - -#[test] -fn font_names_are_written_as_css_strings() { - // A font name could end the CSS string or the declaration and add CSS of its own (#3739). - let fonts = ["Arial\n;position:fixed", "Back\\slash", "O'Brien \"Sans\"", "A,B", "Times New Roman", "M+ 1p"]; - let mut p = Para::default(); - for (i, font) in fonts.iter().enumerate() { - p.push_text(&format!("t{i}"), &Fmt { font: Some((*font).into()), ..Default::default() }); - } - let html = export(&model::to_doc(&Flow { blocks: vec![FBlock::Para(p)], meta: Meta::default() })); - assert!(html.contains("font-family:'Times New Roman'") && html.contains("font-family:'M+ 1p'"), "{html}"); - let styles: Vec<&str> = html.split(" style=\"").skip(1).filter_map(|s| s.split('"').next()).filter(|s| s.contains("font-family")).collect(); - assert_eq!(styles.len(), fonts.len(), "{html}"); - for s in styles { - assert!(!s.contains([';', '\n']) && s.matches('\'').count() == 2, "{s}"); - } - // Read back, each run has its font as it was. - let back = parse(&html); - let runs: Vec<(String, Option)> = back - .blocks - .iter() - .filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }) - .flat_map(|p| p.inlines.iter()) - .filter_map(|i| if let Inline::Text(t, f) = i { Some((t.clone(), f.font.as_deref().map(String::from))) } else { None }) - .collect(); - for (i, font) in fonts.iter().enumerate() { - assert!(runs.contains(&(format!("t{i}"), Some(font.to_string()))), "{font:?} in {runs:?}"); - } -} - -/// A document that reads "I read " then a phonetic guide over `base`, then " daily.". -fn ruby_doc(base: &str, ruby: &str) -> Document { - let props = wordcraft_doc::CharProps::default(); - let mut p = wordcraft_doc::Paragraph::with_text("I read ", props.clone()); - let n = p.len(); - p.insert_object(n, crate::model::ruby_object(base.into(), ruby.into(), 20.0), &props).unwrap(); - let n = p.len(); - p.insert_text(n, " daily.", &props).unwrap(); - let mut d = Document::new(); - d.body = vec![wordcraft_doc::para_block(p)]; - d -} - -/// The first flow ruby: its base text and reading. -fn ruby_of(f: &Flow) -> Option<(String, String)> { - f.blocks.iter().find_map(|b| match b { - FBlock::Para(p) => p.inlines.iter().find_map(|i| match i { - Inline::Ruby { base, ruby, .. } => Some((base.clone(), ruby.clone())), - _ => None, - }), - _ => None, - }) -} - -/// The first document ruby: its base text and reading. -fn doc_ruby(d: &Document) -> Option<(String, String)> { - d.body.iter().find_map(|b| match &**b { - wordcraft_doc::Block::Para(p) => p.objects.iter().find_map(|o| match o { - wordcraft_doc::InlineObject::Ruby { base, ruby, .. } => Some((base.clone(), ruby.clone())), - _ => None, - }), - _ => None, - }) -} - -/// The first document ruby: base size, ruby size, raise and alignment. -fn doc_ruby_attrs(d: &Document) -> Option<(f32, f32, f32, wordcraft_doc::para::RubyAlign)> { - d.body.iter().find_map(|b| match &**b { - wordcraft_doc::Block::Para(p) => p.objects.iter().find_map(|o| match o { - wordcraft_doc::InlineObject::Ruby { base_size, size, raise, align, .. } => Some((*base_size, *size, *raise, *align)), - _ => None, - }), - _ => None, - }) -} - -#[test] -fn ruby_exports_as_ruby_markup() { - // #1681: the base text with its reading in ``, not plain base text. - let out = export(&ruby_doc("漢字", "かんじ")); - assert!(out.contains("

    I read 漢字かんじ daily.

    "), "{out}"); - // The base and the reading are escaped like any other text. - let out = export(&ruby_doc("A&B", "xA&Bx<y"), "{out}"); -} - -#[test] -fn ruby_imports_with_its_surrounding_text() { - let f = parse("

    I read 漢字かんじ daily.

    "); - assert_eq!(ruby_of(&f), Some(("漢字".into(), "かんじ".into()))); - assert_eq!(texts(&f), vec!["NormalNone:I read 漢字 daily."]); - let FBlock::Para(p) = &f.blocks[0] else { panic!() }; - assert!(matches!(&p.inlines[0], Inline::Text(t, _) if t == "I read "), "{:?}", p.inlines); - assert!(matches!(p.inlines.last(), Some(Inline::Text(t, _)) if t == " daily."), "{:?}", p.inlines); - // One document object, holding the base text and the reading. - assert_eq!(doc_ruby(&crate::model::to_doc(&f)), Some(("漢字".into(), "かんじ".into()))); -} - -#[test] -fn ruby_rb_and_rp_fallbacks_import_as_one_guide() { - // `` holds the base text and `` the fallback parentheses for browsers without - // ruby: the parentheses are not text, and the base pieces join. - let f = parse("

    漢字(かんじ)

    "); - assert_eq!(ruby_of(&f), Some(("漢字".into(), "かんじ".into()))); - assert_eq!(texts(&f), vec!["NormalNone:漢字"]); - assert_eq!(doc_ruby(&crate::model::to_doc(&f)), Some(("漢字".into(), "かんじ".into()))); -} - -#[test] -fn ruby_import_uses_the_phonetic_guide_defaults() { - // HTML states no alignment, size or raise: the Phonetic Guide's own defaults over a - // 20 pt run (half the base size, raised clear of the base text, centred)… - let d = crate::html::import("

    漢字かんじ

    "); - let Some((base_size, size, raise, align)) = doc_ruby_attrs(&d) else { panic!("no ruby") }; - assert_eq!((base_size, size, align.ooxml()), (20.0, 10.0, "center")); - assert!((raise - 18.0).abs() < 0.001, "{raise}"); - // …and the guide's 10.5 pt fallback base size when the source states none. - let d = crate::html::import("

    漢字かんじ

    "); - let Some((base_size, size, raise, _)) = doc_ruby_attrs(&d) else { panic!("no ruby") }; - assert_eq!((base_size, size), (10.5, 5.0)); - assert!((raise - 9.4).abs() < 0.001, "{raise}"); -} - -#[test] -fn ruby_survives_an_html_round_trip() { - let html = export(&ruby_doc("漢字", "かんじ")); - assert_eq!(ruby_of(&parse(&html)), Some(("漢字".into(), "かんじ".into()))); - let back = crate::html::import(&html); - assert_eq!(doc_ruby(&back), Some(("漢字".into(), "かんじ".into()))); - assert_eq!(back.plain_text(Default::default()), "I read 漢字 daily."); -} - -#[test] -fn ruby_keeps_the_run_formatting() { - let props = wordcraft_doc::CharProps { bold: Some(true), ..Default::default() }; - let mut p = wordcraft_doc::Paragraph::with_text("I read ", props.clone()); - let n = p.len(); - p.insert_object(n, crate::model::ruby_object("漢字".into(), "かんじ".into(), 20.0), &props).unwrap(); - let mut d = Document::new(); - d.body = vec![wordcraft_doc::para_block(p)]; - assert!(export(&d).contains("漢字かんじ"), "{}", export(&d)); - // Importing it back keeps the formatting for the base text and the reading. - let f = parse("

    漢字かんじ

    "); - let FBlock::Para(p) = &f.blocks[0] else { panic!() }; - assert!(matches!(&p.inlines[0], Inline::Ruby { fmt, .. } if fmt.bold), "{:?}", p.inlines); -} - -#[test] -fn malformed_ruby_keeps_its_text() { - // No reading, an empty reading, a missing base, an unclosed element and a block boundary - // closing it: none of them panics, and the text inside is kept. - let cases: [(&str, &[&str]); 7] = [ - ("

    a 漢字 b

    ", &["NormalNone:a 漢字 b"]), - ("

    a 漢字 b

    ", &["NormalNone:a 漢字 b"]), - ("

    a かんじ b

    ", &["NormalNone:a かんじ b"]), - ("

    a b

    ", &["NormalNone:a b"]), - ("", &[]), - ("

    a 漢字かんじ b

    next

    ", &["NormalNone:a 漢字 b", "NormalNone:next"]), - ("

    a 漢字かんじ

    next

    ", &["NormalNone:a 漢字", "NormalNone:next"]), - ]; - for (src, want) in cases { - assert_eq!(texts(&parse(src)), want, "{src}"); - } - // Ruby in ruby: the inner element's text joins the outer guide's. - assert_eq!(ruby_of(&parse("

    漢字字じかんじ

    ")), Some(("漢字字".into(), "じかんじ".into()))); -} - -#[test] -fn small_big_and_css_font_sizes() { - let f = parse( - "

    small big huge tiny

    ", - ); - let FBlock::Para(p) = &f.blocks[0] else { panic!() }; - let sz = |i: usize| match &p.inlines[i] { - Inline::Text(_, f) => f.size, - _ => None, - }; - assert_eq!(sz(0), Some(10.0)); - assert_eq!(sz(2), Some(14.0)); - assert_eq!(sz(4), Some(36.0)); - assert_eq!(sz(6), Some(10.0)); -} - -#[test] -fn css_important_casing_and_spacing() { - let f = parse( - "

    Styled

    ", - ); - assert_eq!(f.blocks.len(), 1); - let p = match &f.blocks[0] { - FBlock::Para(p) => p, - b => panic!("expected para, got {b:?}"), - }; - assert_eq!(p.align, Some(Align::Center)); - match &p.inlines[0] { - Inline::Text(_, fmt) => { - assert_eq!(fmt.color, Some(wordcraft_doc::props::Rgb(0xFF, 0x00, 0x00))); - assert_eq!(fmt.size, Some(16.0)); - assert!(fmt.bold); - } - i => panic!("expected text inline, got {i:?}"), - } -} - -#[test] -fn table_cell_vertical_alignment() { - let f = parse("
    topmidbot
    "); - let FBlock::Table(t) = &f.blocks[0] else { panic!() }; - assert_eq!(t.rows[0][0].valign, Some(VAlign::Top)); - assert_eq!(t.rows[0][1].valign, Some(VAlign::Center)); - assert_eq!(t.rows[0][2].valign, Some(VAlign::Bottom)); - let doc = crate::model::to_doc(&f); - let wordcraft_doc::Block::Table(dt) = &*doc.body[0] else { panic!() }; - assert_eq!(dt.rows[0].cells[0].props.valign, Some(VAlign::Top)); - assert_eq!(dt.rows[0].cells[1].props.valign, Some(VAlign::Center)); - assert_eq!(dt.rows[0].cells[2].props.valign, Some(VAlign::Bottom)); - let html = export(&doc); - assert!(html.contains("vertical-align:top")); - assert!(html.contains("vertical-align:middle")); - assert!(html.contains("vertical-align:bottom")); -} - -#[test] -fn many_formatted_leading_breaks_trim_quickly() { - // #3705: a paragraph opening with alternating bold and italic `
    ` was trimmed one - // span at a time with `Vec::remove(0)`. - let html = format!("

    {}x

    ", "

    ".repeat(100_000)); - let t = std::time::Instant::now(); - let f = parse(&html); - // About a second on a loaded machine; trimming one span at a time ran past 6 minutes. - assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed()); - let texts: Vec = f.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.text()) } else { None }).collect(); - assert_eq!(texts, ["x"]); -} - -#[test] -fn font_size_at_the_integer_limits_does_not_overflow() { - // `` offsets were added to 3 unchecked, so the extreme values panicked in debug builds. - for size in ["+2147483647", "--2147483648", "-2147483647", "+99999999999"] { - let f = parse(&format!("x")); - assert!(texts(&f).iter().any(|t| t.ends_with(":x")), "size={size}"); - } - // The offsets saturate, then clamp to the 1..=7 scale: 7 is 36 pt, 1 is 7.5 pt. - let size = |html: &str| match &parse(html).blocks[0] { - FBlock::Para(p) => p.inlines.iter().find_map(|i| match i { - Inline::Text(_, fmt) => fmt.size, - _ => None, - }), - _ => None, - }; - assert_eq!(size("x"), Some(36.0)); - assert_eq!(size("x"), Some(36.0)); - assert_eq!(size("x"), Some(7.5)); -} - -#[test] -fn empty_comments_do_not_swallow_the_rest() { - for html in ["

    ab

    c

    ", "

    ab

    c

    "] { - let doc = crate::html::import(html); - assert_eq!(doc.plain_text(Default::default()), "ab\nc", "{html}"); - } -} - -fn paras(f: &Flow) -> Vec<&Para> { - f.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect() -} -fn fmt_of(p: &Para) -> Fmt { - p.inlines.iter().find_map(|i| if let Inline::Text(_, f) = i { Some(f.clone()) } else { None }).unwrap_or_default() -} - -#[test] -fn style_sheet_rules_apply_in_cascade_order() { - let f = parse( - "\ -

    plain

    big

    inline wins

    digits

    ", - ); - let ps = paras(&f); - let (plain, big, inline, digits) = (fmt_of(ps[0]), fmt_of(ps[1]), fmt_of(ps[2]), fmt_of(ps[3])); - assert_eq!((plain.color, plain.size, plain.bold, plain.italic), (Some(wordcraft_doc::Rgb(0, 255, 0)), Some(10.0), false, false)); - assert_eq!((big.size, big.bold), (Some(20.0), true)); - assert_eq!(inline.size, Some(8.0)); - assert_eq!(ps[3].align, Some(Align::Center)); - assert_eq!(digits.size, Some(10.0)); -} - -#[test] -fn paragraph_css_states_spacing_indents_and_line_height() { - let f = parse( - "\ -

    a

    b

    c

    ", - ); - let ps = paras(&f); - assert_eq!( - ps[0].spacing, - Spacing { - before: Some(6.0), - after: Some(3.0), - left: Some(72.0), - right: Some(72.0), - first: Some(-18.0), - line: Some(LineSpacing::Multiple(1.5)), - ..Spacing::default() - } - ); - // Values that don't parse (a comma decimal, `auto`) state nothing. - assert_eq!(ps[1].spacing, Spacing { line: Some(LineSpacing::Exactly(20.0)), ..Spacing::default() }); - assert_eq!(ps[2].spacing, Spacing::default()); -} - -#[test] -fn a_page_opens_as_word_opens_one() { - let html = "\ -

    first

    Head

    kept

    own

    bare

     

    "; - let f = parse_in(html, &|_| None, true); - let ps = paras(&f); - // Times New Roman 12 pt on single lines; headings bold and black at a browser's sizes. - let first = fmt_of(ps[0]); - assert_eq!((first.font.as_deref(), first.size), (Some(PAGE_FONT), Some(12.0))); - assert_eq!(ps[0].spacing.line, Some(LineSpacing::Multiple(1.0))); - let cell = parse_in("
    cell
    ", &|_| None, true); - let FBlock::Table(t) = &cell.blocks[0] else { panic!() }; - let FBlock::Para(cp) = &t.rows[0][0].blocks[0] else { panic!() }; - assert_eq!(cp.spacing.line, Some(LineSpacing::Multiple(1.0))); - let head = fmt_of(ps[1]); - assert_eq!((head.bold, head.size, head.color), (true, Some(24.0), Some(wordcraft_doc::Rgb(0, 0, 0)))); - // A colour the heading inherits stays. - // A generic family names a face on a page; pasted text keeps the document's font. - let generic = parse_in("

    a

    b

    ", &|_| None, true); - let fonts: Vec> = paras(&generic).iter().map(|p| fmt_of(p).font.map(|f| f.to_string())).collect(); - assert_eq!(fonts, [Some("Arial".to_string()), Some(PAGE_FONT.to_string())]); - assert_eq!(fmt_of(paras(&parse("

    a

    "))[0]).font, None); - let inherited = parse_in("

    Head

    ", &|_| None, true); - assert_eq!(fmt_of(paras(&inherited)[0]).color, Some(wordcraft_doc::Rgb(0, 0, 0x80))); - // Automatic spacing where a p or heading leaves its margins out (layout drops it at the - // top of the page and of a cell). - let auto = |p: &Para| (p.spacing.before_auto, p.spacing.after_auto, p.spacing.before, p.spacing.after); - assert_eq!(auto(ps[0]), (true, true, None, None)); - assert_eq!(auto(ps[1]), (true, true, None, None)); - // A span's margin replaces the one its paragraph states; the one it leaves out stays automatic. - assert_eq!(auto(ps[2]), (true, false, None, Some(0.0))); - assert_eq!(auto(ps[3]), (true, false, None, Some(9.75))); - // Other blocks get no automatic spacing. - assert_eq!(auto(ps[4]), (false, false, None, None)); - // The document says so, and the empty paragraph's mark keeps its size. - let d = model::to_doc(&f); - let props = |i: usize| match &*d.body[i] { - wordcraft_doc::Block::Para(p) => p.clone(), - b => panic!("{b:?}"), - }; - assert_eq!((props(0).props.space_before_auto, props(0).props.space_after_auto), (Some(true), Some(true))); - assert_eq!((props(2).props.space_after_auto, props(2).props.space_after), (None, Some(0.0))); - assert_eq!(props(5).mark.size, Some(12.0)); - // `

     

    ` keeps the size of its space for its line. - assert!(ps[5].inlines.is_empty()); - assert_eq!(ps[5].mark.as_ref().and_then(|m| m.size), Some(12.0)); - // A fragment (a paste) keeps the document's own formatting. - let pasted = parse(html); - assert_eq!(fmt_of(paras(&pasted)[0]).font, None); - assert_eq!(paras(&pasted)[0].spacing, Spacing::default()); -} - -#[test] -fn cells_give_their_columns_widths() { - let f = parse( - "
    t
    1ab
    ", - ); - let FBlock::Table(t) = &f.blocks[0] else { panic!("{:?}", f.blocks) }; - assert_eq!(t.widths, [31.5, 150.0, 144.0]); - // A column without a width leaves them all to the layout. - let f = parse("
    ab
    "); - let FBlock::Table(t) = &f.blocks[0] else { panic!() }; - assert!(t.widths.is_empty()); - // A cell beside a cell spanning rows from above gives its own column its width. - let f = - parse("
    ab
    c
    de
    "); - let FBlock::Table(t) = &f.blocks[0] else { panic!() }; - assert_eq!(t.widths, [75.0, 150.0]); -} - -#[test] -fn cells_take_their_css_borders() { - use wordcraft_doc::props::{Border, BorderStyle}; - let f = parse( - "
    ab
    ", - ); - let FBlock::Table(t) = &f.blocks[0] else { panic!() }; - // The style sheet's other cell properties apply too. - assert_eq!(t.rows[0][0].valign, Some(wordcraft_doc::props::VAlign::Bottom)); - let b = t.rows[0][0].borders.expect("borders"); - assert_eq!(b.top, Some(Border::single(0.75))); - assert_eq!(b.bottom, Some(Border { style: BorderStyle::Double, width: 2.0, color: Some(wordcraft_doc::Rgb(255, 0, 0)), space: 0.0 })); - assert_eq!(b.left.map(|l| l.style), Some(BorderStyle::None)); - // A border of no width is none (OOXML has no zero-width line). - assert_eq!(b.right.map(|r| r.style), Some(BorderStyle::None)); - assert_eq!(t.rows[0][1].borders, None); -} - -#[test] -fn style_sheet_edge_cases() { - let f = parse_in( - "\ -

    a

    bare

    b

    ", - &|_| None, - true, - ); - let ps = paras(&f); - // A rule right after statement at-rules still applies. - assert_eq!(fmt_of(ps[0]).color, Some(wordcraft_doc::Rgb(0, 0, 255))); - // The body's margins aren't the paragraphs' indents. - assert_eq!((ps[1].spacing.left, ps[1].spacing.before), (None, None)); - // Bare numbers, percentages and keywords state no margin: automatic spacing stays. - assert_eq!((ps[2].spacing.before_auto, ps[2].spacing.left), (true, None)); - // The `style` attribute wins even over a sheet with more declarations than are read. - let many = (0..600).map(|i| format!("x-{i}: 1")).collect::>().join(";"); - let f = parse(&format!("

    c

    ")); - assert_eq!(fmt_of(paras(&f)[0]).color, Some(wordcraft_doc::Rgb(0, 255, 0))); -} - -#[test] -fn hostile_style_sheets_never_panic() { - // One declaration block shared by many selectors isn't copied for each. - let selectors = vec!["p"; 20_000].join(","); - let huge = format!("

    x

    ", "x: y;".repeat(200_000)); - let _ = parse_in(&huge, &|_| None, true); - let many = ".a { color: red } ".repeat(20_000); - for css in ["{", "}", "p {", "@media { p { x } ", "p { color: red", "/* open", ".{}", "p..a{}", "\u{0}.é{x:y}", many.as_str()] { - let _ = parse_in(&format!("

    x

    y"), &|_| None, true); - } -} diff --git a/crates/formats/src/latex.rs b/crates/formats/src/latex.rs index 488decce0e..9047474488 100644 --- a/crates/formats/src/latex.rs +++ b/crates/formats/src/latex.rs @@ -1,46 +1,26 @@ -//! LaTeX (`.tex`) import and export. -//! -//! **Export** writes a standalone LaTeX 2ε `article` that compiles with pdfLaTeX, XeLaTeX or -//! LuaLaTeX using only packages every TeX distribution ships (`fontenc`, `ulem`, `xcolor`, -//! `multirow`, `amsmath`, `hyperref`). Headings become unnumbered sectioning commands (Word headings -//! carry no numbers), lists `itemize`/`enumerate`, tables `tabular` with the document's column -//! widths, equations `$…$` / `\[…\]`, and character formatting the matching text commands. Pictures -//! can't live inside a single `.tex` file, so each is replaced by its alternative text. -//! -//! **Import** reads the everyday subset of LaTeX that documents are written in: the preamble's -//! `\title`/`\author`, sectioning commands, paragraphs, lists, `quote`, `center`/`flush…`, -//! `verbatim`-like environments, `tabular` (with `\multicolumn`/`\multirow`), inline and display math -//! (kept as equations), text formatting commands and declarations, colours, links, accents and -//! TeX's special characters and ligatures. It is not a TeX engine: macros aren't expanded. Unknown -//! commands are dropped while their braced arguments are kept as text, so no words are lost; layout -//! commands (`\vspace`, `\setlength`, `\usepackage`…) are dropped with their arguments. -//! -//! Like the other text formats, the parser is lenient and never panics on hostile input: group and -//! environment nesting is depth-limited and the input size is capped. +//! LaTeX (`.tex`) import and export via `craft-latex`. -use crate::model::{self, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, ListInfo, Para}; -use wordcraft_doc::{Align, Document, Rgb}; +use wordcraft_doc::{Document, SectionProps}; -mod export; -mod lex; -mod limits; -mod math; -mod parser; -mod table; -mod units; +pub use craft_latex::{escape_text, latex_to_linear, linear_to_latex}; -#[cfg(test)] -use export::Writer; -pub use export::{escape_text, export}; -#[cfg(test)] -use lex::{Tok, tokenize}; -#[cfg(test)] -use limits::MAX_NEST; -pub use limits::MAX_SOURCE; -pub use math::{latex_to_linear, linear_to_latex}; -pub use parser::{import, parse}; -#[cfg(test)] -use units::{length_pt, spec_widths}; +pub fn import(bytes: &[u8]) -> Document { + wordcraft_docx::from_shared_model(craft_latex::import(bytes)).unwrap_or_default() +} -#[cfg(test)] -mod tests; +pub fn import_on(bytes: &[u8], page: &SectionProps) -> Document { + let mut d = import(bytes); + d.last_section = page.clone(); + d +} + +pub fn parse(text: &str) -> craft_flow::Flow { + craft_latex::parse(text) +} + +pub fn export(doc: &Document) -> String { + match wordcraft_docx::to_shared_model(doc) { + Ok(shared) => craft_latex::export(&shared), + Err(_) => String::new(), + } +} diff --git a/crates/formats/src/latex/export.rs b/crates/formats/src/latex/export.rs deleted file mode 100644 index 942c0cc145..0000000000 --- a/crates/formats/src/latex/export.rs +++ /dev/null @@ -1,489 +0,0 @@ -use super::math::linear_to_latex; -use super::{Align, Cell, Document, FBlock, FTable, Fmt, Inline, Kind, Para, Rgb, model}; - -/// Text width of the exported article in points (`\linewidth` when reading widths). -pub(super) const LINE_WIDTH: f32 = 345.0; - -/// LaTeX's `\tabcolsep` (both sides of a cell), points. -pub(super) const TABCOLSEP: f32 = 6.0; - -// ============================================================================================= -// Export - -/// Export `doc` as a LaTeX source file. -pub fn export(doc: &Document) -> String { - let flow = model::from_doc(doc); - let mut w = Writer::default(); - let title_at = flow.blocks.iter().position(|b| matches!(b, FBlock::Para(p) if p.kind == Kind::Title && !p.is_empty())); - let mut title = String::new(); - if let Some(FBlock::Para(p)) = title_at.and_then(|i| flow.blocks.get(i)) { - title = w.inlines(&p.inlines); - } - w.title_at = title_at; - w.blocks(&flow.blocks); - let body = std::mem::take(&mut w.out); - - let meta = &flow.meta; - let mut o = String::new(); - o.push_str("% Written by WordCraft (https://getartcraft.com/apps/wordcraft).\n"); - o.push_str("\\documentclass{article}\n"); - o.push_str("\\usepackage[T1]{fontenc}\n"); - if w.uses.ulem { - o.push_str("\\usepackage[normalem]{ulem}\n"); - } - if w.uses.color { - o.push_str("\\usepackage[table]{xcolor}\n"); - } - if w.uses.multirow { - o.push_str("\\usepackage{multirow}\n"); - } - if w.uses.math { - o.push_str("\\usepackage{amsmath}\n"); - o.push_str("\\usepackage{amssymb}\n"); - } - o.push_str("\\usepackage{hyperref}\n"); - let pdf = [("pdftitle", &meta.title), ("pdfauthor", &meta.author), ("pdfsubject", &meta.subject), ("pdfkeywords", &meta.keywords)]; - let set: Vec = pdf.iter().filter(|(_, v)| !v.trim().is_empty()).map(|(k, v)| format!("{k}={{{}}}", escape_text(v.trim()))).collect(); - if !set.is_empty() { - o.push_str(&format!("\\hypersetup{{{}}}\n", set.join(", "))); - } - if title_at.is_some() { - o.push_str(&format!("\\title{{{title}}}\n")); - // `\maketitle` shows only the title: the document didn't show an author or a date. - o.push_str("\\author{}\n\\date{}\n"); - } - o.push_str("\n\\begin{document}\n\n"); - o.push_str(body.trim_end()); - o.push_str("\n\n\\end{document}\n"); - o -} - -#[derive(Default)] -struct Uses { - ulem: bool, - color: bool, - multirow: bool, - math: bool, -} - -#[derive(Default)] -pub(super) struct Writer { - pub(super) out: String, - uses: Uses, - /// Index of the paragraph `\maketitle` stands for (top level only). - title_at: Option, - /// Inside a table cell: no `verbatim`, paragraphs separated by `\par`. - in_cell: bool, - depth: usize, -} - -const SECTIONS: [&str; 6] = ["section", "subsection", "subsubsection", "paragraph", "subparagraph", "subparagraph"]; - -impl Writer { - fn blocks(&mut self, blocks: &[FBlock]) { - let mut lists: Vec = Vec::new(); - let mut i = 0; - while let Some(b) = blocks.get(i) { - let list = if let FBlock::Para(p) = b { p.list } else { None }; - match list { - Some(li) => { - let depth = (li.level.min(3) + 1) as usize; - while lists.len() > depth || (lists.len() == depth && lists.last() != Some(&li.ordered)) { - self.close_list(&mut lists); - } - while lists.len() < depth { - let env = if li.ordered { "enumerate" } else { "itemize" }; - self.out.push_str(&format!("{}\\begin{{{env}}}\n", " ".repeat(lists.len()))); - lists.push(li.ordered); - } - } - None => { - while !lists.is_empty() { - self.close_list(&mut lists); - } - } - } - match b { - FBlock::Para(p) if p.list.is_some() => { - let text = self.inlines(&p.inlines); - self.page_break(p); - self.out.push_str(&format!("{}\\item {}\n", " ".repeat(lists.len()), text.trim())); - } - FBlock::Para(p) if p.kind == Kind::Code => { - let mut j = i; - let mut lines = Vec::new(); - while let Some(FBlock::Para(q)) = blocks.get(j) { - if q.kind != Kind::Code || q.list.is_some() { - break; - } - lines.push(q.text()); - j += 1; - } - self.page_break(p); - self.code(&lines); - i = j.max(i + 1); - continue; - } - FBlock::Para(p) if p.kind == Kind::Quote => { - let mut j = i; - let mut paras = Vec::new(); - while let Some(FBlock::Para(q)) = blocks.get(j) { - if q.kind != Kind::Quote || q.list.is_some() { - break; - } - paras.push(self.inlines(&q.inlines)); - j += 1; - } - self.page_break(p); - let body: Vec<&str> = paras.iter().map(|s| s.trim()).filter(|s| !s.is_empty()).collect(); - let sep = if self.in_cell { "\\par\n" } else { "\n\n" }; - self.out.push_str(&format!("\\begin{{quote}}\n{}\n\\end{{quote}}\n\n", body.join(sep))); - i = j.max(i + 1); - continue; - } - FBlock::Para(p) => { - let top_title = self.depth == 0 && self.title_at == Some(i); - self.para(p, top_title); - } - FBlock::Table(t) => self.table(t), - } - i += 1; - } - while !lists.is_empty() { - self.close_list(&mut lists); - } - } - - fn close_list(&mut self, lists: &mut Vec) { - if let Some(ordered) = lists.pop() { - let env = if ordered { "enumerate" } else { "itemize" }; - self.out.push_str(&format!("{}\\end{{{env}}}\n", " ".repeat(lists.len()))); - if lists.is_empty() && !self.in_cell { - self.out.push('\n'); - } - } - } - - fn page_break(&mut self, p: &Para) { - if p.page_break && !self.in_cell { - self.out.push_str("\\clearpage\n\n"); - } - } - - fn para(&mut self, p: &Para, top_title: bool) { - self.page_break(p); - if p.kind == Kind::Rule { - self.out.push_str("\\noindent\\rule{\\linewidth}{0.4pt}\n\n"); - return; - } - if top_title { - self.out.push_str("\\maketitle\n\n"); - return; - } - let text = self.inlines(&p.inlines); - let text = text.trim(); - if text.is_empty() { - return; - } - let end = if self.in_cell { "\\par\n" } else { "\n\n" }; - match p.kind { - Kind::Heading(n) if !self.in_cell => { - let cmd = SECTIONS.get(usize::from(n.clamp(1, 6)) - 1).copied().unwrap_or("section"); - // `\\` (a line break) isn't allowed in a heading. - let text = text.replace("\\\\\n", " "); - self.out.push_str(&format!("\\{cmd}*{{{text}}}\n\n")); - } - Kind::Title => self.out.push_str(&format!("\\begin{{center}}\n{{\\LARGE {text}\\par}}\n\\end{{center}}\n\n")), - Kind::Heading(_) => self.out.push_str(&format!("\\textbf{{{text}}}{end}")), - _ => match p.align { - Some(Align::Center) => self.out.push_str(&format!("\\begin{{center}}\n{text}\n\\end{{center}}\n\n")), - Some(Align::Right) => self.out.push_str(&format!("\\begin{{flushright}}\n{text}\n\\end{{flushright}}\n\n")), - _ => { - self.out.push_str(text); - self.out.push_str(end); - } - }, - } - } - - fn code(&mut self, lines: &[String]) { - if self.in_cell { - // `verbatim` can't go in a table cell: typewriter lines instead. - let v: Vec = lines.iter().map(|l| format!("\\texttt{{{}}}", escape_text(&l.replace(' ', "\u{A0}")))).collect(); - self.out.push_str(&v.join("\\\\\n")); - self.out.push_str("\\par\n"); - return; - } - self.out.push_str("\\begin{verbatim}\n"); - for l in lines { - // The environment ends at the first `\end{verbatim}`, so keep it from appearing. The - // control characters go first: removing one afterwards could join a delimiter back up. - let l: String = l.chars().filter(|c| *c == '\t' || !c.is_control()).collect(); - let l = l.replace("\\end{verbatim}", "\\end {verbatim}"); - self.out.push_str(&l); - self.out.push('\n'); - } - self.out.push_str("\\end{verbatim}\n\n"); - } - - /// LaTeX for a run of inlines. - fn inlines(&mut self, inlines: &[Inline]) -> String { - let mut o = String::new(); - for i in inlines { - match i { - Inline::Text(t, f) => { - let s = self.span(t, f); - push_apart(&mut o, &s); - } - Inline::Image(img) => { - let alt = if img.alt.trim().is_empty() { "Picture".to_string() } else { format!("Picture: {}", img.alt.trim()) }; - o.push_str(&format!("\\textit{{[{}]}}", escape_text(&alt))); - } - Inline::Anchor(name) => o.push_str(&format!("\\hypertarget{{{}}}{{}}", escape_url(name))), - Inline::Figure(_) - | Inline::Chart(_) - | Inline::Note { .. } - | Inline::Comment { .. } - | Inline::CommentEnd(_) - | Inline::Shape { .. } - | Inline::Group { .. } - | Inline::FieldStart(_) - | Inline::FieldEnd - | Inline::AnchorEnd(_) => {} - // ASK and SET show nothing (see `model::shows_nothing`). - Inline::Field { instr, .. } if model::shows_nothing(instr) => {} - Inline::Field { result, fmt, .. } | Inline::Control { text: result, fmt, .. } => { - let s = self.span(result, fmt); - push_apart(&mut o, &s); - } - // LaTeX keeps no ruby (no package): its base text, in the run's formatting. - Inline::Ruby { base, fmt, .. } => { - let s = self.span(base, fmt); - push_apart(&mut o, &s); - } - Inline::Equation { linear, display } => { - self.uses.math = true; - let src = linear_to_latex(linear); - if *display && !self.in_cell { - o.push_str(&format!("\n\\[ {src} \\]\n")); - } else { - o.push_str(&format!("${src}$")); - } - } - } - } - o - } - - fn span(&mut self, text: &str, f: &Fmt) -> String { - let mut s = if f.code { escape_text(&text.replace(' ', "\u{A0}")) } else { escape_text(text) }; - if s.is_empty() { - return s; - } - let wrap = |s: String, cmd: &str| format!("\\{cmd}{{{s}}}"); - if f.code { - s = wrap(s, "texttt"); - } - if f.sup { - s = wrap(s, "textsuperscript"); - } else if f.sub { - s = wrap(s, "textsubscript"); - } - if f.bold { - s = wrap(s, "textbf"); - } - if f.italic { - s = wrap(s, "textit"); - } - if f.underline { - self.uses.ulem = true; - s = wrap(s, "uline"); - } - if f.strike { - self.uses.ulem = true; - s = wrap(s, "sout"); - } - if let Some(c) = f.color.filter(|c| *c != Rgb(0, 0, 0)) { - self.uses.color = true; - s = format!("\\textcolor[HTML]{{{}}}{{{s}}}", hex(c)); - } - if let Some(size) = f.size.filter(|v| v.is_finite() && *v >= 1.0 && *v <= 1638.0) { - s = format!("{{\\fontsize{{{}}}{{{}}}\\selectfont {s}}}", num(size), num(size * 1.2)); - } - if let Some(l) = f.link.as_deref().filter(|l| !l.is_empty()) { - s = match l.strip_prefix('#') { - // An internal bookmark reference, not a URL. - Some(target) => format!("\\hyperlink{{{}}}{{{s}}}", escape_url(target)), - // A script, `file:`, UNC or other non-web target becomes plain text: a `\href` - // compiles to a PDF `/URI` action the reader would otherwise follow on click. - None if wordcraft_doc::link::is_safe_href(l) => format!("\\href{{{}}}{{{s}}}", escape_url(l)), - None => s, - }; - } - s - } - - pub(super) fn table(&mut self, t: &FTable) { - let cols = t.cols().clamp(1, wordcraft_doc::table::MAX_COLS); - let widths: Vec = if t.widths.len() == cols && t.widths.iter().all(|w| w.is_finite() && *w > 1.0) { - let total: f32 = t.widths.iter().sum(); - // Scale a table wider than the page to fit. - let k = if total > LINE_WIDTH { LINE_WIDTH / total } else { 1.0 }; - t.widths.iter().map(|w| w * k).collect() - } else { - vec![LINE_WIDTH / cols as f32; cols] - }; - let col = |w: f32| format!("p{{{}pt}}", num((w - 2.0 * TABCOLSEP).max(12.0))); - let spec: String = if t.borderless { - widths.iter().map(|w| col(*w)).collect::>().join(" ") - } else { - let s: String = widths.iter().map(|w| format!("{}|", col(*w))).collect(); - format!("|{s}") - }; - let mut o = if t.borderless { - format!("\\noindent\\begin{{tabular}}{{{spec}}}\n") - } else { - format!("\\noindent\\begin{{tabular}}{{{spec}}}\n\\hline\n") - }; - for row in &t.rows { - let mut cells = Vec::new(); - let mut g = 0usize; - for c in row { - let span = (c.colspan.clamp(1, 63) as usize).min(cols.saturating_sub(g).max(1)); - let w: f32 = widths.get(g..(g + span).min(widths.len())).map(|s| s.iter().sum()).unwrap_or(72.0); - let mut body = if c.covered { String::new() } else { self.cell(c) }; - if c.rowspan > 1 && !c.covered { - self.uses.multirow = true; - body = format!("\\multirow{{{}}}{{=}}{{{body}}}", c.rowspan.min(1000)); - } - if let Some(sh) = c.shading { - self.uses.color = true; - body = format!("\\cellcolor[HTML]{{{}}}{body}", hex(sh)); - } - if span > 1 { - let col_spec = if t.borderless { - col(w) - } else { - let left = if g == 0 { "|" } else { "" }; - format!("{left}{}|", col(w)) - }; - body = format!("\\multicolumn{{{span}}}{{{col_spec}}}{{{body}}}"); - } - cells.push(body); - g += span; - } - if cells.is_empty() { - continue; - } - o.push_str(&cells.join(" & ")); - if t.borderless { - o.push_str(" \\\\\n"); - } else { - o.push_str(" \\\\\n\\hline\n"); - } - } - o.push_str("\\end{tabular}\n"); - self.out.push_str(&o); - self.out.push_str(if self.in_cell { "\\par\n" } else { "\n" }); - } - - fn cell(&mut self, c: &Cell) -> String { - let mut sub = Writer { in_cell: true, depth: self.depth + 1, ..Default::default() }; - if self.depth < model::MAX_DEPTH { - sub.blocks(&c.blocks); - } else { - sub.out = escape_text(&c.text()); - } - self.uses.ulem |= sub.uses.ulem; - self.uses.color |= sub.uses.color; - self.uses.multirow |= sub.uses.multirow; - self.uses.math |= sub.uses.math; - let s = sub.out.trim().trim_end_matches("\\par").trim_end().to_string(); - s.lines().filter(|l| !l.trim().is_empty()).collect::>().join("\n") - } -} - -fn hex(c: Rgb) -> String { - format!("{:02X}{:02X}{:02X}", c.0, c.1, c.2) -} - -/// A length without needless decimals. -fn num(v: f32) -> String { - let r = (v * 100.0).round() / 100.0; - if r.fract() == 0.0 { format!("{r:.0}") } else { format!("{r}") } -} - -/// Appends a run, writing `{}` between a `-` or `'` ending `o` and the same character starting -/// `s`, so TeX does not join them across runs into a dash or quote. -fn push_apart(o: &mut String, s: &str) { - if let Some(c) = s.chars().next() - && (c == '-' || c == '\'') - && o.ends_with(c) - { - o.push_str("{}"); - } - o.push_str(s); -} - -/// Text with LaTeX's special characters escaped. -pub fn escape_text(s: &str) -> String { - let mut o = String::with_capacity(s.len() + 8); - let mut prev = '\0'; - let mut line_has_text = false; - for c in s.chars() { - match c { - '\\' => o.push_str("\\textbackslash{}"), - '{' => o.push_str("\\{"), - '}' => o.push_str("\\}"), - '$' | '&' | '#' | '%' | '_' => { - o.push('\\'); - o.push(c); - } - '~' => o.push_str("\\textasciitilde{}"), - '^' => o.push_str("\\textasciicircum{}"), - '<' => o.push_str("\\textless{}"), - '>' => o.push_str("\\textgreater{}"), - '|' => o.push_str("\\textbar{}"), - '\u{A0}' => o.push('~'), - // TeX collapses a run of spaces into one; a control space keeps each further one. - ' ' if prev == ' ' => o.push_str("\\ "), - // An opening bracket here would become an optional item or line-break argument. - '[' if !line_has_text => o.push_str("{[}"), - // Keep TeX from joining `--`, ``` `` ``` and `''` into dashes and quotes. - '-' if prev == '-' => o.push_str("{}-"), - '`' => o.push_str("\\textasciigrave{}"), - '\'' if prev == '\'' => o.push_str("{}'"), - '\t' => o.push_str("\\quad{}"), - '\n' | '\u{000B}' => { - // A line break; LaTeX refuses one with nothing before it on the line. - o.push_str(if line_has_text { "\\\\\n" } else { "\\mbox{}\\\\\n" }); - line_has_text = false; - prev = c; - continue; - } - c if c.is_control() => {} - c => o.push(c), - } - if !c.is_whitespace() { - line_has_text = true; - } - prev = c; - } - o -} - -/// A URL or anchor name for `\href`/`\hyperlink`: `%`, `#`, `\`, braces escaped. -fn escape_url(s: &str) -> String { - let mut o = String::new(); - for c in s.chars().filter(|c| !c.is_control()) { - // Never `^^`: TeX reads `^^5c` as a backslash, which would start a command. - if c == '^' && o.ends_with('^') { - continue; - } - if matches!(c, '%' | '#' | '\\' | '{' | '}') { - o.push('\\'); - } - o.push(c); - } - o -} diff --git a/crates/formats/src/latex/lex.rs b/crates/formats/src/latex/lex.rs deleted file mode 100644 index 711adb5516..0000000000 --- a/crates/formats/src/latex/lex.rs +++ /dev/null @@ -1,470 +0,0 @@ -#[derive(Clone, Debug, PartialEq)] -pub(super) enum Tok { - /// `\name` (a starred form keeps its `*`) or a control symbol like `\&`. - Cmd(String), - Open, - Close, - /// Text (spaces collapsed to one). - Text(String), - /// A blank line. - Par, - Amp, - /// `~`: a non-breaking space. - Tie, - /// Math source, and whether it is displayed. - Math(String, bool), - /// The content of a verbatim-like environment. - Verbatim(String), - /// `\verb|…|`. - Verb(String), - /// `\begin{name}` and `\end{name}`. - Begin(String), - End(String), -} - -const VERBATIM_ENVS: &[&str] = &["verbatim", "verbatim*", "Verbatim", "lstlisting", "minted", "alltt", "comment"]; - -const MATH_ENVS: &[&str] = &[ - "equation", - "equation*", - "align", - "align*", - "alignat", - "alignat*", - "gather", - "gather*", - "multline", - "multline*", - "flalign", - "flalign*", - "displaymath", - "math", - "eqnarray", - "eqnarray*", -]; - -struct Lexer<'a> { - s: &'a str, - i: usize, - out: Vec, -} - -impl Lexer<'_> { - fn peek(&self) -> Option { - self.s.get(self.i..).and_then(|r| r.chars().next()) - } - fn rest(&self) -> &str { - self.s.get(self.i..).unwrap_or("") - } - fn bump(&mut self) -> Option { - let c = self.peek()?; - self.i += c.len_utf8(); - Some(c) - } - fn text(&mut self, t: &str) { - if let Some(Tok::Text(s)) = self.out.last_mut() { - s.push_str(t); - } else { - self.out.push(Tok::Text(t.to_string())); - } - } - /// Skip the rest of a line (a comment), the line end and the next line's leading blanks. - fn comment(&mut self) { - while let Some(c) = self.bump() { - if c == '\n' { - break; - } - } - while matches!(self.peek(), Some(' ' | '\t')) { - self.bump(); - } - } - /// Read until `end` (not included; consumed) and return what came before. - fn until(&mut self, end: &str) -> String { - match self.rest().find(end) { - Some(at) => { - let s = self.rest().get(..at).unwrap_or("").to_string(); - self.i += at + end.len(); - s - } - None => { - let s = self.rest().to_string(); - self.i = self.s.len(); - s - } - } - } - /// After `\begin` / `\end`: the `{name}`, if well formed. - fn env_name(&mut self) -> Option { - let save = self.i; - while matches!(self.peek(), Some(' ' | '\t')) { - self.bump(); - } - if self.peek() != Some('{') { - self.i = save; - return None; - } - self.bump(); - // Only the next 65 bytes are searched: looking through the rest of the file for a `}` - // made a run of unclosed `\\begin{` quadratic. - let Some(end) = self.rest().bytes().take(65).position(|b| b == b'}') else { - // Unclosed or overlong: leave the `{` to be read as ordinary input. - self.i = save; - return None; - }; - let r = self.rest(); - let name = r.get(..end).unwrap_or("").trim().to_string(); - self.i += end + 1; - Some(name) - } - - fn run(mut self) -> Vec { - while let Some(c) = self.peek() { - match c { - '\\' => { - self.bump(); - let r = self.rest(); - let n = r.find(|c: char| !c.is_ascii_alphabetic()).unwrap_or(r.len()); - if n == 0 { - let Some(sym) = self.bump() else { break }; - match sym { - '(' => { - let m = self.until("\\)"); - self.out.push(Tok::Math(m, false)); - } - '[' => { - let m = self.until("\\]"); - self.out.push(Tok::Math(m, true)); - } - _ => self.out.push(Tok::Cmd(sym.to_string())), - } - continue; - } - let mut name = r.get(..n).unwrap_or("").to_string(); - self.i += n; - if self.peek() == Some('*') { - self.bump(); - name.push('*'); - } - match name.as_str() { - "begin" => match self.env_name() { - Some(env) if VERBATIM_ENVS.contains(&env.as_str()) => { - // `minted{lang}` / `lstlisting[options]`: skip to the line end. - let _options = self.until("\n"); - let v = self.until(&format!("\\end{{{env}}}")); - if env != "comment" { - self.out.push(Tok::Verbatim(v)); - } - } - Some(env) if MATH_ENVS.contains(&env.as_str()) => { - let m = self.until(&format!("\\end{{{env}}}")); - self.out.push(Tok::Math(m, env != "math")); - } - Some(env) => self.out.push(Tok::Begin(env)), - None => self.out.push(Tok::Cmd(name)), - }, - "end" => match self.env_name() { - Some(env) => self.out.push(Tok::End(env)), - None => self.out.push(Tok::Cmd(name)), - }, - "verb" | "verb*" => match self.bump() { - Some(delim) if !delim.is_alphabetic() && !delim.is_whitespace() => { - let v = self.until(&delim.to_string()); - self.out.push(Tok::Verb(v)); - } - _ => {} - }, - _ => { - self.out.push(Tok::Cmd(name)); - // A control word swallows the spaces (and one line end) after it. - let mut newline = false; - while let Some(c) = self.peek() { - if c == ' ' || c == '\t' || (c == '\n' && !newline) { - newline |= c == '\n'; - self.bump(); - } else { - break; - } - } - } - } - } - '%' => self.comment(), - '{' => { - self.bump(); - self.out.push(Tok::Open); - } - '}' => { - self.bump(); - self.out.push(Tok::Close); - } - '&' => { - self.bump(); - self.out.push(Tok::Amp); - } - '~' => { - self.bump(); - self.out.push(Tok::Tie); - } - '$' => { - self.bump(); - if self.peek() == Some('$') { - self.bump(); - let m = self.until("$$"); - self.out.push(Tok::Math(m, true)); - } else { - let m = self.until("$"); - self.out.push(Tok::Math(m, false)); - } - } - '\n' | '\r' | ' ' | '\t' => { - // Whitespace: a blank line ends the paragraph, anything else is one space. - let mut lines = 0; - while let Some(c) = self.peek() { - match c { - '\n' => lines += 1, - ' ' | '\t' | '\r' => {} - _ => break, - } - self.bump(); - } - if lines >= 2 { - self.out.push(Tok::Par); - } else { - self.text(" "); - } - } - _ => { - let r = self.rest(); - let n = r.find(['\\', '%', '{', '}', '&', '~', '$', '\n', '\r', ' ', '\t']).unwrap_or(r.len()).max(c.len_utf8()); - let t = r.get(..n).unwrap_or("").to_string(); - self.i += n; - self.text(&t); - } - } - } - self.out - } -} - -pub(super) fn tokenize(src: &str) -> Vec { - Lexer { s: src, i: 0, out: Vec::new() }.run() -} - -/// TeX's text ligatures: dashes and quotes. -pub(super) fn ligatures(s: &str) -> String { - s.replace("---", "—").replace("--", "–").replace("``", "“").replace("''", "”").replace('`', "‘").replace("<<", "«").replace(">>", "»") -} - -/// Character for a symbol command, if it is one. -pub(super) fn text_symbol(name: &str) -> Option<&'static str> { - Some(match name { - "&" => "&", - "%" => "%", - "$" => "$", - "#" => "#", - "_" => "_", - "{" => "{", - "}" => "}", - " " => " ", - "," | ";" | ":" | "enspace" | "thinspace" => " ", - "quad" => "\u{2003}", - "qquad" => "\u{2003}\u{2003}", - "-" | "/" | "@" | "!" | "noindent" | "indent" | "relax" | "protect" | "selectfont" | "nobreak" | "sloppy" | "fussy" | "centering" - | "raggedright" | "raggedleft" | "normalfont" | "item" => "", - "textbackslash" | "backslash" => "\\", - "textasciitilde" => "~", - "textasciicircum" => "^", - "textless" => "<", - "textgreater" => ">", - "textbar" => "|", - "textbraceleft" => "{", - "textbraceright" => "}", - "textunderscore" => "_", - "textasciigrave" => "`", - "textquotesingle" => "'", - "textquotedbl" => "\"", - "ldots" | "dots" | "textellipsis" => "…", - "textendash" => "–", - "textemdash" => "—", - "textbullet" => "•", - "textperiodcentered" => "·", - "textquoteleft" => "‘", - "textquoteright" => "’", - "textquotedblleft" => "“", - "textquotedblright" => "”", - "guillemotleft" | "guillemetleft" => "«", - "guillemotright" | "guillemetright" => "»", - "copyright" | "textcopyright" => "©", - "textregistered" => "®", - "texttrademark" => "™", - "S" | "textsection" => "§", - "P" | "textparagraph" => "¶", - "dag" | "textdagger" => "†", - "ddag" | "textdaggerdbl" => "‡", - "pounds" | "textsterling" => "£", - "euro" | "texteuro" => "€", - "textyen" => "¥", - "textcent" => "¢", - "textdegree" | "degree" => "°", - "textmu" => "µ", - "texttimes" => "×", - "textdiv" => "÷", - "textpm" => "±", - "textonehalf" => "½", - "textonequarter" => "¼", - "textthreequarters" => "¾", - "textexclamdown" => "¡", - "textquestiondown" => "¿", - "ss" => "ß", - "SS" => "SS", - "ae" => "æ", - "AE" => "Æ", - "oe" => "œ", - "OE" => "Œ", - "o" => "ø", - "O" => "Ø", - "aa" => "å", - "AA" => "Å", - "l" => "ł", - "L" => "Ł", - "i" => "ı", - "j" => "ȷ", - "LaTeX" => "LaTeX", - "LaTeXe" => "LaTeX2ε", - "TeX" => "TeX", - "XeTeX" => "XeTeX", - "LuaTeX" => "LuaTeX", - "BibTeX" => "BibTeX", - _ => return None, - }) -} - -/// Combining mark for an accent command. -pub(super) fn accent_mark(name: &str) -> Option { - Some(match name { - "'" => '\u{301}', - "`" => '\u{300}', - "^" => '\u{302}', - "\"" => '\u{308}', - "~" => '\u{303}', - "=" => '\u{304}', - "." => '\u{307}', - "u" => '\u{306}', - "v" => '\u{30C}', - "H" => '\u{30B}', - "c" => '\u{327}', - "k" => '\u{328}', - "r" => '\u{30A}', - "d" => '\u{323}', - "b" => '\u{331}', - _ => return None, - }) -} - -/// Precomposed letters for the common accents (base + combining mark otherwise). -pub(super) fn compose(base: char, mark: char) -> String { - const TABLE: &[(char, char, char)] = &[ - ('a', '\u{301}', 'á'), - ('e', '\u{301}', 'é'), - ('i', '\u{301}', 'í'), - ('ı', '\u{301}', 'í'), - ('o', '\u{301}', 'ó'), - ('u', '\u{301}', 'ú'), - ('y', '\u{301}', 'ý'), - ('c', '\u{301}', 'ć'), - ('n', '\u{301}', 'ń'), - ('s', '\u{301}', 'ś'), - ('z', '\u{301}', 'ź'), - ('A', '\u{301}', 'Á'), - ('E', '\u{301}', 'É'), - ('I', '\u{301}', 'Í'), - ('O', '\u{301}', 'Ó'), - ('U', '\u{301}', 'Ú'), - ('Y', '\u{301}', 'Ý'), - ('a', '\u{300}', 'à'), - ('e', '\u{300}', 'è'), - ('i', '\u{300}', 'ì'), - ('ı', '\u{300}', 'ì'), - ('o', '\u{300}', 'ò'), - ('u', '\u{300}', 'ù'), - ('A', '\u{300}', 'À'), - ('E', '\u{300}', 'È'), - ('I', '\u{300}', 'Ì'), - ('O', '\u{300}', 'Ò'), - ('U', '\u{300}', 'Ù'), - ('a', '\u{302}', 'â'), - ('e', '\u{302}', 'ê'), - ('i', '\u{302}', 'î'), - ('ı', '\u{302}', 'î'), - ('o', '\u{302}', 'ô'), - ('u', '\u{302}', 'û'), - ('A', '\u{302}', 'Â'), - ('E', '\u{302}', 'Ê'), - ('I', '\u{302}', 'Î'), - ('O', '\u{302}', 'Ô'), - ('U', '\u{302}', 'Û'), - ('a', '\u{308}', 'ä'), - ('e', '\u{308}', 'ë'), - ('i', '\u{308}', 'ï'), - ('ı', '\u{308}', 'ï'), - ('o', '\u{308}', 'ö'), - ('u', '\u{308}', 'ü'), - ('y', '\u{308}', 'ÿ'), - ('A', '\u{308}', 'Ä'), - ('E', '\u{308}', 'Ë'), - ('I', '\u{308}', 'Ï'), - ('O', '\u{308}', 'Ö'), - ('U', '\u{308}', 'Ü'), - ('a', '\u{303}', 'ã'), - ('n', '\u{303}', 'ñ'), - ('o', '\u{303}', 'õ'), - ('A', '\u{303}', 'Ã'), - ('N', '\u{303}', 'Ñ'), - ('O', '\u{303}', 'Õ'), - ('c', '\u{327}', 'ç'), - ('C', '\u{327}', 'Ç'), - ('s', '\u{327}', 'ş'), - ('S', '\u{327}', 'Ş'), - ('a', '\u{30A}', 'å'), - ('A', '\u{30A}', 'Å'), - ('u', '\u{30A}', 'ů'), - ('c', '\u{30C}', 'č'), - ('d', '\u{30C}', 'ď'), - ('e', '\u{30C}', 'ě'), - ('n', '\u{30C}', 'ň'), - ('r', '\u{30C}', 'ř'), - ('s', '\u{30C}', 'š'), - ('t', '\u{30C}', 'ť'), - ('z', '\u{30C}', 'ž'), - ('C', '\u{30C}', 'Č'), - ('D', '\u{30C}', 'Ď'), - ('E', '\u{30C}', 'Ě'), - ('N', '\u{30C}', 'Ň'), - ('R', '\u{30C}', 'Ř'), - ('S', '\u{30C}', 'Š'), - ('T', '\u{30C}', 'Ť'), - ('Z', '\u{30C}', 'Ž'), - ('g', '\u{306}', 'ğ'), - ('G', '\u{306}', 'Ğ'), - ('o', '\u{30B}', 'ő'), - ('u', '\u{30B}', 'ű'), - ('O', '\u{30B}', 'Ő'), - ('U', '\u{30B}', 'Ű'), - ('a', '\u{328}', 'ą'), - ('e', '\u{328}', 'ę'), - ('A', '\u{328}', 'Ą'), - ('E', '\u{328}', 'Ę'), - ('z', '\u{307}', 'ż'), - ('Z', '\u{307}', 'Ż'), - ('a', '\u{304}', 'ā'), - ('e', '\u{304}', 'ē'), - ('i', '\u{304}', 'ī'), - ('o', '\u{304}', 'ō'), - ('u', '\u{304}', 'ū'), - ]; - match TABLE.iter().find(|(b, m, _)| *b == base && *m == mark) { - Some((_, _, c)) => c.to_string(), - None => format!("{base}{mark}"), - } -} diff --git a/crates/formats/src/latex/limits.rs b/crates/formats/src/latex/limits.rs deleted file mode 100644 index 3c5a4fcdd0..0000000000 --- a/crates/formats/src/latex/limits.rs +++ /dev/null @@ -1,11 +0,0 @@ -/// Largest `.tex` source imported (bytes); longer input is cut off. -pub const MAX_SOURCE: usize = 32 << 20; - -/// Nesting limit for groups, environments and arguments (hostile input). -pub(super) const MAX_NEST: usize = 32; - -/// Longest `tabular` column spec read (characters); the rest is ignored. -pub(super) const MAX_SPEC: usize = 4096; - -/// Most bookmarks kept in a row with no text between them. -pub(super) const MAX_ANCHOR_RUN: usize = 64; diff --git a/crates/formats/src/latex/math.rs b/crates/formats/src/latex/math.rs deleted file mode 100644 index 1ef0e6b15a..0000000000 --- a/crates/formats/src/latex/math.rs +++ /dev/null @@ -1,567 +0,0 @@ -use super::limits::MAX_NEST; - -// --------------------------------------------------------------------------------------------- -// Equations: WordCraft keeps them in the linear format (`x=(-b±√(b^2-4ac))/2a`). - -/// Symbols with a LaTeX command: (character, command without the backslash). -const MATH_SYMBOLS: &[(char, &str)] = &[ - ('α', "alpha"), - ('β', "beta"), - ('γ', "gamma"), - ('δ', "delta"), - ('ϵ', "epsilon"), - ('ε', "varepsilon"), - ('ζ', "zeta"), - ('η', "eta"), - ('θ', "theta"), - ('ϑ', "vartheta"), - ('ι', "iota"), - ('κ', "kappa"), - ('λ', "lambda"), - ('μ', "mu"), - ('ν', "nu"), - ('ξ', "xi"), - ('π', "pi"), - ('ϖ', "varpi"), - ('ρ', "rho"), - ('ϱ', "varrho"), - ('σ', "sigma"), - ('ς', "varsigma"), - ('τ', "tau"), - ('υ', "upsilon"), - ('ϕ', "phi"), - ('φ', "varphi"), - ('χ', "chi"), - ('ψ', "psi"), - ('ω', "omega"), - ('Γ', "Gamma"), - ('Δ', "Delta"), - ('Θ', "Theta"), - ('Λ', "Lambda"), - ('Ξ', "Xi"), - ('Π', "Pi"), - ('Σ', "Sigma"), - ('Υ', "Upsilon"), - ('Φ', "Phi"), - ('Ψ', "Psi"), - ('Ω', "Omega"), - ('±', "pm"), - ('∓', "mp"), - ('×', "times"), - ('÷', "div"), - ('⋅', "cdot"), - ('·', "cdot"), - ('∘', "circ"), - ('∗', "ast"), - ('≤', "leq"), - ('≥', "geq"), - ('≠', "neq"), - ('≈', "approx"), - ('≡', "equiv"), - ('∼', "sim"), - ('≃', "simeq"), - ('≅', "cong"), - ('∝', "propto"), - ('≪', "ll"), - ('≫', "gg"), - ('∈', "in"), - ('∉', "notin"), - ('∋', "ni"), - ('⊂', "subset"), - ('⊃', "supset"), - ('⊆', "subseteq"), - ('⊇', "supseteq"), - ('∪', "cup"), - ('∩', "cap"), - ('∖', "setminus"), - ('∅', "emptyset"), - ('∀', "forall"), - ('∃', "exists"), - ('¬', "neg"), - ('∧', "wedge"), - ('∨', "vee"), - ('⊕', "oplus"), - ('⊗', "otimes"), - ('→', "to"), - ('←', "leftarrow"), - ('↔', "leftrightarrow"), - ('⇒', "Rightarrow"), - ('⇐', "Leftarrow"), - ('⇔', "Leftrightarrow"), - ('↦', "mapsto"), - ('↑', "uparrow"), - ('↓', "downarrow"), - ('∑', "sum"), - ('∏', "prod"), - ('∐', "coprod"), - ('∫', "int"), - ('∬', "iint"), - ('∭', "iiint"), - ('∮', "oint"), - ('⋃', "bigcup"), - ('⋂', "bigcap"), - ('∞', "infty"), - ('∂', "partial"), - ('∇', "nabla"), - ('ℏ', "hbar"), - ('ℓ', "ell"), - ('ℵ', "aleph"), - ('ℜ', "Re"), - ('ℑ', "Im"), - ('∠', "angle"), - ('⊥', "perp"), - ('∥', "parallel"), - ('′', "prime"), - ('…', "ldots"), - ('⋯', "cdots"), - ('⋮', "vdots"), - ('⋱', "ddots"), - ('⟨', "langle"), - ('⟩', "rangle"), - ('⌊', "lfloor"), - ('⌋', "rfloor"), - ('⌈', "lceil"), - ('⌉', "rceil"), - ('°', "circ"), - ('•', "bullet"), - ('⊤', "top"), -]; - -/// Extra input spellings mapped to the same symbols. -const MATH_ALIASES: &[(&str, char)] = &[ - ("le", '≤'), - ("ge", '≥'), - ("ne", '≠'), - ("rightarrow", '→'), - ("gets", '←'), - ("land", '∧'), - ("lor", '∨'), - ("lnot", '¬'), - ("dots", '…'), - ("dotsc", '…'), - ("dotsb", '⋯'), - ("varnothing", '∅'), - ("implies", '⇒'), - ("iff", '⇔'), - ("degree", '°'), - ("lbrace", '{'), - ("rbrace", '}'), - ("vert", '|'), - ("mid", '|'), - ("Vert", '‖'), - ("lvert", '|'), - ("rvert", '|'), - ("bot", '⊥'), - ("hslash", 'ℏ'), - ("colon", ':'), -]; - -/// Operator names written upright in both formats. -const MATH_FUNCTIONS: &[&str] = &[ - "sin", "cos", "tan", "cot", "sec", "csc", "arcsin", "arccos", "arctan", "sinh", "cosh", "tanh", "coth", "log", "ln", "lg", "exp", "lim", - "liminf", "limsup", "min", "max", "sup", "inf", "det", "dim", "gcd", "deg", "arg", "ker", "Pr", "hom", -]; - -/// Control words an exported equation may carry besides the symbols and functions above (font -/// switches, delimiters, spacing). -const MATH_COMMANDS: &[&str] = &[ - "mathbb", - "mathbf", - "mathcal", - "mathfrak", - "mathit", - "mathrm", - "mathsf", - "mathtt", - "boldsymbol", - "left", - "right", - "quad", - "qquad", - "displaystyle", - "textstyle", - "limits", - "nolimits", - "not", -]; - -/// May `\\name` from an equation's text be written to the exported file as a command? Only the -/// names known here. An equation's text comes from the document (a file someone else may have -/// made), and `\\input`, `\\write` or `\\def` in it would act on the machine of whoever compiles -/// the export. -fn known_math_command(name: &str) -> bool { - math_symbol(name).is_some() || MATH_FUNCTIONS.contains(&name) || MATH_COMMANDS.contains(&name) -} - -fn math_symbol(name: &str) -> Option { - MATH_SYMBOLS.iter().find(|(_, n)| *n == name).map(|(c, _)| *c).or_else(|| MATH_ALIASES.iter().find(|(n, _)| *n == name).map(|(_, c)| *c)) -} - -/// For every `l` in `chars`, the index of the `r` that closes it, found in one stack pass. -/// Looking groups up here keeps scanning linear: rescanning to the end of the input for every -/// unmatched `(` made long equations quadratic. -pub(super) fn closers(chars: &[char], l: char, r: char) -> Vec> { - let mut out = vec![None; chars.len()]; - let mut open = Vec::new(); - for (i, c) in chars.iter().enumerate() { - if *c == l { - open.push(i); - } else if *c == r - && let Some(o) = open.pop() - && let Some(slot) = out.get_mut(o) - { - *slot = Some(i); - } - } - out -} - -/// The balanced group opening at `open` (see [`closers`]): its content and the index after it. -pub(super) fn balanced(chars: &[char], closers: &[Option], open: usize) -> Option<(String, usize)> { - let close = (*closers.get(open)?)?; - Some((chars.get(open + 1..close).unwrap_or_default().iter().collect(), close + 1)) -} - -/// A linear-format equation as LaTeX math. -pub fn linear_to_latex(linear: &str) -> String { - linear_to_latex_at(linear, 0) -} - -fn linear_to_latex_at(linear: &str, depth: usize) -> String { - let chars: Vec = linear.chars().collect(); - let parens = closers(&chars, '(', ')'); - let mut o = String::new(); - let mut i = 0; - let sub = |s: &str| if depth < MAX_NEST { linear_to_latex_at(s, depth + 1) } else { escape_math_text(s) }; - while let Some(&c) = chars.get(i) { - // (a)/(b) → \frac{a}{b} - // (The `/` is looked for before the group is copied out: copying every group of a deeply - // nested equation only to find no `/` after it was quadratic.) - if c == '(' - && let Some(Some(close)) = parens.get(i) - && chars.get(close + 1) == Some(&'/') - && let Some((num, after)) = balanced(&chars, &parens, i) - && let Some((den, end)) = balanced(&chars, &parens, after + 1) - { - o.push_str(&format!("\\frac{{{}}}{{{}}}", sub(&num), sub(&den))); - i = end; - continue; - } - match c { - '^' | '_' => { - // Never `^^`: TeX reads `^^5c` as a backslash, which would start a command. - if c == '^' && o.ends_with('^') { - o.push_str("{}"); - } - o.push(c); - match balanced(&chars, &parens, i + 1) { - Some((inner, end)) => { - o.push_str(&format!("{{{}}}", sub(&inner))); - i = end; - } - None => i += 1, - } - continue; - } - '√' => { - match balanced(&chars, &parens, i + 1) { - Some((inner, end)) => { - match inner.split_once('&') { - Some((n, x)) => o.push_str(&format!("\\sqrt[{}]{{{}}}", sub(n), sub(x))), - None => o.push_str(&format!("\\sqrt{{{}}}", sub(&inner))), - } - i = end; - } - None => { - o.push_str("\\surd "); - i += 1; - } - } - continue; - } - '\\' => { - // Linear format control words (`\alpha`) are LaTeX's too. - let name: String = chars.iter().skip(i + 1).take_while(|c| c.is_ascii_alphabetic()).collect(); - if name.is_empty() { - o.push_str("\\backslash "); - i += 1; - } else if !known_math_command(&name) { - // Not a command we know: written as the text it is. - o.push_str("\\backslash "); - o.push_str(&name); - i += 1 + name.len(); - } else { - o.push('\\'); - o.push_str(&name); - i += 1 + name.len(); - if chars.get(i).is_some_and(char::is_ascii_alphabetic) { - o.push(' '); - } - } - continue; - } - '#'..='&' => { - o.push('\\'); - o.push(c); - } - '{' => o.push_str("\\{"), - '}' => o.push_str("\\}"), - '~' => o.push_str("\\sim "), - c => match MATH_SYMBOLS.iter().find(|(s, _)| *s == c) { - Some((_, name)) => { - o.push('\\'); - o.push_str(name); - // A control word needs a space only before a letter. - if chars.get(i + 1).is_some_and(char::is_ascii_alphabetic) { - o.push(' '); - } - } - None if c.is_control() => {} - None => o.push(c), - }, - } - i += 1; - } - o.trim().to_string() -} - -fn escape_math_text(s: &str) -> String { - let mut o = String::new(); - for c in s.chars().filter(|c| !matches!(c, '{' | '}' | '\\' | '$' | '%' | '#' | '&') && !c.is_control()) { - // Never `^^` (see `linear_to_latex_at`). - if c == '^' && o.ends_with('^') { - continue; - } - o.push(c); - } - o -} - -/// LaTeX math as a linear-format equation. -pub fn latex_to_linear(src: &str) -> String { - let toks = math_tokens(src); - let mut pos = 0; - let s = math_seq(&toks, &mut pos, 0, false); - s.split_whitespace().collect::>().join(" ") -} - -#[derive(Clone, Debug, PartialEq)] -enum MTok { - Cmd(String), - Open, - Close, - OptOpen, - OptClose, - Char(char), -} - -fn math_tokens(src: &str) -> Vec { - let chars: Vec = src.chars().collect(); - let mut out = Vec::new(); - let mut i = 0; - while let Some(&c) = chars.get(i) { - match c { - '\\' => { - let name: String = chars.iter().skip(i + 1).take_while(|c| c.is_ascii_alphabetic()).collect(); - if name.is_empty() { - if let Some(&n) = chars.get(i + 1) { - out.push(MTok::Cmd(n.to_string())); - } - i += 2; - } else { - i += 1 + name.len(); - out.push(MTok::Cmd(name)); - } - } - '{' => { - out.push(MTok::Open); - i += 1; - } - '}' => { - out.push(MTok::Close); - i += 1; - } - '[' => { - out.push(MTok::OptOpen); - i += 1; - } - ']' => { - out.push(MTok::OptClose); - i += 1; - } - '%' => { - // A comment, to the end of the line. - while chars.get(i).is_some_and(|c| *c != '\n') { - i += 1; - } - } - c => { - out.push(MTok::Char(if c.is_whitespace() { ' ' } else { c })); - i += 1; - } - } - } - out -} - -/// One argument: a `{…}` group or a single token. -fn math_arg(t: &[MTok], pos: &mut usize, depth: usize) -> String { - while t.get(*pos) == Some(&MTok::Char(' ')) { - *pos += 1; - } - match t.get(*pos) { - Some(MTok::Open) => { - *pos += 1; - let s = math_seq(t, pos, depth + 1, false); - if t.get(*pos) == Some(&MTok::Close) { - *pos += 1; - } - s - } - Some(MTok::Close) | None => String::new(), - Some(_) => { - let end = *pos + 1; - let mut p = *pos; - let s = math_seq(t.get(..end).unwrap_or_default(), &mut p, depth + 1, false); - *pos = end; - s - } - } -} - -/// An optional `[…]` argument. -fn math_opt(t: &[MTok], pos: &mut usize, depth: usize) -> Option { - if t.get(*pos) != Some(&MTok::OptOpen) { - return None; - } - *pos += 1; - let s = math_seq(t, pos, depth + 1, true); - if t.get(*pos) == Some(&MTok::OptClose) { - *pos += 1; - } - Some(s) -} - -/// Parenthesise a fraction part or script unless it is a single symbol. -fn wrap_part(s: &str) -> String { - let s = s.trim(); - if s.chars().count() == 1 || (!s.is_empty() && s.chars().all(|c| c.is_ascii_digit())) { s.to_string() } else { format!("({s})") } -} - -fn math_seq(t: &[MTok], pos: &mut usize, depth: usize, in_opt: bool) -> String { - let mut o = String::new(); - while let Some(tok) = t.get(*pos) { - match tok { - MTok::Close => break, - MTok::OptClose if in_opt => break, - MTok::Open => { - *pos += 1; - if depth < MAX_NEST { - o.push_str(&math_seq(t, pos, depth + 1, false)); - } - if t.get(*pos) == Some(&MTok::Close) { - *pos += 1; - } - continue; - } - MTok::OptOpen => o.push('['), - MTok::OptClose => o.push(']'), - MTok::Char('^') | MTok::Char('_') => { - let c = if *tok == MTok::Char('^') { '^' } else { '_' }; - *pos += 1; - let a = if depth < MAX_NEST { math_arg(t, pos, depth) } else { String::new() }; - o.push(c); - o.push_str(&wrap_part(&a)); - continue; - } - MTok::Char('&') => o.push(' '), - MTok::Char('~') => o.push(' '), - MTok::Char(c) => o.push(*c), - MTok::Cmd(name) => { - *pos += 1; - let name = name.as_str(); - if depth >= MAX_NEST { - continue; - } - match name { - "frac" | "dfrac" | "tfrac" | "cfrac" => { - let a = math_arg(t, pos, depth); - let b = math_arg(t, pos, depth); - o.push_str(&format!("{}/{}", wrap_part(&a), wrap_part(&b))); - } - "binom" | "dbinom" | "tbinom" => { - let a = math_arg(t, pos, depth); - let b = math_arg(t, pos, depth); - o.push_str(&format!("({a}¦{b})")); - } - "sqrt" => { - let n = math_opt(t, pos, depth); - let a = math_arg(t, pos, depth); - match n { - Some(n) => o.push_str(&format!("√({n}&{a})")), - None => o.push_str(&format!("√({a})")), - } - } - "left" | "right" | "big" | "Big" | "bigg" | "Bigg" | "bigl" | "bigr" | "Bigl" | "Bigr" | "middle" => { - // Keep the delimiter, drop the sizing; `\left.` is no delimiter. - if t.get(*pos) == Some(&MTok::Char('.')) { - *pos += 1; - } - } - "text" | "textrm" | "textit" | "textbf" | "mathrm" | "mathit" | "mathbf" | "mathsf" | "mathtt" | "mathcal" | "mathbb" - | "mathfrak" | "boldsymbol" | "bm" | "pmb" | "operatorname" | "mbox" | "hbox" | "displaystyle" | "textstyle" | "scriptstyle" => { - if t.get(*pos) == Some(&MTok::Open) || !matches!(name, "displaystyle" | "textstyle" | "scriptstyle") { - let a = math_arg(t, pos, depth); - o.push_str(&a); - } - } - "overline" | "bar" => o.push_str(&format!("{}¯", wrap_part(&math_arg(t, pos, depth)))), - "hat" | "widehat" => o.push_str(&format!("{}̂", math_arg(t, pos, depth))), - "tilde" | "widetilde" => o.push_str(&format!("{}̃", math_arg(t, pos, depth))), - "vec" => o.push_str(&format!("{}⃗", math_arg(t, pos, depth))), - "dot" => o.push_str(&format!("{}̇", math_arg(t, pos, depth))), - "ddot" => o.push_str(&format!("{}̈", math_arg(t, pos, depth))), - "acute" => o.push_str(&format!("{}́", math_arg(t, pos, depth))), - "grave" => o.push_str(&format!("{}̀", math_arg(t, pos, depth))), - "breve" => o.push_str(&format!("{}̆", math_arg(t, pos, depth))), - "check" => o.push_str(&format!("{}̌", math_arg(t, pos, depth))), - "pmod" => { - let a = math_arg(t, pos, depth); - o.push_str(&format!(" (mod {a})")); - } - "bmod" => o.push_str(" mod "), - "begin" | "end" => { - // Inner environments (`matrix`, `cases`, `aligned`…): keep the content. - let _ = math_arg(t, pos, depth); - if name == "begin" { - let _ = math_opt(t, pos, depth); - } - } - "," | ";" | ":" | "quad" | "qquad" | " " | "enspace" | "thinspace" => o.push(' '), - "!" | "limits" | "nolimits" | "nonumber" | "notag" => {} - "\\" | "cr" => o.push(' '), - "{" | "}" | "%" | "#" | "&" | "$" | "_" => o.push_str(name), - "label" | "tag" => { - let _ = math_arg(t, pos, depth); - } - n if MATH_FUNCTIONS.contains(&n) => { - o.push_str(n); - o.push(' '); - } - n => match math_symbol(n) { - Some(c) => o.push(c), - // Linear format knows LaTeX-style control words too. - None => { - o.push('\\'); - o.push_str(n); - o.push(' '); - } - }, - } - continue; - } - } - *pos += 1; - } - o -} diff --git a/crates/formats/src/latex/parser.rs b/crates/formats/src/latex/parser.rs deleted file mode 100644 index cd5e63507b..0000000000 --- a/crates/formats/src/latex/parser.rs +++ /dev/null @@ -1,914 +0,0 @@ -use super::lex::{Tok, accent_mark, compose, ligatures, text_symbol, tokenize}; -use super::limits::{MAX_ANCHOR_RUN, MAX_NEST, MAX_SOURCE, MAX_SPEC}; -use super::math::latex_to_linear; -use super::table::{raw_text, source_text, table}; -use super::units::{length_pt, parse_color, size_of}; -use super::{Align, Document, FBlock, Flow, Fmt, Inline, Kind, ListInfo, Para, model}; - -// ============================================================================================= -// Import - -/// Import a LaTeX source file. -pub fn import(bytes: &[u8]) -> Document { - model::begin_import(); - let mut d = model::to_doc(&parse(&crate::txt::decode(bytes.get(..MAX_SOURCE).unwrap_or(bytes)))); - d.ensure_nonempty(); - d -} - -/// Commands dropped together with their arguments (layout and definitions, not text). -const DROP_WITH_ARGS: &[&str] = &[ - "documentclass", - "usepackage", - "RequirePackage", - "newcommand", - "renewcommand", - "providecommand", - "newcommand*", - "renewcommand*", - "providecommand*", - "newenvironment", - "renewenvironment", - "DeclareMathOperator", - "DeclareMathOperator*", - "def", - "let", - "setlength", - "addtolength", - "setcounter", - "addtocounter", - "stepcounter", - "refstepcounter", - "vspace", - "vspace*", - "hspace", - "hspace*", - "vskip", - "hskip", - "pagestyle", - "thispagestyle", - "pagenumbering", - "bibliographystyle", - "bibliography", - "addbibresource", - "printbibliography", - "input", - "include", - "includeonly", - "graphicspath", - "hypersetup", - "definecolor", - "geometry", - "numberwithin", - "newtheorem", - "newtheorem*", - "theoremstyle", - "phantom", - "hphantom", - "vphantom", - "index", - "nocite", - "addcontentsline", - "markboth", - "markright", - "linespread", - "setstretch", - "fancyhf", - "fancyhead", - "fancyfoot", - "lstset", - "captionsetup", - "setmainfont", - "setsansfont", - "setmonofont", - "newfontfamily", - "date", - "thanks", - "maketitle*", - "tableofcontents", - "listoffigures", - "listoftables", - "appendix", - "frontmatter", - "mainmatter", - "backmatter", - "cline", - "hline", - "toprule", - "midrule", - "bottomrule", - "cmidrule", - "addlinespace", - "endhead", - "endfirsthead", - "endfoot", - "endlastfoot", - "rowcolor", - "arraystretch", - "smallskip", - "medskip", - "bigskip", - "today", - "and", -]; - -/// Paragraph-level context from the enclosing environments. -#[derive(Clone, Copy, Default)] -struct Ctx { - kind: Kind, - align: Option, -} - -pub(super) struct Parser<'a> { - pub(super) t: &'a [Tok], - pub(super) pos: usize, - blocks: Vec, - para: Para, - /// Character formatting in effect (groups save and restore it). - fmt: Fmt, - /// Paragraph context of the enclosing environments. - ctx: Ctx, - /// Text that followed an optional argument's `]` in the same token, emitted next. - pending: Option, - /// Open lists: ordered? - lists: Vec, - page_break: bool, - depth: usize, - meta: model::Meta, - /// Sectioning levels in use: `\part`/`\chapter` push `\section` down. - top: u8, -} - -impl<'a> Parser<'a> { - pub(super) fn new(t: &'a [Tok], depth: usize) -> Parser<'a> { - Parser { - t, - pos: 0, - blocks: Vec::new(), - para: Para::default(), - fmt: Fmt::default(), - ctx: Ctx::default(), - pending: None, - lists: Vec::new(), - page_break: false, - depth, - meta: Default::default(), - top: 0, - } - } - - fn fmt(&self) -> Fmt { - self.fmt.clone() - } - fn fmt_mut(&mut self) -> &mut Fmt { - &mut self.fmt - } - fn ctx(&self) -> Ctx { - self.ctx - } - - /// A bookmark here. `text` looks back past the bookmarks that end the paragraph to see what - /// the last text was, so a run of them with nothing between is kept short: after - /// [`MAX_ANCHOR_RUN`] in a row further ones are left out (thousands of labels with no text - /// between them made reading a file take time quadratic in its size). - fn anchor(&mut self, name: String) { - let run = self.para.inlines.iter().rev().take(MAX_ANCHOR_RUN).take_while(|i| matches!(i, Inline::Anchor(_))).count(); - if run < MAX_ANCHOR_RUN { - self.para.inlines.push(Inline::Anchor(name)); - } - } - - fn text(&mut self, s: &str) { - // Spaces collapse, and none start a paragraph (as in TeX). - let after_space = match self.para.inlines.iter().rev().find(|i| !matches!(i, Inline::Anchor(_))) { - Some(Inline::Text(t, _)) => t.is_empty() || t.ends_with([' ', '\n']), - Some(_) => false, - None => true, - }; - let s = if after_space { s.trim_start_matches(' ') } else { s }; - if s.is_empty() { - return; - } - let f = self.fmt(); - self.para.push_text(s, &f); - } - - /// End the current paragraph (if it has content). - fn flush(&mut self) { - let mut p = std::mem::take(&mut self.para); - p.trim(); - if p.is_empty() && !p.inlines.iter().any(|i| matches!(i, Inline::Image(_) | Inline::Equation { .. })) { - // Keep a list item's position even when empty. - if p.list.is_none() { - return; - } - } - let c = self.ctx(); - if p.kind == Kind::Normal { - p.kind = c.kind; - } - if p.align.is_none() { - p.align = c.align; - } - if self.page_break { - p.page_break = true; - self.page_break = false; - } - self.blocks.push(FBlock::Para(p)); - } - - pub(super) fn skip_spaces(&mut self) { - while let Some(Tok::Text(s)) = self.t.get(self.pos) { - if !s.trim().is_empty() { - break; - } - self.pos += 1; - } - } - - /// The tokens of an optional `[…]` argument, if one follows. - pub(super) fn opt_arg(&mut self) -> Option { - let save = self.pos; - self.skip_spaces(); - let Some(Tok::Text(s)) = self.t.get(self.pos) else { - self.pos = save; - return None; - }; - if !s.starts_with('[') { - self.pos = save; - return None; - } - // Collect raw text up to the matching `]` (no nested brackets across tokens). - let mut out = String::new(); - let mut first = true; - while let Some(tok) = self.t.get(self.pos) { - match tok { - Tok::Text(s) => { - let s = if first { s.get(1..).unwrap_or("") } else { s.as_str() }; - first = false; - if let Some(end) = s.find(']') { - out.push_str(s.get(..end).unwrap_or("")); - let rest = s.get(end + 1..).unwrap_or("").to_string(); - self.pos += 1; - if !rest.trim().is_empty() { - // The text after `]` comes next (tokens are shared, so it waits here). - self.pending = Some(rest); - } - return Some(out); - } - out.push_str(s); - } - Tok::Open => out.push('{'), - Tok::Close => out.push('}'), - Tok::Cmd(c) => { - out.push('\\'); - out.push_str(c); - } - Tok::Par => break, - _ => {} - } - self.pos += 1; - } - Some(out) - } - - /// The tokens of a `{…}` argument (or a single token), consumed. - pub(super) fn arg_tokens(&mut self) -> &'a [Tok] { - self.skip_spaces(); - match self.t.get(self.pos) { - Some(Tok::Open) => { - let start = self.pos + 1; - let mut depth = 0usize; - let mut i = self.pos; - while let Some(tok) = self.t.get(i) { - match tok { - Tok::Open => depth += 1, - Tok::Close => { - depth -= 1; - if depth == 0 { - self.pos = i + 1; - return self.t.get(start..i).unwrap_or_default(); - } - } - _ => {} - } - i += 1; - } - self.pos = self.t.len(); - self.t.get(start..).unwrap_or_default() - } - Some(Tok::Text(_)) => { - // A single character argument (`\textbf x`) is rare: take the whole token. - let i = self.pos; - self.pos += 1; - self.t.get(i..i + 1).unwrap_or_default() - } - Some(Tok::Cmd(_)) => { - let i = self.pos; - self.pos += 1; - self.t.get(i..i + 1).unwrap_or_default() - } - _ => &[], - } - } - - /// An argument as plain text (no formatting): labels, URLs, colour names. - pub(super) fn arg_raw(&mut self) -> String { - raw_text(self.arg_tokens()) - } - - /// An argument as LaTeX source (braces kept): column specs, lengths. - fn arg_source(&mut self) -> String { - source_text(self.arg_tokens()) - } - - /// An argument's text as it would be shown (formatting dropped): titles and authors. - fn arg_text(&mut self) -> String { - let toks = self.arg_tokens(); - let mut sub = Parser::new(toks, self.depth + 1); - if self.depth < MAX_NEST { - sub.run(None); - } - let (blocks, _) = sub.finish(); - let text: Vec = blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.text()) } else { None }).collect(); - squash(&text.join(" ")) - } - - /// Parse `toks` inline into the current paragraph with formatting `f` applied. - fn inline_with(&mut self, toks: &'a [Tok], f: impl FnOnce(&mut Fmt)) { - if self.depth >= MAX_NEST { - let s = raw_text(toks); - self.text(&s); - return; - } - let saved_fmt = self.fmt.clone(); - f(&mut self.fmt); - let (saved_t, saved_pos) = (self.t, self.pos); - self.t = toks; - self.pos = 0; - self.depth += 1; - self.run(None); - self.flush_pending(); - self.depth -= 1; - self.t = saved_t; - self.pos = saved_pos; - self.fmt = saved_fmt; - } - - /// Skip every `[…]` and `{…}` argument that follows (and, for definitions, the macro name). - fn drop_args(&mut self, name: &str) { - if matches!(name, "newcommand" | "renewcommand" | "providecommand" | "newcommand*" | "renewcommand*" | "providecommand*" | "def" | "let") { - self.skip_spaces(); - if matches!(self.t.get(self.pos), Some(Tok::Cmd(_))) { - self.pos += 1; - } - if name == "let" { - self.skip_spaces(); - if matches!(self.t.get(self.pos), Some(Tok::Cmd(_))) { - self.pos += 1; - } - return; - } - if name == "def" { - // `\def\x#1{…}`: the parameter text, then the body. - while matches!(self.t.get(self.pos), Some(Tok::Text(_))) { - self.pos += 1; - } - } - } - loop { - let save = self.pos; - self.skip_spaces(); - match self.t.get(self.pos) { - Some(Tok::Open) => { - let _ = self.arg_tokens(); - } - Some(Tok::Text(s)) if s.starts_with('[') => { - let _ = self.opt_arg(); - if self.pending.is_some() { - return; - } - } - _ => { - self.pos = save; - return; - } - } - } - } - - fn heading(&mut self, level: u8) { - let _ = self.opt_arg(); - let toks = self.arg_tokens(); - self.flush(); - self.para = Para::new(Kind::Heading(level.clamp(1, 6))); - self.inline_with(toks, |_| {}); - self.flush(); - } - - /// Parse until `\end{env}` (when given), a closing brace at this level, or the end. - fn flush_pending(&mut self) { - if let Some(p) = self.pending.take() { - self.text(&ligatures(&p)); - } - } - - pub(super) fn run(&mut self, env: Option<&str>) { - loop { - self.flush_pending(); - let Some(tok) = self.t.get(self.pos) else { break }; - self.pos += 1; - match tok { - Tok::Text(s) => { - let s = ligatures(s); - self.text(&s); - } - Tok::Par => self.flush(), - Tok::Tie => self.text("\u{A0}"), - Tok::Amp => self.text(" "), - Tok::Open => { - // A group scopes declarations (`{\bfseries …}`, `{\centering …\par}`): - // character formatting and paragraph alignment come back at its end. - let toks = { - self.pos -= 1; - self.arg_tokens() - }; - let saved_ctx = self.ctx; - self.inline_with(toks, |_| {}); - self.ctx = saved_ctx; - } - Tok::Close => {} - Tok::Math(m, display) => { - let linear = latex_to_linear(m); - if linear.is_empty() { - continue; - } - if *display { - self.flush(); - self.para.inlines.push(Inline::Equation { linear, display: true }); - self.flush(); - } else { - self.para.inlines.push(Inline::Equation { linear, display: false }); - } - } - Tok::Verbatim(v) => { - self.flush(); - let v = v.strip_prefix('\n').unwrap_or(v); - let v = v.strip_suffix('\n').unwrap_or(v); - for line in v.lines() { - let mut p = Para::new(Kind::Code); - p.push_text(line.trim_end_matches('\r'), &Fmt::default()); - if line.trim().is_empty() { - p.inlines.push(Inline::Text(String::new(), Fmt::default())); - } - self.blocks.push(FBlock::Para(p)); - } - } - Tok::Verb(v) => { - let mut f = self.fmt(); - f.code = true; - self.para.push_text(v, &f); - } - // Too deep: the environment's content goes on at this level. - Tok::Begin(_) if self.depth >= MAX_NEST => {} - Tok::Begin(name) => self.environment(name), - Tok::End(name) => { - if env == Some(name.as_str()) { - return; - } - // A stray `\end`: ignore. - } - Tok::Cmd(name) => self.command(name), - } - } - } - - fn command(&mut self, name: &str) { - if let Some(sym) = text_symbol(name) { - if name == " " { - // A control space is never collapsed into the space before it. - let f = self.fmt(); - self.para.push_text(sym, &f); - } else if name == "item" { - self.item(); - } else if matches!(name, "centering" | "raggedleft" | "raggedright") { - let a = match name { - "centering" => Align::Center, - "raggedleft" => Align::Right, - _ => Align::Left, - }; - self.ctx.align = Some(a); - } else { - self.text(sym); - } - return; - } - if let Some(mark) = accent_mark(name) { - // `\"o` takes one letter of the text after it; `\"{o}` the group. - self.skip_spaces(); - let arg = match self.t.get(self.pos) { - Some(Tok::Text(t)) => { - self.pos += 1; - let mut chars = t.chars(); - let first = chars.next().map(String::from).unwrap_or_default(); - if !chars.as_str().is_empty() { - self.pending = Some(chars.as_str().to_string()); - } - first - } - _ => self.arg_raw(), - }; - let mut chars = arg.chars(); - let s = match chars.next() { - Some(b) => format!("{}{}", compose(b, mark), chars.as_str()), - // An accent over nothing prints the accent alone: the spacing form of the mark. - None => match name { - "`" | "^" | "~" => name.to_string(), - "'" => "\u{B4}".to_string(), - "\"" => "\u{A8}".to_string(), - _ => mark.to_string(), - }, - }; - self.text(&s); - return; - } - if let Some(size) = size_of(name) { - self.fmt_mut().size = (size > 0.0).then_some(size); - return; - } - match name { - "\\" | "newline" | "linebreak" | "break" => { - let _ = self.opt_arg(); - self.text("\n"); - } - "part" | "part*" => self.heading(1), - "chapter" | "chapter*" => self.heading(self.top.max(1)), - "section" | "section*" => self.heading(1 + self.top), - "subsection" | "subsection*" => self.heading(2 + self.top), - "subsubsection" | "subsubsection*" => self.heading(3 + self.top), - "paragraph" | "paragraph*" => self.heading(4 + self.top), - "subparagraph" | "subparagraph*" => self.heading(5 + self.top), - "title" => self.meta.title = self.arg_text(), - "author" => self.meta.author = self.arg_text(), - "maketitle" => { - self.flush(); - if !self.meta.title.is_empty() { - let mut p = Para::new(Kind::Title); - p.push_text(&self.meta.title, &Fmt::default()); - self.blocks.push(FBlock::Para(p)); - } - if !self.meta.author.is_empty() { - let mut p = Para { align: Some(Align::Center), ..Default::default() }; - p.push_text(&self.meta.author, &Fmt::default()); - self.blocks.push(FBlock::Para(p)); - } - } - "newpage" | "clearpage" | "cleardoublepage" | "pagebreak" | "eject" => { - let _ = self.opt_arg(); - self.flush(); - self.page_break = true; - } - "par" => self.flush(), - "hrule" | "hrulefill" | "rule" => { - let _ = self.opt_arg(); - let full = if name == "rule" { - let w = self.arg_source(); - let _ = self.arg_raw(); - w.contains("linewidth") || w.contains("textwidth") || w.contains("columnwidth") - } else { - true - }; - if full { - self.flush(); - self.blocks.push(FBlock::Para(Para::new(Kind::Rule))); - } - } - "textbf" => self.with_arg(|f| f.bold = true), - "textit" | "textsl" => self.with_arg(|f| f.italic = true), - "emph" => self.with_arg(|f| f.italic = !f.italic), - "underline" | "uline" | "ul" | "uuline" => self.with_arg(|f| f.underline = true), - "sout" | "st" | "xout" | "cancel" => self.with_arg(|f| f.strike = true), - "texttt" | "code" | "path" => self.with_arg(|f| f.code = true), - "textsuperscript" => self.with_arg(|f| f.sup = true), - "textsubscript" => self.with_arg(|f| f.sub = true), - "textmd" | "textup" | "textrm" | "textsf" | "textsc" | "textnormal" | "mbox" | "makebox" | "fbox" | "framebox" | "hbox" | "text" - | "textls" | "foreignlanguage" | "enquote" | "MakeUppercase" | "MakeLowercase" | "uppercase" | "lowercase" => { - if name == "foreignlanguage" { - let _ = self.arg_raw(); - } - if matches!(name, "makebox" | "framebox") { - let _ = self.opt_arg(); - let _ = self.opt_arg(); - } - let quote = name == "enquote"; - if quote { - self.text("“"); - } - self.with_arg(|f| { - if name == "textmd" { - f.bold = false; - } - if name == "textup" { - f.italic = false; - } - if name == "textnormal" { - *f = Fmt { link: f.link.clone(), ..Default::default() }; - } - }); - if quote { - self.text("”"); - } - } - "bfseries" | "bf" => self.fmt_mut().bold = true, - "itshape" | "it" | "slshape" | "sl" | "em" => self.fmt_mut().italic = true, - "ttfamily" | "tt" => self.fmt_mut().code = true, - "mdseries" => self.fmt_mut().bold = false, - "upshape" => self.fmt_mut().italic = false, - "rmfamily" | "sffamily" | "scshape" | "sc" | "rm" | "sf" | "normalfont*" => {} - "fontsize" => { - let s = self.arg_source(); - let _ = self.arg_raw(); - self.fmt_mut().size = length_pt(&s).or_else(|| s.trim().parse().ok()).filter(|v: &f32| *v >= 1.0 && *v <= 1638.0); - } - "textcolor" | "color" | "colorbox" | "highlight" | "hl" | "cellcolor" => { - let model = self.opt_arg(); - let spec = if matches!(name, "hl" | "highlight") { "yellow".to_string() } else { self.arg_raw() }; - let c = parse_color(model.as_deref(), &spec); - match name { - "color" => self.fmt_mut().color = c, - "cellcolor" => {} - "textcolor" => self.with_arg(move |f| f.color = c.or(f.color)), - _ => self.with_arg(move |f| f.background = c.or(f.background)), - } - } - "href" => { - let url = model::kept_link(&self.arg_raw()); - self.with_arg(move |f| f.link = url); - } - "url" | "nolinkurl" => { - let url = self.arg_raw(); - let mut f = self.fmt(); - if name == "url" { - f.link = model::kept_link(&url); - } - self.para.push_text(&url, &f); - } - "hyperlink" => { - let target = self.arg_raw(); - let link = model::kept_link(&format!("#{target}")); - self.with_arg(move |f| f.link = link); - } - "hypertarget" => { - let target = self.arg_raw(); - self.anchor(target); - self.with_arg(|_| {}); - } - "label" => { - let l = self.arg_raw(); - self.anchor(l); - } - "ref" | "eqref" | "pageref" | "autoref" | "cref" | "Cref" | "nameref" => { - let l = self.arg_raw(); - let mut f = self.fmt(); - f.link = model::kept_link(&format!("#{l}")); - self.para.push_text(&l, &f); - } - "cite" | "citep" | "citet" | "parencite" | "textcite" | "autocite" => { - let _ = self.opt_arg(); - let _ = self.opt_arg(); - let keys = self.arg_raw(); - let keys: Vec<&str> = keys.split(',').map(str::trim).filter(|k| !k.is_empty()).collect(); - self.text(&format!("[{}]", keys.join(", "))); - } - "footnote" | "footnotetext" | "marginpar" => { - let _ = self.opt_arg(); - self.text(" ("); - self.with_arg(|_| {}); - self.text(")"); - } - "includegraphics" => { - let _ = self.opt_arg(); - let file = self.arg_raw(); - let mut f = self.fmt(); - f.italic = true; - self.para.push_text(&format!("[Picture: {}]", file.trim()), &f); - } - "caption" | "caption*" => { - let _ = self.opt_arg(); - let toks = self.arg_tokens(); - self.flush(); - self.para.align = Some(Align::Center); - self.inline_with(toks, |f| f.italic = true); - self.flush(); - } - "bibitem" => { - let label = self.opt_arg(); - let key = self.arg_raw(); - self.flush(); - self.text(&format!("[{}] ", label.unwrap_or(key))); - } - "multicolumn" | "multirow" => { - // Outside a tabular: keep the content. - let _ = self.arg_raw(); - let _ = self.opt_arg(); - let _ = self.arg_raw(); - self.with_arg(|_| {}); - } - "begin" | "end" => {} - n if DROP_WITH_ARGS.contains(&n) => self.drop_args(n), - _ => { - // Unknown: drop the command, keep its arguments' text. - let _ = self.opt_arg(); - } - } - } - - fn with_arg(&mut self, f: impl FnOnce(&mut Fmt)) { - let toks = self.arg_tokens(); - self.inline_with(toks, f); - } - - fn item(&mut self) { - self.flush(); - let level = self.lists.len().saturating_sub(1).min(8) as u8; - self.para = Para::default(); - if let Some(&ordered) = self.lists.last() { - self.para.list = Some(ListInfo { ordered, level, start: 1, ..Default::default() }); - } - let label = self.opt_arg(); - if let Some(l) = label { - let mut f = self.fmt(); - f.bold = true; - self.para.push_text(&format!("{} ", l.trim()), &f); - } - } - - fn environment(&mut self, name: &str) { - self.depth += 1; - match name { - "document" => { - self.run(Some(name)); - } - "itemize" | "enumerate" | "description" | "list" | "compactitem" | "compactenum" | "inparaenum" => { - let _ = self.opt_arg(); - if name == "list" { - let _ = self.arg_raw(); - let _ = self.arg_raw(); - } - self.flush(); - self.lists.push(matches!(name, "enumerate" | "compactenum" | "inparaenum")); - self.run(Some(name)); - self.flush(); - self.lists.pop(); - } - "quote" | "quotation" | "verse" | "abstract" => { - self.flush(); - if name == "abstract" { - let mut p = Para { align: Some(Align::Center), ..Default::default() }; - p.push_text("Abstract", &Fmt { bold: true, ..Default::default() }); - self.blocks.push(FBlock::Para(p)); - } - let saved = self.ctx; - self.ctx.kind = Kind::Quote; - self.run(Some(name)); - self.flush(); - self.ctx = saved; - } - "center" | "flushleft" | "flushright" | "centering" => { - self.flush(); - let align = match name { - "flushleft" => Align::Left, - "flushright" => Align::Right, - _ => Align::Center, - }; - let saved = self.ctx; - self.ctx.align = Some(align); - self.run(Some(name)); - self.flush(); - self.ctx = saved; - } - "tabular" | "tabular*" | "tabularx" | "tabulary" | "longtable" | "longtable*" | "array" | "tblr" | "longtblr" => { - let _ = self.opt_arg(); - if matches!(name, "tabular*" | "tabularx" | "tabulary") { - let _ = self.arg_raw(); - } - // A real column spec is short; capping it bounds the work on hostile input. - let spec: String = self.arg_source().chars().take(MAX_SPEC).collect(); - let toks = self.until_end(name); - self.flush(); - let t = table(toks, &spec, self.depth); - if !t.rows.is_empty() { - self.blocks.push(FBlock::Table(t)); - } - } - "thebibliography" => { - let _ = self.arg_raw(); - self.flush(); - let mut p = Para::new(Kind::Heading(1 + self.top)); - p.push_text("References", &Fmt::default()); - self.blocks.push(FBlock::Para(p)); - self.run(Some(name)); - self.flush(); - } - "minipage" | "figure" | "figure*" | "table" | "table*" | "wrapfigure" | "subfigure" | "titlepage" | "multicols" | "multicols*" - | "small" | "footnotesize" | "large" | "theorem" | "lemma" | "proof" | "definition" | "example" | "remark" | "corollary" - | "proposition" => { - // Floats and boxes: their content, in order. - let _ = self.opt_arg(); - if matches!(name, "minipage" | "subfigure" | "multicols" | "multicols*") { - let _ = self.arg_raw(); - } - if name == "wrapfigure" { - let _ = self.arg_raw(); - let _ = self.arg_raw(); - } - self.flush(); - if matches!(name, "theorem" | "lemma" | "proof" | "definition" | "example" | "remark" | "corollary" | "proposition") { - let mut label: String = name.chars().take(1).flat_map(char::to_uppercase).chain(name.chars().skip(1)).collect(); - label.push_str(". "); - self.para.push_text(&label, &Fmt { bold: name != "proof", italic: name == "proof", ..Default::default() }); - } - let (saved_fmt, saved_ctx) = (self.fmt.clone(), self.ctx); - if let Some(s) = size_of(name) { - self.fmt.size = (s > 0.0).then_some(s); - } - self.run(Some(name)); - self.flush(); - self.fmt = saved_fmt; - self.ctx = saved_ctx; - } - _ => { - // Unknown environment: its content. - let _ = self.opt_arg(); - let (saved_fmt, saved_ctx) = (self.fmt.clone(), self.ctx); - self.run(Some(name)); - self.fmt = saved_fmt; - self.ctx = saved_ctx; - } - } - self.depth -= 1; - } - - /// The tokens up to the matching `\end{name}` (consumed). - fn until_end(&mut self, name: &str) -> &'a [Tok] { - let start = self.pos; - let mut depth = 0usize; - while let Some(tok) = self.t.get(self.pos) { - match tok { - Tok::Begin(n) if n == name => depth += 1, - Tok::End(n) if n == name => { - if depth == 0 { - let toks = self.t.get(start..self.pos).unwrap_or_default(); - self.pos += 1; - return toks; - } - depth -= 1; - } - _ => {} - } - self.pos += 1; - } - self.t.get(start..).unwrap_or_default() - } - - pub(super) fn finish(mut self) -> (Vec, model::Meta) { - self.flush_pending(); - self.flush(); - (self.blocks, self.meta) - } -} - -/// Parse LaTeX source into a flow. -pub fn parse(src: &str) -> Flow { - let toks = tokenize(src); - // The preamble only contributes metadata; without `\begin{document}` it is all body. - let body_at = toks.iter().position(|t| matches!(t, Tok::Begin(n) if n == "document")); - let mut meta = model::Meta::default(); - let (body, preamble): (&[Tok], &[Tok]) = match body_at { - Some(i) => (toks.get(i + 1..).unwrap_or_default(), toks.get(..i).unwrap_or_default()), - None => (&toks[..], &[]), - }; - let mut pre = Parser::new(preamble, 0); - while let Some(t) = pre.t.get(pre.pos) { - pre.pos += 1; - if let Tok::Cmd(c) = t { - match c.as_str() { - "title" => meta.title = pre.arg_text(), - "author" => meta.author = pre.arg_text(), - _ => {} - } - } - } - let mut p = Parser::new(body, 0); - p.meta = meta; - // `\part` and `\chapter` put `\section` one level down each. - let uses = |names: [&str; 2]| body.iter().any(|t| matches!(t, Tok::Cmd(c) if names.contains(&c.as_str()))); - p.top = u8::from(uses(["part", "part*"])) + u8::from(uses(["chapter", "chapter*"])); - p.run(Some("document")); - let (blocks, meta) = p.finish(); - Flow { blocks, meta } -} - -fn squash(s: &str) -> String { - s.split_whitespace().collect::>().join(" ") -} diff --git a/crates/formats/src/latex/table.rs b/crates/formats/src/latex/table.rs deleted file mode 100644 index f02c20cda6..0000000000 --- a/crates/formats/src/latex/table.rs +++ /dev/null @@ -1,228 +0,0 @@ -use super::lex::{Tok, text_symbol}; -use super::limits::MAX_NEST; -use super::parser::Parser; -use super::units::{parse_color, spec_widths}; -use super::{Cell, FTable}; - -/// Plain text of tokens (arguments that are names, URLs, lengths). -pub(super) fn raw_text(toks: &[Tok]) -> String { - let mut s = String::new(); - for t in toks { - match t { - Tok::Text(x) => s.push_str(x), - Tok::Tie => s.push('~'), - Tok::Cmd(c) => match text_symbol(c) { - Some(sym) => s.push_str(sym), - None if c == "\\" => s.push(' '), - None => { - s.push('\\'); - s.push_str(c); - } - }, - Tok::Verb(v) | Tok::Math(v, _) => s.push_str(v), - Tok::Amp => s.push('&'), - _ => {} - } - } - s.trim().to_string() -} - -/// Tokens back as LaTeX source (comments and layout whitespace normalised). -pub(super) fn source_text(toks: &[Tok]) -> String { - let mut s = String::new(); - for t in toks { - match t { - Tok::Text(x) => s.push_str(x), - Tok::Open => s.push('{'), - Tok::Close => s.push('}'), - Tok::Tie => s.push('~'), - Tok::Amp => s.push('&'), - Tok::Par => s.push(' '), - Tok::Cmd(c) => { - s.push('\\'); - s.push_str(c); - if c.chars().all(|c| c.is_ascii_alphabetic()) { - s.push(' '); - } - } - Tok::Math(m, _) => { - s.push('$'); - s.push_str(m); - s.push('$'); - } - Tok::Verb(v) | Tok::Verbatim(v) => s.push_str(v), - Tok::Begin(n) => s.push_str(&format!("\\begin{{{n}}}")), - Tok::End(n) => s.push_str(&format!("\\end{{{n}}}")), - } - } - s.trim().to_string() -} - -/// A `tabular` body as a table. -pub(super) fn table(toks: &[Tok], spec: &str, depth: usize) -> FTable { - let (spec_cols, widths) = spec_widths(spec); - let mut has_rules = spec.contains('|'); - // Split rows at `\\` and cells at `&`, outside braces and nested environments. - let mut rows: Vec> = Vec::new(); - let mut row: Vec<&[Tok]> = Vec::new(); - let mut start = 0usize; - let mut brace = 0usize; - let mut env = 0usize; - for (i, t) in toks.iter().enumerate() { - match t { - Tok::Open => brace += 1, - Tok::Close => brace = brace.saturating_sub(1), - Tok::Begin(_) => env += 1, - Tok::End(_) => env = env.saturating_sub(1), - Tok::Cmd(c) if brace == 0 && env == 0 && matches!(c.as_str(), "hline" | "cline" | "toprule" | "midrule" | "bottomrule") => { - has_rules = true; - } - Tok::Amp if brace == 0 && env == 0 => { - row.push(toks.get(start..i).unwrap_or_default()); - start = i + 1; - } - Tok::Cmd(c) if brace == 0 && env == 0 && (c == "\\" || c == "tabularnewline") => { - row.push(toks.get(start..i).unwrap_or_default()); - rows.push(std::mem::take(&mut row)); - start = i + 1; - } - _ => {} - } - if rows.len() >= wordcraft_doc::table::MAX_ROWS { - break; - } - } - if start < toks.len() { - row.push(toks.get(start..).unwrap_or_default()); - } - if !row.is_empty() { - rows.push(row); - } - let mut out = FTable { rows: Vec::new(), widths: Vec::new(), borderless: !has_rules, extra: None, kept: None, wrap: None }; - // Rows still covered by a `\multirow` above, per grid column. - let mut covering: Vec = Vec::new(); - for r in rows { - let mut cells = Vec::new(); - let mut g = 0usize; - // A document row keeps MAX_COLS cells; the rest would only widen `covering`, which is - // walked once per row. - for cell_toks in r.into_iter().take(wordcraft_doc::table::MAX_COLS) { - let mut cell = Cell::default(); - let mut content = trim_cell(cell_toks); - // `\multicolumn{n}{spec}{content}`, `\multirow{n}{width}{content}`. Each wrapper - // rescans its content, so only MAX_NEST of them are unwrapped (a cell nests one or - // two); further ones stay in the content. - let mut sub = Parser::new(content, depth + 1); - let mut wrappers = 0usize; - loop { - sub.skip_spaces(); - match sub.t.get(sub.pos) { - Some(Tok::Cmd(c)) if c == "multicolumn" && wrappers < MAX_NEST => { - wrappers += 1; - sub.pos += 1; - cell.colspan = sub.arg_raw().trim().parse::().unwrap_or(1).clamp(1, 63); - let _ = sub.arg_raw(); - content = sub.arg_tokens(); - sub = Parser::new(content, depth + 1); - } - Some(Tok::Cmd(c)) if (c == "multirow" || c == "multirow*") && wrappers < MAX_NEST => { - wrappers += 1; - sub.pos += 1; - let _ = sub.opt_arg(); - cell.rowspan = sub.arg_raw().trim().parse::().map(|n| n.unsigned_abs().clamp(1, 1000) as u32).unwrap_or(1); - let _ = sub.opt_arg(); - let _ = sub.arg_raw(); - let _ = sub.opt_arg(); - content = sub.arg_tokens(); - sub = Parser::new(content, depth + 1); - } - Some(Tok::Cmd(c)) if c == "cellcolor" => { - sub.pos += 1; - let model = sub.opt_arg(); - let spec = sub.arg_raw(); - cell.shading = parse_color(model.as_deref(), &spec); - } - _ => break, - } - } - if covering.get(g).copied().unwrap_or(0) > 0 { - cell.covered = true; - } - if !cell.covered && depth < MAX_NEST { - sub.run(None); - let (blocks, _) = sub.finish(); - cell.blocks = blocks; - } - let span = cell.colspan as usize; - if covering.len() < g + span { - covering.resize(g + span, 0); - } - for k in g..g + span { - if let Some(c) = covering.get_mut(k) - && cell.rowspan > 1 - && !cell.covered - { - *c = cell.rowspan; - } - } - g += span; - cells.push(cell); - } - for c in covering.iter_mut() { - *c = c.saturating_sub(1); - } - // `\hline` alone after the last `\\` is not a row. - let empty = cells.iter().all(|c| !c.covered && c.blocks.is_empty()); - if empty && cells.len() <= 1 { - continue; - } - out.rows.push(cells); - } - let cols = out.cols(); - if !widths.is_empty() && widths.len() == cols && spec_cols == cols { - out.widths = widths; - } - out -} - -/// A cell's tokens without the rules (`\hline`…) and spaces around them. -fn trim_cell(toks: &[Tok]) -> &[Tok] { - let skip = |t: &Tok| match t { - Tok::Cmd(c) => { - matches!(c.as_str(), "hline" | "toprule" | "midrule" | "bottomrule" | "endhead" | "endfirsthead" | "endfoot" | "endlastfoot" | "noalign") - } - Tok::Text(s) => s.trim().is_empty(), - _ => false, - }; - let mut a = 0; - while let Some(t) = toks.get(a) { - if skip(t) { - a += 1; - } else if matches!(t, Tok::Cmd(c) if c == "cline" || c == "cmidrule" || c == "addlinespace") { - // `\cline{2-3}`, `\cmidrule(lr){1-2}`: the command and its argument. - a += 1; - while let Some(Tok::Text(s)) = toks.get(a) { - if s.trim().is_empty() || s.trim_start().starts_with('(') || s.trim_start().starts_with('[') { - a += 1; - } else { - break; - } - } - if toks.get(a) == Some(&Tok::Open) { - while let Some(t) = toks.get(a) { - a += 1; - if *t == Tok::Close { - break; - } - } - } - } else { - break; - } - } - let mut b = toks.len(); - while b > a && toks.get(b - 1).is_some_and(skip) { - b -= 1; - } - toks.get(a..b).unwrap_or_default() -} diff --git a/crates/formats/src/latex/tests.rs b/crates/formats/src/latex/tests.rs deleted file mode 100644 index 2b81895a9c..0000000000 --- a/crates/formats/src/latex/tests.rs +++ /dev/null @@ -1,473 +0,0 @@ -use super::*; -use crate::model::FBlock; - -fn paras(src: &str) -> Vec { - parse(src).blocks.into_iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect() -} - -fn texts(src: &str) -> Vec { - paras(src).iter().map(|p| p.text()).collect() -} - -#[test] -fn empty_accent_group_is_plain_character() { - let t = texts("\\begin{document}x\\^{}y \\~{}z 2\\^{}10 \\texttt{\\~{}/dir}\\end{document}"); - assert_eq!(t, vec!["x^y ~z 2^10 ~/dir".to_string()]); - // The acute and the diaeresis alone are accents, not an apostrophe or a quotation mark. - assert_eq!(texts("\\begin{document}\\'{} \\\"{}\\end{document}"), vec!["\u{B4} \u{A8}".to_string()]); -} - -#[test] -fn unclosed_begin_keeps_its_brace() { - // `env_name` used to consume the `{` before giving up on a missing `}`. - let toks = tokenize("\\begin{ x"); - assert_eq!(toks.first(), Some(&Tok::Cmd("begin".into())), "{toks:?}"); - assert!(toks.contains(&Tok::Open), "{toks:?}"); - let toks = tokenize(&format!("\\end{{{}}}", "n".repeat(100))); - assert!(toks.contains(&Tok::Open), "{toks:?}"); -} - -#[test] -fn spec_widths_repeats_are_bounded() { - assert_eq!(spec_widths("*{3}{p{1cm}}").0, 3); - assert_eq!(spec_widths("*{2}{*{2}{l}}").0, 4); - let deep = format!("{}l{}", "*{1}{".repeat(MAX_NEST + 5), "}".repeat(MAX_NEST + 5)); - let (cols, widths) = spec_widths(&deep); - assert!(cols <= wordcraft_doc::table::MAX_COLS && widths.is_empty()); - let wide = format!("{}p{{1cm}}{}", "*{64}{".repeat(30), "}".repeat(30)); - let (cols, widths) = spec_widths(&wide); - assert_eq!(cols, wordcraft_doc::table::MAX_COLS); - assert!(widths.len() <= wordcraft_doc::table::MAX_COLS); -} - -#[test] -fn latex_length_pt_case_and_plus() { - assert_eq!(length_pt("12pt"), Some(12.0)); - assert_eq!(length_pt("12PT"), Some(12.0)); - assert_eq!(length_pt("+12pt"), Some(12.0)); - assert_eq!(length_pt("1IN"), Some(72.27)); - assert_eq!(length_pt("+2cm"), Some(56.9)); -} - -const ARTICLE: &str = r#"\documentclass[11pt]{article} -\usepackage[utf8]{inputenc} -\usepackage{amsmath} % maths -\newcommand{\R}{\mathbb{R}} -\title{On \emph{Sample} Documents} -\author{Ada Lovelace \and Charles Babbage} -\date{\today} -\begin{document} -\maketitle - -\section{Introduction}\label{sec:intro} -This is \textbf{bold}, \textit{italic}, \underline{underlined} and \texttt{code}. -TeX quotes ``like this'' and dashes -- or --- with 50\% and \$5 \& more~text. - -A second paragraph with a footnote\footnote{The note.} and a -\href{https://example.com}{link}. % a comment -See Section~\ref{sec:intro}. - -\subsection*{Lists} -\begin{itemize} - \item First - \item Second - \begin{enumerate} - \item Nested one - \item[b)] Nested two - \end{enumerate} -\end{itemize} - -\begin{quote} -Quoted text. -\end{quote} - -\begin{center} -Centered. -\end{center} - -\begin{verbatim} -fn main() { - println!("{}", 1 % 2); -} -\end{verbatim} - -\begin{table}[h] -\centering -\begin{tabular}{|p{2cm}|l|} -\hline -\textbf{Name} & Value \\ -\hline -\multicolumn{2}{|c|}{Wide} \\ -alpha & $x^2$ \\ -\hline -\end{tabular} -\caption{A table.} -\end{table} - -Energy: $E = mc^2$ and -\[ \frac{a+b}{2} \leq \sqrt{\alpha} \] -\begin{equation} - \sum_{i=1}^{n} i = \frac{n(n+1)}{2} -\end{equation} - -\newpage -Caf\'e na\"ive \c{c}a \v{s}\'{e} and {\bfseries grouped bold} after. -{\color{red} red text} \textcolor[HTML]{00FF00}{green}. -\end{document} -"#; - -#[test] -fn alignment_declarations_end_with_their_group() { - // #440: `{\centering …\par}` centres only inside the group; the outer alignment returns. - let align = |body: &str| -> Vec<(String, Option)> { - paras(&format!("\\documentclass{{article}}\\begin{{document}}{body}\\end{{document}}")).iter().map(|p| (p.text(), p.align)).collect() - }; - let a = |t: &str, al: Option| (t.to_string(), al); - assert_eq!(align(r"{\centering Center\par}After\par"), vec![a("Center", Some(Align::Center)), a("After", None)]); - assert_eq!( - align(r"\raggedleft Outer\par{\centering Inner\par}Restored\par"), - vec![a("Outer", Some(Align::Right)), a("Inner", Some(Align::Center)), a("Restored", Some(Align::Right))] - ); - assert_eq!(align(r"\centering One\par Two\par"), vec![a("One", Some(Align::Center)), a("Two", Some(Align::Center))]); -} - -#[test] -fn article_structure() { - let flow = parse(ARTICLE); - assert_article_metadata(&flow); - let ps = paras(ARTICLE); - assert_article_blocks(&ps); - assert_article_formatting(&ps); - assert_article_preamble_is_not_text(); -} - -fn assert_article_metadata(flow: &Flow) { - assert_eq!(flow.meta.title, "On Sample Documents"); - assert_eq!(flow.meta.author, "Ada Lovelace Charles Babbage"); -} - -fn assert_article_blocks(ps: &[Para]) { - let find = |t: &str| ps.iter().find(|p| p.text().trim() == t).unwrap_or_else(|| panic!("no {t:?} in {:?}", texts(ARTICLE))); - assert_eq!(find("On Sample Documents").kind, Kind::Title); - assert_eq!(find("Introduction").kind, Kind::Heading(1)); - assert_eq!(find("Lists").kind, Kind::Heading(2)); - assert_eq!(find("First").list, Some(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() })); - assert_eq!(find("Nested one").list, Some(ListInfo { ordered: true, level: 1, start: 1, ..Default::default() })); - assert!(ps.iter().any(|p| p.text().trim() == "b) Nested two" && p.list.is_some()), "{:?}", texts(ARTICLE)); - assert_eq!(find("Quoted text.").kind, Kind::Quote); - assert_eq!(find("Centered.").align, Some(Align::Center)); - assert_eq!(find("fn main() {").kind, Kind::Code); - assert_eq!(find("println!(\"{}\", 1 % 2);").kind, Kind::Code, "verbatim keeps % and indentation"); - assert!(ps.iter().any(|p| p.text() == " println!(\"{}\", 1 % 2);")); - assert_eq!(find("A table.").align, Some(Align::Center)); - let second = ps.iter().find(|p| p.text().starts_with("A second")).unwrap(); - assert!(second.text().contains("footnote (The note.) and a link."), "{}", second.text()); - assert!(second.inlines.iter().any(|i| matches!(i, Inline::Text(t, f) if t == "link" && f.link.as_deref() == Some("https://example.com")))); - assert!(!second.text().contains("comment")); -} - -fn assert_article_formatting(ps: &[Para]) { - let body = ps.iter().find(|p| p.text().starts_with("This is")).unwrap(); - let has = |t: &str, f: &dyn Fn(&Fmt) -> bool| body.inlines.iter().any(|i| matches!(i, Inline::Text(x, fm) if x == t && f(fm))); - assert!(has("bold", &|f| f.bold)); - assert!(has("italic", &|f| f.italic)); - assert!(has("underlined", &|f| f.underline)); - assert!(has("code", &|f| f.code)); - assert!(body.text().contains("“like this” and dashes – or — with 50% and $5 & more\u{A0}text."), "{}", body.text()); - let accents = ps.iter().find(|p| p.text().starts_with("Café")).unwrap(); - assert!(accents.page_break, "\\newpage"); - assert!(accents.text().starts_with("Café naïve ça šé and grouped bold after."), "{}", accents.text()); - assert!(accents.inlines.iter().any(|i| matches!(i, Inline::Text(t, f) if t == "grouped bold" && f.bold))); - assert!(accents.inlines.iter().any(|i| matches!(i, Inline::Text(t, f) if t.starts_with(" after.") && !f.bold)), "{:?}", accents.inlines); - assert!(accents.inlines.iter().any(|i| matches!(i, Inline::Text(t, f) if t.contains("red text") && f.color == Some(Rgb(255, 0, 0))))); - assert!(accents.inlines.iter().any(|i| matches!(i, Inline::Text(t, f) if t == "green" && f.color == Some(Rgb(0, 255, 0))))); -} - -fn assert_article_preamble_is_not_text() { - let all = texts(ARTICLE).join("\n"); - for gone in ["mathbb", "utf8", "11pt", "today", "sec:intro}", "amsmath", "maths"] { - assert!(!all.contains(gone), "{gone:?} leaked: {all}"); - } -} - -#[test] -fn tables_and_math() { - let flow = parse(ARTICLE); - let t = flow.blocks.iter().find_map(|b| if let FBlock::Table(t) = b { Some(t) } else { None }).expect("table"); - assert_eq!(t.rows.len(), 3, "{t:?}"); - assert_eq!(t.rows[0].iter().map(Cell::text).collect::>(), ["Name", "Value"]); - assert_eq!(t.rows[1].len(), 1); - assert_eq!(t.rows[1][0].colspan, 2); - assert_eq!(t.rows[1][0].text(), "Wide"); - assert!(t.widths.is_empty(), "the l column has no width"); - let eq = - |p: &Para| p.inlines.iter().find_map(|i| if let Inline::Equation { linear, display } = i { Some((linear.clone(), *display)) } else { None }); - let ps = paras(ARTICLE); - let energy = ps.iter().find(|p| p.text().starts_with("Energy")).unwrap(); - assert_eq!(eq(energy), Some(("E = mc^2".to_string(), false))); - let displays: Vec<_> = ps.iter().filter_map(eq).filter(|(_, d)| *d).map(|(l, _)| l).collect(); - assert_eq!(displays, ["(a+b)/2 ≤ √(α)", "∑_(i=1)^n i = (n(n+1))/2"]); - let cell = &t.rows[2][1]; - let FBlock::Para(p) = &cell.blocks[0] else { panic!() }; - assert_eq!(eq(p), Some(("x^2".to_string(), false))); -} - -#[test] -fn multirow_cells_and_widths() { - let src = r"\begin{tabular}{p{1in}p{2cm}} -\multirow{2}{*}{Tall} & a \\ - & b \\ -\end{tabular}"; - let flow = parse(src); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("{:?}", flow.blocks) }; - assert_eq!(t.rows.len(), 2); - assert_eq!(t.rows[0][0].rowspan, 2); - assert!(t.rows[1][0].covered); - assert_eq!(t.rows[1][1].text(), "b"); - assert_eq!(t.widths.len(), 2); - assert!((t.widths[0] - (72.27 + 12.0)).abs() < 0.1, "{:?}", t.widths); -} - -#[test] -fn fragments_without_a_document_environment() { - assert_eq!(texts("Just \\textbf{text}.\n\nTwo."), ["Just text.", "Two."]); - assert_eq!(texts(""), Vec::::new()); - assert_eq!(texts("\\unknown{kept} \\unknownopt[dropped]{kept too}"), ["kept kept too"]); - assert_eq!(texts("a\\\\b\\newline c"), ["a\nb\nc"]); - assert_eq!(texts("\\verb|\\x{}| y"), ["\\x{} y"]); - assert_eq!(texts("\\chapter{C}\\section{S}").len(), 2); - let ps = paras("\\chapter{C}\\section{S}"); - assert_eq!((ps[0].kind, ps[1].kind), (Kind::Heading(1), Kind::Heading(2))); -} - -#[test] -fn math_conversions() { - for (tex, linear) in [ - (r"\frac{1}{2}", "1/2"), - (r"x_{i}^{2}", "x_i^2"), - (r"x^{10}", "x^10"), - (r"e^{i\pi}", "e^(iπ)"), - (r"\sqrt[3]{x}", "√(3&x)"), - (r"\left( a \right)", "( a )"), - (r"\sin x + \text{if } y", "sin x + if y"), - (r"a \cdot b \times c \neq d", "a ⋅ b × c ≠ d"), - (r"a \bullet b", "a • b"), - (r"x \top y \bot z", "x ⊤ y ⊥ z"), - (r"f \colon X \to Y", "f : X → Y"), - (r"\hbar \hslash", "ℏ ℏ"), - (r"\bm{v}", "v"), - (r"\acute{x} \grave{y} \breve{z} \check{w}", "x́ ỳ z̆ w̌"), - (r"a \equiv b \pmod{m}", "a ≡ b (mod m)"), - (r"a \bmod b", "a mod b"), - (r"\unknowncmd", r"\unknowncmd"), - ] { - assert_eq!(latex_to_linear(tex), linear, "{tex}"); - } - for (linear, tex) in [ - ("x=(-b±√(b^2-4ac))/(2a)", r"x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}"), - ("α ≤ β", r"\alpha \leq \beta"), - ("αβ", r"\alpha\beta"), - ("πr", r"\pi r"), - ("a • b", r"a \bullet b"), - ("x ⊤ y", r"x \top y"), - ("a_(i+1)", r"a_{i+1}"), - ("50% & #", r"50\% \& \#"), - ("√(3&x)", r"\sqrt[3]{x}"), - ] { - assert_eq!(linear_to_latex(linear), tex, "{linear}"); - } -} - -#[test] -fn equations_survive_a_round_trip() { - let mut d = Document::new(); - let mut p = wordcraft_doc::Paragraph::with_text("Area ", Default::default()); - let n = p.len(); - p.insert_object( - n, - wordcraft_doc::para::InlineObject::Equation { linear: "π r^2".into(), display: false, math: Default::default() }, - &Default::default(), - ) - .unwrap(); - d.body = vec![wordcraft_doc::para_block(p)]; - let tex = export(&d); - assert!(tex.contains("$\\pi r^2$") && tex.contains("\\usepackage{amsmath}"), "{tex}"); - let back = import(tex.as_bytes()); - let wordcraft_doc::Block::Para(bp) = &*back.body[0] else { panic!() }; - assert!( - bp.objects.iter().any(|o| matches!(o, wordcraft_doc::para::InlineObject::Equation { linear, .. } if linear == "π r^2")), - "{:?}", - bp.objects - ); - // Formats without equations keep the text. - let md = crate::markdown::export(&d); - assert!(md.contains("π r^2"), "{md}"); -} - -#[test] -fn leading_brackets_stay_text() { - // `\item [x]` and `\\ [1]` would read the bracket as an optional argument. - assert_eq!(escape_text("[x] a\n [1] b [c]"), "{[}x] a\\\\\n {[}1] b [c]"); - let doc = crate::markdown::import("- [x] done"); - let back = import(export(&doc).as_bytes()); - assert_eq!(back.plain_text(Default::default()), "[x] done"); -} - -#[test] -fn special_characters_round_trip() { - let text = "50% of $5 & #1_a {b} ~c^ \\d |f| -- ``g'' tab\there"; - let d = Document::from_text(text); - let tex = export(&d); - let back = import(tex.as_bytes()); - assert_eq!(back.plain_text(Default::default()).trim(), text.replace('\t', "\u{2003}"), "{tex}"); -} - -#[test] -fn exported_tables_size_their_columns() { - let mut t = FTable { rows: Vec::new(), widths: vec![100.0, 200.0], borderless: false, extra: None, kept: None, wrap: None }; - let cell = |s: &str| Cell { - blocks: vec![FBlock::Para({ - let mut p = Para::default(); - p.push_text(s, &Fmt::default()); - p - })], - ..Default::default() - }; - t.rows.push(vec![cell("a"), cell("b")]); - t.rows.push(vec![Cell { colspan: 2, ..cell("wide") }]); - let mut w = Writer::default(); - w.table(&t); - assert!(w.out.contains("\\begin{tabular}{|p{88pt}|p{188pt}|}"), "{}", w.out); - assert!(w.out.contains("\\multicolumn{2}{|p{288pt}|}{wide}"), "{}", w.out); - let back = parse(&w.out); - let FBlock::Table(bt) = &back.blocks[0] else { panic!() }; - assert_eq!(bt.widths, vec![100.0, 200.0]); - assert_eq!(bt.rows[1][0].colspan, 2); -} - -#[test] -fn urls_preserve_tildes() { - let tex = r"\href{https://example.com/~user/page}{Homepage} and \url{https://example.com/~user/page}"; - let flow = parse(tex); - let FBlock::Para(p) = &flow.blocks[0] else { panic!() }; - assert_eq!(p.inlines[0], Inline::Text("Homepage".to_string(), Fmt { link: Some("https://example.com/~user/page".into()), ..Default::default() })); - assert_eq!( - p.inlines[2], - Inline::Text("https://example.com/~user/page".to_string(), Fmt { link: Some("https://example.com/~user/page".into()), ..Default::default() }) - ); -} - -#[test] -fn exported_borderless_tables_omit_rules() { - let mut t = FTable { rows: Vec::new(), widths: vec![100.0, 200.0], borderless: true, extra: None, kept: None, wrap: None }; - let cell = |s: &str| Cell { - blocks: vec![FBlock::Para({ - let mut p = Para::default(); - p.push_text(s, &Fmt::default()); - p - })], - ..Default::default() - }; - t.rows.push(vec![cell("a"), cell("b")]); - t.rows.push(vec![Cell { colspan: 2, ..cell("wide") }]); - let mut w = Writer::default(); - w.table(&t); - assert!(w.out.contains("\\begin{tabular}{p{88pt} p{188pt}}"), "{}", w.out); - assert!(!w.out.contains("\\hline"), "{}", w.out); - assert!(w.out.contains("\\multicolumn{2}{p{288pt}}{wide}"), "{}", w.out); - let back = parse(&w.out); - let FBlock::Table(bt) = &back.blocks[0] else { panic!() }; - assert!(bt.borderless, "parsed table should be borderless"); - assert_eq!(bt.widths, vec![100.0, 200.0]); - assert_eq!(bt.rows[1][0].colspan, 2); -} - -#[test] -fn table_borders_and_borderless_round_trip() { - use std::sync::Arc; - use wordcraft_doc::{Block, CharProps, Paragraph, Table, para_block}; - - // 1. Borderless table. - let mut doc = Document::new(); - let mut t_borderless = Table::new(2, 2, 400.0); - t_borderless.props.style = None; - for row in &mut t_borderless.rows { - for cell in &mut row.cells { - cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; - } - } - doc.body = vec![Arc::new(Block::Table(t_borderless))]; - let tex = export(&doc); - assert!(!tex.contains("\\hline")); - let back = import(tex.as_bytes()); - let Some(Block::Table(bt)) = back.body.first().map(|b| &**b) else { panic!() }; - assert!(bt.props.style.is_none(), "imported table should remain borderless"); - - // 2. Bordered table. - let mut doc_bordered = Document::new(); - let mut t_bordered = Table::new(2, 2, 400.0); - t_bordered.props.style = Some("TableGrid".to_string()); - for row in &mut t_bordered.rows { - for cell in &mut row.cells { - cell.blocks = vec![para_block(Paragraph::with_text("cell", CharProps::default()))]; - } - } - doc_bordered.body = vec![Arc::new(Block::Table(t_bordered))]; - let tex_bordered = export(&doc_bordered); - assert!(tex_bordered.contains("\\hline")); - let back_bordered = import(tex_bordered.as_bytes()); - let Some(Block::Table(bt_bordered)) = back_bordered.body.first().map(|b| &**b) else { panic!() }; - assert!(bt_bordered.props.style.is_some(), "imported table should remain bordered"); -} - -#[test] -fn verbatim_end_is_kept_out_after_control_characters_go() { - // A control character inside `\end{verbatim}` was removed after the check for it, so - // the code line closed the environment and the rest ran as TeX. - let mut d = Document::new(); - let line = "\\end{verb\u{E}atim}\\input{/etc/passwd}"; - d.body = vec![wordcraft_doc::para_block(wordcraft_doc::Paragraph::with_text(line, Default::default()).styled(model::CODE_STYLE))]; - let out = export(&d); - assert_eq!(out.matches("\\end{verbatim}").count(), 1, "{out}"); - assert!(out.contains("\\end {verbatim}\\input"), "{out}"); -} - -#[test] -fn nested_multicolumn_wrappers_are_bounded() { - // Each `\multicolumn` unwrapped rescanned the rest of the cell; nested ones were all - // unwrapped, quadratic in the nesting. - let n = 50_000; - let src = format!("\\begin{{tabular}}{{l}}{}x{}\\\\\\end{{tabular}}", "\\multicolumn{1}{c}{".repeat(n), "}".repeat(n)); - let t = std::time::Instant::now(); - let f = parse(&src); - // Under a second on a loaded machine; unwrapping every wrapper took 213 s. - assert!(t.elapsed() < std::time::Duration::from_secs(10), "{:?}", t.elapsed()); - assert!(f.blocks.iter().any(|b| matches!(b, FBlock::Table(_)))); - // One or two wrappers, as written in real tables, are still read. - let f = parse("\\begin{tabular}{ll}\\multicolumn{2}{c}{\\multirow{2}{*}{Both}}\\\\\\end{tabular}"); - let Some(FBlock::Table(t)) = f.blocks.first() else { panic!("{:?}", f.blocks) }; - assert_eq!((t.rows[0][0].colspan, t.rows[0][0].rowspan), (2, 2)); - assert_eq!(t.rows[0][0].text(), "Both"); -} - -#[test] -fn ligature_characters_split_across_runs_stay_apart() { - let d = crate::html::import( - "

    a--b ''x

    ", - ); - let tex = export(&d); - assert!(tex.contains("a-{}-b '{}'x"), "{tex}"); - let back = import(tex.as_bytes()); - assert_eq!(back.plain_text(Default::default()).trim(), "a--b ''x", "{tex}"); -} - -#[test] -fn a_tabular_row_keeps_the_cells_a_table_row_holds() { - // A 5,000-cell row with spans widened the multirow tracking every later row walks; the - // document keeps MAX_COLS cells a row, so the rest are left out while parsing. - let wide = vec!["\\multicolumn{63}{c}{x}"; 5000].join("&"); - let toks = tokenize(&format!("{wide}\\\\ a & b \\\\")); - let t = table::table(&toks, "", 0); - assert_eq!(t.rows.first().map(Vec::len), Some(wordcraft_doc::table::MAX_COLS)); - assert_eq!(t.rows.get(1).map(Vec::len), Some(2)); -} diff --git a/crates/formats/src/latex/units.rs b/crates/formats/src/latex/units.rs deleted file mode 100644 index c327cbdc88..0000000000 --- a/crates/formats/src/latex/units.rs +++ /dev/null @@ -1,185 +0,0 @@ -use super::Rgb; -use super::export::{LINE_WIDTH, TABCOLSEP}; -use super::limits::MAX_NEST; -use super::math::{balanced, closers}; - -/// Font size declarations (points, for the default 10 pt article). -pub(super) fn size_of(name: &str) -> Option { - Some(match name { - "tiny" => 5.0, - "scriptsize" => 7.0, - "footnotesize" => 8.0, - "small" => 9.0, - "normalsize" => 0.0, - "large" => 12.0, - "Large" => 14.4, - "LARGE" => 17.28, - "huge" => 20.74, - "Huge" => 24.88, - _ => return None, - }) -} - -/// A named colour (`xcolor`'s base names). -fn named_color(name: &str) -> Option { - Some(match name.trim() { - "black" => Rgb(0, 0, 0), - "white" => Rgb(255, 255, 255), - "red" => Rgb(255, 0, 0), - "green" => Rgb(0, 255, 0), - "blue" => Rgb(0, 0, 255), - "cyan" => Rgb(0, 255, 255), - "magenta" => Rgb(255, 0, 255), - "yellow" => Rgb(255, 255, 0), - "gray" => Rgb(128, 128, 128), - "darkgray" => Rgb(64, 64, 64), - "lightgray" => Rgb(191, 191, 191), - "brown" => Rgb(191, 128, 64), - "lime" => Rgb(191, 255, 0), - "olive" => Rgb(128, 128, 0), - "orange" => Rgb(255, 128, 0), - "pink" => Rgb(255, 191, 191), - "purple" => Rgb(191, 0, 64), - "teal" => Rgb(0, 128, 128), - "violet" => Rgb(128, 0, 128), - _ => return None, - }) -} - -/// `\textcolor[model]{spec}`: HTML, RGB (0–255) and rgb (0–1) models, or a name. -pub(super) fn parse_color(model: Option<&str>, spec: &str) -> Option { - let spec = spec.trim(); - match model.map(str::trim) { - Some("HTML") => crate::model::parse_color(&format!("#{spec}")), - Some("RGB") => { - let v: Vec = spec.split(',').filter_map(|x| x.trim().parse::().ok()).map(|x| x.clamp(0.0, 255.0) as u8).collect(); - match v.as_slice() { - [r, g, b] => Some(Rgb(*r, *g, *b)), - _ => None, - } - } - Some("rgb") => { - let v: Vec = spec - .split(',') - .filter_map(|x| x.trim().parse::().ok()) - .filter(|x| x.is_finite()) - .map(|x| (x.clamp(0.0, 1.0) * 255.0).round() as u8) - .collect(); - match v.as_slice() { - [r, g, b] => Some(Rgb(*r, *g, *b)), - _ => None, - } - } - Some("gray") => spec.parse::().ok().filter(|x| x.is_finite()).map(|x| { - let g = (x.clamp(0.0, 1.0) * 255.0).round() as u8; - Rgb(g, g, g) - }), - _ => { - // `red!50` (a tint) and `red!50!blue` (a mix) keep the first colour. - named_color(spec.split('!').next().unwrap_or(spec)) - } - } -} - -/// A TeX length in points (`3cm`, `0.4\linewidth`), if it is one. -pub(super) fn length_pt(s: &str) -> Option { - let s = s.trim().strip_prefix('+').unwrap_or(s.trim()); - let split = s.find(|c: char| !(c.is_ascii_digit() || c == '.' || c == '-')).unwrap_or(s.len()); - let (n, unit) = s.split_at(split); - let n: f32 = if n.is_empty() { 1.0 } else { n.parse().ok()? }; - let unit = unit.trim(); - // Macro names are case-sensitive (`\TEXTWIDTH` is not `\textwidth`); unit keywords are not. - let unit = if unit.starts_with('\\') { unit.to_owned() } else { unit.to_ascii_lowercase() }; - let k = match unit.as_str() { - "pt" => 1.0, - "bp" => 72.27 / 72.0, - "in" => 72.27, - "cm" => 28.45, - "mm" => 2.845, - "em" => 10.0, - "ex" => 4.3, - "pc" => 12.0, - "\\linewidth" | "\\textwidth" | "\\columnwidth" | "\\hsize" => LINE_WIDTH, - _ => return None, - }; - let v = n * k; - (v.is_finite() && v > 0.0 && v < 10_000.0).then_some(v) -} - -/// Column widths of a `tabular` column spec, when every column has one (`p{3cm}`). -pub(super) fn spec_widths(spec: &str) -> (usize, Vec) { - let (cols, widths) = spec_widths_at(spec, 0); - (cols.min(wordcraft_doc::table::MAX_COLS), if widths.len() == cols { widths } else { Vec::new() }) -} - -/// [`spec_widths`] at nesting `depth` of `*{n}{…}` repeats. The column count saturates and the -/// widths stop growing past [`wordcraft_doc::table::MAX_COLS`], so nested repeats can't overflow -/// or exhaust memory; the returned widths are empty unless every column has one. -fn spec_widths_at(spec: &str, depth: usize) -> (usize, Vec) { - let max_cols = wordcraft_doc::table::MAX_COLS; - let chars: Vec = spec.chars().collect(); - let braces = closers(&chars, '{', '}'); - let mut cols = 0usize; - let mut widths = Vec::new(); - let mut all = true; - let mut i = 0; - while let Some(&c) = chars.get(i) { - match c { - 'l' | 'c' | 'r' | 'X' | 'S' | 'L' | 'C' | 'R' | 'J' => { - cols = cols.saturating_add(1); - all = false; - } - 'p' | 'm' | 'b' => { - cols = cols.saturating_add(1); - match balanced(&chars, &braces, i + 1) { - Some((w, end)) => { - match length_pt(&w) { - Some(v) if widths.len() < max_cols => widths.push(v + 2.0 * TABCOLSEP), - Some(_) => {} - None => all = false, - } - i = end; - continue; - } - None => all = false, - } - } - '*' => { - // `*{3}{l}`: repeat. Past the nesting limit the repeat is skipped, unmeasured. - if let Some((n, end)) = balanced(&chars, &braces, i + 1) - && let Some((inner, end2)) = balanced(&chars, &braces, end) - { - if depth < MAX_NEST { - let n: usize = n.trim().parse().unwrap_or(1).min(64); - let (c2, w2) = spec_widths_at(&inner, depth + 1); - cols = cols.saturating_add(c2.saturating_mul(n)); - if w2.len() == c2 { - for _ in 0..n { - if widths.len() >= max_cols { - break; - } - widths.extend(w2.iter().take(max_cols - widths.len())); - } - } else { - all = false; - } - } else { - all = false; - } - i = end2; - continue; - } - } - '@' | '!' | '>' | '<' => { - // `@{…}`, `>{…}`: skip the argument. - if let Some((_, end)) = balanced(&chars, &braces, i + 1) { - i = end; - continue; - } - } - _ => {} - } - i += 1; - } - (cols, if all && widths.len() == cols { widths } else { Vec::new() }) -} diff --git a/crates/formats/src/markdown.rs b/crates/formats/src/markdown.rs index 5f9859074c..5666d43cda 100644 --- a/crates/formats/src/markdown.rs +++ b/crates/formats/src/markdown.rs @@ -1,40 +1,68 @@ -//! Markdown: a CommonMark subset plus GitHub tables and strikethrough. -//! -//! Import: ATX and setext headings, paragraphs with soft/hard breaks, block quotes (→ Quote -//! style), fenced and indented code (→ one Code paragraph per line), thematic breaks (→ a -//! bottom-border paragraph), bullet and ordered lists (nested by indentation), GFM pipe tables, -//! and inlines: emphasis/strong (the CommonMark delimiter algorithm), `~~strike~~`, code spans, -//! inline links, autolinks, images (`data:` URIs become pictures, others their alt text), -//! backslash escapes, entities, HTML comments (``), `
    ` and `` anchors. -//! -//! Export writes the same constructs; pictures become `![alt](media-name)` placeholders. +//! Markdown import and export via `craft-markdown`. -use std::collections::HashMap; -use std::sync::Arc; +use wordcraft_doc::{Block, Document, SectionProps}; -use wordcraft_doc::table::{MAX_COLS, MAX_ROWS}; -use wordcraft_doc::{Align, Document}; +pub use craft_markdown::{Refs, escape, parse, parse_inlines}; -use crate::html::decode_entity; -use crate::model::{self, Cell, FBlock, FTable, Fmt, Inline, Kind, ListInfo, Para, make_img}; +fn shared_section(page: &SectionProps) -> craft_wordmodel::SectionProps { + let mut d = Document::new(); + d.last_section = page.clone(); + wordcraft_docx::to_shared_model(&d).map(|s| s.last_section).unwrap_or_default() +} -mod blocks; -mod export; -mod inlines; +pub fn import(text: &str) -> Document { + wordcraft_docx::from_shared_model(craft_markdown::import(text)).unwrap_or_default() +} -/// Table cells a Markdown document makes in all, header rows included (hostile input). A short -/// row is padded to its header's width, so a 197 KB file of one-cell rows under a 63-column -/// header made 2 million cells and took 3 to 5 GB to convert; past this, rows are left out. -pub(crate) const MAX_TABLE_CELLS: usize = 1 << 18; +pub fn import_on(text: &str, page: &SectionProps) -> Document { + wordcraft_docx::from_shared_model(craft_markdown::import_on(text, &shared_section(page))).unwrap_or_default() +} -pub use blocks::{Refs, import, import_on, import_on_with_lines, parse, parse_with_lines}; -pub(crate) use export::escape; -pub use export::export; -#[cfg(test)] -use export::{escape_line_start, inlines_md, table_md, write_blocks}; -pub use inlines::parse_inlines; +/// [`import_on`] returning the document and the 0-based starting line of each top-level block. +pub fn import_on_with_lines(text: &str, page: &SectionProps) -> (Document, Vec) { + let doc = import_on(text, page); + let src_lines: Vec<&str> = text.strip_prefix('\u{FEFF}').unwrap_or(text).lines().collect(); + let mut out = Vec::with_capacity(doc.body.len()); + let mut cursor = 0usize; + for block in &doc.body { + let needle = match block.as_ref() { + Block::Para(p) => p.text.split_whitespace().next().unwrap_or(""), + Block::Table(t) => t + .rows + .first() + .and_then(|r| r.cells.first()) + .and_then(|c| c.blocks.first()) + .and_then(|b| match b.as_ref() { + Block::Para(p) => p.text.split_whitespace().next(), + _ => None, + }) + .unwrap_or(""), + }; + let mut found = cursor; + if !needle.is_empty() { + if let Some(rel) = src_lines[cursor..].iter().position(|l| l.contains(needle)) { + found = cursor + rel; + cursor = found + 1; + if matches!(block.as_ref(), Block::Table(_)) { + while src_lines.get(cursor).is_some_and(|l| l.trim_start().starts_with('|')) { + cursor += 1; + } + } + } + } else if cursor >= src_lines.len() { + found = src_lines.len(); + } else if let Some(rel) = src_lines[cursor..].iter().position(|l| !l.trim().is_empty()) { + found = cursor + rel; + cursor = found + 1; + } + out.push(found.min(src_lines.len())); + } + (doc, out) +} -#[cfg(test)] -use blocks::{remap_original_lines, split_row}; -#[cfg(test)] -mod tests; +pub fn export(doc: &Document) -> String { + match wordcraft_docx::to_shared_model(doc) { + Ok(shared) => craft_markdown::export(&shared), + Err(_) => String::new(), + } +} diff --git a/crates/formats/src/markdown/blocks.rs b/crates/formats/src/markdown/blocks.rs deleted file mode 100644 index 0aeb919dd8..0000000000 --- a/crates/formats/src/markdown/blocks.rs +++ /dev/null @@ -1,841 +0,0 @@ -use super::inlines::parse_inlines; -use super::{Align, Arc, Cell, Document, FBlock, FTable, Fmt, HashMap, Kind, ListInfo, MAX_COLS, MAX_ROWS, Para, model}; - -/// Nesting limit for block containers (quotes, list items) and inline links. -pub(super) const MAX_NEST: usize = 32; - -// --------------------------------------------------------------------------------------------- -// Import - -/// Parse Markdown text into a document. -pub fn import(text: &str) -> Document { - model::begin_import(); - model::to_doc(&parse(text)) -} - -/// [`import`] onto `page`: Markdown gives no page size (#1381). -pub fn import_on(text: &str, page: &wordcraft_doc::SectionProps) -> Document { - model::begin_import(); - model::to_doc_in(&parse(text), page.clone()) -} - -/// Link reference definitions: the normalised label to its destination. -/// A destination is shared by every reference to it, not copied into each (#3713). -pub type Refs = HashMap>; - -/// A label as CommonMark compares them: case-folded, and any run of white space one space -/// (§4.7, "matches"). `[FOO bar]` and `[foo BAR]` are the same label. -pub(super) fn ref_label(raw: &str) -> String { - raw.split_whitespace().collect::>().join(" ").to_lowercase() -} - -/// A link reference definition on its own line: `[label]: destination "optional title"`. The -/// destination may be wrapped in `<...>`. Returns the label and the destination. -/// -/// CommonMark allows the title on the following line and the label to span lines; a definition -/// written across lines is left as text here, which is what it was before these were read at all. -fn link_definition(line: &str) -> Option<(String, String)> { - let t = line.trim_start(); - // Up to three spaces of indentation; four makes it indented code. - if line.len() - t.len() > 3 || !t.starts_with('[') { - return None; - } - let bytes = t.as_bytes(); - // The label ends at the first unescaped `]`, and cannot be empty or hold an unescaped `[`. - let mut i = 1; - let mut close = None; - while i < bytes.len() { - match bytes[i] { - b'\\' => i += 1, - b'[' => return None, - b']' => { - close = Some(i); - break; - } - _ => {} - } - i += 1; - } - let close = close?; - let label = ref_label(t.get(1..close)?); - // `[^label]: ...` is a footnote definition, which this importer leaves as text. - if label.starts_with('^') { - return None; - } - if label.is_empty() || t.as_bytes().get(close + 1) != Some(&b':') { - return None; - } - let rest = t.get(close + 2..)?.trim_start(); - if rest.is_empty() { - return None; - } - // The destination: `<...>`, else up to the next space. What follows may only be a title. - let (dest, after) = match rest.strip_prefix('<') { - Some(inner) => { - let end = inner.find('>')?; - (inner.get(..end)?.to_string(), inner.get(end + 1..)?) - } - None => { - let end = rest.find(char::is_whitespace).unwrap_or(rest.len()); - (rest.get(..end)?.to_string(), rest.get(end..)?) - } - }; - if dest.is_empty() { - return None; - } - let title = after.trim(); - let titled = - title.is_empty() || (title.len() >= 2 && matches!((title.chars().next()?, title.chars().last()?), ('"', '"') | ('\'', '\'') | ('(', ')'))); - titled.then_some((label, dest)) -} - -/// Take the link reference definitions out of `lines`, leaving the rest. Lines inside a fenced -/// code block are left alone: a definition there is code, not a definition. Fences open and -/// close by the same rules [`parse_blocks`] applies, or a line it reads as code would be read -/// here as a definition and deleted. -/// -/// A definition has to start a block: CommonMark §4.7 forbids one from interrupting a paragraph, -/// and a line like `[Warning]: unstable` in the middle of one is the author's text. This pass -/// sees lines without the block structure around them, so it takes a definition only after a -/// blank line, at the start of the document, after a heading or thematic break, or after another -/// definition. A definition anywhere else stays in the document and reads as text, which is what -/// it did before definitions were understood at all; deleting a line that was part of a -/// paragraph would lose it silently. -/// -/// Returns the parsed references and the 0-based indexes of the removed lines. -fn take_definitions(lines: &mut Vec) -> (Refs, Vec) { - let mut refs = Refs::new(); - let mut open: Option<(char, usize)> = None; - // The start of the document begins a block. - let mut starts_block = true; - let mut removed = Vec::new(); - let mut at = 0usize; - lines.retain(|line| { - let this = at; - at += 1; - let t = line.trim_start_matches([' ', '\t']); - // Four spaces of indentation is code, not a fence (`parse_blocks`), on both sides. - let marker = (indent(line) < 4).then(|| fence(t)).flatten(); - match open { - Some((c, n)) => { - // A closing fence holds nothing but its own run of fence characters. - if marker.is_some_and(|(fc, fl)| fc == c && fl >= n) && t.trim_start_matches(c).trim().is_empty() { - open = None; - starts_block = true; - } - return true; - } - None => { - if let Some(f) = marker { - open = Some(f); - return true; - } - } - } - if starts_block && let Some((label, dest)) = link_definition(line) { - // The first definition of a label wins, as CommonMark says, and definitions may - // follow one another. - refs.entry(label).or_insert(dest.into()); - removed.push(this); - return false; - } - starts_block = is_blank(line) || atx_heading(t).is_some() || thematic_break(t); - true - }); - (refs, removed) -} - -/// Maps monotonically non-decreasing line indexes in lines filtered of link definitions back to -/// their original source lines in a single linear pass. -pub(super) fn remap_original_lines(block_lines: &mut [usize], removed: &[usize]) { - let mut rem_iter = removed.iter().copied().peekable(); - let mut shift = 0; - for l in block_lines.iter_mut() { - while let Some(&r) = rem_iter.peek() { - if r <= *l + shift { - shift += 1; - rem_iter.next(); - } else { - break; - } - } - *l += shift; - } -} - -/// Context for block parsing: depth, nesting, reference definitions, cell budget, and line recording. -struct BlockCtx<'a> { - depth: usize, - level: u8, - refs: &'a Refs, - budget: &'a std::cell::Cell, - lines_out: Option<&'a mut Vec>, -} - -impl BlockCtx<'_> { - /// Records the starting line of a top-level block if line tracking is active. - fn record_line(&mut self, line: usize) { - if self.depth == 0 - && let Some(lo) = self.lines_out.as_mut() - { - lo.push(line); - } - } - - /// Records `count` blocks originating at `line` if line tracking is active. - fn record_lines(&mut self, line: usize, count: usize) { - if self.depth == 0 - && let Some(lo) = self.lines_out.as_mut() - { - for _ in 0..count { - lo.push(line); - } - } - } -} - -/// Parse Markdown text into the flow model. -pub fn parse(text: &str) -> model::Flow { - parse_with_lines(text).0 -} - -/// [`parse`] returning the flow model and the 0-based starting line of each top-level block. -pub fn parse_with_lines(text: &str) -> (model::Flow, Vec) { - let text = text.strip_prefix('\u{FEFF}').unwrap_or(text); - let mut lines: Vec = text.split('\n').map(|l| expand_tabs(l.trim_end_matches('\r'))).collect(); - let (refs, removed) = take_definitions(&mut lines); - let budget = std::cell::Cell::new(super::MAX_TABLE_CELLS); - let mut block_lines = Vec::new(); - let mut ctx = BlockCtx { depth: 0, level: 0, refs: &refs, budget: &budget, lines_out: Some(&mut block_lines) }; - let mut blocks = parse_blocks(&lines, &mut ctx); - // A leading "Title" from front matter isn't supported; a first-level heading stays a heading. - if blocks.is_empty() { - blocks.push(FBlock::Para(Para::default())); - block_lines.push(0); - } - remap_original_lines(&mut block_lines, &removed); - (model::Flow { blocks, meta: Default::default() }, block_lines) -} - -/// [`import_on`] returning the document and the 0-based starting line of each top-level block. -pub fn import_on_with_lines(text: &str, page: &wordcraft_doc::SectionProps) -> (Document, Vec) { - model::begin_import(); - let (flow, mut lines) = parse_with_lines(text); - let doc = model::to_doc_in(&flow, page.clone()); - if doc.body.len() > lines.len() { - let end_line = text.as_bytes().iter().filter(|&&b| b == b'\n').count(); - lines.resize(doc.body.len(), end_line); - } - (doc, lines) -} - -/// Leading tabs → spaces (tab stops of 4); the rest of the line is kept. -fn expand_tabs(l: &str) -> String { - if !l.starts_with([' ', '\t']) || !l.contains('\t') { - return l.to_string(); - } - let mut out = String::with_capacity(l.len() + 8); - let mut col = 0usize; - let mut rest = l; - while let Some(c) = rest.chars().next() { - match c { - ' ' => { - out.push(' '); - col += 1; - } - '\t' => { - let n = 4 - col % 4; - out.push_str(&" ".repeat(n)); - col += n; - } - _ => break, - } - rest = &rest[c.len_utf8()..]; - } - out.push_str(rest); - out -} - -fn indent(l: &str) -> usize { - l.len() - l.trim_start_matches(' ').len() -} - -fn is_blank(l: &str) -> bool { - l.trim().is_empty() -} - -/// Strip up to `n` leading spaces. -fn strip_indent(l: &str, n: usize) -> &str { - let k = indent(l).min(n); - l.get(k..).unwrap_or("") -} - -fn atx_heading(t: &str) -> Option<(u8, &str)> { - let n = t.chars().take_while(|c| *c == '#').count(); - if !(1..=6).contains(&n) { - return None; - } - let rest = t.get(n..)?; - if !rest.is_empty() && !rest.starts_with([' ', '\t']) { - return None; - } - let mut body = rest.trim(); - // Optional closing sequence. - let trimmed = body.trim_end_matches('#'); - if trimmed.len() != body.len() && (trimmed.is_empty() || trimmed.ends_with(' ')) { - body = trimmed.trim_end(); - } - Some((n as u8, body)) -} - -pub(super) fn thematic_break(t: &str) -> bool { - let mut ch = None; - let mut n = 0; - for c in t.chars() { - match c { - ' ' | '\t' => {} - '*' | '-' | '_' => { - if ch.is_some_and(|x| x != c) { - return false; - } - ch = Some(c); - n += 1; - } - _ => return false, - } - } - n >= 3 -} - -fn fence(t: &str) -> Option<(char, usize)> { - let c = t.chars().next().filter(|c| *c == '`' || *c == '~')?; - let n = t.chars().take_while(|x| *x == c).count(); - if n < 3 { - return None; - } - if c == '`' && t.get(n..).is_some_and(|info| info.contains('`')) { - return None; - } - Some((c, n)) -} - -#[derive(Clone, Copy, PartialEq)] -struct Marker { - ordered: bool, - /// Bullet char or ordered delimiter. - ch: char, - /// Bytes of the marker itself (after the indent). - len: usize, - start: u32, -} - -fn list_marker(t: &str) -> Option { - let first = t.chars().next()?; - let (m, len) = if matches!(first, '-' | '+' | '*') { - (Marker { ordered: false, ch: first, len: 1, start: 1 }, 1) - } else { - let digits = t.chars().take_while(char::is_ascii_digit).count(); - if !(1..=9).contains(&digits) { - return None; - } - let d = t.get(digits..)?.chars().next()?; - if d != '.' && d != ')' { - return None; - } - let start = t.get(..digits)?.parse::().unwrap_or(1); - (Marker { ordered: true, ch: d, len: digits + 1, start }, digits + 1) - }; - let rest = t.get(len..)?; - if rest.is_empty() || rest.starts_with([' ', '\t']) { Some(m) } else { None } -} - -/// A table row without its leading and trailing pipes (an escaped last pipe stays). -fn row_body(l: &str) -> &str { - let t = l.trim(); - let t = t.strip_prefix('|').unwrap_or(t); - if let Some(before) = t.strip_suffix('|') { - let num_bs = before.chars().rev().take_while(|&c| c == '\\').count(); - if num_bs % 2 == 0 { before } else { t } - } else { - t - } -} - -/// The first `keep` cells of a table row, and how many cells it has: the rest are only counted. -pub(super) fn split_row(l: &str, keep: usize) -> (Vec, usize) { - let mut cells = Vec::new(); - let mut count = 0usize; - let mut cur = String::new(); - let mut chars = row_body(l).chars().peekable(); - while let Some(c) = chars.next() { - let full = cells.len() >= keep; - match c { - '\\' => match chars.peek() { - Some(&'|') => { - if !full { - cur.push('|'); - } - chars.next(); - } - Some(&'\\') => { - if !full { - cur.push_str("\\\\"); - } - chars.next(); - } - _ if !full => cur.push('\\'), - _ => {} - }, - '|' => { - if !full { - cells.push(std::mem::take(&mut cur).trim().to_string()); - } - count += 1; - } - _ if !full => cur.push(c), - _ => {} - } - } - if cells.len() < keep { - cells.push(cur.trim().to_string()); - } - (cells, count + 1) -} - -/// The alignments of the first `keep` cells of a delimiter row, and how many cells it has; every -/// cell is checked, so a bad one past `keep` still means it isn't a delimiter row. -fn delimiter_row(l: &str, keep: usize) -> Option<(Vec>, usize)> { - if !l.contains('-') { - return None; - } - let mut out = Vec::new(); - let mut count = 0usize; - // A valid cell is only dashes, colons and spaces, so an escaped pipe can't be in one. - for c in row_body(l).split('|') { - let c = c.trim(); - let left = c.starts_with(':'); - let right = c.ends_with(':'); - let core = c.trim_matches(':'); - if core.is_empty() || !core.chars().all(|x| x == '-') { - return None; - } - if out.len() < keep { - out.push(match (left, right) { - (true, true) => Some(Align::Center), - (false, true) => Some(Align::Right), - (true, false) => Some(Align::Left), - _ => None, - }); - } - count += 1; - } - Some((out, count)) -} - -/// Does this line start a block that interrupts a paragraph? -fn interrupts(l: &str) -> bool { - if indent(l) >= 4 { - return false; - } - let t = l.trim_start_matches([' ', '\t']); - atx_heading(t).is_some() - || thematic_break(t) - || fence(t).is_some() - || t.starts_with('>') - || t.starts_with("") { - break; - } - } - continue; - } - if t.starts_with('>') { - flush_para(&mut para, &mut para_start, &mut out, ctx); - let quote_start = i; - let before = out.len(); - parse_quote(lines, &mut i, ctx, &mut out); - ctx.record_lines(quote_start, out.len() - before); - continue; - } - if para.is_empty() && t.contains('|') && parse_table(lines, &mut i, t, ctx, &mut out) { - continue; - } - if let Some(m) = list_marker(t) - && (para.is_empty() || ((!m.ordered || m.start == 1) && !is_blank(t.get(m.len..).unwrap_or("")))) - { - flush_para(&mut para, &mut para_start, &mut out, ctx); - i = parse_list(lines, i, ctx, &mut out); - continue; - } - if para_start.is_none() { - para_start = Some(i); - } - para.push(line.clone()); - i += 1; - } - flush_para(&mut para, &mut para_start, &mut out, ctx); - out -} - -/// Parses indented code block starting at `lines[*i]`. -fn parse_indented_code(lines: &[String], i: &mut usize, out: &mut Vec, ctx: &mut BlockCtx<'_>) { - let mut code: Vec<(usize, &str)> = Vec::new(); - while let Some(line) = lines.get(*i) { - let cur = *i; - if is_blank(line) { - code.push((cur, "")); - } else if indent(line) >= 4 { - code.push((cur, strip_indent(line, 4))); - } else { - break; - } - *i += 1; - } - while code.last().is_some_and(|(_, line)| line.is_empty()) { - code.pop(); - } - for (line_idx, text) in code { - out.push(code_para(text)); - ctx.record_line(line_idx); - } -} - -/// Parses block quote starting at `lines[*i]`. -fn parse_quote(lines: &[String], i: &mut usize, ctx: &BlockCtx<'_>, out: &mut Vec) { - let mut inner = Vec::new(); - while let Some(line) = lines.get(*i) { - let trimmed = line.trim_start_matches([' ', '\t']); - if indent(line) < 4 && trimmed.starts_with('>') { - let content = &trimmed[1..]; - inner.push(content.strip_prefix(' ').unwrap_or(content).to_string()); - } else if !is_blank(line) && !interrupts(line) && inner.last().is_some_and(|line| !is_blank(line)) { - inner.push(line.clone()); - } else { - break; - } - *i += 1; - } - if ctx.depth >= MAX_NEST { - let mut p = Para::new(Kind::Quote); - p.push_text(&inner.join(" "), &Fmt::default()); - out.push(FBlock::Para(p)); - return; - } - let mut child = BlockCtx { depth: ctx.depth + 1, level: ctx.level, refs: ctx.refs, budget: ctx.budget, lines_out: None }; - for mut block in parse_blocks(&inner, &mut child) { - if let FBlock::Para(p) = &mut block - && p.kind == Kind::Normal - { - p.kind = Kind::Quote; - } - out.push(block); - } -} - -/// Computes columns and byte count for spaces following a list item marker. -fn marker_space(ind: usize, marker_len: usize, after: &str) -> (usize, usize) { - let mut cols = 0; - let mut bytes = 0; - let mut rest = after; - while let Some(c) = rest.chars().next() { - match c { - ' ' => { - cols += 1; - bytes += 1; - rest = &rest[1..]; - if cols >= 4 { - break; - } - } - '\t' => { - let col = ind + marker_len + cols; - let n = 4 - col % 4; - cols += n; - bytes += 1; - break; - } - _ => break, - } - } - (cols, bytes) -} - -/// Parse a list starting at `lines[i]`; returns the index after it. -fn parse_list(lines: &[String], mut i: usize, ctx: &mut BlockCtx<'_>, out: &mut Vec) -> usize { - let Some(first) = lines.get(i).and_then(|l| list_marker(l.trim_start_matches([' ', '\t']))) else { return i + 1 }; - while let Some(line) = lines.get(i) { - let ind = indent(line); - if ind >= 4 { - break; - } - let t = line.trim_start_matches([' ', '\t']); - let Some(m) = list_marker(t) else { break }; - if m.ordered != first.ordered || m.ch != first.ch { - break; - } - let item_start = i; - let after = t.get(m.len..).unwrap_or(""); - let (spaces, skip) = if indent(after) > 4 { - (1, 1) - } else if after.starts_with([' ', '\t']) { - marker_space(ind, m.len, after) - } else { - (0, 0) - }; - let content_indent = if is_blank(after) { ind + m.len + 1 } else { ind + m.len + spaces }; - let item0 = if is_blank(after) { String::new() } else { after.get(skip.min(after.len())..).unwrap_or("").to_string() }; - let mut item: Vec = vec![item0]; - i += 1; - while let Some(l) = lines.get(i) { - if is_blank(l) { - item.push(String::new()); - } else if indent(l) >= content_indent { - item.push(strip_indent(l, content_indent).to_string()); - } else if item.last().is_some_and(|x| !is_blank(x)) && !interrupts(l) && list_marker(l.trim_start_matches([' ', '\t'])).is_none() { - // Lazy continuation of the item's paragraph. - item.push(l.trim_start_matches([' ', '\t']).to_string()); - } else { - break; - } - i += 1; - } - // Blank lines at the end of an item belong between items. - while item.len() > 1 && item.last().is_some_and(String::is_empty) { - item.pop(); - } - let li = ListInfo { ordered: first.ordered, level: ctx.level.min(8), start: first.start, ..Default::default() }; - let inner = if ctx.depth >= MAX_NEST { - let mut p = Para::default(); - p.push_text(&item.join(" "), &Fmt::default()); - vec![FBlock::Para(p)] - } else { - let mut child = - BlockCtx { depth: ctx.depth + 1, level: ctx.level.saturating_add(1).min(8), refs: ctx.refs, budget: ctx.budget, lines_out: None }; - parse_blocks(&item, &mut child) - }; - let before_count = out.len(); - let mut placed = false; - if inner.is_empty() { - out.push(FBlock::Para(Para { list: Some(li), ..Default::default() })); - placed = true; - } - for b in inner { - match b { - FBlock::Para(mut p) if p.list.is_none() => { - if !placed { - p.list = Some(li); - placed = true; - out.push(FBlock::Para(p)); - } else if let Some(FBlock::Para(prev)) = out.last_mut() - && prev.list.is_some_and(|x| x.level == li.level) - { - // A continuation paragraph joins the item with a line break. - prev.push_text("\n", &Fmt::default()); - prev.inlines.extend(p.inlines); - } else { - p.list = Some(li); - out.push(FBlock::Para(p)); - } - } - FBlock::Para(p) => { - if !placed { - // The item starts with a nested list: give it an empty first line. - out.push(FBlock::Para(Para { list: Some(li), ..Default::default() })); - placed = true; - } - out.push(FBlock::Para(p)); - } - other => { - placed = true; - out.push(other); - } - } - } - ctx.record_lines(item_start, out.len() - before_count); - // Blank lines between items. - while lines.get(i).is_some_and(|l| is_blank(l)) { - let next_is_item = lines.get(i + 1).is_some_and(|l| { - indent(l) < 4 && list_marker(l.trim_start_matches([' ', '\t'])).is_some_and(|m| m.ordered == first.ordered && m.ch == first.ch) - }); - if !next_is_item { - return i; - } - i += 1; - } - } - i -} diff --git a/crates/formats/src/markdown/export.rs b/crates/formats/src/markdown/export.rs deleted file mode 100644 index 08f2c782a2..0000000000 --- a/crates/formats/src/markdown/export.rs +++ /dev/null @@ -1,448 +0,0 @@ -use super::blocks::thematic_break; -use super::inlines::{is_punct, within}; -use super::{Align, Arc, Cell, Document, FBlock, FTable, Fmt, Inline, Kind, model}; - -// --------------------------------------------------------------------------------------------- -// Export - -/// Escape text for Markdown (inline context). -pub(crate) fn escape(s: &str) -> String { - let mut out = String::with_capacity(s.len() + 8); - for c in s.chars() { - match c { - '\\' | '`' | '*' | '_' | '[' | ']' | '<' | '>' | '~' | '|' | '&' => { - out.push('\\'); - out.push(c); - } - '\u{000C}' | '\u{000E}' => {} - _ => out.push(c), - } - } - out -} - -/// Escape what would start a block at the beginning of a line. Leading spaces are kept, but at -/// most three (a tab counts as four): four would turn the line into an indented code block. -pub(super) fn escape_line_start(s: String) -> String { - let t = s.trim_start_matches([' ', '\t']); - let lead = s.get(..s.len() - t.len()).unwrap_or(""); - let cols: usize = lead.chars().map(|c| if c == '\t' { 4 } else { 1 }).sum(); - let indent = " ".repeat(cols.min(3)); - let needs = t.starts_with('#') - || t.starts_with('>') - || t.starts_with("- ") - || t.starts_with("+ ") - || t.starts_with("-\t") - || t.starts_with("+\t") - || t == "-" - || t == "+" - || t.starts_with('=') - || (t.starts_with('-') && t.trim_end_matches([' ', '\t']).chars().all(|c| c == '-')) - || thematic_break(t) - || { - let d = t.chars().take_while(char::is_ascii_digit).count(); - d > 0 - && t.get(d..).is_some_and(|r| { - r.starts_with(". ") || r.starts_with(") ") || r.starts_with(".\t") || r.starts_with(")\t") || r == "." || r == ")" - }) - }; - if !needs { - return if cols > 3 || lead.contains('\t') { format!("{indent}{t}") } else { s }; - } - if let Some(d) = t.find(['.', ')']).filter(|_| t.starts_with(|c: char| c.is_ascii_digit())) { - return format!("{indent}{}\\{}", &t[..d], &t[d..]); - } - format!("{indent}\\{t}") -} - -/// [`escape_line_start`] for every line of a body, including those after a manual line break. -fn escape_lines(body: String) -> String { - if !body.contains("\\\n") { - return escape_line_start(body); - } - body.split("\\\n").map(|l| escape_line_start(l.to_string())).collect::>().join("\\\n") -} - -fn code_span(t: &str) -> String { - let t = t.replace("\r\n", " ").replace(['\r', '\n'], " "); - let mut longest = 0; - let mut cur = 0; - for c in t.chars() { - if c == '`' { - cur += 1; - longest = longest.max(cur); - } else { - cur = 0; - } - } - let ticks = "`".repeat(longest + 1); - let pad = if t.starts_with('`') || t.ends_with('`') || (t.starts_with(' ') && t.ends_with(' ') && !t.trim().is_empty()) { " " } else { "" }; - format!("{ticks}{pad}{t}{pad}{ticks}") -} - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] -enum Mark { - Bold, - Italic, - Strike, -} - -/// Inline spans → Markdown with properly nested markers. -pub(super) fn inlines_md(inlines: &[Inline]) -> String { - inlines_md_impl(inlines, false) -} - -fn inlines_md_table(inlines: &[Inline]) -> String { - inlines_md_impl(inlines, true) -} - -fn inlines_md_impl(inlines: &[Inline], in_table: bool) -> String { - // Split into (text, fmt) pieces with whitespace moved outside emphasis. - let mut pieces: Vec<(String, Fmt, Option)> = Vec::new(); - let eq = crate::model::equations_as_text(inlines); - // Markdown keeps no ruby: its base text goes through the text path below. - let inlines = crate::model::ruby_as_text(&eq); - for i in inlines.iter() { - match i { - Inline::Text(t, f) => { - let has_emph = f.bold || f.italic || f.strike; - if has_emph && !f.code { - let core = t.trim_matches([' ', '\t', '\n']); - let lead = &t[..t.len() - t.trim_start_matches([' ', '\t', '\n']).len()]; - let trail = &t[t.trim_end_matches([' ', '\t', '\n']).len()..]; - let plain = Fmt { bold: false, italic: false, strike: false, ..f.clone() }; - if !lead.is_empty() { - pieces.push((lead.to_string(), plain.clone(), None)); - } - if !core.is_empty() { - pieces.push((core.to_string(), f.clone(), None)); - } - if !trail.is_empty() && !core.is_empty() { - pieces.push((trail.to_string(), plain, None)); - } - } else { - pieces.push((t.clone(), f.clone(), None)); - } - } - Inline::Image(img) => { - let name = format!("image.{}", img.ext); - pieces.push((String::new(), Fmt::default(), Some(format!("![{}]({})", escape(&img.alt), name)))); - } - Inline::Anchor(a) => { - let safe: String = a.chars().filter(|c| c.is_alphanumeric() || matches!(c, '_' | '-' | '.' | ':')).collect(); - pieces.push((String::new(), Fmt::default(), Some(format!("")))); - } - Inline::Figure(alt) | Inline::Chart(crate::model::Chart { alt, .. }) => { - pieces.push((String::new(), Fmt::default(), Some(format!("![{}]()", escape(alt))))) - } - Inline::Equation { .. } => {} - // ASK and SET show nothing (see `model::shows_nothing`). - Inline::Field { instr, .. } if model::shows_nothing(instr) => {} - Inline::Field { result, fmt, .. } | Inline::Control { text: result, fmt, .. } => pieces.push((result.clone(), fmt.clone(), None)), - Inline::Note { .. } - | Inline::Comment { .. } - | Inline::CommentEnd(_) - | Inline::Shape { .. } - | Inline::Group { .. } - | Inline::FieldStart(_) - | Inline::FieldEnd - | Inline::AnchorEnd(_) => {} - // Read as its base text by `ruby_as_text` above. - Inline::Ruby { .. } => {} - } - } - let mut out = String::new(); - let mut stack: Vec = Vec::new(); - let mut link: Option> = None; - let close_all = |out: &mut String, stack: &mut Vec| { - while let Some(m) = stack.pop() { - out.push_str(marker(m)); - } - }; - for (text, f, raw) in pieces { - // Links are the outermost construct; a script or other non-web target is plain text. - let f_link = f.link.clone().filter(|l| wordcraft_doc::link::is_safe_href(l)); - if f_link != link || raw.is_some() { - close_all(&mut out, &mut stack); - if let Some(l) = &link { - out.push_str(&format!("]({})", link_target(l))); - } - link = None; - if let Some(r) = raw { - out.push_str(&r); - continue; - } - if let Some(l) = f_link { - out.push('['); - link = Some(l); - } - } - let want: Vec = - [(f.bold, Mark::Bold), (f.italic, Mark::Italic), (f.strike, Mark::Strike)].iter().filter(|x| x.0).map(|x| x.1).collect(); - let body = if f.code { - if in_table { code_span(&text.replace('|', "\\|")) } else { code_span(&text) } - } else { - escape(&text).replace('\n', "\\\n").replace('\t', " ") - }; - // Close until the stack is a prefix of what we want. - let mut close = String::new(); - while let Some(pos) = stack.iter().position(|m| !want.contains(m)) { - while stack.len() > pos { - if let Some(m) = stack.pop() { - close.push_str(marker(m)); - } - } - } - let mut open = String::new(); - for m in want { - if !stack.contains(&m) { - open.push_str(marker(m)); - stack.push(m); - } - } - // CommonMark only reads a closing run after punctuation as closing when whitespace or - // punctuation follows it, and an opening run before punctuation as opening when whitespace - // or punctuation precedes it. Where a letter sits there instead, or an opening run would - // directly follow a closing run (the two would merge into one run), an empty HTML comment (which readers drop) supplies - // the punctuation. - let prev = out.chars().next_back(); - let next = body.chars().next(); - let word = |c: Option| c.is_some_and(|c| !c.is_whitespace() && !is_punct(c)); - out.push_str(&close); - let close_needs = !close.is_empty() && prev.is_some_and(is_punct) && (!open.is_empty() || word(next)); - let open_needs = !open.is_empty() && next.is_some_and(is_punct) && (!close.is_empty() || word(prev)); - if close_needs || open_needs { - out.push_str(EMPTY_COMMENT); - } - out.push_str(&open); - out.push_str(&body); - } - close_all(&mut out, &mut stack); - if let Some(l) = &link { - out.push_str(&format!("]({})", link_target(l))); - } - out -} - -/// `l` as a Markdown link destination that reads back as exactly `l`, the target -/// `is_safe_href` passed. What Markdown would decode (a backslash escape, an entity reference) -/// or end the destination on (`<`, `>`) is backslash-escaped, never deleted: deleting `<` or `>` -/// could turn an accepted relative target into `javascript:…` or `//host`. Control characters -/// can't be written in a destination and are left out, as the safety check ignores them. -fn link_target(l: &str) -> String { - let l: String = l.chars().filter(|c| !c.is_ascii_control()).collect(); - let mut out = String::with_capacity(l.len() + 2); - for (i, c) in l.char_indices() { - let rest = l.get(i + c.len_utf8()..).unwrap_or(""); - match c { - '<' | '>' => out.push('\\'), - // A backslash is literal unless punctuation (or the closing `)`/`>`) follows it. - '\\' if rest.chars().next().is_none_or(|n| n.is_ascii_punctuation()) => out.push('\\'), - '&' if entity_like(rest) => out.push('\\'), - _ => {} - } - out.push(c); - } - if l.contains(|c: char| c.is_whitespace() || matches!(c, '(' | ')' | '<' | '>')) { format!("<{out}>") } else { out } -} - -/// Whether `rest` (the text after a `&`) starts like an entity reference (`amp;`, `#106;`, -/// `#x6A;`), which a Markdown reader would decode. -fn entity_like(rest: &str) -> bool { - let Some(end) = within(rest, 34).find(';') else { return false }; - let body = rest.get(..end).unwrap_or(""); - let body = body.strip_prefix('#').map_or(body, |n| n.strip_prefix(['x', 'X']).unwrap_or(n)); - !body.is_empty() && body.chars().all(|c| c.is_ascii_alphanumeric()) -} - -/// Inline HTML that Markdown readers drop, used to separate emphasis markers from letters. -const EMPTY_COMMENT: &str = ""; - -fn marker(m: Mark) -> &'static str { - match m { - Mark::Bold => "**", - Mark::Italic => "*", - Mark::Strike => "~~", - } -} - -pub(super) fn table_md(t: &FTable, out: &mut Vec) { - let cols = t.cols().max(1); - let cell_md = |c: &Cell| -> String { - let mut parts = Vec::new(); - for b in &c.blocks { - match b { - FBlock::Para(p) => parts.push(inlines_md_table(&p.inlines).replace("\\\n", "
    ")), - FBlock::Table(inner) => parts.push(escape(&inner.rows.iter().flat_map(|r| r.iter().map(Cell::text)).collect::>().join(" "))), - } - } - parts.join("
    ").replace('\n', " ") - }; - let align_of = |c: &Cell| c.blocks.iter().find_map(|b| if let FBlock::Para(p) = b { Some(p.align) } else { None }).flatten(); - let row_md = |r: &Vec| -> Vec { - let mut v: Vec = Vec::new(); - for c in r { - v.push(if c.covered { String::new() } else { cell_md(c) }); - for _ in 1..c.colspan.clamp(1, 63) { - v.push(String::new()); - } - } - v.resize(cols, String::new()); - v - }; - let Some(head) = t.rows.first() else { return }; - out.push(format!("| {} |", row_md(head).join(" | "))); - let mut aligns: Vec> = Vec::new(); - for c in head { - aligns.push(align_of(c)); - for _ in 1..c.colspan.clamp(1, 63) { - aligns.push(None); - } - } - aligns.resize(cols, None); - let delim: Vec<&str> = aligns - .iter() - .map(|a| match a { - Some(Align::Center) => ":---:", - Some(Align::Right) => "---:", - Some(Align::Left) => ":---", - _ => "---", - }) - .collect(); - out.push(format!("| {} |", delim.join(" | "))); - for r in t.rows.iter().skip(1) { - out.push(format!("| {} |", row_md(r).join(" | "))); - } -} - -/// Markdown text of a document. -pub fn export(doc: &Document) -> String { - let flow = model::from_doc(doc); - let mut out: Vec = Vec::new(); - write_blocks(&flow.blocks, &mut out); - let mut s = out.join("\n"); - while s.ends_with("\n\n") { - s.pop(); - } - if !s.ends_with('\n') { - s.push('\n'); - } - s -} - -/// A heading's text on one line, with a trailing run of '#' escaped so a reader does not take it -/// for the optional closing sequence (`## Item #` reads back as "Item"). -fn heading_text(body: &str) -> String { - let mut s = body.replace("\\\n", " "); - let t = s.trim_end(); - let core = t.trim_end_matches('#'); - if core.len() != t.len() && (core.is_empty() || core.ends_with([' ', '\t'])) { - s.insert(core.len(), '\\'); - } - s -} - -pub(super) fn write_blocks(blocks: &[FBlock], out: &mut Vec) { - let mut i = 0; - let mut counters = [0u32; 9]; - let mut prev_list = false; - let mut prev_top_ordered = false; - while let Some(b) = blocks.get(i) { - match b { - FBlock::Table(t) => { - table_md(t, out); - out.push(String::new()); - prev_list = false; - } - FBlock::Para(p) if p.kind == Kind::Code && p.list.is_none() => { - let mut lines = Vec::new(); - while let Some(FBlock::Para(q)) = blocks.get(i) { - if q.kind != Kind::Code || q.list.is_some() { - break; - } - lines.push(q.text()); - i += 1; - } - let longest = lines.iter().map(|l| l.chars().filter(|c| *c == '`').count()).max().unwrap_or(0); - // The fence must be longer than the longest run in the content, or a content - // line is read back as the closing fence (the `.min(64)` cap did exactly that). - let fence = "`".repeat(3.max(longest + 1)); - out.push(fence.clone()); - out.extend(lines); - out.push(fence); - out.push(String::new()); - prev_list = false; - continue; - } - FBlock::Para(p) => { - let body = inlines_md(&p.inlines); - match p.list { - Some(li) => { - let lv = li.level.min(8) as usize; - if !prev_list || (lv == 0 && prev_top_ordered != li.ordered) { - counters = [0; 9]; - } - if lv == 0 { - prev_top_ordered = li.ordered; - } - let n = counters.get_mut(lv).map(|c| { - if *c == 0 { - *c = li.start.saturating_sub(1); - } - *c = c.saturating_add(1); - *c - }); - for d in counters.iter_mut().skip(lv + 1) { - *d = 0; - } - let marker = if li.ordered { format!("{}.", n.unwrap_or(1)) } else { "-".to_string() }; - let pad = " ".repeat(4 * lv); - let cont = " ".repeat(4 * lv + marker.len() + 1); - let body = if p.kind == Kind::Code { - code_span(&p.text()) - } else { - let body = escape_lines(body).replace("\\\n", &format!("\\\n{cont}")); - // The bullet marker can turn a short run of hyphens into a rule. - if thematic_break(&format!("{marker} {body}")) { format!("\\{body}") } else { body } - }; - let line = format!("{pad}{marker} {body}").trim_end().to_string(); - // A list inside a block quote stays in it: every line of the item, its - // continuation lines included, takes the quote marker. - let line = if p.kind == Kind::Quote { format!("> {}", line.replace('\n', "\n> ")) } else { line }; - out.push(line); - prev_list = true; - // A list going into or out of a block quote is a new block: without the - // blank line, an unquoted item would be read back into the quote. - let quoted = |k: Kind| k == Kind::Quote; - let next_is_list = matches!(blocks.get(i + 1), Some(FBlock::Para(q)) if q.list.is_some() && quoted(q.kind) == quoted(p.kind)); - if !next_is_list { - out.push(String::new()); - } - } - None => { - prev_list = false; - let line = match p.kind { - Kind::Heading(n) => format!("{} {}", "#".repeat(n.clamp(1, 6) as usize), heading_text(&body)), - Kind::Title => format!("# {}", heading_text(&body)), - Kind::Rule => "---".to_string(), - Kind::Quote => { - let b = escape_lines(body); - format!("> {}", b.replace("\\\n", "\\\n> ")) - } - _ => { - if body.is_empty() { - i += 1; - continue; - } - escape_lines(body) - } - }; - out.push(line); - out.push(String::new()); - } - } - } - } - i += 1; - } -} diff --git a/crates/formats/src/markdown/inlines.rs b/crates/formats/src/markdown/inlines.rs deleted file mode 100644 index 99c3972463..0000000000 --- a/crates/formats/src/markdown/inlines.rs +++ /dev/null @@ -1,595 +0,0 @@ -use super::blocks::{MAX_NEST, Refs, ref_label}; -use super::{Arc, Fmt, HashMap, Inline, decode_entity, make_img, model}; - -// ---- inlines ---- - -#[derive(Clone, Copy, PartialEq, Eq)] -enum Emph { - Em, - Strong, - Strike, -} - -enum Node { - Text(String, Fmt), - Inl(Inline), - Delim { ch: char, orig: usize, left: usize, open: bool, close: bool, opens: Vec, closes: Vec }, -} - -pub(super) fn is_punct(c: char) -> bool { - c.is_ascii_punctuation() || (!c.is_alphanumeric() && !c.is_whitespace() && !c.is_control()) -} - -/// Parse inline Markdown into formatted spans (on top of `base`). -pub fn parse_inlines(s: &str, base: &Fmt, depth: usize, refs: &Refs) -> Vec { - let nodes = tokenize(s, base, depth, refs); - emit(nodes) -} - -fn push_text(nodes: &mut Vec, s: &str, f: &Fmt) { - if s.is_empty() { - return; - } - if let Some(Node::Text(t, pf)) = nodes.last_mut() - && pf == f - { - t.push_str(s); - return; - } - nodes.push(Node::Text(s.to_string(), f.clone())); -} - -/// The closing `]` matching the `[` at byte `open` (escapes and code spans respected). Gives up -/// after looking `max` bytes ahead. Records in `known` every bracket pair it passes, and every `[` -/// still open at the end of the text: a scan from one of those would take the same path. -fn matching_bracket(s: &str, open: usize, max: usize, ticks: &Backticks, known: &mut HashMap>) -> Option { - let b = s.as_bytes(); - let mut opens: Vec = Vec::new(); - let mut i = open; - while let Some(&c) = b.get(i) { - if i - open > max { - return None; - } - match c { - b'\\' => i += 1, - b'`' => { - let n = b.get(i..).map(|r| r.iter().take_while(|x| **x == b'`').count()).unwrap_or(1); - if let Some(end) = ticks.find(i + n, n) { - i = end + n - 1; - } else { - i += n - 1; - } - } - b'[' => opens.push(i), - b']' => { - if let Some(o) = opens.pop() { - known.insert(o, Some(i)); - } - if opens.is_empty() { - return Some(i); - } - } - _ => {} - } - i += 1; - } - for o in opens { - known.insert(o, None); - } - None -} - -/// Where a text's runs of backticks start, by run length. Finding the run that closes a code span -/// is then a lookup: scanning the rest of the text for it, again for every run nothing closes -/// (each of a different length) and from every link bracket over them, was superlinear. -struct Backticks(HashMap>); - -impl Backticks { - fn new(s: &str) -> Self { - let b = s.as_bytes(); - let mut runs: HashMap> = HashMap::new(); - let mut i = 0; - while let Some(&c) = b.get(i) { - if c == b'`' { - let k = b.get(i..).map(|r| r.iter().take_while(|x| **x == b'`').count()).unwrap_or(1); - runs.entry(k).or_default().push(i); - i += k; - } else { - i += 1; - } - } - Self(runs) - } - - /// Byte index of a run of exactly `n` backticks at or after `from` (which is not inside a run). - fn find(&self, from: usize, n: usize) -> Option { - let starts = self.0.get(&n)?; - starts.get(starts.partition_point(|&p| p < from)).copied() - } -} - -struct LinkScan<'a> { - text: &'a str, - refs: &'a Refs, - ticks: &'a Backticks, - known: HashMap>, - budget: usize, -} - -impl<'a> LinkScan<'a> { - fn parse_at(&mut self, open: usize) -> Option<(&'a str, Arc, usize)> { - let bytes = self.text.as_bytes(); - let close = match self.known.get(&open).copied() { - Some(found) => found, - None if self.budget > 0 => { - let found = matching_bracket(self.text, open, self.budget, self.ticks, &mut self.known); - self.budget = self.budget.saturating_sub(found.unwrap_or(self.text.len()).saturating_sub(open)); - found - } - None => None, - }?; - if self.budget == 0 { - return None; - } - let label_text = self.text.get(open + 1..close)?; - if bytes.get(close + 1) == Some(&b'(') { - let destination = link_dest(self.text, close + 1); - self.budget = self.budget.saturating_sub(destination.as_ref().map_or(self.text.len(), |d| d.1).saturating_sub(close)); - let (destination, end) = destination?; - return Some((label_text, Arc::from(destination), end)); - } - // Full and shortcut reference links use definitions collected before inline parsing. - if self.refs.is_empty() { - return None; - } - let (label, end) = match self.text.get(close + 1..).and_then(|rest| rest.strip_prefix('[')) { - Some(rest) => { - let ahead = within(rest, self.budget); - let shut = ahead.find(']'); - self.budget = self.budget.saturating_sub(shut.map_or(ahead.len(), |n| n + 1)); - let shut = shut?; - let inner = rest.get(..shut)?; - let label = if inner.trim().is_empty() { label_text } else { inner }; - (label, close + 2 + shut + 1) - } - None => (label_text, close + 1), - }; - // A cached bracket pair still costs a pass over its label. Without charging it, - // nested undefined references normalize every suffix again. - if label.len() > self.budget { - self.budget = 0; - return None; - } - self.budget -= label.len(); - Some((label_text, self.refs.get(&ref_label(label))?.clone(), end)) - } -} - -/// Parse `(dest "title")` at byte `at` (which holds `(`): (destination, index after `)`). -fn link_dest(s: &str, at: usize) -> Option<(String, usize)> { - let rest = s.get(at..)?.strip_prefix('(')?; - let mut i = at + 1; - let ws = rest.len() - rest.trim_start().len(); - i += ws; - let r = s.get(i..)?; - let (dest, used) = if let Some(inner) = r.strip_prefix('<') { - // The first `>` not escaped by a backslash ends it; a line break first means no link. - let b = inner.as_bytes(); - let mut k = 0; - let end = loop { - match b.get(k)? { - b'\\' => k += 1, - b'>' => break k, - b'\n' => return None, - _ => {} - } - k += 1; - }; - (inner.get(..end)?.to_string(), end + 2) - } else { - let mut depth = 0i32; - let mut end = r.len(); - let mut chars = r.char_indices().peekable(); - while let Some((k, c)) = chars.next() { - match c { - '\\' => { - chars.next(); - } - '(' => depth += 1, - ')' if depth == 0 => { - end = k; - break; - } - ')' => depth -= 1, - c if c.is_whitespace() => { - end = k; - break; - } - _ => {} - } - } - (r.get(..end)?.to_string(), end) - }; - i += used; - let r = s.get(i..)?; - let ws = r.len() - r.trim_start().len(); - i += ws; - let r = s.get(i..)?; - if let Some(q) = r.chars().next().filter(|c| matches!(c, '"' | '\'' | '(')) { - let close = if q == '(' { ')' } else { q }; - let end = r.get(1..)?.find(close)?; - i += end + 2; - let r = s.get(i..)?; - i += r.len() - r.trim_start().len(); - } - if s.get(i..)?.starts_with(')') { Some((unescape(&dest), i + 1)) } else { None } -} - -fn unescape(s: &str) -> String { - let mut out = String::with_capacity(s.len()); - let mut chars = s.chars().peekable(); - while let Some(c) = chars.next() { - if c == '\\' - && let Some(n) = chars.peek().copied().filter(char::is_ascii_punctuation) - { - out.push(n); - chars.next(); - } else { - out.push(c); - } - } - out -} - -/// The value of attribute `name` in an HTML tag's text. -fn tag_attr(tag: &str, name: &str) -> Option { - let lower = tag.to_ascii_lowercase(); - let mut from = 0; - while let Some(k) = lower.get(from..)?.find(name) { - let at = from + k; - let before = lower.get(..at)?.chars().next_back(); - let after = lower.get(at + name.len()..)?.trim_start(); - if before.is_some_and(char::is_whitespace) && after.starts_with('=') { - let v = after.get(1..)?.trim_start(); - let q = v.chars().next()?; - return Some(if q == '"' || q == '\'' { - let inner = v.get(1..)?; - let off = tag.len() - inner.len(); - let end = inner.find(q)?; - tag.get(off..off + end)?.to_string() - } else { - let end = v.find(|c: char| c.is_whitespace() || c == '>').unwrap_or(v.len()); - let off = tag.len() - v.len(); - tag.get(off..off + end)?.to_string() - }); - } - from = at + name.len(); - } - None -} - -fn tokenize(s: &str, base: &Fmt, depth: usize, refs: &Refs) -> Vec { - let mut nodes: Vec = Vec::new(); - // Bytes the link/image scans and reference-label lookups may read in total. Unmatched - // brackets and nested undefined references would otherwise rescan the rest of the text, - // which is quadratic on hostile input. Once the budget is gone, further brackets are plain - // text. A scan pays for what it reads; earlier bracket matches are looked up in `known`. - let ticks = Backticks::new(s); - let mut links = LinkScan { text: s, refs, ticks: &ticks, known: HashMap::new(), budget: s.len().saturating_mul(8).saturating_add(8192) }; - let b = s.as_bytes(); - let mut i = 0usize; - let mut text_start = 0usize; - macro_rules! flush { - () => { - if text_start < i { - push_text(&mut nodes, s.get(text_start..i).unwrap_or(""), base); - } - }; - } - while let Some(&c) = b.get(i) { - match c { - b'\\' => { - flush!(); - match s.get(i + 1..).and_then(|r| r.chars().next()) { - Some(n) if n.is_ascii_punctuation() => { - push_text(&mut nodes, &n.to_string(), base); - i += 2; - } - _ => { - push_text(&mut nodes, "\\", base); - i += 1; - } - } - text_start = i; - } - b'`' => { - flush!(); - let (code, fmt, end) = code_span(s, i, base, &ticks); - push_text(&mut nodes, &code, &fmt); - i = end; - text_start = i; - } - b'*' | b'_' | b'~' => { - flush!(); - let ch = c as char; - let n = b.get(i..).map(|r| r.iter().take_while(|x| **x == c).count()).unwrap_or(1); - let before = s.get(..i).and_then(|r| r.chars().next_back()).unwrap_or(' '); - let after = s.get(i + n..).and_then(|r| r.chars().next()).unwrap_or(' '); - let lf = !after.is_whitespace() && (!is_punct(after) || before.is_whitespace() || is_punct(before)); - let rf = !before.is_whitespace() && (!is_punct(before) || after.is_whitespace() || is_punct(after)); - let (open, close) = match ch { - '_' => (lf && (!rf || is_punct(before)), rf && (!lf || is_punct(after))), - '~' if n != 2 => (false, false), - _ => (lf, rf), - }; - if open || close { - nodes.push(Node::Delim { ch, orig: n, left: n, open, close, opens: Vec::new(), closes: Vec::new() }); - } else { - push_text(&mut nodes, s.get(i..i + n).unwrap_or(""), base); - } - i += n; - text_start = i; - } - b'!' | b'[' if depth < MAX_NEST => { - let img = c == b'!'; - let open = if img { i + 1 } else { i }; - let parsed = if b.get(open) == Some(&b'[') { links.parse_at(open) } else { None }; - match parsed { - Some((inner, dest, end)) if img => { - flush!(); - let alt: String = parse_inlines(inner, &Fmt::default(), depth + 1, refs) - .iter() - .filter_map(|x| if let Inline::Text(t, _) = x { Some(t.as_str()) } else { None }) - .collect(); - match model::data_uri(&dest).and_then(|d| make_img(d, None, None, &alt)) { - Some(im) => nodes.push(Node::Inl(Inline::Image(im))), - None => push_text(&mut nodes, &alt, base), - } - i = end; - text_start = i; - } - Some((inner, dest, end)) if base.link.is_none() => { - flush!(); - let mut f = base.clone(); - // A destination too long to keep leaves its text unlinked. - f.link = (dest.len() <= model::MAX_LINK).then_some(dest); - if f.link.is_none() { - model::dropped(model::Dropped::Link); - } - for x in parse_inlines(inner, &f, depth + 1, refs) { - nodes.push(Node::Inl(x)); - } - i = end; - text_start = i; - } - _ => i += 1, - } - } - b'<' => { - if let Some((end, parsed)) = angle_inlines(s, i, base) { - flush!(); - nodes.extend(parsed); - i = end; - text_start = end; - } else { - i += 1; - } - } - b'&' => { - let rest = s.get(i..).unwrap_or(""); - match within(rest, 33).find(';').and_then(|k| decode_entity(rest.get(1..k)?).map(|ch| (ch, k))) { - Some((ch, k)) => { - flush!(); - push_text(&mut nodes, &ch, base); - i += k + 1; - text_start = i; - } - None => i += 1, - } - } - _ => i += 1, - } - } - flush!(); - nodes -} - -fn angle_inlines(s: &str, start: usize, base: &Fmt) -> Option<(usize, Vec)> { - if let Some(pos) = s.get(start..).and_then(|rest| rest.strip_prefix("")) { - return Some((start + 4 + pos + 3, Vec::new())); - } - let rest = s.get(start + 1..).unwrap_or(""); - let tag_end = within(rest, MAX_TAG).find('>')?; - let inner = rest.get(..tag_end)?; - let lower = inner.to_ascii_lowercase(); - let end = start + inner.len() + 2; - let mut nodes = Vec::new(); - if !inner.is_empty() - && !inner.contains([' ', '<', '\n']) - && (inner.contains("://") || lower.starts_with("mailto:") || (inner.contains('@') && !inner.contains('/'))) - { - let mut fmt = base.clone(); - fmt.link = Some(if inner.contains('@') && !inner.contains(':') { format!("mailto:{inner}").into() } else { inner.into() }); - push_text(&mut nodes, inner, &fmt); - Some((end, nodes)) - } else if matches!(lower.trim_end_matches('/').trim(), "br") { - push_text(&mut nodes, "\n", base); - Some((end, nodes)) - } else if lower.starts_with("a ") && rest.get(inner.len() + 1..).is_some_and(|tail| tail.to_ascii_lowercase().starts_with("")) { - if let Some(name) = tag_attr(inner, "id").or_else(|| tag_attr(inner, "name")) { - nodes.push(Node::Inl(Inline::Anchor(name))); - } - Some((end + 4, nodes)) - } else { - None - } -} - -fn code_span(s: &str, start: usize, base: &Fmt, ticks: &Backticks) -> (String, Fmt, usize) { - let bytes = s.as_bytes(); - let count = bytes.get(start..).map(|tail| tail.iter().take_while(|byte| **byte == b'`').count()).unwrap_or(1); - let Some(end) = ticks.find(start + count, count) else { - return (s.get(start..start + count).unwrap_or("").to_string(), base.clone(), start + count); - }; - let mut code = s.get(start + count..end).unwrap_or("").replace('\n', " "); - if code.len() >= 2 && code.starts_with(' ') && code.ends_with(' ') && !code.trim().is_empty() { - code = code[1..code.len() - 1].to_string(); - } - let mut fmt = base.clone(); - fmt.code = true; - (code, fmt, end + count) -} - -/// Longest `<...>` tag or autolink taken as inline HTML. -const MAX_TAG: usize = 4096; - -/// The first `n` bytes of `s` (fewer if that would cut a character), so a search for a closing -/// character stays short however long the text is. -pub(super) fn within(s: &str, n: usize) -> &str { - let mut end = n.min(s.len()); - while !s.is_char_boundary(end) { - end -= 1; - } - s.get(..end).unwrap_or("") -} - -fn emph_index(ch: char) -> usize { - match ch { - '*' => 0, - '_' => 1, - _ => 2, - } -} - -/// The CommonMark "process emphasis" pass over the delimiter runs, then flattening into spans. -fn emit(mut nodes: Vec) -> Vec { - // Delimiter stack of node indexes; bottoms per (char, closer length % 3, closer can open). - let mut stack: Vec = Vec::new(); - let mut bottom = [[[None::; 2]; 3]; 3]; - for c in 0..nodes.len() { - let (ch, corig, copen, cclose) = match nodes.get(c) { - Some(Node::Delim { ch, orig, open, close, .. }) => (*ch, *orig, *open, *close), - _ => continue, - }; - if cclose { - loop { - let cleft = match nodes.get(c) { - Some(Node::Delim { left, .. }) => *left, - _ => 0, - }; - if cleft == 0 { - break; - } - let bot = bottom.get(emph_index(ch)).and_then(|x| x.get(corig % 3)).and_then(|x| x.get(copen as usize)).copied().flatten(); - let mut found = None; - for (k, &o) in stack.iter().enumerate().rev() { - if bot.is_some_and(|b| o <= b) { - break; - } - if let Some(Node::Delim { ch: och, orig: oorig, left: oleft, open: oopen, close: oclose, .. }) = nodes.get(o) { - if *och != ch || !*oopen || *oleft == 0 { - continue; - } - if ch == '~' && (*oleft < 2 || cleft < 2) { - continue; - } - let odd = (*oclose || copen) && (oorig + corig) % 3 == 0 && !(oorig % 3 == 0 && corig % 3 == 0); - if odd { - continue; - } - found = Some((k, o, *oleft)); - break; - } - } - let Some((k, o, oleft)) = found else { - if let Some(slot) = bottom.get_mut(emph_index(ch)).and_then(|x| x.get_mut(corig % 3)).and_then(|x| x.get_mut(copen as usize)) { - *slot = Some(c.saturating_sub(1)); - } - break; - }; - let (used, kind) = if ch == '~' { - (2, Emph::Strike) - } else if oleft >= 2 && cleft >= 2 { - (2, Emph::Strong) - } else { - (1, Emph::Em) - }; - if let Some(Node::Delim { left, opens, .. }) = nodes.get_mut(o) { - *left -= used; - opens.push(kind); - } - if let Some(Node::Delim { left, closes, .. }) = nodes.get_mut(c) { - *left -= used; - closes.push(kind); - } - // Delimiters between opener and closer can no longer match. - stack.truncate(k + 1); - if oleft - used == 0 { - stack.truncate(k); - } - } - } - let left = match nodes.get(c) { - Some(Node::Delim { left, .. }) => *left, - _ => 0, - }; - if copen && left > 0 { - stack.push(c); - } - } - // Flatten. - let mut out: Vec = Vec::new(); - let (mut em, mut strong, mut strike) = (0usize, 0usize, 0usize); - let push = |out: &mut Vec, t: &str, f: &Fmt, em: usize, strong: usize, strike: usize| { - if t.is_empty() { - return; - } - let mut f = f.clone(); - f.italic |= em > 0; - f.bold |= strong > 0; - f.strike |= strike > 0; - if let Some(Inline::Text(pt, pf)) = out.last_mut() - && *pf == f - { - pt.push_str(t); - return; - } - out.push(Inline::Text(t.to_string(), f)); - }; - // Delimiter literals take the formatting (link, …) of the text before them: the last text - // node, tracked here rather than searched for backwards from every delimiter (quadratic on - // a paragraph of delimiters). - let mut last_text: Option<&Fmt> = None; - for n in &nodes { - match n { - Node::Text(t, f) => { - push(&mut out, t, f, em, strong, strike); - last_text = Some(f); - } - Node::Inl(Inline::Text(t, f)) => push(&mut out, t, f, em, strong, strike), - Node::Inl(other) => out.push(other.clone()), - Node::Delim { ch, left, opens, closes, .. } => { - for e in closes { - match e { - Emph::Em => em = em.saturating_sub(1), - Emph::Strong => strong = strong.saturating_sub(1), - Emph::Strike => strike = strike.saturating_sub(1), - } - } - if *left > 0 { - let lit: String = std::iter::repeat_n(*ch, *left).collect(); - let f = last_text.map_or_else(Fmt::default, |f| Fmt { bold: false, italic: false, strike: false, ..f.clone() }); - push(&mut out, &lit, &f, em, strong, strike); - } - for e in opens { - match e { - Emph::Em => em += 1, - Emph::Strong => strong += 1, - Emph::Strike => strike += 1, - } - } - } - } - } - out -} diff --git a/crates/formats/src/markdown/tests.rs b/crates/formats/src/markdown/tests.rs deleted file mode 100644 index 439166c96e..0000000000 --- a/crates/formats/src/markdown/tests.rs +++ /dev/null @@ -1,849 +0,0 @@ -use super::*; - -fn inl(s: &str) -> Vec { - parse_inlines(s, &Fmt::default(), 0, &Refs::new()) -} - -fn spans(s: &str) -> Vec<(String, bool, bool)> { - inl(s).into_iter().filter_map(|i| if let Inline::Text(t, f) = i { Some((t, f.bold, f.italic)) } else { None }).collect() -} - -#[test] -fn emphasis() { - assert_eq!(spans("a *b* c"), vec![("a ".into(), false, false), ("b".into(), false, true), (" c".into(), false, false)]); - assert_eq!(spans("**b**"), vec![("b".into(), true, false)]); - assert_eq!(spans("***x***"), vec![("x".into(), true, true)]); - assert_eq!(spans("*a **b** c*"), vec![("a ".into(), false, true), ("b".into(), true, true), (" c".into(), false, true)]); - assert_eq!(spans("snake_case_name"), vec![("snake_case_name".into(), false, false)]); - assert_eq!(spans("2 * 3 * 4"), vec![("2 * 3 * 4".into(), false, false)]); - assert_eq!(spans("**unclosed"), vec![("**unclosed".into(), false, false)]); - let s = inl("~~gone~~"); - assert!(matches!(&s[0], Inline::Text(t, f) if t == "gone" && f.strike)); -} - -#[test] -fn links_code_escapes() { - let s = inl("see [the *site*](http://x.org \"T\") and `a*b*` \\*lit\\* & "); - let link: Vec<_> = s - .iter() - .filter_map(|i| if let Inline::Text(t, f) = i { f.link.as_ref().map(|l| (t.clone(), l.to_string(), f.italic)) } else { None }) - .collect(); - assert_eq!(link[0], ("the ".into(), "http://x.org".into(), false)); - assert_eq!(link[1], ("site".into(), "http://x.org".into(), true)); - assert!(link.iter().any(|l| l.1 == "https://y.com")); - assert!(s.iter().any(|i| matches!(i, Inline::Text(t, f) if t == "a*b*" && f.code))); - let text: String = s.iter().filter_map(|i| if let Inline::Text(t, _) = i { Some(t.as_str()) } else { None }).collect(); - assert!(text.contains("*lit* & "), "{text}"); -} - -/// Reference links: `[text][label]`, `[text][]` and `[text]` take their destination from a -/// definition line, and the definition itself leaves the document. Before, none of the three -/// was a link and the definition stayed as a paragraph of its own (#3238). -#[test] -fn reference_links_resolve_and_their_definitions_leave_the_body() { - let links = |md: &str| -> Vec<(String, String)> { - match &*parse(md).blocks { - [FBlock::Para(p), ..] => p - .inlines - .iter() - .filter_map(|x| match x { - Inline::Text(t, f) => f.link.as_deref().map(|l| (t.clone(), l.to_string())), - _ => None, - }) - .collect(), - _ => vec![], - } - }; - let def = "\n\n[ref1]: https://example.com/ref\n"; - let to_ref = |text: &str| vec![(text.to_string(), "https://example.com/ref".to_string())]; - - assert_eq!(links(&format!("[click here][ref1]{def}")), to_ref("click here"), "full"); - assert_eq!(links(&format!("[ref1][]{def}")), to_ref("ref1"), "collapsed"); - assert_eq!(links(&format!("[ref1]{def}")), to_ref("ref1"), "shortcut"); - // Labels match case-folded with runs of white space collapsed (CommonMark §4.7). - assert_eq!(links(&format!("[click here][ REF1 ]{def}")), to_ref("click here"), "folded label"); - // An undefined label is not a link: an ordinary bracketed phrase stays text. - assert_eq!(links(&format!("[nope]{def}")), vec![], "undefined"); - // Inline links are unaffected. - assert_eq!(links("[x](https://inline.example)"), vec![("x".into(), "https://inline.example".into())], "inline"); - - // The definition is not a paragraph, and its text is nowhere in the body. - let doc = import(&format!("Reference link: [click here][ref1]{def}")); - let text = doc.plain_text(wordcraft_doc::StoryRef::Body); - assert_eq!(text, "Reference link: click here", "{text:?}"); - - // A definition inside a fence is code, not a definition. - let fenced = "```\n[ref1]: https://example.com/ref\n```\n\n[ref1]\n"; - let body = import(fenced).plain_text(wordcraft_doc::StoryRef::Body); - assert!(body.contains("[ref1]: https://example.com/ref"), "the fenced definition is still code: {body:?}"); - assert_eq!(links(fenced), vec![], "a fenced definition defines nothing"); -} - -/// Cached bracket pairs must not make undefined shortcut references normalize every nested -/// suffix again. One unrelated definition keeps this on the reference-lookup path. -#[test] -fn nested_undefined_reference_labels_have_bounded_work() { - let literal = format!("prefix {}x{} suffix", "[".repeat(128_000), "]".repeat(128_000)); - let src = format!("{literal}\n\n[other]: https://example.com/other\n"); - let started = std::time::Instant::now(); - let doc = import(&src); - let elapsed = started.elapsed(); - assert_eq!(doc.plain_text(wordcraft_doc::StoryRef::Body), literal); - assert!(elapsed < std::time::Duration::from_secs(10), "nested undefined references took {elapsed:?}"); -} - -#[test] -fn reference_lookup_keeps_long_unicode_labels_and_nested_text() { - // The work guard is not a label-length cap. Collapsed/full/shortcut references keep their - // case and whitespace matching, even when lowercasing changes the number of UTF-8 bytes. - let label = format!("{}İ END", "Écho ".repeat(1500)); - let key = label.to_lowercase(); - let src = format!("[{label}] [{label}][] [full][{label}]\n\n[{key}]: https://example.com/long\n"); - let flow = parse(&src); - let FBlock::Para(p) = &flow.blocks[0] else { panic!("paragraph") }; - let links: Vec<_> = p - .inlines - .iter() - .filter_map(|x| match x { - Inline::Text(t, f) if f.link.is_some() => Some((t.as_str(), f.link.as_deref())), - _ => None, - }) - .collect(); - assert_eq!( - links, - vec![ - (label.as_str(), Some("https://example.com/long")), - (label.as_str(), Some("https://example.com/long")), - ("full", Some("https://example.com/long")) - ] - ); - - let refs = Refs::from([("known".into(), Arc::from("https://example.com/known"))]); - let out = parse_inlines("[outer [known]] tail [inline](https://example.com/inline)", &Fmt::default(), 0, &refs); - let text: String = out.iter().filter_map(|x| if let Inline::Text(t, _) = x { Some(t.as_str()) } else { None }).collect(); - assert_eq!(text, "[outer known] tail inline"); - assert!(out.iter().any(|x| matches!(x, Inline::Text(t, f) if t == "known" && f.link.as_deref() == Some("https://example.com/known")))); - assert!(out.iter().any(|x| matches!(x, Inline::Text(t, f) if t == "inline" && f.link.as_deref() == Some("https://example.com/inline")))); -} - -/// A definition has to start a block (CommonMark §4.7), and a fence opens and closes by the -/// same rules the block parser uses. Reading a line without that context deleted text: a -/// definition-shaped line inside a paragraph, and every such line inside a code block whose -/// fence this pass thought had closed. -#[test] -fn a_definition_shaped_line_that_is_not_a_definition_stays_in_the_document() { - let body = |md: &str| import(md).plain_text(wordcraft_doc::StoryRef::Body); - - // A definition cannot interrupt a paragraph: this is the author's text, lines and all. - let para = "Status update\n[Warning]: unstable\nmore text\n"; - assert_eq!(body(para), "Status update [Warning]: unstable more text", "{:?}", body(para)); - - // `` ```rust `` holds an info string, so it does not close a fence: the definition-shaped - // line after it is still code. - let code = "```\n```rust\n[foo]: /bar\n```\n\nSee [foo].\n"; - assert!(body(code).contains("[foo]: /bar"), "{:?}", body(code)); - assert!(body(code).ends_with("See [foo]."), "nothing defines foo: {:?}", body(code)); - - // Four spaces of indentation is code, not a fence, so the definition that follows is - // taken and the reference resolves. - let indented = " ```\n\n[x]: /y\n\nSee [x].\n"; - let doc = import(indented); - let text = doc.plain_text(wordcraft_doc::StoryRef::Body); - assert!(text.contains("```") && text.ends_with("See x."), "{text:?}"); - - // A definition still gives up its line after a blank line, at the start of the document, - // after a heading, and after another definition. - let starts = "[a]: /a\n[b]: /b\n\n# Head\n[c]: /c\n\nSee [a], [b], [c].\n"; - assert_eq!(body(starts), "Head\nSee a, b, c.", "{:?}", body(starts)); - - // A footnote definition is not a link definition: it stays in the body. - let footnote = "x[^1]\n\n[^1]: Note\n"; - assert!(body(footnote).contains("[^1]: Note"), "{:?}", body(footnote)); -} - -#[test] -fn blocks() { - let f = parse( - "# Title\n\nPara one\ncontinues.\n\n> quoted\n\n- a\n- b\n 1. nested\n\n```\ncode *x*\n```\n\n---\n\n| A | B |\n|---|:-:|\n| 1 | 2 |\n", - ); - let kinds: Vec = f - .blocks - .iter() - .map(|b| match b { - FBlock::Para(p) => format!("{:?}{:?}:{}", p.kind, p.list.map(|l| (l.ordered, l.level)), p.text()), - FBlock::Table(t) => format!("table{}", t.rows.len()), - }) - .collect(); - assert_eq!( - kinds, - vec![ - "Heading(1)None:Title", - "NormalNone:Para one continues.", - "QuoteNone:quoted", - "NormalSome((false, 0)):a", - "NormalSome((false, 0)):b", - "NormalSome((true, 1)):nested", - "CodeNone:code *x*", - "RuleNone:", - "table2", - ] - ); -} - -#[test] -fn export_escapes_round_trip() { - let mut p = Para::default(); - p.push_text("1. not a list *really*", &Fmt::default()); - let md = escape_line_start(inlines_md(&p.inlines)); - let back = parse(&md); - let FBlock::Para(q) = &back.blocks[0] else { panic!() }; - assert_eq!(q.text(), "1. not a list *really*"); - assert!(q.list.is_none()); -} - -#[test] -fn export_emphasis_next_to_punctuation_round_trips() { - let marks: [(&str, fn(&mut Fmt)); 3] = [("bold", |f| f.bold = true), ("italic", |f| f.italic = true), ("strike", |f| f.strike = true)]; - let cases: [&[(&str, bool)]; 4] = [ - &[("注意:", true), ("请阅读", false)], - &[("a.", true), ("b", false)], - &[("a", false), ("!", true), ("b", false)], - &[("a", false), ("(x)", true), ("b", false)], - ]; - for (name, set) in marks { - for runs in cases { - let mut p = Para::default(); - for (t, on) in runs { - let mut f = Fmt::default(); - if *on { - set(&mut f); - } - p.push_text(t, &f); - } - let md = inlines_md(&p.inlines); - let back = parse(&md); - let FBlock::Para(q) = &back.blocks[0] else { panic!() }; - let want: String = runs.iter().map(|r| r.0).collect(); - assert_eq!(q.text(), want, "{name} exported as {md:?}"); - let got: Vec<(String, bool)> = q - .inlines - .iter() - .filter_map(|i| if let Inline::Text(t, f) = i { Some((t.clone(), f.bold || f.italic || f.strike)) } else { None }) - .collect(); - let expect: Vec<(String, bool)> = runs.iter().map(|(t, on)| (t.to_string(), *on)).collect(); - assert_eq!(got, expect, "{name} exported as {md:?}"); - } - } -} - -#[test] -fn export_adjacent_emphasis_next_to_punctuation_round_trips() { - let b = Fmt { bold: true, ..Fmt::default() }; - let i = Fmt { italic: true, ..Fmt::default() }; - let s = Fmt { strike: true, ..Fmt::default() }; - let cases: [&[(&str, &Fmt)]; 6] = [ - &[("ab", &b), ("!c", &i)], - &[("ab", &i), ("!c", &b)], - &[("ab", &b), ("!c", &s)], - &[("ab", &s), ("(c)", &b)], - &[("a.", &b), ("b", &i)], - &[("a.", &b), ("!b", &i)], - ]; - for runs in cases { - let mut p = Para::default(); - for (t, f) in runs { - p.push_text(t, f); - } - let md = inlines_md(&p.inlines); - let back = parse(&md); - let FBlock::Para(q) = &back.blocks[0] else { panic!() }; - let got: Vec<(String, bool, bool, bool)> = - q.inlines.iter().filter_map(|x| if let Inline::Text(t, f) = x { Some((t.clone(), f.bold, f.italic, f.strike)) } else { None }).collect(); - let expect: Vec<(String, bool, bool, bool)> = runs.iter().map(|(t, f)| (t.to_string(), f.bold, f.italic, f.strike)).collect(); - assert_eq!(got, expect, "exported as {md:?}"); - } -} - -#[test] -fn escape_line_start_preserves_indentation() { - assert_eq!(escape_line_start(" indented text".to_string()), " indented text"); - assert_eq!(escape_line_start(" # not a heading".to_string()), " \\# not a heading"); - assert_eq!(escape_line_start(" 1. not a list".to_string()), " 1\\. not a list"); - assert_eq!(escape_line_start(" > quote".to_string()), " \\> quote"); - // Four spaces or a tab would make an indented code block: the indent is capped at three. - assert_eq!(escape_line_start(" deep".to_string()), " deep"); - assert_eq!(escape_line_start("\tTabbed # text".to_string()), " Tabbed # text"); - assert_eq!(escape_line_start(" # not a heading".to_string()), " \\# not a heading"); - assert_eq!(escape_line_start(" \t1. not a list".to_string()), " 1\\. not a list"); -} - -#[test] -fn leading_unicode_whitespace_is_kept_and_does_not_start_blocks() { - let kinds_text = |md: &str| -> Vec<(Kind, bool, String)> { - parse(md).blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some((p.kind, p.list.is_some(), p.text())) } else { None }).collect() - }; - assert_eq!(kinds_text("\u{3000}\u{3000}日本語の段落\n"), vec![(Kind::Normal, false, "\u{3000}\u{3000}日本語の段落".to_string())]); - for src in ["\u{a0}# not a heading", "\u{3000}1. not a list", "\u{a0}- x", "\u{a0}> not a quote"] { - let mut p = Para::default(); - p.push_text(src, &Fmt::default()); - let md = escape_line_start(inlines_md(&p.inlines)); - assert_eq!(kinds_text(&md), vec![(Kind::Normal, false, src.to_string())], "{src:?} via {md:?}"); - } -} - -#[test] -fn single_backtick_code_span_escaped_pipe() { - let md = "| Command | Status |\n| --- | --- |\n| `cmd \\| grep` | pass |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 2); - assert_eq!(t.rows[1].len(), 2); - let FBlock::Para(p0) = &t.rows[1][0].blocks[0] else { panic!() }; - assert_eq!(p0.inlines.len(), 1); - assert!(matches!(&p0.inlines[0], Inline::Text(t, f) if t == "cmd | grep" && f.code)); - let FBlock::Para(p1) = &t.rows[1][1].blocks[0] else { panic!() }; - assert_eq!(p1.text(), "pass"); - - let mut out = Vec::new(); - table_md(t, &mut out); - let exported = out.join("\n"); - assert!(exported.contains("`cmd \\| grep`"), "Exported: {exported}"); -} - -#[test] -fn double_backtick_code_span_escaped_pipe() { - let md = "| Syntax | Note |\n| --- | --- |\n| ``a \\| b`` | second |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 2); - assert_eq!(t.rows[1].len(), 2); - let FBlock::Para(p0) = &t.rows[1][0].blocks[0] else { panic!() }; - assert_eq!(p0.inlines.len(), 1); - assert!(matches!(&p0.inlines[0], Inline::Text(t, f) if t == "a | b" && f.code)); - let FBlock::Para(p1) = &t.rows[1][1].blocks[0] else { panic!() }; - assert_eq!(p1.text(), "second"); -} - -#[test] -fn code_span_literal_backtick_and_escaped_pipe() { - let md = "| Expression | Detail |\n| --- | --- |\n| ``a`b\\|c`` | distinct |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 2); - assert_eq!(t.rows[1].len(), 2); - let FBlock::Para(p0) = &t.rows[1][0].blocks[0] else { panic!() }; - assert_eq!(p0.inlines.len(), 1); - assert!(matches!(&p0.inlines[0], Inline::Text(t, f) if t == "a`b|c" && f.code)); - let FBlock::Para(p1) = &t.rows[1][1].blocks[0] else { panic!() }; - assert_eq!(p1.text(), "distinct"); -} - -#[test] -fn three_column_table_with_distinctive_text() { - let md = "| Col1 | Col2 | Col3 |\n| --- | --- | --- |\n| `p1\\|q1` | Distinctive Two | Distinctive Three |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 2); - assert_eq!(t.rows[1].len(), 3); - assert_eq!(t.rows[1][0].text(), "p1|q1"); - assert_eq!(t.rows[1][1].text(), "Distinctive Two"); - assert_eq!(t.rows[1][2].text(), "Distinctive Three"); - let FBlock::Para(p0) = &t.rows[1][0].blocks[0] else { panic!() }; - assert!(matches!(&p0.inlines[0], Inline::Text(_, f) if f.code)); - let FBlock::Para(p1) = &t.rows[1][1].blocks[0] else { panic!() }; - assert!(matches!(&p1.inlines[0], Inline::Text(_, f) if !f.code)); - let FBlock::Para(p2) = &t.rows[1][2].blocks[0] else { panic!() }; - assert!(matches!(&p2.inlines[0], Inline::Text(_, f) if !f.code)); -} - -#[test] -fn table_cells_past_the_document_limits_are_not_built() { - // Every body row was padded to the header's width before any cap (#3701). - let wide = format!("{}|\n{}|\n|\n| b |\n", "|a".repeat(50_000), "|-".repeat(50_000)); - crate::model::begin_import(); - let flow = parse(&wide); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 3); - assert!(t.rows.iter().all(|r| r.len() == MAX_COLS)); - assert_eq!(t.rows[2][0].text(), "b"); - assert!(crate::model::Dropped::TableCells.reported(), "the columns left out are logged"); - // Rows past the limit still belong to the table, so the text after it reads as before. - let tall = format!("| a |\n| - |\n{}\nafter\n", "| x |\n".repeat(MAX_ROWS + 10)); - crate::model::begin_import(); - let flow = parse(&tall); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), MAX_ROWS); - let FBlock::Para(p) = &flow.blocks[1] else { panic!("expected paragraph") }; - assert_eq!(p.text(), "after"); - assert_eq!(flow.blocks.len(), 2); - assert!(crate::model::Dropped::TableCells.reported(), "the rows left out are logged"); -} - -/// A short row is padded to its header's width, so a small file of one-cell rows under wide -/// headers made millions of cells (CodeRabbit on #3862). The document's tables share one cell -/// budget: rows past it are left out and logged, and their lines make no paragraphs. -#[test] -fn tables_share_one_cell_budget() { - let header = |n: usize| format!("|{}|\n|{}|\n", vec![" h "; n].join("|"), vec!["-"; n].join("|")); - let fits = crate::markdown::MAX_TABLE_CELLS / MAX_COLS; - let src = format!("{}{}\nafter\n\n{}| 1 | 2 |\n", header(MAX_COLS), "| x |\n".repeat(fits + 100), header(2)); - crate::model::begin_import(); - let flow = parse(&src); - let tables: Vec<&FTable> = flow.blocks.iter().filter_map(|b| if let FBlock::Table(t) = b { Some(t) } else { None }).collect(); - let cells: usize = tables.iter().flat_map(|t| &t.rows).map(Vec::len).sum(); - assert!(cells <= crate::markdown::MAX_TABLE_CELLS, "{cells} cells"); - assert_eq!(tables.iter().map(|t| t.rows.len()).collect::>(), [fits], "the second table is past the budget"); - let texts: Vec = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.text()) } else { None }).collect(); - assert_eq!(texts, ["after"]); - assert!(crate::model::Dropped::TableCells.reported()); - // Within the budget a table is whole and nothing is logged. - crate::model::begin_import(); - let flow = parse(&format!("{}| 1 | 2 |\n", header(2))); - let [FBlock::Table(t)] = flow.blocks.as_slice() else { panic!("{:?}", flow.blocks) }; - assert_eq!(t.rows.len(), 2); - assert!(!crate::model::Dropped::TableCells.reported()); -} - -#[test] -fn a_wide_table_still_checks_every_header_and_delimiter_cell() { - // Only the first 63 cells are kept, but every one is counted and checked. - let head = "|a".repeat(100) + "|\n"; - let delim = |bad: Option| (0..100).map(|i| if Some(i) == bad { "|x" } else { "|-" }).collect::() + "|\n"; - let is_table = |md: String| matches!(parse(&md).blocks.first(), Some(FBlock::Table(_))); - assert!(is_table(format!("{head}{}| b |\n", delim(None)))); - assert!(!is_table(format!("{head}{}| b |\n", delim(Some(80)))), "a bad delimiter cell past the kept ones"); - assert!(!is_table(format!("{}|\n{}| b |\n", "|a".repeat(99), delim(None))), "99 header cells, 100 delimiter cells"); - let (cells, n) = split_row(r"| a | b \| c | d |", 2); - assert_eq!((cells, n), (vec!["a".to_string(), "b | c".to_string()], 3)); -} - -#[test] -fn regular_table_cells_containing_escaped_pipes() { - let md = "| Option | Description |\n| --- | --- |\n| Regular \\| Pipe | Normal text cell |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 2); - assert_eq!(t.rows[1].len(), 2); - assert_eq!(t.rows[1][0].text(), "Regular | Pipe"); - assert_eq!(t.rows[1][1].text(), "Normal text cell"); - let FBlock::Para(p0) = &t.rows[1][0].blocks[0] else { panic!() }; - assert!(matches!(&p0.inlines[0], Inline::Text(_, f) if !f.code)); - - let mut out = Vec::new(); - table_md(t, &mut out); - let exported = out.join("\n"); - assert!(exported.contains("Regular \\| Pipe"), "Exported: {exported}"); - assert!(!exported.contains("Regular \\\\| Pipe"), "Exported: {exported}"); -} - -#[test] -fn unescaped_pipes_behave_as_gfm_column_separators() { - let md = "| Header 1 | Header 2 |\n| --- | --- |\n| `raw|pipe` | Extra Column |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 2); - assert_eq!(t.rows[1].len(), 2); - assert_eq!(t.rows[1][0].text(), "`raw"); - assert_eq!(t.rows[1][1].text(), "pipe`"); -} - -#[test] -fn ordinary_tables_and_standalone_code_spans_unchanged() { - let md = "| Alpha | Beta |\n| --- | --- |\n| 1 | 2 |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!() }; - let mut out = Vec::new(); - table_md(t, &mut out); - assert_eq!(out.join("\n"), "| Alpha | Beta |\n| --- | --- |\n| 1 | 2 |"); - - let mut p = Para::default(); - let f = Fmt { code: true, ..Default::default() }; - p.push_text("cmd | grep", &f); - let exported = inlines_md(&p.inlines); - assert_eq!(exported, "`cmd | grep`"); -} - -#[test] -fn code_span_line_endings_stay_literal_on_export() { - let cases = [ - ("a\n# b", "a # b"), - ("a\n
    b
    ", "a
    b
    "), - ("a\n- b", "a - b"), - ("a\n```", "a ```"), - ("a\r# b", "a # b"), - ("a\r\n# b", "a # b"), - ("a\n\n> b", "a > b"), - ("\na\n", " a "), - ("`a\n# b`", "`a # b`"), - ("cmd | grep", "cmd | grep"), - ]; - let shapes = [ - (Kind::Normal, None), - (Kind::Normal, Some(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() })), - (Kind::Code, Some(ListInfo { ordered: true, level: 0, start: 1, ..Default::default() })), - ]; - for (source, expected) in cases { - for (kind, list) in shapes { - let mut p = Para::new(kind); - p.list = list; - p.push_text(source, &Fmt { code: true, ..Default::default() }); - let mut doc = model::to_doc(&model::Flow { blocks: vec![FBlock::Para(p)], ..Default::default() }); - // The flow builder removes CR controls; keep the native document's raw input. - let p = doc.para_mut(wordcraft_doc::StoryRef::Body, &wordcraft_doc::Path::top(0)).unwrap(); - p.text = source.to_string(); - p.runs[0].len = source.len(); - let bytes = crate::export("md", &doc).unwrap().unwrap(); - let md = String::from_utf8(bytes).unwrap(); - assert_eq!(md.trim_end().lines().count(), 1, "{source:?} {kind:?} {list:?} -> {md:?}"); - assert!(!md.contains('\r'), "{md:?}"); - let back = crate::import("md", md.as_bytes()).unwrap().unwrap(); - let flow = model::from_doc(&back); - let [FBlock::Para(q)] = flow.blocks.as_slice() else { panic!("{md:?}") }; - assert_eq!(q.text(), expected, "{source:?} {kind:?} {list:?} -> {md:?}"); - assert_eq!(q.list, list, "{md:?}"); - assert!(q.inlines.iter().all(|i| matches!(i, Inline::Text(_, f) if f.code)), "{md:?}"); - } - } - let fenced = "```\na\n# b\n```"; - assert_eq!(export(&import(fenced)).trim_end(), fenced); -} - -#[test] -fn code_span_line_endings_in_table_cells_stay_literal() { - for ending in ["\n", "\r", "\r\n"] { - let source = format!("a{ending}# b|c"); - let mut p = Para::default(); - p.push_text(&source, &Fmt { code: true, ..Default::default() }); - let cell = Cell { blocks: vec![FBlock::Para(p)], ..Default::default() }; - let table = FTable { rows: vec![vec![cell, Cell::default()], vec![Cell::default(), Cell::default()]], ..Default::default() }; - let mut doc = model::to_doc(&model::Flow { blocks: vec![FBlock::Table(table)], ..Default::default() }); - let p = doc.para_mut(wordcraft_doc::StoryRef::Body, &wordcraft_doc::Path(vec![0, 0, 0, 0])).unwrap(); - p.text = source.clone(); - p.runs[0].len = source.len(); - let md = export(&doc); - let back = model::from_doc(&import(&md)); - let [FBlock::Table(table), ..] = back.blocks.as_slice() else { panic!("{md:?}") }; - assert!(back.blocks.iter().skip(1).all(|b| matches!(b, FBlock::Para(p) if p.text().is_empty())), "{md:?}"); - assert_eq!(table.rows.len(), 2, "{md:?}"); - assert_eq!(table.rows[0].len(), 2, "{md:?}"); - let [FBlock::Para(p)] = table.rows[0][0].blocks.as_slice() else { panic!("{md:?}") }; - assert_eq!(p.text(), "a # b|c", "{md:?}"); - assert!(p.inlines.iter().all(|i| matches!(i, Inline::Text(_, f) if f.code)), "{md:?}"); - } -} - -#[test] -fn table_code_pipe_export_import_round_trip() { - let mut table = FTable::default(); - let mut cell_a = Cell::default(); - let mut para_a = Para::default(); - let fmt_a = Fmt { code: true, ..Default::default() }; - para_a.push_text("a`b|c", &fmt_a); - cell_a.blocks.push(FBlock::Para(para_a)); - - let mut cell_b = Cell::default(); - let mut para_b = Para::default(); - para_b.push_text("CRITICAL_DISTINCTIVE_INFO", &Fmt::default()); - cell_b.blocks.push(FBlock::Para(para_b)); - - let mut head_a = Cell::default(); - let mut head_para_a = Para::default(); - head_para_a.push_text("Col A", &Fmt::default()); - head_a.blocks.push(FBlock::Para(head_para_a)); - - let mut head_b = Cell::default(); - let mut head_para_b = Para::default(); - head_para_b.push_text("Col B", &Fmt::default()); - head_b.blocks.push(FBlock::Para(head_para_b)); - - table.rows.push(vec![head_a, head_b]); - table.rows.push(vec![cell_a, cell_b]); - - let mut out = Vec::new(); - table_md(&table, &mut out); - let exported_md = out.join("\n"); - - assert!(exported_md.contains("``a`b\\|c``"), "Exported: {exported_md}"); - assert!(exported_md.contains("CRITICAL\\_DISTINCTIVE\\_INFO"), "Exported: {exported_md}"); - - let back_flow = parse(&exported_md); - let FBlock::Table(back_table) = &back_flow.blocks[0] else { panic!("expected table") }; - - assert_eq!(back_table.rows.len(), 2, "Both rows must exist"); - let row1 = &back_table.rows[1]; - assert_eq!(row1.len(), 2, "Both cells must exist in row 1"); - - let back_cell_a = &row1[0]; - let back_cell_b = &row1[1]; - assert_eq!(back_cell_a.text(), "a`b|c", "Cell A text must be preserved"); - assert_eq!(back_cell_b.text(), "CRITICAL_DISTINCTIVE_INFO", "Cell B text must be preserved"); - - let FBlock::Para(back_p_a) = &back_cell_a.blocks[0] else { panic!() }; - assert_eq!(back_p_a.inlines.len(), 1); - assert!( - matches!(&back_p_a.inlines[0], Inline::Text(t, f) if t == "a`b|c" && f.code), - "Code formatting must remain intact on Cell A: {:?}", - back_p_a.inlines - ); - - let FBlock::Para(back_p_b) = &back_cell_b.blocks[0] else { panic!() }; - assert_eq!(back_p_b.inlines.len(), 1); - assert!( - matches!(&back_p_b.inlines[0], Inline::Text(t, f) if t == "CRITICAL_DISTINCTIVE_INFO" && !f.code), - "Cell B formatting must remain plain text: {:?}", - back_p_b.inlines - ); - - assert_eq!(back_cell_a.text(), "a`b|c"); - assert_eq!(back_cell_b.text(), "CRITICAL_DISTINCTIVE_INFO"); - - assert!(!back_cell_a.text().contains("CRITICAL_DISTINCTIVE_INFO")); - assert!(!back_cell_b.text().contains("a`b|c")); - - let doc = import(&exported_md); - let re_exported = export(&doc); - assert!(re_exported.contains("``a`b\\|c``"), "Re-exported: {re_exported}"); - assert!(re_exported.contains("CRITICAL\\_DISTINCTIVE\\_INFO"), "Re-exported: {re_exported}"); - let doc2 = import(&re_exported); - let back_flow2 = model::from_doc(&doc2); - let FBlock::Table(back_table2) = &back_flow2.blocks[0] else { panic!() }; - assert_eq!(back_table2.rows[1][0].text(), "a`b|c"); - assert_eq!(back_table2.rows[1][1].text(), "CRITICAL_DISTINCTIVE_INFO"); -} - -#[test] -fn ordered_list_keeps_start_number() { - let txt = |md: &str| String::from_utf8_lossy(&crate::txt::export(&import(md))).into_owned(); - assert_eq!(txt("5. a\n6. b"), "5.\ta\r\n6.\tb"); - assert_eq!(txt("3. a\n- b\n4. c"), "3.\ta\r\n•\tb\r\n4.\tc"); - assert_eq!(export(&import("5. a\n6. b")).trim_end(), "5. a\n6. b"); -} - -/// A list inside a block quote stays in the quote: its items were imported as quoted list -/// paragraphs but written back without `>`, so the list moved out of the quote. -#[test] -fn a_list_inside_a_quote_stays_quoted() { - let src = "> Intro\n>\n> - one\n> - two\n> - nested\n> 1. first\n\nAfter.\n"; - let want = "> Intro\n\n> - one\n> - two\n> - nested\n> 1. first\n\nAfter."; - let md = export(&import(src)); - assert_eq!(md.trim_end(), want); - assert_eq!(export(&import(&md)).trim_end(), want, "and stays so when read back"); - // An item's continuation line is quoted too. - assert_eq!(export(&import("> - a\\\n> b\n")).trim_end(), "> - a\\\n> b"); - // A list outside a quote is unchanged. - assert_eq!(export(&import("- one\n- two\n")).trim_end(), "- one\n- two"); - // A list after a quoted list stays outside the quote. - let md = export(&import("> - a\n\n2. b\n")); - assert_eq!(md.trim_end(), "> - a\n\n2. b"); - assert_eq!(export(&import(&md)).trim_end(), "> - a\n\n2. b", "and stays so when read back"); -} - -#[test] -fn lines_after_line_break_stay_in_paragraph() { - use wordcraft_doc::{CharProps, Document, Paragraph, StoryRef, para_block}; - let cases = ["Shopping\n- milk\n# not a heading", "a\n1. x", "a\n2) x", "a\n+ x", "a\n---", "a\n***", "a\n==", "a\n--", "a\n> q"]; - for s in cases { - let mut d = Document::new(); - d.body = vec![para_block(Paragraph::with_text(s, CharProps::default()))]; - let md = export(&d); - let back = import(&md); - assert_eq!(back.body.len(), 1, "{s:?} -> {md:?}"); - assert_eq!(back.plain_text(StoryRef::Body), s, "{md:?}"); - } -} - -#[test] -fn lines_after_line_break_stay_in_list_item_and_quote() { - let cases = ["a\n- x", "a\n# h", "a\n1. x", "a\n--", "a\n---", "a\n==", "a\n> q"]; - let shapes: [(Kind, Option); 5] = [ - (Kind::Normal, Some(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() })), - (Kind::Normal, Some(ListInfo { ordered: true, level: 0, start: 1, ..Default::default() })), - (Kind::Normal, Some(ListInfo { ordered: false, level: 1, start: 1, ..Default::default() })), - (Kind::Normal, Some(ListInfo { ordered: true, level: 1, start: 1, ..Default::default() })), - (Kind::Quote, None), - ]; - for s in cases { - for (kind, list) in shapes { - let mut blocks = Vec::new(); - if let Some(li) = list.filter(|l| l.level > 0) { - let mut parent = Para::new(Kind::Normal); - parent.list = Some(ListInfo { ordered: li.ordered, level: 0, start: 1, ..Default::default() }); - parent.push_text("top", &Fmt::default()); - blocks.push(FBlock::Para(parent)); - } - let mut p = Para::new(kind); - p.list = list; - p.push_text(s, &Fmt::default()); - blocks.push(FBlock::Para(p)); - let mut out = Vec::new(); - write_blocks(&blocks, &mut out); - let md = out.join("\n"); - let back = parse(&md); - assert_eq!(back.blocks.len(), blocks.len(), "{s:?} {kind:?} {list:?} -> {md:?}"); - let Some(FBlock::Para(q)) = back.blocks.last() else { panic!("{md:?}") }; - assert_eq!(q.kind, kind, "{md:?}"); - assert_eq!(q.list, list, "{md:?}"); - assert_eq!(q.text(), s, "{md:?}"); - } - } -} - -#[test] -fn table_column_alignments_import_and_export() { - let md = "| Left | Center | Right | Default |\n| :--- | :---: | ---: | --- |\n| 1 | 2 | 3 | 4 |\n"; - let flow = parse(md); - let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") }; - assert_eq!(t.rows.len(), 2); - let aligns: Vec> = - t.rows[0].iter().map(|c| c.blocks.iter().find_map(|b| if let FBlock::Para(p) = b { Some(p.align) } else { None }).flatten()).collect(); - assert_eq!(aligns, vec![Some(Align::Left), Some(Align::Center), Some(Align::Right), None]); - - let mut out = Vec::new(); - table_md(t, &mut out); - let exported = out.join("\n"); - assert!(exported.contains("| :--- | :---: | ---: | --- |"), "Exported: {exported}"); -} - -#[test] -fn tab_separated_list_markers() { - let f = parse("-\tbullet item\n*\tstar item\n+\tplus item\n1.\tone item\n2)\ttwo item\n"); - assert_eq!(f.blocks.len(), 5); - for (i, b) in f.blocks.iter().enumerate() { - let FBlock::Para(p) = b else { panic!("block {i} not a para") }; - assert!(p.list.is_some(), "block {i} should be a list item: {:?}", p); - } - let FBlock::Para(p0) = &f.blocks[0] else { panic!() }; - assert_eq!(p0.text(), "bullet item"); - assert!(!p0.list.unwrap().ordered); - - let FBlock::Para(p3) = &f.blocks[3] else { panic!() }; - assert_eq!(p3.text(), "one item"); - assert!(p3.list.unwrap().ordered); -} - -#[test] -fn html_comments_are_ignored_in_blocks_and_inlines() { - let md = "\n\nHello world!\n\n\n\nAfter comment."; - let flow = parse(md); - assert_eq!(flow.blocks.len(), 2); - let texts: Vec = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.text()) } else { None }).collect(); - assert_eq!(texts, ["Hello world!", "After comment."]); -} - -#[test] -fn a_paragraph_of_delimiters_parses_in_linear_time() { - // #3714: each delimiter's literal looked back for the text before it, to the start of - // the paragraph when there was none (quadratic). - let src = "*_".repeat(200_000); - let t = std::time::Instant::now(); - let out = inl(&src); - // Under a second on a loaded machine; the backwards search ran past 6 minutes. - assert!(t.elapsed() < std::time::Duration::from_secs(10), "{:?}", t.elapsed()); - let text: String = out.iter().filter_map(|i| if let Inline::Text(t, _) = i { Some(t.as_str()) } else { None }).collect(); - assert!(text.chars().all(|c| matches!(c, '*' | '_')), "only delimiters come out"); - // Literal delimiters still take the link of the text before them. - let s = inl("[a](http://x.org) [b*](http://y.org)"); - assert!(s.iter().any(|i| matches!(i, Inline::Text(t, f) if t == "b*" && f.link.as_deref() == Some("http://y.org"))), "{s:?}"); -} - -#[test] -fn exported_link_destinations_read_back_unchanged() { - // Removing `<` and `>` from a destination after the safety check could make a relative - // target a script or network-path one (`java)"); - for l in [ - "javab c", - "javascript:alert(1)", - "javascript:x", - "&", - "javascript\\:x", - "dir\\", - "dir\\(1)\\", - "q?a=1&b=2", - ] { - let out = md(l); - let back = inl(&out); - let got: Vec<_> = back.iter().filter_map(|i| if let Inline::Text(_, f) = i { f.link.as_deref().map(str::to_string) } else { None }).collect(); - assert_eq!(got, [l.to_string()], "{l:?} → {out:?}"); - } - // Ordinary destinations are written as before. - assert_eq!(md("https://example.com/a?b=1&c=2"), "[x](https://example.com/a?b=1&c=2)"); - assert_eq!(md("https://example.com/a b"), "[x]()"); - assert_eq!(md("https://e.com/x\ny"), "[x](https://e.com/xy)"); -} - -/// Verifies that trailing escaped hashes on headings survive round-trip export. -#[test] -fn heading_trailing_hash_survives_round_trip() { - let heading = |md: &str| { - let f = parse(md); - let Some(FBlock::Para(p)) = f.blocks.first() else { panic!("no paragraph in {md:?}") }; - assert_eq!(p.kind, Kind::Heading(2), "{md:?}"); - p.text() - }; - for (src, text, want) in [ - ("## Item \\#\n", "Item #", "## Item \\#\n"), - ("## Issue \\##\n", "Issue ##", "## Issue \\##\n"), - ("## \\#\n", "#", "## \\#\n"), - ("## C#\n", "C#", "## C#\n"), - ] { - assert_eq!(heading(src), text, "{src:?}"); - let md = export(&import(src)); - assert_eq!(md, want, "first export of {src:?}"); - assert_eq!(heading(&md), text, "md was {md:?}"); - assert_eq!(export(&import(&md)), want, "second export of {src:?}"); - } -} - -/// Verifies that block line mapping tracks source-line indexes accurately across definitions. -#[test] -fn block_line_mapping() { - let md = "# Title\n\nFirst paragraph.\n\n## Second\n\n- one\n- two\n\n```\ncode\n```\n\nLast paragraph.\n"; - let (flow, lines) = parse_with_lines(md); - assert_eq!(flow.blocks.len(), lines.len()); - let (doc, doc_lines) = import_on_with_lines(md, &wordcraft_doc::SectionProps::default()); - assert_eq!(doc.body.len(), doc_lines.len()); - assert_eq!(lines, doc_lines); - assert_eq!(lines[0], 0); - assert_eq!(lines[1], 2); - assert_eq!(lines[2], 4); - - // Reference definitions are removed from blocks, but block line numbers must match original source lines. - let md_refs = "[link]: https://example.com\n# Title\n\n[other]: https://example.org\nParagraph with [link][].\n"; - let (flow, lines) = parse_with_lines(md_refs); - assert_eq!(flow.blocks.len(), lines.len()); - assert_eq!(lines[0], 1, "heading on line 1 after definition on line 0"); - assert_eq!(lines[1], 4, "paragraph on line 4 after definition on line 3"); -} - -/// Verifies linear remapping of original lines after removing definitions. -#[test] -fn remap_original_lines_cases() { - let mut lines = vec![0, 1, 2, 3, 4]; - remap_original_lines(&mut lines, &[2, 5]); - assert_eq!(lines, vec![0, 1, 3, 4, 6]); - - let mut lines = vec![0, 1]; - remap_original_lines(&mut lines, &[1, 2, 3]); - assert_eq!(lines, vec![0, 4]); - - let mut lines = vec![0]; - remap_original_lines(&mut lines, &[0]); - assert_eq!(lines, vec![1]); - - let mut lines = vec![1, 1, 2]; - remap_original_lines(&mut lines, &[1]); - assert_eq!(lines, vec![2, 2, 3]); - - let mut lines = vec![0, 2]; - remap_original_lines(&mut lines, &[]); - assert_eq!(lines, vec![0, 2]); - - let mut empty: Vec = Vec::new(); - remap_original_lines(&mut empty, &[1, 2]); - assert!(empty.is_empty()); -} diff --git a/crates/formats/src/mathml.rs b/crates/formats/src/mathml.rs index ea915f60b5..7796fd17f5 100644 --- a/crates/formats/src/mathml.rs +++ b/crates/formats/src/mathml.rs @@ -1,1053 +1,20 @@ -//! Equations as MathML (W3C MathML Core / Presentation MathML): written into HTML export and the -//! HTML a copy puts on the system clipboard, so programs that read MathML can get a formatted -//! equation instead of its linear text (#816); for a copy of one equation alone, the text of the -//! platform's MathML clipboard formats, which Word reads first when it pastes math (#1297; -//! pasting into Word and LibreOffice not yet verified); and read back from pasted or opened HTML. -//! -//! The writer walks the equation tree ([`MNode`]) and wraps it in `` with the linear -//! format as an `application/x-unicodemath` annotation, which renderers ignore and WordCraft reads -//! back exactly. The reader takes that annotation when present, else a TeX annotation, else -//! builds the tree from the presentation elements (fractions, scripts, radicals, under/over -//! limits, tables, fences); both bound their nesting and size. +//! Equations as MathML (W3C MathML Core / Presentation MathML) via `craft-math`. -use wordcraft_doc::math::{ - Arg, ColJc, FracKind, LimLoc, MAX_DEPTH, MClass, MNode, MRun, MScr, MSty, ScriptKind, is_integral, is_nary_char, math_alnum, math_class, - to_linear, -}; -use wordcraft_doc::math_linear::FUNCTION_NAMES; +pub use craft_math::mathml::{NS, UNICODEMATH}; -use crate::html::esc; - -/// The MathML namespace. -pub const NS: &str = "http://www.w3.org/1998/Math/MathML"; -/// The annotation encoding carrying the linear format (UnicodeMath). -pub const UNICODEMATH: &str = "application/x-unicodemath"; - -// --------------------------------------------------------------------------------------------- -// Writer - -/// An equation as a `` element. `linear` (the linear format) goes in an annotation for an -/// exact round trip; empty = taken from the tree. -pub fn to_mathml(nodes: &[MNode], display: bool, linear: &str) -> String { - let mut o = String::new(); - o.push_str(&format!(""); - arg(nodes, &mut o, 0, false); - o.push_str(""); - let linear = if linear.is_empty() { to_linear(nodes) } else { linear.to_string() }; - if !linear.is_empty() { - o.push_str(&format!("{}", esc(&linear))); - } - o.push_str(""); - o +pub fn to_mathml(nodes: &[wordcraft_doc::math::MNode], display: bool, linear: &str) -> String { + let linear = if linear.is_empty() { wordcraft_doc::math::to_linear(nodes) } else { linear.to_string() }; + linear_to_mathml(&linear, display) } -/// An equation known by its linear format, as a `` element. pub fn linear_to_mathml(linear: &str, display: bool) -> String { - to_mathml(&wordcraft_doc::math::parse_linear(linear), display, linear) + let tree = craft_math::math::parse_linear(linear); + craft_math::mathml::to_mathml(&tree, display, linear) } -/// An argument as a row of elements. `eq_arr`: a row of an equation array, whose `&` marks are -/// alignment points, not text. -fn arg(a: &[MNode], o: &mut String, depth: usize, eq_arr: bool) { - if depth > MAX_DEPTH { - return; - } - for n in a { - node(n, o, depth + 1, eq_arr); - } -} - -/// An argument as one element (`` around several). -fn group(a: &[MNode], o: &mut String, depth: usize) { - o.push_str(""); - arg(a, o, depth, false); - o.push_str(""); -} - -fn mo(c: char, attrs: &str, o: &mut String) { - o.push_str(&format!("{}", esc(&c.to_string()))); -} - -fn run(r: &MRun, o: &mut String, eq_arr: bool) { - let color = r.color.map(|c| format!(" mathcolor=\"#{}\"", c.hex())).unwrap_or_default(); - if r.nor { - if !r.text.is_empty() { - o.push_str(&format!("{}", esc(&r.text))); - } - return; - } - let bold = matches!(r.sty, Some(MSty::Bold | MSty::BoldItalic)); - let italic = match r.sty { - None => r.scr == MScr::Roman, - Some(s) => matches!(s, MSty::Italic | MSty::BoldItalic), - }; - let cs: Vec = r.text.chars().collect(); - let mut i = 0; - while let Some(&c) = cs.get(i) { - if c.is_ascii_digit() || (c == '.' && cs.get(i + 1).is_some_and(char::is_ascii_digit)) { - let mut num = String::new(); - while let Some(&d) = - cs.get(i).filter(|d| d.is_ascii_digit() || **d == '.' || **d == ',' && cs.get(i + 1).is_some_and(char::is_ascii_digit)) - { - num.push(d); - i += 1; - } - let num: String = if bold { num.chars().map(|d| math_alnum(d, true, false, MScr::Roman)).collect() } else { num }; - o.push_str(&format!("{}", esc(&num))); - continue; - } - i += 1; - if c.is_whitespace() || (eq_arr && c == '&') { - continue; - } - if c.is_alphabetic() { - if bold || r.scr != MScr::Roman { - let m = math_alnum(c, bold, italic, r.scr); - let plain = if m == c && !italic { " mathvariant=\"normal\"" } else { "" }; - o.push_str(&format!("{}", esc(&m.to_string()))); - } else if italic { - o.push_str(&format!("{}", esc(&c.to_string()))); - } else { - o.push_str(&format!("{}", esc(&c.to_string()))); - } - } else if math_class(c) == MClass::Ord && !c.is_ascii_punctuation() && c != '\u{2061}' { - // ∞, ∂, ∅, ℏ…: identifiers, not operators. - o.push_str(&format!("{}", esc(&c.to_string()))); - } else { - mo(c, &color, o); - } - } -} - -/// A function name (`sin`, `lim`): one upright identifier when it's plain text. -fn func_name(name: &[MNode], o: &mut String, depth: usize) { - match name { - [MNode::Run(r)] if !r.nor && r.text.chars().all(char::is_alphabetic) && !r.text.is_empty() => { - o.push_str(&format!("{}", esc(&r.text))); - } - _ => group(name, o, depth), - } -} - -fn fraction(kind: FracKind, num: &[MNode], den: &[MNode], o: &mut String, depth: usize) { - match kind { - FracKind::Bar | FracKind::NoBar => { - o.push_str(if kind == FracKind::NoBar { "" } else { "" }); - group(num, o, depth); - group(den, o, depth); - o.push_str(""); - } - FracKind::Skewed | FracKind::Linear => { - o.push_str(""); - group(num, o, depth); - mo(if kind == FracKind::Skewed { '⁄' } else { '/' }, "", o); - group(den, o, depth); - o.push_str(""); - } - } -} - -fn script(kind: ScriptKind, base: &[MNode], sub: &[MNode], sup: &[MNode], o: &mut String, depth: usize) { - match kind { - ScriptKind::Sup => { - o.push_str(""); - group(base, o, depth); - group(sup, o, depth); - o.push_str(""); - } - ScriptKind::Sub => { - o.push_str(""); - group(base, o, depth); - group(sub, o, depth); - o.push_str(""); - } - ScriptKind::SubSup => { - o.push_str(""); - group(base, o, depth); - group(sub, o, depth); - group(sup, o, depth); - o.push_str(""); - } - ScriptKind::Pre => { - o.push_str(""); - group(base, o, depth); - o.push_str(""); - if sub.is_empty() { - o.push_str(""); - } else { - group(sub, o, depth); - } - if sup.is_empty() { - o.push_str(""); - } else { - group(sup, o, depth); - } - o.push_str(""); - } - } -} - -fn write_nary(n: &MNode, o: &mut String, depth: usize) { - let MNode::Nary { chr, lim_loc, sub_hide, sup_hide, sub, sup, e, .. } = n else { return }; - let under_over = match lim_loc { - Some(l) => *l == LimLoc::UndOvr, - None => !is_integral(*chr), - }; - let has_sub = !*sub_hide && !sub.is_empty(); - let has_sup = !*sup_hide && !sup.is_empty(); - let tag = match (under_over, has_sub, has_sup) { - (_, false, false) => "", - (true, true, true) => "munderover", - (true, true, false) => "munder", - (true, false, true) => "mover", - (false, true, true) => "msubsup", - (false, true, false) => "msub", - (false, false, true) => "msup", - }; - o.push_str(""); - if !tag.is_empty() { - o.push_str(&format!("<{tag}>")); - } - mo(*chr, " largeop=\"true\" movablelimits=\"false\"", o); - if has_sub { - group(sub, o, depth); - } - if has_sup { - group(sup, o, depth); - } - if !tag.is_empty() { - o.push_str(&format!("")); - } - group(e, o, depth); - o.push_str(""); -} - -fn delimiters(n: &MNode, o: &mut String, depth: usize) { - let MNode::Delim { beg, end, sep, grow, elems, .. } = n else { return }; - let stretchy = if *grow { "" } else { " stretchy=\"false\"" }; - o.push_str(""); - if let Some(begin) = beg { - mo(*begin, &format!(" fence=\"true\" form=\"prefix\"{stretchy}"), o); - } - for (i, element) in elems.iter().enumerate() { - if i > 0 { - mo(sep.unwrap_or('|'), &format!(" separator=\"true\"{stretchy}"), o); - } - group(element, o, depth); - } - if let Some(close) = end { - mo(*close, &format!(" fence=\"true\" form=\"postfix\"{stretchy}"), o); - } - o.push_str(""); -} - -fn node(n: &MNode, o: &mut String, depth: usize, eq_arr: bool) { - match n { - MNode::Run(r) => run(r, o, eq_arr), - MNode::Frac { kind, num, den } => fraction(*kind, num, den, o, depth), - MNode::Script { kind, base, sub, sup } => script(*kind, base, sub, sup, o, depth), - MNode::Rad { deg, deg_hide, e } => { - if *deg_hide || deg.is_empty() { - o.push_str(""); - arg(e, o, depth, false); - o.push_str(""); - } else { - o.push_str(""); - group(e, o, depth); - group(deg, o, depth); - o.push_str(""); - } - } - MNode::Nary { .. } => write_nary(n, o, depth), - MNode::Delim { .. } => delimiters(n, o, depth), - MNode::Func { name, e } => { - o.push_str(""); - func_name(name, o, depth); - mo('\u{2061}', "", o); - group(e, o, depth); - o.push_str(""); - } - MNode::Lim { upper, e, lim } => { - let tag = if *upper { "mover" } else { "munder" }; - o.push_str(&format!("<{tag}>")); - func_name(e, o, depth); - group(lim, o, depth); - o.push_str(&format!("")); - } - MNode::Acc { chr, e } => { - o.push_str(""); - group(e, o, depth); - mo(spacing_accent(*chr), "", o); - o.push_str(""); - } - MNode::Bar { top, e } => { - if *top { - o.push_str(""); - group(e, o, depth); - mo('\u{203E}', "", o); - o.push_str(""); - } else { - o.push_str(""); - group(e, o, depth); - mo('_', "", o); - o.push_str(""); - } - } - MNode::BorderBox { hide, strike, e } => { - let mut notation: Vec<&str> = Vec::new(); - for (h, name) in hide.iter().zip(["top", "bottom", "left", "right"]) { - if !h { - notation.push(name); - } - } - if notation.len() == 4 { - notation = vec!["box"]; - } - for (s, name) in strike.iter().zip(["horizontalstrike", "verticalstrike", "updiagonalstrike", "downdiagonalstrike"]) { - if *s { - notation.push(name); - } - } - o.push_str(&format!("", notation.join(" "))); - arg(e, o, depth, false); - o.push_str(""); - } - MNode::Boxed { e } => group(e, o, depth), - MNode::GroupChr { chr, top, e } => { - let tag = if *top { "mover" } else { "munder" }; - o.push_str(&format!("<{tag}>")); - group(e, o, depth); - mo(*chr, " stretchy=\"true\"", o); - o.push_str(&format!("")); - } - MNode::EqArr { rows } => { - // `displaystyle` marks an equation array; a bare one-column table reads as a matrix. - o.push_str(""); - for r in rows { - o.push_str(""); - arg(r, o, depth, true); - o.push_str(""); - } - o.push_str(""); - } - MNode::Matrix { rows, col_jc } => { - o.push_str(""); - for r in rows { - o.push_str(""); - for (k, cell) in r.iter().enumerate() { - match col_jc.get(k) { - Some(ColJc::Left) => o.push_str(""), - Some(ColJc::Right) => o.push_str(""), - _ => o.push_str(""), - } - arg(cell, o, depth, false); - o.push_str(""); - } - o.push_str(""); - } - o.push_str(""); - } - MNode::Phant { show, zero_wid, zero_asc, zero_desc, e } => { - let mut pad = String::new(); - for (z, a) in [(zero_wid, "width"), (zero_asc, "height"), (zero_desc, "depth")] { - if *z { - pad.push_str(&format!(" {a}=\"0\"")); - } - } - if !pad.is_empty() { - o.push_str(&format!("")); - } - o.push_str(if *show { "" } else { "" }); - arg(e, o, depth, false); - o.push_str(if *show { "" } else { "" }); - if !pad.is_empty() { - o.push_str(""); - } - } - } -} - -/// Combining accents (how equations keep them) and the spacing characters MathML puts over the -/// base, both ways. -const ACCENTS: &[(char, char)] = &[ - ('\u{0302}', '^'), - ('\u{0303}', '~'), - ('\u{0304}', '¯'), - ('\u{0305}', '\u{203E}'), - ('\u{0306}', '˘'), - ('\u{0307}', '˙'), - ('\u{0308}', '¨'), - ('\u{030C}', 'ˇ'), - ('\u{0301}', '´'), - ('\u{0300}', '`'), - ('\u{030A}', '˚'), - ('\u{20D7}', '→'), - ('\u{20D6}', '←'), - ('\u{20E1}', '↔'), - ('\u{20D1}', '⇀'), - ('\u{20D0}', '↼'), -]; - -fn spacing_accent(c: char) -> char { - ACCENTS.iter().find(|(comb, _)| *comb == c).map(|(_, s)| *s).unwrap_or(c) -} - -fn combining_accent(c: char) -> Option { - let c = match c { - 'ˆ' => '^', - '˜' => '~', - '‾' => '\u{203E}', - '\u{20D7}' | '\u{0302}' | '\u{0303}' | '\u{0307}' | '\u{0308}' => return Some(c), - c => c, - }; - ACCENTS.iter().find(|(_, s)| *s == c).map(|(comb, _)| *comb) -} - -// --------------------------------------------------------------------------------------------- -// Reader - -/// Most elements read from one `` element. -pub const MAX_NODES: usize = 20_000; - -/// A parsed MathML element (namespace prefixes dropped, names lowercase). -#[derive(Clone, Debug, Default, PartialEq)] -pub struct El { - pub name: String, - pub attrs: Vec<(String, String)>, - pub children: Vec, -} - -#[derive(Clone, Debug, PartialEq)] -pub enum Item { - El(El), - Text(String), -} - -impl El { - fn attr(&self, name: &str) -> Option<&str> { - self.attrs.iter().find(|(k, _)| k == name).map(|(_, v)| v.as_str()) - } - fn els(&self) -> impl Iterator { - self.children.iter().filter_map(|c| match c { - Item::El(e) => Some(e), - Item::Text(_) => None, - }) - } - /// The text inside, all descendants (depth-limited). - pub fn text(&self) -> String { - let mut s = String::new(); - text_into(self, &mut s, 0); - s - } -} - -fn text_into(e: &El, s: &mut String, depth: usize) { - if depth > MAX_DEPTH { - return; - } - for c in &e.children { - match c { - Item::Text(t) => s.push_str(t), - Item::El(c) => text_into(c, s, depth + 1), - } - } -} - -/// The element name without a namespace prefix (`m:math`, `mml:mfrac`). -pub fn local(name: &str) -> &str { - name.rsplit(':').next().unwrap_or(name) -} - -/// The linear format of a `` element, and whether it's a display equation. `None` when it -/// holds nothing. -pub fn read(math: &El) -> Option<(String, bool)> { - let display = - math.attr("display").is_some_and(|d| d.eq_ignore_ascii_case("block")) || math.attr("mode").is_some_and(|d| d.eq_ignore_ascii_case("display")); - let linear = linear_of(math); - let linear: String = linear.chars().filter(|c| !c.is_control()).collect(); - let linear = linear.trim().to_string(); - (!linear.is_empty()).then_some((linear, display)) -} - -fn linear_of(math: &El) -> String { - // Our own copy carries the linear format exactly. - if let Some(sem) = math.els().find(|e| local(&e.name) == "semantics") { - let ann = |enc: &dyn Fn(&str) -> bool| { - sem.els() - .filter(|e| local(&e.name) == "annotation") - .find(|e| e.attr("encoding").is_some_and(|v| enc(&v.to_ascii_lowercase()))) - .map(El::text) - .filter(|t| !t.trim().is_empty()) - }; - if let Some(t) = ann(&|v| v == UNICODEMATH) { - return t; - } - let tree = to_tree(math); - if !tree.is_empty() { - return to_linear(&tree); - } - if let Some(t) = ann(&|v| v.contains("tex")) { - return to_linear(&wordcraft_doc::math_latex::parse_latex(&t)); - } - return String::new(); - } - to_linear(&to_tree(math)) -} - -/// A MathML element's children as an equation tree. -pub fn to_tree(math: &El) -> Arg { - let mut budget = MAX_NODES; - wordcraft_doc::math::merge_runs(children(math, 0, &mut budget)) -} - -fn children(e: &El, depth: usize, budget: &mut usize) -> Arg { - let els: Vec<&El> = e.els().collect(); - seq(&els, depth, budget) -} - -/// A sequence of elements; an n-ary operator without an operand takes the next element. -fn seq(els: &[&El], depth: usize, budget: &mut usize) -> Arg { - let mut out: Arg = Vec::new(); - let mut i = 0; - while let Some(el) = els.get(i) { - i += 1; - // A function name (`sin`) applies to the next element even without U+2061, - // so it doesn't merge with it into `sinx`. Before an operator (`(`, U+2061) the runs - // merge and the linear format finds the function itself. - if local(&el.name) == "mi" - && let Some(next) = els.get(i).filter(|n| local(&n.name) != "mo") - { - let t = el.text(); - let t = t.trim(); - if FUNCTION_NAMES.contains(&t) && *budget > 0 { - *budget -= 1; - i += 1; - out.push(MNode::Func { name: vec![MNode::Run(MRun::plain(t))], e: elem(next, depth + 1, budget) }); - continue; - } - } - let mut a = elem(el, depth, budget); - if let [MNode::Nary { e, .. }] = a.as_mut_slice() - && e.is_empty() - && let Some(next) = els.get(i) - { - i += 1; - *e = elem(next, depth, budget); - } - out.extend(a); - } - out -} - -/// The `k`th child element as an argument. -fn nth(e: &El, k: usize, depth: usize, budget: &mut usize) -> Arg { - e.els().nth(k).map(|c| elem(c, depth, budget)).unwrap_or_default() -} - -/// A lone operator character (`∑`). -fn lone_char(a: &[MNode]) -> Option { - match a { - [MNode::Run(r)] => { - let mut cs = r.text.trim().chars(); - let c = cs.next()?; - cs.next().is_none().then_some(c) - } - _ => None, - } -} - -/// A plain word (`lim`, `max`). -fn word(a: &[MNode]) -> bool { - matches!(a, [MNode::Run(r)] if r.text.chars().count() > 1 && r.text.chars().all(char::is_alphabetic)) -} - -fn elem(e: &El, depth: usize, budget: &mut usize) -> Arg { - if depth > MAX_DEPTH || *budget == 0 { - return Vec::new(); - } - *budget -= 1; - let d = depth + 1; - match local(&e.name) { - "mi" | "mn" | "mo" | "ms" => { - let t = e.text(); - let t = t.trim(); - if t.is_empty() { - return Vec::new(); - } - let mut r = MRun::new(t); - if local(&e.name) == "mi" && e.attr("mathvariant") == Some("normal") && t.chars().count() == 1 { - r.sty = Some(MSty::Plain); - } - vec![MNode::Run(r)] - } - "mtext" => { - let t = e.text(); - if t.trim().is_empty() { - return Vec::new(); - } - vec![MNode::Run(MRun { text: t, nor: true, ..Default::default() })] - } - "mfrac" => { - let thin = e.attr("linethickness").is_some_and(|t| { - let s = t.trim(); - s.eq_ignore_ascii_case("none") || s.trim_end_matches(|c: char| c.is_alphabetic() || c == '%').trim() == "0" - }); - let kind = if e.attr("bevelled").is_some_and(|b| b.eq_ignore_ascii_case("true")) { - FracKind::Skewed - } else if thin { - FracKind::NoBar - } else { - FracKind::Bar - }; - vec![MNode::Frac { kind, num: nth(e, 0, d, budget), den: nth(e, 1, d, budget) }] - } - "msqrt" => vec![MNode::Rad { deg: Vec::new(), deg_hide: true, e: children(e, d, budget) }], - "mroot" => vec![MNode::Rad { deg: nth(e, 1, d, budget), deg_hide: false, e: nth(e, 0, d, budget) }], - "msup" | "msub" | "msubsup" => { - let name = local(&e.name); - let base = nth(e, 0, d, budget); - let (sub, sup) = match name { - "msup" => (Vec::new(), nth(e, 1, d, budget)), - "msub" => (nth(e, 1, d, budget), Vec::new()), - _ => (nth(e, 1, d, budget), nth(e, 2, d, budget)), - }; - if let Some(c) = lone_char(&base).filter(|c| is_nary_char(*c)) { - return vec![nary(c, LimLoc::SubSup, sub, sup)]; - } - let kind = match name { - "msup" => ScriptKind::Sup, - "msub" => ScriptKind::Sub, - _ => ScriptKind::SubSup, - }; - vec![MNode::Script { kind, base, sub, sup }] - } - "munder" | "mover" | "munderover" => { - let name = local(&e.name); - let base = nth(e, 0, d, budget); - let (under, over) = match name { - "munder" => (nth(e, 1, d, budget), Vec::new()), - "mover" => (Vec::new(), nth(e, 1, d, budget)), - _ => (nth(e, 1, d, budget), nth(e, 2, d, budget)), - }; - if let Some(c) = lone_char(&base).filter(|c| is_nary_char(*c)) { - return vec![nary(c, LimLoc::UndOvr, under, over)]; - } - if name == "munderover" { - let inner = MNode::Lim { upper: false, e: base, lim: under }; - return vec![MNode::Lim { upper: true, e: vec![inner], lim: over }]; - } - let upper = name == "mover"; - let mark = if upper { lone_char(&over) } else { lone_char(&under) }; - match mark { - Some('\u{203E}' | '¯' | '‾') if upper => vec![MNode::Bar { top: true, e: base }], - Some('_' | '\u{203E}' | '¯') if !upper => vec![MNode::Bar { top: false, e: base }], - Some(c @ ('⏞' | '⏟' | '⏜' | '⏝' | '⎴' | '⎵' | '←' | '→' | '↔' | '⇐' | '⇒' | '⇔')) if !word(&base) => { - vec![MNode::GroupChr { chr: c, top: upper, e: base }] - } - Some(c) if upper && combining_accent(c).is_some() => vec![MNode::Acc { chr: combining_accent(c).unwrap_or(c), e: base }], - _ => vec![MNode::Lim { upper, e: base, lim: if upper { over } else { under } }], - } - } - "mmultiscripts" => { - let els: Vec<&El> = e.els().collect(); - let base = els.first().map(|b| elem(b, d, budget)).unwrap_or_default(); - let pre_at = els.iter().position(|c| local(&c.name) == "mprescripts"); - let script = - |k: usize, budget: &mut usize| els.get(k).filter(|c| local(&c.name) != "none").map(|c| elem(c, d, budget)).unwrap_or_default(); - match pre_at { - Some(p) => { - let sub = script(p + 1, budget); - let sup = script(p + 2, budget); - let pre_node = MNode::Script { kind: ScriptKind::Pre, base, sub, sup }; - if p > 1 { - let post_sub = script(1, budget); - let post_sup = script(2, budget); - let kind = match (post_sub.is_empty(), post_sup.is_empty()) { - (true, true) => return vec![pre_node], - (false, true) => ScriptKind::Sub, - (true, false) => ScriptKind::Sup, - (false, false) => ScriptKind::SubSup, - }; - vec![MNode::Script { kind, base: vec![pre_node], sub: post_sub, sup: post_sup }] - } else { - vec![pre_node] - } - } - None => { - let sub = script(1, budget); - let sup = script(2, budget); - let kind = match (sub.is_empty(), sup.is_empty()) { - (true, true) => return base, - (false, true) => ScriptKind::Sub, - (true, false) => ScriptKind::Sup, - (false, false) => ScriptKind::SubSup, - }; - vec![MNode::Script { kind, base, sub, sup }] - } - } - } - "mfenced" => { - let first = |a: &str, dflt: char| e.attr(a).map(|v| v.trim().chars().next()).unwrap_or(Some(dflt)); - let beg = first("open", '('); - let end = first("close", ')'); - let sep = first("separators", ','); - let elems: Vec = e.els().map(|c| elem(c, d, budget)).collect(); - vec![MNode::Delim { beg, end, sep, grow: true, shp_match: false, elems }] - } - "mtable" => { - let mtr_list: Vec<&El> = e.els().filter(|r| matches!(local(&r.name), "mtr" | "mlabeledtr")).take(256).collect(); - let rows: Vec> = - mtr_list.iter().map(|r| r.els().filter(|c| local(&c.name) == "mtd").take(256).map(|c| children(c, d, budget)).collect()).collect(); - if rows.is_empty() { - return Vec::new(); - } - // One column is an equation array only when marked as one (`displaystyle`, alignment - // marks, labelled rows, as TeX's align and our own writer give); else a column vector. - let marked = - e.attr("displaystyle").is_some_and(|v| v.trim() == "true") || e.els().any(|r| local(&r.name) == "mlabeledtr") || has_align_mark(e, 0); - if marked && rows.iter().all(|r| r.len() <= 1) { - return vec![MNode::EqArr { rows: rows.into_iter().map(|mut r| r.pop().unwrap_or_default()).collect() }]; - } - let mut col_jc: Vec = e - .attr("columnalign") - .map(|ca| { - ca.split_whitespace() - .map(|a| match a { - "left" => ColJc::Left, - "right" => ColJc::Right, - _ => ColJc::Center, - }) - .collect() - }) - .unwrap_or_default(); - if col_jc.is_empty() - && let Some(first_row) = mtr_list.first() - { - let cell_aligns: Vec = first_row - .els() - .filter(|c| local(&c.name) == "mtd") - .map(|c| match c.attr("columnalign") { - Some("left") => ColJc::Left, - Some("right") => ColJc::Right, - _ => ColJc::Center, - }) - .collect(); - if cell_aligns.iter().any(|a| *a != ColJc::Center) { - col_jc = cell_aligns; - } - } - vec![MNode::Matrix { rows, col_jc }] - } - "mpadded" => { - let zero_wid = e.attr("width").is_some_and(|w| w.trim() == "0"); - let zero_asc = e.attr("height").is_some_and(|h| h.trim() == "0"); - let zero_desc = e.attr("depth").is_some_and(|d| d.trim() == "0"); - let ch = children(e, d, budget); - if zero_wid || zero_asc || zero_desc { - if let [MNode::Phant { show, e, .. }] = ch.as_slice() { - vec![MNode::Phant { show: *show, zero_wid, zero_asc, zero_desc, e: e.clone() }] - } else { - vec![MNode::Phant { show: true, zero_wid, zero_asc, zero_desc, e: ch }] - } - } else { - ch - } - } - "mphantom" => vec![MNode::Phant { show: false, zero_wid: false, zero_asc: false, zero_desc: false, e: children(e, d, budget) }], - "menclose" => { - let n = e.attr("notation").unwrap_or("longdiv"); - let has = |k: &str| n.split_whitespace().any(|w| w.eq_ignore_ascii_case(k)); - if has("radical") { - return vec![MNode::Rad { deg: Vec::new(), deg_hide: true, e: children(e, d, budget) }]; - } - // An actuarial symbol is the top and right edges. - let actuarial = has("actuarial"); - let is_box = has("box") || has("roundedbox") || has("circle"); - let (top, right) = (has("top") || actuarial, has("right") || actuarial); - let has_border = is_box || top || has("bottom") || has("left") || right; - let strike = - [has("horizontalstrike"), has("verticalstrike"), has("updiagonalstrike") || has("cross"), has("downdiagonalstrike") || has("cross")]; - if has_border || strike.iter().any(|&s| s) { - let hide = if is_box { - [false; 4] - } else if has_border { - [!top, !has("bottom"), !has("left"), !right] - } else { - [true; 4] - }; - return vec![MNode::BorderBox { hide, strike, e: children(e, d, budget) }]; - } - children(e, d, budget) - } - // The first child of `semantics` is the presentation; annotations aren't content. - "semantics" => e.els().next().map(|c| elem(c, d, budget)).unwrap_or_default(), - "annotation" | "annotation-xml" | "mspace" | "none" | "mprescripts" | "maligngroup" | "malignmark" => Vec::new(), - _ => children(e, d, budget), - } -} - -/// Whether `e` holds a `maligngroup` or `malignmark` near the top (in a cell or a row inside it). -fn has_align_mark(e: &El, depth: usize) -> bool { - depth <= 4 && e.els().any(|c| matches!(local(&c.name), "maligngroup" | "malignmark") || has_align_mark(c, depth + 1)) -} - -fn nary(chr: char, lim_loc: LimLoc, sub: Arg, sup: Arg) -> MNode { - let lim_loc = Some(lim_loc).filter(|l| *l != if is_integral(chr) { LimLoc::SubSup } else { LimLoc::UndOvr }); - MNode::Nary { chr, lim_loc, grow: false, sub_hide: sub.is_empty(), sup_hide: sup.is_empty(), sub, sup, e: Vec::new() } -} - -#[cfg(test)] -mod tests { - use super::*; - use wordcraft_doc::math::parse_linear; - - fn ml(linear: &str) -> String { - to_mathml(&parse_linear(linear), false, "") - } - - #[test] - fn runs_split_into_identifiers_numbers_and_operators() { - let m = ml("y=2x"); - assert!(m.starts_with(""), "{m}"); - assert!(m.contains("y=2x"), "{m}"); - assert!(m.contains("y=2x"), "{m}"); - assert!(to_mathml(&parse_linear("x"), true, "").contains("display=\"block\"")); - } - - #[test] - fn structures_become_their_elements() { - for (linear, want) in [ - ("a/b", "ab"), - ("x^2", "x2"), - ("x_s", "xs"), - ("x_i^2", ""), - ("√x", "x"), - ("√(3&x)", "x3"), - ("∑_(i=1)^n▒i", "∑"), - ("∫_0^1▒x", "∫"), - ("(a+b)", "("), - ("sin⁡x", "sin\u{2061}"), - ("■(a&b@c&d)", "ab"), - ] { - let m = ml(linear); - assert!(m.contains(want), "{linear}: {m}"); - } - let acc = to_mathml(&[MNode::Acc { chr: '\u{0302}', e: parse_linear("a") }], false, ""); - assert!(acc.contains("a^"), "{acc}"); - let bar = to_mathml(&[MNode::Bar { top: true, e: parse_linear("a") }], false, ""); - assert!(bar.contains("\u{203E}"), "{bar}"); - let lim = to_mathml(&[MNode::Lim { upper: false, e: parse_linear("lim"), lim: parse_linear("n→∞") }], false, ""); - assert!(lim.contains("limn→∞"), "{lim}"); - let bb = to_mathml(&[MNode::BorderBox { hide: [false; 4], strike: [false; 4], e: parse_linear("a") }], false, ""); - assert!(bb.contains(""), "{bb}"); - let gc = to_mathml(&[MNode::GroupChr { chr: '⏟', top: false, e: parse_linear("a") }], false, ""); - assert!(gc.contains("a⏟"), "{gc}"); - let pre = to_mathml(&[MNode::Script { kind: ScriptKind::Pre, base: parse_linear("C"), sub: parse_linear("n"), sup: vec![] }], false, ""); - assert!(pre.contains("") && pre.contains(""), "{pre}"); - let eq = to_mathml(&[MNode::EqArr { rows: vec![parse_linear("x&=1"), parse_linear("y&=2")] }], false, ""); - assert!(eq.contains("x=1") && !eq.contains("&"), "{eq}"); - let ph = to_mathml(&[MNode::Phant { show: false, zero_wid: true, zero_asc: false, zero_desc: false, e: parse_linear("a") }], false, ""); - assert!(ph.contains(""), "{ph}"); - let styled = to_mathml( - &[ - MNode::Run(MRun { text: "R".into(), scr: MScr::DoubleStruck, ..Default::default() }), - MNode::Run(MRun::plain("d")), - MNode::Run(MRun { text: "speed".into(), nor: true, ..Default::default() }), - ], - false, - "", - ); - assert!( - styled.contains("ℝ") && styled.contains("d") && styled.contains("speed"), - "{styled}" - ); - // Text is escaped. - assert!(ml("a<")); - } - - fn pasted(html: &str) -> Vec<(String, bool)> { - let flow = crate::html::parse(html); - let mut out = Vec::new(); - for b in &flow.blocks { - if let crate::model::FBlock::Para(p) = b { - for i in &p.inlines { - if let crate::model::Inline::Equation { linear, display } = i { - out.push((linear.clone(), *display)); - } - } - } - } - out - } - - #[test] - fn html_export_round_trips_equations() { - for (linear, display) in [("y=2x_s", false), ("x=(-b±√(b^2-4ac))/2a", true), ("∑_(k=1)^n▒k", false), ("■(a&b@c&d)", false)] { - let html = crate::html::inlines_html(&[crate::model::Inline::Equation { linear: linear.into(), display }]); - assert!(html.starts_with("Before {html} after

    ")), vec![(linear.to_string(), display)], "{html}"); - } - } - - #[test] - fn foreign_mathml_is_read_from_its_elements() { - let cases = [ - ("y=2xs", "y=2x_s"), - ("ab+1", "a/(b+1)"), - ("x", "√x"), - ("∑k=1nk", "∑_(k=1)^n▒k"), - ("ex", "e^x"), - // A TeX annotation is used when the presentation is empty. - ("\\frac{1}{2}", "1/2"), - ]; - for (html, want) in cases { - let got = pasted(html); - assert_eq!(got.first().map(|g| g.0.as_str()), Some(want), "{html}"); - } - assert!(pasted("x").first().is_some_and(|g| g.1)); - // Text after the equation still arrives; an unclosed element ends with the input. - let f = crate::html::parse("

    x tail

    "); - assert!(crate::model::to_doc(&f).plain_text(wordcraft_doc::StoryRef::Body).contains("tail")); - assert!(!pasted("

    x").is_empty()); - // Hostile nesting is bounded. - let deep = format!("{}x{}", "".repeat(5000), "".repeat(5000)); - assert!(pasted(&deep).len() <= 1); - } - - #[test] - fn math_over_the_token_cap_is_skipped_to_its_end() { - // Past the cap, the rest of the element is dropped, not read as body text. - let html = format!("

    {}z tail

    ", "y".repeat(MAX_NODES * 2)); - let flow = crate::html::parse(&html); - let mut text = String::new(); - for b in &flow.blocks { - if let crate::model::FBlock::Para(p) = b { - for i in &p.inlines { - if let crate::model::Inline::Text(t, _) = i { - text.push_str(t); - } - } - } - } - assert_eq!(text.trim(), "tail"); - } - - #[test] - fn function_names_apply_without_the_invisible_operator() { - let cases = [ - ("sinx", "sin\u{2061}x"), - ("logab", "log\u{2061}ab"), - ("cos\u{2061}x", "cos\u{2061}x"), - ("abx", "abx"), - ]; - for (html, want) in cases { - assert_eq!(pasted(html).first().map(|g| g.0.as_str()), Some(want), "{html}"); - } - let tree = to_tree(&El { - name: "math".into(), - attrs: vec![], - children: vec![ - Item::El(El { name: "mi".into(), attrs: vec![], children: vec![Item::Text("sin".into())] }), - Item::El(El { name: "mi".into(), attrs: vec![], children: vec![Item::Text("x".into())] }), - ], - }); - assert!(matches!(tree.as_slice(), [MNode::Func { .. }]), "{tree:?}"); - } - - #[test] - fn one_column_tables_are_column_vectors_unless_marked() { - let tree = |html: &str| { - let linear = pasted(html).first().map(|g| g.0.clone()).unwrap_or_default(); - wordcraft_doc::math::parse_linear(&linear) - }; - let vector = tree("ab"); - assert!(matches!(vector.as_slice(), [MNode::Matrix { rows, .. }] if rows.len() == 2), "{vector:?}"); - for marked in [ - "ab", - "a=1b", - ] { - let t = tree(marked); - assert!(matches!(t.as_slice(), [MNode::EqArr { rows }] if rows.len() == 2), "{marked}: {t:?}"); - } - // Our own equation arrays read back as equation arrays from their elements alone. - let ml = to_mathml(&[MNode::EqArr { rows: vec![parse_linear("x=1"), parse_linear("y=2")] }], false, ""); - let ml = ml.replace("application/x-unicodemath", "x-ignored"); - let t = tree(&ml); - assert!(matches!(t.as_slice(), [MNode::EqArr { .. }]), "{ml}: {t:?}"); - } - - #[test] - fn mmultiscripts_and_borderbox_round_trip() { - let html = "X12"; - let p = pasted(html); - assert!(!p.is_empty(), "should parse mmultiscripts with pre and post scripts"); - - let html_box = "x"; - let p_box = pasted(html_box); - assert!(!p_box.is_empty(), "should parse menclose"); - - let html_tbl = "ab"; - let p_tbl = pasted(html_tbl); - assert!(!p_tbl.is_empty(), "should parse mtable with columnalign"); - - let html_pad = "z"; - let p_pad = pasted(html_pad); - assert!(!p_pad.is_empty(), "should parse mpadded phantom"); - } - - #[test] - fn deep_nesting_is_bounded() { - let mut n = parse_linear("x"); - for _ in 0..500 { - n = vec![MNode::Frac { kind: FracKind::Bar, num: n, den: parse_linear("y") }]; - } - let m = to_mathml(&n, false, "z"); - assert!(m.ends_with("
    ")); - } - - #[test] - fn test_mfrac_linethickness_none_and_menclose_notations() { - let el_frac = El { - name: "mfrac".into(), - attrs: vec![("linethickness".into(), "none".into())], - children: vec![ - Item::El(El { name: "mi".into(), attrs: vec![], children: vec![Item::Text("n".into())] }), - Item::El(El { name: "mi".into(), attrs: vec![], children: vec![Item::Text("k".into())] }), - ], - }; - let mut budget = MAX_NODES; - let tree_frac = elem(&el_frac, 0, &mut budget); - assert!(matches!(tree_frac.as_slice(), [MNode::Frac { kind: FracKind::NoBar, .. }]), "{tree_frac:?}"); - - let el_strike = El { - name: "menclose".into(), - attrs: vec![("notation".into(), "horizontalstrike".into())], - children: vec![Item::El(El { name: "mi".into(), attrs: vec![], children: vec![Item::Text("x".into())] })], - }; - let mut budget = MAX_NODES; - let tree_strike = elem(&el_strike, 0, &mut budget); - assert!(matches!(tree_strike.as_slice(), [MNode::BorderBox { strike: [true, false, false, false], .. }]), "{tree_strike:?}"); - - let el_cross = El { - name: "menclose".into(), - attrs: vec![("notation".into(), "cross".into())], - children: vec![Item::El(El { name: "mi".into(), attrs: vec![], children: vec![Item::Text("x".into())] })], - }; - let mut budget = MAX_NODES; - let tree_cross = elem(&el_cross, 0, &mut budget); - assert!(matches!(tree_cross.as_slice(), [MNode::BorderBox { strike: [false, false, true, true], .. }]), "{tree_cross:?}"); - - let el_rad = El { - name: "menclose".into(), - attrs: vec![("notation".into(), "radical".into())], - children: vec![Item::El(El { name: "mi".into(), attrs: vec![], children: vec![Item::Text("x".into())] })], - }; - let mut budget = MAX_NODES; - let tree_rad = elem(&el_rad, 0, &mut budget); - assert!(matches!(tree_rad.as_slice(), [MNode::Rad { deg_hide: true, .. }]), "{tree_rad:?}"); - } +pub fn parse(html_or_math: &str) -> Option> { + let ml = craft_math::mathml::parse(html_or_math)?; + let tree = craft_math::mathml::to_tree(&ml); + let linear = craft_math::math::to_linear(&tree); + Some(wordcraft_doc::math::parse_linear(&linear)) } diff --git a/crates/formats/src/mht.rs b/crates/formats/src/mht.rs index 3ab3d91e36..70683f61c8 100644 --- a/crates/formats/src/mht.rs +++ b/crates/formats/src/mht.rs @@ -1,486 +1,62 @@ -//! MHT / MHTML (Single File Web Page): a web page and its pictures in one MIME message (RFC 2557 -//! `multipart/related`, RFC 2045 transfer encodings). The page is the part the message's `start` -//! parameter names, else its first HTML part; it is read by [`crate::html`], with `img` sources -//! found among the other parts by `Content-Location` (also relative to the page's) or `cid:` -//! `Content-ID`, and the page opens as Word opens one ([`crate::html::import_page`]). Word keeps -//! HTML `w:altChunk`s in this form too. +//! MHT / MHTML (Single File Web Page) import via `craft-mime` and `craft-html`. -use std::collections::HashMap; use std::sync::Arc; -use wordcraft_doc::Document; +use craft_mime::{Body, Part}; +use wordcraft_doc::{Document, SectionProps}; -/// Most parts read from one message (nested multiparts included). -const MAX_PARTS: usize = 10_000; -/// Deepest `multipart` nesting followed. -const MAX_NESTING: usize = 8; -/// Longest header block looked at for one part. -const MAX_HEADER: usize = 64 * 1024; -/// Longest boundary (RFC 2046 §5.1.1). -const MAX_BOUNDARY: usize = 70; /// Picture bytes one page may load, counting every `img` that names a part. const PICTURE_BUDGET: usize = 1 << 30; -/// One leaf part of the message: its headers (names lower-cased) and decoded body. -struct Part { - content_type: String, - location: Option, - id: Option, - /// Shared by every `img` that names it. - body: Arc>, -} - -impl Part { - fn mime(&self) -> String { - self.content_type.split(';').next().unwrap_or("").trim().to_ascii_lowercase() - } -} - /// Does `bytes` look like a MIME message (an `.mht` file)? pub fn is_mht(bytes: &[u8]) -> bool { let head = String::from_utf8_lossy(bytes.get(..bytes.len().min(4096)).unwrap_or(bytes)).to_ascii_lowercase(); head.starts_with("mime-version:") || head.starts_with("content-type: multipart/") || head.contains("\ncontent-type: multipart/related") } -/// Import an MHT file. A message without an HTML part reads as its text (an empty document -/// when there's none). +/// Import an MHT file. pub fn import(bytes: &[u8]) -> Document { - import_on(bytes, &wordcraft_doc::SectionProps::default()) + import_on(bytes, &SectionProps::default()) } -/// [`import`] onto `page`: like HTML, an MHT file gives no page size (#1381). -pub fn import_on(bytes: &[u8], page: &wordcraft_doc::SectionProps) -> Document { - let mut parts = Vec::new(); - let (headers, body) = split_headers(bytes); - let start = param(header(&headers, "content-type").unwrap_or(""), "start").map(|s| s.trim_matches(['<', '>']).to_string()); - collect(&headers, body, &mut parts, 0); - let root = start - .as_deref() - .and_then(|s| parts.iter().position(|p| p.id.as_deref() == Some(s))) - .or_else(|| parts.iter().position(|p| matches!(p.mime().as_str(), "text/html" | "application/xhtml+xml"))); - let Some(root) = root.and_then(|i| parts.get(i)) else { - let text = parts.iter().find(|p| p.mime() == "text/plain").map(|p| p.body.as_slice()).unwrap_or(&[]); - return crate::txt::import_on(text, page); - }; - let html = crate::html::decode_as(&root.body, param(&root.content_type, "charset")); - let base = root.location.as_deref().unwrap_or(""); - // Parts by `Content-Location` (lower case) and `Content-ID`; the first of each wins. - let mut by_location = HashMap::new(); - let mut by_id = HashMap::new(); - for p in &parts { - if let Some(l) = &p.location { - by_location.entry(l.to_ascii_lowercase()).or_insert(p); - } - if let Some(i) = &p.id { - by_id.entry(i.as_str()).or_insert(p); +fn collect_leaves<'a>(part: &'a Part, out: &mut Vec<&'a Part>) { + match &part.body { + Body::Data(_) => out.push(part), + Body::Parts(parts) => { + for p in parts { + collect_leaves(p, out); + } } } +} + +fn part_bytes(part: &Part) -> &[u8] { + match &part.body { + Body::Data(b) => b, + Body::Parts(_) => &[], + } +} + +/// [`import`] onto `page`. +pub fn import_on(bytes: &[u8], page: &SectionProps) -> Document { + let msg = craft_mime::parse(bytes, &craft_mime::Limits::DEFAULT); + let mut leaves = Vec::new(); + collect_leaves(&msg.root, &mut leaves); + let root = craft_mime::start_part(&msg.root) + .or_else(|| leaves.iter().copied().find(|p| matches!(p.declared_media_type(), Some("text/html" | "application/xhtml+xml")))); + let Some(root) = root else { + let text = leaves.iter().copied().find(|p| p.declared_media_type().unwrap_or("text/plain") == "text/plain").map(part_bytes).unwrap_or(&[]); + return crate::txt::import_on(text, page); + }; + let html = crate::html::decode_as(part_bytes(root), root.content_type.get("charset")); + let base = root.content_location().unwrap_or(""); + let resources = craft_mime::Resources::of(&msg.root); let left = std::cell::Cell::new(PICTURE_BUDGET); let loader = |src: &str| -> Option>> { - let src = src.trim(); - let part = match src.get(..4).filter(|s| s.eq_ignore_ascii_case("cid:")).and(src.get(4..)) { - Some(cid) => by_id.get(cid), - // By its location as written, or resolved against the page's. - None => by_location.get(&src.to_ascii_lowercase()).or_else(|| by_location.get(&resolve(base, src).to_ascii_lowercase())), - }?; - left.set(left.get().checked_sub(part.body.len())?); - Some(part.body.clone()) + let part = resources.find(src.trim(), base)?; + let data = part_bytes(part); + left.set(left.get().checked_sub(data.len())?); + Some(Arc::new(data.to_vec())) }; crate::html::import_on(&html, &loader, page) } - -/// `reference` resolved against the page's location `base`, as RFC 2557 §5 asks (RFC 3986 -/// §5.2.2): a reference with a scheme stays as it is, `//host/…` takes the base's scheme, `/…` -/// its scheme and host, an empty path its path and query, anything else is merged with its path -/// (§5.2.3); `.` and `..` segments go (§5.2.4) and fragments are dropped. Nothing is fetched. -fn resolve(base: &str, reference: &str) -> String { - let (b, r) = (UriParts::of(base), UriParts::of(reference)); - let t = if r.scheme.is_some() { - UriParts { path: remove_dots(&r.path), ..r } - } else if r.authority.is_some() { - UriParts { scheme: b.scheme, path: remove_dots(&r.path), ..r } - } else if r.path.is_empty() { - UriParts { query: r.query.or(b.query), ..b } - } else if r.path.starts_with('/') { - UriParts { path: remove_dots(&r.path), query: r.query, ..b } - } else { - let merged = if b.authority.is_some() && b.path.is_empty() { - format!("/{}", r.path) - } else { - format!("{}{}", b.path.rfind('/').and_then(|i| b.path.get(..=i)).unwrap_or(""), r.path) - }; - UriParts { path: remove_dots(&merged), query: r.query, ..b } - }; - t.to_string() -} - -/// A URI's parts (RFC 3986 §3): `scheme:`, `//authority`, path and `?query`; the fragment left out. -struct UriParts<'a> { - scheme: Option<&'a str>, - authority: Option<&'a str>, - path: String, - query: Option<&'a str>, -} - -impl<'a> UriParts<'a> { - fn of(uri: &'a str) -> Self { - let uri = uri.split('#').next().unwrap_or(""); - // A one-letter scheme is a Windows drive (`C:/…`), not a scheme. - let scheme = uri.split_once(':').map(|(k, _)| k).filter(|k| { - k.len() > 1 && k.starts_with(|c: char| c.is_ascii_alphabetic()) && k.chars().all(|c| c.is_ascii_alphanumeric() || "+-.".contains(c)) - }); - let rest = scheme.and_then(|k| uri.get(k.len() + 1..)).unwrap_or(uri); - let (authority, rest) = match rest.strip_prefix("//") { - Some(r) => { - let end = r.find(['/', '?']).unwrap_or(r.len()); - (r.get(..end), r.get(end..).unwrap_or("")) - } - None => (None, rest), - }; - let (path, query) = match rest.split_once('?') { - Some((p, q)) => (p, Some(q)), - None => (rest, None), - }; - UriParts { scheme, authority, path: path.to_string(), query } - } -} - -impl std::fmt::Display for UriParts<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if let Some(s) = self.scheme { - write!(f, "{s}:")?; - } - if let Some(a) = self.authority { - write!(f, "//{a}")?; - } - f.write_str(&self.path)?; - if let Some(q) = self.query { - write!(f, "?{q}")?; - } - Ok(()) - } -} - -/// `path` without its `.` and `..` segments (RFC 3986 §5.2.4); `..` stops at the root. -fn remove_dots(path: &str) -> String { - let mut out: Vec<&str> = Vec::new(); - let segments: Vec<&str> = path.split('/').collect(); - for (i, seg) in segments.iter().enumerate() { - match *seg { - "." => {} - ".." => { - if out.len() > 1 || out.first().is_some_and(|f| !f.is_empty()) { - out.pop(); - } - } - s => out.push(s), - } - if i + 1 == segments.len() && matches!(*seg, "." | "..") { - out.push(""); - } - } - out.join("/") -} - -/// Split a part into its unfolded header lines and its body (after the first empty line). -fn split_headers(b: &[u8]) -> (Vec<(String, String)>, &[u8]) { - // Whichever blank line comes first ends the headers. - let blank = [find_bytes(b, b"\r\n\r\n").map(|i| (i, 4)), find_bytes(b, b"\n\n").map(|i| (i, 2))].into_iter().flatten().min(); - let (head, body) = match blank { - Some((i, n)) => (b.get(..i).unwrap_or(&[]), b.get(i + n..).unwrap_or(&[])), - // Headers only, or a body without them. - None if b.first().is_some_and(|c| c.is_ascii_alphabetic()) && find_bytes(b, b":").is_some() => (b, &[][..]), - None => (&[][..], b), - }; - let head = String::from_utf8_lossy(head.get(..head.len().min(MAX_HEADER)).unwrap_or(head)); - let mut headers: Vec<(String, String)> = Vec::new(); - for line in head.lines() { - if line.starts_with([' ', '\t']) { - if let Some((_, v)) = headers.last_mut() { - v.push(' '); - v.push_str(line.trim()); - } - } else if let Some((k, v)) = line.split_once(':') { - headers.push((k.trim().to_ascii_lowercase(), v.trim().to_string())); - } - } - (headers, body) -} - -fn header<'a>(h: &'a [(String, String)], name: &str) -> Option<&'a str> { - h.iter().find(|(k, _)| k == name).map(|(_, v)| v.as_str()) -} - -/// A `; name=value` parameter of a header value (quotes removed), name case-insensitive. -fn param<'a>(value: &'a str, name: &str) -> Option<&'a str> { - // Split outside quoted strings and comments (RFC 2045 §5.1, RFC 822 §3.3). - let mut quoted = false; - let mut escaped = false; - let mut comment_depth = 0usize; - value - .split(|c| { - if escaped { - escaped = false; - } else if (quoted || comment_depth > 0) && c == '\\' { - escaped = true; - } else if !quoted && c == '(' { - comment_depth = comment_depth.saturating_add(1); - } else if comment_depth > 0 { - if c == ')' { - comment_depth -= 1; - } - } else if c == '"' { - quoted = !quoted; - } - c == ';' && !quoted && comment_depth == 0 - }) - .skip(1) - .find_map(|p| { - let (k, v) = p.split_once('=')?; - k.trim().eq_ignore_ascii_case(name).then(|| v.trim().trim_matches('"')) - }) -} - -/// Gather the leaf parts of a (possibly multipart) entity. -fn collect(headers: &[(String, String)], body: &[u8], out: &mut Vec, depth: usize) { - if out.len() >= MAX_PARTS { - return; - } - let ct = header(headers, "content-type").unwrap_or("text/plain").to_string(); - if ct.trim_start().to_ascii_lowercase().starts_with("multipart/") { - let Some(boundary) = param(&ct, "boundary").filter(|b| !b.is_empty() && b.len() <= MAX_BOUNDARY) else { return }; - if depth >= MAX_NESTING { - return; - } - for chunk in multipart_bodies(body, boundary.as_bytes()) { - let (h, b) = split_headers(chunk); - collect(&h, b, out, depth + 1); - if out.len() >= MAX_PARTS { - return; - } - } - return; - } - let enc = header(headers, "content-transfer-encoding").unwrap_or("").trim().to_ascii_lowercase(); - let body = match enc.as_str() { - "quoted-printable" => quoted_printable(body), - "base64" => crate::model::base64_decode(&String::from_utf8_lossy(body)).unwrap_or_default(), - _ => body.to_vec(), - }; - let clean = |s: &str| s.trim().trim_matches(['<', '>']).to_string(); - out.push(Part { - content_type: ct, - location: header(headers, "content-location").map(clean), - id: header(headers, "content-id").map(clean), - body: Arc::new(body), - }); -} - -/// The bodies between `--boundary` lines, up to the closing `--boundary--`. -fn multipart_bodies<'a>(body: &'a [u8], boundary: &[u8]) -> Vec<&'a [u8]> { - let mut delim = b"--".to_vec(); - delim.extend_from_slice(boundary); - let mut out = Vec::new(); - // Delimiters sit at line starts. - let mut starts = Vec::new(); - let mut i = 0; - while let Some(k) = body.get(i..).and_then(|rest| find_bytes(rest, &delim)) { - let at = i + k; - if at == 0 || body.get(at - 1) == Some(&b'\n') { - starts.push(at); - if starts.len() > MAX_PARTS { - break; - } - } - i = at + delim.len(); - } - for w in starts.windows(2) { - let (Some(&a), Some(&b)) = (w.first(), w.get(1)) else { continue }; - let Some(seg) = body.get(a + delim.len()..b) else { continue }; - if seg.starts_with(b"--") { - break; - } - // The rest of the delimiter line, then the part; the line break before the next - // delimiter belongs to it. - let seg = match find_bytes(seg, b"\n") { - Some(n) => seg.get(n + 1..).unwrap_or(&[]), - None => &[], - }; - let seg = seg.strip_suffix(b"\r\n").or_else(|| seg.strip_suffix(b"\n")).unwrap_or(seg); - out.push(seg); - if out.len() >= MAX_PARTS { - break; - } - } - out -} - -fn find_bytes(hay: &[u8], needle: &[u8]) -> Option { - if needle.is_empty() { - return None; - } - hay.windows(needle.len()).position(|w| w == needle) -} - -/// Decode quoted-printable (RFC 2045 §6.7): `=XX` bytes and `=` soft line breaks; anything -/// malformed is kept as it stands. -fn quoted_printable(b: &[u8]) -> Vec { - let hex = |c: u8| (c as char).to_digit(16).and_then(|d| u8::try_from(d).ok()); - let mut out = Vec::with_capacity(b.len()); - let mut i = 0; - while let Some(&c) = b.get(i) { - if c != b'=' { - out.push(c); - i += 1; - continue; - } - match (b.get(i + 1).copied(), b.get(i + 2).copied()) { - (Some(b'\r'), Some(b'\n')) => i += 3, - (Some(b'\n'), _) => i += 2, - (Some(h), Some(l)) if let (Some(h), Some(l)) = (hex(h), hex(l)) => { - out.push((h << 4) | l); - i += 3; - } - _ => { - out.push(c); - i += 1; - } - } - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - use wordcraft_doc::{Block, InlineObject}; - - const PNG_1X1: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; - - fn message(html_part: &str) -> String { - format!( - "MIME-Version: 1.0\r\nContent-Type: multipart/related; boundary=\"----=_B\"\r\n\r\n\ - ------=_B\r\n{html_part}\r\n\ - ------=_B\r\nContent-Location: file:///C:/page_files/dot.png\r\nContent-Transfer-Encoding: base64\r\nContent-Type: image/png\r\n\r\n{PNG_1X1}\r\n\ - ------=_B--\r\n" - ) - } - - #[test] - fn reads_the_quoted_printable_page_and_its_pictures() { - let page = "Content-Location: file:///C:/page.htm\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/html; charset=\"utf-8\"\r\n\r\n\ -

    Dobar dan, gospo=C4=91o, ovo je =\r\njedan red.

    "; - let d = import(message(page).as_bytes()); - let text = d.plain_text(wordcraft_doc::StoryRef::Body); - assert!(text.contains("Dobar dan, gospođo, ovo je jedan red."), "{text:?}"); - let pictures = - d.body.iter().filter(|b| matches!(&***b, Block::Para(p) if p.objects.iter().any(|o| matches!(o, InlineObject::Image { .. })))).count(); - assert_eq!(pictures, 1); - assert_eq!(d.media.len(), 1); - } - - #[test] - fn pictures_named_relative_to_the_page_resolve_as_uris() { - // The page in `reports/`, its picture in `images/` (RFC 2557 §5: relative URIs resolve - // against the page's Content-Location). - let m = |src: &str| { - format!( - "MIME-Version: 1.0\r\nContent-Type: multipart/related; boundary=b\r\n\r\n\ - --b\r\nContent-Location: https://example.test/reports/page.html\r\nContent-Type: text/html\r\n\r\n

    x

    \r\n\ - --b\r\nContent-Location: https://example.test/images/logo.png\r\nContent-Transfer-Encoding: base64\r\nContent-Type: image/png\r\n\r\n{PNG_1X1}\r\n--b--\r\n" - ) - }; - for src in [ - "../images/logo.png", - "/images/logo.png", - "./../images/./logo.png", - "//example.test/images/logo.png", - "https://example.test/images/logo.png", - ] { - assert_eq!(import(m(src).as_bytes()).media.len(), 1, "{src}"); - } - // Nothing past the root: `..` there stays at the root. - assert_eq!(import(m("../../../images/logo.png").as_bytes()).media.len(), 1); - assert_eq!(import(m("images/logo.png").as_bytes()).media.len(), 0); - assert_eq!(resolve("https://h/a/b/c.html", "../d/./e.png#f"), "https://h/a/d/e.png"); - assert_eq!(resolve("file:///C:/x/page.htm", "page_files/dot.png"), "file:///C:/x/page_files/dot.png"); - assert_eq!(resolve("", "dot.png"), "dot.png"); - // A host with no path, and a base whose query holds slashes (RFC 3986 §5.2.3: the path - // merges, not the query; an authority with an empty path gets its `/`). - assert_eq!(resolve("https://example.test", "logo.png"), "https://example.test/logo.png"); - assert_eq!(resolve("https://example.test/page.html?redirect=/other/", "logo.png"), "https://example.test/logo.png"); - assert_eq!(resolve("https://h/a/p.html?q", "?r"), "https://h/a/p.html?r"); - assert_eq!(resolve("https://h/a/p.html?q", "x.png?v=1"), "https://h/a/x.png?v=1"); - assert_eq!(resolve("https://h/a/p.html", "mailto:x@y"), "mailto:x@y"); - for page in ["https://example.test", "https://example.test/page.html?redirect=/other/"] { - let m = format!( - "MIME-Version: 1.0\r\nContent-Type: multipart/related; boundary=b\r\n\r\n\ - --b\r\nContent-Location: {page}\r\nContent-Type: text/html\r\n\r\n

    x

    \r\n\ - --b\r\nContent-Location: https://example.test/logo.png\r\nContent-Transfer-Encoding: base64\r\nContent-Type: image/png\r\n\r\n{PNG_1X1}\r\n--b--\r\n" - ); - assert_eq!(import(m.as_bytes()).media.len(), 1, "{page}"); - } - } - - #[test] - fn quoted_parameters_do_not_supply_a_false_boundary() { - for name in [r#"name="notes; boundary=wrong""#, r#"name="notes\"; boundary=wrong""#, r#"name="notes (10\" screen; boundary=wrong""#] { - let page = "Content-Type: text/html\r\n\r\n

    Kept content

    "; - let message = message(page).replace("multipart/related; boundary=", &format!("multipart/related; {name}; boundary=")); - let doc = import(message.as_bytes()); - assert_eq!(doc.plain_text(wordcraft_doc::StoryRef::Body).trim(), "Kept content", "{name}"); - } - } - - #[test] - fn quotes_in_comments_do_not_hide_the_boundary() { - for comment in [r#"(10" screen)"#, r#"(outer (10" screen); boundary=wrong)"#, r#"(10\)" screen; boundary=wrong)"#] { - let page = "Content-Type: text/html\r\n\r\n

    Kept content

    "; - let message = message(page).replace("multipart/related; boundary=", &format!("multipart/related {comment}; boundary=")); - let doc = import(message.as_bytes()); - assert_eq!(doc.plain_text(wordcraft_doc::StoryRef::Body).trim(), "Kept content", "{comment}"); - } - } - - #[test] - fn the_part_charset_decides_the_text() { - let page = "Content-Type: text/html; charset=windows-1250\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n

    =E8lanak

    "; - let d = import(message(page).as_bytes()); - assert_eq!(d.plain_text(wordcraft_doc::StoryRef::Body).trim(), "članak"); - } - - #[test] - fn the_start_part_is_the_page_and_cid_names_pictures() { - let m = format!( - "MIME-Version: 1.0\nContent-Type: multipart/related; type=\"text/html\"; start=\"\"; boundary=b\n\n\ - --b\nContent-Type: text/html\n\n

    Not this one

    \n\ - --b\nContent-ID: \nContent-Type: text/html\n\n

    This one

    \n\ - --b\nContent-ID: \nContent-Transfer-Encoding: base64\nContent-Type: image/png\n\n{PNG_1X1}\n--b--\n" - ); - let d = import(m.as_bytes()); - assert_eq!(d.plain_text(wordcraft_doc::StoryRef::Body).trim(), "This one"); - assert_eq!(d.media.len(), 1); - // Without a page, a message reads as its text. - let d = import(b"MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=b\n\n--b\nContent-Type: text/plain\n\nJust text\n--b--\n"); - assert_eq!(d.plain_text(wordcraft_doc::StoryRef::Body).trim(), "Just text"); - assert!(is_mht(m.as_bytes()) && !is_mht(b"

    x

    ")); - } - - #[test] - fn hostile_messages_never_panic() { - for m in [ - &b""[..], - b"MIME-Version: 1.0\r\nContent-Type: multipart/related; boundary=\"\"\r\n\r\n", - b"MIME-Version: 1.0\r\nContent-Type: multipart/related; boundary=x\r\n\r\n--x\r\n--x--", - b"MIME-Version: 1.0\nContent-Type: multipart/related; boundary=x\n\n--x\nContent-Transfer-Encoding: quoted-printable\nContent-Type: text/html\n\n=\n=Z=4", - b"Content-Type: text/html\r\nContent-Transfer-Encoding: base64\r\n\r\n!!!!", - ] { - let _ = import(m); - } - let mut deep = String::from("MIME-Version: 1.0\r\n"); - for i in 0..64 { - deep.push_str(&format!("Content-Type: multipart/related; boundary=b{i}\r\n\r\n--b{i}\r\n")); - } - let _ = import(deep.as_bytes()); - } -} diff --git a/crates/formats/src/model.rs b/crates/formats/src/model.rs index 5ae9584ec2..b4856386a3 100644 --- a/crates/formats/src/model.rs +++ b/crates/formats/src/model.rs @@ -1,190 +1,3 @@ -//! A small "flow" model every text format maps to and from: paragraphs with a kind (heading, -//! quote, code…), list membership, alignment and formatted inline spans; tables of cells holding -//! blocks. [`to_doc`] and [`from_doc`] convert between it and the full [`Document`]. +//! Re-exports of `craft-flow` helpers used by `wordcraft-engine` and crate tests. -use std::sync::Arc; - -use wordcraft_doc::numbering::{AbstractNum, Level, ListKind, Num, levels_for}; -use wordcraft_doc::para::{InlineObject, OBJ, PAGE_BREAK, RubyAlign, Run, ruby_raise}; -use wordcraft_doc::props::{ - Border, BorderStyle, Borders, CellProps, CharProps, NumRef, ParaProps, PropChange, RowProps, TextColor, Underline, VAlign, VMerge, VertAlign, -}; -use wordcraft_doc::section::NumFormat; -use wordcraft_doc::styles::{Style, StyleKind}; -use wordcraft_doc::{Align, Block, Blocks, Document, Paragraph, Revision, RevisionKind, Rgb, Table, para_block}; - -mod color; -mod from_doc; -mod media; -pub mod odf_object; -mod to_doc; -mod types; - -pub use color::parse_color; -pub use from_doc::{ - ListCounter, flow_paras, from_doc, from_doc_tracked, from_doc_with, is_custom_char_style, is_custom_style, is_mono, para_kind, part_flow, - tabs_from_indent, -}; -pub use media::{base64_decode, base64_encode, data_uri, decode_bounded, image_px, make_img, mime_of, percent_decode, sized_img, sniff_image}; -pub use to_doc::{char_props, part_blocks, to_doc, to_doc_in}; -pub use types::{ - Affix, BoxLook, CODE_STYLE, Cell, CellPic, Chain, Chart, Conn, FBlock, FComment, FTable, Flow, FlowOpts, Fmt, FmtChange, Group, Img, Inline, - Kind, Label, ListInfo, MAX_DEPTH, MAX_FONT_NAME, MAX_GROUP_DEPTH, MAX_LINK, MONO_FONT, Member, Meta, OdfObject, Para, RUBY_BASE_SIZE, RevMark, - Spacing, TableExtra, clean_text, equations_as_text, flatten_tables, kept_font, kept_link, ranged_anchors, ruby_as_text, ruby_object, - shows_nothing, -}; -pub(crate) use types::{Dropped, begin_import, dropped}; - -thread_local! { - /// The kinds of [`Dropped`] already reported for the document being imported on this thread. - /// Per thread because a document is parsed and built on one, and the parsers drop things - /// from places that have no log of their own to hand. - static REPORTED: std::cell::Cell = const { std::cell::Cell::new(0) }; -} - -#[cfg(test)] -mod tests { - use super::*; - - /// ASK and SET show nothing (layout shows no result for them): no text export writes the - /// result they hold, from the document or from a flow with fields; ODT keeps it. - #[test] - fn ask_and_set_fields_export_no_text() { - let mut doc = wordcraft_doc::Document::from_text("Before after"); - if let Ok(p) = doc.para_mut(wordcraft_doc::StoryRef::Body, &wordcraft_doc::Path::top(0)) { - for instr in ["SET name other", "ASK other \"Prompt\""] { - let field = wordcraft_doc::para::InlineObject::Field { - instr: instr.into(), - result: "SECRETVALUE".into(), - locked: false, - dirty: false, - code: false, - form: String::new(), - }; - p.insert_object(7, field, &Default::default()).unwrap(); - } - } - for (name, out) in [ - ("markdown", crate::markdown::export(&doc)), - ("html", crate::html::export(&doc)), - ("latex", crate::latex::export(&doc)), - ("rtf", crate::rtf::export(&doc)), - ] { - assert!(out.contains("Before"), "{name}: {out}"); - assert!(!out.contains("SECRETVALUE"), "{name}: {out}"); - } - let flow = from_doc_with(&doc, FlowOpts { fields: true, ..Default::default() }); - assert!(!crate::html::export_flow(&flow, "en").contains("SECRETVALUE")); - // The flow keeps the result for ODT. - let kept = flow - .blocks - .iter() - .any(|b| matches!(b, FBlock::Para(p) if p.inlines.iter().any(|i| matches!(i, Inline::Field { result, .. } if result == "SECRETVALUE")))); - assert!(kept); - } - - #[test] - fn percent_decode_requires_hex_digits() { - assert_eq!(percent_decode("%+A"), b"%+A".to_vec()); - assert_eq!(percent_decode("%-A"), b"%-A".to_vec()); - assert_eq!(percent_decode("%G1"), b"%G1".to_vec()); - assert_eq!(percent_decode("%41"), b"A".to_vec()); - } - - #[test] - fn insert_covered_is_bounded_on_hostile_rowspan() { - // Many rows each carrying the maximum rowspan would be O(rows × rowspan) covered cells; - // the work cap must keep insertion bounded (and quick) instead of exhausting memory. - let mut t = FTable { - rows: (0..20_000).map(|_| vec![Cell { rowspan: 1000, ..Default::default() }]).collect(), - widths: Vec::new(), - borderless: false, - extra: None, - kept: None, - wrap: None, - }; - t.insert_covered(); - assert!(t.rows.iter().all(|r| r.len() <= 64), "a row grew past the column cap"); - } - - #[test] - fn base64_round_trip() { - for data in [&b""[..], b"f", b"fo", b"foo", b"foob", b"fooba", b"foobar", &[0, 255, 128, 7]] { - let e = base64_encode(data); - assert_eq!(base64_decode(&e).unwrap(), data, "{e}"); - } - assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy"); - assert!(base64_decode("@@").is_none()); - assert_eq!(data_uri("data:text/plain,a%20b").unwrap(), b"a b"); - } - - #[test] - fn colors() { - assert_eq!(parse_color("#f00"), Some(Rgb(255, 0, 0))); - assert_eq!(parse_color("#f00f"), Some(Rgb(255, 0, 0))); - assert_eq!(parse_color("#0080FF"), Some(Rgb(0, 128, 255))); - assert_eq!(parse_color("#0080FFff"), Some(Rgb(0, 128, 255))); - // A colour with alpha is composited over the white page it lands on (#0080ff at 50%). - assert_eq!(parse_color("#0080ff80"), Some(Rgb(127, 191, 255))); - assert_eq!(parse_color("rgb(1, 2, 3)"), Some(Rgb(1, 2, 3))); - assert_eq!(parse_color("rgb(1 2 3)"), Some(Rgb(1, 2, 3))); - assert_eq!(parse_color("rgb(1 2 3 / 50%)"), Some(Rgb(128, 129, 129))); - assert_eq!(parse_color("hsl(0, 100%, 50%)"), Some(Rgb(255, 0, 0))); - assert_eq!(parse_color("hsl(120deg 100% 50%)"), Some(Rgb(0, 255, 0))); - assert_eq!(parse_color("hsla(240, 100%, 50%, 0.8)"), Some(Rgb(51, 51, 255))); - assert_eq!(parse_color("navy"), Some(Rgb(0, 0, 128))); - assert_eq!(parse_color("#zz"), None); - assert_eq!(parse_color("rgb(1)"), None); - } - - #[test] - fn list_labels() { - let mut c = ListCounter::default(); - let o = |level| ListInfo { ordered: true, level, start: 1, ..Default::default() }; - assert_eq!(c.label(o(0)), "1."); - assert_eq!(c.label(o(1)), "a."); - assert_eq!(c.label(o(1)), "b."); - assert_eq!(c.label(o(0)), "2."); - assert_eq!(c.label(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() }), "•"); - } - - #[test] - fn covered_cells_inserted() { - let mut t = FTable { - rows: vec![vec![Cell { rowspan: 2, ..Default::default() }, Cell::default()], vec![Cell::default()]], - widths: vec![], - borderless: false, - extra: None, - kept: None, - wrap: None, - }; - t.insert_covered(); - assert_eq!(t.rows[1].len(), 2); - assert!(t.rows[1][0].covered); - } - - #[test] - fn trim_drops_a_long_run_of_blank_spans_in_linear_time() { - // Separately formatted line breaks don't merge; each used to be removed with - // `Vec::remove(0)`, shifting all the rest (quadratic). - let mut p = Para::default(); - let (b, i) = (Fmt { bold: true, ..Default::default() }, Fmt { italic: true, ..Default::default() }); - for _ in 0..200_000 { - p.push_text("\n", &b); - p.push_text(" ", &i); - } - p.push_text(" x ", &Fmt::default()); - let t = std::time::Instant::now(); - p.trim(); - // A fraction of a second; `Vec::remove(0)` per span ran past 6 minutes. - assert!(t.elapsed() < std::time::Duration::from_secs(10), "{:?}", t.elapsed()); - assert_eq!(p.inlines, vec![Inline::Text("x".into(), Fmt::default())]); - // The first span left keeps its formatting and loses only its leading blanks. - let mut p = Para::default(); - p.push_text(" \t", &b); - p.push_text(" a ", &i); - p.push_text("b ", &b); - p.trim(); - assert_eq!(p.inlines, vec![Inline::Text("a ".into(), i), Inline::Text("b".into(), b)]); - } -} +pub use craft_flow::*; diff --git a/crates/formats/src/model/color.rs b/crates/formats/src/model/color.rs deleted file mode 100644 index a80fdf60a7..0000000000 --- a/crates/formats/src/model/color.rs +++ /dev/null @@ -1,196 +0,0 @@ -use super::Rgb; - -fn hsl_to_rgb(h: f64, s: f64, l: f64) -> Rgb { - let h = (h / 360.0).rem_euclid(1.0); - let s = s.clamp(0.0, 1.0); - let l = l.clamp(0.0, 1.0); - if s == 0.0 { - let v = (l * 255.0).round().clamp(0.0, 255.0) as u8; - return Rgb(v, v, v); - } - let q = if l < 0.5 { l * (1.0 + s) } else { l + s - l * s }; - let p = 2.0 * l - q; - let hue_to_rgb = |mut t: f64| -> f64 { - if t < 0.0 { - t += 1.0; - } - if t > 1.0 { - t -= 1.0; - } - if t < 1.0 / 6.0 { - return p + (q - p) * 6.0 * t; - } - if t < 1.0 / 2.0 { - return q; - } - if t < 2.0 / 3.0 { - return p + (q - p) * (2.0 / 3.0 - t) * 6.0; - } - p - }; - let r = (hue_to_rgb(h + 1.0 / 3.0) * 255.0).round().clamp(0.0, 255.0) as u8; - let g = (hue_to_rgb(h) * 255.0).round().clamp(0.0, 255.0) as u8; - let b = (hue_to_rgb(h - 1.0 / 3.0) * 255.0).round().clamp(0.0, 255.0) as u8; - Rgb(r, g, b) -} - -/// Composite `rgb` over the white page a paste lands on, at CSS alpha `a` (0..1): `rgba(0, 0, 0, -/// 0.45)` reads like the rgb(140, 140, 140) it looks like on the site, instead of solid black. -fn over_white(r: u8, g: u8, b: u8, a: f32) -> Rgb { - let mix = |c: u8| (c as f32 * a + 255.0 * (1.0 - a)).round().clamp(0.0, 255.0) as u8; - Rgb(mix(r), mix(g), mix(b)) -} - -/// The alpha of a CSS colour's `/ alpha` or 4th-value part, where present: 1.0 (opaque) otherwise. -fn css_alpha(after: Option<&str>) -> f32 { - let Some(a) = after else { return 1.0 }; - let a = a.trim(); - // CSS Colour 4's `none` is a missing component, and a missing alpha is transparent. - if a.eq_ignore_ascii_case("none") { - return 0.0; - } - // A value that is not a finite number (`nan`, `inf`) is no alpha at all: clamp would keep - // NaN, and `over_white` would turn the colour black. - let parse = |t: &str| t.parse::().ok().filter(|f| f.is_finite()); - if let Some(p) = a.strip_suffix('%') { - return parse(p.trim()).map(|f| f.clamp(0.0, 100.0) / 100.0).unwrap_or(1.0); - } - parse(a).map(|f| f.clamp(0.0, 1.0)).unwrap_or(1.0) -} - -/// Parse a CSS / HTML colour: `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb(...)`, `hsl(...)` or a basic name. -/// A colour with alpha is composited over white: the model keeps opaque colours, and a paste lands -/// on a white page. -pub fn parse_color(s: &str) -> Option { - let s = s.trim().to_ascii_lowercase(); - if let Some(h) = s.strip_prefix('#') { - if (h.len() == 3 || h.len() == 4) && h.is_ascii() { - let d = |i: usize| h.get(i..i + 1).and_then(|x| u8::from_str_radix(x, 16).ok()).map(|v| v * 17); - let (r, g, b) = (d(0)?, d(1)?, d(2)?); - // A 4th digit is the alpha, composited over the page like `rgba(...)`'s is. - let a = if h.len() == 4 { d(3)? as f32 / 255.0 } else { 1.0 }; - return Some(over_white(r, g, b, a)); - } - if h.len() == 8 && h.bytes().all(|b| b.is_ascii_hexdigit()) { - let p = |i: usize| h.get(i..i + 2).and_then(|x| u8::from_str_radix(x, 16).ok()); - return Some(over_white(p(0)?, p(2)?, p(4)?, p(6)? as f32 / 255.0)); - } - return Rgb::parse(h); - } - if let Some(inner) = s.strip_prefix("rgb(").or_else(|| s.strip_prefix("rgba(")) { - let inner = inner.trim_end_matches(')').trim(); - let (rgb_part, alpha_part) = inner.split_once('/').map_or((inner, None), |(a, b)| (a, Some(b))); - // Legacy comma syntax carries the alpha as a 4th value (`rgba(0, 0, 0, 0.45)`). - let parts: Vec<&str> = if rgb_part.contains(',') { rgb_part.split(',').collect() } else { rgb_part.split_whitespace().collect() }; - let alpha = match alpha_part { - Some(a) => css_alpha(Some(a)), - None if parts.len() >= 4 => css_alpha(Some(parts[3])), - _ => 1.0, - }; - let v: Vec = parts - .iter() - .take(3) - .filter_map(|x| { - let x = x.trim(); - if let Some(p) = x.strip_suffix('%') { - p.trim().parse::().ok().map(|f| (f.clamp(0.0, 100.0) * 2.55).round() as u8) - } else { - x.parse::().ok().map(|f| f.clamp(0.0, 255.0).round() as u8) - } - }) - .collect(); - if let [r, g, b] = v[..] { - return Some(over_white(r, g, b, alpha)); - } - return None; - } - if let Some(inner) = s.strip_prefix("hsl(").or_else(|| s.strip_prefix("hsla(")) { - let inner = inner.trim_end_matches(')').trim(); - let (hsl_part, alpha_part) = inner.split_once('/').map_or((inner, None), |(a, b)| (a, Some(b))); - let parts: Vec<&str> = if hsl_part.contains(',') { hsl_part.split(',').collect() } else { hsl_part.split_whitespace().collect() }; - let alpha = match alpha_part { - Some(a) => css_alpha(Some(a)), - None if parts.len() >= 4 => css_alpha(Some(parts[3])), - _ => 1.0, - }; - if parts.len() < 3 { - return None; - } - let h_str = parts[0].trim(); - let h: f64 = if let Some(deg) = h_str.strip_suffix("deg") { - deg.trim().parse().ok()? - } else if let Some(rad) = h_str.strip_suffix("rad") { - rad.trim().parse::().ok()? * 180.0 / std::f64::consts::PI - } else if let Some(turn) = h_str.strip_suffix("turn") { - turn.trim().parse::().ok()? * 360.0 - } else { - h_str.parse().ok()? - }; - let parse_pct = |x: &str| -> Option { - let x = x.trim(); - if let Some(p) = x.strip_suffix('%') { - Some((p.trim().parse::().ok()? / 100.0).clamp(0.0, 1.0)) - } else { - Some(x.parse::().ok()?.clamp(0.0, 1.0)) - } - }; - let s = parse_pct(parts[1])?; - let l = parse_pct(parts[2])?; - let Rgb(r, g, b) = hsl_to_rgb(h, s, l); - return Some(over_white(r, g, b, alpha)); - } - Some(match s.as_str() { - "black" => Rgb(0, 0, 0), - "white" => Rgb(255, 255, 255), - "red" => Rgb(255, 0, 0), - "green" => Rgb(0, 128, 0), - "lime" => Rgb(0, 255, 0), - "blue" => Rgb(0, 0, 255), - "yellow" => Rgb(255, 255, 0), - "cyan" | "aqua" => Rgb(0, 255, 255), - "magenta" | "fuchsia" => Rgb(255, 0, 255), - "gray" | "grey" => Rgb(128, 128, 128), - "silver" => Rgb(192, 192, 192), - "maroon" => Rgb(128, 0, 0), - "olive" => Rgb(128, 128, 0), - "navy" => Rgb(0, 0, 128), - "purple" => Rgb(128, 0, 128), - "teal" => Rgb(0, 128, 128), - "orange" => Rgb(255, 165, 0), - _ => return None, - }) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn eight_digit_hex_rejects_signs() { - assert_eq!(parse_color("#+1+2+3+4"), None); - // #11223344 is alpha 0x44 over white. - assert_eq!(parse_color("#11223344"), Some(Rgb(192, 196, 201))); - } - - #[test] - fn colors_with_alpha_composite_over_white() { - // The secondary text colour of dark-theme sites: solid black would read as the site's - // own emphasis, white is invisible on the page; the composite is what the eye saw. - assert_eq!(parse_color("rgba(0, 0, 0, 0.45)"), Some(Rgb(140, 140, 140))); - assert_eq!(parse_color("rgb(0 0 0 / 0.45)"), Some(Rgb(140, 140, 140))); - assert_eq!(parse_color("rgba(0, 0, 0, 0)"), Some(Rgb(255, 255, 255))); - assert_eq!(parse_color("rgba(255, 0, 0, 1)"), Some(Rgb(255, 0, 0))); - assert_eq!(parse_color("rgba(255, 0, 0, 45%)"), Some(Rgb(255, 140, 140))); - assert_eq!(parse_color("hsla(120, 100%, 50%, 0.5)"), Some(Rgb(128, 255, 128))); - // A 4-digit hex's last digit is the alpha, too: #0008 is black at 8/15 over white. - assert_eq!(parse_color("#0008"), Some(Rgb(119, 119, 119))); - // `none` is a missing alpha, and a missing alpha is transparent, not the opaque default - // an omitted alpha takes. - assert_eq!(parse_color("rgb(0 0 0 / none)"), Some(Rgb(255, 255, 255))); - assert_eq!(parse_color("rgb(0 0 0)"), Some(Rgb(0, 0, 0))); - // An alpha that is not a finite number is no alpha: the colour stays opaque, not black. - assert_eq!(parse_color("rgba(0, 0, 0, nan)"), Some(Rgb(0, 0, 0))); - assert_eq!(parse_color("rgba(0, 0, 0, inf)"), Some(Rgb(0, 0, 0))); - assert_eq!(parse_color("rgba(0, 0, 0, nan%)"), Some(Rgb(0, 0, 0))); - } -} diff --git a/crates/formats/src/model/from_doc.rs b/crates/formats/src/model/from_doc.rs deleted file mode 100644 index ae925ac1c2..0000000000 --- a/crates/formats/src/model/from_doc.rs +++ /dev/null @@ -1,1251 +0,0 @@ -use super::types::{ - BoxLook, Cell, FBlock, FComment, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, MAX_GROUP_DEPTH, Member, Meta, - Para, RevMark, Spacing, -}; -use super::{ - Arc, Block, Blocks, CharProps, Document, InlineObject, NumFormat, OBJ, PAGE_BREAK, ParaProps, Paragraph, StyleKind, Table, TextColor, Underline, - VMerge, VertAlign, -}; - -pub(super) fn uses_kind(blocks: &[FBlock], k: Kind, depth: usize) -> bool { - depth <= MAX_DEPTH - && blocks.iter().any(|b| match b { - FBlock::Para(p) => p.kind == k, - FBlock::Table(t) => t.rows.iter().flatten().any(|c| uses_kind(&c.blocks, k, depth + 1)), - }) -} - -// --------------------------------------------------------------------------------------------- -// Document → Flow - -/// Is this a monospace family name? -pub fn is_mono(font: &str) -> bool { - let f = font.to_ascii_lowercase(); - f.contains("courier") || f.contains("mono") || f.contains("consolas") || f.contains("menlo") || f.contains("code") || f == "monaco" -} - -fn is_code_style(doc: &Document, id: &str) -> bool { - let names = |s: &str| { - let l = s.to_ascii_lowercase().replace([' ', '-', '_'], ""); - l == "code" || l.contains("preformatted") || l == "plaintext" || l == "codeblock" || l == "sourcecode" - }; - doc.styles.chain(id).iter().any(|s| names(&s.id) || names(&s.name)) || names(id) -} - -/// Character formatting from a run's character style chain plus direct formatting (the -/// paragraph style is left out: it is expressed by the paragraph kind). -fn effective(doc: &Document, cp: &CharProps) -> CharProps { - let mut out = CharProps::default(); - if let Some(cs) = cp.style.as_deref() { - for s in doc.styles.chain(cs) { - if s.kind == StyleKind::Character { - out.overlay(&s.chr); - } - } - } - let mut direct = cp.clone(); - direct.style = None; - out.overlay(&direct); - out -} - -fn fmt_of(doc: &Document, cp: &CharProps, in_code: bool) -> Fmt { - let e = effective(doc, cp); - let code = !in_code && e.font.as_deref().is_some_and(is_mono); - Fmt { - bold: e.bold.unwrap_or(false), - italic: e.italic.unwrap_or(false), - underline: e.underline.is_some_and(|u| u != Underline::None) && e.link.is_none(), - strike: e.strike.unwrap_or(false) || e.double_strike.unwrap_or(false), - code, - sup: e.vert_align == Some(VertAlign::Superscript), - sub: e.vert_align == Some(VertAlign::Subscript), - link: e.link.as_deref().map(Arc::from), - color: match e.color { - Some(TextColor::Rgb(c)) => Some(c), - _ => None, - }, - size: e.size, - font: if code || in_code { None } else { e.shown_font().map(Arc::from) }, - background: e.highlight.and_then(|h| h.rgb()).or(e.shading), - ins: None, - del: None, - fmt_change: None, - extra: None, - } -} - -/// The flow formatting of a run with properties `props`: with `opts.chars`, its extra formatting, -/// its custom character style by name (only its direct formatting then its own) and a -/// monospaced font by its name. -fn run_fmt(doc: &Document, para_style: Option<&str>, props: &CharProps, in_code: bool, opts: FlowOpts) -> Fmt { - let mut f = fmt_of(doc, props, in_code); - if !opts.chars { - return f; - } - let custom = props.style.as_deref().filter(|id| is_custom_char_style(doc, id)); - let direct; - let props_now = match custom { - Some(_) => { - direct = CharProps { style: None, ..props.clone() }; - f = fmt_of(doc, &direct, in_code); - &direct - } - None => props, - }; - let e = effective(doc, props_now); - f.extra = extra_of(&e); - // Kerning on or off where it differs from the document's default. - let kerns = |k: Option| k.is_some_and(|k| k.is_finite() && k > 0.0); - let (kern, default) = (kerns(e.kern.or(doc.styles.default_chr.kern)), kerns(doc.styles.default_chr.kern)); - if kern != default { - f.extra.get_or_insert_with(Default::default).kern = Some(if kern { 1.0 } else { 0.0 }); - } - if let Some(id) = custom { - f.extra.get_or_insert_with(Default::default).style = Some(id.to_string()); - } - // A size stated without a complex-script size leaves the complex size to what is above it - // (a style's, or this size where none says one): said where it isn't this size, since a - // writer that gives every script the one size would otherwise change it. - let sized = props.size.is_some() || custom.is_some_and(|id| doc.styles.chain(id).iter().any(|s| s.chr.size.is_some())); - if props.size_cs.is_none() && sized && f.extra.as_ref().is_none_or(|x| x.size_cs.is_none()) { - let cs = doc.styles.resolve_char(para_style, props).size_cs; - if f.size != Some(cs) { - f.extra.get_or_insert_with(Default::default).size_cs = Some(cs); - } - } - if f.code { - f.code = false; - f.font = e.shown_font().map(Arc::from); - } - f -} - -/// The formatting in `e` (effective character properties) that only [`Fmt::extra`] holds; -/// `None` when it sets none of it. -fn extra_of(e: &CharProps) -> Option> { - let set = |v: Option| v.filter(|b| *b); - let x = CharProps { - // What it turns off, over what the style of its paragraph or span turns on, too. - bold: e.bold.filter(|b| !*b), - italic: e.italic.filter(|b| !*b), - strike: e.strike.filter(|b| !*b), - underline: e.underline.filter(|u| *u != Underline::Single), - underline_color: e.underline_color, - double_strike: set(e.double_strike), - caps: e.caps, - small_caps: e.small_caps, - hidden: set(e.hidden), - spacing: e.spacing.filter(|v| *v != 0.0 && v.is_finite()), - scale: e.scale.filter(|v| *v != 100.0 && v.is_finite()), - emphasis: e.emphasis.filter(|m| *m != wordcraft_doc::props::EmphasisMark::None), - position: e.position.filter(|v| *v != 0.0 && v.is_finite()), - outline: set(e.outline), - shadow: set(e.shadow), - emboss: set(e.emboss), - engrave: set(e.engrave), - border: e.border.filter(|b| b.is_visible()), - lang: e.lang.clone().filter(|l| !l.is_empty()), - font_cs: e.font_cs.clone().filter(|f| !f.is_empty()), - font_ea: e.font_ea.clone().filter(|f| !f.is_empty()), - size_cs: e.size_cs.filter(|s| s.is_finite()), - bold_cs: e.bold_cs, - italic_cs: e.italic_cs, - lang_bidi: e.lang_bidi.clone().filter(|l| !l.is_empty()), - lang_ea: e.lang_ea.clone().filter(|l| !l.is_empty()), - ..Default::default() - }; - (x != CharProps::default()).then(|| Box::new(x)) -} - -/// Whether character style `id` of `doc` is one of its own rather than one WordCraft gives links, -/// note and comment marks or placeholders. -pub fn is_custom_char_style(doc: &Document, id: &str) -> bool { - let builtin = - ["DefaultParagraphFont", "Hyperlink", "FollowedHyperlink", "FootnoteReference", "EndnoteReference", "CommentReference", "PlaceholderText"]; - !builtin.contains(&id) && doc.styles.get(id).is_some_and(|s| s.kind == wordcraft_doc::styles::StyleKind::Character) -} - -/// Whether paragraph style `id` of `doc` is one of its own rather than one the flow model's -/// paragraph kinds, lists and notes stand for (Normal, the headings, Title, Quote, Code, List -/// Paragraph, the note texts). -pub fn is_custom_style(doc: &Document, id: &str) -> bool { - // The styles the kinds stand for, and the names formats write fixed styles under. - let kinds = - ["Normal", "Title", "Quote", super::types::CODE_STYLE, "FootnoteText", "EndnoteText", "Standard", "Footnote", "Endnote", "Quotations"]; - let heading = id.strip_prefix("Heading").is_some_and(|n| n.len() == 1 && n.chars().all(|c| c.is_ascii_digit())); - !heading && !kinds.contains(&id) && doc.styles.get(id).is_some_and(|s| s.kind == wordcraft_doc::styles::StyleKind::Paragraph) -} - -/// Whether `doc`'s tab stops are measured from each paragraph's indent (an ODF document whose -/// file says so): [`Para::extra`]'s tab stops then are too. -pub fn tabs_from_indent(doc: &Document) -> bool { - doc.settings.odf.is_some_and(|o| o.tabs_from_indent) -} - -/// The paragraph formatting only [`Para::extra`] holds that `p` sets itself; `None` when none. -fn para_extra(doc: &Document, p: &wordcraft_doc::props::ParaProps) -> Option> { - let tabs = p.tabs.clone().filter(|t| !t.is_empty()).map(|mut tabs| { - // Measured from the paragraph's indent where the document measures them so (ODT). - if tabs_from_indent(doc) { - let indent = doc.styles.resolve_para(p).indent_left; - for t in &mut tabs { - t.pos = (t.pos - indent).clamp(-1584.0, 1584.0); - } - } - tabs - }); - let style = p.style.clone().filter(|id| is_custom_style(doc, id)); - // Its own, else its custom style's: the kind doesn't say (it stops at Heading 6, and is the - // heading style's where the style is based on one). - let outline_level = p.outline_level.or_else(|| style.as_ref().and_then(|_| doc.styles.resolve_para(p).outline_level)); - let x = wordcraft_doc::props::ParaProps { - tabs, - shading: p.shading, - style, - contextual_spacing: p.contextual_spacing, - bidi: p.bidi.filter(|b| *b), - drop_cap: p.drop_cap.filter(|l| *l > 1), - drop_cap_distance: p.drop_cap_distance.filter(|_| p.drop_cap.is_some_and(|l| l > 1)), - keep_lines: p.keep_lines.filter(|k| *k), - outline_level, - widow_control: p.widow_control.filter(|w| !*w), - // Hyphenated (the document hyphenates and the paragraph doesn't opt out): `Some(false)`. - suppress_hyphens: (doc.settings.auto_hyphenation && p.suppress_hyphens != Some(true)).then_some(false), - ..Default::default() - }; - (x != wordcraft_doc::props::ParaProps::default()).then(|| Box::new(x)) -} - -/// Revision `i` as a flow mark (unknown author and date when there is none). -fn rev_mark(doc: &Document, i: u32) -> RevMark { - doc.revisions.get(i as usize).map(|r| RevMark { author: r.author.clone(), date: r.date.clone(), id: Some(i) }).unwrap_or_default() -} - -/// The insertion and deletion `cp` tracks, as flow marks. -fn rev_marks(doc: &Document, cp: &CharProps) -> (Option, Option) { - (cp.ins.map(|i| rev_mark(doc, i)), cp.del.map(|i| rev_mark(doc, i))) -} - -/// Paragraph kind and list membership of a document paragraph. -pub fn para_kind(doc: &Document, p: &Paragraph) -> (Kind, Option) { - let blank = p.plain_text().trim().is_empty() && p.objects.iter().all(|o| o.is_marker()); - props_kind(doc, &p.props, blank) -} - -/// Paragraph kind and list membership of paragraph properties; `blank` = the paragraph shows -/// nothing (a bottom border then makes it a rule). -fn props_kind(doc: &Document, props: &ParaProps, blank: bool) -> (Kind, Option) { - let rp = doc.styles.resolve_para(props); - let style = props.style.as_deref().unwrap_or("Normal"); - // A heading's kind is its style's (Heading 2 looks as Heading 2 whatever its outline level, - // which the paragraph may set apart); else its outline level's. - let style_level = - doc.styles.chain(style).iter().find_map(|s| s.id.strip_prefix("Heading").and_then(|n| n.parse::().ok()).filter(|n| (1..=9).contains(n))); - let kind = if let Some(l) = style_level.filter(|_| rp.outline_level.is_some()) { - Kind::Heading(l.min(6)) - } else if let Some(l) = rp.outline_level { - Kind::Heading((l + 1).min(6)) - } else if doc.styles.chain(style).iter().any(|s| s.id == "Title") { - Kind::Title - } else if doc.styles.chain(style).iter().any(|s| s.id == "Quote" || s.id == "IntenseQuote") { - Kind::Quote - } else if is_code_style(doc, style) { - Kind::Code - } else if blank && rp.borders.and_then(|b| b.bottom).is_some_and(|b| b.is_visible()) { - Kind::Rule - } else { - Kind::Normal - }; - let list = rp.numbering.and_then(|n| { - let lv = doc.numbering.level(n.num, n.level)?; - Some(ListInfo { - ordered: !matches!(lv.format, NumFormat::Bullet | NumFormat::None), - level: n.level.min(8), - start: lv.start, - list: n.num, - label: Some(super::types::Label::of_level(lv.format, &lv.text).placed(lv.indent, lv.hanging, lv.align)), - }) - }); - (kind, list) -} - -fn media_ext(key: &str, data: &[u8]) -> String { - match image::guess_format(data) { - Ok(image::ImageFormat::Png) => "png".into(), - Ok(image::ImageFormat::Jpeg) => "jpeg".into(), - Ok(image::ImageFormat::Gif) => "gif".into(), - Ok(image::ImageFormat::WebP) => "webp".into(), - Ok(image::ImageFormat::Bmp) => "bmp".into(), - _ => key.rsplit('.').next().filter(|e| e.len() <= 5 && *e != key).unwrap_or("png").to_ascii_lowercase(), - } -} - -/// The flow inlines for a note reference, comment mark or field `o` of `doc`, when `opts` asks -/// for them; `None` for other objects. `f` is the formatting of the run `o` is in. -fn rich_inlines(doc: &Document, o: &InlineObject, f: &Fmt, opts: FlowOpts) -> Option> { - Some(match o { - InlineObject::NoteRef { kind, id, custom } if opts.notes => { - // The note's content without its own mark (a note holds no notes). - let story = doc.parts.get(id).map(|part| &part.blocks); - let mut blocks = story.map(|b| flow_blocks(doc, b, 0, FlowOpts { notes: false, ..opts })).unwrap_or_default(); - // The space after the mark is the mark's (see `to_doc`'s `note`); a tab or more - // spaces there are the note's own. - if let Some(FBlock::Para(first)) = blocks.first_mut() { - let k = first.inlines.iter().take_while(|i| matches!(i, Inline::Text(t, _) if t.is_empty())).count(); - first.inlines.drain(..k); - if let Some(Inline::Text(t, _)) = first.inlines.first_mut() - && t.starts_with(' ') - { - t.remove(0); - } - } - let endnote = *kind == wordcraft_doc::para::NoteKind::Endnote; - vec![Inline::Note { endnote, custom: custom.clone(), blocks, fmt: f.clone() }] - } - // The comment, then the replies in its thread (replies to replies too, each answering the - // comment that starts the thread), which have no place of their own. - InlineObject::CommentStart { id } if opts.comments => { - let Some(c) = doc.comments.get(id) else { return Some(Vec::new()) }; - let mut out = vec![Inline::Comment { id: *id, comment: Box::new(flow_comment(doc, c, None, opts)) }]; - let mut thread = std::collections::BTreeSet::from([*id]); - let mut k = 0; - while let Some(Inline::Comment { id: at, .. }) = out.get(k) { - let at = *at; - k += 1; - for (rid, r) in doc.comments.iter().filter(|(_, r)| r.parent == Some(at)) { - if thread.insert(*rid) { - out.push(Inline::Comment { id: *rid, comment: Box::new(flow_comment(doc, r, Some(*id), opts)) }); - } - } - } - out - } - InlineObject::CommentEnd { id } if opts.comments => { - if doc.comments.contains_key(id) { - vec![Inline::CommentEnd(*id)] - } else { - Vec::new() - } - } - // A text box's content, without notes, comments or boxes of its own. - // A drawing shape, text box or group (pictures have their own place below). - InlineObject::Shape { .. } | InlineObject::Group { .. } if opts.boxes => vec![drawing_inline(doc, o, opts, 0, (1.0, 1.0))?], - InlineObject::FieldStart { instr, .. } if opts.fields => vec![Inline::FieldStart(instr.clone())], - InlineObject::FieldEnd if opts.fields => vec![Inline::FieldEnd], - // Where a bookmark ends, so it covers its text (Word's `_GoBack` is the last edit's spot). - InlineObject::BookmarkEnd { name } if opts.fields && name != "_GoBack" => vec![Inline::AnchorEnd(name.clone())], - InlineObject::Field { instr, result, locked, .. } if opts.fields => { - vec![Inline::Field { instr: instr.clone(), result: result.clone(), fmt: f.clone(), locked: *locked }] - } - _ => return None, - }) -} - -/// Comment `c` (a reply to `parent`) of `doc` as a flow comment: its text holds no comments or -/// notes of its own. -/// The flow inline for picture, shape or group `o` of `doc` (a member of groups `depth` deep), -/// its size scaled by `scale` (a group's members are in its own coordinates). A shape or text -/// box keeps its text, but not notes, comments or boxes of its own. `None` for what a flow has -/// no drawing for: a freeform's paths, a picture whose data is missing. -fn drawing_inline(doc: &Document, o: &InlineObject, opts: FlowOpts, depth: usize, scale: (f32, f32)) -> Option { - use wordcraft_doc::para::ShapeKind; - let (sx, sy) = scale; - match o { - // A freeform needs its paths. - InlineObject::Shape { kind, w, h, float, story, fill, stroke, stroke_width, extra, freeform, .. } - if *kind != ShapeKind::Freeform || freeform.is_some() => - { - // A connector stays one in formats that glue them (ODT, with boxes); elsewhere it is a - // straight line. - let kind = match kind { - ShapeKind::StraightConnector | ShapeKind::ElbowConnector | ShapeKind::CurvedConnector if !opts.boxes => ShapeKind::Line, - k => *k, - }; - let inner = FlowOpts { notes: false, comments: false, boxes: false, ..opts }; - let part = story.and_then(|id| doc.parts.get(&id)); - let blocks = part.map(|p| flow_blocks(doc, &p.blocks, 0, inner)).unwrap_or_default(); - let look = BoxLook { - fill: fill.clone().filter(|_| kind != ShapeKind::Line), - line: stroke.filter(|_| stroke_width.is_finite() && *stroke_width > 0.0).map(|c| (c, stroke_width.min(12.0))), - dash: extra.dash.clone(), - arrows: (extra.arrow_start, extra.arrow_end), - inset: part.and_then(|p| p.body.inset.is_some().then(|| p.body.insets_pt())), - auto_fit: part.is_some_and(|p| p.body.auto_fit), - vert: part.map(|p| p.body.vert).unwrap_or_default(), - // Kept only while it is still the freeform it was read as. - geometry: extra.odf_geometry.clone().filter(|_| (kind == ShapeKind::Freeform && freeform.is_some()) || kind == ShapeKind::Rectangle), - conn: (opts.boxes && (extra.id != 0 || extra.start.is_some() || extra.end.is_some())) - .then(|| Box::new(crate::model::Conn { id: extra.id, start: extra.start, end: extra.end })), - // A text box is named after its story; a linked one names the box it continues in. - chain: story.filter(|_| opts.boxes).map(|id| { - let next = part.and_then(|p| p.body.next).map(|n| format!("Frame{n}")); - Box::new(crate::model::Chain { name: format!("Frame{id}"), next }) - }), - }; - let path = freeform.clone().filter(|_| kind == ShapeKind::Freeform); - Some(Inline::Shape { kind, w: w * sx, h: h * sy, float: kept_float(float), blocks, look, path }) - } - InlineObject::Image { media, w, h, alt, float, ole, crop, .. } => { - let data = doc.media.get(media)?; - let alt = if o.is_decorative() { "" } else { alt.as_str() }; - Some(Inline::Image(Img { - data: data.clone(), - ext: media_ext(media, data), - w: w * sx, - h: h * sy, - alt: alt.to_string(), - float: kept_float(float), - object: ole.as_deref().filter(|_| opts.boxes).and_then(|e| super::odf_object::load(doc, e)), - crop: if opts.paras { *crop } else { [0.0; 4] }, - })) - } - InlineObject::Group { w, h, float, ch_w, ch_h, children, .. } if depth < MAX_GROUP_DEPTH => { - let ratio = |outer: f32, inner: f32| { - let r = outer / inner; - if r.is_finite() && r > 0.0 { r } else { 1.0 } - }; - let inner = (sx * ratio(*w, *ch_w), sy * ratio(*h, *ch_h)); - let members: Vec = children - .iter() - .filter_map(|c| { - drawing_inline(doc, &c.obj, opts, depth + 1, inner).map(|inline| Member { x: c.x * inner.0, y: c.y * inner.1, inline }) - }) - .collect(); - (!members.is_empty()).then(|| Inline::Group { w: w * sx, h: h * sy, float: kept_float(float), members }) - } - _ => None, - } -} - -/// The placement a flow keeps for a drawing: when it floats, or turns or mirrors it in line. -fn kept_float(f: &wordcraft_doc::para::Float) -> Option> { - let turned = f.rot.is_finite() && f.rot.rem_euclid(360.0) > 0.001 && f.rot.rem_euclid(360.0) < 359.999; - (f.wrap != wordcraft_doc::para::Wrap::Inline || turned || f.flip_h || f.flip_v).then(|| Box::new(f.clone())) -} - -fn flow_comment(doc: &Document, c: &wordcraft_doc::Comment, parent: Option, opts: FlowOpts) -> FComment { - let story = doc.parts.get(&c.part).map(|p| &p.blocks); - let blocks = story.map(|b| flow_blocks(doc, b, 0, FlowOpts { notes: false, comments: false, ..opts })).unwrap_or_default(); - FComment { author: c.author.clone(), initials: c.initials.clone(), date: c.date.clone(), resolved: c.resolved, parent, blocks } -} - -/// Whether `li`, the list of heading `p`, is the number its outline level has from its heading -/// style's list, not a list of its own: it states that level, and the heading style of that level -/// numbers with that list there. Past Heading 6 no such style gives the written outline a number. -fn numbered_by_outline(doc: &Document, p: &Paragraph, li: &ListInfo) -> bool { - use wordcraft_doc::props::NumRef; - p.props.outline_level == Some(li.level) - && li.level < 6 - && doc.styles.para_style_props(&format!("Heading{}", li.level + 1)).0.numbering == Some(NumRef { num: li.list, level: li.level }) -} - -/// A paragraph's list membership as the flow keeps it: without the list's identity and label -/// unless `opts` asks for them, and none for a heading numbered only through its style or -/// [`numbered_by_outline`], which a format with heading numbering of its own (ODT's outline style) -/// numbers instead, in its first six levels. -fn flow_list(doc: &Document, kind: Kind, list: Option, p: &Paragraph, opts: FlowOpts) -> Option { - let mut li = list?; - if !opts.lists { - (li.list, li.label) = (0, None); - } else if matches!(kind, Kind::Heading(_)) && li.level < 6 && (p.props.numbering.is_none() || numbered_by_outline(doc, p, &li)) { - return None; - } - Some(li) -} - -/// The spacing and indents a paragraph sets itself (its style's are not included). -fn direct_spacing(p: &wordcraft_doc::props::ParaProps) -> Spacing { - Spacing { - before: p.space_before, - after: p.space_after, - line: p.line_spacing, - left: p.indent_left, - right: p.indent_right, - first: p.indent_first, - ..Default::default() - } -} - -/// A document paragraph as flow paragraphs: a page break character inside the text ends the -/// paragraph, and the text after it starts a new one that carries `page_break`. -/// An object's text in the flow: ASK and SET show nothing, whatever result they hold. -fn shown_text(o: &InlineObject) -> &str { - match o { - InlineObject::Field { instr, .. } if super::shows_nothing(instr) => "", - o => o.plain_text(), - } -} - -pub fn flow_paras(doc: &Document, p: &Paragraph) -> Vec { - flow_paras_in(doc, p, FlowOpts::default()) -} - -/// [`flow_paras`]; `tracked` keeps tracked changes: deleted text stays, and text carries its -/// insertion and deletion marks. -fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { - let tracked = opts.tracked; - let (kind, list) = para_kind(doc, p); - let list = flow_list(doc, kind, list, p, opts); - let mut done: Vec = Vec::new(); - let borders = if kind == Kind::Rule { None } else { p.props.borders.filter(|b| b.takes_room()) }; - let mut out = Para { - kind, - list, - align: p.props.align, - page_break: p.props.page_break_before.unwrap_or(false), - keep_next: p.props.keep_next.unwrap_or(false), - inlines: Vec::new(), - spacing: direct_spacing(&p.props), - borders, - section: None, - mark_ins: None, - mark_del: None, - fmt_change: None, - extra: None, - mark: None, - kept: None, - wrap: None, - index: None, - group: None, - }; - if opts.paras { - out.extra = para_extra(doc, &p.props); - } - let in_code = kind == Kind::Code; - let mut k = 0usize; - let mut control = OpenControl::default(); - for (range, props) in p.run_ranges() { - let Some(text) = p.text.get(range.clone()) else { continue }; - // Deleted objects and page breaks stay out even when tracking: the flow has no marks for them. - let hidden = props.hidden.unwrap_or(false) || props.del.is_some(); - // Formats that keep hidden text get it, marked hidden. - let hidden_text = (props.hidden.unwrap_or(false) && !opts.chars) || (props.del.is_some() && !tracked); - let hidden_obj = (props.hidden.unwrap_or(false) && !opts.chars) || props.del.is_some(); - let mut f = run_fmt(doc, p.props.style.as_deref(), props, in_code, opts); - if tracked { - (f.ins, f.del) = rev_marks(doc, props); - f.fmt_change = props.fmt_change.as_deref().map(|c| Box::new(FmtChange { mark: rev_mark(doc, c.rev), old: fmt_of(doc, &c.old, in_code) })); - } - let mut buf = String::new(); - for c in text.chars() { - if c == PAGE_BREAK { - if !hidden { - out.push_text(&std::mem::take(&mut buf), &f); - let next = Para { - kind, - list: None, - align: p.props.align, - page_break: true, - keep_next: p.props.keep_next.unwrap_or(false), - inlines: Vec::new(), - spacing: direct_spacing(&p.props), - borders, - section: None, - mark_ins: None, - mark_del: None, - fmt_change: None, - extra: None, - mark: None, - kept: None, - wrap: None, - index: None, - group: None, - }; - let prev = std::mem::replace(&mut out, next); - if prev.inlines.is_empty() { - out.page_break |= prev.page_break; - out.list = prev.list; - } else { - done.push(prev); - } - } - continue; - } - if c != OBJ { - buf.push(c); - continue; - } - let obj = p.objects.get(k); - k += 1; - control.interrupt(obj, &mut out, &f); - // Hidden text a format leaves out, a deleted object, or a hidden drawing: not in the - // copy (a format keeping hidden text keeps its objects too, as it keeps the text). - if hidden_obj || obj.and_then(InlineObject::frame).is_some_and(|(_, _, f)| f.hidden) { - continue; - } - let from = out.inlines.len(); - if let Some(items) = obj.and_then(|o| rich_inlines(doc, o, &f, opts)) { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - out.inlines.extend(items); - raise_drawings(&mut out.inlines, from, props.position.filter(|_| opts.paras)); - continue; - } - match obj { - Some(o @ InlineObject::Image { media, w, h, alt, float, ole, crop, .. }) => { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - // A picture marked decorative keeps its description in the document, but - // exports with none (`alt=""`), so screen readers skip it. - let alt = if o.is_decorative() { "" } else { alt.as_str() }; - match doc.media.get(media) { - Some(data) => out.inlines.push(Inline::Image(Img { - data: data.clone(), - ext: media_ext(media, data), - w: *w, - h: *h, - alt: alt.to_string(), - float: kept_float(float).filter(|_| opts.paras), - object: ole.as_deref().filter(|_| opts.boxes).and_then(|e| super::odf_object::load(doc, e)), - crop: if opts.paras { *crop } else { [0.0; 4] }, - })), - None if !alt.is_empty() => buf.push_str(alt), - None => {} - } - } - Some(InlineObject::Graphic { alt, w, h, float, graphic }) => { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - out.inlines.extend(graphic_inline(doc, (alt, *w, *h), float, graphic, opts)); - } - Some(InlineObject::Equation { linear, display, math }) => { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - // The linear format is the shared form; an equation known only by its - // structure gets it from there. - let linear = if linear.is_empty() { wordcraft_doc::math::to_linear(&math.nodes) } else { linear.clone() }; - out.inlines.push(Inline::Equation { linear, display: *display }); - } - Some(InlineObject::Ruby { base, ruby, .. }) => { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - // The base text and reading, in the run's formatting; the guide's own - // attributes (alignment, size, raise) stay in the document model. - out.inlines.push(Inline::Ruby { base: base.clone(), ruby: ruby.clone(), fmt: f.clone() }); - } - Some(InlineObject::BookmarkStart { name }) if name != "_GoBack" => { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - out.inlines.push(Inline::Anchor(name.clone())); - } - Some(o @ (InlineObject::ControlStart { .. } | InlineObject::ControlEnd)) if control.object(o, opts, &mut buf, &mut out, &f) => {} - Some(o) => buf.push_str(shown_text(o)), - None => {} - } - raise_drawings(&mut out.inlines, from, props.position.filter(|_| opts.paras)); - } - if !hidden_text { - control.run_end(&buf, &mut out, &f); - } - } - control.finish(&mut out); - if tracked { - let (ins, del) = rev_marks(doc, &p.mark); - (out.mark_ins, out.mark_del) = (ins.map(Box::new), del.map(Box::new)); - // The formatting change belongs to the paragraph the mark ends. - out.fmt_change = p.props.fmt_change.as_deref().map(|c| { - let blank = out.inlines.is_empty(); - let (kind, mut list) = props_kind(doc, &c.old, blank); - if !opts.lists - && let Some(li) = &mut list - { - (li.list, li.label) = (0, None); - } - let old = Para { kind, list, align: c.old.align, page_break: c.old.page_break_before.unwrap_or(false), ..Default::default() }; - Box::new(FmtChange { mark: rev_mark(doc, c.rev), old }) - }); - } - // The paragraph that ends a section carries its properties (the last piece of it does). - out.section.clone_from(&p.section); - out.mark = empty_mark(&out, p, opts); - // Still what the file it came from had (a whole paragraph): its markup, for its format. Not - // for one starting a page: the break may be its element's or the one before's (a break after - // it), and only the model says which is written where. - if opts.paras && done.is_empty() && p.props.page_break_before != Some(true) { - // With its comments and its text boxes' text, while they're as read. - out.kept = doc - .kept - .of("odt") - .and_then(|k| { - k.para(p).filter(|xml| (!xml.contains(" Option { - if let Some(spec) = graphic.chart.as_ref().filter(|_| opts.boxes) { - return Some(Inline::Chart(crate::model::Chart { - spec: spec.clone(), - w, - h, - alt: alt.to_string(), - float: kept_float(float).filter(|_| opts.paras), - source: graphic.source.as_deref().and_then(|e| super::odf_object::load(doc, e)), - })); - } - // A diagram (SmartArt) for formats without one: its drawing as a group of shapes, text boxes - // and pictures every reader draws. - if opts.boxes - && let Some(group) = graphic_group(doc, (w, h), float, graphic) - { - return Some(group); - } - (!alt.trim().is_empty()).then(|| Inline::Figure(alt.to_string())) -} - -/// A graphic's drawing items ([`wordcraft_doc::graphic::GraphicItem`]) as a group `w` × `h` -/// points: its shapes as shapes, its text as borderless text boxes centred on their places, its -/// pictures as pictures. `None` when nothing in it can be drawn so (paths are left out). -fn graphic_group( - doc: &Document, - (w, h): (f32, f32), - float: &wordcraft_doc::para::Float, - graphic: &wordcraft_doc::graphic::Graphic, -) -> Option { - use wordcraft_doc::graphic::{GraphicItem, TextAlign}; - use wordcraft_doc::para::ShapeKind; - let ratio = |outer: f32, inner: f32| { - let r = outer / inner; - if r.is_finite() && r > 0.0 { r } else { 1.0 } - }; - let (sx, sy) = (ratio(w, graphic.w), ratio(h, graphic.h)); - let finite = |r: &[f32; 4]| r.iter().all(|v| v.is_finite()) && r[2] >= 0.0 && r[3] >= 0.0; - let plain = BoxLook { fill: None, line: None, inset: Some([0.0; 4]), ..BoxLook::default() }; - let members: Vec = graphic - .items - .iter() - .take(10_000) - .filter_map(|item| match item { - GraphicItem::Shape { rect, kind, fill, stroke, stroke_width } if finite(rect) => { - let look = BoxLook { - fill: fill.map(wordcraft_doc::fill::Fill::solid).filter(|_| *kind != ShapeKind::Line), - line: stroke.filter(|_| stroke_width.is_finite() && *stroke_width > 0.0).map(|c| (c, stroke_width.min(12.0))), - ..plain.clone() - }; - let inline = Inline::Shape { kind: *kind, w: rect[2] * sx, h: rect[3] * sy, float: None, blocks: Vec::new(), look, path: None }; - Some(Member { x: rect[0] * sx, y: rect[1] * sy, inline }) - } - GraphicItem::Text { rect, text, size, color, bold, align, font } if finite(rect) && !text.trim().is_empty() => { - let size = if size.is_finite() { size.clamp(1.0, 400.0) } else { 11.0 }; - // One line, centred vertically in its place as the graphic draws it. - let line = size * 1.2; - let (bw, bh) = (rect[2] * sx, line.max(1.0)); - let y = rect[1] * sy + (rect[3] * sy - bh) / 2.0; - let fmt = Fmt { bold: *bold, color: Some(*color), size: Some(size), font: font.as_deref().map(Arc::from), ..Fmt::default() }; - let para = Para { - align: Some(match align { - TextAlign::Left => wordcraft_doc::Align::Left, - TextAlign::Right => wordcraft_doc::Align::Right, - _ => wordcraft_doc::Align::Center, - }), - inlines: vec![Inline::Text(text.clone(), fmt)], - spacing: Spacing { before: Some(0.0), after: Some(0.0), ..Spacing::default() }, - ..Para::default() - }; - let inline = Inline::Shape { - kind: ShapeKind::Rectangle, - w: bw, - h: bh, - float: None, - blocks: vec![FBlock::Para(para)], - look: plain.clone(), - path: None, - }; - Some(Member { x: rect[0] * sx, y, inline }) - } - GraphicItem::Image { rect, media } if finite(rect) => { - let data = doc.media.get(media)?; - let img = Img { - data: data.clone(), - ext: media_ext(media, data), - w: rect[2] * sx, - h: rect[3] * sy, - alt: String::new(), - float: None, - object: None, - crop: [0.0; 4], - }; - Some(Member { x: rect[0] * sx, y: rect[1] * sy, inline: Inline::Image(img) }) - } - _ => None, - }) - .collect(); - if members.is_empty() { - return None; - } - // A group is as big as its members together (ODF 1.3 §10.3.15): an invisible rectangle the - // size of the graphic keeps the room around its drawing. - let frame = Inline::Shape { kind: ShapeKind::Rectangle, w, h, float: None, blocks: Vec::new(), look: plain, path: None }; - let all = std::iter::once(Member { x: 0.0, y: 0.0, inline: frame }).chain(members).collect(); - Some(Inline::Group { w, h, float: kept_float(float), members: all }) -} - -/// The mark formatting of `out`, the last piece of `p`, when it holds no text (its height, its -/// drawings' formatting), for -/// formats that keep it (flows asked for paragraph formatting). -fn empty_mark(out: &Para, p: &Paragraph, opts: FlowOpts) -> Option> { - let m = CharProps { ins: None, del: None, fmt_change: None, ..p.mark.clone() }; - (opts.paras && out.textless() && !m.is_empty()).then(|| Box::new(m)) -} - -/// The drawings from `from` on (just added) raised by their run's position `raise` (points): a -/// drawing in line keeps it as its float's `y` (see [`crate::model::Img::float`]). -fn raise_drawings(inlines: &mut [Inline], from: usize, raise: Option) { - let Some(r) = raise.filter(|r| r.is_finite() && *r != 0.0) else { return }; - for i in inlines.iter_mut().skip(from) { - let float = match i { - Inline::Image(img) => &mut img.float, - Inline::Chart(c) => &mut c.float, - Inline::Shape { float, .. } | Inline::Group { float, .. } => float, - _ => continue, - }; - let f = float.get_or_insert_with(Default::default); - if f.wrap == wordcraft_doc::para::Wrap::Inline { - f.y = r; - } - } -} - -/// A content control of plain text being read (formats with form controls): it, its text so -/// far and that text's formatting. -#[derive(Default)] -struct OpenControl(Option<(Box, String, Option)>); - -impl OpenControl { - /// Object `obj` comes: anything but the control's end makes the control its text after all. - fn interrupt(&mut self, obj: Option<&InlineObject>, out: &mut Para, f: &Fmt) { - if !matches!(obj, Some(InlineObject::ControlEnd)) - && let Some((_, text, fmt)) = self.0.take() - { - out.push_text(&text, &fmt.unwrap_or_else(|| f.clone())); - } - } - - /// A content control starts or ends (`o`): whether it is one read here (a plain-text - /// control, in a flow that keeps them). - fn object(&mut self, o: &InlineObject, opts: FlowOpts, buf: &mut String, out: &mut Para, f: &Fmt) -> bool { - match o { - InlineObject::ControlStart { control: c } if opts.boxes && !c.block && plain_control(&c.kind) => { - if !buf.is_empty() { - out.push_text(&std::mem::take(buf), f); - } - self.0 = Some((c.clone(), String::new(), None)); - true - } - InlineObject::ControlEnd if self.0.is_some() => { - self.close(buf, out, f); - true - } - _ => false, - } - } - - /// The control ends: it, with its text (the rest in `buf`), goes into `out`. - fn close(&mut self, buf: &mut String, out: &mut Para, f: &Fmt) { - if let Some((c, mut text, fmt)) = self.0.take() { - text.push_str(&std::mem::take(buf)); - out.inlines.push(Inline::Control { control: c, text, fmt: fmt.unwrap_or_else(|| f.clone()) }); - } - } - - /// A run ends with `buf`: the control's text when one is open, else the paragraph's. - fn run_end(&mut self, buf: &str, out: &mut Para, f: &Fmt) { - match &mut self.0 { - Some((_, text, fmt)) => { - text.push_str(buf); - fmt.get_or_insert_with(|| f.clone()); - } - None => out.push_text(buf, f), - } - } - - /// The paragraph ends: a control it doesn't close is its text. - fn finish(&mut self, out: &mut Para) { - if let Some((_, text, fmt)) = self.0.take() { - out.push_text(&text, &fmt.unwrap_or_default()); - } - } -} - -/// A content control that holds plain text, as a form control does. -fn plain_control(kind: &wordcraft_doc::control::ControlKind) -> bool { - use wordcraft_doc::control::ControlKind as K; - matches!(kind, K::Text { .. } | K::CheckBox { .. } | K::DropDown { .. } | K::ComboBox { .. } | K::Date { .. }) -} - -/// The index kept as read starting at block `i` of `bl`, while the paragraphs it takes are all -/// as read: how many blocks, and its markup. -fn kept_index(doc: &Document, bl: &Blocks, i: usize) -> Option<(usize, std::sync::Arc)> { - let k = doc.kept.of("odt")?; - let p = bl.get(i)?.as_para()?; - let (n, run, xml) = k.index(p)?; - let paras: Vec<&Paragraph> = bl.get(i..i + n)?.iter().filter_map(|b| b.as_para()).collect(); - (paras.len() == *n && wordcraft_doc::kept::run_fingerprint(paras.into_iter()) == *run).then(|| (*n, xml.clone())) -} - -fn flow_blocks(doc: &Document, bl: &Blocks, depth: usize, tracked: FlowOpts) -> Vec { - let mut out = Vec::new(); - let mut skip = 0usize; - for (i, b) in bl.iter().enumerate() { - if skip > 0 { - skip -= 1; - continue; - } - // An index kept as read, while every paragraph it takes still is what was read: its - // flow paragraphs, the first carrying its markup and how many there are. - if tracked.paras - && depth == 0 - && let Some(ix) = kept_index(doc, bl, i) - { - let from = out.len(); - for b in bl.get(i..i + ix.0).unwrap_or(&[]) { - if let Block::Para(p) = &**b { - out.extend(flow_paras_in(doc, p, tracked).into_iter().map(FBlock::Para)); - } - } - let n = out.len() - from; - if let Some(FBlock::Para(first)) = out.get_mut(from) { - first.index = Some(Box::new((ix.1, n))); - } - skip = ix.0.saturating_sub(1); - continue; - } - match &**b { - Block::Para(p) => out.extend(flow_paras_in(doc, p, tracked).into_iter().map(FBlock::Para)), - Block::Table(t) if depth < MAX_DEPTH => out.push(FBlock::Table(flow_table(doc, t, depth, tracked))), - Block::Table(t) => { - for r in &t.rows { - for c in &r.cells { - out.extend(flow_blocks(doc, &c.blocks, depth, tracked)); - } - } - } - } - } - out -} - -/// Word's default cell margins (top, left, bottom, right), points. -const WORD_CELL_MARGINS: [f32; 4] = [0.0, 5.4, 0.0, 5.4]; - -/// What a table's style and borders give one cell, worked out as the layout does, for formats -/// that keep cell formatting (ODT) but have no table styles of Word's kind: its shading, its four -/// edges (a side without a line is a "none" border), its vertical alignment, and its text's -/// character formatting and alignment. -struct StyledCell { - fill: Option, - borders: wordcraft_doc::props::Borders, - v_align: Option, - chr: CharProps, - align: Option, - /// The style's paragraph formatting (spacing, line spacing) under each paragraph's own. - para: ParaProps, -} - -fn styled_cell(t: &Table, style: Option<&wordcraft_doc::styles::TableStyleProps>, (ri, ci): (usize, usize), ncols: usize) -> Option { - use wordcraft_doc::props::{Border, BorderStyle, Borders}; - use wordcraft_doc::styles::{PartSet, TablePart}; - let row = t.rows.get(ri)?; - let cell = row.cells.get(ci)?; - let nrows = t.rows.len(); - let g = t.grid_col(ri, ci); - let span = cell.span(); - let mut out = StyledCell { - fill: None, - borders: Borders::default(), - v_align: None, - chr: CharProps::default(), - align: None, - para: style.map(|s| s.para.clone()).unwrap_or_default(), - }; - let mut part_borders = Vec::new(); - if let Some(st) = style { - let sizes = (st.parts.band_size.unwrap_or(0), st.parts.col_band_size.unwrap_or(0)); - for part in PartSet::of_cell(&t.props.look, sizes, (ri, nrows), (g, span, ncols)).iter() { - let f = st.part(part); - out.fill = f.fill.or(out.fill); - out.v_align = f.v_align.or(out.v_align); - out.chr.overlay(&f.chr); - out.align = f.align.or(out.align); - if part != TablePart::WholeTable - && let Some(b) = f.borders - { - part_borders.push((part, b)); - } - } - } - let mut tb = style.and_then(|s| s.parts.borders).unwrap_or_default(); - if let Some(own) = t.props.borders { - tb.overlay(&own); - } - let cb = cell.props.borders.unwrap_or_default(); - let rowb = row.props.exceptions.as_ref().and_then(|e| e.borders).unwrap_or_default(); - let (first_g, last_g) = (g == row.first_grid(), g + span >= ncols.saturating_sub(row.grid_after())); - let rb = wordcraft_doc::styles::part_borders(&part_borders, (ri, nrows), (first_g, last_g)); - let edge = |own: Option, outer: bool, outer_b: Option, inner_b: Option| own.or(if outer { outer_b } else { inner_b }); - let none = Border { style: BorderStyle::None, width: 0.0, color: None, space: 0.0 }; - let top = edge(cb.top.or(if ri == 0 { rowb.top } else { rowb.between }).or(rb.top), ri == 0, tb.top, tb.between); - let bottom = edge(cb.bottom.or(if ri + 1 == nrows { rowb.bottom } else { rowb.between }).or(rb.bottom), ri + 1 == nrows, tb.bottom, tb.between); - let left = edge( - cb.left.or(if first_g { rowb.left } else { rowb.inside_v }).or(if first_g { rb.left } else { rb.inside_v }), - first_g, - tb.left, - tb.inside_v, - ); - let right = edge( - cb.right.or(if last_g { rowb.right } else { rowb.inside_v }).or(if last_g { rb.right } else { rb.inside_v }), - last_g, - tb.right, - tb.inside_v, - ); - out.borders = Borders { - top: top.or(Some(none)), - bottom: bottom.or(Some(none)), - left: left.or(Some(none)), - right: right.or(Some(none)), - between: None, - inside_v: None, - }; - Some(out) -} - -/// `blocks` with what a table style's character and paragraph formatting changes (see -/// [`wordcraft_doc::styles::StyleSheet::table_para_patch`]) stated on each paragraph and run. -fn with_cell_text(doc: &Document, blocks: &Blocks, chr: &CharProps, align: Option, para: &ParaProps) -> Blocks { - if chr.is_empty() && align.is_none() && para.is_empty() { - return blocks.clone(); - } - blocks - .iter() - .map(|b| match &**b { - Block::Para(p) => { - let mut p = p.clone(); - let pstyle = p.props.style.clone(); - if !chr.is_empty() { - for r in &mut p.runs { - let mut props = doc.styles.table_char_patch(pstyle.as_deref(), r.props.style.as_deref(), chr); - props.overlay(&r.props); - r.props = props; - } - let mut mark = doc.styles.table_char_patch(pstyle.as_deref(), p.mark.style.as_deref(), chr); - mark.overlay(&p.mark); - p.mark = mark; - } - // A part's alignment is table style paragraph formatting too, under the paragraph's styles. - let table = ParaProps { align: align.or(para.align), ..para.clone() }; - if !table.is_empty() { - let mut props = doc.styles.table_para_patch(&p.props, &table); - props.overlay(&p.props); - p.props = props; - } - Arc::new(Block::Para(p)) - } - _ => b.clone(), - }) - .collect() -} - -fn flow_table(doc: &Document, t: &Table, depth: usize, tracked: FlowOpts) -> FTable { - let mut rows = Vec::new(); - // Formats that keep cell formatting get the table style's and the table's borders cell by cell. - let style = t.props.style.as_deref().and_then(|id| doc.styles.table_style(id)); - let bake = tracked.paras; - let ncols = - t.rows.iter().map(|r| r.first_grid() + r.cells.iter().map(|c| c.span()).sum::() + r.grid_after()).max().unwrap_or(0).max(t.grid.len()); - for (ri, r) in t.rows.iter().enumerate() { - let header = r.props.header || (ri == 0 && t.props.look.header_row && t.rows.len() > 1); - let mut cells = Vec::new(); - for (ci, c) in r.cells.iter().enumerate() { - let styled = if bake { styled_cell(t, style.as_ref(), (ri, ci), ncols) } else { None }; - let mut rowspan = 1; - if c.props.vmerge == VMerge::Restart { - let g = t.grid_col(ri, ci); - for (k, below) in t.rows.iter().enumerate().skip(ri + 1) { - let cont = t.cell_at_grid(k, g).and_then(|i| below.cells.get(i)).is_some_and(|bc| bc.props.vmerge == VMerge::Continue); - if !cont { - break; - } - rowspan += 1; - } - } - let blocks = match &styled { - Some(st) => flow_blocks(doc, &with_cell_text(doc, &c.blocks, &st.chr, st.align, &st.para), depth + 1, tracked), - None => flow_blocks(doc, &c.blocks, depth + 1, tracked), - }; - cells.push(Cell { - blocks, - colspan: c.span() as u32, - rowspan, - covered: c.props.vmerge == VMerge::Continue, - header, - shading: c.props.shading.or(styled.as_ref().and_then(|s| s.fill)), - borders: styled.as_ref().map(|s| s.borders).or(c.props.borders), - valign: c.props.valign.or(styled.as_ref().and_then(|s| s.v_align)), - // Formats that keep cell padding get each cell's, its table's or Word's default. - margins: tracked.paras.then(|| c.props.resolved_margins(t.props.cell_margins.unwrap_or(WORD_CELL_MARGINS))), - direction: c.props.text_direction, - // Its background picture, for formats that keep cell formatting (ODT). - picture: c.props.picture.as_deref().filter(|_| tracked.paras).and_then(|p| { - let data = doc.media.get(&p.media)?; - let img = Img { - data: data.clone(), - ext: media_ext(&p.media, data), - w: p.size[0], - h: p.size[1], - alt: String::new(), - float: None, - object: None, - crop: [0.0; 4], - }; - Some(Box::new(crate::model::CellPic { img, repeat: p.repeat, align: p.align })) - }), - width: None, - }); - } - rows.push(cells); - } - // Without a table style and without visible table or cell borders the table shows no grid. - let visible = |b: &Option| b.as_ref().is_some_and(|b| b.any_visible()); - let borderless = - t.props.style.is_none() && !visible(&t.props.borders) && t.rows.iter().flat_map(|r| &r.cells).all(|c| !visible(&c.props.borders)); - // Formats that keep them get the table's alignment, width and row heights. - let extra = tracked.paras.then(|| { - use wordcraft_doc::props::HeightRule; - let heights = t - .rows - .iter() - .map(|r| match (r.props.height, r.props.height_rule) { - (Some(h), HeightRule::Exact) => Some((h, true)), - (Some(h), HeightRule::AtLeast) => Some((h, false)), - _ => None, - }) - .collect::>(); - super::types::TableExtra { - align: t.props.align, - width: t.props.width, - width_pct: t.props.width_pct, - heights, - keep_rows: t.rows.iter().map(|r| r.props.cant_split).collect(), - fixed: t.props.fixed, - space: [t.props.space_before, t.props.space_after], - header_rows: t.rows.iter().take_while(|r| r.props.header).count(), - } - }); - // Kept whole for formats that ask for it: its header rows count even without the rest. - let extra = extra.map(Box::new); - let kept = (tracked.paras && depth == 0).then(|| doc.kept.of("odt").and_then(|k| k.table(t)).cloned()).flatten(); - let wrap = (tracked.paras && depth == 0).then(|| doc.kept.of("odt").and_then(|k| k.table_wrap(t)).cloned().map(Box::new)).flatten(); - FTable { rows, widths: t.grid.clone(), borderless, extra, kept, wrap } -} - -/// The document body as a flow. -pub fn from_doc(doc: &Document) -> Flow { - flow_of(doc, FlowOpts::default()) -} - -/// [`from_doc`] keeping tracked changes: deleted text stays, and text carries its insertion and -/// deletion marks ([`Fmt::ins`], [`Fmt::del`]), for formats that can write revisions (RTF). -pub fn from_doc_tracked(doc: &Document) -> Flow { - flow_of(doc, FlowOpts { tracked: true, ..Default::default() }) -} - -/// [`from_doc`] keeping what `opts` asks for. -pub fn from_doc_with(doc: &Document, opts: FlowOpts) -> Flow { - // Paragraphs kept as read are checked against the document's comments and page setup: - // worked out once for the whole document, not once per paragraph. - wordcraft_doc::kept::while_writing(doc, || flow_of(doc, opts)) -} - -/// `blocks` of `doc` (a header, footer or note) as flow blocks, keeping what `opts` asks for. -pub fn part_flow(doc: &Document, blocks: &Blocks, opts: FlowOpts) -> Vec { - flow_blocks(doc, blocks, 0, opts) -} - -fn flow_of(doc: &Document, tracked: FlowOpts) -> Flow { - let c = &doc.core; - Flow { - blocks: flow_blocks(doc, &doc.body, 0, tracked), - meta: Meta { - title: c.title.clone(), - author: c.creator.clone(), - subject: c.subject.clone(), - keywords: c.keywords.clone(), - description: c.description.clone(), - created: c.created.clone(), - }, - } -} - -/// List labels in document order for list paragraphs of a flow ("1.", "a.", "•"), computed by a -/// simple per-level counter (what text exports show). -#[derive(Default)] -pub struct ListCounter { - counts: [u32; 9], - ordered: [bool; 9], -} - -impl ListCounter { - pub fn reset(&mut self) { - *self = ListCounter::default(); - } - /// Advance for `li` and return its number (1-based; bullets also count). - pub fn next(&mut self, li: ListInfo) -> u32 { - let lv = li.level.min(8) as usize; - if self.ordered.get(lv).copied() != Some(li.ordered) - && let (Some(o), Some(c)) = (self.ordered.get_mut(lv), self.counts.get_mut(lv)) - { - *o = li.ordered; - *c = 0; - } - let n = match self.counts.get_mut(lv) { - Some(c) => { - if *c == 0 { - *c = li.start.saturating_sub(1); - } - *c = c.saturating_add(1); - *c - } - None => 1, - }; - for d in self.counts.iter_mut().skip(lv + 1) { - *d = 0; - } - n - } - pub fn label(&mut self, li: ListInfo) -> String { - let n = self.next(li); - if !li.ordered { - return ["•", "◦", "▪"].get(li.level as usize % 3).copied().unwrap_or("•").to_string(); - } - match li.level % 3 { - 0 => format!("{n}."), - 1 => format!("{}.", wordcraft_doc::section::letters(n)), - _ => format!("{}.", wordcraft_doc::section::roman(n)), - } - } -} diff --git a/crates/formats/src/model/media.rs b/crates/formats/src/model/media.rs deleted file mode 100644 index 4bf50a701c..0000000000 --- a/crates/formats/src/model/media.rs +++ /dev/null @@ -1,154 +0,0 @@ -use super::Arc; -use super::types::Img; - -// --------------------------------------------------------------------------------------------- -// Shared helpers - -/// Image extension from bytes, if it's a format we can display. -pub fn sniff_image(data: &[u8]) -> Option<&'static str> { - match image::guess_format(data).ok()? { - image::ImageFormat::Png => Some("png"), - image::ImageFormat::Jpeg => Some("jpeg"), - image::ImageFormat::Gif => Some("gif"), - image::ImageFormat::WebP => Some("webp"), - image::ImageFormat::Bmp => Some("bmp"), - _ => None, - } -} - -/// Pixel size of encoded image bytes. -pub fn image_px(data: &[u8]) -> Option<(u32, u32)> { - image::ImageReader::new(std::io::Cursor::new(data)).with_guessed_format().ok()?.into_dimensions().ok() -} - -/// Decode an encoded image within the budget pictures are drawn within, so a small file that -/// declares huge dimensions can't exhaust memory when a picture is re-encoded on export. `None` -/// when it would be too large or doesn't decode. -pub fn decode_bounded(data: &[u8]) -> Option { - wordcraft_geom::picture::decode(data, wordcraft_geom::MAX_DECODED_BYTES).ok() -} - -/// A picture from bytes with an optional display size (points); the natural size (96 dpi) is -/// used otherwise, shrunk to fit a 6.5" column. -pub fn make_img(data: impl Into>>, w: Option, h: Option, alt: &str) -> Option { - let data = data.into(); - let ext = sniff_image(&data)?; - let natural = image_px(&data).map(|(a, b)| (a as f32 * 0.75, b as f32 * 0.75)); - Some(sized_img(data, ext, natural, w, h, alt)) -} - -/// A picture of a known format `ext` whose natural size (points) the caller knows, if at all: a -/// metafile, which `image` can't measure. Sized like [`make_img`]. -pub fn sized_img(data: Arc>, ext: &str, natural: Option<(f32, f32)>, w: Option, h: Option, alt: &str) -> Img { - let (pw, ph) = natural.filter(|(a, b)| a.is_finite() && b.is_finite()).unwrap_or((72.0, 72.0)); - let (pw, ph) = (pw.max(1.0), ph.max(1.0)); - let (mut w, mut h) = match (w.filter(|v| v.is_finite() && *v > 0.0), h.filter(|v| v.is_finite() && *v > 0.0)) { - (Some(w), Some(h)) => (w, h), - (Some(w), None) => (w, w * ph / pw), - (None, Some(h)) => (h * pw / ph, h), - (None, None) => (pw, ph), - }; - if w > 468.0 { - h *= 468.0 / w; - w = 468.0; - } - Img { - data, - ext: ext.to_string(), - w: w.clamp(1.0, 1584.0), - h: h.clamp(1.0, 1584.0), - alt: alt.to_string(), - float: None, - object: None, - crop: [0.0; 4], - } -} - -const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -pub fn base64_encode(data: &[u8]) -> String { - let mut out = String::with_capacity(data.len().div_ceil(3) * 4); - for ch in data.chunks(3) { - let b = [ch.first().copied().unwrap_or(0), ch.get(1).copied().unwrap_or(0), ch.get(2).copied().unwrap_or(0)]; - let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); - let idx = [(n >> 18) & 63, (n >> 12) & 63, (n >> 6) & 63, n & 63]; - for (i, v) in idx.iter().enumerate() { - if i <= ch.len() { - out.push(B64.get(*v as usize).copied().unwrap_or(b'A') as char); - } else { - out.push('='); - } - } - } - out -} - -/// Lenient base64 (standard or URL-safe alphabet; whitespace and padding ignored). -pub fn base64_decode(s: &str) -> Option> { - let mut out = Vec::with_capacity(s.len() / 4 * 3); - let mut acc = 0u32; - let mut bits = 0u32; - for c in s.bytes() { - let v = match c { - b'A'..=b'Z' => c - b'A', - b'a'..=b'z' => c - b'a' + 26, - b'0'..=b'9' => c - b'0' + 52, - b'+' | b'-' => 62, - b'/' | b'_' => 63, - b'=' => break, - c if c.is_ascii_whitespace() => continue, - _ => return None, - }; - acc = (acc << 6) | u32::from(v); - bits += 6; - if bits >= 8 { - bits -= 8; - out.push((acc >> bits) as u8); - acc &= (1 << bits) - 1; - } - } - Some(out) -} - -/// Decode a `data:` URI (base64 or percent-encoded). -pub fn data_uri(uri: &str) -> Option> { - let rest = uri.trim().strip_prefix("data:")?; - let (head, data) = rest.split_once(',')?; - if head.ends_with(";base64") { base64_decode(data) } else { Some(percent_decode(data)) } -} - -pub fn percent_decode(s: &str) -> Vec { - let b = s.as_bytes(); - let mut out = Vec::with_capacity(b.len()); - let mut i = 0; - while let Some(&c) = b.get(i) { - if c == b'%' - && let Some(h) = b.get(i + 1..i + 3) - && h.iter().all(u8::is_ascii_hexdigit) - && let Some(hex) = std::str::from_utf8(h).ok() - && let Ok(v) = u8::from_str_radix(hex, 16) - { - out.push(v); - i += 3; - continue; - } - out.push(c); - i += 1; - } - out -} - -pub fn mime_of(ext: &str) -> &'static str { - match ext { - "png" => "image/png", - "jpg" | "jpeg" => "image/jpeg", - "gif" => "image/gif", - "webp" => "image/webp", - "bmp" => "image/bmp", - "svg" => "image/svg+xml", - "ico" => "image/x-icon", - "wmf" => "image/x-wmf", - "emf" => "image/x-emf", - _ => "application/octet-stream", - } -} diff --git a/crates/formats/src/model/odf_object.rs b/crates/formats/src/model/odf_object.rs deleted file mode 100644 index d674864cc9..0000000000 --- a/crates/formats/src/model/odf_object.rs +++ /dev/null @@ -1,72 +0,0 @@ -//! ODF embedded objects ([`OdfObject`]) in a document: their files in -//! [`Document::passthrough`] under `odf-object//`, linked from the picture showing them by an -//! [`Embedded`] whose markup only names the object's media type, and taken back out for the ODT -//! writer. Other formats see the picture alone. - -use std::sync::Arc; - -use wordcraft_doc::Document; -use wordcraft_doc::graphic::{Embedded, EmbeddedRef}; - -use super::types::OdfObject; - -/// Relationship kind of an object's file, and of its replacement picture. -const FILE: &str = "file"; -const REPLACEMENT: &str = "replacement"; -/// Most objects a document keeps, and most files in one. -pub const MAX_OBJECTS: usize = 1000; -pub const MAX_FILES: usize = 256; - -fn esc(s: &str) -> String { - s.replace('&', "&").replace('"', """).replace('<', "<").replace('>', ">") -} - -fn unesc(s: &str) -> String { - s.replace(""", "\"").replace("<", "<").replace(">", ">").replace("&", "&") -} - -/// Keep `obj` in `doc`: its files as passthrough parts, and the link to them. `None` when the -/// document already holds as many objects as it keeps. -pub fn store(doc: &mut Document, obj: &OdfObject) -> Option> { - let taken = |n: usize| { - let prefix = format!("odf-object/{n}/"); - doc.passthrough.range(prefix.clone()..).next().is_some_and(|(k, _)| k.starts_with(&prefix)) - }; - let n = (0..MAX_OBJECTS).find(|&n| !taken(n))?; - let mut refs = Vec::new(); - for (k, (path, bytes)) in obj.files.iter().take(MAX_FILES).enumerate() { - let target = format!("odf-object/{n}/{k}"); - doc.passthrough.insert(target.clone(), bytes.clone()); - refs.push(EmbeddedRef { id: path.clone(), kind: FILE.into(), target, external: false }); - } - if let Some(r) = &obj.replacement { - let target = format!("odf-object/{n}/replacement"); - doc.passthrough.insert(target.clone(), r.clone()); - refs.push(EmbeddedRef { id: String::new(), kind: REPLACEMENT.into(), target, external: false }); - } - let xml = format!("{} mime=\"{}\" ole=\"{}\"/>", Embedded::ODF_OBJECT, esc(&obj.mime), obj.ole); - Some(Arc::new(Embedded { xml, refs })) -} - -/// The object `e` links to in `doc`, when it is an ODF object whose files are all there. -pub fn load(doc: &Document, e: &Embedded) -> Option> { - if !e.is_odf_object() { - return None; - } - let attr = |k: &str| { - let pat = format!(" {k}=\""); - let from = e.xml.find(&pat)? + pat.len(); - let len = e.xml.get(from..)?.find('"')?; - e.xml.get(from..from + len).map(unesc) - }; - let mut obj = OdfObject { mime: attr("mime").unwrap_or_default(), ole: attr("ole").as_deref() == Some("true"), ..Default::default() }; - for r in e.refs.iter().take(MAX_FILES + 1) { - let bytes = doc.passthrough.get(&r.target)?.clone(); - match r.kind.as_str() { - FILE => obj.files.push((r.id.clone(), bytes)), - REPLACEMENT => obj.replacement = Some(bytes), - _ => {} - } - } - (!obj.files.is_empty()).then(|| Arc::new(obj)) -} diff --git a/crates/formats/src/model/to_doc.rs b/crates/formats/src/model/to_doc.rs deleted file mode 100644 index de7dcf5f09..0000000000 --- a/crates/formats/src/model/to_doc.rs +++ /dev/null @@ -1,1001 +0,0 @@ -use super::from_doc::uses_kind; -use super::types::{ - CODE_STYLE, Cell, Dropped, FBlock, FTable, Flow, Fmt, Inline, Kind, ListInfo, MAX_DEPTH, MAX_GROUP_DEPTH, MAX_RUN_STRINGS, MONO_FONT, Para, - RUBY_BASE_SIZE, RevMark, clean_text, dropped, ruby_object, -}; -use super::{ - AbstractNum, Arc, Block, Blocks, Border, BorderStyle, Borders, CellProps, CharProps, Document, InlineObject, Level, ListKind, Num, NumRef, OBJ, - ParaProps, Paragraph, PropChange, Revision, RevisionKind, Rgb, RowProps, Run, Style, StyleKind, Table, TextColor, Underline, VMerge, VertAlign, - levels_for, para_block, -}; -use wordcraft_doc::para::{GroupChild, MAX_GROUP_CHILDREN}; - -pub fn char_props(f: &Fmt) -> CharProps { - let on = |b: bool| if b { Some(true) } else { None }; - CharProps { - bold: on(f.bold), - italic: on(f.italic), - underline: if f.underline { Some(Underline::Single) } else { None }, - strike: on(f.strike), - vert_align: if f.sup { - Some(VertAlign::Superscript) - } else if f.sub { - Some(VertAlign::Subscript) - } else { - None - }, - link: f.link.as_deref().filter(|l| !l.is_empty()).map(str::to_string), - color: f.color.map(TextColor::Rgb), - size: f.size.filter(|s| s.is_finite() && *s >= 1.0 && *s <= 1638.0), - font: if f.code { Some(MONO_FONT.to_string()) } else { f.font.as_deref().filter(|s| !s.is_empty()).map(str::to_string) }, - shading: f.background, - ..Default::default() - } - .with_extra(f) -} - -/// [`Fmt::extra`] laid over the character properties of the other [`Fmt`] fields. -trait WithExtra { - fn with_extra(self, f: &Fmt) -> CharProps; -} - -impl WithExtra for CharProps { - fn with_extra(mut self, f: &Fmt) -> CharProps { - let Some(x) = f.extra.as_deref() else { return self }; - if f.underline - && let Some(u) = x.underline - { - self.underline = Some(u); - } - if f.underline { - self.underline_color = x.underline_color; - } - // Turned off on the text, over what the style of its paragraph or span turns on. - if !f.bold && x.bold == Some(false) { - self.bold = Some(false); - } - if !f.italic && x.italic == Some(false) { - self.italic = Some(false); - } - if !f.strike && x.strike == Some(false) { - // A double strike under it goes too. - (self.strike, self.double_strike) = (Some(false), Some(false)); - } - if !f.underline && x.underline == Some(Underline::None) { - self.underline = Some(Underline::None); - } - if x.double_strike == Some(true) { - (self.strike, self.double_strike) = (None, Some(true)); - } - let finite = |v: Option, lo: f32, hi: f32| v.filter(|v| v.is_finite()).map(|v| v.clamp(lo, hi)); - self.style = x.style.as_deref().map(clean_text).filter(|s| !s.is_empty()); - self.caps = x.caps; - self.small_caps = x.small_caps; - self.hidden = x.hidden; - self.spacing = finite(x.spacing, -1584.0, 1584.0); - self.scale = finite(x.scale, 1.0, 600.0); - self.kern = finite(x.kern, 0.0, 1638.0); - self.emphasis = x.emphasis; - self.position = finite(x.position, -1584.0, 1584.0); - self.outline = x.outline; - self.shadow = x.shadow; - self.emboss = x.emboss; - self.engrave = x.engrave; - self.border = x.border.filter(|b| b.is_visible() && b.width.is_finite()).map(|mut b| { - (b.width, b.space) = (b.width.clamp(0.0, 12.0), if b.space.is_finite() { b.space.clamp(0.0, 31.0) } else { 0.0 }); - b - }); - self.lang = x.lang.as_deref().map(clean_text).filter(|l| !l.is_empty() && l.len() <= 64); - let name = |s: &Option, max: usize| s.as_deref().map(clean_text).filter(|v| !v.is_empty() && v.len() <= max); - self.font_cs = name(&x.font_cs, super::types::MAX_FONT_NAME); - self.font_ea = name(&x.font_ea, super::types::MAX_FONT_NAME); - self.size_cs = x.size_cs.filter(|s| s.is_finite()).map(|s| s.clamp(1.0, 1638.0)); - self.bold_cs = x.bold_cs; - self.italic_cs = x.italic_cs; - self.lang_bidi = name(&x.lang_bidi, 64); - self.lang_ea = name(&x.lang_ea, 64); - self - } -} - -fn code_style() -> Style { - Style { - id: CODE_STYLE.into(), - name: CODE_STYLE.into(), - kind: StyleKind::Paragraph, - based_on: Some("Normal".into()), - para: ParaProps { space_after: Some(0.0), space_before: Some(0.0), line_spacing: Some(Default::default()), ..Default::default() }, - chr: CharProps { font: Some(MONO_FONT.into()), size: Some(10.0), ..Default::default() }, - quick: false, - ..Default::default() - } -} - -fn rule_borders() -> Borders { - Borders { bottom: Some(Border { style: BorderStyle::Single, width: 0.75, color: Some(Rgb(0xA0, 0xA0, 0xA0)), space: 1.0 }), ..Default::default() } -} - -/// Append text (without object characters) or one inline object to the end of `p`. -fn append(p: &mut Paragraph, text: &str, props: &CharProps, obj: Option) { - let text: std::borrow::Cow = if text.contains(OBJ) { text.chars().filter(|c| *c != OBJ).collect::().into() } else { text.into() }; - let piece = match obj { - Some(o) => { - p.objects.push(o); - OBJ.to_string() - } - None => text.into_owned(), - }; - if piece.is_empty() { - return; - } - p.text.push_str(&piece); - match p.runs.last_mut() { - Some(last) if last.props == *props => last.len += piece.len(), - _ => p.runs.push(Run { len: piece.len(), props: props.clone() }), - } -} - -/// The paragraph properties of a flow paragraph (its list as instance `list_num`, when given). -fn para_props(p: &Para, list_num: Option) -> ParaProps { - let mut out = ParaProps { - style: match p.kind { - Kind::Heading(n) => Some(format!("Heading{}", n.clamp(1, 6))), - Kind::Title => Some("Title".into()), - Kind::Quote => Some("Quote".into()), - Kind::Code => Some(CODE_STYLE.into()), - Kind::Rule | Kind::Normal => None, - }, - ..Default::default() - }; - if p.kind == Kind::Rule { - out.borders = Some(rule_borders()); - } else if p.borders.is_some() { - out.borders = p.borders; - } - if let (Some(li), Some(num)) = (p.list, list_num) { - if out.style.is_none() { - out.style = Some("ListParagraph".into()); - } - out.numbering = Some(NumRef { num, level: li.level.min(8) }); - } - out.align = p.align; - let sp = p.spacing; - out.space_before = sp.before; - out.space_after = sp.after; - out.line_spacing = sp.line; - out.indent_left = sp.left; - out.indent_right = sp.right; - out.indent_first = sp.first; - if sp.before_auto { - out.space_before_auto = Some(true); - } - if sp.after_auto { - out.space_after_auto = Some(true); - } - if p.page_break { - out.page_break_before = Some(true); - } - if let Some(st) = p.extra.as_ref().and_then(|x| x.style.clone()) { - out.style = Some(st); - } - if let Some(c) = p.extra.as_ref().and_then(|x| x.contextual_spacing) { - out.contextual_spacing = Some(c); - } - if p.extra.as_ref().and_then(|x| x.bidi) == Some(true) { - out.bidi = Some(true); - } - if let Some(x) = p.extra.as_ref().filter(|x| x.drop_cap.is_some_and(|l| l > 1)) { - (out.drop_cap, out.drop_cap_distance) = - (x.drop_cap.map(|l| l.min(10)), x.drop_cap_distance.filter(|d| d.is_finite()).map(|d| d.clamp(0.0, 1584.0))); - } - if let Some(bg) = p.extra.as_ref().and_then(|x| x.shading) { - out.shading = Some(bg); - } - if let Some(x) = &p.extra { - out.keep_lines = x.keep_lines.or(out.keep_lines); - out.widow_control = x.widow_control.or(out.widow_control); - out.suppress_hyphens = x.suppress_hyphens.or(out.suppress_hyphens); - // An outline level apart from its heading style's. - out.outline_level = x.outline_level.map(|l| l.min(8)).or(out.outline_level); - } - // Tab stops a format gives the paragraph itself: finite, on the page, at most 64. - if let Some(tabs) = p.extra.as_ref().and_then(|x| x.tabs.as_ref()) { - let tabs: Vec<_> = tabs - .iter() - .filter(|t| t.pos.is_finite()) - .map(|t| wordcraft_doc::props::TabStop { pos: t.pos.clamp(-1584.0, 1584.0), ..*t }) - .take(64) - .collect(); - out.tabs = Some(tabs); - } - out -} - -struct Builder<'a> { - doc: &'a mut Document, - /// Revisions already in `doc` by (kind, author, date), so each change is added once. - revs: std::collections::HashMap<(u8, RevMark), u32>, - /// Link and font bytes given to runs so far ([`MAX_RUN_STRINGS`]). - run_strings: usize, - /// The document's comment for each flow comment id. - comments: std::collections::HashMap, - /// The document's list instance for each flow list number ([`ListInfo::list`]). - lists: std::collections::HashMap, - /// Bookmarks that cover text ([`super::types::ranged_anchors`]), those started, and those - /// ended: each started once and ended once, after its start. - ranged: std::collections::HashSet, - started: std::collections::HashSet, - ended: std::collections::HashSet, - /// Text boxes by name, and the boxes whose text continues in a box named so (linked text - /// boxes), linked once all are made ([`Builder::link_boxes`]). - boxes: std::collections::HashMap, - chains: Vec<(u32, String)>, - /// Tables made at the top of the flow so far (see `wordcraft_doc::kept`). - top_tables: usize, -} - -fn rev_key(kind: RevisionKind) -> u8 { - match kind { - RevisionKind::Insert => 0, - RevisionKind::Delete => 1, - RevisionKind::Format => 2, - } -} - -impl<'a> Builder<'a> { - /// A builder for `blocks` into `doc`. - fn new(doc: &'a mut Document, blocks: &[FBlock]) -> Builder<'a> { - let mut revs = std::collections::HashMap::new(); - for (i, r) in doc.revisions.iter().enumerate() { - if r.move_name.is_none() { - let mark = RevMark { author: r.author.clone(), date: r.date.clone(), id: None }; - revs.entry((rev_key(r.kind), mark)).or_insert(i as u32); - } - } - Builder { - doc, - revs, - run_strings: 0, - comments: Default::default(), - lists: Default::default(), - ranged: super::types::ranged_anchors(blocks), - started: Default::default(), - ended: Default::default(), - boxes: Default::default(), - chains: Vec::new(), - top_tables: 0, - } - } - - /// The document revision for a tracked change, added the first time it is seen. - fn revision(&mut self, kind: RevisionKind, m: &RevMark) -> u32 { - let key = (rev_key(kind), m.clone()); - if let Some(i) = self.revs.get(&key) { - return *i; - } - let i = u32::try_from(self.doc.revisions.len()).unwrap_or(u32::MAX); - self.doc.revisions.push(Revision::new(kind, m.author.clone(), m.date.clone())); - self.revs.insert(key, i); - i - } - - /// `cp` as a run may keep it: without its link and font once the runs' copies of them are - /// over budget. - fn budget(&mut self, mut cp: CharProps) -> CharProps { - let n = cp.link.as_ref().map_or(0, String::len).saturating_add(cp.font.as_ref().map_or(0, String::len)); - match self.run_strings.checked_add(n).filter(|&t| t <= MAX_RUN_STRINGS) { - Some(t) => self.run_strings = t, - None => { - if cp.link.is_some() || cp.font.is_some() { - dropped(Dropped::RunStrings); - } - (cp.link, cp.font) = (None, None); - } - } - cp - } - - /// Comment `c` (flow id `id`) added to the document with its text, as a reply when its parent - /// was added before it; its document id, or `None` when the document has no room for it or - /// the flow already used `id`. - fn comment(&mut self, id: u32, c: &super::types::FComment) -> Option { - if self.comments.contains_key(&id) { - return None; - } - let blocks = self.blocks(&c.blocks, 1); - let part = self.doc.try_add_part(wordcraft_doc::PartKind::Comment, blocks).ok()?; - let ids = &self.doc.comments; - let Some(doc_id) = wordcraft_doc::free_id(ids.keys().next_back().copied(), ids.keys().copied(), 0) else { - self.doc.parts.remove(&part); - return None; - }; - let parent = c.parent.and_then(|p| self.comments.get(&p).copied()); - let comment = wordcraft_doc::Comment { - author: clean_text(&c.author), - initials: clean_text(&c.initials), - date: clean_text(&c.date), - parent, - resolved: c.resolved, - part, - ..Default::default() - }; - self.doc.comments.insert(doc_id, comment); - self.comments.insert(id, doc_id); - Some(doc_id) - } - - /// A note story holding `blocks`, as Word's: its paragraphs in the note text style, the first - /// starting with the note's mark and a space. The reference to put in the text, or `None` - /// when the document has no room for another story. - fn note(&mut self, endnote: bool, custom: &str, blocks: &[FBlock]) -> Option { - use wordcraft_doc::para::NoteKind; - let (kind, part, text, mark) = if endnote { - (NoteKind::Endnote, wordcraft_doc::PartKind::Endnote, "EndnoteText", "EndnoteReference") - } else { - (NoteKind::Footnote, wordcraft_doc::PartKind::Footnote, "FootnoteText", "FootnoteReference") - }; - let custom: String = clean_text(custom).chars().filter(|c| !c.is_control()).take(10).collect(); - let id = self.doc.try_add_part(part, Vec::new()).ok()?; - let mut story = self.blocks(blocks, 1); - if story.is_empty() { - story.push(para_block(Paragraph::new())); - } - for (i, b) in story.iter_mut().enumerate() { - if let Block::Para(p) = Arc::make_mut(b) { - if p.props.style.is_none() { - p.props.style = Some(text.into()); - } - if i == 0 { - let starts_blank = p.text.starts_with([' ', '\t']); - let reference = InlineObject::NoteRef { kind, id, custom: custom.clone() }; - if p.insert_object(0, reference, &CharProps { style: Some(mark.into()), ..Default::default() }).is_ok() && !starts_blank { - let at = p.text.len().min(wordcraft_doc::para::OBJ.len_utf8()); - let _ = p.insert_text(at, " ", &CharProps::default()); - } - } - } - } - if let Some(part) = self.doc.parts.get_mut(&id) { - part.blocks = story; - } - Some(InlineObject::NoteRef { kind, id, custom }) - } - - /// The document object for picture, shape or group `i` (members of groups `depth` deep). - fn drawing(&mut self, i: &Inline, depth: usize) -> Option { - // Offsets and distances on the page. - let fit_float = |float: &Option>| { - float.as_deref().cloned().map(|mut f| { - let fit = |v: f32| if v.is_finite() { v.clamp(-1584.0, 1584.0) } else { 0.0 }; - (f.x, f.y) = (fit(f.x), fit(f.y)); - (f.dist, f.dist_top, f.dist_bottom) = (fit(f.dist).max(0.0), fit(f.dist_top).max(0.0), fit(f.dist_bottom).max(0.0)); - f - }) - }; - match i { - Inline::Image(img) => { - let key = self.doc.add_media(img.data.to_vec(), &img.ext); - let (w, h) = (finite_or(img.w, 72.0).clamp(1.0, 1584.0), finite_or(img.h, 72.0).clamp(1.0, 1584.0)); - let float = fit_float(&img.float); - let ole = img.object.as_deref().and_then(|o| super::odf_object::store(self.doc, o)); - // A crop of finite fractions that leaves something of the picture. - let crop = img.crop.map(|c| if c.is_finite() { c.clamp(0.0, 0.99) } else { 0.0 }); - let crop = if crop[0] + crop[2] < 0.99 && crop[1] + crop[3] < 0.99 { crop } else { [0.0; 4] }; - Some(InlineObject::Image { - media: key, - w, - h, - alt: img.alt.clone(), - float: float.unwrap_or_default(), - crop, - fx: Default::default(), - ole, - }) - } - // Its drawing is made from the chart by the program that opens the document - // (`wordcraft_engine`'s chart drawing), which has the theme's colours. - Inline::Chart(c) => { - let (w, h) = (finite_or(c.w, 360.0).clamp(1.0, 1584.0), finite_or(c.h, 216.0).clamp(1.0, 1584.0)); - let mut spec = (*c.spec).clone(); - spec.sanitize(); - let source = c.source.as_deref().and_then(|o| super::odf_object::store(self.doc, o)); - let graphic = wordcraft_doc::graphic::Graphic { - kind: wordcraft_doc::graphic::GraphicKind::Chart, - w, - h, - chart: Some(Arc::new(spec)), - source, - ..Default::default() - }; - Some(InlineObject::Graphic { - w, - h, - alt: clean_text(&c.alt), - float: fit_float(&c.float).unwrap_or_default(), - graphic: Arc::new(graphic), - }) - } - Inline::Shape { kind, w, h, float, blocks, look, path } => { - // A text box always has its story; a shape only when it holds text. - let story = if *kind == wordcraft_doc::para::ShapeKind::TextBox || !blocks.is_empty() { - let story = self.blocks(blocks, 1); - self.doc.try_add_part(wordcraft_doc::PartKind::TextBox, story).ok() - } else { - None - }; - // A named box, and the box its text continues in. - if let (Some(id), Some(c)) = (story, look.chain.as_deref()) { - if !c.name.is_empty() && self.boxes.len() < MAX_NAMED_BOXES { - self.boxes.entry(c.name.clone()).or_insert(id); - } - if let Some(next) = c.next.as_ref().filter(|n| !n.is_empty() && **n != c.name) { - self.chains.push((id, next.clone())); - } - } - if let Some(part) = story.and_then(|id| self.doc.parts.get_mut(&id)) { - let emu = |v: f32| if v.is_finite() { (v.clamp(0.0, 1584.0) * 12_700.0).round() as u32 } else { 0 }; - part.body.inset = look.inset.map(|i| i.map(emu)); - part.body.auto_fit = look.auto_fit; - part.body.vert = look.vert; - } - let (stroke, stroke_width) = match look.line { - Some((c, w)) if w.is_finite() && w > 0.0 => (Some(c), w.min(12.0)), - _ => (None, 0.0), - }; - // A line may be flat; anything else has some size. - let least = if *kind == wordcraft_doc::para::ShapeKind::Line { 0.0 } else { 1.0 }; - let fit = |v: f32| if v.is_finite() { v.clamp(least, 1584.0) } else { 72.0 }; - let obj = InlineObject::Shape { - kind: *kind, - w: fit(*w), - h: fit(*h), - fill: look.fill.clone().and_then(|f| f.sanitized()), - stroke, - stroke_width, - float: float.as_deref().cloned().unwrap_or_default(), - story, - freeform: path - .clone() - .filter(|_| *kind == wordcraft_doc::para::ShapeKind::Freeform) - .map(|f| std::sync::Arc::new((*f).clone().sanitized())), - effects: Default::default(), - extra: wordcraft_doc::connector::ShapeExtra { - dash: look.dash.clone(), - arrow_start: look.arrows.0, - arrow_end: look.arrows.1, - // Its drawing id, and what a connector's ends are glued to (sites bounded). - id: look.conn.as_ref().map_or(0, |c| c.id), - start: look.conn.as_ref().and_then(|c| c.start).filter(|e| e.site <= wordcraft_doc::connector::MAX_SITE), - end: look.conn.as_ref().and_then(|c| c.end).filter(|e| e.site <= wordcraft_doc::connector::MAX_SITE), - odf_geometry: look.geometry.clone().filter(|_| { - (*kind == wordcraft_doc::para::ShapeKind::Freeform && path.is_some()) - || *kind == wordcraft_doc::para::ShapeKind::Rectangle - }), - ..Default::default() - }, - }; - Some(obj) - } - Inline::Group { w, h, float, members } if depth < MAX_GROUP_DEPTH => { - let fit = |v: f32| if v.is_finite() { v.clamp(1.0, 1584.0) } else { 72.0 }; - let (w, h) = (fit(*w), fit(*h)); - let off = |v: f32| if v.is_finite() { v.clamp(-1584.0, 1584.0) } else { 0.0 }; - let children: Vec = members - .iter() - .take(MAX_GROUP_CHILDREN) - .filter_map(|m| self.drawing(&m.inline, depth + 1).map(|obj| GroupChild { x: off(m.x), y: off(m.y), obj })) - .collect(); - (!children.is_empty()).then(|| InlineObject::Group { - w, - h, - float: float.as_deref().cloned().unwrap_or_default(), - ch_w: w, - ch_h: h, - children, - canvas: None, - }) - } - _ => None, - } - } - - /// Keeps a paragraph's markup read from the file (its own, its frame wrap, the index it - /// starts) pending until the document is sealed. - fn keep_pending(&mut self, p: &Para, rev: u64) { - if p.kept.is_none() && p.wrap.is_none() && p.index.is_none() { - return; - } - let group = p.group.as_ref().and_then(|g| g.id).filter(|_| p.kept.is_some()); - if self.doc.kept.get().is_none() { - self.doc.kept = wordcraft_doc::kept::Kept::new(Default::default()); - } - if let Some(k) = self.doc.kept.get_mut() { - if let Some(xml) = &p.kept { - k.keep_pending(rev, xml.clone()); - } - if let Some(w) = &p.wrap { - k.keep_pending_wrap(rev, (**w).clone()); - } - if let Some(ix) = &p.index { - k.keep_pending_index(rev, ix.1, ix.0.clone()); - } - if let Some(g) = group { - k.keep_pending_group(rev, g); - } - } - } - - fn para(&mut self, p: &Para, list_num: Option) -> Paragraph { - let mut out = Paragraph::new(); - out.props = para_props(p, list_num); - if let Some(c) = &p.fmt_change { - // A list item stays in its list instance (only its level may differ); an old list on a - // paragraph that is no list now gets an instance of its own, so Reject can restore it. - // The same goes for an item whose list kind changed (numbered to bulleted or back). - let old_num = match (c.old.list, p.list, list_num) { - (Some(old), Some(now), Some(num)) if old.ordered == now.ordered => Some(num), - (Some(old), _, _) => Some(self.new_list(&[old])), - _ => None, - }; - let old = para_props(&c.old, old_num); - out.props.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, &c.mark), old); - } - if p.keep_next { - out.props.keep_next = Some(true); - } - out.section.clone_from(&p.section); - // Inlines are appended in order, so build the runs directly: inserting each one through - // `Paragraph::insert_text` rescans every earlier run and is quadratic in their number. - for i in &p.inlines { - match i { - Inline::Text(t, f) => { - let t = clean_text(t); - let mut cp = self.budget(char_props(f)); - cp.ins = f.ins.as_ref().map(|m| self.revision(RevisionKind::Insert, m)); - cp.del = f.del.as_ref().map(|m| self.revision(RevisionKind::Delete, m)); - if let Some(c) = &f.fmt_change { - let old = self.budget(char_props(&c.old)); - cp.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, &c.mark), old); - } - if p.kind == Kind::Code { - // Code's font comes from its style, before a change as after it. - if cp.font.as_deref() == Some(MONO_FONT) { - cp.font = None; - } - if let Some(c) = cp.fmt_change.as_deref_mut() - && c.old.font.as_deref() == Some(MONO_FONT) - { - c.old.font = None; - } - } - append(&mut out, &t, &cp, None); - } - Inline::FieldStart(instr) => { - let obj = InlineObject::FieldStart { instr: clean_text(instr), locked: false, dirty: false }; - append(&mut out, "", &CharProps::default(), Some(obj)); - } - Inline::FieldEnd => append(&mut out, "", &CharProps::default(), Some(InlineObject::FieldEnd)), - Inline::Image(_) | Inline::Chart(_) | Inline::Shape { .. } | Inline::Group { .. } => { - if let Some(mut obj) = self.drawing(i, 0) { - // In line, raised or lowered: its run's position (as Word keeps it). In - // a paragraph of its own, its run has the paragraph's formatting. - let mut cp = p - .mark - .as_deref() - .filter(|_| p.textless()) - .map(|m| self.budget(CharProps { ins: None, del: None, fmt_change: None, ..m.clone() })) - .unwrap_or_default(); - if let Some(f) = obj.float_mut() - && f.wrap == wordcraft_doc::para::Wrap::Inline - && f.y != 0.0 - { - cp.position = Some(f.y.clamp(-1584.0, 1584.0)); - f.y = 0.0; - } - append(&mut out, "", &cp, Some(obj)); - } - } - // A bookmark once (the first of a name): a point, or the start of the text it covers. - Inline::Anchor(name) if self.started.insert(name.clone()) => { - append(&mut out, "", &CharProps::default(), Some(InlineObject::BookmarkStart { name: name.clone() })); - if !self.ranged.contains(name) { - append(&mut out, "", &CharProps::default(), Some(InlineObject::BookmarkEnd { name: name.clone() })); - } - } - Inline::Anchor(_) => {} - Inline::AnchorEnd(name) => { - if self.ranged.contains(name) && self.started.contains(name) && self.ended.insert(name.clone()) { - append(&mut out, "", &CharProps::default(), Some(InlineObject::BookmarkEnd { name: name.clone() })); - } - } - Inline::Figure(_) => {} - Inline::Comment { id, comment } => { - // A reply is the comment's, not the text's: it has no range. - if let Some(doc_id) = self.comment(*id, comment) - && comment.parent.is_none() - { - append(&mut out, "", &CharProps::default(), Some(InlineObject::CommentStart { id: doc_id })); - } - } - Inline::CommentEnd(id) => { - if let Some(doc_id) = self.comments.get(id) { - append(&mut out, "", &CharProps::default(), Some(InlineObject::CommentEnd { id: *doc_id })); - } - } - Inline::Note { endnote, custom, blocks, fmt } => { - if let Some(obj) = self.note(*endnote, custom, blocks) { - let style = if *endnote { "EndnoteReference" } else { "FootnoteReference" }; - let cp = CharProps { style: Some(style.into()), ..self.budget(char_props(fmt)) }; - append(&mut out, "", &cp, Some(obj)); - } - } - Inline::Field { instr, result, fmt, locked } => { - let cp = self.budget(char_props(fmt)); - let (instr, result) = (clean_text(instr), clean_text(result)); - if instr.trim().is_empty() { - append(&mut out, &result, &cp, None); - } else { - let obj = InlineObject::Field { instr, result, locked: *locked, dirty: false, code: false, form: String::new() }; - append(&mut out, "", &cp, Some(obj)); - } - } - // A content control around its text. - Inline::Control { control, text, fmt } => { - let cp = self.budget(char_props(fmt)); - let mut c = control.clone(); - c.block = false; - append(&mut out, "", &cp, Some(InlineObject::ControlStart { control: c })); - append(&mut out, &clean_text(text), &cp, None); - append(&mut out, "", &cp, Some(InlineObject::ControlEnd)); - } - Inline::Equation { linear, display } => { - let linear = clean_text(linear); - if !linear.is_empty() { - let obj = InlineObject::Equation { linear, display: *display, math: Default::default() }; - append(&mut out, "", &CharProps::default(), Some(obj)); - } - } - Inline::Ruby { base, ruby, fmt } => { - let (base, ruby) = (clean_text(base), clean_text(ruby)); - let cp = self.budget(char_props(fmt)); - // A source without a base or without a reading (malformed markup) is just its - // text: a guide that annotates nothing is no guide. - if base.is_empty() || ruby.is_empty() { - append(&mut out, if base.is_empty() { &ruby } else { &base }, &cp, None); - } else { - // Ruby attributes a format without them doesn't state (HTML): the - // Phonetic Guide's defaults for a new guide over this run's text. - let obj = ruby_object(base, ruby, cp.size.unwrap_or(RUBY_BASE_SIZE)); - append(&mut out, "", &cp, Some(obj)); - } - } - } - } - if let Some(m) = p.mark.as_deref().filter(|_| p.textless()) { - out.mark = self.budget(CharProps { ins: None, del: None, fmt_change: None, ..m.clone() }); - } - out.mark.ins = p.mark_ins.as_deref().map(|m| self.revision(RevisionKind::Insert, m)); - out.mark.del = p.mark_del.as_deref().map(|m| self.revision(RevisionKind::Delete, m)); - out.normalize(); - out.touch(); - // Its markup kept for the writer, as long as nothing changes the paragraph from here on - // (see `wordcraft_doc::kept`: the reader seals it once the document is done). - self.keep_pending(p, out.rev); - out - } - - /// A new list instance whose levels follow the kinds used in `group` (first use per level). - fn new_list(&mut self, group: &[ListInfo]) -> u32 { - let num_levels = levels_for(ListKind::Numbered); - let bul_levels = levels_for(ListKind::Bullet); - let mut kinds: [Option; 9] = [None; 9]; - for li in group { - if let Some(k) = kinds.get_mut(li.level.min(8) as usize) - && k.is_none() - { - *k = Some(li.ordered); - } - } - let mut starts: [Option; 9] = [None; 9]; - let mut labels: [Option; 9] = [None; 9]; - for li in group { - if let Some(s) = starts.get_mut(li.level.min(8) as usize) - && s.is_none() - { - *s = Some(li.start); - } - if let Some(l) = labels.get_mut(li.level.min(8) as usize) - && l.is_none() - { - *l = li.label; - } - } - // Levels nobody used follow the first level's kind. - let first = group.first().map(|l| l.ordered).unwrap_or(false); - let levels: Vec = (0..9) - .map(|i| { - let ordered = kinds.get(i).copied().flatten().unwrap_or(first); - let src = if ordered { &num_levels } else { &bul_levels }; - let mut level = src.get(i).cloned().unwrap_or_default(); - if ordered && let Some(s) = starts.get(i).copied().flatten() { - level.start = s; - } - // The source's own label: its format and text, or its bullet. - match labels.get(i).copied().flatten() { - Some(l) if ordered && l.format != wordcraft_doc::section::NumFormat::Bullet => { - level.format = l.format; - level.text = l.text(i as u8); - } - Some(l) if !ordered => { - if let Some(c) = l.bullet().filter(|c| !c.is_control() && !level.text.starts_with(*c)) { - let custom = levels_for(ListKind::BulletChar(c)); - level.text = c.to_string(); - level.chr.font = custom.first().and_then(|l| l.chr.font.clone()); - } - } - _ => {} - } - if let Some(l) = labels.get(i).copied().flatten() { - if let Some((indent, hanging)) = l.position() { - (level.indent, level.hanging) = (indent, hanging); - } - if let Some(a) = l.align { - level.align = a; - } - } - level - }) - .collect(); - let numbering = Arc::make_mut(&mut self.doc.numbering); - let aid = numbering.abstracts.iter().map(|a| a.id + 1).max().unwrap_or(0); - numbering.abstracts.push(AbstractNum { id: aid, name: None, levels, ..Default::default() }); - let nid = numbering.nums.iter().map(|n| n.id + 1).max().unwrap_or(1).max(1); - numbering.nums.push(Num { id: nid, abstract_id: aid, ..Default::default() }); - nid - } - - fn blocks(&mut self, blocks: &[FBlock], depth: usize) -> Blocks { - let mut out = Blocks::new(); - let mut i = 0; - while let Some(b) = blocks.get(i) { - match b { - FBlock::Para(p) if p.list.is_some() => { - // A run of consecutive list paragraphs is one list, until an item of another - // list or (when the source doesn't tell lists apart) a top-level item of the - // other kind (bulleted or numbered) starts a new one. A list the source - // numbers goes on where it shows up again. - let mut j = i; - let mut group: Vec = Vec::new(); - let mut top: Option = None; - let id = p.list.map_or(0, |l| l.list); - while let Some(FBlock::Para(q)) = blocks.get(j) { - let Some(li) = q.list else { break }; - if li.list != id { - break; - } - if li.level == 0 && id == 0 { - if top.is_some_and(|o| o != li.ordered) { - break; - } - top = Some(li.ordered); - } - group.push(li); - j += 1; - } - let num = match self.lists.get(&id) { - Some(num) if id != 0 => *num, - _ => { - let num = self.new_list(&group); - if id != 0 { - self.lists.insert(id, num); - } - num - } - }; - for q in blocks.get(i..j).unwrap_or(&[]) { - if let FBlock::Para(q) = q { - let para = self.para(q, Some(num)); - out.push(para_block(para)); - } - } - i = j.max(i + 1); - continue; - } - FBlock::Para(p) => out.push(para_block(self.para(p, None))), - FBlock::Table(t) => { - if depth >= MAX_DEPTH { - // Too deep: flatten into paragraphs. - for row in &t.rows { - for c in row { - out.extend(self.blocks(&c.blocks, depth)); - } - } - } else if let Some(tb) = self.table(t, depth) { - out.push(Arc::new(Block::Table(tb))); - } - } - } - i += 1; - } - out - } - - fn table(&mut self, t: &FTable, depth: usize) -> Option { - let rows: Vec<(usize, &Vec)> = t.rows.iter().enumerate().filter(|(_, r)| !r.is_empty()).take(wordcraft_doc::table::MAX_ROWS).collect(); - if rows.is_empty() { - return None; - } - let cols = t.cols().clamp(1, wordcraft_doc::table::MAX_COLS); - let width = self.doc.last_section.text_width().max(72.0); - // One template row for the grid widths and props; the real rows are built below. Sizing it - // to `rows.len()` would allocate a full rows × cols cell grid only to clear it, which a - // tall table (many rows, wide from merged cells) turns into a large transient spike. - let mut tb = Table::new(1, cols, width); - // A table from a format that keeps cell formatting (ODT) has its cells' own borders and no - // table style of Word's kind: Table Grid's single spacing would change its text. - if t.borderless || t.extra.is_some() { - tb.props.style = None; - } - if t.widths.len() == cols && t.widths.iter().all(|w| w.is_finite() && *w > 1.0) { - tb.grid = t.widths.iter().map(|w| w.clamp(6.0, 1584.0)).collect(); - } - tb.rows.clear(); - if let Some(x) = &t.extra { - tb.props.align = x.align; - tb.props.width_pct = x.width_pct.filter(|p| p.is_finite() && *p > 0.0).map(|p| p.min(100.0)); - tb.props.width = x.width.filter(|w| w.is_finite() && *w > 0.0).map(|w| w.min(1584.0)); - tb.props.fixed |= x.fixed; - let space = |v: Option| v.filter(|v| v.is_finite() && *v > 0.0).map(|v| v.min(1584.0)); - (tb.props.space_before, tb.props.space_after) = (space(x.space[0]), space(x.space[1])); - } - for (ri, r) in rows { - let mut row = wordcraft_doc::Row { props: RowProps::default(), cells: Vec::new(), controls: Default::default() }; - row.props.header = !r.is_empty() && r.iter().all(|c| c.header); - if let Some(&Some((h, exact))) = t.extra.as_ref().and_then(|x| x.heights.get(ri)) - && h.is_finite() - && h > 0.0 - { - row.props.height = Some(h.min(1584.0)); - row.props.height_rule = if exact { wordcraft_doc::props::HeightRule::Exact } else { wordcraft_doc::props::HeightRule::AtLeast }; - } - row.props.cant_split = t.extra.as_ref().is_some_and(|x| x.keep_rows.get(ri) == Some(&true)); - let mut g = 0usize; - for c in r.iter().take(wordcraft_doc::table::MAX_COLS) { - let span = (c.colspan.clamp(1, 63) as usize).min(cols.saturating_sub(g).max(1)); - let w: f32 = tb.grid.get(g..(g + span).min(tb.grid.len())).map(|s| s.iter().sum()).unwrap_or(72.0); - g += span; - let mut blocks = self.blocks(&c.blocks, depth + 1); - if blocks.is_empty() || matches!(blocks.last().map(|b| &**b), Some(Block::Table(_))) { - blocks.push(para_block(Paragraph::new())); - } - let vmerge = if c.covered { - VMerge::Continue - } else if c.rowspan > 1 { - VMerge::Restart - } else { - VMerge::None - }; - // A background picture goes into the document's media. - let picture = c.picture.as_deref().map(|p| { - let media = self.doc.add_media(p.img.data.as_ref().clone(), &p.img.ext); - Box::new(wordcraft_doc::props::CellPicture { media, size: [p.img.w, p.img.h], repeat: p.repeat, align: p.align }) - }); - row.cells.push(wordcraft_doc::Cell { - props: CellProps { - width: Some(w), - span: span as u32, - vmerge, - shading: c.shading, - borders: c.borders, - margins: c.margins.filter(|m| m.iter().all(|v| v.is_finite())).map(|m| m.map(|v| v.clamp(0.0, 1584.0))), - valign: c.valign, - text_direction: c.direction, - picture, - ..Default::default() - }, - blocks, - controls: Default::default(), - }); - } - if row.cells.is_empty() { - row.cells.push(wordcraft_doc::Cell::empty()); - } - tb.rows.push(row); - } - // Its markup kept for the writer, by its place among the top's tables. - if depth == 0 { - if t.kept.is_some() || t.wrap.is_some() { - if self.doc.kept.get().is_none() { - self.doc.kept = wordcraft_doc::kept::Kept::new(Default::default()); - } - if let Some(k) = self.doc.kept.get_mut() { - if let Some(xml) = &t.kept { - k.keep_pending_table(self.top_tables, xml.clone()); - } - if let Some(w) = &t.wrap { - k.keep_pending_table_wrap(self.top_tables, (**w).clone()); - } - } - } - self.top_tables += 1; - } - Some(tb) - } -} - -fn finite_or(v: f32, d: f32) -> f32 { - if v.is_finite() { v } else { d } -} - -/// Build a document from a flow. -pub fn to_doc(flow: &Flow) -> Document { - to_doc_in(flow, wordcraft_doc::SectionProps::default()) -} - -/// [`to_doc`] on a page of the source's own size and margins (`section`), which tables size to. -/// Most named text boxes linked. -const MAX_NAMED_BOXES: usize = 10_000; - -impl Builder<'_> { - /// Each text box whose text continues in another (by name) linked to it, a chain at a time - /// (a box is in at most one chain, and a chain doesn't loop back on itself). - fn link_boxes(&mut self) { - let mut linked_to = std::collections::HashSet::new(); - for (from, name) in std::mem::take(&mut self.chains) { - let Some(&to) = self.boxes.get(&name) else { continue }; - if to == from || !linked_to.insert(to) { - continue; - } - if let Some(p) = self.doc.parts.get_mut(&from) { - p.body.next = Some(to); - } - } - // A link that closes a loop is undone. - for id in self.boxes.values().copied().collect::>() { - let mut seen = std::collections::HashSet::new(); - let mut at = id; - while let Some(next) = self.doc.parts.get(&at).and_then(|p| p.body.next) { - if !seen.insert(at) || seen.len() > MAX_NAMED_BOXES { - if let Some(p) = self.doc.parts.get_mut(&at) { - p.body.next = None; - } - break; - } - at = next; - } - } - } -} - -pub fn to_doc_in(flow: &Flow, section: wordcraft_doc::SectionProps) -> Document { - let mut doc = Document::new(); - doc.last_section = section; - doc.numbering = Default::default(); - let uses_code = uses_kind(&flow.blocks, Kind::Code, 0); - if uses_code { - Arc::make_mut(&mut doc.styles).upsert(code_style()); - } - let mut b = Builder::new(&mut doc, &flow.blocks); - let body = b.blocks(&flow.blocks, 0); - b.link_boxes(); - doc.body = body; - let m = &flow.meta; - doc.core.title = m.title.clone(); - doc.core.creator = m.author.clone(); - doc.core.subject = m.subject.clone(); - doc.core.keywords = m.keywords.clone(); - doc.core.description = m.description.clone(); - doc.core.created = m.created.clone(); - doc.ensure_nonempty(); - doc -} - -/// `blocks` as document blocks of `doc` (their pictures go into its media), for a header, -/// footer or other part an importer adds itself. -pub fn part_blocks(doc: &mut Document, blocks: &[FBlock]) -> Blocks { - let mut out = Builder::new(doc, blocks).blocks(blocks, 0); - if out.is_empty() { - out.push(para_block(Paragraph::new())); - } - out -} diff --git a/crates/formats/src/model/types.rs b/crates/formats/src/model/types.rs deleted file mode 100644 index 01049a3d07..0000000000 --- a/crates/formats/src/model/types.rs +++ /dev/null @@ -1,1089 +0,0 @@ -use super::{Align, Arc, InlineObject, OBJ, REPORTED, Rgb, RubyAlign, VAlign, ruby_raise}; - -/// Font used for code spans and code blocks. -pub const MONO_FONT: &str = "Courier New"; - -/// Style id of code-block paragraphs. -pub const CODE_STYLE: &str = "Code"; - -/// Nesting limit for tables in tables (hostile input). -pub const MAX_DEPTH: usize = 8; - -/// Deepest groups in groups kept (hostile files). -pub const MAX_GROUP_DEPTH: usize = 8; - -/// Longest font name the importers keep, in bytes (as the .docx reader); a longer one (no real -/// font has one) is dropped (#3713). -pub const MAX_FONT_NAME: usize = 256; - -/// Longest hyperlink target the importers keep, in bytes; a longer link is dropped and its text -/// kept (#3713). -pub const MAX_LINK: usize = 8 << 10; - -/// Link and font bytes the document's runs may carry between them. A flow's spans share one -/// copy of a name ([`Fmt`]), but every run of a document owns its own: past this budget, runs -/// lose their link and font rather than multiply a small file into gigabytes (#3713). -pub(super) const MAX_RUN_STRINGS: usize = 64 << 20; - -/// `name` as a span's font, unless it is longer than [`MAX_FONT_NAME`] (then reported, see -/// [`dropped`]). -pub fn kept_font(name: &str) -> Option> { - let kept = (name.len() <= MAX_FONT_NAME).then(|| name.into()); - if kept.is_none() { - dropped(Dropped::Font); - } - kept -} - -/// `target` as a span's link, unless it is longer than [`MAX_LINK`] (then reported, see -/// [`dropped`]). -pub fn kept_link(target: &str) -> Option> { - let kept = (target.len() <= MAX_LINK).then(|| target.into()); - if kept.is_none() { - dropped(Dropped::Link); - } - kept -} - -/// What an importer leaves out to stay within a limit (review of #3873: no limit drops data -/// without saying so). -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum Dropped { - /// A font name over [`MAX_FONT_NAME`]. - Font, - /// A link target over [`MAX_LINK`]. - Link, - /// Runs' links and fonts past [`MAX_RUN_STRINGS`]. - RunStrings, - /// A style name cut to [`MAX_FONT_NAME`]. - #[allow(dead_code)] - StyleName, - /// ODT `text:s` spaces past the document's budget. - Spaces, - /// Markdown table rows and columns past the table limits or the document's cell budget. - TableCells, -} - -impl Dropped { - fn message(self) -> String { - match self { - Dropped::Font => format!("import: font names longer than {MAX_FONT_NAME} bytes are dropped; that text keeps the surrounding font"), - Dropped::Link => format!("import: links longer than {MAX_LINK} bytes are dropped; their text is kept"), - Dropped::RunStrings => { - format!("import: the document's runs carry more than {MAX_RUN_STRINGS} bytes of links and font names; later runs lose them") - } - Dropped::StyleName => format!("import: style names longer than {MAX_FONT_NAME} bytes are cut short"), - Dropped::Spaces => { - "import: the document's `text:s` elements ask for more spaces than it keeps; past that each stands for one".to_string() - } - Dropped::TableCells => format!( - "import: Markdown tables keep {} columns and {} rows at most, and {} cells in all; the rest are left out", - wordcraft_doc::table::MAX_COLS, - wordcraft_doc::table::MAX_ROWS, - crate::markdown::MAX_TABLE_CELLS - ), - } - } - - /// Whether this thread's import has logged this kind ([`dropped`]). - #[cfg(test)] - pub(crate) fn reported(self) -> bool { - REPORTED.with(|r| r.get() & (1u8 << (self as u8)) != 0) - } -} - -/// Log that the importer left something of `kind` out: once per kind per imported document -/// ([`begin_import`]), not once per item. -pub(crate) fn dropped(kind: Dropped) { - let bit = 1u8 << (kind as u8); - if REPORTED.with(|r| r.replace(r.get() | bit)) & bit == 0 { - log::warn!("{}", kind.message()); - } -} - -/// Start a new document's reports: each importer's entry point calls this first. -pub(crate) fn begin_import() { - REPORTED.with(|r| r.set(0)); -} - -/// Character formatting the text formats can express. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct Fmt { - pub bold: bool, - pub italic: bool, - pub underline: bool, - pub strike: bool, - pub code: bool, - pub sup: bool, - pub sub: bool, - /// Shared: every span of a link holds the same target. - pub link: Option>, - pub color: Option, - /// Points. - pub size: Option, - /// Shared, as `link`. - pub font: Option>, - /// Background (highlight / shading). - pub background: Option, - /// Tracked insertion of this text (formats that keep revisions: RTF). - pub ins: Option, - /// Tracked deletion of this text. Only [`from_doc_tracked`] keeps deleted text in a flow. - pub del: Option, - /// Tracked formatting change of this text: who made it and the formatting before it (only - /// [`from_doc_tracked`] and formats that keep revisions set it). - pub fmt_change: Option>>, - /// Formatting beyond the fields above, for formats that keep it (ODT): underline style and - /// colour, double strike, caps, small caps, hidden, spacing, scale, position, outline, - /// shadow, emboss, engrave and language, as character properties. Only flows asked for it - /// ([`FlowOpts::chars`]) hold it. - pub extra: Option>, -} - -/// A tracked formatting change in a flow: who made it, and the formatting before it. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct FmtChange { - pub mark: RevMark, - pub old: T, -} - -/// Who made a tracked change and when (ISO 8601; empty = unknown). -#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] -pub struct RevMark { - pub author: String, - pub date: String, - /// The source's identity of the change, when it has one (an ODT changed region; the - /// revision index in [`from_doc_tracked`]): marks with different ids stay separate - /// revisions. `None` (RTF) = one revision per kind, author and date. - pub id: Option, -} - -/// An embedded picture. -#[derive(Clone, Debug, PartialEq)] -pub struct Img { - pub data: Arc>, - /// File extension (`png`, `jpeg`…). - pub ext: String, - /// Display size, points. - pub w: f32, - pub h: f32, - pub alt: String, - /// How a floating picture is placed and wrapped, for formats that keep it (ODT); `None`: in - /// line with the text. - pub float: Option>, - /// The embedded object this picture shows, kept whole (ODT only; see [`OdfObject`]). - pub object: Option>, - /// How much is cut off its left, top, right and bottom, fractions of its size (0..1), for - /// formats that keep it (ODT). - pub crop: [f32; 4], -} - -/// An embedded object WordCraft shows but doesn't edit, kept whole for saving back (ODF 1.3 -/// §10.4.6 `draw:object`, a sub-document such as a spreadsheet, or §10.4.7 `draw:object-ole`). -#[derive(Clone, Debug, Default, PartialEq)] -pub struct OdfObject { - /// Its media type (`application/vnd.oasis.opendocument.spreadsheet`…). - pub mime: String, - /// An OLE object: one binary file (`draw:object-ole`) rather than a sub-document folder. - pub ole: bool, - /// Its files, by their path in its folder (an OLE object's one file: ""). - pub files: Vec<(String, Arc>)>, - /// The replacement picture as read (LibreOffice's own metafile format, say), written back - /// in place of the picture shown. - pub replacement: Option>>, -} - -/// An editable chart in a flow ([`Inline::Chart`]). -#[derive(Clone, Debug, PartialEq)] -pub struct Chart { - pub spec: Arc, - /// Display size, points. - pub w: f32, - pub h: f32, - pub alt: String, - /// How a floating chart is placed and wrapped; `None`: in line with the text. - pub float: Option>, - /// The chart object as read (ODT), written back whole while the chart is unchanged, so it - /// keeps the look WordCraft doesn't model (its fonts, lines and colours). - pub source: Option>, -} - -#[derive(Clone, Debug, PartialEq)] -pub enum Inline { - Text(String, Fmt), - Image(Img), - /// A bookmark (link target). - Anchor(String), - /// Where bookmark `name`, started by an [`Inline::Anchor`] before it, ends: the bookmark - /// covers the text between. Only flows asked for fields ([`FlowOpts::fields`]) hold them; - /// an anchor without one is a point. - AnchorEnd(String), - /// A chart or diagram known here only by its alt text: written where alt text has a place - /// (an HTML or Markdown image), left out of editable formats so it never turns into body text. - Figure(String), - /// A chart WordCraft can edit, `w` × `h` points: only in flows asked for boxes - /// ([`FlowOpts::boxes`]); others get its alt text as an [`Inline::Figure`]. - Chart(Chart), - /// A content control holding plain `text` (a form control in ODT): only in flows asked for - /// boxes ([`FlowOpts::boxes`]); others get its text. - Control { - control: Box, - text: String, - fmt: Fmt, - }, - /// An equation in the linear format WordCraft keeps them in (`x=(-b±√(b^2-4ac))/2a`). Formats - /// without equations write `linear` as text. - Equation { - linear: String, - display: bool, - }, - /// Ruby (phonetic guide): `ruby` text (furigana, pinyin…) over `base` text, in the formatting - /// of the run it sits in (#1681). Formats without ruby write the base text - /// ([`ruby_as_text`]). - Ruby { - base: String, - ruby: String, - fmt: Fmt, - }, - /// A field (page number, date, document property, cross-reference): its Word field code - /// (`PAGE`, `REF name \h`…) and the result it last showed, in its run's formatting. Only - /// flows asked for fields ([`FlowOpts::fields`]) hold them; formats without fields write the - /// result as text. - Field { - instr: String, - result: String, - fmt: Fmt, - /// Its result is kept, not worked out again (Word's locked field, ODF's `text:fixed`). - locked: bool, - }, - /// A footnote or endnote: its custom mark (empty: numbered) and its content, without the - /// note's own mark. Only flows asked for notes ([`FlowOpts::notes`]) hold them; formats - /// without notes leave them out. - Note { - endnote: bool, - custom: String, - blocks: Vec, - /// The reference's own formatting (beyond the note reference style). - fmt: Fmt, - }, - /// Where comment `id` (the flow's own number for it) starts, and the comment. A reply - /// ([`FComment::parent`]) has no range: it only follows its comment. Only flows asked for - /// comments ([`FlowOpts::comments`]) hold them. - Comment { - id: u32, - comment: Box, - }, - /// Where comment `id`'s range ends. - CommentEnd(u32), - /// A drawing shape or text box: its kind, its size in points, its placement when it floats, - /// its text (none for a shape without), and its look. Only flows asked for them - /// ([`FlowOpts::boxes`]) hold them. - Shape { - kind: wordcraft_doc::para::ShapeKind, - w: f32, - h: f32, - float: Option>, - blocks: Vec, - look: BoxLook, - /// A freeform's paths ([`wordcraft_doc::para::ShapeKind::Freeform`]). - path: Option>, - }, - /// The start of a field whose result is the content after it, up to the matching - /// [`Inline::FieldEnd`] (a table of contents around its entries); its field code. Only flows - /// asked for fields ([`FlowOpts::fields`]) hold them. - FieldStart(String), - /// The end of the innermost [`Inline::FieldStart`]. - FieldEnd, - /// Drawing shapes, text boxes and pictures grouped into one object: its size in points, its - /// placement when it floats, and its members, each at its offset in the group. Only flows - /// asked for shapes ([`FlowOpts::boxes`]) hold them. - Group { - w: f32, - h: f32, - float: Option>, - members: Vec, - }, -} - -/// A member of an [`Inline::Group`]: a shape, text box or picture (its placement in the group is -/// `x`, `y`, points from the group's top left corner; its own float holds only its turn and -/// mirroring). -#[derive(Clone, Debug, PartialEq)] -pub struct Member { - pub x: f32, - pub y: f32, - pub inline: Inline, -} - -/// How a text box or shape looks: its fill, its outline (colour and width, dash and -/// arrowheads), its inner margins, and whether it grows to fit its text. -#[derive(Clone, Debug, PartialEq)] -pub struct BoxLook { - /// Fill: a colour or a gradient; `None` is no fill. - pub fill: Option, - /// Outline colour and width in points; `None` is no line. - pub line: Option<(Rgb, f32)>, - /// The outline's dash pattern. - pub dash: wordcraft_doc::line_dash::LineDash, - /// Arrowheads at a line's start and end. - pub arrows: (bool, bool), - /// Inner margins (left, top, right, bottom) in points; `None` is the default. - pub inset: Option<[f32; 4]>, - /// The box grows to its text; its height is the least it is. - pub auto_fit: bool, - /// The drawing id connectors glue to, and a connector's glued ends (see - /// [`wordcraft_doc::connector`]). - pub conn: Option>, - /// A text box's name and the box its text continues in when it doesn't fit (linked text - /// boxes), by name. - pub chain: Option>, - /// Which way a text box's text runs (turned down or up). - pub vert: wordcraft_doc::props::TextVert, - /// A custom shape's geometry as read (ODT; see `ShapeExtra::odf_geometry`). - pub geometry: Option>, -} - -/// A text box's name, and the name of the box its text continues in ([`BoxLook::chain`]). -#[derive(Clone, Debug, Default, PartialEq)] -pub struct Chain { - pub name: String, - pub next: Option, -} - -/// A shape's drawing id (0: none) and, for a connector, what its ends are glued to. -#[derive(Clone, Copy, Debug, Default, PartialEq)] -pub struct Conn { - pub id: u32, - pub start: Option, - pub end: Option, -} - -impl Default for BoxLook { - /// A new text box's: white, with a thin black line. - fn default() -> BoxLook { - BoxLook { - fill: Some(wordcraft_doc::fill::Fill::solid(Rgb::WHITE)), - line: Some((Rgb::BLACK, 0.75)), - dash: Default::default(), - arrows: (false, false), - inset: None, - auto_fit: false, - conn: None, - chain: None, - vert: Default::default(), - geometry: None, - } - } -} - -/// The bookmarks in `blocks` whose end ([`Inline::AnchorEnd`]) follows their start -/// ([`Inline::Anchor`]): ranges, not points. Tables' cells, notes, comments and shapes' text -/// are looked through, in document order. -pub fn ranged_anchors(blocks: &[FBlock]) -> std::collections::HashSet { - fn walk(blocks: &[FBlock], depth: usize, started: &mut std::collections::HashSet, ranged: &mut std::collections::HashSet) { - if depth > MAX_DEPTH { - return; - } - for b in blocks { - match b { - FBlock::Para(p) => { - for i in &p.inlines { - match i { - Inline::Anchor(n) => { - started.insert(n.clone()); - } - Inline::AnchorEnd(n) if started.contains(n) => { - ranged.insert(n.clone()); - } - Inline::Note { blocks, .. } | Inline::Shape { blocks, .. } => walk(blocks, depth + 1, started, ranged), - Inline::Comment { comment, .. } => walk(&comment.blocks, depth + 1, started, ranged), - _ => {} - } - } - } - FBlock::Table(t) => { - for c in t.rows.iter().flatten() { - walk(&c.blocks, depth + 1, started, ranged); - } - } - } - } - } - let (mut started, mut ranged) = Default::default(); - walk(blocks, 0, &mut started, &mut ranged); - ranged -} - -/// `blocks` with each table replaced by the blocks of its cells, row by row (nested tables too), -/// for places that hold paragraphs only. -pub fn flatten_tables(blocks: &[FBlock]) -> Vec { - let mut out = Vec::new(); - let mut stack: Vec> = vec![blocks.iter()]; - while let Some(top) = stack.last_mut() { - match top.next() { - Some(FBlock::Para(p)) => out.push(FBlock::Para(p.clone())), - Some(FBlock::Table(t)) => { - // Cells in reverse so they come off the stack in order. - for c in t.rows.iter().rev().flat_map(|r| r.iter().rev()) { - stack.push(c.blocks.iter()); - } - } - None => { - stack.pop(); - } - } - } - out -} - -/// A comment in a flow ([`Inline::Comment`]). -#[derive(Clone, Debug, Default, PartialEq)] -pub struct FComment { - pub author: String, - pub initials: String, - /// ISO 8601; empty = unknown. - pub date: String, - pub resolved: bool, - /// The comment this one answers (its flow id). - pub parent: Option, - pub blocks: Vec, -} - -/// A field that never shows its result: `ASK` and `SET` only assign text to a bookmark -/// (ECMA-376 §17.16.5.4, §17.16.5.60), as layout shows them. Its stored result stays in the -/// flow (ODT writes it back); text exports write nothing for it. -pub fn shows_nothing(instr: &str) -> bool { - let name = instr.split_whitespace().next().unwrap_or(""); - name.eq_ignore_ascii_case("ASK") || name.eq_ignore_ascii_case("SET") -} - -/// What [`super::from_doc_with`] keeps besides text and formatting. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct FlowOpts { - /// Tracked changes: deleted text stays, and text carries its insertion and deletion marks. - pub tracked: bool, - /// Fields as [`Inline::Field`] (otherwise their result is text). - pub fields: bool, - /// Footnotes and endnotes as [`Inline::Note`] (otherwise left out). - pub notes: bool, - /// Comments as [`Inline::Comment`] and [`Inline::CommentEnd`] (otherwise left out). - pub comments: bool, - /// Text boxes and drawing shapes as [`Inline::Shape`] (otherwise left out). - pub boxes: bool, - /// Character formatting beyond the [`Fmt`] fields ([`Fmt::extra`]), hidden text kept (as - /// hidden), and a monospaced font by its name rather than as code. - pub chars: bool, - /// Paragraph formatting beyond the [`Para`] fields ([`Para::extra`]). - pub paras: bool, - /// Lists with their identity and labels ([`ListInfo::list`], [`ListInfo::label`]; otherwise - /// consecutive items form one list in the default look of its kind). - pub lists: bool, -} - -/// Base text size (points) a phonetic guide falls back to when the source states none: the -/// Phonetic Guide command's own fallback for an unresolved size -/// (`crates/engine/src/cmd/phonetic.rs`). -pub const RUBY_BASE_SIZE: f32 = 10.5; - -/// Ruby text size for a base of `base_size` points: half the base size, rounded down to a half -/// point, as the Phonetic Guide command sets it. -fn ruby_default_size(base_size: f32) -> f32 { - (base_size.floor() / 2.0).max(1.0) -} - -/// The ruby a format without its own ruby markup gets: a [`InlineObject::Ruby`] over `base` with -/// the Phonetic Guide's defaults (centred, half the base size, raised clear of the base text). -pub fn ruby_object(base: String, ruby: String, base_size: f32) -> InlineObject { - let base_size = if base_size.is_finite() { base_size.clamp(1.0, 1584.0) } else { RUBY_BASE_SIZE }; - let size = ruby_default_size(base_size); - let raise = ruby_raise(0.0, base_size, size).clamp(0.0, 1584.0); - InlineObject::Ruby { base, ruby, align: RubyAlign::default(), size, raise, base_size, lang: String::new(), props: Default::default() } -} - -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub enum Kind { - #[default] - Normal, - /// 1..=6. - Heading(u8), - Title, - Quote, - /// One line of a code block. - Code, - /// A horizontal rule (empty paragraph with a bottom border). - Rule, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct ListInfo { - pub ordered: bool, - /// 0-based nesting level (0..=8). - pub level: u8, - /// Number of the first item of an ordered list (1 unless the source says otherwise). - pub start: u32, - /// The list the paragraph belongs to, when the source tells its lists apart (ODT): items - /// of one list share its number, wherever they are, and a different number starts another - /// list. 0: not stated (consecutive items form one list). - pub list: u32, - /// How the paragraph's level shows its number or bullet, when the source says. - pub label: Option