Here you can find the source of getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
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 |
public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
//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; } }