| Method from org.apache.poi.hslf.model.TextRun Detail: |
public RichTextRun appendText(String s) {
// We will need a StyleTextProp atom
ensureStyleAtomPresent();
// First up, append the text to the
// underlying text atom
int oldSize = getRawText().length();
storeText(
getRawText() + s
);
// If either of the previous styles overran
// the text by one, we need to shuffle that
// extra character onto the new ones
int pOverRun = _styleAtom.getParagraphTextLengthCovered() - oldSize;
int cOverRun = _styleAtom.getCharacterTextLengthCovered() - oldSize;
if(pOverRun > 0) {
TextPropCollection tpc = (TextPropCollection)
_styleAtom.getParagraphStyles().getLast();
tpc.updateTextSize(
tpc.getCharactersCovered() - pOverRun
);
}
if(cOverRun > 0) {
TextPropCollection tpc = (TextPropCollection)
_styleAtom.getCharacterStyles().getLast();
tpc.updateTextSize(
tpc.getCharactersCovered() - cOverRun
);
}
// Next, add the styles for its paragraph and characters
TextPropCollection newPTP =
_styleAtom.addParagraphTextPropCollection(s.length()+pOverRun);
TextPropCollection newCTP =
_styleAtom.addCharacterTextPropCollection(s.length()+cOverRun);
// Now, create the new RichTextRun
RichTextRun nr = new RichTextRun(
this, oldSize, s.length(),
newPTP, newCTP, false, false
);
// Add the new RichTextRun onto our list
RichTextRun[] newRuns = new RichTextRun[_rtRuns.length+1];
System.arraycopy(_rtRuns, 0, newRuns, 0, _rtRuns.length);
newRuns[newRuns.length-1] = nr;
_rtRuns = newRuns;
// And return the new run to the caller
return nr;
}
Adds the supplied text onto the end of the TextRun,
creating a new RichTextRun (returned) for it to
sit in.
In many cases, before calling this, you'll want to add
a newline onto the end of your last RichTextRun |
public synchronized void changeTextInRichTextRun(RichTextRun run,
String s) {
// Figure out which run it is
int runID = -1;
for(int i=0; i< _rtRuns.length; i++) {
if(run.equals(_rtRuns[i])) {
runID = i;
}
}
if(runID == -1) {
throw new IllegalArgumentException("Supplied RichTextRun wasn't from this TextRun");
}
// Ensure a StyleTextPropAtom is present, adding if required
ensureStyleAtomPresent();
// Update the text length for its Paragraph and Character stylings
// If it's shared:
// * calculate the new length based on the run's old text
// * this should leave in any +1's for the end of block if needed
// If it isn't shared:
// * reset the length, to the new string's length
// * add on +1 if the last block
// The last run needs its stylings to be 1 longer than the raw
// text is. This is to define the stylings that any new text
// that is added will inherit
TextPropCollection pCol = run._getRawParagraphStyle();
TextPropCollection cCol = run._getRawCharacterStyle();
int newSize = s.length();
if(runID == _rtRuns.length-1) {
newSize++;
}
if(run._isParagraphStyleShared()) {
pCol.updateTextSize( pCol.getCharactersCovered() - run.getLength() + s.length() );
} else {
pCol.updateTextSize(newSize);
}
if(run._isCharacterStyleShared()) {
cCol.updateTextSize( cCol.getCharactersCovered() - run.getLength() + s.length() );
} else {
cCol.updateTextSize(newSize);
}
// Build up the new text
// As we go through, update the start position for all subsequent runs
// The building relies on the old text still being present
StringBuffer newText = new StringBuffer();
for(int i=0; i< _rtRuns.length; i++) {
int newStartPos = newText.length();
// Build up the new text
if(i != runID) {
// Not the affected run, so keep old text
newText.append(_rtRuns[i].getRawText());
} else {
// Affected run, so use new text
newText.append(s);
}
// Do we need to update the start position of this run?
// (Need to get the text before we update the start pos)
if(i < = runID) {
// Change is after this, so don't need to change start position
} else {
// Change has occured, so update start position
_rtRuns[i].updateStartPosition(newStartPos);
}
}
// Now we can save the new text
storeText(newText.toString());
}
Handles an update to the text stored in one of the Rich Text Runs |
public synchronized void ensureStyleAtomPresent() {
if(_styleAtom != null) {
// All there
return;
}
// Create a new one at the right size
_styleAtom = new StyleTextPropAtom(getRawText().length() + 1);
// Use the TextHeader atom to get at the parent
RecordContainer runAtomsParent = _headerAtom.getParentRecord();
// Add the new StyleTextPropAtom after the TextCharsAtom / TextBytesAtom
Record addAfter = _byteAtom;
if(_byteAtom == null) { addAfter = _charAtom; }
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");
}
// These are the only styles for now
_rtRuns[0].supplyTextProps(
(TextPropCollection)_styleAtom.getParagraphStyles().get(0),
(TextPropCollection)_styleAtom.getCharacterStyles().get(0),
false,
false
);
}
Ensure a StyleTextPropAtom is present for this run,
by adding if required. Normally for internal TextRun use. |
public Hyperlink[] getHyperlinks() {
return Hyperlink.find(this);
}
Returns the array of all hyperlinks in this text run |
protected int getIndex() {
return slwtIndex;
}
|
public String getRawText() {
if(_isUnicode) {
return _charAtom.getText();
} else {
return _byteAtom.getText();
}
}
Returns the raw text content of the run. This hasn't had any
changes applied to it, and so is probably unlikely to print
out nicely. |
public Record[] getRecords() {
return _records;
}
Returns records that make up this text run |
public RichTextRun getRichTextRunAt(int pos) {
for (int i = 0; i < _rtRuns.length; i++) {
int start = _rtRuns[i].getStartIndex();
int end = _rtRuns[i].getEndIndex();
if(pos >= start && pos < end) return _rtRuns[i];
}
return null;
}
Fetch RichTextRun at a given position |
public RichTextRun[] getRichTextRuns() {
return _rtRuns;
}
Fetch the rich text runs (runs of text with the same styling) that
are contained within this block of text |
public int getRunType() {
return _headerAtom.getTextType();
}
Returns the type of the text, from the TextHeaderAtom.
Possible values can be seen from TextHeaderAtom |
protected int getShapeId() {
return shapeId;
}
|
public Sheet getSheet() {
return this.sheet;
}
|
public String getText() {
String rawText = getRawText();
// PowerPoint seems to store files with \r as the line break
// The messes things up on everything but a Mac, so translate
// them to \n
String text = rawText.replace('\r",'\n");
int type = _headerAtom == null ? 0 : _headerAtom.getTextType();
if(type == TextHeaderAtom.TITLE_TYPE || type == TextHeaderAtom.CENTER_TITLE_TYPE){
//0xB acts like cariage return in page titles and like blank in the others
text = text.replace((char) 0x0B, '\n");
} else {
text = text.replace((char) 0x0B, ' ");
}
return text;
}
Returns the text content of the run, which has been made safe
for printing and other use. |
public TextRulerAtom getTextRuler() {
for (int i = 0; i < _records.length; i++) {
if(_records[i] instanceof TextRulerAtom) return (TextRulerAtom)_records[i];
}
return null;
}
|
public String normalize(String s) {
String ns = s.replaceAll("\\r?\\n", "\r");
return ns;
}
Returns a new string with line breaks converted into internal ppt representation |
protected void setIndex(int id) {
slwtIndex = id;
}
|
public synchronized void setRawText(String s) {
// Save the new text to the atoms
storeText(s);
RichTextRun fst = _rtRuns[0];
// Finally, zap and re-do the RichTextRuns
for(int i=0; i< _rtRuns.length; i++) { _rtRuns[i] = null; }
_rtRuns = new RichTextRun[1];
_rtRuns[0] = fst;
// Now handle record stylings:
// If there isn't styling
// no change, stays with no styling
// If there is styling:
// everthing gets the same style that the first block has
if(_styleAtom != null) {
LinkedList pStyles = _styleAtom.getParagraphStyles();
while(pStyles.size() > 1) { pStyles.removeLast(); }
LinkedList cStyles = _styleAtom.getCharacterStyles();
while(cStyles.size() > 1) { cStyles.removeLast(); }
_rtRuns[0].setText(s);
} else {
// Recreate rich text run with no styling
_rtRuns[0] = new RichTextRun(this,0,s.length());
}
}
Changes the text, and sets it all to have the same styling
as the the first character has.
If you care about styling, do setText on a RichTextRun instead |
public void setRunType(int type) {
_headerAtom.setTextType(type);
}
Changes the type of the text. Values should be taken
from TextHeaderAtom. No checking is done to ensure you
set this to a valid value! |
protected void setShapeId(int id) {
shapeId = id;
}
|
public void setSheet(Sheet sheet) {
this.sheet = sheet;
}
|
public synchronized void setText(String s) {
String text = normalize(s);
setRawText(text);
}
Changes the text.
Converts '\r' into '\n' |
public void supplySlideShow(SlideShow ss) {
slideShow = ss;
if(_rtRuns != null) {
for(int i=0; i< _rtRuns.length; i++) {
_rtRuns[i].supplySlideShow(slideShow);
}
}
}
Supply the SlideShow we belong to.
Also passes it on to our child RichTextRuns |