Skip to content

Commit 76eea08

Browse files
committed
Fix table row page breaking
1 parent 970dfd9 commit 76eea08

7 files changed

Lines changed: 37 additions & 63 deletions

File tree

src/PeachPDF/Html/Core/Dom/CssBox.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,7 @@ public virtual bool IsFixed
198198

199199
public virtual bool IsTableRowGroupBox => Display is CssConstants.TableRowGroup or CssConstants.TableHeaderGroup or CssConstants.TableFooterGroup;
200200

201-
/// <summary>
202-
/// Get the href link of the box (by default get "href" attribute)
203-
/// </summary>
204-
public virtual string HrefLink => GetAttribute(HtmlConstants.Href);
201+
public virtual bool IsTableCell => Display is CssConstants.TableCell;
205202

206203
/// <summary>
207204
/// Gets the containing block-box of this box. (The nearest parent box with display=block)
@@ -1225,22 +1222,21 @@ protected double MarginTopCollapse(CssBox? prevSibling)
12251222

12261223
public bool BreakPage()
12271224
{
1228-
var container = this.HtmlContainer;
1225+
var container = HtmlContainer;
12291226

1230-
if (this.Size.Height >= container!.PageSize.Height)
1227+
if (Size.Height >= container!.PageSize.Height)
12311228
return false;
12321229

1233-
var remTop = (this.Location.Y - container.MarginTop) % container.PageSize.Height;
1234-
var remBottom = (this.ActualBottom - container.MarginTop) % container.PageSize.Height;
1230+
var remTop = (Location.Y - container.MarginTop) % container.PageSize.Height;
1231+
var remBottom = (ActualBottom - container.MarginTop) % container.PageSize.Height;
12351232

1236-
if (remTop > remBottom)
1237-
{
1238-
var diff = container.PageSize.Height - remTop;
1239-
this.Location = new RPoint(this.Location.X, this.Location.Y + diff + 1);
1240-
return true;
1241-
}
1233+
if (!(remTop > remBottom)) return false;
1234+
1235+
var diff = container.PageSize.Height - remTop;
1236+
Location = Location with { Y = Location.Y + diff + 1 };
1237+
1238+
return true;
12421239

1243-
return false;
12441240
}
12451241

12461242
/// <summary>

src/PeachPDF/Html/Core/Dom/CssBoxFrame.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ public CssBoxFrame(CssBox? parent, HtmlTag tag)
5454
/// </summary>
5555
public override bool IsClickable => true;
5656

57-
/// <summary>
58-
/// Get the href link of the box (by default get "href" attribute)
59-
/// </summary>
60-
public override string HrefLink => GetAttribute("src");
61-
6257
#region Private methods
6358

6459
/// <summary>

src/PeachPDF/Html/Core/Dom/CssLayoutEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ private static async ValueTask FlowBox(RGraphics g, CssBox blockBox, CssBox box,
611611
word.Left = coordinates.CurrentX;
612612
word.Top = coordinates.CurrentY;
613613

614-
if (!box.IsFixed)
614+
if (box is { IsFixed: false, IsTableCell: false })
615615
{
616616
word.BreakPage();
617617
}

src/PeachPDF/Html/Core/Dom/CssLayoutEngineTable.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

13-
using System;
14-
using System.Collections.Generic;
15-
using System.ComponentModel.DataAnnotations;
16-
using System.Linq;
17-
using System.Threading.Tasks;
1813
using PeachPDF.Html.Adapters;
1914
using PeachPDF.Html.Adapters.Entities;
2015
using PeachPDF.Html.Core.Entities;
2116
using PeachPDF.Html.Core.Parse;
2217
using PeachPDF.Html.Core.Utils;
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Linq;
21+
using System.Threading.Tasks;
2322

2423
namespace PeachPDF.Html.Core.Dom
2524
{
@@ -28,18 +27,11 @@ namespace PeachPDF.Html.Core.Dom
2827
/// </summary>
2928
internal sealed class CssLayoutEngineTable
3029
{
31-
#region Fields and Consts
32-
3330
/// <summary>
3431
/// the main box of the table
3532
/// </summary>
3633
private readonly CssBox _tableBox;
3734

38-
/// <summary>
39-
///
40-
/// </summary>
41-
private CssBox? _caption;
42-
4335
private CssBox? _headerBox;
4436

4537
private CssBox? _footerBox;
@@ -67,9 +59,6 @@ internal sealed class CssLayoutEngineTable
6759

6860
private double[]? _columnMinWidths;
6961

70-
#endregion
71-
72-
7362
/// <summary>
7463
/// Init.
7564
/// </summary>
@@ -194,7 +183,6 @@ private void AssignBoxKinds()
194183
switch (box.Display)
195184
{
196185
case CssConstants.TableCaption:
197-
_caption = box;
198186
break;
199187
case CssConstants.TableRow:
200188
_bodyRows.Add(box);
@@ -626,7 +614,7 @@ private async ValueTask LayoutCells(RGraphics g)
626614
var maxBottom = 0d;
627615
var currentRow = 0;
628616

629-
Dictionary<int, List<CssBox>> rowSpannedBoxes = new Dictionary<int, List<CssBox>>();
617+
Dictionary<int, List<CssBox>> rowSpannedBoxes = new();
630618

631619
for (var i = 0; i < _allRows.Count; i++)
632620
{
@@ -705,7 +693,7 @@ private async ValueTask LayoutCells(RGraphics g)
705693
}
706694

707695
// If one cell crosses page borders then don't need to check other cells in the row
708-
if (_tableBox.PageBreakInside != CssConstants.Avoid) continue;
696+
if (_tableBox.PageBreakInside is CssConstants.Avoid) continue;
709697

710698
breakPage = cell.BreakPage();
711699
if (!breakPage) continue;
@@ -823,13 +811,10 @@ private static int GetRowSpan(CssBox b)
823811
/// <param name="g">Device to use</param>
824812
private static async ValueTask MeasureWords(CssBox box, RGraphics g)
825813
{
826-
if (box != null)
814+
foreach (var childBox in box.Boxes)
827815
{
828-
foreach (var childBox in box.Boxes)
829-
{
830-
await childBox.MeasureWordsSize(g);
831-
await MeasureWords(childBox, g);
832-
}
816+
await childBox.MeasureWordsSize(g);
817+
await MeasureWords(childBox, g);
833818
}
834819
}
835820

@@ -840,7 +825,7 @@ private static async ValueTask MeasureWords(CssBox box, RGraphics g)
840825
/// <returns></returns>
841826
private bool CanReduceWidth()
842827
{
843-
for (int i = 0; i < _columnWidths!.Length; i++)
828+
for (var i = 0; i < _columnWidths!.Length; i++)
844829
{
845830
if (CanReduceWidth(i))
846831
{
@@ -963,7 +948,7 @@ private double GetWidthSum()
963948
{
964949
double f = 0f;
965950

966-
foreach (double t in _columnWidths!)
951+
foreach (var t in _columnWidths!)
967952
{
968953
if (double.IsNaN(t))
969954
throw new Exception("CssTable Algorithm error: There's a NaN in column widths");
@@ -986,7 +971,7 @@ private double GetWidthSum()
986971
/// <param name="b"></param>
987972
private static int GetSpan(CssBox b)
988973
{
989-
double f = CssValueParser.ParseNumber(b.GetAttribute("span"), 1);
974+
var f = CssValueParser.ParseNumber(b.GetAttribute("span"), 1);
990975

991976
return Math.Max(1, Convert.ToInt32(f));
992977
}

src/PeachPDF/Html/Core/Dom/CssRect.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,18 @@ public override string ToString()
177177

178178
public bool BreakPage()
179179
{
180-
var container = this.OwnerBox.HtmlContainer;
180+
var container = OwnerBox.HtmlContainer;
181181

182-
if (this.Height >= container!.PageSize.Height)
182+
if (Height >= container!.PageSize.Height)
183183
return false;
184184

185-
var remTop = (this.Top - container.MarginTop) % container.PageSize.Height;
186-
var remBottom = (this.Bottom - container.MarginTop) % container.PageSize.Height;
185+
var remTop = (Top - container.MarginTop) % container.PageSize.Height;
186+
var remBottom = (Bottom - container.MarginTop) % container.PageSize.Height;
187187

188-
if (remTop > remBottom)
189-
{
190-
this.Top += container.PageSize.Height - remTop + 1;
191-
return true;
192-
}
188+
if (!(remTop > remBottom)) return false;
189+
Top += container.PageSize.Height - remTop + 1;
190+
return true;
193191

194-
return false;
195192
}
196193
}
197194
}

src/PeachPDF/Html/Core/Handlers/BordersDrawHandler.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,20 @@ private static void DrawBorder(Border border, CssBox box, RGraphics g, RRect rec
122122
{
123123
// solid/dotted/dashed border draw as simple line
124124
var pen = GetPen(g, style, color, GetWidth(border, box));
125+
125126
switch (border)
126127
{
127128
case Border.Top:
128-
g.DrawLine(pen, Math.Ceiling(rect.Left), rect.Top + box.ActualBorderTopWidth / 2, rect.Right - 1, rect.Top + box.ActualBorderTopWidth / 2);
129+
g.DrawLine(pen, rect.Left, rect.Top + box.ActualBorderTopWidth / 2, rect.Right, rect.Top + box.ActualBorderTopWidth / 2);
129130
break;
130131
case Border.Left:
131-
g.DrawLine(pen, rect.Left + box.ActualBorderLeftWidth / 2, Math.Ceiling(rect.Top), rect.Left + box.ActualBorderLeftWidth / 2, Math.Floor(rect.Bottom));
132+
g.DrawLine(pen, rect.Left + box.ActualBorderLeftWidth / 2, rect.Top, rect.Left + box.ActualBorderLeftWidth / 2, rect.Bottom);
132133
break;
133134
case Border.Bottom:
134-
g.DrawLine(pen, Math.Ceiling(rect.Left), rect.Bottom - box.ActualBorderBottomWidth / 2, rect.Right - 1, rect.Bottom - box.ActualBorderBottomWidth / 2);
135+
g.DrawLine(pen, rect.Left, rect.Bottom - box.ActualBorderBottomWidth / 2, rect.Right, rect.Bottom - box.ActualBorderBottomWidth / 2);
135136
break;
136137
case Border.Right:
137-
g.DrawLine(pen, rect.Right - box.ActualBorderRightWidth / 2, Math.Ceiling(rect.Top), rect.Right - box.ActualBorderRightWidth / 2, Math.Floor(rect.Bottom));
138+
g.DrawLine(pen, rect.Right - box.ActualBorderRightWidth / 2, rect.Top, rect.Right - box.ActualBorderRightWidth / 2, rect.Bottom);
138139
break;
139140
}
140141
}

src/PeachPDF/PeachPDF.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFrameworks>net8.0</TargetFrameworks>
44
<AssemblyName>PeachPDF</AssemblyName>
55
<PackageId>PeachPDF</PackageId>
6-
<PackageVersion>0.7.20</PackageVersion>
6+
<PackageVersion>0.7.21</PackageVersion>
77
<IncludeSymbols>true</IncludeSymbols>
88
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
99
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>

0 commit comments

Comments
 (0)