Use portable file IO for UI TTF fonts

This commit is contained in:
notmatthewbeshay 2026-03-10 22:43:54 +11:00
parent 594e799089
commit c50aa9c152
2 changed files with 38 additions and 21 deletions

View file

@ -1,40 +1,51 @@
#include "../../Minecraft.World/Platform/stdafx.h"
#include "UI.h"
#include "../../Minecraft.World/Util/StringHelpers.h"
#include "../../Minecraft.World/IO/Files/File.h"
#include "../../Minecraft.World/Util/PortableFileIO.h"
#include "UITTFFont.h"
UITTFFont::UITTFFont(const std::string &path, S32 fallbackCharacter)
{
app.DebugPrintf("UITTFFont opening %s\n",path.c_str());
pbData = NULL;
#if defined(_UNICODE) && !defined(__linux__)
std::wstring wPath = convStringToWstring(path);
HANDLE file = CreateFile(wPath.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#else
HANDLE file = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif
if( file == INVALID_HANDLE_VALUE )
std::FILE *file = PortableFileIO::OpenBinaryFileForRead(wPath);
if( file == NULL )
{
DWORD error = GetLastError();
app.DebugPrintf("Failed to open TTF file with error code %d (%x)\n", error, error);
app.DebugPrintf("Failed to open TTF file\n");
assert(false);
}
DWORD dwHigh=0;
DWORD dwFileSize = GetFileSize(file,&dwHigh);
if(dwFileSize!=0)
if(!PortableFileIO::Seek(file, 0, SEEK_END))
{
DWORD bytesRead;
std::fclose(file);
app.FatalLoadError();
}
pbData = (PBYTE) new BYTE[dwFileSize];
BOOL bSuccess = ReadFile(file,pbData,dwFileSize,&bytesRead,NULL);
if(bSuccess==FALSE)
const __int64 endPosition = PortableFileIO::Tell(file);
if(endPosition < 0)
{
std::fclose(file);
app.FatalLoadError();
}
const std::size_t fileSize = static_cast<std::size_t>(endPosition);
if(fileSize != 0)
{
if(!PortableFileIO::Seek(file, 0, SEEK_SET))
{
std::fclose(file);
app.FatalLoadError();
}
pbData = new std::uint8_t[fileSize];
const std::size_t bytesRead = std::fread(pbData, 1, fileSize, file);
const bool failed = std::ferror(file) != 0 || bytesRead != fileSize;
std::fclose(file);
if(failed)
{
app.FatalLoadError();
}
CloseHandle(file);
IggyFontInstallTruetypeUTF8 ( (void *)pbData, IGGY_TTC_INDEX_none, "Mojangles_TTF", -1, IGGY_FONTFLAG_none );
@ -44,8 +55,12 @@ UITTFFont::UITTFFont(const std::string &path, S32 fallbackCharacter)
IggyFontInstallTruetypeUTF8 ( (void *)pbData, IGGY_TTC_INDEX_none, "Times New Roman", -1, IGGY_FONTFLAG_none );
IggyFontInstallTruetypeUTF8 ( (void *)pbData, IGGY_TTC_INDEX_none, "Arial", -1, IGGY_FONTFLAG_none );
}
else
{
std::fclose(file);
}
}
UITTFFont::~UITTFFont()
{
}
}

View file

@ -1,9 +1,11 @@
#pragma once
#include <cstdint>
class UITTFFont
{
private:
PBYTE pbData;
std::uint8_t *pbData;
//DWORD dwDataSize;
public: