LineBreakMeasurer

时间:2018-04-30 17:11:20

标签: java printing classcastexception

我正在尝试用从右到左的语言(阿拉伯语)打印一个合理的段落,并尝试使用 TextLayout LineBreakerMeasurer 来实现这一点,因为围绕着网络似乎是试图编码每个单词/字符的位置的计算是一个很大的时间和精力的浪费(我已经尝试过它并且没有多少运气)。

我遇到的问题是正确实现 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);
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

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