Example usage for java.awt.font TextAttribute POSTURE

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

Introduction

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

Prototype

TextAttribute POSTURE

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

Click Source Link

Document

Attribute key for the posture of a font.

Usage

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

protected FontKey extractFontKey(Map<Attribute, Object> attributes, Locale locale) {
    AwtFontAttribute fontAttribute = AwtFontAttribute.fromAttributes(attributes);

    Number posture = (Number) attributes.get(TextAttribute.POSTURE);
    boolean italic = TextAttribute.POSTURE_OBLIQUE.equals(posture);//FIXME check for non standard posture

    Number weight = (Number) attributes.get(TextAttribute.WEIGHT);
    boolean bold = TextAttribute.WEIGHT_BOLD.equals(weight);

    return new FontKey(fontAttribute, italic, bold, locale);
}

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

protected boolean canUseGlyphRendering(FontKey fontKey) {
    Map<Attribute, Object> fontAttributes = new HashMap<Attribute, Object>();
    fontKey.fontAttribute.putAttributes(fontAttributes);
    fontAttributes.put(TextAttribute.SIZE, 10f);

    int style = 0;
    if (fontKey.italic) {
        style |= java.awt.Font.ITALIC;
        fontAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
    }// w w w.  ja v a2 s.  c o m
    if (fontKey.bold) {
        style |= java.awt.Font.BOLD;
        fontAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
    }

    Font pdfFont = getFont(fontAttributes, fontKey.locale, false);
    BaseFont baseFont = pdfFont.getBaseFont();
    if (baseFont.getFontType() != BaseFont.FONT_TYPE_TTUNI || baseFont.isFontSpecific()) {
        if (log.isDebugEnabled()) {
            log.debug("pdf font for " + fontKey + " has type " + baseFont.getFontType() + ", symbol "
                    + baseFont.isFontSpecific() + ", cannot use glyph rendering");
        }
        return false;
    }

    java.awt.Font awtFont = fontUtil.getAwtFontFromBundles(fontKey.fontAttribute, style, 10f, fontKey.locale,
            awtIgnoreMissingFont);
    if (awtFont == null) {
        awtFont = new java.awt.Font(fontAttributes);
    }
    String awtFontName = awtFont.getFontName();

    if (log.isDebugEnabled()) {
        log.debug(fontKey + " resolved to awt font " + awtFontName);
    }

    // we need the fonts to be identical.
    // it would be safer to only allow fonts from extensions, 
    // but for now we are just checking the font names.
    // we need to compare full names because we can't get the base name from awt.
    String[][] pdfFontNames = baseFont.getFullFontName();
    boolean nameMatch = false;
    for (String[] nameArray : pdfFontNames) {
        if (nameArray.length >= 4) {
            if (log.isDebugEnabled()) {
                log.debug(fontKey + " resolved to pdf font " + nameArray[3]);
            }

            if (awtFontName.equals(nameArray[3])) {
                nameMatch = true;
                break;
            }
        }
    }

    return nameMatch;
}

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 ww.j  a v 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();
    }
}

From source file:org.apache.fop.svg.ACIUtils.java

/**
 * Tries to find matching fonts in FOP's {@link FontInfo} instance for fonts used by
 * Apache Batik. The method inspects the various GVT attributes found in the ACI.
 * @param aci the ACI to find matching fonts for
 * @param fontInfo the font info instance with FOP's fonts
 * @return an array of matching fonts/*w  ww .  j  a v a 2  s.c o  m*/
 */
public static Font[] findFontsForBatikACI(AttributedCharacterIterator aci, FontInfo fontInfo) {
    List<Font> fonts = new java.util.ArrayList<Font>();
    @SuppressWarnings("unchecked")
    List<GVTFontFamily> gvtFonts = (List<GVTFontFamily>) aci
            .getAttribute(GVTAttributedCharacterIterator.TextAttribute.GVT_FONT_FAMILIES);
    Float posture = (Float) aci.getAttribute(TextAttribute.POSTURE);
    Float taWeight = (Float) aci.getAttribute(TextAttribute.WEIGHT);
    Float fontSize = (Float) aci.getAttribute(TextAttribute.SIZE);

    String style = toStyle(posture);
    int weight = toCSSWeight(taWeight);
    int fsize = (int) (fontSize.floatValue() * 1000);

    String firstFontFamily = null;

    //GVT_FONT can sometimes be different from the fonts in GVT_FONT_FAMILIES
    //or GVT_FONT_FAMILIES can even be empty and only GVT_FONT is set
    /* The following code section is not available until Batik 1.7 is released. */
    GVTFont gvtFont = (GVTFont) aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.GVT_FONT);
    if (gvtFont != null) {
        String gvtFontFamily = gvtFont.getFamilyName();
        if (fontInfo.hasFont(gvtFontFamily, style, weight)) {
            FontTriplet triplet = fontInfo.fontLookup(gvtFontFamily, style, weight);
            Font f = fontInfo.getFontInstance(triplet, fsize);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found a font that matches the GVT font: " + gvtFontFamily + ", " + weight + ", "
                        + style + " -> " + f);
            }
            fonts.add(f);
        }
        firstFontFamily = gvtFontFamily;
    }

    if (gvtFonts != null) {
        boolean haveInstanceOfSVGFontFamily = false;
        for (GVTFontFamily fam : gvtFonts) {
            if (fam instanceof SVGFontFamily) {
                haveInstanceOfSVGFontFamily = true;
            }
            String fontFamily = fam.getFamilyName();
            if (fontInfo.hasFont(fontFamily, style, weight)) {
                FontTriplet triplet = fontInfo.fontLookup(fontFamily, style, weight);
                Font f = fontInfo.getFontInstance(triplet, fsize);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found a font that matches the GVT font family: " + fontFamily + ", " + weight
                            + ", " + style + " -> " + f);
                }
                fonts.add(f);
            }
            if (firstFontFamily == null) {
                firstFontFamily = fontFamily;
            }
        }
        // SVG fonts are embedded fonts in the SVG document and are rarely used; however if they
        // are used but the fonts also exists in the system and are known to FOP then FOP should
        // use them; then the decision whether Batik should stroke the text should be made after
        // no matching fonts are found
        if (fonts.isEmpty() && haveInstanceOfSVGFontFamily) {
            fontInfo.notifyStrokingSVGTextAsShapes(firstFontFamily);
            return null; // Let Batik paint this text!
        }
    }
    if (fonts.isEmpty()) {
        if (firstFontFamily == null) {
            //This will probably never happen. Just to be on the safe side.
            firstFontFamily = "any";
        }
        //lookup with fallback possibility (incl. substitution notification)
        FontTriplet triplet = fontInfo.fontLookup(firstFontFamily, style, weight);
        Font f = fontInfo.getFontInstance(triplet, fsize);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Falling back to adjustable font lookup up for: " + firstFontFamily + ", " + weight + ", "
                    + style + " -> " + f);
        }
        fonts.add(f);
    }
    return fonts.toArray(new Font[fonts.size()]);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * This routine goes through the attributes and sets the font before calling the actual string drawing routine
 *
 * @param iter//  w  ww . j  a  v  a2s  .  c o  m
 */
private void doAttributes(final AttributedCharacterIterator iter) {
    underline = false;
    final Set set = iter.getAttributes().keySet();
    for (Iterator iterator = set.iterator(); iterator.hasNext();) {
        final AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) iterator
                .next();
        if (!(attribute instanceof TextAttribute)) {
            continue;
        }
        final TextAttribute textattribute = (TextAttribute) attribute;
        if (textattribute.equals(TextAttribute.FONT)) {
            final Font font = (Font) iter.getAttributes().get(textattribute);
            setFont(font);
        } else if (textattribute.equals(TextAttribute.UNDERLINE)) {
            if (iter.getAttributes().get(textattribute) == TextAttribute.UNDERLINE_ON) {
                underline = true;
            }
        } else if (textattribute.equals(TextAttribute.SIZE)) {
            final Object obj = iter.getAttributes().get(textattribute);
            if (obj instanceof Integer) {
                final int i = ((Integer) obj).intValue();
                setFont(getFont().deriveFont(getFont().getStyle(), i));
            } else if (obj instanceof Float) {
                final float f = ((Float) obj).floatValue();
                setFont(getFont().deriveFont(getFont().getStyle(), f));
            }
        } else if (textattribute.equals(TextAttribute.FOREGROUND)) {
            setColor((Color) iter.getAttributes().get(textattribute));
        } else if (textattribute.equals(TextAttribute.FAMILY)) {
            final Font font = getFont();
            final Map fontAttributes = font.getAttributes();
            fontAttributes.put(TextAttribute.FAMILY, iter.getAttributes().get(textattribute));
            setFont(font.deriveFont(fontAttributes));
        } else if (textattribute.equals(TextAttribute.POSTURE)) {
            final Font font = getFont();
            final Map fontAttributes = font.getAttributes();
            fontAttributes.put(TextAttribute.POSTURE, iter.getAttributes().get(textattribute));
            setFont(font.deriveFont(fontAttributes));
        } else if (textattribute.equals(TextAttribute.WEIGHT)) {
            final Font font = getFont();
            final Map fontAttributes = font.getAttributes();
            fontAttributes.put(TextAttribute.WEIGHT, iter.getAttributes().get(textattribute));
            setFont(font.deriveFont(fontAttributes));
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfLogicalPageDrawable.java

private PdfTextSpec computeFont(final RichTextSpec.StyledChunk c) {
    final StyleSheet layoutContext = c.getStyleSheet();

    final PdfGraphics2D g2 = (PdfGraphics2D) getGraphics();
    final PdfContentByte cb = g2.getRawContentByte();

    final TypedMapWrapper<Attribute, Object> attributes = new TypedMapWrapper<Attribute, Object>(
            c.getAttributes());/*  w ww  .  j  av a  2 s  . c  o m*/
    final String fontName = attributes.get(TextAttribute.FAMILY, String.class);
    final Number fontSize = attributes.get(TextAttribute.SIZE, 10f, Number.class);
    final String encoding = (String) layoutContext.getStyleProperty(TextStyleKeys.FONTENCODING);
    final boolean embed = globalEmbed || layoutContext.getBooleanStyleProperty(TextStyleKeys.EMBEDDED_FONT);
    final Float weightRaw = attributes.get(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR, Float.class);
    final Float italicsRaw = attributes.get(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, Float.class);

    final BaseFontFontMetrics fontMetrics = getMetaData().getBaseFontFontMetrics(fontName,
            fontSize.doubleValue(), weightRaw >= TextAttribute.WEIGHT_DEMIBOLD,
            italicsRaw >= TextAttribute.POSTURE_OBLIQUE, encoding, embed, false);
    return new PdfTextSpec(layoutContext, getMetaData(), g2, fontMetrics, cb);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfLogicalPageDrawable.java

private int computeStyle(final TypedMapWrapper<Attribute, Object> attributes, final PdfTextSpec pdfTextSpec) {
    final Float weight = attributes.get(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR, Float.class);
    final Float italics = attributes.get(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, Float.class);
    final boolean underlined = attributes.exists(TextAttribute.UNDERLINE);
    final boolean strikethrough = attributes.exists(TextAttribute.STRIKETHROUGH);

    FontNativeContext nativeContext = pdfTextSpec.getFontMetrics().getNativeContext();

    int style = 0;
    if (nativeContext.isNativeBold() == false && weight >= TextAttribute.WEIGHT_DEMIBOLD) {
        style |= Font.BOLD;//from   www.  j  av a2  s. c  o  m
    }
    if (nativeContext.isNativeItalics() == false && italics >= TextAttribute.POSTURE_OBLIQUE) {
        style |= Font.ITALIC;
    }
    if (underlined) {
        style |= Font.UNDERLINE;
    }
    if (strikethrough) {
        style |= Font.STRIKETHRU;
    }
    return style;
}

From source file:org.zkoss.poi.ss.util.SheetUtil.java

/**
 * Copy text attributes from the supplied Font to Java2D AttributedString
 *///w w w.j  av  a 2s  .  c om
private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) {
    str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
    str.addAttribute(TextAttribute.SIZE, (float) font.getFontHeightInPoints());
    if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD)
        str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
    if (font.getItalic())
        str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
    if (font.getUnderline() == Font.U_SINGLE)
        str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
}