From 7da30f77ab0aba7b2199037a264e801848449a2e Mon Sep 17 00:00:00 2001 From: csh Date: Wed, 4 Jun 2025 15:20:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96=EF=BC=9Aco?= =?UTF-8?q?py=E6=94=B9=E6=88=90fallback=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/DTPAdvancedRanges.tsf | 160 ++++++++++++++++----------------- 1 file changed, 78 insertions(+), 82 deletions(-) diff --git a/internal/DTPAdvancedRanges.tsf b/internal/DTPAdvancedRanges.tsf index 9e5a8af..56059cf 100644 --- a/internal/DTPAdvancedRanges.tsf +++ b/internal/DTPAdvancedRanges.tsf @@ -213,9 +213,8 @@ private function SetTblPrByStyleId(var tbl_pr: TblPr; style_id: string); function SetTrTrPr(var tr_pr: TrPr); function SetTrPrByStyleId(var tr_pr: TrPr; style_id: string); - function SetTcTcPr(var tc: Tc); - function SetTcPrByStyleId(var tc_pr: TcPr; style_id: string); - function OverrideTcPrByTblStylePrType(var tc_pr: TcPr; type: string); + function SetTcTcPr(var tc: Tc; type: string); + function SetTcPrByStyleId(var tc_pr: TcPr; style_id: string; type: string); private [weakref]docx_to_pdf_: TSDocxToPdf; @@ -747,8 +746,8 @@ function PRange.Calc(): boolean;override; begin // ppr.rpr是无效的,应该以ppr.pStyle为准 {self.}SetPPPr(p_); + if not p_.PPr.RPr.Sz.Val then p_.PPr.RPr.Sz.Val := docx_to_pdf_.Font.GetDefaultSz(); ppr_ := p_.PPr; - if not ppr_.RPr.Sz.Val then ppr_.RPr.Sz.Val := docx_to_pdf_.Font.GetDefaultSz(); {self.}Init(); {self.}SetLvlText(); @@ -1519,15 +1518,17 @@ end; function PRange.SetPPPr(var p: P); begin - new_ppr := new PPr(); - styles := docx_to_pdf_.DocxComponents.GetStyles(); - new_ppr.Copy(styles.DocDefaults.PPrDefault.PPr); - new_ppr.RPr.Copy(styles.DocDefaults.RPrDefault.RPr); - if {self.}Parent is class(TcRange) then - {self.}SetPPrByStyleId(new_ppr, {self.}Parent.GetTblStyleId()); - {self.}SetPPrByStyleId(new_ppr, p.PPr.PStyle.Val); - new_ppr.Copy(p.PPr); - p.PPr.Copy(new_ppr); + if not p.PPr then + begin + styles := docx_to_pdf_.DocxComponents.GetStyles(); + p.PPr := styles.DocDefaults.PPrDefault.PPr; + end + else begin + if p.PPr.PStyle.Val then + {self.}SetPPrByStyleId(p.PPr, p.PPr.PStyle.Val); + else if {self.}Parent is class(TcRange) then + {self.}SetPPrByStyleId(p.PPr, {self.}Parent.GetTblStyleId()); + end end; function PRange.SetPPrByStyleId(var ppr: PPr; style_id: string); @@ -1536,24 +1537,27 @@ begin style := styles.GetStyleByStyleId(style_id); if ifObj(style) then begin + ppr.SetFallback(style.PPr); based_on := style.BasedOn.Val; - {self.}SetPPrByStyleId(ppr, based_on); - ppr.Copy(style.PPr); - ppr.RPr.Copy(style.RPr); + {self.}SetPPrByStyleId(ifnil(style.PPr) ? ppr : style.PPr, based_on); end end; function PRange.SetRRPr(var r: R; ppr_: PPr); begin - new_rpr := new RPr(); - styles := docx_to_pdf_.DocxComponents.GetStyles(); - new_rpr.Copy(styles.DocDefaults.RPrDefault.RPr); - if {self.}Parent is class(TcRange) then - {self.}SetRPrByTblStyleId(new_rpr, {self.}Parent.GetTblStyleId()); - {self.}SetRPrByStyleId(new_rpr, ppr_.PStyle.Val); - {self.}SetRPrByStyleId(new_rpr, r.RPr.RStyle.Val); - new_rpr.Copy(r.RPr); - r.RPr.Copy(new_rpr); + if ifnil(r.RPr) then + begin + styles := docx_to_pdf_.DocxComponents.GetStyles(); + r.RPr := styles.DocDefaults.RPrDefault.RPr; + end + else begin + if r.RPr.RStyle.Val then + {self.}SetRPrByStyleId(r.RPr, r.RPr.RStyle.Val); + 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; function PRange.SetRPrByTblStyleId(var rpr: RPr; style_id: string); @@ -1561,16 +1565,11 @@ begin styles := docx_to_pdf_.DocxComponents.GetStylesAdapter(); style := styles.GetStyleByStyleId(style_id); 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 pr_type := {self.}Parent.GetPrType(); 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; @@ -1580,9 +1579,9 @@ begin style := styles.GetStyleByStyleId(style_id); if ifObj(style) then begin + rpr.SetFallback(style.RPr); based_on := style.BasedOn.Val; - {self.}SetRPrByStyleId(rpr, based_on); - rpr.Copy(style.RPr); + {self.}SetRPrByStyleId(ifnil(style.RPr) ? rpr : style.RPr, based_on); end end; @@ -1899,25 +1898,25 @@ begin if tbl_pr.TblBorders.Top then begin 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; end if tbl_pr.TblBorders.InsideH and {self.}VMerge <> {self.}Parent.Rows-1 then begin 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; end if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then begin 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; end if tbl_pr.TblBorders.Bottom and {self.}VMerge = {self.}Parent.Rows-1 then begin 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; end end @@ -1926,13 +1925,13 @@ begin if tbl_pr.TblBorders.Bottom then begin 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; end if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then begin 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; end end @@ -1940,19 +1939,19 @@ begin if tbl_pr.TblBorders.Bottom and {self.}VMerge = {self.}Parent.Rows-1 then begin 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; end if tbl_pr.TblBorders.InsideH and {self.}VMerge <> {self.}Parent.Rows-1 then begin 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; end if tbl_pr.TblBorders.InsideV and {self.}Col <> {self.}Parent.Cols()-1 then begin 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; end end @@ -1962,7 +1961,7 @@ begin if tbl_pr.TblBorders.Left then begin 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; end end @@ -1971,7 +1970,7 @@ begin if tbl_pr.TblBorders.Right then begin 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; end end @@ -2046,11 +2045,10 @@ begin pos := 0; for j,tc in tcs do begin - {self.}SetTcTcPr(tc); - if i = 0 then {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "firstRow"); - else if i = length(trs)-1 then {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "lastRow") - else if (i + 1) % 2 = 0 then {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "band1Horz"); - else {self.}OverrideTcPrByTblStylePrType(tc.TcPr, "band2Horz"); + if i = 0 then {self.}SetTcTcPr(tc, "firstRow"); + else if i = length(trs)-1 then {self.}SetTcTcPr(tc, "lastRow") + else if (i + 1) % 2 = 0 then {self.}SetTcTcPr(tc, "band1Horz"); + else {self.}SetTcTcPr(tc, "band2Horz"); grid_span := tc.TcPr.GridSpan; if tc.TcPr.VMerge.IsApplied and tc.TcPr.VMerge.Val <> "restart" then @@ -2197,19 +2195,11 @@ begin {self.}Width := total_width; 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); begin - new_tbl_pr := new TblPr(); - {self.}SetTblPrByStyleId(new_tbl_pr, tbl.TblPr.TblStyle.Val); - new_tbl_pr.Copy(tbl.TblPr); - tbl.TblPr.Copy(new_tbl_pr); + if ifnil(tbl.TblPr) then + tbl.TblPr := new TblPr(); + {self.}SetTblPrByStyleId(tbl.TblPr, tbl.TblPr.TblStyle.Val); end; function TblRange.SetTblPrByStyleId(var tbl_pr: TblPr; style_id: string); @@ -2218,18 +2208,17 @@ begin style := styles.GetStyleByStyleId(style_id); if ifObj(style) then begin + tbl_pr.SetFallback(style.TblPr); based_on := style.BasedOn.Val; - {self.}SetTblPrByStyleId(tbl_pr, based_on); - tbl_pr.Copy(style.TblPr); + {self.}SetTblPrByStyleId(ifnil(style.TblPr) ? tbl_pr : style.TblPr, based_on); end end; function TblRange.SetTrTrPr(var tr: Tr); begin - new_tr_pr := new TrPr(); - {self.}SetTrPrByStyleId(new_tr_pr, tbl_pr_.TblStyle.Val); - new_tr_pr.Copy(tr.TrPr); - tr.TrPr.Copy(new_tr_pr); + if ifnil(tr.TrPr) then + tr.TrPr := new TrPr(); + {self.}SetTrPrByStyleId(tr.TrPr, tbl_pr_.TblStyle.Val); end; function TblRange.SetTrPrByStyleId(var tr_pr: TrPr; style_id: string); @@ -2238,29 +2227,36 @@ begin style := styles.GetStyleByStyleId(style_id); if ifObj(style) then begin + tr_pr.SetFallback(style.TrPr); based_on := style.BasedOn.Val; - {self.}SetTrPrByStyleId(tr_pr, based_on); - tr_pr.Copy(style.TrPr); + {self.}SetTrPrByStyleId(ifnil(style.TrPr) ? tr_pr : style.TrPr, based_on); end end; -function TblRange.SetTcTcPr(var tc: Tc); +function TblRange.SetTcTcPr(var tc: Tc; type: string); begin - new_tc_pr := new TcPr(); - {self.}SetTcPrByStyleId(new_tc_pr, tbl_pr_.TblStyle.Val); - new_tc_pr.Copy(tc.TcPr); - tc.TcPr.Copy(new_tc_pr); + if ifnil(tc.TcPr) then + tc.TcPr := new TcPr(); + {self.}SetTcPrByStyleId(tc.TcPr, tbl_pr_.TblStyle.Val, type); end; -function TblRange.SetTcPrByStyleId(var tc_pr: TcPr; style_id: string); +function TblRange.SetTcPrByStyleId(var tc_pr: TcPr; style_id: string; type: string); begin - styles := docx_to_pdf_.DocxComponents.GetStylesAdapter(); - style := styles.GetStyleByStyleId(style_id); - if ifObj(style) then + if type then begin - based_on := style.BasedOn.Val; - {self.}SetTcPrByStyleId(tc_pr, based_on); - tc_pr.Copy(style.TcPr); + style := docx_to_pdf_.DocxComponents.GetTblStylePrByType(style_id, type); + if ifObj(style) then + 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; @@ -2428,7 +2424,7 @@ begin styles := docx_to_pdf_.DocxComponents.GetStyles(); 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; end;