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 \
--- /dev/null
+/*****************************************************************************
+ * 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";
+}
--- /dev/null
+/*****************************************************************************
+ * 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
* 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
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 )
}
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
/// 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
/// Get the glyph corresponding to the given code
Glyph_t &getGlyph( uint32_t code ) const;
+ bool error( unsigned err, const char *msg );
};