
    hi'                        d Z ddlmZ ddlmZ ddlmZ erddlmZ  G d de	          Z
 G d d	e          Z G d
 de          Z G d de	          Z G d de          Zd ZdS )z?Objects related to layout of rendered text, such as TextFitter.    )annotations)TYPE_CHECKING)	ImageFont)Lengthc                      e Zd ZdZd Zedd            Zd Zd Zd Z	e
d             Ze
d             Ze
d             Ze
d             Ze
d             Zd ZdS )
TextFitterzGValue object that knows how to fit text into given rectangular extents.c                J    |\  }}t                               | ||||f          S Ntuple__new__)clsline_sourceextents	font_filewidthheights         h/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/pptx/text/layout.pyr   zTextFitter.__new__   s(    v}}S;vy"IJJJ    textstrr   tuple[Length, Length]max_sizeintr   returnc                d    t          |          } | |||          }|                    |          S )a+  Return whole-number best fit point size less than or equal to `max_size`.

        The return value is the largest whole-number point size less than or equal to
        `max_size` that allows `text` to fit completely within `extents` when rendered
        using font defined in `font_file`.
        )_LineSource_best_fit_font_size)r   r   r   r   r   r   text_fitters          r   best_fit_font_sizezTextFitter.best_fit_font_size   s8     "$''c+w	::..x888r   c                    | j         }t                              t          dt	          |          dz                       }|                    |          S )z
        Return the largest whole-number point size less than or equal to
        *max_size* that this fitter can fit.
           )_fits_inside_predicate_BinarySearchTreefrom_ordered_sequenceranger   find_max)selfr   	predicatesizess       r   r   zTextFitter._best_fit_font_size"   sG    
 /	!77aXQRAR8S8STT~~i(((r   c                    t                               |          }|                     |          }|                    |          S )z
        Return a (line, remainder) pair where *line* is the longest line in
        *line_source* that will fit in this fitter's width and *remainder* is
        a |_LineSource| object containing the text following the break point.
        )r$   r%   _fits_in_width_predicater'   )r(   r   
point_sizelinesr)   s        r   _break_linezTextFitter._break_line+   s<     "77DD11*==	~~i(((r   c                      fd}|S )z
        Return a function taking a text string value and returns |True| if
        that text fits in this fitter when rendered at *point_size*. Used as
        predicate for _break_line()
        c                \    t          | j        j                  d         }|j        k    S )zp
            Return |True| if *line* fits in this fitter when rendered at
            *point_size*.
            r   )_rendered_sizer   
_font_file_width)linecxr-   r(   s     r   r)   z6TextFitter._fits_in_width_predicate.<locals>.predicate<   s,    
  	:tGGJB$$r    )r(   r-   r)   s   `` r   r,   z#TextFitter._fits_in_width_predicate5   s*    	% 	% 	% 	% 	% 	% r   c                      fd}|S )zReturn  function taking an integer point size argument.

        The function returns |True| if the text in this fitter can be wrapped to fit
        entirely within its extents when rendered at that point size.
        c                                         j        |           }t          d| j                  d         }|t	          |          z  j        k    S )zReturn |True| when text in `line_source` can be wrapped to fit.

            Fit means text can be broken into lines that fit entirely within `extents`
            when rendered at `point_size` using the font defined in `font_file`.
            Tyr"   )_wrap_lines_line_sourcer2   r3   len_height)r-   
text_linescyr(   s      r   r)   z4TextFitter._fits_inside_predicate.<locals>.predicateN   sM     ))$*;ZHHJj$/BB1EBZ(T\99r   r7   )r(   r)   s   ` r   r#   z!TextFitter._fits_inside_predicateF   s$    	: 	: 	: 	: 	: r   c                    | d         S )N   r7   r(   s    r   r3   zTextFitter._font_fileZ       Awr   c                    | d         S )N   r7   rC   s    r   r>   zTextFitter._height^   rD   r   c                    | d         S Nr   r7   rC   s    r   r<   zTextFitter._line_sourceb   rD   r   c                    | d         S Nr"   r7   rC   s    r   r4   zTextFitter._widthf   rD   r   c                    |                      ||          \  }}|g}|r)|                    |                     ||                     |S )z
        Return a sequence of str values representing the text in
        *line_source* wrapped within this fitter when rendered at
        *point_size*.
        )r/   extendr;   )r(   r   r-   r   	remainderr.   s         r   r;   zTextFitter._wrap_linesj   sT     **;
CCi 	BLL)))Z@@AAAr   N)
r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   classmethodr    r   r/   r,   propertyr#   r3   r>   r<   r4   r;   r7   r   r   r   r      s       QQK K K 9 9 9 [9) ) )) ) )  "   X&   X   X   X   X
 
 
 
 
r   r   c                  v    e Zd ZdZd ZddZed             Zd Zdd	Z	e
d
             Zed             Zd ZdS )r$   z]
    A node in a binary search tree. Uniform for root, subtree root, and leaf
    nodes.
    c                0    || _         d | _        d | _        d S r
   )_value_lesser_greater)r(   values     r   __init__z_BinarySearchTree.__init__}   s    r   Nc                     || j                   r| j         }| j        }n| j        }||S |                    ||          S )zc
        Return the largest item in or under this node that satisfies
        *predicate*.
        )rY   rX   rW   r'   )r(   r)   max_	next_nodes       r   r'   z_BinarySearchTree.find_max   sP    
 9TZ   	%:DIIIK!!)T222r   c                    t          |          } | |                                          }|                    |           |S )zx
        Return the root of a balanced binary search tree populated with the
        values in iterable *iseq*.
        )listpop_insert_from_ordered_sequence)r   iseqseqbsts       r   r%   z'_BinarySearchTree.from_ordered_sequence   s=     4jjc#''))nn))#...
r   c                    || j         k     rdnd}t          | |          }| t          | |t          |                     dS |                    |           dS )z
        Insert a new node containing *value* into this tree such that its
        structure as a binary search tree is preserved.
        rW   rX   N)rY   getattrsetattrr$   insert)r(   rY   sidechilds       r   rh   z_BinarySearchTree.insert   sf    
 "DJ..yyJd##=D$ 1% 8 899999LLr   r    c                    || j         j        d}dd|z  z  }| j        r!|| j                            |dz   |          z  }| j        r!|| j                            |dz   |          z  }|S )zq
        A string representation of the tree rooted in this node, useful for
        debugging purposes.
        
u   %s└── z    r"   )rY   r   rW   treerX   )r(   levelprefixr   s       r   rn   z_BinarySearchTree.tree   s    
 "64:???36E>2< 	9DL%%eai888D= 	:DM&&uqy&999Dr   c                    | j         S )z:
        The value object contained in this node.
        )rV   rC   s    r   rY   z_BinarySearchTree.value   s    
 {r   c                    t          |           dk    rg dg fS t          t          |           dz            }| |         }| |dz   d         }| d|         }|||fS )z~
        Return a (medial_value, greater_values, lesser_values) 3-tuple
        obtained by bisecting sequence *seq*.
        r   NrF   r"   )r=   r   )rc   mid_idxmidgreaterlessers        r   _bisectz_BinarySearchTree._bisect   sk     s88q==tR<c#hhl##'lgkmm$XgXGV##r   c                    t          |          dk    rdS |                     |          \  }}}|                     |           |                     |           |                     |           dS )zx
        Insert the new values contained in *seq* into this tree such that
        a balanced tree is produced.
        r   N)r=   rw   rh   ra   )r(   rc   rt   ru   rv   s        r   ra   z/_BinarySearchTree._insert_from_ordered_sequence   sp    
 s88q==F#||C00WfC**7333**622222r   r
   )r   rk   )rN   rO   rP   rQ   rZ   r'   rR   r%   rh   rn   rS   rY   staticmethodrw   ra   r7   r   r   r$   r$   w   s         
  
3 3 3 3 	 	 [	
  
  
       X $ $ \$
3 
3 
3 
3 
3r   r$   c                  6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	r   aj  
    Generates all the possible even-word line breaks in a string of text,
    each in the form of a (line, remainder) 2-tuple where *line* contains the
    text before the break and *remainder* the text after as a |_LineSource|
    object. Its boolean value is |True| when it contains text, |False| when
    its text is the empty string or whitespace only.
    c                    || _         d S r
   _text)r(   r   s     r   rZ   z_LineSource.__init__   s    


r   c                <    | j                                         dk    S )z
        Gives this object boolean behaviors (in Python 3). bool(line_source)
        is False if it contains the empty string or whitespace only.
        rk   r}   striprC   s    r   __bool__z_LineSource.__bool__       
 z!!R''r   c                "    | j         |j         k    S r
   r|   r(   others     r   __eq__z_LineSource.__eq__   s    zU[((r   c              #  8  K   | j                                         }t          dt          |          dz             D ]]}d                    |d|                   }d                    ||d                   }t          |          }t          ||          V  ^dS )z
        Generate a (text, remainder) pair for each possible even-word line
        break in this line source, where *text* is a str value and remainder
        is a |_LineSource| value.
        r"    N)r}   splitr&   r=   joinr   _Line)r(   wordsidx	line_textremainder_textrM   s         r   __iter__z_LineSource.__iter__   s       
  ""CJJN++ 	. 	.Ctt--I XXeCDDk22N#N33I	9------		. 	.r   c                <    | j                                         dk    S )z
        Gives this object boolean behaviors (in Python 2). bool(line_source)
        is False if it contains the empty string or whitespace only.
        rk   r   rC   s    r   __nonzero__z_LineSource.__nonzero__   r   r   c                    d| j         z  S )Nz<_LineSource('%s')>r|   rC   s    r   __repr__z_LineSource.__repr__  s    $tz11r   N)
rN   rO   rP   rQ   rZ   r   r   r   r   r   r7   r   r   r   r      sx           ( ( () ) ). . .( ( (2 2 2 2 2r   r   c                  \    e Zd ZdZd Zd Zd Zd Zd Ze	d             Z
e	d             Zd	S )
r   z
    A candidate line broken at an even word boundary from a string of text,
    and a |_LineSource| value containing the text that remains after the line
    is broken at this spot.
    c                <    t                               | ||f          S r
   r   )r   r   rM   s      r   r   z_Line.__new__  s    }}S4"3444r   c                V    t          | j                  t          |j                  k    S r
   r=   r   r   s     r   __gt__z_Line.__gt__  s    49~~EJ//r   c                .    |                      |           S r
   )r   r   s     r   __lt__z_Line.__lt__  s    ;;u%%%%r   c                *    t          | j                  S r
   r   rC   s    r   __len__z_Line.__len__  s    49~~r   c                (    d| j         d| j        dS )N'z' => ')r   rM   rC   s    r   r   z_Line.__repr__  s     !%DNNN;;r   c                    | d         S rJ   r7   rC   s    r   rM   z_Line.remainder  rD   r   c                    | d         S rH   r7   rC   s    r   r   z
_Line.text  rD   r   N)rN   rO   rP   rQ   r   r   r   r   r   rS   rM   r   r7   r   r   r   r     s         5 5 50 0 0& & &  < < <   X   X  r   r   c                  ,    e Zd ZdZi Zed             ZdS )_Fontsz2
    A memoizing cache for ImageFont objects.
    c                t    ||f| j         vrt          j        ||          | j         ||f<   | j         ||f         S r
   )fontsr   truetype)r   	font_pathr-   s      r   fontz_Fonts.font+  sD    z"#)331:1CIz1Z1ZCIy*-.y)Z011r   N)rN   rO   rP   rQ   r   rR   r   r7   r   r   r   r   $  s>          E2 2 [2 2 2r   r   c                8   d}d}t                               ||          }	 |                    |           \  }}n4# t          $ r' |                    |           \  }}	}
}|
|z
  ||	z
  }}Y nw xY wt          ||z  |z            }t          ||z  |z            }||fS )z
    Return a (width, height) pair representing the size of *text* in English
    Metric Units (EMU) when rendered at *point_size* in the font defined in
    *font_file*.
    i g      R@)r   r   getsizeAttributeErrorgetbboxr   )r   r-   r   emu_per_inchpx_per_inchr   px_width	px_heightlefttoprightbottom	emu_width
emu_heights                 r   r2   r2   2  s     LK;;y*--D9"ll400)) 9 9 9#'<<#5#5 c5&#dlFSL)9 H{*\9::IY,|;<<Jj  s   : .A+*A+N)rQ   
__future__r   typingr   PILr   	pptx.utilr   r   r   objectr$   r   r   r   r2   r7   r   r   <module>r      sY   E E " " " " " "                   !      g g g g g g g gT]3 ]3 ]3 ]3 ]3 ]3 ]3 ]3@+2 +2 +2 +2 +2& +2 +2 +2\    E   >2 2 2 2 2V 2 2 2! ! ! ! !r   