diff --git a/src/share/classes/javax/swing/text/Utilities.java b/src/share/classes/javax/swing/text/Utilities.java index 704cfa02c84c7c3a17ef05eff1bde3d6dc35f109..e3ef4940d5a89a1951715cae259bc30d454a53c1 100644 --- a/src/share/classes/javax/swing/text/Utilities.java +++ b/src/share/classes/javax/swing/text/Utilities.java @@ -390,11 +390,15 @@ public class Utilities { } if ((x >= currX) && (x < nextX)) { // found the hit position... return the appropriate side - if ((round == false) || ((x - currX) < (nextX - x))) { - return i - txtOffset; - } else { - return i + 1 - txtOffset; + int offset = ((round == false) || ((x - currX) < (nextX - x))) ? + (i - txtOffset) : (i + 1 - txtOffset); + // the length of the string measured as a whole may differ from + // the sum of individual character lengths, for example if + // fractional metrics are enabled; and we must guard from this. + while (metrics.charsWidth(txt, txtOffset, offset + 1) > (x - x0)) { + offset--; } + return (offset < 0 ? 0 : offset); } currX = nextX; } @@ -403,24 +407,6 @@ public class Utilities { return txtCount; } - /** - * Adjust text offset so that the length of a resulting string as a whole - * fits into the specified width. - */ - static int adjustOffsetForFractionalMetrics( - Segment s, FontMetrics fm, int offset, int width) { - // Sometimes the offset returned by getTabbedTextOffset is beyond the - // available area, when fractional metrics are enabled. We should - // guard against this. - if (offset < s.count) { - while (offset > 0 && - fm.charsWidth(s.array, s.offset, offset + 1) > width) { - offset--; - } - } - return offset; - } - /** * Determine where to break the given text to fit * within the given span. This tries to find a word boundary. @@ -443,7 +429,6 @@ public class Utilities { int txtCount = s.count; int index = Utilities.getTabbedTextOffset(s, metrics, x0, x, e, startOffset, false); - index = adjustOffsetForFractionalMetrics(s, metrics, index, x - x0); if (index >= txtCount - 1) { return txtCount; diff --git a/src/share/classes/javax/swing/text/WrappedPlainView.java b/src/share/classes/javax/swing/text/WrappedPlainView.java index 2f02a7ca956f200cecce87857abcc66b3fd07303..90d11659c2c6673c19b32b62b3819d7ed4f43e30 100644 --- a/src/share/classes/javax/swing/text/WrappedPlainView.java +++ b/src/share/classes/javax/swing/text/WrappedPlainView.java @@ -239,11 +239,9 @@ public class WrappedPlainView extends BoxView implements TabExpander { tabBase, tabBase + currentWidth, this, p0); } else { - int offset = Utilities.getTabbedTextOffset(segment, metrics, - tabBase, tabBase + currentWidth, this, p0, false); - offset = Utilities.adjustOffsetForFractionalMetrics( - segment, metrics, offset, currentWidth); - p = p0 + offset; + p = p0 + Utilities.getTabbedTextOffset(segment, metrics, + tabBase, tabBase + currentWidth, + this, p0, false); } SegmentCache.releaseSharedSegment(segment); return p;