From 38ada127bfb53911ecd64ced26fd23ec67138b43 Mon Sep 17 00:00:00 2001 From: Kristian Rietveld Date: Sat, 28 Jan 2012 10:34:52 +0100 Subject: Make CoreText backend more robust against broken fonts Patch written in collaboration with Michael Natterer. Some CTFontDescriptors apparently do not have a style name or font family name set. This patch makes the code take such corner cases into account. The font family problem only appears to occur on Snow Leopard systems, we try to fall back on the font name (postscript name), if that fails, we fall back on a default fallback. In some cases a coverage is also not set. (This seems to happen when we cannot map a broken font back to a correct font descriptor). In such cases we simply return an empty PangoCoverage, which will likely cause the engine to fallback on a different font. --- pango/pangocoretext-fontmap.c | 71 ++++++++++++++++++++++++++++++++++++------- pango/pangocoretext.c | 4 +++ 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/pango/pangocoretext-fontmap.c b/pango/pangocoretext-fontmap.c index 511380d4..687c3404 100644 --- a/pango/pangocoretext-fontmap.c +++ b/pango/pangocoretext-fontmap.c @@ -166,6 +166,54 @@ gchar_from_cf_string (CFStringRef str) return buffer; } +static char * +ct_font_descriptor_get_family_name (CTFontDescriptorRef desc) +{ + CFStringRef cf_str; + char *buffer; + + cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontFamilyNameAttribute); + if (!cf_str) + { + int i; + + /* No font family name is set, try to retrieve font name and deduce + * the family name from that instead. + */ + cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontNameAttribute); + if (!cf_str) + { + /* This font is likely broken, return a default family name ... */ + return g_strdup ("Sans"); + } + + buffer = gchar_from_cf_string (cf_str); + CFRelease (cf_str); + + for (i = 0; i < strlen (buffer); i++) + if (buffer[i] == '-') + break; + + if (i < strlen (buffer)) + { + char *ret; + + ret = g_strndup (buffer, i); + g_free (buffer); + + return ret; + } + else + return buffer; + } + /* else */ + + buffer = gchar_from_cf_string (cf_str); + CFRelease (cf_str); + + return buffer; +} + static char * ct_font_descriptor_get_style_name (CTFontDescriptorRef desc) { @@ -173,6 +221,9 @@ ct_font_descriptor_get_style_name (CTFontDescriptorRef desc) char *buffer; cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontStyleNameAttribute); + if (!cf_str) + return NULL; + buffer = gchar_from_cf_string (cf_str); CFRelease (cf_str); @@ -271,6 +322,9 @@ ct_font_descriptor_get_weight (CTFontDescriptorRef desc) static inline gboolean pango_core_text_style_name_is_oblique (const char *style_name) { + if (!style_name) + return FALSE; + return g_strrstr (style_name, "Oblique") != NULL; } @@ -278,25 +332,20 @@ PangoFontDescription * _pango_core_text_font_description_from_ct_font_descriptor (CTFontDescriptorRef desc) { SInt64 font_traits; - char *buffer; + char *family_name; char *style_name; - CFStringRef cf_str; PangoFontDescription *font_desc; font_desc = pango_font_description_new (); /* Family name */ - /* FIXME: Should we actually retrieve the family name from the list of families - * in a font map? + /* FIXME: Should we actually retrieve the family name from the list of + * families in a font map? */ - cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontFamilyNameAttribute); - buffer = gchar_from_cf_string (cf_str); - - pango_font_description_set_family (font_desc, buffer); - - g_free (buffer); - CFRelease (cf_str); + family_name = ct_font_descriptor_get_family_name (desc); + pango_font_description_set_family (font_desc, family_name); + g_free (family_name); /* Weight */ pango_font_description_set_weight (font_desc, diff --git a/pango/pangocoretext.c b/pango/pangocoretext.c index 5023e934..ee743935 100644 --- a/pango/pangocoretext.c +++ b/pango/pangocoretext.c @@ -80,6 +80,10 @@ ct_font_descriptor_get_coverage (CTFontDescriptorRef desc) coverage = pango_coverage_new (); charset = CTFontDescriptorCopyAttribute (desc, kCTFontCharacterSetAttribute); + if (!charset) + /* Return an empty coverage */ + return coverage; + bitmap = CFCharacterSetCreateBitmapRepresentation (kCFAllocatorDefault, charset); -- cgit v1.2.1