Skip to content

armgit / Filee

Public Imported from GitHub
0

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.

fix(ocr): address review feedback on OCR engine integration

smjriflea223ff4eaaab
diff --git a/THIRD-PARTY-NOTICES.md b/THIRD-PARTY-NOTICES.md
index 2e63361c..a572f760 100644
--- a/THIRD-PARTY-NOTICES.md
+++ b/THIRD-PARTY-NOTICES.md
@@ -78,6 +78,8 @@ Downloaded from their official release pages when the user chooses to install th
| Microsoft Visual C++ Redistributable (for Ghostscript, conda-forge `vc14_runtime`) | Microsoft Visual C++ Redistributable license | https://github.com/conda-forge/vc-feedstock |
| FFmpeg 9.0.2 (gyan.dev "full_build-shared" Windows build; ffmpeg.exe, ffprobe.exe and their libraries, incl. x264, x265, libvpx, LAME, Opus, Vorbis, Theora, OpenCORE AMR) | GPL-3.0-or-later (the build is configured with `--enable-gpl --enable-version3`); FFmpeg itself LGPL-2.1-or-later | https://ffmpeg.org, build: https://www.gyan.dev/ffmpeg/builds/ (source: https://github.com/GyanD/codexffmpeg) |
| calibre (ebook-convert) | GPL-3.0 | https://calibre-ebook.com |
+| Tesseract OCR | Apache-2.0 | https://github.com/tesseract-ocr/tesseract |
+| Tesseract trained data (tessdata / tessdata_fast / tessdata_best) | Apache-2.0 | https://github.com/tesseract-ocr/tessdata_fast |
These programs run as separate processes. Their source code is available from the linked projects.
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 9d0d66ef..4d953ec3 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -16,15 +16,17 @@
┌──────────────────────────── Filee.Core ───────────────────────────────────────────┐
│ JobQueue ──► RoutePlanner (Dijkstra over converter edges) ──► IConverter steps │
│ FormatRegistry · Preset · OutputPathResolver · ToolbarProfile · ProfileSelector │
- └──────────────────────────────────────────────┬──────────────────────────────────────┘
+ │ OcrEngineRegistry · IOcrEngine (Apple Vision on macOS, Tesseract elsewhere) │
+ └──────────────────────────────────────────────┬──────────────────────────────────┘
▼
┌──────────────────────────── Filee.Engines ────────────────────────────────────────┐
│ Built in: Magick (images, RAW) · PdfSharp · Pdfium · PdfPig · Svg.Skia · ICNS │
│ HwpxWriter + DocxWriter over DocumentReaders (DOCX, XLSX, XLS, ODS, PPTX, │
│ PDF, HTML, EPUB, MOBI, FB2, HWPX, Markdown) · Spreadsheets · OOXML variants │
- │ · E-mail · Data · E-books · CAD · Fonts · Subtitles · 3D models │
+ │ · E-mail · Data · E-books · CAD · Fonts · Subtitles · 3D models · Image OCR │
│ Bundled: rhwp (HWP→PDF, HWP↔HWPX) · 7-Zip (archives) · Unhwp · Markdig │
│ Optional: FFmpeg · LibreOffice + H2Orestart · calibre · Ghostscript · Pandoc │
+ │ · Tesseract OCR │
└────────────────────────────────────────────────────────────────────────────────────┘
```
@@ -52,6 +54,18 @@
"Compress into one archive" packs the dropped files, unconverted, with the archive engine (`IFileCombiner`).
6. **Report** – progress and results appear in the toast window and in Home → Recent conversions.
+## Optical Character Recognition (OCR)
+
+Filee provides engine-neutral OCR abstractions in `Filee.Core.Ocr` (`IOcrEngine`, `OcrEngineRegistry`, `OcrPageResult`, `OcrLine`, `OcrWord`, `OcrRect`).
+
+- **Engine Discovery**:
+ - `OcrEngineRegistry` discovers available OCR engines without requiring callers to couple to platform-specific projects.
+ - On macOS, `MacOsVisionOcrEngine` (in `Filee.Platform.MacOS`) calls Apple's Vision framework (`VNRecognizeTextRequest`) via Objective-C runtime P/Invoke (`objc_msgSend` on arm64, `objc_msgSend_stret` for 32-byte `CGRect` on x86_64, with `setAutomaticallyDetectsLanguage:` on macOS 13+). It is discovered via reflection at runtime so Windows and Linux builds remain decoupled.
+ - Across all desktop platforms, `TesseractOcrEngine` (in `Filee.Engines.Ocr`) runs Tesseract CLI out-of-process with timeouts and argument lists, supporting 100+ languages including Devanagari (Nepali/Hindi), Indic, Cyrillic, Thai, and Arabic scripts.
+- **Conversion Pipelines**:
+ - `ImageOcrConverter`: converts raster images directly to TXT, DOCX, and Markdown (`image-ocr` engine priority entry).
+ - `PdfDocumentReader`: when `DocumentOptions.Ocr` is enabled, runs OCR on scanned pages. Scanned PDFs with an existing invisible OCR layer (`TextRenderingMode.Neither`) use that layer directly when OCR is enabled, while preserving the scanned page picture when OCR is disabled. Rotated pages (`rotated && !_textOnly`) preserve their rendered scan picture.
+
## Files dragged from a web browser
A picture, a link to a file or a download dragged out of a browser opens the donut like a file from the file manager.
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index a2fe7a27..31a08c84 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -74,8 +74,9 @@ the same on every PC. An external engine is looked for in this order:
calibre and Ghostscript installers put it. Settings → *Engines* shows it as "Found on this PC", with a note when
its version is older than the one Filee downloads.
-Where Filee has no download of an engine (FFmpeg and Ghostscript on macOS, LibreOffice and Ghostscript on Linux),
-step 3 is how it is found. Microsoft Office
+Where Filee has no download of an engine (FFmpeg and Ghostscript on macOS, LibreOffice and Ghostscript on Linux,
+Tesseract on all platforms), step 3 is how it is found; Settings → *Engines* shows install guidance (Homebrew, winget,
+apt) and a "Use my copy…" button to point to its executable. Microsoft Office
and Hancom Office are never used, even when installed. Running from source, the repository's `engines/` folder
(filled by `build/fetch-engines.ps1`) is used.
diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.json
index bc537324..5d6039db 100644
--- a/src/Filee.App/Assets/i18n/ja.json
+++ b/src/Filee.App/Assets/i18n/ja.json
@@ -663,8 +663,8 @@
"engines.guidance.windows": "このPC向けの{0}はFileeからダウンロードできません。\n「手元のプログラムを使う…」を選び、インストール済みのものを指定してください。",
"engines.package.tesseract.name": "Tesseract(多言語テキスト認識)",
"engines.package.tesseract.description": "デーヴァナーガリー(ネパール語/ヒンディー語)、アラビア語、希少言語を含む100以上の言語のテキストを認識します。\nmacOSではApple Visionが英語、CJK、ヨーロッパ言語をネイティブにサポートしています。",
- "engines.guidance.macos.tesseract": "HomebrewでTesseractをインストール(brew install tesseract tesseract-lang)してから「再確認」を選択してください。\nまたは「手持ちのコピーを使う…」を選択してbinフォルダーを指定してください。",
- "engines.guidance.linux.tesseract": "パッケージマネージャーでTesseractをインストール(Ubuntu/Debian: sudo apt install tesseract-ocr tesseract-ocr-all)してから「再確認」を選択してください。\nまたは「手持ちのコピーを使う…」を選択してプログラムフォルダーを指定してください。",
+ "engines.guidance.macos.tesseract": "HomebrewでTesseractをインストール(brew install tesseract tesseract-lang)してから「再確認」を選択してください。\nまたは「手元のプログラムを使う…」を選択してbinフォルダーを指定してください。",
+ "engines.guidance.linux.tesseract": "パッケージマネージャーでTesseractをインストール(Ubuntu/Debian: sudo apt install tesseract-ocr tesseract-ocr-all)してから「再確認」を選択してください。\nまたは「手元のプログラムを使う…」を選択してプログラムフォルダーを指定してください。",
"engines.guidance.windows.tesseract": "UB-Mannheim(github.com/UB-Mannheim/tesseract)からTesseractをインストールし、「再確認」を選択するかインストールフォルダーを指定してください。",
"engines.use_own_app_folder": "{0}.appを含むフォルダーを選択してください",
"engines.progress_detail": "{0}/{1} · {2}/秒 · {3}",
diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.json
index 379ada5e..d8c991f2 100644
--- a/src/Filee.App/Assets/i18n/ko.json
+++ b/src/Filee.App/Assets/i18n/ko.json
@@ -680,8 +680,8 @@
"engines.guidance.windows": "이 PC용 {0} 다운로드는 없어요.\n“내 프로그램 쓰기…”로 설치된 프로그램을 선택해 주세요.",
"engines.package.tesseract.name": "Tesseract (다국어 텍스트 인식)",
"engines.package.tesseract.description": "데바나가리(네팔어/힌디어), 아랍어 및 희귀 문자를 포함한 100개 이상의 언어로 텍스트를 인식합니다.\nmacOS에서는 Apple Vision이 내장되어 영어, CJK 및 유럽어는 추가 설치 없이 동작합니다.",
- "engines.guidance.macos.tesseract": "Homebrew로 Tesseract를 설치(brew install tesseract tesseract-lang)한 뒤 “다시 확인”을 선택하세요.\n또는 “내 사본 사용…”을 선택하고 bin 폴더를 지정하세요.",
- "engines.guidance.linux.tesseract": "패키지 관리자로 Tesseract를 설치(Ubuntu/Debian: sudo apt install tesseract-ocr tesseract-ocr-all)한 뒤 “다시 확인”을 선택하세요.\n또는 “내 사본 사용…”을 선택하고 프로그램 폴더를 지정하세요.",
+ "engines.guidance.macos.tesseract": "Homebrew로 Tesseract를 설치(brew install tesseract tesseract-lang)한 뒤 “다시 확인”을 선택하세요.\n또는 “내 프로그램 쓰기…”을 선택하고 bin 폴더를 지정하세요.",
+ "engines.guidance.linux.tesseract": "패키지 관리자로 Tesseract를 설치(Ubuntu/Debian: sudo apt install tesseract-ocr tesseract-ocr-all)한 뒤 “다시 확인”을 선택하세요.\n또는 “내 프로그램 쓰기…”을 선택하고 프로그램 폴더를 지정하세요.",
"engines.guidance.windows.tesseract": "UB-Mannheim(github.com/UB-Mannheim/tesseract)에서 Tesseract를 설치한 뒤 “다시 확인”을 선택하거나 설치 폴더를 지정하세요.",
"engines.use_own_app_folder": "{0}.app이 들어 있는 폴더 선택",
"engines.progress_detail": "{0} / {1} · {2}/s · {3}",
diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.json
index a12e691e..0a129bb3 100644
--- a/src/Filee.App/Assets/i18n/ru.json
+++ b/src/Filee.App/Assets/i18n/ru.json
@@ -680,8 +680,8 @@
"engines.guidance.windows": "Filee не может скачать {0} для этого компьютера.\nВыберите «Использовать свою копию…» и укажите установленную копию.",
"engines.package.tesseract.name": "Tesseract (многоязычное распознавание текста)",
"engines.package.tesseract.description": "Распознает текст на более чем 100 языках, включая деванагари (непальский/хинди), арабский и редкие письменности.\nВ macOS Apple Vision работает нативно для английского, CJK и европейских языков без него.",
- "engines.guidance.macos.tesseract": "Установите Tesseract с помощью Homebrew (brew install tesseract tesseract-lang), затем выберите «Проверить снова».\nИли выберите «Использовать мою копию…» и укажите папку bin.",
- "engines.guidance.linux.tesseract": "Установите Tesseract с помощью пакетного менеджера (Ubuntu/Debian: sudo apt install tesseract-ocr tesseract-ocr-all), затем выберите «Проверить снова».\nИли выберите «Использовать мою копию…» и укажите папку программы.",
+ "engines.guidance.macos.tesseract": "Установите Tesseract с помощью Homebrew (brew install tesseract tesseract-lang), затем выберите «Проверить снова».\nИли выберите «Использовать свою копию…» и укажите папку bin.",
+ "engines.guidance.linux.tesseract": "Установите Tesseract с помощью пакетного менеджера (Ubuntu/Debian: sudo apt install tesseract-ocr tesseract-ocr-all), затем выберите «Проверить снова».\nИли выберите «Использовать свою копию…» и укажите папку программы.",
"engines.guidance.windows.tesseract": "Установите Tesseract из UB-Mannheim (github.com/UB-Mannheim/tesseract), затем выберите «Проверить снова» или укажите папку установки.",
"engines.use_own_app_folder": "Выберите папку, в которой лежит {0}.app",
"engines.progress_detail": "{0} из {1} · {2}/с · {3}",
diff --git a/src/Filee.App/Services/EngineDownloadService.cs b/src/Filee.App/Services/EngineDownloadService.cs
index 14cd8cac..59ab2239 100644
--- a/src/Filee.App/Services/EngineDownloadService.cs
+++ b/src/Filee.App/Services/EngineDownloadService.cs
@@ -44,9 +44,7 @@ public sealed partial class EnginePackageState : ObservableObject
public string Sizes => EngineDownloads.CanDownload(Package)
? _loc.Format("engines.package.size", FormatBytes(EngineDownloads.DownloadSize(Package)), FormatBytes(Package.InstalledSize))
- : CanSystemInstall(Package)
- ? _loc.Format("engines.package.size", FormatBytes(350_000_000), FormatBytes(Package.InstalledSize))
- : _loc["engines.package.system_required"];
+ : _loc["engines.package.system_required"];
/// <summary>
/// How to get an engine Filee doesn't download on this system (FFmpeg and Ghostscript from Homebrew on macOS,
@@ -56,7 +54,7 @@ public sealed partial class EnginePackageState : ObservableObject
{
get
{
- if (EngineDownloads.CanDownload(Package) || CanSystemInstall(Package))
+ if (EngineDownloads.CanDownload(Package))
return null;
var system = OperatingSystem.IsMacOS() ? "macos" : OperatingSystem.IsLinux() ? "linux" : "windows";
var key = $"engines.guidance.{system}.{Package.Id}";
@@ -116,7 +114,7 @@ public sealed partial class EnginePackageState : ObservableObject
/// <summary>The engine is there: Filee's download or the user's own copy.</summary>
public bool IsInstalled => Status is EnginePackageStatus.Installed or EnginePackageStatus.OwnCopy;
- public bool CanRemove => Status == EnginePackageStatus.Installed || (Package.Id == "tesseract" && Status == EnginePackageStatus.OwnCopy && FromSystem);
+ public bool CanRemove => Status == EnginePackageStatus.Installed;
/// <summary>The user chose this copy ("Stop using it" applies).</summary>
public bool UsesOwnCopy => Status == EnginePackageStatus.OwnCopy && !FromSystem;
public bool ShowsCopyFolder => Status == EnginePackageStatus.OwnCopy;
@@ -124,33 +122,7 @@ public sealed partial class EnginePackageState : ObservableObject
|| (Status == EnginePackageStatus.OwnCopy && FromSystem);
public bool IsBusy => Status is EnginePackageStatus.Queued or EnginePackageStatus.Downloading or EnginePackageStatus.Unpacking;
/// <summary>Also when a copy on the PC is used: Filee's download is the tested version and wins once installed.</summary>
- public bool CanInstall => (EngineDownloads.CanDownload(Package) && (Status is EnginePackageStatus.NotInstalled or EnginePackageStatus.Failed || (Status == EnginePackageStatus.OwnCopy && FromSystem)))
- || (CanSystemInstall(Package) && Status is EnginePackageStatus.NotInstalled or EnginePackageStatus.Failed);
-
- /// <summary>
- /// Whether the app can drive an in-place install of <paramref name="package"/> on this machine: Homebrew on
- /// macOS, winget on Windows. Linux has no handler here (package managers vary too much to drive one
- /// generically), so it keeps the same guidance-text + "Use my copy…" flow as every other engine.
- /// </summary>
- internal static bool CanSystemInstall(EnginePackage package) =>
- package.Id == "tesseract" && (OperatingSystem.IsMacOS() ? LocateBrew() is not null : OperatingSystem.IsWindows());
-
- /// <summary>Homebrew's own binary, at its two standard prefixes or on PATH; null when it isn't installed.</summary>
- internal static string? LocateBrew()
- {
- if (File.Exists("/opt/homebrew/bin/brew")) return "/opt/homebrew/bin/brew";
- if (File.Exists("/usr/local/bin/brew")) return "/usr/local/bin/brew";
- var path = Environment.GetEnvironmentVariable("PATH");
- if (path is not null)
- {
- foreach (var part in path.Split(':'))
- {
- var candidate = Path.Combine(part, "brew");
- if (File.Exists(candidate)) return candidate;
- }
- }
- return null;
- }
+ public bool CanInstall => EngineDownloads.CanDownload(Package) && (Status is EnginePackageStatus.NotInstalled or EnginePackageStatus.Failed || (Status == EnginePackageStatus.OwnCopy && FromSystem));
public string StatusText => Status switch
{
@@ -285,33 +257,26 @@ public sealed class EngineDownloadService
await _oneAtATime.WaitAsync(cancellation.Token);
try
{
- if (state.Package.Id == "tesseract" && !EngineDownloads.CanDownload(state.Package))
- {
- await InstallTesseractSystemAsync(state, cancellation.Token);
- }
- else
+ // Progress<T> reports on the UI thread it was created on.
+ var clock = Stopwatch.StartNew();
+ var rate = new TransferRate();
+ var progress = new Progress<EngineInstallProgress>(p =>
{
- // Progress<T> reports on the UI thread it was created on.
- var clock = Stopwatch.StartNew();
- var rate = new TransferRate();
- var progress = new Progress<EngineInstallProgress>(p =>
+ if (!state.IsBusy)
+ return;
+ state.Status = p.Stage == EngineInstallStage.Downloading ? EnginePackageStatus.Downloading : EnginePackageStatus.Unpacking;
+ state.Progress = p.Fraction * 100;
+ if (p.Stage == EngineInstallStage.Downloading && p.BytesTotal > 0)
{
- if (!state.IsBusy)
- return;
- state.Status = p.Stage == EngineInstallStage.Downloading ? EnginePackageStatus.Downloading : EnginePackageStatus.Unpacking;
- state.Progress = p.Fraction * 100;
- if (p.Stage == EngineInstallStage.Downloading && p.BytesTotal > 0)
- {
- rate.Add(clock.Elapsed, p.BytesDone);
- state.ShowTransfer(p.BytesDone, p.BytesTotal, rate);
- }
- else
- {
- state.ProgressDetail = "";
- }
- });
- await Task.Run(() => _installer.InstallAsync(state.Package, progress, cancellation.Token), cancellation.Token);
- }
+ rate.Add(clock.Elapsed, p.BytesDone);
+ state.ShowTransfer(p.BytesDone, p.BytesTotal, rate);
+ }
+ else
+ {
+ state.ProgressDetail = "";
+ }
+ });
+ await Task.Run(() => _installer.InstallAsync(state.Package, progress, cancellation.Token), cancellation.Token);
}
finally
{
@@ -340,142 +305,6 @@ public sealed class EngineDownloadService
}
}
- private static async Task InstallTesseractSystemAsync(EnginePackageState state, CancellationToken cancellationToken)
- {
- try
- {
- if (OperatingSystem.IsMacOS())
- {
- var brew = EnginePackageState.LocateBrew();
- if (brew is null)
- throw new InvalidOperationException("Homebrew was not found. Install Homebrew from https://brew.sh first.");
-
- state.Status = EnginePackageStatus.Downloading;
- state.Progress = 5;
- state.ProgressDetail = "Starting Homebrew install...";
-
- var psi = new ProcessStartInfo
- {
- FileName = brew,
- ArgumentList = { "install", "tesseract", "tesseract-lang" },
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- UseShellExecute = false,
- CreateNoWindow = true
- };
- var brewDir = Path.GetDirectoryName(brew);
- var currentPath = Environment.GetEnvironmentVariable("PATH") ?? "";
- if (brewDir is not null && !currentPath.Contains(brewDir))
- psi.Environment["PATH"] = brewDir + ":" + currentPath;
- psi.Environment["HOMEBREW_NO_AUTO_UPDATE"] = "1";
- psi.Environment["HOMEBREW_NO_ENV_HINTS"] = "1";
-
- using var process = new Process { StartInfo = psi };
- process.OutputDataReceived += (_, e) =>
- {
- if (!string.IsNullOrWhiteSpace(e.Data))
- {
- Dispatcher.UIThread.Post(() =>
- {
- var line = e.Data.Trim();
- state.ProgressDetail = line;
- if (line.StartsWith("==> Downloading", StringComparison.OrdinalIgnoreCase))
- {
- state.Status = EnginePackageStatus.Downloading;
- if (state.Progress < 50) state.Progress += 10;
- }
- else if (line.StartsWith("==> Pouring", StringComparison.OrdinalIgnoreCase) ||
- line.StartsWith("==> Installing", StringComparison.OrdinalIgnoreCase))
- {
- state.Status = EnginePackageStatus.Unpacking;
- if (state.Progress < 90) state.Progress += 10;
- }
- });
- }
- };
- process.ErrorDataReceived += (_, e) =>
- {
- if (!string.IsNullOrWhiteSpace(e.Data))
- {
- Dispatcher.UIThread.Post(() =>
- {
- state.ProgressDetail = e.Data.Trim();
- });
- }
- };
-
- process.Start();
- process.BeginOutputReadLine();
- process.BeginErrorReadLine();
-
- using (cancellationToken.Register(() =>
- {
- try { process.Kill(true); } catch { }
- }))
- {
- await process.WaitForExitAsync(cancellationToken);
- }
-
- if (process.ExitCode != 0)
- throw new InvalidOperationException($"Homebrew install failed with exit code {process.ExitCode}.");
-
- state.Progress = 100;
- state.ProgressDetail = "";
- }
- else if (OperatingSystem.IsWindows())
- {
- state.Status = EnginePackageStatus.Downloading;
- state.Progress = 10;
- state.ProgressDetail = "Installing Tesseract with winget...";
-
- var psi = new ProcessStartInfo
- {
- FileName = "winget",
- ArgumentList = { "install", "UB-Mannheim.TesseractOCR", "--accept-source-agreements", "--accept-package-agreements" },
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- UseShellExecute = false,
- CreateNoWindow = true
- };
-
- using var process = new Process { StartInfo = psi };
- process.OutputDataReceived += (_, e) =>
- {
- if (!string.IsNullOrWhiteSpace(e.Data))
- {
- Dispatcher.UIThread.Post(() =>
- {
- state.ProgressDetail = e.Data.Trim();
- });
- }
- };
-
- process.Start();
- process.BeginOutputReadLine();
-
- using (cancellationToken.Register(() =>
- {
- try { process.Kill(true); } catch { }
- }))
- {
- await process.WaitForExitAsync(cancellationToken);
- }
-
- if (process.ExitCode != 0)
- throw new InvalidOperationException($"winget install failed with exit code {process.ExitCode}.");
- }
- else
- {
- throw new PlatformNotSupportedException("Automatic installation is not supported on this platform. Please install tesseract using your package manager.");
- }
- }
- finally
- {
- state.Cancellation = null;
- state.ProgressDetail = "";
- }
- }
-
public void Cancel(EnginePackageState state) => state.Cancellation?.Cancel();
/// <summary>
@@ -517,43 +346,6 @@ public sealed class EngineDownloadService
return false;
try
{
- if (state.Package.Id == "tesseract")
- {
- if (OperatingSystem.IsMacOS())
- {
- var brew = EnginePackageState.LocateBrew();
- if (brew is not null)
- {
- var psi = new ProcessStartInfo
- {
- FileName = brew,
- ArgumentList = { "uninstall", "tesseract", "tesseract-lang" },
- UseShellExecute = false,
- CreateNoWindow = true
- };
- var brewDir = Path.GetDirectoryName(brew);
- var currentPath = Environment.GetEnvironmentVariable("PATH") ?? "";
- if (brewDir is not null && !currentPath.Contains(brewDir))
- psi.Environment["PATH"] = brewDir + ":" + currentPath;
- psi.Environment["HOMEBREW_NO_AUTO_UPDATE"] = "1";
- psi.Environment["HOMEBREW_NO_ENV_HINTS"] = "1";
- using var process = Process.Start(psi);
- process?.WaitForExit();
- }
- }
- else if (OperatingSystem.IsWindows())
- {
- using var process = Process.Start(new ProcessStartInfo
- {
- FileName = "winget",
- ArgumentList = { "uninstall", "UB-Mannheim.TesseractOCR" },
- UseShellExecute = false,
- CreateNoWindow = true
- });
- process?.WaitForExit();
- }
- EngineEnvironment.ForgetSystemCopies();
- }
_installer.Uninstall(state.Package);
state.Status = StatusOf(state);
_catalog.Refresh();
diff --git a/src/Filee.App/ViewModels/PresetEditorViewModel.cs b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
index 43bc2b5f..ac723bc5 100644
--- a/src/Filee.App/ViewModels/PresetEditorViewModel.cs
+++ b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
@@ -181,8 +181,7 @@ public sealed partial class PresetEditorViewModel : ObservableObject
new Choice<string?>("tg", "Tajik / Тоҷикӣ"),
new Choice<string?>("tt", "Tatar / Татарча"),
];
- // Vision and Tesseract each claim a real, limited set of languages (SupportedLanguages); the old PaddleOCR
- // engine claimed over 100 it could never actually recognize (no bundled model, no install path). Only offer
+ // Vision and Tesseract each claim a real, supported set of languages (SupportedLanguages). Only offer
// what a registered engine can really attempt, so picking a language never leads straight to "engine missing".
OcrLanguages = [.. OcrLanguages.Where(c => c.Value is null || OcrEngineRegistry.AnyEngineSupports(c.Value))];
diff --git a/src/Filee.Cli/CliHost.cs b/src/Filee.Cli/CliHost.cs
index dae34da3..14bb8b43 100644
--- a/src/Filee.Cli/CliHost.cs
+++ b/src/Filee.Cli/CliHost.cs
@@ -34,8 +34,6 @@ internal sealed class CliHost : IAsyncDisposable
EngineEnvironment.LibreOfficeForDocuments = store.Settings.LibreOfficeForDocuments;
EngineEnvironment.LibreOfficeForSheets = store.Settings.LibreOfficeForSheets;
var converters = EngineRegistry.CreateAll(new EngineEnvironment(store.Directory));
- if (OperatingSystem.IsMacOS())
- Filee.Core.Ocr.OcrEngineRegistry.Register(new Filee.Platform.MacOS.MacOsVisionOcrEngine());
var catalog = new ConverterCatalog(converters) { Priority = store.Settings.EnginePriority };
var queue = new JobQueue(catalog, EngineRegistry.FindPdfMerger(converters), combiner: EngineRegistry.FindFileCombiner(converters),
tiffMerger: EngineRegistry.FindTiffMerger(converters));
diff --git a/src/Filee.Core/Ocr/OcrEngineRegistry.cs b/src/Filee.Core/Ocr/OcrEngineRegistry.cs
index 504bdf7d..998a968b 100644
--- a/src/Filee.Core/Ocr/OcrEngineRegistry.cs
+++ b/src/Filee.Core/Ocr/OcrEngineRegistry.cs
@@ -132,4 +132,13 @@ public static class OcrEngineRegistry
return RegisteredEngines.FirstOrDefault(e => e.Id.Equals(id, StringComparison.OrdinalIgnoreCase));
}
}
+
+ /// <summary>Clears registered engines for test isolation.</summary>
+ internal static void ResetForTesting()
+ {
+ lock (Sync)
+ {
+ RegisteredEngines.Clear();
+ }
+ }
}
diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.cs
index eada26a5..96957f17 100644
--- a/src/Filee.Core/Presets/Preset.cs
+++ b/src/Filee.Core/Presets/Preset.cs
@@ -426,7 +426,7 @@ public sealed class DocumentOptions
/// <summary>
/// Optical Character Recognition for scanned pages and images.
/// </summary>
- public bool Ocr { get; set; } = true;
+ public bool Ocr { get; set; }
/// <summary>
/// Optical Character Recognition language code (e.g. "en-US", "ko-KR"). Null uses the system default.
diff --git a/src/Filee.Core/Settings/AppSettings.cs b/src/Filee.Core/Settings/AppSettings.cs
index 4f488dcb..9304306c 100644
--- a/src/Filee.Core/Settings/AppSettings.cs
+++ b/src/Filee.Core/Settings/AppSettings.cs
@@ -112,7 +112,7 @@ public sealed class DonutSettings
public sealed class AppSettings
{
/// <summary>Current schema version. Increase when the format changes and add a migration.</summary>
- public const int CurrentSchemaVersion = 32;
+ public const int CurrentSchemaVersion = 33;
public int SchemaVersion { get; set; } = CurrentSchemaVersion;
@@ -184,7 +184,7 @@ public sealed class AppSettings
[
"magick", "pdfium", "pdfsharp", "rhwp", "unhwp", "markdown", "spreadsheet", "ooxml", "hwpx-writer", "docx-writer",
"pdf-text", "pdf-slides", "email", "data", "vector", "icns", "font", "cad", "model3d", "geo", "archive", "ebook",
- "subtitle", "wic", "ffmpeg", "pandoc", "ghostscript", "calibre", "libreoffice",
+ "subtitle", "wic", "image-ocr", "ffmpeg", "pandoc", "ghostscript", "calibre", "libreoffice",
];
/// <summary>Set once the first-run welcome has been shown.</summary>
diff --git a/src/Filee.Core/Settings/UserDataStore.cs b/src/Filee.Core/Settings/UserDataStore.cs
index 7aff0c84..5e2910da 100644
--- a/src/Filee.Core/Settings/UserDataStore.cs
+++ b/src/Filee.Core/Settings/UserDataStore.cs
@@ -403,6 +403,10 @@ internal static class SettingsMigrations
if (settings.SchemaVersion < 21)
InsertBefore(settings.EnginePriority, "wic", settings.EnginePriority.Contains("ffmpeg") ? "ffmpeg" : "pandoc");
+ // v33: built-in "image-ocr" engine (image to text/docx/md via Vision/Tesseract) before external converters.
+ if (settings.SchemaVersion < 33 && !settings.EnginePriority.Contains("image-ocr"))
+ InsertBefore(settings.EnginePriority, "image-ocr", settings.EnginePriority.Contains("ffmpeg") ? "ffmpeg" : "pandoc");
+
if (settings.Triggers.Count == 0)
settings.Triggers = TriggerGesture.Defaults();
diff --git a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs
index 2920b655..725c93d1 100644
--- a/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs
+++ b/src/Filee.Engines/Hwp/Hwpx/DocumentReaders.cs
@@ -111,9 +111,9 @@ internal static class DocumentReaders
var media = Path.Combine(workDirectory, $"media-{Guid.NewGuid():N}");
if (format.Equals("pdf", StringComparison.OrdinalIgnoreCase))
{
- var enableOcr = preset?.Document.Ocr ?? true;
+ var enableOcr = preset?.Document.Ocr ?? false;
var ocrLang = preset?.Document.OcrLanguage;
- return await Task.Run(() => PdfDocumentReader.Read(path, media, null, ocrLang, enableOcr), cancellationToken);
+ return await PdfDocumentReader.ReadAsync(path, media, null, ocrLang, enableOcr, workDirectory, cancellationToken: cancellationToken);
}
if (BuiltIn.TryGetValue(format, out var reader))
return await Task.Run(() => reader(path, media), cancellationToken);
diff --git a/src/Filee.Engines/Ocr/ImageOcrConverter.cs b/src/Filee.Engines/Ocr/ImageOcrConverter.cs
index 64e2d217..259c1049 100644
--- a/src/Filee.Engines/Ocr/ImageOcrConverter.cs
+++ b/src/Filee.Engines/Ocr/ImageOcrConverter.cs
@@ -15,7 +15,7 @@ namespace Filee.Engines.Ocr;
/// </summary>
public sealed class ImageOcrConverter : IConverter
{
- private static readonly string[] ImageFormats = ["png", "jpg", "jpeg", "webp", "tiff", "bmp"];
+ private static readonly string[] ImageFormats = ["png", "jpg", "webp", "tiff", "bmp"];
public string Id => "image-ocr";
public string DisplayName => "Image OCR (built-in)";
diff --git a/src/Filee.Engines/Ocr/TesseractOcrEngine.cs b/src/Filee.Engines/Ocr/TesseractOcrEngine.cs
index 2baea8ce..c8eed04a 100644
--- a/src/Filee.Engines/Ocr/TesseractOcrEngine.cs
+++ b/src/Filee.Engines/Ocr/TesseractOcrEngine.cs
@@ -1,6 +1,6 @@
-// Tesseract OCR engine: a single self-contained binary plus pinned .traineddata files, found the same way as every
-// other optional engine (OwnCopies, Filee's own copy, then one already on the PC). The real cross-platform, no-Python
-// fallback for platforms without a native OCR API (Windows, Linux) and for scripts Apple Vision doesn't recognize.
+// Tesseract OCR engine, run as a subprocess with ArgumentList and a timeout, found via the user's own copy or on
+// the system path. Cross-platform fallback for platforms without a native OCR API (Windows, Linux) and for scripts
+// Apple Vision does not recognize (Devanagari, Cyrillic beyond Russian/Ukrainian, Thai, Greek, Arabic script, ...).
using System.Globalization;
using Filee.Core.Ocr;
@@ -25,8 +25,7 @@ public sealed class TesseractOcrEngine : IOcrEngine
/// <summary>
/// Filee's language codes this engine can be asked to recognize, limited to what Tesseract's own trained-data
/// set actually ships (tessdata_fast/best): asking for a code outside this list always fails, so the UI and
- /// <see cref="OcrEngineRegistry"/> only offer what can really work, instead of PaddleOCR's old claimed-but-never
- /// bundled 100+ languages.
+ /// <see cref="OcrEngineRegistry"/> only offer what can really work.
/// </summary>
public IReadOnlyList<string> SupportedLanguages => [.. LanguageMap.Keys];
diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.cs
index bd67cde4..07337741 100644
--- a/src/Filee.Engines/Pdf/PdfDocumentReader.cs
+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.cs
@@ -20,6 +20,8 @@ using System.Text.RegularExpressions;
using Filee.Core.Ocr;
using Filee.Engines.Hwp.Hwpx;
using Filee.Engines.Infrastructure;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Logging.Abstractions;
using PDFtoImage;
using SkiaSharp;
using UglyToad.PdfPig;
@@ -106,10 +108,12 @@ internal sealed partial class PdfDocumentReader
private readonly IOcrEngine? _ocrEngine;
private readonly string? _ocrLanguage;
private readonly bool _enableOcr;
+ private readonly string? _workDirectory;
+ private readonly ILogger _logger;
private byte[]? _pdfBytes;
private int _pictures;
- private PdfDocumentReader(string path, string mediaFolder, bool textOnly, IOcrEngine? ocrEngine = null, string? ocrLanguage = null, bool enableOcr = true)
+ private PdfDocumentReader(string path, string mediaFolder, bool textOnly, IOcrEngine? ocrEngine = null, string? ocrLanguage = null, bool enableOcr = false, string? workDirectory = null, ILogger? logger = null)
{
_path = path;
_mediaFolder = mediaFolder;
@@ -117,14 +121,19 @@ internal sealed partial class PdfDocumentReader
_ocrEngine = ocrEngine;
_ocrLanguage = ocrLanguage;
_enableOcr = enableOcr;
+ _workDirectory = workDirectory;
+ _logger = logger ?? NullLogger.Instance;
}
/// <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();
- public static HDocument Read(string path, string mediaFolder, IOcrEngine? ocrEngine, string? ocrLanguage = null, bool enableOcr = true) =>
- new PdfDocumentReader(path, mediaFolder, textOnly: false, ocrEngine, ocrLanguage, enableOcr).ReadDocument();
+ public static HDocument Read(string path, string mediaFolder, IOcrEngine? ocrEngine, string? ocrLanguage = null, bool enableOcr = false, string? workDirectory = null, ILogger? logger = null) =>
+ new PdfDocumentReader(path, mediaFolder, textOnly: false, ocrEngine, ocrLanguage, enableOcr, workDirectory, logger).ReadDocument();
+
+ public static Task<HDocument> ReadAsync(string path, string mediaFolder, IOcrEngine? ocrEngine, string? ocrLanguage = null, bool enableOcr = false, string? workDirectory = null, ILogger? logger = null, CancellationToken cancellationToken = default) =>
+ new PdfDocumentReader(path, mediaFolder, textOnly: false, ocrEngine, ocrLanguage, enableOcr, workDirectory, logger).ReadDocumentAsync(cancellationToken);
/// <summary>
/// Reads only the text, including invisible text (the OCR layer of scanned PDFs); pictures and scans are left out.
@@ -132,8 +141,11 @@ internal sealed partial class PdfDocumentReader
public static HDocument ReadText(string path) =>
new PdfDocumentReader(path, "", textOnly: true).ReadDocument();
- public static HDocument ReadText(string path, IOcrEngine? ocrEngine, string? ocrLanguage = null, bool enableOcr = true) =>
- new PdfDocumentReader(path, "", textOnly: true, ocrEngine, ocrLanguage, enableOcr).ReadDocument();
+ public static HDocument ReadText(string path, IOcrEngine? ocrEngine, string? ocrLanguage = null, bool enableOcr = false, string? workDirectory = null, ILogger? logger = null) =>
+ new PdfDocumentReader(path, "", textOnly: true, ocrEngine, ocrLanguage, enableOcr, workDirectory, logger).ReadDocument();
+
+ public static Task<HDocument> ReadTextAsync(string path, IOcrEngine? ocrEngine, string? ocrLanguage = null, bool enableOcr = false, string? workDirectory = null, ILogger? logger = null, CancellationToken cancellationToken = default) =>
+ new PdfDocumentReader(path, "", textOnly: true, ocrEngine, ocrLanguage, enableOcr, workDirectory, logger).ReadDocumentAsync(cancellationToken);
// ───────────────────────── Model of a page ─────────────────────────
@@ -215,7 +227,9 @@ internal sealed partial class PdfDocumentReader
// ───────────────────────── Document ─────────────────────────
- private HDocument ReadDocument()
+ private HDocument ReadDocument() => ReadDocumentAsync(CancellationToken.None).GetAwaiter().GetResult();
+
+ private async Task<HDocument> ReadDocumentAsync(CancellationToken cancellationToken)
{
PdfDocument pdf;
try
@@ -233,7 +247,10 @@ internal sealed partial class PdfDocumentReader
try
{
foreach (var page in pdf.GetPages())
- pages.Add(ReadPage(page));
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+ pages.Add(await ReadPageAsync(page, cancellationToken));
+ }
}
catch (PdfDocumentEncryptedException)
{
@@ -283,13 +300,23 @@ internal sealed partial class PdfDocumentReader
}
}
- private PageContent ReadPage(Page page)
+ private PageContent ReadPage(Page page) => ReadPageAsync(page, CancellationToken.None).GetAwaiter().GetResult();
+
+ private async Task<PageContent> ReadPageAsync(Page page, CancellationToken cancellationToken)
{
var crop = page.CropBox.Bounds;
// A page turned by 90° is shown (and rendered) with width and height swapped.
var rotated = page.Rotation.Value % 180 != 0;
var content = new PageContent(page.Number - 1, rotated ? crop.Height : crop.Width, rotated ? crop.Width : crop.Height);
+ // Turned pages when not text-only: always keep the rendered page, exactly as in main.
+ if (rotated && !_textOnly)
+ {
+ content.Scanned = true;
+ content.ScanPath = RenderScan(page.Number - 1);
+ return content;
+ }
+
var links = page.GetHyperlinks()
.Where(h => h.Uri is { } uri && (uri.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || uri.StartsWith("https://", StringComparison.OrdinalIgnoreCase) || uri.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase)))
.Select(h => (Bounds: Shift(h.Bounds, crop), Uri: h.Uri!))
@@ -325,43 +352,41 @@ internal sealed partial class PdfDocumentReader
if (words.Count == 0)
{
- // First check if the page already had an invisible OCR layer that was skipped for formatting
- foreach (var word in pageWords)
+ // When OCR is enabled, first check if the page already has an invisible OCR text layer (TextRenderingMode.Neither)
+ if (_enableOcr)
{
- if (string.IsNullOrWhiteSpace(word.Text))
- continue;
- var letters = word.Letters.Where(l => !string.IsNullOrWhiteSpace(l.Value)).ToList();
- if (letters.Count == 0)
- 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;
- 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));
+ foreach (var word in pageWords)
+ {
+ if (string.IsNullOrWhiteSpace(word.Text))
+ continue;
+ var letters = word.Letters.Where(l => !string.IsNullOrWhiteSpace(l.Value)).ToList();
+ if (letters.Count == 0)
+ 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;
+ 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));
+ }
}
- }
- if (words.Count == 0 || (rotated && !_textOnly))
- {
- if (_enableOcr)
+ // If still no words and OCR is enabled, run the OCR engine on the rendered scan
+ if (words.Count == 0 && _enableOcr)
{
var ocr = _ocrEngine ?? OcrEngineRegistry.GetDefault(_ocrLanguage);
if (ocr is not null && ocr.IsAvailable)
{
var scanPath = RenderScan(page.Number - 1);
+ var isTempScan = _textOnly || string.IsNullOrEmpty(_mediaFolder);
try
{
- var ocrResult = ocr.RecognizeAsync(scanPath, OcrEngineRegistry.ResolveLanguage(ocr, _ocrLanguage)).GetAwaiter().GetResult();
+ var ocrResult = await ocr.RecognizeAsync(scanPath, OcrEngineRegistry.ResolveLanguage(ocr, _ocrLanguage), cancellationToken);
if (ocrResult.Words.Count > 0)
{
var scaleX = content.Width / (double)ocrResult.ImageWidth;
var scaleY = content.Height / (double)ocrResult.ImageHeight;
- // One baseline/top/bottom per OCR-detected line, not per word: on a skewed or photographed
- // scan each word's own pixel box drifts slightly in Y, which (once scaled to PDF points)
- // is enough for Lines() below to split a single real text line into several interleaved
- // reconstructed lines. Tesseract already groups words into lines; use that box.
foreach (var line in ocrResult.Lines)
{
var pdfTop = content.Height - (line.Box.Top * scaleY);
@@ -377,9 +402,20 @@ internal sealed partial class PdfDocumentReader
}
}
}
- catch
+ catch (OperationCanceledException)
{
- // Fall back to pure scan if OCR encounters an error
+ throw;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogWarning(ex, "OCR failed on PDF page {Page}", page.Number);
+ }
+ finally
+ {
+ if (isTempScan && words.Count > 0 && File.Exists(scanPath))
+ {
+ try { File.Delete(scanPath); } catch { }
+ }
}
if (words.Count == 0)
@@ -394,7 +430,7 @@ internal sealed partial class PdfDocumentReader
if (words.Count == 0)
{
- // A scan, a page of drawings, or a turned page: the rendered page keeps everything.
+ // A scan, a page of drawings, or a page without OCR: the rendered page keeps everything.
content.Scanned = true;
if (!_textOnly)
content.ScanPath = RenderScan(page.Number - 1);
@@ -569,7 +605,11 @@ internal sealed partial class PdfDocumentReader
private string NewMediaPath(string extension)
{
- var folder = string.IsNullOrEmpty(_mediaFolder) ? Path.Combine(Path.GetTempPath(), "filee-media") : _mediaFolder;
+ var folder = !string.IsNullOrEmpty(_mediaFolder)
+ ? _mediaFolder
+ : !string.IsNullOrEmpty(_workDirectory)
+ ? Path.Combine(_workDirectory, "pdf-scans")
+ : Path.Combine(Path.GetTempPath(), $"filee-pdf-{Guid.NewGuid():N}");
Directory.CreateDirectory(folder);
return Path.Combine(folder, $"pdf-{++_pictures}.{extension}");
}
diff --git a/src/Filee.Engines/Pdf/PdfTextConverter.cs b/src/Filee.Engines/Pdf/PdfTextConverter.cs
index 1588eed2..9be3f907 100644
--- a/src/Filee.Engines/Pdf/PdfTextConverter.cs
+++ b/src/Filee.Engines/Pdf/PdfTextConverter.cs
@@ -35,12 +35,13 @@ public sealed class PdfTextConverter : IConverter
public EngineStatus GetStatus() => EngineStatus.Available("PDF → TXT, Markdown", $"{EngineVersions.BuiltIn} · {EngineVersions.Library("PdfPig", typeof(UglyToad.PdfPig.PdfDocument))}");
public Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken) =>
- Task.Run(() => step.To == "md" ? Markdown(step, progress, cancellationToken) : Text(step, progress, cancellationToken), cancellationToken);
+ step.To == "md" ? MarkdownAsync(step, progress, cancellationToken) : TextAsync(step, progress, cancellationToken);
- private static IReadOnlyList<string> Text(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken)
+ private static async Task<IReadOnlyList<string>> TextAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken)
{
progress?.Report(0.1);
- var text = PdfDocumentReader.PlainText(PdfDocumentReader.ReadText(step.InputPath, null, step.Preset.Document.OcrLanguage, step.Preset.Document.Ocr));
+ var doc = await PdfDocumentReader.ReadTextAsync(step.InputPath, null, step.Preset.Document.OcrLanguage, step.Preset.Document.Ocr, step.WorkDirectory, cancellationToken: cancellationToken);
+ var text = PdfDocumentReader.PlainText(doc);
cancellationToken.ThrowIfCancellationRequested();
if (text.Length == 0)
throw NoText();
@@ -54,13 +55,13 @@ public sealed class PdfTextConverter : IConverter
return [output];
}
- private static IReadOnlyList<string> Markdown(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken)
+ private static async Task<IReadOnlyList<string>> MarkdownAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken)
{
progress?.Report(0.1);
var textOnly = step.Preset.Document.LeaveOutPictures;
var document = textOnly
- ? PdfDocumentReader.ReadText(step.InputPath, null, step.Preset.Document.OcrLanguage, step.Preset.Document.Ocr)
- : PdfDocumentReader.Read(step.InputPath, Path.Combine(step.WorkDirectory, $"media-{Guid.NewGuid():N}"), null, step.Preset.Document.OcrLanguage, step.Preset.Document.Ocr);
+ ? await PdfDocumentReader.ReadTextAsync(step.InputPath, null, step.Preset.Document.OcrLanguage, step.Preset.Document.Ocr, step.WorkDirectory, cancellationToken: cancellationToken)
+ : await PdfDocumentReader.ReadAsync(step.InputPath, Path.Combine(step.WorkDirectory, $"media-{Guid.NewGuid():N}"), null, step.Preset.Document.OcrLanguage, step.Preset.Document.Ocr, step.WorkDirectory, cancellationToken: cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
if (textOnly && PdfDocumentReader.PlainText(document).Length == 0)
throw NoText();
diff --git a/src/Filee.Platform.MacOS/MacOsVisionOcrEngine.cs b/src/Filee.Platform.MacOS/MacOsVisionOcrEngine.cs
index 780f9e86..61fe7809 100644
--- a/src/Filee.Platform.MacOS/MacOsVisionOcrEngine.cs
+++ b/src/Filee.Platform.MacOS/MacOsVisionOcrEngine.cs
@@ -57,13 +57,15 @@ public sealed class MacOsVisionOcrEngine : IOcrEngine
if (nsPath == 0)
return OcrPageResult.Empty(width, height);
+ nint request = 0;
+ nint handler = 0;
try
{
var urlClass = objc_getClass("NSURL");
var fileUrl = Send(urlClass, sel_registerName("fileURLWithPath:"), nsPath);
var reqClass = objc_getClass("VNRecognizeTextRequest");
- var request = Send(Send(reqClass, sel_registerName("alloc")), sel_registerName("init"));
+ request = Send(Send(reqClass, sel_registerName("alloc")), sel_registerName("init"));
SendInteger(request, sel_registerName("setRecognitionLevel:"), 0); // 0 = VNRequestTextRecognitionLevelAccurate
SendBool(request, sel_registerName("setUsesLanguageCorrection:"), true);
@@ -78,11 +80,15 @@ public sealed class MacOsVisionOcrEngine : IOcrEngine
CFRelease(langString);
}
}
+ else if (OperatingSystem.IsMacOSVersionAtLeast(13))
+ {
+ SendBool(request, sel_registerName("setAutomaticallyDetectsLanguage:"), true);
+ }
var dictClass = objc_getClass("NSDictionary");
var emptyDict = Send(dictClass, sel_registerName("dictionary"));
var handlerClass = objc_getClass("VNImageRequestHandler");
- var handler = Send(Send(handlerClass, sel_registerName("alloc")), sel_registerName("initWithURL:options:"), fileUrl, emptyDict);
+ handler = Send(Send(handlerClass, sel_registerName("alloc")), sel_registerName("initWithURL:options:"), fileUrl, emptyDict);
var arrayClass2 = objc_getClass("NSArray");
var requestsArray = Send(arrayClass2, sel_registerName("arrayWithObject:"), request);
@@ -161,6 +167,10 @@ public sealed class MacOsVisionOcrEngine : IOcrEngine
}
finally
{
+ if (handler != 0)
+ SendVoid(handler, sel_registerName("release"));
+ if (request != 0)
+ SendVoid(request, sel_registerName("release"));
CFRelease(nsPath);
}
}, cancellationToken);
@@ -180,8 +190,15 @@ public sealed class MacOsVisionOcrEngine : IOcrEngine
var image = Send(alloc, sel_registerName("initWithContentsOfFile:"), nsPath);
if (image == 0)
return (0, 0);
- var size = SendSize(image, sel_registerName("size"));
- return ((int)Math.Round(size.Width), (int)Math.Round(size.Height));
+ try
+ {
+ var size = SendSize(image, sel_registerName("size"));
+ return ((int)Math.Round(size.Width), (int)Math.Round(size.Height));
+ }
+ finally
+ {
+ SendVoid(image, sel_registerName("release"));
+ }
}
finally
{
@@ -262,7 +279,20 @@ public sealed class MacOsVisionOcrEngine : IOcrEngine
private static extern float SendFloat(nint receiver, nint selector);
[DllImport(ObjC, EntryPoint = "objc_msgSend")]
- private static extern CGRect SendRect(nint receiver, nint selector);
+ private static extern CGRect SendRectArm64(nint receiver, nint selector);
+
+ [DllImport(ObjC, EntryPoint = "objc_msgSend_stret")]
+ private static extern void SendRectStret(out CGRect result, nint receiver, nint selector);
+
+ private static CGRect SendRect(nint receiver, nint selector)
+ {
+ if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
+ {
+ SendRectStret(out var rect, receiver, selector);
+ return rect;
+ }
+ return SendRectArm64(receiver, selector);
+ }
[DllImport(ObjC, EntryPoint = "objc_msgSend")]
private static extern CGSize SendSize(nint receiver, nint selector);
diff --git a/tests/Filee.App.Tests/EnginesPageTests.cs b/tests/Filee.App.Tests/EnginesPageTests.cs
index dcc491a4..813b0c69 100644
--- a/tests/Filee.App.Tests/EnginesPageTests.cs
+++ b/tests/Filee.App.Tests/EnginesPageTests.cs
@@ -113,15 +113,12 @@ public class EnginesPageTests
}
[AvaloniaFact]
- public void Tesseract_package_has_install_available_when_not_installed()
+ public void Tesseract_package_exists_and_offers_own_copy_or_guidance()
{
TestServices.EnsureInitialized("en");
var downloads = AppHost.Get<EngineDownloadService>();
var tesseract = downloads.Packages.FirstOrDefault(p => p.Package.Id == "tesseract");
Assert.NotNull(tesseract);
- if (EnginePackageState.CanSystemInstall(tesseract.Package))
- {
- Assert.True(tesseract.CanInstall || tesseract.IsInstalled);
- }
+ Assert.True(tesseract.CanUseOwnCopy || tesseract.IsInstalled);
}
}
diff --git a/tests/Filee.Core.Tests/OcrEngineRegistryTests.cs b/tests/Filee.Core.Tests/OcrEngineRegistryTests.cs
index 1a6f16e6..528efd98 100644
--- a/tests/Filee.Core.Tests/OcrEngineRegistryTests.cs
+++ b/tests/Filee.Core.Tests/OcrEngineRegistryTests.cs
@@ -1,10 +1,15 @@
+// Tests for OcrEngineRegistry, OcrPageResult, and OCR document options.
+
using Filee.Core.Ocr;
using Filee.Core.Presets;
namespace Filee.Core.Tests;
-public class OcrEngineRegistryTests
+public class OcrEngineRegistryTests : IDisposable
{
+ public OcrEngineRegistryTests() => OcrEngineRegistry.ResetForTesting();
+ public void Dispose() => OcrEngineRegistry.ResetForTesting();
+
private sealed class MockOcrEngine(string id, string name, bool available, IReadOnlyList<string> languages) : IOcrEngine
{
public string Id => id;
@@ -54,14 +59,14 @@ public class OcrEngineRegistryTests
public void DocumentOptions_defaults_and_cloning()
{
var options = new DocumentOptions();
- Assert.True(options.Ocr);
+ Assert.False(options.Ocr);
Assert.Null(options.OcrLanguage);
- options.Ocr = false;
+ options.Ocr = true;
options.OcrLanguage = "ne";
var clone = options.Clone();
- Assert.False(clone.Ocr);
+ Assert.True(clone.Ocr);
Assert.Equal("ne", clone.OcrLanguage);
}
diff --git a/tests/Filee.Core.Tests/UserDataStoreTests.cs b/tests/Filee.Core.Tests/UserDataStoreTests.cs
index 142da41e..111f201d 100644
--- a/tests/Filee.Core.Tests/UserDataStoreTests.cs
+++ b/tests/Filee.Core.Tests/UserDataStoreTests.cs
@@ -415,7 +415,7 @@ public class UserDataStoreTests
var store = new UserDataStore(dir.Path);
store.Load();
- Assert.Equal(["magick", "markdown", "spreadsheet", "ooxml", "hwpx-writer", "docx-writer", "pdf-text", "pdf-slides", "email", "data", "vector", "icns", "font", "cad", "model3d", "geo", "archive", "ebook", "subtitle", "wic", "ffmpeg", "pandoc", "ghostscript", "calibre", "libreoffice"], store.Settings.EnginePriority);
+ Assert.Equal(["magick", "markdown", "spreadsheet", "ooxml", "hwpx-writer", "docx-writer", "pdf-text", "pdf-slides", "email", "data", "vector", "icns", "font", "cad", "model3d", "geo", "archive", "ebook", "subtitle", "wic", "image-ocr", "ffmpeg", "pandoc", "ghostscript", "calibre", "libreoffice"], store.Settings.EnginePriority);
Assert.Equal(AppSettings.CurrentSchemaVersion, store.Settings.SchemaVersion);
}
@@ -467,7 +467,7 @@ public class UserDataStoreTests
var store = new UserDataStore(dir.Path);
store.Load();
- Assert.Equal(["magick", "markdown", "spreadsheet", "ooxml", "hwpx-writer", "docx-writer", "pdf-text", "pdf-slides", "email", "data", "vector", "icns", "font", "cad", "model3d", "geo", "archive", "ebook", "subtitle", "wic", "ffmpeg", "pandoc", "ghostscript", "calibre", "libreoffice"], store.Settings.EnginePriority);
+ Assert.Equal(["magick", "markdown", "spreadsheet", "ooxml", "hwpx-writer", "docx-writer", "pdf-text", "pdf-slides", "email", "data", "vector", "icns", "font", "cad", "model3d", "geo", "archive", "ebook", "subtitle", "wic", "image-ocr", "ffmpeg", "pandoc", "ghostscript", "calibre", "libreoffice"], store.Settings.EnginePriority);
var ids = store.Profiles.Select(p => p.Id).ToList();
Assert.Equal(ids.IndexOf("office") + 1, ids.IndexOf("spreadsheets"));
Assert.Equal(ids.IndexOf("office") + 2, ids.IndexOf("presentations"));
@@ -642,8 +642,8 @@ public class UserDataStoreTests
// ... and, from schema 15, the 3D model engine before Pandoc (no CAD or archive engine in this list), from
// schema 18 the PDF slides engine there too (no PDF text engine), from schema 21 the WIC engine (no FFmpeg),
- // from schema 25 the geospatial engine after the 3D models engine.
- Assert.Equal(["magick", "email", "data", "vector", "model3d", "geo", "pdf-slides", "wic", "pandoc", "libreoffice"], store.Settings.EnginePriority);
+ // from schema 25 the geospatial engine after the 3D models engine, from schema 33 the image OCR engine before Pandoc.
+ Assert.Equal(["magick", "email", "data", "vector", "model3d", "geo", "pdf-slides", "wic", "image-ocr", "pandoc", "libreoffice"], store.Settings.EnginePriority);
}
[Fact]
@@ -927,7 +927,7 @@ public class UserDataStoreTests
var store = new UserDataStore(dir.Path);
store.Load();
- Assert.Equal(["magick", "cad", "model3d", "geo", "archive", "pandoc", "libreoffice"], store.Settings.EnginePriority);
+ Assert.Equal(["magick", "cad", "model3d", "geo", "archive", "image-ocr", "pandoc", "libreoffice"], store.Settings.EnginePriority);
Assert.Equal(AppSettings.CurrentSchemaVersion, store.Settings.SchemaVersion);
}
@@ -1181,7 +1181,7 @@ public class UserDataStoreTests
var store = new UserDataStore(dir.Path);
store.Load();
- Assert.Equal(["magick", "pdfium", "docx-writer", "pdf-text", "pdf-slides", "email", "geo", "wic", "pandoc", "libreoffice"], store.Settings.EnginePriority);
+ Assert.Equal(["magick", "pdfium", "docx-writer", "pdf-text", "pdf-slides", "email", "geo", "wic", "image-ocr", "pandoc", "libreoffice"], store.Settings.EnginePriority);
Assert.Equal(AppSettings.CurrentSchemaVersion, store.Settings.SchemaVersion);
}
@@ -1196,7 +1196,21 @@ public class UserDataStoreTests
store.Load();
// Not near the top: the HWPX writer stays among the ranks that win ties (see EbookTests).
- Assert.Equal(["magick", "pdfium", "hwpx-writer", "subtitle", "wic", "ffmpeg", "geo", "pandoc", "libreoffice"], store.Settings.EnginePriority);
+ Assert.Equal(["magick", "pdfium", "hwpx-writer", "subtitle", "wic", "image-ocr", "ffmpeg", "geo", "pandoc", "libreoffice"], store.Settings.EnginePriority);
+ Assert.Equal(AppSettings.CurrentSchemaVersion, store.Settings.SchemaVersion);
+ }
+
+ [Fact]
+ public void Version_32_settings_get_the_image_ocr_engine_before_ffmpeg()
+ {
+ using var dir = new TempDir();
+ File.WriteAllText(Path.Combine(dir.Path, "settings.json"),
+ """{ "schemaVersion": 32, "enginePriority": ["magick", "pdfium", "ffmpeg", "pandoc", "libreoffice"] }""");
+
+ var store = new UserDataStore(dir.Path);
+ store.Load();
+
+ Assert.Equal(["magick", "pdfium", "image-ocr", "ffmpeg", "pandoc", "libreoffice"], store.Settings.EnginePriority);
Assert.Equal(AppSettings.CurrentSchemaVersion, store.Settings.SchemaVersion);
}
diff --git a/tests/Filee.Engines.Tests/TesseractOcrTests.cs b/tests/Filee.Engines.Tests/TesseractOcrTests.cs
index b5fe537a..01135b71 100644
--- a/tests/Filee.Engines.Tests/TesseractOcrTests.cs
+++ b/tests/Filee.Engines.Tests/TesseractOcrTests.cs
@@ -1,3 +1,5 @@
+// Tests for TesseractOcrEngine language mapping and image recognition.
+
using Filee.Core.Ocr;
using Filee.Engines.Ocr;
using SkiaSharp;
@@ -19,8 +21,7 @@ public sealed class TesseractOcrTests
[Fact]
public void MapLanguage_returns_null_for_a_code_with_no_trained_data()
{
- // Unlike the old PaddleOCR engine, which silently fell back to English for any code it didn't
- // recognize, an unmapped code must be reported as unsupported rather than guessed at.
+ // An unmapped code must be reported as unsupported rather than guessed at.
Assert.Null(TesseractOcrEngine.MapLanguage("xal"));
}
diff --git a/tests/Filee.Platform.Tests/VisionOcrTests.cs b/tests/Filee.Platform.Tests/VisionOcrTests.cs
index de906b16..68c7bc4e 100644
--- a/tests/Filee.Platform.Tests/VisionOcrTests.cs
+++ b/tests/Filee.Platform.Tests/VisionOcrTests.cs
@@ -1,3 +1,5 @@
+// Tests for Apple Vision OCR engine and macOS OCR pipelines.
+
using Filee.Core.Conversion;
using Filee.Core.Ocr;
using Filee.Core.Presets;
@@ -68,12 +70,13 @@ public sealed class VisionOcrTests
try
{
+ var ocrPreset = new Preset { Document = new DocumentOptions { Ocr = true } };
var txtOut = Path.Combine(workDir, "banner.txt");
var txtStep = new ConversionStep(
InputPath: imagePath,
From: "jpg",
To: "txt",
- Preset: new Preset(),
+ Preset: ocrPreset,
Output: new FixedPath(txtOut),
WorkDirectory: workDir);
@@ -88,7 +91,7 @@ public sealed class VisionOcrTests
InputPath: imagePath,
From: "jpg",
To: "docx",
- Preset: new Preset(),
+ Preset: ocrPreset,
Output: new FixedPath(docxOut),
WorkDirectory: workDir);
@@ -139,7 +142,7 @@ public sealed class VisionOcrTests
// Read the scanned PDF using PdfDocumentReader with OCR enabled
var mediaFolder = Path.Combine(workDir, "media");
- var document = PdfDocumentReader.Read(pdfPath, mediaFolder, engine);
+ var document = PdfDocumentReader.Read(pdfPath, mediaFolder, engine, enableOcr: true, workDirectory: workDir);
Assert.NotEmpty(document.Sections);
var paragraphs = document.Sections.SelectMany(s => s.Blocks).OfType<HParagraph>().ToList();
@@ -149,7 +152,7 @@ public sealed class VisionOcrTests
Assert.Contains("Filee", allText, StringComparison.OrdinalIgnoreCase);
// Verify plain text reader also extracts the OCR text
- var plainDoc = PdfDocumentReader.ReadText(pdfPath, engine);
+ var plainDoc = PdfDocumentReader.ReadText(pdfPath, engine, enableOcr: true, workDirectory: workDir);
var plainText = PdfDocumentReader.PlainText(plainDoc);
Assert.Contains("Filee", plainText, StringComparison.OrdinalIgnoreCase);
}