]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
label-freetype: Handle utf-8 characters better
authorRay Strode <rstrode@redhat.com>
Sat, 2 Dec 2023 18:11:37 +0000 (13:11 -0500)
committerRay Strode <rstrode@redhat.com>
Sat, 2 Dec 2023 23:15:38 +0000 (18:15 -0500)
The plugin currently assumes all characters are 7 byte ascii.

This commit just adds a mbrtowc call around the text to
handle UTF-8 text somewhat better.

src/plugins/controls/label-freetype/plugin.c

index 4981b6ff1c9287e57f4eae4d7ec6aeaeadef7c4e..4744f335c1c039a9b88412c9ecf6c60354b5b27c 100644 (file)
@@ -181,26 +181,44 @@ get_height_of_control (ply_label_plugin_control_t *label)
         return label->area.height;
 }
 
+static bool
+load_character (ply_label_plugin_control_t *label,
+                const char                **text)
+{
+        FT_Error error;
+        size_t character_size;
+        wchar_t character;
+        const char *input_text = *text;
+
+        character_size = mbrtowc (&character, input_text, PLY_UTF8_CHARACTER_SIZE_MAX, NULL);
+
+        if (character_size <= 0) {
+                character = (wchar_t) *input_text;
+                character_size = 1;
+        }
+
+        error = FT_Load_Char (label->face, (FT_ULong) character, FT_LOAD_RENDER | FT_LOAD_TARGET_LIGHT);
+
+        *text = input_text + character_size;
+
+        return !error;
+}
+
 static FT_Int
 width_of_line (ply_label_plugin_control_t *label,
                const char                 *text)
 {
-        FT_Error error;
         FT_Int width = 0;
         FT_Int left_bearing = 0;
 
         while (*text != '\0' && *text != '\n') {
-                error = FT_Load_Char (label->face, *text, FT_LOAD_RENDER | FT_LOAD_TARGET_LIGHT);
-
-                if (!error) {
+                if (load_character (label, &text)) {
                         width += label->face->glyph->advance.x >> 6;
                         left_bearing = label->face->glyph->bitmap_left;
                         /* We don't "go back" when drawing, so when left bearing is
                          * negative (like for 'j'), we simply add to the width. */
                         if (left_bearing < 0)
                                 width += -left_bearing;
-
-                        ++text;
                 }
         }
 
@@ -352,10 +370,8 @@ draw_control (ply_label_plugin_control_t *label,
 
                 while (*cur_c && *cur_c != '\n') {
                         FT_Int extraAdvance = 0, positiveBearingX = 0;
-                        /* TODO: Unicode support. */
-                        error = FT_Load_Char (label->face, *cur_c,
-                                              FT_LOAD_RENDER | FT_LOAD_TARGET_LIGHT);
-                        if (error)
+
+                        if (!load_character (label, &cur_c))
                                 continue;
 
                         /* We consider negative left bearing an increment in size,
@@ -375,8 +391,6 @@ draw_control (ply_label_plugin_control_t *label,
 
                         pen.x += slot->advance.x + extraAdvance;
                         pen.y += slot->advance.y;
-
-                        ++cur_c;
                 }
                 /* skip newline character */
                 if (*cur_c)