| /******************************************************************************* |
| * You may amend and distribute as you like, but don't remove this header! |
| * |
| * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets. |
| * See http://www.codeplex.com/EPPlus for details. |
| * |
| * Copyright (C) 2011 Jan Källman |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| * See the GNU Lesser General Public License for more details. |
| * |
| * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php |
| * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html |
| * |
| * All code and executables are provided "as is" with no warranty either express or implied. |
| * The author accepts no liability for any damage or loss of business that this product may cause. |
| * |
| * Code change notes: |
| * |
| * Author Change Date |
| * ****************************************************************************** |
| * Jan Källman Initial Release 2009-10-01 |
| * Jan Källman License changed GPL-->LGPL 2011-12-16 |
| *******************************************************************************/ |
| |
| using System; |
| using System.Globalization; |
| using System.Xml; |
| |
| namespace OfficeOpenXml.Style.XmlAccess; |
| |
| /// <summary> |
| /// Xml access class for fonts |
| /// </summary> |
| public sealed class ExcelFontXml : StyleXmlHelper { |
| internal ExcelFontXml(XmlNamespaceManager nameSpaceManager) |
| : base(nameSpaceManager) { |
| _name = ""; |
| _size = 0; |
| _family = int.MinValue; |
| _scheme = ""; |
| _color = _color = new(NameSpaceManager); |
| _bold = false; |
| _italic = false; |
| _strike = false; |
| _underlineType = ExcelUnderLineType.None; |
| _verticalAlign = ""; |
| } |
| |
| internal ExcelFontXml(XmlNamespaceManager nsm, XmlNode topNode) |
| : base(nsm, topNode) { |
| _name = GetXmlNodeString(_namePath); |
| _size = (float)GetXmlNodeDecimal(_sizePath); |
| _family = GetXmlNodeIntNull(_familyPath) ?? int.MinValue; |
| _scheme = GetXmlNodeString(_schemePath); |
| _color = new(nsm, topNode.SelectSingleNode(_colorPath, nsm)); |
| _bold = GetBoolValue(topNode, _boldPath); |
| _italic = GetBoolValue(topNode, _italicPath); |
| _strike = GetBoolValue(topNode, _strikePath); |
| _verticalAlign = GetXmlNodeString(_verticalAlignPath); |
| if (topNode.SelectSingleNode(_underLinedPath, NameSpaceManager) != null) { |
| string ut = GetXmlNodeString(_underLinedPath + "/@val"); |
| if (ut == "") { |
| _underlineType = ExcelUnderLineType.Single; |
| } else { |
| if (!Enum.TryParse(ut, true, out _underlineType)) { |
| _underlineType = ExcelUnderLineType.Single; |
| } |
| } |
| } else { |
| _underlineType = ExcelUnderLineType.None; |
| } |
| } |
| |
| internal override string Id => |
| Name |
| + "|" |
| + Size |
| + "|" |
| + Family |
| + "|" |
| + Color.Id |
| + "|" |
| + Scheme |
| + "|" |
| + Bold |
| + "|" |
| + Italic |
| + "|" |
| + Strike |
| + "|" |
| + VerticalAlign |
| + "|" |
| + UnderLineType; |
| |
| private const string _namePath = "d:name/@val"; |
| private string _name; |
| |
| /// <summary> |
| /// The name of the font |
| /// </summary> |
| public string Name { |
| get => _name; |
| set { |
| Scheme = ""; //Reset schema to avoid corrupt file if unsupported font is selected. |
| _name = value; |
| } |
| } |
| |
| private const string _sizePath = "d:sz/@val"; |
| private float _size; |
| |
| /// <summary> |
| /// Font size |
| /// </summary> |
| public float Size { |
| get => _size; |
| set => _size = value; |
| } |
| |
| private const string _familyPath = "d:family/@val"; |
| private int _family; |
| |
| /// <summary> |
| /// Font family |
| /// </summary> |
| public int Family { |
| get => (_family == int.MinValue ? 0 : _family); |
| set => _family = value; |
| } |
| |
| private ExcelColorXml _color; |
| private const string _colorPath = "d:color"; |
| |
| /// <summary> |
| /// Text color |
| /// </summary> |
| public ExcelColorXml Color { |
| get => _color; |
| internal set => _color = value; |
| } |
| |
| private const string _schemePath = "d:scheme/@val"; |
| private string _scheme = ""; |
| |
| /// <summary> |
| /// Font Scheme |
| /// </summary> |
| public string Scheme { |
| get => _scheme; |
| private set => _scheme = value; |
| } |
| |
| private const string _boldPath = "d:b"; |
| private bool _bold; |
| |
| /// <summary> |
| /// If the font is bold |
| /// </summary> |
| public bool Bold { |
| get => _bold; |
| set => _bold = value; |
| } |
| |
| private const string _italicPath = "d:i"; |
| private bool _italic; |
| |
| /// <summary> |
| /// If the font is italic |
| /// </summary> |
| public bool Italic { |
| get => _italic; |
| set => _italic = value; |
| } |
| |
| private const string _strikePath = "d:strike"; |
| private bool _strike; |
| |
| /// <summary> |
| /// If the font is striked out |
| /// </summary> |
| public bool Strike { |
| get => _strike; |
| set => _strike = value; |
| } |
| |
| private const string _underLinedPath = "d:u"; |
| |
| /// <summary> |
| /// If the font is underlined. |
| /// When set to true a the text is underlined with a single line |
| /// </summary> |
| public bool UnderLine { |
| get => UnderLineType != ExcelUnderLineType.None; |
| set => _underlineType = value ? ExcelUnderLineType.Single : ExcelUnderLineType.None; |
| } |
| |
| private ExcelUnderLineType _underlineType; |
| |
| /// <summary> |
| /// If the font is underlined |
| /// </summary> |
| public ExcelUnderLineType UnderLineType { |
| get => _underlineType; |
| set => _underlineType = value; |
| } |
| |
| private const string _verticalAlignPath = "d:vertAlign/@val"; |
| private string _verticalAlign; |
| |
| /// <summary> |
| /// Vertical aligned |
| /// </summary> |
| public string VerticalAlign { |
| get => _verticalAlign; |
| set => _verticalAlign = value; |
| } |
| |
| internal ExcelFontXml Copy() { |
| ExcelFontXml newFont = new ExcelFontXml(NameSpaceManager); |
| newFont.Name = _name; |
| newFont.Size = _size; |
| newFont.Family = _family; |
| newFont.Scheme = _scheme; |
| newFont.Bold = _bold; |
| newFont.Italic = _italic; |
| newFont.UnderLineType = _underlineType; |
| newFont.Strike = _strike; |
| newFont.VerticalAlign = _verticalAlign; |
| newFont.Color = Color.Copy(); |
| return newFont; |
| } |
| |
| internal override XmlNode CreateXmlNode(XmlNode topElement) { |
| TopNode = topElement; |
| |
| if (_bold) { |
| CreateNode(_boldPath); |
| } else { |
| DeleteAllNode(_boldPath); |
| } |
| if (_italic) { |
| CreateNode(_italicPath); |
| } else { |
| DeleteAllNode(_italicPath); |
| } |
| if (_strike) { |
| CreateNode(_strikePath); |
| } else { |
| DeleteAllNode(_strikePath); |
| } |
| |
| if (_underlineType == ExcelUnderLineType.None) { |
| DeleteAllNode(_underLinedPath); |
| } else if (_underlineType == ExcelUnderLineType.Single) { |
| CreateNode(_underLinedPath); |
| } else { |
| var v = _underlineType.ToString(); |
| SetXmlNodeString( |
| _underLinedPath + "/@val", |
| v.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + v.Substring(1)); |
| } |
| |
| if (_verticalAlign != "") { |
| SetXmlNodeString(_verticalAlignPath, _verticalAlign); |
| } |
| if (_size > 0) { |
| SetXmlNodeString(_sizePath, _size.ToString(CultureInfo.InvariantCulture)); |
| } |
| if (_color.Exists) { |
| CreateNode(_colorPath); |
| TopNode.AppendChild( |
| _color.CreateXmlNode(TopNode.SelectSingleNode(_colorPath, NameSpaceManager))); |
| } |
| if (!string.IsNullOrEmpty(_name)) { |
| SetXmlNodeString(_namePath, _name); |
| } |
| if (_family > int.MinValue) { |
| SetXmlNodeString(_familyPath, _family.ToString()); |
| } |
| if (_scheme != "") { |
| SetXmlNodeString(_schemePath, _scheme); |
| } |
| |
| return TopNode; |
| } |
| } |