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.

TestXWPFRun.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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.xwpf.usermodel;
  16. import static org.apache.poi.xwpf.XWPFTestDataSamples.openSampleDocument;
  17. import static org.apache.poi.xwpf.XWPFTestDataSamples.writeOutAndReadBack;
  18. import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
  19. import static org.junit.jupiter.api.Assertions.assertEquals;
  20. import static org.junit.jupiter.api.Assertions.assertFalse;
  21. import static org.junit.jupiter.api.Assertions.assertNotNull;
  22. import static org.junit.jupiter.api.Assertions.assertNull;
  23. import static org.junit.jupiter.api.Assertions.assertSame;
  24. import static org.junit.jupiter.api.Assertions.assertTrue;
  25. import java.io.ByteArrayInputStream;
  26. import java.io.IOException;
  27. import java.math.BigInteger;
  28. import java.util.List;
  29. import org.apache.poi.common.usermodel.PictureType;
  30. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  31. import org.apache.poi.util.LocaleUtil;
  32. import org.apache.poi.util.Units;
  33. import org.apache.poi.wp.usermodel.HeaderFooterType;
  34. import org.apache.poi.xwpf.XWPFTestDataSamples;
  35. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  36. import org.junit.jupiter.api.AfterEach;
  37. import org.junit.jupiter.api.BeforeEach;
  38. import org.junit.jupiter.api.Test;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  40. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  41. import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
  42. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1;
  43. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
  44. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
  45. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
  46. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  47. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
  48. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
  49. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
  50. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear;
  51. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STEm;
  52. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
  53. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STThemeColor;
  54. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
  55. /**
  56. * Tests for XWPF Run
  57. */
  58. class TestXWPFRun {
  59. private CTR ctRun;
  60. private XWPFParagraph p;
  61. private IRunBody irb;
  62. private XWPFDocument doc;
  63. @BeforeEach
  64. void setUp() {
  65. doc = new XWPFDocument();
  66. p = doc.createParagraph();
  67. irb = p;
  68. this.ctRun = CTR.Factory.newInstance();
  69. }
  70. @AfterEach
  71. void tearDown() throws Exception {
  72. doc.close();
  73. }
  74. @Test
  75. void testSetGetText() {
  76. ctRun.addNewT().setStringValue("TEST STRING");
  77. ctRun.addNewT().setStringValue("TEST2 STRING");
  78. ctRun.addNewT().setStringValue("TEST3 STRING");
  79. assertEquals(3, ctRun.sizeOfTArray());
  80. XWPFRun run = new XWPFRun(ctRun, irb);
  81. assertEquals("TEST2 STRING", run.getText(1));
  82. run.setText("NEW STRING", 0);
  83. assertEquals("NEW STRING", run.getText(0));
  84. //run.setText("xxx",14);
  85. //fail("Position wrong");
  86. }
  87. /*
  88. * bug 59208
  89. * Purpose: test all valid boolean-like values
  90. * exercise isCTOnOff(CTOnOff) through all valid permutations
  91. */
  92. @Test
  93. void testCTOnOff() {
  94. CTRPr rpr = ctRun.addNewRPr();
  95. CTOnOff bold = rpr.addNewB();
  96. XWPFRun run = new XWPFRun(ctRun, irb);
  97. // True values: "true", "1", "on"
  98. bold.setVal(STOnOff1.ON);
  99. assertTrue(run.isBold());
  100. bold.setVal(STOnOff1.ON);
  101. assertTrue(run.isBold());
  102. bold.setVal(STOnOff1.ON);
  103. assertTrue(run.isBold());
  104. // False values: "false", "0", "off"
  105. bold.setVal(STOnOff1.OFF);
  106. assertFalse(run.isBold());
  107. bold.setVal(STOnOff1.OFF);
  108. assertFalse(run.isBold());
  109. bold.setVal(STOnOff1.OFF);
  110. assertFalse(run.isBold());
  111. }
  112. @Test
  113. void testSetGetBold() {
  114. CTRPr rpr = ctRun.addNewRPr();
  115. rpr.addNewB().setVal(STOnOff1.ON);
  116. XWPFRun run = new XWPFRun(ctRun, irb);
  117. assertTrue(run.isBold());
  118. run.setBold(false);
  119. // Implementation detail: POI natively prefers <w:b w:val="false"/>,
  120. // but should correctly read val="0" and val="off"
  121. assertEquals("off", rpr.getBArray(0).getVal());
  122. }
  123. @Test
  124. void testSetGetComplexBold() {
  125. CTRPr rpr = ctRun.addNewRPr();
  126. rpr.addNewBCs().setVal(STOnOff1.ON);
  127. XWPFRun run = new XWPFRun(ctRun, irb);
  128. assertTrue(run.isComplexScriptBold());
  129. run.setComplexScriptBold(false);
  130. // Implementation detail: POI natively prefers <w:b w:val="false"/>,
  131. // but should correctly read val="0" and val="off"
  132. assertEquals("off", rpr.getBCsArray(0).getVal());
  133. }
  134. @Test
  135. void testSetGetItalic() {
  136. CTRPr rpr = ctRun.addNewRPr();
  137. rpr.addNewI().setVal(STOnOff1.ON);
  138. XWPFRun run = new XWPFRun(ctRun, irb);
  139. assertTrue(run.isItalic());
  140. run.setItalic(false);
  141. assertEquals("off", rpr.getIArray(0).getVal());
  142. }
  143. @Test
  144. void testSetGetItalicComplex() {
  145. CTRPr rpr = ctRun.addNewRPr();
  146. rpr.addNewICs().setVal(STOnOff1.ON);
  147. XWPFRun run = new XWPFRun(ctRun, irb);
  148. assertTrue(run.isComplexScriptItalic());
  149. run.setComplexScriptItalic(false);
  150. assertEquals("off", rpr.getICsArray(0).getVal());
  151. }
  152. @Test
  153. void testSetGetStrike() {
  154. CTRPr rpr = ctRun.addNewRPr();
  155. rpr.addNewStrike().setVal(STOnOff1.ON);
  156. XWPFRun run = new XWPFRun(ctRun, irb);
  157. assertTrue(run.isStrikeThrough());
  158. run.setStrikeThrough(false);
  159. assertEquals("off", rpr.getStrikeArray(0).getVal());
  160. }
  161. @Test
  162. void testSetGetUnderline() {
  163. CTRPr rpr = ctRun.addNewRPr();
  164. XWPFRun run = new XWPFRun(ctRun, irb);
  165. rpr.addNewU().setVal(STUnderline.DASH);
  166. assertEquals(UnderlinePatterns.DASH.getValue(), run.getUnderline()
  167. .getValue());
  168. run.setUnderline(UnderlinePatterns.NONE);
  169. assertEquals(STUnderline.NONE.intValue(), rpr.getUArray(0).getVal()
  170. .intValue());
  171. }
  172. @Test
  173. void testSetGetFontFamily() {
  174. CTRPr rpr = ctRun.addNewRPr();
  175. rpr.addNewRFonts().setAscii("Times New Roman");
  176. XWPFRun run = new XWPFRun(ctRun, irb);
  177. assertEquals("Times New Roman", run.getFontFamily());
  178. run.setFontFamily("Verdana");
  179. assertEquals("Verdana", rpr.getRFontsArray(0).getAscii());
  180. }
  181. @Test
  182. void testSetGetFontSize() {
  183. CTRPr rpr = ctRun.addNewRPr();
  184. rpr.addNewSz().setVal(BigInteger.valueOf(14));
  185. XWPFRun run = new XWPFRun(ctRun, irb);
  186. assertEquals(7, run.getFontSize());
  187. assertEquals(7.0, run.getFontSizeAsDouble(), 0.01);
  188. run.setFontSize(24);
  189. assertEquals("48", rpr.getSzArray(0).getVal().toString());
  190. run.setFontSize(24.5f);
  191. assertEquals("49", rpr.getSzArray(0).getVal().toString());
  192. assertEquals(25, run.getFontSize());
  193. assertEquals(24.5, run.getFontSizeAsDouble(), 0.01);
  194. }
  195. @Test
  196. void testSetGetFontSizeComplex() {
  197. CTRPr rpr = ctRun.addNewRPr();
  198. rpr.addNewSzCs().setVal(BigInteger.valueOf(14));
  199. XWPFRun run = new XWPFRun(ctRun, irb);
  200. assertEquals(7.0, run.getComplexScriptFontSizeAsDouble(), 0.01);
  201. run.setComplexScriptFontSize(24);
  202. assertEquals("48", rpr.getSzCsArray(0).getVal().toString());
  203. run.setComplexScriptFontSize(24.5f);
  204. assertEquals("49", rpr.getSzCsArray(0).getVal().toString());
  205. assertEquals(24.5, run.getComplexScriptFontSizeAsDouble(), 0.01);
  206. }
  207. @Test
  208. void testSetGetTextForegroundBackground() {
  209. CTRPr rpr = ctRun.addNewRPr();
  210. rpr.addNewPosition().setVal(new BigInteger("4000"));
  211. XWPFRun run = new XWPFRun(ctRun, irb);
  212. assertEquals(4000, run.getTextPosition());
  213. run.setTextPosition(2400);
  214. assertEquals("2400", rpr.getPositionArray(0).getVal().toString());
  215. }
  216. @Test
  217. void testSetGetColor() {
  218. XWPFRun run = new XWPFRun(ctRun, irb);
  219. run.setColor("0F0F0F");
  220. String clr = run.getColor();
  221. assertEquals("0F0F0F", clr);
  222. }
  223. @Test
  224. void testAddCarriageReturn() {
  225. ctRun.addNewT().setStringValue("TEST STRING");
  226. ctRun.addNewCr();
  227. ctRun.addNewT().setStringValue("TEST2 STRING");
  228. ctRun.addNewCr();
  229. ctRun.addNewT().setStringValue("TEST3 STRING");
  230. assertEquals(2, ctRun.sizeOfCrArray());
  231. XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
  232. run.setText("T1");
  233. run.addCarriageReturn();
  234. run.addCarriageReturn();
  235. run.setText("T2");
  236. run.addCarriageReturn();
  237. assertEquals(3, run.getCTR().sizeOfCrArray());
  238. assertEquals("T1\n\nT2\n", run.toString());
  239. }
  240. @Test
  241. void testAddTabsAndLineBreaks() {
  242. ctRun.addNewT().setStringValue("TEST STRING");
  243. ctRun.addNewCr();
  244. ctRun.addNewT().setStringValue("TEST2 STRING");
  245. ctRun.addNewTab();
  246. ctRun.addNewT().setStringValue("TEST3 STRING");
  247. assertEquals(1, ctRun.sizeOfCrArray());
  248. assertEquals(1, ctRun.sizeOfTabArray());
  249. XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
  250. run.setText("T1");
  251. run.addCarriageReturn();
  252. run.setText("T2");
  253. run.addTab();
  254. run.setText("T3");
  255. assertEquals(1, run.getCTR().sizeOfCrArray());
  256. assertEquals(1, run.getCTR().sizeOfTabArray());
  257. assertEquals("T1\nT2\tT3", run.toString());
  258. }
  259. @Test
  260. void testAddPageBreak() {
  261. ctRun.addNewT().setStringValue("TEST STRING");
  262. ctRun.addNewBr();
  263. ctRun.addNewT().setStringValue("TEST2 STRING");
  264. CTBr breac = ctRun.addNewBr();
  265. breac.setClear(STBrClear.LEFT);
  266. ctRun.addNewT().setStringValue("TEST3 STRING");
  267. assertEquals(2, ctRun.sizeOfBrArray());
  268. XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
  269. run.setText("TEXT1");
  270. run.addBreak();
  271. run.setText("TEXT2");
  272. run.addBreak(BreakType.TEXT_WRAPPING);
  273. assertEquals(2, run.getCTR().sizeOfBrArray());
  274. }
  275. /**
  276. * Test that on an existing document, we do the
  277. * right thing with it
  278. */
  279. @Test
  280. void testExisting() throws IOException {
  281. XWPFDocument doc = openSampleDocument("TestDocument.docx");
  282. XWPFParagraph p;
  283. XWPFRun run;
  284. // First paragraph is simple
  285. p = doc.getParagraphArray(0);
  286. assertEquals("This is a test document.", p.getText());
  287. assertEquals(2, p.getRuns().size());
  288. run = p.getRuns().get(0);
  289. assertEquals("This is a test document", run.toString());
  290. assertFalse(run.isBold());
  291. assertFalse(run.isItalic());
  292. assertFalse(run.isStrikeThrough());
  293. assertNull(run.getCTR().getRPr());
  294. run = p.getRuns().get(1);
  295. assertEquals(".", run.toString());
  296. assertFalse(run.isBold());
  297. assertFalse(run.isItalic());
  298. assertFalse(run.isStrikeThrough());
  299. assertNull(run.getCTR().getRPr());
  300. // Next paragraph is all in one style, but a different one
  301. p = doc.getParagraphArray(1);
  302. assertEquals("This bit is in bold and italic", p.getText());
  303. assertEquals(1, p.getRuns().size());
  304. run = p.getRuns().get(0);
  305. assertEquals("This bit is in bold and italic", run.toString());
  306. assertTrue(run.isBold());
  307. assertTrue(run.isItalic());
  308. assertFalse(run.isStrikeThrough());
  309. assertTrue(run.getCTR().getRPr().sizeOfBArray() > 0);
  310. assertFalse(run.getCTR().getRPr().getBArray(0).isSetVal());
  311. // Back to normal
  312. p = doc.getParagraphArray(2);
  313. assertEquals("Back to normal", p.getText());
  314. assertEquals(1, p.getRuns().size());
  315. run = p.getRuns().get(0);
  316. assertEquals("Back to normal", run.toString());
  317. assertFalse(run.isBold());
  318. assertFalse(run.isItalic());
  319. assertFalse(run.isStrikeThrough());
  320. assertNull(run.getCTR().getRPr());
  321. // Different styles in one paragraph
  322. p = doc.getParagraphArray(3);
  323. assertEquals("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.", p.getText());
  324. assertEquals(11, p.getRuns().size());
  325. run = p.getRuns().get(0);
  326. assertEquals("This contains ", run.toString());
  327. assertFalse(run.isBold());
  328. assertFalse(run.isItalic());
  329. assertFalse(run.isStrikeThrough());
  330. assertNull(run.getCTR().getRPr());
  331. run = p.getRuns().get(1);
  332. assertEquals("BOLD", run.toString());
  333. assertTrue(run.isBold());
  334. assertFalse(run.isItalic());
  335. assertFalse(run.isStrikeThrough());
  336. run = p.getRuns().get(2);
  337. assertEquals(", ", run.toString());
  338. assertFalse(run.isBold());
  339. assertFalse(run.isItalic());
  340. assertFalse(run.isStrikeThrough());
  341. assertNull(run.getCTR().getRPr());
  342. run = p.getRuns().get(3);
  343. assertEquals("ITALIC", run.toString());
  344. assertFalse(run.isBold());
  345. assertTrue(run.isItalic());
  346. assertFalse(run.isStrikeThrough());
  347. run = p.getRuns().get(4);
  348. assertEquals(" and ", run.toString());
  349. assertFalse(run.isBold());
  350. assertFalse(run.isItalic());
  351. assertFalse(run.isStrikeThrough());
  352. assertNull(run.getCTR().getRPr());
  353. run = p.getRuns().get(5);
  354. assertEquals("BOTH", run.toString());
  355. assertTrue(run.isBold());
  356. assertTrue(run.isItalic());
  357. assertFalse(run.isStrikeThrough());
  358. run = p.getRuns().get(6);
  359. assertEquals(", as well as ", run.toString());
  360. assertFalse(run.isBold());
  361. assertFalse(run.isItalic());
  362. assertFalse(run.isStrikeThrough());
  363. assertNull(run.getCTR().getRPr());
  364. run = p.getRuns().get(7);
  365. assertEquals("RED", run.toString());
  366. assertFalse(run.isBold());
  367. assertFalse(run.isItalic());
  368. assertFalse(run.isStrikeThrough());
  369. run = p.getRuns().get(8);
  370. assertEquals(" and ", run.toString());
  371. assertFalse(run.isBold());
  372. assertFalse(run.isItalic());
  373. assertFalse(run.isStrikeThrough());
  374. assertNull(run.getCTR().getRPr());
  375. run = p.getRuns().get(9);
  376. assertEquals("YELLOW", run.toString());
  377. assertFalse(run.isBold());
  378. assertFalse(run.isItalic());
  379. assertFalse(run.isStrikeThrough());
  380. run = p.getRuns().get(10);
  381. assertEquals(" text.", run.toString());
  382. assertFalse(run.isBold());
  383. assertFalse(run.isItalic());
  384. assertFalse(run.isStrikeThrough());
  385. assertNull(run.getCTR().getRPr());
  386. doc.close();
  387. }
  388. @Test
  389. void testPictureInHeader() throws IOException {
  390. XWPFDocument sampleDoc = openSampleDocument("headerPic.docx");
  391. XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  392. XWPFHeader header = policy.getDefaultHeader();
  393. int count = 0;
  394. for (XWPFParagraph p : header.getParagraphs()) {
  395. for (XWPFRun r : p.getRuns()) {
  396. List<XWPFPicture> pictures = r.getEmbeddedPictures();
  397. for (XWPFPicture pic : pictures) {
  398. assertNotNull(pic.getPictureData());
  399. assertEquals("DOZOR", pic.getDescription());
  400. assertEquals(5, pic.getPictureData().getPictureType());
  401. assertEquals(PictureType.JPEG, pic.getPictureData().getPictureTypeEnum());
  402. }
  403. count += pictures.size();
  404. }
  405. }
  406. assertEquals(1, count);
  407. sampleDoc.close();
  408. }
  409. @Test
  410. void testSetGetLang() {
  411. XWPFRun run = p.createRun();
  412. assertNull(run.getLang());
  413. run.setLang("en-CA");
  414. assertEquals("en-CA", run.getLang());
  415. run.setLang("fr-CA");
  416. assertEquals("fr-CA", run.getLang());
  417. run.setLang(null);
  418. assertNull(run.getLang());
  419. }
  420. @Test
  421. void testSetGetLang2() {
  422. XWPFRun run = p.createRun();
  423. assertNull(run.getLang());
  424. run.getCTR().addNewRPr().addNewLang().setVal("en-CA");
  425. assertEquals("en-CA", run.getLang());
  426. run.getCTR().getRPr().getLangArray(0).setVal("fr-CA");
  427. assertEquals("fr-CA", run.getLang());
  428. run.getCTR().getRPr().getLangArray(0).setVal(null);
  429. assertNull(run.getLang());
  430. }
  431. @Test
  432. void testAddPicture() throws Exception {
  433. XWPFDocument doc = openSampleDocument("TestDocument.docx");
  434. XWPFParagraph p = doc.getParagraphArray(2);
  435. XWPFRun r = p.getRuns().get(0);
  436. assertEquals(0, doc.getAllPictures().size());
  437. assertEquals(0, r.getEmbeddedPictures().size());
  438. r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
  439. assertEquals(1, doc.getAllPictures().size());
  440. assertEquals(1, r.getEmbeddedPictures().size());
  441. XWPFDocument docBack = writeOutAndReadBack(doc);
  442. XWPFParagraph pBack = docBack.getParagraphArray(2);
  443. XWPFRun rBack = pBack.getRuns().get(0);
  444. assertEquals(1, docBack.getAllPictures().size());
  445. assertEquals(1, rBack.getEmbeddedPictures().size());
  446. docBack.close();
  447. doc.close();
  448. }
  449. /**
  450. * Bugzilla #58237 - Unable to add image to word document header
  451. */
  452. @Test
  453. void testAddPictureInHeader() throws IOException, InvalidFormatException {
  454. try (XWPFDocument doc = openSampleDocument("TestDocument.docx")) {
  455. XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
  456. XWPFParagraph p = hdr.createParagraph();
  457. XWPFRun r = p.createRun();
  458. assertEquals(0, hdr.getAllPictures().size());
  459. assertEquals(0, r.getEmbeddedPictures().size());
  460. r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
  461. assertEquals(1, hdr.getAllPictures().size());
  462. assertEquals(1, r.getEmbeddedPictures().size());
  463. XWPFPicture pic = r.getEmbeddedPictures().get(0);
  464. CTPicture ctPic = pic.getCTPicture();
  465. CTBlipFillProperties ctBlipFill = ctPic.getBlipFill();
  466. assertNotNull(ctBlipFill);
  467. CTBlip ctBlip = ctBlipFill.getBlip();
  468. assertNotNull(ctBlip);
  469. assertEquals("rId1", ctBlip.getEmbed());
  470. try (XWPFDocument docBack = writeOutAndReadBack(doc)) {
  471. XWPFHeader hdrBack = docBack.getHeaderArray(0);
  472. XWPFParagraph pBack = hdrBack.getParagraphArray(0);
  473. XWPFRun rBack = pBack.getRuns().get(0);
  474. assertEquals(1, hdrBack.getAllPictures().size());
  475. assertEquals(1, rBack.getEmbeddedPictures().size());
  476. }
  477. }
  478. }
  479. /**
  480. * Bugzilla #52288 - setting the font family on the
  481. * run mustn't NPE
  482. */
  483. @Test
  484. void testSetFontFamily_52288() throws IOException {
  485. try (XWPFDocument doc = openSampleDocument("52288.docx")) {
  486. doc.getParagraphs().stream()
  487. .flatMap(p -> p.getRuns().stream())
  488. .filter(p -> p != null && p.getText(0) != null)
  489. .forEach(r -> assertDoesNotThrow(() -> r.setFontFamily("Times New Roman")));
  490. }
  491. }
  492. @Test
  493. void testBug55476() throws IOException, InvalidFormatException {
  494. byte[] image = XWPFTestDataSamples.getImage("abstract1.jpg");
  495. try (XWPFDocument document = new XWPFDocument()) {
  496. document.createParagraph().createRun().addPicture(
  497. new ByteArrayInputStream(image), Document.PICTURE_TYPE_JPEG, "test.jpg", Units.toEMU(300), Units.toEMU(100));
  498. try (XWPFDocument docBack = writeOutAndReadBack(document)) {
  499. List<XWPFPicture> pictures = docBack.getParagraphArray(0).getRuns().get(0).getEmbeddedPictures();
  500. assertEquals(1, pictures.size());
  501. }
  502. }
  503. }
  504. @Test
  505. void testBug58922() throws IOException {
  506. try (XWPFDocument document = new XWPFDocument()) {
  507. final XWPFRun run = document.createParagraph().createRun();
  508. assertEquals(-1, run.getFontSize());
  509. run.setFontSize(10);
  510. assertEquals(10, run.getFontSize());
  511. run.setFontSize(Short.MAX_VALUE - 1);
  512. assertEquals(Short.MAX_VALUE - 1, run.getFontSize());
  513. run.setFontSize(Short.MAX_VALUE);
  514. assertEquals(Short.MAX_VALUE, run.getFontSize());
  515. run.setFontSize(Short.MAX_VALUE + 1);
  516. assertEquals(Short.MAX_VALUE + 1, run.getFontSize());
  517. run.setFontSize(Integer.MAX_VALUE - 1);
  518. assertEquals(Integer.MAX_VALUE - 1, run.getFontSize());
  519. run.setFontSize(Integer.MAX_VALUE);
  520. assertEquals(Integer.MAX_VALUE, run.getFontSize());
  521. run.setFontSize(-1);
  522. assertEquals(-1, run.getFontSize());
  523. assertEquals(-1, run.getTextPosition());
  524. run.setTextPosition(10);
  525. assertEquals(10, run.getTextPosition());
  526. run.setTextPosition(Short.MAX_VALUE - 1);
  527. assertEquals(Short.MAX_VALUE - 1, run.getTextPosition());
  528. run.setTextPosition(Short.MAX_VALUE);
  529. assertEquals(Short.MAX_VALUE, run.getTextPosition());
  530. run.setTextPosition(Short.MAX_VALUE + 1);
  531. assertEquals(Short.MAX_VALUE + 1, run.getTextPosition());
  532. run.setTextPosition(Short.MAX_VALUE + 1);
  533. assertEquals(Short.MAX_VALUE + 1, run.getTextPosition());
  534. run.setTextPosition(Integer.MAX_VALUE - 1);
  535. assertEquals(Integer.MAX_VALUE - 1, run.getTextPosition());
  536. run.setTextPosition(Integer.MAX_VALUE);
  537. assertEquals(Integer.MAX_VALUE, run.getTextPosition());
  538. run.setTextPosition(-1);
  539. assertEquals(-1, run.getTextPosition());
  540. }
  541. }
  542. @Test
  543. void testSetters() throws IOException {
  544. try (XWPFDocument document = new XWPFDocument()) {
  545. final XWPFRun run = document.createParagraph().createRun();
  546. // at least trigger some of the setters to ensure classes are included in
  547. // the poi-ooxml-lite
  548. run.setBold(true);
  549. run.setCapitalized(true);
  550. run.setCharacterSpacing(2);
  551. assertEquals(2, run.getCharacterSpacing());
  552. run.setColor("000000");
  553. run.setDoubleStrikethrough(true);
  554. run.setEmbossed(true);
  555. run.setFontFamily("Calibri");
  556. assertEquals("Calibri", run.getFontFamily());
  557. run.setFontSize(10);
  558. assertEquals(10, run.getFontSize());
  559. run.setImprinted(true);
  560. run.setItalic(true);
  561. }
  562. }
  563. @Test
  564. void testSetGetTextScale() throws IOException {
  565. try (XWPFDocument document = new XWPFDocument()) {
  566. final XWPFRun run = document.createParagraph().createRun();
  567. assertEquals(100, run.getTextScale());
  568. run.setTextScale(200);
  569. assertEquals(200, run.getTextScale());
  570. }
  571. }
  572. @Test
  573. void testSetGetTextHighlightColor() throws IOException {
  574. try (XWPFDocument document = new XWPFDocument()) {
  575. final XWPFRun run = document.createParagraph().createRun();
  576. assertSame(STHighlightColor.NONE, run.getTextHighlightColor());
  577. assertFalse(run.isHighlighted());
  578. run.setTextHighlightColor("darkGreen"); // See 17.18.40 ST_HighlightColor (Text Highlight Colors)
  579. assertSame(STHighlightColor.DARK_GREEN, run.getTextHighlightColor());
  580. assertTrue(run.isHighlighted());
  581. run.setTextHighlightColor("none");
  582. assertFalse(run.isHighlighted());
  583. }
  584. }
  585. @Test
  586. void testSetGetVanish() throws IOException {
  587. try (XWPFDocument document = new XWPFDocument()) {
  588. final XWPFRun run = document.createParagraph().createRun();
  589. assertFalse(run.isVanish());
  590. run.setVanish(true);
  591. assertTrue(run.isVanish());
  592. run.setVanish(false);
  593. assertFalse(run.isVanish());
  594. }
  595. }
  596. @Test
  597. void testSetGetVerticalAlignment() throws IOException {
  598. try (XWPFDocument document = new XWPFDocument()) {
  599. XWPFRun run = document.createParagraph().createRun();
  600. assertSame(STVerticalAlignRun.BASELINE, run.getVerticalAlignment());
  601. // Reset to a fresh run so we test case of run not having vertical alignment at all
  602. run = document.createParagraph().createRun();
  603. run.setVerticalAlignment("subscript");
  604. assertSame(STVerticalAlignRun.SUBSCRIPT, run.getVerticalAlignment());
  605. run.setVerticalAlignment("superscript");
  606. assertSame(STVerticalAlignRun.SUPERSCRIPT, run.getVerticalAlignment());
  607. }
  608. }
  609. @Test
  610. void testSetGetVAlign() {
  611. CTRPr rpr = ctRun.addNewRPr();
  612. rpr.addNewVertAlign().setVal(STVerticalAlignRun.SUBSCRIPT);
  613. XWPFRun run = new XWPFRun(ctRun, irb);
  614. run.setSubscript(VerticalAlign.BASELINE);
  615. assertSame(STVerticalAlignRun.BASELINE, rpr.getVertAlignArray(0).getVal());
  616. }
  617. @Test
  618. void testSetGetEmphasisMark() throws IOException {
  619. try (XWPFDocument document = new XWPFDocument()) {
  620. XWPFRun run = document.createParagraph().createRun();
  621. assertSame(STEm.NONE, run.getEmphasisMark());
  622. // Reset to a fresh run so we test case of run not having property at all
  623. run = document.createParagraph().createRun();
  624. run.setEmphasisMark("dot");
  625. assertSame(STEm.DOT, run.getEmphasisMark());
  626. }
  627. }
  628. @Test
  629. void testSetGetUnderlineColor() throws IOException {
  630. try (XWPFDocument document = new XWPFDocument()) {
  631. XWPFRun run = document.createParagraph().createRun();
  632. assertEquals("auto", run.getUnderlineColor());
  633. // Reset to a fresh run so we test case of run not having property at all
  634. run = document.createParagraph().createRun();
  635. String colorRgb = "C0F1a2";
  636. run.setUnderlineColor(colorRgb);
  637. assertEquals(colorRgb.toUpperCase(LocaleUtil.getUserLocale()), run.getUnderlineColor());
  638. run.setUnderlineColor("auto");
  639. assertEquals("auto", run.getUnderlineColor());
  640. }
  641. }
  642. @Test
  643. void testSetGetUnderlineThemeColor() throws IOException {
  644. try (XWPFDocument document = new XWPFDocument()) {
  645. XWPFRun run = document.createParagraph().createRun();
  646. assertSame(STThemeColor.NONE, run.getUnderlineThemeColor());
  647. // Reset to a fresh run so we test case of run not having property at all
  648. run = document.createParagraph().createRun();
  649. String colorName = "accent4";
  650. run.setUnderlineThemeColor(colorName);
  651. assertSame(STThemeColor.Enum.forString(colorName), run.getUnderlineThemeColor());
  652. run.setUnderlineThemeColor("none");
  653. assertSame(STThemeColor.NONE, run.getUnderlineThemeColor());
  654. }
  655. }
  656. @Test
  657. void testSetStyleId() throws IOException {
  658. try (XWPFDocument document = openSampleDocument("SampleDoc.docx")) {
  659. final XWPFRun run = document.createParagraph().createRun();
  660. String styleId = "bolditalic";
  661. run.setStyle(styleId);
  662. String candStyleId = run.getCTR().getRPr().getRStyleArray(0).getVal();
  663. assertNotNull(candStyleId, "Expected to find a run style ID");
  664. assertEquals(styleId, candStyleId);
  665. assertEquals(styleId, run.getStyle());
  666. }
  667. }
  668. @Test
  669. void testGetEmptyStyle() throws IOException {
  670. try (XWPFDocument document = new XWPFDocument()) {
  671. final XWPFRun run = document.createParagraph().createRun();
  672. assertEquals("", run.getStyle());
  673. run.getCTR().addNewRPr();
  674. assertEquals("", run.getStyle());
  675. }
  676. }
  677. @Test
  678. void testGetDepthWidth() throws IOException, InvalidFormatException {
  679. try (XWPFDocument doc = openSampleDocument("TestDocument.docx")) {
  680. XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
  681. XWPFParagraph p = hdr.createParagraph();
  682. XWPFRun r = p.createRun();
  683. assertEquals(0, hdr.getAllPictures().size());
  684. assertEquals(0, r.getEmbeddedPictures().size());
  685. r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
  686. assertEquals(1, hdr.getAllPictures().size());
  687. assertEquals(1, r.getEmbeddedPictures().size());
  688. XWPFPicture pic = r.getEmbeddedPictures().get(0);
  689. assertEquals(pic.getWidth(), Units.toPoints(21), 0.0);
  690. assertEquals(pic.getDepth(), Units.toPoints(32), 0.0);
  691. }
  692. }
  693. @Test
  694. void testWhitespace() throws IOException {
  695. String[] text = new String[] {
  696. " The quick brown fox",
  697. "\t\tjumped over the lazy dog"
  698. };
  699. try (XWPFDocument doc1 = new XWPFDocument()) {
  700. for(String s : text) {
  701. XWPFParagraph p1 = doc1.createParagraph();
  702. XWPFRun r1 = p1.createRun();
  703. r1.setText(s);
  704. }
  705. try (XWPFDocument doc2 = writeOutAndReadBack(doc1)) {
  706. List<XWPFParagraph> paragraphs = doc2.getParagraphs();
  707. assertEquals(2, paragraphs.size());
  708. for (int i = 0; i < text.length; i++) {
  709. XWPFParagraph p1 = paragraphs.get(i);
  710. String expected = text[i];
  711. assertEquals(expected, p1.getText());
  712. CTP ctp = p1.getCTP();
  713. CTR ctr = ctp.getRArray(0);
  714. CTText ctText = ctr.getTArray(0);
  715. // if text has leading whitespace then expect xml-fragment to have xml:space="preserve" set
  716. // <xml-fragment xml:space="preserve" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  717. boolean isWhitespace = Character.isWhitespace(expected.charAt(0));
  718. assertEquals(isWhitespace, ctText.isSetSpace());
  719. }
  720. }
  721. }
  722. }
  723. }