psinterpreter

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: BSD-3-Clause, Unlicense Imports: 6 Imported by: ? modules, 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 psinterpreter implement a Postscript interpreter required to parse .CFF files, and Type1 and Type2 Charstrings. This package provides the low-level mechanisms needed to read such formats; the data is consumed in higher level packages, which implement `PsOperatorHandler`. It also provides helpers to interpret glyph outline descriptions, shared between Type1 and CFF font formats.

See https://adobe-type-tools.github.io/font-tech-notes/pdfs/5177.Type2.pdf

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInterrupt signals the interpreter to stop early, without erroring.
	ErrInterrupt = errors.New("interruption")
)

Functions

func GlobalSubr

func GlobalSubr(state *Machine) error

GlobalSubr pops the subroutine index and call it

func LocalSubr

func LocalSubr(state *Machine) error

LocalSubr pops the subroutine index and call it

Types

type ArgStack

type ArgStack struct {
	Vals [psArgStackSize]float64 // we have to use float64 to properly store floats and int32 values
	// Effecive size currently in use. The first value to
	// pop is at index Top-1
	Top int32
}

func (*ArgStack) Clear

func (a *ArgStack) Clear()

Clear clears the stack

func (*ArgStack) Pop

func (a *ArgStack) Pop() float64

Pop returns the top level value and decrease `Top` It will panic if the stack is empty.

func (*ArgStack) PopN

func (a *ArgStack) PopN(numPop int32) error

PopN check and remove the n top levels entries. Passing a negative `numPop` clears all the stack.

func (*ArgStack) Uint16

func (a *ArgStack) Uint16() uint16

Uint16 returns the top level value as uint16, without popping the stack.

type CharstringReader

type CharstringReader struct {
	// Acumulated segments for the glyph outlines
	Segments []ot.Segment
	// Acumulated bounds for the glyph outlines
	Bounds PathBounds

	CurrentPoint Point
	// contains filtered or unexported fields
}

CharstringReader provides implementation of the operators found in a font charstring.

func (*CharstringReader) ClosePath

func (out *CharstringReader) ClosePath()

ClosePath closes the current contour, adding a segment to the first point if needed.

func (*CharstringReader) Flex

func (out *CharstringReader) Flex(state *Machine) error

func (*CharstringReader) Flex1

func (out *CharstringReader) Flex1(state *Machine) error

func (*CharstringReader) Hflex

func (out *CharstringReader) Hflex(state *Machine) error

func (*CharstringReader) Hflex1

func (out *CharstringReader) Hflex1(state *Machine) error

func (*CharstringReader) Hhcurveto

func (out *CharstringReader) Hhcurveto(state *Machine)

func (*CharstringReader) Hintmask

func (out *CharstringReader) Hintmask(state *Machine)

func (*CharstringReader) Hlineto

func (out *CharstringReader) Hlineto(state *Machine)

func (*CharstringReader) Hmoveto

func (out *CharstringReader) Hmoveto(state *Machine) error

func (*CharstringReader) Hstem

func (out *CharstringReader) Hstem(state *Machine)

func (*CharstringReader) Hvcurveto

func (out *CharstringReader) Hvcurveto(state *Machine)

func (*CharstringReader) Rcurveline

func (out *CharstringReader) Rcurveline(state *Machine) error

func (*CharstringReader) RelativeCurveTo

func (out *CharstringReader) RelativeCurveTo(arg1, arg2, arg3 Point)

RelativeCurveTo draws a curve with controls points computed from the current point and `arg1`, `arg2`, `arg3`

func (*CharstringReader) Rlinecurve

func (out *CharstringReader) Rlinecurve(state *Machine) error

func (*CharstringReader) Rlineto

func (out *CharstringReader) Rlineto(state *Machine)

func (*CharstringReader) Rmoveto

func (out *CharstringReader) Rmoveto(state *Machine) error

func (*CharstringReader) Rrcurveto

func (out *CharstringReader) Rrcurveto(state *Machine)

func (*CharstringReader) Vhcurveto

func (out *CharstringReader) Vhcurveto(state *Machine)

func (*CharstringReader) Vlineto

func (out *CharstringReader) Vlineto(state *Machine)

func (*CharstringReader) Vmoveto

func (out *CharstringReader) Vmoveto(state *Machine) error

func (*CharstringReader) Vstem

func (out *CharstringReader) Vstem(state *Machine)

func (*CharstringReader) Vvcurveto

func (out *CharstringReader) Vvcurveto(state *Machine)

type Context

type Context uint32

Context is the flavour of the Postcript language.

const (
	TopDict         Context = iota // Top dict in CFF files
	PrivateDict                    // Private dict in CFF files
	Type2Charstring                // Charstring in CFF files
	Type1Charstring                // Charstring in Type1 font files
)

type Machine

type Machine struct {
	ArgStack ArgStack
	// contains filtered or unexported fields
}

Machine is a PostScript interpreter. A same interpreter may be re-used using muliples `Run` calls.

func (*Machine) CallSubroutine

func (p *Machine) CallSubroutine(index int32, isLocal bool) error

CallSubroutine calls the subroutine, identified by its index, as found in the instructions (that is, before applying the subroutine biased). `isLocal` controls whether the local or global subroutines are used. No argument stack modification is performed.

func (*Machine) Return

func (p *Machine) Return() error

Return returns from a subroutine call.

func (*Machine) Run

func (p *Machine) Run(instructions []byte, localSubrs, globalSubrs [][]byte, handler OperatorHandler) error

Run runs the instructions in the PostScript context asked by `handler`. `localSubrs` and `globalSubrs` contains the subroutines that may be called in the instructions.

func (*Machine) SkipBytes

func (p *Machine) SkipBytes(count int32)

SkipBytes skips the next `count` bytes from the instructions, and clears the argument stack. It does nothing if `count` exceed the length of the instructions.

type Operator

type Operator struct {
	Operator  byte
	IsEscaped bool
}

Operator is a postcript command, which may be escaped.

func (Operator) String

func (p Operator) String() string

type OperatorHandler

type OperatorHandler interface {
	// Context defines the precise behaviour of the interpreter,
	// which has small nuances depending on the context.
	Context() Context

	// Apply implements the operator defined by `operator` (which is the second byte if `escaped` is true).
	//
	// Returning `ErrInterrupt` stop the parsing of the instructions, without reporting an error.
	// It can be used as an optimization.
	Apply(state *Machine, operator Operator) error
}

OperatorHandler defines the behaviour of an operator.

type PathBounds

type PathBounds struct {
	Min, Max Point
}

PathBounds represents a control bounds for a glyph outline (in font units).

func (*PathBounds) Enlarge

func (b *PathBounds) Enlarge(pt Point)

Enlarge enlarges the bounds to include pt

func (*PathBounds) ToExtents

func (b *PathBounds) ToExtents() ot.GlyphExtents

ToExtents converts a path bounds to the corresponding glyph extents.

type Point

type Point struct{ X, Y float64 }

Point is a 2D Point in font units.

func (*Point) Move

func (p *Point) Move(dx, dy float64)

Move translates the Point.

Jump to

Keyboard shortcuts

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