international font selection

  • Thread starter Thread starter Jim Puls
  • Start date Start date
J

Jim Puls

I'm writing a Windows Forms application in C# which will run generally in a
Western environment; e.g., EN-US. I need to access a unicode font on the
user's system which is capable of displaying Japanese characters - often
there will be one or more of such fonts, but I don't know which ones by
name. If one does not exist, I need to default to a public domain font to be
included with the app.

How do I determine which fonts are available on the user's system, and if
they have the required characters? I could come up with a short list of
probable font name candidates, but is there a better way?

If there are any references to URLs which have discussed this, that would be
fine!

Thanks,

- Jim
 
To determine which fonts are available, you can instantiate an
InstalledFontCollection object and then read its Families property.
This will give you an array of FontFamily objects, and FontFamily.Name
gives you the font name.

I'm not sure how to detect which characters are available in a font,
though. I'd be surprised if there wasn't a way to do it, but I didn't
see it in a quick skim through the docs.
 
Looking specifically for Japanese, you can base it on a font charset value
of 128?
 
Hi Jim,

There is a GDI API function GetGlyphIndices which will help you very much.
The function can be used to determine whether a glyph exists in a font.
Here is a demo below:

1. Add the class below into your project which is used to call the GDI
API function.

public class Win33
{
[DllImport("Gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, // handle to DC
IntPtr hgdiobj // handle to object
);
[DllImport("Gdi32.dll", CharSet=CharSet.Unicode)]
public static extern int GetGlyphIndices(
IntPtr hdc, // handle to DC
[MarshalAs(UnmanagedType.LPWStr)]
string lpstr, // string to convert
int c, // number of characters in string
Int16[] pgi, // array of glyph indices
int fl // glyph options
);
}

2. In order to get the hdc easier, I hereby override the OnPaint()
function (as below). You may achieve your individual aim with your own
method.

protected override void
OnPaint(System.Windows.Forms.PaintEventArgs e)
{
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
14,
FontStyle.Regular,
GraphicsUnit.Point);
System.IntPtr hd = e.Graphics.GetHdc();
Win33.SelectObject(hd,font.ToHfont());
string str = "hello";
int count = str.Length;
Int16[] rtcode = new Int16[count];
Win33.GetGlyphIndices(hd,str,count,rtcode,0xffff);
e.Graphics.ReleaseHdc(hd);
}

If a certain char is not supported by the font, the
"rtcode"(as above) will return zero.
[INFO]
Here is a helpful link about glyph indices of font.:
http://support.microsoft.com/default.aspx?scid=kb;en-us;241020


Regards,

Peter Huang
=============
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
Back
Top