From: Yegor Kozlov Date: Mon, 24 Dec 2007 09:51:14 +0000 (+0000) Subject: fix bug #43781: slide->getShapes->getTextRun returns wrong text X-Git-Tag: REL_3_0_3_BETA1~245 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ba9a2dc400a148c6b2ae32aa8cb11af30ef5572d;p=poi.git fix bug #43781: slide->getShapes->getTextRun returns wrong text git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@606685 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java b/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java index b1761b2d9a..d8ddc7d2f2 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java @@ -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++; } } } 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 2d5c866f77..1649b63bad 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java @@ -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); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java b/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java index 4999df7581..ca6e02d69b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java @@ -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 index 0000000000..ddea91fe89 Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/43781.ppt differ diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java index 933aa176b7..996a733ac9 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java @@ -25,6 +25,7 @@ import org.apache.poi.hslf.model.Shape; import java.io.*; import java.util.HashSet; import java.util.HashMap; +import java.util.ArrayList; import java.awt.*; /** @@ -298,4 +299,35 @@ public class TestBugs extends TestCase { } + /** + * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0. + * ( also fixed followup: getTextRuns() returns no text ) + */ + public void test43781 () throws Exception { + FileInputStream is = new FileInputStream(new File(cwd, "43781.ppt")); + SlideShow ppt = new SlideShow(is); + is.close(); + + assertTrue("No Exceptions while reading file", true); + + Slide slide = ppt.getSlides()[0]; + TextRun[] tr1 = slide.getTextRuns(); + + ArrayList lst = new ArrayList(); + Shape[] shape = slide.getShapes(); + for (int i = 0; i < shape.length; i++) { + if( shape[i] instanceof TextBox){ + TextRun textRun = ((TextBox)shape[i]).getTextRun(); + if(textRun != null) lst.add(textRun); + } + + } + TextRun[] tr2 = new TextRun[lst.size()]; + lst.toArray(tr2); + + assertEquals(tr1.length, tr2.length); + for (int i = 0; i < tr1.length; i++) { + assertEquals(tr1[i].getText(), tr2[i].getText()); + } + } }