extract

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package extract turns a page's content stream into positioned text.

This is the use case the rest of the repo exists to serve, and the reason it is its own package is the failure it has to avoid. Every column of the benchmark in docs/DESIGN.md §1 — 0.01% spaces, a 4,069-character "word", 6% of words over 25 characters — is one bug: PDF does not record words, only glyphs and the positions they are painted at, so a reader that does not reconstruct the gaps between them produces text that is character-for-character correct and unreadable.

Reconstruction needs three things at once, and no two of them live in the same package: the glyph's advance width (font), the composed text rendering matrix (content), and one threshold policy for deciding when a gap is a space (geom.Tolerance). This package is where they meet. It owns no parsing and no tables of its own.

What it produces is doc.Page — the same type the OCR path produces — so a scanned page and a born-digital one reach the sinks the same way.

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{Tol: geom.DefaultTolerance, KeepHidden: true}

DefaultOptions is extraction as the CLI runs it: artifacts dropped, hidden text kept, default tolerances.

Functions

func WrapSpace added in v1.0.0

func WrapSpace(spans []doc.Span, next string) bool

WrapSpace reports whether a line starting with next, wrapped on from one that ends spans, is joined to it by a space: unless a space is already there, both sides are a script written without them, or a dash holds the word together across the break. The space goes on the trailing end of spans' last span; appendLine says why.

Exported because a wrap is also what a page break cuts a paragraph at, and layout.PageBreaks, which joins the halves, has to make the same call at it.

Types

type Extractor

type Extractor struct {
	// contains filtered or unexported fields
}

Extractor reads pages from one document.

It is not safe for concurrent use, because objects.Store makes no such promise and the font cache is unsynchronized. Page-level parallelism uses one Extractor per worker over its own Store, which is what the --jobs flag will do.

func New

func New(s objects.Store, opt Options) *Extractor

New returns an Extractor over s. The Store is borrowed, not owned: closing it remains the caller's job, because the caller opened it and may still be probing it.

func (*Extractor) Document

func (e *Extractor) Document() (*doc.Document, error)

Document extracts every page.

A page that fails to extract yields an empty page rather than an error for the document. One malformed page out of a thousand must not cost the other 999, and an empty page in the output is visible where a missing one would silently renumber everything after it.

Visible to a program, that is. It was not visible to a *reader*: the error was dropped here, so a document converted with a page missing looked exactly like one converted whole, and nothing in any output said which. ISO/TS 32002's cover page was absent from every conversion this package produced for the life of the project and the command exited 0. Failed now records what was lost, and the commands say so on stderr — which is the difference between a converter that is incomplete and one that is wrong about being complete.

func (*Extractor) Failed added in v1.0.0

func (e *Extractor) Failed() []PageError

Failed returns the pages the last Document call could not extract, in page order.

Reset by each Document rather than accumulated, so it describes one extraction and not the Extractor's history.

func (*Extractor) Page

func (e *Extractor) Page(n int) (doc.Page, error)

Page extracts one 1-based page.

type Options

type Options struct {
	// Tol is the threshold policy. The zero value is replaced by
	// geom.DefaultTolerance, because a zero SpaceFrac would infer a space between
	// every pair of glyphs.
	Tol geom.Tolerance

	// KeepArtifacts includes text inside Artifact marked-content regions — running
	// headers, folios, watermarks. Off by default: on a 1,023-page specification
	// that text is the same header a thousand times, and it interleaves with body
	// prose at every page boundary. The blocks are still produced and still
	// carry doc.RoleArtifact when this is on, so the judgement stays inspectable
	// rather than being made silently at read time.
	KeepArtifacts bool

	// KeepHidden includes text drawn in rendering mode 3 or 7. On by default,
	// because invisible text is the text layer under a scanned page and it is
	// exactly what an extractor wants; set false only when comparing against what
	// a viewer displays.
	KeepHidden bool
}

Options configures extraction.

type PageError added in v1.0.0

type PageError struct {
	Page int
	Err  error
}

PageError reports a page Document could not extract, so a caller can say so.

Its own type rather than a bare error because the page number is the actionable half: "page 3 of 14 is missing" tells a reader which part of the document to distrust, where a wrapped error tells them only that something went wrong.

func (PageError) Error added in v1.0.0

func (p PageError) Error() string

Error is the wrapped error unchanged, because Page already names the page in it and a second "page 3:" in front of "extract: page 3: …" is the shape a caller prints to a user. The Page field is for a caller that wants the number on its own.

func (PageError) Unwrap added in v1.0.0

func (p PageError) Unwrap() error

type RuledHeader added in v1.3.0

type RuledHeader struct {
	// Y is the rule's lower edge: the header row is above it, and the table's rows below.
	Y float64
	// Cols are the columns' extents, left to right, each a cell's border.
	Cols [][2]float64
}

RuledHeader is the rule under a table's header row where the producer draws it cell by cell, as Word does: each header cell's bottom border, abutting, with a small square where two cells meet (ADR 0055).

func RuledHeaders added in v1.3.0

func RuledHeaders(rules []doc.Rule) []RuledHeader

RuledHeaders returns the ruled headers among a page's rules, top down.

Only a rule drawn as two or more cells' borders with a joint between each, the evidence a producer gives of a table's columns where it draws no vertical rules. A rule drawn whole is prose's, a page's or a caption's, and two segments that touch with no joint are a rule drawn in two strokes; segments that overlap, as a shaded box's lines do, are not borders at all.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL