diff options
Diffstat (limited to 'libjava/classpath/javax/swing/text/Utilities.java')
-rw-r--r-- | libjava/classpath/javax/swing/text/Utilities.java | 99 |
1 files changed, 66 insertions, 33 deletions
diff --git a/libjava/classpath/javax/swing/text/Utilities.java b/libjava/classpath/javax/swing/text/Utilities.java index 1adc8ff87e9..d109a4a950f 100644 --- a/libjava/classpath/javax/swing/text/Utilities.java +++ b/libjava/classpath/javax/swing/text/Utilities.java @@ -41,12 +41,8 @@ package javax.swing.text; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Point; -import java.awt.Rectangle; import java.text.BreakIterator; -import javax.swing.SwingConstants; -import javax.swing.SwingUtilities; - /** * A set of utilities to deal with text. This is used by several other classes * inside this package. @@ -73,6 +69,10 @@ public class Utilities * are taken into account. Tabs are expanded using the * specified {@link TabExpander}. * + * + * The X and Y coordinates denote the start of the <em>baseline</em> where + * the text should be drawn. + * * @param s the text fragment to be drawn. * @param x the x position for drawing. * @param y the y position for drawing. @@ -88,15 +88,14 @@ public class Utilities // This buffers the chars to be drawn. char[] buffer = s.array; - - // The current x and y pixel coordinates. - int pixelX = x; - int pixelY = y; - // The font metrics of the current selected font. FontMetrics metrics = g.getFontMetrics(); int ascent = metrics.getAscent(); + // The current x and y pixel coordinates. + int pixelX = x; + int pixelY = y - ascent; + int pixelWidth = 0; int pos = s.offset; int len = 0; @@ -238,9 +237,10 @@ public class Utilities int pos; int currentX = x0; - for (pos = p0; pos < s.count; pos++) + for (pos = 0; pos < s.count; pos++) { char nextChar = s.array[s.offset+pos]; + if (nextChar == 0) { if (! round) @@ -256,6 +256,7 @@ public class Utilities else currentX = (int) te.nextTabStop(currentX, pos); } + if (currentX > x) { if (! round) @@ -263,7 +264,8 @@ public class Utilities break; } } - return pos; + + return pos + p0; } /** @@ -510,10 +512,10 @@ public class Utilities { int mark = Utilities.getTabbedTextOffset(s, metrics, x0, x, e, startOffset); BreakIterator breaker = BreakIterator.getWordInstance(); - breaker.setText(s.toString()); - + breaker.setText(s); + // If mark is equal to the end of the string, just use that position - if (mark == s.count) + if (mark == s.count + s.offset) return mark; // Try to find a word boundary previous to the mark at which we @@ -571,15 +573,29 @@ public class Utilities public static final int getPositionAbove(JTextComponent c, int offset, int x) throws BadLocationException { - View rootView = c.getUI().getRootView(c); - Rectangle r = c.modelToView(offset); - int offs = c.viewToModel(new Point(x, r.y)); - int pos = rootView.getNextVisualPositionFrom(offs, - Position.Bias.Forward, - SwingUtilities.calculateInnerArea(c, null), - SwingConstants.NORTH, - new Position.Bias[1]); - return pos; + int offs = getRowStart(c, offset); + + if(offs == -1) + return -1; + + // Effectively calculates the y value of the previous line. + Point pt = c.modelToView(offs-1).getLocation(); + + pt.x = x; + + // Calculate a simple fitting offset. + offs = c.viewToModel(pt); + + // Find out the real x positions of the calculated character and its + // neighbour. + int offsX = c.modelToView(offs).getLocation().x; + int offsXNext = c.modelToView(offs+1).getLocation().x; + + // Chose the one which is nearer to us and return its offset. + if (Math.abs(offsX-x) <= Math.abs(offsXNext-x)) + return offs; + else + return offs+1; } /** @@ -598,14 +614,31 @@ public class Utilities public static final int getPositionBelow(JTextComponent c, int offset, int x) throws BadLocationException { - View rootView = c.getUI().getRootView(c); - Rectangle r = c.modelToView(offset); - int offs = c.viewToModel(new Point(x, r.y)); - int pos = rootView.getNextVisualPositionFrom(offs, - Position.Bias.Forward, - SwingUtilities.calculateInnerArea(c, null), - SwingConstants.SOUTH, - new Position.Bias[1]); - return pos; - } + int offs = getRowEnd(c, offset); + + if(offs == -1) + return -1; + + // Effectively calculates the y value of the previous line. + Point pt = c.modelToView(offs+1).getLocation(); + + pt.x = x; + + // Calculate a simple fitting offset. + offs = c.viewToModel(pt); + + if (offs == c.getDocument().getLength()) + return offs; + + // Find out the real x positions of the calculated character and its + // neighbour. + int offsX = c.modelToView(offs).getLocation().x; + int offsXNext = c.modelToView(offs+1).getLocation().x; + + // Chose the one which is nearer to us and return its offset. + if (Math.abs(offsX-x) <= Math.abs(offsXNext-x)) + return offs; + else + return offs+1; + } } |