GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* Utilities.java -- 2: Copyright (C) 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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) 9: any later version. 10: 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. 15: 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., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 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 24: combination. 25: 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. */ 37: 38: 39: package javax.swing.text; 40: 41: import java.awt.FontMetrics; 42: import java.awt.Graphics; 43: 44: /** 45: * A set of utilities to deal with text. This is used by several other classes 46: * inside this package. 47: * 48: * @author Roman Kennke (roman@ontographics.com) 49: */ 50: public class Utilities 51: { 52: /** 53: * The length of the char buffer that holds the characters to be drawn. 54: */ 55: private static final int BUF_LENGTH = 64; 56: 57: /** 58: * Creates a new <code>Utilities</code> object. 59: */ 60: public Utilities() 61: { 62: // Nothing to be done here. 63: } 64: 65: /** 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}. 69: * 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 75: * technique. 76: * @param startOffset starting offset in the text. 77: * @return the x coordinate at the end of the drawn text. 78: */ 79: public static final int drawTabbedText(Segment s, int x, int y, Graphics g, 80: TabExpander e, int startOffset) 81: { 82: // This buffers the chars to be drawn. 83: char[] buffer = s.array; 84: 85: 86: // The current x and y pixel coordinates. 87: int pixelX = x; 88: int pixelY = y; 89: 90: // The font metrics of the current selected font. 91: FontMetrics metrics = g.getFontMetrics(); 92: int ascent = metrics.getAscent(); 93: 94: int pixelWidth = 0; 95: int pos = s.offset; 96: int len = 0; 97: 98: for (int offset = s.offset; offset < (s.offset + s.count); ++offset) 99: { 100: char c = buffer[offset]; 101: if (c == '\t' || c == '\n') 102: { 103: if (len > 0) { 104: g.drawChars(buffer, pos, len, pixelX, pixelY + ascent); 105: pixelX += pixelWidth; 106: pixelWidth = 0; 107: } 108: pos = offset+1; 109: len = 0; 110: } 111: 112: switch (c) 113: { 114: case '\t': 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 ' '. 117: if (e != null) 118: pixelX = (int) e.nextTabStop((float) pixelX, 119: startOffset + offset - s.offset); 120: else 121: pixelX += metrics.charWidth(' '); 122: break; 123: case '\n': 124: // In case we have a newline, we must jump to the next line. 125: pixelY += metrics.getHeight(); 126: pixelX = x; 127: break; 128: default: 129: ++len; 130: pixelWidth += metrics.charWidth(buffer[offset]); 131: break; 132: } 133: } 134: 135: if (len > 0) 136: g.drawChars(buffer, pos, len, pixelX, pixelY + ascent); 137: 138: return pixelX; 139: } 140: 141: /** 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 151: * into account. 152: */ 153: public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, 154: int x, TabExpander e, 155: int startOffset) 156: { 157: // This buffers the chars to be drawn. 158: char[] buffer = s.array; 159: 160: // The current x coordinate. 161: int pixelX = x; 162: 163: // The current maximum width. 164: int maxWidth = 0; 165: 166: for (int offset = s.offset; offset < (s.offset + s.count); ++offset) 167: { 168: switch (buffer[offset]) 169: { 170: case '\t': 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'. 173: if (e != null) 174: pixelX = (int) e.nextTabStop((float) pixelX, 175: startOffset + offset - s.offset); 176: else 177: pixelX += metrics.charWidth(' '); 178: break; 179: case '\n': 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); 184: pixelX = x; 185: break; 186: default: 187: // Here we draw the char. 188: pixelX += metrics.charWidth(buffer[offset]); 189: break; 190: } 191: } 192: 193: // Take the last line into account. 194: maxWidth = Math.max(maxWidth, pixelX - x); 195: 196: return maxWidth; 197: } 198: }
GNU Classpath (0.18) |