Stashing fonts, start font registry.

This commit is contained in:
2025-12-02 17:18:43 -08:00
parent 49fa7ff8a7
commit 09e4cd7ec6
34 changed files with 111742 additions and 28834 deletions

View File

@@ -88,6 +88,19 @@ if (KTE_ENABLE_TREESITTER)
TreeSitterHighlighter.cc)
endif ()
set(FONT_SOURCES
fonts/Font.cc
)
set(GUI_SOURCES
${FONT_SOURCES}
GUIConfig.cc
GUIRenderer.cc
GUIInputHandler.cc
GUIFrontend.cc
)
set(COMMON_SOURCES
GapBuffer.cc
PieceTable.cc
@@ -109,7 +122,6 @@ set(COMMON_SOURCES
${SYNTAX_SOURCES}
)
set(SYNTAX_HEADERS
syntax/GoHighlighter.h
syntax/HighlighterEngine.h
@@ -142,6 +154,12 @@ set(THEME_HEADERS
themes/Nord.h
)
set(FONT_HEADERS
fonts/Font.h
fonts/FontRegistry.h
fonts/FontRegistry.h
)
set(COMMON_HEADERS
GapBuffer.h
PieceTable.h
@@ -166,7 +184,15 @@ set(COMMON_HEADERS
Highlight.h
${SYNTAX_HEADERS}
)
set(GUI_HEADERS
${THEME_HEADERS}
${FONT_HEADERS}
GUIConfig.h
GUIRenderer.h
GUIInputHandler.h
GUIFrontend.h
)
# kte (terminal-first) executable
@@ -244,16 +270,7 @@ if (${BUILD_GUI})
# io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
# io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
target_sources(kte PRIVATE
Font.h
GUIConfig.cc
GUIConfig.h
GUIRenderer.cc
GUIRenderer.h
GUIInputHandler.cc
GUIInputHandler.h
GUIFrontend.cc
GUIFrontend.h)
target_sources(kte PRIVATE ${COMMON_SOURCES} ${COMMON_HEADERS})
target_compile_definitions(kte PRIVATE KTE_BUILD_GUI=1)
target_link_libraries(kte imgui)
@@ -261,15 +278,11 @@ if (${BUILD_GUI})
add_executable(kge
main.cc
${COMMON_SOURCES}
${GUI_SOURCES}
${COMMON_HEADERS}
GUIConfig.cc
GUIConfig.h
GUIRenderer.cc
GUIRenderer.h
GUIInputHandler.cc
GUIInputHandler.h
GUIFrontend.cc
GUIFrontend.h)
${GUI_HEADERS}
)
target_compile_definitions(kge PRIVATE KTE_BUILD_GUI=1 KTE_DEFAULT_GUI=1 KTE_FONT_SIZE=${KTE_FONT_SIZE})
if (KTE_UNDO_DEBUG)
target_compile_definitions(kge PRIVATE KTE_UNDO_DEBUG=1)

2985
Font.h

File diff suppressed because it is too large Load Diff

View File

@@ -1,22 +1,22 @@
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <string>
#include <imgui.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <imgui.h>
#include <backends/imgui_impl_sdl2.h>
#include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_sdl2.h>
#include "Editor.h"
#include "Command.h"
#include "GUIFrontend.h"
#include <filesystem>
#include "Font.h" // embedded default font (DefaultFontRegular)
#include "Command.h"
#include "Editor.h"
#include "GUIConfig.h"
#include "GUITheme.h"
#include "fonts/Font.h" // embedded default font (DefaultFont)
#include "syntax/HighlighterRegistry.h"
#include "syntax/NullHighlighter.h"
@@ -25,7 +25,7 @@
#define KTE_FONT_SIZE 16.0f
#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
GUIFrontend::Init(Editor &ed)
@@ -324,13 +324,13 @@ GUIFrontend::Shutdown()
bool
GUIFrontend::LoadGuiFont_(const char * /*path*/, float size_px)
GUIFrontend::LoadGuiFont_(const char * /*path*/, const float size_px)
{
const ImGuiIO &io = ImGui::GetIO();
io.Fonts->Clear();
const ImFont *font = io.Fonts->AddFontFromMemoryCompressedTTF(
kte::Fonts::DefaultFontRegularCompressedData,
kte::Fonts::DefaultFontRegularCompressedSize,
kte::Fonts::DefaultFontData,
kte::Fonts::DefaultFontSize,
size_px);
if (!font) {
font = io.Fonts->AddFontDefault();
@@ -338,7 +338,4 @@ GUIFrontend::LoadGuiFont_(const char * /*path*/, float size_px)
(void) font;
io.Fonts->Build();
return true;
}
// No runtime font reload or system font resolution in this simplified build.
}

11881
ext/fonts/B612Mono.h Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,2 @@
#pragma once
namespace kte::Fonts {
}}
namespace kte::Fonts::FiraCode {}

View File

@@ -1,5 +1,5 @@
#pragma once
namespace kte::Fonts {
namespace kte::Fonts::Go {
// File: 'Go/GoMono-BoldItalic.ttf' (188176 bytes)
@@ -8610,4 +8610,4 @@ static const unsigned int DefaultFontRegularCompressedData[101204/4] =
0x440002b1, 0x066405b3, 0x00444400, 0x5dfa0500, 0x00ae3bb9,
};
}}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

4
ext/fonts/README.txt Normal file
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.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +1,13 @@
#!/usr/bin/env python3
import os
import pdb
import re
import sys
import subprocess
import sys
DEFAULT_FONTS = [
'B612_Mono',
'B612Mono',
'BrassMono',
'BrassMonoCode',
'FiraCode',
@@ -18,16 +19,19 @@ DEFAULT_FONTS = [
'Syne',
]
def generate_font(header_file, path):
symbol_name=os.path.splitext(os.path.basename(path))[0]
symbol_name=symbol_name.replace('-', '_')
symbol_name=symbol_name.split('_')[1]
symbol_name = os.path.splitext(os.path.basename(path))[0]
symbol_name = symbol_name.replace('-', '_')
symbol_name = symbol_name.split('_', 2)[1]
output = subprocess.check_output(
f'binary_to_compressed_c "{path}" DefaultFont{symbol_name}',
shell=True)
f'binary_to_compressed_c "{path}" DefaultFont{symbol_name}',
shell=True)
header_file.write('\n\n')
header_file.write(output.decode('utf-8'))
def generate_header(header, guard, files):
try:
os.remove(header)
@@ -37,26 +41,28 @@ def generate_header(header, guard, files):
raise
with open(header, 'wt') as header_file:
header_file.write(f"""#pragma once
namespace kte::Fonts {{
namespace kte::Fonts::{header.removesuffix('.h')} {{
""")
for file in files:
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)
subprocess.call("sed -i '' -e 's/_compressed_data/CompressedData/' " +
subprocess.call("sed -i '' -e 's/_compressed_data/CompressedData/' " +
f"{header_file.name}", shell=True)
def generate_dir(path):
filelist = [os.path.join(path, file) for file in os.listdir(path)
if file.endswith('ttf')]
namespace = f'kte::{path}'
namespace = f'kte::Fonts::{path}'
header = f"{path.replace('-', '_')}.h"
generate_header(header, namespace, filelist)
def main(fonts=None):
if fonts is None:
fonts = DEFAULT_FONTS
@@ -64,6 +70,7 @@ def main(fonts=None):
for font in fonts:
generate_dir(font)
if __name__ == '__main__':
fonts = None
if len(sys.argv) > 1:

11881
fonts/B612Mono.h Normal file

File diff suppressed because it is too large Load Diff

5589
fonts/BrassMono.h Normal file

File diff suppressed because it is too large Load Diff

5921
fonts/BrassMonoCode.h Normal file

File diff suppressed because it is too large Load Diff

2
fonts/FiraCode.h Normal file
View File

@@ -0,0 +1,2 @@
#pragma once
namespace kte::Fonts::FiraCode {}

22
fonts/Font.cc Normal file
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
fonts/Font.h Normal file
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
fonts/FontRegistry.h Normal file
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
fonts/Go.h Normal file

File diff suppressed because it is too large Load Diff

13674
fonts/IBMPlexMono.h Normal file

File diff suppressed because it is too large Load Diff

5635
fonts/Inconsolata.h Normal file

File diff suppressed because it is too large Load Diff

5740
fonts/InconsolataExpanded.h Normal file

File diff suppressed because it is too large Load Diff

1190
fonts/ShareTech.h Normal file

File diff suppressed because it is too large Load Diff

1887
fonts/Syne.h Normal file

File diff suppressed because it is too large Load Diff