U
    c                     @   s   d Z ddlmZmZ ddlmZ ddlmZ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)layout)ConfigurableMixinConfigurationError)
PdfContentPdfResourcesResourceType)FontEngineFactorySimpleFontEngineFactory)pdf_nameT)frozenc                       sR   e Zd ZU dZeejdZee	d< dZ
ee	d< dZ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-0kb_yl26/pyhanko/pdf_utils/text.pyr   !   s    
zTextStyle.process_entries)__name__
__module____qualname____doc__r   r   r   r   r
   __annotations__r   intr   classmethodr   __classcell__r   r   r   r   r      s   


r   )Zx_alignZy_alignr   c                   @   s8   e Zd ZU dZdZeed< dZ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   SimpleBoxLayoutRuler+   boolr   r   r   r   r(   ;   s
   
	r(   c                       sp   e Zd ZdZd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)writerr2   r   )r   __init__r0   _content_content_lines_wrapped_lines	font_namer   Zcreate_font_enginefont_engine_nat_text_height_nat_text_width)selfr0   r3   r1   r2   r8   r   r   r   r4   [   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*)r9   shape	x_advancer0   r   	y_advanceZgraphics_opsZuses_complex_positioningr+   r   abs)r<   txtr9   Zshape_resultr>   r?   opsZ	newline_xZ	newline_yverticalr   extentr   r   r   put_string_lineg   s&    

zTextBox.put_string_linec                 C   s   | j S )zZ
        :return:
            Text content of the text box, broken up into lines.
        )r6   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.
        )r5   rF   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   
)r5   r   r0   r+   splitrE   r%   roundmaxappendr7   r6   r;   r:   )r<   rH   Znatural_text_widthZnatural_text_heightr   linesrC   lineZwrapped_linerD   Zrounded_extentr   r   r   rH      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)r0   r   r   )r<   r0   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    )!r0   Zset_resourcer	   ZFONTr   r8   r9   Zas_resourcer   r;   r:   r+   r*   r   ZMarginsuniformDEFAULT_TEXT_BOX_MARGINDEFAULT_BOX_LAYOUTZsubstitute_marginsZfitr2   r)   rM   widthheightZas_cmencoder   Zx_scaleZy_scaleextendr7   join)r<   r0   r   Znat_text_widthZnat_text_heightrC   Z
box_layoutZmarginsZpositioningZcommand_streamZtext_cursor_startr   r   r   render   sZ     
  
 

zTextBox.render)NNr/   )r    r!   r"   r#   r(   r   r   ZBoxConstraintsr4   rE   propertyrG   rH   setterr   r_   r'   r   r   r   r   r.   S   s$      



r.   N)r#   Zdataclassesr   r   Zpyhanko.pdf_utilsr   Zpyhanko.pdf_utils.config_utilsr   r   Zpyhanko.pdf_utils.contentr   r   r	   Zpyhanko.pdf_utils.fontr
   r   Zpyhanko.pdf_utils.genericr   r   r,   ZAxisAlignmentZ	ALIGN_MIDrY   rX   r(   r.   r   r   r   r   <module>   s    &