[HarfBuzz] [PATCH] Avoid font fallback with CoreText shaper

Khaled Hosny khaledhosny at eglug.org
Mon Nov 25 05:37:25 PST 2013


CoreText does automatic font fallback (AKA "cascading") for  characters
not supported by the requested font, and provides no way to turn it off,
so detect if the returned run uses a font other than the requested one
and fill in the buffer with .notdef glyphs instead of random indices
glyph from a different font.
---
 src/hb-coretext.cc | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/hb-coretext.cc b/src/hb-coretext.cc
index ba80136..398b866 100644
--- a/src/hb-coretext.cc
+++ b/src/hb-coretext.cc
@@ -685,6 +685,33 @@ _hb_coretext_shape (hb_shape_plan_t    *shape_plan,
 
 #undef ALLOCATE_ARRAY
 
+    /* CoreText does automatic font fallback (AKA "cascading") for  characters
+     * not supported by the requested font, and provides no way to turn it off,
+     * so we detect if the returned run uses a font other than the requested
+     * one and fill in the buffer with .notdef glyphs instead of random glyph
+     * indices from a different font.
+     */
+    CFDictionaryRef attributes = CTRunGetAttributes(run);
+    if (!CFEqual(CFDictionaryGetValue(attributes, kCTFontAttributeName), font_data->ct_font)) {
+        for (unsigned int j = 0; j < num_glyphs; j++) {
+            CGGlyph notdef = 0;
+            double advance = CTFontGetAdvancesForGlyphs(font_data->ct_font, kCTFontHorizontalOrientation, &notdef, NULL, 1);
+
+            hb_glyph_info_t *info = &buffer->info[buffer->len];
+
+            info->codepoint = notdef;
+            info->cluster = string_indices[0] + j;
+
+            info->mask = advance;
+            info->var1.u32 = 0;
+            info->var2.u32 = 0;
+
+            buffer->len++;
+        }
+        continue;
+    }
+
     double run_width = CTRunGetTypographicBounds (run, range_all, NULL, NULL, NULL);
 
     for (unsigned int j = 0; j < num_glyphs; j++) {
-- 
1.8.4.1




More information about the HarfBuzz mailing list