U
    g                      @   s   d Z ddlmZ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mZmZ ddlmZmZ dd	lmZ ed
dG dd deZejejjejjdZdZed
dG dd deZG dd deZdS )z-Utilities related to text rendering & layout.    )	dataclassfield)Optional)ConfigurableMixin)ConfigurationError)layout)
PdfContentPdfResourcesResourceType)FontEngineFactorySimpleFontEngineFactory)pdf_nameT)frozenc                       sV   e Zd ZU dZeejdZee	d< dZ
ee	d< dZee e	d< e fdd	Z  ZS )
	TextStylez*Container for basic test styling settings.)default_factoryfont
   	font_sizeNleadingc                    sp   t  | zJ|d }t|tr4|ds<|ds<tdddlm} |||d< W n tk
rj   Y nX d S )Nr   z.otfz.ttfz;'font' must be a path to an OpenType or TrueType font file.r   )GlyphAccumulatorFactory)	superprocess_entries
isinstancestrendswithr   Zpyhanko.pdf_utils.font.opentyper   KeyError)clsZconfig_dictZfcr   	__class__ :/tmp/pip-unpacked-wheel-owvgwkas/pyhanko/pdf_utils/text.pyr   $   s    
zTextStyle.process_entries)__name__
__module____qualname____doc__r   r   r   r   r   __annotations__r   intr   r   classmethodr   __classcell__r   r   r   r    r      s   
r   )Zx_alignZy_alignr   c                   @   s<   e Zd ZU dZdZeed< dZee	j
 ed< dZeed< dS )TextBoxStylez7Extension of :class:`.TextStyle` for use in text boxes.r   border_widthNbox_layout_ruleFvertical_text)r!   r"   r#   r$   r*   r&   r%   r+   r   r   SimpleBoxLayoutRuler,   boolr   r   r   r    r)   @   s
   
	r)   c                       sx   e Zd ZdZdeee eej d fddZ	dd Z
ed	d
 Zedd Zejdd Zedd Zdd Z  ZS )TextBoxzImplementation of a text box that implements the :class:`.PdfContent`
    interface.

    .. note::
        Text boxes currently don't offer automatic word wrapping.
    NF1)style	resourcesboxc                    sN   t  j|||d || _d | _d  | _| _|| _|j|| _	d | _
| _d S )N)writerr3   r   )r   __init__r1   _content_content_lines_wrapped_lines	font_namer   Zcreate_font_enginefont_engine_nat_text_height_nat_text_width)selfr1   r4   r2   r3   r9   r   r   r    r5   `   s    zTextBox.__init__c                 C   s   | j }||}|j| jj }|j| jj }|j}|jr| }| }| jj}	| j	}
|	rh||
8 }t
|}n||
8 }t
|}|d||f 7 }n|d7 }t
|}||fS )Ns	    %g %g Tds    T*)r:   shape	x_advancer1   r   	y_advanceZgraphics_opsZuses_complex_positioningr,   r   abs)r=   txtr:   Zshape_resultr?   r@   opsZ	newline_xZ	newline_yverticalr   extentr   r   r    put_string_linep   s&    

zTextBox.put_string_linec                 C   s   | j S )zZ
        :return:
            Text content of the text box, broken up into lines.
        )r7   r=   r   r   r    content_lines   s    zTextBox.content_linesc                 C   s   | j S )z
        :return:
            The actual text content of the text box.
            This is a modifiable property.

            In textboxes that don't have a fixed size, setting this property
            can cause the text box to be resized.
        )r6   rG   r   r   r    content   s    
zTextBox.contentc                 C   s   || _ d}d}| j}g }| jj}|dD ]R}| |\}}	tt|	}
|r`t||
}||7 }nt||
}||7 }|	| q*|| _
|d| _|| _|| _d S )Nr   
)r6   r   r1   r,   splitrF   r&   roundmaxappendr8   r7   r<   r;   )r=   rI   Znatural_text_widthZnatural_text_heightr   linesrD   lineZwrapped_linerE   Zrounded_extentr   r   r    rI      s&    


c                 C   s   | j }|jdkr|jS |jS )z
        :return:
            The effective leading value, i.e. the
            :attr:`~.TextStyle.leading` attribute of the associated
            :class:`.TextBoxStyle`, or :attr:`~.TextStyle.font_size` if
            not specified.
        N)r1   r   r   )r=   r1   r   r   r    r      s    	zTextBox.leadingc                 C   s0  | j }| jtjtd| j | j d | j}| j	}| j
}| j j}| j j}|d krhtjt}t|}|| j||}g }	|jr|	d|j| jj| jjf  |	|  |	dd| jd|j|f g7 }	|rd||j |d  ||j f }
nd	||j |  }
|	|
 |	| j |	d
 d |	S )N/)categorynamevalues   q %g w 0 0 %g %g re S Qs   BTs   /%s %d Tf %d TLlatin1s   %g %g Td   s   0 %g Tds   ET    )!r1   Zset_resourcer
   ZFONTr   r9   r:   Zas_resourcer   r<   r;   r,   r+   r   ZMarginsuniformDEFAULT_TEXT_BOX_MARGINDEFAULT_BOX_LAYOUTZsubstitute_marginsZfitr3   r*   rN   widthheightZas_cmencoder   Zx_scaleZy_scaleextendr8   join)r=   r1   r   Znat_text_widthZnat_text_heightrD   Z
box_layoutZmarginsZpositioningZcommand_streamZtext_cursor_startr   r   r    render   sR    
	

zTextBox.render)NNr0   )r!   r"   r#   r$   r)   r   r	   r   ZBoxConstraintsr5   rF   propertyrH   rI   setterr   r`   r(   r   r   r   r    r/   X   s&      



r/   N)r$   Zdataclassesr   r   typingr   Zpyhanko.config.apir   Zpyhanko.config.errorsr   Zpyhanko.pdf_utilsr   Zpyhanko.pdf_utils.contentr   r	   r
   Zpyhanko.pdf_utils.fontr   r   Zpyhanko.pdf_utils.genericr   r   r-   ZAxisAlignmentZ	ALIGN_MIDrZ   rY   r)   r/   r   r   r   r    <module>   s$   )