Author: nick Date: Sat Mar 18 10:56:26 2006 New Revision: 386872 URL: http://svn.apache.org/viewcvs?rev=386872&view=rev Log: Support for getting and changing the font of a rich text run
Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java?rev=386872&r1=386871&r2=386872&view=diff ============================================================================== --- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java (original) +++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java Sat Mar 18 10:56:26 2006 @@ -86,5 +86,18 @@ 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); + } } Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java?rev=386872&r1=386871&r2=386872&view=diff ============================================================================== --- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java (original) +++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java Sat Mar 18 10:56:26 2006 @@ -21,6 +21,7 @@ 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; /** @@ -35,6 +36,8 @@ { /** 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; @@ -87,6 +90,12 @@ paragraphStyle = pStyle; characterStyle = cStyle; } + /** + * Supply the SlideShow we belong to + */ + protected void supplySlideShow(SlideShow ss) { + slideShow = ss; + } /** * Get the length of the text @@ -126,6 +135,12 @@ // --------------- 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; } @@ -135,29 +150,139 @@ 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. Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java?rev=386872&r1=386871&r2=386872&view=diff ============================================================================== --- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java (original) +++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java Sat Mar 18 10:56:26 2006 @@ -60,6 +60,9 @@ // 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; @@ -89,7 +92,7 @@ // Find the versions of the core records we'll want to use findMostRecentCoreRecords(); - + // Build up the model level Slides and Notes buildSlidesAndNotes(); } @@ -194,6 +197,13 @@ } } } + + // 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 @@ 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++) { @@ -220,14 +228,11 @@ 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."); } @@ -251,7 +256,7 @@ // 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) { @@ -322,6 +327,16 @@ _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); + } + } } @@ -352,6 +367,16 @@ // 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); + } + } } } @@ -398,4 +423,13 @@ 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; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] Mailing List: http://jakarta.apache.org/site/mail2.html#poi The Apache Jakarta POI Project: http://jakarta.apache.org/poi/