Extract font loading.

This commit is contained in:
Bartosz Taudul 2022-07-25 01:39:13 +02:00
parent 2d327abd80
commit f0f00512b0
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
5 changed files with 87 additions and 58 deletions

View File

@ -195,6 +195,7 @@
<ClCompile Include="..\..\..\zstd\dictBuilder\divsufsort.c" /> <ClCompile Include="..\..\..\zstd\dictBuilder\divsufsort.c" />
<ClCompile Include="..\..\..\zstd\dictBuilder\fastcover.c" /> <ClCompile Include="..\..\..\zstd\dictBuilder\fastcover.c" />
<ClCompile Include="..\..\..\zstd\dictBuilder\zdict.c" /> <ClCompile Include="..\..\..\zstd\dictBuilder\zdict.c" />
<ClCompile Include="..\..\src\Fonts.cpp" />
<ClCompile Include="..\..\src\HttpRequest.cpp" /> <ClCompile Include="..\..\src\HttpRequest.cpp" />
<ClCompile Include="..\..\src\imgui\imgui_impl_glfw.cpp" /> <ClCompile Include="..\..\src\imgui\imgui_impl_glfw.cpp" />
<ClCompile Include="..\..\src\imgui\imgui_impl_opengl3.cpp" /> <ClCompile Include="..\..\src\imgui\imgui_impl_opengl3.cpp" />
@ -311,6 +312,7 @@
<ClInclude Include="..\..\..\zstd\zdict.h" /> <ClInclude Include="..\..\..\zstd\zdict.h" />
<ClInclude Include="..\..\..\zstd\zstd.h" /> <ClInclude Include="..\..\..\zstd\zstd.h" />
<ClInclude Include="..\..\..\zstd\zstd_errors.h" /> <ClInclude Include="..\..\..\zstd\zstd_errors.h" />
<ClInclude Include="..\..\src\Fonts.hpp" />
<ClInclude Include="..\..\src\font\DroidSans.hpp" /> <ClInclude Include="..\..\src\font\DroidSans.hpp" />
<ClInclude Include="..\..\src\font\FiraCodeRetina.hpp" /> <ClInclude Include="..\..\src\font\FiraCodeRetina.hpp" />
<ClInclude Include="..\..\src\font\FontAwesomeSolid.hpp" /> <ClInclude Include="..\..\src\font\FontAwesomeSolid.hpp" />

View File

@ -333,6 +333,9 @@
<ClCompile Include="..\..\src\imgui\imgui_impl_opengl3.cpp"> <ClCompile Include="..\..\src\imgui\imgui_impl_opengl3.cpp">
<Filter>src\imgui</Filter> <Filter>src\imgui</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\Fonts.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\server\TracyEvent.hpp"> <ClInclude Include="..\..\..\server\TracyEvent.hpp">
@ -677,6 +680,9 @@
<ClInclude Include="..\..\src\imgui\imgui_impl_opengl3_loader.h"> <ClInclude Include="..\..\src\imgui\imgui_impl_opengl3_loader.h">
<Filter>src\imgui</Filter> <Filter>src\imgui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Fonts.hpp">
<Filter>src</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="DebugVis.natvis" /> <Natvis Include="DebugVis.natvis" />

50
profiler/src/Fonts.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <imgui.h>
#include <math.h>
#include "Fonts.hpp"
#include "misc/freetype/imgui_freetype.h"
#include "imgui/imgui_impl_opengl3.h"
#include "../../server/IconsFontAwesome5.h"
#include "font/DroidSans.hpp"
#include "font/FiraCodeRetina.hpp"
#include "font/FontAwesomeSolid.hpp"
ImFont* s_bigFont;
ImFont* s_smallFont;
ImFont* s_fixedWidth;
void LoadFonts( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont )
{
static const ImWchar rangesBasic[] = {
0x0020, 0x00FF, // Basic Latin + Latin Supplement
0x03BC, 0x03BC, // micro
0x03C3, 0x03C3, // small sigma
0x2013, 0x2013, // en dash
0x2264, 0x2264, // less-than or equal to
0,
};
static const ImWchar rangesIcons[] = {
ICON_MIN_FA, ICON_MAX_FA,
0
};
ImGuiIO& io = ImGui::GetIO();
ImFontConfig configBasic;
configBasic.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_LightHinting;
ImFontConfig configMerge;
configMerge.MergeMode = true;
configMerge.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_LightHinting;
io.Fonts->Clear();
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 15.0f * scale ), &configBasic, rangesBasic );
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 14.0f * scale ), &configMerge, rangesIcons );
s_fixedWidth = cb_fixedWidth = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FiraCodeRetina_compressed_data, tracy::FiraCodeRetina_compressed_size, round( 15.0f * scale ), &configBasic );
s_bigFont = cb_bigFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 21.0f * scale ), &configBasic );
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 20.0f * scale ), &configMerge, rangesIcons );
s_smallFont = cb_smallFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 10.0f * scale ), &configBasic );
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
}

12
profiler/src/Fonts.hpp Normal file
View File

@ -0,0 +1,12 @@
#ifndef __FONTS_HPP__
#define __FONTS_HPP__
struct ImFont;
extern ImFont* s_bigFont;
extern ImFont* s_smallFont;
extern ImFont* s_fixedWidth;
void LoadFonts( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont );
#endif

View File

@ -48,14 +48,11 @@
#include "../../server/TracyVersion.hpp" #include "../../server/TracyVersion.hpp"
#include "../../server/IconsFontAwesome5.h" #include "../../server/IconsFontAwesome5.h"
#include "misc/freetype/imgui_freetype.h"
#include "font/DroidSans.hpp"
#include "font/FiraCodeRetina.hpp"
#include "font/FontAwesomeSolid.hpp"
#include "icon.hpp" #include "icon.hpp"
#include "ResolvService.hpp" #include "Fonts.hpp"
#include "NativeWindow.hpp"
#include "HttpRequest.hpp" #include "HttpRequest.hpp"
#include "NativeWindow.hpp"
#include "ResolvService.hpp"
static void glfw_error_callback(int error, const char* description) static void glfw_error_callback(int error, const char* description)
{ {
@ -114,9 +111,6 @@ static std::unique_ptr<tracy::UdpListen> broadcastListen;
static std::mutex resolvLock; static std::mutex resolvLock;
static tracy::unordered_flat_map<std::string, std::string> resolvMap; static tracy::unordered_flat_map<std::string, std::string> resolvMap;
static ResolvService resolv( port ); static ResolvService resolv( port );
static ImFont* bigFont;
static ImFont* smallFont;
static ImFont* fixedWidth;
static char addr[1024] = { "127.0.0.1" }; static char addr[1024] = { "127.0.0.1" };
static std::unordered_map<std::string, uint64_t> connHistMap; static std::unordered_map<std::string, uint64_t> connHistMap;
static std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> connHistVec; static std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> connHistVec;
@ -144,41 +138,6 @@ void RunOnMainThread( std::function<void()> cb, bool forceDelay = false )
} }
} }
static void LoadFonts( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont )
{
static const ImWchar rangesBasic[] = {
0x0020, 0x00FF, // Basic Latin + Latin Supplement
0x03BC, 0x03BC, // micro
0x03C3, 0x03C3, // small sigma
0x2013, 0x2013, // en dash
0x2264, 0x2264, // less-than or equal to
0,
};
static const ImWchar rangesIcons[] = {
ICON_MIN_FA, ICON_MAX_FA,
0
};
ImGuiIO& io = ImGui::GetIO();
ImFontConfig configBasic;
configBasic.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_LightHinting;
ImFontConfig configMerge;
configMerge.MergeMode = true;
configMerge.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_LightHinting;
io.Fonts->Clear();
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 15.0f * scale ), &configBasic, rangesBasic );
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 14.0f * scale ), &configMerge, rangesIcons );
fixedWidth = cb_fixedWidth = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FiraCodeRetina_compressed_data, tracy::FiraCodeRetina_compressed_size, round( 15.0f * scale ), &configBasic );
bigFont = cb_bigFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 21.0f * scale ), &configBasic );
io.Fonts->AddFontFromMemoryCompressedTTF( tracy::FontAwesomeSolid_compressed_data, tracy::FontAwesomeSolid_compressed_size, round( 20.0f * scale ), &configMerge, rangesIcons );
smallFont = cb_smallFont = io.Fonts->AddFontFromMemoryCompressedTTF( tracy::DroidSans_compressed_data, tracy::DroidSans_compressed_size, round( 10.0f * scale ), &configBasic );
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
}
static void SetupDPIScale( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont ) static void SetupDPIScale( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont )
{ {
LoadFonts( scale, cb_fixedWidth, cb_bigFont, cb_smallFont ); LoadFonts( scale, cb_fixedWidth, cb_bigFont, cb_smallFont );
@ -399,16 +358,16 @@ int main( int argc, char** argv )
ImGui_ImplGlfw_InitForOpenGL( window, true ); ImGui_ImplGlfw_InitForOpenGL( window, true );
ImGui_ImplOpenGL3_Init( "#version 150" ); ImGui_ImplOpenGL3_Init( "#version 150" );
SetupDPIScale( dpiScale, fixedWidth, bigFont, smallFont ); SetupDPIScale( dpiScale, s_fixedWidth, s_bigFont, s_smallFont );
if( initFileOpen ) if( initFileOpen )
{ {
view = std::make_unique<tracy::View>( RunOnMainThread, *initFileOpen, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, *initFileOpen, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback );
initFileOpen.reset(); initFileOpen.reset();
} }
else if( connectTo ) else if( connectTo )
{ {
view = std::make_unique<tracy::View>( RunOnMainThread, connectTo, port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, connectTo, port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback );
} }
#ifndef TRACY_NO_FILESELECTOR #ifndef TRACY_NO_FILESELECTOR
@ -631,7 +590,7 @@ static void DrawContents()
ImGui::Begin( "Get started", nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse ); ImGui::Begin( "Get started", nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse );
char buf[128]; char buf[128];
sprintf( buf, "Tracy Profiler %i.%i.%i", tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch ); sprintf( buf, "Tracy Profiler %i.%i.%i", tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch );
ImGui::PushFont( bigFont ); ImGui::PushFont( s_bigFont );
tracy::TextCentered( buf ); tracy::TextCentered( buf );
ImGui::PopFont(); ImGui::PopFont();
ImGui::SameLine( ImGui::GetWindowContentRegionMax().x - ImGui::CalcTextSize( ICON_FA_WRENCH ).x - ImGui::GetStyle().FramePadding.x * 2 ); ImGui::SameLine( ImGui::GetWindowContentRegionMax().x - ImGui::CalcTextSize( ICON_FA_WRENCH ).x - ImGui::GetStyle().FramePadding.x * 2 );
@ -642,7 +601,7 @@ static void DrawContents()
bool keepOpenAbout = true; bool keepOpenAbout = true;
if( ImGui::BeginPopupModal( "About Tracy", &keepOpenAbout, ImGuiWindowFlags_AlwaysAutoResize ) ) if( ImGui::BeginPopupModal( "About Tracy", &keepOpenAbout, ImGuiWindowFlags_AlwaysAutoResize ) )
{ {
ImGui::PushFont( bigFont ); ImGui::PushFont( s_bigFont );
tracy::TextCentered( buf ); tracy::TextCentered( buf );
ImGui::PopFont(); ImGui::PopFont();
ImGui::Spacing(); ImGui::Spacing();
@ -788,11 +747,11 @@ static void DrawContents()
{ {
std::string addrPart = std::string( addr, ptr ); std::string addrPart = std::string( addr, ptr );
uint16_t portPart = (uint16_t)atoi( ptr+1 ); uint16_t portPart = (uint16_t)atoi( ptr+1 );
view = std::make_unique<tracy::View>( RunOnMainThread, addrPart.c_str(), portPart, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, addrPart.c_str(), portPart, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback );
} }
else else
{ {
view = std::make_unique<tracy::View>( RunOnMainThread, addr, port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, addr, port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback );
} }
} }
ImGui::SameLine( 0, ImGui::GetTextLineHeight() * 2 ); ImGui::SameLine( 0, ImGui::GetTextLineHeight() * 2 );
@ -813,7 +772,7 @@ static void DrawContents()
loadThread = std::thread( [f] { loadThread = std::thread( [f] {
try try
{ {
view = std::make_unique<tracy::View>( RunOnMainThread, *f, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, *f, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback );
} }
catch( const tracy::UnsupportedVersion& e ) catch( const tracy::UnsupportedVersion& e )
{ {
@ -843,7 +802,7 @@ static void DrawContents()
if( badVer.state != tracy::BadVersionState::Ok ) if( badVer.state != tracy::BadVersionState::Ok )
{ {
if( loadThread.joinable() ) { loadThread.join(); } if( loadThread.joinable() ) { loadThread.join(); }
tracy::BadVersion( badVer, bigFont ); tracy::BadVersion( badVer, s_bigFont );
} }
#endif #endif
@ -946,7 +905,7 @@ static void DrawContents()
} }
if( selected && !loadThread.joinable() ) if( selected && !loadThread.joinable() )
{ {
view = std::make_unique<tracy::View>( RunOnMainThread, v.second.address.c_str(), v.second.port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, v.second.address.c_str(), v.second.port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback );
} }
ImGui::NextColumn(); ImGui::NextColumn();
const auto acttime = ( v.second.activeTime + ( time - v.second.time ) / 1000 ) * 1000000000ll; const auto acttime = ( v.second.activeTime + ( time - v.second.time ) / 1000 ) * 1000000000ll;
@ -997,7 +956,7 @@ static void DrawContents()
} }
else else
{ {
ImGui::PushFont( fixedWidth ); ImGui::PushFont( s_fixedWidth );
ImGui::TextUnformatted( releaseNotes.c_str() ); ImGui::TextUnformatted( releaseNotes.c_str() );
ImGui::PopFont(); ImGui::PopFont();
} }
@ -1038,7 +997,7 @@ static void DrawContents()
} }
if( ImGui::BeginPopupModal( "Loading trace...", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) ) if( ImGui::BeginPopupModal( "Loading trace...", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{ {
ImGui::PushFont( bigFont ); ImGui::PushFont( s_bigFont );
tracy::TextCentered( ICON_FA_HOURGLASS_HALF ); tracy::TextCentered( ICON_FA_HOURGLASS_HALF );
ImGui::PopFont(); ImGui::PopFont();
@ -1115,7 +1074,7 @@ static void DrawContents()
viewShutdown.store( ViewShutdown::False, std::memory_order_relaxed ); viewShutdown.store( ViewShutdown::False, std::memory_order_relaxed );
if( reconnect ) if( reconnect )
{ {
view = std::make_unique<tracy::View>( RunOnMainThread, reconnectAddr.c_str(), reconnectPort, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback ); view = std::make_unique<tracy::View>( RunOnMainThread, reconnectAddr.c_str(), reconnectPort, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, GetMainWindowNative, SetupScaleCallback );
} }
break; break;
default: default:
@ -1124,7 +1083,7 @@ static void DrawContents()
if( ImGui::BeginPopupModal( "Capture cleanup...", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) ) if( ImGui::BeginPopupModal( "Capture cleanup...", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) )
{ {
if( viewShutdown.load( std::memory_order_relaxed ) != ViewShutdown::True ) ImGui::CloseCurrentPopup(); if( viewShutdown.load( std::memory_order_relaxed ) != ViewShutdown::True ) ImGui::CloseCurrentPopup();
ImGui::PushFont( bigFont ); ImGui::PushFont( s_bigFont );
tracy::TextCentered( ICON_FA_BROOM ); tracy::TextCentered( ICON_FA_BROOM );
ImGui::PopFont(); ImGui::PopFont();
animTime += ImGui::GetIO().DeltaTime; animTime += ImGui::GetIO().DeltaTime;