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
This commit is contained in:
Yegor Kozlov 2007-12-24 09:51:14 +00:00
parent d151b35bc8
commit ca66f610e4
5 changed files with 64 additions and 4 deletions

View 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++;
}
}
}

View 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);
}

View 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
*

Binary file not shown.

View File

@ -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());
}
}
}