Java Font Text Width cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics, String suffix)

Here you can find the source of cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics, String suffix)

Description

Returns a shortened string, that fits into the given space with the specified font on the given graphics object considering a possible suffix.

License

Open Source License

Parameter

Parameter Description
text a parameter
spaceForText a parameter
font a parameter
graphics a parameter
suffix a parameter

Declaration

public static String cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics,
        String suffix) 

Method Source Code

//package com.java2s;
/*/* w  ww.ja  v  a  2s .  c o m*/
 * funCKit - functional Circuit Kit
 * Copyright (C) 2013  Lukas Elsner <open@mindrunner.de>
 * Copyright (C) 2013  Peter Dahlberg <catdog2@tuxzone.org>
 * Copyright (C) 2013  Julian Stier <mail@julian-stier.de>
 * Copyright (C) 2013  Sebastian Vetter <mail@b4sti.eu>
 * Copyright (C) 2013  Thomas Poxrucker <poxrucker_t@web.de>
 * Copyright (C) 2013  Alexander Treml <alex.treml@directbox.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

public class Main {
    /**
     * Returns a shortened string, that fits into the given space with the
     * specified font on the given graphics object considering a possible
     * suffix.
     *
     * @param text
     * @param spaceForText
     * @param font
     * @param graphics
     * @param suffix
     * @return
     */
    public static String cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics,
            String suffix) {
        FontMetrics metrics = graphics.getFontMetrics(font);
        int textWidth = metrics.stringWidth(text);
        StringBuilder actualText = new StringBuilder(text);
        int actualTextWidth = metrics.stringWidth(text);
        if (textWidth > spaceForText) {
            int suffixWidth = 0;
            if (!suffix.isEmpty()) {
                suffixWidth = metrics.stringWidth(suffix);
            }
            actualTextWidth += suffixWidth;
            int length = actualText.length();
            while (actualTextWidth > spaceForText && length > 0) {
                actualText.deleteCharAt(length - 1);
                actualTextWidth = metrics.stringWidth(actualText.toString()) + suffixWidth;
                length = actualText.length();
            }
            actualText.append(suffix);
        }
        return actualText.toString();
    }
}

Related

  1. buildWindowsCompressedLabelName(int allocatedWidth, FontMetrics fm, String name, String surname)
  2. charsWidth(char[] data, int off, int len, Font font)
  3. charWidth(char ch, Font font)
  4. computeStringWidth(FontMetrics fm, String str)
  5. findStringLimit(String aString, Font aFont, int aWidth)
  6. findWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth, float[] outWidth)
  7. fontWidth(final Graphics graphics, final Font font, final String str)
  8. getStringWidth(final Font f, final String s)