use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt in project poi by apache.
the class XSSFRichTextString method getIndexOfFormattingRun.
/**
* The index within the string to which the specified formatting run applies.
*
* @param index the index of the formatting run
* @return the index within the string.
*/
public int getIndexOfFormattingRun(int index) {
if (st.sizeOfRArray() == 0)
return 0;
int pos = 0;
for (int i = 0; i < st.sizeOfRArray(); i++) {
CTRElt r = st.getRArray(i);
if (i == index)
return pos;
pos += r.getT().length();
}
return -1;
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt in project poi by apache.
the class XSSFRichTextString method getFontAtIndex.
/**
* Return a copy of the font in use at a particular index.
*
* @param index The index.
* @return A copy of the font that's currently being applied at that
* index or null if no font is being applied or the
* index is out of range.
*/
public XSSFFont getFontAtIndex(int index) {
final ThemesTable themes = getThemesTable();
int pos = 0;
//noinspection deprecation - for performance reasons!
for (CTRElt r : st.getRArray()) {
final int length = r.getT().length();
if (index >= pos && index < pos + length) {
XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr()));
fnt.setThemesTable(themes);
return fnt;
}
pos += length;
}
return null;
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt in project poi by apache.
the class TestSharedStringsTable method testCreateNew.
public void testCreateNew() {
SharedStringsTable sst = new SharedStringsTable();
CTRst st;
int idx;
// Check defaults
assertNotNull(sst.getItems());
assertEquals(0, sst.getItems().size());
assertEquals(0, sst.getCount());
assertEquals(0, sst.getUniqueCount());
st = CTRst.Factory.newInstance();
st.setT("Hello, World!");
idx = sst.addEntry(st);
assertEquals(0, idx);
assertEquals(1, sst.getCount());
assertEquals(1, sst.getUniqueCount());
//add the same entry again
idx = sst.addEntry(st);
assertEquals(0, idx);
assertEquals(2, sst.getCount());
assertEquals(1, sst.getUniqueCount());
//and again
idx = sst.addEntry(st);
assertEquals(0, idx);
assertEquals(3, sst.getCount());
assertEquals(1, sst.getUniqueCount());
st = CTRst.Factory.newInstance();
st.setT("Second string");
idx = sst.addEntry(st);
assertEquals(1, idx);
assertEquals(4, sst.getCount());
assertEquals(2, sst.getUniqueCount());
//add the same entry again
idx = sst.addEntry(st);
assertEquals(1, idx);
assertEquals(5, sst.getCount());
assertEquals(2, sst.getUniqueCount());
st = CTRst.Factory.newInstance();
CTRElt r = st.addNewR();
CTRPrElt pr = r.addNewRPr();
//red
pr.addNewColor().setRgb(new byte[] { (byte) 0xFF, 0, 0 });
//bold
pr.addNewI().setVal(true);
//italic
pr.addNewB().setVal(true);
r.setT("Second string");
idx = sst.addEntry(st);
assertEquals(2, idx);
assertEquals(6, sst.getCount());
assertEquals(3, sst.getUniqueCount());
idx = sst.addEntry(st);
assertEquals(2, idx);
assertEquals(7, sst.getCount());
assertEquals(3, sst.getUniqueCount());
//OK. the sst table is filled, check the contents
assertEquals(3, sst.getItems().size());
assertEquals("Hello, World!", new XSSFRichTextString(sst.getEntryAt(0)).toString());
assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(1)).toString());
assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString());
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt in project poi by apache.
the class XSSFSimpleShape method addNewTextParagraph.
/**
* Add a new paragraph run to this shape, set to the provided rich text string
*
* @return created paragraph run
*/
public XSSFTextParagraph addNewTextParagraph(XSSFRichTextString str) {
CTTextBody txBody = ctShape.getTxBody();
CTTextParagraph p = txBody.addNewP();
if (str.numFormattingRuns() == 0) {
CTRegularTextRun r = p.addNewR();
CTTextCharacterProperties rPr = r.addNewRPr();
rPr.setLang("en-US");
rPr.setSz(1100);
r.setT(str.getString());
} else {
for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) {
CTRElt lt = str.getCTRst().getRArray(i);
CTRPrElt ltPr = lt.getRPr();
if (ltPr == null)
ltPr = lt.addNewRPr();
CTRegularTextRun r = p.addNewR();
CTTextCharacterProperties rPr = r.addNewRPr();
rPr.setLang("en-US");
applyAttributes(ltPr, rPr);
r.setT(lt.getT());
}
}
// Note: the XSSFTextParagraph constructor will create its required XSSFTextRuns from the provided CTTextParagraph
XSSFTextParagraph paragraph = new XSSFTextParagraph(p, ctShape);
_paragraphs.add(paragraph);
return paragraph;
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt in project poi by apache.
the class XSSFRichTextString method getFormatMap.
TreeMap<Integer, CTRPrElt> getFormatMap(CTRst entry) {
int length = 0;
TreeMap<Integer, CTRPrElt> formats = new TreeMap<Integer, CTRPrElt>();
//noinspection deprecation - for performance reasons!
for (CTRElt r : entry.getRArray()) {
String txt = r.getT();
CTRPrElt fmt = r.getRPr();
length += txt.length();
formats.put(length, fmt);
}
return formats;
}
Aggregations