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.

TestTextRun.java 18KB

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