forked from fabiensanglard/cpsb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
276 lines (224 loc) · 5.45 KB
/
build.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import "bytes"
import "fmt"
import "os"
import "os/exec"
import "io/ioutil"
import "io"
import "strings"
import "golang.org/x/image/draw"
import "image/jpeg"
import "image/png"
import "image"
import "path"
var inkscapeBin = "inkscape"
var force = false
func isOlder(src string, than string) bool {
if force {
return false
}
stat, err := os.Stat(src)
// If the file does not exist, it is not older.
if os.IsNotExist(err) {
return false
}
statThan, _ := os.Stat(than)
return stat.ModTime().After(statThan.ModTime())
}
func copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func rescale(ssrc string, sdst string) {
input, _ := os.Open(ssrc)
defer input.Close()
output, err := os.Create(sdst)
defer output.Close()
if err != nil {
panic(fmt.Sprintf("Error creating %s", sdst))
}
// Decode the image (from PNG to image.Image):
var src image.Image
if strings.Contains(ssrc, ".png") {
src, err = png.Decode(input)
} else {
src, err = jpeg.Decode(input)
}
if err != nil {
panic(fmt.Sprintf("Error loading %s", ssrc))
}
// Set the expected size that you want:
dst := image.NewRGBA(image.Rect(0, 0, 100, 100.0*src.Bounds().Max.Y/src.Bounds().Max.X))
// Resize:
draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
// Encode to `output`:
png.Encode(output, dst)
}
func prepareImg(src string, dst string) {
// Compare last modified and skip if necessary
if isOlder(dst, src) {
return
}
if mode == "release" {
// Copy
fmt.Println("Copy:", src, "->", dst)
copy(src, dst)
} else {
fmt.Println("Scale:", src, "->", dst)
rescale(src, dst)
}
}
func prepareDrawing(src string, dst string) {
dst = strings.ReplaceAll(dst, ".svg", ".png")
// Compare last modified and skip if necessary
if isOlder(dst, src) {
return
}
bin := inkscapeBin
arg0 := "--export-filename=" + dst
var arg1 = "--export-dpi=300"
if mode == "debug" {
arg1 = "--export-dpi=100"
}
arg2 := src
fmt.Println(inkscapeBin, arg0, arg1, arg2)
out, err := exec.Command(bin, arg0, arg1, arg2).CombinedOutput()
if err != nil {
fmt.Println("%s %s", string(out), err)
return
}
}
func prepare(folder string, f func(string, string)) {
os.MkdirAll(cwd()+out+"/"+folder, os.ModePerm)
items, _ := ioutil.ReadDir(folder)
for _, item := range items {
if item.IsDir() {
os.MkdirAll(cwd()+out+"/"+folder+item.Name()+"/", os.ModePerm)
prepare(folder+item.Name()+"/", f)
}
var src = cwd() + folder + item.Name()
var dst = cwd() + out + "/" + folder + item.Name()
f(src, dst)
}
}
func toString(fs []string) string {
var b bytes.Buffer
for _, s := range fs {
fmt.Fprintf(&b, "%s ", s)
}
return b.String()
}
func makeCover(src string, dst string) {
src = cwd() + src
dst = cwd() + dst
if isOlder(dst, src) {
return
}
bin := inkscapeBin
args := make([]string, 0)
args = append(args, "--export-filename="+dst)
if mode == "debug" {
args = append(args, "--export-dpi=100")
} else {
args = append(args, "--export-dpi=300")
}
args = append(args, "--export-type=pdf")
args = append(args, "--export-text-to-path")
// arg3 := "--verb"
// arg4 := "ObjectToPath"
// arg3 := ""
args = append(args, src)
fmt.Printf("%s %s\n", inkscapeBin, toString(args))
out, err := exec.Command(bin, args...).CombinedOutput()
if err != nil {
fmt.Println("%s %s", string(out), err)
return
}
}
var mode = "release"
var out = ""
func cwd() string {
cwd, err := os.Getwd()
if err != nil {
panic(err.Error())
}
cwd += "/"
return cwd
}
func currentDir() string {
cwd, err := os.Getwd()
if err != nil {
panic(err.Error())
}
return path.Base(cwd)
}
func checkExecutable(bin string) {
path, err := exec.LookPath(bin)
if err != nil {
fmt.Println(fmt.Sprintf("Could not find executable '%s'", bin))
os.Exit(1)
}
fmt.Println(fmt.Sprintf("Found '%s' -> '%s'", bin, path))
}
func getMode() string {
var args = os.Args
if len(args) > 1 {
return args[1]
}
return ""
}
func main() {
fmt.Println("Building...")
checkExecutable(inkscapeBin)
mode = getMode()
var args = os.Args
if len(args) > 2 {
force = true
}
if mode != "debug" && mode != "release" && mode != "print" {
fmt.Println("Mode must be either 'debug' or 'release' or 'print'.")
return
}
compileOptions := ""
if mode == "print" {
compileOptions = `\def\forprint{}`
mode = "release"
}
outputDirName := "out"
out = outputDirName + "/" + mode
os.MkdirAll(out, os.ModePerm)
makeCover("src/cover/pdf/cover_front.svg", out+"/illu/cover_front.pdf")
makeCover("src/cover/pdf/cover_back.svg", out+"/illu/cover_back.pdf")
prepare("illu/img/", prepareImg)
prepare("illu/d/", prepareDrawing)
bin := "pdflatex"
arg0 := "-output-directory"
arg1 := outputDirName
arg2 := `\def\base{` + out + `} ` + compileOptions + ` \input{src/book.tex}`
fmt.Println(bin, arg0, arg1, arg2)
out, err := exec.Command(bin, arg0, arg1, arg2).CombinedOutput()
if err != nil {
fmt.Println("%s %s", string(out), err)
}
// Rename
var src = outputDirName + "/book.pdf"
var dst = outputDirName + "/" + currentDir() + "_" + getMode() + ".pdf"
os.Rename(src, dst)
}