Anonymous avatar Anonymous committed b6e7d84

Update FontManager to use a size attribute instead of default_font tuple of (alias, size). Also update related tests.

Comments (0)

Files changed (3)

doc/modules/sdl2ext_font.rst

    Manage font 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.
+
+   .. 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 and defaults to the :attr:`default_font` size. 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()
 
 
         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."""
         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.default_font[1]
+        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.
         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[0]
-        size = size or self.default_font[1]
+        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):

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"))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.