Skip to content

Commit fda44e3

Browse files
committed
R0: paint text from the fragment tree, not live CssRect geometry
FragmentPainter painted backgrounds/borders/decoration from fragment.Lines (already fragment-tree-local) but text via CssBox.PaintWords, which iterated box.Words directly - live, absolute-document-Y CssRect.Rectangle - reconciled with LiveTreeOffset's band-top subtraction. BoxFragment.Words (TextFragment records) already existed, already correctly band-localized by FragmentEmitter, and was simply unused for the actual draw call. Split CssBox.PaintWords into CssBox.PaintWord(g, word, wordRect): a single-word primitive taking an already-final rect instead of computing one from live geometry plus an offset parameter. FragmentPainter now loops fragment.Words directly, offsetting each TextFragment.Rect by the same FragmentLocalOffset Lines already uses - no band-top reconciliation needed for text at all, since the geometry was never live to begin with. First stage (R0) of the plan to replace HTML-Renderer's local-correction fragmentation with a real resumable pass-loop matching PeachPDF's architecture. Pure paint-side change, no layout code touched. Full regression suite (35 pixel-diff tests, 16 PDF tests) passes with zero pixel differences.
1 parent 3914ffc commit fda44e3

2 files changed

Lines changed: 70 additions & 67 deletions

File tree

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

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,61 +1403,54 @@ internal void PaintBackground(RGraphics g, RRect rect, bool isFirst, bool isLast
14031403
}
14041404

14051405
/// <summary>
1406-
/// Paint all the words in the box.
1406+
/// Paints one word at <paramref name="wordRect"/>, its final paint position - the caller decides
1407+
/// where that is (fragment-tree-local geometry, offset the same way <see cref="PaintBackground"/>'s
1408+
/// line rects already are; there is no live-tree geometry read here, only style/selection state).
14071409
/// </summary>
14081410
/// <param name="g">the device to draw into</param>
1409-
/// <param name="offset">the current scroll offset to offset the words</param>
1410-
internal void PaintWords(RGraphics g, RPoint offset)
1411+
/// <param name="word">the word to paint</param>
1412+
/// <param name="wordRect">the word's final paint rectangle</param>
1413+
internal void PaintWord(RGraphics g, CssRect word, RRect wordRect)
14111414
{
1412-
if (Width.Length > 0)
1415+
if (word.IsLineBreak)
1416+
return;
1417+
1418+
var clip = g.GetClip();
1419+
clip.Intersect(wordRect);
1420+
if (clip == RRect.Empty)
1421+
return;
1422+
1423+
var isRtl = Direction == CssConstants.Rtl;
1424+
var wordPoint = new RPoint(wordRect.X, wordRect.Y);
1425+
if (word.Selected)
14131426
{
1414-
var isRtl = Direction == CssConstants.Rtl;
1415-
foreach (var word in Words)
1416-
{
1417-
if (!word.IsLineBreak)
1418-
{
1419-
var clip = g.GetClip();
1420-
var wordRect = word.Rectangle;
1421-
wordRect.Offset(offset);
1422-
clip.Intersect(wordRect);
1427+
// handle paint selected word background and with partial word selection
1428+
var wordLine = DomUtils.GetCssLineBoxByWord(word);
1429+
var left = word.SelectedStartOffset > -1 ? word.SelectedStartOffset : (wordLine.Words[0] != word && word.HasSpaceBefore ? -ActualWordSpacing : 0);
1430+
var padWordRight = word.HasSpaceAfter && !wordLine.IsLastSelectedWord(word);
1431+
var width = word.SelectedEndOffset > -1 ? word.SelectedEndOffset : word.Width + (padWordRight ? ActualWordSpacing : 0);
1432+
var rect = new RRect(wordRect.X + left, wordRect.Y, width - left, wordLine.LineHeight);
14231433

1424-
if (clip != RRect.Empty)
1425-
{
1426-
var wordPoint = new RPoint(word.Left + offset.X, word.Top + offset.Y);
1427-
if (word.Selected)
1428-
{
1429-
// handle paint selected word background and with partial word selection
1430-
var wordLine = DomUtils.GetCssLineBoxByWord(word);
1431-
var left = word.SelectedStartOffset > -1 ? word.SelectedStartOffset : (wordLine.Words[0] != word && word.HasSpaceBefore ? -ActualWordSpacing : 0);
1432-
var padWordRight = word.HasSpaceAfter && !wordLine.IsLastSelectedWord(word);
1433-
var width = word.SelectedEndOffset > -1 ? word.SelectedEndOffset : word.Width + (padWordRight ? ActualWordSpacing : 0);
1434-
var rect = new RRect(word.Left + offset.X + left, word.Top + offset.Y, width - left, wordLine.LineHeight);
1435-
1436-
g.DrawRectangle(GetSelectionBackBrush(g, false), rect.X, rect.Y, rect.Width, rect.Height);
1437-
1438-
if (HtmlContainer.SelectionForeColor != RColor.Empty && (word.SelectedStartOffset > 0 || word.SelectedEndIndexOffset > -1))
1439-
{
1440-
g.PushClipExclude(rect);
1441-
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new RSize(word.Width, word.Height), isRtl);
1442-
g.PopClip();
1443-
g.PushClip(rect);
1444-
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new RSize(word.Width, word.Height), isRtl);
1445-
g.PopClip();
1446-
}
1447-
else
1448-
{
1449-
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new RSize(word.Width, word.Height), isRtl);
1450-
}
1451-
}
1452-
else
1453-
{
1454-
// g.DrawRectangle(HtmlContainer.Adapter.GetPen(RColor.Black), wordPoint.X, wordPoint.Y, word.Width - 1, word.Height - 1);
1455-
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new RSize(word.Width, word.Height), isRtl);
1456-
}
1457-
}
1458-
}
1434+
g.DrawRectangle(GetSelectionBackBrush(g, false), rect.X, rect.Y, rect.Width, rect.Height);
1435+
1436+
if (HtmlContainer.SelectionForeColor != RColor.Empty && (word.SelectedStartOffset > 0 || word.SelectedEndIndexOffset > -1))
1437+
{
1438+
g.PushClipExclude(rect);
1439+
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new RSize(word.Width, word.Height), isRtl);
1440+
g.PopClip();
1441+
g.PushClip(rect);
1442+
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new RSize(word.Width, word.Height), isRtl);
1443+
g.PopClip();
1444+
}
1445+
else
1446+
{
1447+
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new RSize(word.Width, word.Height), isRtl);
14591448
}
14601449
}
1450+
else
1451+
{
1452+
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new RSize(word.Width, word.Height), isRtl);
1453+
}
14611454
}
14621455

14631456
/// <summary>

‎Source/HtmlRenderer/Core/Paint/FragmentPainter.cs‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ namespace TheArtOfDev.HtmlRenderer.Core.Paint
1212
/// <summary>
1313
/// Paints a fragmentainer from the immutable fragment tree - the sole paint path now that the old
1414
/// live-tree walk (formerly <c>CssBox.Paint</c>/<c>PaintImp</c>) has been deleted. Every geometric
15-
/// decision reads from the <see cref="BoxFragment"/> being painted;
16-
/// the box back-reference (<see cref="BoxFragment.Box"/>) is consulted only for computed style and,
17-
/// for now, for the paint primitives themselves (<see cref="CssBox.PaintBackground"/>/
18-
/// <see cref="CssBox.PaintWords"/>/<see cref="CssBox.PaintDecoration"/> - widened from <c>protected</c>/
19-
/// <c>private</c> to <c>internal</c> rather than duplicated here, so this stays a faithful re-shaping of
20-
/// the existing, tested paint code rather than a parallel reimplementation).
15+
/// decision reads from the <see cref="BoxFragment"/> being painted, including text: each word paints
16+
/// at its own <see cref="TextFragment.Rect"/>, not <c>CssRect.Rectangle</c> read off the live box.
17+
/// The box back-reference (<see cref="BoxFragment.Box"/>) is consulted only for computed style and
18+
/// paint primitives themselves (<see cref="CssBox.PaintBackground"/>/<see cref="CssBox.PaintWord"/>/
19+
/// <see cref="CssBox.PaintDecoration"/> - widened from <c>protected</c>/<c>private</c> to <c>internal</c>
20+
/// rather than duplicated here, so this stays a faithful re-shaping of the existing, tested paint code
21+
/// rather than a parallel reimplementation).
2122
/// </summary>
2223
/// <remarks>
2324
/// Leaf/replaced types dispatch to their own <see cref="Content.IFragmentContentPainter"/> (matching
@@ -45,14 +46,15 @@ internal sealed class FragmentPainter
4546
/// <summary>
4647
/// The real document-Y top of the fragmentainer currently being painted (<see cref="FragmentainerFragment.LocalOriginY"/>),
4748
/// set once per <see cref="Paint"/> call. Geometry sourced from the fragment tree (<see cref="BoxFragment.Lines"/>/
48-
/// <see cref="BoxFragment.PrimaryRect"/>) is already local to this band (<see cref="Fragmentation.FragmentEmitter"/>
49-
/// subtracts it at build time) and needs no further adjustment for it. Geometry read straight off the
50-
/// live <see cref="CssBox"/> tree instead (<see cref="CssBox.PaintWords"/>'s <c>word.Rectangle</c>,
51-
/// <see cref="CssBoxImage.DrawImageContent"/>'s image-word rect, the visibility cull below) is still
52-
/// absolute document-Y and must have this subtracted to land in the same target frame - missing this
53-
/// distinction was a real bug (found while building the continuous-surface paint path this field
54-
/// supports): every page after the first silently painted zero text, since a fresh per-page surface's
55-
/// origin is this band's top, not the document's.
49+
/// <see cref="BoxFragment.Words"/>/<see cref="BoxFragment.PrimaryRect"/>) is already local to this band
50+
/// (<see cref="Fragmentation.FragmentEmitter"/> subtracts it at build time) and needs no further
51+
/// adjustment for it. Geometry read straight off the live <see cref="CssBox"/> tree instead
52+
/// (<see cref="CssBoxImage.DrawImageContent"/>'s image-word rect, the visibility cull below) is
53+
/// still absolute document-Y and must have this subtracted to land in the same target frame -
54+
/// missing this distinction for text was a real bug (found while building the continuous-surface
55+
/// paint path this field supports, since fixed by moving word painting onto the fragment tree
56+
/// entirely rather than reconciling it): every page after the first silently painted zero text,
57+
/// since a fresh per-page surface's origin is this band's top, not the document's.
5658
/// </summary>
5759
private double _bandTop;
5860

@@ -169,10 +171,8 @@ private void PaintFragmentContent(RGraphics g, BoxFragment fragment)
169171

170172
var clipped = RenderUtils.ClipGraphicsByOverflow(g, box, LiveTreeExtraOffset);
171173
var clip = g.GetClip();
172-
// fragment.Lines is already fragment-local (FragmentEmitter subtracted the band top at build
173-
// time) - only FragmentLocalOffset (scroll + page-origin) applies. box.PaintWords instead
174-
// reads box.Words directly off the live tree (still absolute document-Y), so it needs
175-
// LiveTreeOffset to additionally undo the band top - see _bandTop's doc comment.
174+
// fragment.Lines/fragment.Words are already fragment-local (FragmentEmitter subtracted the
175+
// band top at build time) - only FragmentLocalOffset (scroll + page-origin) applies to either.
176176
var offset = FragmentLocalOffset(box.IsFixed);
177177
var lines = fragment.Lines;
178178

@@ -187,7 +187,17 @@ private void PaintFragmentContent(RGraphics g, BoxFragment fragment)
187187
}
188188
}
189189

190-
box.PaintWords(g, LiveTreeOffset(box.IsFixed));
190+
// Width.Length > 0 gate matches CssBox's own former PaintWords guard - preserved here since
191+
// it's the caller's job now that word painting reads the fragment tree, not the live box.
192+
if (box.Width.Length > 0)
193+
{
194+
foreach (var wordFragment in fragment.Words)
195+
{
196+
var wordRect = wordFragment.Rect;
197+
wordRect.Offset(offset);
198+
box.PaintWord(g, wordFragment.Word, wordRect);
199+
}
200+
}
191201

192202
for (var i = 0; i < lines.Count; i++)
193203
{

0 commit comments

Comments
 (0)