views:

858

answers:

3

My App displays English, Japanese and Chinese characters on a TextBox and a LinkLabel. Currently, I check if there are unicode characters and change the font to MS Mincho or else leave it in Tahoma.

Now MS Mincho displays Japanese properly, but for Chinese I have to use Sim Sun. How can I distinguish between the two?

How can I ensure that unicode text are displayed properly regardless of the font/language?

+2  A: 

If you have unicode characters for each of the text, using a font that supports unicode should cover it properly for you (e.g. Arial Unicode MS).

shahkalpesh
This is perfect. I was tweaking around like what @dtb said. But this ArialUnicode font is not only good looking, but serves the purpose well... :D
Mugunth Kumar
It should be noted that Arial Unicode MS must be licensed before you can redistribute it.
epotter
A: 

You can't ensure that unicode text is displayed properly regardless of the font and language, because there is no single font that can render all possible unicode characters. You have to select a font that can display the unicode characters that you need to render.

MusiGenesis
@MusiGenesis: I think the question is: How do you detect the language so you can choose the right font, given that a string only contains characters of one language?
dtb
+1  A: 

All strings in C# are Unicode. The English (Latin), Japanese and Chinese code points are just located in different code point ranges.

I think you have two options:

  • Find a Unicode font that contains characters for all code points in all three language.

  • Try to guess the language and choose a font that contains the characters for the code points in that language.

For option 2 you can look at the Unicode charts to find out where the different code points are and expand your algorithm to guess the language.

Example for Hiragana:

bool IsHiragana(char ch)
{
    return (ch >= '\u3040') && (ch <= '\u309f');
}

bool IsHiragana(string s)
{
    return s.Count(IsHiragana) > 0;
}
dtb
@dtb: I don't think OP wants to distinguish between Japanese and Chinese etc (atleast that is not what is said). OP says "How can I ensure that unicode text are displayed properly regardless of the font/language?"
shahkalpesh
@shahkalpesh: Right, but there's no perfect answer to this. Not all fonts contain characters for all Unicode code points, so it's not possible to display arbitrary Unicode strings regardless of font.
dtb
@dtb: You are right as well. If I am right, Arial Unicode MS has the coverage for scripts OP wants.
shahkalpesh