o
    e                     @   s   d Z ddlmZ zddlmZ W n ey   ddlmZ Y nw 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 de	ZdS )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                       s   e Zd ZdZddef fddZedd Zedd	 Zed
d Z	edd Z
edd Zdd Z fddZdd ZdefddZdd Zdd Z  ZS )Linez)Object representing a line in text block.Nrawc                    s   |d u ri }| dd| _d|v rtt|d t  | _nddg| _| dd| _| dd| _d|v r;|	d t
 | t| d	| d
g | _d S )Nwmoder   dir      ?g        
line_breaktab_stopbbox)parentspans)getr   listr   r   Zpure_rotation_matrixr   r   r   popsuper__init__r	   restorer   )selfr   	__class__ BD:\Projects\ConvertPro\env\Lib\site-packages\pdf2docx/text/Line.pyr   !   s   
zLine.__init__c                 C      dd | j D }d|S )zIJoining span text. Note image is translated to a placeholder ``<image>``.c                 S   s   g | ]}|j qS r   )text.0spanr   r   r   
<listcomp><   s    zLine.text.<locals>.<listcomp> r   joinr   Z
spans_textr   r   r   r!   9      
z	Line.textc                 C   r    )z%Joining span text with image ignored.c                 S   s   g | ]
}t |tr|jqS r   )
isinstancer   r!   r"   r   r   r   r%   C   s    z!Line.raw_text.<locals>.<listcomp>r&   r'   r)   r   r   r   raw_text@   r*   zLine.raw_textc                 C   s0   | j D ]}t|ts dS |j r dS qdS )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$   r   r   r   white_space_onlyG   s   
zLine.white_space_onlyc                 C   s   t tdd | jS )zGet image spans in this Line.c                 S   s
   t | tS N)r+   r
   )r$   r   r   r   <lambda>T   s   
 z"Line.image_spans.<locals>.<lambda>)r   filterr   r   r   r   r   image_spansP   s   
zLine.image_spansc                 C   s.   | 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   Z
LEFT_RIGHTZ
BOTTOM_TOPZIGNOREr3   r   r   r   text_directionX   s
   zLine.text_directionc                 C   s
   | j  S )z.Remove redundant blanks at the begin/end span.)r   r-   r3   r   r   r   r-   g   s   
z
Line.stripc                    s8   t   }|| j| j| j| jdd | jD d |S )Nc                 S   s   g | ]}|  qS r   )storer"   r   r   r   r%   t   s    zLine.store.<locals>.<listcomp>)r   r   r   r   r   )r   r6   updater   r   r   r   r   )r   resr   r   r   r6   m   s   

z
Line.storec                 C   s0   t |tr|D ]}| | qdS | | dS )zAdd span list to current Line.
        
        Args:
            span_or_list (Span, Iterable): TextSpan or TextSpan list to add.
        N)r+   r   add_span)r   Zspan_or_listr$   r   r   r   add|   s
   
zLine.addr$   c                 C   s   | j | dS )zAdd span to current Line.N)r   appendr.   r   r   r   r9      s   zLine.add_spanc                 C   sN   | | jr
|  S td| ji}| j|_| jD ]}||}|| q|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   
intersectsr:   )r   rectliner$   Zcontained_spanr   r   r   r>      s   


zLine.intersectsc                 C   s@   | j r	|   | jD ]}|| q| jr|d dS dS )z0Create docx line, i.e. a run in ``python-docx``.
N)r   Zadd_runZadd_tabr   	make_docxr   )r   pr$   r   r   r   rB      s   zLine.make_docxr0   )__name__
__module____qualname____doc__dictr   propertyr!   r,   r/   r4   r5   r-   r6   r:   r   r9   r>   rB   __classcell__r   r   r   r   r      s&    




r   N)rG   Zfitzr   collectionsr   ImportErrorcollections.abcr   Zcommon.Elementr   Zcommon.sharer   r	   Zimage.ImageSpanr
   r   r   r   r   r   <module>   s   