Skip to content

Commit 83a1262

Browse files
Added feature for allowing the user to fit an external image in the template keeping the aspect ratio (#574)
* Added feature for allowing the user to fit an external image in the template keeping the aspect ratio * Fixed possible null value when retrieving image template size * Added override for image registry get width and height methods * Cache new size for the replacement image * Added image fit support for ODT * Parse size for ODT as point unit
1 parent 02ca6b5 commit 83a1262

File tree

12 files changed

+217
-40
lines changed

12 files changed

+217
-40
lines changed

document/fr.opensagres.xdocreport.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/preprocessor/sax/DocXBufferedDocumentContentHandler.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,48 @@ public boolean doStartElement(String uri, String localName, String name,
182182
// modify "cx" and "cy" attribute with image script (Velocity,
183183
// Freemarker)
184184
// <wp:extent
185+
// cx="${imageRegistry.getWidth(___imageInfo, '1262380', '1352550')}"
186+
// cy="${imageRegistry.getHeight(___imageInfo, '1262380', '1352550')}" />
187+
// "cx" and "cy" attributes can also be modified as follows
188+
// <wp:extent
185189
// cx="${imageRegistry.getWidth(___imageInfo, '1262380')}"
186190
// cy="${imageRegistry.getHeight(___imageInfo, '1352550')}" />
187191
String newCX = null;
188192
String newCY = null;
193+
String oldCX = null;
194+
String oldCY = null;
189195
int cxIndex = attributes.getIndex(CX_ATTR);
190196
if (cxIndex != -1) {
191-
String oldCX = attributes.getValue(cxIndex);
197+
oldCX = attributes.getValue(cxIndex);
198+
}
199+
int cyIndex = attributes.getIndex(CY_ATTR);
200+
if (cyIndex != -1) {
201+
oldCY = attributes.getValue(cyIndex);
202+
}
203+
204+
// get the parameters for the get width and height methods
205+
String[] parameters = null;
206+
if (oldCX != null && oldCY != null) {
207+
parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + oldCX + "'", "'" + oldCY + "'"};
208+
} else if (oldCX != null) {
209+
parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + oldCX + "'"};
210+
} else if (oldCY != null) {
211+
parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + oldCY + "'"};
212+
}
213+
214+
if (oldCX != null) {
192215
newCX = formatter.getFunctionDirective(
193216
TemplateContextHelper.IMAGE_REGISTRY_KEY,
194217
IImageRegistry.GET_WIDTH_METHOD,
195-
IImageRegistry.IMAGE_INFO, "'" + oldCX + "'");
218+
parameters);
196219
}
197-
int cyIndex = attributes.getIndex(CY_ATTR);
198-
if (cyIndex != -1) {
199-
String oldCY = attributes.getValue(cyIndex);
220+
if (oldCY != null) {
200221
newCY = formatter.getFunctionDirective(
201222
TemplateContextHelper.IMAGE_REGISTRY_KEY,
202223
IImageRegistry.GET_HEIGHT_METHOD,
203-
IImageRegistry.IMAGE_INFO, "'" + oldCY + "'");
224+
parameters);
204225
}
226+
205227
if (newCX != null || newCY != null) {
206228
AttributesImpl attr = toAttributesImpl(attributes);
207229
if (newCX != null) {

document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/images/ODTImageRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Float getSize( String sizeAsDxa )
8282

8383
//TODO parse string containing unit information like "0.582cm"
8484
try{
85-
float sizeAsPixel = Float.parseFloat(sizeAsDxa);
85+
float sizeAsPixel = Float.parseFloat(sizeAsDxa.split(POINT_UNIT)[0]);
8686
sizeAsPixel = (sizeAsPixel / 0.75f) ;
8787
return Float.valueOf(sizeAsPixel);
8888
}catch(NumberFormatException e){

document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/preprocessor/ODTBufferedDocumentContentHandler.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,44 @@ else if ( isDrawFrame( uri, localName, name ) )
195195
//
196196
String newWith = null;
197197
String newHeight = null;
198+
String defaultWidth = null;
199+
String defaultHeight = null;
198200
int widthIndex = attributes.getIndex( SVG_NS, WIDTH_ATTR );
199201
if ( widthIndex != -1 )
200202
{
201-
String defaultWidth = attributes.getValue( widthIndex );
203+
defaultWidth = attributes.getValue( widthIndex );
204+
}
205+
int heightIndex = attributes.getIndex( SVG_NS, HEIGHT_ATTR );
206+
if ( heightIndex != -1 )
207+
{
208+
defaultHeight = attributes.getValue( heightIndex );
209+
}
210+
211+
// get the parameters for the get width and height methods
212+
String[] parameters = null;
213+
if (defaultWidth != null && defaultHeight != null) {
214+
parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + defaultWidth + "'", "'" + defaultHeight + "'"};
215+
} else if (defaultWidth != null) {
216+
parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + defaultWidth + "'"};
217+
} else if (defaultHeight != null) {
218+
parameters = new String[]{IImageRegistry.IMAGE_INFO, "'" + defaultHeight + "'"};
219+
}
220+
221+
if ( defaultWidth != null )
222+
{
202223
newWith =
203224
formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY,
204225
IImageRegistry.GET_WIDTH_METHOD,
205-
IImageRegistry.IMAGE_INFO, "'" + defaultWidth + "'" );
226+
parameters );
206227
}
207-
int heightIndex = attributes.getIndex( SVG_NS, HEIGHT_ATTR );
208-
if ( heightIndex != -1 )
228+
if ( defaultHeight != null )
209229
{
210-
String defaultHeight = attributes.getValue( heightIndex );
211230
newHeight =
212231
formatter.getFunctionDirective( TemplateContextHelper.IMAGE_REGISTRY_KEY,
213232
IImageRegistry.GET_HEIGHT_METHOD,
214-
IImageRegistry.IMAGE_INFO, "'" + defaultHeight + "'" );
233+
parameters );
215234
}
235+
216236
if ( newWith != null || newHeight != null )
217237
{
218238
AttributesImpl attr = toAttributesImpl( attributes );

document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageProvider.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public abstract class AbstractImageProvider
4545

4646
private Float heightFromImageInfo;
4747

48+
private Float[] fittedImageDimensions;
49+
4850
private boolean useImageSize;
4951

5052
private boolean resize;
@@ -80,11 +82,28 @@ public void setUseImageSize( boolean useImageSize )
8082

8183
/*
8284
* (non-Javadoc)
83-
* @see fr.opensagres.xdocreport.document.images.IImageProvider#getWidth()
85+
* @see fr.opensagres.xdocreport.document.images.IImageProvider#getWidth(java.lang.Float)
8486
*/
8587
public Float getWidth(Float defaultWidth)
8688
throws IOException
8789
{
90+
return getWidth(defaultWidth, null);
91+
}
92+
93+
/*
94+
* (non-Javadoc)
95+
* @see fr.opensagres.xdocreport.document.images.IImageProvider#getWidth(java.lang.Float, java.lang.Float)
96+
*/
97+
public Float getWidth(Float defaultWidth, Float defaultHeight)
98+
throws IOException
99+
{
100+
// check if user wants to resize the image in order to fit template
101+
// replacement image aspect ratio is always kept intact
102+
if ( height == null && width == null && defaultWidth != null
103+
&& defaultHeight != null && !isUseImageSize() && isResize() )
104+
{
105+
return getDimensions(defaultWidth, defaultHeight)[0];
106+
}
88107
if ( width != null )
89108
{
90109
return width;
@@ -112,11 +131,28 @@ public Float getWidth(Float defaultWidth)
112131

113132
/*
114133
* (non-Javadoc)
115-
* @see fr.opensagres.xdocreport.document.images.IImageProvider#getHeight()
134+
* @see fr.opensagres.xdocreport.document.images.IImageProvider#getHeight(java.lang.Float)
116135
*/
117136
public Float getHeight(Float defaultHeight)
118137
throws IOException
119138
{
139+
return getHeight(null, defaultHeight);
140+
}
141+
142+
/*
143+
* (non-Javadoc)
144+
* @see fr.opensagres.xdocreport.document.images.IImageProvider#getHeight(java.lang.Float, java.lang.Float)
145+
*/
146+
public Float getHeight(Float defaultWidth, Float defaultHeight)
147+
throws IOException
148+
{
149+
// check if user wants to resize the image in order to fit template
150+
// replacement image aspect ratio is always kept intact
151+
if ( height == null && width == null && defaultWidth != null
152+
&& defaultHeight != null && !isUseImageSize() && isResize() )
153+
{
154+
return getDimensions(defaultWidth, defaultHeight)[1];
155+
}
120156
if ( height != null )
121157
{
122158
return height;
@@ -142,6 +178,33 @@ public Float getHeight(Float defaultHeight)
142178
return heightFromImageInfo = new Float( getImageInfo().getHeight() );
143179
}
144180

181+
/**
182+
* Compute the new dimensions of the replacement image in order to fit the template.
183+
*
184+
* @param defaultWidth template width
185+
* @param defaultHeight template height
186+
* @return new dimensions of the image
187+
* @throws IOException
188+
*/
189+
private Float[] getDimensions(Float defaultWidth, Float defaultHeight)
190+
throws IOException
191+
{
192+
if ( fittedImageDimensions != null )
193+
{
194+
return fittedImageDimensions;
195+
}
196+
197+
float realImageHeight = getImageInfo().getHeight();
198+
float realImageWidth = getImageInfo().getWidth();
199+
float scale = Math.min(defaultWidth / realImageWidth, defaultHeight / realImageHeight);
200+
201+
float finalImageHeight = realImageHeight * scale;
202+
float finalImageWidth = realImageWidth * scale;
203+
204+
fittedImageDimensions = new Float[]{finalImageWidth, finalImageHeight};
205+
return fittedImageDimensions;
206+
}
207+
145208
/*
146209
* (non-Javadoc)
147210
* @see fr.opensagres.xdocreport.document.images.IImageProvider#setWidth(java .lang.Float)

document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/AbstractImageRegistry.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,19 @@ public String getPath( ImageProviderInfo info, String defaultPath )
257257

258258
public String getWidth( ImageProviderInfo info, String defaultWidth )
259259
throws IOException
260+
{
261+
return getWidth(info, defaultWidth, null);
262+
}
263+
264+
public String getWidth( ImageProviderInfo info, String defaultWidth, String defaultHeight )
265+
throws IOException
260266
{
261267
if ( info.isKeepImageTemplate() )
262268
{
263269
return defaultWidth;
264270
}
265271
IImageProvider imageProvider = info.getImageProvider();
266-
Float width = imageProvider.getWidth(getSize(defaultWidth));
272+
Float width = imageProvider.getWidth(getSize(defaultWidth), getSize(defaultHeight));
267273
if ( width != null )
268274
{
269275
return getSize( width );
@@ -273,13 +279,19 @@ public String getWidth( ImageProviderInfo info, String defaultWidth )
273279

274280
public String getHeight( ImageProviderInfo info, String defaultHeight )
275281
throws IOException
282+
{
283+
return getHeight(info, null, defaultHeight);
284+
}
285+
286+
public String getHeight( ImageProviderInfo info, String defaultWidth, String defaultHeight )
287+
throws IOException
276288
{
277289
if ( info.isKeepImageTemplate() )
278290
{
279291
return defaultHeight;
280292
}
281293
IImageProvider imageProvider = info.getImageProvider();
282-
Float height = imageProvider.getHeight(getSize(defaultHeight));
294+
Float height = imageProvider.getHeight(getSize(defaultWidth), getSize(defaultHeight));
283295
if ( height != null )
284296
{
285297
return getSize( height );

document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageProvider.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,23 @@ void write( OutputStream outputStream )
5656
* Returns the width image with pixel unit.
5757
*
5858
* @param defaultWidth as pixel
59+
* @param defaultHeight as pixel
5960
* @return
6061
* @throws IOException
6162
*/
62-
Float getWidth(Float defaultWidth)
63+
Float getWidth(Float defaultWidth, Float defaultHeight)
6364
throws IOException;
6465

66+
/**
67+
* Returns the width image with pixel unit.
68+
*
69+
* @param defaultWidth as pixel
70+
* @return
71+
* @throws IOException
72+
*/
73+
Float getWidth(Float defaultWidth)
74+
throws IOException;
75+
6576
/**
6677
* Set the width image with pixel unit.
6778
*
@@ -71,14 +82,25 @@ Float getWidth(Float defaultWidth)
7182

7283
/**
7384
* Returns the height image with pixel unit.
74-
*
85+
*
86+
* @param defaultWidth as pixel
7587
* @param defaultHeight as pixel
7688
* @return
7789
* @throws IOException
7890
*/
79-
Float getHeight(Float defaultHeight)
91+
Float getHeight(Float defaultWidth, Float defaultHeight)
8092
throws IOException;
8193

94+
/**
95+
* Returns the height image with pixel unit.
96+
*
97+
* @param defaultHeight as pixel
98+
* @return
99+
* @throws IOException
100+
*/
101+
Float getHeight(Float defaultHeight)
102+
throws IOException;
103+
82104
/**
83105
* Set the height image with pixel unit.
84106
*

document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/images/IImageRegistry.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void postProcess()
8080

8181
/**
8282
* Returns the width of the image provider if not null and the given defaultWidth otherwise.
83-
*
83+
*
8484
* @param imageProvider
8585
* @param defaultWidth
8686
* @return
@@ -90,13 +90,37 @@ String getWidth( ImageProviderInfo info, String defaultWidth )
9090
throws IOException;
9191

9292
/**
93-
* Returns the height of the image provider if not null and the given defaultHeight otherwise.
93+
* Returns the width of the image provider if not null and the given defaultWidth otherwise.
9494
*
9595
* @param imageProvider
9696
* @param defaultWidth
97+
* @param defaultHeight
98+
* @return
99+
* @throws IOException
100+
*/
101+
String getWidth( ImageProviderInfo info, String defaultWidth, String defaultHeight )
102+
throws IOException;
103+
104+
/**
105+
* Returns the height of the image provider if not null and the given defaultHeight otherwise.
106+
*
107+
* @param imageProvider
108+
* @param defaultHeight
97109
* @return
98110
* @throws IOException
99111
*/
100112
String getHeight( ImageProviderInfo info, String defaultHeight )
101113
throws IOException;
114+
115+
/**
116+
* Returns the height of the image provider if not null and the given defaultHeight otherwise.
117+
*
118+
* @param imageProvider
119+
* @param defaultWidth
120+
* @param defaultHeight
121+
* @return
122+
* @throws IOException
123+
*/
124+
String getHeight( ImageProviderInfo info, String defaultWidth, String defaultHeight )
125+
throws IOException;
102126
}

0 commit comments

Comments
 (0)