CTRElt lt = st.addNewR();
lt.setT(text);
preserveSpaces(lt.xgetT());
- CTRPrElt pr = lt.addNewRPr();
- if(font != null) setRunAttributes(font.getCTFont(), pr);
+
+ if (font != null) {
+ CTRPrElt pr = lt.addNewRPr();
+ setRunAttributes(font.getCTFont(), pr);
+ }
}
/**
if(ctFont.sizeOfShadowArray() > 0) pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal());
if(ctFont.sizeOfStrikeArray() > 0) pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal());
}
+
+ /**
+ * Does this string have any explicit formatting applied, or is
+ * it just text in the default style?
+ */
+ public boolean hasFormatting() {
+ @SuppressWarnings("deprecation")
+ CTRElt[] rs = st.getRArray();
+ if (rs == null || rs.length == 0) {
+ return false;
+ }
+ for (CTRElt r : rs) {
+ if (r.isSetRPr()) return true;
+ }
+ return false;
+ }
/**
* Removes any formatting that may have been applied to the string.
public void testCreate() {
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
assertEquals("Apache POI", rt.getString());
+ assertEquals(false, rt.hasFormatting());
CTRst st = rt.getCTRst();
assertTrue(st.isSetT());
assertEquals("Apache POI", st.getT());
+ assertEquals(false, rt.hasFormatting());
rt.append(" is cool stuff");
assertEquals(2, st.sizeOfRArray());
assertFalse(st.isSetT());
assertEquals("Apache POI is cool stuff", rt.getString());
+ assertEquals(false, rt.hasFormatting());
}
public void testEmpty() {
rt.append("89");
assertEquals("123456789", rt.getString());
+ assertEquals(false, rt.hasFormatting());
XSSFFont font1 = new XSSFFont();
font1.setBold(true);
rt.applyFont(2, 5, font1);
+ assertEquals(true, rt.hasFormatting());
assertEquals(4, rt.numFormattingRuns());
assertEquals(0, rt.getIndexOfFormattingRun(0));
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
assertEquals("Apache POI", rt.getString());
+ assertEquals(false, rt.hasFormatting());
rt.clearFormatting();
assertTrue(st.isSetT());
assertEquals("Apache POI", rt.getString());
assertEquals(0, rt.numFormattingRuns());
+ assertEquals(false, rt.hasFormatting());
XSSFFont font = new XSSFFont();
font.setBold(true);
rt.applyFont(7, 10, font);
assertEquals(2, rt.numFormattingRuns());
+ assertEquals(true, rt.hasFormatting());
+
rt.clearFormatting();
+
assertEquals("Apache POI", rt.getString());
assertEquals(0, rt.numFormattingRuns());
+ assertEquals(false, rt.hasFormatting());
}
public void testGetFonts() {