Stashing fonts, start font registry.

This commit is contained in:
2025-12-02 19:05:08 -08:00
parent 49fa7ff8a7
commit 09e4cd7ec6
34 changed files with 111742 additions and 28834 deletions
+32 -19
View File
@@ -88,6 +88,19 @@ if (KTE_ENABLE_TREESITTER)
TreeSitterHighlighter.cc) TreeSitterHighlighter.cc)
endif () endif ()
set(FONT_SOURCES
fonts/Font.cc
)
set(GUI_SOURCES
${FONT_SOURCES}
GUIConfig.cc
GUIRenderer.cc
GUIInputHandler.cc
GUIFrontend.cc
)
set(COMMON_SOURCES set(COMMON_SOURCES
GapBuffer.cc GapBuffer.cc
PieceTable.cc PieceTable.cc
@@ -109,7 +122,6 @@ set(COMMON_SOURCES
${SYNTAX_SOURCES} ${SYNTAX_SOURCES}
) )
set(SYNTAX_HEADERS set(SYNTAX_HEADERS
syntax/GoHighlighter.h syntax/GoHighlighter.h
syntax/HighlighterEngine.h syntax/HighlighterEngine.h
@@ -142,6 +154,12 @@ set(THEME_HEADERS
themes/Nord.h themes/Nord.h
) )
set(FONT_HEADERS
fonts/Font.h
fonts/FontRegistry.h
fonts/FontRegistry.h
)
set(COMMON_HEADERS set(COMMON_HEADERS
GapBuffer.h GapBuffer.h
PieceTable.h PieceTable.h
@@ -166,7 +184,15 @@ set(COMMON_HEADERS
Highlight.h Highlight.h
${SYNTAX_HEADERS} ${SYNTAX_HEADERS}
)
set(GUI_HEADERS
${THEME_HEADERS} ${THEME_HEADERS}
${FONT_HEADERS}
GUIConfig.h
GUIRenderer.h
GUIInputHandler.h
GUIFrontend.h
) )
# kte (terminal-first) executable # kte (terminal-first) executable
@@ -244,16 +270,7 @@ if (${BUILD_GUI})
# io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls # io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
# io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls # io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
target_sources(kte PRIVATE target_sources(kte PRIVATE ${COMMON_SOURCES} ${COMMON_HEADERS})
Font.h
GUIConfig.cc
GUIConfig.h
GUIRenderer.cc
GUIRenderer.h
GUIInputHandler.cc
GUIInputHandler.h
GUIFrontend.cc
GUIFrontend.h)
target_compile_definitions(kte PRIVATE KTE_BUILD_GUI=1) target_compile_definitions(kte PRIVATE KTE_BUILD_GUI=1)
target_link_libraries(kte imgui) target_link_libraries(kte imgui)
@@ -261,15 +278,11 @@ if (${BUILD_GUI})
add_executable(kge add_executable(kge
main.cc main.cc
${COMMON_SOURCES} ${COMMON_SOURCES}
${GUI_SOURCES}
${COMMON_HEADERS} ${COMMON_HEADERS}
GUIConfig.cc ${GUI_HEADERS}
GUIConfig.h
GUIRenderer.cc )
GUIRenderer.h
GUIInputHandler.cc
GUIInputHandler.h
GUIFrontend.cc
GUIFrontend.h)
target_compile_definitions(kge PRIVATE KTE_BUILD_GUI=1 KTE_DEFAULT_GUI=1 KTE_FONT_SIZE=${KTE_FONT_SIZE}) target_compile_definitions(kge PRIVATE KTE_BUILD_GUI=1 KTE_DEFAULT_GUI=1 KTE_FONT_SIZE=${KTE_FONT_SIZE})
if (KTE_UNDO_DEBUG) if (KTE_UNDO_DEBUG)
target_compile_definitions(kge PRIVATE KTE_UNDO_DEBUG=1) target_compile_definitions(kge PRIVATE KTE_UNDO_DEBUG=1)
-2985
View File
File diff suppressed because it is too large Load Diff
+14 -17
View File
@@ -1,22 +1,22 @@
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm> #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <string>
#include <imgui.h>
#include <SDL.h> #include <SDL.h>
#include <SDL_opengl.h> #include <SDL_opengl.h>
#include <imgui.h>
#include <backends/imgui_impl_sdl2.h>
#include <backends/imgui_impl_opengl3.h> #include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_sdl2.h>
#include "Editor.h"
#include "Command.h"
#include "GUIFrontend.h" #include "GUIFrontend.h"
#include <filesystem> #include "Command.h"
#include "Font.h" // embedded default font (DefaultFontRegular) #include "Editor.h"
#include "GUIConfig.h" #include "GUIConfig.h"
#include "GUITheme.h" #include "GUITheme.h"
#include "fonts/Font.h" // embedded default font (DefaultFont)
#include "syntax/HighlighterRegistry.h" #include "syntax/HighlighterRegistry.h"
#include "syntax/NullHighlighter.h" #include "syntax/NullHighlighter.h"
@@ -25,7 +25,7 @@
#define KTE_FONT_SIZE 16.0f #define KTE_FONT_SIZE 16.0f
#endif #endif
static const char *kGlslVersion = "#version 150"; // GL 3.2 core (macOS compatible) static auto kGlslVersion = "#version 150"; // GL 3.2 core (macOS compatible)
bool bool
GUIFrontend::Init(Editor &ed) GUIFrontend::Init(Editor &ed)
@@ -324,13 +324,13 @@ GUIFrontend::Shutdown()
bool bool
GUIFrontend::LoadGuiFont_(const char * /*path*/, float size_px) GUIFrontend::LoadGuiFont_(const char * /*path*/, const float size_px)
{ {
const ImGuiIO &io = ImGui::GetIO(); const ImGuiIO &io = ImGui::GetIO();
io.Fonts->Clear(); io.Fonts->Clear();
const ImFont *font = io.Fonts->AddFontFromMemoryCompressedTTF( const ImFont *font = io.Fonts->AddFontFromMemoryCompressedTTF(
kte::Fonts::DefaultFontRegularCompressedData, kte::Fonts::DefaultFontData,
kte::Fonts::DefaultFontRegularCompressedSize, kte::Fonts::DefaultFontSize,
size_px); size_px);
if (!font) { if (!font) {
font = io.Fonts->AddFontDefault(); font = io.Fonts->AddFontDefault();
@@ -339,6 +339,3 @@ GUIFrontend::LoadGuiFont_(const char * /*path*/, float size_px)
io.Fonts->Build(); io.Fonts->Build();
return true; return true;
} }
// No runtime font reload or system font resolution in this simplified build.
+11881
View File
File diff suppressed because it is too large Load Diff
+5898 -2962
View File
File diff suppressed because it is too large Load Diff
+1 -2
View File
@@ -1,3 +1,2 @@
#pragma once #pragma once
namespace kte::Fonts { namespace kte::Fonts::FiraCode {}
}}
+2 -2
View File
@@ -1,5 +1,5 @@
#pragma once #pragma once
namespace kte::Fonts { namespace kte::Fonts::Go {
// File: 'Go/GoMono-BoldItalic.ttf' (188176 bytes) // File: 'Go/GoMono-BoldItalic.ttf' (188176 bytes)
@@ -8610,4 +8610,4 @@ static const unsigned int DefaultFontRegularCompressedData[101204/4] =
0x440002b1, 0x066405b3, 0x00444400, 0x5dfa0500, 0x00ae3bb9, 0x440002b1, 0x066405b3, 0x00444400, 0x5dfa0500, 0x00ae3bb9,
}; };
}} }
+13651 -6838
View File
File diff suppressed because it is too large Load Diff
+5624 -2820
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+4
View File
@@ -0,0 +1,4 @@
These are the available fonts. Build the binary_to_compressed_c
in ../imgui/misc/fonts and put it on your path. These will build
static headers containing the fonts to be placed into fonts/
as part of the font registry.
+1185 -598
View File
File diff suppressed because it is too large Load Diff
+1882 -946
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+5566 -2796
View File
File diff suppressed because it is too large Load Diff
+18 -11
View File
@@ -1,12 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import pdb
import re import re
import sys
import subprocess import subprocess
import sys
DEFAULT_FONTS = [ DEFAULT_FONTS = [
'B612_Mono', 'B612Mono',
'BrassMono', 'BrassMono',
'BrassMonoCode', 'BrassMonoCode',
'FiraCode', 'FiraCode',
@@ -18,16 +19,19 @@ DEFAULT_FONTS = [
'Syne', 'Syne',
] ]
def generate_font(header_file, path): def generate_font(header_file, path):
symbol_name=os.path.splitext(os.path.basename(path))[0] symbol_name = os.path.splitext(os.path.basename(path))[0]
symbol_name=symbol_name.replace('-', '_') symbol_name = symbol_name.replace('-', '_')
symbol_name=symbol_name.split('_')[1] symbol_name = symbol_name.split('_', 2)[1]
output = subprocess.check_output( output = subprocess.check_output(
f'binary_to_compressed_c "{path}" DefaultFont{symbol_name}', f'binary_to_compressed_c "{path}" DefaultFont{symbol_name}',
shell=True) shell=True)
header_file.write('\n\n') header_file.write('\n\n')
header_file.write(output.decode('utf-8')) header_file.write(output.decode('utf-8'))
def generate_header(header, guard, files): def generate_header(header, guard, files):
try: try:
os.remove(header) os.remove(header)
@@ -37,26 +41,28 @@ def generate_header(header, guard, files):
raise raise
with open(header, 'wt') as header_file: with open(header, 'wt') as header_file:
header_file.write(f"""#pragma once header_file.write(f"""#pragma once
namespace kte::Fonts {{ namespace kte::Fonts::{header.removesuffix('.h')} {{
""") """)
for file in files: for file in files:
generate_font(header_file, file) generate_font(header_file, file)
header_file.write('}}'); header_file.write('}');
subprocess.call("sed -i '' -e 's/_compressed_size/CompressedSize/' "+ subprocess.call("sed -i '' -e 's/_compressed_size/CompressedSize/' " +
f"{header_file.name}", shell=True) f"{header_file.name}", shell=True)
subprocess.call("sed -i '' -e 's/_compressed_data/CompressedData/' " + subprocess.call("sed -i '' -e 's/_compressed_data/CompressedData/' " +
f"{header_file.name}", shell=True) f"{header_file.name}", shell=True)
def generate_dir(path): def generate_dir(path):
filelist = [os.path.join(path, file) for file in os.listdir(path) filelist = [os.path.join(path, file) for file in os.listdir(path)
if file.endswith('ttf')] if file.endswith('ttf')]
namespace = f'kte::{path}' namespace = f'kte::Fonts::{path}'
header = f"{path.replace('-', '_')}.h" header = f"{path.replace('-', '_')}.h"
generate_header(header, namespace, filelist) generate_header(header, namespace, filelist)
def main(fonts=None): def main(fonts=None):
if fonts is None: if fonts is None:
fonts = DEFAULT_FONTS fonts = DEFAULT_FONTS
@@ -64,6 +70,7 @@ def main(fonts=None):
for font in fonts: for font in fonts:
generate_dir(font) generate_dir(font)
if __name__ == '__main__': if __name__ == '__main__':
fonts = None fonts = None
if len(sys.argv) > 1: if len(sys.argv) > 1:
+11881
View File
File diff suppressed because it is too large Load Diff
+5589
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
#pragma once
namespace kte::Fonts::FiraCode {}
+22
View File
@@ -0,0 +1,22 @@
#include "Font.h"
#include "imgui.h"
void
Font::Load(const float size) const
{
const ImGuiIO &io = ImGui::GetIO();
io.Fonts->Clear();
const ImFont *font = io.Fonts->AddFontFromMemoryCompressedTTF(
this->data_,
this->size_,
size);
if (!font) {
font = io.Fonts->AddFontDefault();
}
(void) font;
io.Fonts->Build();
}
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <string>
#include <utility>
#include "BrassMonoCode.h"
namespace kte::Fonts {
static const unsigned int DefaultFontSize = DefaultFontBoldCompressedSize;
static const unsigned int *DefaultFontData = DefaultFontBoldCompressedData;
}
class Font {
public:
Font(std::string name, unsigned int *data, const unsigned int size)
: name_(std::move(name)),
data_(data),
size_(size) {}
std::string Name()
{
return name_;
}
void Load(float size) const;
private:
std::string name_;
unsigned int *data_{nullptr};
unsigned int size_{0};
};
+66
View File
@@ -0,0 +1,66 @@
#pragma once
#include <cassert>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include "Font.h"
// Forward declaration
// class Font;
class FontRegistry {
public:
// Get the global instance
static FontRegistry &Instance()
{
static FontRegistry instance;
return instance;
}
// Register a font (usually done at startup or static initialization)
void Register(std::unique_ptr<Font> font)
{
std::lock_guard lock(mutex_);
auto name = font->Name();
assert(fonts_.find(name) == fonts_.end() && "Font already registered!");
fonts_[std::move(name)] = std::move(font);
}
// Get a font by name (const access)
const Font *Get(const std::string &name) const
{
std::lock_guard lock(mutex_);
const auto it = fonts_.find(name);
return it != fonts_.end() ? it->second.get() : nullptr;
}
// Convenience: load a font by name and size
bool LoadFont(const std::string &name, const float size) const
{
if (auto *font = Get(name)) {
font->Load(size);
return true;
}
return false;
}
// Check if font exists
bool HasFont(const std::string &name) const
{
std::lock_guard lock(mutex_);
return fonts_.count(name) > 0;
}
private:
FontRegistry() = default;
mutable std::mutex mutex_;
std::unordered_map<std::string, std::unique_ptr<Font> > fonts_;
};
+8613
View File
File diff suppressed because it is too large Load Diff
+13674
View File
File diff suppressed because it is too large Load Diff
+5635
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+1190
View File
File diff suppressed because it is too large Load Diff
+1887
View File
File diff suppressed because it is too large Load Diff