o
    e5                     @   sl   d 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 G dd de	ZdS )a%  Page object parsed with PDF raw dict.

In addition to base structure described in :py:class:`~pdf2docx.page.RawPage`, 
some new features, e.g. sections, table block, are also included. 
Page elements structure:

* :py:class:`~pdf2docx.page.Page` >> :py:class:`~pdf2docx.layout.Section` >> :py:class:`~pdf2docx.layout.Column`  
    * :py:class:`~pdf2docx.layout.Blocks`
        * :py:class:`~pdf2docx.text.TextBlock` >> 
          :py:class:`~pdf2docx.text.Line` >> 
          :py:class:`~pdf2docx.text.TextSpan` / :py:class:`~pdf2docx.image.ImageSpan` >>
          :py:class:`~pdf2docx.text.Char`
        * :py:class:`~pdf2docx.table.TableBlock` >>
          :py:class:`~pdf2docx.table.Row` >> 
          :py:class:`~pdf2docx.table.Cell`
            * :py:class:`~pdf2docx.layout.Blocks`
            * :py:class:`~pdf2docx.shape.Shapes`
    * :py:class:`~pdf2docx.shape.Shapes`
        * :py:class:`~pdf2docx.shape.Shape.Stroke`
        * :py:class:`~pdf2docx.shape.Shape.Fill`
        * :py:class:`~pdf2docx.shape.Shape.Hyperlink`

::

    {
        "id": 0, # page index
        "width" : w,
        "height": h,
        "margin": [left, right, top, bottom],
        "sections": [{
            ... # section properties
        }, ...],
        "floats": [{
            ... # floating picture
        }, ...]
    }

    )Pt)
WD_SECTION   )BaseCollection)
debug_plot   )BasePage)Sections)
ImageBlockc                       s   e Zd ZdZ									d"dededed	ed
ededede	de
f fddZedd Zdd ZdefddZeddd Zdd Zdd Zdefd d!Z  ZS )#Pagez;Object representing the whole page, e.g. margins, sections.T        Nidskip_parsingwidthheightheaderfootermarginsectionsfloat_imagesc
           
         sX   || _ || _t j|||d |pt| d| _|pd| _|pd| _|	p%t | _	d| _
dS )a  Initialize page layout.

        Args:
            id (int, optional): Page index. Defaults to -1.
            skip_parsing (bool, optional): Don't parse page if True. Defaults to True.
            width (float, optional): Page width. Defaults to 0.0.
            height (float, optional): Page height. Defaults to 0.0.
            header (str, optional): Page header. Defaults to None.
            footer (str, optional): Page footer. Defaults to None.
            margin (tuple, optional): Page margin. Defaults to None.
            sections (Sections, optional): Page contents. Defaults to None.
            float_images (BaseCollection, optional): Float images in th is page. Defaults to None.
        )r   r   r   )parent FN)r   r   super__init__r	   r   r   r   r   r   
_finalized)
selfr   r   r   r   r   r   r   r   r   	__class__ BD:\Projects\ConvertPro\env\Lib\site-packages\pdf2docx/page/Page.pyr   6   s   


zPage.__init__c                 C   s   | j S )N)r   )r   r   r   r    	finalizedb   s   zPage.finalizedc              	   C   s2   | j | j| j| j| j | j| j| j d}|S )z#Store parsed layout in dict format.)r   r   r   r   r   r   r   floats)	r   r   r   r   r   storer   r   r   )r   resr   r   r    r#   f   s   
z
Page.storedatac                 C   s   | dd| _| dd| _| dd| _| dd| _| j| dg  | d	d
| _| dd
| _| 	| dg  d| _
| S )z#Restore Layout from parsed results.r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r"   T)getr   r   r   r   r   restorer   r   _restore_float_imagesr   )r   r%   r   r   r    r'   u   s   zPage.restorezFinal Layoutc                 K   s   | j jdi | d| _| j S )zParse page layout.TNr   )r   parser   )r   settingsr   r   r    r)      s   z
Page.parsec                 K   s^   g }| j D ]}|D ]}|d r||jj q	||jj q	qg }|D ]}||j q$|S )zExtract content from tables (top layout only).
        
        .. note::
            Before running this method, the page layout must be either parsed from source 
            page or restored from parsed data.
        Zextract_stream_table)r   extendblocksZtable_blocksZlattice_table_blocksappendtext)r   r*   collectionssectioncolumnZtablesZtable_blockr   r   r    extract_tables   s   
zPage.extract_tablesc                 C   s|   |j r
|tj}n|jd }t| j|_t| j|_	| j
\}}}}t||_t||_t||_t||_| j| dS )a!  Set page size, margin, and create page. 

        .. note::
            Before running this method, the page layout must be either parsed from source 
            page or restored from parsed data.
        
        Args:
            doc (Document): ``python-docx`` document object
        r   N)Z
paragraphsadd_sectionr   ZNEW_PAGEr   r   r   Z
page_widthr   Zpage_heightr   Zleft_marginZright_marginZ
top_marginZbottom_margin	make_docx)r   docr0   leftrighttopbottomr   r   r    r4      s   




zPage.make_docxrawsc                 C   s4   | j   |D ]}t|}|  | j | qdS )zRestore float images.N)r   resetr
   Zset_float_image_blockr-   )r   r:   rawimager   r   r    r(      s   
zPage._restore_float_images)	r   Tr   r   NNNNN)__name__
__module____qualname____doc__intboolfloatstrtupler	   r   r   propertyr!   r#   dictr'   r   r)   r2   r4   listr(   __classcell__r   r   r   r    r   3   sJ    ,

r   N)rA   Zdocx.sharedr   Zdocx.enum.sectionr   Zcommon.Collectionr   Zcommon.sharer   r   Zlayout.Sectionsr	   Zimage.ImageBlockr
   r   r   r   r   r    <module>   s   '