DrawImage can now take image.Image values; added error messages if the automatic image and font loading doesn't work

This commit is contained in:
Thomas Friedel 2018-10-06 18:20:20 +02:00
parent c71711e498
commit 091db17ac0
2 changed files with 40 additions and 13 deletions

View file

@ -4,6 +4,7 @@ package canvas
import (
"fmt"
"os"
"github.com/golang/freetype/truetype"
)
@ -407,13 +408,16 @@ func parseStyle(value ...interface{}) drawStyle {
case *RadialGradient:
style.radialGradient = v
return style
case *Image, string:
style.image = getImage(v)
}
}
c, ok := parseColor(value...)
if ok {
style.color = c
} else if len(value) == 1 {
switch v := value[0].(type) {
case *Image, string:
style.image = getImage(v)
}
}
return style
}
@ -625,11 +629,11 @@ func (cv *Canvas) SetLineWidth(width float64) {
// SetFont sets the font and font size. The font parameter can be a font loaded
// with the LoadFont function, a filename for a font to load (which will be
// cached), or nil, in which case the first loaded font will be used
func (cv *Canvas) SetFont(font interface{}, size float64) {
if font == nil {
func (cv *Canvas) SetFont(src interface{}, size float64) {
if src == nil {
cv.state.font = defaultFont
} else {
switch v := font.(type) {
switch v := src.(type) {
case *Font:
cv.state.font = v
case *truetype.Font:
@ -639,7 +643,10 @@ func (cv *Canvas) SetFont(font interface{}, size float64) {
cv.state.font = f
} else {
f, err := LoadFont(v)
if err == nil {
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading font %s: %v\n", v, err)
fonts[v] = nil
} else {
fonts[v] = f
cv.state.font = f
}

View file

@ -3,9 +3,12 @@ package canvas
import (
"bytes"
"errors"
"fmt"
"image"
"io/ioutil"
"os"
"runtime"
"strings"
"unsafe"
)
@ -17,7 +20,7 @@ type Image struct {
opaque bool
}
var images = make(map[string]*Image)
var images = make(map[interface{}]*Image)
// LoadImage loads an image. The src parameter can be either an image from the
// standard image package, a byte slice that will be loaded, or a file name
@ -74,21 +77,38 @@ func LoadImage(src interface{}) (*Image, error) {
return img, nil
}
func getImage(image interface{}) *Image {
switch v := image.(type) {
case *Image:
return v
case string:
if img, ok := images[v]; ok {
func getImage(src interface{}) *Image {
if img, ok := images[src]; ok {
return img
}
switch v := src.(type) {
case *Image:
return v
case image.Image:
img, err := LoadImage(v)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading image: %v\n", err)
images[src] = nil
return nil
}
images[v] = img
return img
case string:
img, err := LoadImage(v)
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "format") {
fmt.Fprintf(os.Stderr, "Error loading image %s: %v\nIt may be necessary to import the appropriate decoder, e.g.\nimport _ \"image/jpeg\"\n", v, err)
} else {
fmt.Fprintf(os.Stderr, "Error loading image %s: %v\n", v, err)
}
images[src] = nil
return nil
}
images[v] = img
return img
}
fmt.Fprintf(os.Stderr, "Unknown image type: %T\n", src)
images[src] = nil
return nil
}