Skip to content

Commit

Permalink
Background image with new option of dpi scaling and BIRT-properties #…
Browse files Browse the repository at this point in the history
…1255 (#1256)

* Background image, enhancement to support image dpi for default scaling
and implementation of the BIRT-properties background-height &
-width #1255

* Removed dependencies of designer-classes

* Added scaled calculation of background images with a set Dimension and
an auto-Dimension
  • Loading branch information
speckyspooky committed Apr 15, 2023
1 parent ceb1dde commit 63f06dd
Show file tree
Hide file tree
Showing 37 changed files with 3,430 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editpolicies.ReportElementResizablePolicy;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.figures.IReportElementFigure;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.figures.ReportElementFigure;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.figures.TableFigure;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.handles.AbstractGuideHandle;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.handles.IGuideFeedBackHost;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.tools.ReportElementDragTracker;
Expand All @@ -45,6 +46,7 @@
import org.eclipse.birt.report.designer.util.ColorManager;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.designer.util.ImageManager;
import org.eclipse.birt.report.designer.util.MetricUtility;
import org.eclipse.birt.report.engine.css.engine.value.css.CSSValueConstants;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.MasterPageHandle;
Expand All @@ -55,6 +57,7 @@
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.metadata.DimensionValue;
import org.eclipse.birt.report.model.api.util.ColorUtil;
import org.eclipse.birt.report.model.api.util.DimensionUtil;
import org.eclipse.birt.report.model.api.util.URIUtil;
import org.eclipse.birt.report.model.elements.interfaces.IStyleModel;
import org.eclipse.draw2d.IFigure;
Expand Down Expand Up @@ -690,17 +693,67 @@ protected void refreshBackgroundImage(DesignElementHandle handle) {
// Should not be ExceptionHandler.handle(e), see SCR#73730
image = null;
}

if (image == null) {
figure.setImage(null);
return;
}
int dpi = getImageDPI(backGroundImage);

int dpi = getImageDPI(backGroundImage);
if (figure instanceof ReportElementFigure) {
((ReportElementFigure) figure).setBackgroundImageDPI(dpi);
} else if (figure instanceof TableFigure) {
((TableFigure) figure).setBackgroundImageDPI(dpi);
}

int pxBackgroundHeight = 0;
int pxBackgroundWidth = 0;
double percentageHeight = 1d;
double percentageWidth = 1d;

// calculate the background image height dimension
String propertyValue = handle.getStringProperty(IStyleModel.BACKGROUND_SIZE_HEIGHT);
if (propertyValue != null && !DesignChoiceConstants.BACKGROUND_SIZE_AUTO.equals(propertyValue)
&& !DesignChoiceConstants.BACKGROUND_SIZE_COVER.equals(propertyValue)
&& !DesignChoiceConstants.BACKGROUND_SIZE_CONTAIN.equals(propertyValue)) {

if (propertyValue.endsWith("%")) {
percentageHeight = Double.parseDouble(propertyValue.replace("%", "")) / 100;
} else {
DimensionValue propertyBackgroundHeight = (DimensionValue) handle
.getProperty(IStyleModel.BACKGROUND_SIZE_HEIGHT);

if (propertyBackgroundHeight.getUnits().equals(DesignChoiceConstants.UNITS_PX)) {
pxBackgroundHeight = (int) propertyBackgroundHeight.getMeasure();
} else {
DimensionValue backgroundHeight = DimensionUtil.convertTo(propertyBackgroundHeight.getMeasure(),
propertyBackgroundHeight.getUnits(), DesignChoiceConstants.UNITS_IN);
pxBackgroundHeight = (int) MetricUtility.inchToPixel(backgroundHeight.getMeasure());
}
}
}
figure.setImage(image);

// calculate the background image width dimension
propertyValue = handle.getStringProperty(IStyleModel.BACKGROUND_SIZE_WIDTH);
if (propertyValue != null && !DesignChoiceConstants.BACKGROUND_SIZE_AUTO.equals(propertyValue)
&& !DesignChoiceConstants.BACKGROUND_SIZE_COVER.equals(propertyValue)
&& !DesignChoiceConstants.BACKGROUND_SIZE_CONTAIN.equals(propertyValue)) {

if (propertyValue.endsWith("%")) {
percentageWidth = Double.parseDouble(propertyValue.replace("%", "")) / 100;
} else {
DimensionValue propertyBackgroundWidth = (DimensionValue) handle
.getProperty(IStyleModel.BACKGROUND_SIZE_WIDTH);

if (propertyBackgroundWidth.getUnits().equals(DesignChoiceConstants.UNITS_PX)) {
pxBackgroundWidth = (int) propertyBackgroundWidth.getMeasure();
} else {
DimensionValue backgroundWidth = DimensionUtil.convertTo(propertyBackgroundWidth.getMeasure(),
propertyBackgroundWidth.getUnits(), DesignChoiceConstants.UNITS_IN);
pxBackgroundWidth = (int) MetricUtility.inchToPixel(backgroundWidth.getMeasure());
}
}
}
figure.setImage(image, pxBackgroundHeight, pxBackgroundWidth, percentageHeight, percentageWidth);

Object[] backGroundPosition = getBackgroundPosition(handle);
int backGroundRepeat = getBackgroundRepeat(handle);
Expand Down Expand Up @@ -954,6 +1007,11 @@ protected int getBackgroundRepeat(DesignElementHandle handle) {
return getModelAdapter().getBackgroundRepeat(handle);
}

protected int getBackgroundHeight(DesignElementHandle handle) {
return 0;
// return getModelAdapter().getBackgroundImageHeight(handle, getPreferredSize(), null);
}

protected boolean isFigureLeft(Request request) {
if (!(request instanceof SelectionRequest)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ public interface IReportElementFigure {
*/
void setImage(Image image);

/**
* Sets the Image that this ImageFigure displays.
*
* @param image The Image to be displayed. It can be
* <code>null</code>.
* @param backGroundImageHeight height of the image
* @param backGroundImageWidth width of the image
*/
public void setImage(Image image, int backGroundImageHeight, int backGroundImageWidth);

/**
* Sets the Image that this ImageFigure displays.
*
* @param image The Image to be displayed. It can be
* <code>null</code>.
* @param backGroundImageHeight height of the image
* @param backGroundImageWidth width of the image
* @param percentageHeight percentage of height of the image to base 1.0
* @param percentageWidth percentage of width of the image to base 1.0
*/
public void setImage(Image image, int backGroundImageHeight, int backGroundImageWidth, double percentageHeight,
double percentageWidth);

/**
* Sets the margin of current figure.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,32 @@ public class ReportElementFigure extends Figure implements IReportElementFigure,

private Dimension size = new Dimension();

private Dimension propertySize = new Dimension();

private double percentageHeight = 1;

private double percentageWidth = 1;

private Rectangle clip;

private static final Rectangle OLD_CLIP = new Rectangle();

private int backgroundImageDPI = 0;

/**
* Get the background image dpi
*
* @return get the background image dpi
*/
public int getBackgroundImageDPI() {
return backgroundImageDPI;
}

/**
* Set the background image dpi
*
* @param backgroundImageDPI background image dpi
*/
public void setBackgroundImageDPI(int backgroundImageDPI) {
this.backgroundImageDPI = backgroundImageDPI;
}
Expand Down Expand Up @@ -100,6 +116,11 @@ public Image getImage() {
return img;
}

/**
* Set page clip
*
* @param clip rectangle to clip page
*/
public void setPageClip(Rectangle clip) {
this.clip = clip;
}
Expand Down Expand Up @@ -181,9 +202,9 @@ protected void paintFigure(Graphics graphics) {
}
}

ArrayList xyList = createImageList(x, y);
ArrayList<?> xyList = createImageList(x, y);

Iterator iter = xyList.iterator();
Iterator<?> iter = xyList.iterator();
Dimension imageSize = new Rectangle(image.getBounds()).getSize();
while (iter.hasNext()) {
Point point = (Point) iter.next();
Expand All @@ -202,10 +223,10 @@ protected void paintFigure(Graphics graphics) {
* @param y the y-cordinator of the base image.
* @return the list of all the images to be displayed.
*/
private ArrayList createImageList(int x, int y) {
private ArrayList<Point> createImageList(int x, int y) {
Rectangle area = getBounds();

ArrayList yList = new ArrayList();
ArrayList<Point> yList = new ArrayList<Point>();

if ((repeat & ImageConstants.REPEAT_Y) == 0) {
yList.add(new Point(x, y));
Expand All @@ -223,11 +244,11 @@ private ArrayList createImageList(int x, int y) {
}
}

ArrayList xyList = new ArrayList();
ArrayList<Point> xyList = new ArrayList<Point>();

Iterator iter = yList.iterator();
Iterator<Point> iter = yList.iterator();
while (iter.hasNext()) {
Point point = (Point) iter.next();
Point point = iter.next();

if ((repeat & ImageConstants.REPEAT_X) == 0) {
xyList.add(point);
Expand Down Expand Up @@ -308,23 +329,99 @@ public void setRepeat(int flag) {
*/
@Override
public void setImage(Image image) {
if (img == image) {
this.setImage(image, 0, 0);
}

/**
* Sets the Image that this ImageFigure displays.
*
* @param image The Image to be displayed. It can be
* <code>null</code>.
* @param backGroundImageHeight height of the image
* @param backGroundImageWidth width of the image
*/
@Override
public void setImage(Image image, int backGroundImageHeight, int backGroundImageWidth) {
this.setImage(image, 0, 0, 1, 1);
}

/**
* Sets the Image that this ImageFigure displays.
*
* @param image The Image to be displayed. It can be
* <code>null</code>.
* @param backGroundImageHeight height of the image
* @param backGroundImageWidth width of the image
* @param percentageHeight percentage of height of the image to base 1.0
* @param percentageWidth percentage of width of the image to base 1.0
*/
@Override
public void setImage(Image image, int backGroundImageHeight, int backGroundImageWidth, double percentageHeight,
double percentageWidth) {
if (img == image && propertySize.height == backGroundImageHeight && propertySize.width == backGroundImageWidth
&& this.percentageHeight == percentageHeight && this.percentageWidth == percentageWidth) {
return;
}
img = image;
if (img != null) {
if (backgroundImageDPI > 0) {
propertySize.height = backGroundImageHeight;
propertySize.width = backGroundImageWidth;
this.percentageHeight = percentageHeight;
this.percentageWidth = percentageWidth;

if (backgroundImageDPI > 0 && backGroundImageHeight <= 0 && backGroundImageWidth > 0) {

double inch = 1d;

// scaling factor of correct image relation based on original image width
inch = ((double) image.getBounds().width) / backgroundImageDPI;
int originalWidth = (int) MetricUtility.inchToPixel(inch);
double scaleFactor = (double) backGroundImageWidth / originalWidth;

inch = ((double) image.getBounds().height) / backgroundImageDPI;
size.height = (int) (MetricUtility.inchToPixel(inch) * scaleFactor);
size.width = backGroundImageWidth;

} else if (backgroundImageDPI > 0 && backGroundImageWidth <= 0 && backGroundImageHeight > 0) {

double inch = 1d;

// scaling factor of correct image relation based on original image height
inch = ((double) image.getBounds().height) / backgroundImageDPI;
int originalHeight = (int) MetricUtility.inchToPixel(inch);
double scaleFactor = (double) backGroundImageHeight / originalHeight;

inch = ((double) image.getBounds().width) / backgroundImageDPI;
size.width = (int) (MetricUtility.inchToPixel(inch) * scaleFactor);
size.height = backGroundImageHeight;

} else if (backgroundImageDPI > 0 && (backGroundImageHeight <= 0 && backGroundImageWidth <= 0)) {

double inch = ((double) image.getBounds().width) / backgroundImageDPI;
size.width = (int) MetricUtility.inchToPixel(inch);

inch = ((double) image.getBounds().height) / backgroundImageDPI;
size.height = (int) MetricUtility.inchToPixel(inch);

} else if (backGroundImageHeight > 0 && backGroundImageWidth > 0) {

size.height = backGroundImageHeight;
size.width = backGroundImageWidth;

} else {
size = new Rectangle(image.getBounds()).getSize();
}
} else {
size = new Dimension();
}
// auto scaling of percentage if one percentage is set and the image size is unset
if (percentageHeight != 1.0 && percentageWidth == 1.0 && backGroundImageWidth == 0) {
percentageWidth = percentageHeight;
} else if (percentageWidth != 1.0 && percentageHeight == 1.0 && backGroundImageHeight == 0) {
percentageHeight = percentageWidth;
}
size.height = (int) (size.height * percentageHeight);
size.width = (int) (size.width * percentageWidth);
revalidate();
repaint();
}
Expand Down Expand Up @@ -359,7 +456,7 @@ public void setMargin(Insets newMargin) {
/**
* Returns the margin of current figure.
*
* @return
* @return margin of the element
*/
@Override
public Insets getMargin() {
Expand Down Expand Up @@ -399,7 +496,10 @@ public Dimension getFixMinimumSize(int w, int h) {
}

/**
* @param backGroundImageWidth
* Set the Background image size
*
* @param backGroundImageWidth width of the background image
* @param backGroundImageHeight height of background image
*/
public void setBackGroundImageSize(int backGroundImageWidth, int backGroundImageHeight) {

Expand Down

0 comments on commit 63f06dd

Please sign in to comment.