Java 怎么获取打印尺寸

2019年8月20日22:29:11 评论 21

1.简介

Java 怎么获取打印尺寸?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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);
        }
        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);
    }
}

 

3.总结

示例代码演示了 Java 怎么获取打印尺寸,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: