簡體   English   中英

獲取字符在JEditorPane中的絕對位置

[英]Get absolute position of character in JEditorPane

1個
我需要角色的絕對位置。 帶有:
editorPane.getFontMetrics(f).getAscent()
我到基線只有一個相對距離。 也許有一種方法可以獲取基線的絕對位置?

編輯:這是rect.y + rect.height -metrics.getDescent()-metrics.getAscent()的結果

由於一行中的所有字體都共享基線* ,因此您可以通過調用modelToView並從矩形底部減去下降和上升來計算字符的視覺位置。

由於涉及多種字體,因此JEditorPane的getFont方法顯然是不夠的。 文檔的raw元素屬性也不足夠,因為HTMLDocument的屬性僅對HTML元素本身進行建模。 但是,任何文檔位置的實際字體都可以從相應的View中獲取

static Point getLocation(int pos,
                         JEditorPane editorPane)
throws BadLocationException {

    HTMLDocument doc = (HTMLDocument) editorPane.getDocument();

    View view = editorPane.getUI().getRootView(editorPane);
    int index;
    while ((index = view.getViewIndex(pos, Position.Bias.Backward)) >= 0) {
        view = view.getView(index);
    }

    AttributeSet attr = doc.getStyleSheet().getViewAttributes(view);
    Font f = doc.getStyleSheet().getFont(attr);

    FontMetrics metrics = editorPane.getFontMetrics(f);
    Rectangle rect = editorPane.modelToView(pos);

    return new Point(rect.x,
        rect.y + rect.height - metrics.getDescent() - metrics.getAscent());
}

*為簡單起見,我忽略了帶有懸掛基線和垂直基線的字符。

編輯:由於RTFEditorKit很少使用,我錯誤地認為您正在使用HTMLEditorKit。 這將與RTF文檔一起使用:

static Point getLocation(int pos,
                     JEditorPane editorPane)
throws BadLocationException {
    StyledDocument doc = (StyledDocument) editorPane.getDocument();
    View view = editorPane.getUI().getRootView(editorPane);
    int index;
    while ((index = view.getViewIndex(pos, Position.Bias.Backward)) >= 0) {
        view = view.getView(index);
    }

    AttributeSet attr = view.getAttributes();
    Font f = doc.getFont(attr);

    FontMetrics metrics = editorPane.getFontMetrics(f);
    Rectangle rect = editorPane.modelToView(pos);

    return new Point(rect.x,
        rect.y + rect.height - metrics.getDescent() - metrics.getAscent());
}

我正在使用RTFEditorKit。 所以我更改了代碼:

static Point getLocation(int pos,
            JEditorPane editorPane)
            throws BadLocationException {

        View view = editorPane.getUI().getRootView(editorPane);
        int index;
        while ((index = view.getViewIndex(pos, Position.Bias.Backward)) >= 0) {
            view = view.getView(index);
        }

        AttributeSet set = view.getAttributes();
        if (set != null && set.getAttribute(StyleConstants.FontFamily) != null) {
            Font f = new Font((String) set.getAttribute(StyleConstants.FontFamily), Font.PLAIN, (Integer) set.getAttribute(StyleConstants.FontSize));
            FontMetrics metrics = editorPane.getFontMetrics(f);

            Rectangle rect = editorPane.modelToView(pos);
            return new Point(rect.x,
                    rect.y + rect.height - metrics.getDescent() - metrics.getAscent());
        }
        else {
            return new Point(0, 0);
        }
    }

但是我總是得到當前Font的上升,而不是最大Font的上升。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM