use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.
the class TestTextRun method testAdvancedSetText.
/**
* Test to ensure that changing non rich text between bytes and
* chars works correctly
*/
@SuppressWarnings("unused")
@Test
public void testAdvancedSetText() {
HSLFSlide slideOne = ss.getSlides().get(0);
List<HSLFTextParagraph> paras = slideOne.getTextParagraphs().get(0);
HSLFTextParagraph para = paras.get(0);
TextHeaderAtom tha = null;
TextBytesAtom tba = null;
TextCharsAtom tca = null;
for (Record r : para.getRecords()) {
if (r instanceof TextHeaderAtom)
tha = (TextHeaderAtom) r;
else if (r instanceof TextBytesAtom)
tba = (TextBytesAtom) r;
else if (r instanceof TextCharsAtom)
tca = (TextCharsAtom) r;
}
// Bytes -> Bytes
assertNull(tca);
assertNotNull(tba);
// assertFalse(run._isUnicode);
assertEquals("This is a test title", para.getTextRuns().get(0).getRawText());
String changeBytesOnly = "New Test Title";
HSLFTextParagraph.setText(paras, changeBytesOnly);
para = paras.get(0);
tha = null;
tba = null;
tca = null;
for (Record r : para.getRecords()) {
if (r instanceof TextHeaderAtom)
tha = (TextHeaderAtom) r;
else if (r instanceof TextBytesAtom)
tba = (TextBytesAtom) r;
else if (r instanceof TextCharsAtom)
tca = (TextCharsAtom) r;
}
assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
assertNull(tca);
assertNotNull(tba);
// Bytes -> Chars
assertNull(tca);
assertNotNull(tba);
assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
String changeByteChar = "This is a test title with a 'ġ' g with a dot";
HSLFTextParagraph.setText(paras, changeByteChar);
para = paras.get(0);
tha = null;
tba = null;
tca = null;
for (Record r : para.getRecords()) {
if (r instanceof TextHeaderAtom)
tha = (TextHeaderAtom) r;
else if (r instanceof TextBytesAtom)
tba = (TextBytesAtom) r;
else if (r instanceof TextCharsAtom)
tca = (TextCharsAtom) r;
}
assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
assertNotNull(tca);
assertNull(tba);
// Chars -> Chars
assertNull(tba);
assertNotNull(tca);
assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
String changeCharChar = "This is a test title with a 'Ň' N with a hat";
HSLFTextParagraph.setText(paras, changeCharChar);
para = paras.get(0);
tha = null;
tba = null;
tca = null;
for (Record r : para.getRecords()) {
if (r instanceof TextHeaderAtom)
tha = (TextHeaderAtom) r;
else if (r instanceof TextBytesAtom)
tba = (TextBytesAtom) r;
else if (r instanceof TextCharsAtom)
tca = (TextCharsAtom) r;
}
assertEquals(changeCharChar, HSLFTextParagraph.getRawText(paras));
assertNotNull(tca);
assertNull(tba);
}
use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.
the class HSLFTextParagraph method updateHyperlinks.
private static void updateHyperlinks(List<HSLFTextParagraph> paragraphs) {
TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
RecordContainer _txtbox = headerAtom.getParentRecord();
// remove existing hyperlink records
for (Record r : _txtbox.getChildRecords()) {
if (r instanceof InteractiveInfo || r instanceof TxInteractiveInfoAtom) {
_txtbox.removeChild(r);
}
}
// now go through all the textruns and check for hyperlinks
HSLFHyperlink lastLink = null;
for (HSLFTextParagraph para : paragraphs) {
for (HSLFTextRun run : para) {
HSLFHyperlink thisLink = run.getHyperlink();
if (thisLink != null && thisLink == lastLink) {
// the hyperlink extends over this text run, increase its length
// TODO: the text run might be longer than the hyperlink
thisLink.setEndIndex(thisLink.getEndIndex() + run.getLength());
} else {
if (lastLink != null) {
InteractiveInfo info = lastLink.getInfo();
TxInteractiveInfoAtom txinfo = lastLink.getTextRunInfo();
assert (info != null && txinfo != null);
_txtbox.appendChildRecord(info);
_txtbox.appendChildRecord(txinfo);
}
}
lastLink = thisLink;
}
}
if (lastLink != null) {
InteractiveInfo info = lastLink.getInfo();
TxInteractiveInfoAtom txinfo = lastLink.getTextRunInfo();
assert (info != null && txinfo != null);
_txtbox.appendChildRecord(info);
_txtbox.appendChildRecord(txinfo);
}
}
use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.
the class HSLFTextParagraph method refreshRecords.
/**
* Writes the textbox records back to the document record
*/
private static void refreshRecords(List<HSLFTextParagraph> paragraphs) {
TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
RecordContainer _txtbox = headerAtom.getParentRecord();
if (_txtbox instanceof EscherTextboxWrapper) {
try {
((EscherTextboxWrapper) _txtbox).writeOut(null);
} catch (IOException e) {
throw new HSLFException("failed dummy write", e);
}
}
}
use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.
the class HSLFTextShape method createEmptyParagraph.
private void createEmptyParagraph() {
TextHeaderAtom tha = (TextHeaderAtom) _txtbox.findFirstOfType(TextHeaderAtom._type);
if (tha == null) {
tha = new TextHeaderAtom();
tha.setParentRecord(_txtbox);
_txtbox.appendChildRecord(tha);
}
TextBytesAtom tba = (TextBytesAtom) _txtbox.findFirstOfType(TextBytesAtom._type);
TextCharsAtom tca = (TextCharsAtom) _txtbox.findFirstOfType(TextCharsAtom._type);
if (tba == null && tca == null) {
tba = new TextBytesAtom();
tba.setText(new byte[0]);
_txtbox.appendChildRecord(tba);
}
final String text = ((tba != null) ? tba.getText() : tca.getText());
StyleTextPropAtom sta = (StyleTextPropAtom) _txtbox.findFirstOfType(StyleTextPropAtom._type);
TextPropCollection paraStyle = null, charStyle = null;
if (sta == null) {
int parSiz = text.length();
sta = new StyleTextPropAtom(parSiz + 1);
if (_paragraphs.isEmpty()) {
paraStyle = sta.addParagraphTextPropCollection(parSiz + 1);
charStyle = sta.addCharacterTextPropCollection(parSiz + 1);
} else {
for (HSLFTextParagraph htp : _paragraphs) {
int runsLen = 0;
for (HSLFTextRun htr : htp.getTextRuns()) {
runsLen += htr.getLength();
charStyle = sta.addCharacterTextPropCollection(htr.getLength());
htr.setCharacterStyle(charStyle);
}
paraStyle = sta.addParagraphTextPropCollection(runsLen);
htp.setParagraphStyle(paraStyle);
}
assert (paraStyle != null && charStyle != null);
}
_txtbox.appendChildRecord(sta);
} else {
paraStyle = sta.getParagraphStyles().get(0);
charStyle = sta.getCharacterStyles().get(0);
}
if (_paragraphs.isEmpty()) {
HSLFTextParagraph htp = new HSLFTextParagraph(tha, tba, tca, _paragraphs);
htp.setParagraphStyle(paraStyle);
htp.setParentShape(this);
_paragraphs.add(htp);
HSLFTextRun htr = new HSLFTextRun(htp);
htr.setCharacterStyle(charStyle);
htr.setText(text);
htp.addTextRun(htr);
}
}
Aggregations