Game Development Community

Supporting Degrees Symbol in text fields

by Nathan Bowhay - ESAL · in Torque 3D Professional · 03/31/2011 (2:37 pm) · 13 replies

I noticed the degrees symbol (°) or unicode U+00B0 isn't supported in text fields. By text fields I mean guiTextEditCtrl, but I am guessing it isn't supported in other ctrls as well.

How would I go about supporting this? Is this an issue with the gfont system?

Any help would be awesome!

Note: it keeps adding an A Symbol, look to the right of it.

#1
04/13/2011 (1:14 pm)
It's supported if you've create guiTextEditCtrl like this:
new GuiTextEditCtrl(){
   text = "°";
   position = "0 0";
   extent = "64 18";
};
But I can't input this symbol from keyboard. =(

Also, gFont calc incorrect string length when we use 2byte UTF-8 symbols and if we use ° for example in multiline tooltip like:
%ctrl.tooltip = "Lon = 10.0°nLat = 20.0°S";
we will see garbage at the end of first line.

To fix it in file "gfx/gFont.cpp" in function GFont::wrapString(...)
when we determine string's length we should count 2byte symbols and subtract it from result length:

after
U32 startLine; 

   for (U32 i = 0; i < len;)
   {
add
U32 wide = 0;

after
else if(isValidChar(txt[i]))
         {
            lineStrWidth += getCharInfo(txt[i]).xIncrement;
add
if(txt[i] < 0) // symbols which code > 127
            {
               wide++; i++;
            }
next, in code
if (!needsNewLine)
      {
         // we are done!
         lineLen.push_back(i - startLine);
         return;
      }
change line
lineLen.push_back(i - startLine);
to
lineLen.push_back(i - startLine - wide);
and next, in code
lineLen.push_back(j - startLine);
      i = j;
change
lineLen.push_back(j - startLine);
to
lineLen.push_back(j - startLine - wide);

P.S. Sorry, i can't put degree symbol here, how you've done it?
#2
04/13/2011 (1:47 pm)
Well and I am actually using it in engine code. I have a control that inherits from GuiTextEditCtrl and you set a unit to use for it. When you set the value it appends the unit to it and set the text:
if(mDisplayUnits != UnitTypeNone)
{
    dStrcat(buf, " ");
    dStrcat(buf, getUnitAbbrivaiation(mDisplayUnits));
}

Parent::setText(buf);

and then that just returns "°" when mDisplayUnits is UnitTypeDegrees.

I tried changing that to "&#176;" and it didn't work. So the text in the text control will be something like "10 °".

Hopefully the text in my quotes doesn't get botched on the forum :)

So does that just fix using "&#176;" with newline "n"?
#3
04/13/2011 (1:49 pm)
Ok so it did get botch ignore the fancy A.
#4
04/13/2011 (2:36 pm)
try

dStrcat(buf, 0xC2);
dStrcat(buf, 0xB0);
#5
04/15/2011 (11:33 am)
well that would give you an error, just because those aren't const char*, but I think I get the idea. Will try some stuff.
#6
04/15/2011 (12:08 pm)
I actually just tried creating:
new GuiTextEditCtrl(){  
       text = "&Acirc;&deg;";  
       position = "0 0";  
       extent = "64 18";  
    };

and it just displayed "&Acirc;&deg;" not a degrees symbol.

I am using T3D 1.1 Beta 3. Does it work for you in that build?
#7
04/15/2011 (12:29 pm)
I see you guys have some problems "displaying" degrees symbol here on forums..
Here is a screenie of Aneroun's code (from .cpp file):

www.dedicatedlogic.net/_files/degrees/cpp_code.png
You can simply copy this symbol from any text editor which supports unicode and paste into GUI as text, so after saving (.gui file) it looks like this on notepad:

www.dedicatedlogic.net/_files/degrees/gui_code.png
You still need to apply Aneroun's changes so the gFont calculates correct width.

This is how the GUI looks in engine:

www.dedicatedlogic.net/_files/degrees/engine.png
Btw, one of the ways to get such symbols into "the game" (including "copyright", etc):
1. Find it on web (for copyright you can look at bottom of any webpage.
2. Select that symbol
3. Copy to clipboard
4. In GUI editor paste that symbol where you need it and save.
Or you can create a test.html with contents:
<html>
&deg; &copy;
</html>
Open in broswer - copy needed symbol - paste in code/gui. All you need to remember is -- save files in unicode, or you will loose such characters.
#8
04/15/2011 (12:54 pm)
That is what I have been doing is just using: en.wikipedia.org/wiki/Degree_symbol and copying it from there.

And I have had issues using it on the forums. If I past it in it adds an A with an accent mark on it. If I go back to edit it it changes into several special A's.

Also I made the GFont changes and still no luck getting it to display in tooltips or text fields. I am just going in the gui editor creating a control like GuiButtonCtrl and setting text & tooltip equal to the degree symbol pasted from wikipedia. Both in the edit field in the inspector, the button, and the tooltip all show a square symbol instead. Is there something I'm missing?

Do I need to delete the cached font files (.utf)?
#9
04/15/2011 (1:51 pm)
Okay, I've just tried to use clipboard and I easily can replicate your issue -- I can't paste those chars.
My steps:
1. Open MainMenuGui.gui in GuiEditor
2. paste the "copyright symbol" after "Play on first button
result: square symbol.
Than:
1. Open MainMenuGui.gui in Notepad
2. Paste the "copyright symbol" after "Play on first button
3. Save file
4. Launch Torque - and the copyright symbol is there and can be now copied into clipboard inside torque and THAT symbol can be pasted to any other gui element..

Looks like something broken with clipboard or en/de-coding from/to unicode (UTF8/16?)..
So, I think we now can fill bug-report about this. I'll to find some time to play around with it, but can't promise anything.
#10
04/15/2011 (2:34 pm)
Hmm I did these exact steps in T3D 1.1 Beta 3 FPS Example. art/gui/mainMenuGui.gui and it still shows up as a square.

Used copy and paste from wikapedia article to file opened in windows notepad, vista 64. Changed: text = "Play"; to text = "Play ©";

Note: sorry forgot to make the change above. That change should really be made in torque. Was running a build I just download from my account (no engine changes).
#11
04/15/2011 (2:37 pm)
stupid A grr :)

Looks like the same issue though happens in my build I edited. I am really unable to get it to show up correctly at all. Looks fine in Torsion or notepad, the the build starts and the gui doesn't look correct. Maybe I botched up the changes above or something, looks like they are all there though?
#12
04/15/2011 (2:58 pm)
Quote:Looks fine in Torsion or notepad
Torsion is not unicode-friendly, so you can't use it.
Notepad: be sure you save file as "UTF-8" (if using Windows 7's notepad).

Take this file and open in Torque -- you will see that it displays correctly, even in stock engine.
You can than select & copy text from "Play" button and paste it into "Join" -- it will still correctly display the text.
But if you try to paste it in notepad or browser, it will show with that "A".
#13
04/18/2011 (11:06 am)
Interesting. That seemed to work:
Open mainMenuGui.gui in notepad
copy and paste degree symbol from wikipedia to play button
save as...
select UTF-8
click save

Now I can copy and paste that symbol in the gui editor into VS2010 and it will show up with the A, but when I compile the gui I made works. When I type 90 deg it changes it to show the degrees symbol cool stuff. Also I can paste it into torsion (will show the A) but the code recognizes it (in a string if).

So what is the difference between the two? When I copy it from torque and when I copy it from wikipedia? Is it encoded using extended Ascii vs UTF-8 or what? When I copy the one from wikimedia into torsion it displays fine, torque just doesn't like it.