diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java b/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java index 755dd4d98..44cee7c0e 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java @@ -84,6 +84,12 @@ public class TextBox extends SimpleShape { * TextHeaderAtom, TextBytesAtom ot TextCharsAtom, StyleTextPropAtom etc. */ protected EscherTextboxWrapper _txtbox; + + /** + * Is the TextBox missing the text records which actually + * store the text? + */ + private boolean _missingTextRecords = false; /** * Create a TextBox object and initialize it from the supplied Record container. @@ -544,10 +550,20 @@ public class TextBox extends SimpleShape { public void setSheet(Sheet sheet){ _sheet = sheet; - //initialize _txtrun object. - //we can't do it in the constructor because the sheet is not assigned yet + // Initialize _txtrun object. + // (We can't do it in the constructor because the sheet + // is not assigned then, it's only built once we have + // all the records) if(_txtrun == null) initTextRun(); - + if(_txtrun == null) { + // No text records found, skip + _missingTextRecords = true; + return; + } else { + _missingTextRecords = false; + } + + // Supply the sheet to our child RichTextRuns RichTextRun[] rt = _txtrun.getRichTextRuns(); for (int i = 0; i < rt.length; i++) { rt[i].supplySlideShow(_sheet.getSlideShow()); @@ -555,12 +571,13 @@ public class TextBox extends SimpleShape { } private void initTextRun(){ - TextHeaderAtom tha = null; TextCharsAtom tca = null; TextBytesAtom tba = null; StyleTextPropAtom sta = null; OutlineTextRefAtom ota = null; + + // Find the interesting child records Record[] child = _txtbox.getChildRecords(); for (int i = 0; i < child.length; i++) { if (child[i] instanceof TextHeaderAtom) tha = (TextHeaderAtom)child[i]; @@ -570,8 +587,10 @@ public class TextBox extends SimpleShape { else if (child[i] instanceof TextCharsAtom) tca = (TextCharsAtom)child[i]; } - if (ota != null){ - //TextHeaderAtom, TextBytesAtom and StyleTextPropAtom are stored outside of EscherContainerRecord + // Special handling for cases where there's an OutlineTextRefAtom + if (ota != null) { + // TextHeaderAtom, TextBytesAtom and StyleTextPropAtom are + // stored outside of EscherContainerRecord int idx = ota.getTextIndex(); Slide sl = (Slide)getSheet(); Record[] rec = sl.getSlideAtomsSet().getSlideRecords(); @@ -591,7 +610,17 @@ public class TextBox extends SimpleShape { } } } - if(tba != null) _txtrun = new TextRun(tha,tba,sta); - else if (tca != null) _txtrun = new TextRun(tha,tca,sta); + + // If we found the records we needed, create a TextRun + if(tba != null) { + // Bytes based Text Run + _txtrun = new TextRun(tha,tba,sta); + } else if (tca != null) { + // Characters (unicode) based Text Run + _txtrun = new TextRun(tha,tca,sta); + } else { + // Empty text box + System.err.println("Warning - no text records found for TextBox"); + } } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/data/empty_textbox.ppt b/src/scratchpad/testcases/org/apache/poi/hslf/data/empty_textbox.ppt new file mode 100644 index 000000000..403b2861e Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/empty_textbox.ppt differ diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java index 959593e45..6d2f1fcc3 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java @@ -33,11 +33,15 @@ import java.util.ArrayList; */ public class TestShapes extends TestCase { private SlideShow ppt; + private SlideShow pptB; protected void setUp() throws Exception { String dirname = System.getProperty("HSLF.testdata.path"); String filename = dirname + "/empty.ppt"; ppt = new SlideShow(new HSLFSlideShow(filename)); + + String filenameB = dirname + "/empty_textbox.ppt"; + pptB = new SlideShow(new HSLFSlideShow(filenameB)); } public void testGraphics() throws Exception { @@ -169,6 +173,19 @@ public class TestShapes extends TestCase { assertEquals("Arial", rt.getFontName()); assertEquals(Color.red, txtbox.getFontColor()); } + + /** + * Test with an empty text box + */ + public void testEmptyTextBox() throws Exception { + assertEquals(2, pptB.getSlides().length); + Slide s1 = pptB.getSlides()[0]; + Slide s2 = pptB.getSlides()[1]; + + // Check we can get the shapes count + assertEquals(2, s1.getShapes().length); + assertEquals(2, s2.getShapes().length); + } /** * If you iterate over text shapes in a slide and collect them in a set