137 lines
4.5 KiB
Plaintext
137 lines
4.5 KiB
Plaintext
type TSFontWare = class
|
|
public
|
|
function Create(pdf: PdfFile);
|
|
function GetFontByText(text: string; name: string; bold: boolean; italic: boolean);
|
|
function GetAsciiFont(name: string; bold: boolean; italic: boolean);
|
|
function GetCNSFont(name: string; bold: boolean; italic: boolean);
|
|
function UseExternalFont();
|
|
function SetSubstitutionRules(source: string; target: string);
|
|
function SetDefaultSz(value: real);
|
|
function GetDefaultSz();
|
|
|
|
private
|
|
function GetExternalFont(name:string; bold: boolean; italic: boolean);
|
|
function GetBuiltInCNSFont(name:string; bold: boolean; italic: boolean);
|
|
function GetBuiltInAsciiFont(name:string; bold: boolean; italic: boolean);
|
|
|
|
private
|
|
[weakref]pdf_: PdfFile;
|
|
|
|
is_linux_: boolean; // 是否是linux
|
|
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 ifnil(value) then raise name + " is unsupported font.";
|
|
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 raise "Load font error : " + format("%x", font_name);
|
|
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.GetFontByText(text: string; name: string; bold: boolean; italic: boolean);
|
|
begin
|
|
len := length(text);
|
|
if len > 1 then
|
|
return {self.}GetCNSFont(name, bold, italic);
|
|
else
|
|
return {self.}GetAsciiFont(name, bold, italic);
|
|
end;
|
|
|
|
function TSFontWare.GetCNSFont(name: string; bold: boolean; italic: boolean);
|
|
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);
|
|
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";
|
|
font := pdf_.GetFont(font_name, "GBK-EUC-H");
|
|
return font;
|
|
end;
|
|
|
|
function TSFontWare.GetAsciiFont(name: string; bold: boolean; italic: boolean);
|
|
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);
|
|
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
|
|
font := pdf_.GetFont(font_name, "");
|
|
return font;
|
|
end;
|
|
|
|
function TSFontWare.SetSubstitutionRules(source: string; target: string);
|
|
begin
|
|
substitution_rules_[source] := target;
|
|
end;
|