o
    `ÊæT  ã                   @   s|   d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
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dS )z5
Custom element classes related to text runs (CT_R).
é   )Úqn)Ú
ST_BrClearÚ	ST_BrType)ÚBaseOxmlElementÚOptionalAttributeÚ
ZeroOrMoreÚ	ZeroOrOnec                   @   s$   e Zd ZdZedeƒZedeƒZdS )ÚCT_BrzP
    ``<w:br>`` element, indicating a line, page, or column break in a run.
    zw:typezw:clearN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Útyper   Úclear© r   r   úBD:\Projects\ConvertPro\env\Lib\site-packages\docx\oxml\text\run.pyr	      s    
r	   c                   @   s”   e Zd ZdZedƒZedƒZedƒZedƒZ	edƒZ
edƒZdd	„ Zd
d„ Zdd„ Zdd„ Zedd„ ƒZejdd„ ƒZedd„ ƒZejdd„ ƒZdS )ÚCT_RzJ
    ``<w:r>`` element, containing the properties and text for a run.
    zw:rPrúw:túw:brúw:crúw:tabz	w:drawingc                 C   s   |   d|¡ |S )Né    )Úinsert©ÚselfÚrPrr   r   r   Ú_insert_rPr!   s   zCT_R._insert_rPrc                 C   s4   | j |d�}t| ¡ ƒt|ƒk r| tdƒd¡ |S )zK
        Return a newly added ``<w:t>`` element containing *text*.
        )Útextz	xml:spaceZpreserve)Z_add_tÚlenÚstripÚsetr   )r   r   Útr   r   r   Úadd_t%   s   z
CT_R.add_tc                 C   s   |   ¡ }| |¡ |S )zˆ
        Return a newly appended ``CT_Drawing`` (``<w:drawing>``) child
        element having *inline_or_anchor* as its child.
        )Z_add_drawingÚappend)r   Zinline_or_anchorÚdrawingr   r   r   Úadd_drawing.   s   
zCT_R.add_drawingc                 C   s:   | j dur| dd… n| dd… }|D ]}|  |¡ qdS )zV
        Remove all child elements except the ``<w:rPr>`` element if present.
        Né   )r   Úremove)r   Zcontent_child_elmsÚchildr   r   r   Úclear_content7   s   "ÿzCT_R.clear_contentc                 C   s   | j }|du r	dS |jS )z�
        String contained in w:val attribute of <w:rStyle> grandchild, or
        |None| if that element is not present.
        N)r   Ústyler   r   r   r   r*   ?   s   z
CT_R.stylec                 C   s   |   ¡ }||_dS )z�
        Set the character style of this <w:r> element to *style*. If *style*
        is None, remove the style element.
        N)Zget_or_add_rPrr*   )r   r*   r   r   r   r   r*   J   s   
c                 C   sr   d}| D ]2}|j tdƒkr|j}||dur|nd7 }q|j tdƒkr'|d7 }q|j tdƒtdƒfv r6|d7 }q|S )	z­
        A string representing the textual content of this run, with content
        child elements like ``<w:tab/>`` translated to their Python
        equivalent.
        Ú r   Nr   ú	r   r   Ú
)Útagr   r   )r   r   r(   Zt_textr   r   r   r   S   s   
€z	CT_R.textc                 C   s   |   ¡  t | |¡ d S ©N)r)   Ú_RunContentAppenderÚappend_to_run_from_text©r   r   r   r   r   r   e   s   N)r
   r   r   r   r   r   r   r!   ÚbrZcrÚtabr$   r   r"   r%   r)   Úpropertyr*   Úsetterr   r   r   r   r   r      s(    		



r   c                   @   s   e Zd ZdZdS )ÚCT_TextzN
    ``<w:t>`` element, containing a sequence of characters within a run.
    N)r
   r   r   r   r   r   r   r   r7   k   s    r7   c                   @   s<   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dS )r0   a¢  
    Service object that knows how to translate a Python string into run
    content elements appended to a specified ``<w:r>`` element. Contiguous
    sequences of regular characters are appended in a single ``<w:t>``
    element. Each tab character ('	') causes a ``<w:tab/>`` element to be
    appended. Likewise a newline or carriage return character ('
', '')
    causes a ``<w:cr>`` element to be appended.
    c                 C   s   || _ g | _d S r/   )Ú_rÚ_bfr)r   Úrr   r   r   Ú__init__z   s   
z_RunContentAppender.__init__c                 C   s   | |ƒ}|  |¡ dS )zº
        Create a "one-shot" ``_RunContentAppender`` instance and use it to
        append the run content elements corresponding to *text* to the
        ``<w:r>`` element *r*.
        N)Úadd_text)Úclsr:   r   Zappenderr   r   r   r1   ~   s   z+_RunContentAppender.append_to_run_from_textc                 C   s    |D ]}|   |¡ q|  ¡  dS )z|
        Append the run content elements corresponding to *text* to the
        ``<w:r>`` element of this instance.
        N)Úadd_charÚflush)r   r   Úcharr   r   r   r<   ˆ   s   z_RunContentAppender.add_textc                 C   sL   |dkr|   ¡  | j ¡  dS |dv r|   ¡  | j ¡  dS | j |¡ dS )aX  
        Process the next character of input through the translation finite
        state maching (FSM). There are two possible states, buffer pending
        and not pending, but those are hidden behind the ``.flush()`` method
        which must be called at the end of text to ensure any pending
        ``<w:t>`` element is written.
        r,   z
N)r?   r8   Zadd_tabZadd_brr9   r#   )r   r@   r   r   r   r>   ‘   s   z_RunContentAppender.add_charc                 C   s,   d  | j¡}|r| j |¡ | jd d …= d S )Nr+   )Újoinr9   r8   r"   r2   r   r   r   r?   ¢   s   z_RunContentAppender.flushN)
r
   r   r   r   r;   Úclassmethodr1   r<   r>   r?   r   r   r   r   r0   q   s    
		r0   N)r   Únsr   Zsimpletypesr   r   Zxmlchemyr   r   r   r   r	   r   r7   Úobjectr0   r   r   r   r   Ú<module>   s   U