Commits

tanoku  committed 1b15594

Dropping RWops support from the FreeType module (WIP)

  • Participants
  • Parent commits 127e1e2
  • Branches pgreloaded

Comments (0)

Files changed (5)

File src/freetype/ft_font.c

 #define PYGAME_FREETYPE_FONT_INTERNAL
 
 #include "ft_mod.h"
+#include "ft_wrap.h"
 #include "pgfreetype.h"
 #include "freetypebase_doc.h"
 
+/* Externs */
+extern int PyFile_Check(PyObject *);
+extern PyObject* PyFile_FromString(char *, char *);
+
 /*
  * Constructor/init/destructor
  */
     if ((ft = _get_freetype()) != NULL)
         PGFT_UnloadFont(ft, self);
 
+    Py_XDECREF(self->id.file_ptr);
+
     ((PyObject*)self)->ob_type->tp_free((PyObject *)self);
 }
 
     if (!font)
         return NULL;
 
-    font->rwops = NULL;
+    memset(&font->id, 0, sizeof(FontId));
 
     font->pyfont.get_height = _ftfont_getheight;
     font->pyfont.get_name = _ftfont_getname;
         return -1;
     }
 
-    font->face_index = face_index;
-    font->rwops = PyRWops_NewRO_Threaded(file, &font->autoclose);
-    if (!font->rwops)
+    font->id.face_index = face_index;
+
+    if (PyFile_Check(file))
+    {
+        Py_INCREF(file);
+        font->id.file_ptr = file;
+    }
+    else if (IsTextObj(file))
+    {
+        PyObject *tmp;
+        char *filename;
+
+        if (!UTF8FromObject(file, &filename, &tmp))
+        {
+            PyErr_SetString(PyExc_ValueError, "Failed to decode file name");
+            return -1;
+        }
+        
+        font->id.file_ptr = PyFile_FromString(filename, "rb");
+        Py_XDECREF(tmp);
+    }
+    else
+    {
+        PyErr_SetString(PyExc_ValueError, 
+                "Invalid 'file' parameter (must be a File object or a file name");
         return -1;
+    }
 
-    /*
-     * TODO: Do we have to close it later if autoclose
-     * it 1 or will SLD_RWops handle it?
-     */
-    if (!font->autoclose)
-        PyRWops_Close(font->rwops, font->autoclose);
 
-    if (!font)
+    if (PGFT_TryLoadFont(ft, font) != 0)
     {
-        PyErr_SetString(PyExc_PyGameError, PGFT_GetError());
+        /* TODO: Get a proper error string */
+        PyErr_SetString(PyExc_ValueError, "Failed to load font");
         return -1;
     }
 
 PyFreeTypeFont_New(char *file, int ptsize)
 {
     /* TODO */
+    return NULL;
 }
 
 void
 ftfont_export_capi(void **capi)
 {
-    /* TODO */
+    capi[PYGAME_FREETYPE_FONT_FIRSTSLOT + 0] = &PyFreeTypeFont_Type;
+    capi[PYGAME_FREETYPE_FONT_FIRSTSLOT + 1] = &PyFreeTypeFont_New;
 }

File src/freetype/ft_mod.c

 #define PYGAME_FREETYPE_INTERNAL
 
 #include "ft_mod.h"
+#include "ft_wrap.h"
 #include "pgfreetype.h"
 #include "pgsdl.h"
 #include "freetypebase_doc.h"

File src/freetype/ft_wrap.c

 #define PYGAME_FREETYPE_INTERNAL
 
 #include "ft_mod.h"
+#include "ft_wrap.h"
 #include "pgfreetype.h"
 #include "pgsdl.h"
 #include "freetypebase_doc.h"
 
-void    PGFT_Quit(FreeTypeInstance *);
-int     PGFT_Init(FreeTypeInstance **);
-int     PGFT_TryLoadFont(FreeTypeInstance *ft, PyFreeTypeFont *font);
-void    PGFT_UnloadFont(FreeTypeInstance *, PyFreeTypeFont *);
+/* Externals */
+FILE *PyFile_AsFile(PyObject *p);
+
 
 void    _PGTF_SetError(const char *error_msg, FT_Error error_id);
 
 
-
 static unsigned long 
 _RWread(FT_Stream stream, 
         unsigned long offset, 
         unsigned char* buffer, 
         unsigned long count)
 {
-	SDL_RWops *src;
+    PyObject *obj;
+    FILE *file;
+    unsigned long readc;
 
-	src = (SDL_RWops *)stream->descriptor.pointer;
-	SDL_RWseek(src, (int)offset, SEEK_SET);
+    obj = (PyObject *)stream->descriptor.pointer;
 
-	if (count == 0)
-		return 0;
+    Py_BEGIN_ALLOW_THREADS;
 
-	return SDL_RWread(src, buffer, 1, (int)count);
+        file = PyFile_AsFile(obj);
+        fseek(file, (long int)offset, SEEK_SET);
+
+        readc = count ? fread(buffer, 1, count, file) : 0;
+
+    Py_END_ALLOW_THREADS;
+
+    return readc;
 }
 
-
-
 static FT_Error
 _PGTF_face_request(FTC_FaceID face_id, 
         FT_Library library, 
         FT_Pointer request_data, 
         FT_Face *aface)
 {
-    PyFreeTypeFont *font = (PyFreeTypeFont *)face_id;
+    FontId *id = GET_FONT_ID(face_id); 
 
     FT_Error error = 0;
     FT_Stream stream = NULL;
-    int position;
-    
-	position = SDL_RWtell(font->rwops);
-
-	if (position < 0)
-        goto error_cleanup;
+    FILE *file;
 
     stream = malloc(sizeof(FT_Stream));
 
 	memset(stream, 0, sizeof(FT_Stream));
 
 	stream->read = _RWread;
-	stream->descriptor.pointer = font->rwops;
-	stream->pos = (unsigned long)position;
-	SDL_RWseek(font->rwops, 0, SEEK_END);
-	stream->size = (unsigned long)(SDL_RWtell(font->rwops) - position);
-	SDL_RWseek(font->rwops, position, SEEK_SET);
+    /* do not let FT close the stream, Python will take care of it */
+    stream->close = NULL; 
 
-	font->open_args.flags = FT_OPEN_STREAM;
-	font->open_args.stream = stream;
+	stream->descriptor.pointer = id->file_ptr;
 
-	error = FT_Open_Face(library, &font->open_args, font->face_index, aface);
+    Py_BEGIN_ALLOW_THREADS;
+        file = PyFile_AsFile(id->file_ptr);
+        
+        stream->size = (unsigned long)fseek(file, 0, SEEK_END);
+        stream->pos = (unsigned long)fseek(file, 0, SEEK_SET);
+    Py_END_ALLOW_THREADS;
+
+	id->open_args.flags = FT_OPEN_STREAM;
+	id->open_args.stream = stream;
+
+	error = FT_Open_Face(library, &id->open_args, id->face_index, aface);
 
     if (error)
         goto error_cleanup;

File src/freetype/ft_wrap.h

+/*
+  pygame - Python Game Library
+  Copyright (C) 2000-2001 Pete Shinners
+  Copyright (C) 2008 Marcus von Appen
+  Copyright (C) 2009 Vicent Marti
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Library General Public
+  License as published by the Free Software Foundation; either
+  version 2 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Library General Public License for more details.
+
+  You should have received a copy of the GNU Library General Public
+  License along with this library; if not, write to the Free
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+*/
+
+#ifndef _PYGAME_FREETYPE_WRAP_H_
+#define _PYGAME_FREETYPE_WRAP_H_
+
+#define PYGAME_FREETYPE_INTERNAL
+#include "pgfreetype.h"
+
+typedef struct
+{
+    FT_Library library;
+    FTC_Manager cache_manager;
+    FTC_SBitCache cache_bitmap;
+    FTC_CMapCache cache_charmap;
+} FreeTypeInstance;
+
+FreeTypeInstance *_get_freetype(void);
+
+#define ASSERT_GRAB_FREETYPE(ft_ptr, rvalue)                    \
+    ft_ptr = _get_freetype();                                   \
+    if (ft_ptr == NULL)                                         \
+    {                                                           \
+        PyErr_SetString(PyExc_PyGameError,                      \
+            "The FreeType 2 library hasn't been initialized");  \
+        return (rvalue);                                        \
+    }
+
+#define GET_FONT_ID(f) (&((PyFreeTypeFont *)f)->id)
+
+
+void    PGFT_Quit(FreeTypeInstance *);
+int     PGFT_Init(FreeTypeInstance **);
+int     PGFT_TryLoadFont(FreeTypeInstance *ft, PyFreeTypeFont *font);
+void    PGFT_UnloadFont(FreeTypeInstance *, PyFreeTypeFont *);
+
+
+#endif

File src/freetype/pgfreetype.h

 /*
   pygame - Python Game Library
+  Copyright (C) 2000-2001 Pete Shinners
+  Copyright (C) 2008 Marcus von Appen
   Copyright (C) 2009 Vicent Marti
 
   This library is free software; you can redistribute it and/or
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */
-#ifndef _PYGAME_FONTS_H_
-#define _PYGAME_FONTS_H_
+#ifndef _PYGAME_FREETYPE_H_
+#define _PYGAME_FREETYPE_H_
 
-#include <SDL.h>
 #include "pgbase.h"
 
 #include <ft2build.h>  
 extern "C" {
 #endif
 
-typedef struct
-{
-    FT_Library library;
-    FTC_Manager cache_manager;
-    FTC_SBitCache cache_bitmap;
-    FTC_CMapCache cache_charmap;
-} FreeTypeInstance;
-
-FreeTypeInstance *_get_freetype(void);
-
-#define ASSERT_GRAB_FREETYPE(ft_ptr, rvalue)                    \
-    ft_ptr = _get_freetype();                                   \
-    if (ft_ptr == NULL)                                         \
-    {                                                           \
-        PyErr_SetString(PyExc_PyGameError,                      \
-            "The FreeType 2 library hasn't been initialized");  \
-        return (rvalue);                                        \
-    }
-
 #define PYGAME_FREETYPE_FIRSTSLOT 0
 #define PYGAME_FREETYPE_NUMSLOTS 0
 
 
 typedef struct
 {
+    PyObject *file_ptr;
+    int face_index;
+    FT_Open_Args open_args;
+} FontId;
+
+typedef struct
+{
     PyFont pyfont;
+    FontId id;
 
-    int face_index;
-    SDL_RWops *rwops;
-    int autoclose;
-    FT_Open_Args open_args;
 } PyFreeTypeFont;
 
 #define PyFreeTypeFont_AsFont(x) (((PyFreeTypeFont *)x)->font)