comparison org.eclipse.draw2d/src/org/eclipse/draw2d/TextUtilities.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2007 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13
14 module org.eclipse.draw2d.TextUtilities;
15
16 import java.lang.all;
17
18
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.FontMetrics;
21 import org.eclipse.draw2d.geometry.Dimension;
22 import org.eclipse.draw2d.FigureUtilities;
23
24 /**
25 * Provides miscellaneous text operations. Clients may subclass this class if
26 * necessary.
27 *
28 * @author crevells
29 * @since 3.4
30 */
31 public class TextUtilities {
32
33 /**
34 * a singleton default instance
35 */
36 private static TextUtilities INSTANCE_;
37 public static TextUtilities INSTANCE(){
38 if( INSTANCE_ is null ){
39 synchronized( TextUtilities.classinfo ){
40 if( INSTANCE_ is null ){
41 INSTANCE_ = new TextUtilities();
42 }
43 }
44 }
45 return INSTANCE_;
46 }
47 /**
48 * Returns the Dimensions of <i>s</i> in Font <i>f</i>.
49 *
50 * @param s
51 * the string
52 * @param f
53 * the font
54 * @return the dimensions of the given string
55 */
56 public Dimension getStringExtents(String s, Font f) {
57 return FigureUtilities.getStringExtents(s, f);
58 }
59
60 /**
61 * Returns the Dimensions of the given text, converting newlines and tabs
62 * appropriately.
63 *
64 * @param s
65 * the text
66 * @param f
67 * the font
68 * @return the dimensions of the given text
69 */
70 public Dimension getTextExtents(String s, Font f) {
71 return FigureUtilities.getTextExtents(s, f);
72 }
73
74 /**
75 * Gets the font's ascent.
76 *
77 * @param font
78 * @return the font's ascent
79 */
80 public int getAscent(Font font) {
81 FontMetrics fm = FigureUtilities.getFontMetrics(font);
82 return fm.getHeight() - fm.getDescent();
83 }
84
85 /**
86 * Gets the font's descent.
87 *
88 * @param font
89 * @return the font's descent
90 */
91 public int getDescent(Font font) {
92 return FigureUtilities.getFontMetrics(font).getDescent();
93 }
94
95 /**
96 * Returns the largest substring of <i>s</i> in Font <i>f</i> that can be
97 * confined to the number of pixels in <i>availableWidth<i>.
98 *
99 * @param s
100 * the original string
101 * @param f
102 * the font
103 * @param availableWidth
104 * the available width
105 * @return the largest substring that fits in the given width
106 */
107 public int getLargestSubstringConfinedTo(String s, Font f,
108 int availableWidth) {
109 FontMetrics metrics = FigureUtilities.getFontMetrics(f);
110 int min, max;
111 float avg = metrics.getAverageCharWidth();
112 min = 0;
113 max = s.length + 1;
114
115 // The size of the current guess
116 int guess = 0, guessSize = 0;
117 while ((max - min) > 1) {
118 // Pick a new guess size
119 // New guess is the last guess plus the missing width in pixels
120 // divided by the average character size in pixels
121 guess = guess + cast(int) ((availableWidth - guessSize) / avg);
122
123 if (guess >= max)
124 guess = max - 1;
125 if (guess <= min)
126 guess = min + 1;
127
128 // Measure the current guess
129 guessSize = getTextExtents(s.substring(0, guess), f).width;
130
131 if (guessSize < availableWidth)
132 // We did not use the available width
133 min = guess;
134 else
135 // We exceeded the available width
136 max = guess;
137 }
138 return min;
139 }
140 }