comparison dwtx/draw2d/TextUtilities.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children 1082a0fc2bb8
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
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 dwtx.draw2d.TextUtilities;
15
16 import dwt.dwthelper.utils;
17
18
19 import dwt.graphics.Font;
20 import dwt.graphics.FontMetrics;
21 import dwtx.draw2d.geometry.Dimension;
22 import dwtx.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 public static TextUtilities INSTANCE;
37
38 static this(){
39 INSTANCE = new TextUtilities();
40 }
41
42 /**
43 * Returns the Dimensions of <i>s</i> in Font <i>f</i>.
44 *
45 * @param s
46 * the string
47 * @param f
48 * the font
49 * @return the dimensions of the given string
50 */
51 public Dimension getStringExtents(String s, Font f) {
52 return FigureUtilities.getStringExtents(s, f);
53 }
54
55 /**
56 * Returns the Dimensions of the given text, converting newlines and tabs
57 * appropriately.
58 *
59 * @param s
60 * the text
61 * @param f
62 * the font
63 * @return the dimensions of the given text
64 */
65 public Dimension getTextExtents(String s, Font f) {
66 return FigureUtilities.getTextExtents(s, f);
67 }
68
69 /**
70 * Gets the font's ascent.
71 *
72 * @param font
73 * @return the font's ascent
74 */
75 public int getAscent(Font font) {
76 FontMetrics fm = FigureUtilities.getFontMetrics(font);
77 return fm.getHeight() - fm.getDescent();
78 }
79
80 /**
81 * Gets the font's descent.
82 *
83 * @param font
84 * @return the font's descent
85 */
86 public int getDescent(Font font) {
87 return FigureUtilities.getFontMetrics(font).getDescent();
88 }
89
90 /**
91 * Returns the largest substring of <i>s</i> in Font <i>f</i> that can be
92 * confined to the number of pixels in <i>availableWidth<i>.
93 *
94 * @param s
95 * the original string
96 * @param f
97 * the font
98 * @param availableWidth
99 * the available width
100 * @return the largest substring that fits in the given width
101 */
102 public int getLargestSubstringConfinedTo(String s, Font f,
103 int availableWidth) {
104 FontMetrics metrics = FigureUtilities.getFontMetrics(f);
105 int min, max;
106 float avg = metrics.getAverageCharWidth();
107 min = 0;
108 max = s.length + 1;
109
110 // The size of the current guess
111 int guess = 0, guessSize = 0;
112 while ((max - min) > 1) {
113 // Pick a new guess size
114 // New guess is the last guess plus the missing width in pixels
115 // divided by the average character size in pixels
116 guess = guess + cast(int) ((availableWidth - guessSize) / avg);
117
118 if (guess >= max)
119 guess = max - 1;
120 if (guess <= min)
121 guess = min + 1;
122
123 // Measure the current guess
124 guessSize = getTextExtents(s.substring(0, guess), f).width;
125
126 if (guessSize < availableWidth)
127 // We did not use the available width
128 min = guess;
129 else
130 // We exceeded the available width
131 max = guess;
132 }
133 return min;
134 }
135 }