
    ^jdg                    .   d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ e	rddlmZ dd	lmZ  eed
          oej        ZdZ G d de          Z G d dej                  Ze                    d           e                    d           e                    d            G d de          Z G d de          Zd$dZ G d dej                  Z  G d  d!ej                  Z! G d" d#ej                  Z" e"            Z#dS )%a  Formatted and unformatted document interfaces used by text layout.

Abstract representation
=======================

Styled text in pyglet is represented by one of the :py:class:`~pyglet.text.document.AbstractDocument` classes,
which manage the state representation of text and style independently of how
it is loaded or rendered.

A document consists of the document text (a Unicode string) and a set of
named style ranges.  For example, consider the following (artificial)
example::

    0    5   10   15   20
    The cat sat on the mat.
    +++++++        +++++++    "weight"
                ++++++      "italic"

If this example were to be rendered, "The cat" and "the mat" would have a weight
set ("bold", "thin", etc.), and "on the" would be in italics.  Note that the second
"the" is both weighted and italic.

The document styles recorded for this example would be a specific ``"weight"`` over
ranges (0-7) & (15-22) and ``"italic"`` over range (12-18).  Overlapping styles are
permitted; unlike HTML and other structured markup, the ranges need not be nested.

The document has no knowledge of the semantics of ``"weight"`` names or
``"italic"``, it stores only the style names.  The pyglet layout classes give
meaning to these style names in the way they are rendered; but you are also free
to invent your own style names (which will be ignored by the layout classes).
This can be useful to tag areas of interest in a document, or maintain
references back to the source material.

As well as text, the document can contain arbitrary elements represented by
:py:class:`~pyglet.text.document.InlineElement`.  An inline element behaves
like a single character in the document, but can be rendered by the application.

Paragraph breaks
================

Paragraph breaks are marked with a "newline" character (U+0010).  The Unicode
paragraph break (U+2029) can also be used.

Line breaks (U+2028) can be used to force a line break within a paragraph.

See Unicode recommendation UTR #13 for more information:
https://www.unicode.org/standard/reports/tr13/tr13-5.html.

Document classes
================

Any class implementing :py:class:`~pyglet.text.document.AbstractDocument` provides an interface to a
document model as described above.  In theory a structured document such as
HTML or XML could export this model, though the classes provided by pyglet
implement only unstructured documents.

The :py:class:`~pyglet.text.document.UnformattedDocument` class assumes any styles set are set over the entire
document.  So, regardless of the range specified when setting a ``"weight"``
style attribute, for example, the entire document will receive that style.

The :py:class:`~pyglet.text.document.FormattedDocument` class implements the document model directly, using
the `RunList` class to represent style runs efficiently.

Style attributes
================

The following character style attribute names are recognised by pyglet:

``font_name``
    Font family name, as given to :py:func:`pyglet.font.load`.
``font_size``
    Font size, in points.
``weight``
    String.
``italic``
    Boolean.
``underline``
    4-tuple of ints in range (0, 255) giving RGBA underline color, or None
    (default) for no underline.
``kerning``
    Additional space to insert between glyphs, in points.  Defaults to 0.
``baseline``
    Offset of glyph baseline from line baseline, in points.  Positive values
    give a superscript, negative values give a subscript.  Defaults to 0.
``color``
    4-tuple of ints in range (0, 255) giving RGBA text color
``background_color``
    4-tuple of ints in range (0, 255) giving RGBA text background color; or
    ``None`` for no background fill.

The following paragraph style attribute names are recognised by pyglet.  Note
that paragraph styles are handled no differently from character styles by the
document: it is the application's responsibility to set the style over an
entire paragraph, otherwise results are undefined.

``align``
    ``left`` (default), ``center`` or ``right``.
``indent``
    Additional horizontal space to insert before the first
``leading``
    Additional space to insert between consecutive lines within a paragraph,
    in points.  Defaults to 0.
``line_spacing``
    Distance between consecutive baselines in a paragraph, in points.
    Defaults to ``None``, which automatically calculates the tightest line
    spacing for each line based on the font ascent and descent.
``margin_left``
    Left paragraph margin, in pixels.
``margin_right``
    Right paragraph margin, in pixels.
``margin_top``
    Margin above paragraph, in pixels.
``margin_bottom``
    Margin below paragraph, in pixels.  Adjacent margins do not collapse.
``tab_stops``
    List of horizontal tab stops, in pixels, measured from the left edge of
    the text layout.  Defaults to the empty list.  When the tab stops
    are exhausted, they implicitly continue at 50 pixel intervals.
``wrap``
    Boolean.  If True (the default), text wraps within the width of the layout.

Other attributes can be used to store additional style information within the
document; it will be ignored by the built-in text classes.

All style attributes (including those not present in a document) default to
``None`` (including the so-called "boolean" styles listed above).  The meaning
of a ``None`` style is style- and application-dependent.
    )annotationsN)ABCabstractmethod)TYPE_CHECKINGAny	Generator)event)runlist)Font)
TextLayoutis_pyglet_doc_runindeterminatec                     e Zd ZU dZded<   ded<   ded<   ded<   d&d
Zed'd            Zed(d            Z	ed)d            Z
ed*d            Zed+d             Zed,d!            Zed-d"            Zed.d#            Zed/d$            Zd%S )0InlineElementa  Arbitrary inline element positioned within a formatted document.

    Elements behave like a single glyph in the document.  They are
    measured by their horizontal advance, ascent above the baseline, and
    descent below the baseline.

    The pyglet layout classes reserve space in the layout for elements and
    call the element's methods to ensure they are rendered at the
    appropriate position.

    If the size of an element (any of the ``advance``, ``ascent``, or ``descent``
    variables) is modified it is the application's responsibility to
    trigger a reflow of the appropriate area in the affected layouts.  This
    can be done by forcing a style change over the element's position.
    intadvancedescentascent
int | None	_positionreturnNonec                >    || _         || _        || _        d| _        dS )a=  Initialize the element.

        Args:
            ascent:
                Ascent of the element above the baseline, in pixels.
            descent:
                Descent of the element below the baseline, in pixels. Typically negative.
            advance:
                Width of the element, in pixels.
        N)r   r   r   r   )selfr   r   r   s       O/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/text/document.py__init__zInlineElement.__init__   s$         c                    | j         S )zCharacter position within the document.

        Determined by the layout it is in. Will return ``None`` if it has not been placed.
        )r   r   s    r   positionzInlineElement.position   s     ~r   layoutr   xfloatyzline_xline_yrotationvisibleboolanchor_xanchor_yc                    d S N )r   r!   r"   r$   r%   r&   r'   r(   r)   r+   r,   s              r   placezInlineElement.place   s	     	r   c                    d S r.   r/   )r   r"   r$   r%   s       r   update_translationz InlineElement.update_translation       r   color	list[int]c                    d S r.   r/   )r   r4   s     r   update_colorzInlineElement.update_color   r3   r   translate_xtranslate_yc                    d S r.   r/   )r   r8   r9   s      r   update_view_translationz%InlineElement.update_view_translation   r3   r   c                    d S r.   r/   )r   r(   s     r   update_rotationzInlineElement.update_rotation   r3   r   c                    d S r.   r/   )r   r)   s     r   update_visibilityzInlineElement.update_visibility   r3   r   c                    d S r.   r/   )r   r+   r,   s      r   update_anchorzInlineElement.update_anchor   r3   r   c                    d S r.   r/   )r   r!   s     r   removezInlineElement.remove   r3   r   N)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   r   )r"   r#   r$   r#   r%   r#   r   r   )r4   r5   r   r   )r8   r#   r9   r#   r   r   )r(   r#   r   r   )r)   r*   r   r   )r+   r#   r,   r#   r   r   )r!   r   r   r   )__name__
__module____qualname____doc____annotations__r   propertyr    r   r0   r2   r7   r;   r=   r?   rA   rC   r/   r   r   r   r      s}          LLLLLLKKK        X    ^    ^    ^    ^    ^    ^    ^    ^  r   r   c                      e Zd ZU dZ ej        d          Zded<    ej        d          Zded<   d4d5 fdZ	e
d6d            Zej        d5d            Zd7dZd7dZed8d            Zed9d:d            Zd;dZed<d=d             Zed<d>d"            Zd<d?d%Zd?d&Zd@d'Zd@d(Z	 d<dAd+ZdBd,ZdCd.ZedCd/            ZdCd0ZerdDd1Zd@d2Z dEd3Z! xZ"S  xZ"S )FAbstractDocumenta  Abstract document interface used by all :py:mod:`pyglet.text` classes.

    This class can be overridden to interface pyglet with a third-party
    document format.  It may be easier to implement the document format in
    terms of one of the supplied concrete classes :py:class:`~pyglet.text.document.FormattedDocument` or
    :py:class:`~pyglet.text.document.UnformattedDocument`.
    u
   
[^
 ]*$zre.Pattern[str]_previous_paragraph_reu   [
 ]_next_paragraph_re textstrr   r   c                    t                                                       d| _        g | _        |r|                     d|           dS dS )z_Initialize a document with text.

        Args:
            text: Initial text string.
        rN   r   N)superr   _text	_elementsinsert_textr   rO   	__class__s     r   r   zAbstractDocument.__init__   sW     	
.0 	&Q%%%%%	& 	&r   c                    | j         S )a   Document text.

        For efficient incremental updates, use the :py:func:`~pyglet.text.document.AbstractDocument.insert_text` and
        :py:func:`~pyglet.text.document.AbstractDocument.delete_text` methods instead of replacing this property.

        )rS   r   s    r   rO   zAbstractDocument.text   s     zr   c                    || j         k    rd S |                     dt          | j                              |                     d|           d S Nr   )rS   delete_textlenrU   )r   rO   s     r   rO   zAbstractDocument.text  sN    4:FC
OO,,,D!!!!!r   posr   c                   | j         d|dz                                d          s%| j         d|dz                                d          r|S | j                            | j         d|dz             }|sdS |                                dz   S )zEGet the starting position of a paragraph from the character position.N   
u    r   )rS   endswithrL   searchstartr   r]   ms      r   get_paragraph_startz$AbstractDocument.get_paragraph_start  s     :hsQwh((.. 	$*XcAgX2F2O2OPX2Y2Y 	J"&"="D"DTZQRTWZ[T["\"\ 	1wwyy1}r   c                    | j                             | j        |          }|st          | j                  S |                                dz   S )z@Get the end position of a paragraph from the character position.r_   )rM   rb   rS   r\   rc   rd   s      r   get_paragraph_endz"AbstractDocument.get_paragraph_end  sD    "&"9"@"@S"Q"Q 	#tz??"wwyy1}r   	attributerunlist.AbstractRunIteratorc                    dS )zGet a style iterator over the given style attribute.

        Args:
            attribute:
                Name of style attribute to query.
        Nr/   r   ri   s     r   get_style_runszAbstractDocument.get_style_runs         r   r   r    r   c                    dS )a2  Get an attribute style at the given position.

        Args:
            attribute:
                Name of style attribute to query.
            position:
                Character position of document to query.

        Returns:
            The style set for the attribute at the given position.
        Nr/   r   ri   r    s      r   	get_stylezAbstractDocument.get_style)  rn   r   rc   endc                    |                      |          }t          |                    ||                    \  }}}||k     rt          S |S )a  Get an attribute style over the given range.

        If the style varies over the range, `STYLE_INDETERMINATE` is returned.

        Args:
            attribute:
                Name of style attribute to query.
            start:
                Starting character position.
            end:
                Ending character position (exclusive).

        Returns:
            The style set for the attribute over the given range, or `STYLE_INDETERMINATE` if more than one value is
            set.
        )rm   nextrangesSTYLE_INDETERMINATE)r   ri   rc   rr   iterable_	value_endvalues           r   get_style_rangez AbstractDocument.get_style_range7  sM    " 150C0CI0N0N"8??5##>#>??9es??&&r   Ndpir   c                    dS )a  Get a style iterator over the `pyglet.font.Font` instances used in the document.

        The font instances are created on-demand by inspection of the
        ``font_name``, ``font_size``, ``weight`` and ``italic`` style
        attributes.

        Args:
            dpi:
                Optional resolution to construct fonts at.
                :see: :py:func:`~pyglet.font.load`.
        Nr/   r   r|   s     r   get_font_runszAbstractDocument.get_font_runsO  rn   r   r   c                    dS )a7  Get the font instance used at the given position.

        :see: `get_font_runs`

        Args:
            position:
                Character position of document to query.
            dpi:
                Optional resolution to construct fonts at.
                :see: :py:func:`~pyglet.font.load`.
        Nr/   )r   r    r|   s      r   get_fontzAbstractDocument.get_font]  rn   r   
attributesdict[str, Any] | Nonec                b    |                      |||           |                     d||           dS )a  Insert text into the document.

        Dispatches an :py:meth:`~pyglet.text.document.AbstractDocument.on_insert_text` event.

        Args:
            start:
                Character insertion point within document.
            text:
                Text to insert.
            attributes:
                Optional dictionary giving named style attributes of the inserted text.
        on_insert_textN)_insert_textdispatch_event)r   rc   rO   r   s       r   rU   zAbstractDocument.insert_textk  s;     	%z222,eT:::::r   c                    d                     | j        d |         || j        |d          f          | _        t          |          }| j        D ]&}|j        J |j        |k    r|xj        |z  c_        'd S )NrN   )joinrS   r\   rT   r   )r   rc   rO   r   len_textelements         r   r   zAbstractDocument._insert_text{  s    WWdj%0$
5668JKLL
t99~ 	. 	.G$000 E))!!X-!!	. 	.r   c                `    |                      ||           |                     d||           dS )a  Delete text from the document.

        Dispatches an :py:meth:`on_delete_text` event.

        Args:
            start:
                Starting character position to delete from.
            end:
                Ending character position to delete to (exclusive).

        on_delete_textN)_delete_textr   r   rc   rr   s      r   r[   zAbstractDocument.delete_text  s9     	%%%%,eS99999r   c                *   t          | j                  D ]Y}|j        J ||j        cxk    r|k     rn n| j                            |           ;|j        |k    r|xj        ||z
  z  c_        Z| j        d |         | j        |d          z   | _        d S r.   )listrT   r    r   rC   rS   )r   rc   rr   r   s       r   r   zAbstractDocument._delete_text  s    DN++ 	3 	3G#///)////C/////%%g...."c))!!cEk2!!Z'$*STT*::


r   r   r   c                    |j         
J d            |                     |d|           ||_        | j                            |           | j                            d            dS )a  Insert a element into the document.

        See the :py:class:`~pyglet.text.document.InlineElement` class documentation for details of usage.

        Args:
            position:
                Character insertion point within document.
            element:
                Element to insert.
            attributes: dict
                Optional dictionary giving named style attributes of the inserted text.

        Nz!Element is already in a document. c                    | j         S r.   r    )ds    r   <lambda>z1AbstractDocument.insert_element.<locals>.<lambda>  s    !* r   )key)r    rU   r   rT   appendsort)r   r    r   r   s       r   insert_elementzAbstractDocument.insert_element  sw     '')L'''4444$g&&& 	 4 455555r   c                \    | j         D ]}|j        |k    r|c S d| }t          |          )zGet the element at a specified position.

        Args:
            position:
                Position in the document of the element.

        zNo element at position )rT   r   RuntimeError)r   r    r   msgs       r   get_elementzAbstractDocument.get_element  sL     ~ 	 	G H,, -2223r   dict[str, Any]c                d    |                      |||           |                     d|||           dS )a  Set text style of a range between start and end of the document.

        Dispatches an :py:meth:`~pyglet.text.document.AbstractDocument.on_style_text` event.

        Args:
            start:
                Starting character position.
            end:
                Ending character position (exclusive).
            attributes:
                Dictionary giving named style attributes of the text.

        on_style_textN)
_set_styler   r   rc   rr   r   s       r   	set_stylezAbstractDocument.set_style  s:     	sJ///OUCDDDDDr   c                    d S r.   r/   r   s       r   r   zAbstractDocument._set_style  r3   r   c                    |                      |          }|                     |          }|                     |||           |                     d|||           dS )a   Set the style for a range of paragraphs.

        This is a convenience method for `set_style` that aligns the character range to the enclosing paragraph(s).

        Dispatches an :py:meth:`~pyglet.text.document.AbstractDocument.on_style_text` event.

        Args:
            start:
                Starting character position.
            end:
                Ending character position (exclusive).
            attributes:
                Dictionary giving named style attributes of the paragraphs.

        r   N)rf   rh   r   r   r   s       r   set_paragraph_stylez$AbstractDocument.set_paragraph_style  s`      ((//$$S))sJ///OUCDDDDDr   c                    dS )zText was inserted into the document.

            Args:
                start:
                    Character insertion point within document.
                text:
                    The text that was inserted.

            :event:
            Nr/   )r   rc   rO   s      r   r   zAbstractDocument.on_insert_text  rn   r   c                    dS )a  Text was deleted from the document.

            Args:
                start:
                    Starting character position of deleted text.
                end:
                    Ending character position of deleted text (exclusive).

            :event:
            Nr/   r   s      r   r   zAbstractDocument.on_delete_text  rn   r   c                    dS )a  Text character style was modified.

            Args:
                start:
                    Starting character position of modified text.
                end:
                    Ending character position of modified text (exclusive).
                attributes:
                    Dictionary giving updated named style attributes of the text.

            :event:
            Nr/   r   s       r   r   zAbstractDocument.on_style_text  rn   r   rN   rO   rP   r   r   )r   rP   )r]   r   r   r   )ri   rP   r   rj   r   )ri   rP   r    r   r   r   )ri   rP   rc   r   rr   r   r   r   r.   )r|   r   r   rj   r    r   r|   r   r   r   )rc   r   rO   rP   r   r   r   r   rc   r   rr   r   r   r   )r    r   r   r   r   r   r   r   )r    r   r   r   rc   r   rr   r   r   r   r   r   )rc   r   rO   rP   r   r   )rc   r   rr   r   r   r   r   r   )#rD   rE   rF   rG   recompilerL   rH   rM   r   rI   rO   setterrf   rh   r   rm   rq   r{   r   r   rU   r   r[   r   r   r   r   r   r   _is_pyglet_doc_runr   r   r   __classcell__rW   s   @r   rK   rK      s          /9bj9J.K.KKKKK*4"*\*B*BBBBB& & & & & & &    X 
[" " " ["
 
 
 
       ^     ^   0     ^     ^; ; ; ; ; . . . .: : : :; ; ; ; <@6 6 6 6 6.       E E E E"    ^E E E E*  %
	 
	 
	 
	
	 
	 
	 
		 	 	 	 	 	 	 	3% % % %r   rK   r   r   r   c                  t     e Zd ZdZdd fdZdd
Zd d!dZd" fdZd"dZd" fdZ	d d#dZ
d$d%dZd&dZ xZS )'UnformattedDocumentzA document having uniform style over all text.

    Changes to the style of text within the document affects the entire
    document.  For convenience, the ``position`` parameters of the style
    methods may therefore be omitted.
    rN   rO   rP   r   r   c                X    t                                          |           i | _        dS )z*Create unformatted document with a string.N)rR   r   stylesrV   s     r   r   zUnformattedDocument.__init__   s&    &(r   ri   runlist.ConstRunIteratorc                    | j                             |          }t          j        t	          | j                  |          S r.   )r   getr
   ConstRunIteratorr\   rO   )r   ri   rz   s      r   rm   z"UnformattedDocument.get_style_runs%  s0    	**'DI>>>r   Nr    r   r   c                6    | j                             |          S r.   )r   r   rp   s      r   rq   zUnformattedDocument.get_style)  s    {y)))r   rc   r   rr   r   r   c                n    t                                          dt          | j                  |          S rZ   )rR   r   r\   rO   r   rc   rr   r   rW   s       r   r   zUnformattedDocument.set_style,  s'    ww  C	NNJ???r   c                :    | j                             |           d S r.   )r   updater   s       r   r   zUnformattedDocument._set_style/  s    :&&&&&r   c                n    t                                          dt          | j                  |          S rZ   )rR   r   r\   rO   r   s       r   r   z'UnformattedDocument.set_paragraph_style2  s'    ww**1c$)nnjIIIr   r|   c                |    |                      |          }t          j        t          | j                  |          S )N)r|   )r   r
   r   r\   rO   )r   r|   fts      r   r   z!UnformattedDocument.get_font_runs5  s0    ==S=))'DI;;;r   r   c                N   ddl m} | j                            d          }| j                            d          }| j                            dd          }| j                            dd          }| j                            d	d          }|                    ||||||
          S )Nr   font	font_name	font_sizeweightnormalitalicFstretchr   r   r   r|   )pygletr   r   r   load)	r   r    r|   r   r   r   r   r   r   s	            r   r   zUnformattedDocument.get_font9  s    KOOK00	KOOK00	844511+//)U33yyIfVU\beyfffr   c                P    t          j        t          | j                  d           S r.   )r
   r   r\   rS   r   s    r   get_element_runsz$UnformattedDocument.get_element_runsB  s    'DJ>>>r   r   r   )ri   rP   r   r   r.   )ri   rP   r    r   r   r   r   )r|   r   r   r   )NN)r    r   r|   r   r   r   )r   r   )rD   rE   rF   rG   r   rm   rq   r   r   r   r   r   r   r   r   s   @r   r   r     s         ) ) ) ) ) ) )
? ? ? ?* * * * *@ @ @ @ @ @' ' ' 'J J J J J J< < < < <g g g g g? ? ? ? ? ? ? ?r   r   c                  t     e Zd ZdZd d! fdZd"d
Zd#d$dZd%dZd&d'dZd&d(dZ	d)dZ
d* fdZd+ fdZ xZS ),FormattedDocumentzSimple implementation of a document that maintains text formatting.

    Changes to text style are applied according to the description in
    :py:class:`~pyglet.text.document.AbstractDocument`.  All styles default to ``None``.
    rN   rO   rP   r   r   c                X    i | _         t                                          |           d S r.   )_style_runsrR   r   rV   s     r   r   zFormattedDocument.__init__M  s)    79r   ri   +runlist.RunIterator | _NoStyleRangeIteratorc                p    	 | j         |                                         S # t          $ r
 t          cY S w xY wr.   )r   get_run_iteratorKeyError_no_style_range_iteratorrl   s     r   rm   z FormattedDocument.get_style_runsQ  sH    	,#I.??AAA 	, 	, 	,++++	,s   ! 55r   r    r   
Any | Nonec                L    	 | j         |         |         S # t          $ r Y d S w xY wr.   )r   r   rp   s      r   rq   zFormattedDocument.get_styleW  s<    	#I.x88 	 	 	44	s    
##rc   rr   r   r   c                4   |                                 D ]\  }}	 | j        |         }nW# t          $ rJ t          j        dd           x}| j        |<   |                    dt          | j                             Y nw xY w|                    |||           d S rZ   )	itemsr   r   r
   RunListinsertr\   rS   set_run)r   rc   rr   r   ri   rz   runss          r   r   zFormattedDocument._set_style]  s     * 0 0 2 2 	, 	,Iu0'	2 0 0 05<_Q5M5MMt'	2As4://///0 LLU++++	, 	,s   )AA=<A=Nr|   r   _FontStyleRunsRangeIteratorc           	         t          |                     d          |                     d          |                     d          |                     d          |                     d          |          S )Nr   r   r   r   r   )r   rm   r~   s     r   r   zFormattedDocument.get_font_runsf  sl    *,,,,))))	**  	r   r   c                <    |                      |          }||         S r.   )r   )r   r    r|   	runs_iters       r   r   zFormattedDocument.get_fonto  s     &&s++	""r   _ElementIteratorc                P    t          | j        t          | j                            S r.   )r   rT   r\   rS   r   s    r   r   z"FormattedDocument.get_element_runss  s    DJ@@@r   c                   t                                          |||           t          |          }| j                                        D ]}|                    ||           ||                                D ]\  }}	 | j        |         }nW# t          $ rJ t          j	        dd           x}| j        |<   |                    dt          | j
                             Y nw xY w|                    |||z   |           d S d S rZ   )rR   r   r\   r   valuesr   r   r   r
   r   rO   r   )	r   rc   rO   r   r   r   ri   rz   rW   s	           r   r   zFormattedDocument._insert_textv  s&   UD*555t99$++-- 	) 	)DKKx((((!$.$4$4$6$6 = = 	53+I6DD 3 3 39@D9Q9QQD4+I6KK3ty>>222223 UEH$4e<<<< "!= =s   BAC$#C$c                    t                                          ||           | j                                        D ]}|                    ||           d S r.   )rR   r   r   r   delete)r   rc   rr   r   rW   s       r   r   zFormattedDocument._delete_text  s\    UC((($++-- 	$ 	$DKKs####	$ 	$r   r   r   )ri   rP   r   r   r   )ri   rP   r    r   r   r   r   r.   )r|   r   r   r   r   )r   r   )rc   r   rO   rP   r   r   r   r   r   )rD   rE   rF   rG   r   rm   rq   r   r   r   r   r   r   r   r   s   @r   r   r   F  s              , , , ,    , , , ,    # # # # #A A A A= = = = = = $ $ $ $ $ $ $ $ $ $r   r   elementslist[InlineElement]lengthr   r   <Generator[tuple[int, int, InlineElement | None], None, None]c              #  h   K   d}| D ]#}|j         }|J ||d fV  ||dz   |fV  |dz   }$||d fV  d S )Nr   r_   r   )r   r   lastr   ps        r   _iter_elementsr     sx      D  }}}AtmQ1u

r   c                      e Zd Zd	dZdS )
r   r   r   r   r   r   r   c                t    t          ||          | _        t          |           \  | _        | _        | _        d S r.   )r   _run_list_iterrt   rc   rr   rz   )r   r   r   s      r   r   z_ElementIterator.__init__  s0    ,Xv>>+/::(
DHdjjjr   N)r   r   r   r   r   r   )rD   rE   rF   r   r/   r   r   r   r     s(        6 6 6 6 6 6r   r   c                  &    e Zd ZddZddZddZdS )r   
font_namesrunlist.RunIterator
font_sizesweightsitalicsr   r|   r   r   r   c                P    t          j        |||||f          | _        || _        d S r.   )r
   ZipRunIteratorzip_iterr|   )r   r   r   r   r  r   r|   s          r   r   z$_FontStyleRunsRangeIterator.__init__  s,    .
JQXZa/bccr   rc   r   rr   ,Generator[tuple[int, int, Font], None, None]c           	   #     K   ddl m} | j                            ||          D ]F\  }}}|\  }}}	}
}|                    |||	pdt          |
          |pd| j                  }|||fV  Gd S Nr   r   r   Fr   )r   r   r  ru   r   r*   r|   )r   rc   rr   r   start_end_r   r   r   r   r   r   r   s                r   ru   z"_FontStyleRunsRangeIterator.ranges  s      $(M$8$8$D$D 	# 	# FD&<B9Iy&&'9i8J(SWX^S_S_ipiyty  @D  @H  I  IB$"""""	# 	#r   indexr   c                    ddl m} | j        |         \  }}}}}|                    |||pdt	          |          |pd| j                  S r  )r   r   r  r   r*   r|   )r   r
  r   r   r   r   r   r   s           r   __getitem__z'_FontStyleRunsRangeIterator.__getitem__  st    8<e8L5	9ffgyyIf6HQUV\Q]Q]gngwrw  ~B  ~Fy  G  G  	Gr   N)r   r   r   r   r   r   r  r   r   r   r|   r   r   r   )rc   r   rr   r   r   r  )r
  r   r   r   rD   rE   rF   r   ru   r  r/   r   r   r   r     sV           
# # # #G G G G G Gr   r   c                  &    e Zd ZddZddZdd
ZdS )_NoStyleRangeIteratorr   r   c                    d S r.   r/   r   s    r   r   z_NoStyleRangeIterator.__init__  s    r   rc   r   rr   ,Generator[tuple[int, int, None], None, None]c              #     K   ||d fV  d S r.   r/   r   s      r   ru   z_NoStyleRangeIterator.ranges  s      S$r   r
  c                    d S r.   r/   )r   r
  s     r   r  z!_NoStyleRangeIterator.__getitem__  s    tr   N)r   r   )rc   r   rr   r   r   r  )r
  r   r   r   r  r/   r   r   r  r    sP                   r   r  )r   r   r   r   r   r   )$rG   
__future__r   r   sysabcr   r   typingr   r   r   r   r	   pyglet.textr
   pyglet.font.baser   pyglet.text.layoutr   hasattrr   r   rv   r   EventDispatcherrK   register_event_typer   r   r   RunIteratorr   r   r  r   r/   r   r   <module>r     s   @ # " " " " " 				 



 # # # # # # # # 0 0 0 0 0 0 0 0 0 0             .%%%%%%------WS"566P3;P  & L L L L LC L L L^l l l l lu, l l l^	  $ $%5 6 6 6  $ $%5 6 6 6  $ $_ 5 5 5+? +? +? +? +?* +? +? +?\C$ C$ C$ C$ C$( C$ C$ C$L	 	 	 	6 6 6 6 6w* 6 6 6G G G G G'"5 G G G(    G/    1022   r   