Files
o3de/Gems/AtomLyIntegration/AtomFont/Code/Source/GlyphCache.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

429 lines
11 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
// Purpose:
// - Manage and cache glyphs, retrieving them from the renderer as needed
#if !defined(USE_NULLFONT_ALWAYS)
#include <AtomLyIntegration/AtomFont/GlyphCache.h>
#include <AtomLyIntegration/AtomFont/FontTexture.h>
//-------------------------------------------------------------------------------------------------
AZ::GlyphCache::GlyphCache()
: m_usage(1)
, m_glyphBitmapWidth(0)
, m_glyphBitmapHeight(0)
, m_scaleBitmap(0)
{
m_cacheTable.clear();
m_slotList.clear();
}
//-------------------------------------------------------------------------------------------------
AZ::GlyphCache::~GlyphCache()
{
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::Create(int iCacheSize, int glyphBitmapWidth, int glyphBitmapHeight, AZ::FontSmoothMethod smoothMethod, AZ::FontSmoothAmount smoothAmount, float sizeRatio)
{
m_smoothMethod = smoothMethod;
m_smoothAmount = smoothAmount;
m_glyphBitmapWidth = glyphBitmapWidth;
m_glyphBitmapHeight = glyphBitmapHeight;
if (!CreateSlotList(iCacheSize))
{
ReleaseSlotList();
return 0;
}
int iScaledGlyphWidth = 0;
int iScaledGlyphHeight = 0;
switch (m_smoothMethod)
{
case AZ::FontSmoothMethod::SuperSample:
{
switch (m_smoothAmount)
{
case AZ::FontSmoothAmount::x2:
iScaledGlyphWidth = m_glyphBitmapWidth << 1;
iScaledGlyphHeight = m_glyphBitmapHeight << 1;
break;
case AZ::FontSmoothAmount::x4:
iScaledGlyphWidth = m_glyphBitmapWidth << 2;
iScaledGlyphHeight = m_glyphBitmapHeight << 2;
break;
}
}
break;
}
if (iScaledGlyphWidth)
{
m_scaleBitmap = new GlyphBitmap;
if (!m_scaleBitmap)
{
Release();
return 0;
}
if (!m_scaleBitmap->Create(iScaledGlyphWidth, iScaledGlyphHeight))
{
Release();
return 0;
}
m_fontRenderer.SetGlyphBitmapSize(iScaledGlyphWidth, iScaledGlyphHeight, sizeRatio);
}
else
{
m_fontRenderer.SetGlyphBitmapSize(m_glyphBitmapWidth, m_glyphBitmapHeight, sizeRatio);
}
return 1;
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::Release()
{
ReleaseSlotList();
m_cacheTable.clear();
if (m_scaleBitmap)
{
m_scaleBitmap->Release();
delete m_scaleBitmap;
m_scaleBitmap = 0;
}
m_glyphBitmapWidth = 0;
m_glyphBitmapHeight = 0;
return 1;
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::LoadFontFromFile(const string& fileName)
{
return m_fontRenderer.LoadFromFile(fileName);
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::LoadFontFromMemory(unsigned char* fileBuffer, int dataSize)
{
return m_fontRenderer.LoadFromMemory(fileBuffer, dataSize);
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::ReleaseFont()
{
m_fontRenderer.Release();
return 1;
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::GetGlyphBitmapSize(int* width, int* height)
{
if (width)
{
*width = m_glyphBitmapWidth;
}
if (height)
{
*height = m_glyphBitmapHeight;
}
return 1;
}
//-------------------------------------------------------------------------------------------------
void AZ::GlyphCache::SetGlyphBitmapSize(int width, int height, float sizeRatio)
{
m_fontRenderer.SetGlyphBitmapSize(width, height, sizeRatio);
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::PreCacheGlyph(uint32_t character, const AtomFont::GlyphSize& glyphSize, const FFont::FontHintParams& fontHintParams)
{
CacheTable::iterator pItor = m_cacheTable.find(GetCacheSlotKey(character, glyphSize));
if (pItor != m_cacheTable.end())
{
pItor->second->m_usage = m_usage;
return 1;
}
CacheSlot* slot = GetLRUSlot();
if (!slot)
{
return 0;
}
if (slot->m_usage > 0)
{
UnCacheGlyph(slot->m_currentCharacter, slot->m_glyphSize);
}
if (m_scaleBitmap)
{
int iOffsetMult = 1;
switch (m_smoothAmount)
{
case AZ::FontSmoothAmount::x2:
iOffsetMult = 2;
break;
case AZ::FontSmoothAmount::x4:
iOffsetMult = 4;
break;
}
m_scaleBitmap->Clear();
if (!m_fontRenderer.GetGlyph(m_scaleBitmap, &slot->m_horizontalAdvance, &slot->m_characterWidth, &slot->m_characterHeight, slot->m_characterOffsetX, slot->m_characterOffsetY, 0, 0, character, fontHintParams))
{
return 0;
}
slot->m_characterWidth >>= iOffsetMult >> 1;
slot->m_characterHeight >>= iOffsetMult >> 1;
m_scaleBitmap->BlitScaledTo8(slot->m_glyphBitmap.GetBuffer(), 0, 0, m_scaleBitmap->GetWidth(), m_scaleBitmap->GetHeight(), 0, 0, slot->m_glyphBitmap.GetWidth(), slot->m_glyphBitmap.GetHeight(), slot->m_glyphBitmap.GetWidth());
}
else
{
if (!m_fontRenderer.GetGlyph(&slot->m_glyphBitmap, &slot->m_horizontalAdvance, &slot->m_characterWidth, &slot->m_characterHeight, slot->m_characterOffsetX, slot->m_characterOffsetY, 0, 0, character, fontHintParams))
{
return 0;
}
}
if (m_smoothMethod == AZ::FontSmoothMethod::Blur)
{
slot->m_glyphBitmap.Blur(m_smoothAmount);
}
slot->m_usage = m_usage;
slot->m_currentCharacter = character;
slot->m_glyphSize = glyphSize;
m_cacheTable.insert(AZStd::pair<CacheTableKey, CacheSlot*>(GetCacheSlotKey(character, glyphSize), slot));
return 1;
}
int AZ::GlyphCache::UnCacheGlyph(uint32_t character, const AtomFont::GlyphSize& glyphSize)
{
CacheTable::iterator pItor = m_cacheTable.find(GetCacheSlotKey(character, glyphSize));
if (pItor != m_cacheTable.end())
{
CacheSlot* slot = pItor->second;
slot->Reset();
m_cacheTable.erase(pItor);
return 1;
}
return 0;
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::GlyphCached(uint32_t character, const AtomFont::GlyphSize& glyphSize)
{
return (m_cacheTable.find(GetCacheSlotKey(character, glyphSize)) != m_cacheTable.end());
}
//-------------------------------------------------------------------------------------------------
AZ::CacheSlot* AZ::GlyphCache::GetLRUSlot()
{
unsigned int dwMinUsage = 0xffffffff;
CacheSlot* pLRUSlot = 0;
CacheSlot* slot;
CacheSlotListItor pItor = m_slotList.begin();
while (pItor != m_slotList.end())
{
slot = *pItor;
if (slot->m_usage == 0)
{
return slot;
}
else
{
if (slot->m_usage < dwMinUsage)
{
pLRUSlot = slot;
dwMinUsage = slot->m_usage;
}
}
pItor++;
}
return pLRUSlot;
}
//-------------------------------------------------------------------------------------------------
AZ::CacheSlot* AZ::GlyphCache::GetMRUSlot()
{
unsigned int dwMaxUsage = 0;
CacheSlot* pMRUSlot = 0;
CacheSlot* slot;
CacheSlotListItor pItor = m_slotList.begin();
while (pItor != m_slotList.end())
{
slot = *pItor;
if (slot->m_usage != 0)
{
if (slot->m_usage > dwMaxUsage)
{
pMRUSlot = slot;
dwMaxUsage = slot->m_usage;
}
}
pItor++;
}
return pMRUSlot;
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::GetGlyph(AZ::GlyphBitmap** glyph, int* horizontalAdvance, int* width, int* height, int32_t& m_characterOffsetX, int32_t& m_characterOffsetY, uint32_t character, const AZ::AtomFont::GlyphSize& glyphSize, const AZ::FFont::FontHintParams& fontHintParams)
{
CacheTable::iterator pItor = m_cacheTable.find(GetCacheSlotKey(character, glyphSize));
if (pItor == m_cacheTable.end())
{
if (!PreCacheGlyph(character, glyphSize, fontHintParams))
{
return 0;
}
}
pItor = m_cacheTable.find(GetCacheSlotKey(character, glyphSize));
pItor->second->m_usage = m_usage++;
(*glyph) = &pItor->second->m_glyphBitmap;
if (horizontalAdvance)
{
*horizontalAdvance = pItor->second->m_horizontalAdvance;
}
if (width)
{
*width = pItor->second->m_characterWidth;
}
if (height)
{
*height = pItor->second->m_characterHeight;
}
m_characterOffsetX = pItor->second->m_characterOffsetX;
m_characterOffsetY = pItor->second->m_characterOffsetY;
return 1;
}
//-------------------------------------------------------------------------------------------------
Vec2 AZ::GlyphCache::GetKerning(uint32_t leftGlyph, uint32_t rightGlyph)
{
return m_fontRenderer.GetKerning(leftGlyph, rightGlyph);
}
//-------------------------------------------------------------------------------------------------
float AZ::GlyphCache::GetAscenderToHeightRatio()
{
return m_fontRenderer.GetAscenderToHeightRatio();
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::CreateSlotList(int listSize)
{
for (int i = 0; i < listSize; i++)
{
CacheSlot* cacheSlot = new CacheSlot;
if (!cacheSlot)
{
return 0;
}
if (!cacheSlot->m_glyphBitmap.Create(m_glyphBitmapWidth, m_glyphBitmapHeight))
{
delete cacheSlot;
return 0;
}
cacheSlot->Reset();
cacheSlot->m_slotIndex = i;
m_slotList.push_back(cacheSlot);
}
return 1;
}
//-------------------------------------------------------------------------------------------------
int AZ::GlyphCache::ReleaseSlotList()
{
CacheSlotListItor pItor = m_slotList.begin();
while (pItor != m_slotList.end())
{
(*pItor)->m_glyphBitmap.Release();
delete (*pItor);
pItor = m_slotList.erase(pItor);
}
return 1;
}
//-------------------------------------------------------------------------------------------------
AZ::GlyphCache::CacheTableKey AZ::GlyphCache::GetCacheSlotKey(uint32_t character, const AZ::AtomFont::GlyphSize& glyphSize) const
{
const AZ::AtomFont::GlyphSize clampedGlyphSize = AZ::FontTexture::ClampGlyphSize(glyphSize, m_glyphBitmapWidth, m_glyphBitmapHeight);
return CacheTableKey(clampedGlyphSize, character);
}
#endif // #if !defined(USE_NULLFONT_ALWAYS)