Skip to content

Commit 2da1da5

Browse files
authored
Merge pull request #16 from alfattack/page-break-inside
Page break inside improvement
2 parents e132e92 + 3dc6752 commit 2da1da5

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

Source/Demos/HtmlRenderer.Demo.Common/TestSamples/38. Breaking Pages 4 - Divs.htm renamed to Source/Demos/HtmlRenderer.Demo.Common/TestSamples/37. Breaking Pages 4 - Divs.htm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
This is on page 2
99
</div>
1010

11-
<div style="height: 700px; background-color: red;">
11+
<div style="height: 400px; background-color: red;">
1212
This is on page 3.
1313
</div>
1414

@@ -18,8 +18,11 @@
1818
<div style="height: 500px; background-color: yellow;">
1919
This is also on page 4
2020
</div>
21+
<div style="height: 100px; background-color: brown; page-break-inside: avoid;">
22+
This is also on page 4.
23+
</div>
2124

22-
<div style="height: 400px; background-color: aqua; page-break-inside: avoid;">
25+
<div style="height: 700px; background-color: aqua; page-break-inside: avoid;">
2326
This is on page 5
2427
</div>
2528
</div>
@@ -29,8 +32,7 @@
2932
This is on page 6
3033
</div>
3134

32-
<div style="background-color:yellowgreen;width:90px;height: 1000px;">
33-
</div>
35+
<div style="background-color: yellowgreen; width: 90px; height: 1000px;"></div>
3436
</div>
3537

3638
</body>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<body>
3+
<div style="height: 700px; background-color: indianred;">
4+
This is on page 1
5+
</div>
6+
<div style="background-color: orange; page-break-inside: avoid;">
7+
<div style="background-color: yellowgreen; width: 90px; height: 1000px;"></div>
8+
</div>
9+
</body>
10+
</html>

Source/Demos/HtmlRenderer.Demo.Common/TestSamples/37.Absolute position.htm renamed to Source/Demos/HtmlRenderer.Demo.Common/TestSamples/39. Absolute position.htm

File renamed without changes.

Source/Demos/HtmlRenderer.Demo.Console/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
foreach (var htmlSample in samples)
2525
{
2626
////Just doing one test here. Comment this for all of them.
27-
if (!htmlSample.FullName.Contains("38")) continue;
27+
if (!htmlSample.FullName.Contains("37")) continue;
2828

2929
//await skia.GenerateSampleAsync(htmlSample);
3030
//await svgSkia.GenerateSampleAsync(htmlSample);

Source/HtmlRenderer/Core/Dom/CssBox.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ public virtual void Dispose()
624624
/// <param name="g">Device context to use</param>
625625
protected virtual async Task PerformLayoutImpAsync(RGraphics g)
626626
{
627+
var prevSibling = DomUtils.GetPreviousSibling(this);
627628
if (Display != CssConstants.None)
628629
{
629630
RectanglesReset();
@@ -652,7 +653,6 @@ protected virtual async Task PerformLayoutImpAsync(RGraphics g)
652653

653654
if (Display != CssConstants.TableCell)
654655
{
655-
var prevSibling = DomUtils.GetPreviousSibling(this);
656656
double left;
657657
double top;
658658

@@ -695,17 +695,19 @@ protected virtual async Task PerformLayoutImpAsync(RGraphics g)
695695
: 0);
696696

697697
Location = new RPoint(left, top);
698-
698+
699699
if (this.PageBreakBefore == CssConstants.Always || prevSibling?.PageBreakAfter == CssConstants.Always)
700700
{
701701
this.BreakPage(true);
702702
}
703-
else if (this.PageBreakInside == CssConstants.Avoid
704-
&& ActualHeight + Location.Y > HtmlContainer.PageSize.Height
705-
&& prevSibling != null)
703+
else if (this.PageBreakInside == CssConstants.Avoid && prevSibling != null)
706704
{
707705
// handle page break avoiding.
708-
this.BreakPage(true);
706+
var pageLocationY = Location.Y % HtmlContainer.PageSize.Height;
707+
if (ActualHeight + pageLocationY > HtmlContainer.PageSize.Height)
708+
{
709+
this.BreakPage(true);
710+
}
709711
}
710712

711713
//Start with the assumption this is zero height.
@@ -739,7 +741,6 @@ protected virtual async Task PerformLayoutImpAsync(RGraphics g)
739741
}
740742
else
741743
{
742-
var prevSibling = DomUtils.GetPreviousSibling(this);
743744
if (prevSibling != null)
744745
{
745746
if (Location == RPoint.Empty)

0 commit comments

Comments
 (0)