Re: [Tuxpaint-devel] Using libtextwrap in Tux Paint Config.
An award-winning drawing program for children of all ages
Brought to you by:
wkendrick
|
From: Bill K. <nb...@so...> - 2021-11-15 10:16:26
|
On Mon, Nov 15, 2021 at 01:40:46AM -0800, Bill Kendrick wrote:
<snip>
> Oh, libunibreak might be useful. As with libtextwrap, I see it's
> available in Ubuntu 20.04. However, before I make any more tectonic
> shifts in the Tux Paint Config. codebase, I'd better make sure it's
> available everywhere. :)
>
> I'm encouraged to see the code is still maintained (last commit was
> one week ago), and I'll play with it a little and see whether it
> seems to do what we need.
So I see that this library simply provides a 'map' of where
breaks would be possible.
I put together a little sample program that takes an input
string and constructs one that includes the "~" hints that
the current code is using for word-wrapping:
$ gcc linebreak-test.c -o linebreak-test -l unibreak && ./linebreak-test
タックスペイントを、ウィンドウ内ではなく、画面全体に表示します。ABC 123
3313313313313313313313313323313313313313313313313313313313323313313313313313313313313313313323312221220
111111112111111111121111111111212221220
タ~ッ~ク~ス~ペ~イ~ン~ト~を、~ウ~ィ~ン~ド~ウ~内~で~は~な~く、~画~面~全~体~に~表~示~し~ま~す。~ABC 123
So the good news is, there'll be no need to manually introduce
them into the translated strings.
Here's the code:
========================================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <linebreak.h>
/* gcc linebreak-test.c -o linebreak-test -l unibreak */
// #define LINEBREAK_MUSTBREAK 0 /**< Break is mandatory */
// #define LINEBREAK_ALLOWBREAK 1 /**< Break is allowed */
// #define LINEBREAK_NOBREAK 2 /**< No break is possible */
// #define LINEBREAK_INSIDEACHAR 3 /**< A UTF-8/16 sequence is unfinished */
int main(int argc, char * argv[]) {
char * str = "タックスペイントを、ウィンドウ内ではなく、画面全体に表示します。ABC 123";
char * brks, * newstr;
size_t i, j;
int possible_split_count;
brks = malloc(strlen(str) + 1);
init_linebreak();
set_linebreaks_utf8(str, strlen(str), "ja_JP.utf-8", brks);
printf("%s\n", str);
for (i = 0; i < strlen(str); i++) {
printf("%d", brks[i]);
}
printf("\n");
possible_split_count = 0;
for (i = 0; i < strlen(str); i++) {
if (brks[i] == LINEBREAK_ALLOWBREAK) {
possible_split_count++;
}
if (brks[i] != LINEBREAK_INSIDEACHAR) {
printf("%d", brks[i]);
}
};
printf("\n");
newstr = (char *) malloc(sizeof(char) * (strlen(str) + possible_split_count + 1));
j = 0;
for (i = 0; i < strlen(str); i++) {
newstr[j++] = str[i];
if (brks[i] == LINEBREAK_ALLOWBREAK && str[i] != ' ') {
newstr[j++] = '~';
}
}
printf("%s\n", newstr);
return (0);
}
========================================================================
--
-bill!
Sent from my computer
|