Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Style/ExcelTextFont.cs @ 13401

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 9.5 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;
37using OfficeOpenXml.Drawing;
38using System.Drawing;
39
40namespace OfficeOpenXml.Style
41{
42    /// <summary>
43    /// Linestyle
44    /// </summary>
45    public enum eUnderLineType
46    {
47        Dash,
48        DashHeavy,
49        DashLong,
50        DashLongHeavy,
51        Double,
52        DotDash,
53        DotDashHeavy,
54        DotDotDash,
55        DotDotDashHeavy,
56        Dotted,
57        DottedHeavy,
58        Heavy,
59        None,
60        Single,
61        Wavy,
62        WavyDbl,
63        WavyHeavy,
64        Words
65    }
66    /// <summary>
67    /// Type of font strike
68    /// </summary>
69    public enum eStrikeType
70    {
71        Double,
72        No,
73        Single
74    }
75    /// <summary>
76    /// Used by Rich-text and Paragraphs.
77    /// </summary>
78    public class ExcelTextFont : XmlHelper
79    {
80        string _path;
81        XmlNode _rootNode;
82        internal ExcelTextFont(XmlNamespaceManager namespaceManager, XmlNode rootNode, string path, string[] schemaNodeOrder)
83            : base(namespaceManager, rootNode)
84        {
85            SchemaNodeOrder = schemaNodeOrder;
86            _rootNode = rootNode;
87            if (path != "")
88            {
89                XmlNode node = rootNode.SelectSingleNode(path, namespaceManager);
90                if (node != null)
91                {
92                    TopNode = node;
93                }
94            }
95            _path = path;
96        }
97        string _fontLatinPath = "a:latin/@typeface";
98        public string LatinFont
99        {
100            get
101            {
102                return GetXmlNodeString(_fontLatinPath);
103            }
104            set
105            {
106                CreateTopNode();
107                SetXmlNodeString(_fontLatinPath, value);
108            }
109        }
110
111        protected internal void CreateTopNode()
112        {
113            if (_path!="" && TopNode == _rootNode)
114            {
115                CreateNode(_path);
116                TopNode = _rootNode.SelectSingleNode(_path, NameSpaceManager);
117            }
118        }
119        string _fontCsPath = "a:cs/@typeface";
120        public string ComplexFont
121        {
122            get
123            {
124                return GetXmlNodeString(_fontCsPath);
125            }
126            set
127            {
128                CreateTopNode();
129                SetXmlNodeString(_fontCsPath, value);
130            }
131        }
132        string _boldPath = "@b";
133        public bool Bold
134        {
135            get
136            {
137                return GetXmlNodeBool(_boldPath);
138            }
139            set
140            {
141                CreateTopNode();
142                SetXmlNodeString(_boldPath, value ? "1" : "0");
143            }
144        }
145        string _underLinePath = "@u";
146        public eUnderLineType UnderLine
147        {
148            get
149            {
150                return TranslateUnderline(GetXmlNodeString(_underLinePath));
151            }
152            set
153            {
154                CreateTopNode();
155                SetXmlNodeString(_underLinePath, TranslateUnderlineText(value));
156            }
157        }
158        string _underLineColorPath = "a:uFill/a:solidFill/a:srgbClr/@val";
159        public Color UnderLineColor
160        {
161            get
162            {
163                string col = GetXmlNodeString(_underLineColorPath);
164                if (col == "")
165                {
166                    return Color.Empty;
167                }
168                else
169                {
170                    return Color.FromArgb(int.Parse(col, System.Globalization.NumberStyles.AllowHexSpecifier));
171                }
172            }
173            set
174            {
175                CreateTopNode();
176                SetXmlNodeString(_underLineColorPath, value.ToArgb().ToString("X").Substring(2, 6));
177            }
178        }
179        string _italicPath = "@i";
180        public bool Italic
181        {
182            get
183            {
184                return GetXmlNodeBool(_italicPath);
185            }
186            set
187            {
188                CreateTopNode();
189                SetXmlNodeString(_italicPath, value ? "1" : "0");
190            }
191        }
192        string _strikePath = "@strike";
193        public eStrikeType Strike
194        {
195            get
196            {
197                return TranslateStrike(GetXmlNodeString(_strikePath));
198            }
199            set
200            {
201                CreateTopNode();
202                SetXmlNodeString(_strikePath, TranslateStrikeText(value));
203            }
204        }
205        string _sizePath = "@sz";
206        public float Size
207        {
208            get
209            {
210                return GetXmlNodeInt(_sizePath) / 100;
211            }
212            set
213            {
214                CreateTopNode();
215                SetXmlNodeString(_sizePath, ((int)(value * 100)).ToString());
216            }
217        }
218        string _colorPath = "a:solidFill/a:srgbClr/@val";
219        public Color Color
220        {
221            get
222            {
223                string col = GetXmlNodeString(_colorPath);
224                if (col == "")
225                {
226                    return Color.Empty;
227                }
228                else
229                {
230                    return Color.FromArgb(int.Parse(col, System.Globalization.NumberStyles.AllowHexSpecifier));
231                }
232            }
233            set
234            {
235                CreateTopNode();
236                SetXmlNodeString(_colorPath, value.ToArgb().ToString("X").Substring(2, 6));
237            }
238        }
239        #region "Translate methods"
240        private eUnderLineType TranslateUnderline(string text)
241        {
242            switch (text)
243            {
244                case "sng":
245                    return eUnderLineType.Single;
246                case "dbl":
247                    return eUnderLineType.Double;
248                case "":
249                    return eUnderLineType.None;
250                default:
251                    return (eUnderLineType)Enum.Parse(typeof(eUnderLineType), text);
252            }
253        }
254        private string TranslateUnderlineText(eUnderLineType value)
255        {
256            switch (value)
257            {
258                case eUnderLineType.Single:
259                    return "sng";
260                case eUnderLineType.Double:
261                    return "dbl";
262                default:
263                    string ret = value.ToString();
264                    return ret.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + ret.Substring(1, ret.Length - 1);
265            }
266        }
267        private eStrikeType TranslateStrike(string text)
268        {
269            switch (text)
270            {
271                case "dblStrike":
272                    return eStrikeType.Double;
273                case "sngStrike":
274                    return eStrikeType.Single;
275                default:
276                    return eStrikeType.No;
277            }
278        }
279        private string TranslateStrikeText(eStrikeType value)
280        {
281            switch (value)
282            {
283                case eStrikeType.Single:
284                    return "sngStrike";
285                case eStrikeType.Double:
286                    return "dblStrike";
287                default:
288                    return "noStrike";
289            }
290        }
291        #endregion
292        /// <summary>
293        /// Set the font style from a font object
294        /// </summary>
295        /// <param name="Font"></param>
296        public void SetFromFont(Font Font)
297        {
298            LatinFont = Font.Name;
299            ComplexFont = Font.Name;
300            Size = Font.Size;
301            if (Font.Bold) Bold = Font.Bold;
302            if (Font.Italic) Italic = Font.Italic;
303            if (Font.Underline) UnderLine = eUnderLineType.Single;
304            if (Font.Strikeout) Strike = eStrikeType.Single;
305        }
306    }
307}
Note: See TracBrowser for help on using the repository browser.