From c715f9ee1dd5cb826ce79d116c403fc3cc365691 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 Mar 2024 13:33:57 +0000 Subject: [PATCH] [github-607] Support SVGs in XWPF. Thanks to Etienne Gautier. This closes #607 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1916582 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/poi/xwpf/usermodel/Document.java | 5 +++++ .../java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java | 4 +++- .../java/org/apache/poi/xwpf/usermodel/XWPFRelation.java | 6 ++++++ .../main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java | 1 + .../org/apache/poi/xwpf/usermodel/TestXWPFDocument.java | 5 +++-- .../java/org/apache/poi/common/usermodel/PictureType.java | 2 +- 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/Document.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/Document.java index c124bd536a..9099553da4 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/Document.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/Document.java @@ -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; } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java index 729651e868..d74440fcc9 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java @@ -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() { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java index 83ed6c11f1..4566e6113a 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java @@ -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, diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index e86ca85720..481e5ba8d2 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -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) diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java index 9b1502df55..0546e7cd11 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java @@ -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()); } } } diff --git a/poi/src/main/java/org/apache/poi/common/usermodel/PictureType.java b/poi/src/main/java/org/apache/poi/common/usermodel/PictureType.java index d8497125ac..40427c05b0 100644 --- a/poi/src/main/java/org/apache/poi/common/usermodel/PictureType.java +++ b/poi/src/main/java/org/apache/poi/common/usermodel/PictureType.java @@ -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 */ -- 2.39.5