public CHPX(int fcStart, int fcEnd, CharIndexTranslator translator, byte[] grpprl)
{
- super(fcStart, fcEnd, translator, new SprmBuffer(grpprl));
+ super(fcStart, translator.lookIndexBackward(fcEnd), translator, new SprmBuffer(grpprl));
}
public CHPX(int fcStart, int fcEnd, CharIndexTranslator translator, SprmBuffer buf)
{
- super(fcStart, fcEnd, translator ,buf);
+ super(fcStart, translator.lookIndexBackward(fcEnd), translator ,buf);
}
package org.apache.poi.hwpf.model;\r
\r
public interface CharIndexTranslator {\r
-\r
/**\r
* Calculates the char index of the given byte index.\r
+ * Look forward if index is not in table\r
*\r
* @param bytePos The character offset to check \r
* @return the char index\r
int getCharIndex(int bytePos);\r
\r
/**\r
- * Is the text at the given byte offset unicode, or plain old ascii? In a\r
- * very evil fashion, you have to actually know this to make sense of\r
- * character and paragraph properties :(\r
+ * Check if index is in table\r
+ *\r
+ * @param bytePos\r
+ * @return true if index in table, false if not\r
+ */\r
+\r
+ boolean isIndexInTable(int bytePos);\r
+\r
+ /**\r
+ * Return first index >= bytePos that is in table\r
+ *\r
+ * @param bytePos\r
+ * @return\r
+ */\r
+ public int lookIndexForward(int bytePos);\r
+\r
+ /**\r
+ * Return last index <= bytePos that is in table\r
*\r
- * @param bytePos The character offset to check about\r
- * @return true if the text at the given byte offset is unicode\r
+ * @param bytePos\r
+ * @return\r
*/\r
- boolean isUnicodeAtByteOffset(int bytePos);\r
+ public int lookIndexBackward(int bytePos);\r
\r
}\r
*
* @author Ryan Ackley
*/
-public final class TextPieceTable implements CharIndexTranslator {
+public class TextPieceTable implements CharIndexTranslator {
protected ArrayList<TextPiece> _textPieces = new ArrayList<TextPiece>();
protected ArrayList<TextPiece> _textPiecesFCOrder = new ArrayList<TextPiece>();
// int _multiple;
Collections.sort(_textPiecesFCOrder, new FCComparator());
}
- /**
- * Is the text at the given Character offset unicode, or plain old ascii? In
- * a very evil fashion, you have to actually know this to make sense of
- * character and paragraph properties :(
- *
- * @param cp
- * The character offset to check about
- */
- public boolean isUnicodeAtCharOffset(int cp) {
- boolean lastWas = false;
-
- for(TextPiece tp : _textPieces) {
- // If the text piece covers the character, all good
- if (tp.getStart() <= cp && tp.getEnd() >= cp) {
- return tp.isUnicode();
- }
- // Otherwise keep track for the last one
- lastWas = tp.isUnicode();
- }
-
- // If they ask off the end, just go with the last one...
- return lastWas;
- }
-
- public boolean isUnicodeAtByteOffset(int bytePos) {
- boolean lastWas = false;
-
- for(TextPiece tp : _textPieces) {
- int curByte = tp.getPieceDescriptor().getFilePosition();
- int pieceEnd = curByte + tp.bytesLength();
-
- // If the text piece covers the character, all good
- if (curByte <= bytePos && pieceEnd > bytePos) {
- return tp.isUnicode();
- }
- // Otherwise keep track for the last one
- lastWas = tp.isUnicode();
- // Move along
- curByte = pieceEnd;
- }
-
- // If they ask off the end, just go with the last one...
- return lastWas;
- }
-
public byte[] writeTo(HWPFOutputStream docStream) throws IOException {
PlexOfCps textPlex = new PlexOfCps(PieceDescriptor.getSizeInBytes());
public int getCharIndex(int bytePos) {
int charCount = 0;
- for(TextPiece tp : _textPiecesFCOrder) {
- int pieceStart = tp.getPieceDescriptor().getFilePosition();
-
- if (bytePos > pieceStart + tp.bytesLength()) {
- continue;
- }
-
- if (pieceStart > bytePos) {
- bytePos = pieceStart;
- }
-
- break;
- }
+ bytePos = lookIndexForward(bytePos);
for(TextPiece tp : _textPieces) {
int pieceStart = tp.getPieceDescriptor().getFilePosition();
return charCount;
}
+ public int lookIndexForward(int bytePos) {
+ for(TextPiece tp : _textPiecesFCOrder) {
+ int pieceStart = tp.getPieceDescriptor().getFilePosition();
+
+ if (bytePos > pieceStart + tp.bytesLength()) {
+ continue;
+ }
+
+ if (pieceStart > bytePos) {
+ bytePos = pieceStart;
+ }
+
+ break;
+ }
+ return bytePos;
+ }
+
+ public int lookIndexBackward(int bytePos) {
+ int lastEnd = 0;
+
+ for(TextPiece tp : _textPiecesFCOrder) {
+ int pieceStart = tp.getPieceDescriptor().getFilePosition();
+
+ if (bytePos > pieceStart + tp.bytesLength()) {
+ lastEnd = pieceStart + tp.bytesLength();
+ continue;
+ }
+
+ if (pieceStart > bytePos) {
+ bytePos = lastEnd;
+ }
+
+ break;
+ }
+
+ return bytePos;
+ }
+
+ public boolean isIndexInTable(int bytePos) {
+ for(TextPiece tp : _textPiecesFCOrder) {
+ int pieceStart = tp.getPieceDescriptor().getFilePosition();
+
+ if (bytePos > pieceStart + tp.bytesLength()) {
+ continue;
+ }
+
+ if (pieceStart > bytePos) {
+ return false;
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
private static class FCComparator implements Comparator<TextPiece> {
public int compare(TextPiece textPiece, TextPiece textPiece1) {
if (textPiece.getPieceDescriptor().fc>textPiece1.getPieceDescriptor().fc) {
public CharacterRun getCharacterRun(int index) {
initCharacterRuns();
CHPX chpx = _characters.get(index + _charStart);
+
+ if (chpx == null) {
+ return null;
+ }
int[] point = findRange(_paragraphs, _parStart, Math.max(chpx.getStart(), _start), chpx
.getEnd());
int x = min;
PropertyNode node = (PropertyNode) rpl.get(x);
- while (node.getEnd() <= start && x < rpl.size() - 1) {
+ while (node==null || (node.getEnd() <= start && x < rpl.size() - 1)) {
x++;
node = (PropertyNode) rpl.get(x);
}
int y = x;
node = (PropertyNode) rpl.get(y);
- while (node.getEnd() < end && y < rpl.size() - 1) {
+ while (node==null || (node.getEnd() < end && y < rpl.size() - 1)) {
y++;
node = (PropertyNode) rpl.get(y);
}