Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0a4a3520053b95ec9caec73435167c224ac4499c (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.css2.layout;

import java.util.List;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.css2.property.TextDecorationMeta;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

/**
 * @author mengbo
 */
public class TextLayoutSupport {
	private static final Logger _log = PDPlugin
			.getLogger(TextLayoutSupport.class);

	private static final String[] DELIMITERS = { "\r\n", //$NON-NLS-1$
			"\n", //$NON-NLS-1$
			"\r" //$NON-NLS-1$
	}; //$NON-NLS-1$

	static private int delimeterLength;

	/**
	 * Reuses an existing <code>TextFragmentBox</code>, or creates a new one.
	 * 
	 * @param i
	 *            the index
	 * @param fragments
	 *            the original list of fragments
	 * @return a TextFragmentBox
	 */
	// copied from TextLayout
	protected static TextFragmentBox getFragment(int i, List fragments) {
		if (fragments.size() > i) {
			return (TextFragmentBox) fragments.get(i);
		}
		TextFragmentBox box = new TextFragmentBox();
		fragments.add(box);
		return box;
	}

	/**
	 * Returns the average character width of given TextFragmentbox
	 * 
	 * @param fragment
	 *            the TextFragmentBox
	 * @return the average character width
	 */
	public static float getAverageCharWidth(TextFragmentBox fragment) {
		if (fragment._width != 0 && fragment._length != 0) {
			return fragment._width / (float) fragment._length;
		}
		return 0.0f;
	}

	// ----------------------------------------------------------------------------------------
	/**
	 * this method will create a set of TextFragment. Each fragment will offset
	 * to the original text (whole text for the text figure).
	 */
	public static void layoutNormal(FlowContext context, String text,
			List fragments, Font font, int wrappingStyle, boolean trimLeading) {
		int i = 0; // The index of the current fragment;
		int offset = 0;
		if (trimLeading) {
			offset = 1;
			text = text.substring(1);
		}

		int length = 0; // The length of the current fragment
		float prevAvgCharWidth;
		LineBox currentLine;
		TextFragmentBox fragment;

		while (text.length() > 0) {
			fragment = null;
			prevAvgCharWidth = 0f;
			fragment = getFragment(i, fragments);
			prevAvgCharWidth = getAverageCharWidth(fragment);

			// Check for newline, if it exists, call context.endLine and skip
			// over the newline
			// Exccept for first time through, don't do this.
			if (i != 0) {
				boolean changed = false;
				if (text.charAt(0) == '\r') {
					text = text.substring(1);
					changed = true;
					offset += 1;
				}
				if (text.length() != 0 && text.charAt(0) == '\n') {
					text = text.substring(1);
					changed = true;
					offset += 1;
				}
				if (changed) {
					context.endLine();
				}
			}

			fragment._offset = offset;

			// This loop is done at most twice.
			// The second time through, a context.endLine()
			// was requested, and the loop will break.
			while (true) {
				currentLine = context.getCurrentLine();
				length = FlowUtilities.setupFragmentBasedOnTextSpace(fragment,
						text, font, currentLine.getAvailableWidth(),
						prevAvgCharWidth, wrappingStyle);

				if (fragment._width <= currentLine.getAvailableWidth()
						|| !context.isCurrentLineOccupied()) {
					break;
				}
				context.endLine();
			}
			// fragment.x = context.getCurrentX();
			context.addToCurrentLine(fragment);
			text = text.substring(length);
			offset += length;
			if (text.length() > 0) {
				context.endLine();
			}
			i++;
		}

		// Remove the remaining unused fragments.
		while (i < fragments.size()) {
			fragments.remove(fragments.size() - 1);
		}
	}

	public static void layoutNoWrap(FlowContext context, String text,
			List fragments, Font font) {
		TextFragmentBox fragment;
		int i = 0;
		int offset = 0;

		while (offset < text.length()) {
			int result = nextLineBreak(text, offset);
			fragment = getFragment(i++, fragments);
			fragment._length = result - offset;
			fragment._offset = offset;
			FlowUtilities.setupFragment(fragment, font, text.substring(offset));
			context.getCurrentLine().add(fragment);
			offset = result + delimeterLength;
			if (delimeterLength != 0) {
				// in nextLineBreak we fo
				context.endLine();
			}

		}
		// Remove the remaining unused fragments.
		while (i < fragments.size()) {
			fragments.remove(i++);
		}
	}

	private static int nextLineBreak(String text, int offset) {
		int result = text.length();
		delimeterLength = 0;
		int current;
		for (int i = 0; i < DELIMITERS.length; i++) {
			current = text.indexOf(DELIMITERS[i], offset);
			if (current != -1 && current < result) {
				result = current;
				delimeterLength = DELIMITERS[i].length();
			}
		}
		return result;
	}

	public static void paintTextFigure(Graphics g, List fragments, Font font,
			int textDecoration) {
		paintTextFigure(g, fragments, font, null, textDecoration);
	}

	public static void paintTextDecoration(Graphics g, Rectangle rect,
			int textDecoration) {
		if ((textDecoration & TextDecorationMeta.UNDERLINE) != 0) {
			g.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width
					- 1, rect.y + rect.height - 1);
		}
		if ((textDecoration & TextDecorationMeta.OVERLINE) != 0) {
			g.drawLine(rect.x, rect.y + 1, rect.x + rect.width - 1, rect.y + 1);
		}
		if ((textDecoration & TextDecorationMeta.LINETHROUGH) != 0) {
			g.drawLine(rect.x, rect.y + rect.height / 2, rect.x + rect.width
					- 1, rect.y + rect.height / 2);
		}
	}

	public static void paintTextFigure(Graphics g, List fragments, Font font,
			Color color, int textDecoration) {
		// FIXME: It happens there is problem in this method's parameters. what
		// exception should be catched?
		try {
			TextFragmentBox frag;
			// XXX: adjust font. Here is not using setFont(), because that will
			// result in revalidate
			g.setFont(font);

			for (int i = 0; i < fragments.size(); i++) {
				frag = (TextFragmentBox) fragments.get(i);
				// if (!g.getClip(Rectangle.SINGLETON).intersects(frag))
				// continue;
				String draw;
				draw = frag.getTextData();

				if (color != null) {
					g.setForegroundColor(color);
				}
				g.drawText(draw, frag._x, frag._y);
				if ((textDecoration & TextDecorationMeta.UNDERLINE) != 0) {
					g.drawLine(frag._x, frag._y + frag.getHeight() - 1, frag._x
							+ frag.getWidth(), frag._y + frag.getHeight() - 1);
				}
				if ((textDecoration & TextDecorationMeta.OVERLINE) != 0) {
					g.drawLine(frag._x, frag._y, frag._x + frag.getWidth(),
							frag._y);
				}
				if ((textDecoration & TextDecorationMeta.LINETHROUGH) != 0) {
					g.drawLine(frag._x, frag._y + frag.getHeight() / 2, frag._x
							+ frag.getWidth(), frag._y + frag.getHeight() / 2);
				}

				if (Debug.DEBUG_BASELINE) {
					g.drawLine(frag._x, frag._y + frag.getAscent(), frag._x
							+ frag.getWidth(), frag._y + frag.getAscent());
				}
			}
		} catch (Exception e) {
			// "Error in text painting:"
			_log.info("TextLayoutSupport.Info.1", e); //$NON-NLS-1$
		}
	}

	/**
	 * 
	 * @param g
	 * @param fragments
	 * @param text
	 *            all the text in the Text figure.
	 * @param font
	 * @param color
	 * @param textDecoration
	 * @param start
	 * @param end
	 * @param selectionForeColor
	 * @param selectionBackColor
	 */
	public static void paintTextFigureWithSelection(Graphics g, List fragments,
			String text, Font font, Color color, int textDecoration, int start,
			int end, Color selectionForeColor, Color selectionBackColor) {
		// FIXME: It happens there is problem in this method's parameters. what
		// exception should be catched?
		try {
			TextFragmentBox frag;

			Color originalForeground = g.getForegroundColor();
			Color originalBackgroud = g.getBackgroundColor();

			// XXX: adjust font. Here is not using setFont(), because that will
			// result in revalidate
			g.setFont(font);

			for (int i = 0, n = fragments.size(); i < n; i++) {
				frag = (TextFragmentBox) fragments.get(i);

				// to make things simpler, we always draw the line using default
				// color
				if (color != null) {
					g.setForegroundColor(color);
				}

				// if (!g.getClip(Rectangle.SINGLETON).intersects(frag))
				// continue;
				String draw;
				draw = frag.getTextData();
				if (frag._offset >= end || frag._offset + frag._length <= start) {
					// we are not in selection. no need to change color
					g.drawText(draw, frag._x, frag._y);
					paintTextDecoration(g, frag.getRectangle(), textDecoration);
				} else if (frag._offset >= start
						&& frag._offset + frag._length <= end) {
					// we are fully in selection
					g.setForegroundColor(selectionForeColor);
					g.setBackgroundColor(selectionBackColor);
					g
							.fillRectangle(frag._x, frag._y, FlowUtilities
									.getTextExtents(draw, font).width, frag
									.getHeight());
					g.drawText(draw, frag._x, frag._y);
					paintTextDecoration(g, frag.getRectangle(), textDecoration);
				} else {
					// partial of the fragment's text is in selection.

					// draw the original string first
					g.drawText(draw, frag._x, frag._y);
					// then override with the selected parts.
					g.setForegroundColor(selectionForeColor);
					g.setBackgroundColor(selectionBackColor);
					int partialStart = frag._offset > start ? frag._offset
							: start;
					int partialEnd = (frag._offset + frag._length > end) ? end
							: (frag._offset + frag._length);
					int x = 0;
					String skip = text.substring(frag._offset, partialStart);
					x = FlowUtilities.getTextExtents(skip, font).width;
					String todraw = text.substring(partialStart, partialEnd);
					if (todraw.length() > 0) {
						Dimension dimension = FlowUtilities.getTextExtents(skip
								+ todraw, font);
						g.fillRectangle(frag._x + x, frag._y, dimension.width
								- x, dimension.height);
						g.drawText(skip + todraw, frag._x, frag._y);
						if (color != null) {
							g.setForegroundColor(color);
						} else {
							g.setForegroundColor(originalForeground);
						}
						g.drawText(skip, frag._x, frag._y);
						paintTextDecoration(g, frag.getRectangle(),
								textDecoration);
						g.setForegroundColor(selectionForeColor);
						paintTextDecoration(g,
								new Rectangle(frag._x + x, frag._y,
										dimension.width - x, dimension.height),
								textDecoration);
					}
				}

				// we do this in each loop, to make sure we are using correct
				// color
				g.setForegroundColor(originalForeground);
				g.setBackgroundColor(originalBackgroud);

			}
		} catch (Exception e) {
			// "Error in text painting:"
			_log.info("TextLayoutSupport.Info.1", e); //$NON-NLS-1$
		}
	}

	public static int getBeginX(Object textAlign, Rectangle rect, int textWidth) {
		int x = rect.x;
		if (textAlign != null) {
			String align = textAlign.toString();
			if ("left".equalsIgnoreCase(align)) //$NON-NLS-1$
			{
				x = rect.x + 1;
			} else if ("right".equalsIgnoreCase(align)) //$NON-NLS-1$
			{
				x = rect.x + rect.width - textWidth - 1;
				if (x < 1) {
					x = 1;
				}
			} else if ("center".equalsIgnoreCase(align)) //$NON-NLS-1$
			{
				int offset = (rect.width - textWidth) / 2;
				if (offset <= 0) {
					offset = 0;
				}
				x = x + offset + 1;
			}
		}
		return x;
	}
}

Back to the top