import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.record.StyleTextPropAtom.CharFlagsTextProp;
+import org.apache.poi.hslf.record.StyleTextPropAtom.TextProp;
import org.apache.poi.hslf.record.StyleTextPropAtom.TextPropCollection;
/**
{
/** The TextRun we belong to */
private TextRun parentRun;
+ /** The SlideShow we belong to */
+ private SlideShow slideShow;
/** Where in the parent TextRun we start from */
private int startPos;
paragraphStyle = pStyle;
characterStyle = cStyle;
}
+ /**
+ * Supply the SlideShow we belong to
+ */
+ protected void supplySlideShow(SlideShow ss) {
+ slideShow = ss;
+ }
/**
* Get the length of the text
// --------------- Internal helpers on rich text properties -------
+
+ /**
+ * Fetch the value of the given flag in the CharFlagsTextProp.
+ * Returns false if the CharFlagsTextProp isn't present, since the
+ * text property won't be set if there's no CharFlagsTextProp.
+ */
private boolean isCharFlagsTextPropVal(int index) {
if(characterStyle == null) { return false; }
if(cftp == null) { return false; }
return cftp.getSubValue(index);
}
+ /**
+ * Set the value of the given flag in the CharFlagsTextProp, adding
+ * it if required.
+ */
private void setCharFlagsTextPropVal(int index, boolean value) {
+ // Ensure we have the StyleTextProp atom we're going to need
if(characterStyle == null) {
parentRun.ensureStyleAtomPresent();
+ // characterStyle will now be defined
}
CharFlagsTextProp cftp = (CharFlagsTextProp)
- characterStyle.findByName("char_flags");
- if(cftp == null) {
- cftp = (CharFlagsTextProp)characterStyle.addWithName("char_flags");
+ fetchOrAddTextProp(characterStyle, "char_flags");
+ cftp.setSubValue(value,index);
+ }
+
+ /**
+ * Returns the named TextProp, either by fetching it (if it exists) or adding it
+ * (if it didn't)
+ * @param textPropCol The TextPropCollection to fetch from / add into
+ * @param textPropName The name of the TextProp to fetch/add
+ */
+ private TextProp fetchOrAddTextProp(TextPropCollection textPropCol, String textPropName) {
+ // Fetch / Add the TextProp
+ TextProp tp = textPropCol.findByName(textPropName);
+ if(tp == null) {
+ tp = textPropCol.addWithName(textPropName);
}
+ return tp;
+ }
+
+ /**
+ * Fetch the value of the given Character related TextProp.
+ * Returns -1 if that TextProp isn't present.
+ * If the TextProp isn't present, the value from the appropriate
+ * Master Sheet will apply.
+ */
+ private int getCharTextPropVal(String propName) {
+ if(characterStyle == null) { return -1; }
- cftp.setSubValue(value,index);
+ TextProp cTextProp = characterStyle.findByName(propName);
+ if(cTextProp == null) { return -1; }
+ return cTextProp.getValue();
+ }
+ /**
+ * Fetch the value of the given Paragraph related TextProp.
+ * Returns -1 if that TextProp isn't present.
+ * If the TextProp isn't present, the value from the appropriate
+ * Master Sheet will apply.
+ */
+ private int getParaTextPropVal(String propName) {
+ if(paragraphStyle == null) { return -1; }
+
+ TextProp pTextProp = paragraphStyle.findByName(propName);
+ if(pTextProp == null) { return -1; }
+ return pTextProp.getValue();
}
+ /**
+ * Sets the value of the given Character TextProp, add if required
+ * @param propName The name of the Character TextProp
+ * @param val The value to set for the TextProp
+ */
+ private void setParaTextPropVal(String propName, int val) {
+ // Ensure we have the StyleTextProp atom we're going to need
+ if(paragraphStyle == null) {
+ parentRun.ensureStyleAtomPresent();
+ // paragraphStyle will now be defined
+ }
+
+ TextProp tp = fetchOrAddTextProp(paragraphStyle, propName);
+ tp.setValue(val);
+ }
+ /**
+ * Sets the value of the given Paragraph TextProp, add if required
+ * @param propName The name of the Paragraph TextProp
+ * @param val The value to set for the TextProp
+ */
+ private void setCharTextPropVal(String propName, int val) {
+ // Ensure we have the StyleTextProp atom we're going to need
+ if(characterStyle == null) {
+ parentRun.ensureStyleAtomPresent();
+ // characterStyle will now be defined
+ }
+
+ TextProp tp = fetchOrAddTextProp(characterStyle, propName);
+ tp.setValue(val);
+ }
+
+
// --------------- Friendly getters / setters on rich text properties -------
+
public boolean isBold() {
return isCharFlagsTextPropVal(CharFlagsTextProp.BOLD_IDX);
}
-
public void setBold(boolean bold) {
setCharFlagsTextPropVal(CharFlagsTextProp.BOLD_IDX, bold);
}
+ public boolean isItalic() {
+ return isCharFlagsTextPropVal(CharFlagsTextProp.ITALIC_IDX);
+ }
+ public void setItalic(boolean italic) {
+ setCharFlagsTextPropVal(CharFlagsTextProp.ITALIC_IDX, italic);
+ }
+
+ public boolean isUnderlined() {
+ return isCharFlagsTextPropVal(CharFlagsTextProp.UNDERLINE_IDX);
+ }
+ public void setUnderlined(boolean underlined) {
+ setCharFlagsTextPropVal(CharFlagsTextProp.UNDERLINE_IDX, underlined);
+ }
+
+ public int getFontSize() {
+ return getCharTextPropVal("font.size");
+ }
+ public void setFontSize(int fontSize) {
+ setCharTextPropVal("font.size", fontSize);
+ }
+
+ public void setFontName(String fontName) {
+ // Get the index for this font (adding if needed)
+ int fontIdx = slideShow.getFontCollection().addFont(fontName);
+ setCharTextPropVal("font.index", fontIdx);
+ }
+ public String getFontName() {
+ int fontIdx = getCharTextPropVal("font.index");
+ if(fontIdx == -1) { return null; }
+ return slideShow.getFontCollection().getFontWithId(fontIdx);
+ }
+
+
+ // --------------- Internal HSLF methods, not intended for end-user use! -------
/**
* Internal Use Only - get the underlying paragraph style collection.
// Pointers to the most recent versions of the core records
// (Document, Notes, Slide etc)
private Record[] _mostRecentCoreRecords;
+
+ // Records that are interesting
+ private Record _documentRecord;
// Friendly objects for people to deal with
private Slide[] _slides;
// Find the versions of the core records we'll want to use
findMostRecentCoreRecords();
-
+
// Build up the model level Slides and Notes
buildSlidesAndNotes();
}
}
}
}
+
+ // Now look for the interesting records in there
+ for(int i=0; i<_mostRecentCoreRecords.length; i++) {
+ if(_mostRecentCoreRecords[i].getRecordType() == RecordTypes.Document.typeID) {
+ _documentRecord = _mostRecentCoreRecords[i];
+ }
+ }
}
/**
Vector metaSheetsV = new Vector(10);
// For holding SlideListWithText Records
Vector slwtV = new Vector(10);
- // For holding the Document record we're going to use
- Record documentRecord = null;
// Look for Notes, Slides and Documents
for(int i=0; i<_mostRecentCoreRecords.length; i++) {
if(_mostRecentCoreRecords[i] instanceof org.apache.poi.hslf.record.Slide) {
slidesV.add(_mostRecentCoreRecords[i]);
}
- if(_records[i].getRecordType() == RecordTypes.Document.typeID) {
- documentRecord = _mostRecentCoreRecords[i];
- }
}
- // Ensure we really found a Document record
+ // Ensure we really found a Document record earlier
// If we didn't, then the file is probably corrupt
- if(documentRecord == null) {
+ if(_documentRecord == null) {
throw new CorruptPowerPointFileException("The PowerPoint file didn't contain a Document Record in its PersistPtr blocks. It is probably corrupt.");
}
// There shouldn't be any text duplication - only using the most
// record Document record's SLWTs should see to that
- Record[] docChildren = documentRecord.getChildRecords();
+ Record[] docChildren = _documentRecord.getChildRecords();
for(int i=0; i<docChildren.length; i++) {
// Look for SlideListWithText
if(docChildren[i] instanceof SlideListWithText) {
_notes = new Notes[notesV.size()];
for(int i=0; i<_notes.length; i++) {
_notes[i] = new Notes((org.apache.poi.hslf.record.Notes)notesV.get(i));
+
+ // Now supply ourselves to all the rich text runs
+ // of this note's TextRuns
+ TextRun[] trs = _notes[i].getTextRuns();
+ for(int j=0; j<trs.length; j++) {
+ RichTextRun[] rtrs = trs[j].getRichTextRuns();
+ for(int k=0; k<rtrs.length; k++) {
+ rtrs[k].supplySlideShow(this);
+ }
+ }
}
// Create the Slide model layer
_slides[i] = new Slide(slideRecord,thisNotes,atomSet);
+
+ // Now supply ourselves to all the rich text runs
+ // of this slide's TextRuns
+ TextRun[] trs = _slides[i].getTextRuns();
+ for(int j=0; j<trs.length; j++) {
+ RichTextRun[] rtrs = trs[j].getRichTextRuns();
+ for(int k=0; k<rtrs.length; k++) {
+ rtrs[k].supplySlideShow(this);
+ }
+ }
}
}
public Picture[] getPictures() throws IOException {
return _hslfSlideShow.getPictures();
}
+
+ /**
+ * Helper method for usermodel: Get the font collection
+ */
+ protected FontCollection getFontCollection() { return _fonts; }
+ /**
+ * Helper method for usermodel: Get the document record
+ */
+ protected Record getDocumentRecord() { return _documentRecord; }
}