From 3928e2eb96dddfd36a40003321d6648a31c314c7 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 5 Aug 2024 12:39:10 +0200 Subject: [PATCH] Fallback font, configure font in lua --- .gitignore | 2 +- build_file.cpp | 17 ++++++++++++++--- src/platform/render_opengl.cpp | 10 ++++++---- src/text_editor/commands.cpp | 8 -------- src/text_editor/generated.cpp | 12 ++++++++---- src/text_editor/generated_variables.cpp | 4 +++- src/text_editor/lua_api.cpp | 19 ++++++++++++++++++- src/text_editor/text_editor.cpp | 12 ++++-------- src/text_editor/window.cpp | 8 +++++++- 9 files changed, 61 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 37fadcb..749a9f3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ x64/Debug x64/Release .vs/ -src/external/SDL +src/external/SDL/ build/ *.rdbg \ No newline at end of file diff --git a/build_file.cpp b/build_file.cpp index 55e1bd5..feb9d38 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -402,6 +402,8 @@ void GenerateConfig() { style.add({"DrawScrollbar", "1"}); style.add({"IndentSize", "4"}); style.add({"TrimWhitespaceOnSave", "1"}); + style.add({"FontSize", "12"}); + style.add({"Font", "C:/Windows/Fonts/consola.ttf"}); { MA_Scratch scratch; @@ -409,7 +411,10 @@ void GenerateConfig() { { For(gruvbox) sb.add(Fmt("Color %s = %s;", it.name, C(it.value))); For(colors) sb.add(Fmt("Color Color%s = %s;", it.name, C(it.value))); - For(style) sb.add(Fmt("Int Style%s = %s;", it.name, it.value)); + For(style) { + if (CHAR_IsDigit(it.value[0])) sb.add(Fmt("Int Style%s = %s;", it.name, it.value)); + else sb.add(Fmt("String Style%s = \"%s\";", it.name, it.value)); + } } S8_String string = Merge(scratch, sb, "\n"); OS_WriteFile("../src/text_editor/generated_variables.cpp", string); @@ -423,7 +428,10 @@ void GenerateConfig() { For(colors) sb.add(Fmt("Color.%s = %s", it.name, it.value)); sb.add("Style = {}"); - For(style) sb.add(Fmt("Style.%s = %s", it.name, it.value)); + For(style) { + if (CHAR_IsDigit(it.value[0])) sb.add(Fmt("Style.%s = %s", it.name, it.value)); + else sb.add(Fmt("Style.%s = \"%s\"", it.name, it.value)); + } sb.add(LuaScript); } @@ -432,7 +440,10 @@ void GenerateConfig() { sb.add("void ReloadStyle() {"); { For(colors) sb.add(Fmt(" Color%s = GetColor(\"%s\", Color%s);", it.name, it.name, it.name)); - For(style) sb.add(Fmt(" Style%s = GetStyle(\"%s\", Style%s);", it.name, it.name, it.name)); + For(style) { + if (CHAR_IsDigit(it.value[0])) sb.add(Fmt(" Style%s = GetStyleInt(\"%s\", Style%s);", it.name, it.name, it.name)); + else sb.add(Fmt(" Style%s = GetStyleString(\"%s\", Style%s);", it.name, it.name, it.name)); + } } sb.add("}"); diff --git a/src/platform/render_opengl.cpp b/src/platform/render_opengl.cpp index e5c8712..1910076 100644 --- a/src/platform/render_opengl.cpp +++ b/src/platform/render_opengl.cpp @@ -372,9 +372,11 @@ void DrawCircle(Vec2 pos, float radius, Color color) { } } -String FontPath = "C:\\Windows\\Fonts\\consola.ttf"; -void ReloadFont(int32_t size) { - size = ClampBottom(2, size); +Int GetStyleInt(String name, Int default_int); +String GetStyleString(String name, String default_string); +void ReloadFont() { + Int size = StyleFontSize; + size = ClampBottom((Int)2, size); if (MainFont.texture_id) { glDeleteTextures(1, &MainFont.texture_id); Dealloc(&MainFont.glyphs); @@ -383,7 +385,7 @@ void ReloadFont(int32_t size) { Scratch scratch; Atlas atlas = CreateAtlas(scratch, {2048, 2048}); - MainFont = CreateFont(&atlas, size, FontPath); + MainFont = CreateFont(&atlas, (uint32_t)size, StyleFont); { GLint filter = GL_NEAREST; // GL_LINEAR glCreateTextures(GL_TEXTURE_2D, 1, &atlas.texture_id); diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 3a992fb..8d6bb6d 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -461,14 +461,6 @@ bool GlobalCommand(Event event) { run_window_command = false; } - if (Ctrl(SDLK_MINUS)) { - ReloadFont((int32_t)MainFont.size - 1); - run_window_command = false; - } else if (Ctrl(SDLK_EQUALS)) { - ReloadFont((int32_t)MainFont.size + 1); - run_window_command = false; - } - return run_window_command; } diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index 4222a27..03745fc 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -59,6 +59,8 @@ Style.DrawLineNumbers = 1 Style.DrawScrollbar = 1 Style.IndentSize = 4 Style.TrimWhitespaceOnSave = 1 +Style.FontSize = 12 +Style.Font = "C:/Windows/Fonts/consola.ttf" -- @todo: should we rewrite linux paths to windows on windows and vice-versa? @@ -237,8 +239,10 @@ void ReloadStyle() { ColorTitleBarText = GetColor("TitleBarText", ColorTitleBarText); ColorTitleBarBackground = GetColor("TitleBarBackground", ColorTitleBarBackground); ColorTitleBarSelection = GetColor("TitleBarSelection", ColorTitleBarSelection); - StyleDrawLineNumbers = GetStyle("DrawLineNumbers", StyleDrawLineNumbers); - StyleDrawScrollbar = GetStyle("DrawScrollbar", StyleDrawScrollbar); - StyleIndentSize = GetStyle("IndentSize", StyleIndentSize); - StyleTrimWhitespaceOnSave = GetStyle("TrimWhitespaceOnSave", StyleTrimWhitespaceOnSave); + StyleDrawLineNumbers = GetStyleInt("DrawLineNumbers", StyleDrawLineNumbers); + StyleDrawScrollbar = GetStyleInt("DrawScrollbar", StyleDrawScrollbar); + StyleIndentSize = GetStyleInt("IndentSize", StyleIndentSize); + StyleTrimWhitespaceOnSave = GetStyleInt("TrimWhitespaceOnSave", StyleTrimWhitespaceOnSave); + StyleFontSize = GetStyleInt("FontSize", StyleFontSize); + StyleFont = GetStyleString("Font", StyleFont); } \ No newline at end of file diff --git a/src/text_editor/generated_variables.cpp b/src/text_editor/generated_variables.cpp index 9032ef9..814f307 100644 --- a/src/text_editor/generated_variables.cpp +++ b/src/text_editor/generated_variables.cpp @@ -55,4 +55,6 @@ Color ColorTitleBarSelection = GruvboxLight3; Int StyleDrawLineNumbers = 1; Int StyleDrawScrollbar = 1; Int StyleIndentSize = 4; -Int StyleTrimWhitespaceOnSave = 1; \ No newline at end of file +Int StyleTrimWhitespaceOnSave = 1; +Int StyleFontSize = 12; +String StyleFont = "C:/Windows/Fonts/consola.ttf"; \ No newline at end of file diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 738f845..30ddc36 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -182,7 +182,7 @@ void InitLua() { } } -Int GetStyle(String name, Int default_int) { +Int GetStyleInt(String name, Int default_int) { Int result = default_int; lua_getglobal(LuaState, "Style"); defer { lua_pop(LuaState, 1); }; @@ -198,6 +198,22 @@ Int GetStyle(String name, Int default_int) { return result; } +String GetStyleString(String name, String default_string) { + String result = default_string; + lua_getglobal(LuaState, "Style"); + defer { lua_pop(LuaState, 1); }; + if (lua_istable(LuaState, -1)) { + lua_pushlstring(LuaState, name.data, name.len); + lua_gettable(LuaState, -2); + defer { lua_pop(LuaState, 1); }; + if (lua_isstring(LuaState, -1)) { + const char *string = lua_tostring(LuaState, -1); + result = Copy(Perm, string); + } + } + return result; +} + Color GetColor(String name, Color default_color) { Color result = default_color; lua_getglobal(LuaState, "Color"); @@ -266,6 +282,7 @@ void ReloadLuaConfig() { lua_pop(LuaState, 1); } ReloadStyle(); + ReloadFont(); ForItem(window, Windows) { if (!window.visible || window.absolute_position || window.is_title_bar) continue; window.draw_scrollbar = StyleDrawScrollbar; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 482556e..95a3050 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -19,9 +19,9 @@ bool IsInFullscreen; int FullScreenSizeX, FullScreenSizeY; int FullScreenPositionX, FullScreenPositionY; +#include "generated_variables.cpp" #include "platform/font.cpp" #include "platform/render_opengl.cpp" -#include "generated_variables.cpp" #include "text_editor.h" #include "buffer_helpers.cpp" @@ -282,16 +282,12 @@ int main() if (scale != 1.0f) DPIScale = scale; } + InitScratchBuffer(); InitRender(); - ReloadFont(16); InitLua(); - - Allocator sys_allocator = GetSystemAllocator(); - Buffer *null_buffer = CreateBuffer(sys_allocator, "*scratch*"); - View *null_view = CreateView(null_buffer->id); - - InitWindows(null_view); + ReloadFont(); ReloadLuaConfig(); + InitWindows(); while (AppIsRunning) { FrameID += 1; diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 7b62e8c..02cdaa6 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -109,7 +109,13 @@ bool ToggleVisibility(Window *window) { return visible; } -void InitWindows(View *null_view) { +void InitScratchBuffer() { + Allocator sys_allocator = GetSystemAllocator(); + Buffer *null_buffer = CreateBuffer(sys_allocator, "*scratch*"); + View *null_view = CreateView(null_buffer->id); +} + +void InitWindows() { Allocator sys_allocator = Perm; {