Output extended ASCII chars (CP437) on wxDC Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
Death Knight
Experienced Solver
Experienced Solver
Posts: 90
Joined: Mon Feb 28, 2005 10:24 pm
Location: Turkey, Istanbul
Contact:

Output extended ASCII chars (CP437) on wxDC

Post by Death Knight »

I tried many times but stuck with this...
Just want to print Extended ASCII characters ( CP437 ) on wxDC but I couldn't manage how to do it.
I just use wxDC::DrawText for converting string to image on wxDC.
I use "Monospace" font ("DejaVu Sans Mono" "Book" at my system).

I believe this font has required symbols in it since I could see those at my terminal while Monospace font is selected:

Code: Select all

death@triQuad:~>env printf '\u2501'
━death@triQuad:~>env printf '\u2503'
┃death@triQuad:~>env printf '\u2501'
━death@triQuad:~>env printf '\u2511'
┑death@triQuad:~>env printf '\u2519'
┙death@triQuad:~> 
I define font and call DrawText as:

Code: Select all

wxString line;
for( int i=0; i<100; i++)
line +=buffer[i];//binary buffer
stdfont = wxFont(10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, 0, wxT("Monospace"), wxFONTENCODING_CP437);
mydc->SetFont( stdfont );
mydc->DrawText( line, mx, my );
but this doesn't help. Can't write extended ascii chars properly.

Also I see this error message on stdout when I put non printable character into wxDC::DrawText call.

Code: Select all

Pango-WARNING **: Invalid UTF-8 string passed to pango_layout_set_text()
I believe that I need do some conversion but can't figure out how to do it..
I add code like:

Code: Select all

line=wxString(buffer, wxCSConv(wxFONTENCODING_CP437), 100);
But it gives error : Cannot convert from the charset 'Windows/DOS OEM (CP 437)'!
Could you help me on this?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Output extended ASCII chars (CP437) on wxDC

Post by doublemax »

These character are also defined that way in Unicode. Try without the wxFONTENCODING_CP437 stuff.

Which wxWidgets version are you using? And are you using a Unicode build?
Use the source, Luke!
User avatar
Death Knight
Experienced Solver
Experienced Solver
Posts: 90
Joined: Mon Feb 28, 2005 10:24 pm
Location: Turkey, Istanbul
Contact:

Re: Output extended ASCII chars (CP437) on wxDC

Post by Death Knight »

Sorry for lateness.

I am using wxWidgets 2.8.12 , unicode GTK build on linux.

Without wxFONTENCODING_CP437 stuff, I got no error. but this output :
Output of wxDrawText. Character displayed as &quot;001A&quot; instead of EASCII symbols.
Output of wxDrawText. Character displayed as "001A" instead of EASCII symbols.
EASCII.png (4.54 KiB) Viewed 5269 times
I want to show CP437 characters instead of those hex values at box.
Does it because the font that I am using? If it is, how can I choose font with EASCII characters? If it's not, what can I do for show EASCII characters on GTK?
Thanks.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Output extended ASCII chars (CP437) on wxDC

Post by doublemax »

Are you sure that your "buffer" contains the correct character values?

I tested with this code under Linux and it gives the expected output:

Code: Select all

wxPaintDC dc(this);

dc.Clear();
		
dc.SetFont( wxFont(20, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, 0, wxT("Monospace")));

wxString s( wxT("\x2501 \x2503 \x2511 \x2519") );
dc.DrawText( s, 10, 10);
It also works without setting a new font.
Use the source, Luke!
User avatar
Death Knight
Experienced Solver
Experienced Solver
Posts: 90
Joined: Mon Feb 28, 2005 10:24 pm
Location: Turkey, Istanbul
Contact:

Re: Output extended ASCII chars (CP437) on wxDC

Post by Death Knight »

Himm.
My buffer has raw 8bit data, not unicode data as you use...
This makes the issue.

All I want to write same (similar) characters with wxDC.
Here is a sample
Image

So I think I need to do some conversion but don't know how to do it.
Last edited by Death Knight on Fri Mar 08, 2013 2:35 am, edited 2 times in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Output extended ASCII chars (CP437) on wxDC

Post by doublemax »

Now i understand, the unicode chars in your first post lead me in the wrong direction.

Your try with the wxCSConv(wxFONTENCODING_CP437) was correct. I tested this and on my system it works. I don't know why you got that error message.

If you can't find a solution, you could still write a small routine yourself that converts the CP437 to Unicode chars.

Here's the code that i used for testing:

Code: Select all

void OnPaint(wxPaintEvent &event)
{
  wxPaintDC dc(this);

  dc.Clear();
  
  dc.SetFont( wxFont(16, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL) ); 

  int i=32;
  for(int y=2; y<16; y++) {
    char buffer[17];
    int x;
    for(x=0; x<16; x++) {
      buffer[x]=i;
      i++;
    }
    buffer[x]=0;

    wxString s(buffer, wxCSConv(wxFONTENCODING_CP437), 16);
    dc.DrawText( s, 10, y*24+20);
  }
};
Attachments
Screenshot.png
Screenshot.png (53.66 KiB) Viewed 5255 times
Use the source, Luke!
User avatar
Death Knight
Experienced Solver
Experienced Solver
Posts: 90
Joined: Mon Feb 28, 2005 10:24 pm
Location: Turkey, Istanbul
Contact:

Re: Output extended ASCII chars (CP437) on wxDC

Post by Death Knight »

Himm. This might be a bug related with wxGTK.
Also 0x7F interpreted wrong... It needed to mapped to "\x2302"
Don't know if 0x01 to 0x20 put symbols shown at your system correctly since you initialize i from 32 (0x20).

I don't know if this really efficient but works...
The alternative CP437 to Unicode mapping routine:

Code: Select all

wxString CP437T1=wxT(" \x263A\x263B\x2665\x2666\x2663\x2660\x2022\x25D8\x25CB\x25D9\x2642\x2640\x266A\x266B\x263C\x25BA\x25C4\x2195\x203C\x00B6\x00A7\x25AC\x21A8\x2191\x2193\x2192\x2190\x221F\x2194\x25B2\x25BC");
wxString CP437T2=wxT("\x2302\x00C7\x00FC\x00E9\x00E2\x00E4\x00E0\x00E5\x00E7\x00EA\x00EB\x00E8\x00EF\x00EE\x00EC\x00C4\x00C5\x00C9\x00E6\x00C6\x00F4\x00F6\x00F2\x00FB\x00F9\x00FF\x00D6\x00DC\x00A2\x00A3\x00A5\x20A7\x0192\x00E1\x00ED\x00F3\x00FA\x00F1\x00D1\x00AA\x00BA\x00BF\x2310\x00AC\x00BD\x00BC\x00A1\x00AB\x00BB\x2591\x2592\x2593\x2502\x2524\x2561\x2562\x2556\x2555\x2563\x2551\x2557\x255D\x255C\x255B\x2510\x2514\x2534\x252C\x251C\x2500\x253C\x255E\x255F\x255A\x2554\x2569\x2566\x2560\x2550\x256C\x2567\x2568\x2564\x2565\x2559\x2558\x2552\x2553\x256B\x256A\x2518\x250C\x2588\x2584\x258C\x2590\x2580\x03B1\x00DF\x0393\x03C0\x03A3\x03C3\x00B5\x03C4\x03A6\x0398\x03A9\x03B4\x221E\x03C6\x03B5\x2229\x2261\x00B1\x2265\x2264\x2320\x2321\x00F7\x2248\x00B0\x2219\x00B7\x221A\x207F\x00B2\x25A0\x00A0");

inline wxString CP437toUnicode( wxString& line ){
	wxString ret;
	for(unsigned i=0; i < line.Len() ; i++){
		if(line[i]<0x20)
			ret+=CP437T1[line[i]];
		else if(line[i]>0x7E)
			ret+=CP437T2[line[i]-0x7F];
		else
			ret+=line[i];
		}
	return ret;
	}
Thanks for helps.
Erdem
User avatar
Death Knight
Experienced Solver
Experienced Solver
Posts: 90
Joined: Mon Feb 28, 2005 10:24 pm
Location: Turkey, Istanbul
Contact:

Re: Output extended ASCII chars (CP437) on wxDC

Post by Death Knight »

wxCSConv(wxT("CP437")) works!

I found this code at internet, this explains the issue with wxGTK:

Code: Select all

// on linux the wrong string is returned from wxFontMapper::GetEncodingName(wxFONTENCODING_SHIFT_JIS)
// it returns CP-932, in order to use iconv we need to use CP932
m_SJISConv = wxCSConv(wxT("CP932"));
Post Reply