
    }f%W                     ~   d dl Z d dl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 ddlmZmZ  G d	 d
e          Z G d d          Z edd          Z G d de          Z G d d          Z G d d          Z G d d          Z G d de          Z G d d          Z G d dee          ZdS )    N)
NamedTupleSequenceListNewType   )FPDFException)AlignXPosYPosWrapMode)VectorImageInfo)preload_image)MultiLineBreak	FORM_FEEDc                   $    e Zd ZU eed<   eed<   dS )ExtentsleftrightN)__name__
__module____qualname__float__annotations__     I/var/www/rtfcompare/venv/lib/python3.11/site-packages/fpdf/text_region.pyr   r      s"         
KKKLLLLLr   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )TextRegionMixinz>Mix-in to be added to FPDF() in order to support text regions.c                 b    |                                    t                      j        |i | d S N)clear_text_regionsuper__init__)selfargskwargs	__class__s      r   r#   zTextRegionMixin.__init__   s7       $)&)))))r   c                     || _         d S r    %_TextRegionMixin__current_text_regionr$   regions     r   register_text_regionz$TextRegionMixin.register_text_region   s    %+"""r   c                     | j         |k    S r    r)   r+   s     r   is_current_text_regionz&TextRegionMixin.is_current_text_region   s    )V33r   c                     d | _         d S r    r)   r$   s    r   r!   z!TextRegionMixin.clear_text_region    s    %)"""r   )	r   r   r   __doc__r#   r-   r/   r!   __classcell__r'   s   @r   r   r      sk        HH* * * * *, , ,4 4 4* * * * * * *r   r   	Paragraphc                   D    e Zd ZU dZeed<   eed<   dZeed<   dZ	eed<   dS )LineWrapperzConnects each TextLine with the Paragraph it was written to.
    This allows to access paragraph specific attributes like
    top/bottom margins when rendering the line.
    line	paragraphF
first_line	last_lineN)
r   r   r   r2   r   r   r5   r:   boolr;   r   r   r   r7   r7   (   sS          
 NNNJItr   r7   c            	       t    e Zd Z	 	 	 	 	 	 ddedededefdZd	 Zd
 Zd Z	dde
fdZddZdee         fdZdS )r5   Nr   F
top_marginbottom_marginskip_leading_spaceswrapmodec                    || _         |j        | _        |r\t          j        |          }|t          j        t          j        t          j        t          j        fvrt          d|j	         d          || _
        ||j        | _        n|| _        || _        || _        || _        || j         j        | _        nt!          j        |          | _        g | _        d S )NAText_align must be 'LEFT', 'CENTER', 'RIGHT', or 'JUSTIFY', not ''.)_regionpdfr	   coerceLCRJ
ValueErrorvalue
text_alignline_heightr>   r?   r@   rA   r   _text_fragments)r$   r,   rN   rO   r>   r?   r@   rA   s           r   r#   zParagraph.__init__5   s     : 	j11J%'57EGUW!EEE lXbXhlll   %%1D*D$*#6  L1DMM$OH55DM!r   c                     d| j          d| j         d| j         d| j         d| j         d| j         dt          | j                   dS )	NzParagraph(text_align=z, line_height=z, top_margin=z, bottom_margin=z, skip_leading_spaces=z, wrapmode=z, #text_fragments=))rN   rO   r>   r?   r@   rA   lenrP   r1   s    r   __str__zParagraph.__str__U   s    =DO = =4CS = =bfbq = ="0= =HLH`= =mqmz= = #D$8 9 9= = =	
r   c                     | S r    r   r1   s    r   	__enter__zParagraph.__enter__\   s    r   c                 8    | j                                          d S r    )rE   end_paragraphr$   exc_type	exc_value	tracebacks       r   __exit__zParagraph.__exit___   s    ""$$$$$r   textc                     | j         j        st          d          | j                             |                              dd          }| j                             |d          }|r|D ]	}||_        
| j                            |           d S )N3No font set, you need to call set_font() beforehand Fmarkdown)	rF   font_familyr   normalize_textreplace_preload_font_styleslinkrP   extend)r$   r^   ri   normalized_string	fragmentsfrags         r   writezParagraph.writeb   s    x# 	W UVVV H33D99AA$KKH112Ce1TT	 	!! ! ! 		##I.....r   c                    | j         j        st          d          || j         j        | j        z  }| j                             dd          d         }||j        z  |j        d<   | j        	                    |           d S )Nr`   
Frc   r   font_size_pt)
rF   re   r   	font_sizerO   rh   kgraphics_staterP   append)r$   hfragments      r   lnzParagraph.lnm   s    x# 	W UVVV9"T%55A8000FFqI23hj./##H-----r   returnc           
         g }t          | j        | j        j        | j        j        | j        j        f| j        p| j        j        pt          j        || j	        | j
        | j        p| j        j                  }g | _        |                                }d}|=|                    t          || |                     d}|                                }|=|r*|d         }t          |j        | |j        d          }||d<   |S )N)	max_widthmarginsalignprint_shrA   rO   r@   T)r:   F)r:   r;   )r   rP   rE   	get_widthrF   c_marginrN   r	   rH   rA   rO   r@   get_lineru   r7   r8   r:   )r$   r~   
text_linesmulti_line_break	text_liner:   lasts          r   build_lineszParagraph.build_linesv   s   
) l,X&(9:/GT\%<G]( $ 8 !0|/

 

 

  "$--//	
%k)TjQQQRRRJ(1133I %  	"b>D	4DOt  D "JrNr   )NNr   r   FNr    )r   r   r   r   r<   r   r#   rT   rV   r]   strrn   rx   r   r7   r   r   r   r   r5   r5   4   s          $)!" "
 " " "" " " " "@
 
 
  % % %	/ 	/# 	/ 	/ 	/ 	/. . . .tK'8      r   c                   D    e Zd Z	 	 	 	 	 	 	 	 	 	 d
dededefdZd Zd	 ZdS )ImageParagraphNFr   widthheight
fill_widthc                 p   || _         || _        |rQt          j        |          }|t          j        t          j        t          j        fvrt          d|j         d          || _	        || _
        || _        || _        || _        || _        |	| _        |
| _        || _        || _        d x| _        | _        d S )Nz1Align must be 'LEFT', 'CENTER', or 'RIGHT', not 'rD   )r,   namer	   rG   rH   rI   rJ   rL   rM   r}   r   r   r   keep_aspect_ratior>   r?   ri   titlealt_textimginfo)r$   r,   r   r}   r   r   r   r   r>   r?   ri   r   r   s                r   r#   zImageParagraph.__init__   s     	 	L''EUWeguw777 WWWW   

$!2$*	
 ##4999r   c                 t    t          | j        j        j        | j                  \  | _        | _        | _        | S r    )r   r,   rF   image_cacher   r   r   r1   s    r   
build_linezImageParagraph.build_line   s5     *7KO'*
 *
&	48TY r   c                 (   | j         st          d          t          | j        t                    }| j        r| j        }n| j        d         | j        j        j        z  }| j	        r| j	        }n1| j        d         | j        j        j        z  }||k    s| j
        r|}n|}| j        s||z  |z  }||k    rd S |}	| j        r>| j        t          j        k    r	|	||z
  z  }	n | j        t          j        k    r|	||z
  dz  z  }	|rG| j        j                            | j         | j        |	d ||| j        | j        | j        | j        
  
        S | j        j                            | j        | j         | j        |	d ||| j        | j        | j        d | j                  S )Nz;ImageParagraph.build_line() must be called before render().rv   w   )
svgr   xyr   rv   ri   r   r   r   )r   r   r   r   r   r   rv   ri   r   r   dimsr   )r   RuntimeError
isinstancer   r   r   r,   rF   rs   r   r   r}   r	   rJ   rI   _vector_imageri   r   r   r   _raster_imager   )
r$   col_left	col_width
max_heightis_svgrv   native_hr   native_wr   s
             r   renderzImageParagraph.render   s   x 	M   DI77 ; 	:AAy~(99H: 	
AAy~(99H)##t#{ 	(Hx'Az>>4: 	)zUW$$Y]"uw&&i!mq(( 	;?00HYYj"&"8 1    {,,*]"4 - 
 
 	
r   
NNNFFr   r   NNN)r   r   r   r   r<   r#   r   r   r   r   r   r   r      s        
  !$ !$
 !$ !$ !$ !$ !$ !$F  8
 8
 8
 8
 8
r   r   c            	            e Zd Zddddddddddededed	ef fd
Zd Zd Zd Z	dde
fdZddZ	 	 	 	 	 	 dded	efdZd Z	 	 	 	 	 	 	 	 	 	 ddededefdZ xZS )ParagraphCollectorMixinNLEFTg      ?F)r^   rN   rO   r~   r@   rA   r   img_fill_widthrO   r~   r@   rA   c                   || _         t          j        |          | _        | j        t          j        t          j        t          j        t          j        fvrt          d| j        j	         d          || _
        || _        t          j        |          | _        || _        g | _        d | _         t#                      j        |g|
R i | |r|                     |           |r|                     ||	           d S d S )NrC   rD   )r   )rF   r	   rG   rN   rH   rI   rJ   rK   rL   rM   rO   r~   r   rA   r@   _paragraphs_active_paragraphr"   r#   rn   image)r$   rF   r^   rN   rO   r~   r@   rA   r   r   r%   r&   r'   s               r   r#   z ParagraphCollectorMixin.__init__   s    ,z22?57EGUWeg"FFFmTXTcTimmm   '  11#6 !%.t...v... 	JJt 	7JJs~J66666	7 	7r   c                    | j                             |           rt          d| j        j         d          | j         j        | _        | j                                          d| j         _        | j                             |            | S )NzUnable to enter the same z context recursively.r   )	rF   r/   r   r'   r   page_page_push_local_stackr-   r1   s    r   rV   z!ParagraphCollectorMixin.__enter__  s    8**400 	ZDN,CZZZ   X]
""$$$%%d+++r   c                     | j                                          | j        | j         _        | j                                          |                                  d S r    )rF   r!   r   r   _pop_local_stackr   rY   s       r   r]   z ParagraphCollectorMixin.__exit__&  sF    ""$$$
!!###r   c                     | j         dk    rt          d          | j         ?t          | | j        | j                  }| j                            |           d| _         d S d S )NEXPLICITzaConflicts with active paragraph. Either close the current paragraph or write your text inside it.)r,   rN   r@   AUTOr   r   r5   rN   r@   r   ru   )r$   ps     r   _check_paragraphz(ParagraphCollectorMixin._check_paragraph,  s    !Z//s   !)?$($<  A
 ##A&&&%+D""" *)r   r^   c                 n    |                                   | j        d                             |           d S Nr   )r   r   rn   )r$   r^   ri   s      r   rn   zParagraphCollectorMixin.write:  s6    ""4(((((r   c                 n    |                                   | j        d                             |           d S r   )r   r   rx   )r$   rv   s     r   rx   zParagraphCollectorMixin.ln>  s6    """""r   r   c           	          | j         dk    rt          d          t          | |p| j        ||p| j        |||          }| j                            |           d| _         |S )Nr   Unable to nest paragraphs.)r,   rN   rO   r@   rA   r>   r?   r   )r$   rN   rO   r@   r>   r?   rA   r   s           r   r9   z!ParagraphCollectorMixin.paragraphB  s     !Z// <===!4T_# 3 Ot7O!'
 
 
 	"""!+r   c                 @    | j         st          d          d | _         d S )NzNo active paragraph to end.)r   r   r1   s    r   rX   z%ParagraphCollectorMixin.end_paragraphZ  s*    % 	? =>>>!%r   r   r   r   c                     | j         dk    rt          d          | j         r|                                  t          | |||||||||	|
|          }| j                            |           d S )Nr   r   )
r}   r   r   r   r   r>   r?   ri   r   r   )r   r   rX   r   r   ru   )r$   r   r}   r   r   r   r   r>   r?   ri   r   r   r   s                r   r   zParagraphCollectorMixin.image`  s     !Z// <===! 	!   !/!'
 
 
 	"""""r   r    )NNFr   r   Nr   )r   r   r   r   r<   r   r#   rV   r]   r   r   rn   rx   r9   rX   r   r3   r4   s   @r   r   r      s       
  $)!7 7 7 7 7 "7 7 7 7 7 7 7@	 	 	  , , ,) )# ) ) ) )# # # # $)!  "	    0& & &   #  # 	 #
  #  #  #  #  #  #  #  #  #r   r   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )

TextRegionz3Abstract base class for all text region subclasses.c                     t                      )aZ  
        Return the horizontal extents of the current line.
        Columnar regions simply return the boundaries of the column.
        Regions with non-vertical boundaries need to check how the largest
        font-height in the current line actually fits in there.
        For that reason we include the current y and the line height.
        NotImplementedError)r$   r   r   s      r   current_x_extentszTextRegion.current_x_extents  s     "###r   c                    |j         r4| j        j        | j        j        k    r| j        xj        |j         z  c_        |                     | j        j        d          \  }}| j        j        | j        j        z
  }|| j        j        z
  }|                    |||z
  |          }|r1|j        }|r(| j        j        |z   |k     r| j        xj        |z  c_        |S Nr   )	r>   rF   r   t_marginr   rv   b_marginr   r?   )r$   r9   r   	col_rightbottomr   renderedmargins           r   _render_image_paragraphz"TextRegion._render_image_paragraph  s     	/DHJ1B$B$BHJJ)..JJ"44TXZCC)dh//dhj(
##Hi(.BJOO 	%,F %48:.&88

f$

r   c           	         |sdS || j         _        d}d }d}|D ]}t          |t                    r|                     |          r|dz  }3 ns|j        }d}	|j        D ]}
|
j        rd}	 n|	rM|j        rF|j	        j
        r:| j         j        | j         j        k    r | j         xj        |j	        j
        z  c_        n| j         j        |j        z   |k    r|} n|}|j        }|                     | j         j        d          \  }}| j         j        |k     s| j         j        |k    r|| j         _        | j                             ||j        dt           j        t$          j        d           |j        rB|j	        j        }|r4|	r2| j         j        |z   |k     r| j         xj        |j	        j        z  c_        |dz  }|j        r n|r|d |= |S )Nr   r   FT)rv   bordernew_xnew_yfill)rF   r   r   r   r   r8   rl   
charactersr:   r9   r>   r   r   r   r   _render_styled_text_liner
   r   r   NEXTr;   r?   trailing_form_feed)r$   r   topr   prev_line_heightlast_line_heightrendered_lines
tl_wrapperr   text_renderedrm   r   r   r   s                 r   _render_column_lineszTextRegion._render_column_lines  s8    	1
$ ,	 ,	J*n55 +//
;; "a'NNE&O	 %%/  D (, "
"-
 #,7
 
TX%666HJJ*"6"AAJJJxzI$44v==+;(#3 #,#3 &*&<&<TXZ&K&K#)8:((DHJ),C,C!)DHJ11&)) 2    ' I'1?F I- ITXZ&5HF4R4R

j&:&HH

!#/ E 	,?N?+r   c                 <    |r|                      |||           dS dS )z3Default page rendering a set of lines in one columnN)r   )r$   r   r   r   s       r   _render_lineszTextRegion._render_lines  s2     	?%%j#v>>>>>	? 	?r   c                     g }| j         D ]s}t          |t                    r*|                                }|                    |           A|                    | j                  }|s^|                    |           t|S r    )r   r   r   r   ru   r   r~   rj   )r$   r   r9   r8   	cur_liness        r   collect_lineszTextRegion.collect_lines  s    
) 	- 	-I)^44 - ++--!!$''''%11$-@@	  !!),,,,r   c                     t                      r    r   r1   s    r   r   zTextRegion.render  s    !###r   c                     |                      | j        j        |          \  }}| j        j        |k    r| j        j        |k     r| j        j        }||z
  }|S r    )r   rF   r   r   )r$   r   startendress        r   r   zTextRegion.get_width  sS    ++DHJ??
s8:$(*s"2"2HJEEk
r   N)r   r   r   r2   r   r   r   r   r   r   r   r   r   r   r   r     s        ==$ $ $  6  6  6 p? ? ?
  $ $ $    r   r   c                   0     e Zd ZdZddd fd
Zd Z xZS )TextColumnarMixinz*Enable a TextRegion to perform page breaksN)l_marginr_marginc                     t                      j        |i | ||j        n|| _        | j        }||j        n|| _        |j        | j        z
  }|                     ||           d S r    )r"   r#   r   r   r   _set_left_right)	r$   rF   r   r   r%   r&   r   r   r'   s	           r   r#   zTextColumnarMixin.__init__  st    $)&)))(0(8h}(0(8h%T5)))))r   c                     || j         j        n|}|| j         j        | j         j        z
  n|}||k    r"t	          | j        j         d| d| d          t          ||          | _        d S )Nz(): Right limit (z) lower than left limit (z).)	rF   r   r   r   r   r'   r   r   extents)r$   r   r   s      r   r   z!TextColumnarMixin._set_left_right  s    $(Ltx  d49Mdh///uD==>* I I %I I@DI I I   tU++r   )r   r   r   r2   r#   r   r3   r4   s   @r   r   r     s\        44,04 * * * * * * *, , , , , , ,r   r   c                   X     e Zd Zdddddededef fdZ fd	Zd
 Zd Z	d Z
d Z xZS )TextColumnsr   
   F)ncolsgutterbalancer   r   r   c                    t                      j        |g|R i | d| _        || _        || _        | j        j        | j        j        z
  }||dz
  |z  z
  |z  }| j        j        }	t          |	|	|z             g| _	        t          d|          D ]5}
|	||z   z  }	| j	                            t          |	|	|z                        6t          | j        j        | j        j                  | _        d S )Nr   r   )r"   r#   _cur_column_ncolsr   r   r   r   r   _colsrangeru   maxrF   r   r   _first_page_top)r$   rF   r   r   r   r%   r&   total_wr   c_leftir'   s              r   r#   zTextColumns.__init__	  s    	.t...v...,$t|'88	V33u<	"ffy&899:
q% 	C 	CAi&((FJgffy.@AABBBB"48#4dhjAAr   c                     t                                                       t          | j        j        | j        j                  | _        | j        r(d| _        | j	        | j                 j
        | j        _        | S r   )r"   rV   r  rF   r   r   r  r   r   r   r   r   )r$   r'   s    r   rV   zTextColumns.__enter__   sa    "48#4dhjAA< 	; DD$45:DHJr   c                     | j         r'| j         d                             t                     d S |                     t                     d S r   )r   rn   r   r1   s    r   
new_columnzTextColumns.new_column(  sG     	"R &&y11111JJy!!!!!r   c                    d}| j         j        }| j        r|}|sdS t          d |D                       }|| j        z  }||z
  }	||	k     r]d}||z   }t          j        t          |          | j        z            dz   }
|d         j        j	        |
z  }||k    r||z   }||k    r|}d}t          | j        | j                  D ]}|s dS || j        k    r|| _        |                     dd          \  }}| j         j        |k     s| j         j        |k    r|| j         _        |r'|| j        dz
  k    r| j         j        | j         j        z
  }|                     |||          }|r| j         j        |z   }||k    r|}|r|| j         _        dS dS )z?Rendering a set of lines in one or several columns on one page.FNc              3   .   K   | ]}|j         j        V  d S r    )r8   r   ).0ls     r   	<genexpr>z1TextColumns._render_page_lines.<locals>.<genexpr><  s&      ??qQV]??????r   Tg      ?r   r   )rF   r   r   sumr   mathceilrS   r8   r   r  r   r   r   rv   r   r   )r$   r   r   r   	balancingnext_ypage_bottom
tot_height
col_heightavail_heightlines_per_columnmult_heightcr   r   r   r   s                    r   _render_page_lineszTextColumns._render_page_lines.  s   	< 	& !K ??J?????J#dk1J!C<LL(( 	z)#'9S__t{-J#K#Kc#Q (m07:JJ++ ;.FK''(F %It'55 	# 	#A D$$$#$ "&"8"8A">">HixzH$$
i(?(?%
 8Q4;?33dh&77#88S&QQ #
%556>>"F 	 DHJJJ	  	 r   c                    | j         sd S |                                 }|sd S | j        j        | j        j        z
  }t          | j        j        | j        j                  }|                     |||           |rG| j        	                    d           d| _
        |                     || j        j        |           |Ed S d S )NT)samer   )r   r   rF   rv   r   r  r   r   r  add_pager   )r$   r   r  r  s       r   r   zTextColumns.render_  s     	F''))
 	Fhj48#44dh/<<
O[III 	IH4((( D##J
KHHH  	I 	I 	I 	I 	Ir   c                 4    | j         | j                 \  }}||fS r    )r   r   )r$   r   r   r   r   s        r   r   zTextColumns.current_x_extentsm  s    j!12eU{r   )r   r   r   intr   r<   r#   rV   r	  r  r   r   r3   r4   s   @r   r   r     s        
 B B B 	B
 B B B B B B B.    " " "/  /  / bI I I      r   r   )r  typingr   r   r   r   errorsr   enumsr	   r
   r   r   image_datastructuresr   image_parsingr   
line_breakr   r   r   r   r5   r7   r   r   r   r   r   r   r   r   <module>r'     sc    6 6 6 6 6 6 6 6 6 6 6 6 ! ! ! ! ! ! . . . . . . . . . . . . 1 1 1 1 1 1 ( ( ( ( ( ( 1 1 1 1 1 1 1 1    j   
* * * * * * * *$ GK&&		 	 	 	 	* 	 	 	\ \ \ \ \ \ \ \~d
 d
 d
 d
 d
 d
 d
 d
NF# F# F# F# F# F# F# F#Rl l l l l( l l l^, , , , , , , ,,g g g g g*/ g g g g gr   