gpt4 book ai didi

java - LineBreakMeasurer 中没有堆栈跟踪的 ClassCastException

转载 作者:行者123 更新时间:2023-12-02 11:18:23 25 4
gpt4 key购买 nike

我正在尝试以从右到左的语言(阿拉伯语)打印一个合理的段落,并尝试使用 TextLayoutLineBreakerMeasurer 来实现这一点,因为围绕web 似乎认为,尝试编写每个单词/字符位置的计算代码是浪费时间和精力(我已经尝试过,但运气不佳)。

我遇到的问题是正确实现AttributedCharacterIterator。更具体地说,我收到了 ClassCastException,但没有任何堆栈跟踪

第一个代码片段是打印代码,取自实现 Printable 的类的 print 方法:

AttributedCharacterIterator it = (AttributedCharacterIterator) new MyCharacterIterator(paragraph);
int xPos = (int) pf.getImageableX();
int yPos = (int) pf.getImageableY();

// EDIT: line causing the exception
LineBreakMeasurer measurer = new LineBreakMeasurer(it, frc);
float wrappingWidth = (float) pf.getImageableWidth();

while (measurer.getPosition() < paragraph.length()) {

TextLayout layout = measurer.nextLayout(wrappingWidth);

yPos += (layout.getAscent());
float dx = layout.isLeftToRight() ?
0 : (wrappingWidth - layout.getAdvance());

layout.draw((Graphics2D)g, xPos + dx, yPos);
yPos += layout.getDescent() + layout.getLeading();
}

第二个代码片段是AttributedCharacterIterator的实现:

import java.awt.font.NumericShaper;
import java.lang.reflect.Field;
import java.text.AttributedCharacterIterator;
import java.text.CharacterIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class MyCharacterIterator implements AttributedCharacterIterator {

char[] chars;
int current;

public MyCharacterIterator(String text) {

chars = new char[text.length()+1];
System.out.println(chars.length);
text.getChars(0, text.length(), chars, 0);
System.out.println(new String(chars).length());
current = 0;
}

public Object clone() {
return null;
}

@Override
public char current() {
// TODO Auto-generated method stub
return chars[current];
}

@Override
public char first() {
// TODO Auto-generated method stub
current = 0;
return chars[current];
}

@Override
public int getBeginIndex() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getEndIndex() {
// TODO Auto-generated method stub
return chars.length;
}

@Override
public int getIndex() {
// TODO Auto-generated method stub
return current;
}

@Override
public char last() {
// TODO Auto-generated method stub
return chars[chars.length-1];
}

@Override
public char next() {
// TODO Auto-generated method stub
current += 1;
if(current<chars.length) {
return chars[current];
}
else {
return CharacterIterator.DONE;
}
}

@Override
public char previous() {
// TODO Auto-generated method stub
current -=1;
return chars[current];
}

@Override
public char setIndex(int position) {
// TODO Auto-generated method stub
current = position;
return chars[current];
}

@Override
public Set<Attribute> getAllAttributeKeys() {
// TODO Auto-generated method stub
TreeSet<Attribute> set = new TreeSet<Attribute>();
set.add(TextAttributeConstants.RUN_DIRECTION);
set.add(TextAttributeConstants.NUMERIC_SHAPING);
set.add(TextAttributeConstants.BIDI_EMBEDDING);
return set;
}

@Override
public Object getAttribute(Attribute attribute) {
// TODO Auto-generated method stub
if(attribute==TextAttributeConstants.RUN_DIRECTION) {
return false;
}
else if(attribute==TextAttributeConstants.NUMERIC_SHAPING) {
return NumericShaper.getContextualShaper(100);
}
else if(attribute==TextAttributeConstants.BIDI_EMBEDDING) {
return 0;
}
else return new Object();
}

@Override
public Map<Attribute, Object> getAttributes() {
// TODO Auto-generated method stub
TreeMap<Attribute, Object> map = new TreeMap<Attribute, Object>();
map.clear();
return map;

}

@Override
public int getRunLimit() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getRunLimit(Attribute attribute) {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getRunLimit(Set<? extends Attribute> attributes) {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getRunStart() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getRunStart(Attribute attribute) {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getRunStart(Set<? extends Attribute> attributes) {
// TODO Auto-generated method stub
return 0;
}

private static class TextAttributeConstants {
private static final Class<?> clazz = getClass("java.awt.font.TextAttribute");

static final AttributedCharacterIterator.Attribute RUN_DIRECTION = getTextAttribute("RUN_DIRECTION");
static final AttributedCharacterIterator.Attribute NUMERIC_SHAPING = getTextAttribute("NUMERIC_SHAPING");
static final AttributedCharacterIterator.Attribute BIDI_EMBEDDING = getTextAttribute("BIDI_EMBEDDING");

static final Boolean RUN_DIRECTION_LTR = (clazz == null) ? Boolean.FALSE : (Boolean)getStaticField(clazz, "RUN_DIRECTION_LTR");


private static Class<?> getClass(String name) {
try {
return Class.forName(name, true, null);
} catch (ClassNotFoundException e) {
return null;
}
}

private static Object getStaticField(Class<?> clazz, String name) {
try {
Field f = clazz.getField(name);
return f.get(null);
} catch (NoSuchFieldException | IllegalAccessException x) {
throw new AssertionError(x);
}
}

private static AttributedCharacterIterator.Attribute
getTextAttribute(String name)
{
if (clazz == null) {
// fake attribute
return new AttributedCharacterIterator.Attribute(name) { };
} else {
return (AttributedCharacterIterator.Attribute)getStaticField(clazz, name);
}
}
}

}

最佳答案

事实证明,在这种情况下根本不需要实现 AttributedCharacterIterator,因为 String 对象可以转换为 AttributedString,而后者可以转换为 AttributedCharacterIterator 对象。

关于java - LineBreakMeasurer 中没有堆栈跟踪的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50105156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com