Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firemonkey: Adding a font from resource to memory and using it

In VCL, I could load a font from resource and without saving it I could use it from memory.

Here is the code I use and it works in VCL:

procedure TForm1.Button1Click(Sender: TObject);
var
   ResStream  : tResourceStream;
   FontsCount : DWORD;
begin
   ResStream  := tResourceStream.Create(hInstance, 'MyResourceName', RT_RCDATA);
   winapi.windows.AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount);
   ResStream.Free();
   button1.Font.name := 'MySavedFontNameInResource';
end;

In Firemonkey I just changed button1.Font.name to button1.Font.family but unfortunately the font didn't change. So I think this code is not compatible with firemonkey.

So in Firemonkey, how can I load a font from resource and save it temporary to memory and use it directly from there?

Update:

I saw these pages: Install font in firemonkey, How to use external fonts?

According to Mr Ed 's answer, it seems that there is no solution for this problem in FMX. But maybe we could load the font if we install it before running the app. I tried almost everything but I still can't load the the font.

like image 502
Sky Avatar asked Jan 29 '14 14:01

Sky


2 Answers

There is also StylesSettings.ssFamily : Boolean property in New Delphi and must be set to False to have custom font family working. The same with Size, Style and FontColor.

like image 146
Sebastian Xawery Wiśniowiecki Avatar answered Oct 30 '22 23:10

Sebastian Xawery Wiśniowiecki


This may or may not help.

Disclaimer

This requires extra researching that I have not gotten around to yet, but I believe this to be a good starting point.

Link for the MSDN page :- Custom Font Collections

You need to use the WinAPI.D2D1 unit (you may need FMX.TextLayout and FMX.Canvas.D2D as well) to gain access to the DirectWrite API. You can use this to get at the DirectWrite factories which will allow you to define and load a font from disk.

Once you have the font loaded AFAIK it should then be available to the entire application and hopefully all of the firemonkey controls. Its entirely possible that firemonkey only enumerates fonts at application load though, so this may all be for naught.

As I said, this requires research that I have not gotten to yet, so may only work if you are also custom painting your control (which I will be) and might not be suitable as a result.

like image 31
user3281284 Avatar answered Oct 30 '22 23:10

user3281284