jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.IO.Packaging;
 
namespace Novacode
{
    /// <summary>
    /// Represents a Picture in this document, a Picture is a customized view of an Image.
    /// </summary>
    public class Picture: DocXElement
    {
        private const int EmusInPixel = 9525;
 
        internal Dictionary<PackagePart, PackageRelationship> picture_rels;
        
        internal Image img;
        private string id;
        private string name;
        private string descr;
        private int cx, cy;
        //private string fileName;
        private uint rotation;
        private bool hFlip, vFlip;
        private object pictureShape;
        private XElement xfrm;
        private XElement prstGeom;
 
        /// <summary>
        /// Remove this Picture from this document.
        /// </summary>
        public void Remove()
        {
            Xml.Remove();
        }
 
        /// <summary>
        /// Wraps an XElement as an Image
        /// </summary>
        /// <param name="i">The XElement i to wrap</param>
        internal Picture(DocX document, XElement i, Image img):base(document, i)
        {
            picture_rels = new Dictionary<PackagePart, PackageRelationship>();
            
            this.img = img;
 
            this.id =
            (
                from e in Xml.Descendants()
                where e.Name.LocalName.Equals("blip")
                select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value
            ).Single(); 
 
            this.name = 
            (
                from e in Xml.Descendants()
                let a = e.Attribute(XName.Get("name"))
                where (a != null)
                select a.Value
            ).First();
          
            this.descr =
            (
                from e in Xml.Descendants()
                let a = e.Attribute(XName.Get("descr"))
                where (a != null)
                select a.Value
            ).FirstOrDefault();
 
            this.cx = 
            (
                from e in Xml.Descendants()
                let a = e.Attribute(XName.Get("cx"))
                where (a != null)
                select int.Parse(a.Value)
            ).First();
 
            this.cy = 
            (
                from e in Xml.Descendants()
                let a = e.Attribute(XName.Get("cy"))
                where (a != null)
                select int.Parse(a.Value)
            ).First();
 
            this.xfrm =
            (
                from d in Xml.Descendants()
                where d.Name.LocalName.Equals("xfrm")
                select d
            ).Single();
 
            this.prstGeom =
            (
                from d in Xml.Descendants()
                where d.Name.LocalName.Equals("prstGeom")
                select d
            ).Single();
 
            this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value);
        }
 
        private void SetPictureShape(object shape)
        {
            this.pictureShape = shape;
 
            XAttribute prst = prstGeom.Attribute(XName.Get("prst"));
            if (prst == null)
                prstGeom.Add(new XAttribute(XName.Get("prst"), "rectangle"));
 
            prstGeom.Attribute(XName.Get("prst")).Value = shape.ToString();
        }
 
        /// <summary>
        /// Set the shape of this Picture to one in the BasicShapes enumeration.
        /// </summary>
        /// <param name="shape">A shape from the BasicShapes enumeration.</param>
        public void SetPictureShape(BasicShapes shape)
        {
            SetPictureShape((object)shape);
        }
 
        /// <summary>
        /// Set the shape of this Picture to one in the RectangleShapes enumeration.
        /// </summary>
        /// <param name="shape">A shape from the RectangleShapes enumeration.</param>
        public void SetPictureShape(RectangleShapes shape)
        {
            SetPictureShape((object)shape);
        }
 
        /// <summary>
        /// Set the shape of this Picture to one in the BlockArrowShapes enumeration.
        /// </summary>
        /// <param name="shape">A shape from the BlockArrowShapes enumeration.</param>
        public void SetPictureShape(BlockArrowShapes shape)
        {
            SetPictureShape((object)shape);
        }
 
        /// <summary>
        /// Set the shape of this Picture to one in the EquationShapes enumeration.
        /// </summary>
        /// <param name="shape">A shape from the EquationShapes enumeration.</param>
        public void SetPictureShape(EquationShapes shape)
        {
            SetPictureShape((object)shape);
        }
 
        /// <summary>
        /// Set the shape of this Picture to one in the FlowchartShapes enumeration.
        /// </summary>
        /// <param name="shape">A shape from the FlowchartShapes enumeration.</param>
        public void SetPictureShape(FlowchartShapes shape)
        {
            SetPictureShape((object)shape);
        }
 
        /// <summary>
        /// Set the shape of this Picture to one in the StarAndBannerShapes enumeration.
        /// </summary>
        /// <param name="shape">A shape from the StarAndBannerShapes enumeration.</param>
        public void SetPictureShape(StarAndBannerShapes shape)
        {
            SetPictureShape((object)shape);
        }
 
        /// <summary>
        /// Set the shape of this Picture to one in the CalloutShapes enumeration.
        /// </summary>
        /// <param name="shape">A shape from the CalloutShapes enumeration.</param>
        public void SetPictureShape(CalloutShapes shape)
        {
            SetPictureShape((object)shape);
        }
 
        /// <summary>
        /// A unique id that identifies an Image embedded in this document.
        /// </summary>
        public string Id
        {
            get { return id; }
        }
 
        /// <summary>
        /// Flip this Picture Horizontally.
        /// </summary>
        public bool FlipHorizontal
        {
            get { return hFlip; }
 
            set
            {
                hFlip = value;
 
                XAttribute flipH = xfrm.Attribute(XName.Get("flipH"));
                if (flipH == null)
                    xfrm.Add(new XAttribute(XName.Get("flipH"), "0"));
 
                xfrm.Attribute(XName.Get("flipH")).Value = hFlip ? "1" : "0";
            }
        }
 
        /// <summary>
        /// Flip this Picture Vertically.
        /// </summary>
        public bool FlipVertical
        {
            get { return vFlip; }
 
            set
            {
                vFlip = value;
 
                XAttribute flipV = xfrm.Attribute(XName.Get("flipV"));
                if (flipV == null)
                    xfrm.Add(new XAttribute(XName.Get("flipV"), "0"));
 
                xfrm.Attribute(XName.Get("flipV")).Value = vFlip ? "1" : "0";
            }
        }
 
        /// <summary>
        /// The rotation in degrees of this image, actual value = value % 360
        /// </summary>
        public uint Rotation
        {
            get { return rotation / 60000; }
 
            set
            {
                rotation = (value % 360) * 60000;
                XElement xfrm =
                    (from d in Xml.Descendants()
                    where d.Name.LocalName.Equals("xfrm")
                    select d).Single();
 
                XAttribute rot = xfrm.Attribute(XName.Get("rot"));
                if(rot == null)
                    xfrm.Add(new XAttribute(XName.Get("rot"), 0));
 
                xfrm.Attribute(XName.Get("rot")).Value = rotation.ToString();
            }
        }
 
        /// <summary>
        /// Gets or sets the name of this Image.
        /// </summary>
        public string Name 
        { 
            get { return name; } 
            
            set 
            { 
                name = value;
 
                foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("name")))
                    a.Value = name;
            } 
        }
 
        /// <summary>
        /// Gets or sets the description for this Image.
        /// </summary>
        public string Description 
        { 
            get { return descr; } 
            
            set 
            { 
                descr = value;
 
                foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("descr")))
                    a.Value = descr;
            } 
        }
 
        ///<summary>
        /// Returns the name of the image file for the picture.
        ///</summary>
        public string FileName
        {
          get
          {
            return img.FileName;
          }
        }
 
        /// <summary>
        /// Get or sets the Width of this Image.
        /// </summary>
        public int Width 
        { 
            get { return cx / EmusInPixel; }
            
            set 
            { 
                cx = value;
 
                foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("cx")))
                    a.Value = (cx * EmusInPixel).ToString();
            } 
        }
 
        /// <summary>
        /// Get or sets the height of this Image.
        /// </summary>
        public int Height 
        { 
            get { return cy / EmusInPixel; }
            
            set 
            { 
                cy = value;
 
                foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("cy")))
                    a.Value = (cy * EmusInPixel).ToString();
            } 
        }
 
        //public void Delete()
        //{
        //    // Remove xml
        //    i.Remove();
   
        //    // Rebuild the image collection for this paragraph
        //    // Requires that every Image have a link to its paragraph
 
        //}
    }
}