
    kh                         d Z ddlmZ 	 ddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ  G d de	      Zy# e$ r	 ddlmZ Y 7w xY w)a1  
Text Line objects based on PDF raw dict extracted with ``PyMuPDF``.

Data structure of line in text block referring to this
`link <https://pymupdf.readthedocs.io/en/latest/textpage.html>`_::

    {
        'bbox': (x0,y0,x1,y1),
        'wmode': m,
        'dir': [x,y],
        'spans': [ spans ]
    }
    )Point)Iterable   )TextSpan   )Element)TextDirection)Spans)	ImageSpanc                        e Zd ZdZddef fdZed        Zed        Zed        Z	ed        Z
ed        Zd	 Z fd
Zd ZdefdZd Zd Z xZS )Linez)Object representing a line in text block.rawc                    |i }|j                  dd      | _        d|v r2t        t        |d         t        j                         z        | _        n	ddg| _        |j                  dd      | _        |j                  dd      | _        d|v r|j                  d       t        | -  |       t        | 	      j                  |j                  d
g             | _        y )Nwmoder   dir      ?g        
line_breaktab_stopbbox)parentspans)getr   listr   r   pure_rotation_matrixr   r   r   popsuper__init__r
   restorer   )selfr   	__class__s     N/var/www/teggl/fontify/venv/lib/python3.12/site-packages/pdf2docx/text/Line.pyr   zLine.__init__!   s    ;b WWWa(
 C<E#e*-d.G.G.IIJDHSzDH '',2
A. S=#''&/ $'//0DE
    c                 t    | j                   D cg c]  }|j                   }}dj                  |      S c c}w )zIJoining span text. Note image is translated to a placeholder ``<image>``. )r   textjoinr   span
spans_texts      r!   r%   z	Line.text9   s3     -1JJ7Ddii7
7wwz"" 8s   5c                     | j                   D cg c]  }t        |t              s|j                  ! }}dj	                  |      S c c}w )z%Joining span text with image ignored.r$   )r   
isinstancer   r%   r&   r'   s      r!   raw_textzLine.raw_text@   s=     -1JJUD*T8:TdiiU
Uwwz"" Vs
   AAc                     | j                   D ]0  }t        |t              s y|j                  j	                         s0 y y)zXIf this line contains only white space or not. If True, this line is safe to be removed.FT)r   r+   r   r%   stripr   r(   s     r!   white_space_onlyzLine.white_space_onlyG   s8     JJ 	/DdH-eyy 	/ r"   c                 B    t        t        d | j                              S )zGet image spans in this Line.c                 "    t        | t              S N)r+   r   )r(   s    r!   <lambda>z"Line.image_spans.<locals>.<lambda>T   s    D)4 r"   )r   filterr   r   s    r!   image_spanszLine.image_spansP   s!     F4djj
  	r"   c                     | j                   d   dk(  rt        j                  S | j                   d   dk(  rt        j                  S t        j                  S )zGet text direction. Consider ``LEFT_RIGHT`` and ``LEFT_RIGHT`` only.

        Returns:
            TextDirection: Text direction of this line.
        r   r   r   g      )r   r	   
LEFT_RIGHT
BOTTOM_TOPIGNOREr6   s    r!   text_directionzLine.text_directionX   sH     88A;# +++XXa[D  +++ '''r"   c                 6    | j                   j                         S )z.Remove redundant blanks at the begin/end span.)r   r.   r6   s    r!   r.   z
Line.stripg   s    zz!!r"   c                     t         |          }|j                  | j                  | j                  | j
                  | j                  | j                  D cg c]  }|j                          c}d       |S c c}w )N)r   r   r   r   r   )r   storeupdater   r   r   r   r   )r   resr(   r    s      r!   r?   z
Line.storem   sd    gmo

**((//--)-!%


 	 
s   A7c                 x    t        |t              r|D ]  }| j                  |        y| j                  |       y)zAdd span list to current Line.
        
        Args:
            span_or_list (Span, Iterable): TextSpan or TextSpan list to add.
        N)r+   r   add_span)r   span_or_listr(   s      r!   addzLine.add|   s7     lH-$ $d#$ MM,'r"   r(   c                 :    | j                   j                  |       y)zAdd span to current Line.N)r   appendr/   s     r!   rC   zLine.add_span   s    

$r"   c                    |j                  | j                        r| j                         S t        d| j                  i      }| j
                  |_        | j                  D ]$  }|j                  |      }|j                  |       & |S )zCreate new Line object with spans contained in given bbox.
        
        Args:
            rect (fitz.Rect): Target bbox.
        
        Returns:
            Line: The created Line instance.
        r   )	containsr   copyr   r   r   r   
intersectsrE   )r   rectliner(   contained_spans        r!   rK   zLine.intersects   sv     ==#99; Wdjj)*88 JJ 	%D!__T2NHH^$	% r"   c                    | j                   r8t        | j                         D ]   }|j                         j                          " | j                  D ]  }|j                  |        | j                  r|j                  d       yy)z0Create docx line, i.e. a run in ``python-docx``.
N)r   rangeadd_runadd_tabr   	make_docxr   )r   p_r(   s       r!   rT   zLine.make_docx   sa     ==4==)@199;+>+>+@@ JJ1Dq 11 ??AIIdO?r"   r3   )__name__
__module____qualname____doc__dictr   propertyr%   r,   r0   r7   r<   r.   r?   rE   r   rC   rK   rT   __classcell__)r    s   @r!   r   r      s    3F4 F0 # # # #     ( ("
( G  
2,r"   r   N)rZ   fitzr   collectionsr   ImportErrorcollections.abcr   common.Elementr   common.sharer	   r
   image.ImageSpanr   r    r"   r!   <module>rf      sJ    )$  $ (  'S,7 S,  )()s   : AA