geom

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package geom holds PDF user-space geometry: rectangles, transformation matrices, and the tolerance policy shared by every consumer.

PDF user space has its origin at the lower-left corner with y increasing upward, which is the opposite of most raster conventions. Nothing in this package flips that; conversion is the rasterizer's job.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTolerance = Tolerance{
	SpaceFrac:     0.30,
	WideSpaceFrac: 2.50,
	LineFrac:      0.50,
	ParaFrac:      1.50,

	SizeFrac: 1.06,

	IndentFrac: 1.0,
	IndentMax:  6.0,
	Epsilon:    1e-6,
}

DefaultTolerance is the starting policy. The values are deliberately plain and are expected to move once the golden corpus can measure them; they are not tuned constants inherited from anywhere.

View Source
var Identity = Matrix{A: 1, D: 1}

Identity is the no-op transformation.

Functions

This section is empty.

Types

type Matrix

type Matrix struct {
	A, B, C, D, E, F float64
}

Matrix is a PDF transformation matrix. PDF writes it as the six operands [a b c d e f], standing for the affine matrix

| a b 0 |
| c d 0 |
| e f 1 |

with row vectors, so a point transforms as [x y 1] * M. That row-vector convention is why Mul composes left-to-right: A.Mul(B) applies A first.

func Scale

func Scale(sx, sy float64) Matrix

Scale returns a matrix scaling by (sx, sy).

func Translate

func Translate(tx, ty float64) Matrix

Translate returns a matrix shifting by (tx, ty).

func (Matrix) Apply

func (m Matrix) Apply(x, y float64) (float64, float64)

Apply transforms the point (x, y).

func (Matrix) ApplyVec

func (m Matrix) ApplyVec(x, y float64) (float64, float64)

ApplyVec transforms (x, y) as a direction, ignoring translation. Use this for displacements and advance widths, where the origin offset must not apply.

func (Matrix) Mul

func (m Matrix) Mul(n Matrix) Matrix

Mul returns m composed with n such that the result applies m first, then n. For a glyph this is the natural reading order: text matrix, then CTM.

func (Matrix) ScaleFactors

func (m Matrix) ScaleFactors() (sx, sy float64)

ScaleFactors returns the magnitude of the transformed unit x and y vectors. For text this yields the on-page size of a nominal 1-unit glyph, which is how an effective font size is recovered from a matrix that may also rotate.

type Rect

type Rect struct {
	X0, Y0, X1, Y1 float64
}

Rect is an axis-aligned rectangle in PDF user space, normalized so that X0 <= X1 and Y0 <= Y1. PDF permits either corner order in a /MediaBox array, so construct via NewRect rather than by literal.

func NewRect

func NewRect(x0, y0, x1, y1 float64) Rect

NewRect returns a Rect with corners normalized.

func (Rect) Area

func (r Rect) Area() float64

func (Rect) Height

func (r Rect) Height() float64

func (Rect) Intersect

func (r Rect) Intersect(s Rect) Rect

Intersect returns the overlap of r and s, or the zero Rect if they are disjoint.

func (Rect) IsZero

func (r Rect) IsZero() bool

IsZero reports whether the rectangle has no area.

func (Rect) Union

func (r Rect) Union(s Rect) Rect

Union returns the smallest rectangle containing both r and s. A zero-area operand is treated as absent, so Union over a growing set can start from the zero Rect.

func (Rect) Width

func (r Rect) Width() float64

type Tolerance

type Tolerance struct {
	// SpaceFrac is the fraction of a font's nominal space advance that a
	// horizontal gap must exceed before an inter-word space is inferred.
	// Below 1.0 because many producers emit slightly tightened spacing.
	SpaceFrac float64

	// WideSpaceFrac is the multiple of nominal space advance above which a gap
	// is treated as column or tab separation rather than a single space.
	WideSpaceFrac float64

	// LineFrac is the fraction of font size that a baseline must shift
	// vertically before a new line is started.
	LineFrac float64

	// ParaFrac is the multiple of line height above which a vertical gap is
	// treated as a paragraph break rather than a line break.
	ParaFrac float64

	// SizeFrac is the ratio between two consecutive lines' dominant type sizes
	// above which they are treated as separate blocks even when the vertical step
	// says otherwise. A heading set at ordinary leading is the case: the step alone
	// cannot see it, and without this the heading fuses into the paragraph below.
	//
	// It is a ratio of larger to smaller, so 1.0 would split on any difference at
	// all and is not a usable setting — OCR output reports the same line of type at
	// sizes differing by a few percent. Zero means the default.
	SizeFrac float64

	// IndentFrac is the number of space widths a line must be indented past its
	// block's own margin, while repeating the indent that block's first line was set
	// with, before it is read as starting a new paragraph.
	//
	// This is the paragraph break that has no vertical evidence: a document setting
	// no space between paragraphs steps down by exactly one line at a boundary, so
	// ParaFrac cannot see it and SizeFrac has nothing to compare. Expressed in space
	// widths rather than points so a footnote and a heading are judged alike. Zero
	// means off, which is a usable setting — the rule is the least certain of the
	// three and a caller who wants only vertical evidence can have it.
	IndentFrac float64

	// IndentMax is the number of space widths beyond which an indent is column or
	// cell placement rather than a paragraph's first line, and is ignored.
	IndentMax float64

	// Epsilon is the absolute tolerance for coordinate comparison in user-space
	// units, absorbing float noise from matrix composition.
	Epsilon float64
}

Tolerance is the single place where "close enough" is decided.

It exists because inferring a space from a coordinate gap is the one judgement that determines whether extracted text reads as prose or as one 4000-character word. Scattering epsilons through the extractor makes that judgement untunable and unmeasurable, so every threshold lives here and is benchmarked as a unit.

func (Tolerance) NearlyEqual

func (t Tolerance) NearlyEqual(a, b float64) bool

NearlyEqual reports whether a and b are within the absolute epsilon.

Jump to

Keyboard shortcuts

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