Example usage for com.lowagie.text.pdf PdfContentByte TEXT_RENDER_MODE_FILL

List of usage examples for com.lowagie.text.pdf PdfContentByte TEXT_RENDER_MODE_FILL

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte TEXT_RENDER_MODE_FILL.

Prototype

int TEXT_RENDER_MODE_FILL

To view the source code for com.lowagie.text.pdf PdfContentByte TEXT_RENDER_MODE_FILL.

Click Source Link

Document

A possible text rendering value

Usage

From source file:br.org.archimedes.io.pdf.elements.TextExporter.java

License:Open Source License

public void exportElement(Text text, Object outputObject) throws IOException {

    PDFWriterHelper helper = (PDFWriterHelper) outputObject;
    PdfContentByte cb = helper.getPdfContentByte();

    Point lowerLeft = text.getLowerLeft();
    Point docPoint = helper.modelToDocument(lowerLeft);

    BaseFont font = null;//from   w  w  w . j  a  va 2  s .  co m
    try {
        font = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    } catch (DocumentException e) {
        // Problems creating the font. This means the current
        // platform does not support this encoding or font.
        System.err.println(Messages.TextExporter_FontCreatingError);
        e.printStackTrace();
    }
    cb.setFontAndSize(font, (float) text.getSize());
    cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
    cb.beginText();
    cb.moveText((float) docPoint.getX(), (float) docPoint.getY());
    double angle = 0;
    try {
        angle = Geometrics.calculateAngle(new Point(0, 0), text.getDirection().getPoint());
    } catch (NullArgumentException e) {
        // Shouldn't happen since the text MUST have a direction to exists
        // and the point 0,0 is valid
        e.printStackTrace();
    }
    float degreeAngle = (float) (angle * 180 / Math.PI);
    cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text.getText(), (float) docPoint.getX(),
            (float) docPoint.getY(), degreeAngle);
    cb.endText();
}

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

License:Open Source License

@Override
public void drawGlyphVector(GlyphVector glyphVector, float x, float y) {
    Font awtFont = glyphVector.getFont();
    Map<Attribute, Object> fontAttrs = new HashMap<Attribute, Object>();
    Map<TextAttribute, ?> awtFontAttributes = awtFont.getAttributes();
    fontAttrs.putAll(awtFontAttributes);

    //the following relies on FontInfo.getFontInfo matching the face/font name
    com.lowagie.text.Font currentFont = pdfExporter.getFont(fontAttrs, locale, false);
    boolean bold = (currentFont.getStyle() & com.lowagie.text.Font.BOLD) != 0;
    boolean italic = (currentFont.getStyle() & com.lowagie.text.Font.ITALIC) != 0;

    PdfContentByte text = pdfContentByte.getDuplicate();
    text.beginText();//from w  ww. j  ava 2  s .c o m

    float[] originalCoords = new float[] { x, y };
    float[] transformedCoors = new float[2];
    getTransform().transform(originalCoords, 0, transformedCoors, 0, 1);
    text.setTextMatrix(1, 0, italic ? ITALIC_ANGLE : 0f, 1, transformedCoors[0],
            pdfExporter.getCurrentPageFormat().getPageHeight() - transformedCoors[1]);

    double scaleX = awtFont.getTransform().getScaleX();
    double scaleY = awtFont.getTransform().getScaleY();
    double minScale = Math.min(scaleX, scaleY);
    text.setFontAndSize(currentFont.getBaseFont(), (float) (minScale * awtFont.getSize2D()));

    if (bold) {
        text.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
        text.setLineWidth(currentFont.getSize() * BOLD_STRIKE_FACTOR);
        text.setColorStroke(getColor());
    }

    text.setColorFill(getColor());
    //FIXME find a way to determine the characters that correspond to this glyph vector
    // so that we can map the font glyphs that do not directly map to a character
    text.showText(glyphVector);
    text.resetRGBColorFill();

    if (bold) {
        text.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
        text.setLineWidth(1f);
        text.resetRGBColorStroke();
    }

    text.endText();
    pdfContentByte.add(text);
}

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

License:Open Source License

@Override
public void drawString(final String s, final float x, float y) {
    if (s.length() == 0) {
        return;//from ww  w .  j  a v  a  2 s.  c  o m
    }
    setFillPaint();
    setStrokePaint();

    final AffineTransform at = getTransform();
    final AffineTransform at2 = getTransform();
    at2.translate(x, y);
    at2.concatenate(font.getTransform());
    setTransform(at2);
    final AffineTransform inverse = this.normalizeMatrix();
    final AffineTransform flipper = FLIP_TRANSFORM;
    inverse.concatenate(flipper);
    final double[] mx = new double[6];
    inverse.getMatrix(mx);
    cb.beginText();

    final float fontSize = font.getSize2D();
    if (lastBaseFont == null) {
        final String fontName = font.getName();
        final boolean bold = font.isBold();
        final boolean italic = font.isItalic();

        final BaseFontFontMetrics fontMetrics = metaData.getBaseFontFontMetrics(fontName, fontSize, bold,
                italic, null, metaData.isFeatureSupported(OutputProcessorFeature.EMBED_ALL_FONTS), false);
        final FontNativeContext nativeContext = fontMetrics.getNativeContext();
        lastBaseFont = fontMetrics.getBaseFont();

        cb.setFontAndSize(lastBaseFont, fontSize);
        if (fontMetrics.isTrueTypeFont() && bold && nativeContext.isNativeBold() == false) {
            final float strokeWidth = font.getSize2D() / 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);
        }
    } else {
        cb.setFontAndSize(lastBaseFont, fontSize);
    }

    cb.setTextMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]);
    double width = 0;
    if (fontSize > 0) {
        final float scale = 1000 / fontSize;
        final Font font = this.font.deriveFont(AffineTransform.getScaleInstance(scale, scale));
        final Rectangle2D stringBounds = font.getStringBounds(s, getFontRenderContext());
        width = stringBounds.getWidth() / scale;
    }
    if (s.length() > 1) {
        final float adv = ((float) width - lastBaseFont.getWidthPoint(s, fontSize)) / (s.length() - 1);
        cb.setCharacterSpacing(adv);
    }
    cb.showText(s);
    if (s.length() > 1) {
        cb.setCharacterSpacing(0);
    }
    cb.endText();
    setTransform(at);
    if (underline) {
        // These two are supposed to be taken from the .AFM file
        // int UnderlinePosition = -100;
        final int UnderlineThickness = 50;
        //
        final double d = PdfGraphics2D.asPoints(UnderlineThickness, (int) fontSize);
        setStroke(new BasicStroke((float) d));
        y = (float) ((y) + PdfGraphics2D.asPoints((UnderlineThickness), (int) fontSize));
        final Line2D line = new Line2D.Double(x, y, (width + x), y);
        draw(line);
    }
}

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   ww  w.  j  ava2 s. c  o m
    }

    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.xhtmlrenderer.pdf.ITextOutputDevice.java

License:Open Source License

public void drawString(String s, float x, float y, JustificationInfo info) {
    if (Configuration.isTrue("xr.renderer.replace-missing-characters", false)) {
        s = replaceMissingCharacters(s);
    }/*from w w w.j a v  a  2s . c  o m*/
    if (s.length() == 0)
        return;
    PdfContentByte cb = _currentPage;
    ensureFillColor();
    AffineTransform at = (AffineTransform) getTransform().clone();
    at.translate(x, y);
    AffineTransform inverse = normalizeMatrix(at);
    AffineTransform flipper = AffineTransform.getScaleInstance(1, -1);
    inverse.concatenate(flipper);
    inverse.scale(_dotsPerPoint, _dotsPerPoint);
    double[] mx = new double[6];
    inverse.getMatrix(mx);
    cb.beginText();
    // Check if bold or italic need to be emulated
    boolean resetMode = false;
    FontDescription desc = _font.getFontDescription();
    float fontSize = _font.getSize2D() / _dotsPerPoint;
    cb.setFontAndSize(desc.getFont(), fontSize);
    float b = (float) mx[1];
    float c = (float) mx[2];
    FontSpecification fontSpec = getFontSpecification();
    if (fontSpec != null) {
        int need = ITextFontResolver.convertWeightToInt(fontSpec.fontWeight);
        int have = desc.getWeight();

        if (need > have) {
            cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
            float lineWidth = fontSize * 0.04f; // 4% of font size
            cb.setLineWidth(lineWidth);
            resetMode = true;
            ensureStrokeColor();
        }
        if ((fontSpec.fontStyle == IdentValue.ITALIC) && (desc.getStyle() != IdentValue.ITALIC)) {
            b = 0f;
            c = 0.21256f;
        }
    }
    cb.setTextMatrix((float) mx[0], b, c, (float) mx[3], (float) mx[4], (float) mx[5]);
    if (info == null) {
        cb.showText(s);
    } else {
        PdfTextArray array = makeJustificationArray(s, info);
        cb.showText(array);
    }
    if (resetMode) {
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
        cb.setLineWidth(1);
    }
    cb.endText();
}