
    }fJ                     .   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlm Z! ddlm"Z# ddl$m%Z& 	 ddl'm(Z( n# e)$ r dZ(Y nw xY w ej*        e+          Z,ddddZ- G d d          Z. G d de.          Z/ G d de          Z0 G d de          Z1 G d d e          Z2 G d! d"          Z3 G d# d$e          Z4 G d% d&e          Z5 G d' d(e          Z6 G d) d*e          Z7 G d+ d,e          Z8 G d- d.e          Z9 G d/ d0e          Z: G d1 d2e          Z; G d3 d4e          Z< G d5 d6e.          Z= G d7 d8          Z>	 	 	 d@d:efd;Z?d< Z@d= ZAdAd?ZBdS )Bai  
This module contains the serialization logic that produces a PDF document from a FPDF instance.
Most of the code in this module is used when FPDF.output() is called.

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)defaultdictOrderedDict)contextmanager)BytesIO   )PDFAnnotation)SignatureFlag)FPDFException)RasterImageInfo)build_outline_objs)	Signaturesign_content)build_obj_dictNamePDFArrayPDFContentStreamPDFDate	PDFObject	PDFString)create_dictionary_string)create_list_string)iobj_ref)subset)signer)z/Fit)z/FitHnull)/XYZr   r   1)fullpage	fullwidthrealc                       e Zd ZddZdS )ContentWithoutIDNc                     d S N self_security_handlers     D/var/www/rtfcompare/venv/lib/python3.11/site-packages/fpdf/output.py	serializezContentWithoutID.serialize5   s        r$   )__name__
__module____qualname__r*   r%   r+   r)   r"   r"   4   s(             r+   r"   c                       e Zd Zd ZddZdS )	PDFHeaderc                     || _         d S r$   pdf_version)r'   r3   s     r)   __init__zPDFHeader.__init__:   s    &r+   Nc                     d| j          S )Nz%PDF-r2   r&   s     r)   r*   zPDFHeader.serialize>   s    )t')))r+   r$   r,   r-   r.   r4   r*   r%   r+   r)   r0   r0   9   s7        ' ' '* * * * * *r+   r0   c                         e Zd Zd fd	Z xZS )PDFFontNc                 R   t                                                       t          d          | _        t          |          | _        t          |          | _        |rt          |          nd | _        || _        || _        d | _	        d | _
        d | _        d | _        d | _        d S )NFont)superr4   r   typesubtype	base_fontencodingd_wwdescendant_fonts
to_unicodec_i_d_system_infofont_descriptorc_i_d_to_g_i_d_map)r'   r=   r>   r?   r@   rA   	__class__s         r)   r4   zPDFFont.__init__C   s    LL	G}}i*2<X $!%#"&r+   NNNr,   r-   r.   r4   __classcell__rG   s   @r)   r8   r8   B   s=        ' ' ' ' ' ' ' ' ' 'r+   r8   c                        e Zd Z fdZ xZS )CIDSystemInfoc                     t                                                       t          dd          | _        t          dd          | _        d| _        d S )NAdobeTencryptUCSr   )r;   r4   r   registryordering
supplement)r'   rG   s    r)   r4   zCIDSystemInfo.__init__S   sL    !'4888!%666r+   rI   rK   s   @r)   rM   rM   R   s8                r+   rM   c                   $     e Zd Zdef fdZ xZS )PDFInfocreation_datec                    t                                                       |rt          |d          nd | _        |rt          |d          nd | _        |rt          |d          nd | _        |rt          |d          nd | _        |rt          |d          nd | _        |rt          |d          nd | _        || _	        d S )NTrP   )
r;   r4   r   titlesubjectauthorkeywordscreatorproducerrX   )	r'   rZ   r[   r\   r]   r^   r_   rX   rG   s	           r)   r4   zPDFInfo.__init__[   s     	7<FYud3333$
;BLy$77779?Ii5555T=EO	(D99994;BLy$7777=EO	(D99994*r+   )r,   r-   r.   r   r4   rJ   rK   s   @r)   rW   rW   Z   sG        + + + + + + + + + + +r+   rW   c                       e Zd Zd ZddZdS )AcroFormc                 "    || _         || _        d S r$   fields	sig_flags)r'   rd   re   s      r)   r4   zAcroForm.__init__p   s    "r+   Nc                 |     t           fdt                     D             ||          }t          |d          S )Nc                 2    i | ]}|t          |          S r%   )getattr).0keyr'   s     r)   
<dictcomp>z&AcroForm.serialize.<locals>.<dictcomp>v   s%    :::S'$$$:::r+   )r(   _obj_id 
field_join)r   dirpdf_dict)r'   r(   rl   obj_dicts   `   r)   r*   zAcroForm.serializet   sO    !::::D		:::/
 
 

 S1111r+   NNr6   r%   r+   r)   ra   ra   o   s7        # # #2 2 2 2 2 2r+   ra   c                   "     e Zd Z	 d fd	Z xZS )
PDFCatalogNc                 :   t                                                       t          d          | _        |rt	          |          nd | _        || _        || _        || _        d | _	        d | _
        d | _        d | _        d | _        d | _        d | _        d | _        d S )NCatalog)r;   r4   r   r<   r   langpage_layout	page_modeviewer_preferencespages	acro_formopen_action	mark_infometadatanamesoutlinesstruct_tree_root)r'   rx   ry   rz   r{   rG   s        r)   r4   zPDFCatalog.__init__~   s     	OO	'+5IdOOO	&""4

 $r+   )NNNNrI   rK   s   @r)   ru   ru   }   s@        NR% % % % % % % % % %r+   ru   c                        e Zd Z fdZ xZS )PDFResourcesc                     t                                                       || _        || _        || _        || _        d S r$   )r;   r4   proc_setfontx_objectext_g_state)r'   r   r   r   r   rG   s        r)   r4   zPDFResources.__init__   s<     	 &r+   rI   rK   s   @r)   r   r      s8        ' ' ' ' ' ' ' ' 'r+   r   c                        e Zd Z fdZ xZS )PDFFontStreamc                 v    t                                          |d           t          |          | _        d S NTcontentscompress)r;   r4   lenlength1r'   r   rG   s     r)   r4   zPDFFontStream.__init__   s1    (T:::8}}r+   rI   rK   s   @r)   r   r      s8        % % % % % % % % %r+   r   c                        e Zd Z fdZ xZS )PDFXmpMetadatac                     t                                          |                    d                     t          d          | _        t          d          | _        d S )Nzutf-8r   MetadataXML)r;   r4   encoder   r<   r=   r   s     r)   r4   zPDFXmpMetadata.__init__   sJ    (//'":":;;;$$	E{{r+   rI   rK   s   @r)   r   r      s8        # # # # # # # # #r+   r   c                   *     e Zd ZdZ	 	 	 d fd	Z xZS )
PDFXObject)_id	_contentsfilterlengthr<   r=   widthheightcolor_spacebits_per_componentdecodedecode_parmss_maskNc
                 &   t                                          |           t          d          | _        t          |          | _        || _        || _        || _        || _        t          |          | _	        || _
        |	| _        d | _        d S )Nr   XObject)r;   r4   r   r<   r=   r   r   r   r   r   r   r   r   )r'   r   r=   r   r   r   r   
img_filterr   r   rG   s             r)   r4   zPDFXObject.__init__   s     	(+++OO	G}}
&"4:&&(r+   rH   r,   r-   r.   	__slots__r4   rJ   rK   s   @r)   r   r      sO        I0          r+   r   c                   "     e Zd ZdZ fdZ xZS )PDFICCPObject)r   r   r   r   n	alternatec                     t                                          |d           || _        t          |          | _        d S r   )r;   r4   r   r   r   )r'   r   r   r   rG   s       r)   r4   zPDFICCPObject.__init__   s:     	(T:::ir+   r   rK   s   @r)   r   r      s?        I) ) ) ) ) ) ) ) )r+   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )PDFPage)r   r<   r   durtransannotsgroup	media_boxstruct_parents	resourcesparent_index	_width_pt
_height_ptc                 :   t                                                       t          d          | _        || _        |r|nd | _        || _        t                      | _        d | _	        d | _
        d | _        d | _        d | _        || _        d\  | _        | _        d S )NPagers   )r;   r4   r   r<   r   r   r   r   r   r   r   r   r   r   r   r   r   )r'   duration
transitionr   indexrG   s        r)   r4   zPDFPage.__init__   s     	LL	 '188T
jj
"*4'r+   c                     | j         S r$   )r   r'   s    r)   r   zPDFPage.index  s
    {r+   c                     | j         | j        fS )zGReturn a pair (width, height) in the unit specified to FPDF constructorr   r   r   s    r)   
dimensionszPDFPage.dimensions  s    ~t..r+   c                 $    ||c| _         | _        dS )zHAccepts a pair (width, height) in the unit specified to FPDF constructorNr   )r'   width_pt	height_pts      r)   set_dimensionszPDFPage.set_dimensions  s    *2I'r+   )	r,   r-   r.   r   r4   r   r   r   rJ   rK   s   @r)   r   r      sl        I"5 5 5 5 5*  / / /> > > > > > >r+   r   c                        e Zd Z fdZ xZS )PDFPagesRootc                     t                                                       t          d          | _        || _        || _        d | _        d S )NPages)r;   r4   r   r<   countr   kids)r'   r   r   rG   s      r)   r4   zPDFPagesRoot.__init__  s?    MM	
"			r+   rI   rK   s   @r)   r   r     s8                r+   r   c                   &     e Zd Z fdZddZ xZS )PDFExtGStatec                 V    t                                                       || _        d S r$   )r;   r4   _dict_as_str)r'   dict_as_strrG   s     r)   r4   zPDFExtGState.__init__#  s'    'r+   Nc                 &    | j          d| j         dS )Nz 0 obj
z
endobj)idr   )r'   rr   r(   s      r)   r*   zPDFExtGState.serialize(  s    '>>4#4>>>>r+   rs   )r,   r-   r.   r4   r*   rJ   rK   s   @r)   r   r   "  sL        ( ( ( ( (
? ? ? ? ? ? ? ?r+   r   c                       e Zd Zd ZddZdS )PDFXrefAndTrailerc                 \    || _         |j        dz   | _        d | _        d | _        d | _        d S Nr   )output_builderobj_idr   catalog_objinfo_objencryption_obj)r'   r   s     r)   r4   zPDFXrefAndTrailer.__init__-  s6    ,#*Q.
"r+   Nc                    | j         }t          t          |j                            }g }|                    d           |                    d| j                    |                    d           t          d| j                  D ]&}|                    |j        |         dd           '|                    d           |                    d           |                    d	| j                    |                    d
t          | j	        j
                              |                    dt          | j        j
                              |j        }| j        r<|                    dt          | j        j
                              |j        j        }n4|                                }|dk    r|                    |j                  }|r|                    d| d           |                    d           |                    d           |                    |           |                    d           d                    |          S )Nxrefz0 z0000000000 65535 f r   010z	 00000 n trailer<<z/Size z/Root z/Info z	/Encrypt z/ID []>>	startxrefz%%EOF
)r   strr   bufferappendr   rangeoffsetspdf_refr   r   r   fpdfr   r(   file_id_default_file_idjoin)r'   r(   builderr   outr   r   r   s           r)   r*   zPDFXrefAndTrailer.serialize5  s1   %GN++,,	

6

$
$$%%%

()))Atz** 	B 	BFJJ'/&1@@@@AAAA

9

4

(DJ(()))

:GD$4$788::;;;

7GDM$45577888| 	@JJD74+>+A#B#BDDEEE,4GGllnnG"}}//?? 	+JJ)w)))***

4

;

9

7yy~~r+   r$   r6   r%   r+   r)   r   r   ,  s7        # # #     r+   r   c                       e Zd ZdZd Zd Zd ZddZd Z e	dd          fd	Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zed             Zd ZdS )OutputProducerzVGenerates the final bytearray representing the PDF document, based on a FPDF instance.c                     || _         g | _        i | _        d| _        i | _        i | _        t          t                    | _        t                      | _
        d S )Nr   )r   pdf_objsiccp_i_to_pdf_ir   r   trace_labels_per_obj_idr   intsections_size_per_trace_label	bytearrayr   )r'   r   s     r)   r4   zOutputProducer.__init__W  sM    	!')$-8-=-=*kkr+   c           
      L
   | j         }|j        rV|                                }|dk    r"|                    t	          d                    }|j                            |           | j                            t          |j	                             | 
                                }|                                 }|                                 }|                                 }|j        D ]}|                     |d           |                                 }|                                 }	|                                 }
|                     ||	|
          }|                                 }|                                 \  }}|                                 }|                                 }|                                 }t3          |           }| j                            |           t5          |          |_        |                     |||d         ||||           g }|D ]}||_        ||_        |j        D ]}g }|j         r|                    |j                    |j!        r4tE          |j!        d          r|                    |j!        j                    |D ]G}|j#        tI          |          k    r-tK          d|j#         d|&                                 d          H|'                    |           |j        sd	|_        |D ]}|                    |j                    |D ])}tQ          ||j#        d
z
           j)                  |_*        *|j+        j,        j-        D ]$}||#                                d
z
           |_.        %||_/        ||_0        ||_1        | j2        rJ d| j2                     | j3        rJ dtI          | j3                               | j        D ]}ti          |tj                    rd	}n@tI          | j2                  | j3        |j)        <   | j6        7                    |j)                  }|r\| 8                    |          5  | 9                    |:                    |j                             d	d	d	           n# 1 swxY w Y   | 9                    |:                    |j                             | ;                                 |j<        r=t{          t|          | j2        |j<        |j?        |j@        |jA        |jB                  | _2        | j2        S )z
        This method alters the target FPDF instance
        by assigning IDs to all PDF objects,
        plus a few other properties on PDFPage instances
        r   r   embedded_files)pages_root_objfirst_page_objsig_annotation_objxmp_metadata_objstruct_tree_root_objoutline_dict_objdestz'Invalid reference to non-existing page z present on page z: Nr   z@Nothing should have been appended to the .buffer at this stage: z.No offset should have been set at this stage: )r(   )Cr   r(   r   r   r  generate_passwordsr   r   r0   r3   _add_pages_root_add_catalog
_add_pages_add_annotations_as_objectsr  _add_pdf_obj
_add_fonts_add_images_add_gfxstates_add_resources_dict_add_structure_tree_add_document_outline_add_xmp_metadata	_add_info_add_encryptionr   r   r   _finalize_catalogr   r   r   r  ahasattrpage_numberr   
ValueErrorr   extendr   r   page_refstruct_builderdoc_struct_elemkpgr   r   r   r   r   
isinstancer"   r   get_trace_size_outr*   _log_final_sections_sizes	_sign_keyr   r   
_sign_cert_sign_extra_certs_sign_hashalgo
_sign_time)r'   r   r   r  r   	page_objsr  embedded_filefont_objs_per_indeximg_objs_per_indexgfxstate_objs_per_nameresources_dict_objr	  r
  outline_itemsr  r   r   r   destspage_objannot
page_destsr  outline_itemstruct_elempdf_objtrace_labels                               r)   	bufferizezOutputProducer.bufferizeb  s    y
 ! 	?llnnG"}}//	$@@"55g>>>Yt'788999--//''))OO%%	!==??!0 	? 	?Mm-=>>>>"oo//!--//!%!4!4!6!6!55!35K
 
  $7799*.*D*D*F*F'-1133>>##--// &&T""" 'y11)$Q<1-!5- 	 	
 	
 	
 ! 	' 	'H,HO!3H! ) )
: 2%%ej1117 4wuw77 4%%egl333&  D'#i..88(}dFV}}iqiwiwiyiy}}}   9 Z((((? '"&) 	, 	,LLL*++++ 	H 	HD#Id.>.B$C$FGGDMM.>@ 	F 	FK&{'>'>'@'@1'DEKNN& , 	\ 	\[dk[[	\ 	\ 	\ 	P 	POC<M<MOO	P 	P 	P } 	W 	WG'#344 K"+.t{+;+;WZ(":>>wzJJ W%%k22  II))D<R)SS                
 		'++d>T+UUVVVV&&(((> 		&&# DK {s   /RR	R	c                     t          |t                    s9t          |t                    st          |          }|                    d          }| xj        |dz   z  c_        dS )zAppend data to the bufferlatin1   
N)r&  bytesr   r   r   )r'   datas     r)   r)  zOutputProducer._out  sZ    $&& 	)dC(( !4yy;;x((Dte|#r+   Nc                     | xj         dz  c_         | j         |_        | j                            |           |r|| j        | j         <   | j         S r   )r   r   r   r   r   )r'   r=  r>  s      r)   r  zOutputProducer._add_pdf_obj  sQ    q[
W%%% 	D8CD(5{r+   c                     | j         }t          |j        t          |j                            }|                     |           |S )N)r   r   )r   r   pages_count_dimensions_to_mediaboxdefault_page_dimensionsr  )r'   r   r  s      r)   r  zOutputProducer._add_pages_root  sM    y%"-d.JKK
 
 
 	.)))r+   r   c                    | j         }g }t          |j                                                  |         D ]}|j        dk    rt          ddddd          |_        |                                |j        k    r&t          |                                          |_
        |                     |d           |                    |           t          |j        |j        	          }|                     |d           ||_        |S )
Nz1.3z/Groupz/Transparency
/DeviceRGB)z/Typez/Sz/CSrm   rn   r|   r   )r   listr|   valuesr3   rq   r   r   rI  rH  r   r  r   r   r   r   )r'   _slicer   r0  r8  cs_objs         r)   r  zOutputProducer._add_pages  s   y	TZ..0011&9 	' 	'H%''!)&olSS"" " " ""$$(DDD%<X=P=P=R=R%S%S"h000X&&& &!*T]  F fg... &Hr+   c                    d }| j         j                                        D ]^}|j        D ]T}t	          |t
                    r=|                     |           t	          |j        t                    r|
J d            |}U_|S )Nz0A /Sig annotation is present on more than 1 page)	r   r|   rM  r   r&  r   r  vr   )r'   r  r8  	annot_objs       r)   r  z*OutputProducer._add_annotations_as_objects  s    !	..00 	7 	7H%_ 7 7	i77 7%%i000!)+y99 7.66M 766-6*7 "!r+   c                 8	   i }t          | j        j                                        d           D ]ej        dk    rEj        dvrdnd }t          dj        |          }|                     |d           ||j        <   Sj        d	k    rd
j         }j	        
                                }t          j                  dk    r>t                              d|d                    d j        D                                  t!          j        dd          }|xj        g dz  c_        t!          j        |          }|                    |           |	                    j                   fdj	                                        D             }t/                      }	j                            |	           |	                    d           |	                                }
t          d|d          }|                     |d           ||j        <   t          d|j        j        t;                              }|                     |d           t=          |g          |_        g }d j	                                        D ]_\  }}t          |j                   dk    r|!                    d|ddd                    fd|j         D                        d           `tE          dt          |           d d                    |           d!          }|                     |d           ||_#        tI                      }|                     |d           ||_%        j        }tM          |          |_'        |                     |d           ||_(        d"gd#z  d#z  d$z  }|                                D ]8\  }}tS          |d%z	            ||d$z  <   tS          |d&z            ||d$z  d'z   <   9d                    |          }tE          |*                    d(          d)          }|                     |d           ||_+        tY          |
*          }|                     |d           ||_-        .                                 g|S )+Nc                     | j         S r$   )i)r   s    r)   <lambda>z+OutputProducer._add_fonts.<locals>.<lambda>  s    df r+   rj   core)SymbolZapfDingbatsWinAnsiEncodingType1)r=   r>   r?   fontsTTFzMPDFAA+r   z+Font %s is missing the following glyphs: %sz, c              3   4   K   | ]}t          |          V  d S r$   )chr)ri   xs     r)   	<genexpr>z,OutputProducer._add_fonts.<locals>.<genexpr>,  s(      !F!FQ#a&&!F!F!F!F!F!Fr+   T)notdef_outlinerecommended_glyphs)FFTMGDEFGPOSGSUBMATHhdmxmeta)glyphsc                 V    i | ]%\  }}|j                             |j                  &S r%   )ttfont
getGlyphID
glyph_name)ri   glyphchar_idr   s      r)   rk   z-OutputProducer._add_fonts.<locals>.<dictcomp>J  sB     ! ! !&w T[33E4DEE! ! !r+   Type0z
Identity-HCIDFontType2)r=   r>   r@   rA   c                 L    | dk    rd| dz
  dz	  z  }d| dz  z  }|d|dS | dS )Ni  i   i   
   i   i  04Xr%   )unicode	code_highcode_lows      r)   format_codez.OutputProducer._add_fonts.<locals>.format_codeo  sR    ''$*g.?B-F$F	#)Wu_#="+??????%+++r+   <rw  z> < c              3   .   K   | ]} |          V  d S r$   r%   )ri   coder{  s     r)   rb  z,OutputProducer._add_fonts.<locals>.<genexpr>{  s-      7d7ddD8I8I7d7d7d7d7d7dr+   z>
z/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<</Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
z beginbfchar
zGendbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end             r   rA  r   r   )/sortedr   r]  rM  r<   namer8   r  rU  r   get_all_glyph_namesr   missing_glyphsLOGGERwarningr   ftsubsetOptionsdrop_tables	Subsetterpopulatern  itemsr   saveseekreaddescmissing_width_tt_font_widthsr   rB   rx  r   r   rC   rM   rD   r   	font_namerE   r`  r   rF   r   
font_file2close)r'   r2  r?   core_font_objfontnameglyph_namesoptions	subsettercode_to_glyphoutputttfontstreamcomposite_font_objcid_font_objbfCharrq  code_mappedto_unicode_objcid_system_info_objfont_descriptor_objcid_to_gid_mapcccid_to_gid_map_objfont_file_cs_objr   r{  s                          @@r)   r  zOutputProducer._add_fonts  sE    49?11339L9LMMM ^	 ^	DyF"" y(BBB &% 
 !(#ty8! ! ! !!-999.;#DF++e##0TY00 #k==??t*++a//NNE 		!F!F$2E!F!F!FFF   #*$SWXXX## 
( 
( 
( 
## %.w77	""+"666  ---! ! ! !*.+*;*;*=*=! ! ! !  (((A%{{}} &-#x,& & &" !!"4g>>>.@#DF+  '*&	/%d++	      !!,8886>~6N6N"3 , , , +/+*;*;*=*=  &E;5=))Q.. MMiKiii7d7d7d7dV[Vc7d7d7d0d0diii    "2 6{{  wwv  " ", !!.':::0>"-&3oo#!!"5w???1D.&*i#04X#-!!"5w???/B, #)C#!5!9!.!4!4!6!6 C CIB-0!__N26*14UT\1B1BN26A:..!#!8!8 &6+228<<t& & &" !!"4g>>>2D/#0,#G#G#G !!"2G<<<1A#.

""r+   c                     i }t          | j        j        j                                        d           D ],}|d         dk    r|                     |          ||d         <   -|S )Nc                     | d         S )NrU  r%   )imgs    r)   rV  z,OutputProducer._add_images.<locals>.<lambda>  s
    3s8 r+   rW  usagesr   rU  )r  r   image_cacheimagesrM  
_add_image)r'   r3  r  s      r)   r  zOutputProducer._add_images  sx    I!(//117K7K
 
 
 	D 	DC 8}q  /3s/C/C"3s8,!!r+   c                 0   |d         }|| j         v r| j         |         S d}| j        j        j                                        D ]\  }}||k    r|} n|J t          ||d         |d                   }|                     |d          }|| j         |<   |S )z~
        Returns the PDF object of the ICC profile indexed iccp_i in the FPDF object.
        Adds it if not present.
        iccp_iNdpncs)r   r   r   iccp)r   r   r  icc_profilesr  r   r  )r'   img_infor  iccp_contenticcp_crU  iccp_obj
iccp_pdf_is           r)   _ensure_iccpzOutputProducer._ensure_iccp  s    
 (#T)))'//.;AACC 	 	IFA{{%  ''' !Xe_
 
 
 &&x88
'1V$r+   c                    t          |d                   }d }|                    d          }|dk    r-t          ddt          |d                   dz  dz
   g          }nW|C|                     |          }t          d	t          |          t          d
          dg          }n|dk    r|d         du rd}d|d          d|d          d}t          d|d         |d         |d         ||d         |d         ||	  	        }|                     |d           | j        j	        rId|v rEd|d          }| 
                    |d         |d         dd|d         ||d         d           |_        d|v rct          |d         | j        j        !          }	|                     |	d           |j                            t!          |	j                             |S )"Nr  r  Indexedz/IndexedrK  pal   r   z	/ICCBased0R
DeviceCMYKinvertedTz[1 0 1 0 1 0 1 0]r   dpz /BitsPerComponent bpcr   ImagerD  rA   hf)	r=   r   r   r   r   r   r   r   r   r  smaskz!/Predictor 15 /Colors 1 /Columns 
DeviceGrayr  )rA   r  r  r  r  r  rD  r   )r   r'  r   r   r  r   r   r  r   allow_images_transparencyr  r   r   r   r   r   r   r   )
r'   infor   r   r  r  r   img_objr  
pal_cs_objs
             r)   r  zOutputProducer._add_image  s   4:&&(##)##"\c$u+.>.>!.Ca.G+IJ KK **400J"KZ#c((C#PQQKKL((J4'',JDJJJ4;JJJ&\s)9##E{Cy%

 

 

 	'8,,, 9. 	7d??@T#Y@@B!__cc&c M 
 
GN $$)ety/A  J j(333&&wz}'='=>>>r+   c                     t                      }| j        j                                        D ]/\  }}t	          |          }|                     |d           |||<   0|S )Ngfxstate)r   r    _drawing_graphics_state_registryr  r   r  )r'   r4  
state_dictr  gfxstate_objs        r)   r  zOutputProducer._add_gfxstates  sh    !, $	 J P P R R 	8 	8J'
33LlJ777+7"4((%%r+   c                    d}d\  }}}|r8t          d t          |                                          D                       }|r8t          d t          |                                          D                       }|r+t          d |                                D                       }t          ||||          }|                     |           |S )Nz$[/PDF /Text /ImageB /ImageC /ImageI]rH   c                 D    i | ]\  }}d | t          |j                  S )z/Fr   r   )ri   r   font_objs      r)   rk   z6OutputProducer._add_resources_dict.<locals>.<dictcomp>!  s>       'x !LL'(+"6"6  r+   c                 D    i | ]\  }}d | t          |j                  S )z/Ir  )ri   r   r  s      r)   rk   z6OutputProducer._add_resources_dict.<locals>.<dictcomp>)  s>       &w !LL''*"5"5  r+   c                 D    i | ]\  }}d | t          |j                  S )/r  )ri   r  r  s      r)   rk   z6OutputProducer._add_resources_dict.<locals>.<dictcomp>1  s>       *l JJ 8 8  r+   )r   r   r   r   )rq   r  r  r   r  )	r'   r2  r3  r4  r   r   r   r   resources_objs	            r)   r  z"OutputProducer._add_resources_dict  s#    :&6#h 	 +12E2K2K2M2M+N+N   D  	 *01C1I1I1K1K*L*L   H " 	" .D.J.J.L.L   K %D8
 
 
 	-(((r+   c                     | j         j                                        rdS d}| j         j        D ]}||}|                     |d           |S )zDBuilds a Structure Hierarchy, including image alternate descriptionsNstructure_tree)r   r"  emptyr  )r'   r	  r=  s      r)   r  z"OutputProducer._add_structure_tree=  sf    9#))++ 	4#y/ 	9 	9G#+'.$g'78888##r+   c                     | j         j        sdS d g }}t          | j         j                  D ]2}||}n|                    |           |                     |d           3||fS )N)Nr%   document_outline)r   _outliner   r   r  )r'   r
  r6  r=  s       r)   r  z$OutputProducer._add_document_outlineH  s    y! 	8*.-)$)*<== 	; 	;G'#*  $$W---g'9::::..r+   c                     | j         j        sd S d| j         j         d}t          |          }|                     |           |S )Nu9   <?xpacket begin="ï»¿" id="W5M0MpCehiHzreSzNTczkc9d"?>
z
<?xpacket end="w"?>
)r   xmp_metadatar   r  )r'   xpacketr=  s      r)   r  z OutputProducer._add_xmp_metadataT  sO    y% 	4~tyOe~~~ ))'"""r+   c                    | j         }d }|j        rC	 t          |j        dd          }n*# t          $ r}t	          d|j                   |d }~ww xY wt          t          |dd           t          |dd           t          |dd           t          |dd           t          |dd           t          |d	d           |
          }|                     |           |S )NT)with_tzrQ   zCould not format date: rZ   r[   r\   r]   r^   r_   )rZ   r[   r\   r]   r^   r_   rX   )r   rX   r   	Exceptionr
   rW   rh   r  )r'   r   rX   errorr   s        r)   r  zOutputProducer._add_info\  s   y 	 '(:DRV W W W   #Bd.@BB  $..D)T224400T:t44D)T22T:t44'
 
 
 	(###s   * 
AAAc                     | j         j        r7| j         j        }|                                }|                     |           |S d S r$   )r   r(   get_encryption_objr  )r'   encryption_handlerr=  s      r)   r  zOutputProducer._add_encryptionr  sI    9& 	!%!<(;;==Gg&&&Ntr+   c                     | j         }t          t          |dd           |j        |j        |j                  }|                     |           |S )Nrx   )rx   ry   rz   r{   )r   ru   rh   ry   rz   r{   r  )r'   r   r   s      r)   r  zOutputProducer._add_catalogz  sY    y vt,,(n#6	
 
 
 	+&&&r+   c           	         | j         }||_        ||_        ||_        ||_        |r=t
          j        t
          j        z   }	t          t          |g          |	          |_
        |j        t          v r(t          |j                  gt          |j                 }
n.t          |j                  dddt          |j        dz            g}
t!          |
          |_        |rt%          ddi          |_        |j        rEd |j        D             }t%          dt%          d	t!          |          i          i          |_        d S d S )
Nrc   r   r   d   z/Markedtruec                     g | ]e}|j         	t          |                                                                           d |                                                                 fS )rm   )globally_enclosedr   basenamer*   	file_spec)ri   r1  s     r)   
<listcomp>z4OutputProducer._finalize_catalog.<locals>.<listcomp>  sw       ! 2]335566@@BBll]E\E\E^E^EhEhEjEjll  r+   z/EmbeddedFilesz/Names)r   r|   r   r   r   r	   SIGNATURES_EXISTAPPEND_ONLYra   r   r}   	zoom_modeZOOM_CONFIGSr   r   r   pdf_listr~   rq   r   r  r   )r'   r   r  r  r  r  r	  r
  r   flagszoom_configfile_spec_namess               r)   r  z OutputProducer._finalize_catalog  so    y*';$// 	!2]5NNE$,!3 455% % %K! >\)))**dn-KK )**DNS())K #+;"7"7 	B$,i-@$A$AK! 	 %)%8  O
 !)!8Xx7P7P,Q#R#RS! !K	 	r+   c              #      K   t          | j                  }d V  | j        |xx         t          | j                  |z
  z  cc<   d S r$   )r   r   r  )r'   label	prev_sizes      r)   r(  zOutputProducer._trace_size  sR      $$	*5111S5E5E	5QQ11111r+   c                     t                               d           | j                                        D ].\  }}t                               d|t	          |                     /d S )Nz4Final size summary of the biggest document sections:z- %s: %s)r  debugr  r  _sizeof_fmt)r'   r  section_sizes      r)   r*  z(OutputProducer._log_final_sections_sizes  sg    KLLL#'#E#K#K#M#M 	G 	GE<LLUK,E,EFFFF	G 	Gr+   r$   )r,   r-   r.   __doc__r4   r?  r)  r  r  slicer  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r(  r*  r%   r+   r)   r   r   T  s       \\	" 	" 	"s s sj$ $ $      !&a    ," " "b# b# b#H" " "  ,4 4 4l& & &' ' 'R	$ 	$ 	$
/ 
/ 
/    ,  	 	 	, , ,\ R R ^R
G G G G Gr+   r   Fr  c                     |r|                      ||||          \  }}}}|r|}||z
  |z
  }	n| }||z   }	d||z  dd||z  dd||z  dd|	|z  dd| d          dS )Nzq .2fz 0 0 rm   z cm /IrU  z Do Q)scale_inside_box)
r  ra  yrA   r  keep_aspect_ratioscalepdf_height_to_flipstream_hstream_ys
             r)   stream_content_for_raster_imager    s      7**1aA66
1a %)A-2q5	QY7 	 	E!17 	 	I5	 	$u,5	 	3i	 	 	r+   c           
         d}i }i }d}d}d}t          t          | j                                        d                     }|D ]}||         }	|	|dz   k    r|j        |k    rv|j        ||         d         k    r/|                    |g                               |j                   n(||                                          |}||j        g||<   d}d||<   nB|r|	}|j        g||<   n ||                             |j                   d}n|	}|j        g||<   d}|	}|j        }d}
d}d}|}t          |                                          D ]a\  }}t          |          }||k    r+|s)||vs|d	k     r||v r||= ||
         ||         z   ||
<   ||= n|}
||z   }||v r|d	k    }||= |dz  }_d}bg }t          |                                          D ]\  }}t          t          |                    dk    r8|                    d
| d
|t          |          z   dz
   d
|d                     ]|                    d
| dd

                    d |D                        d           dd
                    |           dS )Nr   r   Fc                     | d         S r   r%   )items    r)   rV  z!_tt_font_widths.<locals>.<lambda>  s
    tAw r+   rW  r   Tr  rm   z [ c              3   N   K   | ] }t          t          |                    V  !d S r$   )r   r   )ri   r  s     r)   rb  z"_tt_font_widths.<locals>.<genexpr>  s.      '@'@CFF'@'@'@'@'@'@r+   z ]
[r}  r   )dictr  r   r  glyph_width
setdefaultr   popr   setr   )r   rangeidrange_range_intervalprevcid	prevwidthintervalrl  rq  
cid_mappedprevknextkprevintrir$  wscwsrA   s                     r)   r  r    s   GFNGIH &**,,2F2FGGGHHF & &E]
'A+&& I--$w(:::%%gr2299%:KLLLL7O'')))%G'0%2C&DF7O*.w'' >(G','8&9F7OO7O**5+<===  G$01F7OH%		EEG	B''  2"gg::g:1B;;#''BwwqE"5MF1I5F5Mq		EC77AgG1QJEEGG
A'' H H2s2ww<<1HH666QR[1_66r!u667777HHFFFsxx'@'@R'@'@'@@@FFFGGGGrwwqzzr+   c                 "    | \  }}d|dd|ddS )Nz[0 0 r  rm   r   r%   )r   r   r   s      r)   rH  rH    s+    $Hi28222)22222r+   Bc                 b    dD ]%}t          |           dk     r| d| | c S | dz  } &| dd| S )N)r}  KiMiGiTiPiEiZii   z3.1fz.1fYi)abs)numsuffixunits      r)   r  r  !  sa    >  s88d??...f.....t!!!!!!r+   )Fr   N)r-  )Cr  loggingcollectionsr   r   
contextlibr   ior   annotationsr   enumsr	   errorsr
   image_datastructuresr   outliner   signr   r   syntaxr   r   r   r   r   r   r   r   rq   r   r  r   r   	fontToolsr   r  endesiver   ImportError	getLoggerr,   r  r  r"   r0   r8   rM   rW   ra   ru   r   r   r   r   r   r   r   r   r   r   r  r  rH  r  r%   r+   r)   <module>rJ     s     0 0 0 0 0 0 0 0 % % % % % %       & & & & & &             ! ! ! ! ! ! 1 1 1 1 1 1 ' ' ' ' ' ' ) ) ) ) ) ) ) )                  9 8 8 8 8 8 2 2 2 2 2 2 ' ' ' ' ' ' ( ( ( ( ( (   FFF 
	8	$	$ ")        
* * * * *  * * *' ' ' ' 'i ' ' '     I   + + + + +i + + +*2 2 2 2 2 2 2 2% % % % % % % %(' ' ' ' '9 ' ' '% % % % %$ % % %# # # # #% # # #' ' ' ' '! ' ' 'T) ) ) ) )$ ) ) )*0> 0> 0> 0> 0>i 0> 0> 0>f    9   ? ? ? ? ?9 ? ? ?% % % % %( % % %Ph	G h	G h	G h	G h	G h	G h	G h	Gb 
 
   2A A AH3 3 3
" " " " " "s   ,A3 3A=<A=