From c641687a28cba9702f66c4c112b5a750dfe805df Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Thu, 3 Jan 2019 14:34:13 +0000 Subject: [PATCH 1/2] Add missing package redirect --- widget/widget.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widget/widget.go b/widget/widget.go index 8cf230d068..35c7066f82 100644 --- a/widget/widget.go +++ b/widget/widget.go @@ -1,5 +1,5 @@ // Package widget defines the UI widgets within the Fyne toolkit -package widget +package widget // import "fyne.io/fyne/widget" import ( "fyne.io/fyne" From 5a7213ff03301b384e8b8a323bed2d1cb1ae8786 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 4 Jan 2019 22:16:15 +0000 Subject: [PATCH 2/2] Use assets directly from memory when we can Avoid caching things that don't have to be written. Depends upon a pending PR to oksvg https://github.com/srwiley/oksvg/pull/8 --- canvas/image.go | 5 ++++- driver/efl/canvas.go | 11 ++++++++-- driver/gl/gl.go | 21 ++++++++++++++----- vendor/github.com/srwiley/oksvg/svgd.go | 28 +++++++++++++++++-------- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/canvas/image.go b/canvas/image.go index b7def2b09c..fe5a9a92e8 100644 --- a/canvas/image.go +++ b/canvas/image.go @@ -31,6 +31,7 @@ type Image struct { // one of the following sources will provide our image data File string // Load the image from a file + Resource fyne.Resource // Load the image from an in-memory resource PixelColor func(x, y, w, h int) color.Color // Render the image from code PixelAspect float32 // Set an aspect ratio for pixel based images @@ -79,7 +80,9 @@ func NewImageFromFile(file string) *Image { // Images returned from this method will scale to fit the canvas object. // The method for scaling can be set using the Fill field. func NewImageFromResource(res fyne.Resource) *Image { - return NewImageFromFile(res.CachePath()) + return &Image{ + Resource: res, + } } // NewImageFromImage returns a new Image instance that is rendered from the Go diff --git a/driver/efl/canvas.go b/driver/efl/canvas.go index 0401567695..0d93488cb1 100644 --- a/driver/efl/canvas.go +++ b/driver/efl/canvas.go @@ -230,7 +230,14 @@ func (c *eflCanvas) loadImage(img *canvas.Image, obj *C.Evas_Object) { size := img.Size() C.evas_object_image_load_size_set(obj, C.int(scaleInt(c, size.Width)), C.int(scaleInt(c, size.Height))) - cstr := C.CString(img.File) + + var file string + if img.Resource != nil { + file = img.Resource.CachePath() + } else { + file = img.File + } + cstr := C.CString(file) C.evas_object_image_file_set(obj, cstr, nil) C.free(unsafe.Pointer(cstr)) } @@ -302,7 +309,7 @@ func (c *eflCanvas) refreshObject(o, o2 fyne.CanvasObject) { alpha := C.int(float64(255) * co.Alpha()) C.evas_object_color_set(obj, alpha, alpha, alpha, alpha) // premul ffffff*alpha - if co.File != "" { + if co.File != "" || co.Resource != nil { c.loadImage(co, obj) } if co.PixelColor != nil { diff --git a/driver/gl/gl.go b/driver/gl/gl.go index 3b617e589d..bc0d14150d 100644 --- a/driver/gl/gl.go +++ b/driver/gl/gl.go @@ -1,11 +1,13 @@ package gl import ( + "bytes" "fmt" "image" "image/draw" _ "image/jpeg" // avoid users having to import when using image widget _ "image/png" // avoid the same for PNG images + "io" "log" "os" "path/filepath" @@ -144,11 +146,21 @@ func (c *glCanvas) newGlImageTexture(obj fyne.CanvasObject) uint32 { return 0 } - if img.File != "" { - if strings.ToLower(filepath.Ext(img.File)) == ".svg" { - icon, err := oksvg.ReadIcon(img.File) + if img.File != "" || img.Resource != nil { + var file io.Reader + var name string + if img.Resource != nil { + name = img.Resource.Name() + file = bytes.NewReader(img.Resource.Content()) + } else { + name = img.File + file, _ = os.Open(img.File) + } + + if strings.ToLower(filepath.Ext(name)) == ".svg" { + icon, err := oksvg.ReadIconStream(file) if err != nil { - log.Println("SVG Load error:", err, img.File) + log.Println("SVG Load error:", err) return 0 } @@ -169,7 +181,6 @@ func (c *glCanvas) newGlImageTexture(obj fyne.CanvasObject) uint32 { icon.Draw(raster, img.Alpha()) } else { - file, _ := os.Open(img.File) pixels, _, err := image.Decode(file) if err != nil { diff --git a/vendor/github.com/srwiley/oksvg/svgd.go b/vendor/github.com/srwiley/oksvg/svgd.go index 5070927ef4..76c803753e 100644 --- a/vendor/github.com/srwiley/oksvg/svgd.go +++ b/vendor/github.com/srwiley/oksvg/svgd.go @@ -773,25 +773,19 @@ func (c *IconCursor) readStartElement(se xml.StartElement) (err error) { return } -// ReadIcon reads the Icon from the named file +// ReadIconStream reads the Icon from the given io.Reader // This only supports a sub-set of SVG, but // is enough to draw many icons. If errMode is provided, // the first value determines if the icon ignores, errors out, or logs a warning // if it does not handle an element found in the icon file. Ignore warnings is // the default if no ErrorMode value is provided. -func ReadIcon(iconFile string, errMode ...ErrorMode) (*SvgIcon, error) { - fin, errf := os.Open(iconFile) - if errf != nil { - return nil, errf - } - defer fin.Close() - +func ReadIconStream(stream io.Reader, errMode ...ErrorMode) (*SvgIcon, error) { icon := &SvgIcon{Ids: make(map[string]interface{}), Transform: rasterx.Identity} cursor := &IconCursor{StyleStack: []PathStyle{DefaultStyle}, icon: icon} if len(errMode) > 0 { cursor.ErrorMode = errMode[0] } - decoder := xml.NewDecoder(fin) + decoder := xml.NewDecoder(stream) decoder.CharsetReader = charset.NewReaderLabel for { t, err := decoder.Token() @@ -839,6 +833,22 @@ func ReadIcon(iconFile string, errMode ...ErrorMode) (*SvgIcon, error) { return icon, nil } +// ReadIcon reads the Icon from the named file +// This only supports a sub-set of SVG, but +// is enough to draw many icons. If errMode is provided, +// the first value determines if the icon ignores, errors out, or logs a warning +// if it does not handle an element found in the icon file. Ignore warnings is +// the default if no ErrorMode value is provided. +func ReadIcon(iconFile string, errMode ...ErrorMode) (*SvgIcon, error) { + fin, errf := os.Open(iconFile) + if errf != nil { + return nil, errf + } + defer fin.Close() + + return ReadIconStream(fin, errMode...) +} + func readFraction(v string) (f float64, err error) { v = strings.TrimSpace(v) d := 1.0