Fixed figure generation

This commit is contained in:
Simon
2024-01-14 20:30:22 +01:00
parent 2736feb0e7
commit 745e41f474
8 changed files with 98 additions and 63 deletions

View File

@@ -20,9 +20,9 @@ internal static class Program
Console.WriteLine("Generating report");
var scans = Scan(tagFileInfos, scanFileInfos);
var path = Path.GetFullPath("report.html");
var path = Path.GetFullPath("report");
using var document = new LatexIncludeDocumentGenerator(path);
using var document = new LatexIncludeDocumentGenerator(path, true);
using var report = new ReportGenerator("OCR Report", document, scans)
.AddComparison("Processing summary (Average)", v =>
{

View File

@@ -9,19 +9,26 @@ namespace ReportGeneration.Abstract;
public abstract class DocumentGeneratorBase : StreamWriterBase, IDocumentGenerator
{
/// <inheritdoc />
protected DocumentGeneratorBase() { }
protected DocumentGeneratorBase()
{
}
/// <inheritdoc />
protected DocumentGeneratorBase(Stream stream) : base(stream) { }
protected DocumentGeneratorBase(Stream stream) : base(stream)
{
}
/// <inheritdoc />
protected DocumentGeneratorBase(string filePath) : base(File.Open(filePath, FileMode.Create,
FileAccess.Write))
{ }
{
}
/// <inheritdoc />
protected DocumentGeneratorBase(Stream stream, Encoding encoding) : base(stream, encoding) { }
protected DocumentGeneratorBase(Stream stream, Encoding encoding) : base(stream, encoding)
{
}
#region Writing
/// <inheritdoc />
@@ -45,16 +52,10 @@ public abstract class DocumentGeneratorBase : StreamWriterBase, IDocumentGenerat
public abstract IDocumentGenerator AppendParagraph(string? text = default);
/// <inheritdoc />
public IDocumentGenerator AppendTable(int columns, Action<ITableGenerator> table)
{
Write(() => MakeTable(columns, new MemoryStream()), table);
return this;
}
protected abstract ITableGenerator MakeTable(int columns, Stream stream);
public abstract IDocumentGenerator AppendTable(int columns, Action<ITableGenerator> table);
#endregion
/// <inheritdoc />
public abstract string FormatImage(string path, IBounds? bounds = default);
}
}

View File

@@ -155,7 +155,7 @@ public abstract class StreamWriterBase : IStreamWriter
Write(writer);
return this;
}
#endregion
#region IDisposable

View File

@@ -0,0 +1,34 @@
using System;
using System.IO;
using System.Text;
using ReportGeneration.Interface;
namespace ReportGeneration.Abstract;
public abstract class SubItemDocumentGeneratorBase : DocumentGeneratorBase
{
protected SubItemDocumentGeneratorBase()
{
}
protected SubItemDocumentGeneratorBase(Stream stream) : base(stream)
{
}
protected SubItemDocumentGeneratorBase(string filePath) : base(filePath)
{
}
protected SubItemDocumentGeneratorBase(Stream stream, Encoding encoding) : base(stream, encoding)
{
}
/// <inheritdoc />
public override IDocumentGenerator AppendTable(int columns, Action<ITableGenerator> table)
{
Write(() => MakeTable(columns, new MemoryStream()), table);
return this;
}
protected abstract ITableGenerator MakeTable(int columns, Stream stream);
}

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace ReportGeneration.Generators;
public class HtmlDocumentGenerator : DocumentGeneratorBase
public class HtmlDocumentGenerator : SubItemDocumentGeneratorBase
{
private int _sectionLevel = 0;

View File

@@ -7,37 +7,34 @@ namespace ReportGeneration.Generators.LatexInclude;
public class LatexIncludeDocumentGenerator : DocumentGeneratorBase
{
private const string figDir = "figures";
private string? _lastHeading;
private int _sectionFigCnt;
public string Path { get; }
public string Dir { get; }
public LatexIncludeDocumentGenerator(string name, bool overwrite = false)
: this(PrepareDir(name, overwrite))
{
}
private LatexIncludeDocumentGenerator(string name) : base($"{name}/main.tex")
{
}
private LatexIncludeDocumentGenerator(string dir) : base($"{dir}/main.tex") => Dir = dir;
private static string PrepareDir(string name, bool overwrite)
private static string PrepareDir(string dirName, bool overwrite)
{
var path = System.IO.Path.GetFullPath(name);
if (Directory.Exists(path))
if (Directory.Exists(dirName))
{
if (overwrite)
{
Directory.Delete(path, true);
Directory.Delete(dirName, true);
}
else
{
throw new InvalidOperationException($"Directory '{path}' already exists!");
throw new InvalidOperationException($"Directory '{dirName}' already exists!");
}
}
Directory.CreateDirectory(path);
return path;
Directory.CreateDirectory(Path.Join(dirName, figDir));
return dirName;
}
#region Writing
@@ -45,30 +42,38 @@ public class LatexIncludeDocumentGenerator : DocumentGeneratorBase
/// <inheritdoc />
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 = text;
_lastHeading = heading;
return AppendParagraph(heading);
return AppendParagraph((level) switch
{
1 => $"\\chapter{{{text}}}",
2 => $"\\section{{{text}}}",
3 => $"\\subsection{{{text}}}",
4 => $"\\subsubsection{{{text}}}",
5 => $"\\subsubsubsection{{{text}}}",
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
});
}
/// <inheritdoc />
protected override ITableGenerator MakeTable(int columns, Stream stream)
public override IDocumentGenerator AppendTable(int columns, Action<ITableGenerator> table)
{
var includePath = System.IO.Path.Join(Path, $"fig_{_lastHeading}_{++_sectionFigCnt}.tex");
var file = File.Open(includePath, FileMode.CreateNew);
string path;
var figCnt = 0;
AppendParagraph($"\\include{includePath}");
do
{
path = Path.Join(Dir, figDir, $"fig_{_lastHeading}_{++figCnt}.tex");
} while (File.Exists(path));
return new LatexIncludeTableGenerator(columns, file);
using var file = File.Open(path, FileMode.CreateNew);
using var writer = new LatexIncludeTableGenerator(columns, file);
writer.Open();
table(writer);
writer.Close();
AppendParagraph($"\\include{{{path}}}");
return this;
}
/// <inheritdoc />
@@ -85,6 +90,5 @@ public class LatexIncludeDocumentGenerator : DocumentGeneratorBase
\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}}";
}

View File

@@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ReportGeneration.Abstract;
using ReportGeneration.Interface;
@@ -9,8 +10,9 @@ namespace ReportGeneration.Generators.LatexInclude;
public class LatexIncludeTableGenerator : TableGeneratorBase
{
private const string rowSuffix = @" \\ \hline";
private const string RowSuffix = @" \\ \hline";
private static Regex _escapeRegex = new("([%_])");
public LatexIncludeTableGenerator(int columns) : base(columns)
{
}
@@ -26,22 +28,16 @@ public class LatexIncludeTableGenerator : TableGeneratorBase
protected override void OnOpen()
{
base.OnOpen();
var tabular = '|' + string.Concat(Enumerable.Repeat("l|", Columns));
var tabular = "|l|" + string.Concat(Enumerable.Repeat("c|", Columns - 1));
WriteLine(@$"
\begin{{table}}[!ht]
\centering
\caption{{Caption}}
\begin{{tabular}}{{{tabular}}}
\hline");
WriteLine(@$"\begin{{tabular}}{{{tabular}}}
\hline");
}
protected override void OnClose()
{
base.OnClose();
WriteLine(@"
\end{{tabular}}
\end{{table}}");
WriteLine(@"\end{tabular}");
}
/// <inheritdoc />
@@ -50,10 +46,10 @@ public class LatexIncludeTableGenerator : TableGeneratorBase
/// <inheritdoc />
public override ITableGenerator AppendRow(IEnumerable<string> row)
{
WriteLine(string.Join('&', row.Select(Escape)) + rowSuffix);
WriteLine(string.Join(" & ", row.Select(Escape)) + RowSuffix);
return this;
}
private string Bold(string str) => $@"\textbf{str}";
private string Escape(string str) => str.Replace("%", "\\%");
private string Bold(string str) => $@"\textbf{{{str}}}";
private string Escape(string str) => _escapeRegex.Replace(str, "\\$1");
}

View File

@@ -5,7 +5,7 @@ using System.Text;
namespace ReportGeneration.Generators;
public class MarkdownDocumentGenerator : DocumentGeneratorBase
public class MarkdownDocumentGenerator : SubItemDocumentGeneratorBase
{
/// <inheritdoc />
public MarkdownDocumentGenerator() { }