Lazarus

Programming => Operating Systems => Topic started by: af0815 on August 27, 2019, 09:31:28 am

Title: to detect the default/standard font of the OS/Widgetset
Post by: af0815 on August 27, 2019, 09:31:28 am
Is it possible to detect the standard or default font on a OS. Or which Font the user have decided to use for the apps.

Actual i have on windows 10 'ArialMT' on RasPi Jessi 'LiberationSans', on RasPi Wheezy (and Buster) 'FreeSans'. But i found no function to detect the actual system standardfont.

How can i detect this ? Because Lazarus can handle this - can somebody give me an advice where the detection in Lazarus resides.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: dbannon on August 27, 2019, 11:13:40 am
Andreas, I had this problem in tomboy-ng. I found a workaround that does, sort of work for me.

KMemo always seemed to pop up with the default font but if it was changed, you needed to know what to change it back to.  So, at startup, I'd push a bit of text into the KMemo control, read its font, and then clear that text. User unaware. Can you apply this model to whatever control you are using ?

Davo
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: korba812 on August 27, 2019, 11:37:22 am
"Screen.SystemFont" indicates default system font.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: wp on August 27, 2019, 11:44:46 pm
But i found no function to detect the actual system standardfont.
Do you really need this? Just name the font "default" like the Object Inspector does.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 28, 2019, 02:44:44 am
The Font property is a problem since years. You can write every nonsens into the Font.Name and you dont't even get an error message, that the font was not found.

Test this (just done in Linux):

The Font in Tooglebox1 is "Sans standard" which is the Linux default.
Code: Pascal  [Select][+][-]
  1. ToggleBox1.Font.Name := 'HonkDiHonk';

where 'HonkDiHonk' is pure nonsense.

No error message. Nothing changes. The font is still default.

But now it gets funny:

Code: Pascal  [Select][+][-]
  1. showMessage (ToggleBox1.Font.Name);

And he shows you not 'default'.
And he shows you not 'Sans standard'
He shows you 'HonkDiHonk'

So even if you ask for the component.Font.Name, you might get not the right answer.


And yes, @wp: some people like design. And not only a machine for number crunshing.

Winni
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: sstvmaster on August 28, 2019, 02:48:25 pm
Something like this?

Code: Pascal  [Select][+][-]
  1. function GetDefaultFontSize: integer;
  2. var
  3.   fd: TFontData;
  4. begin
  5.   fd := GetFontData( Form1.Font.Handle );
  6.   result := round((fd.Height * 72 / Form1.Font.PixelsPerInch) * (-1));
  7. end;
  8.  
  9. procedure TForm1.FormCreate(Sender: TObject);
  10. begin
  11.   // set font name to nonsense
  12.   Form1.Font.name := 'GoodMorningStarshine';
  13.   Label1.Caption := 'Default font name : ' + GetFontData(Self.Font.Handle).Name;
  14.   Label2.Caption := 'Default font size : ' + IntToStr(GetDefaultFontSize);
  15.  
  16.   // get and set system font name/size
  17.   Form1.Font.Name := Screen.SystemFont.Name; // only to be shure
  18.   Form1.Font.SetDefault; // <-- important !!!
  19.   Label3.Caption := 'System font name : ' + Screen.SystemFont.Name;
  20.   Label4.Caption := 'System font size : ' + IntToStr(Screen.SystemFont.Size);
  21. end;
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 28, 2019, 03:07:11 pm
Yes, that is the way it is normaly done.


But insert in your Formcreate as first line the following:

Code: Pascal  [Select][+][-]
  1. Form1.Font.name := 'GoodMorningStarshine';
  2.  

And suddenly you don't know nothing!

Winni
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: sstvmaster on August 29, 2019, 12:03:28 am
And why i should do this? If you override the font name with nonsense, you get nonsense.

I have updated the code in my post please try.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 29, 2019, 01:34:56 am
Yeah, that's what I wanted to show.

Everywhere the compiler or the RTL tries to keep you away from nonsense, but not in this case.

Think about those scenarios:

*In the Formcreate you make a typo inside the string Font.Name: The compiler will not detect it and the RTL - as shown - doesn't care about your nonsens. No error message is given.

* Your program starts on a computer, where the desired font is not installed. As above: No errors detected and no message given.

That was what I wanted to show.

Winni
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: af0815 on August 30, 2019, 10:23:13 am
The Problem i have, is on different RasPi versions the fonts differs and i have a report without bindings to the lcl. So i have to say the reportengine the correct font and size. And this font must available. This must also be done if i use a lcl renderer.

Now i have some Parts to deal with. Thanks
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: Thaddy on August 30, 2019, 01:33:47 pm
If your application relies on a font being present it may be a better idea to include the font as a resource.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 30, 2019, 02:50:05 pm
In Windows installing a font from a resourcee is possible. In Linux you need root rights. Very old, unsolved subject.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: sash on August 30, 2019, 03:29:14 pm
installing a font from a resourcee ... In Linux you need root rights

I always thought it's possible to do on per user basis:
Code: Bash  [Select][+][-]
  1. cp yourfont.ttf ~/.fonts
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: sstvmaster on August 30, 2019, 05:52:47 pm
Is it possible do this in Linux?

Code: Pascal  [Select][+][-]
  1. ...
  2. var
  3.   vFont, vFontName: String;
  4. ...
  5.  
  6. procedure TForm1.FormCreate(Sender: TObject);
  7. begin
  8.   vFont := 'arial.ttf';  // font in same folder as exe
  9.   vFontName := 'Arial';
  10.  
  11.   if AddFontResource(PChar(GetCurrentDir + PathDelim + vFont)) <> 0 then begin
  12.     Font.Name := vFontName;
  13.     Font.Size := 12;
  14.   end;
  15. end;
  16.  
  17. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  18. begin
  19.   if Font.Name = vFontName then
  20.     RemoveFontResource(PChar(GetCurrentDir + PathDelim + vFont));
  21. end;
  22.  
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 30, 2019, 07:34:01 pm
No, this Windows only.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 30, 2019, 08:00:08 pm
Discussion and workaround is here:

https://forum.lazarus.freepascal.org/index.php/topic,26003.msg159027.html (https://forum.lazarus.freepascal.org/index.php/topic,26003.msg159027.html)

Winni
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 30, 2019, 09:32:18 pm
@sash

Quote
I always thought it's possible to do on per user basis:

cp yourfont.ttf ~/.fonts



In Fedora the folder path is /home/{user}/.local/share/fonts

Winni
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: sash on August 30, 2019, 09:57:30 pm
IMO, should work both, I just wrote ~/.fonts beacuse it's shorter (and actually it is deprecated in favor of ~/.local/share/fonts). But point was there's no need for root.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: af0815 on August 30, 2019, 10:43:50 pm
back to topic - i want the actual font, not to inject my own font.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: af0815 on August 30, 2019, 10:47:06 pm
If your application relies on a font being present it may be a better idea to include the font as a resource.
No, i want to know, what is the default font. Background: Between Jessi and Wheezy there must be a change in Raspbian in the default font (from liberationSans to freeSans).

Actual i have update my installscripts and install booth font-package.

fpreport cannot detect the font and use a predefined, not existing font :-( and on a LCL printout you see NOTHING. But there is another Bug ? prevent for a correct printout. I have fixed this with a 'hard' patch, but the issue with the font was only prevent by install all fonts on the systems. Regardless if they are Jessi or Wheezy (Buster) based.
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: winni on August 30, 2019, 11:31:27 pm
After installing new fonts in Linux you must update the cache:

Code: Bash  [Select][+][-]
  1. fc-cache -fv

Did you do it?

Winni
Title: Re: to detect the default/standard font of the OS/Widgetset
Post by: sash on August 31, 2019, 12:46:22 pm
If your application relies on a font being present it may be a better idea to include the font as a resource.

And maybe even introduce an option where user selects a font explicitly with TFontDialog.
TinyPortal © 2005-2018