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

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

Description

Determine where to break the given text to fit within the the given span.

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.
x the target view location to translate to an offset into the text.
e how to expand the tabs. If this value is null, tabs will be expanded as a space character.
startOffset starting offset in the document of the text

Return

the offset into the given text

Declaration

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

Method Source Code

//package com.java2s;
/*//  w  w w .  jav a 2s. c  o m
 * @(#)Utilities.java   1.40 03/01/23
 *
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.awt.FontMetrics;

import javax.swing.text.*;

public class Main {
    /**
     * Determine where to break the given text to fit
     * within the the given span.  This tries to find a
     * whitespace boundary.
     * @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.
     * @param x  the target view location to translate to an
     *   offset into the text.
     * @param e  how to expand the tabs.  If this value is null, 
     *   tabs will be expanded as a space character.
     * @param startOffset starting offset in the document of the text
     * @return  the offset into the given text
     */
    public static final int getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e,
            int startOffset) {
        char[] txt = s.array;
        int txtOffset = s.offset;
        int txtCount = s.count;
        int index = Utilities.getTabbedTextOffset(s, metrics, x0, x, e, startOffset, false);
        for (int i = txtOffset + Math.min(index, txtCount - 1); i >= txtOffset; i--) {

            char ch = txt[i];
            if (Character.isWhitespace(ch)) {
                // found whitespace, break here
                index = i - txtOffset + 1;
                break;
            }
        }
        return index;
    }

    /**
     * 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. clipStringIfNecessary(JComponent c, FontMetrics fm, String string, int availTextWidth)
  2. clipText(FontMetrics fm, String val, int width)
  3. computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)
  4. computeStringWidth(FontMetrics fontMetrics, String str)
  5. drawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)
  6. getClippedText(String text, FontMetrics fm, int maxWidth)
  7. getClippedText(String text, FontMetrics fm, int maxWidth)
  8. getClippedText(String text, FontMetrics fm, int maxWidth)
  9. getDefaultLabelFontMetrics()