use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.
the class HSLFTextParagraph method applyParagraphStyles.
protected static void applyParagraphStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> paraStyles) {
int paraIdx = 0;
for (TextPropCollection p : paraStyles) {
for (int ccPara = 0, ccStyle = p.getCharactersCovered(); ccPara < ccStyle; paraIdx++) {
if (paraIdx >= paragraphs.size()) {
return;
}
HSLFTextParagraph htp = paragraphs.get(paraIdx);
TextPropCollection pCopy = new TextPropCollection(0, TextPropType.paragraph);
pCopy.copy(p);
htp.setParagraphStyle(pCopy);
int len = 0;
for (HSLFTextRun trun : htp.getTextRuns()) {
len += trun.getLength();
}
if (paraIdx == paragraphs.size() - 1) {
len++;
}
pCopy.updateTextSize(len);
ccPara += len;
}
}
}
use of org.apache.poi.hslf.model.textproperties.TextPropCollection 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);
}
}
use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.
the class HSLFTextParagraph method appendText.
/**
* Adds the supplied text onto the end of the TextParagraphs,
* creating a new RichTextRun for it to sit in.
*
* @param text the text string used by this object.
*/
protected static HSLFTextRun appendText(List<HSLFTextParagraph> paragraphs, String text, boolean newParagraph) {
text = toInternalString(text);
// check paragraphs
assert (!paragraphs.isEmpty() && !paragraphs.get(0).getTextRuns().isEmpty());
HSLFTextParagraph htp = paragraphs.get(paragraphs.size() - 1);
HSLFTextRun htr = htp.getTextRuns().get(htp.getTextRuns().size() - 1);
boolean addParagraph = newParagraph;
for (String rawText : text.split("(?<=\r)")) {
// special case, if last text paragraph or run is empty, we will reuse it
boolean lastRunEmpty = (htr.getLength() == 0);
boolean lastParaEmpty = lastRunEmpty && (htp.getTextRuns().size() == 1);
if (addParagraph && !lastParaEmpty) {
TextPropCollection tpc = htp.getParagraphStyle();
HSLFTextParagraph prevHtp = htp;
htp = new HSLFTextParagraph(htp._headerAtom, htp._byteAtom, htp._charAtom, paragraphs);
htp.getParagraphStyle().copy(tpc);
htp.setParentShape(prevHtp.getParentShape());
htp.setShapeId(prevHtp.getShapeId());
htp.supplySheet(prevHtp.getSheet());
paragraphs.add(htp);
}
addParagraph = true;
if (!lastRunEmpty) {
TextPropCollection tpc = htr.getCharacterStyle();
htr = new HSLFTextRun(htp);
htr.getCharacterStyle().copy(tpc);
htp.addTextRun(htr);
}
htr.setText(rawText);
}
storeText(paragraphs);
return htr;
}
use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.
the class HSLFTextParagraph method applyCharacterStyles.
protected static void applyCharacterStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> charStyles) {
int paraIdx = 0, runIdx = 0;
HSLFTextRun trun;
for (int csIdx = 0; csIdx < charStyles.size(); csIdx++) {
TextPropCollection p = charStyles.get(csIdx);
for (int ccRun = 0, ccStyle = p.getCharactersCovered(); ccRun < ccStyle; ) {
HSLFTextParagraph para = paragraphs.get(paraIdx);
List<HSLFTextRun> runs = para.getTextRuns();
trun = runs.get(runIdx);
final int len = trun.getLength();
if (ccRun + len <= ccStyle) {
ccRun += len;
} else {
String text = trun.getRawText();
trun.setText(text.substring(0, ccStyle - ccRun));
HSLFTextRun nextRun = new HSLFTextRun(para);
nextRun.setText(text.substring(ccStyle - ccRun));
runs.add(runIdx + 1, nextRun);
ccRun += ccStyle - ccRun;
}
trun.setCharacterStyle(p);
if (paraIdx == paragraphs.size() - 1 && runIdx == runs.size() - 1) {
if (csIdx < charStyles.size() - 1) {
// special case, empty trailing text run
HSLFTextRun nextRun = new HSLFTextRun(para);
nextRun.setText("");
runs.add(nextRun);
ccRun++;
} else {
// need to add +1 to the last run of the last paragraph
trun.getCharacterStyle().updateTextSize(trun.getLength() + 1);
ccRun++;
}
}
// need to compare it again, in case a run has been added after
if (++runIdx == runs.size()) {
paraIdx++;
runIdx = 0;
}
}
}
}
use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.
the class TextStyleListing method showStyleTextPropAtom.
public static void showStyleTextPropAtom(StyleTextPropAtom stpa) {
System.out.println("\nFound a StyleTextPropAtom");
List<TextPropCollection> paragraphStyles = stpa.getParagraphStyles();
System.out.println("Contains " + paragraphStyles.size() + " paragraph styles:");
for (int i = 0; i < paragraphStyles.size(); i++) {
TextPropCollection tpc = paragraphStyles.get(i);
System.out.println(" In paragraph styling " + i + ":");
System.out.println(" Characters covered is " + tpc.getCharactersCovered());
showTextProps(tpc);
}
List<TextPropCollection> charStyles = stpa.getCharacterStyles();
System.out.println("Contains " + charStyles.size() + " character styles:");
for (int i = 0; i < charStyles.size(); i++) {
TextPropCollection tpc = charStyles.get(i);
System.out.println(" In character styling " + i + ":");
System.out.println(" Characters covered is " + tpc.getCharactersCovered());
showTextProps(tpc);
}
}
Aggregations