From 000b5f316188e48fe6b042f159fd5e2f62554ab8 Mon Sep 17 00:00:00 2001 From: rupashka Date: Sat, 5 Mar 2011 18:27:51 +0300 Subject: [PATCH] 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print() Reviewed-by: peterz --- .../classes/javax/swing/text/Utilities.java | 31 +++++-------------- .../javax/swing/text/WrappedPlainView.java | 8 ++--- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/share/classes/javax/swing/text/Utilities.java b/src/share/classes/javax/swing/text/Utilities.java index 704cfa02c..e3ef4940d 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 2f02a7ca9..90d11659c 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; -- GitLab