org.apache.pdfbox.pdmodel.graphics.state.RenderingMode Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.state.RenderingMode. 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: SetTextRenderingMode.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    if (arguments.isEmpty())
    {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSNumber))
    {
        return;
    }
    COSNumber mode = (COSNumber) base0;
    int val = mode.intValue();
    if (val < 0 || val >= RenderingMode.values().length)
    {
        return;
    }
    RenderingMode renderingMode = RenderingMode.fromInt(val);
    context.getGraphicsState().getTextState().setRenderingMode(renderingMode);
}
 
Example #3
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected void showType3Glyph(Matrix textRenderingMatrix, PDType3Font font, int code,
        String unicode, Vector displacement) throws IOException
{
    PDGraphicsState state = getGraphicsState();
    RenderingMode renderingMode = state.getTextState().getRenderingMode();
    if (!RenderingMode.NEITHER.equals(renderingMode))
    {
        super.showType3Glyph(textRenderingMatrix, font, code, unicode, displacement);
    }
}
 
Example #4
Source File: ColorTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException
{
    for (TextPosition textPosition: textPositions)
    {
        RenderingMode charRenderingMode = renderingMode.get(textPosition);
        float[] charStrokingColor = strokingColor.get(textPosition);
        float[] charNonStrokingColor = nonStrokingColor.get(textPosition);

        StringBuilder textBuilder = new StringBuilder();
        textBuilder.append(textPosition.getUnicode())
                   .append("{");

        if (FILLING_MODES.contains(charRenderingMode))
        {
            textBuilder.append("FILL:")
                       .append(toString(charNonStrokingColor))
                       .append(';');
        }
        
        if (STROKING_MODES.contains(charRenderingMode))
        {
            textBuilder.append("STROKE:")
                       .append(toString(charStrokingColor))
                       .append(';');
        }

        if (CLIPPING_MODES.contains(charRenderingMode))
        {
            textBuilder.append("CLIP;");
        }

        textBuilder.append("}");
        writeString(textBuilder.toString());
    }
}
 
Example #5
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);
        }
    }
}
 
Example #6
Source File: PDFBoxTree.java    From Pdf2Dom with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean isTextStrokeEnabled()
{
    RenderingMode mode = getGraphicsState().getTextState().getRenderingMode();
    return mode == STROKE || mode == STROKE_CLIP || mode == FILL_STROKE || mode == FILL_STROKE_CLIP;
}
 
Example #7
Source File: PDFBoxTree.java    From Pdf2Dom with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean isTextFillEnabled()
{
    RenderingMode mode = getGraphicsState().getTextState().getRenderingMode();
    return mode == FILL || mode == FILL_CLIP || mode == FILL_STROKE || mode == FILL_STROKE_CLIP;
}
 
Example #8
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Set the text rendering mode. This determines whether showing text shall cause glyph outlines
 * to be stroked, filled, used as a clipping boundary, or some combination of the three.
 *
 * @param rm The text rendering mode.
 * @throws IOException If the content stream could not be written.
 */
public void setRenderingMode(RenderingMode rm) throws IOException
{
    writeOperand(rm.intValue());
    writeOperator(OperatorName.SET_TEXT_RENDERINGMODE);
}
 
Example #9
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Set the text rendering mode. This determines whether showing text shall cause glyph outlines
 * to be stroked, filled, used as a clipping boundary, or some combination of the three.
 *
 * @param rm The text rendering mode.
 * @throws IOException If the content stream could not be written.
 */
public void setRenderingMode(RenderingMode rm) throws IOException
{
    writeOperand(rm.intValue());
    writeOperator(OperatorName.SET_TEXT_RENDERINGMODE);
}