diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs index 989bd27..5d62dae 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs @@ -5,7 +5,6 @@ public interface IXLRichString: IXLFontBase, IEquatable { String Text { get; } - IXLRichString Apply(IXLFontBase font); IXLRichString SetBold(); IXLRichString SetBold(Boolean value); IXLRichString SetItalic(); IXLRichString SetItalic(Boolean value); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLPhonetics.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLPhonetics.cs index b120bb8..2b3770f 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLPhonetics.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLPhonetics.cs @@ -6,15 +6,24 @@ { internal class XLPhonetics : IXLPhonetics { - private List phonetics = new List(); + private readonly List _phonetics = new List(); - IXLFontBase m_defaultFont; + readonly IXLFontBase _defaultFont; public XLPhonetics(IXLFontBase defaultFont) { - m_defaultFont = defaultFont; + _defaultFont = defaultFont; Type = XLPhoneticType.FullWidthKatakana; Alignment = XLPhoneticAlignment.Left; - ClearFont(); + this.CopyFont(_defaultFont); + } + + public XLPhonetics(IXLPhonetics defaultPhonetics, IXLFontBase defaultFont) + { + _defaultFont = defaultFont; + Type = defaultPhonetics.Type; + Alignment = defaultPhonetics.Alignment; + + this.CopyFont(defaultPhonetics); } public Boolean Bold { get; set; } @@ -41,29 +50,21 @@ public IXLPhonetics Add(String text, Int32 start, Int32 end) { - phonetics.Add(new XLPhonetic(text, start, end)); + _phonetics.Add(new XLPhonetic(text, start, end)); return this; } public IXLPhonetics ClearText() { - phonetics.Clear(); + _phonetics.Clear(); return this; } public IXLPhonetics ClearFont() { - Bold = m_defaultFont.Bold; - Italic = m_defaultFont.Italic; - Underline = m_defaultFont.Underline; - Strikethrough = m_defaultFont.Strikethrough; - VerticalAlignment = m_defaultFont.VerticalAlignment; - Shadow = m_defaultFont.Shadow; - FontSize = m_defaultFont.FontSize; - FontColor = new XLColor(m_defaultFont.FontColor); - FontName = m_defaultFont.FontName; - FontFamilyNumbering = m_defaultFont.FontFamilyNumbering; + this.CopyFont(_defaultFont); return this; } - public Int32 Count { get { return phonetics.Count; } } + + public Int32 Count { get { return _phonetics.Count; } } public XLPhoneticAlignment Alignment { get; set; } public XLPhoneticType Type { get; set; } @@ -73,7 +74,7 @@ public IEnumerator GetEnumerator() { - return phonetics.GetEnumerator(); + return _phonetics.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() @@ -86,10 +87,10 @@ if (other == null) return false; - Int32 phoneticsCount = phonetics.Count; + Int32 phoneticsCount = _phonetics.Count; for (Int32 i = 0; i < phoneticsCount; i++) { - if (!phonetics[i].Equals(other.ElementAt(i))) + if (!_phonetics[i].Equals(other.ElementAt(i))) return false; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs index 74aa26a..99ad9b4 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs @@ -8,24 +8,11 @@ public XLRichString(String text, IXLFontBase font) { Text = text; - Apply(font); + this.CopyFont(font); } public String Text { get; private set; } - public IXLRichString Apply(IXLFontBase font) - { - Bold = font.Bold; - Italic = font.Italic; - Underline = font.Underline; - Strikethrough = font.Strikethrough; - VerticalAlignment = font.VerticalAlignment; - Shadow = font.Shadow; - FontSize = font.FontSize; - FontColor = new XLColor(font.FontColor); - FontName = font.FontName; - FontFamilyNumbering = font.FontFamilyNumbering; - return this; - } + public Boolean Bold { get; set; } public Boolean Italic { get; set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichText.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichText.cs index ecb7c0b..2e7287a 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichText.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichText.cs @@ -7,12 +7,23 @@ { internal class XLRichText: IXLRichText { - List richTexts = new List(); + List _richTexts = new List(); - IXLFontBase m_defaultFont; + readonly IXLFontBase _defaultFont; public XLRichText(IXLFontBase defaultFont) { - m_defaultFont = defaultFont; + _defaultFont = defaultFont; + } + + public XLRichText(IXLRichText defaultRichText, IXLFontBase defaultFont) + { + _defaultFont = defaultFont; + foreach (var rt in defaultRichText) + AddText(rt.Text, rt); + if (defaultRichText.HasPhonetics) + { + phonetics = new XLPhonetics(defaultRichText.Phonetics, defaultFont); + } } public XLRichText(String text, IXLFontBase defaultFont) @@ -21,7 +32,7 @@ AddText(text); } - public Int32 Count { get { return richTexts.Count; } } + public Int32 Count { get { return _richTexts.Count; } } private Int32 m_length = 0; public Int32 Length { @@ -32,7 +43,7 @@ } public IXLRichString AddText(String text) { - return AddText(text, m_defaultFont); + return AddText(text, _defaultFont); } public IXLRichString AddText(String text, IXLFontBase font) { @@ -42,13 +53,13 @@ public IXLRichString AddText(IXLRichString richText) { - richTexts.Add(richText); + _richTexts.Add(richText); m_length += richText.Text.Length; return richText; } public IXLRichText ClearText() { - richTexts.Clear(); + _richTexts.Clear(); m_length = 0; return this; } @@ -62,8 +73,8 @@ public override string ToString() { - var sb = new StringBuilder(richTexts.Count); - richTexts.ForEach(rt => sb.Append(rt.Text)); + var sb = new StringBuilder(_richTexts.Count); + _richTexts.ForEach(rt => sb.Append(rt.Text)); return sb.ToString(); } @@ -77,10 +88,10 @@ throw new IndexOutOfRangeException("Index and length must refer to a location within the string."); List newRichTexts = new List(); - XLRichText retVal = new XLRichText(m_defaultFont); + XLRichText retVal = new XLRichText(_defaultFont); Int32 lastPosition = 0; - foreach (var rt in richTexts) + foreach (var rt in _richTexts) { if (lastPosition >= index + 1 + length) // We already have what we need { @@ -112,13 +123,13 @@ } lastPosition += rt.Text.Length; } - richTexts = newRichTexts; + _richTexts = newRichTexts; return retVal; } public IEnumerator GetEnumerator() { - return richTexts.GetEnumerator(); + return _richTexts.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() @@ -126,16 +137,16 @@ return GetEnumerator(); } - public Boolean Bold { set { richTexts.ForEach(rt => rt.Bold = value); } } - public Boolean Italic { set { richTexts.ForEach(rt => rt.Italic = value); } } - public XLFontUnderlineValues Underline { set { richTexts.ForEach(rt => rt.Underline = value); } } - public Boolean Strikethrough { set { richTexts.ForEach(rt => rt.Strikethrough = value); } } - public XLFontVerticalTextAlignmentValues VerticalAlignment { set { richTexts.ForEach(rt => rt.VerticalAlignment = value); } } - public Boolean Shadow { set { richTexts.ForEach(rt => rt.Shadow = value); } } - public Double FontSize { set { richTexts.ForEach(rt => rt.FontSize = value); } } - public IXLColor FontColor { set { richTexts.ForEach(rt => rt.FontColor = value); } } - public String FontName { set { richTexts.ForEach(rt => rt.FontName = value); } } - public XLFontFamilyNumberingValues FontFamilyNumbering { set { richTexts.ForEach(rt => rt.FontFamilyNumbering = value); } } + public Boolean Bold { set { _richTexts.ForEach(rt => rt.Bold = value); } } + public Boolean Italic { set { _richTexts.ForEach(rt => rt.Italic = value); } } + public XLFontUnderlineValues Underline { set { _richTexts.ForEach(rt => rt.Underline = value); } } + public Boolean Strikethrough { set { _richTexts.ForEach(rt => rt.Strikethrough = value); } } + public XLFontVerticalTextAlignmentValues VerticalAlignment { set { _richTexts.ForEach(rt => rt.VerticalAlignment = value); } } + public Boolean Shadow { set { _richTexts.ForEach(rt => rt.Shadow = value); } } + public Double FontSize { set { _richTexts.ForEach(rt => rt.FontSize = value); } } + public IXLColor FontColor { set { _richTexts.ForEach(rt => rt.FontColor = value); } } + public String FontName { set { _richTexts.ForEach(rt => rt.FontName = value); } } + public XLFontFamilyNumberingValues FontFamilyNumbering { set { _richTexts.ForEach(rt => rt.FontFamilyNumbering = value); } } public IXLRichText SetBold() { Bold = true; return this; } public IXLRichText SetBold(Boolean value) { Bold = value; return this; } public IXLRichText SetItalic() { Italic = true; return this; } public IXLRichText SetItalic(Boolean value) { Italic = value; return this; } @@ -156,7 +167,7 @@ for (Int32 i = 0; i < count; i++) { - if (richTexts.ElementAt(i) != other.ElementAt(i)) + if (_richTexts.ElementAt(i) != other.ElementAt(i)) return false; } @@ -174,7 +185,7 @@ get { if (phonetics == null) - phonetics = new XLPhonetics(m_defaultFont); + phonetics = new XLPhonetics(_defaultFont); return phonetics; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Extensions.cs b/ClosedXML/ClosedXML/ClosedXML/Extensions.cs index dcaae48..a21b25b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Extensions.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Extensions.cs @@ -147,9 +147,9 @@ if (StringExtensions.IsNullOrWhiteSpace(text)) return 0; - System.Drawing.Font stringFont = new System.Drawing.Font(font.FontName, (float)font.FontSize); + var stringFont = new Font(font.FontName, (float)font.FontSize); - Size textSize = TextRenderer.MeasureText(text, stringFont); + var textSize = TextRenderer.MeasureText(text, stringFont); double width = (double)(((textSize.Width / (double)7) * 256) - (128 / 7)) / 256; width = (double)decimal.Round((decimal)width + 0.2M, 2); @@ -158,11 +158,24 @@ public static Double GetHeight(this IXLFontBase font) { - System.Drawing.Font stringFont = new System.Drawing.Font(font.FontName, (float)font.FontSize); - Size textSize = TextRenderer.MeasureText("X", stringFont); + var stringFont = new Font(font.FontName, (float)font.FontSize); + var textSize = TextRenderer.MeasureText("X", stringFont); return (double)textSize.Height * 0.85; } + public static void CopyFont(this IXLFontBase font, IXLFontBase sourceFont) + { + font.Bold = sourceFont.Bold; + font.Italic = sourceFont.Italic; + font.Underline = sourceFont.Underline; + font.Strikethrough = sourceFont.Strikethrough; + font.VerticalAlignment = sourceFont.VerticalAlignment; + font.Shadow = sourceFont.Shadow; + font.FontSize = sourceFont.FontSize; + font.FontColor = new XLColor(sourceFont.FontColor); + font.FontName = sourceFont.FontName; + font.FontFamilyNumbering = sourceFont.FontFamilyNumbering; + } } }