Browse Source

[bug-65935] add removeTextRun

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898672 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_2
PJ Fanning 2 years ago
parent
commit
b7529cdedf

+ 40
- 2
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java 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
*

+ 22
- 0
poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTextParagraph.java 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

Loading…
Cancel
Save