Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for fs.FS (for embedded files, ideally) and module support #171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"image/jpeg"
"image/png"
"io"
"io/fs"
"math"
"strings"

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
63 changes: 53 additions & 10 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image/jpeg"
_ "image/jpeg"
"image/png"
"io/fs"
"io/ioutil"
"math"
"os"
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down