• Advertisement

• ### Popular Now

• 11
• 9
• 10
• 9
• 10
• Advertisement
• Advertisement
• Advertisement

# Whitespaces in text

This topic is 2685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello all.

Because the DrawText function caused lag even with drawing with a sprite, I switched to drawing with primitives.

Now, when a string contains those symbols (\n \r etc) it will not insert a whiteline (ENTER key) but it will also not display the symbol itself (\n).

What would be the easiest way to let those symbols work?

Greetings Ultimate-Tester

#### Share this post

##### Share on other sites
Advertisement
Quote:
 I switched to drawing with primitives. Now, when a string contains those symbols (\n \r etc) it will not insert a whiteline (ENTER key)

What is "it"? If you're drawing the text yourself (instead of using DrawText), just insert some blank space (for a space) before the next letter.

#### Share this post

##### Share on other sites
That depends on how you draw your text. Usually you parse the string into sprites, and don't generate sprites for whitespace characters.

#### Share this post

##### Share on other sites
I think you are confused with whitespaces and whitelines.
Its like this:

Hello :)
<= this is a whiteline
Hello :)

And as the string it would be like:

"Hello :)\n\n\nHello :)".

#### Share this post

##### Share on other sites
The same principal applies. You need to treat your string as a sequence of sprites and control characters. So "\r" means "Move back to the start of the line", "\n" means "Move down a line", " " means "Move forwards one character", "\t" means "Move forwards 4 spaces" and so on.

#### Share this post

##### Share on other sites
Yes I also know that, but thank you for telling again ;)

The point is that using sprintf or any look-a-like like vsnprintf which I'm using to overload, the \n dissappears (so if I draw hello\nworld, the output is helloworld)

How does the DrawText function handle these escape characters?

#### Share this post

##### Share on other sites
Quote:
 How does the DrawText function handle these escape characters?

You claim to already know this. As Evil Steve said, DrawText (or the underlying layout API) looks for characters that are control characters and adjusts the metrics used for glyph rendering accordingly. Whatever you've replaced DrawText with has to do the same. There are many ways to handle it -- the ideal way for your case will depend on how you draw the text now, specifically. Until you provide more information about that, nobody can provide you more detailed solutions.

#### Share this post

##### Share on other sites
Ok I see your point. I must make myself more clear:

When I use sprintf or any other variation, it will remove this "\n".
How can I now detect if theres a "\n" in the string while it's not there?

I already know that when this "\n" is detected it should move back to the start of the line and move down the height of the text + 2px (for a little spacer) but I just can't detect the "\n" symbol because it has been removed already.

Now my question about DrawText: How can this function handle this "\n" while it has been removed?

EDIT:

I fixed it :) Thank you guys. The problem was I was looking for \\n instead of \n. The \n doesn't get escaped and therefore is invisible, but IS there. The \\n does get escaped and remains visible because it doesn't get processed.

#### Share this post

##### Share on other sites
I have no idea what you are talking about. To my knowledge, sprintf() does not remove newline characters. I suspect you are confused about what it means for a character to be escaped. Can you post a minimal program demonstrating what you think is going on?
#include <cstdio>int main(){    const char *string = "Hello\t,\nWorld!\n\n";    char buffer[1024];    std::sprintf(buffer, "The string is:\n\t\'%s\'\nWasn\'t that fun?\n", string);    std::printf("%s", buffer);}

I don't see how these characters are "removed", from what you have said.

#### Share this post

##### Share on other sites
Yes I think I don't explain correctly what I mean:

When I draw a string that contains an escaped character like "\n", then you will never see the "\n" character because it has been escaped. Therefore, I was confused about how to detect if a string contains an escaped character. I was confused how to find an escaped character because you can't see it visually.

Now I know that the escape sign actually IS there but escaped (invisible) so that means that if you just search a string for "\n", then you will actually get results when a string contains this character. I was searching for "\\n" which means that it wouldn't dissappear because it is an escaped escaped character (lol?)

Anyways, the problem has been solved. I was just being stupid.

#### Share this post

##### Share on other sites

• Advertisement