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