Click here to Skip to main content
15,886,648 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is a great article about unicode manuplation in csharp.

http://csharphelper.com/blog/2014/09/explore-unicode-characters-c/[^]

Also the above link provides binary executable which you can print available unicode characters in the font "Times New Roman".

However, when I try to draw this unicode characters using DrawString method, many of the unicode characters displayed in the text box are just drawn as hallow sqare (i.e. unsupported character) whereas the same characters are shown correct form in the text box.

I have used the same font "Times New Roman" with DrawString method.

I am just curious why there is some discrepancy between textbox and DrawString method even if I am using the same font.

Ocassioanlly, I can draw some characters using Draw String method but many of them are drawn as blank square.

There seems no consistency between these two methods even using the same font.


There shoud not be any discrepancy because I supply "Times New Roman" font in the DrawString method, isn't it?


Is there any clean solution or work around on this problem ? I really want to see both text box and DrawString method give the same results.


Here is the screenshot of it if you need more information about this problem.

https://www.photobox.co.uk/my/photo/full?photo_id=502475949276[^]

What I have tried:

Here is my code of attempting to draw unicode characters using DrawString Method.

int unicodeIndex = 10728;

this._provider = new CultureInfo("en-us");

string unicodeString = ((char)unicodeIndex).ToString(_provider);

this.Font = new Font("Times New Roman", arrowSize);


int x = 200;
int y = 200;

SizeF sizef = g.MeasureString(unicodeString+"W", this.Font, new PointF(x, y), _sformat);
int w = Convert.ToInt32(sizef.Width) + 2;
int h = Convert.ToInt32(sizef.Height) + 2;


RectangleF rectf = new RectangleF(x, y-h/2, w, h);


g.DrawString(unicodeString, this.Font, myBrush, rectf, myFormat);




This is the original code displaying unicode in Text Box using Times New Roman font from the above link shared.

// Display the desired Unicode characters
  private void btnList_Click(object sender, EventArgs e)
  {
      txtChars.Clear();
      txtCharCode.Clear();
      lblSample.Text = "";
      Cursor = Cursors.WaitCursor;
      Refresh();

      // Set the font size.
      float font_size = float.Parse(txtFontSize.Text);
      Font font = new Font("Times New Roman", font_size);
      txtChars.Font = font;
      lblSample.Font = font;

      // Display the characters.
      int min = int.Parse(txtMin.Text);
      int max = int.Parse(txtMax.Text);
      StringBuilder sb = new StringBuilder();
      for (int i = min; i <= max; i++)
          sb.Append(((char)i).ToString());
      txtChars.Text = sb.ToString();
      txtChars.Select(0, 0);

      Cursor = Cursors.Default;
  }
Posted
Updated 10-Dec-19 21:41pm
v8
Comments
[no name] 8-Dec-19 6:31am    
What is the value of myFormat? Do you mabye disabled font fallback here?
auto9817 8-Dec-19 7:59am    
Here is the value of myFormat.

myFormat = new StringFormat();
myFormat.Alignment = StringAlignment.Center;
myFormat.LineAlignment = StringAlignment.Center;
myFormat.FormatFlags = StringFormatFlags.NoWrap;
Richard MacCutchan 8-Dec-19 8:11am    
That is irrelevant, see my answer below.
[no name] 8-Dec-19 8:19am    
That is in no way irrelevant, looks like you don't know about font fallback ;)
Richard MacCutchan 8-Dec-19 9:06am    
Yes, and the fallback in this case is the little squares. All the OP has shown is the alignment values for his text.

The square symbols are merely substitutes for Unicode characters that have no equivalent in the selected font and character set. This is quite common when printing non-latin alphabets.
 
Share this answer
 
The problem is, that Graphics.DrawString will not find the Glyph in "Times New Roman" an for "Times New Roman" no "fallback" is defined.

One thing you can do is to add a "fallback font" for "Times New Roman" in the registry, btw from my point of view not a nice solution. For this, add another item of type REG_MULTI_SZ to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink
where the name is "Times New Roman" and the value should be "Cambria.ttc,Cambria".
Now also DrawString will find the missing glyph in Cambria.

Note:
As mentioned in my comment to the question TextRenderer.DrawText does work without this regsitry modification and looks to me the preferred way to solve your request.
 
Share this answer
 
Comments
Richard MacCutchan 11-Dec-19 4:14am    
It is not a good idea to mess with the registry.
[no name] 11-Dec-19 12:37pm    
*yawn* what an exciting comment... read through the text and you will (hopefully) recognize that I mention that this is not a nice solution and also give an option.
Richard MacCutchan 11-Dec-19 12:55pm    
So why mention it in the first place?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900