
    ,gB                     H   d Z ddlZddl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 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 ddlmZ ddlmZ ddlmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*  G d d      Z+ G d d      Z, G d d      Z- G d d      Z.y)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                       e Zd ZdZd ZddZy)PagezRepresents a single rendered page.

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

    c                 z   |j                         | _        |j                         | _        dD ci c]  }||j                  d|    j
                  ! c}| _        g | _        g | _        i | _	        d g i| _
        t        || j                  | j                  | j                  | j                         || _        y c c}w )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      k/home/viktor/gitlab-persoonlijk/factuur-applicatie/venv/lib/python3.12/site-packages/weasyprint/document.py__init__zPage.__init__!   s    **,
 ,,. ;< (..6$1777<
  
  BZ
xtzz4>>4::V!O<s   $B8c                     t        |      5  |j                  ||       t        | j                  |       ddd       y# 1 sw Y   yxY w)z!Paint the page into the PDF file.adN)r   	transformr   r-   )r.   streamscales      r1   paintz
Page.paintS   s?    V_ 	.u.dnnf-	. 	. 	.s	   *?AN)r   )__name__
__module____qualname____doc__r2   r:        r1   r   r      s    0"d.r@   r   c                   .    e Zd ZdZddddddddddefdZy)DocumentMetadatazMeta-information belonging to a whole :class:`Document`.

    New attributes may be added in future versions of WeasyPrint.
    Nc                     || _         |xs g | _        || _        |xs g | _        || _        || _        || _        |xs g | _        |	| _        |
xs i | _	        || _
        y N)titleauthorsdescriptionkeywords	generatorcreatedmodifiedattachmentslangcustomr   )r.   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r   s               r1   r2   zDocumentMetadata.__init___   sy     

 }" '
 !B
 #  ! '," 	 l &;"r@   )r;   r<   r=   r>   r   r2   r?   r@   r1   rB   rB   Z   s(     "DdT$4?T2;r@   rB   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)		DiskCachezDict-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                     t        |      | _        | j                  j                  dd       i | _        t	               | _        y )NT)parentsexist_ok)r   _pathmkdir_memory_cacheset_disk_paths)r.   folders     r1   r2   zDiskCache.__init__   s6    &\


55r@   c                 r    t        |j                         d      j                         }| j                  |z  S )NF)usedforsecurity)r   encode	hexdigestrT   )r.   keydigests      r1   _path_from_keyzDiskCache._path_from_key   s,    SZZ\59CCEzzF""r@   c                 z    || j                   v r| j                   |   S | j                  |      j                         S rD   )rV   r`   
read_bytesr.   r^   s     r1   __getitem__zDiskCache.__getitem__   s;    $$$$%%c**&&s+6688r@   c                     t        |t              r>| j                  |      }| j                  j	                  |       |j                  |       y || j                  |<   y rD   )
isinstancebytesr`   rX   addwrite_bytesrV   )r.   r^   r'   paths       r1   __setitem__zDiskCache.__setitem__   sN    eU#&&s+D  &U#&+Ds#r@   c                 `    || j                   v xs | j                  |      j                         S rD   )rV   r`   existsrc   s     r1   __contains__zDiskCache.__contains__   s1    4%%% .$++-	/r@   c                     	 | j                   D ]  }|j                  d        | j                  j                          y # t        $ r Y y w xY w)NT)
missing_ok)rX   unlinkrT   rmdir	Exception)r.   rj   s     r1   __del__zDiskCache.__del__   sL    	(( -t,-JJ 		s   =A   	AAN)
r;   r<   r=   r>   r2   r`   rd   rk   rn   rt   r?   r@   r1   rP   rP      s%    !#9,/
r@   rP   c                   V    e Zd ZdZed        Zed        Zd Zd
dZddZ	ddZ
dd	Zy)Documenta  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                    t               }g }g }|d   }|i }n!t        |t        t        f      st        |      }|d   xs g D ]8  }	t	        |	d      st        |	|j                  ||      }	|j                  |	       : t        |||d   |||||d         }
t        j                  t        ||j                  |      }t        j                  d       t        |
||||      }|S )	Ncachestylesheetsmatcher)guess
media_typefont_configcounter_stylepresentational_hints	pdf_forms)rx   url_fetcheroptionsz&Step 4 - Creating formatting structure)r   rf   dictrP   hasattrr   r|   appendr
   	functoolspartialoriginal_get_image_from_urir   r   infor   )clshtmlr}   r~   r   target_collector
page_rulesuser_stylesheetsrx   css	style_forr   contexts                r1   _build_layout_contextzDocument._build_layout_context   s   *,
 =EED)#45e$E=)/R 	)C3	*$// +=J ##C(	) ,"G,B$C
4DK "	 '..'u(('; 	EF); r@   c           
         |
t               }|
t               }| j                  ||||      }t        |j                  |j
                  |j                  |j                  |j                  ||j                        }t        |||      } | |D cg c]  }t        |       c}t        di t        |      |j                  |      }	||	_        |	S c c}w )Nr?   )r   r   r   r   etree_elementr   r   base_urlr   	footnotesr   r   rB   r   r   _html)
r   r   r}   r~   r   r   root_box
page_boxesr/   	renderings
             r1   _renderzDocument._render   s    +-K (NM+++}g7 . 1 173M3MMM733]
 %T8W=
,67T(^77067k+	 		 8s   Cc                 J    || _         || _        || _        i | _        || _        y rD   )pagesmetadatar   fontsr}   )r.   r   r   r   r}   s        r1   r2   zDocument.__init__  s/    
 ! ' 

 'r@   Nc                     || j                   j                  }dd i||<   |D ]  }d|i||<   | j                  ||        y )Nparent)r   r   build_element_structure)r.   	structurer   childs       r1   r   z Document.build_element_structure  sT      JJ44M(0$'7Im$" 	;E (-8Ie((E:	;r@   c                     |dk(  r| j                   }nt        |t              st        |      } t        |       || j                  | j
                  | j                        S )aP  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   rf   listtyper   r   r}   )r.   r   s     r1   copyzDocument.copy  sU    2 E>JJEE4(KEtDz4==$"2"2D4D4DF 	Fr@   c           	          g }g }|g}d}t        | j                        D ]A  \  }}|rt        || |j                  |z        }	nt        ||      }	t	        ||||||	      }C |S )aE  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   )r5   r6   fr4   )	enumerater   r   r%   r	   )
r.   r9   transform_pagesrootskipped_levelslast_by_depthprevious_levelpage_numberpagematrixs
             r1   make_bookmark_treezDocument.make_bookmark_tree?  s     
 !*4::!6 	%K%E6T[[55HI%514nm^V%N	% r@   c                 .   t        j                         }|j                  |       |}|d   x}r.t        |   \  }}d|v r|d   s|d   |d<   d|v r|d   s|d   |d<   t	        | ||fi |}	|r	 || |	       |d   }
|d    }|d   }|8t        j                         }|	j                  |||
|       |j                         S t        |d      r|	j                  |||
|       yt        |d	      5 }|	j                  |||
|       ddd       y# 1 sw Y   yxY w)
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                  r1   	write_pdfzDocument.write_pdf`  sD   6 &**,7# m,,7,$W-MAzJ&w}/E)3I)>&z)':J2K,6|,D()499T3-.
122-(>ZZ\FIIfgz8<??$$67#IIfgz8<fd# =r		"gz8<= = =s   -DDrD   )r   )r   F)Nr   N)r;   r<   r=   r>   classmethodr   r   r2   r   r   r   r   r?   r@   r1   rv   rv      sK      8  .'(;F@B9=r@   rv   )/r>   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   rB   rP   rv   r?   r@   r1   <module>r      sn    %  	   " < ( & ( $ B # E 2 #  ' / )>. >.B7; 7;t, ,^V= V=r@   