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,394 @@
|
|
|
1
|
+
"""按来源行和标记组装页面脚注。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import statistics
|
|
6
|
+
from typing import Any, Sequence
|
|
7
|
+
|
|
8
|
+
from .....schema import BBox
|
|
9
|
+
from ..geometry import _bbox_center_y, _bbox_union_many, _rotate_bbox_from_upright, _rotate_bbox_to_upright
|
|
10
|
+
from ..line_layout import _effective_text_row_gap, _line_effective_height, _lines_tight_output_bbox, _title_fonts_compatible
|
|
11
|
+
from ..models import _LineItem
|
|
12
|
+
from .common import _merge_text_line_content
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _build_grouped_page_footnote_blocks(
|
|
16
|
+
lines: list[_LineItem],
|
|
17
|
+
source_groups: Sequence[set[int]],
|
|
18
|
+
page_size: tuple[float, float],
|
|
19
|
+
) -> tuple[list[dict[str, Any]], set[int]]:
|
|
20
|
+
"""按分隔线组切分几何脚注条目,并返回已消费的来源编号。"""
|
|
21
|
+
|
|
22
|
+
footnote_by_source = {line.source_index: line for line in lines if line.semantic_type == "page_footnote"}
|
|
23
|
+
blocks: list[dict[str, Any]] = []
|
|
24
|
+
consumed_source_indices: set[int] = set()
|
|
25
|
+
for source_group in source_groups:
|
|
26
|
+
group_lines = [footnote_by_source[source_index] for source_index in source_group if source_index in footnote_by_source]
|
|
27
|
+
for angle in sorted({line.angle for line in group_lines}):
|
|
28
|
+
directional_lines = [line for line in group_lines if line.angle == angle]
|
|
29
|
+
tight_bboxes = _tight_page_footnote_bboxes(
|
|
30
|
+
directional_lines,
|
|
31
|
+
page_size,
|
|
32
|
+
)
|
|
33
|
+
for entry_lines in _split_page_footnote_entries(
|
|
34
|
+
directional_lines,
|
|
35
|
+
page_size,
|
|
36
|
+
):
|
|
37
|
+
ordered_lines = sorted(
|
|
38
|
+
entry_lines,
|
|
39
|
+
key=lambda line: (
|
|
40
|
+
_rotate_bbox_to_upright(line.bbox, page_size, angle)[1],
|
|
41
|
+
_rotate_bbox_to_upright(line.bbox, page_size, angle)[0],
|
|
42
|
+
line.source_index,
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
content = _merge_text_line_content([line.text for line in ordered_lines])
|
|
46
|
+
if not content:
|
|
47
|
+
continue
|
|
48
|
+
visual_row_ids = {line.visual_row_id for line in ordered_lines if line.visual_row_id is not None}
|
|
49
|
+
single_run_row_id = (
|
|
50
|
+
ordered_lines[0].visual_row_id
|
|
51
|
+
if len(ordered_lines) == 1
|
|
52
|
+
and ordered_lines[0].split_from_row
|
|
53
|
+
and ordered_lines[0].visual_row_id is not None
|
|
54
|
+
else None
|
|
55
|
+
)
|
|
56
|
+
block = {
|
|
57
|
+
"type": "page_footnote",
|
|
58
|
+
"bbox": _bbox_union_many([tight_bboxes[line.source_index] for line in ordered_lines]),
|
|
59
|
+
"angle": angle,
|
|
60
|
+
"content": content,
|
|
61
|
+
"_visual_row_ids": visual_row_ids,
|
|
62
|
+
"_single_run_row_id": single_run_row_id,
|
|
63
|
+
"_inline_math_regions": [region for line in ordered_lines for region in line.inline_math_regions],
|
|
64
|
+
}
|
|
65
|
+
tight_output_bbox = _lines_tight_output_bbox(
|
|
66
|
+
ordered_lines,
|
|
67
|
+
page_size,
|
|
68
|
+
)
|
|
69
|
+
if tight_output_bbox is not None:
|
|
70
|
+
block["_tight_output_bbox"] = tight_output_bbox
|
|
71
|
+
blocks.append(block)
|
|
72
|
+
consumed_source_indices.update(line.source_index for line in ordered_lines)
|
|
73
|
+
return blocks, consumed_source_indices
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _split_page_footnote_entries(
|
|
77
|
+
lines: list[_LineItem],
|
|
78
|
+
page_size: tuple[float, float],
|
|
79
|
+
) -> list[list[_LineItem]]:
|
|
80
|
+
"""按首行或续行缩进模式,把同一分隔线下的脚注行切成条目。"""
|
|
81
|
+
|
|
82
|
+
if not lines:
|
|
83
|
+
return []
|
|
84
|
+
angle = lines[0].angle
|
|
85
|
+
line_geometry = [(line, _rotate_bbox_to_upright(line.bbox, page_size, angle)) for line in lines]
|
|
86
|
+
line_geometry.sort(key=lambda item: (item[1][1], item[1][0], item[0].source_index))
|
|
87
|
+
if len(line_geometry) == 1:
|
|
88
|
+
return [[line_geometry[0][0]]]
|
|
89
|
+
|
|
90
|
+
effective_heights = [_line_effective_height(line, bbox) for line, bbox in line_geometry]
|
|
91
|
+
median_height = statistics.median(effective_heights)
|
|
92
|
+
glyph_widths = [line.median_glyph_width for line, _bbox in line_geometry if line.median_glyph_width is not None]
|
|
93
|
+
median_glyph_width = statistics.median(glyph_widths) if glyph_widths else 0.5 * median_height
|
|
94
|
+
indent_threshold = max(1.5 * median_height, 2.0 * median_glyph_width)
|
|
95
|
+
base_left = min(bbox[0] for _line, bbox in line_geometry)
|
|
96
|
+
maximum_row_width = max(bbox[2] - bbox[0] for _line, bbox in line_geometry)
|
|
97
|
+
marker_rows = _find_page_footnote_marker_rows(
|
|
98
|
+
line_geometry,
|
|
99
|
+
median_height,
|
|
100
|
+
median_glyph_width,
|
|
101
|
+
base_left,
|
|
102
|
+
)
|
|
103
|
+
if marker_rows:
|
|
104
|
+
return _split_marked_page_footnote_entries(
|
|
105
|
+
line_geometry,
|
|
106
|
+
marker_rows,
|
|
107
|
+
median_height,
|
|
108
|
+
median_glyph_width,
|
|
109
|
+
base_left,
|
|
110
|
+
maximum_row_width,
|
|
111
|
+
indent_threshold,
|
|
112
|
+
)
|
|
113
|
+
return _split_unmarked_page_footnote_entries(
|
|
114
|
+
line_geometry,
|
|
115
|
+
base_left,
|
|
116
|
+
maximum_row_width,
|
|
117
|
+
indent_threshold,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _find_page_footnote_marker_rows(
|
|
122
|
+
line_geometry: list[tuple[_LineItem, BBox]],
|
|
123
|
+
median_height: float,
|
|
124
|
+
median_glyph_width: float,
|
|
125
|
+
base_left: float,
|
|
126
|
+
) -> list[tuple[tuple[_LineItem, BBox], tuple[_LineItem, BBox]]]:
|
|
127
|
+
"""用同视觉行的窄左片段和右侧正文识别脚注编号锚点。"""
|
|
128
|
+
|
|
129
|
+
by_visual_row: dict[int, list[tuple[_LineItem, BBox]]] = {}
|
|
130
|
+
for item in line_geometry:
|
|
131
|
+
line = item[0]
|
|
132
|
+
if line.visual_row_id is None or not line.split_from_row:
|
|
133
|
+
continue
|
|
134
|
+
by_visual_row.setdefault(line.visual_row_id, []).append(item)
|
|
135
|
+
|
|
136
|
+
marker_rows: list[tuple[tuple[_LineItem, BBox], tuple[_LineItem, BBox]]] = []
|
|
137
|
+
marker_width_limit = max(1.5 * median_glyph_width, 0.75 * median_height)
|
|
138
|
+
for members in by_visual_row.values():
|
|
139
|
+
ordered = sorted(members, key=lambda item: item[1][0])
|
|
140
|
+
if len(ordered) < 2:
|
|
141
|
+
continue
|
|
142
|
+
marker, body = ordered[0], ordered[1]
|
|
143
|
+
marker_width = marker[1][2] - marker[1][0]
|
|
144
|
+
body_width = body[1][2] - body[1][0]
|
|
145
|
+
horizontal_gap = body[1][0] - marker[1][2]
|
|
146
|
+
if (
|
|
147
|
+
marker_width > marker_width_limit
|
|
148
|
+
or marker[1][0] - base_left > 0.5 * median_height
|
|
149
|
+
or not 0.0 <= horizontal_gap <= 1.5 * median_height
|
|
150
|
+
or body[1][0] - base_left < max(1.5 * marker_width, 0.75 * median_height)
|
|
151
|
+
or body_width < max(4.0 * marker_width, 4.0 * median_glyph_width)
|
|
152
|
+
or abs(_bbox_center_y(marker[1]) - _bbox_center_y(body[1])) > 0.4 * median_height
|
|
153
|
+
):
|
|
154
|
+
continue
|
|
155
|
+
marker_rows.append((marker, body))
|
|
156
|
+
marker_rows.sort(key=lambda pair: (min(pair[0][1][1], pair[1][1][1]), pair[0][1][0]))
|
|
157
|
+
if not marker_rows:
|
|
158
|
+
marker_rows = _find_geometric_page_footnote_marker_rows(
|
|
159
|
+
line_geometry,
|
|
160
|
+
median_height,
|
|
161
|
+
median_glyph_width,
|
|
162
|
+
base_left,
|
|
163
|
+
)
|
|
164
|
+
return marker_rows
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _find_geometric_page_footnote_marker_rows(
|
|
168
|
+
line_geometry: list[tuple[_LineItem, BBox]],
|
|
169
|
+
median_height: float,
|
|
170
|
+
median_glyph_width: float,
|
|
171
|
+
base_left: float,
|
|
172
|
+
) -> list[tuple[tuple[_LineItem, BBox], tuple[_LineItem, BBox]]]:
|
|
173
|
+
"""在 row id 缺失时用同基线窄标记和右侧正文恢复脚注首行。"""
|
|
174
|
+
|
|
175
|
+
marker_width_limit = max(
|
|
176
|
+
1.5 * median_glyph_width,
|
|
177
|
+
0.75 * median_height,
|
|
178
|
+
)
|
|
179
|
+
candidates: list[
|
|
180
|
+
tuple[
|
|
181
|
+
float,
|
|
182
|
+
tuple[_LineItem, BBox],
|
|
183
|
+
tuple[_LineItem, BBox],
|
|
184
|
+
]
|
|
185
|
+
] = []
|
|
186
|
+
for marker in line_geometry:
|
|
187
|
+
marker_width = marker[1][2] - marker[1][0]
|
|
188
|
+
if marker_width > marker_width_limit or marker[1][0] - base_left > 0.5 * median_height:
|
|
189
|
+
continue
|
|
190
|
+
for body in line_geometry:
|
|
191
|
+
if body is marker:
|
|
192
|
+
continue
|
|
193
|
+
body_width = body[1][2] - body[1][0]
|
|
194
|
+
horizontal_gap = body[1][0] - marker[1][2]
|
|
195
|
+
if (
|
|
196
|
+
not 0.0 <= horizontal_gap <= 1.5 * median_height
|
|
197
|
+
or body[1][0] - base_left
|
|
198
|
+
< max(
|
|
199
|
+
1.5 * marker_width,
|
|
200
|
+
0.4 * median_height,
|
|
201
|
+
)
|
|
202
|
+
or body_width
|
|
203
|
+
< max(
|
|
204
|
+
4.0 * marker_width,
|
|
205
|
+
4.0 * median_glyph_width,
|
|
206
|
+
)
|
|
207
|
+
or abs(_bbox_center_y(marker[1]) - _bbox_center_y(body[1])) > 0.4 * median_height
|
|
208
|
+
or not _title_fonts_compatible(
|
|
209
|
+
marker[0],
|
|
210
|
+
body[0],
|
|
211
|
+
)
|
|
212
|
+
):
|
|
213
|
+
continue
|
|
214
|
+
candidates.append(
|
|
215
|
+
(
|
|
216
|
+
horizontal_gap,
|
|
217
|
+
marker,
|
|
218
|
+
body,
|
|
219
|
+
)
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
output = []
|
|
223
|
+
consumed_sources: set[int] = set()
|
|
224
|
+
for _gap, marker, body in sorted(
|
|
225
|
+
candidates,
|
|
226
|
+
key=lambda item: (
|
|
227
|
+
min(item[1][1][1], item[2][1][1]),
|
|
228
|
+
item[0],
|
|
229
|
+
item[1][0].source_index,
|
|
230
|
+
item[2][0].source_index,
|
|
231
|
+
),
|
|
232
|
+
):
|
|
233
|
+
pair_sources = {
|
|
234
|
+
marker[0].source_index,
|
|
235
|
+
body[0].source_index,
|
|
236
|
+
}
|
|
237
|
+
if pair_sources & consumed_sources:
|
|
238
|
+
continue
|
|
239
|
+
output.append((marker, body))
|
|
240
|
+
consumed_sources.update(pair_sources)
|
|
241
|
+
return output
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _split_marked_page_footnote_entries(
|
|
245
|
+
line_geometry: list[tuple[_LineItem, BBox]],
|
|
246
|
+
marker_rows: list[tuple[tuple[_LineItem, BBox], tuple[_LineItem, BBox]]],
|
|
247
|
+
median_height: float,
|
|
248
|
+
median_glyph_width: float,
|
|
249
|
+
base_left: float,
|
|
250
|
+
maximum_row_width: float,
|
|
251
|
+
indent_threshold: float,
|
|
252
|
+
) -> list[list[_LineItem]]:
|
|
253
|
+
"""把编号、右侧首行和对齐续行聚合,并保留编号区之前的独立脚注。"""
|
|
254
|
+
|
|
255
|
+
first_marker_top = min(marker_rows[0][0][1][1], marker_rows[0][1][1][1])
|
|
256
|
+
prefix_geometry = [item for item in line_geometry if item[1][1] < first_marker_top]
|
|
257
|
+
entries = _split_unmarked_page_footnote_entries(
|
|
258
|
+
prefix_geometry,
|
|
259
|
+
base_left,
|
|
260
|
+
maximum_row_width,
|
|
261
|
+
indent_threshold,
|
|
262
|
+
)
|
|
263
|
+
consumed = {line.source_index for entry in entries for line in entry}
|
|
264
|
+
continuation_tolerance = max(0.75 * median_height, 2.0 * median_glyph_width)
|
|
265
|
+
for marker_index, (marker, body) in enumerate(marker_rows):
|
|
266
|
+
next_marker_top = (
|
|
267
|
+
min(marker_rows[marker_index + 1][0][1][1], marker_rows[marker_index + 1][1][1][1])
|
|
268
|
+
if marker_index + 1 < len(marker_rows)
|
|
269
|
+
else float("inf")
|
|
270
|
+
)
|
|
271
|
+
entry_items = [marker, body]
|
|
272
|
+
consumed.update((marker[0].source_index, body[0].source_index))
|
|
273
|
+
previous = body
|
|
274
|
+
for candidate in line_geometry:
|
|
275
|
+
if candidate[0].source_index in consumed:
|
|
276
|
+
continue
|
|
277
|
+
if candidate[1][1] < body[1][1] or candidate[1][1] >= next_marker_top:
|
|
278
|
+
continue
|
|
279
|
+
if abs(candidate[1][0] - body[1][0]) > continuation_tolerance:
|
|
280
|
+
continue
|
|
281
|
+
if _effective_text_row_gap(previous, candidate) > 1.5 * median_height:
|
|
282
|
+
continue
|
|
283
|
+
entry_items.append(candidate)
|
|
284
|
+
consumed.add(candidate[0].source_index)
|
|
285
|
+
previous = candidate
|
|
286
|
+
entry_items.sort(key=lambda item: (item[1][1], item[1][0], item[0].source_index))
|
|
287
|
+
entries.append([line for line, _bbox in entry_items])
|
|
288
|
+
|
|
289
|
+
leftovers = [item for item in line_geometry if item[0].source_index not in consumed]
|
|
290
|
+
entries.extend(
|
|
291
|
+
_split_unmarked_page_footnote_entries(
|
|
292
|
+
leftovers,
|
|
293
|
+
base_left,
|
|
294
|
+
maximum_row_width,
|
|
295
|
+
indent_threshold,
|
|
296
|
+
)
|
|
297
|
+
)
|
|
298
|
+
entries.sort(
|
|
299
|
+
key=lambda entry: min(
|
|
300
|
+
bbox[1] for line in entry for candidate, bbox in line_geometry if candidate.source_index == line.source_index
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
return entries
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def _split_unmarked_page_footnote_entries(
|
|
307
|
+
line_geometry: list[tuple[_LineItem, BBox]],
|
|
308
|
+
base_left: float,
|
|
309
|
+
maximum_row_width: float,
|
|
310
|
+
indent_threshold: float,
|
|
311
|
+
) -> list[list[_LineItem]]:
|
|
312
|
+
"""沿用首行和续行缩进模式切分没有稳定编号锚点的脚注行。"""
|
|
313
|
+
|
|
314
|
+
if not line_geometry:
|
|
315
|
+
return []
|
|
316
|
+
first_line_is_indented = line_geometry[0][1][0] - base_left > indent_threshold
|
|
317
|
+
|
|
318
|
+
entries: list[list[_LineItem]] = [[line_geometry[0][0]]]
|
|
319
|
+
for previous, current in zip(line_geometry, line_geometry[1:]):
|
|
320
|
+
previous_width = previous[1][2] - previous[1][0]
|
|
321
|
+
previous_is_near_full = previous_width >= 0.8 * maximum_row_width
|
|
322
|
+
current_is_indented = current[1][0] - base_left > indent_threshold
|
|
323
|
+
same_left_compact_continuation = (
|
|
324
|
+
not first_line_is_indented
|
|
325
|
+
and previous_width >= 0.7 * maximum_row_width
|
|
326
|
+
and abs(current[1][0] - previous[1][0]) <= 0.5 * indent_threshold
|
|
327
|
+
and _title_fonts_compatible(previous[0], current[0])
|
|
328
|
+
and _effective_text_row_gap(previous, current) <= indent_threshold
|
|
329
|
+
)
|
|
330
|
+
# 满行后必然续接;其余行按首页观测到的首行/续行缩进模式决定边界。
|
|
331
|
+
continues_previous = (
|
|
332
|
+
previous_is_near_full
|
|
333
|
+
or same_left_compact_continuation
|
|
334
|
+
or (not current_is_indented if first_line_is_indented else current_is_indented)
|
|
335
|
+
)
|
|
336
|
+
if continues_previous:
|
|
337
|
+
entries[-1].append(current[0])
|
|
338
|
+
else:
|
|
339
|
+
entries.append([current[0]])
|
|
340
|
+
return entries
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def _tight_page_footnote_bboxes(
|
|
344
|
+
lines: list[_LineItem],
|
|
345
|
+
page_size: tuple[float, float],
|
|
346
|
+
) -> dict[int, BBox]:
|
|
347
|
+
"""以有效行高和相邻行距收紧脚注框,避免异常字体框跨入相邻条目。"""
|
|
348
|
+
|
|
349
|
+
if not lines:
|
|
350
|
+
return {}
|
|
351
|
+
angle = lines[0].angle
|
|
352
|
+
line_geometry = [(line, _rotate_bbox_to_upright(line.bbox, page_size, angle)) for line in lines]
|
|
353
|
+
line_geometry.sort(key=lambda item: (item[1][1], item[1][0], item[0].source_index))
|
|
354
|
+
median_height = statistics.median(_line_effective_height(line, bbox) for line, bbox in line_geometry)
|
|
355
|
+
row_pitches = [
|
|
356
|
+
current[1][1] - previous[1][1]
|
|
357
|
+
for previous, current in zip(line_geometry, line_geometry[1:])
|
|
358
|
+
if current[1][1] - previous[1][1] > 0.25 * median_height
|
|
359
|
+
]
|
|
360
|
+
median_pitch = statistics.median(row_pitches) if row_pitches else None
|
|
361
|
+
|
|
362
|
+
output: dict[int, BBox] = {}
|
|
363
|
+
for line, bbox in line_geometry:
|
|
364
|
+
tight_height = min(
|
|
365
|
+
bbox[3] - bbox[1],
|
|
366
|
+
_line_effective_height(line, bbox),
|
|
367
|
+
)
|
|
368
|
+
if median_pitch is not None:
|
|
369
|
+
tight_height = min(tight_height, 0.9 * median_pitch)
|
|
370
|
+
tight_height = max(0.1, tight_height)
|
|
371
|
+
center_y = _bbox_center_y(bbox)
|
|
372
|
+
tight_local_bbox = (
|
|
373
|
+
bbox[0],
|
|
374
|
+
center_y - 0.5 * tight_height,
|
|
375
|
+
bbox[2],
|
|
376
|
+
center_y + 0.5 * tight_height,
|
|
377
|
+
)
|
|
378
|
+
output[line.source_index] = _rotate_bbox_from_upright(
|
|
379
|
+
tight_local_bbox,
|
|
380
|
+
page_size,
|
|
381
|
+
angle,
|
|
382
|
+
)
|
|
383
|
+
return output
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
__all__ = [
|
|
387
|
+
"_build_grouped_page_footnote_blocks",
|
|
388
|
+
"_split_page_footnote_entries",
|
|
389
|
+
"_find_page_footnote_marker_rows",
|
|
390
|
+
"_find_geometric_page_footnote_marker_rows",
|
|
391
|
+
"_split_marked_page_footnote_entries",
|
|
392
|
+
"_split_unmarked_page_footnote_entries",
|
|
393
|
+
"_tight_page_footnote_bboxes",
|
|
394
|
+
]
|