diff options
author | Andrea Canciani <ranma42@gmail.com> | 2017-03-20 07:20:03 +0100 |
---|---|---|
committer | Andrea Canciani <ranma42@gmail.com> | 2017-03-20 08:25:37 +0100 |
commit | 3349460fdda4093c755a7d37e04b9fb19df28850 (patch) | |
tree | 311ca07e0ed5452dce04f7ec44a79567d85607e4 | |
parent | 80fee754d95a44075a0da1c01cef0a8780f6e844 (diff) |
cleanupwip/quartz-font-wanna-ct
-rw-r--r-- | src/cairo-quartz-font.c | 174 |
1 files changed, 83 insertions, 91 deletions
diff --git a/src/cairo-quartz-font.c b/src/cairo-quartz-font.c index 1e9a9181e..3374d696c 100644 --- a/src/cairo-quartz-font.c +++ b/src/cairo-quartz-font.c @@ -113,30 +113,30 @@ static cairo_bool_t _cairo_quartz_font_symbols_present = FALSE; #define CGGLYPH_INVALID ((CGGlyph) 0xFFFF) /* kCGFontIndexInvalid */ static void -quartz_font_ensure_symbols(void) +quartz_font_ensure_symbols (void) { - if (_cairo_quartz_font_symbol_lookup_done) + if (likely (_cairo_quartz_font_symbol_lookup_done)) return; - CGFontCopyTableForTagPtr = dlsym(RTLD_DEFAULT, "CGFontCopyTableForTag"); + CGFontCopyTableForTagPtr = dlsym (RTLD_DEFAULT, "CGFontCopyTableForTag"); /* Look for the 10.5 versions first */ - CGFontGetGlyphBBoxesPtr = dlsym(RTLD_DEFAULT, "CGFontGetGlyphBBoxes"); + CGFontGetGlyphBBoxesPtr = dlsym (RTLD_DEFAULT, "CGFontGetGlyphBBoxes"); if (!CGFontGetGlyphBBoxesPtr) - CGFontGetGlyphBBoxesPtr = dlsym(RTLD_DEFAULT, "CGFontGetGlyphBoundingBoxes"); + CGFontGetGlyphBBoxesPtr = dlsym (RTLD_DEFAULT, "CGFontGetGlyphBoundingBoxes"); - CGFontGetGlyphsForUnicharsPtr = dlsym(RTLD_DEFAULT, "CGFontGetGlyphsForUnichars"); + CGFontGetGlyphsForUnicharsPtr = dlsym (RTLD_DEFAULT, "CGFontGetGlyphsForUnichars"); if (!CGFontGetGlyphsForUnicharsPtr) - CGFontGetGlyphsForUnicharsPtr = dlsym(RTLD_DEFAULT, "CGFontGetGlyphsForUnicodes"); + CGFontGetGlyphsForUnicharsPtr = dlsym (RTLD_DEFAULT, "CGFontGetGlyphsForUnicodes"); - CGFontGetFontBBoxPtr = dlsym(RTLD_DEFAULT, "CGFontGetFontBBox"); + CGFontGetFontBBoxPtr = dlsym (RTLD_DEFAULT, "CGFontGetFontBBox"); /* We just need one of these two */ - CGFontCreateWithFontNamePtr = dlsym(RTLD_DEFAULT, "CGFontCreateWithFontName"); - CGFontCreateWithNamePtr = dlsym(RTLD_DEFAULT, "CGFontCreateWithName"); + CGFontCreateWithFontNamePtr = dlsym (RTLD_DEFAULT, "CGFontCreateWithFontName"); + CGFontCreateWithNamePtr = dlsym (RTLD_DEFAULT, "CGFontCreateWithName"); /* These have the same name in 10.4 and 10.5 */ - CGFontGetUnitsPerEmPtr = dlsym(RTLD_DEFAULT, "CGFontGetUnitsPerEm"); + CGFontGetUnitsPerEmPtr = dlsym (RTLD_DEFAULT, "CGFontGetUnitsPerEm"); CGFontGetGlyphAdvancesPtr = dlsym(RTLD_DEFAULT, "CGFontGetGlyphAdvances"); /* @@ -147,24 +147,24 @@ quartz_font_ensure_symbols(void) * CoreText library does not provide the CTFontCreatePathForGlyph * symbol, use it to determine whether to use CoreText at all. */ - CTFontCreatePathForGlyphPtr = dlsym(RTLD_DEFAULT, "CTFontCreatePathForGlyph"); + CTFontCreatePathForGlyphPtr = dlsym (RTLD_DEFAULT, "CTFontCreatePathForGlyph"); if (CTFontCreatePathForGlyphPtr) { - CTFontCreateWithGraphicsFontPtr = dlsym(RTLD_DEFAULT, "CTFontCreateWithGraphicsFont"); - CTFontGetAdvancesForGlyphsPtr = dlsym(RTLD_DEFAULT, "CTFontGetAdvancesForGlyphs"); - CTFontGetBoundingRectsForGlyphsPtr = dlsym(RTLD_DEFAULT, "CTFontGetBoundingRectsForGlyphs"); + CTFontCreateWithGraphicsFontPtr = dlsym (RTLD_DEFAULT, "CTFontCreateWithGraphicsFont"); + CTFontGetAdvancesForGlyphsPtr = dlsym (RTLD_DEFAULT, "CTFontGetAdvancesForGlyphs"); + CTFontGetBoundingRectsForGlyphsPtr = dlsym (RTLD_DEFAULT, "CTFontGetBoundingRectsForGlyphs"); } else { - CGFontGetGlyphPathPtr = dlsym(RTLD_DEFAULT, "CGFontGetGlyphPath"); + CGFontGetGlyphPathPtr = dlsym (RTLD_DEFAULT, "CGFontGetGlyphPath"); } - CGFontGetHMetricsPtr = dlsym(RTLD_DEFAULT, "CGFontGetHMetrics"); - CGFontGetAscentPtr = dlsym(RTLD_DEFAULT, "CGFontGetAscent"); - CGFontGetDescentPtr = dlsym(RTLD_DEFAULT, "CGFontGetDescent"); - CGFontGetLeadingPtr = dlsym(RTLD_DEFAULT, "CGFontGetLeading"); + CGFontGetHMetricsPtr = dlsym (RTLD_DEFAULT, "CGFontGetHMetrics"); + CGFontGetAscentPtr = dlsym (RTLD_DEFAULT, "CGFontGetAscent"); + CGFontGetDescentPtr = dlsym (RTLD_DEFAULT, "CGFontGetDescent"); + CGFontGetLeadingPtr = dlsym (RTLD_DEFAULT, "CGFontGetLeading"); - CGContextGetAllowsFontSmoothingPtr = dlsym(RTLD_DEFAULT, "CGContextGetAllowsFontSmoothing"); - CGContextSetAllowsFontSmoothingPtr = dlsym(RTLD_DEFAULT, "CGContextSetAllowsFontSmoothing"); + CGContextGetAllowsFontSmoothingPtr = dlsym (RTLD_DEFAULT, "CGContextGetAllowsFontSmoothing"); + CGContextSetAllowsFontSmoothingPtr = dlsym (RTLD_DEFAULT, "CGContextSetAllowsFontSmoothing"); - FMGetATSFontRefFromFontPtr = dlsym(RTLD_DEFAULT, "FMGetATSFontRefFromFont"); + FMGetATSFontRefFromFontPtr = dlsym (RTLD_DEFAULT, "FMGetATSFontRefFromFont"); if ((CGFontCreateWithFontNamePtr || CGFontCreateWithNamePtr) && CGFontGetGlyphBBoxesPtr && @@ -206,16 +206,16 @@ _cairo_quartz_font_face_create_for_toy (cairo_toy_font_face_t *toy_face, CGFontRef cgFont = NULL; int loop; - quartz_font_ensure_symbols(); + quartz_font_ensure_symbols (); if (! _cairo_quartz_font_symbols_present) return _cairo_error (CAIRO_STATUS_NO_MEMORY); family = toy_face->family; full_name = malloc (strlen (family) + 64); // give us a bit of room to tack on Bold, Oblique, etc. /* handle CSS-ish faces */ - if (!strcmp(family, "serif") || !strcmp(family, "Times Roman")) + if (!strcmp (family, "serif") || !strcmp (family, "Times Roman")) family = "Times"; - else if (!strcmp(family, "sans-serif") || !strcmp(family, "sans")) + else if (!strcmp(family, "sans-serif") || !strcmp (family, "sans")) family = "Helvetica"; else if (!strcmp(family, "cursive")) family = "Apple Chancery"; @@ -272,7 +272,7 @@ _cairo_quartz_font_face_create_for_toy (cairo_toy_font_face_t *toy_face, static cairo_bool_t _cairo_quartz_font_face_destroy (void *abstract_face) { - cairo_quartz_font_face_t *font_face = (cairo_quartz_font_face_t*) abstract_face; + cairo_quartz_font_face_t *font_face = abstract_face; if (font_face->ctFont) CFRelease (font_face->ctFont); @@ -290,74 +290,67 @@ _cairo_quartz_font_face_scaled_font_create (void *abstract_face, const cairo_font_options_t *options, cairo_scaled_font_t **font_out) { + quartz_CGFontMetrics *m, metrics; cairo_quartz_font_face_t *font_face = abstract_face; - cairo_quartz_scaled_font_t *font = NULL; + cairo_quartz_scaled_font_t *font; cairo_status_t status; - cairo_font_extents_t fs_metrics; - double ems; CGRect bbox; - quartz_font_ensure_symbols(); - if (!_cairo_quartz_font_symbols_present) + quartz_font_ensure_symbols (); + if (unlikely (!_cairo_quartz_font_symbols_present)) return _cairo_error (CAIRO_STATUS_NO_MEMORY); - font = malloc(sizeof(cairo_quartz_scaled_font_t)); - if (font == NULL) + font = calloc (1, sizeof (cairo_quartz_scaled_font_t)); + if (unlikely (font == NULL)) return _cairo_error (CAIRO_STATUS_NO_MEMORY); - memset (font, 0, sizeof(cairo_quartz_scaled_font_t)); - status = _cairo_scaled_font_init (&font->base, &font_face->base, font_matrix, ctm, options, &_cairo_quartz_scaled_font_backend); - if (status) - goto FINISH; - ems = CGFontGetUnitsPerEmPtr (font_face->cgFont); + if (unlikely (status != CAIRO_STATUS_SUCCESS)) { + free (font); + return status; + } /* initialize metrics */ - if (CGFontGetFontBBoxPtr && CGFontGetAscentPtr) { - fs_metrics.ascent = (CGFontGetAscentPtr (font_face->cgFont) / ems); - fs_metrics.descent = - (CGFontGetDescentPtr (font_face->cgFont) / ems); - fs_metrics.height = fs_metrics.ascent + fs_metrics.descent + - (CGFontGetLeadingPtr (font_face->cgFont) / ems); - + if (likely (CGFontGetFontBBoxPtr && CGFontGetAscentPtr)) { + m = &metrics; + metrics.ascent = CGFontGetAscentPtr (font_face->cgFont); + metrics.descent = CGFontGetDescentPtr (font_face->cgFont); + metrics.leading = CGFontGetLeadingPtr (font_face->cgFont); bbox = CGFontGetFontBBoxPtr (font_face->cgFont); - fs_metrics.max_x_advance = CGRectGetMaxX(bbox) / ems; - fs_metrics.max_y_advance = 0.0; - } else { - CGGlyph wGlyph; - UniChar u; - - quartz_CGFontMetrics *m; - m = CGFontGetHMetricsPtr (font_face->cgFont); - + } else if (likely ((m = CGFontGetHMetricsPtr (font_face->cgFont)))) { /* On OX 10.4, GetHMetricsPtr sometimes returns NULL for unknown reasons */ - if (!m) { - status = _cairo_error(CAIRO_STATUS_NULL_POINTER); - goto FINISH; - } + /* We kind of have to guess here; W's big, right? */ + CGGlyph wGlyph = 0; + UniChar u = (UniChar) 'W'; - fs_metrics.ascent = (m->ascent / ems); - fs_metrics.descent = - (m->descent / ems); - fs_metrics.height = fs_metrics.ascent + fs_metrics.descent + (m->leading / ems); + bbox.origin.y = bbox.size.height = 0; - /* We kind of have to guess here; W's big, right? */ - u = (UniChar) 'W'; CGFontGetGlyphsForUnicharsPtr (font_face->cgFont, &u, &wGlyph, 1); - if (wGlyph && CGFontGetGlyphBBoxesPtr (font_face->cgFont, &wGlyph, 1, &bbox)) { - fs_metrics.max_x_advance = CGRectGetMaxX(bbox) / ems; - fs_metrics.max_y_advance = 0.0; - } else { - fs_metrics.max_x_advance = 0.0; - fs_metrics.max_y_advance = 0.0; + if (unlikely (wGlyph == 0 || + !CGFontGetGlyphBBoxesPtr (font_face->cgFont, &wGlyph, 1, &bbox))) { + bbox.origin.x = bbox.size.width = 0; } } - status = _cairo_scaled_font_set_metrics (&font->base, &fs_metrics); + if (likely (m != NULL)) { + cairo_font_extents_t fs_metrics; + double ems = CGFontGetUnitsPerEmPtr (font_face->cgFont); -FINISH: - if (status != CAIRO_STATUS_SUCCESS) { + fs_metrics.ascent = m->ascent / ems; + fs_metrics.descent = -m->descent / ems; + fs_metrics.height = fs_metrics.ascent + fs_metrics.descent + m->leading / ems; + fs_metrics.max_x_advance = CGRectGetMaxX(bbox) / ems; + fs_metrics.max_y_advance = 0.0; + + status = _cairo_scaled_font_set_metrics (&font->base, &fs_metrics); + } else { + status = _cairo_error (CAIRO_STATUS_NULL_POINTER); + } + + if (unlikely (status != CAIRO_STATUS_SUCCESS)) { free (font); } else { *font_out = (cairo_scaled_font_t*) font; @@ -370,7 +363,8 @@ const cairo_font_face_backend_t _cairo_quartz_font_face_backend = { CAIRO_FONT_TYPE_QUARTZ, _cairo_quartz_font_face_create_for_toy, _cairo_quartz_font_face_destroy, - _cairo_quartz_font_face_scaled_font_create + _cairo_quartz_font_face_scaled_font_create, + NULL, /* get_implementation */ }; /** @@ -417,10 +411,9 @@ cairo_quartz_font_face_create_for_cgfont (CGFontRef font) */ static cairo_quartz_font_face_t * -_cairo_quartz_scaled_to_face (void *abstract_font) +_cairo_quartz_scaled_to_face (cairo_quartz_scaled_font_t *scaled_font) { - cairo_quartz_scaled_font_t *sfont = (cairo_quartz_scaled_font_t*) abstract_font; - cairo_font_face_t *font_face = sfont->base.font_face; + cairo_font_face_t *font_face = scaled_font->base.font_face; assert (font_face->backend->type == CAIRO_FONT_TYPE_QUARTZ); return (cairo_quartz_font_face_t*) font_face; } @@ -441,8 +434,7 @@ _cairo_quartz_init_glyph_metrics (cairo_quartz_scaled_font_t *font, cairo_scaled_glyph_t *scaled_glyph) { cairo_int_status_t status = CAIRO_STATUS_SUCCESS; - - cairo_quartz_font_face_t *font_face = _cairo_quartz_scaled_to_face(font); + cairo_quartz_font_face_t *font_face = _cairo_quartz_scaled_to_face (font); cairo_text_extents_t extents = {0, 0, 0, 0, 0, 0}; CGGlyph glyph = _cairo_quartz_scaled_glyph_index (scaled_glyph); int advance; @@ -774,17 +766,17 @@ _cairo_quartz_scaled_glyph_init (void *abstract_font, cairo_scaled_glyph_t *scaled_glyph, cairo_scaled_glyph_info_t info) { - cairo_quartz_scaled_font_t *font = (cairo_quartz_scaled_font_t *) abstract_font; + cairo_quartz_scaled_font_t *scaled_font = abstract_font; cairo_int_status_t status = CAIRO_STATUS_SUCCESS; if (!status && (info & CAIRO_SCALED_GLYPH_INFO_METRICS)) - status = _cairo_quartz_init_glyph_metrics (font, scaled_glyph); + status = _cairo_quartz_init_glyph_metrics (scaled_font, scaled_glyph); if (!status && (info & CAIRO_SCALED_GLYPH_INFO_PATH)) - status = _cairo_quartz_init_glyph_path (font, scaled_glyph); + status = _cairo_quartz_init_glyph_path (scaled_font, scaled_glyph); if (!status && (info & CAIRO_SCALED_GLYPH_INFO_SURFACE)) - status = _cairo_quartz_init_glyph_surface (font, scaled_glyph); + status = _cairo_quartz_init_glyph_surface (scaled_font, scaled_glyph); return status; } @@ -793,13 +785,12 @@ static unsigned long _cairo_quartz_ucs4_to_index (void *abstract_font, uint32_t ucs4) { - cairo_quartz_scaled_font_t *font = (cairo_quartz_scaled_font_t*) abstract_font; - cairo_quartz_font_face_t *ffont = _cairo_quartz_scaled_to_face(font); + cairo_quartz_font_face_t *font_face = _cairo_quartz_scaled_to_face (abstract_font); CGGlyph glyph[2]; UniChar utf16[2]; int len = _cairo_ucs4_to_utf16 (ucs4, utf16); - CGFontGetGlyphsForUnicharsPtr (ffont->cgFont, utf16, glyph, len); + CGFontGetGlyphsForUnicharsPtr (font_face->cgFont, utf16, glyph, len); return glyph[0]; } @@ -844,7 +835,10 @@ static const cairo_scaled_font_backend_t _cairo_quartz_scaled_font_backend = { NULL, /* text_to_glyphs */ _cairo_quartz_ucs4_to_index, _cairo_quartz_load_truetype_table, - NULL, /* map_glyphs_to_unicode */ + NULL, /* index_to_ucs4 */ + NULL, /* is_synthetic */ + NULL, /* index_to_glyph_name */ + NULL, /* load_type1_data */ }; /* @@ -887,14 +881,12 @@ _cairo_quartz_scaled_font_get_ct_font_ref (cairo_scaled_font_t *abstract_font) cairo_font_face_t * cairo_quartz_font_face_create_for_atsu_font_id (ATSUFontID font_id) { - quartz_font_ensure_symbols(); + quartz_font_ensure_symbols (); - if (FMGetATSFontRefFromFontPtr != NULL) { + if (likely (FMGetATSFontRefFromFontPtr)) { ATSFontRef atsFont = FMGetATSFontRefFromFontPtr (font_id); CGFontRef cgFont = CGFontCreateWithPlatformFont (&atsFont); - cairo_font_face_t *ff; - - ff = cairo_quartz_font_face_create_for_cgfont (cgFont); + cairo_font_face_t *ff = cairo_quartz_font_face_create_for_cgfont (cgFont); CGFontRelease (cgFont); |