From f2e05dd6e30d8113b0b3fdbd8ce49519b103dc9c Mon Sep 17 00:00:00 2001 From: Jeffrey Buchbinder Date: Sun, 22 May 2022 14:24:21 -0400 Subject: [PATCH 1/2] Support for fs.FS (for embedded files, ideally) and module support --- context.go | 12 ++++++++++- go.mod | 8 +++++++ util.go | 63 +++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 go.mod diff --git a/context.go b/context.go index 1ddb09f..8164a51 100644 --- a/context.go +++ b/context.go @@ -8,6 +8,7 @@ import ( "image/jpeg" "image/png" "io" + "io/fs" "math" "strings" @@ -695,7 +696,16 @@ func (dc *Context) SetFontFace(fontFace font.Face) { } func (dc *Context) LoadFontFace(path string, points float64) error { - face, err := LoadFontFace(path, points) + face, err := LoadFontFace(nil, path, points) + if err == nil { + dc.fontFace = face + dc.fontHeight = points * 72 / 96 + } + return err +} + +func (dc *Context) LoadFontFaceFS(fS fs.FS, path string, points float64) error { + face, err := LoadFontFace(fS, path, points) if err == nil { dc.fontFace = face dc.fontHeight = points * 72 / 96 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..32aa78e --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/fogleman/gg + +go 1.18 + +require ( + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 +) diff --git a/util.go b/util.go index 4497b7c..7e42c36 100644 --- a/util.go +++ b/util.go @@ -7,6 +7,7 @@ import ( "image/jpeg" _ "image/jpeg" "image/png" + "io/fs" "io/ioutil" "math" "os" @@ -26,17 +27,39 @@ func Degrees(radians float64) float64 { return radians * 180 / math.Pi } -func LoadImage(path string) (image.Image, error) { - file, err := os.Open(path) - if err != nil { - return nil, err +func LoadImage(path string) (image.Image, error) { return LoadImageFS(nil, path) } +func LoadImageFS(fS fs.FS, path string) (image.Image, error) { + var err error + var im image.Image + if fS != nil { + var file fs.File + file, err = fS.Open(path) + if err != nil { + return nil, err + } + im, _, err = image.Decode(file) + } else { + var file *os.File + file, err = os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + im, _, err = image.Decode(file) } - defer file.Close() - im, _, err := image.Decode(file) return im, err } -func LoadPNG(path string) (image.Image, error) { +func LoadPNG(path string) (image.Image, error) { return LoadPNGFS(nil, path) } +func LoadPNGFS(fS fs.FS, path string) (image.Image, error) { + if fS != nil { + file, err := fS.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + return png.Decode(file) + } file, err := os.Open(path) if err != nil { return nil, err @@ -54,7 +77,16 @@ func SavePNG(path string, im image.Image) error { return png.Encode(file, im) } -func LoadJPG(path string) (image.Image, error) { +func LoadJPG(path string) (image.Image, error) { return LoadJPGFS(nil, path) } +func LoadJPGFS(fS fs.FS, path string) (image.Image, error) { + if fS != nil { + file, err := fS.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + return jpeg.Decode(file) + } file, err := os.Open(path) if err != nil { return nil, err @@ -129,8 +161,19 @@ func unfix(x fixed.Int26_6) float64 { // are not thread safe and cannot be used in parallel across goroutines. // You can usually just use the Context.LoadFontFace function instead of // this package-level function. -func LoadFontFace(path string, points float64) (font.Face, error) { - fontBytes, err := ioutil.ReadFile(path) +func LoadFontFace(fS fs.FS, path string, points float64) (font.Face, error) { + var fontBytes []byte + var err error + if fS != nil { + fontBytes, err = ioutil.ReadFile(path) + } else { + fp, err := fS.Open(path) + if err != nil { + return nil, err + } + defer fp.Close() + fontBytes, err = ioutil.ReadAll(fp) + } if err != nil { return nil, err } From 1433033f266a808933e14f6d31125362a36cd22b Mon Sep 17 00:00:00 2001 From: Jeffrey Buchbinder Date: Sun, 22 May 2022 14:38:07 -0400 Subject: [PATCH 2/2] Typo --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index 7e42c36..2654958 100644 --- a/util.go +++ b/util.go @@ -164,7 +164,7 @@ func unfix(x fixed.Int26_6) float64 { func LoadFontFace(fS fs.FS, path string, points float64) (font.Face, error) { var fontBytes []byte var err error - if fS != nil { + if fS == nil { fontBytes, err = ioutil.ReadFile(path) } else { fp, err := fS.Open(path)