]> source.dussan.org Git - poi.git/commitdiff
[github-607] Support SVGs in XWPF. Thanks to Etienne Gautier. This closes #607
authorPJ Fanning <fanningpj@apache.org>
Wed, 27 Mar 2024 13:33:57 +0000 (13:33 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 27 Mar 2024 13:33:57 +0000 (13:33 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1916582 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/Document.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java
poi/src/main/java/org/apache/poi/common/usermodel/PictureType.java

index c124bd536a68aa1dd9bc8355d19d91b06a0d78a8..9099553da4d976cd60280e2b71ddf5c6cca5b351 100644 (file)
@@ -74,4 +74,9 @@ public interface Document {
      * WordPerfect graphics (.wpg)
      */
     public static final int PICTURE_TYPE_WPG = PictureType.WPG.ooxmlId;
+
+    /**
+     * SVG graphics (.svg)
+     */
+    public static final int PICTURE_TYPE_SVG = PictureType.SVG.ooxmlId;
 }
index 729651e86814dda1ae8c5ad5a3f493283493b5e6..d74440fcc9526ee31a154c859bb678d9a4b666b8 100644 (file)
@@ -58,7 +58,7 @@ public class XWPFPictureData extends POIXMLDocumentPart {
     protected static final POIXMLRelation[] RELATIONS;
 
     static {
-        RELATIONS = new POIXMLRelation[14];
+        RELATIONS = new POIXMLRelation[15];
         RELATIONS[PictureType.EMF.ooxmlId] = XWPFRelation.IMAGE_EMF;
         RELATIONS[PictureType.WMF.ooxmlId] = XWPFRelation.IMAGE_WMF;
         RELATIONS[PictureType.PICT.ooxmlId] = XWPFRelation.IMAGE_PICT;
@@ -71,6 +71,7 @@ public class XWPFPictureData extends POIXMLDocumentPart {
         RELATIONS[PictureType.BMP.ooxmlId] = XWPFRelation.IMAGE_BMP;
         RELATIONS[PictureType.WPG.ooxmlId] = XWPFRelation.IMAGE_WPG;
         RELATIONS[PictureType.WDP.ooxmlId] = XWPFRelation.HDPHOTO_WDP;
+        RELATIONS[PictureType.SVG.ooxmlId] = XWPFRelation.IMAGE_SVG;
     }
 
     private Long checksum;
@@ -149,6 +150,7 @@ public class XWPFPictureData extends POIXMLDocumentPart {
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_GIF
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
+     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_SVG
      * @see #getPictureTypeEnum()
      */
     public int getPictureType() {
index 83ed6c11f1115f482515725315efa4992cd8b138..4566e6113a36b03e1592aec44196a05365fc50ec 100644 (file)
@@ -234,6 +234,12 @@ public final class XWPFRelation extends POIXMLRelation {
             "/ppt/media/hdphoto#.wdp",
             XWPFPictureData::new, XWPFPictureData::new
     );
+    public static final XWPFRelation IMAGE_SVG = new XWPFRelation(
+        PictureType.SVG.contentType,
+        IMAGE_PART,
+        "/word/media/image#.svg",
+        XWPFPictureData::new, XWPFPictureData::new
+);
     public static final XWPFRelation IMAGES = new XWPFRelation(
             null,
             IMAGE_PART,
index e86ca8572071fc7153c5c623216f2a4314934764..481e5ba8d21a8ed7698d36e778e84fef9ae11384 100644 (file)
@@ -1207,6 +1207,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_GIF
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
+     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_SVG
      * @see #addPicture(InputStream, PictureType, String, int, int)
      */
     public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
index 9b1502df557d0e747a79a10ad4865fc1961045b0..0546e7cd11b511abedc4e73ba7468c0bb10f2ad9 100644 (file)
@@ -191,11 +191,12 @@ public final class TestXWPFDocument {
             doc.addPictureData(new byte[18], Document.PICTURE_TYPE_EPS);
             doc.addPictureData(new byte[19], Document.PICTURE_TYPE_BMP);
             doc.addPictureData(new byte[20], Document.PICTURE_TYPE_WPG);
+            doc.addPictureData(new byte[21], Document.PICTURE_TYPE_SVG);
 
-            assertEquals(11, doc.getAllPictures().size());
+            assertEquals(12, doc.getAllPictures().size());
 
             try (XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
-                assertEquals(11, doc2.getAllPictures().size());
+                assertEquals(12, doc2.getAllPictures().size());
             }
         }
     }
index d8497125ac29850a98acdd5548d4c48f8cb18fb6..40427c05b059bf63cf0649d45f07fce0c932087f 100644 (file)
@@ -53,7 +53,7 @@ public enum PictureType {
     /** Microsoft Windows Media Photo image (.wdp) */
     WDP("image/vnd.ms-photo", ".wdp", 13),
     /** Scalable vector graphics (.svg) - supported by Office 2016 and higher */
-    SVG("image/svg+xml", ".svg", -1),
+    SVG("image/svg+xml", ".svg", 14),
     /** Unknown picture type - specific to escher bse record */
     UNKNOWN("", ".dat", -1),
     /** Picture type error - specific to escher bse record */