opentype

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: BSD-3-Clause Imports: 7 Imported by: 545

Documentation

Overview

Package opentype implements a glyph rasterizer for TTF (TrueType Fonts) and OTF (OpenType Fonts).

This package provides a high-level API, centered on the NewFace function, implementing the golang.org/x/image/font.Face interface.

The sibling golang.org/x/image/font/sfnt package provides a low-level API.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFace

func NewFace(f *Font, opts *FaceOptions) (font.Face, error)

NewFace returns a new font.Face for the given Font.

If opts is nil, sensible defaults will be used.

Example
package main

import (
	"fmt"
	"image"
	"image/color"
	"log"
	"os"

	"golang.org/x/image/font"
	"golang.org/x/image/font/gofont/goitalic"
	"golang.org/x/image/font/opentype"
	"golang.org/x/image/math/fixed"
)

func main() {
	const (
		width        = 72
		height       = 36
		startingDotX = 6
		startingDotY = 28
	)

	f, err := opentype.Parse(goitalic.TTF)
	if err != nil {
		log.Fatalf("Parse: %v", err)
	}
	face, err := opentype.NewFace(f, &opentype.FaceOptions{
		Size:    32,
		DPI:     72,
		Hinting: font.HintingNone,
	})
	if err != nil {
		log.Fatalf("NewFace: %v", err)
	}

	dst := image.NewGray(image.Rect(0, 0, width, height))
	d := font.Drawer{
		Dst:  dst,
		Src:  image.White,
		Face: face,
		Dot:  fixed.P(startingDotX, startingDotY),
	}
	fmt.Printf("The dot is at %v\n", d.Dot)
	d.DrawString("jel")
	fmt.Printf("The dot is at %v\n", d.Dot)
	d.Src = image.NewUniform(color.Gray{0x7F})
	d.DrawString("ly")
	fmt.Printf("The dot is at %v\n", d.Dot)

	const asciiArt = ".++8"
	buf := make([]byte, 0, height*(width+1))
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			c := asciiArt[dst.GrayAt(x, y).Y>>6]
			if c != '.' {
				// No-op.
			} else if x == startingDotX-1 {
				c = ']'
			} else if y == startingDotY-1 {
				c = '_'
			}
			buf = append(buf, c)
		}
		buf = append(buf, '\n')
	}
	os.Stdout.Write(buf)

}
Output:

The dot is at {6:00 28:00}
The dot is at {41:32 28:00}
The dot is at {66:48 28:00}
.....]..................................................................
.....]..................................................................
.....]..................................................................
.....]..................................+++......+++....................
.....]........+++.......................888......+++....................
.....].......+88+......................+88+......+++....................
.....].......888+......................+88+.....+++.....................
.....].......888+......................+88+.....+++.....................
.....].................................888......+++.....................
.....].................................888......+++.....................
.....]....................++..........+88+......+++.....................
.....]......+88+.......+888888+.......+88+.....+++....+++..........++...
.....]......888......+888888888+......+88+.....+++....++++........+++...
.....]......888.....+888+...+888......888......+++.....+++........++....
.....].....+888....+888......+88+.....888......+++.....+++.......+++....
.....].....+88+....888.......+88+....+88+......+++.....+++......+++.....
.....].....+88+...+888.......+88+....+88+.....+++......+++......+++.....
.....].....888....888+++++++++88+....+88+.....+++......+++.....+++......
.....].....888....88888888888888+....888......+++......++++....++.......
.....]....+888...+88888888888888.....888......+++.......+++...+++.......
.....]....+88+...+888...............+888......+++.......+++..+++........
.....]....+88+...+888...............+88+.....+++........+++..+++........
.....]....888....+888...............+88+.....+++........+++.+++.........
.....]....888....+888...............888......+++........++++++..........
.....]...+888.....888+..............888......+++........++++++..........
.....]...+88+.....+8888+....++8.....888+.....++++........++++...........
.....]...+88+......+8888888888+.....+8888....+++++.......++++...........
_____]___888________+88888888++______+888_____++++_______+++____________
.....]...888...........+++.............++................+++............
.....]..+88+............................................+++.............
.....]..+88+...........................................+++..............
.....].+888............................................+++..............
....888888............................................+++...............
....88888............................................++++...............
....+++.................................................................
.....]..................................................................

Types

type Collection

type Collection = sfnt.Collection

Collection is a collection of one or more fonts.

All of the Collection methods are safe to call concurrently.

func ParseCollection

func ParseCollection(src []byte) (*Collection, error)

ParseCollection parses an OpenType font collection, such as TTC or OTC data, from a []byte data source.

If passed data for a single font, a TTF or OTF instead of a TTC or OTC, it will return a collection containing 1 font.

func ParseCollectionReaderAt

func ParseCollectionReaderAt(src io.ReaderAt) (*Collection, error)

ParseCollectionReaderAt parses an OpenType collection, such as TTC or OTC data, from an io.ReaderAt data source.

If passed data for a single font, a TTF or OTF instead of a TTC or OTC, it will return a collection containing 1 font.

type Face

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

Face implements the font.Face interface for Font values.

A Face is not safe to use concurrently.

func (*Face) Close

func (f *Face) Close() error

Close satisfies the font.Face interface.

func (*Face) Glyph

func (f *Face) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool)

Glyph satisfies the font.Face interface.

func (*Face) GlyphAdvance

func (f *Face) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool)

GlyphAdvance satisfies the font.Face interface.

func (*Face) GlyphBounds

func (f *Face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool)

GlyphBounds satisfies the font.Face interface.

func (*Face) Kern

func (f *Face) Kern(r0, r1 rune) fixed.Int26_6

Kern satisfies the font.Face interface.

func (*Face) Metrics

func (f *Face) Metrics() font.Metrics

Metrics satisfies the font.Face interface.

type FaceOptions

type FaceOptions struct {
	Size    float64      // Size is the font size in points
	DPI     float64      // DPI is the dots per inch resolution
	Hinting font.Hinting // Hinting selects how to quantize a vector font's glyph nodes
}

FaceOptions describes the possible options given to NewFace when creating a new font.Face from a Font.

type Font

type Font = sfnt.Font

Font is an OpenType font, also known as an SFNT font.

All of the Font methods are safe to call concurrently, as long as each call has a different *sfnt.Buffer (or nil).

The Font methods that don't take a *sfnt.Buffer argument are always safe to call concurrently.

func Parse

func Parse(src []byte) (*Font, error)

Parse parses an OpenType font, such as TTF or OTF data, from a []byte data source.

func ParseReaderAt

func ParseReaderAt(src io.ReaderAt) (*Font, error)

ParseReaderAt parses an OpenType font, such as TTF or OTF data, from an io.ReaderAt data source.

Jump to

Keyboard shortcuts

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