Support for getting and changing the font of a rich text run

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@386872 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2006-03-18 18:56:26 +00:00
parent 3b783835df
commit cba78ae839
3 changed files with 188 additions and 16 deletions

View File

@ -86,5 +86,18 @@ public class FontCollection extends RecordContainer {
return fonts.size()-1; //the added font is the last in the list return fonts.size()-1; //the added font is the last in the list
} }
/**
* Get the name of the font at the given ID, or null if there is
* no font at that ID.
* @param id
* @return
*/
public String getFontWithId(int id) {
if(id >= fonts.size()) {
// No font with that id
return null;
}
return (String)fonts.get(id);
}
} }

View File

@ -21,6 +21,7 @@ package org.apache.poi.hslf.usermodel;
import org.apache.poi.hslf.model.TextRun; import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.record.StyleTextPropAtom.CharFlagsTextProp; import org.apache.poi.hslf.record.StyleTextPropAtom.CharFlagsTextProp;
import org.apache.poi.hslf.record.StyleTextPropAtom.TextProp;
import org.apache.poi.hslf.record.StyleTextPropAtom.TextPropCollection; import org.apache.poi.hslf.record.StyleTextPropAtom.TextPropCollection;
/** /**
@ -35,6 +36,8 @@ public class RichTextRun
{ {
/** The TextRun we belong to */ /** The TextRun we belong to */
private TextRun parentRun; private TextRun parentRun;
/** The SlideShow we belong to */
private SlideShow slideShow;
/** Where in the parent TextRun we start from */ /** Where in the parent TextRun we start from */
private int startPos; private int startPos;
@ -87,6 +90,12 @@ public class RichTextRun
paragraphStyle = pStyle; paragraphStyle = pStyle;
characterStyle = cStyle; characterStyle = cStyle;
} }
/**
* Supply the SlideShow we belong to
*/
protected void supplySlideShow(SlideShow ss) {
slideShow = ss;
}
/** /**
* Get the length of the text * Get the length of the text
@ -126,6 +135,12 @@ public class RichTextRun
// --------------- Internal helpers on rich text properties ------- // --------------- 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) { private boolean isCharFlagsTextPropVal(int index) {
if(characterStyle == null) { return false; } if(characterStyle == null) { return false; }
@ -135,29 +150,139 @@ public class RichTextRun
if(cftp == null) { return false; } if(cftp == null) { return false; }
return cftp.getSubValue(index); 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) { private void setCharFlagsTextPropVal(int index, boolean value) {
// Ensure we have the StyleTextProp atom we're going to need
if(characterStyle == null) { if(characterStyle == null) {
parentRun.ensureStyleAtomPresent(); parentRun.ensureStyleAtomPresent();
// characterStyle will now be defined
} }
CharFlagsTextProp cftp = (CharFlagsTextProp) CharFlagsTextProp cftp = (CharFlagsTextProp)
characterStyle.findByName("char_flags"); fetchOrAddTextProp(characterStyle, "char_flags");
if(cftp == null) {
cftp = (CharFlagsTextProp)characterStyle.addWithName("char_flags");
}
cftp.setSubValue(value,index); 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; }
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 ------- // --------------- Friendly getters / setters on rich text properties -------
public boolean isBold() { public boolean isBold() {
return isCharFlagsTextPropVal(CharFlagsTextProp.BOLD_IDX); return isCharFlagsTextPropVal(CharFlagsTextProp.BOLD_IDX);
} }
public void setBold(boolean bold) { public void setBold(boolean bold) {
setCharFlagsTextPropVal(CharFlagsTextProp.BOLD_IDX, 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. * Internal Use Only - get the underlying paragraph style collection.

View File

@ -60,6 +60,9 @@ public class SlideShow
// Pointers to the most recent versions of the core records // Pointers to the most recent versions of the core records
// (Document, Notes, Slide etc) // (Document, Notes, Slide etc)
private Record[] _mostRecentCoreRecords; private Record[] _mostRecentCoreRecords;
// Records that are interesting
private Record _documentRecord;
// Friendly objects for people to deal with // Friendly objects for people to deal with
private Slide[] _slides; private Slide[] _slides;
@ -89,7 +92,7 @@ public class SlideShow
// Find the versions of the core records we'll want to use // Find the versions of the core records we'll want to use
findMostRecentCoreRecords(); findMostRecentCoreRecords();
// Build up the model level Slides and Notes // Build up the model level Slides and Notes
buildSlidesAndNotes(); buildSlidesAndNotes();
} }
@ -194,6 +197,13 @@ public class SlideShow
} }
} }
} }
// 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];
}
}
} }
/** /**
@ -209,8 +219,6 @@ public class SlideShow
Vector metaSheetsV = new Vector(10); Vector metaSheetsV = new Vector(10);
// For holding SlideListWithText Records // For holding SlideListWithText Records
Vector slwtV = new Vector(10); Vector slwtV = new Vector(10);
// For holding the Document record we're going to use
Record documentRecord = null;
// Look for Notes, Slides and Documents // Look for Notes, Slides and Documents
for(int i=0; i<_mostRecentCoreRecords.length; i++) { for(int i=0; i<_mostRecentCoreRecords.length; i++) {
@ -220,14 +228,11 @@ public class SlideShow
if(_mostRecentCoreRecords[i] instanceof org.apache.poi.hslf.record.Slide) { if(_mostRecentCoreRecords[i] instanceof org.apache.poi.hslf.record.Slide) {
slidesV.add(_mostRecentCoreRecords[i]); 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 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."); throw new CorruptPowerPointFileException("The PowerPoint file didn't contain a Document Record in its PersistPtr blocks. It is probably corrupt.");
} }
@ -251,7 +256,7 @@ public class SlideShow
// There shouldn't be any text duplication - only using the most // There shouldn't be any text duplication - only using the most
// record Document record's SLWTs should see to that // 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++) { for(int i=0; i<docChildren.length; i++) {
// Look for SlideListWithText // Look for SlideListWithText
if(docChildren[i] instanceof SlideListWithText) { if(docChildren[i] instanceof SlideListWithText) {
@ -322,6 +327,16 @@ public class SlideShow
_notes = new Notes[notesV.size()]; _notes = new Notes[notesV.size()];
for(int i=0; i<_notes.length; i++) { for(int i=0; i<_notes.length; i++) {
_notes[i] = new Notes((org.apache.poi.hslf.record.Notes)notesV.get(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);
}
}
} }
@ -352,6 +367,16 @@ public class SlideShow
// Create the Slide model layer // Create the Slide model layer
_slides[i] = new Slide(slideRecord,thisNotes,atomSet); _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);
}
}
} }
} }
@ -398,4 +423,13 @@ public class SlideShow
public Picture[] getPictures() throws IOException { public Picture[] getPictures() throws IOException {
return _hslfSlideShow.getPictures(); 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; }
} }