<commit_before>import entity, util
from gameloop import Game, GameLoop

__doc__ = """A text object

L{text} is a module contaning a single class, L{Text}, that can
be used to create text objects. It supports varying fonts, font sizes, colors,
antialiasing, and word wrapping. The L{Text} class inherits from L{Image}, so
it is completely compatible with the rest of Pyrge, but it is also almost totally
self-contained, and can be used in other pygame-based applications with little
effort."""

__all__ = ['Text']

class Text(entity.Image):
    """A text display class.

       L{Text} can be used to create text display objects that have all the
       properties of a Pyrge L{Image}. Objects of this class have a number of
       different attributes that can be manipulated to produce bold text, italics,
       different fonts and font sizes, different colors (for both text and
       background), antialiasing, word-wrapping, and more.

       The constructor for L{Text} takes all of the usual L{Image} arguments,
       plus a number of specialized keyword arguments detailed below.

       @note: A Text object uses colorkey transparency, unless the text color
           is black (C{Color(0,0,0)}), in which case it will use per-pixel
           alpha values.

       @param text: The text that this object will render.

       @keyword font: A pygame Font object that contains all of the rendering
           information, such as font face, size, and bold/italics. If this is
           None, then Pyrge's default font will be used.
       @keyword antialias: Whether to render smooth text by using antialiasing.
       @keyword color: The color to use to render text, either as a pygame
           C{Color} object or as a tuple of RGB or RGBA.
       @keyword background: The color of the background, in the same format as
           the C{color} argument. If this is None, then a transparent background
           will be used.
       @keyword autowidth: If this argument is True (default is False), then
           the Text object will be resized so that the text will fit on a
           single line.
    """
    def __init__(self, text='', *args, **kwargs):
        # do these first, so we can use them for positioning

        # the actual text to render
        self._text = self._replace_escaped(kwargs.get('text', text))

        # the font to use for rendering
        self._font = kwargs.get('font', Game.defaultFont)

        # whether to use antialiasing
        self._aa = kwargs.get('antialias', False)

        # the text and background colors (respectively)
        self._color = kwargs.get('color', (255,255,255))
        self._bgcolor = kwargs.get('background', None)

        # whether the text should all be on one line, and the sprite resized
        # TODO: I don't really like the name "autowidth". What else can we call it?
        self._autowidth = kwargs.get('autowidth', False)

        # position and size
##        x = kwargs.get('x', 0.0)
##        y = kwargs.get('y', 0.0)
        kwargs['w'] = kwargs.get('w', kwargs.get('width', self._font.size(self._text)[0]))
        kwargs['h'] = kwargs.get('h', kwargs.get('height', self._font.get_linesize()))

##        super(Text, self).__init__(x,y,w,h)
        super(Text, self).__init__(*args, **kwargs)

        # change position semantics from center to top-left
        self.x += self.width/2
        self.y += self.height/2

        if len(self._text):
            self._render_text()

    def _render_text(self):
        """This is a helper method to render text to a surface with wrapping."""
        if self._autowidth:
            # "autowidth" means one line, no wrapping, and the left edge
            # stays in the same place
            textlines = [self.text]
            textheight = self.font.get_linesize()
            oldleft = self.left
            self.width = self.font.size(self.text)[0]<commit_msg>Added static kwarg to Text<commit_after>import entity, util
from gameloop import Game, GameLoop

__doc__ = """A text object

L{text} is a module contaning a single class, L{Text}, that can
be used to create text objects. It supports varying fonts, font sizes, colors,
antialiasing, and word wrapping. The L{Text} class inherits from L{Image}, so
it is completely compatible with the rest of Pyrge, but it is also almost totally
self-contained, and can be used in other pygame-based applications with little
effort."""

__all__ = ['Text']

class Text(entity.Image):
    """A text display class.

       L{Text} can be used to create text display objects that have all the
       properties of a Pyrge L{Image}. Objects of this class have a number of
       different attributes that can be manipulated to produce bold text, italics,
       different fonts and font sizes, different colors (for both text and
       background), antialiasing, word-wrapping, and more.

       The constructor for L{Text} takes all of the usual L{Image} arguments,
       plus a number of specialized keyword arguments detailed below.

       @note: A Text object uses colorkey transparency, unless the text color
           is black (C{Color(0,0,0)}), in which case it will use per-pixel
           alpha values.

       @param text: The text that this object will render.

       @keyword font: A pygame Font object that contains all of the rendering
           information, such as font face, size, and bold/italics. If this is
           None, then Pyrge's default font will be used.
       @keyword antialias: Whether to render smooth text by using antialiasing.
       @keyword color: The color to use to render text, either as a pygame
           C{Color} object or as a tuple of RGB or RGBA.
       @keyword background: The color of the background, in the same format as
           the C{color} argument. If this is None, then a transparent background
           will be used.
       @keyword autowidth: If this argument is True (default is False), then
           the Text object will be resized so that the text will fit on a
           single line.
       @keyword static: If this argument is True (the default), the Text's
           position value is treated as its upper-left corner. Otherwise, the
           position is the center of the Text.
    """
    def __init__(self, text='', *args, **kwargs):
        # do these first, so we can use them for positioning

        # the actual text to render
        self._text = self._replace_escaped(kwargs.get('text', text))

        # the font to use for rendering
        self._font = kwargs.get('font', Game.defaultFont)

        # whether to use antialiasing
        self._aa = kwargs.get('antialias', False)

        # the text and background colors (respectively)
        self._color = kwargs.get('color', (255,255,255))
        self._bgcolor = kwargs.get('background', None)

        # whether the text should all be on one line, and the sprite resized
        # TODO: I don't really like the name "autowidth". What else can we call it?
        self._autowidth = kwargs.get('autowidth', False)

        # position and size
##        x = kwargs.get('x', 0.0)
##        y = kwargs.get('y', 0.0)
        kwargs['w'] = kwargs.get('w', kwargs.get('width', self._font.size(self._text)[0]))
        kwargs['h'] = kwargs.get('h', kwargs.get('height', self._font.get_linesize()))

##        super(Text, self).__init__(x,y,w,h)
        super(Text, self).__init__(*args, **kwargs)

        # change position semantics from center to top-left
        if kwargs.get('static', True):
            self.x += self.width/2
            self.y += self.height/2

        if len(self._text):
            self._render_text()

    def _render_text(self):
        """This is a helper method to render text to a surface with wrapping."""
        if self._autowidth:
            # "autowidth" means one line, no wrapping, and the left edge
            # stays in the same place
            textlines = [self.text]
            textheight = self.font.get_linesize()
            oldleft = self.left
            self.width = self.font.size(self.text)[0]
{'fraction_non_alphanumeric': 0.08875959395890072, 'fraction_numerical': 0.0049517207229512255, 'mean_word_length': 2.5827050997782703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5570320', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2180, 'n_words': 956}