diff options
author | pommicket <pommicket@gmail.com> | 2023-07-18 16:32:29 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-07-19 19:02:27 -0400 |
commit | 8d7f6dea1113f48f812f734b4bba510ef35b3f27 (patch) | |
tree | ca67375c4def9f7780388b58d3a44092ecab20db | |
parent | 94a6d21d4c2fe5fc2806cd28ae36a2b0e1928914 (diff) |
start "multifonts"
-rw-r--r-- | config.c | 2 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | ted.c | 40 | ||||
-rw-r--r-- | ted.cfg | 2 | ||||
-rw-r--r-- | ted.h | 4 | ||||
-rw-r--r-- | util.c | 23 | ||||
-rw-r--r-- | util.h | 6 |
7 files changed, 71 insertions, 7 deletions
@@ -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}, @@ -1,5 +1,6 @@ /* TODO: +- kerning - clean up valgrind_suppresions.txt FUTURE FEATURES: - autodetect indentation (tabs vs spaces) @@ -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) { @@ -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 @@ -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; @@ -12,6 +12,7 @@ #error "Unrecognized operating system." #endif #include <wctype.h> +#include <ctype.h> // 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 @@ -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!! |