-
Notifications
You must be signed in to change notification settings - Fork 21
/
deck.go
295 lines (272 loc) · 8.83 KB
/
deck.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Package deck makes slide decks
package deck
import (
"encoding/xml"
"fmt"
"io"
"os"
"strings"
)
// Deck defines the structure of a presentation deck
// The size of the canvas, and series of slides
type Deck struct {
Title string `xml:"title"`
Creator string `xml:"creator"`
Subject string `xml:"subject"`
Publisher string `xml:"publisher"`
Description string `xml:"description"`
Date string `xml:"date"`
Canvas canvas `xml:"canvas"`
Slide []Slide `xml:"slide"`
}
type canvas struct {
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
}
// Slide is the structure of an individual slide within a deck
// <slide bg="black" fg="rgb(255,255,255)" duration="2s" note="hello, world">
// <slide gradcolor1="black" gradcolor2="white" gp="20" duration="2s" note="wassup">
type Slide struct {
Bg string `xml:"bg,attr"`
Fg string `xml:"fg,attr"`
Gradcolor1 string `xml:"gradcolor1,attr"`
Gradcolor2 string `xml:"gradcolor2,attr"`
GradPercent float64 `xml:"gp,attr"`
Duration string `xml:"duration,attr"`
Note string `xml:"note"`
List []List `xml:"list"`
Text []Text `xml:"text"`
Image []Image `xml:"image"`
Ellipse []Ellipse `xml:"ellipse"`
Line []Line `xml:"line"`
Rect []Rect `xml:"rect"`
Curve []Curve `xml:"curve"`
Arc []Arc `xml:"arc"`
Polygon []Polygon `xml:"polygon"`
Polyline []Polyline `xml:"polyline"`
}
// CommonAttr are the common attributes for text and list
type CommonAttr struct {
Xp float64 `xml:"xp,attr"` // X coordinate
Yp float64 `xml:"yp,attr"` // Y coordinate
Sp float64 `xml:"sp,attr"` // size
Lp float64 `xml:"lp,attr"` // linespacing (leading) percentage
Rotation float64 `xml:"rotation,attr"` // Rotation (0-360 degrees)
Type string `xml:"type,attr"` // type: block, plain, code, number, bullet
Align string `xml:"align,attr"` // alignment: center, end, begin
Color string `xml:"color,attr"` // item color
Gradcolor1 string `xml:"gradcolor1,attr"` // gradient color 1
Gradcolor2 string `xml:"gradcolor2,attr"` // gradient color 2
GradPercent float64 `xml:"gp,attr"` // gradient percentage
Opacity float64 `xml:"opacity,attr"` // opacity percentage
Font string `xml:"font,attr"` // font type: i.e. sans, serif, mono
Link string `xml:"link,attr"` // reference to other content (i.e. http:// or mailto:)
}
// Dimension describes a graphics object with width and height
type Dimension struct {
CommonAttr
Wp float64 `xml:"wp,attr"` // width percentage
Hp float64 `xml:"hp,attr"` // height percentage
Hr float64 `xml:"hr,attr"` // height relative percentage
Hw float64 `xml:"hw,attr"` // height by width
}
// ListItem describes a list item
// <list xp="20" yp="70" sp="1.5">
//
// <li>canvas<li>
// <li>slide</li>
//
// </list>
type ListItem struct {
Color string `xml:"color,attr"`
Opacity float64 `xml:"opacity,attr"`
Font string `xml:"font,attr"`
ListText string `xml:",chardata"`
}
// List describes the list element
type List struct {
CommonAttr
Wp float64 `xml:"wp,attr"`
Li []ListItem `xml:"li"`
}
// Text describes the text element
type Text struct {
CommonAttr
Wp float64 `xml:"wp,attr"`
File string `xml:"file,attr"`
Tdata string `xml:",chardata"`
}
// Image describes an image
// <image xp="20" yp="30" width="256" height="256" scale="50" name="picture.png" caption="Pretty picture"/>
type Image struct {
CommonAttr
Width int `xml:"width,attr"` // image width
Height int `xml:"height,attr"` // image height
Scale float64 `xml:"scale,attr"` // image scale percentage
Autoscale string `xml:"autoscale,attr"` // scale the image to the canvas
Name string `xml:"name,attr"` // image file name
Caption string `xml:"caption,attr"` // image caption
}
// Ellipse describes a rectangle with x,y,w,h
// <ellipse xp="45" yp="10" wp="4" hr="75" color="rgb(0,127,0)"/>
type Ellipse struct {
Dimension
}
// Rect describes a rectangle with x,y,w,h
// <rect xp="35" yp="10" wp="4" hp="3"/>
type Rect struct {
Dimension
}
// Line defines a straight line
// <line xp1="20" yp1="10" xp2="30" yp2="10"/>
type Line struct {
Xp1 float64 `xml:"xp1,attr"` // begin x coordinate
Yp1 float64 `xml:"yp1,attr"` // begin y coordinate
Xp2 float64 `xml:"xp2,attr"` // end x coordinate
Yp2 float64 `xml:"yp2,attr"` // end y coordinate
Sp float64 `xml:"sp,attr"` // line thickness
Color string `xml:"color,attr"` // line color
Opacity float64 `xml:"opacity,attr"` // line opacity (1-100)
}
// Curve defines a quadratic Bezier curve
// The begining, ending, and control points are required:
// <curve xp1="60" yp1="10" xp2="75" yp2="20" xp3="70" yp3="10" />
type Curve struct {
Xp1 float64 `xml:"xp1,attr"`
Yp1 float64 `xml:"yp1,attr"`
Xp2 float64 `xml:"xp2,attr"`
Yp2 float64 `xml:"yp2,attr"`
Xp3 float64 `xml:"xp3,attr"`
Yp3 float64 `xml:"yp3,attr"`
Sp float64 `xml:"sp,attr"`
Color string `xml:"color,attr"`
Opacity float64 `xml:"opacity,attr"`
}
// Arc defines an elliptical arc
// the arc is defined by a beginning and ending angle in percentages
// <arc xp="55" yp="10" wp="4" hr="75" a1="0" a2="180"/>
type Arc struct {
Dimension
A1 float64 `xml:"a1,attr"`
A2 float64 `xml:"a2,attr"`
Sp float64 `xml:"sp,attr"`
Opacity float64 `xml:"opacity,attr"`
}
// Polygon defines a polygon, x and y coordinates are specified by
// strings of space-separated percentages:
// <polygon xc="10 20 30" yc="30 40 50"/>
type Polygon struct {
XC string `xml:"xc,attr"`
YC string `xml:"yc,attr"`
Color string `xml:"color,attr"`
Opacity float64 `xml:"opacity,attr"`
}
// Polyline defines a polyline, x and y coordinates are specified by
// strings of space-separated percentages:
// <polyline xc="10 20 30" yc="30 40 50"/>
type Polyline struct {
XC string `xml:"xc,attr"`
YC string `xml:"yc,attr"`
Sp float64 `xml:"sp,attr"` // line thickness
Color string `xml:"color,attr"`
Opacity float64 `xml:"opacity,attr"`
}
// ReadDeck reads the deck description file from a io.Reader
func ReadDeck(r io.ReadCloser, w, h int) (Deck, error) {
var d Deck
err := xml.NewDecoder(r).Decode(&d)
if d.Canvas.Width == 0 {
d.Canvas.Width = w
}
if d.Canvas.Height == 0 {
d.Canvas.Height = h
}
r.Close()
return d, err
}
// Read reads the deck description file
func Read(filename string, w, h int) (Deck, error) {
var d Deck
if filename == "-" {
return ReadDeck(os.Stdin, w, h)
}
r, err := os.Open(filename)
if err != nil {
return d, err
}
return ReadDeck(r, w, h)
}
// Dimen computes the coordinates and size of an object
func Dimen(c canvas, xp, yp, sp float64) (x, y, s float64) {
x = (xp / 100) * float64(c.Width)
y = (yp / 100) * float64(c.Height)
s = (sp / 100) * float64(c.Width)
return
}
// Pwidth computes the percent width based on canvas size
func Pwidth(wp, cw, defval float64) float64 {
if wp == 0 {
return defval
}
return (wp / 100) * cw
}
// Search searches the deck for the specified text, returning the slide number if found
func Search(d Deck, s string) int {
// for every slide...
for i, slide := range d.Slide {
// search lists
for _, l := range slide.List {
for _, ll := range l.Li {
if strings.Contains(ll.ListText, s) {
return i
}
}
}
// search text
for _, t := range slide.Text {
if strings.Contains(t.Tdata, s) {
return i
}
}
}
return -1
}
// Dump shows the decoded description
func Dump(d Deck) {
fmt.Printf("Title: %#v\nCreator: %#v\nDescription: %#v\nDate: %#v\nPublisher: %#v\nSubject: %#v\n",
d.Title, d.Creator, d.Description, d.Date, d.Publisher, d.Subject)
fmt.Printf("Canvas = %v\n", d.Canvas)
for i, s := range d.Slide {
fmt.Printf("Slide [%d] = %+v %+v %+v %+v %+v %+v\n", i, s.Bg, s.Fg, s.Duration, s.Gradcolor1, s.Gradcolor2, s.GradPercent)
for j, l := range s.List {
fmt.Printf("\tList [%d] = %+v\n", j, l)
}
for k, t := range s.Text {
fmt.Printf("\tText [%d] = %+v\n", k, t)
}
for m, im := range s.Image {
fmt.Printf("\tImage [%d] = %+v\n", m, im)
}
for l, line := range s.Line {
fmt.Printf("\tLine [%d] = %+v\n", l, line)
}
for r, rect := range s.Rect {
fmt.Printf("\tRect [%d] = %+v\n", r, rect)
}
for a, arc := range s.Arc {
fmt.Printf("\tArc [%d] = %+v\n", a, arc)
}
for c, curve := range s.Curve {
fmt.Printf("\tCurve [%d] = %+v\n", c, curve)
}
for e, ellipse := range s.Ellipse {
fmt.Printf("\tEllipse [%d] = %+v\n", e, ellipse)
}
for p, polygon := range s.Polygon {
fmt.Printf("\tPolygon [%d] = %+v\n", p, polygon)
}
for p, polyline := range s.Polyline {
fmt.Printf("\tPolyline [%d] = %+v\n", p, polyline)
}
}
}