font

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: GPL-3.0 Imports: 8 Imported by: 1

Documentation

Overview

Package font implements the foundations of PDF font handling. Support for specific font types is provided by sub-packages.

Embedding fonts into PDF files

A font must be "embedded" into a PDF file before it can be used. This library allows to embed the following font types:

  • OpenType fonts (.otf files)
  • TrueType fonts (.ttf files)
  • Type 1 fonts (.pfa or .pfb files, optionally with the corresponding .afm files)
  • Type 3 fonts (glyphs drawn using PDF commands)

Normally, fonts are embedded using the convenience functions in the seehuhn.de/go/pdf/font/embed. If more control is needed, the following functions can be used for the different ways of embedding fonts into PDF files as simple fonts:

Fonts included in the library

Data Types for Representing Fonts

  • An Embedder represents a font before it is embedded into a PDF file.
  • A Layouter is a font which is embedded and ready to typeset text.
  • The type Embedded represents the minimum information about a font required so that a PDF file can be parsed (but no text can be typeset).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Descriptor added in v0.3.5

type Descriptor struct {
	FontName    string     // required, except (usually) for Type 3 fonts
	FontFamily  string     // optional
	FontStretch os2.Width  // optional
	FontWeight  os2.Weight // optional

	IsFixedPitch bool // flag
	IsSerif      bool // flag
	IsSymbolic   bool // flag
	IsScript     bool // flag
	IsItalic     bool // flag
	IsAllCap     bool // flag
	IsSmallCap   bool // flag
	ForceBold    bool // flag

	FontBBox     *pdf.Rectangle // required, except for Type 3 fonts
	ItalicAngle  float64        // required
	Ascent       float64        // required, except for Type 3 fonts
	Descent      float64        // required, except for Type 3 fonts
	Leading      float64        // optional (default: 0)
	CapHeight    float64        // required, except if no latin chars or for Type 3 fonts
	XHeight      float64        // optional (default: 0)
	StemV        float64        // required, except for Type 3 fonts (0 = unknown, set to -1 for Type 3 fonts)
	StemH        float64        // optional (default: 0)
	MaxWidth     float64        // optional (default: 0)
	AvgWidth     float64        // optional (default: 0)
	MissingWidth float64        // optional (default: 0)
}

Descriptor represents a PDF font descriptor.

See section 9.8.1 of PDF 32000-1:2008.

func DecodeDescriptor added in v0.3.5

func DecodeDescriptor(r pdf.Getter, obj pdf.Object) (*Descriptor, error)

DecodeDescriptor reads the font descriptor from a PDF file.

func (*Descriptor) AsDict added in v0.3.5

func (d *Descriptor) AsDict() pdf.Dict

AsDict converts the font descriptor to a PDF dictionary, ready for storing in a PDF file.

type Dicts added in v0.3.5

type Dicts struct {
	PostScriptName pdf.Name
	FontDict       pdf.Dict
	CIDFontDict    pdf.Dict
	FontDescriptor *Descriptor
	FontProgramRef pdf.Reference
	FontProgram    *pdf.Stream
	Type           EmbeddingType
}

Dicts collects all information about a font embedded in a PDF file.

func ExtractDicts added in v0.3.5

func ExtractDicts(r pdf.Getter, fontDictRef pdf.Object) (*Dicts, error)

ExtractDicts reads all information about a font from a PDF file.

type Embedded added in v0.2.0

type Embedded interface {
	Resource
	WritingMode() int // 0 = horizontal, 1 = vertical
	ForeachWidth(s pdf.String, yield func(width float64, is_space bool))
}

Embedded represents a font which is already embedded in a PDF file.

type Embedder added in v0.4.0

type Embedder interface {
	Embed(w pdf.Putter, opt *Options) (Layouter, error)
}

Embedder represents a font which can be embedded in a PDF file.

type EmbeddingType added in v0.3.5

type EmbeddingType int

EmbeddingType represents the different ways font data can be embedded in a PDF file.

The different ways of embedding fonts in a PDF file are represented by values of type EmbeddingType. There are seven different types of embedded simple fonts:

There are four different types of embedded composite fonts:

const (
	Unknown EmbeddingType = iota

	Type1              // Type 1 (simple)
	MMType1            // Multiple Master Type 1 (simple)
	CFFSimple          // CFF font data (simple)
	TrueTypeSimple     // TrueType (simple)
	OpenTypeCFFSimple  // OpenType with CFF glyph outlines (simple)
	OpenTypeGlyfSimple // OpenType with "glyf" glyph outlines (simple)
	Type3              // Type 3 (simple)

	CFFComposite          // CFF font data (composite)
	TrueTypeComposite     // TrueType (composite)
	OpenTypeCFFComposite  // OpenType with CFF glyph outlines (composite)
	OpenTypeGlyfComposite // OpenType with "glyf" glyph outlines (composite)
)

List of all embedding types supported by PDF.

func (EmbeddingType) IsComposite added in v0.3.5

func (t EmbeddingType) IsComposite() bool

IsComposite returns true if the embedded font is a composite PDF font. If the function returns false, the font is a simple PDF font.

Fonts can be embedded into a PDF file either as "simple fonts" or as "composite fonts". Simple fonts lead to smaller PDF files, but only allow to use up to 256 glyphs per embedded copy of the font. Composite fonts allow to use more than 256 glyphs per embedded copy of the font, but lead to larger PDF files.

func (EmbeddingType) MustBe added in v0.3.5

func (t EmbeddingType) MustBe(expected EmbeddingType) error

MustBe returns an error if the embedding type is not as expected.

func (EmbeddingType) String added in v0.3.5

func (t EmbeddingType) String() string

type Geometry added in v0.2.0

type Geometry struct {
	Ascent             float64
	Descent            float64 // negative
	BaseLineDistance   float64
	UnderlinePosition  float64
	UnderlineThickness float64

	GlyphExtents []pdf.Rectangle // indexed by GID
	Widths       []float64       // indexed by GID
}

Geometry collects the various dimensions connected to a font and to the individual glyphs. All fields are measured in PDF text space units, and need to be scaled by the font size.

func (*Geometry) BoundingBox added in v0.3.4

func (g *Geometry) BoundingBox(fontSize float64, gg *GlyphSeq) *pdf.Rectangle

BoundingBox returns the bounding box of a glyph sequence, assuming that it is typeset at point (0, 0) using the given font size.

func (*Geometry) GetGeometry added in v0.3.5

func (g *Geometry) GetGeometry() *Geometry

GetGeometry returns the geometry of a font.

type Glyph added in v0.4.0

type Glyph struct {
	GID glyph.ID

	// Advance is the advance width for the current glyph the client
	// wishes to achieve.  It is measured in PDF text space units,
	// and is already scaled by the font size.
	Advance float64

	// Rise is by how much the glyph should be lifted above the baseline.  The
	// rise is measured in PDF text space units, and is already scaled by the
	// font size.
	Rise float64

	Text []rune
}

Glyph represents a single glyph.

type GlyphSeq added in v0.4.0

type GlyphSeq struct {
	Skip float64
	Seq  []Glyph
}

GlyphSeq represents a sequence of glyphs.

func (*GlyphSeq) Align added in v0.4.0

func (s *GlyphSeq) Align(width float64, q float64)

Align places the glyphs in a space of the given width. q=0 means left alignment, q=1 means right alignment and q=0.5 means centering.

func (*GlyphSeq) Append added in v0.4.0

func (s *GlyphSeq) Append(other *GlyphSeq)

Append modifies s by appending the glyphs from other.

func (*GlyphSeq) TotalWidth added in v0.4.0

func (s *GlyphSeq) TotalWidth() float64

TotalWidth returns the total advance width of the glyph sequence.

type Layouter added in v0.3.5

type Layouter interface {
	Embedded

	// GetGeometry returns font metrics required for typesetting.
	GetGeometry() *Geometry

	// Layout turns a string into a sequence of glyphs.
	Layout(ptSize float64, s string) *GlyphSeq

	// CodeAndWidth converts a glyph ID (corresponding to the given text) into
	// a PDF character code The character code is appended to s. The function
	// returns the new string s, the width of the glyph in PDF text space units
	// (still to be multiplied by the font size), and a value indicating
	// whether PDF word spacing adjustment applies to this glyph.
	CodeAndWidth(s pdf.String, gid glyph.ID, rr []rune) (pdf.String, float64, bool)

	// Close writes the used subset of the font to the PDF file. After close
	// has been called, only previously used glyph/text combinations can be
	// used.
	//
	// If this function is not called by the user, the font will be
	// automatically closed when the PDF file is closed.
	Close() error
}

A Layouter is a font embedded in a PDF file which can typeset text.

type Options added in v0.3.6

type Options struct {
	Language language.Tag

	GsubFeatures map[string]bool
	GposFeatures map[string]bool

	// Composite specifies whether to embed the font as a composite font.
	Composite bool

	MakeGIDToCID func() cmap.GIDToCID                // only used for composite fonts
	MakeEncoder  func(cmap.GIDToCID) cmap.CIDEncoder // only used for composite fonts

	// The default resource name to use for the font within content streams.
	// Leave blank to have names allocated automatically.
	ResName pdf.Name
}

Options allows to customize fonts for embedding into PDF files. Not all fields apply to all font types.

func MergeOptions added in v0.3.6

func MergeOptions(opt, defaultValues *Options) *Options

MergeOptions takes an options struct and a default values struct and returns a new options struct with all fields set to the values from the options struct, except for the fields which are set to the zero value in the options struct. `opt` can be nil in which case the default values are returned. `defaultValues` must not be nil.

type Res added in v0.4.0

type Res struct {
	DefName pdf.Name
	Ref     pdf.Object
}

Res can be embedded in a struct to implement the Resource interface.

func (Res) DefaultName added in v0.4.0

func (r Res) DefaultName() pdf.Name

DefaultName implements the Resource interface.

func (Res) PDFObject added in v0.4.0

func (r Res) PDFObject() pdf.Object

PDFObject implements the Resource interface.

type ResIndirect added in v0.4.0

type ResIndirect struct {
	DefName pdf.Name
	Ref     pdf.Reference
}

ResIndirect can be embedded in a struct to implement the Resource interface.

func (ResIndirect) DefaultName added in v0.4.0

func (r ResIndirect) DefaultName() pdf.Name

DefaultName implements the Resource interface.

func (ResIndirect) PDFObject added in v0.4.0

func (r ResIndirect) PDFObject() pdf.Object

PDFObject implements the Resource interface.

type Resource added in v0.4.0

type Resource interface {
	DefaultName() pdf.Name // return "" to choose names automatically
	PDFObject() pdf.Object // value to use in the resource dictionary
}

Resource is a PDF resource.

Directories

Path Synopsis
Package cff implements CFF font data embedded into PDF files.
Package cff implements CFF font data embedded into PDF files.
Package cmap implements CMap files for embedding in PDF files.
Package cmap implements CMap files for embedding in PDF files.
Package embed provides convenience functions to embed fonts into a PDF file.
Package embed provides convenience functions to embed fonts into a PDF file.
Package opentype implements OpenType fonts embedded into PDF files.
Package opentype implements OpenType fonts embedded into PDF files.
Package truetype implements TrueType fonts embedded into PDF files.
Package truetype implements TrueType fonts embedded into PDF files.
Package type1 implements Type 1 fonts embedded into PDF files.
Package type1 implements Type 1 fonts embedded into PDF files.
Package type3 implements Type 3 fonts embedded into PDF files.
Package type3 implements Type 3 fonts embedded into PDF files.

Jump to

Keyboard shortcuts

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