U
    :vhqv                     @   s.  d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
mZ ddlmZmZmZmZ ddlmZ ddlmZmZ dd	lmZ d
ZdZdZdddddddddddddddgZdeZdZdZdZ G dd  d 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-S ).a%  
Routines for organizing lines and larger blocks of text, with manual and
automatic line wrapping.

The contents of this module are internal to fpdf2, and not part of the public API.
They may change at any time without prior warning or any deprecation period,
in non-backward-compatible ways.
    )Number)
NamedTupleAnyListOptionalUnionSequence)uuid4   )AlignCharVPosTextDirectionWrapMode)FPDFException)CoreFontTTFFont)escape_parens   ­- u   ​u    u    u    u    u    u    u    u    u    u    u    u   　	     
c                   @   s  e Zd ZdZdQeeef eee	ee
ef  dddZdd Zeeeef dd	d
Zejdd
 Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Z ed*d+ Z!ed,d- Z"ed.d/ Z#ed0d1 Z$ed2d3 Z%ed4d5 Z&e
d6d7d8Z'e(d9d:d;Z)d<d= Z*dRe
e
ee+d@dAdBZ,d d9dCdDZ-dSedFdGdHZ.dIdJ Z/dKdL Z0dMdN Z1dOdP Z2dS )TFragmentzS
    A fragment of text with font/size/style and other associated information.
    N
charactersgraphics_stateklinkc                 C   s2   t |trt|| _n|| _|| _|| _|| _d S N)
isinstancestrlistr   r   r   r    )selfr   r   r   r     r&   3/tmp/pip-unpacked-wheel-dvf6lv8i/fpdf/line_break.py__init__2   s    
zFragment.__init__c              	   C   s&   d| j  d| j d| j d| j d	S )NzFragment(characters=z, graphics_state=z, k=z, link=)r   r%   r&   r&   r'   __repr__A   s    $zFragment.__repr__)returnc                 C   s
   | j d S NZcurrent_fontr   r*   r&   r&   r'   fontH   s    zFragment.fontc                 C   s   || j d< d S r-   r.   )r%   vr&   r&   r'   r/   L   s    c                 C   s   | j o| j jdkS )NZTTF)r/   typer*   r&   r&   r'   is_ttf_fontP   s    zFragment.is_ttf_fontc                 C   s
   | j d S )N
font_styler.   r*   r&   r&   r'   r3   T   s    zFragment.font_stylec                 C   s
   | j d S )Nfont_familyr.   r*   r&   r&   r'   r4   X   s    zFragment.font_familyc                 C   s~   | j d }| j d }|tjkr.|| j d 9 }nL|tjkrH|| j d 9 }n2|tjkrb|| j d 9 }n|tjkrz|| j d 9 }|S )Nfont_size_pt	char_vposZ	sub_scaleZ	sup_scaleZ	nom_scaleZdenom_scaler   r   ZSUBZSUPZNOMZDENOM)r%   sizevposr&   r&   r'   r5   \   s    





zFragment.font_size_ptc                 C   s   | j d | j S )Nr5   )r   r   r*   r&   r&   r'   	font_sizej   s    zFragment.font_sizec                 C   s
   | j d S )Nfont_stretchingr.   r*   r&   r&   r'   r;   n   s    zFragment.font_stretchingc                 C   s
   | j d S )Nchar_spacingr.   r*   r&   r&   r'   r<   r   s    zFragment.char_spacingc                 C   s
   | j d S )N	text_moder.   r*   r&   r&   r'   r=   v   s    zFragment.text_modec                 C   s
   | j d S )N	underliner.   r*   r&   r&   r'   r>   z   s    zFragment.underlinec                 C   s
   | j d S )Nstrikethroughr.   r*   r&   r&   r'   r?   ~   s    zFragment.strikethroughc                 C   s
   | j d S )N
draw_colorr.   r*   r&   r&   r'   r@      s    zFragment.draw_colorc                 C   s
   | j d S )N
fill_colorr.   r*   r&   r&   r'   rA      s    zFragment.fill_colorc                 C   s
   | j d S )N
text_colorr.   r*   r&   r&   r'   rB      s    zFragment.text_colorc                 C   s
   | j d S )N
line_widthr.   r*   r&   r&   r'   rC      s    zFragment.line_widthc                 C   s
   | j d S )Nr6   r.   r*   r&   r&   r'   r6      s    zFragment.char_vposc                 C   st   | j d }|tjkr | j d }nF|tjkr6| j d }n0|tjkrL| j d }n|tjkrb| j d }nd}|| j d  S )Nr6   Zsub_liftZsup_liftZnom_liftZ
denom_liftg        r5   r7   )r%   r9   liftr&   r&   r'   rD      s    




zFragment.liftc                 C   s   d | jS )Nr   )joinr   r*   r&   r&   r'   string   s    zFragment.stringc                 C   s   |   S r!   )	get_widthr*   r&   r&   r'   width   s    zFragment.widthc                 C   s
   | j d S )NZtext_shapingr.   r*   r&   r&   r'   text_shaping_parameters   s    z Fragment.text_shaping_parametersc                 C   s   | j r| j d S tjS )Nparagraph_directionrI   r   ZLTRr*   r&   r&   r'   rJ      s    zFragment.paragraph_directionc                 C   s   | j r| j d S tjS )Nfragment_directionrK   r*   r&   r&   r'   rL      s    zFragment.fragment_direction)indexc                 C   s   | j d | | _ d S r!   )r   )r%   rM   r&   r&   r'   trim   s    zFragment.trim)otherc                 C   s$   | j |j ko"| j|jko"| j|jkS r!   )r   r   r   r%   rO   r&   r&   r'   __eq__   s
    

zFragment.__eq__c                 C   s   t | j| j| jfS r!   )hashr   r   r   r*   r&   r&   r'   __hash__   s    zFragment.__hash__r   T)startendchars
initial_csc                 C   s   |dkr| j || }| j|| j| j\}}| j}| jdkrZ|| jd 9 }|| jd 9 }| jdkr|rv||| 7 }n|||d  7 }|| j S )a  
        Return the width of the string with the given font/size/style/etc.

        Args:
            start (int): Index of the start character. Default start of fragment.
            end (int): Index of the end character. Default end of fragment.
            chars (str): Specific text to get the width for (not necessarily the
                same as the contents of the fragment). If given, this takes
                precedence over the start/end arguments.
        Nd   g{Gz?r   r
   )r   r/   Zget_text_widthr5   rI   r<   r;   r   )r%   rT   rU   rV   rW   char_lenwr<   r&   r&   r'   rG      s       

zFragment.get_widthc                 C   s$   | j |j ko"| j|jko"t|| jS )zFReturns if 2 fragments are equivalent other than the characters/string)r   r   r"   	__class__rP   r&   r&   r'   has_same_style   s
    

zFragment.has_same_styleF)	characterc                 C   s   |t kr|st}| j||dS )zP
        Return the width of a single character out of the stored text.
        )rV   rW   )SOFT_HYPHENHYPHENrG   )r%   r]   print_shrW   r&   r&   r'   get_character_width   s    zFragment.get_character_widthc                 C   s4   | j r(| jr| ||||S | ||S | ||S r!   )r2   rI   render_with_text_shapingrender_pdf_text_ttfrender_pdf_text_core)r%   frag_ws
current_wsword_spacingZadjust_xZadjust_yhr&   r&   r'   render_pdf_text  s       zFragment.render_pdf_textc                 C   s&  d}d}| j D ]&}| jjt|}|r|t|7 }q|rtddd}|	t| jjtd}g }	t
|D ]j\}
}t|dd}|
dkr|	d| d qv|| j  d | j }|	|d	d| | d qvd|	}|d
| d7 }n$t|dd}|d| d7 }|S )Nr   r   	utf-16-belatin-1r   (r)     .3f[z] TJ) Tj)rF   r/   subsetpickordchrr   encodedecodesplit	enumerateappendr   r5   rE   )r%   re   rg   retZmapped_textcharmapped_charZu_spacewordsZ
words_strlZword_iwordZadjescaped_textr&   r&   r'   rc     s.    

zFragment.render_pdf_text_ttfc                    s  d}d} j jtd} fdd} j jd   j }	 j  j j	 j
D ]R}
|
d d krfqRt|
d dd}|
d	 d
ks|
d d
kr|r|dt| d7 }d}|||
d	  }|||
d  }|d| j dd||  j dd7 }||7 }|||
d |	 7 }|||
d 7 }|rD|
d |krD||7 }|
d s^|rR|
d |krR|r||dt| d7 }d}|d| j dd||  j dd7 }qR|r|dt| d7 }|S )Nr   r   c                    s&   |  j j  j  jd  d  j S )NrX   rm   )r/   Zscaler5   r;   r   )posr*   r&   r'   
adjust_pos8  s    z5Fragment.render_with_text_shaping.<locals>.adjust_posrX   r|   rj   rk   Zx_offsetr   Zy_offsetrl   z) Tj z1 0 0 1 z.2fz Tm Z	x_advanceZ	y_advanceZforce_positioningrp   )r/   rq   rr   rs   r<   r;   r   Z
shape_textrF   r5   rI   rt   ru   rv   r   )r%   Zpos_xZpos_yrh   rg   rz   textZspace_mapped_coder   r<   Ztir{   ZoffsetxZoffsetyr&   r*   r'   rb   3  sN    
  
$

,z!Fragment.render_with_text_shapingc                 C   s@   d}||kr"||| j  dd7 }t| j}|d| d7 }|S )Nr   rn   z Tw rl   rp   )r   r   rF   )r%   re   rf   rz   r   r&   r&   r'   rd   e  s    
zFragment.render_pdf_text_core)N)r   NNT)FT)3__name__
__module____qualname____doc__r   r$   r#   dictfloatr   intr(   r+   propertyr   r   r/   setterr2   r3   r4   r5   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   r6   rD   rF   rH   rI   rJ   rL   rN   r   rQ   rS   boolrG   r\   ra   ri   rc   rb   rd   r&   r&   r&   r'   r   -   s   	 






















    $		'2r   c                       sB   e Zd ZdZ fddZdd Zdd Zed fd	d
Z  Z	S )TotalPagesSubstitutionFragmenta`  
    A special type of text fragment that represents a placeholder for the total number of pages
    in a PDF document.

    A placeholder will be generated during the initial content rendering phase of a PDF document.
    This placeholder is later replaced by the total number of pages in the document when the final
    output is being produced.
    c                    s   t  j|| t | _d S r!   )superr(   r	   uuidr%   argskwargsr[   r&   r'   r(   x  s    z'TotalPagesSubstitutionFragment.__init__c                 C   s   d| j  dS )z
        This method returns a placeholder string containing a universally unique identifier (UUID4),
        ensuring that the placeholder is distinct and does not conflict with other placeholders
        within the document.
        z::placeholder:z::)r   r*   r&   r&   r'   get_placeholder_string|  s    z5TotalPagesSubstitutionFragment.get_placeholder_stringc                 O   s   || _ || _|  S )ac  
        This method is invoked during the page content rendering phase, which is common to all
        `Fragment` instances. It stores the provided arguments and keyword arguments to preserve
        the necessary information and graphic state for the final substitution rendering.

        The method then returns the unique placeholder string.
        )_render_args_render_kwargsr   r   r&   r&   r'   ri     s    z.TotalPagesSubstitutionFragment.render_pdf_text)replacement_textc                    s   t || _t j| j| jS )a  
        This method is invoked at the output phase. It calls `render_pdf_text()` from the superclass
        to render the fragment with the preserved rendering state (stored in `_render_args` and `_render_kwargs`)
        and insert the final text in place of the placeholder.
        )r$   r   r   ri   r   r   )r%   r   r   r&   r'   render_text_substitution  s    
z7TotalPagesSubstitutionFragment.render_text_substitution)
r   r   r   r   r(   r   ri   r#   r   __classcell__r&   r&   r   r'   r   n  s
   	r   c                   @   sj   e Zd ZU eed< eed< eed< eed< eed< eed< dZe	ed< dZ
e	ed	< d
Zeed< dd ZdS )TextLine	fragments
text_widthnumber_of_spacesalignheight	max_widthFtrailing_nltrailing_form_feedr   indentc                 C   s   | j st S g }d }| j D ]0}|j|kr8|d | q||g |j}q| j d jtjks|| j d js| j d jtjkr|d d d }g }|D ]*}||d jtjkr|d d d n|7 }qt|S )Nr   )r   tuplerL   ry   rJ   r   ZRTL)r%   Zdirectional_runs	directionfragmentZordered_fragmentsrunr&   r&   r'   get_ordered_fragments  s*    


 zTextLine.get_ordered_fragmentsN)r   r   r   r   __annotations__r   r   r   r   r   r   r   r   r&   r&   r&   r'   r     s   
r   c                   @   s>   e Zd ZU eed< eed< eed< eed< eed< eed< dS )	SpaceHintoriginal_fragment_indexoriginal_character_indexcurrent_line_fragment_indexcurrent_line_character_indexrC   r   N)r   r   r   r   r   r   r&   r&   r&   r'   r     s   
r   c                   @   s^   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eed< eed	< eed
< dS )
HyphenHintr   r   r   r   rC   r   curcharcurchar_widthr   r   N)r   r   r   r   r   r   r#   r   r&   r&   r&   r'   r     s   
r   c                	   @   s   e Zd ZdeeedddZedd Zdeee	e
e
eed	d
dZdd Zeeef dddZdeeedddZdd ZedddZdS )CurrentLineFr   r   r`   r   c                 C   s4   || _ || _|| _g | _d| _d| _d| _d| _dS )z
        Per-line text fragment management for use by MultiLineBreak.
            Args:
                print_sh (bool): If true, a soft-hyphen will be rendered
                    normally, instead of triggering a line break. Default: False
        r   N)r   r`   r   r   r   r   space_break_hinthyphen_break_hint)r%   r   r`   r   r&   r&   r'   r(     s    zCurrentLine.__init__c                 C   s0   d}t | jD ]\}}||j|dkd7 }q|S )Nr   rW   )rx   r   rG   )r%   rH   ir   r&   r&   r'   rH     s    zCurrentLine.widthN)r]   character_widthoriginal_fragmentr   r   r   urlc           	      C   s6  |t kst|| _| js8| j|jd|j|j|d n8t|t	rp|
| jd sp| j|jd|j|j|d | jd }|tkrt||t| jt|j| j| j| _|  jd7  _n^|tkrt}|  jd7  _nB|tkr| jst||t| jt|j| j| jt||j|j
| _|tks&| jr2|j| d S )Nr   r   r   r
   )NEWLINEAssertionErrorr   r   ry   r[   r   r   r"   r   r\   BREAKING_SPACE_SYMBOLS_STRr   lenr   rH   r   r   NBSPSPACEr^   r`   r   r_   r   )	r%   r]   r   r   r   r   r   r   Zactive_fragmentr&   r&   r'   add_character  sl    
 
zCurrentLine.add_characterc                 C   sb   | j s
d S | j d }|jd }|dkr^|d |js>| j d= | j sHd S | j d }|jd }qd S )Nr   r   )r   r   rN   )r%   Z	last_frag	last_charr&   r&   r'   trim_trailing_spaces<  s    



z CurrentLine.trim_trailing_spaces)
break_hintc                 C   s6   | j d|j | _ | j r*| j d |j |j| _dS )z
        This function mutates the current_line, applying one of the states
        observed in the past and stored in
        `hyphen_break_hint` or `space_break_hint` attributes.
        Nr   )r   r   rN   r   r   )r%   r   r&   r&   r'   _apply_automatic_hintJ  s    z!CurrentLine._apply_automatic_hint)r   r   r   c                 C   s,   t | j| j| j|| j| j| j ||| jd	S )N)	r   r   r   r   r   r   r   r   r   )r   r   rH   r   r   r   r   )r%   r   r   r   r&   r&   r'   manual_breakU  s    
zCurrentLine.manual_breakc                 C   s   | j d k	p| jd k	S r!   )r   r   r*   r&   r&   r'   automatic_break_possibled  s    z$CurrentLine.automatic_break_possible)r   c                 C   s   |   st| jd k	r|| jd ks0| jj| jjkr|| | j | | jj| jj| j| jj	| jj
| j | jj	| jj
| |fS | | j | jj	| jj
| |fS r!   )r   r   r   r   rC   r   r   r   r   r   r   r   r   )r%   r   r&   r&   r'   automatic_breakg  s0    
	zCurrentLine.automatic_break)Fr   )N)FF)r   r   r   r   r   r(   r   rH   r#   r   r   r   r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   r     s0   
 E     r   c                   @   sT   e Zd Zejdejdddfee e	e
ef ee eeee
ee
d	ddZdd Zd	S )
MultiLineBreakFg      ?r   )	r   r   marginsr   r`   wrapmodeline_heightskip_leading_spacesfirst_line_indentc
           
         sj   || _ t r | _n fdd| _|| _|| _|| _|| _|| _|| _d| _	d| _
d| _|	| _d| _dS )a6  Accept text as Fragments, to be split into individual lines depending
        on line width and text height.
        Args:
            fragments: A sequence of Fragment()s containing text.
            max_width: Either a fixed width as float or a callback function
                get_width(height). If a function, it gets called with the largest
                height encountered on the current line, and must return the
                applicable width for the line with the given height at the current
                vertical position. The height is relevant in those cases where the
                lateral boundaries of the enclosing TextRegion() are not vertical.
            margins (sequence of floats): The extra clearance that may apply at the beginning
                and/or end of a line (usually either FPDF.c_margin or 0.0 for each side).
            align (Align): The horizontal alignment of the current text block.
            print_sh (bool): If True, a soft-hyphen will be rendered
                normally, instead of triggering a line break. Default: False
            wrapmode (WrapMode): Selects word or character based wrapping.
            line_height (float, optional): A multiplier relative to the font
                size changing the vertical space occupied by a line of text. Default 1.0.
            skip_leading_spaces (bool, optional): On each line, any space characters
                at the beginning will be skipped. Default value: False.
            first_line_indent (float, optional): left spacing before first line of text in paragraph.
        c                    s    S r!   r&   )r   r   r&   r'   <lambda>      z)MultiLineBreak.__init__.<locals>.<lambda>r   NT)r   callablerG   r   r   r`   r   r   r   fragment_indexcharacter_indexidx_last_forced_breakr   _is_first_line)
r%   r   r   r   r   r`   r   r   r   r   r&   r   r'   r(     s    #zMultiLineBreak.__init__c              	   C   s,  d}| j }d | _ | jt| jkr$d S d}| |}t|| j| jrF| jndd}| j	D ]}||8 }qT| jrr|| j8 }| j
r| jt| jk r| jt| j| j jkrd| _|  jd7  _qx| j| j j| j }|tkr|  jd7  _qxqqx| jt| jk r| j| j }|j|krN|j}| |}||_| j	D ]}||8 }q,| jrN|| j8 }| jt|jkrvd| _|  jd7  _q|j| j }|j|| j| d}	d}|ttfkr|  jd7  _|js|| j |_d| _|j| jtjkrtjn| j|tk|tkdS |j|	 |krd| _|tkr8|  jd7  _|| jS | jtjkrZ|  || jS |  r|!| j\| _| _}
|  jd7  _|
S || jkrt"d| j| _ || jtjkrtjn| jS |#||	|| j| j|| j |j$ |  jd7  _q|jr(d| _|| jtjkr tjn| jS d S )	NTr   r   r
   r   F)r   r   z8Not enough horizontal space to render a single character)%r   r   r   r   rG   r   r`   r   r   r   r   r   r   r   r:   r   ra   r   	FORM_FEEDr   r   r   r   r   JLrH   r   r   r   ZCHARr   r   r   r   r   r    )r%   
first_charr   Zcurrent_font_heightr   current_linemarginr]   Zcurrent_fragmentr   liner&   r&   r'   get_line  s    






  


zMultiLineBreak.get_lineN)r   r   r   r   r   r   ZWORDr   r   r   r   r   r   r   r(   r   r&   r&   r&   r'   r     s$   
5r   N)(r   Znumbersr   typingr   r   r   r   r   r   r   r	   enumsr   r   r   r   errorsr   Zfontsr   r   utilr   r^   r_   r   ZBREAKING_SPACE_SYMBOLSrE   r   r   r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   <module>   sP   	 
  C,#	 1