
    }fs               
          d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
mZmZmZ dZ G d de          Zd	 Zd
 Zdeeef         defdZdeeee
eeee
f                  f         deeef         deeef         deeef         fdZdZd Zd ZdefdZdefdZdeedf         fdZdee         fdZdee         fdZdefdZ defdZ!defdZ"dS )a  
Various utilities that could not be gathered logically in a specific module.

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.
    N)Number)get_traced_memory
is_tracing)IterableTupleUnion
NamedTuple   c                   x    e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   e	de
eeeef         fd            ZdS )	Paddingr   toprightbottomleftpaddingc                    t          |t          t          f          rt          ||||          S t	          |          dk    r*t          |d         |d         |d         |d                   S t	          |          dk    r*t          |d         |d         |d         |d                   S t	          |          dk    r	t          | S t          dt          |                     )zbReturn a 4-tuple of padding values from a single value or a 2, 3 or 4-tuple according to CSS rules   r            zBpadding shall be a number or a sequence of 2, 3 or 4 numbers, got )
isinstanceintfloatr   len
ValueErrorstr)clsr   s     B/var/www/rtfcompare/venv/lib/python3.11/site-packages/fpdf/util.pynewzPadding.new   s     gU|,, 	?7GWg>>>w<<171:wqz71:wqzJJJw<<171:wqz71:wqzJJJw<<1G$$_QTU\Q]Q]__
 
 	
    N)__name__
__module____qualname__r   r   __annotations__r   r   r   classmethodr   r   r   tuplelistr    r    r   r   r      s         COOOE6FFD&
%UE4 78 
 
 
 [
 
 
r    r   c                 $   t          |           }t          |          t          |          k    sJ d| d|             |                     |                                |                                d          } t          |           |k    sJ | S )Nzplaceholder=z value=r   )r   replaceencode)bufferplaceholdervaluebuffer_sizes       r   buffer_substr0   )   s    f++K{s5zz)))+U++U+Ue+U+U)))^^K..00%,,..!DDFv;;+%%%%Mr    c                 t   t          | t                    rR|                     dd                              dd                              dd                              dd          S |                     d	d
                              dd                              dd                              dd          S )z*Add a backslash character before , ( and )\z\\)z\)(z\(z\r   \s   \\   )s   \)   (s   \(   s   \r)r   r   r*   )ss    r   escape_parensr;   1   s    !S 
IIdF##WS%  WS%  WT5!!		
 	
		%!!	v			v						r    unitreturnc                     t          | t                    rt          |           S | dk    rdS | dk    rdS | dk    rdS | dk    rdS t          d	|            )
z
    Get how many pts are in a unit. (k)

    Args:
        unit (str, float, int): Any of "pt", "mm", "cm", "in", or a number.
    Returns:
        float: The number of points in that unit (assuming 72dpi)
    Raises:
        ValueError
    ptr   mmgjZ@cmgbX<@ing      R@zIncorrect unit: )r   r   r   r   )r<   s    r   get_scale_factorrC   B   sq     $ T{{t||qt||yt||yt||t
...
/
//r    
to_convertold_unitnew_unitc                     t          |          t          |          z  t          | t                    rt          fd| D                       S | z  S )a,  
     Convert a number or sequence of numbers from one unit to another.

     If either unit is a number it will be treated as the number of points per unit.  So 72 would mean 1 inch.

     Args:
        to_convert (float, int, Iterable): The number / list of numbers, or points, to convert
        old_unit (str, float, int): A unit accepted by fpdf.FPDF or a number
        new_unit (str, float, int): A unit accepted by fpdf.FPDF or a number
    Returns:
        (float, tuple): to_convert converted from old_unit to new_unit or a tuple of the same
    c              3   :   K   | ]}t          |d           V  dS )r   N)convert_unit).0iunit_conversion_factors     r   	<genexpr>zconvert_unit.<locals>.<genexpr>n   s0      TTA\!Q(>??TTTTTTr    )rC   r   r   r&   )rD   rE   rF   rL   s      @r   rI   rI   [   sc    " .h77:J8:T:TT*h'' UTTTTTTTTTT...r    ))Mi  )CMi  )Di  )CDi  )Cd   )XCZ   )L2   )XL(   )X
   )IX	   )V   )IVr   )Ir   c                 P    d}t           D ]\  }}| |k    r||z  }| |z  } | |k    |S )z#Convert an integer to Roman numeral )ROMAN_NUMERAL_MAP)nresultnumeralintegers       r   	int2romanri      sJ    F-  7llgFLA 7ll Mr    c                 >    t          t          |                      d S N)printget_mem_usage)prefixs    r   print_mem_usagero      s    	-

     r    c                     t          j                    }t                      }t                      }d}t	                      rdt                      z   }| dd| d| d| S )Nrc   zMalloc stats: z<40 z | Pillow: z | Process RSS: )gccollectget_process_rssget_pillow_allocated_memoryr   get_tracemalloc_traced_memory)rn   _collected_countrsspillowmalloc_statss        r   rm   rm      sp    z||


C )**FL|| J'*G*I*IIQQQ<QQFQQCQQQr    c                  2    t                      } | r| ddS dS )N.1f MiB<unavailable>)get_process_rss_as_mib)
rss_as_mibs    r   rt   rt      s,    '))J '&&&&&?r    c                  T   t          j                    } 	 t          d|  dd          5 }t          |                                                                d                   t          j        d          z  dz  dz  	 cddd           S # 1 swxY w Y   dS # t          $ r Y dS w xY w)	zInspired by psutil source code/proc/z/statmutf8encodingr   SC_PAGE_SIZE   N)osgetpidopenr   readlinesplitsysconfFileNotFoundError)pidstatms     r   r   r      s   
)++C	&3&&&888 	EENN$$**,,Q/00*^,,- 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	    tts5   B AB?B BB BB 
B'&B'c                     d\  } }t          j                    }	 t          d| dd          5 }t          |          }d d d            n# 1 swxY w Y   n# t          $ r | |fcY S w xY w|D ]}|                                }|d         |d         }}|                    d          \  }	}
t          |	d	          t          |
d	          }
}	|
|	z
  }|d
k    r|dz  dz  dd} v|dk    r|dz  dz  dd}| |fS )N)r~   r~   r   z/mapsr   r   r   -r
   z[heap]r   r|   r}   z[stack])r   r   r   r'   r   r   r   )heap_size_in_mibstack_size_in_mibr   	maps_file
maps_lineslinewords
addr_rangepath
addr_startaddr_endsizes               r    get_process_heap_and_stack_sizesr      s   *J''
)++C3%3%%%777 	)9iJ	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 3 3 3!222223 	@ 	@

 8U2YD
)//44
H":r22C"4E4EH
*$8"&+"4>>>>Y#'$;#5 ? ? ? ?...s3   A AA AA AA A('A(c                  4   	 ddl m}  d\  }} |                                                                             D ]}|                    d          r<t          |                                d                             dd                    }S|                    d          r;t          |                                d                             dd                    }|d	z  d	z  d
d|d	z  d	z  d
dS # t          $ r t          j
        d           Y dS w xY w)za
    Get PyMalloc stats from sys._debugmallocstats()
    From experiments, not very reliable
    r   )get_debugmallocstats)r   r   Totalr   ,rc   z# bytes in allocated blocksr   r|   z / r}   z>pymemtrace could not be imported - Run: pip install pymemtracer~   )pymemtrace.debug_malloc_statsr   decode
splitlines
startswithr   r   r*   ImportErrorwarningswarn)r   	allocatedtotalr   s       r   &get_pymalloc_allocated_over_total_sizer      s:   
FFFFFF!	5((**1133>>@@ 	C 	CDw'' CDJJLL,44S"==>>!>?? C

R 0 8 8b A ABB	d"T)OOO%$,2EOOOOO   VWWWs   C2C5 5DDc                      	 ddl m} m t          fd |             D                       }|dz  dz  ddS # t          $ r t          j        d           Y dS w xY w)	z#From experiments, not very reliabler   )get_objects	getsizeofc              3   .   K   | ]} |          V  d S rk   r(   )rJ   objr   s     r   rM   z1get_gc_managed_objs_total_size.<locals>.<genexpr>   s+      FFiinnFFFFFFr    r   r|   r}   z8pympler could not be imported - Run: pip install pymplerr~   )pympler.muppyr   r   sumr   r   r   )r   objs_total_sizer   s     @r   get_gc_managed_objs_total_sizer      s    88888888FFFFFFFFF!D(4/99999   PQQQs   6: AAc                  P    t                      \  } }| dz  dz  dd|dz  dz  ddS )zRequires python -X tracemallocr   r|   z (peak=z) MiB)r   )currentpeaks     r   rv   rv      s@    %''MGTnt#MMMtd0BMMMMMr    c                  ~    ddl m}  | j                                        }|d         |d         z
  }|t          z  ddS )Nr   )Imageallocated_blocksfreed_blocksr|   r}   )PILr   core	get_statsPIL_MEM_BLOCK_SIZE_IN_MIB)r   statsblocks_in_uses      r   ru   ru      sT    J  ""E,-n0EEM77AAAAAr    )#__doc__rr   r   r   numbersr   tracemallocr   r   typingr   r   r   r	   r   r   r0   r;   r   r   rC   r   r&   rI   rd   ri   ro   rm   rt   r   r   r   r   rv   ru   r(   r    r   <module>r      s                      5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5  
 
 
 
 
j 
 
 
.    "05f- 0% 0 0 0 02/eS(5X1E+F"GGH/CK / CK / 5%<	/ / / /. "  ! ! !
RS 
R 
R 
R 
R    fdl 3    /%* / / / /*c
    *
 
 
 
 
Ns N N N NBS B B B B B Br    