Java Font Text Width wrapLineInto(String line, List list, FontMetrics fm, int maxWidth)

Here you can find the source of wrapLineInto(String line, List list, FontMetrics fm, int maxWidth)

Description

Given a line of text and font metrics information, wrap the line and add the new line(s) to list.

License

Apache License

Parameter

Parameter Description
line a line of text
list an output list of strings
fm font metrics
maxWidth maximum width of the line(s)

Declaration

public static void wrapLineInto(String line, List<String> list, FontMetrics fm, int maxWidth) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.FontMetrics;
import java.util.*;

public class Main {
    /**//from w ww.  j  a  v a2 s .  c  om
     * Given a line of text and font metrics information, wrap the line and
     * add the new line(s) to <var>list</var>.
     *
     * @param line a line of text
     * @param list an output list of strings
     * @param fm font metrics
     * @param maxWidth maximum width of the line(s)
     */
    public static void wrapLineInto(String line, List<String> list, FontMetrics fm, int maxWidth) {
        int len = line.length();
        int width;
        while (len > 0 && (width = fm.stringWidth(line)) > maxWidth) {
            // Guess where to split the line. Look for the next space before
            // or after the guess.
            int guess = len * maxWidth / width;
            String before = line.substring(0, guess).trim();

            width = fm.stringWidth(before);
            int pos;
            if (width > maxWidth) // Too long
                pos = findBreakBefore(line, guess);
            else { // Too short or possibly just right
                pos = findBreakAfter(line, guess);
                if (pos != -1) { // Make sure this doesn't make us too long
                    before = line.substring(0, pos).trim();
                    if (fm.stringWidth(before) > maxWidth)
                        pos = findBreakBefore(line, guess);
                }
            }
            if (pos == -1)
                pos = guess; // Split in the middle of the word

            list.add(line.substring(0, pos).trim());
            line = line.substring(pos).trim();
            len = line.length();
        }
        if (len > 0)
            list.add(line);
    }

    /**
     * Returns the index of the first whitespace character or '-' in
     * <var>line</var> that is at or before <var>start</var>. Returns -1 if no
     * such character is found.
     *
     * @param line a string
     * @param start where to star looking
     */
    public static int findBreakBefore(String line, int start) {
        for (int i = start; i >= 0; --i) {
            char c = line.charAt(i);
            if (Character.isWhitespace(c) || c == '-')
                return i;
        }
        return -1;
    }

    /**
     * Returns the index of the first whitespace character or '-' in
     * <var>line</var> that is at or after <var>start</var>. Returns -1 if no
     * such character is found.
     *
     * @param line a string
     * @param start where to star looking
     */
    public static int findBreakAfter(String line, int start) {
        int len = line.length();
        for (int i = start; i < len; ++i) {
            char c = line.charAt(i);
            if (Character.isWhitespace(c) || c == '-')
                return i;
        }
        return -1;
    }
}

Related

  1. stringWidth(Graphics g, String s)
  2. stringWidth(Graphics2D g, String s)
  3. stringWidth(java.awt.Font font, String string)
  4. stringWidth(String text, Font font)
  5. wrap(String str, FontMetrics fm, int maxWidth)