NanoXLSX.Formatting 3.1.0
Loading...
Searching...
No Matches
TextRun.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System.Collections.Generic;
9using NanoXLSX.Styles;
10using NanoXLSX.Utils.Xml;
11
12// TODO: Consider changing this namespace in a future major release.
13namespace NanoXLSX.Extensions
14{
18 public class TextRun
19 {
20 private string text;
21
26 public string Text
27 {
28 get { return text; }
29 set
30 {
31 if (value == null)
32 {
33 throw new Exceptions.FormatException("The text of a text run cannot be null");
34 }
35 text = XmlUtils.SanitizeXmlValue(value); ;
36 }
37 }
38
41 public Font FontStyle { get; set; }
42
49 public TextRun(string text, Font fontStyle = null)
50 {
51 Text = text;
52 FontStyle = fontStyle;
53 }
54
59 public TextRun Copy()
60 {
61 return new TextRun(this.Text, this.FontStyle?.CopyFont());
62 }
63
69 public override bool Equals(object obj)
70 {
71 if (!(obj is TextRun run))
72 return false;
73
74 return text == run.text &&
75 ((FontStyle == null && run.FontStyle == null) ||
76 (FontStyle != null && FontStyle.Equals(run.FontStyle)));
77 }
78
79
84 public override int GetHashCode()
85 {
86 var hashCode = 1049193379;
87 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(text);
88 hashCode = hashCode * -1521134295 + EqualityComparer<Font>.Default.GetHashCode(FontStyle);
89 return hashCode;
90 }
91 }
92}
string Text
Plain text of the run.
Definition TextRun.cs:27
override bool Equals(object obj)
Equals override to compare text runs.
Definition TextRun.cs:69
override int GetHashCode()
HashCode override for text runs.
Definition TextRun.cs:84
TextRun Copy()
Creates a copy of the current text run.
Definition TextRun.cs:59
Font FontStyle
Font style applied to the text run.
Definition TextRun.cs:41
TextRun(string text, Font fontStyle=null)
Constructor to create a text run with optional inline style.
Definition TextRun.cs:49