性能优化:copy改成fallback模式

This commit is contained in:
csh 2025-06-04 15:20:13 +08:00
parent 4be804cb1b
commit 7da30f77ab
1 changed files with 78 additions and 82 deletions

View File

@ -213,9 +213,8 @@ private
function SetTblPrByStyleId(var tbl_pr: TblPr; style_id: string); function SetTblPrByStyleId(var tbl_pr: TblPr; style_id: string);
function SetTrTrPr(var tr_pr: TrPr); function SetTrTrPr(var tr_pr: TrPr);
function SetTrPrByStyleId(var tr_pr: TrPr; style_id: string); function SetTrPrByStyleId(var tr_pr: TrPr; style_id: string);
function SetTcTcPr(var tc: Tc); function SetTcTcPr(var tc: Tc; type: string);
function SetTcPrByStyleId(var tc_pr: TcPr; style_id: string); function SetTcPrByStyleId(var tc_pr: TcPr; style_id: string; type: string);
function OverrideTcPrByTblStylePrType(var tc_pr: TcPr; type: string);
private private
[weakref]docx_to_pdf_: TSDocxToPdf; [weakref]docx_to_pdf_: TSDocxToPdf;
@ -747,8 +746,8 @@ function PRange.Calc(): boolean;override;
begin begin
// ppr.rpr是无效的应该以ppr.pStyle为准 // ppr.rpr是无效的应该以ppr.pStyle为准
{self.}SetPPPr(p_); {self.}SetPPPr(p_);
if not p_.PPr.RPr.Sz.Val then p_.PPr.RPr.Sz.Val := docx_to_pdf_.Font.GetDefaultSz();
ppr_ := p_.PPr; ppr_ := p_.PPr;
if not ppr_.RPr.Sz.Val then ppr_.RPr.Sz.Val := docx_to_pdf_.Font.GetDefaultSz();
{self.}Init(); {self.}Init();
{self.}SetLvlText(); {self.}SetLvlText();
@ -1519,15 +1518,17 @@ end;
function PRange.SetPPPr(var p: P); function PRange.SetPPPr(var p: P);
begin begin
new_ppr := new PPr(); if not p.PPr then
styles := docx_to_pdf_.DocxComponents.GetStyles(); begin
new_ppr.Copy(styles.DocDefaults.PPrDefault.PPr); styles := docx_to_pdf_.DocxComponents.GetStyles();
new_ppr.RPr.Copy(styles.DocDefaults.RPrDefault.RPr); p.PPr := styles.DocDefaults.PPrDefault.PPr;
if {self.}Parent is class(TcRange) then end
{self.}SetPPrByStyleId(new_ppr, {self.}Parent.GetTblStyleId()); else begin
{self.}SetPPrByStyleId(new_ppr, p.PPr.PStyle.Val); if p.PPr.PStyle.Val then
new_ppr.Copy(p.PPr); {self.}SetPPrByStyleId(p.PPr, p.PPr.PStyle.Val);
p.PPr.Copy(new_ppr); else if {self.}Parent is class(TcRange) then
{self.}SetPPrByStyleId(p.PPr, {self.}Parent.GetTblStyleId());
end
end; end;
function PRange.SetPPrByStyleId(var ppr: PPr; style_id: string); function PRange.SetPPrByStyleId(var ppr: PPr; style_id: string);
@ -1536,24 +1537,27 @@ begin
style := styles.GetStyleByStyleId(style_id); style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then if ifObj(style) then
begin begin
ppr.SetFallback(style.PPr);
based_on := style.BasedOn.Val; based_on := style.BasedOn.Val;
{self.}SetPPrByStyleId(ppr, based_on); {self.}SetPPrByStyleId(ifnil(style.PPr) ? ppr : style.PPr, based_on);
ppr.Copy(style.PPr);
ppr.RPr.Copy(style.RPr);
end end
end; end;
function PRange.SetRRPr(var r: R; ppr_: PPr); function PRange.SetRRPr(var r: R; ppr_: PPr);
begin begin
new_rpr := new RPr(); if ifnil(r.RPr) then
styles := docx_to_pdf_.DocxComponents.GetStyles(); begin
new_rpr.Copy(styles.DocDefaults.RPrDefault.RPr); styles := docx_to_pdf_.DocxComponents.GetStyles();
if {self.}Parent is class(TcRange) then r.RPr := styles.DocDefaults.RPrDefault.RPr;
{self.}SetRPrByTblStyleId(new_rpr, {self.}Parent.GetTblStyleId()); end
{self.}SetRPrByStyleId(new_rpr, ppr_.PStyle.Val); else begin
{self.}SetRPrByStyleId(new_rpr, r.RPr.RStyle.Val); if r.RPr.RStyle.Val then
new_rpr.Copy(r.RPr); {self.}SetRPrByStyleId(r.RPr, r.RPr.RStyle.Val);
r.RPr.Copy(new_rpr); else if ppr_.PStyle.Val then
{self.}SetRPrByStyleId(r.RPr, ppr_.PStyle.Val);
else if {self.}Parent is class(TcRange) then
{self.}SetRPrByTblStyleId(r.RPr, {self.}Parent.GetTblStyleId());
end
end; end;
function PRange.SetRPrByTblStyleId(var rpr: RPr; style_id: string); function PRange.SetRPrByTblStyleId(var rpr: RPr; style_id: string);
@ -1561,16 +1565,11 @@ begin
styles := docx_to_pdf_.DocxComponents.GetStylesAdapter(); styles := docx_to_pdf_.DocxComponents.GetStylesAdapter();
style := styles.GetStyleByStyleId(style_id); style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then if ifObj(style) then
begin
based_on := style.BasedOn.Val;
{self.}SetRPrByTblStyleId(rpr, based_on);
rpr.Copy(style.RPr);
end
if {self.}Parent is class(TcRange) then
begin begin
pr_type := {self.}Parent.GetPrType(); pr_type := {self.}Parent.GetPrType();
tbl_style_pr := docx_to_pdf_.DocxComponents.GetTblStylePrByType(style_id, pr_type); tbl_style_pr := docx_to_pdf_.DocxComponents.GetTblStylePrByType(style_id, pr_type);
if tbl_style_pr then rpr.Copy(tbl_style_pr.RPr); rpr.SetFallback(tbl_style_pr.RPr);
{self.}SetRPrByTblStyleId(ifnil(tbl_style_pr.RPr) ? rpr : tbl_style_pr.RPr, style.BasedOn.Val);
end end
end; end;
@ -1580,9 +1579,9 @@ begin
style := styles.GetStyleByStyleId(style_id); style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then if ifObj(style) then
begin begin
rpr.SetFallback(style.RPr);
based_on := style.BasedOn.Val; based_on := style.BasedOn.Val;
{self.}SetRPrByStyleId(rpr, based_on); {self.}SetRPrByStyleId(ifnil(style.RPr) ? rpr : style.RPr, based_on);
rpr.Copy(style.RPr);
end end
end; end;
@ -1899,25 +1898,25 @@ begin
if tbl_pr.TblBorders.Top then if tbl_pr.TblBorders.Top then
begin begin
if not tc_pr_.TcBorders.Top then if not tc_pr_.TcBorders.Top then
tc_pr_.TcBorders.Top.Copy(tbl_pr.TblBorders.Top); tc_pr_.TcBorders.Top.SetFallback(tbl_pr.TblBorders.Top);
range.Top := true; range.Top := true;
end end
if tbl_pr.TblBorders.InsideH and {self.}VMerge <> {self.}Parent.Rows-1 then if tbl_pr.TblBorders.InsideH and {self.}VMerge <> {self.}Parent.Rows-1 then
begin begin
if not tc_pr_.TcBorders.Bottom then if not tc_pr_.TcBorders.Bottom then
tc_pr_.TcBorders.Bottom.Copy(tbl_pr.TblBorders.InsideH); tc_pr_.TcBorders.Bottom.SetFallback(tbl_pr.TblBorders.InsideH);
range.Bottom := true; range.Bottom := true;
end end
if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then
begin begin
if not tc_pr_.TcBorders.Right then if not tc_pr_.TcBorders.Right then
tc_pr_.TcBorders.Right.Copy(tbl_pr.TblBorders.InsideV); tc_pr_.TcBorders.Right.SetFallback(tbl_pr.TblBorders.InsideV);
range.Right := true; range.Right := true;
end end
if tbl_pr.TblBorders.Bottom and {self.}VMerge = {self.}Parent.Rows-1 then if tbl_pr.TblBorders.Bottom and {self.}VMerge = {self.}Parent.Rows-1 then
begin begin
if not tc_pr_.TcBorders.Bottom then if not tc_pr_.TcBorders.Bottom then
tc_pr_.TcBorders.Bottom.Copy(tbl_pr.TblBorders.Bottom); tc_pr_.TcBorders.Bottom.SetFallback(tbl_pr.TblBorders.Bottom);
range.Bottom := true; range.Bottom := true;
end end
end end
@ -1926,13 +1925,13 @@ begin
if tbl_pr.TblBorders.Bottom then if tbl_pr.TblBorders.Bottom then
begin begin
if not tc_pr_.TcBorders.Bottom then if not tc_pr_.TcBorders.Bottom then
tc_pr_.TcBorders.Bottom.Copy(tbl_pr.TblBorders.Bottom); tc_pr_.TcBorders.Bottom.SetFallback(tbl_pr.TblBorders.Bottom);
range.Bottom := true; range.Bottom := true;
end end
if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then
begin begin
if not tc_pr_.TcBorders.Right then if not tc_pr_.TcBorders.Right then
tc_pr_.TcBorders.Right.Copy(tbl_pr.TblBorders.InsideV); tc_pr_.TcBorders.Right.SetFallback(tbl_pr.TblBorders.InsideV);
range.Right := true; range.Right := true;
end end
end end
@ -1940,19 +1939,19 @@ begin
if tbl_pr.TblBorders.Bottom and {self.}VMerge = {self.}Parent.Rows-1 then if tbl_pr.TblBorders.Bottom and {self.}VMerge = {self.}Parent.Rows-1 then
begin begin
if not tc_pr_.TcBorders.Bottom then if not tc_pr_.TcBorders.Bottom then
tc_pr_.TcBorders.Bottom.Copy(tbl_pr.TblBorders.Bottom); tc_pr_.TcBorders.Bottom.SetFallback(tbl_pr.TblBorders.Bottom);
range.Bottom := true; range.Bottom := true;
end end
if tbl_pr.TblBorders.InsideH and {self.}VMerge <> {self.}Parent.Rows-1 then if tbl_pr.TblBorders.InsideH and {self.}VMerge <> {self.}Parent.Rows-1 then
begin begin
if not tc_pr_.TcBorders.Bottom then if not tc_pr_.TcBorders.Bottom then
tc_pr_.TcBorders.Bottom.Copy(tbl_pr.TblBorders.InsideH); tc_pr_.TcBorders.Bottom.SetFallback(tbl_pr.TblBorders.InsideH);
range.Bottom := true; range.Bottom := true;
end end
if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then
begin begin
if not tc_pr_.TcBorders.Right then if not tc_pr_.TcBorders.Right then
tc_pr_.TcBorders.Right.Copy(tbl_pr.TblBorders.InsideV); tc_pr_.TcBorders.Right.SetFallback(tbl_pr.TblBorders.InsideV);
range.Right := true; range.Right := true;
end end
end end
@ -1962,7 +1961,7 @@ begin
if tbl_pr.TblBorders.Left then if tbl_pr.TblBorders.Left then
begin begin
if not tc_pr_.TcBorders.Left then if not tc_pr_.TcBorders.Left then
tc_pr_.TcBorders.Left.Copy(tbl_pr.TblBorders.Left); tc_pr_.TcBorders.Left.SetFallback(tbl_pr.TblBorders.Left);
range.Left := true; range.Left := true;
end end
end end
@ -1971,7 +1970,7 @@ begin
if tbl_pr.TblBorders.Right then if tbl_pr.TblBorders.Right then
begin begin
if not tc_pr_.TcBorders.Right then if not tc_pr_.TcBorders.Right then
tc_pr_.TcBorders.Right.Copy(tbl_pr.TblBorders.Right); tc_pr_.TcBorders.Right.SetFallback(tbl_pr.TblBorders.Right);
range.Right := true; range.Right := true;
end end
end end
@ -2046,11 +2045,10 @@ begin
pos := 0; pos := 0;
for j,tc in tcs do for j,tc in tcs do
begin begin
{self.}SetTcTcPr(tc); if i = 0 then {self.}SetTcTcPr(tc, "firstRow");
if i = 0 then {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "firstRow"); else if i = length(trs)-1 then {self.}SetTcTcPr(tc, "lastRow")
else if i = length(trs)-1 then {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "lastRow") else if (i + 1) % 2 = 0 then {self.}SetTcTcPr(tc, "band1Horz");
else if (i + 1) % 2 = 0 then {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "band1Horz"); else {self.}SetTcTcPr(tc, "band2Horz");
else {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "band2Horz");
grid_span := tc.TcPr.GridSpan; grid_span := tc.TcPr.GridSpan;
if tc.TcPr.VMerge.IsApplied and tc.TcPr.VMerge.Val <> "restart" then if tc.TcPr.VMerge.IsApplied and tc.TcPr.VMerge.Val <> "restart" then
@ -2197,19 +2195,11 @@ begin
{self.}Width := total_width; {self.}Width := total_width;
end; end;
function TblRange.OverrideTcPrByTblStylePrType(var tc_pr: TcPr; type: string);
begin
// tc_pr应该是经过外层copy的
tbl_style_pr := docx_to_pdf_.DocxComponents.GetTblStylePrByType(tbl_pr_.TblStyle.Val, type);
if tbl_style_pr then tc_pr.Copy(tbl_style_pr.TcPr);
end;
function TblRange.SetTblTblPr(var tbl: Tbl); function TblRange.SetTblTblPr(var tbl: Tbl);
begin begin
new_tbl_pr := new TblPr(); if ifnil(tbl.TblPr) then
{self.}SetTblPrByStyleId(new_tbl_pr, tbl.TblPr.TblStyle.Val); tbl.TblPr := new TblPr();
new_tbl_pr.Copy(tbl.TblPr); {self.}SetTblPrByStyleId(tbl.TblPr, tbl.TblPr.TblStyle.Val);
tbl.TblPr.Copy(new_tbl_pr);
end; end;
function TblRange.SetTblPrByStyleId(var tbl_pr: TblPr; style_id: string); function TblRange.SetTblPrByStyleId(var tbl_pr: TblPr; style_id: string);
@ -2218,18 +2208,17 @@ begin
style := styles.GetStyleByStyleId(style_id); style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then if ifObj(style) then
begin begin
tbl_pr.SetFallback(style.TblPr);
based_on := style.BasedOn.Val; based_on := style.BasedOn.Val;
{self.}SetTblPrByStyleId(tbl_pr, based_on); {self.}SetTblPrByStyleId(ifnil(style.TblPr) ? tbl_pr : style.TblPr, based_on);
tbl_pr.Copy(style.TblPr);
end end
end; end;
function TblRange.SetTrTrPr(var tr: Tr); function TblRange.SetTrTrPr(var tr: Tr);
begin begin
new_tr_pr := new TrPr(); if ifnil(tr.TrPr) then
{self.}SetTrPrByStyleId(new_tr_pr, tbl_pr_.TblStyle.Val); tr.TrPr := new TrPr();
new_tr_pr.Copy(tr.TrPr); {self.}SetTrPrByStyleId(tr.TrPr, tbl_pr_.TblStyle.Val);
tr.TrPr.Copy(new_tr_pr);
end; end;
function TblRange.SetTrPrByStyleId(var tr_pr: TrPr; style_id: string); function TblRange.SetTrPrByStyleId(var tr_pr: TrPr; style_id: string);
@ -2238,29 +2227,36 @@ begin
style := styles.GetStyleByStyleId(style_id); style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then if ifObj(style) then
begin begin
tr_pr.SetFallback(style.TrPr);
based_on := style.BasedOn.Val; based_on := style.BasedOn.Val;
{self.}SetTrPrByStyleId(tr_pr, based_on); {self.}SetTrPrByStyleId(ifnil(style.TrPr) ? tr_pr : style.TrPr, based_on);
tr_pr.Copy(style.TrPr);
end end
end; end;
function TblRange.SetTcTcPr(var tc: Tc); function TblRange.SetTcTcPr(var tc: Tc; type: string);
begin begin
new_tc_pr := new TcPr(); if ifnil(tc.TcPr) then
{self.}SetTcPrByStyleId(new_tc_pr, tbl_pr_.TblStyle.Val); tc.TcPr := new TcPr();
new_tc_pr.Copy(tc.TcPr); {self.}SetTcPrByStyleId(tc.TcPr, tbl_pr_.TblStyle.Val, type);
tc.TcPr.Copy(new_tc_pr);
end; end;
function TblRange.SetTcPrByStyleId(var tc_pr: TcPr; style_id: string); function TblRange.SetTcPrByStyleId(var tc_pr: TcPr; style_id: string; type: string);
begin begin
styles := docx_to_pdf_.DocxComponents.GetStylesAdapter(); if type then
style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then
begin begin
based_on := style.BasedOn.Val; style := docx_to_pdf_.DocxComponents.GetTblStylePrByType(style_id, type);
{self.}SetTcPrByStyleId(tc_pr, based_on); if ifObj(style) then
tc_pr.Copy(style.TcPr); tc_pr.SetFallback(style.TcPr);
end
else begin
styles := docx_to_pdf_.DocxComponents.GetStylesAdapter();
style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then
begin
tc_pr.SetFallback(style.TcPr);
based_on := style.BasedOn.Val;
{self.}SetTcPrByStyleId(ifnil(style.TcPr) ? tc_pr : style.TcPr, based_on);
end
end end
end; end;
@ -2428,7 +2424,7 @@ begin
styles := docx_to_pdf_.DocxComponents.GetStyles(); styles := docx_to_pdf_.DocxComponents.GetStyles();
default_rpr := styles.DocDefaults.RPrDefault.RPr; default_rpr := styles.DocDefaults.RPrDefault.RPr;
if rpr then default_rpr.Copy(rpr); default_rpr.SetFallback(rpr);
{self.}BaseSz := strtofloat(default_rpr.Sz.Val) / 2; {self.}BaseSz := strtofloat(default_rpr.Sz.Val) / 2;
end; end;