0

I use some embedded fonts in my .Net 4.5 software. Everything is OK when I run software on Windows 10 but when I tried to test the software on Windows 7 virtual machine, it threw this exception:

Font 'B Koodak' does not support style 'Regular'.

Here is how I load embedded fonts from resources:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
    IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);

public readonly PrivateFontCollection privateFontCollection = new PrivateFontCollection();

public readonly FontFamily Samim;

private readonly FontFamily IranNastaliq;

private readonly FontFamily BKoodak;

private readonly FontFamily BNazanin;

public MainForm()
{
    uint temp = 0;
    byte[][] fontData = new byte[][] { Properties.Resources.Samim, Properties.Resources.IranNastaliq_Web,
        Properties.Resources.BNazanin, Properties.Resources.BKoodak };

    for (int i = 0; i < fontData.Length; i++)
    {
        IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData[i].Length);
        System.Runtime.InteropServices.Marshal.Copy(fontData[i], 0, fontPtr, fontData[i].Length);
        privateFontCollection.AddMemoryFont(fontPtr, fontData[i].Length);
        AddFontMemResourceEx(fontPtr, (uint)fontData[i].Length, IntPtr.Zero, ref temp);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
    }

    Samim = privateFontCollection.Families[3];
    IranNastaliq = privateFontCollection.Families[2];
    BNazanin = privateFontCollection.Families[1];
    BKoodak = privateFontCollection.Families[0];


    InitializeComponent();

    Application.CurrentCulture = new CultureInfo("fa-IR");
    
    // Other codes...
}

private void Draw(ref graphics)
{
    Font font = new Font(BKoodak, 18);
    graphics.DrawString("سلام", font, new SolidBrush(Color.Black), 0, 0, new StringFormat());
}

Why it is OK on Windows 10 but not Windows 7? How should I fix it?


EDIT:
I found this font only supports bold and outline mode. In Microsoft Word, there is no difference between bold and regular styles of this font.
So I changed Font font = new Font(BKoodak, 18); to Font font = new Font(BKoodak, 18, FontStyle.Bold); and now it works on Windows 7.

Now the question is: Why did regular style work OK in Windows 10 despite this font doesn't support regular style?

AmirSina Mashayekh
  • 498
  • 1
  • 5
  • 21
  • *"How should I fix it?"* - stop using software products which are [no longer supported](https://support.microsoft.com/en-us/windows/windows-7-support-ended-on-january-14-2020-b75d4580-2cc7-895a-2c9c-1466d9a53962). – Sinatr Feb 09 '21 at 08:44
  • Does this answer your question? [Font 'Times New Roman' does not support style 'Regular'](https://stackoverflow.com/questions/1245877/font-times-new-roman-does-not-support-style-regular) – Sinatr Feb 09 '21 at 08:46
  • @Sinatr Do you mean Windows 7? I wish I could drop Windows 7 support but most of users of my software are probably still using Windows 7 :( – AmirSina Mashayekh Feb 09 '21 at 08:48
  • @Sinatr I checked that post before. That was about fonts installed on system but my problem is with embedded fonts. My software is a single .exe file. That font (B Koodak) isn't installed on Windows 7 virtual machine. I even installed that font and tried again but as I guessed, problem didn't solve as software has nothing to do with system fonts. It only uses its embedded fonts. – AmirSina Mashayekh Feb 09 '21 at 08:53
  • Have you seen this: [How to: Construct Font Families and Fonts](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-construct-font-families-and-fonts?view=netframeworkdesktop-4.8)? – Maciej Los Feb 09 '21 at 09:54

0 Answers0