Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpriteFont is not aligned #2152

Closed
lampewebdev opened this issue Dec 8, 2021 · 1 comment · Fixed by #2153
Closed

SpriteFont is not aligned #2152

lampewebdev opened this issue Dec 8, 2021 · 1 comment · Fixed by #2153
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior

Comments

@lampewebdev
Copy link

Steps to Reproduce

    const textA = new Text({
      font: spriteFont,
      text: "1",
      origin: Vector.Zero,
    });
    let textAA = new Actor({
      x: 100,
      y: 10,
      z: 1,
    });
    textAA.graphics.add(textA);
    engine.add(textAA);
    const textB = new Text({
      font: spriteFont,
      text: "22",
      origin: Vector.Zero,
    });
    let textBA = new Actor({
      x: 100,
      y: 30,
      z: 2,
    });
    textBA.graphics.add(textB);
    engine.add(textBA);

It works when both text has the same length but breaks when they do not have the same length

Expected Result

Font should be aligned on the X axis

Actual Result

Fonts are not aligned

Screenshot 2021-12-08 at 09 07 56

Environment

  • browsers and versions: Firefox (96.0.0)
  • operating system: MacOS
  • Excalibur versions: 0.25.1

Current Workaround

None

@eonarheim eonarheim added the bug This issue describes undesirable, incorrect, or unexpected behavior label Dec 8, 2021
@eonarheim
Copy link
Member

@lampewebdev Thanks for the excellent bug report!

Definitely not working as expected.

It appears that there is some internal state being shared across in the ex.SpriteFont that causes the placement to be buggy when re-used in other ex.Text objects. As a workaround while we work the bug, cloning the spriteFont seems to work for now (although not ideal).

image

var textA = new ex.Text({
  font: spriteFont.clone(),
  text: "1"
});
var textAA = new ex.Actor({
  anchor: ex.Vector.Zero,
  x: 100,
  y: 10,
  z: 1,
});
textAA.graphics.add(textA);
game.add(textAA);

var textB = new ex.Text({
  font: spriteFont.clone(),
  text: "22"
});
var textBA = new ex.Actor({
  anchor: ex.Vector.Zero,
  x: 100,
  y: 30,
  z: 2,
});
textBA.graphics.add(textB);
game.add(textBA);

eonarheim added a commit that referenced this issue Dec 18, 2021
Closes #2152 

This PR fixes SpriteFont and Font alignment issues by making both of them mostly stateless.

This did happen to uncover some additional bugs in SpriteFont that are fixed in this PR
1. SpriteFont rotation behaved differently than normal Fonts
2. SpriteFont's did not support multiple lines like normal Fonts

<img width="455" alt="image" src="https://user-images.githubusercontent.com/612071/146626331-87610af9-8787-4890-8c4b-af1218487729.png">


## Changes:

- Added a method `measureText` on both SpriteFont and Font to return the bounds of the desired text
- Small tweak to debug system to allow independent position label and position point (made debugging easier)

## TODO Before merge

* [x] Tests
* [x] Some code cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants