重构代码,抽象成range,完成段落的重构

This commit is contained in:
csh
2024-06-21 10:28:47 +08:00
parent 61001c9a46
commit 007da049a3
15 changed files with 1074 additions and 601 deletions
+15 -15
View File
@@ -1,23 +1,23 @@
unit TSColorToolKit;
interface
function HexToRGB(hex);
function HexToRGB(hex);
implementation
function HexToRGB(hex);
begin
hex_string := ifnumber(hex) ? format("%x", hex) : hex;
if length(hex_string) = 7 then
begin
if hex_string[1] <> "#" then raise "Invalid hexadecimal parameter.";
hex_string := hex_string[1:];
end
if length(hex_string) <> 6 then raise "Invalid hexadecimal parameter";
r := eval(&"return 0x" + hex_string[1:2]);
g := eval(&"return 0x" + hex_string[3:4]);
b := eval(&"return 0x" + hex_string[5:6]);
return array(r, g, b);
end;
function HexToRGB(hex);
begin
hex_string := ifnumber(hex) ? format("%x", hex) : hex;
if length(hex_string) = 7 then
begin
if hex_string[1] <> "#" then raise "Invalid hexadecimal parameter.";
hex_string := hex_string[1:];
end
if length(hex_string) <> 6 then raise "Invalid hexadecimal parameter";
r := eval(&"return 0x" + hex_string[1:2]);
g := eval(&"return 0x" + hex_string[3:4]);
b := eval(&"return 0x" + hex_string[5:6]);
return array(r, g, b);
end;
end.
-100
View File
@@ -1,100 +0,0 @@
type TSFontWare = class
public
function Create(pdf);
function Init();
function UseBuiltInFont();
function SetSubstitutionRules(source, target);
function GetFont(name, bold, italic);
private
function GetExternalFont(name, bold, italic);
function GetBuiltInFont(name, bold, italic);
private
pdf_;
is_linux_; // 是否是linux
use_built_in_font_; // 是否使用内置字体
substitution_rules_; // 替换规则
external_reference_;
external_font_cache_;
end;
function TSFontWare.Create(pdf);
begin
pdf_ := pdf;
is_linux_ := true;
use_built_in_font_ := false;
substitution_rules_ := array("宋体": "SimSun", "黑体": "SimHei");
external_reference_ := array();
external_font_cache_ := array();
// self.Init();
end;
function TSFontWare.UseBuiltInFont();
begin
use_built_in_font_ := true;
end;
function TSFontWare.Init();
begin
{$IFDEF LINUX}
is_linux_ := true;
{$ELSE}
is_linux_ := false;
{$ENDIF}
separator := is_linux_ ? "/" : "\\";
path := extractFileDir(sysExecName()) + separator + "funcext" + separator + "WordToPdf" + separator + "fonts" + separator;
files := fileList("", path + "*.tt*");
for i:=0 to length(files)-1 do
begin
filename := files[i]["FileName"];
ext := extractFileExt(filename);
pos := pos(ext, filename);
name := is_linux_ ? filename[:pos-1] : ansiToUTF8(filename[:pos-1]);
external_reference_[name] := array("ext": ext, "path": path + filename);
end
end;
function TSFontWare.GetExternalFont(name, bold, italic);
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.GetFont(name, bold, italic);
begin
return use_built_in_font_ ? self.GetBuiltInFont(name, bold, italic) : self.GetExternalFont(name, bold, italic);
end;
function TSFontWare.GetBuiltInFont(name, bold, italic);
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.SetSubstitutionRules(source, target);
begin
substitution_rules_[source] := target;
end;
-108
View File
@@ -1,108 +0,0 @@
type TSParagraphWare = class
public
function Create(components, styles, paragraph);
function Do();
function GetWords();
property Paragraph read ReadParagraph;
function ReadParagraph();
private
function SetPPr(ppr);
function SetRPr(rpr, ppr);
function SetRPrByStyleId(rpr, style_id);
function SetPPrByStyleId(ppr, style_id);
private
docx_components_;
styles_;
paragraph_;
words_;
end;
function TSParagraphWare.Create(components, styles, paragraph);
begin
docx_components_ := components;
styles_ := styles;
paragraph_ := paragraph;
words_ := array();
end;
function TSParagraphWare.Do();
begin
self.SetPPr(paragraph_.PPr); // styleid与ppr样式合并
rs := paragraph_.Rs();
for i:=0 to length(rs)-1 do
begin
r := rs[i];
self.SetRPr(r.RPr, paragraph_.PPr); // rpr样式与ppr与styleid样式合并
rpr := new RPrUnitDecorator(r.RPr);
pos := 1;
text := r.T.Text;
while pos <= length(text) do
begin
c := text[pos];
pos ++;
if ord(c) > 127 then
begin
c := text[pos-1 : pos+1];
pos += 2;
end
words_[length(words_)] := array(utf8ToAnsi(c), rpr);
end
end
paragraph_.PPr := new PPrUnitDecorator(paragraph_.PPr);
end;
function TSParagraphWare.SetPPrByStyleId(ppr, style_id);
begin
style := styles_.StyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
self.SetPPrByStyleId(ppr, based_on);
ppr.Copy(style.PPr);
ppr.Rpr.Copy(style.RPr);
end
end;
function TSParagraphWare.SetPPr(ppr);
begin
new_ppr := new PPr();
style_id := ppr.PStyle.Val;
self.SetPPrByStyleId(new_ppr, style_id);
new_ppr.Copy(ppr);
ppr.Copy(new_ppr);
end;
function TSParagraphWare.SetRPrByStyleId(rpr, style_id);
begin
style := styles_.StyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
self.SetRPrByStyleId(rpr, based_on);
rpr.Copy(style.RPr);
end
end;
function TSParagraphWare.SetRPr(rpr, ppr);
begin
// rpr,先继承ppr,再继承样式,最后继承自己
new_rpr := new RPr();
style_id := rpr.RStyle.Val;
new_rpr.Copy(ppr.RPr);
self.SetRPrByStyleId(new_rpr, style_id);
new_rpr.Copy(rpr);
rpr.Copy(new_rpr);
end;
function TSParagraphWare.GetWords();
begin
return words_;
end;
function TSParagraphWare.ReadParagraph();
begin
return paragraph_;
end
+59
View File
@@ -0,0 +1,59 @@
type TSPdfSingletonKit = class
public
function Create();
class function GetComponent(components: Components; type: string);
public
hash_: array of tslobj;
private
static singleton_: Singleton;
end;
function TSPdfSingletonKit.Create();
begin
hash_ := array();
end;
class function TSPdfSingletonKit.GetComponent(components: Components; type: string);
begin
if not ifObj(singleton_) then singleton_ := new TSPdfSingletonKit();
if ifnil(singleton_.hash_[components]) then singleton_.hash_[components] := array();
if ifObj(singleton_.hash_[components][type]) then return singleton_.hash_[components][type];
case type of
"styles":
begin
styles := components.Styles;
styles.Deserialize();
singleton_.hash_[components][type] := styles;
return styles;
end
"styles_adapter":
begin
styles := class(TSPdfSingletonKit).GetComponent(components, "styles");
styles_adapter := new StylesAdapter(styles);
singleton_.hash_[components][type] := styles_adapter;
return styles_adapter;
end
"numbering_ware":
begin
numbering := components.Numbering;
numbering_ware := nil;
if ifObj(numbering) then
begin
numbering.Deserialize();
numbering_ware := new TSNumberingWare(numbering);
end
singleton_.hash_[components][type] := numbering_ware;
return numbering_ware;
end
"document_rels_adapter":
begin
document_rels := components.DocumentRels;
document_rels.Deserialize();
rels_adapter := new RelationShipsAdapter(document_rels);
singleton_.hash_[components][type] := rels_adapter;
return rels_adapter;
end
end;
end;
+10
View File
@@ -0,0 +1,10 @@
type TSPoint = class
function Create();
begin
self.X := 0;
self.Y := 0;
end
X: real;
Y: real;
end;