From 89c83b5b08f7e66db1d537211ba1d214d082162f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 12 Oct 2024 19:45:12 -0600 Subject: [PATCH] [coretext] Add hb_coretext_face_create_from_file_or_fail() New API hb_coretext_face_create_from_file_or_fail() --- docs/harfbuzz-sections.txt | 1 + src/hb-coretext-font.cc | 5 ++- src/hb-coretext-shape.cc | 63 ++++++++++++++++++++++++++++++++++++-- src/hb-coretext.h | 4 +++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index fff7730f6..23b4a5af3 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -340,6 +340,7 @@ HB_CORETEXT_TAG_KERX HB_CORETEXT_TAG_MORT HB_CORETEXT_TAG_MORX hb_coretext_face_create +hb_coretext_face_create_from_file_or_fail hb_coretext_font_create hb_coretext_face_get_cg_font hb_coretext_font_get_ct_font diff --git a/src/hb-coretext-font.cc b/src/hb-coretext-font.cc index e06049d44..90da30963 100644 --- a/src/hb-coretext-font.cc +++ b/src/hb-coretext-font.cc @@ -34,6 +34,9 @@ #include "hb-font.hh" #include "hb-machinery.hh" +#if MAC_OS_X_VERSION_MIN_REQUIRED < 101100 +# define kCTFontOrientationDefault kCTFontDefaultOrientation +#endif #define MAX_GLYPHS 64u @@ -157,7 +160,7 @@ hb_coretext_get_glyph_extents (hb_font_t *font, CGGlyph glyphs[1] = { glyph }; CGRect bounds = ::CTFontGetBoundingRectsForGlyphs(ct_font, - kCTFontDefaultOrientation, glyphs, NULL, 1); + kCTFontOrientationDefault, glyphs, NULL, 1); extents->x_bearing = round (bounds.origin.x * x_mult); extents->y_bearing = round (bounds.origin.y * y_mult); diff --git a/src/hb-coretext-shape.cc b/src/hb-coretext-shape.cc index 8f1b16df3..b0b86c5b5 100644 --- a/src/hb-coretext-shape.cc +++ b/src/hb-coretext-shape.cc @@ -335,7 +335,7 @@ _hb_coretext_shaper_face_data_destroy (hb_coretext_face_data_t *data) * Creates an #hb_face_t face object from the specified * CGFontRef. * - * Return value: the new #hb_face_t face object + * Return value: (transfer full): The new face object * * Since: 0.9.10 */ @@ -347,6 +347,65 @@ hb_coretext_face_create (CGFontRef cg_font) return face; } +/** + * hb_coretext_face_create_from_file_or_fail: + * @file_name: A font filename + * @index: The index of the face within the file + * + * Creates an #hb_face_t face object from the specified + * font file and face index. + * + * This is similar in functionality to hb_face_create_for_from_file_or_fail(), + * but uses the CoreText library for loading the font file. + * + * Return value: (transfer full): The new face object, or `NULL` if + * no face is found at the specified index or the file cannot be read. + * + * Since: 0.9.10 + */ +hb_face_t * +hb_coretext_face_create_from_file_or_fail (const char *file_name, + unsigned int index) +{ + auto url = CFURLCreateFromFileSystemRepresentation (nullptr, + (const UInt8 *) file_name, + strlen (file_name), + false); + if (unlikely (!url)) + return nullptr; + + auto ct_font_desc_array = CTFontManagerCreateFontDescriptorsFromURL (url); + if (unlikely (!ct_font_desc_array)) + { + CFRelease (url); + return nullptr; + } + auto ct_font_desc = (CFArrayGetCount (ct_font_desc_array) > index) ? + (CTFontDescriptorRef) CFArrayGetValueAtIndex (ct_font_desc_array, index) : nullptr; + if (unlikely (!ct_font_desc)) + { + CFRelease (ct_font_desc_array); + CFRelease (url); + return nullptr; + } + CFRelease (url); + auto ct_font = ct_font_desc ? CTFontCreateWithFontDescriptor (ct_font_desc, 0, nullptr) : nullptr; + CFRelease (ct_font_desc_array); + if (unlikely (!ct_font)) + return nullptr; + + auto cg_font = ct_font ? CTFontCopyGraphicsFont (ct_font, nullptr) : nullptr; + CFRelease (ct_font); + if (unlikely (!cg_font)) + return nullptr; + + hb_face_t *face = hb_coretext_face_create (cg_font); + if (unlikely (hb_face_is_immutable (face))) + return nullptr; + + return face; +} + /** * hb_coretext_face_get_cg_font: * @face: The #hb_face_t to work upon @@ -444,7 +503,7 @@ _hb_coretext_shaper_font_data_destroy (hb_coretext_font_data_t *data) * instead (rarely needed), you can do so by calling * by hb_coretext_font_set_funcs(). * - * Return value: the new #hb_font_t font object + * Return value: (transfer full): The new font object * * Since: 1.7.2 **/ diff --git a/src/hb-coretext.h b/src/hb-coretext.h index 2287ca83f..3626f1c12 100644 --- a/src/hb-coretext.h +++ b/src/hb-coretext.h @@ -80,6 +80,10 @@ HB_BEGIN_DECLS HB_EXTERN hb_face_t * hb_coretext_face_create (CGFontRef cg_font); +HB_EXTERN hb_face_t * +hb_coretext_face_create_from_file_or_fail (const char *file_name, + unsigned int index); + HB_EXTERN hb_font_t * hb_coretext_font_create (CTFontRef ct_font);