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 | namespace OfficeOpenXml.Style.XmlAccess
|
---|
37 | {
|
---|
38 | /// <summary>
|
---|
39 | /// Xml access class for fonts
|
---|
40 | /// </summary>
|
---|
41 | public sealed class ExcelFontXml : StyleXmlHelper
|
---|
42 | {
|
---|
43 | internal ExcelFontXml(XmlNamespaceManager nameSpaceManager)
|
---|
44 | : base(nameSpaceManager)
|
---|
45 | {
|
---|
46 | _name = "";
|
---|
47 | _size = 0;
|
---|
48 | _family = int.MinValue;
|
---|
49 | _scheme = "";
|
---|
50 | _color = _color = new ExcelColorXml(NameSpaceManager);
|
---|
51 | _bold = false;
|
---|
52 | _italic = false;
|
---|
53 | _strike = false;
|
---|
54 | _underlineType = ExcelUnderLineType.None ;
|
---|
55 | _verticalAlign = "";
|
---|
56 | }
|
---|
57 | internal ExcelFontXml(XmlNamespaceManager nsm, XmlNode topNode) :
|
---|
58 | base(nsm, topNode)
|
---|
59 | {
|
---|
60 | _name = GetXmlNodeString(namePath);
|
---|
61 | _size = (float)GetXmlNodeDecimal(sizePath);
|
---|
62 | _family = GetXmlNodeInt(familyPath);
|
---|
63 | _scheme = GetXmlNodeString(schemePath);
|
---|
64 | _color = new ExcelColorXml(nsm, topNode.SelectSingleNode(_colorPath, nsm));
|
---|
65 | _bold = (topNode.SelectSingleNode(boldPath, NameSpaceManager) != null);
|
---|
66 | _italic = (topNode.SelectSingleNode(italicPath, NameSpaceManager) != null);
|
---|
67 | _strike = (topNode.SelectSingleNode(strikePath, NameSpaceManager) != null);
|
---|
68 | _verticalAlign = GetXmlNodeString(verticalAlignPath);
|
---|
69 | if (topNode.SelectSingleNode(underLinedPath, NameSpaceManager) != null)
|
---|
70 | {
|
---|
71 | string ut = GetXmlNodeString(underLinedPath + "/@val");
|
---|
72 | if (ut == "")
|
---|
73 | {
|
---|
74 | _underlineType = ExcelUnderLineType.Single;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | _underlineType = (ExcelUnderLineType)Enum.Parse(typeof(ExcelUnderLineType), ut, true);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | _underlineType = ExcelUnderLineType.None;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | internal override string Id
|
---|
87 | {
|
---|
88 | get
|
---|
89 | {
|
---|
90 | return Name + "|" + Size + "|" + Family + "|" + Color.Id + "|" + Scheme + "|" + Bold.ToString() + "|" + Italic.ToString() + "|" + Strike.ToString() + "|" + VerticalAlign + "|" + UnderLineType.ToString();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | const string namePath = "d:name/@val";
|
---|
94 | string _name;
|
---|
95 | /// <summary>
|
---|
96 | /// The name of the font
|
---|
97 | /// </summary>
|
---|
98 | public string Name
|
---|
99 | {
|
---|
100 | get
|
---|
101 | {
|
---|
102 | return _name;
|
---|
103 | }
|
---|
104 | set
|
---|
105 | {
|
---|
106 | Scheme = ""; //Reset schema to avoid corrupt file if unsupported font is selected.
|
---|
107 | _name = value;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | const string sizePath = "d:sz/@val";
|
---|
111 | float _size;
|
---|
112 | /// <summary>
|
---|
113 | /// Font size
|
---|
114 | /// </summary>
|
---|
115 | public float Size
|
---|
116 | {
|
---|
117 | get
|
---|
118 | {
|
---|
119 | return _size;
|
---|
120 | }
|
---|
121 | set
|
---|
122 | {
|
---|
123 | _size = value;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | const string familyPath = "d:family/@val";
|
---|
127 | int _family;
|
---|
128 | /// <summary>
|
---|
129 | /// Font family
|
---|
130 | /// </summary>
|
---|
131 | public int Family
|
---|
132 | {
|
---|
133 | get
|
---|
134 | {
|
---|
135 | return _family;
|
---|
136 | }
|
---|
137 | set
|
---|
138 | {
|
---|
139 | _family=value;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | ExcelColorXml _color = null;
|
---|
143 | const string _colorPath = "d:color";
|
---|
144 | /// <summary>
|
---|
145 | /// Text color
|
---|
146 | /// </summary>
|
---|
147 | public ExcelColorXml Color
|
---|
148 | {
|
---|
149 | get
|
---|
150 | {
|
---|
151 | return _color;
|
---|
152 | }
|
---|
153 | internal set
|
---|
154 | {
|
---|
155 | _color=value;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | const string schemePath = "d:scheme/@val";
|
---|
159 | string _scheme="";
|
---|
160 | /// <summary>
|
---|
161 | /// Font Scheme
|
---|
162 | /// </summary>
|
---|
163 | public string Scheme
|
---|
164 | {
|
---|
165 | get
|
---|
166 | {
|
---|
167 | return _scheme;
|
---|
168 | }
|
---|
169 | private set
|
---|
170 | {
|
---|
171 | _scheme=value;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | const string boldPath = "d:b";
|
---|
175 | bool _bold;
|
---|
176 | /// <summary>
|
---|
177 | /// If the font is bold
|
---|
178 | /// </summary>
|
---|
179 | public bool Bold
|
---|
180 | {
|
---|
181 | get
|
---|
182 | {
|
---|
183 | return _bold;
|
---|
184 | }
|
---|
185 | set
|
---|
186 | {
|
---|
187 | _bold=value;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | const string italicPath = "d:i";
|
---|
191 | bool _italic;
|
---|
192 | /// <summary>
|
---|
193 | /// If the font is italic
|
---|
194 | /// </summary>
|
---|
195 | public bool Italic
|
---|
196 | {
|
---|
197 | get
|
---|
198 | {
|
---|
199 | return _italic;
|
---|
200 | }
|
---|
201 | set
|
---|
202 | {
|
---|
203 | _italic=value;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | const string strikePath = "d:strike";
|
---|
207 | bool _strike;
|
---|
208 | /// <summary>
|
---|
209 | /// If the font is striked out
|
---|
210 | /// </summary>
|
---|
211 | public bool Strike
|
---|
212 | {
|
---|
213 | get
|
---|
214 | {
|
---|
215 | return _strike;
|
---|
216 | }
|
---|
217 | set
|
---|
218 | {
|
---|
219 | _strike=value;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | const string underLinedPath = "d:u";
|
---|
223 | /// <summary>
|
---|
224 | /// If the font is underlined.
|
---|
225 | /// When set to true a the text is underlined with a single line
|
---|
226 | /// </summary>
|
---|
227 | public bool UnderLine
|
---|
228 | {
|
---|
229 | get
|
---|
230 | {
|
---|
231 | return UnderLineType!=ExcelUnderLineType.None;
|
---|
232 | }
|
---|
233 | set
|
---|
234 | {
|
---|
235 | _underlineType=value ? ExcelUnderLineType.Single : ExcelUnderLineType.None;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | ExcelUnderLineType _underlineType;
|
---|
239 | /// <summary>
|
---|
240 | /// If the font is underlined
|
---|
241 | /// </summary>
|
---|
242 | public ExcelUnderLineType UnderLineType
|
---|
243 | {
|
---|
244 | get
|
---|
245 | {
|
---|
246 | return _underlineType;
|
---|
247 | }
|
---|
248 | set
|
---|
249 | {
|
---|
250 | _underlineType = value;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | const string verticalAlignPath = "d:vertAlign/@val";
|
---|
254 | string _verticalAlign;
|
---|
255 | /// <summary>
|
---|
256 | /// Vertical aligned
|
---|
257 | /// </summary>
|
---|
258 | public string VerticalAlign
|
---|
259 | {
|
---|
260 | get
|
---|
261 | {
|
---|
262 | return _verticalAlign;
|
---|
263 | }
|
---|
264 | set
|
---|
265 | {
|
---|
266 | _verticalAlign=value;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | public void SetFromFont(System.Drawing.Font Font)
|
---|
270 | {
|
---|
271 | Name=Font.Name;
|
---|
272 | //Family=fnt.FontFamily.;
|
---|
273 | Size=(int)Font.Size;
|
---|
274 | Strike=Font.Strikeout;
|
---|
275 | Bold = Font.Bold;
|
---|
276 | UnderLine=Font.Underline;
|
---|
277 | Italic=Font.Italic;
|
---|
278 | }
|
---|
279 | internal ExcelFontXml Copy()
|
---|
280 | {
|
---|
281 | ExcelFontXml newFont = new ExcelFontXml(NameSpaceManager);
|
---|
282 | newFont.Name = Name;
|
---|
283 | newFont.Size = Size;
|
---|
284 | newFont.Family = Family;
|
---|
285 | newFont.Scheme = Scheme;
|
---|
286 | newFont.Bold = Bold;
|
---|
287 | newFont.Italic = Italic;
|
---|
288 | newFont.UnderLineType = UnderLineType;
|
---|
289 | newFont.Strike = Strike;
|
---|
290 | newFont.VerticalAlign = VerticalAlign;
|
---|
291 | newFont.Color = Color.Copy();
|
---|
292 | return newFont;
|
---|
293 | }
|
---|
294 |
|
---|
295 | internal override XmlNode CreateXmlNode(XmlNode topElement)
|
---|
296 | {
|
---|
297 | TopNode = topElement;
|
---|
298 |
|
---|
299 | if (_bold) CreateNode(boldPath); else DeleteAllNode(boldPath);
|
---|
300 | if (_italic) CreateNode(italicPath); else DeleteAllNode(italicPath);
|
---|
301 | if (_strike) CreateNode(strikePath); else DeleteAllNode(strikePath);
|
---|
302 |
|
---|
303 | if (_underlineType == ExcelUnderLineType.None)
|
---|
304 | {
|
---|
305 | DeleteAllNode(underLinedPath);
|
---|
306 | }
|
---|
307 | else if(_underlineType==ExcelUnderLineType.Single)
|
---|
308 | {
|
---|
309 | CreateNode(underLinedPath);
|
---|
310 | }
|
---|
311 | else
|
---|
312 | {
|
---|
313 | var v=_underlineType.ToString();
|
---|
314 | SetXmlNodeString(underLinedPath + "/@val", v.Substring(0, 1).ToLower() + v.Substring(1));
|
---|
315 | }
|
---|
316 |
|
---|
317 | if (_verticalAlign!="") SetXmlNodeString(verticalAlignPath, _verticalAlign.ToString());
|
---|
318 | SetXmlNodeString(sizePath, _size.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
---|
319 | if (_color.Exists)
|
---|
320 | {
|
---|
321 | CreateNode(_colorPath);
|
---|
322 | TopNode.AppendChild(_color.CreateXmlNode(TopNode.SelectSingleNode(_colorPath, NameSpaceManager)));
|
---|
323 | }
|
---|
324 | SetXmlNodeString(namePath, _name);
|
---|
325 | if(_family>int.MinValue) SetXmlNodeString(familyPath, _family.ToString());
|
---|
326 | if (_scheme != "") SetXmlNodeString(schemePath, _scheme.ToString());
|
---|
327 |
|
---|
328 | return TopNode;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|