From 6602cb2cfbecd014c1d3779cd1f35fc2782c86f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Wed, 11 Dec 2019 00:10:09 +0000 Subject: [PATCH] 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 --- .../poi/xddf/usermodel/text/XDDFTextBody.java | 2 +- .../poi/xslf/usermodel/XSLFAutoShape.java | 2 -- .../apache/poi/xslf/usermodel/XSLFTable.java | 5 ++-- .../poi/xslf/usermodel/XSLFTableCell.java | 2 -- .../poi/xslf/usermodel/XSLFTextBox.java | 2 +- .../poi/xslf/usermodel/XSLFTextRun.java | 11 +++++--- .../poi/xslf/usermodel/XSLFTextShape.java | 9 ++---- .../poi/sl/draw/TestDrawPictureShape.java | 14 +++++----- .../text/TestXDDFTextBodyProperties.java | 4 ++- .../xddf/usermodel/text/TestXDDFTextRun.java | 1 + .../org/apache/poi/xslf/TestXSLFBugs.java | 11 ++++---- .../poi/xslf/usermodel/TestPPTX2PNG.java | 10 +++---- .../poi/xslf/usermodel/TestXMLSlideShow.java | 28 +++++++++---------- .../poi/xslf/usermodel/TestXSLFAutoShape.java | 14 ++++++---- .../xslf/usermodel/TestXSLFPictureShape.java | 13 ++++----- 15 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java index 19555cd884..6eae7b88c2 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java @@ -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); diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFAutoShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFAutoShape.java index 744ccfd175..a69af0e0e7 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFAutoShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFAutoShape.java @@ -78,10 +78,8 @@ public class XSLFAutoShape extends XSLFTextShape implements AutoShape 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")) { diff --git a/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextBodyProperties.java b/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextBodyProperties.java index 3b137d656c..e93f2bdc3d 100644 --- a/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextBodyProperties.java +++ b/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextBodyProperties.java @@ -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); diff --git a/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextRun.java b/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextRun.java index 67d509fa42..e162ba18fb 100644 --- a/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextRun.java +++ b/src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextRun.java @@ -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()); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java index da25fc77e8..a9a9e16b7c 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java @@ -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 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"); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java index 0c754437b0..713b2f2b84 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java @@ -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 data() { diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java index bb6167ee2f..715daa47c8 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java @@ -40,7 +40,7 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListE public class TestXMLSlideShow extends BaseTestSlideShow { private OPCPackage pack; - + @Override public XMLSlideShow createSlideShow() { return new XMLSlideShow(); @@ -50,7 +50,7 @@ public class TestXMLSlideShow extends BaseTestSlideShow show) { try { diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFAutoShape.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFAutoShape.java index d7869d722b..7e7d708ced 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFAutoShape.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFAutoShape.java @@ -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(); } } \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java index a2caca6feb..c80fafef18 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java @@ -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(); } -- 2.39.5