From 42a15b157827dbd9c981416c8919ce5f5c907331 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Mon, 2 Sep 2019 15:03:33 +0100 Subject: console: Use icu to provide proper UTF-8 editing --- src/canvas.c | 7 +++- src/canvas.h | 4 +-- src/console.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/console.h | 4 +++ src/imv.c | 18 +++++++++-- 5 files changed, 124 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/canvas.c b/src/canvas.c index 4a00dbc..eaec538 100644 --- a/src/canvas.c +++ b/src/canvas.c @@ -127,7 +127,7 @@ void imv_canvas_font(struct imv_canvas *canvas, const char *name, int size) pango_font_description_set_absolute_size(canvas->font, size * PANGO_SCALE); } -void imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...) +int imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...) { char line[1024]; va_list args; @@ -140,9 +140,14 @@ void imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, cairo_move_to(canvas->cairo, x, y); pango_cairo_show_layout(canvas->cairo, layout); + + PangoRectangle extents; + pango_layout_get_pixel_extents(layout, NULL, &extents); + g_object_unref(layout); va_end(args); + return extents.width; } void imv_canvas_draw(struct imv_canvas *canvas) diff --git a/src/canvas.h b/src/canvas.h index 4f520fe..03425d9 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -37,8 +37,8 @@ void imv_canvas_fill_checkers(struct imv_canvas *canvas, int size); /* Select the font to draw text with */ void imv_canvas_font(struct imv_canvas *canvas, const char *name, int size); -/* Draw some text on the canvas */ -void imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...); +/* Draw some text on the canvas, returns the width used in pixels */ +int imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...); /* Blit the canvas to the current OpenGL framebuffer */ void imv_canvas_draw(struct imv_canvas *canvas); diff --git a/src/console.c b/src/console.c index 92e578d..01b7925 100644 --- a/src/console.c +++ b/src/console.c @@ -1,15 +1,65 @@ #include "console.h" +#include #include #include +#include +#include struct imv_console { char *buffer; size_t buffer_len; + size_t cursor; + imv_console_callback callback; void *callback_data; }; +/* Iterates forwards over characters in a UTF-8 string */ +static size_t next_char(char *buffer, size_t position) +{ + size_t result = position; + UErrorCode status = U_ZERO_ERROR; + UText *ut = utext_openUTF8(NULL, buffer, -1, &status); + + UBreakIterator *it = ubrk_open(UBRK_CHARACTER, NULL, NULL, 0, &status); + ubrk_setUText(it, ut, &status); + + int boundary = ubrk_following(it, (int)position); + if (boundary != UBRK_DONE) { + result = (size_t)boundary; + } + + ubrk_close(it); + + utext_close(ut); + assert(U_SUCCESS(status)); + return result; +} + +/* Iterates backwards over characters in a UTF-8 string */ +static size_t prev_char(char *buffer, size_t position) +{ + size_t result = position; + UErrorCode status = U_ZERO_ERROR; + UText *ut = utext_openUTF8(NULL, buffer, -1, &status); + + UBreakIterator *it = ubrk_open(UBRK_CHARACTER, NULL, NULL, 0, &status); + ubrk_setUText(it, ut, &status); + + int boundary = ubrk_preceding(it, (int)position); + if (boundary != UBRK_DONE) { + result = (size_t)boundary; + } + + ubrk_close(it); + + utext_close(ut); + assert(U_SUCCESS(status)); + return result; +} + + struct imv_console *imv_console_create(void) { struct imv_console *console = calloc(1, sizeof *console); @@ -45,6 +95,7 @@ void imv_console_activate(struct imv_console *console) } console->buffer = calloc(1, console->buffer_len); + console->cursor = 0; } void imv_console_input(struct imv_console *console, const char *text) @@ -53,7 +104,21 @@ void imv_console_input(struct imv_console *console, const char *text) return; } - strncat(console->buffer, text, console->buffer_len - 1); + /* Increase buffer size if needed */ + if (strlen(text) + strlen(console->buffer) + 1 > console->buffer_len) { + console->buffer_len *= 2; + console->buffer = realloc(console->buffer, console->buffer_len); + assert(console->buffer); + } + + /* memmove over everything after the cursor right then copy in the new bytes */ + size_t to_insert = strlen(text); + size_t old_cursor = console->cursor; + size_t new_cursor = console->cursor + to_insert; + size_t to_shift = strlen(console->buffer) - old_cursor; + memmove(&console->buffer[new_cursor], &console->buffer[old_cursor], to_shift); + memcpy(&console->buffer[old_cursor], text, to_insert); + console->cursor = new_cursor; } bool imv_console_key(struct imv_console *console, const char *key) @@ -77,11 +142,33 @@ bool imv_console_key(struct imv_console *console, const char *key) return true; } + if (!strcmp("Left", key) || !strcmp("Ctrl+b", key)) { + console->cursor = prev_char(console->buffer, console->cursor); + return true; + } + + if (!strcmp("Right", key) || !strcmp("Ctrl+f", key)) { + console->cursor = next_char(console->buffer, console->cursor); + return true; + } + + if (!strcmp("Ctrl+a", key)) { + console->cursor = 0; + return true; + } + + if (!strcmp("Ctrl+e", key)) { + console->cursor = strlen(console->buffer); + return true; + } + if (!strcmp("BackSpace", key)) { - const size_t len = strlen(console->buffer); - if (len > 0) { - console->buffer[len - 1] = '\0'; - } + /* memmove everything after the cursor left */ + size_t new_cursor = prev_char(console->buffer, console->cursor); + size_t old_cursor = console->cursor; + size_t to_shift = strlen(console->buffer) - new_cursor; + memmove(&console->buffer[new_cursor], &console->buffer[old_cursor], to_shift); + console->cursor = new_cursor; return true; } @@ -93,6 +180,11 @@ const char *imv_console_prompt(struct imv_console *console) return console->buffer; } +size_t imv_console_prompt_cursor(struct imv_console *console) +{ + return console->cursor; +} + const char *imv_console_backlog(struct imv_console *console) { (void)console; diff --git a/src/console.h b/src/console.h index 22cf9b6..d8d270d 100644 --- a/src/console.h +++ b/src/console.h @@ -2,6 +2,7 @@ #define IMV_CONSOLE #include +#include struct imv_console; @@ -33,6 +34,9 @@ bool imv_console_key(struct imv_console *console, const char *key); /* What is the console prompt's current text? */ const char *imv_console_prompt(struct imv_console *console); +/* What is the console prompt's current cursor position? (bytes into UTF-8 string) */ +size_t imv_console_prompt_cursor(struct imv_console *console); + /* What is the output history of the console? */ const char *imv_console_backlog(struct imv_console *console); diff --git a/src/imv.c b/src/imv.c index a612bb2..4231fd4 100644 --- a/src/imv.c +++ b/src/imv.c @@ -377,7 +377,7 @@ static void key_handler(struct imv *imv, const struct imv_event *event) { if (imv_console_is_active(imv->console)) { - if (imv_console_key(imv->console, event->data.keyboard.keyname)) { + if (imv_console_key(imv->console, event->data.keyboard.description)) { imv->need_redraw = true; return; } @@ -1209,8 +1209,20 @@ static void render_window(struct imv *imv) imv_canvas_fill_rectangle(imv->canvas, 0, wh - height - bottom_offset, ww, height + bottom_offset); imv_canvas_color(imv->canvas, 1, 1, 1, 1); - imv_canvas_printf(imv->canvas, 0, wh - height - bottom_offset, - ":%s\u2588", imv_console_prompt(imv->console)); + + int x = 0; + /* draw pre-cursor text */ + x += imv_canvas_printf(imv->canvas, x, wh - height - bottom_offset, + ":%.*s", + imv_console_prompt_cursor(imv->console), + imv_console_prompt(imv->console)); + /* draw the cursor */ + imv_canvas_color(imv->canvas, 1, 1, 1, 0.5); + imv_canvas_printf(imv->canvas, x, wh - height - bottom_offset, "\u2588"); + /* any any remaining text on top of the cursor */ + imv_canvas_color(imv->canvas, 1, 1, 1, 1); + imv_canvas_printf(imv->canvas, x, wh - height - bottom_offset, "%s", + imv_console_prompt(imv->console) + imv_console_prompt_cursor(imv->console)); } imv_canvas_draw(imv->canvas); -- cgit v1.2.3