Commits

Marcus von Appen  committed 0c2f6d6 Merge

Merged in shjohnson_pi/py-sdl2-fixes (pull request #10)

One Font Size Default for FontManager

  • Participants
  • Parent commits 5b66989, a5d4b32

Comments (0)

Files changed (3)

File doc/modules/sdl2ext_font.rst

 
       Checks, whether all characters in the passed *text* can be rendered.
 
-.. class:: FontManager(font_path : str[, alias=None[, color=Color(255, 255, 255)[, bg_color=Color(0, 0, 0)]]])
+.. class:: FontManager(font_path : str[, alias=None[, size=16[, color=Color(255, 255, 255)[, bg_color=Color(0, 0, 0)]]]])
 
-   Manage font and rendering of text.
+   Manage fonts and rendering of text.
 
    One font path must be given to initialize the FontManager.
-   :attr:`default_font` will be set to this font. *color* and *bg_color*
-   will give the FontManager a default color.
+   :attr:`default_font` will be set to this font. *size* is the default
+   font size in pixels. *color* and *bg_color* will give the FontManager
+   a default color.
 
    .. attribute:: bg_color
 
 
    .. attribute:: default_font
 
-      Returns the name and size of the current default font being used
-      by the :class:`FontManager`. On assigning :attr:`default_font`,
-      the value must be a tuple with a font alias and a size: ``(alias,
-      size)``.
+      Returns the name of the current default font being used by the
+      :class:`FontManager`. On assigning :attr:`default_font`,
+      the value must be a loaded font alias.
 
-   .. method:: add(font_path : str[, alias=None[, size=16]]) -> sdl2.sdlttf.TTF_Font
+   .. attribute:: size
+
+      The font size in pixels.
+
+   .. method:: add(font_path : str[, alias=None[, size=None]]) -> sdl2.sdlttf.TTF_Font
 
       Add a font to the :class:`FontManager`. *alias* is by default the
       font name, any other name can be passed, *size* is the font size
-      in pixels. though. Returns the font pointer stored in :attr:`fonts`.
+      in pixels and defaults to :attr:`size`. Returns the font pointer
+      stored in :attr:`fonts`.
 
    .. method:: close()
 
       Closes all fonts used by the :class:`FontManager`.
 
-   .. method:: render(text : str[, alias=None[, size=16[, width=None[, color=None[, bg_color=None[, **kwargs]]]]]]) -> sdl2.SDL_Surface
+   .. method:: render(text : str[, alias=None[, size=None[, width=None[, color=None[, bg_color=None[, **kwargs]]]]]]) -> sdl2.SDL_Surface
 
       Renders text to a surface. This method uses the font designated by
       the passed *alias* or, if *alias* is omitted, by the set

File sdl2/ext/font.py

 
         One font path must be given to initialize the FontManager. The
         default_font will be set to this font. color and bg_color
-        will give the FontManager a default color.
+        will give the FontManager a default color. size is the default
+        font size in pixels.
         """
         if not _HASSDLTTF:
             raise UnsupportedError("FontManager requires sdlttf support")
         self._bgcolor = pixels.SDL_Color(255, 255, 255)
         self.color = color
         self.bg_color = bg_color
-        self._default_font = self.add(font_path, alias, size)
+        self.size = size
+        self._default_font = self.add(font_path, alias)
 
     def __del__(self):
         """Close all opened fonts."""
         self.fonts = {}
         self.aliases = {}
 
-    def add(self, font_path, alias=None, size=16):
+    def add(self, font_path, alias=None, size=None):
         """Add a font to the Font Manager.
 
         alias is by default the font name. But another name can be
         passed. Returns the font pointer stored in self.fonts.
         """
+        size = size or self.size
         if alias is None:
             # If no alias given, take the font name as alias
             basename = os.path.basename(font_path)
         self._bgcolor = pixels.SDL_Color(c.r, c.g, c.b, c.a)
 
     @property
+    def size(self):
+        """The font size in pixels."""
+        return self._size
+
+    @size.setter
+    def size(self, value):
+        """The font size in pixels."""
+        self._size = value
+
+    @property
     def default_font(self):
-        """Returns the name and size of the current default_font."""
+        """Returns the name of the current default_font."""
         for alias in self.fonts:
             for size, font in self.fonts[alias].items():
                 if font == self._default_font:
-                    return alias, size
+                    return alias
 
     @default_font.setter
     def default_font(self, value):
-        """value must be a tuple with a font alias and a size: (alias, size)
+        """value must be a font alias
 
-        Set the default_font to the given font name alias and size,
+        Set the default_font to the given font name alias,
         provided it's loaded in the font manager.
         """
-        alias, size = value
+        alias = value
+        size = self.size
         if alias not in self.fonts:
             raise ValueError("Font %s not loaded in FontManager" % alias)
         # Check if size is already loaded, otherwise do it.
             size = list(self.fonts[alias].keys())[0]
         self._default_font = self.fonts[alias][size]
 
-    def render(self, text, alias=None, size=16, width=None, color=None,
+    def render(self, text, alias=None, size=None, width=None, color=None,
                bg_color=None, **kwargs):
         """Renders text to a surface.
 
         If no bg_color or color are given, it will default to the
         FontManager's bg_color and color.
         """
+        alias = alias or self.default_font
+        size = size or self.size
         if bg_color is None:
             bg_color = self._bgcolor
         elif not isinstance(bg_color, pixels.SDL_Color):
             c = convert_to_color(color)
             bg_color = pixels.SDL_Color(c.r, c.g, c.b, c.a)
         if len(self.fonts) == 0:
-            raise TypeError("There are no font selected.")
+            raise TypeError("There are no fonts selected.")
         font = self._default_font
-        if alias is not None:
-            if alias not in self.aliases:
-                raise KeyError("Font %s not loaded" % font)
-            elif size not in self.fonts[alias]:
-                self._change_font_size(alias, size)
-            font = self.fonts[alias][size]
+        if alias not in self.aliases:
+            raise KeyError("Font %s not loaded" % font)
+        elif size not in self.fonts[alias]:
+            self._change_font_size(alias, size)
+        font = self.fonts[alias][size]
         text = byteify(text, "utf-8")
         if width:
             surface = sdlttf.TTF_RenderUTF8_Blended_Wrapped(font, text,

File sdl2/test/sdl2ext_font_test.py

         fm = sdl2ext.FontManager(RESOURCES.get_path("tuffy.ttf"),
                                  bg_color=(100, 0, 0))
         self.assertIsInstance(fm, sdl2ext.FontManager)
-        self.assertEqual(fm.default_font, ("tuffy", 16))
+        self.assertEqual(fm.default_font, "tuffy")
+        self.assertEqual(fm.size, 16)
         self.assertEqual(fm.bg_color, sdl2ext.Color(100, 0, 0, 0))
 
     def test_FontManager_default_font(self):
         fm = sdl2ext.FontManager(RESOURCES.get_path("tuffy.ttf"))
-        self.assertEqual(fm.default_font, ("tuffy", 16))
+        self.assertEqual(fm.default_font, "tuffy")
+        self.assertEqual(fm.size, 16)
         with self.assertRaises(ValueError):
-            fm.default_font = "Inexistent Alias", 16
+            fm.default_font = "Inexistent Alias"
         fm.add(RESOURCES.get_path("tuffy.copy.ttf"), size = 10)
-        fm.default_font = ("tuffy.copy", 10)
-        self.assertEqual(fm.default_font, ("tuffy.copy", 10))
-        fm.default_font = ("tuffy.copy", 16)
-        self.assertEqual(fm.default_font, ("tuffy.copy", 16))
+        fm.default_font = "tuffy.copy"
+        fm.size = 10
+        self.assertEqual(fm.default_font, "tuffy.copy")
+        self.assertEqual(fm.size, 10)
+        fm.default_font = "tuffy.copy"
+        fm.size = 16
+        self.assertEqual(fm.default_font, "tuffy.copy")
+        self.assertEqual(fm.size, 16)
 
     def test_FontManager_add(self):
         fm = sdl2ext.FontManager(RESOURCES.get_path("tuffy.ttf"))