From 3a98c71fa1aad4d71c772d64620f47887827a389 Mon Sep 17 00:00:00 2001 From: QC Date: Mon, 2 Feb 2015 16:44:08 +0100 Subject: [PATCH] Fix: #121 Print utf8 until invalid character. --- source/textbox.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/source/textbox.c b/source/textbox.c index b13cf6d6..49b0eca1 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -156,12 +156,18 @@ void textbox_font ( textbox *tb, TextBoxFontType tbft ) void textbox_text ( textbox *tb, char *text ) { g_free ( tb->text ); - - if ( g_utf8_validate ( text, -1, NULL ) ) { + const gchar *last_pointer = NULL; + if ( g_utf8_validate ( text, -1, &last_pointer ) ) { tb->text = g_strdup ( text ); } else { - tb->text = g_strdup ( "Invalid UTF-8 string." ); + if ( last_pointer != NULL ) { + // Copy string up to invalid character. + tb->text = g_strndup ( text, ( last_pointer - text ) ); + } + else { + tb->text = g_strdup ( "Invalid UTF-8 string." ); + } } pango_layout_set_text ( tb->layout, tb->text, strlen ( tb->text ) );