o
    vh                      @   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                       s\   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                    sn   t  | z&|d }t|tr|ds|dstdddlm} |||d< W d S  ty6   Y d S w )Nr   z.otfz.ttfz;'font' must be a path to an OpenType or TrueType font file.r   )GlyphAccumulatorFactory)	superprocess_entries
isinstancestrendswithr   pyhanko.pdf_utils.font.opentyper   KeyError)clsconfig_dictfcr   	__class__ T/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/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   )x_align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(   r0   r   r   SimpleBoxLayoutRuler1   boolr"   r"   r"   r#   r.   @   s   
 r.   c                       s   e Zd ZdZ			ddedee deej f 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)writerr8   r   )r   __init__r6   _content_content_lines_wrapped_lines	font_namer   create_font_enginefont_engine_nat_text_height_nat_text_width)selfr6   r9   r7   r8   r>   r    r"   r#   r:   `   s   zTextBox.__init__c                 C   s   | j }||}|j| jj }|j| jj }|j}|jrH| }| }| jj}	| j	}
|	r4||
8 }t
|}n||
8 }t
|}|d||f 7 }||fS |d7 }t
|}||fS )Ns	    %g %g Tds    T*)r@   shape	x_advancer6   r   	y_advancegraphics_opsuses_complex_positioningr1   r   abs)rC   txtr@   shape_resultrE   rF   ops	newline_x	newline_yverticalr   extentr"   r"   r#   put_string_linep   s(   

zTextBox.put_string_linec                 C      | j S )zZ
        :return:
            Text content of the text box, broken up into lines.
        )r<   rC   r"   r"   r#   content_lines   s   zTextBox.content_linesc                 C   rR   )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.
        )r;   rS   r"   r"   r#   content   s   
zTextBox.contentc                 C   s   || _ d}d}| j}g }| jj}|dD ])}| |\}}	tt|	}
|r0t||
}||7 }n	t||
}||7 }|	| q|| _
|d| _|| _|| _d S )Nr   
)r;   r   r6   r1   splitrQ   r)   roundmaxappendr=   r<   rB   rA   )rC   rU   natural_text_widthnatural_text_heightr   linesrO   linewrapped_linerP   rounded_extentr"   r"   r#   rU      s&   



c                 C   s   | j }|jdu 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)r6   r   r   )rC   r6   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 u r4tjt}t|}|| j||}g }	|jrP|	d|j| jj| jjf  |	|  |	dd| jd|j|f g7 }	|rzd||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    )!r6   set_resourcer
   FONTr   r>   r@   as_resourcer   rB   rA   r1   r0   r   MarginsuniformDEFAULT_TEXT_BOX_MARGINDEFAULT_BOX_LAYOUTsubstitute_marginsfitr8   r/   rZ   widthheightas_cmencoder   x_scaley_scaleextendr=   join)rC   r6   r   nat_text_widthnat_text_heightrO   
box_layoutmarginspositioningcommand_streamtext_cursor_startr"   r"   r#   render   sR   
	


zTextBox.render)NNr5   )r$   r%   r&   r'   r.   r   r	   r   BoxConstraintsr:   rQ   propertyrT   rU   setterr   r   r+   r"   r"   r    r#   r4   X   s,    



r4   N)r'   dataclassesr   r   typingr   pyhanko.config.apir   pyhanko.config.errorsr   pyhanko.pdf_utilsr   pyhanko.pdf_utils.contentr   r	   r
   pyhanko.pdf_utils.fontr   r   pyhanko.pdf_utils.genericr   r   r2   AxisAlignment	ALIGN_MIDrn   rm   r.   r4   r"   r"   r"   r#   <module>   s&    )