
    kh5                     r    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e	      Zy)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                        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Zed        Zd ZdefdZ ed      d        Zd Zd ZdefdZ xZS )Pagez;Object representing the whole page, e.g. margins, sections.idskip_parsingwidthheightheaderfootermarginsectionsfloat_imagesc
                     || _         || _        t        
|   |||       |xs t	        |       | _        |xs d| _        |xs d| _        |	xs
 t               | _	        d| _
        y)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__s             N/var/www/teggl/fontify/venv/lib/python3.12/site-packages/pdf2docx/page/Page.pyr   zPage.__init__6   sn    . ( 	uVFC
 !9HD$9 ll )<N,<    c                     | j                   S )N)r   )r   s    r   	finalizedzPage.finalizedb   s     $/r    c           	          | j                   | j                  | 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   ress     r   r%   z
Page.storef   s^     

++-))//1	
 
r    datac                    |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        | j
                  j                  |j                  dg              |j                  d	d
      | _        |j                  dd
      | _        | j                  |j                  dg              d| _
        | S )z#Restore Layout from parsed results.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'   s     r   r,   zPage.restoreu   s     ((4$ XXgs+
hhx-hhx2 	dhhz267hhx,hhx, 	""488Hb#9: r    zFinal Layoutc                 `     | j                   j                  di | d| _        | j                   S )zParse page layout.T )r   parser   )r   settingss     r   r0   z
Page.parse   s,     	'h'}}r    c                 "   g }| j                   D ]Y  }|D ]R  }|d   r&|j                  |j                  j                         .|j                  |j                  j                         T [ g }|D ]  }|j                  |j                          |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.
        extract_stream_table)r   extendblockstable_blockslattice_table_blocksappendtext)r   r1   collectionssectioncolumntablestable_blocks          r   extract_tableszPage.extract_tables   s     }} 	KG! K23&&v}}'A'AB&&v}}'I'IJ	K	K & 	,KMM+**+	, r    c                    |j                   r |j                  t        j                        }n|j                  d   }t        | j                        |_        t        | j                        |_	        | j                  \  }}}}t        |      |_        t        |      |_        t        |      |_        t        |      |_        | j                  j                  |       y)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)
paragraphsadd_sectionr   NEW_PAGEr   r   r   
page_widthr   page_heightr   left_marginright_margin
top_marginbottom_margin	make_docx)r   docr;   leftrighttopbottoms          r   rJ   zPage.make_docx   s     >>ooj&9&9:Gll1oG !n o !%U3v h!%yW "6
 	$r    rawsc                     | j                   j                          |D ]8  }t        |      }|j                          | j                   j	                  |       : y)zRestore float images.N)r   resetr   set_float_image_blockr8   )r   rP   rawimages       r   r-   zPage._restore_float_images   sL    ! 	,CsOE'')$$U+	,r    )	r)   Tr*   r*   NNNNN)__name__
__module____qualname____doc__intboolfloatstrtupler
   r   r   propertyr"   r%   dictr,   r   r0   r?   rJ   listr-   __classcell__)r   s   @r   r   r   3   s    E *.$'%(#'#'%)*.48) # ) %)) #)   %)   #	) 
  #)   %)  "*)  &4) X / /4 2   0%>, ,r    r   N)rY   docx.sharedr   docx.enum.sectionr   common.Collectionr   common.sharer   r	   layout.Sectionsr
   image.ImageBlockr   r   r/   r    r   <module>ri      s0   %N  ( . %  & )`,8 `,r    