Skip to content

Commit b7fc8c9

Browse files
committed
Add real per-type fragment content painters (image/hr/frame/marker)
Ports PeachPDF's IFragmentContentPainter/FragmentContentPainters architecture: a stateless painter per replaced/leaf box type, dispatched by FragmentPainter instead of delegating into CssBox.Paint. The actual per-pixel drawing logic stays on the CssBox subclasses (extracted from their PaintImp bodies into internal methods PaintImp itself now also calls, so there is exactly one implementation, not a parallel one) - this keeps the port a faithful re-shaping rather than a rewrite. CssBoxImage/CssBoxFrame gained an explicit EnsureImageLoadStarted/ EnsureVideoImageLoadStarted method: their lazy image-load trigger lives in PaintImp today and is the *primary* load trigger for the common async case (MeasureWordsSize only starts loading when AvoidAsyncImagesLoading/AvoidImagesLateLoading is set), so the new painters must replicate it or async images would never load. List item markers previously painted via a direct box.ListItemBox.Paint(g) call reading the live mutable tree; FragmentEmitter now builds a real BoxFragment for the marker (BoxFragment.MarkerFragment, kept separate from Children to preserve CssBox.PaintImp's paint-after-clip-pop timing, since an outside-position marker can legitimately hang outside the element's own overflow clip) and FragmentPainter paints it from there. CssBox.Paint/PaintImp are NOT deleted: HtmlRenderer.PdfSharp.HtmlContainer still exposes a public single-surface PerformPaint(XGraphics) overload that a caller can reach directly (bypassing PdfGenerator's per-fragmentainer loop) with a real multi-fragmentainer FragmentTree, which the fallback `_root.Paint(g)` branch in HtmlContainerInt.PerformPaint(RGraphics) still serves correctly. Deleting it would require new page-stacking transform logic this session doesn't have a tested replacement for - left as a real, live-verified deviation from the original plan's F3 assumption. Full regression suite (35 image-diff tests) and PDF test suite (15 tests) pass unchanged; all TFMs build clean across the whole solution.
1 parent b056b6c commit b7fc8c9

12 files changed

Lines changed: 256 additions & 35 deletions

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,7 @@ private void HandlePostApiCall()
412412
/// <param name="g">the device to draw to</param>
413413
protected override void PaintImp(RGraphics g)
414414
{
415-
if (_videoImageUrl != null && _imageLoadHandler == null)
416-
{
417-
_imageLoadHandler = new ImageLoadHandler(HtmlContainer, OnLoadImageComplete);
418-
_imageLoadHandler.LoadImage(_videoImageUrl, HtmlTag != null ? HtmlTag.Attributes : null);
419-
}
415+
EnsureVideoImageLoadStarted();
420416

421417
var rects = CommonUtils.GetFirstValueOrDefault(Rectangles);
422418

@@ -429,6 +425,34 @@ protected override void PaintImp(RGraphics g)
429425

430426
BordersDrawHandler.DrawBoxBorders(g, this, rects, true, true);
431427

428+
DrawFrameContent(g, offset);
429+
430+
if (clipped)
431+
g.PopClip();
432+
}
433+
434+
/// <summary>
435+
/// Starts loading the video thumbnail if the video API call resolved a thumbnail URL and loading
436+
/// hasn't started already - the same paint-time trigger pattern as <see cref="CssBoxImage"/>, see
437+
/// 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"/>.
439+
/// </summary>
440+
internal void EnsureVideoImageLoadStarted()
441+
{
442+
if (_videoImageUrl != null && _imageLoadHandler == null)
443+
{
444+
_imageLoadHandler = new ImageLoadHandler(HtmlContainer, OnLoadImageComplete);
445+
_imageLoadHandler.LoadImage(_videoImageUrl, HtmlTag != null ? HtmlTag.Attributes : null);
446+
}
447+
}
448+
449+
/// <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.
453+
/// </summary>
454+
internal void DrawFrameContent(RGraphics g, RPoint offset)
455+
{
432456
var word = Words[0];
433457
var tmpRect = word.Rectangle;
434458
tmpRect.Offset(offset);
@@ -443,9 +467,6 @@ protected override void PaintImp(RGraphics g)
443467
DrawTitle(g, rect);
444468

445469
DrawPlay(g, rect);
446-
447-
if (clipped)
448-
g.PopClip();
449470
}
450471

451472
/// <summary>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,18 @@ protected override void PaintImp(RGraphics g)
9797
{
9898
var offset = (HtmlContainer != null && !IsFixed) ? HtmlContainer.ScrollOffset : RPoint.Empty;
9999
var rect = new RRect(Bounds.X + offset.X, Bounds.Y + offset.Y, Bounds.Width, Bounds.Height);
100+
DrawHrContent(g, rect);
101+
}
100102

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
108+
/// <see cref="Paint.Content.HrFragmentPainter"/>.
109+
/// </summary>
110+
internal void DrawHrContent(RGraphics g, RRect rect)
111+
{
101112
if (rect.Height > 2 && RenderUtils.IsColorVisible(ActualBackgroundColor))
102113
{
103114
g.DrawRectangle(g.GetSolidBrush(ActualBackgroundColor), rect.X, rect.Y, rect.Width, rect.Height);

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@ public RImage Image
7272
/// <param name="g">the device to draw to</param>
7373
protected override void PaintImp(RGraphics g)
7474
{
75-
// load image if it is in visible rectangle
76-
if (_imageLoadHandler == null)
77-
{
78-
_imageLoadHandler = new ImageLoadHandler(HtmlContainer, OnLoadImageComplete);
79-
_imageLoadHandler.LoadImage(GetImageSource(), HtmlTag != null ? HtmlTag.Attributes : null);
80-
}
75+
EnsureImageLoadStarted();
8176

8277
var rect = CommonUtils.GetFirstValueOrDefault(Rectangles);
8378
RPoint offset = RPoint.Empty;
@@ -92,6 +87,35 @@ protected override void PaintImp(RGraphics g)
9287
PaintBackground(g, rect, true, true);
9388
BordersDrawHandler.DrawBoxBorders(g, this, rect, true, true);
9489

90+
DrawImageContent(g, offset);
91+
92+
if (clipped)
93+
g.PopClip();
94+
}
95+
96+
/// <summary>
97+
/// Starts loading the image if it hasn't started already. This is the primary load trigger for
98+
/// the common async case (<see cref="HtmlContainerInt.AvoidAsyncImagesLoading"/>/
99+
/// <see cref="HtmlContainerInt.AvoidImagesLateLoading"/> both false) - <see cref="MeasureWordsSize"/>
100+
/// 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"/>.
102+
/// </summary>
103+
internal void EnsureImageLoadStarted()
104+
{
105+
if (_imageLoadHandler == null)
106+
{
107+
_imageLoadHandler = new ImageLoadHandler(HtmlContainer, OnLoadImageComplete);
108+
_imageLoadHandler.LoadImage(GetImageSource(), HtmlTag != null ? HtmlTag.Attributes : null);
109+
}
110+
}
111+
112+
/// <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.
116+
/// </summary>
117+
internal void DrawImageContent(RGraphics g, RPoint offset)
118+
{
95119
RRect r = _imageWord.Rectangle;
96120
r.Offset(offset);
97121
r.Height -= ActualBorderTopWidth + ActualBorderBottomWidth + ActualPaddingTop + ActualPaddingBottom;
@@ -129,9 +153,6 @@ protected override void PaintImp(RGraphics g)
129153
g.DrawRectangle(g.GetPen(RColor.LightGray), r.X, r.Y, r.Width, r.Height);
130154
}
131155
}
132-
133-
if (clipped)
134-
g.PopClip();
135156
}
136157

137158
/// <summary>

‎Source/HtmlRenderer/Core/Fragmentation/FragmentEmitter.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ private BoxFragment BuildBoxFragment(CssBox box, int fragmentainerIndex, PageBan
169169
}
170170
}
171171

172+
BoxFragment markerFragment = null;
173+
if (box.ListItemBox != null && HasContentInBand(box.ListItemBox, band))
174+
markerFragment = BuildBoxFragment(box.ListItemBox, fragmentainerIndex, band);
175+
172176
var rect = ToLocal(Clip(box.Bounds, band), band);
173177
var wholeBoxRect = ToLocal(box.Bounds, band);
174178

@@ -191,6 +195,7 @@ private BoxFragment BuildBoxFragment(CssBox box, int fragmentainerIndex, PageBan
191195
lines,
192196
words,
193197
children,
198+
markerFragment,
194199
OverflowClip: null);
195200
}
196201

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ internal sealed record TextFragment(RRect Rect, CssRect Word) : Fragment(Rect);
4747
/// The portion of one <see cref="CssBox"/> living in one fragmentainer. A box spanning a page boundary
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,
50-
/// then stacking-ordered child box fragments.
50+
/// 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
53+
/// <c>list-style-position: outside</c> marker can legitimately hang outside the content box's clip.
5154
/// </summary>
5255
internal sealed record BoxFragment(
5356
RRect Rect,
@@ -62,6 +65,7 @@ internal sealed record BoxFragment(
6265
IReadOnlyList<LineFragment> Lines,
6366
IReadOnlyList<TextFragment> Words,
6467
IReadOnlyList<BoxFragment> Children,
68+
BoxFragment MarkerFragment,
6569
RRect? OverflowClip) : Fragment(Rect)
6670
{
6771
/// <summary>The rect a replaced element paints its background/border over: the first line's rect, else this fragment's own rect.</summary>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using TheArtOfDev.HtmlRenderer.Core.Dom;
2+
3+
namespace TheArtOfDev.HtmlRenderer.Core.Paint.Content
4+
{
5+
/// <summary>
6+
/// Dispatches a box to its <see cref="IFragmentContentPainter"/>, matching PeachPDF's
7+
/// <c>FragmentContentPainters.For</c>. A null result tells <see cref="FragmentPainter"/> the generic
8+
/// box-fragment path (background/border per line, words, decoration, stacking-ordered children)
9+
/// applies instead - every leaf/replaced type with its own paint shape is listed here explicitly.
10+
/// </summary>
11+
internal static class FragmentContentPainters
12+
{
13+
internal static IFragmentContentPainter For(CssBox box)
14+
{
15+
switch (box)
16+
{
17+
case CssBoxImage:
18+
return ImageFragmentPainter.Instance;
19+
case CssBoxHr:
20+
return HrFragmentPainter.Instance;
21+
case CssBoxFrame:
22+
return FrameFragmentPainter.Instance;
23+
default:
24+
return null;
25+
}
26+
}
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using TheArtOfDev.HtmlRenderer.Adapters;
2+
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
3+
using TheArtOfDev.HtmlRenderer.Core.Dom;
4+
using TheArtOfDev.HtmlRenderer.Core.Fragments;
5+
6+
namespace TheArtOfDev.HtmlRenderer.Core.Paint.Content
7+
{
8+
/// <summary>Paints an <c>&lt;iframe&gt;</c> fragment - the YouTube/Vimeo video thumbnail/title/play chrome.</summary>
9+
internal sealed class FrameFragmentPainter : ReplacedFragmentPainter
10+
{
11+
internal static readonly FrameFragmentPainter Instance = new FrameFragmentPainter();
12+
13+
private FrameFragmentPainter()
14+
{
15+
}
16+
17+
protected override void DrawContent(RGraphics g, BoxFragment fragment, RPoint offset)
18+
{
19+
var box = (CssBoxFrame)fragment.Box;
20+
box.EnsureVideoImageLoadStarted();
21+
box.DrawFrameContent(g, offset);
22+
}
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using TheArtOfDev.HtmlRenderer.Adapters;
2+
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
3+
using TheArtOfDev.HtmlRenderer.Core.Dom;
4+
using TheArtOfDev.HtmlRenderer.Core.Fragments;
5+
6+
namespace TheArtOfDev.HtmlRenderer.Core.Paint.Content
7+
{
8+
/// <summary>
9+
/// Paints an <c>&lt;hr&gt;</c> fragment. Not a <see cref="ReplacedFragmentPainter"/> - a rule draws
10+
/// each border edge itself rather than going through the shared background+<c>DrawBoxBorders</c> step
11+
/// (see <see cref="CssBoxHr.DrawHrContent"/>), matching PeachPDF's <c>HrFragmentPainter</c>.
12+
/// </summary>
13+
internal sealed class HrFragmentPainter : IFragmentContentPainter
14+
{
15+
internal static readonly HrFragmentPainter Instance = new HrFragmentPainter();
16+
17+
private HrFragmentPainter()
18+
{
19+
}
20+
21+
public void Paint(FragmentPainter painter, RGraphics g, BoxFragment fragment)
22+
{
23+
var box = (CssBoxHr)fragment.Box;
24+
var offset = box.IsFixed ? RPoint.Empty : painter.Container.ScrollOffset;
25+
var rect = fragment.PrimaryRect;
26+
rect.Offset(offset);
27+
box.DrawHrContent(g, rect);
28+
}
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using TheArtOfDev.HtmlRenderer.Adapters;
2+
using TheArtOfDev.HtmlRenderer.Core.Fragments;
3+
4+
namespace TheArtOfDev.HtmlRenderer.Core.Paint.Content
5+
{
6+
/// <summary>
7+
/// Paints one box fragment's own replaced/leaf content - the per-type half of
8+
/// <see cref="FragmentPainter"/>'s dispatch (see <see cref="FragmentContentPainters.For"/>), matching
9+
/// PeachPDF's <c>IFragmentContentPainter</c> shape. Implementations are stateless singletons.
10+
/// </summary>
11+
internal interface IFragmentContentPainter
12+
{
13+
void Paint(FragmentPainter painter, RGraphics g, BoxFragment fragment);
14+
}
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using TheArtOfDev.HtmlRenderer.Adapters;
2+
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
3+
using TheArtOfDev.HtmlRenderer.Core.Dom;
4+
using TheArtOfDev.HtmlRenderer.Core.Fragments;
5+
6+
namespace TheArtOfDev.HtmlRenderer.Core.Paint.Content
7+
{
8+
/// <summary>Paints an <c>&lt;img&gt;</c> fragment - image, or error/loading placeholder.</summary>
9+
internal sealed class ImageFragmentPainter : ReplacedFragmentPainter
10+
{
11+
internal static readonly ImageFragmentPainter Instance = new ImageFragmentPainter();
12+
13+
private ImageFragmentPainter()
14+
{
15+
}
16+
17+
protected override void DrawContent(RGraphics g, BoxFragment fragment, RPoint offset)
18+
{
19+
var box = (CssBoxImage)fragment.Box;
20+
box.EnsureImageLoadStarted();
21+
box.DrawImageContent(g, offset);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)