type TSFontWare = class public function Create(pdf: PdfFile); function GetAsciiFont(name: string; bold: boolean; italic: boolean): PdfFont; function GetCNSFont(name: string; bold: boolean; italic: boolean): PdfFont; function GetSymbolFont(): PdfFont; function UseExternalFont(); function SetSubstitutionRules(source: string; target: string); function SetDefaultSz(value: real); function GetDefaultSz(): real; private function GetExternalFont(name:string; bold: boolean; italic: boolean): PdfFont; function GetBuiltInCNSFont(name:string; bold: boolean; italic: boolean): PdfFont; function GetBuiltInAsciiFont(name:string; bold: boolean; italic: boolean): PdfFont; private [weakref]pdf_: PdfFile; use_built_in_font_: boolean; // 是否使用内置字体 substitution_rules_: array of string; // 替换规则 external_reference_: array of string; default_sz_: real; end; function TSFontWare.Create(pdf: PdfFile); begin pdf_ := pdf; use_built_in_font_ := true; substitution_rules_ := array("宋体": "SimSun", "黑体": "SimHei", "Courier New": "Courier", "Helvetica": "Helvetica", "Times New Roman": "Times-Roman", ); external_reference_ := array(); default_sz_ := 10.5; end; function TSFontWare.SetDefaultSz(value: real); begin default_sz_ := value; end; function TSFontWare.GetDefaultSz(); begin return default_sz_; end; function TSFontWare.UseExternalFont(); begin use_built_in_font_ := false; end; function TSFontWare.GetExternalFont(name:string; bold: boolean; italic: boolean); begin if ifnil(name) or name = '' then name := "等线"; if not ifnil(external_font_cache_[name]) then return external_font_cache_[name]; value := external_reference_[name]; if ifnil(value) then return nil; if value["ext"] = ".ttf" then font_name := pdf_.LoadTTFontFromFile("", value["path"], true); else if value["ext"] = ".ttc" then font_name := pdf_.LoadTTFontFromFile2("", value["path"], 0, true); if not ifString(font_name) then return nil; font := pdf_.GetFont(font_name, "UTF-8"); external_font_cache_[name] := font; return font; end; function TSFontWare.GetCNSFont(name: string; bold: boolean; italic: boolean): PdfFont; begin return use_built_in_font_ ? {self.}GetBuiltInCNSFont(name, bold, italic) : {self.}GetExternalFont(name, bold, italic); end; function TSFontWare.GetBuiltInCNSFont(name: string; bold: boolean; italic: boolean): PdfFont; begin font_name := substitution_rules_[name]; if ifnil(font_name) then font_name := "SimSun"; if bold and italic then font_name += ",BoldItalic"; else if bold then font_name += ",Bold"; else if italic then font_name += ",Italic"; return pdf_.GetFont(font_name, "GBK-EUC-H"); end; function TSFontWare.GetAsciiFont(name: string; bold: boolean; italic: boolean): PdfFont; begin return use_built_in_font_ ? {self.}GetBuiltInAsciiFont(name, bold, italic) : {self.}GetExternalFont(name, bold, italic); end; function TSFontWare.GetBuiltInAsciiFont(name:string; bold: boolean; italic: boolean): PdfFont; begin font_name := substitution_rules_[name]; if ifnil(font_name) then font_name := "Times-Roman"; if font_name = "Courier" or font_name = "Helvetica" then begin if bold and italic then font_name += "-BoldOblique"; else if bold then font_name += "-Bold"; else if italic then font_name += "-Oblique"; end else if font_name = "Times-Roman" then begin if bold and italic then font_name := "Times-BoldItalic"; else if bold then font_name += "Times-Bold"; else if italic then font_name += "Times-Italic"; end return pdf_.GetFont(font_name, ""); end; function TSFontWare.SetSubstitutionRules(source: string; target: string); begin substitution_rules_[source] := target; end; function TSFontWare.GetSymbolFont(): PdfFont; begin return pdf_.GetFont("ZapfDingbats", ""); end;