Author: nick Date: Sun Feb 12 11:01:07 2006 New Revision: 377215 URL: http://svn.apache.org/viewcvs?rev=377215&view=rev Log: Start to support friendly usermodel interface to rich text character properties
Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java?rev=377215&r1=377214&r2=377215&view=diff ============================================================================== --- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java (original) +++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java Sun Feb 12 11:01:07 2006 @@ -285,9 +285,9 @@ /** * Ensure a StyleTextPropAtom is present for this run, - * by adding if required + * by adding if required. Normally for internal TextRun use. */ - private synchronized void ensureStyleAtomPresent() { + public synchronized void ensureStyleAtomPresent() { if(_styleAtom != null) { // All there return; Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java?rev=377215&r1=377214&r2=377215&view=diff ============================================================================== --- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java (original) +++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java Sun Feb 12 11:01:07 2006 @@ -84,8 +84,8 @@ public void setCharacterStyles(LinkedList cs) { charStyles = cs; } /** All the different kinds of paragraph properties we might handle */ - public TextProp[] paragraphTextPropTypes = new TextProp[] { - new BitMaskTextProp(2, 0xF, new String[] { + public static TextProp[] paragraphTextPropTypes = new TextProp[] { + new BitMaskTextProp(2, 0xF, "paragraph_flags", new String[] { "bullet", "bullet.hardfont", "bullet.hardcolor", "bullet.hardsize"} ), @@ -105,7 +105,7 @@ new TextProp(2, 0xA0000, "para_unknown_6") }; /** All the different kinds of character properties we might handle */ - public TextProp[] characterTextPropTypes = new TextProp[] { + public static TextProp[] characterTextPropTypes = new TextProp[] { new CharFlagsTextProp(), new TextProp(2, 0x10000, "font.index"), new TextProp(2, 0x20000, "font.size"), @@ -329,6 +329,48 @@ public int getCharactersCovered() { return charactersCovered; } /** Fetch the TextProps that define this styling */ public LinkedList getTextPropList() { return textPropList; } + + /** Fetch the TextProp with this name, or null if it isn't present */ + public TextProp findByName(String textPropName) { + for(int i=0; i<textPropList.size(); i++) { + TextProp prop = (TextProp)textPropList.get(i); + if(prop.getName().equals(textPropName)) { + return prop; + } + } + return null; + } + + /** Add the TextProp with this name to the list */ + public TextProp addWithName(String name) { + // Find the base TextProp to base on + TextProp base = null; + for(int i=0; i < StyleTextPropAtom.characterTextPropTypes.length; i++) { + if(StyleTextPropAtom.characterTextPropTypes[i].getName().equals(name)) { + base = StyleTextPropAtom.characterTextPropTypes[i]; + } + } + for(int i=0; i < StyleTextPropAtom.paragraphTextPropTypes.length; i++) { + if(StyleTextPropAtom.paragraphTextPropTypes[i].getName().equals(name)) { + base = StyleTextPropAtom.paragraphTextPropTypes[i]; + } + } + if(base == null) { + throw new IllegalArgumentException("No TextProp with name " + name + " is defined to add from"); + } + + // Add a copy of this property, in the right place to the list + TextProp textProp = (TextProp)base.clone(); + int pos = 0; + for(int i=0; i<textPropList.size(); i++) { + TextProp curProp = (TextProp)textPropList.get(i); + if(textProp.getMask() > curProp.getMask()) { + pos++; + } + } + textPropList.add(pos, textProp); + return textProp; + } /** * Create a new collection of text properties (be they paragraph @@ -504,9 +546,10 @@ /** Fetch the list of if the sub properties match or not */ public boolean[] getSubPropMatches() { return subPropMatches; } - private BitMaskTextProp(int sizeOfDataBlock, int maskInHeader, String[] subPropNames) { + private BitMaskTextProp(int sizeOfDataBlock, int maskInHeader, String overallName, String[] subPropNames) { super(sizeOfDataBlock,maskInHeader,"bitmask"); this.subPropNames = subPropNames; + this.propName = overallName; subPropMasks = new int[subPropNames.length]; subPropMatches = new boolean[subPropNames.length]; } @@ -545,6 +588,7 @@ } else { dataValue -= subPropMasks[idx]; } + subPropMatches[idx] = value; } public Object clone(){ @@ -575,7 +619,7 @@ public static final int ENABLE_NUMBERING_2_IDX = 12; private CharFlagsTextProp() { - super(2,0xffff, new String[] { + super(2,0xffff, "char_flags", new String[] { "bold", // 0x0001 "italic", // 0x0002 "underline", // 0x0004 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=377215&r1=377214&r2=377215&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 Sun Feb 12 11:01:07 2006 @@ -20,6 +20,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.TextPropCollection; /** @@ -123,6 +124,39 @@ startPos = startAt; } + + // --------------- Internal helpers on rich text properties ------- + private boolean isCharFlagsTextPropVal(int index) { + if(characterStyle == null) { return false; } + + CharFlagsTextProp cftp = (CharFlagsTextProp) + characterStyle.findByName("char_flags"); + + if(cftp == null) { return false; } + return cftp.getSubValue(index); + } + private void setCharFlagsTextPropVal(int index, boolean value) { + if(characterStyle == null) { + parentRun.ensureStyleAtomPresent(); + } + + CharFlagsTextProp cftp = (CharFlagsTextProp) + characterStyle.findByName("char_flags"); + if(cftp == null) { + cftp = (CharFlagsTextProp)characterStyle.addWithName("char_flags"); + } + + cftp.setSubValue(value,index); + } + + // --------------- 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); + } /** --------------------------------------------------------------------- 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/