You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.6 KiB
C#

// <copyright file="RtfElement.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UCDraw.RTF
{
public class RtfElement
{
public static bool operator ==(RtfElement a, RtfElement b)
{
// If both are null, or both are same instance, return true.
if (object.ReferenceEquals(a, b))
{
return true;
}
return a.Equals(b);
}
public static bool operator !=(RtfElement a, RtfElement b)
{
return !(a == b);
}
}
/// <summary>
/// 1abcde
/// </summary>
public class RtfValueElement : RtfElement
{
public static bool operator ==(RtfValueElement a, RtfValueElement b)
{
// If both are null, or both are same instance, return true.
return a.Equals(b);
}
public static bool operator !=(RtfValueElement a, RtfValueElement b)
{
return !(a == b);
}
public RtfValueElement() { }
public RtfValueElement(string data)
{
this.Data = data;
}
public string Data { get; set; }
public override bool Equals(System.Object obj)
{
// If both are null, or both are same instance, return true.
if (object.ReferenceEquals(this, obj))
{
return true;
}
// If parameter cannot be cast to RtfValueElement return false:
RtfValueElement p = obj as RtfValueElement;
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return Data.Equals(p.Data);
}
/// <inheritdoc/>
public override string ToString()
{
return $"{Data}";
}
}
/// <summary>
/// \ansi
/// </summary>
public class RtfControlElement : RtfElement
{
public override bool Equals(System.Object obj)
{
// If both are null, or both are same instance, return true.
if (object.ReferenceEquals(this, obj))
{
return true;
}
// If parameter cannot be cast to RtfValueElement return false:
RtfControlElement p = obj as RtfControlElement;
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return ControlWord.Equals(p.ControlWord);
}
public RtfControlElement() { }
public RtfControlElement(string controlWord)
{
this.ControlWord = controlWord;
}
public string ControlWord { get; set; }
/// <inheritdoc/>
public override string ToString()
{
return $"\\{ControlWord}";
}
}
/// <summary>
/// \rtf1
/// </summary>
public class RtfControlNumElement : RtfControlElement
{
public override bool Equals(System.Object obj)
{
// If both are null, or both are same instance, return true.
if (object.ReferenceEquals(this, obj))
{
return true;
}
// If parameter cannot be cast to RtfValueElement return false:
RtfControlNumElement p = obj as RtfControlNumElement;
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return ControlWord.Equals(p.ControlWord) && Number == p.Number;
}
public RtfControlNumElement() { }
public RtfControlNumElement(string control, int number)
{
this.ControlWord = control;
this.Number = number;
}
public int Number { get; set; }
/// <inheritdoc/>
public override string ToString()
{
return $"\\{ControlWord}{Number}";
}
}
/// <summary>
/// \'b1 \'a8 \'cb \'f7 \generator Riched20 10.0.19041
/// </summary>
public class RtfControlDataElement : RtfControlElement
{
public override bool Equals(System.Object obj)
{
// If both are null, or both are same instance, return true.
if (object.ReferenceEquals(this, obj))
{
return true;
}
// If parameter cannot be cast to RtfValueElement return false:
RtfControlDataElement p = obj as RtfControlDataElement;
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return ControlWord.Equals(p.ControlWord) && Data.Equals(p.Data);
}
public RtfControlDataElement() { }
public RtfControlDataElement(string control, string data)
{
this.ControlWord = control;
this.Data = data;
}
public string Data { get; set; }
/// <inheritdoc/>
public override string ToString()
{
return $"\\{ControlWord}{Data}";
}
}
public class RtfControlSplitDataElement : RtfControlDataElement
{
public RtfControlSplitDataElement(string control, string data) : base(control, data) {; }
public override string ToString()
{
return $"\\{ControlWord} {Data}";
}
}
public class RtfWChar : RtfControlDataElement
{
public RtfWChar() { this.ControlWord = "'"; }
public RtfWChar(char[] chars)
{
Chars = chars;
}
public RtfWChar(string chars) : this(chars.ToCharArray())
{
}
private char[] chars;
public char[] Chars
{
get
{
if (chars == null)
{
chars = new char[2];
}
return chars;
}
set
{
if (chars == null)
{
chars = new char[2];
}
chars[0] = value[0];
chars[1] = value[1];
this.ControlWord = "'";
this.Data = $"{chars[0]}{chars[1]}";
}
}
/// <inheritdoc/>
public override string ToString()
{
if (Chars == null || Chars.Length < 2)
{
return string.Empty;
}
return $"\\'{chars[0]}{chars[1]}";
}
}
}