Commits

Lenard Lindstrom committed 054dbe9

handle size errors consistently for freetype.Font sized methods (toward Issue #75)

Have Font.get_metrics() raise an exception for an unavailable bitmap size. Make
the pygame.error the exception raised by all sized metric methods for a size
error.

Comments (0)

Files changed (5)

docs/reST/ref/freetype.rst

       Read only. Gets the height of the font. This is the average value of all
       glyphs in the font.
 
-   .. method:: ascender
+   .. attribute:: ascender
 
       | :sl:`The unscaled ascent of the font in font units`
       | :sg:`ascender -> int`
       The descent is the number of units from the font's baseline to the
       bottom of the bounding box.
 
-   .. attribute:: get_sized_ascender
+   .. method:: get_sized_ascender
 
       | :sl:`The scaled ascent of the font in pixels`
-      | :sg:`get_sized_ascender() -> int`
+      | :sg:`get_sized_ascender(<size>=0) -> int`
 
       Return the number of units from the font's baseline to the top of the
       bounding box. It is not adjusted for strong or rotation.
    .. method:: get_sized_descender
 
       | :sl:`The scaled descent of the font in pixels`
-      | :sg:`get_sized_descender() -> int`
+      | :sg:`get_sized_descender(<size>=0) -> int`
 
       Return the number of pixels from the font's baseline to the top of the
       bounding box. It is not adjusted for strong or rotation.
 
-   .. attribute:: get_sized_height
+   .. method:: get_sized_height
 
       | :sl:`The scaled height of the font in pixels`
-      | :sg:`get_sized_height() -> int`
+      | :sg:`get_sized_height(<size>=0) -> int`
 
       Read only. Gets the height of the font. This is the average value of all
       glyphs in the font. It is not adjusted for strong or rotation.
    .. method:: get_sized_glyph_height
 
       | :sl:`The scaled bounding box height of the font in pixels`
-      | :sg:`get_sized_glyph_height() -> int`
+      | :sg:`get_sized_glyph_height(<size>=0) -> int`
 
       Return the glyph bounding box height of the font in pixels.
       This is the average value of all glyphs in the font.
 
 static PyObject *
 get_metrics(FreeTypeInstance *ft, FontRenderMode *render,
-          PgFontObject *font, PGFT_String *text)
+            PgFontObject *font, PGFT_String *text)
 {
     Py_ssize_t length = PGFT_String_GET_LENGTH(text);
     PGFT_char *data = PGFT_String_GET_DATA(text);
     double advance_y;
     Py_ssize_t i;
 
+    if (!_PGFT_GetFontSized(ft, font, render->face_size)) {
+        PyErr_SetString(PyExc_SDLError, _PGFT_GetError(ft));
+        return 0;
+    }
     list = PyList_New(length);
     if (!list) {
         return 0;

src/doc/freetype_doc.h

 
 #define DOC_FONTDESCENDER "descender -> int\nThe unscaled descent of the font in font units"
 
-#define DOC_FONTGETSIZEDASCENDER "get_sized_ascender() -> int\nThe scaled ascent of the font in pixels"
+#define DOC_FONTGETSIZEDASCENDER "get_sized_ascender(<size>=0) -> int\nThe scaled ascent of the font in pixels"
 
-#define DOC_FONTGETSIZEDDESCENDER "get_sized_descender() -> int\nThe scaled descent of the font in pixels"
+#define DOC_FONTGETSIZEDDESCENDER "get_sized_descender(<size>=0) -> int\nThe scaled descent of the font in pixels"
 
-#define DOC_FONTGETSIZEDHEIGHT "get_sized_height() -> int\nThe scaled height of the font in pixels"
+#define DOC_FONTGETSIZEDHEIGHT "get_sized_height(<size>=0) -> int\nThe scaled height of the font in pixels"
 
-#define DOC_FONTGETSIZEDGLYPHHEIGHT "get_sized_glyph_height() -> int\nThe scaled bounding box height of the font in pixels"
+#define DOC_FONTGETSIZEDGLYPHHEIGHT "get_sized_glyph_height(<size>=0) -> int\nThe scaled bounding box height of the font in pixels"
 
 #define DOC_FONTGETSIZES "get_sizes() -> [(int, int, int, float, float), ...]\nget_sizes() -> []\nreturn the available sizes of embedded bitmaps"
 
 The unscaled descent of the font in font units
 
 pygame.freetype.Font.get_sized_ascender
- get_sized_ascender() -> int
+ get_sized_ascender(<size>=0) -> int
 The scaled ascent of the font in pixels
 
 pygame.freetype.Font.get_sized_descender
- get_sized_descender() -> int
+ get_sized_descender(<size>=0) -> int
 The scaled descent of the font in pixels
 
 pygame.freetype.Font.get_sized_height
- get_sized_height() -> int
+ get_sized_height(<size>=0) -> int
 The scaled height of the font in pixels
 
 pygame.freetype.Font.get_sized_glyph_height
- get_sized_glyph_height() -> int
+ get_sized_glyph_height(<size>=0) -> int
 The scaled bounding box height of the font in pixels
 
 pygame.freetype.Font.get_sizes

src/freetype/ft_wrap.c

     FT_Face font = _PGFT_GetFont(ft, fontobj);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return -1;
     }
     return FT_IS_FIXED_WIDTH(font) ? 1 : 0;
     FT_Face font = _PGFT_GetFont(ft, fontobj);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return -1;
     }
     return FT_HAS_FIXED_SIZES(font) ? font->num_fixed_sizes : 0;
     FT_Bitmap_Size *bitmap_size_p;
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return -1;
     }
     if (!FT_HAS_FIXED_SIZES(font) || n > font->num_fixed_sizes) /* cond. or */ {
     font = _PGFT_GetFont(ft, fontobj);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     return font->family_name ? font->family_name : "";
     FT_Face font = _PGFT_GetFont(ft, fontobj);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     return (long)font->height;
     FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     return (long)FX6_TRUNC(FX6_CEIL(font->size->metrics.height));
     FT_Face font = _PGFT_GetFont(ft, fontobj);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     return (long)font->ascender;
     FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     return (long)FX6_TRUNC(FX6_CEIL(font->size->metrics.ascender));
     FT_Face font = _PGFT_GetFont(ft, fontobj);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     return (long)font->descender;
     FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size);
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     return (long)FX6_TRUNC(FX6_FLOOR(font->size->metrics.descender));
     FT_Size_Metrics *metrics;
 
     if (!font) {
-        RAISE(PyExc_RuntimeError, _PGFT_GetError(ft));
+        RAISE(PyExc_SDLError, _PGFT_GetError(ft));
         return 0;
     }
     metrics = &font->size->metrics;

test/freetype_test.py

         self.assertRaises(exception, f.get_rect,
                           'A', size=8, style=ft.STYLE_STRONG)
 
+        # Unsupported point size
+        exception = pygame.error
+        self.assertRaises(exception, f.get_rect, 'A', size=42)
+        self.assertRaises(exception, f.get_metrics, 'A', size=42)
+        self.assertRaises(exception, f.get_sized_ascender, 42)
+        self.assertRaises(exception, f.get_sized_descender, 42)
+        self.assertRaises(exception, f.get_sized_height, 42)
+        self.assertRaises(exception, f.get_sized_glyph_height, 42)
+
     def test_freetype_Font_get_metrics(self):
 
         font = self._TEST_FONTS['sans']