From 7fa51bb7abe61a7108e3a519df13803c3b82e7cd Mon Sep 17 00:00:00 2001 From: Takase <20792268+takase1121@users.noreply.github.com> Date: Thu, 17 Nov 2022 13:09:38 +0800 Subject: [PATCH] Windows font loading hotfix (#1205) * add missing windows header * hold the handle until GC so that the file can't be modified in use, this is a regression when we switched to CreateFile. * set wpath to NULL to avoid double free --- src/renderer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/renderer.c b/src/renderer.c index e34c8504..067df885 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -9,6 +9,7 @@ #include FT_FREETYPE_H #ifdef _WIN32 +#include #include "utfconv.h" #endif @@ -55,7 +56,10 @@ typedef struct RenFont { ERenFontHinting hinting; unsigned char style; unsigned short underline_thickness; +#ifdef _WIN32 unsigned char *file; + HANDLE file_handle; +#endif char path[]; } RenFont; @@ -226,7 +230,7 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial if ((file = CreateFileW(wpath, GENERIC_READ, - 0, + FILE_SHARE_READ, // or else we can't copy fonts NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, @@ -240,8 +244,8 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial if (!ReadFile(file, font_file, font_file_len, &read, NULL) || read != font_file_len) goto failure; - CloseHandle(file); free(wpath); + wpath = NULL; if (FT_New_Memory_Face(library, font_file, read, 0, &face)) goto failure; @@ -270,6 +274,7 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial #ifdef _WIN32 // we need to keep this for freetype font->file = font_file; + font->file_handle = file; #endif if(FT_IS_SCALABLE(face)) @@ -310,6 +315,7 @@ void ren_font_free(RenFont* font) { FT_Done_Face(font->face); #ifdef _WIN32 free(font->file); + CloseHandle(font->file_handle); #endif free(font); }