Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ad660300f49f5cdb6ed2d44686078170d40a8229 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.eclipse.wst.xml.vex.core.internal.layout;

import java.net.URL;

import org.eclipse.wst.xml.vex.core.internal.core.Image;
import org.eclipse.wst.xml.vex.core.internal.core.Point;
import org.eclipse.wst.xml.vex.core.internal.css.Styles;
import org.eclipse.wst.xml.vex.core.internal.dom.Element;

public class ImageBox extends AbstractInlineBox {

	private final Image image;

	public static ImageBox create(final Element element, final LayoutContext context, final int maxWidth) {
		if (element == null)
			return null;
		final Styles styles = context.getStyleSheet().getStyles(element);
		final URL imageUrl = context.resolveUrl(element.getBaseURI(), styles.getBackgroundImage());
		if (imageUrl == null)
			return null;

		final Image image = context.getGraphics().getImage(imageUrl);
		final Point imageDimensions = getImageDimensions(image, styles);
		final int width = Math.min(imageDimensions.getX(), maxWidth);
		final int height = scale(imageDimensions.getY(), imageDimensions.getX(), width);

		final ImageBox result = new ImageBox(image);
		result.setWidth(width);
		result.setHeight(height);
		return result;
	}

	private static Point getImageDimensions(final Image image, final Styles styles) {
		int styleWidth = styles.getElementWidth().get(image.getWidth());
		int styleHeight = styles.getElementHeight().get(image.getHeight());
		if (styleWidth != 0 && styleHeight != 0)
			return new Point(styleWidth, styleHeight);
		if (styleWidth == 0 && styleHeight != 0)
			return new Point(scale(image.getWidth(), image.getHeight(), styleHeight), styleHeight);
		if (styleWidth != 0 && styleHeight == 0)
			return new Point(styleWidth, scale(image.getHeight(), image.getWidth(), styleWidth));
		return new Point(image.getWidth(), image.getHeight());
	}
	
	private static int scale(int opposite, int current, int scaled) {
		return Math.round(1f * scaled / current * opposite);
	}

	public static ImageBox createWithHeight(final Element element, final LayoutContext context, final int maxHeight) {
		if (element == null)
			return null;
		final URL imageUrl = context.resolveUrl(element.getBaseURI(), context.getStyleSheet().getStyles(element).getBackgroundImage());
		if (imageUrl == null)
			return null;

		final Image image = context.getGraphics().getImage(imageUrl);
		final int height = Math.min(image.getHeight(), maxHeight);
		final int width = scale(image.getWidth(), image.getHeight(), height);

		final ImageBox result = new ImageBox(image);
		result.setWidth(width);
		result.setHeight(height);
		return result;
	}

	public ImageBox(final Image image) {
		this.image = image;
		setWidth(image.getWidth());
		setHeight(image.getHeight());
	}

	@Override
	public void paint(final LayoutContext context, final int x, final int y) {
		if (skipPaint(context, x, y))
			return;
		context.getGraphics().drawImage(image, x, y, getWidth(), getHeight());
		super.paint(context, x, y);
	}

	public int getBaseline() {
		return 0;
	}

	@Override
	public void alignOnBaseline(final int baseline) {
		setY(0);
	}

	public boolean isEOL() {
		return false;
	}

	public Pair split(final LayoutContext context, final int maxWidth, final boolean force) {
		return new Pair(null, this);
	}
}

Back to the top