Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Style/XmlAccess/ExcelFontXml.cs @ 13401

Last change on this file since 13401 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 10.7 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Jan Källman                    Initial Release           2009-10-01
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Globalization;
35using System.Text;
36using System.Xml;
37namespace OfficeOpenXml.Style.XmlAccess
38{
39    /// <summary>
40    /// Xml access class for fonts
41    /// </summary>
42    public sealed class ExcelFontXml : StyleXmlHelper
43    {
44        internal ExcelFontXml(XmlNamespaceManager nameSpaceManager)
45            : base(nameSpaceManager)
46        {
47            _name = "";
48            _size = 0;
49            _family = int.MinValue;
50            _scheme = "";
51            _color = _color = new ExcelColorXml(NameSpaceManager);
52            _bold = false;
53            _italic = false;
54            _strike = false;
55            _underlineType = ExcelUnderLineType.None ;
56            _verticalAlign = "";
57        }
58        internal ExcelFontXml(XmlNamespaceManager nsm, XmlNode topNode) :
59            base(nsm, topNode)
60        {
61            _name = GetXmlNodeString(namePath);
62            _size = (float)GetXmlNodeDecimal(sizePath);
63            _family = GetXmlNodeIntNull(familyPath)??int.MinValue;
64            _scheme = GetXmlNodeString(schemePath);
65            _color = new ExcelColorXml(nsm, topNode.SelectSingleNode(_colorPath, nsm));
66            _bold = (topNode.SelectSingleNode(boldPath, NameSpaceManager) != null);
67            _italic = (topNode.SelectSingleNode(italicPath, NameSpaceManager) != null);
68            _strike = (topNode.SelectSingleNode(strikePath, NameSpaceManager) != null);
69            _verticalAlign = GetXmlNodeString(verticalAlignPath);
70            if (topNode.SelectSingleNode(underLinedPath, NameSpaceManager) != null)
71            {
72                string ut = GetXmlNodeString(underLinedPath + "/@val");
73                if (ut == "")
74                {
75                    _underlineType = ExcelUnderLineType.Single;
76                }
77                else
78                {
79                    _underlineType = (ExcelUnderLineType)Enum.Parse(typeof(ExcelUnderLineType), ut, true);
80                }
81            }
82            else
83            {
84                _underlineType = ExcelUnderLineType.None;
85            }
86        }
87        internal override string Id
88        {
89            get
90            {
91                return Name + "|" + Size + "|" + Family + "|" + Color.Id + "|" + Scheme + "|" + Bold.ToString() + "|" + Italic.ToString() + "|" + Strike.ToString() + "|" + VerticalAlign + "|" + UnderLineType.ToString();
92            }
93        }
94        const string namePath = "d:name/@val";
95        string _name;
96        /// <summary>
97        /// The name of the font
98        /// </summary>
99        public string Name
100        {
101            get
102            {
103                return _name;
104            }
105            set
106            {
107                Scheme = "";        //Reset schema to avoid corrupt file if unsupported font is selected.
108                _name = value;
109            }
110        }
111        const string sizePath = "d:sz/@val";
112        float _size;
113        /// <summary>
114        /// Font size
115        /// </summary>
116        public float Size
117        {
118            get
119            {
120                return _size;
121            }
122            set
123            {
124                _size = value;
125            }
126        }
127        const string familyPath = "d:family/@val";
128        int _family;
129        /// <summary>
130        /// Font family
131        /// </summary>
132        public int Family
133        {
134            get
135            {
136                return (_family == int.MinValue ? 0 : _family); ;
137            }
138            set
139            {
140                _family=value;
141            }
142        }
143        ExcelColorXml _color = null;
144        const string _colorPath = "d:color";
145        /// <summary>
146        /// Text color
147        /// </summary>
148        public ExcelColorXml Color
149        {
150            get
151            {
152                return _color;
153            }
154            internal set
155            {
156                _color=value;
157            }
158        }
159        const string schemePath = "d:scheme/@val";
160        string _scheme="";
161        /// <summary>
162        /// Font Scheme
163        /// </summary>
164        public string Scheme
165        {
166            get
167            {
168                return _scheme;
169            }
170            private set
171            {
172                _scheme=value;
173            }
174        }
175        const string boldPath = "d:b";
176        bool _bold;
177        /// <summary>
178        /// If the font is bold
179        /// </summary>
180        public bool Bold
181        {
182            get
183            {
184                return _bold;
185            }
186            set
187            {
188                _bold=value;
189            }
190        }
191        const string italicPath = "d:i";
192        bool _italic;
193        /// <summary>
194        /// If the font is italic
195        /// </summary>
196        public bool Italic
197        {
198            get
199            {
200                return _italic;
201            }
202            set
203            {
204                _italic=value;
205            }
206        }
207        const string strikePath = "d:strike";
208        bool _strike;
209        /// <summary>
210        /// If the font is striked out
211        /// </summary>
212        public bool Strike
213        {
214            get
215            {
216                return _strike;
217            }
218            set
219            {
220                _strike=value;
221            }
222        }
223        const string underLinedPath = "d:u";
224        /// <summary>
225        /// If the font is underlined.
226        /// When set to true a the text is underlined with a single line
227        /// </summary>
228        public bool UnderLine
229        {
230            get
231            {
232                return UnderLineType!=ExcelUnderLineType.None;
233            }
234            set
235            {
236                _underlineType=value ? ExcelUnderLineType.Single : ExcelUnderLineType.None;
237            }
238        }
239        ExcelUnderLineType _underlineType;
240        /// <summary>
241        /// If the font is underlined
242        /// </summary>
243        public ExcelUnderLineType UnderLineType
244        {
245            get
246            {
247                return _underlineType;
248            }
249            set
250            {
251                _underlineType = value;
252            }
253        }
254        const string verticalAlignPath = "d:vertAlign/@val";
255        string _verticalAlign;
256        /// <summary>
257        /// Vertical aligned
258        /// </summary>
259        public string VerticalAlign
260        {
261            get
262            {
263                return _verticalAlign;
264            }
265            set
266            {
267                _verticalAlign=value;
268            }
269        }
270        public void SetFromFont(System.Drawing.Font Font)
271        {
272            Name=Font.Name;
273            //Family=fnt.FontFamily.;
274            Size=(int)Font.Size;
275            Strike=Font.Strikeout;
276            Bold = Font.Bold;
277            UnderLine=Font.Underline;
278            Italic=Font.Italic;
279        }
280        internal ExcelFontXml Copy()
281        {
282            ExcelFontXml newFont = new ExcelFontXml(NameSpaceManager);
283            newFont.Name = _name;
284            newFont.Size = _size;
285            newFont.Family = _family;
286            newFont.Scheme = _scheme;
287            newFont.Bold = _bold;
288            newFont.Italic = _italic;
289            newFont.UnderLineType = _underlineType;
290            newFont.Strike = _strike;
291            newFont.VerticalAlign = _verticalAlign;
292            newFont.Color = Color.Copy();
293            return newFont;
294        }
295
296        internal override XmlNode CreateXmlNode(XmlNode topElement)
297        {
298            TopNode = topElement;
299
300            if (_bold) CreateNode(boldPath); else DeleteAllNode(boldPath);
301            if (_italic) CreateNode(italicPath); else DeleteAllNode(italicPath);
302            if (_strike) CreateNode(strikePath); else DeleteAllNode(strikePath);
303           
304            if (_underlineType == ExcelUnderLineType.None)
305            {
306                DeleteAllNode(underLinedPath);
307            }
308            else if(_underlineType==ExcelUnderLineType.Single)
309            {
310                CreateNode(underLinedPath);
311            }
312            else
313            {
314                var v=_underlineType.ToString();
315                SetXmlNodeString(underLinedPath + "/@val", v.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + v.Substring(1));
316            }
317
318            if (_verticalAlign!="") SetXmlNodeString(verticalAlignPath, _verticalAlign.ToString());
319            if(_size>0) SetXmlNodeString(sizePath, _size.ToString(System.Globalization.CultureInfo.InvariantCulture));
320            if (_color.Exists)
321            {
322                CreateNode(_colorPath);
323                TopNode.AppendChild(_color.CreateXmlNode(TopNode.SelectSingleNode(_colorPath, NameSpaceManager)));
324            }
325            if(!string.IsNullOrEmpty(_name)) SetXmlNodeString(namePath, _name);
326            if(_family>int.MinValue) SetXmlNodeString(familyPath, _family.ToString());
327            if (_scheme != "") SetXmlNodeString(schemePath, _scheme.ToString());
328
329            return TopNode;
330        }
331    }
332}
Note: See TracBrowser for help on using the repository browser.