Finding a TFont by name [SOLVED] lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- carl_caulkett · 2017-07-13 12:50 Hello, is there a way of finding a TFont by supplying a font name, preferably one which doesn't have the overhead of lots of graphics related updates. My aim is to take the list of fonts available from Screen.Fonts; and filter it by characteristic, particularly whether it is Monospaced or not. ---------------------------------------------------------------------- Thaddy · 2017-07-13 15:51 One example would be to assign them to a generic array and write an enumerator/filter for TFont.Pitch. (fpFixed = monospace) ---------------------------------------------------------------------- carl_caulkett · 2017-07-13 16:13 Thanks. I'll investigate that next. I was experimenting with this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TcaFontSelectorForm.DoShow;var  FontName: string;  AFont: TFont;begin  inherited DoShow;  FontList.Items.BeginUpdate;  try    FFontNames.Assign(Screen.Fonts);//    for FontName in FFontNames do  FontName := 'Courier New';      begin        AFont := TFont.Create;      try          AFont.Name := FontName;          if FontIsMonoSpace(HFONT(AFont.Reference.Handle)) then            FontList.Items.Add(FontName);        finally          AFont.Free;      end;//        if FontIsMonoSpace(CreateFont(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, PChar(FontName))) then      end;  finally    FontList.Items.EndUpdate;  end;end;  But ultimately I ran into a hard coded non-implementation in intfbaselcl.inc which simply reads: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TWidgetSet.FontIsMonoSpace(Font: HFont): boolean;begin  Result:=false;end;  Hopefully, I'll have better luck with the Pitch property like Thaddy suggested. Back in a few minutes! ---------------------------------------------------------------------- carl_caulkett · 2017-07-13 18:40 The problem seems to be that the Screen.Fonts property only contains the list of names - it doesn't have the actual font objects in the Objects[] property. This is one of the few occasions over the last couple of months where I've thought to myself that this would be easier under Windows! ---------------------------------------------------------------------- carl_caulkett · 2017-07-13 20:09 I found a promising looking project in the Lazarus examples called FontEnumeration, but when I ran it, it showed all 567 fonts on my machine but wasn't able to filter them when I selected FIXED_PITCH or MONO_PITCH from the Pitch combo and pressed "apply filter". I've just tried setting the font of a displayed control to 'Courier New' and tried reading the Pitch and IsMonoSpace properties, and neither are set correctly. I'm forced to conclude that these properties are not supported with the current default widget set on the Apple Mac. That, in turn, has led me to discover another Lazarus feature, namely the Padlock icon on certain properties in the Ctrl- Space dropdown and the Restricted tab in the Object Inspector! This probably explains why Lazarus does not attempt to limit the choice of editor fonts in the Options -> Editor -> Display dialog. I think I'm going to be forced to keep a list of all known Monospaced fonts and filter on the basis of that. Just the thought of doing that makes me feel dirty, somehow! ---------------------------------------------------------------------- carl_caulkett · 2017-07-13 21:32 I found a rather neat, yet pragmatic solution to the problem on StackOverflow. A fixed width font could be defined as one where the TextWidth of the characters is the same regardless of the character. Luckily, the Canvas.TextWidth property is available on the Apple Mac. The code now looks like this... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TcaFontSelectorForm.DoShow;var  FontName: string;begin  inherited DoShow;  FontList.Items.BeginUpdate;  Cursor := crHourGlass;  Application.ProcessMessages;  try    FFontNames.Assign(Screen.Fonts);    for FontName in FFontNames do      begin        FontList.Canvas.Font.Name := FontName;        FontList.Canvas.Font.Size := 10;        if FontList.Canvas.TextWidth('m') = FontList.Canvas.TextWidth('i') then        FontList.Items.Add(FontName);      end;  finally    FontList.Items.EndUpdate;    Cursor := crDefault;  end;end;  The processing of the fonts does give rise to a slight delay, which I have tried to handle with the showing of the cursor. However, the cursor doesn't appear immediately despite the Application.ProcessMessages; call. Can anyone tell me why that might be? Thanks, Carl ---------------------------------------------------------------------- howardpc · 2017-07-13 21:56 Perhaps because of an unneccesary Assign. Why would Application.ProcessMessages be needed? Try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TcaFontSelectorForm.DoShow;var  FontName: string;begin  inherited DoShow;  Cursor := crHourGlass;  FontList.Items.BeginUpdate;  try    for FontName in Screen.Fonts do    begin      FontList.Canvas.Font.Name:=FontName;      FontList.Canvas.Font.Size:=10;      if (FontList.Canvas.TextWidth('m') = FontList.Canvas.TextWidth('i')) then        FontList.Items.Add(FontName);    end;  finally    FontList.Items.EndUpdate;    Cursor := crDefault;  end;end; ---------------------------------------------------------------------- carl_caulkett · 2017-07-13 22:45 You're right! Screen.Fonts is already populated after all. Why would I need to populate a second list? Well spotted, and thanks - it's a whole lot faster now. ---------------------------------------------------------------------- wp · 2017-07-13 23:09 As an alternative you may want to look at how TScreen collects the font names (needs Lclintf): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetScreenFontsList(FontList: TStrings);var  lf: TLogFont;  DC: HDC;begin  lf.lfCharSet := DEFAULT_CHARSET;  lf.lfFaceName := '';  lf.lfPitchAndFamily := 0;  DC := GetDC(0);  try    EnumFontFamiliesEx(DC, @lf, @EnumFontsNoDups, PtrInt(FontList), 0);  finally    ReleaseDC(0, DC);  end;end;EnumFontFamiliesEx needs a callback function, here EnumFontsNoDups: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function EnumFontsNoDups(var LogFont: TEnumLogFontEx; var Metric: TNewTextMetricEx;  FontType: Longint; Data: LParam): LongInt; extdecl;var  L: TStrings;  S: String;begin  L := TStrings(PtrInt(Data));  S := LogFont.elfLogFont.lfFaceName;  if L.IndexOf(S) < 0 then    L.Add(S);  Result := 1;end;Knowing that the record TLogFont has a field lfPitchAndFamily you certainly can extend this to collect only the fixed-pitch fonts. It is a good exercise navigating through the Lazarus code. You may also want to look at the project fontenumeration in the folger examples/fontenum of the lazarus installation. It makes uses of this technique. ---------------------------------------------------------------------- carl_caulkett · 2017-07-13 23:23 I tried out the fontenumeration project earlier. Changing the Pitch select combos didn't make any difference to the filtering of fonts. Whatever I selected, all 567 fonts on my computer (Apple Mac with Carbon widget set) were displayed. ---------------------------------------------------------------------- wp · 2017-07-14 07:37 You are giving up too early. The fact that the demo is not working like you want does not mean that the idea is wrong, the demo contains all the information you need to solve this - if you would look at the code and would try to understand it. ---------------------------------------------------------------------- carl_caulkett · 2017-07-14 08:25 Quote from: carl_caulkett on July 14, 2017, 01:23:01 amI tried out the fontenumeration project earlier. Changing the Pitch select combos didn't make any difference to the filtering of fonts. Whatever I selected, all 567 fonts on my computer (Apple Mac with Carbon widget set) were displayed. That message was written at 01:23 am. I was *not* giving up too early. In fact, I was already making mistakes due to tiredness. So, I went to bed. Now, in the morning, having reviewed the TScreen code, I feel confident of being up to the task of understanding it. ---------------------------------------------------------------------- Thaddy · 2017-07-14 08:28 ---------------------------------------------------------------------- wp · 2017-07-14 08:35 Quote from: carl_caulkett on July 14, 2017, 10:25:40 amI was *not* giving up too early.You know better, of course, I just had the impression. Excuse my harsh words, but sometimes I get the feeling that people want an finished "cookbook" solution here instead of using their own brain. ---------------------------------------------------------------------- carl_caulkett · 2017-07-14 10:45 I tried your FilterFonts project. I had to change the line lf.lfCharSet := ANSI_CHARSET; to lf.lfCharSet := DEFAULT_CHARSET; otherwise the function TCarbonWidgetSet.EnumFontFamiliesEx(DC: HDC; lpLogFont: PLogFont; Callback: FontEnumExProc; LParam: Lparam; flags: dword): Longint; just bombed out with the message TCarbonWidgetSet.EnumFontFamiliesEx with specific face or char set is not implemented!. Once I got that working, I was only able to see fonts listed in the listbox when no filters were selected. This, I think, is because the LogFont.elfLogFont.lfPitchAndFamily is not being populated properly. I tweaked the callback function to check: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function EnumFontsPitch(  var LogFont: TEnumLogFontEx;  var Metric: TNewTextMetricEx;  FontType: Longint;  Data: LParam):LongInt; stdcall;var  L: TStringList;  S: String;begin  L := TStringList(ptrint(Data));  S := LogFont.elfLogFont.lfFaceName;  if S = 'Courier New' then    begin      if (L.IndexOf(S) < 0) and (LogFont.elfLogFont.lfPitchAndFamily AND FILTER = FILTER) then // BREAKPOINT SET HERE        L.Add(S);      result := 1;    end;end;  LogFont.elfLogFont.lfPitchAndFamily has a byte value of \0 according to LLDB which, if FILTER is anything other than 0, means that L.Add(S) is not called. Unless I'm missing something, it does look like Pitch is not supported by the Carbon widget set on the Apple Mac. ---------------------------------------------------------------------- wp · 2017-07-14 14:12 Yes, it looks like you are right. Of course you still can fall back to the code in reply #6.