
    }ff                     0   d 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ZdZdZ G d d          Z G d de          Z G d de          Z G d de          Z G d d          Z  G 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.
    )
NamedTupleAnyListOptionalUnionSequence)Number   )AlignCharVPosTextDirectionWrapMode)FPDFException)CoreFontTTFFont)escape_parens   ­-     
c                      e Zd ZdZ	 d3deeef         dedede	ee
ef                  fdZd Zed	eeef         fd
            Zej        d             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Z ed             Z!ed             Z"ed             Z#ed             Z$ed             Z%d e
fd!Z&d"e'fd#Z(d$ Z)	 	 	 	 d4d'e
d(e
d)ed*e*fd+Z+d5d-efd.Z,d/ Z-d0 Z.d1 Z/d2 Z0dS )6FragmentzS
    A fragment of text with font/size/style and other associated information.
    N
charactersgraphics_stateklinkc                     t          |t                    rt          |          | _        n|| _        || _        || _        || _        d S N)
isinstancestrlistr   r   r   r   )selfr   r   r   r   s        H/var/www/rtfcompare/venv/lib/python3.11/site-packages/fpdf/line_break.py__init__zFragment.__init__   sH     j#&& 	)":..DOO(DO,			    c           	      H    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$   s    r%   __repr__zFragment.__repr__.   sM    .4? . .#2. .&. .!%. . .	
r'   returnc                     | j         d         S Ncurrent_fontr   r*   s    r%   fontzFragment.font5       ">22r'   c                     || j         d<   d S r.   r0   )r$   vs     r%   r1   zFragment.font9   s    ./N+++r'   c                 0    | j         o| j         j        dk    S )NTTF)r1   typer*   s    r%   is_ttf_fontzFragment.is_ttf_font=   s    y4TY^u44r'   c                     | j         d         S )N
font_styler0   r*   s    r%   r:   zFragment.font_styleA       "<00r'   c                     | j         d         S )Nfont_familyr0   r*   s    r%   r=   zFragment.font_familyE   s    "=11r'   c                 @   | j         d         }| j         d         }|t          j        k    r|| j         d         z  }nb|t          j        k    r|| j         d         z  }nA|t          j        k    r|| j         d         z  }n |t          j        k    r|| j         d         z  }|S )Nfont_size_pt	char_vpos	sub_scale	sup_scale	nom_scaledenom_scaler   r   SUBSUPNOMDENOM)r$   sizevposs      r%   r?   zFragment.font_size_ptI   s    ">2";/8<D'44DDX\!!D'44DDX\!!D'44DDX^##D'66Dr'   c                 ,    | j         d         | j        z  S )Nr?   )r   r   r*   s    r%   	font_sizezFragment.font_sizeW   s    ">2TV;;r'   c                     | j         d         S )Nfont_stretchingr0   r*   s    r%   rO   zFragment.font_stretching[   s    "#455r'   c                     | j         d         S )Nchar_spacingr0   r*   s    r%   rQ   zFragment.char_spacing_   r2   r'   c                     | j         d         S )N	text_moder0   r*   s    r%   rS   zFragment.text_modec       ";//r'   c                     | j         d         S )N	underliner0   r*   s    r%   rV   zFragment.underlineg   rT   r'   c                     | j         d         S )N
draw_colorr0   r*   s    r%   rX   zFragment.draw_colork   r;   r'   c                     | j         d         S )N
fill_colorr0   r*   s    r%   rZ   zFragment.fill_coloro   r;   r'   c                     | j         d         S )N
text_colorr0   r*   s    r%   r\   zFragment.text_colors   r;   r'   c                     | j         d         S )N
line_widthr0   r*   s    r%   r^   zFragment.line_widthw   r;   r'   c                     | j         d         S )Nr@   r0   r*   s    r%   r@   zFragment.char_vpos{   rT   r'   c                 0   | j         d         }|t          j        k    r| j         d         }n\|t          j        k    r| j         d         }n>|t          j        k    r| j         d         }n |t          j        k    r| j         d         }nd}|| j         d         z  S )Nr@   sub_liftsup_liftnom_lift
denom_liftg        r?   rE   )r$   rK   lifts      r%   re   zFragment.lift   s    ";/8<&z2DDX\!!&z2DDX\!!&z2DDX^##&|4DDDd).999r'   c                 6    d                     | j                  S )N )joinr   r*   s    r%   stringzFragment.string   s    wwt'''r'   c                 *    |                                  S r    )	get_widthr*   s    r%   widthzFragment.width   s    ~~r'   c                     | j         d         S )Ntext_shapingr0   r*   s    r%   text_shaping_parametersz Fragment.text_shaping_parameters   r2   r'   c                 B    | j         r| j         d         nt          j        S )Nparagraph_directionro   r   LTRr*   s    r%   rq   zFragment.paragraph_direction   s(     +#D()>??"	
r'   c                 B    | j         r| j         d         nt          j        S )Nfragment_directionrr   r*   s    r%   ru   zFragment.fragment_direction   s(     +#D()=>>"	
r'   indexc                 .    | j         d |         | _         d S r    )r   )r$   rv   s     r%   trimzFragment.trim   s    /&5&1r'   otherc                 b    | j         |j         k    o| j        |j        k    o| j        |j        k    S r    )r   r   r   )r$   ry   s     r%   __eq__zFragment.__eq__   s8    Ou// "#u';;"%'!	
r'   c                 D    t          | j        | j        | j        f          S r    )hashr   r   r   r*   s    r%   __hash__zFragment.__hash__   s    T_d&946BCCCr'   r   Tstartendchars
initial_csc                 $   || j         ||         }| j                            || j        | j                  \  }}| j        }| j        dk    r|| j        dz  z  }|| j        dz  z  }| j        dk    r|r	|||z  z  }n|||dz
  z  z  }|| j        z  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   r1   get_text_widthr?   ro   rQ   rO   r   )r$   r   r   r   r   char_lenwrQ   s           r%   rk   zFragment.get_width   s    $ =OE#I.E	004$d&B
 
1 (3&&%,,AD0477L!!  3\H,,\X\2246zr'   F	characterc                 X    |t           k    r	|st          }|                     ||          S )zP
        Return the width of a single character out of the stored text.
        )r   r   )SOFT_HYPHENHYPHENrk   )r$   r   print_shr   s       r%   get_character_widthzFragment.get_character_width   s/     ##H#I~~I*~EEEr'   c                     | j         r5| j        r|                     ||||          S |                     ||          S |                     ||          S r    )r8   ro   render_with_text_shapingrender_pdf_text_ttfrender_pdf_text_core)r$   frag_ws
current_wsword_spacingadjust_xadjust_yhs          r%   render_pdf_textzFragment.render_pdf_text   sh     	C+ 44h<   ++G\BBB((*===r'   c           	         d}d}| j         D ]B}| j        j                            t	          |                    }|r|t          |          z  }C|r@t          d                    d                              d                    }|	                    t          | j        j                            t	          d                                        }g }	t          |          D ]\  }
}t          |                    d                              d                    }|
dk    r|	                    d| d           Z|| j        z   dz  | j        z  }|	                    |d	d| | d           d                    |	          }|d
| dz  }n>t          |                    d                              d                    }|d| dz  }|S )Nrg   r   	utf-16-belatin-1r   (r)     .3f[z] TJ) Tj)ri   r1   subsetpickordchrr   encodedecodesplit	enumerateappendr   r?   rh   )r$   r   r   retmapped_textcharmapped_charu_spacewords
words_strlword_iwordadjescaped_texts                 r%   r   zFragment.render_pdf_text_ttf   s   K 	0 	0D)*//D		::K 0s;/// 	*#CJJ{$;$;$B$B9$M$MNNG  %%c$)*:*?*?C*I*I&J&JKKEJ )% 0 0 E E$T[[%=%=%D%DY%O%OPPQ;;%%k$kkk2222#df,-4t7HHC%%&C&C&C7&CD&C&C&CDDDD88J//L)|))))CC("";//66yAA L )|))))C
r'   c                     d}d} j         j                            t          d                    } fd} j         j        dz  z   j        z  }	 j                              j         j	         j
                  D ]V}
|
d         t          |
d                                       d                              d          }|
d         d	k    s|
d
         d	k    rc|r|dt          |           dz  }d}| ||
d                   z   }| ||
d
                   z
  }|d| j        z  dd||z
   j        z  ddz  }||z  }| ||
d                   |	z   z  }| ||
d                   z  }|r|
d         |k    r||z  }|
d         s|rG|
d         |k    r;|r|dt          |           dz  }d}|d| j        z  dd||z
   j        z  ddz  }X|r|dt          |           dz  }|S )Nrg   r   c                 ^    | j         j        z  j        z  j        dz  z  dz  j        z  S )Nr   r   )r1   scaler?   rO   r   )posr$   s    r%   
adjust_posz5Fragment.render_with_text_shaping.<locals>.adjust_pos  sJ    )/"#$ '#-/ 	
 &r'   r   r   r   r   x_offsetr   y_offsetr   z) Tj z1 0 0 1 z.2fz Tm 	x_advance	y_advanceforce_positioningr   )r1   r   r   r   rQ   rO   r   
shape_textri   r?   ro   r   r   r   r   )r$   pos_xpos_yr   r   r   textspace_mapped_coder   rQ   tir   offsetxoffsetys   `             r%   r   z!Fragment.render_with_text_shaping  s    I,11#c((;;	 	 	 	 	 (D,@3,FG$&P)&&K*D,H
 
 	X 	XB - (r-())00==DDYOOD*~""bn&9&9 9}T229999CD**R
^"<"<<**R
^"<"<<X461XXXW8NXXXX DLDZZ;00<??EZZ;000E &= 15F F F% %& XX!#M!26G!G!G 9}T229999CDW5DF"2WWW!e)tv9MWWWWW 	10}T**0000C
r'   c                 r    d}||k    r||| j         z  ddz  }t          | j                  }|d| dz  }|S )Nrg   r   z Tw r   r   )r   r   ri   )r$   r   r   r   r   s        r%   r   zFragment.render_pdf_text_coreF  sY    j  g&00000C$T[11%<%%%%
r'   r    )r   NNT)FT)1__name__
__module____qualname____doc__r   r#   r"   dictfloatr   intr&   r+   propertyr   r   r1   setterr8   r:   r=   r?   rM   rO   rQ   rS   rV   rX   rZ   r\   r^   r@   re   ri   rl   ro   rq   ru   rx   r   r{   r~   boolrk   r   r   r   r   r    r'   r%   r   r      s         +/ $)$  	
 uS#X'   
 
 
 3eHg-. 3 3 3 X3 
[0 0 [0 5 5 X5 1 1 X1 2 2 X2   X < < X< 6 6 X6 3 3 X3 0 0 X0 0 0 X0 1 1 X1 1 1 X1 1 1 X1 1 1 X1 0 0 X0 : : X: ( ( X(     X  3 3 X3 
 
 X
 
 
 X
2# 2 2 2 2
C 
 
 
 
D D D
 " "" " 	"
 " " " "HF FS F F F F> > >% % %N0 0 0d    r'   r   c                   n    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dS )TextLine	fragments
text_widthnumber_of_spacesalignheight	max_widthFtrailing_nltrailing_form_feedc                    | j         st                      S g }d }| j         D ]F}|j        |k    r|d                             |           )|                    |g           |j        }G| j         d         j        t
          j        k    s2| 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|z  }.t          |          S )Nr   )r   tupleru   r   rq   r   RTL)r$   directional_runs	directionfragmentordered_fragmentsruns         r%   get_ordered_fragmentszTextLine.get_ordered_fragmentsY  s)   ~ 	77N	 	8 	8H*i77 $++H5555 ''
333$7		>!0M4EEEq!5 Fq!48III/"5# 	 	C V6-:KKKDDbD		QT &'''r'   N)r   r   r   r   __annotations__r   r   r   r   r   r   r   r   r'   r%   r   r   O  s         LLLMMMK$$$$( ( ( ( (r'   r   c                   L    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_indexr^   r   N)r   r   r   r   r   r   r   r'   r%   r   r   q  sZ             !!!!!$$$$"%%%%r'   r   c                   t    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   r^   r   curcharcurchar_widthr   r   N)r   r   r   r   r   r   r"   r   r   r'   r%   r   r   z  s             !!!!!$$$$"%%%%LLLHHHHHr'   r   c                       e Zd ZddedefdZed             Z	 ddeded	e	d
ede
de
dedefdZd Zdeeef         fdZ	 ddededefdZd ZdefdZdS )CurrentLineFr   r   c                 h    || _         || _        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   space_break_hinthyphen_break_hint)r$   r   r   s      r%   r&   zCurrentLine.__init__  s@     # )+ ! !%!%r'   c                 x    d}t          | j                  D ]"\  }}||                    |dk              z  }#|S )Nr   r   )r   r   rk   )r$   rl   ir   s       r%   rl   zCurrentLine.width  sJ    $T^44 	: 	:KAxX''1q5'999EEr'   Nr   character_widthr   r   r   r   r   urlc	                    |t           k    sJ || _        | j        s+| j                            t	          d|||                     nV|| j        d         j        k    s|| j        d         j        k    r*| j                            t	          d|||                     | j        d         }	|t          k    rXt          ||t          | j                  t          |	j
                  | j        | j                  | _        | xj        dz  c_        n|t          k    rt          }| xj        dz  c_        nb|t          k    rW| j        sPt#          ||t          | j                  t          |	j
                  | j        | j        t$          |||
  
        | _        |t          k    s| j        r|	j
                            |           d S d S )Nrg   r   r
   )NEWLINEr   r   r   r   r   r   SPACEr   lenr   rl   r   r   NBSPr   r   r   r   r   )
r$   r   r   r   r   r   r   r   r   active_fragments
             r%   add_characterzCurrentLine.add_character  s    G####~ 
	HN!!(2~q#"F"FGGGG dnR0???DN2&(((N!!(2~q#"F"FGGG.,$-'(DN##O.//
%% %D! !!Q&!!!$I!!Q&!!!+%%dm%%/'(DN##O.//
%& &D" ##t}#&--i88888 $#r'   c                     | j         sd S | j         d         }|j        d         }|dk    rO|                    d           |j        s| j         d= | j         sd S | j         d         }|j        d         }|dk    Md S d S )Nr   r   )r   r   rx   )r$   	last_frag	last_chars      r%   trim_trailing_spacesz CurrentLine.trim_trailing_spaces  s    ~ 	FN2&	(,	3NN2' 'N2&> r*I!,R0I 3r'   
break_hintc                     | 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   rx   r   r   )r$   r  s     r%   _apply_automatic_hintz!CurrentLine._apply_automatic_hint  sU     (P**P(PQ> 	MN2##J$KLLL * ;r'   r   r   r   c           
      b    t          | j        | j        | j        || j        | j        ||          S )N)r   r   r   r   r   r   r   r   )r   r   rl   r   r   r   )r$   r   r   r   s       r%   manual_breakzCurrentLine.manual_break  s>     nz!2;n#1	
 	
 	
 		
r'   c                 &    | j         d up| j        d uS r    )r   r   r*   s    r%   automatic_break_possiblez$CurrentLine.automatic_break_possible
  s    %T1VT5JRV5VVr'   c           	      N   |                                  sJ | j        | j        | j        j        | j        j        k    r|                     | j                   |                     | j        j        | 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^   r	  r  r   r   r   r   r   r   r   r  )r$   r   s     r%   automatic_breakzCurrentLine.automatic_break  s"   ,,.....!-!)%043H3SSS&&t'=>>>&.&4&5&(&>&?   &>&?!!%(( 
 	""4#8999!9!:e$$
 	
r'   )Fr    )FF)r   r   r   r   r   r&   r   rl   r"   r   r   r  r  r   r   r   r	  r   r  r  r  r   r'   r%   r   r     s\       & &% &4 & & & &6   X 79 7979 79 	79
 79 "%79 #&79 79 79 79 79 79r1 1 1	<i6K0L 	< 	< 	< 	< SX
 

)-
KO
 
 
 
W W W
U 
 
 
 
 
 
r'   r   c                       e Zd Zej        dej        ddfdee         de	e
ef         dee         dededed	e
d
efdZd ZdS )MultiLineBreakFg      ?r   r   marginsr   r   wrapmodeline_heightskip_leading_spacesc	                     || _         t                    r| _        n
fd| _        || _        || _        || _        || _        || _        || _        d| _	        d| _
        d| _        dS )a  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.
        c                     S r    r   )r   r   s    r%   <lambda>z)MultiLineBreak.__init__.<locals>.<lambda>P  s    I r'   r   N)r   callablerk   r  r   r   r  r  r  fragment_indexcharacter_indexidx_last_forced_break)	r$   r   r   r  r   r   r  r  r  s	     `      r%   r&   zMultiLineBreak.__init__+  s    B #I 	6&DNN5555DN
  &#6  %)"""r'   c           
         d}| j         }d | _         | j        t          | j                  k    rd S d}|                     |          }t          || j                  }| j        D ]}||z  }| j        r| j        t          | j                  k     r| j	        t          | j        | j                 j
                  k    rd| _	        | xj        dz  c_        b| j        | j                 j
        | j	                 }|t          k    r| xj	        dz  c_	        nn| j        t          | j                  k     | j        t          | j                  k     r| j        | j                 }|j        |k    r2|j        }|                     |          }||_        | j        D ]}||z  }| j	        t          |j
                  k    rd| _	        | xj        dz  c_        |j
        | j	                 }|                    || j        |           }	d}|t          t           fv rv| xj	        dz  c_	        |j        s|| j        z  |_        |                    | j        t*          j        k    rt*          j        n| j        |t          k    |t           k              S |j        |	z   |k    r'|t          k    r*| xj	        dz  c_	        |                    | j                  S | j        t4          j        k    r.|                                 |                    | j                  S |                                r:|                    | j                  \  | _        | _	        }
| xj	        dz  c_	        |
S || j	        k    rt?          d          | j	        | _         |                    | j        t*          j        k    rt*          j        n| j                  S |                     ||	|j!        |j"        | j        | j	        || j        z  |j#                   | xj	        dz  c_	        | j        t          | j                  k     |j        r;|                    | j        t*          j        k    rt*          j        n| j                  S d S )	NTr   )r   r   r
   r   F)r   r   z8Not enough horizontal space to render a single character)$r  r  r   r   rk   r   r   r  r  r  r   r   rM   r   r   r   	FORM_FEEDr  r   r  r   r   JLrl   r  r   CHARr  r  r  r   r  r   r   r   )r$   
first_charr  current_font_heightr   current_linemarginr   current_fragmentr   lines              r%   get_linezMultiLineBreak.get_line\  s   
 $ :%)"#dn"5"5554NN#677	"YOOOl 	  	 FII# 	 %DN(;(;;;'3N4#67B, ,   ,-D(''1,'' N4+>?J(	 %%((A-((( %DN(;(;;; !C$7$777#~d.AB),???&6&@# NN+>??	)2&"l ( (F'II#s+;+F'G'GGG'($##q(##(3D4HII.BB4=^ C  O JWi000$$)$$#- Q*=@P*PL'#00#zUW44EGG$* )W 4'0I'= 1   
 !O3i??%%((A-(('44TZ@@@=HM11 !55777'44TZ@@@88::  
 %44TZ@@	+,((A-((K(D,@@@'R   .2-A*#00#zUW44EGG$*   && / "#$#d&66 %	 	 	   A%  I !C$7$777L  	,,:00dj   tr'   N)r   r   r   r   r   r   WORDr   r   r   r   r  r	   r   r&   r(  r   r'   r%   r  r  *  s         w%] $).* .*H%.* ).* &!	.*
 .* .* .* .* ".* .* .* .*bm m m m mr'   r  N)"r   typingr   r   r   r   r   r   numbersr	   enumsr   r   r   r   errorsr   fontsr   r   utilr   r   r   r   r   r   r  r   r   r   r   r   r  r   r'   r%   <module>r0     s    D C C C C C C C C C C C C C C C       ; ; ; ; ; ; ; ; ; ; ; ; ! ! ! ! ! ! $ $ $ $ $ $ $ $      	
	r r r r r r r rj	( ( ( ( (z ( ( (D    
   
 
 
 
 
 
 
 
`
 `
 `
 `
 `
 `
 `
 `
F_ _ _ _ _ _ _ _ _ _r'   