Skip to content

Commit 3914ffc

Browse files
committed
Fix multi-page PDF text/image invisibility, delete CssBox.Paint/PaintImp
Found while giving FragmentPainter a page-origin translate so HtmlContainerInt.PerformPaint(RGraphics)'s multi-fragmentainer fallback could stop depending on CssBox.Paint: FragmentPainter.PaintFragmentContent painted line backgrounds/borders from the fragment tree's already page-local rects (FragmentEmitter subtracts each band's top at build time), but painted the actual text via CssBox.PaintWords, which reads CssRect.Rectangle straight off the live box tree - still absolute document-Y - offset only by ScrollOffset (always zero for PDF generation). Confirmed via a raw PDF content-stream inspection: every page after the first had zero text-draw (Tj) operators, since a fresh per-page XGraphics's origin is that page's own band top, not the document's. No existing test caught this because none checked page content beyond page count. The same absolute-vs-fragment-local mixup existed in this session's own image/frame content painters and in the overflow-clip helper. FragmentPainter now distinguishes the two coordinate spaces explicitly: FragmentLocalOffset for geometry already sourced from the fragment tree, LiveTreeOffset (and RenderUtils.ClipGraphicsByOverflow's new extraOffset parameter) for geometry read straight off the live CssBox tree, which additionally undoes the current fragmentainer's band top. Added a regression test that asserts every page of a genuinely multi-page PDF has real text operators, not just a page count. With that fixed, HtmlContainerInt.PerformPaint(RGraphics)'s multi- fragmentainer branch now paints every fragmentainer through FragmentPainter (translated back to its real document-Y band top), making CssBox.Paint/PaintImp and their three subclass overrides genuinely unreachable - confirmed via grep and deleted, along with CssBox's now-dead IsRectVisible helper. Full regression suite (35 image-diff tests) and PDF suite (16 tests, including the new one) pass; whole solution builds clean across all TFMs.
1 parent b7fc8c9 commit 3914ffc

11 files changed

Lines changed: 175 additions & 260 deletions

File tree

‎Source/HtmlRenderer/Core/Dom/CssBox.cs‎

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -521,59 +521,7 @@ public void PerformLayout(RGraphics g)
521521
}
522522

523523
/// <summary>
524-
/// Paints the fragment
525-
/// </summary>
526-
/// <param name="g">Device context to use</param>
527-
public void Paint(RGraphics g)
528-
{
529-
try
530-
{
531-
if (Display != CssConstants.None && Visibility == CssConstants.Visible)
532-
{
533-
// use initial clip to draw blocks with Position = fixed. I.e. ignrore page margins
534-
if (this.Position == CssConstants.Fixed)
535-
{
536-
g.SuspendClipping();
537-
}
538-
539-
// don't call paint if the rectangle of the box is not in visible rectangle
540-
bool visible = Rectangles.Count == 0;
541-
if (!visible)
542-
{
543-
var clip = g.GetClip();
544-
var rect = ContainingBlock.ClientRectangle;
545-
rect.X -= 2;
546-
rect.Width += 2;
547-
if (!IsFixed)
548-
{
549-
//rect.Offset(new RPoint(-HtmlContainer.Location.X, -HtmlContainer.Location.Y));
550-
rect.Offset(HtmlContainer.ScrollOffset);
551-
}
552-
clip.Intersect(rect);
553-
554-
if (clip != RRect.Empty)
555-
visible = true;
556-
}
557-
558-
if (visible)
559-
PaintImp(g);
560-
561-
// Restore clips
562-
if (this.Position == CssConstants.Fixed)
563-
{
564-
g.ResumeClipping();
565-
}
566-
567-
}
568-
}
569-
catch (Exception ex)
570-
{
571-
HtmlContainer.ReportError(HtmlRenderErrorType.Paint, "Exception in box paint", ex);
572-
}
573-
}
574-
575-
/// <summary>
576-
/// Set this box in
524+
/// Set this box in
577525
/// </summary>
578526
/// <param name="before"></param>
579527
public void SetBeforeBox(CssBox before)
@@ -1388,97 +1336,6 @@ internal void OffsetTop(double amount)
13881336
Location = new RPoint(Location.X, Location.Y + amount);
13891337
}
13901338

1391-
/// <summary>
1392-
/// Paints the fragment
1393-
/// </summary>
1394-
/// <param name="g">the device to draw to</param>
1395-
protected virtual void PaintImp(RGraphics g)
1396-
{
1397-
if (Display != CssConstants.None && (Display != CssConstants.TableCell || EmptyCells != CssConstants.Hide || !IsSpaceOrEmpty))
1398-
{
1399-
var clipped = RenderUtils.ClipGraphicsByOverflow(g, this);
1400-
1401-
var areas = Rectangles.Count == 0 ? new List<RRect>(new[] { Bounds }) : new List<RRect>(Rectangles.Values);
1402-
var clip = g.GetClip();
1403-
RRect[] rects = areas.ToArray();
1404-
RPoint offset = RPoint.Empty;
1405-
if (!IsFixed)
1406-
{
1407-
offset = HtmlContainer.ScrollOffset;
1408-
}
1409-
1410-
for (int i = 0; i < rects.Length; i++)
1411-
{
1412-
var actualRect = rects[i];
1413-
actualRect.Offset(offset);
1414-
1415-
if (IsRectVisible(actualRect, clip))
1416-
{
1417-
PaintBackground(g, actualRect, i == 0, i == rects.Length - 1);
1418-
BordersDrawHandler.DrawBoxBorders(g, this, actualRect, i == 0, i == rects.Length - 1);
1419-
}
1420-
}
1421-
1422-
PaintWords(g, offset);
1423-
1424-
for (int i = 0; i < rects.Length; i++)
1425-
{
1426-
var actualRect = rects[i];
1427-
actualRect.Offset(offset);
1428-
1429-
if (IsRectVisible(actualRect, clip))
1430-
{
1431-
PaintDecoration(g, actualRect, i == 0, i == rects.Length - 1);
1432-
}
1433-
}
1434-
1435-
// split paint to handle z-order
1436-
foreach (CssBox b in Boxes)
1437-
{
1438-
if (b.Position != CssConstants.Absolute && !b.IsFixed)
1439-
b.Paint(g);
1440-
}
1441-
foreach (CssBox b in Boxes)
1442-
{
1443-
if (b.Position == CssConstants.Absolute)
1444-
b.Paint(g);
1445-
}
1446-
foreach (CssBox b in Boxes)
1447-
{
1448-
if (b.IsFixed)
1449-
b.Paint(g);
1450-
}
1451-
1452-
if (clipped)
1453-
g.PopClip();
1454-
1455-
if (_listItemBox != null)
1456-
{
1457-
_listItemBox.Paint(g);
1458-
}
1459-
1460-
if (RepeatedHeaderRows != null)
1461-
{
1462-
foreach (var repeatedRow in RepeatedHeaderRows)
1463-
{
1464-
repeatedRow.Paint(g);
1465-
}
1466-
}
1467-
}
1468-
}
1469-
1470-
private bool IsRectVisible(RRect rect, RRect clip)
1471-
{
1472-
rect.X -= 2;
1473-
rect.Width += 2;
1474-
clip.Intersect(rect);
1475-
1476-
if (clip != RRect.Empty)
1477-
return true;
1478-
1479-
return false;
1480-
}
1481-
14821339
/// <summary>
14831340
/// Paints the background of the box
14841341
/// </summary>

‎Source/HtmlRenderer/Core/Dom/CssBoxFrame.cs‎

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -406,36 +406,11 @@ private void HandlePostApiCall()
406406
HtmlContainer.RequestRefresh(IsLayoutRequired());
407407
}
408408

409-
/// <summary>
410-
/// Paints the fragment
411-
/// </summary>
412-
/// <param name="g">the device to draw to</param>
413-
protected override void PaintImp(RGraphics g)
414-
{
415-
EnsureVideoImageLoadStarted();
416-
417-
var rects = CommonUtils.GetFirstValueOrDefault(Rectangles);
418-
419-
RPoint offset = (HtmlContainer != null && !IsFixed) ? HtmlContainer.ScrollOffset : RPoint.Empty;
420-
rects.Offset(offset);
421-
422-
var clipped = RenderUtils.ClipGraphicsByOverflow(g, this);
423-
424-
PaintBackground(g, rects, true, true);
425-
426-
BordersDrawHandler.DrawBoxBorders(g, this, rects, true, true);
427-
428-
DrawFrameContent(g, offset);
429-
430-
if (clipped)
431-
g.PopClip();
432-
}
433-
434409
/// <summary>
435410
/// Starts loading the video thumbnail if the video API call resolved a thumbnail URL and loading
436411
/// hasn't started already - the same paint-time trigger pattern as <see cref="CssBoxImage"/>, see
437412
/// its <see cref="CssBoxImage.EnsureImageLoadStarted"/> for why this can't move to measure time.
438-
/// Shared by <see cref="PaintImp"/> and <see cref="Paint.Content.FrameFragmentPainter"/>.
413+
/// Called by <see cref="Paint.Content.FrameFragmentPainter"/>.
439414
/// </summary>
440415
internal void EnsureVideoImageLoadStarted()
441416
{
@@ -447,9 +422,8 @@ internal void EnsureVideoImageLoadStarted()
447422
}
448423

449424
/// <summary>
450-
/// Draws the video thumbnail/title/play-button chrome at <paramref name="offset"/> - the part of
451-
/// <see cref="PaintImp"/> specific to this box's own image word, as opposed to the generic
452-
/// background/border painting shared with every other replaced element.
425+
/// Draws the video thumbnail/title/play-button chrome at <paramref name="offset"/>, leaving
426+
/// background/border painting to the caller (<see cref="Paint.Content.FrameFragmentPainter"/>).
453427
/// </summary>
454428
internal void DrawFrameContent(RGraphics g, RPoint offset)
455429
{

‎Source/HtmlRenderer/Core/Dom/CssBoxHr.cs‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,9 @@ protected override void PerformLayoutImp(RGraphics g)
9090
}
9191

9292
/// <summary>
93-
/// Paints the fragment
94-
/// </summary>
95-
/// <param name="g">the device to draw to</param>
96-
protected override void PaintImp(RGraphics g)
97-
{
98-
var offset = (HtmlContainer != null && !IsFixed) ? HtmlContainer.ScrollOffset : RPoint.Empty;
99-
var rect = new RRect(Bounds.X + offset.X, Bounds.Y + offset.Y, Bounds.Width, Bounds.Height);
100-
DrawHrContent(g, rect);
101-
}
102-
103-
/// <summary>
104-
/// Draws the rule itself at <paramref name="rect"/> (already offset for scroll) - the whole of
105-
/// what <see cref="PaintImp"/> does, since an <c>&lt;hr&gt;</c> has no separate background/border
106-
/// step shared with other replaced elements (it draws each border edge itself, not via
107-
/// <see cref="BordersDrawHandler.DrawBoxBorders"/>). Shared by <see cref="PaintImp"/> and
93+
/// Draws the rule itself at <paramref name="rect"/> (already offset) - an <c>&lt;hr&gt;</c> has
94+
/// no separate background/border step shared with other replaced elements (it draws each border
95+
/// edge itself, not via <see cref="BordersDrawHandler.DrawBoxBorders"/>). Called by
10896
/// <see cref="Paint.Content.HrFragmentPainter"/>.
10997
/// </summary>
11098
internal void DrawHrContent(RGraphics g, RRect rect)

‎Source/HtmlRenderer/Core/Dom/CssBoxImage.cs‎

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,39 +66,12 @@ public RImage Image
6666
get { return _imageWord.Image; }
6767
}
6868

69-
/// <summary>
70-
/// Paints the fragment
71-
/// </summary>
72-
/// <param name="g">the device to draw to</param>
73-
protected override void PaintImp(RGraphics g)
74-
{
75-
EnsureImageLoadStarted();
76-
77-
var rect = CommonUtils.GetFirstValueOrDefault(Rectangles);
78-
RPoint offset = RPoint.Empty;
79-
80-
if (!IsFixed)
81-
offset = HtmlContainer.ScrollOffset;
82-
83-
rect.Offset(offset);
84-
85-
var clipped = RenderUtils.ClipGraphicsByOverflow(g, this);
86-
87-
PaintBackground(g, rect, true, true);
88-
BordersDrawHandler.DrawBoxBorders(g, this, rect, true, true);
89-
90-
DrawImageContent(g, offset);
91-
92-
if (clipped)
93-
g.PopClip();
94-
}
95-
9669
/// <summary>
9770
/// Starts loading the image if it hasn't started already. This is the primary load trigger for
9871
/// the common async case (<see cref="HtmlContainerInt.AvoidAsyncImagesLoading"/>/
9972
/// <see cref="HtmlContainerInt.AvoidImagesLateLoading"/> both false) - <see cref="MeasureWordsSize"/>
10073
/// only starts loading when one of those flags is set, so paint is where loading normally begins.
101-
/// Shared by <see cref="PaintImp"/> and <see cref="Paint.Content.ImageFragmentPainter"/>.
74+
/// Called by <see cref="Paint.Content.ImageFragmentPainter"/>.
10275
/// </summary>
10376
internal void EnsureImageLoadStarted()
10477
{
@@ -110,9 +83,8 @@ internal void EnsureImageLoadStarted()
11083
}
11184

11285
/// <summary>
113-
/// Draws the image itself (or its error/loading placeholder) at <paramref name="offset"/> - the
114-
/// part of <see cref="PaintImp"/> specific to this box's own image word, as opposed to the
115-
/// generic background/border painting shared with every other replaced element.
86+
/// Draws the image itself (or its error/loading placeholder) at <paramref name="offset"/>,
87+
/// leaving background/border painting to the caller (<see cref="Paint.Content.ImageFragmentPainter"/>).
11688
/// </summary>
11789
internal void DrawImageContent(RGraphics g, RPoint offset)
11890
{

‎Source/HtmlRenderer/Core/Fragments/Fragment.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ internal sealed record TextFragment(RRect Rect, CssRect Word) : Fragment(Rect);
4848
/// produces one <see cref="BoxFragment"/> per page. <see cref="Lines"/>/<see cref="Words"/>/<see cref="Children"/>
4949
/// mirror what the old live-tree paint walk painted, in the same order: own decoration rects, own words,
5050
/// then stacking-ordered child box fragments. <see cref="MarkerFragment"/> (a list item's marker, if any)
51-
/// is kept separate from <see cref="Children"/> rather than folded in, matching <c>CssBox.PaintImp</c>'s
52-
/// own paint order - the marker paints last, after this fragment's own overflow clip is popped, since a
51+
/// is kept separate from <see cref="Children"/> rather than folded in, matching the old live-tree
52+
/// walk's own paint order - the marker paints last, after this fragment's own overflow clip is popped, since a
5353
/// <c>list-style-position: outside</c> marker can legitimately hang outside the content box's clip.
5454
/// </summary>
5555
internal sealed record BoxFragment(

‎Source/HtmlRenderer/Core/HtmlContainerInt.cs‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -810,23 +810,21 @@ public void PerformPaint(RGraphics g)
810810
g.PushClip(new RRect(MarginLeft, MarginTop, PageSize.Width, PageSize.Height));
811811
}
812812

813-
// The fragment tree has exactly one fragmentainer for every caller of this overload today
814-
// (WinForms/WPF's continuous single-surface rendering, and any other HasRealPageGrid=false
815-
// container - see FragmentEmitter.Finish's no-real-page-grid path) - FragmentPainter is a
816-
// faithful, verified replacement for CssBox.Paint there (see StageE1SmokeTest's pixel-for-
817-
// pixel comparison, and HtmlRenderingRegressionTests staying green under this path).
818-
// A caller with a real, multi-page grid that reaches this overload instead of the
819-
// fragmentainer-aware one (PdfGenerator always uses that one - see PdfGenerator.AddPdfPages)
820-
// falls back to the old live-tree walk, which paints every page's content onto one
821-
// continuous surface exactly as this method always has; splitting that across fragments
822-
// correctly is what the fragmentainer-aware overload below already does properly.
823-
if (FragmentTree != null && FragmentTree.Fragmentainers.Count == 1)
813+
// Every fragmentainer, painted onto this one continuous surface, each translated back to its
814+
// real document-Y band top - exactly what the old live-tree walk (_root.Paint(g), removed
815+
// once this replaced it) did by construction, since box geometry there was always absolute.
816+
// For every caller of this overload today (WinForms/WPF's continuous single-surface
817+
// rendering, any other HasRealPageGrid=false container) there is exactly one fragmentainer
818+
// whose LocalOriginY is already 0, so this loop runs once with a no-op page origin - a direct
819+
// multi-page-grid caller of this overload (bypassing PdfGenerator's real per-fragmentainer
820+
// loop below) is the only case where more than one iteration, or a non-zero origin, happens.
821+
if (FragmentTree != null)
824822
{
825-
new Paint.FragmentPainter(this).Paint(g, FragmentTree.Fragmentainers[0]);
826-
}
827-
else if (_root != null)
828-
{
829-
_root.Paint(g);
823+
foreach (var fragmentainer in FragmentTree.Fragmentainers)
824+
{
825+
var pageOrigin = new RPoint(0, fragmentainer.LocalOriginY);
826+
new Paint.FragmentPainter(this, pageOrigin).Paint(g, fragmentainer);
827+
}
830828
}
831829

832830
g.PopClip();

‎Source/HtmlRenderer/Core/Paint/Content/HrFragmentPainter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private HrFragmentPainter()
2121
public void Paint(FragmentPainter painter, RGraphics g, BoxFragment fragment)
2222
{
2323
var box = (CssBoxHr)fragment.Box;
24-
var offset = box.IsFixed ? RPoint.Empty : painter.Container.ScrollOffset;
24+
var offset = painter.FragmentLocalOffset(box.IsFixed);
2525
var rect = fragment.PrimaryRect;
2626
rect.Offset(offset);
2727
box.DrawHrContent(g, rect);

‎Source/HtmlRenderer/Core/Paint/Content/ReplacedFragmentPainter.cs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ internal abstract class ReplacedFragmentPainter : IFragmentContentPainter
2020
public void Paint(FragmentPainter painter, RGraphics g, BoxFragment fragment)
2121
{
2222
var box = fragment.Box;
23-
var offset = box.IsFixed ? RPoint.Empty : painter.Container.ScrollOffset;
23+
24+
// fragment.PrimaryRect is fragment-local; the image/video word rect DrawContent's
25+
// implementations read is off the live tree (still absolute document-Y) - each needs its own
26+
// offset flavor, see FragmentPainter.FragmentLocalOffset/LiveTreeOffset's doc comments.
2427
var rect = fragment.PrimaryRect;
25-
rect.Offset(offset);
28+
rect.Offset(painter.FragmentLocalOffset(box.IsFixed));
2629

27-
var clipped = RenderUtils.ClipGraphicsByOverflow(g, box);
30+
var clipped = RenderUtils.ClipGraphicsByOverflow(g, box, painter.LiveTreeExtraOffset);
2831

2932
box.PaintBackground(g, rect, true, true);
3033
BordersDrawHandler.DrawBoxBorders(g, box, rect, true, true);
3134

32-
DrawContent(g, fragment, offset);
35+
DrawContent(g, fragment, painter.LiveTreeOffset(box.IsFixed));
3336

3437
if (clipped)
3538
g.PopClip();

0 commit comments

Comments
 (0)