Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature for allowing the user to fit an external image in the template keeping the aspect ratio #574

Merged
merged 7 commits into from
Jan 25, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,48 @@ public boolean doStartElement(String uri, String localName, String name,
// modify "cx" and "cy" attribute with image script (Velocity,
// Freemarker)
// <wp:extent
// cx="${imageRegistry.getWidth(___imageInfo, '1262380', '1352550')}"
// cy="${imageRegistry.getHeight(___imageInfo, '1262380', '1352550')}" />
// "cx" and "cy" attributes can also be modified as follows
// <wp:extent
// cx="${imageRegistry.getWidth(___imageInfo, '1262380')}"
// cy="${imageRegistry.getHeight(___imageInfo, '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);
}

// 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 + "'");
parameters);
}
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 + "'");
parameters);
}

if (newCX != null || newCY != null) {
AttributesImpl attr = toAttributesImpl(attributes);
if (newCX != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public abstract class AbstractImageProvider

private Float heightFromImageInfo;

private Float[] fittedImageDimensions;

private boolean useImageSize;

private boolean resize;
Expand Down Expand Up @@ -80,11 +82,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;
Expand Down Expand Up @@ -112,11 +131,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;
Expand All @@ -142,6 +178,33 @@ 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
{
if ( fittedImageDimensions != null )
{
return fittedImageDimensions;
}

float realImageHeight = getImageInfo().getHeight();
float realImageWidth = getImageInfo().getWidth();
float scale = Math.min(defaultWidth / realImageWidth, defaultHeight / realImageHeight);

float finalImageHeight = realImageHeight * scale;
float finalImageWidth = realImageWidth * scale;

fittedImageDimensions = new Float[]{finalImageWidth, finalImageHeight};
return fittedImageDimensions;
}

/*
* (non-Javadoc)
* @see fr.opensagres.xdocreport.document.images.IImageProvider#setWidth(java .lang.Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,19 @@ public String getPath( ImageProviderInfo info, String defaultPath )

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
{
if ( info.isKeepImageTemplate() )
{
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 );
Expand All @@ -273,13 +279,19 @@ public String getWidth( ImageProviderInfo info, String 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
{
if ( info.isKeepImageTemplate() )
{
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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void postProcess()

/**
* Returns the width of the image provider if not null and the given defaultWidth otherwise.
*
*
* @param imageProvider
* @param defaultWidth
* @return
Expand All @@ -90,13 +90,37 @@ String getWidth( ImageProviderInfo info, String defaultWidth )
throws IOException;

/**
* Returns the height of the image provider if not null and the given defaultHeight otherwise.
* Returns the width of the image provider if not null and the given defaultWidth otherwise.
*
* @param imageProvider
* @param defaultWidth
* @param defaultHeight
* @return
* @throws IOException
*/
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.
*
* @param imageProvider
* @param defaultWidth
* @param defaultHeight
* @return
* @throws IOException
*/
String getHeight( ImageProviderInfo info, String defaultWidth, String defaultHeight )
throws IOException;
}
Loading
Loading