6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
authorrupashka
Sat, 05 Mar 2011 18:27:51 +0300
changeset 8533 0537e84f6e69
parent 8532 0b3d979bbd52
child 8534 6b492caeba16
6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print() Reviewed-by: peterz
jdk/src/share/classes/javax/swing/text/Utilities.java
jdk/src/share/classes/javax/swing/text/WrappedPlainView.java
--- a/jdk/src/share/classes/javax/swing/text/Utilities.java	Thu Mar 03 18:27:47 2011 +0300
+++ b/jdk/src/share/classes/javax/swing/text/Utilities.java	Sat Mar 05 18:27:51 2011 +0300
@@ -390,11 +390,15 @@
             }
             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;
         }
@@ -404,24 +408,6 @@
     }
 
     /**
-     * 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.
      * @param s  the source of the text
@@ -443,7 +429,6 @@
         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;
--- a/jdk/src/share/classes/javax/swing/text/WrappedPlainView.java	Thu Mar 03 18:27:47 2011 +0300
+++ b/jdk/src/share/classes/javax/swing/text/WrappedPlainView.java	Sat Mar 05 18:27:51 2011 +0300
@@ -239,11 +239,9 @@
                                                 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;