-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (114 loc) · 2.83 KB
/
main.go
File metadata and controls
117 lines (114 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bytes"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
"image"
"io/ioutil"
)
func main() {
a := app.New()
w := a.NewWindow("SVG Editor")
var d []byte
l := &widget.Label{
Wrapping: fyne.TextTruncate,
}
v := &canvas.Raster{
Generator: func(width, height int) image.Image {
l.SetText("")
output := image.NewNRGBA(image.Rect(0, 0, width, height))
if len(d) == 0 {
l.SetText("No Data")
return output
}
icon, err := oksvg.ReadIconStream(bytes.NewReader(d))
if err != nil {
l.SetText(err.Error())
return output
}
inputW, inputH := icon.ViewBox.W, icon.ViewBox.H
iconAspect := inputW / inputH
viewAspect := float64(width) / float64(height)
outputW, outputH := width, height
if viewAspect > iconAspect {
outputW = int(float64(height) * iconAspect)
} else if viewAspect < iconAspect {
outputH = int(float64(width) / iconAspect)
}
scanner := rasterx.NewScannerGV(int(inputW), int(inputH), output, output.Bounds())
raster := rasterx.NewDasher(width, height, scanner)
icon.SetTarget(0, 0, float64(outputW), float64(outputH))
icon.Draw(raster, 1)
defer func() {
if r := recover(); r != nil {
l.SetText(fmt.Sprintf("Crash when rendering SVG: %v", r))
}
}()
return output
},
}
e := &widget.Entry{
MultiLine: true,
OnChanged: func(data string) {
d = []byte(data)
v.Refresh()
},
Wrapping: fyne.TextWrapBreak,
}
s := container.NewHSplit(container.NewVScroll(e), container.NewScroll(v))
t := widget.NewToolbar(
widget.NewToolbarAction(theme.ContentAddIcon(), func() {
e.SetText("")
w.SetTitle("SVG Editor")
}),
widget.NewToolbarAction(theme.FileIcon(), func() {
fd := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if reader == nil {
return
}
data, err := ioutil.ReadAll(reader)
if err != nil {
dialog.ShowError(err, w)
return
}
e.SetText(string(data))
w.SetTitle("SVG Editor - " + reader.URI().Name())
}, w)
fd.SetFilter(storage.NewExtensionFileFilter([]string{".svg"}))
fd.Show()
}),
widget.NewToolbarAction(theme.DocumentSaveIcon(), func() {
fd := dialog.NewFileSave(func(writer fyne.URIWriteCloser, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if writer == nil {
return
}
if _, err := writer.Write(d); err != nil {
dialog.ShowError(err, w)
return
}
}, w)
fd.Show()
}),
)
w.SetContent(container.NewBorder(t, l, nil, nil, s))
w.CenterOnScreen()
w.Resize(fyne.NewSize(800, 600))
w.ShowAndRun()
}