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;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~