ó
    À‘´jõ  ã                  óØ   • S r SSKJr  SSKJr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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r S         SS jjrSS jrSS jrSS jrg)u\  Group the parse's floating text by the scan's block partition.

A page like a stat-panel overview prints its content as short label and value
lines. The parse captures every line, but as separate records â€” `$78B` is one
record, `Investor capital` another, joined by nothing â€” and anything consuming
the records one at a time hands a reader a label without its number. The scan's
response partitions the same page into blocks: every line belongs to exactly
one node, and the lines of one panel come back inside one node.

This module carries that partition onto the parse's own elements. Each plain
text item on a scanned page is assigned to the block whose rectangle holds its
center, and the items of a block are re-parented under one group node in the
document, in their reading order, at the first member's position in the flow.
The items themselves are untouched: their text, boxes and references stay the
parse's own, and nothing is transcribed from the scan. The scan contributes one
fact the parse does not state â€” which lines sit together.

The blocks come from the response's text nodes and from its table nodes. The
scan reads a panel collage as a table as readily as it reads one as text, and
a printed panel is the same panel either way. A table node contributes each of
its cells as a block, so a value and its label bind at the cell, not across
the whole collage; the node's own rectangle stands behind the cells for text
that sits between them, and the smallest containing block always wins.

The same division of authority the graft enforces for figures: the scan looked
at the page, so it decides the partition; the parse's elements supply the
content.

Only plain text items parented to the document body take part. An item under a
picture is the graft's business, a heading or a footnote or a caption has a
role the partition must not swallow, and an item already inside a group is
left alone, whether the parse made the group (a list, an inline group) or an
earlier run did. That is why a rerun over an enriched parse changes nothing.
A block holding fewer than two items gains nothing from a group and gets none.
é    )Úannotations)ÚDictÚListÚOptionalÚSequenceÚTuple)ÚDoclingDocumentÚTextItem)ÚNodeItem)ÚDocItemLabelÚ
GroupLabel)Úlogger)Ú
PageDigest)ÚNormBoxÚnorm_boxÚprov_box)ÚPageScanzscan-text-blockNc                óÒ  • [        U=(       d    0 5      nU R                  R                  5        HI  u  pVXT;  d  M  UR                  c  M  UR                  R                  UR                  R
                  4XE'   MK     SnU GHÝ  nUR                  UR                  5      n	U	c  M$  S U	R                   5        V
s/ s H
  n
U
c  M  U
PM     nn
U	R                   HP  nUR                  S UR                   5       5        [        UR                  5      nUc  M?  UR                  U5        MR     U(       d  Mµ  UR                  UR                  S5      u  pï0 n[        XR                  5       H@  n[!        UXï5      n
[#        X«5      nUc  M  UR%                  U/ 5      R                  U5        MB     ['        U5       H   nUU   n[)        U5      S:  a  M  U R+                  US   [,        R.                  [0        SS9nU H  n[3        U UU5        M     US	-  n[4        R6                  " S
UR                  [)        U5      SR9                  S USS  5       5      5        M¢     GMà     U$ s  sn
f )zßGroup each scanned page's plain text by the scan's block partition.

Mutates `document`: the items of each block move under one group node at
the first member's reading-order position. Returns how many groups were
written.
Nr   c              3  ón   #   • U  H+  oR                   S :X  d  M  [        UR                  5      v •  M-     g7f)ÚtextN)Úkindr   Úbox©Ú.0Úitems     Ú</home/mande/repo/quber/src/quber/core/figures/dpt3/blocks.pyÚ	<genexpr>Ú%group_scanned_text.<locals>.<genexpr>P   s)   é € Ð]²~¨tÏÉÐV\ÑI\Ó*œ §¡×*Ð*²~ùs   ‚5™5c              3  ó^   #   • U  H#  n[        [        U5        H  o"c  M  Uv •  M     M%     g 7f©N)Úmapr   )r   Úrowr   s      r   r   r   T   s%   é € ÐkÒ)9 #ÄSÌÐSV×EW¸cŸ#™#ÑEW™#Ò)9ùs   ‚- -)g      ƒ@g     Àˆ@é   F)ÚsiblingÚlabelÚnameÚafteré   z,Text blocks: page {} grouped {} item(s) ({})z / c              3  óP   #   • U  H  oR                   =(       d    S SS v •  M     g7f)Ú Né   )r   r   s     r   r   r   p   s   é € ÐHºi°dŸI™IŸO¨¨S¨bÕ1ºiùs   ‚$&é   )ÚdictÚpagesÚitemsÚsizeÚwidthÚheightÚgetÚpageÚcontextÚtablesÚextendÚ
cell_boxesr   r   ÚappendÚ_floating_textr   Ú_owning_blockÚ
setdefaultÚsortedÚlenÚinsert_groupr   ÚUNSPECIFIEDÚ
SCAN_BLOCKÚ	_reparentr   ÚinfoÚjoin)ÚdocumentÚscansÚdigestsÚ	page_dimsÚdimsÚpage_nor4   ÚwrittenÚscanÚdigestr   ÚblocksÚtableÚ	table_boxr1   r2   Úmembersr   ÚblockÚblock_indexr/   Úgroups                         r   Úgroup_scanned_textrU   8   s  € ô �	—˜RÓ €DØ!Ÿ™×-Ñ-Ö/‰ˆØÕ 4§9¡9Ó#8Ø!ŸY™YŸ_™_¨d¯i©i×.>Ñ.>Ð?ˆD‹Mñ 0ð €GÜˆØ—‘˜TŸY™YÓ'ˆØ‰>Ùñ ^°v·~²~Ô]ó
â]�Ø÷ Ù]ð 	ð 
ð
 —]”]ˆEØ�M‰MÑk¨×)9Ò)9ÓkÔkÜ  §¡Ó+ˆIØÓ$Ø—‘˜iÖ(ñ	 #ö
 ÙØŸ™ §¡¨NÓ;‰ˆØ-/ˆÜ" 8¯Y©YÖ7ˆDÜ˜4 Ó/ˆCÜ! #Ó.ˆEØÓ Ø×"Ñ" 5¨"Ó-×4Ñ4°TÖ:ñ	 8ô " 'ž?ˆKØ˜KÑ(ˆEÜ�5‹z˜A‹~ÙØ×)Ñ)Ø˜a™¬
×(>Ñ(>ÄZÐW\ð *ð ˆEó �Ü˜( D¨%Ö0ñ à�q‰LˆGÜ�KŠKØ>Ø—	‘	Ü�E“
Ø—
‘
ÑH¸eÀBÀQ¹iÓHÓHö	ô +ñ1 ðP €NùòI
s   Â9I$ÃI$c                óÂ  • U R                   n/ nU R                   HÀ  nUR                  [        R                  :w  d(  UR
                  =(       d    SR                  5       (       d  MK  UR                  (       a  UR                  S   R                  U:w  a  M{  UR                  (       a  UR                  R                  U 5      OSnXRLa  M¯  UR                  U5        MÂ     U$ )a  The page's plain text items parented to the body, in document order.

Everything else keeps its place: an item under a picture belongs to the
graft, a labelled item has a role, and an item already inside a group,
the parse's own or an earlier run's, keeps its group.
r*   r   N)ÚbodyÚtextsr%   r   ÚTEXTr   ÚstripÚprovrJ   ÚparentÚresolver9   )rE   r4   rW   Úfoundr   r\   s         r   r:   r:   u   s¢   € ð �=‰=€DØ€EØ—”ˆØ�:‰:œ×*Ñ*Ó*°4·9±9·?À×2IÑ2I×2KÑ2KÙØ�y�y˜DŸI™I a™L×0Ñ0°DÓ8ÙØ26·+·+�—‘×$Ñ$ XÔ.À4ˆØÒÙØ�‰�TÖñ ð €Ló    c                ó  • U c  gU S   U S   -   S-  U S   U S   -   S-  p2Sn[        U5       HF  u  nu  pgp‰Xbs=::  a  U::  d  M  O  M  Xss=::  a  U	::  d  M)  O  M-  X†-
  X—-
  -  n
Ub
  X¤S   :  d  MC  X¥4nMH     Ub  US   $ S$ )zrThe block whose rectangle holds the item's center; the smallest when
blocks overlap; None when no block claims it.Nr   r#   r(   é   )Ú	enumerate)r   rN   ÚcxÚcyÚbestÚiÚx1Úy1Úx2Úy2Úareas              r   r;   r;   Š   s    € ð �{ØØ�!‰f�s˜1‘v‰o Ñ" S¨¡V¨c°!©f¡_¸Ñ$9ˆØ(,€DÜ(¨Ö0ÑˆÑˆB�BØ�>�r�>‰>˜b�n¨"Ÿn™nØ‘G ¡Ñ(ˆDØ‰|˜t¨1¡g�~Ø�y’ñ	  1ð
 Ñ&ˆ4�‰7Ð0¨DÐ0r_   c                óf  • UR                   (       a  UR                   R                  U 5      OSnUb=  UR                   Vs/ s H   oDR                  UR                  :w  d  M  UPM"     snUl        UR                  R                  UR                  5       5        UR                  5       Ul         gs  snf )z½Move one item under the group, keeping its identity and its order.

Reference surgery rather than delete-and-re-add: the item keeps its
self_ref, so nothing else in the document renumbers.
N)r\   r]   ÚchildrenÚcrefÚself_refr9   Úget_ref)rE   r   rT   Ú
old_parentÚrefs        r   rB   rB   ™   s€   € ð 37·+·+�—‘×$Ñ$ XÔ.À4€JØÑØ.8×.AÒ.AÓ_Ò.A sÇXÁXÐQU×Q^ÑQ^ÑE^ŸsÑ.AÑ_ˆ
ÔØ	‡N�N×Ñ˜$Ÿ,™,›.Ô)Ø—-‘-“/€D…Kùò `s   Á B.Á!B.r    )
rE   r	   rF   zSequence[PageScan]rG   zDict[int, PageDigest]rH   z(Optional[Dict[int, Tuple[float, float]]]ÚreturnÚint)rE   r	   r4   rt   rs   zList[TextItem])r   zOptional[NormBox]rN   zList[NormBox]rs   zOptional[int])rE   r	   r   r
   rT   r   rs   ÚNone) Ú__doc__Ú
__future__r   Útypingr   r   r   r   r   Údocling_core.types.doc.documentr	   r
   Ú!docling_core.types.doc.items.noder   Údocling_core.types.doc.labelsr   r   Úlogurur   Úquber.core.figures.dpt3.modelsr   Úquber.core.figures.geometryr   r   r   Úquber.core.figures.modelsr   rA   rU   r:   r;   rB   © r_   r   Ú<module>r�      s}   ðñ"õH #ç 8Õ 8ç EÝ 6ß BÝ å 5ß CÑ CÝ .ð
 €
ð ;?ð	:Øð:àð:ð #ð:ð 8ð	:ð
 	õ:ôzô*1õ
"r_   