]> source.dussan.org Git - poi.git/commitdiff
bug 63327 allow retrieval of wmf data embedded in emf
authorTim Allison <tallison@apache.org>
Mon, 8 Apr 2019 19:53:21 +0000 (19:53 +0000)
committerTim Allison <tallison@apache.org>
Mon, 8 Apr 2019 19:53:21 +0000 (19:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1857136 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfComment.java
src/scratchpad/testcases/org/apache/poi/hemf/usermodel/HemfPictureTest.java
test-data/slideshow/60677.wmf [new file with mode: 0644]
test-data/spreadsheet/63327.emf [new file with mode: 0644]

index 3162ef5e27319fe3081b3478e751e75680a2cd2c..c9ea7481e135ccaf8d152ef15a5bbf813202d0f1 100644 (file)
@@ -408,7 +408,7 @@ public class HemfComment {
     public static class EmfCommentDataWMF implements EmfCommentData {
         private final Rectangle2D bounds = new Rectangle2D.Double();
         private final List<EmfCommentDataFormat> formats = new ArrayList<>();
-
+        private byte[] wmfData;
         @Override
         public HemfCommentRecordType getCommentRecordType() {
             return HemfCommentRecordType.emfWMF;
@@ -439,12 +439,16 @@ public class HemfComment {
             // WMF metafile in the WinMetafile field.
             int winMetafileSize = (int)leis.readUInt();
 
-            byte[] winMetafile = IOUtils.safelyAllocate(winMetafileSize, MAX_RECORD_LENGTH);
+            wmfData = IOUtils.safelyAllocate(winMetafileSize, MAX_RECORD_LENGTH);
             // some emf comments are truncated, so we don't use readFully here
-            leis.read(winMetafile);
+            leis.read(wmfData);
 
             return leis.getReadIndex()-startIdx;
         }
+
+        public byte[] getWMFData() {
+            return wmfData;
+        }
     }
 
     public static class EmfCommentDataUnicode implements EmfCommentData {
index 5f630b563e2ae6a85d6baaa966e09a415df0177f..07b58f647384d9fef11aee85801bfd4879633cdb 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi.hemf.usermodel;
 
 import static org.apache.poi.POITestCase.assertContains;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.awt.geom.Point2D;
@@ -39,6 +40,7 @@ import java.util.List;
 import java.util.Set;
 import java.util.stream.Stream;
 
+import com.sun.xml.bind.api.impl.NameConverter;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hemf.record.emf.HemfComment;
 import org.apache.poi.hemf.record.emf.HemfComment.EmfComment;
@@ -48,6 +50,9 @@ import org.apache.poi.hemf.record.emf.HemfHeader;
 import org.apache.poi.hemf.record.emf.HemfRecord;
 import org.apache.poi.hemf.record.emf.HemfRecordType;
 import org.apache.poi.hemf.record.emf.HemfText;
+import org.apache.poi.hwmf.record.HwmfRecord;
+import org.apache.poi.hwmf.record.HwmfText;
+import org.apache.poi.hwmf.usermodel.HwmfPicture;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.RecordFormatException;
 import org.junit.Test;
@@ -284,6 +289,35 @@ public class HemfPictureTest {
         }
     }
 
+    @Test
+    public void testWMFInsideEMF() throws Exception {
+
+        byte[] wmfData = null;
+        try (InputStream is = ss_samples.openResourceAsStream("63327.emf")) {
+            HemfPicture pic = new HemfPicture(is);
+            for (HemfRecord record : pic) {
+                if (record.getEmfRecordType() == HemfRecordType.comment) {
+                    HemfComment.EmfComment commentRecord = (HemfComment.EmfComment) record;
+                    HemfComment.EmfCommentData emfCommentData = commentRecord.getCommentData();
+                    if (emfCommentData instanceof HemfComment.EmfCommentDataWMF) {
+                        wmfData = ((HemfComment.EmfCommentDataWMF) emfCommentData).getWMFData();
+                    }
+                }
+            }
+        }
+        assertNotNull(wmfData);
+        assertEquals(230, wmfData.length);
+        HwmfPicture pict = new HwmfPicture(new ByteArrayInputStream(wmfData));
+        String embedded = null;
+        for (HwmfRecord r : pict.getRecords()) {
+            if (r instanceof HwmfText.WmfTextOut) {
+                embedded = ((HwmfText.WmfTextOut) r).getText(StandardCharsets.US_ASCII);
+            }
+        }
+        assertNotNull(embedded);
+        assertEquals("Hw.txt", embedded);
+    }
+
     @Test
     public void testWindowsText() throws Exception {
         try (InputStream is = ss_samples.openResourceAsStream("SimpleEMF_windows.emf")) {
diff --git a/test-data/slideshow/60677.wmf b/test-data/slideshow/60677.wmf
new file mode 100644 (file)
index 0000000..6399d71
Binary files /dev/null and b/test-data/slideshow/60677.wmf differ
diff --git a/test-data/spreadsheet/63327.emf b/test-data/spreadsheet/63327.emf
new file mode 100644 (file)
index 0000000..8e0e457
Binary files /dev/null and b/test-data/spreadsheet/63327.emf differ