0
votes

This is one of the strange behavior of csharp code under my testing.

I use DrawString method to draw some unicode character under Times New Roman Font. However, I can draw some characters and but some characters are just drawn as blank square (i.e. unsupported character.

To elaborate more, I can draw 42 (*) and 43 (+) without any problem using DrawString method.

But I can not draw 9978 and 9900. Both 9978 and 9900 are the supported characters by Times New Roman. Check the font map here.

https://www.martinstoeckli.ch/fontmap/fontmap.html

For your information, when I try to print the same characters in the text box window control, everything works fine. Textbox show all the supported characters including 9978 and 9900.

However, just this DrawString method is inconsistent under my testing. Why I can not draw these supported characters under the font 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", 10);


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);

Some more clue here as this seems tough problem.

I am currently using East Asian Language pack installed on my Window. Is there any possibility that East Asian Language pack installed on my window can cause this problem ? For example, in localized window with Chinese and Japanese language, "Times New Roman" font might be not called correctly ? Just guess though.

2
9978 is dot and 9900 is a Persian character. change your font to "Tahoma" and try again. โ€“ XAMT
I changed to "Tahoma" but it does not work. I only see the blank square. However, 42 (*) and 43 (+) are working fine under "Tahoma" Font. โ€“ learner101

2 Answers

0
votes

I changed your code a bit and It's working well now:

private void Form1_Paint(object sender, PaintEventArgs g)
{
    CultureInfo _provider = new CultureInfo("en-us");

    String drawString = ((char)9900).ToString(_provider);

    Font drawFont = new Font("Times New Roman", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);

    float x = 150.0F;
    float y = 50.0F;

    StringFormat drawFormat = new StringFormat();

    g.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
}
0
votes

Times New Roman (any version, I'm quite sure) actually does not have the characters in question.

The second sentence on https://www.martinstoeckli.ch/fontmap/fontmap.html says:

What you see on this page is all declared as font Arial, though the operating system and the browser can substitute missing characters with characters from other fonts.

What you're seeing on that webpage is font substitution.