Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Recognize text in scans with Tesseract (#288), work in progress
cre8ive-marketing436991e14261
diff --git a/build/fetch-engines.ps1 b/build/fetch-engines.ps1index a8383acf..e0926784 100644--- a/build/fetch-engines.ps1+++ b/build/fetch-engines.ps1@@ -18,6 +18,8 @@ffmpeg/ FFmpeg (GPL-3.0 build, separate program), video and audio; bin/ffmpeg.exe + bin/ffprobe.execalibre/ calibre (GPL-3.0, separate program), extracted from the official MSI via an administrativeinstall: ebook-convert for LIT, LRF, PDB, ... and MOBI / AZW3 output+ tesseract/ Tesseract OCR (Apache-2.0, separate program), UB Mannheim's Windows build unpacked from its+ installer with 7-Zip (never run); the language data is downloaded by the app when neededEvery download is pinned to a version and verified with SHA-256 (src/Filee.Engines/Infrastructure/engines.json,shared with the app). Downloads are cached in build/.cache.@@ -253,6 +255,20 @@ if ('ghostscript' -in $selected) {Copy-VcRuntime $target}+if ('tesseract' -in $selected) {+ # Trimmed like EngineInstaller.TrimTesseract: no training programs, manuals, installer files or language data.+ $setup = Get-Engine 'tesseract'+ $sevenZip = Join-Path $Destination '7zip\7z.exe'+ if (-not (Test-Path $sevenZip)) { throw 'Tesseract is unpacked with engines/7zip: use -Only 7zip,tesseract.' }+ $target = Join-Path $Destination 'tesseract'+ Reset-Folder $target+ & $sevenZip x -y "-o$target" $setup | Out-Null+ if ($LASTEXITCODE -ne 0) { throw "7-Zip could not unpack the Tesseract installer (exit code $LASTEXITCODE)." }+ foreach ($folder in '$PLUGINSDIR', 'tessdata') { Remove-Item -LiteralPath (Join-Path $target $folder) -Recurse -Force -ErrorAction SilentlyContinue }+ Get-ChildItem $target -File | Where-Object { $_.Extension -in '.exe', '.html' -and $_.Name -ne 'tesseract.exe' } | Remove-Item -Force+ if (-not (Test-Path (Join-Path $target 'tesseract.exe'))) { throw 'tesseract.exe not found in the installer.' }+}+if ('calibre' -in $selected) {$msi = Get-Engine 'calibre'$tmp = Join-Path $cache 'calibre-admin'diff --git a/src/Filee.App/Services/PresetAvailability.cs b/src/Filee.App/Services/PresetAvailability.csindex 55ee80f6..7ea02e06 100644--- a/src/Filee.App/Services/PresetAvailability.cs+++ b/src/Filee.App/Services/PresetAvailability.cs@@ -113,15 +113,16 @@ public sealed class PresetAvailabilitybool Possible(RoutePlanner planner) => formats.Any(f =>{var target = TargetFor(f);- if (merge && f.Id == "pdf" && !turns)+ if (merge && f.Id == "pdf" && !turns && !preset.RecognizesTextOf(f.Id))return true; // merged as it isvar frames = preset.SavesFramesOf(f.Id);var motion = preset.ExtractsMotionOf(f.Id);var live = preset.MakesLivePhotoOf(f.Id);+ var ocr = preset.RecognizesTextOf(f.Id);if (!turns)- return planner.CanConvert(f.Id, target, frames, motionPhoto: motion, livePhoto: live);+ return planner.CanConvert(f.Id, target, frames, motionPhoto: motion, livePhoto: live, ocr: ocr);// Rotate / flip: a route only counts when an engine on it turns images (not a PDF or a video copied).- if (planner.Plan(f.Id, target, frames, motionPhoto: motion, livePhoto: live) is not { } route)+ if (planner.Plan(f.Id, target, frames, motionPhoto: motion, livePhoto: live, ocr: ocr) is not { } route)return false;if (route.FirstTransformStep() >= 0)return true;diff --git a/src/Filee.Core/Conversion/IConverter.cs b/src/Filee.Core/Conversion/IConverter.csindex 3a84b9d8..4212cae5 100644--- a/src/Filee.Core/Conversion/IConverter.cs+++ b/src/Filee.Core/Conversion/IConverter.cs@@ -30,7 +30,7 @@ namespace Filee.Core.Conversion;/// <param name="FrameSequence">/// The edge writes every frame of a video as numbered pictures into a folder (<see cref="Presets.MediaOptions.SaveEveryFrame"/>)./// It is never part of an ordinary route (a video → JPG stays one still frame), and with-/// <see cref="RoutePlanner.Plan(string, string, bool, bool, bool, bool)"/> asking for frames it is the whole route.+/// <see cref="RoutePlanner.Plan(string, string, bool, bool, bool, bool, bool)"/> asking for frames it is the whole route./// </param>/// <param name="Unshaped">/// The engine draws text one character after the other, left to right, without complex-script shaping (no joined@@ -41,16 +41,22 @@ namespace Filee.Core.Conversion;/// <param name="MotionPhoto">/// The edge takes the video out of a Live / Motion Photo (<see cref="Presets.MediaOptions.ExtractMotionPhoto"/>). It is/// never part of an ordinary route (a photo → MP4 is otherwise a video of the still picture), and with-/// <see cref="RoutePlanner.Plan(string, string, bool, bool, bool, bool)"/> asking for it it is the whole route.+/// <see cref="RoutePlanner.Plan(string, string, bool, bool, bool, bool, bool)"/> asking for it it is the whole route./// </param>/// <param name="LivePhoto">/// The edge makes a Live / Motion Photo of a short clip of a video (<see cref="Presets.MediaOptions.LivePhoto"/>). It is/// never part of an ordinary route (a video → JPG is otherwise one still frame), and with-/// <see cref="RoutePlanner.Plan(string, string, bool, bool, bool, bool)"/> asking for it it is the whole route.+/// <see cref="RoutePlanner.Plan(string, string, bool, bool, bool, bool, bool)"/> asking for it it is the whole route.+/// </param>+/// <param name="Ocr">+/// The edge recognizes the text of a scan or a picture and writes a searchable PDF (<see cref="Presets.PdfOptions.Ocr"/>).+/// It is never part of an ordinary route, and with <see cref="RoutePlanner.Plan(string, string, bool, bool, bool, bool, bool)"/>+/// asking for it the route starts with it./// </param>public readonly record struct ConversionEdge(string From, string To, int Cost = 10, bool ImageSourcesOnly = false, bool NotForImageSources = false,- bool DirectOnly = false, bool FrameSequence = false, bool Unshaped = false, bool MotionPhoto = false, bool LivePhoto = false);+ bool DirectOnly = false, bool FrameSequence = false, bool Unshaped = false, bool MotionPhoto = false, bool LivePhoto = false,+ bool Ocr = false);/// <summary>/// A conversion an engine refuses with a reason the user can act on, shown in their language (the file'sdiff --git a/src/Filee.Core/Conversion/JobQueue.cs b/src/Filee.Core/Conversion/JobQueue.csindex 9cf06228..11f180d5 100644--- a/src/Filee.Core/Conversion/JobQueue.cs+++ b/src/Filee.Core/Conversion/JobQueue.cs@@ -236,8 +236,9 @@ public sealed class JobQueue : IAsyncDisposablevar file = job.Files[i];if (Stopped(file))return;- // An empty PDF goes the long way, where it fails as empty instead of breaking the whole merge.- if (FormatRegistry.Detect(file.SourcePath)?.Id == "pdf" && !IsEmptyFile(file.SourcePath))+ // An empty PDF goes the long way, where it fails as empty instead of breaking the whole merge; so does+ // one whose text is recognized first.+ if (FormatRegistry.Detect(file.SourcePath)?.Id == "pdf" && !IsEmptyFile(file.SourcePath) && !job.Preset.RecognizesTextOf("pdf")){// Already a PDF, merged as it is: its pages can't be turned (only images going into it can).if (job.Preset.Image.TurnsOrMirrors)@@ -497,7 +498,10 @@ public sealed class JobQueue : IAsyncDisposablevar frames = finalOutput && job.Preset.SavesFramesOf(source.Id);var motionPhoto = finalOutput && job.Preset.ExtractsMotionOf(source.Id);var livePhoto = finalOutput && job.Preset.MakesLivePhotoOf(source.Id);- var route = planner.Plan(source.Id, target, frames, motionPhoto: motionPhoto, livePhoto: livePhoto);+ // "Recognize text": Tesseract writes a searchable PDF first, read on into DOCX, TXT, … from there. Also for the+ // PDFs that are merged into one.+ var ocr = (finalOutput || target == "pdf") && job.Preset.RecognizesTextOf(source.Id);+ var route = planner.Plan(source.Id, target, frames, motionPhoto: motionPhoto, livePhoto: livePhoto, ocr: ocr);if (route is null)return Fail(file, "error.no_route", $"{source.Id} → {target}");@@ -508,7 +512,7 @@ public sealed class JobQueue : IAsyncDisposable{_log.LogInformation("{File} has {Script} text, which {Engine} can't draw: planning a route without it",file.SourcePath, script, route.Steps.First(s => s.Unshaped).Converter.DisplayName);- route = planner.Plan(source.Id, target, frames, shaping: true);+ route = planner.Plan(source.Id, target, frames, shaping: true, ocr: ocr);if (route is null)return Fail(file, "error.needs_shaping", script);}diff --git a/src/Filee.Core/Conversion/RoutePlanner.cs b/src/Filee.Core/Conversion/RoutePlanner.csindex b19047eb..4c89f86f 100644--- a/src/Filee.Core/Conversion/RoutePlanner.cs+++ b/src/Filee.Core/Conversion/RoutePlanner.cs@@ -72,11 +72,20 @@ public sealed class RoutePlanner/// Make a Live / Motion Photo of a video (<see cref="Presets.Preset.MakesLivePhotoOf"/>): the route is one/// <see cref="ConversionEdge.LivePhoto"/> edge, or none. Without it those edges are never taken./// </param>- public Route? Plan(string from, string to, bool frames = false, bool shaping = false, bool motionPhoto = false, bool livePhoto = false)+ /// <param name="ocr">+ /// Recognize the text first (<see cref="Presets.Preset.RecognizesTextOf"/>): an <see cref="ConversionEdge.Ocr"/>+ /// edge to PDF, from the file itself or from a PNG of a picture Tesseract can't read, then the cheapest route from+ /// that PDF on. Without it those edges are never taken.+ /// </param>+ public Route? Plan(string from, string to, bool frames = false, bool shaping = false, bool motionPhoto = false, bool livePhoto = false,+ bool ocr = false){from = from.ToLowerInvariant();to = to.ToLowerInvariant();+ if (ocr)+ return PlanWithOcr(from, to, shaping);+if (frames || motionPhoto || livePhoto){// One of them: frames, then the video of a photo, then a photo of a video.@@ -96,7 +105,7 @@ public sealed class RoutePlannerif (from == to){var best = EdgesFrom(from)- .Where(e => e.Edge.To == to && !e.Edge.FrameSequence && !e.Edge.MotionPhoto && !e.Edge.LivePhoto)+ .Where(e => e.Edge.To == to && !e.Edge.FrameSequence && !e.Edge.MotionPhoto && !e.Edge.LivePhoto && !e.Edge.Ocr).OrderBy(e => e.Cost).FirstOrDefault();return best.Converter is null@@ -125,7 +134,7 @@ public sealed class RoutePlannerforeach (var (converter, edge, edgeCost) in EdgesFrom(node)){- if (edge.To == edge.From || edge.FrameSequence || edge.MotionPhoto || edge.LivePhoto+ if (edge.To == edge.From || edge.FrameSequence || edge.MotionPhoto || edge.LivePhoto || edge.Ocr|| (edge.ImageSourcesOnly && !imageSource) || (edge.NotForImageSources && imageSource)|| (edge.Unshaped && shaping)|| (edge.DirectOnly && (node != from || edge.To != to)))@@ -155,8 +164,41 @@ public sealed class RoutePlanner/// <param name="frames">Every frame of a video as pictures (see <see cref="Plan"/>).</param>/// <param name="motionPhoto">The video of a Live / Motion Photo (see <see cref="Plan"/>).</param>/// <param name="livePhoto">A Live / Motion Photo of a video (see <see cref="Plan"/>).</param>- public bool CanConvert(string from, string to, bool frames = false, bool motionPhoto = false, bool livePhoto = false) =>- Plan(from, to, frames, motionPhoto: motionPhoto, livePhoto: livePhoto) is not null;+ /// <param name="ocr">Recognize the text first (see <see cref="Plan"/>).</param>+ public bool CanConvert(string from, string to, bool frames = false, bool motionPhoto = false, bool livePhoto = false, bool ocr = false) =>+ Plan(from, to, frames, motionPhoto: motionPhoto, livePhoto: livePhoto, ocr: ocr) is not null;++ private Route? PlanWithOcr(string from, string to, bool shaping)+ {+ var steps = new List<RouteStep>();+ var cost = 0;+ var source = from;+ if (OcrEdge(from).Converter is null)+ {+ // A picture Tesseract can't open (HEIC, PSD, …) becomes a PNG first.+ if (FormatRegistry.FindById(from)?.Category != FormatCategory.Image || Plan(from, "png", shaping: shaping) is not { } toPng)+ return null;+ steps.AddRange(toPng.Steps);+ cost += toPng.Cost;+ source = "png";+ }+ var ocr = OcrEdge(source);+ if (ocr.Converter is null)+ return null;+ steps.Add(new RouteStep(ocr.Converter, source, "pdf"));+ cost += ocr.Cost;+ if (to != "pdf")+ {+ if (Plan("pdf", to, shaping: shaping) is not { } rest)+ return null;+ steps.AddRange(rest.Steps);+ cost += rest.Cost;+ }+ return new Route(steps, cost);+ }++ private (IConverter Converter, ConversionEdge Edge, int Cost) OcrEdge(string from) =>+ EdgesFrom(from).Where(e => e.Edge.Ocr && e.Edge.To == "pdf").OrderBy(e => e.Cost).FirstOrDefault();private IReadOnlyList<(IConverter Converter, ConversionEdge Edge, int Cost)> EdgesFrom(string format) =>_edgesFrom.TryGetValue(format, out var edges) ? edges : [];diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.csindex ed631510..29f2d593 100644--- a/src/Filee.Core/Presets/Preset.cs+++ b/src/Filee.Core/Presets/Preset.cs@@ -164,6 +164,15 @@ public sealed class PresetMediaOptions.LivePhotoTargets(Media.LivePhoto).Contains(TargetFormat)&& Formats.FormatRegistry.FindById(sourceFormat)?.Category == Formats.FormatCategory.Video;+ /// <summary>+ /// True if converting a <paramref name="sourceFormat"/> file recognizes its text first (<see cref="PdfOptions.Ocr"/>):+ /// the file is a PDF or a picture and the target one of <see cref="PdfOptions.OcrTargets"/>.+ /// </summary>+ public bool RecognizesTextOf(string sourceFormat) =>+ Pdf.Ocr+ && PdfOptions.OcrTargets.Contains(TargetFormat)+ && (sourceFormat == "pdf" || Formats.FormatRegistry.FindById(sourceFormat)?.Category == Formats.FormatCategory.Image);+/// <summary>True for a <see cref="PresetKind.Tool"/>.</summary>[JsonIgnore]public bool IsTool => Kind == PresetKind.Tool;@@ -409,6 +418,19 @@ public sealed class PdfOptions/// </summary>public bool CompressWithGhostscript { get; set; }+ /// <summary>Formats text read from scans can go into (<see cref="Ocr"/>): those Filee writes from a PDF's text itself.</summary>+ public static readonly IReadOnlyList<string> OcrTargets = ["pdf", "docx", "txt", "md", "hwpx"];++ /// <summary>+ /// Recognize the text of scanned pages and pictures with Tesseract (<see cref="Preset.RecognizesTextOf"/>): a PDF+ /// gets a searchable text layer, a DOCX, TXT, Markdown or HWPX gets the text. Pages that already have text stay as+ /// they are.+ /// </summary>+ public bool Ocr { get; set; }++ /// <summary>With <see cref="Ocr"/>: the language of the text, as a Tesseract code (<c>eng</c>, <c>deu</c>, <c>chi_sim</c>).</summary>+ public string OcrLanguage { get; set; } = "eng";+public PdfOptions Clone() => (PdfOptions)MemberwiseClone();}diff --git a/src/Filee.Engines/EngineRegistry.cs b/src/Filee.Engines/EngineRegistry.csindex b9c0f010..da4daf95 100644--- a/src/Filee.Engines/EngineRegistry.cs+++ b/src/Filee.Engines/EngineRegistry.cs@@ -15,6 +15,7 @@ using Filee.Engines.Infrastructure;using Filee.Engines.Magick;using Filee.Engines.Media;using Filee.Engines.Models3D;+using Filee.Engines.Ocr;using Filee.Engines.Office;using Filee.Engines.Pdf;using Filee.Engines.Subtitles;@@ -66,6 +67,7 @@ public static class EngineRegistrynew EbookConverter(),new PandocConverter(),new GhostscriptConverter(),+ new TesseractConverter(),new CalibreConverter(),new LibreOfficeConverter(env, loggers?.CreateLogger<LibreOfficeConverter>()),];diff --git a/src/Filee.Engines/Filee.Engines.csproj b/src/Filee.Engines/Filee.Engines.csprojindex a0219929..61787e43 100644--- a/src/Filee.Engines/Filee.Engines.csproj+++ b/src/Filee.Engines/Filee.Engines.csproj@@ -45,5 +45,7 @@<EmbeddedResource Include="Hwp\Hwpx\blank.hwpx" LogicalName="Filee.Engines.Hwpx.blank.hwpx" /><!-- Pinned downloads of optional engines, also read by build/fetch-engines.ps1. --><EmbeddedResource Include="Infrastructure\engines.json" LogicalName="Filee.Engines.engines.json" />+ <!-- Pinned Tesseract language data, downloaded when an OCR preset needs it (Ocr/OcrLanguages.cs). -->+ <EmbeddedResource Include="Infrastructure\ocr-languages.json" LogicalName="Filee.Engines.ocr-languages.json" /></ItemGroup></Project>diff --git a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.csindex b98e78d0..a1a26f80 100644--- a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs+++ b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs@@ -105,9 +105,13 @@ internal static class DocumentReaders/// <summary>Reads <paramref name="path"/> (of format <paramref name="format"/>) into the document model.</summary>/// <param name="workDirectory">Scratch folder of the job: extracted pictures go into a new folder inside it.</param>- public static async Task<HDocument> ReadAsync(string path, string format, string workDirectory, CancellationToken cancellationToken)+ /// <param name="ocrLayer">A PDF's OCR layer is its text (<see cref="PdfDocumentReader.ReadWithOcrLayer"/>).</param>+ public static async Task<HDocument> ReadAsync(string path, string format, string workDirectory, CancellationToken cancellationToken,+ bool ocrLayer = false){var media = Path.Combine(workDirectory, $"media-{Guid.NewGuid():N}");+ if (ocrLayer && format == "pdf")+ return await Task.Run(() => PdfDocumentReader.ReadWithOcrLayer(path, media), cancellationToken);if (BuiltIn.TryGetValue(format, out var reader))return await Task.Run(() => reader(path, media), cancellationToken);if (ThroughPandoc.TryGetValue(format, out var pandocReader))diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.csindex bf6f570e..e3ff067d 100644--- a/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.cs+++ b/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.cs@@ -45,7 +45,8 @@ public sealed class HwpxConverter : IConverterpublic async Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken){progress?.Report(0.1);- var document = await DocumentReaders.ReadAsync(step.InputPath, step.From, step.WorkDirectory, cancellationToken);+ var document = await DocumentReaders.ReadAsync(step.InputPath, step.From, step.WorkDirectory, cancellationToken,+ ocrLayer: step.Preset.Pdf.Ocr);progress?.Report(0.6);var output = step.Output.Allocate("hwpx");diff --git a/src/Filee.Engines/Infrastructure/EngineDownloads.cs b/src/Filee.Engines/Infrastructure/EngineDownloads.csindex 7ae3e302..d1f334b1 100644--- a/src/Filee.Engines/Infrastructure/EngineDownloads.cs+++ b/src/Filee.Engines/Infrastructure/EngineDownloads.cs@@ -8,7 +8,7 @@ namespace Filee.Engines.Infrastructure;/// <summary>One download from engines.json.</summary>/// <param name="Kind">-/// Archive format: zip, tar.gz, tar.xz, dmg, msi, oxt or conda.+/// Archive format: zip, tar.gz, tar.xz, dmg, msi, oxt, conda or nsis./// </param>/// <param name="Size">Download size in bytes.</param>public sealed record EngineComponent(string Id, string Version, string Url, string Sha256, long Size, string Kind);@@ -44,6 +44,7 @@ public static class EngineDownloadsnew("ghostscript", OperatingSystem.IsWindows() ? ["ghostscript", "vcruntime"] : ["ghostscript"], 31_000_000, ["ghostscript"]),new("ffmpeg", ["ffmpeg"], 272_000_000, ["ffmpeg"]),new("calibre", ["calibre"], 663_000_000, ["calibre"]),+ new("tesseract", ["tesseract"], 168_000_000, ["tesseract"]),];/// <summary>Total download size of a package in bytes.</summary>@@ -60,6 +61,7 @@ public static class EngineDownloads{"ffmpeg" => "Install FFmpeg with Homebrew (brew install ffmpeg), then check again or select its bin folder.","ghostscript" => "Install Ghostscript with Homebrew (brew install ghostscript), then check again or select its bin folder.",+ "tesseract" => "Install Tesseract with Homebrew (brew install tesseract), then check again or select its bin folder.","calibre" => "The calibre Filee downloads needs macOS 14 or later. Install calibre 7.26 from calibre-ebook.com and select its .app folder.",_ => $"No Filee download is available for {RuntimeIdentifier}. Install {package.Id} for this Mac and select its program or .app folder.",};@@ -68,6 +70,7 @@ public static class EngineDownloads{"libreoffice" => "Install LibreOffice using your distribution's package manager (Ubuntu/Debian: sudo apt install libreoffice), then check again. HWP import through LibreOffice also needs Java and the H2Orestart extension.","ghostscript" => "Install Ghostscript using your distribution's package manager (Ubuntu/Debian: sudo apt install ghostscript), then check again.",+ "tesseract" => "Install Tesseract using your distribution's package manager (Ubuntu/Debian: sudo apt install tesseract-ocr), then check again.",_ => $"No Filee download is available for {RuntimeIdentifier}. Install {package.Id} using your distribution's package manager and select its program folder.",};return $"No Filee download is available for {RuntimeIdentifier}. Select a compatible installed copy.";@@ -79,10 +82,11 @@ public static class EngineDownloads/// <summary>/// Ticked by default in the installer and in the first-run choice. Large downloads are offered, not suggested:/// LibreOffice is only needed for older formats (DOC, XLS, PPT, OpenDocument), FFmpeg (~100 MB) only for video and- /// audio, calibre (~230 MB) only for rare e-book formats and Kindle output. Ghostscript is small but only needed for- /// EPS / PostScript.+ /// audio, calibre (~230 MB) only for rare e-book formats and Kindle output. Ghostscript and Tesseract are small but+ /// only needed for EPS / PostScript and for scans./// </summary>- public static bool IsSuggested(EnginePackage package) => CanDownload(package) && DownloadSize(package) < SuggestLimit && package.Id != "ghostscript";+ public static bool IsSuggested(EnginePackage package) =>+ CanDownload(package) && DownloadSize(package) < SuggestLimit && package.Id is not ("ghostscript" or "tesseract");/// <param name="macOSVersion">The running macOS; downloads whose minimumMacOS is newer are left out. Null keeps all.</param>public static IReadOnlyDictionary<string, EngineComponent> ComponentsForRuntime(string runtimeIdentifier, Version? macOSVersion = null)diff --git a/src/Filee.Engines/Infrastructure/EngineEnvironment.cs b/src/Filee.Engines/Infrastructure/EngineEnvironment.csindex a94979e6..abc9c0d8 100644--- a/src/Filee.Engines/Infrastructure/EngineEnvironment.cs+++ b/src/Filee.Engines/Infrastructure/EngineEnvironment.cs@@ -1,6 +1,6 @@// Shared context for engines: where Filee's own engine copies live and where engines keep their state.// Built-in engines never use software installed on the system (Microsoft Office, Hancom Office). The optional-// engines (FFmpeg, calibre, Pandoc, Ghostscript, LibreOffice) are looked up in this order: a copy the user chose in+// engines (FFmpeg, calibre, Pandoc, Ghostscript, LibreOffice, Tesseract) are looked up in this order: a copy the user chose in// Settings → Engines (OwnCopies), Filee's own copy (installer or download, the tested version), then one already on// the PC (on the PATH or where its installer puts it), so nobody has to download a second FFmpeg they already have.@@ -84,6 +84,7 @@ public sealed class EngineEnvironment(string dataDirectory)["calibre"] = [windows ? "ebook-convert.exe" : "ebook-convert"],["rhwp"] = [windows ? "rhwp.exe" : "rhwp"],["7zip"] = [windows ? "7z.exe" : "7zz"],+ ["tesseract"] = [windows ? "tesseract.exe" : "tesseract"],};public static string ProgramName(string name) => OperatingSystem.IsWindows() ? name@@ -231,6 +232,9 @@ public sealed class EngineEnvironment(string dataDirectory)case "calibre":folders.Add(Path.Combine(root, "Calibre2"));break;+ case "tesseract":+ folders.Add(Path.Combine(root, "Tesseract-OCR"));+ break;case "ghostscript":// C:\Program Files\gs\gs10.04.0\bin, newest firsttrydiff --git a/src/Filee.Engines/Infrastructure/EngineInstaller.cs b/src/Filee.Engines/Infrastructure/EngineInstaller.csindex 12fe5fac..7b7b50f2 100644--- a/src/Filee.Engines/Infrastructure/EngineInstaller.cs+++ b/src/Filee.Engines/Infrastructure/EngineInstaller.cs@@ -7,6 +7,7 @@// entries, no admin rights. Parts headless LibreOffice never uses (help, gallery, most dictionaries) are removed.// * conda-forge packages (Ghostscript, the Microsoft C++ runtime) are zip files holding zstd-compressed tarballs;// only their Windows binaries (Library/bin) are unpacked, with a managed decompressor (no 7-Zip, no installer).+// * NSIS installers (Tesseract) are unpacked with Filee's own 7-Zip, never run: files only, no registry entries.// * A marker file with the component's hash records what is installed, so an engines.json update is detected.using System.Diagnostics;@@ -205,6 +206,20 @@ public sealed class EngineInstallerUnpackConda(file, staging, cancellationToken);Replace(component.Id, staging);break;+ case "traineddata":+ // One Tesseract language (OcrLanguages): the file as it is, in a folder of its own.+ Directory.CreateDirectory(staging);+ File.Copy(file, Path.Combine(staging, component.Id + ".traineddata"));+ Replace(component.Id, staging);+ break;+ case "nsis":+ var sevenZip = Archives.SevenZip.Locate()+ ?? throw new InvalidOperationException($"{component.Id} needs Filee's 7-Zip, which is missing.");+ Directory.CreateDirectory(staging);+ await sevenZip.ExtractAsync(file, staging, [], _root, cancellationToken);+ TrimTesseract(staging);+ Replace(component.Id, staging);+ break;case "oxt":// LibreOffice registers bundled extensions (plain folders under share/extensions) on start-up.var extensions = Path.Combine(FolderOf("libreoffice"), "share", "extensions");@@ -381,6 +396,26 @@ public sealed class EngineInstaller}}+ /// <summary>+ /// Removes what OCR never uses (~80 MB): the training programs and their manuals, the installer's own files and+ /// the bundled language data (Filee downloads its own, see OcrLanguages). tesseract.exe, its DLLs and the+ /// licenses in doc/ stay.+ /// </summary>+ internal static void TrimTesseract(string root)+ {+ foreach (var folder in new[] { "$PLUGINSDIR", "tessdata" })+ {+ var path = Path.Combine(root, folder);+ if (Directory.Exists(path))+ Directory.Delete(path, recursive: true);+ }+ foreach (var file in Directory.EnumerateFiles(root, "*.exe").Concat(Directory.EnumerateFiles(root, "*.html")).ToList())+ {+ if (!string.Equals(Path.GetFileName(file), "tesseract.exe", StringComparison.OrdinalIgnoreCase))+ File.Delete(file);+ }+ }+/// <summary>/// Unpacks the Windows binaries of a conda package (.conda = zip with pkg-*.tar.zst): the files under Library/bin/// land directly in <paramref name="target"/>. Resources such as Ghostscript's fonts are compiled into its DLL.@@ -443,7 +478,7 @@ public sealed class EngineInstaller/// <summary>Makes <paramref name="source"/> the engine folder, replacing a previous version.</summary>private void Replace(string id, string source){- RequiredProgramFolder(id, source, _components[id].Kind is "msi" or "conda" || OperatingSystem.IsWindows());+ RequiredProgramFolder(id, source, _components[id].Kind is "msi" or "conda" or "nsis" || OperatingSystem.IsWindows());var target = FolderOf(id);if (Directory.Exists(target))Directory.Delete(target, recursive: true);diff --git a/src/Filee.Engines/Infrastructure/engines.json b/src/Filee.Engines/Infrastructure/engines.jsonindex 6be3c151..d2158b02 100644--- a/src/Filee.Engines/Infrastructure/engines.json+++ b/src/Filee.Engines/Infrastructure/engines.json@@ -70,6 +70,13 @@"sha256": "0F96AE06165C2419607C1C66726C091A78E89E08EA69F077CF3E2C884E800853","size": 226549760,"kind": "msi"+ },+ "tesseract": {+ "version": "5.4.0.20240606",+ "url": "https://github.com/UB-Mannheim/tesseract/releases/download/v5.4.0.20240606/tesseract-ocr-w64-setup-5.4.0.20240606.exe",+ "sha256": "C885FFF6998E0608BA4BB8AB51436E1C6775C2BAFC2559A19B423E18678B60C9",+ "size": 50175248,+ "kind": "nsis"}},"platforms": {diff --git a/src/Filee.Engines/Infrastructure/ocr-languages.json b/src/Filee.Engines/Infrastructure/ocr-languages.jsonnew file mode 100644index 00000000..9bdfd216--- /dev/null+++ b/src/Filee.Engines/Infrastructure/ocr-languages.json@@ -0,0 +1,351 @@+{+ "_comment": "Tesseract language data (tessdata_fast 4.1.0, Apache-2.0), downloaded on demand when an OCR preset needs a language: pinned URL + SHA-256 per file. culture names the language in the UI.",+ "url": "https://raw.githubusercontent.com/tesseract-ocr/tessdata_fast/4.1.0/{0}.traineddata",+ "languages": {+ "eng": {+ "culture": "en",+ "sha256": "7D4322BD2A7749724879683FC3912CB542F19906C83BCC1A52132556427170B2",+ "size": 4113088+ },+ "deu": {+ "culture": "de",+ "sha256": "19D219BBB6672C869D20A9636C6816A81EB9A71796CB93EBE0CB1530E2CDB22D",+ "size": 1525436+ },+ "fra": {+ "culture": "fr",+ "sha256": "CED037562E8C80C13122DECE28DD477D399AF80911A28791A66A63AC1E3445CA",+ "size": 1130365+ },+ "spa": {+ "culture": "es",+ "sha256": "6F2E04D02774A18F01BED44B1111F2CD7F3BA7AC9DC4373CD3F898A40EA6B464",+ "size": 2294433+ },+ "por": {+ "culture": "pt",+ "sha256": "C4932B937207A9514B7514D518B931A99938C02A28A5A5A553F8599ED58B7DEB",+ "size": 1982756+ },+ "ita": {+ "culture": "it",+ "sha256": "B8F89E1E785118DAC4D51AE042C029A64EDB5C3EE42EF73027A6D412748D8827",+ "size": 2701314+ },+ "nld": {+ "culture": "nl",+ "sha256": "CED0E5E046A84C908A6AA7ACCBEF9A232C4A5D9A8276691B81C6EE64D02963F6",+ "size": 6050296+ },+ "pol": {+ "culture": "pl",+ "sha256": "C4476CDBC0E33D898D32345122B7BE1CBF85ACE15F920F06C7714756E1EF79B2",+ "size": 4765518+ },+ "ces": {+ "culture": "cs",+ "sha256": "934BCAF97EF3348413263331131C9FA7F55F30DB333C711929C124FB635F7E1B",+ "size": 3795684+ },+ "slk": {+ "culture": "sk",+ "sha256": "FBCC400A9C74C6A13D922FCB1211B655D1B165387B675ED75CD2DBD756B974A5",+ "size": 4427661+ },+ "slv": {+ "culture": "sl",+ "sha256": "B937632C17CE5FDF20535D25FEEC044DA10A0C4C8234F4302D5606FD671CF60D",+ "size": 3003829+ },+ "hrv": {+ "culture": "hr",+ "sha256": "9E515D9832CE259DBAB550B1CC6B998F8B929FAF2EDACAACA981B05ADB130571",+ "size": 4103348+ },+ "srp": {+ "culture": "sr-Cyrl",+ "sha256": "AA41AE3D9CC705E60D398AB38A5C3CC8B772C0D420C7D4F0859BEB13D0E321B6",+ "size": 2149931+ },+ "bos": {+ "culture": "bs",+ "sha256": "6CC8CC87CF1AFBFA6A41FEBB725DBADB14BED96A46685906440A6EB8A7892F04",+ "size": 2498242+ },+ "ron": {+ "culture": "ro",+ "sha256": "9ADFDE6B51BA4B97EFD10EA37C3070FD3FC2BAD7815E81F5C3C198CD96216CC9",+ "size": 2376323+ },+ "hun": {+ "culture": "hu",+ "sha256": "35067E7CFE102DCDC953F9A758FDFAA6296B17A1EE6D874EE780FA306430B9FB",+ "size": 5296273+ },+ "bul": {+ "culture": "bg",+ "sha256": "AEBC9B0FCC8CFAF8A9F38A02BB7B85052BD850744696A2C11CF0081820E5B21E",+ "size": 1675212+ },+ "ell": {+ "culture": "el",+ "sha256": "4FBA8A0B461038D51F1C20D043D4F2AC38C4E778F1B90830847F7BD8FA3BA726",+ "size": 1419514+ },+ "rus": {+ "culture": "ru",+ "sha256": "E16E5E036CCE1D9EC2B00063CF8B54472625B9E14D893A169E2B0DEDEB4DF225",+ "size": 3861738+ },+ "ukr": {+ "culture": "uk",+ "sha256": "D59E53E2BDED32F4445F124B4B00240FCAC7E8044C003AB822CCB94F0B3DB59B",+ "size": 3825102+ },+ "bel": {+ "culture": "be",+ "sha256": "9C6668A0B202F3DCFE074B64620D108E1902CA7498A40B5A11B4A3DA6112D58F",+ "size": 3692098+ },+ "tur": {+ "culture": "tr",+ "sha256": "7393381111E1152420FC4092CB44EEF4237580D21B92BF30D7D221AAD192C6B7",+ "size": 4550554+ },+ "swe": {+ "culture": "sv",+ "sha256": "F7304988D41F833EFEBCC2D529DF54B1903ECEBBC3DA1FAABD19A0FDDD4FE586",+ "size": 4167034+ },+ "dan": {+ "culture": "da",+ "sha256": "ACB1FD074487A31D1294FCDFD7D7C673467FFD8AEACB2CCD61EBCBF04EB4E2FA",+ "size": 2580059+ },+ "nor": {+ "culture": "nb",+ "sha256": "0451EB4F8049AE78196806BF878A389A2F40F1386FE038568CF4441226BA6EF2",+ "size": 3610079+ },+ "fin": {+ "culture": "fi",+ "sha256": "61A04CD62B507C3D9AE0E1CDA399E6715EBF49DEA9DF47897C8ACDCD3BD3E13C",+ "size": 7865732+ },+ "isl": {+ "culture": "is",+ "sha256": "5EC828C363F3F0062C3CAF08216677CF5F3C0C981B05FAB445123A16B98F10A9",+ "size": 2278973+ },+ "est": {+ "culture": "et",+ "sha256": "515D4A773682B286369511E83FE412BCFF16A92A886F99C761C1D760A7E30456",+ "size": 4458101+ },+ "lav": {+ "culture": "lv",+ "sha256": "7D9EAF22254B381F18B806CD6CB647862A397A7221ADBCE3E22CC421793A8847",+ "size": 2717247+ },+ "lit": {+ "culture": "lt",+ "sha256": "1E383DF5B055583BC01CB5764ECDF74C540753F2CB3F8205E7105361DA4BC989",+ "size": 3154896+ },+ "cat": {+ "culture": "ca",+ "sha256": "250DB73CD5B380D2798581295DC12F20D0828CDB335A65D833D12DFDBF57117D",+ "size": 1146012+ },+ "eus": {+ "culture": "eu",+ "sha256": "40E7418296C355D9FD9CA843D115C51E740089E576E887443E61704823CD6624",+ "size": 5175921+ },+ "glg": {+ "culture": "gl",+ "sha256": "7947619C5544D86849F563BD737AD90DBEA9F5319FBD838C4747A7A1CD40B260",+ "size": 2554555+ },+ "ara": {+ "culture": "ar",+ "sha256": "E3206D3DC87FD50C24A0FB9F01838615911D25168F4E64415244B67D2BB3E729",+ "size": 1432056+ },+ "fas": {+ "culture": "fa",+ "sha256": "DB1C0A91208AFF00D3CF1ED2C1D23F76419AFD5F024688B4F71ADC3F2CE4A505",+ "size": 431500+ },+ "urd": {+ "culture": "ur",+ "sha256": "62E8250CE2A994106E313A82E26A516A39E2CF159D0CE3C5B5008387FD0D555F",+ "size": 1398718+ },+ "heb": {+ "culture": "he",+ "sha256": "11F9E43AB227F786352A50F75C94C2E9906F1BABA86D93276DA19DA7CE0904DB",+ "size": 961404+ },+ "hin": {+ "culture": "hi",+ "sha256": "4C73FFC59D497C186B19D1E90F5D721D678EA6B2E277B719BEE4E2AF12271825",+ "size": 1122751+ },+ "ben": {+ "culture": "bn",+ "sha256": "31163084C279AAEBD376216F0C3D5C17AD4B5FEE8DB49DAE79C20000B5DE5964",+ "size": 855841+ },+ "mar": {+ "culture": "mr",+ "sha256": "0BA3F2D116972E72FE9E176BC84C38E81DFB6670F4ED1F7F6C8E16A27DA7CB61",+ "size": 2118233+ },+ "guj": {+ "culture": "gu",+ "sha256": "FA69658614B4946A9AFAE8853D67E0689838803DFA3D12C2E35EC53EE6F8DF34",+ "size": 1418394+ },+ "pan": {+ "culture": "pa",+ "sha256": "1EC0907FC3534065EA9AE190C6BB7EC9E5C74FD9D2FA996AAEC7407F11AD8131",+ "size": 497721+ },+ "tam": {+ "culture": "ta",+ "sha256": "D02FBEC24BE4B07E32E80D0CCFC3B6B67A3C5D61C9D0A7C8532677990912C6EC",+ "size": 3237963+ },+ "tel": {+ "culture": "te",+ "sha256": "D10691FDDD5B67802E1C12800EBB321D3B8BCD8D24A2AC3FF206F93188C04AB5",+ "size": 2769654+ },+ "kan": {+ "culture": "kn",+ "sha256": "BD31E6B6AE93271E3BCF5383D306D8EEFBB91542937CD6D735A5930C970E61D8",+ "size": 3608331+ },+ "mal": {+ "culture": "ml",+ "sha256": "BD05CBF1B197E7810D2903419AEDB06F9EF77BFEDF50B358673C1D18D707CDB4",+ "size": 5275996+ },+ "sin": {+ "culture": "si",+ "sha256": "BC75C6DF2375A30C9F7E759FA7D4B58AE3ECF9CE72668A702D01ACB13E551422",+ "size": 1727413+ },+ "nep": {+ "culture": "ne",+ "sha256": "280BA9450B4F21AFBF5985E0DE87857B75A972577C50EF0603C141DDDE4F1CB8",+ "size": 1002911+ },+ "tha": {+ "culture": "th",+ "sha256": "294227CC2D1292B0ACB28D61D4115C88252B96D466CA90B417CF4CF0C67BF07C",+ "size": 1072600+ },+ "vie": {+ "culture": "vi",+ "sha256": "79DF64CAF7BCFB2A27DF5042ECB6121E196EADA34DA774956995747636D5BFA1",+ "size": 531275+ },+ "ind": {+ "culture": "id",+ "sha256": "69786901DA87AB8766C1EA7FBB10B28F2110C14DA3F6C8F2735DF131FBA95D88",+ "size": 1122661+ },+ "msa": {+ "culture": "ms",+ "sha256": "E41A3E5FEBFEC50C90371EB1CBB17A48B10CAD387900E3420B1F134C1B766CBA",+ "size": 1747801+ },+ "fil": {+ "culture": "fil",+ "sha256": "2B17F68014AB44B88B5E2334EBCD0CE0090D9AF7C6FBEF6FAB22E0BBD9A934DD",+ "size": 1841715+ },+ "khm": {+ "culture": "km",+ "sha256": "47F110575341B322052F3BECBEFEE61A3ECF1EF549352B5D4D33D28AFE30D099",+ "size": 1446926+ },+ "lao": {+ "culture": "lo",+ "sha256": "20124962E93E68121E02C49A949D1F9DF5DB87DD62E5E4AA578362CA532444F8",+ "size": 6386744+ },+ "mya": {+ "culture": "my",+ "sha256": "02AA6C25CFE9E583FA7B5D4131EAC948F962308983F0F397DF077DEA58212B03",+ "size": 4640561+ },+ "kor": {+ "culture": "ko",+ "sha256": "6B85E11D9BBF07863B97B3523B1B112844C43E713DF8B66418A081FD1060B3B2",+ "size": 1677415+ },+ "jpn": {+ "culture": "ja",+ "sha256": "1F5DE9236D2E85F5FDF4B3C500F2D4926F8D9449F28F5394472D9E8D83B91B4D",+ "size": 2471260+ },+ "chi_sim": {+ "culture": "zh-Hans",+ "sha256": "A5FCB6F0DB1E1D6D8522F39DB4E848F05984669172E584E8D76B6B3141E1F730",+ "size": 2469156+ },+ "chi_tra": {+ "culture": "zh-Hant",+ "sha256": "529C5B5797D64B126065CD55F2BB4C7FD7B15790798091B1FF259941A829330B",+ "size": 2366642+ },+ "kat": {+ "culture": "ka",+ "sha256": "557ABB6F1C68BC1B286F1BDD00BB6B82F85A427A91899807DAB6C2F6C7986731",+ "size": 2524769+ },+ "hye": {+ "culture": "hy",+ "sha256": "B701D0D95799A716143DEDB0197504E56F27A1BB133D6607FF5778C6988CB67C",+ "size": 3463717+ },+ "aze": {+ "culture": "az",+ "sha256": "A365310848AECB739F19369CB3831D4660FCD9345D798E91A3042455F9CCC9F0",+ "size": 3524799+ },+ "kaz": {+ "culture": "kk",+ "sha256": "FCC01EED3815A42B9C6321C4C9D3606F39B166CBF95ADE98B7D8D12063EAE53D",+ "size": 4734644+ },+ "uzb": {+ "culture": "uz",+ "sha256": "0E39021EEFE692906B50D9BD22E580FA57FE43FA35DB6846AD8B6369DDC21EA8",+ "size": 6470703+ },+ "mon": {+ "culture": "mn",+ "sha256": "A151A3806D61AC43619CD383896D551BA5C3B07388FFEC6FC83C8C604D677570",+ "size": 2137042+ },+ "amh": {+ "culture": "am",+ "sha256": "3EC3311833A108E07D58A1152B00C0CF1848752E4F85769D46E8CA2B718A2CCC",+ "size": 5470094+ },+ "swa": {+ "culture": "sw",+ "sha256": "395439D1EC308535066CBAEA9B15E0E4CC81F8609170AF76AB7E7E8D3EC42F3E",+ "size": 2167651+ },+ "afr": {+ "culture": "af",+ "sha256": "126D480BFAE95BE2A911ED4916465E27BDE75FEA2DA631E21B96762E5F239646",+ "size": 2652786+ }+ }+}diff --git a/src/Filee.Engines/Ocr/OcrLanguages.cs b/src/Filee.Engines/Ocr/OcrLanguages.csnew file mode 100644index 00000000..3c3991c4--- /dev/null+++ b/src/Filee.Engines/Ocr/OcrLanguages.cs@@ -0,0 +1,110 @@+// The languages Tesseract can read text in (tessdata_fast 4.1.0, Apache-2.0), pinned in ocr-languages.json like the+// engines in engines.json. A language is downloaded the first time a preset needs it, checked against its SHA-256 and+// kept in engines/ocr-languages/<code>/<code>.traineddata by EngineInstaller, so a damaged or partial download never+// counts as installed.++using System.Globalization;+using System.Text.Json;+using Filee.Engines.Infrastructure;++namespace Filee.Engines.Ocr;++/// <summary>One language Tesseract reads, by its Tesseract code (<c>eng</c>, <c>chi_sim</c>).</summary>+/// <param name="Culture">The language as a culture name, for its name in the UI.</param>+public sealed record OcrLanguage(string Code, string Culture, long Size)+{+ /// <summary>The language's own name ("Deutsch", "日本語"), the same in every UI language.</summary>+ public string NativeName+ {+ get+ {+ try+ {+ var name = CultureInfo.GetCultureInfo(Culture).NativeName;+ return name.Length > 0 ? char.ToUpper(name[0], CultureInfo.GetCultureInfo(Culture)) + name[1..] : Code;+ }+ catch (CultureNotFoundException)+ {+ return Code;+ }+ }+ }+}++public static class OcrLanguages+{+ /// <summary>Read when a preset doesn't say (and the first language offered).</summary>+ public const string Default = "eng";++ private const string Version = "4.1.0";++ private static readonly (IReadOnlyList<OcrLanguage> All, IReadOnlyDictionary<string, EngineComponent> Components) List = Load();++ /// <summary>Every language offered, English first.</summary>+ public static IReadOnlyList<OcrLanguage> All => List.All;++ /// <summary>The downloads, for <see cref="EngineInstaller"/> (one component per language).</summary>+ internal static IReadOnlyDictionary<string, EngineComponent> Components => List.Components;++ /// <summary>Folder that receives one sub folder per language.</summary>+ public static string Root => Path.Combine(EngineEnvironment.DownloadRoot, "ocr-languages");++ public static OcrLanguage? Find(string code) => All.FirstOrDefault(l => l.Code == code);++ /// <summary>The language data, or null while it isn't downloaded.</summary>+ public static string? FileOf(string code) =>+ EngineEnvironment.FirstExisting(Path.Combine(Root, code, code + ".traineddata")) is { } file+ && new EngineInstaller(Http.Value, Root, Components).IsInstalled(PackageOf(code))+ ? file : null;++ public static bool IsInstalled(string code) => FileOf(code) is not null;++ /// <summary>Downloads <paramref name="code"/> if it isn't there yet; one download at a time.</summary>+ public static async Task<string> EnsureAsync(string code, CancellationToken cancellationToken)+ {+ if (!Components.ContainsKey(code))+ throw new ArgumentException($"Tesseract has no language '{code}' in Filee's list.", nameof(code));+ await OneAtATime.WaitAsync(cancellationToken);+ try+ {+ if (FileOf(code) is { } file)+ return file;+ await new EngineInstaller(Http.Value, Root, Components).InstallAsync(PackageOf(code), progress: null, cancellationToken);+ return FileOf(code) ?? throw new InvalidDataException($"The {code} language data is missing after its download.");+ }+ finally+ {+ OneAtATime.Release();+ }+ }++ private static readonly SemaphoreSlim OneAtATime = new(1, 1);++ /// <summary>Redirects are followed by the installer, which checks every file against its SHA-256.</summary>+ private static readonly Lazy<HttpClient> Http = new(() =>+ {+ var http = new HttpClient(new SocketsHttpHandler { AllowAutoRedirect = false }) { Timeout = TimeSpan.FromMinutes(10) };+ http.DefaultRequestHeaders.UserAgent.ParseAdd($"Filee/{typeof(OcrLanguages).Assembly.GetName().Version}");+ return http;+ });++ private static EnginePackage PackageOf(string code) => new("ocr-" + code, [code], Components[code].Size, ["tesseract"]);++ private static (IReadOnlyList<OcrLanguage>, IReadOnlyDictionary<string, EngineComponent>) Load()+ {+ using var stream = typeof(OcrLanguages).Assembly.GetManifestResourceStream("Filee.Engines.ocr-languages.json")+ ?? throw new InvalidOperationException("Embedded ocr-languages.json is missing.");+ using var json = JsonDocument.Parse(stream);+ var url = json.RootElement.GetProperty("url").GetString()!;+ var languages = new List<OcrLanguage>();+ var components = new Dictionary<string, EngineComponent>();+ foreach (var entry in json.RootElement.GetProperty("languages").EnumerateObject())+ {+ var size = entry.Value.GetProperty("size").GetInt64();+ languages.Add(new OcrLanguage(entry.Name, entry.Value.GetProperty("culture").GetString()!, size));+ components[entry.Name] = new EngineComponent(entry.Name, Version, string.Format(CultureInfo.InvariantCulture, url, entry.Name),+ entry.Value.GetProperty("sha256").GetString()!, size, "traineddata");+ }+ return (languages, components);+ }+}diff --git a/src/Filee.Engines/Ocr/TesseractConverter.cs b/src/Filee.Engines/Ocr/TesseractConverter.csnew file mode 100644index 00000000..98581a91--- /dev/null+++ b/src/Filee.Engines/Ocr/TesseractConverter.cs@@ -0,0 +1,219 @@+// Text recognition (OCR) with Tesseract (Apache-2.0, a separate program downloaded on demand into engines/tesseract;+// on Linux and macOS the one installed with the system's packages). "Recognize text" (PdfOptions.Ocr) turns a scan or+// a picture into a searchable PDF: the page as it looks, with the recognized words as an invisible text layer on top.+// The route planner puts this step first and goes on from the PDF, so DOCX, TXT, Markdown and HWPX get the text+// (PdfDocumentReader reads the layer when the preset asks for OCR).+//+// A PDF keeps its pages that already have text (born-digital pages are never turned into pictures); only pages+// without any are rendered at 300 dpi and recognized, several at a time. The language data comes from OcrLanguages,+// downloaded the first time a language is used, and is copied into the job's folder so Tesseract reads only it.++using Filee.Core.Conversion;+using Filee.Core.Presets;+using Filee.Engines.Infrastructure;+using Filee.Engines.Pdf;+using ImageMagick;+using PDFtoImage;+using PdfSharp.Pdf;+using PdfSharp.Pdf.IO;+using SkiaSharp;+using UglyToad.PdfPig.Exceptions;++// PDFtoImage marks its API for the platforms PDFium ships for; Filee only targets desktop platforms, all covered.+#pragma warning disable CA1416++namespace Filee.Engines.Ocr;++/// <summary>Scans and pictures → searchable PDF through Tesseract.</summary>+public sealed class TesseractConverter : IConverter+{+ private static readonly TimeSpan Timeout = TimeSpan.FromMinutes(10);++ /// <summary>Scans are rendered at this resolution, which Tesseract reads best (and the page keeps its size).</summary>+ internal const int Dpi = 300;++ /// <summary>Pictures Tesseract opens itself (Leptonica); others become a PNG first (RoutePlanner).</summary>+ internal static readonly IReadOnlyList<string> ImageSources = ["png", "jpg", "tiff", "bmp", "webp", "gif"];++ /// <summary>+ /// One thread per Tesseract: Filee runs several pages at a time, and Tesseract's own threads would only compete+ /// (its documentation recommends this). TESSDATA_PREFIX of the system is not used, the job's folder is.+ /// </summary>+ private static readonly Dictionary<string, string> Environment = new()+ {+ ["OMP_THREAD_LIMIT"] = "1",+ ["TESSDATA_PREFIX"] = "",+ };++ private string? _executable;++ public string Id => "tesseract";+ public string DisplayName => "Tesseract OCR";++ /// <summary>One file at a time; its pages are recognized in parallel.</summary>+ public int MaxParallelism => 1;++ public IReadOnlyList<ConversionEdge> Edges { get; } =+ [.. ImageSources.Select(f => new ConversionEdge(f, "pdf", Ocr: true)), new("pdf", "pdf", Ocr: true)];++ public EngineStatus GetStatus()+ {+ _executable = Locate();+ return _executable is null ? EngineStatus.Unavailable("engine.reason.not_installed")+ : EngineStatus.Available(_executable, EngineEnvironment.UsesFileesCopy("tesseract") ? EngineVersions.Component("tesseract") : null);+ }++ /// <summary>The user's chosen copy, Filee's download, then one installed on the system.</summary>+ internal static string? Locate() =>+ EngineEnvironment.OwnProgram("tesseract", "tesseract.exe") ?? EngineEnvironment.BundledProgram("tesseract", "tesseract.exe")+ ?? EngineEnvironment.SystemProgram("tesseract", "tesseract.exe");++ public async Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken)+ {+ var tesseract = _executable ?? Locate() ?? throw new InvalidOperationException("Tesseract is not installed.");+ var language = OcrLanguages.Find(step.Preset.Pdf.OcrLanguage) ?? OcrLanguages.Find(OcrLanguages.Default)!;+ string data;+ try+ {+ data = await OcrLanguages.EnsureAsync(language.Code, cancellationToken);+ }+ catch (HttpRequestException ex)+ {+ throw new ConversionRefusedException("error.ocr_language_download", $"{language.NativeName}: {ex.Message}");+ }+ var scratch = Path.Combine(step.WorkDirectory, "ocr-" + Guid.NewGuid().ToString("N")[..8]);+ var tessdata = Path.Combine(scratch, "tessdata");+ Directory.CreateDirectory(tessdata);+ File.Copy(data, Path.Combine(tessdata, Path.GetFileName(data)));+ var ocr = new Run(tesseract, tessdata, language.Code);+ progress?.Report(0.05);++ var output = step.Output.Allocate("pdf");+ if (output is null)+ return [];+ if (step.From == "pdf")+ {+ await RecognizePdfAsync(ocr, step, scratch, output, progress, cancellationToken);+ }+ else+ {+ var target = Path.Combine(scratch, "page");+ await ocr.ToPdfAsync(step.InputPath, target, HasResolution(step.InputPath) ? null : Dpi, cancellationToken);+ File.Move(target + ".pdf", output, overwrite: true);+ }+ progress?.Report(1);+ return [output];+ }++ /// <summary>+ /// The pages without text become recognized scans, the others are copied. A PDF whose pages all have text is+ /// copied as it is, with a note.+ /// </summary>+ private static async Task RecognizePdfAsync(Run ocr, ConversionStep step, string scratch, string output,+ IProgress<double>? progress, CancellationToken cancellationToken)+ {+ var bytes = await File.ReadAllBytesAsync(step.InputPath, cancellationToken);+ var hasText = PagesWithText(step.InputPath);+ var pages = PageRange.Parse(step.Preset.Pdf.PageRange, hasText.Count);+ var scans = pages.Where(p => !hasText[p]).ToList();+ if (scans.Count == 0 && pages.Count == hasText.Count)+ {+ step.Notice?.Invoke("notice.ocr_has_text");+ File.Copy(step.InputPath, output, overwrite: true);+ return;+ }++ // PDFium draws one page at a time; Tesseract then reads them side by side.+ var options = new RenderOptions(Dpi: Dpi, WithAnnotations: true, WithFormFill: true, BackgroundColor: SKColors.White);+ for (var i = 0; i < scans.Count; i++)+ {+ cancellationToken.ThrowIfCancellationRequested();+ using var bitmap = Conversion.ToImage(bytes, scans[i], null, options);+ await using var file = File.Create(Path.Combine(scratch, $"p{scans[i]}.png"));+ bitmap.Encode(file, SKEncodedImageFormat.Png, 100);+ progress?.Report(0.05 + 0.25 * (i + 1) / scans.Count);+ }+ var done = 0;+ await Parallel.ForEachAsync(scans, new ParallelOptions { MaxDegreeOfParallelism = System.Environment.ProcessorCount, CancellationToken = cancellationToken },+ async (page, ct) =>+ {+ await ocr.ToPdfAsync(Path.Combine(scratch, $"p{page}.png"), Path.Combine(scratch, $"p{page}"), Dpi, ct);+ progress?.Report(0.3 + 0.65 * Interlocked.Increment(ref done) / scans.Count);+ });++ // A new document: the pages and the document information come along, bookmarks and form fields don't.+ using var source = PdfSharpConverter.Open(step.InputPath, PdfDocumentOpenMode.Import);+ using var result = new PdfDocument();+ result.Info.Title = source.Info.Title;+ result.Info.Author = source.Info.Author;+ result.Info.Subject = source.Info.Subject;+ result.Info.Keywords = source.Info.Keywords;+ var recognized = new List<PdfDocument>();+ try+ {+ foreach (var page in pages)+ {+ if (hasText[page])+ {+ result.AddPage(source.Pages[page]);+ continue;+ }+ var scan = PdfReader.Open(Path.Combine(scratch, $"p{page}.pdf"), PdfDocumentOpenMode.Import);+ recognized.Add(scan);+ result.AddPage(scan.Pages[0]);+ }+ result.Save(output);+ }+ finally+ {+ recognized.ForEach(d => d.Dispose());+ }+ }++ /// <summary>For each page: true when it has any text, visible or not (an OCR layer counts).</summary>+ internal static List<bool> PagesWithText(string path)+ {+ try+ {+ using var pdf = UglyToad.PdfPig.PdfDocument.Open(path, new UglyToad.PdfPig.ParsingOptions { UseLenientParsing = true, SkipMissingFonts = true });+ return [.. pdf.GetPages().Select(page => page.Letters.Any(l => !string.IsNullOrWhiteSpace(l.Value)))];+ }+ catch (PdfDocumentEncryptedException)+ {+ throw PdfDocumentReader.PasswordProtected();+ }+ }++ /// <summary>+ /// True when the picture says how large it is meant to be. Without that Tesseract guesses 70 dpi (a page of a+ /// metre and more); 300 dpi is what scanners write.+ /// </summary>+ private static bool HasResolution(string path)+ {+ try+ {+ var info = new MagickImageInfo(path);+ return info.Density is { X: > 1 };+ }+ catch (MagickException)+ {+ return false;+ }+ }++ /// <summary>One language, one Tesseract.</summary>+ private sealed record Run(string Executable, string Tessdata, string Language)+ {+ /// <summary>Writes <c>{outputBase}.pdf</c>.</summary>+ public async Task ToPdfAsync(string input, string outputBase, int? dpi, CancellationToken cancellationToken)+ {+ // tessedit_create_pdf instead of the "pdf" config file, which lives in Tesseract's own tessdata folder.+ List<string> args = [input, outputBase, "--tessdata-dir", Tessdata, "-l", Language, "-c", "tessedit_create_pdf=1"];+ if (dpi is { } value)+ args.AddRange(["--dpi", value.ToString(System.Globalization.CultureInfo.InvariantCulture)]);+ var result = await ProcessRunner.RunAsync(Executable, args, Timeout, cancellationToken, environment: Environment);+ if (result.ExitCode != 0 || !File.Exists(outputBase + ".pdf"))+ throw new InvalidOperationException($"Tesseract failed (exit {result.ExitCode}). {result.StandardError.Trim()}");+ }+ }+}diff --git a/src/Filee.Engines/Office/DocxWriterConverter.cs b/src/Filee.Engines/Office/DocxWriterConverter.csindex 0b2deb23..cacf3bb2 100644--- a/src/Filee.Engines/Office/DocxWriterConverter.cs+++ b/src/Filee.Engines/Office/DocxWriterConverter.cs@@ -32,7 +32,8 @@ public sealed class DocxWriterConverter : IConverterpublic async Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken){progress?.Report(0.1);- var document = await DocumentReaders.ReadAsync(step.InputPath, step.From, step.WorkDirectory, cancellationToken);+ var document = await DocumentReaders.ReadAsync(step.InputPath, step.From, step.WorkDirectory, cancellationToken,+ ocrLayer: step.Preset.Pdf.Ocr);progress?.Report(0.6);var output = step.Output.Allocate("docx");diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.csindex cb4a9058..e3fbe1d0 100644--- a/src/Filee.Engines/Pdf/PdfDocumentReader.cs+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.cs@@ -1,11 +1,12 @@-// PDF → HDocument with PdfPig (Apache-2.0), so PDF → DOCX / HWPX / TXT needs no Word, LibreOffice or OCR engine.+// PDF → HDocument with PdfPig (Apache-2.0), so PDF → DOCX / HWPX / TXT needs no Word or LibreOffice.//// Every page's words (with their positions, fonts, sizes and colours) become lines, lines become paragraphs, and the// paragraphs and pictures are put in reading order, also on two-column pages (PdfDocumentReader.Layout). Headings// come from font sizes, bold / italic from the fonts, page size and margins from the page. Running headers and// footers (also those that change with the chapter), page numbers and a logo repeated at the top or bottom of the// pages are left out. A page without text (a scan) becomes the rendered page as a picture (PDFium via PDFtoImage), so-// the result is complete; there is no OCR.+// the result is complete. With "Recognize text" (PdfOptions.Ocr) Tesseract reads the scans first, and their invisible+// OCR layer is the page's text (ReadWithOcrLayer).//// Rows laid out with tab stops (a number, a label and a date far to the right) stay one line with tabs at the PDF's// positions, and a line that ends early although the next line's first word would have fitted (an address) keeps its@@ -102,19 +103,28 @@ internal sealed partial class PdfDocumentReaderprivate readonly string _path;private readonly string _mediaFolder;private readonly bool _textOnly;+ private readonly bool _ocrLayer;private byte[]? _pdfBytes;private int _pictures;- private PdfDocumentReader(string path, string mediaFolder, bool textOnly)+ private PdfDocumentReader(string path, string mediaFolder, bool textOnly, bool ocrLayer = false){_path = path;_mediaFolder = mediaFolder;_textOnly = textOnly;+ _ocrLayer = ocrLayer;}/// <summary>Reads <paramref name="path"/> with pictures (extracted into <paramref name="mediaFolder"/>) and scans.</summary>public static HDocument Read(string path, string mediaFolder) => new PdfDocumentReader(path, mediaFolder, textOnly: false).ReadDocument();+ /// <summary>+ /// Like <see cref="Read"/>, but the invisible text of a scan (its OCR layer, PdfOptions.Ocr) is the page's text:+ /// the words become paragraphs and the scan under them is left out.+ /// </summary>+ public static HDocument ReadWithOcrLayer(string path, string mediaFolder) =>+ new PdfDocumentReader(path, mediaFolder, textOnly: false, ocrLayer: true).ReadDocument();+/// <summary>/// Reads only the text, including invisible text (the OCR layer of scanned PDFs); pictures and scans are left out./// </summary>@@ -296,16 +306,21 @@ internal sealed partial class PdfDocumentReadervar letters = word.Letters.Where(l => !string.IsNullOrWhiteSpace(l.Value)).ToList();if (letters.Count == 0)continue;- // Invisible text is the OCR layer of a scan: part of the text, not of the page's look.- if (!_textOnly && letters.All(l => l.RenderingMode == TextRenderingMode.Neither))+ // Invisible text is the OCR layer of a scan: part of the text, not of the page's look (unless OCR was asked for).+ var invisible = letters.All(l => l.RenderingMode == TextRenderingMode.Neither);+ if (!_textOnly && !_ocrLayer && invisible)continue;var lead = string.IsNullOrWhiteSpace(word.Lead.Value) ? letters[0] : word.Lead;var box = Shift(word.Box, crop);var size = Median(letters.Select(LetterSize));var center = new PdfPoint((box.Left + box.Right) / 2, (box.Top + box.Bottom) / 2);var link = links.FirstOrDefault(l => Inside(l.Bounds, center)).Uri;+ var format = Format(lead, size);+ // An OCR layer's font only places the words (Tesseract's GlyphLessFont): the document's own font is used.+ if (invisible)+ format = format with { Font = null, Bold = false, Italic = false };words.Add(new TextWord(CleanText(word.Text), box.Left, box.Right, box.Top, box.Bottom,- lead.StartBaseLine.Y - crop.Bottom, Math.Max(1, size), Format(lead, size), link));+ lead.StartBaseLine.Y - crop.Bottom, Math.Max(1, size), format, link));}if (words.Count == 0 || (rotated && !_textOnly))diff --git a/src/Filee.Engines/Pdf/PdfTextConverter.cs b/src/Filee.Engines/Pdf/PdfTextConverter.csindex 86859ffb..d339f54c 100644--- a/src/Filee.Engines/Pdf/PdfTextConverter.cs+++ b/src/Filee.Engines/Pdf/PdfTextConverter.cs@@ -2,7 +2,7 @@// two-column pages), without running headers, footers and page numbers.//// TXT has one paragraph per line and an empty line between paragraphs. The invisible text layer of OCR-processed-// scans is included; scans without one have no text to extract (Filee does no OCR).+// scans is included; scans without one have no text to extract unless the preset recognizes it first (PdfOptions.Ocr).//// Markdown keeps headings, bold / italic and links (MarkdownWriter, as for e-books). The pictures go into a// "<name>_files" folder next to the Markdown file and are linked relatively (MarkdownPictures); a page without text@@ -60,7 +60,9 @@ public sealed class PdfTextConverter : IConvertervar textOnly = step.Preset.Document.LeaveOutPictures;var document = textOnly? PdfDocumentReader.ReadText(step.InputPath)- : PdfDocumentReader.Read(step.InputPath, Path.Combine(step.WorkDirectory, $"media-{Guid.NewGuid():N}"));+ : step.Preset.Pdf.Ocr+ ? PdfDocumentReader.ReadWithOcrLayer(step.InputPath, Path.Combine(step.WorkDirectory, $"media-{Guid.NewGuid():N}"))+ : PdfDocumentReader.Read(step.InputPath, Path.Combine(step.WorkDirectory, $"media-{Guid.NewGuid():N}"));cancellationToken.ThrowIfCancellationRequested();if (textOnly && PdfDocumentReader.PlainText(document).Length == 0)throw NoText();diff --git a/tests/Filee.Core.Tests/OcrRouteTests.cs b/tests/Filee.Core.Tests/OcrRouteTests.csnew file mode 100644index 00000000..44dc7abd--- /dev/null+++ b/tests/Filee.Core.Tests/OcrRouteTests.cs@@ -0,0 +1,100 @@+// "Recognize text" (issue #288): Tesseract's OCR edges start a route only when the preset asks for them, and the+// route goes on from the searchable PDF.++using Filee.Core.Conversion;+using Filee.Core.Presets;++namespace Filee.Core.Tests;++public class OcrRouteTests+{+ private static readonly ConversionEdge[] OcrEdges =+ [new("png", "pdf", Ocr: true), new("jpg", "pdf", Ocr: true), new("pdf", "pdf", Ocr: true)];++ private static string Steps(Route? route) => string.Join(", ", route!.Steps.Select(s => $"{s.Converter.Id}:{s.From}→{s.To}"));++ private static RoutePlanner Planner() => new([+ new FakeConverter("tesseract", OcrEdges),+ new FakeConverter("magick", new ConversionEdge("heic", "png"), new ConversionEdge("png", "pdf"), new ConversionEdge("jpg", "pdf")),+ new FakeConverter("docx", new ConversionEdge("pdf", "docx")),+ new FakeConverter("text", new ConversionEdge("pdf", "txt")),+ ]);++ [Fact]+ public void Ocr_edges_are_never_part_of_an_ordinary_route()+ {+ var planner = Planner();++ Assert.Equal("magick:png→pdf", Steps(planner.Plan("png", "pdf")));+ Assert.Null(planner.Plan("pdf", "pdf")); // no ordinary PDF → PDF edge here+ }++ [Fact]+ public void Asking_for_ocr_starts_with_tesseract_and_goes_on_from_its_pdf()+ {+ var planner = Planner();++ Assert.Equal("tesseract:jpg→pdf", Steps(planner.Plan("jpg", "pdf", ocr: true)));+ Assert.Equal("tesseract:pdf→pdf, docx:pdf→docx", Steps(planner.Plan("pdf", "docx", ocr: true)));+ Assert.Equal("tesseract:png→pdf, text:pdf→txt", Steps(planner.Plan("png", "txt", ocr: true)));+ // A picture Tesseract can't open becomes a PNG first.+ Assert.Equal("magick:heic→png, tesseract:png→pdf, docx:pdf→docx", Steps(planner.Plan("heic", "docx", ocr: true)));+ }++ [Fact]+ public void Without_tesseract_there_is_no_ocr_route()+ {+ var planner = new RoutePlanner([new FakeConverter("magick", new ConversionEdge("png", "pdf"))]);++ Assert.Null(planner.Plan("png", "pdf", ocr: true));+ Assert.False(planner.CanConvert("png", "pdf", ocr: true));+ }++ [Fact]+ public void Text_is_recognized_in_pdfs_and_pictures_for_document_targets()+ {+ var preset = new Preset { TargetFormat = "docx", Pdf = { Ocr = true } };++ Assert.True(preset.RecognizesTextOf("pdf"));+ Assert.True(preset.RecognizesTextOf("jpg"));+ Assert.False(preset.RecognizesTextOf("docx")); // already text+ Assert.False(new Preset { TargetFormat = "png", Pdf = { Ocr = true } }.RecognizesTextOf("pdf"));+ Assert.False(new Preset { TargetFormat = "docx" }.RecognizesTextOf("pdf"));+ }++ [Fact]+ public async Task A_job_with_ocr_runs_tesseract_before_the_document_writer()+ {+ using var dir = new TempDir();+ var scan = dir.File("scan.jpg");+ var tesseract = new FakeRecorder("tesseract", OcrEdges);+ var writer = new FakeRecorder("docx", new ConversionEdge("pdf", "docx"));+ var magick = new FakeRecorder("magick", new ConversionEdge("jpg", "pdf"));+ await using var queue = new JobQueue(new ConverterCatalog([tesseract, writer, magick]));++ var job = new ConversionJob([scan], new Preset { TargetFormat = "docx", Pdf = { Ocr = true } }, "Test");+ await queue.RunAsync(job);++ Assert.Equal(JobState.Completed, job.State);+ Assert.Equal([("jpg", "pdf", false)], tesseract.Steps);+ Assert.Equal([("pdf", "docx", false)], writer.Steps);+ Assert.Empty(magick.Steps);+ }++ [Fact]+ public async Task Pdfs_merged_into_one_are_recognized_first()+ {+ using var dir = new TempDir();+ var a = dir.File("a.pdf");+ var b = dir.File("b.jpg");+ var tesseract = new FakeRecorder("tesseract", OcrEdges);+ var merger = new FakeMerger();+ await using var queue = new JobQueue(new ConverterCatalog([tesseract]), merger);++ var job = new ConversionJob([a, b], new Preset { TargetFormat = "pdf", Pdf = { Ocr = true, MergeIntoSingle = true } }, "Test");+ await queue.RunAsync(job);++ Assert.Equal(JobState.Completed, job.State);+ Assert.Equal([("jpg", "pdf", false), ("pdf", "pdf", false)], tesseract.Steps.OrderBy(s => s.From));+ }+}diff --git a/tests/Filee.Engines.Tests/EngineInstallerTests.cs b/tests/Filee.Engines.Tests/EngineInstallerTests.csindex 8faf7ddf..a81f6bca 100644--- a/tests/Filee.Engines.Tests/EngineInstallerTests.cs+++ b/tests/Filee.Engines.Tests/EngineInstallerTests.cs@@ -318,6 +318,37 @@ public sealed class EngineInstallerTests : IDisposable}/// <summary>IProgress that reports synchronously (Progress<T> would post to the thread pool).</summary>+ [Fact]+ public async Task A_tesseract_language_is_its_file_in_a_folder_of_its_own()+ {+ var data = Encoding.UTF8.GetBytes("lstm");+ var german = Component("deu", "traineddata", data);+ var installer = Installer(new FakeServer(new() { [german.Url] = data }), german);+ var package = new EnginePackage("ocr-deu", ["deu"], data.Length, ["tesseract"]);++ await installer.InstallAsync(package, null, TestContext.Current.CancellationToken);++ Assert.Equal("lstm", File.ReadAllText(Path.Combine(_root, "deu", "deu.traineddata")));+ Assert.True(installer.IsUpToDate(package));+ }++ [Fact]+ public void Tesseract_keeps_only_what_ocr_needs()+ {+ var root = Path.Combine(_root, "tesseract");+ foreach (var file in new[] { "tesseract.exe", "libtesseract-5.dll", "text2image.exe", "tesseract.1.html", "tesseract-uninstall.exe",+ "doc/LICENSE", "tessdata/eng.traineddata", "$PLUGINSDIR/System.dll" })+ {+ Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(root, file))!);+ File.WriteAllText(Path.Combine(root, file), "x");+ }++ EngineInstaller.TrimTesseract(root);++ Assert.Equal(["doc/LICENSE", "libtesseract-5.dll", "tesseract.exe"],+ Directory.GetFiles(root, "*", SearchOption.AllDirectories).Select(f => Path.GetRelativePath(root, f).Replace('\\', '/')).Order(StringComparer.Ordinal));+ }+private sealed class SyncProgress(Action<EngineInstallProgress> report) : IProgress<EngineInstallProgress>{public void Report(EngineInstallProgress value) => report(value);diff --git a/tests/Filee.Engines.Tests/OcrTests.cs b/tests/Filee.Engines.Tests/OcrTests.csnew file mode 100644index 00000000..7003773d--- /dev/null+++ b/tests/Filee.Engines.Tests/OcrTests.cs@@ -0,0 +1,167 @@+// Text recognition with Tesseract (issue #288): scans and pictures become searchable PDFs, and their text goes on into+// DOCX and TXT. The real tests need Tesseract (pwsh build/fetch-engines.ps1 -Only 7zip,tesseract) and download the+// language data they read the first time (English, German).++using System.IO.Compression;+using Filee.Core.Conversion;+using Filee.Core.Presets;+using Filee.Engines.Infrastructure;+using Filee.Engines.Ocr;+using ImageMagick;+using ImageMagick.Drawing;+using PdfSharp.Pdf;+using PdfSharp.Pdf.IO;++namespace Filee.Engines.Tests;++public class OcrLanguageListTests+{+ [Fact]+ public void Every_language_is_pinned_and_named()+ {+ Assert.Equal("eng", OcrLanguages.All[0].Code);+ Assert.Equal(69, OcrLanguages.All.Count);+ Assert.All(OcrLanguages.All, language =>+ {+ Assert.NotEqual(language.Code, language.NativeName); // its culture is known+ var component = OcrLanguages.Components[language.Code];+ Assert.Matches("^[0-9A-F]{64}$", component.Sha256);+ Assert.True(component.Size > 0);+ Assert.Equal($"https://raw.githubusercontent.com/tesseract-ocr/tessdata_fast/4.1.0/{language.Code}.traineddata", component.Url);+ });+ Assert.Equal("Deutsch", OcrLanguages.Find("deu")!.NativeName);+ Assert.Null(OcrLanguages.Find("klingon"));+ }++ [Fact]+ public void Tesseract_is_offered_but_not_suggested()+ {+ var package = EngineDownloads.Packages.Single(p => p.Id == "tesseract");++ Assert.False(EngineDownloads.IsSuggested(package));+ Assert.Equal(OperatingSystem.IsWindows() ? "tesseract.exe" : "tesseract", Assert.Single(EngineEnvironment.OwnCopyPrograms["tesseract"]));+ }+}++public class OcrTests(EngineFixture fx) : IClassFixture<EngineFixture>+{+ private static readonly bool Installed = TesseractConverter.Locate() is not null;++ private const string Skip = "Tesseract is not installed (pwsh build/fetch-engines.ps1 -Only 7zip,tesseract)";++ private static readonly string? Arial = EngineEnvironment.FirstExisting(@"C:\Windows\Fonts\arial.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",+ "/System/Library/Fonts/Supplemental/Arial.ttf");++ /// <summary>A page as a scanner writes it: black text on white at 300 dpi, no text layer.</summary>+ private static string Scan(string folder, string name, params string[] lines)+ {+ using var image = new MagickImage(MagickColors.White, 2480, 800) { Density = new Density(300) };+ var drawables = new Drawables().FontPointSize(28).FillColor(MagickColors.Black);+ if (Arial is not null)+ drawables.Font(Arial);+ for (var i = 0; i < lines.Length; i++)+ drawables.Text(150, 220 + i * 180, lines[i]);+ drawables.Draw(image);+ var path = Path.Combine(folder, name);+ image.Write(path);+ return path;+ }++ private static string PageText(string pdf, int page)+ {+ using var document = UglyToad.PdfPig.PdfDocument.Open(pdf);+ return string.Join(" ", document.GetPage(page).GetWords().Select(w => w.Text));+ }++ private static Preset Ocr(string target, string language = "eng") => new() { TargetFormat = target, Pdf = { Ocr = true, OcrLanguage = language } };++ private static void AssertDone(ConversionJob job) =>+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));++ [Fact]+ public async Task A_picture_becomes_a_searchable_pdf()+ {+ Assert.SkipUnless(Installed, Skip);+ var scan = Scan(fx.NewFolder(), "letter.png", "Filee reads scanned pages", "into searchable documents");++ var job = await fx.ConvertAsync([scan], Ocr("pdf"));++ AssertDone(job);+ var text = PageText(job.Outputs.Single(), 1);+ Assert.Contains("scanned", text);+ Assert.Contains("searchable", text);+ }++ [Fact]+ public async Task A_scanned_pdf_becomes_a_docx_with_its_text_and_without_the_scan()+ {+ Assert.SkipUnless(Installed, Skip);+ var folder = fx.NewFolder();+ var scanPdf = (await fx.ConvertAsync([Scan(folder, "scan.png", "The quarterly report is ready")], new Preset { TargetFormat = "pdf" })).Outputs.Single();+ Assert.Equal("", PageText(scanPdf, 1)); // only a picture++ var job = await fx.ConvertAsync([scanPdf], Ocr("docx"));++ AssertDone(job);+ using var docx = ZipFile.OpenRead(job.Outputs.Single());+ using var reader = new StreamReader(docx.GetEntry("word/document.xml")!.Open());+ var xml = await reader.ReadToEndAsync(TestContext.Current.CancellationToken);+ Assert.Contains("quarterly", xml);+ Assert.DoesNotContain("<w:drawing", xml);+ }++ [Fact]+ public async Task Pages_with_text_stay_as_they_are_and_only_scans_are_recognized()+ {+ Assert.SkipUnless(Installed, Skip);+ var folder = fx.NewFolder();+ var withText = (await fx.ConvertAsync([Scan(folder, "first.png", "This page was searchable before")], Ocr("pdf"))).Outputs.Single();+ var scan = (await fx.ConvertAsync([Scan(folder, "second.png", "This page is only a picture")], new Preset { TargetFormat = "pdf" })).Outputs.Single();+ var mixed = Path.Combine(folder, "mixed.pdf");+ using (var document = new PdfDocument())+ {+ foreach (var part in new[] { withText, scan })+ {+ using var source = PdfReader.Open(part, PdfDocumentOpenMode.Import);+ document.AddPage(source.Pages[0]);+ }+ document.Save(mixed);+ }++ var job = await fx.ConvertAsync([mixed], Ocr("pdf"));++ AssertDone(job);+ var output = job.Outputs.Single();+ Assert.Equal(PageText(withText, 1), PageText(output, 1)); // copied, not recognized again+ Assert.Contains("picture", PageText(output, 2));+ }++ [Fact]+ public async Task A_pdf_that_has_text_everywhere_is_copied_with_a_note()+ {+ Assert.SkipUnless(Installed, Skip);+ var searchable = (await fx.ConvertAsync([Scan(fx.NewFolder(), "done.png", "Already recognized")], Ocr("pdf"))).Outputs.Single();++ var job = await fx.ConvertAsync([searchable], Ocr("pdf"));++ AssertDone(job);+ Assert.Equal("notice.ocr_has_text", job.Files[0].NoticeKey);+ Assert.Equal(await File.ReadAllBytesAsync(searchable, TestContext.Current.CancellationToken),+ await File.ReadAllBytesAsync(job.Outputs.Single(), TestContext.Current.CancellationToken));+ }++ [Fact]+ public async Task Another_language_is_downloaded_when_a_preset_needs_it()+ {+ Assert.SkipUnless(Installed, Skip);+ var scan = Scan(fx.NewFolder(), "brief.jpg", "Die Größe der Straße ändert sich");++ var job = await fx.ConvertAsync([scan], Ocr("txt", "deu"));++ AssertDone(job);+ Assert.True(OcrLanguages.IsInstalled("deu"));+ var text = await File.ReadAllTextAsync(job.Outputs.Single(), TestContext.Current.CancellationToken);+ Assert.Contains("Größe", text);+ Assert.Contains("Straße", text);+ }+}