您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestTextRun.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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.hslf.usermodel;
  16. import static org.junit.Assert.*;
  17. import java.awt.Color;
  18. import java.io.*;
  19. import java.util.List;
  20. import org.apache.poi.POIDataSamples;
  21. import org.apache.poi.hslf.model.textproperties.TextPropCollection;
  22. import org.apache.poi.hslf.record.*;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. /**
  26. * Tests for TextRuns
  27. *
  28. * @author Nick Burch (nick at torchbox dot com)
  29. */
  30. public final class TestTextRun {
  31. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  32. // SlideShow primed on the test data
  33. private HSLFSlideShow ss;
  34. private HSLFSlideShow ssRich;
  35. @Before
  36. public void setUp() throws IOException {
  37. // Basic (non rich) test file
  38. ss = new HSLFSlideShow(_slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
  39. // Rich test file
  40. ssRich = new HSLFSlideShow(_slTests.openResourceAsStream("Single_Coloured_Page.ppt"));
  41. }
  42. /**
  43. * Test to ensure that getting the text works correctly
  44. */
  45. @Test
  46. public void testGetText() {
  47. HSLFSlide slideOne = ss.getSlides().get(0);
  48. List<List<HSLFTextParagraph>> textParas = slideOne.getTextParagraphs();
  49. assertEquals(2, textParas.size());
  50. // Get text works with \n
  51. assertEquals("This is a test title", HSLFTextParagraph.getText(textParas.get(0)));
  52. assertEquals("This is a test subtitle\nThis is on page 1", HSLFTextParagraph.getText(textParas.get(1)));
  53. // Raw text has \r instead
  54. assertEquals("This is a test title", HSLFTextParagraph.getRawText(textParas.get(0)));
  55. assertEquals("This is a test subtitle\rThis is on page 1", HSLFTextParagraph.getRawText(textParas.get(1)));
  56. // Now check on a rich text run
  57. HSLFSlide slideOneR = ssRich.getSlides().get(0);
  58. textParas = slideOneR.getTextParagraphs();
  59. assertEquals(2, textParas.size());
  60. assertEquals("This is a title, it\u2019s in black", HSLFTextParagraph.getText(textParas.get(0)));
  61. assertEquals("This is the subtitle, in bold\nThis bit is blue and italic\nThis bit is red (normal)", HSLFTextParagraph.getText(textParas.get(1)));
  62. assertEquals("This is a title, it\u2019s in black", HSLFTextParagraph.getRawText(textParas.get(0)));
  63. assertEquals("This is the subtitle, in bold\rThis bit is blue and italic\rThis bit is red (normal)", HSLFTextParagraph.getRawText(textParas.get(1)));
  64. }
  65. /**
  66. * Test to ensure changing non rich text bytes->bytes works correctly
  67. */
  68. @Test
  69. public void testSetText() {
  70. HSLFSlide slideOne = ss.getSlides().get(0);
  71. List<List<HSLFTextParagraph>> textRuns = slideOne.getTextParagraphs();
  72. HSLFTextParagraph run = textRuns.get(0).get(0);
  73. HSLFTextRun tr = run.getTextRuns().get(0);
  74. // Check current text
  75. assertEquals("This is a test title", tr.getRawText());
  76. // Change
  77. String changeTo = "New test title";
  78. tr.setText(changeTo);
  79. assertEquals(changeTo, tr.getRawText());
  80. // Ensure trailing \n's are NOT stripped, it is legal to set a text with a trailing '\r'
  81. tr.setText(changeTo + "\n");
  82. assertEquals(changeTo + "\r", tr.getRawText());
  83. }
  84. /**
  85. * Test to ensure that changing non rich text between bytes and
  86. * chars works correctly
  87. */
  88. @SuppressWarnings("unused")
  89. @Test
  90. public void testAdvancedSetText() {
  91. HSLFSlide slideOne = ss.getSlides().get(0);
  92. List<HSLFTextParagraph> paras = slideOne.getTextParagraphs().get(0);
  93. HSLFTextParagraph para = paras.get(0);
  94. TextHeaderAtom tha = null;
  95. TextBytesAtom tba = null;
  96. TextCharsAtom tca = null;
  97. for (Record r : para.getRecords()) {
  98. if (r instanceof TextHeaderAtom) tha = (TextHeaderAtom)r;
  99. else if (r instanceof TextBytesAtom) tba = (TextBytesAtom)r;
  100. else if (r instanceof TextCharsAtom) tca = (TextCharsAtom)r;
  101. }
  102. // Bytes -> Bytes
  103. assertNull(tca);
  104. assertNotNull(tba);
  105. // assertFalse(run._isUnicode);
  106. assertEquals("This is a test title", para.getTextRuns().get(0).getRawText());
  107. String changeBytesOnly = "New Test Title";
  108. HSLFTextParagraph.setText(paras, changeBytesOnly);
  109. para = paras.get(0);
  110. tha = null; tba = null; tca = null;
  111. for (Record r : para.getRecords()) {
  112. if (r instanceof TextHeaderAtom) tha = (TextHeaderAtom)r;
  113. else if (r instanceof TextBytesAtom) tba = (TextBytesAtom)r;
  114. else if (r instanceof TextCharsAtom) tca = (TextCharsAtom)r;
  115. }
  116. assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
  117. assertNull(tca);
  118. assertNotNull(tba);
  119. // Bytes -> Chars
  120. assertNull(tca);
  121. assertNotNull(tba);
  122. assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
  123. String changeByteChar = "This is a test title with a '\u0121' g with a dot";
  124. HSLFTextParagraph.setText(paras, changeByteChar);
  125. para = paras.get(0);
  126. tha = null; tba = null; tca = null;
  127. for (Record r : para.getRecords()) {
  128. if (r instanceof TextHeaderAtom) tha = (TextHeaderAtom)r;
  129. else if (r instanceof TextBytesAtom) tba = (TextBytesAtom)r;
  130. else if (r instanceof TextCharsAtom) tca = (TextCharsAtom)r;
  131. }
  132. assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
  133. assertNotNull(tca);
  134. assertNull(tba);
  135. // Chars -> Chars
  136. assertNull(tba);
  137. assertNotNull(tca);
  138. assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
  139. String changeCharChar = "This is a test title with a '\u0147' N with a hat";
  140. HSLFTextParagraph.setText(paras, changeCharChar);
  141. para = paras.get(0);
  142. tha = null; tba = null; tca = null;
  143. for (Record r : para.getRecords()) {
  144. if (r instanceof TextHeaderAtom) tha = (TextHeaderAtom)r;
  145. else if (r instanceof TextBytesAtom) tba = (TextBytesAtom)r;
  146. else if (r instanceof TextCharsAtom) tca = (TextCharsAtom)r;
  147. }
  148. assertEquals(changeCharChar, HSLFTextParagraph.getRawText(paras));
  149. assertNotNull(tca);
  150. assertNull(tba);
  151. }
  152. /**
  153. * Tests to ensure that non rich text has the right default rich text run
  154. * set up for it
  155. */
  156. @Test
  157. public void testGetRichTextNonRich() {
  158. HSLFSlide slideOne = ss.getSlides().get(0);
  159. List<List<HSLFTextParagraph>> textParass = slideOne.getTextParagraphs();
  160. assertEquals(2, textParass.size());
  161. List<HSLFTextParagraph> trA = textParass.get(0);
  162. List<HSLFTextParagraph> trB = textParass.get(1);
  163. assertEquals(1, trA.size());
  164. assertEquals(2, trB.size());
  165. HSLFTextRun rtrA = trA.get(0).getTextRuns().get(0);
  166. HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
  167. assertEquals(HSLFTextParagraph.getRawText(trA), rtrA.getRawText());
  168. assertEquals(HSLFTextParagraph.getRawText(trB.subList(0, 1)), rtrB.getRawText());
  169. }
  170. /**
  171. * Tests to ensure that the rich text runs are built up correctly
  172. */
  173. @Test
  174. public void testGetRichText() {
  175. HSLFSlide slideOne = ssRich.getSlides().get(0);
  176. List<List<HSLFTextParagraph>> textParass = slideOne.getTextParagraphs();
  177. assertEquals(2, textParass.size());
  178. List<HSLFTextParagraph> trA = textParass.get(0);
  179. List<HSLFTextParagraph> trB = textParass.get(1);
  180. assertEquals(1, trA.size());
  181. assertEquals(3, trB.size());
  182. HSLFTextRun rtrA = trA.get(0).getTextRuns().get(0);
  183. HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
  184. HSLFTextRun rtrC = trB.get(1).getTextRuns().get(0);
  185. HSLFTextRun rtrD = trB.get(2).getTextRuns().get(0);
  186. assertEquals(HSLFTextParagraph.getRawText(trA), rtrA.getRawText());
  187. String trBstr = HSLFTextParagraph.getRawText(trB);
  188. assertEquals(trBstr.substring(0, 30), rtrB.getRawText());
  189. assertEquals(trBstr.substring(30,58), rtrC.getRawText());
  190. assertEquals(trBstr.substring(58,82), rtrD.getRawText());
  191. // Same paragraph styles
  192. assertEquals(trB.get(0).getParagraphStyle(), trB.get(1).getParagraphStyle());
  193. assertEquals(trB.get(0).getParagraphStyle(), trB.get(2).getParagraphStyle());
  194. // Different char styles
  195. assertNotEquals(rtrB.getCharacterStyle(), rtrC.getCharacterStyle());
  196. assertNotEquals(rtrB.getCharacterStyle(), rtrD.getCharacterStyle());
  197. assertNotEquals(rtrC.getCharacterStyle(), rtrD.getCharacterStyle());
  198. }
  199. /**
  200. * Tests to ensure that setting the text where the text isn't rich,
  201. * ensuring that everything stays with the same default styling
  202. */
  203. @Test
  204. public void testSetTextWhereNotRich() {
  205. HSLFSlide slideOne = ss.getSlides().get(0);
  206. List<List<HSLFTextParagraph>> textParass = slideOne.getTextParagraphs();
  207. List<HSLFTextParagraph> trB = textParass.get(0);
  208. assertEquals(1, trB.size());
  209. HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
  210. assertEquals(HSLFTextParagraph.getText(trB), rtrB.getRawText());
  211. // Change text via normal
  212. HSLFTextParagraph.setText(trB, "Test Foo Test");
  213. rtrB = trB.get(0).getTextRuns().get(0);
  214. assertEquals("Test Foo Test", HSLFTextParagraph.getRawText(trB));
  215. assertEquals("Test Foo Test", rtrB.getRawText());
  216. }
  217. /**
  218. * Tests to ensure that setting the text where the text is rich
  219. * sets everything to the same styling
  220. */
  221. @Test
  222. public void testSetTextWhereRich() {
  223. HSLFSlide slideOne = ssRich.getSlides().get(0);
  224. List<List<HSLFTextParagraph>> textParass = slideOne.getTextParagraphs();
  225. List<HSLFTextParagraph> trB = textParass.get(1);
  226. assertEquals(3, trB.size());
  227. HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
  228. HSLFTextRun rtrC = trB.get(1).getTextRuns().get(0);
  229. HSLFTextRun rtrD = trB.get(2).getTextRuns().get(0);
  230. TextPropCollection tpBP = rtrB.getTextParagraph().getParagraphStyle();
  231. TextPropCollection tpBC = rtrB.getCharacterStyle();
  232. TextPropCollection tpCP = rtrC.getTextParagraph().getParagraphStyle();
  233. TextPropCollection tpCC = rtrC.getCharacterStyle();
  234. TextPropCollection tpDP = rtrD.getTextParagraph().getParagraphStyle();
  235. TextPropCollection tpDC = rtrD.getCharacterStyle();
  236. // assertEquals(trB.getRawText().substring(0, 30), rtrB.getRawText());
  237. assertNotNull(tpBP);
  238. assertNotNull(tpBC);
  239. assertNotNull(tpCP);
  240. assertNotNull(tpCC);
  241. assertNotNull(tpDP);
  242. assertNotNull(tpDC);
  243. assertEquals(tpBP,tpCP);
  244. assertEquals(tpBP,tpDP);
  245. assertEquals(tpCP,tpDP);
  246. assertNotEquals(tpBC,tpCC);
  247. assertNotEquals(tpBC,tpDC);
  248. assertNotEquals(tpCC,tpDC);
  249. // Change text via normal
  250. HSLFTextParagraph.setText(trB, "Test Foo Test");
  251. // Ensure now have first style
  252. assertEquals(1, trB.get(0).getTextRuns().size());
  253. rtrB = trB.get(0).getTextRuns().get(0);
  254. assertEquals("Test Foo Test", HSLFTextParagraph.getRawText(trB));
  255. assertEquals("Test Foo Test", rtrB.getRawText());
  256. assertNotNull(rtrB.getCharacterStyle());
  257. assertNotNull(rtrB.getTextParagraph().getParagraphStyle());
  258. assertEquals( tpBP, rtrB.getTextParagraph().getParagraphStyle() );
  259. assertEquals( tpBC, rtrB.getCharacterStyle() );
  260. }
  261. /**
  262. * Test to ensure the right stuff happens if we change the text
  263. * in a rich text run, that doesn't happen to actually be rich
  264. */
  265. @Test
  266. public void testChangeTextInRichTextRunNonRich() {
  267. HSLFSlide slideOne = ss.getSlides().get(0);
  268. List<List<HSLFTextParagraph>> textRuns = slideOne.getTextParagraphs();
  269. List<HSLFTextParagraph> trB = textRuns.get(1);
  270. assertEquals(1, trB.get(0).getTextRuns().size());
  271. HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
  272. assertEquals(HSLFTextParagraph.getRawText(trB.subList(0, 1)), rtrB.getRawText());
  273. assertNotNull(rtrB.getCharacterStyle());
  274. assertNotNull(rtrB.getTextParagraph().getParagraphStyle());
  275. // Change text via rich
  276. rtrB.setText("Test Test Test");
  277. assertEquals("Test Test Test", HSLFTextParagraph.getRawText(trB.subList(0, 1)));
  278. assertEquals("Test Test Test", rtrB.getRawText());
  279. // Will now have dummy props
  280. assertNotNull(rtrB.getCharacterStyle());
  281. assertNotNull(rtrB.getTextParagraph().getParagraphStyle());
  282. }
  283. /**
  284. * Tests to ensure changing the text within rich text runs works
  285. * correctly
  286. */
  287. @Test
  288. public void testChangeTextInRichTextRun() {
  289. HSLFSlide slideOne = ssRich.getSlides().get(0);
  290. List<List<HSLFTextParagraph>> textParass = slideOne.getTextParagraphs();
  291. List<HSLFTextParagraph> trB = textParass.get(1);
  292. assertEquals(3, trB.size());
  293. // We start with 3 text runs, each with their own set of styles,
  294. // but all sharing the same paragraph styles
  295. HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
  296. HSLFTextRun rtrC = trB.get(1).getTextRuns().get(0);
  297. HSLFTextRun rtrD = trB.get(2).getTextRuns().get(0);
  298. TextPropCollection tpBP = rtrB.getTextParagraph().getParagraphStyle();
  299. TextPropCollection tpBC = rtrB.getCharacterStyle();
  300. TextPropCollection tpCP = rtrC.getTextParagraph().getParagraphStyle();
  301. TextPropCollection tpCC = rtrC.getCharacterStyle();
  302. TextPropCollection tpDP = rtrD.getTextParagraph().getParagraphStyle();
  303. TextPropCollection tpDC = rtrD.getCharacterStyle();
  304. // Check text and stylings
  305. assertEquals(HSLFTextParagraph.getRawText(trB).substring(0, 30), rtrB.getRawText());
  306. assertNotNull(tpBP);
  307. assertNotNull(tpBC);
  308. assertNotNull(tpCP);
  309. assertNotNull(tpCC);
  310. assertNotNull(tpDP);
  311. assertNotNull(tpDC);
  312. assertEquals(tpBP, tpCP);
  313. assertEquals(tpBP, tpDP);
  314. assertEquals(tpCP, tpDP);
  315. assertNotEquals(tpBC, tpCC);
  316. assertNotEquals(tpBC, tpDC);
  317. assertNotEquals(tpCC, tpDC);
  318. // Check text in the rich runs
  319. assertEquals("This is the subtitle, in bold\r", rtrB.getRawText());
  320. assertEquals("This bit is blue and italic\r", rtrC.getRawText());
  321. assertEquals("This bit is red (normal)", rtrD.getRawText());
  322. String newBText = "New Subtitle, will still be bold\n";
  323. String newCText = "New blue and italic text\n";
  324. String newDText = "Funky new normal red text";
  325. rtrB.setText(newBText);
  326. rtrC.setText(newCText);
  327. rtrD.setText(newDText);
  328. HSLFTextParagraph.storeText(trB);
  329. assertEquals(newBText.replace('\n','\r'), rtrB.getRawText());
  330. assertEquals(newCText.replace('\n','\r'), rtrC.getRawText());
  331. assertEquals(newDText.replace('\n','\r'), rtrD.getRawText());
  332. assertEquals(newBText.replace('\n','\r') + newCText.replace('\n','\r') + newDText.replace('\n','\r'), HSLFTextParagraph.getRawText(trB));
  333. // The styles should have been updated for the new sizes
  334. assertEquals(newBText.length(), tpBC.getCharactersCovered());
  335. assertEquals(newCText.length(), tpCC.getCharactersCovered());
  336. assertEquals(newDText.length()+1, tpDC.getCharactersCovered()); // Last one is always one larger
  337. // Paragraph style should be sum of text length
  338. assertEquals(
  339. newBText.length() + newCText.length() + newDText.length() +1,
  340. tpBP.getCharactersCovered() + tpCP.getCharactersCovered() + tpDP.getCharactersCovered()
  341. );
  342. // Check stylings still as expected
  343. TextPropCollection ntpBC = rtrB.getCharacterStyle();
  344. TextPropCollection ntpCC = rtrC.getCharacterStyle();
  345. TextPropCollection ntpDC = rtrD.getCharacterStyle();
  346. assertEquals(tpBC.getTextPropList(), ntpBC.getTextPropList());
  347. assertEquals(tpCC.getTextPropList(), ntpCC.getTextPropList());
  348. assertEquals(tpDC.getTextPropList(), ntpDC.getTextPropList());
  349. }
  350. /**
  351. * Test case for Bug 41015.
  352. *
  353. * In some cases RichTextRun.getText() threw StringIndexOutOfBoundsException because
  354. * of the wrong list of potential paragraph properties defined in StyleTextPropAtom.
  355. *
  356. */
  357. @Test
  358. public void testBug41015() throws IOException {
  359. List<HSLFTextRun> rt;
  360. HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("bug-41015.ppt"));
  361. HSLFSlide sl = ppt.getSlides().get(0);
  362. List<List<HSLFTextParagraph>> textParass = sl.getTextParagraphs();
  363. assertEquals(2, textParass.size());
  364. List<HSLFTextParagraph> textParas = textParass.get(0);
  365. rt = textParass.get(0).get(0).getTextRuns();
  366. assertEquals(1, rt.size());
  367. assertEquals(0, textParass.get(0).get(0).getIndentLevel());
  368. assertEquals("sdfsdfsdf", rt.get(0).getRawText());
  369. textParas = textParass.get(1);
  370. String texts[] = {"Sdfsdfsdf\r","Dfgdfg\r","Dfgdfgdfg\r","Sdfsdfs\r","Sdfsdf\r"};
  371. int indents[] = {0,0,0,1,1};
  372. int i=0;
  373. for (HSLFTextParagraph p : textParas) {
  374. assertEquals(texts[i], p.getTextRuns().get(0).getRawText());
  375. assertEquals(indents[i], p.getIndentLevel());
  376. i++;
  377. }
  378. }
  379. /**
  380. * Test creation of TextRun objects.
  381. */
  382. @Test
  383. public void testAddTextRun() {
  384. HSLFSlideShow ppt = new HSLFSlideShow();
  385. HSLFSlide slide = ppt.createSlide();
  386. assertEquals(0, slide.getTextParagraphs().size());
  387. HSLFTextBox shape1 = new HSLFTextBox();
  388. List<HSLFTextParagraph> run1 = shape1.getTextParagraphs();
  389. shape1.setText("Text 1");
  390. slide.addShape(shape1);
  391. //The array of Slide's text runs must be updated when new text shapes are added.
  392. List<List<HSLFTextParagraph>> runs = slide.getTextParagraphs();
  393. assertNotNull(runs);
  394. assertSame(run1, runs.get(0));
  395. HSLFTextBox shape2 = new HSLFTextBox();
  396. List<HSLFTextParagraph> run2 = shape2.getTextParagraphs();
  397. shape2.setText("Text 2");
  398. slide.addShape(shape2);
  399. runs = slide.getTextParagraphs();
  400. assertEquals(2, runs.size());
  401. assertSame(run1, runs.get(0));
  402. assertSame(run2, runs.get(1));
  403. // as getShapes()
  404. List<HSLFShape> sh = slide.getShapes();
  405. assertEquals(2, sh.size());
  406. assertTrue(sh.get(0) instanceof HSLFTextBox);
  407. HSLFTextBox box1 = (HSLFTextBox)sh.get(0);
  408. assertSame(run1, box1.getTextParagraphs());
  409. HSLFTextBox box2 = (HSLFTextBox)sh.get(1);
  410. assertSame(run2, box2.getTextParagraphs());
  411. // test Table - a complex group of shapes containing text objects
  412. HSLFSlide slide2 = ppt.createSlide();
  413. assertTrue(slide2.getTextParagraphs().isEmpty());
  414. HSLFTable table = new HSLFTable(2, 2);
  415. slide2.addShape(table);
  416. runs = slide2.getTextParagraphs();
  417. assertNotNull(runs);
  418. assertEquals(4, runs.size());
  419. }
  420. @Test
  421. public void test48916() throws IOException {
  422. HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("SampleShow.ppt"));
  423. List<HSLFSlide> slides = ppt.getSlides();
  424. for(HSLFSlide slide : slides){
  425. for(HSLFShape sh : slide.getShapes()){
  426. if (!(sh instanceof HSLFTextShape)) continue;
  427. HSLFTextShape tx = (HSLFTextShape)sh;
  428. List<HSLFTextParagraph> paras = tx.getTextParagraphs();
  429. //verify that records cached in TextRun and EscherTextboxWrapper are the same
  430. Record[] runChildren = paras.get(0).getRecords();
  431. Record[] txboxChildren = tx.getEscherTextboxWrapper().getChildRecords();
  432. assertEquals(runChildren.length, txboxChildren.length);
  433. for(int i=0; i < txboxChildren.length; i++){
  434. assertSame(txboxChildren[i], runChildren[i]);
  435. }
  436. // caused NPE prior to fix of Bugzilla #48916
  437. for (HSLFTextParagraph p : paras) {
  438. for (HSLFTextRun rt : p.getTextRuns()) {
  439. rt.setBold(true);
  440. rt.setFontColor(Color.RED);
  441. }
  442. }
  443. tx.storeText();
  444. }
  445. }
  446. ByteArrayOutputStream out = new ByteArrayOutputStream();
  447. ppt.write(out);
  448. ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
  449. for(HSLFSlide slide : ppt.getSlides()){
  450. for(HSLFShape sh : slide.getShapes()){
  451. if(sh instanceof HSLFTextShape){
  452. HSLFTextShape tx = (HSLFTextShape)sh;
  453. List<HSLFTextParagraph> run = tx.getTextParagraphs();
  454. HSLFTextRun rt = run.get(0).getTextRuns().get(0);
  455. assertTrue(rt.isBold());
  456. assertEquals(rt.getFontColor(), Color.RED);
  457. }
  458. }
  459. }
  460. }
  461. @Test
  462. public void test52244() throws IOException {
  463. HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("52244.ppt"));
  464. HSLFSlide slide = ppt.getSlides().get(0);
  465. int sizes[] = { 36, 24, 12, 32, 12, 12 };
  466. int i=0;
  467. for (List<HSLFTextParagraph> textParas : slide.getTextParagraphs()) {
  468. assertEquals("Arial", textParas.get(0).getTextRuns().get(0).getFontFamily());
  469. assertEquals(sizes[i++], (int)textParas.get(0).getTextRuns().get(0).getFontSize());
  470. }
  471. }
  472. }