Here you can find the source of getLineBreakIndex(Graphics g, String text, int index, double maxWidth)
Parameter | Description |
---|---|
g | graphics target |
text | string to render |
index | last line break index |
maxWidth | maximum width of rendered line |
public static int getLineBreakIndex(Graphics g, String text, int index, double maxWidth)
//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; } }