提交 1ab59122 编写于 作者: R rupashka

6857057: api/javax_swing/text/GlyphView/index.html#Methods test fails

Reviewed-by: peterz
上级 64a27af3
......@@ -403,6 +403,24 @@ 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.
......@@ -425,7 +443,7 @@ 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;
......
......@@ -108,7 +108,7 @@ public class WrappedPlainView extends BoxView implements TabExpander {
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 @@ public class WrappedPlainView extends BoxView implements TabExpander {
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;
......
/*
* 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;
}
}
/*
* 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;
}
}
/*
* 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");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册