
    BhB                     H   S r SSKrSSKrSSKJr  SSKJr  SSKJrJ	r	  SSK
JrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SSKJr  SSKJr  SSKJr  SSKJrJr  SSK J!r!  SSK"J#r#  SSK$J%r%J&r&  SSK'J(r(  SSK)J*r*   " S S5      r+ " S S5      r, " S S5      r- " S S5      r.g)zDocument generation management.    N)md5)Path   )CSSDEFAULT_OPTIONS)gather_anchorsmake_page_bookmark_tree)get_all_computed_styles)CounterStyle)TargetCollector)	draw_pagestacked)build_formatting_structure)get_html_metadata)get_image_from_uri)LayoutContextlayout_document)PROGRESS_LOGGER)Matrix)VARIANTSgenerate_pdf)generate_rdf_metadata)FontConfigurationc                   (    \ rS rSrSrS rSS jrSrg)Page   zsRepresents a single rendered page.

Should be obtained from :attr:`Document.pages` but not
instantiated directly.

c                 z   UR                  5       U l        UR                  5       U l        S Vs0 s H  nX!R                  SU 3   R
                  _M!     snU l        / U l        / U l        0 U l	        S / 0U l
        [        XR                  U R                  U R                  U R                  5        Xl        g s  snf )N)toprightbottomleftbleed_)margin_widthwidthmargin_heightheightstylevaluebleed	bookmarkslinksanchorsformsr   	_page_box)selfpage_boxsides      E/var/www/html/env/lib/python3.13/site-packages/weasyprint/document.py__init__Page.__init__!   s    **,
 ,,. ;<: ..6$1777:<
  
  BZ
xtzz4>>4::V!O<s   &B8c                     [        U5         UR                  X"S9  [        U R                  U5        SSS5        g! , (       d  f       g= f)z!Paint the page into the PDF file.adN)r   	transformr   r.   )r/   streamscales      r2   paint
Page.paintS   s2    V_u.dnnf- __s	   &;
A	)r.   r,   r)   r*   r-   r&   r+   r$   N)r   )__name__
__module____qualname____firstlineno____doc__r3   r<   __static_attributes__     r2   r   r      s    0"d.rE   r   c                   8    \ rS rSrSrSSSSSSSSSS\4S jrSrg)DocumentMetadataZ   zxMeta-information belonging to a whole :class:`Document`.

New attributes may be added in future versions of WeasyPrint.
Nc                     Xl         U=(       d    / U l        X0l        U=(       d    / U l        XPl        X`l        Xpl        U=(       d    / U l        Xl        U
=(       d    0 U l	        Xl
        g N)titleauthorsdescriptionkeywords	generatorcreatedmodifiedattachmentslangcustomr   )r/   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   r   s               r2   r3   DocumentMetadata.__init___   sk     

 }" '
 !B
 #  ! '," 	 l &;"rE   )rR   rL   rP   rT   rM   r   rO   rN   rS   rQ   rK   )r>   r?   r@   rA   rB   r   r3   rC   rD   rE   r2   rG   rG   Z   s(     "DdT$4?T2;rE   rG   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)	DiskCache   zDict-like storing images content on disk.

Bytestring values are stored on disk. Other lightweight Python objects
(i.e. RasterImage instances) are still stored in memory.

c                     [        U5      U l        U R                  R                  SSS9  0 U l        [	        5       U l        g )NT)parentsexist_ok)r   _pathmkdir_memory_cacheset_disk_paths)r/   folders     r2   r3   DiskCache.__init__   s6    &\


55rE   c                 l    [        UR                  5       SS9R                  5       nU R                  U-  $ )NF)usedforsecurity)r   encode	hexdigestr\   )r/   keydigests      r2   _path_from_keyDiskCache._path_from_key   s,    SZZ\59CCEzzF""rE   c                 |    XR                   ;   a  U R                   U   $ U R                  U5      R                  5       $ rJ   )r^   ri   
read_bytesr/   rg   s     r2   __getitem__DiskCache.__getitem__   s9    $$$%%c**&&s+6688rE   c                     [        U[        5      (       a>  U R                  U5      nU R                  R	                  U5        UR                  U5        g X R                  U'   g rJ   )
isinstancebytesri   r`   addwrite_bytesr^   )r/   rg   r(   paths       r2   __setitem__DiskCache.__setitem__   sO    eU##&&s+D  &U#&+s#rE   c                 j    XR                   ;   =(       d    U R                  U5      R                  5       $ rJ   )r^   ri   existsrm   s     r2   __contains__DiskCache.__contains__   s/    %%% .$++-	/rE   c                      U R                    H  nUR                  SS9  M     U R                  R                  5         g ! [         a     g f = f)NT)
missing_ok)r`   unlinkr\   rmdir	Exception)r/   ru   s     r2   __del__DiskCache.__del__   sG    	((t, )JJ 		s   <? 
AA)r`   r^   r\   N)r>   r?   r@   rA   rB   r3   ri   rn   rv   rz   r   rC   rD   rE   r2   rW   rW      s%    !#9,/
rE   rW   c                   f    \ rS rSrSr\S 5       r\S 5       rS rSS jr	SS jr
SS	 jrSS
 jrSrg)Document   a  A rendered document ready to be painted in a pydyf stream.

Typically obtained from :meth:`HTML.render() <weasyprint.HTML.render>`, but
can also be instantiated directly with a list of :class:`pages <Page>`, a
set of :class:`metadata <DocumentMetadata>`, a :func:`url_fetcher
<weasyprint.default_url_fetcher>` function, and a :class:`font_config
<weasyprint.text.fonts.FontConfiguration>`.

c                    [        5       n/ n/ nUS   nUc  0 nO&[        U[        [        45      (       d  [        U5      nUS   =(       d    /  H9  n	[	        U	S5      (       d  [        XR                  X#S9n	UR                  U	5        M;     [        XUS   X#XeUS   5      n
[        R                  " [        UUR                  US9n[        R                  " S5        [        XX#U5      nU$ )	Ncachestylesheetsmatcher)guess
media_typefont_configcounter_stylepresentational_hints	pdf_forms)r   url_fetcheroptionsz&Step 4 - Creating formatting structure)r   rq   dictrW   hasattrr   r   appendr
   	functoolspartialoriginal_get_image_from_urir   r   infor   )clshtmlr   r   r   target_collector
page_rulesuser_stylesheetsr   css	style_forr   contexts                r2   _build_layout_contextDocument._build_layout_context   s    *,
 =EED)#455e$E=)/R/C3	**// +J ##C( 0 ,G,B$C
K "	 '..'u(('; 	EF; rE   c           
         Uc
  [        5       nUc
  [        5       nU R                  XX45      n[        UR                  UR
                  UR                  UR                  UR                  UUR                  5      n[        XU5      nU " U Vs/ s H  n[        U5      PM     sn[        S0 [        U5      D6UR                  U5      n	Xl        U	$ s  snf )NrD   )r   r   r   r   etree_elementr   r   base_urlr   	footnotesr   r   rG   r   r   _html)
r   r   r   r   r   r   root_box
page_boxesr0   	renderings
             r2   _renderDocument._render   s    +-K (NM++}7 . 1 173M3MMM733]
 %TW=
,67JT(^J77067k+	 	 8s   Cc                 B    Xl         X l        X0l        0 U l        X@l        g rJ   )pagesmetadatar   fontsr   )r/   r   r   r   r   s        r2   r3   Document.__init__  s'    
 ! ' 

 'rE   Nc                     Uc  U R                   R                  nSS 0X'   U H  nSU0X'   U R                  X5        M     g )Nparent)r   r   build_element_structure)r/   	structurer   childs       r2   r    Document.build_element_structure  sI      JJ44M(0$'7I$"E (-8I((: #rE   c                     US:X  a  U R                   nO [        U[        5      (       d  [        U5      n[        U 5      " XR                  U R
                  U R                  5      $ )a  Take a subset of the pages.

:type pages: :term:`iterable`
:param pages:
    An iterable of :class:`Page` objects from :attr:`pages`.
:return:
    A new :class:`Document` object.

Examples:

Write two PDF files for odd-numbered and even-numbered pages::

    # Python lists count from 0 but pages are numbered from 1.
    # [::2] is a slice of even list indexes but odd-numbered pages.
    document.copy(document.pages[::2]).write_pdf('odd_pages.pdf')
    document.copy(document.pages[1::2]).write_pdf('even_pages.pdf')

Combine multiple documents into one PDF file,
using metadata from the first::

    all_pages = [p for doc in documents for p in doc.pages]
    documents[0].copy(all_pages).write_pdf('combined.pdf')

all)r   rq   listtyper   r   r   )r/   r   s     r2   copyDocument.copy  sT    2 E>JJEE4((KEDz==$"2"2D4D4DF 	FrE   c           	          / n/ nU/nSn[        U R                  5       H;  u  pxU(       a  [        X* UR                  U-  S9n	O	[        XS9n	[	        XXVXy5      nM=     U$ )a  Make a tree of all bookmarks in the document.

:param float scale:
    Zoom scale.
:param bool transform_pages:
    A boolean defining whether the default PDF page transformation
    matrix has to be applied to bookmark coordinates, setting the
    bottom-left corner as the origin.
:return: A list of bookmark subtrees.
    A subtree is ``(label, target, children, state)``. ``label`` is
    a string, ``target`` is ``(page_number, x, y)``  and ``children``
    is a list of child subtrees.

r   )r7   r8   fr6   )	enumerater   r   r&   r	   )
r/   r;   transform_pagesrootskipped_levelslast_by_depthprevious_levelpage_numberpagematrixs
             r2   make_bookmark_treeDocument.make_bookmark_tree?  so     
 !*4::!6K%6T[[55HI%14m%N "7 rE   c                 n   [         R                  " 5       nUR                  U5        UnUS   =n(       a;  [        U   u  pxSU;   a  US   (       d  US   US'   SU;   a  US   (       d  US   US'   [	        XU40 UD6n	U(       a  U" X	5        US   n
US   (       + nUS   nUc7  [
        R                  " 5       nU	R                  XX5        UR                  5       $ [        US5      (       a  U	R                  XX5        g[        US	5       nU	R                  XX5        SSS5        g! , (       d  f       g= f)
a  Paint the pages in a PDF file, with metadata.

:type target:
    :class:`str`, :class:`pathlib.Path` or :term:`file object`
:param target:
    A filename where the PDF file is generated, a file object, or
    :obj:`None`.
:param float zoom:
    The zoom factor in PDF units per CSS units.  **Warning**:
    All CSS units are affected, including physical units like
    ``cm`` and named sizes like ``A4``.  For values other than
    1, the physical CSS units will thus be "wrong".
:type finisher: :term:`callable`
:param finisher:
    A finisher function or callable that accepts the document and a
    :class:`pydyf.PDF` object as parameters. Can be passed to perform
    post-processing on the PDF right before the trailer is written.
:param options:
    The ``options`` parameter includes by default the
    :data:`weasyprint.DEFAULT_OPTIONS` values.
:returns:
    The PDF as :obj:`bytes` if ``target`` is not provided or
    :obj:`None`, otherwise :obj:`None` (the PDF is written to
    ``target``).

pdf_variantversionpdf_version
identifierpdf_identifieruncompressed_pdfNwritewb)r   r   updater   r   ioBytesIOr   getvaluer   open)r/   targetzoomfinisherr   new_optionsvariant_
propertiespdfr   compressr   outputfds                  r2   	write_pdfDocument.write_pdf`  s,   6 &**,7# m,,7,$W-MAJ&w}/E)3I)>&z)':J2K,6|,D()499T-.
122-(>ZZ\FIIfz<??$$67##IIfz<fd#r		"z< $##s   
D&&
D4)r   r   r   r   r   rJ   )r   )r   F)Nr   N)r>   r?   r@   rA   rB   classmethodr   r   r3   r   r   r   r   rC   rD   rE   r2   r   r      sK      8  .'(;F@B9=rE   r   )/rB   r   r   hashlibr   pathlibr    r   r   r,   r   r	   r   r
   css.countersr   css.targetsr   drawr   r   formatting_structure.buildr   r   r   imagesr   r   layoutr   r   loggerr   r   r   r   r   r   pdf.metadatar   
text.fontsr   r   rG   rW   r   rD   rE   r2   <module>r      sn    %  	   " < ( & ( $ B # E 2 #  ' / )>. >.B7; 7;t, ,^V= V=rE   