psinterpreter

package
v0.0.0-...-b7ff752 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT, MIT Imports: 6 Imported by: 0

Documentation

Overview

Package psinterpreter implements 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.

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]int32
	// Effective 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) Float

func (a *ArgStack) Float() float32

Float return the top level value as a real number (which is stored as its binary representation), without popping the stack.

func (*ArgStack) Pop

func (a *ArgStack) Pop() int32

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 []fonts.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 Machine

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

Machine is a PostScript interpreter. A same interpreter may be re-used using multiples `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 PsOperatorHandler) 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 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() fonts.GlyphExtents

ToExtents converts a path bounds to the corresponding glyph extents.

type Point

type Point struct{ X, Y int32 }

Point is a 2D Point in font units.

func (*Point) Move

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

Move translates the Point.

type PsContext

type PsContext uint32

PsContext is the flavour of the PostScript language.

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

type PsOperator

type PsOperator struct {
	Operator  byte
	IsEscaped bool
}

PsOperator is a postscript command, which may be escaped.

func (PsOperator) String

func (p PsOperator) String() string

type PsOperatorHandler

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

	// 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(operator PsOperator, state *Machine) error
}

PsOperatorHandler defines the behavior of an operator.

Jump to

Keyboard shortcuts

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