Example usage for org.apache.poi.ss.usermodel Font getFontHeightInPoints

List of usage examples for org.apache.poi.ss.usermodel Font getFontHeightInPoints

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Font getFontHeightInPoints.

Prototype

short getFontHeightInPoints();

Source Link

Document

Get the font height in points.

Usage

From source file:uk.co.spudsoft.birt.emitters.excel.StyleManagerUtils.java

License:Open Source License

/**
 * Calculate the height of a string formatted according to a set of RichTextRuns and fitted within a give width.
 * @param sourceText/*from ww  w . j av a  2 s . co  m*/
 * The string to be measured.
 * @param defaultFont
 * The font to be used prior to the first RichTextRun.
 * @param widthMM
 * The width of the output.
 * @param richTextRuns
 * The list of RichTextRuns to be applied to the string
 * @return
 * The heigh, in points, of a box big enough to contain the formatted sourceText.
 */
public float calculateTextHeightPoints(String sourceText, Font defaultFont, double widthMM,
        List<RichTextRun> richTextRuns) {
    log.debug("Calculating height for ", sourceText);

    final float widthPt = (float) (72 * Math.max(0, widthMM - 6) / 25.4);

    float totalHeight = 0;
    String[] textLines = sourceText.split("\n");
    int lineStartIndex = 0;
    String lastLine = null;
    Font font = defaultFont;
    for (String textLine : textLines) {
        if (lastLine != null) {
            lineStartIndex += lastLine.length() + 1;
        }
        lastLine = textLine;

        AttributedString attrString = new AttributedString(textLine.isEmpty() ? " " : textLine);
        int runEnd = textLine.length();

        int richTextRunIndex = getRichTextRunIndexForStart(richTextRuns, lineStartIndex);
        if (richTextRunIndex >= 0) {
            font = richTextRuns.get(richTextRunIndex).font;
            if ((richTextRunIndex < richTextRuns.size() - 1)
                    && (richTextRuns.get(richTextRunIndex + 1).startIndex < runEnd)) {
                runEnd = richTextRuns.get(richTextRunIndex + 1).startIndex;
            }
        }

        log.debug("Adding attribute - [", 0, " - ", runEnd, "] = ", defaultFont.getFontName(), " ",
                defaultFont.getFontHeightInPoints(), "pt");
        addFontAttributes(attrString, font, 0, textLine.isEmpty() ? 1 : runEnd);

        for (++richTextRunIndex; (richTextRunIndex < richTextRuns.size())
                && (richTextRuns.get(richTextRunIndex).startIndex < lineStartIndex
                        + textLine.length()); ++richTextRunIndex) {
            RichTextRun run = richTextRuns.get(richTextRunIndex);
            RichTextRun nextRun = richTextRunIndex < richTextRuns.size() - 1
                    ? richTextRuns.get(richTextRunIndex + 1)
                    : null;
            if ((run.startIndex >= lineStartIndex)
                    && (run.startIndex < lineStartIndex + textLine.length() + 1)) {
                int startIdx = run.startIndex - lineStartIndex;
                int endIdx = (nextRun == null ? sourceText.length() : nextRun.startIndex) - lineStartIndex;
                if (endIdx > textLine.length()) {
                    endIdx = textLine.length();
                }
                if (startIdx < endIdx) {
                    log.debug("Adding attribute: [", startIdx, " - ", endIdx, "] = ", run.font.getFontName(),
                            " ", run.font.getFontHeightInPoints(), "pt");
                    addFontAttributes(attrString, run.font, startIdx, endIdx);
                }
            }
        }

        LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(), frc);

        float heightAdjustment = 0.0F;
        int lineLength = textLine.isEmpty() ? 1 : textLine.length();
        while (measurer.getPosition() < lineLength) {
            TextLayout layout = measurer.nextLayout(widthPt);
            float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
            if (layout.getDescent() + layout.getLeading() > heightAdjustment) {
                heightAdjustment = layout.getDescent() + layout.getLeading();
            }
            log.debug("Line: ", textLine, " gives height ", lineHeight, "(", layout.getAscent(), "/",
                    layout.getDescent(), "/", layout.getLeading(), ")");
            totalHeight += lineHeight;
        }
        totalHeight += heightAdjustment;

    }
    log.debug("Height calculated as ", totalHeight);
    return totalHeight;
}