Commits

Anonymous committed b9d0197

Cleanup of FreeType code.

Comments (0)

Files changed (7)

src/freetype/ft_cache.c

 FT_UInt32 _PGFT_Cache_Hash(const FontRenderMode *, FT_UInt);
 FT_UInt32 _PGFT_GetLoadFlags(const FontRenderMode *);
 
-PGFT_CacheNode *_PGFT_Cache_AllocateNode(FreeTypeInstance *, 
-        PGFT_Cache *, const FontRenderMode *, FT_UInt);
-void _PGFT_Cache_FreeNode(PGFT_Cache *, PGFT_CacheNode *);
+FontCacheNode *_PGFT_Cache_AllocateNode(FreeTypeInstance *, 
+        FontCache *, const FontRenderMode *, FT_UInt);
+void _PGFT_Cache_FreeNode(FontCache *, FontCacheNode *);
 
 
 FT_UInt32 _PGFT_GetLoadFlags(const FontRenderMode *render)
 } 
 
 void PGFT_Cache_Init(FreeTypeInstance *ft, 
-        PGFT_Cache *cache, PyFreeTypeFont *parent)
+        FontCache *cache, PyFreeTypeFont *parent)
 {
     int cache_size = MAX(ft->cache_size - 1, PGFT_MIN_CACHE_SIZE - 1);
 
 #endif
 }
 
-void PGFT_Cache_Destroy(PGFT_Cache *cache)
+void PGFT_Cache_Destroy(FontCache *cache)
 {
     FT_UInt i;
-    PGFT_CacheNode *node, *next;
+    FontCacheNode *node, *next;
 
     if (cache == NULL)
         return;
     free(cache->depths);
 }
 
-void PGFT_Cache_Cleanup(PGFT_Cache *cache)
+void PGFT_Cache_Cleanup(FontCache *cache)
 {
     const FT_Byte MAX_BUCKET_DEPTH = 2;
-    PGFT_CacheNode *node, *prev;
+    FontCacheNode *node, *prev;
     FT_UInt32 i;
 
     for (i = 0; i <= cache->size_mask; ++i)
 
 }
 
-FontGlyph *PGFT_Cache_FindGlyph(FreeTypeInstance *ft, PGFT_Cache *cache, 
+FontGlyph *PGFT_Cache_FindGlyph(FreeTypeInstance *ft, FontCache *cache, 
         FT_UInt character, const FontRenderMode *render)
 {
-    PGFT_CacheNode **nodes = cache->nodes;
-    PGFT_CacheNode *node, *prev;
+    FontCacheNode **nodes = cache->nodes;
+    FontCacheNode *node, *prev;
 
     FT_UInt32 hash = _PGFT_Cache_Hash(render, character);
     FT_UInt32 bucket = hash & cache->size_mask;
     return &node->glyph;
 }
 
-void _PGFT_Cache_FreeNode(PGFT_Cache *cache, PGFT_CacheNode *node)
+void _PGFT_Cache_FreeNode(FontCache *cache, FontCacheNode *node)
 {
     if (node == NULL)
         return;
     free(node);
 }
 
-PGFT_CacheNode *_PGFT_Cache_AllocateNode(FreeTypeInstance *ft, 
-        PGFT_Cache *cache, const FontRenderMode *render, FT_UInt character)
+FontCacheNode *_PGFT_Cache_AllocateNode(FreeTypeInstance *ft, 
+        FontCache *cache, const FontRenderMode *render, FT_UInt character)
 {
-    PGFT_CacheNode *node = NULL;
+    FontCacheNode *node = NULL;
     FontGlyph *glyph = NULL;
 
     FT_Glyph_Metrics *metrics;
     /* 
      * Allocate cache node 
      */
-    node = malloc(sizeof(PGFT_CacheNode));
+    node = malloc(sizeof(FontCacheNode));
     glyph = &node->glyph;
 
     /*

src/freetype/ft_font.c

 #include "freetypebase_doc.h"
 
 /*
+ * Auxiliar defines
+ */
+#define PGFT_CHECK_BOOL(_pyobj, _var)               \
+    if (_pyobj)                                     \
+    {                                               \
+        if (!PyBool_Check(_pyobj))                  \
+        {                                           \
+            PyErr_SetString(PyExc_TypeError,        \
+                #_var " must be a boolean value");  \
+            return NULL;                            \
+        }                                           \
+                                                    \
+        _var = PyObject_IsTrue(_pyobj);             \
+    }
+
+/*
  * Constructor/init/destructor
  */
 static PyObject *_ftfont_new(PyTypeObject *type, PyObject *args, PyObject *kwds);

src/freetype/ft_metrics.c

     int character, const FontRenderMode *render, int bbmode,
     void *minx, void *maxx, void *miny, void *maxy, void *advance)
 {
-    FT_Error        error;
-    FTC_ScalerRec   scale;
     FontGlyph *     glyph = NULL;
 
-
     glyph = PGFT_Cache_FindGlyph(ft, &PGFT_INTERNALS(font)->cache, 
             (FT_UInt)character, render);
 
 
     if (bbmode == FT_BBOX_EXACT || bbmode == FT_BBOX_EXACT_GRIDFIT)
     {
-        *(float *)minx =    (FP_266_FLOAT(*(int *)minx));
-        *(float *)miny =    (FP_266_FLOAT(*(int *)miny));
-        *(float *)maxx =    (FP_266_FLOAT(*(int *)maxx));
-        *(float *)maxy =    (FP_266_FLOAT(*(int *)maxy));
-        *(float *)advance = (FP_1616_FLOAT(*(int *)advance));
+#       define FP16_16(i)   ((float)((int)(i) / 65536.0f))
+#       define FP26_6(i)    ((float)((int)(i) / 64.0f))
+
+        *(float *)minx =    FP26_6(*(int *)minx);
+        *(float *)miny =    FP26_6(*(int *)miny);
+        *(float *)maxx =    FP26_6(*(int *)maxx);
+        *(float *)maxy =    FP26_6(*(int *)maxy);
+        *(float *)advance = FP16_16(*(int *)advance);
+
+#       undef FP16_16
+#       undef FP26_6
     }
 
     return 0;
+
 }
 
 
 _PGFT_GetTextSize_INTERNAL(FreeTypeInstance *ft, PyFreeTypeFont *font, 
     const FontRenderMode *render, FontText *text)
 {
-    FT_Vector   extent, advances[MAX_GLYPHS];
+    FT_Vector   extent, advances[PGFT_MAX_GLYPHS];
     FT_Error    error;
     FT_Vector   size;
 

src/freetype/ft_render.c

     mode->pt_size = (FT_UInt16)pt_size;
 
     if (style == FT_STYLE_DEFAULT)
-        mode->style = font->default_style;
+        mode->style = (FT_Byte)font->default_style;
     else
         mode->style = (FT_Byte)style;
 
     const FT_Fixed center = (1 << 15); // 0.5
 
     int         n;
-    FT_Vector   pen, advances[MAX_GLYPHS];
+    FT_Vector   pen, advances[PGFT_MAX_GLYPHS];
     FT_Matrix   rotation_matrix;
     FT_Face     face;
     FT_Error    error;

src/freetype/ft_text.c

 PGFT_LoadFontText(FreeTypeInstance *ft, PyFreeTypeFont *font, 
         const FontRenderMode *render, PyObject *text)
 {
+    const FT_UInt16 UNICODE_BOM_NATIVE  = 0xFEFF;
+    const FT_UInt16 UNICODE_BOM_SWAPPED = 0xFFFE;
+
     int         swapped = 0;
     int         string_length = 0;
 
 PGFT_GetTextAdvances(FreeTypeInstance *ft, PyFreeTypeFont *font, 
         const FontRenderMode *render, FontText *text, FT_Vector *advances)
 {
+    /* Default kerning mode for all text */
+    const int FT_KERNING_MODE = 1;
+
     FT_Face     face;
     FontGlyph   *glyph;
     FT_Pos      track_kern   = 0;

src/freetype/ft_wrap.h

 #   include "pgsdl.h"
 #endif
 
-#define FP_1616_FLOAT(i)    ((float)((int)(i) / 65536.0f))
-#define FP_248_FLOAT(i)     ((float)((int)(i) / 256.0f))
-#define FP_266_FLOAT(i)     ((float)((int)(i) / 64.0f))
 
+/**********************************************************
+ * Internal module defines
+ **********************************************************/
+
+/* Fixed point (26.6) math macros */
 #define PGFT_FLOOR(x)  (   (x)        & -64 )
 #define PGFT_CEIL(x)   ( ( (x) + 63 ) & -64 )
 #define PGFT_ROUND(x)  ( ( (x) + 32 ) & -64 )
 #define PGFT_TRUNC(x)  (   (x) >> 6 )
 
-#define PGFT_CHECK_BOOL(_pyobj, _var)               \
-    if (_pyobj)                                     \
-    {                                               \
-        if (!PyBool_Check(_pyobj))                  \
-        {                                           \
-            PyErr_SetString(PyExc_TypeError,        \
-                #_var " must be a boolean value");  \
-            return NULL;                            \
-        }                                           \
-                                                    \
-        _var = PyObject_IsTrue(_pyobj);             \
-    }
+/* Internal configuration variables */
+#define PGFT_MAX_GLYPHS         64
+#define PGFT_DEFAULT_CACHE_SIZE 64
+#define PGFT_MIN_CACHE_SIZE     32
+#undef  PGFT_DEBUG_CACHE
 
-#define UNICODE_BOM_NATIVE	0xFEFF
-#define UNICODE_BOM_SWAPPED	0xFFFE
 
-#define FT_KERNING_MODE 1 /* KERNING_MODE_DEFAULT */
-
-#define FT_RFLAG_NONE           (0)
-#define FT_RFLAG_ANTIALIAS      (1 << 0)
-#define FT_RFLAG_AUTOHINT       (1 << 1)
-#define FT_RFLAG_VERTICAL       (1 << 2)
-#define FT_RFLAG_HINTED         (1 << 3)
-
-/*
- * Default render flags:
- *      - Antialiasing off
- *      - Autohint off
- *      - Vertical text off
- *      - Hinted on
- */
-#define FT_RFLAG_DEFAULTS       (FT_RFLAG_NONE | FT_RFLAG_HINTED)
-
-#define MAX_GLYPHS      64
-#define PGFT_DEFAULT_CACHE_SIZE 64
-#define PGFT_MIN_CACHE_SIZE 32
-#define PGFT_DEBUG_CACHE
-
-
+/**********************************************************
+ * Internal data structures
+ **********************************************************/
 typedef struct
 {
     FT_Library library;
     FT_UInt16   style;
 } FontRenderMode;
 
-typedef struct  FontGlyph_
+typedef struct  __fontglyph
 {
     FT_UInt     glyph_index;
     FT_Glyph    image;    
     FT_Vector   size;
 } FontGlyph;
 
-typedef struct FontText_
+typedef struct __fonttext
 {
     FontGlyph **glyphs;
     int length;
     struct __cachenode *next;
     FT_UInt32 hash;
 
-} PGFT_CacheNode;
+} FontCacheNode;
 
-typedef struct __glyphcache
+typedef struct __fontcache
 {
-    PGFT_CacheNode  **nodes;
-    PGFT_CacheNode  *free_nodes;
+    FontCacheNode  **nodes;
+    FontCacheNode  *free_nodes;
 
     FT_Byte    *depths;
 
 
     FT_UInt32   size_mask;
     PyFreeTypeFont  *font;
-} PGFT_Cache;
+} FontCache;
 
+#define PGFT_INTERNALS(f) ((FontInternals *)(f->_internals))
 typedef struct FontInternals_
 {
-    PGFT_Cache  cache;
+    FontCache  cache;
     FontText    active_text;
 } FontInternals;
 
-#define PGFT_INTERNALS(f) ((FontInternals *)(f->_internals))
 
 
+/**********************************************************
+ * Module state
+ **********************************************************/
 typedef struct {
     FreeTypeInstance *freetype;
 } _FreeTypeState;
 
 #ifdef IS_PYTHON_3
-extern struct PyModuleDef _freetypemodule;
-#define FREETYPE_MOD_STATE(mod) ((_FreeTypeState*)PyModule_GetState(mod))
-#define FREETYPE_STATE FREETYPE_MOD_STATE(PyState_FindModule(&_freetypemodule))
+    extern struct PyModuleDef _freetypemodule;
+#   define FREETYPE_MOD_STATE(mod) ((_FreeTypeState*)PyModule_GetState(mod))
+#   define FREETYPE_STATE FREETYPE_MOD_STATE(PyState_FindModule(&_freetypemodule))
 #else
-extern _FreeTypeState _modstate;
-#define FREETYPE_MOD_STATE(mod) (&_modstate)
-#define FREETYPE_STATE FREETYPE_MOD_STATE(NULL)
+    extern _FreeTypeState _modstate;
+#   define FREETYPE_MOD_STATE(mod) (&_modstate)
+#   define FREETYPE_STATE FREETYPE_MOD_STATE(NULL)
 #endif
 
 #define ASSERT_GRAB_FREETYPE(ft_ptr, rvalue)                    \
         return (rvalue);                                        \
     }
 
-#define GET_FONT_ID(f) (&((PyFreeTypeFont *)f)->id)
 
-#define FT_FLOOR(X)	((X & -64) / 64)
-#define FT_CEIL(X)	(((X + 63) & -64) / 64)
+
+
+/**********************************************************
+ * Internal API
+ **********************************************************/
 
 /********************************************************* General functions ****/
 const char *PGFT_GetError(FreeTypeInstance *);
 
 
 /******************************************************** Glyph cache management ****/
-void        PGFT_Cache_Init(FreeTypeInstance *ft, PGFT_Cache *cache, PyFreeTypeFont *parent);
-void        PGFT_Cache_Destroy(PGFT_Cache *cache);
-void        PGFT_Cache_Cleanup(PGFT_Cache *cache);
-FontGlyph * PGFT_Cache_FindGlyph(FreeTypeInstance *ft, PGFT_Cache *cache, FT_UInt character, 
+void        PGFT_Cache_Init(FreeTypeInstance *ft, FontCache *cache, PyFreeTypeFont *parent);
+void        PGFT_Cache_Destroy(FontCache *cache);
+void        PGFT_Cache_Cleanup(FontCache *cache);
+FontGlyph * PGFT_Cache_FindGlyph(FreeTypeInstance *ft, FontCache *cache, FT_UInt character, 
                 const FontRenderMode *render);
 
 

src/freetype/pgfreetype.h

 extern "C" {
 #endif
 
+/**********************************************************
+ * Global module constants
+ **********************************************************/
+
+/* Render styles */
 #define FT_STYLE_NORMAL     0x00
 #define FT_STYLE_BOLD		0x01
 #define FT_STYLE_ITALIC     0x02
 #define FT_STYLE_UNDERLINE  0x04
 #define FT_STYLE_DEFAULT    0xFF
 
-/* Sane constant names */
+/* Bounding box modes */
 #define FT_BBOX_EXACT           FT_GLYPH_BBOX_SUBPIXELS
 #define FT_BBOX_EXACT_GRIDFIT   FT_GLYPH_BBOX_GRIDFIT
 #define FT_BBOX_PIXEL           FT_GLYPH_BBOX_TRUNCATE
 #define FT_BBOX_PIXEL_GRIDFIT   FT_GLYPH_BBOX_PIXELS
 
+/* Rendering flags */
+#define FT_RFLAG_NONE           (0)
+#define FT_RFLAG_ANTIALIAS      (1 << 0)
+#define FT_RFLAG_AUTOHINT       (1 << 1)
+#define FT_RFLAG_VERTICAL       (1 << 2)
+#define FT_RFLAG_HINTED         (1 << 3)
+#define FT_RFLAG_DEFAULTS       (FT_RFLAG_NONE | FT_RFLAG_HINTED)
+
 
 #define FT_RENDER_NEWBYTEARRAY      0x0
 #define FT_RENDER_NEWSURFACE        0x1
 #define FT_RENDER_EXISTINGSURFACE   0x2
 
-#define PYGAME_FREETYPE_FIRSTSLOT 0
-#define PYGAME_FREETYPE_NUMSLOTS 0
-
-#ifndef PYGAME_FREETYPE_INTERNAL
-#endif /* PYGAME_FREETYPE_INTERNAL */
+/**********************************************************
+ * Global module types
+ **********************************************************/
 
 typedef struct
 {
     void *_internals;
 } PyFreeTypeFont;
 
+
+/**********************************************************
+ * Module declaration
+ **********************************************************/
+#define PYGAME_FREETYPE_FIRSTSLOT 0
+#define PYGAME_FREETYPE_NUMSLOTS 0
+
 #define PyFreeTypeFont_AsFont(x) (((PyFreeTypeFont *)x)->font)
 
 #define PYGAME_FREETYPE_FONT_FIRSTSLOT \
     (*(PyObject*(*)(const char*,int))PyGameFreeType_C_API[PYGAME_FREETYPE_FONT_FIRSTSLOT + 1])
 #endif /* PYGAME_FREETYPE_FONT_INTERNAL */
 
-/*
- * C API export.
- */
+
+/**********************************************************
+ * C API export
+ **********************************************************/
 #ifdef PYGAME_INTERNAL
     void **PyGameFreeType_C_API;
 #else
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.