.NET Graphics.DrawString with Unicode characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    .NET Graphics.DrawString with Unicode characters

    I am having a problem with basic drawing of unicode characters in Windows 2000 and XP.

    I have written a simplest possible C# WinForms program to test it (just create a new Windows Forms C# application and add a Paint event handler):

    Code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Paint += new PaintEventHandler(Form1_Paint);
        }
    
        void Form1_Paint(object sender, PaintEventArgs e)
        {
            Font f = new Font("Tahoma", 12); // this is used just to get a larger font
    
            // try drawing with DrawString
            e.Graphics.DrawString("Test \u2153", f, SystemBrushes.ControlText, new PointF(10, 50));
    
            // try drawing with DrawText
            TextRenderer.DrawText(e.Graphics, "Test \u2153", f, new Rectangle(10, 10, 50, 50), SystemColors.ControlText);
        }
    }
    In Windows Vista, the unicode character (U2153, representing "1/3" in a single character) is drawn twice, but in Windows XP or 2000, it is not drawn in any case.

    So, the question is: how to draw a unicode string in XP?

    [EDIT:] I just found out something, but decided to add this post anyway: the main problem is - Windows XP Tahoma font simply does not have these characters! When you try to insert this unicode symbol in MS Word using Tahoma font, it is omitted from the list (http://zero2180.net/2008/01/tahoma/).

    I usually use SystemFonts.Mes sageBoxFont to get the default system font (returns "Tahoma" for XP and 2000, "Segoe" for Vista).

    So what font should I then use for unicode?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Find one that supports the characters you want and use that font?

    Comment

    • vekipeki
      Recognized Expert New Member
      • Nov 2007
      • 229

      #3
      Well actually I haven't succeeded writing a superscript "+" sign using any of the common Windows fonts in w2k. For example, Arial Unicode MS font (which is said to be one of Microsoft's most-complete fonts in sense of unicode support), is not included by default in my win2k sp4 installation (does it come with Office?).

      I don't like the idea of an application installing its fonts to the system, and I don't like the idea of using the Arial font instead of Tahoma just to get a couple of glyphs - I try to follow the Windows GUI design guidelines and they suggest avoiding non-system-default fonts in controls. I usually use SystemFonts.Mes sageBoxFont, and I presume (I hope correctly) that it will return a font which supports locale characters on a given machine - we might decide to localize our application to Chinese one day!

      So at the end, what I wanted to avoid was using DrawString to manually create subscript and superscript signs because I thought the win2k and xp "unicode" fonts supported them.


      I have two clean Virtual PC installations of win2k and xp, and have also modified my first program to try some other fonts:

      Code:
      using System.Windows.Forms;
      using System.Drawing;
      
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              this.Paint += new PaintEventHandler(Form1_Paint);
          }
      
          void Form1_Paint(object sender, PaintEventArgs e)
          {
              string[] fontNames = new string[]
                   {  "Arial",
                      "Tahoma",
                      "Microsoft Sans Serif",
                      "Arial Unicode MS",
                      "Lucida Sans Unicode",
                      "MS Sans Serif", 
                      SystemFonts.DefaultFont.FontFamily.Name,
                      SystemFonts.MessageBoxFont.FontFamily.Name };
      
              int yOffset = 10;
      
              foreach (string f in fontNames)
                  using (Font fnt = new Font(f, 12))
                  {
                      e.Graphics.DrawString(fnt.FontFamily.ToString() + " \u2153", fnt, SystemBrushes.ControlText, new PointF(10, yOffset)); yOffset += 20;
                      TextRenderer.DrawText(e.Graphics, fnt.FontFamily.ToString() + " \u2153", fnt, new Point(10, yOffset), SystemColors.ControlText); yOffset += 20;
                  }
          }
      }
      It has a list of constant font family names, and two system defined fonts at the end. Windows XP shows the "1/3" glyph only with plain Arial font, but Win 2000 does not show it using any of the fonts.

      Can anyone tell me if there is a font which works for you in w2k, and should be included by default?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Have you checked using charmap?
        I avoid win2k like the plague so I don't have access to it anymore.

        Comment

        • vekipeki
          Recognized Expert New Member
          • Nov 2007
          • 229

          #5
          Thanks for the tip, I haven't used Char. map for a long time. I started it in both windows xp and 2000, and found which fonts support which glyphs.

          They are pretty mixed up, so I will probably try to isolate those special glyphs and handle them separately, leaving MessageBoxFont as the default font for normal text. Should have done it in the first place! :)

          Thanks!

          Comment

          • mdjuit
            New Member
            • Feb 2010
            • 1

            #6
            How to use charmap for special character for writing a string in drawstring

            How to use charmap for special character for writing a string in drawstring?

            I have a long string, trying to print it using drawstring function, but the special character are getting doubled in the output print. Please help me how to use charmap to get rid of this problem. Your suggestion is very important for me

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              ell actually I haven't succeeded writing a superscript "+" sign
              It might just be easiest to check the unicode standard at unicode.org


              You'll find that superscript plus is 207A


              1/3 is 2153

              Comment

              Working...