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
|
/* ====================================================================
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 java.util.Iterator;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
/**
* Rich text unicode string. These strings can have fonts applied to
* arbitary parts of the string.
*
* <p>
* Note, that in certain cases creating too many HSSFRichTextString cells may cause Excel 2003 and lower to crash
* when changing the color of the cells and then saving the Excel file. Compare two snippets that produce equivalent output:
*
* <p><blockquote><pre>
* HSSFCell hssfCell = row.createCell(idx);
* //rich text consists of two runs
* HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" );
* richString.applyFont( 0, 6, font1 );
* richString.applyFont( 6, 13, font2 );
* hssfCell.setCellValue( richString );
* </pre></blockquote>
*
* and
*
* <p><blockquote><pre>
* //create a cell style and assign the first font to it
* HSSFCellStyle style = workbook.createCellStyle();
* style.setFont(font1);
*
* HSSFCell hssfCell = row.createCell(idx);
* hssfCell.setCellStyle(style);
*
* //rich text consists of one run overriding the cell style
* HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" );
* richString.applyFont( 6, 13, font2 );
* hssfCell.setCellValue( richString );
* </pre></blockquote><p>
*
* Excel always uses the latter approach: for a reach text containing N runs Excel saves the font of the first run in the cell's
* style and subsequent N-1 runs override this font.
*
* <p> For more information regarding this behavior please consult Bugzilla 47543:
*
* <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=47543">
* https://issues.apache.org/bugzilla/show_bug.cgi?id=47543</a>
*/
public final class HSSFRichTextString implements Comparable<HSSFRichTextString>, RichTextString {
/** Place holder for indicating that NO_FONT has been applied here */
public static final short NO_FONT = 0;
private UnicodeString _string;
private InternalWorkbook _book;
private LabelSSTRecord _record;
public HSSFRichTextString()
{
this("");
}
public HSSFRichTextString(String string) {
if (string == null) {
_string = new UnicodeString("");
} else {
_string = new UnicodeString(string);
}
}
HSSFRichTextString(InternalWorkbook book, LabelSSTRecord record) {
setWorkbookReferences(book, record);
_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(InternalWorkbook book, LabelSSTRecord record) {
_book = book;
_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
//an existing 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(endIndex);
}
//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, Font font)
{
applyFont(startIndex, endIndex, ((HSSFFont) font).getIndex());
}
/**
* Sets the font of the entire string.
* @param font The font to use.
*/
public void applyFont(Font 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 text.
*/
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;
}
currentRun = r;
}
if (currentRun == null) {
return NO_FONT;
}
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(HSSFRichTextString r) {
return _string.compareTo(r._string);
}
@Override
public boolean equals(Object o) {
if (o instanceof HSSFRichTextString) {
return _string.equals(((HSSFRichTextString)o)._string);
}
return false;
}
@Override
public int hashCode() {
assert false : "hashCode not designed";
return 42; // any arbitrary constant will do
}
/**
* @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);
}
}
|