
    l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y)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y)
TextFitterzGValue object that knows how to fit text into given rectangular extents.c                @    |\  }}t         j                  | ||||f      S Ntuple__new__)clsline_sourceextents	font_filewidthheights         N/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/pptx/text/layout.pyr   zTextFitter.__new__   s%    v}}S;vy"IJJ    c                N    t        |      } | |||      }|j                  |      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   textr   max_sizer   r   text_fitters          r   best_fit_font_sizezTextFitter.best_fit_font_size   s-     "$'+w	:..x88r   c                    | j                   }t        j                  t        dt	        |      dz               }|j                  |      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rangeintfind_max)selfr   	predicatesizess       r   r   zTextFitter._best_fit_font_size"   s?    
 //	!77aXQRAR8ST~~i((r   c                p    t         j                  |      }| j                  |      }|j                  |      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+   s3     "77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                j    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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                  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_sourcer/   r0   len_height)r*   
text_linescyr%   s      r   r&   z4TextFitter._fits_inside_predicate.<locals>.predicateN   sL     ))$*;*;ZHJj$//B1EBZ(T\\99r   r4   )r%   r&   s   ` r   r   z!TextFitter._fits_inside_predicateF   s    	: r   c                    | d   S )N   r4   r%   s    r   r0   zTextFitter._font_fileZ       Awr   c                    | d   S )N   r4   r@   s    r   r;   zTextFitter._height^   rA   r   c                    | d   S Nr   r4   r@   s    r   r9   zTextFitter._line_sourceb   rA   r   c                    | d   S Nr   r4   r@   s    r   r1   zTextFitter._widthf   rA   r   c                |    | j                  ||      \  }}|g}|r!|j                  | j                  ||             |S )z
        Return a sequence of str values representing the text in
        *line_source* wrapped within this fitter when rendered at
        *point_size*.
        )r,   extendr8   )r%   r   r*   r   	remainderr+   s         r   r8   zTextFitter._wrap_linesj   sB     **;
CiLL)))Z@Ar   N)
r   strr   ztuple[Length, Length]r   r#   r   rK   returnr#   )__name__
__module____qualname____doc__r   classmethodr   r   r,   r)   propertyr   r0   r;   r9   r1   r8   r4   r   r   r   r      s    QK 99!69BE9RU9	9 9))"  &        
r   r   c                  b    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y)r    z]
    A node in a binary search tree. Uniform for root, subtree root, and leaf
    nodes.
    c                .    || _         d | _        d | _        y r
   )_value_lesser_greater)r%   values     r   __init__z_BinarySearchTree.__init__}   s    r   Nc                     || j                         r| j                   }| j                  }n| j                  }||S |j                  ||      S )zc
        Return the largest item in or under this node that satisfies
        *predicate*.
        )rX   rW   rV   r$   )r%   r&   max_	next_nodes       r   r$   z_BinarySearchTree.find_max   sI    
 TZZ ::DIIK!!)T22r   c                j    t        |      } | |j                               }|j                  |       |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/     4j#'')n))#.
r   c                    || j                   k  rdnd}t        | |      }|t        | |t        |             y|j	                  |       y)z
        Insert a new node containing *value* into this tree such that its
        structure as a binary search tree is preserved.
        rV   rW   N)rX   getattrsetattrr    insert)r%   rX   sidechilds       r   rg   z_BinarySearchTree.insert   sE    
 "DJJ.yJd#=D$ 1% 89LLr   c                   || j                   j                  d}dd|z  z  }| j                  r"|| j                  j                  |dz   |      z  }| j                  r"|| j                  j                  |dz   |      z  }|S )zq
        A string representation of the tree rooted in this node, useful for
        debugging purposes.
        
u   %s└── z    r   )rX   r   rV   treerW   )r%   levelprefixr   s       r   rl   z_BinarySearchTree.tree   su    
 "4::??36E>2<<DLL%%eai88D==DMM&&uqy&99Dr   c                    | j                   S )z:
        The value object contained in this node.
        )rU   r@   s    r   rX   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   NrC   r   )r:   r#   )rb   mid_idxmidgreaterlessers        r   _bisectz_BinarySearchTree._bisect   s^     s8q=tR<c#hl#'lgkm$XgGV##r   c                    t        |      dk(  ry| j                  |      \  }}}| j                  |       | j                  |       | j                  |       y)zx
        Insert the new values contained in *seq* into this tree such that
        a balanced tree is produced.
        r   N)r:   ru   rg   r`   )r%   rb   rr   rs   rt   s        r   r`   z/_BinarySearchTree._insert_from_ordered_sequence   sO    
 s8q=#||C0WfC**73**62r   r
   )r    )rM   rN   rO   rP   rY   r$   rQ   r!   rg   rl   rR   rX   staticmethodru   r`   r4   r   r   r    r    w   s\    

3 	 	
    $ $
3r   r    c                  4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	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                    || _         y r
   _text)r%   r   s     r   rY   z_LineSource.__init__   s	    
r   c                <    | j                   j                         dk7  S )z
        Gives this object boolean behaviors (in Python 3). bool(line_source)
        is False if it contains the empty string or whitespace only.
        rw   r|   stripr@   s    r   __bool__z_LineSource.__bool__       
 zz!R''r   c                4    | j                   |j                   k(  S r
   r{   r%   others     r   __eq__z_LineSource.__eq__   s    zzU[[((r   c              #     K   | j                   j                         }t        dt        |      dz         D ]C  }dj	                  |d|       }dj	                  ||d       }t        |      }t        ||       E yw)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_textrJ   s         r   __iter__z_LineSource.__iter__   sw      

  "CJN+ 	.Ct-I XXeCDk2N#N3I	9--		.s   A:A<c                <    | j                   j                         dk7  S )z
        Gives this object boolean behaviors (in Python 2). bool(line_source)
        is False if it contains the empty string or whitespace only.
        rw   r~   r@   s    r   __nonzero__z_LineSource.__nonzero__   r   r   c                     d| j                   z  S )Nz<_LineSource('%s')>r{   r@   s    r   __repr__z_LineSource.__repr__  s    $tzz11r   N)
rM   rN   rO   rP   rY   r   r   r   r   r   r4   r   r   r   r      s%    ().(2r   r   c                  N    e Zd ZdZd Zd Zd Zd Zd Ze	d        Z
e	d        Zy	)
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                2    t         j                  | ||f      S r
   r   )r   r   rJ   s      r   r   z_Line.__new__  s    }}S4"344r   c                X    t        | j                        t        |j                        kD  S r
   r:   r   r   s     r   __gt__z_Line.__gt__  s    499~EJJ//r   c                &    | j                  |       S r
   )r   r   s     r   __lt__z_Line.__lt__  s    ;;u%%%r   c                ,    t        | j                        S r
   r   r@   s    r   __len__z_Line.__len__  s    499~r   c                <    d| j                   d| j                  dS )N'z' => ')r   rJ   r@   s    r   r   z_Line.__repr__  s    !%DNN;;r   c                    | d   S rG   r4   r@   s    r   rJ   z_Line.remainder  rA   r   c                    | d   S rE   r4   r@   s    r   r   z
_Line.text  rA   r   N)rM   rN   rO   rP   r   r   r   r   r   rR   rJ   r   r4   r   r   r   r     sH    50&<    r   r   c                  $    e Zd ZdZi Zed        Zy)_Fontsz2
    A memoizing cache for ImageFont objects.
    c                    ||f| j                   vr%t        j                  ||      | j                   ||f<   | j                   ||f   S r
   )fontsr   truetype)r   	font_pathr*   s      r   fontz_Fonts.font+  sG    z"#))31:1C1CIz1ZCIIy*-.yy)Z011r   N)rM   rN   rO   rP   r   rQ   r   r4   r   r   r   r   $  s      E2 2r   r   c                
   d}d}t         j                  ||      }	 |j                  |       \  }}t        ||z  |z        }t        ||z  |z        }||fS # t        $ r# |j	                  |       \  }}	}
}|
|z
  ||	z
  }}Y Qw xY w)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   r/   r/   2  s     LK;;y*-D9"ll40)
 H{*\9:IY,|;<Jj    9#'<<#5 c5&#dlFSL)9s   A )BBN)rP   
__future__r   typingr   PILr   	pptx.utilr   r   r   objectr    r   r   r   r/   r4   r   r   <module>r      s_    E "    g gT]3 ]3@+2& +2\E >2V 2!r   