DrawText with whitespaces issue :(

Started by
22 comments, last by streamer 15 years, 11 months ago
Quote:Original post by Evil Steve
Quote:Original post by TheMightyDude
Well it must be just me then :(
glyphMetrics.gmCellIncX = -13108 thats not right :(

Also GetGlyphOutline() is returning -1 which is GDI_ERROR isn't it?
So it's failing for some reason :(
Typical, that's the bit I cut out [smile]
It returns GDI_ERROR if the font isn't a truetype font. Here's the unmodified code:
*** Source Snippet Removed ***See if that's any more use...


Well i haven't tried that yet, but I know the following seems to work fine.
I wasn't too happy with the for loop but since this is only called when adding or removing text it shouldn't be too much of an issue.

I will try your code in a while.

Thanks for all your help so far :)
FLOAT CXTextBox::GetStringWidth(std::string String){    RECT String_Info;    ZeroMemory(&String_Info, sizeof(RECT));     int sizeofws = 0;    int found = 0;    if(m_Font)    {        // Get the size of "." <-- closest to a whitespace :)        m_Font->DrawText(NULL, ".", 1, &String_Info, DT_CALCRECT, D3DCOLOR_XRGB(0,0,0));        sizeofws = (int)String_Info.right;        // Now get the size of the string.        m_Font->DrawText(NULL, String.c_str(), String.length(), &String_Info, DT_CALCRECT, D3DCOLOR_XRGB(0,0,0));        // Check for any whitespaces at the end of the string.        if(String[String.length()-1] == 0x20)        {            for(int i=String.length()-1; i>=0; i--)            {                if(String == 0x20)                {                    // Found a whitespace.                    found++;                }                else                {                    // Ok, found a non whitespace at the end just before a whitespace.                    // So I better break.                    break;                }            }        }        if (found>0)        {            // if found >0 calculate the extra width to add.            String_Info.right += (int)(found * sizeofws);        }    }    return (float)String_Info.right;}
Advertisement
Hehe, that code looks vaguely familiar. I did something like it once. The for loop isn't too big of a deal. :)

And don't get down about the multi-line box. :) Wasn't my intention. Just giving you fair heads up. ;-)
Enoch DagorLead DeveloperDark Sky EntertainmentBeyond Protocol
Quote:Original post by EnochDagor
Hehe, that code looks vaguely familiar. I did something like it once. The for loop isn't too big of a deal. :)

Heh, it's very basic and was what I wanted to use a while ago, but wanted to get DrawText() way fixed.
No matter, it works for now with different font sizes :)

Quote:Original post by EnochDagor
And don't get down about the multi-line box. :) Wasn't my intention. Just giving you fair heads up. ;-)


Na, it's ok :P
In most cases I proberbly won't need to use multiline TextBoxes anyway, so theres no problem there :P
Mindyou it would be nice to have that feature though, if I did have it then i could do something like I have now.

atm it uses the following code:
FLOAT CXTextBox::GetStringHeight(std::string String){    RECT String_Info;    ZeroMemory(&String_Info, sizeof(RECT));        if(m_Font)    {       m_Font->DrawText(NULL, String.c_str(), String.length(), &String_Info, DT_CALCRECT, D3DCOLOR_XRGB(0,0,0));      }   return (float)String_Info.bottom;}


The only problem that I know that I will have would be if the second line only had a whitespace in it, but I could use something like what i used in GetStringWidth() so that shouldn't be much of an issue :)
Well one hopes :P
Actually, I had problems with DrawText in a multi-line situation mainly with caret placement. The placement of text is not an issue. But one such problem was the Space at the end of a line causing the calculation of the caret to be weird.

I think I had to do something... figure out the line I was on, measure just that string. Sounds simple enough, eh? But in the end I had a lot of issues with it. It was a long time ago and I am having a hard time remembering it.

Anyway, good luck! :D
Enoch DagorLead DeveloperDark Sky EntertainmentBeyond Protocol
Quote:Original post by EnochDagor
Actually, I had problems with DrawText in a multi-line situation mainly with caret placement. The placement of text is not an issue. But one such problem was the Space at the end of a line causing the calculation of the caret to be weird.

Yeah, I just tried it and couldn't get the cursor on the next line, So I see the issue, most prob play with it later.

Quote:Original post by EnochDagor
I think I had to do something... figure out the line I was on, measure just that string. Sounds simple enough, eh? But in the end I had a lot of issues with it. It was a long time ago and I am having a hard time remembering it.

Yeah, maybe able to work out the height of the strings RECT and divide by the lines and you have the height of the cursor, you then workout the width of each lines and work out when you should be on the next line and place it there, well something like that :P

Anyway, when exactly would you use a multi-line text box in a game?
I can't think of any at the moment.

Quote:Original post by EnochDagor
Anyway, good luck! :D

Thanks.
Quote:Anyway, when exactly would you use a multi-line text box in a game?


You would use one in a description box. Thinking of an example... ever play Everquest or Everquest 2? When you are in your Personal screen... there's a place to edit the details of your character. It is a textbox with multi-line... freeform and all. That is an example of a multi-line textbox in a game. :)

-E
Enoch DagorLead DeveloperDark Sky EntertainmentBeyond Protocol
You can use std::vector of single lines to represent multiline edit box, drawing them one under another,
or you can have some control character/string that will break the lines, for you to know when to write in new line. Something like < br > in HTML.
I'm using this to compute the width of a space :

RECT TempRect = {0,0,0,0};if (0 == pFont->DrawText(NULL, "a", 3, &TempRect, DT_CALCRECT, 0))    ; // <- error handling hereint Awidth = TempRect.right;if (0 == pFont->DrawText(NULL, "a a", 3, &TempRect, DT_CALCRECT, 0))    ; // <- error handling hereint SpaceWidth = TempRect.right - 2 * Awidth;


This works for me but it might not suit your needs
English is not my native language.Sam.
Quote:Original post by rolkA
I'm using this to compute the width of a space :

*** Source Snippet Removed ***

This works for me but it might not suit your needs


Unfortunately he needs something different, where the space is on the end of string: "a "

You can normally check the space you need to draw the string, then check is there is a whitespace on the beginning and on the end. You need two variables for beginning and for the end. Then multiply variables with number of spaces.
beginning_space *= 10;
end_space *= 10;

Start of draw is x + beginning_space , end x + beginning_space + width + end_space.


Quote:Original post by rolkA
I'm using this to compute the width of a space :

*** Source Snippet Removed ***

This works for me but it might not suit your needs


Yeah I tried an 'a' and '|' etc, but found '.' seemed the closest to the width of a space, which was why I used that. :)

This topic is closed to new replies.

Advertisement