/* ====================================================================
Licensed to the Apache Software Foundation (ASF) Under one or more
contributor license agreements. See the NOTICE file distributed with
this work for Additional information regarding copyright ownership.
The ASF licenses this file to You Under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed Under the License is distributed on an "AS Is" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations Under the License.
==================================================================== */
namespace HH.WMS.Utils.NPOI.HSSF.UserModel
{
using System;
using System.IO;
using System.Collections;
using HH.WMS.Utils.NPOI.HSSF.Record;
using HH.WMS.Utils.NPOI.HSSF.Model;
using HH.WMS.Utils.NPOI.Util;
///
/// Rich text Unicode string. These strings can have fonts applied to
/// arbitary parts of the string.
/// @author Glen Stampoultzis (glens at apache.org)
/// @author Jason Height (jheight at apache.org)
///
[Serializable]
public class HSSFRichTextString : IComparable,HH.WMS.Utils.NPOI.SS.UserModel.IRichTextString
{
/** Place holder for indicating that NO_FONT has been applied here */
public const short NO_FONT = 0;
[NonSerialized]
private UnicodeString _string;
private InternalWorkbook _book;
private LabelSSTRecord _record;
///
/// Initializes a new instance of the class.
///
public HSSFRichTextString()
: this("")
{
}
///
/// Initializes a new instance of the class.
///
/// The string.
public HSSFRichTextString(String str)
{
if (str == null)
{
_string = new UnicodeString("");
}
else
{
this._string = new UnicodeString(str);
}
}
///
/// Initializes a new instance of the class.
///
/// The workbook.
/// The record.
public HSSFRichTextString(InternalWorkbook book, LabelSSTRecord record)
{
SetWorkbookReferences(book, record);
this._string = book.GetSSTString(record.SSTIndex);
}
///
/// This must be called to Setup the internal work book references whenever
/// a RichTextString Is Added to a cell
///
/// The workbook.
/// The record.
public void SetWorkbookReferences(InternalWorkbook book, LabelSSTRecord record)
{
this._book = book;
this._record = record;
}
///
/// Called whenever the Unicode string Is modified. When it Is modified
/// we need to Create a new SST index, so that other LabelSSTRecords will not
/// be affected by Changes tat we make to this string.
///
///
private UnicodeString CloneStringIfRequired()
{
if (_book == null)
return _string;
UnicodeString s = (UnicodeString)_string.Clone();
return s;
}
///
/// Adds to SST if required.
///
private void AddToSSTIfRequired()
{
if (_book != null)
{
int index = _book.AddSSTString(_string);
_record.SSTIndex = (index);
//The act of Adding the string to the SST record may have meant that
//a extsing string was returned for the index, so update our local version
_string = _book.GetSSTString(index);
}
}
///
/// Applies a font to the specified Chars of a string.
///
/// The start index to apply the font to (inclusive).
/// The end index to apply the font to (exclusive).
/// The font to use.
public void ApplyFont(int startIndex, int endIndex, short fontIndex)
{
if (startIndex > endIndex)
throw new ArgumentException("Start index must be less than end index.");
if (startIndex < 0 || endIndex > Length)
throw new ArgumentException("Start and end index not in range.");
if (startIndex == endIndex)
return;
//Need to Check what the font Is currently, so we can reapply it after
//the range Is completed
short currentFont = NO_FONT;
if (endIndex != Length)
{
currentFont = this.GetFontAtIndex(endIndex);
}
//Need to clear the current formatting between the startIndex and endIndex
_string = CloneStringIfRequired();
System.Collections.Generic.List formatting = _string.FormatIterator();
ArrayList deletedFR = new ArrayList();
if (formatting != null)
{
IEnumerator formats = formatting.GetEnumerator();
while (formats.MoveNext())
{
UnicodeString.FormatRun r = (UnicodeString.FormatRun)formats.Current;
if ((r.CharacterPos >= startIndex) && (r.CharacterPos < endIndex))
{
deletedFR.Add(r);
}
}
}
foreach (UnicodeString.FormatRun fr in deletedFR)
{
_string.RemoveFormatRun(fr);
}
_string.AddFormatRun(new UnicodeString.FormatRun((short)startIndex, fontIndex));
if (endIndex != Length)
_string.AddFormatRun(new UnicodeString.FormatRun((short)endIndex, currentFont));
AddToSSTIfRequired();
}
///
/// Applies a font to the specified Chars of a string.
///
/// The start index to apply the font to (inclusive).
/// The end index to apply to font to (exclusive).
/// The index of the font to use.
public void ApplyFont(int startIndex, int endIndex, HH.WMS.Utils.NPOI.SS.UserModel.IFont font)
{
ApplyFont(startIndex, endIndex, font.Index);
}
///
/// Sets the font of the entire string.
///
/// The font to use.
public void ApplyFont(HH.WMS.Utils.NPOI.SS.UserModel.IFont font)
{
ApplyFont(0, _string.CharCount, font);
}
///
/// Removes any formatting that may have been applied to the string.
///
public void ClearFormatting()
{
_string = CloneStringIfRequired();
_string.ClearFormatting();
AddToSSTIfRequired();
}
///
/// Returns the plain string representation.
///
/// The string.
public String String
{
get { return _string.String; }
}
///
/// Returns the raw, probably shared Unicode String.
/// Used when tweaking the styles, eg updating font
/// positions.
/// Changes to this string may well effect
/// other RichTextStrings too!
///
/// The raw unicode string.
public UnicodeString RawUnicodeString
{
get
{
return _string;
}
}
///
/// Gets or sets the unicode string.
///
/// The unicode string.
public UnicodeString UnicodeString
{
get { return CloneStringIfRequired(); }
set { this._string = value; }
}
///
/// Gets the number of Chars in the font..
///
/// The length.
public int Length
{
get { return _string.CharCount; }
}
///
/// Returns the font in use at a particular index.
///
/// The index.
/// The font that's currently being applied at that
/// index or null if no font Is being applied or the
/// index Is out of range.
public short GetFontAtIndex(int index)
{
int size = _string.FormatRunCount;
UnicodeString.FormatRun currentRun = null;
for (int i = 0; i < size; i++)
{
UnicodeString.FormatRun r = _string.GetFormatRun(i);
if (r.CharacterPos > index)
break;
else currentRun = r;
}
if (currentRun == null)
return NO_FONT;
else return currentRun.FontIndex;
}
///
/// Gets the number of formatting runs used. There will always be at
/// least one of font NO_FONT.
///
/// The num formatting runs.
public int NumFormattingRuns
{
get { return _string.FormatRunCount; }
}
///
/// The index within the string to which the specified formatting run applies.
///
/// the index of the formatting run
/// the index within the string.
public int GetIndexOfFormattingRun(int index)
{
UnicodeString.FormatRun r = _string.GetFormatRun(index);
return r.CharacterPos;
}
///
/// Gets the font used in a particular formatting run.
///
/// the index of the formatting run.
/// the font number used.
public short GetFontOfFormattingRun(int index)
{
UnicodeString.FormatRun r = _string.GetFormatRun(index);
return r.FontIndex;
}
///
/// Compares one rich text string to another.
///
/// The other rich text string.
///
public int CompareTo(HSSFRichTextString other)
{
return _string.CompareTo(other._string);
}
///
/// Equalses the specified o.
///
/// The o.
///
public override bool Equals(Object o)
{
if (o is HSSFRichTextString)
{
return _string.Equals(((HSSFRichTextString)o).String);
}
return false;
}
public override int GetHashCode ()
{
return _string.GetHashCode ();
}
///
/// Returns a that represents the current .
///
///
/// A that represents the current .
///
public override String ToString()
{
return _string.ToString();
}
///
/// Applies the specified font to the entire string.
///
/// Index of the font to apply.
public void ApplyFont(short fontIndex)
{
ApplyFont(0, _string.CharCount, fontIndex);
}
}
}