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:
- seehuhn.de/go/pdf/font/cff.New
- seehuhn.de/go/pdf/font/truetype.New
- seehuhn.de/go/pdf/font/opentype.New
- seehuhn.de/go/pdf/font/type1.New
- seehuhn.de/go/pdf/font/type3.New
Fonts included in the library ¶
- The 14 standard PDF fonts are available as seehuhn.de/go/pdf/font/type1.Standard.
- The fonts from the Go Font family are available as seehuhn.de/go/pdf/font/gofont.
Data Types for Representing Fonts ¶
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
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.
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 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:
- Type 1: see seehuhn.de/go/pdf/font/type1.EmbedInfo
- Multiple Master Type 1 (not supported by this library)
- CFF font data: see seehuhn.de/go/pdf/font/cff.EmbedInfoSimple
- TrueType: see seehuhn.de/go/pdf/font/truetype.EmbedInfoSimple
- OpenType with CFF glyph outlines: see seehuhn.de/go/pdf/font/opentype.EmbedInfoCFFSimple
- OpenType with "glyf" glyph outlines: see seehuhn.de/go/pdf/font/opentype.EmbedInfoGlyfSimple
- Type 3: see seehuhn.de/go/pdf/font/type3.EmbedInfo
There are four different types of embedded composite fonts:
- CFF font data: see seehuhn.de/go/pdf/font/cff.EmbedInfoComposite
- TrueType: see seehuhn.de/go/pdf/font/truetype.EmbedInfoComposite
- OpenType with CFF glyph outlines: see seehuhn.de/go/pdf/font/opentype.EmbedInfoCFFComposite
- OpenType with "glyf" glyph outlines: see seehuhn.de/go/pdf/font/opentype.EmbedInfoGlyfComposite
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
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
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
GlyphSeq represents a sequence of glyphs.
func (*GlyphSeq) Align ¶ added in v0.4.0
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) TotalWidth ¶ added in v0.4.0
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
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
Res can be embedded in a struct to implement the Resource interface.
func (Res) DefaultName ¶ added in v0.4.0
DefaultName implements the Resource interface.
type ResIndirect ¶ added in v0.4.0
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.
Source Files ¶
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. |