From 7ce58ff9d184515c105ca6dedb49b7027161492f Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 18 Mar 2006 18:56:26 +0000 Subject: [PATCH] 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 --- .../poi/hslf/record/FontCollection.java | 15 +- .../poi/hslf/usermodel/RichTextRun.java | 137 +++++++++++++++++- .../apache/poi/hslf/usermodel/SlideShow.java | 52 +++++-- 3 files changed, 188 insertions(+), 16 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java index c6ee18e53..44b584e75 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java @@ -86,5 +86,18 @@ public class FontCollection extends RecordContainer { 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); + } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java index ac4182121..b94250e6c 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java @@ -21,6 +21,7 @@ package org.apache.poi.hslf.usermodel; 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 @@ public class RichTextRun { /** 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 @@ public class RichTextRun 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 @@ public class RichTextRun // --------------- 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 @@ public class RichTextRun 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; } + + 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. diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java index 23108d478..db024e798 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java @@ -60,6 +60,9 @@ public class SlideShow // 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 @@ public class SlideShow // 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 @@ 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); // 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 @@ public class SlideShow 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 @@ public class SlideShow // 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