
    }f.X                         d Z dZdZdZddlZddlZddlZddlmZ ddl	m
Z
 dd	lmZ d
 Zd Z G d d          Z G d de          ZdS )z PDF Template Helpers for fpdf.pyz%Mariano Reingart <reingart@gmail.com>z#Copyright (C) 2010 Mariano ReingartzLGPL 3.0    N   )get_stack_level)FPDFException)FPDFc                 $    | dz  | dz  dz  | dz  fS )Ni       )cols    F/var/www/rtfcompare/venv/lib/python3.11/site-packages/fpdf/template.py_rgbr      s     5LC3J,c	::    c                     t          |           \  }}}|dk    r|dk    r|dk    s|dk    r	|dz  ddS |dz  dd|dz  dd|dz  ddS )Nr      z.3fz g z rg)r   )r
   rgbs       r   _rgb_as_strr      s~    3iiGAq!	Q166a1ffbc'!!!!!#g:::AG:::!c':::::r   c                   `   e Zd ZdZd(dZd Zed             Zed             Zd)d	Z	d
 Z
e
Zd Zd Zd ZdddddddddddddddddZdddddddddZddddddddddZddddddddddZdddddddZdddddddddd	d Zddddd!ddddddd"d#Zdddddddddddddd$d%Zd*d'ZdS )+FlexTemplatez
    A flexible templating class.

    Allows to apply one or several template definitions to any page of
    a document in any combination.
    Nc           	         t          |t                    st          d          || _        d| _        |r|                     |           | j        | j        | j        | j	        | j
        | j        | j        | j        d| _        i | _        dS )a=  
        Arguments: pdf (fpdf.FPDF() instance): All content will be added to this object.

            elements (list of dicts): A template definition in a list of dicts.
                If you omit this, then you need to call either load_elements()
                or parse_csv() before doing anything else.
        z('pdf' must be an instance of fpdf.FPDF()N)TLIBEBCC39W)
isinstancer   	TypeErrorpdfsplitting_pdfload_elements_text_line_image_rect_ellipse_barcode_code39_writehandlerstexts)selfr#   elementss      r   __init__zFlexTemplate.__init__!   s     #t$$ 	HFGGG! 	)x(((-<	
 	
 


r   c                 ~   i dt           t          d          fdt           t          d          fdt          t          fdt          t          fdt          t          fdt          t          fdt           t          d          fd	t          t          fd
t          dt          dt          dt          dt          dt           t          d          fdt           t          d          fdt          dt
          t          d          fdt          t          fi}|| _        g | _        |D ]}d|vrd|d<   dD ]p}||vrj|d         dk    rK|dk    rd|v r|d         |d<   (|dk    rd|v r|d         |d<   >|dk    rd|v r|d         |d         z   |d<   ]t          d| d          qd|vr|d         dv rd|d<   nt          d          d	|vr|d         dk    rd|v r|d         |d	<   |	                                D ]\  }}||v rt          ||         |          slt          |t                    r|j        nd                    d  |D                       }t          d!| d"| d#t          ||                   j         d$          | j                            |d                                                    dS )%z
        Load a template definition.

        Arguments:

            elements (list of dicts): A template definition in a list of dicts
        nameNtypex1y1x2y2fontsizebolditalic	underline
foreground
backgroundaligntextpriority	multilinerotater   )r4   r5   r6   r7   r9   r   xyhzMandatory key 'z' missing in input datar   r   z(Mandatory key 'x2' missing in input datawz or c                 $    g | ]}d |j          d S )')__name__).0rF   s     r   
<listcomp>z.FlexTemplate.load_elements.<locals>.<listcomp>}   s&    )G)G)G*;aj*;*;*;)G)G)Gr   zValue of element item 'z
' must be z, not ''.)strr5   intfloatobjectboolr1   keysKeyErroritemsr!   rM   joinr"   appendlower)r0   r1   
key_configektttypes          r   r%   zFlexTemplate.load_elements;   su   
S$t**%
 S$t**%
 3,	

 3,
 3,
 3,
 S$t**%
 S%L
 F
 f
 
 #
 #
 c4::&
  S$t**%!
" #
$ $T

+%
& sEl'
 

, !	 '	0 '	0A?? !*7 Q QA::yE)) 99&'fAdG$99&'fAdG$99&'g#&6AdG$"#OQ#O#O#OPPP   1}}V9--AdGG"#MNNNQ;;1V9#5#5!88 !#AfI"((** 	 	166*QqT1"5"56 &a..I

#[[)G)GQ)G)G)GHH 
 $d!dduddTRSTURVZZM`ddd   IQvY__..////O'	0 '	0r   c                     | dd         dv rt          | d          S | d         dk    rt          | d          S t          |           S )z#Allow hex and oct values for colorsN   )0x0X   r   0   rR   )ss    r   _parse_colorcodezFlexTemplate._parse_colorcode   sK     RaR5L  q"::Q43;;q!991vvr   c                 D    t          |           }|dk    rdS |dk     rdS d S )Nr   TFrh   )ri   is     r   _parse_multilinezFlexTemplate._parse_multiline   s.    FFq554q555tr   ,.c                    d"fd	}dt           dfdt           dfd|dfd|dfd|dfd	|dfd
t           dfd|dfdt          dfdt          dfdt          dfd| j        dfd| j        dfdt           dfdt           dfdt          dfd| j        dfd|dff}g | _        |t          j                    }t          ||          5 }t          j	        ||          D ]}|
                    dgt          |          t          |          z
  z             i }	t          ||          D ]\  }
}|
                                }|sJ|d         r0|d         dk    r|d         dv r:t          d|d          d           |d         dk    rd|	d<   e |d         |          |	|d         <   | j                            |	           	 ddd           n# 1 swxY w Y   d! | j        D             | _        dS )#a+  
        Load the template definition from a CSV file.

        Arguments:

            infile (string): The filename of the CSV file.

            delimiter (single character): The character that seperates the fields in the CSV file:
                Usually a comma, semicolon, or tab.

            decimal_sep (single character): The decimal separator used in the file.
                Usually either a point or a comma.

            encoding (string): The character encoding of the file.
                Default is the system default encoding.
        rf   c                 r    t          |                                 p|                    d                    S )z-Convert to float with given decimal seperatorro   )rS   stripreplace)ri   defaultdecimal_seps     r   _varsep_floatz-FlexTemplate.parse_csv.<locals>._varsep_float   s0     !'')).w77SIIJJJr   r4   Tr5   r6   r7   r8   r9   r:   Fr;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   N)encoding)	delimiter rb   r   r   rI   zMandatory value 'z' missing in csv datac                 B    g | ]}|d                                           S )r4   r[   )rN   vals     r   rO   z*FlexTemplate.parse_csv.<locals>.<listcomp>   s(    BBBSS[&&((BBBr   )rf   )rQ   rR   rj   rm   r1   localegetpreferredencodingopencsvreaderextendlenziprr   r   rZ   rV   )r0   infilerx   ru   rw   rv   r\   frowkargsr|   cfgvss      `         r   	parse_csvzFlexTemplate.parse_csv   s   $	K 	K 	K 	K 	K 	K S$S$=$'=$'=$'=$'S% ]E*S% sE"#u%40%840%8c5!S% e$$/7}e,'

* 244H&8,,, 	,z!y999 , ,

B43z??SXX#=>??? #C 4 4 3 3HCB 3q6 
2"1v~~#a&M2I2I $&3$UA$U$U$U'" '" !" !Vz1101E*- )/Ar

c!f$$U+++++,	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,. CBDMBBB			s   ;C<GGGc                    t          |t                    s J dt          |          j         d            |                                | j        vrt          d|           || j        |                                <   d S )N!name must be of type 'str', not 'rP   z%Element not loaded, cannot set item: )r!   rQ   r5   rM   r[   rV   r   r/   )r0   r4   values      r   __setitem__zFlexTemplate.__setitem__   s    #
 
 	G 	GFtDzz/BFFF	G 	G 	G ::<<ty(( N N NOOO#(
4::<<   r   c                     t          |t                    s J dt          |          j         d            |                                | j        v S )Nr   rP   )r!   rQ   r5   rM   r[   rV   )r0   r4   s     r   __contains__zFlexTemplate.__contains__   s`    #
 
 	G 	GFtDzz/BFFF	G 	G 	G zz||ty((r   c                 4   t          |t                    s J dt          |          j         d            || j        vrt          |          |                                | j        v r| j                 S t          fd| j	        D             d           S )Nr   rP   c              3   d   K   | ]*}|d                                           k     |d         V  +dS )r4   rB   Nr{   )rN   rF   keys     r   	<genexpr>z+FlexTemplate.__getitem__.<locals>.<genexpr>   s?      JJ161B1Bc1I1IQvY1I1I1I1IJJr   )
r!   rQ   r5   rM   rV   rW   r[   r/   nextr1   )r0   r4   r   s     @r   __getitem__zFlexTemplate.__getitem__   s    #
 
 	G 	GFtDzz/BFFF	G 	G 	G ty  4.. jjll$*:c?"JJJJJJJD
 
 	
r   c           	      X   t          fd| j        D                       }| j        s,t                      | _        | j                                         d}|                    d          r|dz  }|                    d          r|dz  }|                    d          r|dz  }| j                            |d	         ||d
                    | j                            |d         |d         z
  |d         |d         z
  t          |          |                    dd          dd          S )a  
        Split a string between words, for the parts to fit into a given element
        width. Additional splits will be made replacing any '\n' characters.

        Arguments:

            text (string): The input text string.

            element_name (string): The name of the template element to fit the text inside.

        Returns:
            A list of substrings, each of which will fit into the element width
            when rendered in the element font style and size.
        c              3   |   K   | ]6}|d                                                                            k    2|V  7dS )r4   Nr{   )rN   elementelement_names     r   r   z/FlexTemplate.split_multicell.<locals>.<genexpr>  sV       
 
v$$&&,*<*<*>*>>> >>>>
 
r   ry   r<   r   r=   r   r>   Ur:   r;   r8   r6   r9   r7   rA   TLINESrJ   rH   rB   rA   dry_runoutput)	r   r1   r$   r   add_pagegetset_font
multi_cellrQ   )r0   rB   r   r   styles     `  r   split_multicellzFlexTemplate.split_multicell   sI     
 
 
 
=
 
 
 
 

 ! 	*!%D'')));;v 	SLE;;x   	SLE;;{## 	SLE##GFOUGFOLLL!,,dmgdm+dmgdm+T++gr** - 
 
 	
r   r   ry   	helvetica
         ?F)r6   r7   r8   r9   rB   r:   r;   scaler<   r=   r>   rA   r?   r@   rD   c                T   |sd S | j         }|j        t          |          k    r |j        t	          |            |d}n1d}|j        t          |          k    r |j        t	          |            |                                                                }d}dD ]C}|	                    d| d          r(|
                    d| d          r|dd	         }||z  }D|	r|d
z  }|
r|dz  }|r|dz  }|                    ||||z             |                    ||           ||z
  ||z
  }}||                    |||d||           d S |r|                    |||d||           d S |                    ||||dd          d         }|                    |||d||           d S )NFTry   r   r   r   <></   r   r   r   r   )rJ   rH   rB   borderrA   fillr   r   )r#   
text_colorr   set_text_colorr   
fill_colorset_fill_colorrr   r[   
startswithendswithr   set_xycellr   )r0   r6   r7   r8   r9   rB   r:   r;   r   r<   r=   r>   rA   r?   r@   rD   ___r#   r   r   tagwidthheights                           r   r&   zFlexTemplate._text'  s2   (  	Fh>[4444CZ 0 011DDD~Z!8!888""D$4$455zz||!!##  	 	Cz3zzz** t}}[#[[[/I/I AbDz 	SLE 	SLE 	SLET5$,///

2rRbvHHuT!5tHTTTTT 	UNN6Qe$       >> "   D HHuT!5tHTTTTTr   )r6   r7   r8   r9   r;   r   r?   c                4   | j         j                                                                        t	          |          k    r | j         j        t          |            | j                             ||z             | j                             ||||           d S N)	r#   
draw_color	serializer[   r   set_draw_colorr   set_line_widthline)
r0   r6   r7   r8   r9   r;   r   r?   r   r   s
             r   r'   zFlexTemplate._lineg  s     8((**0022k*6M6MMM#DH#T*%5%566u---b"b"%%%%%r   )r6   r7   r8   r9   r;   r   r?   r@   c                   | j         }|j                                                                        t	          |          k    r |j        t          |            |d}n1d}|j        t	          |          k    r |j        t          |            |	                    ||z             |
                    ||||z
  ||z
  |           d S NDFD)r   )r#   r   r   r[   r   r   r   r   r   r   rectr0   r6   r7   r8   r9   r;   r   r?   r@   r   r   r#   r   s                r   r)   zFlexTemplate._rectx  s     h>##%%++--Z1H1HHHCZ 0 011EEE~Z!8!888""D$4$4554%<(((Rb"r'77777r   c                   | j         }|j                                                                        t	          |          k    r |j        t          |            |d}n1d}|j        t	          |          k    r |j        t          |            |	                    ||z             |
                    ||||z
  ||z
  |           d S r   )r#   r   r   r[   r   r   r   r   r   r   ellipser   s                r   r*   zFlexTemplate._ellipse  s     h>##%%++--Z1H1HHHCZ 0 011EEE~Z!8!888""D$4$4554%<(((BBGR"WE:::::r   )r6   r7   r8   r9   rB   c                Z    |r(| j                             |||||z
  ||z
  d           d S d S )Nry   )rJ   rH   link)r#   image)r0   r6   r7   r8   r9   rB   r   r   s           r   r(   zFlexTemplate._image  sE     	HHNN4R27b2gBNGGGGG	H 	Hr   interleaved 2of5 ntr   )	r6   r7   r8   r9   rB   r:   r;   r   r?   c       	         V   | j         }|j                                                                        t	          |	          k    r |j        t          |	            |                                                                }|dk    r"|                    |||||z  ||z
             d S d S )Nr   )rJ   rH   )	r#   r   r   r[   r   r   r   rr   interleaved2of5)r0   r6   r7   r8   r9   rB   r:   r;   r   r?   r   r   r#   s                r   r+   zFlexTemplate._barcode  s     h>##%%++--Z1H1HHHCZ 0 011zz||!!##(((b"uRHHHHH )(r   g      ?)r6   r7   r9   rB   r;   r   r?   rF   rG   rJ   rH   c                l   ||	|
|(t          j        dt          t                                 | j        }|j                                                                        t          |          k    r |j	        t          |            ||z
  }|dk    rd}|                    |||||z  |           d S )NzVcode39 arguments x/y/w/h are deprecated since v2.4.4, please use x1/y1/y2/size instead
stacklevelr      )warningswarnDeprecationWarningr   r#   r   r   r[   r   r   r   code39)r0   r6   r7   r9   rB   r;   r   r?   rF   rG   rJ   rH   r   r   r#   s                  r   r,   zFlexTemplate._code39  s      =AMQ]amM8 #*,,    h>##%%++--Z1H1HHHCZ 0 011G66A

4Rq11111r   )r6   r7   r8   r9   rB   r:   r;   r   r<   r=   r>   r   r?   c                   |sd S | j         }|j        t          |          k    r |j        t	          |            |                                                                }d}dD ]C}|                    d| d          r(|                    d| d          r|dd         }||z  }D|	r|dz  }|
r|d	z  }|r|d
z  }|	                    ||||z             |
                    ||           |                    d||           d S )Nry   r   r   r   r   r   r   r   r   r   r   )r#   r   r   r   r   rr   r[   r   r   r   r   write)r0   r6   r7   r8   r9   rB   r:   r;   r   r<   r=   r>   r   r?   r   r   r#   r   r   s                      r   r-   zFlexTemplate._write  s=   &  	Fh>[4444CZ 0 011zz||!!##  	 	Cz3zzz** t}}[#[[[/I/I AbDz 	SLE 	SLE 	SLET5$,///

2r		!T4     r           c           	      @   t          | j        d           }| j                                        5  |D ]I}|                                }| j                            |d                                         |                    dd                    |d<   |dk    r\|d         |z  |d<   |d         |z  |d<   |d         |d	         |d         z
  |z  z   |d	<   |d         |d
         |d         z
  |z  z   |d
<   |r|d         |z   |d<   |d	         |z   |d	<   |r|d         |z   |d<   |d
         |z   |d
<   ||d<   |d                                         }|r| j        	                    |||          5  d|v rb|d         rZ| j        	                    |d         |d         |d                   5   | j
        |         di | ddd           n# 1 swxY w Y   n | j
        |         di | ddd           n# 1 swxY w Y   d|v rc|d         r[| j        	                    |d         |d         |d                   5   | j
        |         di | ddd           n# 1 swxY w Y   6 | j
        |         di | K	 ddd           n# 1 swxY w Y   i | _        dS )a`  
        Add the contents of the template to the PDF document.

        Arguments:

            offsetx, offsety (float): Place the template to move its origin to the given coordinates.

            rotate (float): Rotate the inserted template around its (offset) origin.

            scale (float): Scale the inserted template by this factor.
        c                     | d         S )NrC   r	   )rF   s    r   <lambda>z%FlexTemplate.render.<locals>.<lambda>  s
    a
m r   )r   r4   rB   ry   r   r6   r7   r8   r9   r   r5   rE   Nr	   )sortedr1   r#   local_contextcopyr/   r   r[   upperrotationr.   )	r0   offsetxoffsetyrE   r   sorted_elementsr   elehandler_names	            r   renderzFlexTemplate.render  s    !4K4KLLLX##%% 	; 	;* ; ;llnn"jnnS[->->-@-@#''&RTBUBUVVFC<< #D	E 1CI #D	E 1CI #D	c$i'$-.G5-P QCI #D	c$i'$-.G5-P QCI 4 #D	G 3CI #D	G 3CI 4 #D	G 3CI #D	G 3CI$G"6{0022 ;**67GDD ? ?#s??s8}?!%!2!23x=#d)SQUY!W!W C C ;l ; B Bc B B BC C C C C C C C C C C C C C C 8DM,7>>#>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  33x=!X..s8}c$iTSS ? ?7DM,7>>#>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 4l3::c::::9;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;< 


s~   D?J0;G6+G	?G6GG6GG6*J6G::J=G:>?J=IJI!!J$I!%JJJr   )rn   ro   N)r   r   r   r   )rM   
__module____qualname____doc__r2   r%   staticmethodrj   rm   r   r   setr   r   r   r&   r'   r)   r*   r(   r+   r,   r-   r   r	   r   r   r   r      s           4G0 G0 G0R   \   \FC FC FC FCP) ) ) C) ) )
 
 
&
 &
 &
V #>U >U >U >U >UF & & & & &( 8 8 8 8 88 ; ; ; ; ;2 !ab H H H H H "I I I I I2 



2 2 2 2 2L &! &! &! &! &!P+ + + + + +r   r   c                   J     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d
 fd	Zd Zd fd		Z xZS )Templatezr
    A simple templating class.

    Allows to apply a single template definition to all pages of a document.
    NA4portraitmmry   c                    |r(t          j        dt          t                                 dD ]<}t	          t                      |         t                    st          d| d          =t          |||          }|	                    |           |
                    |           |                    |	           |                    |           |                    |
           t                                          ||           dS )	a  
        Arguments:

            infile (str): [**DEPRECATED since 2.2.0**] unused, will be removed in a later version

            elements (list of dicts): A template definition in a list of dicts.
                If you omit this, then you need to call either load_elements()
                or parse_csv() before doing anything else.

            format (str): The page format of the document (eg. "A4" or "letter").

            orientation (str): The orientation of the document.
                Possible values are "portrait"/"P" or "landscape"/"L"

            unit (str): The units used in the template definition.
                One of "mm", "cm", "in", "pt", or a number for points per unit.

            title (str): The title of the document.

            author (str): The author of the document.

            subject (str): The subject matter of the document.

            creator (str): The creator of the document.
        zD"infile" is deprecated since v2.2.0, unused and will soon be removedr   )formatorientationunittitleauthorsubjectcreatorkeywordsz
Argument "z" must be of type str.)r   r   r   )r#   r1   N)r   r   r   r   r!   localsrQ   r"   r   	set_title
set_authorset_creatorset_subjectset_keywordssuperr2   )r0   r   r1   r   r   r   r   r   r   r   r  argr#   	__class__s                r   r2   zTemplate.__init__F  s   L  	MV"*,,   
	
 	J 	JC fhhsmS11 J HS H H HIIIJ&kEEEev      """S844444r   c                 x    | j         j        r|                                  | j                                          dS )z5Finish the current page, and proceed to the next one.N)r#   pager   r   )r0   s    r   r   zTemplate.add_page  s5    8= 	KKMMMr   c                 H   |r(t          j        dt          t                                 | j                            ddd           | j                            dd           t                                                       |r| j        	                    |           d	S d	S )
aa  
        Finish the document and process all pending data.

        Arguments:

            outfile (str): If given, the PDF file will be written to this file name.
                Alternatively, the `.pdf.output()` method can be manually called.

            dest (str): [**DEPRECATED since 2.2.0**] unused, will be removed in a later version.
        zB"dest" is deprecated since v2.2.0, unused and will soon be removedr   r   r   re   Fr   )marginN)
r   r   r   r   r#   r   set_auto_page_breakr  r   r   )r0   outfiledestr
  s      r   r   zTemplate.render  s      	MT"*,,   
 	+sB///$$U1$555 	%HOOG$$$$$	% 	%r   )
NNr   r   r   ry   ry   ry   ry   ry   )NN)rM   r   r   r   r2   r   r   __classcell__)r
  s   @r   r   r   =  s          ?5 ?5 ?5 ?5 ?5 ?5B  % % % % % % % % % %r   r   )r   
__author____copyright____license__r   r}   r   deprecationr   errorsr   fpdfr   r   r   r   r   r	   r   r   <module>r     s   & &4
5             ( ( ( ( ( ( ! ! ! ! ! !      ; ; ;; ; ;a a a a a a a aHf% f% f% f% f%| f% f% f% f% f%r   