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
This commit is contained in:
Takase 2022-11-17 13:09:38 +08:00 committed by GitHub
parent c8e525c126
commit 7fa51bb7ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -9,6 +9,7 @@
#include FT_FREETYPE_H #include FT_FREETYPE_H
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h>
#include "utfconv.h" #include "utfconv.h"
#endif #endif
@ -55,7 +56,10 @@ typedef struct RenFont {
ERenFontHinting hinting; ERenFontHinting hinting;
unsigned char style; unsigned char style;
unsigned short underline_thickness; unsigned short underline_thickness;
#ifdef _WIN32
unsigned char *file; unsigned char *file;
HANDLE file_handle;
#endif
char path[]; char path[];
} RenFont; } RenFont;
@ -226,7 +230,7 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial
if ((file = CreateFileW(wpath, if ((file = CreateFileW(wpath,
GENERIC_READ, GENERIC_READ,
0, FILE_SHARE_READ, // or else we can't copy fonts
NULL, NULL,
OPEN_EXISTING, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 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) if (!ReadFile(file, font_file, font_file_len, &read, NULL) || read != font_file_len)
goto failure; goto failure;
CloseHandle(file);
free(wpath); free(wpath);
wpath = NULL;
if (FT_New_Memory_Face(library, font_file, read, 0, &face)) if (FT_New_Memory_Face(library, font_file, read, 0, &face))
goto failure; goto failure;
@ -270,6 +274,7 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial
#ifdef _WIN32 #ifdef _WIN32
// we need to keep this for freetype // we need to keep this for freetype
font->file = font_file; font->file = font_file;
font->file_handle = file;
#endif #endif
if(FT_IS_SCALABLE(face)) if(FT_IS_SCALABLE(face))
@ -310,6 +315,7 @@ void ren_font_free(RenFont* font) {
FT_Done_Face(font->face); FT_Done_Face(font->face);
#ifdef _WIN32 #ifdef _WIN32
free(font->file); free(font->file);
CloseHandle(font->file_handle);
#endif #endif
free(font); free(font);
} }