]> source.dussan.org Git - poi.git/commitdiff
Make the code for adding a new RichTextRun to a TextRun a bit nicer
authorNick Burch <nick@apache.org>
Tue, 8 Jan 2008 21:35:53 +0000 (21:35 +0000)
committerNick Burch <nick@apache.org>
Tue, 8 Jan 2008 21:35:53 +0000 (21:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610169 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java
src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java

index fa9c72bb806f737428f43f5b11b3bb28b24f3ef1..586932e739803672388eda184a8e3909b000dbb5 100644 (file)
 
 package org.apache.poi.hslf.model;
 
-import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Vector;
 
 import org.apache.poi.hslf.model.textproperties.TextPropCollection;
-import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.record.Record;
+import org.apache.poi.hslf.record.RecordContainer;
+import org.apache.poi.hslf.record.StyleTextPropAtom;
+import org.apache.poi.hslf.record.TextBytesAtom;
+import org.apache.poi.hslf.record.TextCharsAtom;
+import org.apache.poi.hslf.record.TextHeaderAtom;
 import org.apache.poi.hslf.usermodel.RichTextRun;
 import org.apache.poi.hslf.usermodel.SlideShow;
 import org.apache.poi.util.StringUtil;
@@ -258,12 +262,15 @@ public class TextRun
         * Adds the supplied text onto the end of the TextRun, 
         *  creating a new RichTextRun (returned) for it to
         *  sit in. 
+        * In many cases, before calling this, you'll want to add
+        *  a newline onto the end of your last RichTextRun
         */
        public RichTextRun appendText(String s) {
                // We will need a StyleTextProp atom
                ensureStyleAtomPresent();
                
-               // First up, append the text
+               // First up, append the text to the 
+               //  underlying text atom
                int oldSize = getRawText().length();
                storeText(
                                getRawText() + s
@@ -272,21 +279,8 @@ public class TextRun
                // If either of the previous styles overran
                //  the text by one, we need to shuffle that
                //  extra character onto the new ones
-               Iterator it = _styleAtom.getParagraphStyles().iterator();
-               int pLen = 0;
-               while(it.hasNext()) {
-                       TextPropCollection tpc = (TextPropCollection)it.next();
-                       pLen += tpc.getCharactersCovered();
-               }
-               it = _styleAtom.getCharacterStyles().iterator();
-               int cLen = 0;
-               while(it.hasNext()) {
-                       TextPropCollection tpc = (TextPropCollection)it.next();
-                       cLen += tpc.getCharactersCovered();
-               }
-               int pOverRun = pLen - oldSize;
-               int cOverRun = cLen - oldSize;
-               
+               int pOverRun = _styleAtom.getParagraphTextLengthCovered() - oldSize;
+               int cOverRun = _styleAtom.getCharacterTextLengthCovered() - oldSize;
                if(pOverRun > 0) {
                        TextPropCollection tpc = (TextPropCollection)
                                _styleAtom.getParagraphStyles().getLast();
@@ -302,8 +296,7 @@ public class TextRun
                        );
                }
                
-               // Next, add the styles for its 
-               //  paragraph and characters
+               // Next, add the styles for its paragraph and characters
                TextPropCollection newPTP =
                        _styleAtom.addParagraphTextPropCollection(s.length()+pOverRun);
                TextPropCollection newCTP =
index 32f8b2bd03789b6c38a05b052573e3cf0db68049..fdaa9eec2c41f3a3134466bf430638ced4d42fb6 100644 (file)
 
 package org.apache.poi.hslf.record;
 
-import org.apache.poi.hslf.model.textproperties.*;
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.util.POILogger;
-
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.ByteArrayOutputStream;
-import java.util.LinkedList;
-import java.util.Vector;
-import java.util.List;
 import java.util.Iterator;
+import java.util.LinkedList;
+
+import org.apache.poi.hslf.model.textproperties.AlignmentTextProp;
+import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
+import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;
+import org.apache.poi.hslf.model.textproperties.TextProp;
+import org.apache.poi.hslf.model.textproperties.TextPropCollection;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.POILogger;
 
 /**
  * A StyleTextPropAtom (type 4001). Holds basic character properties 
@@ -88,6 +90,37 @@ public class StyleTextPropAtom extends RecordAtom
         *  character stylings
         */
        public void setCharacterStyles(LinkedList cs) { charStyles = cs; }
+       
+       /**
+        * Returns how many characters the paragraph's
+        *  TextPropCollections cover.
+        * (May be one or two more than the underlying text does,
+        *  due to having extra characters meaning something
+        *  special to powerpoint)
+        */
+       public int getParagraphTextLengthCovered() {
+               return getCharactersCovered(paragraphStyles);
+       }
+       /**
+        * Returns how many characters the character's
+        *  TextPropCollections cover.
+        * (May be one or two more than the underlying text does,
+        *  due to having extra characters meaning something
+        *  special to powerpoint)
+        */
+       public int getCharacterTextLengthCovered() {
+               return getCharactersCovered(charStyles);
+       }
+       private int getCharactersCovered(LinkedList styles) {
+               int length = 0;
+               Iterator it = styles.iterator();
+               while(it.hasNext()) {
+                       TextPropCollection tpc =
+                               (TextPropCollection)it.next();
+                       length += tpc.getCharactersCovered();
+               }
+               return length;
+       }
 
        /** All the different kinds of paragraph properties we might handle */
        public static TextProp[] paragraphTextPropTypes = new TextProp[] {
@@ -355,8 +388,7 @@ public class StyleTextPropAtom extends RecordAtom
                charStyles.add(tpc);
                return tpc;
        }
-
-
+       
 /* ************************************************************************ */
 
 
index d16bf7bc02303fa5af22f7cb9888a19e08f1aa11..fe03755079c2eb1da3507466e293ff30da82d542 100644 (file)
 
 package org.apache.poi.hslf.usermodel;
 
-import org.apache.poi.hslf.model.*;
+import java.awt.Color;
+
+import org.apache.poi.hslf.model.MasterSheet;
 import org.apache.poi.hslf.model.Shape;
-import org.apache.poi.hslf.model.textproperties.*;
+import org.apache.poi.hslf.model.Sheet;
+import org.apache.poi.hslf.model.TextRun;
+import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;
+import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
+import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;
+import org.apache.poi.hslf.model.textproperties.TextProp;
+import org.apache.poi.hslf.model.textproperties.TextPropCollection;
 import org.apache.poi.hslf.record.ColorSchemeAtom;
-import org.apache.poi.hslf.exceptions.HSLFException;
-
-import java.awt.*;
 
 
 /**
  * Represents a run of text, all with the same style
  * 
- * TODO: get access to the font/character properties
- *
- * @author Nick Burch
+ * TODO: finish all the getters and setters to the
+ *  font/character/paragraph properties (currently only
+ *  has some of them) 
  */
-
-public class RichTextRun
-{
+public class RichTextRun {
        /** The TextRun we belong to */
        private TextRun parentRun;
        /** The SlideShow we belong to */