Java FontMetrics getLineBreakIndex(Graphics g, String text, int index, double maxWidth)

Here you can find the source of getLineBreakIndex(Graphics g, String text, int index, double maxWidth)

Description

Computes the index of the next line break.

License

Open Source License

Parameter

Parameter Description
g graphics target
text string to render
index last line break index
maxWidth maximum width of rendered line

Return

index of next line break

Declaration

public static int getLineBreakIndex(Graphics g, String text, int index, double maxWidth) 

Method Source Code

//package com.java2s;
/* Copyright 2011 David Hadka
 * //from  www.  j  av a2s  .c o  m
 * This file is part of DGantt.
 * 
 * DGantt is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * DGantt is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for 
 * more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with the DGantt.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.FontMetrics;
import java.awt.Graphics;

public class Main {
    /**
     * Computes the index of the next line break.  Similar to LineBreakMeasurer
     * but modified due to a flaw in Sun's implementation.
     * 
     * @param g graphics target
     * @param text string to render
     * @param index last line break index
     * @param maxWidth maximum width of rendered line
     * @return index of next line break
     */
    public static int getLineBreakIndex(Graphics g, String text, int index, double maxWidth) {
        FontMetrics fm = g.getFontMetrics();
        int i = index;
        double width = 0.0;

        //find last index of satisfiable width
        while (i < text.length()) {
            int codePoint = text.codePointAt(i);
            width += fm.charWidth(codePoint);

            if (width >= maxWidth) { //&& Character.isWhitespace(codePoint))
                break;
            }

            i++;
        }

        if (i == text.length()) {
            return i;
        }

        //find last whitespace index
        while (i > index) {
            int codePoint = text.codePointAt(i);

            if (!Character.isLetterOrDigit(codePoint)) {
                break;
            }

            i--;
        }

        return i;
    }
}

Related

  1. fitString(String s, Graphics2D g2, Rectangle2D rect)
  2. getAllStylesSameWidthsFontsFamillyName()
  3. getCenterOffset(Graphics g, Font f, char ch)
  4. getComponentAverageCharacterWidth(Component component)
  5. getConsoleFontMetrics(Font font)
  6. getLineHeight(Component c, int defaultHeight)
  7. getLineHeight(Component c, int defaultHeight)
  8. getLineHeight(Component component, int count)
  9. getLineHeight(FontMetrics fm)