You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFRichTextString.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import java.util.*;
  17. import java.util.regex.Pattern;
  18. import java.util.regex.Matcher;
  19. import javax.xml.namespace.QName;
  20. import org.apache.poi.ss.usermodel.Font;
  21. import org.apache.poi.ss.usermodel.RichTextString;
  22. import org.apache.poi.xssf.model.StylesTable;
  23. import org.apache.poi.xssf.model.ThemesTable;
  24. import org.apache.poi.util.Internal;
  25. import org.apache.xmlbeans.XmlCursor;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;
  32. /**
  33. * Rich text unicode string. These strings can have fonts applied to arbitary parts of the string.
  34. *
  35. * <p>
  36. * Most strings in a workbook have formatting applied at the cell level, that is, the entire string in the cell has the
  37. * same formatting applied. In these cases, the formatting for the cell is stored in the styles part,
  38. * and the string for the cell can be shared across the workbook. The following code illustrates the example.
  39. * </p>
  40. *
  41. * <blockquote>
  42. * <pre>
  43. * cell1.setCellValue(new XSSFRichTextString("Apache POI"));
  44. * cell2.setCellValue(new XSSFRichTextString("Apache POI"));
  45. * cell3.setCellValue(new XSSFRichTextString("Apache POI"));
  46. * </pre>
  47. * </blockquote>
  48. * In the above example all three cells will use the same string cached on workbook level.
  49. *
  50. * <p>
  51. * Some strings in the workbook may have formatting applied at a level that is more granular than the cell level.
  52. * For instance, specific characters within the string may be bolded, have coloring, italicizing, etc.
  53. * In these cases, the formatting is stored along with the text in the string table, and is treated as
  54. * a unique entry in the workbook. The following xml and code snippet illustrate this.
  55. * </p>
  56. *
  57. * <blockquote>
  58. * <pre>
  59. * XSSFRichTextString s1 = new XSSFRichTextString("Apache POI");
  60. * s1.applyFont(boldArial);
  61. * cell1.setCellValue(s1);
  62. *
  63. * XSSFRichTextString s2 = new XSSFRichTextString("Apache POI");
  64. * s2.applyFont(italicCourier);
  65. * cell2.setCellValue(s2);
  66. * </pre>
  67. * </blockquote>
  68. */
  69. public class XSSFRichTextString implements RichTextString {
  70. private static final Pattern utfPtrn = Pattern.compile("_x([0-9A-Fa-f]{4})_");
  71. private CTRst st;
  72. private StylesTable styles;
  73. /**
  74. * Create a rich text string
  75. */
  76. public XSSFRichTextString(String str) {
  77. st = CTRst.Factory.newInstance();
  78. st.setT(str);
  79. preserveSpaces(st.xgetT());
  80. }
  81. /**
  82. * Create empty rich text string and initialize it with empty string
  83. */
  84. public XSSFRichTextString() {
  85. st = CTRst.Factory.newInstance();
  86. }
  87. /**
  88. * Create a rich text string from the supplied XML bean
  89. */
  90. public XSSFRichTextString(CTRst st) {
  91. this.st = st;
  92. }
  93. /**
  94. * Applies a font to the specified characters of a string.
  95. *
  96. * @param startIndex The start index to apply the font to (inclusive)
  97. * @param endIndex The end index to apply the font to (exclusive)
  98. * @param fontIndex The font to use.
  99. */
  100. public void applyFont(int startIndex, int endIndex, short fontIndex) {
  101. XSSFFont font;
  102. if(styles == null) {
  103. //style table is not set, remember fontIndex and set the run properties later,
  104. //when setStylesTableReference is called
  105. font = new XSSFFont();
  106. font.setFontName("#" + fontIndex);
  107. } else {
  108. font = styles.getFontAt(fontIndex);
  109. }
  110. applyFont(startIndex, endIndex, font);
  111. }
  112. /**
  113. * Applies a font to the specified characters of a string.
  114. *
  115. * @param startIndex The start index to apply the font to (inclusive)
  116. * @param endIndex The end index to apply to font to (exclusive)
  117. * @param font The index of the font to use.
  118. */
  119. public void applyFont(int startIndex, int endIndex, Font font) {
  120. if (startIndex > endIndex)
  121. throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex);
  122. if (startIndex < 0 || endIndex > length())
  123. throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex);
  124. if (startIndex == endIndex)
  125. return;
  126. if(st.sizeOfRArray() == 0 && st.isSetT()) {
  127. //convert <t>string</t> into a text run: <r><t>string</t></r>
  128. st.addNewR().setT(st.getT());
  129. st.unsetT();
  130. }
  131. String text = getString();
  132. XSSFFont xssfFont = (XSSFFont)font;
  133. TreeMap<Integer, CTRPrElt> formats = getFormatMap(st);
  134. CTRPrElt fmt = CTRPrElt.Factory.newInstance();
  135. setRunAttributes(xssfFont.getCTFont(), fmt);
  136. applyFont(formats, startIndex, endIndex, fmt);
  137. CTRst newSt = buildCTRst(text, formats);
  138. st.set(newSt);
  139. }
  140. /**
  141. * Sets the font of the entire string.
  142. * @param font The font to use.
  143. */
  144. public void applyFont(Font font) {
  145. String text = getString();
  146. applyFont(0, text.length(), font);
  147. }
  148. /**
  149. * Applies the specified font to the entire string.
  150. *
  151. * @param fontIndex the font to apply.
  152. */
  153. public void applyFont(short fontIndex) {
  154. XSSFFont font;
  155. if(styles == null) {
  156. font = new XSSFFont();
  157. font.setFontName("#" + fontIndex);
  158. } else {
  159. font = styles.getFontAt(fontIndex);
  160. }
  161. String text = getString();
  162. applyFont(0, text.length(), font);
  163. }
  164. /**
  165. * Append new text to this text run and apply the specify font to it
  166. *
  167. * @param text the text to append
  168. * @param font the font to apply to the appended text or <code>null</code> if no formatting is required
  169. */
  170. public void append(String text, XSSFFont font){
  171. if(st.sizeOfRArray() == 0 && st.isSetT()) {
  172. //convert <t>string</t> into a text run: <r><t>string</t></r>
  173. CTRElt lt = st.addNewR();
  174. lt.setT(st.getT());
  175. preserveSpaces(lt.xgetT());
  176. st.unsetT();
  177. }
  178. CTRElt lt = st.addNewR();
  179. lt.setT(text);
  180. preserveSpaces(lt.xgetT());
  181. if (font != null) {
  182. CTRPrElt pr = lt.addNewRPr();
  183. setRunAttributes(font.getCTFont(), pr);
  184. }
  185. }
  186. /**
  187. * Append new text to this text run
  188. *
  189. * @param text the text to append
  190. */
  191. public void append(String text){
  192. append(text, null);
  193. }
  194. /**
  195. * Copy font attributes from CTFont bean into CTRPrElt bean
  196. */
  197. private void setRunAttributes(CTFont ctFont, CTRPrElt pr){
  198. if(ctFont.sizeOfBArray() > 0) pr.addNewB().setVal(ctFont.getBArray(0).getVal());
  199. if(ctFont.sizeOfUArray() > 0) pr.addNewU().setVal(ctFont.getUArray(0).getVal());
  200. if(ctFont.sizeOfIArray() > 0) pr.addNewI().setVal(ctFont.getIArray(0).getVal());
  201. if(ctFont.sizeOfColorArray() > 0) {
  202. CTColor c1 = ctFont.getColorArray(0);
  203. CTColor c2 = pr.addNewColor();
  204. if(c1.isSetAuto()) c2.setAuto(c1.getAuto());
  205. if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed());
  206. if(c1.isSetRgb()) c2.setRgb(c1.getRgb());
  207. if(c1.isSetTheme()) c2.setTheme(c1.getTheme());
  208. if(c1.isSetTint()) c2.setTint(c1.getTint());
  209. }
  210. if(ctFont.sizeOfSzArray() > 0) pr.addNewSz().setVal(ctFont.getSzArray(0).getVal());
  211. if(ctFont.sizeOfNameArray() > 0) pr.addNewRFont().setVal(ctFont.getNameArray(0).getVal());
  212. if(ctFont.sizeOfFamilyArray() > 0) pr.addNewFamily().setVal(ctFont.getFamilyArray(0).getVal());
  213. if(ctFont.sizeOfSchemeArray() > 0) pr.addNewScheme().setVal(ctFont.getSchemeArray(0).getVal());
  214. if(ctFont.sizeOfCharsetArray() > 0) pr.addNewCharset().setVal(ctFont.getCharsetArray(0).getVal());
  215. if(ctFont.sizeOfCondenseArray() > 0) pr.addNewCondense().setVal(ctFont.getCondenseArray(0).getVal());
  216. if(ctFont.sizeOfExtendArray() > 0) pr.addNewExtend().setVal(ctFont.getExtendArray(0).getVal());
  217. if(ctFont.sizeOfVertAlignArray() > 0) pr.addNewVertAlign().setVal(ctFont.getVertAlignArray(0).getVal());
  218. if(ctFont.sizeOfOutlineArray() > 0) pr.addNewOutline().setVal(ctFont.getOutlineArray(0).getVal());
  219. if(ctFont.sizeOfShadowArray() > 0) pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal());
  220. if(ctFont.sizeOfStrikeArray() > 0) pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal());
  221. }
  222. /**
  223. * Does this string have any explicit formatting applied, or is
  224. * it just text in the default style?
  225. */
  226. public boolean hasFormatting() {
  227. //noinspection deprecation - for performance reasons!
  228. CTRElt[] rs = st.getRArray();
  229. if (rs == null || rs.length == 0) {
  230. return false;
  231. }
  232. for (CTRElt r : rs) {
  233. if (r.isSetRPr()) return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * Removes any formatting that may have been applied to the string.
  239. */
  240. public void clearFormatting() {
  241. String text = getString();
  242. st.setRArray(null);
  243. st.setT(text);
  244. }
  245. /**
  246. * The index within the string to which the specified formatting run applies.
  247. *
  248. * @param index the index of the formatting run
  249. * @return the index within the string.
  250. */
  251. public int getIndexOfFormattingRun(int index) {
  252. if(st.sizeOfRArray() == 0) return 0;
  253. int pos = 0;
  254. for(int i = 0; i < st.sizeOfRArray(); i++){
  255. CTRElt r = st.getRArray(i);
  256. if(i == index) return pos;
  257. pos += r.getT().length();
  258. }
  259. return -1;
  260. }
  261. /**
  262. * Returns the number of characters this format run covers.
  263. *
  264. * @param index the index of the formatting run
  265. * @return the number of characters this format run covers
  266. */
  267. public int getLengthOfFormattingRun(int index) {
  268. if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) {
  269. return -1;
  270. }
  271. CTRElt r = st.getRArray(index);
  272. return r.getT().length();
  273. }
  274. /**
  275. * Returns the plain string representation.
  276. */
  277. public String getString() {
  278. if(st.sizeOfRArray() == 0) {
  279. return utfDecode(st.getT());
  280. }
  281. StringBuilder buf = new StringBuilder();
  282. //noinspection deprecation - for performance reasons!
  283. for(CTRElt r : st.getRArray()){
  284. buf.append(r.getT());
  285. }
  286. return utfDecode(buf.toString());
  287. }
  288. /**
  289. * Removes any formatting and sets new string value
  290. *
  291. * @param s new string value
  292. */
  293. public void setString(String s){
  294. clearFormatting();
  295. st.setT(s);
  296. preserveSpaces(st.xgetT());
  297. }
  298. /**
  299. * Returns the plain string representation.
  300. */
  301. public String toString() {
  302. return getString();
  303. }
  304. /**
  305. * Returns the number of characters in this string.
  306. */
  307. public int length() {
  308. return getString().length();
  309. }
  310. /**
  311. * @return The number of formatting runs used.
  312. */
  313. public int numFormattingRuns() {
  314. return st.sizeOfRArray();
  315. }
  316. /**
  317. * Gets a copy of the font used in a particular formatting run.
  318. *
  319. * @param index the index of the formatting run
  320. * @return A copy of the font used or null if no formatting is applied to the specified text run.
  321. */
  322. public XSSFFont getFontOfFormattingRun(int index) {
  323. if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) {
  324. return null;
  325. }
  326. CTRElt r = st.getRArray(index);
  327. if(r.getRPr() != null) {
  328. XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr()));
  329. fnt.setThemesTable(getThemesTable());
  330. return fnt;
  331. }
  332. return null;
  333. }
  334. /**
  335. * Return a copy of the font in use at a particular index.
  336. *
  337. * @param index The index.
  338. * @return A copy of the font that's currently being applied at that
  339. * index or null if no font is being applied or the
  340. * index is out of range.
  341. */
  342. public XSSFFont getFontAtIndex( int index ) {
  343. final ThemesTable themes = getThemesTable();
  344. int pos = 0;
  345. //noinspection deprecation - for performance reasons!
  346. for(CTRElt r : st.getRArray()){
  347. final int length = r.getT().length();
  348. if(index >= pos && index < pos + length) {
  349. XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr()));
  350. fnt.setThemesTable(themes);
  351. return fnt;
  352. }
  353. pos += length;
  354. }
  355. return null;
  356. }
  357. /**
  358. * Return the underlying xml bean
  359. */
  360. @Internal
  361. public CTRst getCTRst() {
  362. return st;
  363. }
  364. protected void setStylesTableReference(StylesTable tbl){
  365. styles = tbl;
  366. if(st.sizeOfRArray() > 0) {
  367. //noinspection deprecation - for performance reasons!
  368. for (CTRElt r : st.getRArray()) {
  369. CTRPrElt pr = r.getRPr();
  370. if(pr != null && pr.sizeOfRFontArray() > 0){
  371. String fontName = pr.getRFontArray(0).getVal();
  372. if(fontName.startsWith("#")){
  373. int idx = Integer.parseInt(fontName.substring(1));
  374. XSSFFont font = styles.getFontAt(idx);
  375. pr.removeRFont(0);
  376. setRunAttributes(font.getCTFont(), pr);
  377. }
  378. }
  379. }
  380. }
  381. }
  382. /**
  383. *
  384. * CTRPrElt --> CTFont adapter
  385. */
  386. protected static CTFont toCTFont(CTRPrElt pr){
  387. CTFont ctFont = CTFont.Factory.newInstance();
  388. // Bug 58315: there are files where there is no pr-entry for a RichTextString
  389. if(pr == null) {
  390. return ctFont;
  391. }
  392. if(pr.sizeOfBArray() > 0) ctFont.addNewB().setVal(pr.getBArray(0).getVal());
  393. if(pr.sizeOfUArray() > 0) ctFont.addNewU().setVal(pr.getUArray(0).getVal());
  394. if(pr.sizeOfIArray() > 0) ctFont.addNewI().setVal(pr.getIArray(0).getVal());
  395. if(pr.sizeOfColorArray() > 0) {
  396. CTColor c1 = pr.getColorArray(0);
  397. CTColor c2 = ctFont.addNewColor();
  398. if(c1.isSetAuto()) c2.setAuto(c1.getAuto());
  399. if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed());
  400. if(c1.isSetRgb()) c2.setRgb(c1.getRgb());
  401. if(c1.isSetTheme()) c2.setTheme(c1.getTheme());
  402. if(c1.isSetTint()) c2.setTint(c1.getTint());
  403. }
  404. if(pr.sizeOfSzArray() > 0) ctFont.addNewSz().setVal(pr.getSzArray(0).getVal());
  405. if(pr.sizeOfRFontArray() > 0) ctFont.addNewName().setVal(pr.getRFontArray(0).getVal());
  406. if(pr.sizeOfFamilyArray() > 0) ctFont.addNewFamily().setVal(pr.getFamilyArray(0).getVal());
  407. if(pr.sizeOfSchemeArray() > 0) ctFont.addNewScheme().setVal(pr.getSchemeArray(0).getVal());
  408. if(pr.sizeOfCharsetArray() > 0) ctFont.addNewCharset().setVal(pr.getCharsetArray(0).getVal());
  409. if(pr.sizeOfCondenseArray() > 0) ctFont.addNewCondense().setVal(pr.getCondenseArray(0).getVal());
  410. if(pr.sizeOfExtendArray() > 0) ctFont.addNewExtend().setVal(pr.getExtendArray(0).getVal());
  411. if(pr.sizeOfVertAlignArray() > 0) ctFont.addNewVertAlign().setVal(pr.getVertAlignArray(0).getVal());
  412. if(pr.sizeOfOutlineArray() > 0) ctFont.addNewOutline().setVal(pr.getOutlineArray(0).getVal());
  413. if(pr.sizeOfShadowArray() > 0) ctFont.addNewShadow().setVal(pr.getShadowArray(0).getVal());
  414. if(pr.sizeOfStrikeArray() > 0) ctFont.addNewStrike().setVal(pr.getStrikeArray(0).getVal());
  415. return ctFont;
  416. }
  417. /**
  418. * Add the xml:spaces="preserve" attribute if the string has leading or trailing spaces
  419. *
  420. * @param xs the string to check
  421. */
  422. protected static void preserveSpaces(STXstring xs) {
  423. String text = xs.getStringValue();
  424. if (text != null && text.length() > 0) {
  425. char firstChar = text.charAt(0);
  426. char lastChar = text.charAt(text.length() - 1);
  427. if(Character.isWhitespace(firstChar) || Character.isWhitespace(lastChar)) {
  428. XmlCursor c = xs.newCursor();
  429. c.toNextToken();
  430. c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");
  431. c.dispose();
  432. }
  433. }
  434. }
  435. /**
  436. * For all characters which cannot be represented in XML as defined by the XML 1.0 specification,
  437. * the characters are escaped using the Unicode numerical character representation escape character
  438. * format _xHHHH_, where H represents a hexadecimal character in the character's value.
  439. * <p>
  440. * Example: The Unicode character 0D is invalid in an XML 1.0 document,
  441. * so it shall be escaped as <code>_x000D_</code>.
  442. * </p>
  443. * See section 3.18.9 in the OOXML spec.
  444. *
  445. * @param value the string to decode
  446. * @return the decoded string
  447. */
  448. static String utfDecode(String value){
  449. if(value == null || !value.contains("_x")) {
  450. return value;
  451. }
  452. StringBuilder buf = new StringBuilder();
  453. Matcher m = utfPtrn.matcher(value);
  454. int idx = 0;
  455. while(m.find()) {
  456. int pos = m.start();
  457. if( pos > idx) {
  458. buf.append(value.substring(idx, pos));
  459. }
  460. String code = m.group(1);
  461. int icode = Integer.decode("0x" + code);
  462. buf.append((char)icode);
  463. idx = m.end();
  464. }
  465. // small optimization: don't go via StringBuilder if not necessary,
  466. // the encodings are very rare, so we should almost always go via this shortcut.
  467. if(idx == 0) {
  468. return value;
  469. }
  470. buf.append(value.substring(idx));
  471. return buf.toString();
  472. }
  473. void applyFont(TreeMap<Integer, CTRPrElt> formats, int startIndex, int endIndex, CTRPrElt fmt) {
  474. // delete format runs that fit between startIndex and endIndex
  475. // runs intersecting startIndex and endIndex remain
  476. int runStartIdx = 0;
  477. for (Iterator<Integer> it = formats.keySet().iterator(); it.hasNext();) {
  478. int runEndIdx = it.next();
  479. if (runStartIdx >= startIndex && runEndIdx < endIndex) {
  480. it.remove();
  481. }
  482. runStartIdx = runEndIdx;
  483. }
  484. if(startIndex > 0 && !formats.containsKey(startIndex)) {
  485. // If there's a format that starts later in the string, make it start now
  486. for(Map.Entry<Integer, CTRPrElt> entry : formats.entrySet()) {
  487. if(entry.getKey() > startIndex) {
  488. formats.put(startIndex, entry.getValue());
  489. break;
  490. }
  491. }
  492. }
  493. formats.put(endIndex, fmt);
  494. // assure that the range [startIndex, endIndex] consists if a single run
  495. // there can be two or three runs depending whether startIndex or endIndex
  496. // intersected existing format runs
  497. SortedMap<Integer, CTRPrElt> sub = formats.subMap(startIndex, endIndex);
  498. while(sub.size() > 1) sub.remove(sub.lastKey());
  499. }
  500. TreeMap<Integer, CTRPrElt> getFormatMap(CTRst entry){
  501. int length = 0;
  502. TreeMap<Integer, CTRPrElt> formats = new TreeMap<Integer, CTRPrElt>();
  503. //noinspection deprecation - for performance reasons!
  504. for (CTRElt r : entry.getRArray()) {
  505. String txt = r.getT();
  506. CTRPrElt fmt = r.getRPr();
  507. length += txt.length();
  508. formats.put(length, fmt);
  509. }
  510. return formats;
  511. }
  512. CTRst buildCTRst(String text, TreeMap<Integer, CTRPrElt> formats){
  513. if(text.length() != formats.lastKey()) {
  514. throw new IllegalArgumentException("Text length was " + text.length() +
  515. " but the last format index was " + formats.lastKey());
  516. }
  517. CTRst stf = CTRst.Factory.newInstance();
  518. int runStartIdx = 0;
  519. for (Map.Entry<Integer, CTRPrElt> me : formats.entrySet()) {
  520. int runEndIdx = me.getKey();
  521. CTRElt run = stf.addNewR();
  522. String fragment = text.substring(runStartIdx, runEndIdx);
  523. run.setT(fragment);
  524. preserveSpaces(run.xgetT());
  525. CTRPrElt fmt = me.getValue();
  526. if (fmt != null) {
  527. run.setRPr(fmt);
  528. }
  529. runStartIdx = runEndIdx;
  530. }
  531. return stf;
  532. }
  533. private ThemesTable getThemesTable() {
  534. if(styles == null) return null;
  535. return styles.getTheme();
  536. }
  537. }