6857057: api/javax_swing/text/GlyphView/index.html#Methods test fails
Reviewed-by: peterz
--- a/jdk/src/share/classes/javax/swing/text/Utilities.java Tue Jun 01 14:17:38 2010 -0700
+++ b/jdk/src/share/classes/javax/swing/text/Utilities.java Wed Jun 02 11:59:27 2010 +0400
@@ -404,6 +404,24 @@
}
/**
+ * 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
@@ -425,7 +443,7 @@
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 Tue Jun 01 14:17:38 2010 -0700
+++ b/jdk/src/share/classes/javax/swing/text/WrappedPlainView.java Wed Jun 02 11:59:27 2010 +0400
@@ -108,7 +108,7 @@
try {
if (line.isLeaf()) {
- drawText(line, p0, p1, g, x, y);
+ drawText(line, p0, p1, g, x, y);
} else {
// this line contains the composed text.
int idx = line.getElementIndex(p0);
@@ -239,9 +239,11 @@
tabBase, tabBase + currentWidth,
this, p0);
} else {
- p = p0 + Utilities.getTabbedTextOffset(segment, metrics,
- tabBase, tabBase + currentWidth,
- this, p0, false);
+ int offset = Utilities.getTabbedTextOffset(segment, metrics,
+ tabBase, tabBase + currentWidth, this, p0, false);
+ offset = Utilities.adjustOffsetForFractionalMetrics(
+ segment, metrics, offset, currentWidth);
+ p = p0 + offset;
}
SegmentCache.releaseSharedSegment(segment);
return p;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/WrappedPlainView/6857057/StubBranchElement.java Wed Jun 02 11:59:27 2010 +0400
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import javax.swing.text.*;
+
+class StubBranchElement implements Element {
+ Document document = new DefaultStyledDocument();
+ String context;
+ Element[] children = new StubLeafElement[3];
+
+ public StubBranchElement(String context) {
+ this.context = context;
+ int len = context.length() / 3;
+ for (int i = 0; i < 3; i++) {
+ children[i] = new StubLeafElement(
+ context.substring(len * i, len * (i + 1)), this, len * i);
+ }
+ try {
+ document.insertString(0, context, new SimpleAttributeSet());
+ } catch (BadLocationException e) {
+ }
+ }
+
+ public Document getDocument() {
+ return document;
+ }
+
+ public Element getParentElement() {
+ return null;
+ }
+
+ public String getName() {
+ return "StubBranchElement";
+ }
+
+ public AttributeSet getAttributes() {
+ return new SimpleAttributeSet();
+ }
+
+ public int getStartOffset() {
+ return 0;
+ }
+
+ public int getEndOffset() {
+ return document.getLength();
+ }
+
+ public int getElementIndex(int offset) {
+ return offset / 3;
+ }
+
+ public int getElementCount() {
+ return 3;
+ }
+
+ public Element getElement(int index) {
+ return children[index];
+ }
+
+ public boolean isLeaf() {
+ return false;
+ }
+
+ public Element[] getChildren() {
+ return children;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/WrappedPlainView/6857057/StubLeafElement.java Wed Jun 02 11:59:27 2010 +0400
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import javax.swing.text.*;
+
+class StubLeafElement implements Element {
+ Document document = new DefaultStyledDocument();
+ String context;
+ Element parent;
+ int position;
+
+ public StubLeafElement(String context, Element parent, int position) {
+ this.context = context;
+ this.parent = parent;
+ this.position = position;
+ try {
+ document.insertString(0, context, new SimpleAttributeSet());
+ } catch (BadLocationException e) {
+ }
+ }
+
+ public Document getDocument() {
+ return document;
+ }
+
+ public Element getParentElement() {
+ return parent;
+ }
+
+ public String getName() {
+ return "StubLeafElement";
+ }
+
+ public AttributeSet getAttributes() {
+ return new SimpleAttributeSet();
+ }
+
+ public int getStartOffset() {
+ return position;
+ }
+
+ public int getEndOffset() {
+ return position + document.getLength();
+ }
+
+ public int getElementIndex(int offset) {
+ return 0;
+ }
+
+ public int getElementCount() {
+ return 0;
+ }
+
+ public Element getElement(int index) {
+ return this;
+ }
+
+ public boolean isLeaf() {
+ return true;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/WrappedPlainView/6857057/bug6857057.java Wed Jun 02 11:59:27 2010 +0400
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6857057
+ * @summary test that the JCK GlyphView2021 test doesn't fail
+ * @author Sergey Groznyh
+ * @run main bug6857057
+ */
+
+import javax.swing.*;
+import javax.swing.text.Element;
+import javax.swing.text.GlyphView;
+import javax.swing.text.View;
+
+public class bug6857057 {
+
+ bug6857057() {
+ Element elem = new StubBranchElement(" G L Y P H V");
+ GlyphView view = new GlyphView(elem);
+ float pos = elem.getStartOffset();
+ float len = elem.getEndOffset() - pos;
+ int res = view.getBreakWeight(View.X_AXIS, pos, len);
+ if (res != View.ExcellentBreakWeight) {
+ throw new RuntimeException("breakWeight != ExcellentBreakWeight");
+ }
+ }
+
+ public static void main(String[] args) throws Throwable {
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ new bug6857057();
+ }
+ });
+
+ System.out.println("OK");
+ }
+}