
    ^j                       d dl mZ d dlZd dlmZmZmZ d dlZd dlm	Z	 d dl
mZmZ d dlmZ d dlmZmZmZmZmZmZmZ d dlmZmZ er"d d	lmZmZ d d
lmZmZ d dl m!Z! d dl"m#Z# d dl$m%Z%  G d de          Z& G d de          Z' G d de          Z( G d dee	          Z)e)*                    d           e)*                    d           dS )    )annotationsN)TYPE_CHECKINGAnyClassVar)EventDispatcher)grapheme_breakGlyphPosition)runlist)
TextLayout_AbstractBox_InlineElementBox_InvalidRange_is_pyglet_doc_run_LayoutContext_Line)ScrollableTextDecorationGroupScrollableTextLayoutGroup)AnchorXAnchorY)BatchGroup)ShaderProgram)
VertexList)AbstractDocumentc                  "    e Zd ZdZd
dZdd	ZdS )_IncrementalLayoutContextNvertex_listr   returnNonec                D    | j         j                            |           d S N)linevertex_listsappend)selfr   s     Y/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/text/layout/incremental.pyadd_listz"_IncrementalLayoutContext.add_list%   s!    	%%k22222    boxr   c                    d S r!    )r%   r)   s     r&   add_boxz!_IncrementalLayoutContext.add_box(   s    r(   )r   r   r   r   )r)   r   r   r   )__name__
__module____qualname__r"   r'   r,   r+   r(   r&   r   r      sC        
 D3 3 3 3     r(   r   c                      e Zd ZU dZded<   dS )IncrementalTextLayoutGroupr   r   r   r   #ClassVar[tuple[int, int, int, int]]scissor_areaNr-   r.   r/   r4   __annotations__r+   r(   r&   r1   r1   ,   &          9CLBBBBBBr(   r1   c                      e Zd ZU dZded<   dS )IncrementalTextDecorationGroupr2   r3   r4   Nr5   r+   r(   r&   r9   r9   3   r7   r(   r9   c                      e Zd ZU dZded<   ded<   dZded<   dZded	<   d
Zded<   dZded<   e	Z
ded<   eZded<   dZded<   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ed<   ded<   	 	 	 	 	 dd fd?Zdd@ZddAZddBZddCZddDZddHZddJZddMZddNZddOZddPZddQZddRZdddTZeddU            Zej         d fdV            ZeddW            Z!e!j         d fdX            Z!eddY            Z"e"j         d fdZ            Z"edd\            Z#e#j         d fd^            Z#edd_            Z$e$j         dd`            Z$edda            Z%e%j         ddb            Z%eddc            Z&e&j         ddd            Z&edde            Z'e'j         ddf            Z'eddg            Z(e(j         ddh            Z(ddiZ)ddjZ*ddkZ+ddlZ,eddm            Z-e-j         ddo            Z-eddp            Z.e.j         ddr            Z.edds            Z/e/j         ddu            Z/ddvZ0eddw            Z1e1j         ddx            Z1eddy            Z2e2j         ddz            Z2edd{            Z3e3j         dd}            Z3edd~            Z4e4j         dd            Z4ddZ5dddZ6ddZ7ddZ8ddZ9ddZ:ddZ;ddZ<ddZ=ddZ>ddZ?e@rddZA xZBS  xZBS )IncrementalTextLayouta  Displayed text suitable for interactive editing and/or scrolling large documents.

    Unlike :py:func:`~pyglet.text.layout.TextLayout` and
    :py:class:`~pyglet.text.layout.ScrollableTextLayout`, this class generates
    vertex lists only for lines of text that are visible.  As the document is
    scrolled, vertex lists are deleted and created as appropriate to keep
    video memory usage to a minimum and improve rendering speed.

    Changes to the document are quickly reflected in this layout, as only the
    affected line(s) are reflowed.  Use :py:meth:`~pyglet.text.layout.TextLayout.begin_update` and
    :py:meth:`~pyglet.text.layout.TextLayout.end_update` to
    further reduce the amount of processing required.

    The layout can also display a text selection (text with a different
    background color).  The :py:class:`~pyglet.text.caret.Caret` class implements
    a visible text cursor and provides event handlers for scrolling, selecting and
    editing text in an incremental text layout.

    Attributes:
        group_class: Default group used to set the state for all glyphs.
        decoration_class: Default group used to set the state for all decorations including background colors and
            underlines.
    z	list[Any]glyphslist[_Line]linesr   int_selection_start_selection_end)   rB   rB   rB   tuple[int, int, int, int]_selection_color).   j      rB   _selection_background_colorz*ClassVar[type[IncrementalTextLayoutGroup]]group_classz.ClassVar[type[IncrementalTextDecorationGroup]]decoration_class_translate_x_translate_yr   _invalid_glyphs_invalid_flow_invalid_lines_invalid_style_invalid_vertex_lines_visible_lineszrunlist.RunList_owner_runs_width_heightNleftbottomFTdocumentr   xfloatyzwidthheightanchor_xr   anchor_yr   rotation	multilinebooldpifloat | NonebatchBatch | NonegroupGroup | NoneprogramShaderProgram | None
wrap_linesr   r   c                   ||d}t          |          g | _        g | _        g | _        t	                      | _        t	                      | _        t	                      | _        t	                      | _        t	                      | _	        t	                      | _
        t          j        dd           | _        t                                          |||||||||	|
|||||           d S )NzCInvalid size. IncrementalTextLayout width or height cannot be None.r   )	Exceptionr<   offsetsr>   r   rM   rN   rO   rP   rQ   rR   r
   RunListrS   super__init__)r%   rX   rY   r[   r\   r]   r^   r_   r`   ra   rb   rd   rf   rh   rj   rl   msg	__class__s                    r&   rr   zIncrementalTextLayout.__init__m   s     =FNWCC..    
,*__+oo+oo%2__"+oo"?1d331aE68XxYbdginpu *	. 	. 	. 	. 	.r(   c                    | j         | j        | j        | j        f}| j                                        D ]	}||_        
|| j        _        || j        _        d S r!   )	rV   rW   rT   rU   group_cachevaluesr4   background_decoration_groupforeground_decoration_group)r%   arearh   s      r&   _update_scissor_areaz*IncrementalTextLayout._update_scissor_area   s]    	4;T\B%,,.. 	& 	&E!%E8<(58<(555r(   c                h    | j         s
J d            |                     d| j         j                   d S )Nz1Cannot remove document from IncrementalTextLayoutr   )	_documenton_insert_texttextr%   s    r&   _init_documentz$IncrementalTextLayout._init_document   s9    ~RRRRR~At~233333r(   c                `    |                      dt          | j        j                             d S Nr   )on_delete_textlenr}   r   r   s    r&   _uninit_documentz&IncrementalTextLayout._uninit_document   s+    As4>#67788888r(   c                    | j         S r!   )r>   r   s    r&   
_get_linesz IncrementalTextLayout._get_lines   s
    zr(   c                    | j         D ]}|                    |            d | _        | j        r| j                            |            d | _        d S r!   )r>   delete_batchr}   remove_handlers)r%   r"   s     r&   r   zIncrementalTextLayout.delete   sZ    J 	 	DKK> 	1N**4000r(   startr   strc                f   t          |          }d g|z  | j        ||<   t          dddd          g|z  | j        ||<   | j                            ||           | j        rE| j        dk    r:| j        | j	        j
                 }| j                            |j
        ||z              | j                            ||           | j                            ||           | j                            ||           | j        D ]}|j
        |k    r|xj
        |z  c_
        |                                  d S )Nr   top)r   r<   r	   ro   rM   insert
_multiline_content_valignr>   rR   r   rN   
invalidaterP   rS   _update)r%   r   r   len_textvisible_liner"   s         r&   r~   z$IncrementalTextLayout.on_insert_text   s;   t99$(6H#4E%K %21aA%>%>$?($JU5[!##E8444
 ? 	Pt3u<<:d&9&?@L)),*<eh>NOOO!!%222""5(333x000J 	' 	'DzU""

h&

r(   endc                   g | j         ||<   g | j        ||<   | j        rB| j        dk    r7| j        | j        j                 }| j                            |j        |           | j	        
                    ||           | j        
                    ||           | j        
                    ||           | j        
                    ||           ||z
  }| j        D ]*}|j        |k    rt          |j        |z
  |          |_        +|dk    r| j                            dd           n| j                            |dz
  |           |                                  d S )Nr   r      )r<   ro   r   r   r>   rR   r   rN   r   rM   r   rP   rS   maxr   )r%   r   r   r   sizer"   s         r&   r   z$IncrementalTextLayout.on_delete_text   sa   !#E#I"$U3Y ? 	Ct3u<<:d&9&?@L)),*<cBBB##E3///!!%---""5#...s+++U{J 	; 	;DzE!! d!2E::
A::))!Q////))%!)U;;;r(   
attributesdict[str, Any]c                   d|v sd|v sd|v sd|v r| j                             ||           n?d|v sd|v r| j                            ||           n| j                            ||           |                                  d S )N	font_name	font_sizeweightitaliccolorbackground_color)rM   r   rP   rN   r   )r%   r   r   r   s       r&   on_style_textz#IncrementalTextLayout.on_style_text   s    *$$z(A(AXQ[E[E[_gku_u_u ++E37777
""&8J&F&F**5#6666))%555r(   c                   | j         sd S | j                                        p1| j                                        p| j                                        }t          | j                  }| j        s| j        D ]}|	                    |            | j        d d = | j        
                    t          d                     | j                            d| j                  }|j        | j        d         _        |j        | j        d         _        dx| j        d         _        | j        d         _        | j                            dd           | j                                         |                                  |                                  |                                  |                                  |                                  t          | j                  | _        | j        d         j        | _        | j        d         j        | _        |t          | j                  k    r|                                  |r|                     d           d S d S )Nr   rd   Tr   on_layout_update) _update_enabledrM   
is_invalidrN   rO   r   rv   r<   r>   r   r$   r   rX   get_font_dpiascentdescentparagraph_beginparagraph_endr   ro   clear_update_glyphs_update_flow_glyphs_update_flow_lines_update_visible_lines_update_vertex_lists_line_count_ascent_descentr{   dispatch_event)r%   trigger_update_event
len_groupsr"   fonts        r&   r   zIncrementalTextLayout._update   s*   # 	F $ 4 ? ? A A !A $ 2 = = ? ?!A $ 3 > > @ @ 	 )**
{ 
	!
 " "D!!!!
111JeAhh'''=))!);;D#';DJqM $(LDJqM!JNNDJqM)DJqM,G**1a000L     """!!!""$$$!!###tz??z!}+
1- T-....%%''' 	4 233333	4 	4r(   c                F   | j                                         \  }}||z
  dk    rd S | j        j        }|dk    rY||dz
           }||         }t	          j        |          }t	          j        |          }t          ||||          rn|dz  }|dk    Yt          |          }||k     rY||dz
           }||         }t	          j        |          }t	          j        |          }t          ||||          rn|dz  }||k     Yt          j	        | j
                            | j                  | j
                                        f          }	|	                    ||          D ]\  }
}\  }}|r2t          |          | j        |
<   t#          dddd          | j        |
<   =| j        j        |
|         }|                    |          \  }}|| j        |
|<   || j        |
|<   |                     | j        | j        ||           | j                            ||           d S )Nr   r   r   )rM   validaterX   r   unicodedatacategoryr   r   r
   ZipRunIteratorr}   get_font_runsr   get_element_runsrangesr   r<   r	   ro   
get_glyphs_get_owner_runsrS   rN   r   )r%   invalid_startinvalid_endr   rV   rightright_ccleft_ccr   runsr   r   r   elementr<   ro   s                   r&   r   z$IncrementalTextLayout._update_glyphs  sb   %)%9%B%B%D%D"{&!++F }!a)*D'E"+E22H!*400GdGUH== QM a t99H$$a(D%E"+E22H!*400GdGUH== 1K H$$ %N((TY(77N++--'/ 0 0 ,0;;}k+R+R 	2 	2'E3w 2%6w%?%?E"&3Aq!Q&?&?U##})%)4"&//$"7"7)/E#I&*1U3Y'' 	T-t{M;WWW 	%%m[AAAAAr(   c           	     r   | j                                         \  }}||z
  dk    rd S d}t          | j                  D ]\  }}|j        |k    r n|}t          d|dz
            }	 | j        |         }t          ||j                  }|                    |            t          |          | j        |<   | j	        
                    ||dz              nX# t          $ rK d}d}t          d          }| j                            |           | j	                            dd           Y nw xY wd}|}|                     | j        | j        | j        |t%          | j        j                            D ]
}	 | j        |         }|                    |            |j        |j        z   }	|j        |j        z   }
|	| j        k    r|
|	k     rd}|| j        |<   | j	        
                    ||dz              nE# t          $ r8 | j                            |           | j	                            |d           Y nw xY w|j        |j        z   }|dz  }	 | j        |         }||j        k    r||k    r n# t          $ r Y w xY w|t%          | j        j                  k    rS|dk    rM| j        |d          D ]3}|j        |j        z   }	|	| j        k    rd}|                    |            4| j        |d = |st%          | j                  dk    r2d}| j        D ]}t          |j        |j        z   |          } || _        d S d S )Nr   r   FT)rN   r   	enumerater>   r   r   minr   r   rO   r   
IndexErrorr$   r   _flow_glyphsr<   ro   rS   r   r}   r   r]   margin_left_content_widthlength)r%   r   r   
line_indexir"   content_width_invalid
next_startold_lineold_line_widthnew_line_width	next_linecontent_widths                r&   r   z)IncrementalTextLayout._update_flow_glyphs>  s   %)%7%@%@%B%B"{&!++F 
 ,, 	 	GAtz]**JJ JN++

	-:j)Dtz::MKK%*=%9%9DJz"**:zA~FFFF 	- 	- 	-JM88DJd###&&q!,,,,,	- !&"
%%dk4<AQS`befjftfybzbz{{ 	 	D::j1%%%!)(2F!F!%d.>!>!T%888^n=\=\,0))-
:&#..z:>JJJJ : : :
!!$'''#**:q99999: dk1J!OJ Jz2	00Z+5M5ME   
 T^01111j1nn
:;;/ " "!)(2F!F!T%888,0)D!!!!
:;;'  	0C
OOq$8$8M
 R R #DJ1A$A= Q Q"/D %9$8s9   'A,C AD)(D)3A;G//?H10H1	I**
I87I8c                    | j                                         \  }}||z
  dk    rd S |                     | j        ||          }| j                            ||           d S r   )rO   r   _flow_linesr>   rQ   r   )r%   r   r   s      r&   r   z(IncrementalTextLayout._update_flow_lines  sj    %)%8%A%A%C%C"{&!++F&&tz=+NN 	"--m[IIIIIr(   c           	     D   t           j        }d}t          | j                  D ]`\  }}|j        |j        z   | j        k     rt          ||          }|j        |j        z   | j        | j	        z
  k    rt          ||          dz   }at          | j        j        t          |t          | j                                      D ]"}| j        |                             |            #t          |t          | j        j        t          | j                                      D ]"}| j        |                             |            #| j                            || j        j                   | j                            | j        j        |           || j        _        || j        _        d S )Nr   r   )sysmaxsizer   r>   r[   r   rL   r   r   r^   r   rangerR   r   r   r   r   rQ   r   )r%   r   r   r   r"   s        r&   r   z+IncrementalTextLayout._update_visible_lines  sy    ,, 	& 	&GAtv$t'888E1v#d&7$+&EEE#qkkAo t*0#eS__2M2MNN 	' 	'AJqM  &&&&sC 3 7TZIIJJ 	' 	'AJqM  &&&& 	"--eT5H5NOOO"--d.A.EsKKK$)!"%r(   update_view_translationc           
     L   | j                                         \  }}| j                            |                     |          |                     |          dz              | j                                        \  }}||z
  dk    rd S | j                            d          }| j                            d          }| j        | j        z
  dk    rLt          j
        || j        | j        | j                  }t          j
        || j        | j        | j                  }t          | | j        ||          }| j        ||         }	t!          | j                  | _        |	d         j        | _        |	d         j        | _        |                                 | _        |                                 | _        |                                 }
t!          | j                  }|	D ]}|                    |            ||_        |j        }||j        z   | j        k    r9||j        z   | j        | j         z
  k     r n0| !                    |j"        || j        |
|j#        |j$        |           |r| %                                 |t!          | j                  k    r| &                                 d S d S )Nr   r   r   r   )'rP   r   rQ   r   get_line_from_positionrX   get_style_runsrA   r@   r
   OverriddenRunIteratorrD   rH   r   r}   r>   r   r   r   r   r   r   _get_left_anchor_anchor_left_get_bottom_anchor_anchor_bottom_get_top_anchorrv   r   r"   r[   rL   r^   _create_vertex_listsrY   r   boxes_update_view_translationr{   )r%   r   style_invalid_startstyle_invalid_endr   r   colors_iterbackground_itercontextr>   
top_anchorgroup_ctr"   r[   s                 r&   r   z*IncrementalTextLayout._update_vertex_lists  s   151D1M1M1O1O.."--''(;<<''(9::Q>	@ 	@ 	@ &*%?%H%H%J%J"{&!++Fm227;;-667IJJ!66::!7%#%	' 'K
 &;%#0	2 2O ,D$.+__
=45tz??Qxa( 1133"5577))++
t'(( 	q 	qDKKGLA 4<$"3334;!2T[!@@@%%dfa1BJPTPZ\`\fhopppp # 	,))+++ s4+,,,,%%''''' -,r(   c                    | j         S r!   )_xr   s    r&   rY   zIncrementalTextLayout.x  	    wr(   c                r    t                                          |           |                                  d S r!   )rq   _set_xr{   )r%   rY   rt   s     r&   rY   zIncrementalTextLayout.x  1    q!!#####r(   c                    | j         S r!   )_yr   s    r&   r[   zIncrementalTextLayout.y  r   r(   c                    t                                          |           | j                            dt	          | j        j                             |                                  d S r   )rq   _set_yrQ   r   r   rX   r   r{   )r%   r[   rt   s     r&   r[   zIncrementalTextLayout.y  sW    q"--aT]5G1H1HIII!!#####r(   c                    | j         S r!   )_zr   s    r&   r\   zIncrementalTextLayout.z  r   r(   c                r    t                                          |           |                                  d S r!   )rq   _set_zr{   )r%   r\   rt   s     r&   r\   zIncrementalTextLayout.z  r   r(   tuple[float, float, float]c                *    | j         | j        | j        fS r!   )r   r  r  r   s    r&   positionzIncrementalTextLayout.position  s    w((r(   r  c                "   | j         }t                                          |           | j         |k    r2| j                            dt          | j        j                             |                                  | 	                                 d S r   )
r  rq   _set_positionrQ   r   r   rX   r   r   r{   )r%   r  old_yrt   s      r&   r  zIncrementalTextLayout.position
  s    h'''7e&11!S9K5L5LMMM%%'''!!#####r(   c                    | j         S r!   )	_anchor_xr   s    r&   r_   zIncrementalTextLayout.anchor_x  
    ~r(   c                    || _         |                                  |                                  |                                  d S r!   )r  _update_anchorr{   r   )r%   r_   s     r&   r_   zIncrementalTextLayout.anchor_x  E    !!!###%%'''''r(   c                    | j         S r!   )	_anchor_yr   s    r&   r`   zIncrementalTextLayout.anchor_y  r  r(   c                    || _         |                                  |                                  |                                  d S r!   )r  r  r{   r   )r%   r`   s     r&   r`   zIncrementalTextLayout.anchor_y#  r  r(   c                    | j         S r!   )rT   r   s    r&   r]   zIncrementalTextLayout.width*  s
    {r(   c                    || j         k    rd S || _         | j                            dt          | j        j                             |                                  d S r   )rT   rN   r   r   rX   r   r   )r%   r]   s     r&   r]   zIncrementalTextLayout.width.  sT     DKF%%aT]-?)@)@AAAr(   c                    | j         S r!   )rU   r   s    r&   r^   zIncrementalTextLayout.height7  s
    |r(   c                    || j         k    rd S || _         |                                  | j        r|                                  d S d S r!   )rU   r   r   r   )r%   r^   s     r&   r^   zIncrementalTextLayout.height;  s\     T\!!F""$$$ 	(%%'''''	( 	(r(   c                    | j         S r!   )r   r   s    r&   rb   zIncrementalTextLayout.multilineE  s
    r(   c                    | j                             dt          | j        j                             || _        |                                  |                                  d S r   )rN   r   r   rX   r   r   _wrap_lines_invariantr   )r%   rb   s     r&   rb   zIncrementalTextLayout.multilineI  sR    %%aT]-?)@)@AAA#""$$$r(   c                    | j         | j        j        | j        j                 D ],}|j        D ]"}|                    | j        | j                   #-|                     d           d S )Non_translation_update)	r>   rR   r   r   r   r   rK   rL   r   r%   r"   r)   s      r&   r   z.IncrementalTextLayout._update_view_translationP  s    Jt289L9PPQ 	R 	RDz R R++D,=t?PQQQQR 	344444r(   c                    | j         | j        j        | j        j                 D ]2}|j        D ](}|                    | j        | j        | j                   )3d S r!   )	r>   rR   r   r   r   update_translationr   r  r  r!  s      r&   _update_translationz)IncrementalTextLayout._update_translationX  sq    Jt289L9PPQ 	B 	BDz B B&&twAAAAB	B 	Br(   c                D   |                                  | _        |                                 | _        | j        |                                 }}| j        | j        j        | j        j                 D ].}|}|j	        D ]"}|
                    ||           ||j        z  }#/d S r!   )r   r   r   r   r   r>   rR   r   r   r   update_anchoradvance)r%   anchor_left
anchor_topr"   r_   r)   s         r&   r  z$IncrementalTextLayout._update_anchor^  s     1133"5577#'#4d6J6J6L6LZ Jt289L9PPQ 	( 	(D #Hz ( (!!(J777CK'(	( 	(r(   c                   | j         }| j        dk    rt          d| j                   }nC| j        dk    rd}n5| j        dk    rt          d| j                   dz  }nd}t          |          | j        dk    r| |z   S | j        dk    r| | j        z   S | j        dk    rdS | j        dk    r2| j        dk    r| j         | j        dz  | j        d
z  z
  |z
  S ||dz  z
  S d}t          |          )z2Returns the anchor for the Y axis from the bottom.r   r   rW   center   =`content_valign` must be either "top", "bottom", or "center".baseliner   N   zC`anchor_y` must be either "top", "bottom", "center", or "baseline".)rU   r   r   rn   r  r   r   r   r%   r^   offsetrs   s       r&   r   z(IncrementalTextLayout._get_bottom_anchorq  s   5((DL))FF!X--FF!X--DL))Q.FFQCC.. >U""7V##>Z''7T\))>X%%1>X%%1$$)=)DMQ,>>&HHFaK''Snnr(   c                    | j         S )zRotation will always be 0 as incremental layouts cannot be rotated.

        Raises:
            NotImplementedError: Rotating IncrementalTextLayout's is not supported.
        )	_rotationr   s    r&   ra   zIncrementalTextLayout.rotation  s     ~r(   anglec                $    d}t          |          )Nz2Rotating IncrementalTextLayout's is not supported.)NotImplementedError)r%   r4  rs   s      r&   ra   zIncrementalTextLayout.rotation  s    B!#&&&r(   c                    | j         S )a4  Horizontal scroll offset.

        The initial value is 0, and the left edge of the text will touch the left
        side of the layout bounds.  A positive value causes the text to "scroll"
        to the right.  Values are automatically clipped into the range
        ``[0, content_width - width]``
        )rK   r   s    r&   view_xzIncrementalTextLayout.view_x  s       r(   r8  c                    t          dt          | j        | j        z
  |                    }|| j        k    r|| _        |                                  d S d S r   )r   r   r   rT   rK   r   )r%   r8  translations      r&   r8  zIncrementalTextLayout.view_x  s[    !S!4t{!BFKKLL$+++ +D))+++++ ,+r(   c                    | j         S )a  Vertical scroll offset.

        The initial value is 0, and the top of the text will touch the top of the
        layout bounds (unless the content height is less than the layout height,
        in which case `content_valign` is used).

        A negative value causes the text to "scroll" upwards.  Values outside of
        the range ``[height - content_height, 0]`` are automatically clipped in
        range.
        )rL   r   s    r&   view_yzIncrementalTextLayout.view_y  s       r(   r<  c                    t          dt          | j        | j        z
  |                    }|| j        k    rG|| _        |                                  |                     d           |                                  d S d S )Nr   F)r   )r   r   r^   _content_heightrL   r   r   r   )r%   r<  r:  s      r&   r<  zIncrementalTextLayout.view_y  s     !St/C!CVLLMM$+++ +D&&(((%%e%DDD))+++++	 ,+r(   c                   t          d|          }t          |t          | j        j                            }|| j        k    r|| j        k    rdS || j        k    r|| j        k     r| j                            t          || j                  t          || j                             | j                            t          || j                  t          || j                             n@| j                            | j        | j                   | j                            ||           || _        || _        | 	                                 dS )zSet the text selection range.

        If ``start`` equals ``end`` no selection will be visible.

        Args:
            start: Starting character position of selection.
            end: End of selection, exclusive.
        r   N)
r   r   r   rX   r   r@   rA   rP   r   r   )r%   r   r   s      r&   set_selectionz#IncrementalTextLayout.set_selection  s/    Au#s4=-..//D)))cT5H.H.HF&&&543F+F+F**3ud6K+L+LcRWY]YnNoNoppp**3sD4G+H+H#cSWSfJgJghhhh **4+@$BUVVV**5#666 %!r(   c                    | j         S )zStarting position of the active selection.

        :see: py:meth:`~pyglet.text.layout.IncrementalTextLayout.set_selection`
        )r@   r   s    r&   selection_startz%IncrementalTextLayout.selection_start       $$r(   c                <    |                      || j                   d S r!   )r@  rA   )r%   r   s     r&   rB  z%IncrementalTextLayout.selection_start  s!    5$"566666r(   c                    | j         S )zEnd position of the active selection (exclusive).

        :see: py:meth:`~pyglet.text.layout.IncrementalTextLayout.set_selection`
        )rA   r   s    r&   selection_endz#IncrementalTextLayout.selection_end  s     ""r(   c                <    |                      | j        |           d S r!   )r@  r@   )r%   r   s     r&   rF  z#IncrementalTextLayout.selection_end  s!    40#66666r(   c                    | j         S )zoText color of active selection.

        The color is an RGBA tuple with components in range [0, 255].
        )rD   r   s    r&   selection_colorz%IncrementalTextLayout.selection_color  rC  r(   r   c                ^    || _         | j                            | j        | j                   d S r!   )rD   rP   r   r@   rA   )r%   r   s     r&   rI  z%IncrementalTextLayout.selection_color  s/     %&&t'<d>QRRRRRr(   c                    | j         S )zuBackground color of active selection.

        The color is an RGBA tuple with components in range [0, 255].
        )rH   r   s    r&   selection_background_colorz0IncrementalTextLayout.selection_background_color
  s     //r(   r   c                ^    || _         | j                            | j        | j                   d S r!   )rH   rP   r   r@   rA   )r%   r   s     r&   rL  z0IncrementalTextLayout.selection_background_color  s/    +;(&&t'<d>QRRRRRr(   c                Z    |                      ||          }|                     ||          S )z-Get the closest document position to a point.)get_line_from_pointget_position_on_line)r%   rY   r[   r"   s       r&   get_position_from_pointz-IncrementalTextLayout.get_position_from_point  s-    ''1--((q111r(   line_idx
int | Nonetuple[float, float]c                   |'| j         d         }| j         D ]}|j        |k    r n|}n| j         |         }|j        }| j                            dt          d|dz
                      }|d}n|                     |          }||j        z  }|j        D ]>}||j        z
  dk    r||	                    |          z  } n||j        z  }||j
        z  }?||j        |z   fS )ai  Get the X, Y coordinates of a character position in the document.

        The position that ends a line has an ambiguous point: it can be either
        the end of the line, or the beginning of the next line.  You may
        optionally specify a line index to disambiguate the case.

        The resulting Y coordinate gives the baseline of the line.
        Nr   r.  r   )r>   r   rY   r}   	get_styler   _parse_distancer   r   get_point_in_boxr'  r[   )r%   r  rR  r"   r   rY   r.  r)   s           r&   get_point_from_positionz-IncrementalTextLayout.get_point_from_position  s    :a=D!Z ! !	?X--E :h'DF>++JAx!|8L8LMMHH++H55HDJ: 	 	C#*$))S))(333
"HAA$&8###r(   c                   || j         z  }||                                 | j        z   | j        z
  z  }d}| j        D ]}||j        |j        z   k    r n|dz  }|t          | j                  k    rt          | j                  dz
  }|S )z&Get the closest line index to a point.r   r   )rK   _get_content_heightrW   rL   r>   r[   r   r   )r%   rY   r[   r   r"   s        r&   rO  z)IncrementalTextLayout.get_line_from_pointB  s    	T	T%%''$+58III
J 	 	D46DL(((!OJJTZ((TZ1,Jr(   c                X    | j         |         }|j        | j        z   |j        | j        z   fS )z)Get the X, Y coordinates of a line index.)r>   rY   rK   r[   rL   )r%   rR  r"   s      r&   get_point_from_linez)IncrementalTextLayout.get_point_from_lineP  s-    z(#v))46D4E+EEEr(   c                B    d}| j         D ]}|j        |k    r n|dz  }|S )z;Get the line index of a character position in the document.r   )r>   r   )r%   r  r"   r   s       r&   r   z,IncrementalTextLayout.get_line_from_positionU  s:     	 	I))AIDDr(   c                P    t          | j        |         j        | j        z             S )z@Get the first document character position of a given line index.)r?   r>   r   r   )r%   rR  s     r&   get_position_from_linez,IncrementalTextLayout.get_position_from_line^  s!    4:h'-7888r(   c                .   | j         |         }|| j        z  }|| j        z  }||j        k     r|j        S |j        }|j        }|j        D ]K}d||z
  cxk    r|j        k     r n n||                    ||z
            z  } n||j        z  }||j        z  }L|S )zJGet the closest document position for a given line index and X coordinate.r   )	r>   rK   rV   rY   r   r   r'  get_position_in_boxr   )r%   rR  rY   r"   r  last_glyph_xr)   s          r&   rP  z*IncrementalTextLayout.get_position_on_lineb  s    z(#	T	TYtv::::v: 	# 	#CA$2222s{22222C33A4DEEECK'L
"HHr(   c                $   | j         
| j        }d}n{| j         }| j        dk    rd}nf| j        dk    rt          d| j         | j        z
            }n=| j        dk    r!t          d| j         | j        z
            dz  }nd}t	          |          ||z
  S )zMReturns the height of the layout content factoring in the vertical alignment.Nr   r   rW   r+  r,  r-  )rU   content_heightr   r   rn   r0  s       r&   r[  z)IncrementalTextLayout._get_content_heightw  s    <(FFF\F#u,,%11Qt/B BCC%11Qt/B BCCqHUnn$r(   c                    | j         }| j        dk    rdS | j        dk    r|dz   S | j        dk    r| S d}t          |          )z0Returns the anchor for the X axis from the left.rV   r   r+  r,  r   z7`anchor_x` must be either "left", "center", or "right".)r]   r  rn   )r%   r]   rs   s      r&   r   z&IncrementalTextLayout._get_left_anchor  sY    
>V##1>X%%aZ= >W$$6MGnnr(   c                4   | j         |         }|j        |j        z   }|j        |j        z   }|| j        k    r	|| _        dS || j        | j        z
  k     r|| j        z   | _        dS t          | j                  | j        | j        z
  k    r| j         | _        dS dS )zAAdjust `view_y` so that the line with the given index is visible.N)r>   r[   r   r   r<  r^   absrf  )r%   rR  r"   y1y2s        r&   ensure_line_visiblez)IncrementalTextLayout.ensure_line_visible  s    z(#Vdk!Vdl"DKKK$++++t{*DKKK 3dk AAA..DKKK BAr(   c                   || j         z  }|| j        k    r	|| _        dS || j        | j        z   k    s#|| j        | j        z   k    r!| j        | j        k    r|| j        z
  | _        dS | j        | j        z   | j        k    r| j        | _        dS dS )zAdjust ``view_x`` so that the given X coordinate is visible.

        The X coordinate is given relative to the current ``view_x``.
        N)rV   r8  r]   r   )r%   rY   s     r&   ensure_x_visiblez&IncrementalTextLayout.ensure_x_visible  s    
 	
TYDKKK$+
***qDK$*4L/L/LSWSfimisSsSsdj.DKKK[4:%(;;;-DKKK <;r(   c                    dS )a  Some or all of the layout text was reflowed.

            Text reflow is caused by document edits or changes to the layout's
            size.  Changes to the layout's position or active selection, and
            certain document edits such as text color, do not cause a reflow.

            Handle this event to update the position of a graphical element
            that depends on the laid out position of a glyph or line.

            :event:
            Nr+   r   s    r&   r   z&IncrementalTextLayout.on_layout_update  s      r(   )r   r   r   NNrV   rW   r   FNNNNT) rX   r   rY   rZ   r[   rZ   r\   rZ   r]   r?   r^   r?   r_   r   r`   r   ra   rZ   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rc   r   r   )r   r   )r   r=   )r   r?   r   r   r   r   )r   r?   r   r?   r   r   )r   r?   r   r?   r   r   r   r   )T)r   rc   r   r   )r   rZ   )rY   rZ   r   r   )r[   rZ   r   r   )r\   rZ   r   r   )r   r	  )r  r	  r   r   )r   r   )r_   r   r   r   )r   r   )r`   r   r   r   )r   r?   )r]   r?   r   r   )r^   r?   r   r   )r   rc   )rb   rc   r   r   )r4  rZ   r   r   )r8  r?   r   r   )r<  r?   r   r   )r   r?   r   r   )r   r?   r   r   )r   rC   )r   rC   r   r   )r   rC   r   r   )rY   rZ   r[   rZ   r   r?   r!   )r  r?   rR  rS  r   rT  )rR  r?   r   rT  )r  r?   r   r?   )rR  r?   r   r?   )rR  r?   rY   rZ   r   r?   )rR  r?   r   r   )Cr-   r.   r/   __doc__r6   r@   rA   rD   rH   r1   rI   r9   rJ   rK   rL   rr   r{   r   r   r   r   r~   r   r   r   r   r   r   r   r   propertyrY   setterr[   r\   r  r_   r`   r]   r^   rb   r   r$  r  r   ra   r8  r<  r@  rB  rF  rI  rL  rQ  rY  rO  r]  r   ra  rP  r[  r   rl  rn  r   r   __classcell__)rt   s   @r&   r;   r;   :   ss         0 N2FFFFF=PPPPP>XKXXXXGeeeeeLL""""    !!!!!!!!((((!!!!    KKKLLL 9:26rw]aJN. . . . . . .B= = = =4 4 4 49 9 9 9         2   6   &4 &4 &4 &4P.B .B .B .B`N0 N0 N0 N0`J J J J& & & &.8( 8( 8( 8( 8(t    X X$ $ $ $ $ X$    X X$ $ $ $ $ X$    X X$ $ $ $ $ X$ ) ) ) X) _$ $ $ $ $ _$    X _( ( ( _(    X _( ( ( _(    X \   \    X ]( ( ( ](    X    5 5 5 5B B B B( ( ( (&   :    X _' ' ' _' ! ! ! X! ], , , ], ! ! ! X! ], , , ],   8 % % % X% 7 7 7 7 # # # X# 7 7 7 7 % % % X% S S S S 0 0 0 X0  &S S S '&S2 2 2 2
"$ "$ "$ "$ "$H   F F F F
   9 9 9 9   *   &   
/ 
/ 
/ 
/. . . .   	 	 	 	 	 	 	 	   r(   r;   r   r   )+
__future__r   r   typingr   r   r   r   pyglet.eventr   pyglet.font.baser   r	   pyglet.textr
   pyglet.text.layout.baser   r   r   r   r   r   r   pyglet.text.layout.scrollingr   r   pyglet.customtypesr   r   pyglet.graphicsr   r   pyglet.graphics.shaderr   pyglet.graphics.vertexdomainr   pyglet.text.documentr   r   r1   r9   r;   register_event_typer+   r(   r&   <module>r     s`   " " " " " " 



 / / / / / / / / / /     ( ( ( ( ( ( : : : : : : : :                        b a a a a a a a 633333333,,,,,,,,444444777777555555       C C C C C!: C C CC C C C C%B C C CF F F F FJ F F FR  ) )*< = = =  ) )*A B B B B Br(   