Project

General

Profile

Download (3.38 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11

    
12
package org.eclipse.draw2d;
13

    
14
import org.eclipse.swt.graphics.Font;
15
import org.eclipse.swt.graphics.FontMetrics;
16

    
17
import org.eclipse.draw2d.geometry.Dimension;
18

    
19
/**
20
 * Provides miscellaneous text operations. Clients may subclass this class if
21
 * necessary.
22
 * 
23
 * @author crevells
24
 * @since 3.4
25
 */
26
public class TextUtilities {
27

    
28
	/**
29
	 * a singleton default instance
30
	 */
31
	public static TextUtilities INSTANCE = new TextUtilities();
32

    
33
	/**
34
	 * Returns the Dimensions of <i>s</i> in Font <i>f</i>.
35
	 * 
36
	 * @param s
37
	 *            the string
38
	 * @param f
39
	 *            the font
40
	 * @return the dimensions of the given string
41
	 */
42
	public Dimension getStringExtents(String s, Font f) {
43
		return FigureUtilities.getStringExtents(s, f);
44
	}
45

    
46
	/**
47
	 * Returns the Dimensions of the given text, converting newlines and tabs
48
	 * appropriately.
49
	 * 
50
	 * @param s
51
	 *            the text
52
	 * @param f
53
	 *            the font
54
	 * @return the dimensions of the given text
55
	 */
56
	public Dimension getTextExtents(String s, Font f) {
57
		return FigureUtilities.getTextExtents(s, f);
58
	}
59

    
60
	/**
61
	 * Gets the font's ascent.
62
	 * 
63
	 * @param font
64
	 * @return the font's ascent
65
	 */
66
	public int getAscent(Font font) {
67
		FontMetrics fm = FigureUtilities.getFontMetrics(font);
68
	    // UNSUPPORTED - getDescent() not implemented in RAP
69
//		return fm.getHeight() - fm.getDescent();
70
		return fm.getHeight();
71
	}
72

    
73
	/**
74
	 * Gets the font's descent.
75
	 * 
76
	 * @param font
77
	 * @return the font's descent
78
	 */
79
	public int getDescent(Font font) {
80
	     // UNSUPPORTED - getDescent() not implemented in RAP
81
//		return FigureUtilities.getFontMetrics(font).getDescent();
82
		return 0;
83
	}
84

    
85
	/**
86
	 * Returns the largest substring of <i>s</i> in Font <i>f</i> that can be
87
	 * confined to the number of pixels in <i>availableWidth<i>.
88
	 * 
89
	 * @param s
90
	 *            the original string
91
	 * @param f
92
	 *            the font
93
	 * @param availableWidth
94
	 *            the available width
95
	 * @return the largest substring that fits in the given width
96
	 */
97
	public int getLargestSubstringConfinedTo(String s, Font f,
98
			int availableWidth) {
99
		FontMetrics metrics = FigureUtilities.getFontMetrics(f);
100
		int min, max;
101
		float avg = metrics.getAverageCharWidth();
102
		min = 0;
103
		max = s.length() + 1;
104

    
105
		// The size of the current guess
106
		int guess = 0, guessSize = 0;
107
		while ((max - min) > 1) {
108
			// Pick a new guess size
109
			// New guess is the last guess plus the missing width in pixels
110
			// divided by the average character size in pixels
111
			guess = guess + (int) ((availableWidth - guessSize) / avg);
112

    
113
			if (guess >= max)
114
				guess = max - 1;
115
			if (guess <= min)
116
				guess = min + 1;
117

    
118
			// Measure the current guess
119
			guessSize = getTextExtents(s.substring(0, guess), f).width;
120

    
121
			if (guessSize < availableWidth)
122
				// We did not use the available width
123
				min = guess;
124
			else
125
				// We exceeded the available width
126
				max = guess;
127
		}
128
		return min;
129
	}
130
}
(154-154/171)