o
    e                     @   s4  d dl mZ d dlZd dlmZ d dlmZmZ G dd deZG dd deZ	G d	d
 d
eZ
G dd deZG dd dZG dd dZdd Zdd Zd6dedefddZd7defddZdefddZd efd!d"Zd8d$ed%ed&ed'ed(ef
d)d*Zd+efd,d-Zd.ed/ed0efd1d2Zd9d0efd4d5ZdS ):    )EnumN)Iterable)getColorListgetColorInfoListc                   @   (   e Zd ZdZdZdZdZdZdZdZ	dS )		BlockTypezBlock types.r               N)
__name__
__module____qualname____doc__	UNDEFINEDZTEXTZIMAGEZLATTICE_TABLEZSTREAM_TABLEZFLOAT_IMAGE r   r   ED:\Projects\ConvertPro\env\Lib\site-packages\pdf2docx/common/share.pyr          r   c                   @   r   )	RectTypezShape type in context.r	   r
   r             N)
r   r   r   r   Z	HIGHLIGHTZ	UNDERLINEZSTRIKEZ	HYPERLINKZBORDERZSHADINGr   r   r   r   r      r   r   c                   @   s    e Zd ZdZdZdZdZdZdS )TextDirectiona-  Text direction.    
    * LEFT_RIGHT: from left to right within a line, and lines go from top to bottom
    * BOTTOM_TOP: from bottom to top within a line, and lines go from left to right
    * MIX       : a mixture if LEFT_RIGHT and BOTTOM_TOP
    * IGNORE    : neither LEFT_RIGHT nor BOTTOM_TOP
    r   r   r	   r
   N)r   r   r   r   ZIGNORE
LEFT_RIGHT
BOTTOM_TOPMIXr   r   r   r   r      s    r   c                   @   r   )	TextAlignmentzText alignment.

    .. note::
        The difference between ``NONE`` and ``UNKNOWN``: 

        * NONE: none of left/right/center align -> need TAB stop
        * UNKNOWN: can't decide, e.g. single line only
    r   r   r	   r
   r   r   N)
r   r   r   r   NONEUNKNOWNLEFTZCENTERRIGHTZJUSTIFYr   r   r   r   r   )   s    r   c                   @   s@   e Zd ZdZedd Zedd Zedd Zedd	 Zd
S )ITextz2Text related interface considering text direction.c                 C   s   t jS )z0Text direction is from left to right by default.)r   r   selfr   r   r   text_direction<   s   zIText.text_directionc                 C   s   | j tjkp| j tjkS )z3Check whether text direction is from left to right.)r%   r   r   r   r#   r   r   r   is_horizontal_textA   s   
zIText.is_horizontal_textc                 C      | j tjkS )z3Check whether text direction is from bottom to top.)r%   r   r   r#   r   r   r   is_vertical_textG   s   zIText.is_vertical_textc                 C   r'   )zYCheck whether text direction is either from left to 
        right or from bottom to top.)r%   r   r   r#   r   r   r   is_mix_textL   s   zIText.is_mix_textN)	r   r   r   r   propertyr%   r&   r(   r)   r   r   r   r   r"   :   s    


r"   c                   @   s    e Zd ZdZdd Zdd ZdS )lazypropertyz-Calculate only once and cache property value.c                 C   s
   || _ d S Nfunc)r$   r.   r   r   r   __init__U   s   
zlazyproperty.__init__c                 C   s*   |d u r| S |  |}t|| j j| |S r,   )r.   setattrr   )r$   instanceclsvaluer   r   r   __get__X   s
   
zlazyproperty.__get__N)r   r   r   r   r/   r4   r   r   r   r   r+   S   s    r+   c                 C   s$   zt |  W dS  ty   Y dS w )z$Whether can be converted to a float.FT)float
ValueError)Z
str_numberr   r   r   	is_numberd   s   
r7   c                 c   s<    | D ]}t |trt ||st||E dH  q|V  qdS )z%Yield items from any nested iterable.N)
isinstancer   flatten)itemsklassitemr   r   r   r9   n   s   r9   numberndigitsc                 C   s   d| }t ||  | S )zPRound number to lower bound with specified digits, e.g. lower_round(1.26, 1)=1.2g      $@int)r=   r>   nr   r   r   lower_roundv   s   rB    namec                 C   sf   | r|   t v rt |   }ntdtt d }t | }|d d |d d |d d fS )zaGet a named RGB color (or random color) from fitz predefined colors, e.g. 'red' -> (1.0,0.0,0.0).r   r	   g     o@r
   r   )upperr   indexrandomrandintlenr   )rD   poscr   r   r   rgb_component_from_name   s
   
"rL   srgbc                    s(   t | dd d  fdddD S )zsrgb value to R,G,B components, e.g. 16711680 -> (255, 0, 0).
    
    Equal to PyMuPDF built-in method::

        [int(255*x) for x in fitz.sRGB_to_pdf(x)]
    r
   N   c                    s"   g | ]}t  ||d   dqS )r
   r   r?   ).0isr   r   
<listcomp>   s   " z!rgb_component.<locals>.<listcomp>)r   r
   r   )hexzfill)rM   r   rQ   r   rgb_component   s   rV   rgbc                 C   s>   d}t | D ]\}}|t|d ddd|    7 }qt|S )z:RGB components to decimal value, e.g. (1,0,0) -> 16711680.r      r   r   r
   )	enumerater@   )rW   resrP   xr   r   r   rgb_to_value   s   "r\   d   rK   myk
cmyk_scalec           	      C   sr   d| t |  d|t |   }d|t |  d|t |   }d|t |  d|t |   }t|||g}|S )zCMYK components to GRB value.      ?)r5   r\   )	rK   r^   r_   r`   ra   rgbrZ   r   r   r   cmyk_to_rgb   s
      rf   
componentsc           
      C   s   t | }|dkrtt| \}}}}t||||dd}|S |dkr1tt| \}}}	t|||	g}|S |dkrDt| d }t|||g}|S d}|S )z-Gray/RGB/CMYK mode components to color value.r   rb   )ra   r   r	   r   )rI   mapr5   rf   r\   )
rg   numrK   r^   r_   r`   colorrc   rd   re   r   r   r   	rgb_value   s   rk   widthheighttitlec                 C   s,   | j ||d}td}|jd||dd |S )zInsert a new page with given title.
    
    Args:
        doc (fitz.Document): pdf document object.
        width (float): Page width.
        height (float): Page height.
        title (str): Page title shown in page.
    )rl   rm   gray)   r      )rj   Zfontsize)new_pagerL   Zinsert_text)docrl   rm   rn   pagero   r   r   r   rr      s   
rr   Tc                    s    fdd}|S )a  Plot the returned objects of inner function.
    
    Args:
        title (str): Page title.
        show (bool, optional): Don't plot if show==False. Default to True.
    
    .. note::
        Prerequisite of the inner function: 
            - the first argument is a :py:class:`~pdf2docx.page.BasePage` instance.
            - the last argument is configuration parameters in ``dict`` type.
    c                    s    fdd}|S )Nc                     sx    | i |}| d }| dd}| dd }| dd }r:|r:|r:|d ur:t||j|j}|| || |S )Nr   debugFZ	debug_docZdebug_filename)getrr   rl   rm   Zplotsave)argskwargsobjectsrt   ru   rs   filenameZ
debug_page)r.   showrn   r   r   inner   s   

z*debug_plot.<locals>.wrapper.<locals>.innerr   )r.   r}   r|   rn   r-   r   wrapper   s   zdebug_plot.<locals>.wrapperr   )rn   r|   r   r   r~   r   
debug_plot   s   r   )r   )rC   )r]   )T)enumr   rG   collections.abcr   Z
fitz.utilsr   r   r   r   r   r   r"   r+   r7   r9   r5   r@   rB   strrL   rV   listr\   rf   rk   rr   r   r   r   r   r   <module>   s(   


	 	