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

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

Description

Determines the width of the given segment of text taking tabs into consideration.

License

Open Source License

Parameter

Parameter Description
s the source of the text
metrics the font metrics to use for the calculation
x the X origin >= 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 width of the text

Declaration

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

Method Source Code

//package com.java2s;
/*/*from  w w w.  j  a  v a 2 s  .  co 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 {
    /**
     * Determines the width of the given segment of text taking tabs 
     * into consideration.  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 x  the X origin >= 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 width of the text
     */
    public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e,
            int startOffset) {
        int nextX = x;
        char[] txt = s.array;
        int txtOffset = s.offset;
        int n = s.offset + s.count;
        for (int i = txtOffset; i < n; i++) {
            if (txt[i] == '\t') {
                if (e != null) {
                    nextX = (int) e.nextTabStop((float) nextX, startOffset + i - txtOffset);
                } else {
                    nextX += metrics.charWidth(' ');
                }
            } else if (txt[i] != '\n') {
                nextX += metrics.charWidth(txt[i]);
            }
            // Ignore newlines, they take up space and we shouldn't be
            // counting them.
        }
        return nextX - x;
    }
}

Related

  1. getFontMetrics(JComponent c, Graphics g, Font font)
  2. getFRC(JComponent c, FontMetrics fm)
  3. getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)
  4. getMethodGetFontMetrics()
  5. getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
  6. getTextBounds(FontMetrics fm, String s)
  7. getWidthOfText(FontMetrics fontMetrics, String text)
  8. 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)
  9. stringWidth(JComponent c, FontMetrics fm, String string)