Search in sources :

Example 11 with PDGraphicsState

use of org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState in project pdfbox by apache.

the class PageDrawer method drawGlyph.

/**
 * Renders a glyph.
 *
 * @param path the GeneralPath for the glyph
 * @param font the font
 * @param code character code
 * @param displacement the glyph's displacement (advance)
 * @param at the transformation
 * @throws IOException if something went wrong
 */
private void drawGlyph(GeneralPath path, PDFont font, int code, Vector displacement, AffineTransform at) throws IOException {
    PDGraphicsState state = getGraphicsState();
    RenderingMode renderingMode = state.getTextState().getRenderingMode();
    if (path != null) {
        // TODO: How should vertical fonts be handled?
        if (!font.isEmbedded() && !font.isVertical() && !font.isStandard14() && font.hasExplicitWidth(code)) {
            float fontWidth = font.getWidthFromFont(code);
            if (// ignore spaces
            fontWidth > 0 && Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001) {
                float pdfWidth = displacement.getX() * 1000;
                at.scale(pdfWidth / fontWidth, 1);
            }
        }
        // render glyph
        Shape glyph = at.createTransformedShape(path);
        if (renderingMode.isFill()) {
            graphics.setComposite(state.getNonStrokingJavaComposite());
            graphics.setPaint(getNonStrokingPaint());
            setClip();
            graphics.fill(glyph);
        }
        if (renderingMode.isStroke()) {
            graphics.setComposite(state.getStrokingJavaComposite());
            graphics.setPaint(getStrokingPaint());
            graphics.setStroke(getStroke());
            setClip();
            graphics.draw(glyph);
        }
        if (renderingMode.isClip()) {
            textClippings.add(glyph);
        }
    }
}
Also used : RenderingMode(org.apache.pdfbox.pdmodel.graphics.state.RenderingMode) Shape(java.awt.Shape) PDGraphicsState(org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState)

Aggregations

PDGraphicsState (org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState)11 Matrix (org.apache.pdfbox.util.Matrix)7 PDResources (org.apache.pdfbox.pdmodel.PDResources)5 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)3 Shape (java.awt.Shape)2 Rectangle2D (java.awt.geom.Rectangle2D)2 RenderingMode (org.apache.pdfbox.pdmodel.graphics.state.RenderingMode)2 BasicStroke (java.awt.BasicStroke)1 Paint (java.awt.Paint)1 Point (java.awt.Point)1 TexturePaint (java.awt.TexturePaint)1 GeneralPath (java.awt.geom.GeneralPath)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TrueTypeFont (org.apache.fontbox.ttf.TrueTypeFont)1 BoundingBox (org.apache.fontbox.util.BoundingBox)1 SetMatrix (org.apache.pdfbox.contentstream.operator.state.SetMatrix)1 COSString (org.apache.pdfbox.cos.COSString)1 PDCIDFont (org.apache.pdfbox.pdmodel.font.PDCIDFont)1