https://git.reactos.org/?p=reactos.git;a=commitdiff;h=1868985dde6217225f83b0e20c69639387e60fb2
commit 1868985dde6217225f83b0e20c69639387e60fb2 Author: Katayama Hirofumi MZ <katayama.hirofumi...@gmail.com> AuthorDate: Wed Jan 22 11:56:41 2025 +0900 Commit: GitHub <nore...@github.com> CommitDate: Wed Jan 22 11:56:41 2025 +0900 [FREETYPE][NTGDI] Simplify IntGdiLoadFontsFromMemory (#7651) Simplify code logic and reduce management cost. JIRA issue: CORE-19973 - Add IntGdiLoadFontByIndexFromMemory helper function. - Simplify IntGdiLoadFontsFromMemory function. --- win32ss/gdi/ntgdi/freetype.c | 115 +++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 54 deletions(-) diff --git a/win32ss/gdi/ntgdi/freetype.c b/win32ss/gdi/ntgdi/freetype.c index 13932681b29..b6ba46316c7 100644 --- a/win32ss/gdi/ntgdi/freetype.c +++ b/win32ss/gdi/ntgdi/freetype.c @@ -1737,44 +1737,13 @@ IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont, FT_ULong os2_ulCodePageRange1; FT_UShort os2_usWeightClass; - if (SharedFace == NULL && CharSetIndex == -1) - { - /* load a face from memory */ - IntLockFreeType(); - Error = FT_New_Memory_Face( - g_FreeTypeLibrary, - pLoadFont->Memory->Buffer, - pLoadFont->Memory->BufferSize, - ((FontIndex != -1) ? FontIndex : 0), - &Face); - - if (!Error) - SharedFace = SharedFace_Create(Face, pLoadFont->Memory); + ASSERT(SharedFace != NULL); + ASSERT(FontIndex != -1); - IntUnLockFreeType(); - - if (!Error && FT_IS_SFNT(Face)) - pLoadFont->IsTrueType = TRUE; - - if (Error || SharedFace == NULL) - { - if (SharedFace) - SharedFace_Release(SharedFace); - - if (Error == FT_Err_Unknown_File_Format) - DPRINT1("Unknown font file format\n"); - else - DPRINT1("Error reading font (error code: %d)\n", Error); - return 0; /* failure */ - } - } - else - { - Face = SharedFace->Face; - IntLockFreeType(); - SharedFace_AddRef(SharedFace); - IntUnLockFreeType(); - } + IntLockFreeType(); + Face = SharedFace->Face; + SharedFace_AddRef(SharedFace); + IntUnLockFreeType(); /* allocate a FONT_ENTRY */ Entry = ExAllocatePoolWithTag(PagedPool, sizeof(FONT_ENTRY), TAG_FONT); @@ -1984,16 +1953,6 @@ IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont, } IntUnLockFreeType(); - if (FontIndex == -1) - { - FT_Long iFace, num_faces = Face->num_faces; - for (iFace = 1; iFace < num_faces; ++iFace) - { - FaceCount += IntGdiLoadFontsFromMemory(pLoadFont, NULL, iFace, -1); - } - FontIndex = 0; - } - if (CharSetIndex == -1) { INT i; @@ -2046,6 +2005,57 @@ IntGdiLoadFontsFromMemory(PGDI_LOAD_FONT pLoadFont, return FaceCount; /* number of loaded faces */ } +static INT FASTCALL +IntGdiLoadFontByIndexFromMemory(PGDI_LOAD_FONT pLoadFont, FT_Long FontIndex) +{ + FT_Error Error; + FT_Face Face; + FT_Long iFace, num_faces; + PSHARED_FACE SharedFace; + INT FaceCount = 0; + + IntLockFreeType(); + + /* Load a face from memory */ + Error = FT_New_Memory_Face(g_FreeTypeLibrary, + pLoadFont->Memory->Buffer, pLoadFont->Memory->BufferSize, + ((FontIndex == -1) ? 0 : FontIndex), &Face); + if (Error) + { + if (Error == FT_Err_Unknown_File_Format) + DPRINT1("Unknown font file format\n"); + else + DPRINT1("Error reading font (error code: %d)\n", Error); + IntUnLockFreeType(); + return 0; /* Failure */ + } + + pLoadFont->IsTrueType = FT_IS_SFNT(Face); + num_faces = Face->num_faces; + SharedFace = SharedFace_Create(Face, pLoadFont->Memory); + + IntUnLockFreeType(); + + if (!SharedFace) + { + DPRINT1("SharedFace_Create failed\n"); + EngSetLastError(ERROR_NOT_ENOUGH_MEMORY); + return 0; /* Failure */ + } + + if (FontIndex == -1) + { + for (iFace = 1; iFace < num_faces; ++iFace) + { + FaceCount += IntGdiLoadFontByIndexFromMemory(pLoadFont, iFace); + } + FontIndex = 0; + } + + FaceCount += IntGdiLoadFontsFromMemory(pLoadFont, SharedFace, FontIndex, -1); + return FaceCount; +} + static LPCWSTR FASTCALL NameFromCharSet(BYTE CharSet) { @@ -2172,14 +2182,13 @@ IntGdiAddFontResourceEx(PUNICODE_STRING FileName, DWORD Characteristics, return 0; } + RtlZeroMemory(&LoadFont, sizeof(LoadFont)); LoadFont.pFileName = &PathName; LoadFont.Memory = SharedMem_Create(Buffer, ViewSize, TRUE); LoadFont.Characteristics = Characteristics; RtlInitUnicodeString(&LoadFont.RegValueName, NULL); - LoadFont.IsTrueType = FALSE; LoadFont.CharSet = DEFAULT_CHARSET; - LoadFont.PrivateEntry = NULL; - FontCount = IntGdiLoadFontsFromMemory(&LoadFont, NULL, -1, -1); + FontCount = IntGdiLoadFontByIndexFromMemory(&LoadFont, -1); /* Release our copy */ IntLockFreeType(); @@ -2463,13 +2472,11 @@ IntGdiAddFontMemResource(PVOID Buffer, DWORD dwSize, PDWORD pNumAdded) } RtlCopyMemory(BufferCopy, Buffer, dwSize); - LoadFont.pFileName = NULL; + RtlZeroMemory(&LoadFont, sizeof(LoadFont)); LoadFont.Memory = SharedMem_Create(BufferCopy, dwSize, FALSE); LoadFont.Characteristics = FR_PRIVATE | FR_NOT_ENUM; RtlInitUnicodeString(&LoadFont.RegValueName, NULL); - LoadFont.IsTrueType = FALSE; - LoadFont.PrivateEntry = NULL; - FaceCount = IntGdiLoadFontsFromMemory(&LoadFont, NULL, -1, -1); + FaceCount = IntGdiLoadFontByIndexFromMemory(&LoadFont, -1); RtlFreeUnicodeString(&LoadFont.RegValueName);