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

[Question] How to get the height of wrapped text? #138

Open
Bozar opened this issue Aug 16, 2018 · 3 comments
Open

[Question] How to get the height of wrapped text? #138

Bozar opened this issue Aug 16, 2018 · 3 comments

Comments

@Bozar
Copy link

Bozar commented Aug 16, 2018

Hi Ondras, I have made a game with rot.js, the libray is very handy! <3

But there is one feature I hope rot.js could provide natively. When I want to print a long text in the message board, the width and height of which is fixed, I need to know how 'tall' the wrapped text is. For example, the height of the following text is 3:

This is a
very long and
warpped text.

I have tried myself following these steps:

  • Split the original string by spaces.
  • Use a loop to join the splitted strings when its length is less than messageBoard.width.
  • When the loop ends, push the joined string into an array (wrappedText).
  • Go to Step 2.

The length of the array (wrappedText) is the height of the wrapped text.

I don't know whether there are some better solutions. Could you please shed some light on this? Thank you. :)

@blinkdog
Copy link
Contributor

I think you're looking for ROT.Text.measure:

measure: function(str, maxWidth)

This returns an object with fields width and height.

> ROT.Text.measure("This is a very long and wrapped text.", 13)
{ width: 13, height: 3 }

Maybe @ondras can add an entry to the interactive manual?

@atiaxi
Copy link

atiaxi commented Aug 16, 2018 via email

@Bozar
Copy link
Author

Bozar commented Aug 17, 2018

Wow, this is amazing. Thank you all!

That said, that's only useful if you don't need the height before you do the drawing :)

I have figured out a way to work around this.

let width = display.drawText(999, 999, "This is a very long and wrapped text.", 13);
display.clear();
display.drawText(1, 1, "This is a very long and wrapped text.", 13);

console.log("The width of the wrapped text is: " + width);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants