Commit 1d9265ad40d3aacb6e44a6737ff292ae68993790

sammy 2008-04-30T14:10:08

* Improve constructor code in the FTFont and FTLayout C bindings. Shorter (40 lines) and more consistend code.

diff --git a/src/FTFont/FTFontGlue.cpp b/src/FTFont/FTFontGlue.cpp
index 7a7d862..13ec6fa 100644
--- a/src/FTFont/FTFontGlue.cpp
+++ b/src/FTFont/FTFontGlue.cpp
@@ -30,82 +30,44 @@
 
 FTGL_BEGIN_C_DECLS
 
-static inline FTGLfont *createFTFont(FTGL::FontType type, const char *fontname)
-{
-    FTGLfont *ftgl = (FTGLfont*)malloc(sizeof(FTGLfont));
-    ftgl->type = type;
-    switch(type)
-    {
-        case FTGL::FONT_BITMAP:
-            ftgl->ptr = new FTGLBitmapFont(fontname);
-            break;
-        case FTGL::FONT_PIXMAP:
-            ftgl->ptr = new FTGLPixmapFont(fontname);
-            break;
-        case FTGL::FONT_OUTLINE:
-            ftgl->ptr = new FTGLOutlineFont(fontname);
-            break;
-        case FTGL::FONT_POLYGON:
-            ftgl->ptr = new FTGLPolygonFont(fontname);
-            break;
-        case FTGL::FONT_EXTRUDE:
-            ftgl->ptr = new FTGLExtrdFont(fontname);
-            break;
-        case FTGL::FONT_TEXTURE:
-            ftgl->ptr = new FTGLTextureFont(fontname);
-            break;
-    }
-
-    if(ftgl->ptr->Error())
-    {
-        ftglDestroyFont(ftgl);
-        return NULL;
+#define C_TOR(cname, cargs, cxxname, cxxarg, cxxtype) \
+    FTGLfont* cname cargs \
+    { \
+        cxxname *f = new cxxname cxxarg; \
+        if(f->Error()) \
+        { \
+            delete f; \
+            return NULL; \
+        } \
+        FTGLfont *ftgl = (FTGLfont *)malloc(sizeof(FTGLfont)); \
+        ftgl->ptr = f; \
+        ftgl->type = cxxtype; \
+        return ftgl; \
     }
 
-    return ftgl;
-}
-
 // FTGLBitmapFont::FTGLBitmapFont();
-FTGLfont* ftglCreateBitmapFont(const char *fontname)
-{
-    FTGLfont *ftgl = createFTFont(FTGL::FONT_BITMAP, fontname);
-    return ftgl;
-}
+C_TOR(ftglCreateBitmapFont, (const char *fontname),
+      FTGLBitmapFont, (fontname), FONT_BITMAP);
 
 // FTGLExtrdFont::FTGLExtrdFont();
-FTGLfont* ftglCreateExtrdFont(const char *fontname)
-{
-    FTGLfont *ftgl = createFTFont(FTGL::FONT_EXTRUDE, fontname);
-    return ftgl;
-}
+C_TOR(ftglCreateExtrdFont, (const char *fontname),
+      FTGLExtrdFont, (fontname), FONT_EXTRUDE);
 
 // FTGLOutlineFont::FTGLOutlineFont();
-FTGLfont* ftglCreateOutlineFont(const char *fontname)
-{
-    FTGLfont *ftgl = createFTFont(FTGL::FONT_OUTLINE, fontname);
-    return ftgl;
-}
+C_TOR(ftglCreateOutlineFont, (const char *fontname),
+      FTGLOutlineFont, (fontname), FONT_OUTLINE);
 
 // FTGLPixmapFont::FTGLPixmapFont();
-FTGLfont* ftglCreatePixmapFont(const char *fontname)
-{
-    FTGLfont *ftgl = createFTFont(FTGL::FONT_PIXMAP, fontname);
-    return ftgl;
-}
+C_TOR(ftglCreatePixmapFont, (const char *fontname),
+      FTGLPixmapFont, (fontname), FONT_PIXMAP);
 
 // FTGLPolygonFont::FTGLPolygonFont();
-FTGLfont* ftglCreatePolygonFont(const char *fontname)
-{
-    FTGLfont *ftgl = createFTFont(FTGL::FONT_POLYGON, fontname);
-    return ftgl;
-}
+C_TOR(ftglCreatePolygonFont, (const char *fontname),
+      FTGLPolygonFont, (fontname), FONT_POLYGON);
 
 // FTGLTextureFont::FTGLTextureFont();
-FTGLfont* ftglCreateTextureFont(const char *fontname)
-{
-    FTGLfont *ftgl = createFTFont(FTGL::FONT_TEXTURE, fontname);
-    return ftgl;
-}
+C_TOR(ftglCreateTextureFont, (const char *fontname),
+      FTGLTextureFont, (fontname), FONT_TEXTURE);
 
 #define C_FUN(cret, cname, cargs, cxxerr, cxxname, cxxarg) \
     cret cname cargs \
diff --git a/src/FTLayout/FTLayoutGlue.cpp b/src/FTLayout/FTLayoutGlue.cpp
index 3ed890f..b4b877e 100644
--- a/src/FTLayout/FTLayoutGlue.cpp
+++ b/src/FTLayout/FTLayoutGlue.cpp
@@ -30,26 +30,23 @@
 
 FTGL_BEGIN_C_DECLS
 
-static inline FTGLlayout *createFTLayout(FTGL::LayoutType type)
-{
-    FTGLlayout *layout = (FTGLlayout*)malloc(sizeof(FTGLlayout));
-    layout->font = NULL;
-    layout->type = type;
-    switch(type)
-    {
-        case FTGL::LAYOUT_SIMPLE:
-            layout->ptr = new FTSimpleLayout();
-            break;
+#define C_TOR(cname, cargs, cxxname, cxxarg, cxxtype) \
+    FTGLlayout* cname cargs \
+    { \
+        cxxname *l = new cxxname cxxarg; \
+        if(l->Error()) \
+        { \
+            delete l; \
+            return NULL; \
+        } \
+        FTGLlayout *ftgl = (FTGLlayout *)malloc(sizeof(FTGLlayout)); \
+        ftgl->ptr = l; \
+        ftgl->type = cxxtype; \
+        return ftgl; \
     }
-    return layout;
-}
 
 // FTSimpleLayout::FTSimpleLayout();
-FTGLlayout* ftglCreateSimpleLayout()
-{
-    FTGLlayout *layout = createFTLayout(FTGL::LAYOUT_SIMPLE);
-    return layout;
-}
+C_TOR(ftglCreateSimpleLayout, (), FTSimpleLayout, (), LAYOUT_SIMPLE);
 
 #define C_FUN(cret, cname, cargs, cxxerr, cxxname, cxxarg) \
     cret cname cargs \