Skip to content

Commit

Permalink
font/opentype: add Parse and similar functions
Browse files Browse the repository at this point in the history
They delegate to the corresponding sfnt functions. Users (such as
example_test.go) now only need to import one package: opentype.

Also expand opentype and sfnt package docs to emphasize their
differences.

Change-Id: If641f978baa147780e5521aa3e65ee7db3ac1bc3
Reviewed-on: https://go-review.googlesource.com/c/image/+/257539
Trust: Nigel Tao <nigeltao@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
  • Loading branch information
nigeltao committed Sep 27, 2020
1 parent a67d67e commit e162460
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
3 changes: 1 addition & 2 deletions font/opentype/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goitalic"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/math/fixed"
)

Expand All @@ -26,7 +25,7 @@ func ExampleNewFace() {
startingDotY = 28
)

f, err := sfnt.Parse(goitalic.TTF)
f, err := opentype.Parse(goitalic.TTF)
if err != nil {
log.Fatalf("Parse: %v", err)
}
Expand Down
69 changes: 61 additions & 8 deletions font/opentype/opentype.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,72 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package opentype implements the font.Face interface based on SFNT
// font file formats.
// 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.
package opentype // import "golang.org/x/image/font/opentype"

import (
"image"
"image/draw"
"io"

"golang.org/x/image/font"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/math/fixed"
"golang.org/x/image/vector"
)

// 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 ParseCollection(src []byte) (*Collection, error) {
return sfnt.ParseCollection(src)
}

// 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.
func ParseCollectionReaderAt(src io.ReaderAt) (*Collection, error) {
return sfnt.ParseCollectionReaderAt(src)
}

// Collection is a collection of one or more fonts.
//
// All of the Collection methods are safe to call concurrently.
type Collection = sfnt.Collection

// Parse parses an OpenType font, such as TTF or OTF data, from a []byte data
// source.
func Parse(src []byte) (*Font, error) {
return sfnt.Parse(src)
}

// ParseReaderAt parses an OpenType font, such as TTF or OTF data, from an
// io.ReaderAt data source.
func ParseReaderAt(src io.ReaderAt) (*Font, error) {
return sfnt.ParseReaderAt(src)
}

// 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.
type Font = sfnt.Font

// FaceOptions describes the possible options given to NewFace when
// creating a new font.Face from a sfnt.Font.
// creating a new font.Face from a Font.
type FaceOptions struct {
Size float64 // Size is the font size in points
DPI float64 // DPI is the dots per inch resolution
Expand All @@ -32,9 +82,11 @@ func defaultFaceOptions() *FaceOptions {
}
}

// Face implements the font.Face interface for sfnt.Font values.
// Face implements the font.Face interface for Font values.
//
// A Face is not safe to use concurrently.
type Face struct {
f *sfnt.Font
f *Font
hinting font.Hinting
scale fixed.Int26_6

Expand All @@ -46,9 +98,10 @@ type Face struct {
mask image.Alpha
}

// NewFace returns a new font.Face for the given sfnt.Font.
// if opts is nil, sensible defaults will be used.
func NewFace(f *sfnt.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.
func NewFace(f *Font, opts *FaceOptions) (font.Face, error) {
if opts == nil {
opts = defaultFaceOptions()
}
Expand Down
10 changes: 8 additions & 2 deletions font/sfnt/sfnt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

//go:generate go run gen.go

// Package sfnt implements a decoder for SFNT font file formats, including
// TrueType and OpenType.
// Package sfnt implements a decoder for TTF (TrueType Fonts) and OTF
// (OpenType Fonts). Such fonts are also known as SFNT fonts.
//
// This package provides a low-level API and does not depend on vector
// rasterization packages.
//
// The sibling golang.org/x/image/font/opentype package provides a high-level
// API, including glyph rasterization.
package sfnt // import "golang.org/x/image/font/sfnt"

// This implementation was written primarily to the
Expand Down

0 comments on commit e162460

Please sign in to comment.