get Printed Size - Java java.awt

Java examples for java.awt:Graphics2D

Description

get Printed Size

Demo Code


import org.apache.log4j.Logger;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.util.*;
import java.util.regex.Pattern;

public class Main{
    private static final Logger LOG = Logger.getLogger(DrawingUtil.class);
    private static final Set<Character> breaks;
    public static final Dimension getPrintedSize(String string, Font f,
            Graphics2D g, double allowedWidth) {
        int totalHeight = 0;
        for (String each : getLinesThatFit(string, f, g, allowedWidth)) {
            totalHeight += getTextHeight(g, each, f);
        }//w ww  .  j av  a 2 s  .c o  m
        return new Dimension((int) allowedWidth, totalHeight);
    }
    public static final List<String> getLinesThatFit(String string, Font f,
            Graphics2D g, double allowedWidth) {
        LOG.trace(String.format(
                "Breaking string %s into lines that fit %s", string,
                allowedWidth));
        if (string == null) {
            List<String> lines = new LinkedList<String>();
            lines.add("");
            return lines;
        }
        char[] chars = string.toCharArray();
        int lastLineBreak = 0;
        int lastBreakpoint = -1;
        float lineWidth = 0;
        String buffer = "";
        List<String> lines = new LinkedList<String>();
        for (int i = 0; i < chars.length; i++) {
            char each = chars[i];
            if (isLineBreak(each)) {
                lines.add(string.substring(lastLineBreak, i + 1));
                buffer = "";
                lastLineBreak = i;
                lastBreakpoint = -1;
                lineWidth = 0;
                LOG.trace("Found line break char");
                continue;
            }
            if (Character.isSpaceChar(each)) {
                LOG.trace("Found space");
                lastBreakpoint = i;
            }

            buffer += each;
            lineWidth = getTextWidth(g, buffer, f);
            if (i == chars.length - 1) {
                LOG.trace("Reached end");
                lines.add(string.substring(lastLineBreak));
            }
            LOG.trace(String.format("String width: %s  Allowed: %s",
                    lineWidth, allowedWidth));
            if (lineWidth <= allowedWidth) {
                LOG.trace(String.format("String %s fits", buffer));
                continue;
            }
            int breakAt = (lastBreakpoint > 0) ? lastBreakpoint : i;
            lines.add(string.substring(lastLineBreak, breakAt + 1));
            buffer = "";
            lastLineBreak = breakAt;
            lastBreakpoint = -1;
            i = breakAt;
            lineWidth = 0;
        }
        ListIterator<String> li = lines.listIterator();
        while (li.hasNext()) {
            String each = li.next();
            while (each.startsWith(" ")
                    || (!each.isEmpty() && isLineBreak(each.charAt(0)))) {
                each = each.substring(1);
            }
            li.set(each);
        }
        return lines;
    }
    public static final float getTextHeight(Graphics2D g, String string,
            Font font) {
        LineMetrics lm = font.getLineMetrics(string,
                g.getFontRenderContext());
        return lm.getHeight();
    }
    private static boolean isLineBreak(char c) {
        return breaks.contains(c);
    }
    public static final float getTextWidth(Graphics2D g, String string,
            Font font) {
        return g.getFontMetrics(font).stringWidth(string);
    }
}

Related Tutorials