Fallback font, configure font in lua

This commit is contained in:
Krzosa Karol
2024-08-05 12:39:10 +02:00
parent c4bc48bba1
commit 3928e2eb96
9 changed files with 61 additions and 31 deletions

2
.gitignore vendored
View File

@@ -2,6 +2,6 @@ x64/Debug
x64/Release x64/Release
.vs/ .vs/
src/external/SDL src/external/SDL/
build/ build/
*.rdbg *.rdbg

View File

@@ -402,6 +402,8 @@ void GenerateConfig() {
style.add({"DrawScrollbar", "1"}); style.add({"DrawScrollbar", "1"});
style.add({"IndentSize", "4"}); style.add({"IndentSize", "4"});
style.add({"TrimWhitespaceOnSave", "1"}); style.add({"TrimWhitespaceOnSave", "1"});
style.add({"FontSize", "12"});
style.add({"Font", "C:/Windows/Fonts/consola.ttf"});
{ {
MA_Scratch scratch; MA_Scratch scratch;
@@ -409,7 +411,10 @@ void GenerateConfig() {
{ {
For(gruvbox) sb.add(Fmt("Color %s = %s;", it.name, C(it.value))); 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(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"); S8_String string = Merge(scratch, sb, "\n");
OS_WriteFile("../src/text_editor/generated_variables.cpp", string); 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)); For(colors) sb.add(Fmt("Color.%s = %s", it.name, it.value));
sb.add("Style = {}"); 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); sb.add(LuaScript);
} }
@@ -432,7 +440,10 @@ void GenerateConfig() {
sb.add("void ReloadStyle() {"); sb.add("void ReloadStyle() {");
{ {
For(colors) sb.add(Fmt(" Color%s = GetColor(\"%s\", Color%s);", it.name, it.name, it.name)); 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("}"); sb.add("}");

View File

@@ -372,9 +372,11 @@ void DrawCircle(Vec2 pos, float radius, Color color) {
} }
} }
String FontPath = "C:\\Windows\\Fonts\\consola.ttf"; Int GetStyleInt(String name, Int default_int);
void ReloadFont(int32_t size) { String GetStyleString(String name, String default_string);
size = ClampBottom(2, size); void ReloadFont() {
Int size = StyleFontSize;
size = ClampBottom((Int)2, size);
if (MainFont.texture_id) { if (MainFont.texture_id) {
glDeleteTextures(1, &MainFont.texture_id); glDeleteTextures(1, &MainFont.texture_id);
Dealloc(&MainFont.glyphs); Dealloc(&MainFont.glyphs);
@@ -383,7 +385,7 @@ void ReloadFont(int32_t size) {
Scratch scratch; Scratch scratch;
Atlas atlas = CreateAtlas(scratch, {2048, 2048}); Atlas atlas = CreateAtlas(scratch, {2048, 2048});
MainFont = CreateFont(&atlas, size, FontPath); MainFont = CreateFont(&atlas, (uint32_t)size, StyleFont);
{ {
GLint filter = GL_NEAREST; // GL_LINEAR GLint filter = GL_NEAREST; // GL_LINEAR
glCreateTextures(GL_TEXTURE_2D, 1, &atlas.texture_id); glCreateTextures(GL_TEXTURE_2D, 1, &atlas.texture_id);

View File

@@ -461,14 +461,6 @@ bool GlobalCommand(Event event) {
run_window_command = false; 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; return run_window_command;
} }

View File

@@ -59,6 +59,8 @@ Style.DrawLineNumbers = 1
Style.DrawScrollbar = 1 Style.DrawScrollbar = 1
Style.IndentSize = 4 Style.IndentSize = 4
Style.TrimWhitespaceOnSave = 1 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? -- @todo: should we rewrite linux paths to windows on windows and vice-versa?
@@ -237,8 +239,10 @@ void ReloadStyle() {
ColorTitleBarText = GetColor("TitleBarText", ColorTitleBarText); ColorTitleBarText = GetColor("TitleBarText", ColorTitleBarText);
ColorTitleBarBackground = GetColor("TitleBarBackground", ColorTitleBarBackground); ColorTitleBarBackground = GetColor("TitleBarBackground", ColorTitleBarBackground);
ColorTitleBarSelection = GetColor("TitleBarSelection", ColorTitleBarSelection); ColorTitleBarSelection = GetColor("TitleBarSelection", ColorTitleBarSelection);
StyleDrawLineNumbers = GetStyle("DrawLineNumbers", StyleDrawLineNumbers); StyleDrawLineNumbers = GetStyleInt("DrawLineNumbers", StyleDrawLineNumbers);
StyleDrawScrollbar = GetStyle("DrawScrollbar", StyleDrawScrollbar); StyleDrawScrollbar = GetStyleInt("DrawScrollbar", StyleDrawScrollbar);
StyleIndentSize = GetStyle("IndentSize", StyleIndentSize); StyleIndentSize = GetStyleInt("IndentSize", StyleIndentSize);
StyleTrimWhitespaceOnSave = GetStyle("TrimWhitespaceOnSave", StyleTrimWhitespaceOnSave); StyleTrimWhitespaceOnSave = GetStyleInt("TrimWhitespaceOnSave", StyleTrimWhitespaceOnSave);
StyleFontSize = GetStyleInt("FontSize", StyleFontSize);
StyleFont = GetStyleString("Font", StyleFont);
} }

View File

@@ -55,4 +55,6 @@ Color ColorTitleBarSelection = GruvboxLight3;
Int StyleDrawLineNumbers = 1; Int StyleDrawLineNumbers = 1;
Int StyleDrawScrollbar = 1; Int StyleDrawScrollbar = 1;
Int StyleIndentSize = 4; Int StyleIndentSize = 4;
Int StyleTrimWhitespaceOnSave = 1; Int StyleTrimWhitespaceOnSave = 1;
Int StyleFontSize = 12;
String StyleFont = "C:/Windows/Fonts/consola.ttf";

View File

@@ -182,7 +182,7 @@ void InitLua() {
} }
} }
Int GetStyle(String name, Int default_int) { Int GetStyleInt(String name, Int default_int) {
Int result = default_int; Int result = default_int;
lua_getglobal(LuaState, "Style"); lua_getglobal(LuaState, "Style");
defer { lua_pop(LuaState, 1); }; defer { lua_pop(LuaState, 1); };
@@ -198,6 +198,22 @@ Int GetStyle(String name, Int default_int) {
return result; 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 GetColor(String name, Color default_color) {
Color result = default_color; Color result = default_color;
lua_getglobal(LuaState, "Color"); lua_getglobal(LuaState, "Color");
@@ -266,6 +282,7 @@ void ReloadLuaConfig() {
lua_pop(LuaState, 1); lua_pop(LuaState, 1);
} }
ReloadStyle(); ReloadStyle();
ReloadFont();
ForItem(window, Windows) { ForItem(window, Windows) {
if (!window.visible || window.absolute_position || window.is_title_bar) continue; if (!window.visible || window.absolute_position || window.is_title_bar) continue;
window.draw_scrollbar = StyleDrawScrollbar; window.draw_scrollbar = StyleDrawScrollbar;

View File

@@ -19,9 +19,9 @@ bool IsInFullscreen;
int FullScreenSizeX, FullScreenSizeY; int FullScreenSizeX, FullScreenSizeY;
int FullScreenPositionX, FullScreenPositionY; int FullScreenPositionX, FullScreenPositionY;
#include "generated_variables.cpp"
#include "platform/font.cpp" #include "platform/font.cpp"
#include "platform/render_opengl.cpp" #include "platform/render_opengl.cpp"
#include "generated_variables.cpp"
#include "text_editor.h" #include "text_editor.h"
#include "buffer_helpers.cpp" #include "buffer_helpers.cpp"
@@ -282,16 +282,12 @@ int main()
if (scale != 1.0f) DPIScale = scale; if (scale != 1.0f) DPIScale = scale;
} }
InitScratchBuffer();
InitRender(); InitRender();
ReloadFont(16);
InitLua(); InitLua();
ReloadFont();
Allocator sys_allocator = GetSystemAllocator();
Buffer *null_buffer = CreateBuffer(sys_allocator, "*scratch*");
View *null_view = CreateView(null_buffer->id);
InitWindows(null_view);
ReloadLuaConfig(); ReloadLuaConfig();
InitWindows();
while (AppIsRunning) { while (AppIsRunning) {
FrameID += 1; FrameID += 1;

View File

@@ -109,7 +109,13 @@ bool ToggleVisibility(Window *window) {
return visible; 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; Allocator sys_allocator = Perm;
{ {