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
|
/* ====================================================================
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.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.UnicodeString;
import java.util.Iterator;
/**
* 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)
*/
public class HSSFRichTextString
implements Comparable
{
/** Place holder for indicating that NO_FONT has been applied here */
public static final short NO_FONT = 0;
private UnicodeString string;
private Workbook book;
private LabelSSTRecord record;
public HSSFRichTextString()
{
this("");
}
public HSSFRichTextString( String string )
{
if (string == null)
string = "";
this.string = new UnicodeString(string);
}
HSSFRichTextString(Workbook book, LabelSSTRecord record) {
setWorkbookReferences(book, record);
this.string = book.getSSTString(record.getSSTIndex());
}
/** This must be called to setup the internal work book references whenever
* a RichTextString is added to a cell
*/
void setWorkbookReferences(Workbook 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 that we make to this string.
*/
private UnicodeString cloneStringIfRequired() {
if (book == null)
return string;
UnicodeString s = (UnicodeString)string.clone();
return s;
}
private void addToSSTIfRequired() {
if (book != null) {
int index = book.addSSTString(string);
record.setSSTIndex(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 characters of a string.
*
* @param startIndex The start index to apply the font to (inclusive)
* @param endIndex The end index to apply the font to (exclusive)
* @param fontIndex The font to use.
*/
public void applyFont(int startIndex, int endIndex, short fontIndex)
{
if (startIndex > endIndex)
throw new IllegalArgumentException("Start index must be less than end index.");
if (startIndex < 0 || endIndex > length())
throw new IllegalArgumentException("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(startIndex);
}
//Need to clear the current formatting between the startIndex and endIndex
string = cloneStringIfRequired();
Iterator formatting = string.formatIterator();
if (formatting != null) {
while (formatting.hasNext()) {
UnicodeString.FormatRun r = (UnicodeString.FormatRun)formatting.next();
if ((r.getCharacterPos() >= startIndex) && (r.getCharacterPos() < endIndex))
formatting.remove();
}
}
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 characters of a string.
*
* @param startIndex The start index to apply the font to (inclusive)
* @param endIndex The end index to apply to font to (exclusive)
* @param font The index of the font to use.
*/
public void applyFont(int startIndex, int endIndex, HSSFFont font)
{
applyFont(startIndex, endIndex, font.getIndex());
}
/**
* Sets the font of the entire string.
* @param font The font to use.
*/
public void applyFont(HSSFFont font)
{
applyFont(0, string.getCharCount(), 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.
*/
public String getString()
{
return string.getString();
}
/**
* Used internally by the HSSFCell to get the internal
* string value.
* Will ensure the string is not shared
*/
UnicodeString getUnicodeString() {
return cloneStringIfRequired();
}
/**
* 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!
*/
UnicodeString getRawUnicodeString() {
return string;
}
/** Used internally by the HSSFCell to set the internal string value*/
void setUnicodeString(UnicodeString str) {
this.string = str;
}
/**
* @return the number of characters in the font.
*/
public int length()
{
return string.getCharCount();
}
/**
* Returns the font in use at a particular index.
*
* @param index The index.
* @return 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.getFormatRunCount();
UnicodeString.FormatRun currentRun = null;
for (int i=0;i<size;i++) {
UnicodeString.FormatRun r = string.getFormatRun(i);
if (r.getCharacterPos() > index)
break;
else currentRun = r;
}
if (currentRun == null)
return NO_FONT;
else return currentRun.getFontIndex();
}
/**
* @return The number of formatting runs used. There will always be at
* least one of font NO_FONT.
*
* @see #NO_FONT
*/
public int numFormattingRuns()
{
return string.getFormatRunCount();
}
/**
* The index within the string to which the specified formatting run applies.
* @param index the index of the formatting run
* @return the index within the string.
*/
public int getIndexOfFormattingRun(int index)
{
UnicodeString.FormatRun r = string.getFormatRun(index);
return r.getCharacterPos();
}
/**
* Gets the font used in a particular formatting run.
*
* @param index the index of the formatting run
* @return the font number used.
*/
public short getFontOfFormattingRun(int index)
{
UnicodeString.FormatRun r = string.getFormatRun(index);
return r.getFontIndex();
}
/**
* Compares one rich text string to another.
*/
public int compareTo( Object o )
{
HSSFRichTextString r = (HSSFRichTextString)o;
return string.compareTo(r.string);
}
public boolean equals(Object o) {
if (o instanceof HSSFRichTextString) {
return string.equals(((HSSFRichTextString)o).string);
}
return false;
}
/**
* @return the plain text representation of this string.
*/
public String toString()
{
return string.toString();
}
/**
* Applies the specified font to the entire string.
*
* @param fontIndex the font to apply.
*/
public void applyFont( short fontIndex )
{
applyFont(0, string.getCharCount(), fontIndex);
}
}
|