From 2da78511518a9fd86e2ed762f01814b64d92fde8 Mon Sep 17 00:00:00 2001 From: Tom Pethtel Date: Sat, 18 Jan 2020 22:20:21 -0500 Subject: [PATCH] Add method to parse font from byte array --- context.go | 9 +++++++++ util.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/context.go b/context.go index 1ddb09f..8babfbb 100644 --- a/context.go +++ b/context.go @@ -703,6 +703,15 @@ func (dc *Context) LoadFontFace(path string, points float64) error { return err } +func (dc *Context) LoadFontFaceFromBytes(fontData []byte, points float64) error { + face, err := LoadFontFaceFromBytes(fontData, points) + if err == nil { + dc.fontFace = face + dc.fontHeight = points * 72 / 96 + } + return err +} + func (dc *Context) FontHeight() float64 { return dc.fontHeight } diff --git a/util.go b/util.go index 0806235..0d9ef4b 100644 --- a/util.go +++ b/util.go @@ -134,6 +134,14 @@ func LoadFontFace(path string, points float64) (font.Face, error) { if err != nil { return nil, err } + face, err := LoadFontFaceFromBytes(fontBytes, points) + if err != nil { + return nil, err + } + return face, nil +} + +func LoadFontFaceFromBytes(fontBytes []byte, points float64) (font.Face, error) { f, err := truetype.Parse(fontBytes) if err != nil { return nil, err