Changing the font on a canvas lazarus / 31-LCL ---------------------------------------------------------------------- cov · 2025-06-24 18:51 I'm writing a small CAD application which loads a DXF drawing file. If the file contains text, it may simply describe the font used as Arial. I am having a tinker with Graeme Geldenhuis' fpTTF module which claims to cache the installed fonts on the machine (I am using Mate on Manjaro). As I understand it, I initiate the font cache: 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   gTTFontCache.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts/');  gTTFontCache.BuildFontCache;   Then I use the name of the font to find it: 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 gTTFontCache.Find('Arial'); However, I am unsure of what happens next. How do I assign the font to the canvas? TIA. ---------------------------------------------------------------------- wp · 2025-06-25 10:06 Which kind of Canvas? A TCanvas as used by the LCL, or a TFPCustomCanvas descendant as used by fcl-image? ---------------------------------------------------------------------- cov · 2025-06-25 12:24 Thanks for the response. Tbitmap.canvas. 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       img:= TBitmap.Create;      Img.SetSize(500,500);      Img.Canvas.Brush.Style := bsSolid;      Img.Canvas.Brush.Color := clRed;      img.Canvas.TextOut(0,0,CharacterList); ---------------------------------------------------------------------- Handoko · 2025-06-25 14:07 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Graphics, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);const  LinesPerColumn = 20;var  Names: array of string;  X, Y:  Integer;  i:     Integer;  S:     string;begin  SetLength(Names, Screen.Fonts.Count);   // Load all font names  for i := 0 to Screen.Fonts.Count-1 do    Names[i] := Screen.Fonts[i];   // Show it  for i := 0 to Length(Names)-1 do  begin    S := (i+1).ToString + ' ' +  Names[i];    X := (i div LinesPerColumn) * 200;    Y := (i mod LinesPerColumn) * 20;    Canvas.Brush.Color := clForm;    Canvas.Font.Name := Names[i];    Canvas.Font.Size := 12;    Canvas.TextOut(X+50, Y+50, S);  end;  Canvas.Font.Name := 'default';  Canvas.TextOut(120, 15, 'Total installed fonts = ' + Length(Names).toString);  end; end. ---------------------------------------------------------------------- etrusco · 2025-06-25 14:13 What are you trying to achieve? AFAICS gTTFontCache is only used by fp-pdf. Are you following Google/Gemini (mis)directions? To list the fonts installed on your system, use Screen.Fonts. To set the Font face, set Font.Name (or assign from another Font). ---------------------------------------------------------------------- wp · 2025-06-25 20:09 To my knowledge, 'Arial' is a Windows font, and probably does not exist on your Linux system. You could use the following function FindFontOrReplacement to find the first existing font from a list provided to it: 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 FindFontOrReplacement(SimilarFonts: String): String;var  fonts: TStringArray;  i: Integer;begin  fonts := SimilarFonts.Split([',', ';', #13, #10, #9]);  // Allowed separators in the font list  for i := 0 to High(fonts) do    if Screen.Fonts.IndexOf(trim(fonts[i])) > 0 then    begin      Result := fonts[i];      exit;    end;  Result := '';end; procedure TForm1.Button1Click(Sender: TObject);const  CharacterList: String = 'abcdefghijklmnopqrstuvwxyz';var  img: TBitmap;begin  img:= TBitmap.Create;  img.SetSize(300,100);  img.Canvas.Brush.Style := bsSolid;  img.Canvas.Brush.Color := clRed;  img.Canvas.Font.Name := FindFontOrReplacement('Arial;Liberation Sans;Noto Sans');  img.Canvas.TextOut(0,0,CharacterList);  Image1.Picture.Assign(img);  Image1.Width := img.Width;  Image1.Height := img.Height;  img.Free;end; In this example, the img.Canvas.Font is either 'Arial' or 'Liberation Sans' or 'Noto Sans' depending on which one of them is found first among the Screen.Fonts list. ---------------------------------------------------------------------- cov · 2025-06-26 09:10 Thanks. Yes, Arial needs to be replaced with the closest font on the user's system. Your code looks great. I'll give it a try.