Re-enable runtime font rendering if a font handler is assigned, clean up some font handler things.

This commit is contained in:
elasota
2021-04-15 19:32:15 -04:00
parent c49bab04d5
commit 4e8e76f8fc
15 changed files with 103 additions and 140 deletions

View File

@@ -137,6 +137,7 @@ GpFileSystem_Win32::GpFileSystem_Win32()
m_userSavesDir.append(L"\\");
m_logsDir.append(L"\\");
m_fontCacheDir.append(L"\\");
m_resourcesDir.append(L"\\");
}
DWORD modulePathSize = GetModuleFileNameW(nullptr, m_executablePath, MAX_PATH);
@@ -177,6 +178,7 @@ GpFileSystem_Win32::GpFileSystem_Win32()
{
m_packagedDir = std::wstring(m_executablePath) + L"Packaged\\";
m_housesDir = std::wstring(m_executablePath) + L"Packaged\\Houses\\";
m_resourcesDir = std::wstring(m_executablePath) + L"Resources\\";
}
}
@@ -453,9 +455,6 @@ bool GpFileSystem_Win32::ResolvePath(PortabilityLayer::VirtualDirectory_t virtua
case PortabilityLayer::VirtualDirectories::kLogs:
baseDir = m_logsDir.c_str();
break;
case PortabilityLayer::VirtualDirectories::kFontCache:
baseDir = m_fontCacheDir.c_str();
break;
default:
return false;
}

View File

@@ -35,40 +35,6 @@ namespace PortabilityLayer
return minIndexInclusive;
}
bool AntiAliasTable::LoadFromCache(const char *cacheFileName)
{
GpIOStream *stream = PLDrivers::GetFileSystem()->OpenFile(PortabilityLayer::VirtualDirectories::kFontCache, cacheFileName, false, GpFileCreationDispositions::kOpenExisting);
if (!stream)
return false;
BEUInt32_t cacheVersion;
if (stream->Read(&cacheVersion, sizeof(cacheVersion)) != sizeof(cacheVersion) || cacheVersion != kCacheVersion)
{
stream->Close();
return false;
}
const size_t readSize = sizeof(m_aaTranslate);
const bool readOK = (stream->Read(m_aaTranslate, readSize) == readSize);
stream->Close();
return readOK;
}
void AntiAliasTable::SaveToCache(const char *cacheFileName)
{
GpIOStream *stream = PLDrivers::GetFileSystem()->OpenFile(PortabilityLayer::VirtualDirectories::kFontCache, cacheFileName, true, GpFileCreationDispositions::kCreateOrOverwrite);
if (!stream)
return;
BEUInt32_t cacheVersion(static_cast<uint32_t>(kCacheVersion));
stream->Write(&cacheVersion, sizeof(cacheVersion));
stream->Write(m_aaTranslate, sizeof(m_aaTranslate));
stream->Close();
}
void AntiAliasTable::GenerateForPaletteFast(const RGBAColor &baseColorRef)
{
const RGBAColor baseColor = baseColorRef;
@@ -191,16 +157,8 @@ namespace PortabilityLayer
}
}
void AntiAliasTable::GenerateForPalette(const RGBAColor &baseColorRef, const RGBAColor *colors, size_t numColors, bool cacheable)
void AntiAliasTable::GenerateForPalette(const RGBAColor &baseColorRef, const RGBAColor *colors, size_t numColors)
{
char cacheFileName[256];
if (cacheable)
{
sprintf(cacheFileName, "aa_p_%02x%02x%02x%02x.cache", static_cast<int>(baseColorRef.r), static_cast<int>(baseColorRef.g), static_cast<int>(baseColorRef.b), static_cast<int>(baseColorRef.a));
if (LoadFromCache(cacheFileName))
return;
}
const RGBAColor baseColor = baseColorRef;
if (numColors > 256)
@@ -259,21 +217,10 @@ namespace PortabilityLayer
m_aaTranslate[i][b] = static_cast<uint8_t>(bestColor);
}
}
if (cacheable)
SaveToCache(cacheFileName);
}
void AntiAliasTable::GenerateForSimpleScale(uint8_t colorChannel, bool cacheable)
void AntiAliasTable::GenerateForSimpleScale(uint8_t colorChannel)
{
char cacheFileName[256];
if (cacheable)
{
sprintf(cacheFileName, "aa_t_%02x.cache", static_cast<int>(colorChannel));
if (LoadFromCache(cacheFileName))
return;
}
const double gamma = 1.8;
const double rcpGamma = 1.0 / gamma;
const double rcp255 = 1.0 / 255.0;
@@ -294,8 +241,5 @@ namespace PortabilityLayer
m_aaTranslate[baseColor][opacity] = static_cast<uint8_t>(floor(blendedColorGammaSpace * 255.0 + 0.5));
}
}
if (cacheable)
SaveToCache(cacheFileName);
}
}

View File

@@ -12,14 +12,8 @@ namespace PortabilityLayer
// Striped 256x16 because constant background color is more likely than constant sample
uint8_t m_aaTranslate[256][16];
void GenerateForPalette(const RGBAColor &baseColor, const RGBAColor *colors, size_t numColors, bool cacheable);
void GenerateForPalette(const RGBAColor &baseColor, const RGBAColor *colors, size_t numColors);
void GenerateForPaletteFast(const RGBAColor &baseColor);
void GenerateForSimpleScale(uint8_t colorChannel, bool cacheable);
private:
bool LoadFromCache(const char *path);
void SaveToCache(const char *path);
static const unsigned int kCacheVersion = 1;
void GenerateForSimpleScale(uint8_t colorChannel);
};
}

View File

@@ -106,6 +106,16 @@ namespace PortabilityLayer
return font;
}
void FontFamily::UnloadVariation(int variation)
{
FontSpec &spec = m_fontSpecs[variation];
if (spec.m_font)
{
spec.m_font->Destroy();
spec.m_font = nullptr;
}
}
PortabilityLayer::FontHacks FontFamily::GetHacksForVariation(int variation) const
{
return m_fontSpecs[variation].m_hacks;

View File

@@ -30,6 +30,7 @@ namespace PortabilityLayer
int GetVariationForFlags(int flags) const;
IGpFont *GetFontForVariation(int variation);
void UnloadVariation(int variation);
FontHacks GetHacksForVariation(int variation) const;
FontFamilyID_t GetFamilyID() const;

View File

@@ -32,8 +32,6 @@ namespace PortabilityLayer
FontFamily *GetFont(FontFamilyID_t fontFamilyID) const override;
void GetFontPreset(FontPreset_t fontPreset, FontFamilyID_t *outFamilyID, int *outSize, int *outVariationFlags, bool *outAA) const override;
RenderedFont *GetRenderedFontFromFamily(FontFamily *font, int size, bool aa, int flags) override;
RenderedFont *LoadCachedRenderedFont(FontFamilyID_t familyID, int size, bool aa, int flags) override;
void PurgeCache() override;
@@ -41,6 +39,9 @@ namespace PortabilityLayer
static FontManagerImpl *GetInstance();
private:
RenderedFont *LoadAndRenderFontUsingFontHandler(FontFamily *font, int size, bool aa, int flags);
RenderedFont *LoadAndRenderFont(FontFamilyID_t familyID, int size, bool aa, int flags);
static const unsigned int kNumCachedRenderedFonts = 32;
struct CachedRenderedFont
@@ -206,44 +207,49 @@ namespace PortabilityLayer
m_usageCounter++;
}
RenderedFont *FontManagerImpl::GetRenderedFontFromFamily(FontFamily *fontFamily, int size, bool aa, int flags)
RenderedFont *FontManagerImpl::LoadAndRenderFontUsingFontHandler(FontFamily *fontFamily, int size, bool aa, int flags)
{
PortabilityLayer::FontManager *fm = PortabilityLayer::FontManager::GetInstance();
RenderedFont *rfont = nullptr;
CachedRenderedFont *cacheSlot = nullptr;
FontFamilyID_t familyID = fontFamily->GetFamilyID();
if (this->FindOrReserveCacheSlot(familyID, size, aa, cacheSlot, rfont))
return rfont;
rfont = fm->LoadCachedRenderedFont(familyID, size, aa, flags);
if (rfont)
{
ReplaceCachedRenderedFont(*cacheSlot, rfont, familyID, size, aa, flags);
return rfont;
}
const int variation = fontFamily->GetVariationForFlags(flags);
IGpFont *hostFont = fontFamily->GetFontForVariation(variation);
if (!hostFont)
return nullptr;
rfont = FontRenderer::GetInstance()->RenderFont(hostFont, size, aa, fontFamily->GetHacksForVariation(variation));
if (rfont)
{
ReplaceCachedRenderedFont(*cacheSlot, rfont, familyID, size, aa, flags);
return rfont;
}
RenderedFont *rfont = FontRenderer::GetInstance()->RenderFont(hostFont, size, aa, fontFamily->GetHacksForVariation(variation));
fontFamily->UnloadVariation(variation);
return rfont;
}
RenderedFont *FontManagerImpl::LoadCachedRenderedFont(FontFamilyID_t familyID, int size, bool aa, int flags)
{
CachedRenderedFont *cacheSlot = nullptr;
RenderedFont *rfont = nullptr;
if (this->FindOrReserveCacheSlot(familyID, size, aa, cacheSlot, rfont))
return rfont;
rfont = LoadAndRenderFont(familyID, size, aa, flags);
if (rfont)
ReplaceCachedRenderedFont(*cacheSlot, rfont, familyID, size, aa, flags);
return rfont;
}
RenderedFont *FontManagerImpl::LoadAndRenderFont(FontFamilyID_t familyID, int size, bool aa, int flags)
{
FontFamily *fontFamily = this->GetFont(familyID);
RenderedFont *rfont = nullptr;
if (PLDrivers::GetFontHandler() != nullptr)
{
rfont = LoadAndRenderFontUsingFontHandler(fontFamily, size, aa, flags);
if (rfont != nullptr)
return rfont;
}
if (!m_fontArchive)
{
m_fontArchiveFile = PortabilityLayer::FileManager::GetInstance()->OpenCompositeFile(VirtualDirectories::kApplicationData, PSTR("Fonts"));
@@ -266,7 +272,6 @@ namespace PortabilityLayer
}
}
FontFamily *fontFamily = this->GetFont(familyID);
int variation = fontFamily->GetVariationForFlags(flags);
FontHacks hacks = FontHacks_None;
@@ -328,13 +333,13 @@ namespace PortabilityLayer
return nullptr;
THandle<void> res = m_fontArchive->LoadResource('RFNT', 1000 + static_cast<int>(fontIndex));
if (!res)
return nullptr;
if (res)
{
PortabilityLayer::MemReaderStream stream(*res, res.MMBlock()->m_size);
PortabilityLayer::MemReaderStream stream(*res, res.MMBlock()->m_size);
RenderedFont *rfont = PortabilityLayer::FontRenderer::GetInstance()->LoadCache(&stream);
res.Dispose();
rfont = PortabilityLayer::FontRenderer::GetInstance()->LoadCache(&stream);
res.Dispose();
}
return rfont;
}

View File

@@ -2,6 +2,7 @@
#include "FontHacks.h"
#include "FontFamilyID.h"
#include "FontPresets.h"
struct IGpFont;
@@ -10,34 +11,6 @@ namespace PortabilityLayer
class FontFamily;
class RenderedFont;
namespace FontPresets
{
enum FontPreset
{
kSystem12,
kSystem12Bold,
kApplication8,
kApplication9,
kApplication9Bold,
kApplication10Bold,
kApplication12Bold,
kApplication14,
kApplication14Bold,
kApplication18,
kApplication40,
kMono10,
kHandwriting24,
kHandwriting48,
kCount,
};
}
typedef FontPresets::FontPreset FontPreset_t;
class FontManager
{
public:
@@ -47,8 +20,6 @@ namespace PortabilityLayer
virtual FontFamily *GetFont(FontFamilyID_t fontFamilyID) const = 0;
virtual void GetFontPreset(FontPreset_t fontPreset, FontFamilyID_t *outFamilyID, int *outSize, int *outVariationFlags, bool *outAA) const = 0;
virtual RenderedFont *GetRenderedFontFromFamily(FontFamily *fontFamily, int fontSize, bool aa, int flags) = 0;
virtual RenderedFont *LoadCachedRenderedFont(FontFamilyID_t familyID, int size, bool aa, int flags) = 0;
virtual void PurgeCache() = 0;

View File

@@ -0,0 +1,32 @@
#pragma once
namespace PortabilityLayer
{
namespace FontPresets
{
enum FontPreset
{
kSystem12,
kSystem12Bold,
kApplication8,
kApplication9,
kApplication9Bold,
kApplication10Bold,
kApplication12Bold,
kApplication14,
kApplication14Bold,
kApplication18,
kApplication40,
kMono10,
kHandwriting24,
kHandwriting48,
kCount,
};
}
typedef FontPresets::FontPreset FontPreset_t;
}

View File

@@ -1330,16 +1330,19 @@ namespace PortabilityLayer
return ccs->m_category;
}
FontFamily *EditboxWidget::GetFontFamily() const
FontPreset_t EditboxWidget::GetFontPreset() const
{
FontFamilyID_t preset = FontFamilyIDs::kCount;
PortabilityLayer::FontManager::GetInstance()->GetFontPreset(FontPresets::kSystem12, &preset, nullptr, nullptr, nullptr);
return PortabilityLayer::FontManager::GetInstance()->GetFont(preset);
return FontPresets::kSystem12;
}
RenderedFont *EditboxWidget::GetRenderedFont() const
{
return PortabilityLayer::FontManager::GetInstance()->GetRenderedFontFromFamily(GetFontFamily(), 12, true, FontFamilyFlag_None);
PortabilityLayer::FontFamilyID_t fontFamilyID = FontFamilyIDs::kCount;
int size = 0;
int varFlags = 0;
bool aa = false;
PortabilityLayer::FontManager::GetInstance()->GetFontPreset(GetFontPreset(), &fontFamilyID, &size, &varFlags, &aa);
return PortabilityLayer::FontManager::GetInstance()->LoadCachedRenderedFont(fontFamilyID, size, aa, varFlags);
}
void EditboxWidget::SetMultiLine(bool isMultiLine)

View File

@@ -1,5 +1,6 @@
#pragma once
#include "FontPresets.h"
#include "PascalStr.h"
#include "PLWidgets.h"
#include "Vec2i.h"
@@ -111,7 +112,7 @@ namespace PortabilityLayer
size_t IdentifySpanLength(size_t startChar, SpanScanDirection scanDirection) const;
static CharacterCategory CategorizeCharacter(uint8_t character);
PortabilityLayer::FontFamily *GetFontFamily() const;
PortabilityLayer::FontPreset_t GetFontPreset() const;
PortabilityLayer::RenderedFont *GetRenderedFont() const;
uint8_t *m_chars;

View File

@@ -2016,7 +2016,7 @@ PortabilityLayer::RenderedFont *GetFont(PortabilityLayer::FontPreset_t fontPrese
if (familyID == PortabilityLayer::FontFamilyIDs::kCount)
return nullptr;
return fontManager->GetRenderedFontFromFamily(fontManager->GetFont(familyID), size, aa, variationFlags);
return fontManager->LoadCachedRenderedFont(familyID, size, aa, variationFlags);
}
#include "stb_image_write.h"

View File

@@ -105,6 +105,7 @@
<ClInclude Include="FontFamily.h" />
<ClInclude Include="FontFamilyID.h" />
<ClInclude Include="FontManager.h" />
<ClInclude Include="FontPresets.h" />
<ClInclude Include="FontRenderer.h" />
<ClInclude Include="GpAppInterface.h" />
<ClInclude Include="GPArchive.h" />

View File

@@ -432,6 +432,9 @@
<ClInclude Include="FontFamilyID.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="FontPresets.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CFileStream.cpp">

View File

@@ -293,7 +293,7 @@ namespace PortabilityLayer
if (mutex)
mutex->Unlock();
entry.m_aaTable.GenerateForSimpleScale(tone, false);
entry.m_aaTable.GenerateForSimpleScale(tone);
return entry.m_aaTable;
}

View File

@@ -17,7 +17,6 @@ namespace PortabilityLayer
kCursors,
kHighScores,
kLogs,
kFontCache,
kSourceExport,
};