StyleTextPropAtompublic class StyleTextPropAtom extends RecordAtom A StyleTextPropAtom (type 4001). Holds basic character properties
(bold, italic, underline, font size etc) and paragraph properties
(alignment, line spacing etc) for the block of text (TextBytesAtom
or TextCharsAtom) that this record follows.
You will find two lists within this class.
1 - Paragraph style list (paragraphStyles)
2 - Character style list (charStyles)
Both are lists of TextPropCollections. These define how many characters
the style applies to, and what style elements make up the style (another
list, this time of TextProps). Each TextProp has a value, which somehow
encapsulates a property of the style |
Fields Summary |
---|
private byte[] | _header | private static long | _type | private byte[] | reserved | private byte[] | rawContents | private boolean | initialisedOnly set to true once setParentTextSize(int) is called.
Until then, no stylings will have been decoded | private LinkedList | paragraphStylesThe list of all the different paragraph stylings we code for.
Each entry is a TextPropCollection, which tells you how many
Characters the paragraph covers, and also contains the TextProps
that actually define the styling of the paragraph. | private LinkedList | charStylesThe list of all the different character stylings we code for.
Each entry is a TextPropCollection, which tells you how many
Characters the character styling covers, and also contains the
TextProps that actually define the styling of the characters. | public static TextProp[] | paragraphTextPropTypesAll the different kinds of paragraph properties we might handle | public static TextProp[] | characterTextPropTypesAll the different kinds of character properties we might handle |
Constructors Summary |
---|
protected StyleTextPropAtom(byte[] source, int start, int len)For the Text Style Properties (StyleTextProp) Atom
/* *************** record code follows ********************** */
// Sanity Checking - we're always at least 8+10 bytes long
if(len < 18) {
len = 18;
if(source.length - start < 18) {
throw new RuntimeException("Not enough data to form a StyleTextPropAtom (min size 18 bytes long) - found " + (source.length - start));
}
}
// Get the header
_header = new byte[8];
System.arraycopy(source,start,_header,0,8);
// Save the contents of the atom, until we're asked to go and
// decode them (via a call to setParentTextSize(int)
rawContents = new byte[len-8];
System.arraycopy(source,start+8,rawContents,0,rawContents.length);
reserved = new byte[0];
// Set empty linked lists, ready for when they call setParentTextSize
paragraphStyles = new LinkedList();
charStyles = new LinkedList();
| public StyleTextPropAtom(int parentTextSize)A new set of text style properties for some text without any.
_header = new byte[8];
rawContents = new byte[0];
reserved = new byte[0];
// Set our type
LittleEndian.putInt(_header,2,(short)_type);
// Our initial size is 10
LittleEndian.putInt(_header,4,10);
// Set empty paragraph and character styles
paragraphStyles = new LinkedList();
charStyles = new LinkedList();
TextPropCollection defaultParagraphTextProps =
new TextPropCollection(parentTextSize, (short)0);
paragraphStyles.add(defaultParagraphTextProps);
TextPropCollection defaultCharacterTextProps =
new TextPropCollection(parentTextSize);
charStyles.add(defaultCharacterTextProps);
// Set us as now initialised
initialised = true;
|
Methods Summary |
---|
public org.apache.poi.hslf.model.textproperties.TextPropCollection | addCharacterTextPropCollection(int charactersCovered)Create a new Character TextPropCollection, and add it to the list
TextPropCollection tpc = new TextPropCollection(charactersCovered);
charStyles.add(tpc);
return tpc;
| public org.apache.poi.hslf.model.textproperties.TextPropCollection | addParagraphTextPropCollection(int charactersCovered)Create a new Paragraph TextPropCollection, and add it to the list
TextPropCollection tpc = new TextPropCollection(charactersCovered, (short)0);
paragraphStyles.add(tpc);
return tpc;
| public java.util.LinkedList | getCharacterStyles() return charStyles;
| public java.util.LinkedList | getParagraphStyles()
return paragraphStyles;
| public long | getRecordType()We are of type 4001 return _type;
| public void | setCharacterStyles(java.util.LinkedList cs)Updates the link list of TextPropCollections which make up the
character stylings charStyles = cs;
| public void | setParagraphStyles(java.util.LinkedList ps)Updates the link list of TextPropCollections which make up the
paragraph stylings paragraphStyles = ps;
| public void | setParentTextSize(int size)Tell us how much text the parent TextCharsAtom or TextBytesAtom
contains, so we can go ahead and initialise ourselves.
int pos = 0;
int textHandled = 0;
// While we have text in need of paragraph stylings, go ahead and
// grok the contents as paragraph formatting data
int prsize = size;
while(pos < rawContents.length && textHandled < prsize) {
// First up, fetch the number of characters this applies to
int textLen = LittleEndian.getInt(rawContents,pos);
textHandled += textLen;
pos += 4;
// Fetch the 2 byte value that is safe to ignore as 0
short paraIgn = LittleEndian.getShort(rawContents,pos);
pos += 2;
// Grab the 4 byte value that tells us what properties follow
int paraFlags = LittleEndian.getInt(rawContents,pos);
pos += 4;
// Now make sense of those properties
TextPropCollection thisCollection = new TextPropCollection(textLen, paraIgn);
int plSize = thisCollection.buildTextPropList(
paraFlags, paragraphTextPropTypes, rawContents, pos);
pos += plSize;
// Save this properties set
paragraphStyles.add(thisCollection);
// Handle extra 1 paragraph styles at the end
if(pos < rawContents.length && textHandled == size) {
prsize++;
}
}
if (rawContents.length > 0 && textHandled != (size+1)){
logger.log(POILogger.WARN, "Problem reading paragraph style runs: textHandled = " + textHandled + ", text.size+1 = " + (size+1));
}
// Now do the character stylings
textHandled = 0;
int chsize = size;
while(pos < rawContents.length && textHandled < chsize) {
// First up, fetch the number of characters this applies to
int textLen = LittleEndian.getInt(rawContents,pos);
textHandled += textLen;
pos += 4;
// There is no 2 byte value
short no_val = -1;
// Grab the 4 byte value that tells us what properties follow
int charFlags = LittleEndian.getInt(rawContents,pos);
pos += 4;
// Now make sense of those properties
// (Assuming we actually have some)
TextPropCollection thisCollection = new TextPropCollection(textLen, no_val);
int chSize = thisCollection.buildTextPropList(
charFlags, characterTextPropTypes, rawContents, pos);
pos += chSize;
// Save this properties set
charStyles.add(thisCollection);
// Handle extra 1 char styles at the end
if(pos < rawContents.length && textHandled == size) {
chsize++;
}
}
if (rawContents.length > 0 && textHandled != (size+1)){
logger.log(POILogger.WARN, "Problem reading character style runs: textHandled = " + textHandled + ", text.size+1 = " + (size+1));
}
// Handle anything left over
if(pos < rawContents.length) {
reserved = new byte[rawContents.length-pos];
System.arraycopy(rawContents,pos,reserved,0,reserved.length);
}
initialised = true;
| public java.lang.String | toString()Dump the record content into StringBuffer
StringBuffer out = new StringBuffer();
out.append("Paragraph properties\n");
for (Iterator it1 = getParagraphStyles().iterator(); it1.hasNext();) {
TextPropCollection pr = (TextPropCollection)it1.next();
out.append(" chars covered: " + pr.getCharactersCovered() + "\n");
for (Iterator it2 = pr.getTextPropList().iterator(); it2.hasNext(); ) {
TextProp p = (TextProp)it2.next();
out.append(" " + p.getName() + " = " + p.getValue() + "\n");
}
}
out.append("Character properties\n");
for (Iterator it1 = getCharacterStyles().iterator(); it1.hasNext();) {
TextPropCollection pr = (TextPropCollection)it1.next();
out.append(" chars covered: " + pr.getCharactersCovered() + "\n");
for (Iterator it2 = pr.getTextPropList().iterator(); it2.hasNext(); ) {
TextProp p = (TextProp)it2.next();
out.append(" " + p.getName() + " = " + p.getValue() + "\n");
}
}
return out.toString();
| private void | updateRawContents()Updates the cache of the raw contents. Serialised the styles out.
if(!initialised) {
// We haven't groked the styles since creation, so just stick
// with what we found
return;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// First up, we need to serialise the paragraph properties
for(int i=0; i<paragraphStyles.size(); i++) {
TextPropCollection tpc = (TextPropCollection)paragraphStyles.get(i);
tpc.writeOut(baos);
}
// Now, we do the character ones
for(int i=0; i<charStyles.size(); i++) {
TextPropCollection tpc = (TextPropCollection)charStyles.get(i);
tpc.writeOut(baos);
}
rawContents = baos.toByteArray();
| public void | writeOut(java.io.OutputStream out)Write the contents of the record back, so it can be written
to disk
// First thing to do is update the raw bytes of the contents, based
// on the properties
updateRawContents();
// Now ensure that the header size is correct
int newSize = rawContents.length + reserved.length;
LittleEndian.putInt(_header,4,newSize);
// Write out the (new) header
out.write(_header);
// Write out the styles
out.write(rawContents);
// Write out any extra bits
out.write(reserved);
|
|