
    \
jG                         d Z ddlZddlZddlmZmZmZ  G d de          Z	 G d d          Z
ddZdd	Zdd
Zd Zd Z G d dej                  Z G d de          Z G d de          ZdS )a  Text formatting, layout and display.

This module provides classes for loading styled documents from text files,
HTML files and a pyglet-specific markup format.  Documents can be styled with
multiple fonts, colours, styles, text sizes, margins, paragraph alignments,
and so on.  

Using the layout classes, documents can be laid out on a single line or
word-wrapped to fit a rectangle.  A layout can then be efficiently drawn in
a window or updated incrementally (for example, to support interactive text
editing).

The label classes provide a simple interface for the common case where an
application simply needs to display some text in a window.

A plain text label can be created with::

    label = pyglet.text.Label('Hello, world', 
                              font_name='Times New Roman', 
                              font_size=36,
                              x=10, y=10)

Alternatively, a styled text label using HTML can be created with::

    label = pyglet.text.HTMLLabel('<b>Hello</b>, <i>world</i>',
                                  x=10, y=10)

Either label can then be drawn at any time with::

    label.draw()

For details on the subset of HTML supported, see `pyglet.text.formats.html`.

Refer to the Programming Guide for advanced usage of the document and layout
classes, including interactive editing, embedding objects within documents and
creating scrollable layouts.

.. versionadded:: 1.1
    N)layoutdocumentcaretc                       e Zd ZdZdS )DocumentDecodeExceptionz)An error occurred decoding document text.N)__name__
__module____qualname____doc__     N/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/text/__init__.pyr   r   R   s        33Dr   r   c                       e Zd ZdZddZdS )DocumentDecoderzAbstract document decoder.
    Nc                      t          d          )aM  Decode document text.
        
        :Parameters:
            `text` : str
                Text to decode
            `location` : `Location`
                Location to use as base path for additional resources
                referenced within the document (for example, HTML images).

        :rtype: `AbstractDocument`
        abstract)NotImplementedError)selftextlocations      r   decodezDocumentDecoder.decode[   s     "*---r   N)r   r	   r
   r   r   r   r   r   r   r   W   s2         . . . . . .r   r   c                 d   |=t           j                            |           \  }}|                                dv rd}nd}|dk    rddlm} |                                S |dk    rddlm} |                                S |dk    rdd	lm	} |
                                S t          d
|z            )a  Get a document decoder for the given filename and MIME type.

    If `mimetype` is omitted it is guessed from the filename extension.

    The following MIME types are supported:

    ``text/plain``
        Plain text
    ``text/html``
        HTML 4 Transitional
    ``text/vnd.pyglet-attributed``
        Attributed text; see `pyglet.text.formats.attributed`

    `DocumentDecodeException` is raised if another MIME type is given.

    :Parameters:
        `filename` : str
            Filename to guess the MIME type from.  If a MIME type is given,
            the filename is ignored.
        `mimetype` : str
            MIME type to lookup, or ``None`` to guess the type from the
            filename.

    :rtype: `DocumentDecoder`
    N)z.htmz.htmlz.xhtml	text/html
text/plainr   )	plaintext)htmltext/vnd.pyglet-attributed)
attributedzUnknown format "%s")ospathsplitextlowerpyglet.text.formatsr   PlainTextDecoderr   HTMLDecoderr   AttributedTextDecoderr   )filenamemimetype_extr   r   r   s          r   get_decoderr,   j   s    4 !!(++399;;555"HH#H<111111))+++	[	 	 ,,,,,,!!!	1	1	1222222//111%&;h&FGGGr   c                    t          | |          }|s<t          |           5 }|                                }ddd           n# 1 swxY w Y   n(|                                }|                                 t	          |d          r|                                }t          j                            t          j
                            |                     }|                    ||          S )a  Load a document from a file.

    :Parameters:
        `filename` : str
            Filename of document to load.
        `file` : file-like object
            File object containing encoded data.  If omitted, `filename` is
            loaded from disk.
        `mimetype` : str
            MIME type of the document.  If omitted, the filename extension is
            used to guess a MIME type.  See `get_decoder` for a list of
            supported MIME types.

    :rtype: `AbstractDocument`
    Nr   )r,   openreadclosehasattrr   pygletresourceFileLocationr    r!   dirname)r(   filer)   decoderffile_contentsr   s          r   loadr:      s      (H--G (^^ 	%qFFHHM	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 		

}h'' /%,,..++BGOOH,E,EFFH>>-222s   AA
Ac                 N    t          dd          }|                    | |          S )a<  Create a document directly from some HTML formatted text.

    :Parameters:
        `text` : str
            HTML data to decode.
        `location` : str
            Location giving the base path for additional resources
            referenced from the document (e.g., images).

    :rtype: `FormattedDocument`
    Nr   r,   r   )r   r   r7   s      r   decode_htmlr=      s'     $,,G>>$)))r   c                 L    t          dd          }|                    |           S )zCreate a document directly from some attributed text.

    See `pyglet.text.formats.attributed` for a description of attributed text.

    :Parameters:
        `text` : str
            Attributed text to decode.

    :rtype: `FormattedDocument`
    Nr   r<   r   r7   s     r   decode_attributedr@      s&     $ <==G>>$r   c                 L    t          dd          }|                    |           S )zCreate a document directly from some plain text.

    :Parameters:
        `text` : str
            Plain text to initialise the document with.

    :rtype: `UnformattedDocument`
    Nr   r<   r?   s     r   decode_textrB      s%     $--G>>$r   c                       e Zd ZdZ	 	 	 	 d fd	Zed             Zej        d	             Zed
             Zej        d             Zed             Z	e	j        d             Z	ed             Z
e
j        d             Z
ed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zd Zd Z xZS )DocumentLabelz~Base label class.

    A label is a layout that exposes convenience methods for manipulating the
    associated document.
    Nr   leftbaselineFc           	          t          t          |                               |||||	|
|           || _        || _        || _        || _        |                                  dS )a  Create a label for a given document.

        :Parameters:
            `document` : `AbstractDocument`
                Document to attach to the layout.
            `x` : int
                X coordinate of the label.
            `y` : int
                Y coordinate of the label.
            `width` : int
                Width of the label in pixels, or None
            `height` : int
                Height of the label in pixels, or None
            `anchor_x` : str
                Anchor point of the X coordinate: one of ``"left"``,
                ``"center"`` or ``"right"``.
            `anchor_y` : str
                Anchor point of the Y coordinate: one of ``"bottom"``,
                ``"baseline"``, ``"center"`` or ``"top"``.
            `multiline` : bool
                If True, the label will be word-wrapped and accept newline
                characters.  You must also set the width of the label.
            `dpi` : float
                Resolution of the fonts in this layout.  Defaults to 96.
            `batch` : `~pyglet.graphics.Batch`
                Optional graphics batch to add the label to.
            `group` : `~pyglet.graphics.Group`
                Optional graphics group to use.

        )widthheight	multilinedpibatchgroupN)superrD   __init___x_y	_anchor_x	_anchor_y_update)r   r   xyrH   rI   anchor_xanchor_yrJ   rK   rL   rM   	__class__s               r   rO   zDocumentLabel.__init__   sp    D 	mT""++H276?035 	, 	O 	O 	O
 !!r   c                     | j         j        S )z3The text of the label.

        :type: str
        r   r   r   s    r   r   zDocumentLabel.text  s     }!!r   c                     || j         _        d S r   r[   r   r   s     r   r   zDocumentLabel.text  s    !r   c                 6    | j                             d          S )zText color.

        Color is a 4-tuple of RGBA components, each in range [0, 255].

        :type: (int, int, int, int)
        colorr   	get_styler\   s    r   r`   zDocumentLabel.color#  s     }&&w///r   c                 p    | j                             dt          | j         j                  d|i           d S )Nr   r`   r   	set_stylelenr   )r   r`   s     r   r`   zDocumentLabel.color-  s?    3t}'9#:#:!(% 0	2 	2 	2 	2 	2r   c                     | j         d         S )a  Blend opacity.

        This property sets the alpha component of the colour of the label's
        vertices.  With the default blend mode, this allows the layout to be
        drawn with fractional opacity, blending with the background.

        An opacity of 255 (the default) has no effect.  An opacity of 128 will
        make the label appear semi-translucent.

        :type: int
           )r`   r\   s    r   opacityzDocumentLabel.opacity2  s     z!}r   c           	          || j         d         k    r;t          t          t          g | j         d d         |R                     | _         d S d S )Nrh   )r`   listmapint)r   alphas     r   ri   zDocumentLabel.opacityA  sP    DJqM!!c#'?BQB'?'?'?@@AADJJJ "!r   c                 6    | j                             d          S )zFont family name.

        The font name, as passed to :py:func:`pyglet.font.load`.  A list of names can
        optionally be given: the first matching font will be used.

        :type: str or list
        	font_namera   r\   s    r   rp   zDocumentLabel.font_nameF  s     }&&{333r   c                 p    | j                             dt          | j         j                  d|i           d S )Nr   rp   rd   )r   rp   s     r   rp   zDocumentLabel.font_nameQ  ?    3t}'9#:#:!,i 8	: 	: 	: 	: 	:r   c                 6    | j                             d          S )z4Font size, in points.

        :type: float
        	font_sizera   r\   s    r   rt   zDocumentLabel.font_sizeV  s     }&&{333r   c                 p    | j                             dt          | j         j                  d|i           d S )Nr   rt   rd   )r   rt   s     r   rt   zDocumentLabel.font_size^  rr   r   c                 6    | j                             d          S )z.Bold font style.

        :type: bool
        boldra   r\   s    r   rw   zDocumentLabel.boldc  s     }&&v...r   c                 p    | j                             dt          | j         j                  d|i           d S )Nr   rw   rd   )r   rw   s     r   rw   zDocumentLabel.boldk  s>    3t}'9#:#:!'	0 	0 	0 	0 	0r   c                 6    | j                             d          S )z0Italic font style.

        :type: bool
        italicra   r\   s    r   rz   zDocumentLabel.italicp  s     }&&x000r   c                 p    | j                             dt          | j         j                  d|i           d S )Nr   rz   rd   )r   rz   s     r   rz   zDocumentLabel.italicx  s?    3t}'9#:#:!)6 2	4 	4 	4 	4 	4r   c                 h    | j                             |dt          | j         j                            S )ap  Get a document style value by name.

        If the document has more than one value of the named style,
        `pyglet.text.document.STYLE_INDETERMINATE` is returned.

        :Parameters:
            `name` : str
                Style name to query.  See documentation for
                `pyglet.text.layout` for known style names.

        :rtype: object
        r   )r   get_style_rangerf   r   )r   names     r   rb   zDocumentLabel.get_style}  s+     },,T1c$-:L6M6MNNNr   c                 p    | j                             dt          | j         j                  ||i           dS )a2  Set a document style value by name over the whole document.

        :Parameters:
            `name` : str
                Name of the style to set.  See documentation for
                `pyglet.text.layout` for known style names.
            `value` : object
                Value of the style.

        r   Nrd   )r   r~   values      r   re   zDocumentLabel.set_style  s5     	3t}'9#:#:T5MJJJJJr   )Nr   r   NNrE   rF   FNNN)r   r	   r
   r   rO   propertyr   setterr`   ri   rp   rt   rw   rz   rb   re   __classcell__rY   s   @r   rD   rD      s         !%.2+5>B+ + + + + +Z " " X" 
[" " [" 0 0 X0 \2 2 \2   X ^B B ^B 4 4 X4 : : : 4 4 X4 : : : / / X/ 
[0 0 [0 1 1 X1 ]4 4 ]4O O OK K K K K K Kr   rD   c                   2     e Zd ZdZ	 	 	 	 	 	 	 d
 fd		Z xZS )LabelzPlain text label.
     NF   r   r   r   r   rE   rF   c                     t          |          }t          t          |                               |||	|
|||||||           | j                            dt          | j        j                  |||||||d           dS )a  Create a plain text label.

        :Parameters:
            `text` : str
                Text to display.
            `font_name` : str or list
                Font family name(s).  If more than one name is given, the
                first matching name is used.
            `font_size` : float
                Font size, in points.
            `bold` : bool/str
                Bold font style.
            `italic` : bool/str
                Italic font style.
            `stretch` : bool/str
                 Stretch font style.
            `color` : (int, int, int, int)
                Font colour, as RGBA components in range [0, 255].
            `x` : int
                X coordinate of the label.
            `y` : int
                Y coordinate of the label.
            `width` : int
                Width of the label in pixels, or None
            `height` : int
                Height of the label in pixels, or None
            `anchor_x` : str
                Anchor point of the X coordinate: one of ``"left"``,
                ``"center"`` or ``"right"``.
            `anchor_y` : str
                Anchor point of the Y coordinate: one of ``"bottom"``,
                ``"baseline"``, ``"center"`` or ``"top"``.
            `align` : str
                Horizontal alignment of text on a line, only applies if
                a width is supplied. One of ``"left"``, ``"center"``
                or ``"right"``.
            `multiline` : bool
                If True, the label will be word-wrapped and accept newline
                characters.  You must also set the width of the label.
            `dpi` : float
                Resolution of the fonts in this layout.  Defaults to 96.
            `batch` : `~pyglet.graphics.Batch`
                Optional graphics batch to add the label to.
            `group` : `~pyglet.graphics.Group`
                Optional graphics group to use.

        r   )rp   rt   rw   rz   stretchr`   alignN)rB   rN   r   rO   r   re   rf   r   )r   r   rp   rt   rw   rz   r   r`   rU   rV   rH   rI   rW   rX   r   rJ   rK   rL   rM   r   rY   s                       r   rO   zLabel.__init__  s    l t$$eT##HaE6$,h$-sE5	B 	B 	B 	3t}'9#:#:""=
 =
 	 	 	 	 	r   )r   NNFFFr   r   r   NNrE   rF   rE   FNNN)r   r	   r
   r   rO   r   r   s   @r   r   r     sh          SX+.2+5>BC C C C C C C C C Cr   r   c                   b     e Zd ZdZ	 	 	 	 d fd	Zed	             Zej        d
             Z xZS )	HTMLLabelz}HTML formatted text label.
    
    A subset of HTML 4.01 is supported.  See `pyglet.text.formats.html` for
    details.
    r   Nr   rE   rF   Fc                     || _         || _        t          ||          }t          t          |                               ||||||||	|
||           dS )at  Create a label with an HTML string.

        :Parameters:
            `text` : str
                HTML formatted text to display.
            `location` : `Location`
                Location object for loading images referred to in the
                document.  By default, the working directory is used.
            `x` : int
                X coordinate of the label.
            `y` : int
                Y coordinate of the label.
            `width` : int
                Width of the label in pixels, or None
            `height` : int
                Height of the label in pixels, or None
            `anchor_x` : str
                Anchor point of the X coordinate: one of ``"left"``,
                ``"center"`` or ``"right"``.
            `anchor_y` : str
                Anchor point of the Y coordinate: one of ``"bottom"``,
                ``"baseline"``, ``"center"`` or ``"top"``.
            `multiline` : bool
                If True, the label will be word-wrapped and render paragraph
                and line breaks.  You must also set the width of the label.
            `dpi` : float
                Resolution of the fonts in this layout.  Defaults to 96.
            `batch` : `~pyglet.graphics.Batch`
                Optional graphics batch to add the label to.
            `group` : `~pyglet.graphics.Group`
                Optional graphics group to use.

        N)_text	_locationr=   rN   r   rO   )r   r   r   rU   rV   rH   rI   rW   rX   rJ   rK   rL   rM   r   rY   s                 r   rO   zHTMLLabel.__init__  sl    J 
!tX..i''!Qv(0((13u	F 	F 	F 	F 	Fr   c                     | j         S )z>HTML formatted text of the label.

        :type: str
        )r   r\   s    r   r   zHTMLLabel.text  s     zr   c                 H    || _         t          || j                  | _        d S r   )r   r=   r   r   r^   s     r   r   zHTMLLabel.text  s     
#D$.99r   )r   Nr   r   NNrE   rF   FNNN)	r   r	   r
   r   rO   r   r   r   r   r   s   @r   r   r     s          *..2+5>B*F *F *F *F *F *FX   X 
[: : [: : : : :r   r   r   )NN)r   os.pathr    r2   pyglet.textr   r   r   	Exceptionr   r   r,   r:   r=   r@   rB   
TextLayoutrD   r   r   r   r   r   <module>r      s  H& &P   / / / / / / / / / /	 	 	 	 	i 	 	 	
. . . . . . . .&+H +H +H +H\3 3 3 3>* * * *      
  
  
 tK tK tK tK tKF% tK tK tKnG G G G GM G G GT>: >: >: >: >: >: >: >: >: >:r   