Recent

Author Topic: Finding List of Fonts & Printers  (Read 884 times)

dsiders

  • Hero Member
  • *****
  • Posts: 1677
Re: Finding List of Fonts & Printers
« Reply #15 on: August 08, 2026, 04:38:10 pm »
Why  F := FontInUse.Name  is unacceptable still puzzles me.  ?? 🤷‍♂️

I'll bet that if you examine the value, it is
Code: Pascal  [Select][+][-]
  1. 'default'
- the unresolved font name. And that does not appear in the font list.

Boleeman

  • Hero Member
  • *****
  • Posts: 1167
Re: Finding List of Fonts & Printers
« Reply #16 on: August 08, 2026, 05:40:11 pm »
Not sure J-G if you know but

XEL controls and KControls both on OPM has

XelFontManager , XelFontPreview  and  KPrintSetupDialog

J-G

  • Hero Member
  • *****
  • Posts: 1209
Re: Finding List of Fonts & Printers
« Reply #17 on: August 08, 2026, 06:21:41 pm »
Why  F := FontInUse.Name  is unacceptable still puzzles me.  ?? 🤷‍♂️

I'll bet that if you examine the value, it is
Code: Pascal  [Select][+][-]
  1. 'default'
- the unresolved font name. And that does not appear in the font list.
That's solved the issue for me @dsiders  -  though it has also flagged up a further question.

You are quite correct in that FontInUse at the time of assignment did not have a 'name' (I'll presume that it had been given 'default' automatically upon creation). What I thought I'd done was equate it to the font assigned to 'Character' at design time but in fact that didn't happen until after the call to Populate_FontList - which is where I had used F := FontInUse.Name - - -  and got the error.

Moving FontInUse := Character.Font to before the call to 'Populate' resolves the problem and I can 'watch' FontInUse since the ACCESS VIOLATION doesn't appear.

Now, the more peculiar thing is that at design time I'd allocated 'Tahoma' as the font for 'Character'  -  I'd later determined that 'Segoe UI' would be preferable (in the light of information about UNICODE) and I'd manually assigned that name in the FormCreate.  Looking now at the Object Inspector and the properties of 'Character', 'Segoe UI' is not in the list of fonts ???  but it IS in my FontList ??

Further tests show that setting the font 'name' at run-time does not circumvent the error, a complete assignment of an existing font is needed - thus :
Code: Pascal  [Select][+][-]
  1.   Character.Font.Name := 'Segoe UI';
  2.  
  3.   FontInUse.Name:= 'Segoe UI';
  4.  
  5.   FontInUse := Character.Font;

Line 3 allows compilation but causes AV error when run,   Line 5 'works'.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1209
Re: Finding List of Fonts & Printers
« Reply #18 on: August 08, 2026, 07:05:30 pm »
Not sure J-G if you know but

XEL controls and KControls both on OPM has

XelFontManager , XelFontPreview  and  KPrintSetupDialog

Thanks for the 'heads up'  -  No, I was not aware that these controls were available, and consequently haven't looked at what they offer.

My interest is more to do with improving my understanding of how Lazarus/Pascal works so using pre-designed controls is not a priority.

Additionally, my 'fumbling in the dark' - and reporting failures - may well enlighten others who are also struggeling to get their heads around concepts. Well, that is a hope at least :)

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1209
Re: Finding List of Fonts & Printers
« Reply #19 on: August 08, 2026, 08:29:01 pm »
Yes Mike - it all makes sense  BUT  -  No, the AV was triggered in the first line of procedure Populate_FontList;  where the assignment of the String 'F' is made from FontInUse.Name.

I did eventually realize that it must be an issue to do with the TFont not being created (but hadn't appreciated that it must be 'Nil').  In fact I'm now thinking that by using FontInUse := Character.Font; I'm effectively 'Creating' FontInUse  but cannot now fathom whether or not it should be 'Free'd  :-[

Yes the best place would be OnFormClose and I can easily do that - - -  but do I need to ?  or will the fact that I haven't done a specific 'Create' mean that FontInUse.Free will cause an error?
« Last Edit: August 09, 2026, 01:27:41 am by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Fred vS

  • Hero Member
  • *****
  • Posts: 3958
    • StrumPract is the musicians best friend
Re: Finding List of Fonts & Printers
« Reply #20 on: August 09, 2026, 01:11:35 am »
Hello.

Here what I use for Windows and Unix to have a list of fonts installed by language:

Code: Pascal  [Select][+][-]
  1. uses
  2.  {$ifdef windows}windows,{$endif}process, ...
  3.  
  4. ...
  5.  
  6. {$ifdef windows}
  7. function EnumFontsNoDups(
  8.   var LogFont: TEnumLogFontEx;
  9.   var Metric: TNewTextMetricEx;
  10.   FontType: Longint;
  11.   Data: LParam):LongInt; stdcall;
  12. var
  13.   L: TStringList;
  14.   S: String;
  15. begin
  16.   L := TStringList(ptrint(Data));
  17.   S := LogFont.elfLogFont.lfFaceName;
  18.   if L.IndexOf(S)<0 then
  19.     L.Add(S);
  20.   result := 1;
  21. end;
  22.  
  23. procedure listlangfont(lang : String);
  24. var
  25.   DC: HDC;
  26.   lf: TLogFont;
  27.   L: TStringList;
  28.   x: Integer;
  29. begin
  30.  
  31.   {
  32.   DEFAULT_PITCH  = 0;
  33.   FIXED_PITCH    = 1;
  34.   VARIABLE_PITCH = 2;
  35.   MONO_FONT      = 8;
  36.  
  37.   // font character sets
  38.   ANSI_CHARSET        = 0;
  39.   DEFAULT_CHARSET     = 1;
  40.   SYMBOL_CHARSET      = 2;
  41.   // added for ISO_8859_2 under gtk
  42.   FCS_ISO_10646_1     = 4;  // Unicode;
  43.   FCS_ISO_8859_1      = 5;  //  ISO Latin-1 (Western Europe);
  44.   FCS_ISO_8859_2      = 6;  //  ISO Latin-2 (Eastern Europe);
  45.   FCS_ISO_8859_3      = 7;  //  ISO Latin-3 (Southern Europe);
  46.   FCS_ISO_8859_4      = 8;  //  ISO Latin-4 (Northern Europe);
  47.   FCS_ISO_8859_5      = 9;  //  ISO Cyrillic;
  48.   FCS_ISO_8859_6      = 10; //  ISO Arabic;
  49.   FCS_ISO_8859_7      = 11; //  ISO Greek;
  50.   FCS_ISO_8859_8      = 12; //  ISO Hebrew;
  51.   FCS_ISO_8859_9      = 13; //  ISO Latin-5 (Turkish);
  52.   FCS_ISO_8859_10     = 14; //  ISO Latin-6 (Nordic);
  53.   FCS_ISO_8859_15     = 15; //  ISO Latin-9, or Latin-0 (Revised Western-European);
  54.   //FCS_koi8_r          = 16; //  KOI8 Russian;
  55.   //FCS_koi8_u          = 17; //  KOI8 Ukrainian (see RFC 2319);
  56.   //FCS_koi8_ru         = 18; //  KOI8 Russian/Ukrainian
  57.   //FCS_koi8_uni        = 19; //  KOI8 ``Unified'' (Russian, Ukrainian, and Byelorussian);
  58.   //FCS_koi8_e          = 20; //  KOI8 ``European,'' ISO-IR-111, or ECMA-Cyrillic;
  59.   // end of our own additions
  60.   MAC_CHARSET         = 77;
  61.   SHIFTJIS_CHARSET    = 128;
  62.   HANGEUL_CHARSET     = 129;
  63.   JOHAB_CHARSET       = 130;
  64.   GB2312_CHARSET      = 134;
  65.   CHINESEBIG5_CHARSET = 136;
  66.   GREEK_CHARSET       = 161;
  67.   TURKISH_CHARSET     = 162;
  68.   VIETNAMESE_CHARSET  = 163;
  69.   HEBREW_CHARSET      = 177;
  70.   ARABIC_CHARSET      = 178;
  71.   BALTIC_CHARSET      = 186;
  72.   RUSSIAN_CHARSET     = 204;
  73.   THAI_CHARSET        = 222;
  74.   EASTEUROPE_CHARSET  = 238;
  75.   OEM_CHARSET         = 255;
  76.   }
  77.  
  78.   lf.lfPitchAndFamily := 0;
  79.  
  80.   if lang = 'ru' then lf.lfCharSet := 204 else
  81.   if lang = 'ar' then lf.lfCharSet := 178 else
  82.   if lang = 'he' then lf.lfCharSet := 177 else
  83.   if lang = 'el' then lf.lfCharSet := 161 else
  84.   if lang = 'zh' then lf.lfCharSet := 136 else
  85.   lf.lfCharSet := 1;  
  86.  
  87.   lf.lfFaceName := '';
  88.  
  89.   L := TStringList.create;
  90.   L.Sorted := True;
  91.   L.Duplicates := dupIgnore;
  92.  
  93.   x := 0;
  94.  
  95.   DC := GetDC(0);
  96.   try
  97.     EnumFontFamiliesEX(DC, @lf, @EnumFontsNoDups, ptrint(L), 0);
  98.      L.Sort;
  99.    
  100.           gridlistfont.rowcount := 0;
  101.        
  102.       while x < L.count do
  103.         begin
  104.           gridlistfont.rowcount :=  gridlistfont.rowcount + 1;
  105.           gridlistfont[0][gridlistfont.rowcount - 1] := L[x];
  106.           inc(x);
  107.         end;
  108.    
  109.    finally
  110.     ReleaseDC(0, DC);
  111.     L.Free;
  112.   end;
  113. end;
  114. {$endif}
  115.  
  116.  
  117. {$ifdef unix}
  118. procedure listlangfont(lang : String);
  119. var
  120.   x, y: integer;
  121.   s, S2 : string;
  122.   comstr : ansistring;
  123.   sl: TStringList;
  124.  begin
  125.  
  126.   comstr := '';
  127.  
  128.   if fileexists('/usr/bin/fc-list') then
  129.     comstr := '/usr/bin/fc-list'
  130.   else
  131.     if fileexists('/usr/local/bin/fc-list') then
  132.       comstr := '/usr/local/bin/fc-list';
  133.  
  134.   if comstr <> '' then
  135.     begin
  136.  
  137.       sl := TStringList.create;
  138.       sl.Sorted := True;
  139.       sl.Duplicates := dupIgnore;
  140.  
  141.       x := 0;
  142.  
  143.       RunCommand(comstr, [':lang='+ lang, '--format=%{family[0]}\n'], s);
  144.  
  145.       while (system.pos(lineend,s) > 0) do
  146.         begin
  147.           y := system.pos(lineend,s);
  148.           s2 := system.Copy(s, 1, y - 1);
  149.           //writeln(s2);
  150.           sl.Add(s2);
  151.           s := system.Copy(s, (y + 1) , length(s));
  152.           inc(x);
  153.         end;
  154.  
  155.       //writeln('String lines ' + inttostr(x));
  156.       //writeln('TStringList ' + inttostr(sl.count));
  157.  
  158.        x :=  0;
  159.  
  160.       while x < sl.count do
  161.         begin
  162.           gridlistfont.rowcount :=  x + 1;
  163.           gridlistfont[0][x] := string(sl[x]);
  164.           inc(x);
  165.         end;
  166.        
  167.       sl.free;
  168.      
  169.       end;
  170.        
  171.     end;
  172. {$endif}
« Last Edit: August 09, 2026, 01:35:25 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

J-G

  • Hero Member
  • *****
  • Posts: 1209
Re: Finding List of Fonts & Printers
« Reply #21 on: August 09, 2026, 01:22:49 pm »
With the emphasis settling on getting a useable list of FONTS, the matter of Printer Names / Setting has been neglected but this morning I looked properly at this aspect of my original question.

If you've been reading from the start then you may have seen my museing at the end of Post #5 that it might not be necessary to assign a list of printers to a TComboBox.  Well today I have confirned that that is the case.  Simply adding the TPrinterSetupDialogue does the job - the available printers are simply present without any need for 'assign' to a List.

This revalation led me to re-consider the FONT aspect again. I'm sure that very early on (probably before even raising the question!) I tried to 'Execute' the FontChangeDialogue - without success.  Buoyed up by the PrinterDialogue success I have looked again at the FontChangeDialogue and can confirm that it does work in exactly the same way that the PrinterSettupDialogue does (no doubt many {all?} of you were well aware of this  ::) ) - providing a means of selecting all aspects of the Font (Name, Size, Style) thus eliminating the need for the TComboBox entirely.

I've yet to follow up with transfering the 'selection' to the other aspects of the program but that should just be a matter of using 'ItemIndex' or similar.

The excercise has not been in vain though -  I've learned a great deal about 'creating' Objects and the pitfalls that can befall one who lacks knowledge 🤣
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1209
Re: Finding List of Fonts & Printers
« Reply #22 on: August 09, 2026, 08:23:14 pm »
I've yet to follow up with transfering the 'selection' to the other aspects of the program but that should just be a matter of using 'ItemIndex' or similar.

I have now 'followed up' and eliminated the TStringList and the TComboBox - though I have retained the code in my 'General Utilities' Unit on the off-chance that I may need a separate list at some time in a future Project.

Transfering the values was a little more complex that I envisaged. On using the FontChange.Execute I found that if I did nothing to the Font Name  ie. simply clicked on [OK]  -  the font name returned was 'default'   The name highlighted was correctly shown to be the pre-set 'FontInUse.Name'.  Selecting any other font from the list correctly returned the appropriate name.  The 'Size' was returned according to the displayed setting as was the the 'Style' - even though they were not what had been set for the FontInUse.

To circumvent this I created a var - 'OldFont' - which I made equal to FontInUse at the start of the call to the FontChangeDialogue. I also set the Size and Style of the FontChangeDialogue from 'Old_Font'.  This made the display in the dialogue correct and any changes made to size or style were returned correctly. The Name however was still 'default'.  A further test had to be made to correct the name : 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Fnt_ChangeClick(Sender: TObject);
  2. begin
  3.   Old_Font              := FontinUse;
  4.   FontChange.Font.Size  := Old_Font.Size;
  5.   FontChange.Font.Style := Old_Font.Style;
  6.  
  7.   FontChange.Execute;
  8.  
  9.   FontInUse := FontChange.Font;
  10.  
  11.   if FontInUse.Name = 'default' then
  12.     FontInUse.Name := Old_Font.Name;
  13.  
  14.   FontName.Font    := FontInUse;  // these are TLabels on the Form
  15.   Character.Font   := FontInUse;    // used to show the name and Char
  16.                                                       // in the selected font
  17.   FontName.Caption := FontInUse.Name;
  18.   FontSize.Caption := IntToStr(FontInUse.Size);
  19. end;
  20.  

It seems (to me) that I may have discovered a bug in the TFontDialogue. Surely the 'name' returned ought to be whatever has been displayed - whether or not a change has been made - particularly since the 'name' is known at point of entry  - - -  and since the name is known (and set), why are the attributes not set accordingly?

It is very possible that this has been found and corrected in later releases - remember I'm still using Laz 2.2.0  -  If so, all well and good, but I thought it worth bringing the posibility to the attention of anyone with the knowledge and ability to effect a correction.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

jamie

  • Hero Member
  • *****
  • Posts: 7892
Re: Finding List of Fonts & Printers
« Reply #23 on: August 09, 2026, 10:00:44 pm »
you are using the wrong fields for your purpose.

TLABEL descends from a TCOMPONENT, which has the name of the component not a field display.


 All components have the property "NAME" if they derive from TCOMPONENT.

 If you are making your own classes, then don't create any fields using "NAME" if they are derived from TCOMPONENT.

 I suppose since the controls are managed via the TCOMPONENT inheritance it would seem logical to do this however, you could end with two or more
TLABELS with the same name and doing a search via the TCOMPONENT method, it will only find the first one, you need to be able to step over to the next.

It would most likely be easier on the eyes for the rest of us if you used something other than NAME.


Jamie




« Last Edit: August 09, 2026, 10:03:07 pm by jamie »
The only true wisdom is knowing you know nothing

J-G

  • Hero Member
  • *****
  • Posts: 1209
Re: Finding List of Fonts & Printers
« Reply #24 on: August 09, 2026, 11:05:31 pm »
Thanks for the input @Jamie  but I'm totally confused.

I've not making my own class (never have) so I've only used .Name where it is a property of an existing component. 

Unless you are saying that 'FontName' is inappropriate ?    That (to me) is the most logical title for the TLable which displays the Name of the Font in use.

The only other component that I could use would be TStaticText but I cannot see why that might be any different, nor do I understand why (or how) I might want to do a search?  I'm not 'looking' for anything, I'm merely 'selecting' from a list of names presented to me in the FontChangeDialogue.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018