paths

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package paths provides tools for manipulating 2d paths consisting of line segments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IDsFromSVG added in v0.0.3

func IDsFromSVG(r io.Reader, ids []string) (map[string]*Paths, error)

Types

type Bounds

type Bounds struct {
	Min, Max Vec2
}

Bounds describes an axis-aligned bounding box.

type Font added in v0.0.3

type Font struct {
	LineAdvance float64
	Glyph       map[rune]*FontGlyph
}

Font describes a typeface made up of paths.

func NewFont added in v0.0.3

func NewFont(svg io.Reader, fc *FontConfig) (*Font, error)

NewFont creates a font from the given SVG file. It's not an official SVG font; it's an SVG file with glyphs named groups or paths (named using the ID).

func (*Font) ScaleFromRuneHeight added in v0.0.3

func (f *Font) ScaleFromRuneHeight(r rune, height float64) (float64, error)

type FontConfig added in v0.0.3

type FontConfig struct {
	Glyph       map[rune]FontGlyphConfig // how to extract glyphs from SVG
	AdvanceRune rune                     // if set, multiply the advance by the width of this rune
	Advance     float64                  // how much advance to use (which is added to the width of the glyph).
	SpaceRune   rune                     // if set, multiply the space advance by the width of this rune
	Space       float64                  // how much advance to use for a space
	LineRune    rune                     // if set, multiply the line advance by the height of this rune
	Line        float64                  // how much to advance the y coord to start a new line
}

FontConfig describes an SVG file that contains glyphs for a font.

type FontGlyph added in v0.0.3

type FontGlyph struct {
	Width, Height float64
	Advance       float64
	Paths         *Paths
}

FontGlyph describes paths of a single letter from a font.

func (*FontGlyph) TransformMatrixCopy added in v0.0.3

func (g *FontGlyph) TransformMatrixCopy(m *svgXform) []Path

type FontGlyphConfig added in v0.0.3

type FontGlyphConfig struct {
	Dot          Vec2    // where in the svg the letter is to be found.
	ID           string  // the ID of the group/path in the SVG file
	DeltaAdvance float64 // if set, how much to fiddle the advance for this glyph
}

FontGlyphConfig describes how to find a particular glyph in an SVG file.

type Path

type Path struct {
	V []Vec2
}

A Path is a contiguous series of line segments, from the first point in the V slice to the last.

type Paths

type Paths struct {
	Bounds Bounds
	P      []Path
}

Paths is a set of paths, along with a view bounds.

func FromSVG

func FromSVG(r io.Reader) (*Paths, error)

FromSVG parses an SVG file, extracting paths. This provides only limited SVG parsing support, and will fail or produce incorrect results if the SVG file uses features that it doesn't understand.

func GlyphsToPaths added in v0.0.3

func GlyphsToPaths(offset Vec2, pgs []PositionedGlyph) *Paths

func (*Paths) Clip

func (ps *Paths) Clip(b Bounds)

Clip removes all line segments outside the given bounds. If a path crosses the bounds, it's broken into multiple paths.

func (*Paths) DefaultConfig added in v0.0.5

func (ps *Paths) DefaultConfig() *SVGConfig

func (*Paths) Rotate added in v0.0.3

func (ps *Paths) Rotate(theta float64)

Rotate rotates all paths by the given angle (in radians) about the center of the bounds. The bounds are updated to an axis-aligned bounding box that contains the original (rotated) bounds.

func (*Paths) SVG

func (ps *Paths) SVG(w io.Writer) error

SVG writes an SVG file that contains black strokes along the paths.

func (*Paths) SVGWithConfig added in v0.0.5

func (ps *Paths) SVGWithConfig(w io.Writer, cfg *SVGConfig) error

func (*Paths) Simplify

func (ps *Paths) Simplify(tol float64)

Simplify removes points from paths, with the guarantee that all removed points are within the given tolerance (distance) from the new path.

func (*Paths) Sort

func (ps *Paths) Sort(cfg *SortConfig)

Sort reorders paths to reduce the amount of movement between the end of one path and the start of the next. This is intended to improve rendering time using a physical xy plotter. The reordering can be configured in a limited way.

func (*Paths) TightenBounds added in v0.0.3

func (ps *Paths) TightenBounds()

TightenBounds adjusts the bounds to exactly contain the paths. If there are no paths, the bounds are set to zero.

func (*Paths) Transform

func (ps *Paths) Transform(nb Bounds)

Transform resizes all paths so that the rectangle forming the current bounds is the size of the new bounds. The bounds are also updated to the new bounds.

func (*Paths) Translate added in v0.0.3

func (ps *Paths) Translate(dx Vec2)

Translate moves all the paths by the given amount.

type PositionedGlyph added in v0.0.3

type PositionedGlyph struct {
	Pos   Vec2
	Scale float64
	G     *FontGlyph
}

func LayoutText added in v0.0.3

func LayoutText(f *Font, s string, scale float64, w float64) ([]PositionedGlyph, error)

type SVGConfig added in v0.0.5

type SVGConfig struct {
	// width and height attributes of the top-level SVG.
	Width, Height string
	// the four dimensions of the view box.
	ViewBox [4]int
	// The stroke-width attribute of paths generated.
	StrokeWidth string
}

SVGConfig provides configuration for the svg generated.

type SortConfig

type SortConfig struct {
	Split   bool // ok to split continuous paths
	Reverse bool // ok to draw paths in the reverse direction
}

SortConfig provides options for path sorting.

type Vec2

type Vec2 [2]float64

Vec2 is a 2-dimensional vector.

Jump to

Keyboard shortcuts

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