Example usage for com.itextpdf.kernel.colors DeviceGray DeviceGray

List of usage examples for com.itextpdf.kernel.colors DeviceGray DeviceGray

Introduction

In this page you can find the example usage for com.itextpdf.kernel.colors DeviceGray DeviceGray.

Prototype

public DeviceGray(float value) 

Source Link

Document

Creates DeviceGray color by given grayscale.

Usage

From source file:org.reactome.server.tools.diagram.exporter.raster.itext.awt.PdfGraphics2D.java

License:Open Source License

private void setPaint(boolean fill) {
    if (paint instanceof Color) {
        Color color = (Color) paint;
        int alpha = color.getAlpha();
        if (fill) {
            if (alpha != currentFillGState) {
                currentFillGState = alpha;
                PdfExtGState gs = fillGState[alpha];
                if (gs == null) {
                    gs = new PdfExtGState();
                    gs.setFillOpacity(alpha / 255f);
                    fillGState[alpha] = gs;
                }//from w w  w.  j a  v a  2 s.c om
                canvas.setExtGState(gs);
            }
            canvas.setFillColor(prepareColor(color));
        } else {
            if (alpha != currentStrokeGState) {
                currentStrokeGState = alpha;
                PdfExtGState gs = strokeGState[alpha];
                if (gs == null) {
                    gs = new PdfExtGState();
                    gs.setStrokeOpacity(alpha / 255f);
                    strokeGState[alpha] = gs;
                }
                canvas.setExtGState(gs);
            }
            canvas.setStrokeColor(prepareColor(color));
        }
    } else if (paint instanceof GradientPaint) {
        final GradientPaint gp = (GradientPaint) paint;
        final Point2D p1 = gp.getPoint1();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getPoint2();
        transform.transform(p2, p2);
        final Color c1 = gp.getColor1();
        final Color c2 = gp.getColor2();
        final PdfShading.Axial shading = new PdfShading.Axial(DeviceRgb.RED.getColorSpace(), (float) p1.getX(),
                normalizeY((float) p1.getY()), c1.getRGBComponents(null), (float) p2.getX(),
                normalizeY((float) p2.getY()), c2.getRGBComponents(null));
        final PdfPattern.Shading pattern = new PdfPattern.Shading(shading);
        if (fill)
            canvas.setFillColorShading(pattern);
        else
            canvas.setStrokeColorShading(pattern);
    } else if (paint instanceof LinearGradientPaint) {
        final LinearGradientPaint gp = (LinearGradientPaint) paint;
        final Point2D p1 = gp.getStartPoint();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getEndPoint();
        transform.transform(p2, p2);
        final float[][] colors = Arrays.stream(gp.getColors()).map(color -> color.getColorComponents(null))
                .toArray(float[][]::new);
        final AxialShading shading = new AxialShading(DeviceRgb.RED.getColorSpace(), (float) p1.getX(),
                normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), colors);
        //         final PdfShading.Axial shading = new PdfShading.Axial(DeviceRgb.RED.getColorSpace(), (float) p1.getX(), normalizeY((float) p1.getY()), c1.getRGBComponents(null), (float) p2.getX(), normalizeY((float) p2.getY()), c2.getRGBComponents(null));
        final PdfPattern.Shading pattern = new PdfPattern.Shading(shading);
        if (fill)
            canvas.setFillColorShading(pattern);
        else
            canvas.setStrokeColorShading(pattern);
    } else if (paint instanceof TexturePaint) {
        //         try {
        //            TexturePaint tp = (TexturePaint) paint;
        //            BufferedImage img = tp.getImage();
        //            Rectangle2D rect = tp.getAnchorRect();
        //            com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(img, null);
        //            PdfPatternPainter pattern = canvas.createPattern(image.getWidth(), image.getHeight());
        //            AffineTransform inverse = this.normalizeMatrix();
        //            inverse.translate(rect.getX(), rect.getY());
        //            inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
        //            double[] mx = new double[6];
        //            inverse.getMatrix(mx);
        //            pattern.setPatternMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]);
        //            image.setAbsolutePosition(0, 0);
        //            pattern.addImage(image);
        //            if (fill)
        //               canvas.setPatternFill(pattern);
        //            else
        //               canvas.setPatternStroke(pattern);
        //         } catch (Exception ex) {
        //            if (fill)
        //               canvas.setColorFill(BaseColor.GRAY);
        //            else
        //               canvas.setColorStroke(BaseColor.GRAY);
        //         }
    } else {
        try {
            BufferedImage img = null;
            int type = BufferedImage.TYPE_4BYTE_ABGR;
            if (paint.getTransparency() == Transparency.OPAQUE) {
                type = BufferedImage.TYPE_3BYTE_BGR;
            }
            img = new BufferedImage((int) width, (int) height, type);
            Graphics2D g = (Graphics2D) img.getGraphics();
            g.transform(transform);
            AffineTransform inv = transform.createInverse();
            Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight());
            fillRect = inv.createTransformedShape(fillRect);
            g.setPaint(paint);
            g.fill(fillRect);
            g.dispose();
        } catch (Exception ex) {
            if (fill)
                canvas.setFillColor(new DeviceGray(0.5f));
            else
                canvas.setStrokeColor(new DeviceGray(0.5f));
        }
    }
}