|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Drawing; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | +using TheArtOfDev.HtmlRenderer.Core; |
| 7 | +using TheArtOfDev.HtmlRenderer.Core.Fragments; |
| 8 | +using TheArtOfDev.HtmlRenderer.WinForms; |
| 9 | + |
| 10 | +namespace TheArtOfDev.HtmlRenderer.IntegrationTest; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Verifies a real, previously-undocumented gap found while auditing this port's fragmentation engine |
| 14 | +/// against PeachPDF a second time (the first audit produced the R0-R10 plan; this is a later, separate |
| 15 | +/// pass over what remained): <c>InlineFragmentation.ApplyLineBreaking</c>'s orphans merge-back correction |
| 16 | +/// only ever ran once at least one earlier break already existed (<c>breaks.Count > 1</c>), which can |
| 17 | +/// never be true while still deciding a paragraph's very FIRST run - so a paragraph starting close enough |
| 18 | +/// to a page's bottom that fewer than <c>orphans</c> lines fit there was left with a too-small stranded |
| 19 | +/// first fragment, uncorrected. Confirmed by temporarily reverting the fix and re-running this exact test: |
| 20 | +/// it reliably reproduced a 1-line first page against <c>orphans:2</c> at several filler counts (13, 28, |
| 21 | +/// 43, 58 - the same ~15-count period the page-height/line-height ratio produces). |
| 22 | +/// </summary> |
| 23 | +[TestClass] |
| 24 | +[DoNotParallelize] |
| 25 | +public sealed class OrphansOnFirstRunTest |
| 26 | +{ |
| 27 | + private static HtmlContainerInt GetInternal(HtmlContainer wrapper) |
| 28 | + { |
| 29 | + var prop = typeof(HtmlContainer).GetProperty("HtmlContainerInt", BindingFlags.NonPublic | BindingFlags.Instance)!; |
| 30 | + return (HtmlContainerInt)prop.GetValue(wrapper)!; |
| 31 | + } |
| 32 | + |
| 33 | + private static IEnumerable<(string Text, double Top)> AllTargetWords(BoxFragment f) |
| 34 | + { |
| 35 | + foreach (var w in f.Words) |
| 36 | + if (!w.Word.IsLineBreak && w.Word.Text.StartsWith("TargetLine")) |
| 37 | + yield return (w.Word.Text, w.Rect.Top); |
| 38 | + foreach (var c in f.Children) |
| 39 | + foreach (var x in AllTargetWords(c)) |
| 40 | + yield return x; |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public async Task ParagraphStartingNearPageBottom_NeverStrandsFewerThanOrphansLines() |
| 45 | + { |
| 46 | + // Sweep filler counts rather than hardcoding one - this is a "just barely fits" calibration |
| 47 | + // (see this session's own established testing lesson), and the exact boundary depends on |
| 48 | + // font-metric arithmetic other changes are expected to keep touching. |
| 49 | + for (var fillerCount = 1; fillerCount < 60; fillerCount++) |
| 50 | + { |
| 51 | + using var wrapper = new HtmlContainer(); |
| 52 | + var filler = string.Concat(Enumerable.Repeat("<p style='margin:0;'>filler line</p>", fillerCount)); |
| 53 | + await wrapper.SetHtml( |
| 54 | + $""" |
| 55 | + <html><body> |
| 56 | + {filler} |
| 57 | + <p style="margin:0; orphans:2;">TargetLineOne TargetLineTwo TargetLineThree TargetLineFour TargetLineFive TargetLineSix TargetLineSeven TargetLineEight TargetLineNine TargetLineTen</p> |
| 58 | + </body></html> |
| 59 | + """); |
| 60 | + |
| 61 | + var container = GetInternal(wrapper); |
| 62 | + container.PageSize = new TheArtOfDev.HtmlRenderer.Adapters.Entities.RSize(200, 300); |
| 63 | + container.MarginTop = 0; |
| 64 | + wrapper.MaxSize = new SizeF(200, 0); |
| 65 | + |
| 66 | + using var bitmap = new Bitmap(200, 20000); |
| 67 | + using var g = Graphics.FromImage(bitmap); |
| 68 | + wrapper.PerformLayout(g); |
| 69 | + |
| 70 | + var tree = container.FragmentTree; |
| 71 | + Assert.IsNotNull(tree); |
| 72 | + |
| 73 | + var wordInfo = new List<(string Text, int Page, double Top)>(); |
| 74 | + for (var pi = 0; pi < tree.Fragmentainers.Count; pi++) |
| 75 | + foreach (var w in AllTargetWords(tree.Fragmentainers[pi].Root)) |
| 76 | + wordInfo.Add((w.Text, pi, System.Math.Round(w.Top, 1))); |
| 77 | + |
| 78 | + if (wordInfo.Count == 0) |
| 79 | + continue; // paragraph didn't appear in this bitmap height at this filler count - try the next |
| 80 | + |
| 81 | + var firstPage = wordInfo[0].Page; |
| 82 | + var linesOnFirstPage = wordInfo.Where(w => w.Page == firstPage).Select(w => w.Top).Distinct().Count(); |
| 83 | + |
| 84 | + Assert.IsGreaterThanOrEqualTo(2, linesOnFirstPage, |
| 85 | + $"at fillerCount={fillerCount}, the paragraph's first page-fragment kept only {linesOnFirstPage} line(s), fewer than orphans:2 requires"); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments