Commits

Anonymous committed 787b40b

Refactored font point size into FontRenderMode struct.

Comments (0)

Files changed (6)

src/freetype/ft_cache.c

         glyph = &cache->nodes[hash & cache->size_mask];
 
         if (*glyph == NULL)
-            *glyph = _PGFT_Cache_AllocateGlyph(cache, render, glyph_index);
+        {
+           *glyph = _PGFT_Cache_AllocateGlyph(cache, render, glyph_index);
+           return *glyph;
+        }
         
         if ((*glyph)->glyph_index == glyph_index)
+        {
             return *glyph;
+        }
 
     } while (cuckoo_hash++ < 2);
 

src/freetype/ft_font.c

                 &text, &style, &vertical_obj, &rotation, &ptsize))
         return NULL;
 
-    PGFT_CHECK_PTSIZE();
     PGFT_CHECK_BOOL(vertical_obj, vertical);
 
     /* Build rendering mode, always anti-aliased by default */
-    PGFT_BuildRenderMode(&render, style, vertical, 1, rotation);
+    if (PGFT_BuildRenderMode(ft, font, 
+            &render, ptsize, style, vertical, 1, rotation) != 0)
+    {
+        PyErr_SetString(PyExc_PyGameError, PGFT_GetError(ft));
+        return NULL;
+    }
 
-    error = PGFT_GetTextSize(ft, (PyFreeTypeFont *)self, ptsize, &render,
-            text, &width, &height);
+    error = PGFT_GetTextSize(ft, font, &render,text, &width, &height);
 
     if (error)
         PyErr_SetString(PyExc_RuntimeError, PGFT_GetError(ft));
     };
 
     PyFreeTypeFont *font = (PyFreeTypeFont *)self;
+    FontRenderMode render;
 
     /* aux vars */
     void *buf = NULL;
                 &text, &ptsize, &bbmode))
         return NULL;
 
-    /* check ptsize */
-    PGFT_CHECK_PTSIZE();
+    /*
+     * Build the render mode with the given size and no
+     * rotation/styles/vertical text
+     */
+    if (PGFT_BuildRenderMode(ft, font, 
+                &render, ptsize, FT_STYLE_NORMAL, 0, 1, 0) != 0)
+    {
+        PyErr_SetString(PyExc_PyGameError, PGFT_GetError(ft));
+        return NULL;
+    }
 
     /* check text */
     if (PyUnicode_Check(text))
                                                         \
         if (PGFT_GetMetrics(ft,                         \
                 (PyFreeTypeFont *)self, char_id,        \
-                ptsize, bbmode,                         \
+                &render, bbmode,                        \
                 &minx_##_mt, &maxx_##_mt,               \
                 &miny_##_mt, &maxy_##_mt,               \
                 &advance_##_mt) == 0)                   \
     };
 
     PyFreeTypeFont *font = (PyFreeTypeFont *)self;
+    FontRenderMode render;
 
     /* input arguments */
     PyObject *text = NULL;
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i", kwlist, &text, &ptsize))
         return NULL;
 
-    PGFT_CHECK_PTSIZE();
+    /*
+     * Build the render mode with the given size and no
+     * rotation/styles/vertical text
+     */
+    if (PGFT_BuildRenderMode(ft, font, 
+                &render, ptsize, FT_STYLE_NORMAL, 0, 1, 0) != 0)
+    {
+        PyErr_SetString(PyExc_PyGameError, PGFT_GetError(ft));
+        return NULL;
+    }
 
-    rbuffer = PGFT_Render_PixelArray(ft, font, ptsize, text, &width, &height);
+    rbuffer = PGFT_Render_PixelArray(ft, font, &render, text, &width, &height);
 
     if (!rbuffer)
     {
                 &style, &vertical_obj, &rotation, &antialias_obj, &ptsize))
         return NULL;
 
-    PGFT_CHECK_PTSIZE();
-
     if (!PyColor_Check(fg_color))
     {
         PyErr_SetString(PyExc_TypeError, "fgcolor must be a Color");
     PGFT_CHECK_BOOL(vertical_obj, vertical);
     PGFT_CHECK_BOOL(antialias_obj, antialias);
 
-    /* TODO: handle antialiasing */
-    PGFT_BuildRenderMode(&render, style, vertical, antialias, rotation);
+    if (PGFT_BuildRenderMode(ft, font, 
+                &render, ptsize, style, vertical, antialias, rotation) != 0)
+    {
+        PyErr_SetString(PyExc_PyGameError, PGFT_GetError(ft));
+        return NULL;
+    }
 
     if (!target_surf || target_surf == Py_None)
     {
         /* TODO! */
         PyObject *r_surface = NULL;
 
-        r_surface = PGFT_Render_NewSurface(ft, font, ptsize, &render, text,
+        r_surface = PGFT_Render_NewSurface(ft, font, &render, text,
                 (PyColor *)fg_color, (PyColor *)bg_color, &width, &height);
 
         if (!r_surface)
     }
     else if (PySDLSurface_Check(target_surf))
     {
-        if (PGFT_Render_ExistingSurface(ft, font, ptsize, &render, 
+        if (PGFT_Render_ExistingSurface(ft, font, &render, 
                 text, (PySDLSurface *)target_surf,
                 xpos, ypos, (PyColor *)fg_color, (PyColor *)bg_color,
                 &width, &height) != 0)

src/freetype/ft_metrics.c

 
 
 int PGFT_GetMetrics(FreeTypeInstance *ft, PyFreeTypeFont *font,
-    int character, int font_size, int bbmode,
+    int character, const FontRenderMode *render, int bbmode,
     void *minx, void *maxx, void *miny, void *maxy, void *advance)
 {
     FT_Error error;
     FTC_ScalerRec scale;
     FT_Glyph glyph;
 
+    /*
+     * TODO:
+     * Load the glyph information from the cache
+     */
+
+    return -1;
+
+#if 0
     _PGFT_BuildScaler(font, &scale, font_size);
 
     error = _PGFT_LoadGlyph(ft, font, 0, &scale, character, &glyph, NULL);
         _PGFT_SetError(ft, "Failed to load glyph metrics", error);
         return error;
     }
+#endif
 
     _PGFT_GetMetrics_INTERNAL(glyph, (FT_UInt)bbmode, minx, maxx, miny, maxy, advance);
 
 
 int
 _PGFT_GetTextSize_INTERNAL(FreeTypeInstance *ft, PyFreeTypeFont *font, 
-    int pt_size, FontRenderMode *render, FontText *text)
+    const FontRenderMode *render, FontText *text)
 {
     FT_Vector   extent, advances[MAX_GLYPHS];
     FT_Error    error;
     FT_Vector   size;
 
-    error = PGFT_GetTextAdvances(ft, font, pt_size, render, text, advances);
+    error = PGFT_GetTextAdvances(ft, font, render, text, advances);
 
     if (error)
         return error;
 }
 
 int
-PGFT_GetTextSize(FreeTypeInstance *ft, PyFreeTypeFont *font, int pt_size,
-    FontRenderMode *render, PyObject *text, int *w, int *h)
+PGFT_GetTextSize(FreeTypeInstance *ft, PyFreeTypeFont *font,
+    const FontRenderMode *render, PyObject *text, int *w, int *h)
 {
     FontText *font_text;
 
-    font_text = PGFT_LoadFontText(ft, font, pt_size, render, text);
+    font_text = PGFT_LoadFontText(ft, font, render, text);
 
     if (!font_text)
         return -1;
 
-    if (_PGFT_GetTextSize_INTERNAL(ft, font, pt_size, render, font_text) != 0)
+    if (_PGFT_GetTextSize_INTERNAL(ft, font, render, font_text) != 0)
         return -1;
 
     *w = PGFT_TRUNC(PGFT_ROUND(font_text->text_size.x));

src/freetype/ft_render.c

     return bold_str;
 }
 
-void 
-PGFT_BuildRenderMode(FontRenderMode *mode, int style, int vertical, int antialias, int rotation)
+int 
+PGFT_BuildRenderMode(FreeTypeInstance *ft, 
+        PyFreeTypeFont *font, FontRenderMode *mode, 
+        int pt_size, int style, int vertical, int antialias, int rotation)
 {
     int angle;
 
+    if (pt_size == -1)
+    {
+        if (font->default_ptsize == -1)
+        {
+            _PGFT_SetError(ft, "No font point size specified"
+                    " and no default font size in typeface", 0);
+            return -1;
+        }
+
+        pt_size = font->default_ptsize;
+    }
+
+    mode->pt_size = (FT_UInt16)pt_size;
     mode->style = (FT_Byte)style;
     mode->render_flags = FT_RFLAG_DEFAULTS;
 
     angle = rotation % 360;
     while (angle < 0) angle += 360;
     mode->rotation_angle = (FT_UInt16)angle;
+
+    /* TODO: handle error returns on function calls */
+    return 0;
 }
 
 
  *********************************************************/
 #ifdef HAVE_PYGAME_SDL_VIDEO
 int PGFT_Render_ExistingSurface(FreeTypeInstance *ft, PyFreeTypeFont *font,
-    int font_size, FontRenderMode *render, PyObject *text, PySDLSurface *_surface, 
+    const FontRenderMode *render, PyObject *text, PySDLSurface *_surface, 
     int x, int y, PyColor *fgcolor, PyColor *bgcolor, int *_width, int *_height)
 {
     static const FontRenderPtr __SDLrenderFuncs[] =
     }
 
     /* build font text */
-    font_text = PGFT_LoadFontText(ft, font, font_size, render, text);
+    font_text = PGFT_LoadFontText(ft, font, render, text);
 
     if (!font_text)
         return -1;
 
-    _PGFT_GetTextSize_INTERNAL(ft, font, font_size, render, font_text);
+    _PGFT_GetTextSize_INTERNAL(ft, font, render, font_text);
 
     width = PGFT_TRUNC(PGFT_ROUND(font_text->text_size.x));
     height = PGFT_TRUNC(PGFT_ROUND(font_text->text_size.y));
     /*
      * Render!
      */
-    if (_PGFT_Render_INTERNAL(ft, font, font_text, font_size, fgcolor, &font_surf, render) != 0)
+    if (_PGFT_Render_INTERNAL(ft, font, font_text, render, fgcolor, &font_surf) != 0)
         return -1;
 
     *_width = width;
 }
 
 PyObject *PGFT_Render_NewSurface(FreeTypeInstance *ft, PyFreeTypeFont *font, 
-    int ptsize, FontRenderMode *render, PyObject *text,
+    const FontRenderMode *render, PyObject *text,
     PyColor *fgcolor, PyColor *bgcolor, int *_width, int *_height)
 {
     int locked = 0;
     int width, height;
 
     /* build font text */
-    font_text = PGFT_LoadFontText(ft, font, ptsize, render, text);
+    font_text = PGFT_LoadFontText(ft, font, render, text);
 
     if (!font_text)
         return NULL;
 
-    if (_PGFT_GetTextSize_INTERNAL(ft, font, ptsize, render, font_text) != 0)
+    if (_PGFT_GetTextSize_INTERNAL(ft, font, render, font_text) != 0)
         return NULL;
 
     width = PGFT_TRUNC(PGFT_ROUND(font_text->text_size.x));
     /*
      * Render the text!
      */
-    if (_PGFT_Render_INTERNAL(ft, font, font_text, ptsize, fgcolor, &font_surf, render) != 0)
+    if (_PGFT_Render_INTERNAL(ft, font, font_text, render, fgcolor, &font_surf) != 0)
     {
         SDL_FreeSurface(surface);
         return NULL;
  *********************************************************/
 
 PyObject *PGFT_Render_PixelArray(FreeTypeInstance *ft, PyFreeTypeFont *font,
-    int ptsize, PyObject *text, int *_width, int *_height)
+    const FontRenderMode *render, PyObject *text, int *_width, int *_height)
 {
     FT_Byte *buffer = NULL;
     PyObject *array = NULL;
     int width, height;
 
     FontText *font_text;
-    FontRenderMode render;
     int array_size;
 
-    /* 
-     * TODO: pixel arrays must also take custom rendering styles
-     * ... or should them?
-     */
-    PGFT_BuildRenderMode(&render, FT_STYLE_NORMAL, 0, 1, 0);
-
     /* build font text */
-    font_text = PGFT_LoadFontText(ft, font, ptsize, &render, text);
+    font_text = PGFT_LoadFontText(ft, font, render, text);
 
     if (!font_text)
         goto cleanup;
 
-    if (_PGFT_GetTextSize_INTERNAL(ft, font, ptsize, &render, font_text) != 0)
+    if (_PGFT_GetTextSize_INTERNAL(ft, font, render, font_text) != 0)
         goto cleanup;
 
     width = PGFT_TRUNC(PGFT_ROUND(font_text->text_size.x));
     surf.format = NULL;
     surf.render = __render_glyph_ByteArray;
 
-    if (_PGFT_Render_INTERNAL(ft, font, font_text, ptsize, 0x0, &surf, &render) != 0)
+    if (_PGFT_Render_INTERNAL(ft, font, font_text, render, 0x0, &surf) != 0)
         goto cleanup;
 
     *_width = width;
  *
  *********************************************************/
 int _PGFT_Render_INTERNAL(FreeTypeInstance *ft, PyFreeTypeFont *font, 
-    FontText *text, int font_size, PyColor *fg_color,
-    FontSurface *surface, FontRenderMode *render)
+    FontText *text, const FontRenderMode *render, PyColor *fg_color,
+    FontSurface *surface)
 {
     const FT_Fixed center = (1 << 15); // 0.5
 
     /******************************************************
      * Load scaler, size & face
      ******************************************************/
-    face = _PGFT_GetFaceSized(ft, font, font_size);
+    face = _PGFT_GetFaceSized(ft, font, render->pt_size);
 
     if (!face)
     {
     /******************************************************
      * Load advance information
      ******************************************************/
-    error = PGFT_GetTextAdvances(ft, font, font_size, render, text, advances);
+    error = PGFT_GetTextAdvances(ft, font, render, text, advances);
 
     if (error)
     {

src/freetype/ft_text.c

 
 FontText *
 PGFT_LoadFontText(FreeTypeInstance *ft, PyFreeTypeFont *font, 
-        int pt_size, FontRenderMode *render, PyObject *text)
+        const FontRenderMode *render, PyObject *text)
 {
-    /*
-     * TODO:
-     * - Hash the text string and size
-     * - store it in the ft instance
-     * - automatically free the previous one
-     * - return existing ones if available
-     */
-
     FT_Int32 load_flags = FT_LOAD_DEFAULT; 
 
     int         swapped = 0;
     }
 
     /* load our sized face */
-    face = _PGFT_GetFaceSized(ft, font, pt_size);
+    face = _PGFT_GetFaceSized(ft, font, render->pt_size);
 
     if (!face)
     {
 }
 
 int
-PGFT_GetTextAdvances(FreeTypeInstance *ft, PyFreeTypeFont *font, int pt_size, 
-        FontRenderMode *render, FontText *text, FT_Vector *advances)
+PGFT_GetTextAdvances(FreeTypeInstance *ft, PyFreeTypeFont *font, 
+        const FontRenderMode *render, FontText *text, FT_Vector *advances)
 {
     FT_Face     face;
     FontGlyph   *glyph;
     FT_Int      i;
     FT_Fixed    bold_str    = 0;
 
-    face = _PGFT_GetFaceSized(ft, font, pt_size);
+    face = _PGFT_GetFaceSized(ft, font, render->pt_size);
 
     if (!face)
         return -1;

src/freetype/ft_wrap.h

 
 
 /********************************************************* Metrics management ****/
-int         PGFT_GetTextSize(FreeTypeInstance *ft, PyFreeTypeFont *font, int pt_size,
-                FontRenderMode *render, PyObject *text, int *w, int *h);
+int         PGFT_GetTextSize(FreeTypeInstance *ft, PyFreeTypeFont *font,
+                const FontRenderMode *render, PyObject *text, int *w, int *h);
 
 int         PGFT_GetMetrics(FreeTypeInstance *ft, PyFreeTypeFont *font,
-                int character, int font_size, int bbmode, 
+                int character, const FontRenderMode *render, int bbmode, 
                 void *minx, void *maxx, void *miny, void *maxy, void *advance);
 
 int         _PGFT_GetTextSize_INTERNAL(FreeTypeInstance *ft, PyFreeTypeFont *font, 
-                int pt_size, FontRenderMode *render, FontText *text);
+                const FontRenderMode *render, FontText *text);
 
 void        _PGFT_GetMetrics_INTERNAL(FT_Glyph, FT_UInt, int *, int *, int *, int *, int *);
 
 
 /******************************************************************* Rendering ****/
 PyObject *  PGFT_Render_PixelArray(FreeTypeInstance *ft, PyFreeTypeFont *font,
-                int ptsize, PyObject *text, int *_width, int *_height);
+                const FontRenderMode *render, PyObject *text, int *_width, int *_height);
 
 PyObject *  PGFT_Render_NewSurface(FreeTypeInstance *ft, PyFreeTypeFont *font,
-                int ptsize, FontRenderMode *render, PyObject *text,
+                const FontRenderMode *render, PyObject *text,
                 PyColor *fgcolor, PyColor *bgcolor, int *_width, int *_height);
 
 int         PGFT_Render_ExistingSurface(FreeTypeInstance *ft, PyFreeTypeFont *font,
-                int font_size, FontRenderMode *render, PyObject *text, 
+                const FontRenderMode *render, PyObject *text, 
                 PySDLSurface *_surface, int x, int y, PyColor *fgcolor, PyColor *bgcolor,
                 int *_width, int *_height);
 
-void        PGFT_BuildRenderMode(FontRenderMode *mode, int style, int vertical, 
-                int antialias, int rotation);
+int         PGFT_BuildRenderMode(FreeTypeInstance *ft, 
+                PyFreeTypeFont *font, FontRenderMode *mode, int pt_size, 
+                int style, int vertical, int antialias, int rotation);
 
 FT_Fixed    PGFT_GetBoldStrength(FT_Face face);
 
 int         _PGFT_Render_INTERNAL(FreeTypeInstance *ft, PyFreeTypeFont *font, 
-                FontText *text, int font_size, PyColor *fg_color, 
-                FontSurface *surface, FontRenderMode *render);
+                FontText *text, const FontRenderMode *render, PyColor *fg_color, 
+                FontSurface *surface);
 
 
 /******************************************************************* Render callbacks ****/
 void __render_glyph_ByteArray(int x, int y, FontSurface *surface, FT_Bitmap *bitmap, PyColor *color);
 
 /******************************************************** Font text management ****/
-FontText *  PGFT_LoadFontText(FreeTypeInstance *ft, PyFreeTypeFont *font, int pt_size, 
-                FontRenderMode *render, PyObject *text);
+FontText *  PGFT_LoadFontText(FreeTypeInstance *ft, PyFreeTypeFont *font, 
+                const FontRenderMode *render, PyObject *text);
 
-int         PGFT_GetTextAdvances(FreeTypeInstance *ft, PyFreeTypeFont *font, int pt_size, 
-                FontRenderMode *render, FontText *text, FT_Vector *advances);
+int         PGFT_GetTextAdvances(FreeTypeInstance *ft, PyFreeTypeFont *font, 
+                const FontRenderMode *render, FontText *text, FT_Vector *advances);
 
 FT_UInt16 * PGFT_BuildUnicodeString(PyObject *);