o
    Õj“  ã                   @   sP   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ G d	d
„ d
eƒZ	dS )ak  
Objects representing PDF path (stroke and filling) extracted by ``page.get_drawings()``.

This method is new since ``PyMuPDF`` 1.18.0, with both pdf raw path and annotations like Line,
Square and Highlight considered.

* https://pymupdf.readthedocs.io/en/latest/page.html#Page.get_drawings
* https://pymupdf.readthedocs.io/en/latest/faq.html#extracting-drawings
é    Né   )ÚImagesExtractor)Úlazyproperty)Ú
Collectioné   )ÚPathc                   @   sp   e Zd ZdZdefdd„Zedd„ ƒZedd„ ƒZ	d	d
„ Z
dd„ Z					ddededededef
dd„ZdS )ÚPathszA collection of paths.Úrawsc                 C   sB   dd| j j| j jf}|D ]}t|ƒ}|j |¡sq|  |¡ q| S )z>Initialize paths from raw data get by ``page.get_drawings()``.r   )ÚparentÚwidthÚheightr   ÚbboxÚ
intersectsÚappend)Úselfr	   ÚrectÚrawÚpath© r   úU/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/pdf2docx/shape/Paths.pyÚrestore   s   zPaths.restorec                 C   s"   t  ¡ }| jD ]}||jO }q|S )N)ÚfitzÚRectÚ
_instancesr   )r   r   Úinstancer   r   r   r       s   z
Paths.bboxc                 C   s   | j D ]}|js dS qdS )z@It is iso-oriented when all contained segments are iso-oriented.FT)r   Úis_iso_oriented)r   r   r   r   r   r   '   s   
zPaths.is_iso_orientedc                 C   s4   | j sdS | ¡ }| j D ]}| |¡ q| ¡  dS )zePlot paths for debug purpose.

        Args:
            page (fitz.Page): ``PyMuPDF`` page.
        N)r   Z	new_shapeÚplotÚcommit)r   ÚpageZcanvasr   r   r   r   r   /   s   
z
Paths.plotc                 C   s*   g }| j D ]}|jsq| | ¡ ¡ q|S )zˆConvert contained paths to ISO strokes or rectangular fills.

        Returns:
            list: A list of ``Shape`` raw dicts.
        )r   r   ÚextendÚ	to_shapes)r   Zshapesr   r   r   r   r    <   s
   
zPaths.to_shapesé   r   ç      @Úmin_svg_gap_dxÚmin_svg_gap_dyÚmin_wÚmin_hÚclip_image_res_ratioc              	   C   s   g }| j r| |  ¡ ¡ |g fS g }t| jjƒ}| ||||¡}	dtdtfdd„}
dd„ |	D ƒ}| j	D ] }t
|	|ƒD ]\\}}}|j |¡rR|
||ƒsP| |¡  nq:q3t
|	|ƒD ]2\\}}}|j r}| | ¡ ¡ |D ]}| |jt |¡d|d¡ qkqY| |jt |¡d|d¡ qY||fS )	a¶  Convert paths to iso-oriented shapes or images. The semantic type of path is either
        table/text style or vector graphic. This method is to:
        * detect svg regions -> exist at least one non-iso-oriented path
        * convert svg to bitmap by clipping page
        * convert the rest paths to iso-oriented shapes for further table/text style parsing

        Args:
            min_svg_gap_dx (float): Merge svg if the horizontal gap is less than this value.
            min_svg_gap_dy (float): Merge svg if the vertical gap is less than this value.
            min_w (float): Ignore contours if the bbox width is less than this value.
            min_h (float): Ignore contours if the bbox height is less than this value.
            clip_image_res_ratio (float, optional): Resolution ratio of clipped bitmap.
                Defaults to 3.0.

        Returns:
            tuple: (list of shape raw dict, list of image raw dict).
        r   Úcontoursc                 S   s&   |D ]}t  |¡ | j¡r dS qdS )NTF)r   r   Úcontainsr   )r   r(   r   r   r   r   Úcontained_in_inner_contoursq   s   z?Paths.to_shapes_and_images.<locals>.contained_in_inner_contoursc                 S   s   g | ]}t ƒ ‘qS r   )r   )Ú.0Ú_r   r   r   Ú
<listcomp>w   s    z.Paths.to_shapes_and_images.<locals>.<listcomp>T)r   Zrm_imager'   )r   r   r    r   r
   Zpage_engineZdetect_svg_contoursr   Úlistr   Úzipr   r   r   Zclip_page_to_dictr   r   )r   r#   r$   r%   r&   r'   Z
iso_shapesZimagesZieÚgroupsr*   Zgroup_pathsr   r   Zinner_bboxesÚpathsZsvg_bboxr   r   r   Úto_shapes_and_imagesJ   s<   
þ€
þÿ
þzPaths.to_shapes_and_imagesN)r!   r!   r   r   r"   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   r   r   r   Úpropertyr   r   r    Úfloatr2   r   r   r   r   r      s2    

ûÿþýüûr   )
r6   r   Zimage.ImagesExtractorr   Zcommon.sharer   Zcommon.Collectionr   r   r   r   r   r   r   Ú<module>   s    
