From fb9213e6c52b96a768504d55d95e00ebd998f49a Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 14 Jan 2024 19:40:42 +0100 Subject: [PATCH] Added latex include table generator --- Implementation/ReportGenerator/Program.cs | 3 +- .../LatexIncludeDocumentGenerator.cs | 90 +++++++++++++++++++ .../LatexIncludeTableGenerator.cs | 59 ++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeDocumentGenerator.cs create mode 100644 ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeTableGenerator.cs diff --git a/Implementation/ReportGenerator/Program.cs b/Implementation/ReportGenerator/Program.cs index 240442b..58f7d8e 100644 --- a/Implementation/ReportGenerator/Program.cs +++ b/Implementation/ReportGenerator/Program.cs @@ -1,6 +1,7 @@ using Common.Extensions; using Ocr.Report.Models; using ReportGeneration.Generators; +using ReportGeneration.Generators.LatexInclude; namespace Ocr.Report; @@ -21,7 +22,7 @@ internal static class Program var path = Path.GetFullPath("report.html"); - using var document = new HtmlDocumentGenerator(path); + using var document = new LatexIncludeDocumentGenerator(path); using var report = new ReportGenerator("OCR Report", document, scans) .AddComparison("Processing summary (Average)", v => { diff --git a/ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeDocumentGenerator.cs b/ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeDocumentGenerator.cs new file mode 100644 index 0000000..a3c4d04 --- /dev/null +++ b/ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeDocumentGenerator.cs @@ -0,0 +1,90 @@ +using System; +using System.IO; +using ReportGeneration.Abstract; +using ReportGeneration.Interface; + +namespace ReportGeneration.Generators.LatexInclude; + +public class LatexIncludeDocumentGenerator : DocumentGeneratorBase +{ + private string? _lastHeading; + private int _sectionFigCnt; + + public string Path { get; } + + public LatexIncludeDocumentGenerator(string name, bool overwrite = false) + : this(PrepareDir(name, overwrite)) + { + } + + private LatexIncludeDocumentGenerator(string name) : base($"{name}/main.tex") + { + } + + private static string PrepareDir(string name, bool overwrite) + { + var path = System.IO.Path.GetFullPath(name); + if (Directory.Exists(path)) + { + if (overwrite) + { + Directory.Delete(path, true); + } + else + { + throw new InvalidOperationException($"Directory '{path}' already exists!"); + } + } + + Directory.CreateDirectory(path); + return path; + } + + #region Writing + + /// + public override IDocumentGenerator AppendHeading(int level, string text) + { + _sectionFigCnt = 0; + var heading = (level) switch + { + 1 => $"\\chapter{text}", + 2 => $"\\section{text}", + 3 => $"\\subsection{text}", + 4 => $"\\subsubsection{text}", + 5 => $"\\subsubsubsection{text}", + _ => throw new ArgumentOutOfRangeException(nameof(level), level, null) + }; + + _lastHeading = heading; + return AppendParagraph(heading); + } + + /// + protected override ITableGenerator MakeTable(int columns, Stream stream) + { + var includePath = System.IO.Path.Join(Path, $"fig_{_lastHeading}_{++_sectionFigCnt}.tex"); + var file = File.Open(includePath, FileMode.CreateNew); + + AppendParagraph($"\\include{includePath}"); + + return new LatexIncludeTableGenerator(columns, file); + } + + /// + public override IDocumentGenerator AppendParagraph(string? text = default) + { + AppendLine(text); + AppendLine(); + return this; + } + + #endregion + + public override string FormatImage(string path, IBounds? bounds = default) => @$" +\begin{{figure}}[ht] + \centering + \fbox{{\includegraphics[width=.7\textwidth]{{{path}}}}} + \caption{{Ein gut für die Texterkennung geeigneter Screenshot. Die wesentlichen Inhalte weisen einen guten Kontrast zum Hintergrund auf und befinden sich in Bereichen mit gleichmäßiger Helligkeit.}} +\end{{figure}}"; +} \ No newline at end of file diff --git a/ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeTableGenerator.cs b/ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeTableGenerator.cs new file mode 100644 index 0000000..ef050d6 --- /dev/null +++ b/ReportGeneration/ReportGeneration.Generators/LatexInclude/LatexIncludeTableGenerator.cs @@ -0,0 +1,59 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using ReportGeneration.Abstract; +using ReportGeneration.Interface; + +namespace ReportGeneration.Generators.LatexInclude; + +public class LatexIncludeTableGenerator : TableGeneratorBase +{ + private const string rowSuffix = @" \\ \hline"; + + public LatexIncludeTableGenerator(int columns) : base(columns) + { + } + + public LatexIncludeTableGenerator(int columns, Stream stream) : base(columns, stream) + { + } + + public LatexIncludeTableGenerator(int columns, Stream stream, Encoding encoding) : base(columns, stream, encoding) + { + } + + protected override void OnOpen() + { + base.OnOpen(); + var tabular = '|' + string.Concat(Enumerable.Repeat("l|", Columns)); + + WriteLine(@$" +\begin{{table}}[!ht] + \centering + \caption{{Caption}} + \begin{{tabular}}{{{tabular}}} + \hline"); + } + + protected override void OnClose() + { + base.OnClose(); + WriteLine(@" + \end{{tabular}} +\end{{table}}"); + } + + /// + public override ITableGenerator AppendHeader(IEnumerable row) => AppendRow(row.Select(Bold)); + + /// + public override ITableGenerator AppendRow(IEnumerable row) + { + WriteLine(string.Join('&', row.Select(Escape)) + rowSuffix); + return this; + } + + private string Bold(string str) => $@"\textbf{str}"; + private string Escape(string str) => str.Replace("%", "\\%"); +} \ No newline at end of file