Search in sources :

Example 11 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PageDrawer method hasBlendMode.

private boolean hasBlendMode(PDTransparencyGroup group, Set<COSBase> groupsDone) {
    if (groupsDone.contains(group.getCOSObject())) {
        // The group was already processed. Avoid endless recursion.
        return false;
    }
    groupsDone.add(group.getCOSObject());
    PDResources resources = group.getResources();
    if (resources == null) {
        return false;
    }
    for (COSName name : resources.getExtGStateNames()) {
        PDExtendedGraphicsState extGState = resources.getExtGState(name);
        if (extGState == null) {
            continue;
        }
        BlendMode blendMode = extGState.getBlendMode();
        if (blendMode != BlendMode.NORMAL) {
            return true;
        }
    }
    // Recursively process nested transparency groups
    for (COSName name : resources.getXObjectNames()) {
        PDXObject xObject;
        try {
            xObject = resources.getXObject(name);
        } catch (IOException ex) {
            continue;
        }
        if (xObject instanceof PDTransparencyGroup && hasBlendMode((PDTransparencyGroup) xObject, groupsDone)) {
            return true;
        }
    }
    return false;
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSName(com.tom_roush.pdfbox.cos.COSName) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) BlendMode(com.tom_roush.pdfbox.pdmodel.graphics.blend.BlendMode) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 12 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method processType3Stream.

/**
 * Processes a Type 3 character stream.
 *
 * @param charProc Type 3 character procedure
 * @param textRenderingMatrix the Text Rendering Matrix
 * @throws IOException if there is an error reading or parsing the character content stream.
 */
protected void processType3Stream(PDType3CharProc charProc, Matrix textRenderingMatrix) throws IOException {
    if (currentPage == null) {
        throw new IllegalStateException("No current page, call " + "#processChildStream(PDContentStream, PDPage) instead");
    }
    PDResources parent = pushResources(charProc);
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();
    // replace the CTM with the TRM
    getGraphicsState().setCurrentTransformationMatrix(textRenderingMatrix);
    // transform the CTM using the stream's matrix (this is the FontMatrix)
    getGraphicsState().getCurrentTransformationMatrix().concatenate(charProc.getMatrix());
    // note: we don't clip to the BBox as it is often wrong, see PDFBOX-1917
    // save text matrices (Type 3 stream may contain BT/ET, see PDFBOX-2137)
    Matrix textMatrixOld = textMatrix;
    textMatrix = new Matrix();
    Matrix textLineMatrixOld = textLineMatrix;
    textLineMatrix = new Matrix();
    processStreamOperators(charProc);
    // restore text matrices
    textMatrix = textMatrixOld;
    textLineMatrix = textLineMatrixOld;
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState)

Example 13 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method processStream.

/**
 * Process a content stream.
 *
 * @param contentStream the content stream
 * @throws IOException if there is an exception while processing the stream
 */
private void processStream(PDContentStream contentStream) throws IOException {
    PDResources parent = pushResources(contentStream);
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();
    Matrix parentMatrix = initialMatrix;
    // transform the CTM using the stream's matrix
    getGraphicsState().getCurrentTransformationMatrix().concatenate(contentStream.getMatrix());
    // the stream's initial matrix includes the parent CTM, e.g. this allows a scaled form
    initialMatrix = getGraphicsState().getCurrentTransformationMatrix().clone();
    // clip to bounding box
    PDRectangle bbox = contentStream.getBBox();
    clipToRect(bbox);
    processStreamOperators(contentStream);
    initialMatrix = parentMatrix;
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState)

Example 14 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method processTransparencyGroup.

/**
 * Processes a transparency group stream.
 *
 * @param group the transparency group.
 *
 * @throws IOException
 */
protected void processTransparencyGroup(PDTransparencyGroup group) throws IOException {
    if (currentPage == null) {
        throw new IllegalStateException("No current page, call " + "#processChildStream(PDContentStream, PDPage) instead");
    }
    PDResources parent = pushResources(group);
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();
    Matrix parentMatrix = initialMatrix;
    // the stream's initial matrix includes the parent CTM, e.g. this allows a scaled form
    initialMatrix = getGraphicsState().getCurrentTransformationMatrix().clone();
    // transform the CTM using the stream's matrix
    getGraphicsState().getCurrentTransformationMatrix().concatenate(group.getMatrix());
    // Before execution of the transparency group XObject’s content stream,
    // the current blend mode in the graphics state shall be initialized to Normal,
    // the current stroking and nonstroking alpha constants to 1.0, and the current soft mask to None.
    getGraphicsState().setBlendMode(BlendMode.NORMAL);
    getGraphicsState().setAlphaConstant(1);
    getGraphicsState().setNonStrokeAlphaConstant(1);
    getGraphicsState().setSoftMask(null);
    // clip to bounding box
    clipToRect(group.getBBox());
    processStreamOperators(group);
    initialMatrix = parentMatrix;
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState)

Example 15 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method processTilingPattern.

/**
 * Process the given tiling pattern. Allows the pattern matrix to be overridden for custom
 * rendering.
 *
 * @param tilingPattern the tiling pattern
 * @param color color to use, if this is an uncoloured pattern, otherwise null.
 * @param colorSpace color space to use, if this is an uncoloured pattern, otherwise null.
 * @param patternMatrix the pattern matrix, may be overridden for custom rendering.
 * @throws IOException if there is an error reading or parsing the tiling pattern content stream.
 */
protected final void processTilingPattern(PDTilingPattern tilingPattern, PDColor color, PDColorSpace colorSpace, Matrix patternMatrix) throws IOException {
    PDResources parent = pushResources(tilingPattern);
    Matrix parentMatrix = initialMatrix;
    initialMatrix = Matrix.concatenate(initialMatrix, patternMatrix);
    // save the original graphics state
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();
    // save a clean state (new clipping path, line path, etc.)
    RectF bbox = new RectF();
    tilingPattern.getBBox().transform(patternMatrix).computeBounds(bbox, true);
    PDRectangle rect = new PDRectangle((float) bbox.left, (float) bbox.top, (float) bbox.width(), (float) bbox.height());
    graphicsStack.push(new PDGraphicsState(rect));
    // non-colored patterns have to be given a color
    if (colorSpace != null) {
        color = new PDColor(color.getComponents(), colorSpace);
        getGraphicsState().setNonStrokingColorSpace(colorSpace);
        getGraphicsState().setNonStrokingColor(color);
        getGraphicsState().setStrokingColorSpace(colorSpace);
        getGraphicsState().setStrokingColor(color);
    }
    // transform the CTM using the stream's matrix
    getGraphicsState().getCurrentTransformationMatrix().concatenate(patternMatrix);
    // clip to bounding box
    clipToRect(tilingPattern.getBBox());
    processStreamOperators(tilingPattern);
    initialMatrix = parentMatrix;
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
Also used : RectF(android.graphics.RectF) Matrix(com.tom_roush.pdfbox.util.Matrix) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Aggregations

PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)44 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)13 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)13 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)11 COSName (com.tom_roush.pdfbox.cos.COSName)9 Matrix (com.tom_roush.pdfbox.util.Matrix)9 IOException (java.io.IOException)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 File (java.io.File)7 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)5 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)4 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)4 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)4 PDGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState)4 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)4 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)3 PDStream (com.tom_roush.pdfbox.pdmodel.common.PDStream)3