Repository: libgdx/libgdx

Issue #4576: TextField crash when writing brackets if color markup is enabled
Created: 2017-01-28T16:47:04Z
Issue details
Opening and closing brackets [] when color markup is enabled causes an IndexOutOfBoundsException regardless of how many characters are between the brackets
Reproduction steps/code
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.utils.viewport.FitViewport;

/** {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms. */
public class Test extends ApplicationAdapter {
    private Stage stage;
    private Skin skin;

    @Override
    public void create() {
        stage = new Stage(new FitViewport(640, 480));
        skin = new Skin(Gdx.files.internal("skin.json"));

        BitmapFont font = skin.getFont("default-font");

        font.getData().markupEnabled = true;

        stage.addActor(new TextField("", skin));

        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height);
    }

    @Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
    }
}
Version of LibGDX and/or relevant dependencies
Attempted on LibGDX versions 1.9.5 and 1.9.6-SNAPSHOT
Stacktrace
Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: index can't be >= size: 2 >= 1
	at com.badlogic.gdx.utils.FloatArray.get(FloatArray.java:104)
	at com.badlogic.gdx.scenes.scene2d.ui.TextField.drawCursor(TextField.java:373)
	at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:343)
	at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110)
	at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57)
	at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128)
	at test.Test.render(Test.java:82)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Please select the affected platforms

 Android
 iOS (robovm)
 iOS (MOE)
 HTML/GWT
 Windows
 Linux
 MacOS

Duplicate issue #5865: Input error in the TextField or TextArea
Created: 2019-12-20T08:49:07Z
Issue details
while clicking on a text field for input in place without symbols
Reproduction steps/code
public class InputTest extends ApplicationAdapter {

	Stage stage;
	@Override
	public void create() {		
		FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("arialbd.ttf"));
		FreeTypeFontParameter parameter = new FreeTypeFontParameter();
		parameter.size = 16;
		parameter.hinting = Hinting.None;
		parameter.magFilter = TextureFilter.Linear;
		parameter.minFilter = TextureFilter.Linear;
		BitmapFont font = generator.generateFont(parameter);
		font.getData().markupEnabled = true;
		
		TextFieldStyle style = new TextFieldStyle();
		style.font = font;
		style.fontColor = Color.WHITE;
		
		// variant 1:
		//TextArea tf = new TextArea(String.format("[#%s] test [#%s]\n",Color.GOLD.toString(), Color.WHITE.toString()), style);
		// variant 2:
		TextArea tf = new TextArea(String.format("[#%s] test [#%s]",Color.GOLD.toString(), Color.WHITE.toString()), style);
		tf.setSize(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
				
		stage = new Stage();
		stage.addActor(tf);
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		stage.act();
		stage.draw();
	}
}
Version of LibGDX and/or relevant dependencies
1.9.10
Stacktrace
//variant 1:
java.lang.ArrayIndexOutOfBoundsException: 28
	at com.badlogic.gdx.scenes.scene2d.ui.TextField.calculateOffsets(TextField.java:235)
	at com.badlogic.gdx.scenes.scene2d.ui.TextArea.calculateOffsets(TextArea.java:268)
	at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:330)
	at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110)
	at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57)
	at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128)
	at com.libgdxemoji.InputTest.render(InputTest.java:48)
	at com.badlogic.gdx.backends.lwjgl.LwjglCanvas$3.run(LwjglCanvas.java:265)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
//variant 2:
java.lang.RuntimeException: Actor: TextArea
	at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:202)
	at com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:152)
	at com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:281)
	at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:357)
	at com.badlogic.gdx.backends.lwjgl.LwjglCanvas$3.run(LwjglCanvas.java:259)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 16
	at com.badlogic.gdx.scenes.scene2d.ui.TextArea.letterUnderCursor(TextArea.java:90)
	at com.badlogic.gdx.scenes.scene2d.ui.TextField$TextFieldClickListener.setCursorPosition(TextField.java:855)
	at com.badlogic.gdx.scenes.scene2d.ui.TextArea$TextAreaListener.setCursorPosition(TextArea.java:391)
	at com.badlogic.gdx.scenes.scene2d.ui.TextField$TextFieldClickListener.touchDown(TextField.java:835)
	at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
	at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:188)
	... 18 more
Please select the affected platforms

 Android
 iOS (robovm)
 iOS (MOE)
 HTML/GWT
[x ] Windows
 Linux
 MacOS

Closing comment (2020-01-17T18:20:20Z): Duplicate of #4576 : in short, markups are not supported by TextField and TextArea.