Lines 37-43
Link Here
|
37 |
* convertion. |
37 |
* convertion. |
38 |
* @author Ryan Ackley |
38 |
* @author Ryan Ackley |
39 |
*/ |
39 |
*/ |
40 |
public final class TextPieceTable |
40 |
public final class TextPieceTable implements CharIndexTranslator |
41 |
{ |
41 |
{ |
42 |
protected ArrayList _textPieces = new ArrayList(); |
42 |
protected ArrayList _textPieces = new ArrayList(); |
43 |
//int _multiple; |
43 |
//int _multiple; |
Lines 150-180
Link Here
|
150 |
// If they ask off the end, just go with the last one... |
150 |
// If they ask off the end, just go with the last one... |
151 |
return lastWas; |
151 |
return lastWas; |
152 |
} |
152 |
} |
153 |
/** |
153 |
|
154 |
* Is the text at the given byte offset |
|
|
155 |
* unicode, or plain old ascii? |
156 |
* In a very evil fashion, you have to actually |
157 |
* know this to make sense of character and |
158 |
* paragraph properties :( |
159 |
* @param bytePos The character offset to check about |
160 |
*/ |
161 |
public boolean isUnicodeAtByteOffset(int bytePos) { |
154 |
public boolean isUnicodeAtByteOffset(int bytePos) { |
162 |
boolean lastWas = false; |
155 |
boolean lastWas = false; |
163 |
int curByte = 0; |
156 |
|
164 |
|
157 |
|
165 |
Iterator it = _textPieces.iterator(); |
158 |
Iterator it = _textPieces.iterator(); |
166 |
while(it.hasNext()) { |
159 |
while(it.hasNext()) { |
167 |
TextPiece tp = (TextPiece)it.next(); |
160 |
TextPiece tp = (TextPiece)it.next(); |
168 |
int nextByte = curByte + tp.bytesLength(); |
161 |
int curByte = tp.getPieceDescriptor().getFilePosition(); |
|
|
162 |
int pieceEnd = curByte + tp.bytesLength(); |
169 |
|
163 |
|
170 |
// If the text piece covers the character, all good |
164 |
// If the text piece covers the character, all good |
171 |
if(curByte <= bytePos && nextByte >= bytePos) { |
165 |
if(curByte <= bytePos && pieceEnd > bytePos) { |
172 |
return tp.isUnicode(); |
166 |
return tp.isUnicode(); |
173 |
} |
167 |
} |
174 |
// Otherwise keep track for the last one |
168 |
// Otherwise keep track for the last one |
175 |
lastWas = tp.isUnicode(); |
169 |
lastWas = tp.isUnicode(); |
176 |
// Move along |
170 |
// Move along |
177 |
curByte = nextByte; |
171 |
curByte = pieceEnd; |
178 |
} |
172 |
} |
179 |
|
173 |
|
180 |
// If they ask off the end, just go with the last one... |
174 |
// If they ask off the end, just go with the last one... |
Lines 268-271
Link Here
|
268 |
} |
262 |
} |
269 |
return false; |
263 |
return false; |
270 |
} |
264 |
} |
|
|
265 |
/* (non-Javadoc) |
266 |
* @see org.apache.poi.hwpf.model.CharIndexTranslator#getLengthInChars(int) |
267 |
*/ |
268 |
public int getCharIndex(int bytePos) { |
269 |
int charCount = 0; |
270 |
|
271 |
Iterator it = _textPieces.iterator(); |
272 |
while (it.hasNext()) { |
273 |
TextPiece tp = (TextPiece) it.next(); |
274 |
int pieceStart = tp.getPieceDescriptor().getFilePosition(); |
275 |
if(pieceStart >= bytePos) { |
276 |
break; |
277 |
} |
278 |
|
279 |
int bytesLength = tp.bytesLength(); |
280 |
int pieceEnd = pieceStart + bytesLength; |
281 |
|
282 |
int toAdd = bytePos > pieceEnd ? bytesLength : bytesLength |
283 |
- (pieceEnd - bytePos); |
284 |
|
285 |
if (tp.isUnicode()) { |
286 |
charCount += toAdd / 2; |
287 |
} else { |
288 |
charCount += toAdd; |
289 |
} |
290 |
} |
291 |
|
292 |
return charCount; |
293 |
} |
294 |
|
271 |
} |
295 |
} |