Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Text engine
- Luidenka
-
Scratcher
18 posts
Text engine
Hi Scratchers,
I'm making a text engine. When I tested the code, some of the charaters jammed together, but some of them are far from each other. May I ask that how can I make the script so that I can kept the width of charaters are the same?
Project:
https://scratch.mit.edu/projects/622320154/
I'm making a text engine. When I tested the code, some of the charaters jammed together, but some of them are far from each other. May I ask that how can I make the script so that I can kept the width of charaters are the same?
Project:
https://scratch.mit.edu/projects/622320154/
- awesome-llama
-
Scratcher
1000+ posts
Text engine
The issue comes from the costumes you are using. The center of each costume is the center of each letter except not every letter has the same width there. Some letters are thinner and some are wider which messes up the spacing you already are using.
The better (but a bit tedious) solution would be to adjust every costume so that each letter is aligned. For example, you could make sure that the left side of every letter is aligned with the costume's center:

A different solution also exists and that is to couteract the costume centering with code. You already know how wide each character is so the distance from center to edge is just half of that. Here's how I would align all the letters with code:

The better (but a bit tedious) solution would be to adjust every costume so that each letter is aligned. For example, you could make sure that the left side of every letter is aligned with the costume's center:

A different solution also exists and that is to couteract the costume centering with code. You already know how wide each character is so the distance from center to edge is just half of that. Here's how I would align all the letters with code:

Last edited by awesome-llama (Dec. 31, 2021 09:19:00)
- Luidenka
-
Scratcher
18 posts
Text engine
I tried the couteract solution and it worked! Thank you so much!
- awesome-llama
-
Scratcher
1000+ posts
Text engine
Another question is how can I make a wraparound?The simplest way would be to just detect if your sprite is over a certain limit, e.g. it has reached x=220, and then just move it back to the start and move it downwards to start the new line.
You most likely want to handle entire words being wrapped rather than doing it letter-by-letter, so for that you would instead have to detect words in some way first, check if the word fits, and if it doesn't, move to the next line before writing that word.
Since words are separated by spaces, you can make your code check for the next word by going through each letter until another space is found. Every letter it goes through can be counted (you'll want to add up the widths of each letter). That total width can be compared with the space available on the current line.
- cIoudyness
-
Scratcher
500+ posts
Text engine
it appears that my assistance is not needed but you can also just start with a monospace font like courier (@qucchia has a project with a lot of extra fonts you can use in the sve without pain)
- Luidenka
-
Scratcher
18 posts
Text engine
Another question is how can I make a wraparound?The simplest way would be to just detect if your sprite is over a certain limit, e.g. it has reached x=220, and then just move it back to the start and move it downwards to start the new line.
You most likely want to handle entire words being wrapped rather than doing it letter-by-letter, so for that you would instead have to detect words in some way first, check if the word fits, and if it doesn't, move to the next line before writing that word.
Since words are separated by spaces, you can make your code check for the next word by going through each letter until another space is found. Every letter it goes through can be counted (you'll want to add up the widths of each letter). That total width can be compared with the space available on the current line.
Can you show an example of this method?
- Luidenka
-
Scratcher
18 posts
Text engine
Another question is how can I make a wraparound?The simplest way would be to just detect if your sprite is over a certain limit, e.g. it has reached x=220, and then just move it back to the start and move it downwards to start the new line.
You most likely want to handle entire words being wrapped rather than doing it letter-by-letter, so for that you would instead have to detect words in some way first, check if the word fits, and if it doesn't, move to the next line before writing that word.
Since words are separated by spaces, you can make your code check for the next word by going through each letter until another space is found. Every letter it goes through can be counted (you'll want to add up the widths of each letter). That total width can be compared with the space available on the current line.
Can you show an example of this method? I've done the part that counting the widths of each word
- awesome-llama-test
-
Scratcher
100+ posts
Text engine
I've remixed your project and made my own version of the text renderer instead. There's too much to comment on if I were to use yours and what you haven't realised is that it can be made a whole lot simpler! There are lots of comments to read:Another question is how can I make a wraparound?The simplest way would be to just detect if your sprite is over a certain limit, e.g. it has reached x=220, and then just move it back to the start and move it downwards to start the new line.
You most likely want to handle entire words being wrapped rather than doing it letter-by-letter, so for that you would instead have to detect words in some way first, check if the word fits, and if it doesn't, move to the next line before writing that word.
Since words are separated by spaces, you can make your code check for the next word by going through each letter until another space is found. Every letter it goes through can be counted (you'll want to add up the widths of each letter). That total width can be compared with the space available on the current line.
Can you show an example of this method? I've done the part that counting the widths of each word
https://scratch.mit.edu/projects/622859633/

- Discussion Forums
- » Help with Scripts
-
» Text engine



