goafm

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: GPL-3.0 Imports: 6 Imported by: 1

README

goafm

A Go library for parsing Adobe Font Metrics (AFM) files. Supports the full specification.

Adobe Font Metrics (AFM) files provide information about the size, position, and spacing of characters in a font. They are used by software applications to correctly display text using a specific font. The goafm library provides a convenient way to parse AFM files and extract the information they contain.

Features

Parsing & extraction of the following information:

  • Font metrics
  • Character metrics
  • Kerning pairs
  • Track kerning
  • Composites Characters

Installation

Use go get to install the library:

go get github.com/Sett17/goafm

Usage

ParseFile takes in a filename and returns a FontMetric pointer and an error.

Parse takes in a byte slice and returns a FontMetric pointer and an error.

package main

import (
  "fmt"

  "github.com/Sett17/goafm"
)

func main() {
  font, err := goafm.ParseFile("font.afm")
  if err != nil {
    panic(err)
  }
  fmt.Printf("%#v", font)
}

Documentation

Overview

Package goafm provides a custom type Number which wraps float64 value.

Index

Constants

View Source
const (
	FontMetrics = iota
	CharMetrics
	KernData
	KernPairsHorizontal
	KernPairsVertical
	KernTracking
	Composites
)

Variables

View Source
var AGLFN = map[uint16]NamePair{}/* 586 elements not displayed */

AGLFN is the Adobe Glyph List for New Fonts.

Functions

func Fields

func Fields(s string) []string

Types

type AFMError

type AFMError struct {
	Line int
	Msg  string
}

AFMError represents an error in the AFM file.

func NewAFMError

func NewAFMError(line int, format string, a ...any) *AFMError

NewAFMError creates a new AFMError with the given line number and error message.

func (AFMError) Error

func (A AFMError) Error() string

Error implements the error interface for AFMError.

type Array

type Array struct {
	A []interface{}
}

Array is the implementation of an array in the AFM specification.

func (*Array) Append

func (a *Array) Append(v interface{})

Append adds a value to the Array.

func (*Array) String

func (a *Array) String() string

String returns a string representation of Array.

type Boolean

type Boolean bool

Boolean represents a boolean value in the AFM specification.

func NewBoolean

func NewBoolean(b string) Boolean

NewBoolean creates a new Boolean with the value represented by the string.

type CharMetric

type CharMetric struct {
	C   int       // C is the character code. REQUIRED.
	WX  Number    // WX is the character width. REQUIRED.
	W0X Number    // W0X is the character width in x for direction 0.
	W1X Number    // W1X is the character width in x for direction 1.
	WY  Number    // WY is the character width in y.
	W0Y Number    // W0Y is the character width in y for direction 0.
	W1Y Number    // W1Y is the character width in y for direction 1.
	W   [2]Number // W is the character width vector.
	W0  [2]Number // W0 is the character width vector for direction 0.
	W1  [2]Number // W1 is the character width vector for direction 1.
	VV  [2]Number // VV has the same meaning as VVector in the global font program information, but for a single character.
	N   string    // N is the character name.
	B   [4]Number // B is the bounding box.
	L   [2]string // L is the ligature sequence.
}

CharMetric defines the structure of a character metric in an AFM file. It holds information about a character's code, width, bounding box, name, and ligature sequence.

func (CharMetric) String

func (c CharMetric) String() string

type Composite

type Composite struct {
	// Name is the name of the composite
	Name string
	// Parts is a slice of CompositePart instances
	Parts []CompositePart
}

Composite represents a structure containing information about a composite

type CompositePart

type CompositePart struct {
	// Name is the name of the part
	Name string
	// Dx is the x-dimension of the part
	Dx Number
	// Dy is the y-dimension of the part
	Dy Number
}

CompositePart represents a single part in a composite

type FontMetric

type FontMetric struct {
	FontName             string    // (Required) Name of the font program as presented to the PostScript language `findfont` operator.
	FullName             string    // (Optional) The full text name of the font.
	FamilyName           string    // (Optional) The name of the typeface family to which the font belongs.
	Weight               string    // (Optional) Weight of the font.
	FontBBox             [4]Number // Four numbers giving the lower left corner and the upper right corner of the font bounding box, in the sequence llx lly urx ury.
	Version              string    // (Optional) Font program version identifier. Matches the string found in the FontInfo dictionary of the font program itself.
	Notice               string    // (Optional) Font name trademark or copyright notice.
	EncodingScheme       string    // (Optional) String indicating the default encoding vector for this font program.
	MappingScheme        int       // (Not present with base font programs.) Integer code describing the mapping scheme.
	EscapeChar           int       // (Required if MappingScheme 3, not present otherwise). The byte value of the escape character used for this escape-mapped font program.
	CharacterSet         string    // (Optional) String describing the character set (glyph complement) of this font program.
	Characters           int       // (Optional) The number of characters defined in this font program.
	IsBaseFont           Boolean   // (Optional) boolean is true if this font program is a base font and false otherwise.
	VVector              [2]Number // (Required when MetricsSets 2) Components of a vector from origin 0 (the origin for writing direction 0) to origin 1 (the origin for writing direction 1).
	IsFixedV             Boolean   // (Optional) If boolean is true, this indicates that VVector is the same for every character in this font.
	IsCIDFont            Boolean   // (Required if AFM is for a CID-keyed font) If the boolean is true, the font is a CID-keyed font, and the metrics are in CID number order
	CapHeight            Number    // (Optional) Usually the y-value of the top of the capital H. If this font program contains no capital H, this keyword might be missing or numbermight be 0.
	XHeight              Number    // (Optional) Typically the y-value of the top of the lowercase x. If this font program contains no lowercase x, this keyword might be missing or numbermight be 0.
	Ascender             Number    // (Optional) For roman font programs: usually the y-value of the top of the lowercase d. If this font program contains no lowercase d, this keyword mightbe missing or number might be 0.
	Descender            Number    // (Optional) For roman font programs: typically the y-value of the bottom of the lowercase p. If this font program contains no lowercase p, this keywordmight be missing or number might be 0.
	StdHW                Number    // (Optional) This number specifies the dominant width of horizontal stems
	StdVW                Number    // (Optional) This number specifies the dominant width of vertical stems
	BlendAxisTypes       Array     // (Required) The value of BlendAxisTypes is an array of strings that specify the name of each axis in the order in which the master designs are organizedin the multiple master font program.
	BlendDesignPositions Array     // (Required) The value of BlendDesignPositions is an array of k arrays that give the locations of the k master designs in the design space. E
	BlendDesignMap       Array     // (Required) The value of BlendDesignMap is an array of n arrays where n is the number of design axes contained in the multiple master font program.
	WeightVector         Array     // (Required) The WeightVector array specifies the factors for deriving a weighted average of the master designs in a multiple master font program.
	UnderlinePosition    Number    // (Optional) Distance from the baseline for centering underlining strokes.
	UnderlineThickness   Number    // (Optional) This is the stroke width for underlining, and is generally proportional to the stroke widths of characters in the font program.
	ItalicAngle          Number    // (Optional) Angle (in degrees counter-clockwise from the vertical) of the dominant vertical strokes of the font.
	CharWidth            [2]Number // (Optional) The x and y components of the width vector of this font program’s characters.
	IsFixedPitch         Boolean   // (Optional) If boolean is true, this indicates that the font program is a fixed pitch (monospaced) font.

	CharMetricsByCode map[int]*CharMetric
	CharMetricsByName map[string]*CharMetric

	KernPair         map[string]map[string]*KernPair
	KernPairVertical map[string]map[string]*KernPair

	KernTracks map[int]*KernTrack

	Composites map[string]*Composite
}

FontMetric from an AFM file provides both global metrics for a font program and the metrics of each individual character.

func Parse

func Parse(b []byte) (*FontMetric, error)

Parse takes in a byte slice and returns a FontMetric pointer and an error.

func ParseFile

func ParseFile(filename string) (*FontMetric, error)

ParseFile takes in a filename and returns a FontMetric pointer and an error.

func (*FontMetric) MetricByCode

func (fm *FontMetric) MetricByCode(code int) *CharMetric

MetricByCode returns the CharMetric of the font metric by code.

func (*FontMetric) MetricByName

func (fm *FontMetric) MetricByName(name string) *CharMetric

MetricByName returns the CharMetric of the font metric by name.

func (*FontMetric) MetricByRune

func (fm *FontMetric) MetricByRune(r rune) *CharMetric

MetricByRune returns the CharMetric of the font metric by the rune representation.

type KernPair

type KernPair struct {
	// First is the first character of the kern pair
	First string
	// Second is the second character of the kern pair
	Second string
	// DX is the horizontal adjustment for the kern pair
	DX Number
	// DY is the vertical adjustment for the kern pair
	DY Number
}

KernPair defines the KernPair data structure

type KernTrack

type KernTrack struct {
	// Degree holds the degree of the kerning.
	Degree int
	// MinPt holds the minimum point size for the kerning.
	MinPt Number
	// MinKern holds the minimum kerning for the minimum point size.
	MinKern Number
	// MaxPt holds the maximum point size for the kerning.
	MaxPt Number
	// MaxKern holds the maximum kerning for the maximum point size.
	MaxKern Number
}

KernTrack holds information about kerning for a font with a specific degree.

func (*KernTrack) Fn

func (kt *KernTrack) Fn(pt Number) Number

Fn returns the kerning for a given point size.

type NamePair

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

NamePair is a pair of glyph name and character name.

type Number

type Number float64

Number type wraps float64 value.

func NewNumber

func NewNumber(num string) Number

NewNumber creates a new Number type from a string representation of a float64.

func (*Number) GetFloat

func (n *Number) GetFloat() float64

GetFloat returns the float64 value of a Number type.

func (*Number) GetInt

func (n *Number) GetInt() int

GetInt returns the int value of a Number type.

func (*Number) IsInt

func (n *Number) IsInt() bool

IsInt returns a boolean indicating whether the Number type holds an int value.

func (*Number) SetFloat

func (n *Number) SetFloat(f float64)

SetFloat sets the float64 value of a Number type.

func (*Number) SetInt

func (n *Number) SetInt(i int)

SetInt sets the int value of a Number type.

Jump to

Keyboard shortcuts

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