GraphicsPath AddString not support enough to the font when use right to left language if operating system lower than Win10
Asked Answered
R

2

11

I need to generate an image from a string in my WPF application, and show it. Here is my code:

// Create Image and generate string on it
System.Windows.Controls.Image img = new System.Windows.Controls.Image();


// This is the font that I need to render
Font DefaultFont = new Font("Oybab Tuz", 40f, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);

// Generate image from string
GraphicsPath graphicsPath = new GraphicsPath();

// As you see, the string include Right to left character, also include some other character in the middle
graphicsPath.AddString("سىناق123456789سىناق", DefaultFont.FontFamily, (int)DefaultFont.Style, DefaultFont.Size * 96f / 72f, new PointF(0f, 0f), StringFormat.GenericDefault);

// Turn the string to the image
RectangleF bounds = graphicsPath.GetBounds();

Bitmap bitmap = new Bitmap((int)(bounds.Width + bounds.X), (int)(bounds.Height + bounds.Y));
Graphics graphic = Graphics.FromImage(bitmap);

graphic.FillPath(new SolidBrush(System.Drawing.Color.FromArgb(0x33, 0x00, 0xFF)), graphicsPath);
graphicsPath.Dispose();
graphic.Dispose();


// turn the Image to ImageSource
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);

img.Source =  (ImageSource)(new ImageSourceConverter()).ConvertFrom(memoryStream);

// Add the image to the window content
this.Content = img;

Now this will display perfectly fine in windows 10, such like this:

In the windows 10 operating system

But, if the operating system lower than Windows 10, such like: Windows XP, Windows 7, Windows 8.X, it will display like this:

enter image description here

Also not only the number, if I put some other character such like :"&&"

"سىناق&&سىناق"

The lower operating system still shows it like:

"سىناق سىناق"

it seems like the operating system auto remove the characters in the middle.

I knew the font which I'm using is not include the number or the character which I put in it, But Windows 10 could display it right.

Here is the font: Download font file

Because the character will not only display in a specific place, so I can't split the string, and render it one by one.

So I really want to know why, also I hope some guys could give me advice to fix this problem when operating system lower than Windows 10. Thank you.

Retrorse answered 25/12, 2018 at 11:47 Comment(17)
The net library for a lot of methods are just wrappers that call windows dll. So if there is an operating difference then there are issues with the windows dll, the culture settings on the PC are different, or the installed fonts are different. Do you have all the windows updates on the PCs? What fonts are you using? Windows will automatically choose the closest font if the one selected is not available. Probably this error is due to different fonts being used. Find out the Font name that is being used in good and bad results.Helium
@jdweng, Thanks for reply, the font file address I have put it in my question, I also sure I install the all update in every operating system, I also confirm the culture settings are same and operating system is clean, not other fonts in it.Retrorse
Are all your characters support in the font. I suspect you have "Non Supported Unicode Characters". See : wavemetrics.com/forum/general/…Helium
@jdweng, As I said in my question before, the number and some character Not in the font, but I just want it work in lower version operating system as like Windows 10.Retrorse
I'm not sure how to fix. I do not think you want to make TUZ the default font since it is missing characters. You want the default font to contain all characters. Then use TUZ as the selected font. Using the Character Map tool in Word/Excel you allow you to adding missing characters to the TUZ font. That would be manual and do not know how to do it in software. I would use Excel and open the Character Map for TUZ and see what characters are missing.Helium
I am unable to reproduce that!! the same string is showed on both win 10 and win 7!! prntscr.com/m11xjjRoseannroseanna
@SamTheDev, That's because you're not download that font and install it, Then it would use operating system default font. Please download font and install it from that link which I put in my question before.Retrorse
When a font does not have a glyph for a requested code point (a.k.a. character), the system has to find another font to display it. So, you're basically observing this behavior. How it works is very complex and depends not only on the Windows version, but also on the PC you're running, and the technology you use (WPF vs Winforms, native GDI, etc.). Windows 10 just does a better job than its predecessors in this case. That doesn't mean you cannot get to the same result with lower versions. If you really want to scare yourself: msdn.microsoft.com/en-us/globalization/mt791278Microprint
My first advice would be to edit that font and simply add the characters you need. That's the easiest way by far. My second advice would be to try use WPF typography engine (DirectWrite) instead of System.Drawing (GDI+/Uniscribe). In other word, try to stick with the latest technology (this doesn't always work though...) Otherwise google for "Font Linking" "Font Fallback". You should be able to change the registry to get it working. learn.microsoft.com/en-us/globalization/input/font-technology archives.miloush.net/michkap/archive/2005/10/01/476022.htmlMicroprint
@SimonMourier, I just tried Font Linking not working. it only show correct when the caracter not together in notepad in Windows. WPF typography engine I did not try now, I need to generate font use some Stroke maybe inner or outer, I don't know it would support or not, even it will support I need to change more.Retrorse
The fact Windows 10 is more capable does not mean lower version have problems. It just mean Windows 10 is more modern and fills more gaps. As you said, the initial problem really comes from the font itself (you could for example embed a newer version with all needed characters in your app learn.microsoft.com/en-us/dotnet/api/…). You can't have modern capabilities with older technologies and OSes.Microprint
I had similar issues with differently drawn bold text and Chinese chars geekswithblogs.net/akraus1/archive/2014/12/01/160433.aspx but that was a nightmare to repro on different machines because the exact behavior did change between localized windows versions. But you could try out the GDI DrawString methods if that provides different answers.Permeance
@AloisKraus, I tried add my font name in in Font Link register key. but the application still not workingRetrorse
IMO, the best bet is to try to fix this with the font itself. I would try finding a different download of the same font, as it's possible that there's a version that has all the characters that you need. Another thing to consider is to try and find a similar font that includes all the needed characters, if that's an option. You could also edit the font (e.g. find a tool to paste in the missing glyphs from a different font), or find someone online to do it for you (e.g. on fiverr.com).Noontide
Do you get the same problem if you use MeasureString and DrawString instead of using the GraphicsPath technique?Birdcage
@Wyck, Graphics.DrawString is same result (But the render result of image not clear as GraphicsPath).Retrorse
@FilipMilovanović, Thank you but I really need a way to fix it without changing the font, Font link seems to a great way, but I don't know Is that my problem or not I can't let it worked.Retrorse
C
0

Visit this link: https://learn.microsoft.com/en-us/windows/win32/api/gdipluspath/nf-gdipluspath-graphicspath-addstring(inconstwchar_inint_inconstfontfamily_inint_inreal_inconstrect__inconststringformat)

It is a Windows website for the GraphicsPath AddString method and how to use it. If you scroll down, you will find a tab marked requirements. Double-check whether your windows operating systems meet those requirements. If they don't, the path won't work.

Corrientes answered 4/5, 2020 at 20:2 Comment(0)
W
-1

I found the following question from the past :

Generic GDI+ exception in GraphicsPath.AddString() with certain fonts and characters

It my be that in older versions of windows you will face some issues, There is a link for a specific KB that suppose to fix it - worth trying :)

By the way have you tried it with other fonts types or change font types just to check if something will change.

Hope it helped

Asaf

Wellington answered 1/1, 2019 at 5:53 Comment(3)
But how about in windows 8.1 and windows server 2012?Retrorse
Also how about XP?Retrorse
Hi, It is a Microsoft know issue. If they solved it just for Windows 7 , Vista and a few Servers there is nothing that i can do about it ;).Wellington

© 2022 - 2025 — McMap. All rights reserved.