Recent

Author Topic: Help Needed: Cross Platform Third Party Fonts  (Read 1207 times)

Bogen85

  • Hero Member
  • *****
  • Posts: 572
Re: Help Needed: Cross Platform Third Party Fonts
« Reply #15 on: February 03, 2023, 05:02:04 pm »
Exactly, I  have say

home/username/myprogram/myfont.ttf

and I want to copy it to

home/username/.fonts/myfont.ttf at runtime, so it's available, only once, if exists don't copy it again... and create the folder if don't exists, all by code

Something like this? (Untested on my part... this is just a possible example of how you could do it...)


Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils, FileUtil;
  3.  
  4. function SearchPath(const exe: string): string;
  5.   begin
  6.     result := ExeSearch(exe, GetEnvironmentVariable('PATH'));
  7.   end;
  8.  
  9. function CopyFileToHomeDir(const sourceFile, destFolder: string): Boolean;
  10. var
  11.   ExeName, ExePath, sourcePath, destPath: string;
  12. begin
  13.   // Get directory current application is found in
  14.   ExeName := ParamStr(0);
  15.   //ExePath := ExtractFilePath(ExeName);
  16.   ExePath := SearchPath(ExeName);
  17.  
  18.   // Check if source file exists
  19.   sourcePath := ExePath + PathDelim + sourceFile;
  20.   if not FileExists(sourcePath) then
  21.   begin
  22.     Result := False;
  23.     Exit;
  24.   end;
  25.  
  26.   // Check if destination folder exists
  27.   destPath := GetUserDir + PathDelim + destFolder;
  28.   if not DirectoryExists(destPath) then
  29.     CreateDir(destPath);
  30.  
  31.   // Check if file exists in destination folder
  32.   destPath := destPath + PathDelim + sourceFile;
  33.   if FileExists(destPath) then
  34.   begin
  35.     Result := True;
  36.     Exit;
  37.   end;
  38.  
  39.   // Copy file to destination folder
  40.   Result := CopyFile(sourcePath, destPath);
  41. end;

lainz

  • Hero Member
  • *****
  • Posts: 4127
Re: Help Needed: Cross Platform Third Party Fonts
« Reply #16 on: February 03, 2023, 09:51:39 pm »
Thanks it works, it copies the fonts!

Code: Pascal  [Select][+][-]
  1.   {$IFDEF LINUX}
  2.   CopyFileToHomeDir('VictorMono-Bold.otf', '.fonts');
  3.   CopyFileToHomeDir('VictorMono-Regular.otf', '.fonts');
  4.   {$ENDIF}

Thanks =)

Now is missing macOS, and all 3 major OS are covered.

Bogen85

  • Hero Member
  • *****
  • Posts: 572
Re: Help Needed: Cross Platform Third Party Fonts
« Reply #17 on: February 03, 2023, 11:12:55 pm »
Thanks it works, it copies the fonts!

Code: Pascal  [Select][+][-]
  1.   {$IFDEF LINUX}
  2.   CopyFileToHomeDir('VictorMono-Bold.otf', '.fonts');
  3.   CopyFileToHomeDir('VictorMono-Regular.otf', '.fonts');
  4.   {$ENDIF}

Thanks =)

Good! However, I just asked Open AI...
Quote
I need a function for free pascal.
It receives 2 strings. 1) a file 2) a folder

For the file, it chesks for it's existence in the folder the application is being run from. This file with the application directory path is prepended to the file as the source file.

For the folder, it checks for it's existence in the user's home directory (home directory must be prepended).
If the folder does exist, it creates it.
This folder in the user's home directory is the destination folder.

If the source file exists, the the existence of the same file is checked or in the destination folder. If it does not exist in the destionation folder it is copied there.

It did not calculate a source path correctly so I added SearchPath on my own, and the call to it.

Apart from that, the rest is verbatim from OpenAI's reply...

Hmmm... I see it auto corrected a typo of mine (if the directory exists, create it... it knew to add not exists...)
« Last Edit: February 03, 2023, 11:16:33 pm by Bogen85 »

 

TinyPortal © 2005-2018