Some support for changing text in Rich text runs

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353800 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2005-11-29 22:22:13 +00:00
parent d875a88d28
commit e805a38c10
2 changed files with 68 additions and 18 deletions

View File

@ -83,7 +83,7 @@ public class TextRun
String runRawText = getText(); String runRawText = getText();
// Figure out the rich text runs // Figure out the rich text runs
// TODO: Handle when paragraph style and character styles don't match up // Assumes the paragraph styles are never shorter than the character ones
LinkedList pStyles = new LinkedList(); LinkedList pStyles = new LinkedList();
LinkedList cStyles = new LinkedList(); LinkedList cStyles = new LinkedList();
if(_styleAtom != null) { if(_styleAtom != null) {
@ -91,24 +91,48 @@ public class TextRun
pStyles = _styleAtom.getParagraphStyles(); pStyles = _styleAtom.getParagraphStyles();
cStyles = _styleAtom.getCharacterStyles(); cStyles = _styleAtom.getCharacterStyles();
} }
if(pStyles.size() != cStyles.size()) {
throw new RuntimeException("Don't currently handle case of overlapping styles");
}
int pos = 0; _rtRuns = new RichTextRun[cStyles.size()];
_rtRuns = new RichTextRun[pStyles.size()];
for(int i=0; i<_rtRuns.length; i++) {
TextPropCollection pProps = (TextPropCollection)pStyles.get(i);
TextPropCollection cProps = (TextPropCollection)cStyles.get(i);
int len = cProps.getCharactersCovered();
_rtRuns[i] = new RichTextRun(this, pos, len, pProps, cProps);
pos += len;
}
// Handle case of no current style, with a default // Handle case of no current style, with a default
if(_rtRuns.length == 0) { if(_rtRuns.length == 0) {
_rtRuns = new RichTextRun[1]; _rtRuns = new RichTextRun[1];
_rtRuns[0] = new RichTextRun(this, 0, runRawText.length()); _rtRuns[0] = new RichTextRun(this, 0, runRawText.length());
} else {
// Build up Rich Text Runs, one for each character style block
int pos = 0;
int curP = 0;
int pLenRemain = ((TextPropCollection)pStyles.get(curP)).getCharactersCovered();
// Build one for each character style
for(int i=0; i<_rtRuns.length; i++) {
// Get the Props to use
TextPropCollection pProps = (TextPropCollection)pStyles.get(curP);
TextPropCollection cProps = (TextPropCollection)cStyles.get(i);
// Get the length to extend over
// (Last run is often 1 char shorter than the cProp claims)
int len = cProps.getCharactersCovered();
if(len+pos > runRawText.length()) {
len = runRawText.length()-pos;
}
// Build the rich text run
_rtRuns[i] = new RichTextRun(this, pos, len, pProps, cProps);
pos += len;
// See if we need to move onto the next paragraph style
pLenRemain -= len;
if(pLenRemain == 0) {
curP++;
if(curP < pStyles.size()) {
pLenRemain = ((TextPropCollection)pStyles.get(curP)).getCharactersCovered();
}
}
if(pLenRemain < 0) {
throw new IllegalStateException("Paragraph style ran out before character style did!");
}
}
} }
} }
@ -227,6 +251,10 @@ public class TextRun
// Save the new text to the atoms // Save the new text to the atoms
storeText(s); storeText(s);
// Finally, zap and re-do the RichTextRuns
for(int i=0; i<_rtRuns.length; i++) { _rtRuns[i] = null; }
_rtRuns = new RichTextRun[1];
// Now handle record stylings: // Now handle record stylings:
// If there isn't styling // If there isn't styling
// no change, stays with no styling // no change, stays with no styling
@ -243,12 +271,14 @@ public class TextRun
TextPropCollection cCol = (TextPropCollection)cStyles.getFirst(); TextPropCollection cCol = (TextPropCollection)cStyles.getFirst();
pCol.updateTextSize(s.length()); pCol.updateTextSize(s.length());
cCol.updateTextSize(s.length()); cCol.updateTextSize(s.length());
}
// Finally, zap and re-do the RichTextRuns // Recreate rich text run with first styling
_rtRuns = new RichTextRun[1]; _rtRuns[0] = new RichTextRun(this,0,s.length(), pCol, cCol);
} else {
// Recreate rich text run with no styling
_rtRuns[0] = new RichTextRun(this,0,s.length()); _rtRuns[0] = new RichTextRun(this,0,s.length());
} }
}
/** /**
* Ensure a StyleTextPropAtom is present for this run, * Ensure a StyleTextPropAtom is present for this run,
@ -270,6 +300,15 @@ public class TextRun
Record addAfter = _byteAtom; Record addAfter = _byteAtom;
if(_byteAtom == null) { addAfter = _charAtom; } if(_byteAtom == null) { addAfter = _charAtom; }
runAtomsParent.addChildAfter(_styleAtom, addAfter); runAtomsParent.addChildAfter(_styleAtom, addAfter);
// Feed this to our sole rich text run
if(_rtRuns.length != 1) {
throw new IllegalStateException("Needed to add StyleTextPropAtom when had many rich text runs");
}
_rtRuns[0].supplyTextProps(
(TextPropCollection)_styleAtom.getParagraphStyles().get(0),
(TextPropCollection)_styleAtom.getCharacterStyles().get(0)
);
} }
// Accesser methods follow // Accesser methods follow

View File

@ -76,6 +76,17 @@ public class RichTextRun
characterStyle = cStyle; characterStyle = cStyle;
} }
/**
* Supply (normally default) textprops, when a run gets them
*/
public void supplyTextProps(TextPropCollection pStyle, TextPropCollection cStyle) {
if(paragraphStyle != null || characterStyle != null) {
throw new IllegalStateException("Can't call supplyTextProps if run already has some");
}
paragraphStyle = pStyle;
characterStyle = cStyle;
}
/** /**
* Fetch the text, in output suitable form * Fetch the text, in output suitable form
*/ */