o
    jqiO                     @  sT  U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ ddlm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' dZ(de)d< G dd de Z*G dd de Z+G dd de Z,G dd de Z-G dd de Z.G dd de Z/G dd dZ0d S )!z'Section-related custom element classes.    )annotations)deepcopy)CallableIteratorListSequencecast)etree)	TypeAlias)WD_HEADER_FOOTERWD_ORIENTATIONWD_SECTION_START)nsmap)CT_OnOff)ST_SignedTwipsMeasureST_TwipsMeasure	XsdString)CT_Tbl)CT_P)BaseOxmlElementOptionalAttributeRequiredAttribute
ZeroOrMore	ZeroOrOne)LengthlazypropertyzCT_P | CT_Tblr
   BlockElementc                   @  sX   e Zd ZU dZded< ded< ded< ded	< ed
ddZedddZedddZ	dS )	CT_HdrFtrzN`w:hdr` and `w:ftr`, the root element for header and footer part respectively.zCallable[[], CT_P]Zadd_pz
List[CT_P]Zp_lstzList[CT_Tbl]Ztbl_lstzCallable[[CT_Tbl], CT_Tbl]Z_insert_tblzw:p 
successorszw:tblreturnList[CT_P | CT_Tbl]c                 C  s
   |  dS )zGenerate all `w:p` and `w:tbl` elements in this header or footer.

        Elements appear in document order. Elements shaded by nesting in a `w:ins` or
        other "wrapper" element will not be included.
        z./w:p | ./w:tbl)xpathselfr   r   X/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/docx/oxml/section.pyinner_content_elements)   s   
z CT_HdrFtr.inner_content_elementsN)r!   r"   )
__name__
__module____qualname____doc____annotations__r   ptblpropertyr'   r   r   r   r&   r      s   
 r   c                   @  s6   e Zd ZU dZedeZded< edeZ	ded< dS )	CT_HdrFtrRefz5`w:headerReference` and `w:footerReference` elements.w:typer   type_zr:idstrrIdN)
r(   r)   r*   r+   r   r   r2   r,   r   r4   r   r   r   r&   r0   3   s   
 r0   c                   @  s   e Zd ZU dZedeZded< edeZ	ded< edeZ
ded< ed	eZded
< edeZded< edeZded< edeZded< dS )
CT_PageMarz-``<w:pgMar>`` element, defining page margins.zw:topLength | Nonetopzw:rightrightzw:bottombottomzw:leftleftzw:headerheaderzw:footerfooterzw:guttergutterN)r(   r)   r*   r+   r   r   r7   r,   r   r8   r9   r:   r;   r<   r=   r   r   r   r&   r5   <   s.   
 r5   c                   @  sN   e Zd ZU dZedeZded< edeZded< ede	e	j
dZd	ed
< dS )	CT_PageSzz?``<w:pgSz>`` element, defining page dimensions and orientation.zw:wr6   wzw:hhzw:orient)defaultr   orientN)r(   r)   r*   r+   r   r   r?   r,   r@   r   PORTRAITrB   r   r   r   r&   r>   V   s   
 r>   c                   @  s  e Zd ZU dZded< ded< ded< ded	< d
ed< d
ed< ded< ded< dZededZededZe	dedd dZ
ded< e	dedd dZded< e	dedd dZded < e	d!ed"d dZd#ed$< [dmd+d,Zdmd-d.Zednd0d1Zejdod4d1Zdpd5d6Zednd7d8Zejdod9d8Zdqd;d<Zdqd=d>Zednd?d@ZejdodAd@ZedndBdCZejdodDdCZdrdFdGZedndHdIZejdodJdIZedsdLdMZejdtdOdMZedndPdQZejdudRdQZedndSdTZejdudUdTZedvdWdXZdwdYdZZdxd[d\Z ednd]d^Z!e!jdud_d^Z!edydadbZ"e"jdzdddbZ"ed{dfdgZ#e#jd|didgZ#edndjdkZ$e$jdudldkZ$dS )}	CT_SectPrzA`w:sectPr` element, the container element for section properties.zCallable[[], CT_PageMar]get_or_add_pgMarzCallable[[], CT_PageSz]get_or_add_pgSzzCallable[[], CT_OnOff]get_or_add_titlePgzCallable[[], CT_SectType]get_or_add_typezCallable[[], CT_HdrFtrRef]_add_footerReference_add_headerReferencezCallable[[], None]_remove_titlePg_remove_type)zw:footnotePrzw:endnotePrr1   w:pgSzw:pgMarz
w:paperSrczw:pgBorderszw:lnNumTypezw:pgNumTypezw:colsz
w:formProtzw:vAlignzw:noEndnote	w:titlePgzw:textDirectionzw:bidizw:rtlGutterz	w:docGridzw:printerSettingszw:sectPrChangezw:headerReferencer   zw:footerReferencer1      NzCT_SectType | NonetyperM      zCT_PageSz | NonepgSzrN      zCT_PageMar | NonepgMarrO      zCT_OnOff | NonetitlePgr2   r   r4   r3   r!   r0   c                 C     |   }||_||_|S )zxReturn newly added CT_HdrFtrRef element of `type_` with `rId`.

        The element tag is `w:footerReference`.
        )rI   r2   r4   )r%   r2   r4   footerReferencer   r   r&   add_footerReference      zCT_SectPr.add_footerReferencec                 C  rX   )zxReturn newly added CT_HdrFtrRef element of `type_` with `rId`.

        The element tag is `w:headerReference`.
        )rJ   r2   r4   )r%   r2   r4   headerReferencer   r   r&   add_headerReference   r[   zCT_SectPr.add_headerReferencer6   c                 C     | j }|du r	dS |jS )zValue of the `w:bottom` attr of `<w:pgMar>` child element, as |Length|.

        |None| when either the element or the attribute is not present.
        N)rU   r9   r%   rU   r   r   r&   bottom_margin      zCT_SectPr.bottom_marginvalueint | Length | Nonec                 C  2   |   }|d u st|tr||_d S t||_d S N)rE   
isinstancer   r9   r%   rb   rU   r   r   r&   r`         *c                 C  s   t | }|j  |S )zReturn an exact duplicate of this ``<w:sectPr>`` element tree suitable for
        use in adding a section break.

        All rsid* attributes are removed from the root ``<w:sectPr>`` element.
        )r   attribclear)r%   Zcloned_sectPrr   r   r&   clone   s   
zCT_SectPr.clonec                 C  r^   )a  Distance from bottom edge of page to bottom edge of the footer.

        This is the value of the `w:footer` attribute in the `w:pgMar` child element,
        as a |Length| object, or |None| if either the element or the attribute is not
        present.
        N)rU   r<   r_   r   r   r&   r<      s   zCT_SectPr.footerc                 C  rd   re   )rE   rf   r   r<   rg   r   r   r&   r<      rh   CT_HdrFtrRef | Nonec                 C  s(   dt | }| |}|sdS |d S )zAReturn footerReference element of `type_` or None if not present.z!./w:footerReference[@w:type='%s']Nr   )r   to_xmlr#   )r%   r2   pathZfooterReferencesr   r   r&   get_footerReference   s
   
zCT_SectPr.get_footerReferencec                 C  s,   |  dt| }t|dkrdS |d S )zAReturn headerReference element of `type_` or None if not present.z!./w:headerReference[@w:type='%s']r   N)r#   r   rm   len)r%   r2   Zmatching_headerReferencesr   r   r&   get_headerReference   s   zCT_SectPr.get_headerReferencec                 C  r^   )zThe value of the ``w:gutter`` attribute in the ``<w:pgMar>`` child element,
        as a |Length| object, or |None| if either the element or the attribute is not
        present.N)rU   r=   r_   r   r   r&   r=         zCT_SectPr.gutterc                 C  rd   re   )rE   rf   r   r=   rg   r   r   r&   r=      rh   c                 C  r^   )zDistance from top edge of page to top edge of header.

        This value comes from the `w:header` attribute on the `w:pgMar` child element.
        |None| if either the element or the attribute is not present.
        N)rU   r;   r_   r   r   r&   r;      s   zCT_SectPr.headerc                 C  rd   re   )rE   rf   r   r;   rg   r   r   r&   r;     rh   Iterator[CT_P | CT_Tbl]c                 C  s
   t | S )zGenerate all `w:p` and `w:tbl` elements in this section.

        Elements appear in document order. Elements shaded by nesting in a `w:ins` or
        other "wrapper" element will not be included.
        )_SectBlockElementIteratoriter_sect_block_elementsr$   r   r   r&   iter_inner_content  s   
zCT_SectPr.iter_inner_contentc                 C  r^   )zThe value of the ``w:left`` attribute in the ``<w:pgMar>`` child element, as
        a |Length| object, or |None| if either the element or the attribute is not
        present.N)rU   r:   r_   r   r   r&   left_margin  rr   zCT_SectPr.left_marginc                 C  rd   re   )rE   rf   r   r:   rg   r   r   r&   rw     rh   r   c                 C  s   | j }|du r
tjS |jS )z`WD_ORIENTATION` member indicating page-orientation for this section.

        This is the value of the `orient` attribute on the `w:pgSz` child, or
        `WD_ORIENTATION.PORTRAIT` if not present.
        N)rS   r   rC   rB   r%   rS   r   r   r&   orientation  s   zCT_SectPr.orientationWD_ORIENTATION | Nonec                 C  s"   |   }|r||_d S tj|_d S re   )rF   r   rC   rB   r%   rb   rS   r   r   r&   ry   *  s   c                 C  r^   )ziValue in EMU of the `h` attribute of the `w:pgSz` child element.

        |None| if not present.
        N)rS   r@   rx   r   r   r&   page_height/  ra   zCT_SectPr.page_heightc                 C     |   }||_d S re   )rF   r@   r{   r   r   r&   r|   :     
c                 C  r^   )zoValue in EMU of the ``w`` attribute of the ``<w:pgSz>`` child element.

        |None| if not present.
        N)rS   r?   rx   r   r   r&   
page_width?  ra   zCT_SectPr.page_widthc                 C  r}   re   )rF   r?   r{   r   r   r&   r   J  r~   CT_SectPr | Nonec                 C  s"   |  d}t|dkr|d S dS )zCSectPr immediately preceding this one or None if this is the first.z./preceding::w:sectPr[1]r   N)r#   rp   )r%   Zpreceding_sectPrsr   r   r&   preceding_sectPrO  s   
zCT_SectPr.preceding_sectPrc                 C  .   |  |}|du rtd|j}| | |S )zCReturn rId of w:footerReference child of `type_` after removing it.Nz!CT_SectPr has no footer reference)ro   
ValueErrorr4   remove)r%   r2   rY   r4   r   r   r&   remove_footerReferenceV     

z CT_SectPr.remove_footerReferencec                 C  r   )zCReturn rId of w:headerReference child of `type_` after removing it.Nz!CT_SectPr has no header reference)rq   r   r4   r   )r%   r2   r\   r4   r   r   r&   remove_headerReference`  r   z CT_SectPr.remove_headerReferencec                 C  r^   )zThe value of the ``w:right`` attribute in the ``<w:pgMar>`` child element, as
        a |Length| object, or |None| if either the element or the attribute is not
        present.N)rU   r8   r_   r   r   r&   right_marginj  rr   zCT_SectPr.right_marginc                 C  r}   re   )rE   r8   rg   r   r   r&   r   t  r~   r   c                 C  s$   | j }|du s|jdu rtjS |jS )zThe member of the ``WD_SECTION_START`` enumeration corresponding to the value
        of the ``val`` attribute of the ``<w:type>`` child element, or
        ``WD_SECTION_START.NEW_PAGE`` if not present.N)rQ   valr   NEW_PAGE)r%   rQ   r   r   r&   
start_typey  s   zCT_SectPr.start_typeWD_SECTION_START | Nonec                 C  s0   |d u s	|t ju r|   d S |  }||_d S re   )r   r   rL   rH   r   )r%   rb   rQ   r   r   r&   r     s
   
boolc                 C  s   | j }|du r	dS |jS )zEValue of `w:titlePg/@val` or |False| if `./w:titlePg` is not present.NF)rW   r   )r%   rW   r   r   r&   titlePg_val  s   zCT_SectPr.titlePg_valbool | Nonec                 C  s"   |dv r
|    d S d|  _d S )N)NFT)rK   rG   r   )r%   rb   r   r   r&   r     s   c                 C  r^   )zThe value of the ``w:top`` attribute in the ``<w:pgMar>`` child element, as a
        |Length| object, or |None| if either the element or the attribute is not
        present.N)rU   r7   r_   r   r   r&   
top_margin  rr   zCT_SectPr.top_marginc                 C  r}   re   )rE   r7   rg   r   r   r&   r     r~   )r2   r   r4   r3   r!   r0   )r!   r6   )rb   rc   )r!   rD   )r2   r   r!   rl   )r!   rs   )r!   r   )rb   rz   )rb   r6   )r!   r   )r2   r   r!   r3   )r2   r   )r!   r   )rb   r   )r!   r   )rb   r   )%r(   r)   r*   r+   r,   Z_tag_seqr   r\   rY   r   rQ   rS   rU   rW   rZ   r]   r/   r`   setterrk   r<   ro   rq   r=   r;   rv   rw   ry   r|   r   r   r   r   r   r   r   r   r   r   r   r&   rD   d   s   
 








		
	





			rD   c                   @  s$   e Zd ZU dZedeZded< dS )CT_SectTypez:``<w:sectType>`` element, defining the section start type.zw:valr   r   N)r(   r)   r*   r+   r   r   r   r,   r   r   r   r&   r     s
   
 r   c                   @  s|   e Zd ZU dZdZded< dZded< ddd	ZedddZ	dddZ
dddZed ddZd!ddZed"ddZdS )#rt   zGenerates the block-item XML elements in a section.

    A block-item element is a `CT_P` (paragraph) or a `CT_Tbl` (table).
    Nzetree.XPath | None_compiled_blocks_xpath_compiled_count_xpathsectPrrD   c                 C  s
   || _ d S re   )_sectPr)r%   r   r   r   r&   __init__  s   
z"_SectBlockElementIterator.__init__r!   Iterator[BlockElement]c                 C  s   | |  S )zIGenerate each CT_P or CT_Tbl element within extents governed by `sectPr`.)_iter_sect_block_elements)clsr   r   r   r&   ru     s   z2_SectBlockElementIterator.iter_sect_block_elementsc                 c  sZ    | j | j}}||}|dkrdn| ||d  }| ||d D ]}|V  q%dS )z0Generate each CT_P or CT_Tbl element in section.r      N)r   _sectPrsindex%_count_of_blocks_in_and_above_section_blocks_in_and_above_section)r%   r   ZsectPrsZ
sectPr_idxZn_blks_to_skipelementr   r   r&   r     s   
z3_SectBlockElementIterator._iter_sect_block_elementsSequence[BlockElement]c                 C  s6   | j du rtj| jtdd| _ | j }ttt ||S )FAll ps and tbls in section defined by `sectPr` and all prior sections.NF
namespacesregexp)r   r	   XPath"_blocks_in_and_above_section_xpathr   r   r   r   r%   r   r#   r   r   r&   r     s   
z6_SectBlockElementIterator._blocks_in_and_above_sectionr3   c              	   C  s,   d}d}d}| d| d| d| d| 	S )zHXPath expr for ps and tbls in context of a sectPr and all prior sectPrs.z./parent::w:pPr/parent::w:pzself::w:sectPr[parent::w:body]z-preceding-sibling::*[self::w:p | self::w:tbl]z | /r   )r%   Zp_sect_term_blockZbody_sect_termZpred_ps_and_tblsr   r   r&   r     s   
z<_SectBlockElementIterator._blocks_in_and_above_section_xpathintc                 C  s>   | j du rtjd| j dtdd| _ | j }ttt||S )r   Nzcount()Fr   )r   r	   r   r   r   r   r   floatr   r   r   r&   r     s   
z?_SectBlockElementIterator._count_of_blocks_in_and_above_sectionSequence[CT_SectPr]c                 C  s   | j dS )z5All w:sectPr elements in document, in document-order.zC/w:document/w:body/w:p/w:pPr/w:sectPr | /w:document/w:body/w:sectPr)r   r#   r$   r   r   r&   r     s   z"_SectBlockElementIterator._sectPrs)r   rD   )r   rD   r!   r   )r!   r   )r   rD   r!   r   )r!   r3   )r   rD   r!   r   )r!   r   )r(   r)   r*   r+   r   r,   r   r   classmethodru   r   r   r   r   r   r   r   r   r   r&   rt     s   
 



rt   N)1r+   
__future__r   copyr   typingr   r   r   r   r   Zlxmlr	   Ztyping_extensionsr
   Zdocx.enum.sectionr   r   r   Zdocx.oxml.nsr   Zdocx.oxml.sharedr   Zdocx.oxml.simpletypesr   r   r   Zdocx.oxml.tabler   Zdocx.oxml.text.paragraphr   Zdocx.oxml.xmlchemyr   r   r   r   r   Zdocx.sharedr   r   r   r,   r   r0   r5   r>   rD   r   rt   r   r   r   r&   <module>   s0    	  H