Ejemplo n.º 1
0
    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]
            self.x = oldleft + self.width / 2.
        else:
            textlines = self._wrap(self.text)
            textheight = len(textlines) * self.font.get_linesize()

        textwidth = self.width
        self.height = textheight

        # fix in case we're drawing black text
        if self.background is None and self.color == Game.rgb(0, 0, 0):
            ALPHA = Game.Constants.SRCALPHA
        else:
            ALPHA = 0

        surf = Game.Surface((textwidth, textheight), ALPHA)

        for lineno, line in enumerate(textlines):
            if self.background is not None:
                r = self.font.render(line, self.antialias, self.color,
                                     self.background)
            else:
                r = self.font.render(line, self.antialias, self.color)
            surf.blit(r, (0, lineno * self.font.get_linesize()))

        self.pixels = surf

        if self.background is None:
            # per-pixel alphas override colorkeys, so this line won't do anything
            # if the text is black
            self.pixels.set_colorkey((0, 0, 0), Game.Constants.RLEACCEL)

        self.redraw()