[vlc-devel] commit: Skins2: Invent ft2_strerror() to improve freetype2 error reporting. (JP Dinger )

git version control git at videolan.org
Sat Dec 5 22:35:13 CET 2009


vlc | branch: master | JP Dinger <jpd at videolan.org> | Sun Nov 22 19:06:47 2009 +0100| [82dfc1b375b66acb389e80475009ebf0244ccf64] | committer: JP Dinger 

Skins2: Invent ft2_strerror() to improve freetype2 error reporting.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=82dfc1b375b66acb389e80475009ebf0244ccf64
---

 modules/gui/skins2/Modules.am       |    2 +
 modules/gui/skins2/src/ft2_err.c    |   68 ++++++++++++++++++++++++++++++++
 modules/gui/skins2/src/ft2_err.h    |   34 ++++++++++++++++
 modules/gui/skins2/src/ft2_font.cpp |   73 ++++++++++++++++++-----------------
 modules/gui/skins2/src/ft2_font.hpp |    3 +-
 5 files changed, 144 insertions(+), 36 deletions(-)

diff --git a/modules/gui/skins2/Modules.am b/modules/gui/skins2/Modules.am
index 228626e..bb33366 100644
--- a/modules/gui/skins2/Modules.am
+++ b/modules/gui/skins2/Modules.am
@@ -113,6 +113,8 @@ SOURCES_skins2 = \
 	src/ft2_bitmap.hpp \
 	src/ft2_font.cpp \
 	src/ft2_font.hpp \
+	src/ft2_err.c \
+	src/ft2_err.h \
 	src/generic_bitmap.cpp \
 	src/generic_bitmap.hpp \
 	src/generic_font.hpp \
diff --git a/modules/gui/skins2/src/ft2_err.c b/modules/gui/skins2/src/ft2_err.c
new file mode 100644
index 0000000..079434f
--- /dev/null
+++ b/modules/gui/skins2/src/ft2_err.c
@@ -0,0 +1,68 @@
+/*****************************************************************************
+ * ft2_err.c: Provide a strerror() type function for freetype2
+ *****************************************************************************
+ * Copyright (C) 2009 the VideoLAN team
+ *
+ * Authors: JP Dinger <jpd (at) videolan (dot) org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include <ft2build.h>
+#include "ft2_err.h"
+
+/* Warning: This file includes the error definitions header twice.
+ * Further, freetype2 errors are not contiguous, so instead of a sparse
+ * array we first look up the actual entry by linear search and only
+ * then return the actual error string. It could be improved to binary
+ * search (assuming the freetype2 source stays sorted), but error
+ * reporting shouldn't need to be efficient.
+ */
+
+#define FT_NOERRORDEF_( sym, num, str ) num,
+#define FT_ERRORDEF_(   sym, num, str ) num,
+
+static const unsigned short ft2_errorindex[] =
+{
+#include FT_ERROR_DEFINITIONS_H
+};
+
+#undef  FT_NOERRORDEF_
+#undef  FT_ERRORDEF_
+
+
+#define FT_NOERRORDEF_( sym, num, str ) str,
+#define FT_ERRORDEF_(   sym, num, str ) str,
+
+static const char *ft2_errorstrings[] =
+{
+#include FT_ERROR_DEFINITIONS_H
+};
+
+#undef  FT_NOERRORDEF_
+#undef  FT_ERRORDEF_
+
+enum { ft2_num_errors = sizeof(ft2_errorindex)/sizeof(*ft2_errorindex) };
+
+const char *ft2_strerror(unsigned err)
+{
+    unsigned i;
+    for( i=0; i<ft2_num_errors; ++i )
+        if( err==ft2_errorindex[i] )
+            break;
+
+    return i<ft2_num_errors ? ft2_errorstrings[i] :
+           "An error freetype2 neglected to specify";
+}
diff --git a/modules/gui/skins2/src/ft2_err.h b/modules/gui/skins2/src/ft2_err.h
new file mode 100644
index 0000000..135fcc8
--- /dev/null
+++ b/modules/gui/skins2/src/ft2_err.h
@@ -0,0 +1,34 @@
+/*****************************************************************************
+ * ft2_err.h: turn freetype2 errors into strings
+ *****************************************************************************
+ * Copyright (C) 2009 the VideoLAN team
+ *
+ * Authors: JP Dinger <jpd (at) videolan (dot) org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef FT2_ERR_H
+#define FT2_ERR_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+const char *ft2_strerror(unsigned err);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/modules/gui/skins2/src/ft2_font.cpp b/modules/gui/skins2/src/ft2_font.cpp
index b0ade15..1fad1e0 100644
--- a/modules/gui/skins2/src/ft2_font.cpp
+++ b/modules/gui/skins2/src/ft2_font.cpp
@@ -22,8 +22,10 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#include <errno.h>
 #include "ft2_font.hpp"
 #include "ft2_bitmap.hpp"
+#include "ft2_err.h"
 #include "../utils/ustring.hpp"
 
 #ifdef HAVE_FRIBIDI
@@ -40,59 +42,58 @@ FT2Font::FT2Font( intf_thread_t *pIntf, const string &rName, int size ):
 
 FT2Font::~FT2Font()
 {
-    // Clear the glyph cache
     GlyphMap_t::iterator iter;
     for( iter = m_glyphCache.begin(); iter != m_glyphCache.end(); ++iter )
-    {
         FT_Done_Glyph( (*iter).second.m_glyph );
-    }
-    if( m_face )
-    {
-        FT_Done_Face( m_face );
-    }
-    if( m_lib )
-    {
-        FT_Done_FreeType( m_lib );
-    }
-    free( m_buffer );
+    if( m_face ) FT_Done_Face( m_face );
+    if( m_lib )  FT_Done_FreeType( m_lib );
+    delete[] m_buffer;
 }
 
 
 bool FT2Font::init()
 {
-    int err;
+    unsigned err;
 
-    // Initialize libfreetype
-    if( FT_Init_FreeType( &m_lib ) )
+    if( err = FT_Init_FreeType( &m_lib ) )
     {
-        msg_Err( getIntf(), "failed to initialize freetype" );
+        msg_Err( getIntf(), "failed to initialize freetype (%s)",
+                 ft2_strerror( err ) );
         return false;
     }
 
-    // Open the font
     FILE *file = fopen( m_name.c_str(), "rb" );
-    if( file )
+    if( !file )
     {
-        msg_Dbg( getIntf(), "loading font %s", m_name.c_str() );
-    }
-    else
-    {
-        msg_Dbg( getIntf(), "unable to open the font %s", m_name.c_str() );
+        msg_Dbg( getIntf(), "failed to open font %s (%s)",
+                 m_name.c_str(), strerror(errno) );
         return false;
     }
-    // Get the file size
+    msg_Dbg( getIntf(), "loading font %s", m_name.c_str() );
+
     fseek( file, 0, SEEK_END );
-    int size = ftell( file );
+    long size = ftell( file );
     rewind( file );
-    // Allocate the buffer
-    m_buffer = malloc( size );
+
+    if( -1==size )
+    {
+        msg_Dbg( getIntf(), "fseek loading font %s (%s)",
+                 m_name.c_str(), strerror(errno) );
+        fclose( file );
+        return false;
+    }
+
+    m_buffer = new (std::nothrow) char[size];
     if( !m_buffer )
+    {
+        fclose( file );
         return false;
-    // Read the font data
+    }
+
     fread( m_buffer, size, 1, file );
     fclose( file );
 
-    // Load the font from the buffer
+
     err = FT_New_Memory_Face( m_lib, (const FT_Byte*)m_buffer, size, 0,
                               &m_face );
     if ( err == FT_Err_Unknown_File_Format )
@@ -102,22 +103,24 @@ bool FT2Font::init()
     }
     else if ( err )
     {
-        msg_Err( getIntf(), "error opening font (%s)", m_name.c_str() );
+        msg_Err( getIntf(), "error opening font %s (%s)",
+                 m_name.c_str(), ft2_strerror(err) );
         return false;
     }
 
     // Select the charset
-    if( FT_Select_Charmap( m_face, ft_encoding_unicode ) )
+    if( err = FT_Select_Charmap( m_face, ft_encoding_unicode ) )
     {
-        msg_Err( getIntf(), "font has no UNICODE table (%s)", m_name.c_str() );
+        msg_Err( getIntf(), "font %s has no UNICODE table (%s)",
+                 m_name.c_str(), ft2_strerror(err) );
         return false;
     }
 
     // Set the pixel size
-    if( FT_Set_Pixel_Sizes( m_face, 0, m_size ) )
+    if( err = FT_Set_Pixel_Sizes( m_face, 0, m_size ) )
     {
-        msg_Warn( getIntf(), "cannot set a pixel size of %d (%s)", m_size,
-                  m_name.c_str() );
+        msg_Warn( getIntf(), "cannot set a pixel size of %d for %s (%s)",
+                  m_size, m_name.c_str(), ft2_strerror(err) );
     }
 
     // Get the font metrucs
diff --git a/modules/gui/skins2/src/ft2_font.hpp b/modules/gui/skins2/src/ft2_font.hpp
index 862ec6b..6b7cafc 100644
--- a/modules/gui/skins2/src/ft2_font.hpp
+++ b/modules/gui/skins2/src/ft2_font.hpp
@@ -67,7 +67,7 @@ private:
     /// File name
     const string m_name;
     /// Buffer to store the font
-    void *m_buffer;
+    char *m_buffer;
     /// Pixel size of the font
     int m_size;
     /// Handle to FT library
@@ -81,6 +81,7 @@ private:
 
     /// Get the glyph corresponding to the given code
     Glyph_t &getGlyph( uint32_t code ) const;
+    bool error( unsigned err, const char *msg );
 };
 
 




More information about the vlc-devel mailing list