2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.swing
.text
;
41 import java
.awt
.FontMetrics
;
42 import java
.awt
.Graphics
;
45 * A set of utilities to deal with text. This is used by several other classes
46 * inside this package.
48 * @author Roman Kennke (roman@ontographics.com)
50 public class Utilities
53 * The length of the char buffer that holds the characters to be drawn.
55 private static final int BUF_LENGTH
= 64;
58 * Creates a new <code>Utilities</code> object.
62 // Nothing to be done here.
66 * Draws the given text segment. Contained tabs and newline characters
67 * are taken into account. Tabs are expanded using the
68 * specified {@link TabExpander}.
70 * @param s the text fragment to be drawn.
71 * @param x the x position for drawing.
72 * @param y the y position for drawing.
73 * @param g the {@link Graphics} context for drawing.
74 * @param e the {@link TabExpander} which specifies the Tab-expanding
76 * @param startOffset starting offset in the text.
77 * @return the x coordinate at the end of the drawn text.
79 public static final int drawTabbedText(Segment s
, int x
, int y
, Graphics g
,
80 TabExpander e
, int startOffset
)
82 // This buffers the chars to be drawn.
83 char[] buffer
= s
.array
;
86 // The current x and y pixel coordinates.
90 // The font metrics of the current selected font.
91 FontMetrics metrics
= g
.getFontMetrics();
92 int ascent
= metrics
.getAscent();
98 for (int offset
= s
.offset
; offset
< (s
.offset
+ s
.count
); ++offset
)
100 char c
= buffer
[offset
];
101 if (c
== '\t' || c
== '\n')
104 g
.drawChars(buffer
, pos
, len
, pixelX
, pixelY
+ ascent
);
105 pixelX
+= pixelWidth
;
115 // In case we have a tab, we just 'jump' over the tab.
116 // When we have no tab expander we just use the width of ' '.
118 pixelX
= (int) e
.nextTabStop((float) pixelX
,
119 startOffset
+ offset
- s
.offset
);
121 pixelX
+= metrics
.charWidth(' ');
124 // In case we have a newline, we must jump to the next line.
125 pixelY
+= metrics
.getHeight();
130 pixelWidth
+= metrics
.charWidth(buffer
[offset
]);
136 g
.drawChars(buffer
, pos
, len
, pixelX
, pixelY
+ ascent
);
142 * Determines the width, that the given text <code>s</code> would take
143 * if it was printed with the given {@link java.awt.FontMetrics} on the
144 * specified screen position.
145 * @param s the text fragment
146 * @param metrics the font metrics of the font to be used
147 * @param x the x coordinate of the point at which drawing should be done
148 * @param e the {@link TabExpander} to be used
149 * @param startOffset the index in <code>s</code> where to start
150 * @returns the width of the given text s. This takes tabs and newlines
153 public static final int getTabbedTextWidth(Segment s
, FontMetrics metrics
,
154 int x
, TabExpander e
,
157 // This buffers the chars to be drawn.
158 char[] buffer
= s
.array
;
160 // The current x coordinate.
163 // The current maximum width.
166 for (int offset
= s
.offset
; offset
< (s
.offset
+ s
.count
); ++offset
)
168 switch (buffer
[offset
])
171 // In case we have a tab, we just 'jump' over the tab.
172 // When we have no tab expander we just use the width of 'm'.
174 pixelX
= (int) e
.nextTabStop((float) pixelX
,
175 startOffset
+ offset
- s
.offset
);
177 pixelX
+= metrics
.charWidth(' ');
180 // In case we have a newline, we must 'draw'
181 // the buffer and jump on the next line.
182 pixelX
+= metrics
.charWidth(buffer
[offset
]);
183 maxWidth
= Math
.max(maxWidth
, pixelX
- x
);
187 // Here we draw the char.
188 pixelX
+= metrics
.charWidth(buffer
[offset
]);
193 // Take the last line into account.
194 maxWidth
= Math
.max(maxWidth
, pixelX
- x
);