1. self.替换成{self.}

2. 修复table表格绘画失败问题
This commit is contained in:
csh 2024-07-10 16:17:44 +08:00
parent 615f4a5b39
commit d3f9431b26
15 changed files with 276 additions and 269 deletions

View File

@ -31,7 +31,7 @@ private
private
pdf_: PdfFile;
docx_components_: Components; // Components@DOCX
docx_components_: TSDocxComponentsWare; // TSDocxComponentsWare
cache_path_: string; // 临时目录,用来存放临时文件
sect_ware_array_: array of TSSectWare; // 页面布局组件数组
font_ware_: TSFontWare; // 字体部件
@ -44,10 +44,10 @@ end;
function TSDocxToPdf.Create(alias: string; file: string);
begin
pdf_ := new PdfFile();
self.InitPdfEncoder();
self.InitDocxComponents(alias, file);
self.InitCachePath(file);
self.InitSectWare();
{self.}InitPdfEncoder();
{self.}InitDocxComponents(alias, file);
{self.}InitCachePath(file);
{self.}InitSectWare();
font_ware_ := new TSFontWare(pdf_);
current_page_ := nil;
point_ := new TSPoint();
@ -58,7 +58,6 @@ end;
function TSDocxToPdf.Destroy();
begin
removeDir("", cache_path_);
class(TSPdfSingletonKit).Release(docx_components_);
end;
function TSDocxToPdf.SaveToFile(alias: string; file: string): integer;
@ -73,14 +72,14 @@ begin
begin
if prev <> sect_ware then
begin
self.AddPage(sect_ware);
{self.}AddPage(sect_ware);
prev := sect_ware;
end
elements := sect_ware.Elements;
for _,element in elements do
begin
if element.LocalName = "p" then self.TransformP(sect_ware, element);
else if element.LocalName = "tbl" then self.TransformTbl(sect_ware, element);
if element.LocalName = "p" then {self.}TransformP(sect_ware, element);
else if element.LocalName = "tbl" then {self.}TransformTbl(sect_ware, element);
end
end
end;
@ -120,7 +119,7 @@ end;
function TSDocxToPdf.InitDocxComponents(alias: string; file: string);
begin
namespace "DOCX";
docx_components_ := new Components();
docx_components_ := new TSDocxComponentsWare();
[err, msg] := docx_components_.OpenFile(alias, file, nil);
if err then raise "Create obejct 'TSDocxFile' failed.";
end;
@ -139,7 +138,7 @@ begin
document := docx_components_.Document;
document.Deserialize();
sect_ware_array_ := array();
self.AllocateElementsToSectWare();
{self.}AllocateElementsToSectWare();
end;
function TSDocxToPdf.AllocateElementsToSectWare();
@ -177,14 +176,14 @@ begin
current_page_ := pdf_.AddPage();
current_page_.SetWidth(sect_ware.SectPr.PgSz.W);
current_page_.SetHeight(sect_ware.SectPr.PgSz.H);
self.ResetCoordinates(sect_ware);
{self.}ResetCoordinates(sect_ware);
len := length(page_array_);
page_array_[len] := current_page_;
page_index_[current_page_] := len;
if sysparams["_PDF_PAGE_GRID_DEBUG_"] then
self.PrintGrid(current_page_, sect_ware);
{self.}PrintGrid(current_page_, sect_ware);
return current_page_;
end;

View File

@ -1,18 +1,18 @@
type TSPdfCellRange = class(TSPdfBasicRange)
public
function Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: Components; sect_ware: TSSectWare; tc: Tc; tbl_pr: TblPr);
function Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: TSDocxComponentsWare; sect_ware: TSSectWare; tc: Tc; tbl_pr: TblPr);
function Calc();
function AlignHeight(surplus: real);
function AlignHeight(height: real; surplus: real);
function GetLastPage();
function Do();override;
private
docx_to_pdf_: TSDocxToPdf;
[weakref]docx_to_pdf_: TSDocxToPdf;
page_: PdfPage;
docx_components_: Components;
sect_ware_: TSSectWare;
tc_: Tc;
tbl_pr_: TblPr;
[weakref]docx_components_ware_: TSDocxComponentsWare;
[weakref]sect_ware_: TSSectWare;
[weakref]tc_: Tc;
[weakref]tbl_pr_: TblPr;
region_array_: array of Region; // 单元格可能跨页,所以可能存在多个
end;
@ -27,46 +27,46 @@ type Region = class
RangeArr: array of TSPdfAbstractRange;
end;
function TSPdfCellRange.Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: Components; sect_ware: TSSectWare; tc: Tc; tbl_pr: TblPr);
function TSPdfCellRange.Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: TSDocxComponentsWare; sect_ware: TSSectWare; tc: Tc; tbl_pr: TblPr);
begin
docx_to_pdf_ := docx_to_pdf;
page_ := pg;
docx_components_ := components;
docx_components_ware_ := components;
sect_ware_ := sect_ware;
tc_ := tc;
tbl_pr_ := tbl_pr;
region_array_ := array();
self.Page := page_;
{self.}Page := page_;
end;
function TSPdfCellRange.Calc();
begin
region := new Region();
region.RectangleRange.EndX := self.StartX;
region.RectangleRange.EndY := self.StartY;
region.RectangleRange.Width := self.Width;
region.RectangleRange.FixedHeight := self.FixedHeight;
region.RectangleRange.EndX := {self.}StartX;
region.RectangleRange.EndY := {self.}StartY;
region.RectangleRange.Width := {self.}Width;
region.RectangleRange.FixedHeight := {self.}FixedHeight;
region.RectangleRange.Page := page_;
region_array_[length(region_array_)] := region;
self.EndX := self.StartX;
self.EndY := self.StartY;
cell_x := self.EndX + tbl_pr_.TblCellMar.Left.W;
cell_y := self.EndY - tbl_pr_.TblCellMar.Top.W;
cell_w := self.Width - tbl_pr_.TblCellMar.Right.W - tbl_pr_.TblCellMar.Left.W;
cell_h := self.FixedHeight;
{self.}EndX := {self.}StartX;
{self.}EndY := {self.}StartY;
cell_x := {self.}EndX + tbl_pr_.TblCellMar.Left.W;
cell_y := {self.}EndY - tbl_pr_.TblCellMar.Top.W;
cell_w := {self.}Width - tbl_pr_.TblCellMar.Right.W - tbl_pr_.TblCellMar.Left.W;
cell_h := {self.}FixedHeight;
elements := tc_.Elements();
for _,element in elements do
begin
range := nil;
if element.LocalName = "p" then
begin
range := new TSPdfParagraphRange(docx_to_pdf_, page_, docx_components_, sect_ware_, element);
range := new TSPdfParagraphRange(docx_to_pdf_, page_, docx_components_ware_, sect_ware_, element);
range.SetExtraStyleId(tbl_pr_.TblStyle.Val);
end
else if element.LocalName = "tbl" then
begin
range := new TSPdfTableRange(docx_to_pdf_, page_, docx_components_, sect_ware_, element);
range := new TSPdfTableRange(docx_to_pdf_, page_, docx_components_ware_, sect_ware_, element);
end
if ifObj(range) then
begin
@ -76,12 +76,12 @@ begin
range.FixedHeight := cell_h;
range.Calc();
region.RangeArr[length(region.RangeArr)] := range;
self.DynamicHeight += range.DynamicHeight;
{self.}DynamicHeight += range.DynamicHeight;
cell_y -= range.DynamicHeight;
end
end
self.EndY := cell_y;
self.DynamicHeight += tbl_pr_.TblCellMar.Top.W + tbl_pr_.TblCellMar.Bottom.W;
{self.}EndY := cell_y;
{self.}DynamicHeight += tbl_pr_.TblCellMar.Top.W + tbl_pr_.TblCellMar.Bottom.W;
end;
function TSPdfCellRange.Do();override;
@ -94,16 +94,17 @@ begin
end
end;
function TSPdfCellRange.AlignHeight(surplus: real);
function TSPdfCellRange.AlignHeight(height: real; surplus: real);
begin
region := region_array_[0];
if surplus < 1e-6 then
begin
region.RectangleRange.DynamicHeight := height;
if region.RectangleRange.FixedHeight then region.RectangleRange.DynamicHeight := region.RectangleRange.FixedHeight;
self.EndY := self.StartY - region.RectangleRange.DynamicHeight;
{self.}EndY := {self.}StartY - region.RectangleRange.DynamicHeight;
return;
end
region.RectangleRange.DynamicHeight := self.StartY - sect_ware_.SectPr.PgMar.Bottom;
region.RectangleRange.DynamicHeight := {self.}StartY - sect_ware_.SectPr.PgMar.Bottom;
arr := region.RangeArr;
region.RangeArr := array();
span := sect_ware_.SectPr.PgSz.H - sect_ware_.SectPr.PgMar.Top - sect_ware_.SectPr.PgMar.Bottom;
@ -112,11 +113,11 @@ begin
begin
page_ := docx_to_pdf_.GetNextPage(page_);
region := new Region();
region.RectangleRange.EndX := self.StartX;
region.RectangleRange.EndY := self.StartY;
region.RectangleRange.Width := self.Width;
region.RectangleRange.EndX := {self.}StartX;
region.RectangleRange.EndY := {self.}StartY;
region.RectangleRange.Width := {self.}Width;
region.RectangleRange.DynamicHeight := span;
region.Page := page_;
region.RectangleRange.Page := page_;
region_array_[length(region_array_)] := region;
surplus -= span;
hash[region.RectangleRange.Page] := region;
@ -125,14 +126,14 @@ begin
begin
page_ := docx_to_pdf_.GetNextPage(page_);
region := new Region();
region.RectangleRange.EndX := self.StartX;
region.RectangleRange.EndX := {self.}StartX;
region.RectangleRange.EndY := sect_ware_.SectPr.PgSz.H - sect_ware_.SectPr.PgMar.Top;
region.RectangleRange.Width := self.Width;
region.RectangleRange.Width := {self.}Width;
region.RectangleRange.DynamicHeight := surplus;
region.RectangleRange.Page := page_;
region_array_[length(region_array_)] := region;
hash[region.RectangleRange.Page] := region;
self.EndY := region.RectangleRange.EndY - surplus;
{self.}EndY := region.RectangleRange.EndY - surplus;
end
for _,range in arr do
begin

View File

@ -12,7 +12,7 @@ end;
function TSPdfLineRange.Create(pg: PdfPage);
begin
self.Page := pg;
{self.}Page := pg;
range_array_ := array();
end;
@ -49,9 +49,9 @@ begin
last := range_array_[length(range_array_)-1];
case jc of
"center":
offset := (self.Width - last.EndX + first.EndX - last.Width) / 2;
offset := ({self.}Width - last.EndX + first.EndX - last.Width) / 2;
"right":
offset := self.Width - last.EndX + first.EndX - last.Width;
offset := {self.}Width - last.EndX + first.EndX - last.Width;
end;
if offset <= 0 then return;
for _,range in range_array_ do

View File

@ -1,6 +1,6 @@
type TSPdfParagraphRange = class(TSPdfBasicRange)
public
function Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: Components; sect_ware: TSSectWare; paragraph: P);
function Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: TSDocxComponentsWare; sect_ware: TSSectWare; paragraph: P);
function Calc();
function Do();override;
function SetExtraStyleId(style_id: string);
@ -20,62 +20,72 @@ private
function SplitTextToTextRange(text: string; rpr: RPr);
function RToDrawingRange(r: R; ppr: PPr);
function SetLinesAlignment(ppr: PPr);
function ResetCoordinates(ppr);
function ResetCoordinates(ppr: PPr);
function NewLineRange(): TSPdfLineRange;
private
docx_to_pdf_: TSDocxToPdf;
docx_components_: Components;
sect_ware_: TSSectWare;
paragraph_: P;
extra_style_id_: string;
[weakref]docx_to_pdf_: TSDocxToPdf;
[weakref]docx_components_ware_: TSDocxComponentsWare;
[weakref]sect_ware_: TSSectWare;
[weakref]paragraph_: P;
page_: PdfPage;
extra_style_id_: string;
range_array_: array of TSPdfBasicRange;
line_range_array_: array of TSPdfLineRange;
end;
function TSPdfParagraphRange.Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: Components; sect_ware: TSSectWare; paragraph: P);
function TSPdfParagraphRange.Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; components: TSDocxComponentsWare; sect_ware: TSSectWare; paragraph: P);
begin
docx_to_pdf_ := docx_to_pdf;
page_ := pg;
docx_components_ := Components;
docx_components_ware_ := components;
sect_ware_ := sect_ware;
paragraph_ := paragraph;
extra_style_id_ := "";
range_array_ := array();
line_range_array_ := array();
self.Page := page_;
{self.}Page := page_;
end;
function TSPdfParagraphRange.Calc(): tableArray;
begin
self.SetPPr(paragraph_.PPr);
self.SetLvlText();
{self.}SetPPr(paragraph_.PPr);
{self.}SetLvlText();
ppr := new PPrUnitDecorator(paragraph_.PPr);
self.ResetCoordinates(ppr);
self.EndX := self.StartX;
self.EndY := self.StartY;
self.CheckAndAddPage(self.EndY, ppr.Spacing.Before);
self.EndY -= ppr.Spacing.Before;
self.DynamicHeight += ppr.Spacing.Before;
{self.}ResetCoordinates(ppr);
{self.}EndX := {self.}StartX;
{self.}EndY := {self.}StartY;
{self.}CheckAndAddPage({self.}EndY, ppr.Spacing.Before);
{self.}EndY -= ppr.Spacing.Before;
{self.}DynamicHeight += ppr.Spacing.Before;
rs := paragraph_.Rs();
if length(rs) = 0 then
begin
line_space := self.GetParagraphLineSpace(ppr.RPr.Sz.Val, ppr.Spacing.Line);
self.DynamicHeight += ppr.Spacing.After + line_space;
self.EndY -= self.DynamicHeight;
line_space := {self.}GetParagraphLineSpace(ppr.RPr.Sz.Val, ppr.Spacing.Line);
{self.}DynamicHeight += ppr.Spacing.After + line_space;
{self.}EndY -= {self.}DynamicHeight;
end
else begin
for _, r in rs do
begin
self.SetRPr(r.RPr, paragraph_.PPr);
{self.}SetRPr(r.RPr, paragraph_.PPr);
if r.Br.Type = "page" then
self.CheckAndAddPage(sect_ware_.SectPr.PgMar.Bottom, 1);
else if ifObj(r.Drawing.XmlNode) then self.RToDrawingRange(r, ppr);
else self.RToTextRange(r, ppr);
{self.}CheckAndAddPage(sect_ware_.SectPr.PgMar.Bottom, 1);
else if ifObj(r.Drawing.XmlNode) then {self.}RToDrawingRange(r, ppr);
else {self.}RToTextRange(r, ppr);
end
end
self.RangesToLines(ppr);
{self.}RangesToLines(ppr);
// hyperlinks := paragraph_.Hyperlinks(); // TOC
// for _,hyperlink in hyperlinks do
// begin
// rs := hyperlink.Rs();
// for _,r in rs do
// begin
// if r.FldChar.FldCharType = "begin" then break;
// {self.}RToTextRange(r, ppr);
// end
// end
end;
function TSPdfParagraphRange.Do();
@ -92,53 +102,53 @@ end;
function TSPdfParagraphRange.NewLineRange(): TSPdfLineRange;
begin
line_range := new TSPdfLineRange(page_);
line_range.StartX := self.EndX;
line_range.StartY := self.EndY;
line_range.Width := self.Width;
line_range.StartX := {self.}EndX;
line_range.StartY := {self.}EndY;
line_range.Width := {self.}Width;
return line_range;
end;
function TSPdfParagraphRange.RangesToLines(ppr: PPr);
begin
line_range := self.NewLineRange();
line_range := {self.}NewLineRange();
i := 0;
max_size := 0;
max_y := 0;
while i <= length(range_array_)-1 do
begin
range := range_array_[i];
if i = 0 then self.EndX += ppr.Ind.FirstLine;
if i = 0 then {self.}EndX += ppr.Ind.FirstLine;
if range is class(TSPdfTextRange) and range.RPr.Sz.Val > max_size then
max_size := range.RPr.Sz.Val;
if range.DynamicHeight > max_y then max_y := range.DynamicHeight;
line_space := self.GetParagraphLineSpace(max_size, ppr.Spacing.Line);
diff := self.EndY - sect_ware_.SectPr.PgMar.Bottom;
if self.CheckAndAddPage(self.EndY, max(line_space, range.DynamicHeight)) then
self.DynamicHeight += diff;
if_newline := self.EndX + range.Width - self.StartX > self.Width + 1e-6;
if if_newline and range.Width < self.Width then
line_space := {self.}GetParagraphLineSpace(max_size, ppr.Spacing.Line);
diff := {self.}EndY - sect_ware_.SectPr.PgMar.Bottom;
if {self.}CheckAndAddPage({self.}EndY, max(line_space, range.DynamicHeight)) then
{self.}DynamicHeight += diff;
if_newline := {self.}EndX + range.Width - {self.}StartX > {self.}Width + 1e-6;
if if_newline and range.Width < {self.}Width then
begin
offset := line_space > max_y ? (line_space - max_size) / 2 + max_size - max_size / 5 : max_y;
max_value := max(line_space, max_y);
line_range.Page := page_;
line_range.EndY := self.EndY - max_value;
line_range.EndY := {self.}EndY - max_value;
line_range.DynamicHeight := max_value;
line_range.SetAllRangeProp(pg: page_, ey: self.EndY - offset);
line_range.SetAllRangeProp(pg: page_, ey: {self.}EndY - offset);
line_range_array_[length(line_range_array_)] := line_range;
line_range := self.NewLineRange();
line_range := {self.}NewLineRange();
max_size := 0;
max_y := 0;
self.DynamicHeight += max_value;
self.EndY -= max_value;
self.EndX := self.StartX;
{self.}DynamicHeight += max_value;
{self.}EndY -= max_value;
{self.}EndX := {self.}StartX;
// w:hanging
sz := range.RPr.SzCs.Val ? range.RPr.SzCs.Val : range.RPr.Sz.Val ? range.RPr.Sz.Val : docx_to_pdf_.Font.GetDefaultSz();
self.EndX -= ppr.Ind.HangingChars ? ppr.Ind.HangingChars * sz : ppr.Ind.Hanging;
{self.}EndX -= ppr.Ind.HangingChars ? ppr.Ind.HangingChars * sz : ppr.Ind.Hanging;
continue;
end
range.EndX := self.EndX - range.StartX;
self.EndX += range.Width;
range.EndX := {self.}EndX - range.StartX;
{self.}EndX += range.Width;
line_range.AddRange(range);
i++;
if i = length(range_array_) then
@ -146,15 +156,15 @@ begin
offset := line_space > max_y ? (line_space - max_size) / 2 + max_size - max_size / 5 : max_y;
max_value := max(line_space, max_y) + ppr.Spacing.After;
line_range.Page := page_;
line_range.EndY := self.EndY - max_value;
line_range.EndY := {self.}EndY - max_value;
line_range.DynamicHeight := max_value;
line_range.SetAllRangeProp(pg: page_, ey: self.EndY - offset);
line_range.SetAllRangeProp(pg: page_, ey: {self.}EndY - offset);
line_range_array_[length(line_range_array_)] := line_range;
self.DynamicHeight += max_value;
self.EndY -= max_value;
{self.}DynamicHeight += max_value;
{self.}EndY -= max_value;
end
end
self.SetLinesAlignment(ppr);
{self.}SetLinesAlignment(ppr);
end;
function TSPdfParagraphRange.SetLinesAlignment(ppr: PPr);
@ -172,7 +182,7 @@ begin
page_ := docx_to_pdf_.GetNextPage(page_);
if ifnil(page_) then page_ := docx_to_pdf_.AddPage(sect_ware_);
point := docx_to_pdf_.GetCurrentPoint();
self.EndY := point.Y;
{self.}EndY := point.Y;
return true;
end
return false;
@ -194,7 +204,7 @@ function TSPdfParagraphRange.RToTextRange(r: R; ppr: PPr);
begin
rpr := new RPrUnitDecorator(r.RPr);
text := r.T.Text;
if ifString(text) then self.SplitTextToTextRange(text, rpr);
if ifString(text) then {self.}SplitTextToTextRange(text, rpr);
end;
function TSPdfParagraphRange.SplitTextToTextRange(text: string; rpr: RPr);
@ -202,7 +212,7 @@ begin
pos := 1;
while pos <= length(text) do
begin
num := self.GetUtf8CharLength(text[pos]);
num := {self.}GetUtf8CharLength(text[pos]);
word := text[pos : pos+num-1];
word := utf8ToAnsi(word);
pos += num;
@ -223,16 +233,16 @@ end;
function TSPdfParagraphRange.RToDrawingRange(r: R; ppr: PPr);
begin
xfrm := new XfrmUnitDecorator(r.Drawing._Inline.Graphic.GraphicData.Pic.SpPr.Xfrm);
rels_adapter := class(TSPdfSingletonKit).GetComponent(docx_components_, "document_rels_adapter");
rels_adapter := docx_components_ware_.GetDocumentRelsAdapter();
id := r.Drawing._Inline.Graphic.GraphicData.Pic.BlipFill.Blip.Embed;
rel := rels_adapter.Id(id);
image_path := "word/" + rel.Target;
image := docx_components_.Zip().Get(image_path);
image := docx_components_ware_.Zip().Get(image_path);
data := image.Data();
image_path := docx_to_pdf_.GetCachePath(image_path);
writeFile(rwBinary(), "", image_path, 0, length(data)-1, data);
image := nil;
case self.GetImageFileType(data) of
case {self.}GetImageFileType(data) of
"png":
image := docx_to_pdf_.GetPdf().LoadPngImageFromFile("", image_path);
"jpg":
@ -283,23 +293,23 @@ end;
function TSPdfParagraphRange.SetPPr(var ppr: PPr);
begin
new_ppr := new PPr();
styles := class(TSPdfSingletonKit).GetComponent(docx_components_, "styles");
styles := docx_components_ware_.GetStyles();
new_ppr.Copy(styles.DocDefaults.PPrDefault.PPr);
new_ppr.RPr.Copy(styles.DocDefaults.RPrDefault.RPr);
self.SetPPrByStyleId(new_ppr, extra_style_id_);
self.SetPPrByStyleId(new_ppr, ppr.PStyle.Val);
{self.}SetPPrByStyleId(new_ppr, extra_style_id_);
{self.}SetPPrByStyleId(new_ppr, ppr.PStyle.Val);
new_ppr.Copy(ppr);
ppr.Copy(new_ppr);
end;
function TSPdfParagraphRange.SetPPrByStyleId(var ppr: PPr; style_id: string);
begin
styles := class(TSPdfSingletonKit).GetComponent(docx_components_, "styles_adapter");
styles := docx_components_ware_.GetStylesAdapter();
style := styles.StyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
self.SetPPrByStyleId(ppr, based_on);
{self.}SetPPrByStyleId(ppr, based_on);
ppr.Copy(style.PPr);
ppr.RPr.Copy(style.RPr);
end
@ -311,39 +321,39 @@ begin
new_rpr := new RPr();
style_id := rpr.RStyle.Val;
new_rpr.Copy(ppr.RPr);
self.SetRPrByStyleId(new_rpr, style_id);
{self.}SetRPrByStyleId(new_rpr, style_id);
new_rpr.Copy(rpr);
rpr.Copy(new_rpr);
end;
function TSPdfParagraphRange.SetRPrByStyleId(var rpr: RPr; style_id: string);
begin
styles := class(TSPdfSingletonKit).GetComponent(docx_components_, "styles_adapter");
styles := docx_components_ware_.GetStylesAdapter();
style := styles.StyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
self.SetRPrByStyleId(rpr, based_on);
{self.}SetRPrByStyleId(rpr, based_on);
rpr.Copy(style.RPr);
end
end;
function TSPdfParagraphRange.SetLvlText();
begin
numbering_ware := class(TSPdfSingletonKit).GetComponent(docx_components_, "numbering_ware");
numbering_ware := docx_components_ware_.GetNumberingWare();
if not ifObj(numbering_ware) then return;
[lvl_text, lvl] := numbering_ware.GetNumberLvl(paragraph_.PPr);
if lvl_text = "" and ifnil(lvl) then return;
self.SetRPr(lvl.RPr, paragraph_.PPr);
{self.}SetRPr(lvl.RPr, paragraph_.PPr);
rpr := new RPrUnitDecorator(lvl.RPr);
self.SplitTextToTextRange(lvl_text, rpr);
{self.}SplitTextToTextRange(lvl_text, rpr);
end;
function TSPdfParagraphRange.ResetCoordinates(ppr);
function TSPdfParagraphRange.ResetCoordinates(ppr: PPr);
begin
// 根据段落的间距确定新的坐标
sz := ppr.RPr.SzCs.Val ? ppr.RPr.SzCs.Val : ppr.RPr.Sz.Val ? ppr.RPr.Sz.Val : docx_to_pdf_.Font.GetDefaultSz();
self.StartX += ppr.Ind.LeftChars ? ppr.Ind.LeftChars * sz : ppr.Ind.Left;
self.Width -= ppr.Ind.LeftChars ? ppr.Ind.LeftChars * sz : ppr.Ind.Left;
self.Width -= ppr.Ind.RightChars ? ppr.Ind.RightChars * sz : ppr.Ind.Right;
{self.}StartX += ppr.Ind.LeftChars ? ppr.Ind.LeftChars * sz : ppr.Ind.Left;
{self.}Width -= ppr.Ind.LeftChars ? ppr.Ind.LeftChars * sz : ppr.Ind.Left;
{self.}Width -= ppr.Ind.RightChars ? ppr.Ind.RightChars * sz : ppr.Ind.Right;
end;

View File

@ -16,12 +16,12 @@ private
function SetTcPrByStyleId(var tc_pr: TcPr; style_id: string);
private
docx_to_pdf_: TSDocxToPdf;
docx_components_: Components;
sect_ware_: TSSectWare;
table_: Tbl;
range_array_: tableArray;
[weakref]docx_to_pdf_: TSDocxToPdf;
[weakref]docx_components_ware_: Components;
[weakref]sect_ware_: TSSectWare;
[weakref]table_: Tbl;
page_: PdfPage;
[weakref]range_array_: tableArray;
point_: TSPoint;
end;
@ -29,27 +29,28 @@ function TSPdfTableRange.Create(docx_to_pdf: TSDocxToPdf; pg: PdfPage; component
begin
docx_to_pdf_ := docx_to_pdf;
page_ := pg;
docx_components_ := Components;
docx_components_ware_ := Components;
sect_ware_ := sect_ware;
table_ := table;
range_array_ := array();
self.Page := page_;
{self.}Page := page_;
end;
function TSPdfTableRange.Calc();
begin
self.SetTblPr(table_.TblPr);
// return;
{self.}SetTblPr(table_.TblPr);
tbl_pr := new TblPrUnitDecorator(table_.TblPr);
grid_cols := table_.TblGrid.GridCols();
for i:=0 to length(grid_cols)-1 do
grid_cols[i] := new GridColUnitDecorator(grid_cols[i]);
self.ResetCoordinates(tbl_pr, grid_cols);
self.EndX := self.StartX;
self.EndY := self.StartY;
{self.}ResetCoordinates(tbl_pr, grid_cols);
{self.}EndX := {self.}StartX;
{self.}EndY := {self.}StartY;
// 如果是根据内容自适应应该计算并调整grid_cols的值
trs := table_.Trs();
for i,tr in trs do
range_array_[i] := self.ProcessTrData(grid_cols, tr, tbl_pr);
range_array_[i] := {self.}ProcessTrData(grid_cols, tr, tbl_pr);
end;
function TSPdfTableRange.Do();
@ -66,7 +67,7 @@ begin
page_ := docx_to_pdf_.GetNextPage(page_);
if ifnil(page_) then page_ := docx_to_pdf_.AddPage(sect_ware_);
point := docx_to_pdf_.GetCurrentPoint();
self.EndY := point.Y;
{self.}EndY := point.Y;
return true;
end
return false;
@ -74,18 +75,18 @@ end;
function TSPdfTableRange.ProcessTrData(grid_cols: array of GridCol; tr: Tr; tbl_pr: TblPr): array of TSPdfAbstractRange;
begin
self.SetTrPr(tr.TrPr);
{self.}SetTrPr(tr.TrPr);
tr_pr := new TrPrUnitDecorator(tr.TrPr);
height := tr_pr.TrHeight.Val;
tc_x := self.EndX;
tc_y := self.EndY;
tc_x := {self.}EndX;
tc_y := {self.}EndY;
tc_h := height;
max_height := 0;
cell_range_array := array();
tcs := tr.Tcs();
for i,tc in tcs do
begin
cell_range := new TSPdfCellRange(docx_to_pdf_, page_, docx_components_, sect_ware_, tc, tbl_pr);
cell_range := new TSPdfCellRange(docx_to_pdf_, page_, docx_components_ware_, sect_ware_, tc, tbl_pr);
cell_range.StartX := tc_x;
cell_range.StartY := tc_y;
cell_range.Width := grid_cols[i].W;
@ -96,11 +97,11 @@ begin
cell_range_array[length(cell_range_array)] := cell_range;
end
if tc_h > max_height then max_height := tc_h;
surplus := max_height - (self.EndY - sect_ware_.SectPr.PgMar.Bottom);
surplus := max_height - ({self.}EndY - sect_ware_.SectPr.PgMar.Bottom);
for _,range in cell_range_array do
begin
range.AlignHeight(surplus);
self.EndY := range.EndY; // 理论上每个range.EndY都是一个值
range.AlignHeight(max_height, surplus);
{self.}EndY := range.EndY; // 理论上每个range.EndY都是一个值
page_ := range.GetLastPage();
end
return cell_range_array;
@ -111,37 +112,37 @@ begin
total_width := 0;
for _,grid_col in grid_cols do
total_width += grid_col.W;
diff := total_width - self.Width;
diff := total_width - {self.}Width;
case tbl_pr.jc.Val of
"center":
begin
offset := diff/2;
self.StartX -= offset;
{self.}StartX -= offset;
end
"right":
begin
self.StartX -= diff;
{self.}StartX -= diff;
end
end;
self.Width := total_width;
{self.}Width := total_width;
end;
function TSPdfTableRange.SetTblPr(var tbl_pr: TblPr);
begin
new_tbl_pr := new TblPr();
self.SetTblPrByStyleId(new_tbl_pr, tbl_pr.TblStyle.Val);
{self.}SetTblPrByStyleId(new_tbl_pr, tbl_pr.TblStyle.Val);
new_tbl_pr.Copy(tbl_pr);
tbl_pr.Copy(new_tbl_pr);
end;
function TSPdfTableRange.SetTblPrByStyleId(var tbl_pr: TblPr; style_id: string);
begin
styles := class(TSPdfSingletonKit).GetComponent(docx_components_, "styles_adapter");
styles := docx_components_ware_.GetStylesAdapter();
style := styles.StyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
self.SetTblPrByStyleId(tbl_pr, based_on);
{self.}SetTblPrByStyleId(tbl_pr, based_on);
tbl_pr.Copy(style.TblPr);
end
end;
@ -149,19 +150,19 @@ end;
function TSPdfTableRange.SetTrPr(var tr_pr: TrPr);
begin
new_tr_pr := new TrPr();
self.SetTrPrByStyleId(new_tr_pr, table_.TblPr.TblStyle.Val);
{self.}SetTrPrByStyleId(new_tr_pr, table_.TblPr.TblStyle.Val);
new_tr_pr.Copy(tr_pr);
tr_pr.Copy(new_tr_pr);
end;
function TSPdfTableRange.SetTrPrByStyleId(var tr_pr: TrPr; style_id: string);
begin
styles := class(TSPdfSingletonKit).GetComponent(docx_components_, "styles_adapter");
styles := docx_components_ware_.GetStylesAdapter();
style := styles.StyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
self.SetTrPrByStyleId(tr_pr, based_on);
{self.}SetTrPrByStyleId(tr_pr, based_on);
tr_pr.Copy(style.TrPr);
end
end;
@ -169,19 +170,19 @@ end;
function TSPdfTableRange.SetTcPr(var tc_pr: TcPr);
begin
new_tc_pr := new TcPr();
self.SetTcPrByStyleId(new_tc_pr, table_.TblPr.TblStyle.Val);
{self.}SetTcPrByStyleId(new_tc_pr, table_.TblPr.TblStyle.Val);
new_tc_pr.Copy(tc_pr);
tc_pr.Copy(new_tc_pr);
end;
function TSPdfTableRange.SetTcPrByStyleId(var tc_pr: TcPr; style_id: string);
begin
styles := class(TSPdfSingletonKit).GetComponent(docx_components_, "styles_adapter");
styles := docx_components_ware_.GetStylesAdapter();
style := styles.StyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
self.SetTcPrByStyleId(tc_pr, based_on);
{self.}SetTcPrByStyleId(tc_pr, based_on);
tc_pr.Copy(style.TcPr);
end
end;

View File

@ -0,0 +1,9 @@
type TSPdfTocRange = class(TSPdfBasicRange)
public
function Create();
end;
function TSPdfTocRange.Create();
begin
end;

View File

@ -16,12 +16,12 @@ end;
function TSPdfBasicRange.Create();
begin
self.StartX := 0;
self.StartY := 0;
self.EndX := 0;
self.EndY := 0;
self.Width := 0;
self.FixedHeight := 0;
self.DynamicHeight := 0;
self.Page := nil;
{self.}StartX := 0;
{self.}StartY := 0;
{self.}EndX := 0;
{self.}EndY := 0;
{self.}Width := 0;
{self.}FixedHeight := 0;
{self.}DynamicHeight := 0;
{self.}Page := nil;
end;

View File

@ -10,19 +10,19 @@ end;
function TSPdfImageRange.Create();
begin
class(TSPdfBasicRange).Create();
self.Image := nil;
{self.}Image := nil;
end;
function TSPdfImageRange.Do();
begin
// println("image = {}, x = {}, y = {}, w = {}, h = {}", self.image, self.endx, self.endy, self.width, self.DynamicHeight);
self.Page.DrawImage(self.Image, self.EndX, self.EndY, self.Width, self.DynamicHeight);
// println("image = {}, x = {}, y = {}, w = {}, h = {}", {self.}image, {self.}endx, {self.}endy, {self.}width, {self.}DynamicHeight);
{self.}Page.DrawImage({self.}Image, {self.}EndX, {self.}EndY, {self.}Width, {self.}DynamicHeight);
if sysparams["_PDF_IMAGE_DEBUG_"] then
begin
self.Page.SetLineWidth(0.1);
self.Page.SetRGBStroke(0.8, 0.8, 0);
self.Page.Rectangle(self.EndX, self.EndY, self.Width, self.DynamicHeight);
self.Page.Stroke();
{self.}Page.SetLineWidth(0.1);
{self.}Page.SetRGBStroke(0.8, 0.8, 0);
{self.}Page.Rectangle({self.}EndX, {self.}EndY, {self.}Width, {self.}DynamicHeight);
{self.}Page.Stroke();
end
end;

View File

@ -12,11 +12,11 @@ end;
function TSPdfRectangleRange.Do();override;
begin
// self.Page.SetRGBStroke(1.0, 0.0, 0.0);
// println("page = {}, endx = {}, endy = {}, DynamicHeight = {}, Width = {}", self.Page, self.EndX, self.EndY, self.DynamicHeight, self.Width);
self.Page.SetGrayStroke(0.5);
self.Page.SetLineWidth(0.5);
self.Page.Rectangle(self.EndX, self.EndY - self.DynamicHeight, self.Width, self.DynamicHeight);
self.Page.Stroke();
self.Page.SetGrayStroke(0);
// {self.}Page.SetRGBStroke(1.0, 0.0, 0.0);
// println("page = {}, endx = {}, endy = {}, DynamicHeight = {}, Width = {}", {self.}Page, {self.}EndX, {self.}EndY, {self.}DynamicHeight, {self.}Width);
{self.}Page.SetGrayStroke(0.5);
{self.}Page.SetLineWidth(0.5);
{self.}Page.Rectangle({self.}EndX, {self.}EndY - {self.}DynamicHeight, {self.}Width, {self.}DynamicHeight);
{self.}Page.Stroke();
{self.}Page.SetGrayStroke(0);
end;

View File

@ -13,31 +13,31 @@ end;
function TSPdfTextRange.Create();
begin
class(TSPdfBasicRange).Create();
self.RPr := nil;
self.Text := "";
self.Font := nil;
{self.}RPr := nil;
{self.}Text := "";
{self.}Font := nil;
end;
function TSPdfTextRange.Do();
begin
// println("text = {}, endx = {}, endy = {}, width = {}, page = {}", ansiToUtf8(text), endx, endy, width, page);
[r, g, b] := array(0, 0, 0);
if self.RPr.Color.Val then [r, g, b] := TSColorToolKit.HexToRGB(self.RPr.Color.Val);
self.Page.SetRGBFill(r / 255, g / 255, b / 255);
self.Page.SetFontAndSize(self.Font, self.RPr.Sz.Val);
self.Page.BeginText();
self.Page.TextOut(self.EndX, self.EndY, self.Text);
self.Page.EndText();
self.Page.SetRGBFill(0, 0, 0);
if {self.}RPr.Color.Val then [r, g, b] := TSColorToolKit.HexToRGB({self.}RPr.Color.Val);
{self.}Page.SetRGBFill(r / 255, g / 255, b / 255);
{self.}Page.SetFontAndSize({self.}Font, {self.}RPr.Sz.Val);
{self.}Page.BeginText();
{self.}Page.TextOut({self.}EndX, {self.}EndY, {self.}Text);
{self.}Page.EndText();
{self.}Page.SetRGBFill(0, 0, 0);
if sysparams["_PDF_TEXT_DEBUG_"] then
begin
self.Page.SetLineWidth(0.1);
self.Page.SetRGBStroke(1.0, 0.5, 0.0);
self.Page.MoveTo(0, self.EndY);
self.Page.LineTo(600, self.EndY);
self.Page.Stroke();
{self.}Page.SetLineWidth(0.1);
{self.}Page.SetRGBStroke(1.0, 0.5, 0.0);
{self.}Page.MoveTo(0, {self.}EndY);
{self.}Page.LineTo(600, {self.}EndY);
{self.}Page.Stroke();
end
end;

View File

@ -1,65 +0,0 @@
type TSPdfSingletonKit = class
public
function Create();
class function Release(components: Components);
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.Release(components: Components);
begin
singleton_.hash_[components] := nil;
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;

View File

@ -1,8 +1,8 @@
type TSPoint = class
function Create();
begin
self.X := 0;
self.Y := 0;
{self.}X := 0;
{self.}Y := 0;
end
X: real;
Y: real;

View File

@ -0,0 +1,52 @@
type TSDocxComponentsWare = class(Components)
public
function Create();
function GetStyles(): Styles;
function GetStylesAdapter(): StylesAdapter;
function GetDocumentRelsAdapter(): RelationShipsAdapter;
function GetNumberingWare(): TSNumberingWare;
private
styles_deserialize_flag_;
styles_adapter_;
document_rels_adapter_;
numbering_ware_;
end;
function TSDocxComponentsWare.Create();
begin
class(Components).Create();
end;
function TSDocxComponentsWare.GetStyles(): Styles;
begin
if styles_deserialize_flag_ then return {self.}Styles;
{self.}Styles.Deserialize();
styles_deserialize_flag_ := true;
return {self.}Styles;
end;
function TSDocxComponentsWare.GetStylesAdapter(): StylesAdapter;
begin
if styles_adapter_ then return styles_adapter_;
styles_adapter_ := new StylesAdapter({self.}GetStyles());
return styles_adapter_;
end;
function TSDocxComponentsWare.GetDocumentRelsAdapter(): RelationShipsAdapter;
begin
if document_rels_adapter_ then return document_rels_adapter_;
{self.}DocumentRels.Deserialize();
document_rels_adapter_ := new RelationShipsAdapter({self.}DocumentRels);
return document_rels_adapter_;
end;
function TSDocxComponentsWare.GetNumberingWare(): TSNumberingWare;
begin
if numbering_ware_ then return numbering_ware_;
if not {self.}Numbering then return nil;
{self.}Numbering.Deserialize();
numbering_ware_ := new TSNumberingWare({self.}Numbering);
return numbering_ware_;
end;

View File

@ -65,7 +65,7 @@ end;
function TSFontWare.GetFont(name: string; bold: boolean; italic: boolean);
begin
return use_built_in_font_ ? self.GetBuiltInFont(name, bold, italic) : self.GetExternalFont(name, bold, italic);
return use_built_in_font_ ? {self.}GetBuiltInFont(name, bold, italic) : {self.}GetExternalFont(name, bold, italic);
end;
function TSFontWare.GetBuiltInFont(name: string; bold: boolean; italic: boolean);

View File

@ -13,19 +13,19 @@ end;
function TSSectWare.Create();
begin
self.Elements := array();
self.SectPr := nil;
self.BaseSize := 0;
{self.}Elements := array();
{self.}SectPr := nil;
{self.}BaseSize := 0;
end;
function TSSectWare.Do();
begin
if ifObj(self.SectPr) then
self.BaseSize := round(self.SectPr.DocGrid.LinePitch * 0.75);
// println("LinePitch = {}, Type = {}", self.SectPr.DocGrid.LinePitch, self.SectPr.DocGrid.Type);
// println("Width = {}, Height = {}", self.SectPr.PgSz.W, self.SectPr.PgSz.H);
if ifObj({self.}SectPr) then
{self.}BaseSize := round({self.}SectPr.DocGrid.LinePitch * 0.75);
// println("LinePitch = {}, Type = {}", {self.}SectPr.DocGrid.LinePitch, {self.}SectPr.DocGrid.Type);
// println("Width = {}, Height = {}", {self.}SectPr.PgSz.W, {self.}SectPr.PgSz.H);
// println("Top = {}, Right = {}, Bottom = {}, Left = {}, Header = {}, Footer = {}\n",
// self.SectPr.PgMar.Top, self.SectPr.PgMar.Right, self.SectPr.PgMar.Bottom, self.SectPr.PgMar.Left, self.SectPr.PgMar.Header, self.SectPr.PgMar.Footer);
// {self.}SectPr.PgMar.Top, {self.}SectPr.PgMar.Right, {self.}SectPr.PgMar.Bottom, {self.}SectPr.PgMar.Left, {self.}SectPr.PgMar.Header, {self.}SectPr.PgMar.Footer);
end;
function TSSectWare.AddElement(element: tslobj);