Anonymous avatar Anonymous committed 3e312e8

Changed argument order for freetype.Font.render() and its destination surface tuple.

Comments (0)

Files changed (4)

doc/src/freetypebase.xml

       <desc>Gets or sets the default size of the Font in points.</desc>
     </attr>
     <method name="render">
-      <call>render(dest, text, fgcolor [, bgcolor, style, rotation, ptsize]) -> int, int[, :class:`pygame2.sdl.video.Surface`]</call>
+      <call>render(text, fgcolor [, bgcolor, style, rotation, ptsize, dest]) -> :class:`pygame2.sdl.video.Surface`, int, int</call>
       <desc>
         Renders a text on a SDL surface.
 
         *ptsize* argument, a text rotation via the *rotation* argument,
         or a new text style via the *style* argument.
 
-        This function is only available when PyGame2 has been compiled
+        This function is only available when Pygame2 has been compiled
         with SDL support.
       </desc>
     </method>

examples/freetype/sdlfont.py

     screen = video.set_mode (800, 600)
     screen.fill (colors["grey_light"])
 
-    w, h, sf = font.render(None, "Hello World", colors["red"],
-                colors['grey_dark'], ptsize=64,
-                style=ftconstants.STYLE_UNDERLINE|ftconstants.STYLE_ITALIC)
+    sf, w, h = font.render("Hello World", colors["red"], colors['grey_dark'],
+        ptsize=64, style=ftconstants.STYLE_UNDERLINE|ftconstants.STYLE_ITALIC)
     screen.blit (sf, (32, 32))
 
-    font.render((screen, 32, 128), "abcdefghijklm", colors["grey_dark"],
-                colors["green"], ptsize=64)
+    font.render("abcdefghijklm", colors["grey_dark"], colors["green"],
+                ptsize=64, dest=(screen, 32, 128))
 
     font.vertical = True
-    font.render((screen, 32, 190), "Vertical?", colors["blue"], None, ptsize=32)
+    font.render("Vertical?", colors["blue"], ptsize=32, dest=(screen, 32, 190))
     font.vertical = False
 
-    font.render((screen, 64, 190), "Let's spin!", colors["red"], None,
-                ptsize=48, rotation=55)
+    font.render("Let's spin!", colors["red"], ptsize=48, rotation=55,
+                dest=(screen, 64, 190))
 
-    font.render((screen, 150, 270), "All around!", colors["green"], None,
-                ptsize=48, rotation=-55)
+    font.render("All around!", colors["green"], ptsize=48, rotation=-55,
+                dest=(screen, 150, 270))
 
-    font.render((screen, 250, 220), "and BLEND", pygame2.Color(255, 0, 0, 128),
-                None, ptsize=64)
+    font.render("and BLEND", pygame2.Color(255, 0, 0, 128), ptsize=64,
+                dest=(screen, 250, 220))
 
-    font.render((screen, 258, 237), "or BLAND!",
-                pygame2.Color(0, 0xCC, 28, 128), None, ptsize=64)
+    font.render("or BLAND!", pygame2.Color(0, 0xCC, 28, 128), ptsize=64,
+                dest=(screen, 258, 237))
 
-    font.render((screen, 298, 320), "I \u2665 Unicode",
-                pygame2.Color(0, 0xCC, 0xDD), None, ptsize=64)
+    font.render("I \u2665 Unicode", pygame2.Color(0, 0xCC, 0xDD), ptsize=64,
+                dest=(screen, 298, 320))
 
-    font.render((screen, 480, 32), "\u2665", colors["grey_light"],
-                colors["red"], ptsize=148)
+    font.render("\u2665", colors["grey_light"], colors["red"], ptsize=148,
+                dest=(screen, 480, 32))
 
-    font.render((screen, 380, 380), "...yes, this is a SDL surface",
-                pygame2.Color(0, 0, 0), None,ptsize=24,
-                style=ftconstants.STYLE_BOLD)
+    font.render("...yes, this is a SDL surface", pygame2.Color(0, 0, 0),
+                ptsize=24, style=ftconstants.STYLE_BOLD,
+                dest=(screen, 380, 380))
 
     screen.flip ()
 

src/freetype/ft_font.c

     /* keyword list */
     static char *kwlist[] = 
     { 
-        "dest", "text", "fgcolor", "bgcolor", 
-        "style", "rotation", "ptsize", NULL
+        "text", "fgcolor", "bgcolor", "style", "rotation", "ptsize", "dest",
+        NULL
     };
 
     PyFreeTypeFont *font = (PyFreeTypeFont *)self;
     FreeTypeInstance *ft;
     ASSERT_GRAB_FREETYPE(ft, NULL);
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO|Oiii", kwlist,
-                &target_tuple, &text, &fg_color_obj, /* required */
-                &bg_color_obj, &style, &rotation, &ptsize)) /* optional */
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OiiiO", kwlist, &text,
+            &fg_color_obj, &bg_color_obj, &style, &rotation, &ptsize,
+            &target_tuple))
         return NULL;
 
-
-    if (PyColor_Check(fg_color_obj))
+    if (fg_color_obj && PyColor_Check(fg_color_obj))
     {
         PyColor *c = (PyColor *)fg_color_obj;
         fg_color.r = c->r;
         return NULL;
     }
 
-    if (target_tuple == Py_None)
+    if (!target_tuple || target_tuple == Py_None)
     {
         SDL_Surface *r_surface = NULL;
+        PyObject *surface_obj = NULL;
 
         r_surface = PGFT_Render_NewSurface(ft, font, &render, text,
                 &fg_color, bg_color_obj ? &bg_color : NULL, &width, &height);
             return NULL;
         }
 
-        rtuple = Py_BuildValue("(iiO)", width, height,
-                PySDLSurface_NewFromSDLSurface(r_surface));
+        surface_obj = PySDLSurface_NewFromSDLSurface(r_surface);
+        if (!surface_obj)
+        {
+            SDL_FreeSurface (r_surface);
+            return NULL;
+        }
+        rtuple = Py_BuildValue("(Oii)", surface_obj, width, height);
+        if (!rtuple)
+        {
+            Py_DECREF (surface_obj);
+            return NULL;
+        }
     }
     else
     {
             return NULL;
         }
 
-        rtuple = Py_BuildValue("(ii)", width, height);
+        rtuple = Py_BuildValue("(Oii)", surface_obj, width, height);
     }
 
     return rtuple;
     if (PGFT_TryLoadFont_Filename(ft, font, filename, face_index) != 0)
     {
         PyErr_SetString(PyExc_PyGameError, PGFT_GetError(ft));
-        Py_XDECREF (font);
+        Py_XDECREF ((PyObject*)font);
         return NULL;
     }
 

test/freetype_font_test.py

         color = pygame2.base.Color(0, 0, 0)
 
         # make sure we always have a valid fg color
-        self.assertRaises(TypeError, font.render, None, 'FoobarBaz')
-        self.assertRaises(TypeError, font.render, None, 'FoobarBaz', None)
+        self.assertRaises(TypeError, font.render, 'FoobarBaz')
+        self.assertRaises(TypeError, font.render, 'FoobarBaz', None)
 
         # render to new surface
-        rend = font.render(None, 'FoobarBaz', pygame2.base.Color(0, 0, 0), None, ptsize=24)
+        rend = font.render('FoobarBaz', pygame2.base.Color(0, 0, 0), ptsize=24)
         self.assertTrue(isinstance(rend, tuple))
-        self.assertTrue(isinstance(rend[0], int))
+        self.assertTrue(isinstance(rend[0], pygame2.base.Surface))
         self.assertTrue(isinstance(rend[1], int))
-        self.assertTrue(isinstance(rend[2], pygame2.base.Surface))
+        self.assertTrue(isinstance(rend[2], int))
 
         # render to existing surface
-        rend = font.render((surf, 32, 32), 'FoobarBaz', color, None, ptsize=24)
+        rend = font.render('FoobarBaz', color, ptsize=24, dest=(surf, 32, 32))
         self.assertTrue(isinstance(rend, tuple))
-        self.assertTrue(isinstance(rend[0], int))
+        self.assertTrue(rend[0] == surf)
         self.assertTrue(isinstance(rend[1], int))
+        self.assertTrue(isinstance(rend[2], int))
 
         # render empty to new surface
-        rend = font.render(None, '', pygame2.base.Color(0, 0, 0), None, ptsize=24)
+        rend = font.render('', pygame2.base.Color(0, 0, 0), ptsize=24)
         self.assertTrue(isinstance(rend, tuple))
-        self.assertTrue(isinstance(rend[0], int) and rend[0] == 0)
+        self.assertTrue(isinstance(rend[0], pygame2.base.Surface))
         self.assertTrue(isinstance(rend[1], int) and rend[1] == 0)
-        self.assertTrue(isinstance(rend[2], pygame2.base.Surface))
-        self.assertTrue(rend[2].size == (0, 0))
+        self.assertTrue(isinstance(rend[2], int) and rend[2] == 0)
+        self.assertTrue(rend[0].size == (0, 0))
         
         # render empty to existing surface
-        rend = font.render((surf, 32, 32), '', color, None, ptsize=24)
+        rend = font.render('', color, ptsize=24, dest=(surf, 32, 32))
         self.assertTrue(isinstance(rend, tuple))
-        self.assertTrue(isinstance(rend[0], int) and rend[0] == 0)
+        self.assertTrue(rend[0] == surf)
         self.assertTrue(isinstance(rend[1], int) and rend[1] == 0)
+        self.assertTrue(isinstance(rend[2], int) and rend[2] == 0)
         
         # misc parameter test
-        self.assertRaises(ValueError, font.render, None, 'foobar', color)
-        self.assertRaises(TypeError, font.render, None, 'foobar', color, "",
-                ptsize=24)
-        self.assertRaises(ValueError, font.render, None, 'foobar', color, None,
-                style=42, ptsize=24)
-        self.assertRaises(TypeError, font.render, None, 'foobar', color, None,
-                style=None, ptsize=24)
-        self.assertRaises(ValueError, font.render, None, 'foobar', color, None,
-                style=97, ptsize=24)
+        self.assertRaises(ValueError, font.render, 'foobar', color)
+        self.assertRaises(TypeError, font.render, 'foobar', color, "",ptsize=24)
+        self.assertRaises(ValueError, font.render, 'foobar', color, None,
+                          style=42, ptsize=24)
+        self.assertRaises(TypeError, font.render, 'foobar', color, None,
+                          style=None, ptsize=24)
+        self.assertRaises(ValueError, font.render,'foobar', color, None,
+                          style=97, ptsize=24)
 
         pygame2.sdl.video.quit()
         ft.quit ()
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.