fixed utf-8 characters returning the wrong width with strlen. added a bunch of stuff

This commit is contained in:
david committed 2026-10-06 17:50:28 -03:00
1 parent 9a26c0c173
commit ee844db845
8 files changed
+1680 -22

No files matched your search

+3 -1
View File
@@ -3,8 +3,10 @@ bin_PROGRAMS = mikusays
mikusays_SOURCES = \
src/main.c \
src/colors.c \
src/helper.c \
defs/all.h \
defs/mikuart.h
defs/mikuart.h \
defs/helper.h
AM_CFLAGS = -Wall -Wextra
Generated
+12 -3
View File
@@ -104,7 +104,8 @@ CONFIG_CLEAN_VPATH_FILES =
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am__dirstamp = $(am__leading_dot)dirstamp
am_mikusays_OBJECTS = src/main.$(OBJEXT) src/colors.$(OBJEXT)
am_mikusays_OBJECTS = src/main.$(OBJEXT) src/colors.$(OBJEXT) \
src/helper.$(OBJEXT)
mikusays_OBJECTS = $(am_mikusays_OBJECTS)
mikusays_LDADD = $(LDADD)
AM_V_P = $(am__v_P_@AM_V@)
@@ -122,7 +123,8 @@ am__v_at_1 =
DEFAULT_INCLUDES = -I.@am__isrc@
depcomp = $(SHELL) $(top_srcdir)/build-aux/depcomp
am__maybe_remake_depfiles = depfiles
am__depfiles_remade = src/$(DEPDIR)/colors.Po src/$(DEPDIR)/main.Po
am__depfiles_remade = src/$(DEPDIR)/colors.Po src/$(DEPDIR)/helper.Po \
src/$(DEPDIR)/main.Po
am__mv = mv -f
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
@@ -291,8 +293,10 @@ top_srcdir = @top_srcdir@
mikusays_SOURCES = \
src/main.c \
src/colors.c \
src/helper.c \
defs/all.h \
defs/mikuart.h
defs/mikuart.h \
defs/helper.h
AM_CFLAGS = -Wall -Wextra
EXTRA_DIST = LICENSE README.md
@@ -384,6 +388,8 @@ src/$(DEPDIR)/$(am__dirstamp):
src/main.$(OBJEXT): src/$(am__dirstamp) src/$(DEPDIR)/$(am__dirstamp)
src/colors.$(OBJEXT): src/$(am__dirstamp) \
src/$(DEPDIR)/$(am__dirstamp)
src/helper.$(OBJEXT): src/$(am__dirstamp) \
src/$(DEPDIR)/$(am__dirstamp)
mikusays$(EXEEXT): $(mikusays_OBJECTS) $(mikusays_DEPENDENCIES) $(EXTRA_mikusays_DEPENDENCIES)
@rm -f mikusays$(EXEEXT)
@@ -397,6 +403,7 @@ distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/colors.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/helper.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/main.Po@am__quote@ # am--include-marker
$(am__depfiles_remade):
@@ -749,6 +756,7 @@ clean-am: clean-binPROGRAMS clean-generic mostlyclean-am
distclean: distclean-am
-rm -f $(am__CONFIG_DISTCLEAN_FILES)
-rm -f src/$(DEPDIR)/colors.Po
-rm -f src/$(DEPDIR)/helper.Po
-rm -f src/$(DEPDIR)/main.Po
-rm -f Makefile
distclean-am: clean-am distclean-compile distclean-generic \
@@ -798,6 +806,7 @@ maintainer-clean: maintainer-clean-am
-rm -f $(am__CONFIG_DISTCLEAN_FILES)
-rm -rf $(top_srcdir)/autom4te.cache
-rm -f src/$(DEPDIR)/colors.Po
-rm -f src/$(DEPDIR)/helper.Po
-rm -f src/$(DEPDIR)/main.Po
-rm -f Makefile
maintainer-clean-am: distclean-am maintainer-clean-generic
+1 -1
View File
@@ -1,5 +1,5 @@
AC_PREREQ([2.69])
AC_INIT([mikusays], [0.1.1], [], [mikusays], [https://github.com/davideldios7/mikusays])
AC_INIT([mikusays], [0.2.0], [], [mikusays], [https://github.com/davideldios7/mikusays])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([foreign subdir-objects -Wall])
+1
View File
@@ -7,6 +7,7 @@
#include <string.h>
#include <locale.h>
#include "helper.h"
//COLORS.C
void Void(void); //function that does FUCKING NOTHING BECAUSE MY STUPID LSP THINK I'M NOT USING THIS FUCKING FILE AND GIVES ME A WARNINGGENROENIGGHUIOAGHUIHUIAUHIDFG
+6
View File
@@ -0,0 +1,6 @@
#ifndef helper_h
#define helper_h
int displaywidth(const char *text);
#endif
+1574
View File
File diff suppressed because it is too large. Load diff
+78
View File
@@ -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;
}
+5 -17
View File
@@ -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)