aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/helper.c78
-rw-r--r--src/main.c22
2 files changed, 83 insertions, 17 deletions
diff --git a/src/helper.c b/src/helper.c
new file mode 100644
index 0000000..2749daa
--- /dev/null
+++ b/src/helper.c
@@ -0,0 +1,78 @@
+#include "../defs/all.h"
+#include "../defs/widechar.h"
+
+/* this reads one utf81 character from the start of bytes
+ it puts the character's number in *codepoint and returns how many bytes it used (its always 1 or more)
+ invalid input it stores U+FFFD (�) and returns 1, so the caller always moves forward. */
+static int utf8_decode(const unsigned char *bytes, unsigned long *codepoint){
+ unsigned long value = bytes[0];
+ unsigned long minallowed = 0; //rejects overlong encodings
+ int sequence_length;
+ int byte_index;
+
+ if(value < 0x80){
+ *codepoint = value;
+ return 1;
+ } else if(value >= 0xC2 && value <= 0xDF){
+ sequence_length = 2;
+ value &= 0x1F;
+ minallowed = 0x80;
+ } else if(value >= 0xE0 && value <= 0xEF){
+ sequence_length = 3;
+ value &= 0x0F;
+ minallowed = 0x800;
+ } else if(value >= 0xF0 && value <= 0xF4){
+ sequence_length = 4;
+ value &= 0x07;
+ minallowed = 0x10000;
+ } else {
+ *codepoint = 0xFFFD;
+ return 1;
+ }
+
+ for(byte_index = 1; byte_index < sequence_length; byte_index++){
+ if((bytes[byte_index] & 0xC0) != 0x80){
+ *codepoint = 0xFFFD;
+ return 1;
+ }
+ value = (value << 6) | (bytes[byte_index] & 0x3F);
+ }
+
+ if(value < minallowed || value > 0x10FFFF || (value >= 0xD800 && value <= 0xDFFF)){
+ *codepoint = 0xFFFD;
+ return 1;
+ }
+
+ *codepoint = value;
+ return sequence_length;
+}
+
+/* this turns the library's return value into a column count so 0 1 or 2 */
+static int codepoint_width(unsigned long codepoint){
+ int width = widechar_wcwidth(codepoint);
+ if(width > 0)
+ return width;
+
+ switch(width){
+ case widechar_ambiguous:
+ case widechar_private_use:
+ return 1;
+ case widechar_widened_in_9:
+ return 2;
+ default:
+ return 0;
+ }
+}
+
+/* how many terminal columns a string takes up */
+int displaywidth(const char *text){
+ const unsigned char *current_byte = (const unsigned char *)text;
+ int total_width = 0;
+
+ while(*current_byte){
+ unsigned long codepoint;
+ current_byte += utf8_decode(current_byte, &codepoint);
+ total_width += codepoint_width(codepoint);
+ }
+ return total_width;
+}
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 <number> 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)