From ee844db84596e8ba8e0b84f770d66b6e6fabb028 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 6 Oct 2026 17:50:28 -0300 Subject: fixed utf-8 characters returning the wrong width with strlen. added a bunch of stuff --- src/main.c | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 1445292..8c0fda3 100644 --- a/src/main.c +++ b/src/main.c @@ -63,7 +63,7 @@ char **wraptext(const char *text, int max, int *count){ char *tok = strtok(copy, " \t\n"); while(tok != NULL){ - if(strlen(current_line)+strlen(tok) < (size_t)max){ + if((size_t)displaywidth(current_line)+(size_t)displaywidth(tok) < (size_t)max){ if(current_line[0] != '\0'){ strcat(current_line, " "); } @@ -101,7 +101,7 @@ char **getbubblelines(char *text){ size_t content_width = 0; for (int i = 0; templines[i] != NULL; i++) { - size_t current_len = strlen(templines[i]); + size_t current_len = displaywidth(templines[i]); if (current_len > content_width) content_width = current_len; } @@ -128,13 +128,13 @@ char **getbubblelines(char *text){ strcat(bubblelines[0], top_right); for (int i = 0; lines[i] != NULL; i++) { - int line_width = strlen(lines[i]); + int line_width = displaywidth(lines[i]); int padding_needed = (bubble_width - 2) - line_width; if (padding_needed < 0) padding_needed = 0; int left_padding = padding_needed / 2; int right_padding = padding_needed - left_padding; - - bubblelines[i + 1] = malloc(strlen(vertical) * 2 + 2 + line_width + padding_needed + 1); + + bubblelines[i + 1] = malloc(strlen(vertical) * 2 + 2 + strlen(lines[i]) + padding_needed + 1); sprintf(bubblelines[i + 1], "%s %*s%s%*s %s",vertical, left_padding, "", lines[i], right_padding, "", vertical); } @@ -177,18 +177,6 @@ static void list_all_art_styles(void){ printf("\nUse --style to select a specific style, or omit for random selection.\n"); } - -//display width in columns: counts utf 8 codepoints (skips continuation bytes) -//assumes every character is 1 column wide -//emojis and other bullshit won't work bu tthat's okay since who the fuck will try to put '🥺' into this -static int displaywidth(const char *s){ - int w = 0; - for(; *s; s++) - if(((unsigned char)*s & 0xC0) != 0x80) - w++; - return w; -} - void draw_miku_says(char *text, int style){ char **speech_bubble_lines = getbubblelines(text); if(speech_bubble_lines == NULL) -- cgit v1.3-2-g0d8e