Fix initialization of text body when used in XSLF

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871150 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2019-12-11 00:10:09 +00:00
parent 37a5888565
commit 6602cb2cfb
15 changed files with 63 additions and 65 deletions

View File

@ -38,7 +38,6 @@ public class XDDFTextBody {
public XDDFTextBody(TextContainer parent) {
this(parent, CTTextBody.Factory.newInstance());
initialize();
}
@Internal
@ -64,6 +63,7 @@ public class XDDFTextBody {
bp.setRightToLeft(false);
XDDFTextParagraph p = addNewParagraph();
p.setTextAlignment(TextAlignment.LEFT);
p.appendRegularRun("");
XDDFRunProperties end = p.addAfterLastRunProperties();
end.setLanguage(Locale.US);
end.setFontSize(11.0);

View File

@ -78,10 +78,8 @@ public class XSLFAutoShape extends XSLFTextShape implements AutoShape<XSLFShape,
CTTextBody txBody = shape.getTxBody();
if (txBody == null && create) {
XDDFTextBody body = new XDDFTextBody(this);
initTextBody(body);
shape.setTxBody(body.getXmlObject());
txBody = shape.getTxBody();
getTextParagraphs().add(newTextParagraph(txBody.getPArray(0)));
}
return txBody;
}

View File

@ -33,6 +33,7 @@ import org.apache.poi.sl.draw.DrawTextShape;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
@ -181,7 +182,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
col.setW(width);
for(XSLFTableRow row : _rows) {
XSLFTableCell cell = row.addCell();
cell.getTextBody(true);
new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
}
updateRowColIndexes();
}
@ -200,7 +201,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
col.setW(width);
for(XSLFTableRow row : _rows) {
XSLFTableCell cell = row.insertCell(colIdx);
cell.getTextBody(true);
new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
}
updateRowColIndexes();
}

View File

@ -89,10 +89,8 @@ public class XSLFTableCell extends XSLFTextShape implements TableCell<XSLFShape,
CTTextBody txBody = cell.getTxBody();
if (txBody == null && create) {
XDDFTextBody body = new XDDFTextBody(this);
initTextBody(body);
cell.setTxBody(body.getXmlObject());
txBody = cell.getTxBody();
getTextParagraphs().add(newTextParagraph(txBody.getPArray(0)));
}
return txBody;
}

View File

@ -57,7 +57,7 @@ public class XSLFTextBox extends XSLFAutoShape implements TextBox<XSLFShape, XSL
prst.setPrst(STShapeType.RECT);
prst.addNewAvLst();
XDDFTextBody body = new XDDFTextBody(null);
initTextBody(body);
body.initialize();
ct.setTxBody(body.getXmlObject());
return ct;

View File

@ -28,9 +28,9 @@ import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.sl.draw.DrawPaint;
import org.apache.poi.sl.usermodel.PaintStyle;
import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
import org.apache.poi.sl.usermodel.TextParagraph;
import org.apache.poi.sl.usermodel.TextRun;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xslf.model.CharacterPropertyFetcher;
@ -101,6 +101,7 @@ public class XSLFTextRun implements TextRun {
*
* @return the xmlbeans object
*/
@Internal
public XmlObject getXmlObject(){
return _r;
}
@ -558,7 +559,9 @@ public class XSLFTextRun implements TextRun {
Double srcFontSize = r.getFontSize();
if (srcFontSize == null) {
if (getFontSize() != null) setFontSize(null);
if (getFontSize() != null) {
setFontSize(null);
}
} else if(!srcFontSize.equals(getFontSize())) {
setFontSize(srcFontSize);
}
@ -644,7 +647,7 @@ public class XSLFTextRun implements TextRun {
}
return;
}
CTTextCharacterProperties props = getRPr(false);
if (props == null) {
return;
@ -811,7 +814,7 @@ public class XSLFTextRun implements TextRun {
font = coll.getLatin();
}
// SYMBOL is missing
if (font == null || !font.isSetTypeface() || "".equals(font.getTypeface())) {
font = coll.getLatin();
}

View File

@ -38,7 +38,6 @@ import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.text.TextContainer;
import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
import org.apache.poi.xddf.usermodel.text.XDDFTextParagraph;
import org.apache.poi.xslf.model.PropertyFetcher;
import org.apache.poi.xslf.model.TextBodyPropertyFetcher;
import org.apache.xmlbeans.XmlObject;
@ -72,11 +71,6 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
}
}
protected static void initTextBody(XDDFTextBody body) {
XDDFTextParagraph p = body.getParagraph(0);
p.appendRegularRun("");
}
@Beta
public XDDFTextBody getTextBody() {
CTTextBody txBody = getTextBody(false);
@ -213,6 +207,7 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
CTTextParagraph p;
if (txBody == null) {
txBody = getTextBody(true);
new XDDFTextBody(this, txBody).initialize();
p = txBody.getPArray(0);
p.removeR(0);
} else {
@ -669,11 +664,11 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
XSLFTextShape otherTS = (XSLFTextShape) other;
CTTextBody otherTB = otherTS.getTextBody(false);
CTTextBody thisTB = getTextBody(true);
if (otherTB == null) {
return;
}
CTTextBody thisTB = getTextBody(true);
thisTB.setBodyPr((CTTextBodyProperties) otherTB.getBodyPr().copy());
if (thisTB.isSetLstStyle()) {

View File

@ -52,7 +52,7 @@ public class TestDrawPictureShape {
xslfOnly = true;
}
}
/** a generic way to open a sample slideshow document **/
public static SlideShow<?,?> openSampleDocument(String sampleName) throws IOException {
InputStream is = ssSamples.openResourceAsStream(sampleName);
@ -70,16 +70,16 @@ public class TestDrawPictureShape {
assumeFalse(xslfOnly);
testResize("pictures.ppt");
}
@Test
public void testResizeXSLF() throws IOException {
testResize("shapes.pptx");
}
public void testResize(String file) throws IOException {
SlideShow<?,?> ss = openSampleDocument(file);
Slide<?,?> slide = ss.getSlides().get(0);
PictureShape<?,?> picShape = null;
for (Shape<?,?> shape : slide.getShapes()) {
@ -97,7 +97,7 @@ public class TestDrawPictureShape {
(int)picShape.getAnchor().getHeight()
);
assertEquals(dimPd, dimShape);
double newWidth = (dimPd.getWidth()*(100d/dimPd.getHeight()));
// ... -1 is a rounding error
Rectangle2D expRect = new Rectangle2D.Double(rbf(50+300-newWidth, picShape), 50, rbf(newWidth, picShape), 100);
@ -110,7 +110,7 @@ public class TestDrawPictureShape {
assertEquals(expRect.getHeight(), actRect.getHeight(), .0001);
ss.close();
}
// round back and forth - points -> master -> points
static double rbf(double val, PictureShape<?,?> picShape) {
if (picShape.getClass().getName().contains("HSLF")) {

View File

@ -30,7 +30,9 @@ public class TestXDDFTextBodyProperties {
@Test
public void testProperties() throws IOException {
XDDFBodyProperties body = new XDDFTextBody(null).getBodyProperties();
XDDFTextBody text = new XDDFTextBody(null);
text.initialize();
XDDFBodyProperties body = text.getBodyProperties();
CTTextBodyProperties props = body.getXmlObject();
body.setBottomInset(null);

View File

@ -46,6 +46,7 @@ public class TestXDDFTextRun {
sh.addNewTextParagraph();
XDDFTextBody body = sh.getTextBody();
body.initialize();
XDDFTextParagraph para = body.getParagraph(0);
XDDFTextRun r = para.appendRegularRun("text");
assertEquals(LocaleUtil.getUserLocale().toLanguageTag(), r.getLanguage().toLanguageTag());

View File

@ -84,7 +84,6 @@ import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
public class TestXSLFBugs {
private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
@ -294,18 +293,18 @@ public class TestXSLFBugs {
is.close();
PackagePartName ppn = PackagingURIHelper.createPartName("/ppt/media/image1.png");
XMLSlideShow ppt1 = new XMLSlideShow(new ByteArrayInputStream(buf));
XSLFSlide slide1 = ppt1.getSlides().get(0);
Optional<XSLFShape> shapeToDelete1 =
slide1.getShapes().stream().filter(s -> s instanceof XSLFPictureShape).findFirst();
assertTrue(shapeToDelete1.isPresent());
slide1.removeShape(shapeToDelete1.get());
assertTrue(slide1.getRelationParts().stream()
.allMatch(rp -> "rId1,rId3".contains(rp.getRelationship().getId()) ));
assertNotNull(ppt1.getPackage().getPart(ppn));
ppt1.close();
@ -330,7 +329,7 @@ public class TestXSLFBugs {
assertNull(ppt3.getPackage().getPart(ppn));
ppt3.close();
}
@Test
public void bug51187() throws Exception {
XMLSlideShow ss1 = XSLFTestDataSamples.openSampleDocument("51187.pptx");

View File

@ -37,7 +37,7 @@ import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
/**
* Test class for testing PPTX2PNG utility which renderes .ppt and .pptx slideshows
* Test class for testing PPTX2PNG utility which renders .ppt and .pptx slideshows
*/
@RunWith(Parameterized.class)
public class TestPPTX2PNG {
@ -49,8 +49,8 @@ public class TestPPTX2PNG {
"backgrounds.pptx, layouts.pptx, sample.pptx, shapes.pptx, 54880_chinese.ppt, keyframes.pptx," +
"customGeo.pptx, customGeo.ppt, wrench.emf, santa.wmf";
@BeforeClass
public static void checkHslf() {
try {
@ -59,11 +59,11 @@ public class TestPPTX2PNG {
xslfOnly = true;
}
}
// use filename instead of File object to omit full pathname in test name
@Parameter(value = 0)
public String pptFile;
@SuppressWarnings("ConstantConditions")
@Parameters(name="{0}")
public static Collection<String> data() {

View File

@ -40,7 +40,7 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListE
public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagraph> {
private OPCPackage pack;
@Override
public XMLSlideShow createSlideShow() {
return new XMLSlideShow();
@ -50,7 +50,7 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
public void setUp() throws Exception {
pack = OPCPackage.open(slTests.openResourceAsStream("sample.pptx"));
}
@After
public void tearDown() {
pack.revert();
@ -77,7 +77,7 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
// Check it has some slides
assertFalse(xml.getSlides().isEmpty());
assertFalse(xml.getSlideMasters().isEmpty());
xml.close();
}
@ -122,10 +122,10 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
assertNotNull(notesMaster);
assertNotNull(xml.getNotesMaster());
xml.close();
}
@Test
public void testMetadataBasics() throws IOException {
XMLSlideShow xml = new XMLSlideShow(pack);
@ -139,34 +139,34 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
assertNull(xml.getProperties().getCoreProperties().getTitle());
assertFalse(xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().isPresent());
xml.close();
}
@Test
public void testComments() throws Exception {
// Default sample file has none
XMLSlideShow xml = new XMLSlideShow(pack);
assertNull(xml.getCommentAuthors());
for (XSLFSlide slide : xml.getSlides()) {
assertTrue(slide.getComments().isEmpty());
}
// Try another with comments
XMLSlideShow xmlComments = new XMLSlideShow(slTests.openResourceAsStream("45545_Comment.pptx"));
// Has one author
assertNotNull(xmlComments.getCommentAuthors());
assertEquals(1, xmlComments.getCommentAuthors().getCTCommentAuthorsList().sizeOfCmAuthorArray());
assertEquals("XPVMWARE01", xmlComments.getCommentAuthors().getAuthorById(0).getName());
// First two slides have comments
int i = -1;
for (XSLFSlide slide : xmlComments.getSlides()) {
i++;
if(i == 0) {
assertNotNull(slide.getCommentsPart());
assertEquals(1, slide.getCommentsPart().getNumberOfComments());
@ -182,11 +182,11 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
assertTrue(slide.getComments().isEmpty());
}
}
xmlComments.close();
xml.close();
}
@Override
public XMLSlideShow reopen(SlideShow<XSLFShape,XSLFTextParagraph> show) {
try {

View File

@ -108,7 +108,7 @@ public class TestXSLFAutoShape {
assertEquals(TextDirection.VERTICAL, shape.getTextDirection());
shape.setTextDirection(null);
assertEquals(TextDirection.HORIZONTAL, shape.getTextDirection());
ppt.close();
}
@ -211,7 +211,7 @@ public class TestXSLFAutoShape {
p.setTextAlign(null);
assertEquals(TextAlign.LEFT, p.getTextAlign());
assertFalse(p.getXmlObject().getPPr().isSetAlgn());
ppt.close();
}
@ -231,7 +231,7 @@ public class TestXSLFAutoShape {
assertEquals(1, p.getTextRuns().size());
assertSame(r, p.getTextRuns().get(0));
assertEquals(18.0, r.getFontSize(), 0); // default font size for text boxes
assertFalse(rPr.isSetSz());
r.setFontSize(10.0);
@ -285,7 +285,7 @@ public class TestXSLFAutoShape {
assertEquals("POI", r.getRawText());
r.setText(null);
assertNull(r.getRawText());
ppt.close();
}
@ -301,11 +301,13 @@ public class TestXSLFAutoShape {
assertEquals(ShapeType.TRIANGLE, shape.getShapeType());
for(ShapeType tp : ShapeType.values()) {
if (tp.ooxmlId == -1 || tp == ShapeType.SEAL) continue;
if (tp.ooxmlId == -1 || tp == ShapeType.SEAL) {
continue;
}
shape.setShapeType(tp);
assertEquals(tp, shape.getShapeType());
}
ppt.close();
}
}

View File

@ -16,7 +16,6 @@
==================================================================== */
package org.apache.poi.xslf.usermodel;
import static org.apache.poi.POIDataSamples.TEST_PROPERTY;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -226,31 +225,31 @@ public class TestXSLFPictureShape {
InputStream is = _slTests.openResourceAsStream("shapes.pptx");
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
XSLFSlide slide = ppt.getSlides().get(0);
XSLFPictureShape ps = (XSLFPictureShape)slide.getShapes().get(3);
slide.removeShape(ps);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ppt.write(bos);
ppt.close();
XMLSlideShow ppt2 = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray()));
assertTrue(ppt2.getPictureData().isEmpty());
ppt2.close();
}
@Test
public void testTiffImageBug59742() throws Exception {
XMLSlideShow slideShow = new XMLSlideShow();
final InputStream tiffStream = _slTests.openResourceAsStream("testtiff.tif");
final byte[] pictureData = IOUtils.toByteArray(tiffStream);
IOUtils.closeQuietly(tiffStream);
XSLFPictureData pic = slideShow.addPicture(pictureData, PictureType.TIFF);
assertEquals("image/tiff", pic.getContentType());
assertEquals("image1.tiff", pic.getFileName());
slideShow.close();
}