forked from ajstarks/svgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltr.go
178 lines (157 loc) · 3.86 KB
/
ltr.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
// ltr: Layer Tennis remixes
package main
import (
"flag"
"fmt"
"os"
"github.com/ajstarks/svgo"
)
var (
canvas = svg.New(os.Stdout)
poster, opacity, row, col, offset bool
title string
width, height int
)
const (
stdwidth = 900
stdheight = 280
ni = 11
)
// imagefiles returns a list of files in the specifed directory
// or nil on error. Each file includes the prepended directory name
func imagefiles(directory string) []string {
f, ferr := os.Open(directory)
if ferr != nil {
return nil
}
defer f.Close()
files, derr := f.Readdir(-1)
if derr != nil || len(files) == 0 {
return nil
}
names := make([]string, len(files))
for i, v := range files {
names[i] = directory + "/" + v.Name()
}
return names
}
// ltposter creates poster style: a title, followed by a list
// of volleys
func ltposter(x, y, w, h int, f []string) {
canvas.Image(x, y, w*2, h*2, f[0]) // first file, assumed to be the banner
y = y + (h * 2)
for i := 1; i < len(f); i += 2 {
canvas.Image(x, y, w, h, f[i])
canvas.Image(x+w, y, w, h, f[i+1])
if i%2 == 1 {
y += h
}
}
}
// ltcol creates a single column of volley images
func ltcol(x, y, w, h int, f []string) {
for i := 0; i < len(f); i++ {
canvas.Image(x, y, w, h, f[i])
y += h
}
}
// ltop creates a view with each volley stacked together with
// semi-transparent opacity
func ltop(x, y, w, h int, f []string) {
for i := 1; i < len(f); i++ { // skip the first file, assumed to be the banner
canvas.Image(x, y, w, h, f[i], "opacity:0.2")
}
}
// ltrow creates a row-wise view of volley images.
func ltrow(x, y, w, h int, f []string) {
for i := 0; i < len(f); i++ {
canvas.Image(x, y, w, h, f[i])
x += w
}
}
// ltoffset creates a view where each volley is offset from its opposing volley.
func ltoffset(x, y, w, h int, f []string) {
for i := 1; i < len(f); i++ { // skip the first file, assumed to be the banner
if i%2 == 0 {
x += w
} else {
x = 0
}
canvas.Image(x, y, w, h, f[i])
y += h
}
}
// dotitle creates the title
func dotitle(s string) {
if len(title) > 0 {
canvas.Title(title)
} else {
canvas.Title(s)
}
}
// init sets up the command line flags.
func init() {
flag.BoolVar(&poster, "poster", false, "poster style")
flag.BoolVar(&opacity, "opacity", false, "opacity style")
flag.BoolVar(&row, "row", false, "display is a single row")
flag.BoolVar(&col, "col", false, "display in a single column")
flag.BoolVar(&offset, "offset", false, "display in a row, even layers offset")
flag.IntVar(&width, "width", stdwidth, "image width")
flag.IntVar(&height, "height", stdheight, "image height")
flag.StringVar(&title, "title", "", "title")
flag.Parse()
}
func main() {
x := 0
y := 0
nd := len(flag.Args())
for i, dir := range flag.Args() {
filelist := imagefiles(dir)
if len(filelist) != ni || filelist == nil {
fmt.Fprintf(os.Stderr, "in the %s directory, need %d images, read %d\n", dir, ni, len(filelist))
continue
}
switch {
case opacity:
if i == 0 {
canvas.Start(width*nd, height*nd)
dotitle(dir)
}
ltop(x, y, width, height, filelist)
y += height
case poster:
if i == 0 {
canvas.Start(width, ((height*(ni-1)/4)+height)*nd)
dotitle(dir)
}
ltposter(x, y, width/2, height/2, filelist)
y += (height * 3) + (height / 2)
case col:
if i == 0 {
canvas.Start(width*nd, height*ni)
dotitle(dir)
}
ltcol(x, y, width, height, filelist)
x += width
case row:
if i == 0 {
canvas.Start(width*ni, height*nd)
dotitle(dir)
}
ltrow(x, y, width, height, filelist)
y += height
case offset:
n := ni - 1
pw := width * 2
ph := nd * (height * (n))
if i == 0 {
canvas.Start(pw, ph)
canvas.Rect(0, 0, pw, ph, "fill:white")
dotitle(dir)
}
ltoffset(x, y, width, height, filelist)
y += n * height
}
}
canvas.End()
}