Make a start on supporting adding new text to a hslf textrun

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610160 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-01-08 21:19:51 +00:00
parent e84bc3efb3
commit 56b73eddb9
2 changed files with 158 additions and 8 deletions

View File

@ -20,6 +20,7 @@
package org.apache.poi.hslf.model;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
@ -252,9 +253,81 @@ public class TextRun
// Update methods follow
/**
* Adds the supplied text onto the end of the TextRun,
* creating a new RichTextRun (returned) for it to
* sit in.
*/
public RichTextRun appendText(String s) {
// We will need a StyleTextProp atom
ensureStyleAtomPresent();
// First up, append the text
int oldSize = getRawText().length();
storeText(
getRawText() + s
);
// If either of the previous styles overran
// the text by one, we need to shuffle that
// extra character onto the new ones
Iterator it = _styleAtom.getParagraphStyles().iterator();
int pLen = 0;
while(it.hasNext()) {
TextPropCollection tpc = (TextPropCollection)it.next();
pLen += tpc.getCharactersCovered();
}
it = _styleAtom.getCharacterStyles().iterator();
int cLen = 0;
while(it.hasNext()) {
TextPropCollection tpc = (TextPropCollection)it.next();
cLen += tpc.getCharactersCovered();
}
int pOverRun = pLen - oldSize;
int cOverRun = cLen - oldSize;
if(pOverRun > 0) {
TextPropCollection tpc = (TextPropCollection)
_styleAtom.getParagraphStyles().getLast();
tpc.updateTextSize(
tpc.getCharactersCovered() - pOverRun
);
}
if(cOverRun > 0) {
TextPropCollection tpc = (TextPropCollection)
_styleAtom.getCharacterStyles().getLast();
tpc.updateTextSize(
tpc.getCharactersCovered() - cOverRun
);
}
// Next, add the styles for its
// paragraph and characters
TextPropCollection newPTP =
_styleAtom.addParagraphTextPropCollection(s.length()+pOverRun);
TextPropCollection newCTP =
_styleAtom.addCharacterTextPropCollection(s.length()+cOverRun);
// Now, create the new RichTextRun
RichTextRun nr = new RichTextRun(
this, oldSize, s.length(),
newPTP, newCTP, false, false
);
// Add the new RichTextRun onto our list
RichTextRun[] newRuns = new RichTextRun[_rtRuns.length+1];
System.arraycopy(_rtRuns, 0, newRuns, 0, _rtRuns.length);
newRuns[newRuns.length-1] = nr;
_rtRuns = newRuns;
// And return the new run to the caller
return nr;
}
/**
* Saves the given string to the records. Doesn't touch the stylings.
* Saves the given string to the records. Doesn't
* touch the stylings.
*/
private void storeText(String s) {
// Remove a single trailing \n, as there is an implicit one at the

View File

@ -16,16 +16,22 @@
*/
package org.apache.poi.hslf.usermodel;
import java.io.*;
import java.awt.*;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.SlideListWithText;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import junit.framework.TestCase;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.SlideMaster;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.SlideListWithText;
/**
* Test that the friendly getters and setters on RichTextRun
* behave as expected.
@ -549,4 +555,75 @@ if(false) {
assertEquals(0, rt.getBulletOffset());
assertEquals('\u263A', rt.getBulletChar());
}
public void testAddText() throws Exception {
FileInputStream is = new FileInputStream(new File(System.getProperty("HSLF.testdata.path"), "bullets.ppt"));
SlideShow ppt = new SlideShow(is);
is.close();
assertTrue("No Exceptions while reading file", true);
RichTextRun rt;
TextRun[] txt;
Slide[] slides = ppt.getSlides();
assertEquals(2, slides.length);
txt = slides[0].getTextRuns();
assertEquals(2, txt.length);
assertEquals("Title text", txt[0].getRawText());
assertEquals(1, txt[0].getRichTextRuns().length);
rt = txt[0].getRichTextRuns()[0];
assertFalse(rt.isBullet());
// Add some new text
txt[0].appendText("Foo! I'm new!");
assertEquals(2, txt[0].getRichTextRuns().length);
rt = txt[0].getRichTextRuns()[0];
assertFalse(rt.isBold());
assertEquals("Title text", rt.getText());
rt = txt[0].getRichTextRuns()[1];
assertFalse(rt.isBold());
assertEquals("Foo! I'm new!", rt.getText());
rt.setBold(true);
// And some more
txt[0].appendText("Me too!");
assertEquals(3, txt[0].getRichTextRuns().length);
rt = txt[0].getRichTextRuns()[0];
assertFalse(rt.isBold());
assertEquals("Title text", rt.getText());
rt = txt[0].getRichTextRuns()[1];
assertTrue(rt.isBold());
assertEquals("Foo! I'm new!", rt.getText());
rt = txt[0].getRichTextRuns()[2];
assertFalse(rt.isBold());
assertEquals("Me too!", rt.getText());
// Save and re-open
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
ppt = new SlideShow(new ByteArrayInputStream(out.toByteArray()));
slides = ppt.getSlides();
assertEquals(2, slides.length);
txt = slides[0].getTextRuns();
assertEquals(2, txt.length);
assertEquals(3, txt[0].getRichTextRuns().length);
rt = txt[0].getRichTextRuns()[0];
assertFalse(rt.isBold());
assertEquals("Title text", rt.getText());
rt = txt[0].getRichTextRuns()[1];
assertTrue(rt.isBold());
assertEquals("Foo! I'm new!", rt.getText());
rt = txt[0].getRichTextRuns()[2];
assertFalse(rt.isBold());
assertEquals("Me too!", rt.getText());
// FileOutputStream fout = new FileOutputStream("/tmp/foo.ppt");
// ppt.write(fout);
}
}