commit 00e02c5bc470c53169964a21c53112c33fddb52e
parent 09d1d1cec19a7cbc73e534e577c66264c481e12b
Author: nibo <nibo@relim.de>
Date: Mon, 13 Jan 2025 19:29:22 +0100
Fix splitting a multibyte character
If there has to be added space between lyrics' characters
because the annotation/chord above them wouldn't fit
otherwise the splitting the lyrics would separate bytes
that form one character meaning possibly one grapheme
cluster. Well that's an ugly thing right? Well this
commit fixes this. Adding a whole library for fixing
this issue seems a little bloated on the other hand
I'm glad there's libgrapheme available with a license
so I can use it.
Diffstat:
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,7 +4,7 @@ MANPREFIX = ${PREFIX}/share/man
COLOR = 0
VARS = -DVERSION=\"${VERSION}\" -DCOLOR=${COLOR} -DPREFIX=\"${PREFIX}\"
CFLAGS = -pedantic -Wall -Wextra
-LDFLAGS = -lpdfio -ltoml -lfontconfig
+LDFLAGS = -lpdfio -ltoml -lfontconfig -lgrapheme
SRC = util.c config.c chordpro.c chord_diagram.c out_pdf.c lorid.c
compile:
diff --git a/out_pdf.c b/out_pdf.c
@@ -6,6 +6,7 @@
#include <sys/stat.h>
#include <errno.h>
#include <fontconfig/fontconfig.h>
+#include <grapheme.h>
#include "chordpro.h"
#include "config.h"
#include "out_pdf.h"
@@ -1490,7 +1491,13 @@ pdf_texts_add_lyrics(
t++;
for (sp = ctx->spaces; *sp; sp++) {
if ((*sp)->line_item_index == i && (*sp)->text_index == c) {
- // TODO: This code splits multibyte characters which leads to invalid UTF-8
+ size_t len = grapheme_next_character_break_utf8(&item->u.text->text[c], 10);
+ size_t i;
+ for (i = 0; i<len-1; i++, t++) {
+ c++;
+ (*texts)[ctx->text]->text = erealloc((*texts)[ctx->text]->text, (t+1) * sizeof(char));
+ (*texts)[ctx->text]->text[t] = item->u.text->text[c];
+ }
(*texts)[ctx->text]->text = erealloc((*texts)[ctx->text]->text, (t+1) * sizeof(char));
(*texts)[ctx->text]->text[t] = 0;
(*texts)[ctx->text]->style = cho_style_copy(item->u.text->style);