This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 658a064914 XSLF: set table cell borders on all cells sharing the edge 
(#1536)
658a064914 is described below

commit 658a064914370693f2ab3a125862971e35b00f0a
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Nov 5 12:45:52 2026 +0100

    XSLF: set table cell borders on all cells sharing the edge (#1536)
    
    PowerPoint draws an edge shared by two cells from the bottom or right
    border of the upper or left cell, even if that border is unset or has no
    fill, and it ignores the borders of merged-away cells. So borders set on
    one cell left gaps, especially around merged cells.
    
    Setting or removing a border now also updates the other cell sharing the
    edge, including all neighbours along a merged range. A border set on the
    outer edge of a merged-away cell also goes to the first merged cell.
    
    XSLFTable.insertRow now refreshes the row and column indexes, which the
    neighbour lookup relies on.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/poi/xslf/usermodel/XSLFTable.java   |   1 +
 .../apache/poi/xslf/usermodel/XSLFTableCell.java   | 113 +++++++++++++++++++-
 .../apache/poi/xslf/usermodel/TestXSLFTable.java   | 116 +++++++++++++++++++++
 3 files changed, 228 insertions(+), 2 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java 
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
index f34fe8b9e6..25ebae2aba 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
@@ -182,6 +182,7 @@ public class XSLFTable extends XSLFGraphicFrame implements 
Iterable<XSLFTableRow
             row.addCell();
         }
         _rows.add(rowIdx, row);
+        updateRowColIndexes();
         return row;
     }
 
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 52522ef34d..3720fa1968 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
@@ -21,6 +21,7 @@ package org.apache.poi.xslf.usermodel;
 
 import java.awt.Color;
 import java.awt.geom.Rectangle2D;
+import java.util.function.BiConsumer;
 
 import org.apache.poi.sl.draw.DrawPaint;
 import org.apache.poi.sl.usermodel.ColorStyle;
@@ -42,6 +43,13 @@ import org.openxmlformats.schemas.drawingml.x2006.main.*;
 
 /**
  * Represents a cell of a table in a .pptx presentation
+ * <p>
+ * PowerPoint draws an edge shared by two cells from the bottom or right 
border of the upper or left cell,
+ * even if that border is unset or has no fill, and it ignores the borders of 
merged-away cells.
+ * So setting or removing a border also updates the other cell sharing that 
edge. For a merged-away cell,
+ * it also updates the first cell of the merged range and all the neighbours 
along the merged range.
+ * If two cells set the same shared edge, the last call wins.
+ * Merging cells does not update existing borders, so merge the cells before 
setting their borders.
  */
 public class XSLFTableCell extends XSLFTextShape implements 
TableCell<XSLFShape, XSLFTextParagraph> {
     private CTTableCellProperties _tcPr;
@@ -144,11 +152,108 @@ public class XSLFTableCell extends XSLFTextShape 
implements TableCell<XSLFShape,
         CTLineProperties props = getCTLine(edge, true);
         if (props != null) {
             props.set(properties.getXmlObject().copy());
+            syncBorder(edge);
         }
     }
 
+    /**
+     * Copies a border to the cells which share its edge
+     */
+    private void syncBorder(BorderEdge edge) {
+        final CTLineProperties ln = getCTLine(edge, false);
+        forEachSharedEdge(edge, (cell, sharedEdge) -> 
cell.getCTLine(sharedEdge, true).set(ln));
+    }
+
+    private void forEachSharedEdge(BorderEdge edge, BiConsumer<XSLFTableCell, 
BorderEdge> action) {
+        final XSLFTableCell first = getFirstMergedCell();
+        final int firstRow = first.row;
+        final int firstCol = first.col;
+        final int lastRow = Math.min(firstRow + first.getRowSpan(), 
table.getNumberOfRows()) - 1;
+        final int lastCol = Math.min(firstCol + first.getGridSpan(), 
table.getNumberOfColumns()) - 1;
+
+        // the borders of the first cell apply to the whole merged range
+        final boolean outerEdge = first == this || switch (edge) {
+            case top -> row == firstRow;
+            case bottom -> row == lastRow;
+            case left -> col == firstCol;
+            case right -> col == lastCol;
+        };
+        // the edges inside a merged range aren't visible
+        if (!outerEdge) {
+            return;
+        }
+
+        if (first != this) {
+            action.accept(first, edge);
+        }
+
+        switch (edge) {
+            case top -> applyToRow(firstRow - 1, firstCol, lastCol, 
BorderEdge.bottom, action);
+            case bottom -> applyToRow(lastRow + 1, firstCol, lastCol, 
BorderEdge.top, action);
+            case left -> applyToColumn(firstCol - 1, firstRow, lastRow, 
BorderEdge.right, action);
+            case right -> applyToColumn(lastCol + 1, firstRow, lastRow, 
BorderEdge.left, action);
+        }
+    }
+
+    private void applyToRow(int rowIdx, int firstCol, int lastCol, BorderEdge 
edge,
+            BiConsumer<XSLFTableCell, BorderEdge> action) {
+        for (int colIdx = firstCol; colIdx <= lastCol; colIdx++) {
+            XSLFTableCell cell = table.getCell(rowIdx, colIdx);
+            if (cell != null) {
+                action.accept(cell, edge);
+            }
+        }
+    }
+
+    private void applyToColumn(int colIdx, int firstRow, int lastRow, 
BorderEdge edge,
+            BiConsumer<XSLFTableCell, BorderEdge> action) {
+        for (int rowIdx = firstRow; rowIdx <= lastRow; rowIdx++) {
+            XSLFTableCell cell = table.getCell(rowIdx, colIdx);
+            if (cell != null) {
+                action.accept(cell, edge);
+            }
+        }
+    }
+
+    /**
+     * @return the first cell of the merged range containing this cell, or 
this cell if it isn't merged
+     */
+    private XSLFTableCell getFirstMergedCell() {
+        XSLFTableCell first = this;
+        while (first.isHMerged() && first.col > 0) {
+            XSLFTableCell previous = table.getCell(first.row, first.col - 1);
+            if (previous == null) {
+                break;
+            }
+            first = previous;
+        }
+        while (first.isVMerged() && first.row > 0) {
+            XSLFTableCell previous = table.getCell(first.row - 1, first.col);
+            if (previous == null) {
+                break;
+            }
+            first = previous;
+        }
+        return first;
+    }
+
+    private boolean isHMerged() {
+        CTTableCell c = getCell();
+        return c.isSetHMerge() && c.getHMerge();
+    }
+
+    private boolean isVMerged() {
+        CTTableCell c = getCell();
+        return c.isSetVMerge() && c.getVMerge();
+    }
+
     @Override
     public void removeBorder(BorderEdge edge) {
+        removeOwnBorder(edge);
+        forEachSharedEdge(edge, XSLFTableCell::removeOwnBorder);
+    }
+
+    private void removeOwnBorder(BorderEdge edge) {
         CTTableCellProperties pr = getCellProperties(false);
         if (pr == null) {
             return;
@@ -248,6 +353,7 @@ public class XSLFTableCell extends XSLFTextShape implements 
TableCell<XSLFShape,
             return;
         }
         ln.setW(Units.toEMU(width));
+        syncBorder(edge);
     }
 
     private CTLineProperties setBorderDefaults(BorderEdge edge) {
@@ -306,6 +412,7 @@ public class XSLFTableCell extends XSLFTextShape implements 
TableCell<XSLFShape,
         CTSolidColorFillProperties fill = ln.addNewSolidFill();
         XSLFColor c = new XSLFColor(fill, getSheet().getTheme(), 
fill.getSchemeClr(), getSheet());
         c.setColor(color);
+        syncBorder(edge);
     }
 
     @SuppressWarnings("WeakerAccess")
@@ -338,6 +445,7 @@ public class XSLFTableCell extends XSLFTextShape implements 
TableCell<XSLFShape,
 
         CTLineProperties ln = setBorderDefaults(edge);
         ln.setCmpd(STCompoundLine.Enum.forInt(compound.ooxmlId));
+        syncBorder(edge);
     }
 
     @SuppressWarnings("WeakerAccess")
@@ -361,6 +469,7 @@ public class XSLFTableCell extends XSLFTextShape implements 
TableCell<XSLFShape,
             ln.addNewPrstDash();
         }
         ln.getPrstDash().setVal(STPresetLineDashVal.Enum.forInt(dash.ooxmlId));
+        syncBorder(edge);
     }
 
     @SuppressWarnings("WeakerAccess")
@@ -381,6 +490,7 @@ public class XSLFTableCell extends XSLFTextShape implements 
TableCell<XSLFShape,
 
         CTLineProperties ln = setBorderDefaults(edge);
         ln.setCap(STLineCap.Enum.forInt(cap.ooxmlId));
+        syncBorder(edge);
     }
 
     /**
@@ -683,8 +793,7 @@ public class XSLFTableCell extends XSLFTextShape implements 
TableCell<XSLFShape,
      */
     @Override
     public boolean isMerged() {
-        CTTableCell c = getCell();
-        return (c.isSetHMerge() && c.getHMerge()) || (c.isSetVMerge() && 
c.getVMerge());
+        return isHMerged() || isVMerged();
     }
 
     /**
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java 
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java
index c76849e46f..128a852c97 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java
@@ -299,6 +299,122 @@ class TestXSLFTable {
         ppt.close();
     }
 
+    @Test
+    void sharedEdgeIsSetOnBothCells() throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFTable tbl = ppt.createSlide().createTable(3, 3);
+            XSLFTableCell centre = tbl.getCell(1, 1);
+
+            centre.setBorderColor(BorderEdge.top, Color.red);
+            centre.setBorderWidth(BorderEdge.top, 2.0);
+            assertEquals(Color.red, tbl.getCell(0, 
1).getBorderColor(BorderEdge.bottom));
+            assertEquals(2.0, tbl.getCell(0, 
1).getBorderWidth(BorderEdge.bottom), 0);
+
+            centre.setBorderColor(BorderEdge.left, Color.green);
+            assertEquals(Color.green, tbl.getCell(1, 
0).getBorderColor(BorderEdge.right));
+
+            centre.setBorderColor(BorderEdge.bottom, Color.blue);
+            assertEquals(Color.blue, tbl.getCell(2, 
1).getBorderColor(BorderEdge.top));
+
+            centre.setBorderColor(BorderEdge.right, Color.yellow);
+            assertEquals(Color.yellow, tbl.getCell(1, 
2).getBorderColor(BorderEdge.left));
+
+            // cells which don't share the edge are left alone
+            assertNull(tbl.getCell(0, 0).getBorderColor(BorderEdge.bottom));
+            assertNull(tbl.getCell(0, 1).getBorderColor(BorderEdge.top));
+            assertNull(tbl.getCell(2, 2).getBorderColor(BorderEdge.top));
+
+            // the last call for a shared edge wins
+            tbl.getCell(0, 1).setBorderColor(BorderEdge.bottom, Color.black);
+            assertEquals(Color.black, centre.getBorderColor(BorderEdge.top));
+
+            centre.removeBorder(BorderEdge.top);
+            assertNull(centre.getBorderColor(BorderEdge.top));
+            assertNull(tbl.getCell(0, 1).getBorderColor(BorderEdge.bottom));
+        }
+    }
+
+    @Test
+    void borderOnTableEdgeHasNoNeighbour() throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFTable tbl = ppt.createSlide().createTable(1, 1);
+            for (BorderEdge edge : BorderEdge.values()) {
+                tbl.getCell(0, 0).setBorderColor(edge, Color.red);
+                assertEquals(Color.red, tbl.getCell(0, 
0).getBorderColor(edge));
+            }
+        }
+    }
+
+    @Test
+    void mergedCellBorderIsSetOnNeighbours() throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFTable tbl = ppt.createSlide().createTable(6, 5);
+            tbl.mergeCells(2, 3, 1, 2);
+            XSLFTableCell first = tbl.getCell(2, 1);
+            for (BorderEdge edge : BorderEdge.values()) {
+                first.setBorderColor(edge, Color.blue);
+            }
+
+            for (int col = 1; col <= 2; col++) {
+                assertEquals(Color.blue, tbl.getCell(1, 
col).getBorderColor(BorderEdge.bottom));
+                assertEquals(Color.blue, tbl.getCell(4, 
col).getBorderColor(BorderEdge.top));
+            }
+            for (int row = 2; row <= 3; row++) {
+                assertEquals(Color.blue, tbl.getCell(row, 
0).getBorderColor(BorderEdge.right));
+                assertEquals(Color.blue, tbl.getCell(row, 
3).getBorderColor(BorderEdge.left));
+            }
+            assertNull(tbl.getCell(1, 0).getBorderColor(BorderEdge.bottom));
+            assertNull(tbl.getCell(1, 3).getBorderColor(BorderEdge.bottom));
+            assertNull(tbl.getCell(4, 3).getBorderColor(BorderEdge.left));
+        }
+    }
+
+    @Test
+    void mergedAwayCellBorderIsSetOnFirstCell() throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFTable tbl = ppt.createSlide().createTable(5, 4);
+            tbl.mergeCells(1, 3, 1, 2);
+            XSLFTableCell first = tbl.getCell(1, 1);
+            XSLFTableCell mergedAway = tbl.getCell(3, 2);
+
+            mergedAway.setBorderColor(BorderEdge.bottom, Color.red);
+            assertEquals(Color.red, 
mergedAway.getBorderColor(BorderEdge.bottom));
+            assertEquals(Color.red, first.getBorderColor(BorderEdge.bottom));
+            assertEquals(Color.red, tbl.getCell(4, 
1).getBorderColor(BorderEdge.top));
+            assertEquals(Color.red, tbl.getCell(4, 
2).getBorderColor(BorderEdge.top));
+
+            mergedAway.setBorderColor(BorderEdge.right, Color.green);
+            assertEquals(Color.green, first.getBorderColor(BorderEdge.right));
+            for (int row = 1; row <= 3; row++) {
+                assertEquals(Color.green, tbl.getCell(row, 
3).getBorderColor(BorderEdge.left));
+            }
+
+            // an edge inside the merged range stays on the cell
+            mergedAway.setBorderColor(BorderEdge.top, Color.blue);
+            assertEquals(Color.blue, 
mergedAway.getBorderColor(BorderEdge.top));
+            assertNull(first.getBorderColor(BorderEdge.top));
+            assertNull(tbl.getCell(2, 2).getBorderColor(BorderEdge.bottom));
+
+            mergedAway.removeBorder(BorderEdge.bottom);
+            assertNull(first.getBorderColor(BorderEdge.bottom));
+            assertNull(tbl.getCell(4, 1).getBorderColor(BorderEdge.top));
+        }
+    }
+
+    @Test
+    void sharedEdgeAfterInsertRow() throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFTable tbl = ppt.createSlide().createTable(2, 2);
+            XSLFTableRow inserted = tbl.insertRow(1);
+
+            inserted.getCells().get(1).setBorderColor(BorderEdge.top, 
Color.red);
+            assertEquals(Color.red, tbl.getCell(0, 
1).getBorderColor(BorderEdge.bottom));
+
+            tbl.getCell(2, 0).setBorderColor(BorderEdge.top, Color.blue);
+            assertEquals(Color.blue, 
inserted.getCells().get(0).getBorderColor(BorderEdge.bottom));
+        }
+    }
+
     @Test
     void removeTable() throws IOException {
         XMLSlideShow ss = 
XSLFTestDataSamples.openSampleDocument("shapes.pptx");


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to