summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrea Canciani <ranma42@gmail.com>2011-07-15 10:47:56 +0200
committerAndrea Canciani <ranma42@gmail.com>2011-08-14 16:06:31 +0200
commit889d027d9cbe6e05f957f8580ac69d47c4f54dd1 (patch)
treec9657318c8227ad793ff3969da2561b14f15b080
parent79bd4df84c9158759e6a060de2b587479017d3c5 (diff)
font-face: Do not reference error font faces directly
Introduce the function _cairo_font_options_create_in_error(). This makes it possible to observe the creation of cairo_font_option_t error objects by breaking on it and makes the code cleaner by passing around the status obtained from _cairo_error() instead of throwing the error.
-rw-r--r--src/cairo-default-context.c6
-rw-r--r--src/cairo-font-face-private.h3
-rw-r--r--src/cairo-font-face-twin.c2
-rw-r--r--src/cairo-font-face.c54
-rw-r--r--src/cairo-ft-font.c65
-rw-r--r--src/cairo-quartz-font.c7
-rw-r--r--src/cairo-scaled-font.c2
-rw-r--r--src/cairo-toy-font-face.c49
-rw-r--r--src/cairo-user-font.c6
-rw-r--r--src/cairo-win32-font.c12
-rw-r--r--src/cairo.c2
11 files changed, 106 insertions, 102 deletions
diff --git a/src/cairo-default-context.c b/src/cairo-default-context.c
index 28b268734..bd9994193 100644
--- a/src/cairo-default-context.c
+++ b/src/cairo-default-context.c
@@ -1129,10 +1129,8 @@ _cairo_default_context_get_font_face (void *abstract_cr)
cairo_status_t status;
status = _cairo_gstate_get_font_face (cr->gstate, &font_face);
- if (unlikely (status)) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *) &_cairo_font_face_nil;
- }
+ if (unlikely (status))
+ return _cairo_font_face_create_in_error (status);
return font_face;
}
diff --git a/src/cairo-font-face-private.h b/src/cairo-font-face-private.h
index ebbec7273..2b0805c46 100644
--- a/src/cairo-font-face-private.h
+++ b/src/cairo-font-face-private.h
@@ -65,7 +65,8 @@ struct _cairo_toy_font_face {
cairo_font_face_t *impl_face; /* The non-toy font face this actually uses */
};
-extern const cairo_private cairo_font_face_t _cairo_font_face_nil;
+cairo_private cairo_font_face_t *
+_cairo_font_face_create_in_error (cairo_status_t status);
cairo_private void
_cairo_font_face_init (cairo_font_face_t *font_face,
diff --git a/src/cairo-font-face-twin.c b/src/cairo-font-face-twin.c
index 606a88a18..c914c3009 100644
--- a/src/cairo-font-face-twin.c
+++ b/src/cairo-font-face-twin.c
@@ -727,7 +727,7 @@ _cairo_font_face_twin_create_fallback (void)
twin_font_face = _cairo_font_face_twin_create_internal ();
if (! twin_font_face_create_properties (twin_font_face)) {
cairo_font_face_destroy (twin_font_face);
- return (cairo_font_face_t *) &_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
return twin_font_face;
diff --git a/src/cairo-font-face.c b/src/cairo-font-face.c
index 1f80aa7ea..9c3a9c335 100644
--- a/src/cairo-font-face.c
+++ b/src/cairo-font-face.c
@@ -61,7 +61,7 @@
/* #cairo_font_face_t */
-const cairo_font_face_t _cairo_font_face_nil = {
+static const cairo_font_face_t _cairo_font_face_nil = {
{ 0 }, /* hash_entry */
CAIRO_STATUS_NO_MEMORY, /* status */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
@@ -69,6 +69,58 @@ const cairo_font_face_t _cairo_font_face_nil = {
NULL
};
+static const cairo_font_face_t _cairo_font_face_null_pointer = {
+ { 0 }, /* hash_entry */
+ CAIRO_STATUS_NULL_POINTER, /* status */
+ CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
+ { 0, 0, 0, NULL }, /* user_data */
+ NULL
+};
+
+static const cairo_font_face_t _cairo_font_face_invalid_string = {
+ { 0 }, /* hash_entry */
+ CAIRO_STATUS_INVALID_STRING, /* status */
+ CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
+ { 0, 0, 0, NULL }, /* user_data */
+ NULL
+};
+
+static const cairo_font_face_t _cairo_font_face_invalid_slant = {
+ { 0 }, /* hash_entry */
+ CAIRO_STATUS_INVALID_SLANT, /* status */
+ CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
+ { 0, 0, 0, NULL }, /* user_data */
+ NULL
+};
+
+static const cairo_font_face_t _cairo_font_face_invalid_weight = {
+ { 0 }, /* hash_entry */
+ CAIRO_STATUS_INVALID_WEIGHT, /* status */
+ CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
+ { 0, 0, 0, NULL }, /* user_data */
+ NULL
+};
+
+cairo_font_face_t *
+_cairo_font_face_create_in_error (cairo_status_t status)
+{
+ assert (status != CAIRO_STATUS_SUCCESS);
+
+ if (status == CAIRO_STATUS_NO_MEMORY)
+ return (cairo_font_face_t *) &_cairo_font_face_nil;
+ else if (status == CAIRO_STATUS_NULL_POINTER)
+ return (cairo_font_face_t *) &_cairo_font_face_null_pointer;
+ else if (status == CAIRO_STATUS_INVALID_STRING)
+ return (cairo_font_face_t *) &_cairo_font_face_invalid_string;
+ else if (status == CAIRO_STATUS_INVALID_SLANT)
+ return (cairo_font_face_t *) &_cairo_font_face_invalid_slant;
+ else if (status == CAIRO_STATUS_INVALID_WEIGHT)
+ return (cairo_font_face_t *) &_cairo_font_face_invalid_weight;
+
+ /* XXX use shared error objects */
+ return (cairo_font_face_t *) &_cairo_font_face_nil;
+}
+
cairo_status_t
_cairo_font_face_set_error (cairo_font_face_t *font_face,
cairo_status_t status)
diff --git a/src/cairo-ft-font.c b/src/cairo-ft-font.c
index c79e0a606..6bdf8aeda 100644
--- a/src/cairo-ft-font.c
+++ b/src/cairo-ft-font.c
@@ -2656,21 +2656,21 @@ static cairo_status_t
_cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
cairo_font_face_t **font_face_out)
{
- cairo_font_face_t *font_face = (cairo_font_face_t *) &_cairo_font_face_nil;
+ cairo_font_face_t *font_face;
FcPattern *pattern;
int fcslant;
int fcweight;
pattern = FcPatternCreate ();
- if (!pattern) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
+ if (unlikely (pattern == NULL)) {
+ font_face = _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
return font_face->status;
}
if (!FcPatternAddString (pattern,
FC_FAMILY, (unsigned char *) toy_face->family))
{
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
+ font_face = _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
goto FREE_PATTERN;
}
@@ -2689,7 +2689,7 @@ _cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
}
if (!FcPatternAddInteger (pattern, FC_SLANT, fcslant)) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
+ font_face = _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
goto FREE_PATTERN;
}
@@ -2705,7 +2705,7 @@ _cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
}
if (!FcPatternAddInteger (pattern, FC_WEIGHT, fcweight)) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
+ font_face = _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
goto FREE_PATTERN;
}
@@ -2812,10 +2812,8 @@ _cairo_ft_font_face_get_implementation (void *abstract_face,
/* Cache the resolved font whilst the FcConfig remains consistent. */
resolved = font_face->resolved_font_face;
if (resolved != NULL) {
- if (! FcInitBringUptoDate ()) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *) &_cairo_font_face_nil;
- }
+ if (unlikely (! FcInitBringUptoDate ()))
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
if (font_face->resolved_config == FcConfigGetCurrent ())
return cairo_font_face_reference (resolved);
@@ -2860,10 +2858,8 @@ _cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
cairo_ft_font_face_t *font_face;
font_face = malloc (sizeof (cairo_ft_font_face_t));
- if (unlikely (font_face == NULL)) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *) &_cairo_font_face_nil;
- }
+ if (unlikely (font_face == NULL))
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
font_face->unscaled = NULL;
font_face->next = NULL;
@@ -2871,8 +2867,7 @@ _cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
font_face->pattern = FcPatternDuplicate (pattern);
if (unlikely (font_face->pattern == NULL)) {
free (font_face);
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *) &_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
font_face->resolved_font_face = NULL;
@@ -2919,10 +2914,8 @@ _cairo_ft_font_face_create (cairo_ft_unscaled_font_t *unscaled,
/* No match found, create a new one */
font_face = malloc (sizeof (cairo_ft_font_face_t));
- if (unlikely (!font_face)) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *)&_cairo_font_face_nil;
- }
+ if (unlikely (font_face == NULL))
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
font_face->unscaled = unscaled;
_cairo_unscaled_font_reference (&unscaled->base);
@@ -3112,25 +3105,25 @@ _cairo_ft_resolve_pattern (FcPattern *pattern,
status = _compute_transform (&sf, &scale);
if (unlikely (status))
- return (cairo_font_face_t *)&_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (status);
pattern = FcPatternDuplicate (pattern);
- if (pattern == NULL)
- return (cairo_font_face_t *)&_cairo_font_face_nil;
+ if (unlikely (pattern == NULL))
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
- if (! FcPatternAddDouble (pattern, FC_PIXEL_SIZE, sf.y_scale)) {
- font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
+ if (unlikely (! FcPatternAddDouble (pattern, FC_PIXEL_SIZE, sf.y_scale))) {
+ font_face = _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
goto FREE_PATTERN;
}
- if (! FcConfigSubstitute (NULL, pattern, FcMatchPattern)) {
- font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
+ if (unlikely (! FcConfigSubstitute (NULL, pattern, FcMatchPattern))) {
+ font_face = _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
goto FREE_PATTERN;
}
status = _cairo_ft_font_options_substitute (font_options, pattern);
- if (status) {
- font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
+ if (unlikely (status)) {
+ font_face = _cairo_font_face_create_in_error (status);
goto FREE_PATTERN;
}
@@ -3138,7 +3131,7 @@ _cairo_ft_resolve_pattern (FcPattern *pattern,
status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
if (unlikely (status)) {
- font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
+ font_face = _cairo_font_face_create_in_error (status);
goto FREE_PATTERN;
}
@@ -3154,8 +3147,12 @@ _cairo_ft_resolve_pattern (FcPattern *pattern,
}
status = _cairo_ft_unscaled_font_create_for_pattern (resolved, &unscaled);
- if (unlikely (status || unscaled == NULL)) {
- font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
+ if (unlikely (status)) {
+ font_face = _cairo_font_face_create_in_error (status);
+ goto FREE_RESOLVED;
+ }
+ if (unlikely (unscaled == NULL)) {
+ font_face = _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NULL_POINTER));
goto FREE_RESOLVED;
}
} else
@@ -3222,7 +3219,7 @@ cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
if (unlikely (status))
- return (cairo_font_face_t *) &_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (status);
if (unlikely (unscaled == NULL)) {
/* Store the pattern. We will resolve it and create unscaled
* font when creating scaled fonts */
@@ -3294,7 +3291,7 @@ cairo_ft_font_face_create_for_ft_face (FT_Face face,
status = _cairo_ft_unscaled_font_create_from_face (face, &unscaled);
if (unlikely (status))
- return (cairo_font_face_t *)&_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (status);
ft_options.load_flags = load_flags;
ft_options.extra_flags = 0;
diff --git a/src/cairo-quartz-font.c b/src/cairo-quartz-font.c
index 32d9bfea9..8beb225ee 100644
--- a/src/cairo-quartz-font.c
+++ b/src/cairo-quartz-font.c
@@ -355,11 +355,8 @@ cairo_quartz_font_face_create_for_cgfont (CGFontRef font)
quartz_font_ensure_symbols();
font_face = malloc (sizeof (cairo_quartz_font_face_t));
- if (!font_face) {
- cairo_status_t ignore_status;
- ignore_status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *)&_cairo_font_face_nil;
- }
+ if (unlikely (font_face == NULL))
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
font_face->cgFont = CGFontRetain (font);
diff --git a/src/cairo-scaled-font.c b/src/cairo-scaled-font.c
index 0bc23f56b..b0afb0995 100644
--- a/src/cairo-scaled-font.c
+++ b/src/cairo-scaled-font.c
@@ -2914,7 +2914,7 @@ cairo_font_face_t *
cairo_scaled_font_get_font_face (cairo_scaled_font_t *scaled_font)
{
if (scaled_font->status)
- return (cairo_font_face_t*) &_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (scaled_font->status);
if (scaled_font->original_font_face != NULL)
return scaled_font->original_font_face;
diff --git a/src/cairo-toy-font-face.c b/src/cairo-toy-font-face.c
index 54683a37f..9e01039d8 100644
--- a/src/cairo-toy-font-face.c
+++ b/src/cairo-toy-font-face.c
@@ -44,39 +44,6 @@
#include "cairo-error-private.h"
#include "cairo-font-face-private.h"
-static const cairo_font_face_t _cairo_font_face_null_pointer = {
- { 0 }, /* hash_entry */
- CAIRO_STATUS_NULL_POINTER, /* status */
- CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
- { 0, 0, 0, NULL }, /* user_data */
- NULL
-};
-
-static const cairo_font_face_t _cairo_font_face_invalid_string = {
- { 0 }, /* hash_entry */
- CAIRO_STATUS_INVALID_STRING, /* status */
- CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
- { 0, 0, 0, NULL }, /* user_data */
- NULL
-};
-
-static const cairo_font_face_t _cairo_font_face_invalid_slant = {
- { 0 }, /* hash_entry */
- CAIRO_STATUS_INVALID_SLANT, /* status */
- CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
- { 0, 0, 0, NULL }, /* user_data */
- NULL
-};
-
-static const cairo_font_face_t _cairo_font_face_invalid_weight = {
- { 0 }, /* hash_entry */
- CAIRO_STATUS_INVALID_WEIGHT, /* status */
- CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
- { 0, 0, 0, NULL }, /* user_data */
- NULL
-};
-
-
static const cairo_font_face_backend_t _cairo_toy_font_face_backend;
static int
@@ -260,16 +227,12 @@ cairo_toy_font_face_create (const char *family,
cairo_hash_table_t *hash_table;
if (family == NULL)
- return (cairo_font_face_t*) &_cairo_font_face_null_pointer;
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NULL_POINTER));
/* Make sure we've got valid UTF-8 for the family */
status = _cairo_utf8_to_ucs4 (family, -1, NULL, NULL);
- if (unlikely (status)) {
- if (status == CAIRO_STATUS_INVALID_STRING)
- return (cairo_font_face_t*) &_cairo_font_face_invalid_string;
-
- return (cairo_font_face_t*) &_cairo_font_face_nil;
- }
+ if (unlikely (status))
+ return _cairo_font_face_create_in_error (status);
switch (slant) {
case CAIRO_FONT_SLANT_NORMAL:
@@ -277,7 +240,7 @@ cairo_toy_font_face_create (const char *family,
case CAIRO_FONT_SLANT_OBLIQUE:
break;
default:
- return (cairo_font_face_t*) &_cairo_font_face_invalid_slant;
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SLANT));
}
switch (weight) {
@@ -285,7 +248,7 @@ cairo_toy_font_face_create (const char *family,
case CAIRO_FONT_WEIGHT_BOLD:
break;
default:
- return (cairo_font_face_t*) &_cairo_font_face_invalid_weight;
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_WEIGHT));
}
if (*family == '\0')
@@ -338,7 +301,7 @@ cairo_toy_font_face_create (const char *family,
UNWIND_HASH_TABLE_LOCK:
_cairo_toy_font_face_hash_table_unlock ();
UNWIND:
- return (cairo_font_face_t*) &_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (status);
}
slim_hidden_def (cairo_toy_font_face_create);
diff --git a/src/cairo-user-font.c b/src/cairo-user-font.c
index 7015eae6b..49c41ad71 100644
--- a/src/cairo-user-font.c
+++ b/src/cairo-user-font.c
@@ -543,10 +543,8 @@ cairo_user_font_face_create (void)
cairo_user_font_face_t *font_face;
font_face = malloc (sizeof (cairo_user_font_face_t));
- if (!font_face) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *)&_cairo_font_face_nil;
- }
+ if (unlikely (font_face == NULL))
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
_cairo_font_face_init (&font_face->base, &_cairo_user_font_face_backend);
diff --git a/src/cairo-win32-font.c b/src/cairo-win32-font.c
index 866898048..10467a224 100644
--- a/src/cairo-win32-font.c
+++ b/src/cairo-win32-font.c
@@ -2178,10 +2178,8 @@ cairo_win32_font_face_create_for_logfontw_hfont (LOGFONTW *logfont, HFONT font)
cairo_status_t status;
hash_table = _cairo_win32_font_face_hash_table_lock ();
- if (unlikely (hash_table == NULL)) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
- return (cairo_font_face_t *)&_cairo_font_face_nil;
- }
+ if (unlikely (hash_table == NULL))
+ return _cairo_font_face_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
_cairo_win32_font_face_init_key (&key, logfont, font);
@@ -2201,8 +2199,8 @@ cairo_win32_font_face_create_for_logfontw_hfont (LOGFONTW *logfont, HFONT font)
/* Otherwise create it and insert into hash table. */
font_face = malloc (sizeof (cairo_win32_font_face_t));
- if (!font_face) {
- _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
+ if (unlikely (font_face == NULL)) {
+ status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto FAIL;
}
@@ -2223,7 +2221,7 @@ DONE:
FAIL:
_cairo_win32_font_face_hash_table_unlock ();
- return (cairo_font_face_t *)&_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (status);
}
/**
diff --git a/src/cairo.c b/src/cairo.c
index 4b034f447..6a7766cef 100644
--- a/src/cairo.c
+++ b/src/cairo.c
@@ -2687,7 +2687,7 @@ cairo_font_face_t *
cairo_get_font_face (cairo_t *cr)
{
if (unlikely (cr->status))
- return (cairo_font_face_t*) &_cairo_font_face_nil;
+ return _cairo_font_face_create_in_error (cr->status);
return cr->backend->get_font_face (cr);
}