@@ -178,6 +178,94 @@ lttfdata(lua_State *L) {
178178 return 1 ;
179179}
180180
181+ #elif defined(__linux__ )
182+
183+ #include <fontconfig/fontconfig.h>
184+ #include <stdio.h>
185+ #include <stdlib.h>
186+
187+ static void *
188+ free_data (void * ud , void * ptr , size_t oszie , size_t nsize ) {
189+ free (ptr );
190+ return NULL ;
191+ }
192+
193+ static int
194+ lttfdata (lua_State * L ) {
195+ const char * familyName = luaL_checkstring (L , 1 );
196+
197+ if (!FcInit ()) {
198+ return luaL_error (L , "Failed to initialize fontconfig" );
199+ }
200+
201+ FcPattern * pattern = FcNameParse ((const FcChar8 * )familyName );
202+ if (!pattern ) {
203+ FcFini ();
204+ return luaL_error (L , "Failed to parse font name: %s" , familyName );
205+ }
206+
207+ FcConfigSubstitute (NULL , pattern , FcMatchPattern );
208+ FcDefaultSubstitute (pattern );
209+
210+ FcResult result ;
211+ FcPattern * match = FcFontMatch (NULL , pattern , & result );
212+ FcPatternDestroy (pattern );
213+
214+ if (!match || result != FcResultMatch ) {
215+ if (match ) FcPatternDestroy (match );
216+ FcFini ();
217+ return luaL_error (L , "Font not found: %s" , familyName );
218+ }
219+
220+ FcChar8 * filename ;
221+ if (FcPatternGetString (match , FC_FILE , 0 , & filename ) != FcResultMatch ) {
222+ FcPatternDestroy (match );
223+ FcFini ();
224+ return luaL_error (L , "Failed to get font file path for: %s" , familyName );
225+ }
226+
227+ FILE * file = fopen ((const char * )filename , "rb" );
228+ if (!file ) {
229+ FcPatternDestroy (match );
230+ FcFini ();
231+ return luaL_error (L , "Failed to open font file: %s" , filename );
232+ }
233+
234+ fseek (file , 0 , SEEK_END );
235+ long fileSize = ftell (file );
236+ fseek (file , 0 , SEEK_SET );
237+
238+ if (fileSize <= 0 ) {
239+ fclose (file );
240+ FcPatternDestroy (match );
241+ FcFini ();
242+ return luaL_error (L , "Invalid font file size: %s" , filename );
243+ }
244+
245+ char * buf = malloc (fileSize + 1 );
246+ if (!buf ) {
247+ fclose (file );
248+ FcPatternDestroy (match );
249+ FcFini ();
250+ return luaL_error (L , "Out of memory : sysfont" );
251+ }
252+
253+ size_t bytesRead = fread (buf , 1 , fileSize , file );
254+ fclose (file );
255+ FcPatternDestroy (match );
256+ FcFini ();
257+
258+ if (bytesRead != fileSize ) {
259+ free (buf );
260+ return luaL_error (L , "Failed to read font file: %s" , filename );
261+ }
262+
263+ buf [fileSize ] = 0 ;
264+
265+ lua_pushexternalstring (L , buf , fileSize , free_data , NULL );
266+ return 1 ;
267+ }
268+
181269#else
182270
183271static int
0 commit comments