Issues with label rendering in Japanese

Hi! I love your framework. However, I found a couple of issues with the label feature when using Japanese text.

1 Setting labelWidth or lineWidth sometimes outputs 'undefined'.

2 Using labelWidth and labelHeight with dynamic font sizes creates weird gaps.

I've attached an index.html file where you can reproduce these issues. It would be great if these could be fixed.
Thanks!

ZIM - Code Creativity
<script type="module">
    import zim from "https://zimjs.org/cdn/018/zim";

    new Frame(FIT, 1024, 768, light, dark, ready);

    function ready() {

        // โœ•
        const text1 = new Label({
            text: "ใƒ†ใ‚ญใ‚นใƒˆใฎใƒใ‚ฐใ‚’็™บ่ฆ‹ใ—ใพใ—ใŸใ€‚ใ‚ˆใ‚ใ—ใใŠ้ก˜ใ„ใ—ใพใ™ใ€‚",
            align: CENTER,
            splitWords: true,
            lineWidth: 300,
        }).loc(200, 200);


        // ใ€‡
        const text2 = new Label({
            text: "ใƒ†ใ‚ญใ‚นใƒˆใฎใƒใ‚ฐใ‚’็™บ่ฆ‹ใ—ใพใ—ใŸใ‚ˆใ‚ใ—ใใŠ้ก˜ใ„ใ—ใพใ™ใ€‚",
            align: CENTER,
            splitWords: true,
            labelWidth: 300,
        }).center();

        // โ–ณ
        const text = new Label({
            text: "ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚ใ‚",
            align: CENTER,
            splitWords: true,
            labelWidth: 300,
            labelHeight: 100,
        }).loc(700, 600);

    }
</script>
<meta name="viewport" content="width=device-width, user-scalable=no" />
1 Like

image

I see the undefined. I also see the gaps using English letters.

But have not been able to get the undefined with English letters.

Will have a look at it when we can - we are just trying to launch ZIM 019 tomorrow and have a big event tonight. But hopefully, we can get a fix in for tomorrow.

1 Like

looking into it... the undefined seems to be coming from createjsText() - so seeing if we can look into it there.

1 Like

image

Not sure why words[j+1] would be null... and why it shows up in Japanese and never has in English so far... but this seems to fix it.

1 Like

Ah... we have an order bug with both splitWords and labelWidth/labelHeight.

The words are being split at the width before trying to fit them into the height. Then it takes those and the new lines are considered spaces as it tries to fit them in the size.

image

Okay... will give it a little thought.

1 Like

That is a tricky one. Could do it pretty easily if we assume or know that it is all letters with no spaces. Then we just fit all letters and don't worry about words. In Japanese, would this happen a lot? We could provide an extra parameter called fitLetters and if that is set to true, we just fit the characters into the space provided.

Would that be a good solution? I am not sure I can do an iterative word based solution. If anyone wants to think about it, they are welcome to check out the Label code.

Another solution would be to put spaces between the characters

image

Let us know what you think.

1 Like

Thank you for your quick support! And congratulations on the new version!

I used to use Phaser, Its wordWrap works correctly with Japanese text.
Since Phaser lacks a built-in labelWidth and labelHeight feature, I had to manually adjust the font size using a while loop.
Phaser's original codes might be a helpful reference for fixing this issue.

It works perfectly in ZIM 019! :heart_eyes:
Thank you very much!
I sent a small gift as thanks.
Sorry the amount is so small.
I'm broke :rofl:

Hi! The "undefined" bug has been fixed completely.
But I found a new bug.
When I use labelWidth, one character disappears.
I'd be happy if you could check it out when you have time.

ZIM - Code Creativity
<!-- zimjs.com - JavaScript Canvas Framework -->
<script type="module">

    import zim from "https://zimjs.org/cdn/019/zim";

    new Frame(FIT, 600, 800, light, dark, ready);
    function ready() {
        //ใ€‡
        const text1 = new Label({
            text: "ใ“ใ‚“ใซใกใฏใ€‚ใŠไธ–่ฉฑใซใชใฃใฆใŠใ‚Šใพใ™ใ€‚",
            size: 20,
            align: CENTER,
        }).loc(300, 100);

        //โœ•
        const text2 = new Label({
            text: "ใ“ใ‚“ใซใกใฏใ€‚ใŠไธ–่ฉฑใซใชใฃใฆใŠใ‚Šใพใ™ใ€‚",
            size: 60,
            align: CENTER,
            splitWords: true,
            labelWidth: 250,
            labelHeight: 100
        }).center();
    } 

</script>
<meta name="viewport" content="width=device-width, user-scalable=no" />
1 Like

image

We removed the height and it still disappears. We changed the width and a different character disappeared as shown in the screen capture.

In English, the characters are all there...

image

Will try and take a look at it after some festivities going on here.

1 Like

The missing characters are in CreateJS again...

We are missing the ไธ– and the ใพ both which come before the full pink line is drawn.

So something in this code in CreateJS:

str = words[0];
w = ctx.measureText(str).width;
zogr(words)
for (var j = 1, jl = words.length; j < jl; j += 2) {
	// Line needs to wrap:
	if (words[j+1]==null) words[j+1] = "";
	zogy(words[j])
	var wordW = ctx.measureText(words[j] + words[j+1]).width;
	if (w + wordW > this.lineWidth) {
		zogp(str)
		if (paint) { this._drawTextLine(ctx, str, count * lineHeight); }
		if (lines) { lines.push(str); }
		if (w > maxW) { maxW = w; }
		str = words[j+1];
		w = ctx.measureText(str).width;
		count++;
	} else {
		str += words[j] + words[j+1];
		w += wordW;
	}
}

and then the end goes on:

if (paint) {
	this._drawTextLine(ctx, str, count * lineHeight);
	zogg(str)
}
1 Like

Oh... I see, there already is wrapping for CJS characters

And it is getting the list of the characters as separate so that is good. The red list is right. It is just dropping a letter each new line.

1 Like

Does not seem to be a problem with English letter words

1 Like

But if I remove the spaces from the word list and keep just the letters as words then it does the same thing with the English letters.

So it is not a CJK character issue really... that's good.

1 Like

Oh!! Okay - the space on the end of the line is being removed but with the CJK there is no space so it is removing the previous character. Maybe that is in the j+=2 - I don't usually work in the +=2 - anyway... will have a nap and get back to it after.

image

1 Like

Thank you so much for your efforts! :pray:
I'm sorry I couldn't be of more help.
No rush at all. Please take your time!

The CreateJS code came from here:

and this code works with as it has j += 1

Ah... I am now thinking that we brought this in to CreateJS - Yes - in ZIM NFT 00 we added it to our CreateJS 1.3.4

Added support for wrapping Chinese characters to CreateJS 1.3.4 - thanks Yanwenge and
solution here: createjs TextField CJK text wrapping support | about my life of multimedia

And so it is our fault. Sigh. lol.

We did not notice the missing characters when adjusting to the j += 2. This also means the undefined was probably us as well.

Okay... seeing if we can figure a solution.

On a good note, it probably means we have a solution to the space in the lines as to fit cjk characters into a box you just wrap on the character and avoid spaces all together. So can work on that too.

1 Like

Okay - the code was using a test that did not detect all the JCK characters.

We rebuilt the loop that the blog was using with a better test and so it handles a mix of the characters as well including spaces. If there are no JCK characters it does the original CreateJS code.

image

Do a hard refresh and let us know how it goes!

1 Like

Awesome! Everything works perfectly now!
I really appreciate your efforts.
Have a nice Christmas! :christmas_tree:

1 Like

You too - thanks for letting us know about the issue!

2 Likes