ó
    À‘´jf  ã                  ót  • S r SSKJr  SSKJr  SSKJrJrJrJ	r	  SSK
JrJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJr  SSKJr  SSK J!r!J"r"J#r#  \RH                  \RJ                  4r&  S         SS jjr          SS jr'        SS jr(        SS jr)SS jr*g)u¡  The track's graft: supplant content readings, preserve role-labelled text.

The scan's reading replaces docling's stray text fragments under a chart â€” two
readings of one chart must not coexist. But docling labels its text items, and
an item labelled footnote or caption is not a reading of the chart: it is
native page text with a known role. The shared
`quber.core.figures.graft.graft_figures` deletes every text child of a read
picture except the labels in its `preserve` argument. The dpt-2 path passes
none, so a footnote the parse filed under a read picture is deleted and the
correction agent's read of the page image is its only record. This wrapper
passes `PRESERVED_LABELS`, which is what gives footnote resolution its second
source.

Everything else â€” how figures and pictures are matched, how a region is
reshaped to hold one picture per figure, what lands on the picture â€” is that
shared machinery, unchanged.
é    )Úannotations)ÚPath)ÚDictÚListÚOptionalÚTuple)ÚBoundingBoxÚCoordOrigin)ÚProvenanceItem)ÚDoclingDocument)ÚDocItemLabel)Úlogger)ÚExtractedTable)ÚNormBoxÚnorm_boxÚprov_box)Úgraft_figures)ÚPageScan)Úattach_attributionÚmarkdown_to_table_dataÚreading_order_anchorNc                ó    • [        XX#[        S9$ )zvCarry each scan's figure text onto the matching picture, keeping
footnote- and caption-labelled items in the document.)ÚsourceÚpreserve)Úshared_graft_figuresÚPRESERVED_LABELS)ÚdocumentÚscansÚ	page_dimsr   s       Ú;/home/mande/repo/quber/src/quber/core/figures/dpt3/graft.pyr   r   )   s   € ô   °ÔTdÑeÐeó    c           	     ób  • U Vs0 s H"  oDR                   (       d  M  UR                   U_M$     nn/ nU GHq  nUR                   GH\  nUR                   b  UR                  c  UR                  b  M-  UR	                  UR                   5      n	U	b(  U	R
                  =(       d    SR                  5       (       d.  UR                  SUR                   SUR                   S35        M¡  [        U	R
                  5      n
[        UR                  UR                  U5      n[        X	U5      u  pÍUc  U R                  X«S9nOU R                  XÊX½S9n[!        XX“5        UR"                  Ul        [$        R&                  " SUR                  UR                   UR"                  5        GM_     GMt     U$ s  snf )uC  Insert each captured table the document held no element for.

The capture step vetted the grid the way it vets every scanned table, and
the region it stands on holds no table, no picture and no parse text â€” a
map's legend is the standing example â€” so the document gains a table at
the region's reading-order position, attributed like any other. Without
this the page's only reading of those values stays outside the document.

Mutates `document`. Each inserted grid's record is given the new table's
reference. Returns one error per orphan whose extraction produced no body.
Ú úpage z: the scan read a table (zz) over a region the parse holds nothing for, and its extraction produced no body; the values are in the table records only)ÚdataÚprov)Úsiblingr%   r&   Úafterz<Orphan table: page {} inserted the scan's reading ({}) as {})Útable_idÚtablesÚ	table_refÚpicture_refÚgetÚmarkdownÚstripÚappendÚpageÚchunk_idr   Ú_table_provÚboxr   Ú	add_tableÚinsert_tabler   Úself_refr   Úinfo)r   r   r*   r   ÚtÚby_idÚerrorsÚscanÚscannedÚ	extractedr%   r&   r'   r(   Úinserteds                  r    Úinsert_orphan_tablesr@   4   s~  € ñ" %+Ó9¢F˜q¯j­j‹]ˆQ�Z‰Z˜Š]¡F€EÐ9Ø€FÜˆØ—{•{ˆGØ×ÑÑ'¨7×+<Ñ+<Ñ+HÈG×L_ÑL_ÑLkÙØŸ	™	 '×"2Ñ"2Ó3ˆIØÑ ¨×);Ñ);×)A¸r×(HÑ(H×(JÑ(JØ—‘Ø˜DŸI™I˜;Ð&?À×@PÑ@PÐ?Qð R4ð 4ôñ
 Ü)¨)×*<Ñ*<Ó=ˆDÜ˜wŸ{™{¨D¯I©I°yÓAˆDÜ1°(ÀyÓQ‰NˆGØ‰Ø#×-Ñ-°4Ð-ÐC‘à#×0Ñ0¸ÐRVÐ0Ðd�Ü˜x°9ÔHØ (× 1Ñ 1ˆGÔÜ�KŠKØNØ—	‘	Ø× Ñ Ø×!Ñ!÷	ô) #ñ ð6 €Mùò; :s
   …F,�F,c                ó¼   • [        U 5      nUc  gUR                  US5      u  pEUu  pgp‰[        Xd-  X„-  SU-
  U-  SU	-
  U-  [        R                  S9n
[        XSS9$ )zIA provenance record for an inserted table, its box in bottom-left points.N©g      ƒ@g     Àˆ@g      ð?)ÚlÚrr9   ÚbÚcoord_origin)r   r   )Úpage_noÚbboxÚcharspan)r   r-   r	   r
   Ú
BOTTOMLEFTr   )r4   r1   r   Ú
normalizedÚwidthÚheightÚx1Úy1Úx2Úy2rH   s              r    r3   r3   e   su   € ô ˜#“€JØÑØØ—M‘M $¨Ó7�M€EØ�N€BˆBÜØ
‰*Ø
‰*Ø�‰8�vÑ
Ø�‰8�vÑ
Ü ×+Ñ+ñ€Dô  $¸FÑCÐCr!   c           	     ó˜  ^• 0 nUR                    Hu  nUR                  (       d  M  UR                  S   R                  nUR                  US5      u  pg[	        XFU5      mTc  MT  UR                  U/ 5      R                  T5        Mw     / nU  H¹  n	U	R                    H¦  n
U
R                  c  U
R                  c  U
R                  b  M,  [        U
R                  5      mTb6  [        U4S jUR                  U	R                  / 5       5       5      (       a  Mz  UR                  SU	R                   SU
R                   S35        M¨     M»     U$ )uï  One flag per scanned table over a region the document holds nothing for.

The scan reads a stat-panel collage as a table, and the parse routinely
holds neither a table nor a picture there â€” the region is native text. The
capture step then never extracts it, and without this check the table would
vanish with no record that anything had been read. The cells stay in the
digest records, and the page's own text is grouped by the same node's
rectangles, so the flag is a pointer for review rather than a loss report.

A region any table in the document shares any area with on its page is not
flagged, however small the overlap: the table engine is authoritative for
text-layer tables, and the scan re-reading one is redundancy by design, not
a loss.
r   rB   c              3  ó<   >#   • U  H  n[        TU5      v •  M     g 7f)N)Ú	_overlaps)Ú.0Úotherr4   s     €r    Ú	<genexpr>Ú!unhomed_tables.<locals>.<genexpr>�   s   øé € Ð&lÒJkÀ¤y°°e×'<Ð'<ÒJkùs   ƒr$   z: the scan returned a table (z‘) over a region the parse holds no table or picture for; its cells are in the digest records and the page's own text is grouped by its rectangles)r*   r&   rG   r-   r   Ú
setdefaultr0   r)   r+   r,   r   r4   Úanyr1   r2   )r   r   r   Útables_by_pageÚitemr1   rL   rM   Úflagsr<   Útabler4   s              @r    Úunhomed_tablesr_   z   s%  ø€ ð& 02€NØ—”ˆØ�y�yÙØ�y‰y˜‰|×#Ñ#ˆØ!Ÿ™ d¨NÓ;‰ˆÜ�t FÓ+ˆØ‹?Ø×%Ñ% d¨BÓ/×6Ñ6°sÖ;ñ  ð €EÛˆØ—[”[ˆEØ�~‰~Ñ)¨U¯_©_Ñ-HÈE×L]ÑL]ÑLiÙÜ˜5Ÿ9™9Ó%ˆCØ‰¤3Ô&lÈ.×J\ÑJ\Ð]a×]fÑ]fÐhjÔJkÓ&l×#lÑ#lÙØ�L‰LØ˜Ÿ	™	�{Ð"?ÀÇÁÐ?Oð PGð Göó !ñ ð €Lr!   c                ó¤   • [        U S   US   5      [        U S   US   5      :„  =(       a%    [        U S   US   5      [        U S   US   5      :„  $ )z'Do the two boxes share any area at all?é   r   é   é   )ÚminÚmax)ÚonerV   s     r    rT   rT   §   sZ   € äˆs�1‰v�u˜Q‘xÓ ¤3 s¨1¡v¨u°Q©xÓ#8Ñ8×j¼SÀÀQÁÈÈqÉÓ=RÔUXÐY\Ð]^ÑY_ÐafÐghÑaiÓUjÑ=jÐjr!   )NN)
r   r   r   úList[PageScan]r   z(Optional[Dict[int, Tuple[float, float]]]r   zOptional[Path]Úreturnz!Tuple[DoclingDocument, List[str]])
r   r   r   rg   r*   zList[ExtractedTable]r   úDict[int, Tuple[float, float]]rh   ú	List[str])r4   zOptional[Dict[str, float]]r1   Úintr   ri   rh   zOptional[ProvenanceItem])r   rg   r   r   r   ri   rh   rj   )rf   r   rV   r   rh   Úbool)+Ú__doc__Ú
__future__r   Úpathlibr   Útypingr   r   r   r   Údocling_core.types.doc.baser	   r
   Ú'docling_core.types.doc.common.referencer   Údocling_core.types.doc.documentr   Údocling_core.types.doc.labelsr   Úlogurur   Úquber.core.extractors.baser   Úquber.core.figures.geometryr   r   r   Úquber.core.figures.graftr   r   Úquber.core.figures.modelsr   Úquber.core.fusion.graftr   r   r   ÚFOOTNOTEÚCAPTIONr   r@   r3   r_   rT   © r!   r    Ú<module>r~      s(  ðñõ$ #å ß .Ó .ç @Ý BÝ ;Ý 6Ý å 5ß CÑ CÝ JÝ .ß dÑ dð !×)Ñ)¨<×+?Ñ+?Ð@Ð ð ;?Ø!ð	fØðfàðfð 8ðfð ð	fð
 'õfð.Øð.àð.ð !ð.ð .ð	.ð
 ô.ðbDØ	#ðDà
ðDð .ðDð ô	Dð**Øð*àð*ð .ð*ð ô	*õZkr!   