[vlc-commits] [Git][videolan/vlc][master] 2 commits: freetype: darwin: fix font index issue

Steve Lhomme (@robUx4) gitlab at videolan.org
Wed Apr 24 12:38:15 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
2e3c0fb6 by Jiepeng Huang at 2024-04-24T12:09:30+00:00
freetype: darwin: fix font index issue

In macOS/iOS/tvOS, font collection file contains multiple fonts.
The 0 index should not be used by default, but the corresponding
index should be used. This will fix some font rendering bugs and
fallback font bugs.

- - - - -
c52066f2 by Jiepeng Huang at 2024-04-24T12:09:30+00:00
freetype: darwin: ignore case whe comparing

The `psz_lcname` is lowercase, so we need to use `strcasecmp`
instead.

- - - - -


1 changed file:

- modules/text_renderer/freetype/fonts/darwin.c


Changes:

=====================================
modules/text_renderer/freetype/fonts/darwin.c
=====================================
@@ -56,7 +56,66 @@ static char* getPathForFontDescription(CTFontDescriptorRef fontDescriptor)
     return retPath;
 }
 
-static void addNewFontToFamily(vlc_font_select_t *fs, CTFontDescriptorRef iter, char *path, vlc_family_t *p_family)
+int getFontIndexInFontFile(const char* psz_filePath, const char* psz_family) {
+    CFStringRef cfFilePath = CFStringCreateWithCString(kCFAllocatorDefault, psz_filePath, kCFStringEncodingUTF8);
+    if (cfFilePath == NULL) {
+        return -1;
+    }
+    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfFilePath, kCFURLPOSIXPathStyle, false);
+    if (url == NULL) {
+        CFRelease(cfFilePath);
+        return -1;
+    }
+    CFArrayRef fontDescriptors = CTFontManagerCreateFontDescriptorsFromURL(url);
+    if (fontDescriptors == NULL) {
+        CFRelease(cfFilePath);
+        CFRelease(url);
+        return -1;
+    }
+    CFIndex numberOfFontDescriptors = CFArrayGetCount(fontDescriptors);
+
+    int index = 0;
+
+    for (CFIndex i = 0; i < numberOfFontDescriptors; i++) {
+        CTFontDescriptorRef descriptor = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fontDescriptors, i);
+        CFStringRef familyName = (CFStringRef)CTFontDescriptorCopyAttribute(descriptor, kCTFontFamilyNameAttribute);
+        CFStringRef fontName = (CFStringRef)CTFontDescriptorCopyAttribute(descriptor, kCTFontNameAttribute);
+        CFStringRef displayName = (CFStringRef)CTFontDescriptorCopyAttribute(descriptor, kCTFontDisplayNameAttribute);
+        char* familyNameStr = FromCFString(familyName, kCFStringEncodingUTF8);
+        char* fontNameStr = FromCFString(fontName, kCFStringEncodingUTF8);
+        char* displayNameStr = FromCFString(displayName, kCFStringEncodingUTF8);
+
+        if (!strcasecmp(familyNameStr, psz_family) || !strcasecmp(fontNameStr, psz_family) || !strcasecmp(displayNameStr, psz_family)) {
+            index = i;
+            FREENULL(familyNameStr);
+            FREENULL(fontNameStr);
+            FREENULL(displayNameStr);
+            CFRelease(familyName);
+            CFRelease(fontName);
+            CFRelease(displayName);
+            break;
+        }
+
+        FREENULL(familyNameStr);
+        FREENULL(fontNameStr);
+        FREENULL(displayNameStr);
+        CFRelease(familyName);
+        CFRelease(fontName);
+        CFRelease(displayName);
+    }
+    if (fontDescriptors != NULL) {
+        CFRelease(fontDescriptors);
+    }
+    if (url != NULL) {
+        CFRelease(url);
+    }
+    if (cfFilePath != NULL) {
+        CFRelease(cfFilePath);
+    }
+    return index;
+}
+
+static void addNewFontToFamily(vlc_font_select_t *fs, CTFontDescriptorRef iter, char *path, vlc_family_t *p_family, int index)
 {
     int i_flags = 0;
     CFDictionaryRef fontTraits = CTFontDescriptorCopyAttribute(iter, kCTFontTraitsAttribute);
@@ -81,7 +140,7 @@ static void addNewFontToFamily(vlc_font_select_t *fs, CTFontDescriptorRef iter,
 #else
     VLC_UNUSED(fs);
 #endif
-    NewFont(path, 0, i_flags, p_family);
+    NewFont(path, index, i_flags, p_family);
 
     CFRelease(fontTraits);
 }
@@ -241,7 +300,14 @@ int CoreText_GetFamily(vlc_font_select_t *fs, const char *psz_lcname,
             continue;
         }
 
-        addNewFontToFamily(fs, iter, path, p_family);
+        /* get the index of the font family in the font file */
+        int fontIndex = getFontIndexInFontFile(path, psz_lcname);
+        if (fontIndex < 0) {
+            FREENULL(path);
+            continue;
+        }
+
+        addNewFontToFamily(fs, iter, path, p_family, fontIndex);
     }
 
     i_ret = VLC_SUCCESS;
@@ -328,7 +394,13 @@ int CoreText_GetFallbacks(vlc_font_select_t *fs, const char *psz_lcname,
         goto done;
     }
 
-    addNewFontToFamily(fs, fallbackFontDescriptor, strdup(psz_fontPath), p_family);
+    /* get the index of the font family in the font file */
+    int fontIndex = getFontIndexInFontFile(psz_fontPath, psz_fallbackFamilyName);
+    if (fontIndex < 0) {
+        goto done;
+    }
+
+    addNewFontToFamily(fs, fallbackFontDescriptor, strdup(psz_fontPath), p_family, fontIndex);
 
     i_ret = VLC_SUCCESS;
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/9226c3dcb5afcf61100ae854028a2d220b0d2384...c52066f20458165436ff9c8831869fa783d12a30

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/9226c3dcb5afcf61100ae854028a2d220b0d2384...c52066f20458165436ff9c8831869fa783d12a30
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list