From 8d7f6dea1113f48f812f734b4bba510ef35b3f27 Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 18 Jul 2023 16:32:29 -0400 Subject: start "multifonts" --- config.c | 2 ++ main.c | 1 + ted.c | 40 ++++++++++++++++++++++++++++++++++------ ted.cfg | 2 ++ ted.h | 4 ++++ util.c | 23 ++++++++++++++++++++++- util.h | 6 ++++++ 7 files changed, 71 insertions(+), 7 deletions(-) diff --git a/config.c b/config.c index e70bf98..6a14689 100644 --- a/config.c +++ b/config.c @@ -135,6 +135,8 @@ static const SettingString settings_string[] = { {"lsp-configuration", settings_zero.lsp_configuration, sizeof settings_zero.lsp_configuration, true}, {"comment-start", settings_zero.comment_start, sizeof settings_zero.comment_start, true}, {"comment-end", settings_zero.comment_end, sizeof settings_zero.comment_end, true}, + {"font", settings_zero.font, sizeof settings_zero.font, false}, + {"font-bold", settings_zero.font_bold, sizeof settings_zero.font_bold, false}, }; static const SettingKeyCombo settings_key_combo[] = { {"hover-key", &settings_zero.hover_key, true}, diff --git a/main.c b/main.c index 8fa816c..7a3f986 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ /* TODO: +- kerning - clean up valgrind_suppresions.txt FUTURE FEATURES: - autodetect indentation (tabs vs spaces) diff --git a/ted.c b/ted.c index 73a5969..19d360c 100644 --- a/ted.c +++ b/ted.c @@ -287,12 +287,13 @@ Status ted_get_file(Ted const *ted, const char *name, char *out, size_t outsz) { return false; } -static Font *ted_load_font(Ted *ted, const char *filename) { +static Font *ted_load_single_font(Ted *ted, const char *filename) { char path[TED_PATH_MAX]; if (!ted_get_file(ted, filename, path, sizeof path)) { - die("Couldn't find font file %s", filename); + ted_error(ted, "Couldn't find font file '%s'", filename); + return NULL; } - + arr_foreach_ptr(ted->all_fonts, LoadedFont, f) { if (paths_eq(path, f->path)) return f->font; @@ -300,20 +301,47 @@ static Font *ted_load_font(Ted *ted, const char *filename) { Font *font = text_font_load(path, ted_active_settings(ted)->text_size); if (!font) { - die("Couldn't load font %s: %s\n", path, text_get_err()); + ted_error(ted, "Couldn't load font '%s': %s\n", path, text_get_err()); + return NULL; } LoadedFont *f = arr_addp(ted->all_fonts); f->path = str_dup(path); f->font = font; + return font; +} + +static Font *ted_load_multifont(Ted *ted, const char *filenames) { + char filename[TED_PATH_MAX]; + Font *font = NULL; + + while (*filenames) { + while (*filenames == ',') ++filenames; + size_t len = strcspn(filenames, ","); + strn_cpy(filename, sizeof filename, filenames, len); + str_trim(filename); + if (*filename) { + font = ted_load_single_font(ted, filename); + } + filenames += len; + } return font; } void ted_load_fonts(Ted *ted) { ted_free_fonts(ted); - ted->font = ted_load_font(ted, "assets/font.ttf"); - ted->font_bold = ted_load_font(ted, "assets/font-bold.ttf"); + Settings *settings = ted_active_settings(ted); + ted->font = ted_load_multifont(ted, settings->font); + if (!ted->font) { + ted->font = ted_load_multifont(ted, "assets/font.ttf"); + if (!ted->font) + die("Couldn't load default font: %s.", ted->message); + } + ted->font_bold = ted_load_multifont(ted, settings->font_bold); + if (!ted->font_bold) { + ted->font_bold = ted->font; + } } void ted_change_text_size(Ted *ted, float new_size) { diff --git a/ted.cfg b/ted.cfg index 268f09b..016cbc6 100644 --- a/ted.cfg +++ b/ted.cfg @@ -14,6 +14,8 @@ scrolloff = 5 # if you do a bunch of typing, then undo, it will generally # undo the past this many seconds of editing. undo-save-time = 6 +font = assets/font.ttf +font-bold = assets/font-bold.ttf text-size = 18 border-thickness = 1 max-menu-width = 600 diff --git a/ted.h b/ted.h index e3e75e1..23c086e 100644 --- a/ted.h +++ b/ted.h @@ -301,6 +301,10 @@ typedef struct { char build_command[1024]; /// Default build command for if `Cargo.toml`, `Makefile`, etc. do not exist. char build_default_command[1024]; + /// Comma separated list of paths to font files. + char font[4096]; + /// Comma separated list of paths to bold font files. + char font_bold[4096]; LanguageExtension *language_extensions; /// dynamic array, sorted by KEY_COMBO(modifier, key) KeyAction *key_actions; diff --git a/util.c b/util.c index 02c5fc7..96622ec 100644 --- a/util.c +++ b/util.c @@ -12,6 +12,7 @@ #error "Unrecognized operating system." #endif #include +#include // on 16-bit systems, this is 16383. on 32/64-bit systems, this is 1073741823 // it is unusual to have a string that long. @@ -235,11 +236,31 @@ void strn_cpy(char *dst, size_t dst_sz, const char *src, size_t src_len) { dst[n] = 0; } -// safer version of strcpy. dst_sz includes a null terminator. void str_cpy(char *dst, size_t dst_sz, const char *src) { strn_cpy(dst, dst_sz, src, SIZE_MAX); } +void str_trim_start(char *str) { + size_t n = strspn(str, "\r\v\t\n\f "); + size_t len = strlen(str); + memmove(str, str + n, len - n); + str[len - n] = '\0'; +} + +void str_trim_end(char *str) { + size_t i = strlen(str); + while (i > 0 && isspace(str[i - 1])) { + str[i - 1] = '\0'; + --i; + } +} + + +void str_trim(char *str) { + str_trim_end(str); + str_trim_start(str); +} + char *a_sprintf(PRINTF_FORMAT_STRING const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2); char *a_sprintf(const char *fmt, ...) { // idk if you can always just pass NULL to vsnprintf diff --git a/util.h b/util.h index 15181bc..1278e87 100644 --- a/util.h +++ b/util.h @@ -96,6 +96,12 @@ void str_cat(char *dst, size_t dst_sz, const char *src); void strn_cpy(char *dst, size_t dst_sz, const char *src, size_t src_len); /// a safer version of strcpy. `dst_sz` includes a null-terminator. void str_cpy(char *dst, size_t dst_sz, const char *src); +/// trim whitespace from the start of a string +void str_trim_start(char *str); +/// trim whitespace from the end of a string +void str_trim_end(char *str); +/// trim whitespace from both sides of a string +void str_trim(char *str); /// equivalent to GNU function asprintf (like sprintf, but allocates the string with malloc). char *a_sprintf(const char *fmt, ...); /// convert binary number to string. make sure `s` can hold at least 65 bytes!! -- cgit v1.2.3