o
    ½Ùøiq5  ã                   @   s  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 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„ dƒZ*G dd„ dƒZ+G dd„ dƒZ,dS )zDocument generation management.é    N)Úmd5)ÚPathé   )ÚCSSÚDEFAULT_OPTIONS)Úgather_anchorsÚmake_page_bookmark_tree)Úget_all_computed_styles)ÚCounterStyle)ÚTargetCollector)Ú	draw_page)Úbuild_formatting_structure)Úget_html_metadata)Úget_image_from_uri)ÚLayoutContextÚlayout_document)ÚPROGRESS_LOGGER)ÚMatrix)ÚVARIANTSÚgenerate_pdf)ÚDocumentMetadata)ÚFontConfigurationc                   @   s"   e Zd ZdZdd„ Zddd„ZdS )	ÚPagezRepresents a single rendered page.

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

    c                    sf   ˆ   ¡ | _ˆ  ¡ | _‡ fdd„dD ƒ| _g | _g | _i | _d g i| _t	ˆ | j| j| j| jƒ ˆ | _
d S )Nc                    s    i | ]}|ˆ j d |› � j“qS )Úbleed_)ÚstyleÚvalue)Ú.0Úside©Úpage_box© ú`/home/develop/edictos_py/central_edictos/env/lib/python3.10/site-packages/weasyprint/document.pyÚ
<dictcomp>*   s    ÿÿz!Page.__init__.<locals>.<dictcomp>)ÚtopÚrightÚbottomÚleft)Úmargin_widthÚwidthÚmargin_heightÚheightÚbleedÚ	bookmarksÚlinksÚanchorsÚformsr   Ú	_page_box)Úselfr   r    r   r!   Ú__init__!   s   


þ	

zPage.__init__r   c                 C   sH   |  ¡ � |j||d� t| j|ƒ W d  ƒ dS 1 sw   Y  dS )z!Paint the page into the PDF file.©ÚaÚdN)ÚstackedÚ	transformr   r0   )r1   ÚstreamÚscaler    r    r!   ÚpaintS   s   
"þz
Page.paintN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r:   r    r    r    r!   r      s    2r   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú	DiskCachez¸Dict-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                 C   s,   t |ƒ| _| jjddd� i | _tƒ | _d S )NT)ÚparentsÚexist_ok)r   Ú_pathÚmkdirÚ_memory_cacheÚsetÚ_disk_paths)r1   Úfolderr    r    r!   r2   b   s   
zDiskCache.__init__c                 C   s   t | ¡ dd� ¡ }| j| S )NF)Úusedforsecurity)r   ÚencodeÚ	hexdigestrB   )r1   ÚkeyÚdigestr    r    r!   Ú_path_from_keyh   s   
zDiskCache._path_from_keyc                 C   s"   || j v r
| j | S |  |¡ ¡ S ©N)rD   rM   Ú
read_bytes©r1   rK   r    r    r!   Ú__getitem__l   s   

zDiskCache.__getitem__c                 C   s<   t |tƒr|  |¡}| j |¡ | |¡ d S || j|< d S rN   )Ú
isinstanceÚbytesrM   rF   ÚaddÚwrite_bytesrD   )r1   rK   r   Úpathr    r    r!   Ú__setitem__r   s
   

zDiskCache.__setitem__c                 C   s   || j v p|  |¡ ¡ S rN   )rD   rM   ÚexistsrP   r    r    r!   Ú__contains__z   s   
þzDiskCache.__contains__c                 C   s>   z| j D ]}|jdd� q| j ¡  W d S  ty   Y d S w )NT)Ú
missing_ok)rF   ÚunlinkrB   ÚrmdirÚ	Exception)r1   rV   r    r    r!   Ú__del__   s   
þzDiskCache.__del__N)
r;   r<   r=   r>   r2   rM   rQ   rW   rY   r^   r    r    r    r!   r?   Z   s    r?   c                   @   sN   e Zd ZdZedd„ ƒZedd„ ƒZdd„ Zdd	d
„Zddd„Z	ddd„Z
dS )Ú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 :class:`url_fetcher
    <weasyprint.urls.URLFetcher>`, and a :class:`font_config
    <weasyprint.text.fonts.FontConfiguration>`.

    c                 C   sÌ   t ƒ }g }g }g }	|d }
|
d u ri }
nt|
ttfƒst|
ƒ}
|d p$g D ]}t|dƒs6t||j|||d�}|	 |¡ q%t||	|d |||||||d ƒ
}t	j
t|
|j|d�}t d¡ t|||||ƒ}|S )	NÚcacheÚstylesheetsÚmatcher)ÚguessÚ
media_typeÚfont_configÚcounter_styleÚcolor_profilesÚpresentational_hintsÚ	pdf_forms)r`   Úurl_fetcherÚoptionsz&Step 4 - Creating formatting structure)r   rR   Údictr?   Úhasattrr   rd   Úappendr	   Ú	functoolsÚpartialÚoriginal_get_image_from_urirj   r   Úinfor   )ÚclsÚhtmlre   rf   rg   rk   Útarget_collectorÚ
page_rulesÚlayersÚuser_stylesheetsr`   ÚcssÚ	style_forr   Úcontextr    r    r!   Ú_build_layout_context”   s@   
ý
ýþ
þzDocument._build_layout_contextc           
      C   sš   |d u rt ƒ }|d u rtƒ }|d u ri }|  |||||¡}t|j|j|j|j|j||j	ƒ}t
|||ƒ}| dd„ |D ƒtdi t|ƒ¤Ž|j||ƒ}	||	_|	S )Nc                 S   s   g | ]}t |ƒ‘qS r    )r   )r   r   r    r    r!   Ú
<listcomp>É   s    z$Document._render.<locals>.<listcomp>r    )r   r
   r|   r   Úetree_elementrz   r   Úbase_urlru   Ú	footnotesr   r   r   rj   Ú_html)
rs   rt   re   rf   rg   rk   r{   Úroot_boxÚ
page_boxesÚ	renderingr    r    r!   Ú_render´   s,   
ÿ
ýýzDocument._renderc                 C   s(   || _ || _|| _i | _|| _|| _d S rN   )ÚpagesÚmetadatarj   Úfontsre   rg   )r1   r†   r‡   rj   re   rg   r    r    r!   r2   Ï   s   
zDocument.__init__Úallc                 C   s>   |dkr| j }n	t|tƒst|ƒ}t| ƒ|| j| 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')

        r‰   )r†   rR   ÚlistÚtyper‡   rj   re   rg   )r1   r†   r    r    r!   Úcopyã   s   
þzDocument.copyr   Fc           
      C   sd   g }g }|g}d}t | jƒD ]!\}}|r t|| |j| d�}	nt||d�}	t||||||	ƒ}q|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   )r4   r5   Úfr3   )Ú	enumerater†   r   r*   r   )
r1   r9   Útransform_pagesÚrootÚskipped_levelsÚlast_by_depthÚprevious_levelÚpage_numberÚpageÚmatrixr    r    r!   Úmake_bookmark_tree  s   þzDocument.make_bookmark_treeNc                 K   s(  t  ¡ }| |¡ |}|d  }r3t| \}}d|v r%|d s%|d |d< d|v r3|d s3|d |d< t| ||fi |¤Ž}	|rD|| |	ƒ |d }
|d  }|d }|du ret ¡ }|	 |||
|¡ | ¡ S t	|dƒrt|	 |||
|¡ dS t
|d	ƒ�}|	 |||
|¡ W d  ƒ dS 1 s�w   Y  dS )
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ž   Úgetvaluerm   Úopen)r1   ÚtargetÚzoomÚfinisherrk   Únew_optionsÚvariantÚ_Ú
propertiesÚpdfr›   Úcompressr™   ÚoutputÚfdr    r    r!   Ú	write_pdf%  s0   



"ÿzDocument.write_pdf)r‰   )r   F)Nr   N)r;   r<   r=   r>   Úclassmethodr|   r…   r2   rŒ   r—   r°   r    r    r    r!   r_   ‰   s    




!!r_   )-r>   ro   r¡   Úhashlibr   Úpathlibr   Ú r   r   r.   r   r   ry   r	   Úcss.countersr
   Úcss.targetsr   Údrawr   Úformatting_structure.buildr   rt   r   Úimagesr   rq   Úlayoutr   r   Úloggerr   r–   r   r¬   r   r   Úpdf.metadatar   Ú
text.fontsr   r   r?   r_   r    r    r    r!   Ú<module>   s.    A/