Recent

Author Topic: Incorrect code in /fontenum/ demo project  (Read 897 times)

zoltanleo

  • Sr. Member
  • ****
  • Posts: 409
Incorrect code in /fontenum/ demo project
« on: May 23, 2022, 10:16:14 pm »
Hi all

While researching the demo application code (<LazDir>\examples\fontenum\), I found that the source code is:
Code: Pascal  [Select][+][-]
  1. function EnumFamilyFonts(
  2.   var eLogFont: TEnumLogFontEx;
  3.   var Metric:TNewTextMetricEx;
  4.   FontType:longint;
  5.   Data:LParam):longint; stdcall;
  6. <skiped>
  7.   // collect styles
  8.   s :=eLogFont.elfStyle;
  9.   if LStyles.IndexOf(s)<0 then begin
  10.     // encode bold, italic
  11.     n := eLogFont.elfLogFont.lfItalic;
  12.    
  13.     if (eLogFont.elfLogFont.lfWeight > FW_MEDIUM) then n := n or 2;//<-- incorrect code
  14.   end;                                                                  
  15.  

returns the same font style for italic and italic bold (see screenshot).

I made some changes. I think the bug has been fixed now.
Code: Pascal  [Select][+][-]
  1. //probably it's correct code    
  2. if (eLogFont.elfLogFont.lfWeight > FW_MEDIUM)
  3.   then n := (n or 2)
  4.   else
  5.     if (n > 0) then n:= (n xor 2);

Can someone create a ticket in the bug tracker if I'm right and this problem is solved in this way?
Win10 LTSC x64/Deb 11 amd64/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.7

Sorry for my bad English, I'm using translator ;)

Hightower

  • Newbie
  • Posts: 3
Re: Incorrect code in /fontenum/ demo project
« Reply #1 on: May 24, 2022, 02:48:14 pm »
Fonts are needed to render text properly. This is a good place to start since this program uses the same fonts that your OS does. Here’s how to do it:
 Click on ‘Go’ at the top of the window
 Select ‘go to folder…’
 Type “~Library” in the search box

wp

  • Hero Member
  • *****
  • Posts: 9757
Re: Incorrect code in /fontenum/ demo project
« Reply #2 on: May 24, 2022, 03:39:17 pm »
Fixed it in main (replaced the "xor 2" by "and not 2" which is less of a mind-twister to me).

zoltanleo

  • Sr. Member
  • ****
  • Posts: 409
Re: Incorrect code in /fontenum/ demo project
« Reply #3 on: May 24, 2022, 06:45:57 pm »
Fixed it in main (replaced the "xor 2" by "and not 2" which is less of a mind-twister to me).

I did some more experimenting and found that text formatting "doesn't work" for Linux. As a result, I tried to make a few more changes in the code so that the demo example worked correctly.

Code: Pascal  [Select][+][-]
  1. function EnumFamilyFonts(...);
  2.     //if (eLogFont.elfLogFont.lfWeight > FW_MEDIUM) then n:= n or 2; <--- instead
  3.  
  4.     if (n = 0) //regular,bold
  5.     then
  6.       case eLogFont.elfLogFont.lfWeight of
  7.         FW_NORMAL: n:= 0;
  8.         FW_BOLD: n:= 2;
  9.       end
  10.     else //italic, italic bold
  11.       case eLogFont.elfLogFont.lfWeight of
  12.         FW_NORMAL: n:= 1;
  13.         FW_BOLD: n:= 3;
  14.       end;

and

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.SelectFont;
  2. ....
  3.   //F.Style := [];
  4.   //if i and 1 <> 0 then F.Style := F.Style + [fsItalic];
  5.   //if i and 2 <> 0 then F.Style := F.Style + [fsBold];
  6.   i := ptrint(lbStyles.Items.Objects[lbStyles.ItemIndex]);
  7.   case i of
  8.     1: F.Style := [fsItalic];
  9.     2: F.Style := [fsBold];
  10.     3: F.Style := [fsBold, fsItalic];
  11.   else F.Style:= [];
  12.   end;
  13. ....

Someone test my work please
Win10 LTSC x64/Deb 11 amd64/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.7

Sorry for my bad English, I'm using translator ;)

wp

  • Hero Member
  • *****
  • Posts: 9757
Re: Incorrect code in /fontenum/ demo project
« Reply #4 on: May 24, 2022, 07:57:58 pm »
No, this is not working either. (it just rewrites the idea to set bit 1 for italic and bit 2 for bold styles in a different way (*) ). I tested Ubuntu and OpenSUSE (gtk2), as well as Mint (gkt2, qt), and in none of them the styles are displayed. When I add 
Code: Pascal  [Select][+][-]
  1.   with eLogFont do
  2.     DebugLn(Format('Name: %s, Style: %s - lfWeight: %d, lfItalic: %d', [
  3.       elfLogFont.lfFaceName, elfStyle, elfLogFont.lfWeight, elfLogFont.lfItalic
  4.     ]));  

after the "begin" of EnumFamilyFonts I see in the console output window that the elfLogFont members lfWeight and lfItalic are not reported correctly in Linux. lfWeight is always 400 (= FW_NORMAL) and lfItalic is always 0 (= false), no matter what eLogFont.elfStyle says.

It would be tempting to extract the missing font style from the eLogFont.elfStyle string, but this is deceiving because the string is localized - at least on Windows.

So, I think it must be accepted that this example does not work on Linux.

But maybe the Linux experts have better ideas...

-----------------
(*)
I think that part can still be improved and coded in a way analogous to SelectFont:
Code: Pascal  [Select][+][-]
  1.  // collect styles
  2.   s :=eLogFont.elfStyle;
  3.   if LStyles.IndexOf(s)<0 then begin
  4.     // encode italic (bit 1), bold (bit 2) -- see SelectFont()
  5.     n := 0;
  6.     if eLogFont.elfLogFont.lfItalic <> 0 then
  7.       n := n or 1;
  8.     if eLogFont.elfLogFont.lfWeight > FW_MEDIUM then
  9.       n := n or 2;
  10.     LStyles.AddObject(eLogFont.elfStyle, TObject(ptrint(n)));
  11.   end;                        
« Last Edit: May 24, 2022, 08:04:45 pm by wp »

wp

  • Hero Member
  • *****
  • Posts: 9757
Re: Incorrect code in /fontenum/ demo project
« Reply #5 on: May 25, 2022, 12:01:26 am »
Found that the font styles are named consistently "Bold" and "Italic" in gtk2. In qt there is also an "Oblique", as well as several intermediate weight styles which the LCL does not paint anyway; there was only one font with localized style names on my notebook with qt.

Therefore, I extended the EnumFamilyFonts function of the example to search these key words in the style string for the Linux widgetsets, and committed this:

Code: Pascal  [Select][+][-]
  1.   // collect styles
  2.   s :=eLogFont.elfStyle;
  3.   if LStyles.IndexOf(s)<0 then begin
  4.     // encode bold (bit 0), italic (bit 1) -- see SelectFont
  5.     n := 0;
  6.     {$IFDEF LCLWin32}
  7.     if (eLogFont.elfLogFont.lfItalic <> 0) then
  8.       n := n or 1;
  9.     if (eLogFont.elfLogFont.lfWeight > FW_MEDIUM) then
  10.       n := n or 2;
  11.     {$ENDIF}
  12.     {$IF DEFINED(LCLGtk2) or DEFINED(LCLGtk3) or DEFINED(LCLQt) or DEFINED(LCLQt5)}
  13.     s := Lowercase(s);
  14.     if (pos('italic', s) <> 0) or (pos('oblique', s) <> 0) then
  15.       n := n or 1;
  16.     if (pos('bold', s) <> 0) then
  17.       n := n or 2;
  18.     {$ENDIF}
  19.     LStyles.AddObject(eLogFont.elfStyle, TObject(ptrint(n)));
  20.   end;

Not very elegant and not perfect, but at least better than the present situation.

zoltanleo

  • Sr. Member
  • ****
  • Posts: 409
Re: Incorrect code in /fontenum/ demo project
« Reply #6 on: May 25, 2022, 01:55:23 am »

Therefore, I extended the EnumFamilyFonts function of the example to search these key words in the style string for the Linux widgetsets
Thank U, Werner. Nice work!

Also I found that the list of font styles is not showing for Darwin Cocoa.
Win10 LTSC x64/Deb 11 amd64/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.7

Sorry for my bad English, I'm using translator ;)

dbannon

  • Hero Member
  • *****
  • Posts: 2042
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Incorrect code in /fontenum/ demo project
« Reply #7 on: May 25, 2022, 02:13:42 am »
WP's solution applies to only Linux and Windows. My Mac is Sierra so probably too old to be authoritative. Some Mac user ??

WP, these commits you are making wrt the Examples, there seems a lot of small fixes. I was trying to group all the changes I made into a small number of merges so it was easy to list them if and when its ultimately merged to a release. Is there a way to keep track of all these small merges ?

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

wp

  • Hero Member
  • *****
  • Posts: 9757
Re: Incorrect code in /fontenum/ demo project
« Reply #8 on: May 25, 2022, 06:41:58 am »
Also I found that the list of font styles is not showing for Darwin Cocoa.
Yes. I guess the EnumFontFamiliesEx function is not fully implemented for the cocoa widgetset, yet. It only populates the filename and fullname members in the LogFont, and the Metric members are zero, as well. Your screenshot of the system dialog shows that the needed information does exist (of course), but I don't have the knowledge of extract it from mac's internal structures.

wp

  • Hero Member
  • *****
  • Posts: 9757
Re: Incorrect code in /fontenum/ demo project
« Reply #9 on: May 25, 2022, 06:51:54 am »
WP, these commits you are making wrt the Examples, there seems a lot of small fixes. I was trying to group all the changes I made into a small number of merges so it was easy to list them if and when its ultimately merged to a release. Is there a way to keep track of all these small merges ?
I don't know. Of course I try to make commits of these small things as complete as possible. But it usually happens that after I committed a fix it comes to my mind that I should have checked also some other aspect. And this leads to the next commit, and so on... Sorry, that's the way it is.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4159
  • Compiler Developer
Re: Incorrect code in /fontenum/ demo project
« Reply #10 on: May 25, 2022, 09:23:38 am »
Also I found that the list of font styles is not showing for Darwin Cocoa.
Yes. I guess the EnumFontFamiliesEx function is not fully implemented for the cocoa widgetset, yet. It only populates the filename and fullname members in the LogFont, and the Metric members are zero, as well. Your screenshot of the system dialog shows that the needed information does exist (of course), but I don't have the knowledge of extract it from mac's internal structures.

Then best post it as a bug report and someone more knowledgable in Cocoa will take a look. :)

zoltanleo

  • Sr. Member
  • ****
  • Posts: 409
Re: Incorrect code in /fontenum/ demo project
« Reply #11 on: May 25, 2022, 10:01:53 pm »
Then best post it as a bug report and someone more knowledgable in Cocoa will take a look. :)

The corresponding ticket has been created:
https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/39771

Thank a lot WP
Win10 LTSC x64/Deb 11 amd64/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.7

Sorry for my bad English, I'm using translator ;)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2747
    • havefunsoft.com
Re: Incorrect code in /fontenum/ demo project
« Reply #12 on: May 30, 2022, 10:09:44 pm »
i did post the link to the branch with fixes at the issue.

 

TinyPortal © 2005-2018