Java Swing Font Metrics getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)

Here you can find the source of getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)

Description

Determines the relative offset into the given text that best represents the given span in the view coordinate system.

License

Open Source License

Parameter

Parameter Description
s the source of the text
metrics the font metrics to use for the calculation
x0 the starting view location representing the start of the given text >= 0.
x the target view location to translate to an offset into the text >= 0.
e how to expand the tabs. If this value is null, tabs will be expanded as a space character.
startOffset starting offset of the text in the document >= 0

Return

the offset into the text >= 0

Declaration

public static final int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e,
        int startOffset) 

Method Source Code


//package com.java2s;
/*//from ww w . ja  va 2s. c o  m
 * @(#)Utilities.java   1.38 01/12/03
 *
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import javax.swing.text.*;

import java.awt.FontMetrics;

public class Main {
    /**
     * Determines the relative offset into the given text that
     * best represents the given span in the view coordinate
     * system.  This is implemented in a 1.1 style coordinate 
     * system where ints are used and 72dpi is assumed.
     *
     * @param s  the source of the text
     * @param metrics the font metrics to use for the calculation
     * @param x0 the starting view location representing the start
     *   of the given text >= 0.
     * @param x  the target view location to translate to an
     *   offset into the text >= 0.
     * @param e  how to expand the tabs.  If this value is null, 
     *   tabs will be expanded as a space character.
     * @param startOffset starting offset of the text in the document >= 0
     * @return  the offset into the text >= 0
     */
    public static final int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e,
            int startOffset) {
        return getTabbedTextOffset(s, metrics, x0, x, e, startOffset, true);
    }

    public static final int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e,
            int startOffset, boolean round) {
        if (x0 >= x) {
            // x before x0, return.
            return 0;
        }
        int currX = x0;
        int nextX = currX;
        // s may be a shared segment, so it is copied prior to calling
        // the tab expander
        char[] txt = s.array;
        int txtOffset = s.offset;
        int txtCount = s.count;
        int n = s.offset + s.count;
        for (int i = s.offset; i < n; i++) {
            if (txt[i] == '\t') {
                if (e != null) {
                    nextX = (int) e.nextTabStop((float) nextX, startOffset + i - txtOffset);
                } else {
                    nextX += metrics.charWidth(' ');
                }
            } else {
                nextX += metrics.charWidth(txt[i]);
            }
            if ((x >= currX) && (x < nextX)) {
                // found the hit position... return the appropriate side
                if ((round == false) || ((x - currX) < (nextX - x))) {
                    return i - txtOffset;
                } else {
                    return i + 1 - txtOffset;
                }
            }
            currX = nextX;
        }

        // didn't find, return end offset
        return txtCount;
    }
}

Related

  1. getFontMetrics(JComponent c, Graphics g, Font f)
  2. getFontMetrics(JComponent c, Graphics g, Font font)
  3. getFRC(JComponent c, FontMetrics fm)
  4. getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)
  5. getMethodGetFontMetrics()
  6. getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
  7. getTextBounds(FontMetrics fm, String s)
  8. getWidthOfText(FontMetrics fontMetrics, String text)
  9. layoutMultilineCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap, int minLines, int maxLines)