Java Font Text Width findWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth, float[] outWidth)

Here you can find the source of findWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth, float[] outWidth)

Description

Determines how many characters may be laid in sequence before reaching a setOn width.

License

Open Source License

Parameter

Parameter Description
metrics Font metrics used for layout..
chars Input array of chars.
off Offset into array of chars.
len Number of chars in input.
maxWidth Maximum width for layout.
outWidth outWidth[0] will hold the exact width of the fitted characters on return. May be null May be null .

Return

The number of characters that fit within the specified width.

Declaration

public static int findWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth,
        float[] outWidth) 

Method Source Code


//package com.java2s;
/*/*from   ww w  .  ja  va 2  s.  co  m*/
 * Copyright (c) 2014. Massachusetts Institute of Technology
 * Released under the BSD 2-Clause License
 * http://opensource.org/licenses/BSD-2-Clause
 */

import java.awt.*;

public class Main {
    /**
     * Determines how many characters may be laid in sequence 
     * before reaching a setOn width.
     * 
     * @param metrics   Font metrics used for layout..
     * @param chars     Input array of chars.
     * @param off       Offset into array of chars.
     * @param len       Number of chars in input.
     * @param maxWidth  Maximum width for layout.
     * @param outWidth  {@code outWidth[0]} will hold the exact width
     *                  of the fitted characters on return. May be {@code null}
     *                  May be {@code null}.
     *                  
     * @return The number of characters that fit within the specified width.
     */
    public static int findWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth,
            float[] outWidth) {
        int i = 0;
        int lineWidth = 0;
        for (; i < len; i++) {
            int charWidth = metrics.charWidth(chars[i + off]);
            lineWidth += charWidth;
            if (lineWidth > maxWidth) {
                lineWidth -= charWidth;
                break;
            }
        }

        if (outWidth != null) {
            outWidth[0] = lineWidth;
        }

        return i;

        /*
        // Version to use if kerning tables become available.
        if( len == 0 ) {
        if( outWidth != null ) { 
        outWidth[0] = 0f;
        }
        return 0;
        }
            
        float width = metrics.charWidth( chars[off] );
        if( width > maxWidth ) {
        if( outWidth != null ) {
        outWidth[0] = 0f;
        }
        return 0;
        }
            
        float prevAdvance = width;
            
        for( int i = 1; i < len; i++ ) {
        float advance = metrics.charsWidth( chars, off + i - 1, 2 );
        prevAdvance   = advance - prevAdvance;
        width += prevAdvance;
            
        if( width > maxWidth ) {
        if( outWidth != null ) {
            outWidth[0] = width - prevAdvance;
        }
        return i;
        }
        }
            
        if( outWidth != null ) {
        outWidth[0] = width;
        }
        return len;
        */
    }

    /**
     * Determines how many characters may be laid in sequence 
     * before reaching a setOn width.
     * 
     * @param metrics   Font metrics used for layout..
     * @param seq       Input CharSequence to layout.
     * @param off       Offset into seq where input starts.
     * @param len       Number of chars in input.
     * @param maxWidth  Maximum width for layout.
     * @param outWidth  {@code outWidth[0]} will hold the exact width
     *                  the exact width of the fitted characters on return.
     *                  May be {@code null}.
     *                  
     * @return The number of characters that fit within the specified width.
     */
    public static int findWidth(FontMetrics metrics, CharSequence seq, int off, int len, float maxWidth,
            float[] outWidth) {
        int i = 0;
        int lineWidth = 0;
        for (; i < len; i++) {
            int charWidth = metrics.charWidth(seq.charAt(i + off));
            lineWidth += charWidth;
            if (lineWidth > maxWidth) {
                lineWidth -= charWidth;
                break;
            }
        }

        if (outWidth != null) {
            outWidth[0] = lineWidth;
        }

        return i;

        /*
        // Version to use if kerning tables become available.
        if( len == 0 ) {
        if( outWidth != null ) {
        outWidth[0] = 0f;
        }
        return 0;
        }
            
        char[] arr = { 0, seq.charAt( off ) };
        float width = metrics.charsWidth( arr, 1, 1 );
        if( width > maxWidth ) {
        if( outWidth[0] != 0f ) {
        outWidth[0] = 0f;
        }
        return 0;
        }
            
        float prevAdvance = width;
            
        for( int i = 1; i < len; i++ ) {
        arr[0] = arr[1];
        arr[1] = seq.charAt( i + off );
        float advance = metrics.charsWidth( arr, 0, 2 );
        prevAdvance   = advance - prevAdvance;
        width += prevAdvance;
            
        if( width > maxWidth ) {
        if( outWidth != null ) {
            outWidth[0] = width - prevAdvance;
        }
        return i;
        }
        }
            
        if( outWidth != null ) {
        outWidth[0] = width;
        }
        return len;
        */
    }
}

Related

  1. charsWidth(char[] data, int off, int len, Font font)
  2. charWidth(char ch, Font font)
  3. computeStringWidth(FontMetrics fm, String str)
  4. cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics, String suffix)
  5. findStringLimit(String aString, Font aFont, int aWidth)
  6. fontWidth(final Graphics graphics, final Font font, final String str)
  7. getStringWidth(final Font f, final String s)
  8. getStringWidth(Font font, String text)
  9. getStringWidth(FontMetrics f, String s)