Recent

Author Topic: to detect the default/standard font of the OS/Widgetset  (Read 944 times)

af0815

  • Sr. Member
  • ****
  • Posts: 344
to detect the default/standard font of the OS/Widgetset
« 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.
regards
Andreas

dbannon

  • Hero Member
  • *****
  • Posts: 644
    • tomboy-ng, a rewrite of the classic Tomboy
Re: to detect the default/standard font of the OS/Widgetset
« Reply #1 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
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

korba812

  • Full Member
  • ***
  • Posts: 104
Re: to detect the default/standard font of the OS/Widgetset
« Reply #2 on: August 27, 2019, 11:37:22 am »
"Screen.SystemFont" indicates default system font.

wp

  • Hero Member
  • *****
  • Posts: 6137
Re: to detect the default/standard font of the OS/Widgetset
« Reply #3 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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

winni

  • Full Member
  • ***
  • Posts: 172
Re: to detect the default/standard font of the OS/Widgetset
« Reply #4 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

sstvmaster

  • Full Member
  • ***
  • Posts: 116
Re: to detect the default/standard font of the OS/Widgetset
« Reply #5 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;
« Last Edit: August 29, 2019, 12:48:50 am by sstvmaster »
Lazarus 2.0.4 x32
Lazarus 2.1.0 Trunk x32
OS Win 7 32bit

winni

  • Full Member
  • ***
  • Posts: 172
Re: to detect the default/standard font of the OS/Widgetset
« Reply #6 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

sstvmaster

  • Full Member
  • ***
  • Posts: 116
Re: to detect the default/standard font of the OS/Widgetset
« Reply #7 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.
« Last Edit: August 29, 2019, 12:43:43 am by sstvmaster »
Lazarus 2.0.4 x32
Lazarus 2.1.0 Trunk x32
OS Win 7 32bit

winni

  • Full Member
  • ***
  • Posts: 172
Re: to detect the default/standard font of the OS/Widgetset
« Reply #8 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

af0815

  • Sr. Member
  • ****
  • Posts: 344
Re: to detect the default/standard font of the OS/Widgetset
« Reply #9 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
regards
Andreas

Thaddy

  • Hero Member
  • *****
  • Posts: 8673
Re: to detect the default/standard font of the OS/Widgetset
« Reply #10 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.
Most people that want to use threading should learn to patch their jeans first: use a needle.

winni

  • Full Member
  • ***
  • Posts: 172
Re: to detect the default/standard font of the OS/Widgetset
« Reply #11 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.

sash

  • Sr. Member
  • ****
  • Posts: 265
Re: to detect the default/standard font of the OS/Widgetset
« Reply #12 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
Lazarus 2.0.2 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 19.04 XFCE

sstvmaster

  • Full Member
  • ***
  • Posts: 116
Re: to detect the default/standard font of the OS/Widgetset
« Reply #13 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.  
Lazarus 2.0.4 x32
Lazarus 2.1.0 Trunk x32
OS Win 7 32bit

winni

  • Full Member
  • ***
  • Posts: 172
Re: to detect the default/standard font of the OS/Widgetset
« Reply #14 on: August 30, 2019, 07:34:01 pm »
No, this Windows only.