Branch
Hash :
3ccc27dc
Author :
Date :
2025-06-19T08:06:53
[autofit] Really fix handling of `RTLD_DEFAULT`. This commit actually implements what commit 43ec023e1a7 describes. * src/autofit/ft-hb.c (FT_RTLD_FLAGS): New macro for `dlopen`; it uses `RTLD_GLOBAL` only if `RTLD_DEFAULT` is available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/****************************************************************************
*
* ft-hb.c
*
* FreeType-HarfBuzz bridge (body).
*
* Copyright (C) 2025 by
* Behdad Esfahbod.
*
* This file is part of the FreeType project, and may only be used,
* modified, and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*/
#if !defined( _WIN32 ) && !defined( _GNU_SOURCE )
# define _GNU_SOURCE 1 /* for RTLD_DEFAULT */
#endif
#include <freetype/freetype.h>
#include <freetype/internal/ftmemory.h>
#include "afglobal.h"
#include "ft-hb.h"
#if defined( FT_CONFIG_OPTION_USE_HARFBUZZ ) && \
defined( FT_CONFIG_OPTION_USE_HARFBUZZ_DYNAMIC )
#ifndef FT_LIBHARFBUZZ
# ifdef _WIN32
# define FT_LIBHARFBUZZ "libharfbuzz-0.dll"
# else
# ifdef __APPLE__
# define FT_LIBHARFBUZZ "libharfbuzz.0.dylib"
# else
# define FT_LIBHARFBUZZ "libharfbuzz.so.0"
# endif
# endif
#endif
#ifdef _WIN32
# include <windows.h>
#else /* !_WIN32 */
# include <dlfcn.h>
/* The GCC pragma suppresses the warning "ISO C forbids */
/* assignment between function pointer and 'void *'", which */
/* inevitably gets emitted with `-Wpedantic`; see the man */
/* page of function `dlsym` for more information. */
# if defined( __GNUC__ )
# pragma GCC diagnostic push
# ifndef __cplusplus
# pragma GCC diagnostic ignored "-Wpedantic"
# endif
# endif
#endif /* !_WIN32 */
FT_LOCAL_DEF( void )
ft_hb_funcs_init( struct AF_ModuleRec_ *af_module )
{
FT_Memory memory = af_module->root.memory;
FT_Error error;
ft_hb_funcs_t *funcs = NULL;
ft_hb_version_atleast_func_t version_atleast = NULL;
#ifdef _WIN32
HANDLE lib;
# define DLSYM( lib, name ) \
(ft_ ## name ## _func_t)GetProcAddress( lib, #name )
#else
void *lib;
# define DLSYM( lib, name ) \
(ft_ ## name ## _func_t)dlsym( lib, #name )
#endif
af_module->hb_funcs = NULL;
if ( FT_NEW( funcs ) )
return;
FT_ZERO( funcs );
#ifdef _WIN32
lib = LoadLibraryA( FT_LIBHARFBUZZ );
if ( !lib )
goto Fail;
version_atleast = DLSYM( lib, hb_version_atleast );
#else /* !_WIN32 */
# ifdef RTLD_DEFAULT
# define FT_RTLD_FLAGS RTLD_LAZY | RTLD_GLOBAL
lib = RTLD_DEFAULT;
version_atleast = DLSYM( lib, hb_version_atleast );
# else
# define FT_RTLD_FLAGS RTLD_LAZY
# endif
if ( !version_atleast )
{
/* Load the HarfBuzz library.
*
* We never close the library, since we opened it with RTLD_GLOBAL.
* This is important for the case where we are using HarfBuzz as a
* shared library, and we want to use the symbols from the library in
* other shared libraries or clients. HarfBuzz holds onto global
* variables, and closing the library will cause them to be
* invalidated.
*/
lib = dlopen( FT_LIBHARFBUZZ, FT_RTLD_FLAGS );
if ( !lib )
goto Fail;
version_atleast = DLSYM( lib, hb_version_atleast );
}
#endif /* !_WIN32 */
if ( !version_atleast )
goto Fail;
/* Load all symbols we use. */
#define HB_EXTERN( ret, name, args ) \
{ \
funcs->name = DLSYM( lib, name ); \
if ( !funcs->name ) \
goto Fail; \
}
#include "ft-hb-decls.h"
#undef HB_EXTERN
#undef DLSYM
af_module->hb_funcs = funcs;
return;
Fail:
if ( funcs )
FT_FREE( funcs );
}
FT_LOCAL_DEF( void )
ft_hb_funcs_done( struct AF_ModuleRec_ *af_module )
{
FT_Memory memory = af_module->root.memory;
if ( af_module->hb_funcs )
{
FT_FREE( af_module->hb_funcs );
af_module->hb_funcs = NULL;
}
}
FT_LOCAL_DEF( FT_Bool )
ft_hb_enabled( struct AF_FaceGlobalsRec_ *globals )
{
return globals->module->hb_funcs != NULL;
}
#ifndef _WIN32
# if defined( __GNUC__ )
# pragma GCC diagnostic pop
# endif
#endif
#else /* !FT_CONFIG_OPTION_USE_HARFBUZZ_DYNAMIC */
FT_LOCAL_DEF( FT_Bool )
ft_hb_enabled( struct AF_FaceGlobalsRec_ *globals )
{
FT_UNUSED( globals );
#ifdef FT_CONFIG_OPTION_USE_HARFBUZZ
return TRUE;
#else
return FALSE;
#endif
}
#endif /* !FT_CONFIG_OPTION_USE_HARFBUZZ_DYNAMIC */
/* END */