秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  278 随笔 :: 0 文章 :: 308 评论 :: 20万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

lazarus使用报表时遇到某些电脑缺少字体,造成打印出来的效果有差异,为避免这个问题,可以手工安装字体,也可以用程序拷贝字体到指定的文件夹(/usr/share/fonts或~/.local/share/fonts),如果拷贝到~/.local/share/fonts程序不需要root权限,以下代码将应用程序font目录的所有ttf字体文件拷贝到~/.local/share/fonts
这个没特别的,只是将font目录的ttf文件拷贝到指定目录。

直接上代码:

复制代码
uses FileUtil,LazFileUtils;//要添加这2个单元
procedure TForm1.CopyDirFile(const SourceDirName,DestDir: string);
var
  i, j: integer;
  FilesFoundToCopy : TStringList;
  SourceDirectoryAndFileName, SubDirStructure, FinalisedDestDir, FinalisedFileName : string;
  SourceDir:string;
begin
  SourceDir:= SourceDirName;
  SubDirStructure := '';
  FinalisedDestDir := '';

  SetCurrentDir(SourceDirName);
  FilesFoundToCopy := FindAllFiles(SourceDirName, '*', True);

  try
    for i := 0 to FilesFoundToCopy.Count -1 do
    begin
      SourceDirectoryAndFileName := ChompPathDelim(CleanAndExpandDirectory(FilesFoundToCopy.Strings[i]));

      SubDirStructure := IncludeTrailingPathDelimiter(ExtractFileDir(SourceDirectoryAndFileName));
      if SourceDir+'/'=SubDirStructure then SubDirStructure:='';
      j:= pos(SourceDir,SubDirStructure)+length(SourceDir);
      if pos(SourceDir,SubDirStructure)>0 then
         SubDirStructure:=Copy(SubDirStructure, j,length(SubDirStructure));

      FinalisedDestDir :=DestDir+SubDirStructure;
      FinalisedFileName := ExtractFileName(FilesFoundToCopy.Strings[i]);

      if not DirPathExists(FinalisedDestDir) then
      begin
        ForceDirectories(FinalisedDestDir);
      end;

      if (pos('.ttf',SourceDirectoryAndFileName)>0) then//只拷贝ttf字体文件
        FileUtil.CopyFile(SourceDirectoryAndFileName, FinalisedDestDir+FinalisedFileName, true);
    end;
  finally
    FilesFoundToCopy.free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);

begin
  //只拷贝程序font文件夹的所有ttf字体文件
  CopyDirFile(ExtractFilePath(ParamStr(0))+'font/',ConcatPaths([GetUserDir, '.local', 'share', 'fonts'])+'/');
end;
      
复制代码

 

posted on   秋·风  阅读(341)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示