font

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package font provides font parsing, metrics, and font definition generation APIs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(fontFileStr, encodingFileStr, dstDirStr string, msgWriter io.Writer, embed bool) error

Make generates a font definition file in JSON format. A definition file of this type is required to use non-core fonts in the PDF documents that gopdfkit generates. See the fontmaker command in the gopdfkit package for a command-line interface to this function.

fontFileStr is the name of the TrueType file (extension .ttf), OpenType file (extension .otf) or binary Type1 file (extension .pfb) from which to generate a definition file. OpenType files with TrueType outlines and CFF/PostScript outlines are both supported; the outline type cannot be determined from the file extension alone. If a Type1 file is specified, a metric file with the same pathname except with the extension .afm must be present.

encodingFileStr is the name of the encoding file that corresponds to the font.

dstDirStr is the name of the directory in which to save the definition file and, if embed is true, the compressed font file.

msgWriter is the writer that receives messages throughout the process. Use nil to turn off messages.

embed is true if the font is to be embedded in the PDF files.

Types

type OpenType

type OpenType struct {
	Embeddable         bool              // Whether the font permits embedding.
	PostScriptOutlines bool              // Whether the font uses CFF/PostScript outlines.
	CFFData            []byte            // Raw CFF table data.
	UnitsPerEm         uint16            // Font units per em.
	PostScriptName     string            // PostScript font name.
	Bold               bool              // Whether the font is bold.
	ItalicAngle        int16             // Italic angle from the font metadata.
	IsFixedPitch       bool              // Whether the font is fixed pitch.
	TypoAscender       int16             // Typographic ascender.
	TypoDescender      int16             // Typographic descender.
	UnderlinePosition  int16             // Underline position.
	UnderlineThickness int16             // Underline thickness.
	Xmin               int16             // Minimum glyph bounding-box X coordinate.
	Ymin               int16             // Minimum glyph bounding-box Y coordinate.
	Xmax               int16             // Maximum glyph bounding-box X coordinate.
	Ymax               int16             // Maximum glyph bounding-box Y coordinate.
	CapHeight          int16             // Capital-letter height.
	Widths             []uint16          // Glyph widths.
	Chars              map[uint16]uint16 // Character-to-glyph map.
}

OpenType contains metrics and embedded font data from an OpenType font.

func ParseOpenType

func ParseOpenType(fileStr string) (openTypeRec OpenType, err error)

ParseOpenType extracts metrics from an OpenType font file. It supports both TrueType outlines and CFF/PostScript outlines.

type TrueType

type TrueType struct {
	Embeddable         bool              // Whether the font permits embedding.
	UnitsPerEm         uint16            // Font units per em.
	PostScriptName     string            // PostScript font name.
	Bold               bool              // Whether the font is bold.
	ItalicAngle        int16             // Italic angle from the font metadata.
	IsFixedPitch       bool              // Whether the font is fixed pitch.
	TypoAscender       int16             // Typographic ascender.
	TypoDescender      int16             // Typographic descender.
	UnderlinePosition  int16             // Underline position.
	UnderlineThickness int16             // Underline thickness.
	Xmin               int16             // Minimum glyph bounding-box X coordinate.
	Ymin               int16             // Minimum glyph bounding-box Y coordinate.
	Xmax               int16             // Maximum glyph bounding-box X coordinate.
	Ymax               int16             // Maximum glyph bounding-box Y coordinate.
	CapHeight          int16             // Capital-letter height.
	Widths             []uint16          // Glyph widths.
	Chars              map[uint16]uint16 // Character-to-glyph map.
}

TrueType contains metrics of a TrueType font.

func ParseTTF

func ParseTTF(fileStr string) (ttfRec TrueType, err error)

ParseTTF extracts various metrics from a TrueType font file.

Example
package main

import (
	"fmt"

	"github.com/cssbruno/gopdfkit/font"
	"github.com/cssbruno/gopdfkit/internal/testsupport/example"
)

func main() {
	ttf, err := font.ParseTTF(example.FontDir() + "/calligra.ttf")
	if err == nil {
		fmt.Printf("Postscript name:  %s\n", ttf.PostScriptName)
		fmt.Printf("unitsPerEm:       %8d\n", ttf.UnitsPerEm)
		fmt.Printf("Xmin:             %8d\n", ttf.Xmin)
		fmt.Printf("Ymin:             %8d\n", ttf.Ymin)
		fmt.Printf("Xmax:             %8d\n", ttf.Xmax)
		fmt.Printf("Ymax:             %8d\n", ttf.Ymax)
	} else {
		fmt.Printf("%s\n", err)
	}
}
Output:
Postscript name:  CalligrapherRegular
unitsPerEm:           1000
Xmin:                 -173
Ymin:                 -234
Xmax:                 1328
Ymax:                  899

Jump to

Keyboard shortcuts

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