Cannot use ac3 fonts with AddFontResource

P

Pat O

I have read in a couple of different places that an alternative to
font linking is to load the font for a language with the Win32 API
call AddFontResource. This is often suggested with fonts that have
been compressed (ac3 files), as a way of ensuring that the correct
font is loaded as only one AGFA compressed font can be active at a
time. When I attempt to create a form that has various labels on it
(one each for English, Chinese, Japanese, and Korean) I find that each
label is assigned the Tahoma font despite asking for a language
appropriate font. I then added code to load the fonts directly using
AddFontResource. For each font I get an error 13 back. This is
INVALID_DATA. Any idea why I would get this? I am able to load TTF
and TTC files just fine. Here is the code, note that it is C# and I
am inter oping in the call to AddFontResource.


[DllImport("Coredll.dll", SetLastError = true)]
private static extern int AddFontResource(string fontpath);

// If I change the file names below to point to ttc files all is
well.
private void CalcMemoryUse()
{
loadFont(@"/Windows/cour.ttf", @"MS Gothic", @"MS Gothic:
{0}KB", lblMSGothic, 1);
loadFont(@"/Windows/msgothic.ac3", @"MS Gothic", @"MS Gothic:
{0}KB", lblMSGothic, 3);
loadFont(@"/Windows/simsun.ac3", @"Sim Sun", @"Sim Sun: {0}KB",
lblSimSun, 2);
loadFont(@"/Windows/gulim.ac3", @"Gulim", @"Gulim: {0}KB",
lblGulim, 2);
}

private void loadFont(string fontFileName, string fontName, string
format, Label outputLabel, int fontCount)
{
long beginTotalMemory = GC.GetTotalMemory(true);
int loaded = AddFontResource(fontFileName);
if (loaded != fontCount)
{
// Use the Win32Exception class to lookup the error code and
convert it to a message.
int errorCode = Marshal.GetLastWin32Error();
string msg = string.Format(@"Failed to load {0} font from file
{1} - Error:{2}|{3}", fontName, fontFileName, errorCode, new
Win32Exception(errorCode).Message);
MessageBox.Show(msg, @"Failed to load Font",
MessageBoxButtons.OK, MessageBoxIcon.Hand,
MessageBoxDefaultButton.Button1);
outputLabel.Text = msg;
}
else
{
outputLabel.Text = FormattedKBUsed(format, beginTotalMemory,
GC.GetTotalMemory(true));
}
}

private string FormattedKBUsed(string format, long bytesPre, long
bytesPost)
{
Debug.Assert(bytesPost >= bytesPre);
double delta = (bytesPost - bytesPre)/1024.0;
return string.Format(format, delta);
}

Pat O
Cognex, Corp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

"embedding" fonts in Excel 1
SendMessage, fonts 2
RTF Render with Scale 3

Top