-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
277 lines (227 loc) · 7.19 KB
/
main.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
277
package main
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
type CallbackLine func(string)
func readLines(callbackLine CallbackLine) error {
bs, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
var buf *bytes.Buffer = bytes.NewBuffer(bs)
zr, err := gzip.NewReader(buf)
if err != nil {
return err
}
defer zr.Close()
scanner := bufio.NewScanner(zr)
for scanner.Scan() {
line := scanner.Text()
callbackLine(line)
}
return nil
}
func Exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func createCurrentStyle(defaultStyle *Style) *Style {
var styleFilePath string
if len(os.Args) > 1 {
styleFilePath = os.Args[1]
}
if Exists(styleFilePath) {
style, err := createStyle(styleFilePath)
if err == nil {
return style
} else {
return defaultStyle
}
} else {
return defaultStyle
}
}
func toStrokeObject(addStrokeContents AddOrDelStrokeContents) *StrokeObject {
strokeObject := new(StrokeObject)
strokeObject.PageUuid = "0" // default value
strokeObject.Uuid = addStrokeContents.Uuid
strokeObject.Pts = addStrokeContents.Pts
strokeObject.PenId = addStrokeContents.PenId
strokeObject.LogicalStrokeWidth = 0 // default value
return strokeObject
}
func copyStrokeObject(strokeObject StrokeObject) *StrokeObject {
retVal := new(StrokeObject)
retVal.PageUuid = strokeObject.PageUuid
retVal.Uuid = strokeObject.Uuid
retVal.Pts = strokeObject.Pts
retVal.PenId = strokeObject.PenId
retVal.LogicalStrokeWidth = strokeObject.LogicalStrokeWidth
return retVal
}
func fixColor(argbColorStr string, callback func(Color)) {
argb := strings.Split(argbColorStr, ",")
r, err1 := strconv.ParseUint(argb[1], 10, 8)
g, err2 := strconv.ParseUint(argb[2], 10, 8)
b, err3 := strconv.ParseUint(argb[3], 10, 8)
if err1 == nil && err2 == nil && err3 == nil {
callback(Color{R: uint8(r), G: uint8(g), B: uint8(b)})
}
}
type GroupAndStrokesMap map[string][]string
func appendStrokeUuidsIntoGroupAndStrokesMap(groupAndStrokesMap GroupAndStrokesMap, addGroupContents *AddGroupContents) {
key := addGroupContents.Uuid
groupAndStrokesMap[key] = []string{}
for _, addStrokeContents := range addGroupContents.Strokes {
groupAndStrokesMap[key] = append(groupAndStrokesMap[key], addStrokeContents.Uuid)
}
}
func toStrokeColorList(pageContents *PageContents) []Color {
defaultPenColor := Color{R: 8, G: 8, B: 8}
strokeColorList := []Color{}
colorMap := make(map[int]string)
//widthMap := make(map[int]int) // TODO support pen width in the future.
for _, penModel := range pageContents.ThemeSettings.PenModelList {
colorMap[penModel.PenId] = penModel.Color
}
maxPenIdValue := 0
for k, _ := range colorMap {
if maxPenIdValue < k {
maxPenIdValue = k
}
}
for penId := 0; penId < (maxPenIdValue + 1); penId++ {
penModelColor, ok := colorMap[penId]
if ok {
fixColor(penModelColor, func(color Color) {
strokeColorList = append(strokeColorList, color)
})
} else {
strokeColorList = append(strokeColorList, defaultPenColor)
}
}
return strokeColorList
}
func main() {
defaultStyle := createDefaultStyle()
groupAndStrokesMap := make(GroupAndStrokesMap)
db := new(Db)
callbackLine := func(line string) {
cmd0 := new(Cmd)
err := json.Unmarshal([]byte(line), &cmd0)
if err == nil {
if cmd0.CmdName == "PUT_APP_NAME" {
putAppNameContents := new(PutAppNameContents)
json.Unmarshal([]byte(cmd0.Contents), &putAppNameContents)
}
if cmd0.CmdName == "ADD_PAGE" {
addPageContents := new(AddPageContents)
json.Unmarshal([]byte(cmd0.Contents), &addPageContents)
pageContents := new(PageContents)
json.Unmarshal([]byte(addPageContents.PageContents), &pageContents)
defaultStyle.FillBackground = true
fixColor(pageContents.ThemeSettings.ColorModel.Paper, func(color Color) {
defaultStyle.BackgroundColor = color
})
strokeColorList := toStrokeColorList(pageContents)
if len(strokeColorList) > 0 {
defaultStyle.StrokeColorList = strokeColorList
}
}
if cmd0.CmdName == "ADD_STROKE" {
addStrokeContents := new(AddOrDelStrokeContents)
json.Unmarshal([]byte(cmd0.Contents), &addStrokeContents)
strokeObject := toStrokeObject(*addStrokeContents)
db.Add(*strokeObject)
}
if cmd0.CmdName == "ADD_STROKES" {
addStrokesContents := new(AddStrokesContents)
json.Unmarshal([]byte(cmd0.Contents), &addStrokesContents)
for _, addStrokeContents := range addStrokesContents.Strokes {
strokeObject := toStrokeObject(addStrokeContents)
db.Add(*strokeObject)
}
}
if cmd0.CmdName == "ADD_GROUP" {
addGroupContents := new(AddGroupContents)
json.Unmarshal([]byte(cmd0.Contents), &addGroupContents)
appendStrokeUuidsIntoGroupAndStrokesMap(groupAndStrokesMap, addGroupContents)
for _, addStrokeContents := range addGroupContents.Strokes {
strokeObject := toStrokeObject(addStrokeContents)
db.Add(*strokeObject)
}
}
if cmd0.CmdName == "ADD_GROUPS" {
addGroupsContents := new(AddGroupsContents)
json.Unmarshal([]byte(cmd0.Contents), &addGroupsContents)
for _, addGroupContents := range addGroupsContents.Groups {
appendStrokeUuidsIntoGroupAndStrokesMap(groupAndStrokesMap, &addGroupContents)
for _, addStrokeContents := range addGroupContents.Strokes {
strokeObject := toStrokeObject(addStrokeContents)
db.Add(*strokeObject)
}
}
}
if cmd0.CmdName == "TRANSFORM_GROUP" {
transformGroupContents := new(TransformGroupContents)
json.Unmarshal([]byte(cmd0.Contents), &transformGroupContents)
strokeUuids, ok := groupAndStrokesMap[transformGroupContents.Uuid]
if ok {
values := transformGroupContents.MatrixValues
transformMatrix := matrix2d{
values[0], values[1], values[2],
values[3], values[4], values[5],
values[6], values[7], values[8]}
for _, strokeUuid := range strokeUuids {
strokeObject, err := db.Get(strokeUuid)
if err == nil {
db.Remove(strokeUuid)
aCopy := copyStrokeObject(strokeObject)
aCopy.Pts = mapPoints(transformMatrix, strokeObject.Pts)
db.Add(*aCopy)
}
}
}
}
/*
// For now, do not do anything for this command.
if cmd0.CmdName == "DEL_GROUPS" {
delGroupsContents := new(DelGroupsContents)
json.Unmarshal([]byte(cmd0.Contents), &delGroupsContents)
}
*/
if cmd0.CmdName == "DEL_STROKES" {
delStrokesContents := new(DelStrokesContents)
json.Unmarshal([]byte(cmd0.Contents), &delStrokesContents)
for _, delStrokeContents := range delStrokesContents.Strokes {
db.Remove(delStrokeContents.Uuid)
}
}
/*
// For now, do not do anything for this command.
if cmd0.CmdName == "PUT_TITLE_TEXT" {
putTitleTextContents := new(PutTitleTextContents)
json.Unmarshal([]byte(cmd0.Contents), &putTitleTextContents)
}
// For now, do not do anything for this command.
if cmd0.CmdName == "PUT_GROUP_TEXT" {
putGroupTextContents := new(PutGroupTextContents)
json.Unmarshal([]byte(cmd0.Contents), &putGroupTextContents)
}
*/
}
}
err := readLines(callbackLine)
if err == nil {
svg := createSvg(db, createCurrentStyle(defaultStyle))
fmt.Fprintln(os.Stdout, svg)
}
}