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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
using System;
using System.Linq;
using System.Xml.Linq;
using System.Drawing;
using System.Globalization;
namespace Novacode
{
    /// <summary>
    /// A text formatting.
    /// </summary>
    public class Formatting : IComparable
    {
        private XElement rPr;
        private bool hidden;
        private bool bold;
        private bool italic;
        private StrikeThrough strikethrough;
        private Script script;
        private Highlight highlight;
        private double? size;
        private Color? fontColor;
        private Color? underlineColor;
        private UnderlineStyle underlineStyle;
        private Misc misc;
        private CapsStyle capsStyle;
        private FontFamily fontFamily;
        private int? percentageScale;
        private int? kerning;
        private int? position;
        private double? spacing;
 
        private CultureInfo language;
 
        /// <summary>
        /// A text formatting.
        /// </summary>
        public Formatting()
        {
            capsStyle = CapsStyle.none;
            strikethrough = StrikeThrough.none;
            script = Script.none;
            highlight = Highlight.none;
            underlineStyle = UnderlineStyle.none;
            misc = Misc.none;
 
            // Use current culture by default
            language = CultureInfo.CurrentCulture;
 
            rPr = new XElement(XName.Get("rPr", DocX.w.NamespaceName));
        }
 
        /// <summary>
        /// Text language
        /// </summary>
        public CultureInfo Language 
        { 
            get 
            { 
                return language; 
            } 
            
            set 
            { 
                language = value; 
            } 
        }
 
        public static Formatting Parse(XElement rPr)
        {
            Formatting formatting = new Formatting();
 
            // Build up the Formatting object.
            foreach (XElement option in rPr.Elements())
            {
                switch (option.Name.LocalName)
                {
                    case "lang": formatting.Language = new CultureInfo(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName), null) ?? option.GetAttribute(XName.Get("eastAsia", DocX.w.NamespaceName), null) ?? option.GetAttribute(XName.Get("bidi", DocX.w.NamespaceName))); break;
                    case "spacing": formatting.Spacing = Double.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 20.0; break;
                    case "position": formatting.Position = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2; break;
                    case "kern": formatting.Position = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2; break;
                    case "w": formatting.PercentageScale = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))); break;
                    case "rFonts": break;
                    case "vanish": formatting.hidden = true; break;
                    case "b": formatting.Bold = true; break;
                    case "i": formatting.Italic = true; break;
 
                    default: break;
                }
            }
 
            return formatting;
        }
 
        internal XElement Xml
        {
            get
            {
                rPr = new XElement(XName.Get("rPr", DocX.w.NamespaceName));
 
                if (language != null)
                    rPr.Add(new XElement(XName.Get("lang", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), language.Name)));
                
                if(spacing.HasValue)
                    rPr.Add(new XElement(XName.Get("spacing", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), spacing.Value * 20)));
 
                if(position.HasValue)
                    rPr.Add(new XElement(XName.Get("position", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), position.Value * 2)));                   
 
                if (kerning.HasValue)
                    rPr.Add(new XElement(XName.Get("kern", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), kerning.Value * 2)));                   
 
                if (percentageScale.HasValue)
                    rPr.Add(new XElement(XName.Get("w", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), percentageScale)));
 
                if (fontFamily != null)
                {
                    rPr.Add
                    (
                        new XElement
                        (
                            XName.Get("rFonts", DocX.w.NamespaceName), 
                            new XAttribute(XName.Get("ascii", DocX.w.NamespaceName), fontFamily.Name),
                            new XAttribute(XName.Get("hAnsi", DocX.w.NamespaceName), fontFamily.Name), // Added by Maurits Elbers to support non-standard characters. See http://docx.codeplex.com/Thread/View.aspx?ThreadId=70097&ANCHOR#Post453865
                            new XAttribute(XName.Get("cs", DocX.w.NamespaceName), fontFamily.Name)    // Added by Maurits Elbers to support non-standard characters. See http://docx.codeplex.com/Thread/View.aspx?ThreadId=70097&ANCHOR#Post453865
                        )
                    );
                }
 
                if(hidden)
                    rPr.Add(new XElement(XName.Get("vanish", DocX.w.NamespaceName)));
 
                if (bold)
                    rPr.Add(new XElement(XName.Get("b", DocX.w.NamespaceName)));
 
                if (italic)
                    rPr.Add(new XElement(XName.Get("i", DocX.w.NamespaceName)));
 
                switch (underlineStyle)
                {
                    case UnderlineStyle.none:
                        break;
                    case UnderlineStyle.singleLine:
                        rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "single")));
                        break;
                    case UnderlineStyle.doubleLine:
                        rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "double")));
                        break;
                    default:
                        rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), underlineStyle.ToString())));
                        break;
                }
 
                if(underlineColor.HasValue)
                {
                    // If an underlineColor has been set but no underlineStyle has been set
                    if (underlineStyle == UnderlineStyle.none)
                    {
                        // Set the underlineStyle to the default
                        underlineStyle = UnderlineStyle.singleLine;
                        rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "single")));
                    }
 
                    rPr.Element(XName.Get("u", DocX.w.NamespaceName)).Add(new XAttribute(XName.Get("color", DocX.w.NamespaceName), underlineColor.Value.ToHex()));
                }
 
                switch (strikethrough)
                {
                    case StrikeThrough.none:
                        break;
                    case StrikeThrough.strike:
                        rPr.Add(new XElement(XName.Get("strike", DocX.w.NamespaceName)));
                        break;
                    case StrikeThrough.doubleStrike:
                        rPr.Add(new XElement(XName.Get("dstrike", DocX.w.NamespaceName)));
                        break;
                    default:
                        break;
                }
                  
                switch (script)
                {
                    case Script.none:
                        break;
                    default:
                        rPr.Add(new XElement(XName.Get("vertAlign", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), script.ToString())));
                        break;
                }
 
                if (size.HasValue)
                {
                    rPr.Add(new XElement(XName.Get("sz", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), (size * 2).ToString())));
                    rPr.Add(new XElement(XName.Get("szCs", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), (size * 2).ToString())));
                }
 
                if(fontColor.HasValue)
                    rPr.Add(new XElement(XName.Get("color", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), fontColor.Value.ToHex())));
 
                switch (highlight)
                {
                    case Highlight.none:
                        break;
                    default:
                        rPr.Add(new XElement(XName.Get("highlight", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), highlight.ToString())));
                        break;
                }
 
                switch (capsStyle)
                {
                    case CapsStyle.none:
                        break;
                    default:
                        rPr.Add(new XElement(XName.Get(capsStyle.ToString(), DocX.w.NamespaceName)));
                        break;
                }
 
                switch (misc)
                {
                    case Misc.none:
                        break;
                    case Misc.outlineShadow:
                        rPr.Add(new XElement(XName.Get("outline", DocX.w.NamespaceName)));
                        rPr.Add(new XElement(XName.Get("shadow", DocX.w.NamespaceName)));
                        break;
                    case Misc.engrave:
                        rPr.Add(new XElement(XName.Get("imprint", DocX.w.NamespaceName)));
                        break;
                    default:
                        rPr.Add(new XElement(XName.Get(misc.ToString(), DocX.w.NamespaceName)));
                        break;
                }
 
                return rPr;
            }
        }
 
        /// <summary>
        /// This formatting will apply Bold.
        /// </summary>
        public bool Bold { get { return bold; } set { bold = value;} }
 
        /// <summary>
        /// This formatting will apply Italic.
        /// </summary>
        public bool Italic { get { return italic; } set { italic = value; } }
 
        /// <summary>
        /// This formatting will apply StrickThrough.
        /// </summary>
        public StrikeThrough StrikeThrough { get { return strikethrough; } set { strikethrough = value; } }
 
        /// <summary>
        /// The script that this formatting should be, normal, superscript or subscript.
        /// </summary>
        public Script Script { get { return script; } set { script = value; } }
        
        /// <summary>
        /// The Size of this text, must be between 0 and 1638.
        /// </summary>
        public double? Size 
        { 
            get { return size; } 
            
            set 
            { 
                double? temp = value * 2;
 
                if (temp - (int)temp == 0)
                {
                    if(value > 0 && value < 1639)
                        size = value;
                    else
                        throw new ArgumentException("Size", "Value must be in the range 0 - 1638");
                }
 
                else
                    throw new ArgumentException("Size", "Value must be either a whole or half number, examples: 32, 32.5");
            } 
        }
 
        /// <summary>
        /// Percentage scale must be one of the following values 200, 150, 100, 90, 80, 66, 50 or 33.
        /// </summary>
        public int? PercentageScale
        { 
            get { return percentageScale; } 
            
            set 
            {
                if ((new int?[] { 200, 150, 100, 90, 80, 66, 50, 33 }).Contains(value))
                    percentageScale = value; 
                else
                    throw new ArgumentOutOfRangeException("PercentageScale", "Value must be one of the following: 200, 150, 100, 90, 80, 66, 50 or 33");
            } 
        }
 
        /// <summary>
        /// The Kerning to apply to this text must be one of the following values 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72.
        /// </summary>
        public int? Kerning 
        { 
            get { return kerning; } 
            
            set 
            { 
                if(new int?[] {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72}.Contains(value))
                    kerning = value; 
                else
                    throw new ArgumentOutOfRangeException("Kerning", "Value must be one of the following: 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48 or 72");
            } 
        }
 
        /// <summary>
        /// Text position must be in the range (-1585 - 1585).
        /// </summary>
        public int? Position
        {
            get { return position; }
 
            set
            {
                if (value > -1585 && value < 1585)
                    position = value;
                else
                    throw new ArgumentOutOfRangeException("Position", "Value must be in the range -1585 - 1585");
            }
        }
 
        /// <summary>
        /// Text spacing must be in the range (-1585 - 1585).
        /// </summary>
        public double? Spacing
        {
            get { return spacing; }
 
            set
            {
                double? temp = value * 20;
 
                if (temp - (int)temp == 0)
                {
                    if (value > -1585 && value < 1585)
                        spacing = value;
                    else
                        throw new ArgumentException("Spacing", "Value must be in the range: -1584 - 1584");
                }
 
                else
                    throw new ArgumentException("Spacing", "Value must be either a whole or acurate to one decimal, examples: 32, 32.1, 32.2, 32.9");
            } 
        }
 
        /// <summary>
        /// The colour of the text.
        /// </summary>
        public Color? FontColor { get { return fontColor; } set { fontColor = value; } }
 
        /// <summary>
        /// Highlight colour.
        /// </summary>
        public Highlight Highlight { get { return highlight; } set { highlight = value; } }
       
        /// <summary>
        /// The Underline style that this formatting applies.
        /// </summary>
        public UnderlineStyle UnderlineStyle { get { return underlineStyle; } set { underlineStyle = value; } }
        
        /// <summary>
        /// The underline colour.
        /// </summary>
        public Color? UnderlineColor { get { return underlineColor; } set { underlineColor = value; } }
        
        /// <summary>
        /// Misc settings.
        /// </summary>
        public Misc Misc { get { return misc; } set { misc = value; } }
        
        /// <summary>
        /// Is this text hidden or visible.
        /// </summary>
        public bool Hidden { get { return hidden; } set { hidden = value; } }
        
        /// <summary>
        /// Capitalization style.
        /// </summary>
        public CapsStyle CapsStyle { get { return capsStyle; } set { capsStyle = value; } }
        
        /// <summary>
        /// The font familt of this formatting.
        /// </summary>
        /// <!-- 
        /// Bug found and fixed by krugs525 on August 12 2009.
        /// Use TFS compare to see exact code change.
        /// -->
        public FontFamily FontFamily { get { return fontFamily; } set { fontFamily = value; } }
 
        public int CompareTo(object obj)
        {
            Formatting other = (Formatting)obj;
 
            if(other.hidden != this.hidden)
                return -1;
 
            if(other.bold != this.bold)
                return -1;
 
            if(other.italic != this.italic)
                return -1;
 
            if(other.strikethrough != this.strikethrough)
                return -1;
 
            if(other.script != this.script)
                return -1;
 
            if(other.highlight != this.highlight)
                return -1;
 
            if(other.size != this.size)
                return -1;
 
            if(other.fontColor != this.fontColor)
                return -1;
 
            if(other.underlineColor != this.underlineColor)
                return -1;
 
            if(other.underlineStyle != this.underlineStyle)
                return -1;
 
            if(other.misc != this.misc)
                return -1;
 
            if(other.capsStyle != this.capsStyle)
                return -1;
 
            if(other.fontFamily != this.fontFamily)
                return -1;
 
            if(other.percentageScale != this.percentageScale)
                return -1;
 
            if(other.kerning != this.kerning)
                return -1;
 
            if(other.position != this.position)
                return -1;
 
            if(other.spacing != this.spacing)
                return -1;
 
            if (!other.language.Equals(this.language))
                return -1;
 
            return 0;
        }
    }
}