Example #1
0
    def set_font(self, font, font_size, color, selected_color, antialias=True):
        """
        Set the text font.

        :param font: Name or list of names for font (see pygame.font.match_font for precise format)
        :type font: str, list
        :param font_size: Size of font in pixels
        :type font_size: int
        :param color: Text color
        :type color: tuple
        :param selected_color: Text color when widget is selected
        :type selected_color: tuple
        :param antialias: Determines if antialias is applied to font (uses more processing power)
        :type antialias: bool
        :return: None
        """
        assert isinstance(font, str)
        assert isinstance(font_size, int)
        assert isinstance(color, tuple)
        assert isinstance(selected_color, tuple)
        assert isinstance(antialias, bool)
        self._font_name = font
        self._font = _fonts.get_font(font, font_size)
        self._font_size = font_size
        self._font_color = color
        self._font_selected_color = selected_color
        self._font_antialias = antialias
        self._apply_font()
Example #2
0
    def add_text(self,
                 x: NumberType,
                 y: NumberType,
                 text: str,
                 font: Union[str, 'Font', 'Path'],
                 size: int,
                 color: ColorType,
                 prev: bool = True,
                 antialias=True,
                 centered=False) -> str:
        """
        Adds a text.

        :param x: X position (px), being ``0`` the center of the object
        :param y: Y position (px), being ``0`` the center of the object
        :param text: Text to draw
        :param font: Font path or pygame object
        :param size: Size of the font to render
        :param color: Font color
        :param prev: If ``True`` draw previous the object, else draws post
        :param antialias: Font antialias enabled
        :param centered: If ``True`` the text is centered into the position
        :return: ID of the decoration
        """
        coords = [(x, y)]
        assert_list_vector(coords, 2)
        text = str(text)
        font_obj = _fonts.get_font(font, size)
        surface_font = font_obj.render(text, antialias, color)
        surface = make_surface(width=surface_font.get_width(),
                               height=surface_font.get_height(),
                               alpha=True)
        surface.blit(surface_font, (0, 0))
        return self._add_decor(DECORATION_TEXT, prev,
                               (tuple(coords), surface, centered))
Example #3
0
    def set_font(self, font, font_size, color, selected_color, background_color, antialias=True):
        """
        Set the text font.

        :param font: Font name (see :py:class:`pygame.font.match_font` for precise format)
        :type font: str
        :param font_size: Size of font in pixels
        :type font_size: int
        :param color: Text color
        :type color: tuple
        :param selected_color: Text color when widget is selected
        :type selected_color: tuple
        :param background_color: Font background color
        :type background_color: tuple
        :param antialias: Determines if antialias is applied to font (uses more processing power)
        :type antialias: bool
        :return: None
        """
        assert isinstance(font, str)
        assert isinstance(font_size, int)
        assert isinstance(color, tuple)
        assert isinstance(selected_color, tuple)
        assert isinstance(background_color, (tuple, type(None)))
        assert isinstance(antialias, bool)

        self._font = _fonts.get_font(font, font_size)
        self._font_antialias = antialias
        self._font_background_color = background_color
        self._font_color = color
        self._font_name = font
        self._font_selected_color = selected_color
        self._font_size = font_size

        self._apply_font()
Example #4
0
    def set_font(self,
                 font,
                 font_size,
                 color,
                 selected_color,
                 background_color,
                 antialias=True):
        """
        Set the text font.

        :param font: Font name (see :py:class:`pygame.font.match_font` for precise format)
        :type font: str
        :param font_size: Size of font in pixels
        :type font_size: int
        :param color: Text color
        :type color: tuple
        :param selected_color: Text color when widget is selected
        :type selected_color: tuple
        :param background_color: Font background color
        :type background_color: tuple, None
        :param antialias: Determines if antialias is applied to font (uses more processing power)
        :type antialias: bool
        :return: None
        """
        assert isinstance(font, str)
        assert isinstance(font_size, (int, float))
        assert isinstance(antialias, bool)
        assert_color(color)
        assert_color(selected_color)

        if background_color is not None:
            assert_color(background_color)

            # If background is a color and it's transparent raise a warning
            # Font background color must be opaque, otherwise the results are quite bad
            if len(background_color) == 4 and background_color[3] != 255:
                background_color = None
                msg = 'font background color must be opaque, alpha channel must be 255'
                warnings.warn(msg)

        font_size = int(font_size)

        self._font = _fonts.get_font(font, font_size)
        self._font_antialias = antialias
        self._font_background_color = background_color
        self._font_color = color
        self._font_name = font
        self._font_selected_color = selected_color
        self._font_size = font_size

        self._last_render_hash = 0  # Force widget render
        self._apply_font()
Example #5
0
    def _apply_font(self) -> None:
        if self._state_text_font is None:
            self._state_text_font = self._font_name
        if self._state_text_font_size is None:
            self._state_text_font_size = self._font_size
        self._state_font = _fonts.get_font(self._state_text_font,
                                           self._state_text_font_size)

        # Compute the height
        height = self._font_render_string('TEST').get_height()
        self._switch_height = int(height * self._switch_height_factor)
        self._slider_height = int(
            self._switch_height *
            self._slider_height_factor) - 2 * self._switch_border_width

        # Render the state texts
        for t in range(self._total_states):
            f_render = self._state_font.render(self._state_text[t], True,
                                               self._state_text_font_color[t])
            self._switch_font_rendered.append(f_render)