Example usage for java.awt.font TextAttribute SUPERSCRIPT_SUB

List of usage examples for java.awt.font TextAttribute SUPERSCRIPT_SUB

Introduction

In this page you can find the example usage for java.awt.font TextAttribute SUPERSCRIPT_SUB.

Prototype

Integer SUPERSCRIPT_SUB

To view the source code for java.awt.font TextAttribute SUPERSCRIPT_SUB.

Click Source Link

Document

Standard subscript.

Usage

From source file:com.dlya.facturews.DlyaPdfExporter2.java

/**
 * Creates a PDF font./*  w  w  w.ja  v  a2 s  . c  om*/
 * 
 * @param attributes the text attributes of the font
 * @param locale the locale for which to create the font
 * @param setFontLines whether to set underline and strikethrough as font style
 * @return the PDF font for the specified attributes
 */
@SuppressWarnings("deprecation")
protected Font getFont(Map<Attribute, Object> attributes, Locale locale, boolean setFontLines) {
    JRFont jrFont = new JRBaseFont(attributes);

    Exception initialException = null;

    Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND);

    // use the same font scale ratio as in JRStyledText.getAwtAttributedString
    float fontSizeScale = 1f;
    Integer scriptStyle = (Integer) attributes.get(TextAttribute.SUPERSCRIPT);
    if (scriptStyle != null && (TextAttribute.SUPERSCRIPT_SUB.equals(scriptStyle)
            || TextAttribute.SUPERSCRIPT_SUPER.equals(scriptStyle))) {
        fontSizeScale = 2f / 3;
    }

    Font font = null;
    PdfFont pdfFont = null;
    FontKey key = new FontKey(jrFont.getFontName(), jrFont.isBold(), jrFont.isItalic());

    if (fontMap != null && fontMap.containsKey(key)) {
        pdfFont = pdfFontMap.get(key);
    } else {
        FontInfo fontInfo = FontUtil.getInstance(jasperReportsContext).getFontInfo(jrFont.getFontName(),
                locale);
        if (fontInfo == null) {
            //fontName NOT found in font extensions
            pdfFont = new PdfFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded());
        } else {
            //fontName found in font extensions
            FontFamily family = fontInfo.getFontFamily();
            FontFace face = fontInfo.getFontFace();
            int faceStyle = java.awt.Font.PLAIN;

            if (face == null) {
                //fontName matches family name in font extension
                if (jrFont.isBold() && jrFont.isItalic()) {
                    face = family.getBoldItalicFace();
                    faceStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC;
                }

                if (face == null && jrFont.isBold()) {
                    face = family.getBoldFace();
                    faceStyle = java.awt.Font.BOLD;
                }

                if (face == null && jrFont.isItalic()) {
                    face = family.getItalicFace();
                    faceStyle = java.awt.Font.ITALIC;
                }

                if (face == null) {
                    face = family.getNormalFace();
                    faceStyle = java.awt.Font.PLAIN;
                }

                //               if (face == null)
                //               {
                //                  throw new JRRuntimeException("Font family '" + family.getName() + "' does not have the normal font face.");
                //               }
            } else {
                //fontName matches face name in font extension; not family name
                faceStyle = fontInfo.getStyle();
            }

            String pdfFontName = null;
            int pdfFontStyle = java.awt.Font.PLAIN;
            if (jrFont.isBold() && jrFont.isItalic()) {
                pdfFontName = family.getBoldItalicPdfFont();
                pdfFontStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC;
            }

            if (pdfFontName == null && jrFont.isBold()) {
                pdfFontName = family.getBoldPdfFont();
                pdfFontStyle = java.awt.Font.BOLD;
            }

            if (pdfFontName == null && jrFont.isItalic()) {
                pdfFontName = family.getItalicPdfFont();
                pdfFontStyle = java.awt.Font.ITALIC;
            }

            if (pdfFontName == null) {
                pdfFontName = family.getNormalPdfFont();
                pdfFontStyle = java.awt.Font.PLAIN;
            }

            if (pdfFontName == null) {
                //in theory, face file cannot be null here
                pdfFontName = (face == null || face.getFile() == null ? jrFont.getPdfFontName()
                        : face.getFile());
                pdfFontStyle = faceStyle;//FIXMEFONT not sure this is correct, in case we inherit pdfFontName from default properties
            }

            //            String ttf = face.getFile();
            //            if (ttf == null)
            //            {
            //               throw new JRRuntimeException("The '" + face.getName() + "' font face in family '" + family.getName() + "' returns a null file.");
            //            }

            pdfFont = new PdfFont(pdfFontName,
                    family.getPdfEncoding() == null ? jrFont.getPdfEncoding() : family.getPdfEncoding(),
                    family.isPdfEmbedded() == null ? jrFont.isPdfEmbedded()
                            : family.isPdfEmbedded().booleanValue(),
                    jrFont.isBold() && ((pdfFontStyle & java.awt.Font.BOLD) == 0),
                    jrFont.isItalic() && ((pdfFontStyle & java.awt.Font.ITALIC) == 0));
        }
    }

    int pdfFontStyle = (pdfFont.isPdfSimulatedBold() ? Font.BOLD : 0)
            | (pdfFont.isPdfSimulatedItalic() ? Font.ITALIC : 0);
    if (setFontLines) {
        pdfFontStyle |= (jrFont.isUnderline() ? Font.UNDERLINE : 0)
                | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0);
    }

    try {
        font = FontFactory.getFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(), pdfFont.isPdfEmbedded(),
                jrFont.getFontSize() * fontSizeScale, pdfFontStyle, forecolor);

        // check if FontFactory didn't find the font
        if (font.getBaseFont() == null && font.family() == Font.UNDEFINED) {
            font = null;
        }
    } catch (Exception e) {
        initialException = e;
    }

    if (font == null) {
        byte[] bytes = null;

        try {
            bytes = RepositoryUtil.getInstance(jasperReportsContext)
                    .getBytesFromLocation(pdfFont.getPdfFontName());
        } catch (JRException e) {
            throw //NOPMD
            new JRRuntimeException("Could not load the following font : " + "\npdfFontName   : "
                    + pdfFont.getPdfFontName() + "\npdfEncoding   : " + pdfFont.getPdfEncoding()
                    + "\nisPdfEmbedded : " + pdfFont.isPdfEmbedded(), initialException);
        }

        BaseFont baseFont = null;

        try {
            baseFont = BaseFont.createFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(),
                    pdfFont.isPdfEmbedded(), true, bytes, null);
        } catch (DocumentException e) {
            throw new JRRuntimeException(e);
        } catch (IOException e) {
            throw new JRRuntimeException(e);
        }

        font = new Font(baseFont, jrFont.getFontSize() * fontSizeScale, pdfFontStyle, forecolor);
    }

    return font;
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java

/**
 *
 *///from   www .j a  v  a 2 s.c o m
protected Chunk getChunk(Map<Attribute, Object> attributes, String text, Locale locale) {
    // underline and strikethrough are set on the chunk below
    Font font = getFont(attributes, locale, false);

    Chunk chunk = new Chunk(text, font);

    if (hasUnderline(attributes)) {
        // using the same values as sun.font.Fond2D
        chunk.setUnderline(null, 0, 1f / 18, 0, -1f / 12, 0);
    }

    if (hasStrikethrough(attributes)) {
        // using the same thickness as sun.font.Fond2D.
        // the position is calculated in Fond2D based on the ascent, defaulting 
        // to iText default position which depends on the font size
        chunk.setUnderline(null, 0, 1f / 18, 0, 1f / 3, 0);
    }

    Color backcolor = (Color) attributes.get(TextAttribute.BACKGROUND);
    if (backcolor != null) {
        chunk.setBackground(backcolor);
    }

    Object script = attributes.get(TextAttribute.SUPERSCRIPT);
    if (script != null) {
        if (TextAttribute.SUPERSCRIPT_SUPER.equals(script)) {
            chunk.setTextRise(font.getCalculatedLeading(1f) / 2);
        } else if (TextAttribute.SUPERSCRIPT_SUB.equals(script)) {
            chunk.setTextRise(-font.getCalculatedLeading(1f) / 2);
        }
    }

    if (splitCharacter != null) {
        //TODO use line break offsets if available?
        chunk.setSplitCharacter(splitCharacter);
    }

    return chunk;
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

/**
 *
 *///from w ww . j  a v a  2 s .  co  m
protected HSSFFont getLoadedFont(JRFont font, short forecolor, Map<Attribute, Object> attributes,
        Locale locale) {

    HSSFFont cellFont = null;
    String fontName = fontUtil.getExportFontFamily(font.getFontName(), locale, getExporterKey());

    boolean isFontSizeFixEnabled = getCurrentItemConfiguration().isFontSizeFixEnabled();

    short superscriptType = HSSFFont.SS_NONE;

    if (attributes != null && attributes.get(TextAttribute.SUPERSCRIPT) != null) {
        Object value = attributes.get(TextAttribute.SUPERSCRIPT);
        if (TextAttribute.SUPERSCRIPT_SUPER.equals(value)) {
            superscriptType = HSSFFont.SS_SUPER;
        } else if (TextAttribute.SUPERSCRIPT_SUB.equals(value)) {
            superscriptType = HSSFFont.SS_SUB;
        }
    }
    for (int i = 0; i < loadedFonts.size(); i++) {
        HSSFFont cf = (HSSFFont) loadedFonts.get(i);
        short fontSize = (short) font.getFontsize();
        if (isFontSizeFixEnabled) {
            fontSize -= 1;
        }
        if (cf.getFontName().equals(fontName) && (cf.getColor() == forecolor)
                && (cf.getFontHeightInPoints() == fontSize)
                && ((cf.getUnderline() == HSSFFont.U_SINGLE) ? (font.isUnderline()) : (!font.isUnderline()))
                && (cf.getStrikeout() == font.isStrikeThrough()) && (cf.getBold() == font.isBold())
                && (cf.getItalic() == font.isItalic()) && (cf.getTypeOffset() == superscriptType)) {
            cellFont = cf;
            break;
        }
    }

    if (cellFont == null) {

        cellFont = workbook.createFont();
        cellFont.setFontName(fontName);
        cellFont.setColor(forecolor);
        short fontSize = (short) font.getFontsize();
        if (isFontSizeFixEnabled) {
            fontSize -= 1;
        }
        cellFont.setFontHeightInPoints(fontSize);

        if (font.isUnderline()) {
            cellFont.setUnderline(HSSFFont.U_SINGLE);
        }
        if (font.isStrikeThrough()) {
            cellFont.setStrikeout(true);
        }
        if (font.isBold()) {
            cellFont.setBold(true);
        }
        if (font.isItalic()) {
            cellFont.setItalic(true);
        }

        cellFont.setTypeOffset(superscriptType);
        loadedFonts.add(cellFont);
    }
    return cellFont;
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java

/**
 * Creates a PDF font.//  w w  w.j  a  va  2s . c  om
 * 
 * @param attributes the text attributes of the font
 * @param locale the locale for which to create the font
 * @param setFontLines whether to set underline and strikethrough as font style
 * @return the PDF font for the specified attributes
 */
protected Font getFont(Map<Attribute, Object> attributes, Locale locale, boolean setFontLines) {
    JRFont jrFont = new JRBaseFont(attributes);

    Exception initialException = null;

    Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND);

    // use the same font scale ratio as in JRStyledText.getAwtAttributedString
    float fontSizeScale = 1f;
    Integer scriptStyle = (Integer) attributes.get(TextAttribute.SUPERSCRIPT);
    if (scriptStyle != null && (TextAttribute.SUPERSCRIPT_SUB.equals(scriptStyle)
            || TextAttribute.SUPERSCRIPT_SUPER.equals(scriptStyle))) {
        fontSizeScale = 2f / 3;
    }

    Font font = null;
    String pdfFontName = null;
    String pdfEncoding = null;
    boolean isPdfEmbedded = false;
    boolean isPdfSimulatedBold = false;
    boolean isPdfSimulatedItalic = false;

    FontInfo fontInfo = (FontInfo) attributes.get(JRTextAttribute.FONT_INFO);
    if (fontInfo == null) {
        fontInfo = fontUtil.getFontInfo(jrFont.getFontName(), locale);
    }

    if (fontInfo == null) {
        //fontName NOT found in font extensions
        pdfFontName = jrFont.getPdfFontName();
        pdfEncoding = jrFont.getPdfEncoding();
        isPdfEmbedded = jrFont.isPdfEmbedded();
    } else {
        //fontName found in font extensions
        FontFamily family = fontInfo.getFontFamily();

        int pdfFontStyle = java.awt.Font.PLAIN;

        FontFace fontFace = fontInfo.getFontFace();
        if (fontFace != null) {
            pdfFontName = fontFace.getPdf();
            pdfFontName = pdfFontName == null ? fontFace.getTtf() : pdfFontName;
            pdfFontStyle = fontInfo.getStyle();
        }

        if (pdfFontName == null && jrFont.isBold() && jrFont.isItalic()) {
            fontFace = family.getBoldItalicFace();
            if (fontFace != null) {
                pdfFontName = fontFace.getPdf();
                pdfFontName = pdfFontName == null ? fontFace.getTtf() : pdfFontName;
                pdfFontStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC;
            }
        }

        if (pdfFontName == null && jrFont.isBold()) {
            fontFace = family.getBoldFace();
            if (fontFace != null) {
                pdfFontName = fontFace.getPdf();
                pdfFontName = pdfFontName == null ? fontFace.getTtf() : pdfFontName;
                pdfFontStyle = java.awt.Font.BOLD;
            }
        }

        if (pdfFontName == null && jrFont.isItalic()) {
            fontFace = family.getItalicFace();
            if (fontFace != null) {
                pdfFontName = fontFace.getPdf();
                pdfFontName = pdfFontName == null ? fontFace.getTtf() : pdfFontName;
                pdfFontStyle = java.awt.Font.ITALIC;
            }
        }

        if (pdfFontName == null) {
            fontFace = family.getNormalFace();
            if (fontFace != null) {
                pdfFontName = fontFace.getPdf();
                pdfFontName = pdfFontName == null ? fontFace.getTtf() : pdfFontName;
                pdfFontStyle = java.awt.Font.PLAIN;
            }
        }

        if (pdfFontName == null) {
            pdfFontName = jrFont.getPdfFontName();
        }

        pdfEncoding = family.getPdfEncoding() == null ? jrFont.getPdfEncoding() : family.getPdfEncoding();
        isPdfEmbedded = family.isPdfEmbedded() == null ? jrFont.isPdfEmbedded() : family.isPdfEmbedded();
        isPdfSimulatedBold = jrFont.isBold() && ((pdfFontStyle & java.awt.Font.BOLD) == 0);
        isPdfSimulatedItalic = jrFont.isItalic() && ((pdfFontStyle & java.awt.Font.ITALIC) == 0);
    }

    int pdfFontStyle = (isPdfSimulatedBold ? Font.BOLD : 0) | (isPdfSimulatedItalic ? Font.ITALIC : 0);
    if (setFontLines) {
        pdfFontStyle |= (jrFont.isUnderline() ? Font.UNDERLINE : 0)
                | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0);
    }

    try {
        font = FontFactory.getFont(pdfFontName, pdfEncoding, isPdfEmbedded,
                jrFont.getFontsize() * fontSizeScale, pdfFontStyle, forecolor);
        // check if FontFactory didn't find the font
        if (font != null && font.getBaseFont() == null && font.getFamily() == Font.UNDEFINED) {
            font = null;
        }
    } catch (Exception e) {
        initialException = e;
    }

    if (font == null) {
        byte[] bytes = null;

        try {
            bytes = getRepository().getBytesFromLocation(pdfFontName);
        } catch (JRException e) {
            throw //NOPMD
            new JRRuntimeException(EXCEPTION_MESSAGE_KEY_FONT_LOADING_ERROR,
                    new Object[] { pdfFontName, pdfEncoding, isPdfEmbedded }, initialException);
        }

        BaseFont baseFont = null;

        try {
            baseFont = BaseFont.createFont(pdfFontName, pdfEncoding, isPdfEmbedded, true, bytes, null);
        } catch (DocumentException e) {
            throw new JRRuntimeException(e);
        } catch (IOException e) {
            throw new JRRuntimeException(e);
        }

        font = new Font(baseFont, jrFont.getFontsize() * fontSizeScale, pdfFontStyle, forecolor);
    }

    return font;
}

From source file:net.sf.jasperreports.engine.export.HtmlExporter.java

protected void exportStyledTextRun(Map<Attribute, Object> attributes, String text, String tooltip,
        Locale locale, LineSpacingEnum lineSpacing, Float lineSpacingSize, float lineSpacingFactor,
        Color backcolor, boolean hyperlinkStarted) throws IOException {
    boolean localHyperlink = false;
    JRPrintHyperlink hyperlink = (JRPrintHyperlink) attributes.get(JRTextAttribute.HYPERLINK);
    if (!hyperlinkStarted && hyperlink != null) {
        localHyperlink = startHyperlink(hyperlink);
    }// w  w  w.  j av  a 2 s .  c om

    boolean isBold = TextAttribute.WEIGHT_BOLD.equals(attributes.get(TextAttribute.WEIGHT));
    boolean isItalic = TextAttribute.POSTURE_OBLIQUE.equals(attributes.get(TextAttribute.POSTURE));

    String fontFamily = resolveFontFamily(attributes, locale);

    // do not put single quotes around family name here because the value might already contain quotes, 
    // especially if it is coming from font extension export configuration
    writer.write("<span style=\"font-family: ");
    // don't encode single quotes as the output would be too verbose and too much of a chance compared to previous releases
    writer.write(JRStringUtil.encodeXmlAttribute(fontFamily, true));
    writer.write("; ");

    Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND);
    if (!hyperlinkStarted || !Color.black.equals(forecolor)) {
        writer.write("color: ");
        writer.write(JRColorUtil.getCssColor(forecolor));
        writer.write("; ");
    }

    Color runBackcolor = (Color) attributes.get(TextAttribute.BACKGROUND);
    if (runBackcolor != null && !runBackcolor.equals(backcolor)) {
        writer.write("background-color: ");
        writer.write(JRColorUtil.getCssColor(runBackcolor));
        writer.write("; ");
    }

    writer.write("font-size: ");
    writer.write(toSizeUnit((Float) attributes.get(TextAttribute.SIZE)));
    writer.write(";");

    switch (lineSpacing) {
    case SINGLE:
    default: {
        if (lineSpacingFactor == 0) {
            writer.write(" line-height: 1; *line-height: normal;");
        } else {
            writer.write(" line-height: " + lineSpacingFactor + ";");
        }
        break;
    }
    case ONE_AND_HALF: {
        if (lineSpacingFactor == 0) {
            writer.write(" line-height: 1.5;");
        } else {
            writer.write(" line-height: " + lineSpacingFactor + ";");
        }
        break;
    }
    case DOUBLE: {
        if (lineSpacingFactor == 0) {
            writer.write(" line-height: 2.0;");
        } else {
            writer.write(" line-height: " + lineSpacingFactor + ";");
        }
        break;
    }
    case PROPORTIONAL: {
        if (lineSpacingSize != null) {
            writer.write(" line-height: " + lineSpacingSize + ";");
        }
        break;
    }
    case AT_LEAST:
    case FIXED: {
        if (lineSpacingSize != null) {
            writer.write(" line-height: " + lineSpacingSize + "px;");
        }
        break;
    }
    }

    /*
    if (!horizontalAlignment.equals(CSS_TEXT_ALIGN_LEFT))
    {
       writer.write(" text-align: ");
       writer.write(horizontalAlignment);
       writer.write(";");
    }
    */

    if (isBold) {
        writer.write(" font-weight: bold;");
    }
    if (isItalic) {
        writer.write(" font-style: italic;");
    }
    if (TextAttribute.UNDERLINE_ON.equals(attributes.get(TextAttribute.UNDERLINE))) {
        writer.write(" text-decoration: underline;");
    }
    if (TextAttribute.STRIKETHROUGH_ON.equals(attributes.get(TextAttribute.STRIKETHROUGH))) {
        writer.write(" text-decoration: line-through;");
    }

    if (TextAttribute.SUPERSCRIPT_SUPER.equals(attributes.get(TextAttribute.SUPERSCRIPT))) {
        writer.write(" vertical-align: super;");
    } else if (TextAttribute.SUPERSCRIPT_SUB.equals(attributes.get(TextAttribute.SUPERSCRIPT))) {
        writer.write(" vertical-align: sub;");
    }

    writer.write("\"");

    if (tooltip != null) {
        writer.write(" title=\"");
        writer.write(JRStringUtil.encodeXmlAttribute(tooltip));
        writer.write("\"");
    }

    writer.write(">");

    writer.write(JRStringUtil.htmlEncode(text));

    writer.write("</span>");

    if (localHyperlink) {
        endHyperlink();
    }
}