Chinese characters are rendering in bold - c#

Silverlight renders few Chinese characters as Bold in a text box. This textbox has no font family set on it. Please refer to the following screenshot
In the above screenshot, first character is bolder than the other. One of the MSDN posts says that this is because of the font family problem and setting SimSun font family should fix the problem. Here is the screenshot after the applying the font family.
It looks like setting SimSun is actually fixing the issue. But I am not sure if this is the correct fix as my application allows to enter data in any language and not just Chinese. So I don't want to set font family to Chinese font.
I am wondering, since the data is Unicode (UTF16) encoded, the rendering engine should take care of choosing the correct font and render it properly right?
I am looking for proper ways to fix this problem. Any suggestions would be helpful.
Using Silverlight 3 with .NET3.5.

The problem is with the font's description of itself. Your default font, depending on your computer, is likely Arial, which may not render some Chinese characters very nicely. SimSun is a font that is designed to render Chinese characters, so just like Latin-based fonts tend to render English nicely, SimSun renders Han characters nicer.
Wikipedia has a list of Unicode fonts that are meant for internationalized text fields such as yours. Maybe give a few of them a try until you find one that meets your needs.
http://en.wikipedia.org/wiki/Unicode_typeface#List_of_Unicode_fonts

Related

How do I get the real font used by a Windows Form?

When displaying text in a Windows Form (like a TextBox for example) I can enter Chinese characters in it if I want, and they will get displayed correctly. However, the font used by the Form is not actually capable of rendering Chinese. As a result, if I try to use the Form's font in order to create a PDF file containing the text, it won't work:
myPdf.RenderText(myForm.Font, myForm.Text);
In this case in Windows 10, myForm.Font is just Microsoft Sans Serif (or Arial in older Windows versions,) which does not support Chinese (or CJK in general) and as a result the PDF won't render correctly when embedding the Arial font in it.
So, how do I find out which font is really used to render text in a Windows Form? It can't be what the Font property says it is. In this particular case, it's probably "SimSun" or some other system font that supports Chinese characters, but there doesn't seem to be any way whatsoever to find out.
How do I deal with this? If it's not possible to get the true font of a Form, then could I somehow detect what font (or combination of fonts) Windows would use to render a piece of text on a Form before actually displaying the text on that Form?

WPF character spacing irregular in RichTextBox

I have a problem with character spacing.
Basically I have something like this which comes from a txt file:
****************
*System Details*
****************
Looks nice and uniform, however, when I open have this go into a RichTextBox this happens:
Irregular character spacing example:
I've tried all different properties to try and stretch it, render it etc. but nothing works.
The data is coming in from code-behind OpenDialogBox that stores all the lines of the file in a string[]. A foreach loop then sends the lines into the RTB. (It needs to be a loop as each line gets checked)
Any help it greatly appreciated!
Many thanks
This is most likely a font choice problem. By default WPF uses Segoe UI on Windows 7 and above which is a non-monospaced font. This means that each character will not necessarily take up the same amount of space as each other character leading to issues if you are trying to align characters between lines. The easiest way to get alignment to work is by changing the font to a monospaced font by setting the FontFamily property on the RichTextBox.

How to choose a fallback font?

We generate some text that use specified font. If there is no such character in the specified font, font flalback replace it with other one. On export to other formats(for instance PDF) we need to know what characters are missed in specified font and what font should we use to take missed characters and show the text correctly.
It's possible to get the missed characters using Uniscribe, but how to know when could we find missed characters. We can't go through all fonts in search for right one because there could be hundred fonts installed. Even if we find and font with necessary characters we need to choose the most appropriate.

Text using Cambria Math font in Windows forms gets shifted vertically [duplicate]

I wanted to show some mathematical expressions in a winforms textbox. So I thought the "Cambria Math" font would be a good choice but the text looked strange due the high top and bottom margin of the font. First I thought I made a mistake but according to this question, it's the correct behavior of the font.
Why does Cambria Math have these big margin values and how can I display my string correctly in the textbox like Word 2010?
(Note that I know only a little bit about typography ;)
Edit: I had to make the textbox that tall otherwise the caret would be invisible. The font size of the textbox is set to 8.25pt
Cambria Math uses Microsoft's mathematical OpenType extensions.
Word 2007 and later understand these and display the text with reasonable spacing.
However, notepad and Word 2000 display the text with enormous spacing, just like winforms. I guess the font has this much space by default because some characters (like U+2320, top half integral) are much larger than the alphanumerics.
If you use Cambria Math with a font engine (such as the one used by winforms) that doesn't understand the math extensions, you're going to get the big spacing.
If you're displaying simple expressions you might as well use Cambria.

Pasting CJK characters to a RichTextBox adds an unwanted second font

If you paste the below into a RichTextBox, the CJK character and everything afterwards turns into a second font - SimSun.
Inserting a CJK character: 色
...makes this font change.
This affects the formatting of the RichTextBox in an unwanted way, and also massively slows down large copy/pastes.
I have tried setting the RichTextBox to use a unicode font such as "Lucida Sans Unicode", and also made sure the source I was copying from (Wordpad) was also using the "Lucida Sans Unicode" font. However, at the end of the day, when I paste to the RichTextBox, it forces the use of another font such as SimSun (or other fonts like "Shonar Bangla" or "Microsoft Himalaya" if we use another type of character).
Since "Lucida Sans Unicode" has them all, why can't it stick to the one font?
I noticed someone else also has a similar problem:
RichTextBox - retain original formatting (font), even after paste
Unfortunately, the solutions on that page didn't work for me as I still find the RTB uses another font after the CJK character.
I've been having this problem on and off, myself.
After flipping around here and there on the internet today, I noticed something on msdn and tried something. Add this line to your Form1_Load(), or to the form constructor AFTER the InitializeComponent() line:
richTextBox1.LanguageOption = RichTextBoxLanguageOptions.DualFont;
I'm no expert, and I can't tell you anything about WHY it works but it DOES for me!
Unfortunately despite its slightly confusing name, Lucida Sans Unicode doesn't contain CJK characters (as you could expect from fonts with Unicode in its name). This triggers some font fallback mechanism which results in a font that contains the required symbol. The only possible solution I see is to use a font that actually contains all required characters.
To complement Petr's answer (and partially use information from his comment), it turns out that many Unicode symbols render much slower in a RichTextBox.
But by filtering out unicode characters from about 1280 (0x500) and greater, I have found the paste to be as quick as before.

Categories

Resources