From 121657de30d29afbdbcd008ebc37c01035bf3d73 Mon Sep 17 00:00:00 2001 From: Vlad-Florin Ilie Date: Fri, 26 Aug 2022 18:07:46 +0300 Subject: [PATCH 1/6] Added feature for allowing the user to fit an external image in the template keeping the aspect ratio --- .../DocXBufferedDocumentContentHandler.java | 24 +++++--- .../images/AbstractImageProvider.java | 59 ++++++++++++++++++- .../images/AbstractImageRegistry.java | 8 +-- .../document/images/IImageProvider.java | 28 ++++++++- .../document/images/IImageRegistry.java | 6 +- .../xdocreport/document/json/JSONImage.java | 18 +++++- ...eprocessorImageWithFreemarkerTestCase.java | 8 +-- ...PreprocessorImageWithVelocityTestCase.java | 8 +-- 8 files changed, 130 insertions(+), 29 deletions(-) diff --git a/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java b/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java index cddaf0f08..316993992 100644 --- a/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java +++ b/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java @@ -182,26 +182,34 @@ public boolean doStartElement(String uri, String localName, String name, // modify "cx" and "cy" attribute with image script (Velocity, // Freemarker) // + // cx="${imageRegistry.getWidth(___imageInfo, '1262380', '1352550')}" + // cy="${imageRegistry.getHeight(___imageInfo, '1262380', '1352550')}" /> String newCX = null; String newCY = null; + String oldCX = null; + String oldCY = null; int cxIndex = attributes.getIndex(CX_ATTR); if (cxIndex != -1) { - String oldCX = attributes.getValue(cxIndex); + oldCX = attributes.getValue(cxIndex); + } + int cyIndex = attributes.getIndex(CY_ATTR); + if (cyIndex != -1) { + oldCY = attributes.getValue(cyIndex); + } + + if (oldCX != null) { newCX = formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY, IImageRegistry.GET_WIDTH_METHOD, - IImageRegistry.IMAGE_INFO, "'" + oldCX + "'"); + IImageRegistry.IMAGE_INFO, "'" + oldCX + "'", "'" + oldCY + "'"); } - int cyIndex = attributes.getIndex(CY_ATTR); - if (cyIndex != -1) { - String oldCY = attributes.getValue(cyIndex); + if (oldCY != null) { newCY = formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY, IImageRegistry.GET_HEIGHT_METHOD, - IImageRegistry.IMAGE_INFO, "'" + oldCY + "'"); + IImageRegistry.IMAGE_INFO, "'" + oldCX + "'", "'" + oldCY + "'"); } + if (newCX != null || newCY != null) { AttributesImpl attr = toAttributesImpl(attributes); if (newCX != null) { diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java index 3cc2f9bb0..21a48e60b 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java @@ -80,11 +80,28 @@ public void setUseImageSize( boolean useImageSize ) /* * (non-Javadoc) - * @see fr.opensagres.xdocreport.document.images.IImageProvider#getWidth() + * @see fr.opensagres.xdocreport.document.images.IImageProvider#getWidth(java.lang.Float) */ public Float getWidth(Float defaultWidth) throws IOException { + return getWidth(defaultWidth, null); + } + + /* + * (non-Javadoc) + * @see fr.opensagres.xdocreport.document.images.IImageProvider#getWidth(java.lang.Float, java.lang.Float) + */ + public Float getWidth(Float defaultWidth, Float defaultHeight) + throws IOException + { + // check if user wants to resize the image in order to fit template + // replacement image aspect ratio is always kept intact + if ( height == null && width == null && defaultWidth != null + && defaultHeight != null && !isUseImageSize() && isResize() ) + { + return getDimensions(defaultWidth, defaultHeight)[0]; + } if ( width != null ) { return width; @@ -112,11 +129,28 @@ public Float getWidth(Float defaultWidth) /* * (non-Javadoc) - * @see fr.opensagres.xdocreport.document.images.IImageProvider#getHeight() + * @see fr.opensagres.xdocreport.document.images.IImageProvider#getHeight(java.lang.Float) */ public Float getHeight(Float defaultHeight) throws IOException { + return getHeight(null, defaultHeight); + } + + /* + * (non-Javadoc) + * @see fr.opensagres.xdocreport.document.images.IImageProvider#getHeight(java.lang.Float, java.lang.Float) + */ + public Float getHeight(Float defaultWidth, Float defaultHeight) + throws IOException + { + // check if user wants to resize the image in order to fit template + // replacement image aspect ratio is always kept intact + if ( height == null && width == null && defaultWidth != null + && defaultHeight != null && !isUseImageSize() && isResize() ) + { + return getDimensions(defaultWidth, defaultHeight)[1]; + } if ( height != null ) { return height; @@ -142,6 +176,27 @@ public Float getHeight(Float defaultHeight) return heightFromImageInfo = new Float( getImageInfo().getHeight() ); } + /** + * Compute the new dimensions of the replacement image in order to fit the template. + * + * @param defaultWidth template width + * @param defaultHeight template height + * @return new dimensions of the image + * @throws IOException + */ + private Float[] getDimensions(Float defaultWidth, Float defaultHeight) + throws IOException + { + float realImageHeight = getImageInfo().getHeight(); + float realImageWidth = getImageInfo().getWidth(); + float scale = Math.min(defaultWidth / realImageWidth, defaultHeight / realImageHeight); + + float finalImageHeight = realImageHeight * scale; + float finalImageWidth = realImageWidth * scale; + + return new Float[]{finalImageWidth, finalImageHeight}; + } + /* * (non-Javadoc) * @see fr.opensagres.xdocreport.document.images.IImageProvider#setWidth(java .lang.Float) diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java index 1c0119e4a..951a4bed8 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java @@ -255,7 +255,7 @@ public String getPath( ImageProviderInfo info, String defaultPath ) protected abstract String getPath( ImageProviderInfo info ); - public String getWidth( ImageProviderInfo info, String defaultWidth ) + public String getWidth( ImageProviderInfo info, String defaultWidth, String defaultHeight ) throws IOException { if ( info.isKeepImageTemplate() ) @@ -263,7 +263,7 @@ public String getWidth( ImageProviderInfo info, String defaultWidth ) return defaultWidth; } IImageProvider imageProvider = info.getImageProvider(); - Float width = imageProvider.getWidth(getSize(defaultWidth)); + Float width = imageProvider.getWidth(getSize(defaultWidth), getSize(defaultHeight)); if ( width != null ) { return getSize( width ); @@ -271,7 +271,7 @@ public String getWidth( ImageProviderInfo info, String defaultWidth ) return defaultWidth; } - public String getHeight( ImageProviderInfo info, String defaultHeight ) + public String getHeight( ImageProviderInfo info, String defaultWidth, String defaultHeight ) throws IOException { if ( info.isKeepImageTemplate() ) @@ -279,7 +279,7 @@ public String getHeight( ImageProviderInfo info, String defaultHeight ) return defaultHeight; } IImageProvider imageProvider = info.getImageProvider(); - Float height = imageProvider.getHeight(getSize(defaultHeight)); + Float height = imageProvider.getHeight(getSize(defaultWidth), getSize(defaultHeight)); if ( height != null ) { return getSize( height ); diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageProvider.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageProvider.java index 5d8ebed5f..ee816b33a 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageProvider.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageProvider.java @@ -56,12 +56,23 @@ void write( OutputStream outputStream ) * Returns the width image with pixel unit. * * @param defaultWidth as pixel + * @param defaultHeight as pixel * @return * @throws IOException */ - Float getWidth(Float defaultWidth) + Float getWidth(Float defaultWidth, Float defaultHeight) throws IOException; + /** + * Returns the width image with pixel unit. + * + * @param defaultWidth as pixel + * @return + * @throws IOException + */ + Float getWidth(Float defaultWidth) + throws IOException; + /** * Set the width image with pixel unit. * @@ -71,14 +82,25 @@ Float getWidth(Float defaultWidth) /** * Returns the height image with pixel unit. - * + * + * @param defaultWidth as pixel * @param defaultHeight as pixel * @return * @throws IOException */ - Float getHeight(Float defaultHeight) + Float getHeight(Float defaultWidth, Float defaultHeight) throws IOException; + /** + * Returns the height image with pixel unit. + * + * @param defaultHeight as pixel + * @return + * @throws IOException + */ + Float getHeight(Float defaultHeight) + throws IOException; + /** * Set the height image with pixel unit. * diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java index 969b0df06..951ca5863 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java @@ -83,10 +83,11 @@ void postProcess() * * @param imageProvider * @param defaultWidth + * @param defaultHeight * @return * @throws IOException */ - String getWidth( ImageProviderInfo info, String defaultWidth ) + String getWidth( ImageProviderInfo info, String defaultWidth, String defaultHeight ) throws IOException; /** @@ -94,9 +95,10 @@ String getWidth( ImageProviderInfo info, String defaultWidth ) * * @param imageProvider * @param defaultWidth + * @param defaultHeight * @return * @throws IOException */ - String getHeight( ImageProviderInfo info, String defaultHeight ) + String getHeight( ImageProviderInfo info, String defaultWidth, String defaultHeight ) throws IOException; } diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/json/JSONImage.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/json/JSONImage.java index 49618d98a..e6048d3ba 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/json/JSONImage.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/json/JSONImage.java @@ -49,26 +49,40 @@ public ImageFormat getImageFormat() return null; } - public Float getWidth(Float defaultWidth) + public Float getWidth(Float defaultWidth, Float defaultHeight) throws IOException { // TODO Auto-generated method stub return null; } + @Override + public Float getWidth(Float defaultWidth) + throws IOException + { + return getWidth(defaultWidth, null); + } + public void setWidth( Float width ) { // TODO Auto-generated method stub } - public Float getHeight(Float defaultHeight) + public Float getHeight(Float defaultWidth, Float defaultHeight) throws IOException { // TODO Auto-generated method stub return null; } + @Override + public Float getHeight(Float defaultHeight) + throws IOException + { + return getHeight(null, defaultHeight); + } + public void setHeight( Float height ) { // TODO Auto-generated method stub diff --git a/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithFreemarkerTestCase.java b/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithFreemarkerTestCase.java index 533b58859..460ce803b 100644 --- a/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithFreemarkerTestCase.java +++ b/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithFreemarkerTestCase.java @@ -148,7 +148,7 @@ public void testImageWithSimpleField() + "" // + "" - + "" + + "" + "" + "" @@ -171,7 +171,7 @@ public void testImageWithSimpleField() + "" + "" // + "" - + "" + + "" + "" + "" + "" @@ -590,7 +590,7 @@ public void testImageWithListFieldInTable() + "" + "" // + "" - + "" + + "" + "" + "" @@ -614,7 +614,7 @@ public void testImageWithListFieldInTable() + "" + "" // + "" - + "" + + "" + "" + "" + "" diff --git a/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithVelocityTestCase.java b/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithVelocityTestCase.java index b37a2707f..45a6f56e6 100644 --- a/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithVelocityTestCase.java +++ b/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/docx/preprocessor/DocxPreprocessorImageWithVelocityTestCase.java @@ -147,7 +147,7 @@ public void testImageWithSimpleField() + "" + "" // + "" - + "" + + "" + "" + "" @@ -170,7 +170,7 @@ public void testImageWithSimpleField() + "" + "" // + "" - + "" + + "" + "" + "" + "" @@ -593,7 +593,7 @@ public void testImageWithListFieldInTable() + "" + "" // + "" - + "" + + "" + "" + "" + "" @@ -616,7 +616,7 @@ public void testImageWithListFieldInTable() + "" + "" // + "" - + "" + + "" + "" + "" + "" From 8df05ec7ab13fc8e7bce6ab32b51eed5b3dfeb0f Mon Sep 17 00:00:00 2001 From: Vlad-Florin Ilie Date: Fri, 26 Aug 2022 19:00:35 +0300 Subject: [PATCH 2/6] Fixed possible null value when retrieving image template size --- .../DocXBufferedDocumentContentHandler.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java b/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java index 316993992..6342dabf9 100644 --- a/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java +++ b/document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java @@ -184,6 +184,10 @@ public boolean doStartElement(String uri, String localName, String name, // + // "cx" and "cy" attributes can also be modified as follows + // String newCX = null; String newCY = null; String oldCX = null; @@ -197,17 +201,27 @@ public boolean doStartElement(String uri, String localName, String name, oldCY = attributes.getValue(cyIndex); } + // get the parameters for the get width and height methods + String[] parameters = null; + if (oldCX != null && oldCY != null) { + parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + oldCX + "'", "'" + oldCY + "'"}; + } else if (oldCX != null) { + parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + oldCX + "'"}; + } else if (oldCY != null) { + parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + oldCY + "'"}; + } + if (oldCX != null) { newCX = formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY, IImageRegistry.GET_WIDTH_METHOD, - IImageRegistry.IMAGE_INFO, "'" + oldCX + "'", "'" + oldCY + "'"); + parameters); } if (oldCY != null) { newCY = formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY, IImageRegistry.GET_HEIGHT_METHOD, - IImageRegistry.IMAGE_INFO, "'" + oldCX + "'", "'" + oldCY + "'"); + parameters); } if (newCX != null || newCY != null) { From c145274e097637048d3146bcb5aff63ec53b93d3 Mon Sep 17 00:00:00 2001 From: Vlad-Florin Ilie Date: Fri, 26 Aug 2022 21:03:16 +0300 Subject: [PATCH 3/6] Added override for image registry get width and height methods --- .../images/AbstractImageRegistry.java | 12 ++++++++++ .../document/images/IImageRegistry.java | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java index 951a4bed8..9ceafa630 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java @@ -255,6 +255,12 @@ public String getPath( ImageProviderInfo info, String defaultPath ) protected abstract String getPath( ImageProviderInfo info ); + public String getWidth( ImageProviderInfo info, String defaultWidth ) + throws IOException + { + return getWidth(info, defaultWidth, null); + } + public String getWidth( ImageProviderInfo info, String defaultWidth, String defaultHeight ) throws IOException { @@ -271,6 +277,12 @@ public String getWidth( ImageProviderInfo info, String defaultWidth, String defa return defaultWidth; } + public String getHeight( ImageProviderInfo info, String defaultHeight ) + throws IOException + { + return getHeight(info, null, defaultHeight); + } + public String getHeight( ImageProviderInfo info, String defaultWidth, String defaultHeight ) throws IOException { diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java index 951ca5863..f40302c33 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java @@ -78,6 +78,17 @@ void postProcess() */ List getImageProviderInfos(); + /** + * Returns the width of the image provider if not null and the given defaultWidth otherwise. + * + * @param imageProvider + * @param defaultWidth + * @return + * @throws IOException + */ + String getWidth( ImageProviderInfo info, String defaultWidth ) + throws IOException; + /** * Returns the width of the image provider if not null and the given defaultWidth otherwise. * @@ -90,6 +101,17 @@ void postProcess() String getWidth( ImageProviderInfo info, String defaultWidth, String defaultHeight ) throws IOException; + /** + * Returns the height of the image provider if not null and the given defaultHeight otherwise. + * + * @param imageProvider + * @param defaultHeight + * @return + * @throws IOException + */ + String getHeight( ImageProviderInfo info, String defaultHeight ) + throws IOException; + /** * Returns the height of the image provider if not null and the given defaultHeight otherwise. * From e08856f97e12e016af82a1d5883a93340daab1a7 Mon Sep 17 00:00:00 2001 From: Vlad-Florin Ilie Date: Fri, 26 Aug 2022 21:05:08 +0300 Subject: [PATCH 4/6] Cache new size for the replacement image --- .../document/images/AbstractImageProvider.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java index 21a48e60b..b91e7559d 100644 --- a/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java +++ b/document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java @@ -45,6 +45,8 @@ public abstract class AbstractImageProvider private Float heightFromImageInfo; + private Float[] fittedImageDimensions; + private boolean useImageSize; private boolean resize; @@ -187,6 +189,11 @@ public Float getHeight(Float defaultWidth, Float defaultHeight) private Float[] getDimensions(Float defaultWidth, Float defaultHeight) throws IOException { + if ( fittedImageDimensions != null ) + { + return fittedImageDimensions; + } + float realImageHeight = getImageInfo().getHeight(); float realImageWidth = getImageInfo().getWidth(); float scale = Math.min(defaultWidth / realImageWidth, defaultHeight / realImageHeight); @@ -194,7 +201,8 @@ private Float[] getDimensions(Float defaultWidth, Float defaultHeight) float finalImageHeight = realImageHeight * scale; float finalImageWidth = realImageWidth * scale; - return new Float[]{finalImageWidth, finalImageHeight}; + fittedImageDimensions = new Float[]{finalImageWidth, finalImageHeight}; + return fittedImageDimensions; } /* From ec6022f53ec079495681efce299c515d206f870b Mon Sep 17 00:00:00 2001 From: Vlad-Florin Ilie Date: Fri, 26 Aug 2022 21:23:38 +0300 Subject: [PATCH 5/6] Added image fit support for ODT --- .../ODTBufferedDocumentContentHandler.java | 32 +++++++++++++++---- ...eprocessorImageWithFreemarkerTestCase.java | 8 ++--- ...PreprocessorImageWithVelocityTestCase.java | 8 ++--- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTBufferedDocumentContentHandler.java b/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTBufferedDocumentContentHandler.java index e0bdf9f51..fad9b8817 100644 --- a/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTBufferedDocumentContentHandler.java +++ b/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTBufferedDocumentContentHandler.java @@ -195,24 +195,44 @@ else if ( isDrawFrame( uri, localName, name ) ) // String newWith = null; String newHeight = null; + String defaultWidth = null; + String defaultHeight = null; int widthIndex = attributes.getIndex( SVG_NS, WIDTH_ATTR ); if ( widthIndex != -1 ) { - String defaultWidth = attributes.getValue( widthIndex ); + defaultWidth = attributes.getValue( widthIndex ); + } + int heightIndex = attributes.getIndex( SVG_NS, HEIGHT_ATTR ); + if ( heightIndex != -1 ) + { + defaultHeight = attributes.getValue( heightIndex ); + } + + // get the parameters for the get width and height methods + String[] parameters = null; + if (defaultWidth != null && defaultHeight != null) { + parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + defaultWidth + "'", "'" + defaultHeight + "'"}; + } else if (defaultWidth != null) { + parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + defaultWidth + "'"}; + } else if (defaultHeight != null) { + parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + defaultHeight + "'"}; + } + + if ( defaultWidth != null ) + { newWith = formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY, IImageRegistry.GET_WIDTH_METHOD, - IImageRegistry.IMAGE_INFO, "'" + defaultWidth + "'" ); + parameters ); } - int heightIndex = attributes.getIndex( SVG_NS, HEIGHT_ATTR ); - if ( heightIndex != -1 ) + if ( defaultHeight != null ) { - String defaultHeight = attributes.getValue( heightIndex ); newHeight = formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY, IImageRegistry.GET_HEIGHT_METHOD, - IImageRegistry.IMAGE_INFO, "'" + defaultHeight + "'" ); + parameters ); } + if ( newWith != null || newHeight != null ) { AttributesImpl attr = toAttributesImpl( attributes ); diff --git a/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTPreprocessorImageWithFreemarkerTestCase.java b/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTPreprocessorImageWithFreemarkerTestCase.java index 26364b66d..27233c33c 100644 --- a/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTPreprocessorImageWithFreemarkerTestCase.java +++ b/integrationtests/fr.opensagres.xdocreport.core.test/src/test/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTPreprocessorImageWithFreemarkerTestCase.java @@ -110,9 +110,9 @@ public void testImageWithSimpleField() + "" + + "svg:height=\"${___ImageRegistry.getHeight(___imageInfo,'21pt','22.51pt')}\" " + "draw:z-index=\"0\">" + "" + + "svg:height=\"${___ImageRegistry.getHeight(___imageInfo,'21pt','22.51pt')}\" " + "draw:z-index=\"0\">" + "" + + "svg:height=\"${___ImageRegistry.getHeight($___imageInfo,'21pt','22.51pt')}\" " + "draw:z-index=\"0\">" + "" + + "svg:height=\"${___ImageRegistry.getHeight($___imageInfo,'21pt','22.51pt')}\" " + "draw:z-index=\"0\">" + " Date: Mon, 29 Aug 2022 14:51:32 +0300 Subject: [PATCH 6/6] Parse size for ODT as point unit --- .../xdocreport/document/odt/images/ODTImageRegistry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/images/ODTImageRegistry.java b/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/images/ODTImageRegistry.java index 973786488..6449ee421 100644 --- a/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/images/ODTImageRegistry.java +++ b/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/images/ODTImageRegistry.java @@ -82,7 +82,7 @@ public Float getSize( String sizeAsDxa ) //TODO parse string containing unit information like "0.582cm" try{ - float sizeAsPixel = Float.parseFloat(sizeAsDxa); + float sizeAsPixel = Float.parseFloat(sizeAsDxa.split(POINT_UNIT)[0]); sizeAsPixel = (sizeAsPixel / 0.75f) ; return Float.valueOf(sizeAsPixel); }catch(NumberFormatException e){