aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Barnes <craigbarnes@protonmail.com>2024-01-23 20:33:46 +0000
committerCraig Barnes <craigbarnes@protonmail.com>2024-01-23 22:04:41 +0000
commit6ed1c28d2c5209c0149ff222ec2f96369bab7308 (patch)
tree0f129c41106e4603ccfbc0a717b62cbc192fbd28
parentslave: fix typo (diff)
terminal: simplify some string-related code in reload_fonts()
-rw-r--r--terminal.c36
-rw-r--r--xmalloc.c2
-rw-r--r--xmalloc.h12
3 files changed, 20 insertions, 30 deletions
diff --git a/terminal.c b/terminal.c
index 5332ada0..5d0dc01d 100644
--- a/terminal.c
+++ b/terminal.c
@@ -939,11 +939,7 @@ reload_fonts(struct terminal *term, bool resize_grid)
snprintf(size, sizeof(size), ":size=%.2f",
term->font_sizes[i][j].pt_size * scale);
- size_t len = strlen(font->pattern) + strlen(size) + 1;
- names[i][j] = xmalloc(len);
-
- strcpy(names[i][j], font->pattern);
- strcat(names[i][j], size);
+ names[i][j] = xstrjoin(font->pattern, size);
}
}
@@ -966,30 +962,14 @@ reload_fonts(struct terminal *term, bool resize_grid)
const char **names_bold_italic = (const char **)(custom_bold_italic ? names[3] : names[0]);
const bool use_dpi = term->font_is_sized_by_dpi;
+ char *dpi = xasprintf("dpi=%.2f", use_dpi ? term->font_dpi : 96.);
- char *attrs[4] = {NULL};
- int attr_len[4] = {-1, -1, -1, -1}; /* -1, so that +1 (below) results in 0 */
-
- for (size_t i = 0; i < 2; i++) {
- attr_len[0] = snprintf(
- attrs[0], attr_len[0] + 1, "dpi=%.2f",
- use_dpi ? term->font_dpi : 96);
- attr_len[1] = snprintf(
- attrs[1], attr_len[1] + 1, "dpi=%.2f:%s",
- use_dpi ? term->font_dpi : 96, !custom_bold ? "weight=bold" : "");
- attr_len[2] = snprintf(
- attrs[2], attr_len[2] + 1, "dpi=%.2f:%s",
- use_dpi ? term->font_dpi : 96, !custom_italic ? "slant=italic" : "");
- attr_len[3] = snprintf(
- attrs[3], attr_len[3] + 1, "dpi=%.2f:%s",
- use_dpi ? term->font_dpi : 96, !custom_bold_italic ? "weight=bold:slant=italic" : "");
-
- if (i > 0)
- continue;
-
- for (size_t i = 0; i < 4; i++)
- attrs[i] = xmalloc(attr_len[i] + 1);
- }
+ char *attrs[4] = {
+ [0] = dpi, /* Takes ownership */
+ [1] = xstrjoin(dpi, !custom_bold ? ":weight=bold" : ""),
+ [2] = xstrjoin(dpi, !custom_italic ? ":slant=italic" : ""),
+ [3] = xstrjoin(dpi, !custom_bold_italic ? ":weight=bold:slant=italic" : ""),
+ };
struct fcft_font *fonts[4];
struct font_load_data data[4] = {
diff --git a/xmalloc.c b/xmalloc.c
index 5d1fc997..ded7f4e3 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -1,8 +1,6 @@
#include <errno.h>
-#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
#include "xmalloc.h"
#include "debug.h"
diff --git a/xmalloc.h b/xmalloc.h
index 8a3098b8..74282f8f 100644
--- a/xmalloc.h
+++ b/xmalloc.h
@@ -2,6 +2,7 @@
#include <stdarg.h>
#include <stddef.h>
+#include <string.h>
#include <wchar.h>
#include <uchar.h>
@@ -16,3 +17,14 @@ char *xstrndup(const char *str, size_t n) XSTRDUP;
char *xasprintf(const char *format, ...) PRINTF(1) XMALLOC;
char *xvasprintf(const char *format, va_list va) VPRINTF(1) XMALLOC;
char32_t *xc32dup(const char32_t *str) XSTRDUP;
+
+static inline char *
+xstrjoin(const char *s1, const char *s2)
+{
+ size_t n1 = strlen(s1);
+ size_t n2 = strlen(s2);
+ char *joined = xmalloc(n1 + n2 + 1);
+ memcpy(joined, s1, n1);
+ memcpy(joined + n1, s2, n2 + 1);
+ return joined;
+}