-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagetemplate.go
213 lines (192 loc) · 6.44 KB
/
imagetemplate.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Package imagetemplate generates images from JSON templates and application-specific variables.
package imagetemplate
import (
"bytes"
"encoding/json"
"fmt"
"image"
"io"
"io/ioutil"
"runtime/debug"
"github.com/LLKennedy/imagetemplate/v3/render"
"github.com/LLKennedy/imagetemplate/v3/scaffold"
"golang.org/x/tools/godoc/vfs"
)
// Loader creates image builders from several input options and writes the finished product to several output formats.
type Loader interface {
Load() LoadOptions
Write() WriteOptions
}
// LoadOptions chooses the input format for Loader.
type LoadOptions interface {
FromBuilder(builder scaffold.Builder) (Loader, render.NamedProperties, error)
FromBytes(bytes []byte) (Loader, render.NamedProperties, error)
FromFile(path string) (Loader, render.NamedProperties, error)
FromJSON(raw json.RawMessage) (Loader, render.NamedProperties, error)
FromReader(reader io.Reader) (Loader, render.NamedProperties, error)
}
// WriteOptions chooses the output format for Loader.
type WriteOptions interface {
ToBuilder(props render.NamedProperties) (scaffold.Builder, error)
ToBMP(props render.NamedProperties) ([]byte, error)
ToCanvas(props render.NamedProperties) (render.Canvas, error)
ToImage(props render.NamedProperties) (image.Image, error)
ToBMPReader(props render.NamedProperties) (io.Reader, error)
}
type loader struct {
builder scaffold.Builder
fs vfs.FileSystem
}
// New returns a new loader with the default file system.
func New() Loader {
return NewUsing(vfs.OS("."))
}
// NewUsing returns a new loader using a specified vfs.
func NewUsing(fs vfs.FileSystem) Loader {
if fs == nil {
fs = vfs.OS(".")
}
return loader{
fs: fs,
builder: scaffold.NewBuilder(fs),
}
}
// Load returns the load options for a loader.
func (l loader) Load() LoadOptions {
return l
}
// Write returns the write options for a loader.
func (l loader) Write() WriteOptions {
return l
}
// FromBuilder constructs a loader using a pre-existing builder.
func (l loader) FromBuilder(builder scaffold.Builder) (newLoader Loader, props render.NamedProperties, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in FromBuilder: %v\n%s", r, debug.Stack())
}
}()
l.builder = builder
props = l.builder.GetNamedPropertiesList()
return l, props, nil
}
// FromBytes constructs a loader from the bytes of a template file.
func (l loader) FromBytes(bytes []byte) (newLoader Loader, props render.NamedProperties, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in FromBytes: %v\n%s", r, debug.Stack())
}
}()
l.builder, err = l.builder.LoadComponentsData(bytes)
return l, l.builder.GetNamedPropertiesList(), err
}
// FromFile constructs a loader from the template file located at the provided path.
func (l loader) FromFile(path string) (newLoader Loader, props render.NamedProperties, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in FromFile: %v\n%s", r, debug.Stack())
}
}()
l.builder, err = l.builder.LoadComponentsFile(path)
return l, l.builder.GetNamedPropertiesList(), err
}
// FromJSON constructs a loader from the raw JSON template data provided.
func (l loader) FromJSON(raw json.RawMessage) (newLoader Loader, props render.NamedProperties, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in FromJSON: %v\n%s", r, debug.Stack())
}
}()
rawData, _ := raw.MarshalJSON() //This function literally cannot error, so ignore the error output
l.builder, err = l.builder.LoadComponentsData(rawData)
return l, l.builder.GetNamedPropertiesList(), err
}
// FromReader constructs a loader from the streamed bytes of a template file.
func (l loader) FromReader(reader io.Reader) (newLoader Loader, props render.NamedProperties, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in FromReader: %v\n%s", r, debug.Stack())
}
}()
var raw []byte
raw, err = ioutil.ReadAll(reader)
if err != nil {
return l, nil, err
}
l.builder, err = l.builder.LoadComponentsData(raw)
return l, l.builder.GetNamedPropertiesList(), err
}
// ToBuilder returns the finished render contained within its builder.
func (l loader) ToBuilder(props render.NamedProperties) (newBuilder scaffold.Builder, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in ToBuilder: %v\n%s", r, debug.Stack())
}
}()
l.builder, err = applyProps(l.builder, props)
return l.builder, err
}
// ToBMP returns the finished render as the bytes of a bitmap file.
func (l loader) ToBMP(props render.NamedProperties) (raw []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in ToBMP: %v\n%s", r, debug.Stack())
}
}()
l.builder, err = applyProps(l.builder, props)
if err != nil {
return nil, err
}
return l.builder.WriteToBMP()
}
// ToCanvas returns the finished render as a canvas object.
func (l loader) ToCanvas(props render.NamedProperties) (c render.Canvas, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in ToCanvas: %v\n%s", r, debug.Stack())
}
}()
l.builder, err = applyProps(l.builder, props)
if err != nil {
return nil, err
}
return l.builder.GetCanvas(), nil
}
// ToImage returns the finished render as an image.Image object.
func (l loader) ToImage(props render.NamedProperties) (img image.Image, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in ToImage: %v\n%s", r, debug.Stack())
}
}()
l.builder, err = applyProps(l.builder, props)
if err != nil {
return nil, err
}
return l.builder.GetCanvas().GetUnderlyingImage(), nil
}
// ToBMPReader returns the finished render as streamed bytes of a bitmap file.
func (l loader) ToBMPReader(props render.NamedProperties) (rdr io.Reader, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic in ToBMPReader: %v\n%s", r, debug.Stack())
}
}()
l.builder, err = applyProps(l.builder, props)
if err != nil {
return nil, err
}
rawData, err := l.builder.WriteToBMP()
if err != nil {
return nil, err
}
return bytes.NewReader(rawData), nil
}
func applyProps(builder scaffold.Builder, props render.NamedProperties) (newBuilder scaffold.Builder, err error) {
builder, err = builder.SetNamedProperties(props)
if err != nil {
return builder, err
}
builder, err = builder.ApplyComponents()
return builder, err
}