docvortex 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docvortex/__init__.py +21 -0
- docvortex/analyzers/__init__.py +3 -0
- docvortex/analyzers/native/__init__.py +37 -0
- docvortex/analyzers/native/_shared/__init__.py +3 -0
- docvortex/analyzers/native/_shared/hyperlink.py +16 -0
- docvortex/analyzers/native/_shared/image.py +8 -0
- docvortex/analyzers/native/_shared/markup/__init__.py +47 -0
- docvortex/analyzers/native/_shared/markup/anchors.py +30 -0
- docvortex/analyzers/native/_shared/markup/formula.py +37 -0
- docvortex/analyzers/native/_shared/markup/projector.py +57 -0
- docvortex/analyzers/native/_shared/markup/styles.py +19 -0
- docvortex/analyzers/native/_shared/mathml.py +17 -0
- docvortex/analyzers/native/_shared/names.py +7 -0
- docvortex/analyzers/native/_shared/xycut.py +414 -0
- docvortex/analyzers/native/contracts.py +44 -0
- docvortex/analyzers/native/csv.py +351 -0
- docvortex/analyzers/native/epub/__init__.py +18 -0
- docvortex/analyzers/native/epub/constants.py +45 -0
- docvortex/analyzers/native/epub/converter.py +80 -0
- docvortex/analyzers/native/epub/errors.py +20 -0
- docvortex/analyzers/native/epub/metadata.py +31 -0
- docvortex/analyzers/native/epub/package.py +649 -0
- docvortex/analyzers/native/epub/xhtml.py +306 -0
- docvortex/analyzers/native/html/__init__.py +6 -0
- docvortex/analyzers/native/html/anchors.py +271 -0
- docvortex/analyzers/native/html/constants.py +25 -0
- docvortex/analyzers/native/html/contracts.py +7 -0
- docvortex/analyzers/native/html/converter.py +117 -0
- docvortex/analyzers/native/html/document.py +389 -0
- docvortex/analyzers/native/html/errors.py +12 -0
- docvortex/analyzers/native/html/resources.py +365 -0
- docvortex/analyzers/native/html/selector.py +421 -0
- docvortex/analyzers/native/models.py +209 -0
- docvortex/analyzers/native/ofd/__init__.py +14 -0
- docvortex/analyzers/native/ofd/constants.py +59 -0
- docvortex/analyzers/native/ofd/converter.py +31 -0
- docvortex/analyzers/native/ofd/errors.py +16 -0
- docvortex/analyzers/native/ofd/geometry.py +215 -0
- docvortex/analyzers/native/ofd/images.py +124 -0
- docvortex/analyzers/native/ofd/metadata.py +53 -0
- docvortex/analyzers/native/ofd/models.py +170 -0
- docvortex/analyzers/native/ofd/package.py +346 -0
- docvortex/analyzers/native/ofd/path.py +201 -0
- docvortex/analyzers/native/ofd/reading_order.py +293 -0
- docvortex/analyzers/native/ofd/resources.py +122 -0
- docvortex/analyzers/native/ofd/scene.py +347 -0
- docvortex/analyzers/native/ofd/table.py +240 -0
- docvortex/analyzers/native/ofd/text.py +521 -0
- docvortex/analyzers/native/office/__init__.py +3 -0
- docvortex/analyzers/native/office/doc/__init__.py +3 -0
- docvortex/analyzers/native/office/doc/bookmarks.py +81 -0
- docvortex/analyzers/native/office/doc/doc_converter.py +573 -0
- docvortex/analyzers/native/office/doc/fib.py +261 -0
- docvortex/analyzers/native/office/doc/fields.py +101 -0
- docvortex/analyzers/native/office/doc/formatting.py +171 -0
- docvortex/analyzers/native/office/doc/images.py +162 -0
- docvortex/analyzers/native/office/doc/lists.py +356 -0
- docvortex/analyzers/native/office/doc/models.py +164 -0
- docvortex/analyzers/native/office/doc/parser.py +851 -0
- docvortex/analyzers/native/office/doc/pieces.py +256 -0
- docvortex/analyzers/native/office/doc/records.py +40 -0
- docvortex/analyzers/native/office/doc/sprm.py +269 -0
- docvortex/analyzers/native/office/doc/styles.py +215 -0
- docvortex/analyzers/native/office/docx/__init__.py +3 -0
- docvortex/analyzers/native/office/docx/context.py +71 -0
- docvortex/analyzers/native/office/docx/docx_converter.py +614 -0
- docvortex/analyzers/native/office/docx/equationxml.py +119 -0
- docvortex/analyzers/native/office/docx/fields.py +816 -0
- docvortex/analyzers/native/office/docx/formatting_types.py +23 -0
- docvortex/analyzers/native/office/docx/main.py +50 -0
- docvortex/analyzers/native/office/docx/numbering.py +491 -0
- docvortex/analyzers/native/office/docx/office_xml.py +57 -0
- docvortex/analyzers/native/office/docx/package_normalizer.py +248 -0
- docvortex/analyzers/native/office/docx/resources.py +526 -0
- docvortex/analyzers/native/office/docx/styles.py +693 -0
- docvortex/analyzers/native/office/docx/tables.py +515 -0
- docvortex/analyzers/native/office/equation/__init__.py +3 -0
- docvortex/analyzers/native/office/equation/image.py +470 -0
- docvortex/analyzers/native/office/equation/latex_dict.py +324 -0
- docvortex/analyzers/native/office/equation/mtef.py +885 -0
- docvortex/analyzers/native/office/equation/mtef_v5.py +941 -0
- docvortex/analyzers/native/office/equation/omml.py +561 -0
- docvortex/analyzers/native/office/equation/ooxml.py +62 -0
- docvortex/analyzers/native/office/errors.py +33 -0
- docvortex/analyzers/native/office/image.py +307 -0
- docvortex/analyzers/native/office/legacy/__init__.py +3 -0
- docvortex/analyzers/native/office/legacy/binary.py +43 -0
- docvortex/analyzers/native/office/legacy/officeart.py +362 -0
- docvortex/analyzers/native/office/legacy/ole.py +110 -0
- docvortex/analyzers/native/office/limits.py +12 -0
- docvortex/analyzers/native/office/odf/__init__.py +3 -0
- docvortex/analyzers/native/office/odf/chart.py +76 -0
- docvortex/analyzers/native/office/odf/constants.py +70 -0
- docvortex/analyzers/native/office/odf/converters.py +403 -0
- docvortex/analyzers/native/office/odf/errors.py +18 -0
- docvortex/analyzers/native/office/odf/metadata.py +91 -0
- docvortex/analyzers/native/office/odf/models.py +176 -0
- docvortex/analyzers/native/office/odf/package.py +270 -0
- docvortex/analyzers/native/office/odf/styles.py +329 -0
- docvortex/analyzers/native/office/odf/table.py +469 -0
- docvortex/analyzers/native/office/odf/text.py +1002 -0
- docvortex/analyzers/native/office/ooxml_chart.py +1016 -0
- docvortex/analyzers/native/office/opc.py +38 -0
- docvortex/analyzers/native/office/ppt/__init__.py +3 -0
- docvortex/analyzers/native/office/ppt/models.py +118 -0
- docvortex/analyzers/native/office/ppt/parser.py +1895 -0
- docvortex/analyzers/native/office/ppt/ppt_converter.py +292 -0
- docvortex/analyzers/native/office/ppt/records.py +131 -0
- docvortex/analyzers/native/office/ppt/style_text.py +247 -0
- docvortex/analyzers/native/office/pptx/__init__.py +3 -0
- docvortex/analyzers/native/office/pptx/context.py +113 -0
- docvortex/analyzers/native/office/pptx/lists.py +558 -0
- docvortex/analyzers/native/office/pptx/main.py +20 -0
- docvortex/analyzers/native/office/pptx/package_normalizer.py +321 -0
- docvortex/analyzers/native/office/pptx/pptx_converter.py +323 -0
- docvortex/analyzers/native/office/pptx/resources.py +329 -0
- docvortex/analyzers/native/office/pptx/shapes.py +393 -0
- docvortex/analyzers/native/office/pptx/text_styles.py +625 -0
- docvortex/analyzers/native/office/pptx/titles.py +178 -0
- docvortex/analyzers/native/office/rich_text.py +420 -0
- docvortex/analyzers/native/office/rtf/__init__.py +3 -0
- docvortex/analyzers/native/office/rtf/converter.py +708 -0
- docvortex/analyzers/native/office/rtf/lexer.py +213 -0
- docvortex/analyzers/native/office/rtf/math.py +339 -0
- docvortex/analyzers/native/office/rtf/models.py +185 -0
- docvortex/analyzers/native/office/rtf/parser.py +1553 -0
- docvortex/analyzers/native/office/spreadsheet/__init__.py +3 -0
- docvortex/analyzers/native/office/spreadsheet/html.py +81 -0
- docvortex/analyzers/native/office/spreadsheet/models.py +79 -0
- docvortex/analyzers/native/office/spreadsheet/projector.py +928 -0
- docvortex/analyzers/native/office/streams.py +18 -0
- docvortex/analyzers/native/office/xls/__init__.py +3 -0
- docvortex/analyzers/native/office/xls/chart.py +132 -0
- docvortex/analyzers/native/office/xls/embedded_chart.py +299 -0
- docvortex/analyzers/native/office/xls/models.py +109 -0
- docvortex/analyzers/native/office/xls/number_format.py +521 -0
- docvortex/analyzers/native/office/xls/parser.py +1145 -0
- docvortex/analyzers/native/office/xls/records.py +201 -0
- docvortex/analyzers/native/office/xls/strings.py +205 -0
- docvortex/analyzers/native/office/xls/xls_converter.py +354 -0
- docvortex/analyzers/native/office/xlsx/__init__.py +3 -0
- docvortex/analyzers/native/office/xlsx/main.py +20 -0
- docvortex/analyzers/native/office/xlsx/ooxml_ole.py +522 -0
- docvortex/analyzers/native/office/xlsx/package_normalizer.py +310 -0
- docvortex/analyzers/native/office/xlsx/xlsx_converter.py +716 -0
- docvortex/analyzers/native/pdf/__init__.py +3 -0
- docvortex/analyzers/native/pdf/auxiliary_text.py +1671 -0
- docvortex/analyzers/native/pdf/char_geometry.py +1662 -0
- docvortex/analyzers/native/pdf/code_blocks.py +535 -0
- docvortex/analyzers/native/pdf/formulas.py +1985 -0
- docvortex/analyzers/native/pdf/geometry.py +281 -0
- docvortex/analyzers/native/pdf/graphics.py +1501 -0
- docvortex/analyzers/native/pdf/index_blocks.py +268 -0
- docvortex/analyzers/native/pdf/inline/__init__.py +3 -0
- docvortex/analyzers/native/pdf/inline/common.py +112 -0
- docvortex/analyzers/native/pdf/inline/detection.py +590 -0
- docvortex/analyzers/native/pdf/inline/matching.py +1185 -0
- docvortex/analyzers/native/pdf/inline/materialize.py +457 -0
- docvortex/analyzers/native/pdf/inline/scripts.py +975 -0
- docvortex/analyzers/native/pdf/inline/types.py +385 -0
- docvortex/analyzers/native/pdf/line_layout.py +1106 -0
- docvortex/analyzers/native/pdf/line_merging.py +1223 -0
- docvortex/analyzers/native/pdf/models.py +246 -0
- docvortex/analyzers/native/pdf/native_text.py +1004 -0
- docvortex/analyzers/native/pdf/pipeline.py +1390 -0
- docvortex/analyzers/native/pdf/script_geometry.py +636 -0
- docvortex/analyzers/native/pdf/shared.py +30 -0
- docvortex/analyzers/native/pdf/spatial_text.py +383 -0
- docvortex/analyzers/native/pdf/table_annotations.py +446 -0
- docvortex/analyzers/native/pdf/table_constants.py +48 -0
- docvortex/analyzers/native/pdf/table_detection.py +227 -0
- docvortex/analyzers/native/pdf/table_filled_grid.py +218 -0
- docvortex/analyzers/native/pdf/table_geometry.py +40 -0
- docvortex/analyzers/native/pdf/table_materialization.py +444 -0
- docvortex/analyzers/native/pdf/table_recovery/__init__.py +15 -0
- docvortex/analyzers/native/pdf/table_recovery/candidate.py +356 -0
- docvortex/analyzers/native/pdf/table_recovery/contracts.py +154 -0
- docvortex/analyzers/native/pdf/table_recovery/engine.py +609 -0
- docvortex/analyzers/native/pdf/table_recovery/geometry.py +164 -0
- docvortex/analyzers/native/pdf/table_recovery/sparse_common.py +85 -0
- docvortex/analyzers/native/pdf/table_recovery/sparse_hybrid.py +804 -0
- docvortex/analyzers/native/pdf/table_recovery/sparse_multiline.py +1122 -0
- docvortex/analyzers/native/pdf/table_recovery/text.py +414 -0
- docvortex/analyzers/native/pdf/table_recovery/text_grid.py +618 -0
- docvortex/analyzers/native/pdf/table_recovery/vector.py +1931 -0
- docvortex/analyzers/native/pdf/table_rows.py +34 -0
- docvortex/analyzers/native/pdf/table_rules.py +1129 -0
- docvortex/analyzers/native/pdf/table_text_styles.py +283 -0
- docvortex/analyzers/native/pdf/tables.py +147 -0
- docvortex/analyzers/native/pdf/text_assembly/__init__.py +3 -0
- docvortex/analyzers/native/pdf/text_assembly/annotations.py +581 -0
- docvortex/analyzers/native/pdf/text_assembly/assembly.py +292 -0
- docvortex/analyzers/native/pdf/text_assembly/common.py +477 -0
- docvortex/analyzers/native/pdf/text_assembly/footnotes.py +394 -0
- docvortex/analyzers/native/pdf/text_assembly/merging.py +1274 -0
- docvortex/analyzers/native/pdf/text_assembly/rows.py +692 -0
- docvortex/analyzers/native/pdf/text_blocks.py +82 -0
- docvortex/analyzers/native/pdf/text_styles.py +55 -0
- docvortex/analyzers/native/pdf/title_analysis/__init__.py +3 -0
- docvortex/analyzers/native/pdf/title_analysis/body_profile.py +215 -0
- docvortex/analyzers/native/pdf/title_analysis/common.py +117 -0
- docvortex/analyzers/native/pdf/title_analysis/document_profile.py +164 -0
- docvortex/analyzers/native/pdf/title_analysis/lane_titles.py +758 -0
- docvortex/analyzers/native/pdf/title_analysis/page_titles.py +1024 -0
- docvortex/analyzers/native/pdf/title_analysis/prototype.py +194 -0
- docvortex/analyzers/native/pdf/title_analysis/structural.py +1081 -0
- docvortex/analyzers/native/pdf/titles.py +75 -0
- docvortex/analyzers/native/pdf/typography.py +19 -0
- docvortex/analyzers/native/pdf/visual_annotations.py +1262 -0
- docvortex/api.py +180 -0
- docvortex/assets/__init__.py +5 -0
- docvortex/assets/store.py +51 -0
- docvortex/cli.py +54 -0
- docvortex/codecs/__init__.py +3 -0
- docvortex/codecs/html/__init__.py +22 -0
- docvortex/codecs/html/contracts.py +236 -0
- docvortex/codecs/html/materializer.py +331 -0
- docvortex/codecs/html/parser.py +763 -0
- docvortex/codecs/html/resources.py +34 -0
- docvortex/codecs/json.py +17 -0
- docvortex/content/__init__.py +5 -0
- docvortex/content/inline.py +248 -0
- docvortex/content/markup/__init__.py +44 -0
- docvortex/content/markup/anchors.py +188 -0
- docvortex/content/markup/formula.py +280 -0
- docvortex/content/markup/projector.py +1237 -0
- docvortex/content/markup/styles.py +327 -0
- docvortex/content/mathml.py +167 -0
- docvortex/content/normalization.py +188 -0
- docvortex/content/spans.py +183 -0
- docvortex/content/table/__init__.py +18 -0
- docvortex/content/table/blocks.py +152 -0
- docvortex/content/table/content.py +425 -0
- docvortex/content/table/document.py +104 -0
- docvortex/content/table/html.py +399 -0
- docvortex/content/table/models.py +76 -0
- docvortex/content/table/rules.py +42 -0
- docvortex/content/table/structure.py +221 -0
- docvortex/content/tree.py +5 -0
- docvortex/document/__init__.py +3 -0
- docvortex/document/contracts.py +22 -0
- docvortex/document/detection.py +389 -0
- docvortex/document/filetypes.py +175 -0
- docvortex/document/page_range.py +167 -0
- docvortex/document/pdf/__init__.py +19 -0
- docvortex/document/pdf/classify.py +1138 -0
- docvortex/document/pdf/constants.py +132 -0
- docvortex/document/pdf/diagnostics.py +350 -0
- docvortex/document/pdf/document.py +582 -0
- docvortex/document/pdf/font_runtime.py +335 -0
- docvortex/document/pdf/geometry.py +31 -0
- docvortex/document/pdf/images.py +654 -0
- docvortex/document/pdf/native_annotations.py +367 -0
- docvortex/document/pdf/native_contracts.py +169 -0
- docvortex/document/pdf/native_coordinates.py +216 -0
- docvortex/document/pdf/native_lifecycle.py +16 -0
- docvortex/document/pdf/native_objects.py +902 -0
- docvortex/document/pdf/native_text_geometry.py +315 -0
- docvortex/document/pdf/pdfium.py +325 -0
- docvortex/document/pdf/raster.py +46 -0
- docvortex/document/pdf/text/__init__.py +62 -0
- docvortex/document/pdf/text/contracts.py +211 -0
- docvortex/document/pdf/text/extract.py +165 -0
- docvortex/document/pdf/text/geometry.py +16 -0
- docvortex/document/pdf/text/groups.py +162 -0
- docvortex/document/pdf/visual_geometry.py +201 -0
- docvortex/document/pdf/visuals.py +343 -0
- docvortex/document/source.py +92 -0
- docvortex/errors.py +21 -0
- docvortex/export/__init__.py +3 -0
- docvortex/export/bundle.py +98 -0
- docvortex/export/files.py +66 -0
- docvortex/export/middle.py +208 -0
- docvortex/foundation/__init__.py +3 -0
- docvortex/foundation/geometry.py +125 -0
- docvortex/foundation/hyperlink.py +65 -0
- docvortex/foundation/image.py +48 -0
- docvortex/foundation/image_encoding.py +30 -0
- docvortex/foundation/image_payload.py +280 -0
- docvortex/foundation/language.py +92 -0
- docvortex/foundation/platform.py +38 -0
- docvortex/foundation/text.py +153 -0
- docvortex/foundation/type_identity.py +20 -0
- docvortex/foundation/xml_names.py +20 -0
- docvortex/options.py +30 -0
- docvortex/postprocess/__init__.py +3 -0
- docvortex/postprocess/content.py +53 -0
- docvortex/postprocess/document.py +19 -0
- docvortex/postprocess/lists.py +236 -0
- docvortex/postprocess/page_blocks.py +214 -0
- docvortex/postprocess/pages.py +95 -0
- docvortex/postprocess/paragraphs.py +580 -0
- docvortex/postprocess/visual.py +715 -0
- docvortex/render/__init__.py +48 -0
- docvortex/render/_internal/__init__.py +3 -0
- docvortex/render/_internal/common/__init__.py +3 -0
- docvortex/render/_internal/common/context.py +43 -0
- docvortex/render/_internal/common/html_table.py +178 -0
- docvortex/render/_internal/common/index.py +33 -0
- docvortex/render/_internal/common/list_items.py +158 -0
- docvortex/render/_internal/common/planner.py +140 -0
- docvortex/render/_internal/docx/__init__.py +3 -0
- docvortex/render/_internal/docx/assets.py +202 -0
- docvortex/render/_internal/docx/inline.py +434 -0
- docvortex/render/_internal/docx/math.py +220 -0
- docvortex/render/_internal/docx/renderer.py +905 -0
- docvortex/render/_internal/docx/styles.py +195 -0
- docvortex/render/_internal/docx/table.py +442 -0
- docvortex/render/_internal/epub/__init__.py +5 -0
- docvortex/render/_internal/epub/assets.py +173 -0
- docvortex/render/_internal/epub/package.py +249 -0
- docvortex/render/_internal/epub/renderer.py +1156 -0
- docvortex/render/_internal/html/__init__.py +3 -0
- docvortex/render/_internal/html/inline.py +346 -0
- docvortex/render/_internal/html/renderer.py +1041 -0
- docvortex/render/_internal/html/sanitizer.py +478 -0
- docvortex/render/_internal/html/table.py +121 -0
- docvortex/render/_internal/latex/__init__.py +1 -0
- docvortex/render/_internal/latex/assets.py +85 -0
- docvortex/render/_internal/latex/inline.py +145 -0
- docvortex/render/_internal/latex/renderer.py +506 -0
- docvortex/render/_internal/latex/table.py +347 -0
- docvortex/render/_internal/markdown/__init__.py +3 -0
- docvortex/render/_internal/markdown/assets.py +78 -0
- docvortex/render/_internal/markdown/blocks.py +635 -0
- docvortex/render/_internal/markdown/escaping.py +51 -0
- docvortex/render/_internal/markdown/inline.py +260 -0
- docvortex/render/_internal/markdown/renderer.py +93 -0
- docvortex/render/_internal/markdown/table.py +281 -0
- docvortex/render/_internal/pdf/__init__.py +3 -0
- docvortex/render/_internal/pdf/assets.py +197 -0
- docvortex/render/_internal/pdf/formula.py +417 -0
- docvortex/render/_internal/pdf/inline.py +343 -0
- docvortex/render/_internal/pdf/renderer.py +734 -0
- docvortex/render/_internal/pdf/styles.py +206 -0
- docvortex/render/_internal/pdf/table.py +272 -0
- docvortex/render/_internal/structured_content/__init__.py +3 -0
- docvortex/render/_internal/structured_content/renderer.py +193 -0
- docvortex/render/api.py +199 -0
- docvortex/render/contracts.py +205 -0
- docvortex/render/docx.py +38 -0
- docvortex/render/epub.py +35 -0
- docvortex/render/fragments.py +70 -0
- docvortex/render/html.py +29 -0
- docvortex/render/latex.py +24 -0
- docvortex/render/markdown.py +50 -0
- docvortex/render/pdf.py +25 -0
- docvortex/render/structured_content.py +23 -0
- docvortex/resources/epub/docvortex.css +91 -0
- docvortex/resources/fasttext-langdetect/lid.176.ftz +0 -0
- docvortex/resources/fonts/DroidSansFallbackFull.ttf +0 -0
- docvortex/resources/fonts/NOTICE +190 -0
- docvortex/resources/fonts/manifest.json +9 -0
- docvortex/resources/html/docvortex.css +601 -0
- docvortex/resources/html/docvortex.min.css +1 -0
- docvortex/result.py +91 -0
- docvortex/schema.py +1141 -0
- docvortex/version.py +3 -0
- docvortex-0.2.1.dist-info/METADATA +193 -0
- docvortex-0.2.1.dist-info/RECORD +364 -0
- docvortex-0.2.1.dist-info/WHEEL +5 -0
- docvortex-0.2.1.dist-info/entry_points.txt +2 -0
- docvortex-0.2.1.dist-info/licenses/LICENSE.md +21 -0
- docvortex-0.2.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
"""Middle JSON InlineSpan 到 ReportLab Paragraph fragment 的转换。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from hashlib import sha1
|
|
7
|
+
import html
|
|
8
|
+
import re
|
|
9
|
+
from typing import Iterable
|
|
10
|
+
|
|
11
|
+
from loguru import logger
|
|
12
|
+
from reportlab.lib.abag import ABag
|
|
13
|
+
from reportlab.lib.styles import ParagraphStyle
|
|
14
|
+
from reportlab.platypus import Paragraph
|
|
15
|
+
from reportlab.platypus.paraparser import ParaParser
|
|
16
|
+
|
|
17
|
+
from ....schema import CodeInlineSpan, EquationInlineSpan, HyperlinkSpan, InlineSpan, TextSpan
|
|
18
|
+
from .formula import FormulaRenderer, InlineFormulaImage, PdfFormulaError
|
|
19
|
+
from .styles import ACCENT_COLOR, HAN_FONT, JAPANESE_FONT, KOREAN_FONT, MONO_FONT, UNICODE_FALLBACK_FONT
|
|
20
|
+
|
|
21
|
+
_BOOKMARK_SAFE_RE = re.compile(r"[^A-Za-z0-9_]+")
|
|
22
|
+
_BOOKMARK_MAX_LENGTH = 80
|
|
23
|
+
_FORMULA_SOURCE_PREFIX = "docvortex-formula:"
|
|
24
|
+
_INVALID_TEXT_RE = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f\ud800-\udfff\ufffe\uffff]")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PdfAnchorRegistry:
|
|
28
|
+
"""把 MiddleJson anchor 映射为稳定唯一的 PDF destination 名称。"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, anchors: Iterable[str]) -> None:
|
|
31
|
+
"""预注册全部可见标题与页面脚注 anchor。"""
|
|
32
|
+
self._names: dict[str, str] = {}
|
|
33
|
+
self._attached: set[str] = set()
|
|
34
|
+
self._used_names: set[str] = set()
|
|
35
|
+
for anchor in anchors:
|
|
36
|
+
normalized = anchor.strip()
|
|
37
|
+
if normalized and normalized not in self._names:
|
|
38
|
+
self._names[normalized] = self._allocate_name(normalized)
|
|
39
|
+
|
|
40
|
+
def resolve(self, anchor: str | None) -> str | None:
|
|
41
|
+
"""解析已注册 anchor,空值或未知值返回 None。"""
|
|
42
|
+
normalized = (anchor or "").strip()
|
|
43
|
+
return self._names.get(normalized) if normalized else None
|
|
44
|
+
|
|
45
|
+
def attach_markup(self, anchor: str | None) -> str:
|
|
46
|
+
"""返回首个目标的 Paragraph anchor 标签,重复目标仅记录告警。"""
|
|
47
|
+
normalized = (anchor or "").strip()
|
|
48
|
+
name = self.resolve(normalized)
|
|
49
|
+
if name is None:
|
|
50
|
+
return ""
|
|
51
|
+
if normalized in self._attached:
|
|
52
|
+
logger.warning("Duplicate PDF anchor ignored: {}", normalized)
|
|
53
|
+
return ""
|
|
54
|
+
self._attached.add(normalized)
|
|
55
|
+
return f'<a name="{html.escape(name, quote=True)}"/>'
|
|
56
|
+
|
|
57
|
+
def _allocate_name(self, anchor: str) -> str:
|
|
58
|
+
"""为原始 anchor 分配合法 ASCII destination,并稳定处理碰撞。"""
|
|
59
|
+
base = _BOOKMARK_SAFE_RE.sub("_", anchor).strip("_")
|
|
60
|
+
if not base or not base[0].isalpha():
|
|
61
|
+
base = f"b_{base}"
|
|
62
|
+
candidate = base[:_BOOKMARK_MAX_LENGTH]
|
|
63
|
+
if candidate not in self._used_names:
|
|
64
|
+
self._used_names.add(candidate)
|
|
65
|
+
return candidate
|
|
66
|
+
digest = sha1(anchor.encode("utf-8")).hexdigest()[:10]
|
|
67
|
+
prefix_length = _BOOKMARK_MAX_LENGTH - len(digest) - 1
|
|
68
|
+
candidate = f"{base[:prefix_length]}_{digest}"
|
|
69
|
+
ordinal = 1
|
|
70
|
+
while candidate in self._used_names:
|
|
71
|
+
suffix = f"_{ordinal}"
|
|
72
|
+
candidate = f"{candidate[: _BOOKMARK_MAX_LENGTH - len(suffix)]}{suffix}"
|
|
73
|
+
ordinal += 1
|
|
74
|
+
self._used_names.add(candidate)
|
|
75
|
+
return candidate
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(slots=True)
|
|
79
|
+
class PdfInlineContext:
|
|
80
|
+
"""保存一份 PDF 文档共享的公式、anchor 与定位状态。"""
|
|
81
|
+
|
|
82
|
+
formulas: FormulaRenderer
|
|
83
|
+
anchors: PdfAnchorRegistry
|
|
84
|
+
formula_images: dict[str, InlineFormulaImage] = field(default_factory=dict)
|
|
85
|
+
next_formula_id: int = 0
|
|
86
|
+
|
|
87
|
+
def location(self, page_idx: int, block_index: int | None, block_type: str) -> str:
|
|
88
|
+
"""返回稳定的 page/block 告警定位文本。"""
|
|
89
|
+
return f"page_idx={page_idx}, block_index={block_index}, block_type={block_type}"
|
|
90
|
+
|
|
91
|
+
def register_formula(self, image: InlineFormulaImage) -> str:
|
|
92
|
+
"""注册一个 Paragraph 可引用的行内公式代理并返回内部 token。"""
|
|
93
|
+
token = f"{_FORMULA_SOURCE_PREFIX}{self.next_formula_id}"
|
|
94
|
+
self.next_formula_id += 1
|
|
95
|
+
self.formula_images[token] = image
|
|
96
|
+
return token
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class _PdfParaParser(ParaParser):
|
|
100
|
+
"""让 ReportLab Paragraph 接受不经 ImageReader 的行内公式 token。"""
|
|
101
|
+
|
|
102
|
+
def __init__(self, formula_images: dict[str, InlineFormulaImage]) -> None:
|
|
103
|
+
"""保存当前文档的公式代理映射。"""
|
|
104
|
+
super().__init__()
|
|
105
|
+
self._formula_images = formula_images
|
|
106
|
+
|
|
107
|
+
def end_img(self) -> None:
|
|
108
|
+
"""把 docvortex-formula token 解析为带基线几何的矢量图片 fragment。"""
|
|
109
|
+
frag = self._stack[-1]
|
|
110
|
+
if not getattr(frag, "_selfClosingTag", ""):
|
|
111
|
+
raise ValueError("Parser failure in <img/>")
|
|
112
|
+
source = str(getattr(frag, "src", ""))
|
|
113
|
+
image = self._formula_images.get(source)
|
|
114
|
+
if image is None:
|
|
115
|
+
raise ValueError(f"Unknown PDF inline formula token: {source}")
|
|
116
|
+
vector = image.vector
|
|
117
|
+
definition = frag.cbDefn = ABag()
|
|
118
|
+
definition.kind = "img"
|
|
119
|
+
definition.src = source
|
|
120
|
+
definition.image = image
|
|
121
|
+
definition.width = vector.width
|
|
122
|
+
definition.height = vector.height
|
|
123
|
+
definition.valign = -vector.descent
|
|
124
|
+
del frag._selfClosingTag
|
|
125
|
+
self.handle_data("")
|
|
126
|
+
self._pop("img")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def build_pdf_paragraph(
|
|
130
|
+
spans: list[InlineSpan],
|
|
131
|
+
style: ParagraphStyle,
|
|
132
|
+
*,
|
|
133
|
+
context: PdfInlineContext,
|
|
134
|
+
page_idx: int,
|
|
135
|
+
block_index: int | None,
|
|
136
|
+
block_type: str,
|
|
137
|
+
max_width: float,
|
|
138
|
+
anchor: str | None = None,
|
|
139
|
+
preserve_newlines: bool = False,
|
|
140
|
+
) -> Paragraph:
|
|
141
|
+
"""把完整 InlineSpan 列表构造成支持矢量公式与链接的 Paragraph。"""
|
|
142
|
+
markup = context.anchors.attach_markup(anchor)
|
|
143
|
+
markup += render_pdf_inline_markup(
|
|
144
|
+
spans,
|
|
145
|
+
context=context,
|
|
146
|
+
page_idx=page_idx,
|
|
147
|
+
block_index=block_index,
|
|
148
|
+
block_type=block_type,
|
|
149
|
+
font_size=style.fontSize,
|
|
150
|
+
max_width=max_width,
|
|
151
|
+
preserve_newlines=preserve_newlines,
|
|
152
|
+
)
|
|
153
|
+
parser = _PdfParaParser(context.formula_images)
|
|
154
|
+
parsed_style, fragments, bullet_fragments = parser.parse(markup or "​", style)
|
|
155
|
+
bullet_text = None
|
|
156
|
+
if bullet_fragments:
|
|
157
|
+
bullet_text = "".join(getattr(fragment, "text", "") for fragment in bullet_fragments)
|
|
158
|
+
return Paragraph("", parsed_style, bulletText=bullet_text, frags=fragments)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def render_pdf_inline_markup(
|
|
162
|
+
spans: list[InlineSpan],
|
|
163
|
+
*,
|
|
164
|
+
context: PdfInlineContext,
|
|
165
|
+
page_idx: int,
|
|
166
|
+
block_index: int | None,
|
|
167
|
+
block_type: str,
|
|
168
|
+
font_size: float,
|
|
169
|
+
max_width: float,
|
|
170
|
+
preserve_newlines: bool = False,
|
|
171
|
+
) -> str:
|
|
172
|
+
"""把行内 Span 转成仅包含 renderer 生成标签的安全 Paragraph markup。"""
|
|
173
|
+
return "".join(
|
|
174
|
+
_render_span(
|
|
175
|
+
span,
|
|
176
|
+
context=context,
|
|
177
|
+
page_idx=page_idx,
|
|
178
|
+
block_index=block_index,
|
|
179
|
+
block_type=block_type,
|
|
180
|
+
font_size=font_size,
|
|
181
|
+
max_width=max_width,
|
|
182
|
+
preserve_newlines=preserve_newlines,
|
|
183
|
+
)
|
|
184
|
+
for span in spans
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def render_plain_text_markup(text: str, *, preserve_newlines: bool = False) -> str:
|
|
189
|
+
"""转义普通文本,并按字符脚本显式选择 Latin 或中日韩字体。"""
|
|
190
|
+
normalized = _INVALID_TEXT_RE.sub("\ufffd", text)
|
|
191
|
+
if not normalized:
|
|
192
|
+
return ""
|
|
193
|
+
segments: list[str] = []
|
|
194
|
+
current_font: str | None = None
|
|
195
|
+
current_text: list[str] = []
|
|
196
|
+
|
|
197
|
+
def flush() -> None:
|
|
198
|
+
"""把当前同字体字符片段编码为 Paragraph markup。"""
|
|
199
|
+
if not current_text:
|
|
200
|
+
return
|
|
201
|
+
escaped = html.escape("".join(current_text), quote=False)
|
|
202
|
+
if preserve_newlines:
|
|
203
|
+
escaped = escaped.replace("\n", "<br/>")
|
|
204
|
+
else:
|
|
205
|
+
escaped = escaped.replace("\n", " ")
|
|
206
|
+
if current_font is None:
|
|
207
|
+
segments.append(escaped)
|
|
208
|
+
else:
|
|
209
|
+
segments.append(f'<font name="{current_font}">{escaped}</font>')
|
|
210
|
+
current_text.clear()
|
|
211
|
+
|
|
212
|
+
for character in normalized:
|
|
213
|
+
font = _font_for_character(character)
|
|
214
|
+
if font != current_font:
|
|
215
|
+
flush()
|
|
216
|
+
current_font = font
|
|
217
|
+
current_text.append(character)
|
|
218
|
+
flush()
|
|
219
|
+
return "".join(segments)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _render_span(
|
|
223
|
+
span: InlineSpan,
|
|
224
|
+
*,
|
|
225
|
+
context: PdfInlineContext,
|
|
226
|
+
page_idx: int,
|
|
227
|
+
block_index: int | None,
|
|
228
|
+
block_type: str,
|
|
229
|
+
font_size: float,
|
|
230
|
+
max_width: float,
|
|
231
|
+
preserve_newlines: bool,
|
|
232
|
+
) -> str:
|
|
233
|
+
"""按 InlineSpan discriminator 输出单个安全 Paragraph 片段。"""
|
|
234
|
+
if isinstance(span, TextSpan):
|
|
235
|
+
markup = render_plain_text_markup(span.content, preserve_newlines=preserve_newlines)
|
|
236
|
+
return _apply_text_styles(markup, tuple(span.styles))
|
|
237
|
+
if isinstance(span, CodeInlineSpan):
|
|
238
|
+
markup = render_plain_text_markup(span.content, preserve_newlines=preserve_newlines)
|
|
239
|
+
return f'<font name="{MONO_FONT}" size="{max(6.0, font_size * 0.9):.3f}">{markup}</font>'
|
|
240
|
+
if isinstance(span, EquationInlineSpan):
|
|
241
|
+
try:
|
|
242
|
+
vector = context.formulas.render(span.content, inline=True, font_size=font_size)
|
|
243
|
+
if vector.width > max_width > 0:
|
|
244
|
+
vector = vector.scaled(max_width / vector.width)
|
|
245
|
+
token = context.register_formula(InlineFormulaImage(vector))
|
|
246
|
+
return (
|
|
247
|
+
f'<img src="{token}" width="{vector.width:.3f}" height="{vector.height:.3f}" valign="{-vector.descent:.3f}"/>'
|
|
248
|
+
)
|
|
249
|
+
except PdfFormulaError as exc:
|
|
250
|
+
logger.warning(
|
|
251
|
+
"PDF inline formula fallback: {} ({})",
|
|
252
|
+
exc,
|
|
253
|
+
context.location(page_idx, block_index, block_type),
|
|
254
|
+
)
|
|
255
|
+
fallback = render_plain_text_markup(f"${span.content}$", preserve_newlines=preserve_newlines)
|
|
256
|
+
return f'<font name="{MONO_FONT}" color="#6b7280">{fallback}</font>'
|
|
257
|
+
if isinstance(span, HyperlinkSpan):
|
|
258
|
+
child_markup = "".join(
|
|
259
|
+
_render_span(
|
|
260
|
+
child,
|
|
261
|
+
context=context,
|
|
262
|
+
page_idx=page_idx,
|
|
263
|
+
block_index=block_index,
|
|
264
|
+
block_type=block_type,
|
|
265
|
+
font_size=font_size,
|
|
266
|
+
max_width=max_width,
|
|
267
|
+
preserve_newlines=preserve_newlines,
|
|
268
|
+
)
|
|
269
|
+
for child in span.content
|
|
270
|
+
)
|
|
271
|
+
target = _resolve_link_target(span.url, context.anchors)
|
|
272
|
+
if target is None:
|
|
273
|
+
if span.url.startswith("#"):
|
|
274
|
+
logger.warning(
|
|
275
|
+
"Unmatched PDF internal link: {} ({})",
|
|
276
|
+
span.url,
|
|
277
|
+
context.location(page_idx, block_index, block_type),
|
|
278
|
+
)
|
|
279
|
+
return child_markup
|
|
280
|
+
return f'<a href="{html.escape(target, quote=True)}" color="{ACCENT_COLOR.hexval()}" underline="1">{child_markup}</a>'
|
|
281
|
+
raise TypeError(f"Unsupported InlineSpan type: {type(span).__name__}")
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def _apply_text_styles(markup: str, styles: tuple[str, ...]) -> str:
|
|
285
|
+
"""按固定顺序把 MiddleJson 文本样式映射到 Paragraph 标签。"""
|
|
286
|
+
if not markup:
|
|
287
|
+
return ""
|
|
288
|
+
tags: list[str] = []
|
|
289
|
+
if "bold" in styles:
|
|
290
|
+
tags.append("b")
|
|
291
|
+
if "italic" in styles:
|
|
292
|
+
tags.append("i")
|
|
293
|
+
if "underline" in styles or "emphasis" in styles:
|
|
294
|
+
tags.append("u")
|
|
295
|
+
if "strikethrough" in styles:
|
|
296
|
+
tags.append("strike")
|
|
297
|
+
if "superscript" in styles:
|
|
298
|
+
tags.append("super")
|
|
299
|
+
elif "subscript" in styles:
|
|
300
|
+
tags.append("sub")
|
|
301
|
+
for tag in tags:
|
|
302
|
+
markup = f"<{tag}>{markup}</{tag}>"
|
|
303
|
+
return markup
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def _resolve_link_target(url: str, anchors: PdfAnchorRegistry) -> str | None:
|
|
307
|
+
"""把 fragment 映射为 PDF destination,其他安全 URL 原样保留。"""
|
|
308
|
+
if url.startswith("#"):
|
|
309
|
+
destination = anchors.resolve(url[1:])
|
|
310
|
+
return f"#{destination}" if destination is not None else None
|
|
311
|
+
return url or None
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def _font_for_character(character: str) -> str | None:
|
|
315
|
+
"""为中日韩字符选择 CID 字体,并为非 WinAnsi 字符选择 Unicode 回退。"""
|
|
316
|
+
codepoint = ord(character)
|
|
317
|
+
if 0x3040 <= codepoint <= 0x30FF or 0x31F0 <= codepoint <= 0x31FF or 0xFF66 <= codepoint <= 0xFF9D:
|
|
318
|
+
return JAPANESE_FONT
|
|
319
|
+
if 0x1100 <= codepoint <= 0x11FF or 0x3130 <= codepoint <= 0x318F or 0xAC00 <= codepoint <= 0xD7AF:
|
|
320
|
+
return KOREAN_FONT
|
|
321
|
+
if (
|
|
322
|
+
0x2E80 <= codepoint <= 0x303F
|
|
323
|
+
or 0x3400 <= codepoint <= 0x4DBF
|
|
324
|
+
or 0x4E00 <= codepoint <= 0x9FFF
|
|
325
|
+
or 0xF900 <= codepoint <= 0xFAFF
|
|
326
|
+
or 0xFF00 <= codepoint <= 0xFF65
|
|
327
|
+
or 0x20000 <= codepoint <= 0x2FA1F
|
|
328
|
+
):
|
|
329
|
+
return HAN_FONT
|
|
330
|
+
try:
|
|
331
|
+
character.encode("cp1252")
|
|
332
|
+
except UnicodeEncodeError:
|
|
333
|
+
return UNICODE_FALLBACK_FONT
|
|
334
|
+
return None
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
__all__ = [
|
|
338
|
+
"PdfAnchorRegistry",
|
|
339
|
+
"PdfInlineContext",
|
|
340
|
+
"build_pdf_paragraph",
|
|
341
|
+
"render_pdf_inline_markup",
|
|
342
|
+
"render_plain_text_markup",
|
|
343
|
+
]
|