]> source.dussan.org Git - poi.git/commitdiff
fix bug #43781: slide->getShapes->getTextRun returns wrong text
authorYegor Kozlov <yegor@apache.org>
Mon, 24 Dec 2007 09:51:14 +0000 (09:51 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 24 Dec 2007 09:51:14 +0000 (09:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@606685 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java
src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java
src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java
src/scratchpad/testcases/org/apache/poi/hslf/data/43781.ppt [new file with mode: 0755]
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java

index b1761b2d9a50b077015434da8c74b8c2a8a444bf..d8ddc7d2f2e9013b6c7cde4cf9d04cd1edba782e 100644 (file)
@@ -153,7 +153,7 @@ public abstract class Sheet {
      */
     protected static void findTextRuns(Record[] records, Vector found) {
         // Look for a TextHeaderAtom
-        for (int i = 0; i < (records.length - 1); i++) {
+        for (int i = 0, slwtIndex=0; i < (records.length - 1); i++) {
             if (records[i] instanceof TextHeaderAtom) {
                 TextRun trun = null;
                 TextHeaderAtom tha = (TextHeaderAtom) records[i];
@@ -179,7 +179,6 @@ public abstract class Sheet {
                     // TextSpecInfoAtom - Safe to ignore
                 } else {
                     System.err.println("Found a TextHeaderAtom not followed by a TextBytesAtom or TextCharsAtom: Followed by " + records[i + 1].getRecordType());
-                    continue;
                 }
 
                 if (trun != null) {
@@ -191,12 +190,14 @@ public abstract class Sheet {
                     Record[] recs = new Record[lst.size()];
                     lst.toArray(recs);
                     trun._records = recs;
+                    trun.setIndex(slwtIndex);
                     
                     found.add(trun);
                     i++;
                 } else {
                     // Not a valid one, so skip on to next and look again
                 }
+                slwtIndex++;
             }
         }
     }
index 2d5c866f775534639fc5b08fbdab89415bae3b2a..1649b63bad3cb3a795d186d47db1a85c75d2a16c 100644 (file)
@@ -196,7 +196,7 @@ public class TextBox extends SimpleShape {
         } catch (IOException e){
             throw new HSLFException(e);
         }
-        if(getAnchor().equals(new java.awt.Rectangle())) resizeToFitText();
+        if(getAnchor().equals(new java.awt.Rectangle()) && !"".equals(getText())) resizeToFitText();
     }
 
     /**
@@ -264,6 +264,14 @@ public class TextBox extends SimpleShape {
         EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
         setEscherProperty(opt, EscherProperties.TEXT__ANCHORTEXT, align);
     }
+
+    public void setHorizontalAlignment(int align){
+        _txtrun.getRichTextRuns()[0].setAlignment(align);
+    }
+    public int getHorizontalAlignment(){
+        return _txtrun.getRichTextRuns()[0].getAlignment();
+    }
+
     /**
      * Returns the distance (in points) between the bottom of the text frame
      * and the bottom of the inscribed rectangle of the shape that contains the text.
@@ -466,7 +474,11 @@ public class TextBox extends SimpleShape {
         TextRun[] runs = sheet.getTextRuns();
         if (ota != null) {
             int idx = ota.getTextIndex();
-            if(idx < runs.length) _txtrun = runs[idx];
+            for (int i = 0; i < runs.length; i++) {
+                if(runs[i].getIndex() == idx){
+                    _txtrun = runs[i];
+                }
+            }
             if(_txtrun == null) {
                 logger.log(POILogger.WARN, "text run not found for OutlineTextRefAtom.TextIndex=" + idx);
             }
index 4999df758168f6342fbd26c7a4ea7b57215afa84..ca6e02d69b0568671c3ad3e1a4821f7ac5171688 100644 (file)
@@ -50,6 +50,7 @@ public class TextRun
        private SlideShow slideShow;
     private Sheet sheet;
     private int shapeId;
+    private int slwtIndex; //position in the owning SlideListWithText
     /**
      * all text run records that follow TextHeaderAtom.
      * (there can be misc InteractiveInfo, TxInteractiveInfo and other records)
@@ -537,6 +538,20 @@ public class TextRun
         shapeId = id;
     }
 
+    /**
+     * @return  0-based index of the text run in the SLWT container
+     */
+    protected int getIndex(){
+        return slwtIndex;
+    }
+
+    /**
+     *  @param id 0-based index of the text run in the SLWT container
+     */
+    protected void setIndex(int id){
+        slwtIndex = id;
+    }
+
     /**
      * Returns the array of all hyperlinks in this text run
      *
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/data/43781.ppt b/src/scratchpad/testcases/org/apache/poi/hslf/data/43781.ppt
new file mode 100755 (executable)
index 0000000..ddea91f
Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/43781.ppt differ
index 933aa176b7fcdbae347580271ee8391b9b643018..996a733ac981a5ea444cb099a0416bbad5ea9f98 100644 (file)
@@ -25,6 +25,7 @@ import org.apache.poi.hslf.model.Shape;
 import java.io.*;\r
 import java.util.HashSet;\r
 import java.util.HashMap;\r
+import java.util.ArrayList;\r
 import java.awt.*;\r
 \r
 /**\r
@@ -298,4 +299,35 @@ public class TestBugs extends TestCase {
 \r
     }\r
 \r
+    /**\r
+     * Bug 38256:  RuntimeException: Couldn't instantiate the class for type with id 0.\r
+     * ( also fixed followup: getTextRuns() returns no text )\r
+     */\r
+    public void test43781 () throws Exception {\r
+        FileInputStream is = new FileInputStream(new File(cwd, "43781.ppt"));\r
+        SlideShow ppt = new SlideShow(is);\r
+        is.close();\r
+\r
+        assertTrue("No Exceptions while reading file", true);\r
+\r
+        Slide slide = ppt.getSlides()[0];\r
+        TextRun[] tr1 = slide.getTextRuns();\r
+\r
+        ArrayList lst = new ArrayList();\r
+        Shape[] shape = slide.getShapes();\r
+        for (int i = 0; i < shape.length; i++) {\r
+            if( shape[i] instanceof TextBox){\r
+                TextRun textRun = ((TextBox)shape[i]).getTextRun();\r
+                if(textRun != null) lst.add(textRun);\r
+            }\r
+\r
+        }\r
+        TextRun[] tr2 = new TextRun[lst.size()];\r
+        lst.toArray(tr2);\r
+\r
+        assertEquals(tr1.length, tr2.length);\r
+        for (int i = 0; i < tr1.length; i++) {\r
+            assertEquals(tr1[i].getText(), tr2[i].getText());\r
+        }\r
+    }\r
 }\r