Make the code for adding a new RichTextRun to a TextRun a bit nicer

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610169 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-01-08 21:35:53 +00:00
parent f06b3921bd
commit 5471fcbdce
3 changed files with 70 additions and 42 deletions

View File

@ -20,12 +20,16 @@
package org.apache.poi.hslf.model; package org.apache.poi.hslf.model;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Vector; import java.util.Vector;
import org.apache.poi.hslf.model.textproperties.TextPropCollection; import org.apache.poi.hslf.model.textproperties.TextPropCollection;
import org.apache.poi.hslf.record.*; import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordContainer;
import org.apache.poi.hslf.record.StyleTextPropAtom;
import org.apache.poi.hslf.record.TextBytesAtom;
import org.apache.poi.hslf.record.TextCharsAtom;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.hslf.usermodel.RichTextRun; import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow; import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
@ -258,12 +262,15 @@ public class TextRun
* Adds the supplied text onto the end of the TextRun, * Adds the supplied text onto the end of the TextRun,
* creating a new RichTextRun (returned) for it to * creating a new RichTextRun (returned) for it to
* sit in. * sit in.
* In many cases, before calling this, you'll want to add
* a newline onto the end of your last RichTextRun
*/ */
public RichTextRun appendText(String s) { public RichTextRun appendText(String s) {
// We will need a StyleTextProp atom // We will need a StyleTextProp atom
ensureStyleAtomPresent(); ensureStyleAtomPresent();
// First up, append the text // First up, append the text to the
// underlying text atom
int oldSize = getRawText().length(); int oldSize = getRawText().length();
storeText( storeText(
getRawText() + s getRawText() + s
@ -272,21 +279,8 @@ public class TextRun
// If either of the previous styles overran // If either of the previous styles overran
// the text by one, we need to shuffle that // the text by one, we need to shuffle that
// extra character onto the new ones // extra character onto the new ones
Iterator it = _styleAtom.getParagraphStyles().iterator(); int pOverRun = _styleAtom.getParagraphTextLengthCovered() - oldSize;
int pLen = 0; int cOverRun = _styleAtom.getCharacterTextLengthCovered() - oldSize;
while(it.hasNext()) {
TextPropCollection tpc = (TextPropCollection)it.next();
pLen += tpc.getCharactersCovered();
}
it = _styleAtom.getCharacterStyles().iterator();
int cLen = 0;
while(it.hasNext()) {
TextPropCollection tpc = (TextPropCollection)it.next();
cLen += tpc.getCharactersCovered();
}
int pOverRun = pLen - oldSize;
int cOverRun = cLen - oldSize;
if(pOverRun > 0) { if(pOverRun > 0) {
TextPropCollection tpc = (TextPropCollection) TextPropCollection tpc = (TextPropCollection)
_styleAtom.getParagraphStyles().getLast(); _styleAtom.getParagraphStyles().getLast();
@ -302,8 +296,7 @@ public class TextRun
); );
} }
// Next, add the styles for its // Next, add the styles for its paragraph and characters
// paragraph and characters
TextPropCollection newPTP = TextPropCollection newPTP =
_styleAtom.addParagraphTextPropCollection(s.length()+pOverRun); _styleAtom.addParagraphTextPropCollection(s.length()+pOverRun);
TextPropCollection newCTP = TextPropCollection newCTP =

View File

@ -19,17 +19,19 @@
package org.apache.poi.hslf.record; package org.apache.poi.hslf.record;
import org.apache.poi.hslf.model.textproperties.*; import java.io.ByteArrayOutputStream;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogger;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.util.LinkedList;
import java.util.Vector;
import java.util.List;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import org.apache.poi.hslf.model.textproperties.AlignmentTextProp;
import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;
import org.apache.poi.hslf.model.textproperties.TextProp;
import org.apache.poi.hslf.model.textproperties.TextPropCollection;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogger;
/** /**
* A StyleTextPropAtom (type 4001). Holds basic character properties * A StyleTextPropAtom (type 4001). Holds basic character properties
@ -88,6 +90,37 @@ public class StyleTextPropAtom extends RecordAtom
* character stylings * character stylings
*/ */
public void setCharacterStyles(LinkedList cs) { charStyles = cs; } public void setCharacterStyles(LinkedList cs) { charStyles = cs; }
/**
* Returns how many characters the paragraph's
* TextPropCollections cover.
* (May be one or two more than the underlying text does,
* due to having extra characters meaning something
* special to powerpoint)
*/
public int getParagraphTextLengthCovered() {
return getCharactersCovered(paragraphStyles);
}
/**
* Returns how many characters the character's
* TextPropCollections cover.
* (May be one or two more than the underlying text does,
* due to having extra characters meaning something
* special to powerpoint)
*/
public int getCharacterTextLengthCovered() {
return getCharactersCovered(charStyles);
}
private int getCharactersCovered(LinkedList styles) {
int length = 0;
Iterator it = styles.iterator();
while(it.hasNext()) {
TextPropCollection tpc =
(TextPropCollection)it.next();
length += tpc.getCharactersCovered();
}
return length;
}
/** All the different kinds of paragraph properties we might handle */ /** All the different kinds of paragraph properties we might handle */
public static TextProp[] paragraphTextPropTypes = new TextProp[] { public static TextProp[] paragraphTextPropTypes = new TextProp[] {
@ -355,8 +388,7 @@ public class StyleTextPropAtom extends RecordAtom
charStyles.add(tpc); charStyles.add(tpc);
return tpc; return tpc;
} }
/* ************************************************************************ */ /* ************************************************************************ */

View File

@ -20,25 +20,28 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import org.apache.poi.hslf.model.*; import java.awt.Color;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.textproperties.*;
import org.apache.poi.hslf.record.ColorSchemeAtom;
import org.apache.poi.hslf.exceptions.HSLFException;
import java.awt.*; import org.apache.poi.hslf.model.MasterSheet;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;
import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;
import org.apache.poi.hslf.model.textproperties.TextProp;
import org.apache.poi.hslf.model.textproperties.TextPropCollection;
import org.apache.poi.hslf.record.ColorSchemeAtom;
/** /**
* Represents a run of text, all with the same style * Represents a run of text, all with the same style
* *
* TODO: get access to the font/character properties * TODO: finish all the getters and setters to the
* * font/character/paragraph properties (currently only
* @author Nick Burch * has some of them)
*/ */
public class RichTextRun {
public class RichTextRun
{
/** The TextRun we belong to */ /** The TextRun we belong to */
private TextRun parentRun; private TextRun parentRun;
/** The SlideShow we belong to */ /** The SlideShow we belong to */