Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multiple renderers in same process #296

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Tesseract.Tests/ResultRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void CanRenderResultsIntoTextFile()
}

var expectedOutputFilename = Path.ChangeExtension(resultPath, "txt");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a Text file \"{expectedOutputFilename}\" to have been created; but non was found.");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a Text file \"{expectedOutputFilename}\" to have been created; but none was found.");
}

[Test]
Expand All @@ -55,7 +55,7 @@ public void CanRenderResultsIntoPdfFile()
}

var expectedOutputFilename = Path.ChangeExtension(resultPath, "pdf");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a PDF file \"{expectedOutputFilename}\" to have been created; but non was found.");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a PDF file \"{expectedOutputFilename}\" to have been created; but none was found.");
}

[Test]
Expand All @@ -68,7 +68,7 @@ public void CanRenderMultiplePageDocumentToPdfFile()
}

var expectedOutputFilename = Path.ChangeExtension(resultPath, "pdf");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a PDF file \"{expectedOutputFilename}\" to have been created; but non was found.");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a PDF file \"{expectedOutputFilename}\" to have been created; but none was found.");
}

[Test]
Expand All @@ -81,7 +81,7 @@ public void CanRenderResultsIntoHOcrFile()
}

var expectedOutputFilename = Path.ChangeExtension(resultPath, "hocr");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a HOCR file \"{expectedOutputFilename}\" to have been created; but non was found.");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a HOCR file \"{expectedOutputFilename}\" to have been created; but none was found.");
}

[Test]
Expand All @@ -94,7 +94,7 @@ public void CanRenderResultsIntoUnlvFile()
}

var expectedOutputFilename = Path.ChangeExtension(resultPath, "unlv");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a Unlv file \"{expectedOutputFilename}\" to have been created; but non was found.");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a Unlv file \"{expectedOutputFilename}\" to have been created; but none was found.");
}

[Test]
Expand All @@ -107,7 +107,26 @@ public void CanRenderResultsIntoBoxFile()
}

var expectedOutputFilename = Path.ChangeExtension(resultPath, "box");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a Box file \"{expectedOutputFilename}\" to have been created; but non was found.");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a Box file \"{expectedOutputFilename}\" to have been created; but none was found.");
}

[Test]
public void CanRenderResultsIntoMultipleOutputFormats()
{
var resultPath = TestResultRunFile(@"ResultRenderers\PDF\phototest");
List<RenderedFormat> formats = new List<RenderedFormat> { RenderedFormat.HOCR, RenderedFormat.PDF, RenderedFormat.TEXT };
using (var renderer = ResultRenderer.CreateRenderers(resultPath, DataPath, formats))
{
var examplePixPath = this.TestFilePath("Ocr/phototest.tif");
ProcessFile(renderer, examplePixPath);
}

var expectedOutputFilename = Path.ChangeExtension(resultPath, "pdf");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a PDF file \"{expectedOutputFilename}\" to have been created; but none was found.");
expectedOutputFilename = Path.ChangeExtension(resultPath, "hocr");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a HOCR file \"{expectedOutputFilename}\" to have been created; but none was found.");
expectedOutputFilename = Path.ChangeExtension(resultPath, "txt");
Assert.That(File.Exists(expectedOutputFilename), $"Expected a TEXT file \"{expectedOutputFilename}\" to have been created; but none was found.");
}

private void ProcessMultipageTiff(IResultRenderer renderer, string filename)
Expand Down
81 changes: 81 additions & 0 deletions src/Tesseract/ResultRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Tesseract.Internal;

namespace Tesseract
{
/// <summary>
/// Rendered formats supported by Tesseract.
/// </summary>
public enum RenderedFormat
{
TEXT, HOCR, PDF, UNLV, BOX
}

/// <summary>
/// Represents a native result renderer (e.g. text, pdf, etc).
/// </summary>
Expand All @@ -16,6 +25,78 @@ public abstract class ResultRenderer : DisposableBase, IResultRenderer
{
#region Factory Methods

/// <summary>
/// Creates renderers for specified output formats.
/// </summary>
/// <param name="outputbase"></param>
/// <param name="dataPath"></param>
/// <param name="outputFormats"></param>
/// <returns></returns>
public static IResultRenderer CreateRenderers(string outputbase, string dataPath, List<RenderedFormat> outputFormats)
{
IResultRenderer renderer = null;

foreach (RenderedFormat format in outputFormats)
{
switch (format)
{
case RenderedFormat.TEXT:
if (renderer == null)
{
renderer = CreateTextRenderer(outputbase);
}
else
{
Interop.TessApi.Native.ResultRendererInsert(((ResultRenderer)renderer).Handle, new TextResultRenderer(outputbase).Handle);
}
break;
case RenderedFormat.HOCR:
if (renderer == null)
{
renderer = CreateHOcrRenderer(outputbase);
}
else
{
Interop.TessApi.Native.ResultRendererInsert(((ResultRenderer)renderer).Handle, new HOcrResultRenderer(outputbase).Handle);
}
break;
case RenderedFormat.PDF:
//dataPath = Interop.TessApi.Native.BaseAPIGetDatapath(handle);
if (renderer == null)
{
renderer = CreatePdfRenderer(outputbase, dataPath);
}
else
{
Interop.TessApi.Native.ResultRendererInsert(((ResultRenderer)renderer).Handle, new PdfResultRenderer(outputbase, dataPath).Handle);
}
break;
case RenderedFormat.BOX:
if (renderer == null)
{
renderer = CreateBoxRenderer(outputbase);
}
else
{
Interop.TessApi.Native.ResultRendererInsert(((ResultRenderer)renderer).Handle, new BoxResultRenderer(outputbase).Handle);
}
break;
case RenderedFormat.UNLV:
if (renderer == null)
{
renderer = CreateUnlvRenderer(outputbase);
}
else
{
Interop.TessApi.Native.ResultRendererInsert(((ResultRenderer)renderer).Handle, new UnlvResultRenderer(outputbase).Handle);
}
break;
}
}

return renderer;
}

/// <summary>
/// Creates a <see cref="IResultRenderer">result renderer</see> that render that generates a searchable
/// pdf file from tesseract's output.
Expand Down