Skip to content
oculix-orgPublic

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

133 Commits

Folders and files

Repository files navigation

Legerix

Tesseract + Leptonica natives bundled as a cross-platform Maven artifact, with a thin Java loader. From Latin legere, "to read".

Modeled on Apertix, which does the same thing for OpenCV. Legerix solves the equivalent problem for JVM OCR bindings, Octachorix first: ship the matching Tesseract version with the application instead of relying on a system-wide apt install tesseract-ocr, which on Ubuntu 24.04 ships libtesseract.so.5.0.3 (Tesseract 5.3.4) and is missing the TessBaseAPIGetPAGEText symbol introduced in 5.5.x.

Coordinates

<dependency>
    <groupId>io.github.oculix-org</groupId>
    <artifactId>legerix</artifactId>
    <version>5.5.2-1</version>
</dependency>

Versioning: <tesseract-version>-<build-number>. A bump of the build number re-ships the same Tesseract version with a CI/packaging fix; a Tesseract upstream release resets the build to 1.

What's in the jar

Everything Legerix ships lives under META-INF/legerix/, and nothing else in a jar belongs to Legerix.

Path under META-INF/legerix/ Content
natives/linux-x86-64/ glibc ≥ 2.38 build (Ubuntu 24.04)
natives/linux-x86-64-legacy/ glibc ≥ 2.28 build (manylinux_2_28)
natives/linux-aarch64/ glibc ≥ 2.38 build
natives/linux-aarch64-legacy/ glibc ≥ 2.28 build
natives/darwin/ macOS x86_64
natives/darwin-aarch64/ macOS Apple Silicon
natives/win32-x86-64/ Windows x86_64 (vcpkg toolchain)
natives/<tier>/legerix-natives.txt what that tier ships, written at build time
tessdata/*.traineddata 5 bundled fast models, ~12 MB total (see below)
legerix.properties Legerix's own version and native versions

Plus io/github/julienmerconsulting/legerix/Legerix.class, the Java loader. Each tier directory contains both libtesseract and libleptonica; Windows also carries the vcpkg DLLs they import.

Reserved namespace, single source, strict manifest

A consumer is free to shade Legerix into a fat jar, and free to ship its own natives under whatever generic directory names it likes. Legerix reads none of them, and Legerix reads nothing through the class loader:

  • its own space. META-INF/legerix/ is Legerix's and only Legerix's. A consumer's OpenCV under linux-x86-64/ is simply not Legerix's business any more, and cannot end up in its cache (Legerix#21, measured by David Young: 24 MB of foreign native on macOS, 67 MB on Linux).
  • one source. The container of Legerix.class is opened once and the manifest, the natives and the language models are all read from it, entry by entry. getResourceAsStream() is never used for the payload: another jar earlier on the class path can expose the very same path and answer instead.
  • a contract, not a hint. No tier manifest, a declared file missing, the tesseract/leptonica pair not declared, a name that is not a plain file name, an entry twice: loadNatives() fails, before loading anything, saying which file and which jar. There is no fallback to the old generic paths and no reuse of an existing cache to make up for a broken payload.
  • a verified extraction. loadNatives() works in <cache>/<legerix version>/<tier>/, with the language models in <cache>/<legerix version>/tessdata/ beside it, the same directories from one run to the next; two applications on two Legerix versions never touch each other's files. Every declared file is checked against the payload, size and CRC-32 read from the jar's index, before it is reused, and extracted again otherwise, through a temporary file moved into place atomically. A start where nothing changed writes nothing. Nothing is ever deleted: a file the payload does not name is never loaded, and a *.traineddata dropped into tessdata/ stays there. A lock is held only while the directory is brought up to date, so a second JVM waits and then finds everything in place.
  • its own identity. The version comes from legerix.properties in the payload, not from the jar manifest, which belongs to the consumer once Legerix is shaded.

What this does not claim: it is not authentication. Someone who deliberately replaces a binary inside META-INF/legerix/ gets what they put there, and assertBundledTesseract() remains a version check, not a certificate of provenance. What it removes is collisions, ambiguous resolution and mixed extractions.

macOS prerequisite: Homebrew

On both macOS tiers the bundled Leptonica is linked against Homebrew's image codecs and references them by their Homebrew path (/opt/homebrew on Apple Silicon, /usr/local on Intel). A Mac without those formulae cannot load the natives. Install Homebrew (https://brew.sh) and:

brew install jpeg-turbo libpng libtiff webp zstd xz libdeflate

If they are missing, Legerix.loadNatives() fails with an UnsatisfiedLinkError whose message says exactly this, with the brew install line to run, before the raw dyld text. Linux needs nothing: its tiers link their codecs statically. Windows needs nothing either: its tier carries the vcpkg DLLs.

Public API

import io.github.julienmerconsulting.legerix.Legerix;

// Extract natives + tessdata to a per-user cache, load both libs into the JVM.
Path nativesDir   = Legerix.loadNatives();

// Absolute paths of the two files loadNatives() loaded: what an
// absolute-path binding takes. Never list the directory and guess.
Path tesseractLib = Legerix.getTesseractLibraryPath();
Path leptonicaLib = Legerix.getLeptonicaLibraryPath();

// Path to the extracted tessdata folder.
Path tessdataDir  = Legerix.getTessdataPath();

// Detected runtime tier on Linux: "modern", "legacy" or "n/a" off-Linux.
String tier       = Legerix.getGlibcTier();

// Tesseract upstream version of this payload (e.g. "5.5.2"), and Legerix's
// own full version (e.g. "5.5.2-1"). Both read from Legerix's own payload,
// never from the jar manifest, which is the consumer's after shading.
String tessVer    = Legerix.getTesseractVersion();
String legerixVer = Legerix.getLegerixVersion();

Typical wiring: Octachorix, by absolute path

Scribe scribe = Scribe.builder()
        .tesseractLibrary(Legerix.getTesseractLibraryPath())
        .leptonicaLibrary(Legerix.getLeptonicaLibraryPath())
        .datapath(Legerix.getTessdataPath())
        .language("eng")
        .build();
String text = scribe.read(image, EnumSet.noneOf(PageLevel.class)).text();

tess4j is not supported: it resolves libtesseract by short name through JNA and can bind a system Tesseract while Legerix's copy sits unused (see Legerix#20). loadNatives() refuses to run when tess4j is on the classpath and says so.

Languages / tessdata

Five lightweight tessdata_fast language models are bundled in the jar (~12 MB total), chosen to cover roughly 80% of the world's population by primary spoken language:

Code Language Approx. size
eng English ~4 MB
fra French ~1.1 MB
spa Spanish ~1 MB
chi_sim Simplified Chinese ~3 MB
hin Hindi ~3.2 MB

All five are tessdata_fast (LSTM-only, optimized for speed) — the same trade-off Tesseract makes for its mobile / embedded targets. For higher accuracy at the cost of size and latency, swap them out with tessdata_best from upstream.

Available programmatically via Legerix.BUNDLED_LANGUAGES.

Using a bundled language

Scribe fra = Scribe.builder()
        .tesseractLibrary(Legerix.getTesseractLibraryPath())
        .leptonicaLibrary(Legerix.getLeptonicaLibraryPath())
        .datapath(Legerix.getTessdataPath())
        .language("fra")                 // or "eng+fra" for combined
        .build();
Scribe eng = fra.withLanguage("eng");    // same natives, its own session

Adding more languages

If you need a language outside the bundled set (German, Japanese, Arabic, multi-language combinations, etc.), drop the corresponding *.traineddata file into the same cache directory that getTessdataPath() returns, then keep using it as the datapath:

Path tessdata = Legerix.getTessdataPath();
// On first run: download deu.traineddata into tessdata.resolve("deu.traineddata").
// (Get it from https://github.com/tesseract-ocr/tessdata_fast or tessdata_best.)
Scribe deu = Scribe.builder()
        .tesseractLibrary(Legerix.getTesseractLibraryPath())
        .leptonicaLibrary(Legerix.getLeptonicaLibraryPath())
        .datapath(tessdata)
        .language("deu")
        .build();

The cache directory is writable and persistent, keyed on the full Legerix version so a build-suffix bump invalidates stale extracted natives (~/.cache/legerix/5.5.2-1/<tier>-<run>/tessdata/ on Linux, %LOCALAPPDATA%\legerix\5.5.2-1\<tier>-<run>\tessdata\ on Windows).

Alternatively, point the session at a completely separate tessdata folder you control:

Scribe ara = Scribe.builder()
        .tesseractLibrary(Legerix.getTesseractLibraryPath())
        .leptonicaLibrary(Legerix.getLeptonicaLibraryPath())
        .datapath(Path.of("/opt/myapp/tessdata"))
        .language("ara")
        .build();

A future release may add a helper like Legerix.installLanguage("deu") that downloads on-demand and caches.

Glibc tier picker

On Linux, loadNatives() runs ldd --version at startup and picks linux-<arch>-legacy/ if the detected glibc is older than 2.38, else the modern build. No env var required, no system tesseract required, no apt install required.

For non-Linux platforms the tier is reported as "n/a".

Building locally

The natives are produced by GitHub Actions (.github/workflows/build.yml, 7-job matrix). Reproducing locally just for one platform, Linux or macOS:

# Builds the codecs, Leptonica and Tesseract from source, links the codecs
# statically on Linux, stages the two libraries of this host's tier under
# src/main/resources/META-INF/legerix/natives/<tier>/ and fetches tessdata.
./scripts/build-self-contained-local.sh
LEGERIX_ALLOW_EMPTY_TIERS=1 ./scripts/write-natives-manifest.sh
mvn -B install

The manifest step is not optional: a tier without one is a tier Legerix refuses to load. LEGERIX_ALLOW_EMPTY_TIERS=1 lets the six tiers this host did not build stay without a manifest; a release stages all seven and fails if one is empty. scripts/run-payload-probes.sh then exercises the packaging contract on the jar just built.

CI is the source of truth for cross-platform builds; local builds populate only the current host's resource directory.

License

MIT for Legerix itself. Bundled natives keep their upstream licenses (Apache 2.0 for Tesseract and the eng traineddata, BSD-2-Clause for Leptonica). See NOTICE and THIRD-PARTY.txt.

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages