Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 473fed7a200f98c9f89ba61ea500084b5174b54c (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
 * Copyright (c) 2016 Florian Thienel and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 		Florian Thienel - initial API and implementation
 *******************************************************************************/
package org.eclipse.vex.core.internal.boxes;

import java.net.URL;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.vex.core.internal.core.Graphics;
import org.eclipse.vex.core.internal.core.Length;
import org.eclipse.vex.core.internal.core.Point;
import org.eclipse.vex.core.internal.core.Rectangle;

public class Image extends BaseBox implements IInlineBox {

	private IBox parent;
	private int top;
	private int left;
	private int width;
	private int height;
	private int maxWidth;
	private LineWrappingRule lineWrappingAtStart;
	private LineWrappingRule lineWrappingAtEnd;

	private URL imageUrl;
	private Length preferredWidth;
	private Length preferredHeight;

	private org.eclipse.vex.core.internal.core.Image image; // TODO use a cache for the actual image data

	private boolean layoutValid;

	@Override
	public void setParent(final IBox parent) {
		this.parent = parent;
	}

	@Override
	public IBox getParent() {
		return parent;
	}

	@Override
	public int getAbsoluteTop() {
		if (parent == null) {
			return top;
		}
		return parent.getAbsoluteTop() + top;
	}

	@Override
	public int getAbsoluteLeft() {
		if (parent == null) {
			return left;
		}
		return parent.getAbsoluteLeft() + left;
	}

	@Override
	public int getTop() {
		return top;
	}

	@Override
	public int getLeft() {
		return left;
	}

	@Override
	public void setPosition(final int top, final int left) {
		this.top = top;
		this.left = left;
	}

	@Override
	public int getWidth() {
		return width;
	}

	@Override
	public int getHeight() {
		return height;
	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle(left, top, width, height);
	}

	@Override
	public int getBaseline() {
		return height;
	}

	@Override
	public int getMaxWidth() {
		return maxWidth;
	}

	@Override
	public void setMaxWidth(final int maxWidth) {
		this.maxWidth = maxWidth;
		layoutValid = false;
	}

	@Override
	public int getInvisibleGapAtStart(final Graphics graphics) {
		return 0;
	}

	@Override
	public int getInvisibleGapAtEnd(final Graphics graphics) {
		return 0;
	}

	@Override
	public LineWrappingRule getLineWrappingAtStart() {
		return lineWrappingAtStart;
	}

	public void setLineWrappingAtStart(final LineWrappingRule wrappingRule) {
		lineWrappingAtStart = wrappingRule;
	}

	@Override
	public LineWrappingRule getLineWrappingAtEnd() {
		return lineWrappingAtEnd;
	}

	public void setLineWrappingAtEnd(final LineWrappingRule wrappingRule) {
		lineWrappingAtEnd = wrappingRule;
	}

	@Override
	public boolean requiresSplitForLineWrapping() {
		return lineWrappingAtStart == LineWrappingRule.REQUIRED || lineWrappingAtEnd == LineWrappingRule.REQUIRED;
	}

	public URL getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(final URL imageUrl) {
		this.imageUrl = imageUrl;
		layoutValid = false;
	}

	public Length getPreferredWidth() {
		return preferredWidth;
	}

	public void setPreferredWidth(final Length preferredWidth) {
		this.preferredWidth = preferredWidth;
		layoutValid = false;
	}

	public Length getPreferredHeight() {
		return preferredHeight;
	}

	public void setPreferredHeight(final Length preferredHeight) {
		this.preferredHeight = preferredHeight;
		layoutValid = false;
	}

	@Override
	public void accept(final IBoxVisitor visitor) {
		visitor.visit(this);
	}

	@Override
	public <T> T accept(final IBoxVisitorWithResult<T> visitor) {
		return visitor.visit(this);
	}

	@Override
	public void layout(final Graphics graphics) {
		if (layoutValid) {
			return;
		}

		image = graphics.getImage(imageUrl);
		final Point dimensions = calculateActualDimensions();

		width = dimensions.getX();
		height = dimensions.getY();

		layoutValid = true;
	}

	private Point calculateActualDimensions() {
		final int width = preferredWidth == null ? 0 : preferredWidth.get(maxWidth);
		final int height = preferredHeight == null ? 0 : preferredHeight.get(image.getHeight());
		if (width != 0 && height != 0) {
			return new Point(width, height);
		}
		if (width == 0 && height != 0) {
			return new Point(scale(image.getWidth(), image.getHeight(), height), height);
		}
		if (width != 0 && height == 0) {
			return new Point(width, scale(image.getHeight(), image.getWidth(), width));
		}
		return ensureMaxWidthNotExceeded(new Point(image.getWidth(), image.getHeight()));
	}

	private Point ensureMaxWidthNotExceeded(final Point dimensions) {
		if (maxWidth > 0 && dimensions.getX() > maxWidth) {
			return new Point(maxWidth, scale(dimensions.getY(), dimensions.getX(), maxWidth));
		}
		return dimensions;
	}

	private static int scale(final int opposite, final int current, final int scaled) {
		return Math.round(1f * scaled / current * opposite);
	}

	@Override
	public Collection<IBox> reconcileLayout(final Graphics graphics) {
		final int oldHeight = height;
		final int oldWidth = width;

		layout(graphics);

		if (oldHeight != height || oldWidth != width) {
			return Collections.singleton(getParent());
		} else {
			return NOTHING_INVALIDATED;
		}
	}

	@Override
	public void paint(final Graphics graphics) {
		graphics.drawImage(image, 0, 0, width, height);
	}

	@Override
	public boolean canJoin(final IInlineBox other) {
		return false;
	}

	@Override
	public boolean join(final IInlineBox other) {
		return false;
	}

	@Override
	public boolean canSplit() {
		return false;
	}

	@Override
	public IInlineBox splitTail(final Graphics graphics, final int headWidth, final boolean force) {
		throw new UnsupportedOperationException("Image cannot be split!");
	}

}

Back to the top