[vlc-commits] freetype: darwin: Fix CoreText_GetFallbacks

Marvin Scholz git at videolan.org
Tue Jun 19 00:23:36 CEST 2018


vlc | branch: master | Marvin Scholz <epirat07 at gmail.com> | Tue Jun 19 00:23:08 2018 +0200| [70caceceb657a38861835876854707a71db3370b] | committer: Marvin Scholz

freetype: darwin: Fix CoreText_GetFallbacks

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

 modules/text_renderer/freetype/fonts/darwin.c | 38 +++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/modules/text_renderer/freetype/fonts/darwin.c b/modules/text_renderer/freetype/fonts/darwin.c
index b460afe72f..9e031fc6ee 100644
--- a/modules/text_renderer/freetype/fonts/darwin.c
+++ b/modules/text_renderer/freetype/fonts/darwin.c
@@ -200,6 +200,39 @@ end:
     return p_family;
 }
 
+/* Obtains a copy of the contents of a CFString in ASCII encoding.
+ * Returns char* (must be freed by caller) or NULL on failure.
+ */
+static char* CFStringCopyUTF8CString(CFStringRef cfString)
+{
+    // Try the quick way to obtain the buffer
+    const char *tmpBuffer = CFStringGetCStringPtr(cfString, kCFStringEncodingUTF8);
+
+    if (tmpBuffer != NULL) {
+       return strdup(tmpBuffer);
+    }
+
+    // The quick way did not work, try the long way
+    CFIndex length = CFStringGetLength(cfString);
+    CFIndex maxSize =
+        CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
+
+    // If result would exceed LONG_MAX, kCFNotFound is returned
+    if (unlikely(maxSize == kCFNotFound)) {
+        return NULL;
+    }
+
+    // Account for the null terminator
+    maxSize++;
+
+    char *buffer = (char *)malloc(maxSize);
+    Boolean success = CFStringGetCString(cfString, buffer, maxSize, kCFStringEncodingUTF8);
+
+    if (!success)
+        FREENULL(buffer);
+    return buffer;
+}
+
 vlc_family_t *CoreText_GetFallbacks(filter_t *p_filter, const char *psz_family, uni_char_t codepoint)
 {
     filter_sys_t *p_sys = p_filter->p_sys;
@@ -227,15 +260,16 @@ vlc_family_t *CoreText_GetFallbacks(filter_t *p_filter, const char *psz_family,
     CFStringRef fallbackFontFamilyName = CTFontCopyFamilyName(fallbackFont);
 
     /* create a new family object */
-    const char *psz_fallbackFamilyName = CFStringGetCStringPtr(fallbackFontFamilyName, kCFStringEncodingUTF8);
+    char *psz_fallbackFamilyName = CFStringCopyUTF8CString(fallbackFontFamilyName);
     if (psz_fallbackFamilyName == NULL) {
+        msg_Warn(p_filter, "Failed to convert font family name CFString to C string");
         goto done;
     }
 #ifndef NDEBUG
     msg_Dbg(p_filter, "Will deploy fallback font '%s'", psz_fallbackFamilyName);
 #endif
 
-    psz_lc_fallback = ToLower(strdup(psz_fallbackFamilyName));
+    psz_lc_fallback = ToLower(psz_fallbackFamilyName);
 
     p_family = vlc_dictionary_value_for_key(&p_sys->family_map, psz_lc_fallback);
     if (p_family) {



More information about the vlc-commits mailing list