2

I tried to add font from memory (embedded resource) and use it for my windows form (c++/cli) application... The code is working, but when the specified font is not installed on the computer the textbox is using the default font instead of my custom font. CompatibleTextRenderingDefault is set to true.

    System::Drawing::Text::PrivateFontCollection^ privateFont = gcnew System::Drawing::Text::PrivateFontCollection();

    IO::Stream^ fontStream = Reflection::Assembly::GetExecutingAssembly()->GetManifestResourceStream("textfont.otf");
    array<Byte>^ fontData = gcnew array<Byte>(fontStream->Length);
    fontStream->Read(fontData, 0, (int)fontStream->Length);
    fontStream->Close();

    pin_ptr<byte> fontAddress = &fontData[0];
    privateFont->AddMemoryFont((IntPtr)fontAddress, fontData->Length);

    this->TextBox_Username->Font = gcnew System::Drawing::Font(safe_cast<System::Drawing::FontFamily^>(privateFont->Families[0]), 9.749999F, System::Drawing::FontStyle::Bold);
    this->TextBox_Password->Font = gcnew System::Drawing::Font(safe_cast<System::Drawing::FontFamily^>(privateFont->Families[0]), 9.749999F, System::Drawing::FontStyle::Bold);
David Yaw
  • 27,383
  • 4
  • 60
  • 93
SylenZ
  • 23
  • 2
  • what would you expect to happen? The font is not known to the OS, so it is using the default system one as a fallback. – Igor May 02 '16 at 15:36
  • 1
    @Igor Thanks for the fast reply. Is there any way to load font from resource and use it without installing it? – SylenZ May 02 '16 at 15:49
  • do you use Qt? I have no idea how to do it there. – Igor May 02 '16 at 16:01
  • 1
    .net and GDI+ rendering to use memory font.. I found this method here and as I know it should work with fonts not installed since I load the .otf file from memory. – SylenZ May 02 '16 at 16:26

1 Answers1

0

Both methods (AddFontFile, AddMemoryFont) available to load font into PrivateFontCollection do not fully support OpenType fonts. Use TrueType font instead.

See also https://social.msdn.microsoft.com/Forums/en-US/40834a16-2378-4aeb-a499-25f835fe5738/opentype-font-as-embedded-resource?forum=winforms