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

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

Introduction

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

Prototype

int AWT_DESCENT

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

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  av  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.
 * /*  ww w. j a  va  2 s .c  o m*/
 * @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.eclipse.birt.report.engine.layout.pdf.font.FontInfo.java

License:Open Source License

protected void setupFontSize() {

    if (bf == null) {
        lineWidth = 1;/*from  w  ww  . j av a  2 s .c o  m*/
        fontHeight = fontSize;
        baselinePosition = fontSize;
        underlinePosition = fontSize;
        linethroughPosition = fontSize / 2;
        overlinePosition = 0;
        return;
    }

    float ascent = bf.getFontDescriptor(BaseFont.AWT_ASCENT, fontSize);
    float descent = bf.getFontDescriptor(BaseFont.AWT_DESCENT, fontSize);
    if (BaseFont.FONT_TYPE_T1 == bf.getFontType()) {
        // uses the FontBBox for Type1 font, refer to the implementation
        // of free type API
        ascent = bf.getFontDescriptor(BaseFont.BBOXURY, fontSize);
        descent = bf.getFontDescriptor(BaseFont.BBOXLLY, fontSize);
    }
    if (descent > 0) {
        // In some cases, the Type1 font (perhaps loading from PFM?) return
        // positive descent
        descent = -descent;
    }
    float baseline = bf.getFontDescriptor(BaseFont.UNDERLINE_POSITION, fontSize);
    float baseline_thickness = bf.getFontDescriptor(BaseFont.UNDERLINE_THICKNESS, fontSize);
    float strike = bf.getFontDescriptor(BaseFont.STRIKETHROUGH_POSITION, fontSize);
    float strike_thickness = bf.getFontDescriptor(BaseFont.STRIKETHROUGH_THICKNESS, fontSize);

    lineWidth = baseline_thickness;
    if (lineWidth == 0) {
        lineWidth = strike_thickness;
        if (lineWidth == 0) {
            lineWidth = fontSize / 20;
        }
    }
    fontHeight = ascent - descent;
    //TODO: the -lineWidth/2 should be move to the draw function
    baselinePosition = ascent - lineWidth / 2;
    underlinePosition = ascent - baseline - lineWidth / 2;
    if (strike == 0) {
        linethroughPosition = fontHeight / 2 - lineWidth / 2;
    } else {
        linethroughPosition = ascent - strike - lineWidth / 2;
    }
    //TODO: overline is not same with the HTML, we need change it in future.
    overlinePosition = 0;
}

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 av a 2s  .  c  o  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.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 . j  a va  2  s.  co  m*/

    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;
}