Search in sources :

Example 1 with PDShading

use of org.apache.pdfbox.pdmodel.graphics.shading.PDShading in project pdfbox by apache.

the class PageDrawer method shadingFill.

@Override
public void shadingFill(COSName shadingName) throws IOException {
    PDShading shading = getResources().getShading(shadingName);
    if (shading == null) {
        LOG.error("shading " + shadingName + " does not exist in resources dictionary");
        return;
    }
    Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
    Paint paint = shading.toPaint(ctm);
    paint = applySoftMaskToPaint(paint, getGraphicsState().getSoftMask());
    graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    graphics.setPaint(paint);
    graphics.setClip(null);
    lastClip = null;
    // get the transformed BBox and intersect with current clipping path
    // need to do it here and not in shading getRaster() because it may have been rotated
    PDRectangle bbox = shading.getBBox();
    if (bbox != null) {
        Area bboxArea = new Area(bbox.transform(ctm));
        bboxArea.intersect(getGraphicsState().getCurrentClippingPath());
        graphics.fill(bboxArea);
    } else {
        graphics.fill(getGraphicsState().getCurrentClippingPath());
    }
}
Also used : Area(java.awt.geom.Area) PDShading(org.apache.pdfbox.pdmodel.graphics.shading.PDShading) Matrix(org.apache.pdfbox.util.Matrix) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint)

Example 2 with PDShading

use of org.apache.pdfbox.pdmodel.graphics.shading.PDShading in project pdfbox-graphics2d by rototor.

the class PdfBoxGraphics2D method fill.

public void fill(Shape s) {
    try {
        contentStreamSaveState();
        PDShading shading = applyPaint();
        walkShape(s);
        if (shading != null) {
            /*
				 * NB: the shading fill doesn't work with shapes with zero or negative
				 * dimensions (width and/or height): in these cases a normal fill is used
				 */
            Rectangle2D r2d = s.getBounds2D();
            if ((r2d.getWidth() <= 0) || (r2d.getHeight() <= 0)) {
                /*
					 * But we apply the shading as color, we usually want to avoid that because it
					 * creates another nested XForm for that ...
					 */
                applyShadingAsColor(shading);
                contentStream.fill();
            } else {
                contentStream.clip();
                contentStream.shadingFill(shading);
            }
        } else {
            contentStream.fill();
        }
        contentStreamRestoreState();
    } catch (IOException e) {
        throwException(e);
    }
}
Also used : PDShading(org.apache.pdfbox.pdmodel.graphics.shading.PDShading) IOException(java.io.IOException)

Example 3 with PDShading

use of org.apache.pdfbox.pdmodel.graphics.shading.PDShading in project pdfbox by apache.

the class PageDrawer method intersectShadingBBox.

// checks whether this is a shading pattern and if yes,
// get the transformed BBox and intersect with current paint area
// need to do it here and not in shading getRaster() because it may have been rotated
private void intersectShadingBBox(PDColor color, Area area) throws IOException {
    if (color.getColorSpace() instanceof PDPattern) {
        PDColorSpace colorSpace = color.getColorSpace();
        PDAbstractPattern pat = ((PDPattern) colorSpace).getPattern(color);
        if (pat instanceof PDShadingPattern) {
            PDShading shading = ((PDShadingPattern) pat).getShading();
            PDRectangle bbox = shading.getBBox();
            if (bbox != null) {
                Matrix m = Matrix.concatenate(getInitialMatrix(), pat.getMatrix());
                Area bboxArea = new Area(bbox.transform(m));
                area.intersect(bboxArea);
            }
        }
    }
}
Also used : PDAbstractPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern) Area(java.awt.geom.Area) PDShading(org.apache.pdfbox.pdmodel.graphics.shading.PDShading) Matrix(org.apache.pdfbox.util.Matrix) PDPattern(org.apache.pdfbox.pdmodel.graphics.color.PDPattern) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDShadingPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDShadingPattern) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 4 with PDShading

use of org.apache.pdfbox.pdmodel.graphics.shading.PDShading in project pdfbox by apache.

the class PageDrawer method getPaint.

/**
 * Returns an AWT paint for the given PDColor.
 *
 * @param color The color to get a paint for. This can be an actual color or a pattern.
 * @throws IOException
 */
protected Paint getPaint(PDColor color) throws IOException {
    PDColorSpace colorSpace = color.getColorSpace();
    if (!(colorSpace instanceof PDPattern)) {
        float[] rgb = colorSpace.toRGB(color.getComponents());
        return new Color(clampColor(rgb[0]), clampColor(rgb[1]), clampColor(rgb[2]));
    } else {
        PDPattern patternSpace = (PDPattern) colorSpace;
        PDAbstractPattern pattern = patternSpace.getPattern(color);
        if (pattern instanceof PDTilingPattern) {
            PDTilingPattern tilingPattern = (PDTilingPattern) pattern;
            if (tilingPattern.getPaintType() == PDTilingPattern.PAINT_COLORED) {
                // colored tiling pattern
                return tilingPaintFactory.create(tilingPattern, null, null, xform);
            } else {
                // uncolored tiling pattern
                return tilingPaintFactory.create(tilingPattern, patternSpace.getUnderlyingColorSpace(), color, xform);
            }
        } else {
            PDShadingPattern shadingPattern = (PDShadingPattern) pattern;
            PDShading shading = shadingPattern.getShading();
            if (shading == null) {
                LOG.error("shadingPattern is null, will be filled with transparency");
                return new Color(0, 0, 0, 0);
            }
            return shading.toPaint(Matrix.concatenate(getInitialMatrix(), shadingPattern.getMatrix()));
        }
    }
}
Also used : PDAbstractPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern) PDTilingPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern) PDShading(org.apache.pdfbox.pdmodel.graphics.shading.PDShading) Color(java.awt.Color) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor) PDPattern(org.apache.pdfbox.pdmodel.graphics.color.PDPattern) PDShadingPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDShadingPattern) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 5 with PDShading

use of org.apache.pdfbox.pdmodel.graphics.shading.PDShading in project pdfbox by apache.

the class PDResources method getShading.

/**
 * Returns the shading resource with the given name, or null if none exists.
 *
 * @param name Name of the shading resource.
 * @throws IOException if something went wrong.
 */
public PDShading getShading(COSName name) throws IOException {
    COSObject indirect = getIndirect(COSName.SHADING, name);
    if (cache != null && indirect != null) {
        PDShading cached = cache.getShading(indirect);
        if (cached != null) {
            return cached;
        }
    }
    // get the instance
    PDShading shading = null;
    COSDictionary dict = (COSDictionary) get(COSName.SHADING, name);
    if (dict != null) {
        shading = PDShading.create(dict);
    }
    if (cache != null) {
        cache.put(indirect, shading);
    }
    return shading;
}
Also used : PDShading(org.apache.pdfbox.pdmodel.graphics.shading.PDShading) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSObject(org.apache.pdfbox.cos.COSObject)

Aggregations

PDShading (org.apache.pdfbox.pdmodel.graphics.shading.PDShading)9 IOException (java.io.IOException)3 Area (java.awt.geom.Area)2 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)2 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)2 PDPattern (org.apache.pdfbox.pdmodel.graphics.color.PDPattern)2 PDAbstractPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern)2 PDShadingPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDShadingPattern)2 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)2 Matrix (org.apache.pdfbox.util.Matrix)2 Color (java.awt.Color)1 Paint (java.awt.Paint)1 TexturePaint (java.awt.TexturePaint)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSName (org.apache.pdfbox.cos.COSName)1 COSObject (org.apache.pdfbox.cos.COSObject)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)1 PDTilingPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern)1 PreflightPath (org.apache.pdfbox.preflight.PreflightPath)1