[bug-65935] add removeTextRun

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898672 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-03-07 11:05:05 +00:00
parent 3eb1ef578e
commit b7529cdedf
2 changed files with 62 additions and 2 deletions

View File

@ -18,6 +18,7 @@ package org.apache.poi.xslf.usermodel;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
@ -103,12 +104,12 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
@Override
public List<XSLFTextRun> getTextRuns() {
return _runs;
return Collections.unmodifiableList(_runs);
}
@Override
public Iterator<XSLFTextRun> iterator() {
return _runs.iterator();
return getTextRuns().iterator();
}
/**
@ -125,6 +126,43 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
return run;
}
/**
* Remove a text run
*
* @param textRun a run of text
* @return whether the run was removed
* @since POI 5.2.2
*/
public boolean removeTextRun(XSLFTextRun textRun) {
if (_runs.remove(textRun)) {
XmlObject xo = textRun.getXmlObject();
if (xo instanceof CTRegularTextRun) {
for (int i = 0; i < getXmlObject().sizeOfRArray(); i++) {
if (getXmlObject().getRArray(i).equals(xo)) {
getXmlObject().removeR(i);
return true;
}
}
} else if (xo instanceof CTTextField) {
for (int i = 0; i < getXmlObject().sizeOfFldArray(); i++) {
if (getXmlObject().getFldArray(i).equals(xo)) {
getXmlObject().removeFld(i);
return true;
}
}
} else if (xo instanceof CTTextLineBreak) {
for (int i = 0; i < getXmlObject().sizeOfBrArray(); i++) {
if (getXmlObject().getBrArray(i).equals(xo)) {
getXmlObject().removeBr(i);
return true;
}
}
}
return false;
}
return false;
}
/**
* Insert a line break
*

View File

@ -169,6 +169,28 @@ class TestXSLFTextParagraph {
}
}
@Test
void testRemoveTextRun() throws IOException {
try (XMLSlideShow ppt = new XMLSlideShow()) {
XSLFSlide slide = ppt.createSlide();
XSLFTextShape sh = slide.createAutoShape();
sh.setLineColor(Color.black);
XSLFTextParagraph p = sh.addNewTextParagraph();
XSLFTextRun run = p.addNewTextRun();
run.setText(
"Paragraph formatting allows for more granular control " +
"of text within a shape. Properties here apply to all text " +
"residing within the corresponding paragraph.");
assertTrue(p.removeTextRun(run));
assertTrue(p.getTextRuns().isEmpty());
assertEquals(0, p.getXmlObject().sizeOfRArray());
}
}
/**
* test breaking test into lines.
* This test requires that the Arial font is available and will run only on windows