diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/linebuffer.c | 27 | ||||
| -rw-r--r-- | src/utf8.c | 74 |
2 files changed, 92 insertions, 9 deletions
diff --git a/src/linebuffer.c b/src/linebuffer.c index 6773ca1..3fa08b0 100644 --- a/src/linebuffer.c +++ b/src/linebuffer.c @@ -1,4 +1,5 @@ #include "linebuffer.h" +#include "utf8.h" #include <ctype.h> #include <stdbool.h> #include <stdio.h> @@ -6,19 +7,23 @@ #include <string.h> #define NULL_TERMINATOR_LEN 1 +#define UTF8_MAX_BYTES_PER_CHAR 4 -LineBuffer *linebuffer_create(size_t buffer_capacity) { +LineBuffer *linebuffer_create(size_t line_width) { LineBuffer *lb = malloc(sizeof(LineBuffer)); if (lb == NULL) { return NULL; } + size_t buffer_capacity = line_width * UTF8_MAX_BYTES_PER_CHAR; lb->_buffer = malloc(buffer_capacity + NULL_TERMINATOR_LEN); if (lb->_buffer == NULL) { free(lb); return NULL; } lb->_num_words = 0; + lb->_line_width = line_width; lb->_buffer_length = 0; + lb->_display_width = 0; lb->_buffer_capacity = buffer_capacity; return lb; } @@ -31,6 +36,7 @@ void linebuffer_destroy(LineBuffer *lb) { void linebuffer_clear(LineBuffer *lb) { lb->_num_words = 0; lb->_buffer_length = 0; + lb->_display_width = 0; } bool linebuffer_append_word(LineBuffer *lb, const char *word) { @@ -40,25 +46,28 @@ bool linebuffer_append_word(LineBuffer *lb, const char *word) { if (lb->_num_words > 0) { lb->_buffer[lb->_buffer_length] = ' '; lb->_buffer_length++; + lb->_display_width++; } - size_t word_length = strlen(word); - memcpy(lb->_buffer + lb->_buffer_length, word, word_length); - lb->_buffer_length += word_length; + size_t word_byte_length = strlen(word); + size_t word_display_width = utf8_display_width(word); + memcpy(lb->_buffer + lb->_buffer_length, word, word_byte_length); + lb->_buffer_length += word_byte_length; + lb->_display_width += word_display_width; lb->_num_words++; return true; } bool linebuffer_can_fit(LineBuffer *lb, const char *word) { - size_t append_len = strlen(word); + size_t word_display_width = utf8_display_width(word); + size_t required_width = word_display_width; if (lb->_num_words > 0) { - // Account for space before the word - append_len++; + required_width++; } - return append_len <= linebuffer_space_remaining(lb); + return required_width <= linebuffer_space_remaining(lb); } size_t linebuffer_space_remaining(LineBuffer *lb) { - return lb->_buffer_capacity - lb->_buffer_length; + return lb->_line_width - lb->_display_width; } int linebuffer_write(LineBuffer *lb, FILE *fp) { diff --git a/src/utf8.c b/src/utf8.c new file mode 100644 index 0000000..07b751e --- /dev/null +++ b/src/utf8.c @@ -0,0 +1,74 @@ +#include "utf8.h" +#include <unicode/ustring.h> +#include <unicode/uchar.h> +#include <unicode/ubrk.h> +#include <string.h> +#include <stdlib.h> + +size_t utf8_display_width(const char *str) { + if (str == NULL) { + return 0; + } + + UErrorCode status = U_ZERO_ERROR; + int32_t utf8_len = (int32_t)strlen(str); + + // Convert UTF-8 to UTF-16 for ICU processing + UChar *utf16 = NULL; + int32_t utf16_len = 0; + + // First call to get required length + u_strFromUTF8(NULL, 0, &utf16_len, str, utf8_len, &status); + if (status != U_BUFFER_OVERFLOW_ERROR && U_FAILURE(status)) { + return 0; + } + + status = U_ZERO_ERROR; + utf16 = (UChar *)malloc((utf16_len + 1) * sizeof(UChar)); + if (utf16 == NULL) { + return 0; + } + + u_strFromUTF8(utf16, utf16_len + 1, NULL, str, utf8_len, &status); + if (U_FAILURE(status)) { + free(utf16); + return 0; + } + + // Use break iterator to count grapheme clusters + UBreakIterator *bi = ubrk_open(UBRK_CHARACTER, NULL, utf16, utf16_len, &status); + if (U_FAILURE(status)) { + free(utf16); + return 0; + } + + size_t width = 0; + int32_t start = ubrk_first(bi); + int32_t end = ubrk_next(bi); + + while (end != UBRK_DONE) { + // Get the first code point of this grapheme cluster + UChar32 ch; + int32_t idx = start; + U16_NEXT(utf16, idx, utf16_len, ch); + + // Determine display width based on East Asian Width property + int32_t ea_width = u_getIntPropertyValue(ch, UCHAR_EAST_ASIAN_WIDTH); + + // Full-width and Wide characters take 2 columns + if (ea_width == U_EA_FULLWIDTH || ea_width == U_EA_WIDE) { + width += 2; + } else { + // All others (Narrow, Halfwidth, Neutral, Ambiguous) take 1 column + width += 1; + } + + start = end; + end = ubrk_next(bi); + } + + ubrk_close(bi); + free(utf16); + + return width; +} |