Skip to content
Browse files

Make FreeType error handling give sensible error descriptions

  • Loading branch information...
1 parent 2948366 commit 167f4400af75c8dd2e46be6f23afeb84ed0f5bb0 @mgdm mgdm committed
Showing with 51 additions and 9 deletions.
  1. +22 −0 cairo.c
  2. +4 −8 cairo_ft_font.c
  3. +18 −0 php_cairo.h
  4. +6 −0 php_cairo_api.h
  5. +1 −1 tests/CairoFontFace/CairoFtFontFace/cairo_ft_font_face_create.phpt
View
22 cairo.c
@@ -39,6 +39,9 @@ zend_object_handlers cairo_std_object_handlers;
#if defined(CAIRO_HAS_FT_FONT) && defined(HAVE_FREETYPE)
ZEND_DECLARE_MODULE_GLOBALS(cairo)
+const php_cairo_ft_error php_cairo_ft_errors[] =
+#include FT_ERRORS_H
+
PHP_GINIT_FUNCTION(cairo)
{
cairo_globals->ft_lib = NULL;
@@ -51,6 +54,7 @@ PHP_GSHUTDOWN_FUNCTION(cairo)
}
}
+
#endif
/* Cairo Functions */
@@ -604,6 +608,24 @@ ZEND_BEGIN_ARG_INFO(cairo_recording_surface_create_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, extents)
ZEND_END_ARG_INFO()
+
+#if defined(CAIRO_HAS_FT_FONT) && defined(HAVE_FREETYPE)
+
+const char* php_cairo_get_ft_error(int error TSRMLS_DC) {
+ int i;
+ php_cairo_ft_error *current_error = php_cairo_ft_errors;
+
+ while (current_error->err_msg != NULL) {
+ if (current_error->err_code == error) {
+ return current_error->err_msg;
+ }
+ current_error++;
+ }
+ return NULL;
+}
+
+#endif
+
/* {{{ proto int cairo_version(void)
Returns an integer version number of the cairo library being used */
PHP_FUNCTION(cairo_version)
View
12 cairo_ft_font.c
@@ -187,7 +187,7 @@ PHP_FUNCTION(cairo_ft_font_face_create)
}
if(php_stream_stat(stream, &ssbuf) != 0) {
- zend_error(E_WARNING, "Cannot determine size of stream");
+ zend_error(E_WARNING, "cairo_ft_font_face_create(): Cannot determine size of stream");
if(owned_stream) {
php_stream_close(stream);
}
@@ -199,14 +199,10 @@ PHP_FUNCTION(cairo_ft_font_face_create)
error = php_cairo_create_ft_font_face(font_face_object, stream, owned_stream, load_flags, 0 TSRMLS_CC);
if (error) {
- if (error == FT_Err_Unknown_File_Format) {
- zend_error(E_WARNING, "Unknown file format");
- } else {
- zend_error(E_WARNING, "Error %d occurred opening the file", error);
- }
-
+ char *err_string = php_cairo_get_ft_error(error TSRMLS_CC);
+ zend_error(E_WARNING, "cairo_ft_font_face_create(): An error occurred opening the file: %s", err_string);
RETURN_NULL();
- }
+ }
PHP_CAIRO_ERROR(cairo_font_face_status(font_face_object->font_face));
}
View
18 php_cairo.h
@@ -170,6 +170,7 @@ ZEND_BEGIN_MODULE_GLOBALS(cairo)
FT_Library ft_lib;
ZEND_END_MODULE_GLOBALS(cairo)
+
#ifdef ZTS
# define CAIROG(v) TSRMG(cairo_globals_id, zend_cairo_globals *, v)
#else
@@ -177,6 +178,23 @@ ZEND_END_MODULE_GLOBALS(cairo)
#endif
ZEND_EXTERN_MODULE_GLOBALS(cairo)
+
+
+typedef struct _php_cairo_ft_error {
+ int err_code;
+ const char *err_msg;
+} php_cairo_ft_error;
+
+extern const php_cairo_ft_error php_cairo_ft_errors[];
+
+/* Helper for getting FreeType error strings */
+const char* php_cairo_get_ft_error(int error TSRMLS_DC);
+
+#undef __FTERRORS_H__
+#define FT_ERRORDEF( e, v, s ) { e, s },
+#define FT_ERROR_START_LIST {
+#define FT_ERROR_END_LIST { 0, 0 } };
+
#endif
PHP_MINIT_FUNCTION(cairo_matrix);
View
6 php_cairo_api.h
@@ -63,6 +63,7 @@ typedef struct _cairo_surface_object {
cairo_surface_t *surface;
char * buffer;
stream_closure *closure;
+ zval *parent_zval;
} cairo_surface_object;
typedef struct _cairo_matrix_object {
@@ -110,6 +111,11 @@ extern zend_class_entry* php_cairo_get_path_ce();
extern cairo_font_options_t* php_cairo_font_options_copy(const cairo_font_options_t *);
extern cairo_t * php_cairo_context_reference(cairo_t *context);
+/* Helper for FreeType etc */
+#if defined(CAIRO_HAS_FT_FONT) && defined(HAVE_FREETYPE)
+const char* php_cairo_get_ft_error(int error);
+#endif
+
/* Helpers to make fetching internal objects work right with extended classes */
static inline cairo_context_object* cairo_context_object_get(zval *zobj TSRMLS_DC)
{
View
2 tests/CairoFontFace/CairoFtFontFace/cairo_ft_font_face_create.phpt
@@ -48,5 +48,5 @@ NULL
Warning: cairo_ft_font_face_create expects parameter 1 to be a string or a stream resource in %s on line 21
NULL
-Warning: cairo_ft_font_face_create(): An error occurred opening the file in %s on line 25
+Warning: cairo_ft_font_face_create(): An error occurred opening the file: %s in %s on line 25
NULL

0 comments on commit 167f440

Please sign in to comment.
Something went wrong with that request. Please try again.