Added latex include table generator
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using Common.Extensions;
|
using Common.Extensions;
|
||||||
using Ocr.Report.Models;
|
using Ocr.Report.Models;
|
||||||
using ReportGeneration.Generators;
|
using ReportGeneration.Generators;
|
||||||
|
using ReportGeneration.Generators.LatexInclude;
|
||||||
|
|
||||||
namespace Ocr.Report;
|
namespace Ocr.Report;
|
||||||
|
|
||||||
@@ -21,7 +22,7 @@ internal static class Program
|
|||||||
|
|
||||||
var path = Path.GetFullPath("report.html");
|
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)
|
using var report = new ReportGenerator("OCR Report", document, scans)
|
||||||
.AddComparison("Processing summary (Average)", v =>
|
.AddComparison("Processing summary (Average)", v =>
|
||||||
{
|
{
|
||||||
|
|||||||
+90
@@ -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
|
||||||
|
|
||||||
|
/// <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 = heading;
|
||||||
|
return AppendParagraph(heading);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
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}}";
|
||||||
|
}
|
||||||
+59
@@ -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}}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override ITableGenerator AppendHeader(IEnumerable<string> row) => AppendRow(row.Select(Bold));
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override ITableGenerator AppendRow(IEnumerable<string> row)
|
||||||
|
{
|
||||||
|
WriteLine(string.Join('&', row.Select(Escape)) + rowSuffix);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string Bold(string str) => $@"\textbf{str}";
|
||||||
|
private string Escape(string str) => str.Replace("%", "\\%");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user