[SOLVED] BitmapText ColorTags NegativeArraySizeException


        // just for this micro-demo.
        BitmapFont font = new BitmapFont();
        font.setCharSet(new BitmapCharacterSet());
        font.setPages(new Material[0]);
        //  in game it's loaded with AssetManager, of course

        BitmapText text = new BitmapText(font);
        text.setText("\\#FFF#");
    Exception in thread "main" java.lang.NegativeArraySizeException
        at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:68)
        at java.lang.StringBuilder.<init>(StringBuilder.java:101)
        at com.jme3.font.ColorTags.setText(ColorTags.java:80)
        at com.jme3.font.Letters.setText(Letters.java:68)
        at com.jme3.font.BitmapText.setText(BitmapText.java:181)
ColorTags.java sample ![image|700x474](upload://h9Z4sDQCZGAUYXCOnHGdGtMxDnw.png)
// ColorTags.java:80
StringBuilder builder = new StringBuilder(charSeq.length()-7);

why 7? I suspect, it expects color tag in \#RGBA# (7 characters) and doesn’t count that \#RGB# (6 characters) is valid too.

Looks like it’s safe to replace this 7 number with 6. Please confirm and I’ll create PR.

PS: affected jME version is 3.3.0-stable.

1 Like

I think 6 is just as wrong as 7, isn’t it? It won’t break but also why bother?

Wouldn’t it depend on the pattern that was triggered?

And really, to be honest I’m not sure why it’s bothering to send a size to StringBuilder at all. It’s not really saving anything important.

Probably just replace it was new StringBuilder() and try not to micro-optimize. (BitmapText and related classes are a case study in wrong-headed micro-optimization.)

1 Like

You are right, it depends on pattern, but minimal pattern length is 6.

Anyway,

+100500

2 Likes