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
.vs/
src/external/SDL
src/external/SDL/
build/
*.rdbg

View File

@@ -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("}");

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -56,3 +56,5 @@ Int StyleDrawLineNumbers = 1;
Int StyleDrawScrollbar = 1;
Int StyleIndentSize = 4;
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;
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;

View File

@@ -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;

View File

@@ -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;
{