zhao
2021-07-09 0821715ebc11d3934d0594a1cc2c39686d808906
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* ====================================================================
   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;
 
    /// <summary>
    /// 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)
    /// </summary> 
    [Serializable]
    public class HSSFRichTextString : IComparable<HSSFRichTextString>,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;
 
        /// <summary>
        /// Initializes a new instance of the <see cref="HSSFRichTextString"/> class.
        /// </summary>
        public HSSFRichTextString()
            : this("")
        {
 
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="HSSFRichTextString"/> class.
        /// </summary>
        /// <param name="str">The string.</param>
        public HSSFRichTextString(String str)
        {
            if (str == null)
            {
                _string = new UnicodeString("");
            }
            else
            {
                this._string = new UnicodeString(str);
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="HSSFRichTextString"/> class.
        /// </summary>
        /// <param name="book">The workbook.</param>
        /// <param name="record">The record.</param>
        public HSSFRichTextString(InternalWorkbook book, LabelSSTRecord record)
        {
            SetWorkbookReferences(book, record);
 
            this._string = book.GetSSTString(record.SSTIndex);
        }
 
        /// <summary>
        /// This must be called to Setup the internal work book references whenever
        /// a RichTextString Is Added to a cell
        /// </summary>
        /// <param name="book">The workbook.</param>
        /// <param name="record">The record.</param>
        public void SetWorkbookReferences(InternalWorkbook book, LabelSSTRecord record)
        {
            this._book = book;
            this._record = record;
        }
 
        /// <summary>
        /// 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.
        /// </summary>
        /// <returns></returns>
        private UnicodeString CloneStringIfRequired()
        {
            if (_book == null)
                return _string;
            UnicodeString s = (UnicodeString)_string.Clone();
            return s;
        }
 
        /// <summary>
        /// Adds to SST if required.
        /// </summary>
        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);
            }
        }
 
 
        /// <summary>
        /// Applies a font to the specified Chars of a string.
        /// </summary>
        /// <param name="startIndex">The start index to apply the font to (inclusive).</param>
        /// <param name="endIndex">The end index to apply the font to (exclusive).</param>
        /// <param name="fontIndex">The font to use.</param>
        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<UnicodeString.FormatRun> 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();
        }
 
        /// <summary>
        /// Applies a font to the specified Chars of a string.
        /// </summary>
        /// <param name="startIndex">The start index to apply the font to (inclusive).</param>
        /// <param name="endIndex"> The end index to apply to font to (exclusive).</param>
        /// <param name="font">The index of the font to use.</param>
        public void ApplyFont(int startIndex, int endIndex, HH.WMS.Utils.NPOI.SS.UserModel.IFont font)
        {
            ApplyFont(startIndex, endIndex, font.Index);
        }
 
        /// <summary>
        /// Sets the font of the entire string.
        /// </summary>
        /// <param name="font">The font to use.</param>
        public void ApplyFont(HH.WMS.Utils.NPOI.SS.UserModel.IFont font)
        {
            ApplyFont(0, _string.CharCount, font);
        }
 
        /// <summary>
        /// Removes any formatting that may have been applied to the string.
        /// </summary>
        public void ClearFormatting()
        {
            _string = CloneStringIfRequired();
            _string.ClearFormatting();
            AddToSSTIfRequired();
        }
 
        /// <summary>
        /// Returns the plain string representation.
        /// </summary>
        /// <value>The string.</value>
        public String String
        {
            get { return _string.String; }
        }
        /// <summary>
        /// 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!
        /// </summary>
        /// <value>The raw unicode string.</value>
        public UnicodeString RawUnicodeString
        {
            get
            {
                return _string;
            }
        }
 
        /// <summary>
        /// Gets or sets the unicode string.
        /// </summary>
        /// <value>The unicode string.</value>
        public UnicodeString UnicodeString
        {
            get { return CloneStringIfRequired(); }
            set { this._string = value; }
        }
 
 
        /// <summary>
        /// Gets the number of Chars in the font..
        /// </summary>
        /// <value>The length.</value>
        public int Length
        {
            get { return _string.CharCount; }
        }
 
        /// <summary>
        /// Returns the font in use at a particular index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <returns>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.</returns>
        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;
        }
 
        /// <summary>
        /// Gets the number of formatting runs used. There will always be at
        /// least one of font NO_FONT.
        /// </summary>
        /// <value>The num formatting runs.</value>
        public int NumFormattingRuns
        {
            get { return _string.FormatRunCount; }
        }
 
        /// <summary>
        /// The index within the string to which the specified formatting run applies.
        /// </summary>
        /// <param name="index">the index of the formatting run</param>
        /// <returns>the index within the string.</returns>
        public int GetIndexOfFormattingRun(int index)
        {
            UnicodeString.FormatRun r = _string.GetFormatRun(index);
            return r.CharacterPos;
        }
 
        /// <summary>
        /// Gets the font used in a particular formatting run.
        /// </summary>
        /// <param name="index">the index of the formatting run.</param>
        /// <returns>the font number used.</returns>
        public short GetFontOfFormattingRun(int index)
        {
            UnicodeString.FormatRun r = _string.GetFormatRun(index);
            return r.FontIndex;
        }
 
        /// <summary>
        /// Compares one rich text string to another.
        /// </summary>
        /// <param name="other">The other rich text string.</param>
        /// <returns></returns>
        public int CompareTo(HSSFRichTextString other)
        {
            return _string.CompareTo(other._string);
        }
 
        /// <summary>
        /// Equalses the specified o.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <returns></returns>
        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 ();
        }
 
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override String ToString()
        {
            return _string.ToString();
        }
 
        /// <summary>
        /// Applies the specified font to the entire string.
        /// </summary>
        /// <param name="fontIndex">Index of the font to apply.</param>
        public void ApplyFont(short fontIndex)
        {
            ApplyFont(0, _string.CharCount, fontIndex);
        }
    }
}