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.

TestHemfPicture.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.hemf.usermodel;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertThrows;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import java.awt.geom.Point2D;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.InputStream;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.ArrayList;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Set;
  29. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  30. import org.apache.poi.POIDataSamples;
  31. import org.apache.poi.hemf.record.emf.HemfComment;
  32. import org.apache.poi.hemf.record.emf.HemfComment.EmfComment;
  33. import org.apache.poi.hemf.record.emf.HemfComment.EmfCommentDataFormat;
  34. import org.apache.poi.hemf.record.emf.HemfComment.EmfCommentDataMultiformats;
  35. import org.apache.poi.hemf.record.emf.HemfHeader;
  36. import org.apache.poi.hemf.record.emf.HemfRecord;
  37. import org.apache.poi.hemf.record.emf.HemfRecordType;
  38. import org.apache.poi.hemf.record.emf.HemfText;
  39. import org.apache.poi.hwmf.record.HwmfRecord;
  40. import org.apache.poi.hwmf.record.HwmfText;
  41. import org.apache.poi.hwmf.usermodel.HwmfEmbedded;
  42. import org.apache.poi.hwmf.usermodel.HwmfEmbeddedType;
  43. import org.apache.poi.hwmf.usermodel.HwmfPicture;
  44. import org.apache.poi.util.IOUtils;
  45. import org.apache.poi.util.RecordFormatException;
  46. import org.junit.jupiter.api.Test;
  47. public class TestHemfPicture {
  48. private static final POIDataSamples ss_samples = POIDataSamples.getSpreadSheetInstance();
  49. private static final POIDataSamples sl_samples = POIDataSamples.getSlideShowInstance();
  50. @Test
  51. void testBasicWindows() throws Exception {
  52. try (InputStream is = ss_samples.openResourceAsStream("SimpleEMF_windows.emf")) {
  53. HemfPicture pic = new HemfPicture(is);
  54. HemfHeader header = pic.getHeader();
  55. assertEquals(27864, header.getBytes());
  56. assertEquals(31, header.getRecords());
  57. assertEquals(3, header.getHandles());
  58. assertEquals(346000, header.getMicroDimension().getWidth(), 0);
  59. assertEquals(194000, header.getMicroDimension().getHeight(), 0);
  60. List<HemfRecord> records = pic.getRecords();
  61. assertEquals(31, records.size());
  62. }
  63. }
  64. @Test
  65. void testBasicMac() throws Exception {
  66. try (InputStream is = ss_samples.openResourceAsStream("SimpleEMF_mac.emf")) {
  67. HemfPicture pic = new HemfPicture(is);
  68. HemfHeader header = pic.getHeader();
  69. int records = 0;
  70. boolean extractedData = false;
  71. for (HemfRecord record : pic) {
  72. if (record.getEmfRecordType() == HemfRecordType.comment) {
  73. HemfComment.EmfCommentData comment = ((EmfComment) record).getCommentData();
  74. if (comment instanceof EmfCommentDataMultiformats) {
  75. for (EmfCommentDataFormat d : ((EmfCommentDataMultiformats) comment).getFormats()) {
  76. byte[] data = d.getRawData();
  77. //make sure header starts at 0
  78. assertEquals('%', data[0]);
  79. assertEquals('P', data[1]);
  80. assertEquals('D', data[2]);
  81. assertEquals('F', data[3]);
  82. //make sure byte array ends at EOF\n
  83. assertEquals('E', data[data.length - 4]);
  84. assertEquals('O', data[data.length - 3]);
  85. assertEquals('F', data[data.length - 2]);
  86. assertEquals('\n', data[data.length - 1]);
  87. extractedData = true;
  88. }
  89. }
  90. }
  91. records++;
  92. }
  93. assertTrue(extractedData);
  94. assertEquals(header.getRecords(), records);
  95. }
  96. }
  97. @Test
  98. void testMacText() throws Exception {
  99. try (InputStream is = ss_samples.openResourceAsStream("SimpleEMF_mac.emf")) {
  100. HemfPicture pic = new HemfPicture(is);
  101. double lastY = -1;
  102. double lastX = -1;
  103. //derive this from the font information!
  104. long fudgeFactorX = 1000;
  105. StringBuilder sb = new StringBuilder();
  106. for (HemfRecord record : pic) {
  107. if (record.getEmfRecordType().equals(HemfRecordType.extTextOutW)) {
  108. HemfText.EmfExtTextOutW extTextOutW = (HemfText.EmfExtTextOutW) record;
  109. Point2D reference = extTextOutW.getReference();
  110. if (lastY > -1 && lastY != reference.getY()) {
  111. sb.append("\n");
  112. lastX = -1;
  113. }
  114. if (lastX > -1 && reference.getX() - lastX > fudgeFactorX) {
  115. sb.append(" ");
  116. }
  117. sb.append(extTextOutW.getText());
  118. lastY = reference.getY();
  119. lastX = reference.getX();
  120. }
  121. }
  122. String txt = sb.toString();
  123. assertContains(txt, "Tika http://incubator.apache.org");
  124. assertContains(txt, "Latest News\n");
  125. }
  126. }
  127. @Test
  128. void testWMFInsideEMF() throws Exception {
  129. byte[] wmfData = null;
  130. try (InputStream is = ss_samples.openResourceAsStream("63327.emf")) {
  131. HemfPicture pic = new HemfPicture(is);
  132. for (HemfRecord record : pic) {
  133. if (record.getEmfRecordType() == HemfRecordType.comment) {
  134. HemfComment.EmfComment commentRecord = (HemfComment.EmfComment) record;
  135. HemfComment.EmfCommentData emfCommentData = commentRecord.getCommentData();
  136. if (emfCommentData instanceof HemfComment.EmfCommentDataWMF) {
  137. wmfData = ((HemfComment.EmfCommentDataWMF) emfCommentData).getWMFData();
  138. }
  139. }
  140. }
  141. }
  142. assertNotNull(wmfData);
  143. assertEquals(230, wmfData.length);
  144. HwmfPicture pict = new HwmfPicture(new ByteArrayInputStream(wmfData));
  145. String embedded = null;
  146. for (HwmfRecord r : pict.getRecords()) {
  147. if (r instanceof HwmfText.WmfTextOut) {
  148. embedded = ((HwmfText.WmfTextOut) r).getText(StandardCharsets.US_ASCII);
  149. }
  150. }
  151. assertNotNull(embedded);
  152. assertEquals("Hw.txt", embedded);
  153. }
  154. @Test
  155. void testWindowsText() throws Exception {
  156. try (InputStream is = ss_samples.openResourceAsStream("SimpleEMF_windows.emf")) {
  157. HemfPicture pic = new HemfPicture(is);
  158. double lastY = -1;
  159. double lastX = -1;
  160. long fudgeFactorX = 1000;//derive this from the font or frame/bounds information
  161. StringBuilder sb = new StringBuilder();
  162. Set<String> expectedParts = new HashSet<>();
  163. expectedParts.add("C:\\Users\\tallison\\");
  164. expectedParts.add("testPDF.pdf");
  165. int foundExpected = 0;
  166. for (HemfRecord record : pic) {
  167. if (record.getEmfRecordType().equals(HemfRecordType.extTextOutW)) {
  168. HemfText.EmfExtTextOutW extTextOutW = (HemfText.EmfExtTextOutW) record;
  169. Point2D reference = extTextOutW.getReference();
  170. if (lastY > -1 && lastY != reference.getY()) {
  171. sb.append("\n");
  172. lastX = -1;
  173. }
  174. if (lastX > -1 && reference.getX() - lastX > fudgeFactorX) {
  175. sb.append(" ");
  176. }
  177. String txt = extTextOutW.getText();
  178. if (expectedParts.contains(txt)) {
  179. foundExpected++;
  180. }
  181. sb.append(txt);
  182. lastY = reference.getY();
  183. lastX = reference.getX();
  184. }
  185. }
  186. String txt = sb.toString();
  187. assertContains(txt, "C:\\Users\\tallison\\\n");
  188. assertContains(txt, "asf2-git-1.x\\tika-\n");
  189. assertEquals(expectedParts.size(), foundExpected);
  190. }
  191. }
  192. @Test
  193. void testInfiniteLoopOnFile() throws Exception {
  194. try (InputStream is = ss_samples.openResourceAsStream("61294.emf")) {
  195. HemfPicture pic = new HemfPicture(is);
  196. assertThrows(RecordFormatException.class, () -> pic.forEach(r -> {}));
  197. }
  198. }
  199. @Test
  200. void testInfiniteLoopOnByteArray() throws Exception {
  201. try (InputStream is = ss_samples.openResourceAsStream("61294.emf")) {
  202. UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
  203. IOUtils.copy(is, bos);
  204. HemfPicture pic = new HemfPicture(bos.toInputStream());
  205. assertThrows(RecordFormatException.class, () -> pic.forEach(r -> {}));
  206. }
  207. }
  208. @Test
  209. void nestedWmfEmf() throws Exception {
  210. try (InputStream is = sl_samples.openResourceAsStream("nested_wmf.emf")) {
  211. HemfPicture emf1 = new HemfPicture(is);
  212. List<HwmfEmbedded> embeds = new ArrayList<>();
  213. emf1.getEmbeddings().forEach(embeds::add);
  214. assertEquals(1, embeds.size());
  215. assertEquals(HwmfEmbeddedType.WMF, embeds.get(0).getEmbeddedType());
  216. HwmfPicture wmf = new HwmfPicture(new ByteArrayInputStream(embeds.get(0).getRawData()));
  217. embeds.clear();
  218. wmf.getEmbeddings().forEach(embeds::add);
  219. assertEquals(3, embeds.size());
  220. assertEquals(HwmfEmbeddedType.EMF, embeds.get(0).getEmbeddedType());
  221. HemfPicture emf2 = new HemfPicture(new ByteArrayInputStream(embeds.get(0).getRawData()));
  222. embeds.clear();
  223. emf2.getEmbeddings().forEach(embeds::add);
  224. assertTrue(embeds.isEmpty());
  225. }
  226. }
  227. /* govdocs1 064213.doc-0.emf contains an example of extextouta */
  228. }