forked from duanlizhi/gooxml
/
slide.go
171 lines (146 loc) · 5.03 KB
/
slide.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
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package presentation
import (
"errors"
"fmt"
"baliance.com/gooxml"
"baliance.com/gooxml/common"
"baliance.com/gooxml/measurement"
"baliance.com/gooxml/schema/soo/dml"
"baliance.com/gooxml/schema/soo/pml"
)
type Slide struct {
sid *pml.CT_SlideIdListEntry
x *pml.Sld
p *Presentation
}
// X returns the inner wrapped XML type.
func (s Slide) X() *pml.Sld {
return s.x
}
// PlaceHolders returns all of the content place holders within a given slide.
func (s Slide) PlaceHolders() []PlaceHolder {
ret := []PlaceHolder{}
for _, spChc := range s.x.CSld.SpTree.Choice {
for _, sp := range spChc.Sp {
if sp.NvSpPr != nil && sp.NvSpPr.NvPr != nil && sp.NvSpPr.NvPr.Ph != nil {
ret = append(ret, PlaceHolder{sp, s.x})
}
}
}
return ret
}
// GetPlaceholder returns a placeholder given its type. If there are multiplace
// placeholders of the same type, this method returns the first one. You must use the
// PlaceHolders() method to access the others.
func (s Slide) GetPlaceholder(t pml.ST_PlaceholderType) (PlaceHolder, error) {
for _, spChc := range s.x.CSld.SpTree.Choice {
for _, sp := range spChc.Sp {
if sp.NvSpPr != nil && sp.NvSpPr.NvPr != nil && sp.NvSpPr.NvPr.Ph != nil {
if sp.NvSpPr.NvPr.Ph.TypeAttr == t {
return PlaceHolder{sp, s.x}, nil
}
}
}
}
return PlaceHolder{}, errors.New("unable to find placeholder")
}
// GetPlaceholderByIndex returns a placeholder given its index. If there are multiplace
// placeholders of the same index, this method returns the first one. You must use the
// PlaceHolders() method to access the others.
func (s Slide) GetPlaceholderByIndex(idx uint32) (PlaceHolder, error) {
for _, spChc := range s.x.CSld.SpTree.Choice {
for _, sp := range spChc.Sp {
if sp.NvSpPr != nil && sp.NvSpPr.NvPr != nil && sp.NvSpPr.NvPr.Ph != nil {
if (idx == 0 && sp.NvSpPr.NvPr.Ph.IdxAttr == nil) ||
(sp.NvSpPr.NvPr.Ph.IdxAttr != nil && *sp.NvSpPr.NvPr.Ph.IdxAttr == idx) {
return PlaceHolder{sp, s.x}, nil
}
}
}
}
return PlaceHolder{}, errors.New("unable to find placeholder")
}
// ValidateWithPath validates the slide passing path informaton for a better
// error message
func (s Slide) ValidateWithPath(path string) error {
// schema checks
if err := s.x.ValidateWithPath(path); err != nil {
return err
}
// stuff we've figured out
for _, c := range s.x.CSld.SpTree.Choice {
for _, sp := range c.Sp {
if sp.TxBody != nil {
if len(sp.TxBody.P) == 0 {
return errors.New(path + " : slide shape with a txbody must contain paragraphs")
}
}
}
}
return nil
}
// AddTextBox adds an empty textbox to a slide.
func (s Slide) AddTextBox() TextBox {
c := pml.NewCT_GroupShapeChoice()
s.x.CSld.SpTree.Choice = append(s.x.CSld.SpTree.Choice, c)
sp := pml.NewCT_Shape()
c.Sp = append(c.Sp, sp)
sp.SpPr = dml.NewCT_ShapeProperties()
sp.SpPr.Xfrm = dml.NewCT_Transform2D()
sp.SpPr.PrstGeom = dml.NewCT_PresetGeometry2D()
sp.SpPr.PrstGeom.PrstAttr = dml.ST_ShapeTypeRect
sp.TxBody = dml.NewCT_TextBody()
sp.TxBody.BodyPr = dml.NewCT_TextBodyProperties()
sp.TxBody.BodyPr.WrapAttr = dml.ST_TextWrappingTypeSquare
sp.TxBody.BodyPr.SpAutoFit = dml.NewCT_TextShapeAutofit()
tb := TextBox{sp}
tb.Properties().SetWidth(3 * measurement.Inch)
tb.Properties().SetHeight(1 * measurement.Inch)
tb.Properties().SetPosition(0, 0)
return tb
}
// AddImage adds an image textbox to a slide.
func (s Slide) AddImage(img common.ImageRef) Image {
c := pml.NewCT_GroupShapeChoice()
s.x.CSld.SpTree.Choice = append(s.x.CSld.SpTree.Choice, c)
pic := pml.NewCT_Picture()
c.Pic = append(c.Pic, pic)
pic.NvPicPr.CNvPicPr = dml.NewCT_NonVisualPictureProperties()
pic.NvPicPr.CNvPicPr.PicLocks = dml.NewCT_PictureLocking()
pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspectAttr = gooxml.Bool(true)
pic.BlipFill = dml.NewCT_BlipFillProperties()
pic.BlipFill.Blip = dml.NewCT_Blip()
imgIdx := 0
for i, ig := range s.p.Images {
if ig == img {
imgIdx = i + 1
break
}
}
var imgID string
for i, os := range s.p.Slides() {
if os.x == s.x {
fn := fmt.Sprintf("../media/image%d.%s", imgIdx, img.Format())
rel := s.p.slideRels[i].AddRelationship(fn, gooxml.ImageType)
imgID = rel.ID()
}
}
pic.BlipFill.Blip.EmbedAttr = gooxml.String(imgID)
pic.BlipFill.Stretch = dml.NewCT_StretchInfoProperties()
pic.BlipFill.Stretch.FillRect = dml.NewCT_RelativeRect()
pic.SpPr = dml.NewCT_ShapeProperties()
pic.SpPr.PrstGeom = dml.NewCT_PresetGeometry2D()
pic.SpPr.PrstGeom.PrstAttr = dml.ST_ShapeTypeRect
ir := Image{pic}
sz := img.Size()
ir.Properties().SetWidth(measurement.Distance(sz.X) * measurement.Pixel72)
ir.Properties().SetHeight(measurement.Distance(sz.Y) * measurement.Pixel72)
ir.Properties().SetPosition(0, 0)
return ir
}