~eliasnaur/gio-patches

font/opentype: add Collection type and methods v1 PROPOSED

Greg Pomerantz
Greg Pomerantz: 1
 font/opentype: add Collection type and methods

 1 files changed, 47 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/9345/mbox | git am -3
Learn more about email & git

[PATCH] font/opentype: add Collection type and methods Export this patch

Greg Pomerantz
The Collection type wraps sfnt.Collection and allows lazy-loading
of fonts from SFNT collections.
---
 font/opentype/opentype.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/font/opentype/opentype.go b/font/opentype/opentype.go
index f311044..9f8e4c5 100644
--- a/font/opentype/opentype.go
+++ b/font/opentype/opentype.go
@@ -5,6 +5,8 @@
package opentype

import (
	"io"

	"unicode"
	"unicode/utf8"

@@ -23,6 +25,11 @@ type Font struct {
	buf  sfnt.Buffer
}

// Collection is a collection of one or more fonts.
type Collection struct {
	coll *sfnt.Collection
}

type opentype struct {
	Font    *sfnt.Font
	Hinting font.Hinting
@@ -38,6 +45,46 @@ func Parse(src []byte) (*Font, error) {
	return &Font{font: fnt}, nil
}

// ParseCollection parses an SFNT 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) {
	c, err := sfnt.ParseCollection(src)
	if err != nil {
		return nil, err
	}
	return &Collection{c}, nil
}

// ParseCollectionReaderAt parses an SFNT 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) {
	c, err := sfnt.ParseCollectionReaderAt(src)
	if err != nil {
		return nil, err
	}
	return &Collection{c}, nil
}

// NumFonts returns the number of fonts in the collection.
func (c *Collection) NumFonts() int {
	return c.coll.NumFonts()
}

// Font returns the i'th font in the collection.
func (c *Collection) Font(i int) (*Font, error) {
	fnt, err := c.coll.Font(i)
	if err != nil {
		return nil, err
	}
	return &Font{font: fnt}, nil
}

func (f *Font) Layout(ppem fixed.Int26_6, str string, opts text.LayoutOptions) *text.Layout {
	return layoutText(&f.buf, ppem, str, &opentype{Font: f.font, Hinting: font.HintingFull}, opts)
}
-- 
2.16.2
LGTM, but I need your sign-off to please the lawyers. Sorry.
View this thread in the archives