terminal: simplify some string-related code in reload_fonts()

This commit is contained in:
Craig Barnes 2024-01-23 20:33:46 +00:00
parent 8073ad352b
commit 6ed1c28d2c
3 changed files with 20 additions and 30 deletions

View file

@ -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] = {

View file

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

View file

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