aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authordavid <[email protected]>2026-10-13 23:04:06 -0300
committerdavid <[email protected]>2026-10-13 23:04:06 -0300
commit7aff48a428f5c9c765f71d364594a9f273d04e8a (patch)
treeca3ea9eb7d079766fb5096794550cfcddbe2300a /src/main.c
parentf0f954d463f7ab0e0ce792527e6567611ed67bf9 (diff)
downloadmikusays-c-7aff48a428f5c9c765f71d364594a9f273d04e8a.tar.gz
mikusays-c-7aff48a428f5c9c765f71d364594a9f273d04e8a.zip
split text
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c78
1 files changed, 68 insertions, 10 deletions
diff --git a/src/main.c b/src/main.c
index c6879b6..350832e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,7 +1,5 @@
#include "../defs/all.h"
#include "../defs/mikuart.h"
-#include <stdlib.h>
-#include <string.h>
char **getmikuart(int style){
if(style < 0)
@@ -55,6 +53,43 @@ static void freelines(char **lines){
free(lines);
}
+static char **push_chunk(char **chunks, int *count, const char *start, size_t len){
+ chunks = realloc(chunks, (*count + 2) * sizeof(char *));
+ chunks[*count] = malloc(len + 1);
+ memcpy(chunks[*count], start, len);
+ chunks[*count][len] = '\0';
+ (*count)++;
+ chunks[*count] = NULL;
+ return chunks;
+}
+
+char **split_word_by_width(const char *word, int max_width){
+ char **chunks = NULL;
+ int count = 0;
+ size_t length = strlen(word);
+ size_t start = 0, i = 0;
+ int chunk_width = 0;
+
+ while(i < length){
+ unsigned long codepoint;
+ int bytes = utf8_decode((const unsigned char *)word + i, &codepoint);
+ int width = codepoint_width(codepoint);
+
+ if(i > start && chunk_width + width > max_width){
+ chunks = push_chunk(chunks, &count, word + start, i - start);
+ start = i;
+ chunk_width = 0;
+ }
+ chunk_width += width;
+ i += bytes;
+ }
+
+ if(i > start)
+ chunks = push_chunk(chunks, &count, word + start, i - start);
+
+ return chunks;
+}
+
char **wraptext(const char *text, int max, int *count){
char *copy = strdup(text);
char *current_line = calloc(strlen(text)+1, sizeof(char));
@@ -69,17 +104,40 @@ char **wraptext(const char *text, int max, int *count){
}
strcat(current_line, tok);
} else{
- if(current_line[0] != '\0'){
- lines = realloc(lines, (*count + 2) * sizeof(char *));
- lines[*count] = strdup(current_line);
- (*count)++;
- lines[*count] = NULL;
- }
- strcpy(current_line, tok);
+ if(displaywidth(tok) > max){
+ if(current_line[0] != '\0'){
+ lines = realloc(lines, (*count + 2) * sizeof(char *));
+ lines[*count] = strdup(current_line);
+ (*count)++;
+ lines[*count] = NULL;
+ }
+
+ char **chunks = split_word_by_width(tok, max);
+ int n = 0;
+ while(chunks[n] != NULL) n++;
+
+ for(int i = 0; i < n - 1; i++){
+ lines = realloc(lines, (*count + 2) * sizeof(char *));
+ lines[*count] = chunks[i];
+ (*count)++;
+ lines[*count] = NULL;
+ }
+
+ strcpy(current_line, chunks[n - 1]);
+ free(chunks[n - 1]);
+ free(chunks);
+ } else{
+ if(current_line[0] != '\0'){
+ lines = realloc(lines, (*count + 2) * sizeof(char *));
+ lines[*count] = strdup(current_line);
+ (*count)++;
+ lines[*count] = NULL;
+ }
+ strcpy(current_line, tok);
+ }
}
tok = strtok(NULL, " \t\n");
}
-
if(current_line[0] != '\0'){
lines = realloc(lines, (*count + 2) * sizeof(char *));
lines[*count] = strdup(current_line);