Java Code Examples for org.apache.pdfbox.pdmodel.graphics.state.RenderingMode#isClip()

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.state.RenderingMode#isClip() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * End buffering the text clipping path, if any.
 */
private void endTextClip()
{
    PDGraphicsState state = getGraphicsState();
    RenderingMode renderingMode = state.getTextState().getRenderingMode();
    
    // apply the buffered clip as one area
    if (renderingMode.isClip() && !textClippings.isEmpty())
    {
        // PDFBOX-4150: this is much faster than using textClippingArea.add(new Area(glyph))
        // https://stackoverflow.com/questions/21519007/fast-union-of-shapes-in-java
        GeneralPath path = new GeneralPath();
        for (Shape shape : textClippings)
        {
            path.append(shape, false);
        }
        state.intersectClippingPath(path);
        textClippings = new ArrayList<Shape>();

        // PDFBOX-3681: lastClip needs to be reset, because after intersection it is still the same 
        // object, thus setClip() would believe that it is cached.
        lastClip = null;
    }
}
 
Example 2
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Render the font using the Glyph2D interface.
 * 
 * @param glyph2D the Glyph2D implementation provided a GeneralPath for each 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 drawGlyph2D(Glyph2D glyph2D, PDFont font, int code, Vector displacement,
                         AffineTransform at) throws IOException
{
    PDGraphicsState state = getGraphicsState();
    RenderingMode renderingMode = state.getTextState().getRenderingMode();

    GeneralPath path = glyph2D.getPathForCharacterCode(code);
    if (path != null)
    {
        // Stretch non-embedded glyph if it does not match the height/width contained in the PDF.
        // Vertical fonts have zero X displacement, so the following code scales to 0 if we don't skip it.
        // TODO: How should vertical fonts be handled?
        if (!font.isEmbedded() && !font.isVertical() && !font.isStandard14() && font.hasExplicitWidth(code))
        {
            float fontWidth = font.getWidthFromFont(code);
            if (fontWidth > 0 && // ignore spaces
                    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();
            if (isContentRendered())
            {
                graphics.fill(glyph);
            }
        }

        if (renderingMode.isStroke())
        {
            graphics.setComposite(state.getStrokingJavaComposite());
            graphics.setPaint(getStrokingPaint());
            graphics.setStroke(getStroke());
            setClip();
            if (isContentRendered())
            {
                graphics.draw(glyph);
            }
        }

        if (renderingMode.isClip())
        {
            textClippings.add(glyph);
        }
    }
}