diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java index d30bfa5521..f84c37d08e 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java @@ -735,27 +735,44 @@ public class XSLFTableCell extends XSLFTextShape implements TableCell shapes = slide.getShapes(); + assertEquals(1, shapes.size()); + assertTrue(shapes.get(0) instanceof XSLFTable); + XSLFTable tbl = (XSLFTable)shapes.get(0); + assertEquals(1, tbl.getNumberOfColumns()); + assertEquals(2, tbl.getNumberOfRows()); + + List rows = tbl.getRows(); + assertEquals(2, rows.size()); + + // First row has 1 col and 1 runs + XSLFTableRow row0 = rows.get(0); + List cells0 = row0.getCells(); + assertEquals(1, cells0.size()); + + List paras0 = cells0.get(0).getTextParagraphs(); + assertEquals(1, paras0.size()); + + List runs0 = paras0.get(0).getTextRuns(); + assertEquals(1, runs0.size()); + + // IMPORTANT -> this should not be a normal text run (was a bug previously) + XSLFTextRun run00 = runs0.get(0); + assertEquals("XSLFCellTextRun", run00.getClass().getSimpleName()); + assertFalse(run00.isBold()); + assertFalse(run00.isItalic()); + assertNotNull(run00.getFontColor()); + assertTrue(run00.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run00.getFontColor()).getSolidColor().getColor()); + + // Second row has 1 col and 3 runs + XSLFTableRow row1 = rows.get(1); + List cells1 = row1.getCells(); + assertEquals(1, cells1.size()); + + List paras1 = cells1.get(0).getTextParagraphs(); + assertEquals(1, paras1.size()); + + List runs1 = paras1.get(0).getTextRuns(); + assertEquals(3, runs1.size()); + + // IMPORTANT -> this should not be a normal text run (was a bug previously) + XSLFTextRun run10 = runs1.get(0); + assertEquals("XSLFCellTextRun", run10.getClass().getSimpleName()); + assertTrue(run10.isBold()); + assertFalse(run10.isItalic()); + assertNotNull(run10.getFontColor()); + assertTrue(run10.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run10.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run11 = runs1.get(1); + assertEquals("XSLFCellTextRun", run11.getClass().getSimpleName()); + assertFalse(run11.isBold()); + assertFalse(run11.isItalic()); + assertNotNull(run11.getFontColor()); + assertTrue(run11.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run11.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run12 = runs1.get(2); + assertEquals("XSLFCellTextRun", run12.getClass().getSimpleName()); + assertFalse(run12.isBold()); + assertTrue(run12.isItalic()); + assertNotNull(run12.getFontColor()); + assertTrue(run12.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run12.getFontColor()).getSolidColor().getColor()); + + ppt.close(); + } + + @Test + void testCorrectlyReadsTextRunStylingForCellsWithTheme() throws IOException { + XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("table-with-theme.pptx"); + + XSLFSlide slide = ppt.getSlides().get(0); + List shapes = slide.getShapes(); + assertEquals(1, shapes.size()); + assertTrue(shapes.get(0) instanceof XSLFTable); + XSLFTable tbl = (XSLFTable)shapes.get(0); + assertEquals(1, tbl.getNumberOfColumns()); + assertEquals(2, tbl.getNumberOfRows()); + + List rows = tbl.getRows(); + assertEquals(2, rows.size()); + + // First row has 1 col and 3 runs + XSLFTableRow row0 = rows.get(0); + List cells0 = row0.getCells(); + assertEquals(1, cells0.size()); + + List paras0 = cells0.get(0).getTextParagraphs(); + assertEquals(1, paras0.size()); + + List runs0 = paras0.get(0).getTextRuns(); + assertEquals(3, runs0.size()); + + // IMPORTANT -> this should not be a normal text run (was a bug previously) + XSLFTextRun run00 = runs0.get(0); + assertEquals("XSLFCellTextRun", run00.getClass().getSimpleName()); + assertTrue(run00.isBold()); + assertFalse(run00.isItalic()); + assertNotNull(run00.getFontColor()); + assertTrue(run00.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.white, ((PaintStyle.SolidPaint)run00.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run01 = runs0.get(1); + assertEquals("XSLFCellTextRun", run01.getClass().getSimpleName()); + assertTrue(run01.isBold()); + assertFalse(run01.isItalic()); + assertNotNull(run01.getFontColor()); + assertTrue(run01.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.white, ((PaintStyle.SolidPaint)run01.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run02 = runs0.get(2); + assertEquals("XSLFCellTextRun", run02.getClass().getSimpleName()); + assertFalse(run02.isBold()); + assertFalse(run02.isItalic()); + assertNotNull(run02.getFontColor()); + assertTrue(run02.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.red, ((PaintStyle.SolidPaint)run02.getFontColor()).getSolidColor().getColor()); + + // Second row has 1 col and 7 runs + XSLFTableRow row1 = rows.get(1); + List cells1 = row1.getCells(); + assertEquals(1, cells1.size()); + + List paras1 = cells1.get(0).getTextParagraphs(); + assertEquals(1, paras1.size()); + + List runs1 = paras1.get(0).getTextRuns(); + assertEquals(7, runs1.size()); + + // IMPORTANT -> this should not be a normal text run (was a bug previously) + XSLFTextRun run10 = runs1.get(0); + assertEquals("XSLFCellTextRun", run10.getClass().getSimpleName()); + assertTrue(run10.isBold()); + assertFalse(run10.isItalic()); + assertNotNull(run10.getFontColor()); + assertTrue(run10.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run10.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run11 = runs1.get(1); + assertEquals("XSLFCellTextRun", run11.getClass().getSimpleName()); + assertFalse(run11.isBold()); + assertFalse(run11.isItalic()); + assertNotNull(run11.getFontColor()); + assertTrue(run11.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run11.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run12 = runs1.get(2); + assertEquals("XSLFCellTextRun", run12.getClass().getSimpleName()); + assertFalse(run12.isBold()); + assertTrue(run12.isItalic()); + assertNotNull(run12.getFontColor()); + assertTrue(run12.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run12.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run13 = runs1.get(3); + assertEquals("XSLFCellTextRun", run13.getClass().getSimpleName()); + assertFalse(run13.isBold()); + assertFalse(run13.isItalic()); + assertNotNull(run13.getFontColor()); + assertTrue(run13.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run13.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run14 = runs1.get(4); + assertEquals("XSLFCellTextRun", run14.getClass().getSimpleName()); + assertFalse(run14.isBold()); + assertFalse(run14.isItalic()); + assertNotNull(run14.getFontColor()); + assertTrue(run14.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.yellow, ((PaintStyle.SolidPaint)run14.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run15 = runs1.get(5); + assertEquals("XSLFCellTextRun", run15.getClass().getSimpleName()); + assertFalse(run15.isBold()); + assertFalse(run15.isItalic()); + assertNotNull(run15.getFontColor()); + assertTrue(run15.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run15.getFontColor()).getSolidColor().getColor()); + + XSLFTextRun run16 = runs1.get(6); + assertEquals("XSLFCellTextRun", run16.getClass().getSimpleName()); + assertFalse(run16.isBold()); + assertFalse(run16.isItalic()); + assertNotNull(run16.getFontColor()); + assertTrue(run16.getFontColor() instanceof PaintStyle.SolidPaint); + assertEquals(Color.black, ((PaintStyle.SolidPaint)run16.getFontColor()).getSolidColor().getColor()); + + ppt.close(); + } + +} diff --git a/test-data/slideshow/table-with-no-theme.pptx b/test-data/slideshow/table-with-no-theme.pptx new file mode 100644 index 0000000000..6fc99e5ee9 Binary files /dev/null and b/test-data/slideshow/table-with-no-theme.pptx differ diff --git a/test-data/slideshow/table-with-theme.pptx b/test-data/slideshow/table-with-theme.pptx new file mode 100644 index 0000000000..d0a8ead6dc Binary files /dev/null and b/test-data/slideshow/table-with-theme.pptx differ