CTRst buildCTRst(String text, TreeMap<Integer, CTRPrElt> formats){ if(text.length() != formats.lastKey()) { throw new IllegalArgumentException("Text length was " + text.length() + " but the last format index was " + formats.lastKey()); } CTRst stf = CTRst.Factory.newInstance(); int runStartIdx = 0; for (Map.Entry<Integer, CTRPrElt> me : formats.entrySet()) { int runEndIdx = me.getKey(); CTRElt run = stf.addNewR(); String fragment = text.substring(runStartIdx, runEndIdx); run.setT(fragment); preserveSpaces(run.xgetT()); CTRPrElt fmt = me.getValue(); if (fmt != null) { run.setRPr(fmt); } runStartIdx = runEndIdx; } return stf; }
/** * Append new text to this text run and apply the specify font to it * * @param text the text to append * @param font the font to apply to the appended text or <code>null</code> if no formatting is required */ public void append(String text, XSSFFont font){ if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> CTRElt lt = st.addNewR(); lt.setT(st.getT()); preserveSpaces(lt.xgetT()); st.unsetT(); } CTRElt lt = st.addNewR(); lt.setT(text); preserveSpaces(lt.xgetT()); if (font != null) { CTRPrElt pr = lt.addNewRPr(); setRunAttributes(font.getCTFont(), pr); } }
/** * Applies a font to the specified characters of a string. * * @param startIndex The start index to apply the font to (inclusive) * @param endIndex The end index to apply to font to (exclusive) * @param font The index of the font to use. */ public void applyFont(int startIndex, int endIndex, Font font) { if (startIndex > endIndex) throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex); if (startIndex < 0 || endIndex > length()) throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex); if (startIndex == endIndex) return; if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> st.addNewR().setT(st.getT()); st.unsetT(); } String text = getString(); XSSFFont xssfFont = (XSSFFont)font; TreeMap<Integer, CTRPrElt> formats = getFormatMap(st); CTRPrElt fmt = CTRPrElt.Factory.newInstance(); setRunAttributes(xssfFont.getCTFont(), fmt); applyFont(formats, startIndex, endIndex, fmt); CTRst newSt = buildCTRst(text, formats); st.set(newSt); }
CTRst buildCTRst(String text, TreeMap<Integer, CTRPrElt> formats){ if(text.length() != formats.lastKey()) { throw new IllegalArgumentException("Text length was " + text.length() + " but the last format index was " + formats.lastKey()); } CTRst st = CTRst.Factory.newInstance(); int runStartIdx = 0; for (Iterator<Integer> it = formats.keySet().iterator(); it.hasNext();) { int runEndIdx = it.next(); CTRElt run = st.addNewR(); String fragment = text.substring(runStartIdx, runEndIdx); run.setT(fragment); preserveSpaces(run.xgetT()); CTRPrElt fmt = formats.get(runEndIdx); if(fmt != null) run.setRPr(fmt); runStartIdx = runEndIdx; } return st; }
CTRst buildCTRst(String text, TreeMap<Integer, CTRPrElt> formats){ if(text.length() != formats.lastKey()) { throw new IllegalArgumentException("Text length was " + text.length() + " but the last format index was " + formats.lastKey()); } CTRst stf = CTRst.Factory.newInstance(); int runStartIdx = 0; for (Map.Entry<Integer, CTRPrElt> me : formats.entrySet()) { int runEndIdx = me.getKey(); CTRElt run = stf.addNewR(); String fragment = text.substring(runStartIdx, runEndIdx); run.setT(fragment); preserveSpaces(run.xgetT()); CTRPrElt fmt = me.getValue(); if (fmt != null) { run.setRPr(fmt); } runStartIdx = runEndIdx; } return stf; }
/** * Append new text to this text run and apply the specify font to it * * @param text the text to append * @param font the font to apply to the appended text or <code>null</code> if no formatting is required */ public void append(String text, XSSFFont font){ if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> CTRElt lt = st.addNewR(); lt.setT(st.getT()); preserveSpaces(lt.xgetT()); st.unsetT(); } CTRElt lt = st.addNewR(); lt.setT(text); preserveSpaces(lt.xgetT()); CTRPrElt pr = lt.addNewRPr(); if(font != null) setRunAttributes(font.getCTFont(), pr); }
/** * Append new text to this text run and apply the specify font to it * * @param text the text to append * @param font the font to apply to the appended text or <code>null</code> if no formatting is required */ public void append(String text, XSSFFont font){ if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> CTRElt lt = st.addNewR(); lt.setT(st.getT()); preserveSpaces(lt.xgetT()); st.unsetT(); } CTRElt lt = st.addNewR(); lt.setT(text); preserveSpaces(lt.xgetT()); if (font != null) { CTRPrElt pr = lt.addNewRPr(); setRunAttributes(font.getCTFont(), pr); } }
/** * Applies a font to the specified characters of a string. * * @param startIndex The start index to apply the font to (inclusive) * @param endIndex The end index to apply to font to (exclusive) * @param font The index of the font to use. */ public void applyFont(int startIndex, int endIndex, Font font) { if (startIndex > endIndex) throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex); if (startIndex < 0 || endIndex > length()) throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex); if (startIndex == endIndex) return; if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> st.addNewR().setT(st.getT()); st.unsetT(); } String text = getString(); XSSFFont xssfFont = (XSSFFont)font; TreeMap<Integer, CTRPrElt> formats = getFormatMap(st); CTRPrElt fmt = CTRPrElt.Factory.newInstance(); setRunAttributes(xssfFont.getCTFont(), fmt); applyFont(formats, startIndex, endIndex, fmt); CTRst newSt = buildCTRst(text, formats); st.set(newSt); }
/** * Applies a font to the specified characters of a string. * * @param startIndex The start index to apply the font to (inclusive) * @param endIndex The end index to apply to font to (exclusive) * @param font The index of the font to use. */ public void applyFont(int startIndex, int endIndex, Font font) { if (startIndex > endIndex) throw new IllegalArgumentException("Start index must be less than end index."); if (startIndex < 0 || endIndex > length()) throw new IllegalArgumentException("Start and end index not in range."); if (startIndex == endIndex) return; if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> st.addNewR().setT(st.getT()); st.unsetT(); } String text = getString(); XSSFFont xssfFont = (XSSFFont)font; TreeMap<Integer, CTRPrElt> formats = getFormatMap(st); CTRPrElt fmt = CTRPrElt.Factory.newInstance(); setRunAttributes(xssfFont.getCTFont(), fmt); applyFont(formats, startIndex, endIndex, fmt); CTRst newSt = buildCTRst(text, formats); st.set(newSt); }