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:org.apache.metamodel.excel.ExcelUtils.java

License:Apache License

public static Style getCellStyle(Workbook workbook, Cell cell) {
    if (cell == null) {
        return Style.NO_STYLE;
    }/*from w ww.  j av  a  2  s . c  om*/
    final CellStyle cellStyle = cell.getCellStyle();

    final short fontIndex = cellStyle.getFontIndex();
    final Font font = workbook.getFontAt(fontIndex);
    final StyleBuilder styleBuilder = new StyleBuilder();

    // Font bold, italic, underline
    if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) {
        styleBuilder.bold();
    }
    if (font.getItalic()) {
        styleBuilder.italic();
    }
    if (font.getUnderline() != FontUnderline.NONE.getByteValue()) {
        styleBuilder.underline();
    }

    // Font size
    final Font stdFont = workbook.getFontAt((short) 0);
    final short fontSize = font.getFontHeightInPoints();
    if (stdFont.getFontHeightInPoints() != fontSize) {
        styleBuilder.fontSize(fontSize, SizeUnit.PT);
    }

    // Font color
    final short colorIndex = font.getColor();
    if (font instanceof HSSFFont) {
        if (colorIndex != HSSFFont.COLOR_NORMAL) {
            final HSSFWorkbook wb = (HSSFWorkbook) workbook;
            HSSFColor color = wb.getCustomPalette().getColor(colorIndex);
            if (color != null) {
                short[] triplet = color.getTriplet();
                styleBuilder.foreground(triplet);
            }
        }
    } else if (font instanceof XSSFFont) {
        XSSFFont xssfFont = (XSSFFont) font;

        XSSFColor color = xssfFont.getXSSFColor();
        if (color != null) {
            String argbHex = color.getARGBHex();
            if (argbHex != null) {
                styleBuilder.foreground(argbHex.substring(2));
            }
        }
    } else {
        throw new IllegalStateException(
                "Unexpected font type: " + (font == null ? "null" : font.getClass()) + ")");
    }

    // Background color
    if (cellStyle.getFillPattern() == 1) {
        Color color = cellStyle.getFillForegroundColorColor();
        if (color instanceof HSSFColor) {
            short[] triplet = ((HSSFColor) color).getTriplet();
            if (triplet != null) {
                styleBuilder.background(triplet);
            }
        } else if (color instanceof XSSFColor) {
            String argb = ((XSSFColor) color).getARGBHex();
            if (argb != null) {
                styleBuilder.background(argb.substring(2));
            }
        } else {
            throw new IllegalStateException(
                    "Unexpected color type: " + (color == null ? "null" : color.getClass()) + ")");
        }
    }

    // alignment
    switch (cellStyle.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        styleBuilder.leftAligned();
        break;
    case CellStyle.ALIGN_RIGHT:
        styleBuilder.rightAligned();
        break;
    case CellStyle.ALIGN_CENTER:
        styleBuilder.centerAligned();
        break;
    case CellStyle.ALIGN_JUSTIFY:
        styleBuilder.justifyAligned();
        break;
    }

    return styleBuilder.create();
}

From source file:org.joeffice.spreadsheet.cell.CellRenderer.java

License:Apache License

public static void decorateComponent(Cell cell, JComponent renderingComponent, JComponent defaultRenderer) {
    CellStyle style = cell.getCellStyle();

    // Background neither the index or the color works for XSSF cells
    Color backgroundColor = CellUtils.poiToAwtColor(style.getFillBackgroundColorColor());
    if (backgroundColor != null) {
        renderingComponent.setBackground(backgroundColor);
    } else {/*from  www  .  ja  va  2 s  .c  om*/
        renderingComponent.setBackground(defaultRenderer.getBackground());
    }

    // Font and forground
    short fontIndex = style.getFontIndex();
    if (fontIndex > 0) {
        Font xlsFont = cell.getSheet().getWorkbook().getFontAt(fontIndex);
        java.awt.Font font = java.awt.Font.decode(xlsFont.getFontName());
        font = font.deriveFont((float) xlsFont.getFontHeightInPoints());
        font = font.deriveFont(java.awt.Font.PLAIN);
        if (xlsFont.getItalic()) {
            font = font.deriveFont(java.awt.Font.ITALIC);
        }
        if (xlsFont.getBoldweight() == Font.BOLDWEIGHT_BOLD) {
            font = font.deriveFont(java.awt.Font.BOLD);
        }
        if (xlsFont.getUnderline() > Font.U_NONE) {
            // no underline in fonts
        }
        short fontColorIndex = xlsFont.getColor();
        Color fontColor = CellUtils.shortToColor(fontColorIndex);
        if (fontColor != null) {
            renderingComponent.setForeground(fontColor);
        } else {
            renderingComponent.setForeground(defaultRenderer.getForeground());
        }
        renderingComponent.setFont(font);
    } else {
        renderingComponent.setForeground(defaultRenderer.getForeground());
        renderingComponent.setFont(defaultRenderer.getFont());
    }

    // Borders
    // At the moment done in renderer but should be done with a JLayer to paint over the grid
    renderingComponent.setBorder(new CellBorder(cell));

    if (cell.getCellComment() != null) {
        renderingComponent.setToolTipText(cell.getCellComment().getString().getString());
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.HSSFFontWrapper.java

License:Open Source License

/**
 * Creates a HSSFFontWrapper for the excel font.
 *
 * @param font/*from   w  ww  .  java2  s.co m*/
 *          the font.
 */
public HSSFFontWrapper(final Font font) {
    if (font == null) {
        throw new NullPointerException("Font is null");
    }
    if (font.getColor() < 0) {
        throw new IllegalArgumentException("Negative color index is not allowed");
    }

    fontName = normalizeFontName(font.getFontName());
    fontHeight = font.getFontHeightInPoints();
    bold = font.getBold();
    italic = font.getItalic();
    underline = (font.getUnderline() != HSSFFont.U_NONE);
    strikethrough = font.getStrikeout();
    colorIndex = font.getColor();
}

From source file:org.tiefaces.components.websheet.utility.CellStyleUtility.java

License:MIT License

/**
 * Gets the row style.//from  www  .ja  v a  2s . co m
 *
 * @param wb
 *            the wb
 * @param poiCell
 *            the poi cell
 * @param inputType
 *            the input type
 * @param rowHeight
 *            the row height
 * @param rowspan
 *            the rowspan
 * @return the row style
 */
public static String getRowStyle(final Workbook wb, final Cell poiCell, final String inputType,
        final float rowHeight, final int rowspan) {

    CellStyle cellStyle = poiCell.getCellStyle();
    if ((cellStyle != null) && (rowspan == 1)) {
        short fontIdx = cellStyle.getFontIndex();
        Font font = wb.getFontAt(fontIdx);
        float maxHeight = rowHeight;
        if (!inputType.isEmpty()) {
            maxHeight = Math.min(font.getFontHeightInPoints() + 8f, rowHeight);
        }
        return "height:" + WebSheetUtility.pointsToPixels(maxHeight) + "px;";
    }
    return "";
}

From source file:org.tiefaces.components.websheet.utility.CellStyleUtility.java

License:MIT License

/**
 * Gets the cell font style./*from  ww  w  . ja v a 2 s. c o m*/
 *
 * @param wb
 *            the wb
 * @param poiCell
 *            the poi cell
 * @return the cell font style
 */
public static String getCellFontStyle(final Workbook wb, final Cell poiCell) {

    CellStyle cellStyle = poiCell.getCellStyle();
    StringBuilder webStyle = new StringBuilder();
    if (cellStyle != null) {
        short fontIdx = cellStyle.getFontIndex();
        Font font = wb.getFontAt(fontIdx);
        if (font.getItalic()) {
            webStyle.append("font-style: italic;");
        }
        if (font.getBold()) {
            webStyle.append("font-weight: bold;");
        }
        webStyle.append("font-size: " + font.getFontHeightInPoints() + "pt;");
        String decoration = getCellFontDecoration(font);
        if (decoration.length() > 0) {
            webStyle.append("text-decoration:" + decoration + ";");
        }
        webStyle.append(getCellFontColor(font));

    }
    return webStyle.toString();

}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private void fillFont(CFont font, Font excelFont) {
    font.setName(excelFont.getFontName());

    // TODO    CFont font
    //font.setColor( excelFont.getColor() );

    font.setHeight(excelFont.getFontHeight());
    font.setHeightInPoints(excelFont.getFontHeightInPoints());

    // TODO ?  ? Boldweight, ?? ? ? 
    short boldWeight = excelFont.getBoldweight();
    if (boldWeight >= 700)
        font.setBold(true);/*from   www. j  a v a  2  s .  co m*/

    font.setItalic(excelFont.getItalic());
    font.setStrikeout(excelFont.getStrikeout());

    byte underline = excelFont.getUnderline();
    if (underline != Font.U_NONE)
        font.setUnderline(true);
    if (underline == Font.U_DOUBLE || underline == Font.U_DOUBLE_ACCOUNTING)
        font.setDoubleUnderline(true);
}

From source file:ru.spb.nicetu.tableviewer.server.XlsToHtml.java

License:Apache License

private void fontStyle(CellStyle style, Formatter out, boolean isBuiltIn) {
    Font font = wb.getFontAt(style.getFontIndex());

    if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_BOLD)
        styleOut("font-weight", "bold", out, isBuiltIn);
    if (font.getItalic())
        styleOut("font-style", "italic", out, isBuiltIn);
    if (font.getFontName() != null && !"".equals(font.getFontName()))
        styleOut("font-family", font.getFontName(), out, isBuiltIn);

    int fontheight = font.getFontHeightInPoints();
    if (fontheight == 9) {
        //fix for stupid ol Windows
        fontheight = 10;/*  w  w  w .j a  v  a 2  s  .  co  m*/
    }
    styleOut("font-size", "" + fontheight + "pt", out, isBuiltIn);

    // Font color is handled with the other colors
}

From source file:sol.neptune.elisaboard.service.VPlanToHtml.java

License:Apache License

private void fontStyle(CellStyle style) {
    Font font = wb.getFontAt(style.getFontIndex());

    if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_NORMAL) {
        out.format("  font-weight: bold;%n");
    }/*from  w  w  w . ja  v a  2s .c  om*/
    if (font.getItalic()) {
        out.format("  font-style: italic;%n");
    }

    int fontheight = font.getFontHeightInPoints();
    if (fontheight == 9) {
        //fix for stupid ol Windows
        fontheight = 10;
    }
    out.format("  font-size: %dpt;%n", fontheight);

    // Font color is handled with the other colors
}

From source file:uk.co.certait.test.ExcelToHtmlConverter.java

License:Apache License

private void fontStyle(CellStyle style) {
    Font font = wb.getFontAt(style.getFontIndex());

    if (font.getBold()) {
        out.format("  font-weight: bold;%n");
    }/*  w w  w.j a  va 2  s. c o  m*/
    if (font.getItalic()) {
        out.format("  font-style: italic;%n");
    }

    int fontheight = font.getFontHeightInPoints();
    if (fontheight == 9) {
        // fix for stupid ol Windows
        fontheight = 10;
    }
    out.format("  font-size: %dpt;%n", fontheight);

    // Font color is handled with the other colors
}

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

License:Open Source License

/**
 * Add font details to an AttributedString.
 * @param attrString// ww  w  .ja  va 2 s.c  o  m
 * The AttributedString to modify.
 * @param font
 * The font to take attributes from.
 * @param startIdx
 * The index of the first character to be attributed (inclusive).
 * @param endIdx
 * The index of the last character to be attributed (inclusive). 
 */
protected void addFontAttributes(AttributedString attrString, Font font, int startIdx, int endIdx) {
    attrString.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
    attrString.addAttribute(TextAttribute.SIZE, (float) font.getFontHeightInPoints(), startIdx, endIdx);
    if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD)
        attrString.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
    if (font.getItalic())
        attrString.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
    if (font.getUnderline() == Font.U_SINGLE)
        attrString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
}