[vlc-commits] freetype: sort coretext match results

Francois Cartegnie git at videolan.org
Tue Aug 11 13:30:38 CEST 2020


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Thu Aug  6 17:34:09 2020 +0200| [ed731fc6440ee47c6d002759383c928f2676ef23] | committer: Francois Cartegnie

freetype: sort coretext match results

as we're dumb to select only from bold & italic properties

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

 modules/text_renderer/freetype/fonts/darwin.c | 44 ++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/modules/text_renderer/freetype/fonts/darwin.c b/modules/text_renderer/freetype/fonts/darwin.c
index cc2a130ad5..646853c9d6 100644
--- a/modules/text_renderer/freetype/fonts/darwin.c
+++ b/modules/text_renderer/freetype/fonts/darwin.c
@@ -118,6 +118,47 @@ static const char *CoreText_TranslateGenericFamily(const char *psz_family)
     return psz_family;
 }
 
+static float ScoreCTFontMatchResults(CTFontDescriptorRef desc)
+{
+    float score = 0.0;
+    float value;
+    CFDictionaryRef fontTraits = CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
+    if(fontTraits)
+    {
+        CFNumberRef trait = CFDictionaryGetValue(fontTraits, kCTFontWeightTrait);
+        if(trait)
+        {
+            CFNumberGetValue(trait, kCFNumberFloatType, &value);
+            if(value < 0)
+                score -= value;
+            else if(value > 0.23)
+                score += value - 0.23;
+        }
+
+        trait = CFDictionaryGetValue(fontTraits, kCTFontWidthTrait);
+        if(trait)
+        {
+            CFNumberGetValue(trait, kCFNumberFloatType, &value);
+            score += fabs(value);
+        }
+
+        CFRelease(fontTraits);
+    }
+    return score;
+}
+
+static CFComparisonResult SortCTFontMatchResults(CTFontDescriptorRef desc1,
+                                                 CTFontDescriptorRef desc2, void *ctx)
+{
+    VLC_UNUSED(ctx);
+    float score1 = ScoreCTFontMatchResults(desc1);
+    float score2 = ScoreCTFontMatchResults(desc2);
+    if(score1 <= score2)
+        return (score1 == score2) ? kCFCompareEqualTo : kCFCompareLessThan;
+    else
+        return kCFCompareGreaterThan;
+}
+
 const vlc_family_t *CoreText_GetFamily(vlc_font_select_t *fs, const char *psz_family)
 {
     if (unlikely(psz_family == NULL)) {
@@ -174,7 +215,8 @@ const vlc_family_t *CoreText_GetFamily(vlc_font_select_t *fs, const char *psz_fa
         goto end;
     }
 
-    matchedFontDescriptions = CTFontCollectionCreateMatchingFontDescriptors(coreTextFontCollection);
+    matchedFontDescriptions = CTFontCollectionCreateMatchingFontDescriptorsSortedWithCallback(
+          coreTextFontCollection, SortCTFontMatchResults, fs);
     if (matchedFontDescriptions == NULL) {
         msg_Warn(fs->p_obj, "CTFontCollectionCreateMatchingFontDescriptors (2) failed!");
         goto end;



More information about the vlc-commits mailing list