using System.Diagnostics; using System.Globalization; using System.Text; using System.Text.RegularExpressions; using PdfSharp.Pdf; namespace OpenNest.Tests.Reporting; /// PDF inspection for report tests: PDFsharp for structure, Poppler for text. public static class ReportPdf { public static (int, int) EffectiveSize(PdfPage page) { var box = page.MediaBox; var width = (int)System.Math.Round(box.Width); var height = (int)System.Math.Round(box.Height); return page.Rotate % 180 == 0 ? (width, height) : (height, width); } public static IEnumerable ContentStreams(PdfPage page) { foreach (var item in page.Contents.Elements) { var dictionary = (item as PdfSharp.Pdf.Advanced.PdfReference)?.Value as PdfDictionary ?? item as PdfDictionary; if (dictionary?.Stream != null) yield return Encoding.Latin1.GetString(dictionary.Stream.UnfilteredValue); } } /// Visual-row text of one page; bbox coordinates avoid platform-specific -layout order. public static string Text(string path, int page) => LayoutPage(Run("-bbox", "-f", Page(page), "-l", Page(page), path, "-")); /// Every page's visual-row text, in order. public static string[] Pages(string path) { var html = Run("-bbox", path, "-"); return Regex.Matches(html, @"]*>.*?", RegexOptions.Singleline) .Select(match => LayoutPage(match.Value)).ToArray(); } private static string LayoutPage(string html) { var rows = new List<(double Top, List<(double Left, string Text)> Words)>(); foreach (var word in ParseWords(html).OrderBy(word => word.Top).ThenBy(word => word.Left)) { if (rows.Count == 0 || System.Math.Abs(word.Top - rows[^1].Top) > 1.5) rows.Add((word.Top, [])); rows[^1].Words.Add((word.Left, word.Text)); } return string.Join("\n", rows.Select(row => string.Join(" ", row.Words.OrderBy(word => word.Left).Select(word => word.Text)))) + "\n"; } /// Summary-page card grid geometry, as laid out by NestPdfWriter. public const double CardLeft = 36, CardWidth = 129, CardGap = 8; public const int CardsPerRow = 4; /// /// Each summary part card's text, lines joined by '\n', in reading order (page, row, column). /// A card ends with its "Plates:" line; header, footer and the weight note are excluded. /// public static List PartCards(string path) { var html = Run("-bbox", path, "-"); var cards = new List<(int Page, double Top, int Column, string Text)>(); var started = false; var pages = Regex.Matches(html, @"]*>.*?", RegexOptions.Singleline).Select(match => match.Value).ToList(); for (var page = 0; page < pages.Count; page++) { if (LayoutPage(pages[page]).Contains("Plate 1 of ")) break; var words = ParseWords(pages[page]); var rows = Rows(words); // Cards start below the "Parts" heading; long notes can push it past the first page. var parts = rows.FindIndex(row => Join(row.Words) == "Parts"); if (!started && parts < 0) continue; started = true; var top = parts >= 0 ? rows[parts].Top : 0; var excluded = rows.Where(row => Join(row.Words) is var text && (text.StartsWith("Weights need", StringComparison.Ordinal) || text.StartsWith("Nest report:", StringComparison.Ordinal) || Regex.IsMatch(text, @"^(Generated \S+ \S+ )?Page \d+ of \d+$"))) .Select(row => row.Top).ToList(); for (var column = 0; column < CardsPerRow; column++) { var left = CardLeft + column * (CardWidth + CardGap); var inside = words.Where(word => word.Left >= left - 1 && word.Right <= left + CardWidth + 1 && word.Top > top + 1 && !excluded.Any(row => System.Math.Abs(row - word.Top) <= 1.5)).ToList(); var lines = new List(); var start = 0.0; foreach (var row in Rows(inside)) { if (lines.Count == 0) start = row.Top; var text = Join(row.Words); lines.Add(text); if (text.StartsWith("Plates:", StringComparison.Ordinal)) { cards.Add((page, start, column, string.Join("\n", lines))); lines.Clear(); } } Assert.Empty(lines); } } return cards.OrderBy(card => card.Page).ThenBy(card => System.Math.Round(card.Top)).ThenBy(card => card.Column) .Select(card => card.Text).ToList(); } private static List<(double Top, List<(double Left, double Top, double Right, double Bottom, string Text)> Words)> Rows( IEnumerable<(double Left, double Top, double Right, double Bottom, string Text)> words) { var rows = new List<(double Top, List<(double Left, double Top, double Right, double Bottom, string Text)> Words)>(); foreach (var word in words.OrderBy(word => word.Top).ThenBy(word => word.Left)) { if (rows.Count == 0 || System.Math.Abs(word.Top - rows[^1].Top) > 1.5) rows.Add((word.Top, [])); rows[^1].Words.Add(word); } return rows; } private static string Join(IEnumerable<(double Left, double Top, double Right, double Bottom, string Text)> words) => string.Join(" ", words.OrderBy(word => word.Left).Select(word => word.Text)); /// Words with page-space boxes (points, Y down). public static List<(double Left, double Top, double Right, double Bottom, string Text)> Words(string path, int page) => ParseWords(Run("-bbox", "-f", Page(page), "-l", Page(page), path, "-")); private static List<(double Left, double Top, double Right, double Bottom, string Text)> ParseWords(string xml) { return Regex.Matches(xml, "([^<]*)") .Select(match => (Number(match.Groups[1].Value), Number(match.Groups[2].Value), Number(match.Groups[3].Value), Number(match.Groups[4].Value), System.Net.WebUtility.HtmlDecode(match.Groups[5].Value))) .ToList(); } private static string Page(int page) => page.ToString(CultureInfo.InvariantCulture); private static double Number(string value) => double.Parse(value, CultureInfo.InvariantCulture); /// Names the pdftotext to run, instead of the first on PATH that accepts -bbox. public const string ToolVariable = "OPENNEST_PDFTOTEXT"; private static readonly Lazy<(string Tool, string Problem)> PdfToText = new(FindPdfToText); /// /// The pdftotext the text assertions run. PATH can hold more than one: Git for Windows /// ships Xpdf's, which has no -bbox, and a shell that puts Git first hides Poppler's. /// So the search takes the first that offers -bbox rather than the first it meets. /// private static (string Tool, string Problem) FindPdfToText() { var named = Environment.GetEnvironmentVariable(ToolVariable); if (!string.IsNullOrWhiteSpace(named)) return (named, null); var name = OperatingSystem.IsWindows() ? "pdftotext.exe" : "pdftotext"; var found = (Environment.GetEnvironmentVariable("PATH") ?? string.Empty) .Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Select(folder => Path.Combine(folder.Trim('"'), name)) .Where(File.Exists) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); var tool = FirstWithBbox(found, Help); if (tool != null) return (tool, null); return found.Count == 0 ? (null, null) : (null, $"No pdftotext on PATH accepts -bbox (found {string.Join(", ", found)}). " + $"Install Poppler, or name its pdftotext in {ToolVariable}."); } /// The first candidate whose help text offers -bbox, or null when none does. internal static string FirstWithBbox(IEnumerable candidates, Func help) => candidates.FirstOrDefault(candidate => Regex.IsMatch(help(candidate), @"(?m)^\s*-bbox(?![\w-])")); /// What a pdftotext prints for -h, on either stream; empty when it cannot run. private static string Help(string tool) { try { var info = new ProcessStartInfo(tool) { RedirectStandardOutput = true, RedirectStandardError = true, }; info.ArgumentList.Add("-h"); using var process = Process.Start(info)!; var error = process.StandardError.ReadToEndAsync(); var output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); return output + error.Result; } catch (Exception exception) when (exception is System.ComponentModel.Win32Exception or IOException or InvalidOperationException) { return string.Empty; } } private static string Run(params string[] arguments) { var (tool, problem) = PdfToText.Value; Assert.True(problem == null, problem); Skip.If(tool == null, "pdftotext (poppler-utils) is not installed."); var info = new ProcessStartInfo(tool) { RedirectStandardOutput = true, RedirectStandardError = true, }; foreach (var argument in arguments) info.ArgumentList.Add(argument); Process process; try { process = Process.Start(info)!; } catch (System.ComponentModel.Win32Exception exception) { // Only a pdftotext named in the variable gets here: one found on PATH exists. Assert.Fail($"{tool} ({ToolVariable}) cannot start: {exception.Message}"); throw; } using (process) { var text = process.StandardOutput.ReadToEnd(); process.WaitForExit(); Assert.Equal(0, process.ExitCode); return text; } } }