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:
parent
c71711e498
commit
091db17ac0
2 changed files with 40 additions and 13 deletions
19
canvas.go
19
canvas.go
|
@ -4,6 +4,7 @@ package canvas
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
)
|
)
|
||||||
|
@ -407,13 +408,16 @@ func parseStyle(value ...interface{}) drawStyle {
|
||||||
case *RadialGradient:
|
case *RadialGradient:
|
||||||
style.radialGradient = v
|
style.radialGradient = v
|
||||||
return style
|
return style
|
||||||
case *Image, string:
|
|
||||||
style.image = getImage(v)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, ok := parseColor(value...)
|
c, ok := parseColor(value...)
|
||||||
if ok {
|
if ok {
|
||||||
style.color = c
|
style.color = c
|
||||||
|
} else if len(value) == 1 {
|
||||||
|
switch v := value[0].(type) {
|
||||||
|
case *Image, string:
|
||||||
|
style.image = getImage(v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return style
|
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
|
// 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
|
// 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
|
// cached), or nil, in which case the first loaded font will be used
|
||||||
func (cv *Canvas) SetFont(font interface{}, size float64) {
|
func (cv *Canvas) SetFont(src interface{}, size float64) {
|
||||||
if font == nil {
|
if src == nil {
|
||||||
cv.state.font = defaultFont
|
cv.state.font = defaultFont
|
||||||
} else {
|
} else {
|
||||||
switch v := font.(type) {
|
switch v := src.(type) {
|
||||||
case *Font:
|
case *Font:
|
||||||
cv.state.font = v
|
cv.state.font = v
|
||||||
case *truetype.Font:
|
case *truetype.Font:
|
||||||
|
@ -639,7 +643,10 @@ func (cv *Canvas) SetFont(font interface{}, size float64) {
|
||||||
cv.state.font = f
|
cv.state.font = f
|
||||||
} else {
|
} else {
|
||||||
f, err := LoadFont(v)
|
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
|
fonts[v] = f
|
||||||
cv.state.font = f
|
cv.state.font = f
|
||||||
}
|
}
|
||||||
|
|
34
images.go
34
images.go
|
@ -3,9 +3,12 @@ package canvas
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,7 +20,7 @@ type Image struct {
|
||||||
opaque bool
|
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
|
// 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
|
// 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
|
return img, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImage(image interface{}) *Image {
|
func getImage(src interface{}) *Image {
|
||||||
switch v := image.(type) {
|
if img, ok := images[src]; ok {
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
switch v := src.(type) {
|
||||||
case *Image:
|
case *Image:
|
||||||
return v
|
return v
|
||||||
case string:
|
case image.Image:
|
||||||
if img, ok := images[v]; ok {
|
|
||||||
return img
|
|
||||||
}
|
|
||||||
img, err := LoadImage(v)
|
img, err := LoadImage(v)
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
images[v] = img
|
images[v] = img
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "Unknown image type: %T\n", src)
|
||||||
|
images[src] = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue