mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
Load fonts from memory (fixes #2810)
Missing: Freeing the memory again. But not so important since we keep the same fonts until end of process anyway in our case. @QingGo Could you give this a try from the Github build artifacts?
This commit is contained in:
parent
5228473ec9
commit
77d47a85ee
|
@ -3949,18 +3949,24 @@ void CClient::LoadFont()
|
|||
IOHANDLE File = Storage()->OpenFile(pFontFile, IOFLAG_READ, IStorage::TYPE_ALL, aFilename, sizeof(aFilename));
|
||||
if(File)
|
||||
{
|
||||
size_t Size = io_length(File);
|
||||
unsigned char *pBuf = (unsigned char *)malloc(Size);
|
||||
io_read(File, pBuf, Size);
|
||||
io_close(File);
|
||||
IEngineTextRender *pTextRender = Kernel()->RequestInterface<IEngineTextRender>();
|
||||
pDefaultFont = pTextRender->GetFont(aFilename);
|
||||
if(pDefaultFont == NULL)
|
||||
pDefaultFont = pTextRender->LoadFont(aFilename);
|
||||
pDefaultFont = pTextRender->LoadFont(aFilename, pBuf, Size);
|
||||
|
||||
File = Storage()->OpenFile(pFallbackFontFile, IOFLAG_READ, IStorage::TYPE_ALL, aFilename, sizeof(aFilename));
|
||||
if(File)
|
||||
{
|
||||
size_t Size = io_length(File);
|
||||
unsigned char *pBuf = (unsigned char *)malloc(Size);
|
||||
io_read(File, pBuf, Size);
|
||||
io_close(File);
|
||||
IEngineTextRender *pTextRender = Kernel()->RequestInterface<IEngineTextRender>();
|
||||
LoadedFallbackFont = pTextRender->LoadFallbackFont(pDefaultFont, aFilename);
|
||||
LoadedFallbackFont = pTextRender->LoadFallbackFont(pDefaultFont, aFilename, pBuf, Size);
|
||||
}
|
||||
|
||||
Kernel()->RequestInterface<IEngineTextRender>()->SetDefaultFont(pDefaultFont);
|
||||
|
|
|
@ -657,18 +657,21 @@ public:
|
|||
IOHANDLE File = pStorage->OpenFile(pFontFile, IOFLAG_READ, IStorage::TYPE_ALL, aFilename, sizeof(aFilename));
|
||||
if(File)
|
||||
{
|
||||
size_t Size = io_length(File);
|
||||
unsigned char *pBuf = (unsigned char *)malloc(Size);
|
||||
io_read(File, pBuf, Size);
|
||||
io_close(File);
|
||||
LoadFont(aFilename);
|
||||
LoadFont(aFilename, pBuf, Size);
|
||||
}
|
||||
}
|
||||
|
||||
virtual CFont *LoadFont(const char *pFilename)
|
||||
virtual CFont *LoadFont(const char *pFilename, const unsigned char *pBuf, size_t Size)
|
||||
{
|
||||
CFont *pFont = new CFont();
|
||||
|
||||
str_copy(pFont->m_aFilename, pFilename, sizeof(pFont->m_aFilename));
|
||||
|
||||
if(FT_New_Face(m_FTLibrary, pFont->m_aFilename, 0, &pFont->m_FtFace))
|
||||
if(FT_New_Memory_Face(m_FTLibrary, pBuf, Size, 0, &pFont->m_FtFace))
|
||||
{
|
||||
delete pFont;
|
||||
return NULL;
|
||||
|
@ -696,12 +699,12 @@ public:
|
|||
return pFont;
|
||||
}
|
||||
|
||||
virtual bool LoadFallbackFont(CFont* pFont, const char *pFilename)
|
||||
virtual bool LoadFallbackFont(CFont *pFont, const char *pFilename, const unsigned char *pBuf, size_t Size)
|
||||
{
|
||||
CFont::SFontFallBack FallbackFont;
|
||||
str_copy(FallbackFont.m_aFilename, pFilename, sizeof(FallbackFont.m_aFilename));
|
||||
|
||||
if(FT_New_Face(m_FTLibrary, pFilename, 0, &FallbackFont.m_FtFace) == 0)
|
||||
if(FT_New_Memory_Face(m_FTLibrary, pBuf, Size, 0, &FallbackFont.m_FtFace) == 0)
|
||||
{
|
||||
dbg_msg("textrender", "loaded fallback font from '%s'", pFilename);
|
||||
pFont->m_FtFallbackFonts.emplace_back(std::move(FallbackFont));
|
||||
|
|
|
@ -78,8 +78,8 @@ class ITextRender : public IInterface
|
|||
public:
|
||||
virtual void SetCursor(CTextCursor *pCursor, float x, float y, float FontSize, int Flags) = 0;
|
||||
|
||||
virtual CFont *LoadFont(const char *pFilename) = 0;
|
||||
virtual bool LoadFallbackFont(CFont* pFont, const char *pFilename) = 0;
|
||||
virtual CFont *LoadFont(const char *pFilename, const unsigned char *pBuf, size_t Size) = 0;
|
||||
virtual bool LoadFallbackFont(CFont *pFont, const char *pFilename, const unsigned char *pBuf, size_t Size) = 0;
|
||||
virtual CFont *GetFont(int FontIndex) = 0;
|
||||
virtual CFont *GetFont(const char *pFilename) = 0;
|
||||
virtual void DestroyFont(CFont *pFont) = 0;
|
||||
|
|
Loading…
Reference in a new issue