Example usage for com.lowagie.text.pdf BaseFont AWT_LEADING

List of usage examples for com.lowagie.text.pdf BaseFont AWT_LEADING

Introduction

In this page you can find the example usage for com.lowagie.text.pdf BaseFont AWT_LEADING.

Prototype

int AWT_LEADING

To view the source code for com.lowagie.text.pdf BaseFont AWT_LEADING.

Click Source Link

Document

java.awt.Font property

Usage

From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableParagraph.java

License:Open Source License

@SuppressWarnings("unchecked")
private void postProcessLineHeightAndBaseline() {
    // adjust line height and baseline
    Font font = getMostOftenUsedFont();
    if (font == null || font.getBaseFont() == null) {
        font = this.font;
    }//from  www . j a  v  a2  s. c o  m
    if (font != null && font.getBaseFont() != null) {
        // iText and open office computes proportional line height differently
        // [iText] line height = coefficient * font size
        // [open office] line height = coefficient * (font ascender + font descender + font extra margin)
        // we have to increase paragraph line height to generate pdf similar to open office document
        // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph
        float size = font.getSize();
        float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size);
        float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value
        float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size);
        float multiplier = (ascender + descender + margin) / size;
        if (multipliedLeading > 0.0f) {
            setMultipliedLeading(getMultipliedLeading() * multiplier);
        }

        // iText seems to output text with baseline lower than open office
        // we raise all paragraph text by some amount
        // again this may be inaccurate if fonts with different size are used in this paragraph
        float itextdescender = -font.getBaseFont().getFontDescriptor(BaseFont.DESCENT, size); // negative
        float textRise = itextdescender + getTotalLeading() - font.getSize() * multiplier;
        ArrayList<Chunk> chunks = getChunks();
        for (Chunk chunk : chunks) {
            Font f = chunk.getFont();
            if (f != null) {
                // have to raise underline and strikethru as well
                float s = f.getSize();
                if (f.isUnderlined()) {
                    f.setStyle(f.getStyle() & ~Font.UNDERLINE);
                    chunk.setUnderline(s * 1 / 17, s * -1 / 7 + textRise);
                }
                if (f.isStrikethru()) {
                    f.setStyle(f.getStyle() & ~Font.STRIKETHRU);
                    chunk.setUnderline(s * 1 / 17, s * 1 / 4 + textRise);
                }
            }
            chunk.setTextRise(chunk.getTextRise() + textRise);
        }
    }
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableParagraph.java

License:Open Source License

/**
 * Adjust iText multiplied leading according the given font.
 * //from   w w w  . ja  v  a  2  s . c om
 * @param font
 */
public void adjustMultipliedLeading(Font font) {
    if (originMultipliedLeading != null && font != null && font.getBaseFont() != null) {
        // iText and open office computes proportional line height differently
        // [iText] line height = coefficient * font size
        // [MS Word] line height = coefficient * (font ascender + font descender + font extra margin)
        // we have to increase paragraph line height to generate pdf similar to OOXML docx document
        // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph
        float size = font.getSize();
        float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size);
        float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value
        float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size);
        float multiplier = (ascender + descender + margin) / size;
        super.setMultipliedLeading(originMultipliedLeading * multiplier);
    }
}

From source file:org.odftoolkit.odfdom.converter.internal.itext.stylable.StylableParagraph.java

License:Open Source License

@SuppressWarnings("unchecked")
public Element getElement() {
    if (!elementPostProcessed) {
        elementPostProcessed = true;/*  w w  w .j a v a  2s .co  m*/

        // add space if this paragraph is empty
        // otherwise it's height will be zero
        boolean empty = true;
        ArrayList<Chunk> chunks = getChunks();
        for (Chunk chunk : chunks) {
            if (chunk.getImage() == null && chunk.getContent() != null && chunk.getContent().length() > 0) {
                empty = false;
                break;
            }
        }
        if (empty) {
            super.add(new Chunk("\u00A0")); // non breaking space
        }

        // adjust line height and baseline
        if (font != null && font.getBaseFont() != null) {
            // iText and open office computes proportional line height differently
            // [iText] line height = coefficient * font size
            // [open office] line height = coefficient * (font ascender + font descender + font extra margin)
            // we have to increase paragraph line height to generate pdf similar to open office document
            // this algorithm may be inaccurate if fonts with different multipliers are used in this paragraph
            float size = font.getSize();
            float ascender = font.getBaseFont().getFontDescriptor(BaseFont.AWT_ASCENT, size);
            float descender = -font.getBaseFont().getFontDescriptor(BaseFont.AWT_DESCENT, size); // negative value
            float margin = font.getBaseFont().getFontDescriptor(BaseFont.AWT_LEADING, size);
            float multiplier = (ascender + descender + margin) / size;
            if (multipliedLeading > 0.0f) {
                setMultipliedLeading(getMultipliedLeading() * multiplier);
            }

            // iText seems to output text with baseline lower than open office
            // we raise all paragraph text by some amount
            // again this may be inaccurate if fonts with different size are used in this paragraph
            float itextdescender = -font.getBaseFont().getFontDescriptor(BaseFont.DESCENT, size); // negative
            float textRise = itextdescender + getTotalLeading() - font.getSize() * multiplier;
            chunks = getChunks();
            for (Chunk chunk : chunks) {
                Font f = chunk.getFont();
                if (f != null) {
                    // have to raise underline and strikethru as well
                    float s = f.getSize();
                    if (f.isUnderlined()) {
                        f.setStyle(f.getStyle() & ~Font.UNDERLINE);
                        chunk.setUnderline(s * 1 / 17, s * -1 / 7 + textRise);
                    }
                    if (f.isStrikethru()) {
                        f.setStyle(f.getStyle() & ~Font.STRIKETHRU);
                        chunk.setUnderline(s * 1 / 17, s * 1 / 4 + textRise);
                    }
                }
                chunk.setTextRise(chunk.getTextRise() + textRise);
            }
        }

        // wrap this paragraph into a table if necessary
        if (wrapperCell != null) {
            // background color or borders were set
            wrapperCell.addElement(this);
            wrapperTable = createTable(wrapperCell);
            if (getIndentationLeft() > 0.0f || getIndentationRight() > 0.0f || getSpacingBefore() > 0.0f
                    || getSpacingAfter() > 0.0f) {
                // margins were set, have to wrap the cell again
                PdfPCell outerCell = createCell();
                outerCell.setPaddingLeft(getIndentationLeft());
                setIndentationLeft(0.0f);
                outerCell.setPaddingRight(getIndentationRight());
                setIndentationRight(0.0f);
                outerCell.setPaddingTop(getSpacingBefore());
                setSpacingBefore(0.0f);
                outerCell.setPaddingBottom(getSpacingAfter());
                setSpacingAfter(0.0f);
                outerCell.addElement(wrapperTable);
                wrapperTable = createTable(outerCell);
            }
        }
    }
    return wrapperTable != null ? wrapperTable : this;
}

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

License:Open Source License

protected void drawText(final RenderableText renderableText, final long contentX2) {
    if (renderableText.getLength() == 0) {
        return;/*from w  w  w .  j a  v  a  2s. c  om*/
    }

    final long posX = renderableText.getX();
    final long posY = renderableText.getY();
    final float x1 = (float) (StrictGeomUtility.toExternalValue(posX));

    final PdfContentByte cb;
    PdfTextSpec textSpec = (PdfTextSpec) getTextSpec();
    if (textSpec == null) {
        final StyleSheet layoutContext = renderableText.getStyleSheet();

        // The code below may be weird, but at least it is predictable weird.
        final String fontName = getMetaData()
                .getNormalizedFontFamilyName((String) layoutContext.getStyleProperty(TextStyleKeys.FONT));
        final String encoding = (String) layoutContext.getStyleProperty(TextStyleKeys.FONTENCODING);
        final float fontSize = (float) layoutContext.getDoubleStyleProperty(TextStyleKeys.FONTSIZE, 10);

        final boolean embed = globalEmbed || layoutContext.getBooleanStyleProperty(TextStyleKeys.EMBEDDED_FONT);
        final boolean bold = layoutContext.getBooleanStyleProperty(TextStyleKeys.BOLD);
        final boolean italics = layoutContext.getBooleanStyleProperty(TextStyleKeys.ITALIC);

        final BaseFontFontMetrics fontMetrics = getMetaData().getBaseFontFontMetrics(fontName, fontSize, bold,
                italics, encoding, embed, false);

        final PdfGraphics2D g2 = (PdfGraphics2D) getGraphics();
        final Color cssColor = (Color) layoutContext.getStyleProperty(ElementStyleKeys.PAINT);
        g2.setPaint(cssColor);
        g2.setFillPaint();
        g2.setStrokePaint();
        // final float translateY = (float) affineTransform.getTranslateY();

        cb = g2.getRawContentByte();

        textSpec = new PdfTextSpec(layoutContext, getMetaData(), g2, fontMetrics, cb);
        setTextSpec(textSpec);

        cb.beginText();
        cb.setFontAndSize(fontMetrics.getBaseFont(), fontSize);
    } else {
        cb = textSpec.getContentByte();
    }

    final BaseFontFontMetrics baseFontRecord = textSpec.getFontMetrics();
    final BaseFont baseFont = baseFontRecord.getBaseFont();
    final float ascent;
    if (legacyLineHeightCalc) {
        final float awtAscent = baseFont.getFontDescriptor(BaseFont.AWT_ASCENT, textSpec.getFontSize());
        final float awtLeading = baseFont.getFontDescriptor(BaseFont.AWT_LEADING, textSpec.getFontSize());
        ascent = awtAscent + awtLeading;
    } else {
        ascent = baseFont.getFontDescriptor(BaseFont.BBOXURY, textSpec.getFontSize());
    }
    final float y2 = (float) (StrictGeomUtility.toExternalValue(posY) + ascent);
    final float y = globalHeight - y2;

    final AffineTransform affineTransform = textSpec.getGraphics().getTransform();
    final float translateX = (float) affineTransform.getTranslateX();

    final FontNativeContext nativeContext = baseFontRecord.getNativeContext();
    if (baseFontRecord.isTrueTypeFont() && textSpec.isBold() && nativeContext.isNativeBold() == false) {
        final float strokeWidth = textSpec.getFontSize() / 30.0f; // right from iText ...
        if (strokeWidth == 1) {
            cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
        } else {
            cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
            cb.setLineWidth(strokeWidth);
        }
    } else {
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
    }

    // if the font does not declare to be italics already, emulate it ..
    if (baseFontRecord.isTrueTypeFont() && textSpec.isItalics() && nativeContext.isNativeItalics() == false) {
        final float italicAngle = baseFont.getFontDescriptor(BaseFont.ITALICANGLE, textSpec.getFontSize());
        if (italicAngle == 0) {
            // italics requested, but the font itself does not supply italics gylphs.
            cb.setTextMatrix(1, 0, PdfLogicalPageDrawable.ITALIC_ANGLE, 1, x1 + translateX, y);
        } else {
            cb.setTextMatrix(x1 + translateX, y);
        }
    } else {
        cb.setTextMatrix(x1 + translateX, y);
    }

    final OutputProcessorMetaData metaData = getMetaData();
    final GlyphList gs = renderableText.getGlyphs();
    final int offset = renderableText.getOffset();

    final CodePointBuffer codePointBuffer = getCodePointBuffer();
    if (metaData.isFeatureSupported(OutputProcessorFeature.FAST_FONTRENDERING)
            && isNormalTextSpacing(renderableText)) {
        final int maxLength = renderableText.computeMaximumTextSize(contentX2);
        final String text = gs.getText(renderableText.getOffset(), maxLength, codePointBuffer);

        cb.showText(text);
    } else {
        final PdfTextArray textArray = new PdfTextArray();
        final StringBuilder buffer = new StringBuilder(gs.getSize());
        final int maxPos = offset + renderableText.computeMaximumTextSize(contentX2);

        for (int i = offset; i < maxPos; i++) {
            final Glyph g = gs.getGlyph(i);
            final Spacing spacing = g.getSpacing();
            if (i != offset) {
                final float optimum = (float) StrictGeomUtility.toFontMetricsValue(spacing.getMinimum());
                if (optimum != 0) {
                    textArray.add(buffer.toString());
                    textArray.add(-optimum / textSpec.getFontSize());
                    buffer.setLength(0);
                }
            }

            final String text = gs.getGlyphAsString(i, codePointBuffer);
            buffer.append(text);
        }
        if (buffer.length() > 0) {
            textArray.add(buffer.toString());
        }
        cb.showText(textArray);
    }
}

From source file:org.pentaho.reporting.libraries.fonts.itext.BaseFontFontMetrics.java

License:Open Source License

public BaseFontFontMetrics(final FontNativeContext record, final BaseFont baseFont, final float size) {
    if (baseFont == null) {
        throw new NullPointerException("BaseFont is invalid.");
    }//w  w w.  jav  a 2s  . c  om

    this.record = record;
    this.baseFont = baseFont;
    this.size = size;
    this.cpBuffer = new char[4];
    this.cachedWidths = new long[256 - 32];
    Arrays.fill(cachedWidths, -1);

    sizeScaled = FontStrictGeomUtility.toInternalValue(size);

    this.ascent = (long) baseFont.getFontDescriptor(BaseFont.AWT_ASCENT, sizeScaled);
    this.descent = (long) -baseFont.getFontDescriptor(BaseFont.AWT_DESCENT, sizeScaled);
    this.leading = (long) baseFont.getFontDescriptor(BaseFont.AWT_LEADING, sizeScaled);
    italicsAngle = FontStrictGeomUtility
            .toInternalValue(baseFont.getFontDescriptor(BaseFont.ITALICANGLE, size));
    maxAscent = (long) baseFont.getFontDescriptor(BaseFont.BBOXURY, sizeScaled);
    maxDescent = (long) -baseFont.getFontDescriptor(BaseFont.BBOXLLY, sizeScaled);
    maxCharAdvance = (long) baseFont.getFontDescriptor(BaseFont.AWT_MAXADVANCE, sizeScaled);

    final int[] charBBox = this.baseFont.getCharBBox('x');
    if (charBBox != null) {
        this.xHeight = (long) (charBBox[3] * size);
    }
    if (this.xHeight == 0) {
        this.xHeight = getAscent() / 2;
    }
    this.trueTypeFont = baseFont.getFontType() == BaseFont.FONT_TYPE_TT
            || baseFont.getFontType() == BaseFont.FONT_TYPE_TTUNI;
}